-
Notifications
You must be signed in to change notification settings - Fork 275
/
os_util_linux.go
213 lines (191 loc) · 5.65 KB
/
os_util_linux.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package util
import (
"fmt"
"io"
"io/fs"
"log"
"log/syslog"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
)
const siaUnixGroup = "athenz"
func NewSysLogger() (io.Writer, error) {
return syslog.New(syslog.LOG_INFO|syslog.LOG_DAEMON, "siad")
}
func UpdateFile(fileName string, contents []byte, uid, gid int, perm os.FileMode, directUpdate, verbose bool) error {
err := UpdateFileContents(fileName, contents, perm, directUpdate, verbose)
if err != nil {
return err
}
currentUid, currentGid := uidGidForUser("")
if currentUid != uid || currentGid != gid {
log.Printf("Changing file %s ownership to %d:%d...\n", fileName, uid, gid)
err = os.Chown(fileName, uid, gid)
if err != nil {
log.Printf("Cannot chown file %s to %d:%d, err: %v\n", fileName, uid, gid, err)
return err
}
}
return nil
}
func SvcAttrs(username, groupname string) (int, int, int) {
// Default file mode for service key
fileMode := 0400
// Get uid and gid for the username.
uid, gid := uidGidForUser(username)
// Override the group id if user explicitly specified the group.
ggid := -1
if groupname != "" {
ggid = gidForGroup(groupname)
}
// if the group is not specified or invalid then we'll default
// to our unix group name called athenz
if ggid == -1 {
ggid = gidForGroup(siaUnixGroup)
}
// if we have a valid value then update the gid
// otherwise use the user group id value
if ggid != -1 {
gid = ggid
}
if gid != -1 {
fileMode = 0440
}
return uid, gid, fileMode
}
func UidGidForUserGroup(username, groupname string) (int, int) {
// Get uid and gid for the username.
uid, gid := uidGidForUser(username)
// Override the group id if user explicitly specified the group.
ggid := -1
if groupname != "" {
ggid = gidForGroup(groupname)
}
// if the group is not specified or invalid then we'll default
// to our unix group name called athenz
if ggid == -1 {
ggid = gidForGroup(siaUnixGroup)
}
// if we have a valid value then update the gid
// otherwise use the user group id value
if ggid != -1 {
gid = ggid
}
return uid, gid
}
func gidForGroup(groupname string) int {
//shelling out to id is used here because the os/user package
//requires cgo, which doesn't cross-compile. we can use getent group
//command but instead we opted for a simple grep for /etc/group
cmdStr := fmt.Sprintf("^%s:", groupname)
out, err := exec.Command(GetUtilPath("grep"), cmdStr, "/etc/group").Output()
if err != nil {
log.Printf("Cannot exec '%s %s /etc/group': %v\n", GetUtilPath("grep"), groupname, err)
return -1
}
s := strings.Trim(string(out), "\n\r ")
comps := strings.Split(string(out), ":")
if len(comps) < 3 {
log.Printf("Invalid response from grep group command: %s\n", s)
return -1
}
//the group id should be the third value: 'group_name:password:group_id:group_list'
id, err := strconv.Atoi(comps[2])
if err != nil {
log.Printf("Invalid response from getent group command: %s\n", s)
return -1
}
return id
}
func idCommand(username, arg string) int {
//shelling out to id is used here because the os/user package
//requires cgo, which doesn't cross-compile
var out []byte
var err error
if username == "" {
out, err = exec.Command(GetUtilPath("id"), arg).Output()
} else {
out, err = exec.Command(GetUtilPath("id"), arg, username).Output()
}
if err != nil {
log.Fatalf("Cannot exec 'id %s %s': %v\n", arg, username, err)
}
s := strings.Trim(string(out), "\n\r ")
id, err := strconv.Atoi(s)
if err != nil {
log.Fatalf("Unexpected UID/GID format in user record: %s\n", string(out))
}
return id
}
func uidGidForUser(username string) (int, int) {
uid := idCommand(username, "-u")
gid := idCommand(username, "-g")
return uid, gid
}
func SetupSIADirs(siaMainDir, siaLinkDir string, ownerUid, ownerGid int) error {
// Create the certs directory, if it doesn't exist
certDir := fmt.Sprintf("%s/certs", siaMainDir)
if !FileExists(certDir) {
err := os.MkdirAll(certDir, 0755)
if err != nil {
return fmt.Errorf("unable to create certs dir: %q, error: %v", certDir, err)
}
}
// Create the keys directory, if it doesn't exist
keyDir := fmt.Sprintf("%s/keys", siaMainDir)
if !FileExists(keyDir) {
err := os.MkdirAll(keyDir, 0755)
if err != nil {
return fmt.Errorf("unable to create keys dir: %q, error: %v", keyDir, err)
}
}
// update our main and then subdirectories
changeDirectoryOwnership(siaMainDir, ownerUid, ownerGid)
setupDirOwnership(certDir, ownerUid, ownerGid)
setupDirOwnership(keyDir, ownerUid, ownerGid)
//make sure the link directory exists as well
if siaLinkDir != "" && !FileExists(siaLinkDir) {
err := os.Symlink(siaMainDir, siaLinkDir)
if err != nil {
log.Printf("Unable to symlink SIA directory '%s': %v\n", siaLinkDir, err)
return nil
}
}
return nil
}
func changeDirectoryOwnership(path string, ownerUid, ownerGid int) error {
if ownerUid == -1 && ownerGid == -1 {
return nil
}
log.Printf("setting %s directory ownership set to %d/%d...\n", path, ownerUid, ownerGid)
err := os.Chown(path, ownerUid, ownerGid)
if err != nil {
log.Printf("unable to update ownership: error %v\n", err)
}
return err
}
func setupDirOwnership(siaDir string, ownerUid, ownerGid int) {
filepath.WalkDir(siaDir, func(path string, dirEntry fs.DirEntry, err error) error {
if err == nil {
err = changeDirectoryOwnership(path, ownerUid, ownerGid)
}
return err
})
}
func SyscallSetGid(gid int) error {
return syscall.Setgid(gid)
}
func SyscallSetUid(uid int) error {
return syscall.Setuid(uid)
}
func validateScriptArguments(args []string) bool {
if len(args) != 0 && !strings.HasPrefix(args[0], "/") {
log.Printf("script must start with fully qualified path: %s\n", args[0])
return false
}
return true
}