This repository has been archived on 2026-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
ollie-9p/cmd/ollie-9p-mount/main.go

98 lines
2.2 KiB
Go

// ollie-9p-mount — per-session FUSE proxy that attaches to olliesrv with a custom uname.
//
// Each agent session gets its own mount. Tools running inside the session see
// the full ollie namespace through $OLLIE, but all 9P operations carry the
// session's identity (Uname). The server can then enforce access control
// (e.g. rejecting self-prompts).
//
// Usage:
//
// ollie-9p-mount [-u uname] [-s service] [-addr address] <mountpoint>
//
// If -u is not given, $OLLIE_SESSION_ID is used.
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"9fans.net/go/plan9/client"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
)
var (
userFlag = flag.String("u", "", "uname for 9P attach (default: $OLLIE_SESSION_ID)")
serviceFlag = flag.String("s", "ollie", "9P service name")
netFlag = flag.String("net", "", "network type (unix, tcp); empty = namespace")
addrFlag = flag.String("addr", "", "9P address; empty = namespace service")
)
func main() {
flag.Parse()
if flag.NArg() != 1 {
fmt.Fprintf(os.Stderr, "usage: ollie-9p-mount [-u uname] [-s service] <mountpoint>\n")
os.Exit(1)
}
mnt := flag.Arg(0)
uname := *userFlag
if uname == "" {
uname = os.Getenv("OLLIE_SESSION_ID")
}
if uname == "" {
fmt.Fprintf(os.Stderr, "ollie-9p-mount: no uname (-u or $OLLIE_SESSION_ID)\n")
os.Exit(1)
}
fsys, err := dial(uname)
if err != nil {
log.Fatalf("dial: %v", err)
}
os.MkdirAll(mnt, 0755)
root := &p9Dir{fsys: fsys, path: ""}
server, err := fs.Mount(mnt, root, &fs.Options{
MountOptions: fuse.MountOptions{
FsName: "ollie-9p",
Name: "ollie",
DisableXAttrs: true,
},
})
if err != nil {
log.Fatalf("mount: %v", err)
}
// Unmount on signal.
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
server.Unmount()
}()
server.Wait()
}
func dial(uname string) (*client.Fsys, error) {
var conn *client.Conn
var err error
if *addrFlag != "" {
n := *netFlag
if n == "" {
n = "tcp"
}
conn, err = client.Dial(n, *addrFlag)
} else {
conn, err = client.DialService(*serviceFlag)
}
if err != nil {
return nil, err
}
return conn.Attach(nil, uname, "")
}