mountd.c revision 75754
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 75754 2001-04-21 00:55:17Z iedowse $"; 492999Swollman#endif /*not lint*/ 501558Srgrimes 511558Srgrimes#include <sys/param.h> 521558Srgrimes#include <sys/mount.h> 5374462Salfred#include <sys/fcntl.h> 541558Srgrimes#include <sys/stat.h> 551558Srgrimes#include <sys/syslog.h> 5624330Sguido#include <sys/sysctl.h> 571558Srgrimes 581558Srgrimes#include <rpc/rpc.h> 591558Srgrimes#include <rpc/pmap_clnt.h> 6074462Salfred#include <rpc/pmap_prot.h> 6174462Salfred#include <rpcsvc/mount.h> 621558Srgrimes#include <nfs/rpcv2.h> 639336Sdfr#include <nfs/nfsproto.h> 6424330Sguido#include <nfs/nfs.h> 6523681Speter#include <ufs/ufs/ufsmount.h> 6623681Speter#include <msdosfs/msdosfsmount.h> 6754093Ssemenu#include <ntfs/ntfsmount.h> 6823681Speter#include <isofs/cd9660/cd9660_mount.h> /* XXX need isofs in include */ 691558Srgrimes 701558Srgrimes#include <arpa/inet.h> 711558Srgrimes 721558Srgrimes#include <ctype.h> 7337663Scharnier#include <err.h> 741558Srgrimes#include <errno.h> 751558Srgrimes#include <grp.h> 761558Srgrimes#include <netdb.h> 771558Srgrimes#include <pwd.h> 781558Srgrimes#include <signal.h> 791558Srgrimes#include <stdio.h> 801558Srgrimes#include <stdlib.h> 811558Srgrimes#include <string.h> 821558Srgrimes#include <unistd.h> 831558Srgrimes#include "pathnames.h" 841558Srgrimes 851558Srgrimes#ifdef DEBUG 861558Srgrimes#include <stdarg.h> 871558Srgrimes#endif 881558Srgrimes 8974462Salfred#ifndef MOUNTDLOCK 9074462Salfred#define MOUNTDLOCK "/var/run/mountd.lock" 9174462Salfred#endif 9274462Salfred 931558Srgrimes/* 941558Srgrimes * Structures for keeping the mount list and export list 951558Srgrimes */ 961558Srgrimesstruct mountlist { 971558Srgrimes struct mountlist *ml_next; 981558Srgrimes char ml_host[RPCMNT_NAMELEN+1]; 991558Srgrimes char ml_dirp[RPCMNT_PATHLEN+1]; 1001558Srgrimes}; 1011558Srgrimes 1021558Srgrimesstruct dirlist { 1031558Srgrimes struct dirlist *dp_left; 1041558Srgrimes struct dirlist *dp_right; 1051558Srgrimes int dp_flag; 1061558Srgrimes struct hostlist *dp_hosts; /* List of hosts this dir exported to */ 1071558Srgrimes char dp_dirp[1]; /* Actually malloc'd to size of dir */ 1081558Srgrimes}; 1091558Srgrimes/* dp_flag bits */ 1101558Srgrimes#define DP_DEFSET 0x1 1119336Sdfr#define DP_HOSTSET 0x2 1129336Sdfr#define DP_KERB 0x4 1131558Srgrimes 1141558Srgrimesstruct exportlist { 1151558Srgrimes struct exportlist *ex_next; 1161558Srgrimes struct dirlist *ex_dirl; 1171558Srgrimes struct dirlist *ex_defdir; 1181558Srgrimes int ex_flag; 1191558Srgrimes fsid_t ex_fs; 1201558Srgrimes char *ex_fsdir; 12127447Sdfr char *ex_indexfile; 1221558Srgrimes}; 1231558Srgrimes/* ex_flag bits */ 1241558Srgrimes#define EX_LINKED 0x1 1251558Srgrimes 1261558Srgrimesstruct netmsk { 12774462Salfred struct sockaddr_storage nt_net; 12842144Sdfr u_int32_t nt_mask; 12942144Sdfr char *nt_name; 1301558Srgrimes}; 1311558Srgrimes 1321558Srgrimesunion grouptypes { 13374462Salfred struct addrinfo *gt_addrinfo; 1341558Srgrimes struct netmsk gt_net; 1351558Srgrimes}; 1361558Srgrimes 1371558Srgrimesstruct grouplist { 1381558Srgrimes int gr_type; 1391558Srgrimes union grouptypes gr_ptr; 1401558Srgrimes struct grouplist *gr_next; 1411558Srgrimes}; 1421558Srgrimes/* Group types */ 1431558Srgrimes#define GT_NULL 0x0 1441558Srgrimes#define GT_HOST 0x1 1451558Srgrimes#define GT_NET 0x2 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 *)); 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); 1921558Srgrimesvoid mntsrv __P((struct svc_req *, SVCXPRT *)); 1931558Srgrimesvoid nextfield __P((char **, char **)); 1941558Srgrimesvoid out_of_mem __P((void)); 19572650Sgreenvoid parsecred __P((char *, struct xucred *)); 1961558Srgrimesint put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *)); 19774462Salfredint scan_tree __P((struct dirlist *, struct sockaddr *)); 19837663Scharnierstatic void usage __P((void)); 1991558Srgrimesint xdr_dir __P((XDR *, char *)); 2001558Srgrimesint xdr_explist __P((XDR *, caddr_t)); 2019336Sdfrint xdr_fhs __P((XDR *, caddr_t)); 2021558Srgrimesint xdr_mlist __P((XDR *, caddr_t)); 20374462Salfredvoid terminate __P((int)); 2041558Srgrimes 20574462Salfredstatic int bitcmp __P((void *, void *, int)); 20674462Salfredstatic int netpartcmp __P((struct sockaddr *, struct sockaddr *, int)); 20774462Salfredstatic int sacmp __P((struct sockaddr *, struct sockaddr *)); 20874462Salfredstatic int allones __P((struct sockaddr_storage *, int)); 20974462Salfredstatic int countones __P((struct sockaddr *)); 21074462Salfred 2111558Srgrimesstruct exportlist *exphead; 2121558Srgrimesstruct mountlist *mlhead; 2131558Srgrimesstruct grouplist *grphead; 2141558Srgrimeschar exname[MAXPATHLEN]; 21572650Sgreenstruct xucred def_anon = { 21672650Sgreen 0, 21772650Sgreen (uid_t)-2, 2181558Srgrimes 1, 21972650Sgreen { (gid_t)-2 }, 22072650Sgreen NULL 2211558Srgrimes}; 22225087Sdfrint force_v2 = 0; 2239336Sdfrint resvport_only = 1; 2249336Sdfrint dir_only = 1; 22531705Sguidoint log = 0; 22675754Siedowseint got_sighup = 0; 22774462Salfred 2281558Srgrimesint opt_flags; 22974462Salfredstatic int have_v6 = 1; 23074462Salfred#ifdef NI_WITHSCOPEID 23174462Salfredstatic const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID; 23274462Salfred#else 23374462Salfredstatic const int ninumeric = NI_NUMERICHOST; 23474462Salfred#endif 23574462Salfred 23674462Salfredint mountdlockfd; 2371558Srgrimes/* Bits for above */ 2381558Srgrimes#define OP_MAPROOT 0x01 2391558Srgrimes#define OP_MAPALL 0x02 2401558Srgrimes#define OP_KERB 0x04 2411558Srgrimes#define OP_MASK 0x08 2421558Srgrimes#define OP_NET 0x10 2431558Srgrimes#define OP_ALLDIRS 0x40 24474462Salfred#define OP_MASKLEN 0x200 2451558Srgrimes 2461558Srgrimes#ifdef DEBUG 2471558Srgrimesint debug = 1; 2481558Srgrimesvoid SYSLOG __P((int, const char *, ...)); 2491558Srgrimes#define syslog SYSLOG 2501558Srgrimes#else 2511558Srgrimesint debug = 0; 2521558Srgrimes#endif 2531558Srgrimes 2541558Srgrimes/* 2551558Srgrimes * Mountd server for NFS mount protocol as described in: 2561558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A 2571558Srgrimes * The optional arguments are the exports file name 2581558Srgrimes * default: _PATH_EXPORTS 2591558Srgrimes * and "-n" to allow nonroot mount. 2601558Srgrimes */ 2611558Srgrimesint 2621558Srgrimesmain(argc, argv) 2631558Srgrimes int argc; 2641558Srgrimes char **argv; 2651558Srgrimes{ 26675754Siedowse fd_set readfds; 26774462Salfred SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp; 26874462Salfred struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf; 26974462Salfred int udpsock, tcpsock, udp6sock, tcp6sock; 27074462Salfred int xcreated = 0, s; 27174462Salfred int one = 1; 27232656Sbde int c, error, mib[3]; 27323681Speter struct vfsconf vfc; 2741558Srgrimes 27575635Siedowse udp6conf = tcp6conf = NULL; 27675635Siedowse udp6sock = tcp6sock = NULL; 27775635Siedowse 27874462Salfred /* Check that another mountd isn't already running. */ 27974462Salfred if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1) 28074462Salfred err(1, "%s", MOUNTDLOCK); 28174462Salfred 28274462Salfred if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) 28374462Salfred errx(1, "another rpc.mountd is already running. Aborting"); 28474462Salfred s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 28574462Salfred if (s < 0) 28674462Salfred have_v6 = 0; 28774462Salfred else 28874462Salfred close(s); 28923681Speter error = getvfsbyname("nfs", &vfc); 29023681Speter if (error && vfsisloadable("nfs")) { 2912999Swollman if(vfsload("nfs")) 2922999Swollman err(1, "vfsload(nfs)"); 2932999Swollman endvfsent(); /* flush cache */ 29423681Speter error = getvfsbyname("nfs", &vfc); 2952999Swollman } 29623681Speter if (error) 2972999Swollman errx(1, "NFS support is not available in the running kernel"); 2982999Swollman 29931665Sguido while ((c = getopt(argc, argv, "2dlnr")) != -1) 3001558Srgrimes switch (c) { 30125087Sdfr case '2': 30225087Sdfr force_v2 = 1; 30325087Sdfr break; 3049336Sdfr case 'n': 3059336Sdfr resvport_only = 0; 3069336Sdfr break; 3079336Sdfr case 'r': 3089336Sdfr dir_only = 0; 3099336Sdfr break; 3108688Sphk case 'd': 3118688Sphk debug = debug ? 0 : 1; 3128688Sphk break; 31331656Sguido case 'l': 31431656Sguido log = 1; 31531656Sguido break; 3161558Srgrimes default: 31737663Scharnier usage(); 3181558Srgrimes }; 3191558Srgrimes argc -= optind; 3201558Srgrimes argv += optind; 3211558Srgrimes grphead = (struct grouplist *)NULL; 3221558Srgrimes exphead = (struct exportlist *)NULL; 3231558Srgrimes mlhead = (struct mountlist *)NULL; 3241558Srgrimes if (argc == 1) { 3251558Srgrimes strncpy(exname, *argv, MAXPATHLEN-1); 3261558Srgrimes exname[MAXPATHLEN-1] = '\0'; 3271558Srgrimes } else 3281558Srgrimes strcpy(exname, _PATH_EXPORTS); 3291558Srgrimes openlog("mountd", LOG_PID, LOG_DAEMON); 3301558Srgrimes if (debug) 33137663Scharnier warnx("getting export list"); 3321558Srgrimes get_exportlist(); 3331558Srgrimes if (debug) 33437663Scharnier warnx("getting mount list"); 3351558Srgrimes get_mountlist(); 3361558Srgrimes if (debug) 33737663Scharnier warnx("here we go"); 3381558Srgrimes if (debug == 0) { 3391558Srgrimes daemon(0, 0); 3401558Srgrimes signal(SIGINT, SIG_IGN); 3411558Srgrimes signal(SIGQUIT, SIG_IGN); 3421558Srgrimes } 34375754Siedowse signal(SIGHUP, huphandler); 34474462Salfred signal(SIGTERM, terminate); 3451558Srgrimes { FILE *pidfile = fopen(_PATH_MOUNTDPID, "w"); 3461558Srgrimes if (pidfile != NULL) { 3471558Srgrimes fprintf(pidfile, "%d\n", getpid()); 3481558Srgrimes fclose(pidfile); 3491558Srgrimes } 3501558Srgrimes } 35174462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 35274462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 35374462Salfred udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 35474462Salfred tcpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 35574791Salfred udpconf = getnetconfigent("udp"); 35674791Salfred tcpconf = getnetconfigent("tcp"); 35774791Salfred if (!have_v6) 35874791Salfred goto skip_v6; 35974462Salfred udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 36074462Salfred tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 36174462Salfred /* 36274462Salfred * We're doing host-based access checks here, so don't allow 36374462Salfred * v4-in-v6 to confuse things. The kernel will disable it 36474462Salfred * by default on NFS sockets too. 36574462Salfred */ 36674462Salfred if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6, 36774462Salfred IPV6_BINDV6ONLY, &one, sizeof one) < 0){ 36874462Salfred syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket"); 36974462Salfred exit(1); 37074462Salfred } 37174462Salfred if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6, 37274462Salfred IPV6_BINDV6ONLY, &one, sizeof one) < 0){ 37374462Salfred syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket"); 37474462Salfred exit(1); 37574462Salfred } 37674462Salfred udp6conf = getnetconfigent("udp6"); 37774462Salfred tcp6conf = getnetconfigent("tcp6"); 37874791Salfred 37974791Salfredskip_v6: 38024759Sguido if (!resvport_only) { 38124759Sguido mib[0] = CTL_VFS; 38232656Sbde mib[1] = vfc.vfc_typenum; 38324759Sguido mib[2] = NFS_NFSPRIVPORT; 38424759Sguido if (sysctl(mib, 3, NULL, NULL, &resvport_only, 38524759Sguido sizeof(resvport_only)) != 0 && errno != ENOENT) { 38624759Sguido syslog(LOG_ERR, "sysctl: %m"); 38724759Sguido exit(1); 38824759Sguido } 38924330Sguido } 3909202Srgrimes if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL || 3919202Srgrimes (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) { 39237663Scharnier syslog(LOG_ERR, "can't create socket"); 3931558Srgrimes exit(1); 3941558Srgrimes } 39574462Salfred if (udpsock != -1 && udpconf != NULL) { 39674462Salfred bindresvport(udpsock, NULL); 39774462Salfred udptransp = svc_dg_create(udpsock, 0, 0); 39874462Salfred if (udptransp != NULL) { 39974462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1, 40074462Salfred mntsrv, udpconf)) 40174462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service"); 40274462Salfred else 40374462Salfred xcreated++; 40474462Salfred if (!force_v2) { 40574462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3, 40674462Salfred mntsrv, udpconf)) 40774462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service"); 40874462Salfred else 40974462Salfred xcreated++; 41074462Salfred } 41174462Salfred } else 41274462Salfred syslog(LOG_WARNING, "can't create UDP services"); 41374462Salfred 41474462Salfred } 41574462Salfred if (tcpsock != -1 && tcpconf != NULL) { 41674462Salfred bindresvport(tcpsock, NULL); 41774462Salfred listen(tcpsock, SOMAXCONN); 41874462Salfred tcptransp = svc_vc_create(tcpsock, 0, 0); 41974462Salfred if (tcptransp != NULL) { 42074462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1, 42174462Salfred mntsrv, tcpconf)) 42274462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service"); 42374462Salfred else 42474462Salfred xcreated++; 42574462Salfred if (!force_v2) { 42674462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3, 42774462Salfred mntsrv, tcpconf)) 42874462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service"); 42974462Salfred else 43074462Salfred xcreated++; 43174462Salfred } 43274462Salfred } else 43374462Salfred syslog(LOG_WARNING, "can't create TCP service"); 43474462Salfred 43574462Salfred } 43674791Salfred if (have_v6 && udp6sock != -1 && udp6conf != NULL) { 43774462Salfred bindresvport(udp6sock, NULL); 43874462Salfred udp6transp = svc_dg_create(udp6sock, 0, 0); 43974462Salfred if (udp6transp != NULL) { 44074462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1, 44174462Salfred mntsrv, udp6conf)) 44274462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service"); 44374462Salfred else 44474462Salfred xcreated++; 44574462Salfred if (!force_v2) { 44674462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3, 44774462Salfred mntsrv, udp6conf)) 44874462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service"); 44974462Salfred else 45074462Salfred xcreated++; 45174462Salfred } 45274462Salfred } else 45374462Salfred syslog(LOG_WARNING, "can't create UDP6 service"); 45474462Salfred 45574462Salfred } 45674791Salfred if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) { 45774462Salfred bindresvport(tcp6sock, NULL); 45874462Salfred listen(tcp6sock, SOMAXCONN); 45974462Salfred tcp6transp = svc_vc_create(tcp6sock, 0, 0); 46074462Salfred if (tcp6transp != NULL) { 46174462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1, 46274462Salfred mntsrv, tcp6conf)) 46374462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service"); 46474462Salfred else 46574462Salfred xcreated++; 46674462Salfred if (!force_v2) { 46774462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3, 46874462Salfred mntsrv, tcp6conf)) 46974462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service"); 47074462Salfred else 47174462Salfred xcreated++; 47274462Salfred } 47374462Salfred } else 47474462Salfred syslog(LOG_WARNING, "can't create TCP6 service"); 47574462Salfred 47674462Salfred } 47774462Salfred if (xcreated == 0) { 47874462Salfred syslog(LOG_ERR, "could not create any services"); 4791558Srgrimes exit(1); 4801558Srgrimes } 48175754Siedowse 48275754Siedowse /* Expand svc_run() here so that we can call get_exportlist(). */ 48375754Siedowse for (;;) { 48475754Siedowse if (got_sighup) { 48575754Siedowse get_exportlist(); 48675754Siedowse got_sighup = 0; 48775754Siedowse } 48875754Siedowse readfds = svc_fdset; 48975754Siedowse switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) { 49075754Siedowse case -1: 49175754Siedowse if (errno == EINTR) 49275754Siedowse continue; 49375754Siedowse syslog(LOG_ERR, "mountd died: select: %m"); 49475754Siedowse exit(1); 49575754Siedowse case 0: 49675754Siedowse continue; 49775754Siedowse default: 49875754Siedowse svc_getreqset(&readfds); 49975754Siedowse } 50075754Siedowse } 5011558Srgrimes} 5021558Srgrimes 50337663Scharnierstatic void 50437663Scharnierusage() 50537663Scharnier{ 50637663Scharnier fprintf(stderr, 50737663Scharnier "usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n"); 50837663Scharnier exit(1); 50937663Scharnier} 51037663Scharnier 5111558Srgrimes/* 5121558Srgrimes * The mount rpc service 5131558Srgrimes */ 5141558Srgrimesvoid 5151558Srgrimesmntsrv(rqstp, transp) 5161558Srgrimes struct svc_req *rqstp; 5171558Srgrimes SVCXPRT *transp; 5181558Srgrimes{ 5191558Srgrimes struct exportlist *ep; 5201558Srgrimes struct dirlist *dp; 5219336Sdfr struct fhreturn fhr; 5221558Srgrimes struct stat stb; 5231558Srgrimes struct statfs fsb; 52474462Salfred struct addrinfo *ai; 52574462Salfred char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 52674462Salfred int lookup_failed = 1; 52774462Salfred struct sockaddr *saddr; 5289336Sdfr u_short sport; 52923681Speter char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN]; 53028911Sguido int bad = 0, defset, hostset; 5319336Sdfr sigset_t sighup_mask; 5321558Srgrimes 5339336Sdfr sigemptyset(&sighup_mask); 5349336Sdfr sigaddset(&sighup_mask, SIGHUP); 53574462Salfred saddr = svc_getrpccaller(transp)->buf; 53674462Salfred switch (saddr->sa_family) { 53774462Salfred case AF_INET6: 53875635Siedowse sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 53974462Salfred break; 54074462Salfred case AF_INET: 54175635Siedowse sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 54274462Salfred break; 54374462Salfred default: 54474462Salfred syslog(LOG_ERR, "request from unknown address family"); 54574462Salfred return; 54674462Salfred } 54774462Salfred lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, 54874462Salfred NULL, 0, 0); 54974462Salfred getnameinfo(saddr, saddr->sa_len, numerichost, 55074462Salfred sizeof numerichost, NULL, 0, NI_NUMERICHOST); 55174462Salfred ai = NULL; 5521558Srgrimes switch (rqstp->rq_proc) { 5531558Srgrimes case NULLPROC: 5541558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 55537663Scharnier syslog(LOG_ERR, "can't send reply"); 5561558Srgrimes return; 5571558Srgrimes case RPCMNT_MOUNT: 5589336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 55931656Sguido syslog(LOG_NOTICE, 56031656Sguido "mount request from %s from unprivileged port", 56174462Salfred numerichost); 5621558Srgrimes svcerr_weakauth(transp); 5631558Srgrimes return; 5641558Srgrimes } 5651558Srgrimes if (!svc_getargs(transp, xdr_dir, rpcpath)) { 56631656Sguido syslog(LOG_NOTICE, "undecodable mount request from %s", 56774462Salfred numerichost); 5681558Srgrimes svcerr_decode(transp); 5691558Srgrimes return; 5701558Srgrimes } 5711558Srgrimes 5721558Srgrimes /* 5731558Srgrimes * Get the real pathname and make sure it is a directory 5749336Sdfr * or a regular file if the -r option was specified 5759336Sdfr * and it exists. 5761558Srgrimes */ 57751968Salfred if (realpath(rpcpath, dirpath) == NULL || 5781558Srgrimes stat(dirpath, &stb) < 0 || 5799336Sdfr (!S_ISDIR(stb.st_mode) && 58074462Salfred (dir_only || !S_ISREG(stb.st_mode))) || 5811558Srgrimes statfs(dirpath, &fsb) < 0) { 5821558Srgrimes chdir("/"); /* Just in case realpath doesn't */ 58331656Sguido syslog(LOG_NOTICE, 58437663Scharnier "mount request from %s for non existent path %s", 58574462Salfred numerichost, dirpath); 5861558Srgrimes if (debug) 58737663Scharnier warnx("stat failed on %s", dirpath); 58828911Sguido bad = ENOENT; /* We will send error reply later */ 5891558Srgrimes } 5901558Srgrimes 5911558Srgrimes /* Check in the exports list */ 5929336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 5931558Srgrimes ep = ex_search(&fsb.f_fsid); 5949336Sdfr hostset = defset = 0; 5959336Sdfr if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) || 5961558Srgrimes ((dp = dirp_search(ep->ex_dirl, dirpath)) && 59774462Salfred chk_host(dp, saddr, &defset, &hostset)) || 59874462Salfred (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 59974462Salfred scan_tree(ep->ex_dirl, saddr) == 0))) { 60028911Sguido if (bad) { 60128911Sguido if (!svc_sendreply(transp, xdr_long, 60228911Sguido (caddr_t)&bad)) 60337663Scharnier syslog(LOG_ERR, "can't send reply"); 60428911Sguido sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 60528911Sguido return; 60628911Sguido } 6079336Sdfr if (hostset & DP_HOSTSET) 6089336Sdfr fhr.fhr_flag = hostset; 6099336Sdfr else 6109336Sdfr fhr.fhr_flag = defset; 6119336Sdfr fhr.fhr_vers = rqstp->rq_vers; 6121558Srgrimes /* Get the file handle */ 61323681Speter memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 6149336Sdfr if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 6151558Srgrimes bad = errno; 61637663Scharnier syslog(LOG_ERR, "can't get fh for %s", dirpath); 6171558Srgrimes if (!svc_sendreply(transp, xdr_long, 6181558Srgrimes (caddr_t)&bad)) 61937663Scharnier syslog(LOG_ERR, "can't send reply"); 6209336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6211558Srgrimes return; 6221558Srgrimes } 6239336Sdfr if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr)) 62437663Scharnier syslog(LOG_ERR, "can't send reply"); 62574462Salfred if (!lookup_failed) 62674462Salfred add_mlist(host, dirpath); 6271558Srgrimes else 62874462Salfred add_mlist(numerichost, dirpath); 6291558Srgrimes if (debug) 63037663Scharnier warnx("mount successful"); 63131656Sguido if (log) 63231656Sguido syslog(LOG_NOTICE, 63331656Sguido "mount request succeeded from %s for %s", 63474462Salfred numerichost, dirpath); 63531656Sguido } else { 6361558Srgrimes bad = EACCES; 63731656Sguido syslog(LOG_NOTICE, 63831656Sguido "mount request denied from %s for %s", 63974462Salfred numerichost, dirpath); 64031656Sguido } 64128911Sguido 64228911Sguido if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad)) 64337663Scharnier syslog(LOG_ERR, "can't send reply"); 6449336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6451558Srgrimes return; 6461558Srgrimes case RPCMNT_DUMP: 6471558Srgrimes if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL)) 64837663Scharnier syslog(LOG_ERR, "can't send reply"); 64931656Sguido else if (log) 65031656Sguido syslog(LOG_NOTICE, 65131656Sguido "dump request succeeded from %s", 65274462Salfred numerichost); 6531558Srgrimes return; 6541558Srgrimes case RPCMNT_UMOUNT: 6559336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 65631656Sguido syslog(LOG_NOTICE, 65731656Sguido "umount request from %s from unprivileged port", 65874462Salfred numerichost); 6591558Srgrimes svcerr_weakauth(transp); 6601558Srgrimes return; 6611558Srgrimes } 66251968Salfred if (!svc_getargs(transp, xdr_dir, rpcpath)) { 66331656Sguido syslog(LOG_NOTICE, "undecodable umount request from %s", 66474462Salfred numerichost); 6651558Srgrimes svcerr_decode(transp); 6661558Srgrimes return; 6671558Srgrimes } 66851968Salfred if (realpath(rpcpath, dirpath) == NULL) { 66951968Salfred syslog(LOG_NOTICE, "umount request from %s " 67051968Salfred "for non existent path %s", 67174462Salfred numerichost, dirpath); 67251968Salfred } 6731558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 67437663Scharnier syslog(LOG_ERR, "can't send reply"); 67574462Salfred if (!lookup_failed) 67675635Siedowse del_mlist(host, dirpath); 67775635Siedowse del_mlist(numerichost, dirpath); 67831656Sguido if (log) 67931656Sguido syslog(LOG_NOTICE, 68031656Sguido "umount request succeeded from %s for %s", 68174462Salfred numerichost, dirpath); 6821558Srgrimes return; 6831558Srgrimes case RPCMNT_UMNTALL: 6849336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 68531656Sguido syslog(LOG_NOTICE, 68631656Sguido "umountall request from %s from unprivileged port", 68774462Salfred numerichost); 6881558Srgrimes svcerr_weakauth(transp); 6891558Srgrimes return; 6901558Srgrimes } 6911558Srgrimes if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL)) 69237663Scharnier syslog(LOG_ERR, "can't send reply"); 69374462Salfred if (!lookup_failed) 69475635Siedowse del_mlist(host, NULL); 69575635Siedowse del_mlist(numerichost, NULL); 69631656Sguido if (log) 69731656Sguido syslog(LOG_NOTICE, 69831656Sguido "umountall request succeeded from %s", 69974462Salfred numerichost); 7001558Srgrimes return; 7011558Srgrimes case RPCMNT_EXPORT: 7021558Srgrimes if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL)) 70337663Scharnier syslog(LOG_ERR, "can't send reply"); 70431656Sguido if (log) 70531656Sguido syslog(LOG_NOTICE, 70631656Sguido "export request succeeded from %s", 70774462Salfred numerichost); 7081558Srgrimes return; 7091558Srgrimes default: 7101558Srgrimes svcerr_noproc(transp); 7111558Srgrimes return; 7121558Srgrimes } 7131558Srgrimes} 7141558Srgrimes 7151558Srgrimes/* 7161558Srgrimes * Xdr conversion for a dirpath string 7171558Srgrimes */ 7181558Srgrimesint 7191558Srgrimesxdr_dir(xdrsp, dirp) 7201558Srgrimes XDR *xdrsp; 7211558Srgrimes char *dirp; 7221558Srgrimes{ 7231558Srgrimes return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 7241558Srgrimes} 7251558Srgrimes 7261558Srgrimes/* 7279336Sdfr * Xdr routine to generate file handle reply 7281558Srgrimes */ 7291558Srgrimesint 7309336Sdfrxdr_fhs(xdrsp, cp) 7311558Srgrimes XDR *xdrsp; 7329336Sdfr caddr_t cp; 7331558Srgrimes{ 7349336Sdfr register struct fhreturn *fhrp = (struct fhreturn *)cp; 7359336Sdfr u_long ok = 0, len, auth; 7361558Srgrimes 7371558Srgrimes if (!xdr_long(xdrsp, &ok)) 7381558Srgrimes return (0); 7399336Sdfr switch (fhrp->fhr_vers) { 7409336Sdfr case 1: 7419336Sdfr return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 7429336Sdfr case 3: 7439336Sdfr len = NFSX_V3FH; 7449336Sdfr if (!xdr_long(xdrsp, &len)) 7459336Sdfr return (0); 7469336Sdfr if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 7479336Sdfr return (0); 7489336Sdfr if (fhrp->fhr_flag & DP_KERB) 7499336Sdfr auth = RPCAUTH_KERB4; 7509336Sdfr else 7519336Sdfr auth = RPCAUTH_UNIX; 7529336Sdfr len = 1; 7539336Sdfr if (!xdr_long(xdrsp, &len)) 7549336Sdfr return (0); 7559336Sdfr return (xdr_long(xdrsp, &auth)); 7569336Sdfr }; 7579336Sdfr return (0); 7581558Srgrimes} 7591558Srgrimes 7601558Srgrimesint 7611558Srgrimesxdr_mlist(xdrsp, cp) 7621558Srgrimes XDR *xdrsp; 7631558Srgrimes caddr_t cp; 7641558Srgrimes{ 7651558Srgrimes struct mountlist *mlp; 7661558Srgrimes int true = 1; 7671558Srgrimes int false = 0; 7681558Srgrimes char *strp; 7691558Srgrimes 7701558Srgrimes mlp = mlhead; 7711558Srgrimes while (mlp) { 7721558Srgrimes if (!xdr_bool(xdrsp, &true)) 7731558Srgrimes return (0); 7741558Srgrimes strp = &mlp->ml_host[0]; 7751558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN)) 7761558Srgrimes return (0); 7771558Srgrimes strp = &mlp->ml_dirp[0]; 7781558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 7791558Srgrimes return (0); 7801558Srgrimes mlp = mlp->ml_next; 7811558Srgrimes } 7821558Srgrimes if (!xdr_bool(xdrsp, &false)) 7831558Srgrimes return (0); 7841558Srgrimes return (1); 7851558Srgrimes} 7861558Srgrimes 7871558Srgrimes/* 7881558Srgrimes * Xdr conversion for export list 7891558Srgrimes */ 7901558Srgrimesint 7911558Srgrimesxdr_explist(xdrsp, cp) 7921558Srgrimes XDR *xdrsp; 7931558Srgrimes caddr_t cp; 7941558Srgrimes{ 7951558Srgrimes struct exportlist *ep; 7961558Srgrimes int false = 0; 7979336Sdfr int putdef; 7989336Sdfr sigset_t sighup_mask; 7991558Srgrimes 8009336Sdfr sigemptyset(&sighup_mask); 8019336Sdfr sigaddset(&sighup_mask, SIGHUP); 8029336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 8031558Srgrimes ep = exphead; 8041558Srgrimes while (ep) { 8051558Srgrimes putdef = 0; 8061558Srgrimes if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef)) 8071558Srgrimes goto errout; 8081558Srgrimes if (ep->ex_defdir && putdef == 0 && 8091558Srgrimes put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 8101558Srgrimes &putdef)) 8111558Srgrimes goto errout; 8121558Srgrimes ep = ep->ex_next; 8131558Srgrimes } 8149336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8151558Srgrimes if (!xdr_bool(xdrsp, &false)) 8161558Srgrimes return (0); 8171558Srgrimes return (1); 8181558Srgrimeserrout: 8199336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8201558Srgrimes return (0); 8211558Srgrimes} 8221558Srgrimes 8231558Srgrimes/* 8241558Srgrimes * Called from xdr_explist() to traverse the tree and export the 8251558Srgrimes * directory paths. 8261558Srgrimes */ 8271558Srgrimesint 8281558Srgrimesput_exlist(dp, xdrsp, adp, putdefp) 8291558Srgrimes struct dirlist *dp; 8301558Srgrimes XDR *xdrsp; 8311558Srgrimes struct dirlist *adp; 8321558Srgrimes int *putdefp; 8331558Srgrimes{ 8341558Srgrimes struct grouplist *grp; 8351558Srgrimes struct hostlist *hp; 8361558Srgrimes int true = 1; 8371558Srgrimes int false = 0; 8381558Srgrimes int gotalldir = 0; 8391558Srgrimes char *strp; 8401558Srgrimes 8411558Srgrimes if (dp) { 8421558Srgrimes if (put_exlist(dp->dp_left, xdrsp, adp, putdefp)) 8431558Srgrimes return (1); 8441558Srgrimes if (!xdr_bool(xdrsp, &true)) 8451558Srgrimes return (1); 8461558Srgrimes strp = dp->dp_dirp; 8471558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 8481558Srgrimes return (1); 8491558Srgrimes if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 8501558Srgrimes gotalldir = 1; 8511558Srgrimes *putdefp = 1; 8521558Srgrimes } 8531558Srgrimes if ((dp->dp_flag & DP_DEFSET) == 0 && 8541558Srgrimes (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 8551558Srgrimes hp = dp->dp_hosts; 8561558Srgrimes while (hp) { 8571558Srgrimes grp = hp->ht_grp; 8581558Srgrimes if (grp->gr_type == GT_HOST) { 8591558Srgrimes if (!xdr_bool(xdrsp, &true)) 8601558Srgrimes return (1); 86174462Salfred strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 8628871Srgrimes if (!xdr_string(xdrsp, &strp, 8631558Srgrimes RPCMNT_NAMELEN)) 8641558Srgrimes return (1); 8651558Srgrimes } else if (grp->gr_type == GT_NET) { 8661558Srgrimes if (!xdr_bool(xdrsp, &true)) 8671558Srgrimes return (1); 8681558Srgrimes strp = grp->gr_ptr.gt_net.nt_name; 8698871Srgrimes if (!xdr_string(xdrsp, &strp, 8701558Srgrimes RPCMNT_NAMELEN)) 8711558Srgrimes return (1); 8721558Srgrimes } 8731558Srgrimes hp = hp->ht_next; 8741558Srgrimes if (gotalldir && hp == (struct hostlist *)NULL) { 8751558Srgrimes hp = adp->dp_hosts; 8761558Srgrimes gotalldir = 0; 8771558Srgrimes } 8781558Srgrimes } 8791558Srgrimes } 8801558Srgrimes if (!xdr_bool(xdrsp, &false)) 8811558Srgrimes return (1); 8821558Srgrimes if (put_exlist(dp->dp_right, xdrsp, adp, putdefp)) 8831558Srgrimes return (1); 8841558Srgrimes } 8851558Srgrimes return (0); 8861558Srgrimes} 8871558Srgrimes 8881558Srgrimes#define LINESIZ 10240 8891558Srgrimeschar line[LINESIZ]; 8901558SrgrimesFILE *exp_file; 8911558Srgrimes 8921558Srgrimes/* 8931558Srgrimes * Get the export list 8941558Srgrimes */ 8951558Srgrimesvoid 8961558Srgrimesget_exportlist() 8971558Srgrimes{ 8981558Srgrimes struct exportlist *ep, *ep2; 8991558Srgrimes struct grouplist *grp, *tgrp; 9001558Srgrimes struct exportlist **epp; 9011558Srgrimes struct dirlist *dirhead; 9021558Srgrimes struct statfs fsb, *fsp; 90372650Sgreen struct xucred anon; 9041558Srgrimes char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 9051558Srgrimes int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp; 9061558Srgrimes 90751968Salfred dirp = NULL; 90851968Salfred dirplen = 0; 90951968Salfred 9101558Srgrimes /* 9111558Srgrimes * First, get rid of the old list 9121558Srgrimes */ 9131558Srgrimes ep = exphead; 9141558Srgrimes while (ep) { 9151558Srgrimes ep2 = ep; 9161558Srgrimes ep = ep->ex_next; 9171558Srgrimes free_exp(ep2); 9181558Srgrimes } 9191558Srgrimes exphead = (struct exportlist *)NULL; 9201558Srgrimes 9211558Srgrimes grp = grphead; 9221558Srgrimes while (grp) { 9231558Srgrimes tgrp = grp; 9241558Srgrimes grp = grp->gr_next; 9251558Srgrimes free_grp(tgrp); 9261558Srgrimes } 9271558Srgrimes grphead = (struct grouplist *)NULL; 9281558Srgrimes 9291558Srgrimes /* 9301558Srgrimes * And delete exports that are in the kernel for all local 9311558Srgrimes * file systems. 9321558Srgrimes * XXX: Should know how to handle all local exportable file systems 93323681Speter * instead of just "ufs". 9341558Srgrimes */ 9351558Srgrimes num = getmntinfo(&fsp, MNT_NOWAIT); 9361558Srgrimes for (i = 0; i < num; i++) { 9371558Srgrimes union { 9381558Srgrimes struct ufs_args ua; 9391558Srgrimes struct iso_args ia; 9401558Srgrimes struct mfs_args ma; 9419336Sdfr struct msdosfs_args da; 94254093Ssemenu struct ntfs_args na; 9431558Srgrimes } targs; 9441558Srgrimes 94523681Speter if (!strcmp(fsp->f_fstypename, "mfs") || 94623681Speter !strcmp(fsp->f_fstypename, "ufs") || 94723681Speter !strcmp(fsp->f_fstypename, "msdos") || 94854093Ssemenu !strcmp(fsp->f_fstypename, "ntfs") || 94923681Speter !strcmp(fsp->f_fstypename, "cd9660")) { 9509336Sdfr targs.ua.fspec = NULL; 9519336Sdfr targs.ua.export.ex_flags = MNT_DELEXPORT; 9529336Sdfr if (mount(fsp->f_fstypename, fsp->f_mntonname, 9531558Srgrimes fsp->f_flags | MNT_UPDATE, 9541558Srgrimes (caddr_t)&targs) < 0) 95537663Scharnier syslog(LOG_ERR, "can't delete exports for %s", 95674462Salfred fsp->f_mntonname); 9571558Srgrimes } 9581558Srgrimes fsp++; 9591558Srgrimes } 9601558Srgrimes 9611558Srgrimes /* 9621558Srgrimes * Read in the exports file and build the list, calling 9631558Srgrimes * mount() as we go along to push the export rules into the kernel. 9641558Srgrimes */ 9651558Srgrimes if ((exp_file = fopen(exname, "r")) == NULL) { 96637663Scharnier syslog(LOG_ERR, "can't open %s", exname); 9671558Srgrimes exit(2); 9681558Srgrimes } 9691558Srgrimes dirhead = (struct dirlist *)NULL; 9701558Srgrimes while (get_line()) { 9711558Srgrimes if (debug) 97237663Scharnier warnx("got line %s", line); 9731558Srgrimes cp = line; 9741558Srgrimes nextfield(&cp, &endcp); 9751558Srgrimes if (*cp == '#') 9761558Srgrimes goto nextline; 9771558Srgrimes 9781558Srgrimes /* 9791558Srgrimes * Set defaults. 9801558Srgrimes */ 9811558Srgrimes has_host = FALSE; 9821558Srgrimes anon = def_anon; 9831558Srgrimes exflags = MNT_EXPORTED; 9841558Srgrimes got_nondir = 0; 9851558Srgrimes opt_flags = 0; 9861558Srgrimes ep = (struct exportlist *)NULL; 9871558Srgrimes 9881558Srgrimes /* 9891558Srgrimes * Create new exports list entry 9901558Srgrimes */ 9911558Srgrimes len = endcp-cp; 9921558Srgrimes tgrp = grp = get_grp(); 9931558Srgrimes while (len > 0) { 9941558Srgrimes if (len > RPCMNT_NAMELEN) { 9951558Srgrimes getexp_err(ep, tgrp); 9961558Srgrimes goto nextline; 9971558Srgrimes } 9981558Srgrimes if (*cp == '-') { 9991558Srgrimes if (ep == (struct exportlist *)NULL) { 10001558Srgrimes getexp_err(ep, tgrp); 10011558Srgrimes goto nextline; 10021558Srgrimes } 10031558Srgrimes if (debug) 100437663Scharnier warnx("doing opt %s", cp); 10051558Srgrimes got_nondir = 1; 10061558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 10071558Srgrimes &exflags, &anon)) { 10081558Srgrimes getexp_err(ep, tgrp); 10091558Srgrimes goto nextline; 10101558Srgrimes } 10111558Srgrimes } else if (*cp == '/') { 10121558Srgrimes savedc = *endcp; 10131558Srgrimes *endcp = '\0'; 10141558Srgrimes if (check_dirpath(cp) && 10151558Srgrimes statfs(cp, &fsb) >= 0) { 10161558Srgrimes if (got_nondir) { 101737663Scharnier syslog(LOG_ERR, "dirs must be first"); 10181558Srgrimes getexp_err(ep, tgrp); 10191558Srgrimes goto nextline; 10201558Srgrimes } 10211558Srgrimes if (ep) { 10221558Srgrimes if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] || 10231558Srgrimes ep->ex_fs.val[1] != fsb.f_fsid.val[1]) { 10241558Srgrimes getexp_err(ep, tgrp); 10251558Srgrimes goto nextline; 10261558Srgrimes } 10271558Srgrimes } else { 10281558Srgrimes /* 10291558Srgrimes * See if this directory is already 10301558Srgrimes * in the list. 10311558Srgrimes */ 10321558Srgrimes ep = ex_search(&fsb.f_fsid); 10331558Srgrimes if (ep == (struct exportlist *)NULL) { 10341558Srgrimes ep = get_exp(); 10351558Srgrimes ep->ex_fs = fsb.f_fsid; 10361558Srgrimes ep->ex_fsdir = (char *) 10371558Srgrimes malloc(strlen(fsb.f_mntonname) + 1); 10381558Srgrimes if (ep->ex_fsdir) 10391558Srgrimes strcpy(ep->ex_fsdir, 10401558Srgrimes fsb.f_mntonname); 10411558Srgrimes else 10421558Srgrimes out_of_mem(); 10431558Srgrimes if (debug) 104474462Salfred warnx("making new ep fs=0x%x,0x%x", 104574462Salfred fsb.f_fsid.val[0], 104674462Salfred fsb.f_fsid.val[1]); 10471558Srgrimes } else if (debug) 104837663Scharnier warnx("found ep fs=0x%x,0x%x", 10491558Srgrimes fsb.f_fsid.val[0], 10501558Srgrimes fsb.f_fsid.val[1]); 10511558Srgrimes } 10521558Srgrimes 10531558Srgrimes /* 10541558Srgrimes * Add dirpath to export mount point. 10551558Srgrimes */ 10561558Srgrimes dirp = add_expdir(&dirhead, cp, len); 10571558Srgrimes dirplen = len; 10581558Srgrimes } else { 10591558Srgrimes getexp_err(ep, tgrp); 10601558Srgrimes goto nextline; 10611558Srgrimes } 10621558Srgrimes *endcp = savedc; 10631558Srgrimes } else { 10641558Srgrimes savedc = *endcp; 10651558Srgrimes *endcp = '\0'; 10661558Srgrimes got_nondir = 1; 10671558Srgrimes if (ep == (struct exportlist *)NULL) { 10681558Srgrimes getexp_err(ep, tgrp); 10691558Srgrimes goto nextline; 10701558Srgrimes } 10711558Srgrimes 10721558Srgrimes /* 10731558Srgrimes * Get the host or netgroup. 10741558Srgrimes */ 10751558Srgrimes setnetgrent(cp); 10761558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 10771558Srgrimes do { 10781558Srgrimes if (has_host) { 10791558Srgrimes grp->gr_next = get_grp(); 10801558Srgrimes grp = grp->gr_next; 10811558Srgrimes } 10821558Srgrimes if (netgrp) { 108337003Sjoerg if (hst == 0) { 108437663Scharnier syslog(LOG_ERR, 108537663Scharnier "null hostname in netgroup %s, skipping", cp); 108637004Sjoerg grp->gr_type = GT_IGNORE; 108737003Sjoerg } else if (get_host(hst, grp, tgrp)) { 108837663Scharnier syslog(LOG_ERR, 108937663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 109029317Sjlemon grp->gr_type = GT_IGNORE; 10911558Srgrimes } 10927401Swpaul } else if (get_host(cp, grp, tgrp)) { 109337663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 109429317Sjlemon grp->gr_type = GT_IGNORE; 10951558Srgrimes } 10961558Srgrimes has_host = TRUE; 10971558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 10981558Srgrimes endnetgrent(); 10991558Srgrimes *endcp = savedc; 11001558Srgrimes } 11011558Srgrimes cp = endcp; 11021558Srgrimes nextfield(&cp, &endcp); 11031558Srgrimes len = endcp - cp; 11041558Srgrimes } 11051558Srgrimes if (check_options(dirhead)) { 11061558Srgrimes getexp_err(ep, tgrp); 11071558Srgrimes goto nextline; 11081558Srgrimes } 11091558Srgrimes if (!has_host) { 111075641Siedowse grp->gr_type = GT_DEFAULT; 11111558Srgrimes if (debug) 111237663Scharnier warnx("adding a default entry"); 11131558Srgrimes 11141558Srgrimes /* 11151558Srgrimes * Don't allow a network export coincide with a list of 11161558Srgrimes * host(s) on the same line. 11171558Srgrimes */ 11181558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 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/* 140074462Salfred * Some helper functions for netmasks. They all assume masks in network 140174462Salfred * order (big endian). 140274462Salfred */ 140374462Salfredstatic int 140474462Salfredbitcmp(void *dst, void *src, int bitlen) 140574462Salfred{ 140674462Salfred int i; 140774462Salfred u_int8_t *p1 = dst, *p2 = src; 140874462Salfred u_int8_t bitmask; 140974462Salfred int bytelen, bitsleft; 141074462Salfred 141174462Salfred bytelen = bitlen / 8; 141274462Salfred bitsleft = bitlen % 8; 141374462Salfred 141474462Salfred if (debug) { 141574462Salfred printf("comparing:\n"); 141674462Salfred for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++) 141774462Salfred printf("%02x", p1[i]); 141874462Salfred printf("\n"); 141974462Salfred for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++) 142074462Salfred printf("%02x", p2[i]); 142174462Salfred printf("\n"); 142274462Salfred } 142374462Salfred 142474462Salfred for (i = 0; i < bytelen; i++) { 142574462Salfred if (*p1 != *p2) 142674462Salfred return 1; 142774462Salfred p1++; 142874462Salfred p2++; 142974462Salfred } 143074462Salfred 143174462Salfred for (i = 0; i < bitsleft; i++) { 143274462Salfred bitmask = 1 << (7 - i); 143374462Salfred if ((*p1 & bitmask) != (*p2 & bitmask)) 143474462Salfred return 1; 143574462Salfred } 143674462Salfred 143774462Salfred return 0; 143874462Salfred} 143974462Salfred 144074462Salfred/* 14411558Srgrimes * Scan for a host match in a directory tree. 14421558Srgrimes */ 14431558Srgrimesint 14449336Sdfrchk_host(dp, saddr, defsetp, hostsetp) 14451558Srgrimes struct dirlist *dp; 144674462Salfred struct sockaddr *saddr; 14471558Srgrimes int *defsetp; 14489336Sdfr int *hostsetp; 14491558Srgrimes{ 14501558Srgrimes struct hostlist *hp; 14511558Srgrimes struct grouplist *grp; 145274462Salfred struct addrinfo *ai; 14531558Srgrimes 14541558Srgrimes if (dp) { 14551558Srgrimes if (dp->dp_flag & DP_DEFSET) 14569336Sdfr *defsetp = dp->dp_flag; 14571558Srgrimes hp = dp->dp_hosts; 14581558Srgrimes while (hp) { 14591558Srgrimes grp = hp->ht_grp; 14601558Srgrimes switch (grp->gr_type) { 14611558Srgrimes case GT_HOST: 146274462Salfred ai = grp->gr_ptr.gt_addrinfo; 146374462Salfred for (; ai; ai = ai->ai_next) { 146474462Salfred if (!sacmp(ai->ai_addr, saddr)) { 146574462Salfred *hostsetp = 146674462Salfred (hp->ht_flag | DP_HOSTSET); 146774462Salfred return (1); 146874462Salfred } 14699336Sdfr } 14701558Srgrimes break; 14711558Srgrimes case GT_NET: 147274462Salfred if (!netpartcmp(saddr, 147374462Salfred (struct sockaddr *) &grp->gr_ptr.gt_net.nt_net, 147474462Salfred grp->gr_ptr.gt_net.nt_mask)) { 147574462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 147674462Salfred return (1); 147774462Salfred } 14781558Srgrimes break; 14791558Srgrimes }; 14801558Srgrimes hp = hp->ht_next; 14811558Srgrimes } 14821558Srgrimes } 14831558Srgrimes return (0); 14841558Srgrimes} 14851558Srgrimes 14861558Srgrimes/* 14871558Srgrimes * Scan tree for a host that matches the address. 14881558Srgrimes */ 14891558Srgrimesint 14901558Srgrimesscan_tree(dp, saddr) 14911558Srgrimes struct dirlist *dp; 149274462Salfred struct sockaddr *saddr; 14931558Srgrimes{ 14949336Sdfr int defset, hostset; 14951558Srgrimes 14961558Srgrimes if (dp) { 14971558Srgrimes if (scan_tree(dp->dp_left, saddr)) 14981558Srgrimes return (1); 14999336Sdfr if (chk_host(dp, saddr, &defset, &hostset)) 15001558Srgrimes return (1); 15011558Srgrimes if (scan_tree(dp->dp_right, saddr)) 15021558Srgrimes return (1); 15031558Srgrimes } 15041558Srgrimes return (0); 15051558Srgrimes} 15061558Srgrimes 15071558Srgrimes/* 15081558Srgrimes * Traverse the dirlist tree and free it up. 15091558Srgrimes */ 15101558Srgrimesvoid 15111558Srgrimesfree_dir(dp) 15121558Srgrimes struct dirlist *dp; 15131558Srgrimes{ 15141558Srgrimes 15151558Srgrimes if (dp) { 15161558Srgrimes free_dir(dp->dp_left); 15171558Srgrimes free_dir(dp->dp_right); 15181558Srgrimes free_host(dp->dp_hosts); 15191558Srgrimes free((caddr_t)dp); 15201558Srgrimes } 15211558Srgrimes} 15221558Srgrimes 15231558Srgrimes/* 15241558Srgrimes * Parse the option string and update fields. 15251558Srgrimes * Option arguments may either be -<option>=<value> or 15261558Srgrimes * -<option> <value> 15271558Srgrimes */ 15281558Srgrimesint 15291558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr) 15301558Srgrimes char **cpp, **endcpp; 15311558Srgrimes struct exportlist *ep; 15321558Srgrimes struct grouplist *grp; 15331558Srgrimes int *has_hostp; 15341558Srgrimes int *exflagsp; 153572650Sgreen struct xucred *cr; 15361558Srgrimes{ 15371558Srgrimes char *cpoptarg, *cpoptend; 15381558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 15391558Srgrimes int allflag, usedarg; 15401558Srgrimes 154151968Salfred savedc2 = '\0'; 15421558Srgrimes cpopt = *cpp; 15431558Srgrimes cpopt++; 15441558Srgrimes cp = *endcpp; 15451558Srgrimes savedc = *cp; 15461558Srgrimes *cp = '\0'; 15471558Srgrimes while (cpopt && *cpopt) { 15481558Srgrimes allflag = 1; 15491558Srgrimes usedarg = -2; 155037663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 15511558Srgrimes *cpoptend++ = '\0'; 155237663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 15531558Srgrimes *cpoptarg++ = '\0'; 15541558Srgrimes } else { 155537663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 15561558Srgrimes *cpoptarg++ = '\0'; 15571558Srgrimes else { 15581558Srgrimes *cp = savedc; 15591558Srgrimes nextfield(&cp, &endcp); 15601558Srgrimes **endcpp = '\0'; 15611558Srgrimes if (endcp > cp && *cp != '-') { 15621558Srgrimes cpoptarg = cp; 15631558Srgrimes savedc2 = *endcp; 15641558Srgrimes *endcp = '\0'; 15651558Srgrimes usedarg = 0; 15661558Srgrimes } 15671558Srgrimes } 15681558Srgrimes } 15691558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 15701558Srgrimes *exflagsp |= MNT_EXRDONLY; 15711558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 15721558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 15731558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 15741558Srgrimes usedarg++; 15751558Srgrimes parsecred(cpoptarg, cr); 15761558Srgrimes if (allflag == 0) { 15771558Srgrimes *exflagsp |= MNT_EXPORTANON; 15781558Srgrimes opt_flags |= OP_MAPALL; 15791558Srgrimes } else 15801558Srgrimes opt_flags |= OP_MAPROOT; 15811558Srgrimes } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) { 15821558Srgrimes *exflagsp |= MNT_EXKERB; 15831558Srgrimes opt_flags |= OP_KERB; 15841558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 15851558Srgrimes !strcmp(cpopt, "m"))) { 15861558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 158737663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 15881558Srgrimes return (1); 15891558Srgrimes } 15901558Srgrimes usedarg++; 15911558Srgrimes opt_flags |= OP_MASK; 15921558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 15931558Srgrimes !strcmp(cpopt, "n"))) { 159474462Salfred if (strchr(cpoptarg, '/') != NULL) { 159574462Salfred if (debug) 159674462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 159774462Salfred opt_flags |= OP_MASKLEN; 159874462Salfred } 15991558Srgrimes if (grp->gr_type != GT_NULL) { 160037663Scharnier syslog(LOG_ERR, "network/host conflict"); 16011558Srgrimes return (1); 16021558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 160337663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 16041558Srgrimes return (1); 16051558Srgrimes } 16061558Srgrimes grp->gr_type = GT_NET; 16071558Srgrimes *has_hostp = 1; 16081558Srgrimes usedarg++; 16091558Srgrimes opt_flags |= OP_NET; 16101558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 16111558Srgrimes opt_flags |= OP_ALLDIRS; 161227447Sdfr } else if (!strcmp(cpopt, "public")) { 161327447Sdfr *exflagsp |= MNT_EXPUBLIC; 161427447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 161527447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 161627447Sdfr opt_flags |= OP_MAPALL; 161727447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 161827447Sdfr ep->ex_indexfile = strdup(cpoptarg); 16191558Srgrimes } else { 162037663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 16211558Srgrimes return (1); 16221558Srgrimes } 16231558Srgrimes if (usedarg >= 0) { 16241558Srgrimes *endcp = savedc2; 16251558Srgrimes **endcpp = savedc; 16261558Srgrimes if (usedarg > 0) { 16271558Srgrimes *cpp = cp; 16281558Srgrimes *endcpp = endcp; 16291558Srgrimes } 16301558Srgrimes return (0); 16311558Srgrimes } 16321558Srgrimes cpopt = cpoptend; 16331558Srgrimes } 16341558Srgrimes **endcpp = savedc; 16351558Srgrimes return (0); 16361558Srgrimes} 16371558Srgrimes 16381558Srgrimes/* 16391558Srgrimes * Translate a character string to the corresponding list of network 16401558Srgrimes * addresses for a hostname. 16411558Srgrimes */ 16421558Srgrimesint 16437401Swpaulget_host(cp, grp, tgrp) 16441558Srgrimes char *cp; 16451558Srgrimes struct grouplist *grp; 16467401Swpaul struct grouplist *tgrp; 16471558Srgrimes{ 16487401Swpaul struct grouplist *checkgrp; 164975635Siedowse struct addrinfo *ai, *tai, hints; 165074462Salfred int ecode; 165174462Salfred char host[NI_MAXHOST]; 16521558Srgrimes 165374462Salfred if (grp->gr_type != GT_NULL) { 165474462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 16551558Srgrimes return (1); 16561558Srgrimes } 165774462Salfred memset(&hints, 0, sizeof hints); 165874462Salfred hints.ai_flags = AI_CANONNAME; 165974462Salfred hints.ai_protocol = IPPROTO_UDP; 166074462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 166174462Salfred if (ecode != 0) { 166275635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 166374462Salfred return 1; 166474462Salfred } 166574462Salfred grp->gr_ptr.gt_addrinfo = ai; 166674462Salfred while (ai != NULL) { 166774462Salfred if (ai->ai_canonname == NULL) { 166874462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 166974462Salfred sizeof host, NULL, 0, ninumeric) != 0) 167074462Salfred strlcpy(host, "?", sizeof(host)); 167174462Salfred ai->ai_canonname = strdup(host); 167274462Salfred ai->ai_flags |= AI_CANONNAME; 167375641Siedowse } 167474462Salfred if (debug) 167575635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 167675635Siedowse /* 167775635Siedowse * Sanity check: make sure we don't already have an entry 167875635Siedowse * for this host in the grouplist. 167975635Siedowse */ 168075635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 168175635Siedowse checkgrp = checkgrp->gr_next) { 168275635Siedowse if (checkgrp->gr_type != GT_HOST) 168375635Siedowse continue; 168475635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 168575635Siedowse tai = tai->ai_next) { 168675635Siedowse if (sacmp(tai->ai_addr, ai->ai_addr) != 0) 168775635Siedowse continue; 168875635Siedowse if (debug) 168975635Siedowse fprintf(stderr, 169075635Siedowse "ignoring duplicate host %s\n", 169175635Siedowse ai->ai_canonname); 169275635Siedowse grp->gr_type = GT_IGNORE; 169375635Siedowse return (0); 169475635Siedowse } 169575635Siedowse } 169674462Salfred ai = ai->ai_next; 16971558Srgrimes } 169875635Siedowse grp->gr_type = GT_HOST; 16991558Srgrimes return (0); 17001558Srgrimes} 17011558Srgrimes 17021558Srgrimes/* 17031558Srgrimes * Free up an exports list component 17041558Srgrimes */ 17051558Srgrimesvoid 17061558Srgrimesfree_exp(ep) 17071558Srgrimes struct exportlist *ep; 17081558Srgrimes{ 17091558Srgrimes 17101558Srgrimes if (ep->ex_defdir) { 17111558Srgrimes free_host(ep->ex_defdir->dp_hosts); 17121558Srgrimes free((caddr_t)ep->ex_defdir); 17131558Srgrimes } 17141558Srgrimes if (ep->ex_fsdir) 17151558Srgrimes free(ep->ex_fsdir); 171627447Sdfr if (ep->ex_indexfile) 171727447Sdfr free(ep->ex_indexfile); 17181558Srgrimes free_dir(ep->ex_dirl); 17191558Srgrimes free((caddr_t)ep); 17201558Srgrimes} 17211558Srgrimes 17221558Srgrimes/* 17231558Srgrimes * Free hosts. 17241558Srgrimes */ 17251558Srgrimesvoid 17261558Srgrimesfree_host(hp) 17271558Srgrimes struct hostlist *hp; 17281558Srgrimes{ 17291558Srgrimes struct hostlist *hp2; 17301558Srgrimes 17311558Srgrimes while (hp) { 17321558Srgrimes hp2 = hp; 17331558Srgrimes hp = hp->ht_next; 17341558Srgrimes free((caddr_t)hp2); 17351558Srgrimes } 17361558Srgrimes} 17371558Srgrimes 17381558Srgrimesstruct hostlist * 17391558Srgrimesget_ht() 17401558Srgrimes{ 17411558Srgrimes struct hostlist *hp; 17421558Srgrimes 17431558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 17441558Srgrimes if (hp == (struct hostlist *)NULL) 17451558Srgrimes out_of_mem(); 17461558Srgrimes hp->ht_next = (struct hostlist *)NULL; 17479336Sdfr hp->ht_flag = 0; 17481558Srgrimes return (hp); 17491558Srgrimes} 17501558Srgrimes 17511558Srgrimes/* 17521558Srgrimes * Out of memory, fatal 17531558Srgrimes */ 17541558Srgrimesvoid 17551558Srgrimesout_of_mem() 17561558Srgrimes{ 17571558Srgrimes 175837663Scharnier syslog(LOG_ERR, "out of memory"); 17591558Srgrimes exit(2); 17601558Srgrimes} 17611558Srgrimes 17621558Srgrimes/* 17631558Srgrimes * Do the mount syscall with the update flag to push the export info into 17641558Srgrimes * the kernel. 17651558Srgrimes */ 17661558Srgrimesint 17671558Srgrimesdo_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb) 17681558Srgrimes struct exportlist *ep; 17691558Srgrimes struct grouplist *grp; 17701558Srgrimes int exflags; 177172650Sgreen struct xucred *anoncrp; 17721558Srgrimes char *dirp; 17731558Srgrimes int dirplen; 17741558Srgrimes struct statfs *fsb; 17751558Srgrimes{ 177674462Salfred struct sockaddr_storage ss; 177774462Salfred struct addrinfo *ai; 177874462Salfred char *cp = NULL; 17791558Srgrimes int done; 17801558Srgrimes char savedc = '\0'; 17811558Srgrimes union { 17821558Srgrimes struct ufs_args ua; 17831558Srgrimes struct iso_args ia; 17841558Srgrimes struct mfs_args ma; 17859336Sdfr#ifdef __NetBSD__ 17869336Sdfr struct msdosfs_args da; 178774462Salfred struct adosfs_args aa; 17889336Sdfr#endif 178954093Ssemenu struct ntfs_args na; 17901558Srgrimes } args; 17911558Srgrimes 17921558Srgrimes args.ua.fspec = 0; 17931558Srgrimes args.ua.export.ex_flags = exflags; 17941558Srgrimes args.ua.export.ex_anon = *anoncrp; 179527447Sdfr args.ua.export.ex_indexfile = ep->ex_indexfile; 179675641Siedowse if (grp->gr_type == GT_HOST) 179774462Salfred ai = grp->gr_ptr.gt_addrinfo; 179875641Siedowse else 179975641Siedowse ai = NULL; 18001558Srgrimes done = FALSE; 18011558Srgrimes while (!done) { 18021558Srgrimes switch (grp->gr_type) { 18031558Srgrimes case GT_HOST: 180475641Siedowse if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 180574462Salfred goto skip; 180675641Siedowse args.ua.export.ex_addr = ai->ai_addr; 180775641Siedowse args.ua.export.ex_addrlen = ai->ai_addrlen; 18081558Srgrimes args.ua.export.ex_masklen = 0; 18091558Srgrimes break; 18101558Srgrimes case GT_NET: 181174462Salfred args.ua.export.ex_addr = (struct sockaddr *) 181274462Salfred &grp->gr_ptr.gt_net.nt_net; 181374462Salfred if (args.ua.export.ex_addr->sa_family == AF_INET6 && 181474462Salfred have_v6 == 0) 181574462Salfred goto skip; 181674462Salfred args.ua.export.ex_addrlen = 181774462Salfred args.ua.export.ex_addr->sa_len; 181874462Salfred memset(&ss, 0, sizeof ss); 181974462Salfred ss.ss_family = args.ua.export.ex_addr->sa_family; 182074462Salfred ss.ss_len = args.ua.export.ex_addr->sa_len; 182174462Salfred if (allones(&ss, grp->gr_ptr.gt_net.nt_mask) != 0) { 182274462Salfred syslog(LOG_ERR, "Bad network flag"); 182374462Salfred if (cp) 182474462Salfred *cp = savedc; 182574462Salfred return (1); 18261558Srgrimes } 182774462Salfred args.ua.export.ex_mask = (struct sockaddr *)&ss; 182874462Salfred args.ua.export.ex_masklen = ss.ss_len; 18291558Srgrimes break; 183075641Siedowse case GT_DEFAULT: 183175641Siedowse args.ua.export.ex_addr = NULL; 183275641Siedowse args.ua.export.ex_addrlen = 0; 183375641Siedowse args.ua.export.ex_mask = NULL; 183475641Siedowse args.ua.export.ex_masklen = 0; 183575641Siedowse break; 18367401Swpaul case GT_IGNORE: 18377401Swpaul return(0); 18387401Swpaul break; 18391558Srgrimes default: 184037663Scharnier syslog(LOG_ERR, "bad grouptype"); 18411558Srgrimes if (cp) 18421558Srgrimes *cp = savedc; 18431558Srgrimes return (1); 18441558Srgrimes }; 18451558Srgrimes 18461558Srgrimes /* 18471558Srgrimes * XXX: 18481558Srgrimes * Maybe I should just use the fsb->f_mntonname path instead 18491558Srgrimes * of looping back up the dirp to the mount point?? 18501558Srgrimes * Also, needs to know how to export all types of local 185123681Speter * exportable file systems and not just "ufs". 18521558Srgrimes */ 18539336Sdfr while (mount(fsb->f_fstypename, dirp, 185474462Salfred fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) { 18551558Srgrimes if (cp) 18561558Srgrimes *cp-- = savedc; 18571558Srgrimes else 18581558Srgrimes cp = dirp + dirplen - 1; 18591558Srgrimes if (errno == EPERM) { 186075635Siedowse if (debug) 186175635Siedowse warnx("can't change attributes for %s", 186275635Siedowse dirp); 18631558Srgrimes syslog(LOG_ERR, 186437663Scharnier "can't change attributes for %s", dirp); 18651558Srgrimes return (1); 18661558Srgrimes } 18671558Srgrimes if (opt_flags & OP_ALLDIRS) { 186837663Scharnier syslog(LOG_ERR, "could not remount %s: %m", 18694895Swollman dirp); 18701558Srgrimes return (1); 18711558Srgrimes } 18721558Srgrimes /* back up over the last component */ 18731558Srgrimes while (*cp == '/' && cp > dirp) 18741558Srgrimes cp--; 18751558Srgrimes while (*(cp - 1) != '/' && cp > dirp) 18761558Srgrimes cp--; 18771558Srgrimes if (cp == dirp) { 18781558Srgrimes if (debug) 187937663Scharnier warnx("mnt unsucc"); 188037663Scharnier syslog(LOG_ERR, "can't export %s", dirp); 18811558Srgrimes return (1); 18821558Srgrimes } 18831558Srgrimes savedc = *cp; 18841558Srgrimes *cp = '\0'; 18851558Srgrimes } 188674462Salfredskip: 188775641Siedowse if (ai != NULL) 188874462Salfred ai = ai->ai_next; 188975641Siedowse if (ai == NULL) 18901558Srgrimes done = TRUE; 18911558Srgrimes } 18921558Srgrimes if (cp) 18931558Srgrimes *cp = savedc; 18941558Srgrimes return (0); 18951558Srgrimes} 18961558Srgrimes 18971558Srgrimes/* 18981558Srgrimes * Translate a net address. 18991558Srgrimes */ 19001558Srgrimesint 19011558Srgrimesget_net(cp, net, maskflg) 19021558Srgrimes char *cp; 19031558Srgrimes struct netmsk *net; 19041558Srgrimes int maskflg; 19051558Srgrimes{ 19061558Srgrimes struct netent *np; 190774462Salfred char *name, *p, *prefp; 190874462Salfred struct sockaddr_in sin, *sinp; 190974462Salfred struct sockaddr *sa; 191074462Salfred struct addrinfo hints, *ai = NULL; 191174462Salfred char netname[NI_MAXHOST]; 191274462Salfred long preflen; 191374462Salfred int ecode; 19141558Srgrimes 191575635Siedowse p = prefp = NULL; 191674462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 191774462Salfred p = strchr(cp, '/'); 191874462Salfred *p = '\0'; 191974462Salfred prefp = p + 1; 192074462Salfred } 192174462Salfred 192274462Salfred if ((np = getnetbyname(cp)) != NULL) { 192374462Salfred sin.sin_family = AF_INET; 192474462Salfred sin.sin_len = sizeof sin; 192574462Salfred sin.sin_addr = inet_makeaddr(np->n_net, 0); 192674462Salfred sa = (struct sockaddr *)&sin; 192774462Salfred } else if (isdigit(*cp)) { 192874462Salfred memset(&hints, 0, sizeof hints); 192974462Salfred hints.ai_family = AF_UNSPEC; 193074462Salfred hints.ai_flags = AI_NUMERICHOST; 193174462Salfred if (getaddrinfo(cp, NULL, &hints, &ai) != 0) { 193274462Salfred /* 193374462Salfred * If getaddrinfo() failed, try the inet4 network 193474462Salfred * notation with less than 3 dots. 193574462Salfred */ 193674462Salfred sin.sin_family = AF_INET; 193774462Salfred sin.sin_len = sizeof sin; 193874462Salfred sin.sin_addr = inet_makeaddr(inet_network(cp),0); 193974462Salfred if (debug) 194074462Salfred fprintf(stderr, "get_net: v4 addr %x\n", 194174462Salfred sin.sin_addr.s_addr); 194274462Salfred sa = (struct sockaddr *)&sin; 194374462Salfred } else 194474462Salfred sa = ai->ai_addr; 194574462Salfred } else if (isxdigit(*cp) || *cp == ':') { 194674462Salfred memset(&hints, 0, sizeof hints); 194774462Salfred hints.ai_family = AF_UNSPEC; 194874462Salfred hints.ai_flags = AI_NUMERICHOST; 194974462Salfred if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 195074462Salfred sa = ai->ai_addr; 195174462Salfred else 195274462Salfred goto fail; 19531558Srgrimes } else 195474462Salfred goto fail; 195525318Spst 195674462Salfred ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname, 195774462Salfred NULL, 0, ninumeric); 195874462Salfred if (ecode != 0) 195974462Salfred goto fail; 196074462Salfred 19611558Srgrimes if (maskflg) 196274462Salfred net->nt_mask = countones(sa); 19631558Srgrimes else { 196474462Salfred if (opt_flags & OP_MASKLEN) { 196574462Salfred preflen = strtol(prefp, NULL, 10); 196674462Salfred if (preflen == LONG_MIN && errno == ERANGE) 196774462Salfred goto fail; 196874462Salfred net->nt_mask = (int)preflen; 196974462Salfred *p = '/'; 197074462Salfred } 197174462Salfred 19721558Srgrimes if (np) 19731558Srgrimes name = np->n_name; 197474462Salfred else { 197574462Salfred if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 197674462Salfred NULL, 0, ninumeric) != 0) 197774462Salfred strlcpy(netname, "?", sizeof(netname)); 197874462Salfred name = netname; 197974462Salfred } 198074462Salfred net->nt_name = strdup(name); 198174462Salfred memcpy(&net->nt_net, sa, sa->sa_len); 198274462Salfred } 198374462Salfred 198474462Salfred if (!maskflg && sa->sa_family == AF_INET && 198574462Salfred !(opt_flags & (OP_MASK|OP_MASKLEN))) { 198674462Salfred sinp = (struct sockaddr_in *)sa; 198774462Salfred if (IN_CLASSA(sinp->sin_addr.s_addr)) 198874462Salfred net->nt_mask = 8; 198974462Salfred else if (IN_CLASSB(sinp->sin_addr.s_addr)) 199074462Salfred net->nt_mask = 16; 199174462Salfred else if (IN_CLASSC(sinp->sin_addr.s_addr)) 199274462Salfred net->nt_mask = 24; 199374462Salfred else if (IN_CLASSD(sinp->sin_addr.s_addr)) 199474462Salfred net->nt_mask = 28; 19951558Srgrimes else 199674462Salfred net->nt_mask = 32; /* XXX */ 19971558Srgrimes } 199874462Salfred 199974462Salfred if (ai) 200074462Salfred freeaddrinfo(ai); 200174462Salfred return 0; 200274462Salfred 200374462Salfredfail: 200474462Salfred if (ai) 200574462Salfred freeaddrinfo(ai); 200674462Salfred return 1; 20071558Srgrimes} 20081558Srgrimes 20091558Srgrimes/* 20101558Srgrimes * Parse out the next white space separated field 20111558Srgrimes */ 20121558Srgrimesvoid 20131558Srgrimesnextfield(cp, endcp) 20141558Srgrimes char **cp; 20151558Srgrimes char **endcp; 20161558Srgrimes{ 20171558Srgrimes char *p; 20181558Srgrimes 20191558Srgrimes p = *cp; 20201558Srgrimes while (*p == ' ' || *p == '\t') 20211558Srgrimes p++; 20221558Srgrimes if (*p == '\n' || *p == '\0') 20231558Srgrimes *cp = *endcp = p; 20241558Srgrimes else { 20251558Srgrimes *cp = p++; 20261558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 20271558Srgrimes p++; 20281558Srgrimes *endcp = p; 20291558Srgrimes } 20301558Srgrimes} 20311558Srgrimes 20321558Srgrimes/* 20331558Srgrimes * Get an exports file line. Skip over blank lines and handle line 20341558Srgrimes * continuations. 20351558Srgrimes */ 20361558Srgrimesint 20371558Srgrimesget_line() 20381558Srgrimes{ 20391558Srgrimes char *p, *cp; 20401558Srgrimes int len; 20411558Srgrimes int totlen, cont_line; 20421558Srgrimes 20431558Srgrimes /* 20441558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 20451558Srgrimes */ 20461558Srgrimes p = line; 20471558Srgrimes totlen = 0; 20481558Srgrimes do { 20491558Srgrimes if (fgets(p, LINESIZ - totlen, exp_file) == NULL) 20501558Srgrimes return (0); 20511558Srgrimes len = strlen(p); 20521558Srgrimes cp = p + len - 1; 20531558Srgrimes cont_line = 0; 20541558Srgrimes while (cp >= p && 20551558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 20561558Srgrimes if (*cp == '\\') 20571558Srgrimes cont_line = 1; 20581558Srgrimes cp--; 20591558Srgrimes len--; 20601558Srgrimes } 20611558Srgrimes *++cp = '\0'; 20621558Srgrimes if (len > 0) { 20631558Srgrimes totlen += len; 20641558Srgrimes if (totlen >= LINESIZ) { 206537663Scharnier syslog(LOG_ERR, "exports line too long"); 20661558Srgrimes exit(2); 20671558Srgrimes } 20681558Srgrimes p = cp; 20691558Srgrimes } 20701558Srgrimes } while (totlen == 0 || cont_line); 20711558Srgrimes return (1); 20721558Srgrimes} 20731558Srgrimes 20741558Srgrimes/* 20751558Srgrimes * Parse a description of a credential. 20761558Srgrimes */ 20771558Srgrimesvoid 20781558Srgrimesparsecred(namelist, cr) 20791558Srgrimes char *namelist; 208072650Sgreen struct xucred *cr; 20811558Srgrimes{ 20821558Srgrimes char *name; 20831558Srgrimes int cnt; 20841558Srgrimes char *names; 20851558Srgrimes struct passwd *pw; 20861558Srgrimes struct group *gr; 20871558Srgrimes int ngroups, groups[NGROUPS + 1]; 20881558Srgrimes 20891558Srgrimes /* 209037663Scharnier * Set up the unprivileged user. 20911558Srgrimes */ 20921558Srgrimes cr->cr_uid = -2; 20931558Srgrimes cr->cr_groups[0] = -2; 20941558Srgrimes cr->cr_ngroups = 1; 20951558Srgrimes /* 20961558Srgrimes * Get the user's password table entry. 20971558Srgrimes */ 20981558Srgrimes names = strsep(&namelist, " \t\n"); 20991558Srgrimes name = strsep(&names, ":"); 21001558Srgrimes if (isdigit(*name) || *name == '-') 21011558Srgrimes pw = getpwuid(atoi(name)); 21021558Srgrimes else 21031558Srgrimes pw = getpwnam(name); 21041558Srgrimes /* 21051558Srgrimes * Credentials specified as those of a user. 21061558Srgrimes */ 21071558Srgrimes if (names == NULL) { 21081558Srgrimes if (pw == NULL) { 210937663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 21101558Srgrimes return; 21111558Srgrimes } 21121558Srgrimes cr->cr_uid = pw->pw_uid; 21131558Srgrimes ngroups = NGROUPS + 1; 21141558Srgrimes if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 211537663Scharnier syslog(LOG_ERR, "too many groups"); 21161558Srgrimes /* 21171558Srgrimes * Convert from int's to gid_t's and compress out duplicate 21181558Srgrimes */ 21191558Srgrimes cr->cr_ngroups = ngroups - 1; 21201558Srgrimes cr->cr_groups[0] = groups[0]; 21211558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 21221558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 21231558Srgrimes return; 21241558Srgrimes } 21251558Srgrimes /* 21261558Srgrimes * Explicit credential specified as a colon separated list: 21271558Srgrimes * uid:gid:gid:... 21281558Srgrimes */ 21291558Srgrimes if (pw != NULL) 21301558Srgrimes cr->cr_uid = pw->pw_uid; 21311558Srgrimes else if (isdigit(*name) || *name == '-') 21321558Srgrimes cr->cr_uid = atoi(name); 21331558Srgrimes else { 213437663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 21351558Srgrimes return; 21361558Srgrimes } 21371558Srgrimes cr->cr_ngroups = 0; 21381558Srgrimes while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) { 21391558Srgrimes name = strsep(&names, ":"); 21401558Srgrimes if (isdigit(*name) || *name == '-') { 21411558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 21421558Srgrimes } else { 21431558Srgrimes if ((gr = getgrnam(name)) == NULL) { 214437663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 21451558Srgrimes continue; 21461558Srgrimes } 21471558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 21481558Srgrimes } 21491558Srgrimes } 21501558Srgrimes if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS) 215137663Scharnier syslog(LOG_ERR, "too many groups"); 21521558Srgrimes} 21531558Srgrimes 21541558Srgrimes#define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50) 21551558Srgrimes/* 21561558Srgrimes * Routines that maintain the remote mounttab 21571558Srgrimes */ 21581558Srgrimesvoid 21591558Srgrimesget_mountlist() 21601558Srgrimes{ 21611558Srgrimes struct mountlist *mlp, **mlpp; 216223681Speter char *host, *dirp, *cp; 21631558Srgrimes char str[STRSIZ]; 21641558Srgrimes FILE *mlfile; 21651558Srgrimes 21661558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 216753117Sbillf if (errno == ENOENT) 216853117Sbillf return; 216953117Sbillf else { 217053117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 217153117Sbillf return; 217253117Sbillf } 21731558Srgrimes } 21741558Srgrimes mlpp = &mlhead; 21751558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 217623681Speter cp = str; 217723681Speter host = strsep(&cp, " \t\n"); 217823681Speter dirp = strsep(&cp, " \t\n"); 217923681Speter if (host == NULL || dirp == NULL) 21801558Srgrimes continue; 21811558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 218237663Scharnier if (mlp == (struct mountlist *)NULL) 218337663Scharnier out_of_mem(); 218423681Speter strncpy(mlp->ml_host, host, RPCMNT_NAMELEN); 218523681Speter mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 218623681Speter strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 218723681Speter mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 21881558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 21891558Srgrimes *mlpp = mlp; 21901558Srgrimes mlpp = &mlp->ml_next; 21911558Srgrimes } 21921558Srgrimes fclose(mlfile); 21931558Srgrimes} 21941558Srgrimes 219575635Siedowsevoid 219675635Siedowsedel_mlist(char *hostp, char *dirp) 21971558Srgrimes{ 21981558Srgrimes struct mountlist *mlp, **mlpp; 21991558Srgrimes struct mountlist *mlp2; 22001558Srgrimes FILE *mlfile; 22011558Srgrimes int fnd = 0; 22021558Srgrimes 22031558Srgrimes mlpp = &mlhead; 22041558Srgrimes mlp = mlhead; 22051558Srgrimes while (mlp) { 22061558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 22071558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 22081558Srgrimes fnd = 1; 22091558Srgrimes mlp2 = mlp; 22101558Srgrimes *mlpp = mlp = mlp->ml_next; 22111558Srgrimes free((caddr_t)mlp2); 22121558Srgrimes } else { 22131558Srgrimes mlpp = &mlp->ml_next; 22141558Srgrimes mlp = mlp->ml_next; 22151558Srgrimes } 22161558Srgrimes } 22171558Srgrimes if (fnd) { 22181558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 221937663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 22201558Srgrimes return; 22211558Srgrimes } 22221558Srgrimes mlp = mlhead; 22231558Srgrimes while (mlp) { 22241558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 22251558Srgrimes mlp = mlp->ml_next; 22261558Srgrimes } 22271558Srgrimes fclose(mlfile); 22281558Srgrimes } 22291558Srgrimes} 22301558Srgrimes 22311558Srgrimesvoid 22321558Srgrimesadd_mlist(hostp, dirp) 22331558Srgrimes char *hostp, *dirp; 22341558Srgrimes{ 22351558Srgrimes struct mountlist *mlp, **mlpp; 22361558Srgrimes FILE *mlfile; 22371558Srgrimes 22381558Srgrimes mlpp = &mlhead; 22391558Srgrimes mlp = mlhead; 22401558Srgrimes while (mlp) { 22411558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 22421558Srgrimes return; 22431558Srgrimes mlpp = &mlp->ml_next; 22441558Srgrimes mlp = mlp->ml_next; 22451558Srgrimes } 22461558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 224737663Scharnier if (mlp == (struct mountlist *)NULL) 224837663Scharnier out_of_mem(); 22491558Srgrimes strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN); 22501558Srgrimes mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 22511558Srgrimes strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 22521558Srgrimes mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 22531558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 22541558Srgrimes *mlpp = mlp; 22551558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 225637663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 22571558Srgrimes return; 22581558Srgrimes } 22591558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 22601558Srgrimes fclose(mlfile); 22611558Srgrimes} 22621558Srgrimes 22631558Srgrimes/* 22641558Srgrimes * Free up a group list. 22651558Srgrimes */ 22661558Srgrimesvoid 22671558Srgrimesfree_grp(grp) 22681558Srgrimes struct grouplist *grp; 22691558Srgrimes{ 22701558Srgrimes if (grp->gr_type == GT_HOST) { 227174462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 227274462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 22731558Srgrimes } else if (grp->gr_type == GT_NET) { 22741558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 22751558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 22761558Srgrimes } 22771558Srgrimes free((caddr_t)grp); 22781558Srgrimes} 22791558Srgrimes 22801558Srgrimes#ifdef DEBUG 22811558Srgrimesvoid 22821558SrgrimesSYSLOG(int pri, const char *fmt, ...) 22831558Srgrimes{ 22841558Srgrimes va_list ap; 22851558Srgrimes 22861558Srgrimes va_start(ap, fmt); 22871558Srgrimes vfprintf(stderr, fmt, ap); 22881558Srgrimes va_end(ap); 22891558Srgrimes} 22901558Srgrimes#endif /* DEBUG */ 22911558Srgrimes 22921558Srgrimes/* 22931558Srgrimes * Check options for consistency. 22941558Srgrimes */ 22951558Srgrimesint 22961558Srgrimescheck_options(dp) 22971558Srgrimes struct dirlist *dp; 22981558Srgrimes{ 22991558Srgrimes 23001558Srgrimes if (dp == (struct dirlist *)NULL) 23011558Srgrimes return (1); 23021558Srgrimes if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) || 23031558Srgrimes (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) || 23041558Srgrimes (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) { 23051558Srgrimes syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive"); 23061558Srgrimes return (1); 23071558Srgrimes } 23081558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 23091558Srgrimes syslog(LOG_ERR, "-mask requires -net"); 23101558Srgrimes return (1); 23111558Srgrimes } 23121558Srgrimes if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 231345927Salex syslog(LOG_ERR, "-alldirs has multiple directories"); 23141558Srgrimes return (1); 23151558Srgrimes } 23161558Srgrimes return (0); 23171558Srgrimes} 23181558Srgrimes 23191558Srgrimes/* 23201558Srgrimes * Check an absolute directory path for any symbolic links. Return true 23211558Srgrimes */ 23221558Srgrimesint 23231558Srgrimescheck_dirpath(dirp) 23241558Srgrimes char *dirp; 23251558Srgrimes{ 23261558Srgrimes char *cp; 23271558Srgrimes int ret = 1; 23281558Srgrimes struct stat sb; 23291558Srgrimes 23301558Srgrimes cp = dirp + 1; 23311558Srgrimes while (*cp && ret) { 23321558Srgrimes if (*cp == '/') { 23331558Srgrimes *cp = '\0'; 23349336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23351558Srgrimes ret = 0; 23361558Srgrimes *cp = '/'; 23371558Srgrimes } 23381558Srgrimes cp++; 23391558Srgrimes } 23409336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 23411558Srgrimes ret = 0; 23421558Srgrimes return (ret); 23431558Srgrimes} 23449336Sdfr 234574462Salfredstatic int 234674462Salfrednetpartcmp(struct sockaddr *s1, struct sockaddr *s2, int bitlen) 234774462Salfred{ 234874462Salfred void *src, *dst; 234974462Salfred 235074462Salfred if (s1->sa_family != s2->sa_family) 235174462Salfred return 1; 235274462Salfred 235374462Salfred switch (s1->sa_family) { 235474462Salfred case AF_INET: 235574462Salfred src = &((struct sockaddr_in *)s1)->sin_addr; 235674462Salfred dst = &((struct sockaddr_in *)s2)->sin_addr; 235774462Salfred if (bitlen > sizeof(((struct sockaddr_in *)s1)->sin_addr) * 8) 235874462Salfred return 1; 235974462Salfred break; 236074462Salfred case AF_INET6: 236174462Salfred src = &((struct sockaddr_in6 *)s1)->sin6_addr; 236274462Salfred dst = &((struct sockaddr_in6 *)s2)->sin6_addr; 236374462Salfred if (((struct sockaddr_in6 *)s1)->sin6_scope_id != 236474462Salfred ((struct sockaddr_in6 *)s2)->sin6_scope_id) 236574462Salfred return 1; 236674462Salfred if (bitlen > sizeof(((struct sockaddr_in6 *)s1)->sin6_addr) * 8) 236774462Salfred return 1; 236874462Salfred break; 236974462Salfred default: 237074462Salfred return 1; 237174462Salfred } 237274462Salfred 237374462Salfred return bitcmp(src, dst, bitlen); 237474462Salfred} 237574462Salfred 237674462Salfredstatic int 237774462Salfredallones(struct sockaddr_storage *ssp, int bitlen) 237874462Salfred{ 237974462Salfred u_int8_t *p; 238074462Salfred int bytelen, bitsleft, i; 238174462Salfred int zerolen; 238274462Salfred 238374462Salfred switch (ssp->ss_family) { 238474462Salfred case AF_INET: 238574462Salfred p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr; 238674462Salfred zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr); 238774462Salfred break; 238874462Salfred case AF_INET6: 238974462Salfred p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr; 239074462Salfred zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr); 239174462Salfred break; 239274462Salfred default: 239374462Salfred return -1; 239474462Salfred } 239574462Salfred 239674462Salfred memset(p, 0, zerolen); 239774462Salfred 239874462Salfred bytelen = bitlen / 8; 239974462Salfred bitsleft = bitlen % 8; 240074462Salfred 240174462Salfred if (bytelen > zerolen) 240274462Salfred return -1; 240374462Salfred 240474462Salfred for (i = 0; i < bytelen; i++) 240574462Salfred *p++ = 0xff; 240674462Salfred 240774462Salfred for (i = 0; i < bitsleft; i++) 240874462Salfred *p |= 1 << (7 - i); 240974462Salfred 241074462Salfred return 0; 241174462Salfred} 241274462Salfred 241374462Salfredstatic int 241474462Salfredcountones(struct sockaddr *sa) 241574462Salfred{ 241674462Salfred void *mask; 241774462Salfred int i, bits = 0, bytelen; 241874462Salfred u_int8_t *p; 241974462Salfred 242074462Salfred switch (sa->sa_family) { 242174462Salfred case AF_INET: 242274462Salfred mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr; 242374462Salfred bytelen = 4; 242474462Salfred break; 242574462Salfred case AF_INET6: 242674462Salfred mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr; 242774462Salfred bytelen = 16; 242874462Salfred break; 242974462Salfred default: 243074462Salfred return 0; 243174462Salfred } 243274462Salfred 243374462Salfred p = mask; 243474462Salfred 243574462Salfred for (i = 0; i < bytelen; i++, p++) { 243674462Salfred if (*p != 0xff) { 243774462Salfred for (bits = 0; bits < 8; bits++) { 243874462Salfred if (!(*p & (1 << (7 - bits)))) 243974462Salfred break; 244074462Salfred } 244174462Salfred break; 244274462Salfred } 244374462Salfred } 244474462Salfred 244574462Salfred return (i * 8 + bits); 244674462Salfred} 244774462Salfred 244874462Salfredstatic int 244974462Salfredsacmp(struct sockaddr *sa1, struct sockaddr *sa2) 245074462Salfred{ 245174462Salfred void *p1, *p2; 245274462Salfred int len; 245374462Salfred 245474462Salfred if (sa1->sa_family != sa2->sa_family) 245574462Salfred return 1; 245674462Salfred 245774462Salfred switch (sa1->sa_family) { 245874462Salfred case AF_INET: 245974462Salfred p1 = &((struct sockaddr_in *)sa1)->sin_addr; 246074462Salfred p2 = &((struct sockaddr_in *)sa2)->sin_addr; 246174462Salfred len = 4; 246274462Salfred break; 246374462Salfred case AF_INET6: 246474462Salfred p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr; 246574462Salfred p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr; 246674462Salfred len = 16; 246774462Salfred if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 246874462Salfred ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 246974462Salfred return 1; 247074462Salfred break; 247174462Salfred default: 247274462Salfred return 1; 247374462Salfred } 247474462Salfred 247574462Salfred return memcmp(p1, p2, len); 247674462Salfred} 247774462Salfred 247875754Siedowsevoid 247975754Siedowsehuphandler(int sig) 248075754Siedowse{ 248175754Siedowse got_sighup = 1; 248275754Siedowse} 248375754Siedowse 248474462Salfredvoid terminate(sig) 248574462Salfredint sig; 248674462Salfred{ 248774462Salfred close(mountdlockfd); 248874462Salfred unlink(MOUNTDLOCK); 248974792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 249074792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 249174462Salfred exit (0); 249274462Salfred} 2493