agentbbs/internal/files/server.go
Anthony Ettinger 6dc94bd784 feat(files): SFTP member storage — private workspaces + shared public area + mgmt TUI
Implements M4 (Files). A fully virtual Go SFTP server (pkg/sftp + crypto/ssh,
no OS users) wired as an "sftp" subsystem on the existing :22 wish listener, so
members reach their files with their login key:

    sftp files@bbs.profullstack.com     # scp/rsync ride the same endpoint

Identity is the SSH key (the username is conventional/ignored). Two areas per
session: a private, quota-limited /me workspace and a single shared public file
area /public (old-school BBS file area; world-read, members-only write by
default, operator-moderated). This reverses the old NG1 "no sharing" boundary in
favour of one sanctioned, inspectable sharing surface (PRD §9.3 amended).

internal/files:
- backend.go  service, layout, quota/usage, live-session registry, operator API
- fs.go       per-session virtual FS; resolve() is the single security
              chokepoint (area confinement + symlink-escape guard) + pkg/sftp
              request handlers
- server.go   subsystem handler: key auth -> member session -> request server,
              with byte metering and force-disconnect
- tui.go      in-BBS member browser (hub plugin "Files")
- admin.go    operator management TUI: sessions, workspaces/quotas, public area

Operator console: ssh sftp@<host> (allowlist-gated; sftpadmin@/filesadmin@
aliases) — list/disconnect sessions, set per-user quotas, revoke SFTP access,
toggle public write, moderate the public area.

store: files_access (per-user quota override + revoked) and files_settings
(public-write mode) tables + methods. main.go wiring guarded by AGENTBBS_FILES
(+ AGENTBBS_FILES_QUOTA_MB, default 1 GiB). Route names reserved.

Tests (incl -race): path traversal/confinement, symlink-escape rejection,
public-write ACL, quota enforcement, usage accounting, and an end-to-end run
against a real SFTP client. Docs: docs/files.md; PRD §5.3/§5.3.1/§9.3 + README
updated.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 09:35:04 +00:00

94 lines
2.6 KiB
Go

package files
import (
"io"
"github.com/charmbracelet/ssh"
"github.com/pkg/sftp"
"github.com/profullstack/agentbbs/internal/auth"
)
// Subsystem returns the wish/ssh "sftp" subsystem handler. Wire it with
//
// wish.WithSubsystem("sftp", svc.Subsystem())
//
// so members reach their files over the existing :22 listener. Identity is the
// connecting SSH key (the username is ignored); access is refused for non-members,
// banned accounts, and members whose SFTP access an operator has revoked.
func (s *Service) Subsystem() ssh.SubsystemHandler {
return func(sess ssh.Session) {
fp := auth.Fingerprint(sess.PublicKey())
if fp == "" {
io.WriteString(sess.Stderr(), "files: an SSH key is required (try: sftp -i ~/.ssh/id_ed25519 ...)\n")
_ = sess.Exit(1)
return
}
u, ok, err := s.st.UserByFingerprint(fp)
if err != nil {
io.WriteString(sess.Stderr(), "files: account lookup failed\n")
_ = sess.Exit(1)
return
}
if !ok {
io.WriteString(sess.Stderr(), "files: this key isn't a member — register first: ssh join@\n")
_ = sess.Exit(1)
return
}
if u.Banned {
io.WriteString(sess.Stderr(), "files: this account is suspended\n")
_ = sess.Exit(1)
return
}
if fa, err := s.st.FilesAccess(u.ID); err == nil && fa.Revoked {
io.WriteString(sess.Stderr(), "files: SFTP access has been revoked for this account\n")
_ = sess.Exit(1)
return
}
fsSess, err := s.newSession(u)
if err != nil {
io.WriteString(sess.Stderr(), "files: could not open your workspace\n")
_ = sess.Exit(1)
return
}
rw := &countingRWC{inner: sess}
conn := s.reg.add(u.Name, fp, sess.RemoteAddr().String(), rw.Close)
rw.conn = conn
defer s.reg.remove(conn.id)
handlers := sftp.Handlers{FileGet: fsSess, FilePut: fsSess, FileCmd: fsSess, FileList: fsSess}
srv := sftp.NewRequestServer(rw, handlers)
defer srv.Close()
if err := srv.Serve(); err != nil && err != io.EOF {
io.WriteString(sess.Stderr(), "files: session ended\n")
}
}
}
// countingRWC wraps the SSH channel to meter bytes for the management TUI and to
// expose Close for force-disconnect. Read = bytes from the client (uploads);
// Write = bytes to the client (downloads).
type countingRWC struct {
inner io.ReadWriteCloser
conn *liveConn
}
func (c *countingRWC) Read(p []byte) (int, error) {
n, err := c.inner.Read(p)
if c.conn != nil {
c.conn.rxBytes.Add(int64(n))
}
return n, err
}
func (c *countingRWC) Write(p []byte) (int, error) {
n, err := c.inner.Write(p)
if c.conn != nil {
c.conn.txBytes.Add(int64(n))
}
return n, err
}
func (c *countingRWC) Close() error { return c.inner.Close() }