-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbecomeuser_unix.go
39 lines (36 loc) · 948 Bytes
/
becomeuser_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//go:build unix || linux
package webserv
import (
"os"
"os/user"
"strconv"
"syscall"
)
// BecomeUser switches to the given userName if not empty.
//
// It sets the GID, UID and changes the USER and HOME
// environment variables accordingly. It unsets XDG_CONFIG_HOME.
//
// Returns ErrBecomeUserNotImplemented if the current OS is not supported.
func BecomeUser(userName string) error {
var err error
if userName != "" {
var u *user.User
if u, err = user.Lookup(userName); err == nil {
var uid, gid int
if uid, err = strconv.Atoi(u.Uid); err == nil {
if gid, err = strconv.Atoi(u.Gid); err == nil {
if err = syscall.Setgid(gid); err == nil {
if err = syscall.Setuid(uid); err == nil {
_ = os.Unsetenv("XDG_CONFIG_HOME")
if err = os.Setenv("HOME", u.HomeDir); err == nil {
err = os.Setenv("USER", u.Username)
}
}
}
}
}
}
}
return newErrBecomeUser(userName, err)
}