Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite chuser() for simplicity and correctness #1203

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions cmd/yggdrasil/chuser_openbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//go:build openbsd
// +build openbsd

package main

import (
"fmt"
"os/user"
"strconv"
"strings"

"golang.org/x/sys/unix"
)

func chuser(input string) error {
givenUser, givenGroup, _ := strings.Cut(input, ":")

var (
err error
usr *user.User
grp *user.Group
uid, gid int
)

if usr, err = user.Lookup(givenUser); err != nil {
if usr, err = user.LookupId(givenUser); err != nil {
return err
}
}
if uid, err = strconv.Atoi(usr.Uid); err != nil {
return err
}

if givenGroup != "" {
if grp, err = user.LookupGroup(givenGroup); err != nil {
if grp, err = user.LookupGroupId(givenGroup); err != nil {
return err
}
}

gid, _ = strconv.Atoi(grp.Gid)
} else {
gid, _ = strconv.Atoi(usr.Gid)
}

if err := unix.Setgroups([]int{gid}); err != nil {
return fmt.Errorf("setgroups: %d: %v", gid, err)
}
if err := unix.Setresgid(gid, gid, gid); err != nil {
return fmt.Errorf("setresgid: %d: %v", gid, err)
}
if err := unix.Setresuid(uid, uid, uid); err != nil {
return fmt.Errorf("setresuid: %d: %v", uid, err)
}

return nil
}
4 changes: 2 additions & 2 deletions cmd/yggdrasil/chuser_unix.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || solaris
// +build aix darwin dragonfly freebsd linux netbsd solaris

package main

Expand Down
Loading