-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
251 lines (223 loc) · 5.54 KB
/
system.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
/*
Package rs provides a resource system which enforces unix style access control.
Resources are stored as nugo.Nodes and can either have a []byte slice
as source or implement the Executable interface. Using the Save and
Load syscalls, structs are gob encoded and decoded to an access
controlled resource.
Anonymous account has uid,gid 0,0 whereas the Root account 1,1.
*/
package rs
import (
"bufio"
"encoding/base64"
"fmt"
"io"
"path"
"strings"
"sync"
"time"
"github.com/gregoryv/fox"
"github.com/gregoryv/nexus"
"github.com/gregoryv/nugo"
)
// NewSystem returns a system with installed resources resembling a
// unix filesystem.
func NewSystem() *System {
sys := &System{
mounts: make(map[string]*nugo.Node),
Accounts: []*Account{},
Groups: []*Group{},
}
asRoot := Root.Use(sys)
asRoot.mount("/", nugo.ModeDir|nugo.ModeSort|nugo.ModeDistinct)
installSys(sys)
return sys
}
// installSys creates default resources on the system. Should only be
// called once on one system.
func installSys(sys *System) {
asRoot := Root.Use(sys)
asRoot.Mkdir("/bin", 01755)
asRoot.Mkdir("/etc", 00755)
asRoot.Mkdir("/etc/accounts", 00755)
asRoot.Mkdir("/etc/groups", 00755)
asRoot.Mkdir("/tmp", 07777)
asRoot.Install("/bin/chmod", ExecFunc(Chmod), 00755)
asRoot.Install("/bin/chown", &Chown{}, 00755)
asRoot.Install("/bin/ls", ExecFunc(Ls), 01755)
asRoot.Install("/bin/mkacc", ExecFunc(Mkacc), 00755)
asRoot.Install("/bin/mkdir", ExecFunc(Mkdir), 00755)
asRoot.Install("/bin/secure", ExecFunc(Secure), 00755)
asRoot.AddAccount(Anonymous)
asRoot.AddAccount(Root)
}
type System struct {
mounts map[string]*nugo.Node
Accounts []*Account
Groups []*Group
auditer fox.Logger // Used audit Syscall.Exec calls
lm sync.RWMutex
lastModified time.Time // last time a resource was modified
}
// touch synced update of lastModified field
func (me *System) touch() {
me.lm.Lock()
me.lastModified = time.Now()
me.lm.Unlock()
}
// LastModified returns last time resources state was modified.
func (me *System) LastModified() time.Time {
me.lm.RLock()
defer me.lm.RUnlock()
return me.lastModified
}
// NextUID returns next available uid
func (me *System) NextUID() int {
var uid int
for _, acc := range me.Accounts {
if acc.UID > uid {
uid = acc.UID
}
}
return uid + 1
}
// accountByUID
func (me *System) accountByUID(uid int) (*Account, error) {
for _, acc := range me.Accounts {
if acc.UID == uid {
return acc, nil
}
}
return nil, fmt.Errorf("uid %v not found", uid)
}
// NextGID returns next available gid
func (me *System) NextGID() int {
var gid int
for _, acc := range me.Accounts {
for _, id := range acc.Groups {
if id > gid {
gid = id
}
}
}
return gid + 1
}
// groupByGID
func (me *System) groupByGID(gid int) (*Group, error) {
for _, group := range me.Groups {
if group.gid == gid {
return group, nil
}
}
return nil, fmt.Errorf("gid %v not found", gid)
}
// SetAuditer sets the auditer for Syscall.Exec calls
func (me *System) SetAuditer(auditer fox.Logger) *System {
me.auditer = auditer
return me
}
func (me *System) mount(rn *nugo.Node) error {
abspath := path.Clean(rn.Name)
if _, found := me.mounts[abspath]; found {
return fmt.Errorf("mount: %s already exists", abspath)
}
me.mounts[abspath] = rn
me.touch()
return nil
}
// rootNode returns the mounting point of the abspath. Currently only
// "/" is available.
func (me *System) rootNode(abspath string) *nugo.Node {
rn := me.mounts["/"]
for p, n := range me.mounts {
if strings.Index(abspath, p) == 0 {
if len(n.Name) > len(rn.Name) {
rn = n
}
}
}
return rn
}
// Export
func (me *System) Export(w io.Writer) error {
root := me.rootNode("/")
exp := NodeExporter(w)
exp(root, "/", nil)
root.Walk(NodeExporter(w))
return nil
}
// NodeExporter writes each node with it's content as base64 encoded string
func NodeExporter(writer io.Writer) nugo.Visitor {
p, _ := nexus.NewPrinter(writer)
return func(node *nugo.Node, abspath string, w *nugo.Walker) {
if _, isExecutable := node.Content.(Executable); isExecutable {
return
}
p.Print(uint32(node.Mode), node.UID, node.GID, " ", abspath)
defer p.Println()
if !node.IsDir() {
p.Print(" ")
b64 := base64.NewEncoder(base64.StdEncoding, p)
switch content := node.Content.(type) {
case []byte:
b64.Write(content)
case nil:
return
default:
panic(fmt.Sprintf(
"cannot export %t, only []byte supported", content,
))
}
}
}
}
// Import imports resources to the system from the given reader to the abspath.
func (me *System) Import(abspath string, r io.Reader) error {
scanner := bufio.NewScanner(r)
rn := me.rootNode(abspath)
for scanner.Scan() {
n := nugo.NewNode("undef")
var (
mode nugo.NodeMode
abspath string
content string
)
_, err := fmt.Sscanf(scanner.Text(), "%d %d %d %s %s",
&mode,
&n.UID,
&n.GID,
&abspath,
&content,
)
if err != nil && err != io.EOF {
return err
}
n.Name = path.Base(abspath)
if content != "EOF" {
n.Content, err = base64.StdEncoding.DecodeString(content)
if err != nil {
return err
}
}
if existing, err := rn.Find(abspath); err == nil {
existing.UID = n.UID
existing.GID = n.GID
existing.Mode = mode
existing.Content = n.Content
} else {
parent, err := rn.Find(path.Dir(abspath))
if err != nil {
return err
}
parent.Add(n)
n.Mode = mode
}
}
return nil
}
func isSibling(lastpath, abspath string) bool {
return lastpath == path.Dir(abspath)
}
func isChild(lastpath, abspath string) bool {
return lastpath == path.Dir(path.Dir(abspath))
}