|
Some checks failed
Code scanning - action / CodeQL-Build (push) Has been cancelled
A Handler learned who was calling only in Mount, from the raw RPC header. Every other procedure ran for anyone holding a file handle, and ACCESS echoed the rights asked for. A server for one user on a shared machine, like a loopback mount, could not keep the other local users out. Two additions, both opt-in: - AuthUnixFromContext(ctx) (AuthUnix, bool) returns the parsed AUTH_UNIX credential (uid, gid, groups, machine name, stamp) of the call a context belongs to: the one Mount, FSStat and Authorize get. It reports false for AUTH_NULL, other flavors and a malformed AUTH_UNIX body, one over RFC 5531's bounds (255-byte name, 16 groups) or with trailing bytes. - Server.Authorize func(ctx, prog, vers, proc uint32) error is called before every procedure other than NULL. An error refuses the call with that procedure's own failure: NFS3ERR_ACCES and its resfail body (or the status of an *NFSStatusError, such as NFS3ERR_PERM or ROFS), MNT3ERR_ACCES or MNT3ERR_PERM, NFS4ERR_ACCESS or NFS4ERR_PERM with the COMPOUND's tag, and AUTH_ERROR AUTH_TOOWEAK for UMNT, which has no status. With Authorize set, a malformed AUTH_UNIX credential is rejected with AUTH_BADCRED before Authorize sees it. ACCESS asks Authorize about the procedure behind each right requested (READ and EXECUTE: READ, LOOKUP: LOOKUP, MODIFY and EXTEND: WRITE, DELETE: REMOVE) and leaves out the refused ones, so a client does not offer what the server will refuse. A hook on Server rather than an optional Handler interface, because handlers are usually wrapped in helpers.CachingHandler, which would hide it. Without Authorize nothing changes. MSG_DENIED replies, which nothing sent before, needed two fixes: they wrote the accept_stat-like ResponseCode where the reject_stat belongs, and AuthError and RPCMismatchError encoded their bodies little-endian. Checked on a macOS mount: the client sends AUTH_SYS whenever Mount offers AuthFlavorUnix, with the user's uid and up to 16 groups. A read-only policy lets cat through, makes test -w false and touch and rm fail with EACCES; refusing the mounting user makes mount_nfs fail with "Permission denied". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> |
||
|---|---|---|
| .github | ||
| example | ||
| file | ||
| helpers | ||
| auth.go | ||
| auth_test.go | ||
| auth_unix_test.go | ||
| conn.go | ||
| conn_concurrency_test.go | ||
| CONTRIBUTING.md | ||
| drc.go | ||
| drc_test.go | ||
| errors.go | ||
| file.go | ||
| filesystem.go | ||
| go.mod | ||
| go.sum | ||
| handler.go | ||
| LICENSE | ||
| locker.go | ||
| log.go | ||
| memlocker.go | ||
| memlocker_test.go | ||
| mount.go | ||
| mount_test.go | ||
| mountinterface.go | ||
| nfs.go | ||
| nfs4.go | ||
| nfs4_attrs.go | ||
| nfs4_onaccess.go | ||
| nfs4_onclose.go | ||
| nfs4_oncommit.go | ||
| nfs4_oncreate.go | ||
| nfs4_ongetattr.go | ||
| nfs4_ongetfh.go | ||
| nfs4_onlock.go | ||
| nfs4_onlockt.go | ||
| nfs4_onlocku.go | ||
| nfs4_onlookup.go | ||
| nfs4_onlookupp.go | ||
| nfs4_onopen.go | ||
| nfs4_onopenconfirm.go | ||
| nfs4_onopendowngrade.go | ||
| nfs4_onputfh.go | ||
| nfs4_onputrootfh.go | ||
| nfs4_onread.go | ||
| nfs4_onreaddir.go | ||
| nfs4_onreadlink.go | ||
| nfs4_onreleaselockowner.go | ||
| nfs4_onremove.go | ||
| nfs4_onrename.go | ||
| nfs4_onrenew.go | ||
| nfs4_onrestorefh.go | ||
| nfs4_onsavefh.go | ||
| nfs4_onsecinfo.go | ||
| nfs4_onsetattr.go | ||
| nfs4_onsetclientid.go | ||
| nfs4_onsetclientidconfirm.go | ||
| nfs4_onwrite.go | ||
| nfs4_state.go | ||
| nfs4_state_test.go | ||
| nfs4_test.go | ||
| nfs_concurrency_test.go | ||
| nfs_fsinfo_test.go | ||
| nfs_onaccess.go | ||
| nfs_oncommit.go | ||
| nfs_oncreate.go | ||
| nfs_onfsinfo.go | ||
| nfs_onfsstat.go | ||
| nfs_ongetattr.go | ||
| nfs_onlink.go | ||
| nfs_onlookup.go | ||
| nfs_onmkdir.go | ||
| nfs_onmknod.go | ||
| nfs_onpathconf.go | ||
| nfs_onread.go | ||
| nfs_onreaddir.go | ||
| nfs_onreaddirplus.go | ||
| nfs_onreadlink.go | ||
| nfs_onremove.go | ||
| nfs_onrename.go | ||
| nfs_onrmdir.go | ||
| nfs_onsetattr.go | ||
| nfs_onsymlink.go | ||
| nfs_onwrite.go | ||
| nfs_symlink_test.go | ||
| nfs_test.go | ||
| nfsinterface.go | ||
| README.md | ||
| SECURITY.md | ||
| server.go | ||
| server_test.go | ||
| time.go | ||
Golang Network File Server
NFSv3 protocol implementation in pure Golang.
Current Status:
- Minimally tested
- Mounts, read-only and read-write support
Usage
The most interesting demo is currently in example/osview.
Start the server
go run ./example/osview ..
The local folder at . will be the initial view in the mount. mutations to metadata or contents
will be stored purely in memory and not written back to the OS. When run, this
demo will print the port it is listening on.
The mount can be accessed using a command similar to
mount -o port=<n>,mountport=<n> -t nfs localhost:/mount <mountpoint> (For Mac users)
or
mount -o port=<n>,mountport=<n>,nfsvers=3,noacl,tcp -t nfs localhost:/mount <mountpoint> (For Linux users)
API
The NFS server runs on a net.Listener to export a file system to NFS clients.
Usage is structured similarly to many other golang network servers.
package main
import (
"fmt"
"log"
"net"
"github.com/go-git/go-billy/v5/memfs"
nfs "github.com/willscott/go-nfs"
nfshelper "github.com/willscott/go-nfs/helpers"
)
func main() {
listener, err := net.Listen("tcp", ":0")
panicOnErr(err, "starting TCP listener")
fmt.Printf("Server running at %s\n", listener.Addr())
mem := memfs.New()
f, err := mem.Create("hello.txt")
panicOnErr(err, "creating file")
_, err = f.Write([]byte("hello world"))
panicOnErr(err, "writing data")
f.Close()
handler := nfshelper.NewNullAuthHandler(mem)
cacheHelper := nfshelper.NewCachingHandler(handler, 1)
panicOnErr(nfs.Serve(listener, cacheHelper), "serving nfs")
}
func panicOnErr(err error, desc ...interface{}) {
if err == nil {
return
}
log.Println(desc...)
log.Panicln(err)
}
Notes
-
Ports are typically determined through portmap. The need for running portmap (which is the only part that needs a privileged listening port) can be avoided through specific mount options. e.g.
mount -o port=n,mountport=n -t nfs host:/mount /localmount -
This server currently uses billy to provide a file system abstraction layer. There are some edges of the NFS protocol which do not translate to this abstraction.
- NFS expects access to an
inodeor equivalent unique identifier to reference files in a file system. These are considered opaque identifiers here, which means they will not work as expected in cases of hard linking. - The billy abstraction layer does not extend to exposing
uidandgidownership of files. If ownership is important to your file system, you will need to ensure that theos.FileInfomeets additional constraints. In particular, theSys()escape hatch is queried by this library, and if your file system populates asyscall.Stat_tconcrete struct, the ownership specified in that object will be used. You can also return afile.FileInfowhich doesn't vary between platforms so may be easier to deal with.
- NFS expects access to an
-
Access control: NFS clients assert who is calling, they don't prove it. With
AUTH_UNIX(sec=sys) each call carries the caller's uid and gids, whichnfs.AuthUnixFromContextreturns. SetServer.Authorizeto refuse calls by caller and procedure, for instance to serve only the user who mounted on a single-user machine; ACCESS then reports only the rights it allows. TheHandler'sMountmust offernfs.AuthFlavorUnixfor clients to send these credentials. -
Relevant RFCS: 5531 - RPC protocol, 1813 - NFSv3, 1094 - NFS