Skip to content

Commit

Permalink
Added permissions checking on cd
Browse files Browse the repository at this point in the history
  • Loading branch information
thewalla07 committed Mar 17, 2016
1 parent 21a0e07 commit d070758
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import (

func setup(r *bufio.Reader, w *bufio.Writer, thisUser *utils.User) (err error) {

reader := bufio.NewReader(os.Stdin)

fmt.Print("Please enter your username and hit enter: ")
reader := bufio.NewReader(os.Stdin)
uname, _ := reader.ReadString('\n')

thisUser.Uname = strings.TrimSpace(uname)
Expand Down Expand Up @@ -85,6 +84,7 @@ func setup(r *bufio.Reader, w *bufio.Writer, thisUser *utils.User) (err error) {
} else {

err = utils.FileToStruct(utils.GetUserHome()+"/.mdfs/client/"+uname+"/.user_data", &thisUser)
w.WriteByte(9)

}

Expand Down
2 changes: 1 addition & 1 deletion mdservice/config/mdservice_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {
}

conf := config.ParseConfiguration("./mdservice/config/mdservice_conf.json")
conf.Path = utils.GetUserHome() + "/.mdfs/mdservice"
conf.Path = utils.GetUserHome() + "/.mdfs/mdservice/"

// save the new configuration to file
err = config.SetConfiguration(conf, conf.Path+"/.mdservice_conf.json")
Expand Down
32 changes: 20 additions & 12 deletions server/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,40 +606,48 @@ func listGroupsMemberOf(uuid uint64, conn net.Conn, r *bufio.Reader, w *bufio.Wr
return
}

func checkBase(uuid uint64, targetPath string, md *MDService) (auth bool) {
func checkBase(uuid uint64, targetPath string, mod string, md *MDService) (auth bool) {

basePath := strings.TrimSuffix(targetPath, "/"+path.Base(targetPath))
return checkEntry(uuid, basePath, md)
fmt.Println("Checking basePath: " + basePath)
return checkEntry(uuid, basePath, mod, md)
}

func checkEntry(uuid uint64, targetPath, mod string, md *MDService) (auth bool) {

// check all the d in dirs for Xecute
dirs := strings.Split(targetPath, "/")
owner, groups, permissions, err := getPerm(targetPath + "/.perm")
if err != nil {
fmt.Println("No permissions file")
return false
if targetPath == "/" || targetPath == "" {
fmt.Println("Root dir")
return true
}

traverser := ""

for i, d := range dirs {
if i != 0 && i != len(dirs) {
fmt.Printf("%d, %s\n", i, d)
if i != 0 {
traverser = path.Join("/", traverser, d)
owner, groups, permissions, err := getPerm(md.getPath() + "files/" + traverser + "/")
if err != nil {
fmt.Println("NO PERM FILE AT: " + md.getPath() + "files" + traverser + "/.perm")
return false
}

fmt.Printf("%d, %s, %d\n", i, d, owner)

hasGroup := false
if owner == uuid {

return true

} else if groups != nil {

hasGroup := false

for _, g := range groups {
err = md.userDB.View(func(tx *bolt.Tx) error {

b := tx.Bucket([]byte("groups"))

v := b.Get(g)
v := b.Get(itob(g))

if v == nil {
return nil
Expand Down Expand Up @@ -673,5 +681,5 @@ func checkEntry(uuid uint64, targetPath, mod string, md *MDService) (auth bool)
}
}

return true
return false
}
41 changes: 33 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,26 @@ func createPerm(filepath string, owner uint64, groups []uint64, permissions []bo
tmpPerm.Groups = groups
tmpPerm.Permissions = permissions

fmt.Printf("%d,%v,%v", owner, groups, permissions)
fmt.Printf("%d,%v,%v", tmpPerm.Owner, tmpPerm.Groups, tmpPerm.Permissions)
fmt.Println("MAKNG PERM FILE AT", filepath+"/.perm")

return utils.StructToFile(tmpPerm, filepath+"/.perm")
}

func getPerm(filepath string) (owner uint64, groups []uint64, permissions []bool, err error) {

var tmpPerm utils.Perm

fmt.Println(filepath + "/.perm")

err = utils.FileToStruct(filepath+"/.perm", &tmpPerm)
owner = tmpPerm.Owner
groups = tmpPerm.Groups
permissions = tmpPerm.Permissions

fmt.Printf("%d,%v,%v", owner, groups, permissions)

return
}

Expand All @@ -475,15 +483,31 @@ func handleRequest(conn net.Conn, in TCPServer) (err error) {
// var code uint8
fmt.Println("Ready to read code")

// read in the handling code from the connected client
code, err := r.ReadByte()

var uintUuid uint64

// is this a new user?
if code == 10 {

fmt.Println("new user")
in.handleCode(0, code, conn, r, w)

} else {

fmt.Println("Existing user")

}

uuid, _ := r.ReadString('\n')
uintUuid, err := strconv.ParseUint(strings.TrimSpace(uuid), 10, 64)
uintUuid, err = strconv.ParseUint(strings.TrimSpace(uuid), 10, 64)
if err != nil {
conn.Close()
return
return err
}

// read in the handling code from the connected client
code, err := r.ReadByte()
code, err = r.ReadByte()
// as long as there is no error in the code reading in..
for code != 0 {

Expand Down Expand Up @@ -557,7 +581,7 @@ func ls(uuid uint64, conn net.Conn, r *bufio.Reader, w *bufio.Writer, md *MDServ

} else {

checkEntry(uuid, targetPath, md)
checkBase(uuid, targetPath, "r", md)

msg = msg + targetPath + ":," // note comma to denote newline
for _, file := range files {
Expand Down Expand Up @@ -610,7 +634,7 @@ func mkdir(uuid uint64, conn net.Conn, r *bufio.Reader, w *bufio.Writer, md *MDS

// MkdirAll creates an entire file path if some dirs are missing

if !utils.IsHidden(targetPath) && checkBase(uuid, targetPath, md) {
if !utils.IsHidden(targetPath) && checkBase(uuid, targetPath, "w", md) {
os.Mkdir(md.getPath()+"files"+targetPath, 0777)
permissions := []bool{false, false, false, false, false, false}
var groups []uint64
Expand Down Expand Up @@ -733,7 +757,7 @@ func cd(uuid uint64, conn net.Conn, r *bufio.Reader, w *bufio.Writer, md *MDServ
w.WriteByte(1)
w.Flush()

} else if !checkEntry(uuid, targetPath, md) { // success!
} else if !checkEntry(uuid, targetPath, "x", md) { // success!

fmt.Println("Access denied to dir " + targetPath)
// notify success to client (no specific code, just not 1 or 0)
Expand Down Expand Up @@ -1004,6 +1028,7 @@ func send(uuid uint64, conn net.Conn, r *bufio.Reader, w *bufio.Writer, md *MDSe
func newUser(conn net.Conn, r *bufio.Reader, w *bufio.Writer, md *MDService) (err error) {

// get the uuid for the new user

var newUser utils.User
err = md.userDB.Update(func(tx *bolt.Tx) (err error) {

Expand Down Expand Up @@ -1039,7 +1064,7 @@ func newUser(conn net.Conn, r *bufio.Reader, w *bufio.Writer, md *MDService) (er
return err
}

fmt.Println("writing uuid")
fmt.Println("writing uuid of: " + idStr)
w.WriteString(idStr + "\n")
fmt.Println("written")

Expand Down

0 comments on commit d070758

Please sign in to comment.