From 5147ac036412d9dc7e9f8897e6f2f91f081f1baa Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 30 Nov 2018 14:56:53 +0100 Subject: [PATCH] use Getenv instead of LookupEnv so users can set USER="" and HOME="" + lookup different username only if it is different thant the current username --- packer/config_file_unix.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/packer/config_file_unix.go b/packer/config_file_unix.go index 10337202d..5c7a31760 100644 --- a/packer/config_file_unix.go +++ b/packer/config_file_unix.go @@ -28,25 +28,21 @@ func configDir() (string, error) { } func homeDir() (string, error) { - var u *user.User - /// First prefer the HOME environmental variable - if home, ok := os.LookupEnv("HOME"); ok { + // First prefer the HOME environmental variable + if home := os.Getenv("HOME"); home != "" { log.Printf("Detected home directory from env var: %s", home) return home, nil } - /// Fall back to the passwd database if not found which follows - /// the same semantics as bourne shell - var err error + // Fall back to the passwd database if not found which follows + // the same semantics as bourne shell + u, err := user.Current() - // Check username specified in the environment first - if username, ok := os.LookupEnv("USER"); ok { + // Get homedir from specified username + // if it is set and different than what we have + if username := os.Getenv("USER"); username != "" && err == nil && u.Username != username { u, err = user.Lookup(username) - - } else { - // Otherwise we assume the current user - u, err = user.Current() } // Fail if we were unable to read the record