9f5a015f42
Updates #2419. Squashed commit of the following: commit e57e6ce56c59d4ef42b1716247fe48a86404179e Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Jun 15 19:37:15 2021 +0300 all: imp build tags
50 lines
883 B
Go
50 lines
883 B
Go
//go:build darwin || freebsd || linux || netbsd || openbsd
|
|
// +build darwin freebsd linux netbsd openbsd
|
|
|
|
package aghos
|
|
|
|
import (
|
|
"fmt"
|
|
"os/user"
|
|
"strconv"
|
|
"syscall"
|
|
)
|
|
|
|
func setGroup(groupName string) (err error) {
|
|
g, err := user.LookupGroup(groupName)
|
|
if err != nil {
|
|
return fmt.Errorf("looking up group: %w", err)
|
|
}
|
|
|
|
gid, err := strconv.Atoi(g.Gid)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing gid: %w", err)
|
|
}
|
|
|
|
err = syscall.Setgid(gid)
|
|
if err != nil {
|
|
return fmt.Errorf("setting gid: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func setUser(userName string) (err error) {
|
|
u, err := user.Lookup(userName)
|
|
if err != nil {
|
|
return fmt.Errorf("looking up user: %w", err)
|
|
}
|
|
|
|
uid, err := strconv.Atoi(u.Uid)
|
|
if err != nil {
|
|
return fmt.Errorf("parsing uid: %w", err)
|
|
}
|
|
|
|
err = syscall.Setuid(uid)
|
|
if err != nil {
|
|
return fmt.Errorf("setting uid: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|