mountd.c revision 192993
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 192993 2009-05-28 20:28:13Z rmacklem $"); 47105267Scharnier 481558Srgrimes#include <sys/param.h> 49192934Srmacklem#include <sys/fcntl.h> 50192934Srmacklem#include <sys/linker.h> 51192934Srmacklem#include <sys/module.h> 521558Srgrimes#include <sys/mount.h> 531558Srgrimes#include <sys/stat.h> 54192934Srmacklem#include <sys/sysctl.h> 551558Srgrimes#include <sys/syslog.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> 64192934Srmacklem#include <nfs/nfssvc.h> 6583653Speter#include <nfsserver/nfs.h> 661558Srgrimes 67192934Srmacklem#include <fs/nfs/nfsport.h> 68192934Srmacklem 691558Srgrimes#include <arpa/inet.h> 701558Srgrimes 711558Srgrimes#include <ctype.h> 7237663Scharnier#include <err.h> 731558Srgrimes#include <errno.h> 741558Srgrimes#include <grp.h> 75149433Spjd#include <libutil.h> 76103949Smike#include <limits.h> 771558Srgrimes#include <netdb.h> 781558Srgrimes#include <pwd.h> 791558Srgrimes#include <signal.h> 801558Srgrimes#include <stdio.h> 811558Srgrimes#include <stdlib.h> 821558Srgrimes#include <string.h> 831558Srgrimes#include <unistd.h> 841558Srgrimes#include "pathnames.h" 85158857Srodrigc#include "mntopts.h" 861558Srgrimes 871558Srgrimes#ifdef DEBUG 881558Srgrimes#include <stdarg.h> 891558Srgrimes#endif 901558Srgrimes 911558Srgrimes/* 921558Srgrimes * Structures for keeping the mount list and export list 931558Srgrimes */ 941558Srgrimesstruct mountlist { 951558Srgrimes struct mountlist *ml_next; 961558Srgrimes char ml_host[RPCMNT_NAMELEN+1]; 971558Srgrimes char ml_dirp[RPCMNT_PATHLEN+1]; 981558Srgrimes}; 991558Srgrimes 1001558Srgrimesstruct dirlist { 1011558Srgrimes struct dirlist *dp_left; 1021558Srgrimes struct dirlist *dp_right; 1031558Srgrimes int dp_flag; 1041558Srgrimes struct hostlist *dp_hosts; /* List of hosts this dir exported to */ 1051558Srgrimes char dp_dirp[1]; /* Actually malloc'd to size of dir */ 1061558Srgrimes}; 1071558Srgrimes/* dp_flag bits */ 1081558Srgrimes#define DP_DEFSET 0x1 1099336Sdfr#define DP_HOSTSET 0x2 1101558Srgrimes 1111558Srgrimesstruct exportlist { 1121558Srgrimes struct exportlist *ex_next; 1131558Srgrimes struct dirlist *ex_dirl; 1141558Srgrimes struct dirlist *ex_defdir; 1151558Srgrimes int ex_flag; 1161558Srgrimes fsid_t ex_fs; 1171558Srgrimes char *ex_fsdir; 11827447Sdfr char *ex_indexfile; 119184588Sdfr int ex_numsecflavors; 120184588Sdfr int ex_secflavors[MAXSECFLAVORS]; 1211558Srgrimes}; 1221558Srgrimes/* ex_flag bits */ 1231558Srgrimes#define EX_LINKED 0x1 1241558Srgrimes 1251558Srgrimesstruct netmsk { 12674462Salfred struct sockaddr_storage nt_net; 12775801Siedowse struct sockaddr_storage nt_mask; 12842144Sdfr char *nt_name; 1291558Srgrimes}; 1301558Srgrimes 1311558Srgrimesunion grouptypes { 13274462Salfred struct addrinfo *gt_addrinfo; 1331558Srgrimes struct netmsk gt_net; 1341558Srgrimes}; 1351558Srgrimes 1361558Srgrimesstruct grouplist { 1371558Srgrimes int gr_type; 1381558Srgrimes union grouptypes gr_ptr; 1391558Srgrimes struct grouplist *gr_next; 1401558Srgrimes}; 1411558Srgrimes/* Group types */ 1421558Srgrimes#define GT_NULL 0x0 1431558Srgrimes#define GT_HOST 0x1 1441558Srgrimes#define GT_NET 0x2 14575641Siedowse#define GT_DEFAULT 0x3 1467401Swpaul#define GT_IGNORE 0x5 1471558Srgrimes 1481558Srgrimesstruct hostlist { 1499336Sdfr int ht_flag; /* Uses DP_xx bits */ 1501558Srgrimes struct grouplist *ht_grp; 1511558Srgrimes struct hostlist *ht_next; 1521558Srgrimes}; 1531558Srgrimes 1549336Sdfrstruct fhreturn { 1559336Sdfr int fhr_flag; 1569336Sdfr int fhr_vers; 1579336Sdfr nfsfh_t fhr_fh; 158184588Sdfr int fhr_numsecflavors; 159184588Sdfr int *fhr_secflavors; 1609336Sdfr}; 1619336Sdfr 1621558Srgrimes/* Global defs */ 16392882Simpchar *add_expdir(struct dirlist **, char *, int); 16492882Simpvoid add_dlist(struct dirlist **, struct dirlist *, 16592882Simp struct grouplist *, int); 16692882Simpvoid add_mlist(char *, char *); 16792882Simpint check_dirpath(char *); 16892882Simpint check_options(struct dirlist *); 16975801Siedowseint checkmask(struct sockaddr *sa); 17092882Simpint chk_host(struct dirlist *, struct sockaddr *, int *, int *); 171172827Smatteovoid create_service(struct netconfig *nconf); 17275635Siedowsevoid del_mlist(char *hostp, char *dirp); 17392882Simpstruct dirlist *dirp_search(struct dirlist *, char *); 17492882Simpint do_mount(struct exportlist *, struct grouplist *, int, 17592882Simp struct xucred *, char *, int, struct statfs *); 17692882Simpint do_opt(char **, char **, struct exportlist *, struct grouplist *, 17792882Simp int *, int *, struct xucred *); 17892882Simpstruct exportlist *ex_search(fsid_t *); 17992882Simpstruct exportlist *get_exp(void); 18092882Simpvoid free_dir(struct dirlist *); 18192882Simpvoid free_exp(struct exportlist *); 18292882Simpvoid free_grp(struct grouplist *); 18392882Simpvoid free_host(struct hostlist *); 18492882Simpvoid get_exportlist(void); 18592882Simpint get_host(char *, struct grouplist *, struct grouplist *); 18692882Simpstruct hostlist *get_ht(void); 18792882Simpint get_line(void); 18892882Simpvoid get_mountlist(void); 18992882Simpint get_net(char *, struct netmsk *, int); 19092882Simpvoid getexp_err(struct exportlist *, struct grouplist *); 19192882Simpstruct grouplist *get_grp(void); 19292882Simpvoid hang_dirp(struct dirlist *, struct grouplist *, 19392882Simp struct exportlist *, int); 19475754Siedowsevoid huphandler(int sig); 19575801Siedowseint makemask(struct sockaddr_storage *ssp, int bitlen); 19692882Simpvoid mntsrv(struct svc_req *, SVCXPRT *); 19792882Simpvoid nextfield(char **, char **); 19892882Simpvoid out_of_mem(void); 19992882Simpvoid parsecred(char *, struct xucred *); 200100117Salfredint put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int); 20175801Siedowsevoid *sa_rawaddr(struct sockaddr *sa, int *nbytes); 20275801Siedowseint sacmp(struct sockaddr *sa1, struct sockaddr *sa2, 20375801Siedowse struct sockaddr *samask); 20492882Simpint scan_tree(struct dirlist *, struct sockaddr *); 20592882Simpstatic void usage(void); 20692882Simpint xdr_dir(XDR *, char *); 20792882Simpint xdr_explist(XDR *, caddr_t); 208100117Salfredint xdr_explist_brief(XDR *, caddr_t); 20992882Simpint xdr_fhs(XDR *, caddr_t); 21092882Simpint xdr_mlist(XDR *, caddr_t); 21192882Simpvoid terminate(int); 2121558Srgrimes 2131558Srgrimesstruct exportlist *exphead; 2141558Srgrimesstruct mountlist *mlhead; 2151558Srgrimesstruct grouplist *grphead; 216166440Spjdchar *exnames_default[2] = { _PATH_EXPORTS, NULL }; 217166440Spjdchar **exnames; 218172827Smatteochar **hosts = NULL; 21972650Sgreenstruct xucred def_anon = { 22091354Sdd XUCRED_VERSION, 22172650Sgreen (uid_t)-2, 2221558Srgrimes 1, 22372650Sgreen { (gid_t)-2 }, 22472650Sgreen NULL 2251558Srgrimes}; 22625087Sdfrint force_v2 = 0; 2279336Sdfrint resvport_only = 1; 228172827Smatteoint nhosts = 0; 2299336Sdfrint dir_only = 1; 230121767Speterint dolog = 0; 23175754Siedowseint got_sighup = 0; 232172827Smatteoint xcreated = 0; 23374462Salfred 234172827Smatteochar *svcport_str = NULL; 235172827Smatteo 2361558Srgrimesint opt_flags; 23774462Salfredstatic int have_v6 = 1; 23874462Salfred 239192934Srmacklemint v4root_phase = 0; 240192934Srmacklemchar v4root_dirpath[PATH_MAX + 1]; 241192934Srmacklemint run_v4server = 0; 242192934Srmacklemint has_publicfh = 0; 243192934Srmacklem 244149433Spjdstruct pidfh *pfh = NULL; 24575801Siedowse/* Bits for opt_flags above */ 2461558Srgrimes#define OP_MAPROOT 0x01 2471558Srgrimes#define OP_MAPALL 0x02 24883653Speter/* 0x4 free */ 2491558Srgrimes#define OP_MASK 0x08 2501558Srgrimes#define OP_NET 0x10 2511558Srgrimes#define OP_ALLDIRS 0x40 25275801Siedowse#define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */ 253100336Sjoerg#define OP_QUIET 0x100 25474462Salfred#define OP_MASKLEN 0x200 255184588Sdfr#define OP_SEC 0x400 2561558Srgrimes 2571558Srgrimes#ifdef DEBUG 2581558Srgrimesint debug = 1; 25992882Simpvoid SYSLOG(int, const char *, ...) __printflike(2, 3); 2601558Srgrimes#define syslog SYSLOG 2611558Srgrimes#else 2621558Srgrimesint debug = 0; 2631558Srgrimes#endif 2641558Srgrimes 2651558Srgrimes/* 2661558Srgrimes * Mountd server for NFS mount protocol as described in: 2671558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A 2681558Srgrimes * The optional arguments are the exports file name 2691558Srgrimes * default: _PATH_EXPORTS 2701558Srgrimes * and "-n" to allow nonroot mount. 2711558Srgrimes */ 2721558Srgrimesint 2731558Srgrimesmain(argc, argv) 2741558Srgrimes int argc; 2751558Srgrimes char **argv; 2761558Srgrimes{ 27775754Siedowse fd_set readfds; 278172827Smatteo struct netconfig *nconf; 279172827Smatteo char *endptr, **hosts_bak; 280172827Smatteo void *nc_handle; 281149433Spjd pid_t otherpid; 282172827Smatteo in_port_t svcport; 283172827Smatteo int c, k, s; 284109363Smbr int maxrec = RPC_MAXDATASIZE; 2851558Srgrimes 28674462Salfred /* Check that another mountd isn't already running. */ 287150214Spjd pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid); 288149433Spjd if (pfh == NULL) { 289149433Spjd if (errno == EEXIST) 290149433Spjd errx(1, "mountd already running, pid: %d.", otherpid); 291149433Spjd warn("cannot open or create pidfile"); 292149433Spjd } 29374462Salfred 29474462Salfred s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 29574462Salfred if (s < 0) 29674462Salfred have_v6 = 0; 29774462Salfred else 29874462Salfred close(s); 2992999Swollman 300192993Srmacklem while ((c = getopt(argc, argv, "2deh:lnp:r")) != -1) 3011558Srgrimes switch (c) { 30225087Sdfr case '2': 30325087Sdfr force_v2 = 1; 30425087Sdfr break; 305192993Srmacklem case 'e': 306192934Srmacklem run_v4server = 1; 307192934Srmacklem break; 3089336Sdfr case 'n': 3099336Sdfr resvport_only = 0; 3109336Sdfr break; 3119336Sdfr case 'r': 3129336Sdfr dir_only = 0; 3139336Sdfr break; 3148688Sphk case 'd': 3158688Sphk debug = debug ? 0 : 1; 3168688Sphk break; 31731656Sguido case 'l': 318121767Speter dolog = 1; 31931656Sguido break; 320126572Sbms case 'p': 321126572Sbms endptr = NULL; 322126572Sbms svcport = (in_port_t)strtoul(optarg, &endptr, 10); 323126572Sbms if (endptr == NULL || *endptr != '\0' || 324126572Sbms svcport == 0 || svcport >= IPPORT_MAX) 325126572Sbms usage(); 326172827Smatteo svcport_str = strdup(optarg); 327126572Sbms break; 328172827Smatteo case 'h': 329172827Smatteo ++nhosts; 330172827Smatteo hosts_bak = hosts; 331172827Smatteo hosts_bak = realloc(hosts, nhosts * sizeof(char *)); 332172827Smatteo if (hosts_bak == NULL) { 333172827Smatteo if (hosts != NULL) { 334172827Smatteo for (k = 0; k < nhosts; k++) 335172827Smatteo free(hosts[k]); 336172827Smatteo free(hosts); 337172827Smatteo out_of_mem(); 338172827Smatteo } 339172827Smatteo } 340172827Smatteo hosts = hosts_bak; 341172827Smatteo hosts[nhosts - 1] = strdup(optarg); 342172827Smatteo if (hosts[nhosts - 1] == NULL) { 343172827Smatteo for (k = 0; k < (nhosts - 1); k++) 344172827Smatteo free(hosts[k]); 345172827Smatteo free(hosts); 346172827Smatteo out_of_mem(); 347172827Smatteo } 348172827Smatteo break; 3491558Srgrimes default: 35037663Scharnier usage(); 3511558Srgrimes }; 352192934Srmacklem 353192934Srmacklem /* 354192993Srmacklem * If the "-e" option was specified OR only the nfsd module is 355192934Srmacklem * found in the server, run "nfsd". 356192934Srmacklem * Otherwise, try and run "nfsserver". 357192934Srmacklem */ 358192934Srmacklem if (run_v4server > 0) { 359192934Srmacklem if (modfind("nfsd") < 0) { 360192934Srmacklem /* Not present in kernel, try loading it */ 361192934Srmacklem if (kldload("nfsd") < 0 || modfind("nfsd") < 0) 362192934Srmacklem errx(1, "NFS server is not available"); 363192934Srmacklem } 364192934Srmacklem } else if (modfind("nfsserver") < 0 && modfind("nfsd") >= 0) { 365192934Srmacklem run_v4server = 1; 366192934Srmacklem } else if (modfind("nfsserver") < 0) { 367192934Srmacklem /* Not present in kernel, try loading it */ 368192934Srmacklem if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0) 369192934Srmacklem errx(1, "NFS server is not available"); 370192934Srmacklem } 371192934Srmacklem 3721558Srgrimes argc -= optind; 3731558Srgrimes argv += optind; 3741558Srgrimes grphead = (struct grouplist *)NULL; 3751558Srgrimes exphead = (struct exportlist *)NULL; 3761558Srgrimes mlhead = (struct mountlist *)NULL; 377166440Spjd if (argc > 0) 378166440Spjd exnames = argv; 379166440Spjd else 380166440Spjd exnames = exnames_default; 3811558Srgrimes openlog("mountd", LOG_PID, LOG_DAEMON); 3821558Srgrimes if (debug) 38337663Scharnier warnx("getting export list"); 3841558Srgrimes get_exportlist(); 3851558Srgrimes if (debug) 38637663Scharnier warnx("getting mount list"); 3871558Srgrimes get_mountlist(); 3881558Srgrimes if (debug) 38937663Scharnier warnx("here we go"); 3901558Srgrimes if (debug == 0) { 3911558Srgrimes daemon(0, 0); 3921558Srgrimes signal(SIGINT, SIG_IGN); 3931558Srgrimes signal(SIGQUIT, SIG_IGN); 3941558Srgrimes } 39575754Siedowse signal(SIGHUP, huphandler); 39674462Salfred signal(SIGTERM, terminate); 397164394Srodrigc signal(SIGPIPE, SIG_IGN); 398149433Spjd 399149433Spjd pidfile_write(pfh); 400149433Spjd 40174462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 40274462Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 403109363Smbr rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); 404109363Smbr 40524759Sguido if (!resvport_only) { 40683687Speter if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL, 40783687Speter &resvport_only, sizeof(resvport_only)) != 0 && 40883687Speter errno != ENOENT) { 40924759Sguido syslog(LOG_ERR, "sysctl: %m"); 41024759Sguido exit(1); 41124759Sguido } 41224330Sguido } 413126572Sbms 414172827Smatteo /* 415172827Smatteo * If no hosts were specified, add a wildcard entry to bind to 416172827Smatteo * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the 417172827Smatteo * list. 418172827Smatteo */ 419172827Smatteo if (nhosts == 0) { 420172827Smatteo hosts = malloc(sizeof(char**)); 421172827Smatteo if (hosts == NULL) 422172827Smatteo out_of_mem(); 423172827Smatteo hosts[0] = "*"; 424172827Smatteo nhosts = 1; 425172827Smatteo } else { 426172827Smatteo hosts_bak = hosts; 427172827Smatteo if (have_v6) { 428172827Smatteo hosts_bak = realloc(hosts, (nhosts + 2) * 429172827Smatteo sizeof(char *)); 430172827Smatteo if (hosts_bak == NULL) { 431172827Smatteo for (k = 0; k < nhosts; k++) 432172827Smatteo free(hosts[k]); 433172827Smatteo free(hosts); 434172827Smatteo out_of_mem(); 435172827Smatteo } else 436172827Smatteo hosts = hosts_bak; 437172827Smatteo nhosts += 2; 438172827Smatteo hosts[nhosts - 2] = "::1"; 439172827Smatteo } else { 440172827Smatteo hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *)); 441172827Smatteo if (hosts_bak == NULL) { 442172827Smatteo for (k = 0; k < nhosts; k++) 443172827Smatteo free(hosts[k]); 444172827Smatteo free(hosts); 445172827Smatteo out_of_mem(); 446172827Smatteo } else { 447172827Smatteo nhosts += 1; 448172827Smatteo hosts = hosts_bak; 449126572Sbms } 450172827Smatteo } 45174462Salfred 452172827Smatteo hosts[nhosts - 1] = "127.0.0.1"; 45374462Salfred } 45474462Salfred 455172827Smatteo nc_handle = setnetconfig(); 456172827Smatteo while ((nconf = getnetconfig(nc_handle))) { 457172827Smatteo if (nconf->nc_flag & NC_VISIBLE) { 458172827Smatteo if (have_v6 == 0 && strcmp(nconf->nc_protofmly, 459172827Smatteo "inet6") == 0) { 460172827Smatteo /* DO NOTHING */ 461172827Smatteo } else 462172827Smatteo create_service(nconf); 463172827Smatteo } 46474462Salfred } 465172827Smatteo endnetconfig(nc_handle); 46674462Salfred 46774462Salfred if (xcreated == 0) { 46874462Salfred syslog(LOG_ERR, "could not create any services"); 4691558Srgrimes exit(1); 4701558Srgrimes } 47175754Siedowse 47275754Siedowse /* Expand svc_run() here so that we can call get_exportlist(). */ 47375754Siedowse for (;;) { 47475754Siedowse if (got_sighup) { 47575754Siedowse get_exportlist(); 47675754Siedowse got_sighup = 0; 47775754Siedowse } 47875754Siedowse readfds = svc_fdset; 47975754Siedowse switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) { 48075754Siedowse case -1: 48175754Siedowse if (errno == EINTR) 48275754Siedowse continue; 48375754Siedowse syslog(LOG_ERR, "mountd died: select: %m"); 48475754Siedowse exit(1); 48575754Siedowse case 0: 48675754Siedowse continue; 48775754Siedowse default: 48875754Siedowse svc_getreqset(&readfds); 48975754Siedowse } 49075754Siedowse } 491172827Smatteo} 492172827Smatteo 493172827Smatteo/* 494172827Smatteo * This routine creates and binds sockets on the appropriate 495172827Smatteo * addresses. It gets called one time for each transport and 496172827Smatteo * registrates the service with rpcbind on that trasport. 497172827Smatteo */ 498172827Smatteovoid 499172827Smatteocreate_service(struct netconfig *nconf) 500172827Smatteo{ 501172827Smatteo struct addrinfo hints, *res = NULL; 502172827Smatteo struct sockaddr_in *sin; 503172827Smatteo struct sockaddr_in6 *sin6; 504172827Smatteo struct __rpc_sockinfo si; 505172827Smatteo struct netbuf servaddr; 506172827Smatteo SVCXPRT *transp = NULL; 507172827Smatteo int aicode; 508172827Smatteo int fd; 509172827Smatteo int nhostsbak; 510172827Smatteo int one = 1; 511172827Smatteo int r; 512172827Smatteo int registered = 0; 513172827Smatteo u_int32_t host_addr[4]; /* IPv4 or IPv6 */ 514172827Smatteo 515172827Smatteo if ((nconf->nc_semantics != NC_TPI_CLTS) && 516172827Smatteo (nconf->nc_semantics != NC_TPI_COTS) && 517172827Smatteo (nconf->nc_semantics != NC_TPI_COTS_ORD)) 518172827Smatteo return; /* not my type */ 519172827Smatteo 520172827Smatteo /* 521172827Smatteo * XXX - using RPC library internal functions. 522172827Smatteo */ 523172827Smatteo if (!__rpc_nconf2sockinfo(nconf, &si)) { 524172827Smatteo syslog(LOG_ERR, "cannot get information for %s", 525172827Smatteo nconf->nc_netid); 526172827Smatteo return; 527172827Smatteo } 528172827Smatteo 529172827Smatteo /* Get mountd's address on this transport */ 530172827Smatteo memset(&hints, 0, sizeof hints); 531172827Smatteo hints.ai_flags = AI_PASSIVE; 532172827Smatteo hints.ai_family = si.si_af; 533172827Smatteo hints.ai_socktype = si.si_socktype; 534172827Smatteo hints.ai_protocol = si.si_proto; 535172827Smatteo 536172827Smatteo /* 537172827Smatteo * Bind to specific IPs if asked to 538172827Smatteo */ 539172827Smatteo nhostsbak = nhosts; 540172827Smatteo while (nhostsbak > 0) { 541172827Smatteo --nhostsbak; 542172827Smatteo /* 543172827Smatteo * XXX - using RPC library internal functions. 544172827Smatteo */ 545172827Smatteo if ((fd = __rpc_nconf2fd(nconf)) < 0) { 546172827Smatteo int non_fatal = 0; 547172827Smatteo if (errno == EPROTONOSUPPORT && 548172827Smatteo nconf->nc_semantics != NC_TPI_CLTS) 549172827Smatteo non_fatal = 1; 550172827Smatteo 551172827Smatteo syslog(non_fatal ? LOG_DEBUG : LOG_ERR, 552172827Smatteo "cannot create socket for %s", nconf->nc_netid); 553172827Smatteo return; 554172827Smatteo } 555172827Smatteo 556172827Smatteo switch (hints.ai_family) { 557172827Smatteo case AF_INET: 558172827Smatteo if (inet_pton(AF_INET, hosts[nhostsbak], 559172827Smatteo host_addr) == 1) { 560172827Smatteo hints.ai_flags &= AI_NUMERICHOST; 561172827Smatteo } else { 562172827Smatteo /* 563172827Smatteo * Skip if we have an AF_INET6 address. 564172827Smatteo */ 565172827Smatteo if (inet_pton(AF_INET6, hosts[nhostsbak], 566172827Smatteo host_addr) == 1) { 567172827Smatteo close(fd); 568172827Smatteo continue; 569172827Smatteo } 570172827Smatteo } 571172827Smatteo break; 572172827Smatteo case AF_INET6: 573172827Smatteo if (inet_pton(AF_INET6, hosts[nhostsbak], 574172827Smatteo host_addr) == 1) { 575172827Smatteo hints.ai_flags &= AI_NUMERICHOST; 576172827Smatteo } else { 577172827Smatteo /* 578172827Smatteo * Skip if we have an AF_INET address. 579172827Smatteo */ 580172827Smatteo if (inet_pton(AF_INET, hosts[nhostsbak], 581172827Smatteo host_addr) == 1) { 582172827Smatteo close(fd); 583172827Smatteo continue; 584172827Smatteo } 585172827Smatteo } 586172827Smatteo 587172827Smatteo /* 588172827Smatteo * We're doing host-based access checks here, so don't 589172827Smatteo * allow v4-in-v6 to confuse things. The kernel will 590172827Smatteo * disable it by default on NFS sockets too. 591172827Smatteo */ 592172827Smatteo if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, 593172827Smatteo sizeof one) < 0) { 594172827Smatteo syslog(LOG_ERR, 595172827Smatteo "can't disable v4-in-v6 on IPv6 socket"); 596172827Smatteo exit(1); 597172827Smatteo } 598172827Smatteo break; 599172827Smatteo default: 600172827Smatteo break; 601172827Smatteo } 602172827Smatteo 603172827Smatteo /* 604172827Smatteo * If no hosts were specified, just bind to INADDR_ANY 605172827Smatteo */ 606172827Smatteo if (strcmp("*", hosts[nhostsbak]) == 0) { 607172827Smatteo if (svcport_str == NULL) { 608172827Smatteo res = malloc(sizeof(struct addrinfo)); 609172827Smatteo if (res == NULL) 610172827Smatteo out_of_mem(); 611172827Smatteo res->ai_flags = hints.ai_flags; 612172827Smatteo res->ai_family = hints.ai_family; 613172827Smatteo res->ai_protocol = hints.ai_protocol; 614172827Smatteo switch (res->ai_family) { 615172827Smatteo case AF_INET: 616172827Smatteo sin = malloc(sizeof(struct sockaddr_in)); 617172827Smatteo if (sin == NULL) 618172827Smatteo out_of_mem(); 619172827Smatteo sin->sin_family = AF_INET; 620172827Smatteo sin->sin_port = htons(0); 621172827Smatteo sin->sin_addr.s_addr = htonl(INADDR_ANY); 622172827Smatteo res->ai_addr = (struct sockaddr*) sin; 623172827Smatteo res->ai_addrlen = (socklen_t) 624172827Smatteo sizeof(res->ai_addr); 625172827Smatteo break; 626172827Smatteo case AF_INET6: 627172827Smatteo sin6 = malloc(sizeof(struct sockaddr_in6)); 628173056Ssimon if (sin6 == NULL) 629172827Smatteo out_of_mem(); 630172827Smatteo sin6->sin6_family = AF_INET6; 631172827Smatteo sin6->sin6_port = htons(0); 632172827Smatteo sin6->sin6_addr = in6addr_any; 633172827Smatteo res->ai_addr = (struct sockaddr*) sin6; 634172827Smatteo res->ai_addrlen = (socklen_t) 635172827Smatteo sizeof(res->ai_addr); 636172827Smatteo break; 637172827Smatteo default: 638172827Smatteo break; 639172827Smatteo } 640172827Smatteo } else { 641172827Smatteo if ((aicode = getaddrinfo(NULL, svcport_str, 642172827Smatteo &hints, &res)) != 0) { 643172827Smatteo syslog(LOG_ERR, 644172827Smatteo "cannot get local address for %s: %s", 645172827Smatteo nconf->nc_netid, 646172827Smatteo gai_strerror(aicode)); 647172827Smatteo continue; 648172827Smatteo } 649172827Smatteo } 650172827Smatteo } else { 651172827Smatteo if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str, 652172827Smatteo &hints, &res)) != 0) { 653172827Smatteo syslog(LOG_ERR, 654172827Smatteo "cannot get local address for %s: %s", 655172827Smatteo nconf->nc_netid, gai_strerror(aicode)); 656172827Smatteo continue; 657172827Smatteo } 658172827Smatteo } 659172827Smatteo 660172827Smatteo r = bindresvport_sa(fd, res->ai_addr); 661172827Smatteo if (r != 0) { 662172827Smatteo syslog(LOG_ERR, "bindresvport_sa: %m"); 663172827Smatteo exit(1); 664172827Smatteo } 665172827Smatteo 666172827Smatteo if (nconf->nc_semantics != NC_TPI_CLTS) 667172827Smatteo listen(fd, SOMAXCONN); 668172827Smatteo 669172827Smatteo if (nconf->nc_semantics == NC_TPI_CLTS ) 670172827Smatteo transp = svc_dg_create(fd, 0, 0); 671172827Smatteo else 672172827Smatteo transp = svc_vc_create(fd, RPC_MAXDATASIZE, 673172827Smatteo RPC_MAXDATASIZE); 674172827Smatteo 675172827Smatteo if (transp != (SVCXPRT *) NULL) { 676172827Smatteo if (!svc_reg(transp, RPCPROG_MNT, RPCMNT_VER1, mntsrv, 677172827Smatteo NULL)) 678172827Smatteo syslog(LOG_ERR, 679172827Smatteo "can't register %s RPCMNT_VER1 service", 680172827Smatteo nconf->nc_netid); 681172827Smatteo if (!force_v2) { 682172827Smatteo if (!svc_reg(transp, RPCPROG_MNT, RPCMNT_VER3, 683172827Smatteo mntsrv, NULL)) 684172827Smatteo syslog(LOG_ERR, 685172827Smatteo "can't register %s RPCMNT_VER3 service", 686172827Smatteo nconf->nc_netid); 687172827Smatteo } 688172827Smatteo } else 689172827Smatteo syslog(LOG_WARNING, "can't create %s services", 690172827Smatteo nconf->nc_netid); 691172827Smatteo 692172827Smatteo if (registered == 0) { 693172827Smatteo registered = 1; 694172827Smatteo memset(&hints, 0, sizeof hints); 695172827Smatteo hints.ai_flags = AI_PASSIVE; 696172827Smatteo hints.ai_family = si.si_af; 697172827Smatteo hints.ai_socktype = si.si_socktype; 698172827Smatteo hints.ai_protocol = si.si_proto; 699172827Smatteo 700172827Smatteo if (svcport_str == NULL) { 701172827Smatteo svcport_str = malloc(NI_MAXSERV * sizeof(char)); 702172827Smatteo if (svcport_str == NULL) 703172827Smatteo out_of_mem(); 704172827Smatteo 705172827Smatteo if (getnameinfo(res->ai_addr, 706172827Smatteo res->ai_addr->sa_len, NULL, NI_MAXHOST, 707172827Smatteo svcport_str, NI_MAXSERV * sizeof(char), 708172827Smatteo NI_NUMERICHOST | NI_NUMERICSERV)) 709172827Smatteo errx(1, "Cannot get port number"); 710172827Smatteo } 711172827Smatteo 712172827Smatteo if((aicode = getaddrinfo(NULL, svcport_str, &hints, 713172827Smatteo &res)) != 0) { 714172827Smatteo syslog(LOG_ERR, "cannot get local address: %s", 715172827Smatteo gai_strerror(aicode)); 716172827Smatteo exit(1); 717172827Smatteo } 718172827Smatteo 719172827Smatteo servaddr.buf = malloc(res->ai_addrlen); 720172827Smatteo memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen); 721172827Smatteo servaddr.len = res->ai_addrlen; 722172827Smatteo 723172827Smatteo rpcb_set(RPCPROG_MNT, RPCMNT_VER1, nconf, &servaddr); 724172827Smatteo rpcb_set(RPCPROG_MNT, RPCMNT_VER3, nconf, &servaddr); 725172827Smatteo 726172827Smatteo xcreated++; 727172827Smatteo freeaddrinfo(res); 728172827Smatteo } 729172827Smatteo } /* end while */ 7301558Srgrimes} 7311558Srgrimes 73237663Scharnierstatic void 73337663Scharnierusage() 73437663Scharnier{ 73537663Scharnier fprintf(stderr, 736192993Srmacklem "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] " 737172827Smatteo "[-h <bindip>] [export_file ...]\n"); 73837663Scharnier exit(1); 73937663Scharnier} 74037663Scharnier 7411558Srgrimes/* 7421558Srgrimes * The mount rpc service 7431558Srgrimes */ 7441558Srgrimesvoid 7451558Srgrimesmntsrv(rqstp, transp) 7461558Srgrimes struct svc_req *rqstp; 7471558Srgrimes SVCXPRT *transp; 7481558Srgrimes{ 7491558Srgrimes struct exportlist *ep; 7501558Srgrimes struct dirlist *dp; 7519336Sdfr struct fhreturn fhr; 7521558Srgrimes struct stat stb; 7531558Srgrimes struct statfs fsb; 75474462Salfred char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 75574462Salfred int lookup_failed = 1; 75674462Salfred struct sockaddr *saddr; 7579336Sdfr u_short sport; 75823681Speter char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN]; 75928911Sguido int bad = 0, defset, hostset; 7609336Sdfr sigset_t sighup_mask; 7611558Srgrimes 7629336Sdfr sigemptyset(&sighup_mask); 7639336Sdfr sigaddset(&sighup_mask, SIGHUP); 76474462Salfred saddr = svc_getrpccaller(transp)->buf; 76574462Salfred switch (saddr->sa_family) { 76674462Salfred case AF_INET6: 76775635Siedowse sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 76874462Salfred break; 76974462Salfred case AF_INET: 77075635Siedowse sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 77174462Salfred break; 77274462Salfred default: 77374462Salfred syslog(LOG_ERR, "request from unknown address family"); 77474462Salfred return; 77574462Salfred } 77674462Salfred lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, 77774462Salfred NULL, 0, 0); 77874462Salfred getnameinfo(saddr, saddr->sa_len, numerichost, 77974462Salfred sizeof numerichost, NULL, 0, NI_NUMERICHOST); 7801558Srgrimes switch (rqstp->rq_proc) { 7811558Srgrimes case NULLPROC: 782121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL)) 78337663Scharnier syslog(LOG_ERR, "can't send reply"); 7841558Srgrimes return; 7851558Srgrimes case RPCMNT_MOUNT: 7869336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 78731656Sguido syslog(LOG_NOTICE, 78831656Sguido "mount request from %s from unprivileged port", 78974462Salfred numerichost); 7901558Srgrimes svcerr_weakauth(transp); 7911558Srgrimes return; 7921558Srgrimes } 793121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 79431656Sguido syslog(LOG_NOTICE, "undecodable mount request from %s", 79574462Salfred numerichost); 7961558Srgrimes svcerr_decode(transp); 7971558Srgrimes return; 7981558Srgrimes } 7991558Srgrimes 8001558Srgrimes /* 8011558Srgrimes * Get the real pathname and make sure it is a directory 8029336Sdfr * or a regular file if the -r option was specified 8039336Sdfr * and it exists. 8041558Srgrimes */ 80551968Salfred if (realpath(rpcpath, dirpath) == NULL || 8061558Srgrimes stat(dirpath, &stb) < 0 || 8079336Sdfr (!S_ISDIR(stb.st_mode) && 80874462Salfred (dir_only || !S_ISREG(stb.st_mode))) || 8091558Srgrimes statfs(dirpath, &fsb) < 0) { 8101558Srgrimes chdir("/"); /* Just in case realpath doesn't */ 81131656Sguido syslog(LOG_NOTICE, 81237663Scharnier "mount request from %s for non existent path %s", 81374462Salfred numerichost, dirpath); 8141558Srgrimes if (debug) 81537663Scharnier warnx("stat failed on %s", dirpath); 81628911Sguido bad = ENOENT; /* We will send error reply later */ 8171558Srgrimes } 8181558Srgrimes 8191558Srgrimes /* Check in the exports list */ 8209336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 8211558Srgrimes ep = ex_search(&fsb.f_fsid); 8229336Sdfr hostset = defset = 0; 8239336Sdfr if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) || 8241558Srgrimes ((dp = dirp_search(ep->ex_dirl, dirpath)) && 82574462Salfred chk_host(dp, saddr, &defset, &hostset)) || 82674462Salfred (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 82774462Salfred scan_tree(ep->ex_dirl, saddr) == 0))) { 82828911Sguido if (bad) { 829121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 83028911Sguido (caddr_t)&bad)) 83137663Scharnier syslog(LOG_ERR, "can't send reply"); 83228911Sguido sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 83328911Sguido return; 83428911Sguido } 8359336Sdfr if (hostset & DP_HOSTSET) 8369336Sdfr fhr.fhr_flag = hostset; 8379336Sdfr else 8389336Sdfr fhr.fhr_flag = defset; 8399336Sdfr fhr.fhr_vers = rqstp->rq_vers; 8401558Srgrimes /* Get the file handle */ 84123681Speter memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 8429336Sdfr if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 8431558Srgrimes bad = errno; 84437663Scharnier syslog(LOG_ERR, "can't get fh for %s", dirpath); 845121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 8461558Srgrimes (caddr_t)&bad)) 84737663Scharnier syslog(LOG_ERR, "can't send reply"); 8489336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8491558Srgrimes return; 8501558Srgrimes } 851184588Sdfr fhr.fhr_numsecflavors = ep->ex_numsecflavors; 852184588Sdfr fhr.fhr_secflavors = ep->ex_secflavors; 853121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, 854121556Speter (caddr_t)&fhr)) 85537663Scharnier syslog(LOG_ERR, "can't send reply"); 85674462Salfred if (!lookup_failed) 85774462Salfred add_mlist(host, dirpath); 8581558Srgrimes else 85974462Salfred add_mlist(numerichost, dirpath); 8601558Srgrimes if (debug) 86137663Scharnier warnx("mount successful"); 862121767Speter if (dolog) 86331656Sguido syslog(LOG_NOTICE, 86431656Sguido "mount request succeeded from %s for %s", 86574462Salfred numerichost, dirpath); 86631656Sguido } else { 8671558Srgrimes bad = EACCES; 86831656Sguido syslog(LOG_NOTICE, 86931656Sguido "mount request denied from %s for %s", 87074462Salfred numerichost, dirpath); 87131656Sguido } 87228911Sguido 873121556Speter if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, 874121556Speter (caddr_t)&bad)) 87537663Scharnier syslog(LOG_ERR, "can't send reply"); 8769336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 8771558Srgrimes return; 8781558Srgrimes case RPCMNT_DUMP: 879121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL)) 88037663Scharnier syslog(LOG_ERR, "can't send reply"); 881121767Speter else if (dolog) 88231656Sguido syslog(LOG_NOTICE, 88331656Sguido "dump request succeeded from %s", 88474462Salfred numerichost); 8851558Srgrimes return; 8861558Srgrimes case RPCMNT_UMOUNT: 8879336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 88831656Sguido syslog(LOG_NOTICE, 88931656Sguido "umount request from %s from unprivileged port", 89074462Salfred numerichost); 8911558Srgrimes svcerr_weakauth(transp); 8921558Srgrimes return; 8931558Srgrimes } 894121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 89531656Sguido syslog(LOG_NOTICE, "undecodable umount request from %s", 89674462Salfred numerichost); 8971558Srgrimes svcerr_decode(transp); 8981558Srgrimes return; 8991558Srgrimes } 90051968Salfred if (realpath(rpcpath, dirpath) == NULL) { 90151968Salfred syslog(LOG_NOTICE, "umount request from %s " 90251968Salfred "for non existent path %s", 90374462Salfred numerichost, dirpath); 90451968Salfred } 905121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 90637663Scharnier syslog(LOG_ERR, "can't send reply"); 90774462Salfred if (!lookup_failed) 90875635Siedowse del_mlist(host, dirpath); 90975635Siedowse del_mlist(numerichost, dirpath); 910121767Speter if (dolog) 91131656Sguido syslog(LOG_NOTICE, 91231656Sguido "umount request succeeded from %s for %s", 91374462Salfred numerichost, dirpath); 9141558Srgrimes return; 9151558Srgrimes case RPCMNT_UMNTALL: 9169336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 91731656Sguido syslog(LOG_NOTICE, 91831656Sguido "umountall request from %s from unprivileged port", 91974462Salfred numerichost); 9201558Srgrimes svcerr_weakauth(transp); 9211558Srgrimes return; 9221558Srgrimes } 923121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 92437663Scharnier syslog(LOG_ERR, "can't send reply"); 92574462Salfred if (!lookup_failed) 92675635Siedowse del_mlist(host, NULL); 92775635Siedowse del_mlist(numerichost, NULL); 928121767Speter if (dolog) 92931656Sguido syslog(LOG_NOTICE, 93031656Sguido "umountall request succeeded from %s", 93174462Salfred numerichost); 9321558Srgrimes return; 9331558Srgrimes case RPCMNT_EXPORT: 934121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL)) 935121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, 936121556Speter (caddr_t)NULL)) 937100117Salfred syslog(LOG_ERR, "can't send reply"); 938121767Speter if (dolog) 93931656Sguido syslog(LOG_NOTICE, 94031656Sguido "export request succeeded from %s", 94174462Salfred numerichost); 9421558Srgrimes return; 9431558Srgrimes default: 9441558Srgrimes svcerr_noproc(transp); 9451558Srgrimes return; 9461558Srgrimes } 9471558Srgrimes} 9481558Srgrimes 9491558Srgrimes/* 9501558Srgrimes * Xdr conversion for a dirpath string 9511558Srgrimes */ 9521558Srgrimesint 9531558Srgrimesxdr_dir(xdrsp, dirp) 9541558Srgrimes XDR *xdrsp; 9551558Srgrimes char *dirp; 9561558Srgrimes{ 9571558Srgrimes return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 9581558Srgrimes} 9591558Srgrimes 9601558Srgrimes/* 9619336Sdfr * Xdr routine to generate file handle reply 9621558Srgrimes */ 9631558Srgrimesint 9649336Sdfrxdr_fhs(xdrsp, cp) 9651558Srgrimes XDR *xdrsp; 9669336Sdfr caddr_t cp; 9671558Srgrimes{ 96892806Sobrien struct fhreturn *fhrp = (struct fhreturn *)cp; 9699336Sdfr u_long ok = 0, len, auth; 970184588Sdfr int i; 9711558Srgrimes 9721558Srgrimes if (!xdr_long(xdrsp, &ok)) 9731558Srgrimes return (0); 9749336Sdfr switch (fhrp->fhr_vers) { 9759336Sdfr case 1: 9769336Sdfr return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 9779336Sdfr case 3: 9789336Sdfr len = NFSX_V3FH; 9799336Sdfr if (!xdr_long(xdrsp, &len)) 9809336Sdfr return (0); 9819336Sdfr if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 9829336Sdfr return (0); 983184588Sdfr if (fhrp->fhr_numsecflavors) { 984184588Sdfr if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors)) 985184588Sdfr return (0); 986184588Sdfr for (i = 0; i < fhrp->fhr_numsecflavors; i++) 987184588Sdfr if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i])) 988184588Sdfr return (0); 989184588Sdfr return (1); 990184588Sdfr } else { 991184588Sdfr auth = AUTH_SYS; 992184588Sdfr len = 1; 993184588Sdfr if (!xdr_long(xdrsp, &len)) 994184588Sdfr return (0); 995184588Sdfr return (xdr_long(xdrsp, &auth)); 996184588Sdfr } 9979336Sdfr }; 9989336Sdfr return (0); 9991558Srgrimes} 10001558Srgrimes 10011558Srgrimesint 10021558Srgrimesxdr_mlist(xdrsp, cp) 10031558Srgrimes XDR *xdrsp; 10041558Srgrimes caddr_t cp; 10051558Srgrimes{ 10061558Srgrimes struct mountlist *mlp; 10071558Srgrimes int true = 1; 10081558Srgrimes int false = 0; 10091558Srgrimes char *strp; 10101558Srgrimes 10111558Srgrimes mlp = mlhead; 10121558Srgrimes while (mlp) { 10131558Srgrimes if (!xdr_bool(xdrsp, &true)) 10141558Srgrimes return (0); 10151558Srgrimes strp = &mlp->ml_host[0]; 10161558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN)) 10171558Srgrimes return (0); 10181558Srgrimes strp = &mlp->ml_dirp[0]; 10191558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 10201558Srgrimes return (0); 10211558Srgrimes mlp = mlp->ml_next; 10221558Srgrimes } 10231558Srgrimes if (!xdr_bool(xdrsp, &false)) 10241558Srgrimes return (0); 10251558Srgrimes return (1); 10261558Srgrimes} 10271558Srgrimes 10281558Srgrimes/* 10291558Srgrimes * Xdr conversion for export list 10301558Srgrimes */ 10311558Srgrimesint 1032100117Salfredxdr_explist_common(xdrsp, cp, brief) 10331558Srgrimes XDR *xdrsp; 10341558Srgrimes caddr_t cp; 1035100117Salfred int brief; 10361558Srgrimes{ 10371558Srgrimes struct exportlist *ep; 10381558Srgrimes int false = 0; 10399336Sdfr int putdef; 10409336Sdfr sigset_t sighup_mask; 10411558Srgrimes 10429336Sdfr sigemptyset(&sighup_mask); 10439336Sdfr sigaddset(&sighup_mask, SIGHUP); 10449336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 10451558Srgrimes ep = exphead; 10461558Srgrimes while (ep) { 10471558Srgrimes putdef = 0; 1048100117Salfred if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, 1049100117Salfred &putdef, brief)) 10501558Srgrimes goto errout; 10511558Srgrimes if (ep->ex_defdir && putdef == 0 && 10521558Srgrimes put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 1053100117Salfred &putdef, brief)) 10541558Srgrimes goto errout; 10551558Srgrimes ep = ep->ex_next; 10561558Srgrimes } 10579336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 10581558Srgrimes if (!xdr_bool(xdrsp, &false)) 10591558Srgrimes return (0); 10601558Srgrimes return (1); 10611558Srgrimeserrout: 10629336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 10631558Srgrimes return (0); 10641558Srgrimes} 10651558Srgrimes 10661558Srgrimes/* 10671558Srgrimes * Called from xdr_explist() to traverse the tree and export the 10681558Srgrimes * directory paths. 10691558Srgrimes */ 10701558Srgrimesint 1071100117Salfredput_exlist(dp, xdrsp, adp, putdefp, brief) 10721558Srgrimes struct dirlist *dp; 10731558Srgrimes XDR *xdrsp; 10741558Srgrimes struct dirlist *adp; 10751558Srgrimes int *putdefp; 1076100117Salfred int brief; 10771558Srgrimes{ 10781558Srgrimes struct grouplist *grp; 10791558Srgrimes struct hostlist *hp; 10801558Srgrimes int true = 1; 10811558Srgrimes int false = 0; 10821558Srgrimes int gotalldir = 0; 10831558Srgrimes char *strp; 10841558Srgrimes 10851558Srgrimes if (dp) { 1086100117Salfred if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief)) 10871558Srgrimes return (1); 10881558Srgrimes if (!xdr_bool(xdrsp, &true)) 10891558Srgrimes return (1); 10901558Srgrimes strp = dp->dp_dirp; 10911558Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 10921558Srgrimes return (1); 10931558Srgrimes if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 10941558Srgrimes gotalldir = 1; 10951558Srgrimes *putdefp = 1; 10961558Srgrimes } 1097100117Salfred if (brief) { 1098100117Salfred if (!xdr_bool(xdrsp, &true)) 1099100117Salfred return (1); 1100100117Salfred strp = "(...)"; 1101100117Salfred if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 1102100117Salfred return (1); 1103100117Salfred } else if ((dp->dp_flag & DP_DEFSET) == 0 && 11041558Srgrimes (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 11051558Srgrimes hp = dp->dp_hosts; 11061558Srgrimes while (hp) { 11071558Srgrimes grp = hp->ht_grp; 11081558Srgrimes if (grp->gr_type == GT_HOST) { 11091558Srgrimes if (!xdr_bool(xdrsp, &true)) 11101558Srgrimes return (1); 111174462Salfred strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 11128871Srgrimes if (!xdr_string(xdrsp, &strp, 11131558Srgrimes RPCMNT_NAMELEN)) 11141558Srgrimes return (1); 11151558Srgrimes } else if (grp->gr_type == GT_NET) { 11161558Srgrimes if (!xdr_bool(xdrsp, &true)) 11171558Srgrimes return (1); 11181558Srgrimes strp = grp->gr_ptr.gt_net.nt_name; 11198871Srgrimes if (!xdr_string(xdrsp, &strp, 11201558Srgrimes RPCMNT_NAMELEN)) 11211558Srgrimes return (1); 11221558Srgrimes } 11231558Srgrimes hp = hp->ht_next; 11241558Srgrimes if (gotalldir && hp == (struct hostlist *)NULL) { 11251558Srgrimes hp = adp->dp_hosts; 11261558Srgrimes gotalldir = 0; 11271558Srgrimes } 11281558Srgrimes } 11291558Srgrimes } 11301558Srgrimes if (!xdr_bool(xdrsp, &false)) 11311558Srgrimes return (1); 1132100117Salfred if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief)) 11331558Srgrimes return (1); 11341558Srgrimes } 11351558Srgrimes return (0); 11361558Srgrimes} 11371558Srgrimes 1138100117Salfredint 1139100117Salfredxdr_explist(xdrsp, cp) 1140100117Salfred XDR *xdrsp; 1141100117Salfred caddr_t cp; 1142100117Salfred{ 1143100117Salfred 1144100117Salfred return xdr_explist_common(xdrsp, cp, 0); 1145100117Salfred} 1146100117Salfred 1147100117Salfredint 1148100117Salfredxdr_explist_brief(xdrsp, cp) 1149100117Salfred XDR *xdrsp; 1150100117Salfred caddr_t cp; 1151100117Salfred{ 1152100117Salfred 1153100117Salfred return xdr_explist_common(xdrsp, cp, 1); 1154100117Salfred} 1155100117Salfred 115696622Siedowsechar *line; 115796622Siedowseint linesize; 11581558SrgrimesFILE *exp_file; 11591558Srgrimes 11601558Srgrimes/* 1161166440Spjd * Get the export list from one, currently open file 11621558Srgrimes */ 1163166440Spjdstatic void 1164166440Spjdget_exportlist_one() 11651558Srgrimes{ 11661558Srgrimes struct exportlist *ep, *ep2; 11671558Srgrimes struct grouplist *grp, *tgrp; 11681558Srgrimes struct exportlist **epp; 11691558Srgrimes struct dirlist *dirhead; 1170166440Spjd struct statfs fsb; 117172650Sgreen struct xucred anon; 11721558Srgrimes char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 1173166440Spjd int len, has_host, exflags, got_nondir, dirplen, netgrp; 11741558Srgrimes 1175192934Srmacklem v4root_phase = 0; 11761558Srgrimes dirhead = (struct dirlist *)NULL; 11771558Srgrimes while (get_line()) { 11781558Srgrimes if (debug) 117937663Scharnier warnx("got line %s", line); 11801558Srgrimes cp = line; 11811558Srgrimes nextfield(&cp, &endcp); 11821558Srgrimes if (*cp == '#') 11831558Srgrimes goto nextline; 11841558Srgrimes 11851558Srgrimes /* 11861558Srgrimes * Set defaults. 11871558Srgrimes */ 11881558Srgrimes has_host = FALSE; 11891558Srgrimes anon = def_anon; 11901558Srgrimes exflags = MNT_EXPORTED; 11911558Srgrimes got_nondir = 0; 11921558Srgrimes opt_flags = 0; 11931558Srgrimes ep = (struct exportlist *)NULL; 1194192934Srmacklem dirp = NULL; 11951558Srgrimes 11961558Srgrimes /* 1197192934Srmacklem * Handle the V4 root dir. 1198192934Srmacklem */ 1199192934Srmacklem if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') { 1200192934Srmacklem /* 1201192934Srmacklem * V4: just indicates that it is the v4 root point, 1202192934Srmacklem * so skip over that and set v4root_phase. 1203192934Srmacklem */ 1204192934Srmacklem if (v4root_phase > 0) { 1205192934Srmacklem syslog(LOG_ERR, "V4:duplicate line, ignored"); 1206192934Srmacklem goto nextline; 1207192934Srmacklem } 1208192934Srmacklem v4root_phase = 1; 1209192934Srmacklem cp += 3; 1210192934Srmacklem nextfield(&cp, &endcp); 1211192934Srmacklem } 1212192934Srmacklem 1213192934Srmacklem /* 12141558Srgrimes * Create new exports list entry 12151558Srgrimes */ 12161558Srgrimes len = endcp-cp; 12171558Srgrimes tgrp = grp = get_grp(); 12181558Srgrimes while (len > 0) { 12191558Srgrimes if (len > RPCMNT_NAMELEN) { 12201558Srgrimes getexp_err(ep, tgrp); 12211558Srgrimes goto nextline; 12221558Srgrimes } 12231558Srgrimes if (*cp == '-') { 12241558Srgrimes if (ep == (struct exportlist *)NULL) { 12251558Srgrimes getexp_err(ep, tgrp); 12261558Srgrimes goto nextline; 12271558Srgrimes } 12281558Srgrimes if (debug) 122937663Scharnier warnx("doing opt %s", cp); 12301558Srgrimes got_nondir = 1; 12311558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 12321558Srgrimes &exflags, &anon)) { 12331558Srgrimes getexp_err(ep, tgrp); 12341558Srgrimes goto nextline; 12351558Srgrimes } 12361558Srgrimes } else if (*cp == '/') { 12371558Srgrimes savedc = *endcp; 12381558Srgrimes *endcp = '\0'; 1239192934Srmacklem if (v4root_phase > 1) { 1240192934Srmacklem if (dirp != NULL) { 1241192934Srmacklem syslog(LOG_ERR, "Multiple V4 dirs"); 1242192934Srmacklem getexp_err(ep, tgrp); 1243192934Srmacklem goto nextline; 1244192934Srmacklem } 1245192934Srmacklem } 12461558Srgrimes if (check_dirpath(cp) && 12471558Srgrimes statfs(cp, &fsb) >= 0) { 12481558Srgrimes if (got_nondir) { 124937663Scharnier syslog(LOG_ERR, "dirs must be first"); 12501558Srgrimes getexp_err(ep, tgrp); 12511558Srgrimes goto nextline; 12521558Srgrimes } 1253192934Srmacklem if (v4root_phase == 1) { 1254192934Srmacklem if (dirp != NULL) { 1255192934Srmacklem syslog(LOG_ERR, "Multiple V4 dirs"); 12561558Srgrimes getexp_err(ep, tgrp); 12571558Srgrimes goto nextline; 12581558Srgrimes } 1259192934Srmacklem if (strlen(v4root_dirpath) == 0) { 1260192934Srmacklem strlcpy(v4root_dirpath, cp, 1261192934Srmacklem sizeof (v4root_dirpath)); 1262192934Srmacklem } else if (strcmp(v4root_dirpath, cp) 1263192934Srmacklem != 0) { 1264192934Srmacklem syslog(LOG_ERR, 1265192934Srmacklem "different V4 dirpath %s", cp); 1266192934Srmacklem getexp_err(ep, tgrp); 1267192934Srmacklem goto nextline; 1268192934Srmacklem } 1269192934Srmacklem dirp = cp; 1270192934Srmacklem v4root_phase = 2; 1271192934Srmacklem got_nondir = 1; 1272192934Srmacklem ep = get_exp(); 12731558Srgrimes } else { 1274192934Srmacklem if (ep) { 1275192934Srmacklem if (ep->ex_fs.val[0] != 1276192934Srmacklem fsb.f_fsid.val[0] || 1277192934Srmacklem ep->ex_fs.val[1] != 1278192934Srmacklem fsb.f_fsid.val[1]) { 1279192934Srmacklem getexp_err(ep, tgrp); 1280192934Srmacklem goto nextline; 1281192934Srmacklem } 1282192934Srmacklem } else { 1283192934Srmacklem /* 1284192934Srmacklem * See if this directory is already 1285192934Srmacklem * in the list. 1286192934Srmacklem */ 1287192934Srmacklem ep = ex_search(&fsb.f_fsid); 1288192934Srmacklem if (ep == (struct exportlist *)NULL) { 1289192934Srmacklem ep = get_exp(); 1290192934Srmacklem ep->ex_fs = fsb.f_fsid; 1291192934Srmacklem ep->ex_fsdir = (char *)malloc 1292192934Srmacklem (strlen(fsb.f_mntonname) + 1); 1293192934Srmacklem if (ep->ex_fsdir) 1294192934Srmacklem strcpy(ep->ex_fsdir, 1295192934Srmacklem fsb.f_mntonname); 1296192934Srmacklem else 1297192934Srmacklem out_of_mem(); 1298192934Srmacklem if (debug) 1299192934Srmacklem warnx( 1300192934Srmacklem "making new ep fs=0x%x,0x%x", 1301192934Srmacklem fsb.f_fsid.val[0], 1302192934Srmacklem fsb.f_fsid.val[1]); 1303192934Srmacklem } else if (debug) 1304192934Srmacklem warnx("found ep fs=0x%x,0x%x", 1305192934Srmacklem fsb.f_fsid.val[0], 1306192934Srmacklem fsb.f_fsid.val[1]); 1307192934Srmacklem } 1308192934Srmacklem 13091558Srgrimes /* 1310192934Srmacklem * Add dirpath to export mount point. 13111558Srgrimes */ 1312192934Srmacklem dirp = add_expdir(&dirhead, cp, len); 1313192934Srmacklem dirplen = len; 13141558Srgrimes } 13151558Srgrimes } else { 13161558Srgrimes getexp_err(ep, tgrp); 13171558Srgrimes goto nextline; 13181558Srgrimes } 13191558Srgrimes *endcp = savedc; 13201558Srgrimes } else { 13211558Srgrimes savedc = *endcp; 13221558Srgrimes *endcp = '\0'; 13231558Srgrimes got_nondir = 1; 13241558Srgrimes if (ep == (struct exportlist *)NULL) { 13251558Srgrimes getexp_err(ep, tgrp); 13261558Srgrimes goto nextline; 13271558Srgrimes } 13281558Srgrimes 13291558Srgrimes /* 13301558Srgrimes * Get the host or netgroup. 13311558Srgrimes */ 13321558Srgrimes setnetgrent(cp); 13331558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 13341558Srgrimes do { 13351558Srgrimes if (has_host) { 13361558Srgrimes grp->gr_next = get_grp(); 13371558Srgrimes grp = grp->gr_next; 13381558Srgrimes } 13391558Srgrimes if (netgrp) { 134037003Sjoerg if (hst == 0) { 134137663Scharnier syslog(LOG_ERR, 134237663Scharnier "null hostname in netgroup %s, skipping", cp); 134337004Sjoerg grp->gr_type = GT_IGNORE; 134437003Sjoerg } else if (get_host(hst, grp, tgrp)) { 134537663Scharnier syslog(LOG_ERR, 134637663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 134729317Sjlemon grp->gr_type = GT_IGNORE; 13481558Srgrimes } 13497401Swpaul } else if (get_host(cp, grp, tgrp)) { 135037663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 135129317Sjlemon grp->gr_type = GT_IGNORE; 13521558Srgrimes } 13531558Srgrimes has_host = TRUE; 13541558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 13551558Srgrimes endnetgrent(); 13561558Srgrimes *endcp = savedc; 13571558Srgrimes } 13581558Srgrimes cp = endcp; 13591558Srgrimes nextfield(&cp, &endcp); 13601558Srgrimes len = endcp - cp; 13611558Srgrimes } 13621558Srgrimes if (check_options(dirhead)) { 13631558Srgrimes getexp_err(ep, tgrp); 13641558Srgrimes goto nextline; 13651558Srgrimes } 13661558Srgrimes if (!has_host) { 136775641Siedowse grp->gr_type = GT_DEFAULT; 13681558Srgrimes if (debug) 136937663Scharnier warnx("adding a default entry"); 13701558Srgrimes 13711558Srgrimes /* 13721558Srgrimes * Don't allow a network export coincide with a list of 13731558Srgrimes * host(s) on the same line. 13741558Srgrimes */ 13751558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 137675801Siedowse syslog(LOG_ERR, "network/host conflict"); 13771558Srgrimes getexp_err(ep, tgrp); 13781558Srgrimes goto nextline; 137929317Sjlemon 138074462Salfred /* 138174462Salfred * If an export list was specified on this line, make sure 138229317Sjlemon * that we have at least one valid entry, otherwise skip it. 138329317Sjlemon */ 138429317Sjlemon } else { 138529317Sjlemon grp = tgrp; 138674462Salfred while (grp && grp->gr_type == GT_IGNORE) 138729317Sjlemon grp = grp->gr_next; 138829317Sjlemon if (! grp) { 138929317Sjlemon getexp_err(ep, tgrp); 139029317Sjlemon goto nextline; 139129317Sjlemon } 13921558Srgrimes } 13931558Srgrimes 1394192934Srmacklem if (v4root_phase == 1) { 1395192934Srmacklem syslog(LOG_ERR, "V4:root, no dirp, ignored"); 1396192934Srmacklem getexp_err(ep, tgrp); 1397192934Srmacklem goto nextline; 1398192934Srmacklem } 1399192934Srmacklem 14001558Srgrimes /* 14011558Srgrimes * Loop through hosts, pushing the exports into the kernel. 14021558Srgrimes * After loop, tgrp points to the start of the list and 14031558Srgrimes * grp points to the last entry in the list. 14041558Srgrimes */ 14051558Srgrimes grp = tgrp; 14061558Srgrimes do { 140775635Siedowse if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 140875635Siedowse &fsb)) { 140975635Siedowse getexp_err(ep, tgrp); 141075635Siedowse goto nextline; 141175635Siedowse } 14121558Srgrimes } while (grp->gr_next && (grp = grp->gr_next)); 14131558Srgrimes 14141558Srgrimes /* 1415192934Srmacklem * For V4: don't enter in mount lists. 1416192934Srmacklem */ 1417192934Srmacklem if (v4root_phase > 0 && v4root_phase <= 2) 1418192934Srmacklem goto nextline; 1419192934Srmacklem 1420192934Srmacklem /* 14211558Srgrimes * Success. Update the data structures. 14221558Srgrimes */ 14231558Srgrimes if (has_host) { 14249336Sdfr hang_dirp(dirhead, tgrp, ep, opt_flags); 14251558Srgrimes grp->gr_next = grphead; 14261558Srgrimes grphead = tgrp; 14271558Srgrimes } else { 14281558Srgrimes hang_dirp(dirhead, (struct grouplist *)NULL, ep, 14299336Sdfr opt_flags); 14301558Srgrimes free_grp(grp); 14311558Srgrimes } 14321558Srgrimes dirhead = (struct dirlist *)NULL; 14331558Srgrimes if ((ep->ex_flag & EX_LINKED) == 0) { 14341558Srgrimes ep2 = exphead; 14351558Srgrimes epp = &exphead; 14361558Srgrimes 14371558Srgrimes /* 14381558Srgrimes * Insert in the list in alphabetical order. 14391558Srgrimes */ 14401558Srgrimes while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { 14411558Srgrimes epp = &ep2->ex_next; 14421558Srgrimes ep2 = ep2->ex_next; 14431558Srgrimes } 14441558Srgrimes if (ep2) 14451558Srgrimes ep->ex_next = ep2; 14461558Srgrimes *epp = ep; 14471558Srgrimes ep->ex_flag |= EX_LINKED; 14481558Srgrimes } 14491558Srgrimesnextline: 1450192934Srmacklem v4root_phase = 0; 14511558Srgrimes if (dirhead) { 14521558Srgrimes free_dir(dirhead); 14531558Srgrimes dirhead = (struct dirlist *)NULL; 14541558Srgrimes } 14551558Srgrimes } 14561558Srgrimes} 14571558Srgrimes 14581558Srgrimes/* 1459166440Spjd * Get the export list from all specified files 1460166440Spjd */ 1461166440Spjdvoid 1462166440Spjdget_exportlist() 1463166440Spjd{ 1464166440Spjd struct exportlist *ep, *ep2; 1465166440Spjd struct grouplist *grp, *tgrp; 1466166440Spjd struct export_args export; 1467166440Spjd struct iovec *iov; 1468166440Spjd struct statfs *fsp, *mntbufp; 1469166440Spjd struct xvfsconf vfc; 1470166440Spjd char *dirp; 1471166440Spjd char errmsg[255]; 1472166440Spjd int dirplen, num, i; 1473166440Spjd int iovlen; 1474168684Spjd int done; 1475192934Srmacklem struct nfsex_args eargs; 1476166440Spjd 1477192934Srmacklem v4root_dirpath[0] = '\0'; 1478166440Spjd bzero(&export, sizeof(export)); 1479166440Spjd export.ex_flags = MNT_DELEXPORT; 1480166440Spjd dirp = NULL; 1481166440Spjd dirplen = 0; 1482166440Spjd iov = NULL; 1483166440Spjd iovlen = 0; 1484166440Spjd bzero(errmsg, sizeof(errmsg)); 1485166440Spjd 1486166440Spjd /* 1487166440Spjd * First, get rid of the old list 1488166440Spjd */ 1489166440Spjd ep = exphead; 1490166440Spjd while (ep) { 1491166440Spjd ep2 = ep; 1492166440Spjd ep = ep->ex_next; 1493166440Spjd free_exp(ep2); 1494166440Spjd } 1495166440Spjd exphead = (struct exportlist *)NULL; 1496166440Spjd 1497166440Spjd grp = grphead; 1498166440Spjd while (grp) { 1499166440Spjd tgrp = grp; 1500166440Spjd grp = grp->gr_next; 1501166440Spjd free_grp(tgrp); 1502166440Spjd } 1503166440Spjd grphead = (struct grouplist *)NULL; 1504166440Spjd 1505166440Spjd /* 1506192934Srmacklem * and the old V4 root dir. 1507192934Srmacklem */ 1508192934Srmacklem bzero(&eargs, sizeof (eargs)); 1509192934Srmacklem eargs.export.ex_flags = MNT_DELEXPORT; 1510192934Srmacklem if (run_v4server > 0 && 1511192934Srmacklem nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 && 1512192934Srmacklem errno != ENOENT) 1513192934Srmacklem syslog(LOG_ERR, "Can't delete exports for V4:"); 1514192934Srmacklem 1515192934Srmacklem /* 1516192934Srmacklem * and clear flag that notes if a public fh has been exported. 1517192934Srmacklem */ 1518192934Srmacklem has_publicfh = 0; 1519192934Srmacklem 1520192934Srmacklem /* 1521166440Spjd * And delete exports that are in the kernel for all local 1522166440Spjd * filesystems. 1523166440Spjd * XXX: Should know how to handle all local exportable filesystems. 1524166440Spjd */ 1525166440Spjd num = getmntinfo(&mntbufp, MNT_NOWAIT); 1526166440Spjd 1527166440Spjd if (num > 0) { 1528166440Spjd build_iovec(&iov, &iovlen, "fstype", NULL, 0); 1529166440Spjd build_iovec(&iov, &iovlen, "fspath", NULL, 0); 1530166440Spjd build_iovec(&iov, &iovlen, "from", NULL, 0); 1531166440Spjd build_iovec(&iov, &iovlen, "update", NULL, 0); 1532166440Spjd build_iovec(&iov, &iovlen, "export", &export, sizeof(export)); 1533166440Spjd build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 1534166440Spjd } 1535166440Spjd 1536166440Spjd for (i = 0; i < num; i++) { 1537166440Spjd fsp = &mntbufp[i]; 1538166440Spjd if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) { 1539166440Spjd syslog(LOG_ERR, "getvfsbyname() failed for %s", 1540166440Spjd fsp->f_fstypename); 1541166440Spjd continue; 1542166440Spjd } 1543166440Spjd 1544166440Spjd /* 1545166440Spjd * Do not delete export for network filesystem by 1546166440Spjd * passing "export" arg to nmount(). 1547166440Spjd * It only makes sense to do this for local filesystems. 1548166440Spjd */ 1549166440Spjd if (vfc.vfc_flags & VFCF_NETWORK) 1550166440Spjd continue; 1551166440Spjd 1552166440Spjd iov[1].iov_base = fsp->f_fstypename; 1553166440Spjd iov[1].iov_len = strlen(fsp->f_fstypename) + 1; 1554166440Spjd iov[3].iov_base = fsp->f_mntonname; 1555166440Spjd iov[3].iov_len = strlen(fsp->f_mntonname) + 1; 1556166440Spjd iov[5].iov_base = fsp->f_mntfromname; 1557166440Spjd iov[5].iov_len = strlen(fsp->f_mntfromname) + 1; 1558166440Spjd 1559166440Spjd if (nmount(iov, iovlen, fsp->f_flags) < 0 && 1560166440Spjd errno != ENOENT && errno != ENOTSUP) { 1561166440Spjd syslog(LOG_ERR, 1562166440Spjd "can't delete exports for %s: %m %s", 1563166440Spjd fsp->f_mntonname, errmsg); 1564166440Spjd } 1565166440Spjd } 1566166440Spjd 1567166440Spjd if (iov != NULL) { 1568166440Spjd /* Free strings allocated by strdup() in getmntopts.c */ 1569166440Spjd free(iov[0].iov_base); /* fstype */ 1570166440Spjd free(iov[2].iov_base); /* fspath */ 1571166440Spjd free(iov[4].iov_base); /* from */ 1572166440Spjd free(iov[6].iov_base); /* update */ 1573166440Spjd free(iov[8].iov_base); /* export */ 1574166440Spjd free(iov[10].iov_base); /* errmsg */ 1575166440Spjd 1576166440Spjd /* free iov, allocated by realloc() */ 1577166440Spjd free(iov); 1578166440Spjd iovlen = 0; 1579166440Spjd } 1580166440Spjd 1581166440Spjd /* 1582166440Spjd * Read in the exports file and build the list, calling 1583166440Spjd * nmount() as we go along to push the export rules into the kernel. 1584166440Spjd */ 1585168684Spjd done = 0; 1586166440Spjd for (i = 0; exnames[i] != NULL; i++) { 1587166440Spjd if (debug) 1588166440Spjd warnx("reading exports from %s", exnames[i]); 1589166440Spjd if ((exp_file = fopen(exnames[i], "r")) == NULL) { 1590168684Spjd syslog(LOG_WARNING, "can't open %s", exnames[i]); 1591168684Spjd continue; 1592166440Spjd } 1593166440Spjd get_exportlist_one(); 1594166440Spjd fclose(exp_file); 1595168684Spjd done++; 1596166440Spjd } 1597168684Spjd if (done == 0) { 1598168684Spjd syslog(LOG_ERR, "can't open any exports file"); 1599168684Spjd exit(2); 1600168684Spjd } 1601192934Srmacklem 1602192934Srmacklem /* 1603192934Srmacklem * If there was no public fh, clear any previous one set. 1604192934Srmacklem */ 1605192934Srmacklem if (run_v4server > 0 && has_publicfh == 0) 1606192934Srmacklem (void) nfssvc(NFSSVC_NOPUBLICFH, NULL); 1607166440Spjd} 1608166440Spjd 1609166440Spjd/* 16101558Srgrimes * Allocate an export list element 16111558Srgrimes */ 16121558Srgrimesstruct exportlist * 16131558Srgrimesget_exp() 16141558Srgrimes{ 16151558Srgrimes struct exportlist *ep; 16161558Srgrimes 16171558Srgrimes ep = (struct exportlist *)malloc(sizeof (struct exportlist)); 16181558Srgrimes if (ep == (struct exportlist *)NULL) 16191558Srgrimes out_of_mem(); 162023681Speter memset(ep, 0, sizeof(struct exportlist)); 16211558Srgrimes return (ep); 16221558Srgrimes} 16231558Srgrimes 16241558Srgrimes/* 16251558Srgrimes * Allocate a group list element 16261558Srgrimes */ 16271558Srgrimesstruct grouplist * 16281558Srgrimesget_grp() 16291558Srgrimes{ 16301558Srgrimes struct grouplist *gp; 16311558Srgrimes 16321558Srgrimes gp = (struct grouplist *)malloc(sizeof (struct grouplist)); 16331558Srgrimes if (gp == (struct grouplist *)NULL) 16341558Srgrimes out_of_mem(); 163523681Speter memset(gp, 0, sizeof(struct grouplist)); 16361558Srgrimes return (gp); 16371558Srgrimes} 16381558Srgrimes 16391558Srgrimes/* 16401558Srgrimes * Clean up upon an error in get_exportlist(). 16411558Srgrimes */ 16421558Srgrimesvoid 16431558Srgrimesgetexp_err(ep, grp) 16441558Srgrimes struct exportlist *ep; 16451558Srgrimes struct grouplist *grp; 16461558Srgrimes{ 16471558Srgrimes struct grouplist *tgrp; 16481558Srgrimes 1649100336Sjoerg if (!(opt_flags & OP_QUIET)) 1650100336Sjoerg syslog(LOG_ERR, "bad exports list line %s", line); 16511558Srgrimes if (ep && (ep->ex_flag & EX_LINKED) == 0) 16521558Srgrimes free_exp(ep); 16531558Srgrimes while (grp) { 16541558Srgrimes tgrp = grp; 16551558Srgrimes grp = grp->gr_next; 16561558Srgrimes free_grp(tgrp); 16571558Srgrimes } 16581558Srgrimes} 16591558Srgrimes 16601558Srgrimes/* 16611558Srgrimes * Search the export list for a matching fs. 16621558Srgrimes */ 16631558Srgrimesstruct exportlist * 16641558Srgrimesex_search(fsid) 16651558Srgrimes fsid_t *fsid; 16661558Srgrimes{ 16671558Srgrimes struct exportlist *ep; 16681558Srgrimes 16691558Srgrimes ep = exphead; 16701558Srgrimes while (ep) { 16711558Srgrimes if (ep->ex_fs.val[0] == fsid->val[0] && 16721558Srgrimes ep->ex_fs.val[1] == fsid->val[1]) 16731558Srgrimes return (ep); 16741558Srgrimes ep = ep->ex_next; 16751558Srgrimes } 16761558Srgrimes return (ep); 16771558Srgrimes} 16781558Srgrimes 16791558Srgrimes/* 16801558Srgrimes * Add a directory path to the list. 16811558Srgrimes */ 16821558Srgrimeschar * 16831558Srgrimesadd_expdir(dpp, cp, len) 16841558Srgrimes struct dirlist **dpp; 16851558Srgrimes char *cp; 16861558Srgrimes int len; 16871558Srgrimes{ 16881558Srgrimes struct dirlist *dp; 16891558Srgrimes 16901558Srgrimes dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); 169137663Scharnier if (dp == (struct dirlist *)NULL) 169237663Scharnier out_of_mem(); 16931558Srgrimes dp->dp_left = *dpp; 16941558Srgrimes dp->dp_right = (struct dirlist *)NULL; 16951558Srgrimes dp->dp_flag = 0; 16961558Srgrimes dp->dp_hosts = (struct hostlist *)NULL; 16971558Srgrimes strcpy(dp->dp_dirp, cp); 16981558Srgrimes *dpp = dp; 16991558Srgrimes return (dp->dp_dirp); 17001558Srgrimes} 17011558Srgrimes 17021558Srgrimes/* 17031558Srgrimes * Hang the dir list element off the dirpath binary tree as required 17041558Srgrimes * and update the entry for host. 17051558Srgrimes */ 17061558Srgrimesvoid 17079336Sdfrhang_dirp(dp, grp, ep, flags) 17081558Srgrimes struct dirlist *dp; 17091558Srgrimes struct grouplist *grp; 17101558Srgrimes struct exportlist *ep; 17119336Sdfr int flags; 17121558Srgrimes{ 17131558Srgrimes struct hostlist *hp; 17141558Srgrimes struct dirlist *dp2; 17151558Srgrimes 17169336Sdfr if (flags & OP_ALLDIRS) { 17171558Srgrimes if (ep->ex_defdir) 17181558Srgrimes free((caddr_t)dp); 17191558Srgrimes else 17201558Srgrimes ep->ex_defdir = dp; 17219336Sdfr if (grp == (struct grouplist *)NULL) { 17221558Srgrimes ep->ex_defdir->dp_flag |= DP_DEFSET; 17239336Sdfr } else while (grp) { 17241558Srgrimes hp = get_ht(); 17251558Srgrimes hp->ht_grp = grp; 17261558Srgrimes hp->ht_next = ep->ex_defdir->dp_hosts; 17271558Srgrimes ep->ex_defdir->dp_hosts = hp; 17281558Srgrimes grp = grp->gr_next; 17291558Srgrimes } 17301558Srgrimes } else { 17311558Srgrimes 17321558Srgrimes /* 173337663Scharnier * Loop through the directories adding them to the tree. 17341558Srgrimes */ 17351558Srgrimes while (dp) { 17361558Srgrimes dp2 = dp->dp_left; 17379336Sdfr add_dlist(&ep->ex_dirl, dp, grp, flags); 17381558Srgrimes dp = dp2; 17391558Srgrimes } 17401558Srgrimes } 17411558Srgrimes} 17421558Srgrimes 17431558Srgrimes/* 17441558Srgrimes * Traverse the binary tree either updating a node that is already there 17451558Srgrimes * for the new directory or adding the new node. 17461558Srgrimes */ 17471558Srgrimesvoid 17489336Sdfradd_dlist(dpp, newdp, grp, flags) 17491558Srgrimes struct dirlist **dpp; 17501558Srgrimes struct dirlist *newdp; 17511558Srgrimes struct grouplist *grp; 17529336Sdfr int flags; 17531558Srgrimes{ 17541558Srgrimes struct dirlist *dp; 17551558Srgrimes struct hostlist *hp; 17561558Srgrimes int cmp; 17571558Srgrimes 17581558Srgrimes dp = *dpp; 17591558Srgrimes if (dp) { 17601558Srgrimes cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 17611558Srgrimes if (cmp > 0) { 17629336Sdfr add_dlist(&dp->dp_left, newdp, grp, flags); 17631558Srgrimes return; 17641558Srgrimes } else if (cmp < 0) { 17659336Sdfr add_dlist(&dp->dp_right, newdp, grp, flags); 17661558Srgrimes return; 17671558Srgrimes } else 17681558Srgrimes free((caddr_t)newdp); 17691558Srgrimes } else { 17701558Srgrimes dp = newdp; 17711558Srgrimes dp->dp_left = (struct dirlist *)NULL; 17721558Srgrimes *dpp = dp; 17731558Srgrimes } 17741558Srgrimes if (grp) { 17751558Srgrimes 17761558Srgrimes /* 17771558Srgrimes * Hang all of the host(s) off of the directory point. 17781558Srgrimes */ 17791558Srgrimes do { 17801558Srgrimes hp = get_ht(); 17811558Srgrimes hp->ht_grp = grp; 17821558Srgrimes hp->ht_next = dp->dp_hosts; 17831558Srgrimes dp->dp_hosts = hp; 17841558Srgrimes grp = grp->gr_next; 17851558Srgrimes } while (grp); 17869336Sdfr } else { 17871558Srgrimes dp->dp_flag |= DP_DEFSET; 17889336Sdfr } 17891558Srgrimes} 17901558Srgrimes 17911558Srgrimes/* 17921558Srgrimes * Search for a dirpath on the export point. 17931558Srgrimes */ 17941558Srgrimesstruct dirlist * 179574462Salfreddirp_search(dp, dirp) 17961558Srgrimes struct dirlist *dp; 179774462Salfred char *dirp; 17981558Srgrimes{ 17991558Srgrimes int cmp; 18001558Srgrimes 18011558Srgrimes if (dp) { 180274462Salfred cmp = strcmp(dp->dp_dirp, dirp); 18031558Srgrimes if (cmp > 0) 180474462Salfred return (dirp_search(dp->dp_left, dirp)); 18051558Srgrimes else if (cmp < 0) 180674462Salfred return (dirp_search(dp->dp_right, dirp)); 18071558Srgrimes else 18081558Srgrimes return (dp); 18091558Srgrimes } 18101558Srgrimes return (dp); 18111558Srgrimes} 18121558Srgrimes 18131558Srgrimes/* 18141558Srgrimes * Scan for a host match in a directory tree. 18151558Srgrimes */ 18161558Srgrimesint 18179336Sdfrchk_host(dp, saddr, defsetp, hostsetp) 18181558Srgrimes struct dirlist *dp; 181974462Salfred struct sockaddr *saddr; 18201558Srgrimes int *defsetp; 18219336Sdfr int *hostsetp; 18221558Srgrimes{ 18231558Srgrimes struct hostlist *hp; 18241558Srgrimes struct grouplist *grp; 182574462Salfred struct addrinfo *ai; 18261558Srgrimes 18271558Srgrimes if (dp) { 18281558Srgrimes if (dp->dp_flag & DP_DEFSET) 18299336Sdfr *defsetp = dp->dp_flag; 18301558Srgrimes hp = dp->dp_hosts; 18311558Srgrimes while (hp) { 18321558Srgrimes grp = hp->ht_grp; 18331558Srgrimes switch (grp->gr_type) { 18341558Srgrimes case GT_HOST: 183574462Salfred ai = grp->gr_ptr.gt_addrinfo; 183674462Salfred for (; ai; ai = ai->ai_next) { 183775801Siedowse if (!sacmp(ai->ai_addr, saddr, NULL)) { 183874462Salfred *hostsetp = 183974462Salfred (hp->ht_flag | DP_HOSTSET); 184074462Salfred return (1); 184174462Salfred } 18429336Sdfr } 184375801Siedowse break; 18441558Srgrimes case GT_NET: 184575801Siedowse if (!sacmp(saddr, (struct sockaddr *) 184675801Siedowse &grp->gr_ptr.gt_net.nt_net, 184775801Siedowse (struct sockaddr *) 184875801Siedowse &grp->gr_ptr.gt_net.nt_mask)) { 184974462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 185074462Salfred return (1); 185174462Salfred } 185275801Siedowse break; 185375801Siedowse } 18541558Srgrimes hp = hp->ht_next; 18551558Srgrimes } 18561558Srgrimes } 18571558Srgrimes return (0); 18581558Srgrimes} 18591558Srgrimes 18601558Srgrimes/* 18611558Srgrimes * Scan tree for a host that matches the address. 18621558Srgrimes */ 18631558Srgrimesint 18641558Srgrimesscan_tree(dp, saddr) 18651558Srgrimes struct dirlist *dp; 186674462Salfred struct sockaddr *saddr; 18671558Srgrimes{ 18689336Sdfr int defset, hostset; 18691558Srgrimes 18701558Srgrimes if (dp) { 18711558Srgrimes if (scan_tree(dp->dp_left, saddr)) 18721558Srgrimes return (1); 18739336Sdfr if (chk_host(dp, saddr, &defset, &hostset)) 18741558Srgrimes return (1); 18751558Srgrimes if (scan_tree(dp->dp_right, saddr)) 18761558Srgrimes return (1); 18771558Srgrimes } 18781558Srgrimes return (0); 18791558Srgrimes} 18801558Srgrimes 18811558Srgrimes/* 18821558Srgrimes * Traverse the dirlist tree and free it up. 18831558Srgrimes */ 18841558Srgrimesvoid 18851558Srgrimesfree_dir(dp) 18861558Srgrimes struct dirlist *dp; 18871558Srgrimes{ 18881558Srgrimes 18891558Srgrimes if (dp) { 18901558Srgrimes free_dir(dp->dp_left); 18911558Srgrimes free_dir(dp->dp_right); 18921558Srgrimes free_host(dp->dp_hosts); 18931558Srgrimes free((caddr_t)dp); 18941558Srgrimes } 18951558Srgrimes} 18961558Srgrimes 18971558Srgrimes/* 1898184588Sdfr * Parse a colon separated list of security flavors 1899184588Sdfr */ 1900184588Sdfrint 1901184588Sdfrparsesec(seclist, ep) 1902184588Sdfr char *seclist; 1903184588Sdfr struct exportlist *ep; 1904184588Sdfr{ 1905184588Sdfr char *cp, savedc; 1906184588Sdfr int flavor; 1907184588Sdfr 1908184588Sdfr ep->ex_numsecflavors = 0; 1909184588Sdfr for (;;) { 1910184588Sdfr cp = strchr(seclist, ':'); 1911184588Sdfr if (cp) { 1912184588Sdfr savedc = *cp; 1913184588Sdfr *cp = '\0'; 1914184588Sdfr } 1915184588Sdfr 1916184588Sdfr if (!strcmp(seclist, "sys")) 1917184588Sdfr flavor = AUTH_SYS; 1918184588Sdfr else if (!strcmp(seclist, "krb5")) 1919184588Sdfr flavor = RPCSEC_GSS_KRB5; 1920184588Sdfr else if (!strcmp(seclist, "krb5i")) 1921184588Sdfr flavor = RPCSEC_GSS_KRB5I; 1922184588Sdfr else if (!strcmp(seclist, "krb5p")) 1923184588Sdfr flavor = RPCSEC_GSS_KRB5P; 1924184588Sdfr else { 1925184588Sdfr if (cp) 1926184588Sdfr *cp = savedc; 1927184588Sdfr syslog(LOG_ERR, "bad sec flavor: %s", seclist); 1928184588Sdfr return (1); 1929184588Sdfr } 1930184588Sdfr if (ep->ex_numsecflavors == MAXSECFLAVORS) { 1931184588Sdfr if (cp) 1932184588Sdfr *cp = savedc; 1933184588Sdfr syslog(LOG_ERR, "too many sec flavors: %s", seclist); 1934184588Sdfr return (1); 1935184588Sdfr } 1936184588Sdfr ep->ex_secflavors[ep->ex_numsecflavors] = flavor; 1937184588Sdfr ep->ex_numsecflavors++; 1938184588Sdfr if (cp) { 1939184588Sdfr *cp = savedc; 1940184588Sdfr seclist = cp + 1; 1941184588Sdfr } else { 1942184588Sdfr break; 1943184588Sdfr } 1944184588Sdfr } 1945184588Sdfr return (0); 1946184588Sdfr} 1947184588Sdfr 1948184588Sdfr/* 19491558Srgrimes * Parse the option string and update fields. 19501558Srgrimes * Option arguments may either be -<option>=<value> or 19511558Srgrimes * -<option> <value> 19521558Srgrimes */ 19531558Srgrimesint 19541558Srgrimesdo_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr) 19551558Srgrimes char **cpp, **endcpp; 19561558Srgrimes struct exportlist *ep; 19571558Srgrimes struct grouplist *grp; 19581558Srgrimes int *has_hostp; 19591558Srgrimes int *exflagsp; 196072650Sgreen struct xucred *cr; 19611558Srgrimes{ 19621558Srgrimes char *cpoptarg, *cpoptend; 19631558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 19641558Srgrimes int allflag, usedarg; 19651558Srgrimes 196651968Salfred savedc2 = '\0'; 19671558Srgrimes cpopt = *cpp; 19681558Srgrimes cpopt++; 19691558Srgrimes cp = *endcpp; 19701558Srgrimes savedc = *cp; 19711558Srgrimes *cp = '\0'; 19721558Srgrimes while (cpopt && *cpopt) { 19731558Srgrimes allflag = 1; 19741558Srgrimes usedarg = -2; 197537663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 19761558Srgrimes *cpoptend++ = '\0'; 197737663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 19781558Srgrimes *cpoptarg++ = '\0'; 19791558Srgrimes } else { 198037663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 19811558Srgrimes *cpoptarg++ = '\0'; 19821558Srgrimes else { 19831558Srgrimes *cp = savedc; 19841558Srgrimes nextfield(&cp, &endcp); 19851558Srgrimes **endcpp = '\0'; 19861558Srgrimes if (endcp > cp && *cp != '-') { 19871558Srgrimes cpoptarg = cp; 19881558Srgrimes savedc2 = *endcp; 19891558Srgrimes *endcp = '\0'; 19901558Srgrimes usedarg = 0; 19911558Srgrimes } 19921558Srgrimes } 19931558Srgrimes } 19941558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 19951558Srgrimes *exflagsp |= MNT_EXRDONLY; 19961558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 19971558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 19981558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 19991558Srgrimes usedarg++; 20001558Srgrimes parsecred(cpoptarg, cr); 20011558Srgrimes if (allflag == 0) { 20021558Srgrimes *exflagsp |= MNT_EXPORTANON; 20031558Srgrimes opt_flags |= OP_MAPALL; 20041558Srgrimes } else 20051558Srgrimes opt_flags |= OP_MAPROOT; 20061558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 200775801Siedowse !strcmp(cpopt, "m"))) { 20081558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 200937663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 20101558Srgrimes return (1); 20111558Srgrimes } 20121558Srgrimes usedarg++; 20131558Srgrimes opt_flags |= OP_MASK; 20141558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 20151558Srgrimes !strcmp(cpopt, "n"))) { 201674462Salfred if (strchr(cpoptarg, '/') != NULL) { 201774462Salfred if (debug) 201874462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 201974462Salfred opt_flags |= OP_MASKLEN; 202074462Salfred } 20211558Srgrimes if (grp->gr_type != GT_NULL) { 202237663Scharnier syslog(LOG_ERR, "network/host conflict"); 20231558Srgrimes return (1); 20241558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 202537663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 20261558Srgrimes return (1); 20271558Srgrimes } 20281558Srgrimes grp->gr_type = GT_NET; 20291558Srgrimes *has_hostp = 1; 20301558Srgrimes usedarg++; 20311558Srgrimes opt_flags |= OP_NET; 20321558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 20331558Srgrimes opt_flags |= OP_ALLDIRS; 203427447Sdfr } else if (!strcmp(cpopt, "public")) { 203527447Sdfr *exflagsp |= MNT_EXPUBLIC; 203627447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 203727447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 203827447Sdfr opt_flags |= OP_MAPALL; 203927447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 204027447Sdfr ep->ex_indexfile = strdup(cpoptarg); 2041100336Sjoerg } else if (!strcmp(cpopt, "quiet")) { 2042100336Sjoerg opt_flags |= OP_QUIET; 2043184588Sdfr } else if (!strcmp(cpopt, "sec")) { 2044184588Sdfr if (parsesec(cpoptarg, ep)) 2045184588Sdfr return (1); 2046184588Sdfr opt_flags |= OP_SEC; 2047184588Sdfr usedarg++; 20481558Srgrimes } else { 204937663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 20501558Srgrimes return (1); 20511558Srgrimes } 20521558Srgrimes if (usedarg >= 0) { 20531558Srgrimes *endcp = savedc2; 20541558Srgrimes **endcpp = savedc; 20551558Srgrimes if (usedarg > 0) { 20561558Srgrimes *cpp = cp; 20571558Srgrimes *endcpp = endcp; 20581558Srgrimes } 20591558Srgrimes return (0); 20601558Srgrimes } 20611558Srgrimes cpopt = cpoptend; 20621558Srgrimes } 20631558Srgrimes **endcpp = savedc; 20641558Srgrimes return (0); 20651558Srgrimes} 20661558Srgrimes 20671558Srgrimes/* 20681558Srgrimes * Translate a character string to the corresponding list of network 20691558Srgrimes * addresses for a hostname. 20701558Srgrimes */ 20711558Srgrimesint 20727401Swpaulget_host(cp, grp, tgrp) 20731558Srgrimes char *cp; 20741558Srgrimes struct grouplist *grp; 20757401Swpaul struct grouplist *tgrp; 20761558Srgrimes{ 20777401Swpaul struct grouplist *checkgrp; 207875635Siedowse struct addrinfo *ai, *tai, hints; 207974462Salfred int ecode; 208074462Salfred char host[NI_MAXHOST]; 20811558Srgrimes 208274462Salfred if (grp->gr_type != GT_NULL) { 208374462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 20841558Srgrimes return (1); 20851558Srgrimes } 208674462Salfred memset(&hints, 0, sizeof hints); 208774462Salfred hints.ai_flags = AI_CANONNAME; 208874462Salfred hints.ai_protocol = IPPROTO_UDP; 208974462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 209074462Salfred if (ecode != 0) { 209175635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 209274462Salfred return 1; 209374462Salfred } 209474462Salfred grp->gr_ptr.gt_addrinfo = ai; 209574462Salfred while (ai != NULL) { 209674462Salfred if (ai->ai_canonname == NULL) { 209774462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 2098146187Sume sizeof host, NULL, 0, NI_NUMERICHOST) != 0) 209974462Salfred strlcpy(host, "?", sizeof(host)); 210074462Salfred ai->ai_canonname = strdup(host); 210174462Salfred ai->ai_flags |= AI_CANONNAME; 210275641Siedowse } 210374462Salfred if (debug) 210475635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 210575635Siedowse /* 210675635Siedowse * Sanity check: make sure we don't already have an entry 210775635Siedowse * for this host in the grouplist. 210875635Siedowse */ 210975635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 211075635Siedowse checkgrp = checkgrp->gr_next) { 211175635Siedowse if (checkgrp->gr_type != GT_HOST) 211275635Siedowse continue; 211375635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 211475635Siedowse tai = tai->ai_next) { 211575801Siedowse if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0) 211675635Siedowse continue; 211775635Siedowse if (debug) 211875635Siedowse fprintf(stderr, 211975635Siedowse "ignoring duplicate host %s\n", 212075635Siedowse ai->ai_canonname); 212175635Siedowse grp->gr_type = GT_IGNORE; 212275635Siedowse return (0); 212375635Siedowse } 212475635Siedowse } 212574462Salfred ai = ai->ai_next; 21261558Srgrimes } 212775635Siedowse grp->gr_type = GT_HOST; 21281558Srgrimes return (0); 21291558Srgrimes} 21301558Srgrimes 21311558Srgrimes/* 21321558Srgrimes * Free up an exports list component 21331558Srgrimes */ 21341558Srgrimesvoid 21351558Srgrimesfree_exp(ep) 21361558Srgrimes struct exportlist *ep; 21371558Srgrimes{ 21381558Srgrimes 21391558Srgrimes if (ep->ex_defdir) { 21401558Srgrimes free_host(ep->ex_defdir->dp_hosts); 21411558Srgrimes free((caddr_t)ep->ex_defdir); 21421558Srgrimes } 21431558Srgrimes if (ep->ex_fsdir) 21441558Srgrimes free(ep->ex_fsdir); 214527447Sdfr if (ep->ex_indexfile) 214627447Sdfr free(ep->ex_indexfile); 21471558Srgrimes free_dir(ep->ex_dirl); 21481558Srgrimes free((caddr_t)ep); 21491558Srgrimes} 21501558Srgrimes 21511558Srgrimes/* 21521558Srgrimes * Free hosts. 21531558Srgrimes */ 21541558Srgrimesvoid 21551558Srgrimesfree_host(hp) 21561558Srgrimes struct hostlist *hp; 21571558Srgrimes{ 21581558Srgrimes struct hostlist *hp2; 21591558Srgrimes 21601558Srgrimes while (hp) { 21611558Srgrimes hp2 = hp; 21621558Srgrimes hp = hp->ht_next; 21631558Srgrimes free((caddr_t)hp2); 21641558Srgrimes } 21651558Srgrimes} 21661558Srgrimes 21671558Srgrimesstruct hostlist * 21681558Srgrimesget_ht() 21691558Srgrimes{ 21701558Srgrimes struct hostlist *hp; 21711558Srgrimes 21721558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 21731558Srgrimes if (hp == (struct hostlist *)NULL) 21741558Srgrimes out_of_mem(); 21751558Srgrimes hp->ht_next = (struct hostlist *)NULL; 21769336Sdfr hp->ht_flag = 0; 21771558Srgrimes return (hp); 21781558Srgrimes} 21791558Srgrimes 21801558Srgrimes/* 21811558Srgrimes * Out of memory, fatal 21821558Srgrimes */ 21831558Srgrimesvoid 21841558Srgrimesout_of_mem() 21851558Srgrimes{ 21861558Srgrimes 218737663Scharnier syslog(LOG_ERR, "out of memory"); 21881558Srgrimes exit(2); 21891558Srgrimes} 21901558Srgrimes 21911558Srgrimes/* 2192158857Srodrigc * Do the nmount() syscall with the update flag to push the export info into 21931558Srgrimes * the kernel. 21941558Srgrimes */ 21951558Srgrimesint 2196158857Srodrigcdo_mount(struct exportlist *ep, struct grouplist *grp, int exflags, 2197158857Srodrigc struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb) 21981558Srgrimes{ 219975841Siedowse struct statfs fsb1; 220074462Salfred struct addrinfo *ai; 2201192934Srmacklem struct export_args ea, *eap; 2202158857Srodrigc char errmsg[255]; 2203158857Srodrigc char *cp; 22041558Srgrimes int done; 2205158857Srodrigc char savedc; 2206158857Srodrigc struct iovec *iov; 2207184588Sdfr int i, iovlen; 2208158857Srodrigc int ret; 2209192934Srmacklem struct nfsex_args nfsea; 22101558Srgrimes 2211192934Srmacklem if (run_v4server > 0) 2212192934Srmacklem eap = &nfsea.export; 2213192934Srmacklem else 2214192934Srmacklem eap = &ea; 2215192934Srmacklem 2216158857Srodrigc cp = NULL; 2217158857Srodrigc savedc = '\0'; 2218158857Srodrigc iov = NULL; 2219158857Srodrigc iovlen = 0; 2220158857Srodrigc ret = 0; 222175801Siedowse 2222192934Srmacklem bzero(eap, sizeof (struct export_args)); 2223158857Srodrigc bzero(errmsg, sizeof(errmsg)); 2224192934Srmacklem eap->ex_flags = exflags; 2225192934Srmacklem eap->ex_anon = *anoncrp; 2226192934Srmacklem eap->ex_indexfile = ep->ex_indexfile; 222775641Siedowse if (grp->gr_type == GT_HOST) 222874462Salfred ai = grp->gr_ptr.gt_addrinfo; 222975641Siedowse else 223075641Siedowse ai = NULL; 2231192934Srmacklem eap->ex_numsecflavors = ep->ex_numsecflavors; 2232192934Srmacklem for (i = 0; i < eap->ex_numsecflavors; i++) 2233192934Srmacklem eap->ex_secflavors[i] = ep->ex_secflavors[i]; 2234192934Srmacklem if (eap->ex_numsecflavors == 0) { 2235192934Srmacklem eap->ex_numsecflavors = 1; 2236192934Srmacklem eap->ex_secflavors[0] = AUTH_SYS; 2237184588Sdfr } 22381558Srgrimes done = FALSE; 2239158857Srodrigc 2240192934Srmacklem if (v4root_phase == 0) { 2241192934Srmacklem build_iovec(&iov, &iovlen, "fstype", NULL, 0); 2242192934Srmacklem build_iovec(&iov, &iovlen, "fspath", NULL, 0); 2243192934Srmacklem build_iovec(&iov, &iovlen, "from", NULL, 0); 2244192934Srmacklem build_iovec(&iov, &iovlen, "update", NULL, 0); 2245192934Srmacklem build_iovec(&iov, &iovlen, "export", eap, 2246192934Srmacklem sizeof (struct export_args)); 2247192934Srmacklem build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 2248192934Srmacklem } 2249158857Srodrigc 22501558Srgrimes while (!done) { 22511558Srgrimes switch (grp->gr_type) { 22521558Srgrimes case GT_HOST: 225375641Siedowse if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 225474462Salfred goto skip; 2255192934Srmacklem eap->ex_addr = ai->ai_addr; 2256192934Srmacklem eap->ex_addrlen = ai->ai_addrlen; 2257192934Srmacklem eap->ex_masklen = 0; 22581558Srgrimes break; 22591558Srgrimes case GT_NET: 226075801Siedowse if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 && 226174462Salfred have_v6 == 0) 226274462Salfred goto skip; 2263192934Srmacklem eap->ex_addr = 226475801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net; 2265192934Srmacklem eap->ex_addrlen = 2266158857Srodrigc ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len; 2267192934Srmacklem eap->ex_mask = 226875801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask; 2269192934Srmacklem eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len; 22701558Srgrimes break; 227175641Siedowse case GT_DEFAULT: 2272192934Srmacklem eap->ex_addr = NULL; 2273192934Srmacklem eap->ex_addrlen = 0; 2274192934Srmacklem eap->ex_mask = NULL; 2275192934Srmacklem eap->ex_masklen = 0; 227675641Siedowse break; 22777401Swpaul case GT_IGNORE: 2278158857Srodrigc ret = 0; 2279158857Srodrigc goto error_exit; 22807401Swpaul break; 22811558Srgrimes default: 228237663Scharnier syslog(LOG_ERR, "bad grouptype"); 22831558Srgrimes if (cp) 22841558Srgrimes *cp = savedc; 2285158857Srodrigc ret = 1; 2286158857Srodrigc goto error_exit; 22871558Srgrimes }; 22881558Srgrimes 22891558Srgrimes /* 2290192934Srmacklem * For V4:, use the nfssvc() syscall, instead of mount(). 22911558Srgrimes */ 2292192934Srmacklem if (v4root_phase == 2) { 2293192934Srmacklem nfsea.fspec = v4root_dirpath; 2294192934Srmacklem if (run_v4server > 0 && 2295192934Srmacklem nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) { 2296192934Srmacklem syslog(LOG_ERR, "Exporting V4: failed"); 2297192934Srmacklem return (2); 2298158857Srodrigc } 2299192934Srmacklem } else { 2300192934Srmacklem /* 2301192934Srmacklem * XXX: 2302192934Srmacklem * Maybe I should just use the fsb->f_mntonname path 2303192934Srmacklem * instead of looping back up the dirp to the mount 2304192934Srmacklem * point?? 2305192934Srmacklem * Also, needs to know how to export all types of local 2306192934Srmacklem * exportable filesystems and not just "ufs". 2307192934Srmacklem */ 2308192934Srmacklem iov[1].iov_base = fsb->f_fstypename; /* "fstype" */ 2309192934Srmacklem iov[1].iov_len = strlen(fsb->f_fstypename) + 1; 2310192934Srmacklem iov[3].iov_base = fsb->f_mntonname; /* "fspath" */ 2311192934Srmacklem iov[3].iov_len = strlen(fsb->f_mntonname) + 1; 2312192934Srmacklem iov[5].iov_base = fsb->f_mntfromname; /* "from" */ 2313192934Srmacklem iov[5].iov_len = strlen(fsb->f_mntfromname) + 1; 2314192934Srmacklem 2315192934Srmacklem while (nmount(iov, iovlen, fsb->f_flags) < 0) { 2316192934Srmacklem if (cp) 2317192934Srmacklem *cp-- = savedc; 2318192934Srmacklem else 2319192934Srmacklem cp = dirp + dirplen - 1; 2320192934Srmacklem if (opt_flags & OP_QUIET) { 2321192934Srmacklem ret = 1; 2322192934Srmacklem goto error_exit; 2323192934Srmacklem } 2324192934Srmacklem if (errno == EPERM) { 2325192934Srmacklem if (debug) 2326192934Srmacklem warnx("can't change attributes for %s", 2327192934Srmacklem dirp); 2328192934Srmacklem syslog(LOG_ERR, 2329192934Srmacklem "can't change attributes for %s", 233075635Siedowse dirp); 2331192934Srmacklem ret = 1; 2332192934Srmacklem goto error_exit; 2333192934Srmacklem } 2334192934Srmacklem if (opt_flags & OP_ALLDIRS) { 2335192934Srmacklem if (errno == EINVAL) 2336192934Srmacklem syslog(LOG_ERR, 2337100336Sjoerg "-alldirs requested but %s is not a filesystem mountpoint", 2338192934Srmacklem dirp); 2339192934Srmacklem else 2340192934Srmacklem syslog(LOG_ERR, 2341192934Srmacklem "could not remount %s: %m", 2342192934Srmacklem dirp); 2343192934Srmacklem ret = 1; 2344192934Srmacklem goto error_exit; 2345192934Srmacklem } 2346192934Srmacklem /* back up over the last component */ 2347192934Srmacklem while (*cp == '/' && cp > dirp) 2348192934Srmacklem cp--; 2349192934Srmacklem while (*(cp - 1) != '/' && cp > dirp) 2350192934Srmacklem cp--; 2351192934Srmacklem if (cp == dirp) { 2352192934Srmacklem if (debug) 2353192934Srmacklem warnx("mnt unsucc"); 2354192934Srmacklem syslog(LOG_ERR, "can't export %s %s", 2355192934Srmacklem dirp, errmsg); 2356192934Srmacklem ret = 1; 2357192934Srmacklem goto error_exit; 2358192934Srmacklem } 2359192934Srmacklem savedc = *cp; 2360192934Srmacklem *cp = '\0'; 2361192934Srmacklem /* 2362192934Srmacklem * Check that we're still on the same 2363192934Srmacklem * filesystem. 2364192934Srmacklem */ 2365192934Srmacklem if (statfs(dirp, &fsb1) != 0 || 2366192934Srmacklem bcmp(&fsb1.f_fsid, &fsb->f_fsid, 2367192934Srmacklem sizeof (fsb1.f_fsid)) != 0) { 2368192934Srmacklem *cp = savedc; 2369100336Sjoerg syslog(LOG_ERR, 2370192934Srmacklem "can't export %s %s", dirp, 2371192934Srmacklem errmsg); 2372192934Srmacklem ret = 1; 2373192934Srmacklem goto error_exit; 2374192934Srmacklem } 23751558Srgrimes } 23761558Srgrimes } 2377192934Srmacklem 2378192934Srmacklem /* 2379192934Srmacklem * For the experimental server: 2380192934Srmacklem * If this is the public directory, get the file handle 2381192934Srmacklem * and load it into the kernel via the nfssvc() syscall. 2382192934Srmacklem */ 2383192934Srmacklem if (run_v4server > 0 && (exflags & MNT_EXPUBLIC) != 0) { 2384192934Srmacklem fhandle_t fh; 2385192934Srmacklem char *public_name; 2386192934Srmacklem 2387192934Srmacklem if (eap->ex_indexfile != NULL) 2388192934Srmacklem public_name = eap->ex_indexfile; 2389192934Srmacklem else 2390192934Srmacklem public_name = dirp; 2391192934Srmacklem if (getfh(public_name, &fh) < 0) 2392192934Srmacklem syslog(LOG_ERR, 2393192934Srmacklem "Can't get public fh for %s", public_name); 2394192934Srmacklem else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0) 2395192934Srmacklem syslog(LOG_ERR, 2396192934Srmacklem "Can't set public fh for %s", public_name); 2397192934Srmacklem else 2398192934Srmacklem has_publicfh = 1; 2399192934Srmacklem } 240074462Salfredskip: 240175641Siedowse if (ai != NULL) 240274462Salfred ai = ai->ai_next; 240375641Siedowse if (ai == NULL) 24041558Srgrimes done = TRUE; 24051558Srgrimes } 24061558Srgrimes if (cp) 24071558Srgrimes *cp = savedc; 2408158857Srodrigcerror_exit: 2409158857Srodrigc /* free strings allocated by strdup() in getmntopts.c */ 2410158857Srodrigc if (iov != NULL) { 2411158857Srodrigc free(iov[0].iov_base); /* fstype */ 2412158857Srodrigc free(iov[2].iov_base); /* fspath */ 2413158857Srodrigc free(iov[4].iov_base); /* from */ 2414158857Srodrigc free(iov[6].iov_base); /* update */ 2415158857Srodrigc free(iov[8].iov_base); /* export */ 2416158857Srodrigc free(iov[10].iov_base); /* errmsg */ 2417158857Srodrigc 2418158857Srodrigc /* free iov, allocated by realloc() */ 2419158857Srodrigc free(iov); 2420158857Srodrigc } 2421158857Srodrigc return (ret); 24221558Srgrimes} 24231558Srgrimes 24241558Srgrimes/* 24251558Srgrimes * Translate a net address. 242675801Siedowse * 242775801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address. 24281558Srgrimes */ 24291558Srgrimesint 24301558Srgrimesget_net(cp, net, maskflg) 24311558Srgrimes char *cp; 24321558Srgrimes struct netmsk *net; 24331558Srgrimes int maskflg; 24341558Srgrimes{ 243575861Siedowse struct netent *np = NULL; 243674462Salfred char *name, *p, *prefp; 243775801Siedowse struct sockaddr_in sin; 243875861Siedowse struct sockaddr *sa = NULL; 243974462Salfred struct addrinfo hints, *ai = NULL; 244074462Salfred char netname[NI_MAXHOST]; 244174462Salfred long preflen; 24421558Srgrimes 244375635Siedowse p = prefp = NULL; 244474462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 244574462Salfred p = strchr(cp, '/'); 244674462Salfred *p = '\0'; 244774462Salfred prefp = p + 1; 244874462Salfred } 244974462Salfred 245075861Siedowse /* 245175861Siedowse * Check for a numeric address first. We wish to avoid 245275861Siedowse * possible DNS lookups in getnetbyname(). 245375861Siedowse */ 245475861Siedowse if (isxdigit(*cp) || *cp == ':') { 245574462Salfred memset(&hints, 0, sizeof hints); 245675801Siedowse /* Ensure the mask and the network have the same family. */ 245775801Siedowse if (maskflg && (opt_flags & OP_NET)) 245875801Siedowse hints.ai_family = net->nt_net.ss_family; 245975801Siedowse else if (!maskflg && (opt_flags & OP_HAVEMASK)) 246075801Siedowse hints.ai_family = net->nt_mask.ss_family; 246175801Siedowse else 246275801Siedowse hints.ai_family = AF_UNSPEC; 246374462Salfred hints.ai_flags = AI_NUMERICHOST; 246475861Siedowse if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 246575861Siedowse sa = ai->ai_addr; 246675861Siedowse if (sa != NULL && ai->ai_family == AF_INET) { 246774462Salfred /* 246875801Siedowse * The address in `cp' is really a network address, so 246975801Siedowse * use inet_network() to re-interpret this correctly. 247075801Siedowse * e.g. "127.1" means 127.1.0.0, not 127.0.0.1. 247174462Salfred */ 247275801Siedowse bzero(&sin, sizeof sin); 247374462Salfred sin.sin_family = AF_INET; 247474462Salfred sin.sin_len = sizeof sin; 247575801Siedowse sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 247674462Salfred if (debug) 247775801Siedowse fprintf(stderr, "get_net: v4 addr %s\n", 247875801Siedowse inet_ntoa(sin.sin_addr)); 247974462Salfred sa = (struct sockaddr *)&sin; 248075861Siedowse } 248175861Siedowse } 248275861Siedowse if (sa == NULL && (np = getnetbyname(cp)) != NULL) { 248375861Siedowse bzero(&sin, sizeof sin); 248475861Siedowse sin.sin_family = AF_INET; 248575861Siedowse sin.sin_len = sizeof sin; 248675861Siedowse sin.sin_addr = inet_makeaddr(np->n_net, 0); 248775861Siedowse sa = (struct sockaddr *)&sin; 248875861Siedowse } 248975861Siedowse if (sa == NULL) 249074462Salfred goto fail; 249125318Spst 249275801Siedowse if (maskflg) { 249375801Siedowse /* The specified sockaddr is a mask. */ 249475801Siedowse if (checkmask(sa) != 0) 249575801Siedowse goto fail; 249675801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 249775801Siedowse opt_flags |= OP_HAVEMASK; 249875801Siedowse } else { 249975801Siedowse /* The specified sockaddr is a network address. */ 250075801Siedowse bcopy(sa, &net->nt_net, sa->sa_len); 250174462Salfred 250275801Siedowse /* Get a network name for the export list. */ 250375801Siedowse if (np) { 250475801Siedowse name = np->n_name; 250575801Siedowse } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 2506146187Sume NULL, 0, NI_NUMERICHOST) == 0) { 250775801Siedowse name = netname; 250875801Siedowse } else { 250975801Siedowse goto fail; 251075801Siedowse } 251175801Siedowse if ((net->nt_name = strdup(name)) == NULL) 251275801Siedowse out_of_mem(); 251375801Siedowse 251475801Siedowse /* 251575801Siedowse * Extract a mask from either a "/<masklen>" suffix, or 251675801Siedowse * from the class of an IPv4 address. 251775801Siedowse */ 251874462Salfred if (opt_flags & OP_MASKLEN) { 251974462Salfred preflen = strtol(prefp, NULL, 10); 252075801Siedowse if (preflen < 0L || preflen == LONG_MAX) 252174462Salfred goto fail; 252275801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 252375801Siedowse if (makemask(&net->nt_mask, (int)preflen) != 0) 252475801Siedowse goto fail; 252575801Siedowse opt_flags |= OP_HAVEMASK; 252674462Salfred *p = '/'; 252775801Siedowse } else if (sa->sa_family == AF_INET && 252875801Siedowse (opt_flags & OP_MASK) == 0) { 252975801Siedowse in_addr_t addr; 253074462Salfred 253175801Siedowse addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 253275801Siedowse if (IN_CLASSA(addr)) 253375801Siedowse preflen = 8; 253475801Siedowse else if (IN_CLASSB(addr)) 253575801Siedowse preflen = 16; 253675801Siedowse else if (IN_CLASSC(addr)) 253775801Siedowse preflen = 24; 253875801Siedowse else if (IN_CLASSD(addr)) 253975801Siedowse preflen = 28; 254075801Siedowse else 254175801Siedowse preflen = 32; /* XXX */ 254275801Siedowse 254375801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 254475801Siedowse makemask(&net->nt_mask, (int)preflen); 254575801Siedowse opt_flags |= OP_HAVEMASK; 254674462Salfred } 254774462Salfred } 254874462Salfred 254974462Salfred if (ai) 255074462Salfred freeaddrinfo(ai); 255174462Salfred return 0; 255274462Salfred 255374462Salfredfail: 255474462Salfred if (ai) 255574462Salfred freeaddrinfo(ai); 255674462Salfred return 1; 25571558Srgrimes} 25581558Srgrimes 25591558Srgrimes/* 25601558Srgrimes * Parse out the next white space separated field 25611558Srgrimes */ 25621558Srgrimesvoid 25631558Srgrimesnextfield(cp, endcp) 25641558Srgrimes char **cp; 25651558Srgrimes char **endcp; 25661558Srgrimes{ 25671558Srgrimes char *p; 25681558Srgrimes 25691558Srgrimes p = *cp; 25701558Srgrimes while (*p == ' ' || *p == '\t') 25711558Srgrimes p++; 25721558Srgrimes if (*p == '\n' || *p == '\0') 25731558Srgrimes *cp = *endcp = p; 25741558Srgrimes else { 25751558Srgrimes *cp = p++; 25761558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 25771558Srgrimes p++; 25781558Srgrimes *endcp = p; 25791558Srgrimes } 25801558Srgrimes} 25811558Srgrimes 25821558Srgrimes/* 25831558Srgrimes * Get an exports file line. Skip over blank lines and handle line 25841558Srgrimes * continuations. 25851558Srgrimes */ 25861558Srgrimesint 25871558Srgrimesget_line() 25881558Srgrimes{ 25891558Srgrimes char *p, *cp; 259096622Siedowse size_t len; 25911558Srgrimes int totlen, cont_line; 25921558Srgrimes 25931558Srgrimes /* 25941558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 25951558Srgrimes */ 25961558Srgrimes p = line; 25971558Srgrimes totlen = 0; 25981558Srgrimes do { 259996622Siedowse if ((p = fgetln(exp_file, &len)) == NULL) 26001558Srgrimes return (0); 26011558Srgrimes cp = p + len - 1; 26021558Srgrimes cont_line = 0; 26031558Srgrimes while (cp >= p && 26041558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 26051558Srgrimes if (*cp == '\\') 26061558Srgrimes cont_line = 1; 26071558Srgrimes cp--; 26081558Srgrimes len--; 26091558Srgrimes } 261079117Sdd if (cont_line) { 261179117Sdd *++cp = ' '; 261279117Sdd len++; 261379117Sdd } 261496622Siedowse if (linesize < len + totlen + 1) { 261596622Siedowse linesize = len + totlen + 1; 261696622Siedowse line = realloc(line, linesize); 261796622Siedowse if (line == NULL) 261896622Siedowse out_of_mem(); 26191558Srgrimes } 262096622Siedowse memcpy(line + totlen, p, len); 262196622Siedowse totlen += len; 262296622Siedowse line[totlen] = '\0'; 26231558Srgrimes } while (totlen == 0 || cont_line); 26241558Srgrimes return (1); 26251558Srgrimes} 26261558Srgrimes 26271558Srgrimes/* 26281558Srgrimes * Parse a description of a credential. 26291558Srgrimes */ 26301558Srgrimesvoid 26311558Srgrimesparsecred(namelist, cr) 26321558Srgrimes char *namelist; 263372650Sgreen struct xucred *cr; 26341558Srgrimes{ 26351558Srgrimes char *name; 26361558Srgrimes int cnt; 26371558Srgrimes char *names; 26381558Srgrimes struct passwd *pw; 26391558Srgrimes struct group *gr; 2640136051Sstefanf gid_t groups[NGROUPS + 1]; 2641136051Sstefanf int ngroups; 26421558Srgrimes 264391354Sdd cr->cr_version = XUCRED_VERSION; 26441558Srgrimes /* 264537663Scharnier * Set up the unprivileged user. 26461558Srgrimes */ 26471558Srgrimes cr->cr_uid = -2; 26481558Srgrimes cr->cr_groups[0] = -2; 26491558Srgrimes cr->cr_ngroups = 1; 26501558Srgrimes /* 26511558Srgrimes * Get the user's password table entry. 26521558Srgrimes */ 26531558Srgrimes names = strsep(&namelist, " \t\n"); 26541558Srgrimes name = strsep(&names, ":"); 26551558Srgrimes if (isdigit(*name) || *name == '-') 26561558Srgrimes pw = getpwuid(atoi(name)); 26571558Srgrimes else 26581558Srgrimes pw = getpwnam(name); 26591558Srgrimes /* 26601558Srgrimes * Credentials specified as those of a user. 26611558Srgrimes */ 26621558Srgrimes if (names == NULL) { 26631558Srgrimes if (pw == NULL) { 266437663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 26651558Srgrimes return; 26661558Srgrimes } 26671558Srgrimes cr->cr_uid = pw->pw_uid; 26681558Srgrimes ngroups = NGROUPS + 1; 26691558Srgrimes if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 267037663Scharnier syslog(LOG_ERR, "too many groups"); 26711558Srgrimes /* 2672136051Sstefanf * Compress out duplicate. 26731558Srgrimes */ 26741558Srgrimes cr->cr_ngroups = ngroups - 1; 26751558Srgrimes cr->cr_groups[0] = groups[0]; 26761558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 26771558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 26781558Srgrimes return; 26791558Srgrimes } 26801558Srgrimes /* 26811558Srgrimes * Explicit credential specified as a colon separated list: 26821558Srgrimes * uid:gid:gid:... 26831558Srgrimes */ 26841558Srgrimes if (pw != NULL) 26851558Srgrimes cr->cr_uid = pw->pw_uid; 26861558Srgrimes else if (isdigit(*name) || *name == '-') 26871558Srgrimes cr->cr_uid = atoi(name); 26881558Srgrimes else { 268937663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 26901558Srgrimes return; 26911558Srgrimes } 26921558Srgrimes cr->cr_ngroups = 0; 26931558Srgrimes while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) { 26941558Srgrimes name = strsep(&names, ":"); 26951558Srgrimes if (isdigit(*name) || *name == '-') { 26961558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 26971558Srgrimes } else { 26981558Srgrimes if ((gr = getgrnam(name)) == NULL) { 269937663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 27001558Srgrimes continue; 27011558Srgrimes } 27021558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 27031558Srgrimes } 27041558Srgrimes } 27051558Srgrimes if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS) 270637663Scharnier syslog(LOG_ERR, "too many groups"); 27071558Srgrimes} 27081558Srgrimes 27091558Srgrimes#define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50) 27101558Srgrimes/* 27111558Srgrimes * Routines that maintain the remote mounttab 27121558Srgrimes */ 27131558Srgrimesvoid 27141558Srgrimesget_mountlist() 27151558Srgrimes{ 27161558Srgrimes struct mountlist *mlp, **mlpp; 271723681Speter char *host, *dirp, *cp; 27181558Srgrimes char str[STRSIZ]; 27191558Srgrimes FILE *mlfile; 27201558Srgrimes 27211558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 272253117Sbillf if (errno == ENOENT) 272353117Sbillf return; 272453117Sbillf else { 272553117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 272653117Sbillf return; 272753117Sbillf } 27281558Srgrimes } 27291558Srgrimes mlpp = &mlhead; 27301558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 273123681Speter cp = str; 273223681Speter host = strsep(&cp, " \t\n"); 273323681Speter dirp = strsep(&cp, " \t\n"); 273423681Speter if (host == NULL || dirp == NULL) 27351558Srgrimes continue; 27361558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 273737663Scharnier if (mlp == (struct mountlist *)NULL) 273837663Scharnier out_of_mem(); 273923681Speter strncpy(mlp->ml_host, host, RPCMNT_NAMELEN); 274023681Speter mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 274123681Speter strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 274223681Speter mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 27431558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 27441558Srgrimes *mlpp = mlp; 27451558Srgrimes mlpp = &mlp->ml_next; 27461558Srgrimes } 27471558Srgrimes fclose(mlfile); 27481558Srgrimes} 27491558Srgrimes 275075635Siedowsevoid 275175635Siedowsedel_mlist(char *hostp, char *dirp) 27521558Srgrimes{ 27531558Srgrimes struct mountlist *mlp, **mlpp; 27541558Srgrimes struct mountlist *mlp2; 27551558Srgrimes FILE *mlfile; 27561558Srgrimes int fnd = 0; 27571558Srgrimes 27581558Srgrimes mlpp = &mlhead; 27591558Srgrimes mlp = mlhead; 27601558Srgrimes while (mlp) { 27611558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 27621558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 27631558Srgrimes fnd = 1; 27641558Srgrimes mlp2 = mlp; 27651558Srgrimes *mlpp = mlp = mlp->ml_next; 27661558Srgrimes free((caddr_t)mlp2); 27671558Srgrimes } else { 27681558Srgrimes mlpp = &mlp->ml_next; 27691558Srgrimes mlp = mlp->ml_next; 27701558Srgrimes } 27711558Srgrimes } 27721558Srgrimes if (fnd) { 27731558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 277437663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 27751558Srgrimes return; 27761558Srgrimes } 27771558Srgrimes mlp = mlhead; 27781558Srgrimes while (mlp) { 27791558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 27801558Srgrimes mlp = mlp->ml_next; 27811558Srgrimes } 27821558Srgrimes fclose(mlfile); 27831558Srgrimes } 27841558Srgrimes} 27851558Srgrimes 27861558Srgrimesvoid 27871558Srgrimesadd_mlist(hostp, dirp) 27881558Srgrimes char *hostp, *dirp; 27891558Srgrimes{ 27901558Srgrimes struct mountlist *mlp, **mlpp; 27911558Srgrimes FILE *mlfile; 27921558Srgrimes 27931558Srgrimes mlpp = &mlhead; 27941558Srgrimes mlp = mlhead; 27951558Srgrimes while (mlp) { 27961558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 27971558Srgrimes return; 27981558Srgrimes mlpp = &mlp->ml_next; 27991558Srgrimes mlp = mlp->ml_next; 28001558Srgrimes } 28011558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 280237663Scharnier if (mlp == (struct mountlist *)NULL) 280337663Scharnier out_of_mem(); 28041558Srgrimes strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN); 28051558Srgrimes mlp->ml_host[RPCMNT_NAMELEN] = '\0'; 28061558Srgrimes strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN); 28071558Srgrimes mlp->ml_dirp[RPCMNT_PATHLEN] = '\0'; 28081558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 28091558Srgrimes *mlpp = mlp; 28101558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 281137663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 28121558Srgrimes return; 28131558Srgrimes } 28141558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 28151558Srgrimes fclose(mlfile); 28161558Srgrimes} 28171558Srgrimes 28181558Srgrimes/* 28191558Srgrimes * Free up a group list. 28201558Srgrimes */ 28211558Srgrimesvoid 28221558Srgrimesfree_grp(grp) 28231558Srgrimes struct grouplist *grp; 28241558Srgrimes{ 28251558Srgrimes if (grp->gr_type == GT_HOST) { 282674462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 282774462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 28281558Srgrimes } else if (grp->gr_type == GT_NET) { 28291558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 28301558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 28311558Srgrimes } 28321558Srgrimes free((caddr_t)grp); 28331558Srgrimes} 28341558Srgrimes 28351558Srgrimes#ifdef DEBUG 28361558Srgrimesvoid 28371558SrgrimesSYSLOG(int pri, const char *fmt, ...) 28381558Srgrimes{ 28391558Srgrimes va_list ap; 28401558Srgrimes 28411558Srgrimes va_start(ap, fmt); 28421558Srgrimes vfprintf(stderr, fmt, ap); 28431558Srgrimes va_end(ap); 28441558Srgrimes} 28451558Srgrimes#endif /* DEBUG */ 28461558Srgrimes 28471558Srgrimes/* 28481558Srgrimes * Check options for consistency. 28491558Srgrimes */ 28501558Srgrimesint 28511558Srgrimescheck_options(dp) 28521558Srgrimes struct dirlist *dp; 28531558Srgrimes{ 28541558Srgrimes 2855192934Srmacklem if (v4root_phase == 0 && dp == NULL) 28561558Srgrimes return (1); 285783653Speter if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) { 285883653Speter syslog(LOG_ERR, "-mapall and -maproot mutually exclusive"); 28591558Srgrimes return (1); 28601558Srgrimes } 28611558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 286275801Siedowse syslog(LOG_ERR, "-mask requires -network"); 286375801Siedowse return (1); 28641558Srgrimes } 286575801Siedowse if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) { 286675801Siedowse syslog(LOG_ERR, "-network requires mask specification"); 286775801Siedowse return (1); 286875801Siedowse } 286975801Siedowse if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) { 287075801Siedowse syslog(LOG_ERR, "-mask and /masklen are mutually exclusive"); 287175801Siedowse return (1); 287275801Siedowse } 28731558Srgrimes if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 287445927Salex syslog(LOG_ERR, "-alldirs has multiple directories"); 28751558Srgrimes return (1); 28761558Srgrimes } 2877192934Srmacklem if (v4root_phase > 0 && 2878192934Srmacklem (opt_flags & 2879192934Srmacklem ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) { 2880192934Srmacklem syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:"); 2881192934Srmacklem return (1); 2882192934Srmacklem } 28831558Srgrimes return (0); 28841558Srgrimes} 28851558Srgrimes 28861558Srgrimes/* 28871558Srgrimes * Check an absolute directory path for any symbolic links. Return true 28881558Srgrimes */ 28891558Srgrimesint 28901558Srgrimescheck_dirpath(dirp) 28911558Srgrimes char *dirp; 28921558Srgrimes{ 28931558Srgrimes char *cp; 28941558Srgrimes int ret = 1; 28951558Srgrimes struct stat sb; 28961558Srgrimes 28971558Srgrimes cp = dirp + 1; 28981558Srgrimes while (*cp && ret) { 28991558Srgrimes if (*cp == '/') { 29001558Srgrimes *cp = '\0'; 29019336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 29021558Srgrimes ret = 0; 29031558Srgrimes *cp = '/'; 29041558Srgrimes } 29051558Srgrimes cp++; 29061558Srgrimes } 29079336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 29081558Srgrimes ret = 0; 29091558Srgrimes return (ret); 29101558Srgrimes} 29119336Sdfr 291275801Siedowse/* 291375801Siedowse * Make a netmask according to the specified prefix length. The ss_family 291475801Siedowse * and other non-address fields must be initialised before calling this. 291575801Siedowse */ 291675801Siedowseint 291775801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen) 291874462Salfred{ 291975801Siedowse u_char *p; 292075801Siedowse int bits, i, len; 292174462Salfred 292275801Siedowse if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL) 292375801Siedowse return (-1); 2924103949Smike if (bitlen > len * CHAR_BIT) 292575801Siedowse return (-1); 292674462Salfred 292775801Siedowse for (i = 0; i < len; i++) { 2928103949Smike bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen; 292975801Siedowse *p++ = (1 << bits) - 1; 293075801Siedowse bitlen -= bits; 293174462Salfred } 293275801Siedowse return 0; 293374462Salfred} 293474462Salfred 293575801Siedowse/* 293675801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask 293775801Siedowse * is acceptable (i.e. of the form 1...10....0). 293875801Siedowse */ 293975801Siedowseint 294075801Siedowsecheckmask(struct sockaddr *sa) 294174462Salfred{ 294275801Siedowse u_char *mask; 294375801Siedowse int i, len; 294474462Salfred 294575801Siedowse if ((mask = sa_rawaddr(sa, &len)) == NULL) 294675801Siedowse return (-1); 294775801Siedowse 294875801Siedowse for (i = 0; i < len; i++) 294975801Siedowse if (mask[i] != 0xff) 295075801Siedowse break; 295175801Siedowse if (i < len) { 295275801Siedowse if (~mask[i] & (u_char)(~mask[i] + 1)) 295375801Siedowse return (-1); 295475801Siedowse i++; 295574462Salfred } 295675801Siedowse for (; i < len; i++) 295775801Siedowse if (mask[i] != 0) 295875801Siedowse return (-1); 295975801Siedowse return (0); 296074462Salfred} 296174462Salfred 296275801Siedowse/* 296375801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if 296475801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'. 296575801Siedowse * If samask is NULL, perform a full comparision. 296675801Siedowse */ 296775801Siedowseint 296875801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask) 296974462Salfred{ 297075801Siedowse unsigned char *p1, *p2, *mask; 297175801Siedowse int len, i; 297274462Salfred 297375801Siedowse if (sa1->sa_family != sa2->sa_family || 297475801Siedowse (p1 = sa_rawaddr(sa1, &len)) == NULL || 297575801Siedowse (p2 = sa_rawaddr(sa2, NULL)) == NULL) 297675801Siedowse return (1); 297775801Siedowse 297875801Siedowse switch (sa1->sa_family) { 297974462Salfred case AF_INET6: 298075801Siedowse if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 298175801Siedowse ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 298275801Siedowse return (1); 298374462Salfred break; 298474462Salfred } 298574462Salfred 298675801Siedowse /* Simple binary comparison if no mask specified. */ 298775801Siedowse if (samask == NULL) 298875801Siedowse return (memcmp(p1, p2, len)); 298974462Salfred 299075801Siedowse /* Set up the mask, and do a mask-based comparison. */ 299175801Siedowse if (sa1->sa_family != samask->sa_family || 299275801Siedowse (mask = sa_rawaddr(samask, NULL)) == NULL) 299375801Siedowse return (1); 299474462Salfred 299575801Siedowse for (i = 0; i < len; i++) 299675801Siedowse if ((p1[i] & mask[i]) != (p2[i] & mask[i])) 299775801Siedowse return (1); 299875801Siedowse return (0); 299974462Salfred} 300074462Salfred 300175801Siedowse/* 300275801Siedowse * Return a pointer to the part of the sockaddr that contains the 300375801Siedowse * raw address, and set *nbytes to its length in bytes. Returns 300475801Siedowse * NULL if the address family is unknown. 300575801Siedowse */ 300675801Siedowsevoid * 300775801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) { 300875801Siedowse void *p; 300974462Salfred int len; 301074462Salfred 301175801Siedowse switch (sa->sa_family) { 301274462Salfred case AF_INET: 301375801Siedowse len = sizeof(((struct sockaddr_in *)sa)->sin_addr); 301475801Siedowse p = &((struct sockaddr_in *)sa)->sin_addr; 301574462Salfred break; 301674462Salfred case AF_INET6: 301775801Siedowse len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); 301875801Siedowse p = &((struct sockaddr_in6 *)sa)->sin6_addr; 301974462Salfred break; 302074462Salfred default: 302175801Siedowse p = NULL; 302275801Siedowse len = 0; 302374462Salfred } 302474462Salfred 302575801Siedowse if (nbytes != NULL) 302675801Siedowse *nbytes = len; 302775801Siedowse return (p); 302874462Salfred} 302974462Salfred 303075754Siedowsevoid 303175754Siedowsehuphandler(int sig) 303275754Siedowse{ 303375754Siedowse got_sighup = 1; 303475754Siedowse} 303575754Siedowse 303674462Salfredvoid terminate(sig) 303774462Salfredint sig; 303874462Salfred{ 3039149433Spjd pidfile_remove(pfh); 304074792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL); 304174792Salfred rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL); 304274462Salfred exit (0); 304374462Salfred} 3044192934Srmacklem 3045