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$");
40 * These functions support the macros and help fiddle mbuf chains for
41 * the nfs op functions. They do things like create the rpc header and
42 * copy data between mbuf chains and uio lists.
44 #include <fs/nfs/nfsport.h>
46 extern u_int32_t newnfs_true, newnfs_false;
47 extern int nfs_pubfhset;
48 extern struct nfsclienthashhead *nfsclienthash;
49 extern int nfsrv_clienthashsize;
50 extern struct nfslockhashhead *nfslockhash;
51 extern int nfsrv_lockhashsize;
52 extern struct nfssessionhash *nfssessionhash;
53 extern int nfsrv_sessionhashsize;
54 extern int nfsrv_useacl;
55 extern uid_t nfsrv_defaultuid;
56 extern gid_t nfsrv_defaultgid;
58 char nfs_v2pubfh[NFSX_V2FH];
59 struct nfsdontlisthead nfsrv_dontlisthead;
60 struct nfslayouthead nfsrv_recalllisthead;
61 static nfstype newnfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK,
62 NFNON, NFCHR, NFNON };
63 extern nfstype nfsv34_type[9];
65 static u_int32_t nfsrv_isannfserr(u_int32_t);
67 SYSCTL_DECL(_vfs_nfsd);
69 static int enable_checkutf8 = 1;
70 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_checkutf8, CTLFLAG_RW,
72 "Enable the NFSv4 check for the UTF8 compliant name required by rfc3530");
74 static int enable_nobodycheck = 1;
75 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nobodycheck, CTLFLAG_RW,
76 &enable_nobodycheck, 0,
77 "Enable the NFSv4 check when setting user nobody as owner");
79 static int enable_nogroupcheck = 1;
80 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nogroupcheck, CTLFLAG_RW,
81 &enable_nogroupcheck, 0,
82 "Enable the NFSv4 check when setting group nogroup as owner");
84 static char nfsrv_hexdigit(char, int *);
87 * Maps errno values to nfs error numbers.
88 * Use NFSERR_IO as the catch all for ones not specifically defined in
89 * RFC 1094. (It now includes the errors added for NFSv3.)
91 static u_char nfsrv_v2errmap[NFSERR_REMOTE] = {
92 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
93 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
94 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
95 NFSERR_IO, NFSERR_EXIST, NFSERR_XDEV, NFSERR_NODEV, NFSERR_NOTDIR,
96 NFSERR_ISDIR, NFSERR_INVAL, NFSERR_IO, NFSERR_IO, NFSERR_IO,
97 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
98 NFSERR_MLINK, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
99 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
100 NFSERR_IO, 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_NAMETOL, NFSERR_IO, NFSERR_IO,
105 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
110 * Maps errno values to nfs error numbers.
111 * Although it is not obvious whether or not NFS clients really care if
112 * a returned error value is in the specified list for the procedure, the
113 * safest thing to do is filter them appropriately. For Version 2, the
114 * X/Open XNFS document is the only specification that defines error values
115 * for each RPC (The RFC simply lists all possible error values for all RPCs),
116 * so I have decided to not do this for Version 2.
117 * The first entry is the default error return and the rest are the valid
118 * errors for that RPC in increasing numeric order.
120 static short nfsv3err_null[] = {
125 static short nfsv3err_getattr[] = {
135 static short nfsv3err_setattr[] = {
152 static short nfsv3err_lookup[] = {
166 static short nfsv3err_access[] = {
176 static short nfsv3err_readlink[] = {
189 static short nfsv3err_read[] = {
202 static short nfsv3err_write[] = {
218 static short nfsv3err_create[] = {
236 static short nfsv3err_mkdir[] = {
254 static short nfsv3err_symlink[] = {
272 static short nfsv3err_mknod[] = {
291 static short nfsv3err_remove[] = {
306 static short nfsv3err_rmdir[] = {
325 static short nfsv3err_rename[] = {
349 static short nfsv3err_link[] = {
370 static short nfsv3err_readdir[] = {
384 static short nfsv3err_readdirplus[] = {
399 static short nfsv3err_fsstat[] = {
409 static short nfsv3err_fsinfo[] = {
418 static short nfsv3err_pathconf[] = {
427 static short nfsv3err_commit[] = {
437 static short *nfsrv_v3errmap[] = {
455 nfsv3err_readdirplus,
463 * And the same for V4.
465 static short nfsv4err_null[] = {
470 static short nfsv4err_access[] = {
487 static short nfsv4err_close[] = {
511 static short nfsv4err_commit[] = {
529 static short nfsv4err_create[] = {
558 static short nfsv4err_delegpurge[] = {
566 NFSERR_STALECLIENTID,
570 static short nfsv4err_delegreturn[] = {
589 static short nfsv4err_getattr[] = {
606 static short nfsv4err_getfh[] = {
618 static short nfsv4err_link[] = {
650 static short nfsv4err_lock[] = {
676 NFSERR_RECLAIMCONFLICT,
680 NFSERR_STALECLIENTID,
685 static short nfsv4err_lockt[] = {
704 NFSERR_STALECLIENTID,
708 static short nfsv4err_locku[] = {
734 static short nfsv4err_lookup[] = {
757 static short nfsv4err_lookupp[] = {
773 static short nfsv4err_nverify[] = {
793 static short nfsv4err_open[] = {
824 NFSERR_RECLAIMCONFLICT,
830 NFSERR_STALECLIENTID,
836 static short nfsv4err_openattr[] = {
857 static short nfsv4err_openconfirm[] = {
878 static short nfsv4err_opendowngrade[] = {
898 static short nfsv4err_putfh[] = {
911 static short nfsv4err_putpubfh[] = {
919 static short nfsv4err_putrootfh[] = {
927 static short nfsv4err_read[] = {
955 static short nfsv4err_readdir[] = {
976 static short nfsv4err_readlink[] = {
994 static short nfsv4err_remove[] = {
1009 NFSERR_NOFILEHANDLE,
1019 static short nfsv4err_rename[] = {
1036 NFSERR_NOFILEHANDLE,
1049 static short nfsv4err_renew[] = {
1052 NFSERR_ADMINREVOKED,
1059 NFSERR_STALECLIENTID,
1063 static short nfsv4err_restorefh[] = {
1076 static short nfsv4err_savefh[] = {
1081 NFSERR_NOFILEHANDLE,
1088 static short nfsv4err_secinfo[] = {
1100 NFSERR_NOFILEHANDLE,
1108 static short nfsv4err_setattr[] = {
1111 NFSERR_ADMINREVOKED,
1129 NFSERR_NOFILEHANDLE,
1138 NFSERR_STALESTATEID,
1142 static short nfsv4err_setclientid[] = {
1153 static short nfsv4err_setclientidconfirm[] = {
1159 NFSERR_STALECLIENTID,
1163 static short nfsv4err_verify[] = {
1174 NFSERR_NOFILEHANDLE,
1182 static short nfsv4err_write[] = {
1185 NFSERR_ADMINREVOKED,
1201 NFSERR_NOFILEHANDLE,
1210 NFSERR_STALESTATEID,
1214 static short nfsv4err_releaselockowner[] = {
1216 NFSERR_ADMINREVOKED,
1223 NFSERR_STALECLIENTID,
1227 static short *nfsrv_v4errmap[] = {
1235 nfsv4err_delegpurge,
1236 nfsv4err_delegreturn,
1248 nfsv4err_openconfirm,
1249 nfsv4err_opendowngrade,
1263 nfsv4err_setclientid,
1264 nfsv4err_setclientidconfirm,
1267 nfsv4err_releaselockowner,
1271 * Trim tlen bytes off the end of the mbuf list and then ensure
1272 * the end of the last mbuf is nul filled to a long boundary,
1273 * as indicated by the value of "nul".
1274 * Return the last mbuf in the updated list and free and mbufs
1275 * that follow it in the original list.
1276 * This is somewhat different than the old nfsrv_adj() with
1277 * support for ext_pgs mbufs. It frees the remaining mbufs
1278 * instead of setting them 0 length, since lists of ext_pgs
1279 * mbufs are all expected to be non-empty.
1282 nfsrv_adj(struct mbuf *mp, int len, int nul)
1284 struct mbuf *m, *m2;
1286 int i, lastlen, pgno, plen, tlen, trim;
1291 * Find the last mbuf after adjustment and
1292 * how much it needs to be adjusted by.
1298 if (m->m_next == NULL)
1302 /* m is now the last mbuf and tlen the total length. */
1304 if (len >= m->m_len) {
1305 /* Need to trim away the last mbuf(s). */
1316 lastlen = m->m_len - len;
1319 * m is now the last mbuf after trimming and its length needs to
1321 * Adjust the last mbuf and set cp to point to where nuls must be
1324 if ((m->m_flags & M_EXTPG) != 0) {
1325 pgno = m->m_epg_npgs - 1;
1326 off = (pgno == 0) ? m->m_epg_1st_off : 0;
1327 plen = m_epg_pagelen(m, pgno, off);
1328 if (m->m_len > lastlen) {
1329 /* Trim this mbuf. */
1330 trim = m->m_len - lastlen;
1331 while (trim >= plen) {
1333 ("nfsrv_adj: freeing page 0"));
1335 pg = PHYS_TO_VM_PAGE(m->m_epg_pa[pgno]);
1336 vm_page_unwire_noq(pg);
1341 off = (pgno == 0) ? m->m_epg_1st_off : 0;
1342 plen = m_epg_pagelen(m, pgno, off);
1345 m->m_epg_last_len = plen;
1348 cp = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
1349 cp += off + plen - nul;
1352 cp = mtod(m, char *) + m->m_len - nul;
1355 /* Write the nul bytes. */
1356 for (i = 0; i < nul; i++)
1359 /* Free up any mbufs past "m". */
1368 * Make these functions instead of macros, so that the kernel text size
1369 * doesn't get too big...
1372 nfsrv_wcc(struct nfsrv_descript *nd, int before_ret,
1373 struct nfsvattr *before_nvap, int after_ret, struct nfsvattr *after_nvap)
1378 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1381 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1382 *tl++ = newnfs_true;
1383 txdr_hyper(before_nvap->na_size, tl);
1385 txdr_nfsv3time(&(before_nvap->na_mtime), tl);
1387 txdr_nfsv3time(&(before_nvap->na_ctime), tl);
1389 nfsrv_postopattr(nd, after_ret, after_nvap);
1393 nfsrv_postopattr(struct nfsrv_descript *nd, int after_ret,
1394 struct nfsvattr *after_nvap)
1398 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1403 nfsrv_fillattr(nd, after_nvap);
1408 * Fill in file attributes for V2 and 3. For V4, call a separate
1409 * routine that sifts through all the attribute bits.
1412 nfsrv_fillattr(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1414 struct nfs_fattr *fp;
1418 * Build space for the attribute structure.
1420 if (nd->nd_flag & ND_NFSV3)
1421 fattr_size = NFSX_V3FATTR;
1423 fattr_size = NFSX_V2FATTR;
1424 NFSM_BUILD(fp, struct nfs_fattr *, fattr_size);
1427 * Now just fill it all in.
1429 fp->fa_nlink = txdr_unsigned(nvap->na_nlink);
1430 fp->fa_uid = txdr_unsigned(nvap->na_uid);
1431 fp->fa_gid = txdr_unsigned(nvap->na_gid);
1432 if (nd->nd_flag & ND_NFSV3) {
1433 fp->fa_type = vtonfsv34_type(nvap->na_type);
1434 fp->fa_mode = vtonfsv34_mode(nvap->na_mode);
1435 txdr_hyper(nvap->na_size, &fp->fa3_size);
1436 txdr_hyper(nvap->na_bytes, &fp->fa3_used);
1437 fp->fa3_rdev.specdata1 = txdr_unsigned(NFSMAJOR(nvap->na_rdev));
1438 fp->fa3_rdev.specdata2 = txdr_unsigned(NFSMINOR(nvap->na_rdev));
1439 fp->fa3_fsid.nfsuquad[0] = 0;
1440 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(nvap->na_fsid);
1441 txdr_hyper(nvap->na_fileid, &fp->fa3_fileid);
1442 txdr_nfsv3time(&nvap->na_atime, &fp->fa3_atime);
1443 txdr_nfsv3time(&nvap->na_mtime, &fp->fa3_mtime);
1444 txdr_nfsv3time(&nvap->na_ctime, &fp->fa3_ctime);
1446 fp->fa_type = vtonfsv2_type(nvap->na_type);
1447 fp->fa_mode = vtonfsv2_mode(nvap->na_type, nvap->na_mode);
1448 fp->fa2_size = txdr_unsigned(nvap->na_size);
1449 fp->fa2_blocksize = txdr_unsigned(nvap->na_blocksize);
1450 if (nvap->na_type == VFIFO)
1451 fp->fa2_rdev = 0xffffffff;
1453 fp->fa2_rdev = txdr_unsigned(nvap->na_rdev);
1454 fp->fa2_blocks = txdr_unsigned(nvap->na_bytes / NFS_FABLKSIZE);
1455 fp->fa2_fsid = txdr_unsigned(nvap->na_fsid);
1456 fp->fa2_fileid = txdr_unsigned(nvap->na_fileid);
1457 txdr_nfsv2time(&nvap->na_atime, &fp->fa2_atime);
1458 txdr_nfsv2time(&nvap->na_mtime, &fp->fa2_mtime);
1459 txdr_nfsv2time(&nvap->na_ctime, &fp->fa2_ctime);
1464 * This function gets a file handle out of an mbuf list.
1465 * It returns 0 for success, EBADRPC otherwise.
1466 * If sets the third flagp argument to 1 if the file handle is
1467 * the public file handle.
1468 * For NFSv4, if the length is incorrect, set nd_repstat == NFSERR_BADHANDLE
1471 nfsrv_mtofh(struct nfsrv_descript *nd, struct nfsrvfh *fhp)
1474 int error = 0, len, copylen;
1476 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1477 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1478 len = fxdr_unsigned(int, *tl);
1479 if (len == 0 && nfs_pubfhset && (nd->nd_flag & ND_NFSV3) &&
1480 nd->nd_procnum == NFSPROC_LOOKUP) {
1481 nd->nd_flag |= ND_PUBLOOKUP;
1486 /* If len == NFSX_V4PNFSFH the RPC is a pNFS DS one. */
1487 if (len == NFSX_V4PNFSFH && (nd->nd_flag & ND_NFSV41) != 0) {
1488 copylen = NFSX_MYFH;
1489 len = NFSM_RNDUP(len);
1490 nd->nd_flag |= ND_DSSERVER;
1491 } else if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1492 if (nd->nd_flag & ND_NFSV4) {
1493 if (len > 0 && len <= NFSX_V4FHMAX) {
1494 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1497 nd->nd_repstat = NFSERR_BADHANDLE;
1510 * For NFSv2, the file handle is always 32 bytes on the
1511 * wire, but this server only cares about the first
1512 * NFSRV_MAXFH bytes.
1515 copylen = NFSRV_MAXFH;
1517 NFSM_DISSECT(tl, u_int32_t *, len);
1518 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1519 nd->nd_procnum == NFSPROC_LOOKUP &&
1520 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1521 nd->nd_flag |= ND_PUBLOOKUP;
1524 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1525 fhp->nfsrvfh_len = copylen;
1527 NFSEXITCODE2(error, nd);
1532 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1533 * numbers not specified for the associated procedure.
1534 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1535 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1536 * RPC procedure is not involved.
1537 * Returns the error number in XDR.
1540 nfsd_errmap(struct nfsrv_descript *nd)
1542 short *defaulterrp, *errp;
1544 if (!nd->nd_repstat)
1546 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1547 if (nd->nd_procnum == NFSPROC_NOOP)
1548 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1549 if (nd->nd_flag & ND_NFSV3)
1550 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1551 else if (nd->nd_repstat == EBADRPC)
1552 return (txdr_unsigned(NFSERR_BADXDR));
1553 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1554 nd->nd_repstat == NFSERR_OPILLEGAL)
1555 return (txdr_unsigned(nd->nd_repstat));
1556 else if ((nd->nd_flag & ND_NFSV41) != 0) {
1557 if (nd->nd_repstat == EOPNOTSUPP)
1558 nd->nd_repstat = NFSERR_NOTSUPP;
1559 nd->nd_repstat = nfsrv_isannfserr(nd->nd_repstat);
1560 return (txdr_unsigned(nd->nd_repstat));
1562 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1564 if (*errp == nd->nd_repstat)
1565 return (txdr_unsigned(nd->nd_repstat));
1566 return (txdr_unsigned(*defaulterrp));
1568 if (nd->nd_repstat <= NFSERR_REMOTE)
1569 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1570 return (txdr_unsigned(NFSERR_IO));
1574 * Check to see if the error is a valid NFS one. If not, replace it with
1578 nfsrv_isannfserr(u_int32_t errval)
1581 if (errval == NFSERR_OK)
1583 if (errval >= NFSERR_BADHANDLE && errval <= NFSERR_MAXERRVAL)
1585 if (errval > 0 && errval <= NFSERR_REMOTE)
1586 return (nfsrv_v2errmap[errval - 1]);
1591 * Check to see if setting a uid/gid is permitted when creating a new
1592 * file object. (Called when uid and/or gid is specified in the
1593 * settable attributes for V4.
1596 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1601 * If not setting either uid nor gid, it's OK.
1603 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1605 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid == nfsrv_defaultuid &&
1606 enable_nobodycheck == 1)
1607 || (NFSVNO_ISSETGID(nvap) && nvap->na_gid == nfsrv_defaultgid &&
1608 enable_nogroupcheck == 1)) {
1609 error = NFSERR_BADOWNER;
1612 if (nd->nd_cred->cr_uid == 0)
1614 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1615 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1616 !groupmember(nvap->na_gid, nd->nd_cred)))
1617 error = NFSERR_PERM;
1620 NFSEXITCODE2(error, nd);
1625 * and this routine fixes up the settable attributes for V4 if allowed
1626 * by nfsrv_checkuidgid().
1629 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1630 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1631 struct nfsexstuff *exp)
1634 struct nfsvattr nva;
1637 nfsattrbit_t nattrbits;
1640 * Maybe this should be done for V2 and 3 but it never has been
1641 * and nobody seems to be upset, so I think it's best not to change
1642 * the V2 and 3 semantics.
1644 if ((nd->nd_flag & ND_NFSV4) == 0)
1646 NFSVNO_ATTRINIT(&nva);
1647 NFSZERO_ATTRBIT(&nattrbits);
1648 tuid = nd->nd_cred->cr_uid;
1649 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1650 NFSVNO_ISSETUID(nvap) &&
1651 nvap->na_uid != nd->nd_cred->cr_uid) {
1652 if (nd->nd_cred->cr_uid == 0) {
1653 nva.na_uid = nvap->na_uid;
1655 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1657 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1660 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1661 NFSVNO_ISSETATIME(nvap)) {
1662 nva.na_atime = nvap->na_atime;
1664 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1666 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1667 NFSVNO_ISSETMTIME(nvap)) {
1668 nva.na_mtime = nvap->na_mtime;
1670 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1672 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1673 NFSVNO_ISSETGID(nvap)) {
1674 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1675 groupmember(nvap->na_gid, nd->nd_cred)) {
1676 nd->nd_cred->cr_uid = 0;
1677 nva.na_gid = nvap->na_gid;
1679 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1681 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1685 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1687 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1690 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1691 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1692 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1694 #ifdef NFS4_ACL_EXTATTR_NAME
1695 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1696 nfsrv_useacl != 0 && aclp != NULL) {
1697 if (aclp->acl_cnt > 0) {
1698 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1700 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1705 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1706 nd->nd_cred->cr_uid = tuid;
1709 NFSEXITCODE2(0, nd);
1713 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1714 * char isn't a hex digit.
1717 nfsrv_hexdigit(char c, int *err)
1721 if (c >= '0' && c <= '9')
1723 if (c >= 'a' && c <= 'f')
1724 return (c - 'a' + ((char)10));
1725 if (c >= 'A' && c <= 'F')
1726 return (c - 'A' + ((char)10));
1729 return (1); /* BOGUS */
1733 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1737 nfsrv_errmoved(int op)
1741 errp = nfsrv_v4errmap[op];
1742 while (*errp != 0) {
1743 if (*errp == NFSERR_MOVED)
1751 * Fill in attributes for a Referral.
1752 * (Return the number of bytes of XDR created.)
1755 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1756 struct nfsreferral *refp, int getattr, int *reterrp)
1758 u_int32_t *tl, *retnump;
1760 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1761 int fslocationsbit = 0;
1762 nfsattrbit_t tmpbits, refbits;
1764 NFSREFERRAL_ATTRBIT(&refbits);
1766 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1767 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1769 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1773 * Check for the case where unsupported referral attributes are
1776 NFSSET_ATTRBIT(&tmpbits, retbitp);
1777 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1778 if (NFSNONZERO_ATTRBIT(&tmpbits))
1781 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1782 *reterrp = NFSERR_MOVED;
1787 * Now we can fill in the attributes.
1789 NFSSET_ATTRBIT(&tmpbits, retbitp);
1790 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1793 * Put out the attribute bitmap for the ones being filled in
1794 * and get the field for the number of attributes returned.
1796 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1797 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1798 prefixnum += NFSX_UNSIGNED;
1801 * Now, loop around filling in the attributes for each bit set.
1803 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1804 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1806 case NFSATTRBIT_TYPE:
1807 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1808 *tl = txdr_unsigned(NFDIR);
1809 retnum += NFSX_UNSIGNED;
1811 case NFSATTRBIT_FSID:
1812 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1814 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1816 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1817 retnum += NFSX_V4FSID;
1819 case NFSATTRBIT_RDATTRERROR:
1820 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1822 *tl = txdr_unsigned(NFSERR_MOVED);
1825 retnum += NFSX_UNSIGNED;
1827 case NFSATTRBIT_FSLOCATIONS:
1828 retnum += nfsm_strtom(nd, "/", 1);
1829 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1830 *tl = txdr_unsigned(refp->nfr_srvcnt);
1831 retnum += NFSX_UNSIGNED;
1832 cp = refp->nfr_srvlist;
1833 for (i = 0; i < refp->nfr_srvcnt; i++) {
1834 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1835 *tl = txdr_unsigned(1);
1836 retnum += NFSX_UNSIGNED;
1837 cp2 = STRCHR(cp, ':');
1842 retnum += nfsm_strtom(nd, cp, len);
1845 cp2 = STRCHR(cp, ',');
1850 retnum += nfsm_strtom(nd, cp, len);
1855 case NFSATTRBIT_MOUNTEDONFILEID:
1856 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1857 txdr_hyper(refp->nfr_dfileno, tl);
1858 retnum += NFSX_HYPER;
1861 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1865 *retnump = txdr_unsigned(retnum);
1866 return (retnum + prefixnum);
1870 * Parse a file name out of a request.
1873 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1874 NFSPATHLEN_T *outlenp)
1876 char *fromcp, *tocp, val = '\0';
1879 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1888 * For V4, check for lookup parent.
1889 * Otherwise, get the component name.
1891 if ((nd->nd_flag & ND_NFSV4) && nd->nd_procnum == NFSV4OP_LOOKUPP) {
1893 hash += ((u_char)'.');
1895 hash += ((u_char)'.');
1899 * First, get the name length.
1901 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1902 len = fxdr_unsigned(int, *tl);
1903 if (len > NFS_MAXNAMLEN) {
1904 nd->nd_repstat = NFSERR_NAMETOL;
1907 } else if (len <= 0) {
1908 nd->nd_repstat = NFSERR_INVAL;
1914 * Now, copy the component name into the buffer.
1916 fromcp = nd->nd_dpos;
1918 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1919 for (i = 0; i < len; i++) {
1926 fromcp = mtod(md, caddr_t);
1929 if (*fromcp == '\0') {
1930 nd->nd_repstat = EACCES;
1935 * For lookups on the public filehandle, do some special
1936 * processing on the name. (The public file handle is the
1937 * root of the public file system for this server.)
1939 if (nd->nd_flag & ND_PUBLOOKUP) {
1941 * If the first char is ASCII, it is a canonical
1942 * path, otherwise it is a native path. (RFC2054
1943 * doesn't actually state what it is if the first
1944 * char isn't ASCII or 0x80, so I assume native.)
1945 * pubtype == 1 -> native path
1946 * pubtype == 2 -> canonical path
1949 if (*fromcp & 0x80) {
1951 * Since RFC2054 doesn't indicate
1952 * that a native path of just 0x80
1953 * isn't allowed, I'll replace the
1954 * 0x80 with '/' instead of just
1964 * '/' only allowed in a native path
1966 if (*fromcp == '/' && pubtype != 1) {
1967 nd->nd_repstat = EACCES;
1973 * For the special case of 2 hex digits after a
1974 * '%' in an absolute path, calculate the value.
1975 * percent == 1 -> indicates "get first hex digit"
1976 * percent == 2 -> indicates "get second hex digit"
1979 digit = nfsrv_hexdigit(*fromcp, &error);
1981 nd->nd_repstat = EACCES;
1992 hash += ((u_char)val);
1996 if (*fromcp == '%' && pubtype == 2) {
1998 * Must be followed by 2 hex digits
2000 if ((len - i) < 3) {
2001 nd->nd_repstat = EACCES;
2008 hash += ((u_char)*fromcp);
2014 * Normal, non lookup on public, name.
2016 if (*fromcp == '/') {
2017 if (nd->nd_flag & ND_NFSV4)
2018 nd->nd_repstat = NFSERR_BADNAME;
2020 nd->nd_repstat = EACCES;
2024 hash += ((u_char)*fromcp);
2032 nd->nd_dpos = fromcp;
2033 i = NFSM_RNDUP(len) - len;
2038 error = nfsm_advance(nd, i, rem);
2045 * For v4, don't allow lookups of '.' or '..' and
2046 * also check for non-utf8 strings.
2048 if (nd->nd_flag & ND_NFSV4) {
2049 if ((outlen == 1 && bufp[0] == '.') ||
2050 (outlen == 2 && bufp[0] == '.' &&
2052 nd->nd_repstat = NFSERR_BADNAME;
2056 if (enable_checkutf8 == 1 &&
2057 nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
2058 nd->nd_repstat = NFSERR_INVAL;
2065 *outlenp = (size_t)outlen;
2069 NFSEXITCODE2(error, nd);
2077 static int inited = 0;
2084 * Initialize client queues. Don't free/reinitialize
2085 * them when nfsds are restarted.
2087 nfsclienthash = malloc(sizeof(struct nfsclienthashhead) *
2088 nfsrv_clienthashsize, M_NFSDCLIENT, M_WAITOK | M_ZERO);
2089 for (i = 0; i < nfsrv_clienthashsize; i++)
2090 LIST_INIT(&nfsclienthash[i]);
2091 nfslockhash = malloc(sizeof(struct nfslockhashhead) *
2092 nfsrv_lockhashsize, M_NFSDLOCKFILE, M_WAITOK | M_ZERO);
2093 for (i = 0; i < nfsrv_lockhashsize; i++)
2094 LIST_INIT(&nfslockhash[i]);
2095 nfssessionhash = malloc(sizeof(struct nfssessionhash) *
2096 nfsrv_sessionhashsize, M_NFSDSESSION, M_WAITOK | M_ZERO);
2097 for (i = 0; i < nfsrv_sessionhashsize; i++) {
2098 mtx_init(&nfssessionhash[i].mtx, "nfssm", NULL, MTX_DEF);
2099 LIST_INIT(&nfssessionhash[i].list);
2101 LIST_INIT(&nfsrv_dontlisthead);
2102 TAILQ_INIT(&nfsrv_recalllisthead);
2104 /* and the v2 pubfh should be all zeros */
2105 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2109 * Check the v4 root exports.
2110 * Return 0 if ok, 1 otherwise.
2113 nfsd_checkrootexp(struct nfsrv_descript *nd)
2116 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2118 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2119 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2121 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2122 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2124 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2125 ND_EXGSS)) == (ND_GSS | ND_EXGSS))
2129 if ((nd->nd_flag & ND_EXTLS) == 0)
2131 if ((nd->nd_flag & (ND_TLSCERTUSER | ND_EXTLSCERTUSER)) ==
2132 (ND_TLSCERTUSER | ND_EXTLSCERTUSER))
2134 if ((nd->nd_flag & (ND_TLSCERT | ND_EXTLSCERT | ND_EXTLSCERTUSER)) ==
2135 (ND_TLSCERT | ND_EXTLSCERT))
2137 if ((nd->nd_flag & (ND_TLS | ND_EXTLSCERTUSER | ND_EXTLSCERT)) ==
2144 * Parse the first part of an NFSv4 compound to find out what the minor
2148 nfsd_getminorvers(struct nfsrv_descript *nd, u_char *tag, u_char **tagstrp,
2149 int *taglenp, u_int32_t *minversp)
2152 int error = 0, taglen = -1;
2153 u_char *tagstr = NULL;
2155 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2156 taglen = fxdr_unsigned(int, *tl);
2157 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
2161 if (taglen <= NFSV4_SMALLSTR)
2164 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2165 error = nfsrv_mtostr(nd, tagstr, taglen);
2168 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2169 *minversp = fxdr_unsigned(u_int32_t, *tl);
2171 if (*minversp == NFSV41_MINORVERSION)
2172 nd->nd_flag |= ND_NFSV41;
2173 else if (*minversp == NFSV42_MINORVERSION)
2174 nd->nd_flag |= (ND_NFSV41 | ND_NFSV42);
2177 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
2178 free(tagstr, M_TEMP);