agent: re-add support for user name in socket perms

This commit is contained in:
Ryan Uber 2015-01-20 16:21:23 -08:00
parent 145c56b47d
commit a6c877c7ee

View File

@ -9,6 +9,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
"os/user"
"runtime" "runtime"
"strconv" "strconv"
"time" "time"
@ -103,32 +104,41 @@ func stringHash(s string) string {
// on a given file. It takes a map, which defines the permissions to be set. // on a given file. It takes a map, which defines the permissions to be set.
// All permission/ownership settings are optional. If no user or group is // All permission/ownership settings are optional. If no user or group is
// specified, the current user/group will be used. Mode is optional, and has // specified, the current user/group will be used. Mode is optional, and has
// no default (the operation is not performed if absent). // no default (the operation is not performed if absent). User may be
// specified by name or ID, but group may only be specified by ID.
func setFilePermissions(path string, perms map[string]string) error { func setFilePermissions(path string, perms map[string]string) error {
var err error var err error
uid, gid := os.Getuid(), os.Getgid() uid, gid := os.Getuid(), os.Getgid()
if _, ok := perms["uid"]; ok {
if uid, err = strconv.Atoi(perms["uid"]); err != nil { if _, ok := perms["user"]; ok {
return fmt.Errorf("invalid user id specified: %v", perms["uid"]) if uid, err = strconv.Atoi(perms["user"]); err == nil {
goto GROUP
} }
// Try looking up the user by name
if u, err := user.Lookup(perms["user"]); err == nil {
uid, _ = strconv.Atoi(u.Uid)
goto GROUP
}
return fmt.Errorf("invalid user specified: %v", perms["user"])
} }
if _, ok := perms["gid"]; ok {
if gid, err = strconv.Atoi(perms["gid"]); err != nil { GROUP:
return fmt.Errorf("invalid group id specified: %v", perms["gid"]) if _, ok := perms["group"]; ok {
if gid, err = strconv.Atoi(perms["group"]); err != nil {
return fmt.Errorf("invalid group specified: %v", perms["group"])
} }
} }
if err := os.Chown(path, uid, gid); err != nil { if err := os.Chown(path, uid, gid); err != nil {
return fmt.Errorf( return fmt.Errorf("failed setting ownership to %d:%d on %q: %s",
"failed setting ownership to %d:%d on %q: %s",
uid, gid, path, err) uid, gid, path, err)
} }
if _, ok := perms["mode"]; ok { if _, ok := perms["mode"]; ok {
mode, err := strconv.ParseUint(perms["mode"], 8, 32) mode, err := strconv.ParseUint(perms["mode"], 8, 32)
if err != nil { if err != nil {
return fmt.Errorf("invalid mode specified for %q: %s", return fmt.Errorf("invalid mode specified: %v", perms["mode"])
path, perms["mode"])
} }
if err := os.Chmod(path, os.FileMode(mode)); err != nil { if err := os.Chmod(path, os.FileMode(mode)); err != nil {
return fmt.Errorf("failed setting permissions to %d on %q: %s", return fmt.Errorf("failed setting permissions to %d on %q: %s",