-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
mode.go
311 lines (282 loc) · 8.18 KB
/
mode.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Mgmt
// Copyright (C) 2013-2021+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package util
import (
"fmt"
"os"
"strings"
)
// Constant bytes for the who (u, g, or o) and the what (r, w, x, s, or t).
const (
ModeUser uint32 = 64
ModeGroup uint32 = 8
ModeOther uint32 = 1
ModeRead uint32 = 4
ModeWrite uint32 = 2
ModeExec uint32 = 1
ModeSetU uint32 = 4
ModeSetG uint32 = 2
ModeSticky uint32 = 1
)
// modeIsValidWho checks that only the characters 'u', 'g', 'o' are in the who
// string. It expects that 'a' was expanded to "ugo" already and will return
// false if not.
func modeIsValidWho(who string) bool {
for _, w := range []string{"u", "g", "o"} {
who = strings.ReplaceAll(who, w, "")
}
return len(who) == 0
}
// modeIsValidWhat checks that only the valid mode characters are in the what
// string ('r', 'w', 'x', 's', 't').
func modeIsValidWhat(what string) bool {
for _, w := range []string{"r", "w", "x", "s", "t"} {
what = strings.ReplaceAll(what, w, "")
}
return len(what) == 0
}
// modeAssigned executes an assigment symbolic mode string (u=r). It clears out
// any bits for every subject in who and then assigns the specified modes in
// what.
func modeAssigned(who, what string, from os.FileMode) (os.FileMode, error) {
// Clear out any users defined in 'who'.
for _, w := range who {
switch w {
case 'u':
from = from &^ os.FileMode(448) // 111000000 in bytes
from = from &^ os.ModeSetuid
case 'g':
from = from &^ os.FileMode(56) // 111000 in bytes
from = from &^ os.ModeSetgid
case 'o':
from = from &^ os.FileMode(7) // 111 in bytes
}
}
for _, c := range what {
switch c {
case 'r':
m := modeValueFrom(who, ModeRead)
if from&m == 0 {
from = from | m
}
case 'w':
m := modeValueFrom(who, ModeWrite)
if from&m == 0 {
from = from | m
}
case 'x':
m := modeValueFrom(who, ModeExec)
if from&m == 0 {
from = from | m
}
case 's':
for _, w := range who {
switch w {
case 'u':
from = from | os.ModeSetuid
case 'g':
from = from | os.ModeSetgid
}
}
case 't':
from = from | os.ModeSticky
default:
return os.FileMode(0), fmt.Errorf("invalid character %q", c)
}
}
return from, nil
}
// modeAdded executes an addition symbolic mode string (u+x) and will add the
// bits requested in what if not present.
func modeAdded(who, what string, from os.FileMode) (os.FileMode, error) {
for _, c := range what {
switch c {
case 'r':
m := modeValueFrom(who, ModeRead)
if from&m == 0 {
from = from | m
}
case 'w':
m := modeValueFrom(who, ModeWrite)
if from&m == 0 {
from = from | m
}
case 'x':
m := modeValueFrom(who, ModeExec)
if from&m == 0 {
from = from | m
}
case 's':
for _, w := range who {
switch w {
case 'u':
from = from | os.ModeSetuid
case 'g':
from = from | os.ModeSetgid
}
}
case 't':
from = from | os.ModeSticky
default:
return os.FileMode(0), fmt.Errorf("invalid character %q", c)
}
}
return from, nil
}
// modeSubtracted executes an subtraction symbolic mode string (u+x) and will
// removethe bits requested in what if present.
func modeSubtracted(who, what string, from os.FileMode) (os.FileMode, error) {
for _, c := range what {
switch c {
case 'r':
m := modeValueFrom(who, ModeRead)
if from&m != 0 {
from = from &^ m
}
case 'w':
m := modeValueFrom(who, ModeWrite)
if from&m != 0 {
from = from &^ m
}
case 'x':
m := modeValueFrom(who, ModeExec)
if from&m != 0 {
from = from &^ m
}
case 's':
for _, w := range who {
switch w {
case 'u':
if from&os.ModeSetuid != 0 {
from = from &^ os.ModeSetuid
}
case 'g':
if from&os.ModeSetgid != 0 {
from = from &^ os.ModeSetgid
}
}
}
case 't':
if from&os.ModeSticky != 0 {
from = from | os.ModeSticky
}
default:
return os.FileMode(0), fmt.Errorf("invalid character %q", c)
}
}
return from, nil
}
// modeValueFrom will return the bits requested for the mode in the correct
// possitions for the specified subjects in who.
func modeValueFrom(who string, modeType uint32) os.FileMode {
i := uint32(0)
for _, w := range who {
switch w {
case 'u':
i += ModeUser * uint32(modeType)
case 'g':
i += ModeGroup * uint32(modeType)
case 'o':
i += ModeOther * uint32(modeType)
}
}
return os.FileMode(i)
}
// ParseSymbolicModes parses a slice of symbolic mode strings. By default it
// will only accept the assignment input (=), but if allowAssign is true, then
// all symbolic mode strings (=, +, -) can be used as well.
//
// Symbolic mode is expected to be a string of who (user, group, other) then the
// operation (=, +, -) then the change (read, write, execute, setuid, setgid,
// sticky).
//
// Eg: ug=rw
//
// If you repeat yourself in the slice (eg. u=rw,u=w) ParseSymbolicModes will
// fail with an error.
func ParseSymbolicModes(modes []string, from os.FileMode, allowAssign bool) (os.FileMode, error) {
symModes := make([]struct {
mode, who, what string
parse func(who, what string, from os.FileMode) (os.FileMode, error)
}, len(modes))
for i, mode := range modes {
symModes[i].mode = mode
// If string contains '=' and no '+/-' it is safe to guess it is
// an assign.
if strings.Contains(mode, "=") && !strings.ContainsAny(mode, "+-") {
m := strings.Split(mode, "=")
if len(m) != 2 {
return os.FileMode(0), fmt.Errorf("only a single %q is allowed but found %d", "=", len(m))
}
symModes[i].who = m[0]
symModes[i].what = m[1]
symModes[i].parse = modeAssigned
continue
} else if strings.Contains(mode, "+") && !strings.ContainsAny(mode, "=-") && allowAssign {
m := strings.Split(mode, "+")
if len(m) != 2 {
return os.FileMode(0), fmt.Errorf("only a single %q is allowed but found %d", "+", len(m))
}
symModes[i].who = m[0]
symModes[i].what = m[1]
symModes[i].parse = modeAdded
continue
} else if strings.Contains(mode, "-") && !strings.ContainsAny(mode, "=+") && allowAssign {
m := strings.Split(mode, "-")
if len(m) != 2 {
return os.FileMode(0), fmt.Errorf("only a single %q is allowed but found %d", "-", len(m))
}
symModes[i].who = m[0]
symModes[i].what = m[1]
symModes[i].parse = modeSubtracted
continue
}
return os.FileMode(0), fmt.Errorf("%s is not a valid a symbolic mode", symModes[i].mode)
}
// Validate input and verify the slice of symbolic modes does not
// contain redundancy.
seen := make(map[rune]struct{}) // validate that subjects are not duplicated
for i := range symModes {
if strings.ContainsRune(symModes[i].who, 'a') || symModes[i].who == "" {
// If 'a' or empty who (implicit 'a') is called and
// there are more symbolic modes in the slice then it
// must be a repetition.
if len(symModes) > 1 {
return os.FileMode(0), fmt.Errorf("subject was repeated: each subject (u,g,o) is only accepted once")
}
symModes[i].who = "ugo"
}
if !modeIsValidWhat(symModes[i].what) || !modeIsValidWho(symModes[i].who) {
return os.FileMode(0), fmt.Errorf("unexpected character assignment in %s", symModes[i].mode)
}
for _, w := range symModes[i].who {
if _, ok := seen[w]; ok {
return os.FileMode(0), fmt.Errorf("subject was repeated: only define each subject (u,g,o) once")
}
seen[w] = struct{}{}
}
}
// Parse each symbolic mode accumulatively onto the file mode.
for _, m := range symModes {
var err error
from, err = m.parse(m.who, m.what, from)
if err != nil {
return os.FileMode(0), err
}
}
return from, nil
}