-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
83 lines (77 loc) · 1.91 KB
/
files.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
package model
import (
"fmt"
. "github.com/mickael-kerjean/filestash/server/common"
_ "github.com/mickael-kerjean/filestash/server/model/backend"
"strings"
)
func NewBackend(ctx *App, conn map[string]string) (IBackend, error) {
isAllowed := func() bool {
// by default, a hacker could use filestash to establish connections outside of what's
// define in the config file. We need to prevent this
possibilities := make([]map[string]interface{}, 0)
for i:=0; i< len(Config.Conn); i++ {
d := Config.Conn[i]
if d["type"] != conn["type"] {
continue
}
if val, ok := d["hostname"]; ok == true {
if val != conn["hostname"] {
continue
}
}
if val, ok := d["path"]; ok == true {
if val == nil {
val = "/"
}
if configPath, ok := val.(string); ok == false {
continue
} else if strings.HasPrefix(conn["path"], configPath) == false {
continue
}
}
if val, ok := d["url"]; ok == true {
if val != conn["url"] {
continue
}
}
possibilities = append(possibilities, Config.Conn[i])
}
if len(possibilities) > 0 {
return true
}
return false
}
if isAllowed() == false {
return Backend.Get(BACKEND_NIL), ErrNotAllowed
}
return Backend.Get(conn["type"]).Init(conn, ctx)
}
func GetHome(b IBackend, base string) (string, error) {
if _, err := b.Ls(base); err != nil {
return base, err
}
home := "/"
if obj, ok := b.(interface{ Home() (string, error) }); ok {
tmp, err := obj.Home()
if err != nil {
return base, err
}
home = EnforceDirectory(tmp)
}
base = EnforceDirectory(base)
if strings.HasPrefix(home, base) {
return "/" + home[len(base):], nil
}
return "/", nil
}
func MapStringInterfaceToMapStringString(m map[string]interface{}) map[string]string {
res := make(map[string]string)
for key, value := range m {
res[key] = fmt.Sprintf("%v", value)
if res[key] == "<nil>" {
res[key] = ""
}
}
return res
}