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 disable_checkutf8 = 0;
65 SYSCTL_INT(_vfs_nfsd, OID_AUTO, disable_checkutf8, CTLFLAG_RW,
66 &disable_checkutf8, 0,
67 "Disable the NFSv4 check for a UTF8 compliant name");
69 static char nfsrv_hexdigit(char, int *);
72 * Maps errno values to nfs error numbers.
73 * Use NFSERR_IO as the catch all for ones not specifically defined in
74 * RFC 1094. (It now includes the errors added for NFSv3.)
76 static u_char nfsrv_v2errmap[NFSERR_REMOTE] = {
77 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
78 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
79 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
80 NFSERR_IO, NFSERR_EXIST, NFSERR_XDEV, NFSERR_NODEV, NFSERR_NOTDIR,
81 NFSERR_ISDIR, NFSERR_INVAL, NFSERR_IO, NFSERR_IO, NFSERR_IO,
82 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
83 NFSERR_MLINK, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
84 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
85 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
86 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
87 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
88 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
89 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
90 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
95 * Maps errno values to nfs error numbers.
96 * Although it is not obvious whether or not NFS clients really care if
97 * a returned error value is in the specified list for the procedure, the
98 * safest thing to do is filter them appropriately. For Version 2, the
99 * X/Open XNFS document is the only specification that defines error values
100 * for each RPC (The RFC simply lists all possible error values for all RPCs),
101 * so I have decided to not do this for Version 2.
102 * The first entry is the default error return and the rest are the valid
103 * errors for that RPC in increasing numeric order.
105 static short nfsv3err_null[] = {
110 static short nfsv3err_getattr[] = {
120 static short nfsv3err_setattr[] = {
137 static short nfsv3err_lookup[] = {
151 static short nfsv3err_access[] = {
161 static short nfsv3err_readlink[] = {
174 static short nfsv3err_read[] = {
187 static short nfsv3err_write[] = {
203 static short nfsv3err_create[] = {
221 static short nfsv3err_mkdir[] = {
239 static short nfsv3err_symlink[] = {
257 static short nfsv3err_mknod[] = {
276 static short nfsv3err_remove[] = {
291 static short nfsv3err_rmdir[] = {
310 static short nfsv3err_rename[] = {
334 static short nfsv3err_link[] = {
355 static short nfsv3err_readdir[] = {
369 static short nfsv3err_readdirplus[] = {
384 static short nfsv3err_fsstat[] = {
394 static short nfsv3err_fsinfo[] = {
403 static short nfsv3err_pathconf[] = {
412 static short nfsv3err_commit[] = {
422 static short *nfsrv_v3errmap[] = {
440 nfsv3err_readdirplus,
448 * And the same for V4.
450 static short nfsv4err_null[] = {
455 static short nfsv4err_access[] = {
472 static short nfsv4err_close[] = {
496 static short nfsv4err_commit[] = {
514 static short nfsv4err_create[] = {
543 static short nfsv4err_delegpurge[] = {
551 NFSERR_STALECLIENTID,
555 static short nfsv4err_delegreturn[] = {
574 static short nfsv4err_getattr[] = {
591 static short nfsv4err_getfh[] = {
603 static short nfsv4err_link[] = {
635 static short nfsv4err_lock[] = {
661 NFSERR_RECLAIMCONFLICT,
665 NFSERR_STALECLIENTID,
670 static short nfsv4err_lockt[] = {
689 NFSERR_STALECLIENTID,
693 static short nfsv4err_locku[] = {
719 static short nfsv4err_lookup[] = {
742 static short nfsv4err_lookupp[] = {
758 static short nfsv4err_nverify[] = {
778 static short nfsv4err_open[] = {
809 NFSERR_RECLAIMCONFLICT,
815 NFSERR_STALECLIENTID,
821 static short nfsv4err_openattr[] = {
842 static short nfsv4err_openconfirm[] = {
863 static short nfsv4err_opendowngrade[] = {
883 static short nfsv4err_putfh[] = {
896 static short nfsv4err_putpubfh[] = {
904 static short nfsv4err_putrootfh[] = {
912 static short nfsv4err_read[] = {
940 static short nfsv4err_readdir[] = {
961 static short nfsv4err_readlink[] = {
979 static short nfsv4err_remove[] = {
1004 static short nfsv4err_rename[] = {
1021 NFSERR_NOFILEHANDLE,
1034 static short nfsv4err_renew[] = {
1037 NFSERR_ADMINREVOKED,
1044 NFSERR_STALECLIENTID,
1048 static short nfsv4err_restorefh[] = {
1061 static short nfsv4err_savefh[] = {
1066 NFSERR_NOFILEHANDLE,
1073 static short nfsv4err_secinfo[] = {
1085 NFSERR_NOFILEHANDLE,
1093 static short nfsv4err_setattr[] = {
1096 NFSERR_ADMINREVOKED,
1114 NFSERR_NOFILEHANDLE,
1123 NFSERR_STALESTATEID,
1127 static short nfsv4err_setclientid[] = {
1137 static short nfsv4err_setclientidconfirm[] = {
1143 NFSERR_STALECLIENTID,
1147 static short nfsv4err_verify[] = {
1158 NFSERR_NOFILEHANDLE,
1166 static short nfsv4err_write[] = {
1169 NFSERR_ADMINREVOKED,
1185 NFSERR_NOFILEHANDLE,
1194 NFSERR_STALESTATEID,
1198 static short nfsv4err_releaselockowner[] = {
1200 NFSERR_ADMINREVOKED,
1207 NFSERR_STALECLIENTID,
1211 static short *nfsrv_v4errmap[] = {
1219 nfsv4err_delegpurge,
1220 nfsv4err_delegreturn,
1232 nfsv4err_openconfirm,
1233 nfsv4err_opendowngrade,
1247 nfsv4err_setclientid,
1248 nfsv4err_setclientidconfirm,
1251 nfsv4err_releaselockowner,
1255 * A fiddled version of m_adj() that ensures null fill to a long
1256 * boundary and only trims off the back end
1259 nfsrv_adj(mbuf_t mp, int len, int nul)
1266 * Trim from tail. Scan the mbuf chain,
1267 * calculating its length and finding the last mbuf.
1268 * If the adjustment only affects this mbuf, then just
1269 * adjust and return. Otherwise, rescan and truncate
1270 * after the remaining size.
1275 count += mbuf_len(m);
1276 if (mbuf_next(m) == NULL)
1280 if (mbuf_len(m) > len) {
1281 mbuf_setlen(m, mbuf_len(m) - len);
1283 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1284 for (i = 0; i < nul; i++)
1293 * Correct length for chain is "count".
1294 * Find the mbuf with last data, adjust its length,
1295 * and toss data from remaining mbufs on chain.
1297 for (m = mp; m; m = mbuf_next(m)) {
1298 if (mbuf_len(m) >= count) {
1299 mbuf_setlen(m, count);
1301 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1302 for (i = 0; i < nul; i++)
1307 count -= mbuf_len(m);
1309 for (m = mbuf_next(m); m; m = mbuf_next(m))
1314 * Make these functions instead of macros, so that the kernel text size
1315 * doesn't get too big...
1318 nfsrv_wcc(struct nfsrv_descript *nd, int before_ret,
1319 struct nfsvattr *before_nvap, int after_ret, struct nfsvattr *after_nvap)
1324 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1327 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1328 *tl++ = newnfs_true;
1329 txdr_hyper(before_nvap->na_size, tl);
1331 txdr_nfsv3time(&(before_nvap->na_mtime), tl);
1333 txdr_nfsv3time(&(before_nvap->na_ctime), tl);
1335 nfsrv_postopattr(nd, after_ret, after_nvap);
1339 nfsrv_postopattr(struct nfsrv_descript *nd, int after_ret,
1340 struct nfsvattr *after_nvap)
1344 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1349 nfsrv_fillattr(nd, after_nvap);
1354 * Fill in file attributes for V2 and 3. For V4, call a separate
1355 * routine that sifts through all the attribute bits.
1358 nfsrv_fillattr(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1360 struct nfs_fattr *fp;
1364 * Build space for the attribute structure.
1366 if (nd->nd_flag & ND_NFSV3)
1367 fattr_size = NFSX_V3FATTR;
1369 fattr_size = NFSX_V2FATTR;
1370 NFSM_BUILD(fp, struct nfs_fattr *, fattr_size);
1373 * Now just fill it all in.
1375 fp->fa_nlink = txdr_unsigned(nvap->na_nlink);
1376 fp->fa_uid = txdr_unsigned(nvap->na_uid);
1377 fp->fa_gid = txdr_unsigned(nvap->na_gid);
1378 if (nd->nd_flag & ND_NFSV3) {
1379 fp->fa_type = vtonfsv34_type(nvap->na_type);
1380 fp->fa_mode = vtonfsv34_mode(nvap->na_mode);
1381 txdr_hyper(nvap->na_size, &fp->fa3_size);
1382 txdr_hyper(nvap->na_bytes, &fp->fa3_used);
1383 fp->fa3_rdev.specdata1 = txdr_unsigned(NFSMAJOR(nvap->na_rdev));
1384 fp->fa3_rdev.specdata2 = txdr_unsigned(NFSMINOR(nvap->na_rdev));
1385 fp->fa3_fsid.nfsuquad[0] = 0;
1386 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(nvap->na_fsid);
1387 fp->fa3_fileid.nfsuquad[0] = 0;
1388 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(nvap->na_fileid);
1389 txdr_nfsv3time(&nvap->na_atime, &fp->fa3_atime);
1390 txdr_nfsv3time(&nvap->na_mtime, &fp->fa3_mtime);
1391 txdr_nfsv3time(&nvap->na_ctime, &fp->fa3_ctime);
1393 fp->fa_type = vtonfsv2_type(nvap->na_type);
1394 fp->fa_mode = vtonfsv2_mode(nvap->na_type, nvap->na_mode);
1395 fp->fa2_size = txdr_unsigned(nvap->na_size);
1396 fp->fa2_blocksize = txdr_unsigned(nvap->na_blocksize);
1397 if (nvap->na_type == VFIFO)
1398 fp->fa2_rdev = 0xffffffff;
1400 fp->fa2_rdev = txdr_unsigned(nvap->na_rdev);
1401 fp->fa2_blocks = txdr_unsigned(nvap->na_bytes / NFS_FABLKSIZE);
1402 fp->fa2_fsid = txdr_unsigned(nvap->na_fsid);
1403 fp->fa2_fileid = txdr_unsigned(nvap->na_fileid);
1404 txdr_nfsv2time(&nvap->na_atime, &fp->fa2_atime);
1405 txdr_nfsv2time(&nvap->na_mtime, &fp->fa2_mtime);
1406 txdr_nfsv2time(&nvap->na_ctime, &fp->fa2_ctime);
1411 * This function gets a file handle out of an mbuf list.
1412 * It returns 0 for success, EBADRPC otherwise.
1413 * If sets the third flagp argument to 1 if the file handle is
1414 * the public file handle.
1415 * For NFSv4, if the length is incorrect, set nd_repstat == NFSERR_BADHANDLE
1418 nfsrv_mtofh(struct nfsrv_descript *nd, struct nfsrvfh *fhp)
1421 int error = 0, len, copylen;
1423 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1424 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1425 len = fxdr_unsigned(int, *tl);
1426 if (len == 0 && nfs_pubfhset && (nd->nd_flag & ND_NFSV3) &&
1427 nd->nd_procnum == NFSPROC_LOOKUP) {
1428 nd->nd_flag |= ND_PUBLOOKUP;
1431 if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1432 if (nd->nd_flag & ND_NFSV4) {
1433 if (len > 0 && len <= NFSX_V4FHMAX) {
1434 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1437 nd->nd_repstat = NFSERR_BADHANDLE;
1451 * For NFSv2, the file handle is always 32 bytes on the
1452 * wire, but this server only cares about the first
1453 * NFSRV_MAXFH bytes.
1456 copylen = NFSRV_MAXFH;
1458 NFSM_DISSECT(tl, u_int32_t *, len);
1459 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1460 nd->nd_procnum == NFSPROC_LOOKUP &&
1461 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1462 nd->nd_flag |= ND_PUBLOOKUP;
1465 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1466 fhp->nfsrvfh_len = copylen;
1468 NFSEXITCODE2(error, nd);
1473 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1474 * numbers not specified for the associated procedure.
1475 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1476 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1477 * RPC procedure is not involved.
1478 * Returns the error number in XDR.
1481 nfsd_errmap(struct nfsrv_descript *nd)
1483 short *defaulterrp, *errp;
1485 if (!nd->nd_repstat)
1487 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1488 if (nd->nd_procnum == NFSPROC_NOOP)
1489 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1490 if (nd->nd_flag & ND_NFSV3)
1491 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1492 else if (nd->nd_repstat == EBADRPC)
1493 return (txdr_unsigned(NFSERR_BADXDR));
1494 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1495 nd->nd_repstat == NFSERR_OPILLEGAL)
1496 return (txdr_unsigned(nd->nd_repstat));
1497 else if ((nd->nd_flag & ND_NFSV41) != 0) {
1498 if (nd->nd_repstat == EOPNOTSUPP)
1499 nd->nd_repstat = NFSERR_NOTSUPP;
1500 nd->nd_repstat = nfsrv_isannfserr(nd->nd_repstat);
1501 return (txdr_unsigned(nd->nd_repstat));
1503 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1505 if (*errp == nd->nd_repstat)
1506 return (txdr_unsigned(nd->nd_repstat));
1507 return (txdr_unsigned(*defaulterrp));
1509 if (nd->nd_repstat <= NFSERR_REMOTE)
1510 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1511 return (txdr_unsigned(NFSERR_IO));
1515 * Check to see if the error is a valid NFS one. If not, replace it with
1519 nfsrv_isannfserr(u_int32_t errval)
1522 if (errval == NFSERR_OK)
1524 if (errval >= NFSERR_BADHANDLE && errval <= NFSERR_DELEGREVOKED)
1526 if (errval > 0 && errval <= NFSERR_REMOTE)
1527 return (nfsrv_v2errmap[errval - 1]);
1532 * Check to see if setting a uid/gid is permitted when creating a new
1533 * file object. (Called when uid and/or gid is specified in the
1534 * settable attributes for V4.
1537 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1542 * If not setting either uid nor gid, it's OK.
1544 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1546 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid == nfsrv_defaultuid)
1547 || (NFSVNO_ISSETGID(nvap) && nvap->na_gid == nfsrv_defaultgid)) {
1548 error = NFSERR_BADOWNER;
1551 if (nd->nd_cred->cr_uid == 0)
1553 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1554 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1555 !groupmember(nvap->na_gid, nd->nd_cred)))
1556 error = NFSERR_PERM;
1559 NFSEXITCODE2(error, nd);
1564 * and this routine fixes up the settable attributes for V4 if allowed
1565 * by nfsrv_checkuidgid().
1568 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1569 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1570 struct nfsexstuff *exp)
1573 struct nfsvattr nva;
1576 nfsattrbit_t nattrbits;
1579 * Maybe this should be done for V2 and 3 but it never has been
1580 * and nobody seems to be upset, so I think it's best not to change
1581 * the V2 and 3 semantics.
1583 if ((nd->nd_flag & ND_NFSV4) == 0)
1585 NFSVNO_ATTRINIT(&nva);
1586 NFSZERO_ATTRBIT(&nattrbits);
1587 tuid = nd->nd_cred->cr_uid;
1588 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1589 NFSVNO_ISSETUID(nvap) &&
1590 nvap->na_uid != nd->nd_cred->cr_uid) {
1591 if (nd->nd_cred->cr_uid == 0) {
1592 nva.na_uid = nvap->na_uid;
1594 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1596 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1599 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1600 NFSVNO_ISSETATIME(nvap)) {
1601 nva.na_atime = nvap->na_atime;
1603 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1605 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1606 NFSVNO_ISSETMTIME(nvap)) {
1607 nva.na_mtime = nvap->na_mtime;
1609 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1611 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1612 NFSVNO_ISSETGID(nvap)) {
1613 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1614 groupmember(nvap->na_gid, nd->nd_cred)) {
1615 nd->nd_cred->cr_uid = 0;
1616 nva.na_gid = nvap->na_gid;
1618 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1620 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1624 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1626 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1629 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1630 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1631 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1633 #ifdef NFS4_ACL_EXTATTR_NAME
1634 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1635 nfsrv_useacl != 0 && aclp != NULL) {
1636 if (aclp->acl_cnt > 0) {
1637 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1639 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1644 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1645 nd->nd_cred->cr_uid = tuid;
1648 NFSEXITCODE2(0, nd);
1652 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1653 * char isn't a hex digit.
1656 nfsrv_hexdigit(char c, int *err)
1660 if (c >= '0' && c <= '9')
1662 if (c >= 'a' && c <= 'f')
1663 return (c - 'a' + ((char)10));
1664 if (c >= 'A' && c <= 'F')
1665 return (c - 'A' + ((char)10));
1668 return (1); /* BOGUS */
1672 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1676 nfsrv_errmoved(int op)
1680 errp = nfsrv_v4errmap[op];
1681 while (*errp != 0) {
1682 if (*errp == NFSERR_MOVED)
1690 * Fill in attributes for a Referral.
1691 * (Return the number of bytes of XDR created.)
1694 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1695 struct nfsreferral *refp, int getattr, int *reterrp)
1697 u_int32_t *tl, *retnump;
1699 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1700 int fslocationsbit = 0;
1701 nfsattrbit_t tmpbits, refbits;
1703 NFSREFERRAL_ATTRBIT(&refbits);
1705 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1706 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1708 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1712 * Check for the case where unsupported referral attributes are
1715 NFSSET_ATTRBIT(&tmpbits, retbitp);
1716 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1717 if (NFSNONZERO_ATTRBIT(&tmpbits))
1720 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1721 *reterrp = NFSERR_MOVED;
1726 * Now we can fill in the attributes.
1728 NFSSET_ATTRBIT(&tmpbits, retbitp);
1729 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1732 * Put out the attribute bitmap for the ones being filled in
1733 * and get the field for the number of attributes returned.
1735 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1736 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1737 prefixnum += NFSX_UNSIGNED;
1740 * Now, loop around filling in the attributes for each bit set.
1742 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1743 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1745 case NFSATTRBIT_TYPE:
1746 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1747 *tl = txdr_unsigned(NFDIR);
1748 retnum += NFSX_UNSIGNED;
1750 case NFSATTRBIT_FSID:
1751 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1753 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1755 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1756 retnum += NFSX_V4FSID;
1758 case NFSATTRBIT_RDATTRERROR:
1759 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1761 *tl = txdr_unsigned(NFSERR_MOVED);
1764 retnum += NFSX_UNSIGNED;
1766 case NFSATTRBIT_FSLOCATIONS:
1767 retnum += nfsm_strtom(nd, "/", 1);
1768 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1769 *tl = txdr_unsigned(refp->nfr_srvcnt);
1770 retnum += NFSX_UNSIGNED;
1771 cp = refp->nfr_srvlist;
1772 for (i = 0; i < refp->nfr_srvcnt; i++) {
1773 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1774 *tl = txdr_unsigned(1);
1775 retnum += NFSX_UNSIGNED;
1776 cp2 = STRCHR(cp, ':');
1781 retnum += nfsm_strtom(nd, cp, len);
1784 cp2 = STRCHR(cp, ',');
1789 retnum += nfsm_strtom(nd, cp, len);
1794 case NFSATTRBIT_MOUNTEDONFILEID:
1795 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1797 *tl = txdr_unsigned(refp->nfr_dfileno);
1798 retnum += NFSX_HYPER;
1801 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1805 *retnump = txdr_unsigned(retnum);
1806 return (retnum + prefixnum);
1810 * Parse a file name out of a request.
1813 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1814 NFSPATHLEN_T *outlenp)
1816 char *fromcp, *tocp, val = '\0';
1819 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1828 * For V4, check for lookup parent.
1829 * Otherwise, get the component name.
1831 if ((nd->nd_flag & ND_NFSV4) && nd->nd_procnum == NFSV4OP_LOOKUPP) {
1833 hash += ((u_char)'.');
1835 hash += ((u_char)'.');
1839 * First, get the name length.
1841 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1842 len = fxdr_unsigned(int, *tl);
1843 if (len > NFS_MAXNAMLEN) {
1844 nd->nd_repstat = NFSERR_NAMETOL;
1847 } else if (len <= 0) {
1848 nd->nd_repstat = NFSERR_INVAL;
1854 * Now, copy the component name into the buffer.
1856 fromcp = nd->nd_dpos;
1858 rem = NFSMTOD(md, caddr_t) + mbuf_len(md) - fromcp;
1859 for (i = 0; i < len; i++) {
1866 fromcp = NFSMTOD(md, caddr_t);
1869 if (*fromcp == '\0') {
1870 nd->nd_repstat = EACCES;
1875 * For lookups on the public filehandle, do some special
1876 * processing on the name. (The public file handle is the
1877 * root of the public file system for this server.)
1879 if (nd->nd_flag & ND_PUBLOOKUP) {
1881 * If the first char is ASCII, it is a canonical
1882 * path, otherwise it is a native path. (RFC2054
1883 * doesn't actually state what it is if the first
1884 * char isn't ASCII or 0x80, so I assume native.)
1885 * pubtype == 1 -> native path
1886 * pubtype == 2 -> canonical path
1889 if (*fromcp & 0x80) {
1891 * Since RFC2054 doesn't indicate
1892 * that a native path of just 0x80
1893 * isn't allowed, I'll replace the
1894 * 0x80 with '/' instead of just
1904 * '/' only allowed in a native path
1906 if (*fromcp == '/' && pubtype != 1) {
1907 nd->nd_repstat = EACCES;
1913 * For the special case of 2 hex digits after a
1914 * '%' in an absolute path, calculate the value.
1915 * percent == 1 -> indicates "get first hex digit"
1916 * percent == 2 -> indicates "get second hex digit"
1919 digit = nfsrv_hexdigit(*fromcp, &error);
1921 nd->nd_repstat = EACCES;
1932 hash += ((u_char)val);
1936 if (*fromcp == '%' && pubtype == 2) {
1938 * Must be followed by 2 hex digits
1940 if ((len - i) < 3) {
1941 nd->nd_repstat = EACCES;
1948 hash += ((u_char)*fromcp);
1954 * Normal, non lookup on public, name.
1956 if (*fromcp == '/') {
1957 if (nd->nd_flag & ND_NFSV4)
1958 nd->nd_repstat = NFSERR_BADNAME;
1960 nd->nd_repstat = EACCES;
1964 hash += ((u_char)*fromcp);
1972 nd->nd_dpos = fromcp;
1973 i = NFSM_RNDUP(len) - len;
1978 error = nfsm_advance(nd, i, rem);
1985 * For v4, don't allow lookups of '.' or '..' and
1986 * also check for non-utf8 strings.
1988 if (nd->nd_flag & ND_NFSV4) {
1989 if ((outlen == 1 && bufp[0] == '.') ||
1990 (outlen == 2 && bufp[0] == '.' &&
1992 nd->nd_repstat = NFSERR_BADNAME;
1996 if (disable_checkutf8 == 0 &&
1997 nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
1998 nd->nd_repstat = NFSERR_INVAL;
2005 *outlenp = (size_t)outlen;
2009 NFSEXITCODE2(error, nd);
2017 static int inited = 0;
2024 * Initialize client queues. Don't free/reinitialize
2025 * them when nfsds are restarted.
2027 for (i = 0; i < NFSCLIENTHASHSIZE; i++)
2028 LIST_INIT(&nfsclienthash[i]);
2029 for (i = 0; i < NFSLOCKHASHSIZE; i++)
2030 LIST_INIT(&nfslockhash[i]);
2031 for (i = 0; i < NFSSESSIONHASHSIZE; i++)
2032 LIST_INIT(&nfssessionhash[i].list);
2034 /* and the v2 pubfh should be all zeros */
2035 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2039 * Check the v4 root exports.
2040 * Return 0 if ok, 1 otherwise.
2043 nfsd_checkrootexp(struct nfsrv_descript *nd)
2046 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2048 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2049 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2051 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2052 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2054 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2055 ND_EXGSS)) == (ND_GSS | ND_EXGSS))
2061 * Parse the first part of an NFSv4 compound to find out what the minor
2065 nfsd_getminorvers(struct nfsrv_descript *nd, u_char *tag, u_char **tagstrp,
2066 int *taglenp, u_int32_t *minversp)
2069 int error = 0, taglen = -1;
2070 u_char *tagstr = NULL;
2072 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2073 taglen = fxdr_unsigned(int, *tl);
2074 if (taglen < 0 || taglen > NFSV4_OPAQUELIMIT) {
2078 if (taglen <= NFSV4_SMALLSTR)
2081 tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
2082 error = nfsrv_mtostr(nd, tagstr, taglen);
2085 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED);
2086 *minversp = fxdr_unsigned(u_int32_t, *tl);
2088 if (*minversp == NFSV41_MINORVERSION)
2089 nd->nd_flag |= ND_NFSV41;
2092 if (tagstr != NULL && taglen > NFSV4_SMALLSTR)
2093 free(tagstr, M_TEMP);