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;
48 extern int nfsrv_clienthashsize;
49 extern struct nfslockhashhead *nfslockhash;
50 extern int nfsrv_lockhashsize;
51 extern struct nfssessionhash *nfssessionhash;
52 extern int nfsrv_sessionhashsize;
53 extern int nfsrv_useacl;
54 extern uid_t nfsrv_defaultuid;
55 extern gid_t nfsrv_defaultgid;
57 char nfs_v2pubfh[NFSX_V2FH];
58 static nfstype newnfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK,
59 NFNON, NFCHR, NFNON };
60 extern nfstype nfsv34_type[9];
61 #endif /* !APPLEKEXT */
63 static u_int32_t nfsrv_isannfserr(u_int32_t);
65 SYSCTL_DECL(_vfs_nfsd);
67 static int enable_checkutf8 = 1;
68 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_checkutf8, CTLFLAG_RW,
70 "Enable the NFSv4 check for the UTF8 compliant name required by rfc3530");
72 static int enable_nobodycheck = 1;
73 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nobodycheck, CTLFLAG_RW,
74 &enable_nobodycheck, 0,
75 "Enable the NFSv4 check when setting user nobody as owner");
77 static int enable_nogroupcheck = 1;
78 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_nogroupcheck, CTLFLAG_RW,
79 &enable_nogroupcheck, 0,
80 "Enable the NFSv4 check when setting group nogroup as owner");
82 static char nfsrv_hexdigit(char, int *);
85 * Maps errno values to nfs error numbers.
86 * Use NFSERR_IO as the catch all for ones not specifically defined in
87 * RFC 1094. (It now includes the errors added for NFSv3.)
89 static u_char nfsrv_v2errmap[NFSERR_REMOTE] = {
90 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
91 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
92 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
93 NFSERR_IO, NFSERR_EXIST, NFSERR_XDEV, NFSERR_NODEV, NFSERR_NOTDIR,
94 NFSERR_ISDIR, NFSERR_INVAL, NFSERR_IO, NFSERR_IO, NFSERR_IO,
95 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
96 NFSERR_MLINK, 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_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_NAMETOL, NFSERR_IO, NFSERR_IO,
103 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
108 * Maps errno values to nfs error numbers.
109 * Although it is not obvious whether or not NFS clients really care if
110 * a returned error value is in the specified list for the procedure, the
111 * safest thing to do is filter them appropriately. For Version 2, the
112 * X/Open XNFS document is the only specification that defines error values
113 * for each RPC (The RFC simply lists all possible error values for all RPCs),
114 * so I have decided to not do this for Version 2.
115 * The first entry is the default error return and the rest are the valid
116 * errors for that RPC in increasing numeric order.
118 static short nfsv3err_null[] = {
123 static short nfsv3err_getattr[] = {
133 static short nfsv3err_setattr[] = {
150 static short nfsv3err_lookup[] = {
164 static short nfsv3err_access[] = {
174 static short nfsv3err_readlink[] = {
187 static short nfsv3err_read[] = {
200 static short nfsv3err_write[] = {
216 static short nfsv3err_create[] = {
234 static short nfsv3err_mkdir[] = {
252 static short nfsv3err_symlink[] = {
270 static short nfsv3err_mknod[] = {
289 static short nfsv3err_remove[] = {
304 static short nfsv3err_rmdir[] = {
323 static short nfsv3err_rename[] = {
347 static short nfsv3err_link[] = {
368 static short nfsv3err_readdir[] = {
382 static short nfsv3err_readdirplus[] = {
397 static short nfsv3err_fsstat[] = {
407 static short nfsv3err_fsinfo[] = {
416 static short nfsv3err_pathconf[] = {
425 static short nfsv3err_commit[] = {
435 static short *nfsrv_v3errmap[] = {
453 nfsv3err_readdirplus,
461 * And the same for V4.
463 static short nfsv4err_null[] = {
468 static short nfsv4err_access[] = {
485 static short nfsv4err_close[] = {
509 static short nfsv4err_commit[] = {
527 static short nfsv4err_create[] = {
556 static short nfsv4err_delegpurge[] = {
564 NFSERR_STALECLIENTID,
568 static short nfsv4err_delegreturn[] = {
587 static short nfsv4err_getattr[] = {
604 static short nfsv4err_getfh[] = {
616 static short nfsv4err_link[] = {
648 static short nfsv4err_lock[] = {
674 NFSERR_RECLAIMCONFLICT,
678 NFSERR_STALECLIENTID,
683 static short nfsv4err_lockt[] = {
702 NFSERR_STALECLIENTID,
706 static short nfsv4err_locku[] = {
732 static short nfsv4err_lookup[] = {
755 static short nfsv4err_lookupp[] = {
771 static short nfsv4err_nverify[] = {
791 static short nfsv4err_open[] = {
822 NFSERR_RECLAIMCONFLICT,
828 NFSERR_STALECLIENTID,
834 static short nfsv4err_openattr[] = {
855 static short nfsv4err_openconfirm[] = {
876 static short nfsv4err_opendowngrade[] = {
896 static short nfsv4err_putfh[] = {
909 static short nfsv4err_putpubfh[] = {
917 static short nfsv4err_putrootfh[] = {
925 static short nfsv4err_read[] = {
953 static short nfsv4err_readdir[] = {
974 static short nfsv4err_readlink[] = {
992 static short nfsv4err_remove[] = {
1007 NFSERR_NOFILEHANDLE,
1017 static short nfsv4err_rename[] = {
1034 NFSERR_NOFILEHANDLE,
1047 static short nfsv4err_renew[] = {
1050 NFSERR_ADMINREVOKED,
1057 NFSERR_STALECLIENTID,
1061 static short nfsv4err_restorefh[] = {
1074 static short nfsv4err_savefh[] = {
1079 NFSERR_NOFILEHANDLE,
1086 static short nfsv4err_secinfo[] = {
1098 NFSERR_NOFILEHANDLE,
1106 static short nfsv4err_setattr[] = {
1109 NFSERR_ADMINREVOKED,
1127 NFSERR_NOFILEHANDLE,
1136 NFSERR_STALESTATEID,
1140 static short nfsv4err_setclientid[] = {
1150 static short nfsv4err_setclientidconfirm[] = {
1156 NFSERR_STALECLIENTID,
1160 static short nfsv4err_verify[] = {
1171 NFSERR_NOFILEHANDLE,
1179 static short nfsv4err_write[] = {
1182 NFSERR_ADMINREVOKED,
1198 NFSERR_NOFILEHANDLE,
1207 NFSERR_STALESTATEID,
1211 static short nfsv4err_releaselockowner[] = {
1213 NFSERR_ADMINREVOKED,
1220 NFSERR_STALECLIENTID,
1224 static short *nfsrv_v4errmap[] = {
1232 nfsv4err_delegpurge,
1233 nfsv4err_delegreturn,
1245 nfsv4err_openconfirm,
1246 nfsv4err_opendowngrade,
1260 nfsv4err_setclientid,
1261 nfsv4err_setclientidconfirm,
1264 nfsv4err_releaselockowner,
1268 * A fiddled version of m_adj() that ensures null fill to a long
1269 * boundary and only trims off the back end
1272 nfsrv_adj(mbuf_t mp, int len, int nul)
1279 * Trim from tail. Scan the mbuf chain,
1280 * calculating its length and finding the last mbuf.
1281 * If the adjustment only affects this mbuf, then just
1282 * adjust and return. Otherwise, rescan and truncate
1283 * after the remaining size.
1288 count += mbuf_len(m);
1289 if (mbuf_next(m) == NULL)
1293 if (mbuf_len(m) > len) {
1294 mbuf_setlen(m, mbuf_len(m) - len);
1296 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1297 for (i = 0; i < nul; i++)
1306 * Correct length for chain is "count".
1307 * Find the mbuf with last data, adjust its length,
1308 * and toss data from remaining mbufs on chain.
1310 for (m = mp; m; m = mbuf_next(m)) {
1311 if (mbuf_len(m) >= count) {
1312 mbuf_setlen(m, count);
1314 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1315 for (i = 0; i < nul; i++)
1320 count -= mbuf_len(m);
1322 for (m = mbuf_next(m); m; m = mbuf_next(m))
1327 * Make these functions instead of macros, so that the kernel text size
1328 * doesn't get too big...
1331 nfsrv_wcc(struct nfsrv_descript *nd, int before_ret,
1332 struct nfsvattr *before_nvap, int after_ret, struct nfsvattr *after_nvap)
1337 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1340 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1341 *tl++ = newnfs_true;
1342 txdr_hyper(before_nvap->na_size, tl);
1344 txdr_nfsv3time(&(before_nvap->na_mtime), tl);
1346 txdr_nfsv3time(&(before_nvap->na_ctime), tl);
1348 nfsrv_postopattr(nd, after_ret, after_nvap);
1352 nfsrv_postopattr(struct nfsrv_descript *nd, int after_ret,
1353 struct nfsvattr *after_nvap)
1357 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1362 nfsrv_fillattr(nd, after_nvap);
1367 * Fill in file attributes for V2 and 3. For V4, call a separate
1368 * routine that sifts through all the attribute bits.
1371 nfsrv_fillattr(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1373 struct nfs_fattr *fp;
1377 * Build space for the attribute structure.
1379 if (nd->nd_flag & ND_NFSV3)
1380 fattr_size = NFSX_V3FATTR;
1382 fattr_size = NFSX_V2FATTR;
1383 NFSM_BUILD(fp, struct nfs_fattr *, fattr_size);
1386 * Now just fill it all in.
1388 fp->fa_nlink = txdr_unsigned(nvap->na_nlink);
1389 fp->fa_uid = txdr_unsigned(nvap->na_uid);
1390 fp->fa_gid = txdr_unsigned(nvap->na_gid);
1391 if (nd->nd_flag & ND_NFSV3) {
1392 fp->fa_type = vtonfsv34_type(nvap->na_type);
1393 fp->fa_mode = vtonfsv34_mode(nvap->na_mode);
1394 txdr_hyper(nvap->na_size, &fp->fa3_size);
1395 txdr_hyper(nvap->na_bytes, &fp->fa3_used);
1396 fp->fa3_rdev.specdata1 = txdr_unsigned(NFSMAJOR(nvap->na_rdev));
1397 fp->fa3_rdev.specdata2 = txdr_unsigned(NFSMINOR(nvap->na_rdev));
1398 fp->fa3_fsid.nfsuquad[0] = 0;
1399 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(nvap->na_fsid);
1400 fp->fa3_fileid.nfsuquad[0] = 0;
1401 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(nvap->na_fileid);
1402 txdr_nfsv3time(&nvap->na_atime, &fp->fa3_atime);
1403 txdr_nfsv3time(&nvap->na_mtime, &fp->fa3_mtime);
1404 txdr_nfsv3time(&nvap->na_ctime, &fp->fa3_ctime);
1406 fp->fa_type = vtonfsv2_type(nvap->na_type);
1407 fp->fa_mode = vtonfsv2_mode(nvap->na_type, nvap->na_mode);
1408 fp->fa2_size = txdr_unsigned(nvap->na_size);
1409 fp->fa2_blocksize = txdr_unsigned(nvap->na_blocksize);
1410 if (nvap->na_type == VFIFO)
1411 fp->fa2_rdev = 0xffffffff;
1413 fp->fa2_rdev = txdr_unsigned(nvap->na_rdev);
1414 fp->fa2_blocks = txdr_unsigned(nvap->na_bytes / NFS_FABLKSIZE);
1415 fp->fa2_fsid = txdr_unsigned(nvap->na_fsid);
1416 fp->fa2_fileid = txdr_unsigned(nvap->na_fileid);
1417 txdr_nfsv2time(&nvap->na_atime, &fp->fa2_atime);
1418 txdr_nfsv2time(&nvap->na_mtime, &fp->fa2_mtime);
1419 txdr_nfsv2time(&nvap->na_ctime, &fp->fa2_ctime);
1424 * This function gets a file handle out of an mbuf list.
1425 * It returns 0 for success, EBADRPC otherwise.
1426 * If sets the third flagp argument to 1 if the file handle is
1427 * the public file handle.
1428 * For NFSv4, if the length is incorrect, set nd_repstat == NFSERR_BADHANDLE
1431 nfsrv_mtofh(struct nfsrv_descript *nd, struct nfsrvfh *fhp)
1434 int error = 0, len, copylen;
1436 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1437 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1438 len = fxdr_unsigned(int, *tl);
1439 if (len == 0 && nfs_pubfhset && (nd->nd_flag & ND_NFSV3) &&
1440 nd->nd_procnum == NFSPROC_LOOKUP) {
1441 nd->nd_flag |= ND_PUBLOOKUP;
1444 if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1445 if (nd->nd_flag & ND_NFSV4) {
1446 if (len > 0 && len <= NFSX_V4FHMAX) {
1447 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1450 nd->nd_repstat = NFSERR_BADHANDLE;
1464 * For NFSv2, the file handle is always 32 bytes on the
1465 * wire, but this server only cares about the first
1466 * NFSRV_MAXFH bytes.
1469 copylen = NFSRV_MAXFH;
1471 NFSM_DISSECT(tl, u_int32_t *, len);
1472 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1473 nd->nd_procnum == NFSPROC_LOOKUP &&
1474 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1475 nd->nd_flag |= ND_PUBLOOKUP;
1478 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1479 fhp->nfsrvfh_len = copylen;
1481 NFSEXITCODE2(error, nd);
1486 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1487 * numbers not specified for the associated procedure.
1488 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1489 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1490 * RPC procedure is not involved.
1491 * Returns the error number in XDR.
1494 nfsd_errmap(struct nfsrv_descript *nd)
1496 short *defaulterrp, *errp;
1498 if (!nd->nd_repstat)
1500 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1501 if (nd->nd_procnum == NFSPROC_NOOP)
1502 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1503 if (nd->nd_flag & ND_NFSV3)
1504 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1505 else if (nd->nd_repstat == EBADRPC)
1506 return (txdr_unsigned(NFSERR_BADXDR));
1507 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1508 nd->nd_repstat == NFSERR_OPILLEGAL)
1509 return (txdr_unsigned(nd->nd_repstat));
1510 else if ((nd->nd_flag & ND_NFSV41) != 0) {
1511 if (nd->nd_repstat == EOPNOTSUPP)
1512 nd->nd_repstat = NFSERR_NOTSUPP;
1513 nd->nd_repstat = nfsrv_isannfserr(nd->nd_repstat);
1514 return (txdr_unsigned(nd->nd_repstat));
1516 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1518 if (*errp == nd->nd_repstat)
1519 return (txdr_unsigned(nd->nd_repstat));
1520 return (txdr_unsigned(*defaulterrp));
1522 if (nd->nd_repstat <= NFSERR_REMOTE)
1523 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1524 return (txdr_unsigned(NFSERR_IO));
1528 * Check to see if the error is a valid NFS one. If not, replace it with
1532 nfsrv_isannfserr(u_int32_t errval)
1535 if (errval == NFSERR_OK)
1537 if (errval >= NFSERR_BADHANDLE && errval <= NFSERR_DELEGREVOKED)
1539 if (errval > 0 && errval <= NFSERR_REMOTE)
1540 return (nfsrv_v2errmap[errval - 1]);
1545 * Check to see if setting a uid/gid is permitted when creating a new
1546 * file object. (Called when uid and/or gid is specified in the
1547 * settable attributes for V4.
1550 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1555 * If not setting either uid nor gid, it's OK.
1557 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1559 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid == nfsrv_defaultuid &&
1560 enable_nobodycheck == 1)
1561 || (NFSVNO_ISSETGID(nvap) && nvap->na_gid == nfsrv_defaultgid &&
1562 enable_nogroupcheck == 1)) {
1563 error = NFSERR_BADOWNER;
1566 if (nd->nd_cred->cr_uid == 0)
1568 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1569 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1570 !groupmember(nvap->na_gid, nd->nd_cred)))
1571 error = NFSERR_PERM;
1574 NFSEXITCODE2(error, nd);
1579 * and this routine fixes up the settable attributes for V4 if allowed
1580 * by nfsrv_checkuidgid().
1583 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1584 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1585 struct nfsexstuff *exp)
1588 struct nfsvattr nva;
1591 nfsattrbit_t nattrbits;
1594 * Maybe this should be done for V2 and 3 but it never has been
1595 * and nobody seems to be upset, so I think it's best not to change
1596 * the V2 and 3 semantics.
1598 if ((nd->nd_flag & ND_NFSV4) == 0)
1600 NFSVNO_ATTRINIT(&nva);
1601 NFSZERO_ATTRBIT(&nattrbits);
1602 tuid = nd->nd_cred->cr_uid;
1603 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1604 NFSVNO_ISSETUID(nvap) &&
1605 nvap->na_uid != nd->nd_cred->cr_uid) {
1606 if (nd->nd_cred->cr_uid == 0) {
1607 nva.na_uid = nvap->na_uid;
1609 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1611 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1614 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1615 NFSVNO_ISSETATIME(nvap)) {
1616 nva.na_atime = nvap->na_atime;
1618 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1620 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1621 NFSVNO_ISSETMTIME(nvap)) {
1622 nva.na_mtime = nvap->na_mtime;
1624 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1626 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1627 NFSVNO_ISSETGID(nvap)) {
1628 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1629 groupmember(nvap->na_gid, nd->nd_cred)) {
1630 nd->nd_cred->cr_uid = 0;
1631 nva.na_gid = nvap->na_gid;
1633 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1635 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1639 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1641 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1644 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1645 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1646 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1648 #ifdef NFS4_ACL_EXTATTR_NAME
1649 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1650 nfsrv_useacl != 0 && aclp != NULL) {
1651 if (aclp->acl_cnt > 0) {
1652 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1654 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1659 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1660 nd->nd_cred->cr_uid = tuid;
1663 NFSEXITCODE2(0, nd);
1667 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1668 * char isn't a hex digit.
1671 nfsrv_hexdigit(char c, int *err)
1675 if (c >= '0' && c <= '9')
1677 if (c >= 'a' && c <= 'f')
1678 return (c - 'a' + ((char)10));
1679 if (c >= 'A' && c <= 'F')
1680 return (c - 'A' + ((char)10));
1683 return (1); /* BOGUS */
1687 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1691 nfsrv_errmoved(int op)
1695 errp = nfsrv_v4errmap[op];
1696 while (*errp != 0) {
1697 if (*errp == NFSERR_MOVED)
1705 * Fill in attributes for a Referral.
1706 * (Return the number of bytes of XDR created.)
1709 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1710 struct nfsreferral *refp, int getattr, int *reterrp)
1712 u_int32_t *tl, *retnump;
1714 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1715 int fslocationsbit = 0;
1716 nfsattrbit_t tmpbits, refbits;
1718 NFSREFERRAL_ATTRBIT(&refbits);
1720 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1721 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1723 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1727 * Check for the case where unsupported referral attributes are
1730 NFSSET_ATTRBIT(&tmpbits, retbitp);
1731 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1732 if (NFSNONZERO_ATTRBIT(&tmpbits))
1735 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1736 *reterrp = NFSERR_MOVED;
1741 * Now we can fill in the attributes.
1743 NFSSET_ATTRBIT(&tmpbits, retbitp);
1744 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1747 * Put out the attribute bitmap for the ones being filled in
1748 * and get the field for the number of attributes returned.
1750 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1751 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1752 prefixnum += NFSX_UNSIGNED;
1755 * Now, loop around filling in the attributes for each bit set.
1757 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1758 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1760 case NFSATTRBIT_TYPE:
1761 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1762 *tl = txdr_unsigned(NFDIR);
1763 retnum += NFSX_UNSIGNED;
1765 case NFSATTRBIT_FSID:
1766 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1768 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1770 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1771 retnum += NFSX_V4FSID;
1773 case NFSATTRBIT_RDATTRERROR:
1774 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1776 *tl = txdr_unsigned(NFSERR_MOVED);
1779 retnum += NFSX_UNSIGNED;
1781 case NFSATTRBIT_FSLOCATIONS:
1782 retnum += nfsm_strtom(nd, "/", 1);
1783 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1784 *tl = txdr_unsigned(refp->nfr_srvcnt);
1785 retnum += NFSX_UNSIGNED;
1786 cp = refp->nfr_srvlist;
1787 for (i = 0; i < refp->nfr_srvcnt; i++) {
1788 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1789 *tl = txdr_unsigned(1);
1790 retnum += NFSX_UNSIGNED;
1791 cp2 = STRCHR(cp, ':');
1796 retnum += nfsm_strtom(nd, cp, len);
1799 cp2 = STRCHR(cp, ',');
1804 retnum += nfsm_strtom(nd, cp, len);
1809 case NFSATTRBIT_MOUNTEDONFILEID:
1810 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1812 *tl = txdr_unsigned(refp->nfr_dfileno);
1813 retnum += NFSX_HYPER;
1816 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1820 *retnump = txdr_unsigned(retnum);
1821 return (retnum + prefixnum);
1825 * Parse a file name out of a request.
1828 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1829 NFSPATHLEN_T *outlenp)
1831 char *fromcp, *tocp, val = '\0';
1834 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1843 * For V4, check for lookup parent.
1844 * Otherwise, get the component name.
1846 if ((nd->nd_flag & ND_NFSV4) && nd->nd_procnum == NFSV4OP_LOOKUPP) {
1848 hash += ((u_char)'.');
1850 hash += ((u_char)'.');
1854 * First, get the name length.
1856 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1857 len = fxdr_unsigned(int, *tl);
1858 if (len > NFS_MAXNAMLEN) {
1859 nd->nd_repstat = NFSERR_NAMETOL;
1862 } else if (len <= 0) {
1863 nd->nd_repstat = NFSERR_INVAL;
1869 * Now, copy the component name into the buffer.
1871 fromcp = nd->nd_dpos;
1873 rem = NFSMTOD(md, caddr_t) + mbuf_len(md) - fromcp;
1874 for (i = 0; i < len; i++) {
1881 fromcp = NFSMTOD(md, caddr_t);
1884 if (*fromcp == '\0') {
1885 nd->nd_repstat = EACCES;
1890 * For lookups on the public filehandle, do some special
1891 * processing on the name. (The public file handle is the
1892 * root of the public file system for this server.)
1894 if (nd->nd_flag & ND_PUBLOOKUP) {
1896 * If the first char is ASCII, it is a canonical
1897 * path, otherwise it is a native path. (RFC2054
1898 * doesn't actually state what it is if the first
1899 * char isn't ASCII or 0x80, so I assume native.)
1900 * pubtype == 1 -> native path
1901 * pubtype == 2 -> canonical path
1904 if (*fromcp & 0x80) {
1906 * Since RFC2054 doesn't indicate
1907 * that a native path of just 0x80
1908 * isn't allowed, I'll replace the
1909 * 0x80 with '/' instead of just
1919 * '/' only allowed in a native path
1921 if (*fromcp == '/' && pubtype != 1) {
1922 nd->nd_repstat = EACCES;
1928 * For the special case of 2 hex digits after a
1929 * '%' in an absolute path, calculate the value.
1930 * percent == 1 -> indicates "get first hex digit"
1931 * percent == 2 -> indicates "get second hex digit"
1934 digit = nfsrv_hexdigit(*fromcp, &error);
1936 nd->nd_repstat = EACCES;
1947 hash += ((u_char)val);
1951 if (*fromcp == '%' && pubtype == 2) {
1953 * Must be followed by 2 hex digits
1955 if ((len - i) < 3) {
1956 nd->nd_repstat = EACCES;
1963 hash += ((u_char)*fromcp);
1969 * Normal, non lookup on public, name.
1971 if (*fromcp == '/') {
1972 if (nd->nd_flag & ND_NFSV4)
1973 nd->nd_repstat = NFSERR_BADNAME;
1975 nd->nd_repstat = EACCES;
1979 hash += ((u_char)*fromcp);
1987 nd->nd_dpos = fromcp;
1988 i = NFSM_RNDUP(len) - len;
1993 error = nfsm_advance(nd, i, rem);
2000 * For v4, don't allow lookups of '.' or '..' and
2001 * also check for non-utf8 strings.
2003 if (nd->nd_flag & ND_NFSV4) {
2004 if ((outlen == 1 && bufp[0] == '.') ||
2005 (outlen == 2 && bufp[0] == '.' &&
2007 nd->nd_repstat = NFSERR_BADNAME;
2011 if (enable_checkutf8 == 1 &&
2012 nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
2013 nd->nd_repstat = NFSERR_INVAL;
2020 *outlenp = (size_t)outlen;
2024 NFSEXITCODE2(error, nd);
2032 static int inited = 0;
2039 * Initialize client queues. Don't free/reinitialize
2040 * them when nfsds are restarted.
2042 nfsclienthash = malloc(sizeof(struct nfsclienthashhead) *
2043 nfsrv_clienthashsize, M_NFSDCLIENT, M_WAITOK | M_ZERO);
2044 for (i = 0; i < nfsrv_clienthashsize; i++)
2045 LIST_INIT(&nfsclienthash[i]);
2046 nfslockhash = malloc(sizeof(struct nfslockhashhead) *
2047 nfsrv_lockhashsize, M_NFSDLOCKFILE, M_WAITOK | M_ZERO);
2048 for (i = 0; i < nfsrv_lockhashsize; i++)
2049 LIST_INIT(&nfslockhash[i]);
2050 nfssessionhash = malloc(sizeof(struct nfssessionhash) *
2051 nfsrv_sessionhashsize, M_NFSDSESSION, M_WAITOK | M_ZERO);
2052 for (i = 0; i < nfsrv_sessionhashsize; i++) {
2053 mtx_init(&nfssessionhash[i].mtx, "nfssm", NULL, MTX_DEF);
2054 LIST_INIT(&nfssessionhash[i].list);
2057 /* and the v2 pubfh should be all zeros */
2058 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2062 * Check the v4 root exports.
2063 * Return 0 if ok, 1 otherwise.
2066 nfsd_checkrootexp(struct nfsrv_descript *nd)
2069 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2071 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2072 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2074 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2075 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2077 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2078 ND_EXGSS)) == (ND_GSS | ND_EXGSS))
2084 * Parse the first part of an NFSv4 compound to find out what the minor
2088 nfsd_getminorvers(struct nfsrv_descript *nd, u_char *tag, u_char **tagstrp,
2089 int *taglenp, u_int32_t *minversp)
2092 int error = 0, taglen = -1;
2093 u_char *tagstr = NULL;
2095 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2096 taglen = fxdr_unsigned(int, *tl);
2097 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
2101 if (taglen <= NFSV4_SMALLSTR)
2104 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2105 error = nfsrv_mtostr(nd, tagstr, taglen);
2108 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2109 *minversp = fxdr_unsigned(u_int32_t, *tl);
2111 if (*minversp == NFSV41_MINORVERSION)
2112 nd->nd_flag |= ND_NFSV41;
2115 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
2116 free(tagstr, M_TEMP);