This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
acl.go
422 lines (369 loc) · 9.11 KB
/
acl.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright 2016 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright 2015 Joseph Naegele
//
// Portions of this code are derived from go-acl by Joseph Naegele
// (https://github.com/naegeldjd/go-acl) which is under an MIT license that can
// be found in the accompanying LICENSE.MIT file
//
//+build linux
// Package acl is a wrapper over libacl that dlopens it instead of being linked
// to it. The code is based on go-acl by Joseph Naegele
// (https://github.com/naegelejd/go-acl).
package acl
// #cgo LDFLAGS: -ldl
// #include <stdlib.h>
// #include <dlfcn.h>
// #include <stdio.h>
// #include <sys/acl.h>
// #include <sys/types.h>
// #include <unistd.h>
//
// acl_t
// my_acl_from_text(void *f, const char *acl)
// {
// acl_t (*acl_from_text)(const char *);
//
// acl_from_text = f;
// return acl_from_text(acl);
// }
//
// int
// my_acl_set_file(void *f, const char *path_p, acl_type_t type, acl_t acl)
// {
// int (*acl_set_file)(const char *, acl_type_t, acl_t);
//
// acl_set_file = f;
// return acl_set_file(path_p, type, acl);
// }
//
// int
// my_acl_free(void *f, acl_t acl)
// {
// int (*acl_free)(acl_t);
//
// acl_free = f;
// return acl_free(acl);
// }
//
// int
// my_acl_valid(void *f, acl_t acl)
// {
// int (*acl_valid)(acl_t);
//
// acl_valid = f;
// return acl_valid(acl);
// }
//
// int
// my_acl_create_entry(void *f, acl_t *acl_p, acl_entry_t *entry_p)
// {
// int (*acl_create_entry)(acl_t *, acl_entry_t *);
//
// acl_create_entry = f;
// return acl_create_entry(acl_p, entry_p);
// }
//
// int
// my_acl_set_tag_type(void *f, acl_entry_t entry_d, acl_tag_t tag_type)
// {
// int (*acl_set_tag_type)(acl_entry_t, acl_tag_t);
//
// acl_set_tag_type = f;
// return acl_set_tag_type(entry_d, tag_type);
// }
//
// int
// my_acl_get_permset(void *f, acl_entry_t entry_d, acl_permset_t *permset_p)
// {
// int (*acl_get_permset)(acl_entry_t, acl_permset_t *);
//
// acl_get_permset = f;
// return acl_get_permset(entry_d, permset_p);
// }
//
// int
// my_acl_add_perm(void *f, acl_permset_t permset_d, acl_perm_t perm)
// {
// int (*acl_add_perm)(acl_permset_t, acl_perm_t);
//
// acl_add_perm = f;
// return acl_add_perm(permset_d, perm);
// }
import "C"
import (
"errors"
"fmt"
"os"
"unsafe"
"github.com/hashicorp/errwrap"
)
const (
otherExec = 1 << iota
otherWrite
otherRead
groupExec
groupWrite
groupRead
userExec
userWrite
userRead
)
const (
TagUserObj Tag = C.ACL_USER_OBJ
TagUser = C.ACL_USER
TagGroupObj = C.ACL_GROUP_OBJ
TagGroup = C.ACL_GROUP
TagMask = C.ACL_MASK
TagOther = C.ACL_OTHER
)
const (
PermRead Perm = C.ACL_READ
PermWrite = C.ACL_WRITE
PermExecute = C.ACL_EXECUTE
)
type ACL struct {
lib *libHandle
a C.acl_t
}
// Entry is an entry in an ACL.
type Entry struct {
a *ACL
e C.acl_entry_t
}
// Permset is a set of permissions.
type Permset struct {
a *ACL
p C.acl_permset_t
}
// Perm represents a permission in the e_perm ACL field
type Perm int
// Tag represents an ACL e_tag entry
type Tag int
var ErrSoNotFound = errors.New("unable to open a handle to libacl")
type libHandle struct {
handle unsafe.Pointer
libname string
}
func getHandle() (*libHandle, error) {
for _, name := range []string{
"libacl.so.1",
"libacl.so",
} {
libname := C.CString(name)
defer C.free(unsafe.Pointer(libname))
handle := C.dlopen(libname, C.RTLD_LAZY)
if handle != nil {
h := &libHandle{
handle: handle,
libname: name,
}
return h, nil
}
}
return nil, ErrSoNotFound
}
func getSymbolPointer(handle unsafe.Pointer, symbol string) (unsafe.Pointer, error) {
sym := C.CString(symbol)
defer C.free(unsafe.Pointer(sym))
C.dlerror()
p := C.dlsym(handle, sym)
e := C.dlerror()
if e != nil {
return nil, errwrap.Wrap(fmt.Errorf("error resolving symbol %q", symbol), errors.New(C.GoString(e)))
}
return p, nil
}
func (h *libHandle) close() error {
C.dlerror()
C.dlclose(h.handle)
e := C.dlerror()
if e != nil {
return errwrap.Wrap(fmt.Errorf("error closing %v", h.libname), errors.New(C.GoString(e)))
}
return nil
}
// InitACL dlopens libacl and returns an ACL object if successful.
func InitACL() (*ACL, error) {
h, err := getHandle()
if err != nil {
return nil, err
}
return &ACL{lib: h}, nil
}
// ParseACL parses a string representation of an ACL.
func (a *ACL) ParseACL(acl string) error {
acl_from_text, err := getSymbolPointer(a.lib.handle, "acl_from_text")
if err != nil {
return err
}
cacl := C.CString(acl)
defer C.free(unsafe.Pointer(cacl))
retACL, err := C.my_acl_from_text(acl_from_text, cacl)
if retACL == nil {
return errwrap.Wrap(errors.New("error calling acl_from_text"), err)
}
a.a = retACL
return nil
}
// Free frees libacl's internal structures and closes libacl.
func (a *ACL) Free() error {
acl_free, err := getSymbolPointer(a.lib.handle, "acl_free")
if err != nil {
return err
}
ret, err := C.my_acl_free(acl_free, a.a)
if ret < 0 {
return errwrap.Wrap(errors.New("error calling acl_free"), err)
}
return a.lib.close()
}
// SetFileACLDefault sets the "default" ACL for path.
func (a *ACL) SetFileACLDefault(path string) error {
acl_set_file, err := getSymbolPointer(a.lib.handle, "acl_set_file")
if err != nil {
return err
}
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret, err := C.my_acl_set_file(acl_set_file, cpath, C.ACL_TYPE_DEFAULT, a.a)
if ret < 0 {
return errwrap.Wrap(errors.New("error calling acl_set_file"), err)
}
return nil
}
// Valid checks whether the ACL is valid.
func (a *ACL) Valid() error {
acl_valid, err := getSymbolPointer(a.lib.handle, "acl_valid")
if err != nil {
return err
}
ret, err := C.my_acl_valid(acl_valid, a.a)
if ret < 0 {
return errwrap.Wrap(errors.New("invalid acl"), err)
}
return nil
}
// AddBaseEntries adds the base ACL entries from the file permissions.
func (a *ACL) AddBaseEntries(path string) error {
fi, err := os.Lstat(path)
if err != nil {
return err
}
mode := fi.Mode().Perm()
var r, w, x bool
// set USER_OBJ entry
r = mode&userRead == userRead
w = mode&userWrite == userWrite
x = mode&userExec == userExec
if err := a.addBaseEntryFromMode(TagUserObj, r, w, x); err != nil {
return err
}
// set GROUP_OBJ entry
r = mode&groupRead == groupRead
w = mode&groupWrite == groupWrite
x = mode&groupExec == groupExec
if err := a.addBaseEntryFromMode(TagGroupObj, r, w, x); err != nil {
return err
}
// set OTHER entry
r = mode&otherRead == otherRead
w = mode&otherWrite == otherWrite
x = mode&otherExec == otherExec
if err := a.addBaseEntryFromMode(TagOther, r, w, x); err != nil {
return err
}
return nil
}
func (a *ACL) createEntry() (*Entry, error) {
acl_create_entry, err := getSymbolPointer(a.lib.handle, "acl_create_entry")
if err != nil {
return nil, err
}
var e C.acl_entry_t
rv, err := C.my_acl_create_entry(acl_create_entry, &a.a, &e)
if rv < 0 {
return nil, errwrap.Wrap(errors.New("unable to create entry"), err)
}
return &Entry{a, e}, nil
}
func (a *ACL) addBaseEntryFromMode(tag Tag, read, write, execute bool) error {
e, err := a.createEntry()
if err != nil {
return err
}
if err = e.setTag(tag); err != nil {
return err
}
p, err := e.getPermset(a)
if err != nil {
return err
}
if err := p.addPermsFromMode(read, write, execute); err != nil {
return err
}
return nil
}
func (entry *Entry) getPermset(a *ACL) (*Permset, error) {
acl_get_permset, err := getSymbolPointer(a.lib.handle, "acl_get_permset")
if err != nil {
return nil, err
}
var ps C.acl_permset_t
rv, err := C.my_acl_get_permset(acl_get_permset, entry.e, &ps)
if rv < 0 {
return nil, errwrap.Wrap(errors.New("unable to get permset"), err)
}
return &Permset{a, ps}, nil
}
func (entry *Entry) setTag(t Tag) error {
acl_set_tag_type, err := getSymbolPointer(entry.a.lib.handle, "acl_set_tag_type")
if err != nil {
return err
}
rv, err := C.my_acl_set_tag_type(acl_set_tag_type, entry.e, C.acl_tag_t(t))
if rv < 0 {
return errwrap.Wrap(errors.New("unable to set tag"), err)
}
return nil
}
func (pset *Permset) addPerm(perm Perm) error {
acl_add_perm, err := getSymbolPointer(pset.a.lib.handle, "acl_add_perm")
if err != nil {
return err
}
rv, err := C.my_acl_add_perm(acl_add_perm, pset.p, C.acl_perm_t(perm))
if rv < 0 {
return errwrap.Wrap(errors.New("unable to add perm to permset"), err)
}
return nil
}
func (p *Permset) addPermsFromMode(read, write, execute bool) error {
if read {
if err := p.addPerm(PermRead); err != nil {
return err
}
}
if write {
if err := p.addPerm(PermWrite); err != nil {
return err
}
}
if execute {
if err := p.addPerm(PermExecute); err != nil {
return err
}
}
return nil
}