| 1 | |
| 2 | |
| 3 | package reversesshfs |
| 4 | |
| 5 | import ( |
| 6 | "encoding/binary" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "os" |
| 10 | "path" |
| 11 | "path/filepath" |
| 12 | "slices" |
| 13 | "strings" |
| 14 | "sync" |
| 15 | "time" |
| 16 | |
| 17 | "github.com/pkg/sftp" |
| 18 | ) |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | type rootedHandlers struct { |
| 29 | rootPath string |
| 30 | root *os.Root |
| 31 | cloud bool |
| 32 | readonly bool |
| 33 | readonlyNames []string |
| 34 | foldedNames []string |
| 35 | guardRoot string |
| 36 | rootedSys |
| 37 | |
| 38 | mu sync.Mutex |
| 39 | relay *relay |
| 40 | now func() time.Time |
| 41 | } |
| 42 | |
| 43 | |
| 44 | const maxHandles = 4096 |
| 45 | |
| 46 | func newRootedServer(rwc io.ReadWriteCloser, localPath string, readonly bool, readonlyNames []string) (*sftp.RequestServer, *rootedHandlers, error) { |
| 47 | return newRootedServerAt(rwc, localPath, slashPath(localPath), readonly, readonlyNames) |
| 48 | } |
| 49 | |
| 50 | func newRootedServerAt(rwc io.ReadWriteCloser, localPath, exportedPath string, readonly bool, readonlyNames []string) (*sftp.RequestServer, *rootedHandlers, error) { |
| 51 | root, err := os.OpenRoot(localPath) |
| 52 | if err != nil { |
| 53 | return nil, nil, err |
| 54 | } |
| 55 | sys, err := openRootedSys(localPath) |
| 56 | if err != nil { |
| 57 | root.Close() |
| 58 | return nil, nil, err |
| 59 | } |
| 60 | h := &rootedHandlers{ |
| 61 | rootPath: exportedPath, |
| 62 | root: root, |
| 63 | readonly: readonly, |
| 64 | readonlyNames: readonlyNames, |
| 65 | guardRoot: filepath.Clean(localPath), |
| 66 | rootedSys: sys, |
| 67 | relay: newRelay(), |
| 68 | now: time.Now, |
| 69 | } |
| 70 | for _, n := range readonlyNames { |
| 71 | h.foldedNames = append(h.foldedNames, FoldName(n)) |
| 72 | } |
| 73 | handlers := sftp.Handlers{FileGet: h, FilePut: h, FileCmd: h, FileList: h} |
| 74 | |
| 75 | |
| 76 | srv := sftp.NewRequestServer(rwc, handlers, sftp.WithStartDirectory(startDirectory(h.rootPath)), sftp.WithRSSequential(), |
| 77 | |
| 78 | sftp.WithRSMaxHandles(maxHandles), |
| 79 | |
| 80 | sftp.WithRSMaxTxPacket(64*1024)) |
| 81 | return srv, h, nil |
| 82 | } |
| 83 | |
| 84 | func (h *rootedHandlers) Close() error { |
| 85 | return errors.Join(h.root.Close(), h.rootedSys.close()) |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | func (h *rootedHandlers) rel(p string) (string, error) { |
| 91 | if !path.IsAbs(p) { |
| 92 | p = path.Join(h.rootPath, p) |
| 93 | } |
| 94 | p = path.Clean(p) |
| 95 | if p == h.rootPath { |
| 96 | return ".", nil |
| 97 | } |
| 98 | prefix := h.rootPath |
| 99 | if prefix != "/" { |
| 100 | prefix += "/" |
| 101 | } |
| 102 | if r, ok := strings.CutPrefix(p, prefix); ok { |
| 103 | return r, nil |
| 104 | } |
| 105 | return "", errDenied |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | func (h *rootedHandlers) writableRel(p string, entry bool) (string, error) { |
| 111 | if h.readonly { |
| 112 | return "", errDenied |
| 113 | } |
| 114 | r, err := h.rel(p) |
| 115 | if err != nil { |
| 116 | return "", err |
| 117 | } |
| 118 | if h.isReadonlyName(r) || !writableName(r) || (h.cloud && r == ".") { |
| 119 | return "", errDenied |
| 120 | } |
| 121 | if lexicalGuard { |
| 122 | if err := h.guard(r, entry); err != nil { |
| 123 | return "", err |
| 124 | } |
| 125 | } |
| 126 | return r, nil |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | var Guard func(root, rel string, entry bool) bool |
| 141 | |
| 142 | func (h *rootedHandlers) guard(rel string, entry bool) error { |
| 143 | if !h.cloud && Guard != nil && !Guard(h.guardRoot, rel, entry) { |
| 144 | return errDenied |
| 145 | } |
| 146 | return nil |
| 147 | } |
| 148 | |
| 149 | func (h *rootedHandlers) isReadonlyName(rel string) bool { |
| 150 | for _, c := range strings.Split(rel, "/") { |
| 151 | fc := FoldName(c) |
| 152 | if slices.Contains(h.foldedNames, fc) { |
| 153 | return true |
| 154 | } |
| 155 | } |
| 156 | return false |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | func sameName(s, name string) bool { |
| 162 | return FoldName(s) == FoldName(name) |
| 163 | } |
| 164 | |
| 165 | |
| 166 | func (h *rootedHandlers) Fileread(r *sftp.Request) (io.ReaderAt, error) { |
| 167 | rel, err := h.rel(r.Filepath) |
| 168 | if err != nil { |
| 169 | return nil, err |
| 170 | } |
| 171 | f, err := h.openRead(rel) |
| 172 | if err != nil { |
| 173 | return nil, err |
| 174 | } |
| 175 | return f, nil |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | func (h *rootedHandlers) openRead(rel string) (*os.File, error) { |
| 181 | f, err := h.root.OpenFile(rel, os.O_RDONLY|openNonblock, 0) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | fi, err := f.Stat() |
| 186 | if err != nil { |
| 187 | f.Close() |
| 188 | return nil, err |
| 189 | } |
| 190 | if !fi.Mode().IsRegular() && !fi.IsDir() { |
| 191 | f.Close() |
| 192 | return nil, &os.PathError{Op: "open", Path: rel, Err: errDenied} |
| 193 | } |
| 194 | return f, nil |
| 195 | } |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | type writableFile struct { |
| 201 | *os.File |
| 202 | appends bool |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | func (f writableFile) WriteAt(b []byte, off int64) (int, error) { |
| 209 | if f.appends { |
| 210 | return appendWrite(f.File, b) |
| 211 | } |
| 212 | return f.File.WriteAt(b, off) |
| 213 | } |
| 214 | |
| 215 | func (h *rootedHandlers) openWritable(r *sftp.Request) (writableFile, error) { |
| 216 | if !h.cloud && r.Pflags().Creat { |
| 217 | h.creating(r.Filepath) |
| 218 | } |
| 219 | f, err := h.openFile(r) |
| 220 | if err != nil { |
| 221 | return writableFile{}, err |
| 222 | } |
| 223 | return writableFile{File: f, appends: r.Pflags().Append}, nil |
| 224 | } |
| 225 | |
| 226 | |
| 227 | func (h *rootedHandlers) Filewrite(r *sftp.Request) (io.WriterAt, error) { |
| 228 | f, err := h.openWritable(r) |
| 229 | if err != nil { |
| 230 | return nil, err |
| 231 | } |
| 232 | return f, nil |
| 233 | } |
| 234 | |
| 235 | |
| 236 | func (h *rootedHandlers) OpenFile(r *sftp.Request) (sftp.WriterAtReaderAt, error) { |
| 237 | f, err := h.openWritable(r) |
| 238 | if err != nil { |
| 239 | return nil, err |
| 240 | } |
| 241 | return f, nil |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | const permMask = 0o777 |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | func openMode(r *sftp.Request) (uint32, bool) { |
| 253 | if len(r.Attrs) != 4 { |
| 254 | return 0, false |
| 255 | } |
| 256 | return binary.BigEndian.Uint32(r.Attrs) & permMask, true |
| 257 | } |
| 258 | |
| 259 | |
| 260 | func (h *rootedHandlers) Filecmd(r *sftp.Request) error { |
| 261 | switch r.Method { |
| 262 | case "Setstat": |
| 263 | |
| 264 | if f, ok := r.HandleFile().(writableFile); ok { |
| 265 | return h.fsetstat(f.File, r) |
| 266 | } |
| 267 | |
| 268 | if f, ok := r.HandleFile().(*os.File); ok && !h.isOpenAt(f, r.Filepath) { |
| 269 | return &os.PathError{Op: "setstat", Path: r.Filepath, Err: os.ErrNotExist} |
| 270 | } |
| 271 | if h.isNoopTimes(r) { |
| 272 | return nil |
| 273 | } |
| 274 | return h.setstat(r) |
| 275 | case "Rename": |
| 276 | return h.renaming(r, true) |
| 277 | case "Link": |
| 278 | h.creating(r.Target) |
| 279 | return h.link(r) |
| 280 | case "Remove", "Rmdir": |
| 281 | |
| 282 | |
| 283 | if h.consumeNoopRemoval(r.Filepath) { |
| 284 | return nil |
| 285 | } |
| 286 | h.removing(r.Filepath) |
| 287 | return h.removalFailed(r.Filepath, h.remove(r)) |
| 288 | case "Mkdir": |
| 289 | h.creating(r.Filepath) |
| 290 | return h.mkdir(r) |
| 291 | case "Symlink": |
| 292 | h.creating(r.Target) |
| 293 | return h.symlink(r) |
| 294 | } |
| 295 | return sftp.ErrSSHFxOpUnsupported |
| 296 | } |
| 297 | |
| 298 | |
| 299 | func (h *rootedHandlers) PosixRename(r *sftp.Request) error { |
| 300 | return h.renaming(r, false) |
| 301 | } |
| 302 | |
| 303 | func (h *rootedHandlers) renaming(r *sftp.Request, noReplace bool) error { |
| 304 | h.removing(r.Filepath) |
| 305 | h.creating(r.Target) |
| 306 | return h.removalFailed(r.Filepath, h.rename(r, noReplace)) |
| 307 | } |
| 308 | |
| 309 | |
| 310 | func (h *rootedHandlers) isOpenAt(f *os.File, p string) bool { |
| 311 | rel, err := h.rel(p) |
| 312 | if err != nil { |
| 313 | return false |
| 314 | } |
| 315 | fi, err := f.Stat() |
| 316 | if err != nil { |
| 317 | return false |
| 318 | } |
| 319 | at, err := h.root.Lstat(rel) |
| 320 | return err == nil && os.SameFile(fi, at) |
| 321 | } |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | func (h *rootedHandlers) isNoopTimes(r *sftp.Request) bool { |
| 328 | flags := r.AttrFlags() |
| 329 | if h.readonly || flags.Size || flags.UidGid || flags.Permissions || !flags.Acmodtime { |
| 330 | return false |
| 331 | } |
| 332 | rel, err := h.rel(r.Filepath) |
| 333 | if err != nil || !h.isReadonlyName(rel) { |
| 334 | return false |
| 335 | } |
| 336 | fi, err := h.root.Lstat(rel) |
| 337 | if err != nil { |
| 338 | return false |
| 339 | } |
| 340 | |
| 341 | mtime := uint32(fi.ModTime().Unix()) |
| 342 | attrs := r.Attributes() |
| 343 | return attrs != nil && attrs.Atime == mtime && attrs.Mtime == mtime |
| 344 | } |
| 345 | |
| 346 | |
| 347 | func (h *rootedHandlers) Filelist(r *sftp.Request) (sftp.ListerAt, error) { |
| 348 | rel, err := h.rel(r.Filepath) |
| 349 | if err != nil { |
| 350 | return nil, err |
| 351 | } |
| 352 | switch r.Method { |
| 353 | case "List": |
| 354 | f, err := h.openRead(rel) |
| 355 | if err != nil { |
| 356 | return nil, err |
| 357 | } |
| 358 | return &dirLister{f: f}, nil |
| 359 | case "Stat": |
| 360 | |
| 361 | var f *os.File |
| 362 | switch hf := r.HandleFile().(type) { |
| 363 | case writableFile: |
| 364 | f = hf.File |
| 365 | case *os.File: |
| 366 | f = hf |
| 367 | } |
| 368 | if f != nil { |
| 369 | fi, err := f.Stat() |
| 370 | if err != nil { |
| 371 | return nil, err |
| 372 | } |
| 373 | return listerAt{fi}, nil |
| 374 | } |
| 375 | fi, err := h.root.Stat(rel) |
| 376 | if err != nil { |
| 377 | return nil, err |
| 378 | } |
| 379 | return listerAt{fi}, nil |
| 380 | } |
| 381 | return nil, sftp.ErrSSHFxOpUnsupported |
| 382 | } |
| 383 | |
| 384 | |
| 385 | func (h *rootedHandlers) Lstat(r *sftp.Request) (sftp.ListerAt, error) { |
| 386 | rel, err := h.rel(r.Filepath) |
| 387 | if err != nil { |
| 388 | return nil, err |
| 389 | } |
| 390 | fi, err := h.root.Lstat(rel) |
| 391 | if err != nil { |
| 392 | return nil, err |
| 393 | } |
| 394 | return listerAt{fi}, nil |
| 395 | } |
| 396 | |
| 397 | |
| 398 | func (h *rootedHandlers) Readlink(p string) (string, error) { |
| 399 | rel, err := h.rel(p) |
| 400 | if err != nil { |
| 401 | return "", err |
| 402 | } |
| 403 | return h.root.Readlink(rel) |
| 404 | } |
| 405 | |
| 406 | |
| 407 | |
| 408 | func (h *rootedHandlers) RealPath(p string) (string, error) { |
| 409 | return realPath(h.rootPath, p), nil |
| 410 | } |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | type dirLister struct { |
| 420 | cloud bool |
| 421 | mu sync.Mutex |
| 422 | f *os.File |
| 423 | buf []os.FileInfo |
| 424 | next int64 |
| 425 | eof bool |
| 426 | } |
| 427 | |
| 428 | func (d *dirLister) ListAt(ls []os.FileInfo, offset int64) (int, error) { |
| 429 | d.mu.Lock() |
| 430 | defer d.mu.Unlock() |
| 431 | if offset != d.next { |
| 432 | return 0, &os.PathError{Op: "readdir", Path: d.f.Name(), Err: errors.ErrUnsupported} |
| 433 | } |
| 434 | for len(d.buf) < len(ls) && !d.eof { |
| 435 | fis, err := d.f.Readdir(len(ls) - len(d.buf)) |
| 436 | d.buf = append(d.buf, fis...) |
| 437 | if errors.Is(err, io.EOF) { |
| 438 | d.eof = true |
| 439 | } else if err != nil { |
| 440 | return 0, err |
| 441 | } |
| 442 | } |
| 443 | n := copy(ls, d.buf) |
| 444 | d.buf = d.buf[n:] |
| 445 | d.next += int64(n) |
| 446 | if n < len(ls) && d.eof { |
| 447 | return n, io.EOF |
| 448 | } |
| 449 | return n, nil |
| 450 | } |
| 451 | |
| 452 | func (d *dirLister) Close() error { |
| 453 | err := d.f.Close() |
| 454 | if d.cloud { |
| 455 | <-cloudSlots |
| 456 | } |
| 457 | return err |
| 458 | } |
| 459 | |
| 460 | type listerAt []os.FileInfo |
| 461 | |
| 462 | func (l listerAt) ListAt(ls []os.FileInfo, offset int64) (int, error) { |
| 463 | if offset >= int64(len(l)) { |
| 464 | return 0, io.EOF |
| 465 | } |
| 466 | n := copy(ls, l[offset:]) |
| 467 | if n < len(ls) { |
| 468 | return n, io.EOF |
| 469 | } |
| 470 | return n, nil |
| 471 | } |