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
|
package main
import (
"fmt"
"path"
"sync"
p9 "github.com/Harvey-OS/ninep/protocol"
"git.samanthony.xyz/buth/back/auth"
)
type File interface {
Qid() (p9.QID, error)
// TODO
}
type Dir interface {
File
// TODO: walk, create
}
// Authfs is the root p9.NineServer.
type Authfs struct {
*RootDir
*UsersDir
*SessionsDir
mu sync.Mutex // guards below
files map[p9.FID]File
}
func NewAuthfs(rootPath string) (*Authfs, error) {
root := NewRootDir()
users, err := NewUsersDir(rootPath, root.Version)
if err != nil {
return nil, err
}
return &Authfs{
root,
users,
NewSessionsDir(root.Version),
sync.Mutex{},
make(map[p9.FID]File),
}, nil
}
func (fs *Authfs) Close() {
fs.RootDir.Close()
fs.UsersDir.Close()
fs.SessionsDir.Close()
}
func (fs *Authfs) Rattach(fid, afid p9.FID, uname, aname string) (p9.QID, error) {
if afid != p9.NOFID {
return p9.QID{}, fmt.Errorf("authfs: no authentication required")
}
var (
f File
err error
ok bool
)
aname = path.Clean(path.Join("/", aname))
dir, file := path.Split(aname)
switch dir {
case "/":
switch file {
case "":
f = fs.RootDir
case "users":
f = fs.UsersDir
case "sessions":
f = fs.SessionsDir
default:
return p9.QID{}, ErrFileNotExist
}
case "/users/":
f, err = fs.UsersDir.Walk(file)
if err != nil {
return p9.QID{}, err
}
case "/sessions/":
sessid, err := auth.ParseSessId(file)
if err != nil {
return p9.QID{}, err
}
f, ok = fs.SessionsDir.Get(sessid)
if !ok {
return p9.QID{}, ErrFileNotExist
}
default:
return p9.QID{}, ErrFileNotExist
}
if f == nil {
panic("unreachable")
}
qid, err := f.Qid()
if err != nil {
return p9.QID{}, err
}
if err := fs.attach(fid, f); err != nil {
return p9.QID{}, err
}
return qid, nil
}
func (fs *Authfs) attach(fid p9.FID, f File) error {
fs.mu.Lock()
defer fs.mu.Unlock()
if _, exists := fs.files[fid]; exists {
return ErrFidExist
}
fs.files[fid] = f
return nil
}
|