mountd.c revision 91354
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 91354 2002-02-27 04:45:37Z dd $"; 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> 6483653Speter#include <nfsserver/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 1121558Srgrimes 1131558Srgrimesstruct exportlist { 1141558Srgrimes struct exportlist *ex_next; 1151558Srgrimes struct dirlist *ex_dirl; 1161558Srgrimes struct dirlist *ex_defdir; 1171558Srgrimes int ex_flag; 1181558Srgrimes fsid_t ex_fs; 1191558Srgrimes char *ex_fsdir; 12027447Sdfr char *ex_indexfile; 1211558Srgrimes}; 1221558Srgrimes/* ex_flag bits */ 1231558Srgrimes#define EX_LINKED 0x1 1241558Srgrimes 1251558Srgrimesstruct netmsk { 12674462Salfred struct sockaddr_storage nt_net; 12775801Siedowse struct sockaddr_storage nt_mask; 12842144Sdfr char *nt_name; 1291558Srgrimes}; 1301558Srgrimes 1311558Srgrimesunion grouptypes { 13274462Salfred struct addrinfo *gt_addrinfo; 1331558Srgrimes struct netmsk gt_net; 1341558Srgrimes}; 1351558Srgrimes 1361558Srgrimesstruct grouplist { 1371558Srgrimes int gr_type; 1381558Srgrimes union grouptypes gr_ptr; 1391558Srgrimes struct grouplist *gr_next; 1401558Srgrimes}; 1411558Srgrimes/* Group types */ 1421558Srgrimes#define GT_NULL 0x0 1431558Srgrimes#define GT_HOST 0x1 1441558Srgrimes#define GT_NET 0x2 14575641Siedowse#define GT_DEFAULT 0x3 1467401Swpaul#define GT_IGNORE 0x5 1471558Srgrimes 1481558Srgrimesstruct hostlist { 1499336Sdfr int ht_flag; /* Uses DP_xx bits */ 1501558Srgrimes struct grouplist *ht_grp; 1511558Srgrimes struct hostlist *ht_next; 1521558Srgrimes}; 1531558Srgrimes 1549336Sdfrstruct fhreturn { 1559336Sdfr int fhr_flag; 1569336Sdfr int fhr_vers; 1579336Sdfr nfsfh_t fhr_fh; 1589336Sdfr}; 1599336Sdfr 1601558Srgrimes/* Global defs */ 1611558Srgrimeschar *add_expdir __P((struct dirlist **, char *, int)); 1621558Srgrimesvoid add_dlist __P((struct dirlist **, struct dirlist *, 1639336Sdfr struct grouplist *, int)); 1641558Srgrimesvoid add_mlist __P((char *, char *)); 1651558Srgrimesint check_dirpath __P((char *)); 1661558Srgrimesint check_options __P((struct dirlist *)); 16775801Siedowseint checkmask(struct sockaddr *sa); 16874462Salfredint chk_host __P((struct dirlist *, struct sockaddr *, int *, int *)); 16975635Siedowsevoid del_mlist(char *hostp, char *dirp); 1701558Srgrimesstruct dirlist *dirp_search __P((struct dirlist *, char *)); 1711558Srgrimesint do_mount __P((struct exportlist *, struct grouplist *, int, 17272650Sgreen struct xucred *, char *, int, struct statfs *)); 1731558Srgrimesint do_opt __P((char **, char **, struct exportlist *, struct grouplist *, 17472650Sgreen int *, int *, struct xucred *)); 1751558Srgrimesstruct exportlist *ex_search __P((fsid_t *)); 1761558Srgrimesstruct exportlist *get_exp __P((void)); 1771558Srgrimesvoid free_dir __P((struct dirlist *)); 1781558Srgrimesvoid free_exp __P((struct exportlist *)); 1791558Srgrimesvoid free_grp __P((struct grouplist *)); 1801558Srgrimesvoid free_host __P((struct hostlist *)); 1811558Srgrimesvoid get_exportlist __P((void)); 1827401Swpaulint get_host __P((char *, struct grouplist *, struct grouplist *)); 1831558Srgrimesstruct hostlist *get_ht __P((void)); 1841558Srgrimesint get_line __P((void)); 1851558Srgrimesvoid get_mountlist __P((void)); 1861558Srgrimesint get_net __P((char *, struct netmsk *, int)); 1871558Srgrimesvoid getexp_err __P((struct exportlist *, struct grouplist *)); 1881558Srgrimesstruct grouplist *get_grp __P((void)); 1891558Srgrimesvoid hang_dirp __P((struct dirlist *, struct grouplist *, 1901558Srgrimes struct exportlist *, int)); 19175754Siedowsevoid huphandler(int sig); 19275801Siedowseint makemask(struct sockaddr_storage *ssp, int bitlen); 1931558Srgrimesvoid mntsrv __P((struct svc_req *, SVCXPRT *)); 1941558Srgrimesvoid nextfield __P((char **, char **)); 1951558Srgrimesvoid out_of_mem __P((void)); 19672650Sgreenvoid parsecred __P((char *, struct xucred *)); 1971558Srgrimesint put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *)); 19875801Siedowsevoid *sa_rawaddr(struct sockaddr *sa, int *nbytes); 19975801Siedowseint sacmp(struct sockaddr *sa1, struct sockaddr *sa2, 20075801Siedowse struct sockaddr *samask); 20174462Salfredint scan_tree __P((struct dirlist *, struct sockaddr *)); 20237663Scharnierstatic void usage __P((void)); 2031558Srgrimesint xdr_dir __P((XDR *, char *)); 2041558Srgrimesint xdr_explist __P((XDR *, caddr_t)); 2059336Sdfrint xdr_fhs __P((XDR *, caddr_t)); 2061558Srgrimesint xdr_mlist __P((XDR *, caddr_t)); 20774462Salfredvoid terminate __P((int)); 2081558Srgrimes 2091558Srgrimesstruct exportlist *exphead; 2101558Srgrimesstruct mountlist *mlhead; 2111558Srgrimesstruct grouplist *grphead; 2121558Srgrimeschar exname[MAXPATHLEN]; 21372650Sgreenstruct xucred def_anon = { 21491354Sdd XUCRED_VERSION, 21572650Sgreen (uid_t)-2, 2161558Srgrimes 1, 21772650Sgreen { (gid_t)-2 }, 21872650Sgreen NULL 2191558Srgrimes}; 22025087Sdfrint force_v2 = 0; 2219336Sdfrint resvport_only = 1; 2229336Sdfrint dir_only = 1; 22331705Sguidoint log = 0; 22475754Siedowseint got_sighup = 0; 22574462Salfred 2261558Srgrimesint opt_flags; 22774462Salfredstatic int have_v6 = 1; 22874462Salfred#ifdef NI_WITHSCOPEID 22974462Salfredstatic const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID; 23074462Salfred#else 23174462Salfredstatic const int ninumeric = NI_NUMERICHOST; 23274462Salfred#endif 23374462Salfred 23474462Salfredint mountdlockfd; 23575801Siedowse/* Bits for opt_flags above */ 2361558Srgrimes#define OP_MAPROOT 0x01 2371558Srgrimes#define OP_MAPALL 0x02 23883653Speter/* 0x4 free */ 2391558Srgrimes#define OP_MASK 0x08 2401558Srgrimes#define OP_NET 0x10 2411558Srgrimes#define OP_ALLDIRS 0x40 24275801Siedowse#define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */ 24374462Salfred#define OP_MASKLEN 0x200 2441558Srgrimes 2451558Srgrimes#ifdef DEBUG 2461558Srgrimesint debug = 1; 24781911Skrisvoid SYSLOG __P((int, const char *, ...)) __printflike(2, 3); 2481558Srgrimes#define syslog SYSLOG 2491558Srgrimes#else 2501558Srgrimesint debug = 0; 2511558Srgrimes#endif 2521558Srgrimes 2531558Srgrimes/* 2541558Srgrimes * Mountd server for NFS mount protocol as described in: 2551558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A 2561558Srgrimes * The optional arguments are the exports file name 2571558Srgrimes * default: _PATH_EXPORTS 2581558Srgrimes * and "-n" to allow nonroot mount. 2591558Srgrimes */ 2601558Srgrimesint 2611558Srgrimesmain(argc, argv) 2621558Srgrimes int argc; 2631558Srgrimes char **argv; 2641558Srgrimes{ 26575754Siedowse fd_set readfds; 26674462Salfred SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp; 26774462Salfred struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf; 26874462Salfred int udpsock, tcpsock, udp6sock, tcp6sock; 26974462Salfred int xcreated = 0, s; 27074462Salfred int one = 1; 27183687Speter int c, error; 2721558Srgrimes 27375635Siedowse udp6conf = tcp6conf = NULL; 27475635Siedowse udp6sock = tcp6sock = NULL; 27575635Siedowse 27674462Salfred /* Check that another mountd isn't already running. */ 27774462Salfred if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) 27874462Salfred err(1, "%s", MOUNTDLOCK); 27974462Salfred 28074462Salfred if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) 28174462Salfred errx(1, "another rpc.mountd is already running. Aborting"); 28274462Salfred s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 28374462Salfred if (s < 0) 28474462Salfred have_v6 = 0; 28574462Salfred else 28674462Salfred close(s); 28783687Speter if (modfind("nfsserver") < 0) { 28883687Speter /* Not present in kernel, try loading it */ 28983687Speter if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0) 29083687Speter errx(1, "NFS server is not available or loadable"); 2912999Swollman } 2922999Swollman 29331665Sguido while ((c = getopt(argc, argv, "2dlnr")) != -1) 2941558Srgrimes switch (c) { 29525087Sdfr case '2': 29625087Sdfr force_v2 = 1; 29725087Sdfr break; 2989336Sdfr case 'n': 2999336Sdfr resvport_only = 0; 3009336Sdfr break; 3019336Sdfr case 'r': 3029336Sdfr dir_only = 0; 3039336Sdfr break; 3048688Sphk case 'd': 3058688Sphk debug = debug ? 0 : 1; 3068688Sphk break; 30731656Sguido case 'l': 30831656Sguido log = 1; 30931656Sguido break; 3101558Srgrimes default: 31137663Scharnier usage(); 3121558Srgrimes }; 3131558Srgrimes argc -= optind; 3141558Srgrimes argv += optind; 3151558Srgrimes grphead = (struct grouplist *)NULL; 3161558Srgrimes exphead = (struct exportlist *)NULL; 3171558Srgrimes mlhead = (struct mountlist *)NULL; 3181558Srgrimes if (argc == 1) { 3191558Srgrimes strncpy(exname, *argv, MAXPATHLEN-1); 3201558Srgrimes exname[MAXPATHLEN-1] = '\0'; 3211558Srgrimes } else 3221558Srgrimes strcpy(exname, _PATH_EXPORTS); 3231558Srgrimes openlog("mountd", LOG_PID, LOG_DAEMON); 3241558Srgrimes if (debug) 32537663Scharnier warnx("getting export list"); 3261558Srgrimes get_exportlist(); 3271558Srgrimes if (debug) 32837663Scharnier warnx("getting mount list"); 3291558Srgrimes get_mountlist(); 3301558Srgrimes if (debug) 33137663Scharnier warnx("here we go"); 3321558Srgrimes if (debug == 0) { 3331558Srgrimes daemon(0, 0); 3341558Srgrimes signal(SIGINT, SIG_IGN); 3351558Srgrimes signal(SIGQUIT, SIG_IGN); 3361558Srgrimes } 33775754Siedowse signal(SIGHUP, huphandler); 33874462Salfred signal(SIGTERM, terminate); 3391558Srgrimes { FILE *pidfile = fopen(_PATH_MOUNTDPID, "w"); 3401558Srgrimes if (pidfile != NULL) { 3411558Srgrimes fprintf(pidfile, "%d\n", getpid()); 3421558Srgrimes fclose(pidfile); 3431558Srgrimes } 3441558Srgrimes } 34574462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 34674462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 34774462Salfred udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 34874462Salfred tcpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 34974791Salfred udpconf = getnetconfigent("udp"); 35074791Salfred tcpconf = getnetconfigent("tcp"); 35174791Salfred if (!have_v6) 35274791Salfred goto skip_v6; 35374462Salfred udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 35474462Salfred tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 35574462Salfred /* 35674462Salfred * We're doing host-based access checks here, so don't allow 35774462Salfred * v4-in-v6 to confuse things. The kernel will disable it 35874462Salfred * by default on NFS sockets too. 35974462Salfred */ 36074462Salfred if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6, 36174462Salfred IPV6_BINDV6ONLY, &one, sizeof one) < 0){ 36274462Salfred syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket"); 36374462Salfred exit(1); 36474462Salfred } 36574462Salfred if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6, 36674462Salfred IPV6_BINDV6ONLY, &one, sizeof one) < 0){ 36774462Salfred syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket"); 36874462Salfred exit(1); 36974462Salfred } 37074462Salfred udp6conf = getnetconfigent("udp6"); 37174462Salfred tcp6conf = getnetconfigent("tcp6"); 37274791Salfred 37374791Salfredskip_v6: 37424759Sguido if (!resvport_only) { 37583687Speter if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL, 37683687Speter &resvport_only, sizeof(resvport_only)) != 0 && 37783687Speter errno != ENOENT) { 37824759Sguido syslog(LOG_ERR, "sysctl: %m"); 37924759Sguido exit(1); 38024759Sguido } 38124330Sguido } 3829202Srgrimes if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL || 3839202Srgrimes (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) { 38437663Scharnier syslog(LOG_ERR, "can't create socket"); 3851558Srgrimes exit(1); 3861558Srgrimes } 38774462Salfred if (udpsock != -1 && udpconf != NULL) { 38874462Salfred bindresvport(udpsock, NULL); 38974462Salfred udptransp = svc_dg_create(udpsock, 0, 0); 39074462Salfred if (udptransp != NULL) { 39174462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1, 39274462Salfred mntsrv, udpconf)) 39374462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service"); 39474462Salfred else 39574462Salfred xcreated++; 39674462Salfred if (!force_v2) { 39774462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3, 39874462Salfred mntsrv, udpconf)) 39974462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service"); 40074462Salfred else 40174462Salfred xcreated++; 40274462Salfred } 40374462Salfred } else 40474462Salfred syslog(LOG_WARNING, "can't create UDP services"); 40574462Salfred 40674462Salfred } 40774462Salfred if (tcpsock != -1 && tcpconf != NULL) { 40874462Salfred bindresvport(tcpsock, NULL); 40974462Salfred listen(tcpsock, SOMAXCONN); 41074462Salfred tcptransp = svc_vc_create(tcpsock, 0, 0); 41174462Salfred if (tcptransp != NULL) { 41274462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1, 41374462Salfred mntsrv, tcpconf)) 41474462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service"); 41574462Salfred else 41674462Salfred xcreated++; 41774462Salfred if (!force_v2) { 41874462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3, 41974462Salfred mntsrv, tcpconf)) 42074462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service"); 42174462Salfred else 42274462Salfred xcreated++; 42374462Salfred } 42474462Salfred } else 42574462Salfred syslog(LOG_WARNING, "can't create TCP service"); 42674462Salfred 42774462Salfred } 42874791Salfred if (have_v6 && udp6sock != -1 && udp6conf != NULL) { 42974462Salfred bindresvport(udp6sock, NULL); 43074462Salfred udp6transp = svc_dg_create(udp6sock, 0, 0); 43174462Salfred if (udp6transp != NULL) { 43274462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1, 43374462Salfred mntsrv, udp6conf)) 43474462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service"); 43574462Salfred else 43674462Salfred xcreated++; 43774462Salfred if (!force_v2) { 43874462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3, 43974462Salfred mntsrv, udp6conf)) 44074462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service"); 44174462Salfred else 44274462Salfred xcreated++; 44374462Salfred } 44474462Salfred } else 44574462Salfred syslog(LOG_WARNING, "can't create UDP6 service"); 44674462Salfred 44774462Salfred } 44874791Salfred if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) { 44974462Salfred bindresvport(tcp6sock, NULL); 45074462Salfred listen(tcp6sock, SOMAXCONN); 45174462Salfred tcp6transp = svc_vc_create(tcp6sock, 0, 0); 45274462Salfred if (tcp6transp != NULL) { 45374462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1, 45474462Salfred mntsrv, tcp6conf)) 45574462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service"); 45674462Salfred else 45774462Salfred xcreated++; 45874462Salfred if (!force_v2) { 45974462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3, 46074462Salfred mntsrv, tcp6conf)) 46174462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service"); 46274462Salfred else 46374462Salfred xcreated++; 46474462Salfred } 46574462Salfred } else 46674462Salfred syslog(LOG_WARNING, "can't create TCP6 service"); 46774462Salfred 46874462Salfred } 46974462Salfred if (xcreated == 0) { 47074462Salfred syslog(LOG_ERR, "could not create any services"); 4711558Srgrimes exit(1); 4721558Srgrimes } 47375754Siedowse 47475754Siedowse /* Expand svc_run() here so that we can call get_exportlist(). */ 47575754Siedowse for (;;) { 47675754Siedowse if (got_sighup) { 47775754Siedowse get_exportlist(); 47875754Siedowse got_sighup = 0; 47975754Siedowse } 48075754Siedowse readfds = svc_fdset; 48175754Siedowse switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) { 48275754Siedowse case -1: 48375754Siedowse if (errno == EINTR) 48475754Siedowse continue; 48575754Siedowse syslog(LOG_ERR, "mountd died: select: %m"); 48675754Siedowse exit(1); 48775754Siedowse case 0: 48875754Siedowse continue; 48975754Siedowse default: 49075754Siedowse svc_getreqset(&readfds); 49175754Siedowse } 49275754Siedowse } 4931558Srgrimes} 4941558Srgrimes 49537663Scharnierstatic void 49637663Scharnierusage() 49737663Scharnier{ 49837663Scharnier fprintf(stderr, 49937663Scharnier "usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n"); 50037663Scharnier exit(1); 50137663Scharnier} 50237663Scharnier 5031558Srgrimes/* 5041558Srgrimes * The mount rpc service 5051558Srgrimes */ 5061558Srgrimesvoid 5071558Srgrimesmntsrv(rqstp, transp) 5081558Srgrimes struct svc_req *rqstp; 5091558Srgrimes SVCXPRT *transp; 5101558Srgrimes{ 5111558Srgrimes struct exportlist *ep; 5121558Srgrimes struct dirlist *dp; 5139336Sdfr struct fhreturn fhr; 5141558Srgrimes struct stat stb; 5151558Srgrimes struct statfs fsb; 51674462Salfred struct addrinfo *ai; 51774462Salfred char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 51874462Salfred int lookup_failed = 1; 51974462Salfred struct sockaddr *saddr; 5209336Sdfr u_short sport; 52123681Speter char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN]; 52228911Sguido int bad = 0, defset, hostset; 5239336Sdfr sigset_t sighup_mask; 5241558Srgrimes 5259336Sdfr sigemptyset(&sighup_mask); 5269336Sdfr sigaddset(&sighup_mask, SIGHUP); 52774462Salfred saddr = svc_getrpccaller(transp)->buf; 52874462Salfred switch (saddr->sa_family) { 52974462Salfred case AF_INET6: 53075635Siedowse sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 53174462Salfred break; 53274462Salfred case AF_INET: 53375635Siedowse sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 53474462Salfred break; 53574462Salfred default: 53674462Salfred syslog(LOG_ERR, "request from unknown address family"); 53774462Salfred return; 53874462Salfred } 53974462Salfred lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, 54074462Salfred NULL, 0, 0); 54174462Salfred getnameinfo(saddr, saddr->sa_len, numerichost, 54274462Salfred sizeof numerichost, NULL, 0, NI_NUMERICHOST); 54374462Salfred ai = NULL; 5441558Srgrimes switch (rqstp->rq_proc) { 5451558Srgrimes case NULLPROC: 5461558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 54737663Scharnier syslog(LOG_ERR, "can't send reply"); 5481558Srgrimes return; 5491558Srgrimes case RPCMNT_MOUNT: 5509336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 55131656Sguido syslog(LOG_NOTICE, 55231656Sguido "mount request from %s from unprivileged port", 55374462Salfred numerichost); 5541558Srgrimes svcerr_weakauth(transp); 5551558Srgrimes return; 5561558Srgrimes } 5571558Srgrimes if (!svc_getargs(transp, xdr_dir, rpcpath)) { 55831656Sguido syslog(LOG_NOTICE, "undecodable mount request from %s", 55974462Salfred numerichost); 5601558Srgrimes svcerr_decode(transp); 5611558Srgrimes return; 5621558Srgrimes } 5631558Srgrimes 5641558Srgrimes /* 5651558Srgrimes * Get the real pathname and make sure it is a directory 5669336Sdfr * or a regular file if the -r option was specified 5679336Sdfr * and it exists. 5681558Srgrimes */ 56951968Salfred if (realpath(rpcpath, dirpath) == NULL || 5701558Srgrimes stat(dirpath, &stb) < 0 || 5719336Sdfr (!S_ISDIR(stb.st_mode) && 57274462Salfred (dir_only || !S_ISREG(stb.st_mode))) || 5731558Srgrimes statfs(dirpath, &fsb) < 0) { 5741558Srgrimes chdir("/"); /* Just in case realpath doesn't */ 57531656Sguido syslog(LOG_NOTICE, 57637663Scharnier "mount request from %s for non existent path %s", 57774462Salfred numerichost, dirpath); 5781558Srgrimes if (debug) 57937663Scharnier warnx("stat failed on %s", dirpath); 58028911Sguido bad = ENOENT; /* We will send error reply later */ 5811558Srgrimes } 5821558Srgrimes 5831558Srgrimes /* Check in the exports list */ 5849336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 5851558Srgrimes ep = ex_search(&fsb.f_fsid); 5869336Sdfr hostset = defset = 0; 5879336Sdfr if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) || 5881558Srgrimes ((dp = dirp_search(ep->ex_dirl, dirpath)) && 58974462Salfred chk_host(dp, saddr, &defset, &hostset)) || 59074462Salfred (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 59174462Salfred scan_tree(ep->ex_dirl, saddr) == 0))) { 59228911Sguido if (bad) { 59328911Sguido if (!svc_sendreply(transp, xdr_long, 59428911Sguido (caddr_t)&bad)) 59537663Scharnier syslog(LOG_ERR, "can't send reply"); 59628911Sguido sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 59728911Sguido return; 59828911Sguido } 5999336Sdfr if (hostset & DP_HOSTSET) 6009336Sdfr fhr.fhr_flag = hostset; 6019336Sdfr else 6029336Sdfr fhr.fhr_flag = defset; 6039336Sdfr fhr.fhr_vers = rqstp->rq_vers; 6041558Srgrimes /* Get the file handle */ 60523681Speter memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 6069336Sdfr if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 6071558Srgrimes bad = errno; 60837663Scharnier syslog(LOG_ERR, "can't get fh for %s", dirpath); 6091558Srgrimes if (!svc_sendreply(transp, xdr_long, 6101558Srgrimes (caddr_t)&bad)) 61137663Scharnier syslog(LOG_ERR, "can't send reply"); 6129336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6131558Srgrimes return; 6141558Srgrimes } 6159336Sdfr if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr)) 61637663Scharnier syslog(LOG_ERR, "can't send reply"); 61774462Salfred if (!lookup_failed) 61874462Salfred add_mlist(host, dirpath); 6191558Srgrimes else 62074462Salfred add_mlist(numerichost, dirpath); 6211558Srgrimes if (debug) 62237663Scharnier warnx("mount successful"); 62331656Sguido if (log) 62431656Sguido syslog(LOG_NOTICE, 62531656Sguido "mount request succeeded from %s for %s", 62674462Salfred numerichost, dirpath); 62731656Sguido } else { 6281558Srgrimes bad = EACCES; 62931656Sguido syslog(LOG_NOTICE, 63031656Sguido "mount request denied from %s for %s", 63174462Salfred numerichost, dirpath); 63231656Sguido } 63328911Sguido 63428911Sguido if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad)) 63537663Scharnier syslog(LOG_ERR, "can't send reply"); 6369336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6371558Srgrimes return; 6381558Srgrimes case RPCMNT_DUMP: 6391558Srgrimes if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL)) 64037663Scharnier syslog(LOG_ERR, "can't send reply"); 64131656Sguido else if (log) 64231656Sguido syslog(LOG_NOTICE, 64331656Sguido "dump request succeeded from %s", 64474462Salfred numerichost); 6451558Srgrimes return; 6461558Srgrimes case RPCMNT_UMOUNT: 6479336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 64831656Sguido syslog(LOG_NOTICE, 64931656Sguido "umount request from %s from unprivileged port", 65074462Salfred numerichost); 6511558Srgrimes svcerr_weakauth(transp); 6521558Srgrimes return; 6531558Srgrimes } 65451968Salfred if (!svc_getargs(transp, xdr_dir, rpcpath)) { 65531656Sguido syslog(LOG_NOTICE, "undecodable umount request from %s", 65674462Salfred numerichost); 6571558Srgrimes svcerr_decode(transp); 6581558Srgrimes return; 6591558Srgrimes } 66051968Salfred if (realpath(rpcpath, dirpath) == NULL) { 66151968Salfred syslog(LOG_NOTICE, "umount request from %s " 66251968Salfred "for non existent path %s", 66374462Salfred numerichost, dirpath); 66451968Salfred } 6651558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 66637663Scharnier syslog(LOG_ERR, "can't send reply"); 66774462Salfred if (!lookup_failed) 66875635Siedowse del_mlist(host, dirpath); 66975635Siedowse del_mlist(numerichost, dirpath); 67031656Sguido if (log) 67131656Sguido syslog(LOG_NOTICE, 67231656Sguido "umount request succeeded from %s for %s", 67374462Salfred numerichost, dirpath); 6741558Srgrimes return; 6751558Srgrimes case RPCMNT_UMNTALL: 6769336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 67731656Sguido syslog(LOG_NOTICE, 67831656Sguido "umountall request from %s from unprivileged port", 67974462Salfred numerichost); 6801558Srgrimes svcerr_weakauth(transp); 6811558Srgrimes return; 6821558Srgrimes } 6831558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 68437663Scharnier syslog(LOG_ERR, "can't send reply"); 68574462Salfred if (!lookup_failed) 68675635Siedowse del_mlist(host, NULL); 68775635Siedowse del_mlist(numerichost, NULL); 68831656Sguido if (log) 68931656Sguido syslog(LOG_NOTICE, 69031656Sguido "umountall request succeeded from %s", 69174462Salfred numerichost); 6921558Srgrimes return; 6931558Srgrimes case RPCMNT_EXPORT: 6941558Srgrimes if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL)) 69537663Scharnier syslog(LOG_ERR, "can't send reply"); 69631656Sguido if (log) 69731656Sguido syslog(LOG_NOTICE, 69831656Sguido "export request succeeded from %s", 69974462Salfred numerichost); 7001558Srgrimes return; 7011558Srgrimes default: 7021558Srgrimes svcerr_noproc(transp); 7031558Srgrimes return; 7041558Srgrimes } 7051558Srgrimes} 7061558Srgrimes 7071558Srgrimes/* 7081558Srgrimes * Xdr conversion for a dirpath string 7091558Srgrimes */ 7101558Srgrimesint 7111558Srgrimesxdr_dir(xdrsp, dirp) 7121558Srgrimes XDR *xdrsp; 7131558Srgrimes char *dirp; 7141558Srgrimes{ 7151558Srgrimes return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 7161558Srgrimes} 7171558Srgrimes 7181558Srgrimes/* 7199336Sdfr * Xdr routine to generate file handle reply 7201558Srgrimes */ 7211558Srgrimesint 7229336Sdfrxdr_fhs(xdrsp, cp) 7231558Srgrimes XDR *xdrsp; 7249336Sdfr caddr_t cp; 7251558Srgrimes{ 7269336Sdfr register struct fhreturn *fhrp = (struct fhreturn *)cp; 7279336Sdfr u_long ok = 0, len, auth; 7281558Srgrimes 7291558Srgrimes if (!xdr_long(xdrsp, &ok)) 7301558Srgrimes return (0); 7319336Sdfr switch (fhrp->fhr_vers) { 7329336Sdfr case 1: 7339336Sdfr return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 7349336Sdfr case 3: 7359336Sdfr len = NFSX_V3FH; 7369336Sdfr if (!xdr_long(xdrsp, &len)) 7379336Sdfr return (0); 7389336Sdfr if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 7399336Sdfr return (0); 74083653Speter auth = RPCAUTH_UNIX; 7419336Sdfr len = 1; 7429336Sdfr if (!xdr_long(xdrsp, &len)) 7439336Sdfr return (0); 7449336Sdfr return (xdr_long(xdrsp, &auth)); 7459336Sdfr }; 7469336Sdfr return (0); 7471558Srgrimes} 7481558Srgrimes 7491558Srgrimesint 7501558Srgrimesxdr_mlist(xdrsp, cp) 7511558Srgrimes XDR *xdrsp; 7521558Srgrimes caddr_t cp; 7531558Srgrimes{ 7541558Srgrimes struct mountlist *mlp; 7551558Srgrimes int true = 1; 7561558Srgrimes int false = 0; 7571558Srgrimes char *strp; 7581558Srgrimes 7591558Srgrimes mlp = mlhead; 7601558Srgrimes while (mlp) { 7611558Srgrimes if (!xdr_bool(xdrsp, &true)) 7621558Srgrimes return (0); 7631558Srgrimes strp = &mlp->ml_host[0]; 7641558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN)) 7651558Srgrimes return (0); 7661558Srgrimes strp = &mlp->ml_dirp[0]; 7671558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 7681558Srgrimes return (0); 7691558Srgrimes mlp = mlp->ml_next; 7701558Srgrimes } 7711558Srgrimes if (!xdr_bool(xdrsp, &false)) 7721558Srgrimes return (0); 7731558Srgrimes return (1); 7741558Srgrimes} 7751558Srgrimes 7761558Srgrimes/* 7771558Srgrimes * Xdr conversion for export list 7781558Srgrimes */ 7791558Srgrimesint 7801558Srgrimesxdr_explist(xdrsp, cp) 7811558Srgrimes XDR *xdrsp; 7821558Srgrimes caddr_t cp; 7831558Srgrimes{ 7841558Srgrimes struct exportlist *ep; 7851558Srgrimes int false = 0; 7869336Sdfr int putdef; 7879336Sdfr sigset_t sighup_mask; 7881558Srgrimes 7899336Sdfr sigemptyset(&sighup_mask); 7909336Sdfr sigaddset(&sighup_mask, SIGHUP); 7919336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 7921558Srgrimes ep = exphead; 7931558Srgrimes while (ep) { 7941558Srgrimes putdef = 0; 7951558Srgrimes if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef)) 7961558Srgrimes goto errout; 7971558Srgrimes if (ep->ex_defdir && putdef == 0 && 7981558Srgrimes put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 7991558Srgrimes &putdef)) 8001558Srgrimes goto errout; 8011558Srgrimes ep = ep->ex_next; 8021558Srgrimes } 8039336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8041558Srgrimes if (!xdr_bool(xdrsp, &false)) 8051558Srgrimes return (0); 8061558Srgrimes return (1); 8071558Srgrimeserrout: 8089336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8091558Srgrimes return (0); 8101558Srgrimes} 8111558Srgrimes 8121558Srgrimes/* 8131558Srgrimes * Called from xdr_explist() to traverse the tree and export the 8141558Srgrimes * directory paths. 8151558Srgrimes */ 8161558Srgrimesint 8171558Srgrimesput_exlist(dp, xdrsp, adp, putdefp) 8181558Srgrimes struct dirlist *dp; 8191558Srgrimes XDR *xdrsp; 8201558Srgrimes struct dirlist *adp; 8211558Srgrimes int *putdefp; 8221558Srgrimes{ 8231558Srgrimes struct grouplist *grp; 8241558Srgrimes struct hostlist *hp; 8251558Srgrimes int true = 1; 8261558Srgrimes int false = 0; 8271558Srgrimes int gotalldir = 0; 8281558Srgrimes char *strp; 8291558Srgrimes 8301558Srgrimes if (dp) { 8311558Srgrimes if (put_exlist(dp->dp_left, xdrsp, adp, putdefp)) 8321558Srgrimes return (1); 8331558Srgrimes if (!xdr_bool(xdrsp, &true)) 8341558Srgrimes return (1); 8351558Srgrimes strp = dp->dp_dirp; 8361558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 8371558Srgrimes return (1); 8381558Srgrimes if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 8391558Srgrimes gotalldir = 1; 8401558Srgrimes *putdefp = 1; 8411558Srgrimes } 8421558Srgrimes if ((dp->dp_flag & DP_DEFSET) == 0 && 8431558Srgrimes (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 8441558Srgrimes hp = dp->dp_hosts; 8451558Srgrimes while (hp) { 8461558Srgrimes grp = hp->ht_grp; 8471558Srgrimes if (grp->gr_type == GT_HOST) { 8481558Srgrimes if (!xdr_bool(xdrsp, &true)) 8491558Srgrimes return (1); 85074462Salfred strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 8518871Srgrimes if (!xdr_string(xdrsp, &strp, 8521558Srgrimes RPCMNT_NAMELEN)) 8531558Srgrimes return (1); 8541558Srgrimes } else if (grp->gr_type == GT_NET) { 8551558Srgrimes if (!xdr_bool(xdrsp, &true)) 8561558Srgrimes return (1); 8571558Srgrimes strp = grp->gr_ptr.gt_net.nt_name; 8588871Srgrimes if (!xdr_string(xdrsp, &strp, 8591558Srgrimes RPCMNT_NAMELEN)) 8601558Srgrimes return (1); 8611558Srgrimes } 8621558Srgrimes hp = hp->ht_next; 8631558Srgrimes if (gotalldir && hp == (struct hostlist *)NULL) { 8641558Srgrimes hp = adp->dp_hosts; 8651558Srgrimes gotalldir = 0; 8661558Srgrimes } 8671558Srgrimes } 8681558Srgrimes } 8691558Srgrimes if (!xdr_bool(xdrsp, &false)) 8701558Srgrimes return (1); 8711558Srgrimes if (put_exlist(dp->dp_right, xdrsp, adp, putdefp)) 8721558Srgrimes return (1); 8731558Srgrimes } 8741558Srgrimes return (0); 8751558Srgrimes} 8761558Srgrimes 8771558Srgrimes#define LINESIZ 10240 8781558Srgrimeschar line[LINESIZ]; 8791558SrgrimesFILE *exp_file; 8801558Srgrimes 8811558Srgrimes/* 8821558Srgrimes * Get the export list 8831558Srgrimes */ 8841558Srgrimesvoid 8851558Srgrimesget_exportlist() 8861558Srgrimes{ 8871558Srgrimes struct exportlist *ep, *ep2; 8881558Srgrimes struct grouplist *grp, *tgrp; 8891558Srgrimes struct exportlist **epp; 8901558Srgrimes struct dirlist *dirhead; 8911558Srgrimes struct statfs fsb, *fsp; 89272650Sgreen struct xucred anon; 8931558Srgrimes char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 8941558Srgrimes int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp; 8951558Srgrimes 89651968Salfred dirp = NULL; 89751968Salfred dirplen = 0; 89851968Salfred 8991558Srgrimes /* 9001558Srgrimes * First, get rid of the old list 9011558Srgrimes */ 9021558Srgrimes ep = exphead; 9031558Srgrimes while (ep) { 9041558Srgrimes ep2 = ep; 9051558Srgrimes ep = ep->ex_next; 9061558Srgrimes free_exp(ep2); 9071558Srgrimes } 9081558Srgrimes exphead = (struct exportlist *)NULL; 9091558Srgrimes 9101558Srgrimes grp = grphead; 9111558Srgrimes while (grp) { 9121558Srgrimes tgrp = grp; 9131558Srgrimes grp = grp->gr_next; 9141558Srgrimes free_grp(tgrp); 9151558Srgrimes } 9161558Srgrimes grphead = (struct grouplist *)NULL; 9171558Srgrimes 9181558Srgrimes /* 9191558Srgrimes * And delete exports that are in the kernel for all local 9201558Srgrimes * file systems. 9211558Srgrimes * XXX: Should know how to handle all local exportable file systems 92223681Speter * instead of just "ufs". 9231558Srgrimes */ 9241558Srgrimes num = getmntinfo(&fsp, MNT_NOWAIT); 9251558Srgrimes for (i = 0; i < num; i++) { 9261558Srgrimes union { 9271558Srgrimes struct ufs_args ua; 9281558Srgrimes struct iso_args ia; 9299336Sdfr struct msdosfs_args da; 93054093Ssemenu struct ntfs_args na; 9311558Srgrimes } targs; 9321558Srgrimes 93377435Sphk if (!strcmp(fsp->f_fstypename, "ufs") || 93477577Sru !strcmp(fsp->f_fstypename, "msdosfs") || 93554093Ssemenu !strcmp(fsp->f_fstypename, "ntfs") || 93623681Speter !strcmp(fsp->f_fstypename, "cd9660")) { 9379336Sdfr targs.ua.fspec = NULL; 9389336Sdfr targs.ua.export.ex_flags = MNT_DELEXPORT; 9399336Sdfr if (mount(fsp->f_fstypename, fsp->f_mntonname, 94077405Siedowse fsp->f_flags | MNT_UPDATE, (caddr_t)&targs) < 0 && 94177405Siedowse errno != ENOENT) 94277405Siedowse syslog(LOG_ERR, 94377405Siedowse "can't delete exports for %s: %m", 94474462Salfred fsp->f_mntonname); 9451558Srgrimes } 9461558Srgrimes fsp++; 9471558Srgrimes } 9481558Srgrimes 9491558Srgrimes /* 9501558Srgrimes * Read in the exports file and build the list, calling 9511558Srgrimes * mount() as we go along to push the export rules into the kernel. 9521558Srgrimes */ 9531558Srgrimes if ((exp_file = fopen(exname, "r")) == NULL) { 95437663Scharnier syslog(LOG_ERR, "can't open %s", exname); 9551558Srgrimes exit(2); 9561558Srgrimes } 9571558Srgrimes dirhead = (struct dirlist *)NULL; 9581558Srgrimes while (get_line()) { 9591558Srgrimes if (debug) 96037663Scharnier warnx("got line %s", line); 9611558Srgrimes cp = line; 9621558Srgrimes nextfield(&cp, &endcp); 9631558Srgrimes if (*cp == '#') 9641558Srgrimes goto nextline; 9651558Srgrimes 9661558Srgrimes /* 9671558Srgrimes * Set defaults. 9681558Srgrimes */ 9691558Srgrimes has_host = FALSE; 9701558Srgrimes anon = def_anon; 9711558Srgrimes exflags = MNT_EXPORTED; 9721558Srgrimes got_nondir = 0; 9731558Srgrimes opt_flags = 0; 9741558Srgrimes ep = (struct exportlist *)NULL; 9751558Srgrimes 9761558Srgrimes /* 9771558Srgrimes * Create new exports list entry 9781558Srgrimes */ 9791558Srgrimes len = endcp-cp; 9801558Srgrimes tgrp = grp = get_grp(); 9811558Srgrimes while (len > 0) { 9821558Srgrimes if (len > RPCMNT_NAMELEN) { 9831558Srgrimes getexp_err(ep, tgrp); 9841558Srgrimes goto nextline; 9851558Srgrimes } 9861558Srgrimes if (*cp == '-') { 9871558Srgrimes if (ep == (struct exportlist *)NULL) { 9881558Srgrimes getexp_err(ep, tgrp); 9891558Srgrimes goto nextline; 9901558Srgrimes } 9911558Srgrimes if (debug) 99237663Scharnier warnx("doing opt %s", cp); 9931558Srgrimes got_nondir = 1; 9941558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 9951558Srgrimes &exflags, &anon)) { 9961558Srgrimes getexp_err(ep, tgrp); 9971558Srgrimes goto nextline; 9981558Srgrimes } 9991558Srgrimes } else if (*cp == '/') { 10001558Srgrimes savedc = *endcp; 10011558Srgrimes *endcp = '\0'; 10021558Srgrimes if (check_dirpath(cp) && 10031558Srgrimes statfs(cp, &fsb) >= 0) { 10041558Srgrimes if (got_nondir) { 100537663Scharnier syslog(LOG_ERR, "dirs must be first"); 10061558Srgrimes getexp_err(ep, tgrp); 10071558Srgrimes goto nextline; 10081558Srgrimes } 10091558Srgrimes if (ep) { 10101558Srgrimes if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] || 10111558Srgrimes ep->ex_fs.val[1] != fsb.f_fsid.val[1]) { 10121558Srgrimes getexp_err(ep, tgrp); 10131558Srgrimes goto nextline; 10141558Srgrimes } 10151558Srgrimes } else { 10161558Srgrimes /* 10171558Srgrimes * See if this directory is already 10181558Srgrimes * in the list. 10191558Srgrimes */ 10201558Srgrimes ep = ex_search(&fsb.f_fsid); 10211558Srgrimes if (ep == (struct exportlist *)NULL) { 10221558Srgrimes ep = get_exp(); 10231558Srgrimes ep->ex_fs = fsb.f_fsid; 10241558Srgrimes ep->ex_fsdir = (char *) 10251558Srgrimes malloc(strlen(fsb.f_mntonname) + 1); 10261558Srgrimes if (ep->ex_fsdir) 10271558Srgrimes strcpy(ep->ex_fsdir, 10281558Srgrimes fsb.f_mntonname); 10291558Srgrimes else 10301558Srgrimes out_of_mem(); 10311558Srgrimes if (debug) 103274462Salfred warnx("making new ep fs=0x%x,0x%x", 103374462Salfred fsb.f_fsid.val[0], 103474462Salfred fsb.f_fsid.val[1]); 10351558Srgrimes } else if (debug) 103637663Scharnier warnx("found ep fs=0x%x,0x%x", 10371558Srgrimes fsb.f_fsid.val[0], 10381558Srgrimes fsb.f_fsid.val[1]); 10391558Srgrimes } 10401558Srgrimes 10411558Srgrimes /* 10421558Srgrimes * Add dirpath to export mount point. 10431558Srgrimes */ 10441558Srgrimes dirp = add_expdir(&dirhead, cp, len); 10451558Srgrimes dirplen = len; 10461558Srgrimes } else { 10471558Srgrimes getexp_err(ep, tgrp); 10481558Srgrimes goto nextline; 10491558Srgrimes } 10501558Srgrimes *endcp = savedc; 10511558Srgrimes } else { 10521558Srgrimes savedc = *endcp; 10531558Srgrimes *endcp = '\0'; 10541558Srgrimes got_nondir = 1; 10551558Srgrimes if (ep == (struct exportlist *)NULL) { 10561558Srgrimes getexp_err(ep, tgrp); 10571558Srgrimes goto nextline; 10581558Srgrimes } 10591558Srgrimes 10601558Srgrimes /* 10611558Srgrimes * Get the host or netgroup. 10621558Srgrimes */ 10631558Srgrimes setnetgrent(cp); 10641558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 10651558Srgrimes do { 10661558Srgrimes if (has_host) { 10671558Srgrimes grp->gr_next = get_grp(); 10681558Srgrimes grp = grp->gr_next; 10691558Srgrimes } 10701558Srgrimes if (netgrp) { 107137003Sjoerg if (hst == 0) { 107237663Scharnier syslog(LOG_ERR, 107337663Scharnier "null hostname in netgroup %s, skipping", cp); 107437004Sjoerg grp->gr_type = GT_IGNORE; 107537003Sjoerg } else if (get_host(hst, grp, tgrp)) { 107637663Scharnier syslog(LOG_ERR, 107737663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 107829317Sjlemon grp->gr_type = GT_IGNORE; 10791558Srgrimes } 10807401Swpaul } else if (get_host(cp, grp, tgrp)) { 108137663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 108229317Sjlemon grp->gr_type = GT_IGNORE; 10831558Srgrimes } 10841558Srgrimes has_host = TRUE; 10851558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 10861558Srgrimes endnetgrent(); 10871558Srgrimes *endcp = savedc; 10881558Srgrimes } 10891558Srgrimes cp = endcp; 10901558Srgrimes nextfield(&cp, &endcp); 10911558Srgrimes len = endcp - cp; 10921558Srgrimes } 10931558Srgrimes if (check_options(dirhead)) { 10941558Srgrimes getexp_err(ep, tgrp); 10951558Srgrimes goto nextline; 10961558Srgrimes } 10971558Srgrimes if (!has_host) { 109875641Siedowse grp->gr_type = GT_DEFAULT; 10991558Srgrimes if (debug) 110037663Scharnier warnx("adding a default entry"); 11011558Srgrimes 11021558Srgrimes /* 11031558Srgrimes * Don't allow a network export coincide with a list of 11041558Srgrimes * host(s) on the same line. 11051558Srgrimes */ 11061558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 110775801Siedowse syslog(LOG_ERR, "network/host conflict"); 11081558Srgrimes getexp_err(ep, tgrp); 11091558Srgrimes goto nextline; 111029317Sjlemon 111174462Salfred /* 111274462Salfred * If an export list was specified on this line, make sure 111329317Sjlemon * that we have at least one valid entry, otherwise skip it. 111429317Sjlemon */ 111529317Sjlemon } else { 111629317Sjlemon grp = tgrp; 111774462Salfred while (grp && grp->gr_type == GT_IGNORE) 111829317Sjlemon grp = grp->gr_next; 111929317Sjlemon if (! grp) { 112029317Sjlemon getexp_err(ep, tgrp); 112129317Sjlemon goto nextline; 112229317Sjlemon } 11231558Srgrimes } 11241558Srgrimes 11251558Srgrimes /* 11261558Srgrimes * Loop through hosts, pushing the exports into the kernel. 11271558Srgrimes * After loop, tgrp points to the start of the list and 11281558Srgrimes * grp points to the last entry in the list. 11291558Srgrimes */ 11301558Srgrimes grp = tgrp; 11311558Srgrimes do { 113275635Siedowse if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 113375635Siedowse &fsb)) { 113475635Siedowse getexp_err(ep, tgrp); 113575635Siedowse goto nextline; 113675635Siedowse } 11371558Srgrimes } while (grp->gr_next && (grp = grp->gr_next)); 11381558Srgrimes 11391558Srgrimes /* 11401558Srgrimes * Success. Update the data structures. 11411558Srgrimes */ 11421558Srgrimes if (has_host) { 11439336Sdfr hang_dirp(dirhead, tgrp, ep, opt_flags); 11441558Srgrimes grp->gr_next = grphead; 11451558Srgrimes grphead = tgrp; 11461558Srgrimes } else { 11471558Srgrimes hang_dirp(dirhead, (struct grouplist *)NULL, ep, 11489336Sdfr opt_flags); 11491558Srgrimes free_grp(grp); 11501558Srgrimes } 11511558Srgrimes dirhead = (struct dirlist *)NULL; 11521558Srgrimes if ((ep->ex_flag & EX_LINKED) == 0) { 11531558Srgrimes ep2 = exphead; 11541558Srgrimes epp = &exphead; 11551558Srgrimes 11561558Srgrimes /* 11571558Srgrimes * Insert in the list in alphabetical order. 11581558Srgrimes */ 11591558Srgrimes while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { 11601558Srgrimes epp = &ep2->ex_next; 11611558Srgrimes ep2 = ep2->ex_next; 11621558Srgrimes } 11631558Srgrimes if (ep2) 11641558Srgrimes ep->ex_next = ep2; 11651558Srgrimes *epp = ep; 11661558Srgrimes ep->ex_flag |= EX_LINKED; 11671558Srgrimes } 11681558Srgrimesnextline: 11691558Srgrimes if (dirhead) { 11701558Srgrimes free_dir(dirhead); 11711558Srgrimes dirhead = (struct dirlist *)NULL; 11721558Srgrimes } 11731558Srgrimes } 11741558Srgrimes fclose(exp_file); 11751558Srgrimes} 11761558Srgrimes 11771558Srgrimes/* 11781558Srgrimes * Allocate an export list element 11791558Srgrimes */ 11801558Srgrimesstruct exportlist * 11811558Srgrimesget_exp() 11821558Srgrimes{ 11831558Srgrimes struct exportlist *ep; 11841558Srgrimes 11851558Srgrimes ep = (struct exportlist *)malloc(sizeof (struct exportlist)); 11861558Srgrimes if (ep == (struct exportlist *)NULL) 11871558Srgrimes out_of_mem(); 118823681Speter memset(ep, 0, sizeof(struct exportlist)); 11891558Srgrimes return (ep); 11901558Srgrimes} 11911558Srgrimes 11921558Srgrimes/* 11931558Srgrimes * Allocate a group list element 11941558Srgrimes */ 11951558Srgrimesstruct grouplist * 11961558Srgrimesget_grp() 11971558Srgrimes{ 11981558Srgrimes struct grouplist *gp; 11991558Srgrimes 12001558Srgrimes gp = (struct grouplist *)malloc(sizeof (struct grouplist)); 12011558Srgrimes if (gp == (struct grouplist *)NULL) 12021558Srgrimes out_of_mem(); 120323681Speter memset(gp, 0, sizeof(struct grouplist)); 12041558Srgrimes return (gp); 12051558Srgrimes} 12061558Srgrimes 12071558Srgrimes/* 12081558Srgrimes * Clean up upon an error in get_exportlist(). 12091558Srgrimes */ 12101558Srgrimesvoid 12111558Srgrimesgetexp_err(ep, grp) 12121558Srgrimes struct exportlist *ep; 12131558Srgrimes struct grouplist *grp; 12141558Srgrimes{ 12151558Srgrimes struct grouplist *tgrp; 12161558Srgrimes 121737663Scharnier syslog(LOG_ERR, "bad exports list line %s", line); 12181558Srgrimes if (ep && (ep->ex_flag & EX_LINKED) == 0) 12191558Srgrimes free_exp(ep); 12201558Srgrimes while (grp) { 12211558Srgrimes tgrp = grp; 12221558Srgrimes grp = grp->gr_next; 12231558Srgrimes free_grp(tgrp); 12241558Srgrimes } 12251558Srgrimes} 12261558Srgrimes 12271558Srgrimes/* 12281558Srgrimes * Search the export list for a matching fs. 12291558Srgrimes */ 12301558Srgrimesstruct exportlist * 12311558Srgrimesex_search(fsid) 12321558Srgrimes fsid_t *fsid; 12331558Srgrimes{ 12341558Srgrimes struct exportlist *ep; 12351558Srgrimes 12361558Srgrimes ep = exphead; 12371558Srgrimes while (ep) { 12381558Srgrimes if (ep->ex_fs.val[0] == fsid->val[0] && 12391558Srgrimes ep->ex_fs.val[1] == fsid->val[1]) 12401558Srgrimes return (ep); 12411558Srgrimes ep = ep->ex_next; 12421558Srgrimes } 12431558Srgrimes return (ep); 12441558Srgrimes} 12451558Srgrimes 12461558Srgrimes/* 12471558Srgrimes * Add a directory path to the list. 12481558Srgrimes */ 12491558Srgrimeschar * 12501558Srgrimesadd_expdir(dpp, cp, len) 12511558Srgrimes struct dirlist **dpp; 12521558Srgrimes char *cp; 12531558Srgrimes int len; 12541558Srgrimes{ 12551558Srgrimes struct dirlist *dp; 12561558Srgrimes 12571558Srgrimes dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); 125837663Scharnier if (dp == (struct dirlist *)NULL) 125937663Scharnier out_of_mem(); 12601558Srgrimes dp->dp_left = *dpp; 12611558Srgrimes dp->dp_right = (struct dirlist *)NULL; 12621558Srgrimes dp->dp_flag = 0; 12631558Srgrimes dp->dp_hosts = (struct hostlist *)NULL; 12641558Srgrimes strcpy(dp->dp_dirp, cp); 12651558Srgrimes *dpp = dp; 12661558Srgrimes return (dp->dp_dirp); 12671558Srgrimes} 12681558Srgrimes 12691558Srgrimes/* 12701558Srgrimes * Hang the dir list element off the dirpath binary tree as required 12711558Srgrimes * and update the entry for host. 12721558Srgrimes */ 12731558Srgrimesvoid 12749336Sdfrhang_dirp(dp, grp, ep, flags) 12751558Srgrimes struct dirlist *dp; 12761558Srgrimes struct grouplist *grp; 12771558Srgrimes struct exportlist *ep; 12789336Sdfr int flags; 12791558Srgrimes{ 12801558Srgrimes struct hostlist *hp; 12811558Srgrimes struct dirlist *dp2; 12821558Srgrimes 12839336Sdfr if (flags & OP_ALLDIRS) { 12841558Srgrimes if (ep->ex_defdir) 12851558Srgrimes free((caddr_t)dp); 12861558Srgrimes else 12871558Srgrimes ep->ex_defdir = dp; 12889336Sdfr if (grp == (struct grouplist *)NULL) { 12891558Srgrimes ep->ex_defdir->dp_flag |= DP_DEFSET; 12909336Sdfr } else while (grp) { 12911558Srgrimes hp = get_ht(); 12921558Srgrimes hp->ht_grp = grp; 12931558Srgrimes hp->ht_next = ep->ex_defdir->dp_hosts; 12941558Srgrimes ep->ex_defdir->dp_hosts = hp; 12951558Srgrimes grp = grp->gr_next; 12961558Srgrimes } 12971558Srgrimes } else { 12981558Srgrimes 12991558Srgrimes /* 130037663Scharnier * Loop through the directories adding them to the tree. 13011558Srgrimes */ 13021558Srgrimes while (dp) { 13031558Srgrimes dp2 = dp->dp_left; 13049336Sdfr add_dlist(&ep->ex_dirl, dp, grp, flags); 13051558Srgrimes dp = dp2; 13061558Srgrimes } 13071558Srgrimes } 13081558Srgrimes} 13091558Srgrimes 13101558Srgrimes/* 13111558Srgrimes * Traverse the binary tree either updating a node that is already there 13121558Srgrimes * for the new directory or adding the new node. 13131558Srgrimes */ 13141558Srgrimesvoid 13159336Sdfradd_dlist(dpp, newdp, grp, flags) 13161558Srgrimes struct dirlist **dpp; 13171558Srgrimes struct dirlist *newdp; 13181558Srgrimes struct grouplist *grp; 13199336Sdfr int flags; 13201558Srgrimes{ 13211558Srgrimes struct dirlist *dp; 13221558Srgrimes struct hostlist *hp; 13231558Srgrimes int cmp; 13241558Srgrimes 13251558Srgrimes dp = *dpp; 13261558Srgrimes if (dp) { 13271558Srgrimes cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 13281558Srgrimes if (cmp > 0) { 13299336Sdfr add_dlist(&dp->dp_left, newdp, grp, flags); 13301558Srgrimes return; 13311558Srgrimes } else if (cmp < 0) { 13329336Sdfr add_dlist(&dp->dp_right, newdp, grp, flags); 13331558Srgrimes return; 13341558Srgrimes } else 13351558Srgrimes free((caddr_t)newdp); 13361558Srgrimes } else { 13371558Srgrimes dp = newdp; 13381558Srgrimes dp->dp_left = (struct dirlist *)NULL; 13391558Srgrimes *dpp = dp; 13401558Srgrimes } 13411558Srgrimes if (grp) { 13421558Srgrimes 13431558Srgrimes /* 13441558Srgrimes * Hang all of the host(s) off of the directory point. 13451558Srgrimes */ 13461558Srgrimes do { 13471558Srgrimes hp = get_ht(); 13481558Srgrimes hp->ht_grp = grp; 13491558Srgrimes hp->ht_next = dp->dp_hosts; 13501558Srgrimes dp->dp_hosts = hp; 13511558Srgrimes grp = grp->gr_next; 13521558Srgrimes } while (grp); 13539336Sdfr } else { 13541558Srgrimes dp->dp_flag |= DP_DEFSET; 13559336Sdfr } 13561558Srgrimes} 13571558Srgrimes 13581558Srgrimes/* 13591558Srgrimes * Search for a dirpath on the export point. 13601558Srgrimes */ 13611558Srgrimesstruct dirlist * 136274462Salfreddirp_search(dp, dirp) 13631558Srgrimes struct dirlist *dp; 136474462Salfred char *dirp; 13651558Srgrimes{ 13661558Srgrimes int cmp; 13671558Srgrimes 13681558Srgrimes if (dp) { 136974462Salfred cmp = strcmp(dp->dp_dirp, dirp); 13701558Srgrimes if (cmp > 0) 137174462Salfred return (dirp_search(dp->dp_left, dirp)); 13721558Srgrimes else if (cmp < 0) 137374462Salfred return (dirp_search(dp->dp_right, dirp)); 13741558Srgrimes else 13751558Srgrimes return (dp); 13761558Srgrimes } 13771558Srgrimes return (dp); 13781558Srgrimes} 13791558Srgrimes 13801558Srgrimes/* 13811558Srgrimes * Scan for a host match in a directory tree. 13821558Srgrimes */ 13831558Srgrimesint 13849336Sdfrchk_host(dp, saddr, defsetp, hostsetp) 13851558Srgrimes struct dirlist *dp; 138674462Salfred struct sockaddr *saddr; 13871558Srgrimes int *defsetp; 13889336Sdfr int *hostsetp; 13891558Srgrimes{ 13901558Srgrimes struct hostlist *hp; 13911558Srgrimes struct grouplist *grp; 139274462Salfred struct addrinfo *ai; 13931558Srgrimes 13941558Srgrimes if (dp) { 13951558Srgrimes if (dp->dp_flag & DP_DEFSET) 13969336Sdfr *defsetp = dp->dp_flag; 13971558Srgrimes hp = dp->dp_hosts; 13981558Srgrimes while (hp) { 13991558Srgrimes grp = hp->ht_grp; 14001558Srgrimes switch (grp->gr_type) { 14011558Srgrimes case GT_HOST: 140274462Salfred ai = grp->gr_ptr.gt_addrinfo; 140374462Salfred for (; ai; ai = ai->ai_next) { 140475801Siedowse if (!sacmp(ai->ai_addr, saddr, NULL)) { 140574462Salfred *hostsetp = 140674462Salfred (hp->ht_flag | DP_HOSTSET); 140774462Salfred return (1); 140874462Salfred } 14099336Sdfr } 141075801Siedowse break; 14111558Srgrimes case GT_NET: 141275801Siedowse if (!sacmp(saddr, (struct sockaddr *) 141375801Siedowse &grp->gr_ptr.gt_net.nt_net, 141475801Siedowse (struct sockaddr *) 141575801Siedowse &grp->gr_ptr.gt_net.nt_mask)) { 141674462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 141774462Salfred return (1); 141874462Salfred } 141975801Siedowse break; 142075801Siedowse } 14211558Srgrimes hp = hp->ht_next; 14221558Srgrimes } 14231558Srgrimes } 14241558Srgrimes return (0); 14251558Srgrimes} 14261558Srgrimes 14271558Srgrimes/* 14281558Srgrimes * Scan tree for a host that matches the address. 14291558Srgrimes */ 14301558Srgrimesint 14311558Srgrimesscan_tree(dp, saddr) 14321558Srgrimes struct dirlist *dp; 143374462Salfred struct sockaddr *saddr; 14341558Srgrimes{ 14359336Sdfr int defset, hostset; 14361558Srgrimes 14371558Srgrimes if (dp) { 14381558Srgrimes if (scan_tree(dp->dp_left, saddr)) 14391558Srgrimes return (1); 14409336Sdfr if (chk_host(dp, saddr, &defset, &hostset)) 14411558Srgrimes return (1); 14421558Srgrimes if (scan_tree(dp->dp_right, saddr)) 14431558Srgrimes return (1); 14441558Srgrimes } 14451558Srgrimes return (0); 14461558Srgrimes} 14471558Srgrimes 14481558Srgrimes/* 14491558Srgrimes * Traverse the dirlist tree and free it up. 14501558Srgrimes */ 14511558Srgrimesvoid 14521558Srgrimesfree_dir(dp) 14531558Srgrimes struct dirlist *dp; 14541558Srgrimes{ 14551558Srgrimes 14561558Srgrimes if (dp) { 14571558Srgrimes free_dir(dp->dp_left); 14581558Srgrimes free_dir(dp->dp_right); 14591558Srgrimes free_host(dp->dp_hosts); 14601558Srgrimes free((caddr_t)dp); 14611558Srgrimes } 14621558Srgrimes} 14631558Srgrimes 14641558Srgrimes/* 14651558Srgrimes * Parse the option string and update fields. 14661558Srgrimes * Option arguments may either be -<option>=<value> or 14671558Srgrimes * -<option> <value> 14681558Srgrimes */ 14691558Srgrimesint 14701558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr) 14711558Srgrimes char **cpp, **endcpp; 14721558Srgrimes struct exportlist *ep; 14731558Srgrimes struct grouplist *grp; 14741558Srgrimes int *has_hostp; 14751558Srgrimes int *exflagsp; 147672650Sgreen struct xucred *cr; 14771558Srgrimes{ 14781558Srgrimes char *cpoptarg, *cpoptend; 14791558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 14801558Srgrimes int allflag, usedarg; 14811558Srgrimes 148251968Salfred savedc2 = '\0'; 14831558Srgrimes cpopt = *cpp; 14841558Srgrimes cpopt++; 14851558Srgrimes cp = *endcpp; 14861558Srgrimes savedc = *cp; 14871558Srgrimes *cp = '\0'; 14881558Srgrimes while (cpopt && *cpopt) { 14891558Srgrimes allflag = 1; 14901558Srgrimes usedarg = -2; 149137663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 14921558Srgrimes *cpoptend++ = '\0'; 149337663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 14941558Srgrimes *cpoptarg++ = '\0'; 14951558Srgrimes } else { 149637663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 14971558Srgrimes *cpoptarg++ = '\0'; 14981558Srgrimes else { 14991558Srgrimes *cp = savedc; 15001558Srgrimes nextfield(&cp, &endcp); 15011558Srgrimes **endcpp = '\0'; 15021558Srgrimes if (endcp > cp && *cp != '-') { 15031558Srgrimes cpoptarg = cp; 15041558Srgrimes savedc2 = *endcp; 15051558Srgrimes *endcp = '\0'; 15061558Srgrimes usedarg = 0; 15071558Srgrimes } 15081558Srgrimes } 15091558Srgrimes } 15101558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 15111558Srgrimes *exflagsp |= MNT_EXRDONLY; 15121558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 15131558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 15141558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 15151558Srgrimes usedarg++; 15161558Srgrimes parsecred(cpoptarg, cr); 15171558Srgrimes if (allflag == 0) { 15181558Srgrimes *exflagsp |= MNT_EXPORTANON; 15191558Srgrimes opt_flags |= OP_MAPALL; 15201558Srgrimes } else 15211558Srgrimes opt_flags |= OP_MAPROOT; 15221558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 152375801Siedowse !strcmp(cpopt, "m"))) { 15241558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 152537663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 15261558Srgrimes return (1); 15271558Srgrimes } 15281558Srgrimes usedarg++; 15291558Srgrimes opt_flags |= OP_MASK; 15301558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 15311558Srgrimes !strcmp(cpopt, "n"))) { 153274462Salfred if (strchr(cpoptarg, '/') != NULL) { 153374462Salfred if (debug) 153474462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 153574462Salfred opt_flags |= OP_MASKLEN; 153674462Salfred } 15371558Srgrimes if (grp->gr_type != GT_NULL) { 153837663Scharnier syslog(LOG_ERR, "network/host conflict"); 15391558Srgrimes return (1); 15401558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 154137663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 15421558Srgrimes return (1); 15431558Srgrimes } 15441558Srgrimes grp->gr_type = GT_NET; 15451558Srgrimes *has_hostp = 1; 15461558Srgrimes usedarg++; 15471558Srgrimes opt_flags |= OP_NET; 15481558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 15491558Srgrimes opt_flags |= OP_ALLDIRS; 155027447Sdfr } else if (!strcmp(cpopt, "public")) { 155127447Sdfr *exflagsp |= MNT_EXPUBLIC; 155227447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 155327447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 155427447Sdfr opt_flags |= OP_MAPALL; 155527447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 155627447Sdfr ep->ex_indexfile = strdup(cpoptarg); 15571558Srgrimes } else { 155837663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 15591558Srgrimes return (1); 15601558Srgrimes } 15611558Srgrimes if (usedarg >= 0) { 15621558Srgrimes *endcp = savedc2; 15631558Srgrimes **endcpp = savedc; 15641558Srgrimes if (usedarg > 0) { 15651558Srgrimes *cpp = cp; 15661558Srgrimes *endcpp = endcp; 15671558Srgrimes } 15681558Srgrimes return (0); 15691558Srgrimes } 15701558Srgrimes cpopt = cpoptend; 15711558Srgrimes } 15721558Srgrimes **endcpp = savedc; 15731558Srgrimes return (0); 15741558Srgrimes} 15751558Srgrimes 15761558Srgrimes/* 15771558Srgrimes * Translate a character string to the corresponding list of network 15781558Srgrimes * addresses for a hostname. 15791558Srgrimes */ 15801558Srgrimesint 15817401Swpaulget_host(cp, grp, tgrp) 15821558Srgrimes char *cp; 15831558Srgrimes struct grouplist *grp; 15847401Swpaul struct grouplist *tgrp; 15851558Srgrimes{ 15867401Swpaul struct grouplist *checkgrp; 158775635Siedowse struct addrinfo *ai, *tai, hints; 158874462Salfred int ecode; 158974462Salfred char host[NI_MAXHOST]; 15901558Srgrimes 159174462Salfred if (grp->gr_type != GT_NULL) { 159274462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 15931558Srgrimes return (1); 15941558Srgrimes } 159574462Salfred memset(&hints, 0, sizeof hints); 159674462Salfred hints.ai_flags = AI_CANONNAME; 159774462Salfred hints.ai_protocol = IPPROTO_UDP; 159874462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 159974462Salfred if (ecode != 0) { 160075635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 160174462Salfred return 1; 160274462Salfred } 160374462Salfred grp->gr_ptr.gt_addrinfo = ai; 160474462Salfred while (ai != NULL) { 160574462Salfred if (ai->ai_canonname == NULL) { 160674462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 160774462Salfred sizeof host, NULL, 0, ninumeric) != 0) 160874462Salfred strlcpy(host, "?", sizeof(host)); 160974462Salfred ai->ai_canonname = strdup(host); 161074462Salfred ai->ai_flags |= AI_CANONNAME; 161175641Siedowse } 161274462Salfred if (debug) 161375635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 161475635Siedowse /* 161575635Siedowse * Sanity check: make sure we don't already have an entry 161675635Siedowse * for this host in the grouplist. 161775635Siedowse */ 161875635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 161975635Siedowse checkgrp = checkgrp->gr_next) { 162075635Siedowse if (checkgrp->gr_type != GT_HOST) 162175635Siedowse continue; 162275635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 162375635Siedowse tai = tai->ai_next) { 162475801Siedowse if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0) 162575635Siedowse continue; 162675635Siedowse if (debug) 162775635Siedowse fprintf(stderr, 162875635Siedowse "ignoring duplicate host %s\n", 162975635Siedowse ai->ai_canonname); 163075635Siedowse grp->gr_type = GT_IGNORE; 163175635Siedowse return (0); 163275635Siedowse } 163375635Siedowse } 163474462Salfred ai = ai->ai_next; 16351558Srgrimes } 163675635Siedowse grp->gr_type = GT_HOST; 16371558Srgrimes return (0); 16381558Srgrimes} 16391558Srgrimes 16401558Srgrimes/* 16411558Srgrimes * Free up an exports list component 16421558Srgrimes */ 16431558Srgrimesvoid 16441558Srgrimesfree_exp(ep) 16451558Srgrimes struct exportlist *ep; 16461558Srgrimes{ 16471558Srgrimes 16481558Srgrimes if (ep->ex_defdir) { 16491558Srgrimes free_host(ep->ex_defdir->dp_hosts); 16501558Srgrimes free((caddr_t)ep->ex_defdir); 16511558Srgrimes } 16521558Srgrimes if (ep->ex_fsdir) 16531558Srgrimes free(ep->ex_fsdir); 165427447Sdfr if (ep->ex_indexfile) 165527447Sdfr free(ep->ex_indexfile); 16561558Srgrimes free_dir(ep->ex_dirl); 16571558Srgrimes free((caddr_t)ep); 16581558Srgrimes} 16591558Srgrimes 16601558Srgrimes/* 16611558Srgrimes * Free hosts. 16621558Srgrimes */ 16631558Srgrimesvoid 16641558Srgrimesfree_host(hp) 16651558Srgrimes struct hostlist *hp; 16661558Srgrimes{ 16671558Srgrimes struct hostlist *hp2; 16681558Srgrimes 16691558Srgrimes while (hp) { 16701558Srgrimes hp2 = hp; 16711558Srgrimes hp = hp->ht_next; 16721558Srgrimes free((caddr_t)hp2); 16731558Srgrimes } 16741558Srgrimes} 16751558Srgrimes 16761558Srgrimesstruct hostlist * 16771558Srgrimesget_ht() 16781558Srgrimes{ 16791558Srgrimes struct hostlist *hp; 16801558Srgrimes 16811558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 16821558Srgrimes if (hp == (struct hostlist *)NULL) 16831558Srgrimes out_of_mem(); 16841558Srgrimes hp->ht_next = (struct hostlist *)NULL; 16859336Sdfr hp->ht_flag = 0; 16861558Srgrimes return (hp); 16871558Srgrimes} 16881558Srgrimes 16891558Srgrimes/* 16901558Srgrimes * Out of memory, fatal 16911558Srgrimes */ 16921558Srgrimesvoid 16931558Srgrimesout_of_mem() 16941558Srgrimes{ 16951558Srgrimes 169637663Scharnier syslog(LOG_ERR, "out of memory"); 16971558Srgrimes exit(2); 16981558Srgrimes} 16991558Srgrimes 17001558Srgrimes/* 17011558Srgrimes * Do the mount syscall with the update flag to push the export info into 17021558Srgrimes * the kernel. 17031558Srgrimes */ 17041558Srgrimesint 17051558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb) 17061558Srgrimes struct exportlist *ep; 17071558Srgrimes struct grouplist *grp; 17081558Srgrimes int exflags; 170972650Sgreen struct xucred *anoncrp; 17101558Srgrimes char *dirp; 17111558Srgrimes int dirplen; 17121558Srgrimes struct statfs *fsb; 17131558Srgrimes{ 171475841Siedowse struct statfs fsb1; 171574462Salfred struct addrinfo *ai; 171675801Siedowse struct export_args *eap; 171774462Salfred char *cp = NULL; 17181558Srgrimes int done; 17191558Srgrimes char savedc = '\0'; 17201558Srgrimes union { 17211558Srgrimes struct ufs_args ua; 17221558Srgrimes struct iso_args ia; 17239336Sdfr struct msdosfs_args da; 172454093Ssemenu struct ntfs_args na; 17251558Srgrimes } args; 17261558Srgrimes 172775801Siedowse bzero(&args, sizeof args); 172875801Siedowse /* XXX, we assume that all xx_args look like ufs_args. */ 17291558Srgrimes args.ua.fspec = 0; 173075801Siedowse eap = &args.ua.export; 173175801Siedowse 173275801Siedowse eap->ex_flags = exflags; 173375801Siedowse eap->ex_anon = *anoncrp; 173475801Siedowse eap->ex_indexfile = ep->ex_indexfile; 173575641Siedowse if (grp->gr_type == GT_HOST) 173674462Salfred ai = grp->gr_ptr.gt_addrinfo; 173775641Siedowse else 173875641Siedowse ai = NULL; 17391558Srgrimes done = FALSE; 17401558Srgrimes while (!done) { 17411558Srgrimes switch (grp->gr_type) { 17421558Srgrimes case GT_HOST: 174375641Siedowse if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 174474462Salfred goto skip; 174575801Siedowse eap->ex_addr = ai->ai_addr; 174675801Siedowse eap->ex_addrlen = ai->ai_addrlen; 174775801Siedowse eap->ex_masklen = 0; 17481558Srgrimes break; 17491558Srgrimes case GT_NET: 175075801Siedowse if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 && 175174462Salfred have_v6 == 0) 175274462Salfred goto skip; 175375801Siedowse eap->ex_addr = 175475801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net; 175575801Siedowse eap->ex_addrlen = args.ua.export.ex_addr->sa_len; 175675801Siedowse eap->ex_mask = 175775801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask; 175875801Siedowse eap->ex_masklen = args.ua.export.ex_mask->sa_len; 17591558Srgrimes break; 176075641Siedowse case GT_DEFAULT: 176175801Siedowse eap->ex_addr = NULL; 176275801Siedowse eap->ex_addrlen = 0; 176375801Siedowse eap->ex_mask = NULL; 176475801Siedowse eap->ex_masklen = 0; 176575641Siedowse break; 17667401Swpaul case GT_IGNORE: 17677401Swpaul return(0); 17687401Swpaul break; 17691558Srgrimes default: 177037663Scharnier syslog(LOG_ERR, "bad grouptype"); 17711558Srgrimes if (cp) 17721558Srgrimes *cp = savedc; 17731558Srgrimes return (1); 17741558Srgrimes }; 17751558Srgrimes 17761558Srgrimes /* 17771558Srgrimes * XXX: 17781558Srgrimes * Maybe I should just use the fsb->f_mntonname path instead 17791558Srgrimes * of looping back up the dirp to the mount point?? 17801558Srgrimes * Also, needs to know how to export all types of local 178123681Speter * exportable file systems and not just "ufs". 17821558Srgrimes */ 17839336Sdfr while (mount(fsb->f_fstypename, dirp, 178474462Salfred fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) { 17851558Srgrimes if (cp) 17861558Srgrimes *cp-- = savedc; 17871558Srgrimes else 17881558Srgrimes cp = dirp + dirplen - 1; 17891558Srgrimes if (errno == EPERM) { 179075635Siedowse if (debug) 179175635Siedowse warnx("can't change attributes for %s", 179275635Siedowse dirp); 17931558Srgrimes syslog(LOG_ERR, 179437663Scharnier "can't change attributes for %s", dirp); 17951558Srgrimes return (1); 17961558Srgrimes } 17971558Srgrimes if (opt_flags & OP_ALLDIRS) { 179837663Scharnier syslog(LOG_ERR, "could not remount %s: %m", 17994895Swollman dirp); 18001558Srgrimes return (1); 18011558Srgrimes } 18021558Srgrimes /* back up over the last component */ 18031558Srgrimes while (*cp == '/' && cp > dirp) 18041558Srgrimes cp--; 18051558Srgrimes while (*(cp - 1) != '/' && cp > dirp) 18061558Srgrimes cp--; 18071558Srgrimes if (cp == dirp) { 18081558Srgrimes if (debug) 180937663Scharnier warnx("mnt unsucc"); 181037663Scharnier syslog(LOG_ERR, "can't export %s", dirp); 18111558Srgrimes return (1); 18121558Srgrimes } 18131558Srgrimes savedc = *cp; 18141558Srgrimes *cp = '\0'; 181575841Siedowse /* Check that we're still on the same filesystem. */ 181675841Siedowse if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid, 181775841Siedowse &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) { 181875841Siedowse *cp = savedc; 181975841Siedowse syslog(LOG_ERR, "can't export %s", dirp); 182075841Siedowse return (1); 182175841Siedowse } 18221558Srgrimes } 182374462Salfredskip: 182475641Siedowse if (ai != NULL) 182574462Salfred ai = ai->ai_next; 182675641Siedowse if (ai == NULL) 18271558Srgrimes done = TRUE; 18281558Srgrimes } 18291558Srgrimes if (cp) 18301558Srgrimes *cp = savedc; 18311558Srgrimes return (0); 18321558Srgrimes} 18331558Srgrimes 18341558Srgrimes/* 18351558Srgrimes * Translate a net address. 183675801Siedowse * 183775801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address. 18381558Srgrimes */ 18391558Srgrimesint 18401558Srgrimesget_net(cp, net, maskflg) 18411558Srgrimes char *cp; 18421558Srgrimes struct netmsk *net; 18431558Srgrimes int maskflg; 18441558Srgrimes{ 184575861Siedowse struct netent *np = NULL; 184674462Salfred char *name, *p, *prefp; 184775801Siedowse struct sockaddr_in sin; 184875861Siedowse struct sockaddr *sa = NULL; 184974462Salfred struct addrinfo hints, *ai = NULL; 185074462Salfred char netname[NI_MAXHOST]; 185174462Salfred long preflen; 18521558Srgrimes 185375635Siedowse p = prefp = NULL; 185474462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 185574462Salfred p = strchr(cp, '/'); 185674462Salfred *p = '\0'; 185774462Salfred prefp = p + 1; 185874462Salfred } 185974462Salfred 186075861Siedowse /* 186175861Siedowse * Check for a numeric address first. We wish to avoid 186275861Siedowse * possible DNS lookups in getnetbyname(). 186375861Siedowse */ 186475861Siedowse if (isxdigit(*cp) || *cp == ':') { 186574462Salfred memset(&hints, 0, sizeof hints); 186675801Siedowse /* Ensure the mask and the network have the same family. */ 186775801Siedowse if (maskflg && (opt_flags & OP_NET)) 186875801Siedowse hints.ai_family = net->nt_net.ss_family; 186975801Siedowse else if (!maskflg && (opt_flags & OP_HAVEMASK)) 187075801Siedowse hints.ai_family = net->nt_mask.ss_family; 187175801Siedowse else 187275801Siedowse hints.ai_family = AF_UNSPEC; 187374462Salfred hints.ai_flags = AI_NUMERICHOST; 187475861Siedowse if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 187575861Siedowse sa = ai->ai_addr; 187675861Siedowse if (sa != NULL && ai->ai_family == AF_INET) { 187774462Salfred /* 187875801Siedowse * The address in `cp' is really a network address, so 187975801Siedowse * use inet_network() to re-interpret this correctly. 188075801Siedowse * e.g. "127.1" means 127.1.0.0, not 127.0.0.1. 188174462Salfred */ 188275801Siedowse bzero(&sin, sizeof sin); 188374462Salfred sin.sin_family = AF_INET; 188474462Salfred sin.sin_len = sizeof sin; 188575801Siedowse sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 188674462Salfred if (debug) 188775801Siedowse fprintf(stderr, "get_net: v4 addr %s\n", 188875801Siedowse inet_ntoa(sin.sin_addr)); 188974462Salfred sa = (struct sockaddr *)&sin; 189075861Siedowse } 189175861Siedowse } 189275861Siedowse if (sa == NULL && (np = getnetbyname(cp)) != NULL) { 189375861Siedowse bzero(&sin, sizeof sin); 189475861Siedowse sin.sin_family = AF_INET; 189575861Siedowse sin.sin_len = sizeof sin; 189675861Siedowse sin.sin_addr = inet_makeaddr(np->n_net, 0); 189775861Siedowse sa = (struct sockaddr *)&sin; 189875861Siedowse } 189975861Siedowse if (sa == NULL) 190074462Salfred goto fail; 190125318Spst 190275801Siedowse if (maskflg) { 190375801Siedowse /* The specified sockaddr is a mask. */ 190475801Siedowse if (checkmask(sa) != 0) 190575801Siedowse goto fail; 190675801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 190775801Siedowse opt_flags |= OP_HAVEMASK; 190875801Siedowse } else { 190975801Siedowse /* The specified sockaddr is a network address. */ 191075801Siedowse bcopy(sa, &net->nt_net, sa->sa_len); 191174462Salfred 191275801Siedowse /* Get a network name for the export list. */ 191375801Siedowse if (np) { 191475801Siedowse name = np->n_name; 191575801Siedowse } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 191675801Siedowse NULL, 0, ninumeric) == 0) { 191775801Siedowse name = netname; 191875801Siedowse } else { 191975801Siedowse goto fail; 192075801Siedowse } 192175801Siedowse if ((net->nt_name = strdup(name)) == NULL) 192275801Siedowse out_of_mem(); 192375801Siedowse 192475801Siedowse /* 192575801Siedowse * Extract a mask from either a "/<masklen>" suffix, or 192675801Siedowse * from the class of an IPv4 address. 192775801Siedowse */ 192874462Salfred if (opt_flags & OP_MASKLEN) { 192974462Salfred preflen = strtol(prefp, NULL, 10); 193075801Siedowse if (preflen < 0L || preflen == LONG_MAX) 193174462Salfred goto fail; 193275801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 193375801Siedowse if (makemask(&net->nt_mask, (int)preflen) != 0) 193475801Siedowse goto fail; 193575801Siedowse opt_flags |= OP_HAVEMASK; 193674462Salfred *p = '/'; 193775801Siedowse } else if (sa->sa_family == AF_INET && 193875801Siedowse (opt_flags & OP_MASK) == 0) { 193975801Siedowse in_addr_t addr; 194074462Salfred 194175801Siedowse addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 194275801Siedowse if (IN_CLASSA(addr)) 194375801Siedowse preflen = 8; 194475801Siedowse else if (IN_CLASSB(addr)) 194575801Siedowse preflen = 16; 194675801Siedowse else if (IN_CLASSC(addr)) 194775801Siedowse preflen = 24; 194875801Siedowse else if (IN_CLASSD(addr)) 194975801Siedowse preflen = 28; 195075801Siedowse else 195175801Siedowse preflen = 32; /* XXX */ 195275801Siedowse 195375801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 195475801Siedowse makemask(&net->nt_mask, (int)preflen); 195575801Siedowse opt_flags |= OP_HAVEMASK; 195674462Salfred } 195774462Salfred } 195874462Salfred 195974462Salfred if (ai) 196074462Salfred freeaddrinfo(ai); 196174462Salfred return 0; 196274462Salfred 196374462Salfredfail: 196474462Salfred if (ai) 196574462Salfred freeaddrinfo(ai); 196674462Salfred return 1; 19671558Srgrimes} 19681558Srgrimes 19691558Srgrimes/* 19701558Srgrimes * Parse out the next white space separated field 19711558Srgrimes */ 19721558Srgrimesvoid 19731558Srgrimesnextfield(cp, endcp) 19741558Srgrimes char **cp; 19751558Srgrimes char **endcp; 19761558Srgrimes{ 19771558Srgrimes char *p; 19781558Srgrimes 19791558Srgrimes p = *cp; 19801558Srgrimes while (*p == ' ' || *p == '\t') 19811558Srgrimes p++; 19821558Srgrimes if (*p == '\n' || *p == '\0') 19831558Srgrimes *cp = *endcp = p; 19841558Srgrimes else { 19851558Srgrimes *cp = p++; 19861558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 19871558Srgrimes p++; 19881558Srgrimes *endcp = p; 19891558Srgrimes } 19901558Srgrimes} 19911558Srgrimes 19921558Srgrimes/* 19931558Srgrimes * Get an exports file line. Skip over blank lines and handle line 19941558Srgrimes * continuations. 19951558Srgrimes */ 19961558Srgrimesint 19971558Srgrimesget_line() 19981558Srgrimes{ 19991558Srgrimes char *p, *cp; 20001558Srgrimes int len; 20011558Srgrimes int totlen, cont_line; 20021558Srgrimes 20031558Srgrimes /* 20041558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 20051558Srgrimes */ 20061558Srgrimes p = line; 20071558Srgrimes totlen = 0; 20081558Srgrimes do { 20091558Srgrimes if (fgets(p, LINESIZ - totlen, exp_file) == NULL) 20101558Srgrimes return (0); 20111558Srgrimes len = strlen(p); 20121558Srgrimes cp = p + len - 1; 20131558Srgrimes cont_line = 0; 20141558Srgrimes while (cp >= p && 20151558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 20161558Srgrimes if (*cp == '\\') 20171558Srgrimes cont_line = 1; 20181558Srgrimes cp--; 20191558Srgrimes len--; 20201558Srgrimes } 202179117Sdd if (cont_line) { 202279117Sdd *++cp = ' '; 202379117Sdd len++; 202479117Sdd } 20251558Srgrimes *++cp = '\0'; 20261558Srgrimes if (len > 0) { 20271558Srgrimes totlen += len; 20281558Srgrimes if (totlen >= LINESIZ) { 202937663Scharnier syslog(LOG_ERR, "exports line too long"); 20301558Srgrimes exit(2); 20311558Srgrimes } 20321558Srgrimes p = cp; 20331558Srgrimes } 20341558Srgrimes } while (totlen == 0 || cont_line); 20351558Srgrimes return (1); 20361558Srgrimes} 20371558Srgrimes 20381558Srgrimes/* 20391558Srgrimes * Parse a description of a credential. 20401558Srgrimes */ 20411558Srgrimesvoid 20421558Srgrimesparsecred(namelist, cr) 20431558Srgrimes char *namelist; 204472650Sgreen struct xucred *cr; 20451558Srgrimes{ 20461558Srgrimes char *name; 20471558Srgrimes int cnt; 20481558Srgrimes char *names; 20491558Srgrimes struct passwd *pw; 20501558Srgrimes struct group *gr; 20511558Srgrimes int ngroups, groups[NGROUPS + 1]; 20521558Srgrimes 205391354Sdd cr->cr_version = XUCRED_VERSION; 20541558Srgrimes /* 205537663Scharnier * Set up the unprivileged user. 20561558Srgrimes */ 20571558Srgrimes cr->cr_uid = -2; 20581558Srgrimes cr->cr_groups[0] = -2; 20591558Srgrimes cr->cr_ngroups = 1; 20601558Srgrimes /* 20611558Srgrimes * Get the user's password table entry. 20621558Srgrimes */ 20631558Srgrimes names = strsep(&namelist, " \t\n"); 20641558Srgrimes name = strsep(&names, ":"); 20651558Srgrimes if (isdigit(*name) || *name == '-') 20661558Srgrimes pw = getpwuid(atoi(name)); 20671558Srgrimes else 20681558Srgrimes pw = getpwnam(name); 20691558Srgrimes /* 20701558Srgrimes * Credentials specified as those of a user. 20711558Srgrimes */ 20721558Srgrimes if (names == NULL) { 20731558Srgrimes if (pw == NULL) { 207437663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 20751558Srgrimes return; 20761558Srgrimes } 20771558Srgrimes cr->cr_uid = pw->pw_uid; 20781558Srgrimes ngroups = NGROUPS + 1; 20791558Srgrimes if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 208037663Scharnier syslog(LOG_ERR, "too many groups"); 20811558Srgrimes /* 20821558Srgrimes * Convert from int's to gid_t's and compress out duplicate 20831558Srgrimes */ 20841558Srgrimes cr->cr_ngroups = ngroups - 1; 20851558Srgrimes cr->cr_groups[0] = groups[0]; 20861558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 20871558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 20881558Srgrimes return; 20891558Srgrimes } 20901558Srgrimes /* 20911558Srgrimes * Explicit credential specified as a colon separated list: 20921558Srgrimes * uid:gid:gid:... 20931558Srgrimes */ 20941558Srgrimes if (pw != NULL) 20951558Srgrimes cr->cr_uid = pw->pw_uid; 20961558Srgrimes else if (isdigit(*name) || *name == '-') 20971558Srgrimes cr->cr_uid = atoi(name); 20981558Srgrimes else { 209937663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 21001558Srgrimes return; 21011558Srgrimes } 21021558Srgrimes cr->cr_ngroups = 0; 21031558Srgrimes while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) { 21041558Srgrimes name = strsep(&names, ":"); 21051558Srgrimes if (isdigit(*name) || *name == '-') { 21061558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 21071558Srgrimes } else { 21081558Srgrimes if ((gr = getgrnam(name)) == NULL) { 210937663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 21101558Srgrimes continue; 21111558Srgrimes } 21121558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 21131558Srgrimes } 21141558Srgrimes } 21151558Srgrimes if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS) 211637663Scharnier syslog(LOG_ERR, "too many groups"); 21171558Srgrimes} 21181558Srgrimes 21191558Srgrimes#define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50) 21201558Srgrimes/* 21211558Srgrimes * Routines that maintain the remote mounttab 21221558Srgrimes */ 21231558Srgrimesvoid 21241558Srgrimesget_mountlist() 21251558Srgrimes{ 21261558Srgrimes struct mountlist *mlp, **mlpp; 212723681Speter char *host, *dirp, *cp; 21281558Srgrimes char str[STRSIZ]; 21291558Srgrimes FILE *mlfile; 21301558Srgrimes 21311558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 213253117Sbillf if (errno == ENOENT) 213353117Sbillf return; 213453117Sbillf else { 213553117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 213653117Sbillf return; 213753117Sbillf } 21381558Srgrimes } 21391558Srgrimes mlpp = &mlhead; 21401558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 214123681Speter cp = str; 214223681Speter host = strsep(&cp, " \t\n"); 214323681Speter dirp = strsep(&cp, " \t\n"); 214423681Speter if (host == NULL || dirp == NULL) 21451558Srgrimes continue; 21461558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 214737663Scharnier if (mlp == (struct mountlist *)NULL) 214837663Scharnier out_of_mem(); 214923681Speter strncpy(mlp->ml_host, host, RPCMNT_NAMELEN); 215023681Speter mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 215123681Speter strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 215223681Speter mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 21531558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 21541558Srgrimes *mlpp = mlp; 21551558Srgrimes mlpp = &mlp->ml_next; 21561558Srgrimes } 21571558Srgrimes fclose(mlfile); 21581558Srgrimes} 21591558Srgrimes 216075635Siedowsevoid 216175635Siedowsedel_mlist(char *hostp, char *dirp) 21621558Srgrimes{ 21631558Srgrimes struct mountlist *mlp, **mlpp; 21641558Srgrimes struct mountlist *mlp2; 21651558Srgrimes FILE *mlfile; 21661558Srgrimes int fnd = 0; 21671558Srgrimes 21681558Srgrimes mlpp = &mlhead; 21691558Srgrimes mlp = mlhead; 21701558Srgrimes while (mlp) { 21711558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 21721558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 21731558Srgrimes fnd = 1; 21741558Srgrimes mlp2 = mlp; 21751558Srgrimes *mlpp = mlp = mlp->ml_next; 21761558Srgrimes free((caddr_t)mlp2); 21771558Srgrimes } else { 21781558Srgrimes mlpp = &mlp->ml_next; 21791558Srgrimes mlp = mlp->ml_next; 21801558Srgrimes } 21811558Srgrimes } 21821558Srgrimes if (fnd) { 21831558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 218437663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 21851558Srgrimes return; 21861558Srgrimes } 21871558Srgrimes mlp = mlhead; 21881558Srgrimes while (mlp) { 21891558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 21901558Srgrimes mlp = mlp->ml_next; 21911558Srgrimes } 21921558Srgrimes fclose(mlfile); 21931558Srgrimes } 21941558Srgrimes} 21951558Srgrimes 21961558Srgrimesvoid 21971558Srgrimesadd_mlist(hostp, dirp) 21981558Srgrimes char *hostp, *dirp; 21991558Srgrimes{ 22001558Srgrimes struct mountlist *mlp, **mlpp; 22011558Srgrimes FILE *mlfile; 22021558Srgrimes 22031558Srgrimes mlpp = &mlhead; 22041558Srgrimes mlp = mlhead; 22051558Srgrimes while (mlp) { 22061558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 22071558Srgrimes return; 22081558Srgrimes mlpp = &mlp->ml_next; 22091558Srgrimes mlp = mlp->ml_next; 22101558Srgrimes } 22111558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 221237663Scharnier if (mlp == (struct mountlist *)NULL) 221337663Scharnier out_of_mem(); 22141558Srgrimes strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN); 22151558Srgrimes mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 22161558Srgrimes strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 22171558Srgrimes mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 22181558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 22191558Srgrimes *mlpp = mlp; 22201558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 222137663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 22221558Srgrimes return; 22231558Srgrimes } 22241558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 22251558Srgrimes fclose(mlfile); 22261558Srgrimes} 22271558Srgrimes 22281558Srgrimes/* 22291558Srgrimes * Free up a group list. 22301558Srgrimes */ 22311558Srgrimesvoid 22321558Srgrimesfree_grp(grp) 22331558Srgrimes struct grouplist *grp; 22341558Srgrimes{ 22351558Srgrimes if (grp->gr_type == GT_HOST) { 223674462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 223774462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 22381558Srgrimes } else if (grp->gr_type == GT_NET) { 22391558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 22401558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 22411558Srgrimes } 22421558Srgrimes free((caddr_t)grp); 22431558Srgrimes} 22441558Srgrimes 22451558Srgrimes#ifdef DEBUG 22461558Srgrimesvoid 22471558SrgrimesSYSLOG(int pri, const char *fmt, ...) 22481558Srgrimes{ 22491558Srgrimes va_list ap; 22501558Srgrimes 22511558Srgrimes va_start(ap, fmt); 22521558Srgrimes vfprintf(stderr, fmt, ap); 22531558Srgrimes va_end(ap); 22541558Srgrimes} 22551558Srgrimes#endif /* DEBUG */ 22561558Srgrimes 22571558Srgrimes/* 22581558Srgrimes * Check options for consistency. 22591558Srgrimes */ 22601558Srgrimesint 22611558Srgrimescheck_options(dp) 22621558Srgrimes struct dirlist *dp; 22631558Srgrimes{ 22641558Srgrimes 22651558Srgrimes if (dp == (struct dirlist *)NULL) 22661558Srgrimes return (1); 226783653Speter if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) { 226883653Speter syslog(LOG_ERR, "-mapall and -maproot mutually exclusive"); 22691558Srgrimes return (1); 22701558Srgrimes } 22711558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 227275801Siedowse syslog(LOG_ERR, "-mask requires -network"); 227375801Siedowse return (1); 22741558Srgrimes } 227575801Siedowse if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) { 227675801Siedowse syslog(LOG_ERR, "-network requires mask specification"); 227775801Siedowse return (1); 227875801Siedowse } 227975801Siedowse if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) { 228075801Siedowse syslog(LOG_ERR, "-mask and /masklen are mutually exclusive"); 228175801Siedowse return (1); 228275801Siedowse } 22831558Srgrimes if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 228445927Salex syslog(LOG_ERR, "-alldirs has multiple directories"); 22851558Srgrimes return (1); 22861558Srgrimes } 22871558Srgrimes return (0); 22881558Srgrimes} 22891558Srgrimes 22901558Srgrimes/* 22911558Srgrimes * Check an absolute directory path for any symbolic links. Return true 22921558Srgrimes */ 22931558Srgrimesint 22941558Srgrimescheck_dirpath(dirp) 22951558Srgrimes char *dirp; 22961558Srgrimes{ 22971558Srgrimes char *cp; 22981558Srgrimes int ret = 1; 22991558Srgrimes struct stat sb; 23001558Srgrimes 23011558Srgrimes cp = dirp + 1; 23021558Srgrimes while (*cp && ret) { 23031558Srgrimes if (*cp == '/') { 23041558Srgrimes *cp = '\0'; 23059336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23061558Srgrimes ret = 0; 23071558Srgrimes *cp = '/'; 23081558Srgrimes } 23091558Srgrimes cp++; 23101558Srgrimes } 23119336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23121558Srgrimes ret = 0; 23131558Srgrimes return (ret); 23141558Srgrimes} 23159336Sdfr 231675801Siedowse/* 231775801Siedowse * Make a netmask according to the specified prefix length. The ss_family 231875801Siedowse * and other non-address fields must be initialised before calling this. 231975801Siedowse */ 232075801Siedowseint 232175801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen) 232274462Salfred{ 232375801Siedowse u_char *p; 232475801Siedowse int bits, i, len; 232574462Salfred 232675801Siedowse if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL) 232775801Siedowse return (-1); 232875801Siedowse if (bitlen > len * NBBY) 232975801Siedowse return (-1); 233074462Salfred 233175801Siedowse for (i = 0; i < len; i++) { 233275801Siedowse bits = (bitlen > NBBY) ? NBBY : bitlen; 233375801Siedowse *p++ = (1 << bits) - 1; 233475801Siedowse bitlen -= bits; 233574462Salfred } 233675801Siedowse return 0; 233774462Salfred} 233874462Salfred 233975801Siedowse/* 234075801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask 234175801Siedowse * is acceptable (i.e. of the form 1...10....0). 234275801Siedowse */ 234375801Siedowseint 234475801Siedowsecheckmask(struct sockaddr *sa) 234574462Salfred{ 234675801Siedowse u_char *mask; 234775801Siedowse int i, len; 234874462Salfred 234975801Siedowse if ((mask = sa_rawaddr(sa, &len)) == NULL) 235075801Siedowse return (-1); 235175801Siedowse 235275801Siedowse for (i = 0; i < len; i++) 235375801Siedowse if (mask[i] != 0xff) 235475801Siedowse break; 235575801Siedowse if (i < len) { 235675801Siedowse if (~mask[i] & (u_char)(~mask[i] + 1)) 235775801Siedowse return (-1); 235875801Siedowse i++; 235974462Salfred } 236075801Siedowse for (; i < len; i++) 236175801Siedowse if (mask[i] != 0) 236275801Siedowse return (-1); 236375801Siedowse return (0); 236474462Salfred} 236574462Salfred 236675801Siedowse/* 236775801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if 236875801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'. 236975801Siedowse * If samask is NULL, perform a full comparision. 237075801Siedowse */ 237175801Siedowseint 237275801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask) 237374462Salfred{ 237475801Siedowse unsigned char *p1, *p2, *mask; 237575801Siedowse int len, i; 237674462Salfred 237775801Siedowse if (sa1->sa_family != sa2->sa_family || 237875801Siedowse (p1 = sa_rawaddr(sa1, &len)) == NULL || 237975801Siedowse (p2 = sa_rawaddr(sa2, NULL)) == NULL) 238075801Siedowse return (1); 238175801Siedowse 238275801Siedowse switch (sa1->sa_family) { 238374462Salfred case AF_INET6: 238475801Siedowse if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 238575801Siedowse ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 238675801Siedowse return (1); 238774462Salfred break; 238874462Salfred } 238974462Salfred 239075801Siedowse /* Simple binary comparison if no mask specified. */ 239175801Siedowse if (samask == NULL) 239275801Siedowse return (memcmp(p1, p2, len)); 239374462Salfred 239475801Siedowse /* Set up the mask, and do a mask-based comparison. */ 239575801Siedowse if (sa1->sa_family != samask->sa_family || 239675801Siedowse (mask = sa_rawaddr(samask, NULL)) == NULL) 239775801Siedowse return (1); 239874462Salfred 239975801Siedowse for (i = 0; i < len; i++) 240075801Siedowse if ((p1[i] & mask[i]) != (p2[i] & mask[i])) 240175801Siedowse return (1); 240275801Siedowse return (0); 240374462Salfred} 240474462Salfred 240575801Siedowse/* 240675801Siedowse * Return a pointer to the part of the sockaddr that contains the 240775801Siedowse * raw address, and set *nbytes to its length in bytes. Returns 240875801Siedowse * NULL if the address family is unknown. 240975801Siedowse */ 241075801Siedowsevoid * 241175801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) { 241275801Siedowse void *p; 241374462Salfred int len; 241474462Salfred 241575801Siedowse switch (sa->sa_family) { 241674462Salfred case AF_INET: 241775801Siedowse len = sizeof(((struct sockaddr_in *)sa)->sin_addr); 241875801Siedowse p = &((struct sockaddr_in *)sa)->sin_addr; 241974462Salfred break; 242074462Salfred case AF_INET6: 242175801Siedowse len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); 242275801Siedowse p = &((struct sockaddr_in6 *)sa)->sin6_addr; 242374462Salfred break; 242474462Salfred default: 242575801Siedowse p = NULL; 242675801Siedowse len = 0; 242774462Salfred } 242874462Salfred 242975801Siedowse if (nbytes != NULL) 243075801Siedowse *nbytes = len; 243175801Siedowse return (p); 243274462Salfred} 243374462Salfred 243475754Siedowsevoid 243575754Siedowsehuphandler(int sig) 243675754Siedowse{ 243775754Siedowse got_sighup = 1; 243875754Siedowse} 243975754Siedowse 244074462Salfredvoid terminate(sig) 244174462Salfredint sig; 244274462Salfred{ 244374462Salfred close(mountdlockfd); 244474462Salfred unlink(MOUNTDLOCK); 244574792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 244674792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 244774462Salfred exit (0); 244874462Salfred} 2449