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 int nfsrv_useacl;
50 extern uid_t nfsrv_defaultuid;
51 extern gid_t nfsrv_defaultgid;
53 char nfs_v2pubfh[NFSX_V2FH];
54 static nfstype newnfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK,
55 NFNON, NFCHR, NFNON };
56 extern nfstype nfsv34_type[9];
57 #endif /* !APPLEKEXT */
59 static char nfsrv_hexdigit(char, int *);
62 * Maps errno values to nfs error numbers.
63 * Use NFSERR_IO as the catch all for ones not specifically defined in
66 static u_char nfsrv_v2errmap[ELAST] = {
67 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
68 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
69 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
70 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
71 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
72 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
73 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
74 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
75 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
76 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
77 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
78 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
79 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
80 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
81 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
82 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
87 * Maps errno values to nfs error numbers.
88 * Although it is not obvious whether or not NFS clients really care if
89 * a returned error value is in the specified list for the procedure, the
90 * safest thing to do is filter them appropriately. For Version 2, the
91 * X/Open XNFS document is the only specification that defines error values
92 * for each RPC (The RFC simply lists all possible error values for all RPCs),
93 * so I have decided to not do this for Version 2.
94 * The first entry is the default error return and the rest are the valid
95 * errors for that RPC in increasing numeric order.
97 static short nfsv3err_null[] = {
102 static short nfsv3err_getattr[] = {
112 static short nfsv3err_setattr[] = {
129 static short nfsv3err_lookup[] = {
143 static short nfsv3err_access[] = {
153 static short nfsv3err_readlink[] = {
166 static short nfsv3err_read[] = {
179 static short nfsv3err_write[] = {
195 static short nfsv3err_create[] = {
213 static short nfsv3err_mkdir[] = {
231 static short nfsv3err_symlink[] = {
249 static short nfsv3err_mknod[] = {
268 static short nfsv3err_remove[] = {
283 static short nfsv3err_rmdir[] = {
302 static short nfsv3err_rename[] = {
326 static short nfsv3err_link[] = {
347 static short nfsv3err_readdir[] = {
361 static short nfsv3err_readdirplus[] = {
376 static short nfsv3err_fsstat[] = {
386 static short nfsv3err_fsinfo[] = {
395 static short nfsv3err_pathconf[] = {
404 static short nfsv3err_commit[] = {
414 static short *nfsrv_v3errmap[] = {
432 nfsv3err_readdirplus,
440 * And the same for V4.
442 static short nfsv4err_null[] = {
447 static short nfsv4err_access[] = {
464 static short nfsv4err_close[] = {
488 static short nfsv4err_commit[] = {
506 static short nfsv4err_create[] = {
535 static short nfsv4err_delegpurge[] = {
543 NFSERR_STALECLIENTID,
547 static short nfsv4err_delegreturn[] = {
566 static short nfsv4err_getattr[] = {
583 static short nfsv4err_getfh[] = {
595 static short nfsv4err_link[] = {
627 static short nfsv4err_lock[] = {
653 NFSERR_RECLAIMCONFLICT,
657 NFSERR_STALECLIENTID,
662 static short nfsv4err_lockt[] = {
681 NFSERR_STALECLIENTID,
685 static short nfsv4err_locku[] = {
711 static short nfsv4err_lookup[] = {
734 static short nfsv4err_lookupp[] = {
750 static short nfsv4err_nverify[] = {
770 static short nfsv4err_open[] = {
801 NFSERR_RECLAIMCONFLICT,
807 NFSERR_STALECLIENTID,
813 static short nfsv4err_openattr[] = {
834 static short nfsv4err_openconfirm[] = {
855 static short nfsv4err_opendowngrade[] = {
875 static short nfsv4err_putfh[] = {
888 static short nfsv4err_putpubfh[] = {
896 static short nfsv4err_putrootfh[] = {
904 static short nfsv4err_read[] = {
932 static short nfsv4err_readdir[] = {
953 static short nfsv4err_readlink[] = {
971 static short nfsv4err_remove[] = {
996 static short nfsv4err_rename[] = {
1013 NFSERR_NOFILEHANDLE,
1026 static short nfsv4err_renew[] = {
1029 NFSERR_ADMINREVOKED,
1036 NFSERR_STALECLIENTID,
1040 static short nfsv4err_restorefh[] = {
1053 static short nfsv4err_savefh[] = {
1058 NFSERR_NOFILEHANDLE,
1065 static short nfsv4err_secinfo[] = {
1077 NFSERR_NOFILEHANDLE,
1085 static short nfsv4err_setattr[] = {
1088 NFSERR_ADMINREVOKED,
1106 NFSERR_NOFILEHANDLE,
1115 NFSERR_STALESTATEID,
1119 static short nfsv4err_setclientid[] = {
1129 static short nfsv4err_setclientidconfirm[] = {
1135 NFSERR_STALECLIENTID,
1139 static short nfsv4err_verify[] = {
1150 NFSERR_NOFILEHANDLE,
1158 static short nfsv4err_write[] = {
1161 NFSERR_ADMINREVOKED,
1177 NFSERR_NOFILEHANDLE,
1186 NFSERR_STALESTATEID,
1190 static short nfsv4err_releaselockowner[] = {
1192 NFSERR_ADMINREVOKED,
1199 NFSERR_STALECLIENTID,
1203 static short *nfsrv_v4errmap[] = {
1211 nfsv4err_delegpurge,
1212 nfsv4err_delegreturn,
1224 nfsv4err_openconfirm,
1225 nfsv4err_opendowngrade,
1239 nfsv4err_setclientid,
1240 nfsv4err_setclientidconfirm,
1243 nfsv4err_releaselockowner,
1247 * A fiddled version of m_adj() that ensures null fill to a long
1248 * boundary and only trims off the back end
1251 nfsrv_adj(mbuf_t mp, int len, int nul)
1258 * Trim from tail. Scan the mbuf chain,
1259 * calculating its length and finding the last mbuf.
1260 * If the adjustment only affects this mbuf, then just
1261 * adjust and return. Otherwise, rescan and truncate
1262 * after the remaining size.
1267 count += mbuf_len(m);
1268 if (mbuf_next(m) == NULL)
1272 if (mbuf_len(m) > len) {
1273 mbuf_setlen(m, mbuf_len(m) - len);
1275 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1276 for (i = 0; i < nul; i++)
1285 * Correct length for chain is "count".
1286 * Find the mbuf with last data, adjust its length,
1287 * and toss data from remaining mbufs on chain.
1289 for (m = mp; m; m = mbuf_next(m)) {
1290 if (mbuf_len(m) >= count) {
1291 mbuf_setlen(m, count);
1293 cp = NFSMTOD(m, caddr_t) + mbuf_len(m) - nul;
1294 for (i = 0; i < nul; i++)
1299 count -= mbuf_len(m);
1301 for (m = mbuf_next(m); m; m = mbuf_next(m))
1306 * Make these functions instead of macros, so that the kernel text size
1307 * doesn't get too big...
1310 nfsrv_wcc(struct nfsrv_descript *nd, int before_ret,
1311 struct nfsvattr *before_nvap, int after_ret, struct nfsvattr *after_nvap)
1316 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1319 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1320 *tl++ = newnfs_true;
1321 txdr_hyper(before_nvap->na_size, tl);
1323 txdr_nfsv3time(&(before_nvap->na_mtime), tl);
1325 txdr_nfsv3time(&(before_nvap->na_ctime), tl);
1327 nfsrv_postopattr(nd, after_ret, after_nvap);
1331 nfsrv_postopattr(struct nfsrv_descript *nd, int after_ret,
1332 struct nfsvattr *after_nvap)
1336 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1341 nfsrv_fillattr(nd, after_nvap);
1346 * Fill in file attributes for V2 and 3. For V4, call a separate
1347 * routine that sifts through all the attribute bits.
1350 nfsrv_fillattr(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1352 struct nfs_fattr *fp;
1356 * Build space for the attribute structure.
1358 if (nd->nd_flag & ND_NFSV3)
1359 fattr_size = NFSX_V3FATTR;
1361 fattr_size = NFSX_V2FATTR;
1362 NFSM_BUILD(fp, struct nfs_fattr *, fattr_size);
1365 * Now just fill it all in.
1367 fp->fa_nlink = txdr_unsigned(nvap->na_nlink);
1368 fp->fa_uid = txdr_unsigned(nvap->na_uid);
1369 fp->fa_gid = txdr_unsigned(nvap->na_gid);
1370 if (nd->nd_flag & ND_NFSV3) {
1371 fp->fa_type = vtonfsv34_type(nvap->na_type);
1372 fp->fa_mode = vtonfsv34_mode(nvap->na_mode);
1373 txdr_hyper(nvap->na_size, &fp->fa3_size);
1374 txdr_hyper(nvap->na_bytes, &fp->fa3_used);
1375 fp->fa3_rdev.specdata1 = txdr_unsigned(NFSMAJOR(nvap->na_rdev));
1376 fp->fa3_rdev.specdata2 = txdr_unsigned(NFSMINOR(nvap->na_rdev));
1377 fp->fa3_fsid.nfsuquad[0] = 0;
1378 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(nvap->na_fsid);
1379 fp->fa3_fileid.nfsuquad[0] = 0;
1380 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(nvap->na_fileid);
1381 txdr_nfsv3time(&nvap->na_atime, &fp->fa3_atime);
1382 txdr_nfsv3time(&nvap->na_mtime, &fp->fa3_mtime);
1383 txdr_nfsv3time(&nvap->na_ctime, &fp->fa3_ctime);
1385 fp->fa_type = vtonfsv2_type(nvap->na_type);
1386 fp->fa_mode = vtonfsv2_mode(nvap->na_type, nvap->na_mode);
1387 fp->fa2_size = txdr_unsigned(nvap->na_size);
1388 fp->fa2_blocksize = txdr_unsigned(nvap->na_blocksize);
1389 if (nvap->na_type == VFIFO)
1390 fp->fa2_rdev = 0xffffffff;
1392 fp->fa2_rdev = txdr_unsigned(nvap->na_rdev);
1393 fp->fa2_blocks = txdr_unsigned(nvap->na_bytes / NFS_FABLKSIZE);
1394 fp->fa2_fsid = txdr_unsigned(nvap->na_fsid);
1395 fp->fa2_fileid = txdr_unsigned(nvap->na_fileid);
1396 txdr_nfsv2time(&nvap->na_atime, &fp->fa2_atime);
1397 txdr_nfsv2time(&nvap->na_mtime, &fp->fa2_mtime);
1398 txdr_nfsv2time(&nvap->na_ctime, &fp->fa2_ctime);
1403 * This function gets a file handle out of an mbuf list.
1404 * It returns 0 for success, EBADRPC otherwise.
1405 * If sets the third flagp argument to 1 if the file handle is
1406 * the public file handle.
1407 * For NFSv4, if the length is incorrect, set nd_repstat == NFSERR_BADHANDLE
1410 nfsrv_mtofh(struct nfsrv_descript *nd, struct nfsrvfh *fhp)
1413 int error = 0, len, copylen;
1415 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1416 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1417 len = fxdr_unsigned(int, *tl);
1418 if (len == 0 && nfs_pubfhset && (nd->nd_flag & ND_NFSV3) &&
1419 nd->nd_procnum == NFSPROC_LOOKUP) {
1420 nd->nd_flag |= ND_PUBLOOKUP;
1423 if (len < NFSRV_MINFH || len > NFSRV_MAXFH) {
1424 if (nd->nd_flag & ND_NFSV4) {
1425 if (len > 0 && len <= NFSX_V4FHMAX) {
1426 error = nfsm_advance(nd, NFSM_RNDUP(len), -1);
1429 nd->nd_repstat = NFSERR_BADHANDLE;
1443 * For NFSv2, the file handle is always 32 bytes on the
1444 * wire, but this server only cares about the first
1445 * NFSRV_MAXFH bytes.
1448 copylen = NFSRV_MAXFH;
1450 NFSM_DISSECT(tl, u_int32_t *, len);
1451 if ((nd->nd_flag & ND_NFSV2) && nfs_pubfhset &&
1452 nd->nd_procnum == NFSPROC_LOOKUP &&
1453 !NFSBCMP((caddr_t)tl, nfs_v2pubfh, NFSX_V2FH)) {
1454 nd->nd_flag |= ND_PUBLOOKUP;
1457 NFSBCOPY(tl, (caddr_t)fhp->nfsrvfh_data, copylen);
1458 fhp->nfsrvfh_len = copylen;
1460 NFSEXITCODE2(error, nd);
1465 * Map errnos to NFS error numbers. For Version 3 and 4 also filter out error
1466 * numbers not specified for the associated procedure.
1467 * NFSPROC_NOOP is a special case, where the high order bits of nd_repstat
1468 * should be cleared. NFSPROC_NOOP is used to return errors when a valid
1469 * RPC procedure is not involved.
1470 * Returns the error number in XDR.
1473 nfsd_errmap(struct nfsrv_descript *nd)
1475 short *defaulterrp, *errp;
1477 if (!nd->nd_repstat)
1479 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) {
1480 if (nd->nd_procnum == NFSPROC_NOOP)
1481 return (txdr_unsigned(nd->nd_repstat & 0xffff));
1482 if (nd->nd_flag & ND_NFSV3)
1483 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
1484 else if (nd->nd_repstat == EBADRPC)
1485 return (txdr_unsigned(NFSERR_BADXDR));
1486 else if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
1487 nd->nd_repstat == NFSERR_OPILLEGAL)
1488 return (txdr_unsigned(nd->nd_repstat));
1490 errp = defaulterrp = nfsrv_v4errmap[nd->nd_procnum];
1492 if (*errp == nd->nd_repstat)
1493 return (txdr_unsigned(nd->nd_repstat));
1494 return (txdr_unsigned(*defaulterrp));
1496 if (nd->nd_repstat <= ELAST)
1497 return (txdr_unsigned(nfsrv_v2errmap[nd->nd_repstat - 1]));
1498 return (txdr_unsigned(NFSERR_IO));
1502 * Check to see if setting a uid/gid is permitted when creating a new
1503 * file object. (Called when uid and/or gid is specified in the
1504 * settable attributes for V4.
1507 nfsrv_checkuidgid(struct nfsrv_descript *nd, struct nfsvattr *nvap)
1512 * If not setting either uid nor gid, it's OK.
1514 if (NFSVNO_NOTSETUID(nvap) && NFSVNO_NOTSETGID(nvap))
1516 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid == nfsrv_defaultuid)
1517 || (NFSVNO_ISSETGID(nvap) && nvap->na_gid == nfsrv_defaultgid)) {
1518 error = NFSERR_BADOWNER;
1521 if (nd->nd_cred->cr_uid == 0)
1523 if ((NFSVNO_ISSETUID(nvap) && nvap->na_uid != nd->nd_cred->cr_uid) ||
1524 (NFSVNO_ISSETGID(nvap) && nvap->na_gid != nd->nd_cred->cr_gid &&
1525 !groupmember(nvap->na_gid, nd->nd_cred)))
1526 error = NFSERR_PERM;
1529 NFSEXITCODE2(error, nd);
1534 * and this routine fixes up the settable attributes for V4 if allowed
1535 * by nfsrv_checkuidgid().
1538 nfsrv_fixattr(struct nfsrv_descript *nd, vnode_t vp,
1539 struct nfsvattr *nvap, NFSACL_T *aclp, NFSPROC_T *p, nfsattrbit_t *attrbitp,
1540 struct nfsexstuff *exp)
1543 struct nfsvattr nva;
1546 nfsattrbit_t nattrbits;
1549 * Maybe this should be done for V2 and 3 but it never has been
1550 * and nobody seems to be upset, so I think it's best not to change
1551 * the V2 and 3 semantics.
1553 if ((nd->nd_flag & ND_NFSV4) == 0)
1555 NFSVNO_ATTRINIT(&nva);
1556 NFSZERO_ATTRBIT(&nattrbits);
1557 tuid = nd->nd_cred->cr_uid;
1558 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNER) &&
1559 NFSVNO_ISSETUID(nvap) &&
1560 nvap->na_uid != nd->nd_cred->cr_uid) {
1561 if (nd->nd_cred->cr_uid == 0) {
1562 nva.na_uid = nvap->na_uid;
1564 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNER);
1566 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNER);
1569 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESSSET) &&
1570 NFSVNO_ISSETATIME(nvap)) {
1571 nva.na_atime = nvap->na_atime;
1573 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEACCESSSET);
1575 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFYSET) &&
1576 NFSVNO_ISSETMTIME(nvap)) {
1577 nva.na_mtime = nvap->na_mtime;
1579 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_TIMEMODIFYSET);
1581 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP) &&
1582 NFSVNO_ISSETGID(nvap)) {
1583 if (nvap->na_gid == nd->nd_cred->cr_gid ||
1584 groupmember(nvap->na_gid, nd->nd_cred)) {
1585 nd->nd_cred->cr_uid = 0;
1586 nva.na_gid = nvap->na_gid;
1588 NFSSETBIT_ATTRBIT(&nattrbits, NFSATTRBIT_OWNERGROUP);
1590 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_OWNERGROUP);
1594 error = nfsvno_setattr(vp, &nva, nd->nd_cred, p, exp);
1596 NFSCLRALL_ATTRBIT(attrbitp, &nattrbits);
1599 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) &&
1600 NFSVNO_ISSETSIZE(nvap) && nvap->na_size != (u_quad_t)0) {
1601 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_SIZE);
1603 #ifdef NFS4_ACL_EXTATTR_NAME
1604 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_ACL) &&
1605 nfsrv_useacl != 0 && aclp != NULL) {
1606 if (aclp->acl_cnt > 0) {
1607 error = nfsrv_setacl(vp, aclp, nd->nd_cred, p);
1609 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1614 NFSCLRBIT_ATTRBIT(attrbitp, NFSATTRBIT_ACL);
1615 nd->nd_cred->cr_uid = tuid;
1618 NFSEXITCODE2(0, nd);
1622 * Translate an ASCII hex digit to it's binary value. Return -1 if the
1623 * char isn't a hex digit.
1626 nfsrv_hexdigit(char c, int *err)
1630 if (c >= '0' && c <= '9')
1632 if (c >= 'a' && c <= 'f')
1633 return (c - 'a' + ((char)10));
1634 if (c >= 'A' && c <= 'F')
1635 return (c - 'A' + ((char)10));
1638 return (1); /* BOGUS */
1642 * Check to see if NFSERR_MOVED can be returned for this op. Return 1 iff
1646 nfsrv_errmoved(int op)
1650 errp = nfsrv_v4errmap[op];
1651 while (*errp != 0) {
1652 if (*errp == NFSERR_MOVED)
1660 * Fill in attributes for a Referral.
1661 * (Return the number of bytes of XDR created.)
1664 nfsrv_putreferralattr(struct nfsrv_descript *nd, nfsattrbit_t *retbitp,
1665 struct nfsreferral *refp, int getattr, int *reterrp)
1667 u_int32_t *tl, *retnump;
1669 int prefixnum, retnum = 0, i, len, bitpos, rderrbit = 0, nonrefbit = 0;
1670 int fslocationsbit = 0;
1671 nfsattrbit_t tmpbits, refbits;
1673 NFSREFERRAL_ATTRBIT(&refbits);
1675 NFSCLRBIT_ATTRBIT(&refbits, NFSATTRBIT_RDATTRERROR);
1676 else if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_RDATTRERROR))
1678 if (NFSISSET_ATTRBIT(retbitp, NFSATTRBIT_FSLOCATIONS))
1682 * Check for the case where unsupported referral attributes are
1685 NFSSET_ATTRBIT(&tmpbits, retbitp);
1686 NFSCLRALL_ATTRBIT(&tmpbits, &refbits);
1687 if (NFSNONZERO_ATTRBIT(&tmpbits))
1690 if (nonrefbit && !fslocationsbit && (getattr || !rderrbit)) {
1691 *reterrp = NFSERR_MOVED;
1696 * Now we can fill in the attributes.
1698 NFSSET_ATTRBIT(&tmpbits, retbitp);
1699 NFSCLRNOT_ATTRBIT(&tmpbits, &refbits);
1702 * Put out the attribute bitmap for the ones being filled in
1703 * and get the field for the number of attributes returned.
1705 prefixnum = nfsrv_putattrbit(nd, &tmpbits);
1706 NFSM_BUILD(retnump, u_int32_t *, NFSX_UNSIGNED);
1707 prefixnum += NFSX_UNSIGNED;
1710 * Now, loop around filling in the attributes for each bit set.
1712 for (bitpos = 0; bitpos < NFSATTRBIT_MAX; bitpos++) {
1713 if (NFSISSET_ATTRBIT(&tmpbits, bitpos)) {
1715 case NFSATTRBIT_TYPE:
1716 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1717 *tl = txdr_unsigned(NFDIR);
1718 retnum += NFSX_UNSIGNED;
1720 case NFSATTRBIT_FSID:
1721 NFSM_BUILD(tl, u_int32_t *, NFSX_V4FSID);
1723 *tl++ = txdr_unsigned(NFSV4ROOT_FSID0);
1725 *tl = txdr_unsigned(NFSV4ROOT_REFERRAL);
1726 retnum += NFSX_V4FSID;
1728 case NFSATTRBIT_RDATTRERROR:
1729 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1731 *tl = txdr_unsigned(NFSERR_MOVED);
1734 retnum += NFSX_UNSIGNED;
1736 case NFSATTRBIT_FSLOCATIONS:
1737 retnum += nfsm_strtom(nd, "/", 1);
1738 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1739 *tl = txdr_unsigned(refp->nfr_srvcnt);
1740 retnum += NFSX_UNSIGNED;
1741 cp = refp->nfr_srvlist;
1742 for (i = 0; i < refp->nfr_srvcnt; i++) {
1743 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1744 *tl = txdr_unsigned(1);
1745 retnum += NFSX_UNSIGNED;
1746 cp2 = STRCHR(cp, ':');
1751 retnum += nfsm_strtom(nd, cp, len);
1754 cp2 = STRCHR(cp, ',');
1759 retnum += nfsm_strtom(nd, cp, len);
1764 case NFSATTRBIT_MOUNTEDONFILEID:
1765 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER);
1767 *tl = txdr_unsigned(refp->nfr_dfileno);
1768 retnum += NFSX_HYPER;
1771 printf("EEK! Bad V4 refattr bitpos=%d\n", bitpos);
1775 *retnump = txdr_unsigned(retnum);
1776 return (retnum + prefixnum);
1780 * Parse a file name out of a request.
1783 nfsrv_parsename(struct nfsrv_descript *nd, char *bufp, u_long *hashp,
1784 NFSPATHLEN_T *outlenp)
1786 char *fromcp, *tocp, val = '\0';
1789 int rem, len, error = 0, pubtype = 0, outlen = 0, percent = 0;
1798 * For V4, check for lookup parent.
1799 * Otherwise, get the component name.
1801 if ((nd->nd_flag & ND_NFSV4) && nd->nd_procnum == NFSV4OP_LOOKUPP) {
1803 hash += ((u_char)'.');
1805 hash += ((u_char)'.');
1809 * First, get the name length.
1811 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1812 len = fxdr_unsigned(int, *tl);
1813 if (len > NFS_MAXNAMLEN) {
1814 nd->nd_repstat = NFSERR_NAMETOL;
1817 } else if (len <= 0) {
1818 nd->nd_repstat = NFSERR_INVAL;
1824 * Now, copy the component name into the buffer.
1826 fromcp = nd->nd_dpos;
1828 rem = NFSMTOD(md, caddr_t) + mbuf_len(md) - fromcp;
1829 for (i = 0; i < len; i++) {
1836 fromcp = NFSMTOD(md, caddr_t);
1839 if (*fromcp == '\0') {
1840 nd->nd_repstat = EACCES;
1845 * For lookups on the public filehandle, do some special
1846 * processing on the name. (The public file handle is the
1847 * root of the public file system for this server.)
1849 if (nd->nd_flag & ND_PUBLOOKUP) {
1851 * If the first char is ASCII, it is a canonical
1852 * path, otherwise it is a native path. (RFC2054
1853 * doesn't actually state what it is if the first
1854 * char isn't ASCII or 0x80, so I assume native.)
1855 * pubtype == 1 -> native path
1856 * pubtype == 2 -> canonical path
1859 if (*fromcp & 0x80) {
1861 * Since RFC2054 doesn't indicate
1862 * that a native path of just 0x80
1863 * isn't allowed, I'll replace the
1864 * 0x80 with '/' instead of just
1874 * '/' only allowed in a native path
1876 if (*fromcp == '/' && pubtype != 1) {
1877 nd->nd_repstat = EACCES;
1883 * For the special case of 2 hex digits after a
1884 * '%' in an absolute path, calculate the value.
1885 * percent == 1 -> indicates "get first hex digit"
1886 * percent == 2 -> indicates "get second hex digit"
1889 digit = nfsrv_hexdigit(*fromcp, &error);
1891 nd->nd_repstat = EACCES;
1902 hash += ((u_char)val);
1906 if (*fromcp == '%' && pubtype == 2) {
1908 * Must be followed by 2 hex digits
1910 if ((len - i) < 3) {
1911 nd->nd_repstat = EACCES;
1918 hash += ((u_char)*fromcp);
1924 * Normal, non lookup on public, name.
1926 if (*fromcp == '/') {
1927 if (nd->nd_flag & ND_NFSV4)
1928 nd->nd_repstat = NFSERR_BADNAME;
1930 nd->nd_repstat = EACCES;
1934 hash += ((u_char)*fromcp);
1942 nd->nd_dpos = fromcp;
1943 i = NFSM_RNDUP(len) - len;
1948 error = nfsm_advance(nd, i, rem);
1955 * For v4, don't allow lookups of '.' or '..' and
1956 * also check for non-utf8 strings.
1958 if (nd->nd_flag & ND_NFSV4) {
1959 if ((outlen == 1 && bufp[0] == '.') ||
1960 (outlen == 2 && bufp[0] == '.' &&
1962 nd->nd_repstat = NFSERR_BADNAME;
1966 if (nfsrv_checkutf8((u_int8_t *)bufp, outlen)) {
1967 nd->nd_repstat = NFSERR_INVAL;
1974 *outlenp = (size_t)outlen;
1978 NFSEXITCODE2(error, nd);
1983 * Check the tcp socket sequence number has been acknowledged.
1986 nfsrv_checksockseqnum(struct socket *so, tcp_seq tcpseqval)
1988 tcp_seq maxseq, unaseq;
1991 error = nfsrv_getsocksndseq(so, &maxseq, &unaseq);
1994 ret = SEQ_GEQ(unaseq, tcpseqval);
1999 * Get the tcp sequence number to be acknowledged.
2002 nfsrv_getsockseqnum(struct socket *so, tcp_seq *tcpseqp)
2004 tcp_seq maxseq, unaseq;
2008 sbcc = so->so_snd.sb_cc;
2009 error = nfsrv_getsocksndseq(so, &maxseq, &unaseq);
2013 * Set the seq# to a value that will
2014 * be at least the end of the reply.
2015 * When this sequence# is acknowledged
2016 * by the client, the client has received
2019 *tcpseqp = sbcc + maxseq;
2027 static int inited = 0;
2034 * Initialize client queues. Don't free/reinitialize
2035 * them when nfsds are restarted.
2037 for (i = 0; i < NFSCLIENTHASHSIZE; i++)
2038 LIST_INIT(&nfsclienthash[i]);
2039 for (i = 0; i < NFSLOCKHASHSIZE; i++)
2040 LIST_INIT(&nfslockhash[i]);
2042 /* and the v2 pubfh should be all zeros */
2043 NFSBZERO(nfs_v2pubfh, NFSX_V2FH);
2047 * Check the v4 root exports.
2048 * Return 0 if ok, 1 otherwise.
2051 nfsd_checkrootexp(struct nfsrv_descript *nd)
2054 if ((nd->nd_flag & (ND_GSS | ND_EXAUTHSYS)) == ND_EXAUTHSYS)
2056 if ((nd->nd_flag & (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY)) ==
2057 (ND_GSSINTEGRITY | ND_EXGSSINTEGRITY))
2059 if ((nd->nd_flag & (ND_GSSPRIVACY | ND_EXGSSPRIVACY)) ==
2060 (ND_GSSPRIVACY | ND_EXGSSPRIVACY))
2062 if ((nd->nd_flag & (ND_GSS | ND_GSSINTEGRITY | ND_GSSPRIVACY |
2063 ND_EXGSS)) == (ND_GSS | ND_EXGSS))