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_rootfhset;
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];
66 static u_int32_t nfsrv_isannfserr(u_int32_t);
68 SYSCTL_DECL(_vfs_nfsd);
70 static int enable_checkutf8 = 1;
71 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_checkutf8, CTLFLAG_RW,
73 "Enable the NFSv4 check for the UTF8 compliant name required by rfc3530");
75 static int enable_nobodycheck = 1;
76 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nobodycheck, CTLFLAG_RW,
77 &enable_nobodycheck, 0,
78 "Enable the NFSv4 check when setting user nobody as owner");
80 static int enable_nogroupcheck = 1;
81 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nogroupcheck, CTLFLAG_RW,
82 &enable_nogroupcheck, 0,
83 "Enable the NFSv4 check when setting group nogroup as owner");
85 static char nfsrv_hexdigit(char, int *);
88 * Maps errno values to nfs error numbers.
89 * Use NFSERR_IO as the catch all for ones not specifically defined in
90 * RFC 1094. (It now includes the errors added for NFSv3.)
92 static u_char nfsrv_v2errmap[NFSERR_REMOTE] = {
93 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
94 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
95 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
96 NFSERR_IO, NFSERR_EXIST, NFSERR_XDEV, NFSERR_NODEV, NFSERR_NOTDIR,
97 NFSERR_ISDIR, NFSERR_INVAL, NFSERR_IO, NFSERR_IO, NFSERR_IO,
98 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
99 NFSERR_MLINK, 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_IO, NFSERR_IO, NFSERR_IO,
105 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
106 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
111 * Maps errno values to nfs error numbers.
112 * Although it is not obvious whether or not NFS clients really care if
113 * a returned error value is in the specified list for the procedure, the
114 * safest thing to do is filter them appropriately. For Version 2, the
115 * X/Open XNFS document is the only specification that defines error values
116 * for each RPC (The RFC simply lists all possible error values for all RPCs),
117 * so I have decided to not do this for Version 2.
118 * The first entry is the default error return and the rest are the valid
119 * errors for that RPC in increasing numeric order.
121 static short nfsv3err_null[] = {
126 static short nfsv3err_getattr[] = {
136 static short nfsv3err_setattr[] = {
153 static short nfsv3err_lookup[] = {
167 static short nfsv3err_access[] = {
177 static short nfsv3err_readlink[] = {
190 static short nfsv3err_read[] = {
203 static short nfsv3err_write[] = {
219 static short nfsv3err_create[] = {
237 static short nfsv3err_mkdir[] = {
255 static short nfsv3err_symlink[] = {
273 static short nfsv3err_mknod[] = {
292 static short nfsv3err_remove[] = {
307 static short nfsv3err_rmdir[] = {
326 static short nfsv3err_rename[] = {
350 static short nfsv3err_link[] = {
371 static short nfsv3err_readdir[] = {
385 static short nfsv3err_readdirplus[] = {
400 static short nfsv3err_fsstat[] = {
410 static short nfsv3err_fsinfo[] = {
419 static short nfsv3err_pathconf[] = {
428 static short nfsv3err_commit[] = {
438 static short *nfsrv_v3errmap[] = {
456 nfsv3err_readdirplus,
464 * And the same for V4.
466 static short nfsv4err_null[] = {
471 static short nfsv4err_access[] = {
488 static short nfsv4err_close[] = {
512 static short nfsv4err_commit[] = {
530 static short nfsv4err_create[] = {
559 static short nfsv4err_delegpurge[] = {
567 NFSERR_STALECLIENTID,
571 static short nfsv4err_delegreturn[] = {
590 static short nfsv4err_getattr[] = {
607 static short nfsv4err_getfh[] = {
619 static short nfsv4err_link[] = {
651 static short nfsv4err_lock[] = {
677 NFSERR_RECLAIMCONFLICT,
681 NFSERR_STALECLIENTID,
686 static short nfsv4err_lockt[] = {
705 NFSERR_STALECLIENTID,
709 static short nfsv4err_locku[] = {
735 static short nfsv4err_lookup[] = {
758 static short nfsv4err_lookupp[] = {
774 static short nfsv4err_nverify[] = {
794 static short nfsv4err_open[] = {
825 NFSERR_RECLAIMCONFLICT,
831 NFSERR_STALECLIENTID,
837 static short nfsv4err_openattr[] = {
858 static short nfsv4err_openconfirm[] = {
879 static short nfsv4err_opendowngrade[] = {
899 static short nfsv4err_putfh[] = {
912 static short nfsv4err_putpubfh[] = {
920 static short nfsv4err_putrootfh[] = {
928 static short nfsv4err_read[] = {
956 static short nfsv4err_readdir[] = {
977 static short nfsv4err_readlink[] = {
995 static short nfsv4err_remove[] = {
1010 NFSERR_NOFILEHANDLE,
1020 static short nfsv4err_rename[] = {
1037 NFSERR_NOFILEHANDLE,
1050 static short nfsv4err_renew[] = {
1053 NFSERR_ADMINREVOKED,
1060 NFSERR_STALECLIENTID,
1064 static short nfsv4err_restorefh[] = {
1077 static short nfsv4err_savefh[] = {
1082 NFSERR_NOFILEHANDLE,
1089 static short nfsv4err_secinfo[] = {
1101 NFSERR_NOFILEHANDLE,
1109 static short nfsv4err_setattr[] = {
1112 NFSERR_ADMINREVOKED,
1130 NFSERR_NOFILEHANDLE,
1139 NFSERR_STALESTATEID,
1143 static short nfsv4err_setclientid[] = {
1154 static short nfsv4err_setclientidconfirm[] = {
1160 NFSERR_STALECLIENTID,
1164 static short nfsv4err_verify[] = {
1175 NFSERR_NOFILEHANDLE,
1183 static short nfsv4err_write[] = {
1186 NFSERR_ADMINREVOKED,
1202 NFSERR_NOFILEHANDLE,
1211 NFSERR_STALESTATEID,
1215 static short nfsv4err_releaselockowner[] = {
1217 NFSERR_ADMINREVOKED,
1224 NFSERR_STALECLIENTID,
1228 static short *nfsrv_v4errmap[] = {
1236 nfsv4err_delegpurge,
1237 nfsv4err_delegreturn,
1249 nfsv4err_openconfirm,
1250 nfsv4err_opendowngrade,
1264 nfsv4err_setclientid,
1265 nfsv4err_setclientidconfirm,
1268 nfsv4err_releaselockowner,
1272 * Trim tlen bytes off the end of the mbuf list and then ensure
1273 * the end of the last mbuf is nul filled to a long boundary,
1274 * as indicated by the value of "nul".
1275 * Return the last mbuf in the updated list and free and mbufs
1276 * that follow it in the original list.
1277 * This is somewhat different than the old nfsrv_adj() with
1278 * support for ext_pgs mbufs. It frees the remaining mbufs
1279 * instead of setting them 0 length, since lists of ext_pgs
1280 * mbufs are all expected to be non-empty.
1283 nfsrv_adj(struct mbuf *mp, int len, int nul)
1285 struct mbuf *m, *m2;
1287 int i, lastlen, pgno, plen, tlen, trim;
1292 * Find the last mbuf after adjustment and
1293 * how much it needs to be adjusted by.
1299 if (m->m_next == NULL)
1303 /* m is now the last mbuf and tlen the total length. */
1305 if (len >= m->m_len) {
1306 /* Need to trim away the last mbuf(s). */
1317 lastlen = m->m_len - len;
1320 * m is now the last mbuf after trimming and its length needs to
1322 * Adjust the last mbuf and set cp to point to where nuls must be
1325 if ((m->m_flags & M_EXTPG) != 0) {
1326 pgno = m->m_epg_npgs - 1;
1327 off = (pgno == 0) ? m->m_epg_1st_off : 0;
1328 plen = m_epg_pagelen(m, pgno, off);
1329 if (m->m_len > lastlen) {
1330 /* Trim this mbuf. */
1331 trim = m->m_len - lastlen;
1332 while (trim >= plen) {
1334 ("nfsrv_adj: freeing page 0"));
1336 pg = PHYS_TO_VM_PAGE(m->m_epg_pa[pgno]);
1337 vm_page_unwire_noq(pg);
1342 off = (pgno == 0) ? m->m_epg_1st_off : 0;
1343 plen = m_epg_pagelen(m, pgno, off);
1346 m->m_epg_last_len = plen;
1349 cp = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
1350 cp += off + plen - nul;
1353 cp = mtod(m, char *) + m->m_len - nul;
1356 /* Write the nul bytes. */
1357 for (i = 0; i < nul; i++)
1360 /* Free up any mbufs past "m". */
1369 * Make these functions instead of macros, so that the kernel text size
1370 * doesn't get too big...
1373 nfsrv_wcc(struct nfsrv_descript *nd, int before_ret,
1374 struct nfsvattr *before_nvap, int after_ret, struct nfsvattr *after_nvap)
1379 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1382 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1383 *tl++ = newnfs_true;
1384 txdr_hyper(before_nvap->na_size, tl);
1386 txdr_nfsv3time(&(before_nvap->na_mtime), tl);
1388 txdr_nfsv3time(&(before_nvap->na_ctime), tl);
1390 nfsrv_postopattr(nd, after_ret, after_nvap);
1394 nfsrv_postopattr(struct nfsrv_descript *nd, int after_ret,
1395 struct nfsvattr *after_nvap)
1399 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1404 nfsrv_fillattr(nd, after_nvap);
1409 * Fill in file attributes for V2 and 3. For V4, call a separate
1410 * routine that sifts through all the attribute bits.
1413 nfsrv_fillattr(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1415 struct nfs_fattr *fp;
1419 * Build space for the attribute structure.
1421 if (nd->nd_flag & ND_NFSV3)
1422 fattr_size = NFSX_V3FATTR;
1424 fattr_size = NFSX_V2FATTR;
1425 NFSM_BUILD(fp, struct nfs_fattr *, fattr_size);
1428 * Now just fill it all in.
1430 fp->fa_nlink = txdr_unsigned(nvap->na_nlink);
1431 fp->fa_uid = txdr_unsigned(nvap->na_uid);
1432 fp->fa_gid = txdr_unsigned(nvap->na_gid);
1433 if (nd->nd_flag & ND_NFSV3) {
1434 fp->fa_type = vtonfsv34_type(nvap->na_type);
1435 fp->fa_mode = vtonfsv34_mode(nvap->na_mode);
1436 txdr_hyper(nvap->na_size, (uint32_t*)&fp->fa3_size);
1437 txdr_hyper(nvap->na_bytes, (uint32_t*)&fp->fa3_used);
1438 fp->fa3_rdev.specdata1 = txdr_unsigned(NFSMAJOR(nvap->na_rdev));
1439 fp->fa3_rdev.specdata2 = txdr_unsigned(NFSMINOR(nvap->na_rdev));
1440 fp->fa3_fsid.nfsuquad[0] = 0;
1441 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(nvap->na_fsid);
1442 txdr_hyper(nvap->na_fileid, (uint32_t*)&fp->fa3_fileid);
1443 txdr_nfsv3time(&nvap->na_atime, &fp->fa3_atime);
1444 txdr_nfsv3time(&nvap->na_mtime, &fp->fa3_mtime);
1445 txdr_nfsv3time(&nvap->na_ctime, &fp->fa3_ctime);
1447 fp->fa_type = vtonfsv2_type(nvap->na_type);
1448 fp->fa_mode = vtonfsv2_mode(nvap->na_type, nvap->na_mode);
1449 fp->fa2_size = txdr_unsigned(nvap->na_size);
1450 fp->fa2_blocksize = txdr_unsigned(nvap->na_blocksize);
1451 if (nvap->na_type == VFIFO)
1452 fp->fa2_rdev = 0xffffffff;
1454 fp->fa2_rdev = txdr_unsigned(nvap->na_rdev);
1455 fp->fa2_blocks = txdr_unsigned(nvap->na_bytes / NFS_FABLKSIZE);
1456 fp->fa2_fsid = txdr_unsigned(nvap->na_fsid);
1457 fp->fa2_fileid = txdr_unsigned(nvap->na_fileid);
1458 txdr_nfsv2time(&nvap->na_atime, &fp->fa2_atime);
1459 txdr_nfsv2time(&nvap->na_mtime, &fp->fa2_mtime);
1460 txdr_nfsv2time(&nvap->na_ctime, &fp->fa2_ctime);
1465 * This function gets a file handle out of an mbuf list.
1466 * It returns 0 for success, EBADRPC otherwise.
1467 * If sets the third flagp argument to 1 if the file handle is
1468 * the public file handle.
1469 * For NFSv4, if the length is incorrect, set nd_repstat == NFSERR_BADHANDLE
1472 nfsrv_mtofh(struct nfsrv_descript *nd, struct nfsrvfh *fhp)
1475 int error = 0, len, copylen;
1477 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1478 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1479 len = fxdr_unsigned(int, *tl);
1480 if (len == 0 && nfs_pubfhset && (nd->nd_flag & ND_NFSV3) &&
1481 nd->nd_procnum == NFSPROC_LOOKUP) {
1482 nd->nd_flag |= ND_PUBLOOKUP;
1487 /* If len == NFSX_V4PNFSFH the RPC is a pNFS DS one. */
1488 if (len == NFSX_V4PNFSFH && (nd->nd_flag & ND_NFSV41) != 0) {
1489 copylen = NFSX_MYFH;
1490 len = NFSM_RNDUP(len);
1491 nd->nd_flag |= ND_DSSERVER;
1492 } else if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1493 if (nd->nd_flag & ND_NFSV4) {
1494 if (len > 0 && len <= NFSX_V4FHMAX) {
1495 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1498 nd->nd_repstat = NFSERR_BADHANDLE;
1511 * For NFSv2, the file handle is always 32 bytes on the
1512 * wire, but this server only cares about the first
1513 * NFSRV_MAXFH bytes.
1516 copylen = NFSRV_MAXFH;
1518 NFSM_DISSECT(tl, u_int32_t *, len);
1519 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1520 nd->nd_procnum == NFSPROC_LOOKUP &&
1521 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1522 nd->nd_flag |= ND_PUBLOOKUP;
1525 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1526 fhp->nfsrvfh_len = copylen;
1528 NFSEXITCODE2(error, nd);
1533 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1534 * numbers not specified for the associated procedure.
1535 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1536 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1537 * RPC procedure is not involved.
1538 * Returns the error number in XDR.
1541 nfsd_errmap(struct nfsrv_descript *nd)
1543 short *defaulterrp, *errp;
1545 if (!nd->nd_repstat)
1547 if ((nd->nd_repstat & NFSERR_AUTHERR) != 0)
1548 return (txdr_unsigned(NFSERR_ACCES));
1549 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1550 if (nd->nd_procnum == NFSPROC_NOOP)
1551 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1552 if (nd->nd_flag & ND_NFSV3)
1553 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1554 else if (nd->nd_repstat == EBADRPC)
1555 return (txdr_unsigned(NFSERR_BADXDR));
1556 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1557 nd->nd_repstat == NFSERR_OPILLEGAL)
1558 return (txdr_unsigned(nd->nd_repstat));
1559 else if (nd->nd_repstat == NFSERR_REPLYFROMCACHE)
1560 return (txdr_unsigned(NFSERR_IO));
1561 else if ((nd->nd_flag & ND_NFSV41) != 0) {
1562 if (nd->nd_repstat == EOPNOTSUPP)
1563 nd->nd_repstat = NFSERR_NOTSUPP;
1564 nd->nd_repstat = nfsrv_isannfserr(nd->nd_repstat);
1565 return (txdr_unsigned(nd->nd_repstat));
1567 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1569 if (*errp == nd->nd_repstat)
1570 return (txdr_unsigned(nd->nd_repstat));
1571 return (txdr_unsigned(*defaulterrp));
1573 if (nd->nd_repstat <= NFSERR_REMOTE)
1574 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1575 return (txdr_unsigned(NFSERR_IO));
1579 * Check to see if the error is a valid NFS one. If not, replace it with
1583 nfsrv_isannfserr(u_int32_t errval)
1586 if (errval == NFSERR_OK)
1588 if (errval >= NFSERR_BADHANDLE && errval <= NFSERR_MAXERRVAL)
1590 if (errval > 0 && errval <= NFSERR_REMOTE)
1591 return (nfsrv_v2errmap[errval - 1]);
1596 * Check to see if setting a uid/gid is permitted when creating a new
1597 * file object. (Called when uid and/or gid is specified in the
1598 * settable attributes for V4.
1601 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1606 * If not setting either uid nor gid, it's OK.
1608 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1610 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid == nfsrv_defaultuid &&
1611 enable_nobodycheck == 1)
1612 || (NFSVNO_ISSETGID(nvap) && nvap->na_gid == nfsrv_defaultgid &&
1613 enable_nogroupcheck == 1)) {
1614 error = NFSERR_BADOWNER;
1617 if (nd->nd_cred->cr_uid == 0)
1619 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1620 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1621 !groupmember(nvap->na_gid, nd->nd_cred)))
1622 error = NFSERR_PERM;
1625 NFSEXITCODE2(error, nd);
1630 * and this routine fixes up the settable attributes for V4 if allowed
1631 * by nfsrv_checkuidgid().
1634 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1635 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1636 struct nfsexstuff *exp)
1639 struct nfsvattr nva;
1642 nfsattrbit_t nattrbits;
1645 * Maybe this should be done for V2 and 3 but it never has been
1646 * and nobody seems to be upset, so I think it's best not to change
1647 * the V2 and 3 semantics.
1649 if ((nd->nd_flag & ND_NFSV4) == 0)
1651 NFSVNO_ATTRINIT(&nva);
1652 NFSZERO_ATTRBIT(&nattrbits);
1653 tuid = nd->nd_cred->cr_uid;
1654 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1655 NFSVNO_ISSETUID(nvap) &&
1656 nvap->na_uid != nd->nd_cred->cr_uid) {
1657 if (nd->nd_cred->cr_uid == 0) {
1658 nva.na_uid = nvap->na_uid;
1660 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1662 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1665 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1666 NFSVNO_ISSETATIME(nvap)) {
1667 nva.na_atime = nvap->na_atime;
1669 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1671 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1672 NFSVNO_ISSETMTIME(nvap)) {
1673 nva.na_mtime = nvap->na_mtime;
1675 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1677 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1678 NFSVNO_ISSETGID(nvap)) {
1679 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1680 groupmember(nvap->na_gid, nd->nd_cred)) {
1681 nd->nd_cred->cr_uid = 0;
1682 nva.na_gid = nvap->na_gid;
1684 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1686 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1690 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1692 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1695 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1696 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1697 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1699 #ifdef NFS4_ACL_EXTATTR_NAME
1700 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1701 nfsrv_useacl != 0 && aclp != NULL) {
1702 if (aclp->acl_cnt > 0) {
1703 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1705 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1710 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1711 nd->nd_cred->cr_uid = tuid;
1714 NFSEXITCODE2(0, nd);
1718 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1719 * char isn't a hex digit.
1722 nfsrv_hexdigit(char c, int *err)
1726 if (c >= '0' && c <= '9')
1728 if (c >= 'a' && c <= 'f')
1729 return (c - 'a' + ((char)10));
1730 if (c >= 'A' && c <= 'F')
1731 return (c - 'A' + ((char)10));
1734 return (1); /* BOGUS */
1738 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1742 nfsrv_errmoved(int op)
1746 errp = nfsrv_v4errmap[op];
1747 while (*errp != 0) {
1748 if (*errp == NFSERR_MOVED)
1756 * Fill in attributes for a Referral.
1757 * (Return the number of bytes of XDR created.)
1760 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1761 struct nfsreferral *refp, int getattr, int *reterrp)
1763 u_int32_t *tl, *retnump;
1765 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1766 int fslocationsbit = 0;
1767 nfsattrbit_t tmpbits, refbits;
1769 NFSREFERRAL_ATTRBIT(&refbits);
1771 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1772 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1774 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1778 * Check for the case where unsupported referral attributes are
1781 NFSSET_ATTRBIT(&tmpbits, retbitp);
1782 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1783 if (NFSNONZERO_ATTRBIT(&tmpbits))
1786 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1787 *reterrp = NFSERR_MOVED;
1792 * Now we can fill in the attributes.
1794 NFSSET_ATTRBIT(&tmpbits, retbitp);
1795 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1798 * Put out the attribute bitmap for the ones being filled in
1799 * and get the field for the number of attributes returned.
1801 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1802 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1803 prefixnum += NFSX_UNSIGNED;
1806 * Now, loop around filling in the attributes for each bit set.
1808 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1809 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1811 case NFSATTRBIT_TYPE:
1812 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1813 *tl = txdr_unsigned(NFDIR);
1814 retnum += NFSX_UNSIGNED;
1816 case NFSATTRBIT_FSID:
1817 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1819 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1821 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1822 retnum += NFSX_V4FSID;
1824 case NFSATTRBIT_RDATTRERROR:
1825 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1827 *tl = txdr_unsigned(NFSERR_MOVED);
1830 retnum += NFSX_UNSIGNED;
1832 case NFSATTRBIT_FSLOCATIONS:
1833 retnum += nfsm_strtom(nd, "/", 1);
1834 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1835 *tl = txdr_unsigned(refp->nfr_srvcnt);
1836 retnum += NFSX_UNSIGNED;
1837 cp = refp->nfr_srvlist;
1838 for (i = 0; i < refp->nfr_srvcnt; i++) {
1839 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1840 *tl = txdr_unsigned(1);
1841 retnum += NFSX_UNSIGNED;
1842 cp2 = STRCHR(cp, ':');
1847 retnum += nfsm_strtom(nd, cp, len);
1850 cp2 = STRCHR(cp, ',');
1855 retnum += nfsm_strtom(nd, cp, len);
1860 case NFSATTRBIT_MOUNTEDONFILEID:
1861 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1862 txdr_hyper(refp->nfr_dfileno, tl);
1863 retnum += NFSX_HYPER;
1866 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1870 *retnump = txdr_unsigned(retnum);
1871 return (retnum + prefixnum);
1875 * Parse a file name out of a request.
1878 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1879 NFSPATHLEN_T *outlenp)
1881 char *fromcp, *tocp, val = '\0';
1884 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1893 * For V4, check for lookup parent.
1894 * Otherwise, get the component name.
1896 if ((nd->nd_flag & ND_NFSV4) && (nd->nd_procnum == NFSV4OP_LOOKUPP ||
1897 nd->nd_procnum == NFSV4OP_SECINFONONAME)) {
1899 hash += ((u_char)'.');
1901 hash += ((u_char)'.');
1905 * First, get the name length.
1907 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1908 len = fxdr_unsigned(int, *tl);
1909 if (len > NFS_MAXNAMLEN) {
1910 nd->nd_repstat = NFSERR_NAMETOL;
1913 } else if (len <= 0) {
1914 nd->nd_repstat = NFSERR_INVAL;
1920 * Now, copy the component name into the buffer.
1922 fromcp = nd->nd_dpos;
1924 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1925 for (i = 0; i < len; i++) {
1932 fromcp = mtod(md, caddr_t);
1935 if (*fromcp == '\0') {
1936 nd->nd_repstat = EACCES;
1941 * For lookups on the public filehandle, do some special
1942 * processing on the name. (The public file handle is the
1943 * root of the public file system for this server.)
1945 if (nd->nd_flag & ND_PUBLOOKUP) {
1947 * If the first char is ASCII, it is a canonical
1948 * path, otherwise it is a native path. (RFC2054
1949 * doesn't actually state what it is if the first
1950 * char isn't ASCII or 0x80, so I assume native.)
1951 * pubtype == 1 -> native path
1952 * pubtype == 2 -> canonical path
1955 if (*fromcp & 0x80) {
1957 * Since RFC2054 doesn't indicate
1958 * that a native path of just 0x80
1959 * isn't allowed, I'll replace the
1960 * 0x80 with '/' instead of just
1970 * '/' only allowed in a native path
1972 if (*fromcp == '/' && pubtype != 1) {
1973 nd->nd_repstat = EACCES;
1979 * For the special case of 2 hex digits after a
1980 * '%' in an absolute path, calculate the value.
1981 * percent == 1 -> indicates "get first hex digit"
1982 * percent == 2 -> indicates "get second hex digit"
1985 digit = nfsrv_hexdigit(*fromcp, &error);
1987 nd->nd_repstat = EACCES;
1998 hash += ((u_char)val);
2002 if (*fromcp == '%' && pubtype == 2) {
2004 * Must be followed by 2 hex digits
2006 if ((len - i) < 3) {
2007 nd->nd_repstat = EACCES;
2014 hash += ((u_char)*fromcp);
2020 * Normal, non lookup on public, name.
2022 if (*fromcp == '/') {
2023 if (nd->nd_flag & ND_NFSV4)
2024 nd->nd_repstat = NFSERR_BADNAME;
2026 nd->nd_repstat = EACCES;
2030 hash += ((u_char)*fromcp);
2038 nd->nd_dpos = fromcp;
2039 i = NFSM_RNDUP(len) - len;
2044 error = nfsm_advance(nd, i, rem);
2051 * For v4, don't allow lookups of '.' or '..' and
2052 * also check for non-utf8 strings.
2054 if (nd->nd_flag & ND_NFSV4) {
2055 if ((outlen == 1 && bufp[0] == '.') ||
2056 (outlen == 2 && bufp[0] == '.' &&
2058 nd->nd_repstat = NFSERR_BADNAME;
2062 if (enable_checkutf8 == 1 &&
2063 nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
2064 nd->nd_repstat = NFSERR_INVAL;
2071 *outlenp = (size_t)outlen + 1;
2075 NFSEXITCODE2(error, nd);
2083 static int inited = 0;
2090 * Initialize client queues. Don't free/reinitialize
2091 * them when nfsds are restarted.
2093 nfsclienthash = malloc(sizeof(struct nfsclienthashhead) *
2094 nfsrv_clienthashsize, M_NFSDCLIENT, M_WAITOK | M_ZERO);
2095 for (i = 0; i < nfsrv_clienthashsize; i++)
2096 LIST_INIT(&nfsclienthash[i]);
2097 nfslockhash = malloc(sizeof(struct nfslockhashhead) *
2098 nfsrv_lockhashsize, M_NFSDLOCKFILE, M_WAITOK | M_ZERO);
2099 for (i = 0; i < nfsrv_lockhashsize; i++)
2100 LIST_INIT(&nfslockhash[i]);
2101 nfssessionhash = malloc(sizeof(struct nfssessionhash) *
2102 nfsrv_sessionhashsize, M_NFSDSESSION, M_WAITOK | M_ZERO);
2103 for (i = 0; i < nfsrv_sessionhashsize; i++) {
2104 mtx_init(&nfssessionhash[i].mtx, "nfssm", NULL, MTX_DEF);
2105 LIST_INIT(&nfssessionhash[i].list);
2107 LIST_INIT(&nfsrv_dontlisthead);
2108 TAILQ_INIT(&nfsrv_recalllisthead);
2110 /* and the v2 pubfh should be all zeros */
2111 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2115 * Check the v4 root exports.
2116 * Return 0 if ok, 1 otherwise.
2119 nfsd_checkrootexp(struct nfsrv_descript *nd)
2122 if (nfs_rootfhset == 0)
2123 return (NFSERR_AUTHERR | AUTH_FAILED);
2125 * For NFSv4.1/4.2, if the client specifies SP4_NONE, then these
2126 * operations are allowed regardless of the value of the "sec=XXX"
2127 * field in the V4: exports line.
2128 * As such, these Kerberos checks only apply to NFSv4.0 mounts.
2130 if ((nd->nd_flag & ND_NFSV41) != 0)
2132 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2134 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2135 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2137 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2138 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2140 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2141 ND_EXGSS)) == (ND_GSS | ND_EXGSS))
2143 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2145 if ((nd->nd_flag & ND_EXTLS) == 0)
2147 if ((nd->nd_flag & (ND_TLSCERTUSER | ND_EXTLSCERTUSER)) ==
2148 (ND_TLSCERTUSER | ND_EXTLSCERTUSER))
2150 if ((nd->nd_flag & (ND_TLSCERT | ND_EXTLSCERT | ND_EXTLSCERTUSER)) ==
2151 (ND_TLSCERT | ND_EXTLSCERT))
2153 if ((nd->nd_flag & (ND_TLS | ND_EXTLSCERTUSER | ND_EXTLSCERT)) ==
2157 /* There is currently no auth_stat for this. */
2158 if ((nd->nd_flag & ND_TLS) == 0)
2159 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS);
2160 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST);
2162 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
2166 * Parse the first part of an NFSv4 compound to find out what the minor
2170 nfsd_getminorvers(struct nfsrv_descript *nd, u_char *tag, u_char **tagstrp,
2171 int *taglenp, u_int32_t *minversp)
2174 int error = 0, taglen = -1;
2175 u_char *tagstr = NULL;
2177 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2178 taglen = fxdr_unsigned(int, *tl);
2179 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
2183 if (taglen <= NFSV4_SMALLSTR)
2186 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2187 error = nfsrv_mtostr(nd, tagstr, taglen);
2190 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2191 *minversp = fxdr_unsigned(u_int32_t, *tl);
2193 if (*minversp == NFSV41_MINORVERSION)
2194 nd->nd_flag |= ND_NFSV41;
2195 else if (*minversp == NFSV42_MINORVERSION)
2196 nd->nd_flag |= (ND_NFSV41 | ND_NFSV42);
2199 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
2200 free(tagstr, M_TEMP);