blob: 53ecfe3c61985cb13f5ee00651a493ab693aebbf [file] [log] [blame]
diff --git a/google_guest_agent/accounts_unix.go b/google_guest_agent/accounts_unix.go
index 701f9f1..a4ad7f9 100644
--- a/google_guest_agent/accounts_unix.go
+++ b/google_guest_agent/accounts_unix.go
@@ -23,20 +23,28 @@ import (
"syscall"
)
-func getUID(path string) string {
+func getUIDAndGID(path string) (string, string) {
if dir, err := os.Stat(path); err == nil {
if stat, ok := dir.Sys().(*syscall.Stat_t); ok {
- return fmt.Sprintf("%d", stat.Uid)
+ return fmt.Sprintf("%d", stat.Uid), fmt.Sprintf("%d", stat.Gid)
}
}
- return ""
+ return "", ""
}
-func createUser(username, uid string) error {
+func createUser(username, uid, gid string) error {
useradd := config.Section("Accounts").Key("useradd_cmd").MustString("useradd -m -s /bin/bash -p * {user}")
if uid != "" {
useradd = fmt.Sprintf("%s -u %s", useradd, uid)
}
+ if gid != "" {
+ groupadd := config.Section("Accounts").Key("groupadd_cmd").MustString("groupadd {group}")
+ groupadd = fmt.Sprintf("%s -g %s", groupadd, gid)
+ if err := runCmd(createUserGroupCmd(groupadd, "", username)); err != nil {
+ return err
+ }
+ useradd = fmt.Sprintf("%s -g %s", useradd, gid)
+ }
return runCmd(createUserGroupCmd(useradd, username, ""))
}
diff --git a/google_guest_agent/accounts_windows.go b/google_guest_agent/accounts_windows.go
index d9fefd5..9398214 100644
--- a/google_guest_agent/accounts_windows.go
+++ b/google_guest_agent/accounts_windows.go
@@ -137,7 +137,7 @@ func addUserToGroup(username, group string) error {
return nil
}
-func createUser(username, pwd string) error {
+func createUser(username, pwd, _ string) error {
uPtr, err := syscall.UTF16PtrFromString(username)
if err != nil {
return fmt.Errorf("error encoding username to UTF16: %v", err)
@@ -183,6 +183,6 @@ func userExists(name string) (bool, error) {
return true, nil
}
-func getUID(path string) string {
- return ""
+func getUIDAndGID(path string) (string, string) {
+ return "", ""
}
diff --git a/google_guest_agent/non_windows_accounts.go b/google_guest_agent/non_windows_accounts.go
index 04f8734..9ff9ab5 100644
--- a/google_guest_agent/non_windows_accounts.go
+++ b/google_guest_agent/non_windows_accounts.go
@@ -345,12 +345,12 @@ func createUserGroupCmd(cmd, user, group string) *exec.Cmd {
// createGoogleUser creates a Google managed user account if needed and adds it
// to the configured groups.
func createGoogleUser(user string) error {
- var uid string
+ var uid, gid string
if config.Section("Accounts").Key("reuse_homedir").MustBool(false) {
- uid = getUID(fmt.Sprintf("/home/%s", user))
+ uid, gid = getUIDAndGID(fmt.Sprintf("/home/%s", user))
}
- if err := createUser(user, uid); err != nil {
+ if err := createUser(user, uid, gid); err != nil {
return err
}
groups := config.Section("Accounts").Key("groups").MustString("adm,dip,docker,lxd,plugdev,video")
diff --git a/google_guest_agent/windows_accounts.go b/google_guest_agent/windows_accounts.go
index dc23831..e64f0c8 100644
--- a/google_guest_agent/windows_accounts.go
+++ b/google_guest_agent/windows_accounts.go
@@ -139,7 +139,7 @@ func (k windowsKey) createOrResetPwd() (*credsJSON, error) {
}
} else {
logger.Infof("Creating user %s", k.UserName)
- if err := createUser(k.UserName, pwd); err != nil {
+ if err := createUser(k.UserName, pwd, ""); err != nil {
return nil, fmt.Errorf("error running createUser: %v", err)
}
if k.AddToAdministrators == nil || *k.AddToAdministrators == true {