-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
90 lines (83 loc) · 1.53 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"os"
"os/user"
"strconv"
"strings"
)
func main() {
// Get our user and group list
un := os.Getenv("USER")
if un == "" {
os.Exit(3)
}
gl := strings.Split(os.Getenv("GROUP"), ",")
if len(gl) == 0 || gl[0] == "" {
os.Exit(4)
}
// Get user struct, primary gid, and gid (string) array
u, err := user.Lookup(un)
if err != nil {
os.Exit(2)
}
gid, err := strconv.ParseInt(u.Gid, 10, 64)
if err != nil {
os.Exit(2)
}
gs, err := u.GroupIds()
if err != nil {
os.Exit(2)
}
// Convert gid strings to gid ints
var gids []int64
for _, v := range gs {
g, err := strconv.ParseInt(v, 10, 64)
if err == nil {
gids = append(gids, g)
}
}
// Check each of the supplied group names/gids
for _, v := range gl {
ggid, err := strconv.ParseInt(v, 10, 64)
if err == nil {
// Group is a gid
if ggid == gid {
// gid matches primary group
os.Exit(0)
} else {
for _, v := range gids {
if ggid == v {
// gid matches secondary group
os.Exit(0)
}
}
}
} else {
// Group is a group name
fmt.Printf("v: %s\n", v)
g, err := user.LookupGroup(v)
if err != nil {
// Group is invalid
continue
}
ggid, err := strconv.ParseInt(g.Gid, 10, 64)
if err != nil {
// Group doesn't exist or gid isn't numeric
continue
}
if ggid == gid {
// gid matches primary group
os.Exit(0)
} else {
for _, v := range gids {
if ggid == v {
// gid matches secondary group
os.Exit(0)
}
}
}
}
}
os.Exit(1)
}