-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.go
531 lines (447 loc) · 10.6 KB
/
objects.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package vimages
import (
"database/sql"
"errors"
"fmt"
"sort"
"strconv"
"strings"
"github.com/alankm/privileges"
)
var (
errBadWrite = errors.New("call to write without proper setup")
errInternal = errors.New("internal error")
errExist = errors.New("object doesn't exist")
errBadDate = errors.New("date must be an unsigned integer")
errCollission = errors.New("collission detected with no overwrite set")
)
type shortData struct {
Name string `json:"name"`
Type string `json:"type"`
}
type longData struct {
Name string `json:"name"`
Type string `json:"type"`
Rules string `json:"permissions"`
}
type object interface {
Rules() *privileges.Rules
Read(...string) interface{}
Write(...string) interface{}
Exec(...string) interface{}
otype() string
delete() ([]string, error)
}
func newObject(tx *sql.Tx, s *privileges.Session, overwrite bool, path, checksum string, headers map[string][]string) ([]string, error) {
var deleted []string
rules, _ := privileges.NewRules("root", "root", "0755")
var obj object = newFolder(tx, s, "", rules)
route := strings.Split(path[1:], "/")
ctype := "folder"
var date uint64
a, ok := headers["Date"]
if ok {
date, _ = strconv.ParseUint(a[0], 10, 64)
}
author := ""
a, ok = headers["Author"]
if ok {
author = a[0]
}
description := ""
a, ok = headers["Description"]
if ok {
description = a[0]
}
for i, o := range route {
if i == len(route)-1 && checksum != emptyPayloadChecksum {
ctype = "image"
}
val, err := s.Exec(obj)
if err != nil {
return nil, err
}
children, ok := val.(map[string]object)
if !ok {
return nil, errInternal
}
gid, _ := s.Gid("", "")
mod, _ := s.Umask("")
rule, _ := privileges.NewRules(s.User, gid, mod)
path := ""
for j := 0; j <= i; j++ {
path = path + "/" + route[j]
}
if children[o] == nil { // make an entry
if !s.CanWrite(obj) {
return nil, errDenied
}
if ctype == "folder" {
folder := newFolder(tx, s, path, rule)
children[o] = folder
_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, true, s.User, gid, mod, "", 0, "", "")
if err != nil {
return nil, err
}
} else { // image
_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, false, s.User, gid, mod, checksum, date, author, description)
if err != nil {
return nil, err
}
return deleted, nil
}
} else if children[o].otype() != ctype { // overwrite?
if !overwrite {
return nil, errCollission
}
if !s.CanWrite(obj) {
return nil, errDenied
}
// overwrite EVERYTHING
d, err := children[o].delete()
if err != nil {
return nil, err
}
deleted = append(deleted, d...)
if ctype == "folder" {
folder := newFolder(tx, s, path, rule)
children[o] = folder
_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, true, s.User, gid, mod, checksum, date, author, description)
if err != nil {
return nil, err
}
} else { // image
_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, false, s.User, gid, mod, checksum, date, author, description)
if err != nil {
return nil, err
}
return deleted, nil
}
} else if i == len(route)-1 { // overwrite if final image
if !overwrite {
return nil, errCollission
}
if s.CanWrite(children[o]) {
d, err := children[o].delete()
if err != nil {
return nil, err
}
deleted = append(deleted, d...)
_, err = tx.Exec("DELETE FROM vimages WHERE path=?", path)
if err != nil {
return nil, err
}
_, err = tx.Exec("INSERT INTO vimages VALUES(?,?,?,?,?,?,?,?,?)", path, false, s.User, gid, mod, checksum, date, author, description)
if err != nil {
return nil, err
}
return deleted, nil
} else {
return nil, errDenied
}
}
obj = children[o]
}
return deleted, nil
}
func getObject(tx *sql.Tx, s *privileges.Session, path string) (object, error) {
rules, _ := privileges.NewRules("root", "root", "0755")
var obj object = newFolder(tx, s, "", rules)
if len(path) < 2 {
return obj, nil
}
route := strings.Split(path[1:], "/")
for _, o := range route {
val, err := s.Exec(obj)
if err != nil {
return nil, err
}
children, ok := val.(map[string]object)
if !ok {
return nil, errInternal
}
obj = children[o]
if obj == nil {
return nil, errExist
}
}
return obj, nil
}
type folder struct {
tx *sql.Tx
s *privileges.Session
path string
rules *privileges.Rules
}
func newFolder(tx *sql.Tx, s *privileges.Session, path string, rules *privileges.Rules) *folder {
f := new(folder)
f.s = s
f.tx = tx
f.path = path
f.rules = rules
return f
}
func (f *folder) otype() string {
return "folder"
}
func (f *folder) Rules() *privileges.Rules {
return f.rules
}
func (f *folder) Read(args ...string) interface{} {
if !f.s.CanExec(f) {
return errDenied
}
ret, err := f.children()
if err != nil {
return err
}
var names []string
var pref string
for _, child := range ret {
i := strings.LastIndex(child.path, "/")
pref = child.path[:i+1]
names = append(names, child.path[i+1:])
}
sort.Strings(names)
if args != nil && len(args) > 0 && args[0] == "long" {
var list []longData
for _, name := range names {
rec := ret[pref+name]
t := "image"
if rec.dir {
t = "folder"
}
rule, _ := privileges.NewRules(rec.owner, rec.group, rec.rules)
list = append(list, longData{
Name: name,
Type: t,
Rules: rule.Symbolic(rec.dir),
})
}
return list
} else {
var list []shortData
for _, name := range names {
rec := ret[pref+name]
t := "image"
if rec.dir {
t = "folder"
}
list = append(list, shortData{
Name: name,
Type: t,
})
}
return list
}
}
func (f *folder) Write(args ...string) interface{} {
fmt.Println("1")
if !f.s.CanExec(f) {
return errDenied
}
fmt.Println("2")
if args == nil || len(args) != 2 {
return errBadWrite
}
fmt.Println("3")
switch args[0] {
case "CHMOD":
if f.s.CanChmod(f.rules, args[1]) {
f.tx.Exec("UPDATE vimages SET permissions=? WHERE path=?", args[1], f.path)
return nil
}
return errDenied
case "CHOWN":
if f.s.CanChown(f.rules, args[1]) {
f.tx.Exec("UPDATE vimages SET owner_user=? WHERE path=?", args[1], f.path)
return nil
}
return errDenied
case "CHGRP":
if f.s.CanChgrp(f.rules, args[1]) {
f.tx.Exec("UPDATE vimages SET owner_group=? WHERE path=?", args[1], f.path)
return nil
}
return errDenied
case "DELETE":
exec := f.Exec("")
children, ok := exec.(map[string]object)
if !ok {
return exec
}
fmt.Println("DELETING...")
fmt.Println(args[1])
fmt.Println("")
target := children[args[1]]
if target == nil {
return errBadWrite
}
deleted, err := target.delete()
if err != nil {
return err
}
return deleted
default:
return errBadWrite
}
}
func (f *folder) Exec(args ...string) interface{} {
records, err := f.children()
if err != nil {
return err
}
exec := make(map[string]object)
for _, rec := range records {
vals := strings.Split(rec.path, "/")
path := vals[len(vals)-1]
rules, err := privileges.NewRules(rec.owner, rec.group, rec.rules)
if err != nil {
return err
}
if rec.dir {
exec[path] = newFolder(f.tx, f.s, rec.path, rules)
} else {
exec[path] = newImage(f.tx, f.s, rec.path, rec.chk, rules, rec)
}
}
return exec
}
func (f *folder) delete() ([]string, error) {
var deleted []string
children, err := f.s.Exec(f)
if err != nil {
return nil, err
}
for _, child := range children.(map[string]object) {
d, err := child.delete()
if err != nil {
return nil, err
}
deleted = append(deleted, d...)
}
_, err = f.tx.Exec("DELETE FROM vimages WHERE path=?", f.path)
if err != nil {
return nil, err
}
return deleted, nil
}
func (f *folder) children() (map[string]*record, error) {
rows, err := f.tx.Query("SELECT * FROM vimages WHERE path LIKE ? AND path NOT LIKE ?", f.path+"/%", f.path+"/%/%")
if err != nil {
return nil, err
}
defer rows.Close()
var ret = make(map[string]*record)
for rows.Next() {
rec, err := recordFromRows(rows)
if err != nil {
return nil, err
}
ret[rec.path] = rec
}
return ret, nil
}
type imgData struct {
Path string `json:"path"`
Date uint64 `json:"date"`
Checksum string `json:"checksum"`
Author string `json:"author"`
Description string `json:"description"`
}
type image struct {
tx *sql.Tx
s *privileges.Session
path string
rules *privileges.Rules
chk string
rec *record
}
func newImage(tx *sql.Tx, s *privileges.Session, path, chk string, rules *privileges.Rules, rec *record) *image {
i := new(image)
i.tx = tx
i.path = path
i.rules = rules
i.chk = chk
i.rec = rec
return i
}
func (i *image) otype() string {
return "image"
}
func (i *image) Rules() *privileges.Rules {
return i.rules
}
func (i *image) Read(args ...string) interface{} {
if args != nil && len(args) > 0 && args[0] == "attributes" {
var data = imgData{
Path: i.rec.path,
Date: i.rec.date,
Checksum: i.rec.chk,
Author: i.rec.auth,
Description: i.rec.desc,
}
return data
} else {
return i.chk
}
}
func (i *image) Write(args ...string) interface{} {
if args == nil || len(args) != 2 {
return errBadWrite
}
switch args[0] {
case "CHMOD":
if i.s.CanChown(i.rules, args[1]) {
i.tx.Exec("UPDATE vimages SET permissions=? WHERE path=?", args[1], i.path)
return nil
}
return errDenied
case "CHOWN":
if i.s.CanChown(i.rules, args[1]) {
i.tx.Exec("UPDATE vimages SET owner_user=? WHERE path=?", args[1], i.path)
return nil
}
return errDenied
case "CHGRP":
if i.s.CanChgrp(i.rules, args[1]) {
i.tx.Exec("UPDATE vimages SET owner_group=? WHERE path=?", args[1], i.path)
return nil
}
return errDenied
case "AUTHOR":
i.tx.Exec("UPDATE vimages SET author=? WHERE path=?", args[1], i.path)
return nil
case "DESCRIPTION":
i.tx.Exec("UPDATE vimages SET description=? WHERE path=?", args[1], i.path)
return nil
case "DATE":
date, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return errBadDate
}
i.tx.Exec("UPDATE vimages SET date=? WHERE path=?", date, i.path)
return nil
default:
return errInternal
}
}
func (i *image) Exec(args ...string) interface{} {
return nil
}
func (i *image) delete() ([]string, error) {
var deleted []string
_, err := i.tx.Exec("DELETE FROM vimages WHERE path=?", i.path)
if err != nil {
return nil, err
}
rows, err := i.tx.Query("SELECT * FROM vimages WHERE checksum=?", i.chk)
if err != nil {
return nil, err
}
defer rows.Close()
if !rows.Next() {
deleted = append(deleted, i.chk)
}
return deleted, nil
}