-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscall.go
367 lines (336 loc) · 9.73 KB
/
syscall.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package rs
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"path"
"strings"
"github.com/gregoryv/fox"
"github.com/gregoryv/nugo"
)
type Syscall struct {
sys *System // todo should probably hide this
acc *Account
auditer fox.Logger // used to audit who executes what
}
// SetAuditer sets the auditer to use. nil disables audit.
func (me *Syscall) SetAuditer(v fox.Logger) { me.auditer = v }
// SetGroup
func (me *Syscall) SetGroup(abspath string, gid int) error {
n, err := me.stat(abspath)
if err != nil {
return wrap("SetGroup", err)
}
if !me.acc.owns(n) && me.acc != Root {
return fmt.Errorf("SetGroup: %v not owner of %s", me.acc.UID, abspath)
}
n.GID = gid
me.sys.touch()
return nil
}
// SetOwner
func (me *Syscall) SetOwner(abspath string, uid int) error {
n, err := me.stat(abspath)
if err != nil {
return wrap("SetOwner", err)
}
if !me.acc.owns(n) && me.acc != Root {
return fmt.Errorf("SetOwner: %v not owner of %s", me.acc.UID, abspath)
}
n.UID = uid
me.sys.touch()
return nil
}
// SetMode sets the mode of abspath if the caller is the owner or Root.
// Only permissions bits can be set for now.
func (me *Syscall) SetMode(abspath string, mode Mode) error {
n, err := me.stat(abspath)
if err != nil {
return wrap("SetMode", err)
}
if !me.acc.owns(n) && me.acc != Root {
return fmt.Errorf("SetMode: %v not owner of %s", me.acc.UID, abspath)
}
if nugo.NodeMode(mode) > nugo.ModePerm {
return fmt.Errorf("SetMode: invalid mode")
}
n.SetPerm(nugo.NodeMode(mode)) // todo add SetMode
me.sys.touch()
return nil
}
// RemoveAll
func (me *Syscall) RemoveAll(abspath string) error {
n, err := me.stat(abspath)
if err != nil {
return wrap("RemoveAll", err)
}
n.Parent.DelChild(n.Name)
me.sys.touch()
return nil
}
// Create returns a new resource for writing. Fails if existing
// resource is directory. Caller must close resource.
func (me *Syscall) Create(abspath string) (*Resource, error) {
rif, _ := me.Stat(abspath)
if rif != nil && rif.IsDir() == nil {
return nil, fmt.Errorf("Create: %s is a directory", abspath)
}
dir, Name := path.Split(abspath)
parent, err := me.Stat(dir)
if err != nil {
return nil, wrap("Create", err)
}
if err := me.acc.permitted(OpWrite, parent.node); err != nil {
return nil, wrap("Create", err)
}
n := parent.node.Make(Name)
n.SetPerm(00644)
n.UnsetMode(nugo.ModeDir)
n.Lock()
r := newResource(n, OpWrite)
r.buf = &bytes.Buffer{}
r.unlock = n.Unlock
me.sys.touch()
return r, nil
}
// SaveAs save src to the given abspath. Fails if abspath already exists.
func (me *Syscall) SaveAs(abspath string, src interface{}) error {
if _, err := me.Stat(abspath); err == nil {
return fmt.Errorf("SaveAs: %s exists", abspath)
}
w, err := me.Create(abspath)
if err != nil {
return wrap("SaveAs", err)
}
defer w.Close()
return wrap("SaveAs", gob.NewEncoder(w).Encode(src))
}
// Save saves src to the given abspath. Overwrites existing resource.
// If src implements io.WriterTo interface that is used otherwise it's gob encoded.
func (me *Syscall) Save(abspath string, src interface{}) error {
rif, _ := me.Stat(abspath)
if rif != nil && rif.IsDir() == nil {
return fmt.Errorf("Save: %s is directory", abspath)
}
w, err := me.Create(abspath)
if err != nil {
return wrap("Save", err)
}
defer w.Close()
switch src := src.(type) {
case io.WriterTo:
_, err := src.WriteTo(w)
return err
default:
return wrap("Save", gob.NewEncoder(w).Encode(src))
}
}
// Load loads the resource from abspath. If res implements
// io.ReaderFrom that is used otherwise gob.Decoded.
func (me *Syscall) Load(res interface{}, abspath string) error {
r, err := me.Open(abspath)
if err != nil {
return fmt.Errorf("Load: %w", err)
}
defer r.Close()
switch res := res.(type) {
case io.ReaderFrom:
_, err := res.ReadFrom(r)
return wrap("Load", err)
default:
err := gob.NewDecoder(r).Decode(res)
return wrap("Load", err)
}
}
// Open resource for reading. Underlying source must be string or []byte.
// If resource is open for writing this call blocks.
func (me *Syscall) Open(abspath string) (*Resource, error) {
n, err := me.stat(abspath)
if err != nil {
return nil, fmt.Errorf("Open: %s", err)
}
if err := me.acc.permitted(OpRead, n); err != nil {
return nil, wrap("Open", err)
}
r := newResource(n, OpRead)
r.unlock = n.RUnlock
switch content := n.Content.(type) {
case []byte:
r.Reader = bytes.NewReader(content)
default:
// todo figure out how to read Any source
return nil, fmt.Errorf("Open: %s(%T) non readable source", abspath, content)
}
// Resource must be closed to unlock
n.RLock()
return r, nil
}
// LoadAccount
func (me *Syscall) LoadAccount(acc *Account, Name string) error {
return me.Load(acc, "/etc/accounts/"+Name)
}
func (me *Syscall) LoadGroup(group *Group, Name string) error {
return me.Load(group, "/etc/groups/"+Name)
}
// Install resource at the absolute path
func (me *Syscall) Install(abspath string, cmd Executable, mode nugo.NodeMode,
) (*ResInfo, error) {
dir, Name := path.Split(abspath)
parent, err := me.Stat(dir)
if err != nil {
return nil, wrap("Install", err)
}
if err := me.acc.permitted(OpWrite, parent.node); err != nil {
return nil, wrap("Install", err)
}
n := parent.node.Make(Name)
n.SetPerm(mode)
n.Content = cmd
n.UnsetMode(nugo.ModeDir)
me.sys.touch()
return &ResInfo{node: n}, nil
}
// Fexec creates and executes a new command and directs the output to
// the given writer.
func (me *Syscall) Fexec(w io.Writer, cli string) error {
parts := strings.Split(cli, " ")
cmd := NewCmd(parts[0], parts[1:]...)
cmd.Out = w
return me.Run(cmd)
}
// Execf formats command line and calls Exec
func (me *Syscall) Execf(format string, v ...interface{}) error {
return me.Exec(fmt.Sprintf(format, v...))
}
// Exec splits the cli on whitespace and executes the first as
// absolute path and the rest as arguments
func (me *Syscall) Exec(cli string) error {
parts := strings.Split(cli, " ")
return me.Run(NewCmd(parts[0], parts[1:]...))
}
// Run executes the given command. Fails if e.g. resource is not
// Executable. All exec calls are audited if system has an auditer
// configured.
func (me *Syscall) Run(cmd *Cmd) error {
n, err := me.stat(cmd.Abspath)
if err != nil {
return err
}
switch content := n.Content.(type) {
case Executable:
// If needed setuid can be checked and enforced here
cmd.Sys = me
err = content.Exec(cmd)
if me.auditer != nil {
msg := fmt.Sprintf("%v %s", me.acc.UID, cmd.String())
if err != nil {
// don't audit the actual error message, leave that to
// other form of logging
msg = fmt.Sprintf("%s ERR", msg)
}
me.auditer.Log(msg)
}
return err
default:
return fmt.Errorf("Cannot run %T", content)
}
}
type Mode nugo.NodeMode
// AddAccount adds a new account to the system. Name and uid must be
// unique.
func (me *Syscall) AddAccount(acc *Account) error {
for _, existing := range me.sys.Accounts {
if existing.UID == acc.UID {
return fmt.Errorf("uid exists")
}
if existing.Name == acc.Name {
return fmt.Errorf("name exists")
}
}
me.joinGroup(&Group{Name: acc.Name, gid: acc.Groups[0]})
abspath := fmt.Sprintf("/etc/accounts/%s", acc.Name)
if err := me.Save(abspath, acc); err != nil {
return err
}
me.sys.Accounts = append(me.sys.Accounts, acc)
me.sys.touch()
return nil
}
// joinGroup adds a new group to the system. Name and uid must be
// unique.
func (me *Syscall) joinGroup(group *Group) error {
for _, existing := range me.sys.Groups {
if existing.gid == group.gid {
return fmt.Errorf("gid exists")
}
if existing.Name == group.Name {
return fmt.Errorf("name exists")
}
}
me.sys.Groups = append(me.sys.Groups, group)
abspath := fmt.Sprintf("/etc/groups/%s", group.Name)
return me.Save(abspath, group)
}
// Mkdir creates the absolute path whith a given mode where the parent
// must exist.
func (me *Syscall) Mkdir(abspath string, mode Mode) (*ResInfo, error) {
dir, Name := path.Split(abspath)
parent, err := me.stat(dir)
if err != nil {
return nil, fmt.Errorf("Mkdir: %w", err)
}
if err := me.acc.permitted(OpWrite, parent); err != nil {
return nil, fmt.Errorf("Mkdir: %w", err)
}
n := parent.Make(Name)
n.SetPerm(nugo.NodeMode(mode))
me.sys.touch()
return &ResInfo{node: n}, nil
}
// Stat returns the node of the abspath if account is allowed to reach
// it, ie. all nodes up to it must have execute flags set.
func (me *Syscall) Stat(abspath string) (*ResInfo, error) {
n, err := me.stat(abspath)
if err != nil {
return nil, fmt.Errorf("Stat %v", err)
}
return &ResInfo{node: n}, nil
}
// stat returns the node of the abspath if account is allowed to reach
// it, ie. all nodes up to it must have execute mode set.
func (me *Syscall) stat(abspath string) (*nugo.Node, error) {
rn := me.sys.rootNode(abspath)
n, err := rn.Find(abspath)
if err != nil {
return nil, err
}
parent := n.Parent
// check each parent for access
for parent != nil {
if err := me.acc.permitted(OpExec, parent); err != nil {
return nil, fmt.Errorf("%s uid:%d: %v", abspath, me.acc.UID, err)
}
parent = parent.Parent
}
return n, nil
}
func wrap(prefix string, err error) error {
if err != nil {
return fmt.Errorf("%s: %w", prefix, err)
}
return nil
}
// mount creates a root node for the given path.
func (me *Syscall) mount(abspath string, mode nugo.NodeMode) error {
rn := nugo.NewRootNode(abspath, mode)
rn.SetSeal(me.acc.UID, me.acc.gid(), 01755)
return me.sys.mount(rn)
}
// Visitor is called during a walk with a specific node and the
// absolute path to that node. Use the given Walker to stop if needed.
type Visitor func(child *ResInfo, abspath string, w *nugo.Walker)
// NextUID returns potential next user id.
func (me *Syscall) NextUID() int { return me.sys.NextUID() }
// NextGID returns potential next group id.
func (me *Syscall) NextGID() int { return me.sys.NextGID() }