mountd.c revision 164394
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 * 4. Neither the name of the University nor the names of its contributors 171558Srgrimes * may be used to endorse or promote products derived from this software 181558Srgrimes * without specific prior written permission. 191558Srgrimes * 201558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301558Srgrimes * SUCH DAMAGE. 311558Srgrimes */ 321558Srgrimes 331558Srgrimes#ifndef lint 3437663Scharnierstatic const char copyright[] = 351558Srgrimes"@(#) Copyright (c) 1989, 1993\n\ 361558Srgrimes The Regents of the University of California. All rights reserved.\n"; 372999Swollman#endif /*not lint*/ 381558Srgrimes 39105267Scharnier#if 0 401558Srgrimes#ifndef lint 4137663Scharnierstatic char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95"; 42105267Scharnier#endif /*not lint*/ 4337663Scharnier#endif 441558Srgrimes 45105267Scharnier#include <sys/cdefs.h> 46105267Scharnier__FBSDID("$FreeBSD: head/usr.sbin/mountd/mountd.c 164394 2006-11-18 22:43:20Z rodrigc $"); 47105267Scharnier 481558Srgrimes#include <sys/param.h> 491558Srgrimes#include <sys/mount.h> 5074462Salfred#include <sys/fcntl.h> 511558Srgrimes#include <sys/stat.h> 521558Srgrimes#include <sys/syslog.h> 5324330Sguido#include <sys/sysctl.h> 5496622Siedowse#include <sys/linker.h> 5596622Siedowse#include <sys/module.h> 561558Srgrimes 571558Srgrimes#include <rpc/rpc.h> 58109363Smbr#include <rpc/rpc_com.h> 591558Srgrimes#include <rpc/pmap_clnt.h> 6074462Salfred#include <rpc/pmap_prot.h> 6174462Salfred#include <rpcsvc/mount.h> 621558Srgrimes#include <nfs/rpcv2.h> 639336Sdfr#include <nfs/nfsproto.h> 6483653Speter#include <nfsserver/nfs.h> 651558Srgrimes 661558Srgrimes#include <arpa/inet.h> 671558Srgrimes 681558Srgrimes#include <ctype.h> 6937663Scharnier#include <err.h> 701558Srgrimes#include <errno.h> 711558Srgrimes#include <grp.h> 72149433Spjd#include <libutil.h> 73103949Smike#include <limits.h> 741558Srgrimes#include <netdb.h> 751558Srgrimes#include <pwd.h> 761558Srgrimes#include <signal.h> 771558Srgrimes#include <stdio.h> 781558Srgrimes#include <stdlib.h> 791558Srgrimes#include <string.h> 801558Srgrimes#include <unistd.h> 811558Srgrimes#include "pathnames.h" 82158857Srodrigc#include "mntopts.h" 831558Srgrimes 841558Srgrimes#ifdef DEBUG 851558Srgrimes#include <stdarg.h> 861558Srgrimes#endif 871558Srgrimes 881558Srgrimes/* 891558Srgrimes * Structures for keeping the mount list and export list 901558Srgrimes */ 911558Srgrimesstruct mountlist { 921558Srgrimes struct mountlist *ml_next; 931558Srgrimes char ml_host[RPCMNT_NAMELEN+1]; 941558Srgrimes char ml_dirp[RPCMNT_PATHLEN+1]; 951558Srgrimes}; 961558Srgrimes 971558Srgrimesstruct dirlist { 981558Srgrimes struct dirlist *dp_left; 991558Srgrimes struct dirlist *dp_right; 1001558Srgrimes int dp_flag; 1011558Srgrimes struct hostlist *dp_hosts; /* List of hosts this dir exported to */ 1021558Srgrimes char dp_dirp[1]; /* Actually malloc'd to size of dir */ 1031558Srgrimes}; 1041558Srgrimes/* dp_flag bits */ 1051558Srgrimes#define DP_DEFSET 0x1 1069336Sdfr#define DP_HOSTSET 0x2 1071558Srgrimes 1081558Srgrimesstruct exportlist { 1091558Srgrimes struct exportlist *ex_next; 1101558Srgrimes struct dirlist *ex_dirl; 1111558Srgrimes struct dirlist *ex_defdir; 1121558Srgrimes int ex_flag; 1131558Srgrimes fsid_t ex_fs; 1141558Srgrimes char *ex_fsdir; 11527447Sdfr char *ex_indexfile; 1161558Srgrimes}; 1171558Srgrimes/* ex_flag bits */ 1181558Srgrimes#define EX_LINKED 0x1 1191558Srgrimes 1201558Srgrimesstruct netmsk { 12174462Salfred struct sockaddr_storage nt_net; 12275801Siedowse struct sockaddr_storage nt_mask; 12342144Sdfr char *nt_name; 1241558Srgrimes}; 1251558Srgrimes 1261558Srgrimesunion grouptypes { 12774462Salfred struct addrinfo *gt_addrinfo; 1281558Srgrimes struct netmsk gt_net; 1291558Srgrimes}; 1301558Srgrimes 1311558Srgrimesstruct grouplist { 1321558Srgrimes int gr_type; 1331558Srgrimes union grouptypes gr_ptr; 1341558Srgrimes struct grouplist *gr_next; 1351558Srgrimes}; 1361558Srgrimes/* Group types */ 1371558Srgrimes#define GT_NULL 0x0 1381558Srgrimes#define GT_HOST 0x1 1391558Srgrimes#define GT_NET 0x2 14075641Siedowse#define GT_DEFAULT 0x3 1417401Swpaul#define GT_IGNORE 0x5 1421558Srgrimes 1431558Srgrimesstruct hostlist { 1449336Sdfr int ht_flag; /* Uses DP_xx bits */ 1451558Srgrimes struct grouplist *ht_grp; 1461558Srgrimes struct hostlist *ht_next; 1471558Srgrimes}; 1481558Srgrimes 1499336Sdfrstruct fhreturn { 1509336Sdfr int fhr_flag; 1519336Sdfr int fhr_vers; 1529336Sdfr nfsfh_t fhr_fh; 1539336Sdfr}; 1549336Sdfr 1551558Srgrimes/* Global defs */ 15692882Simpchar *add_expdir(struct dirlist **, char *, int); 15792882Simpvoid add_dlist(struct dirlist **, struct dirlist *, 15892882Simp struct grouplist *, int); 15992882Simpvoid add_mlist(char *, char *); 16092882Simpint check_dirpath(char *); 16192882Simpint check_options(struct dirlist *); 16275801Siedowseint checkmask(struct sockaddr *sa); 16392882Simpint chk_host(struct dirlist *, struct sockaddr *, int *, int *); 16475635Siedowsevoid del_mlist(char *hostp, char *dirp); 16592882Simpstruct dirlist *dirp_search(struct dirlist *, char *); 16692882Simpint do_mount(struct exportlist *, struct grouplist *, int, 16792882Simp struct xucred *, char *, int, struct statfs *); 16892882Simpint do_opt(char **, char **, struct exportlist *, struct grouplist *, 16992882Simp int *, int *, struct xucred *); 17092882Simpstruct exportlist *ex_search(fsid_t *); 17192882Simpstruct exportlist *get_exp(void); 17292882Simpvoid free_dir(struct dirlist *); 17392882Simpvoid free_exp(struct exportlist *); 17492882Simpvoid free_grp(struct grouplist *); 17592882Simpvoid free_host(struct hostlist *); 17692882Simpvoid get_exportlist(void); 17792882Simpint get_host(char *, struct grouplist *, struct grouplist *); 17892882Simpstruct hostlist *get_ht(void); 17992882Simpint get_line(void); 18092882Simpvoid get_mountlist(void); 18192882Simpint get_net(char *, struct netmsk *, int); 18292882Simpvoid getexp_err(struct exportlist *, struct grouplist *); 18392882Simpstruct grouplist *get_grp(void); 18492882Simpvoid hang_dirp(struct dirlist *, struct grouplist *, 18592882Simp struct exportlist *, int); 18675754Siedowsevoid huphandler(int sig); 18775801Siedowseint makemask(struct sockaddr_storage *ssp, int bitlen); 18892882Simpvoid mntsrv(struct svc_req *, SVCXPRT *); 18992882Simpvoid nextfield(char **, char **); 19092882Simpvoid out_of_mem(void); 19192882Simpvoid parsecred(char *, struct xucred *); 192100117Salfredint put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int); 19375801Siedowsevoid *sa_rawaddr(struct sockaddr *sa, int *nbytes); 19475801Siedowseint sacmp(struct sockaddr *sa1, struct sockaddr *sa2, 19575801Siedowse struct sockaddr *samask); 19692882Simpint scan_tree(struct dirlist *, struct sockaddr *); 19792882Simpstatic void usage(void); 19892882Simpint xdr_dir(XDR *, char *); 19992882Simpint xdr_explist(XDR *, caddr_t); 200100117Salfredint xdr_explist_brief(XDR *, caddr_t); 20192882Simpint xdr_fhs(XDR *, caddr_t); 20292882Simpint xdr_mlist(XDR *, caddr_t); 20392882Simpvoid terminate(int); 2041558Srgrimes 2051558Srgrimesstruct exportlist *exphead; 2061558Srgrimesstruct mountlist *mlhead; 2071558Srgrimesstruct grouplist *grphead; 2081558Srgrimeschar exname[MAXPATHLEN]; 20972650Sgreenstruct xucred def_anon = { 21091354Sdd XUCRED_VERSION, 21172650Sgreen (uid_t)-2, 2121558Srgrimes 1, 21372650Sgreen { (gid_t)-2 }, 21472650Sgreen NULL 2151558Srgrimes}; 21625087Sdfrint force_v2 = 0; 2179336Sdfrint resvport_only = 1; 2189336Sdfrint dir_only = 1; 219121767Speterint dolog = 0; 22075754Siedowseint got_sighup = 0; 22174462Salfred 2221558Srgrimesint opt_flags; 22374462Salfredstatic int have_v6 = 1; 22474462Salfred 225149433Spjdstruct pidfh *pfh = NULL; 22675801Siedowse/* Bits for opt_flags above */ 2271558Srgrimes#define OP_MAPROOT 0x01 2281558Srgrimes#define OP_MAPALL 0x02 22983653Speter/* 0x4 free */ 2301558Srgrimes#define OP_MASK 0x08 2311558Srgrimes#define OP_NET 0x10 2321558Srgrimes#define OP_ALLDIRS 0x40 23375801Siedowse#define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */ 234100336Sjoerg#define OP_QUIET 0x100 23574462Salfred#define OP_MASKLEN 0x200 2361558Srgrimes 2371558Srgrimes#ifdef DEBUG 2381558Srgrimesint debug = 1; 23992882Simpvoid SYSLOG(int, const char *, ...) __printflike(2, 3); 2401558Srgrimes#define syslog SYSLOG 2411558Srgrimes#else 2421558Srgrimesint debug = 0; 2431558Srgrimes#endif 2441558Srgrimes 2451558Srgrimes/* 2461558Srgrimes * Mountd server for NFS mount protocol as described in: 2471558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A 2481558Srgrimes * The optional arguments are the exports file name 2491558Srgrimes * default: _PATH_EXPORTS 2501558Srgrimes * and "-n" to allow nonroot mount. 2511558Srgrimes */ 2521558Srgrimesint 2531558Srgrimesmain(argc, argv) 2541558Srgrimes int argc; 2551558Srgrimes char **argv; 2561558Srgrimes{ 25775754Siedowse fd_set readfds; 258126572Sbms struct sockaddr_in sin; 259126572Sbms struct sockaddr_in6 sin6; 260126572Sbms char *endptr; 26174462Salfred SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp; 26274462Salfred struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf; 263149433Spjd pid_t otherpid; 26474462Salfred int udpsock, tcpsock, udp6sock, tcp6sock; 26574462Salfred int xcreated = 0, s; 266109363Smbr int maxrec = RPC_MAXDATASIZE; 26774462Salfred int one = 1; 268126572Sbms int c, r; 269126572Sbms in_port_t svcport = 0; 2701558Srgrimes 27175635Siedowse udp6conf = tcp6conf = NULL; 272126643Smarkm udp6sock = tcp6sock = 0; 27375635Siedowse 27474462Salfred /* Check that another mountd isn't already running. */ 275150214Spjd pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid); 276149433Spjd if (pfh == NULL) { 277149433Spjd if (errno == EEXIST) 278149433Spjd errx(1, "mountd already running, pid: %d.", otherpid); 279149433Spjd warn("cannot open or create pidfile"); 280149433Spjd } 28174462Salfred 28274462Salfred s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 28374462Salfred if (s < 0) 28474462Salfred have_v6 = 0; 28574462Salfred else 28674462Salfred close(s); 28783687Speter if (modfind("nfsserver") < 0) { 28883687Speter /* Not present in kernel, try loading it */ 28983687Speter if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0) 29083687Speter errx(1, "NFS server is not available or loadable"); 2912999Swollman } 2922999Swollman 293126572Sbms while ((c = getopt(argc, argv, "2dlnp:r")) != -1) 2941558Srgrimes switch (c) { 29525087Sdfr case '2': 29625087Sdfr force_v2 = 1; 29725087Sdfr break; 2989336Sdfr case 'n': 2999336Sdfr resvport_only = 0; 3009336Sdfr break; 3019336Sdfr case 'r': 3029336Sdfr dir_only = 0; 3039336Sdfr break; 3048688Sphk case 'd': 3058688Sphk debug = debug ? 0 : 1; 3068688Sphk break; 30731656Sguido case 'l': 308121767Speter dolog = 1; 30931656Sguido break; 310126572Sbms case 'p': 311126572Sbms endptr = NULL; 312126572Sbms svcport = (in_port_t)strtoul(optarg, &endptr, 10); 313126572Sbms if (endptr == NULL || *endptr != '\0' || 314126572Sbms svcport == 0 || svcport >= IPPORT_MAX) 315126572Sbms usage(); 316126572Sbms break; 3171558Srgrimes default: 31837663Scharnier usage(); 3191558Srgrimes }; 3201558Srgrimes argc -= optind; 3211558Srgrimes argv += optind; 3221558Srgrimes grphead = (struct grouplist *)NULL; 3231558Srgrimes exphead = (struct exportlist *)NULL; 3241558Srgrimes mlhead = (struct mountlist *)NULL; 3251558Srgrimes if (argc == 1) { 3261558Srgrimes strncpy(exname, *argv, MAXPATHLEN-1); 3271558Srgrimes exname[MAXPATHLEN-1] = '\0'; 3281558Srgrimes } else 3291558Srgrimes strcpy(exname, _PATH_EXPORTS); 3301558Srgrimes openlog("mountd", LOG_PID, LOG_DAEMON); 3311558Srgrimes if (debug) 33237663Scharnier warnx("getting export list"); 3331558Srgrimes get_exportlist(); 3341558Srgrimes if (debug) 33537663Scharnier warnx("getting mount list"); 3361558Srgrimes get_mountlist(); 3371558Srgrimes if (debug) 33837663Scharnier warnx("here we go"); 3391558Srgrimes if (debug == 0) { 3401558Srgrimes daemon(0, 0); 3411558Srgrimes signal(SIGINT, SIG_IGN); 3421558Srgrimes signal(SIGQUIT, SIG_IGN); 3431558Srgrimes } 34475754Siedowse signal(SIGHUP, huphandler); 34574462Salfred signal(SIGTERM, terminate); 346164394Srodrigc signal(SIGPIPE, SIG_IGN); 347149433Spjd 348149433Spjd pidfile_write(pfh); 349149433Spjd 35074462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 35174462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 35274462Salfred udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 35374462Salfred tcpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 35474791Salfred udpconf = getnetconfigent("udp"); 35574791Salfred tcpconf = getnetconfigent("tcp"); 356109363Smbr 357109363Smbr rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); 358109363Smbr 35974791Salfred if (!have_v6) 36074791Salfred goto skip_v6; 36174462Salfred udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 36274462Salfred tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); 36374462Salfred /* 36474462Salfred * We're doing host-based access checks here, so don't allow 36574462Salfred * v4-in-v6 to confuse things. The kernel will disable it 36674462Salfred * by default on NFS sockets too. 36774462Salfred */ 36874462Salfred if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6, 369117684Srwatson IPV6_V6ONLY, &one, sizeof one) < 0) { 37074462Salfred syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket"); 37174462Salfred exit(1); 37274462Salfred } 37374462Salfred if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6, 374117684Srwatson IPV6_V6ONLY, &one, sizeof one) < 0) { 375117684Srwatson syslog(LOG_ERR, "can't disable v4-in-v6 on TCP socket"); 37674462Salfred exit(1); 37774462Salfred } 37874462Salfred udp6conf = getnetconfigent("udp6"); 37974462Salfred tcp6conf = getnetconfigent("tcp6"); 38074791Salfred 38174791Salfredskip_v6: 38224759Sguido if (!resvport_only) { 38383687Speter if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL, 38483687Speter &resvport_only, sizeof(resvport_only)) != 0 && 38583687Speter errno != ENOENT) { 38624759Sguido syslog(LOG_ERR, "sysctl: %m"); 38724759Sguido exit(1); 38824759Sguido } 38924330Sguido } 390126572Sbms if (svcport != 0) { 391126572Sbms bzero(&sin, sizeof(struct sockaddr_in)); 392126572Sbms sin.sin_len = sizeof(struct sockaddr_in); 393126572Sbms sin.sin_family = AF_INET; 394126572Sbms sin.sin_port = htons(svcport); 395126572Sbms 396126572Sbms bzero(&sin6, sizeof(struct sockaddr_in6)); 397126572Sbms sin6.sin6_len = sizeof(struct sockaddr_in6); 398126572Sbms sin6.sin6_family = AF_INET6; 399126572Sbms sin6.sin6_port = htons(svcport); 400126572Sbms } 40174462Salfred if (udpsock != -1 && udpconf != NULL) { 402126572Sbms if (svcport != 0) { 403126572Sbms r = bindresvport(udpsock, &sin); 404126572Sbms if (r != 0) { 405126572Sbms syslog(LOG_ERR, "bindresvport: %m"); 406126572Sbms exit(1); 407126572Sbms } 408126572Sbms } else 409126572Sbms (void)bindresvport(udpsock, NULL); 41074462Salfred udptransp = svc_dg_create(udpsock, 0, 0); 41174462Salfred if (udptransp != NULL) { 41274462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1, 41374462Salfred mntsrv, udpconf)) 41474462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service"); 41574462Salfred else 41674462Salfred xcreated++; 41774462Salfred if (!force_v2) { 41874462Salfred if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3, 41974462Salfred mntsrv, udpconf)) 42074462Salfred syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service"); 42174462Salfred else 42274462Salfred xcreated++; 42374462Salfred } 42474462Salfred } else 42574462Salfred syslog(LOG_WARNING, "can't create UDP services"); 42674462Salfred 42774462Salfred } 42874462Salfred if (tcpsock != -1 && tcpconf != NULL) { 429126572Sbms if (svcport != 0) { 430126572Sbms r = bindresvport(tcpsock, &sin); 431126572Sbms if (r != 0) { 432126572Sbms syslog(LOG_ERR, "bindresvport: %m"); 433126572Sbms exit(1); 434126572Sbms } 435126572Sbms } else 436126572Sbms (void)bindresvport(tcpsock, NULL); 43774462Salfred listen(tcpsock, SOMAXCONN); 438109363Smbr tcptransp = svc_vc_create(tcpsock, RPC_MAXDATASIZE, RPC_MAXDATASIZE); 43974462Salfred if (tcptransp != NULL) { 44074462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1, 44174462Salfred mntsrv, tcpconf)) 44274462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service"); 44374462Salfred else 44474462Salfred xcreated++; 44574462Salfred if (!force_v2) { 44674462Salfred if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3, 44774462Salfred mntsrv, tcpconf)) 44874462Salfred syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service"); 44974462Salfred else 45074462Salfred xcreated++; 45174462Salfred } 45274462Salfred } else 45374462Salfred syslog(LOG_WARNING, "can't create TCP service"); 45474462Salfred 45574462Salfred } 45674791Salfred if (have_v6 && udp6sock != -1 && udp6conf != NULL) { 457126572Sbms if (svcport != 0) { 458126572Sbms r = bindresvport_sa(udp6sock, 459126572Sbms (struct sockaddr *)&sin6); 460126572Sbms if (r != 0) { 461126572Sbms syslog(LOG_ERR, "bindresvport_sa: %m"); 462126572Sbms exit(1); 463126572Sbms } 464126572Sbms } else 465126572Sbms (void)bindresvport_sa(udp6sock, NULL); 46674462Salfred udp6transp = svc_dg_create(udp6sock, 0, 0); 46774462Salfred if (udp6transp != NULL) { 46874462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1, 46974462Salfred mntsrv, udp6conf)) 47074462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service"); 47174462Salfred else 47274462Salfred xcreated++; 47374462Salfred if (!force_v2) { 47474462Salfred if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3, 47574462Salfred mntsrv, udp6conf)) 47674462Salfred syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service"); 47774462Salfred else 47874462Salfred xcreated++; 47974462Salfred } 48074462Salfred } else 48174462Salfred syslog(LOG_WARNING, "can't create UDP6 service"); 48274462Salfred 48374462Salfred } 48474791Salfred if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) { 485126572Sbms if (svcport != 0) { 486126572Sbms r = bindresvport_sa(tcp6sock, 487126572Sbms (struct sockaddr *)&sin6); 488126572Sbms if (r != 0) { 489126572Sbms syslog(LOG_ERR, "bindresvport_sa: %m"); 490126572Sbms exit(1); 491126572Sbms } 492126572Sbms } else 493126572Sbms (void)bindresvport_sa(tcp6sock, NULL); 49474462Salfred listen(tcp6sock, SOMAXCONN); 495109363Smbr tcp6transp = svc_vc_create(tcp6sock, RPC_MAXDATASIZE, RPC_MAXDATASIZE); 49674462Salfred if (tcp6transp != NULL) { 49774462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1, 49874462Salfred mntsrv, tcp6conf)) 49974462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service"); 50074462Salfred else 50174462Salfred xcreated++; 50274462Salfred if (!force_v2) { 50374462Salfred if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3, 50474462Salfred mntsrv, tcp6conf)) 50574462Salfred syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service"); 50674462Salfred else 50774462Salfred xcreated++; 50874462Salfred } 50974462Salfred } else 51074462Salfred syslog(LOG_WARNING, "can't create TCP6 service"); 51174462Salfred 51274462Salfred } 51374462Salfred if (xcreated == 0) { 51474462Salfred syslog(LOG_ERR, "could not create any services"); 5151558Srgrimes exit(1); 5161558Srgrimes } 51775754Siedowse 51875754Siedowse /* Expand svc_run() here so that we can call get_exportlist(). */ 51975754Siedowse for (;;) { 52075754Siedowse if (got_sighup) { 52175754Siedowse get_exportlist(); 52275754Siedowse got_sighup = 0; 52375754Siedowse } 52475754Siedowse readfds = svc_fdset; 52575754Siedowse switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) { 52675754Siedowse case -1: 52775754Siedowse if (errno == EINTR) 52875754Siedowse continue; 52975754Siedowse syslog(LOG_ERR, "mountd died: select: %m"); 53075754Siedowse exit(1); 53175754Siedowse case 0: 53275754Siedowse continue; 53375754Siedowse default: 53475754Siedowse svc_getreqset(&readfds); 53575754Siedowse } 53675754Siedowse } 5371558Srgrimes} 5381558Srgrimes 53937663Scharnierstatic void 54037663Scharnierusage() 54137663Scharnier{ 54237663Scharnier fprintf(stderr, 543126572Sbms "usage: mountd [-2] [-d] [-l] [-n] [-p <port>] [-r] " 544126572Sbms "[export_file]\n"); 54537663Scharnier exit(1); 54637663Scharnier} 54737663Scharnier 5481558Srgrimes/* 5491558Srgrimes * The mount rpc service 5501558Srgrimes */ 5511558Srgrimesvoid 5521558Srgrimesmntsrv(rqstp, transp) 5531558Srgrimes struct svc_req *rqstp; 5541558Srgrimes SVCXPRT *transp; 5551558Srgrimes{ 5561558Srgrimes struct exportlist *ep; 5571558Srgrimes struct dirlist *dp; 5589336Sdfr struct fhreturn fhr; 5591558Srgrimes struct stat stb; 5601558Srgrimes struct statfs fsb; 56174462Salfred char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 56274462Salfred int lookup_failed = 1; 56374462Salfred struct sockaddr *saddr; 5649336Sdfr u_short sport; 56523681Speter char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN]; 56628911Sguido int bad = 0, defset, hostset; 5679336Sdfr sigset_t sighup_mask; 5681558Srgrimes 5699336Sdfr sigemptyset(&sighup_mask); 5709336Sdfr sigaddset(&sighup_mask, SIGHUP); 57174462Salfred saddr = svc_getrpccaller(transp)->buf; 57274462Salfred switch (saddr->sa_family) { 57374462Salfred case AF_INET6: 57475635Siedowse sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 57574462Salfred break; 57674462Salfred case AF_INET: 57775635Siedowse sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 57874462Salfred break; 57974462Salfred default: 58074462Salfred syslog(LOG_ERR, "request from unknown address family"); 58174462Salfred return; 58274462Salfred } 58374462Salfred lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, 58474462Salfred NULL, 0, 0); 58574462Salfred getnameinfo(saddr, saddr->sa_len, numerichost, 58674462Salfred sizeof numerichost, NULL, 0, NI_NUMERICHOST); 5871558Srgrimes switch (rqstp->rq_proc) { 5881558Srgrimes case NULLPROC: 589121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL)) 59037663Scharnier syslog(LOG_ERR, "can't send reply"); 5911558Srgrimes return; 5921558Srgrimes case RPCMNT_MOUNT: 5939336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 59431656Sguido syslog(LOG_NOTICE, 59531656Sguido "mount request from %s from unprivileged port", 59674462Salfred numerichost); 5971558Srgrimes svcerr_weakauth(transp); 5981558Srgrimes return; 5991558Srgrimes } 600121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 60131656Sguido syslog(LOG_NOTICE, "undecodable mount request from %s", 60274462Salfred numerichost); 6031558Srgrimes svcerr_decode(transp); 6041558Srgrimes return; 6051558Srgrimes } 6061558Srgrimes 6071558Srgrimes /* 6081558Srgrimes * Get the real pathname and make sure it is a directory 6099336Sdfr * or a regular file if the -r option was specified 6109336Sdfr * and it exists. 6111558Srgrimes */ 61251968Salfred if (realpath(rpcpath, dirpath) == NULL || 6131558Srgrimes stat(dirpath, &stb) < 0 || 6149336Sdfr (!S_ISDIR(stb.st_mode) && 61574462Salfred (dir_only || !S_ISREG(stb.st_mode))) || 6161558Srgrimes statfs(dirpath, &fsb) < 0) { 6171558Srgrimes chdir("/"); /* Just in case realpath doesn't */ 61831656Sguido syslog(LOG_NOTICE, 61937663Scharnier "mount request from %s for non existent path %s", 62074462Salfred numerichost, dirpath); 6211558Srgrimes if (debug) 62237663Scharnier warnx("stat failed on %s", dirpath); 62328911Sguido bad = ENOENT; /* We will send error reply later */ 6241558Srgrimes } 6251558Srgrimes 6261558Srgrimes /* Check in the exports list */ 6279336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 6281558Srgrimes ep = ex_search(&fsb.f_fsid); 6299336Sdfr hostset = defset = 0; 6309336Sdfr if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) || 6311558Srgrimes ((dp = dirp_search(ep->ex_dirl, dirpath)) && 63274462Salfred chk_host(dp, saddr, &defset, &hostset)) || 63374462Salfred (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 63474462Salfred scan_tree(ep->ex_dirl, saddr) == 0))) { 63528911Sguido if (bad) { 636121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 63728911Sguido (caddr_t)&bad)) 63837663Scharnier syslog(LOG_ERR, "can't send reply"); 63928911Sguido sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 64028911Sguido return; 64128911Sguido } 6429336Sdfr if (hostset & DP_HOSTSET) 6439336Sdfr fhr.fhr_flag = hostset; 6449336Sdfr else 6459336Sdfr fhr.fhr_flag = defset; 6469336Sdfr fhr.fhr_vers = rqstp->rq_vers; 6471558Srgrimes /* Get the file handle */ 64823681Speter memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 6499336Sdfr if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 6501558Srgrimes bad = errno; 65137663Scharnier syslog(LOG_ERR, "can't get fh for %s", dirpath); 652121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 6531558Srgrimes (caddr_t)&bad)) 65437663Scharnier syslog(LOG_ERR, "can't send reply"); 6559336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6561558Srgrimes return; 6571558Srgrimes } 658121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, 659121556Speter (caddr_t)&fhr)) 66037663Scharnier syslog(LOG_ERR, "can't send reply"); 66174462Salfred if (!lookup_failed) 66274462Salfred add_mlist(host, dirpath); 6631558Srgrimes else 66474462Salfred add_mlist(numerichost, dirpath); 6651558Srgrimes if (debug) 66637663Scharnier warnx("mount successful"); 667121767Speter if (dolog) 66831656Sguido syslog(LOG_NOTICE, 66931656Sguido "mount request succeeded from %s for %s", 67074462Salfred numerichost, dirpath); 67131656Sguido } else { 6721558Srgrimes bad = EACCES; 67331656Sguido syslog(LOG_NOTICE, 67431656Sguido "mount request denied from %s for %s", 67574462Salfred numerichost, dirpath); 67631656Sguido } 67728911Sguido 678121556Speter if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, 679121556Speter (caddr_t)&bad)) 68037663Scharnier syslog(LOG_ERR, "can't send reply"); 6819336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 6821558Srgrimes return; 6831558Srgrimes case RPCMNT_DUMP: 684121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL)) 68537663Scharnier syslog(LOG_ERR, "can't send reply"); 686121767Speter else if (dolog) 68731656Sguido syslog(LOG_NOTICE, 68831656Sguido "dump request succeeded from %s", 68974462Salfred numerichost); 6901558Srgrimes return; 6911558Srgrimes case RPCMNT_UMOUNT: 6929336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 69331656Sguido syslog(LOG_NOTICE, 69431656Sguido "umount request from %s from unprivileged port", 69574462Salfred numerichost); 6961558Srgrimes svcerr_weakauth(transp); 6971558Srgrimes return; 6981558Srgrimes } 699121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 70031656Sguido syslog(LOG_NOTICE, "undecodable umount request from %s", 70174462Salfred numerichost); 7021558Srgrimes svcerr_decode(transp); 7031558Srgrimes return; 7041558Srgrimes } 70551968Salfred if (realpath(rpcpath, dirpath) == NULL) { 70651968Salfred syslog(LOG_NOTICE, "umount request from %s " 70751968Salfred "for non existent path %s", 70874462Salfred numerichost, dirpath); 70951968Salfred } 710121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 71137663Scharnier syslog(LOG_ERR, "can't send reply"); 71274462Salfred if (!lookup_failed) 71375635Siedowse del_mlist(host, dirpath); 71475635Siedowse del_mlist(numerichost, dirpath); 715121767Speter if (dolog) 71631656Sguido syslog(LOG_NOTICE, 71731656Sguido "umount request succeeded from %s for %s", 71874462Salfred numerichost, dirpath); 7191558Srgrimes return; 7201558Srgrimes case RPCMNT_UMNTALL: 7219336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 72231656Sguido syslog(LOG_NOTICE, 72331656Sguido "umountall request from %s from unprivileged port", 72474462Salfred numerichost); 7251558Srgrimes svcerr_weakauth(transp); 7261558Srgrimes return; 7271558Srgrimes } 728121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 72937663Scharnier syslog(LOG_ERR, "can't send reply"); 73074462Salfred if (!lookup_failed) 73175635Siedowse del_mlist(host, NULL); 73275635Siedowse del_mlist(numerichost, NULL); 733121767Speter if (dolog) 73431656Sguido syslog(LOG_NOTICE, 73531656Sguido "umountall request succeeded from %s", 73674462Salfred numerichost); 7371558Srgrimes return; 7381558Srgrimes case RPCMNT_EXPORT: 739121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL)) 740121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, 741121556Speter (caddr_t)NULL)) 742100117Salfred syslog(LOG_ERR, "can't send reply"); 743121767Speter if (dolog) 74431656Sguido syslog(LOG_NOTICE, 74531656Sguido "export request succeeded from %s", 74674462Salfred numerichost); 7471558Srgrimes return; 7481558Srgrimes default: 7491558Srgrimes svcerr_noproc(transp); 7501558Srgrimes return; 7511558Srgrimes } 7521558Srgrimes} 7531558Srgrimes 7541558Srgrimes/* 7551558Srgrimes * Xdr conversion for a dirpath string 7561558Srgrimes */ 7571558Srgrimesint 7581558Srgrimesxdr_dir(xdrsp, dirp) 7591558Srgrimes XDR *xdrsp; 7601558Srgrimes char *dirp; 7611558Srgrimes{ 7621558Srgrimes return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 7631558Srgrimes} 7641558Srgrimes 7651558Srgrimes/* 7669336Sdfr * Xdr routine to generate file handle reply 7671558Srgrimes */ 7681558Srgrimesint 7699336Sdfrxdr_fhs(xdrsp, cp) 7701558Srgrimes XDR *xdrsp; 7719336Sdfr caddr_t cp; 7721558Srgrimes{ 77392806Sobrien struct fhreturn *fhrp = (struct fhreturn *)cp; 7749336Sdfr u_long ok = 0, len, auth; 7751558Srgrimes 7761558Srgrimes if (!xdr_long(xdrsp, &ok)) 7771558Srgrimes return (0); 7789336Sdfr switch (fhrp->fhr_vers) { 7799336Sdfr case 1: 7809336Sdfr return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 7819336Sdfr case 3: 7829336Sdfr len = NFSX_V3FH; 7839336Sdfr if (!xdr_long(xdrsp, &len)) 7849336Sdfr return (0); 7859336Sdfr if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 7869336Sdfr return (0); 78783653Speter auth = RPCAUTH_UNIX; 7889336Sdfr len = 1; 7899336Sdfr if (!xdr_long(xdrsp, &len)) 7909336Sdfr return (0); 7919336Sdfr return (xdr_long(xdrsp, &auth)); 7929336Sdfr }; 7939336Sdfr return (0); 7941558Srgrimes} 7951558Srgrimes 7961558Srgrimesint 7971558Srgrimesxdr_mlist(xdrsp, cp) 7981558Srgrimes XDR *xdrsp; 7991558Srgrimes caddr_t cp; 8001558Srgrimes{ 8011558Srgrimes struct mountlist *mlp; 8021558Srgrimes int true = 1; 8031558Srgrimes int false = 0; 8041558Srgrimes char *strp; 8051558Srgrimes 8061558Srgrimes mlp = mlhead; 8071558Srgrimes while (mlp) { 8081558Srgrimes if (!xdr_bool(xdrsp, &true)) 8091558Srgrimes return (0); 8101558Srgrimes strp = &mlp->ml_host[0]; 8111558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN)) 8121558Srgrimes return (0); 8131558Srgrimes strp = &mlp->ml_dirp[0]; 8141558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 8151558Srgrimes return (0); 8161558Srgrimes mlp = mlp->ml_next; 8171558Srgrimes } 8181558Srgrimes if (!xdr_bool(xdrsp, &false)) 8191558Srgrimes return (0); 8201558Srgrimes return (1); 8211558Srgrimes} 8221558Srgrimes 8231558Srgrimes/* 8241558Srgrimes * Xdr conversion for export list 8251558Srgrimes */ 8261558Srgrimesint 827100117Salfredxdr_explist_common(xdrsp, cp, brief) 8281558Srgrimes XDR *xdrsp; 8291558Srgrimes caddr_t cp; 830100117Salfred int brief; 8311558Srgrimes{ 8321558Srgrimes struct exportlist *ep; 8331558Srgrimes int false = 0; 8349336Sdfr int putdef; 8359336Sdfr sigset_t sighup_mask; 8361558Srgrimes 8379336Sdfr sigemptyset(&sighup_mask); 8389336Sdfr sigaddset(&sighup_mask, SIGHUP); 8399336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 8401558Srgrimes ep = exphead; 8411558Srgrimes while (ep) { 8421558Srgrimes putdef = 0; 843100117Salfred if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, 844100117Salfred &putdef, brief)) 8451558Srgrimes goto errout; 8461558Srgrimes if (ep->ex_defdir && putdef == 0 && 8471558Srgrimes put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 848100117Salfred &putdef, brief)) 8491558Srgrimes goto errout; 8501558Srgrimes ep = ep->ex_next; 8511558Srgrimes } 8529336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8531558Srgrimes if (!xdr_bool(xdrsp, &false)) 8541558Srgrimes return (0); 8551558Srgrimes return (1); 8561558Srgrimeserrout: 8579336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8581558Srgrimes return (0); 8591558Srgrimes} 8601558Srgrimes 8611558Srgrimes/* 8621558Srgrimes * Called from xdr_explist() to traverse the tree and export the 8631558Srgrimes * directory paths. 8641558Srgrimes */ 8651558Srgrimesint 866100117Salfredput_exlist(dp, xdrsp, adp, putdefp, brief) 8671558Srgrimes struct dirlist *dp; 8681558Srgrimes XDR *xdrsp; 8691558Srgrimes struct dirlist *adp; 8701558Srgrimes int *putdefp; 871100117Salfred int brief; 8721558Srgrimes{ 8731558Srgrimes struct grouplist *grp; 8741558Srgrimes struct hostlist *hp; 8751558Srgrimes int true = 1; 8761558Srgrimes int false = 0; 8771558Srgrimes int gotalldir = 0; 8781558Srgrimes char *strp; 8791558Srgrimes 8801558Srgrimes if (dp) { 881100117Salfred if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief)) 8821558Srgrimes return (1); 8831558Srgrimes if (!xdr_bool(xdrsp, &true)) 8841558Srgrimes return (1); 8851558Srgrimes strp = dp->dp_dirp; 8861558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 8871558Srgrimes return (1); 8881558Srgrimes if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 8891558Srgrimes gotalldir = 1; 8901558Srgrimes *putdefp = 1; 8911558Srgrimes } 892100117Salfred if (brief) { 893100117Salfred if (!xdr_bool(xdrsp, &true)) 894100117Salfred return (1); 895100117Salfred strp = "(...)"; 896100117Salfred if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 897100117Salfred return (1); 898100117Salfred } else if ((dp->dp_flag & DP_DEFSET) == 0 && 8991558Srgrimes (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 9001558Srgrimes hp = dp->dp_hosts; 9011558Srgrimes while (hp) { 9021558Srgrimes grp = hp->ht_grp; 9031558Srgrimes if (grp->gr_type == GT_HOST) { 9041558Srgrimes if (!xdr_bool(xdrsp, &true)) 9051558Srgrimes return (1); 90674462Salfred strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 9078871Srgrimes if (!xdr_string(xdrsp, &strp, 9081558Srgrimes RPCMNT_NAMELEN)) 9091558Srgrimes return (1); 9101558Srgrimes } else if (grp->gr_type == GT_NET) { 9111558Srgrimes if (!xdr_bool(xdrsp, &true)) 9121558Srgrimes return (1); 9131558Srgrimes strp = grp->gr_ptr.gt_net.nt_name; 9148871Srgrimes if (!xdr_string(xdrsp, &strp, 9151558Srgrimes RPCMNT_NAMELEN)) 9161558Srgrimes return (1); 9171558Srgrimes } 9181558Srgrimes hp = hp->ht_next; 9191558Srgrimes if (gotalldir && hp == (struct hostlist *)NULL) { 9201558Srgrimes hp = adp->dp_hosts; 9211558Srgrimes gotalldir = 0; 9221558Srgrimes } 9231558Srgrimes } 9241558Srgrimes } 9251558Srgrimes if (!xdr_bool(xdrsp, &false)) 9261558Srgrimes return (1); 927100117Salfred if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief)) 9281558Srgrimes return (1); 9291558Srgrimes } 9301558Srgrimes return (0); 9311558Srgrimes} 9321558Srgrimes 933100117Salfredint 934100117Salfredxdr_explist(xdrsp, cp) 935100117Salfred XDR *xdrsp; 936100117Salfred caddr_t cp; 937100117Salfred{ 938100117Salfred 939100117Salfred return xdr_explist_common(xdrsp, cp, 0); 940100117Salfred} 941100117Salfred 942100117Salfredint 943100117Salfredxdr_explist_brief(xdrsp, cp) 944100117Salfred XDR *xdrsp; 945100117Salfred caddr_t cp; 946100117Salfred{ 947100117Salfred 948100117Salfred return xdr_explist_common(xdrsp, cp, 1); 949100117Salfred} 950100117Salfred 95196622Siedowsechar *line; 95296622Siedowseint linesize; 9531558SrgrimesFILE *exp_file; 9541558Srgrimes 9551558Srgrimes/* 9561558Srgrimes * Get the export list 9571558Srgrimes */ 9581558Srgrimesvoid 9591558Srgrimesget_exportlist() 9601558Srgrimes{ 9611558Srgrimes struct exportlist *ep, *ep2; 9621558Srgrimes struct grouplist *grp, *tgrp; 9631558Srgrimes struct exportlist **epp; 964158857Srodrigc struct export_args export; 9651558Srgrimes struct dirlist *dirhead; 966158857Srodrigc struct iovec *iov; 967160175Srodrigc struct statfs fsb, *fsp, *mntbufp; 96872650Sgreen struct xucred anon; 969159360Srodrigc struct xvfsconf vfc; 9701558Srgrimes char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 971158857Srodrigc char errmsg[255]; 9721558Srgrimes int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp; 973158857Srodrigc int iovlen; 9741558Srgrimes 975158857Srodrigc bzero(&export, sizeof(export)); 976158857Srodrigc export.ex_flags = MNT_DELEXPORT; 97751968Salfred dirp = NULL; 97851968Salfred dirplen = 0; 979158857Srodrigc iov = NULL; 980158857Srodrigc iovlen = 0; 981158857Srodrigc bzero(errmsg, sizeof(errmsg)); 98251968Salfred 9831558Srgrimes /* 9841558Srgrimes * First, get rid of the old list 9851558Srgrimes */ 9861558Srgrimes ep = exphead; 9871558Srgrimes while (ep) { 9881558Srgrimes ep2 = ep; 9891558Srgrimes ep = ep->ex_next; 9901558Srgrimes free_exp(ep2); 9911558Srgrimes } 9921558Srgrimes exphead = (struct exportlist *)NULL; 9931558Srgrimes 9941558Srgrimes grp = grphead; 9951558Srgrimes while (grp) { 9961558Srgrimes tgrp = grp; 9971558Srgrimes grp = grp->gr_next; 9981558Srgrimes free_grp(tgrp); 9991558Srgrimes } 10001558Srgrimes grphead = (struct grouplist *)NULL; 10011558Srgrimes 10021558Srgrimes /* 10031558Srgrimes * And delete exports that are in the kernel for all local 100496707Strhodes * filesystems. 1005160175Srodrigc * XXX: Should know how to handle all local exportable filesystems. 10061558Srgrimes */ 1007160175Srodrigc num = getmntinfo(&mntbufp, MNT_NOWAIT); 1008158857Srodrigc 1009158857Srodrigc if (num > 0) { 1010158857Srodrigc build_iovec(&iov, &iovlen, "fstype", NULL, 0); 1011158857Srodrigc build_iovec(&iov, &iovlen, "fspath", NULL, 0); 1012158857Srodrigc build_iovec(&iov, &iovlen, "from", NULL, 0); 1013158857Srodrigc build_iovec(&iov, &iovlen, "update", NULL, 0); 1014158857Srodrigc build_iovec(&iov, &iovlen, "export", &export, sizeof(export)); 1015158857Srodrigc build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 1016158857Srodrigc } 1017158857Srodrigc 10181558Srgrimes for (i = 0; i < num; i++) { 1019160175Srodrigc fsp = &mntbufp[i]; 1020159360Srodrigc if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) { 1021159360Srodrigc syslog(LOG_ERR, "getvfsbyname() failed for %s", 1022159360Srodrigc fsp->f_fstypename); 1023159360Srodrigc continue; 1024159360Srodrigc } 1025159360Srodrigc 1026159360Srodrigc /* 1027159360Srodrigc * Do not delete export for network filesystem by 1028159360Srodrigc * passing "export" arg to nmount(). 1029159360Srodrigc * It only makes sense to do this for local filesystems. 1030159360Srodrigc */ 1031159360Srodrigc if (vfc.vfc_flags & VFCF_NETWORK) 1032159360Srodrigc continue; 1033159360Srodrigc 1034158877Srodrigc iov[1].iov_base = fsp->f_fstypename; 1035158877Srodrigc iov[1].iov_len = strlen(fsp->f_fstypename) + 1; 1036158877Srodrigc iov[3].iov_base = fsp->f_mntonname; 1037158877Srodrigc iov[3].iov_len = strlen(fsp->f_mntonname) + 1; 1038158877Srodrigc iov[5].iov_base = fsp->f_mntfromname; 1039158877Srodrigc iov[5].iov_len = strlen(fsp->f_mntfromname) + 1; 10401558Srgrimes 1041158877Srodrigc /* 1042158877Srodrigc * Kick out MNT_ROOTFS. It should not be passed from 1043158877Srodrigc * userland to kernel. It should only be used 1044158877Srodrigc * internally in the kernel. 1045158877Srodrigc */ 1046158877Srodrigc if (fsp->f_flags & MNT_ROOTFS) { 1047158877Srodrigc fsp->f_flags &= ~MNT_ROOTFS; 1048158877Srodrigc } 1049158857Srodrigc 1050158877Srodrigc if (nmount(iov, iovlen, fsp->f_flags) < 0 && 1051159017Srodrigc errno != ENOENT && errno != ENOTSUP) { 1052158877Srodrigc syslog(LOG_ERR, 1053158877Srodrigc "can't delete exports for %s: %m %s", 1054158877Srodrigc fsp->f_mntonname, errmsg); 10551558Srgrimes } 10561558Srgrimes } 10571558Srgrimes 1058158857Srodrigc if (iov != NULL) { 1059158857Srodrigc /* Free strings allocated by strdup() in getmntopts.c */ 1060158857Srodrigc free(iov[0].iov_base); /* fstype */ 1061158857Srodrigc free(iov[2].iov_base); /* fspath */ 1062158857Srodrigc free(iov[4].iov_base); /* from */ 1063158857Srodrigc free(iov[6].iov_base); /* update */ 1064158857Srodrigc free(iov[8].iov_base); /* export */ 1065158857Srodrigc free(iov[10].iov_base); /* errmsg */ 1066158857Srodrigc 1067158857Srodrigc /* free iov, allocated by realloc() */ 1068158857Srodrigc free(iov); 1069158857Srodrigc iovlen = 0; 1070158857Srodrigc } 1071158857Srodrigc 10721558Srgrimes /* 10731558Srgrimes * Read in the exports file and build the list, calling 1074158857Srodrigc * nmount() as we go along to push the export rules into the kernel. 10751558Srgrimes */ 10761558Srgrimes if ((exp_file = fopen(exname, "r")) == NULL) { 107737663Scharnier syslog(LOG_ERR, "can't open %s", exname); 10781558Srgrimes exit(2); 10791558Srgrimes } 10801558Srgrimes dirhead = (struct dirlist *)NULL; 10811558Srgrimes while (get_line()) { 10821558Srgrimes if (debug) 108337663Scharnier warnx("got line %s", line); 10841558Srgrimes cp = line; 10851558Srgrimes nextfield(&cp, &endcp); 10861558Srgrimes if (*cp == '#') 10871558Srgrimes goto nextline; 10881558Srgrimes 10891558Srgrimes /* 10901558Srgrimes * Set defaults. 10911558Srgrimes */ 10921558Srgrimes has_host = FALSE; 10931558Srgrimes anon = def_anon; 10941558Srgrimes exflags = MNT_EXPORTED; 10951558Srgrimes got_nondir = 0; 10961558Srgrimes opt_flags = 0; 10971558Srgrimes ep = (struct exportlist *)NULL; 10981558Srgrimes 10991558Srgrimes /* 11001558Srgrimes * Create new exports list entry 11011558Srgrimes */ 11021558Srgrimes len = endcp-cp; 11031558Srgrimes tgrp = grp = get_grp(); 11041558Srgrimes while (len > 0) { 11051558Srgrimes if (len > RPCMNT_NAMELEN) { 11061558Srgrimes getexp_err(ep, tgrp); 11071558Srgrimes goto nextline; 11081558Srgrimes } 11091558Srgrimes if (*cp == '-') { 11101558Srgrimes if (ep == (struct exportlist *)NULL) { 11111558Srgrimes getexp_err(ep, tgrp); 11121558Srgrimes goto nextline; 11131558Srgrimes } 11141558Srgrimes if (debug) 111537663Scharnier warnx("doing opt %s", cp); 11161558Srgrimes got_nondir = 1; 11171558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 11181558Srgrimes &exflags, &anon)) { 11191558Srgrimes getexp_err(ep, tgrp); 11201558Srgrimes goto nextline; 11211558Srgrimes } 11221558Srgrimes } else if (*cp == '/') { 11231558Srgrimes savedc = *endcp; 11241558Srgrimes *endcp = '\0'; 11251558Srgrimes if (check_dirpath(cp) && 11261558Srgrimes statfs(cp, &fsb) >= 0) { 11271558Srgrimes if (got_nondir) { 112837663Scharnier syslog(LOG_ERR, "dirs must be first"); 11291558Srgrimes getexp_err(ep, tgrp); 11301558Srgrimes goto nextline; 11311558Srgrimes } 11321558Srgrimes if (ep) { 11331558Srgrimes if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] || 11341558Srgrimes ep->ex_fs.val[1] != fsb.f_fsid.val[1]) { 11351558Srgrimes getexp_err(ep, tgrp); 11361558Srgrimes goto nextline; 11371558Srgrimes } 11381558Srgrimes } else { 11391558Srgrimes /* 11401558Srgrimes * See if this directory is already 11411558Srgrimes * in the list. 11421558Srgrimes */ 11431558Srgrimes ep = ex_search(&fsb.f_fsid); 11441558Srgrimes if (ep == (struct exportlist *)NULL) { 11451558Srgrimes ep = get_exp(); 11461558Srgrimes ep->ex_fs = fsb.f_fsid; 11471558Srgrimes ep->ex_fsdir = (char *) 11481558Srgrimes malloc(strlen(fsb.f_mntonname) + 1); 11491558Srgrimes if (ep->ex_fsdir) 11501558Srgrimes strcpy(ep->ex_fsdir, 11511558Srgrimes fsb.f_mntonname); 11521558Srgrimes else 11531558Srgrimes out_of_mem(); 11541558Srgrimes if (debug) 115574462Salfred warnx("making new ep fs=0x%x,0x%x", 115674462Salfred fsb.f_fsid.val[0], 115774462Salfred fsb.f_fsid.val[1]); 11581558Srgrimes } else if (debug) 115937663Scharnier warnx("found ep fs=0x%x,0x%x", 11601558Srgrimes fsb.f_fsid.val[0], 11611558Srgrimes fsb.f_fsid.val[1]); 11621558Srgrimes } 11631558Srgrimes 11641558Srgrimes /* 11651558Srgrimes * Add dirpath to export mount point. 11661558Srgrimes */ 11671558Srgrimes dirp = add_expdir(&dirhead, cp, len); 11681558Srgrimes dirplen = len; 11691558Srgrimes } else { 11701558Srgrimes getexp_err(ep, tgrp); 11711558Srgrimes goto nextline; 11721558Srgrimes } 11731558Srgrimes *endcp = savedc; 11741558Srgrimes } else { 11751558Srgrimes savedc = *endcp; 11761558Srgrimes *endcp = '\0'; 11771558Srgrimes got_nondir = 1; 11781558Srgrimes if (ep == (struct exportlist *)NULL) { 11791558Srgrimes getexp_err(ep, tgrp); 11801558Srgrimes goto nextline; 11811558Srgrimes } 11821558Srgrimes 11831558Srgrimes /* 11841558Srgrimes * Get the host or netgroup. 11851558Srgrimes */ 11861558Srgrimes setnetgrent(cp); 11871558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 11881558Srgrimes do { 11891558Srgrimes if (has_host) { 11901558Srgrimes grp->gr_next = get_grp(); 11911558Srgrimes grp = grp->gr_next; 11921558Srgrimes } 11931558Srgrimes if (netgrp) { 119437003Sjoerg if (hst == 0) { 119537663Scharnier syslog(LOG_ERR, 119637663Scharnier "null hostname in netgroup %s, skipping", cp); 119737004Sjoerg grp->gr_type = GT_IGNORE; 119837003Sjoerg } else if (get_host(hst, grp, tgrp)) { 119937663Scharnier syslog(LOG_ERR, 120037663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 120129317Sjlemon grp->gr_type = GT_IGNORE; 12021558Srgrimes } 12037401Swpaul } else if (get_host(cp, grp, tgrp)) { 120437663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 120529317Sjlemon grp->gr_type = GT_IGNORE; 12061558Srgrimes } 12071558Srgrimes has_host = TRUE; 12081558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 12091558Srgrimes endnetgrent(); 12101558Srgrimes *endcp = savedc; 12111558Srgrimes } 12121558Srgrimes cp = endcp; 12131558Srgrimes nextfield(&cp, &endcp); 12141558Srgrimes len = endcp - cp; 12151558Srgrimes } 12161558Srgrimes if (check_options(dirhead)) { 12171558Srgrimes getexp_err(ep, tgrp); 12181558Srgrimes goto nextline; 12191558Srgrimes } 12201558Srgrimes if (!has_host) { 122175641Siedowse grp->gr_type = GT_DEFAULT; 12221558Srgrimes if (debug) 122337663Scharnier warnx("adding a default entry"); 12241558Srgrimes 12251558Srgrimes /* 12261558Srgrimes * Don't allow a network export coincide with a list of 12271558Srgrimes * host(s) on the same line. 12281558Srgrimes */ 12291558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 123075801Siedowse syslog(LOG_ERR, "network/host conflict"); 12311558Srgrimes getexp_err(ep, tgrp); 12321558Srgrimes goto nextline; 123329317Sjlemon 123474462Salfred /* 123574462Salfred * If an export list was specified on this line, make sure 123629317Sjlemon * that we have at least one valid entry, otherwise skip it. 123729317Sjlemon */ 123829317Sjlemon } else { 123929317Sjlemon grp = tgrp; 124074462Salfred while (grp && grp->gr_type == GT_IGNORE) 124129317Sjlemon grp = grp->gr_next; 124229317Sjlemon if (! grp) { 124329317Sjlemon getexp_err(ep, tgrp); 124429317Sjlemon goto nextline; 124529317Sjlemon } 12461558Srgrimes } 12471558Srgrimes 12481558Srgrimes /* 12491558Srgrimes * Loop through hosts, pushing the exports into the kernel. 12501558Srgrimes * After loop, tgrp points to the start of the list and 12511558Srgrimes * grp points to the last entry in the list. 12521558Srgrimes */ 12531558Srgrimes grp = tgrp; 12541558Srgrimes do { 125575635Siedowse if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 125675635Siedowse &fsb)) { 125775635Siedowse getexp_err(ep, tgrp); 125875635Siedowse goto nextline; 125975635Siedowse } 12601558Srgrimes } while (grp->gr_next && (grp = grp->gr_next)); 12611558Srgrimes 12621558Srgrimes /* 12631558Srgrimes * Success. Update the data structures. 12641558Srgrimes */ 12651558Srgrimes if (has_host) { 12669336Sdfr hang_dirp(dirhead, tgrp, ep, opt_flags); 12671558Srgrimes grp->gr_next = grphead; 12681558Srgrimes grphead = tgrp; 12691558Srgrimes } else { 12701558Srgrimes hang_dirp(dirhead, (struct grouplist *)NULL, ep, 12719336Sdfr opt_flags); 12721558Srgrimes free_grp(grp); 12731558Srgrimes } 12741558Srgrimes dirhead = (struct dirlist *)NULL; 12751558Srgrimes if ((ep->ex_flag & EX_LINKED) == 0) { 12761558Srgrimes ep2 = exphead; 12771558Srgrimes epp = &exphead; 12781558Srgrimes 12791558Srgrimes /* 12801558Srgrimes * Insert in the list in alphabetical order. 12811558Srgrimes */ 12821558Srgrimes while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { 12831558Srgrimes epp = &ep2->ex_next; 12841558Srgrimes ep2 = ep2->ex_next; 12851558Srgrimes } 12861558Srgrimes if (ep2) 12871558Srgrimes ep->ex_next = ep2; 12881558Srgrimes *epp = ep; 12891558Srgrimes ep->ex_flag |= EX_LINKED; 12901558Srgrimes } 12911558Srgrimesnextline: 12921558Srgrimes if (dirhead) { 12931558Srgrimes free_dir(dirhead); 12941558Srgrimes dirhead = (struct dirlist *)NULL; 12951558Srgrimes } 12961558Srgrimes } 12971558Srgrimes fclose(exp_file); 12981558Srgrimes} 12991558Srgrimes 13001558Srgrimes/* 13011558Srgrimes * Allocate an export list element 13021558Srgrimes */ 13031558Srgrimesstruct exportlist * 13041558Srgrimesget_exp() 13051558Srgrimes{ 13061558Srgrimes struct exportlist *ep; 13071558Srgrimes 13081558Srgrimes ep = (struct exportlist *)malloc(sizeof (struct exportlist)); 13091558Srgrimes if (ep == (struct exportlist *)NULL) 13101558Srgrimes out_of_mem(); 131123681Speter memset(ep, 0, sizeof(struct exportlist)); 13121558Srgrimes return (ep); 13131558Srgrimes} 13141558Srgrimes 13151558Srgrimes/* 13161558Srgrimes * Allocate a group list element 13171558Srgrimes */ 13181558Srgrimesstruct grouplist * 13191558Srgrimesget_grp() 13201558Srgrimes{ 13211558Srgrimes struct grouplist *gp; 13221558Srgrimes 13231558Srgrimes gp = (struct grouplist *)malloc(sizeof (struct grouplist)); 13241558Srgrimes if (gp == (struct grouplist *)NULL) 13251558Srgrimes out_of_mem(); 132623681Speter memset(gp, 0, sizeof(struct grouplist)); 13271558Srgrimes return (gp); 13281558Srgrimes} 13291558Srgrimes 13301558Srgrimes/* 13311558Srgrimes * Clean up upon an error in get_exportlist(). 13321558Srgrimes */ 13331558Srgrimesvoid 13341558Srgrimesgetexp_err(ep, grp) 13351558Srgrimes struct exportlist *ep; 13361558Srgrimes struct grouplist *grp; 13371558Srgrimes{ 13381558Srgrimes struct grouplist *tgrp; 13391558Srgrimes 1340100336Sjoerg if (!(opt_flags & OP_QUIET)) 1341100336Sjoerg syslog(LOG_ERR, "bad exports list line %s", line); 13421558Srgrimes if (ep && (ep->ex_flag & EX_LINKED) == 0) 13431558Srgrimes free_exp(ep); 13441558Srgrimes while (grp) { 13451558Srgrimes tgrp = grp; 13461558Srgrimes grp = grp->gr_next; 13471558Srgrimes free_grp(tgrp); 13481558Srgrimes } 13491558Srgrimes} 13501558Srgrimes 13511558Srgrimes/* 13521558Srgrimes * Search the export list for a matching fs. 13531558Srgrimes */ 13541558Srgrimesstruct exportlist * 13551558Srgrimesex_search(fsid) 13561558Srgrimes fsid_t *fsid; 13571558Srgrimes{ 13581558Srgrimes struct exportlist *ep; 13591558Srgrimes 13601558Srgrimes ep = exphead; 13611558Srgrimes while (ep) { 13621558Srgrimes if (ep->ex_fs.val[0] == fsid->val[0] && 13631558Srgrimes ep->ex_fs.val[1] == fsid->val[1]) 13641558Srgrimes return (ep); 13651558Srgrimes ep = ep->ex_next; 13661558Srgrimes } 13671558Srgrimes return (ep); 13681558Srgrimes} 13691558Srgrimes 13701558Srgrimes/* 13711558Srgrimes * Add a directory path to the list. 13721558Srgrimes */ 13731558Srgrimeschar * 13741558Srgrimesadd_expdir(dpp, cp, len) 13751558Srgrimes struct dirlist **dpp; 13761558Srgrimes char *cp; 13771558Srgrimes int len; 13781558Srgrimes{ 13791558Srgrimes struct dirlist *dp; 13801558Srgrimes 13811558Srgrimes dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); 138237663Scharnier if (dp == (struct dirlist *)NULL) 138337663Scharnier out_of_mem(); 13841558Srgrimes dp->dp_left = *dpp; 13851558Srgrimes dp->dp_right = (struct dirlist *)NULL; 13861558Srgrimes dp->dp_flag = 0; 13871558Srgrimes dp->dp_hosts = (struct hostlist *)NULL; 13881558Srgrimes strcpy(dp->dp_dirp, cp); 13891558Srgrimes *dpp = dp; 13901558Srgrimes return (dp->dp_dirp); 13911558Srgrimes} 13921558Srgrimes 13931558Srgrimes/* 13941558Srgrimes * Hang the dir list element off the dirpath binary tree as required 13951558Srgrimes * and update the entry for host. 13961558Srgrimes */ 13971558Srgrimesvoid 13989336Sdfrhang_dirp(dp, grp, ep, flags) 13991558Srgrimes struct dirlist *dp; 14001558Srgrimes struct grouplist *grp; 14011558Srgrimes struct exportlist *ep; 14029336Sdfr int flags; 14031558Srgrimes{ 14041558Srgrimes struct hostlist *hp; 14051558Srgrimes struct dirlist *dp2; 14061558Srgrimes 14079336Sdfr if (flags & OP_ALLDIRS) { 14081558Srgrimes if (ep->ex_defdir) 14091558Srgrimes free((caddr_t)dp); 14101558Srgrimes else 14111558Srgrimes ep->ex_defdir = dp; 14129336Sdfr if (grp == (struct grouplist *)NULL) { 14131558Srgrimes ep->ex_defdir->dp_flag |= DP_DEFSET; 14149336Sdfr } else while (grp) { 14151558Srgrimes hp = get_ht(); 14161558Srgrimes hp->ht_grp = grp; 14171558Srgrimes hp->ht_next = ep->ex_defdir->dp_hosts; 14181558Srgrimes ep->ex_defdir->dp_hosts = hp; 14191558Srgrimes grp = grp->gr_next; 14201558Srgrimes } 14211558Srgrimes } else { 14221558Srgrimes 14231558Srgrimes /* 142437663Scharnier * Loop through the directories adding them to the tree. 14251558Srgrimes */ 14261558Srgrimes while (dp) { 14271558Srgrimes dp2 = dp->dp_left; 14289336Sdfr add_dlist(&ep->ex_dirl, dp, grp, flags); 14291558Srgrimes dp = dp2; 14301558Srgrimes } 14311558Srgrimes } 14321558Srgrimes} 14331558Srgrimes 14341558Srgrimes/* 14351558Srgrimes * Traverse the binary tree either updating a node that is already there 14361558Srgrimes * for the new directory or adding the new node. 14371558Srgrimes */ 14381558Srgrimesvoid 14399336Sdfradd_dlist(dpp, newdp, grp, flags) 14401558Srgrimes struct dirlist **dpp; 14411558Srgrimes struct dirlist *newdp; 14421558Srgrimes struct grouplist *grp; 14439336Sdfr int flags; 14441558Srgrimes{ 14451558Srgrimes struct dirlist *dp; 14461558Srgrimes struct hostlist *hp; 14471558Srgrimes int cmp; 14481558Srgrimes 14491558Srgrimes dp = *dpp; 14501558Srgrimes if (dp) { 14511558Srgrimes cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 14521558Srgrimes if (cmp > 0) { 14539336Sdfr add_dlist(&dp->dp_left, newdp, grp, flags); 14541558Srgrimes return; 14551558Srgrimes } else if (cmp < 0) { 14569336Sdfr add_dlist(&dp->dp_right, newdp, grp, flags); 14571558Srgrimes return; 14581558Srgrimes } else 14591558Srgrimes free((caddr_t)newdp); 14601558Srgrimes } else { 14611558Srgrimes dp = newdp; 14621558Srgrimes dp->dp_left = (struct dirlist *)NULL; 14631558Srgrimes *dpp = dp; 14641558Srgrimes } 14651558Srgrimes if (grp) { 14661558Srgrimes 14671558Srgrimes /* 14681558Srgrimes * Hang all of the host(s) off of the directory point. 14691558Srgrimes */ 14701558Srgrimes do { 14711558Srgrimes hp = get_ht(); 14721558Srgrimes hp->ht_grp = grp; 14731558Srgrimes hp->ht_next = dp->dp_hosts; 14741558Srgrimes dp->dp_hosts = hp; 14751558Srgrimes grp = grp->gr_next; 14761558Srgrimes } while (grp); 14779336Sdfr } else { 14781558Srgrimes dp->dp_flag |= DP_DEFSET; 14799336Sdfr } 14801558Srgrimes} 14811558Srgrimes 14821558Srgrimes/* 14831558Srgrimes * Search for a dirpath on the export point. 14841558Srgrimes */ 14851558Srgrimesstruct dirlist * 148674462Salfreddirp_search(dp, dirp) 14871558Srgrimes struct dirlist *dp; 148874462Salfred char *dirp; 14891558Srgrimes{ 14901558Srgrimes int cmp; 14911558Srgrimes 14921558Srgrimes if (dp) { 149374462Salfred cmp = strcmp(dp->dp_dirp, dirp); 14941558Srgrimes if (cmp > 0) 149574462Salfred return (dirp_search(dp->dp_left, dirp)); 14961558Srgrimes else if (cmp < 0) 149774462Salfred return (dirp_search(dp->dp_right, dirp)); 14981558Srgrimes else 14991558Srgrimes return (dp); 15001558Srgrimes } 15011558Srgrimes return (dp); 15021558Srgrimes} 15031558Srgrimes 15041558Srgrimes/* 15051558Srgrimes * Scan for a host match in a directory tree. 15061558Srgrimes */ 15071558Srgrimesint 15089336Sdfrchk_host(dp, saddr, defsetp, hostsetp) 15091558Srgrimes struct dirlist *dp; 151074462Salfred struct sockaddr *saddr; 15111558Srgrimes int *defsetp; 15129336Sdfr int *hostsetp; 15131558Srgrimes{ 15141558Srgrimes struct hostlist *hp; 15151558Srgrimes struct grouplist *grp; 151674462Salfred struct addrinfo *ai; 15171558Srgrimes 15181558Srgrimes if (dp) { 15191558Srgrimes if (dp->dp_flag & DP_DEFSET) 15209336Sdfr *defsetp = dp->dp_flag; 15211558Srgrimes hp = dp->dp_hosts; 15221558Srgrimes while (hp) { 15231558Srgrimes grp = hp->ht_grp; 15241558Srgrimes switch (grp->gr_type) { 15251558Srgrimes case GT_HOST: 152674462Salfred ai = grp->gr_ptr.gt_addrinfo; 152774462Salfred for (; ai; ai = ai->ai_next) { 152875801Siedowse if (!sacmp(ai->ai_addr, saddr, NULL)) { 152974462Salfred *hostsetp = 153074462Salfred (hp->ht_flag | DP_HOSTSET); 153174462Salfred return (1); 153274462Salfred } 15339336Sdfr } 153475801Siedowse break; 15351558Srgrimes case GT_NET: 153675801Siedowse if (!sacmp(saddr, (struct sockaddr *) 153775801Siedowse &grp->gr_ptr.gt_net.nt_net, 153875801Siedowse (struct sockaddr *) 153975801Siedowse &grp->gr_ptr.gt_net.nt_mask)) { 154074462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 154174462Salfred return (1); 154274462Salfred } 154375801Siedowse break; 154475801Siedowse } 15451558Srgrimes hp = hp->ht_next; 15461558Srgrimes } 15471558Srgrimes } 15481558Srgrimes return (0); 15491558Srgrimes} 15501558Srgrimes 15511558Srgrimes/* 15521558Srgrimes * Scan tree for a host that matches the address. 15531558Srgrimes */ 15541558Srgrimesint 15551558Srgrimesscan_tree(dp, saddr) 15561558Srgrimes struct dirlist *dp; 155774462Salfred struct sockaddr *saddr; 15581558Srgrimes{ 15599336Sdfr int defset, hostset; 15601558Srgrimes 15611558Srgrimes if (dp) { 15621558Srgrimes if (scan_tree(dp->dp_left, saddr)) 15631558Srgrimes return (1); 15649336Sdfr if (chk_host(dp, saddr, &defset, &hostset)) 15651558Srgrimes return (1); 15661558Srgrimes if (scan_tree(dp->dp_right, saddr)) 15671558Srgrimes return (1); 15681558Srgrimes } 15691558Srgrimes return (0); 15701558Srgrimes} 15711558Srgrimes 15721558Srgrimes/* 15731558Srgrimes * Traverse the dirlist tree and free it up. 15741558Srgrimes */ 15751558Srgrimesvoid 15761558Srgrimesfree_dir(dp) 15771558Srgrimes struct dirlist *dp; 15781558Srgrimes{ 15791558Srgrimes 15801558Srgrimes if (dp) { 15811558Srgrimes free_dir(dp->dp_left); 15821558Srgrimes free_dir(dp->dp_right); 15831558Srgrimes free_host(dp->dp_hosts); 15841558Srgrimes free((caddr_t)dp); 15851558Srgrimes } 15861558Srgrimes} 15871558Srgrimes 15881558Srgrimes/* 15891558Srgrimes * Parse the option string and update fields. 15901558Srgrimes * Option arguments may either be -<option>=<value> or 15911558Srgrimes * -<option> <value> 15921558Srgrimes */ 15931558Srgrimesint 15941558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr) 15951558Srgrimes char **cpp, **endcpp; 15961558Srgrimes struct exportlist *ep; 15971558Srgrimes struct grouplist *grp; 15981558Srgrimes int *has_hostp; 15991558Srgrimes int *exflagsp; 160072650Sgreen struct xucred *cr; 16011558Srgrimes{ 16021558Srgrimes char *cpoptarg, *cpoptend; 16031558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 16041558Srgrimes int allflag, usedarg; 16051558Srgrimes 160651968Salfred savedc2 = '\0'; 16071558Srgrimes cpopt = *cpp; 16081558Srgrimes cpopt++; 16091558Srgrimes cp = *endcpp; 16101558Srgrimes savedc = *cp; 16111558Srgrimes *cp = '\0'; 16121558Srgrimes while (cpopt && *cpopt) { 16131558Srgrimes allflag = 1; 16141558Srgrimes usedarg = -2; 161537663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 16161558Srgrimes *cpoptend++ = '\0'; 161737663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 16181558Srgrimes *cpoptarg++ = '\0'; 16191558Srgrimes } else { 162037663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 16211558Srgrimes *cpoptarg++ = '\0'; 16221558Srgrimes else { 16231558Srgrimes *cp = savedc; 16241558Srgrimes nextfield(&cp, &endcp); 16251558Srgrimes **endcpp = '\0'; 16261558Srgrimes if (endcp > cp && *cp != '-') { 16271558Srgrimes cpoptarg = cp; 16281558Srgrimes savedc2 = *endcp; 16291558Srgrimes *endcp = '\0'; 16301558Srgrimes usedarg = 0; 16311558Srgrimes } 16321558Srgrimes } 16331558Srgrimes } 16341558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 16351558Srgrimes *exflagsp |= MNT_EXRDONLY; 16361558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 16371558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 16381558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 16391558Srgrimes usedarg++; 16401558Srgrimes parsecred(cpoptarg, cr); 16411558Srgrimes if (allflag == 0) { 16421558Srgrimes *exflagsp |= MNT_EXPORTANON; 16431558Srgrimes opt_flags |= OP_MAPALL; 16441558Srgrimes } else 16451558Srgrimes opt_flags |= OP_MAPROOT; 16461558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 164775801Siedowse !strcmp(cpopt, "m"))) { 16481558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 164937663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 16501558Srgrimes return (1); 16511558Srgrimes } 16521558Srgrimes usedarg++; 16531558Srgrimes opt_flags |= OP_MASK; 16541558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 16551558Srgrimes !strcmp(cpopt, "n"))) { 165674462Salfred if (strchr(cpoptarg, '/') != NULL) { 165774462Salfred if (debug) 165874462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 165974462Salfred opt_flags |= OP_MASKLEN; 166074462Salfred } 16611558Srgrimes if (grp->gr_type != GT_NULL) { 166237663Scharnier syslog(LOG_ERR, "network/host conflict"); 16631558Srgrimes return (1); 16641558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 166537663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 16661558Srgrimes return (1); 16671558Srgrimes } 16681558Srgrimes grp->gr_type = GT_NET; 16691558Srgrimes *has_hostp = 1; 16701558Srgrimes usedarg++; 16711558Srgrimes opt_flags |= OP_NET; 16721558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 16731558Srgrimes opt_flags |= OP_ALLDIRS; 167427447Sdfr } else if (!strcmp(cpopt, "public")) { 167527447Sdfr *exflagsp |= MNT_EXPUBLIC; 167627447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 167727447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 167827447Sdfr opt_flags |= OP_MAPALL; 167927447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 168027447Sdfr ep->ex_indexfile = strdup(cpoptarg); 1681100336Sjoerg } else if (!strcmp(cpopt, "quiet")) { 1682100336Sjoerg opt_flags |= OP_QUIET; 16831558Srgrimes } else { 168437663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 16851558Srgrimes return (1); 16861558Srgrimes } 16871558Srgrimes if (usedarg >= 0) { 16881558Srgrimes *endcp = savedc2; 16891558Srgrimes **endcpp = savedc; 16901558Srgrimes if (usedarg > 0) { 16911558Srgrimes *cpp = cp; 16921558Srgrimes *endcpp = endcp; 16931558Srgrimes } 16941558Srgrimes return (0); 16951558Srgrimes } 16961558Srgrimes cpopt = cpoptend; 16971558Srgrimes } 16981558Srgrimes **endcpp = savedc; 16991558Srgrimes return (0); 17001558Srgrimes} 17011558Srgrimes 17021558Srgrimes/* 17031558Srgrimes * Translate a character string to the corresponding list of network 17041558Srgrimes * addresses for a hostname. 17051558Srgrimes */ 17061558Srgrimesint 17077401Swpaulget_host(cp, grp, tgrp) 17081558Srgrimes char *cp; 17091558Srgrimes struct grouplist *grp; 17107401Swpaul struct grouplist *tgrp; 17111558Srgrimes{ 17127401Swpaul struct grouplist *checkgrp; 171375635Siedowse struct addrinfo *ai, *tai, hints; 171474462Salfred int ecode; 171574462Salfred char host[NI_MAXHOST]; 17161558Srgrimes 171774462Salfred if (grp->gr_type != GT_NULL) { 171874462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 17191558Srgrimes return (1); 17201558Srgrimes } 172174462Salfred memset(&hints, 0, sizeof hints); 172274462Salfred hints.ai_flags = AI_CANONNAME; 172374462Salfred hints.ai_protocol = IPPROTO_UDP; 172474462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 172574462Salfred if (ecode != 0) { 172675635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 172774462Salfred return 1; 172874462Salfred } 172974462Salfred grp->gr_ptr.gt_addrinfo = ai; 173074462Salfred while (ai != NULL) { 173174462Salfred if (ai->ai_canonname == NULL) { 173274462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 1733146187Sume sizeof host, NULL, 0, NI_NUMERICHOST) != 0) 173474462Salfred strlcpy(host, "?", sizeof(host)); 173574462Salfred ai->ai_canonname = strdup(host); 173674462Salfred ai->ai_flags |= AI_CANONNAME; 173775641Siedowse } 173874462Salfred if (debug) 173975635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 174075635Siedowse /* 174175635Siedowse * Sanity check: make sure we don't already have an entry 174275635Siedowse * for this host in the grouplist. 174375635Siedowse */ 174475635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 174575635Siedowse checkgrp = checkgrp->gr_next) { 174675635Siedowse if (checkgrp->gr_type != GT_HOST) 174775635Siedowse continue; 174875635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 174975635Siedowse tai = tai->ai_next) { 175075801Siedowse if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0) 175175635Siedowse continue; 175275635Siedowse if (debug) 175375635Siedowse fprintf(stderr, 175475635Siedowse "ignoring duplicate host %s\n", 175575635Siedowse ai->ai_canonname); 175675635Siedowse grp->gr_type = GT_IGNORE; 175775635Siedowse return (0); 175875635Siedowse } 175975635Siedowse } 176074462Salfred ai = ai->ai_next; 17611558Srgrimes } 176275635Siedowse grp->gr_type = GT_HOST; 17631558Srgrimes return (0); 17641558Srgrimes} 17651558Srgrimes 17661558Srgrimes/* 17671558Srgrimes * Free up an exports list component 17681558Srgrimes */ 17691558Srgrimesvoid 17701558Srgrimesfree_exp(ep) 17711558Srgrimes struct exportlist *ep; 17721558Srgrimes{ 17731558Srgrimes 17741558Srgrimes if (ep->ex_defdir) { 17751558Srgrimes free_host(ep->ex_defdir->dp_hosts); 17761558Srgrimes free((caddr_t)ep->ex_defdir); 17771558Srgrimes } 17781558Srgrimes if (ep->ex_fsdir) 17791558Srgrimes free(ep->ex_fsdir); 178027447Sdfr if (ep->ex_indexfile) 178127447Sdfr free(ep->ex_indexfile); 17821558Srgrimes free_dir(ep->ex_dirl); 17831558Srgrimes free((caddr_t)ep); 17841558Srgrimes} 17851558Srgrimes 17861558Srgrimes/* 17871558Srgrimes * Free hosts. 17881558Srgrimes */ 17891558Srgrimesvoid 17901558Srgrimesfree_host(hp) 17911558Srgrimes struct hostlist *hp; 17921558Srgrimes{ 17931558Srgrimes struct hostlist *hp2; 17941558Srgrimes 17951558Srgrimes while (hp) { 17961558Srgrimes hp2 = hp; 17971558Srgrimes hp = hp->ht_next; 17981558Srgrimes free((caddr_t)hp2); 17991558Srgrimes } 18001558Srgrimes} 18011558Srgrimes 18021558Srgrimesstruct hostlist * 18031558Srgrimesget_ht() 18041558Srgrimes{ 18051558Srgrimes struct hostlist *hp; 18061558Srgrimes 18071558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 18081558Srgrimes if (hp == (struct hostlist *)NULL) 18091558Srgrimes out_of_mem(); 18101558Srgrimes hp->ht_next = (struct hostlist *)NULL; 18119336Sdfr hp->ht_flag = 0; 18121558Srgrimes return (hp); 18131558Srgrimes} 18141558Srgrimes 18151558Srgrimes/* 18161558Srgrimes * Out of memory, fatal 18171558Srgrimes */ 18181558Srgrimesvoid 18191558Srgrimesout_of_mem() 18201558Srgrimes{ 18211558Srgrimes 182237663Scharnier syslog(LOG_ERR, "out of memory"); 18231558Srgrimes exit(2); 18241558Srgrimes} 18251558Srgrimes 18261558Srgrimes/* 1827158857Srodrigc * Do the nmount() syscall with the update flag to push the export info into 18281558Srgrimes * the kernel. 18291558Srgrimes */ 18301558Srgrimesint 1831158857Srodrigcdo_mount(struct exportlist *ep, struct grouplist *grp, int exflags, 1832158857Srodrigc struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb) 18331558Srgrimes{ 183475841Siedowse struct statfs fsb1; 183574462Salfred struct addrinfo *ai; 1836158857Srodrigc struct export_args eap; 1837158857Srodrigc char errmsg[255]; 1838158857Srodrigc char *cp; 18391558Srgrimes int done; 1840158857Srodrigc char savedc; 1841158857Srodrigc struct iovec *iov; 1842158857Srodrigc int iovlen; 1843158857Srodrigc int ret; 18441558Srgrimes 1845158857Srodrigc cp = NULL; 1846158857Srodrigc savedc = '\0'; 1847158857Srodrigc iov = NULL; 1848158857Srodrigc iovlen = 0; 1849158857Srodrigc ret = 0; 185075801Siedowse 1851158857Srodrigc bzero(&eap, sizeof(eap)); 1852158857Srodrigc bzero(errmsg, sizeof(errmsg)); 1853158857Srodrigc eap.ex_flags = exflags; 1854158857Srodrigc eap.ex_anon = *anoncrp; 1855158857Srodrigc eap.ex_indexfile = ep->ex_indexfile; 185675641Siedowse if (grp->gr_type == GT_HOST) 185774462Salfred ai = grp->gr_ptr.gt_addrinfo; 185875641Siedowse else 185975641Siedowse ai = NULL; 18601558Srgrimes done = FALSE; 1861158857Srodrigc 1862158857Srodrigc build_iovec(&iov, &iovlen, "fstype", NULL, 0); 1863158857Srodrigc build_iovec(&iov, &iovlen, "fspath", NULL, 0); 1864158857Srodrigc build_iovec(&iov, &iovlen, "from", NULL, 0); 1865158857Srodrigc build_iovec(&iov, &iovlen, "update", NULL, 0); 1866158857Srodrigc build_iovec(&iov, &iovlen, "export", &eap, sizeof(eap)); 1867158857Srodrigc build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 1868158857Srodrigc 18691558Srgrimes while (!done) { 18701558Srgrimes switch (grp->gr_type) { 18711558Srgrimes case GT_HOST: 187275641Siedowse if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 187374462Salfred goto skip; 1874158857Srodrigc eap.ex_addr = ai->ai_addr; 1875158857Srodrigc eap.ex_addrlen = ai->ai_addrlen; 1876158857Srodrigc eap.ex_masklen = 0; 18771558Srgrimes break; 18781558Srgrimes case GT_NET: 187975801Siedowse if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 && 188074462Salfred have_v6 == 0) 188174462Salfred goto skip; 1882158857Srodrigc eap.ex_addr = 188375801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net; 1884158857Srodrigc eap.ex_addrlen = 1885158857Srodrigc ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len; 1886158857Srodrigc eap.ex_mask = 188775801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask; 1888158857Srodrigc eap.ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len; 18891558Srgrimes break; 189075641Siedowse case GT_DEFAULT: 1891158857Srodrigc eap.ex_addr = NULL; 1892158857Srodrigc eap.ex_addrlen = 0; 1893158857Srodrigc eap.ex_mask = NULL; 1894158857Srodrigc eap.ex_masklen = 0; 189575641Siedowse break; 18967401Swpaul case GT_IGNORE: 1897158857Srodrigc ret = 0; 1898158857Srodrigc goto error_exit; 18997401Swpaul break; 19001558Srgrimes default: 190137663Scharnier syslog(LOG_ERR, "bad grouptype"); 19021558Srgrimes if (cp) 19031558Srgrimes *cp = savedc; 1904158857Srodrigc ret = 1; 1905158857Srodrigc goto error_exit; 19061558Srgrimes }; 19071558Srgrimes 19081558Srgrimes /* 19091558Srgrimes * XXX: 19101558Srgrimes * Maybe I should just use the fsb->f_mntonname path instead 19111558Srgrimes * of looping back up the dirp to the mount point?? 19121558Srgrimes * Also, needs to know how to export all types of local 191396707Strhodes * exportable filesystems and not just "ufs". 19141558Srgrimes */ 1915158857Srodrigc iov[1].iov_base = fsb->f_fstypename; /* "fstype" */ 1916158857Srodrigc iov[1].iov_len = strlen(fsb->f_fstypename) + 1; 1917158857Srodrigc iov[3].iov_base = fsb->f_mntonname; /* "fspath" */ 1918158857Srodrigc iov[3].iov_len = strlen(fsb->f_mntonname) + 1; 1919158857Srodrigc iov[5].iov_base = fsb->f_mntfromname; /* "from" */ 1920158857Srodrigc iov[5].iov_len = strlen(fsb->f_mntfromname) + 1; 1921158857Srodrigc 1922163512Srink /* 1923163512Srink * Remount the filesystem, but chop off the MNT_ROOTFS flag 1924163512Srink * as it is used internally (and will result in an error if 1925163512Srink * specified) 1926163512Srink */ 1927163512Srink while (nmount(iov, iovlen, fsb->f_flags & ~MNT_ROOTFS) < 0) { 19281558Srgrimes if (cp) 19291558Srgrimes *cp-- = savedc; 19301558Srgrimes else 19311558Srgrimes cp = dirp + dirplen - 1; 1932158857Srodrigc if (opt_flags & OP_QUIET) { 1933158857Srodrigc ret = 1; 1934158857Srodrigc goto error_exit; 1935158857Srodrigc } 19361558Srgrimes if (errno == EPERM) { 193775635Siedowse if (debug) 193875635Siedowse warnx("can't change attributes for %s", 193975635Siedowse dirp); 19401558Srgrimes syslog(LOG_ERR, 194137663Scharnier "can't change attributes for %s", dirp); 1942158857Srodrigc ret = 1; 1943158857Srodrigc goto error_exit; 19441558Srgrimes } 19451558Srgrimes if (opt_flags & OP_ALLDIRS) { 1946100336Sjoerg if (errno == EINVAL) 1947100336Sjoerg syslog(LOG_ERR, 1948100336Sjoerg "-alldirs requested but %s is not a filesystem mountpoint", 1949100336Sjoerg dirp); 1950100336Sjoerg else 1951100336Sjoerg syslog(LOG_ERR, 1952100336Sjoerg "could not remount %s: %m", 1953100336Sjoerg dirp); 1954158857Srodrigc ret = 1; 1955158857Srodrigc goto error_exit; 19561558Srgrimes } 19571558Srgrimes /* back up over the last component */ 19581558Srgrimes while (*cp == '/' && cp > dirp) 19591558Srgrimes cp--; 19601558Srgrimes while (*(cp - 1) != '/' && cp > dirp) 19611558Srgrimes cp--; 19621558Srgrimes if (cp == dirp) { 19631558Srgrimes if (debug) 196437663Scharnier warnx("mnt unsucc"); 196537663Scharnier syslog(LOG_ERR, "can't export %s", dirp); 1966158857Srodrigc ret = 1; 1967158857Srodrigc goto error_exit; 19681558Srgrimes } 19691558Srgrimes savedc = *cp; 19701558Srgrimes *cp = '\0'; 197175841Siedowse /* Check that we're still on the same filesystem. */ 197275841Siedowse if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid, 197375841Siedowse &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) { 197475841Siedowse *cp = savedc; 197575841Siedowse syslog(LOG_ERR, "can't export %s", dirp); 1976158857Srodrigc ret = 1; 1977158857Srodrigc goto error_exit; 197875841Siedowse } 19791558Srgrimes } 198074462Salfredskip: 198175641Siedowse if (ai != NULL) 198274462Salfred ai = ai->ai_next; 198375641Siedowse if (ai == NULL) 19841558Srgrimes done = TRUE; 19851558Srgrimes } 19861558Srgrimes if (cp) 19871558Srgrimes *cp = savedc; 1988158857Srodrigcerror_exit: 1989158857Srodrigc /* free strings allocated by strdup() in getmntopts.c */ 1990158857Srodrigc if (iov != NULL) { 1991158857Srodrigc free(iov[0].iov_base); /* fstype */ 1992158857Srodrigc free(iov[2].iov_base); /* fspath */ 1993158857Srodrigc free(iov[4].iov_base); /* from */ 1994158857Srodrigc free(iov[6].iov_base); /* update */ 1995158857Srodrigc free(iov[8].iov_base); /* export */ 1996158857Srodrigc free(iov[10].iov_base); /* errmsg */ 1997158857Srodrigc 1998158857Srodrigc /* free iov, allocated by realloc() */ 1999158857Srodrigc free(iov); 2000158857Srodrigc } 2001158857Srodrigc return (ret); 20021558Srgrimes} 20031558Srgrimes 20041558Srgrimes/* 20051558Srgrimes * Translate a net address. 200675801Siedowse * 200775801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address. 20081558Srgrimes */ 20091558Srgrimesint 20101558Srgrimesget_net(cp, net, maskflg) 20111558Srgrimes char *cp; 20121558Srgrimes struct netmsk *net; 20131558Srgrimes int maskflg; 20141558Srgrimes{ 201575861Siedowse struct netent *np = NULL; 201674462Salfred char *name, *p, *prefp; 201775801Siedowse struct sockaddr_in sin; 201875861Siedowse struct sockaddr *sa = NULL; 201974462Salfred struct addrinfo hints, *ai = NULL; 202074462Salfred char netname[NI_MAXHOST]; 202174462Salfred long preflen; 20221558Srgrimes 202375635Siedowse p = prefp = NULL; 202474462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 202574462Salfred p = strchr(cp, '/'); 202674462Salfred *p = '\0'; 202774462Salfred prefp = p + 1; 202874462Salfred } 202974462Salfred 203075861Siedowse /* 203175861Siedowse * Check for a numeric address first. We wish to avoid 203275861Siedowse * possible DNS lookups in getnetbyname(). 203375861Siedowse */ 203475861Siedowse if (isxdigit(*cp) || *cp == ':') { 203574462Salfred memset(&hints, 0, sizeof hints); 203675801Siedowse /* Ensure the mask and the network have the same family. */ 203775801Siedowse if (maskflg && (opt_flags & OP_NET)) 203875801Siedowse hints.ai_family = net->nt_net.ss_family; 203975801Siedowse else if (!maskflg && (opt_flags & OP_HAVEMASK)) 204075801Siedowse hints.ai_family = net->nt_mask.ss_family; 204175801Siedowse else 204275801Siedowse hints.ai_family = AF_UNSPEC; 204374462Salfred hints.ai_flags = AI_NUMERICHOST; 204475861Siedowse if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 204575861Siedowse sa = ai->ai_addr; 204675861Siedowse if (sa != NULL && ai->ai_family == AF_INET) { 204774462Salfred /* 204875801Siedowse * The address in `cp' is really a network address, so 204975801Siedowse * use inet_network() to re-interpret this correctly. 205075801Siedowse * e.g. "127.1" means 127.1.0.0, not 127.0.0.1. 205174462Salfred */ 205275801Siedowse bzero(&sin, sizeof sin); 205374462Salfred sin.sin_family = AF_INET; 205474462Salfred sin.sin_len = sizeof sin; 205575801Siedowse sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 205674462Salfred if (debug) 205775801Siedowse fprintf(stderr, "get_net: v4 addr %s\n", 205875801Siedowse inet_ntoa(sin.sin_addr)); 205974462Salfred sa = (struct sockaddr *)&sin; 206075861Siedowse } 206175861Siedowse } 206275861Siedowse if (sa == NULL && (np = getnetbyname(cp)) != NULL) { 206375861Siedowse bzero(&sin, sizeof sin); 206475861Siedowse sin.sin_family = AF_INET; 206575861Siedowse sin.sin_len = sizeof sin; 206675861Siedowse sin.sin_addr = inet_makeaddr(np->n_net, 0); 206775861Siedowse sa = (struct sockaddr *)&sin; 206875861Siedowse } 206975861Siedowse if (sa == NULL) 207074462Salfred goto fail; 207125318Spst 207275801Siedowse if (maskflg) { 207375801Siedowse /* The specified sockaddr is a mask. */ 207475801Siedowse if (checkmask(sa) != 0) 207575801Siedowse goto fail; 207675801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 207775801Siedowse opt_flags |= OP_HAVEMASK; 207875801Siedowse } else { 207975801Siedowse /* The specified sockaddr is a network address. */ 208075801Siedowse bcopy(sa, &net->nt_net, sa->sa_len); 208174462Salfred 208275801Siedowse /* Get a network name for the export list. */ 208375801Siedowse if (np) { 208475801Siedowse name = np->n_name; 208575801Siedowse } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 2086146187Sume NULL, 0, NI_NUMERICHOST) == 0) { 208775801Siedowse name = netname; 208875801Siedowse } else { 208975801Siedowse goto fail; 209075801Siedowse } 209175801Siedowse if ((net->nt_name = strdup(name)) == NULL) 209275801Siedowse out_of_mem(); 209375801Siedowse 209475801Siedowse /* 209575801Siedowse * Extract a mask from either a "/<masklen>" suffix, or 209675801Siedowse * from the class of an IPv4 address. 209775801Siedowse */ 209874462Salfred if (opt_flags & OP_MASKLEN) { 209974462Salfred preflen = strtol(prefp, NULL, 10); 210075801Siedowse if (preflen < 0L || preflen == LONG_MAX) 210174462Salfred goto fail; 210275801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 210375801Siedowse if (makemask(&net->nt_mask, (int)preflen) != 0) 210475801Siedowse goto fail; 210575801Siedowse opt_flags |= OP_HAVEMASK; 210674462Salfred *p = '/'; 210775801Siedowse } else if (sa->sa_family == AF_INET && 210875801Siedowse (opt_flags & OP_MASK) == 0) { 210975801Siedowse in_addr_t addr; 211074462Salfred 211175801Siedowse addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 211275801Siedowse if (IN_CLASSA(addr)) 211375801Siedowse preflen = 8; 211475801Siedowse else if (IN_CLASSB(addr)) 211575801Siedowse preflen = 16; 211675801Siedowse else if (IN_CLASSC(addr)) 211775801Siedowse preflen = 24; 211875801Siedowse else if (IN_CLASSD(addr)) 211975801Siedowse preflen = 28; 212075801Siedowse else 212175801Siedowse preflen = 32; /* XXX */ 212275801Siedowse 212375801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 212475801Siedowse makemask(&net->nt_mask, (int)preflen); 212575801Siedowse opt_flags |= OP_HAVEMASK; 212674462Salfred } 212774462Salfred } 212874462Salfred 212974462Salfred if (ai) 213074462Salfred freeaddrinfo(ai); 213174462Salfred return 0; 213274462Salfred 213374462Salfredfail: 213474462Salfred if (ai) 213574462Salfred freeaddrinfo(ai); 213674462Salfred return 1; 21371558Srgrimes} 21381558Srgrimes 21391558Srgrimes/* 21401558Srgrimes * Parse out the next white space separated field 21411558Srgrimes */ 21421558Srgrimesvoid 21431558Srgrimesnextfield(cp, endcp) 21441558Srgrimes char **cp; 21451558Srgrimes char **endcp; 21461558Srgrimes{ 21471558Srgrimes char *p; 21481558Srgrimes 21491558Srgrimes p = *cp; 21501558Srgrimes while (*p == ' ' || *p == '\t') 21511558Srgrimes p++; 21521558Srgrimes if (*p == '\n' || *p == '\0') 21531558Srgrimes *cp = *endcp = p; 21541558Srgrimes else { 21551558Srgrimes *cp = p++; 21561558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 21571558Srgrimes p++; 21581558Srgrimes *endcp = p; 21591558Srgrimes } 21601558Srgrimes} 21611558Srgrimes 21621558Srgrimes/* 21631558Srgrimes * Get an exports file line. Skip over blank lines and handle line 21641558Srgrimes * continuations. 21651558Srgrimes */ 21661558Srgrimesint 21671558Srgrimesget_line() 21681558Srgrimes{ 21691558Srgrimes char *p, *cp; 217096622Siedowse size_t len; 21711558Srgrimes int totlen, cont_line; 21721558Srgrimes 21731558Srgrimes /* 21741558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 21751558Srgrimes */ 21761558Srgrimes p = line; 21771558Srgrimes totlen = 0; 21781558Srgrimes do { 217996622Siedowse if ((p = fgetln(exp_file, &len)) == NULL) 21801558Srgrimes return (0); 21811558Srgrimes cp = p + len - 1; 21821558Srgrimes cont_line = 0; 21831558Srgrimes while (cp >= p && 21841558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 21851558Srgrimes if (*cp == '\\') 21861558Srgrimes cont_line = 1; 21871558Srgrimes cp--; 21881558Srgrimes len--; 21891558Srgrimes } 219079117Sdd if (cont_line) { 219179117Sdd *++cp = ' '; 219279117Sdd len++; 219379117Sdd } 219496622Siedowse if (linesize < len + totlen + 1) { 219596622Siedowse linesize = len + totlen + 1; 219696622Siedowse line = realloc(line, linesize); 219796622Siedowse if (line == NULL) 219896622Siedowse out_of_mem(); 21991558Srgrimes } 220096622Siedowse memcpy(line + totlen, p, len); 220196622Siedowse totlen += len; 220296622Siedowse line[totlen] = '\0'; 22031558Srgrimes } while (totlen == 0 || cont_line); 22041558Srgrimes return (1); 22051558Srgrimes} 22061558Srgrimes 22071558Srgrimes/* 22081558Srgrimes * Parse a description of a credential. 22091558Srgrimes */ 22101558Srgrimesvoid 22111558Srgrimesparsecred(namelist, cr) 22121558Srgrimes char *namelist; 221372650Sgreen struct xucred *cr; 22141558Srgrimes{ 22151558Srgrimes char *name; 22161558Srgrimes int cnt; 22171558Srgrimes char *names; 22181558Srgrimes struct passwd *pw; 22191558Srgrimes struct group *gr; 2220136051Sstefanf gid_t groups[NGROUPS + 1]; 2221136051Sstefanf int ngroups; 22221558Srgrimes 222391354Sdd cr->cr_version = XUCRED_VERSION; 22241558Srgrimes /* 222537663Scharnier * Set up the unprivileged user. 22261558Srgrimes */ 22271558Srgrimes cr->cr_uid = -2; 22281558Srgrimes cr->cr_groups[0] = -2; 22291558Srgrimes cr->cr_ngroups = 1; 22301558Srgrimes /* 22311558Srgrimes * Get the user's password table entry. 22321558Srgrimes */ 22331558Srgrimes names = strsep(&namelist, " \t\n"); 22341558Srgrimes name = strsep(&names, ":"); 22351558Srgrimes if (isdigit(*name) || *name == '-') 22361558Srgrimes pw = getpwuid(atoi(name)); 22371558Srgrimes else 22381558Srgrimes pw = getpwnam(name); 22391558Srgrimes /* 22401558Srgrimes * Credentials specified as those of a user. 22411558Srgrimes */ 22421558Srgrimes if (names == NULL) { 22431558Srgrimes if (pw == NULL) { 224437663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 22451558Srgrimes return; 22461558Srgrimes } 22471558Srgrimes cr->cr_uid = pw->pw_uid; 22481558Srgrimes ngroups = NGROUPS + 1; 22491558Srgrimes if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 225037663Scharnier syslog(LOG_ERR, "too many groups"); 22511558Srgrimes /* 2252136051Sstefanf * Compress out duplicate. 22531558Srgrimes */ 22541558Srgrimes cr->cr_ngroups = ngroups - 1; 22551558Srgrimes cr->cr_groups[0] = groups[0]; 22561558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 22571558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 22581558Srgrimes return; 22591558Srgrimes } 22601558Srgrimes /* 22611558Srgrimes * Explicit credential specified as a colon separated list: 22621558Srgrimes * uid:gid:gid:... 22631558Srgrimes */ 22641558Srgrimes if (pw != NULL) 22651558Srgrimes cr->cr_uid = pw->pw_uid; 22661558Srgrimes else if (isdigit(*name) || *name == '-') 22671558Srgrimes cr->cr_uid = atoi(name); 22681558Srgrimes else { 226937663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 22701558Srgrimes return; 22711558Srgrimes } 22721558Srgrimes cr->cr_ngroups = 0; 22731558Srgrimes while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) { 22741558Srgrimes name = strsep(&names, ":"); 22751558Srgrimes if (isdigit(*name) || *name == '-') { 22761558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 22771558Srgrimes } else { 22781558Srgrimes if ((gr = getgrnam(name)) == NULL) { 227937663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 22801558Srgrimes continue; 22811558Srgrimes } 22821558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 22831558Srgrimes } 22841558Srgrimes } 22851558Srgrimes if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS) 228637663Scharnier syslog(LOG_ERR, "too many groups"); 22871558Srgrimes} 22881558Srgrimes 22891558Srgrimes#define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50) 22901558Srgrimes/* 22911558Srgrimes * Routines that maintain the remote mounttab 22921558Srgrimes */ 22931558Srgrimesvoid 22941558Srgrimesget_mountlist() 22951558Srgrimes{ 22961558Srgrimes struct mountlist *mlp, **mlpp; 229723681Speter char *host, *dirp, *cp; 22981558Srgrimes char str[STRSIZ]; 22991558Srgrimes FILE *mlfile; 23001558Srgrimes 23011558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 230253117Sbillf if (errno == ENOENT) 230353117Sbillf return; 230453117Sbillf else { 230553117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 230653117Sbillf return; 230753117Sbillf } 23081558Srgrimes } 23091558Srgrimes mlpp = &mlhead; 23101558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 231123681Speter cp = str; 231223681Speter host = strsep(&cp, " \t\n"); 231323681Speter dirp = strsep(&cp, " \t\n"); 231423681Speter if (host == NULL || dirp == NULL) 23151558Srgrimes continue; 23161558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 231737663Scharnier if (mlp == (struct mountlist *)NULL) 231837663Scharnier out_of_mem(); 231923681Speter strncpy(mlp->ml_host, host, RPCMNT_NAMELEN); 232023681Speter mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 232123681Speter strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 232223681Speter mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 23231558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 23241558Srgrimes *mlpp = mlp; 23251558Srgrimes mlpp = &mlp->ml_next; 23261558Srgrimes } 23271558Srgrimes fclose(mlfile); 23281558Srgrimes} 23291558Srgrimes 233075635Siedowsevoid 233175635Siedowsedel_mlist(char *hostp, char *dirp) 23321558Srgrimes{ 23331558Srgrimes struct mountlist *mlp, **mlpp; 23341558Srgrimes struct mountlist *mlp2; 23351558Srgrimes FILE *mlfile; 23361558Srgrimes int fnd = 0; 23371558Srgrimes 23381558Srgrimes mlpp = &mlhead; 23391558Srgrimes mlp = mlhead; 23401558Srgrimes while (mlp) { 23411558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 23421558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 23431558Srgrimes fnd = 1; 23441558Srgrimes mlp2 = mlp; 23451558Srgrimes *mlpp = mlp = mlp->ml_next; 23461558Srgrimes free((caddr_t)mlp2); 23471558Srgrimes } else { 23481558Srgrimes mlpp = &mlp->ml_next; 23491558Srgrimes mlp = mlp->ml_next; 23501558Srgrimes } 23511558Srgrimes } 23521558Srgrimes if (fnd) { 23531558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 235437663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 23551558Srgrimes return; 23561558Srgrimes } 23571558Srgrimes mlp = mlhead; 23581558Srgrimes while (mlp) { 23591558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 23601558Srgrimes mlp = mlp->ml_next; 23611558Srgrimes } 23621558Srgrimes fclose(mlfile); 23631558Srgrimes } 23641558Srgrimes} 23651558Srgrimes 23661558Srgrimesvoid 23671558Srgrimesadd_mlist(hostp, dirp) 23681558Srgrimes char *hostp, *dirp; 23691558Srgrimes{ 23701558Srgrimes struct mountlist *mlp, **mlpp; 23711558Srgrimes FILE *mlfile; 23721558Srgrimes 23731558Srgrimes mlpp = &mlhead; 23741558Srgrimes mlp = mlhead; 23751558Srgrimes while (mlp) { 23761558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 23771558Srgrimes return; 23781558Srgrimes mlpp = &mlp->ml_next; 23791558Srgrimes mlp = mlp->ml_next; 23801558Srgrimes } 23811558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 238237663Scharnier if (mlp == (struct mountlist *)NULL) 238337663Scharnier out_of_mem(); 23841558Srgrimes strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN); 23851558Srgrimes mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 23861558Srgrimes strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 23871558Srgrimes mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 23881558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 23891558Srgrimes *mlpp = mlp; 23901558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 239137663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 23921558Srgrimes return; 23931558Srgrimes } 23941558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 23951558Srgrimes fclose(mlfile); 23961558Srgrimes} 23971558Srgrimes 23981558Srgrimes/* 23991558Srgrimes * Free up a group list. 24001558Srgrimes */ 24011558Srgrimesvoid 24021558Srgrimesfree_grp(grp) 24031558Srgrimes struct grouplist *grp; 24041558Srgrimes{ 24051558Srgrimes if (grp->gr_type == GT_HOST) { 240674462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 240774462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 24081558Srgrimes } else if (grp->gr_type == GT_NET) { 24091558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 24101558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 24111558Srgrimes } 24121558Srgrimes free((caddr_t)grp); 24131558Srgrimes} 24141558Srgrimes 24151558Srgrimes#ifdef DEBUG 24161558Srgrimesvoid 24171558SrgrimesSYSLOG(int pri, const char *fmt, ...) 24181558Srgrimes{ 24191558Srgrimes va_list ap; 24201558Srgrimes 24211558Srgrimes va_start(ap, fmt); 24221558Srgrimes vfprintf(stderr, fmt, ap); 24231558Srgrimes va_end(ap); 24241558Srgrimes} 24251558Srgrimes#endif /* DEBUG */ 24261558Srgrimes 24271558Srgrimes/* 24281558Srgrimes * Check options for consistency. 24291558Srgrimes */ 24301558Srgrimesint 24311558Srgrimescheck_options(dp) 24321558Srgrimes struct dirlist *dp; 24331558Srgrimes{ 24341558Srgrimes 24351558Srgrimes if (dp == (struct dirlist *)NULL) 24361558Srgrimes return (1); 243783653Speter if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) { 243883653Speter syslog(LOG_ERR, "-mapall and -maproot mutually exclusive"); 24391558Srgrimes return (1); 24401558Srgrimes } 24411558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 244275801Siedowse syslog(LOG_ERR, "-mask requires -network"); 244375801Siedowse return (1); 24441558Srgrimes } 244575801Siedowse if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) { 244675801Siedowse syslog(LOG_ERR, "-network requires mask specification"); 244775801Siedowse return (1); 244875801Siedowse } 244975801Siedowse if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) { 245075801Siedowse syslog(LOG_ERR, "-mask and /masklen are mutually exclusive"); 245175801Siedowse return (1); 245275801Siedowse } 24531558Srgrimes if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 245445927Salex syslog(LOG_ERR, "-alldirs has multiple directories"); 24551558Srgrimes return (1); 24561558Srgrimes } 24571558Srgrimes return (0); 24581558Srgrimes} 24591558Srgrimes 24601558Srgrimes/* 24611558Srgrimes * Check an absolute directory path for any symbolic links. Return true 24621558Srgrimes */ 24631558Srgrimesint 24641558Srgrimescheck_dirpath(dirp) 24651558Srgrimes char *dirp; 24661558Srgrimes{ 24671558Srgrimes char *cp; 24681558Srgrimes int ret = 1; 24691558Srgrimes struct stat sb; 24701558Srgrimes 24711558Srgrimes cp = dirp + 1; 24721558Srgrimes while (*cp && ret) { 24731558Srgrimes if (*cp == '/') { 24741558Srgrimes *cp = '\0'; 24759336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 24761558Srgrimes ret = 0; 24771558Srgrimes *cp = '/'; 24781558Srgrimes } 24791558Srgrimes cp++; 24801558Srgrimes } 24819336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 24821558Srgrimes ret = 0; 24831558Srgrimes return (ret); 24841558Srgrimes} 24859336Sdfr 248675801Siedowse/* 248775801Siedowse * Make a netmask according to the specified prefix length. The ss_family 248875801Siedowse * and other non-address fields must be initialised before calling this. 248975801Siedowse */ 249075801Siedowseint 249175801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen) 249274462Salfred{ 249375801Siedowse u_char *p; 249475801Siedowse int bits, i, len; 249574462Salfred 249675801Siedowse if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL) 249775801Siedowse return (-1); 2498103949Smike if (bitlen > len * CHAR_BIT) 249975801Siedowse return (-1); 250074462Salfred 250175801Siedowse for (i = 0; i < len; i++) { 2502103949Smike bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen; 250375801Siedowse *p++ = (1 << bits) - 1; 250475801Siedowse bitlen -= bits; 250574462Salfred } 250675801Siedowse return 0; 250774462Salfred} 250874462Salfred 250975801Siedowse/* 251075801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask 251175801Siedowse * is acceptable (i.e. of the form 1...10....0). 251275801Siedowse */ 251375801Siedowseint 251475801Siedowsecheckmask(struct sockaddr *sa) 251574462Salfred{ 251675801Siedowse u_char *mask; 251775801Siedowse int i, len; 251874462Salfred 251975801Siedowse if ((mask = sa_rawaddr(sa, &len)) == NULL) 252075801Siedowse return (-1); 252175801Siedowse 252275801Siedowse for (i = 0; i < len; i++) 252375801Siedowse if (mask[i] != 0xff) 252475801Siedowse break; 252575801Siedowse if (i < len) { 252675801Siedowse if (~mask[i] & (u_char)(~mask[i] + 1)) 252775801Siedowse return (-1); 252875801Siedowse i++; 252974462Salfred } 253075801Siedowse for (; i < len; i++) 253175801Siedowse if (mask[i] != 0) 253275801Siedowse return (-1); 253375801Siedowse return (0); 253474462Salfred} 253574462Salfred 253675801Siedowse/* 253775801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if 253875801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'. 253975801Siedowse * If samask is NULL, perform a full comparision. 254075801Siedowse */ 254175801Siedowseint 254275801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask) 254374462Salfred{ 254475801Siedowse unsigned char *p1, *p2, *mask; 254575801Siedowse int len, i; 254674462Salfred 254775801Siedowse if (sa1->sa_family != sa2->sa_family || 254875801Siedowse (p1 = sa_rawaddr(sa1, &len)) == NULL || 254975801Siedowse (p2 = sa_rawaddr(sa2, NULL)) == NULL) 255075801Siedowse return (1); 255175801Siedowse 255275801Siedowse switch (sa1->sa_family) { 255374462Salfred case AF_INET6: 255475801Siedowse if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 255575801Siedowse ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 255675801Siedowse return (1); 255774462Salfred break; 255874462Salfred } 255974462Salfred 256075801Siedowse /* Simple binary comparison if no mask specified. */ 256175801Siedowse if (samask == NULL) 256275801Siedowse return (memcmp(p1, p2, len)); 256374462Salfred 256475801Siedowse /* Set up the mask, and do a mask-based comparison. */ 256575801Siedowse if (sa1->sa_family != samask->sa_family || 256675801Siedowse (mask = sa_rawaddr(samask, NULL)) == NULL) 256775801Siedowse return (1); 256874462Salfred 256975801Siedowse for (i = 0; i < len; i++) 257075801Siedowse if ((p1[i] & mask[i]) != (p2[i] & mask[i])) 257175801Siedowse return (1); 257275801Siedowse return (0); 257374462Salfred} 257474462Salfred 257575801Siedowse/* 257675801Siedowse * Return a pointer to the part of the sockaddr that contains the 257775801Siedowse * raw address, and set *nbytes to its length in bytes. Returns 257875801Siedowse * NULL if the address family is unknown. 257975801Siedowse */ 258075801Siedowsevoid * 258175801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) { 258275801Siedowse void *p; 258374462Salfred int len; 258474462Salfred 258575801Siedowse switch (sa->sa_family) { 258674462Salfred case AF_INET: 258775801Siedowse len = sizeof(((struct sockaddr_in *)sa)->sin_addr); 258875801Siedowse p = &((struct sockaddr_in *)sa)->sin_addr; 258974462Salfred break; 259074462Salfred case AF_INET6: 259175801Siedowse len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); 259275801Siedowse p = &((struct sockaddr_in6 *)sa)->sin6_addr; 259374462Salfred break; 259474462Salfred default: 259575801Siedowse p = NULL; 259675801Siedowse len = 0; 259774462Salfred } 259874462Salfred 259975801Siedowse if (nbytes != NULL) 260075801Siedowse *nbytes = len; 260175801Siedowse return (p); 260274462Salfred} 260374462Salfred 260475754Siedowsevoid 260575754Siedowsehuphandler(int sig) 260675754Siedowse{ 260775754Siedowse got_sighup = 1; 260875754Siedowse} 260975754Siedowse 261074462Salfredvoid terminate(sig) 261174462Salfredint sig; 261274462Salfred{ 2613149433Spjd pidfile_remove(pfh); 261474792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 261574792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 261674462Salfred exit (0); 261774462Salfred} 2618