mountd.c revision 77435
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 77435 2001-05-29 20:39:47Z phk $"; 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> 6677162Sru#include <fs/msdosfs/msdosfsmount.h> 6777223Sru#include <fs/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; 9409336Sdfr struct msdosfs_args da; 94154093Ssemenu struct ntfs_args na; 9421558Srgrimes } targs; 9431558Srgrimes 94477435Sphk if (!strcmp(fsp->f_fstypename, "ufs") || 94523681Speter !strcmp(fsp->f_fstypename, "msdos") || 94654093Ssemenu !strcmp(fsp->f_fstypename, "ntfs") || 94723681Speter !strcmp(fsp->f_fstypename, "cd9660")) { 9489336Sdfr targs.ua.fspec = NULL; 9499336Sdfr targs.ua.export.ex_flags = MNT_DELEXPORT; 9509336Sdfr if (mount(fsp->f_fstypename, fsp->f_mntonname, 95177405Siedowse fsp->f_flags | MNT_UPDATE, (caddr_t)&targs) < 0 && 95277405Siedowse errno != ENOENT) 95377405Siedowse syslog(LOG_ERR, 95477405Siedowse "can't delete exports for %s: %m", 95574462Salfred fsp->f_mntonname); 9561558Srgrimes } 9571558Srgrimes fsp++; 9581558Srgrimes } 9591558Srgrimes 9601558Srgrimes /* 9611558Srgrimes * Read in the exports file and build the list, calling 9621558Srgrimes * mount() as we go along to push the export rules into the kernel. 9631558Srgrimes */ 9641558Srgrimes if ((exp_file = fopen(exname, "r")) == NULL) { 96537663Scharnier syslog(LOG_ERR, "can't open %s", exname); 9661558Srgrimes exit(2); 9671558Srgrimes } 9681558Srgrimes dirhead = (struct dirlist *)NULL; 9691558Srgrimes while (get_line()) { 9701558Srgrimes if (debug) 97137663Scharnier warnx("got line %s", line); 9721558Srgrimes cp = line; 9731558Srgrimes nextfield(&cp, &endcp); 9741558Srgrimes if (*cp == '#') 9751558Srgrimes goto nextline; 9761558Srgrimes 9771558Srgrimes /* 9781558Srgrimes * Set defaults. 9791558Srgrimes */ 9801558Srgrimes has_host = FALSE; 9811558Srgrimes anon = def_anon; 9821558Srgrimes exflags = MNT_EXPORTED; 9831558Srgrimes got_nondir = 0; 9841558Srgrimes opt_flags = 0; 9851558Srgrimes ep = (struct exportlist *)NULL; 9861558Srgrimes 9871558Srgrimes /* 9881558Srgrimes * Create new exports list entry 9891558Srgrimes */ 9901558Srgrimes len = endcp-cp; 9911558Srgrimes tgrp = grp = get_grp(); 9921558Srgrimes while (len > 0) { 9931558Srgrimes if (len > RPCMNT_NAMELEN) { 9941558Srgrimes getexp_err(ep, tgrp); 9951558Srgrimes goto nextline; 9961558Srgrimes } 9971558Srgrimes if (*cp == '-') { 9981558Srgrimes if (ep == (struct exportlist *)NULL) { 9991558Srgrimes getexp_err(ep, tgrp); 10001558Srgrimes goto nextline; 10011558Srgrimes } 10021558Srgrimes if (debug) 100337663Scharnier warnx("doing opt %s", cp); 10041558Srgrimes got_nondir = 1; 10051558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 10061558Srgrimes &exflags, &anon)) { 10071558Srgrimes getexp_err(ep, tgrp); 10081558Srgrimes goto nextline; 10091558Srgrimes } 10101558Srgrimes } else if (*cp == '/') { 10111558Srgrimes savedc = *endcp; 10121558Srgrimes *endcp = '\0'; 10131558Srgrimes if (check_dirpath(cp) && 10141558Srgrimes statfs(cp, &fsb) >= 0) { 10151558Srgrimes if (got_nondir) { 101637663Scharnier syslog(LOG_ERR, "dirs must be first"); 10171558Srgrimes getexp_err(ep, tgrp); 10181558Srgrimes goto nextline; 10191558Srgrimes } 10201558Srgrimes if (ep) { 10211558Srgrimes if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] || 10221558Srgrimes ep->ex_fs.val[1] != fsb.f_fsid.val[1]) { 10231558Srgrimes getexp_err(ep, tgrp); 10241558Srgrimes goto nextline; 10251558Srgrimes } 10261558Srgrimes } else { 10271558Srgrimes /* 10281558Srgrimes * See if this directory is already 10291558Srgrimes * in the list. 10301558Srgrimes */ 10311558Srgrimes ep = ex_search(&fsb.f_fsid); 10321558Srgrimes if (ep == (struct exportlist *)NULL) { 10331558Srgrimes ep = get_exp(); 10341558Srgrimes ep->ex_fs = fsb.f_fsid; 10351558Srgrimes ep->ex_fsdir = (char *) 10361558Srgrimes malloc(strlen(fsb.f_mntonname) + 1); 10371558Srgrimes if (ep->ex_fsdir) 10381558Srgrimes strcpy(ep->ex_fsdir, 10391558Srgrimes fsb.f_mntonname); 10401558Srgrimes else 10411558Srgrimes out_of_mem(); 10421558Srgrimes if (debug) 104374462Salfred warnx("making new ep fs=0x%x,0x%x", 104474462Salfred fsb.f_fsid.val[0], 104574462Salfred fsb.f_fsid.val[1]); 10461558Srgrimes } else if (debug) 104737663Scharnier warnx("found ep fs=0x%x,0x%x", 10481558Srgrimes fsb.f_fsid.val[0], 10491558Srgrimes fsb.f_fsid.val[1]); 10501558Srgrimes } 10511558Srgrimes 10521558Srgrimes /* 10531558Srgrimes * Add dirpath to export mount point. 10541558Srgrimes */ 10551558Srgrimes dirp = add_expdir(&dirhead, cp, len); 10561558Srgrimes dirplen = len; 10571558Srgrimes } else { 10581558Srgrimes getexp_err(ep, tgrp); 10591558Srgrimes goto nextline; 10601558Srgrimes } 10611558Srgrimes *endcp = savedc; 10621558Srgrimes } else { 10631558Srgrimes savedc = *endcp; 10641558Srgrimes *endcp = '\0'; 10651558Srgrimes got_nondir = 1; 10661558Srgrimes if (ep == (struct exportlist *)NULL) { 10671558Srgrimes getexp_err(ep, tgrp); 10681558Srgrimes goto nextline; 10691558Srgrimes } 10701558Srgrimes 10711558Srgrimes /* 10721558Srgrimes * Get the host or netgroup. 10731558Srgrimes */ 10741558Srgrimes setnetgrent(cp); 10751558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 10761558Srgrimes do { 10771558Srgrimes if (has_host) { 10781558Srgrimes grp->gr_next = get_grp(); 10791558Srgrimes grp = grp->gr_next; 10801558Srgrimes } 10811558Srgrimes if (netgrp) { 108237003Sjoerg if (hst == 0) { 108337663Scharnier syslog(LOG_ERR, 108437663Scharnier "null hostname in netgroup %s, skipping", cp); 108537004Sjoerg grp->gr_type = GT_IGNORE; 108637003Sjoerg } else if (get_host(hst, grp, tgrp)) { 108737663Scharnier syslog(LOG_ERR, 108837663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 108929317Sjlemon grp->gr_type = GT_IGNORE; 10901558Srgrimes } 10917401Swpaul } else if (get_host(cp, grp, tgrp)) { 109237663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 109329317Sjlemon grp->gr_type = GT_IGNORE; 10941558Srgrimes } 10951558Srgrimes has_host = TRUE; 10961558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 10971558Srgrimes endnetgrent(); 10981558Srgrimes *endcp = savedc; 10991558Srgrimes } 11001558Srgrimes cp = endcp; 11011558Srgrimes nextfield(&cp, &endcp); 11021558Srgrimes len = endcp - cp; 11031558Srgrimes } 11041558Srgrimes if (check_options(dirhead)) { 11051558Srgrimes getexp_err(ep, tgrp); 11061558Srgrimes goto nextline; 11071558Srgrimes } 11081558Srgrimes if (!has_host) { 110975641Siedowse grp->gr_type = GT_DEFAULT; 11101558Srgrimes if (debug) 111137663Scharnier warnx("adding a default entry"); 11121558Srgrimes 11131558Srgrimes /* 11141558Srgrimes * Don't allow a network export coincide with a list of 11151558Srgrimes * host(s) on the same line. 11161558Srgrimes */ 11171558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 111875801Siedowse syslog(LOG_ERR, "network/host conflict"); 11191558Srgrimes getexp_err(ep, tgrp); 11201558Srgrimes goto nextline; 112129317Sjlemon 112274462Salfred /* 112374462Salfred * If an export list was specified on this line, make sure 112429317Sjlemon * that we have at least one valid entry, otherwise skip it. 112529317Sjlemon */ 112629317Sjlemon } else { 112729317Sjlemon grp = tgrp; 112874462Salfred while (grp && grp->gr_type == GT_IGNORE) 112929317Sjlemon grp = grp->gr_next; 113029317Sjlemon if (! grp) { 113129317Sjlemon getexp_err(ep, tgrp); 113229317Sjlemon goto nextline; 113329317Sjlemon } 11341558Srgrimes } 11351558Srgrimes 11361558Srgrimes /* 11371558Srgrimes * Loop through hosts, pushing the exports into the kernel. 11381558Srgrimes * After loop, tgrp points to the start of the list and 11391558Srgrimes * grp points to the last entry in the list. 11401558Srgrimes */ 11411558Srgrimes grp = tgrp; 11421558Srgrimes do { 114375635Siedowse if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 114475635Siedowse &fsb)) { 114575635Siedowse getexp_err(ep, tgrp); 114675635Siedowse goto nextline; 114775635Siedowse } 11481558Srgrimes } while (grp->gr_next && (grp = grp->gr_next)); 11491558Srgrimes 11501558Srgrimes /* 11511558Srgrimes * Success. Update the data structures. 11521558Srgrimes */ 11531558Srgrimes if (has_host) { 11549336Sdfr hang_dirp(dirhead, tgrp, ep, opt_flags); 11551558Srgrimes grp->gr_next = grphead; 11561558Srgrimes grphead = tgrp; 11571558Srgrimes } else { 11581558Srgrimes hang_dirp(dirhead, (struct grouplist *)NULL, ep, 11599336Sdfr opt_flags); 11601558Srgrimes free_grp(grp); 11611558Srgrimes } 11621558Srgrimes dirhead = (struct dirlist *)NULL; 11631558Srgrimes if ((ep->ex_flag & EX_LINKED) == 0) { 11641558Srgrimes ep2 = exphead; 11651558Srgrimes epp = &exphead; 11661558Srgrimes 11671558Srgrimes /* 11681558Srgrimes * Insert in the list in alphabetical order. 11691558Srgrimes */ 11701558Srgrimes while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { 11711558Srgrimes epp = &ep2->ex_next; 11721558Srgrimes ep2 = ep2->ex_next; 11731558Srgrimes } 11741558Srgrimes if (ep2) 11751558Srgrimes ep->ex_next = ep2; 11761558Srgrimes *epp = ep; 11771558Srgrimes ep->ex_flag |= EX_LINKED; 11781558Srgrimes } 11791558Srgrimesnextline: 11801558Srgrimes if (dirhead) { 11811558Srgrimes free_dir(dirhead); 11821558Srgrimes dirhead = (struct dirlist *)NULL; 11831558Srgrimes } 11841558Srgrimes } 11851558Srgrimes fclose(exp_file); 11861558Srgrimes} 11871558Srgrimes 11881558Srgrimes/* 11891558Srgrimes * Allocate an export list element 11901558Srgrimes */ 11911558Srgrimesstruct exportlist * 11921558Srgrimesget_exp() 11931558Srgrimes{ 11941558Srgrimes struct exportlist *ep; 11951558Srgrimes 11961558Srgrimes ep = (struct exportlist *)malloc(sizeof (struct exportlist)); 11971558Srgrimes if (ep == (struct exportlist *)NULL) 11981558Srgrimes out_of_mem(); 119923681Speter memset(ep, 0, sizeof(struct exportlist)); 12001558Srgrimes return (ep); 12011558Srgrimes} 12021558Srgrimes 12031558Srgrimes/* 12041558Srgrimes * Allocate a group list element 12051558Srgrimes */ 12061558Srgrimesstruct grouplist * 12071558Srgrimesget_grp() 12081558Srgrimes{ 12091558Srgrimes struct grouplist *gp; 12101558Srgrimes 12111558Srgrimes gp = (struct grouplist *)malloc(sizeof (struct grouplist)); 12121558Srgrimes if (gp == (struct grouplist *)NULL) 12131558Srgrimes out_of_mem(); 121423681Speter memset(gp, 0, sizeof(struct grouplist)); 12151558Srgrimes return (gp); 12161558Srgrimes} 12171558Srgrimes 12181558Srgrimes/* 12191558Srgrimes * Clean up upon an error in get_exportlist(). 12201558Srgrimes */ 12211558Srgrimesvoid 12221558Srgrimesgetexp_err(ep, grp) 12231558Srgrimes struct exportlist *ep; 12241558Srgrimes struct grouplist *grp; 12251558Srgrimes{ 12261558Srgrimes struct grouplist *tgrp; 12271558Srgrimes 122837663Scharnier syslog(LOG_ERR, "bad exports list line %s", line); 12291558Srgrimes if (ep && (ep->ex_flag & EX_LINKED) == 0) 12301558Srgrimes free_exp(ep); 12311558Srgrimes while (grp) { 12321558Srgrimes tgrp = grp; 12331558Srgrimes grp = grp->gr_next; 12341558Srgrimes free_grp(tgrp); 12351558Srgrimes } 12361558Srgrimes} 12371558Srgrimes 12381558Srgrimes/* 12391558Srgrimes * Search the export list for a matching fs. 12401558Srgrimes */ 12411558Srgrimesstruct exportlist * 12421558Srgrimesex_search(fsid) 12431558Srgrimes fsid_t *fsid; 12441558Srgrimes{ 12451558Srgrimes struct exportlist *ep; 12461558Srgrimes 12471558Srgrimes ep = exphead; 12481558Srgrimes while (ep) { 12491558Srgrimes if (ep->ex_fs.val[0] == fsid->val[0] && 12501558Srgrimes ep->ex_fs.val[1] == fsid->val[1]) 12511558Srgrimes return (ep); 12521558Srgrimes ep = ep->ex_next; 12531558Srgrimes } 12541558Srgrimes return (ep); 12551558Srgrimes} 12561558Srgrimes 12571558Srgrimes/* 12581558Srgrimes * Add a directory path to the list. 12591558Srgrimes */ 12601558Srgrimeschar * 12611558Srgrimesadd_expdir(dpp, cp, len) 12621558Srgrimes struct dirlist **dpp; 12631558Srgrimes char *cp; 12641558Srgrimes int len; 12651558Srgrimes{ 12661558Srgrimes struct dirlist *dp; 12671558Srgrimes 12681558Srgrimes dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); 126937663Scharnier if (dp == (struct dirlist *)NULL) 127037663Scharnier out_of_mem(); 12711558Srgrimes dp->dp_left = *dpp; 12721558Srgrimes dp->dp_right = (struct dirlist *)NULL; 12731558Srgrimes dp->dp_flag = 0; 12741558Srgrimes dp->dp_hosts = (struct hostlist *)NULL; 12751558Srgrimes strcpy(dp->dp_dirp, cp); 12761558Srgrimes *dpp = dp; 12771558Srgrimes return (dp->dp_dirp); 12781558Srgrimes} 12791558Srgrimes 12801558Srgrimes/* 12811558Srgrimes * Hang the dir list element off the dirpath binary tree as required 12821558Srgrimes * and update the entry for host. 12831558Srgrimes */ 12841558Srgrimesvoid 12859336Sdfrhang_dirp(dp, grp, ep, flags) 12861558Srgrimes struct dirlist *dp; 12871558Srgrimes struct grouplist *grp; 12881558Srgrimes struct exportlist *ep; 12899336Sdfr int flags; 12901558Srgrimes{ 12911558Srgrimes struct hostlist *hp; 12921558Srgrimes struct dirlist *dp2; 12931558Srgrimes 12949336Sdfr if (flags & OP_ALLDIRS) { 12951558Srgrimes if (ep->ex_defdir) 12961558Srgrimes free((caddr_t)dp); 12971558Srgrimes else 12981558Srgrimes ep->ex_defdir = dp; 12999336Sdfr if (grp == (struct grouplist *)NULL) { 13001558Srgrimes ep->ex_defdir->dp_flag |= DP_DEFSET; 13019336Sdfr if (flags & OP_KERB) 13029336Sdfr ep->ex_defdir->dp_flag |= DP_KERB; 13039336Sdfr } else while (grp) { 13041558Srgrimes hp = get_ht(); 13059336Sdfr if (flags & OP_KERB) 13069336Sdfr hp->ht_flag |= DP_KERB; 13071558Srgrimes hp->ht_grp = grp; 13081558Srgrimes hp->ht_next = ep->ex_defdir->dp_hosts; 13091558Srgrimes ep->ex_defdir->dp_hosts = hp; 13101558Srgrimes grp = grp->gr_next; 13111558Srgrimes } 13121558Srgrimes } else { 13131558Srgrimes 13141558Srgrimes /* 131537663Scharnier * Loop through the directories adding them to the tree. 13161558Srgrimes */ 13171558Srgrimes while (dp) { 13181558Srgrimes dp2 = dp->dp_left; 13199336Sdfr add_dlist(&ep->ex_dirl, dp, grp, flags); 13201558Srgrimes dp = dp2; 13211558Srgrimes } 13221558Srgrimes } 13231558Srgrimes} 13241558Srgrimes 13251558Srgrimes/* 13261558Srgrimes * Traverse the binary tree either updating a node that is already there 13271558Srgrimes * for the new directory or adding the new node. 13281558Srgrimes */ 13291558Srgrimesvoid 13309336Sdfradd_dlist(dpp, newdp, grp, flags) 13311558Srgrimes struct dirlist **dpp; 13321558Srgrimes struct dirlist *newdp; 13331558Srgrimes struct grouplist *grp; 13349336Sdfr int flags; 13351558Srgrimes{ 13361558Srgrimes struct dirlist *dp; 13371558Srgrimes struct hostlist *hp; 13381558Srgrimes int cmp; 13391558Srgrimes 13401558Srgrimes dp = *dpp; 13411558Srgrimes if (dp) { 13421558Srgrimes cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 13431558Srgrimes if (cmp > 0) { 13449336Sdfr add_dlist(&dp->dp_left, newdp, grp, flags); 13451558Srgrimes return; 13461558Srgrimes } else if (cmp < 0) { 13479336Sdfr add_dlist(&dp->dp_right, newdp, grp, flags); 13481558Srgrimes return; 13491558Srgrimes } else 13501558Srgrimes free((caddr_t)newdp); 13511558Srgrimes } else { 13521558Srgrimes dp = newdp; 13531558Srgrimes dp->dp_left = (struct dirlist *)NULL; 13541558Srgrimes *dpp = dp; 13551558Srgrimes } 13561558Srgrimes if (grp) { 13571558Srgrimes 13581558Srgrimes /* 13591558Srgrimes * Hang all of the host(s) off of the directory point. 13601558Srgrimes */ 13611558Srgrimes do { 13621558Srgrimes hp = get_ht(); 13639336Sdfr if (flags & OP_KERB) 13649336Sdfr hp->ht_flag |= DP_KERB; 13651558Srgrimes hp->ht_grp = grp; 13661558Srgrimes hp->ht_next = dp->dp_hosts; 13671558Srgrimes dp->dp_hosts = hp; 13681558Srgrimes grp = grp->gr_next; 13691558Srgrimes } while (grp); 13709336Sdfr } else { 13711558Srgrimes dp->dp_flag |= DP_DEFSET; 13729336Sdfr if (flags & OP_KERB) 13739336Sdfr dp->dp_flag |= DP_KERB; 13749336Sdfr } 13751558Srgrimes} 13761558Srgrimes 13771558Srgrimes/* 13781558Srgrimes * Search for a dirpath on the export point. 13791558Srgrimes */ 13801558Srgrimesstruct dirlist * 138174462Salfreddirp_search(dp, dirp) 13821558Srgrimes struct dirlist *dp; 138374462Salfred char *dirp; 13841558Srgrimes{ 13851558Srgrimes int cmp; 13861558Srgrimes 13871558Srgrimes if (dp) { 138874462Salfred cmp = strcmp(dp->dp_dirp, dirp); 13891558Srgrimes if (cmp > 0) 139074462Salfred return (dirp_search(dp->dp_left, dirp)); 13911558Srgrimes else if (cmp < 0) 139274462Salfred return (dirp_search(dp->dp_right, dirp)); 13931558Srgrimes else 13941558Srgrimes return (dp); 13951558Srgrimes } 13961558Srgrimes return (dp); 13971558Srgrimes} 13981558Srgrimes 13991558Srgrimes/* 14001558Srgrimes * Scan for a host match in a directory tree. 14011558Srgrimes */ 14021558Srgrimesint 14039336Sdfrchk_host(dp, saddr, defsetp, hostsetp) 14041558Srgrimes struct dirlist *dp; 140574462Salfred struct sockaddr *saddr; 14061558Srgrimes int *defsetp; 14079336Sdfr int *hostsetp; 14081558Srgrimes{ 14091558Srgrimes struct hostlist *hp; 14101558Srgrimes struct grouplist *grp; 141174462Salfred struct addrinfo *ai; 14121558Srgrimes 14131558Srgrimes if (dp) { 14141558Srgrimes if (dp->dp_flag & DP_DEFSET) 14159336Sdfr *defsetp = dp->dp_flag; 14161558Srgrimes hp = dp->dp_hosts; 14171558Srgrimes while (hp) { 14181558Srgrimes grp = hp->ht_grp; 14191558Srgrimes switch (grp->gr_type) { 14201558Srgrimes case GT_HOST: 142174462Salfred ai = grp->gr_ptr.gt_addrinfo; 142274462Salfred for (; ai; ai = ai->ai_next) { 142375801Siedowse if (!sacmp(ai->ai_addr, saddr, NULL)) { 142474462Salfred *hostsetp = 142574462Salfred (hp->ht_flag | DP_HOSTSET); 142674462Salfred return (1); 142774462Salfred } 14289336Sdfr } 142975801Siedowse break; 14301558Srgrimes case GT_NET: 143175801Siedowse if (!sacmp(saddr, (struct sockaddr *) 143275801Siedowse &grp->gr_ptr.gt_net.nt_net, 143375801Siedowse (struct sockaddr *) 143475801Siedowse &grp->gr_ptr.gt_net.nt_mask)) { 143574462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 143674462Salfred return (1); 143774462Salfred } 143875801Siedowse break; 143975801Siedowse } 14401558Srgrimes hp = hp->ht_next; 14411558Srgrimes } 14421558Srgrimes } 14431558Srgrimes return (0); 14441558Srgrimes} 14451558Srgrimes 14461558Srgrimes/* 14471558Srgrimes * Scan tree for a host that matches the address. 14481558Srgrimes */ 14491558Srgrimesint 14501558Srgrimesscan_tree(dp, saddr) 14511558Srgrimes struct dirlist *dp; 145274462Salfred struct sockaddr *saddr; 14531558Srgrimes{ 14549336Sdfr int defset, hostset; 14551558Srgrimes 14561558Srgrimes if (dp) { 14571558Srgrimes if (scan_tree(dp->dp_left, saddr)) 14581558Srgrimes return (1); 14599336Sdfr if (chk_host(dp, saddr, &defset, &hostset)) 14601558Srgrimes return (1); 14611558Srgrimes if (scan_tree(dp->dp_right, saddr)) 14621558Srgrimes return (1); 14631558Srgrimes } 14641558Srgrimes return (0); 14651558Srgrimes} 14661558Srgrimes 14671558Srgrimes/* 14681558Srgrimes * Traverse the dirlist tree and free it up. 14691558Srgrimes */ 14701558Srgrimesvoid 14711558Srgrimesfree_dir(dp) 14721558Srgrimes struct dirlist *dp; 14731558Srgrimes{ 14741558Srgrimes 14751558Srgrimes if (dp) { 14761558Srgrimes free_dir(dp->dp_left); 14771558Srgrimes free_dir(dp->dp_right); 14781558Srgrimes free_host(dp->dp_hosts); 14791558Srgrimes free((caddr_t)dp); 14801558Srgrimes } 14811558Srgrimes} 14821558Srgrimes 14831558Srgrimes/* 14841558Srgrimes * Parse the option string and update fields. 14851558Srgrimes * Option arguments may either be -<option>=<value> or 14861558Srgrimes * -<option> <value> 14871558Srgrimes */ 14881558Srgrimesint 14891558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr) 14901558Srgrimes char **cpp, **endcpp; 14911558Srgrimes struct exportlist *ep; 14921558Srgrimes struct grouplist *grp; 14931558Srgrimes int *has_hostp; 14941558Srgrimes int *exflagsp; 149572650Sgreen struct xucred *cr; 14961558Srgrimes{ 14971558Srgrimes char *cpoptarg, *cpoptend; 14981558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 14991558Srgrimes int allflag, usedarg; 15001558Srgrimes 150151968Salfred savedc2 = '\0'; 15021558Srgrimes cpopt = *cpp; 15031558Srgrimes cpopt++; 15041558Srgrimes cp = *endcpp; 15051558Srgrimes savedc = *cp; 15061558Srgrimes *cp = '\0'; 15071558Srgrimes while (cpopt && *cpopt) { 15081558Srgrimes allflag = 1; 15091558Srgrimes usedarg = -2; 151037663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 15111558Srgrimes *cpoptend++ = '\0'; 151237663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 15131558Srgrimes *cpoptarg++ = '\0'; 15141558Srgrimes } else { 151537663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 15161558Srgrimes *cpoptarg++ = '\0'; 15171558Srgrimes else { 15181558Srgrimes *cp = savedc; 15191558Srgrimes nextfield(&cp, &endcp); 15201558Srgrimes **endcpp = '\0'; 15211558Srgrimes if (endcp > cp && *cp != '-') { 15221558Srgrimes cpoptarg = cp; 15231558Srgrimes savedc2 = *endcp; 15241558Srgrimes *endcp = '\0'; 15251558Srgrimes usedarg = 0; 15261558Srgrimes } 15271558Srgrimes } 15281558Srgrimes } 15291558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 15301558Srgrimes *exflagsp |= MNT_EXRDONLY; 15311558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 15321558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 15331558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 15341558Srgrimes usedarg++; 15351558Srgrimes parsecred(cpoptarg, cr); 15361558Srgrimes if (allflag == 0) { 15371558Srgrimes *exflagsp |= MNT_EXPORTANON; 15381558Srgrimes opt_flags |= OP_MAPALL; 15391558Srgrimes } else 15401558Srgrimes opt_flags |= OP_MAPROOT; 15411558Srgrimes } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) { 15421558Srgrimes *exflagsp |= MNT_EXKERB; 15431558Srgrimes opt_flags |= OP_KERB; 15441558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 154575801Siedowse !strcmp(cpopt, "m"))) { 15461558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 154737663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 15481558Srgrimes return (1); 15491558Srgrimes } 15501558Srgrimes usedarg++; 15511558Srgrimes opt_flags |= OP_MASK; 15521558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 15531558Srgrimes !strcmp(cpopt, "n"))) { 155474462Salfred if (strchr(cpoptarg, '/') != NULL) { 155574462Salfred if (debug) 155674462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 155774462Salfred opt_flags |= OP_MASKLEN; 155874462Salfred } 15591558Srgrimes if (grp->gr_type != GT_NULL) { 156037663Scharnier syslog(LOG_ERR, "network/host conflict"); 15611558Srgrimes return (1); 15621558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 156337663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 15641558Srgrimes return (1); 15651558Srgrimes } 15661558Srgrimes grp->gr_type = GT_NET; 15671558Srgrimes *has_hostp = 1; 15681558Srgrimes usedarg++; 15691558Srgrimes opt_flags |= OP_NET; 15701558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 15711558Srgrimes opt_flags |= OP_ALLDIRS; 157227447Sdfr } else if (!strcmp(cpopt, "public")) { 157327447Sdfr *exflagsp |= MNT_EXPUBLIC; 157427447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 157527447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 157627447Sdfr opt_flags |= OP_MAPALL; 157727447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 157827447Sdfr ep->ex_indexfile = strdup(cpoptarg); 15791558Srgrimes } else { 158037663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 15811558Srgrimes return (1); 15821558Srgrimes } 15831558Srgrimes if (usedarg >= 0) { 15841558Srgrimes *endcp = savedc2; 15851558Srgrimes **endcpp = savedc; 15861558Srgrimes if (usedarg > 0) { 15871558Srgrimes *cpp = cp; 15881558Srgrimes *endcpp = endcp; 15891558Srgrimes } 15901558Srgrimes return (0); 15911558Srgrimes } 15921558Srgrimes cpopt = cpoptend; 15931558Srgrimes } 15941558Srgrimes **endcpp = savedc; 15951558Srgrimes return (0); 15961558Srgrimes} 15971558Srgrimes 15981558Srgrimes/* 15991558Srgrimes * Translate a character string to the corresponding list of network 16001558Srgrimes * addresses for a hostname. 16011558Srgrimes */ 16021558Srgrimesint 16037401Swpaulget_host(cp, grp, tgrp) 16041558Srgrimes char *cp; 16051558Srgrimes struct grouplist *grp; 16067401Swpaul struct grouplist *tgrp; 16071558Srgrimes{ 16087401Swpaul struct grouplist *checkgrp; 160975635Siedowse struct addrinfo *ai, *tai, hints; 161074462Salfred int ecode; 161174462Salfred char host[NI_MAXHOST]; 16121558Srgrimes 161374462Salfred if (grp->gr_type != GT_NULL) { 161474462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 16151558Srgrimes return (1); 16161558Srgrimes } 161774462Salfred memset(&hints, 0, sizeof hints); 161874462Salfred hints.ai_flags = AI_CANONNAME; 161974462Salfred hints.ai_protocol = IPPROTO_UDP; 162074462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 162174462Salfred if (ecode != 0) { 162275635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 162374462Salfred return 1; 162474462Salfred } 162574462Salfred grp->gr_ptr.gt_addrinfo = ai; 162674462Salfred while (ai != NULL) { 162774462Salfred if (ai->ai_canonname == NULL) { 162874462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 162974462Salfred sizeof host, NULL, 0, ninumeric) != 0) 163074462Salfred strlcpy(host, "?", sizeof(host)); 163174462Salfred ai->ai_canonname = strdup(host); 163274462Salfred ai->ai_flags |= AI_CANONNAME; 163375641Siedowse } 163474462Salfred if (debug) 163575635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 163675635Siedowse /* 163775635Siedowse * Sanity check: make sure we don't already have an entry 163875635Siedowse * for this host in the grouplist. 163975635Siedowse */ 164075635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 164175635Siedowse checkgrp = checkgrp->gr_next) { 164275635Siedowse if (checkgrp->gr_type != GT_HOST) 164375635Siedowse continue; 164475635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 164575635Siedowse tai = tai->ai_next) { 164675801Siedowse if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0) 164775635Siedowse continue; 164875635Siedowse if (debug) 164975635Siedowse fprintf(stderr, 165075635Siedowse "ignoring duplicate host %s\n", 165175635Siedowse ai->ai_canonname); 165275635Siedowse grp->gr_type = GT_IGNORE; 165375635Siedowse return (0); 165475635Siedowse } 165575635Siedowse } 165674462Salfred ai = ai->ai_next; 16571558Srgrimes } 165875635Siedowse grp->gr_type = GT_HOST; 16591558Srgrimes return (0); 16601558Srgrimes} 16611558Srgrimes 16621558Srgrimes/* 16631558Srgrimes * Free up an exports list component 16641558Srgrimes */ 16651558Srgrimesvoid 16661558Srgrimesfree_exp(ep) 16671558Srgrimes struct exportlist *ep; 16681558Srgrimes{ 16691558Srgrimes 16701558Srgrimes if (ep->ex_defdir) { 16711558Srgrimes free_host(ep->ex_defdir->dp_hosts); 16721558Srgrimes free((caddr_t)ep->ex_defdir); 16731558Srgrimes } 16741558Srgrimes if (ep->ex_fsdir) 16751558Srgrimes free(ep->ex_fsdir); 167627447Sdfr if (ep->ex_indexfile) 167727447Sdfr free(ep->ex_indexfile); 16781558Srgrimes free_dir(ep->ex_dirl); 16791558Srgrimes free((caddr_t)ep); 16801558Srgrimes} 16811558Srgrimes 16821558Srgrimes/* 16831558Srgrimes * Free hosts. 16841558Srgrimes */ 16851558Srgrimesvoid 16861558Srgrimesfree_host(hp) 16871558Srgrimes struct hostlist *hp; 16881558Srgrimes{ 16891558Srgrimes struct hostlist *hp2; 16901558Srgrimes 16911558Srgrimes while (hp) { 16921558Srgrimes hp2 = hp; 16931558Srgrimes hp = hp->ht_next; 16941558Srgrimes free((caddr_t)hp2); 16951558Srgrimes } 16961558Srgrimes} 16971558Srgrimes 16981558Srgrimesstruct hostlist * 16991558Srgrimesget_ht() 17001558Srgrimes{ 17011558Srgrimes struct hostlist *hp; 17021558Srgrimes 17031558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 17041558Srgrimes if (hp == (struct hostlist *)NULL) 17051558Srgrimes out_of_mem(); 17061558Srgrimes hp->ht_next = (struct hostlist *)NULL; 17079336Sdfr hp->ht_flag = 0; 17081558Srgrimes return (hp); 17091558Srgrimes} 17101558Srgrimes 17111558Srgrimes/* 17121558Srgrimes * Out of memory, fatal 17131558Srgrimes */ 17141558Srgrimesvoid 17151558Srgrimesout_of_mem() 17161558Srgrimes{ 17171558Srgrimes 171837663Scharnier syslog(LOG_ERR, "out of memory"); 17191558Srgrimes exit(2); 17201558Srgrimes} 17211558Srgrimes 17221558Srgrimes/* 17231558Srgrimes * Do the mount syscall with the update flag to push the export info into 17241558Srgrimes * the kernel. 17251558Srgrimes */ 17261558Srgrimesint 17271558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb) 17281558Srgrimes struct exportlist *ep; 17291558Srgrimes struct grouplist *grp; 17301558Srgrimes int exflags; 173172650Sgreen struct xucred *anoncrp; 17321558Srgrimes char *dirp; 17331558Srgrimes int dirplen; 17341558Srgrimes struct statfs *fsb; 17351558Srgrimes{ 173675841Siedowse struct statfs fsb1; 173774462Salfred struct addrinfo *ai; 173875801Siedowse struct export_args *eap; 173974462Salfred char *cp = NULL; 17401558Srgrimes int done; 17411558Srgrimes char savedc = '\0'; 17421558Srgrimes union { 17431558Srgrimes struct ufs_args ua; 17441558Srgrimes struct iso_args ia; 17459336Sdfr struct msdosfs_args da; 174654093Ssemenu struct ntfs_args na; 17471558Srgrimes } args; 17481558Srgrimes 174975801Siedowse bzero(&args, sizeof args); 175075801Siedowse /* XXX, we assume that all xx_args look like ufs_args. */ 17511558Srgrimes args.ua.fspec = 0; 175275801Siedowse eap = &args.ua.export; 175375801Siedowse 175475801Siedowse eap->ex_flags = exflags; 175575801Siedowse eap->ex_anon = *anoncrp; 175675801Siedowse eap->ex_indexfile = ep->ex_indexfile; 175775641Siedowse if (grp->gr_type == GT_HOST) 175874462Salfred ai = grp->gr_ptr.gt_addrinfo; 175975641Siedowse else 176075641Siedowse ai = NULL; 17611558Srgrimes done = FALSE; 17621558Srgrimes while (!done) { 17631558Srgrimes switch (grp->gr_type) { 17641558Srgrimes case GT_HOST: 176575641Siedowse if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 176674462Salfred goto skip; 176775801Siedowse eap->ex_addr = ai->ai_addr; 176875801Siedowse eap->ex_addrlen = ai->ai_addrlen; 176975801Siedowse eap->ex_masklen = 0; 17701558Srgrimes break; 17711558Srgrimes case GT_NET: 177275801Siedowse if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 && 177374462Salfred have_v6 == 0) 177474462Salfred goto skip; 177575801Siedowse eap->ex_addr = 177675801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net; 177775801Siedowse eap->ex_addrlen = args.ua.export.ex_addr->sa_len; 177875801Siedowse eap->ex_mask = 177975801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask; 178075801Siedowse eap->ex_masklen = args.ua.export.ex_mask->sa_len; 17811558Srgrimes break; 178275641Siedowse case GT_DEFAULT: 178375801Siedowse eap->ex_addr = NULL; 178475801Siedowse eap->ex_addrlen = 0; 178575801Siedowse eap->ex_mask = NULL; 178675801Siedowse eap->ex_masklen = 0; 178775641Siedowse break; 17887401Swpaul case GT_IGNORE: 17897401Swpaul return(0); 17907401Swpaul break; 17911558Srgrimes default: 179237663Scharnier syslog(LOG_ERR, "bad grouptype"); 17931558Srgrimes if (cp) 17941558Srgrimes *cp = savedc; 17951558Srgrimes return (1); 17961558Srgrimes }; 17971558Srgrimes 17981558Srgrimes /* 17991558Srgrimes * XXX: 18001558Srgrimes * Maybe I should just use the fsb->f_mntonname path instead 18011558Srgrimes * of looping back up the dirp to the mount point?? 18021558Srgrimes * Also, needs to know how to export all types of local 180323681Speter * exportable file systems and not just "ufs". 18041558Srgrimes */ 18059336Sdfr while (mount(fsb->f_fstypename, dirp, 180674462Salfred fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) { 18071558Srgrimes if (cp) 18081558Srgrimes *cp-- = savedc; 18091558Srgrimes else 18101558Srgrimes cp = dirp + dirplen - 1; 18111558Srgrimes if (errno == EPERM) { 181275635Siedowse if (debug) 181375635Siedowse warnx("can't change attributes for %s", 181475635Siedowse dirp); 18151558Srgrimes syslog(LOG_ERR, 181637663Scharnier "can't change attributes for %s", dirp); 18171558Srgrimes return (1); 18181558Srgrimes } 18191558Srgrimes if (opt_flags & OP_ALLDIRS) { 182037663Scharnier syslog(LOG_ERR, "could not remount %s: %m", 18214895Swollman dirp); 18221558Srgrimes return (1); 18231558Srgrimes } 18241558Srgrimes /* back up over the last component */ 18251558Srgrimes while (*cp == '/' && cp > dirp) 18261558Srgrimes cp--; 18271558Srgrimes while (*(cp - 1) != '/' && cp > dirp) 18281558Srgrimes cp--; 18291558Srgrimes if (cp == dirp) { 18301558Srgrimes if (debug) 183137663Scharnier warnx("mnt unsucc"); 183237663Scharnier syslog(LOG_ERR, "can't export %s", dirp); 18331558Srgrimes return (1); 18341558Srgrimes } 18351558Srgrimes savedc = *cp; 18361558Srgrimes *cp = '\0'; 183775841Siedowse /* Check that we're still on the same filesystem. */ 183875841Siedowse if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid, 183975841Siedowse &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) { 184075841Siedowse *cp = savedc; 184175841Siedowse syslog(LOG_ERR, "can't export %s", dirp); 184275841Siedowse return (1); 184375841Siedowse } 18441558Srgrimes } 184574462Salfredskip: 184675641Siedowse if (ai != NULL) 184774462Salfred ai = ai->ai_next; 184875641Siedowse if (ai == NULL) 18491558Srgrimes done = TRUE; 18501558Srgrimes } 18511558Srgrimes if (cp) 18521558Srgrimes *cp = savedc; 18531558Srgrimes return (0); 18541558Srgrimes} 18551558Srgrimes 18561558Srgrimes/* 18571558Srgrimes * Translate a net address. 185875801Siedowse * 185975801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address. 18601558Srgrimes */ 18611558Srgrimesint 18621558Srgrimesget_net(cp, net, maskflg) 18631558Srgrimes char *cp; 18641558Srgrimes struct netmsk *net; 18651558Srgrimes int maskflg; 18661558Srgrimes{ 186775861Siedowse struct netent *np = NULL; 186874462Salfred char *name, *p, *prefp; 186975801Siedowse struct sockaddr_in sin; 187075861Siedowse struct sockaddr *sa = NULL; 187174462Salfred struct addrinfo hints, *ai = NULL; 187274462Salfred char netname[NI_MAXHOST]; 187374462Salfred long preflen; 18741558Srgrimes 187575635Siedowse p = prefp = NULL; 187674462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 187774462Salfred p = strchr(cp, '/'); 187874462Salfred *p = '\0'; 187974462Salfred prefp = p + 1; 188074462Salfred } 188174462Salfred 188275861Siedowse /* 188375861Siedowse * Check for a numeric address first. We wish to avoid 188475861Siedowse * possible DNS lookups in getnetbyname(). 188575861Siedowse */ 188675861Siedowse if (isxdigit(*cp) || *cp == ':') { 188774462Salfred memset(&hints, 0, sizeof hints); 188875801Siedowse /* Ensure the mask and the network have the same family. */ 188975801Siedowse if (maskflg && (opt_flags & OP_NET)) 189075801Siedowse hints.ai_family = net->nt_net.ss_family; 189175801Siedowse else if (!maskflg && (opt_flags & OP_HAVEMASK)) 189275801Siedowse hints.ai_family = net->nt_mask.ss_family; 189375801Siedowse else 189475801Siedowse hints.ai_family = AF_UNSPEC; 189574462Salfred hints.ai_flags = AI_NUMERICHOST; 189675861Siedowse if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 189775861Siedowse sa = ai->ai_addr; 189875861Siedowse if (sa != NULL && ai->ai_family == AF_INET) { 189974462Salfred /* 190075801Siedowse * The address in `cp' is really a network address, so 190175801Siedowse * use inet_network() to re-interpret this correctly. 190275801Siedowse * e.g. "127.1" means 127.1.0.0, not 127.0.0.1. 190374462Salfred */ 190475801Siedowse bzero(&sin, sizeof sin); 190574462Salfred sin.sin_family = AF_INET; 190674462Salfred sin.sin_len = sizeof sin; 190775801Siedowse sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 190874462Salfred if (debug) 190975801Siedowse fprintf(stderr, "get_net: v4 addr %s\n", 191075801Siedowse inet_ntoa(sin.sin_addr)); 191174462Salfred sa = (struct sockaddr *)&sin; 191275861Siedowse } 191375861Siedowse } 191475861Siedowse if (sa == NULL && (np = getnetbyname(cp)) != NULL) { 191575861Siedowse bzero(&sin, sizeof sin); 191675861Siedowse sin.sin_family = AF_INET; 191775861Siedowse sin.sin_len = sizeof sin; 191875861Siedowse sin.sin_addr = inet_makeaddr(np->n_net, 0); 191975861Siedowse sa = (struct sockaddr *)&sin; 192075861Siedowse } 192175861Siedowse if (sa == NULL) 192274462Salfred goto fail; 192325318Spst 192475801Siedowse if (maskflg) { 192575801Siedowse /* The specified sockaddr is a mask. */ 192675801Siedowse if (checkmask(sa) != 0) 192775801Siedowse goto fail; 192875801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 192975801Siedowse opt_flags |= OP_HAVEMASK; 193075801Siedowse } else { 193175801Siedowse /* The specified sockaddr is a network address. */ 193275801Siedowse bcopy(sa, &net->nt_net, sa->sa_len); 193374462Salfred 193475801Siedowse /* Get a network name for the export list. */ 193575801Siedowse if (np) { 193675801Siedowse name = np->n_name; 193775801Siedowse } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 193875801Siedowse NULL, 0, ninumeric) == 0) { 193975801Siedowse name = netname; 194075801Siedowse } else { 194175801Siedowse goto fail; 194275801Siedowse } 194375801Siedowse if ((net->nt_name = strdup(name)) == NULL) 194475801Siedowse out_of_mem(); 194575801Siedowse 194675801Siedowse /* 194775801Siedowse * Extract a mask from either a "/<masklen>" suffix, or 194875801Siedowse * from the class of an IPv4 address. 194975801Siedowse */ 195074462Salfred if (opt_flags & OP_MASKLEN) { 195174462Salfred preflen = strtol(prefp, NULL, 10); 195275801Siedowse if (preflen < 0L || preflen == LONG_MAX) 195374462Salfred goto fail; 195475801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 195575801Siedowse if (makemask(&net->nt_mask, (int)preflen) != 0) 195675801Siedowse goto fail; 195775801Siedowse opt_flags |= OP_HAVEMASK; 195874462Salfred *p = '/'; 195975801Siedowse } else if (sa->sa_family == AF_INET && 196075801Siedowse (opt_flags & OP_MASK) == 0) { 196175801Siedowse in_addr_t addr; 196274462Salfred 196375801Siedowse addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 196475801Siedowse if (IN_CLASSA(addr)) 196575801Siedowse preflen = 8; 196675801Siedowse else if (IN_CLASSB(addr)) 196775801Siedowse preflen = 16; 196875801Siedowse else if (IN_CLASSC(addr)) 196975801Siedowse preflen = 24; 197075801Siedowse else if (IN_CLASSD(addr)) 197175801Siedowse preflen = 28; 197275801Siedowse else 197375801Siedowse preflen = 32; /* XXX */ 197475801Siedowse 197575801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 197675801Siedowse makemask(&net->nt_mask, (int)preflen); 197775801Siedowse opt_flags |= OP_HAVEMASK; 197874462Salfred } 197974462Salfred } 198074462Salfred 198174462Salfred if (ai) 198274462Salfred freeaddrinfo(ai); 198374462Salfred return 0; 198474462Salfred 198574462Salfredfail: 198674462Salfred if (ai) 198774462Salfred freeaddrinfo(ai); 198874462Salfred return 1; 19891558Srgrimes} 19901558Srgrimes 19911558Srgrimes/* 19921558Srgrimes * Parse out the next white space separated field 19931558Srgrimes */ 19941558Srgrimesvoid 19951558Srgrimesnextfield(cp, endcp) 19961558Srgrimes char **cp; 19971558Srgrimes char **endcp; 19981558Srgrimes{ 19991558Srgrimes char *p; 20001558Srgrimes 20011558Srgrimes p = *cp; 20021558Srgrimes while (*p == ' ' || *p == '\t') 20031558Srgrimes p++; 20041558Srgrimes if (*p == '\n' || *p == '\0') 20051558Srgrimes *cp = *endcp = p; 20061558Srgrimes else { 20071558Srgrimes *cp = p++; 20081558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 20091558Srgrimes p++; 20101558Srgrimes *endcp = p; 20111558Srgrimes } 20121558Srgrimes} 20131558Srgrimes 20141558Srgrimes/* 20151558Srgrimes * Get an exports file line. Skip over blank lines and handle line 20161558Srgrimes * continuations. 20171558Srgrimes */ 20181558Srgrimesint 20191558Srgrimesget_line() 20201558Srgrimes{ 20211558Srgrimes char *p, *cp; 20221558Srgrimes int len; 20231558Srgrimes int totlen, cont_line; 20241558Srgrimes 20251558Srgrimes /* 20261558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 20271558Srgrimes */ 20281558Srgrimes p = line; 20291558Srgrimes totlen = 0; 20301558Srgrimes do { 20311558Srgrimes if (fgets(p, LINESIZ - totlen, exp_file) == NULL) 20321558Srgrimes return (0); 20331558Srgrimes len = strlen(p); 20341558Srgrimes cp = p + len - 1; 20351558Srgrimes cont_line = 0; 20361558Srgrimes while (cp >= p && 20371558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 20381558Srgrimes if (*cp == '\\') 20391558Srgrimes cont_line = 1; 20401558Srgrimes cp--; 20411558Srgrimes len--; 20421558Srgrimes } 20431558Srgrimes *++cp = '\0'; 20441558Srgrimes if (len > 0) { 20451558Srgrimes totlen += len; 20461558Srgrimes if (totlen >= LINESIZ) { 204737663Scharnier syslog(LOG_ERR, "exports line too long"); 20481558Srgrimes exit(2); 20491558Srgrimes } 20501558Srgrimes p = cp; 20511558Srgrimes } 20521558Srgrimes } while (totlen == 0 || cont_line); 20531558Srgrimes return (1); 20541558Srgrimes} 20551558Srgrimes 20561558Srgrimes/* 20571558Srgrimes * Parse a description of a credential. 20581558Srgrimes */ 20591558Srgrimesvoid 20601558Srgrimesparsecred(namelist, cr) 20611558Srgrimes char *namelist; 206272650Sgreen struct xucred *cr; 20631558Srgrimes{ 20641558Srgrimes char *name; 20651558Srgrimes int cnt; 20661558Srgrimes char *names; 20671558Srgrimes struct passwd *pw; 20681558Srgrimes struct group *gr; 20691558Srgrimes int ngroups, groups[NGROUPS + 1]; 20701558Srgrimes 20711558Srgrimes /* 207237663Scharnier * Set up the unprivileged user. 20731558Srgrimes */ 20741558Srgrimes cr->cr_uid = -2; 20751558Srgrimes cr->cr_groups[0] = -2; 20761558Srgrimes cr->cr_ngroups = 1; 20771558Srgrimes /* 20781558Srgrimes * Get the user's password table entry. 20791558Srgrimes */ 20801558Srgrimes names = strsep(&namelist, " \t\n"); 20811558Srgrimes name = strsep(&names, ":"); 20821558Srgrimes if (isdigit(*name) || *name == '-') 20831558Srgrimes pw = getpwuid(atoi(name)); 20841558Srgrimes else 20851558Srgrimes pw = getpwnam(name); 20861558Srgrimes /* 20871558Srgrimes * Credentials specified as those of a user. 20881558Srgrimes */ 20891558Srgrimes if (names == NULL) { 20901558Srgrimes if (pw == NULL) { 209137663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 20921558Srgrimes return; 20931558Srgrimes } 20941558Srgrimes cr->cr_uid = pw->pw_uid; 20951558Srgrimes ngroups = NGROUPS + 1; 20961558Srgrimes if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 209737663Scharnier syslog(LOG_ERR, "too many groups"); 20981558Srgrimes /* 20991558Srgrimes * Convert from int's to gid_t's and compress out duplicate 21001558Srgrimes */ 21011558Srgrimes cr->cr_ngroups = ngroups - 1; 21021558Srgrimes cr->cr_groups[0] = groups[0]; 21031558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 21041558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 21051558Srgrimes return; 21061558Srgrimes } 21071558Srgrimes /* 21081558Srgrimes * Explicit credential specified as a colon separated list: 21091558Srgrimes * uid:gid:gid:... 21101558Srgrimes */ 21111558Srgrimes if (pw != NULL) 21121558Srgrimes cr->cr_uid = pw->pw_uid; 21131558Srgrimes else if (isdigit(*name) || *name == '-') 21141558Srgrimes cr->cr_uid = atoi(name); 21151558Srgrimes else { 211637663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 21171558Srgrimes return; 21181558Srgrimes } 21191558Srgrimes cr->cr_ngroups = 0; 21201558Srgrimes while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) { 21211558Srgrimes name = strsep(&names, ":"); 21221558Srgrimes if (isdigit(*name) || *name == '-') { 21231558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 21241558Srgrimes } else { 21251558Srgrimes if ((gr = getgrnam(name)) == NULL) { 212637663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 21271558Srgrimes continue; 21281558Srgrimes } 21291558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 21301558Srgrimes } 21311558Srgrimes } 21321558Srgrimes if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS) 213337663Scharnier syslog(LOG_ERR, "too many groups"); 21341558Srgrimes} 21351558Srgrimes 21361558Srgrimes#define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50) 21371558Srgrimes/* 21381558Srgrimes * Routines that maintain the remote mounttab 21391558Srgrimes */ 21401558Srgrimesvoid 21411558Srgrimesget_mountlist() 21421558Srgrimes{ 21431558Srgrimes struct mountlist *mlp, **mlpp; 214423681Speter char *host, *dirp, *cp; 21451558Srgrimes char str[STRSIZ]; 21461558Srgrimes FILE *mlfile; 21471558Srgrimes 21481558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 214953117Sbillf if (errno == ENOENT) 215053117Sbillf return; 215153117Sbillf else { 215253117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 215353117Sbillf return; 215453117Sbillf } 21551558Srgrimes } 21561558Srgrimes mlpp = &mlhead; 21571558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 215823681Speter cp = str; 215923681Speter host = strsep(&cp, " \t\n"); 216023681Speter dirp = strsep(&cp, " \t\n"); 216123681Speter if (host == NULL || dirp == NULL) 21621558Srgrimes continue; 21631558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 216437663Scharnier if (mlp == (struct mountlist *)NULL) 216537663Scharnier out_of_mem(); 216623681Speter strncpy(mlp->ml_host, host, RPCMNT_NAMELEN); 216723681Speter mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 216823681Speter strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 216923681Speter mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 21701558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 21711558Srgrimes *mlpp = mlp; 21721558Srgrimes mlpp = &mlp->ml_next; 21731558Srgrimes } 21741558Srgrimes fclose(mlfile); 21751558Srgrimes} 21761558Srgrimes 217775635Siedowsevoid 217875635Siedowsedel_mlist(char *hostp, char *dirp) 21791558Srgrimes{ 21801558Srgrimes struct mountlist *mlp, **mlpp; 21811558Srgrimes struct mountlist *mlp2; 21821558Srgrimes FILE *mlfile; 21831558Srgrimes int fnd = 0; 21841558Srgrimes 21851558Srgrimes mlpp = &mlhead; 21861558Srgrimes mlp = mlhead; 21871558Srgrimes while (mlp) { 21881558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 21891558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 21901558Srgrimes fnd = 1; 21911558Srgrimes mlp2 = mlp; 21921558Srgrimes *mlpp = mlp = mlp->ml_next; 21931558Srgrimes free((caddr_t)mlp2); 21941558Srgrimes } else { 21951558Srgrimes mlpp = &mlp->ml_next; 21961558Srgrimes mlp = mlp->ml_next; 21971558Srgrimes } 21981558Srgrimes } 21991558Srgrimes if (fnd) { 22001558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 220137663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 22021558Srgrimes return; 22031558Srgrimes } 22041558Srgrimes mlp = mlhead; 22051558Srgrimes while (mlp) { 22061558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 22071558Srgrimes mlp = mlp->ml_next; 22081558Srgrimes } 22091558Srgrimes fclose(mlfile); 22101558Srgrimes } 22111558Srgrimes} 22121558Srgrimes 22131558Srgrimesvoid 22141558Srgrimesadd_mlist(hostp, dirp) 22151558Srgrimes char *hostp, *dirp; 22161558Srgrimes{ 22171558Srgrimes struct mountlist *mlp, **mlpp; 22181558Srgrimes FILE *mlfile; 22191558Srgrimes 22201558Srgrimes mlpp = &mlhead; 22211558Srgrimes mlp = mlhead; 22221558Srgrimes while (mlp) { 22231558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 22241558Srgrimes return; 22251558Srgrimes mlpp = &mlp->ml_next; 22261558Srgrimes mlp = mlp->ml_next; 22271558Srgrimes } 22281558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 222937663Scharnier if (mlp == (struct mountlist *)NULL) 223037663Scharnier out_of_mem(); 22311558Srgrimes strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN); 22321558Srgrimes mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 22331558Srgrimes strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 22341558Srgrimes mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 22351558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 22361558Srgrimes *mlpp = mlp; 22371558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 223837663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 22391558Srgrimes return; 22401558Srgrimes } 22411558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 22421558Srgrimes fclose(mlfile); 22431558Srgrimes} 22441558Srgrimes 22451558Srgrimes/* 22461558Srgrimes * Free up a group list. 22471558Srgrimes */ 22481558Srgrimesvoid 22491558Srgrimesfree_grp(grp) 22501558Srgrimes struct grouplist *grp; 22511558Srgrimes{ 22521558Srgrimes if (grp->gr_type == GT_HOST) { 225374462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 225474462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 22551558Srgrimes } else if (grp->gr_type == GT_NET) { 22561558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 22571558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 22581558Srgrimes } 22591558Srgrimes free((caddr_t)grp); 22601558Srgrimes} 22611558Srgrimes 22621558Srgrimes#ifdef DEBUG 22631558Srgrimesvoid 22641558SrgrimesSYSLOG(int pri, const char *fmt, ...) 22651558Srgrimes{ 22661558Srgrimes va_list ap; 22671558Srgrimes 22681558Srgrimes va_start(ap, fmt); 22691558Srgrimes vfprintf(stderr, fmt, ap); 22701558Srgrimes va_end(ap); 22711558Srgrimes} 22721558Srgrimes#endif /* DEBUG */ 22731558Srgrimes 22741558Srgrimes/* 22751558Srgrimes * Check options for consistency. 22761558Srgrimes */ 22771558Srgrimesint 22781558Srgrimescheck_options(dp) 22791558Srgrimes struct dirlist *dp; 22801558Srgrimes{ 22811558Srgrimes 22821558Srgrimes if (dp == (struct dirlist *)NULL) 22831558Srgrimes return (1); 22841558Srgrimes if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) || 22851558Srgrimes (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) || 22861558Srgrimes (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) { 22871558Srgrimes syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive"); 22881558Srgrimes return (1); 22891558Srgrimes } 22901558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 229175801Siedowse syslog(LOG_ERR, "-mask requires -network"); 229275801Siedowse return (1); 22931558Srgrimes } 229475801Siedowse if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) { 229575801Siedowse syslog(LOG_ERR, "-network requires mask specification"); 229675801Siedowse return (1); 229775801Siedowse } 229875801Siedowse if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) { 229975801Siedowse syslog(LOG_ERR, "-mask and /masklen are mutually exclusive"); 230075801Siedowse return (1); 230175801Siedowse } 23021558Srgrimes if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 230345927Salex syslog(LOG_ERR, "-alldirs has multiple directories"); 23041558Srgrimes return (1); 23051558Srgrimes } 23061558Srgrimes return (0); 23071558Srgrimes} 23081558Srgrimes 23091558Srgrimes/* 23101558Srgrimes * Check an absolute directory path for any symbolic links. Return true 23111558Srgrimes */ 23121558Srgrimesint 23131558Srgrimescheck_dirpath(dirp) 23141558Srgrimes char *dirp; 23151558Srgrimes{ 23161558Srgrimes char *cp; 23171558Srgrimes int ret = 1; 23181558Srgrimes struct stat sb; 23191558Srgrimes 23201558Srgrimes cp = dirp + 1; 23211558Srgrimes while (*cp && ret) { 23221558Srgrimes if (*cp == '/') { 23231558Srgrimes *cp = '\0'; 23249336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23251558Srgrimes ret = 0; 23261558Srgrimes *cp = '/'; 23271558Srgrimes } 23281558Srgrimes cp++; 23291558Srgrimes } 23309336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23311558Srgrimes ret = 0; 23321558Srgrimes return (ret); 23331558Srgrimes} 23349336Sdfr 233575801Siedowse/* 233675801Siedowse * Make a netmask according to the specified prefix length. The ss_family 233775801Siedowse * and other non-address fields must be initialised before calling this. 233875801Siedowse */ 233975801Siedowseint 234075801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen) 234174462Salfred{ 234275801Siedowse u_char *p; 234375801Siedowse int bits, i, len; 234474462Salfred 234575801Siedowse if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL) 234675801Siedowse return (-1); 234775801Siedowse if (bitlen > len * NBBY) 234875801Siedowse return (-1); 234974462Salfred 235075801Siedowse for (i = 0; i < len; i++) { 235175801Siedowse bits = (bitlen > NBBY) ? NBBY : bitlen; 235275801Siedowse *p++ = (1 << bits) - 1; 235375801Siedowse bitlen -= bits; 235474462Salfred } 235575801Siedowse return 0; 235674462Salfred} 235774462Salfred 235875801Siedowse/* 235975801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask 236075801Siedowse * is acceptable (i.e. of the form 1...10....0). 236175801Siedowse */ 236275801Siedowseint 236375801Siedowsecheckmask(struct sockaddr *sa) 236474462Salfred{ 236575801Siedowse u_char *mask; 236675801Siedowse int i, len; 236774462Salfred 236875801Siedowse if ((mask = sa_rawaddr(sa, &len)) == NULL) 236975801Siedowse return (-1); 237075801Siedowse 237175801Siedowse for (i = 0; i < len; i++) 237275801Siedowse if (mask[i] != 0xff) 237375801Siedowse break; 237475801Siedowse if (i < len) { 237575801Siedowse if (~mask[i] & (u_char)(~mask[i] + 1)) 237675801Siedowse return (-1); 237775801Siedowse i++; 237874462Salfred } 237975801Siedowse for (; i < len; i++) 238075801Siedowse if (mask[i] != 0) 238175801Siedowse return (-1); 238275801Siedowse return (0); 238374462Salfred} 238474462Salfred 238575801Siedowse/* 238675801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if 238775801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'. 238875801Siedowse * If samask is NULL, perform a full comparision. 238975801Siedowse */ 239075801Siedowseint 239175801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask) 239274462Salfred{ 239375801Siedowse unsigned char *p1, *p2, *mask; 239475801Siedowse int len, i; 239574462Salfred 239675801Siedowse if (sa1->sa_family != sa2->sa_family || 239775801Siedowse (p1 = sa_rawaddr(sa1, &len)) == NULL || 239875801Siedowse (p2 = sa_rawaddr(sa2, NULL)) == NULL) 239975801Siedowse return (1); 240075801Siedowse 240175801Siedowse switch (sa1->sa_family) { 240274462Salfred case AF_INET6: 240375801Siedowse if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 240475801Siedowse ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 240575801Siedowse return (1); 240674462Salfred break; 240774462Salfred } 240874462Salfred 240975801Siedowse /* Simple binary comparison if no mask specified. */ 241075801Siedowse if (samask == NULL) 241175801Siedowse return (memcmp(p1, p2, len)); 241274462Salfred 241375801Siedowse /* Set up the mask, and do a mask-based comparison. */ 241475801Siedowse if (sa1->sa_family != samask->sa_family || 241575801Siedowse (mask = sa_rawaddr(samask, NULL)) == NULL) 241675801Siedowse return (1); 241774462Salfred 241875801Siedowse for (i = 0; i < len; i++) 241975801Siedowse if ((p1[i] & mask[i]) != (p2[i] & mask[i])) 242075801Siedowse return (1); 242175801Siedowse return (0); 242274462Salfred} 242374462Salfred 242475801Siedowse/* 242575801Siedowse * Return a pointer to the part of the sockaddr that contains the 242675801Siedowse * raw address, and set *nbytes to its length in bytes. Returns 242775801Siedowse * NULL if the address family is unknown. 242875801Siedowse */ 242975801Siedowsevoid * 243075801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) { 243175801Siedowse void *p; 243274462Salfred int len; 243374462Salfred 243475801Siedowse switch (sa->sa_family) { 243574462Salfred case AF_INET: 243675801Siedowse len = sizeof(((struct sockaddr_in *)sa)->sin_addr); 243775801Siedowse p = &((struct sockaddr_in *)sa)->sin_addr; 243874462Salfred break; 243974462Salfred case AF_INET6: 244075801Siedowse len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); 244175801Siedowse p = &((struct sockaddr_in6 *)sa)->sin6_addr; 244274462Salfred break; 244374462Salfred default: 244475801Siedowse p = NULL; 244575801Siedowse len = 0; 244674462Salfred } 244774462Salfred 244875801Siedowse if (nbytes != NULL) 244975801Siedowse *nbytes = len; 245075801Siedowse return (p); 245174462Salfred} 245274462Salfred 245375754Siedowsevoid 245475754Siedowsehuphandler(int sig) 245575754Siedowse{ 245675754Siedowse got_sighup = 1; 245775754Siedowse} 245875754Siedowse 245974462Salfredvoid terminate(sig) 246074462Salfredint sig; 246174462Salfred{ 246274462Salfred close(mountdlockfd); 246374462Salfred unlink(MOUNTDLOCK); 246474792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 246574792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 246674462Salfred exit (0); 246774462Salfred} 2468