2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 * These functions support the macros and help fiddle mbuf chains for
42 * the nfs op functions. They do things like create the rpc header and
43 * copy data between mbuf chains and uio lists.
45 #include <fs/nfs/nfsport.h>
47 extern u_int32_t newnfs_true, newnfs_false;
48 extern int nfs_pubfhset;
49 extern struct nfsclienthashhead *nfsclienthash;
50 extern int nfsrv_clienthashsize;
51 extern struct nfslockhashhead *nfslockhash;
52 extern int nfsrv_lockhashsize;
53 extern struct nfssessionhash *nfssessionhash;
54 extern int nfsrv_sessionhashsize;
55 extern int nfsrv_useacl;
56 extern uid_t nfsrv_defaultuid;
57 extern gid_t nfsrv_defaultgid;
59 char nfs_v2pubfh[NFSX_V2FH];
60 struct nfsdontlisthead nfsrv_dontlisthead;
61 struct nfslayouthead nfsrv_recalllisthead;
62 static nfstype newnfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK,
63 NFNON, NFCHR, NFNON };
64 extern nfstype nfsv34_type[9];
65 #endif /* !APPLEKEXT */
67 static u_int32_t nfsrv_isannfserr(u_int32_t);
69 SYSCTL_DECL(_vfs_nfsd);
71 static int enable_checkutf8 = 1;
72 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_checkutf8, CTLFLAG_RW,
74 "Enable the NFSv4 check for the UTF8 compliant name required by rfc3530");
76 static int enable_nobodycheck = 1;
77 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nobodycheck, CTLFLAG_RW,
78 &enable_nobodycheck, 0,
79 "Enable the NFSv4 check when setting user nobody as owner");
81 static int enable_nogroupcheck = 1;
82 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nogroupcheck, CTLFLAG_RW,
83 &enable_nogroupcheck, 0,
84 "Enable the NFSv4 check when setting group nogroup as owner");
86 static char nfsrv_hexdigit(char, int *);
89 * Maps errno values to nfs error numbers.
90 * Use NFSERR_IO as the catch all for ones not specifically defined in
91 * RFC 1094. (It now includes the errors added for NFSv3.)
93 static u_char nfsrv_v2errmap[NFSERR_REMOTE] = {
94 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
95 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
96 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
97 NFSERR_IO, NFSERR_EXIST, NFSERR_XDEV, NFSERR_NODEV, NFSERR_NOTDIR,
98 NFSERR_ISDIR, NFSERR_INVAL, NFSERR_IO, NFSERR_IO, NFSERR_IO,
99 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
100 NFSERR_MLINK, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
101 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
102 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
103 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
104 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
105 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
106 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
107 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
112 * Maps errno values to nfs error numbers.
113 * Although it is not obvious whether or not NFS clients really care if
114 * a returned error value is in the specified list for the procedure, the
115 * safest thing to do is filter them appropriately. For Version 2, the
116 * X/Open XNFS document is the only specification that defines error values
117 * for each RPC (The RFC simply lists all possible error values for all RPCs),
118 * so I have decided to not do this for Version 2.
119 * The first entry is the default error return and the rest are the valid
120 * errors for that RPC in increasing numeric order.
122 static short nfsv3err_null[] = {
127 static short nfsv3err_getattr[] = {
137 static short nfsv3err_setattr[] = {
154 static short nfsv3err_lookup[] = {
168 static short nfsv3err_access[] = {
178 static short nfsv3err_readlink[] = {
191 static short nfsv3err_read[] = {
204 static short nfsv3err_write[] = {
220 static short nfsv3err_create[] = {
238 static short nfsv3err_mkdir[] = {
256 static short nfsv3err_symlink[] = {
274 static short nfsv3err_mknod[] = {
293 static short nfsv3err_remove[] = {
308 static short nfsv3err_rmdir[] = {
327 static short nfsv3err_rename[] = {
351 static short nfsv3err_link[] = {
372 static short nfsv3err_readdir[] = {
386 static short nfsv3err_readdirplus[] = {
401 static short nfsv3err_fsstat[] = {
411 static short nfsv3err_fsinfo[] = {
420 static short nfsv3err_pathconf[] = {
429 static short nfsv3err_commit[] = {
439 static short *nfsrv_v3errmap[] = {
457 nfsv3err_readdirplus,
465 * And the same for V4.
467 static short nfsv4err_null[] = {
472 static short nfsv4err_access[] = {
489 static short nfsv4err_close[] = {
513 static short nfsv4err_commit[] = {
531 static short nfsv4err_create[] = {
560 static short nfsv4err_delegpurge[] = {
568 NFSERR_STALECLIENTID,
572 static short nfsv4err_delegreturn[] = {
591 static short nfsv4err_getattr[] = {
608 static short nfsv4err_getfh[] = {
620 static short nfsv4err_link[] = {
652 static short nfsv4err_lock[] = {
678 NFSERR_RECLAIMCONFLICT,
682 NFSERR_STALECLIENTID,
687 static short nfsv4err_lockt[] = {
706 NFSERR_STALECLIENTID,
710 static short nfsv4err_locku[] = {
736 static short nfsv4err_lookup[] = {
759 static short nfsv4err_lookupp[] = {
775 static short nfsv4err_nverify[] = {
795 static short nfsv4err_open[] = {
826 NFSERR_RECLAIMCONFLICT,
832 NFSERR_STALECLIENTID,
838 static short nfsv4err_openattr[] = {
859 static short nfsv4err_openconfirm[] = {
880 static short nfsv4err_opendowngrade[] = {
900 static short nfsv4err_putfh[] = {
913 static short nfsv4err_putpubfh[] = {
921 static short nfsv4err_putrootfh[] = {
929 static short nfsv4err_read[] = {
957 static short nfsv4err_readdir[] = {
978 static short nfsv4err_readlink[] = {
996 static short nfsv4err_remove[] = {
1011 NFSERR_NOFILEHANDLE,
1021 static short nfsv4err_rename[] = {
1038 NFSERR_NOFILEHANDLE,
1051 static short nfsv4err_renew[] = {
1054 NFSERR_ADMINREVOKED,
1061 NFSERR_STALECLIENTID,
1065 static short nfsv4err_restorefh[] = {
1078 static short nfsv4err_savefh[] = {
1083 NFSERR_NOFILEHANDLE,
1090 static short nfsv4err_secinfo[] = {
1102 NFSERR_NOFILEHANDLE,
1110 static short nfsv4err_setattr[] = {
1113 NFSERR_ADMINREVOKED,
1131 NFSERR_NOFILEHANDLE,
1140 NFSERR_STALESTATEID,
1144 static short nfsv4err_setclientid[] = {
1155 static short nfsv4err_setclientidconfirm[] = {
1161 NFSERR_STALECLIENTID,
1165 static short nfsv4err_verify[] = {
1176 NFSERR_NOFILEHANDLE,
1184 static short nfsv4err_write[] = {
1187 NFSERR_ADMINREVOKED,
1203 NFSERR_NOFILEHANDLE,
1212 NFSERR_STALESTATEID,
1216 static short nfsv4err_releaselockowner[] = {
1218 NFSERR_ADMINREVOKED,
1225 NFSERR_STALECLIENTID,
1229 static short *nfsrv_v4errmap[] = {
1237 nfsv4err_delegpurge,
1238 nfsv4err_delegreturn,
1250 nfsv4err_openconfirm,
1251 nfsv4err_opendowngrade,
1265 nfsv4err_setclientid,
1266 nfsv4err_setclientidconfirm,
1269 nfsv4err_releaselockowner,
1273 * A fiddled version of m_adj() that ensures null fill to a long
1274 * boundary and only trims off the back end
1277 nfsrv_adj(mbuf_t mp, int len, int nul)
1284 * Trim from tail. Scan the mbuf chain,
1285 * calculating its length and finding the last mbuf.
1286 * If the adjustment only affects this mbuf, then just
1287 * adjust and return. Otherwise, rescan and truncate
1288 * after the remaining size.
1293 count += mbuf_len(m);
1294 if (mbuf_next(m) == NULL)
1298 if (mbuf_len(m) > len) {
1299 mbuf_setlen(m, mbuf_len(m) - len);
1301 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1302 for (i = 0; i < nul; i++)
1311 * Correct length for chain is "count".
1312 * Find the mbuf with last data, adjust its length,
1313 * and toss data from remaining mbufs on chain.
1315 for (m = mp; m; m = mbuf_next(m)) {
1316 if (mbuf_len(m) >= count) {
1317 mbuf_setlen(m, count);
1319 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1320 for (i = 0; i < nul; i++)
1325 count -= mbuf_len(m);
1327 for (m = mbuf_next(m); m; m = mbuf_next(m))
1332 * Make these functions instead of macros, so that the kernel text size
1333 * doesn't get too big...
1336 nfsrv_wcc(struct nfsrv_descript *nd, int before_ret,
1337 struct nfsvattr *before_nvap, int after_ret, struct nfsvattr *after_nvap)
1342 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1345 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1346 *tl++ = newnfs_true;
1347 txdr_hyper(before_nvap->na_size, tl);
1349 txdr_nfsv3time(&(before_nvap->na_mtime), tl);
1351 txdr_nfsv3time(&(before_nvap->na_ctime), tl);
1353 nfsrv_postopattr(nd, after_ret, after_nvap);
1357 nfsrv_postopattr(struct nfsrv_descript *nd, int after_ret,
1358 struct nfsvattr *after_nvap)
1362 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1367 nfsrv_fillattr(nd, after_nvap);
1372 * Fill in file attributes for V2 and 3. For V4, call a separate
1373 * routine that sifts through all the attribute bits.
1376 nfsrv_fillattr(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1378 struct nfs_fattr *fp;
1382 * Build space for the attribute structure.
1384 if (nd->nd_flag & ND_NFSV3)
1385 fattr_size = NFSX_V3FATTR;
1387 fattr_size = NFSX_V2FATTR;
1388 NFSM_BUILD(fp, struct nfs_fattr *, fattr_size);
1391 * Now just fill it all in.
1393 fp->fa_nlink = txdr_unsigned(nvap->na_nlink);
1394 fp->fa_uid = txdr_unsigned(nvap->na_uid);
1395 fp->fa_gid = txdr_unsigned(nvap->na_gid);
1396 if (nd->nd_flag & ND_NFSV3) {
1397 fp->fa_type = vtonfsv34_type(nvap->na_type);
1398 fp->fa_mode = vtonfsv34_mode(nvap->na_mode);
1399 txdr_hyper(nvap->na_size, &fp->fa3_size);
1400 txdr_hyper(nvap->na_bytes, &fp->fa3_used);
1401 fp->fa3_rdev.specdata1 = txdr_unsigned(NFSMAJOR(nvap->na_rdev));
1402 fp->fa3_rdev.specdata2 = txdr_unsigned(NFSMINOR(nvap->na_rdev));
1403 fp->fa3_fsid.nfsuquad[0] = 0;
1404 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(nvap->na_fsid);
1405 txdr_hyper(nvap->na_fileid, &fp->fa3_fileid);
1406 txdr_nfsv3time(&nvap->na_atime, &fp->fa3_atime);
1407 txdr_nfsv3time(&nvap->na_mtime, &fp->fa3_mtime);
1408 txdr_nfsv3time(&nvap->na_ctime, &fp->fa3_ctime);
1410 fp->fa_type = vtonfsv2_type(nvap->na_type);
1411 fp->fa_mode = vtonfsv2_mode(nvap->na_type, nvap->na_mode);
1412 fp->fa2_size = txdr_unsigned(nvap->na_size);
1413 fp->fa2_blocksize = txdr_unsigned(nvap->na_blocksize);
1414 if (nvap->na_type == VFIFO)
1415 fp->fa2_rdev = 0xffffffff;
1417 fp->fa2_rdev = txdr_unsigned(nvap->na_rdev);
1418 fp->fa2_blocks = txdr_unsigned(nvap->na_bytes / NFS_FABLKSIZE);
1419 fp->fa2_fsid = txdr_unsigned(nvap->na_fsid);
1420 fp->fa2_fileid = txdr_unsigned(nvap->na_fileid);
1421 txdr_nfsv2time(&nvap->na_atime, &fp->fa2_atime);
1422 txdr_nfsv2time(&nvap->na_mtime, &fp->fa2_mtime);
1423 txdr_nfsv2time(&nvap->na_ctime, &fp->fa2_ctime);
1428 * This function gets a file handle out of an mbuf list.
1429 * It returns 0 for success, EBADRPC otherwise.
1430 * If sets the third flagp argument to 1 if the file handle is
1431 * the public file handle.
1432 * For NFSv4, if the length is incorrect, set nd_repstat == NFSERR_BADHANDLE
1435 nfsrv_mtofh(struct nfsrv_descript *nd, struct nfsrvfh *fhp)
1438 int error = 0, len, copylen;
1440 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1441 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1442 len = fxdr_unsigned(int, *tl);
1443 if (len == 0 && nfs_pubfhset && (nd->nd_flag & ND_NFSV3) &&
1444 nd->nd_procnum == NFSPROC_LOOKUP) {
1445 nd->nd_flag |= ND_PUBLOOKUP;
1450 /* If len == NFSX_V4PNFSFH the RPC is a pNFS DS one. */
1451 if (len == NFSX_V4PNFSFH && (nd->nd_flag & ND_NFSV41) != 0) {
1452 copylen = NFSX_MYFH;
1453 len = NFSM_RNDUP(len);
1454 nd->nd_flag |= ND_DSSERVER;
1455 } else if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1456 if (nd->nd_flag & ND_NFSV4) {
1457 if (len > 0 && len <= NFSX_V4FHMAX) {
1458 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1461 nd->nd_repstat = NFSERR_BADHANDLE;
1474 * For NFSv2, the file handle is always 32 bytes on the
1475 * wire, but this server only cares about the first
1476 * NFSRV_MAXFH bytes.
1479 copylen = NFSRV_MAXFH;
1481 NFSM_DISSECT(tl, u_int32_t *, len);
1482 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1483 nd->nd_procnum == NFSPROC_LOOKUP &&
1484 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1485 nd->nd_flag |= ND_PUBLOOKUP;
1488 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1489 fhp->nfsrvfh_len = copylen;
1491 NFSEXITCODE2(error, nd);
1496 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1497 * numbers not specified for the associated procedure.
1498 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1499 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1500 * RPC procedure is not involved.
1501 * Returns the error number in XDR.
1504 nfsd_errmap(struct nfsrv_descript *nd)
1506 short *defaulterrp, *errp;
1508 if (!nd->nd_repstat)
1510 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1511 if (nd->nd_procnum == NFSPROC_NOOP)
1512 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1513 if (nd->nd_flag & ND_NFSV3)
1514 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1515 else if (nd->nd_repstat == EBADRPC)
1516 return (txdr_unsigned(NFSERR_BADXDR));
1517 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1518 nd->nd_repstat == NFSERR_OPILLEGAL)
1519 return (txdr_unsigned(nd->nd_repstat));
1520 else if ((nd->nd_flag & ND_NFSV41) != 0) {
1521 if (nd->nd_repstat == EOPNOTSUPP)
1522 nd->nd_repstat = NFSERR_NOTSUPP;
1523 nd->nd_repstat = nfsrv_isannfserr(nd->nd_repstat);
1524 return (txdr_unsigned(nd->nd_repstat));
1526 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1528 if (*errp == nd->nd_repstat)
1529 return (txdr_unsigned(nd->nd_repstat));
1530 return (txdr_unsigned(*defaulterrp));
1532 if (nd->nd_repstat <= NFSERR_REMOTE)
1533 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1534 return (txdr_unsigned(NFSERR_IO));
1538 * Check to see if the error is a valid NFS one. If not, replace it with
1542 nfsrv_isannfserr(u_int32_t errval)
1545 if (errval == NFSERR_OK)
1547 if (errval >= NFSERR_BADHANDLE && errval <= NFSERR_DELEGREVOKED)
1549 if (errval > 0 && errval <= NFSERR_REMOTE)
1550 return (nfsrv_v2errmap[errval - 1]);
1555 * Check to see if setting a uid/gid is permitted when creating a new
1556 * file object. (Called when uid and/or gid is specified in the
1557 * settable attributes for V4.
1560 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1565 * If not setting either uid nor gid, it's OK.
1567 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1569 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid == nfsrv_defaultuid &&
1570 enable_nobodycheck == 1)
1571 || (NFSVNO_ISSETGID(nvap) && nvap->na_gid == nfsrv_defaultgid &&
1572 enable_nogroupcheck == 1)) {
1573 error = NFSERR_BADOWNER;
1576 if (nd->nd_cred->cr_uid == 0)
1578 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1579 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1580 !groupmember(nvap->na_gid, nd->nd_cred)))
1581 error = NFSERR_PERM;
1584 NFSEXITCODE2(error, nd);
1589 * and this routine fixes up the settable attributes for V4 if allowed
1590 * by nfsrv_checkuidgid().
1593 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1594 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1595 struct nfsexstuff *exp)
1598 struct nfsvattr nva;
1601 nfsattrbit_t nattrbits;
1604 * Maybe this should be done for V2 and 3 but it never has been
1605 * and nobody seems to be upset, so I think it's best not to change
1606 * the V2 and 3 semantics.
1608 if ((nd->nd_flag & ND_NFSV4) == 0)
1610 NFSVNO_ATTRINIT(&nva);
1611 NFSZERO_ATTRBIT(&nattrbits);
1612 tuid = nd->nd_cred->cr_uid;
1613 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1614 NFSVNO_ISSETUID(nvap) &&
1615 nvap->na_uid != nd->nd_cred->cr_uid) {
1616 if (nd->nd_cred->cr_uid == 0) {
1617 nva.na_uid = nvap->na_uid;
1619 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1621 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1624 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1625 NFSVNO_ISSETATIME(nvap)) {
1626 nva.na_atime = nvap->na_atime;
1628 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1630 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1631 NFSVNO_ISSETMTIME(nvap)) {
1632 nva.na_mtime = nvap->na_mtime;
1634 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1636 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1637 NFSVNO_ISSETGID(nvap)) {
1638 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1639 groupmember(nvap->na_gid, nd->nd_cred)) {
1640 nd->nd_cred->cr_uid = 0;
1641 nva.na_gid = nvap->na_gid;
1643 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1645 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1649 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1651 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1654 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1655 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1656 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1658 #ifdef NFS4_ACL_EXTATTR_NAME
1659 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1660 nfsrv_useacl != 0 && aclp != NULL) {
1661 if (aclp->acl_cnt > 0) {
1662 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1664 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1669 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1670 nd->nd_cred->cr_uid = tuid;
1673 NFSEXITCODE2(0, nd);
1677 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1678 * char isn't a hex digit.
1681 nfsrv_hexdigit(char c, int *err)
1685 if (c >= '0' && c <= '9')
1687 if (c >= 'a' && c <= 'f')
1688 return (c - 'a' + ((char)10));
1689 if (c >= 'A' && c <= 'F')
1690 return (c - 'A' + ((char)10));
1693 return (1); /* BOGUS */
1697 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1701 nfsrv_errmoved(int op)
1705 errp = nfsrv_v4errmap[op];
1706 while (*errp != 0) {
1707 if (*errp == NFSERR_MOVED)
1715 * Fill in attributes for a Referral.
1716 * (Return the number of bytes of XDR created.)
1719 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1720 struct nfsreferral *refp, int getattr, int *reterrp)
1722 u_int32_t *tl, *retnump;
1724 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1725 int fslocationsbit = 0;
1726 nfsattrbit_t tmpbits, refbits;
1728 NFSREFERRAL_ATTRBIT(&refbits);
1730 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1731 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1733 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1737 * Check for the case where unsupported referral attributes are
1740 NFSSET_ATTRBIT(&tmpbits, retbitp);
1741 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1742 if (NFSNONZERO_ATTRBIT(&tmpbits))
1745 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1746 *reterrp = NFSERR_MOVED;
1751 * Now we can fill in the attributes.
1753 NFSSET_ATTRBIT(&tmpbits, retbitp);
1754 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1757 * Put out the attribute bitmap for the ones being filled in
1758 * and get the field for the number of attributes returned.
1760 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1761 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1762 prefixnum += NFSX_UNSIGNED;
1765 * Now, loop around filling in the attributes for each bit set.
1767 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1768 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1770 case NFSATTRBIT_TYPE:
1771 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1772 *tl = txdr_unsigned(NFDIR);
1773 retnum += NFSX_UNSIGNED;
1775 case NFSATTRBIT_FSID:
1776 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1778 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1780 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1781 retnum += NFSX_V4FSID;
1783 case NFSATTRBIT_RDATTRERROR:
1784 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1786 *tl = txdr_unsigned(NFSERR_MOVED);
1789 retnum += NFSX_UNSIGNED;
1791 case NFSATTRBIT_FSLOCATIONS:
1792 retnum += nfsm_strtom(nd, "/", 1);
1793 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1794 *tl = txdr_unsigned(refp->nfr_srvcnt);
1795 retnum += NFSX_UNSIGNED;
1796 cp = refp->nfr_srvlist;
1797 for (i = 0; i < refp->nfr_srvcnt; i++) {
1798 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1799 *tl = txdr_unsigned(1);
1800 retnum += NFSX_UNSIGNED;
1801 cp2 = STRCHR(cp, ':');
1806 retnum += nfsm_strtom(nd, cp, len);
1809 cp2 = STRCHR(cp, ',');
1814 retnum += nfsm_strtom(nd, cp, len);
1819 case NFSATTRBIT_MOUNTEDONFILEID:
1820 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1821 txdr_hyper(refp->nfr_dfileno, tl);
1822 retnum += NFSX_HYPER;
1825 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1829 *retnump = txdr_unsigned(retnum);
1830 return (retnum + prefixnum);
1834 * Parse a file name out of a request.
1837 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1838 NFSPATHLEN_T *outlenp)
1840 char *fromcp, *tocp, val = '\0';
1843 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1852 * For V4, check for lookup parent.
1853 * Otherwise, get the component name.
1855 if ((nd->nd_flag & ND_NFSV4) && nd->nd_procnum == NFSV4OP_LOOKUPP) {
1857 hash += ((u_char)'.');
1859 hash += ((u_char)'.');
1863 * First, get the name length.
1865 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1866 len = fxdr_unsigned(int, *tl);
1867 if (len > NFS_MAXNAMLEN) {
1868 nd->nd_repstat = NFSERR_NAMETOL;
1871 } else if (len <= 0) {
1872 nd->nd_repstat = NFSERR_INVAL;
1878 * Now, copy the component name into the buffer.
1880 fromcp = nd->nd_dpos;
1882 rem = NFSMTOD(md, caddr_t) + mbuf_len(md) - fromcp;
1883 for (i = 0; i < len; i++) {
1890 fromcp = NFSMTOD(md, caddr_t);
1893 if (*fromcp == '\0') {
1894 nd->nd_repstat = EACCES;
1899 * For lookups on the public filehandle, do some special
1900 * processing on the name. (The public file handle is the
1901 * root of the public file system for this server.)
1903 if (nd->nd_flag & ND_PUBLOOKUP) {
1905 * If the first char is ASCII, it is a canonical
1906 * path, otherwise it is a native path. (RFC2054
1907 * doesn't actually state what it is if the first
1908 * char isn't ASCII or 0x80, so I assume native.)
1909 * pubtype == 1 -> native path
1910 * pubtype == 2 -> canonical path
1913 if (*fromcp & 0x80) {
1915 * Since RFC2054 doesn't indicate
1916 * that a native path of just 0x80
1917 * isn't allowed, I'll replace the
1918 * 0x80 with '/' instead of just
1928 * '/' only allowed in a native path
1930 if (*fromcp == '/' && pubtype != 1) {
1931 nd->nd_repstat = EACCES;
1937 * For the special case of 2 hex digits after a
1938 * '%' in an absolute path, calculate the value.
1939 * percent == 1 -> indicates "get first hex digit"
1940 * percent == 2 -> indicates "get second hex digit"
1943 digit = nfsrv_hexdigit(*fromcp, &error);
1945 nd->nd_repstat = EACCES;
1956 hash += ((u_char)val);
1960 if (*fromcp == '%' && pubtype == 2) {
1962 * Must be followed by 2 hex digits
1964 if ((len - i) < 3) {
1965 nd->nd_repstat = EACCES;
1972 hash += ((u_char)*fromcp);
1978 * Normal, non lookup on public, name.
1980 if (*fromcp == '/') {
1981 if (nd->nd_flag & ND_NFSV4)
1982 nd->nd_repstat = NFSERR_BADNAME;
1984 nd->nd_repstat = EACCES;
1988 hash += ((u_char)*fromcp);
1996 nd->nd_dpos = fromcp;
1997 i = NFSM_RNDUP(len) - len;
2002 error = nfsm_advance(nd, i, rem);
2009 * For v4, don't allow lookups of '.' or '..' and
2010 * also check for non-utf8 strings.
2012 if (nd->nd_flag & ND_NFSV4) {
2013 if ((outlen == 1 && bufp[0] == '.') ||
2014 (outlen == 2 && bufp[0] == '.' &&
2016 nd->nd_repstat = NFSERR_BADNAME;
2020 if (enable_checkutf8 == 1 &&
2021 nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
2022 nd->nd_repstat = NFSERR_INVAL;
2029 *outlenp = (size_t)outlen;
2033 NFSEXITCODE2(error, nd);
2041 static int inited = 0;
2048 * Initialize client queues. Don't free/reinitialize
2049 * them when nfsds are restarted.
2051 nfsclienthash = malloc(sizeof(struct nfsclienthashhead) *
2052 nfsrv_clienthashsize, M_NFSDCLIENT, M_WAITOK | M_ZERO);
2053 for (i = 0; i < nfsrv_clienthashsize; i++)
2054 LIST_INIT(&nfsclienthash[i]);
2055 nfslockhash = malloc(sizeof(struct nfslockhashhead) *
2056 nfsrv_lockhashsize, M_NFSDLOCKFILE, M_WAITOK | M_ZERO);
2057 for (i = 0; i < nfsrv_lockhashsize; i++)
2058 LIST_INIT(&nfslockhash[i]);
2059 nfssessionhash = malloc(sizeof(struct nfssessionhash) *
2060 nfsrv_sessionhashsize, M_NFSDSESSION, M_WAITOK | M_ZERO);
2061 for (i = 0; i < nfsrv_sessionhashsize; i++) {
2062 mtx_init(&nfssessionhash[i].mtx, "nfssm", NULL, MTX_DEF);
2063 LIST_INIT(&nfssessionhash[i].list);
2065 LIST_INIT(&nfsrv_dontlisthead);
2066 TAILQ_INIT(&nfsrv_recalllisthead);
2068 /* and the v2 pubfh should be all zeros */
2069 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2073 * Check the v4 root exports.
2074 * Return 0 if ok, 1 otherwise.
2077 nfsd_checkrootexp(struct nfsrv_descript *nd)
2080 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2082 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2083 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2085 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2086 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2088 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2089 ND_EXGSS)) == (ND_GSS | ND_EXGSS))
2095 * Parse the first part of an NFSv4 compound to find out what the minor
2099 nfsd_getminorvers(struct nfsrv_descript *nd, u_char *tag, u_char **tagstrp,
2100 int *taglenp, u_int32_t *minversp)
2103 int error = 0, taglen = -1;
2104 u_char *tagstr = NULL;
2106 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2107 taglen = fxdr_unsigned(int, *tl);
2108 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
2112 if (taglen <= NFSV4_SMALLSTR)
2115 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2116 error = nfsrv_mtostr(nd, tagstr, taglen);
2119 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2120 *minversp = fxdr_unsigned(u_int32_t, *tl);
2122 if (*minversp == NFSV41_MINORVERSION)
2123 nd->nd_flag |= ND_NFSV41;
2126 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
2127 free(tagstr, M_TEMP);