AdGuardHome/internal/aghos/user_unix.go
Ainar Garipov 48c44c29ab Pull request: all: support setgid, setuid on unix
Updates #2763.

Squashed commit of the following:

commit bd2077c6569b53ae341a58aa73de6063d7037e8e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Jun 4 16:25:17 2021 +0300

    all: move rlimit_nofile, imp docs

commit ba95d4ab7c722bf83300d626a598aface37539ad
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Jun 4 15:12:23 2021 +0300

    all: support setgid, setuid on unix
2021-06-04 16:35:34 +03:00

50 lines
884 B
Go

// +build darwin freebsd linux netbsd openbsd
//go: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
}