跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

rooted_unix.go

348 行 · 9.3 KB历史下载
1//go:build linux || darwin
2
3package reversesshfs
4
5import (
6 "errors"
7 "os"
8 "path"
9 "path/filepath"
10 "strings"
11
12 "github.com/pkg/sftp"
13 "golang.org/x/sys/unix"
14)
15
16var errDenied error = unix.EACCES
17
18// lexicalGuard: Guard is asked about the path as the client sent it, and compares it folded (see FoldName).
19// On Windows the names are asked after the handles are open instead.
20const lexicalGuard = true
21
22// openNonblock: see openRead.
23const openNonblock = unix.O_NONBLOCK
24
25// appendWrite writes b at the end of f, which openFile opened with O_APPEND.
26// pwrite(2) would honor its offset on macOS.
27func appendWrite(f *os.File, b []byte) (int, error) {
28 return f.Write(b)
29}
30
31type rootedSys struct {
32 rootFD int
33}
34
35func openRootedSys(localPath string) (rootedSys, error) {
36 rootFD, err := unix.Open(localPath, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
37 if err != nil {
38 return rootedSys{}, &os.PathError{Op: "open", Path: localPath, Err: err}
39 }
40 return rootedSys{rootFD: rootFD}, nil
41}
42
43func (s rootedSys) close() error {
44 return unix.Close(s.rootFD)
45}
46
47// slashPath returns the request path of the host path p.
48func slashPath(p string) string {
49 return path.Clean(filepath.ToSlash(p))
50}
51
52func startDirectory(rootPath string) string {
53 return rootPath
54}
55
56func realPath(rootPath, p string) string {
57 if !path.IsAbs(p) {
58 p = path.Join(rootPath, p)
59 }
60 return path.Clean(p)
61}
62
63func writableName(string) bool {
64 return true
65}
66
67// openParent returns a directory fd for the parent of rel, and the base name.
68// The caller must close the fd.
69func (h *rootedHandlers) openParent(rel string) (int, string, error) {
70 fd, err := unix.Dup(h.rootFD)
71 if err != nil {
72 return -1, "", err
73 }
74 if rel == "." {
75 return fd, ".", nil
76 }
77 dir, base := path.Split(rel)
78 if dir != "" {
79 for _, c := range strings.Split(strings.TrimSuffix(dir, "/"), "/") {
80 if c == "" || c == "." || c == ".." {
81 unix.Close(fd)
82 return -1, "", unix.EACCES
83 }
84 next, err := h.openat(fd, c, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_CLOEXEC|unix.O_NONBLOCK, 0)
85 unix.Close(fd)
86 if err != nil {
87 return -1, "", err
88 }
89 fd = next
90 }
91 }
92 return fd, base, nil
93}
94
95func (h *rootedHandlers) openFile(r *sftp.Request) (*os.File, error) {
96 rel, err := h.writableRel(r.Filepath, false)
97 if err != nil {
98 return nil, err
99 }
100 pf := r.Pflags()
101 flags := unix.O_NOFOLLOW | unix.O_CLOEXEC
102 switch {
103 case pf.Read && (pf.Write || pf.Append):
104 flags |= unix.O_RDWR
105 case pf.Write || pf.Append:
106 flags |= unix.O_WRONLY
107 default:
108 flags |= unix.O_RDONLY
109 }
110 // See writableFile.WriteAt.
111 if pf.Append {
112 flags |= unix.O_APPEND
113 }
114 if pf.Creat {
115 flags |= unix.O_CREAT
116 }
117 if pf.Trunc && !h.cloud {
118 flags |= unix.O_TRUNC
119 }
120 if pf.Excl {
121 flags |= unix.O_EXCL
122 }
123 var mode uint32 = 0o644
124 if m, ok := openMode(r); ok {
125 mode = m
126 }
127 dirfd, base, err := h.openParent(rel)
128 if err != nil {
129 return nil, err
130 }
131 defer unix.Close(dirfd)
132 // O_NONBLOCK: a FIFO would block the server, which handles one request at a time.
133 // It has no effect on regular files, the only ones served.
134 fd, err := h.openat(dirfd, base, flags|unix.O_NONBLOCK, mode)
135 if err != nil {
136 return nil, &os.PathError{Op: "open", Path: r.Filepath, Err: err}
137 }
138 var st unix.Stat_t
139 if err := unix.Fstat(fd, &st); err != nil || st.Mode&unix.S_IFMT != unix.S_IFREG || (h.cloud && st.Nlink != 1) {
140 unix.Close(fd)
141 if err == nil {
142 err = errDenied
143 }
144 return nil, &os.PathError{Op: "open", Path: r.Filepath, Err: err}
145 }
146 if h.cloud && pf.Trunc {
147 if err := unix.Ftruncate(fd, 0); err != nil {
148 unix.Close(fd)
149 return nil, err
150 }
151 }
152 return os.NewFile(uintptr(fd), r.Filepath), nil
153}
154
155// inParent calls f with the parent directory fd and the base name of the writable path p.
156func (h *rootedHandlers) inParent(op, p string, f func(dirfd int, base string) error) error {
157 rel, err := h.writableRel(p, true)
158 if err != nil {
159 return err
160 }
161 dirfd, base, err := h.openParent(rel)
162 if err != nil {
163 return err
164 }
165 defer unix.Close(dirfd)
166 if err := f(dirfd, base); err != nil {
167 return &os.PathError{Op: op, Path: p, Err: err}
168 }
169 return nil
170}
171
172func (h *rootedHandlers) remove(r *sftp.Request) error {
173 var flags int
174 if r.Method == "Rmdir" {
175 flags = unix.AT_REMOVEDIR
176 }
177 return h.inParent(strings.ToLower(r.Method), r.Filepath, func(dirfd int, base string) error {
178 return unix.Unlinkat(dirfd, base, flags)
179 })
180}
181
182func (h *rootedHandlers) mkdir(r *sftp.Request) error {
183 var mode uint32 = 0o755
184 if a := r.Attributes(); a != nil && r.AttrFlags().Permissions {
185 mode = a.Mode & permMask
186 }
187 return h.inParent("mkdir", r.Filepath, func(dirfd int, base string) error {
188 return unix.Mkdirat(dirfd, base, mode)
189 })
190}
191
192// symlink has the link target in Filepath and the link path in Target.
193func (h *rootedHandlers) symlink(r *sftp.Request) error {
194 return h.inParent("symlink", r.Target, func(dirfd int, base string) error {
195 return unix.Symlinkat(r.Filepath, dirfd, base)
196 })
197}
198
199func (h *rootedHandlers) link(r *sftp.Request) error {
200 return h.twoPaths(r.Filepath, r.Target, func(oldfd int, oldBase string, newfd int, newBase string) error {
201 return unix.Linkat(oldfd, oldBase, newfd, newBase, 0)
202 })
203}
204
205func (h *rootedHandlers) rename(r *sftp.Request, noReplace bool) error {
206 return h.twoPaths(r.Filepath, r.Target, func(oldfd int, oldBase string, newfd int, newBase string) error {
207 if noReplace {
208 err := renameNoReplace(oldfd, oldBase, newfd, newBase)
209 if !errors.Is(err, unix.EINVAL) && !errors.Is(err, unix.ENOSYS) && !errors.Is(err, unix.ENOTSUP) && !errors.Is(err, unix.EOPNOTSUPP) {
210 return err
211 }
212 // The file system cannot do it atomically.
213 var st unix.Stat_t
214 if err := unix.Fstatat(newfd, newBase, &st, unix.AT_SYMLINK_NOFOLLOW); err == nil {
215 return os.ErrExist
216 }
217 }
218 return unix.Renameat(oldfd, oldBase, newfd, newBase)
219 })
220}
221
222func (h *rootedHandlers) twoPaths(oldPath, newPath string, f func(oldfd int, oldBase string, newfd int, newBase string) error) error {
223 oldRel, err := h.writableRel(oldPath, true)
224 if err != nil {
225 return err
226 }
227 newRel, err := h.writableRel(newPath, true)
228 if err != nil {
229 return err
230 }
231 oldfd, oldBase, err := h.openParent(oldRel)
232 if err != nil {
233 return err
234 }
235 defer unix.Close(oldfd)
236 newfd, newBase, err := h.openParent(newRel)
237 if err != nil {
238 return err
239 }
240 defer unix.Close(newfd)
241 if err := f(oldfd, oldBase, newfd, newBase); err != nil {
242 return &os.LinkError{Op: "rename", Old: oldPath, New: newPath, Err: err}
243 }
244 return nil
245}
246
247func (h *rootedHandlers) setstat(r *sftp.Request) error {
248 rel, err := h.writableRel(r.Filepath, false)
249 if err != nil {
250 return err
251 }
252 flags := r.AttrFlags()
253 attrs := r.Attributes()
254 if attrs == nil {
255 return unix.EINVAL
256 }
257 dirfd, base, err := h.openParent(rel)
258 if err != nil {
259 return err
260 }
261 defer unix.Close(dirfd)
262 if flags.Size {
263 fd, err := unix.Openat(dirfd, base, unix.O_WRONLY|unix.O_NOFOLLOW|unix.O_CLOEXEC|unix.O_NONBLOCK, 0)
264 if err != nil {
265 return &os.PathError{Op: "truncate", Path: r.Filepath, Err: err}
266 }
267 err = unix.Ftruncate(fd, int64(attrs.Size))
268 unix.Close(fd)
269 if err != nil {
270 return &os.PathError{Op: "truncate", Path: r.Filepath, Err: err}
271 }
272 }
273 if flags.Permissions {
274 if err := fchmodatNoFollow(dirfd, base, attrs.Mode&permMask); err != nil {
275 return &os.PathError{Op: "chmod", Path: r.Filepath, Err: err}
276 }
277 }
278 if flags.UidGid {
279 if err := unix.Fchownat(dirfd, base, int(attrs.UID), int(attrs.GID), unix.AT_SYMLINK_NOFOLLOW); err != nil {
280 return &os.PathError{Op: "chown", Path: r.Filepath, Err: err}
281 }
282 }
283 if flags.Acmodtime {
284 ts := []unix.Timespec{
285 unix.NsecToTimespec(int64(attrs.Atime) * 1e9),
286 unix.NsecToTimespec(int64(attrs.Mtime) * 1e9),
287 }
288 if err := unix.UtimesNanoAt(dirfd, base, ts, unix.AT_SYMLINK_NOFOLLOW); err != nil {
289 return &os.PathError{Op: "chtimes", Path: r.Filepath, Err: err}
290 }
291 }
292 return nil
293}
294
295// fsetstat applies a FSETSTAT to the file f of the handle.
296func (h *rootedHandlers) fsetstat(f *os.File, r *sftp.Request) error {
297 flags := r.AttrFlags()
298 attrs := r.Attributes()
299 if attrs == nil {
300 return unix.EINVAL
301 }
302 fd := int(f.Fd())
303 if flags.Size {
304 if err := unix.Ftruncate(fd, int64(attrs.Size)); err != nil {
305 return &os.PathError{Op: "truncate", Path: f.Name(), Err: err}
306 }
307 }
308 if flags.Permissions {
309 if err := unix.Fchmod(fd, attrs.Mode&permMask); err != nil {
310 return &os.PathError{Op: "chmod", Path: f.Name(), Err: err}
311 }
312 }
313 if flags.UidGid {
314 if err := unix.Fchown(fd, int(attrs.UID), int(attrs.GID)); err != nil {
315 return &os.PathError{Op: "chown", Path: f.Name(), Err: err}
316 }
317 }
318 if flags.Acmodtime {
319 tv := []unix.Timeval{
320 unix.NsecToTimeval(int64(attrs.Atime) * 1e9),
321 unix.NsecToTimeval(int64(attrs.Mtime) * 1e9),
322 }
323 if err := unix.Futimes(fd, tv); err != nil {
324 return &os.PathError{Op: "chtimes", Path: f.Name(), Err: err}
325 }
326 }
327 return nil
328}
329
330// StatVFS implements sftp.StatVFSFileCmder.
331func (h *rootedHandlers) StatVFS(r *sftp.Request) (*sftp.StatVFS, error) {
332 if _, err := h.rel(r.Filepath); err != nil {
333 return nil, err
334 }
335 var st unix.Statfs_t
336 if err := unix.Fstatfs(h.rootFD, &st); err != nil {
337 return nil, err
338 }
339 return statVFS(&st), nil
340}
341
342// Cloud shares stop at nested mounts, including when walking parent directories.
343func (h *rootedHandlers) openat(fd int, name string, flags int, mode uint32) (int, error) {
344 if h.cloud {
345 return cloudOpenat(fd, name, flags, mode)
346 }
347 return unix.Openat(fd, name, flags, mode)
348}