-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (47 loc) · 999 Bytes
/
main.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"os"
"os/user"
"path"
"strconv"
"strings"
"syscall"
)
var usage string = "setuidgid: usage: setuidgid username program [arg...]"
func checkError(err error) {
if err != nil {
fmt.Printf("setuidgid: fatal: %v\n", err)
os.Exit(111)
}
}
func main() {
if len(os.Args) <= 2 {
fmt.Println(usage)
os.Exit(100)
}
username := os.Args[1]
program := os.Args[2]
pargv := os.Args[2:]
user, err := user.Lookup(username)
checkError(err)
uid, err := strconv.Atoi(user.Uid)
checkError(err)
gid, err := strconv.Atoi(user.Gid)
checkError(err)
err = syscall.Setgroups([]int{gid})
checkError(err)
err = syscall.Setgid(gid)
checkError(err)
err = syscall.Setuid(uid)
checkError(err)
if path.IsAbs(program) {
err := syscall.Exec(program, pargv, os.Environ())
checkError(err)
}
for _, p := range strings.Split(os.Getenv("PATH"), ":") {
absPath := path.Join(p, program)
err = syscall.Exec(absPath, pargv, os.Environ())
}
checkError(err)
}