2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
39 * These functions support the macros and help fiddle mbuf chains for
40 * the nfs op functions. They do things like create the rpc header and
41 * copy data between mbuf chains and uio lists.
43 #include <fs/nfs/nfsport.h>
45 extern u_int32_t newnfs_true, newnfs_false;
46 extern int nfs_pubfhset;
47 extern struct nfsclienthashhead nfsclienthash[NFSCLIENTHASHSIZE];
48 extern struct nfslockhashhead nfslockhash[NFSLOCKHASHSIZE];
49 extern struct nfssessionhash nfssessionhash[NFSSESSIONHASHSIZE];
50 extern int nfsrv_useacl;
51 extern uid_t nfsrv_defaultuid;
52 extern gid_t nfsrv_defaultgid;
54 char nfs_v2pubfh[NFSX_V2FH];
55 static nfstype newnfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK,
56 NFNON, NFCHR, NFNON };
57 extern nfstype nfsv34_type[9];
58 #endif /* !APPLEKEXT */
60 static u_int32_t nfsrv_isannfserr(u_int32_t);
62 SYSCTL_DECL(_vfs_nfsd);
64 static int enable_checkutf8 = 1;
65 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_checkutf8, CTLFLAG_RW,
67 "Enable the NFSv4 check for the UTF8 compliant name required by rfc3530");
69 static int enable_nobodycheck = 1;
70 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nobodycheck, CTLFLAG_RW,
71 &enable_nobodycheck, 0,
72 "Enable the NFSv4 check when setting user nobody as owner");
74 static int enable_nogroupcheck = 1;
75 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nogroupcheck, CTLFLAG_RW,
76 &enable_nogroupcheck, 0,
77 "Enable the NFSv4 check when setting group nogroup as owner");
79 static char nfsrv_hexdigit(char, int *);
82 * Maps errno values to nfs error numbers.
83 * Use NFSERR_IO as the catch all for ones not specifically defined in
84 * RFC 1094. (It now includes the errors added for NFSv3.)
86 static u_char nfsrv_v2errmap[NFSERR_REMOTE] = {
87 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
88 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
89 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
90 NFSERR_IO, NFSERR_EXIST, NFSERR_XDEV, NFSERR_NODEV, NFSERR_NOTDIR,
91 NFSERR_ISDIR, NFSERR_INVAL, NFSERR_IO, NFSERR_IO, NFSERR_IO,
92 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
93 NFSERR_MLINK, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
94 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
95 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
96 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
97 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
98 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
99 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
100 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
105 * Maps errno values to nfs error numbers.
106 * Although it is not obvious whether or not NFS clients really care if
107 * a returned error value is in the specified list for the procedure, the
108 * safest thing to do is filter them appropriately. For Version 2, the
109 * X/Open XNFS document is the only specification that defines error values
110 * for each RPC (The RFC simply lists all possible error values for all RPCs),
111 * so I have decided to not do this for Version 2.
112 * The first entry is the default error return and the rest are the valid
113 * errors for that RPC in increasing numeric order.
115 static short nfsv3err_null[] = {
120 static short nfsv3err_getattr[] = {
130 static short nfsv3err_setattr[] = {
147 static short nfsv3err_lookup[] = {
161 static short nfsv3err_access[] = {
171 static short nfsv3err_readlink[] = {
184 static short nfsv3err_read[] = {
197 static short nfsv3err_write[] = {
213 static short nfsv3err_create[] = {
231 static short nfsv3err_mkdir[] = {
249 static short nfsv3err_symlink[] = {
267 static short nfsv3err_mknod[] = {
286 static short nfsv3err_remove[] = {
301 static short nfsv3err_rmdir[] = {
320 static short nfsv3err_rename[] = {
344 static short nfsv3err_link[] = {
365 static short nfsv3err_readdir[] = {
379 static short nfsv3err_readdirplus[] = {
394 static short nfsv3err_fsstat[] = {
404 static short nfsv3err_fsinfo[] = {
413 static short nfsv3err_pathconf[] = {
422 static short nfsv3err_commit[] = {
432 static short *nfsrv_v3errmap[] = {
450 nfsv3err_readdirplus,
458 * And the same for V4.
460 static short nfsv4err_null[] = {
465 static short nfsv4err_access[] = {
482 static short nfsv4err_close[] = {
506 static short nfsv4err_commit[] = {
524 static short nfsv4err_create[] = {
553 static short nfsv4err_delegpurge[] = {
561 NFSERR_STALECLIENTID,
565 static short nfsv4err_delegreturn[] = {
584 static short nfsv4err_getattr[] = {
601 static short nfsv4err_getfh[] = {
613 static short nfsv4err_link[] = {
645 static short nfsv4err_lock[] = {
671 NFSERR_RECLAIMCONFLICT,
675 NFSERR_STALECLIENTID,
680 static short nfsv4err_lockt[] = {
699 NFSERR_STALECLIENTID,
703 static short nfsv4err_locku[] = {
729 static short nfsv4err_lookup[] = {
752 static short nfsv4err_lookupp[] = {
768 static short nfsv4err_nverify[] = {
788 static short nfsv4err_open[] = {
819 NFSERR_RECLAIMCONFLICT,
825 NFSERR_STALECLIENTID,
831 static short nfsv4err_openattr[] = {
852 static short nfsv4err_openconfirm[] = {
873 static short nfsv4err_opendowngrade[] = {
893 static short nfsv4err_putfh[] = {
906 static short nfsv4err_putpubfh[] = {
914 static short nfsv4err_putrootfh[] = {
922 static short nfsv4err_read[] = {
950 static short nfsv4err_readdir[] = {
971 static short nfsv4err_readlink[] = {
989 static short nfsv4err_remove[] = {
1004 NFSERR_NOFILEHANDLE,
1014 static short nfsv4err_rename[] = {
1031 NFSERR_NOFILEHANDLE,
1044 static short nfsv4err_renew[] = {
1047 NFSERR_ADMINREVOKED,
1054 NFSERR_STALECLIENTID,
1058 static short nfsv4err_restorefh[] = {
1071 static short nfsv4err_savefh[] = {
1076 NFSERR_NOFILEHANDLE,
1083 static short nfsv4err_secinfo[] = {
1095 NFSERR_NOFILEHANDLE,
1103 static short nfsv4err_setattr[] = {
1106 NFSERR_ADMINREVOKED,
1124 NFSERR_NOFILEHANDLE,
1133 NFSERR_STALESTATEID,
1137 static short nfsv4err_setclientid[] = {
1147 static short nfsv4err_setclientidconfirm[] = {
1153 NFSERR_STALECLIENTID,
1157 static short nfsv4err_verify[] = {
1168 NFSERR_NOFILEHANDLE,
1176 static short nfsv4err_write[] = {
1179 NFSERR_ADMINREVOKED,
1195 NFSERR_NOFILEHANDLE,
1204 NFSERR_STALESTATEID,
1208 static short nfsv4err_releaselockowner[] = {
1210 NFSERR_ADMINREVOKED,
1217 NFSERR_STALECLIENTID,
1221 static short *nfsrv_v4errmap[] = {
1229 nfsv4err_delegpurge,
1230 nfsv4err_delegreturn,
1242 nfsv4err_openconfirm,
1243 nfsv4err_opendowngrade,
1257 nfsv4err_setclientid,
1258 nfsv4err_setclientidconfirm,
1261 nfsv4err_releaselockowner,
1265 * A fiddled version of m_adj() that ensures null fill to a long
1266 * boundary and only trims off the back end
1269 nfsrv_adj(mbuf_t mp, int len, int nul)
1276 * Trim from tail. Scan the mbuf chain,
1277 * calculating its length and finding the last mbuf.
1278 * If the adjustment only affects this mbuf, then just
1279 * adjust and return. Otherwise, rescan and truncate
1280 * after the remaining size.
1285 count += mbuf_len(m);
1286 if (mbuf_next(m) == NULL)
1290 if (mbuf_len(m) > len) {
1291 mbuf_setlen(m, mbuf_len(m) - len);
1293 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1294 for (i = 0; i < nul; i++)
1303 * Correct length for chain is "count".
1304 * Find the mbuf with last data, adjust its length,
1305 * and toss data from remaining mbufs on chain.
1307 for (m = mp; m; m = mbuf_next(m)) {
1308 if (mbuf_len(m) >= count) {
1309 mbuf_setlen(m, count);
1311 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1312 for (i = 0; i < nul; i++)
1317 count -= mbuf_len(m);
1319 for (m = mbuf_next(m); m; m = mbuf_next(m))
1324 * Make these functions instead of macros, so that the kernel text size
1325 * doesn't get too big...
1328 nfsrv_wcc(struct nfsrv_descript *nd, int before_ret,
1329 struct nfsvattr *before_nvap, int after_ret, struct nfsvattr *after_nvap)
1334 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1337 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1338 *tl++ = newnfs_true;
1339 txdr_hyper(before_nvap->na_size, tl);
1341 txdr_nfsv3time(&(before_nvap->na_mtime), tl);
1343 txdr_nfsv3time(&(before_nvap->na_ctime), tl);
1345 nfsrv_postopattr(nd, after_ret, after_nvap);
1349 nfsrv_postopattr(struct nfsrv_descript *nd, int after_ret,
1350 struct nfsvattr *after_nvap)
1354 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1359 nfsrv_fillattr(nd, after_nvap);
1364 * Fill in file attributes for V2 and 3. For V4, call a separate
1365 * routine that sifts through all the attribute bits.
1368 nfsrv_fillattr(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1370 struct nfs_fattr *fp;
1374 * Build space for the attribute structure.
1376 if (nd->nd_flag & ND_NFSV3)
1377 fattr_size = NFSX_V3FATTR;
1379 fattr_size = NFSX_V2FATTR;
1380 NFSM_BUILD(fp, struct nfs_fattr *, fattr_size);
1383 * Now just fill it all in.
1385 fp->fa_nlink = txdr_unsigned(nvap->na_nlink);
1386 fp->fa_uid = txdr_unsigned(nvap->na_uid);
1387 fp->fa_gid = txdr_unsigned(nvap->na_gid);
1388 if (nd->nd_flag & ND_NFSV3) {
1389 fp->fa_type = vtonfsv34_type(nvap->na_type);
1390 fp->fa_mode = vtonfsv34_mode(nvap->na_mode);
1391 txdr_hyper(nvap->na_size, &fp->fa3_size);
1392 txdr_hyper(nvap->na_bytes, &fp->fa3_used);
1393 fp->fa3_rdev.specdata1 = txdr_unsigned(NFSMAJOR(nvap->na_rdev));
1394 fp->fa3_rdev.specdata2 = txdr_unsigned(NFSMINOR(nvap->na_rdev));
1395 fp->fa3_fsid.nfsuquad[0] = 0;
1396 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(nvap->na_fsid);
1397 fp->fa3_fileid.nfsuquad[0] = 0;
1398 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(nvap->na_fileid);
1399 txdr_nfsv3time(&nvap->na_atime, &fp->fa3_atime);
1400 txdr_nfsv3time(&nvap->na_mtime, &fp->fa3_mtime);
1401 txdr_nfsv3time(&nvap->na_ctime, &fp->fa3_ctime);
1403 fp->fa_type = vtonfsv2_type(nvap->na_type);
1404 fp->fa_mode = vtonfsv2_mode(nvap->na_type, nvap->na_mode);
1405 fp->fa2_size = txdr_unsigned(nvap->na_size);
1406 fp->fa2_blocksize = txdr_unsigned(nvap->na_blocksize);
1407 if (nvap->na_type == VFIFO)
1408 fp->fa2_rdev = 0xffffffff;
1410 fp->fa2_rdev = txdr_unsigned(nvap->na_rdev);
1411 fp->fa2_blocks = txdr_unsigned(nvap->na_bytes / NFS_FABLKSIZE);
1412 fp->fa2_fsid = txdr_unsigned(nvap->na_fsid);
1413 fp->fa2_fileid = txdr_unsigned(nvap->na_fileid);
1414 txdr_nfsv2time(&nvap->na_atime, &fp->fa2_atime);
1415 txdr_nfsv2time(&nvap->na_mtime, &fp->fa2_mtime);
1416 txdr_nfsv2time(&nvap->na_ctime, &fp->fa2_ctime);
1421 * This function gets a file handle out of an mbuf list.
1422 * It returns 0 for success, EBADRPC otherwise.
1423 * If sets the third flagp argument to 1 if the file handle is
1424 * the public file handle.
1425 * For NFSv4, if the length is incorrect, set nd_repstat == NFSERR_BADHANDLE
1428 nfsrv_mtofh(struct nfsrv_descript *nd, struct nfsrvfh *fhp)
1431 int error = 0, len, copylen;
1433 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1434 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1435 len = fxdr_unsigned(int, *tl);
1436 if (len == 0 && nfs_pubfhset && (nd->nd_flag & ND_NFSV3) &&
1437 nd->nd_procnum == NFSPROC_LOOKUP) {
1438 nd->nd_flag |= ND_PUBLOOKUP;
1441 if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1442 if (nd->nd_flag & ND_NFSV4) {
1443 if (len > 0 && len <= NFSX_V4FHMAX) {
1444 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1447 nd->nd_repstat = NFSERR_BADHANDLE;
1461 * For NFSv2, the file handle is always 32 bytes on the
1462 * wire, but this server only cares about the first
1463 * NFSRV_MAXFH bytes.
1466 copylen = NFSRV_MAXFH;
1468 NFSM_DISSECT(tl, u_int32_t *, len);
1469 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1470 nd->nd_procnum == NFSPROC_LOOKUP &&
1471 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1472 nd->nd_flag |= ND_PUBLOOKUP;
1475 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1476 fhp->nfsrvfh_len = copylen;
1478 NFSEXITCODE2(error, nd);
1483 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1484 * numbers not specified for the associated procedure.
1485 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1486 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1487 * RPC procedure is not involved.
1488 * Returns the error number in XDR.
1491 nfsd_errmap(struct nfsrv_descript *nd)
1493 short *defaulterrp, *errp;
1495 if (!nd->nd_repstat)
1497 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1498 if (nd->nd_procnum == NFSPROC_NOOP)
1499 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1500 if (nd->nd_flag & ND_NFSV3)
1501 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1502 else if (nd->nd_repstat == EBADRPC)
1503 return (txdr_unsigned(NFSERR_BADXDR));
1504 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1505 nd->nd_repstat == NFSERR_OPILLEGAL)
1506 return (txdr_unsigned(nd->nd_repstat));
1507 else if ((nd->nd_flag & ND_NFSV41) != 0) {
1508 if (nd->nd_repstat == EOPNOTSUPP)
1509 nd->nd_repstat = NFSERR_NOTSUPP;
1510 nd->nd_repstat = nfsrv_isannfserr(nd->nd_repstat);
1511 return (txdr_unsigned(nd->nd_repstat));
1513 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1515 if (*errp == nd->nd_repstat)
1516 return (txdr_unsigned(nd->nd_repstat));
1517 return (txdr_unsigned(*defaulterrp));
1519 if (nd->nd_repstat <= NFSERR_REMOTE)
1520 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1521 return (txdr_unsigned(NFSERR_IO));
1525 * Check to see if the error is a valid NFS one. If not, replace it with
1529 nfsrv_isannfserr(u_int32_t errval)
1532 if (errval == NFSERR_OK)
1534 if (errval >= NFSERR_BADHANDLE && errval <= NFSERR_DELEGREVOKED)
1536 if (errval > 0 && errval <= NFSERR_REMOTE)
1537 return (nfsrv_v2errmap[errval - 1]);
1542 * Check to see if setting a uid/gid is permitted when creating a new
1543 * file object. (Called when uid and/or gid is specified in the
1544 * settable attributes for V4.
1547 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1552 * If not setting either uid nor gid, it's OK.
1554 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1556 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid == nfsrv_defaultuid &&
1557 enable_nobodycheck == 1)
1558 || (NFSVNO_ISSETGID(nvap) && nvap->na_gid == nfsrv_defaultgid &&
1559 enable_nogroupcheck == 1)) {
1560 error = NFSERR_BADOWNER;
1563 if (nd->nd_cred->cr_uid == 0)
1565 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1566 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1567 !groupmember(nvap->na_gid, nd->nd_cred)))
1568 error = NFSERR_PERM;
1571 NFSEXITCODE2(error, nd);
1576 * and this routine fixes up the settable attributes for V4 if allowed
1577 * by nfsrv_checkuidgid().
1580 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1581 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1582 struct nfsexstuff *exp)
1585 struct nfsvattr nva;
1588 nfsattrbit_t nattrbits;
1591 * Maybe this should be done for V2 and 3 but it never has been
1592 * and nobody seems to be upset, so I think it's best not to change
1593 * the V2 and 3 semantics.
1595 if ((nd->nd_flag & ND_NFSV4) == 0)
1597 NFSVNO_ATTRINIT(&nva);
1598 NFSZERO_ATTRBIT(&nattrbits);
1599 tuid = nd->nd_cred->cr_uid;
1600 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1601 NFSVNO_ISSETUID(nvap) &&
1602 nvap->na_uid != nd->nd_cred->cr_uid) {
1603 if (nd->nd_cred->cr_uid == 0) {
1604 nva.na_uid = nvap->na_uid;
1606 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1608 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1611 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1612 NFSVNO_ISSETATIME(nvap)) {
1613 nva.na_atime = nvap->na_atime;
1615 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1617 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1618 NFSVNO_ISSETMTIME(nvap)) {
1619 nva.na_mtime = nvap->na_mtime;
1621 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1623 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1624 NFSVNO_ISSETGID(nvap)) {
1625 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1626 groupmember(nvap->na_gid, nd->nd_cred)) {
1627 nd->nd_cred->cr_uid = 0;
1628 nva.na_gid = nvap->na_gid;
1630 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1632 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1636 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1638 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1641 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1642 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1643 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1645 #ifdef NFS4_ACL_EXTATTR_NAME
1646 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1647 nfsrv_useacl != 0 && aclp != NULL) {
1648 if (aclp->acl_cnt > 0) {
1649 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1651 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1656 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1657 nd->nd_cred->cr_uid = tuid;
1660 NFSEXITCODE2(0, nd);
1664 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1665 * char isn't a hex digit.
1668 nfsrv_hexdigit(char c, int *err)
1672 if (c >= '0' && c <= '9')
1674 if (c >= 'a' && c <= 'f')
1675 return (c - 'a' + ((char)10));
1676 if (c >= 'A' && c <= 'F')
1677 return (c - 'A' + ((char)10));
1680 return (1); /* BOGUS */
1684 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1688 nfsrv_errmoved(int op)
1692 errp = nfsrv_v4errmap[op];
1693 while (*errp != 0) {
1694 if (*errp == NFSERR_MOVED)
1702 * Fill in attributes for a Referral.
1703 * (Return the number of bytes of XDR created.)
1706 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1707 struct nfsreferral *refp, int getattr, int *reterrp)
1709 u_int32_t *tl, *retnump;
1711 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1712 int fslocationsbit = 0;
1713 nfsattrbit_t tmpbits, refbits;
1715 NFSREFERRAL_ATTRBIT(&refbits);
1717 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1718 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1720 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1724 * Check for the case where unsupported referral attributes are
1727 NFSSET_ATTRBIT(&tmpbits, retbitp);
1728 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1729 if (NFSNONZERO_ATTRBIT(&tmpbits))
1732 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1733 *reterrp = NFSERR_MOVED;
1738 * Now we can fill in the attributes.
1740 NFSSET_ATTRBIT(&tmpbits, retbitp);
1741 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1744 * Put out the attribute bitmap for the ones being filled in
1745 * and get the field for the number of attributes returned.
1747 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1748 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1749 prefixnum += NFSX_UNSIGNED;
1752 * Now, loop around filling in the attributes for each bit set.
1754 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1755 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1757 case NFSATTRBIT_TYPE:
1758 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1759 *tl = txdr_unsigned(NFDIR);
1760 retnum += NFSX_UNSIGNED;
1762 case NFSATTRBIT_FSID:
1763 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1765 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1767 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1768 retnum += NFSX_V4FSID;
1770 case NFSATTRBIT_RDATTRERROR:
1771 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1773 *tl = txdr_unsigned(NFSERR_MOVED);
1776 retnum += NFSX_UNSIGNED;
1778 case NFSATTRBIT_FSLOCATIONS:
1779 retnum += nfsm_strtom(nd, "/", 1);
1780 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1781 *tl = txdr_unsigned(refp->nfr_srvcnt);
1782 retnum += NFSX_UNSIGNED;
1783 cp = refp->nfr_srvlist;
1784 for (i = 0; i < refp->nfr_srvcnt; i++) {
1785 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1786 *tl = txdr_unsigned(1);
1787 retnum += NFSX_UNSIGNED;
1788 cp2 = STRCHR(cp, ':');
1793 retnum += nfsm_strtom(nd, cp, len);
1796 cp2 = STRCHR(cp, ',');
1801 retnum += nfsm_strtom(nd, cp, len);
1806 case NFSATTRBIT_MOUNTEDONFILEID:
1807 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1809 *tl = txdr_unsigned(refp->nfr_dfileno);
1810 retnum += NFSX_HYPER;
1813 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1817 *retnump = txdr_unsigned(retnum);
1818 return (retnum + prefixnum);
1822 * Parse a file name out of a request.
1825 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1826 NFSPATHLEN_T *outlenp)
1828 char *fromcp, *tocp, val = '\0';
1831 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1840 * For V4, check for lookup parent.
1841 * Otherwise, get the component name.
1843 if ((nd->nd_flag & ND_NFSV4) && nd->nd_procnum == NFSV4OP_LOOKUPP) {
1845 hash += ((u_char)'.');
1847 hash += ((u_char)'.');
1851 * First, get the name length.
1853 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1854 len = fxdr_unsigned(int, *tl);
1855 if (len > NFS_MAXNAMLEN) {
1856 nd->nd_repstat = NFSERR_NAMETOL;
1859 } else if (len <= 0) {
1860 nd->nd_repstat = NFSERR_INVAL;
1866 * Now, copy the component name into the buffer.
1868 fromcp = nd->nd_dpos;
1870 rem = NFSMTOD(md, caddr_t) + mbuf_len(md) - fromcp;
1871 for (i = 0; i < len; i++) {
1878 fromcp = NFSMTOD(md, caddr_t);
1881 if (*fromcp == '\0') {
1882 nd->nd_repstat = EACCES;
1887 * For lookups on the public filehandle, do some special
1888 * processing on the name. (The public file handle is the
1889 * root of the public file system for this server.)
1891 if (nd->nd_flag & ND_PUBLOOKUP) {
1893 * If the first char is ASCII, it is a canonical
1894 * path, otherwise it is a native path. (RFC2054
1895 * doesn't actually state what it is if the first
1896 * char isn't ASCII or 0x80, so I assume native.)
1897 * pubtype == 1 -> native path
1898 * pubtype == 2 -> canonical path
1901 if (*fromcp & 0x80) {
1903 * Since RFC2054 doesn't indicate
1904 * that a native path of just 0x80
1905 * isn't allowed, I'll replace the
1906 * 0x80 with '/' instead of just
1916 * '/' only allowed in a native path
1918 if (*fromcp == '/' && pubtype != 1) {
1919 nd->nd_repstat = EACCES;
1925 * For the special case of 2 hex digits after a
1926 * '%' in an absolute path, calculate the value.
1927 * percent == 1 -> indicates "get first hex digit"
1928 * percent == 2 -> indicates "get second hex digit"
1931 digit = nfsrv_hexdigit(*fromcp, &error);
1933 nd->nd_repstat = EACCES;
1944 hash += ((u_char)val);
1948 if (*fromcp == '%' && pubtype == 2) {
1950 * Must be followed by 2 hex digits
1952 if ((len - i) < 3) {
1953 nd->nd_repstat = EACCES;
1960 hash += ((u_char)*fromcp);
1966 * Normal, non lookup on public, name.
1968 if (*fromcp == '/') {
1969 if (nd->nd_flag & ND_NFSV4)
1970 nd->nd_repstat = NFSERR_BADNAME;
1972 nd->nd_repstat = EACCES;
1976 hash += ((u_char)*fromcp);
1984 nd->nd_dpos = fromcp;
1985 i = NFSM_RNDUP(len) - len;
1990 error = nfsm_advance(nd, i, rem);
1997 * For v4, don't allow lookups of '.' or '..' and
1998 * also check for non-utf8 strings.
2000 if (nd->nd_flag & ND_NFSV4) {
2001 if ((outlen == 1 && bufp[0] == '.') ||
2002 (outlen == 2 && bufp[0] == '.' &&
2004 nd->nd_repstat = NFSERR_BADNAME;
2008 if (enable_checkutf8 == 1 &&
2009 nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
2010 nd->nd_repstat = NFSERR_INVAL;
2017 *outlenp = (size_t)outlen;
2021 NFSEXITCODE2(error, nd);
2029 static int inited = 0;
2036 * Initialize client queues. Don't free/reinitialize
2037 * them when nfsds are restarted.
2039 for (i = 0; i < NFSCLIENTHASHSIZE; i++)
2040 LIST_INIT(&nfsclienthash[i]);
2041 for (i = 0; i < NFSLOCKHASHSIZE; i++)
2042 LIST_INIT(&nfslockhash[i]);
2043 for (i = 0; i < NFSSESSIONHASHSIZE; i++)
2044 LIST_INIT(&nfssessionhash[i].list);
2046 /* and the v2 pubfh should be all zeros */
2047 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2051 * Check the v4 root exports.
2052 * Return 0 if ok, 1 otherwise.
2055 nfsd_checkrootexp(struct nfsrv_descript *nd)
2058 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2060 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2061 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2063 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2064 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2066 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2067 ND_EXGSS)) == (ND_GSS | ND_EXGSS))
2073 * Parse the first part of an NFSv4 compound to find out what the minor
2077 nfsd_getminorvers(struct nfsrv_descript *nd, u_char *tag, u_char **tagstrp,
2078 int *taglenp, u_int32_t *minversp)
2081 int error = 0, taglen = -1;
2082 u_char *tagstr = NULL;
2084 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2085 taglen = fxdr_unsigned(int, *tl);
2086 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
2090 if (taglen <= NFSV4_SMALLSTR)
2093 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2094 error = nfsrv_mtostr(nd, tagstr, taglen);
2097 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2098 *minversp = fxdr_unsigned(u_int32_t, *tl);
2100 if (*minversp == NFSV41_MINORVERSION)
2101 nd->nd_flag |= ND_NFSV41;
2104 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
2105 free(tagstr, M_TEMP);