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 * A fiddled version of m_adj() that ensures null fill to a long
1272 * boundary and only trims off the back end
1275 nfsrv_adj(struct mbuf *mp, int len, int nul)
1282 * Trim from tail. Scan the mbuf chain,
1283 * calculating its length and finding the last mbuf.
1284 * If the adjustment only affects this mbuf, then just
1285 * adjust and return. Otherwise, rescan and truncate
1286 * after the remaining size.
1292 if (m->m_next == NULL)
1296 if (m->m_len > len) {
1299 cp = mtod(m, caddr_t) + m->m_len - nul;
1300 for (i = 0; i < nul; i++)
1309 * Correct length for chain is "count".
1310 * Find the mbuf with last data, adjust its length,
1311 * and toss data from remaining mbufs on chain.
1313 for (m = mp; m; m = m->m_next) {
1314 if (m->m_len >= count) {
1317 cp = mtod(m, caddr_t) + m->m_len - nul;
1318 for (i = 0; i < nul; i++)
1325 for (m = m->m_next; m; m = m->m_next)
1330 * Make these functions instead of macros, so that the kernel text size
1331 * doesn't get too big...
1334 nfsrv_wcc(struct nfsrv_descript *nd, int before_ret,
1335 struct nfsvattr *before_nvap, int after_ret, struct nfsvattr *after_nvap)
1340 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1343 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1344 *tl++ = newnfs_true;
1345 txdr_hyper(before_nvap->na_size, tl);
1347 txdr_nfsv3time(&(before_nvap->na_mtime), tl);
1349 txdr_nfsv3time(&(before_nvap->na_ctime), tl);
1351 nfsrv_postopattr(nd, after_ret, after_nvap);
1355 nfsrv_postopattr(struct nfsrv_descript *nd, int after_ret,
1356 struct nfsvattr *after_nvap)
1360 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1365 nfsrv_fillattr(nd, after_nvap);
1370 * Fill in file attributes for V2 and 3. For V4, call a separate
1371 * routine that sifts through all the attribute bits.
1374 nfsrv_fillattr(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1376 struct nfs_fattr *fp;
1380 * Build space for the attribute structure.
1382 if (nd->nd_flag & ND_NFSV3)
1383 fattr_size = NFSX_V3FATTR;
1385 fattr_size = NFSX_V2FATTR;
1386 NFSM_BUILD(fp, struct nfs_fattr *, fattr_size);
1389 * Now just fill it all in.
1391 fp->fa_nlink = txdr_unsigned(nvap->na_nlink);
1392 fp->fa_uid = txdr_unsigned(nvap->na_uid);
1393 fp->fa_gid = txdr_unsigned(nvap->na_gid);
1394 if (nd->nd_flag & ND_NFSV3) {
1395 fp->fa_type = vtonfsv34_type(nvap->na_type);
1396 fp->fa_mode = vtonfsv34_mode(nvap->na_mode);
1397 txdr_hyper(nvap->na_size, &fp->fa3_size);
1398 txdr_hyper(nvap->na_bytes, &fp->fa3_used);
1399 fp->fa3_rdev.specdata1 = txdr_unsigned(NFSMAJOR(nvap->na_rdev));
1400 fp->fa3_rdev.specdata2 = txdr_unsigned(NFSMINOR(nvap->na_rdev));
1401 fp->fa3_fsid.nfsuquad[0] = 0;
1402 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(nvap->na_fsid);
1403 txdr_hyper(nvap->na_fileid, &fp->fa3_fileid);
1404 txdr_nfsv3time(&nvap->na_atime, &fp->fa3_atime);
1405 txdr_nfsv3time(&nvap->na_mtime, &fp->fa3_mtime);
1406 txdr_nfsv3time(&nvap->na_ctime, &fp->fa3_ctime);
1408 fp->fa_type = vtonfsv2_type(nvap->na_type);
1409 fp->fa_mode = vtonfsv2_mode(nvap->na_type, nvap->na_mode);
1410 fp->fa2_size = txdr_unsigned(nvap->na_size);
1411 fp->fa2_blocksize = txdr_unsigned(nvap->na_blocksize);
1412 if (nvap->na_type == VFIFO)
1413 fp->fa2_rdev = 0xffffffff;
1415 fp->fa2_rdev = txdr_unsigned(nvap->na_rdev);
1416 fp->fa2_blocks = txdr_unsigned(nvap->na_bytes / NFS_FABLKSIZE);
1417 fp->fa2_fsid = txdr_unsigned(nvap->na_fsid);
1418 fp->fa2_fileid = txdr_unsigned(nvap->na_fileid);
1419 txdr_nfsv2time(&nvap->na_atime, &fp->fa2_atime);
1420 txdr_nfsv2time(&nvap->na_mtime, &fp->fa2_mtime);
1421 txdr_nfsv2time(&nvap->na_ctime, &fp->fa2_ctime);
1426 * This function gets a file handle out of an mbuf list.
1427 * It returns 0 for success, EBADRPC otherwise.
1428 * If sets the third flagp argument to 1 if the file handle is
1429 * the public file handle.
1430 * For NFSv4, if the length is incorrect, set nd_repstat == NFSERR_BADHANDLE
1433 nfsrv_mtofh(struct nfsrv_descript *nd, struct nfsrvfh *fhp)
1436 int error = 0, len, copylen;
1438 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1439 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1440 len = fxdr_unsigned(int, *tl);
1441 if (len == 0 && nfs_pubfhset && (nd->nd_flag & ND_NFSV3) &&
1442 nd->nd_procnum == NFSPROC_LOOKUP) {
1443 nd->nd_flag |= ND_PUBLOOKUP;
1448 /* If len == NFSX_V4PNFSFH the RPC is a pNFS DS one. */
1449 if (len == NFSX_V4PNFSFH && (nd->nd_flag & ND_NFSV41) != 0) {
1450 copylen = NFSX_MYFH;
1451 len = NFSM_RNDUP(len);
1452 nd->nd_flag |= ND_DSSERVER;
1453 } else if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1454 if (nd->nd_flag & ND_NFSV4) {
1455 if (len > 0 && len <= NFSX_V4FHMAX) {
1456 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1459 nd->nd_repstat = NFSERR_BADHANDLE;
1472 * For NFSv2, the file handle is always 32 bytes on the
1473 * wire, but this server only cares about the first
1474 * NFSRV_MAXFH bytes.
1477 copylen = NFSRV_MAXFH;
1479 NFSM_DISSECT(tl, u_int32_t *, len);
1480 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1481 nd->nd_procnum == NFSPROC_LOOKUP &&
1482 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1483 nd->nd_flag |= ND_PUBLOOKUP;
1486 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1487 fhp->nfsrvfh_len = copylen;
1489 NFSEXITCODE2(error, nd);
1494 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1495 * numbers not specified for the associated procedure.
1496 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1497 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1498 * RPC procedure is not involved.
1499 * Returns the error number in XDR.
1502 nfsd_errmap(struct nfsrv_descript *nd)
1504 short *defaulterrp, *errp;
1506 if (!nd->nd_repstat)
1508 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1509 if (nd->nd_procnum == NFSPROC_NOOP)
1510 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1511 if (nd->nd_flag & ND_NFSV3)
1512 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1513 else if (nd->nd_repstat == EBADRPC)
1514 return (txdr_unsigned(NFSERR_BADXDR));
1515 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1516 nd->nd_repstat == NFSERR_OPILLEGAL)
1517 return (txdr_unsigned(nd->nd_repstat));
1518 else if ((nd->nd_flag & ND_NFSV41) != 0) {
1519 if (nd->nd_repstat == EOPNOTSUPP)
1520 nd->nd_repstat = NFSERR_NOTSUPP;
1521 nd->nd_repstat = nfsrv_isannfserr(nd->nd_repstat);
1522 return (txdr_unsigned(nd->nd_repstat));
1524 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1526 if (*errp == nd->nd_repstat)
1527 return (txdr_unsigned(nd->nd_repstat));
1528 return (txdr_unsigned(*defaulterrp));
1530 if (nd->nd_repstat <= NFSERR_REMOTE)
1531 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1532 return (txdr_unsigned(NFSERR_IO));
1536 * Check to see if the error is a valid NFS one. If not, replace it with
1540 nfsrv_isannfserr(u_int32_t errval)
1543 if (errval == NFSERR_OK)
1545 if (errval >= NFSERR_BADHANDLE && errval <= NFSERR_MAXERRVAL)
1547 if (errval > 0 && errval <= NFSERR_REMOTE)
1548 return (nfsrv_v2errmap[errval - 1]);
1553 * Check to see if setting a uid/gid is permitted when creating a new
1554 * file object. (Called when uid and/or gid is specified in the
1555 * settable attributes for V4.
1558 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1563 * If not setting either uid nor gid, it's OK.
1565 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1567 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid == nfsrv_defaultuid &&
1568 enable_nobodycheck == 1)
1569 || (NFSVNO_ISSETGID(nvap) && nvap->na_gid == nfsrv_defaultgid &&
1570 enable_nogroupcheck == 1)) {
1571 error = NFSERR_BADOWNER;
1574 if (nd->nd_cred->cr_uid == 0)
1576 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1577 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1578 !groupmember(nvap->na_gid, nd->nd_cred)))
1579 error = NFSERR_PERM;
1582 NFSEXITCODE2(error, nd);
1587 * and this routine fixes up the settable attributes for V4 if allowed
1588 * by nfsrv_checkuidgid().
1591 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1592 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1593 struct nfsexstuff *exp)
1596 struct nfsvattr nva;
1599 nfsattrbit_t nattrbits;
1602 * Maybe this should be done for V2 and 3 but it never has been
1603 * and nobody seems to be upset, so I think it's best not to change
1604 * the V2 and 3 semantics.
1606 if ((nd->nd_flag & ND_NFSV4) == 0)
1608 NFSVNO_ATTRINIT(&nva);
1609 NFSZERO_ATTRBIT(&nattrbits);
1610 tuid = nd->nd_cred->cr_uid;
1611 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1612 NFSVNO_ISSETUID(nvap) &&
1613 nvap->na_uid != nd->nd_cred->cr_uid) {
1614 if (nd->nd_cred->cr_uid == 0) {
1615 nva.na_uid = nvap->na_uid;
1617 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1619 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1622 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1623 NFSVNO_ISSETATIME(nvap)) {
1624 nva.na_atime = nvap->na_atime;
1626 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1628 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1629 NFSVNO_ISSETMTIME(nvap)) {
1630 nva.na_mtime = nvap->na_mtime;
1632 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1634 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1635 NFSVNO_ISSETGID(nvap)) {
1636 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1637 groupmember(nvap->na_gid, nd->nd_cred)) {
1638 nd->nd_cred->cr_uid = 0;
1639 nva.na_gid = nvap->na_gid;
1641 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1643 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1647 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1649 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1652 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1653 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1654 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1656 #ifdef NFS4_ACL_EXTATTR_NAME
1657 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1658 nfsrv_useacl != 0 && aclp != NULL) {
1659 if (aclp->acl_cnt > 0) {
1660 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1662 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1667 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1668 nd->nd_cred->cr_uid = tuid;
1671 NFSEXITCODE2(0, nd);
1675 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1676 * char isn't a hex digit.
1679 nfsrv_hexdigit(char c, int *err)
1683 if (c >= '0' && c <= '9')
1685 if (c >= 'a' && c <= 'f')
1686 return (c - 'a' + ((char)10));
1687 if (c >= 'A' && c <= 'F')
1688 return (c - 'A' + ((char)10));
1691 return (1); /* BOGUS */
1695 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1699 nfsrv_errmoved(int op)
1703 errp = nfsrv_v4errmap[op];
1704 while (*errp != 0) {
1705 if (*errp == NFSERR_MOVED)
1713 * Fill in attributes for a Referral.
1714 * (Return the number of bytes of XDR created.)
1717 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1718 struct nfsreferral *refp, int getattr, int *reterrp)
1720 u_int32_t *tl, *retnump;
1722 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1723 int fslocationsbit = 0;
1724 nfsattrbit_t tmpbits, refbits;
1726 NFSREFERRAL_ATTRBIT(&refbits);
1728 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1729 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1731 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1735 * Check for the case where unsupported referral attributes are
1738 NFSSET_ATTRBIT(&tmpbits, retbitp);
1739 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1740 if (NFSNONZERO_ATTRBIT(&tmpbits))
1743 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1744 *reterrp = NFSERR_MOVED;
1749 * Now we can fill in the attributes.
1751 NFSSET_ATTRBIT(&tmpbits, retbitp);
1752 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1755 * Put out the attribute bitmap for the ones being filled in
1756 * and get the field for the number of attributes returned.
1758 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1759 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1760 prefixnum += NFSX_UNSIGNED;
1763 * Now, loop around filling in the attributes for each bit set.
1765 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1766 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1768 case NFSATTRBIT_TYPE:
1769 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1770 *tl = txdr_unsigned(NFDIR);
1771 retnum += NFSX_UNSIGNED;
1773 case NFSATTRBIT_FSID:
1774 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1776 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1778 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1779 retnum += NFSX_V4FSID;
1781 case NFSATTRBIT_RDATTRERROR:
1782 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1784 *tl = txdr_unsigned(NFSERR_MOVED);
1787 retnum += NFSX_UNSIGNED;
1789 case NFSATTRBIT_FSLOCATIONS:
1790 retnum += nfsm_strtom(nd, "/", 1);
1791 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1792 *tl = txdr_unsigned(refp->nfr_srvcnt);
1793 retnum += NFSX_UNSIGNED;
1794 cp = refp->nfr_srvlist;
1795 for (i = 0; i < refp->nfr_srvcnt; i++) {
1796 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1797 *tl = txdr_unsigned(1);
1798 retnum += NFSX_UNSIGNED;
1799 cp2 = STRCHR(cp, ':');
1804 retnum += nfsm_strtom(nd, cp, len);
1807 cp2 = STRCHR(cp, ',');
1812 retnum += nfsm_strtom(nd, cp, len);
1817 case NFSATTRBIT_MOUNTEDONFILEID:
1818 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1819 txdr_hyper(refp->nfr_dfileno, tl);
1820 retnum += NFSX_HYPER;
1823 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1827 *retnump = txdr_unsigned(retnum);
1828 return (retnum + prefixnum);
1832 * Parse a file name out of a request.
1835 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1836 NFSPATHLEN_T *outlenp)
1838 char *fromcp, *tocp, val = '\0';
1841 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1850 * For V4, check for lookup parent.
1851 * Otherwise, get the component name.
1853 if ((nd->nd_flag & ND_NFSV4) && nd->nd_procnum == NFSV4OP_LOOKUPP) {
1855 hash += ((u_char)'.');
1857 hash += ((u_char)'.');
1861 * First, get the name length.
1863 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1864 len = fxdr_unsigned(int, *tl);
1865 if (len > NFS_MAXNAMLEN) {
1866 nd->nd_repstat = NFSERR_NAMETOL;
1869 } else if (len <= 0) {
1870 nd->nd_repstat = NFSERR_INVAL;
1876 * Now, copy the component name into the buffer.
1878 fromcp = nd->nd_dpos;
1880 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1881 for (i = 0; i < len; i++) {
1888 fromcp = mtod(md, caddr_t);
1891 if (*fromcp == '\0') {
1892 nd->nd_repstat = EACCES;
1897 * For lookups on the public filehandle, do some special
1898 * processing on the name. (The public file handle is the
1899 * root of the public file system for this server.)
1901 if (nd->nd_flag & ND_PUBLOOKUP) {
1903 * If the first char is ASCII, it is a canonical
1904 * path, otherwise it is a native path. (RFC2054
1905 * doesn't actually state what it is if the first
1906 * char isn't ASCII or 0x80, so I assume native.)
1907 * pubtype == 1 -> native path
1908 * pubtype == 2 -> canonical path
1911 if (*fromcp & 0x80) {
1913 * Since RFC2054 doesn't indicate
1914 * that a native path of just 0x80
1915 * isn't allowed, I'll replace the
1916 * 0x80 with '/' instead of just
1926 * '/' only allowed in a native path
1928 if (*fromcp == '/' && pubtype != 1) {
1929 nd->nd_repstat = EACCES;
1935 * For the special case of 2 hex digits after a
1936 * '%' in an absolute path, calculate the value.
1937 * percent == 1 -> indicates "get first hex digit"
1938 * percent == 2 -> indicates "get second hex digit"
1941 digit = nfsrv_hexdigit(*fromcp, &error);
1943 nd->nd_repstat = EACCES;
1954 hash += ((u_char)val);
1958 if (*fromcp == '%' && pubtype == 2) {
1960 * Must be followed by 2 hex digits
1962 if ((len - i) < 3) {
1963 nd->nd_repstat = EACCES;
1970 hash += ((u_char)*fromcp);
1976 * Normal, non lookup on public, name.
1978 if (*fromcp == '/') {
1979 if (nd->nd_flag & ND_NFSV4)
1980 nd->nd_repstat = NFSERR_BADNAME;
1982 nd->nd_repstat = EACCES;
1986 hash += ((u_char)*fromcp);
1994 nd->nd_dpos = fromcp;
1995 i = NFSM_RNDUP(len) - len;
2000 error = nfsm_advance(nd, i, rem);
2007 * For v4, don't allow lookups of '.' or '..' and
2008 * also check for non-utf8 strings.
2010 if (nd->nd_flag & ND_NFSV4) {
2011 if ((outlen == 1 && bufp[0] == '.') ||
2012 (outlen == 2 && bufp[0] == '.' &&
2014 nd->nd_repstat = NFSERR_BADNAME;
2018 if (enable_checkutf8 == 1 &&
2019 nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
2020 nd->nd_repstat = NFSERR_INVAL;
2027 *outlenp = (size_t)outlen;
2031 NFSEXITCODE2(error, nd);
2039 static int inited = 0;
2046 * Initialize client queues. Don't free/reinitialize
2047 * them when nfsds are restarted.
2049 nfsclienthash = malloc(sizeof(struct nfsclienthashhead) *
2050 nfsrv_clienthashsize, M_NFSDCLIENT, M_WAITOK | M_ZERO);
2051 for (i = 0; i < nfsrv_clienthashsize; i++)
2052 LIST_INIT(&nfsclienthash[i]);
2053 nfslockhash = malloc(sizeof(struct nfslockhashhead) *
2054 nfsrv_lockhashsize, M_NFSDLOCKFILE, M_WAITOK | M_ZERO);
2055 for (i = 0; i < nfsrv_lockhashsize; i++)
2056 LIST_INIT(&nfslockhash[i]);
2057 nfssessionhash = malloc(sizeof(struct nfssessionhash) *
2058 nfsrv_sessionhashsize, M_NFSDSESSION, M_WAITOK | M_ZERO);
2059 for (i = 0; i < nfsrv_sessionhashsize; i++) {
2060 mtx_init(&nfssessionhash[i].mtx, "nfssm", NULL, MTX_DEF);
2061 LIST_INIT(&nfssessionhash[i].list);
2063 LIST_INIT(&nfsrv_dontlisthead);
2064 TAILQ_INIT(&nfsrv_recalllisthead);
2066 /* and the v2 pubfh should be all zeros */
2067 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2071 * Check the v4 root exports.
2072 * Return 0 if ok, 1 otherwise.
2075 nfsd_checkrootexp(struct nfsrv_descript *nd)
2078 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2080 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2081 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2083 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2084 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2086 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2087 ND_EXGSS)) == (ND_GSS | ND_EXGSS))
2093 * Parse the first part of an NFSv4 compound to find out what the minor
2097 nfsd_getminorvers(struct nfsrv_descript *nd, u_char *tag, u_char **tagstrp,
2098 int *taglenp, u_int32_t *minversp)
2101 int error = 0, taglen = -1;
2102 u_char *tagstr = NULL;
2104 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2105 taglen = fxdr_unsigned(int, *tl);
2106 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
2110 if (taglen <= NFSV4_SMALLSTR)
2113 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2114 error = nfsrv_mtostr(nd, tagstr, taglen);
2117 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2118 *minversp = fxdr_unsigned(u_int32_t, *tl);
2120 if (*minversp == NFSV41_MINORVERSION)
2121 nd->nd_flag |= ND_NFSV41;
2122 else if (*minversp == NFSV42_MINORVERSION)
2123 nd->nd_flag |= (ND_NFSV41 | ND_NFSV42);
2126 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
2127 free(tagstr, M_TEMP);