#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: nfs_srvsubs.c,v 1.17 2023/03/23 19:52:42 riastradh Exp $");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/filedesc.h>
#include <sys/time.h>
#include <sys/dirent.h>
#include <sys/once.h>
#include <sys/kauth.h>
#include <sys/atomic.h>
#include <uvm/uvm_extern.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
#include <nfs/nfsnode.h>
#include <nfs/nfs.h>
#include <nfs/xdr_subs.h>
#include <nfs/nfsm_subs.h>
#include <nfs/nfsmount.h>
#include <nfs/nfsrtt.h>
#include <nfs/nfs_var.h>
#include <miscfs/specfs/specdev.h>
#include <netinet/in.h>
int
nfs_namei(struct nameidata *ndp, nfsrvfh_t *nsfh, uint32_t len, struct nfssvc_sock *slp, struct mbuf *nam, struct mbuf **mdp, char **dposp, struct vnode **retdirp, int *dirattr_retp, struct vattr *dirattrp, struct lwp *l, int kerbflag, int pubflag)
{
int i, rem;
struct mbuf *md;
char *fromcp, *tocp, *cp, *path;
struct vnode *dp;
int error, rdonly;
int neverfollow;
struct componentname *cnp = &ndp->ni_cnd;
*retdirp = NULL;
ndp->ni_pathbuf = NULL;
if (len > NFS_MAXPATHLEN - 1)
return (ENAMETOOLONG);
if (len == 0)
return (EACCES);
path = PNBUF_GET();
fromcp = *dposp;
tocp = path;
md = *mdp;
rem = mtod(md, char *) + md->m_len - fromcp;
for (i = 0; i < len; i++) {
while (rem == 0) {
md = md->m_next;
if (md == NULL) {
error = EBADRPC;
goto out;
}
fromcp = mtod(md, void *);
rem = md->m_len;
}
if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
error = EACCES;
goto out;
}
*tocp++ = *fromcp++;
rem--;
}
*tocp = '\0';
*mdp = md;
*dposp = fromcp;
len = nfsm_rndup(len)-len;
if (len > 0) {
if (rem >= len)
*dposp += len;
else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
goto out;
}
error = nfsrv_fhtovp(nsfh, false, &dp, ndp->ni_cnd.cn_cred, slp,
nam, &rdonly, kerbflag, pubflag);
if (error)
goto out;
if (dp->v_type != VDIR) {
vrele(dp);
error = ENOTDIR;
goto out;
}
if (rdonly)
cnp->cn_flags |= RDONLY;
*retdirp = dp;
if (dirattr_retp != NULL) {
vn_lock(dp, LK_SHARED | LK_RETRY);
*dirattr_retp = VOP_GETATTR(dp, dirattrp, ndp->ni_cnd.cn_cred);
VOP_UNLOCK(dp);
}
if (pubflag) {
cp = PNBUF_GET();
fromcp = path;
tocp = cp;
if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
switch ((unsigned char)*fromcp) {
case WEBNFS_NATIVE_CHAR:
fromcp++;
break;
default:
error = EIO;
vrele(dp);
PNBUF_PUT(cp);
goto out;
}
}
while (*fromcp != '\0') {
if (*fromcp == WEBNFS_ESC_CHAR) {
if (fromcp[1] != '\0' && fromcp[2] != '\0') {
fromcp++;
*tocp++ = HEXSTRTOI(fromcp);
fromcp += 2;
continue;
} else {
error = ENOENT;
vrele(dp);
PNBUF_PUT(cp);
goto out;
}
} else
*tocp++ = *fromcp++;
}
*tocp = '\0';
PNBUF_PUT(path);
path = cp;
}
ndp->ni_atdir = NULL;
ndp->ni_pathbuf = pathbuf_assimilate(path);
if (ndp->ni_pathbuf == NULL) {
error = ENOMEM;
goto out;
}
if (pubflag) {
if (path[0] == '/')
dp = rootvnode;
} else {
cnp->cn_flags |= NOCROSSMOUNT;
}
neverfollow = !pubflag;
error = lookup_for_nfsd(ndp, dp, neverfollow);
if (error) {
goto out;
}
return 0;
out:
if (ndp->ni_pathbuf != NULL) {
pathbuf_destroy(ndp->ni_pathbuf);
ndp->ni_pathbuf = NULL;
} else {
PNBUF_PUT(path);
}
return (error);
}
int
nfsrv_fhtovp(nfsrvfh_t *nsfh, int lockflag, struct vnode **vpp,
kauth_cred_t cred, struct nfssvc_sock *slp, struct mbuf *nam, int *rdonlyp,
int kerbflag, int pubflag)
{
struct mount *mp;
kauth_cred_t credanon;
int error, exflags;
struct sockaddr_in *saddr;
fhandle_t *fhp;
fhp = NFSRVFH_FHANDLE(nsfh);
*vpp = (struct vnode *)0;
if (nfs_ispublicfh(nsfh)) {
if (!pubflag || !nfs_pub.np_valid)
return (ESTALE);
fhp = nfs_pub.np_handle;
}
error = netexport_check(&fhp->fh_fsid, nam, &mp, &exflags, &credanon);
if (error) {
return error;
}
error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, vpp);
if (error)
return (error);
if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
saddr = mtod(nam, struct sockaddr_in *);
if ((saddr->sin_family == AF_INET) &&
ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
vput(*vpp);
return (NFSERR_AUTHERR | AUTH_TOOWEAK);
}
if ((saddr->sin_family == AF_INET6) &&
ntohs(saddr->sin_port) >= IPV6PORT_RESERVED) {
vput(*vpp);
return (NFSERR_AUTHERR | AUTH_TOOWEAK);
}
}
if (exflags & MNT_EXKERB) {
if (!kerbflag) {
vput(*vpp);
return (NFSERR_AUTHERR | AUTH_TOOWEAK);
}
} else if (kerbflag) {
vput(*vpp);
return (NFSERR_AUTHERR | AUTH_TOOWEAK);
} else if (kauth_cred_geteuid(cred) == 0 ||
(exflags & MNT_EXPORTANON)) {
kauth_cred_clone(credanon, cred);
}
if (exflags & MNT_EXRDONLY)
*rdonlyp = 1;
else
*rdonlyp = 0;
if (!lockflag)
VOP_UNLOCK(*vpp);
return (0);
}
int
nfs_ispublicfh(const nfsrvfh_t *nsfh)
{
const char *cp = (const void *)(NFSRVFH_DATA(nsfh));
int i;
if (NFSRVFH_SIZE(nsfh) == 0) {
return true;
}
if (NFSRVFH_SIZE(nsfh) != NFSX_V2FH) {
return false;
}
for (i = 0; i < NFSX_V2FH; i++)
if (*cp++ != 0)
return false;
return true;
}
int
nfsrv_composefh(struct vnode *vp, nfsrvfh_t *nsfh, bool v3)
{
int error;
size_t fhsize;
fhsize = NFSD_MAXFHSIZE;
error = vfs_composefh(vp, (void *)NFSRVFH_DATA(nsfh), &fhsize);
if (NFSX_FHTOOBIG_P(fhsize, v3)) {
error = EOPNOTSUPP;
}
if (error != 0) {
return error;
}
if (!v3 && fhsize < NFSX_V2FH) {
memset((char *)NFSRVFH_DATA(nsfh) + fhsize, 0,
NFSX_V2FH - fhsize);
fhsize = NFSX_V2FH;
}
if ((fhsize % NFSX_UNSIGNED) != 0) {
return EOPNOTSUPP;
}
nsfh->nsfh_size = fhsize;
return 0;
}
int
nfsrv_comparefh(const nfsrvfh_t *fh1, const nfsrvfh_t *fh2)
{
if (NFSRVFH_SIZE(fh1) != NFSRVFH_SIZE(fh2)) {
return NFSRVFH_SIZE(fh2) - NFSRVFH_SIZE(fh1);
}
return memcmp(NFSRVFH_DATA(fh1), NFSRVFH_DATA(fh2), NFSRVFH_SIZE(fh1));
}
void
nfsrv_copyfh(nfsrvfh_t *fh1, const nfsrvfh_t *fh2)
{
size_t size;
fh1->nsfh_size = size = NFSRVFH_SIZE(fh2);
memcpy(NFSRVFH_DATA(fh1), NFSRVFH_DATA(fh2), size);
}