2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 * These functions support the macros and help fiddle mbuf chains for
42 * the nfs op functions. They do things like create the rpc header and
43 * copy data between mbuf chains and uio lists.
45 #include <fs/nfs/nfsport.h>
47 extern u_int32_t newnfs_true, newnfs_false;
48 extern int nfs_pubfhset;
49 extern struct nfsclienthashhead *nfsclienthash;
50 extern int nfsrv_clienthashsize;
51 extern struct nfslockhashhead *nfslockhash;
52 extern int nfsrv_lockhashsize;
53 extern struct nfssessionhash *nfssessionhash;
54 extern int nfsrv_sessionhashsize;
55 extern int nfsrv_useacl;
56 extern uid_t nfsrv_defaultuid;
57 extern gid_t nfsrv_defaultgid;
59 char nfs_v2pubfh[NFSX_V2FH];
60 static nfstype newnfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK,
61 NFNON, NFCHR, NFNON };
62 extern nfstype nfsv34_type[9];
63 #endif /* !APPLEKEXT */
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(mbuf_t 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.
1291 count += mbuf_len(m);
1292 if (mbuf_next(m) == NULL)
1296 if (mbuf_len(m) > len) {
1297 mbuf_setlen(m, mbuf_len(m) - len);
1299 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - 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 = mbuf_next(m)) {
1314 if (mbuf_len(m) >= count) {
1315 mbuf_setlen(m, count);
1317 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1318 for (i = 0; i < nul; i++)
1323 count -= mbuf_len(m);
1325 for (m = mbuf_next(m); m; m = mbuf_next(m))
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;
1446 if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1447 if (nd->nd_flag & ND_NFSV4) {
1448 if (len > 0 && len <= NFSX_V4FHMAX) {
1449 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1452 nd->nd_repstat = NFSERR_BADHANDLE;
1466 * For NFSv2, the file handle is always 32 bytes on the
1467 * wire, but this server only cares about the first
1468 * NFSRV_MAXFH bytes.
1471 copylen = NFSRV_MAXFH;
1473 NFSM_DISSECT(tl, u_int32_t *, len);
1474 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1475 nd->nd_procnum == NFSPROC_LOOKUP &&
1476 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1477 nd->nd_flag |= ND_PUBLOOKUP;
1480 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1481 fhp->nfsrvfh_len = copylen;
1483 NFSEXITCODE2(error, nd);
1488 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1489 * numbers not specified for the associated procedure.
1490 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1491 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1492 * RPC procedure is not involved.
1493 * Returns the error number in XDR.
1496 nfsd_errmap(struct nfsrv_descript *nd)
1498 short *defaulterrp, *errp;
1500 if (!nd->nd_repstat)
1502 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1503 if (nd->nd_procnum == NFSPROC_NOOP)
1504 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1505 if (nd->nd_flag & ND_NFSV3)
1506 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1507 else if (nd->nd_repstat == EBADRPC)
1508 return (txdr_unsigned(NFSERR_BADXDR));
1509 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1510 nd->nd_repstat == NFSERR_OPILLEGAL)
1511 return (txdr_unsigned(nd->nd_repstat));
1512 else if ((nd->nd_flag & ND_NFSV41) != 0) {
1513 if (nd->nd_repstat == EOPNOTSUPP)
1514 nd->nd_repstat = NFSERR_NOTSUPP;
1515 nd->nd_repstat = nfsrv_isannfserr(nd->nd_repstat);
1516 return (txdr_unsigned(nd->nd_repstat));
1518 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1520 if (*errp == nd->nd_repstat)
1521 return (txdr_unsigned(nd->nd_repstat));
1522 return (txdr_unsigned(*defaulterrp));
1524 if (nd->nd_repstat <= NFSERR_REMOTE)
1525 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1526 return (txdr_unsigned(NFSERR_IO));
1530 * Check to see if the error is a valid NFS one. If not, replace it with
1534 nfsrv_isannfserr(u_int32_t errval)
1537 if (errval == NFSERR_OK)
1539 if (errval >= NFSERR_BADHANDLE && errval <= NFSERR_DELEGREVOKED)
1541 if (errval > 0 && errval <= NFSERR_REMOTE)
1542 return (nfsrv_v2errmap[errval - 1]);
1547 * Check to see if setting a uid/gid is permitted when creating a new
1548 * file object. (Called when uid and/or gid is specified in the
1549 * settable attributes for V4.
1552 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1557 * If not setting either uid nor gid, it's OK.
1559 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1561 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid == nfsrv_defaultuid &&
1562 enable_nobodycheck == 1)
1563 || (NFSVNO_ISSETGID(nvap) && nvap->na_gid == nfsrv_defaultgid &&
1564 enable_nogroupcheck == 1)) {
1565 error = NFSERR_BADOWNER;
1568 if (nd->nd_cred->cr_uid == 0)
1570 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1571 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1572 !groupmember(nvap->na_gid, nd->nd_cred)))
1573 error = NFSERR_PERM;
1576 NFSEXITCODE2(error, nd);
1581 * and this routine fixes up the settable attributes for V4 if allowed
1582 * by nfsrv_checkuidgid().
1585 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1586 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1587 struct nfsexstuff *exp)
1590 struct nfsvattr nva;
1593 nfsattrbit_t nattrbits;
1596 * Maybe this should be done for V2 and 3 but it never has been
1597 * and nobody seems to be upset, so I think it's best not to change
1598 * the V2 and 3 semantics.
1600 if ((nd->nd_flag & ND_NFSV4) == 0)
1602 NFSVNO_ATTRINIT(&nva);
1603 NFSZERO_ATTRBIT(&nattrbits);
1604 tuid = nd->nd_cred->cr_uid;
1605 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1606 NFSVNO_ISSETUID(nvap) &&
1607 nvap->na_uid != nd->nd_cred->cr_uid) {
1608 if (nd->nd_cred->cr_uid == 0) {
1609 nva.na_uid = nvap->na_uid;
1611 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1613 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1616 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1617 NFSVNO_ISSETATIME(nvap)) {
1618 nva.na_atime = nvap->na_atime;
1620 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1622 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1623 NFSVNO_ISSETMTIME(nvap)) {
1624 nva.na_mtime = nvap->na_mtime;
1626 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1628 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1629 NFSVNO_ISSETGID(nvap)) {
1630 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1631 groupmember(nvap->na_gid, nd->nd_cred)) {
1632 nd->nd_cred->cr_uid = 0;
1633 nva.na_gid = nvap->na_gid;
1635 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1637 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1641 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1643 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1646 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1647 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1648 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1650 #ifdef NFS4_ACL_EXTATTR_NAME
1651 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1652 nfsrv_useacl != 0 && aclp != NULL) {
1653 if (aclp->acl_cnt > 0) {
1654 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1656 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1661 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1662 nd->nd_cred->cr_uid = tuid;
1665 NFSEXITCODE2(0, nd);
1669 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1670 * char isn't a hex digit.
1673 nfsrv_hexdigit(char c, int *err)
1677 if (c >= '0' && c <= '9')
1679 if (c >= 'a' && c <= 'f')
1680 return (c - 'a' + ((char)10));
1681 if (c >= 'A' && c <= 'F')
1682 return (c - 'A' + ((char)10));
1685 return (1); /* BOGUS */
1689 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1693 nfsrv_errmoved(int op)
1697 errp = nfsrv_v4errmap[op];
1698 while (*errp != 0) {
1699 if (*errp == NFSERR_MOVED)
1707 * Fill in attributes for a Referral.
1708 * (Return the number of bytes of XDR created.)
1711 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1712 struct nfsreferral *refp, int getattr, int *reterrp)
1714 u_int32_t *tl, *retnump;
1716 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1717 int fslocationsbit = 0;
1718 nfsattrbit_t tmpbits, refbits;
1720 NFSREFERRAL_ATTRBIT(&refbits);
1722 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1723 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1725 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1729 * Check for the case where unsupported referral attributes are
1732 NFSSET_ATTRBIT(&tmpbits, retbitp);
1733 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1734 if (NFSNONZERO_ATTRBIT(&tmpbits))
1737 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1738 *reterrp = NFSERR_MOVED;
1743 * Now we can fill in the attributes.
1745 NFSSET_ATTRBIT(&tmpbits, retbitp);
1746 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1749 * Put out the attribute bitmap for the ones being filled in
1750 * and get the field for the number of attributes returned.
1752 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1753 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1754 prefixnum += NFSX_UNSIGNED;
1757 * Now, loop around filling in the attributes for each bit set.
1759 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1760 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1762 case NFSATTRBIT_TYPE:
1763 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1764 *tl = txdr_unsigned(NFDIR);
1765 retnum += NFSX_UNSIGNED;
1767 case NFSATTRBIT_FSID:
1768 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1770 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1772 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1773 retnum += NFSX_V4FSID;
1775 case NFSATTRBIT_RDATTRERROR:
1776 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1778 *tl = txdr_unsigned(NFSERR_MOVED);
1781 retnum += NFSX_UNSIGNED;
1783 case NFSATTRBIT_FSLOCATIONS:
1784 retnum += nfsm_strtom(nd, "/", 1);
1785 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1786 *tl = txdr_unsigned(refp->nfr_srvcnt);
1787 retnum += NFSX_UNSIGNED;
1788 cp = refp->nfr_srvlist;
1789 for (i = 0; i < refp->nfr_srvcnt; i++) {
1790 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1791 *tl = txdr_unsigned(1);
1792 retnum += NFSX_UNSIGNED;
1793 cp2 = STRCHR(cp, ':');
1798 retnum += nfsm_strtom(nd, cp, len);
1801 cp2 = STRCHR(cp, ',');
1806 retnum += nfsm_strtom(nd, cp, len);
1811 case NFSATTRBIT_MOUNTEDONFILEID:
1812 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1813 txdr_hyper(refp->nfr_dfileno, tl);
1814 retnum += NFSX_HYPER;
1817 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1821 *retnump = txdr_unsigned(retnum);
1822 return (retnum + prefixnum);
1826 * Parse a file name out of a request.
1829 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1830 NFSPATHLEN_T *outlenp)
1832 char *fromcp, *tocp, val = '\0';
1835 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1844 * For V4, check for lookup parent.
1845 * Otherwise, get the component name.
1847 if ((nd->nd_flag & ND_NFSV4) && nd->nd_procnum == NFSV4OP_LOOKUPP) {
1849 hash += ((u_char)'.');
1851 hash += ((u_char)'.');
1855 * First, get the name length.
1857 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1858 len = fxdr_unsigned(int, *tl);
1859 if (len > NFS_MAXNAMLEN) {
1860 nd->nd_repstat = NFSERR_NAMETOL;
1863 } else if (len <= 0) {
1864 nd->nd_repstat = NFSERR_INVAL;
1870 * Now, copy the component name into the buffer.
1872 fromcp = nd->nd_dpos;
1874 rem = NFSMTOD(md, caddr_t) + mbuf_len(md) - fromcp;
1875 for (i = 0; i < len; i++) {
1882 fromcp = NFSMTOD(md, caddr_t);
1885 if (*fromcp == '\0') {
1886 nd->nd_repstat = EACCES;
1891 * For lookups on the public filehandle, do some special
1892 * processing on the name. (The public file handle is the
1893 * root of the public file system for this server.)
1895 if (nd->nd_flag & ND_PUBLOOKUP) {
1897 * If the first char is ASCII, it is a canonical
1898 * path, otherwise it is a native path. (RFC2054
1899 * doesn't actually state what it is if the first
1900 * char isn't ASCII or 0x80, so I assume native.)
1901 * pubtype == 1 -> native path
1902 * pubtype == 2 -> canonical path
1905 if (*fromcp & 0x80) {
1907 * Since RFC2054 doesn't indicate
1908 * that a native path of just 0x80
1909 * isn't allowed, I'll replace the
1910 * 0x80 with '/' instead of just
1920 * '/' only allowed in a native path
1922 if (*fromcp == '/' && pubtype != 1) {
1923 nd->nd_repstat = EACCES;
1929 * For the special case of 2 hex digits after a
1930 * '%' in an absolute path, calculate the value.
1931 * percent == 1 -> indicates "get first hex digit"
1932 * percent == 2 -> indicates "get second hex digit"
1935 digit = nfsrv_hexdigit(*fromcp, &error);
1937 nd->nd_repstat = EACCES;
1948 hash += ((u_char)val);
1952 if (*fromcp == '%' && pubtype == 2) {
1954 * Must be followed by 2 hex digits
1956 if ((len - i) < 3) {
1957 nd->nd_repstat = EACCES;
1964 hash += ((u_char)*fromcp);
1970 * Normal, non lookup on public, name.
1972 if (*fromcp == '/') {
1973 if (nd->nd_flag & ND_NFSV4)
1974 nd->nd_repstat = NFSERR_BADNAME;
1976 nd->nd_repstat = EACCES;
1980 hash += ((u_char)*fromcp);
1988 nd->nd_dpos = fromcp;
1989 i = NFSM_RNDUP(len) - len;
1994 error = nfsm_advance(nd, i, rem);
2001 * For v4, don't allow lookups of '.' or '..' and
2002 * also check for non-utf8 strings.
2004 if (nd->nd_flag & ND_NFSV4) {
2005 if ((outlen == 1 && bufp[0] == '.') ||
2006 (outlen == 2 && bufp[0] == '.' &&
2008 nd->nd_repstat = NFSERR_BADNAME;
2012 if (enable_checkutf8 == 1 &&
2013 nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
2014 nd->nd_repstat = NFSERR_INVAL;
2021 *outlenp = (size_t)outlen;
2025 NFSEXITCODE2(error, nd);
2033 static int inited = 0;
2040 * Initialize client queues. Don't free/reinitialize
2041 * them when nfsds are restarted.
2043 nfsclienthash = malloc(sizeof(struct nfsclienthashhead) *
2044 nfsrv_clienthashsize, M_NFSDCLIENT, M_WAITOK | M_ZERO);
2045 for (i = 0; i < nfsrv_clienthashsize; i++)
2046 LIST_INIT(&nfsclienthash[i]);
2047 nfslockhash = malloc(sizeof(struct nfslockhashhead) *
2048 nfsrv_lockhashsize, M_NFSDLOCKFILE, M_WAITOK | M_ZERO);
2049 for (i = 0; i < nfsrv_lockhashsize; i++)
2050 LIST_INIT(&nfslockhash[i]);
2051 nfssessionhash = malloc(sizeof(struct nfssessionhash) *
2052 nfsrv_sessionhashsize, M_NFSDSESSION, M_WAITOK | M_ZERO);
2053 for (i = 0; i < nfsrv_sessionhashsize; i++) {
2054 mtx_init(&nfssessionhash[i].mtx, "nfssm", NULL, MTX_DEF);
2055 LIST_INIT(&nfssessionhash[i].list);
2058 /* and the v2 pubfh should be all zeros */
2059 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2063 * Check the v4 root exports.
2064 * Return 0 if ok, 1 otherwise.
2067 nfsd_checkrootexp(struct nfsrv_descript *nd)
2070 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2072 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2073 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2075 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2076 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2078 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2079 ND_EXGSS)) == (ND_GSS | ND_EXGSS))
2085 * Parse the first part of an NFSv4 compound to find out what the minor
2089 nfsd_getminorvers(struct nfsrv_descript *nd, u_char *tag, u_char **tagstrp,
2090 int *taglenp, u_int32_t *minversp)
2093 int error = 0, taglen = -1;
2094 u_char *tagstr = NULL;
2096 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2097 taglen = fxdr_unsigned(int, *tl);
2098 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
2102 if (taglen <= NFSV4_SMALLSTR)
2105 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2106 error = nfsrv_mtostr(nd, tagstr, taglen);
2109 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2110 *minversp = fxdr_unsigned(u_int32_t, *tl);
2112 if (*minversp == NFSV41_MINORVERSION)
2113 nd->nd_flag |= ND_NFSV41;
2116 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
2117 free(tagstr, M_TEMP);