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>
38 * These functions support the macros and help fiddle mbuf chains for
39 * the nfs op functions. They do things like create the rpc header and
40 * copy data between mbuf chains and uio lists.
42 #include <fs/nfs/nfsport.h>
44 extern u_int32_t newnfs_true, newnfs_false;
45 extern int nfs_pubfhset;
46 extern int nfsrv_clienthashsize;
47 extern int nfsrv_lockhashsize;
48 extern int nfsrv_sessionhashsize;
49 extern int nfsrv_useacl;
50 extern uid_t nfsrv_defaultuid;
51 extern gid_t nfsrv_defaultgid;
53 NFSD_VNET_DECLARE(struct nfsclienthashhead *, nfsclienthash);
54 NFSD_VNET_DECLARE(struct nfslockhashhead *, nfslockhash);
55 NFSD_VNET_DECLARE(struct nfssessionhash *, nfssessionhash);
56 NFSD_VNET_DECLARE(int, nfs_rootfhset);
57 NFSD_VNET_DECLARE(uid_t, nfsrv_defaultuid);
58 NFSD_VNET_DECLARE(gid_t, nfsrv_defaultgid);
60 NFSD_VNET_DEFINE(struct nfsdontlisthead, nfsrv_dontlisthead);
63 char nfs_v2pubfh[NFSX_V2FH];
64 struct nfsdontlisthead nfsrv_dontlisthead;
65 struct nfslayouthead nfsrv_recalllisthead;
66 static nfstype newnfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK,
67 NFNON, NFCHR, NFNON };
68 extern nfstype nfsv34_type[9];
70 static u_int32_t nfsrv_isannfserr(u_int32_t);
72 SYSCTL_DECL(_vfs_nfsd);
74 static int enable_checkutf8 = 1;
75 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_checkutf8, CTLFLAG_RW,
77 "Enable the NFSv4 check for the UTF8 compliant name required by rfc3530");
79 static int enable_nobodycheck = 1;
80 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nobodycheck, CTLFLAG_RW,
81 &enable_nobodycheck, 0,
82 "Enable the NFSv4 check when setting user nobody as owner");
84 static int enable_nogroupcheck = 1;
85 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nogroupcheck, CTLFLAG_RW,
86 &enable_nogroupcheck, 0,
87 "Enable the NFSv4 check when setting group nogroup as owner");
89 static char nfsrv_hexdigit(char, int *);
92 * Maps errno values to nfs error numbers.
93 * Use NFSERR_IO as the catch all for ones not specifically defined in
94 * RFC 1094. (It now includes the errors added for NFSv3.)
96 static u_char nfsrv_v2errmap[NFSERR_REMOTE] = {
97 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
98 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
99 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
100 NFSERR_IO, NFSERR_EXIST, NFSERR_XDEV, NFSERR_NODEV, NFSERR_NOTDIR,
101 NFSERR_ISDIR, NFSERR_INVAL, NFSERR_IO, NFSERR_IO, NFSERR_IO,
102 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
103 NFSERR_MLINK, 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_IO, NFSERR_IO, NFSERR_IO,
107 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
108 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
109 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
110 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
115 * Maps errno values to nfs error numbers.
116 * Although it is not obvious whether or not NFS clients really care if
117 * a returned error value is in the specified list for the procedure, the
118 * safest thing to do is filter them appropriately. For Version 2, the
119 * X/Open XNFS document is the only specification that defines error values
120 * for each RPC (The RFC simply lists all possible error values for all RPCs),
121 * so I have decided to not do this for Version 2.
122 * The first entry is the default error return and the rest are the valid
123 * errors for that RPC in increasing numeric order.
125 static short nfsv3err_null[] = {
130 static short nfsv3err_getattr[] = {
140 static short nfsv3err_setattr[] = {
157 static short nfsv3err_lookup[] = {
171 static short nfsv3err_access[] = {
181 static short nfsv3err_readlink[] = {
194 static short nfsv3err_read[] = {
207 static short nfsv3err_write[] = {
223 static short nfsv3err_create[] = {
241 static short nfsv3err_mkdir[] = {
259 static short nfsv3err_symlink[] = {
277 static short nfsv3err_mknod[] = {
296 static short nfsv3err_remove[] = {
311 static short nfsv3err_rmdir[] = {
330 static short nfsv3err_rename[] = {
354 static short nfsv3err_link[] = {
375 static short nfsv3err_readdir[] = {
389 static short nfsv3err_readdirplus[] = {
404 static short nfsv3err_fsstat[] = {
414 static short nfsv3err_fsinfo[] = {
423 static short nfsv3err_pathconf[] = {
432 static short nfsv3err_commit[] = {
442 static short *nfsrv_v3errmap[] = {
460 nfsv3err_readdirplus,
468 * And the same for V4.
470 static short nfsv4err_null[] = {
475 static short nfsv4err_access[] = {
492 static short nfsv4err_close[] = {
516 static short nfsv4err_commit[] = {
534 static short nfsv4err_create[] = {
563 static short nfsv4err_delegpurge[] = {
571 NFSERR_STALECLIENTID,
575 static short nfsv4err_delegreturn[] = {
594 static short nfsv4err_getattr[] = {
611 static short nfsv4err_getfh[] = {
623 static short nfsv4err_link[] = {
655 static short nfsv4err_lock[] = {
681 NFSERR_RECLAIMCONFLICT,
685 NFSERR_STALECLIENTID,
690 static short nfsv4err_lockt[] = {
709 NFSERR_STALECLIENTID,
713 static short nfsv4err_locku[] = {
739 static short nfsv4err_lookup[] = {
762 static short nfsv4err_lookupp[] = {
778 static short nfsv4err_nverify[] = {
798 static short nfsv4err_open[] = {
829 NFSERR_RECLAIMCONFLICT,
835 NFSERR_STALECLIENTID,
841 static short nfsv4err_openattr[] = {
862 static short nfsv4err_openconfirm[] = {
883 static short nfsv4err_opendowngrade[] = {
903 static short nfsv4err_putfh[] = {
916 static short nfsv4err_putpubfh[] = {
924 static short nfsv4err_putrootfh[] = {
932 static short nfsv4err_read[] = {
960 static short nfsv4err_readdir[] = {
981 static short nfsv4err_readlink[] = {
999 static short nfsv4err_remove[] = {
1014 NFSERR_NOFILEHANDLE,
1024 static short nfsv4err_rename[] = {
1041 NFSERR_NOFILEHANDLE,
1054 static short nfsv4err_renew[] = {
1057 NFSERR_ADMINREVOKED,
1064 NFSERR_STALECLIENTID,
1068 static short nfsv4err_restorefh[] = {
1081 static short nfsv4err_savefh[] = {
1086 NFSERR_NOFILEHANDLE,
1093 static short nfsv4err_secinfo[] = {
1105 NFSERR_NOFILEHANDLE,
1113 static short nfsv4err_setattr[] = {
1116 NFSERR_ADMINREVOKED,
1134 NFSERR_NOFILEHANDLE,
1143 NFSERR_STALESTATEID,
1147 static short nfsv4err_setclientid[] = {
1158 static short nfsv4err_setclientidconfirm[] = {
1164 NFSERR_STALECLIENTID,
1168 static short nfsv4err_verify[] = {
1179 NFSERR_NOFILEHANDLE,
1187 static short nfsv4err_write[] = {
1190 NFSERR_ADMINREVOKED,
1206 NFSERR_NOFILEHANDLE,
1215 NFSERR_STALESTATEID,
1219 static short nfsv4err_releaselockowner[] = {
1221 NFSERR_ADMINREVOKED,
1228 NFSERR_STALECLIENTID,
1232 static short *nfsrv_v4errmap[] = {
1240 nfsv4err_delegpurge,
1241 nfsv4err_delegreturn,
1253 nfsv4err_openconfirm,
1254 nfsv4err_opendowngrade,
1268 nfsv4err_setclientid,
1269 nfsv4err_setclientidconfirm,
1272 nfsv4err_releaselockowner,
1276 * Trim tlen bytes off the end of the mbuf list and then ensure
1277 * the end of the last mbuf is nul filled to a long boundary,
1278 * as indicated by the value of "nul".
1279 * Return the last mbuf in the updated list and free and mbufs
1280 * that follow it in the original list.
1281 * This is somewhat different than the old nfsrv_adj() with
1282 * support for ext_pgs mbufs. It frees the remaining mbufs
1283 * instead of setting them 0 length, since lists of ext_pgs
1284 * mbufs are all expected to be non-empty.
1287 nfsrv_adj(struct mbuf *mp, int len, int nul)
1289 struct mbuf *m, *m2;
1291 int i, lastlen, pgno, plen, tlen, trim;
1296 * Find the last mbuf after adjustment and
1297 * how much it needs to be adjusted by.
1303 if (m->m_next == NULL)
1307 /* m is now the last mbuf and tlen the total length. */
1309 if (len >= m->m_len) {
1310 /* Need to trim away the last mbuf(s). */
1321 lastlen = m->m_len - len;
1324 * m is now the last mbuf after trimming and its length needs to
1326 * Adjust the last mbuf and set cp to point to where nuls must be
1329 if ((m->m_flags & M_EXTPG) != 0) {
1330 pgno = m->m_epg_npgs - 1;
1331 off = (pgno == 0) ? m->m_epg_1st_off : 0;
1332 plen = m_epg_pagelen(m, pgno, off);
1333 if (m->m_len > lastlen) {
1334 /* Trim this mbuf. */
1335 trim = m->m_len - lastlen;
1336 while (trim >= plen) {
1338 ("nfsrv_adj: freeing page 0"));
1340 pg = PHYS_TO_VM_PAGE(m->m_epg_pa[pgno]);
1341 vm_page_unwire_noq(pg);
1346 off = (pgno == 0) ? m->m_epg_1st_off : 0;
1347 plen = m_epg_pagelen(m, pgno, off);
1350 m->m_epg_last_len = plen;
1353 cp = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
1354 cp += off + plen - nul;
1357 cp = mtod(m, char *) + m->m_len - nul;
1360 /* Write the nul bytes. */
1361 for (i = 0; i < nul; i++)
1364 /* Free up any mbufs past "m". */
1373 * Make these functions instead of macros, so that the kernel text size
1374 * doesn't get too big...
1377 nfsrv_wcc(struct nfsrv_descript *nd, int before_ret,
1378 struct nfsvattr *before_nvap, int after_ret, struct nfsvattr *after_nvap)
1383 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1386 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1387 *tl++ = newnfs_true;
1388 txdr_hyper(before_nvap->na_size, tl);
1390 txdr_nfsv3time(&(before_nvap->na_mtime), tl);
1392 txdr_nfsv3time(&(before_nvap->na_ctime), tl);
1394 nfsrv_postopattr(nd, after_ret, after_nvap);
1398 nfsrv_postopattr(struct nfsrv_descript *nd, int after_ret,
1399 struct nfsvattr *after_nvap)
1403 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1408 nfsrv_fillattr(nd, after_nvap);
1413 * Fill in file attributes for V2 and 3. For V4, call a separate
1414 * routine that sifts through all the attribute bits.
1417 nfsrv_fillattr(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1419 struct nfs_fattr *fp;
1423 * Build space for the attribute structure.
1425 if (nd->nd_flag & ND_NFSV3)
1426 fattr_size = NFSX_V3FATTR;
1428 fattr_size = NFSX_V2FATTR;
1429 NFSM_BUILD(fp, struct nfs_fattr *, fattr_size);
1432 * Now just fill it all in.
1434 fp->fa_nlink = txdr_unsigned(nvap->na_nlink);
1435 fp->fa_uid = txdr_unsigned(nvap->na_uid);
1436 fp->fa_gid = txdr_unsigned(nvap->na_gid);
1437 if (nd->nd_flag & ND_NFSV3) {
1438 fp->fa_type = vtonfsv34_type(nvap->na_type);
1439 fp->fa_mode = vtonfsv34_mode(nvap->na_mode);
1440 txdr_hyper(nvap->na_size, (uint32_t*)&fp->fa3_size);
1441 txdr_hyper(nvap->na_bytes, (uint32_t*)&fp->fa3_used);
1442 fp->fa3_rdev.specdata1 = txdr_unsigned(NFSMAJOR(nvap->na_rdev));
1443 fp->fa3_rdev.specdata2 = txdr_unsigned(NFSMINOR(nvap->na_rdev));
1444 fp->fa3_fsid.nfsuquad[0] = 0;
1445 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(nvap->na_fsid);
1446 txdr_hyper(nvap->na_fileid, (uint32_t*)&fp->fa3_fileid);
1447 txdr_nfsv3time(&nvap->na_atime, &fp->fa3_atime);
1448 txdr_nfsv3time(&nvap->na_mtime, &fp->fa3_mtime);
1449 txdr_nfsv3time(&nvap->na_ctime, &fp->fa3_ctime);
1451 fp->fa_type = vtonfsv2_type(nvap->na_type);
1452 fp->fa_mode = vtonfsv2_mode(nvap->na_type, nvap->na_mode);
1453 fp->fa2_size = txdr_unsigned(nvap->na_size);
1454 fp->fa2_blocksize = txdr_unsigned(nvap->na_blocksize);
1455 if (nvap->na_type == VFIFO)
1456 fp->fa2_rdev = 0xffffffff;
1458 fp->fa2_rdev = txdr_unsigned(nvap->na_rdev);
1459 fp->fa2_blocks = txdr_unsigned(nvap->na_bytes / NFS_FABLKSIZE);
1460 fp->fa2_fsid = txdr_unsigned(nvap->na_fsid);
1461 fp->fa2_fileid = txdr_unsigned(nvap->na_fileid);
1462 txdr_nfsv2time(&nvap->na_atime, &fp->fa2_atime);
1463 txdr_nfsv2time(&nvap->na_mtime, &fp->fa2_mtime);
1464 txdr_nfsv2time(&nvap->na_ctime, &fp->fa2_ctime);
1469 * This function gets a file handle out of an mbuf list.
1470 * It returns 0 for success, EBADRPC otherwise.
1471 * If sets the third flagp argument to 1 if the file handle is
1472 * the public file handle.
1473 * For NFSv4, if the length is incorrect, set nd_repstat == NFSERR_BADHANDLE
1476 nfsrv_mtofh(struct nfsrv_descript *nd, struct nfsrvfh *fhp)
1479 int error = 0, len, copylen;
1481 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1482 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1483 len = fxdr_unsigned(int, *tl);
1484 if (len == 0 && nfs_pubfhset && (nd->nd_flag & ND_NFSV3) &&
1485 nd->nd_procnum == NFSPROC_LOOKUP) {
1486 nd->nd_flag |= ND_PUBLOOKUP;
1491 /* If len == NFSX_V4PNFSFH the RPC is a pNFS DS one. */
1492 if (len == NFSX_V4PNFSFH && (nd->nd_flag & ND_NFSV41) != 0) {
1493 copylen = NFSX_MYFH;
1494 len = NFSM_RNDUP(len);
1495 nd->nd_flag |= ND_DSSERVER;
1496 } else if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1497 if (nd->nd_flag & ND_NFSV4) {
1498 if (len > 0 && len <= NFSX_V4FHMAX) {
1499 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1502 nd->nd_repstat = NFSERR_BADHANDLE;
1515 * For NFSv2, the file handle is always 32 bytes on the
1516 * wire, but this server only cares about the first
1517 * NFSRV_MAXFH bytes.
1520 copylen = NFSRV_MAXFH;
1522 NFSM_DISSECT(tl, u_int32_t *, len);
1523 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1524 nd->nd_procnum == NFSPROC_LOOKUP &&
1525 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1526 nd->nd_flag |= ND_PUBLOOKUP;
1529 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1530 fhp->nfsrvfh_len = copylen;
1532 NFSEXITCODE2(error, nd);
1537 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1538 * numbers not specified for the associated procedure.
1539 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1540 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1541 * RPC procedure is not involved.
1542 * Returns the error number in XDR.
1545 nfsd_errmap(struct nfsrv_descript *nd)
1547 short *defaulterrp, *errp;
1549 if (!nd->nd_repstat)
1551 if ((nd->nd_repstat & NFSERR_AUTHERR) != 0)
1552 return (txdr_unsigned(NFSERR_ACCES));
1553 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1554 if (nd->nd_procnum == NFSPROC_NOOP)
1555 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1556 if (nd->nd_flag & ND_NFSV3)
1557 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1558 else if (nd->nd_repstat == EBADRPC)
1559 return (txdr_unsigned(NFSERR_BADXDR));
1560 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1561 nd->nd_repstat == NFSERR_OPILLEGAL)
1562 return (txdr_unsigned(nd->nd_repstat));
1563 else if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
1564 return (txdr_unsigned(NFSERR_IO));
1565 else if ((nd->nd_flag & ND_NFSV41) != 0) {
1566 if (nd->nd_repstat == EOPNOTSUPP)
1567 nd->nd_repstat = NFSERR_NOTSUPP;
1568 nd->nd_repstat = nfsrv_isannfserr(nd->nd_repstat);
1569 return (txdr_unsigned(nd->nd_repstat));
1571 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1573 if (*errp == nd->nd_repstat)
1574 return (txdr_unsigned(nd->nd_repstat));
1575 return (txdr_unsigned(*defaulterrp));
1577 if (nd->nd_repstat <= NFSERR_REMOTE)
1578 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1579 return (txdr_unsigned(NFSERR_IO));
1583 * Check to see if the error is a valid NFS one. If not, replace it with
1587 nfsrv_isannfserr(u_int32_t errval)
1590 if (errval == NFSERR_OK)
1592 if (errval >= NFSERR_BADHANDLE && errval <= NFSERR_MAXERRVAL)
1594 if (errval > 0 && errval <= NFSERR_REMOTE)
1595 return (nfsrv_v2errmap[errval - 1]);
1600 * Check to see if setting a uid/gid is permitted when creating a new
1601 * file object. (Called when uid and/or gid is specified in the
1602 * settable attributes for V4.
1605 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1610 * If not setting either uid nor gid, it's OK.
1612 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1614 if ((NFSVNO_ISSETUID(nvap) &&
1615 nvap->na_uid == NFSD_VNET(nfsrv_defaultuid) &&
1616 enable_nobodycheck == 1) ||
1617 (NFSVNO_ISSETGID(nvap) &&
1618 nvap->na_gid == NFSD_VNET(nfsrv_defaultgid) &&
1619 enable_nogroupcheck == 1)) {
1620 error = NFSERR_BADOWNER;
1623 if (nd->nd_cred->cr_uid == 0)
1625 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1626 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1627 !groupmember(nvap->na_gid, nd->nd_cred)))
1628 error = NFSERR_PERM;
1631 NFSEXITCODE2(error, nd);
1636 * and this routine fixes up the settable attributes for V4 if allowed
1637 * by nfsrv_checkuidgid().
1640 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1641 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1642 struct nfsexstuff *exp)
1645 struct nfsvattr nva;
1648 nfsattrbit_t nattrbits;
1651 * Maybe this should be done for V2 and 3 but it never has been
1652 * and nobody seems to be upset, so I think it's best not to change
1653 * the V2 and 3 semantics.
1655 if ((nd->nd_flag & ND_NFSV4) == 0)
1657 NFSVNO_ATTRINIT(&nva);
1658 NFSZERO_ATTRBIT(&nattrbits);
1659 tuid = nd->nd_cred->cr_uid;
1660 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1661 NFSVNO_ISSETUID(nvap) &&
1662 nvap->na_uid != nd->nd_cred->cr_uid) {
1663 if (nd->nd_cred->cr_uid == 0) {
1664 nva.na_uid = nvap->na_uid;
1666 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1668 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1671 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1672 NFSVNO_ISSETATIME(nvap)) {
1673 nva.na_atime = nvap->na_atime;
1675 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1677 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1678 NFSVNO_ISSETMTIME(nvap)) {
1679 nva.na_mtime = nvap->na_mtime;
1681 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1683 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1684 NFSVNO_ISSETGID(nvap)) {
1685 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1686 groupmember(nvap->na_gid, nd->nd_cred)) {
1687 nd->nd_cred->cr_uid = 0;
1688 nva.na_gid = nvap->na_gid;
1690 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1692 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1696 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1698 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1701 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1702 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1703 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1705 #ifdef NFS4_ACL_EXTATTR_NAME
1706 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1707 nfsrv_useacl != 0 && aclp != NULL) {
1708 if (aclp->acl_cnt > 0) {
1709 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1711 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1716 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1717 nd->nd_cred->cr_uid = tuid;
1720 NFSEXITCODE2(0, nd);
1724 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1725 * char isn't a hex digit.
1728 nfsrv_hexdigit(char c, int *err)
1732 if (c >= '0' && c <= '9')
1734 if (c >= 'a' && c <= 'f')
1735 return (c - 'a' + ((char)10));
1736 if (c >= 'A' && c <= 'F')
1737 return (c - 'A' + ((char)10));
1740 return (1); /* BOGUS */
1744 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1748 nfsrv_errmoved(int op)
1752 errp = nfsrv_v4errmap[op];
1753 while (*errp != 0) {
1754 if (*errp == NFSERR_MOVED)
1762 * Fill in attributes for a Referral.
1763 * (Return the number of bytes of XDR created.)
1766 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1767 struct nfsreferral *refp, int getattr, int *reterrp)
1769 u_int32_t *tl, *retnump;
1771 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1772 int fslocationsbit = 0;
1773 nfsattrbit_t tmpbits, refbits;
1775 NFSREFERRAL_ATTRBIT(&refbits);
1777 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1778 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1780 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1784 * Check for the case where unsupported referral attributes are
1787 NFSSET_ATTRBIT(&tmpbits, retbitp);
1788 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1789 if (NFSNONZERO_ATTRBIT(&tmpbits))
1792 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1793 *reterrp = NFSERR_MOVED;
1798 * Now we can fill in the attributes.
1800 NFSSET_ATTRBIT(&tmpbits, retbitp);
1801 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1804 * Put out the attribute bitmap for the ones being filled in
1805 * and get the field for the number of attributes returned.
1807 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1808 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1809 prefixnum += NFSX_UNSIGNED;
1812 * Now, loop around filling in the attributes for each bit set.
1814 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1815 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1817 case NFSATTRBIT_TYPE:
1818 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1819 *tl = txdr_unsigned(NFDIR);
1820 retnum += NFSX_UNSIGNED;
1822 case NFSATTRBIT_FSID:
1823 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1825 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1827 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1828 retnum += NFSX_V4FSID;
1830 case NFSATTRBIT_RDATTRERROR:
1831 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1833 *tl = txdr_unsigned(NFSERR_MOVED);
1836 retnum += NFSX_UNSIGNED;
1838 case NFSATTRBIT_FSLOCATIONS:
1839 retnum += nfsm_strtom(nd, "/", 1);
1840 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1841 *tl = txdr_unsigned(refp->nfr_srvcnt);
1842 retnum += NFSX_UNSIGNED;
1843 cp = refp->nfr_srvlist;
1844 for (i = 0; i < refp->nfr_srvcnt; i++) {
1845 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1846 *tl = txdr_unsigned(1);
1847 retnum += NFSX_UNSIGNED;
1848 cp2 = STRCHR(cp, ':');
1853 retnum += nfsm_strtom(nd, cp, len);
1856 cp2 = STRCHR(cp, ',');
1861 retnum += nfsm_strtom(nd, cp, len);
1866 case NFSATTRBIT_MOUNTEDONFILEID:
1867 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1868 txdr_hyper(refp->nfr_dfileno, tl);
1869 retnum += NFSX_HYPER;
1872 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1876 *retnump = txdr_unsigned(retnum);
1877 return (retnum + prefixnum);
1881 * Parse a file name out of a request.
1884 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1885 NFSPATHLEN_T *outlenp)
1887 char *fromcp, *tocp, val = '\0';
1890 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1899 * For V4, check for lookup parent.
1900 * Otherwise, get the component name.
1902 if ((nd->nd_flag & ND_NFSV4) && (nd->nd_procnum == NFSV4OP_LOOKUPP ||
1903 nd->nd_procnum == NFSV4OP_SECINFONONAME)) {
1905 hash += ((u_char)'.');
1907 hash += ((u_char)'.');
1911 * First, get the name length.
1913 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1914 len = fxdr_unsigned(int, *tl);
1915 if (len > NFS_MAXNAMLEN) {
1916 nd->nd_repstat = NFSERR_NAMETOL;
1919 } else if (len <= 0) {
1920 nd->nd_repstat = NFSERR_INVAL;
1926 * Now, copy the component name into the buffer.
1928 fromcp = nd->nd_dpos;
1930 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1931 for (i = 0; i < len; i++) {
1938 fromcp = mtod(md, caddr_t);
1941 if (*fromcp == '\0') {
1942 nd->nd_repstat = EACCES;
1947 * For lookups on the public filehandle, do some special
1948 * processing on the name. (The public file handle is the
1949 * root of the public file system for this server.)
1951 if (nd->nd_flag & ND_PUBLOOKUP) {
1953 * If the first char is ASCII, it is a canonical
1954 * path, otherwise it is a native path. (RFC2054
1955 * doesn't actually state what it is if the first
1956 * char isn't ASCII or 0x80, so I assume native.)
1957 * pubtype == 1 -> native path
1958 * pubtype == 2 -> canonical path
1961 if (*fromcp & 0x80) {
1963 * Since RFC2054 doesn't indicate
1964 * that a native path of just 0x80
1965 * isn't allowed, I'll replace the
1966 * 0x80 with '/' instead of just
1976 * '/' only allowed in a native path
1978 if (*fromcp == '/' && pubtype != 1) {
1979 nd->nd_repstat = EACCES;
1985 * For the special case of 2 hex digits after a
1986 * '%' in an absolute path, calculate the value.
1987 * percent == 1 -> indicates "get first hex digit"
1988 * percent == 2 -> indicates "get second hex digit"
1991 digit = nfsrv_hexdigit(*fromcp, &error);
1993 nd->nd_repstat = EACCES;
2004 hash += ((u_char)val);
2008 if (*fromcp == '%' && pubtype == 2) {
2010 * Must be followed by 2 hex digits
2012 if ((len - i) < 3) {
2013 nd->nd_repstat = EACCES;
2020 hash += ((u_char)*fromcp);
2026 * Normal, non lookup on public, name.
2028 if (*fromcp == '/') {
2029 if (nd->nd_flag & ND_NFSV4)
2030 nd->nd_repstat = NFSERR_BADNAME;
2032 nd->nd_repstat = EACCES;
2036 hash += ((u_char)*fromcp);
2044 nd->nd_dpos = fromcp;
2045 i = NFSM_RNDUP(len) - len;
2050 error = nfsm_advance(nd, i, rem);
2057 * For v4, don't allow lookups of '.' or '..' and
2058 * also check for non-utf8 strings.
2060 if (nd->nd_flag & ND_NFSV4) {
2061 if ((outlen == 1 && bufp[0] == '.') ||
2062 (outlen == 2 && bufp[0] == '.' &&
2064 nd->nd_repstat = NFSERR_BADNAME;
2068 if (enable_checkutf8 == 1 &&
2069 nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
2070 nd->nd_repstat = NFSERR_INVAL;
2077 *outlenp = (size_t)outlen + 1;
2081 NFSEXITCODE2(error, nd);
2092 * Initialize client queues. Don't free/reinitialize
2093 * them when nfsds are restarted.
2095 NFSD_VNET(nfsclienthash) = malloc(sizeof(struct nfsclienthashhead) *
2096 nfsrv_clienthashsize, M_NFSDCLIENT, M_WAITOK | M_ZERO);
2097 for (i = 0; i < nfsrv_clienthashsize; i++)
2098 LIST_INIT(&NFSD_VNET(nfsclienthash)[i]);
2099 NFSD_VNET(nfslockhash) = malloc(sizeof(struct nfslockhashhead) *
2100 nfsrv_lockhashsize, M_NFSDLOCKFILE, M_WAITOK | M_ZERO);
2101 for (i = 0; i < nfsrv_lockhashsize; i++)
2102 LIST_INIT(&NFSD_VNET(nfslockhash)[i]);
2103 NFSD_VNET(nfssessionhash) = malloc(sizeof(struct nfssessionhash) *
2104 nfsrv_sessionhashsize, M_NFSDSESSION, M_WAITOK | M_ZERO);
2105 for (i = 0; i < nfsrv_sessionhashsize; i++) {
2106 mtx_init(&NFSD_VNET(nfssessionhash)[i].mtx, "nfssm", NULL,
2108 LIST_INIT(&NFSD_VNET(nfssessionhash)[i].list);
2110 LIST_INIT(&nfsrv_dontlisthead);
2111 TAILQ_INIT(&nfsrv_recalllisthead);
2113 /* and the v2 pubfh should be all zeros */
2114 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2118 * Check the v4 root exports.
2119 * Return 0 if ok, 1 otherwise.
2122 nfsd_checkrootexp(struct nfsrv_descript *nd)
2125 if (NFSD_VNET(nfs_rootfhset) == 0)
2126 return (NFSERR_AUTHERR | AUTH_FAILED);
2128 * For NFSv4.1/4.2, if the client specifies SP4_NONE, then these
2129 * operations are allowed regardless of the value of the "sec=XXX"
2130 * field in the V4: exports line.
2131 * As such, these Kerberos checks only apply to NFSv4.0 mounts.
2133 if ((nd->nd_flag & ND_NFSV41) != 0)
2135 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2137 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2138 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2140 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2141 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2143 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2144 ND_EXGSS)) == (ND_GSS | ND_EXGSS))
2146 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2148 if ((nd->nd_flag & ND_EXTLS) == 0)
2150 if ((nd->nd_flag & (ND_TLSCERTUSER | ND_EXTLSCERTUSER)) ==
2151 (ND_TLSCERTUSER | ND_EXTLSCERTUSER))
2153 if ((nd->nd_flag & (ND_TLSCERT | ND_EXTLSCERT | ND_EXTLSCERTUSER)) ==
2154 (ND_TLSCERT | ND_EXTLSCERT))
2156 if ((nd->nd_flag & (ND_TLS | ND_EXTLSCERTUSER | ND_EXTLSCERT)) ==
2160 /* There is currently no auth_stat for this. */
2161 if ((nd->nd_flag & ND_TLS) == 0)
2162 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS);
2163 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST);
2165 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2169 * Parse the first part of an NFSv4 compound to find out what the minor
2173 nfsd_getminorvers(struct nfsrv_descript *nd, u_char *tag, u_char **tagstrp,
2174 int *taglenp, u_int32_t *minversp)
2177 int error = 0, taglen = -1;
2178 u_char *tagstr = NULL;
2180 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2181 taglen = fxdr_unsigned(int, *tl);
2182 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
2186 if (taglen <= NFSV4_SMALLSTR)
2189 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2190 error = nfsrv_mtostr(nd, tagstr, taglen);
2193 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2194 *minversp = fxdr_unsigned(u_int32_t, *tl);
2196 if (*minversp == NFSV41_MINORVERSION)
2197 nd->nd_flag |= ND_NFSV41;
2198 else if (*minversp == NFSV42_MINORVERSION)
2199 nd->nd_flag |= (ND_NFSV41 | ND_NFSV42);
2202 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
2203 free(tagstr, M_TEMP);