aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/sid77/drop/drop.go
blob: 0fb64a94884fdecbb8ec4f135c84c01c85021ee8 (plain)
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
package drop

import (
	"os/user"
	"strconv"

	"github.com/sid77/drop/syscall"
)

func DropPrivileges(runAsUser string) (err error) {
	usr, err := user.Lookup(runAsUser)
	if err != nil {
		return err
	}

	gid, err := strconv.Atoi(usr.Gid)
	if err != nil {
		return err
	}

	uid, err := strconv.Atoi(usr.Uid)
	if err != nil {
		return err
	}

	if err = syscall.Setgid(gid); err != nil {
		return err
	}

	if err = syscall.Setuid(uid); err != nil {
		return err
	}

	return nil
}