/
glob.go
95 lines (87 loc) · 2.06 KB
/
glob.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package internal
import (
"os"
"path/filepath"
"strings"
"v.io/v23/context"
"v.io/v23/glob"
"v.io/v23/naming"
"v.io/v23/rpc"
"v.io/v23/security"
"v.io/v23/verror"
)
func globChildren(ctx *context.T, call rpc.GlobChildrenServerCall, serverConfig *serverConfig, m *glob.Element) error {
sCall := call.Security()
n := findRoles(ctx, sCall, serverConfig.root)
suffix := sCall.Suffix()
if len(suffix) > 0 {
n = n.find(strings.Split(suffix, "/"), false)
}
if n == nil {
return verror.ErrNoExistOrNoAccess.Errorf(ctx, "does not exist or access denied")
}
for c := range n.children {
if m.Match(c) {
//nolint:errcheck
call.SendStream().Send(naming.GlobChildrenReplyName{Value: c})
}
}
return nil
}
// findRoles finds all the roles to which the caller has access.
func findRoles(ctx *context.T, call security.Call, root string) *node {
blessingNames, _ := security.RemoteBlessingNames(ctx, call)
tree := newNode()
//nolint:errcheck
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(path, ".conf") {
return nil
}
c, err := loadExpandedConfig(path, nil)
if err != nil {
return nil
}
if !hasAccess(c, blessingNames) {
return nil
}
relPath, err := filepath.Rel(root, path)
if err != nil {
return nil
}
tree.find(strings.Split(strings.TrimSuffix(relPath, ".conf"), string(filepath.Separator)), true)
return nil
})
return tree
}
type node struct {
children map[string]*node
}
func newNode() *node {
return &node{children: make(map[string]*node)}
}
func (n *node) find(names []string, create bool) *node {
for {
if len(names) == 0 {
return n
}
if next, ok := n.children[names[0]]; ok {
n = next
names = names[1:]
continue
}
if create {
nn := newNode()
n.children[names[0]] = nn
n = nn
names = names[1:]
continue
}
return nil
}
}