mountd.c revision 75635
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1989, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * This code is derived from software contributed to Berkeley by 61558Srgrimes * Herb Hasler and Rick Macklem at The University of Guelph. 71558Srgrimes * 81558Srgrimes * Redistribution and use in source and binary forms, with or without 91558Srgrimes * modification, are permitted provided that the following conditions 101558Srgrimes * are met: 111558Srgrimes * 1. Redistributions of source code must retain the above copyright 121558Srgrimes * notice, this list of conditions and the following disclaimer. 131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141558Srgrimes * notice, this list of conditions and the following disclaimer in the 151558Srgrimes * documentation and/or other materials provided with the distribution. 161558Srgrimes * 3. All advertising materials mentioning features or use of this software 171558Srgrimes * must display the following acknowledgement: 181558Srgrimes * This product includes software developed by the University of 191558Srgrimes * California, Berkeley and its contributors. 201558Srgrimes * 4. Neither the name of the University nor the names of its contributors 211558Srgrimes * may be used to endorse or promote products derived from this software 221558Srgrimes * without specific prior written permission. 231558Srgrimes * 241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341558Srgrimes * SUCH DAMAGE. 351558Srgrimes */ 361558Srgrimes 371558Srgrimes#ifndef lint 3837663Scharnierstatic const char copyright[] = 391558Srgrimes"@(#) Copyright (c) 1989, 1993\n\ 401558Srgrimes The Regents of the University of California. All rights reserved.\n"; 412999Swollman#endif /*not lint*/ 421558Srgrimes 431558Srgrimes#ifndef lint 4437663Scharnier#if 0 4537663Scharnierstatic char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95"; 4637663Scharnier#endif 472999Swollmanstatic const char rcsid[] = 4850476Speter "$FreeBSD: head/usr.sbin/mountd/mountd.c 75635 2001-04-17 22:25:48Z iedowse $"; 492999Swollman#endif /*not lint*/ 501558Srgrimes 511558Srgrimes#include <sys/param.h> 521558Srgrimes#include <sys/mount.h> 5374462Salfred#include <sys/fcntl.h> 541558Srgrimes#include <sys/stat.h> 551558Srgrimes#include <sys/syslog.h> 5624330Sguido#include <sys/sysctl.h> 571558Srgrimes 581558Srgrimes#include <rpc/rpc.h> 591558Srgrimes#include <rpc/pmap_clnt.h> 6074462Salfred#include <rpc/pmap_prot.h> 6174462Salfred#include <rpcsvc/mount.h> 621558Srgrimes#include <nfs/rpcv2.h> 639336Sdfr#include <nfs/nfsproto.h> 6424330Sguido#include <nfs/nfs.h> 6523681Speter#include <ufs/ufs/ufsmount.h> 6623681Speter#include <msdosfs/msdosfsmount.h> 6754093Ssemenu#include <ntfs/ntfsmount.h> 6823681Speter#include <isofs/cd9660/cd9660_mount.h> /* XXX need isofs in include */ 691558Srgrimes 701558Srgrimes#include <arpa/inet.h> 711558Srgrimes 721558Srgrimes#include <ctype.h> 7337663Scharnier#include <err.h> 741558Srgrimes#include <errno.h> 751558Srgrimes#include <grp.h> 761558Srgrimes#include <netdb.h> 771558Srgrimes#include <pwd.h> 781558Srgrimes#include <signal.h> 791558Srgrimes#include <stdio.h> 801558Srgrimes#include <stdlib.h> 811558Srgrimes#include <string.h> 821558Srgrimes#include <unistd.h> 831558Srgrimes#include "pathnames.h" 841558Srgrimes 851558Srgrimes#ifdef DEBUG 861558Srgrimes#include <stdarg.h> 871558Srgrimes#endif 881558Srgrimes 8974462Salfred#ifndef MOUNTDLOCK 9074462Salfred#define MOUNTDLOCK "/var/run/mountd.lock" 9174462Salfred#endif 9274462Salfred 931558Srgrimes/* 941558Srgrimes * Structures for keeping the mount list and export list 951558Srgrimes */ 961558Srgrimesstruct mountlist { 971558Srgrimes struct mountlist *ml_next; 981558Srgrimes char ml_host[RPCMNT_NAMELEN+1]; 991558Srgrimes char ml_dirp[RPCMNT_PATHLEN+1]; 1001558Srgrimes}; 1011558Srgrimes 1021558Srgrimesstruct dirlist { 1031558Srgrimes struct dirlist *dp_left; 1041558Srgrimes struct dirlist *dp_right; 1051558Srgrimes int dp_flag; 1061558Srgrimes struct hostlist *dp_hosts; /* List of hosts this dir exported to */ 1071558Srgrimes char dp_dirp[1]; /* Actually malloc'd to size of dir */ 1081558Srgrimes}; 1091558Srgrimes/* dp_flag bits */ 1101558Srgrimes#define DP_DEFSET 0x1 1119336Sdfr#define DP_HOSTSET 0x2 1129336Sdfr#define DP_KERB 0x4 1131558Srgrimes 1141558Srgrimesstruct exportlist { 1151558Srgrimes struct exportlist *ex_next; 1161558Srgrimes struct dirlist *ex_dirl; 1171558Srgrimes struct dirlist *ex_defdir; 1181558Srgrimes int ex_flag; 1191558Srgrimes fsid_t ex_fs; 1201558Srgrimes char *ex_fsdir; 12127447Sdfr char *ex_indexfile; 1221558Srgrimes}; 1231558Srgrimes/* ex_flag bits */ 1241558Srgrimes#define EX_LINKED 0x1 1251558Srgrimes 1261558Srgrimesstruct netmsk { 12774462Salfred struct sockaddr_storage nt_net; 12842144Sdfr u_int32_t nt_mask; 12942144Sdfr char *nt_name; 1301558Srgrimes}; 1311558Srgrimes 1321558Srgrimesunion grouptypes { 13374462Salfred struct addrinfo *gt_addrinfo; 1341558Srgrimes struct netmsk gt_net; 1351558Srgrimes}; 1361558Srgrimes 1371558Srgrimesstruct grouplist { 1381558Srgrimes int gr_type; 1391558Srgrimes union grouptypes gr_ptr; 1401558Srgrimes struct grouplist *gr_next; 1411558Srgrimes}; 1421558Srgrimes/* Group types */ 1431558Srgrimes#define GT_NULL 0x0 1441558Srgrimes#define GT_HOST 0x1 1451558Srgrimes#define GT_NET 0x2 1467401Swpaul#define GT_IGNORE 0x5 1471558Srgrimes 1481558Srgrimesstruct hostlist { 1499336Sdfr int ht_flag; /* Uses DP_xx bits */ 1501558Srgrimes struct grouplist *ht_grp; 1511558Srgrimes struct hostlist *ht_next; 1521558Srgrimes}; 1531558Srgrimes 1549336Sdfrstruct fhreturn { 1559336Sdfr int fhr_flag; 1569336Sdfr int fhr_vers; 1579336Sdfr nfsfh_t fhr_fh; 1589336Sdfr}; 1599336Sdfr 1601558Srgrimes/* Global defs */ 1611558Srgrimeschar *add_expdir __P((struct dirlist **, char *, int)); 1621558Srgrimesvoid add_dlist __P((struct dirlist **, struct dirlist *, 1639336Sdfr struct grouplist *, int)); 1641558Srgrimesvoid add_mlist __P((char *, char *)); 1651558Srgrimesint check_dirpath __P((char *)); 1661558Srgrimesint check_options __P((struct dirlist *)); 16774462Salfredint chk_host __P((struct dirlist *, struct sockaddr *, int *, int *)); 16875635Siedowsevoid del_mlist(char *hostp, char *dirp); 1691558Srgrimesstruct dirlist *dirp_search __P((struct dirlist *, char *)); 1701558Srgrimesint do_mount __P((struct exportlist *, struct grouplist *, int, 17172650Sgreen struct xucred *, char *, int, struct statfs *)); 1721558Srgrimesint do_opt __P((char **, char **, struct exportlist *, struct grouplist *, 17372650Sgreen int *, int *, struct xucred *)); 1741558Srgrimesstruct exportlist *ex_search __P((fsid_t *)); 1751558Srgrimesstruct exportlist *get_exp __P((void)); 1761558Srgrimesvoid free_dir __P((struct dirlist *)); 1771558Srgrimesvoid free_exp __P((struct exportlist *)); 1781558Srgrimesvoid free_grp __P((struct grouplist *)); 1791558Srgrimesvoid free_host __P((struct hostlist *)); 1801558Srgrimesvoid get_exportlist __P((void)); 1817401Swpaulint get_host __P((char *, struct grouplist *, struct grouplist *)); 1821558Srgrimesstruct hostlist *get_ht __P((void)); 1831558Srgrimesint get_line __P((void)); 1841558Srgrimesvoid get_mountlist __P((void)); 1851558Srgrimesint get_net __P((char *, struct netmsk *, int)); 1861558Srgrimesvoid getexp_err __P((struct exportlist *, struct grouplist *)); 1871558Srgrimesstruct grouplist *get_grp __P((void)); 1881558Srgrimesvoid hang_dirp __P((struct dirlist *, struct grouplist *, 1891558Srgrimes struct exportlist *, int)); 1901558Srgrimesvoid mntsrv __P((struct svc_req *, SVCXPRT *)); 1911558Srgrimesvoid nextfield __P((char **, char **)); 1921558Srgrimesvoid out_of_mem __P((void)); 19372650Sgreenvoid parsecred __P((char *, struct xucred *)); 1941558Srgrimesint put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *)); 19574462Salfredint scan_tree __P((struct dirlist *, struct sockaddr *)); 19637663Scharnierstatic void usage __P((void)); 1971558Srgrimesint xdr_dir __P((XDR *, char *)); 1981558Srgrimesint xdr_explist __P((XDR *, caddr_t)); 1999336Sdfrint xdr_fhs __P((XDR *, caddr_t)); 2001558Srgrimesint xdr_mlist __P((XDR *, caddr_t)); 20174462Salfredvoid terminate __P((int)); 2021558Srgrimes 20374462Salfredstatic int bitcmp __P((void *, void *, int)); 20474462Salfredstatic int netpartcmp __P((struct sockaddr *, struct sockaddr *, int)); 20574462Salfredstatic int sacmp __P((struct sockaddr *, struct sockaddr *)); 20674462Salfredstatic int allones __P((struct sockaddr_storage *, int)); 20774462Salfredstatic int countones __P((struct sockaddr *)); 20874462Salfred 2091558Srgrimesstruct exportlist *exphead; 2101558Srgrimesstruct mountlist *mlhead; 2111558Srgrimesstruct grouplist *grphead; 2121558Srgrimeschar exname[MAXPATHLEN]; 21372650Sgreenstruct xucred def_anon = { 21472650Sgreen 0, 21572650Sgreen (uid_t)-2, 2161558Srgrimes 1, 21772650Sgreen { (gid_t)-2 }, 21872650Sgreen NULL 2191558Srgrimes}; 22025087Sdfrint force_v2 = 0; 2219336Sdfrint resvport_only = 1; 2229336Sdfrint dir_only = 1; 22331705Sguidoint log = 0; 22474462Salfred 2251558Srgrimesint opt_flags; 22674462Salfredstatic int have_v6 = 1; 22774462Salfred#ifdef NI_WITHSCOPEID 22874462Salfredstatic const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID; 22974462Salfred#else 23074462Salfredstatic const int ninumeric = NI_NUMERICHOST; 23174462Salfred#endif 23274462Salfred 23374462Salfredint mountdlockfd; 2341558Srgrimes/* Bits for above */ 2351558Srgrimes#define OP_MAPROOT 0x01 2361558Srgrimes#define OP_MAPALL 0x02 2371558Srgrimes#define OP_KERB 0x04 2381558Srgrimes#define OP_MASK 0x08 2391558Srgrimes#define OP_NET 0x10 2401558Srgrimes#define OP_ALLDIRS 0x40 24174462Salfred#define OP_MASKLEN 0x200 2421558Srgrimes 2431558Srgrimes#ifdef DEBUG 2441558Srgrimesint debug = 1; 2451558Srgrimesvoid SYSLOG __P((int, const char *, ...)); 2461558Srgrimes#define syslog SYSLOG 2471558Srgrimes#else 2481558Srgrimesint debug = 0; 2491558Srgrimes#endif 2501558Srgrimes 2511558Srgrimes/* 2521558Srgrimes * Mountd server for NFS mount protocol as described in: 2531558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A 2541558Srgrimes * The optional arguments are the exports file name 2551558Srgrimes * default: _PATH_EXPORTS 2561558Srgrimes * and "-n" to allow nonroot mount. 2571558Srgrimes */ 2581558Srgrimesint 2591558Srgrimesmain(argc, argv) 2601558Srgrimes int argc; 2611558Srgrimes char **argv; 2621558Srgrimes{ 26374462Salfred SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp; 26474462Salfred struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf; 26574462Salfred int udpsock, tcpsock, udp6sock, tcp6sock; 26674462Salfred int xcreated = 0, s; 26774462Salfred int one = 1; 26832656Sbde int c, error, mib[3]; 26923681Speter struct vfsconf vfc; 2701558Srgrimes 27175635Siedowse udp6conf = tcp6conf = NULL; 27275635Siedowse udp6sock = tcp6sock = NULL; 27375635Siedowse 27474462Salfred /* Check that another mountd isn't already running. */ 27574462Salfred if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) 27674462Salfred err(1, "%s", MOUNTDLOCK); 27774462Salfred 27874462Salfred if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) 27974462Salfred errx(1, "another rpc.mountd is already running. Aborting"); 28074462Salfred s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 28174462Salfred if (s < 0) 28274462Salfred have_v6 = 0; 28374462Salfred else 28474462Salfred close(s); 28523681Speter error = getvfsbyname("nfs", &vfc); 28623681Speter if (error && vfsisloadable("nfs")) { 2872999Swollman if(vfsload("nfs")) 2882999Swollman err(1, "vfsload(nfs)"); 2892999Swollman endvfsent(); /* flush cache */ 29023681Speter error = getvfsbyname("nfs", &vfc); 2912999Swollman } 29223681Speter if (error) 2932999Swollman errx(1, "NFS support is not available in the running kernel"); 2942999Swollman 29531665Sguido while ((c = getopt(argc, argv, "2dlnr")) != -1) 2961558Srgrimes switch (c) { 29725087Sdfr case '2': 29825087Sdfr force_v2 = 1; 29925087Sdfr break; 3009336Sdfr case 'n': 3019336Sdfr resvport_only = 0; 3029336Sdfr break; 3039336Sdfr case 'r': 3049336Sdfr dir_only = 0; 3059336Sdfr break; 3068688Sphk case 'd': 3078688Sphk debug = debug ? 0 : 1; 3088688Sphk break; 30931656Sguido case 'l': 31031656Sguido log = 1; 31131656Sguido break; 3121558Srgrimes default: 31337663Scharnier usage(); 3141558Srgrimes }; 3151558Srgrimes argc -= optind; 3161558Srgrimes argv += optind; 3171558Srgrimes grphead = (struct grouplist *)NULL; 3181558Srgrimes exphead = (struct exportlist *)NULL; 3191558Srgrimes mlhead = (struct mountlist *)NULL; 3201558Srgrimes if (argc == 1) { 3211558Srgrimes strncpy(exname, *argv, MAXPATHLEN-1); 3221558Srgrimes exname[MAXPATHLEN-1] = '\0'; 3231558Srgrimes } else 3241558Srgrimes strcpy(exname, _PATH_EXPORTS); 3251558Srgrimes openlog("mountd", LOG_PID, LOG_DAEMON); 3261558Srgrimes if (debug) 32737663Scharnier warnx("getting export list"); 3281558Srgrimes get_exportlist(); 3291558Srgrimes if (debug) 33037663Scharnier warnx("getting mount list"); 3311558Srgrimes get_mountlist(); 3321558Srgrimes if (debug) 33337663Scharnier warnx("here we go"); 3341558Srgrimes if (debug == 0) { 3351558Srgrimes daemon(0, 0); 3361558Srgrimes signal(SIGINT, SIG_IGN); 3371558Srgrimes signal(SIGQUIT, SIG_IGN); 3381558Srgrimes } 3391558Srgrimes signal(SIGHUP, (void (*) __P((int))) get_exportlist); 34074462Salfred signal(SIGTERM, terminate); 3411558Srgrimes { FILE *pidfile = fopen(_PATH_MOUNTDPID, "w"); 3421558Srgrimes if (pidfile != NULL) { 3431558Srgrimes fprintf(pidfile, "%d\n", getpid()); 3441558Srgrimes fclose(pidfile); 3451558Srgrimes } 3461558Srgrimes } 34774462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 34874462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 34974462Salfred udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 35074462Salfred tcpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 35174791Salfred udpconf = getnetconfigent("udp"); 35274791Salfred tcpconf = getnetconfigent("tcp"); 35374791Salfred if (!have_v6) 35474791Salfred goto skip_v6; 35574462Salfred udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 35674462Salfred tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 35774462Salfred /* 35874462Salfred * We're doing host-based access checks here, so don't allow 35974462Salfred * v4-in-v6 to confuse things. The kernel will disable it 36074462Salfred * by default on NFS sockets too. 36174462Salfred */ 36274462Salfred if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6, 36374462Salfred IPV6_BINDV6ONLY, &one, sizeof one) < 0){ 36474462Salfred syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket"); 36574462Salfred exit(1); 36674462Salfred } 36774462Salfred if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6, 36874462Salfred IPV6_BINDV6ONLY, &one, sizeof one) < 0){ 36974462Salfred syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket"); 37074462Salfred exit(1); 37174462Salfred } 37274462Salfred udp6conf = getnetconfigent("udp6"); 37374462Salfred tcp6conf = getnetconfigent("tcp6"); 37474791Salfred 37574791Salfredskip_v6: 37624759Sguido if (!resvport_only) { 37724759Sguido mib[0] = CTL_VFS; 37832656Sbde mib[1] = vfc.vfc_typenum; 37924759Sguido mib[2] = NFS_NFSPRIVPORT; 38024759Sguido if (sysctl(mib, 3, NULL, NULL, &resvport_only, 38124759Sguido sizeof(resvport_only)) != 0 && errno != ENOENT) { 38224759Sguido syslog(LOG_ERR, "sysctl: %m"); 38324759Sguido exit(1); 38424759Sguido } 38524330Sguido } 3869202Srgrimes if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL || 3879202Srgrimes (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) { 38837663Scharnier syslog(LOG_ERR, "can't create socket"); 3891558Srgrimes exit(1); 3901558Srgrimes } 39174462Salfred if (udpsock != -1 && udpconf != NULL) { 39274462Salfred bindresvport(udpsock, NULL); 39374462Salfred udptransp = svc_dg_create(udpsock, 0, 0); 39474462Salfred if (udptransp != NULL) { 39574462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1, 39674462Salfred mntsrv, udpconf)) 39774462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service"); 39874462Salfred else 39974462Salfred xcreated++; 40074462Salfred if (!force_v2) { 40174462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3, 40274462Salfred mntsrv, udpconf)) 40374462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service"); 40474462Salfred else 40574462Salfred xcreated++; 40674462Salfred } 40774462Salfred } else 40874462Salfred syslog(LOG_WARNING, "can't create UDP services"); 40974462Salfred 41074462Salfred } 41174462Salfred if (tcpsock != -1 && tcpconf != NULL) { 41274462Salfred bindresvport(tcpsock, NULL); 41374462Salfred listen(tcpsock, SOMAXCONN); 41474462Salfred tcptransp = svc_vc_create(tcpsock, 0, 0); 41574462Salfred if (tcptransp != NULL) { 41674462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1, 41774462Salfred mntsrv, tcpconf)) 41874462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service"); 41974462Salfred else 42074462Salfred xcreated++; 42174462Salfred if (!force_v2) { 42274462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3, 42374462Salfred mntsrv, tcpconf)) 42474462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service"); 42574462Salfred else 42674462Salfred xcreated++; 42774462Salfred } 42874462Salfred } else 42974462Salfred syslog(LOG_WARNING, "can't create TCP service"); 43074462Salfred 43174462Salfred } 43274791Salfred if (have_v6 && udp6sock != -1 && udp6conf != NULL) { 43374462Salfred bindresvport(udp6sock, NULL); 43474462Salfred udp6transp = svc_dg_create(udp6sock, 0, 0); 43574462Salfred if (udp6transp != NULL) { 43674462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1, 43774462Salfred mntsrv, udp6conf)) 43874462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service"); 43974462Salfred else 44074462Salfred xcreated++; 44174462Salfred if (!force_v2) { 44274462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3, 44374462Salfred mntsrv, udp6conf)) 44474462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service"); 44574462Salfred else 44674462Salfred xcreated++; 44774462Salfred } 44874462Salfred } else 44974462Salfred syslog(LOG_WARNING, "can't create UDP6 service"); 45074462Salfred 45174462Salfred } 45274791Salfred if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) { 45374462Salfred bindresvport(tcp6sock, NULL); 45474462Salfred listen(tcp6sock, SOMAXCONN); 45574462Salfred tcp6transp = svc_vc_create(tcp6sock, 0, 0); 45674462Salfred if (tcp6transp != NULL) { 45774462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1, 45874462Salfred mntsrv, tcp6conf)) 45974462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service"); 46074462Salfred else 46174462Salfred xcreated++; 46274462Salfred if (!force_v2) { 46374462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3, 46474462Salfred mntsrv, tcp6conf)) 46574462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service"); 46674462Salfred else 46774462Salfred xcreated++; 46874462Salfred } 46974462Salfred } else 47074462Salfred syslog(LOG_WARNING, "can't create TCP6 service"); 47174462Salfred 47274462Salfred } 47374462Salfred if (xcreated == 0) { 47474462Salfred syslog(LOG_ERR, "could not create any services"); 4751558Srgrimes exit(1); 4761558Srgrimes } 4771558Srgrimes svc_run(); 47837663Scharnier syslog(LOG_ERR, "mountd died"); 4791558Srgrimes exit(1); 4801558Srgrimes} 4811558Srgrimes 48237663Scharnierstatic void 48337663Scharnierusage() 48437663Scharnier{ 48537663Scharnier fprintf(stderr, 48637663Scharnier "usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n"); 48737663Scharnier exit(1); 48837663Scharnier} 48937663Scharnier 4901558Srgrimes/* 4911558Srgrimes * The mount rpc service 4921558Srgrimes */ 4931558Srgrimesvoid 4941558Srgrimesmntsrv(rqstp, transp) 4951558Srgrimes struct svc_req *rqstp; 4961558Srgrimes SVCXPRT *transp; 4971558Srgrimes{ 4981558Srgrimes struct exportlist *ep; 4991558Srgrimes struct dirlist *dp; 5009336Sdfr struct fhreturn fhr; 5011558Srgrimes struct stat stb; 5021558Srgrimes struct statfs fsb; 50374462Salfred struct addrinfo *ai; 50474462Salfred char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 50574462Salfred int lookup_failed = 1; 50674462Salfred struct sockaddr *saddr; 5079336Sdfr u_short sport; 50823681Speter char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN]; 50928911Sguido int bad = 0, defset, hostset; 5109336Sdfr sigset_t sighup_mask; 5111558Srgrimes 5129336Sdfr sigemptyset(&sighup_mask); 5139336Sdfr sigaddset(&sighup_mask, SIGHUP); 51474462Salfred saddr = svc_getrpccaller(transp)->buf; 51574462Salfred switch (saddr->sa_family) { 51674462Salfred case AF_INET6: 51775635Siedowse sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 51874462Salfred break; 51974462Salfred case AF_INET: 52075635Siedowse sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 52174462Salfred break; 52274462Salfred default: 52374462Salfred syslog(LOG_ERR, "request from unknown address family"); 52474462Salfred return; 52574462Salfred } 52674462Salfred lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, 52774462Salfred NULL, 0, 0); 52874462Salfred getnameinfo(saddr, saddr->sa_len, numerichost, 52974462Salfred sizeof numerichost, NULL, 0, NI_NUMERICHOST); 53074462Salfred ai = NULL; 5311558Srgrimes switch (rqstp->rq_proc) { 5321558Srgrimes case NULLPROC: 5331558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 53437663Scharnier syslog(LOG_ERR, "can't send reply"); 5351558Srgrimes return; 5361558Srgrimes case RPCMNT_MOUNT: 5379336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 53831656Sguido syslog(LOG_NOTICE, 53931656Sguido "mount request from %s from unprivileged port", 54074462Salfred numerichost); 5411558Srgrimes svcerr_weakauth(transp); 5421558Srgrimes return; 5431558Srgrimes } 5441558Srgrimes if (!svc_getargs(transp, xdr_dir, rpcpath)) { 54531656Sguido syslog(LOG_NOTICE, "undecodable mount request from %s", 54674462Salfred numerichost); 5471558Srgrimes svcerr_decode(transp); 5481558Srgrimes return; 5491558Srgrimes } 5501558Srgrimes 5511558Srgrimes /* 5521558Srgrimes * Get the real pathname and make sure it is a directory 5539336Sdfr * or a regular file if the -r option was specified 5549336Sdfr * and it exists. 5551558Srgrimes */ 55651968Salfred if (realpath(rpcpath, dirpath) == NULL || 5571558Srgrimes stat(dirpath, &stb) < 0 || 5589336Sdfr (!S_ISDIR(stb.st_mode) && 55974462Salfred (dir_only || !S_ISREG(stb.st_mode))) || 5601558Srgrimes statfs(dirpath, &fsb) < 0) { 5611558Srgrimes chdir("/"); /* Just in case realpath doesn't */ 56231656Sguido syslog(LOG_NOTICE, 56337663Scharnier "mount request from %s for non existent path %s", 56474462Salfred numerichost, dirpath); 5651558Srgrimes if (debug) 56637663Scharnier warnx("stat failed on %s", dirpath); 56728911Sguido bad = ENOENT; /* We will send error reply later */ 5681558Srgrimes } 5691558Srgrimes 5701558Srgrimes /* Check in the exports list */ 5719336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 5721558Srgrimes ep = ex_search(&fsb.f_fsid); 5739336Sdfr hostset = defset = 0; 5749336Sdfr if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) || 5751558Srgrimes ((dp = dirp_search(ep->ex_dirl, dirpath)) && 57674462Salfred chk_host(dp, saddr, &defset, &hostset)) || 57774462Salfred (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 57874462Salfred scan_tree(ep->ex_dirl, saddr) == 0))) { 57928911Sguido if (bad) { 58028911Sguido if (!svc_sendreply(transp, xdr_long, 58128911Sguido (caddr_t)&bad)) 58237663Scharnier syslog(LOG_ERR, "can't send reply"); 58328911Sguido sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 58428911Sguido return; 58528911Sguido } 5869336Sdfr if (hostset & DP_HOSTSET) 5879336Sdfr fhr.fhr_flag = hostset; 5889336Sdfr else 5899336Sdfr fhr.fhr_flag = defset; 5909336Sdfr fhr.fhr_vers = rqstp->rq_vers; 5911558Srgrimes /* Get the file handle */ 59223681Speter memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 5939336Sdfr if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 5941558Srgrimes bad = errno; 59537663Scharnier syslog(LOG_ERR, "can't get fh for %s", dirpath); 5961558Srgrimes if (!svc_sendreply(transp, xdr_long, 5971558Srgrimes (caddr_t)&bad)) 59837663Scharnier syslog(LOG_ERR, "can't send reply"); 5999336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6001558Srgrimes return; 6011558Srgrimes } 6029336Sdfr if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr)) 60337663Scharnier syslog(LOG_ERR, "can't send reply"); 60474462Salfred if (!lookup_failed) 60574462Salfred add_mlist(host, dirpath); 6061558Srgrimes else 60774462Salfred add_mlist(numerichost, dirpath); 6081558Srgrimes if (debug) 60937663Scharnier warnx("mount successful"); 61031656Sguido if (log) 61131656Sguido syslog(LOG_NOTICE, 61231656Sguido "mount request succeeded from %s for %s", 61374462Salfred numerichost, dirpath); 61431656Sguido } else { 6151558Srgrimes bad = EACCES; 61631656Sguido syslog(LOG_NOTICE, 61731656Sguido "mount request denied from %s for %s", 61874462Salfred numerichost, dirpath); 61931656Sguido } 62028911Sguido 62128911Sguido if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad)) 62237663Scharnier syslog(LOG_ERR, "can't send reply"); 6239336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6241558Srgrimes return; 6251558Srgrimes case RPCMNT_DUMP: 6261558Srgrimes if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL)) 62737663Scharnier syslog(LOG_ERR, "can't send reply"); 62831656Sguido else if (log) 62931656Sguido syslog(LOG_NOTICE, 63031656Sguido "dump request succeeded from %s", 63174462Salfred numerichost); 6321558Srgrimes return; 6331558Srgrimes case RPCMNT_UMOUNT: 6349336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 63531656Sguido syslog(LOG_NOTICE, 63631656Sguido "umount request from %s from unprivileged port", 63774462Salfred numerichost); 6381558Srgrimes svcerr_weakauth(transp); 6391558Srgrimes return; 6401558Srgrimes } 64151968Salfred if (!svc_getargs(transp, xdr_dir, rpcpath)) { 64231656Sguido syslog(LOG_NOTICE, "undecodable umount request from %s", 64374462Salfred numerichost); 6441558Srgrimes svcerr_decode(transp); 6451558Srgrimes return; 6461558Srgrimes } 64751968Salfred if (realpath(rpcpath, dirpath) == NULL) { 64851968Salfred syslog(LOG_NOTICE, "umount request from %s " 64951968Salfred "for non existent path %s", 65074462Salfred numerichost, dirpath); 65151968Salfred } 6521558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 65337663Scharnier syslog(LOG_ERR, "can't send reply"); 65474462Salfred if (!lookup_failed) 65575635Siedowse del_mlist(host, dirpath); 65675635Siedowse del_mlist(numerichost, dirpath); 65731656Sguido if (log) 65831656Sguido syslog(LOG_NOTICE, 65931656Sguido "umount request succeeded from %s for %s", 66074462Salfred numerichost, dirpath); 6611558Srgrimes return; 6621558Srgrimes case RPCMNT_UMNTALL: 6639336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 66431656Sguido syslog(LOG_NOTICE, 66531656Sguido "umountall request from %s from unprivileged port", 66674462Salfred numerichost); 6671558Srgrimes svcerr_weakauth(transp); 6681558Srgrimes return; 6691558Srgrimes } 6701558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 67137663Scharnier syslog(LOG_ERR, "can't send reply"); 67274462Salfred if (!lookup_failed) 67375635Siedowse del_mlist(host, NULL); 67475635Siedowse del_mlist(numerichost, NULL); 67531656Sguido if (log) 67631656Sguido syslog(LOG_NOTICE, 67731656Sguido "umountall request succeeded from %s", 67874462Salfred numerichost); 6791558Srgrimes return; 6801558Srgrimes case RPCMNT_EXPORT: 6811558Srgrimes if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL)) 68237663Scharnier syslog(LOG_ERR, "can't send reply"); 68331656Sguido if (log) 68431656Sguido syslog(LOG_NOTICE, 68531656Sguido "export request succeeded from %s", 68674462Salfred numerichost); 6871558Srgrimes return; 6881558Srgrimes default: 6891558Srgrimes svcerr_noproc(transp); 6901558Srgrimes return; 6911558Srgrimes } 6921558Srgrimes} 6931558Srgrimes 6941558Srgrimes/* 6951558Srgrimes * Xdr conversion for a dirpath string 6961558Srgrimes */ 6971558Srgrimesint 6981558Srgrimesxdr_dir(xdrsp, dirp) 6991558Srgrimes XDR *xdrsp; 7001558Srgrimes char *dirp; 7011558Srgrimes{ 7021558Srgrimes return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 7031558Srgrimes} 7041558Srgrimes 7051558Srgrimes/* 7069336Sdfr * Xdr routine to generate file handle reply 7071558Srgrimes */ 7081558Srgrimesint 7099336Sdfrxdr_fhs(xdrsp, cp) 7101558Srgrimes XDR *xdrsp; 7119336Sdfr caddr_t cp; 7121558Srgrimes{ 7139336Sdfr register struct fhreturn *fhrp = (struct fhreturn *)cp; 7149336Sdfr u_long ok = 0, len, auth; 7151558Srgrimes 7161558Srgrimes if (!xdr_long(xdrsp, &ok)) 7171558Srgrimes return (0); 7189336Sdfr switch (fhrp->fhr_vers) { 7199336Sdfr case 1: 7209336Sdfr return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 7219336Sdfr case 3: 7229336Sdfr len = NFSX_V3FH; 7239336Sdfr if (!xdr_long(xdrsp, &len)) 7249336Sdfr return (0); 7259336Sdfr if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 7269336Sdfr return (0); 7279336Sdfr if (fhrp->fhr_flag & DP_KERB) 7289336Sdfr auth = RPCAUTH_KERB4; 7299336Sdfr else 7309336Sdfr auth = RPCAUTH_UNIX; 7319336Sdfr len = 1; 7329336Sdfr if (!xdr_long(xdrsp, &len)) 7339336Sdfr return (0); 7349336Sdfr return (xdr_long(xdrsp, &auth)); 7359336Sdfr }; 7369336Sdfr return (0); 7371558Srgrimes} 7381558Srgrimes 7391558Srgrimesint 7401558Srgrimesxdr_mlist(xdrsp, cp) 7411558Srgrimes XDR *xdrsp; 7421558Srgrimes caddr_t cp; 7431558Srgrimes{ 7441558Srgrimes struct mountlist *mlp; 7451558Srgrimes int true = 1; 7461558Srgrimes int false = 0; 7471558Srgrimes char *strp; 7481558Srgrimes 7491558Srgrimes mlp = mlhead; 7501558Srgrimes while (mlp) { 7511558Srgrimes if (!xdr_bool(xdrsp, &true)) 7521558Srgrimes return (0); 7531558Srgrimes strp = &mlp->ml_host[0]; 7541558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN)) 7551558Srgrimes return (0); 7561558Srgrimes strp = &mlp->ml_dirp[0]; 7571558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 7581558Srgrimes return (0); 7591558Srgrimes mlp = mlp->ml_next; 7601558Srgrimes } 7611558Srgrimes if (!xdr_bool(xdrsp, &false)) 7621558Srgrimes return (0); 7631558Srgrimes return (1); 7641558Srgrimes} 7651558Srgrimes 7661558Srgrimes/* 7671558Srgrimes * Xdr conversion for export list 7681558Srgrimes */ 7691558Srgrimesint 7701558Srgrimesxdr_explist(xdrsp, cp) 7711558Srgrimes XDR *xdrsp; 7721558Srgrimes caddr_t cp; 7731558Srgrimes{ 7741558Srgrimes struct exportlist *ep; 7751558Srgrimes int false = 0; 7769336Sdfr int putdef; 7779336Sdfr sigset_t sighup_mask; 7781558Srgrimes 7799336Sdfr sigemptyset(&sighup_mask); 7809336Sdfr sigaddset(&sighup_mask, SIGHUP); 7819336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 7821558Srgrimes ep = exphead; 7831558Srgrimes while (ep) { 7841558Srgrimes putdef = 0; 7851558Srgrimes if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef)) 7861558Srgrimes goto errout; 7871558Srgrimes if (ep->ex_defdir && putdef == 0 && 7881558Srgrimes put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 7891558Srgrimes &putdef)) 7901558Srgrimes goto errout; 7911558Srgrimes ep = ep->ex_next; 7921558Srgrimes } 7939336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 7941558Srgrimes if (!xdr_bool(xdrsp, &false)) 7951558Srgrimes return (0); 7961558Srgrimes return (1); 7971558Srgrimeserrout: 7989336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 7991558Srgrimes return (0); 8001558Srgrimes} 8011558Srgrimes 8021558Srgrimes/* 8031558Srgrimes * Called from xdr_explist() to traverse the tree and export the 8041558Srgrimes * directory paths. 8051558Srgrimes */ 8061558Srgrimesint 8071558Srgrimesput_exlist(dp, xdrsp, adp, putdefp) 8081558Srgrimes struct dirlist *dp; 8091558Srgrimes XDR *xdrsp; 8101558Srgrimes struct dirlist *adp; 8111558Srgrimes int *putdefp; 8121558Srgrimes{ 8131558Srgrimes struct grouplist *grp; 8141558Srgrimes struct hostlist *hp; 8151558Srgrimes int true = 1; 8161558Srgrimes int false = 0; 8171558Srgrimes int gotalldir = 0; 8181558Srgrimes char *strp; 8191558Srgrimes 8201558Srgrimes if (dp) { 8211558Srgrimes if (put_exlist(dp->dp_left, xdrsp, adp, putdefp)) 8221558Srgrimes return (1); 8231558Srgrimes if (!xdr_bool(xdrsp, &true)) 8241558Srgrimes return (1); 8251558Srgrimes strp = dp->dp_dirp; 8261558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 8271558Srgrimes return (1); 8281558Srgrimes if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 8291558Srgrimes gotalldir = 1; 8301558Srgrimes *putdefp = 1; 8311558Srgrimes } 8321558Srgrimes if ((dp->dp_flag & DP_DEFSET) == 0 && 8331558Srgrimes (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 8341558Srgrimes hp = dp->dp_hosts; 8351558Srgrimes while (hp) { 8361558Srgrimes grp = hp->ht_grp; 8371558Srgrimes if (grp->gr_type == GT_HOST) { 8381558Srgrimes if (!xdr_bool(xdrsp, &true)) 8391558Srgrimes return (1); 84074462Salfred strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 8418871Srgrimes if (!xdr_string(xdrsp, &strp, 8421558Srgrimes RPCMNT_NAMELEN)) 8431558Srgrimes return (1); 8441558Srgrimes } else if (grp->gr_type == GT_NET) { 8451558Srgrimes if (!xdr_bool(xdrsp, &true)) 8461558Srgrimes return (1); 8471558Srgrimes strp = grp->gr_ptr.gt_net.nt_name; 8488871Srgrimes if (!xdr_string(xdrsp, &strp, 8491558Srgrimes RPCMNT_NAMELEN)) 8501558Srgrimes return (1); 8511558Srgrimes } 8521558Srgrimes hp = hp->ht_next; 8531558Srgrimes if (gotalldir && hp == (struct hostlist *)NULL) { 8541558Srgrimes hp = adp->dp_hosts; 8551558Srgrimes gotalldir = 0; 8561558Srgrimes } 8571558Srgrimes } 8581558Srgrimes } 8591558Srgrimes if (!xdr_bool(xdrsp, &false)) 8601558Srgrimes return (1); 8611558Srgrimes if (put_exlist(dp->dp_right, xdrsp, adp, putdefp)) 8621558Srgrimes return (1); 8631558Srgrimes } 8641558Srgrimes return (0); 8651558Srgrimes} 8661558Srgrimes 8671558Srgrimes#define LINESIZ 10240 8681558Srgrimeschar line[LINESIZ]; 8691558SrgrimesFILE *exp_file; 8701558Srgrimes 8711558Srgrimes/* 8721558Srgrimes * Get the export list 8731558Srgrimes */ 8741558Srgrimesvoid 8751558Srgrimesget_exportlist() 8761558Srgrimes{ 8771558Srgrimes struct exportlist *ep, *ep2; 8781558Srgrimes struct grouplist *grp, *tgrp; 8791558Srgrimes struct exportlist **epp; 8801558Srgrimes struct dirlist *dirhead; 8811558Srgrimes struct statfs fsb, *fsp; 88274462Salfred struct addrinfo *ai; 88372650Sgreen struct xucred anon; 8841558Srgrimes char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 8851558Srgrimes int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp; 8861558Srgrimes 88751968Salfred dirp = NULL; 88851968Salfred dirplen = 0; 88951968Salfred 8901558Srgrimes /* 8911558Srgrimes * First, get rid of the old list 8921558Srgrimes */ 8931558Srgrimes ep = exphead; 8941558Srgrimes while (ep) { 8951558Srgrimes ep2 = ep; 8961558Srgrimes ep = ep->ex_next; 8971558Srgrimes free_exp(ep2); 8981558Srgrimes } 8991558Srgrimes exphead = (struct exportlist *)NULL; 9001558Srgrimes 9011558Srgrimes grp = grphead; 9021558Srgrimes while (grp) { 9031558Srgrimes tgrp = grp; 9041558Srgrimes grp = grp->gr_next; 9051558Srgrimes free_grp(tgrp); 9061558Srgrimes } 9071558Srgrimes grphead = (struct grouplist *)NULL; 9081558Srgrimes 9091558Srgrimes /* 9101558Srgrimes * And delete exports that are in the kernel for all local 9111558Srgrimes * file systems. 9121558Srgrimes * XXX: Should know how to handle all local exportable file systems 91323681Speter * instead of just "ufs". 9141558Srgrimes */ 9151558Srgrimes num = getmntinfo(&fsp, MNT_NOWAIT); 9161558Srgrimes for (i = 0; i < num; i++) { 9171558Srgrimes union { 9181558Srgrimes struct ufs_args ua; 9191558Srgrimes struct iso_args ia; 9201558Srgrimes struct mfs_args ma; 9219336Sdfr struct msdosfs_args da; 92254093Ssemenu struct ntfs_args na; 9231558Srgrimes } targs; 9241558Srgrimes 92523681Speter if (!strcmp(fsp->f_fstypename, "mfs") || 92623681Speter !strcmp(fsp->f_fstypename, "ufs") || 92723681Speter !strcmp(fsp->f_fstypename, "msdos") || 92854093Ssemenu !strcmp(fsp->f_fstypename, "ntfs") || 92923681Speter !strcmp(fsp->f_fstypename, "cd9660")) { 9309336Sdfr targs.ua.fspec = NULL; 9319336Sdfr targs.ua.export.ex_flags = MNT_DELEXPORT; 9329336Sdfr if (mount(fsp->f_fstypename, fsp->f_mntonname, 9331558Srgrimes fsp->f_flags | MNT_UPDATE, 9341558Srgrimes (caddr_t)&targs) < 0) 93537663Scharnier syslog(LOG_ERR, "can't delete exports for %s", 93674462Salfred fsp->f_mntonname); 9371558Srgrimes } 9381558Srgrimes fsp++; 9391558Srgrimes } 9401558Srgrimes 9411558Srgrimes /* 9421558Srgrimes * Read in the exports file and build the list, calling 9431558Srgrimes * mount() as we go along to push the export rules into the kernel. 9441558Srgrimes */ 9451558Srgrimes if ((exp_file = fopen(exname, "r")) == NULL) { 94637663Scharnier syslog(LOG_ERR, "can't open %s", exname); 9471558Srgrimes exit(2); 9481558Srgrimes } 9491558Srgrimes dirhead = (struct dirlist *)NULL; 9501558Srgrimes while (get_line()) { 9511558Srgrimes if (debug) 95237663Scharnier warnx("got line %s", line); 9531558Srgrimes cp = line; 9541558Srgrimes nextfield(&cp, &endcp); 9551558Srgrimes if (*cp == '#') 9561558Srgrimes goto nextline; 9571558Srgrimes 9581558Srgrimes /* 9591558Srgrimes * Set defaults. 9601558Srgrimes */ 9611558Srgrimes has_host = FALSE; 9621558Srgrimes anon = def_anon; 9631558Srgrimes exflags = MNT_EXPORTED; 9641558Srgrimes got_nondir = 0; 9651558Srgrimes opt_flags = 0; 9661558Srgrimes ep = (struct exportlist *)NULL; 9671558Srgrimes 9681558Srgrimes /* 9691558Srgrimes * Create new exports list entry 9701558Srgrimes */ 9711558Srgrimes len = endcp-cp; 9721558Srgrimes tgrp = grp = get_grp(); 9731558Srgrimes while (len > 0) { 9741558Srgrimes if (len > RPCMNT_NAMELEN) { 9751558Srgrimes getexp_err(ep, tgrp); 9761558Srgrimes goto nextline; 9771558Srgrimes } 9781558Srgrimes if (*cp == '-') { 9791558Srgrimes if (ep == (struct exportlist *)NULL) { 9801558Srgrimes getexp_err(ep, tgrp); 9811558Srgrimes goto nextline; 9821558Srgrimes } 9831558Srgrimes if (debug) 98437663Scharnier warnx("doing opt %s", cp); 9851558Srgrimes got_nondir = 1; 9861558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 9871558Srgrimes &exflags, &anon)) { 9881558Srgrimes getexp_err(ep, tgrp); 9891558Srgrimes goto nextline; 9901558Srgrimes } 9911558Srgrimes } else if (*cp == '/') { 9921558Srgrimes savedc = *endcp; 9931558Srgrimes *endcp = '\0'; 9941558Srgrimes if (check_dirpath(cp) && 9951558Srgrimes statfs(cp, &fsb) >= 0) { 9961558Srgrimes if (got_nondir) { 99737663Scharnier syslog(LOG_ERR, "dirs must be first"); 9981558Srgrimes getexp_err(ep, tgrp); 9991558Srgrimes goto nextline; 10001558Srgrimes } 10011558Srgrimes if (ep) { 10021558Srgrimes if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] || 10031558Srgrimes ep->ex_fs.val[1] != fsb.f_fsid.val[1]) { 10041558Srgrimes getexp_err(ep, tgrp); 10051558Srgrimes goto nextline; 10061558Srgrimes } 10071558Srgrimes } else { 10081558Srgrimes /* 10091558Srgrimes * See if this directory is already 10101558Srgrimes * in the list. 10111558Srgrimes */ 10121558Srgrimes ep = ex_search(&fsb.f_fsid); 10131558Srgrimes if (ep == (struct exportlist *)NULL) { 10141558Srgrimes ep = get_exp(); 10151558Srgrimes ep->ex_fs = fsb.f_fsid; 10161558Srgrimes ep->ex_fsdir = (char *) 10171558Srgrimes malloc(strlen(fsb.f_mntonname) + 1); 10181558Srgrimes if (ep->ex_fsdir) 10191558Srgrimes strcpy(ep->ex_fsdir, 10201558Srgrimes fsb.f_mntonname); 10211558Srgrimes else 10221558Srgrimes out_of_mem(); 10231558Srgrimes if (debug) 102474462Salfred warnx("making new ep fs=0x%x,0x%x", 102574462Salfred fsb.f_fsid.val[0], 102674462Salfred fsb.f_fsid.val[1]); 10271558Srgrimes } else if (debug) 102837663Scharnier warnx("found ep fs=0x%x,0x%x", 10291558Srgrimes fsb.f_fsid.val[0], 10301558Srgrimes fsb.f_fsid.val[1]); 10311558Srgrimes } 10321558Srgrimes 10331558Srgrimes /* 10341558Srgrimes * Add dirpath to export mount point. 10351558Srgrimes */ 10361558Srgrimes dirp = add_expdir(&dirhead, cp, len); 10371558Srgrimes dirplen = len; 10381558Srgrimes } else { 10391558Srgrimes getexp_err(ep, tgrp); 10401558Srgrimes goto nextline; 10411558Srgrimes } 10421558Srgrimes *endcp = savedc; 10431558Srgrimes } else { 10441558Srgrimes savedc = *endcp; 10451558Srgrimes *endcp = '\0'; 10461558Srgrimes got_nondir = 1; 10471558Srgrimes if (ep == (struct exportlist *)NULL) { 10481558Srgrimes getexp_err(ep, tgrp); 10491558Srgrimes goto nextline; 10501558Srgrimes } 10511558Srgrimes 10521558Srgrimes /* 10531558Srgrimes * Get the host or netgroup. 10541558Srgrimes */ 10551558Srgrimes setnetgrent(cp); 10561558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 10571558Srgrimes do { 10581558Srgrimes if (has_host) { 10591558Srgrimes grp->gr_next = get_grp(); 10601558Srgrimes grp = grp->gr_next; 10611558Srgrimes } 10621558Srgrimes if (netgrp) { 106337003Sjoerg if (hst == 0) { 106437663Scharnier syslog(LOG_ERR, 106537663Scharnier "null hostname in netgroup %s, skipping", cp); 106637004Sjoerg grp->gr_type = GT_IGNORE; 106737003Sjoerg } else if (get_host(hst, grp, tgrp)) { 106837663Scharnier syslog(LOG_ERR, 106937663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 107029317Sjlemon grp->gr_type = GT_IGNORE; 10711558Srgrimes } 10727401Swpaul } else if (get_host(cp, grp, tgrp)) { 107337663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 107429317Sjlemon grp->gr_type = GT_IGNORE; 10751558Srgrimes } 10761558Srgrimes has_host = TRUE; 10771558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 10781558Srgrimes endnetgrent(); 10791558Srgrimes *endcp = savedc; 10801558Srgrimes } 10811558Srgrimes cp = endcp; 10821558Srgrimes nextfield(&cp, &endcp); 10831558Srgrimes len = endcp - cp; 10841558Srgrimes } 10851558Srgrimes if (check_options(dirhead)) { 10861558Srgrimes getexp_err(ep, tgrp); 10871558Srgrimes goto nextline; 10881558Srgrimes } 10891558Srgrimes if (!has_host) { 10901558Srgrimes grp->gr_type = GT_HOST; 10911558Srgrimes if (debug) 109237663Scharnier warnx("adding a default entry"); 10931558Srgrimes /* add a default group and make the grp list NULL */ 109474462Salfred ai = malloc(sizeof(struct addrinfo)); 109574462Salfred ai->ai_flags = 0; 109674462Salfred ai->ai_family = AF_INET; /* XXXX */ 109774462Salfred ai->ai_socktype = SOCK_DGRAM; 109874462Salfred /* setting the length to 0 will match anything */ 109974462Salfred ai->ai_addrlen = 0; 110074462Salfred ai->ai_flags = AI_CANONNAME; 110174462Salfred ai->ai_canonname = strdup("Default"); 110274462Salfred ai->ai_addr = NULL; 110374462Salfred ai->ai_next = NULL; 110474462Salfred grp->gr_ptr.gt_addrinfo = ai; 11051558Srgrimes 11061558Srgrimes /* 11071558Srgrimes * Don't allow a network export coincide with a list of 11081558Srgrimes * host(s) on the same line. 11091558Srgrimes */ 11101558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 11111558Srgrimes getexp_err(ep, tgrp); 11121558Srgrimes goto nextline; 111329317Sjlemon 111474462Salfred /* 111574462Salfred * If an export list was specified on this line, make sure 111629317Sjlemon * that we have at least one valid entry, otherwise skip it. 111729317Sjlemon */ 111829317Sjlemon } else { 111929317Sjlemon grp = tgrp; 112074462Salfred while (grp && grp->gr_type == GT_IGNORE) 112129317Sjlemon grp = grp->gr_next; 112229317Sjlemon if (! grp) { 112329317Sjlemon getexp_err(ep, tgrp); 112429317Sjlemon goto nextline; 112529317Sjlemon } 11261558Srgrimes } 11271558Srgrimes 11281558Srgrimes /* 11291558Srgrimes * Loop through hosts, pushing the exports into the kernel. 11301558Srgrimes * After loop, tgrp points to the start of the list and 11311558Srgrimes * grp points to the last entry in the list. 11321558Srgrimes */ 11331558Srgrimes grp = tgrp; 11341558Srgrimes do { 113575635Siedowse if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 113675635Siedowse &fsb)) { 113775635Siedowse getexp_err(ep, tgrp); 113875635Siedowse goto nextline; 113975635Siedowse } 11401558Srgrimes } while (grp->gr_next && (grp = grp->gr_next)); 11411558Srgrimes 11421558Srgrimes /* 11431558Srgrimes * Success. Update the data structures. 11441558Srgrimes */ 11451558Srgrimes if (has_host) { 11469336Sdfr hang_dirp(dirhead, tgrp, ep, opt_flags); 11471558Srgrimes grp->gr_next = grphead; 11481558Srgrimes grphead = tgrp; 11491558Srgrimes } else { 11501558Srgrimes hang_dirp(dirhead, (struct grouplist *)NULL, ep, 11519336Sdfr opt_flags); 11521558Srgrimes free_grp(grp); 11531558Srgrimes } 11541558Srgrimes dirhead = (struct dirlist *)NULL; 11551558Srgrimes if ((ep->ex_flag & EX_LINKED) == 0) { 11561558Srgrimes ep2 = exphead; 11571558Srgrimes epp = &exphead; 11581558Srgrimes 11591558Srgrimes /* 11601558Srgrimes * Insert in the list in alphabetical order. 11611558Srgrimes */ 11621558Srgrimes while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { 11631558Srgrimes epp = &ep2->ex_next; 11641558Srgrimes ep2 = ep2->ex_next; 11651558Srgrimes } 11661558Srgrimes if (ep2) 11671558Srgrimes ep->ex_next = ep2; 11681558Srgrimes *epp = ep; 11691558Srgrimes ep->ex_flag |= EX_LINKED; 11701558Srgrimes } 11711558Srgrimesnextline: 11721558Srgrimes if (dirhead) { 11731558Srgrimes free_dir(dirhead); 11741558Srgrimes dirhead = (struct dirlist *)NULL; 11751558Srgrimes } 11761558Srgrimes } 11771558Srgrimes fclose(exp_file); 11781558Srgrimes} 11791558Srgrimes 11801558Srgrimes/* 11811558Srgrimes * Allocate an export list element 11821558Srgrimes */ 11831558Srgrimesstruct exportlist * 11841558Srgrimesget_exp() 11851558Srgrimes{ 11861558Srgrimes struct exportlist *ep; 11871558Srgrimes 11881558Srgrimes ep = (struct exportlist *)malloc(sizeof (struct exportlist)); 11891558Srgrimes if (ep == (struct exportlist *)NULL) 11901558Srgrimes out_of_mem(); 119123681Speter memset(ep, 0, sizeof(struct exportlist)); 11921558Srgrimes return (ep); 11931558Srgrimes} 11941558Srgrimes 11951558Srgrimes/* 11961558Srgrimes * Allocate a group list element 11971558Srgrimes */ 11981558Srgrimesstruct grouplist * 11991558Srgrimesget_grp() 12001558Srgrimes{ 12011558Srgrimes struct grouplist *gp; 12021558Srgrimes 12031558Srgrimes gp = (struct grouplist *)malloc(sizeof (struct grouplist)); 12041558Srgrimes if (gp == (struct grouplist *)NULL) 12051558Srgrimes out_of_mem(); 120623681Speter memset(gp, 0, sizeof(struct grouplist)); 12071558Srgrimes return (gp); 12081558Srgrimes} 12091558Srgrimes 12101558Srgrimes/* 12111558Srgrimes * Clean up upon an error in get_exportlist(). 12121558Srgrimes */ 12131558Srgrimesvoid 12141558Srgrimesgetexp_err(ep, grp) 12151558Srgrimes struct exportlist *ep; 12161558Srgrimes struct grouplist *grp; 12171558Srgrimes{ 12181558Srgrimes struct grouplist *tgrp; 12191558Srgrimes 122037663Scharnier syslog(LOG_ERR, "bad exports list line %s", line); 12211558Srgrimes if (ep && (ep->ex_flag & EX_LINKED) == 0) 12221558Srgrimes free_exp(ep); 12231558Srgrimes while (grp) { 12241558Srgrimes tgrp = grp; 12251558Srgrimes grp = grp->gr_next; 12261558Srgrimes free_grp(tgrp); 12271558Srgrimes } 12281558Srgrimes} 12291558Srgrimes 12301558Srgrimes/* 12311558Srgrimes * Search the export list for a matching fs. 12321558Srgrimes */ 12331558Srgrimesstruct exportlist * 12341558Srgrimesex_search(fsid) 12351558Srgrimes fsid_t *fsid; 12361558Srgrimes{ 12371558Srgrimes struct exportlist *ep; 12381558Srgrimes 12391558Srgrimes ep = exphead; 12401558Srgrimes while (ep) { 12411558Srgrimes if (ep->ex_fs.val[0] == fsid->val[0] && 12421558Srgrimes ep->ex_fs.val[1] == fsid->val[1]) 12431558Srgrimes return (ep); 12441558Srgrimes ep = ep->ex_next; 12451558Srgrimes } 12461558Srgrimes return (ep); 12471558Srgrimes} 12481558Srgrimes 12491558Srgrimes/* 12501558Srgrimes * Add a directory path to the list. 12511558Srgrimes */ 12521558Srgrimeschar * 12531558Srgrimesadd_expdir(dpp, cp, len) 12541558Srgrimes struct dirlist **dpp; 12551558Srgrimes char *cp; 12561558Srgrimes int len; 12571558Srgrimes{ 12581558Srgrimes struct dirlist *dp; 12591558Srgrimes 12601558Srgrimes dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); 126137663Scharnier if (dp == (struct dirlist *)NULL) 126237663Scharnier out_of_mem(); 12631558Srgrimes dp->dp_left = *dpp; 12641558Srgrimes dp->dp_right = (struct dirlist *)NULL; 12651558Srgrimes dp->dp_flag = 0; 12661558Srgrimes dp->dp_hosts = (struct hostlist *)NULL; 12671558Srgrimes strcpy(dp->dp_dirp, cp); 12681558Srgrimes *dpp = dp; 12691558Srgrimes return (dp->dp_dirp); 12701558Srgrimes} 12711558Srgrimes 12721558Srgrimes/* 12731558Srgrimes * Hang the dir list element off the dirpath binary tree as required 12741558Srgrimes * and update the entry for host. 12751558Srgrimes */ 12761558Srgrimesvoid 12779336Sdfrhang_dirp(dp, grp, ep, flags) 12781558Srgrimes struct dirlist *dp; 12791558Srgrimes struct grouplist *grp; 12801558Srgrimes struct exportlist *ep; 12819336Sdfr int flags; 12821558Srgrimes{ 12831558Srgrimes struct hostlist *hp; 12841558Srgrimes struct dirlist *dp2; 12851558Srgrimes 12869336Sdfr if (flags & OP_ALLDIRS) { 12871558Srgrimes if (ep->ex_defdir) 12881558Srgrimes free((caddr_t)dp); 12891558Srgrimes else 12901558Srgrimes ep->ex_defdir = dp; 12919336Sdfr if (grp == (struct grouplist *)NULL) { 12921558Srgrimes ep->ex_defdir->dp_flag |= DP_DEFSET; 12939336Sdfr if (flags & OP_KERB) 12949336Sdfr ep->ex_defdir->dp_flag |= DP_KERB; 12959336Sdfr } else while (grp) { 12961558Srgrimes hp = get_ht(); 12979336Sdfr if (flags & OP_KERB) 12989336Sdfr hp->ht_flag |= DP_KERB; 12991558Srgrimes hp->ht_grp = grp; 13001558Srgrimes hp->ht_next = ep->ex_defdir->dp_hosts; 13011558Srgrimes ep->ex_defdir->dp_hosts = hp; 13021558Srgrimes grp = grp->gr_next; 13031558Srgrimes } 13041558Srgrimes } else { 13051558Srgrimes 13061558Srgrimes /* 130737663Scharnier * Loop through the directories adding them to the tree. 13081558Srgrimes */ 13091558Srgrimes while (dp) { 13101558Srgrimes dp2 = dp->dp_left; 13119336Sdfr add_dlist(&ep->ex_dirl, dp, grp, flags); 13121558Srgrimes dp = dp2; 13131558Srgrimes } 13141558Srgrimes } 13151558Srgrimes} 13161558Srgrimes 13171558Srgrimes/* 13181558Srgrimes * Traverse the binary tree either updating a node that is already there 13191558Srgrimes * for the new directory or adding the new node. 13201558Srgrimes */ 13211558Srgrimesvoid 13229336Sdfradd_dlist(dpp, newdp, grp, flags) 13231558Srgrimes struct dirlist **dpp; 13241558Srgrimes struct dirlist *newdp; 13251558Srgrimes struct grouplist *grp; 13269336Sdfr int flags; 13271558Srgrimes{ 13281558Srgrimes struct dirlist *dp; 13291558Srgrimes struct hostlist *hp; 13301558Srgrimes int cmp; 13311558Srgrimes 13321558Srgrimes dp = *dpp; 13331558Srgrimes if (dp) { 13341558Srgrimes cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 13351558Srgrimes if (cmp > 0) { 13369336Sdfr add_dlist(&dp->dp_left, newdp, grp, flags); 13371558Srgrimes return; 13381558Srgrimes } else if (cmp < 0) { 13399336Sdfr add_dlist(&dp->dp_right, newdp, grp, flags); 13401558Srgrimes return; 13411558Srgrimes } else 13421558Srgrimes free((caddr_t)newdp); 13431558Srgrimes } else { 13441558Srgrimes dp = newdp; 13451558Srgrimes dp->dp_left = (struct dirlist *)NULL; 13461558Srgrimes *dpp = dp; 13471558Srgrimes } 13481558Srgrimes if (grp) { 13491558Srgrimes 13501558Srgrimes /* 13511558Srgrimes * Hang all of the host(s) off of the directory point. 13521558Srgrimes */ 13531558Srgrimes do { 13541558Srgrimes hp = get_ht(); 13559336Sdfr if (flags & OP_KERB) 13569336Sdfr hp->ht_flag |= DP_KERB; 13571558Srgrimes hp->ht_grp = grp; 13581558Srgrimes hp->ht_next = dp->dp_hosts; 13591558Srgrimes dp->dp_hosts = hp; 13601558Srgrimes grp = grp->gr_next; 13611558Srgrimes } while (grp); 13629336Sdfr } else { 13631558Srgrimes dp->dp_flag |= DP_DEFSET; 13649336Sdfr if (flags & OP_KERB) 13659336Sdfr dp->dp_flag |= DP_KERB; 13669336Sdfr } 13671558Srgrimes} 13681558Srgrimes 13691558Srgrimes/* 13701558Srgrimes * Search for a dirpath on the export point. 13711558Srgrimes */ 13721558Srgrimesstruct dirlist * 137374462Salfreddirp_search(dp, dirp) 13741558Srgrimes struct dirlist *dp; 137574462Salfred char *dirp; 13761558Srgrimes{ 13771558Srgrimes int cmp; 13781558Srgrimes 13791558Srgrimes if (dp) { 138074462Salfred cmp = strcmp(dp->dp_dirp, dirp); 13811558Srgrimes if (cmp > 0) 138274462Salfred return (dirp_search(dp->dp_left, dirp)); 13831558Srgrimes else if (cmp < 0) 138474462Salfred return (dirp_search(dp->dp_right, dirp)); 13851558Srgrimes else 13861558Srgrimes return (dp); 13871558Srgrimes } 13881558Srgrimes return (dp); 13891558Srgrimes} 13901558Srgrimes 13911558Srgrimes/* 139274462Salfred * Some helper functions for netmasks. They all assume masks in network 139374462Salfred * order (big endian). 139474462Salfred */ 139574462Salfredstatic int 139674462Salfredbitcmp(void *dst, void *src, int bitlen) 139774462Salfred{ 139874462Salfred int i; 139974462Salfred u_int8_t *p1 = dst, *p2 = src; 140074462Salfred u_int8_t bitmask; 140174462Salfred int bytelen, bitsleft; 140274462Salfred 140374462Salfred bytelen = bitlen / 8; 140474462Salfred bitsleft = bitlen % 8; 140574462Salfred 140674462Salfred if (debug) { 140774462Salfred printf("comparing:\n"); 140874462Salfred for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++) 140974462Salfred printf("%02x", p1[i]); 141074462Salfred printf("\n"); 141174462Salfred for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++) 141274462Salfred printf("%02x", p2[i]); 141374462Salfred printf("\n"); 141474462Salfred } 141574462Salfred 141674462Salfred for (i = 0; i < bytelen; i++) { 141774462Salfred if (*p1 != *p2) 141874462Salfred return 1; 141974462Salfred p1++; 142074462Salfred p2++; 142174462Salfred } 142274462Salfred 142374462Salfred for (i = 0; i < bitsleft; i++) { 142474462Salfred bitmask = 1 << (7 - i); 142574462Salfred if ((*p1 & bitmask) != (*p2 & bitmask)) 142674462Salfred return 1; 142774462Salfred } 142874462Salfred 142974462Salfred return 0; 143074462Salfred} 143174462Salfred 143274462Salfred/* 14331558Srgrimes * Scan for a host match in a directory tree. 14341558Srgrimes */ 14351558Srgrimesint 14369336Sdfrchk_host(dp, saddr, defsetp, hostsetp) 14371558Srgrimes struct dirlist *dp; 143874462Salfred struct sockaddr *saddr; 14391558Srgrimes int *defsetp; 14409336Sdfr int *hostsetp; 14411558Srgrimes{ 14421558Srgrimes struct hostlist *hp; 14431558Srgrimes struct grouplist *grp; 144474462Salfred struct addrinfo *ai; 14451558Srgrimes 14461558Srgrimes if (dp) { 14471558Srgrimes if (dp->dp_flag & DP_DEFSET) 14489336Sdfr *defsetp = dp->dp_flag; 14491558Srgrimes hp = dp->dp_hosts; 14501558Srgrimes while (hp) { 14511558Srgrimes grp = hp->ht_grp; 14521558Srgrimes switch (grp->gr_type) { 14531558Srgrimes case GT_HOST: 145474462Salfred ai = grp->gr_ptr.gt_addrinfo; 145574462Salfred for (; ai; ai = ai->ai_next) { 145674462Salfred if (!sacmp(ai->ai_addr, saddr)) { 145774462Salfred *hostsetp = 145874462Salfred (hp->ht_flag | DP_HOSTSET); 145974462Salfred return (1); 146074462Salfred } 14619336Sdfr } 14621558Srgrimes break; 14631558Srgrimes case GT_NET: 146474462Salfred if (!netpartcmp(saddr, 146574462Salfred (struct sockaddr *) &grp->gr_ptr.gt_net.nt_net, 146674462Salfred grp->gr_ptr.gt_net.nt_mask)) { 146774462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 146874462Salfred return (1); 146974462Salfred } 14701558Srgrimes break; 14711558Srgrimes }; 14721558Srgrimes hp = hp->ht_next; 14731558Srgrimes } 14741558Srgrimes } 14751558Srgrimes return (0); 14761558Srgrimes} 14771558Srgrimes 14781558Srgrimes/* 14791558Srgrimes * Scan tree for a host that matches the address. 14801558Srgrimes */ 14811558Srgrimesint 14821558Srgrimesscan_tree(dp, saddr) 14831558Srgrimes struct dirlist *dp; 148474462Salfred struct sockaddr *saddr; 14851558Srgrimes{ 14869336Sdfr int defset, hostset; 14871558Srgrimes 14881558Srgrimes if (dp) { 14891558Srgrimes if (scan_tree(dp->dp_left, saddr)) 14901558Srgrimes return (1); 14919336Sdfr if (chk_host(dp, saddr, &defset, &hostset)) 14921558Srgrimes return (1); 14931558Srgrimes if (scan_tree(dp->dp_right, saddr)) 14941558Srgrimes return (1); 14951558Srgrimes } 14961558Srgrimes return (0); 14971558Srgrimes} 14981558Srgrimes 14991558Srgrimes/* 15001558Srgrimes * Traverse the dirlist tree and free it up. 15011558Srgrimes */ 15021558Srgrimesvoid 15031558Srgrimesfree_dir(dp) 15041558Srgrimes struct dirlist *dp; 15051558Srgrimes{ 15061558Srgrimes 15071558Srgrimes if (dp) { 15081558Srgrimes free_dir(dp->dp_left); 15091558Srgrimes free_dir(dp->dp_right); 15101558Srgrimes free_host(dp->dp_hosts); 15111558Srgrimes free((caddr_t)dp); 15121558Srgrimes } 15131558Srgrimes} 15141558Srgrimes 15151558Srgrimes/* 15161558Srgrimes * Parse the option string and update fields. 15171558Srgrimes * Option arguments may either be -<option>=<value> or 15181558Srgrimes * -<option> <value> 15191558Srgrimes */ 15201558Srgrimesint 15211558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr) 15221558Srgrimes char **cpp, **endcpp; 15231558Srgrimes struct exportlist *ep; 15241558Srgrimes struct grouplist *grp; 15251558Srgrimes int *has_hostp; 15261558Srgrimes int *exflagsp; 152772650Sgreen struct xucred *cr; 15281558Srgrimes{ 15291558Srgrimes char *cpoptarg, *cpoptend; 15301558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 15311558Srgrimes int allflag, usedarg; 15321558Srgrimes 153351968Salfred savedc2 = '\0'; 15341558Srgrimes cpopt = *cpp; 15351558Srgrimes cpopt++; 15361558Srgrimes cp = *endcpp; 15371558Srgrimes savedc = *cp; 15381558Srgrimes *cp = '\0'; 15391558Srgrimes while (cpopt && *cpopt) { 15401558Srgrimes allflag = 1; 15411558Srgrimes usedarg = -2; 154237663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 15431558Srgrimes *cpoptend++ = '\0'; 154437663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 15451558Srgrimes *cpoptarg++ = '\0'; 15461558Srgrimes } else { 154737663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 15481558Srgrimes *cpoptarg++ = '\0'; 15491558Srgrimes else { 15501558Srgrimes *cp = savedc; 15511558Srgrimes nextfield(&cp, &endcp); 15521558Srgrimes **endcpp = '\0'; 15531558Srgrimes if (endcp > cp && *cp != '-') { 15541558Srgrimes cpoptarg = cp; 15551558Srgrimes savedc2 = *endcp; 15561558Srgrimes *endcp = '\0'; 15571558Srgrimes usedarg = 0; 15581558Srgrimes } 15591558Srgrimes } 15601558Srgrimes } 15611558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 15621558Srgrimes *exflagsp |= MNT_EXRDONLY; 15631558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 15641558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 15651558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 15661558Srgrimes usedarg++; 15671558Srgrimes parsecred(cpoptarg, cr); 15681558Srgrimes if (allflag == 0) { 15691558Srgrimes *exflagsp |= MNT_EXPORTANON; 15701558Srgrimes opt_flags |= OP_MAPALL; 15711558Srgrimes } else 15721558Srgrimes opt_flags |= OP_MAPROOT; 15731558Srgrimes } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) { 15741558Srgrimes *exflagsp |= MNT_EXKERB; 15751558Srgrimes opt_flags |= OP_KERB; 15761558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 15771558Srgrimes !strcmp(cpopt, "m"))) { 15781558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 157937663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 15801558Srgrimes return (1); 15811558Srgrimes } 15821558Srgrimes usedarg++; 15831558Srgrimes opt_flags |= OP_MASK; 15841558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 15851558Srgrimes !strcmp(cpopt, "n"))) { 158674462Salfred if (strchr(cpoptarg, '/') != NULL) { 158774462Salfred if (debug) 158874462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 158974462Salfred opt_flags |= OP_MASKLEN; 159074462Salfred } 15911558Srgrimes if (grp->gr_type != GT_NULL) { 159237663Scharnier syslog(LOG_ERR, "network/host conflict"); 15931558Srgrimes return (1); 15941558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 159537663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 15961558Srgrimes return (1); 15971558Srgrimes } 15981558Srgrimes grp->gr_type = GT_NET; 15991558Srgrimes *has_hostp = 1; 16001558Srgrimes usedarg++; 16011558Srgrimes opt_flags |= OP_NET; 16021558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 16031558Srgrimes opt_flags |= OP_ALLDIRS; 160427447Sdfr } else if (!strcmp(cpopt, "public")) { 160527447Sdfr *exflagsp |= MNT_EXPUBLIC; 160627447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 160727447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 160827447Sdfr opt_flags |= OP_MAPALL; 160927447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 161027447Sdfr ep->ex_indexfile = strdup(cpoptarg); 16111558Srgrimes } else { 161237663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 16131558Srgrimes return (1); 16141558Srgrimes } 16151558Srgrimes if (usedarg >= 0) { 16161558Srgrimes *endcp = savedc2; 16171558Srgrimes **endcpp = savedc; 16181558Srgrimes if (usedarg > 0) { 16191558Srgrimes *cpp = cp; 16201558Srgrimes *endcpp = endcp; 16211558Srgrimes } 16221558Srgrimes return (0); 16231558Srgrimes } 16241558Srgrimes cpopt = cpoptend; 16251558Srgrimes } 16261558Srgrimes **endcpp = savedc; 16271558Srgrimes return (0); 16281558Srgrimes} 16291558Srgrimes 16301558Srgrimes/* 16311558Srgrimes * Translate a character string to the corresponding list of network 16321558Srgrimes * addresses for a hostname. 16331558Srgrimes */ 16341558Srgrimesint 16357401Swpaulget_host(cp, grp, tgrp) 16361558Srgrimes char *cp; 16371558Srgrimes struct grouplist *grp; 16387401Swpaul struct grouplist *tgrp; 16391558Srgrimes{ 16407401Swpaul struct grouplist *checkgrp; 164175635Siedowse struct addrinfo *ai, *tai, hints; 164274462Salfred int ecode; 164374462Salfred char host[NI_MAXHOST]; 16441558Srgrimes 164574462Salfred if (grp->gr_type != GT_NULL) { 164674462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 16471558Srgrimes return (1); 16481558Srgrimes } 164974462Salfred memset(&hints, 0, sizeof hints); 165074462Salfred hints.ai_flags = AI_CANONNAME; 165174462Salfred hints.ai_protocol = IPPROTO_UDP; 165274462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 165374462Salfred if (ecode != 0) { 165475635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 165574462Salfred return 1; 165674462Salfred } 165774462Salfred grp->gr_ptr.gt_addrinfo = ai; 165874462Salfred while (ai != NULL) { 165974462Salfred if (ai->ai_canonname == NULL) { 166074462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 166174462Salfred sizeof host, NULL, 0, ninumeric) != 0) 166274462Salfred strlcpy(host, "?", sizeof(host)); 166374462Salfred ai->ai_canonname = strdup(host); 166474462Salfred ai->ai_flags |= AI_CANONNAME; 166574462Salfred } else 166674462Salfred ai->ai_flags &= ~AI_CANONNAME; 166774462Salfred if (debug) 166875635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 166975635Siedowse /* 167075635Siedowse * Sanity check: make sure we don't already have an entry 167175635Siedowse * for this host in the grouplist. 167275635Siedowse */ 167375635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 167475635Siedowse checkgrp = checkgrp->gr_next) { 167575635Siedowse if (checkgrp->gr_type != GT_HOST) 167675635Siedowse continue; 167775635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 167875635Siedowse tai = tai->ai_next) { 167975635Siedowse if (sacmp(tai->ai_addr, ai->ai_addr) != 0) 168075635Siedowse continue; 168175635Siedowse if (debug) 168275635Siedowse fprintf(stderr, 168375635Siedowse "ignoring duplicate host %s\n", 168475635Siedowse ai->ai_canonname); 168575635Siedowse grp->gr_type = GT_IGNORE; 168675635Siedowse return (0); 168775635Siedowse } 168875635Siedowse } 168974462Salfred ai = ai->ai_next; 16901558Srgrimes } 169175635Siedowse grp->gr_type = GT_HOST; 16921558Srgrimes return (0); 16931558Srgrimes} 16941558Srgrimes 16951558Srgrimes/* 16961558Srgrimes * Free up an exports list component 16971558Srgrimes */ 16981558Srgrimesvoid 16991558Srgrimesfree_exp(ep) 17001558Srgrimes struct exportlist *ep; 17011558Srgrimes{ 17021558Srgrimes 17031558Srgrimes if (ep->ex_defdir) { 17041558Srgrimes free_host(ep->ex_defdir->dp_hosts); 17051558Srgrimes free((caddr_t)ep->ex_defdir); 17061558Srgrimes } 17071558Srgrimes if (ep->ex_fsdir) 17081558Srgrimes free(ep->ex_fsdir); 170927447Sdfr if (ep->ex_indexfile) 171027447Sdfr free(ep->ex_indexfile); 17111558Srgrimes free_dir(ep->ex_dirl); 17121558Srgrimes free((caddr_t)ep); 17131558Srgrimes} 17141558Srgrimes 17151558Srgrimes/* 17161558Srgrimes * Free hosts. 17171558Srgrimes */ 17181558Srgrimesvoid 17191558Srgrimesfree_host(hp) 17201558Srgrimes struct hostlist *hp; 17211558Srgrimes{ 17221558Srgrimes struct hostlist *hp2; 17231558Srgrimes 17241558Srgrimes while (hp) { 17251558Srgrimes hp2 = hp; 17261558Srgrimes hp = hp->ht_next; 17271558Srgrimes free((caddr_t)hp2); 17281558Srgrimes } 17291558Srgrimes} 17301558Srgrimes 17311558Srgrimesstruct hostlist * 17321558Srgrimesget_ht() 17331558Srgrimes{ 17341558Srgrimes struct hostlist *hp; 17351558Srgrimes 17361558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 17371558Srgrimes if (hp == (struct hostlist *)NULL) 17381558Srgrimes out_of_mem(); 17391558Srgrimes hp->ht_next = (struct hostlist *)NULL; 17409336Sdfr hp->ht_flag = 0; 17411558Srgrimes return (hp); 17421558Srgrimes} 17431558Srgrimes 17441558Srgrimes/* 17451558Srgrimes * Out of memory, fatal 17461558Srgrimes */ 17471558Srgrimesvoid 17481558Srgrimesout_of_mem() 17491558Srgrimes{ 17501558Srgrimes 175137663Scharnier syslog(LOG_ERR, "out of memory"); 17521558Srgrimes exit(2); 17531558Srgrimes} 17541558Srgrimes 17551558Srgrimes/* 17561558Srgrimes * Do the mount syscall with the update flag to push the export info into 17571558Srgrimes * the kernel. 17581558Srgrimes */ 17591558Srgrimesint 17601558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb) 17611558Srgrimes struct exportlist *ep; 17621558Srgrimes struct grouplist *grp; 17631558Srgrimes int exflags; 176472650Sgreen struct xucred *anoncrp; 17651558Srgrimes char *dirp; 17661558Srgrimes int dirplen; 17671558Srgrimes struct statfs *fsb; 17681558Srgrimes{ 176974462Salfred struct sockaddr *addrp; 177074462Salfred struct sockaddr_storage ss; 177174462Salfred struct addrinfo *ai; 177274462Salfred int addrlen; 177374462Salfred char *cp = NULL; 17741558Srgrimes int done; 17751558Srgrimes char savedc = '\0'; 17761558Srgrimes union { 17771558Srgrimes struct ufs_args ua; 17781558Srgrimes struct iso_args ia; 17791558Srgrimes struct mfs_args ma; 17809336Sdfr#ifdef __NetBSD__ 17819336Sdfr struct msdosfs_args da; 178274462Salfred struct adosfs_args aa; 17839336Sdfr#endif 178454093Ssemenu struct ntfs_args na; 17851558Srgrimes } args; 17861558Srgrimes 178775635Siedowse ai = NULL; 178875635Siedowse addrlen = 0; 17891558Srgrimes args.ua.fspec = 0; 17901558Srgrimes args.ua.export.ex_flags = exflags; 17911558Srgrimes args.ua.export.ex_anon = *anoncrp; 179227447Sdfr args.ua.export.ex_indexfile = ep->ex_indexfile; 179374462Salfred if (grp->gr_type == GT_HOST) { 179474462Salfred ai = grp->gr_ptr.gt_addrinfo; 179574462Salfred addrp = ai->ai_addr; 179674462Salfred addrlen = ai->ai_addrlen; 179774462Salfred } else 179874462Salfred addrp = NULL; 17991558Srgrimes done = FALSE; 18001558Srgrimes while (!done) { 18011558Srgrimes switch (grp->gr_type) { 18021558Srgrimes case GT_HOST: 180374462Salfred if (addrp != NULL && addrp->sa_family == AF_INET6 && 180474462Salfred have_v6 == 0) 180574462Salfred goto skip; 180674462Salfred args.ua.export.ex_addr = addrp; 180774462Salfred args.ua.export.ex_addrlen = addrlen; 18081558Srgrimes args.ua.export.ex_masklen = 0; 18091558Srgrimes break; 18101558Srgrimes case GT_NET: 181174462Salfred args.ua.export.ex_addr = (struct sockaddr *) 181274462Salfred &grp->gr_ptr.gt_net.nt_net; 181374462Salfred if (args.ua.export.ex_addr->sa_family == AF_INET6 && 181474462Salfred have_v6 == 0) 181574462Salfred goto skip; 181674462Salfred args.ua.export.ex_addrlen = 181774462Salfred args.ua.export.ex_addr->sa_len; 181874462Salfred memset(&ss, 0, sizeof ss); 181974462Salfred ss.ss_family = args.ua.export.ex_addr->sa_family; 182074462Salfred ss.ss_len = args.ua.export.ex_addr->sa_len; 182174462Salfred if (allones(&ss, grp->gr_ptr.gt_net.nt_mask) != 0) { 182274462Salfred syslog(LOG_ERR, "Bad network flag"); 182374462Salfred if (cp) 182474462Salfred *cp = savedc; 182574462Salfred return (1); 18261558Srgrimes } 182774462Salfred args.ua.export.ex_mask = (struct sockaddr *)&ss; 182874462Salfred args.ua.export.ex_masklen = ss.ss_len; 18291558Srgrimes break; 18307401Swpaul case GT_IGNORE: 18317401Swpaul return(0); 18327401Swpaul break; 18331558Srgrimes default: 183437663Scharnier syslog(LOG_ERR, "bad grouptype"); 18351558Srgrimes if (cp) 18361558Srgrimes *cp = savedc; 18371558Srgrimes return (1); 18381558Srgrimes }; 18391558Srgrimes 18401558Srgrimes /* 18411558Srgrimes * XXX: 18421558Srgrimes * Maybe I should just use the fsb->f_mntonname path instead 18431558Srgrimes * of looping back up the dirp to the mount point?? 18441558Srgrimes * Also, needs to know how to export all types of local 184523681Speter * exportable file systems and not just "ufs". 18461558Srgrimes */ 18479336Sdfr while (mount(fsb->f_fstypename, dirp, 184874462Salfred fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) { 18491558Srgrimes if (cp) 18501558Srgrimes *cp-- = savedc; 18511558Srgrimes else 18521558Srgrimes cp = dirp + dirplen - 1; 18531558Srgrimes if (errno == EPERM) { 185475635Siedowse if (debug) 185575635Siedowse warnx("can't change attributes for %s", 185675635Siedowse dirp); 18571558Srgrimes syslog(LOG_ERR, 185837663Scharnier "can't change attributes for %s", dirp); 18591558Srgrimes return (1); 18601558Srgrimes } 18611558Srgrimes if (opt_flags & OP_ALLDIRS) { 186237663Scharnier syslog(LOG_ERR, "could not remount %s: %m", 18634895Swollman dirp); 18641558Srgrimes return (1); 18651558Srgrimes } 18661558Srgrimes /* back up over the last component */ 18671558Srgrimes while (*cp == '/' && cp > dirp) 18681558Srgrimes cp--; 18691558Srgrimes while (*(cp - 1) != '/' && cp > dirp) 18701558Srgrimes cp--; 18711558Srgrimes if (cp == dirp) { 18721558Srgrimes if (debug) 187337663Scharnier warnx("mnt unsucc"); 187437663Scharnier syslog(LOG_ERR, "can't export %s", dirp); 18751558Srgrimes return (1); 18761558Srgrimes } 18771558Srgrimes savedc = *cp; 18781558Srgrimes *cp = '\0'; 18791558Srgrimes } 188074462Salfredskip: 18811558Srgrimes if (addrp) { 188274462Salfred ai = ai->ai_next; 188374462Salfred if (ai == NULL) 18841558Srgrimes done = TRUE; 188574462Salfred else { 188674462Salfred addrp = ai->ai_addr; 188774462Salfred addrlen = ai->ai_addrlen; 188874462Salfred } 18891558Srgrimes } else 18901558Srgrimes done = TRUE; 18911558Srgrimes } 18921558Srgrimes if (cp) 18931558Srgrimes *cp = savedc; 18941558Srgrimes return (0); 18951558Srgrimes} 18961558Srgrimes 18971558Srgrimes/* 18981558Srgrimes * Translate a net address. 18991558Srgrimes */ 19001558Srgrimesint 19011558Srgrimesget_net(cp, net, maskflg) 19021558Srgrimes char *cp; 19031558Srgrimes struct netmsk *net; 19041558Srgrimes int maskflg; 19051558Srgrimes{ 19061558Srgrimes struct netent *np; 190774462Salfred char *name, *p, *prefp; 190874462Salfred struct sockaddr_in sin, *sinp; 190974462Salfred struct sockaddr *sa; 191074462Salfred struct addrinfo hints, *ai = NULL; 191174462Salfred char netname[NI_MAXHOST]; 191274462Salfred long preflen; 191374462Salfred int ecode; 19141558Srgrimes 191575635Siedowse p = prefp = NULL; 191674462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 191774462Salfred p = strchr(cp, '/'); 191874462Salfred *p = '\0'; 191974462Salfred prefp = p + 1; 192074462Salfred } 192174462Salfred 192274462Salfred if ((np = getnetbyname(cp)) != NULL) { 192374462Salfred sin.sin_family = AF_INET; 192474462Salfred sin.sin_len = sizeof sin; 192574462Salfred sin.sin_addr = inet_makeaddr(np->n_net, 0); 192674462Salfred sa = (struct sockaddr *)&sin; 192774462Salfred } else if (isdigit(*cp)) { 192874462Salfred memset(&hints, 0, sizeof hints); 192974462Salfred hints.ai_family = AF_UNSPEC; 193074462Salfred hints.ai_flags = AI_NUMERICHOST; 193174462Salfred if (getaddrinfo(cp, NULL, &hints, &ai) != 0) { 193274462Salfred /* 193374462Salfred * If getaddrinfo() failed, try the inet4 network 193474462Salfred * notation with less than 3 dots. 193574462Salfred */ 193674462Salfred sin.sin_family = AF_INET; 193774462Salfred sin.sin_len = sizeof sin; 193874462Salfred sin.sin_addr = inet_makeaddr(inet_network(cp),0); 193974462Salfred if (debug) 194074462Salfred fprintf(stderr, "get_net: v4 addr %x\n", 194174462Salfred sin.sin_addr.s_addr); 194274462Salfred sa = (struct sockaddr *)&sin; 194374462Salfred } else 194474462Salfred sa = ai->ai_addr; 194574462Salfred } else if (isxdigit(*cp) || *cp == ':') { 194674462Salfred memset(&hints, 0, sizeof hints); 194774462Salfred hints.ai_family = AF_UNSPEC; 194874462Salfred hints.ai_flags = AI_NUMERICHOST; 194974462Salfred if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 195074462Salfred sa = ai->ai_addr; 195174462Salfred else 195274462Salfred goto fail; 19531558Srgrimes } else 195474462Salfred goto fail; 195525318Spst 195674462Salfred ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname, 195774462Salfred NULL, 0, ninumeric); 195874462Salfred if (ecode != 0) 195974462Salfred goto fail; 196074462Salfred 19611558Srgrimes if (maskflg) 196274462Salfred net->nt_mask = countones(sa); 19631558Srgrimes else { 196474462Salfred if (opt_flags & OP_MASKLEN) { 196574462Salfred preflen = strtol(prefp, NULL, 10); 196674462Salfred if (preflen == LONG_MIN && errno == ERANGE) 196774462Salfred goto fail; 196874462Salfred net->nt_mask = (int)preflen; 196974462Salfred *p = '/'; 197074462Salfred } 197174462Salfred 19721558Srgrimes if (np) 19731558Srgrimes name = np->n_name; 197474462Salfred else { 197574462Salfred if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 197674462Salfred NULL, 0, ninumeric) != 0) 197774462Salfred strlcpy(netname, "?", sizeof(netname)); 197874462Salfred name = netname; 197974462Salfred } 198074462Salfred net->nt_name = strdup(name); 198174462Salfred memcpy(&net->nt_net, sa, sa->sa_len); 198274462Salfred } 198374462Salfred 198474462Salfred if (!maskflg && sa->sa_family == AF_INET && 198574462Salfred !(opt_flags & (OP_MASK|OP_MASKLEN))) { 198674462Salfred sinp = (struct sockaddr_in *)sa; 198774462Salfred if (IN_CLASSA(sinp->sin_addr.s_addr)) 198874462Salfred net->nt_mask = 8; 198974462Salfred else if (IN_CLASSB(sinp->sin_addr.s_addr)) 199074462Salfred net->nt_mask = 16; 199174462Salfred else if (IN_CLASSC(sinp->sin_addr.s_addr)) 199274462Salfred net->nt_mask = 24; 199374462Salfred else if (IN_CLASSD(sinp->sin_addr.s_addr)) 199474462Salfred net->nt_mask = 28; 19951558Srgrimes else 199674462Salfred net->nt_mask = 32; /* XXX */ 19971558Srgrimes } 199874462Salfred 199974462Salfred if (ai) 200074462Salfred freeaddrinfo(ai); 200174462Salfred return 0; 200274462Salfred 200374462Salfredfail: 200474462Salfred if (ai) 200574462Salfred freeaddrinfo(ai); 200674462Salfred return 1; 20071558Srgrimes} 20081558Srgrimes 20091558Srgrimes/* 20101558Srgrimes * Parse out the next white space separated field 20111558Srgrimes */ 20121558Srgrimesvoid 20131558Srgrimesnextfield(cp, endcp) 20141558Srgrimes char **cp; 20151558Srgrimes char **endcp; 20161558Srgrimes{ 20171558Srgrimes char *p; 20181558Srgrimes 20191558Srgrimes p = *cp; 20201558Srgrimes while (*p == ' ' || *p == '\t') 20211558Srgrimes p++; 20221558Srgrimes if (*p == '\n' || *p == '\0') 20231558Srgrimes *cp = *endcp = p; 20241558Srgrimes else { 20251558Srgrimes *cp = p++; 20261558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 20271558Srgrimes p++; 20281558Srgrimes *endcp = p; 20291558Srgrimes } 20301558Srgrimes} 20311558Srgrimes 20321558Srgrimes/* 20331558Srgrimes * Get an exports file line. Skip over blank lines and handle line 20341558Srgrimes * continuations. 20351558Srgrimes */ 20361558Srgrimesint 20371558Srgrimesget_line() 20381558Srgrimes{ 20391558Srgrimes char *p, *cp; 20401558Srgrimes int len; 20411558Srgrimes int totlen, cont_line; 20421558Srgrimes 20431558Srgrimes /* 20441558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 20451558Srgrimes */ 20461558Srgrimes p = line; 20471558Srgrimes totlen = 0; 20481558Srgrimes do { 20491558Srgrimes if (fgets(p, LINESIZ - totlen, exp_file) == NULL) 20501558Srgrimes return (0); 20511558Srgrimes len = strlen(p); 20521558Srgrimes cp = p + len - 1; 20531558Srgrimes cont_line = 0; 20541558Srgrimes while (cp >= p && 20551558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 20561558Srgrimes if (*cp == '\\') 20571558Srgrimes cont_line = 1; 20581558Srgrimes cp--; 20591558Srgrimes len--; 20601558Srgrimes } 20611558Srgrimes *++cp = '\0'; 20621558Srgrimes if (len > 0) { 20631558Srgrimes totlen += len; 20641558Srgrimes if (totlen >= LINESIZ) { 206537663Scharnier syslog(LOG_ERR, "exports line too long"); 20661558Srgrimes exit(2); 20671558Srgrimes } 20681558Srgrimes p = cp; 20691558Srgrimes } 20701558Srgrimes } while (totlen == 0 || cont_line); 20711558Srgrimes return (1); 20721558Srgrimes} 20731558Srgrimes 20741558Srgrimes/* 20751558Srgrimes * Parse a description of a credential. 20761558Srgrimes */ 20771558Srgrimesvoid 20781558Srgrimesparsecred(namelist, cr) 20791558Srgrimes char *namelist; 208072650Sgreen struct xucred *cr; 20811558Srgrimes{ 20821558Srgrimes char *name; 20831558Srgrimes int cnt; 20841558Srgrimes char *names; 20851558Srgrimes struct passwd *pw; 20861558Srgrimes struct group *gr; 20871558Srgrimes int ngroups, groups[NGROUPS + 1]; 20881558Srgrimes 20891558Srgrimes /* 209037663Scharnier * Set up the unprivileged user. 20911558Srgrimes */ 20921558Srgrimes cr->cr_uid = -2; 20931558Srgrimes cr->cr_groups[0] = -2; 20941558Srgrimes cr->cr_ngroups = 1; 20951558Srgrimes /* 20961558Srgrimes * Get the user's password table entry. 20971558Srgrimes */ 20981558Srgrimes names = strsep(&namelist, " \t\n"); 20991558Srgrimes name = strsep(&names, ":"); 21001558Srgrimes if (isdigit(*name) || *name == '-') 21011558Srgrimes pw = getpwuid(atoi(name)); 21021558Srgrimes else 21031558Srgrimes pw = getpwnam(name); 21041558Srgrimes /* 21051558Srgrimes * Credentials specified as those of a user. 21061558Srgrimes */ 21071558Srgrimes if (names == NULL) { 21081558Srgrimes if (pw == NULL) { 210937663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 21101558Srgrimes return; 21111558Srgrimes } 21121558Srgrimes cr->cr_uid = pw->pw_uid; 21131558Srgrimes ngroups = NGROUPS + 1; 21141558Srgrimes if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 211537663Scharnier syslog(LOG_ERR, "too many groups"); 21161558Srgrimes /* 21171558Srgrimes * Convert from int's to gid_t's and compress out duplicate 21181558Srgrimes */ 21191558Srgrimes cr->cr_ngroups = ngroups - 1; 21201558Srgrimes cr->cr_groups[0] = groups[0]; 21211558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 21221558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 21231558Srgrimes return; 21241558Srgrimes } 21251558Srgrimes /* 21261558Srgrimes * Explicit credential specified as a colon separated list: 21271558Srgrimes * uid:gid:gid:... 21281558Srgrimes */ 21291558Srgrimes if (pw != NULL) 21301558Srgrimes cr->cr_uid = pw->pw_uid; 21311558Srgrimes else if (isdigit(*name) || *name == '-') 21321558Srgrimes cr->cr_uid = atoi(name); 21331558Srgrimes else { 213437663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 21351558Srgrimes return; 21361558Srgrimes } 21371558Srgrimes cr->cr_ngroups = 0; 21381558Srgrimes while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) { 21391558Srgrimes name = strsep(&names, ":"); 21401558Srgrimes if (isdigit(*name) || *name == '-') { 21411558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 21421558Srgrimes } else { 21431558Srgrimes if ((gr = getgrnam(name)) == NULL) { 214437663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 21451558Srgrimes continue; 21461558Srgrimes } 21471558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 21481558Srgrimes } 21491558Srgrimes } 21501558Srgrimes if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS) 215137663Scharnier syslog(LOG_ERR, "too many groups"); 21521558Srgrimes} 21531558Srgrimes 21541558Srgrimes#define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50) 21551558Srgrimes/* 21561558Srgrimes * Routines that maintain the remote mounttab 21571558Srgrimes */ 21581558Srgrimesvoid 21591558Srgrimesget_mountlist() 21601558Srgrimes{ 21611558Srgrimes struct mountlist *mlp, **mlpp; 216223681Speter char *host, *dirp, *cp; 21631558Srgrimes char str[STRSIZ]; 21641558Srgrimes FILE *mlfile; 21651558Srgrimes 21661558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 216753117Sbillf if (errno == ENOENT) 216853117Sbillf return; 216953117Sbillf else { 217053117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 217153117Sbillf return; 217253117Sbillf } 21731558Srgrimes } 21741558Srgrimes mlpp = &mlhead; 21751558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 217623681Speter cp = str; 217723681Speter host = strsep(&cp, " \t\n"); 217823681Speter dirp = strsep(&cp, " \t\n"); 217923681Speter if (host == NULL || dirp == NULL) 21801558Srgrimes continue; 21811558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 218237663Scharnier if (mlp == (struct mountlist *)NULL) 218337663Scharnier out_of_mem(); 218423681Speter strncpy(mlp->ml_host, host, RPCMNT_NAMELEN); 218523681Speter mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 218623681Speter strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 218723681Speter mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 21881558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 21891558Srgrimes *mlpp = mlp; 21901558Srgrimes mlpp = &mlp->ml_next; 21911558Srgrimes } 21921558Srgrimes fclose(mlfile); 21931558Srgrimes} 21941558Srgrimes 219575635Siedowsevoid 219675635Siedowsedel_mlist(char *hostp, char *dirp) 21971558Srgrimes{ 21981558Srgrimes struct mountlist *mlp, **mlpp; 21991558Srgrimes struct mountlist *mlp2; 22001558Srgrimes FILE *mlfile; 22011558Srgrimes int fnd = 0; 22021558Srgrimes 22031558Srgrimes mlpp = &mlhead; 22041558Srgrimes mlp = mlhead; 22051558Srgrimes while (mlp) { 22061558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 22071558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 22081558Srgrimes fnd = 1; 22091558Srgrimes mlp2 = mlp; 22101558Srgrimes *mlpp = mlp = mlp->ml_next; 22111558Srgrimes free((caddr_t)mlp2); 22121558Srgrimes } else { 22131558Srgrimes mlpp = &mlp->ml_next; 22141558Srgrimes mlp = mlp->ml_next; 22151558Srgrimes } 22161558Srgrimes } 22171558Srgrimes if (fnd) { 22181558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 221937663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 22201558Srgrimes return; 22211558Srgrimes } 22221558Srgrimes mlp = mlhead; 22231558Srgrimes while (mlp) { 22241558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 22251558Srgrimes mlp = mlp->ml_next; 22261558Srgrimes } 22271558Srgrimes fclose(mlfile); 22281558Srgrimes } 22291558Srgrimes} 22301558Srgrimes 22311558Srgrimesvoid 22321558Srgrimesadd_mlist(hostp, dirp) 22331558Srgrimes char *hostp, *dirp; 22341558Srgrimes{ 22351558Srgrimes struct mountlist *mlp, **mlpp; 22361558Srgrimes FILE *mlfile; 22371558Srgrimes 22381558Srgrimes mlpp = &mlhead; 22391558Srgrimes mlp = mlhead; 22401558Srgrimes while (mlp) { 22411558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 22421558Srgrimes return; 22431558Srgrimes mlpp = &mlp->ml_next; 22441558Srgrimes mlp = mlp->ml_next; 22451558Srgrimes } 22461558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 224737663Scharnier if (mlp == (struct mountlist *)NULL) 224837663Scharnier out_of_mem(); 22491558Srgrimes strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN); 22501558Srgrimes mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 22511558Srgrimes strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 22521558Srgrimes mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 22531558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 22541558Srgrimes *mlpp = mlp; 22551558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 225637663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 22571558Srgrimes return; 22581558Srgrimes } 22591558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 22601558Srgrimes fclose(mlfile); 22611558Srgrimes} 22621558Srgrimes 22631558Srgrimes/* 22641558Srgrimes * Free up a group list. 22651558Srgrimes */ 22661558Srgrimesvoid 22671558Srgrimesfree_grp(grp) 22681558Srgrimes struct grouplist *grp; 22691558Srgrimes{ 22701558Srgrimes if (grp->gr_type == GT_HOST) { 227174462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 227274462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 22731558Srgrimes } else if (grp->gr_type == GT_NET) { 22741558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 22751558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 22761558Srgrimes } 22771558Srgrimes free((caddr_t)grp); 22781558Srgrimes} 22791558Srgrimes 22801558Srgrimes#ifdef DEBUG 22811558Srgrimesvoid 22821558SrgrimesSYSLOG(int pri, const char *fmt, ...) 22831558Srgrimes{ 22841558Srgrimes va_list ap; 22851558Srgrimes 22861558Srgrimes va_start(ap, fmt); 22871558Srgrimes vfprintf(stderr, fmt, ap); 22881558Srgrimes va_end(ap); 22891558Srgrimes} 22901558Srgrimes#endif /* DEBUG */ 22911558Srgrimes 22921558Srgrimes/* 22931558Srgrimes * Check options for consistency. 22941558Srgrimes */ 22951558Srgrimesint 22961558Srgrimescheck_options(dp) 22971558Srgrimes struct dirlist *dp; 22981558Srgrimes{ 22991558Srgrimes 23001558Srgrimes if (dp == (struct dirlist *)NULL) 23011558Srgrimes return (1); 23021558Srgrimes if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) || 23031558Srgrimes (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) || 23041558Srgrimes (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) { 23051558Srgrimes syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive"); 23061558Srgrimes return (1); 23071558Srgrimes } 23081558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 23091558Srgrimes syslog(LOG_ERR, "-mask requires -net"); 23101558Srgrimes return (1); 23111558Srgrimes } 23121558Srgrimes if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 231345927Salex syslog(LOG_ERR, "-alldirs has multiple directories"); 23141558Srgrimes return (1); 23151558Srgrimes } 23161558Srgrimes return (0); 23171558Srgrimes} 23181558Srgrimes 23191558Srgrimes/* 23201558Srgrimes * Check an absolute directory path for any symbolic links. Return true 23211558Srgrimes */ 23221558Srgrimesint 23231558Srgrimescheck_dirpath(dirp) 23241558Srgrimes char *dirp; 23251558Srgrimes{ 23261558Srgrimes char *cp; 23271558Srgrimes int ret = 1; 23281558Srgrimes struct stat sb; 23291558Srgrimes 23301558Srgrimes cp = dirp + 1; 23311558Srgrimes while (*cp && ret) { 23321558Srgrimes if (*cp == '/') { 23331558Srgrimes *cp = '\0'; 23349336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23351558Srgrimes ret = 0; 23361558Srgrimes *cp = '/'; 23371558Srgrimes } 23381558Srgrimes cp++; 23391558Srgrimes } 23409336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23411558Srgrimes ret = 0; 23421558Srgrimes return (ret); 23431558Srgrimes} 23449336Sdfr 234574462Salfredstatic int 234674462Salfrednetpartcmp(struct sockaddr *s1, struct sockaddr *s2, int bitlen) 234774462Salfred{ 234874462Salfred void *src, *dst; 234974462Salfred 235074462Salfred if (s1->sa_family != s2->sa_family) 235174462Salfred return 1; 235274462Salfred 235374462Salfred switch (s1->sa_family) { 235474462Salfred case AF_INET: 235574462Salfred src = &((struct sockaddr_in *)s1)->sin_addr; 235674462Salfred dst = &((struct sockaddr_in *)s2)->sin_addr; 235774462Salfred if (bitlen > sizeof(((struct sockaddr_in *)s1)->sin_addr) * 8) 235874462Salfred return 1; 235974462Salfred break; 236074462Salfred case AF_INET6: 236174462Salfred src = &((struct sockaddr_in6 *)s1)->sin6_addr; 236274462Salfred dst = &((struct sockaddr_in6 *)s2)->sin6_addr; 236374462Salfred if (((struct sockaddr_in6 *)s1)->sin6_scope_id != 236474462Salfred ((struct sockaddr_in6 *)s2)->sin6_scope_id) 236574462Salfred return 1; 236674462Salfred if (bitlen > sizeof(((struct sockaddr_in6 *)s1)->sin6_addr) * 8) 236774462Salfred return 1; 236874462Salfred break; 236974462Salfred default: 237074462Salfred return 1; 237174462Salfred } 237274462Salfred 237374462Salfred return bitcmp(src, dst, bitlen); 237474462Salfred} 237574462Salfred 237674462Salfredstatic int 237774462Salfredallones(struct sockaddr_storage *ssp, int bitlen) 237874462Salfred{ 237974462Salfred u_int8_t *p; 238074462Salfred int bytelen, bitsleft, i; 238174462Salfred int zerolen; 238274462Salfred 238374462Salfred switch (ssp->ss_family) { 238474462Salfred case AF_INET: 238574462Salfred p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr; 238674462Salfred zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr); 238774462Salfred break; 238874462Salfred case AF_INET6: 238974462Salfred p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr; 239074462Salfred zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr); 239174462Salfred break; 239274462Salfred default: 239374462Salfred return -1; 239474462Salfred } 239574462Salfred 239674462Salfred memset(p, 0, zerolen); 239774462Salfred 239874462Salfred bytelen = bitlen / 8; 239974462Salfred bitsleft = bitlen % 8; 240074462Salfred 240174462Salfred if (bytelen > zerolen) 240274462Salfred return -1; 240374462Salfred 240474462Salfred for (i = 0; i < bytelen; i++) 240574462Salfred *p++ = 0xff; 240674462Salfred 240774462Salfred for (i = 0; i < bitsleft; i++) 240874462Salfred *p |= 1 << (7 - i); 240974462Salfred 241074462Salfred return 0; 241174462Salfred} 241274462Salfred 241374462Salfredstatic int 241474462Salfredcountones(struct sockaddr *sa) 241574462Salfred{ 241674462Salfred void *mask; 241774462Salfred int i, bits = 0, bytelen; 241874462Salfred u_int8_t *p; 241974462Salfred 242074462Salfred switch (sa->sa_family) { 242174462Salfred case AF_INET: 242274462Salfred mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr; 242374462Salfred bytelen = 4; 242474462Salfred break; 242574462Salfred case AF_INET6: 242674462Salfred mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr; 242774462Salfred bytelen = 16; 242874462Salfred break; 242974462Salfred default: 243074462Salfred return 0; 243174462Salfred } 243274462Salfred 243374462Salfred p = mask; 243474462Salfred 243574462Salfred for (i = 0; i < bytelen; i++, p++) { 243674462Salfred if (*p != 0xff) { 243774462Salfred for (bits = 0; bits < 8; bits++) { 243874462Salfred if (!(*p & (1 << (7 - bits)))) 243974462Salfred break; 244074462Salfred } 244174462Salfred break; 244274462Salfred } 244374462Salfred } 244474462Salfred 244574462Salfred return (i * 8 + bits); 244674462Salfred} 244774462Salfred 244874462Salfredstatic int 244974462Salfredsacmp(struct sockaddr *sa1, struct sockaddr *sa2) 245074462Salfred{ 245174462Salfred void *p1, *p2; 245274462Salfred int len; 245374462Salfred 245474462Salfred if (sa1->sa_family != sa2->sa_family) 245574462Salfred return 1; 245674462Salfred 245774462Salfred switch (sa1->sa_family) { 245874462Salfred case AF_INET: 245974462Salfred p1 = &((struct sockaddr_in *)sa1)->sin_addr; 246074462Salfred p2 = &((struct sockaddr_in *)sa2)->sin_addr; 246174462Salfred len = 4; 246274462Salfred break; 246374462Salfred case AF_INET6: 246474462Salfred p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr; 246574462Salfred p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr; 246674462Salfred len = 16; 246774462Salfred if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 246874462Salfred ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 246974462Salfred return 1; 247074462Salfred break; 247174462Salfred default: 247274462Salfred return 1; 247374462Salfred } 247474462Salfred 247574462Salfred return memcmp(p1, p2, len); 247674462Salfred} 247774462Salfred 247874462Salfredvoid terminate(sig) 247974462Salfredint sig; 248074462Salfred{ 248174462Salfred close(mountdlockfd); 248274462Salfred unlink(MOUNTDLOCK); 248374792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 248474792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 248574462Salfred exit (0); 248674462Salfred} 2487