mountd.c revision 75861
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 75861 2001-04-23 10:12:31Z 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; 12875801Siedowse struct sockaddr_storage 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 14675641Siedowse#define GT_DEFAULT 0x3 1477401Swpaul#define GT_IGNORE 0x5 1481558Srgrimes 1491558Srgrimesstruct hostlist { 1509336Sdfr int ht_flag; /* Uses DP_xx bits */ 1511558Srgrimes struct grouplist *ht_grp; 1521558Srgrimes struct hostlist *ht_next; 1531558Srgrimes}; 1541558Srgrimes 1559336Sdfrstruct fhreturn { 1569336Sdfr int fhr_flag; 1579336Sdfr int fhr_vers; 1589336Sdfr nfsfh_t fhr_fh; 1599336Sdfr}; 1609336Sdfr 1611558Srgrimes/* Global defs */ 1621558Srgrimeschar *add_expdir __P((struct dirlist **, char *, int)); 1631558Srgrimesvoid add_dlist __P((struct dirlist **, struct dirlist *, 1649336Sdfr struct grouplist *, int)); 1651558Srgrimesvoid add_mlist __P((char *, char *)); 1661558Srgrimesint check_dirpath __P((char *)); 1671558Srgrimesint check_options __P((struct dirlist *)); 16875801Siedowseint checkmask(struct sockaddr *sa); 16974462Salfredint chk_host __P((struct dirlist *, struct sockaddr *, int *, int *)); 17075635Siedowsevoid del_mlist(char *hostp, char *dirp); 1711558Srgrimesstruct dirlist *dirp_search __P((struct dirlist *, char *)); 1721558Srgrimesint do_mount __P((struct exportlist *, struct grouplist *, int, 17372650Sgreen struct xucred *, char *, int, struct statfs *)); 1741558Srgrimesint do_opt __P((char **, char **, struct exportlist *, struct grouplist *, 17572650Sgreen int *, int *, struct xucred *)); 1761558Srgrimesstruct exportlist *ex_search __P((fsid_t *)); 1771558Srgrimesstruct exportlist *get_exp __P((void)); 1781558Srgrimesvoid free_dir __P((struct dirlist *)); 1791558Srgrimesvoid free_exp __P((struct exportlist *)); 1801558Srgrimesvoid free_grp __P((struct grouplist *)); 1811558Srgrimesvoid free_host __P((struct hostlist *)); 1821558Srgrimesvoid get_exportlist __P((void)); 1837401Swpaulint get_host __P((char *, struct grouplist *, struct grouplist *)); 1841558Srgrimesstruct hostlist *get_ht __P((void)); 1851558Srgrimesint get_line __P((void)); 1861558Srgrimesvoid get_mountlist __P((void)); 1871558Srgrimesint get_net __P((char *, struct netmsk *, int)); 1881558Srgrimesvoid getexp_err __P((struct exportlist *, struct grouplist *)); 1891558Srgrimesstruct grouplist *get_grp __P((void)); 1901558Srgrimesvoid hang_dirp __P((struct dirlist *, struct grouplist *, 1911558Srgrimes struct exportlist *, int)); 19275754Siedowsevoid huphandler(int sig); 19375801Siedowseint makemask(struct sockaddr_storage *ssp, int bitlen); 1941558Srgrimesvoid mntsrv __P((struct svc_req *, SVCXPRT *)); 1951558Srgrimesvoid nextfield __P((char **, char **)); 1961558Srgrimesvoid out_of_mem __P((void)); 19772650Sgreenvoid parsecred __P((char *, struct xucred *)); 1981558Srgrimesint put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *)); 19975801Siedowsevoid *sa_rawaddr(struct sockaddr *sa, int *nbytes); 20075801Siedowseint sacmp(struct sockaddr *sa1, struct sockaddr *sa2, 20175801Siedowse struct sockaddr *samask); 20274462Salfredint scan_tree __P((struct dirlist *, struct sockaddr *)); 20337663Scharnierstatic void usage __P((void)); 2041558Srgrimesint xdr_dir __P((XDR *, char *)); 2051558Srgrimesint xdr_explist __P((XDR *, caddr_t)); 2069336Sdfrint xdr_fhs __P((XDR *, caddr_t)); 2071558Srgrimesint xdr_mlist __P((XDR *, caddr_t)); 20874462Salfredvoid terminate __P((int)); 2091558Srgrimes 2101558Srgrimesstruct exportlist *exphead; 2111558Srgrimesstruct mountlist *mlhead; 2121558Srgrimesstruct grouplist *grphead; 2131558Srgrimeschar exname[MAXPATHLEN]; 21472650Sgreenstruct xucred def_anon = { 21572650Sgreen 0, 21672650Sgreen (uid_t)-2, 2171558Srgrimes 1, 21872650Sgreen { (gid_t)-2 }, 21972650Sgreen NULL 2201558Srgrimes}; 22125087Sdfrint force_v2 = 0; 2229336Sdfrint resvport_only = 1; 2239336Sdfrint dir_only = 1; 22431705Sguidoint log = 0; 22575754Siedowseint got_sighup = 0; 22674462Salfred 2271558Srgrimesint opt_flags; 22874462Salfredstatic int have_v6 = 1; 22974462Salfred#ifdef NI_WITHSCOPEID 23074462Salfredstatic const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID; 23174462Salfred#else 23274462Salfredstatic const int ninumeric = NI_NUMERICHOST; 23374462Salfred#endif 23474462Salfred 23574462Salfredint mountdlockfd; 23675801Siedowse/* Bits for opt_flags above */ 2371558Srgrimes#define OP_MAPROOT 0x01 2381558Srgrimes#define OP_MAPALL 0x02 2391558Srgrimes#define OP_KERB 0x04 2401558Srgrimes#define OP_MASK 0x08 2411558Srgrimes#define OP_NET 0x10 2421558Srgrimes#define OP_ALLDIRS 0x40 24375801Siedowse#define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */ 24474462Salfred#define OP_MASKLEN 0x200 2451558Srgrimes 2461558Srgrimes#ifdef DEBUG 2471558Srgrimesint debug = 1; 2481558Srgrimesvoid SYSLOG __P((int, const char *, ...)); 2491558Srgrimes#define syslog SYSLOG 2501558Srgrimes#else 2511558Srgrimesint debug = 0; 2521558Srgrimes#endif 2531558Srgrimes 2541558Srgrimes/* 2551558Srgrimes * Mountd server for NFS mount protocol as described in: 2561558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A 2571558Srgrimes * The optional arguments are the exports file name 2581558Srgrimes * default: _PATH_EXPORTS 2591558Srgrimes * and "-n" to allow nonroot mount. 2601558Srgrimes */ 2611558Srgrimesint 2621558Srgrimesmain(argc, argv) 2631558Srgrimes int argc; 2641558Srgrimes char **argv; 2651558Srgrimes{ 26675754Siedowse fd_set readfds; 26774462Salfred SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp; 26874462Salfred struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf; 26974462Salfred int udpsock, tcpsock, udp6sock, tcp6sock; 27074462Salfred int xcreated = 0, s; 27174462Salfred int one = 1; 27232656Sbde int c, error, mib[3]; 27323681Speter struct vfsconf vfc; 2741558Srgrimes 27575635Siedowse udp6conf = tcp6conf = NULL; 27675635Siedowse udp6sock = tcp6sock = NULL; 27775635Siedowse 27874462Salfred /* Check that another mountd isn't already running. */ 27974462Salfred if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) 28074462Salfred err(1, "%s", MOUNTDLOCK); 28174462Salfred 28274462Salfred if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) 28374462Salfred errx(1, "another rpc.mountd is already running. Aborting"); 28474462Salfred s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 28574462Salfred if (s < 0) 28674462Salfred have_v6 = 0; 28774462Salfred else 28874462Salfred close(s); 28923681Speter error = getvfsbyname("nfs", &vfc); 29023681Speter if (error && vfsisloadable("nfs")) { 2912999Swollman if(vfsload("nfs")) 2922999Swollman err(1, "vfsload(nfs)"); 2932999Swollman endvfsent(); /* flush cache */ 29423681Speter error = getvfsbyname("nfs", &vfc); 2952999Swollman } 29623681Speter if (error) 2972999Swollman errx(1, "NFS support is not available in the running kernel"); 2982999Swollman 29931665Sguido while ((c = getopt(argc, argv, "2dlnr")) != -1) 3001558Srgrimes switch (c) { 30125087Sdfr case '2': 30225087Sdfr force_v2 = 1; 30325087Sdfr break; 3049336Sdfr case 'n': 3059336Sdfr resvport_only = 0; 3069336Sdfr break; 3079336Sdfr case 'r': 3089336Sdfr dir_only = 0; 3099336Sdfr break; 3108688Sphk case 'd': 3118688Sphk debug = debug ? 0 : 1; 3128688Sphk break; 31331656Sguido case 'l': 31431656Sguido log = 1; 31531656Sguido break; 3161558Srgrimes default: 31737663Scharnier usage(); 3181558Srgrimes }; 3191558Srgrimes argc -= optind; 3201558Srgrimes argv += optind; 3211558Srgrimes grphead = (struct grouplist *)NULL; 3221558Srgrimes exphead = (struct exportlist *)NULL; 3231558Srgrimes mlhead = (struct mountlist *)NULL; 3241558Srgrimes if (argc == 1) { 3251558Srgrimes strncpy(exname, *argv, MAXPATHLEN-1); 3261558Srgrimes exname[MAXPATHLEN-1] = '\0'; 3271558Srgrimes } else 3281558Srgrimes strcpy(exname, _PATH_EXPORTS); 3291558Srgrimes openlog("mountd", LOG_PID, LOG_DAEMON); 3301558Srgrimes if (debug) 33137663Scharnier warnx("getting export list"); 3321558Srgrimes get_exportlist(); 3331558Srgrimes if (debug) 33437663Scharnier warnx("getting mount list"); 3351558Srgrimes get_mountlist(); 3361558Srgrimes if (debug) 33737663Scharnier warnx("here we go"); 3381558Srgrimes if (debug == 0) { 3391558Srgrimes daemon(0, 0); 3401558Srgrimes signal(SIGINT, SIG_IGN); 3411558Srgrimes signal(SIGQUIT, SIG_IGN); 3421558Srgrimes } 34375754Siedowse signal(SIGHUP, huphandler); 34474462Salfred signal(SIGTERM, terminate); 3451558Srgrimes { FILE *pidfile = fopen(_PATH_MOUNTDPID, "w"); 3461558Srgrimes if (pidfile != NULL) { 3471558Srgrimes fprintf(pidfile, "%d\n", getpid()); 3481558Srgrimes fclose(pidfile); 3491558Srgrimes } 3501558Srgrimes } 35174462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 35274462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 35374462Salfred udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 35474462Salfred tcpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 35574791Salfred udpconf = getnetconfigent("udp"); 35674791Salfred tcpconf = getnetconfigent("tcp"); 35774791Salfred if (!have_v6) 35874791Salfred goto skip_v6; 35974462Salfred udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 36074462Salfred tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 36174462Salfred /* 36274462Salfred * We're doing host-based access checks here, so don't allow 36374462Salfred * v4-in-v6 to confuse things. The kernel will disable it 36474462Salfred * by default on NFS sockets too. 36574462Salfred */ 36674462Salfred if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6, 36774462Salfred IPV6_BINDV6ONLY, &one, sizeof one) < 0){ 36874462Salfred syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket"); 36974462Salfred exit(1); 37074462Salfred } 37174462Salfred if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6, 37274462Salfred IPV6_BINDV6ONLY, &one, sizeof one) < 0){ 37374462Salfred syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket"); 37474462Salfred exit(1); 37574462Salfred } 37674462Salfred udp6conf = getnetconfigent("udp6"); 37774462Salfred tcp6conf = getnetconfigent("tcp6"); 37874791Salfred 37974791Salfredskip_v6: 38024759Sguido if (!resvport_only) { 38124759Sguido mib[0] = CTL_VFS; 38232656Sbde mib[1] = vfc.vfc_typenum; 38324759Sguido mib[2] = NFS_NFSPRIVPORT; 38424759Sguido if (sysctl(mib, 3, NULL, NULL, &resvport_only, 38524759Sguido sizeof(resvport_only)) != 0 && errno != ENOENT) { 38624759Sguido syslog(LOG_ERR, "sysctl: %m"); 38724759Sguido exit(1); 38824759Sguido } 38924330Sguido } 3909202Srgrimes if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL || 3919202Srgrimes (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) { 39237663Scharnier syslog(LOG_ERR, "can't create socket"); 3931558Srgrimes exit(1); 3941558Srgrimes } 39574462Salfred if (udpsock != -1 && udpconf != NULL) { 39674462Salfred bindresvport(udpsock, NULL); 39774462Salfred udptransp = svc_dg_create(udpsock, 0, 0); 39874462Salfred if (udptransp != NULL) { 39974462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1, 40074462Salfred mntsrv, udpconf)) 40174462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service"); 40274462Salfred else 40374462Salfred xcreated++; 40474462Salfred if (!force_v2) { 40574462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3, 40674462Salfred mntsrv, udpconf)) 40774462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service"); 40874462Salfred else 40974462Salfred xcreated++; 41074462Salfred } 41174462Salfred } else 41274462Salfred syslog(LOG_WARNING, "can't create UDP services"); 41374462Salfred 41474462Salfred } 41574462Salfred if (tcpsock != -1 && tcpconf != NULL) { 41674462Salfred bindresvport(tcpsock, NULL); 41774462Salfred listen(tcpsock, SOMAXCONN); 41874462Salfred tcptransp = svc_vc_create(tcpsock, 0, 0); 41974462Salfred if (tcptransp != NULL) { 42074462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1, 42174462Salfred mntsrv, tcpconf)) 42274462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service"); 42374462Salfred else 42474462Salfred xcreated++; 42574462Salfred if (!force_v2) { 42674462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3, 42774462Salfred mntsrv, tcpconf)) 42874462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service"); 42974462Salfred else 43074462Salfred xcreated++; 43174462Salfred } 43274462Salfred } else 43374462Salfred syslog(LOG_WARNING, "can't create TCP service"); 43474462Salfred 43574462Salfred } 43674791Salfred if (have_v6 && udp6sock != -1 && udp6conf != NULL) { 43774462Salfred bindresvport(udp6sock, NULL); 43874462Salfred udp6transp = svc_dg_create(udp6sock, 0, 0); 43974462Salfred if (udp6transp != NULL) { 44074462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1, 44174462Salfred mntsrv, udp6conf)) 44274462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service"); 44374462Salfred else 44474462Salfred xcreated++; 44574462Salfred if (!force_v2) { 44674462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3, 44774462Salfred mntsrv, udp6conf)) 44874462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service"); 44974462Salfred else 45074462Salfred xcreated++; 45174462Salfred } 45274462Salfred } else 45374462Salfred syslog(LOG_WARNING, "can't create UDP6 service"); 45474462Salfred 45574462Salfred } 45674791Salfred if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) { 45774462Salfred bindresvport(tcp6sock, NULL); 45874462Salfred listen(tcp6sock, SOMAXCONN); 45974462Salfred tcp6transp = svc_vc_create(tcp6sock, 0, 0); 46074462Salfred if (tcp6transp != NULL) { 46174462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1, 46274462Salfred mntsrv, tcp6conf)) 46374462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service"); 46474462Salfred else 46574462Salfred xcreated++; 46674462Salfred if (!force_v2) { 46774462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3, 46874462Salfred mntsrv, tcp6conf)) 46974462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service"); 47074462Salfred else 47174462Salfred xcreated++; 47274462Salfred } 47374462Salfred } else 47474462Salfred syslog(LOG_WARNING, "can't create TCP6 service"); 47574462Salfred 47674462Salfred } 47774462Salfred if (xcreated == 0) { 47874462Salfred syslog(LOG_ERR, "could not create any services"); 4791558Srgrimes exit(1); 4801558Srgrimes } 48175754Siedowse 48275754Siedowse /* Expand svc_run() here so that we can call get_exportlist(). */ 48375754Siedowse for (;;) { 48475754Siedowse if (got_sighup) { 48575754Siedowse get_exportlist(); 48675754Siedowse got_sighup = 0; 48775754Siedowse } 48875754Siedowse readfds = svc_fdset; 48975754Siedowse switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) { 49075754Siedowse case -1: 49175754Siedowse if (errno == EINTR) 49275754Siedowse continue; 49375754Siedowse syslog(LOG_ERR, "mountd died: select: %m"); 49475754Siedowse exit(1); 49575754Siedowse case 0: 49675754Siedowse continue; 49775754Siedowse default: 49875754Siedowse svc_getreqset(&readfds); 49975754Siedowse } 50075754Siedowse } 5011558Srgrimes} 5021558Srgrimes 50337663Scharnierstatic void 50437663Scharnierusage() 50537663Scharnier{ 50637663Scharnier fprintf(stderr, 50737663Scharnier "usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n"); 50837663Scharnier exit(1); 50937663Scharnier} 51037663Scharnier 5111558Srgrimes/* 5121558Srgrimes * The mount rpc service 5131558Srgrimes */ 5141558Srgrimesvoid 5151558Srgrimesmntsrv(rqstp, transp) 5161558Srgrimes struct svc_req *rqstp; 5171558Srgrimes SVCXPRT *transp; 5181558Srgrimes{ 5191558Srgrimes struct exportlist *ep; 5201558Srgrimes struct dirlist *dp; 5219336Sdfr struct fhreturn fhr; 5221558Srgrimes struct stat stb; 5231558Srgrimes struct statfs fsb; 52474462Salfred struct addrinfo *ai; 52574462Salfred char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 52674462Salfred int lookup_failed = 1; 52774462Salfred struct sockaddr *saddr; 5289336Sdfr u_short sport; 52923681Speter char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN]; 53028911Sguido int bad = 0, defset, hostset; 5319336Sdfr sigset_t sighup_mask; 5321558Srgrimes 5339336Sdfr sigemptyset(&sighup_mask); 5349336Sdfr sigaddset(&sighup_mask, SIGHUP); 53574462Salfred saddr = svc_getrpccaller(transp)->buf; 53674462Salfred switch (saddr->sa_family) { 53774462Salfred case AF_INET6: 53875635Siedowse sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 53974462Salfred break; 54074462Salfred case AF_INET: 54175635Siedowse sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 54274462Salfred break; 54374462Salfred default: 54474462Salfred syslog(LOG_ERR, "request from unknown address family"); 54574462Salfred return; 54674462Salfred } 54774462Salfred lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, 54874462Salfred NULL, 0, 0); 54974462Salfred getnameinfo(saddr, saddr->sa_len, numerichost, 55074462Salfred sizeof numerichost, NULL, 0, NI_NUMERICHOST); 55174462Salfred ai = NULL; 5521558Srgrimes switch (rqstp->rq_proc) { 5531558Srgrimes case NULLPROC: 5541558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 55537663Scharnier syslog(LOG_ERR, "can't send reply"); 5561558Srgrimes return; 5571558Srgrimes case RPCMNT_MOUNT: 5589336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 55931656Sguido syslog(LOG_NOTICE, 56031656Sguido "mount request from %s from unprivileged port", 56174462Salfred numerichost); 5621558Srgrimes svcerr_weakauth(transp); 5631558Srgrimes return; 5641558Srgrimes } 5651558Srgrimes if (!svc_getargs(transp, xdr_dir, rpcpath)) { 56631656Sguido syslog(LOG_NOTICE, "undecodable mount request from %s", 56774462Salfred numerichost); 5681558Srgrimes svcerr_decode(transp); 5691558Srgrimes return; 5701558Srgrimes } 5711558Srgrimes 5721558Srgrimes /* 5731558Srgrimes * Get the real pathname and make sure it is a directory 5749336Sdfr * or a regular file if the -r option was specified 5759336Sdfr * and it exists. 5761558Srgrimes */ 57751968Salfred if (realpath(rpcpath, dirpath) == NULL || 5781558Srgrimes stat(dirpath, &stb) < 0 || 5799336Sdfr (!S_ISDIR(stb.st_mode) && 58074462Salfred (dir_only || !S_ISREG(stb.st_mode))) || 5811558Srgrimes statfs(dirpath, &fsb) < 0) { 5821558Srgrimes chdir("/"); /* Just in case realpath doesn't */ 58331656Sguido syslog(LOG_NOTICE, 58437663Scharnier "mount request from %s for non existent path %s", 58574462Salfred numerichost, dirpath); 5861558Srgrimes if (debug) 58737663Scharnier warnx("stat failed on %s", dirpath); 58828911Sguido bad = ENOENT; /* We will send error reply later */ 5891558Srgrimes } 5901558Srgrimes 5911558Srgrimes /* Check in the exports list */ 5929336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 5931558Srgrimes ep = ex_search(&fsb.f_fsid); 5949336Sdfr hostset = defset = 0; 5959336Sdfr if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) || 5961558Srgrimes ((dp = dirp_search(ep->ex_dirl, dirpath)) && 59774462Salfred chk_host(dp, saddr, &defset, &hostset)) || 59874462Salfred (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 59974462Salfred scan_tree(ep->ex_dirl, saddr) == 0))) { 60028911Sguido if (bad) { 60128911Sguido if (!svc_sendreply(transp, xdr_long, 60228911Sguido (caddr_t)&bad)) 60337663Scharnier syslog(LOG_ERR, "can't send reply"); 60428911Sguido sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 60528911Sguido return; 60628911Sguido } 6079336Sdfr if (hostset & DP_HOSTSET) 6089336Sdfr fhr.fhr_flag = hostset; 6099336Sdfr else 6109336Sdfr fhr.fhr_flag = defset; 6119336Sdfr fhr.fhr_vers = rqstp->rq_vers; 6121558Srgrimes /* Get the file handle */ 61323681Speter memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 6149336Sdfr if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 6151558Srgrimes bad = errno; 61637663Scharnier syslog(LOG_ERR, "can't get fh for %s", dirpath); 6171558Srgrimes if (!svc_sendreply(transp, xdr_long, 6181558Srgrimes (caddr_t)&bad)) 61937663Scharnier syslog(LOG_ERR, "can't send reply"); 6209336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6211558Srgrimes return; 6221558Srgrimes } 6239336Sdfr if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr)) 62437663Scharnier syslog(LOG_ERR, "can't send reply"); 62574462Salfred if (!lookup_failed) 62674462Salfred add_mlist(host, dirpath); 6271558Srgrimes else 62874462Salfred add_mlist(numerichost, dirpath); 6291558Srgrimes if (debug) 63037663Scharnier warnx("mount successful"); 63131656Sguido if (log) 63231656Sguido syslog(LOG_NOTICE, 63331656Sguido "mount request succeeded from %s for %s", 63474462Salfred numerichost, dirpath); 63531656Sguido } else { 6361558Srgrimes bad = EACCES; 63731656Sguido syslog(LOG_NOTICE, 63831656Sguido "mount request denied from %s for %s", 63974462Salfred numerichost, dirpath); 64031656Sguido } 64128911Sguido 64228911Sguido if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad)) 64337663Scharnier syslog(LOG_ERR, "can't send reply"); 6449336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6451558Srgrimes return; 6461558Srgrimes case RPCMNT_DUMP: 6471558Srgrimes if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL)) 64837663Scharnier syslog(LOG_ERR, "can't send reply"); 64931656Sguido else if (log) 65031656Sguido syslog(LOG_NOTICE, 65131656Sguido "dump request succeeded from %s", 65274462Salfred numerichost); 6531558Srgrimes return; 6541558Srgrimes case RPCMNT_UMOUNT: 6559336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 65631656Sguido syslog(LOG_NOTICE, 65731656Sguido "umount request from %s from unprivileged port", 65874462Salfred numerichost); 6591558Srgrimes svcerr_weakauth(transp); 6601558Srgrimes return; 6611558Srgrimes } 66251968Salfred if (!svc_getargs(transp, xdr_dir, rpcpath)) { 66331656Sguido syslog(LOG_NOTICE, "undecodable umount request from %s", 66474462Salfred numerichost); 6651558Srgrimes svcerr_decode(transp); 6661558Srgrimes return; 6671558Srgrimes } 66851968Salfred if (realpath(rpcpath, dirpath) == NULL) { 66951968Salfred syslog(LOG_NOTICE, "umount request from %s " 67051968Salfred "for non existent path %s", 67174462Salfred numerichost, dirpath); 67251968Salfred } 6731558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 67437663Scharnier syslog(LOG_ERR, "can't send reply"); 67574462Salfred if (!lookup_failed) 67675635Siedowse del_mlist(host, dirpath); 67775635Siedowse del_mlist(numerichost, dirpath); 67831656Sguido if (log) 67931656Sguido syslog(LOG_NOTICE, 68031656Sguido "umount request succeeded from %s for %s", 68174462Salfred numerichost, dirpath); 6821558Srgrimes return; 6831558Srgrimes case RPCMNT_UMNTALL: 6849336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 68531656Sguido syslog(LOG_NOTICE, 68631656Sguido "umountall request from %s from unprivileged port", 68774462Salfred numerichost); 6881558Srgrimes svcerr_weakauth(transp); 6891558Srgrimes return; 6901558Srgrimes } 6911558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 69237663Scharnier syslog(LOG_ERR, "can't send reply"); 69374462Salfred if (!lookup_failed) 69475635Siedowse del_mlist(host, NULL); 69575635Siedowse del_mlist(numerichost, NULL); 69631656Sguido if (log) 69731656Sguido syslog(LOG_NOTICE, 69831656Sguido "umountall request succeeded from %s", 69974462Salfred numerichost); 7001558Srgrimes return; 7011558Srgrimes case RPCMNT_EXPORT: 7021558Srgrimes if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL)) 70337663Scharnier syslog(LOG_ERR, "can't send reply"); 70431656Sguido if (log) 70531656Sguido syslog(LOG_NOTICE, 70631656Sguido "export request succeeded from %s", 70774462Salfred numerichost); 7081558Srgrimes return; 7091558Srgrimes default: 7101558Srgrimes svcerr_noproc(transp); 7111558Srgrimes return; 7121558Srgrimes } 7131558Srgrimes} 7141558Srgrimes 7151558Srgrimes/* 7161558Srgrimes * Xdr conversion for a dirpath string 7171558Srgrimes */ 7181558Srgrimesint 7191558Srgrimesxdr_dir(xdrsp, dirp) 7201558Srgrimes XDR *xdrsp; 7211558Srgrimes char *dirp; 7221558Srgrimes{ 7231558Srgrimes return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 7241558Srgrimes} 7251558Srgrimes 7261558Srgrimes/* 7279336Sdfr * Xdr routine to generate file handle reply 7281558Srgrimes */ 7291558Srgrimesint 7309336Sdfrxdr_fhs(xdrsp, cp) 7311558Srgrimes XDR *xdrsp; 7329336Sdfr caddr_t cp; 7331558Srgrimes{ 7349336Sdfr register struct fhreturn *fhrp = (struct fhreturn *)cp; 7359336Sdfr u_long ok = 0, len, auth; 7361558Srgrimes 7371558Srgrimes if (!xdr_long(xdrsp, &ok)) 7381558Srgrimes return (0); 7399336Sdfr switch (fhrp->fhr_vers) { 7409336Sdfr case 1: 7419336Sdfr return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 7429336Sdfr case 3: 7439336Sdfr len = NFSX_V3FH; 7449336Sdfr if (!xdr_long(xdrsp, &len)) 7459336Sdfr return (0); 7469336Sdfr if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 7479336Sdfr return (0); 7489336Sdfr if (fhrp->fhr_flag & DP_KERB) 7499336Sdfr auth = RPCAUTH_KERB4; 7509336Sdfr else 7519336Sdfr auth = RPCAUTH_UNIX; 7529336Sdfr len = 1; 7539336Sdfr if (!xdr_long(xdrsp, &len)) 7549336Sdfr return (0); 7559336Sdfr return (xdr_long(xdrsp, &auth)); 7569336Sdfr }; 7579336Sdfr return (0); 7581558Srgrimes} 7591558Srgrimes 7601558Srgrimesint 7611558Srgrimesxdr_mlist(xdrsp, cp) 7621558Srgrimes XDR *xdrsp; 7631558Srgrimes caddr_t cp; 7641558Srgrimes{ 7651558Srgrimes struct mountlist *mlp; 7661558Srgrimes int true = 1; 7671558Srgrimes int false = 0; 7681558Srgrimes char *strp; 7691558Srgrimes 7701558Srgrimes mlp = mlhead; 7711558Srgrimes while (mlp) { 7721558Srgrimes if (!xdr_bool(xdrsp, &true)) 7731558Srgrimes return (0); 7741558Srgrimes strp = &mlp->ml_host[0]; 7751558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN)) 7761558Srgrimes return (0); 7771558Srgrimes strp = &mlp->ml_dirp[0]; 7781558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 7791558Srgrimes return (0); 7801558Srgrimes mlp = mlp->ml_next; 7811558Srgrimes } 7821558Srgrimes if (!xdr_bool(xdrsp, &false)) 7831558Srgrimes return (0); 7841558Srgrimes return (1); 7851558Srgrimes} 7861558Srgrimes 7871558Srgrimes/* 7881558Srgrimes * Xdr conversion for export list 7891558Srgrimes */ 7901558Srgrimesint 7911558Srgrimesxdr_explist(xdrsp, cp) 7921558Srgrimes XDR *xdrsp; 7931558Srgrimes caddr_t cp; 7941558Srgrimes{ 7951558Srgrimes struct exportlist *ep; 7961558Srgrimes int false = 0; 7979336Sdfr int putdef; 7989336Sdfr sigset_t sighup_mask; 7991558Srgrimes 8009336Sdfr sigemptyset(&sighup_mask); 8019336Sdfr sigaddset(&sighup_mask, SIGHUP); 8029336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 8031558Srgrimes ep = exphead; 8041558Srgrimes while (ep) { 8051558Srgrimes putdef = 0; 8061558Srgrimes if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef)) 8071558Srgrimes goto errout; 8081558Srgrimes if (ep->ex_defdir && putdef == 0 && 8091558Srgrimes put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 8101558Srgrimes &putdef)) 8111558Srgrimes goto errout; 8121558Srgrimes ep = ep->ex_next; 8131558Srgrimes } 8149336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8151558Srgrimes if (!xdr_bool(xdrsp, &false)) 8161558Srgrimes return (0); 8171558Srgrimes return (1); 8181558Srgrimeserrout: 8199336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8201558Srgrimes return (0); 8211558Srgrimes} 8221558Srgrimes 8231558Srgrimes/* 8241558Srgrimes * Called from xdr_explist() to traverse the tree and export the 8251558Srgrimes * directory paths. 8261558Srgrimes */ 8271558Srgrimesint 8281558Srgrimesput_exlist(dp, xdrsp, adp, putdefp) 8291558Srgrimes struct dirlist *dp; 8301558Srgrimes XDR *xdrsp; 8311558Srgrimes struct dirlist *adp; 8321558Srgrimes int *putdefp; 8331558Srgrimes{ 8341558Srgrimes struct grouplist *grp; 8351558Srgrimes struct hostlist *hp; 8361558Srgrimes int true = 1; 8371558Srgrimes int false = 0; 8381558Srgrimes int gotalldir = 0; 8391558Srgrimes char *strp; 8401558Srgrimes 8411558Srgrimes if (dp) { 8421558Srgrimes if (put_exlist(dp->dp_left, xdrsp, adp, putdefp)) 8431558Srgrimes return (1); 8441558Srgrimes if (!xdr_bool(xdrsp, &true)) 8451558Srgrimes return (1); 8461558Srgrimes strp = dp->dp_dirp; 8471558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 8481558Srgrimes return (1); 8491558Srgrimes if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 8501558Srgrimes gotalldir = 1; 8511558Srgrimes *putdefp = 1; 8521558Srgrimes } 8531558Srgrimes if ((dp->dp_flag & DP_DEFSET) == 0 && 8541558Srgrimes (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 8551558Srgrimes hp = dp->dp_hosts; 8561558Srgrimes while (hp) { 8571558Srgrimes grp = hp->ht_grp; 8581558Srgrimes if (grp->gr_type == GT_HOST) { 8591558Srgrimes if (!xdr_bool(xdrsp, &true)) 8601558Srgrimes return (1); 86174462Salfred strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 8628871Srgrimes if (!xdr_string(xdrsp, &strp, 8631558Srgrimes RPCMNT_NAMELEN)) 8641558Srgrimes return (1); 8651558Srgrimes } else if (grp->gr_type == GT_NET) { 8661558Srgrimes if (!xdr_bool(xdrsp, &true)) 8671558Srgrimes return (1); 8681558Srgrimes strp = grp->gr_ptr.gt_net.nt_name; 8698871Srgrimes if (!xdr_string(xdrsp, &strp, 8701558Srgrimes RPCMNT_NAMELEN)) 8711558Srgrimes return (1); 8721558Srgrimes } 8731558Srgrimes hp = hp->ht_next; 8741558Srgrimes if (gotalldir && hp == (struct hostlist *)NULL) { 8751558Srgrimes hp = adp->dp_hosts; 8761558Srgrimes gotalldir = 0; 8771558Srgrimes } 8781558Srgrimes } 8791558Srgrimes } 8801558Srgrimes if (!xdr_bool(xdrsp, &false)) 8811558Srgrimes return (1); 8821558Srgrimes if (put_exlist(dp->dp_right, xdrsp, adp, putdefp)) 8831558Srgrimes return (1); 8841558Srgrimes } 8851558Srgrimes return (0); 8861558Srgrimes} 8871558Srgrimes 8881558Srgrimes#define LINESIZ 10240 8891558Srgrimeschar line[LINESIZ]; 8901558SrgrimesFILE *exp_file; 8911558Srgrimes 8921558Srgrimes/* 8931558Srgrimes * Get the export list 8941558Srgrimes */ 8951558Srgrimesvoid 8961558Srgrimesget_exportlist() 8971558Srgrimes{ 8981558Srgrimes struct exportlist *ep, *ep2; 8991558Srgrimes struct grouplist *grp, *tgrp; 9001558Srgrimes struct exportlist **epp; 9011558Srgrimes struct dirlist *dirhead; 9021558Srgrimes struct statfs fsb, *fsp; 90372650Sgreen struct xucred anon; 9041558Srgrimes char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 9051558Srgrimes int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp; 9061558Srgrimes 90751968Salfred dirp = NULL; 90851968Salfred dirplen = 0; 90951968Salfred 9101558Srgrimes /* 9111558Srgrimes * First, get rid of the old list 9121558Srgrimes */ 9131558Srgrimes ep = exphead; 9141558Srgrimes while (ep) { 9151558Srgrimes ep2 = ep; 9161558Srgrimes ep = ep->ex_next; 9171558Srgrimes free_exp(ep2); 9181558Srgrimes } 9191558Srgrimes exphead = (struct exportlist *)NULL; 9201558Srgrimes 9211558Srgrimes grp = grphead; 9221558Srgrimes while (grp) { 9231558Srgrimes tgrp = grp; 9241558Srgrimes grp = grp->gr_next; 9251558Srgrimes free_grp(tgrp); 9261558Srgrimes } 9271558Srgrimes grphead = (struct grouplist *)NULL; 9281558Srgrimes 9291558Srgrimes /* 9301558Srgrimes * And delete exports that are in the kernel for all local 9311558Srgrimes * file systems. 9321558Srgrimes * XXX: Should know how to handle all local exportable file systems 93323681Speter * instead of just "ufs". 9341558Srgrimes */ 9351558Srgrimes num = getmntinfo(&fsp, MNT_NOWAIT); 9361558Srgrimes for (i = 0; i < num; i++) { 9371558Srgrimes union { 9381558Srgrimes struct ufs_args ua; 9391558Srgrimes struct iso_args ia; 9401558Srgrimes struct mfs_args ma; 9419336Sdfr struct msdosfs_args da; 94254093Ssemenu struct ntfs_args na; 9431558Srgrimes } targs; 9441558Srgrimes 94523681Speter if (!strcmp(fsp->f_fstypename, "mfs") || 94623681Speter !strcmp(fsp->f_fstypename, "ufs") || 94723681Speter !strcmp(fsp->f_fstypename, "msdos") || 94854093Ssemenu !strcmp(fsp->f_fstypename, "ntfs") || 94923681Speter !strcmp(fsp->f_fstypename, "cd9660")) { 9509336Sdfr targs.ua.fspec = NULL; 9519336Sdfr targs.ua.export.ex_flags = MNT_DELEXPORT; 9529336Sdfr if (mount(fsp->f_fstypename, fsp->f_mntonname, 9531558Srgrimes fsp->f_flags | MNT_UPDATE, 9541558Srgrimes (caddr_t)&targs) < 0) 95537663Scharnier syslog(LOG_ERR, "can't delete exports for %s", 95674462Salfred fsp->f_mntonname); 9571558Srgrimes } 9581558Srgrimes fsp++; 9591558Srgrimes } 9601558Srgrimes 9611558Srgrimes /* 9621558Srgrimes * Read in the exports file and build the list, calling 9631558Srgrimes * mount() as we go along to push the export rules into the kernel. 9641558Srgrimes */ 9651558Srgrimes if ((exp_file = fopen(exname, "r")) == NULL) { 96637663Scharnier syslog(LOG_ERR, "can't open %s", exname); 9671558Srgrimes exit(2); 9681558Srgrimes } 9691558Srgrimes dirhead = (struct dirlist *)NULL; 9701558Srgrimes while (get_line()) { 9711558Srgrimes if (debug) 97237663Scharnier warnx("got line %s", line); 9731558Srgrimes cp = line; 9741558Srgrimes nextfield(&cp, &endcp); 9751558Srgrimes if (*cp == '#') 9761558Srgrimes goto nextline; 9771558Srgrimes 9781558Srgrimes /* 9791558Srgrimes * Set defaults. 9801558Srgrimes */ 9811558Srgrimes has_host = FALSE; 9821558Srgrimes anon = def_anon; 9831558Srgrimes exflags = MNT_EXPORTED; 9841558Srgrimes got_nondir = 0; 9851558Srgrimes opt_flags = 0; 9861558Srgrimes ep = (struct exportlist *)NULL; 9871558Srgrimes 9881558Srgrimes /* 9891558Srgrimes * Create new exports list entry 9901558Srgrimes */ 9911558Srgrimes len = endcp-cp; 9921558Srgrimes tgrp = grp = get_grp(); 9931558Srgrimes while (len > 0) { 9941558Srgrimes if (len > RPCMNT_NAMELEN) { 9951558Srgrimes getexp_err(ep, tgrp); 9961558Srgrimes goto nextline; 9971558Srgrimes } 9981558Srgrimes if (*cp == '-') { 9991558Srgrimes if (ep == (struct exportlist *)NULL) { 10001558Srgrimes getexp_err(ep, tgrp); 10011558Srgrimes goto nextline; 10021558Srgrimes } 10031558Srgrimes if (debug) 100437663Scharnier warnx("doing opt %s", cp); 10051558Srgrimes got_nondir = 1; 10061558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 10071558Srgrimes &exflags, &anon)) { 10081558Srgrimes getexp_err(ep, tgrp); 10091558Srgrimes goto nextline; 10101558Srgrimes } 10111558Srgrimes } else if (*cp == '/') { 10121558Srgrimes savedc = *endcp; 10131558Srgrimes *endcp = '\0'; 10141558Srgrimes if (check_dirpath(cp) && 10151558Srgrimes statfs(cp, &fsb) >= 0) { 10161558Srgrimes if (got_nondir) { 101737663Scharnier syslog(LOG_ERR, "dirs must be first"); 10181558Srgrimes getexp_err(ep, tgrp); 10191558Srgrimes goto nextline; 10201558Srgrimes } 10211558Srgrimes if (ep) { 10221558Srgrimes if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] || 10231558Srgrimes ep->ex_fs.val[1] != fsb.f_fsid.val[1]) { 10241558Srgrimes getexp_err(ep, tgrp); 10251558Srgrimes goto nextline; 10261558Srgrimes } 10271558Srgrimes } else { 10281558Srgrimes /* 10291558Srgrimes * See if this directory is already 10301558Srgrimes * in the list. 10311558Srgrimes */ 10321558Srgrimes ep = ex_search(&fsb.f_fsid); 10331558Srgrimes if (ep == (struct exportlist *)NULL) { 10341558Srgrimes ep = get_exp(); 10351558Srgrimes ep->ex_fs = fsb.f_fsid; 10361558Srgrimes ep->ex_fsdir = (char *) 10371558Srgrimes malloc(strlen(fsb.f_mntonname) + 1); 10381558Srgrimes if (ep->ex_fsdir) 10391558Srgrimes strcpy(ep->ex_fsdir, 10401558Srgrimes fsb.f_mntonname); 10411558Srgrimes else 10421558Srgrimes out_of_mem(); 10431558Srgrimes if (debug) 104474462Salfred warnx("making new ep fs=0x%x,0x%x", 104574462Salfred fsb.f_fsid.val[0], 104674462Salfred fsb.f_fsid.val[1]); 10471558Srgrimes } else if (debug) 104837663Scharnier warnx("found ep fs=0x%x,0x%x", 10491558Srgrimes fsb.f_fsid.val[0], 10501558Srgrimes fsb.f_fsid.val[1]); 10511558Srgrimes } 10521558Srgrimes 10531558Srgrimes /* 10541558Srgrimes * Add dirpath to export mount point. 10551558Srgrimes */ 10561558Srgrimes dirp = add_expdir(&dirhead, cp, len); 10571558Srgrimes dirplen = len; 10581558Srgrimes } else { 10591558Srgrimes getexp_err(ep, tgrp); 10601558Srgrimes goto nextline; 10611558Srgrimes } 10621558Srgrimes *endcp = savedc; 10631558Srgrimes } else { 10641558Srgrimes savedc = *endcp; 10651558Srgrimes *endcp = '\0'; 10661558Srgrimes got_nondir = 1; 10671558Srgrimes if (ep == (struct exportlist *)NULL) { 10681558Srgrimes getexp_err(ep, tgrp); 10691558Srgrimes goto nextline; 10701558Srgrimes } 10711558Srgrimes 10721558Srgrimes /* 10731558Srgrimes * Get the host or netgroup. 10741558Srgrimes */ 10751558Srgrimes setnetgrent(cp); 10761558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 10771558Srgrimes do { 10781558Srgrimes if (has_host) { 10791558Srgrimes grp->gr_next = get_grp(); 10801558Srgrimes grp = grp->gr_next; 10811558Srgrimes } 10821558Srgrimes if (netgrp) { 108337003Sjoerg if (hst == 0) { 108437663Scharnier syslog(LOG_ERR, 108537663Scharnier "null hostname in netgroup %s, skipping", cp); 108637004Sjoerg grp->gr_type = GT_IGNORE; 108737003Sjoerg } else if (get_host(hst, grp, tgrp)) { 108837663Scharnier syslog(LOG_ERR, 108937663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 109029317Sjlemon grp->gr_type = GT_IGNORE; 10911558Srgrimes } 10927401Swpaul } else if (get_host(cp, grp, tgrp)) { 109337663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 109429317Sjlemon grp->gr_type = GT_IGNORE; 10951558Srgrimes } 10961558Srgrimes has_host = TRUE; 10971558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 10981558Srgrimes endnetgrent(); 10991558Srgrimes *endcp = savedc; 11001558Srgrimes } 11011558Srgrimes cp = endcp; 11021558Srgrimes nextfield(&cp, &endcp); 11031558Srgrimes len = endcp - cp; 11041558Srgrimes } 11051558Srgrimes if (check_options(dirhead)) { 11061558Srgrimes getexp_err(ep, tgrp); 11071558Srgrimes goto nextline; 11081558Srgrimes } 11091558Srgrimes if (!has_host) { 111075641Siedowse grp->gr_type = GT_DEFAULT; 11111558Srgrimes if (debug) 111237663Scharnier warnx("adding a default entry"); 11131558Srgrimes 11141558Srgrimes /* 11151558Srgrimes * Don't allow a network export coincide with a list of 11161558Srgrimes * host(s) on the same line. 11171558Srgrimes */ 11181558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 111975801Siedowse syslog(LOG_ERR, "network/host conflict"); 11201558Srgrimes getexp_err(ep, tgrp); 11211558Srgrimes goto nextline; 112229317Sjlemon 112374462Salfred /* 112474462Salfred * If an export list was specified on this line, make sure 112529317Sjlemon * that we have at least one valid entry, otherwise skip it. 112629317Sjlemon */ 112729317Sjlemon } else { 112829317Sjlemon grp = tgrp; 112974462Salfred while (grp && grp->gr_type == GT_IGNORE) 113029317Sjlemon grp = grp->gr_next; 113129317Sjlemon if (! grp) { 113229317Sjlemon getexp_err(ep, tgrp); 113329317Sjlemon goto nextline; 113429317Sjlemon } 11351558Srgrimes } 11361558Srgrimes 11371558Srgrimes /* 11381558Srgrimes * Loop through hosts, pushing the exports into the kernel. 11391558Srgrimes * After loop, tgrp points to the start of the list and 11401558Srgrimes * grp points to the last entry in the list. 11411558Srgrimes */ 11421558Srgrimes grp = tgrp; 11431558Srgrimes do { 114475635Siedowse if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 114575635Siedowse &fsb)) { 114675635Siedowse getexp_err(ep, tgrp); 114775635Siedowse goto nextline; 114875635Siedowse } 11491558Srgrimes } while (grp->gr_next && (grp = grp->gr_next)); 11501558Srgrimes 11511558Srgrimes /* 11521558Srgrimes * Success. Update the data structures. 11531558Srgrimes */ 11541558Srgrimes if (has_host) { 11559336Sdfr hang_dirp(dirhead, tgrp, ep, opt_flags); 11561558Srgrimes grp->gr_next = grphead; 11571558Srgrimes grphead = tgrp; 11581558Srgrimes } else { 11591558Srgrimes hang_dirp(dirhead, (struct grouplist *)NULL, ep, 11609336Sdfr opt_flags); 11611558Srgrimes free_grp(grp); 11621558Srgrimes } 11631558Srgrimes dirhead = (struct dirlist *)NULL; 11641558Srgrimes if ((ep->ex_flag & EX_LINKED) == 0) { 11651558Srgrimes ep2 = exphead; 11661558Srgrimes epp = &exphead; 11671558Srgrimes 11681558Srgrimes /* 11691558Srgrimes * Insert in the list in alphabetical order. 11701558Srgrimes */ 11711558Srgrimes while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { 11721558Srgrimes epp = &ep2->ex_next; 11731558Srgrimes ep2 = ep2->ex_next; 11741558Srgrimes } 11751558Srgrimes if (ep2) 11761558Srgrimes ep->ex_next = ep2; 11771558Srgrimes *epp = ep; 11781558Srgrimes ep->ex_flag |= EX_LINKED; 11791558Srgrimes } 11801558Srgrimesnextline: 11811558Srgrimes if (dirhead) { 11821558Srgrimes free_dir(dirhead); 11831558Srgrimes dirhead = (struct dirlist *)NULL; 11841558Srgrimes } 11851558Srgrimes } 11861558Srgrimes fclose(exp_file); 11871558Srgrimes} 11881558Srgrimes 11891558Srgrimes/* 11901558Srgrimes * Allocate an export list element 11911558Srgrimes */ 11921558Srgrimesstruct exportlist * 11931558Srgrimesget_exp() 11941558Srgrimes{ 11951558Srgrimes struct exportlist *ep; 11961558Srgrimes 11971558Srgrimes ep = (struct exportlist *)malloc(sizeof (struct exportlist)); 11981558Srgrimes if (ep == (struct exportlist *)NULL) 11991558Srgrimes out_of_mem(); 120023681Speter memset(ep, 0, sizeof(struct exportlist)); 12011558Srgrimes return (ep); 12021558Srgrimes} 12031558Srgrimes 12041558Srgrimes/* 12051558Srgrimes * Allocate a group list element 12061558Srgrimes */ 12071558Srgrimesstruct grouplist * 12081558Srgrimesget_grp() 12091558Srgrimes{ 12101558Srgrimes struct grouplist *gp; 12111558Srgrimes 12121558Srgrimes gp = (struct grouplist *)malloc(sizeof (struct grouplist)); 12131558Srgrimes if (gp == (struct grouplist *)NULL) 12141558Srgrimes out_of_mem(); 121523681Speter memset(gp, 0, sizeof(struct grouplist)); 12161558Srgrimes return (gp); 12171558Srgrimes} 12181558Srgrimes 12191558Srgrimes/* 12201558Srgrimes * Clean up upon an error in get_exportlist(). 12211558Srgrimes */ 12221558Srgrimesvoid 12231558Srgrimesgetexp_err(ep, grp) 12241558Srgrimes struct exportlist *ep; 12251558Srgrimes struct grouplist *grp; 12261558Srgrimes{ 12271558Srgrimes struct grouplist *tgrp; 12281558Srgrimes 122937663Scharnier syslog(LOG_ERR, "bad exports list line %s", line); 12301558Srgrimes if (ep && (ep->ex_flag & EX_LINKED) == 0) 12311558Srgrimes free_exp(ep); 12321558Srgrimes while (grp) { 12331558Srgrimes tgrp = grp; 12341558Srgrimes grp = grp->gr_next; 12351558Srgrimes free_grp(tgrp); 12361558Srgrimes } 12371558Srgrimes} 12381558Srgrimes 12391558Srgrimes/* 12401558Srgrimes * Search the export list for a matching fs. 12411558Srgrimes */ 12421558Srgrimesstruct exportlist * 12431558Srgrimesex_search(fsid) 12441558Srgrimes fsid_t *fsid; 12451558Srgrimes{ 12461558Srgrimes struct exportlist *ep; 12471558Srgrimes 12481558Srgrimes ep = exphead; 12491558Srgrimes while (ep) { 12501558Srgrimes if (ep->ex_fs.val[0] == fsid->val[0] && 12511558Srgrimes ep->ex_fs.val[1] == fsid->val[1]) 12521558Srgrimes return (ep); 12531558Srgrimes ep = ep->ex_next; 12541558Srgrimes } 12551558Srgrimes return (ep); 12561558Srgrimes} 12571558Srgrimes 12581558Srgrimes/* 12591558Srgrimes * Add a directory path to the list. 12601558Srgrimes */ 12611558Srgrimeschar * 12621558Srgrimesadd_expdir(dpp, cp, len) 12631558Srgrimes struct dirlist **dpp; 12641558Srgrimes char *cp; 12651558Srgrimes int len; 12661558Srgrimes{ 12671558Srgrimes struct dirlist *dp; 12681558Srgrimes 12691558Srgrimes dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); 127037663Scharnier if (dp == (struct dirlist *)NULL) 127137663Scharnier out_of_mem(); 12721558Srgrimes dp->dp_left = *dpp; 12731558Srgrimes dp->dp_right = (struct dirlist *)NULL; 12741558Srgrimes dp->dp_flag = 0; 12751558Srgrimes dp->dp_hosts = (struct hostlist *)NULL; 12761558Srgrimes strcpy(dp->dp_dirp, cp); 12771558Srgrimes *dpp = dp; 12781558Srgrimes return (dp->dp_dirp); 12791558Srgrimes} 12801558Srgrimes 12811558Srgrimes/* 12821558Srgrimes * Hang the dir list element off the dirpath binary tree as required 12831558Srgrimes * and update the entry for host. 12841558Srgrimes */ 12851558Srgrimesvoid 12869336Sdfrhang_dirp(dp, grp, ep, flags) 12871558Srgrimes struct dirlist *dp; 12881558Srgrimes struct grouplist *grp; 12891558Srgrimes struct exportlist *ep; 12909336Sdfr int flags; 12911558Srgrimes{ 12921558Srgrimes struct hostlist *hp; 12931558Srgrimes struct dirlist *dp2; 12941558Srgrimes 12959336Sdfr if (flags & OP_ALLDIRS) { 12961558Srgrimes if (ep->ex_defdir) 12971558Srgrimes free((caddr_t)dp); 12981558Srgrimes else 12991558Srgrimes ep->ex_defdir = dp; 13009336Sdfr if (grp == (struct grouplist *)NULL) { 13011558Srgrimes ep->ex_defdir->dp_flag |= DP_DEFSET; 13029336Sdfr if (flags & OP_KERB) 13039336Sdfr ep->ex_defdir->dp_flag |= DP_KERB; 13049336Sdfr } else while (grp) { 13051558Srgrimes hp = get_ht(); 13069336Sdfr if (flags & OP_KERB) 13079336Sdfr hp->ht_flag |= DP_KERB; 13081558Srgrimes hp->ht_grp = grp; 13091558Srgrimes hp->ht_next = ep->ex_defdir->dp_hosts; 13101558Srgrimes ep->ex_defdir->dp_hosts = hp; 13111558Srgrimes grp = grp->gr_next; 13121558Srgrimes } 13131558Srgrimes } else { 13141558Srgrimes 13151558Srgrimes /* 131637663Scharnier * Loop through the directories adding them to the tree. 13171558Srgrimes */ 13181558Srgrimes while (dp) { 13191558Srgrimes dp2 = dp->dp_left; 13209336Sdfr add_dlist(&ep->ex_dirl, dp, grp, flags); 13211558Srgrimes dp = dp2; 13221558Srgrimes } 13231558Srgrimes } 13241558Srgrimes} 13251558Srgrimes 13261558Srgrimes/* 13271558Srgrimes * Traverse the binary tree either updating a node that is already there 13281558Srgrimes * for the new directory or adding the new node. 13291558Srgrimes */ 13301558Srgrimesvoid 13319336Sdfradd_dlist(dpp, newdp, grp, flags) 13321558Srgrimes struct dirlist **dpp; 13331558Srgrimes struct dirlist *newdp; 13341558Srgrimes struct grouplist *grp; 13359336Sdfr int flags; 13361558Srgrimes{ 13371558Srgrimes struct dirlist *dp; 13381558Srgrimes struct hostlist *hp; 13391558Srgrimes int cmp; 13401558Srgrimes 13411558Srgrimes dp = *dpp; 13421558Srgrimes if (dp) { 13431558Srgrimes cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 13441558Srgrimes if (cmp > 0) { 13459336Sdfr add_dlist(&dp->dp_left, newdp, grp, flags); 13461558Srgrimes return; 13471558Srgrimes } else if (cmp < 0) { 13489336Sdfr add_dlist(&dp->dp_right, newdp, grp, flags); 13491558Srgrimes return; 13501558Srgrimes } else 13511558Srgrimes free((caddr_t)newdp); 13521558Srgrimes } else { 13531558Srgrimes dp = newdp; 13541558Srgrimes dp->dp_left = (struct dirlist *)NULL; 13551558Srgrimes *dpp = dp; 13561558Srgrimes } 13571558Srgrimes if (grp) { 13581558Srgrimes 13591558Srgrimes /* 13601558Srgrimes * Hang all of the host(s) off of the directory point. 13611558Srgrimes */ 13621558Srgrimes do { 13631558Srgrimes hp = get_ht(); 13649336Sdfr if (flags & OP_KERB) 13659336Sdfr hp->ht_flag |= DP_KERB; 13661558Srgrimes hp->ht_grp = grp; 13671558Srgrimes hp->ht_next = dp->dp_hosts; 13681558Srgrimes dp->dp_hosts = hp; 13691558Srgrimes grp = grp->gr_next; 13701558Srgrimes } while (grp); 13719336Sdfr } else { 13721558Srgrimes dp->dp_flag |= DP_DEFSET; 13739336Sdfr if (flags & OP_KERB) 13749336Sdfr dp->dp_flag |= DP_KERB; 13759336Sdfr } 13761558Srgrimes} 13771558Srgrimes 13781558Srgrimes/* 13791558Srgrimes * Search for a dirpath on the export point. 13801558Srgrimes */ 13811558Srgrimesstruct dirlist * 138274462Salfreddirp_search(dp, dirp) 13831558Srgrimes struct dirlist *dp; 138474462Salfred char *dirp; 13851558Srgrimes{ 13861558Srgrimes int cmp; 13871558Srgrimes 13881558Srgrimes if (dp) { 138974462Salfred cmp = strcmp(dp->dp_dirp, dirp); 13901558Srgrimes if (cmp > 0) 139174462Salfred return (dirp_search(dp->dp_left, dirp)); 13921558Srgrimes else if (cmp < 0) 139374462Salfred return (dirp_search(dp->dp_right, dirp)); 13941558Srgrimes else 13951558Srgrimes return (dp); 13961558Srgrimes } 13971558Srgrimes return (dp); 13981558Srgrimes} 13991558Srgrimes 14001558Srgrimes/* 14011558Srgrimes * Scan for a host match in a directory tree. 14021558Srgrimes */ 14031558Srgrimesint 14049336Sdfrchk_host(dp, saddr, defsetp, hostsetp) 14051558Srgrimes struct dirlist *dp; 140674462Salfred struct sockaddr *saddr; 14071558Srgrimes int *defsetp; 14089336Sdfr int *hostsetp; 14091558Srgrimes{ 14101558Srgrimes struct hostlist *hp; 14111558Srgrimes struct grouplist *grp; 141274462Salfred struct addrinfo *ai; 14131558Srgrimes 14141558Srgrimes if (dp) { 14151558Srgrimes if (dp->dp_flag & DP_DEFSET) 14169336Sdfr *defsetp = dp->dp_flag; 14171558Srgrimes hp = dp->dp_hosts; 14181558Srgrimes while (hp) { 14191558Srgrimes grp = hp->ht_grp; 14201558Srgrimes switch (grp->gr_type) { 14211558Srgrimes case GT_HOST: 142274462Salfred ai = grp->gr_ptr.gt_addrinfo; 142374462Salfred for (; ai; ai = ai->ai_next) { 142475801Siedowse if (!sacmp(ai->ai_addr, saddr, NULL)) { 142574462Salfred *hostsetp = 142674462Salfred (hp->ht_flag | DP_HOSTSET); 142774462Salfred return (1); 142874462Salfred } 14299336Sdfr } 143075801Siedowse break; 14311558Srgrimes case GT_NET: 143275801Siedowse if (!sacmp(saddr, (struct sockaddr *) 143375801Siedowse &grp->gr_ptr.gt_net.nt_net, 143475801Siedowse (struct sockaddr *) 143575801Siedowse &grp->gr_ptr.gt_net.nt_mask)) { 143674462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 143774462Salfred return (1); 143874462Salfred } 143975801Siedowse break; 144075801Siedowse } 14411558Srgrimes hp = hp->ht_next; 14421558Srgrimes } 14431558Srgrimes } 14441558Srgrimes return (0); 14451558Srgrimes} 14461558Srgrimes 14471558Srgrimes/* 14481558Srgrimes * Scan tree for a host that matches the address. 14491558Srgrimes */ 14501558Srgrimesint 14511558Srgrimesscan_tree(dp, saddr) 14521558Srgrimes struct dirlist *dp; 145374462Salfred struct sockaddr *saddr; 14541558Srgrimes{ 14559336Sdfr int defset, hostset; 14561558Srgrimes 14571558Srgrimes if (dp) { 14581558Srgrimes if (scan_tree(dp->dp_left, saddr)) 14591558Srgrimes return (1); 14609336Sdfr if (chk_host(dp, saddr, &defset, &hostset)) 14611558Srgrimes return (1); 14621558Srgrimes if (scan_tree(dp->dp_right, saddr)) 14631558Srgrimes return (1); 14641558Srgrimes } 14651558Srgrimes return (0); 14661558Srgrimes} 14671558Srgrimes 14681558Srgrimes/* 14691558Srgrimes * Traverse the dirlist tree and free it up. 14701558Srgrimes */ 14711558Srgrimesvoid 14721558Srgrimesfree_dir(dp) 14731558Srgrimes struct dirlist *dp; 14741558Srgrimes{ 14751558Srgrimes 14761558Srgrimes if (dp) { 14771558Srgrimes free_dir(dp->dp_left); 14781558Srgrimes free_dir(dp->dp_right); 14791558Srgrimes free_host(dp->dp_hosts); 14801558Srgrimes free((caddr_t)dp); 14811558Srgrimes } 14821558Srgrimes} 14831558Srgrimes 14841558Srgrimes/* 14851558Srgrimes * Parse the option string and update fields. 14861558Srgrimes * Option arguments may either be -<option>=<value> or 14871558Srgrimes * -<option> <value> 14881558Srgrimes */ 14891558Srgrimesint 14901558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr) 14911558Srgrimes char **cpp, **endcpp; 14921558Srgrimes struct exportlist *ep; 14931558Srgrimes struct grouplist *grp; 14941558Srgrimes int *has_hostp; 14951558Srgrimes int *exflagsp; 149672650Sgreen struct xucred *cr; 14971558Srgrimes{ 14981558Srgrimes char *cpoptarg, *cpoptend; 14991558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 15001558Srgrimes int allflag, usedarg; 15011558Srgrimes 150251968Salfred savedc2 = '\0'; 15031558Srgrimes cpopt = *cpp; 15041558Srgrimes cpopt++; 15051558Srgrimes cp = *endcpp; 15061558Srgrimes savedc = *cp; 15071558Srgrimes *cp = '\0'; 15081558Srgrimes while (cpopt && *cpopt) { 15091558Srgrimes allflag = 1; 15101558Srgrimes usedarg = -2; 151137663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 15121558Srgrimes *cpoptend++ = '\0'; 151337663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 15141558Srgrimes *cpoptarg++ = '\0'; 15151558Srgrimes } else { 151637663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 15171558Srgrimes *cpoptarg++ = '\0'; 15181558Srgrimes else { 15191558Srgrimes *cp = savedc; 15201558Srgrimes nextfield(&cp, &endcp); 15211558Srgrimes **endcpp = '\0'; 15221558Srgrimes if (endcp > cp && *cp != '-') { 15231558Srgrimes cpoptarg = cp; 15241558Srgrimes savedc2 = *endcp; 15251558Srgrimes *endcp = '\0'; 15261558Srgrimes usedarg = 0; 15271558Srgrimes } 15281558Srgrimes } 15291558Srgrimes } 15301558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 15311558Srgrimes *exflagsp |= MNT_EXRDONLY; 15321558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 15331558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 15341558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 15351558Srgrimes usedarg++; 15361558Srgrimes parsecred(cpoptarg, cr); 15371558Srgrimes if (allflag == 0) { 15381558Srgrimes *exflagsp |= MNT_EXPORTANON; 15391558Srgrimes opt_flags |= OP_MAPALL; 15401558Srgrimes } else 15411558Srgrimes opt_flags |= OP_MAPROOT; 15421558Srgrimes } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) { 15431558Srgrimes *exflagsp |= MNT_EXKERB; 15441558Srgrimes opt_flags |= OP_KERB; 15451558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 154675801Siedowse !strcmp(cpopt, "m"))) { 15471558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 154837663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 15491558Srgrimes return (1); 15501558Srgrimes } 15511558Srgrimes usedarg++; 15521558Srgrimes opt_flags |= OP_MASK; 15531558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 15541558Srgrimes !strcmp(cpopt, "n"))) { 155574462Salfred if (strchr(cpoptarg, '/') != NULL) { 155674462Salfred if (debug) 155774462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 155874462Salfred opt_flags |= OP_MASKLEN; 155974462Salfred } 15601558Srgrimes if (grp->gr_type != GT_NULL) { 156137663Scharnier syslog(LOG_ERR, "network/host conflict"); 15621558Srgrimes return (1); 15631558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 156437663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 15651558Srgrimes return (1); 15661558Srgrimes } 15671558Srgrimes grp->gr_type = GT_NET; 15681558Srgrimes *has_hostp = 1; 15691558Srgrimes usedarg++; 15701558Srgrimes opt_flags |= OP_NET; 15711558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 15721558Srgrimes opt_flags |= OP_ALLDIRS; 157327447Sdfr } else if (!strcmp(cpopt, "public")) { 157427447Sdfr *exflagsp |= MNT_EXPUBLIC; 157527447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 157627447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 157727447Sdfr opt_flags |= OP_MAPALL; 157827447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 157927447Sdfr ep->ex_indexfile = strdup(cpoptarg); 15801558Srgrimes } else { 158137663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 15821558Srgrimes return (1); 15831558Srgrimes } 15841558Srgrimes if (usedarg >= 0) { 15851558Srgrimes *endcp = savedc2; 15861558Srgrimes **endcpp = savedc; 15871558Srgrimes if (usedarg > 0) { 15881558Srgrimes *cpp = cp; 15891558Srgrimes *endcpp = endcp; 15901558Srgrimes } 15911558Srgrimes return (0); 15921558Srgrimes } 15931558Srgrimes cpopt = cpoptend; 15941558Srgrimes } 15951558Srgrimes **endcpp = savedc; 15961558Srgrimes return (0); 15971558Srgrimes} 15981558Srgrimes 15991558Srgrimes/* 16001558Srgrimes * Translate a character string to the corresponding list of network 16011558Srgrimes * addresses for a hostname. 16021558Srgrimes */ 16031558Srgrimesint 16047401Swpaulget_host(cp, grp, tgrp) 16051558Srgrimes char *cp; 16061558Srgrimes struct grouplist *grp; 16077401Swpaul struct grouplist *tgrp; 16081558Srgrimes{ 16097401Swpaul struct grouplist *checkgrp; 161075635Siedowse struct addrinfo *ai, *tai, hints; 161174462Salfred int ecode; 161274462Salfred char host[NI_MAXHOST]; 16131558Srgrimes 161474462Salfred if (grp->gr_type != GT_NULL) { 161574462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 16161558Srgrimes return (1); 16171558Srgrimes } 161874462Salfred memset(&hints, 0, sizeof hints); 161974462Salfred hints.ai_flags = AI_CANONNAME; 162074462Salfred hints.ai_protocol = IPPROTO_UDP; 162174462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 162274462Salfred if (ecode != 0) { 162375635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 162474462Salfred return 1; 162574462Salfred } 162674462Salfred grp->gr_ptr.gt_addrinfo = ai; 162774462Salfred while (ai != NULL) { 162874462Salfred if (ai->ai_canonname == NULL) { 162974462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 163074462Salfred sizeof host, NULL, 0, ninumeric) != 0) 163174462Salfred strlcpy(host, "?", sizeof(host)); 163274462Salfred ai->ai_canonname = strdup(host); 163374462Salfred ai->ai_flags |= AI_CANONNAME; 163475641Siedowse } 163574462Salfred if (debug) 163675635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 163775635Siedowse /* 163875635Siedowse * Sanity check: make sure we don't already have an entry 163975635Siedowse * for this host in the grouplist. 164075635Siedowse */ 164175635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 164275635Siedowse checkgrp = checkgrp->gr_next) { 164375635Siedowse if (checkgrp->gr_type != GT_HOST) 164475635Siedowse continue; 164575635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 164675635Siedowse tai = tai->ai_next) { 164775801Siedowse if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0) 164875635Siedowse continue; 164975635Siedowse if (debug) 165075635Siedowse fprintf(stderr, 165175635Siedowse "ignoring duplicate host %s\n", 165275635Siedowse ai->ai_canonname); 165375635Siedowse grp->gr_type = GT_IGNORE; 165475635Siedowse return (0); 165575635Siedowse } 165675635Siedowse } 165774462Salfred ai = ai->ai_next; 16581558Srgrimes } 165975635Siedowse grp->gr_type = GT_HOST; 16601558Srgrimes return (0); 16611558Srgrimes} 16621558Srgrimes 16631558Srgrimes/* 16641558Srgrimes * Free up an exports list component 16651558Srgrimes */ 16661558Srgrimesvoid 16671558Srgrimesfree_exp(ep) 16681558Srgrimes struct exportlist *ep; 16691558Srgrimes{ 16701558Srgrimes 16711558Srgrimes if (ep->ex_defdir) { 16721558Srgrimes free_host(ep->ex_defdir->dp_hosts); 16731558Srgrimes free((caddr_t)ep->ex_defdir); 16741558Srgrimes } 16751558Srgrimes if (ep->ex_fsdir) 16761558Srgrimes free(ep->ex_fsdir); 167727447Sdfr if (ep->ex_indexfile) 167827447Sdfr free(ep->ex_indexfile); 16791558Srgrimes free_dir(ep->ex_dirl); 16801558Srgrimes free((caddr_t)ep); 16811558Srgrimes} 16821558Srgrimes 16831558Srgrimes/* 16841558Srgrimes * Free hosts. 16851558Srgrimes */ 16861558Srgrimesvoid 16871558Srgrimesfree_host(hp) 16881558Srgrimes struct hostlist *hp; 16891558Srgrimes{ 16901558Srgrimes struct hostlist *hp2; 16911558Srgrimes 16921558Srgrimes while (hp) { 16931558Srgrimes hp2 = hp; 16941558Srgrimes hp = hp->ht_next; 16951558Srgrimes free((caddr_t)hp2); 16961558Srgrimes } 16971558Srgrimes} 16981558Srgrimes 16991558Srgrimesstruct hostlist * 17001558Srgrimesget_ht() 17011558Srgrimes{ 17021558Srgrimes struct hostlist *hp; 17031558Srgrimes 17041558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 17051558Srgrimes if (hp == (struct hostlist *)NULL) 17061558Srgrimes out_of_mem(); 17071558Srgrimes hp->ht_next = (struct hostlist *)NULL; 17089336Sdfr hp->ht_flag = 0; 17091558Srgrimes return (hp); 17101558Srgrimes} 17111558Srgrimes 17121558Srgrimes/* 17131558Srgrimes * Out of memory, fatal 17141558Srgrimes */ 17151558Srgrimesvoid 17161558Srgrimesout_of_mem() 17171558Srgrimes{ 17181558Srgrimes 171937663Scharnier syslog(LOG_ERR, "out of memory"); 17201558Srgrimes exit(2); 17211558Srgrimes} 17221558Srgrimes 17231558Srgrimes/* 17241558Srgrimes * Do the mount syscall with the update flag to push the export info into 17251558Srgrimes * the kernel. 17261558Srgrimes */ 17271558Srgrimesint 17281558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb) 17291558Srgrimes struct exportlist *ep; 17301558Srgrimes struct grouplist *grp; 17311558Srgrimes int exflags; 173272650Sgreen struct xucred *anoncrp; 17331558Srgrimes char *dirp; 17341558Srgrimes int dirplen; 17351558Srgrimes struct statfs *fsb; 17361558Srgrimes{ 173775841Siedowse struct statfs fsb1; 173874462Salfred struct addrinfo *ai; 173975801Siedowse struct export_args *eap; 174074462Salfred char *cp = NULL; 17411558Srgrimes int done; 17421558Srgrimes char savedc = '\0'; 17431558Srgrimes union { 17441558Srgrimes struct ufs_args ua; 17451558Srgrimes struct iso_args ia; 17461558Srgrimes struct mfs_args ma; 17479336Sdfr struct msdosfs_args da; 174854093Ssemenu struct ntfs_args na; 17491558Srgrimes } args; 17501558Srgrimes 175175801Siedowse bzero(&args, sizeof args); 175275801Siedowse /* XXX, we assume that all xx_args look like ufs_args. */ 17531558Srgrimes args.ua.fspec = 0; 175475801Siedowse eap = &args.ua.export; 175575801Siedowse 175675801Siedowse eap->ex_flags = exflags; 175775801Siedowse eap->ex_anon = *anoncrp; 175875801Siedowse eap->ex_indexfile = ep->ex_indexfile; 175975641Siedowse if (grp->gr_type == GT_HOST) 176074462Salfred ai = grp->gr_ptr.gt_addrinfo; 176175641Siedowse else 176275641Siedowse ai = NULL; 17631558Srgrimes done = FALSE; 17641558Srgrimes while (!done) { 17651558Srgrimes switch (grp->gr_type) { 17661558Srgrimes case GT_HOST: 176775641Siedowse if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 176874462Salfred goto skip; 176975801Siedowse eap->ex_addr = ai->ai_addr; 177075801Siedowse eap->ex_addrlen = ai->ai_addrlen; 177175801Siedowse eap->ex_masklen = 0; 17721558Srgrimes break; 17731558Srgrimes case GT_NET: 177475801Siedowse if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 && 177574462Salfred have_v6 == 0) 177674462Salfred goto skip; 177775801Siedowse eap->ex_addr = 177875801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net; 177975801Siedowse eap->ex_addrlen = args.ua.export.ex_addr->sa_len; 178075801Siedowse eap->ex_mask = 178175801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask; 178275801Siedowse eap->ex_masklen = args.ua.export.ex_mask->sa_len; 17831558Srgrimes break; 178475641Siedowse case GT_DEFAULT: 178575801Siedowse eap->ex_addr = NULL; 178675801Siedowse eap->ex_addrlen = 0; 178775801Siedowse eap->ex_mask = NULL; 178875801Siedowse eap->ex_masklen = 0; 178975641Siedowse break; 17907401Swpaul case GT_IGNORE: 17917401Swpaul return(0); 17927401Swpaul break; 17931558Srgrimes default: 179437663Scharnier syslog(LOG_ERR, "bad grouptype"); 17951558Srgrimes if (cp) 17961558Srgrimes *cp = savedc; 17971558Srgrimes return (1); 17981558Srgrimes }; 17991558Srgrimes 18001558Srgrimes /* 18011558Srgrimes * XXX: 18021558Srgrimes * Maybe I should just use the fsb->f_mntonname path instead 18031558Srgrimes * of looping back up the dirp to the mount point?? 18041558Srgrimes * Also, needs to know how to export all types of local 180523681Speter * exportable file systems and not just "ufs". 18061558Srgrimes */ 18079336Sdfr while (mount(fsb->f_fstypename, dirp, 180874462Salfred fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) { 18091558Srgrimes if (cp) 18101558Srgrimes *cp-- = savedc; 18111558Srgrimes else 18121558Srgrimes cp = dirp + dirplen - 1; 18131558Srgrimes if (errno == EPERM) { 181475635Siedowse if (debug) 181575635Siedowse warnx("can't change attributes for %s", 181675635Siedowse dirp); 18171558Srgrimes syslog(LOG_ERR, 181837663Scharnier "can't change attributes for %s", dirp); 18191558Srgrimes return (1); 18201558Srgrimes } 18211558Srgrimes if (opt_flags & OP_ALLDIRS) { 182237663Scharnier syslog(LOG_ERR, "could not remount %s: %m", 18234895Swollman dirp); 18241558Srgrimes return (1); 18251558Srgrimes } 18261558Srgrimes /* back up over the last component */ 18271558Srgrimes while (*cp == '/' && cp > dirp) 18281558Srgrimes cp--; 18291558Srgrimes while (*(cp - 1) != '/' && cp > dirp) 18301558Srgrimes cp--; 18311558Srgrimes if (cp == dirp) { 18321558Srgrimes if (debug) 183337663Scharnier warnx("mnt unsucc"); 183437663Scharnier syslog(LOG_ERR, "can't export %s", dirp); 18351558Srgrimes return (1); 18361558Srgrimes } 18371558Srgrimes savedc = *cp; 18381558Srgrimes *cp = '\0'; 183975841Siedowse /* Check that we're still on the same filesystem. */ 184075841Siedowse if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid, 184175841Siedowse &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) { 184275841Siedowse *cp = savedc; 184375841Siedowse syslog(LOG_ERR, "can't export %s", dirp); 184475841Siedowse return (1); 184575841Siedowse } 18461558Srgrimes } 184774462Salfredskip: 184875641Siedowse if (ai != NULL) 184974462Salfred ai = ai->ai_next; 185075641Siedowse if (ai == NULL) 18511558Srgrimes done = TRUE; 18521558Srgrimes } 18531558Srgrimes if (cp) 18541558Srgrimes *cp = savedc; 18551558Srgrimes return (0); 18561558Srgrimes} 18571558Srgrimes 18581558Srgrimes/* 18591558Srgrimes * Translate a net address. 186075801Siedowse * 186175801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address. 18621558Srgrimes */ 18631558Srgrimesint 18641558Srgrimesget_net(cp, net, maskflg) 18651558Srgrimes char *cp; 18661558Srgrimes struct netmsk *net; 18671558Srgrimes int maskflg; 18681558Srgrimes{ 186975861Siedowse struct netent *np = NULL; 187074462Salfred char *name, *p, *prefp; 187175801Siedowse struct sockaddr_in sin; 187275861Siedowse struct sockaddr *sa = NULL; 187374462Salfred struct addrinfo hints, *ai = NULL; 187474462Salfred char netname[NI_MAXHOST]; 187574462Salfred long preflen; 18761558Srgrimes 187775635Siedowse p = prefp = NULL; 187874462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 187974462Salfred p = strchr(cp, '/'); 188074462Salfred *p = '\0'; 188174462Salfred prefp = p + 1; 188274462Salfred } 188374462Salfred 188475861Siedowse /* 188575861Siedowse * Check for a numeric address first. We wish to avoid 188675861Siedowse * possible DNS lookups in getnetbyname(). 188775861Siedowse */ 188875861Siedowse if (isxdigit(*cp) || *cp == ':') { 188974462Salfred memset(&hints, 0, sizeof hints); 189075801Siedowse /* Ensure the mask and the network have the same family. */ 189175801Siedowse if (maskflg && (opt_flags & OP_NET)) 189275801Siedowse hints.ai_family = net->nt_net.ss_family; 189375801Siedowse else if (!maskflg && (opt_flags & OP_HAVEMASK)) 189475801Siedowse hints.ai_family = net->nt_mask.ss_family; 189575801Siedowse else 189675801Siedowse hints.ai_family = AF_UNSPEC; 189774462Salfred hints.ai_flags = AI_NUMERICHOST; 189875861Siedowse if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 189975861Siedowse sa = ai->ai_addr; 190075861Siedowse if (sa != NULL && ai->ai_family == AF_INET) { 190174462Salfred /* 190275801Siedowse * The address in `cp' is really a network address, so 190375801Siedowse * use inet_network() to re-interpret this correctly. 190475801Siedowse * e.g. "127.1" means 127.1.0.0, not 127.0.0.1. 190574462Salfred */ 190675801Siedowse bzero(&sin, sizeof sin); 190774462Salfred sin.sin_family = AF_INET; 190874462Salfred sin.sin_len = sizeof sin; 190975801Siedowse sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 191074462Salfred if (debug) 191175801Siedowse fprintf(stderr, "get_net: v4 addr %s\n", 191275801Siedowse inet_ntoa(sin.sin_addr)); 191374462Salfred sa = (struct sockaddr *)&sin; 191475861Siedowse } 191575861Siedowse } 191675861Siedowse if (sa == NULL && (np = getnetbyname(cp)) != NULL) { 191775861Siedowse bzero(&sin, sizeof sin); 191875861Siedowse sin.sin_family = AF_INET; 191975861Siedowse sin.sin_len = sizeof sin; 192075861Siedowse sin.sin_addr = inet_makeaddr(np->n_net, 0); 192175861Siedowse sa = (struct sockaddr *)&sin; 192275861Siedowse } 192375861Siedowse if (sa == NULL) 192474462Salfred goto fail; 192525318Spst 192675801Siedowse if (maskflg) { 192775801Siedowse /* The specified sockaddr is a mask. */ 192875801Siedowse if (checkmask(sa) != 0) 192975801Siedowse goto fail; 193075801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 193175801Siedowse opt_flags |= OP_HAVEMASK; 193275801Siedowse } else { 193375801Siedowse /* The specified sockaddr is a network address. */ 193475801Siedowse bcopy(sa, &net->nt_net, sa->sa_len); 193574462Salfred 193675801Siedowse /* Get a network name for the export list. */ 193775801Siedowse if (np) { 193875801Siedowse name = np->n_name; 193975801Siedowse } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 194075801Siedowse NULL, 0, ninumeric) == 0) { 194175801Siedowse name = netname; 194275801Siedowse } else { 194375801Siedowse goto fail; 194475801Siedowse } 194575801Siedowse if ((net->nt_name = strdup(name)) == NULL) 194675801Siedowse out_of_mem(); 194775801Siedowse 194875801Siedowse /* 194975801Siedowse * Extract a mask from either a "/<masklen>" suffix, or 195075801Siedowse * from the class of an IPv4 address. 195175801Siedowse */ 195274462Salfred if (opt_flags & OP_MASKLEN) { 195374462Salfred preflen = strtol(prefp, NULL, 10); 195475801Siedowse if (preflen < 0L || preflen == LONG_MAX) 195574462Salfred goto fail; 195675801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 195775801Siedowse if (makemask(&net->nt_mask, (int)preflen) != 0) 195875801Siedowse goto fail; 195975801Siedowse opt_flags |= OP_HAVEMASK; 196074462Salfred *p = '/'; 196175801Siedowse } else if (sa->sa_family == AF_INET && 196275801Siedowse (opt_flags & OP_MASK) == 0) { 196375801Siedowse in_addr_t addr; 196474462Salfred 196575801Siedowse addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 196675801Siedowse if (IN_CLASSA(addr)) 196775801Siedowse preflen = 8; 196875801Siedowse else if (IN_CLASSB(addr)) 196975801Siedowse preflen = 16; 197075801Siedowse else if (IN_CLASSC(addr)) 197175801Siedowse preflen = 24; 197275801Siedowse else if (IN_CLASSD(addr)) 197375801Siedowse preflen = 28; 197475801Siedowse else 197575801Siedowse preflen = 32; /* XXX */ 197675801Siedowse 197775801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 197875801Siedowse makemask(&net->nt_mask, (int)preflen); 197975801Siedowse opt_flags |= OP_HAVEMASK; 198074462Salfred } 198174462Salfred } 198274462Salfred 198374462Salfred if (ai) 198474462Salfred freeaddrinfo(ai); 198574462Salfred return 0; 198674462Salfred 198774462Salfredfail: 198874462Salfred if (ai) 198974462Salfred freeaddrinfo(ai); 199074462Salfred return 1; 19911558Srgrimes} 19921558Srgrimes 19931558Srgrimes/* 19941558Srgrimes * Parse out the next white space separated field 19951558Srgrimes */ 19961558Srgrimesvoid 19971558Srgrimesnextfield(cp, endcp) 19981558Srgrimes char **cp; 19991558Srgrimes char **endcp; 20001558Srgrimes{ 20011558Srgrimes char *p; 20021558Srgrimes 20031558Srgrimes p = *cp; 20041558Srgrimes while (*p == ' ' || *p == '\t') 20051558Srgrimes p++; 20061558Srgrimes if (*p == '\n' || *p == '\0') 20071558Srgrimes *cp = *endcp = p; 20081558Srgrimes else { 20091558Srgrimes *cp = p++; 20101558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 20111558Srgrimes p++; 20121558Srgrimes *endcp = p; 20131558Srgrimes } 20141558Srgrimes} 20151558Srgrimes 20161558Srgrimes/* 20171558Srgrimes * Get an exports file line. Skip over blank lines and handle line 20181558Srgrimes * continuations. 20191558Srgrimes */ 20201558Srgrimesint 20211558Srgrimesget_line() 20221558Srgrimes{ 20231558Srgrimes char *p, *cp; 20241558Srgrimes int len; 20251558Srgrimes int totlen, cont_line; 20261558Srgrimes 20271558Srgrimes /* 20281558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 20291558Srgrimes */ 20301558Srgrimes p = line; 20311558Srgrimes totlen = 0; 20321558Srgrimes do { 20331558Srgrimes if (fgets(p, LINESIZ - totlen, exp_file) == NULL) 20341558Srgrimes return (0); 20351558Srgrimes len = strlen(p); 20361558Srgrimes cp = p + len - 1; 20371558Srgrimes cont_line = 0; 20381558Srgrimes while (cp >= p && 20391558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 20401558Srgrimes if (*cp == '\\') 20411558Srgrimes cont_line = 1; 20421558Srgrimes cp--; 20431558Srgrimes len--; 20441558Srgrimes } 20451558Srgrimes *++cp = '\0'; 20461558Srgrimes if (len > 0) { 20471558Srgrimes totlen += len; 20481558Srgrimes if (totlen >= LINESIZ) { 204937663Scharnier syslog(LOG_ERR, "exports line too long"); 20501558Srgrimes exit(2); 20511558Srgrimes } 20521558Srgrimes p = cp; 20531558Srgrimes } 20541558Srgrimes } while (totlen == 0 || cont_line); 20551558Srgrimes return (1); 20561558Srgrimes} 20571558Srgrimes 20581558Srgrimes/* 20591558Srgrimes * Parse a description of a credential. 20601558Srgrimes */ 20611558Srgrimesvoid 20621558Srgrimesparsecred(namelist, cr) 20631558Srgrimes char *namelist; 206472650Sgreen struct xucred *cr; 20651558Srgrimes{ 20661558Srgrimes char *name; 20671558Srgrimes int cnt; 20681558Srgrimes char *names; 20691558Srgrimes struct passwd *pw; 20701558Srgrimes struct group *gr; 20711558Srgrimes int ngroups, groups[NGROUPS + 1]; 20721558Srgrimes 20731558Srgrimes /* 207437663Scharnier * Set up the unprivileged user. 20751558Srgrimes */ 20761558Srgrimes cr->cr_uid = -2; 20771558Srgrimes cr->cr_groups[0] = -2; 20781558Srgrimes cr->cr_ngroups = 1; 20791558Srgrimes /* 20801558Srgrimes * Get the user's password table entry. 20811558Srgrimes */ 20821558Srgrimes names = strsep(&namelist, " \t\n"); 20831558Srgrimes name = strsep(&names, ":"); 20841558Srgrimes if (isdigit(*name) || *name == '-') 20851558Srgrimes pw = getpwuid(atoi(name)); 20861558Srgrimes else 20871558Srgrimes pw = getpwnam(name); 20881558Srgrimes /* 20891558Srgrimes * Credentials specified as those of a user. 20901558Srgrimes */ 20911558Srgrimes if (names == NULL) { 20921558Srgrimes if (pw == NULL) { 209337663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 20941558Srgrimes return; 20951558Srgrimes } 20961558Srgrimes cr->cr_uid = pw->pw_uid; 20971558Srgrimes ngroups = NGROUPS + 1; 20981558Srgrimes if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 209937663Scharnier syslog(LOG_ERR, "too many groups"); 21001558Srgrimes /* 21011558Srgrimes * Convert from int's to gid_t's and compress out duplicate 21021558Srgrimes */ 21031558Srgrimes cr->cr_ngroups = ngroups - 1; 21041558Srgrimes cr->cr_groups[0] = groups[0]; 21051558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 21061558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 21071558Srgrimes return; 21081558Srgrimes } 21091558Srgrimes /* 21101558Srgrimes * Explicit credential specified as a colon separated list: 21111558Srgrimes * uid:gid:gid:... 21121558Srgrimes */ 21131558Srgrimes if (pw != NULL) 21141558Srgrimes cr->cr_uid = pw->pw_uid; 21151558Srgrimes else if (isdigit(*name) || *name == '-') 21161558Srgrimes cr->cr_uid = atoi(name); 21171558Srgrimes else { 211837663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 21191558Srgrimes return; 21201558Srgrimes } 21211558Srgrimes cr->cr_ngroups = 0; 21221558Srgrimes while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) { 21231558Srgrimes name = strsep(&names, ":"); 21241558Srgrimes if (isdigit(*name) || *name == '-') { 21251558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 21261558Srgrimes } else { 21271558Srgrimes if ((gr = getgrnam(name)) == NULL) { 212837663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 21291558Srgrimes continue; 21301558Srgrimes } 21311558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 21321558Srgrimes } 21331558Srgrimes } 21341558Srgrimes if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS) 213537663Scharnier syslog(LOG_ERR, "too many groups"); 21361558Srgrimes} 21371558Srgrimes 21381558Srgrimes#define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50) 21391558Srgrimes/* 21401558Srgrimes * Routines that maintain the remote mounttab 21411558Srgrimes */ 21421558Srgrimesvoid 21431558Srgrimesget_mountlist() 21441558Srgrimes{ 21451558Srgrimes struct mountlist *mlp, **mlpp; 214623681Speter char *host, *dirp, *cp; 21471558Srgrimes char str[STRSIZ]; 21481558Srgrimes FILE *mlfile; 21491558Srgrimes 21501558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 215153117Sbillf if (errno == ENOENT) 215253117Sbillf return; 215353117Sbillf else { 215453117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 215553117Sbillf return; 215653117Sbillf } 21571558Srgrimes } 21581558Srgrimes mlpp = &mlhead; 21591558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 216023681Speter cp = str; 216123681Speter host = strsep(&cp, " \t\n"); 216223681Speter dirp = strsep(&cp, " \t\n"); 216323681Speter if (host == NULL || dirp == NULL) 21641558Srgrimes continue; 21651558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 216637663Scharnier if (mlp == (struct mountlist *)NULL) 216737663Scharnier out_of_mem(); 216823681Speter strncpy(mlp->ml_host, host, RPCMNT_NAMELEN); 216923681Speter mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 217023681Speter strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 217123681Speter mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 21721558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 21731558Srgrimes *mlpp = mlp; 21741558Srgrimes mlpp = &mlp->ml_next; 21751558Srgrimes } 21761558Srgrimes fclose(mlfile); 21771558Srgrimes} 21781558Srgrimes 217975635Siedowsevoid 218075635Siedowsedel_mlist(char *hostp, char *dirp) 21811558Srgrimes{ 21821558Srgrimes struct mountlist *mlp, **mlpp; 21831558Srgrimes struct mountlist *mlp2; 21841558Srgrimes FILE *mlfile; 21851558Srgrimes int fnd = 0; 21861558Srgrimes 21871558Srgrimes mlpp = &mlhead; 21881558Srgrimes mlp = mlhead; 21891558Srgrimes while (mlp) { 21901558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 21911558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 21921558Srgrimes fnd = 1; 21931558Srgrimes mlp2 = mlp; 21941558Srgrimes *mlpp = mlp = mlp->ml_next; 21951558Srgrimes free((caddr_t)mlp2); 21961558Srgrimes } else { 21971558Srgrimes mlpp = &mlp->ml_next; 21981558Srgrimes mlp = mlp->ml_next; 21991558Srgrimes } 22001558Srgrimes } 22011558Srgrimes if (fnd) { 22021558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 220337663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 22041558Srgrimes return; 22051558Srgrimes } 22061558Srgrimes mlp = mlhead; 22071558Srgrimes while (mlp) { 22081558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 22091558Srgrimes mlp = mlp->ml_next; 22101558Srgrimes } 22111558Srgrimes fclose(mlfile); 22121558Srgrimes } 22131558Srgrimes} 22141558Srgrimes 22151558Srgrimesvoid 22161558Srgrimesadd_mlist(hostp, dirp) 22171558Srgrimes char *hostp, *dirp; 22181558Srgrimes{ 22191558Srgrimes struct mountlist *mlp, **mlpp; 22201558Srgrimes FILE *mlfile; 22211558Srgrimes 22221558Srgrimes mlpp = &mlhead; 22231558Srgrimes mlp = mlhead; 22241558Srgrimes while (mlp) { 22251558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 22261558Srgrimes return; 22271558Srgrimes mlpp = &mlp->ml_next; 22281558Srgrimes mlp = mlp->ml_next; 22291558Srgrimes } 22301558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 223137663Scharnier if (mlp == (struct mountlist *)NULL) 223237663Scharnier out_of_mem(); 22331558Srgrimes strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN); 22341558Srgrimes mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 22351558Srgrimes strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 22361558Srgrimes mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 22371558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 22381558Srgrimes *mlpp = mlp; 22391558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 224037663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 22411558Srgrimes return; 22421558Srgrimes } 22431558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 22441558Srgrimes fclose(mlfile); 22451558Srgrimes} 22461558Srgrimes 22471558Srgrimes/* 22481558Srgrimes * Free up a group list. 22491558Srgrimes */ 22501558Srgrimesvoid 22511558Srgrimesfree_grp(grp) 22521558Srgrimes struct grouplist *grp; 22531558Srgrimes{ 22541558Srgrimes if (grp->gr_type == GT_HOST) { 225574462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 225674462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 22571558Srgrimes } else if (grp->gr_type == GT_NET) { 22581558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 22591558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 22601558Srgrimes } 22611558Srgrimes free((caddr_t)grp); 22621558Srgrimes} 22631558Srgrimes 22641558Srgrimes#ifdef DEBUG 22651558Srgrimesvoid 22661558SrgrimesSYSLOG(int pri, const char *fmt, ...) 22671558Srgrimes{ 22681558Srgrimes va_list ap; 22691558Srgrimes 22701558Srgrimes va_start(ap, fmt); 22711558Srgrimes vfprintf(stderr, fmt, ap); 22721558Srgrimes va_end(ap); 22731558Srgrimes} 22741558Srgrimes#endif /* DEBUG */ 22751558Srgrimes 22761558Srgrimes/* 22771558Srgrimes * Check options for consistency. 22781558Srgrimes */ 22791558Srgrimesint 22801558Srgrimescheck_options(dp) 22811558Srgrimes struct dirlist *dp; 22821558Srgrimes{ 22831558Srgrimes 22841558Srgrimes if (dp == (struct dirlist *)NULL) 22851558Srgrimes return (1); 22861558Srgrimes if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) || 22871558Srgrimes (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) || 22881558Srgrimes (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) { 22891558Srgrimes syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive"); 22901558Srgrimes return (1); 22911558Srgrimes } 22921558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 229375801Siedowse syslog(LOG_ERR, "-mask requires -network"); 229475801Siedowse return (1); 22951558Srgrimes } 229675801Siedowse if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) { 229775801Siedowse syslog(LOG_ERR, "-network requires mask specification"); 229875801Siedowse return (1); 229975801Siedowse } 230075801Siedowse if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) { 230175801Siedowse syslog(LOG_ERR, "-mask and /masklen are mutually exclusive"); 230275801Siedowse return (1); 230375801Siedowse } 23041558Srgrimes if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 230545927Salex syslog(LOG_ERR, "-alldirs has multiple directories"); 23061558Srgrimes return (1); 23071558Srgrimes } 23081558Srgrimes return (0); 23091558Srgrimes} 23101558Srgrimes 23111558Srgrimes/* 23121558Srgrimes * Check an absolute directory path for any symbolic links. Return true 23131558Srgrimes */ 23141558Srgrimesint 23151558Srgrimescheck_dirpath(dirp) 23161558Srgrimes char *dirp; 23171558Srgrimes{ 23181558Srgrimes char *cp; 23191558Srgrimes int ret = 1; 23201558Srgrimes struct stat sb; 23211558Srgrimes 23221558Srgrimes cp = dirp + 1; 23231558Srgrimes while (*cp && ret) { 23241558Srgrimes if (*cp == '/') { 23251558Srgrimes *cp = '\0'; 23269336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23271558Srgrimes ret = 0; 23281558Srgrimes *cp = '/'; 23291558Srgrimes } 23301558Srgrimes cp++; 23311558Srgrimes } 23329336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23331558Srgrimes ret = 0; 23341558Srgrimes return (ret); 23351558Srgrimes} 23369336Sdfr 233775801Siedowse/* 233875801Siedowse * Make a netmask according to the specified prefix length. The ss_family 233975801Siedowse * and other non-address fields must be initialised before calling this. 234075801Siedowse */ 234175801Siedowseint 234275801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen) 234374462Salfred{ 234475801Siedowse u_char *p; 234575801Siedowse int bits, i, len; 234674462Salfred 234775801Siedowse if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL) 234875801Siedowse return (-1); 234975801Siedowse if (bitlen > len * NBBY) 235075801Siedowse return (-1); 235174462Salfred 235275801Siedowse for (i = 0; i < len; i++) { 235375801Siedowse bits = (bitlen > NBBY) ? NBBY : bitlen; 235475801Siedowse *p++ = (1 << bits) - 1; 235575801Siedowse bitlen -= bits; 235674462Salfred } 235775801Siedowse return 0; 235874462Salfred} 235974462Salfred 236075801Siedowse/* 236175801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask 236275801Siedowse * is acceptable (i.e. of the form 1...10....0). 236375801Siedowse */ 236475801Siedowseint 236575801Siedowsecheckmask(struct sockaddr *sa) 236674462Salfred{ 236775801Siedowse u_char *mask; 236875801Siedowse int i, len; 236974462Salfred 237075801Siedowse if ((mask = sa_rawaddr(sa, &len)) == NULL) 237175801Siedowse return (-1); 237275801Siedowse 237375801Siedowse for (i = 0; i < len; i++) 237475801Siedowse if (mask[i] != 0xff) 237575801Siedowse break; 237675801Siedowse if (i < len) { 237775801Siedowse if (~mask[i] & (u_char)(~mask[i] + 1)) 237875801Siedowse return (-1); 237975801Siedowse i++; 238074462Salfred } 238175801Siedowse for (; i < len; i++) 238275801Siedowse if (mask[i] != 0) 238375801Siedowse return (-1); 238475801Siedowse return (0); 238574462Salfred} 238674462Salfred 238775801Siedowse/* 238875801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if 238975801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'. 239075801Siedowse * If samask is NULL, perform a full comparision. 239175801Siedowse */ 239275801Siedowseint 239375801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask) 239474462Salfred{ 239575801Siedowse unsigned char *p1, *p2, *mask; 239675801Siedowse int len, i; 239774462Salfred 239875801Siedowse if (sa1->sa_family != sa2->sa_family || 239975801Siedowse (p1 = sa_rawaddr(sa1, &len)) == NULL || 240075801Siedowse (p2 = sa_rawaddr(sa2, NULL)) == NULL) 240175801Siedowse return (1); 240275801Siedowse 240375801Siedowse switch (sa1->sa_family) { 240474462Salfred case AF_INET6: 240575801Siedowse if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 240675801Siedowse ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 240775801Siedowse return (1); 240874462Salfred break; 240974462Salfred } 241074462Salfred 241175801Siedowse /* Simple binary comparison if no mask specified. */ 241275801Siedowse if (samask == NULL) 241375801Siedowse return (memcmp(p1, p2, len)); 241474462Salfred 241575801Siedowse /* Set up the mask, and do a mask-based comparison. */ 241675801Siedowse if (sa1->sa_family != samask->sa_family || 241775801Siedowse (mask = sa_rawaddr(samask, NULL)) == NULL) 241875801Siedowse return (1); 241974462Salfred 242075801Siedowse for (i = 0; i < len; i++) 242175801Siedowse if ((p1[i] & mask[i]) != (p2[i] & mask[i])) 242275801Siedowse return (1); 242375801Siedowse return (0); 242474462Salfred} 242574462Salfred 242675801Siedowse/* 242775801Siedowse * Return a pointer to the part of the sockaddr that contains the 242875801Siedowse * raw address, and set *nbytes to its length in bytes. Returns 242975801Siedowse * NULL if the address family is unknown. 243075801Siedowse */ 243175801Siedowsevoid * 243275801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) { 243375801Siedowse void *p; 243474462Salfred int len; 243574462Salfred 243675801Siedowse switch (sa->sa_family) { 243774462Salfred case AF_INET: 243875801Siedowse len = sizeof(((struct sockaddr_in *)sa)->sin_addr); 243975801Siedowse p = &((struct sockaddr_in *)sa)->sin_addr; 244074462Salfred break; 244174462Salfred case AF_INET6: 244275801Siedowse len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); 244375801Siedowse p = &((struct sockaddr_in6 *)sa)->sin6_addr; 244474462Salfred break; 244574462Salfred default: 244675801Siedowse p = NULL; 244775801Siedowse len = 0; 244874462Salfred } 244974462Salfred 245075801Siedowse if (nbytes != NULL) 245175801Siedowse *nbytes = len; 245275801Siedowse return (p); 245374462Salfred} 245474462Salfred 245575754Siedowsevoid 245675754Siedowsehuphandler(int sig) 245775754Siedowse{ 245875754Siedowse got_sighup = 1; 245975754Siedowse} 246075754Siedowse 246174462Salfredvoid terminate(sig) 246274462Salfredint sig; 246374462Salfred{ 246474462Salfred close(mountdlockfd); 246574462Salfred unlink(MOUNTDLOCK); 246674792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 246774792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 246874462Salfred exit (0); 246974462Salfred} 2470