1. juster
  2. maw

Commits

juster  committed c3c6327

Change lookupSudoUser to use SUDO_GID and SUDO_UID env vars

  • Participants
  • Parent commits 7347556
  • Branches default

Comments (0)

Files changed (3)

File aur.go

View file
  • Ignore whitespace
 	}
 
 	// If we are running under sudo, we do not want our files to be owned by root.
-	sudoUser := lookupSudoUser()
-	if sudoUser != nil {
-		os.Chown(srcpath, sudoUser.Uid, sudoUser.Gid)
+	uid, gid := lookupSudoUser()
+	if uid != 0 {
+		os.Chown(srcpath, uid, gid)
 	}
 
 	srcdir, err := srcpkg.Extract(aur.buildroot)
 		return nil, FetchErrorWrap(pkgname, err)
 	}
 
-	if sudoUser != nil {
-		chownDirRec(srcdir, sudoUser.Uid, sudoUser.Gid)
+	if uid != 0 {
+		chownDirRec(srcdir, uid, gid)
 	}
 
 	pkgpaths, err := aur.builder.Build(srcdir)

File main.go

View file
  • Ignore whitespace
 
 import (
 	"os"
-	"os/user"
 	"fmt"
 	"exec"
+	"strconv"
 )
 
 type CmdOpt int
 	mopt.Targets = newtargs
 }
 
-/* This is used by other files, like in srcpkg.go and aur.go.
- * Kind of awkward placement but ohwell... */
-func lookupSudoUser() *user.User {
-	sudouser := os.Getenv("SUDO_USER")
-	if sudouser == "" {
-		return nil
+// This is used by other files, like in srcpkg.go and aur.go.
+// Kind of awkward placement but oh well...
+func lookupSudoUser() (uid, gid int) {
+	uidstr := os.Getenv("SUDO_UID")
+	if uidstr == "" {
+		return
 	}
-	userobj, _ := user.Lookup(sudouser)
-	return userobj
+	gidstr := os.Getenv("SUDO_GID")
+	if gidstr == "" {
+		return
+	}
+
+	var err os.Error
+	uid, err = strconv.Atoi(uidstr)
+	if err != nil {
+		return
+	}
+	gid, err = strconv.Atoi(gidstr)
+	if err != nil {
+		uid = 0
+		return
+	}
+	return
 }
 
 func ParseOpts(cmdopts []string) *MawOpt {

File srcpkg.go

View file
  • Ignore whitespace
 
 	// If we are running under sudo, we must chmod the tempfile to the user
 	// whom we are going to be dropping privledges to (the SUDO_USER).
-	sudouser := lookupSudoUser()
-	if sudouser == nil {
+	uid, gid := lookupSudoUser()
+	if uid == 0 {
 		return pathfile, nil
 	}
-	tmpfile.Chown(sudouser.Uid, sudouser.Gid)
+	tmpfile.Chown(uid, gid)
 	return pathfile, nil
 }