Skip to content

Commit

Permalink
files/passwd: don't shell out to id
Browse files Browse the repository at this point in the history
Use the user lookup logic we already have instead of shelling out to id
to see if users exist.

Fix up the existing logic to handle lookup errors and finding nothing
differently.
  • Loading branch information
Andrew Jeddeloh committed Oct 9, 2019
1 parent d03ab9e commit 258d72f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
4 changes: 0 additions & 4 deletions internal/distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ var (
systemConfigDir = "/usr/lib/ignition"

// Helper programs
chrootCmd = "chroot"
groupaddCmd = "groupadd"
idCmd = "id"
mdadmCmd = "mdadm"
mountCmd = "mount"
sgdiskCmd = "sgdisk"
Expand Down Expand Up @@ -71,9 +69,7 @@ func DiskByPartUUIDDir() string { return diskByPartUUIDDir }
func KernelCmdlinePath() string { return kernelCmdlinePath }
func SystemConfigDir() string { return fromEnv("SYSTEM_CONFIG_DIR", systemConfigDir) }

func ChrootCmd() string { return chrootCmd }
func GroupaddCmd() string { return groupaddCmd }
func IdCmd() string { return idCmd }
func MdadmCmd() string { return mdadmCmd }
func MountCmd() string { return mountCmd }
func SgdiskCmd() string { return sgdiskCmd }
Expand Down
16 changes: 4 additions & 12 deletions internal/exec/util/passwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/coreos/ignition/v2/config/v3_1_experimental/types"
"github.com/coreos/ignition/v2/internal/as_user"
"github.com/coreos/ignition/v2/internal/distro"
"github.com/coreos/ignition/v2/internal/log"
)

func appendIfTrue(args []string, test *bool, newargs string) []string {
Expand Down Expand Up @@ -111,18 +110,11 @@ func (u Util) EnsureUser(c types.PasswdUser) error {

// CheckIfUserExists will return Info log when user is empty
func (u Util) CheckIfUserExists(c types.PasswdUser) (bool, error) {
code := -1
cmd := exec.Command(distro.ChrootCmd(), u.DestDir, distro.IdCmd(), c.Name)
stdout, err := cmd.CombinedOutput()
_, err := u.userLookup(c.Name)
if _, ok := err.(user.UnknownUserError); ok {
return false, nil
}
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
code = exitErr.Sys().(syscall.WaitStatus).ExitStatus()
}
if code == 1 {
u.Info("checking if user \"%s\" exists: %s", c.Name, fmt.Errorf("[Attention] %v: Cmd: %s Stdout: %s", err, log.QuotedCmd(cmd), stdout))
return false, nil
}
u.Logger.Info("error encountered (%T): %v", err, err)
return false, err
}
return true, nil
Expand Down
16 changes: 14 additions & 2 deletions internal/exec/util/user_group_lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ static int user_lookup_fn(lookup_ctxt_t *ctxt) {
goto out_err;
}

if(getpwnam_r(ctxt->name, &p, buf, sizeof(buf), &pptr) != 0 || !pptr) {
if(getpwnam_r(ctxt->name, &p, buf, sizeof(buf), &pptr) != 0) {
goto out_err;
}

if (!pptr) {
// successfully found nothing
ctxt->res->name = NULL;
return 0;
}

if(!(ctxt->res->name = strdup(p.pw_name))) {
goto out_err;
}
Expand Down Expand Up @@ -98,10 +104,16 @@ static int group_lookup_fn(lookup_ctxt_t *ctxt) {
goto out_err;
}

if(getgrnam_r(ctxt->name, &g, buf, sizeof(buf), &gptr) != 0 || !gptr) {
if(getgrnam_r(ctxt->name, &g, buf, sizeof(buf), &gptr) != 0) {
goto out_err;
}

if (!gptr) {
// successfully found nothing
ctxt->res->name = NULL;
return 0;
}

if(!(ctxt->res->name = strdup(g.gr_name))) {
goto out_err;
}
Expand Down
4 changes: 2 additions & 2 deletions internal/exec/util/user_group_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (u Util) userLookup(name string) (*user.User, error) {
}

if res.name == nil {
return nil, fmt.Errorf("user %q not found", name)
return nil, user.UnknownUserError(fmt.Sprintf("user %q not found", name))
}

homedir, err := u.JoinPath(C.GoString(res.home))
Expand Down Expand Up @@ -67,7 +67,7 @@ func (u Util) groupLookup(name string) (*user.Group, error) {
}

if res.name == nil {
return nil, fmt.Errorf("user %q not found", name)
return nil, user.UnknownGroupError(fmt.Sprintf("group %q not found", name))
}

grp := &user.Group{
Expand Down

0 comments on commit 258d72f

Please sign in to comment.