mountd.c revision 240902
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 240902 2012-09-24 23:57:16Z 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> 629336Sdfr#include <nfs/nfsproto.h> 63192934Srmacklem#include <nfs/nfssvc.h> 6483653Speter#include <nfsserver/nfs.h> 651558Srgrimes 66192934Srmacklem#include <fs/nfs/nfsport.h> 67192934Srmacklem 681558Srgrimes#include <arpa/inet.h> 691558Srgrimes 701558Srgrimes#include <ctype.h> 7137663Scharnier#include <err.h> 721558Srgrimes#include <errno.h> 731558Srgrimes#include <grp.h> 74149433Spjd#include <libutil.h> 75103949Smike#include <limits.h> 761558Srgrimes#include <netdb.h> 771558Srgrimes#include <pwd.h> 781558Srgrimes#include <signal.h> 791558Srgrimes#include <stdio.h> 801558Srgrimes#include <stdlib.h> 811558Srgrimes#include <string.h> 821558Srgrimes#include <unistd.h> 831558Srgrimes#include "pathnames.h" 84158857Srodrigc#include "mntopts.h" 851558Srgrimes 861558Srgrimes#ifdef DEBUG 871558Srgrimes#include <stdarg.h> 881558Srgrimes#endif 891558Srgrimes 901558Srgrimes/* 911558Srgrimes * Structures for keeping the mount list and export list 921558Srgrimes */ 931558Srgrimesstruct mountlist { 941558Srgrimes struct mountlist *ml_next; 95194880Sdfr char ml_host[MNTNAMLEN+1]; 96194880Sdfr char ml_dirp[MNTPATHLEN+1]; 971558Srgrimes}; 981558Srgrimes 991558Srgrimesstruct dirlist { 1001558Srgrimes struct dirlist *dp_left; 1011558Srgrimes struct dirlist *dp_right; 1021558Srgrimes int dp_flag; 1031558Srgrimes struct hostlist *dp_hosts; /* List of hosts this dir exported to */ 1041558Srgrimes char dp_dirp[1]; /* Actually malloc'd to size of dir */ 1051558Srgrimes}; 1061558Srgrimes/* dp_flag bits */ 1071558Srgrimes#define DP_DEFSET 0x1 1089336Sdfr#define DP_HOSTSET 0x2 1091558Srgrimes 1101558Srgrimesstruct exportlist { 1111558Srgrimes struct exportlist *ex_next; 1121558Srgrimes struct dirlist *ex_dirl; 1131558Srgrimes struct dirlist *ex_defdir; 1141558Srgrimes int ex_flag; 1151558Srgrimes fsid_t ex_fs; 1161558Srgrimes char *ex_fsdir; 11727447Sdfr char *ex_indexfile; 118184588Sdfr int ex_numsecflavors; 119184588Sdfr int ex_secflavors[MAXSECFLAVORS]; 120240902Srmacklem int ex_defnumsecflavors; 121240902Srmacklem int ex_defsecflavors[MAXSECFLAVORS]; 1221558Srgrimes}; 1231558Srgrimes/* ex_flag bits */ 1241558Srgrimes#define EX_LINKED 0x1 1251558Srgrimes 1261558Srgrimesstruct netmsk { 12774462Salfred struct sockaddr_storage nt_net; 12875801Siedowse struct sockaddr_storage nt_mask; 12942144Sdfr char *nt_name; 1301558Srgrimes}; 1311558Srgrimes 1321558Srgrimesunion grouptypes { 13374462Salfred struct addrinfo *gt_addrinfo; 1341558Srgrimes struct netmsk gt_net; 1351558Srgrimes}; 1361558Srgrimes 1371558Srgrimesstruct grouplist { 1381558Srgrimes int gr_type; 1391558Srgrimes union grouptypes gr_ptr; 1401558Srgrimes struct grouplist *gr_next; 141240902Srmacklem int gr_numsecflavors; 142240902Srmacklem int gr_secflavors[MAXSECFLAVORS]; 1431558Srgrimes}; 1441558Srgrimes/* Group types */ 1451558Srgrimes#define GT_NULL 0x0 1461558Srgrimes#define GT_HOST 0x1 1471558Srgrimes#define GT_NET 0x2 14875641Siedowse#define GT_DEFAULT 0x3 1497401Swpaul#define GT_IGNORE 0x5 1501558Srgrimes 1511558Srgrimesstruct hostlist { 1529336Sdfr int ht_flag; /* Uses DP_xx bits */ 1531558Srgrimes struct grouplist *ht_grp; 1541558Srgrimes struct hostlist *ht_next; 1551558Srgrimes}; 1561558Srgrimes 1579336Sdfrstruct fhreturn { 1589336Sdfr int fhr_flag; 1599336Sdfr int fhr_vers; 1609336Sdfr nfsfh_t fhr_fh; 161184588Sdfr int fhr_numsecflavors; 162184588Sdfr int *fhr_secflavors; 1639336Sdfr}; 1649336Sdfr 165222623Srmacklem#define GETPORT_MAXTRY 20 /* Max tries to get a port # */ 166222623Srmacklem 1671558Srgrimes/* Global defs */ 16892882Simpchar *add_expdir(struct dirlist **, char *, int); 16992882Simpvoid add_dlist(struct dirlist **, struct dirlist *, 170240902Srmacklem struct grouplist *, int, struct exportlist *); 17192882Simpvoid add_mlist(char *, char *); 17292882Simpint check_dirpath(char *); 17392882Simpint check_options(struct dirlist *); 17475801Siedowseint checkmask(struct sockaddr *sa); 175240902Srmacklemint chk_host(struct dirlist *, struct sockaddr *, int *, int *, int *, 176240902Srmacklem int **); 177222623Srmacklemstatic int create_service(struct netconfig *nconf); 178222623Srmacklemstatic void complete_service(struct netconfig *nconf, char *port_str); 179222623Srmacklemstatic void clearout_service(void); 18075635Siedowsevoid del_mlist(char *hostp, char *dirp); 18192882Simpstruct dirlist *dirp_search(struct dirlist *, char *); 18292882Simpint do_mount(struct exportlist *, struct grouplist *, int, 18392882Simp struct xucred *, char *, int, struct statfs *); 18492882Simpint do_opt(char **, char **, struct exportlist *, struct grouplist *, 18592882Simp int *, int *, struct xucred *); 18692882Simpstruct exportlist *ex_search(fsid_t *); 18792882Simpstruct exportlist *get_exp(void); 18892882Simpvoid free_dir(struct dirlist *); 18992882Simpvoid free_exp(struct exportlist *); 19092882Simpvoid free_grp(struct grouplist *); 19192882Simpvoid free_host(struct hostlist *); 19292882Simpvoid get_exportlist(void); 19392882Simpint get_host(char *, struct grouplist *, struct grouplist *); 19492882Simpstruct hostlist *get_ht(void); 19592882Simpint get_line(void); 19692882Simpvoid get_mountlist(void); 19792882Simpint get_net(char *, struct netmsk *, int); 19892882Simpvoid getexp_err(struct exportlist *, struct grouplist *); 19992882Simpstruct grouplist *get_grp(void); 20092882Simpvoid hang_dirp(struct dirlist *, struct grouplist *, 20192882Simp struct exportlist *, int); 20275754Siedowsevoid huphandler(int sig); 20375801Siedowseint makemask(struct sockaddr_storage *ssp, int bitlen); 20492882Simpvoid mntsrv(struct svc_req *, SVCXPRT *); 20592882Simpvoid nextfield(char **, char **); 20692882Simpvoid out_of_mem(void); 20792882Simpvoid parsecred(char *, struct xucred *); 208216587Scharnierint parsesec(char *, struct exportlist *); 209100117Salfredint put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int); 21075801Siedowsevoid *sa_rawaddr(struct sockaddr *sa, int *nbytes); 21175801Siedowseint sacmp(struct sockaddr *sa1, struct sockaddr *sa2, 21275801Siedowse struct sockaddr *samask); 21392882Simpint scan_tree(struct dirlist *, struct sockaddr *); 21492882Simpstatic void usage(void); 21592882Simpint xdr_dir(XDR *, char *); 21692882Simpint xdr_explist(XDR *, caddr_t); 217100117Salfredint xdr_explist_brief(XDR *, caddr_t); 218216587Scharnierint xdr_explist_common(XDR *, caddr_t, int); 21992882Simpint xdr_fhs(XDR *, caddr_t); 22092882Simpint xdr_mlist(XDR *, caddr_t); 22192882Simpvoid terminate(int); 2221558Srgrimes 2231558Srgrimesstruct exportlist *exphead; 2241558Srgrimesstruct mountlist *mlhead; 2251558Srgrimesstruct grouplist *grphead; 226166440Spjdchar *exnames_default[2] = { _PATH_EXPORTS, NULL }; 227166440Spjdchar **exnames; 228172827Smatteochar **hosts = NULL; 22972650Sgreenstruct xucred def_anon = { 23091354Sdd XUCRED_VERSION, 23172650Sgreen (uid_t)-2, 2321558Srgrimes 1, 23372650Sgreen { (gid_t)-2 }, 23472650Sgreen NULL 2351558Srgrimes}; 23625087Sdfrint force_v2 = 0; 2379336Sdfrint resvport_only = 1; 238172827Smatteoint nhosts = 0; 2399336Sdfrint dir_only = 1; 240121767Speterint dolog = 0; 24175754Siedowseint got_sighup = 0; 242172827Smatteoint xcreated = 0; 24374462Salfred 244172827Smatteochar *svcport_str = NULL; 245222623Srmacklemstatic int mallocd_svcport = 0; 246222623Srmacklemstatic int *sock_fd; 247222623Srmacklemstatic int sock_fdcnt; 248222623Srmacklemstatic int sock_fdpos; 249172827Smatteo 2501558Srgrimesint opt_flags; 25174462Salfredstatic int have_v6 = 1; 25274462Salfred 253192934Srmacklemint v4root_phase = 0; 254192934Srmacklemchar v4root_dirpath[PATH_MAX + 1]; 255220980Srmacklemint run_v4server = 1; 256192934Srmacklemint has_publicfh = 0; 257192934Srmacklem 258149433Spjdstruct pidfh *pfh = NULL; 25975801Siedowse/* Bits for opt_flags above */ 2601558Srgrimes#define OP_MAPROOT 0x01 2611558Srgrimes#define OP_MAPALL 0x02 26283653Speter/* 0x4 free */ 2631558Srgrimes#define OP_MASK 0x08 2641558Srgrimes#define OP_NET 0x10 2651558Srgrimes#define OP_ALLDIRS 0x40 26675801Siedowse#define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */ 267100336Sjoerg#define OP_QUIET 0x100 26874462Salfred#define OP_MASKLEN 0x200 269184588Sdfr#define OP_SEC 0x400 2701558Srgrimes 2711558Srgrimes#ifdef DEBUG 2721558Srgrimesint debug = 1; 27392882Simpvoid SYSLOG(int, const char *, ...) __printflike(2, 3); 2741558Srgrimes#define syslog SYSLOG 2751558Srgrimes#else 2761558Srgrimesint debug = 0; 2771558Srgrimes#endif 2781558Srgrimes 2791558Srgrimes/* 2801558Srgrimes * Mountd server for NFS mount protocol as described in: 2811558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A 2821558Srgrimes * The optional arguments are the exports file name 2831558Srgrimes * default: _PATH_EXPORTS 2841558Srgrimes * and "-n" to allow nonroot mount. 2851558Srgrimes */ 2861558Srgrimesint 287216587Scharniermain(int argc, char **argv) 2881558Srgrimes{ 28975754Siedowse fd_set readfds; 290172827Smatteo struct netconfig *nconf; 291172827Smatteo char *endptr, **hosts_bak; 292172827Smatteo void *nc_handle; 293149433Spjd pid_t otherpid; 294172827Smatteo in_port_t svcport; 295172827Smatteo int c, k, s; 296109363Smbr int maxrec = RPC_MAXDATASIZE; 297222623Srmacklem int attempt_cnt, port_len, port_pos, ret; 298222623Srmacklem char **port_list; 2991558Srgrimes 30074462Salfred /* Check that another mountd isn't already running. */ 301150214Spjd pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid); 302149433Spjd if (pfh == NULL) { 303149433Spjd if (errno == EEXIST) 304149433Spjd errx(1, "mountd already running, pid: %d.", otherpid); 305149433Spjd warn("cannot open or create pidfile"); 306149433Spjd } 30774462Salfred 30874462Salfred s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 30974462Salfred if (s < 0) 31074462Salfred have_v6 = 0; 31174462Salfred else 31274462Salfred close(s); 3132999Swollman 314220980Srmacklem while ((c = getopt(argc, argv, "2deh:lnop:r")) != -1) 3151558Srgrimes switch (c) { 31625087Sdfr case '2': 31725087Sdfr force_v2 = 1; 31825087Sdfr break; 319192993Srmacklem case 'e': 320220980Srmacklem /* now a no-op, since this is the default */ 321192934Srmacklem break; 3229336Sdfr case 'n': 3239336Sdfr resvport_only = 0; 3249336Sdfr break; 3259336Sdfr case 'r': 3269336Sdfr dir_only = 0; 3279336Sdfr break; 3288688Sphk case 'd': 3298688Sphk debug = debug ? 0 : 1; 3308688Sphk break; 33131656Sguido case 'l': 332121767Speter dolog = 1; 33331656Sguido break; 334220980Srmacklem case 'o': 335220980Srmacklem run_v4server = 0; 336220980Srmacklem break; 337126572Sbms case 'p': 338126572Sbms endptr = NULL; 339126572Sbms svcport = (in_port_t)strtoul(optarg, &endptr, 10); 340126572Sbms if (endptr == NULL || *endptr != '\0' || 341126572Sbms svcport == 0 || svcport >= IPPORT_MAX) 342126572Sbms usage(); 343172827Smatteo svcport_str = strdup(optarg); 344126572Sbms break; 345172827Smatteo case 'h': 346172827Smatteo ++nhosts; 347172827Smatteo hosts_bak = hosts; 348172827Smatteo hosts_bak = realloc(hosts, nhosts * sizeof(char *)); 349172827Smatteo if (hosts_bak == NULL) { 350172827Smatteo if (hosts != NULL) { 351172827Smatteo for (k = 0; k < nhosts; k++) 352172827Smatteo free(hosts[k]); 353172827Smatteo free(hosts); 354172827Smatteo out_of_mem(); 355172827Smatteo } 356172827Smatteo } 357172827Smatteo hosts = hosts_bak; 358172827Smatteo hosts[nhosts - 1] = strdup(optarg); 359172827Smatteo if (hosts[nhosts - 1] == NULL) { 360172827Smatteo for (k = 0; k < (nhosts - 1); k++) 361172827Smatteo free(hosts[k]); 362172827Smatteo free(hosts); 363172827Smatteo out_of_mem(); 364172827Smatteo } 365172827Smatteo break; 3661558Srgrimes default: 36737663Scharnier usage(); 3681558Srgrimes }; 369192934Srmacklem 370192934Srmacklem /* 371220980Srmacklem * Unless the "-o" option was specified, try and run "nfsd". 372220980Srmacklem * If "-o" was specified, try and run "nfsserver". 373192934Srmacklem */ 374192934Srmacklem if (run_v4server > 0) { 375192934Srmacklem if (modfind("nfsd") < 0) { 376192934Srmacklem /* Not present in kernel, try loading it */ 377192934Srmacklem if (kldload("nfsd") < 0 || modfind("nfsd") < 0) 378192934Srmacklem errx(1, "NFS server is not available"); 379192934Srmacklem } 380192934Srmacklem } else if (modfind("nfsserver") < 0) { 381192934Srmacklem /* Not present in kernel, try loading it */ 382192934Srmacklem if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0) 383192934Srmacklem errx(1, "NFS server is not available"); 384192934Srmacklem } 385192934Srmacklem 3861558Srgrimes argc -= optind; 3871558Srgrimes argv += optind; 3881558Srgrimes grphead = (struct grouplist *)NULL; 3891558Srgrimes exphead = (struct exportlist *)NULL; 3901558Srgrimes mlhead = (struct mountlist *)NULL; 391166440Spjd if (argc > 0) 392166440Spjd exnames = argv; 393166440Spjd else 394166440Spjd exnames = exnames_default; 3951558Srgrimes openlog("mountd", LOG_PID, LOG_DAEMON); 3961558Srgrimes if (debug) 39737663Scharnier warnx("getting export list"); 3981558Srgrimes get_exportlist(); 3991558Srgrimes if (debug) 40037663Scharnier warnx("getting mount list"); 4011558Srgrimes get_mountlist(); 4021558Srgrimes if (debug) 40337663Scharnier warnx("here we go"); 4041558Srgrimes if (debug == 0) { 4051558Srgrimes daemon(0, 0); 4061558Srgrimes signal(SIGINT, SIG_IGN); 4071558Srgrimes signal(SIGQUIT, SIG_IGN); 4081558Srgrimes } 40975754Siedowse signal(SIGHUP, huphandler); 41074462Salfred signal(SIGTERM, terminate); 411164394Srodrigc signal(SIGPIPE, SIG_IGN); 412149433Spjd 413149433Spjd pidfile_write(pfh); 414149433Spjd 415194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS, NULL); 416194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL); 417109363Smbr rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); 418109363Smbr 41924759Sguido if (!resvport_only) { 42083687Speter if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL, 42183687Speter &resvport_only, sizeof(resvport_only)) != 0 && 42283687Speter errno != ENOENT) { 42324759Sguido syslog(LOG_ERR, "sysctl: %m"); 42424759Sguido exit(1); 42524759Sguido } 42624330Sguido } 427126572Sbms 428172827Smatteo /* 429172827Smatteo * If no hosts were specified, add a wildcard entry to bind to 430172827Smatteo * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the 431172827Smatteo * list. 432172827Smatteo */ 433172827Smatteo if (nhosts == 0) { 434172827Smatteo hosts = malloc(sizeof(char**)); 435172827Smatteo if (hosts == NULL) 436172827Smatteo out_of_mem(); 437172827Smatteo hosts[0] = "*"; 438172827Smatteo nhosts = 1; 439172827Smatteo } else { 440172827Smatteo hosts_bak = hosts; 441172827Smatteo if (have_v6) { 442172827Smatteo hosts_bak = realloc(hosts, (nhosts + 2) * 443172827Smatteo sizeof(char *)); 444172827Smatteo if (hosts_bak == NULL) { 445172827Smatteo for (k = 0; k < nhosts; k++) 446172827Smatteo free(hosts[k]); 447172827Smatteo free(hosts); 448172827Smatteo out_of_mem(); 449172827Smatteo } else 450172827Smatteo hosts = hosts_bak; 451172827Smatteo nhosts += 2; 452172827Smatteo hosts[nhosts - 2] = "::1"; 453172827Smatteo } else { 454172827Smatteo hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *)); 455172827Smatteo if (hosts_bak == NULL) { 456172827Smatteo for (k = 0; k < nhosts; k++) 457172827Smatteo free(hosts[k]); 458172827Smatteo free(hosts); 459172827Smatteo out_of_mem(); 460172827Smatteo } else { 461172827Smatteo nhosts += 1; 462172827Smatteo hosts = hosts_bak; 463126572Sbms } 464172827Smatteo } 46574462Salfred 466172827Smatteo hosts[nhosts - 1] = "127.0.0.1"; 46774462Salfred } 46874462Salfred 469222623Srmacklem attempt_cnt = 1; 470222623Srmacklem sock_fdcnt = 0; 471222623Srmacklem sock_fd = NULL; 472222623Srmacklem port_list = NULL; 473222623Srmacklem port_len = 0; 474172827Smatteo nc_handle = setnetconfig(); 475172827Smatteo while ((nconf = getnetconfig(nc_handle))) { 476172827Smatteo if (nconf->nc_flag & NC_VISIBLE) { 477172827Smatteo if (have_v6 == 0 && strcmp(nconf->nc_protofmly, 478172827Smatteo "inet6") == 0) { 479172827Smatteo /* DO NOTHING */ 480222623Srmacklem } else { 481222623Srmacklem ret = create_service(nconf); 482222623Srmacklem if (ret == 1) 483222623Srmacklem /* Ignore this call */ 484222623Srmacklem continue; 485222623Srmacklem if (ret < 0) { 486222623Srmacklem /* 487222623Srmacklem * Failed to bind port, so close off 488222623Srmacklem * all sockets created and try again 489222623Srmacklem * if the port# was dynamically 490222623Srmacklem * assigned via bind(2). 491222623Srmacklem */ 492222623Srmacklem clearout_service(); 493222623Srmacklem if (mallocd_svcport != 0 && 494222623Srmacklem attempt_cnt < GETPORT_MAXTRY) { 495222623Srmacklem free(svcport_str); 496222623Srmacklem svcport_str = NULL; 497222623Srmacklem mallocd_svcport = 0; 498222623Srmacklem } else { 499222623Srmacklem errno = EADDRINUSE; 500222623Srmacklem syslog(LOG_ERR, 501222623Srmacklem "bindresvport_sa: %m"); 502222623Srmacklem exit(1); 503222623Srmacklem } 504222623Srmacklem 505222623Srmacklem /* Start over at the first service. */ 506222623Srmacklem free(sock_fd); 507222623Srmacklem sock_fdcnt = 0; 508222623Srmacklem sock_fd = NULL; 509222623Srmacklem nc_handle = setnetconfig(); 510222623Srmacklem attempt_cnt++; 511222623Srmacklem } else if (mallocd_svcport != 0 && 512222623Srmacklem attempt_cnt == GETPORT_MAXTRY) { 513222623Srmacklem /* 514222623Srmacklem * For the last attempt, allow 515222623Srmacklem * different port #s for each nconf 516222623Srmacklem * by saving the svcport_str and 517222623Srmacklem * setting it back to NULL. 518222623Srmacklem */ 519222623Srmacklem port_list = realloc(port_list, 520222623Srmacklem (port_len + 1) * sizeof(char *)); 521222623Srmacklem if (port_list == NULL) 522222623Srmacklem out_of_mem(); 523222623Srmacklem port_list[port_len++] = svcport_str; 524222623Srmacklem svcport_str = NULL; 525222623Srmacklem mallocd_svcport = 0; 526222623Srmacklem } 527222623Srmacklem } 528222623Srmacklem } 529222623Srmacklem } 530222623Srmacklem 531222623Srmacklem /* 532222623Srmacklem * Successfully bound the ports, so call complete_service() to 533222623Srmacklem * do the rest of the setup on the service(s). 534222623Srmacklem */ 535222623Srmacklem sock_fdpos = 0; 536222623Srmacklem port_pos = 0; 537222623Srmacklem nc_handle = setnetconfig(); 538222623Srmacklem while ((nconf = getnetconfig(nc_handle))) { 539222623Srmacklem if (nconf->nc_flag & NC_VISIBLE) { 540222623Srmacklem if (have_v6 == 0 && strcmp(nconf->nc_protofmly, 541222623Srmacklem "inet6") == 0) { 542222623Srmacklem /* DO NOTHING */ 543222623Srmacklem } else if (port_list != NULL) { 544222623Srmacklem if (port_pos >= port_len) { 545222623Srmacklem syslog(LOG_ERR, "too many port#s"); 546222623Srmacklem exit(1); 547222623Srmacklem } 548222623Srmacklem complete_service(nconf, port_list[port_pos++]); 549172827Smatteo } else 550222623Srmacklem complete_service(nconf, svcport_str); 551172827Smatteo } 55274462Salfred } 553172827Smatteo endnetconfig(nc_handle); 554222623Srmacklem free(sock_fd); 555222623Srmacklem if (port_list != NULL) { 556222623Srmacklem for (port_pos = 0; port_pos < port_len; port_pos++) 557222623Srmacklem free(port_list[port_pos]); 558222623Srmacklem free(port_list); 559222623Srmacklem } 56074462Salfred 56174462Salfred if (xcreated == 0) { 56274462Salfred syslog(LOG_ERR, "could not create any services"); 5631558Srgrimes exit(1); 5641558Srgrimes } 56575754Siedowse 56675754Siedowse /* Expand svc_run() here so that we can call get_exportlist(). */ 56775754Siedowse for (;;) { 56875754Siedowse if (got_sighup) { 56975754Siedowse get_exportlist(); 57075754Siedowse got_sighup = 0; 57175754Siedowse } 57275754Siedowse readfds = svc_fdset; 57375754Siedowse switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) { 57475754Siedowse case -1: 57575754Siedowse if (errno == EINTR) 57675754Siedowse continue; 57775754Siedowse syslog(LOG_ERR, "mountd died: select: %m"); 57875754Siedowse exit(1); 57975754Siedowse case 0: 58075754Siedowse continue; 58175754Siedowse default: 58275754Siedowse svc_getreqset(&readfds); 58375754Siedowse } 58475754Siedowse } 585172827Smatteo} 586172827Smatteo 587172827Smatteo/* 588172827Smatteo * This routine creates and binds sockets on the appropriate 589222623Srmacklem * addresses. It gets called one time for each transport. 590222623Srmacklem * It returns 0 upon success, 1 for ingore the call and -1 to indicate 591222623Srmacklem * bind failed with EADDRINUSE. 592222623Srmacklem * Any file descriptors that have been created are stored in sock_fd and 593222623Srmacklem * the total count of them is maintained in sock_fdcnt. 594172827Smatteo */ 595222623Srmacklemstatic int 596172827Smatteocreate_service(struct netconfig *nconf) 597172827Smatteo{ 598172827Smatteo struct addrinfo hints, *res = NULL; 599172827Smatteo struct sockaddr_in *sin; 600172827Smatteo struct sockaddr_in6 *sin6; 601172827Smatteo struct __rpc_sockinfo si; 602172827Smatteo int aicode; 603172827Smatteo int fd; 604172827Smatteo int nhostsbak; 605172827Smatteo int one = 1; 606172827Smatteo int r; 607172827Smatteo u_int32_t host_addr[4]; /* IPv4 or IPv6 */ 608222623Srmacklem int mallocd_res; 609172827Smatteo 610172827Smatteo if ((nconf->nc_semantics != NC_TPI_CLTS) && 611172827Smatteo (nconf->nc_semantics != NC_TPI_COTS) && 612172827Smatteo (nconf->nc_semantics != NC_TPI_COTS_ORD)) 613222623Srmacklem return (1); /* not my type */ 614172827Smatteo 615172827Smatteo /* 616172827Smatteo * XXX - using RPC library internal functions. 617172827Smatteo */ 618172827Smatteo if (!__rpc_nconf2sockinfo(nconf, &si)) { 619172827Smatteo syslog(LOG_ERR, "cannot get information for %s", 620172827Smatteo nconf->nc_netid); 621222623Srmacklem return (1); 622172827Smatteo } 623172827Smatteo 624172827Smatteo /* Get mountd's address on this transport */ 625172827Smatteo memset(&hints, 0, sizeof hints); 626172827Smatteo hints.ai_flags = AI_PASSIVE; 627172827Smatteo hints.ai_family = si.si_af; 628172827Smatteo hints.ai_socktype = si.si_socktype; 629172827Smatteo hints.ai_protocol = si.si_proto; 630172827Smatteo 631172827Smatteo /* 632172827Smatteo * Bind to specific IPs if asked to 633172827Smatteo */ 634172827Smatteo nhostsbak = nhosts; 635172827Smatteo while (nhostsbak > 0) { 636172827Smatteo --nhostsbak; 637222623Srmacklem sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int)); 638222623Srmacklem if (sock_fd == NULL) 639222623Srmacklem out_of_mem(); 640222623Srmacklem sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */ 641222623Srmacklem mallocd_res = 0; 642222623Srmacklem 643172827Smatteo /* 644172827Smatteo * XXX - using RPC library internal functions. 645172827Smatteo */ 646172827Smatteo if ((fd = __rpc_nconf2fd(nconf)) < 0) { 647172827Smatteo int non_fatal = 0; 648172827Smatteo if (errno == EPROTONOSUPPORT && 649172827Smatteo nconf->nc_semantics != NC_TPI_CLTS) 650172827Smatteo non_fatal = 1; 651172827Smatteo 652172827Smatteo syslog(non_fatal ? LOG_DEBUG : LOG_ERR, 653172827Smatteo "cannot create socket for %s", nconf->nc_netid); 654222623Srmacklem if (non_fatal != 0) 655222623Srmacklem continue; 656222623Srmacklem exit(1); 657172827Smatteo } 658172827Smatteo 659172827Smatteo switch (hints.ai_family) { 660172827Smatteo case AF_INET: 661172827Smatteo if (inet_pton(AF_INET, hosts[nhostsbak], 662172827Smatteo host_addr) == 1) { 663222623Srmacklem hints.ai_flags |= AI_NUMERICHOST; 664172827Smatteo } else { 665172827Smatteo /* 666172827Smatteo * Skip if we have an AF_INET6 address. 667172827Smatteo */ 668172827Smatteo if (inet_pton(AF_INET6, hosts[nhostsbak], 669172827Smatteo host_addr) == 1) { 670172827Smatteo close(fd); 671172827Smatteo continue; 672172827Smatteo } 673172827Smatteo } 674172827Smatteo break; 675172827Smatteo case AF_INET6: 676172827Smatteo if (inet_pton(AF_INET6, hosts[nhostsbak], 677172827Smatteo host_addr) == 1) { 678222623Srmacklem hints.ai_flags |= AI_NUMERICHOST; 679172827Smatteo } else { 680172827Smatteo /* 681172827Smatteo * Skip if we have an AF_INET address. 682172827Smatteo */ 683172827Smatteo if (inet_pton(AF_INET, hosts[nhostsbak], 684172827Smatteo host_addr) == 1) { 685172827Smatteo close(fd); 686172827Smatteo continue; 687172827Smatteo } 688172827Smatteo } 689172827Smatteo 690172827Smatteo /* 691172827Smatteo * We're doing host-based access checks here, so don't 692172827Smatteo * allow v4-in-v6 to confuse things. The kernel will 693172827Smatteo * disable it by default on NFS sockets too. 694172827Smatteo */ 695172827Smatteo if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, 696172827Smatteo sizeof one) < 0) { 697172827Smatteo syslog(LOG_ERR, 698172827Smatteo "can't disable v4-in-v6 on IPv6 socket"); 699172827Smatteo exit(1); 700172827Smatteo } 701172827Smatteo break; 702172827Smatteo default: 703172827Smatteo break; 704172827Smatteo } 705172827Smatteo 706172827Smatteo /* 707172827Smatteo * If no hosts were specified, just bind to INADDR_ANY 708172827Smatteo */ 709172827Smatteo if (strcmp("*", hosts[nhostsbak]) == 0) { 710172827Smatteo if (svcport_str == NULL) { 711172827Smatteo res = malloc(sizeof(struct addrinfo)); 712172827Smatteo if (res == NULL) 713172827Smatteo out_of_mem(); 714222623Srmacklem mallocd_res = 1; 715172827Smatteo res->ai_flags = hints.ai_flags; 716172827Smatteo res->ai_family = hints.ai_family; 717172827Smatteo res->ai_protocol = hints.ai_protocol; 718172827Smatteo switch (res->ai_family) { 719172827Smatteo case AF_INET: 720172827Smatteo sin = malloc(sizeof(struct sockaddr_in)); 721172827Smatteo if (sin == NULL) 722172827Smatteo out_of_mem(); 723172827Smatteo sin->sin_family = AF_INET; 724172827Smatteo sin->sin_port = htons(0); 725172827Smatteo sin->sin_addr.s_addr = htonl(INADDR_ANY); 726172827Smatteo res->ai_addr = (struct sockaddr*) sin; 727172827Smatteo res->ai_addrlen = (socklen_t) 728222623Srmacklem sizeof(struct sockaddr_in); 729172827Smatteo break; 730172827Smatteo case AF_INET6: 731172827Smatteo sin6 = malloc(sizeof(struct sockaddr_in6)); 732173056Ssimon if (sin6 == NULL) 733172827Smatteo out_of_mem(); 734172827Smatteo sin6->sin6_family = AF_INET6; 735172827Smatteo sin6->sin6_port = htons(0); 736172827Smatteo sin6->sin6_addr = in6addr_any; 737172827Smatteo res->ai_addr = (struct sockaddr*) sin6; 738172827Smatteo res->ai_addrlen = (socklen_t) 739222623Srmacklem sizeof(struct sockaddr_in6); 740222623Srmacklem break; 741172827Smatteo default: 742222623Srmacklem syslog(LOG_ERR, "bad addr fam %d", 743222623Srmacklem res->ai_family); 744222623Srmacklem exit(1); 745172827Smatteo } 746172827Smatteo } else { 747172827Smatteo if ((aicode = getaddrinfo(NULL, svcport_str, 748172827Smatteo &hints, &res)) != 0) { 749172827Smatteo syslog(LOG_ERR, 750172827Smatteo "cannot get local address for %s: %s", 751172827Smatteo nconf->nc_netid, 752172827Smatteo gai_strerror(aicode)); 753222623Srmacklem close(fd); 754172827Smatteo continue; 755172827Smatteo } 756172827Smatteo } 757172827Smatteo } else { 758172827Smatteo if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str, 759172827Smatteo &hints, &res)) != 0) { 760172827Smatteo syslog(LOG_ERR, 761172827Smatteo "cannot get local address for %s: %s", 762172827Smatteo nconf->nc_netid, gai_strerror(aicode)); 763222623Srmacklem close(fd); 764172827Smatteo continue; 765172827Smatteo } 766172827Smatteo } 767172827Smatteo 768222623Srmacklem /* Store the fd. */ 769222623Srmacklem sock_fd[sock_fdcnt - 1] = fd; 770222623Srmacklem 771222623Srmacklem /* Now, attempt the bind. */ 772172827Smatteo r = bindresvport_sa(fd, res->ai_addr); 773172827Smatteo if (r != 0) { 774222623Srmacklem if (errno == EADDRINUSE && mallocd_svcport != 0) { 775222623Srmacklem if (mallocd_res != 0) { 776222623Srmacklem free(res->ai_addr); 777222623Srmacklem free(res); 778222623Srmacklem } else 779222623Srmacklem freeaddrinfo(res); 780222623Srmacklem return (-1); 781222623Srmacklem } 782172827Smatteo syslog(LOG_ERR, "bindresvport_sa: %m"); 783172827Smatteo exit(1); 784172827Smatteo } 785172827Smatteo 786222623Srmacklem if (svcport_str == NULL) { 787222623Srmacklem svcport_str = malloc(NI_MAXSERV * sizeof(char)); 788222623Srmacklem if (svcport_str == NULL) 789222623Srmacklem out_of_mem(); 790222623Srmacklem mallocd_svcport = 1; 791222623Srmacklem 792222623Srmacklem if (getnameinfo(res->ai_addr, 793222623Srmacklem res->ai_addr->sa_len, NULL, NI_MAXHOST, 794222623Srmacklem svcport_str, NI_MAXSERV * sizeof(char), 795222623Srmacklem NI_NUMERICHOST | NI_NUMERICSERV)) 796222623Srmacklem errx(1, "Cannot get port number"); 797222623Srmacklem } 798222623Srmacklem if (mallocd_res != 0) { 799222623Srmacklem free(res->ai_addr); 800222623Srmacklem free(res); 801222623Srmacklem } else 802222623Srmacklem freeaddrinfo(res); 803222623Srmacklem res = NULL; 804222623Srmacklem } 805222623Srmacklem return (0); 806222623Srmacklem} 807222623Srmacklem 808222623Srmacklem/* 809222623Srmacklem * Called after all the create_service() calls have succeeded, to complete 810222623Srmacklem * the setup and registration. 811222623Srmacklem */ 812222623Srmacklemstatic void 813222623Srmacklemcomplete_service(struct netconfig *nconf, char *port_str) 814222623Srmacklem{ 815222623Srmacklem struct addrinfo hints, *res = NULL; 816222623Srmacklem struct __rpc_sockinfo si; 817222623Srmacklem struct netbuf servaddr; 818222623Srmacklem SVCXPRT *transp = NULL; 819222623Srmacklem int aicode, fd, nhostsbak; 820222623Srmacklem int registered = 0; 821222623Srmacklem 822222623Srmacklem if ((nconf->nc_semantics != NC_TPI_CLTS) && 823222623Srmacklem (nconf->nc_semantics != NC_TPI_COTS) && 824222623Srmacklem (nconf->nc_semantics != NC_TPI_COTS_ORD)) 825222623Srmacklem return; /* not my type */ 826222623Srmacklem 827222623Srmacklem /* 828222623Srmacklem * XXX - using RPC library internal functions. 829222623Srmacklem */ 830222623Srmacklem if (!__rpc_nconf2sockinfo(nconf, &si)) { 831222623Srmacklem syslog(LOG_ERR, "cannot get information for %s", 832222623Srmacklem nconf->nc_netid); 833222623Srmacklem return; 834222623Srmacklem } 835222623Srmacklem 836222623Srmacklem nhostsbak = nhosts; 837222623Srmacklem while (nhostsbak > 0) { 838222623Srmacklem --nhostsbak; 839222623Srmacklem if (sock_fdpos >= sock_fdcnt) { 840222623Srmacklem /* Should never happen. */ 841222623Srmacklem syslog(LOG_ERR, "Ran out of socket fd's"); 842222623Srmacklem return; 843222623Srmacklem } 844222623Srmacklem fd = sock_fd[sock_fdpos++]; 845222623Srmacklem if (fd < 0) 846222623Srmacklem continue; 847222623Srmacklem 848172827Smatteo if (nconf->nc_semantics != NC_TPI_CLTS) 849172827Smatteo listen(fd, SOMAXCONN); 850172827Smatteo 851172827Smatteo if (nconf->nc_semantics == NC_TPI_CLTS ) 852172827Smatteo transp = svc_dg_create(fd, 0, 0); 853172827Smatteo else 854172827Smatteo transp = svc_vc_create(fd, RPC_MAXDATASIZE, 855172827Smatteo RPC_MAXDATASIZE); 856172827Smatteo 857172827Smatteo if (transp != (SVCXPRT *) NULL) { 858194880Sdfr if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv, 859172827Smatteo NULL)) 860172827Smatteo syslog(LOG_ERR, 861194880Sdfr "can't register %s MOUNTVERS service", 862172827Smatteo nconf->nc_netid); 863172827Smatteo if (!force_v2) { 864194880Sdfr if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3, 865172827Smatteo mntsrv, NULL)) 866172827Smatteo syslog(LOG_ERR, 867194880Sdfr "can't register %s MOUNTVERS3 service", 868172827Smatteo nconf->nc_netid); 869172827Smatteo } 870172827Smatteo } else 871172827Smatteo syslog(LOG_WARNING, "can't create %s services", 872172827Smatteo nconf->nc_netid); 873172827Smatteo 874172827Smatteo if (registered == 0) { 875172827Smatteo registered = 1; 876172827Smatteo memset(&hints, 0, sizeof hints); 877172827Smatteo hints.ai_flags = AI_PASSIVE; 878172827Smatteo hints.ai_family = si.si_af; 879172827Smatteo hints.ai_socktype = si.si_socktype; 880172827Smatteo hints.ai_protocol = si.si_proto; 881172827Smatteo 882222623Srmacklem if ((aicode = getaddrinfo(NULL, port_str, &hints, 883172827Smatteo &res)) != 0) { 884172827Smatteo syslog(LOG_ERR, "cannot get local address: %s", 885172827Smatteo gai_strerror(aicode)); 886172827Smatteo exit(1); 887172827Smatteo } 888172827Smatteo 889172827Smatteo servaddr.buf = malloc(res->ai_addrlen); 890172827Smatteo memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen); 891172827Smatteo servaddr.len = res->ai_addrlen; 892172827Smatteo 893194880Sdfr rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr); 894194880Sdfr rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr); 895172827Smatteo 896172827Smatteo xcreated++; 897172827Smatteo freeaddrinfo(res); 898172827Smatteo } 899172827Smatteo } /* end while */ 9001558Srgrimes} 9011558Srgrimes 902222623Srmacklem/* 903222623Srmacklem * Clear out sockets after a failure to bind one of them, so that the 904222623Srmacklem * cycle of socket creation/binding can start anew. 905222623Srmacklem */ 90637663Scharnierstatic void 907222623Srmacklemclearout_service(void) 908222623Srmacklem{ 909222623Srmacklem int i; 910222623Srmacklem 911222623Srmacklem for (i = 0; i < sock_fdcnt; i++) { 912222623Srmacklem if (sock_fd[i] >= 0) { 913222623Srmacklem shutdown(sock_fd[i], SHUT_RDWR); 914222623Srmacklem close(sock_fd[i]); 915222623Srmacklem } 916222623Srmacklem } 917222623Srmacklem} 918222623Srmacklem 919222623Srmacklemstatic void 920216587Scharnierusage(void) 92137663Scharnier{ 92237663Scharnier fprintf(stderr, 923192993Srmacklem "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] " 924172827Smatteo "[-h <bindip>] [export_file ...]\n"); 92537663Scharnier exit(1); 92637663Scharnier} 92737663Scharnier 9281558Srgrimes/* 9291558Srgrimes * The mount rpc service 9301558Srgrimes */ 9311558Srgrimesvoid 932216587Scharniermntsrv(struct svc_req *rqstp, SVCXPRT *transp) 9331558Srgrimes{ 9341558Srgrimes struct exportlist *ep; 9351558Srgrimes struct dirlist *dp; 9369336Sdfr struct fhreturn fhr; 9371558Srgrimes struct stat stb; 9381558Srgrimes struct statfs fsb; 93974462Salfred char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 94074462Salfred int lookup_failed = 1; 94174462Salfred struct sockaddr *saddr; 9429336Sdfr u_short sport; 943194880Sdfr char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN]; 94428911Sguido int bad = 0, defset, hostset; 9459336Sdfr sigset_t sighup_mask; 946240902Srmacklem int numsecflavors, *secflavorsp; 9471558Srgrimes 9489336Sdfr sigemptyset(&sighup_mask); 9499336Sdfr sigaddset(&sighup_mask, SIGHUP); 95074462Salfred saddr = svc_getrpccaller(transp)->buf; 95174462Salfred switch (saddr->sa_family) { 95274462Salfred case AF_INET6: 95375635Siedowse sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 95474462Salfred break; 95574462Salfred case AF_INET: 95675635Siedowse sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 95774462Salfred break; 95874462Salfred default: 95974462Salfred syslog(LOG_ERR, "request from unknown address family"); 96074462Salfred return; 96174462Salfred } 96274462Salfred lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, 96374462Salfred NULL, 0, 0); 96474462Salfred getnameinfo(saddr, saddr->sa_len, numerichost, 96574462Salfred sizeof numerichost, NULL, 0, NI_NUMERICHOST); 9661558Srgrimes switch (rqstp->rq_proc) { 9671558Srgrimes case NULLPROC: 968121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL)) 96937663Scharnier syslog(LOG_ERR, "can't send reply"); 9701558Srgrimes return; 971194880Sdfr case MOUNTPROC_MNT: 9729336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 97331656Sguido syslog(LOG_NOTICE, 97431656Sguido "mount request from %s from unprivileged port", 97574462Salfred numerichost); 9761558Srgrimes svcerr_weakauth(transp); 9771558Srgrimes return; 9781558Srgrimes } 979121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 98031656Sguido syslog(LOG_NOTICE, "undecodable mount request from %s", 98174462Salfred numerichost); 9821558Srgrimes svcerr_decode(transp); 9831558Srgrimes return; 9841558Srgrimes } 9851558Srgrimes 9861558Srgrimes /* 9871558Srgrimes * Get the real pathname and make sure it is a directory 9889336Sdfr * or a regular file if the -r option was specified 9899336Sdfr * and it exists. 9901558Srgrimes */ 99151968Salfred if (realpath(rpcpath, dirpath) == NULL || 9921558Srgrimes stat(dirpath, &stb) < 0 || 9939336Sdfr (!S_ISDIR(stb.st_mode) && 99474462Salfred (dir_only || !S_ISREG(stb.st_mode))) || 9951558Srgrimes statfs(dirpath, &fsb) < 0) { 9961558Srgrimes chdir("/"); /* Just in case realpath doesn't */ 99731656Sguido syslog(LOG_NOTICE, 99837663Scharnier "mount request from %s for non existent path %s", 99974462Salfred numerichost, dirpath); 10001558Srgrimes if (debug) 100137663Scharnier warnx("stat failed on %s", dirpath); 100228911Sguido bad = ENOENT; /* We will send error reply later */ 10031558Srgrimes } 10041558Srgrimes 10051558Srgrimes /* Check in the exports list */ 10069336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 10071558Srgrimes ep = ex_search(&fsb.f_fsid); 10089336Sdfr hostset = defset = 0; 1009240902Srmacklem if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset, 1010240902Srmacklem &numsecflavors, &secflavorsp) || 10111558Srgrimes ((dp = dirp_search(ep->ex_dirl, dirpath)) && 1012240902Srmacklem chk_host(dp, saddr, &defset, &hostset, &numsecflavors, 1013240902Srmacklem &secflavorsp)) || 101474462Salfred (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 101574462Salfred scan_tree(ep->ex_dirl, saddr) == 0))) { 101628911Sguido if (bad) { 1017121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 101828911Sguido (caddr_t)&bad)) 101937663Scharnier syslog(LOG_ERR, "can't send reply"); 102028911Sguido sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 102128911Sguido return; 102228911Sguido } 1023240902Srmacklem if (hostset & DP_HOSTSET) { 10249336Sdfr fhr.fhr_flag = hostset; 1025240902Srmacklem fhr.fhr_numsecflavors = numsecflavors; 1026240902Srmacklem fhr.fhr_secflavors = secflavorsp; 1027240902Srmacklem } else { 10289336Sdfr fhr.fhr_flag = defset; 1029240902Srmacklem fhr.fhr_numsecflavors = ep->ex_defnumsecflavors; 1030240902Srmacklem fhr.fhr_secflavors = ep->ex_defsecflavors; 1031240902Srmacklem } 10329336Sdfr fhr.fhr_vers = rqstp->rq_vers; 10331558Srgrimes /* Get the file handle */ 103423681Speter memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 10359336Sdfr if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 10361558Srgrimes bad = errno; 103737663Scharnier syslog(LOG_ERR, "can't get fh for %s", dirpath); 1038121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 10391558Srgrimes (caddr_t)&bad)) 104037663Scharnier syslog(LOG_ERR, "can't send reply"); 10419336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 10421558Srgrimes return; 10431558Srgrimes } 1044121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, 1045121556Speter (caddr_t)&fhr)) 104637663Scharnier syslog(LOG_ERR, "can't send reply"); 104774462Salfred if (!lookup_failed) 104874462Salfred add_mlist(host, dirpath); 10491558Srgrimes else 105074462Salfred add_mlist(numerichost, dirpath); 10511558Srgrimes if (debug) 105237663Scharnier warnx("mount successful"); 1053121767Speter if (dolog) 105431656Sguido syslog(LOG_NOTICE, 105531656Sguido "mount request succeeded from %s for %s", 105674462Salfred numerichost, dirpath); 105731656Sguido } else { 10581558Srgrimes bad = EACCES; 105931656Sguido syslog(LOG_NOTICE, 106031656Sguido "mount request denied from %s for %s", 106174462Salfred numerichost, dirpath); 106231656Sguido } 106328911Sguido 1064121556Speter if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, 1065121556Speter (caddr_t)&bad)) 106637663Scharnier syslog(LOG_ERR, "can't send reply"); 10679336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 10681558Srgrimes return; 1069194880Sdfr case MOUNTPROC_DUMP: 1070121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL)) 107137663Scharnier syslog(LOG_ERR, "can't send reply"); 1072121767Speter else if (dolog) 107331656Sguido syslog(LOG_NOTICE, 107431656Sguido "dump request succeeded from %s", 107574462Salfred numerichost); 10761558Srgrimes return; 1077194880Sdfr case MOUNTPROC_UMNT: 10789336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 107931656Sguido syslog(LOG_NOTICE, 108031656Sguido "umount request from %s from unprivileged port", 108174462Salfred numerichost); 10821558Srgrimes svcerr_weakauth(transp); 10831558Srgrimes return; 10841558Srgrimes } 1085121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 108631656Sguido syslog(LOG_NOTICE, "undecodable umount request from %s", 108774462Salfred numerichost); 10881558Srgrimes svcerr_decode(transp); 10891558Srgrimes return; 10901558Srgrimes } 109151968Salfred if (realpath(rpcpath, dirpath) == NULL) { 109251968Salfred syslog(LOG_NOTICE, "umount request from %s " 109351968Salfred "for non existent path %s", 109474462Salfred numerichost, dirpath); 109551968Salfred } 1096121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 109737663Scharnier syslog(LOG_ERR, "can't send reply"); 109874462Salfred if (!lookup_failed) 109975635Siedowse del_mlist(host, dirpath); 110075635Siedowse del_mlist(numerichost, dirpath); 1101121767Speter if (dolog) 110231656Sguido syslog(LOG_NOTICE, 110331656Sguido "umount request succeeded from %s for %s", 110474462Salfred numerichost, dirpath); 11051558Srgrimes return; 1106194880Sdfr case MOUNTPROC_UMNTALL: 11079336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 110831656Sguido syslog(LOG_NOTICE, 110931656Sguido "umountall request from %s from unprivileged port", 111074462Salfred numerichost); 11111558Srgrimes svcerr_weakauth(transp); 11121558Srgrimes return; 11131558Srgrimes } 1114121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 111537663Scharnier syslog(LOG_ERR, "can't send reply"); 111674462Salfred if (!lookup_failed) 111775635Siedowse del_mlist(host, NULL); 111875635Siedowse del_mlist(numerichost, NULL); 1119121767Speter if (dolog) 112031656Sguido syslog(LOG_NOTICE, 112131656Sguido "umountall request succeeded from %s", 112274462Salfred numerichost); 11231558Srgrimes return; 1124194880Sdfr case MOUNTPROC_EXPORT: 1125121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL)) 1126121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, 1127121556Speter (caddr_t)NULL)) 1128100117Salfred syslog(LOG_ERR, "can't send reply"); 1129121767Speter if (dolog) 113031656Sguido syslog(LOG_NOTICE, 113131656Sguido "export request succeeded from %s", 113274462Salfred numerichost); 11331558Srgrimes return; 11341558Srgrimes default: 11351558Srgrimes svcerr_noproc(transp); 11361558Srgrimes return; 11371558Srgrimes } 11381558Srgrimes} 11391558Srgrimes 11401558Srgrimes/* 11411558Srgrimes * Xdr conversion for a dirpath string 11421558Srgrimes */ 11431558Srgrimesint 1144216587Scharnierxdr_dir(XDR *xdrsp, char *dirp) 11451558Srgrimes{ 1146194880Sdfr return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); 11471558Srgrimes} 11481558Srgrimes 11491558Srgrimes/* 11509336Sdfr * Xdr routine to generate file handle reply 11511558Srgrimes */ 11521558Srgrimesint 1153216587Scharnierxdr_fhs(XDR *xdrsp, caddr_t cp) 11541558Srgrimes{ 115592806Sobrien struct fhreturn *fhrp = (struct fhreturn *)cp; 11569336Sdfr u_long ok = 0, len, auth; 1157184588Sdfr int i; 11581558Srgrimes 11591558Srgrimes if (!xdr_long(xdrsp, &ok)) 11601558Srgrimes return (0); 11619336Sdfr switch (fhrp->fhr_vers) { 11629336Sdfr case 1: 11639336Sdfr return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 11649336Sdfr case 3: 11659336Sdfr len = NFSX_V3FH; 11669336Sdfr if (!xdr_long(xdrsp, &len)) 11679336Sdfr return (0); 11689336Sdfr if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 11699336Sdfr return (0); 1170184588Sdfr if (fhrp->fhr_numsecflavors) { 1171184588Sdfr if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors)) 1172184588Sdfr return (0); 1173184588Sdfr for (i = 0; i < fhrp->fhr_numsecflavors; i++) 1174184588Sdfr if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i])) 1175184588Sdfr return (0); 1176184588Sdfr return (1); 1177184588Sdfr } else { 1178184588Sdfr auth = AUTH_SYS; 1179184588Sdfr len = 1; 1180184588Sdfr if (!xdr_long(xdrsp, &len)) 1181184588Sdfr return (0); 1182184588Sdfr return (xdr_long(xdrsp, &auth)); 1183184588Sdfr } 11849336Sdfr }; 11859336Sdfr return (0); 11861558Srgrimes} 11871558Srgrimes 11881558Srgrimesint 1189216587Scharnierxdr_mlist(XDR *xdrsp, caddr_t cp __unused) 11901558Srgrimes{ 11911558Srgrimes struct mountlist *mlp; 11921558Srgrimes int true = 1; 11931558Srgrimes int false = 0; 11941558Srgrimes char *strp; 11951558Srgrimes 11961558Srgrimes mlp = mlhead; 11971558Srgrimes while (mlp) { 11981558Srgrimes if (!xdr_bool(xdrsp, &true)) 11991558Srgrimes return (0); 12001558Srgrimes strp = &mlp->ml_host[0]; 1201194880Sdfr if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) 12021558Srgrimes return (0); 12031558Srgrimes strp = &mlp->ml_dirp[0]; 1204194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 12051558Srgrimes return (0); 12061558Srgrimes mlp = mlp->ml_next; 12071558Srgrimes } 12081558Srgrimes if (!xdr_bool(xdrsp, &false)) 12091558Srgrimes return (0); 12101558Srgrimes return (1); 12111558Srgrimes} 12121558Srgrimes 12131558Srgrimes/* 12141558Srgrimes * Xdr conversion for export list 12151558Srgrimes */ 12161558Srgrimesint 1217216587Scharnierxdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief) 12181558Srgrimes{ 12191558Srgrimes struct exportlist *ep; 12201558Srgrimes int false = 0; 12219336Sdfr int putdef; 12229336Sdfr sigset_t sighup_mask; 12231558Srgrimes 12249336Sdfr sigemptyset(&sighup_mask); 12259336Sdfr sigaddset(&sighup_mask, SIGHUP); 12269336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 12271558Srgrimes ep = exphead; 12281558Srgrimes while (ep) { 12291558Srgrimes putdef = 0; 1230100117Salfred if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, 1231100117Salfred &putdef, brief)) 12321558Srgrimes goto errout; 12331558Srgrimes if (ep->ex_defdir && putdef == 0 && 12341558Srgrimes put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 1235100117Salfred &putdef, brief)) 12361558Srgrimes goto errout; 12371558Srgrimes ep = ep->ex_next; 12381558Srgrimes } 12399336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 12401558Srgrimes if (!xdr_bool(xdrsp, &false)) 12411558Srgrimes return (0); 12421558Srgrimes return (1); 12431558Srgrimeserrout: 12449336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 12451558Srgrimes return (0); 12461558Srgrimes} 12471558Srgrimes 12481558Srgrimes/* 12491558Srgrimes * Called from xdr_explist() to traverse the tree and export the 12501558Srgrimes * directory paths. 12511558Srgrimes */ 12521558Srgrimesint 1253216587Scharnierput_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp, 1254216587Scharnier int brief) 12551558Srgrimes{ 12561558Srgrimes struct grouplist *grp; 12571558Srgrimes struct hostlist *hp; 12581558Srgrimes int true = 1; 12591558Srgrimes int false = 0; 12601558Srgrimes int gotalldir = 0; 12611558Srgrimes char *strp; 12621558Srgrimes 12631558Srgrimes if (dp) { 1264100117Salfred if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief)) 12651558Srgrimes return (1); 12661558Srgrimes if (!xdr_bool(xdrsp, &true)) 12671558Srgrimes return (1); 12681558Srgrimes strp = dp->dp_dirp; 1269194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 12701558Srgrimes return (1); 12711558Srgrimes if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 12721558Srgrimes gotalldir = 1; 12731558Srgrimes *putdefp = 1; 12741558Srgrimes } 1275100117Salfred if (brief) { 1276100117Salfred if (!xdr_bool(xdrsp, &true)) 1277100117Salfred return (1); 1278100117Salfred strp = "(...)"; 1279194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 1280100117Salfred return (1); 1281100117Salfred } else if ((dp->dp_flag & DP_DEFSET) == 0 && 12821558Srgrimes (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 12831558Srgrimes hp = dp->dp_hosts; 12841558Srgrimes while (hp) { 12851558Srgrimes grp = hp->ht_grp; 12861558Srgrimes if (grp->gr_type == GT_HOST) { 12871558Srgrimes if (!xdr_bool(xdrsp, &true)) 12881558Srgrimes return (1); 128974462Salfred strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 12908871Srgrimes if (!xdr_string(xdrsp, &strp, 1291194880Sdfr MNTNAMLEN)) 12921558Srgrimes return (1); 12931558Srgrimes } else if (grp->gr_type == GT_NET) { 12941558Srgrimes if (!xdr_bool(xdrsp, &true)) 12951558Srgrimes return (1); 12961558Srgrimes strp = grp->gr_ptr.gt_net.nt_name; 12978871Srgrimes if (!xdr_string(xdrsp, &strp, 1298194880Sdfr MNTNAMLEN)) 12991558Srgrimes return (1); 13001558Srgrimes } 13011558Srgrimes hp = hp->ht_next; 13021558Srgrimes if (gotalldir && hp == (struct hostlist *)NULL) { 13031558Srgrimes hp = adp->dp_hosts; 13041558Srgrimes gotalldir = 0; 13051558Srgrimes } 13061558Srgrimes } 13071558Srgrimes } 13081558Srgrimes if (!xdr_bool(xdrsp, &false)) 13091558Srgrimes return (1); 1310100117Salfred if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief)) 13111558Srgrimes return (1); 13121558Srgrimes } 13131558Srgrimes return (0); 13141558Srgrimes} 13151558Srgrimes 1316100117Salfredint 1317216587Scharnierxdr_explist(XDR *xdrsp, caddr_t cp) 1318100117Salfred{ 1319100117Salfred 1320100117Salfred return xdr_explist_common(xdrsp, cp, 0); 1321100117Salfred} 1322100117Salfred 1323100117Salfredint 1324216587Scharnierxdr_explist_brief(XDR *xdrsp, caddr_t cp) 1325100117Salfred{ 1326100117Salfred 1327100117Salfred return xdr_explist_common(xdrsp, cp, 1); 1328100117Salfred} 1329100117Salfred 133096622Siedowsechar *line; 133196622Siedowseint linesize; 13321558SrgrimesFILE *exp_file; 13331558Srgrimes 13341558Srgrimes/* 1335166440Spjd * Get the export list from one, currently open file 13361558Srgrimes */ 1337166440Spjdstatic void 1338216587Scharnierget_exportlist_one(void) 13391558Srgrimes{ 13401558Srgrimes struct exportlist *ep, *ep2; 13411558Srgrimes struct grouplist *grp, *tgrp; 13421558Srgrimes struct exportlist **epp; 13431558Srgrimes struct dirlist *dirhead; 1344166440Spjd struct statfs fsb; 134572650Sgreen struct xucred anon; 13461558Srgrimes char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 1347166440Spjd int len, has_host, exflags, got_nondir, dirplen, netgrp; 13481558Srgrimes 1349192934Srmacklem v4root_phase = 0; 13501558Srgrimes dirhead = (struct dirlist *)NULL; 13511558Srgrimes while (get_line()) { 13521558Srgrimes if (debug) 135337663Scharnier warnx("got line %s", line); 13541558Srgrimes cp = line; 13551558Srgrimes nextfield(&cp, &endcp); 13561558Srgrimes if (*cp == '#') 13571558Srgrimes goto nextline; 13581558Srgrimes 13591558Srgrimes /* 13601558Srgrimes * Set defaults. 13611558Srgrimes */ 13621558Srgrimes has_host = FALSE; 13631558Srgrimes anon = def_anon; 13641558Srgrimes exflags = MNT_EXPORTED; 13651558Srgrimes got_nondir = 0; 13661558Srgrimes opt_flags = 0; 13671558Srgrimes ep = (struct exportlist *)NULL; 1368192934Srmacklem dirp = NULL; 13691558Srgrimes 13701558Srgrimes /* 1371192934Srmacklem * Handle the V4 root dir. 1372192934Srmacklem */ 1373192934Srmacklem if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') { 1374192934Srmacklem /* 1375192934Srmacklem * V4: just indicates that it is the v4 root point, 1376192934Srmacklem * so skip over that and set v4root_phase. 1377192934Srmacklem */ 1378192934Srmacklem if (v4root_phase > 0) { 1379192934Srmacklem syslog(LOG_ERR, "V4:duplicate line, ignored"); 1380192934Srmacklem goto nextline; 1381192934Srmacklem } 1382192934Srmacklem v4root_phase = 1; 1383192934Srmacklem cp += 3; 1384192934Srmacklem nextfield(&cp, &endcp); 1385192934Srmacklem } 1386192934Srmacklem 1387192934Srmacklem /* 13881558Srgrimes * Create new exports list entry 13891558Srgrimes */ 13901558Srgrimes len = endcp-cp; 13911558Srgrimes tgrp = grp = get_grp(); 13921558Srgrimes while (len > 0) { 1393194880Sdfr if (len > MNTNAMLEN) { 13941558Srgrimes getexp_err(ep, tgrp); 13951558Srgrimes goto nextline; 13961558Srgrimes } 13971558Srgrimes if (*cp == '-') { 13981558Srgrimes if (ep == (struct exportlist *)NULL) { 13991558Srgrimes getexp_err(ep, tgrp); 14001558Srgrimes goto nextline; 14011558Srgrimes } 14021558Srgrimes if (debug) 140337663Scharnier warnx("doing opt %s", cp); 14041558Srgrimes got_nondir = 1; 14051558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 14061558Srgrimes &exflags, &anon)) { 14071558Srgrimes getexp_err(ep, tgrp); 14081558Srgrimes goto nextline; 14091558Srgrimes } 14101558Srgrimes } else if (*cp == '/') { 14111558Srgrimes savedc = *endcp; 14121558Srgrimes *endcp = '\0'; 1413192934Srmacklem if (v4root_phase > 1) { 1414192934Srmacklem if (dirp != NULL) { 1415192934Srmacklem syslog(LOG_ERR, "Multiple V4 dirs"); 1416192934Srmacklem getexp_err(ep, tgrp); 1417192934Srmacklem goto nextline; 1418192934Srmacklem } 1419192934Srmacklem } 14201558Srgrimes if (check_dirpath(cp) && 14211558Srgrimes statfs(cp, &fsb) >= 0) { 14221558Srgrimes if (got_nondir) { 142337663Scharnier syslog(LOG_ERR, "dirs must be first"); 14241558Srgrimes getexp_err(ep, tgrp); 14251558Srgrimes goto nextline; 14261558Srgrimes } 1427192934Srmacklem if (v4root_phase == 1) { 1428192934Srmacklem if (dirp != NULL) { 1429192934Srmacklem syslog(LOG_ERR, "Multiple V4 dirs"); 14301558Srgrimes getexp_err(ep, tgrp); 14311558Srgrimes goto nextline; 14321558Srgrimes } 1433192934Srmacklem if (strlen(v4root_dirpath) == 0) { 1434192934Srmacklem strlcpy(v4root_dirpath, cp, 1435192934Srmacklem sizeof (v4root_dirpath)); 1436192934Srmacklem } else if (strcmp(v4root_dirpath, cp) 1437192934Srmacklem != 0) { 1438192934Srmacklem syslog(LOG_ERR, 1439192934Srmacklem "different V4 dirpath %s", cp); 1440192934Srmacklem getexp_err(ep, tgrp); 1441192934Srmacklem goto nextline; 1442192934Srmacklem } 1443192934Srmacklem dirp = cp; 1444192934Srmacklem v4root_phase = 2; 1445192934Srmacklem got_nondir = 1; 1446192934Srmacklem ep = get_exp(); 14471558Srgrimes } else { 1448192934Srmacklem if (ep) { 1449192934Srmacklem if (ep->ex_fs.val[0] != 1450192934Srmacklem fsb.f_fsid.val[0] || 1451192934Srmacklem ep->ex_fs.val[1] != 1452192934Srmacklem fsb.f_fsid.val[1]) { 1453192934Srmacklem getexp_err(ep, tgrp); 1454192934Srmacklem goto nextline; 1455192934Srmacklem } 1456192934Srmacklem } else { 1457192934Srmacklem /* 1458192934Srmacklem * See if this directory is already 1459192934Srmacklem * in the list. 1460192934Srmacklem */ 1461192934Srmacklem ep = ex_search(&fsb.f_fsid); 1462192934Srmacklem if (ep == (struct exportlist *)NULL) { 1463192934Srmacklem ep = get_exp(); 1464192934Srmacklem ep->ex_fs = fsb.f_fsid; 1465192934Srmacklem ep->ex_fsdir = (char *)malloc 1466192934Srmacklem (strlen(fsb.f_mntonname) + 1); 1467192934Srmacklem if (ep->ex_fsdir) 1468192934Srmacklem strcpy(ep->ex_fsdir, 1469192934Srmacklem fsb.f_mntonname); 1470192934Srmacklem else 1471192934Srmacklem out_of_mem(); 1472192934Srmacklem if (debug) 1473192934Srmacklem warnx( 1474192934Srmacklem "making new ep fs=0x%x,0x%x", 1475192934Srmacklem fsb.f_fsid.val[0], 1476192934Srmacklem fsb.f_fsid.val[1]); 1477192934Srmacklem } else if (debug) 1478192934Srmacklem warnx("found ep fs=0x%x,0x%x", 1479192934Srmacklem fsb.f_fsid.val[0], 1480192934Srmacklem fsb.f_fsid.val[1]); 1481192934Srmacklem } 1482192934Srmacklem 14831558Srgrimes /* 1484192934Srmacklem * Add dirpath to export mount point. 14851558Srgrimes */ 1486192934Srmacklem dirp = add_expdir(&dirhead, cp, len); 1487192934Srmacklem dirplen = len; 14881558Srgrimes } 14891558Srgrimes } else { 14901558Srgrimes getexp_err(ep, tgrp); 14911558Srgrimes goto nextline; 14921558Srgrimes } 14931558Srgrimes *endcp = savedc; 14941558Srgrimes } else { 14951558Srgrimes savedc = *endcp; 14961558Srgrimes *endcp = '\0'; 14971558Srgrimes got_nondir = 1; 14981558Srgrimes if (ep == (struct exportlist *)NULL) { 14991558Srgrimes getexp_err(ep, tgrp); 15001558Srgrimes goto nextline; 15011558Srgrimes } 15021558Srgrimes 15031558Srgrimes /* 15041558Srgrimes * Get the host or netgroup. 15051558Srgrimes */ 15061558Srgrimes setnetgrent(cp); 15071558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 15081558Srgrimes do { 15091558Srgrimes if (has_host) { 15101558Srgrimes grp->gr_next = get_grp(); 15111558Srgrimes grp = grp->gr_next; 15121558Srgrimes } 15131558Srgrimes if (netgrp) { 151437003Sjoerg if (hst == 0) { 151537663Scharnier syslog(LOG_ERR, 151637663Scharnier "null hostname in netgroup %s, skipping", cp); 151737004Sjoerg grp->gr_type = GT_IGNORE; 151837003Sjoerg } else if (get_host(hst, grp, tgrp)) { 151937663Scharnier syslog(LOG_ERR, 152037663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 152129317Sjlemon grp->gr_type = GT_IGNORE; 15221558Srgrimes } 15237401Swpaul } else if (get_host(cp, grp, tgrp)) { 152437663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 152529317Sjlemon grp->gr_type = GT_IGNORE; 15261558Srgrimes } 15271558Srgrimes has_host = TRUE; 15281558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 15291558Srgrimes endnetgrent(); 15301558Srgrimes *endcp = savedc; 15311558Srgrimes } 15321558Srgrimes cp = endcp; 15331558Srgrimes nextfield(&cp, &endcp); 15341558Srgrimes len = endcp - cp; 15351558Srgrimes } 15361558Srgrimes if (check_options(dirhead)) { 15371558Srgrimes getexp_err(ep, tgrp); 15381558Srgrimes goto nextline; 15391558Srgrimes } 15401558Srgrimes if (!has_host) { 154175641Siedowse grp->gr_type = GT_DEFAULT; 15421558Srgrimes if (debug) 154337663Scharnier warnx("adding a default entry"); 15441558Srgrimes 15451558Srgrimes /* 15461558Srgrimes * Don't allow a network export coincide with a list of 15471558Srgrimes * host(s) on the same line. 15481558Srgrimes */ 15491558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 155075801Siedowse syslog(LOG_ERR, "network/host conflict"); 15511558Srgrimes getexp_err(ep, tgrp); 15521558Srgrimes goto nextline; 155329317Sjlemon 155474462Salfred /* 155574462Salfred * If an export list was specified on this line, make sure 155629317Sjlemon * that we have at least one valid entry, otherwise skip it. 155729317Sjlemon */ 155829317Sjlemon } else { 155929317Sjlemon grp = tgrp; 156074462Salfred while (grp && grp->gr_type == GT_IGNORE) 156129317Sjlemon grp = grp->gr_next; 156229317Sjlemon if (! grp) { 156329317Sjlemon getexp_err(ep, tgrp); 156429317Sjlemon goto nextline; 156529317Sjlemon } 15661558Srgrimes } 15671558Srgrimes 1568192934Srmacklem if (v4root_phase == 1) { 1569192934Srmacklem syslog(LOG_ERR, "V4:root, no dirp, ignored"); 1570192934Srmacklem getexp_err(ep, tgrp); 1571192934Srmacklem goto nextline; 1572192934Srmacklem } 1573192934Srmacklem 15741558Srgrimes /* 15751558Srgrimes * Loop through hosts, pushing the exports into the kernel. 15761558Srgrimes * After loop, tgrp points to the start of the list and 15771558Srgrimes * grp points to the last entry in the list. 15781558Srgrimes */ 15791558Srgrimes grp = tgrp; 15801558Srgrimes do { 158175635Siedowse if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 158275635Siedowse &fsb)) { 158375635Siedowse getexp_err(ep, tgrp); 158475635Siedowse goto nextline; 158575635Siedowse } 15861558Srgrimes } while (grp->gr_next && (grp = grp->gr_next)); 15871558Srgrimes 15881558Srgrimes /* 1589192934Srmacklem * For V4: don't enter in mount lists. 1590192934Srmacklem */ 1591194773Srmacklem if (v4root_phase > 0 && v4root_phase <= 2) { 1592194773Srmacklem /* 1593194773Srmacklem * Since these structures aren't used by mountd, 1594194773Srmacklem * free them up now. 1595194773Srmacklem */ 1596194773Srmacklem if (ep != NULL) 1597194773Srmacklem free_exp(ep); 1598194773Srmacklem while (tgrp != NULL) { 1599194773Srmacklem grp = tgrp; 1600194773Srmacklem tgrp = tgrp->gr_next; 1601194773Srmacklem free_grp(grp); 1602194773Srmacklem } 1603192934Srmacklem goto nextline; 1604194773Srmacklem } 1605192934Srmacklem 1606192934Srmacklem /* 16071558Srgrimes * Success. Update the data structures. 16081558Srgrimes */ 16091558Srgrimes if (has_host) { 16109336Sdfr hang_dirp(dirhead, tgrp, ep, opt_flags); 16111558Srgrimes grp->gr_next = grphead; 16121558Srgrimes grphead = tgrp; 16131558Srgrimes } else { 16141558Srgrimes hang_dirp(dirhead, (struct grouplist *)NULL, ep, 16159336Sdfr opt_flags); 16161558Srgrimes free_grp(grp); 16171558Srgrimes } 16181558Srgrimes dirhead = (struct dirlist *)NULL; 16191558Srgrimes if ((ep->ex_flag & EX_LINKED) == 0) { 16201558Srgrimes ep2 = exphead; 16211558Srgrimes epp = &exphead; 16221558Srgrimes 16231558Srgrimes /* 16241558Srgrimes * Insert in the list in alphabetical order. 16251558Srgrimes */ 16261558Srgrimes while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { 16271558Srgrimes epp = &ep2->ex_next; 16281558Srgrimes ep2 = ep2->ex_next; 16291558Srgrimes } 16301558Srgrimes if (ep2) 16311558Srgrimes ep->ex_next = ep2; 16321558Srgrimes *epp = ep; 16331558Srgrimes ep->ex_flag |= EX_LINKED; 16341558Srgrimes } 16351558Srgrimesnextline: 1636192934Srmacklem v4root_phase = 0; 16371558Srgrimes if (dirhead) { 16381558Srgrimes free_dir(dirhead); 16391558Srgrimes dirhead = (struct dirlist *)NULL; 16401558Srgrimes } 16411558Srgrimes } 16421558Srgrimes} 16431558Srgrimes 16441558Srgrimes/* 1645166440Spjd * Get the export list from all specified files 1646166440Spjd */ 1647166440Spjdvoid 1648216587Scharnierget_exportlist(void) 1649166440Spjd{ 1650166440Spjd struct exportlist *ep, *ep2; 1651166440Spjd struct grouplist *grp, *tgrp; 1652166440Spjd struct export_args export; 1653166440Spjd struct iovec *iov; 1654166440Spjd struct statfs *fsp, *mntbufp; 1655166440Spjd struct xvfsconf vfc; 1656166440Spjd char errmsg[255]; 1657230352Seadler int num, i; 1658166440Spjd int iovlen; 1659168684Spjd int done; 1660192934Srmacklem struct nfsex_args eargs; 1661166440Spjd 1662192934Srmacklem v4root_dirpath[0] = '\0'; 1663166440Spjd bzero(&export, sizeof(export)); 1664166440Spjd export.ex_flags = MNT_DELEXPORT; 1665166440Spjd iov = NULL; 1666166440Spjd iovlen = 0; 1667166440Spjd bzero(errmsg, sizeof(errmsg)); 1668166440Spjd 1669166440Spjd /* 1670166440Spjd * First, get rid of the old list 1671166440Spjd */ 1672166440Spjd ep = exphead; 1673166440Spjd while (ep) { 1674166440Spjd ep2 = ep; 1675166440Spjd ep = ep->ex_next; 1676166440Spjd free_exp(ep2); 1677166440Spjd } 1678166440Spjd exphead = (struct exportlist *)NULL; 1679166440Spjd 1680166440Spjd grp = grphead; 1681166440Spjd while (grp) { 1682166440Spjd tgrp = grp; 1683166440Spjd grp = grp->gr_next; 1684166440Spjd free_grp(tgrp); 1685166440Spjd } 1686166440Spjd grphead = (struct grouplist *)NULL; 1687166440Spjd 1688166440Spjd /* 1689192934Srmacklem * and the old V4 root dir. 1690192934Srmacklem */ 1691192934Srmacklem bzero(&eargs, sizeof (eargs)); 1692192934Srmacklem eargs.export.ex_flags = MNT_DELEXPORT; 1693192934Srmacklem if (run_v4server > 0 && 1694192934Srmacklem nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 && 1695192934Srmacklem errno != ENOENT) 1696192934Srmacklem syslog(LOG_ERR, "Can't delete exports for V4:"); 1697192934Srmacklem 1698192934Srmacklem /* 1699192934Srmacklem * and clear flag that notes if a public fh has been exported. 1700192934Srmacklem */ 1701192934Srmacklem has_publicfh = 0; 1702192934Srmacklem 1703192934Srmacklem /* 1704166440Spjd * And delete exports that are in the kernel for all local 1705166440Spjd * filesystems. 1706166440Spjd * XXX: Should know how to handle all local exportable filesystems. 1707166440Spjd */ 1708166440Spjd num = getmntinfo(&mntbufp, MNT_NOWAIT); 1709166440Spjd 1710166440Spjd if (num > 0) { 1711166440Spjd build_iovec(&iov, &iovlen, "fstype", NULL, 0); 1712166440Spjd build_iovec(&iov, &iovlen, "fspath", NULL, 0); 1713166440Spjd build_iovec(&iov, &iovlen, "from", NULL, 0); 1714166440Spjd build_iovec(&iov, &iovlen, "update", NULL, 0); 1715166440Spjd build_iovec(&iov, &iovlen, "export", &export, sizeof(export)); 1716166440Spjd build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 1717166440Spjd } 1718166440Spjd 1719166440Spjd for (i = 0; i < num; i++) { 1720166440Spjd fsp = &mntbufp[i]; 1721166440Spjd if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) { 1722166440Spjd syslog(LOG_ERR, "getvfsbyname() failed for %s", 1723166440Spjd fsp->f_fstypename); 1724166440Spjd continue; 1725166440Spjd } 1726166440Spjd 1727166440Spjd /* 1728166440Spjd * Do not delete export for network filesystem by 1729166440Spjd * passing "export" arg to nmount(). 1730166440Spjd * It only makes sense to do this for local filesystems. 1731166440Spjd */ 1732166440Spjd if (vfc.vfc_flags & VFCF_NETWORK) 1733166440Spjd continue; 1734166440Spjd 1735166440Spjd iov[1].iov_base = fsp->f_fstypename; 1736166440Spjd iov[1].iov_len = strlen(fsp->f_fstypename) + 1; 1737166440Spjd iov[3].iov_base = fsp->f_mntonname; 1738166440Spjd iov[3].iov_len = strlen(fsp->f_mntonname) + 1; 1739166440Spjd iov[5].iov_base = fsp->f_mntfromname; 1740166440Spjd iov[5].iov_len = strlen(fsp->f_mntfromname) + 1; 1741166440Spjd 1742166440Spjd if (nmount(iov, iovlen, fsp->f_flags) < 0 && 1743166440Spjd errno != ENOENT && errno != ENOTSUP) { 1744166440Spjd syslog(LOG_ERR, 1745166440Spjd "can't delete exports for %s: %m %s", 1746166440Spjd fsp->f_mntonname, errmsg); 1747166440Spjd } 1748166440Spjd } 1749166440Spjd 1750166440Spjd if (iov != NULL) { 1751166440Spjd /* Free strings allocated by strdup() in getmntopts.c */ 1752166440Spjd free(iov[0].iov_base); /* fstype */ 1753166440Spjd free(iov[2].iov_base); /* fspath */ 1754166440Spjd free(iov[4].iov_base); /* from */ 1755166440Spjd free(iov[6].iov_base); /* update */ 1756166440Spjd free(iov[8].iov_base); /* export */ 1757166440Spjd free(iov[10].iov_base); /* errmsg */ 1758166440Spjd 1759166440Spjd /* free iov, allocated by realloc() */ 1760166440Spjd free(iov); 1761166440Spjd iovlen = 0; 1762166440Spjd } 1763166440Spjd 1764166440Spjd /* 1765166440Spjd * Read in the exports file and build the list, calling 1766166440Spjd * nmount() as we go along to push the export rules into the kernel. 1767166440Spjd */ 1768168684Spjd done = 0; 1769166440Spjd for (i = 0; exnames[i] != NULL; i++) { 1770166440Spjd if (debug) 1771166440Spjd warnx("reading exports from %s", exnames[i]); 1772166440Spjd if ((exp_file = fopen(exnames[i], "r")) == NULL) { 1773168684Spjd syslog(LOG_WARNING, "can't open %s", exnames[i]); 1774168684Spjd continue; 1775166440Spjd } 1776166440Spjd get_exportlist_one(); 1777166440Spjd fclose(exp_file); 1778168684Spjd done++; 1779166440Spjd } 1780168684Spjd if (done == 0) { 1781168684Spjd syslog(LOG_ERR, "can't open any exports file"); 1782168684Spjd exit(2); 1783168684Spjd } 1784192934Srmacklem 1785192934Srmacklem /* 1786192934Srmacklem * If there was no public fh, clear any previous one set. 1787192934Srmacklem */ 1788192934Srmacklem if (run_v4server > 0 && has_publicfh == 0) 1789192934Srmacklem (void) nfssvc(NFSSVC_NOPUBLICFH, NULL); 1790166440Spjd} 1791166440Spjd 1792166440Spjd/* 17931558Srgrimes * Allocate an export list element 17941558Srgrimes */ 17951558Srgrimesstruct exportlist * 1796216587Scharnierget_exp(void) 17971558Srgrimes{ 17981558Srgrimes struct exportlist *ep; 17991558Srgrimes 1800224003Sdelphij ep = (struct exportlist *)calloc(1, sizeof (struct exportlist)); 18011558Srgrimes if (ep == (struct exportlist *)NULL) 18021558Srgrimes out_of_mem(); 18031558Srgrimes return (ep); 18041558Srgrimes} 18051558Srgrimes 18061558Srgrimes/* 18071558Srgrimes * Allocate a group list element 18081558Srgrimes */ 18091558Srgrimesstruct grouplist * 1810216587Scharnierget_grp(void) 18111558Srgrimes{ 18121558Srgrimes struct grouplist *gp; 18131558Srgrimes 1814224003Sdelphij gp = (struct grouplist *)calloc(1, sizeof (struct grouplist)); 18151558Srgrimes if (gp == (struct grouplist *)NULL) 18161558Srgrimes out_of_mem(); 18171558Srgrimes return (gp); 18181558Srgrimes} 18191558Srgrimes 18201558Srgrimes/* 18211558Srgrimes * Clean up upon an error in get_exportlist(). 18221558Srgrimes */ 18231558Srgrimesvoid 1824216587Scharniergetexp_err(struct exportlist *ep, struct grouplist *grp) 18251558Srgrimes{ 18261558Srgrimes struct grouplist *tgrp; 18271558Srgrimes 1828100336Sjoerg if (!(opt_flags & OP_QUIET)) 1829100336Sjoerg syslog(LOG_ERR, "bad exports list line %s", line); 18301558Srgrimes if (ep && (ep->ex_flag & EX_LINKED) == 0) 18311558Srgrimes free_exp(ep); 18321558Srgrimes while (grp) { 18331558Srgrimes tgrp = grp; 18341558Srgrimes grp = grp->gr_next; 18351558Srgrimes free_grp(tgrp); 18361558Srgrimes } 18371558Srgrimes} 18381558Srgrimes 18391558Srgrimes/* 18401558Srgrimes * Search the export list for a matching fs. 18411558Srgrimes */ 18421558Srgrimesstruct exportlist * 1843216587Scharnierex_search(fsid_t *fsid) 18441558Srgrimes{ 18451558Srgrimes struct exportlist *ep; 18461558Srgrimes 18471558Srgrimes ep = exphead; 18481558Srgrimes while (ep) { 18491558Srgrimes if (ep->ex_fs.val[0] == fsid->val[0] && 18501558Srgrimes ep->ex_fs.val[1] == fsid->val[1]) 18511558Srgrimes return (ep); 18521558Srgrimes ep = ep->ex_next; 18531558Srgrimes } 18541558Srgrimes return (ep); 18551558Srgrimes} 18561558Srgrimes 18571558Srgrimes/* 18581558Srgrimes * Add a directory path to the list. 18591558Srgrimes */ 18601558Srgrimeschar * 1861216587Scharnieradd_expdir(struct dirlist **dpp, char *cp, int len) 18621558Srgrimes{ 18631558Srgrimes struct dirlist *dp; 18641558Srgrimes 18651558Srgrimes dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); 186637663Scharnier if (dp == (struct dirlist *)NULL) 186737663Scharnier out_of_mem(); 18681558Srgrimes dp->dp_left = *dpp; 18691558Srgrimes dp->dp_right = (struct dirlist *)NULL; 18701558Srgrimes dp->dp_flag = 0; 18711558Srgrimes dp->dp_hosts = (struct hostlist *)NULL; 18721558Srgrimes strcpy(dp->dp_dirp, cp); 18731558Srgrimes *dpp = dp; 18741558Srgrimes return (dp->dp_dirp); 18751558Srgrimes} 18761558Srgrimes 18771558Srgrimes/* 18781558Srgrimes * Hang the dir list element off the dirpath binary tree as required 18791558Srgrimes * and update the entry for host. 18801558Srgrimes */ 18811558Srgrimesvoid 1882216587Scharnierhang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep, 1883216587Scharnier int flags) 18841558Srgrimes{ 18851558Srgrimes struct hostlist *hp; 18861558Srgrimes struct dirlist *dp2; 18871558Srgrimes 18889336Sdfr if (flags & OP_ALLDIRS) { 18891558Srgrimes if (ep->ex_defdir) 18901558Srgrimes free((caddr_t)dp); 18911558Srgrimes else 18921558Srgrimes ep->ex_defdir = dp; 18939336Sdfr if (grp == (struct grouplist *)NULL) { 18941558Srgrimes ep->ex_defdir->dp_flag |= DP_DEFSET; 1895240902Srmacklem /* Save the default security flavors list. */ 1896240902Srmacklem ep->ex_defnumsecflavors = ep->ex_numsecflavors; 1897240902Srmacklem if (ep->ex_numsecflavors > 0) 1898240902Srmacklem memcpy(ep->ex_defsecflavors, ep->ex_secflavors, 1899240902Srmacklem sizeof(ep->ex_secflavors)); 19009336Sdfr } else while (grp) { 19011558Srgrimes hp = get_ht(); 19021558Srgrimes hp->ht_grp = grp; 19031558Srgrimes hp->ht_next = ep->ex_defdir->dp_hosts; 19041558Srgrimes ep->ex_defdir->dp_hosts = hp; 1905240902Srmacklem /* Save the security flavors list for this host set. */ 1906240902Srmacklem grp->gr_numsecflavors = ep->ex_numsecflavors; 1907240902Srmacklem if (ep->ex_numsecflavors > 0) 1908240902Srmacklem memcpy(grp->gr_secflavors, ep->ex_secflavors, 1909240902Srmacklem sizeof(ep->ex_secflavors)); 19101558Srgrimes grp = grp->gr_next; 19111558Srgrimes } 19121558Srgrimes } else { 19131558Srgrimes 19141558Srgrimes /* 191537663Scharnier * Loop through the directories adding them to the tree. 19161558Srgrimes */ 19171558Srgrimes while (dp) { 19181558Srgrimes dp2 = dp->dp_left; 1919240902Srmacklem add_dlist(&ep->ex_dirl, dp, grp, flags, ep); 19201558Srgrimes dp = dp2; 19211558Srgrimes } 19221558Srgrimes } 19231558Srgrimes} 19241558Srgrimes 19251558Srgrimes/* 19261558Srgrimes * Traverse the binary tree either updating a node that is already there 19271558Srgrimes * for the new directory or adding the new node. 19281558Srgrimes */ 19291558Srgrimesvoid 1930216587Scharnieradd_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp, 1931240902Srmacklem int flags, struct exportlist *ep) 19321558Srgrimes{ 19331558Srgrimes struct dirlist *dp; 19341558Srgrimes struct hostlist *hp; 19351558Srgrimes int cmp; 19361558Srgrimes 19371558Srgrimes dp = *dpp; 19381558Srgrimes if (dp) { 19391558Srgrimes cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 19401558Srgrimes if (cmp > 0) { 1941240902Srmacklem add_dlist(&dp->dp_left, newdp, grp, flags, ep); 19421558Srgrimes return; 19431558Srgrimes } else if (cmp < 0) { 1944240902Srmacklem add_dlist(&dp->dp_right, newdp, grp, flags, ep); 19451558Srgrimes return; 19461558Srgrimes } else 19471558Srgrimes free((caddr_t)newdp); 19481558Srgrimes } else { 19491558Srgrimes dp = newdp; 19501558Srgrimes dp->dp_left = (struct dirlist *)NULL; 19511558Srgrimes *dpp = dp; 19521558Srgrimes } 19531558Srgrimes if (grp) { 19541558Srgrimes 19551558Srgrimes /* 19561558Srgrimes * Hang all of the host(s) off of the directory point. 19571558Srgrimes */ 19581558Srgrimes do { 19591558Srgrimes hp = get_ht(); 19601558Srgrimes hp->ht_grp = grp; 19611558Srgrimes hp->ht_next = dp->dp_hosts; 19621558Srgrimes dp->dp_hosts = hp; 1963240902Srmacklem /* Save the security flavors list for this host set. */ 1964240902Srmacklem grp->gr_numsecflavors = ep->ex_numsecflavors; 1965240902Srmacklem if (ep->ex_numsecflavors > 0) 1966240902Srmacklem memcpy(grp->gr_secflavors, ep->ex_secflavors, 1967240902Srmacklem sizeof(ep->ex_secflavors)); 19681558Srgrimes grp = grp->gr_next; 19691558Srgrimes } while (grp); 19709336Sdfr } else { 19711558Srgrimes dp->dp_flag |= DP_DEFSET; 1972240902Srmacklem /* Save the default security flavors list. */ 1973240902Srmacklem ep->ex_defnumsecflavors = ep->ex_numsecflavors; 1974240902Srmacklem if (ep->ex_numsecflavors > 0) 1975240902Srmacklem memcpy(ep->ex_defsecflavors, ep->ex_secflavors, 1976240902Srmacklem sizeof(ep->ex_secflavors)); 19779336Sdfr } 19781558Srgrimes} 19791558Srgrimes 19801558Srgrimes/* 19811558Srgrimes * Search for a dirpath on the export point. 19821558Srgrimes */ 19831558Srgrimesstruct dirlist * 1984216587Scharnierdirp_search(struct dirlist *dp, char *dirp) 19851558Srgrimes{ 19861558Srgrimes int cmp; 19871558Srgrimes 19881558Srgrimes if (dp) { 198974462Salfred cmp = strcmp(dp->dp_dirp, dirp); 19901558Srgrimes if (cmp > 0) 199174462Salfred return (dirp_search(dp->dp_left, dirp)); 19921558Srgrimes else if (cmp < 0) 199374462Salfred return (dirp_search(dp->dp_right, dirp)); 19941558Srgrimes else 19951558Srgrimes return (dp); 19961558Srgrimes } 19971558Srgrimes return (dp); 19981558Srgrimes} 19991558Srgrimes 20001558Srgrimes/* 20011558Srgrimes * Scan for a host match in a directory tree. 20021558Srgrimes */ 20031558Srgrimesint 2004216587Scharnierchk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp, 2005240902Srmacklem int *hostsetp, int *numsecflavors, int **secflavorsp) 20061558Srgrimes{ 20071558Srgrimes struct hostlist *hp; 20081558Srgrimes struct grouplist *grp; 200974462Salfred struct addrinfo *ai; 20101558Srgrimes 20111558Srgrimes if (dp) { 20121558Srgrimes if (dp->dp_flag & DP_DEFSET) 20139336Sdfr *defsetp = dp->dp_flag; 20141558Srgrimes hp = dp->dp_hosts; 20151558Srgrimes while (hp) { 20161558Srgrimes grp = hp->ht_grp; 20171558Srgrimes switch (grp->gr_type) { 20181558Srgrimes case GT_HOST: 201974462Salfred ai = grp->gr_ptr.gt_addrinfo; 202074462Salfred for (; ai; ai = ai->ai_next) { 202175801Siedowse if (!sacmp(ai->ai_addr, saddr, NULL)) { 202274462Salfred *hostsetp = 202374462Salfred (hp->ht_flag | DP_HOSTSET); 2024240902Srmacklem if (numsecflavors != NULL) { 2025240902Srmacklem *numsecflavors = 2026240902Srmacklem grp->gr_numsecflavors; 2027240902Srmacklem *secflavorsp = 2028240902Srmacklem grp->gr_secflavors; 2029240902Srmacklem } 203074462Salfred return (1); 203174462Salfred } 20329336Sdfr } 203375801Siedowse break; 20341558Srgrimes case GT_NET: 203575801Siedowse if (!sacmp(saddr, (struct sockaddr *) 203675801Siedowse &grp->gr_ptr.gt_net.nt_net, 203775801Siedowse (struct sockaddr *) 203875801Siedowse &grp->gr_ptr.gt_net.nt_mask)) { 203974462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 2040240902Srmacklem if (numsecflavors != NULL) { 2041240902Srmacklem *numsecflavors = 2042240902Srmacklem grp->gr_numsecflavors; 2043240902Srmacklem *secflavorsp = 2044240902Srmacklem grp->gr_secflavors; 2045240902Srmacklem } 204674462Salfred return (1); 204774462Salfred } 204875801Siedowse break; 204975801Siedowse } 20501558Srgrimes hp = hp->ht_next; 20511558Srgrimes } 20521558Srgrimes } 20531558Srgrimes return (0); 20541558Srgrimes} 20551558Srgrimes 20561558Srgrimes/* 20571558Srgrimes * Scan tree for a host that matches the address. 20581558Srgrimes */ 20591558Srgrimesint 2060216587Scharnierscan_tree(struct dirlist *dp, struct sockaddr *saddr) 20611558Srgrimes{ 20629336Sdfr int defset, hostset; 20631558Srgrimes 20641558Srgrimes if (dp) { 20651558Srgrimes if (scan_tree(dp->dp_left, saddr)) 20661558Srgrimes return (1); 2067240902Srmacklem if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL)) 20681558Srgrimes return (1); 20691558Srgrimes if (scan_tree(dp->dp_right, saddr)) 20701558Srgrimes return (1); 20711558Srgrimes } 20721558Srgrimes return (0); 20731558Srgrimes} 20741558Srgrimes 20751558Srgrimes/* 20761558Srgrimes * Traverse the dirlist tree and free it up. 20771558Srgrimes */ 20781558Srgrimesvoid 2079216587Scharnierfree_dir(struct dirlist *dp) 20801558Srgrimes{ 20811558Srgrimes 20821558Srgrimes if (dp) { 20831558Srgrimes free_dir(dp->dp_left); 20841558Srgrimes free_dir(dp->dp_right); 20851558Srgrimes free_host(dp->dp_hosts); 20861558Srgrimes free((caddr_t)dp); 20871558Srgrimes } 20881558Srgrimes} 20891558Srgrimes 20901558Srgrimes/* 2091184588Sdfr * Parse a colon separated list of security flavors 2092184588Sdfr */ 2093184588Sdfrint 2094216587Scharnierparsesec(char *seclist, struct exportlist *ep) 2095184588Sdfr{ 2096184588Sdfr char *cp, savedc; 2097184588Sdfr int flavor; 2098184588Sdfr 2099184588Sdfr ep->ex_numsecflavors = 0; 2100184588Sdfr for (;;) { 2101184588Sdfr cp = strchr(seclist, ':'); 2102184588Sdfr if (cp) { 2103184588Sdfr savedc = *cp; 2104184588Sdfr *cp = '\0'; 2105184588Sdfr } 2106184588Sdfr 2107184588Sdfr if (!strcmp(seclist, "sys")) 2108184588Sdfr flavor = AUTH_SYS; 2109184588Sdfr else if (!strcmp(seclist, "krb5")) 2110184588Sdfr flavor = RPCSEC_GSS_KRB5; 2111184588Sdfr else if (!strcmp(seclist, "krb5i")) 2112184588Sdfr flavor = RPCSEC_GSS_KRB5I; 2113184588Sdfr else if (!strcmp(seclist, "krb5p")) 2114184588Sdfr flavor = RPCSEC_GSS_KRB5P; 2115184588Sdfr else { 2116184588Sdfr if (cp) 2117184588Sdfr *cp = savedc; 2118184588Sdfr syslog(LOG_ERR, "bad sec flavor: %s", seclist); 2119184588Sdfr return (1); 2120184588Sdfr } 2121184588Sdfr if (ep->ex_numsecflavors == MAXSECFLAVORS) { 2122184588Sdfr if (cp) 2123184588Sdfr *cp = savedc; 2124184588Sdfr syslog(LOG_ERR, "too many sec flavors: %s", seclist); 2125184588Sdfr return (1); 2126184588Sdfr } 2127184588Sdfr ep->ex_secflavors[ep->ex_numsecflavors] = flavor; 2128184588Sdfr ep->ex_numsecflavors++; 2129184588Sdfr if (cp) { 2130184588Sdfr *cp = savedc; 2131184588Sdfr seclist = cp + 1; 2132184588Sdfr } else { 2133184588Sdfr break; 2134184588Sdfr } 2135184588Sdfr } 2136184588Sdfr return (0); 2137184588Sdfr} 2138184588Sdfr 2139184588Sdfr/* 21401558Srgrimes * Parse the option string and update fields. 21411558Srgrimes * Option arguments may either be -<option>=<value> or 21421558Srgrimes * -<option> <value> 21431558Srgrimes */ 21441558Srgrimesint 2145216587Scharnierdo_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp, 2146216587Scharnier int *has_hostp, int *exflagsp, struct xucred *cr) 21471558Srgrimes{ 21481558Srgrimes char *cpoptarg, *cpoptend; 21491558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 21501558Srgrimes int allflag, usedarg; 21511558Srgrimes 215251968Salfred savedc2 = '\0'; 21531558Srgrimes cpopt = *cpp; 21541558Srgrimes cpopt++; 21551558Srgrimes cp = *endcpp; 21561558Srgrimes savedc = *cp; 21571558Srgrimes *cp = '\0'; 21581558Srgrimes while (cpopt && *cpopt) { 21591558Srgrimes allflag = 1; 21601558Srgrimes usedarg = -2; 216137663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 21621558Srgrimes *cpoptend++ = '\0'; 216337663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 21641558Srgrimes *cpoptarg++ = '\0'; 21651558Srgrimes } else { 216637663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 21671558Srgrimes *cpoptarg++ = '\0'; 21681558Srgrimes else { 21691558Srgrimes *cp = savedc; 21701558Srgrimes nextfield(&cp, &endcp); 21711558Srgrimes **endcpp = '\0'; 21721558Srgrimes if (endcp > cp && *cp != '-') { 21731558Srgrimes cpoptarg = cp; 21741558Srgrimes savedc2 = *endcp; 21751558Srgrimes *endcp = '\0'; 21761558Srgrimes usedarg = 0; 21771558Srgrimes } 21781558Srgrimes } 21791558Srgrimes } 21801558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 21811558Srgrimes *exflagsp |= MNT_EXRDONLY; 21821558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 21831558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 21841558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 21851558Srgrimes usedarg++; 21861558Srgrimes parsecred(cpoptarg, cr); 21871558Srgrimes if (allflag == 0) { 21881558Srgrimes *exflagsp |= MNT_EXPORTANON; 21891558Srgrimes opt_flags |= OP_MAPALL; 21901558Srgrimes } else 21911558Srgrimes opt_flags |= OP_MAPROOT; 21921558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 219375801Siedowse !strcmp(cpopt, "m"))) { 21941558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 219537663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 21961558Srgrimes return (1); 21971558Srgrimes } 21981558Srgrimes usedarg++; 21991558Srgrimes opt_flags |= OP_MASK; 22001558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 22011558Srgrimes !strcmp(cpopt, "n"))) { 220274462Salfred if (strchr(cpoptarg, '/') != NULL) { 220374462Salfred if (debug) 220474462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 220574462Salfred opt_flags |= OP_MASKLEN; 220674462Salfred } 22071558Srgrimes if (grp->gr_type != GT_NULL) { 220837663Scharnier syslog(LOG_ERR, "network/host conflict"); 22091558Srgrimes return (1); 22101558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 221137663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 22121558Srgrimes return (1); 22131558Srgrimes } 22141558Srgrimes grp->gr_type = GT_NET; 22151558Srgrimes *has_hostp = 1; 22161558Srgrimes usedarg++; 22171558Srgrimes opt_flags |= OP_NET; 22181558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 22191558Srgrimes opt_flags |= OP_ALLDIRS; 222027447Sdfr } else if (!strcmp(cpopt, "public")) { 222127447Sdfr *exflagsp |= MNT_EXPUBLIC; 222227447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 222327447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 222427447Sdfr opt_flags |= OP_MAPALL; 222527447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 222627447Sdfr ep->ex_indexfile = strdup(cpoptarg); 2227100336Sjoerg } else if (!strcmp(cpopt, "quiet")) { 2228100336Sjoerg opt_flags |= OP_QUIET; 2229184588Sdfr } else if (!strcmp(cpopt, "sec")) { 2230184588Sdfr if (parsesec(cpoptarg, ep)) 2231184588Sdfr return (1); 2232184588Sdfr opt_flags |= OP_SEC; 2233184588Sdfr usedarg++; 22341558Srgrimes } else { 223537663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 22361558Srgrimes return (1); 22371558Srgrimes } 22381558Srgrimes if (usedarg >= 0) { 22391558Srgrimes *endcp = savedc2; 22401558Srgrimes **endcpp = savedc; 22411558Srgrimes if (usedarg > 0) { 22421558Srgrimes *cpp = cp; 22431558Srgrimes *endcpp = endcp; 22441558Srgrimes } 22451558Srgrimes return (0); 22461558Srgrimes } 22471558Srgrimes cpopt = cpoptend; 22481558Srgrimes } 22491558Srgrimes **endcpp = savedc; 22501558Srgrimes return (0); 22511558Srgrimes} 22521558Srgrimes 22531558Srgrimes/* 22541558Srgrimes * Translate a character string to the corresponding list of network 22551558Srgrimes * addresses for a hostname. 22561558Srgrimes */ 22571558Srgrimesint 2258216587Scharnierget_host(char *cp, struct grouplist *grp, struct grouplist *tgrp) 22591558Srgrimes{ 22607401Swpaul struct grouplist *checkgrp; 226175635Siedowse struct addrinfo *ai, *tai, hints; 226274462Salfred int ecode; 226374462Salfred char host[NI_MAXHOST]; 22641558Srgrimes 226574462Salfred if (grp->gr_type != GT_NULL) { 226674462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 22671558Srgrimes return (1); 22681558Srgrimes } 226974462Salfred memset(&hints, 0, sizeof hints); 227074462Salfred hints.ai_flags = AI_CANONNAME; 227174462Salfred hints.ai_protocol = IPPROTO_UDP; 227274462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 227374462Salfred if (ecode != 0) { 227475635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 227574462Salfred return 1; 227674462Salfred } 227774462Salfred grp->gr_ptr.gt_addrinfo = ai; 227874462Salfred while (ai != NULL) { 227974462Salfred if (ai->ai_canonname == NULL) { 228074462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 2281146187Sume sizeof host, NULL, 0, NI_NUMERICHOST) != 0) 228274462Salfred strlcpy(host, "?", sizeof(host)); 228374462Salfred ai->ai_canonname = strdup(host); 228474462Salfred ai->ai_flags |= AI_CANONNAME; 228575641Siedowse } 228674462Salfred if (debug) 228775635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 228875635Siedowse /* 228975635Siedowse * Sanity check: make sure we don't already have an entry 229075635Siedowse * for this host in the grouplist. 229175635Siedowse */ 229275635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 229375635Siedowse checkgrp = checkgrp->gr_next) { 229475635Siedowse if (checkgrp->gr_type != GT_HOST) 229575635Siedowse continue; 229675635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 229775635Siedowse tai = tai->ai_next) { 229875801Siedowse if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0) 229975635Siedowse continue; 230075635Siedowse if (debug) 230175635Siedowse fprintf(stderr, 230275635Siedowse "ignoring duplicate host %s\n", 230375635Siedowse ai->ai_canonname); 230475635Siedowse grp->gr_type = GT_IGNORE; 230575635Siedowse return (0); 230675635Siedowse } 230775635Siedowse } 230874462Salfred ai = ai->ai_next; 23091558Srgrimes } 231075635Siedowse grp->gr_type = GT_HOST; 23111558Srgrimes return (0); 23121558Srgrimes} 23131558Srgrimes 23141558Srgrimes/* 23151558Srgrimes * Free up an exports list component 23161558Srgrimes */ 23171558Srgrimesvoid 2318216587Scharnierfree_exp(struct exportlist *ep) 23191558Srgrimes{ 23201558Srgrimes 23211558Srgrimes if (ep->ex_defdir) { 23221558Srgrimes free_host(ep->ex_defdir->dp_hosts); 23231558Srgrimes free((caddr_t)ep->ex_defdir); 23241558Srgrimes } 23251558Srgrimes if (ep->ex_fsdir) 23261558Srgrimes free(ep->ex_fsdir); 232727447Sdfr if (ep->ex_indexfile) 232827447Sdfr free(ep->ex_indexfile); 23291558Srgrimes free_dir(ep->ex_dirl); 23301558Srgrimes free((caddr_t)ep); 23311558Srgrimes} 23321558Srgrimes 23331558Srgrimes/* 23341558Srgrimes * Free hosts. 23351558Srgrimes */ 23361558Srgrimesvoid 2337216587Scharnierfree_host(struct hostlist *hp) 23381558Srgrimes{ 23391558Srgrimes struct hostlist *hp2; 23401558Srgrimes 23411558Srgrimes while (hp) { 23421558Srgrimes hp2 = hp; 23431558Srgrimes hp = hp->ht_next; 23441558Srgrimes free((caddr_t)hp2); 23451558Srgrimes } 23461558Srgrimes} 23471558Srgrimes 23481558Srgrimesstruct hostlist * 2349216587Scharnierget_ht(void) 23501558Srgrimes{ 23511558Srgrimes struct hostlist *hp; 23521558Srgrimes 23531558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 23541558Srgrimes if (hp == (struct hostlist *)NULL) 23551558Srgrimes out_of_mem(); 23561558Srgrimes hp->ht_next = (struct hostlist *)NULL; 23579336Sdfr hp->ht_flag = 0; 23581558Srgrimes return (hp); 23591558Srgrimes} 23601558Srgrimes 23611558Srgrimes/* 23621558Srgrimes * Out of memory, fatal 23631558Srgrimes */ 23641558Srgrimesvoid 2365216587Scharnierout_of_mem(void) 23661558Srgrimes{ 23671558Srgrimes 236837663Scharnier syslog(LOG_ERR, "out of memory"); 23691558Srgrimes exit(2); 23701558Srgrimes} 23711558Srgrimes 23721558Srgrimes/* 2373158857Srodrigc * Do the nmount() syscall with the update flag to push the export info into 23741558Srgrimes * the kernel. 23751558Srgrimes */ 23761558Srgrimesint 2377158857Srodrigcdo_mount(struct exportlist *ep, struct grouplist *grp, int exflags, 2378158857Srodrigc struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb) 23791558Srgrimes{ 238075841Siedowse struct statfs fsb1; 238174462Salfred struct addrinfo *ai; 2382192934Srmacklem struct export_args ea, *eap; 2383158857Srodrigc char errmsg[255]; 2384158857Srodrigc char *cp; 23851558Srgrimes int done; 2386158857Srodrigc char savedc; 2387158857Srodrigc struct iovec *iov; 2388184588Sdfr int i, iovlen; 2389158857Srodrigc int ret; 2390192934Srmacklem struct nfsex_args nfsea; 23911558Srgrimes 2392192934Srmacklem if (run_v4server > 0) 2393192934Srmacklem eap = &nfsea.export; 2394192934Srmacklem else 2395192934Srmacklem eap = &ea; 2396192934Srmacklem 2397158857Srodrigc cp = NULL; 2398158857Srodrigc savedc = '\0'; 2399158857Srodrigc iov = NULL; 2400158857Srodrigc iovlen = 0; 2401158857Srodrigc ret = 0; 240275801Siedowse 2403192934Srmacklem bzero(eap, sizeof (struct export_args)); 2404158857Srodrigc bzero(errmsg, sizeof(errmsg)); 2405192934Srmacklem eap->ex_flags = exflags; 2406192934Srmacklem eap->ex_anon = *anoncrp; 2407192934Srmacklem eap->ex_indexfile = ep->ex_indexfile; 240875641Siedowse if (grp->gr_type == GT_HOST) 240974462Salfred ai = grp->gr_ptr.gt_addrinfo; 241075641Siedowse else 241175641Siedowse ai = NULL; 2412192934Srmacklem eap->ex_numsecflavors = ep->ex_numsecflavors; 2413192934Srmacklem for (i = 0; i < eap->ex_numsecflavors; i++) 2414192934Srmacklem eap->ex_secflavors[i] = ep->ex_secflavors[i]; 2415192934Srmacklem if (eap->ex_numsecflavors == 0) { 2416192934Srmacklem eap->ex_numsecflavors = 1; 2417192934Srmacklem eap->ex_secflavors[0] = AUTH_SYS; 2418184588Sdfr } 24191558Srgrimes done = FALSE; 2420158857Srodrigc 2421192934Srmacklem if (v4root_phase == 0) { 2422192934Srmacklem build_iovec(&iov, &iovlen, "fstype", NULL, 0); 2423192934Srmacklem build_iovec(&iov, &iovlen, "fspath", NULL, 0); 2424192934Srmacklem build_iovec(&iov, &iovlen, "from", NULL, 0); 2425192934Srmacklem build_iovec(&iov, &iovlen, "update", NULL, 0); 2426192934Srmacklem build_iovec(&iov, &iovlen, "export", eap, 2427192934Srmacklem sizeof (struct export_args)); 2428192934Srmacklem build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 2429192934Srmacklem } 2430158857Srodrigc 24311558Srgrimes while (!done) { 24321558Srgrimes switch (grp->gr_type) { 24331558Srgrimes case GT_HOST: 243475641Siedowse if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 243574462Salfred goto skip; 2436192934Srmacklem eap->ex_addr = ai->ai_addr; 2437192934Srmacklem eap->ex_addrlen = ai->ai_addrlen; 2438192934Srmacklem eap->ex_masklen = 0; 24391558Srgrimes break; 24401558Srgrimes case GT_NET: 244175801Siedowse if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 && 244274462Salfred have_v6 == 0) 244374462Salfred goto skip; 2444192934Srmacklem eap->ex_addr = 244575801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net; 2446192934Srmacklem eap->ex_addrlen = 2447158857Srodrigc ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len; 2448192934Srmacklem eap->ex_mask = 244975801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask; 2450192934Srmacklem eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len; 24511558Srgrimes break; 245275641Siedowse case GT_DEFAULT: 2453192934Srmacklem eap->ex_addr = NULL; 2454192934Srmacklem eap->ex_addrlen = 0; 2455192934Srmacklem eap->ex_mask = NULL; 2456192934Srmacklem eap->ex_masklen = 0; 245775641Siedowse break; 24587401Swpaul case GT_IGNORE: 2459158857Srodrigc ret = 0; 2460158857Srodrigc goto error_exit; 24617401Swpaul break; 24621558Srgrimes default: 246337663Scharnier syslog(LOG_ERR, "bad grouptype"); 24641558Srgrimes if (cp) 24651558Srgrimes *cp = savedc; 2466158857Srodrigc ret = 1; 2467158857Srodrigc goto error_exit; 24681558Srgrimes }; 24691558Srgrimes 24701558Srgrimes /* 2471192934Srmacklem * For V4:, use the nfssvc() syscall, instead of mount(). 24721558Srgrimes */ 2473192934Srmacklem if (v4root_phase == 2) { 2474192934Srmacklem nfsea.fspec = v4root_dirpath; 2475192934Srmacklem if (run_v4server > 0 && 2476192934Srmacklem nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) { 2477192934Srmacklem syslog(LOG_ERR, "Exporting V4: failed"); 2478192934Srmacklem return (2); 2479158857Srodrigc } 2480192934Srmacklem } else { 2481192934Srmacklem /* 2482192934Srmacklem * XXX: 2483192934Srmacklem * Maybe I should just use the fsb->f_mntonname path 2484192934Srmacklem * instead of looping back up the dirp to the mount 2485192934Srmacklem * point?? 2486192934Srmacklem * Also, needs to know how to export all types of local 2487192934Srmacklem * exportable filesystems and not just "ufs". 2488192934Srmacklem */ 2489192934Srmacklem iov[1].iov_base = fsb->f_fstypename; /* "fstype" */ 2490192934Srmacklem iov[1].iov_len = strlen(fsb->f_fstypename) + 1; 2491192934Srmacklem iov[3].iov_base = fsb->f_mntonname; /* "fspath" */ 2492192934Srmacklem iov[3].iov_len = strlen(fsb->f_mntonname) + 1; 2493192934Srmacklem iov[5].iov_base = fsb->f_mntfromname; /* "from" */ 2494192934Srmacklem iov[5].iov_len = strlen(fsb->f_mntfromname) + 1; 2495192934Srmacklem 2496192934Srmacklem while (nmount(iov, iovlen, fsb->f_flags) < 0) { 2497192934Srmacklem if (cp) 2498192934Srmacklem *cp-- = savedc; 2499192934Srmacklem else 2500192934Srmacklem cp = dirp + dirplen - 1; 2501192934Srmacklem if (opt_flags & OP_QUIET) { 2502192934Srmacklem ret = 1; 2503192934Srmacklem goto error_exit; 2504192934Srmacklem } 2505192934Srmacklem if (errno == EPERM) { 2506192934Srmacklem if (debug) 2507239744Sdelphij warnx("can't change attributes for %s: %s", 2508239744Sdelphij dirp, errmsg); 2509192934Srmacklem syslog(LOG_ERR, 2510239744Sdelphij "can't change attributes for %s: %s", 2511239744Sdelphij dirp, errmsg); 2512192934Srmacklem ret = 1; 2513192934Srmacklem goto error_exit; 2514192934Srmacklem } 2515192934Srmacklem if (opt_flags & OP_ALLDIRS) { 2516192934Srmacklem if (errno == EINVAL) 2517192934Srmacklem syslog(LOG_ERR, 2518100336Sjoerg "-alldirs requested but %s is not a filesystem mountpoint", 2519192934Srmacklem dirp); 2520192934Srmacklem else 2521192934Srmacklem syslog(LOG_ERR, 2522192934Srmacklem "could not remount %s: %m", 2523192934Srmacklem dirp); 2524192934Srmacklem ret = 1; 2525192934Srmacklem goto error_exit; 2526192934Srmacklem } 2527192934Srmacklem /* back up over the last component */ 2528192934Srmacklem while (*cp == '/' && cp > dirp) 2529192934Srmacklem cp--; 2530192934Srmacklem while (*(cp - 1) != '/' && cp > dirp) 2531192934Srmacklem cp--; 2532192934Srmacklem if (cp == dirp) { 2533192934Srmacklem if (debug) 2534192934Srmacklem warnx("mnt unsucc"); 2535192934Srmacklem syslog(LOG_ERR, "can't export %s %s", 2536192934Srmacklem dirp, errmsg); 2537192934Srmacklem ret = 1; 2538192934Srmacklem goto error_exit; 2539192934Srmacklem } 2540192934Srmacklem savedc = *cp; 2541192934Srmacklem *cp = '\0'; 2542192934Srmacklem /* 2543192934Srmacklem * Check that we're still on the same 2544192934Srmacklem * filesystem. 2545192934Srmacklem */ 2546192934Srmacklem if (statfs(dirp, &fsb1) != 0 || 2547192934Srmacklem bcmp(&fsb1.f_fsid, &fsb->f_fsid, 2548192934Srmacklem sizeof (fsb1.f_fsid)) != 0) { 2549192934Srmacklem *cp = savedc; 2550100336Sjoerg syslog(LOG_ERR, 2551192934Srmacklem "can't export %s %s", dirp, 2552192934Srmacklem errmsg); 2553192934Srmacklem ret = 1; 2554192934Srmacklem goto error_exit; 2555192934Srmacklem } 25561558Srgrimes } 25571558Srgrimes } 2558192934Srmacklem 2559192934Srmacklem /* 2560192934Srmacklem * For the experimental server: 2561192934Srmacklem * If this is the public directory, get the file handle 2562192934Srmacklem * and load it into the kernel via the nfssvc() syscall. 2563192934Srmacklem */ 2564192934Srmacklem if (run_v4server > 0 && (exflags & MNT_EXPUBLIC) != 0) { 2565192934Srmacklem fhandle_t fh; 2566192934Srmacklem char *public_name; 2567192934Srmacklem 2568192934Srmacklem if (eap->ex_indexfile != NULL) 2569192934Srmacklem public_name = eap->ex_indexfile; 2570192934Srmacklem else 2571192934Srmacklem public_name = dirp; 2572192934Srmacklem if (getfh(public_name, &fh) < 0) 2573192934Srmacklem syslog(LOG_ERR, 2574192934Srmacklem "Can't get public fh for %s", public_name); 2575192934Srmacklem else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0) 2576192934Srmacklem syslog(LOG_ERR, 2577192934Srmacklem "Can't set public fh for %s", public_name); 2578192934Srmacklem else 2579192934Srmacklem has_publicfh = 1; 2580192934Srmacklem } 258174462Salfredskip: 258275641Siedowse if (ai != NULL) 258374462Salfred ai = ai->ai_next; 258475641Siedowse if (ai == NULL) 25851558Srgrimes done = TRUE; 25861558Srgrimes } 25871558Srgrimes if (cp) 25881558Srgrimes *cp = savedc; 2589158857Srodrigcerror_exit: 2590158857Srodrigc /* free strings allocated by strdup() in getmntopts.c */ 2591158857Srodrigc if (iov != NULL) { 2592158857Srodrigc free(iov[0].iov_base); /* fstype */ 2593158857Srodrigc free(iov[2].iov_base); /* fspath */ 2594158857Srodrigc free(iov[4].iov_base); /* from */ 2595158857Srodrigc free(iov[6].iov_base); /* update */ 2596158857Srodrigc free(iov[8].iov_base); /* export */ 2597158857Srodrigc free(iov[10].iov_base); /* errmsg */ 2598158857Srodrigc 2599158857Srodrigc /* free iov, allocated by realloc() */ 2600158857Srodrigc free(iov); 2601158857Srodrigc } 2602158857Srodrigc return (ret); 26031558Srgrimes} 26041558Srgrimes 26051558Srgrimes/* 26061558Srgrimes * Translate a net address. 260775801Siedowse * 260875801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address. 26091558Srgrimes */ 26101558Srgrimesint 2611216587Scharnierget_net(char *cp, struct netmsk *net, int maskflg) 26121558Srgrimes{ 261375861Siedowse struct netent *np = NULL; 261474462Salfred char *name, *p, *prefp; 261575801Siedowse struct sockaddr_in sin; 261675861Siedowse struct sockaddr *sa = NULL; 261774462Salfred struct addrinfo hints, *ai = NULL; 261874462Salfred char netname[NI_MAXHOST]; 261974462Salfred long preflen; 26201558Srgrimes 262175635Siedowse p = prefp = NULL; 262274462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 262374462Salfred p = strchr(cp, '/'); 262474462Salfred *p = '\0'; 262574462Salfred prefp = p + 1; 262674462Salfred } 262774462Salfred 262875861Siedowse /* 262975861Siedowse * Check for a numeric address first. We wish to avoid 263075861Siedowse * possible DNS lookups in getnetbyname(). 263175861Siedowse */ 263275861Siedowse if (isxdigit(*cp) || *cp == ':') { 263374462Salfred memset(&hints, 0, sizeof hints); 263475801Siedowse /* Ensure the mask and the network have the same family. */ 263575801Siedowse if (maskflg && (opt_flags & OP_NET)) 263675801Siedowse hints.ai_family = net->nt_net.ss_family; 263775801Siedowse else if (!maskflg && (opt_flags & OP_HAVEMASK)) 263875801Siedowse hints.ai_family = net->nt_mask.ss_family; 263975801Siedowse else 264075801Siedowse hints.ai_family = AF_UNSPEC; 264174462Salfred hints.ai_flags = AI_NUMERICHOST; 264275861Siedowse if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 264375861Siedowse sa = ai->ai_addr; 264475861Siedowse if (sa != NULL && ai->ai_family == AF_INET) { 264574462Salfred /* 264675801Siedowse * The address in `cp' is really a network address, so 264775801Siedowse * use inet_network() to re-interpret this correctly. 264875801Siedowse * e.g. "127.1" means 127.1.0.0, not 127.0.0.1. 264974462Salfred */ 265075801Siedowse bzero(&sin, sizeof sin); 265174462Salfred sin.sin_family = AF_INET; 265274462Salfred sin.sin_len = sizeof sin; 265375801Siedowse sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 265474462Salfred if (debug) 265575801Siedowse fprintf(stderr, "get_net: v4 addr %s\n", 265675801Siedowse inet_ntoa(sin.sin_addr)); 265774462Salfred sa = (struct sockaddr *)&sin; 265875861Siedowse } 265975861Siedowse } 266075861Siedowse if (sa == NULL && (np = getnetbyname(cp)) != NULL) { 266175861Siedowse bzero(&sin, sizeof sin); 266275861Siedowse sin.sin_family = AF_INET; 266375861Siedowse sin.sin_len = sizeof sin; 266475861Siedowse sin.sin_addr = inet_makeaddr(np->n_net, 0); 266575861Siedowse sa = (struct sockaddr *)&sin; 266675861Siedowse } 266775861Siedowse if (sa == NULL) 266874462Salfred goto fail; 266925318Spst 267075801Siedowse if (maskflg) { 267175801Siedowse /* The specified sockaddr is a mask. */ 267275801Siedowse if (checkmask(sa) != 0) 267375801Siedowse goto fail; 267475801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 267575801Siedowse opt_flags |= OP_HAVEMASK; 267675801Siedowse } else { 267775801Siedowse /* The specified sockaddr is a network address. */ 267875801Siedowse bcopy(sa, &net->nt_net, sa->sa_len); 267974462Salfred 268075801Siedowse /* Get a network name for the export list. */ 268175801Siedowse if (np) { 268275801Siedowse name = np->n_name; 268375801Siedowse } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 2684146187Sume NULL, 0, NI_NUMERICHOST) == 0) { 268575801Siedowse name = netname; 268675801Siedowse } else { 268775801Siedowse goto fail; 268875801Siedowse } 268975801Siedowse if ((net->nt_name = strdup(name)) == NULL) 269075801Siedowse out_of_mem(); 269175801Siedowse 269275801Siedowse /* 269375801Siedowse * Extract a mask from either a "/<masklen>" suffix, or 269475801Siedowse * from the class of an IPv4 address. 269575801Siedowse */ 269674462Salfred if (opt_flags & OP_MASKLEN) { 269774462Salfred preflen = strtol(prefp, NULL, 10); 269875801Siedowse if (preflen < 0L || preflen == LONG_MAX) 269974462Salfred goto fail; 270075801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 270175801Siedowse if (makemask(&net->nt_mask, (int)preflen) != 0) 270275801Siedowse goto fail; 270375801Siedowse opt_flags |= OP_HAVEMASK; 270474462Salfred *p = '/'; 270575801Siedowse } else if (sa->sa_family == AF_INET && 270675801Siedowse (opt_flags & OP_MASK) == 0) { 270775801Siedowse in_addr_t addr; 270874462Salfred 270975801Siedowse addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 271075801Siedowse if (IN_CLASSA(addr)) 271175801Siedowse preflen = 8; 271275801Siedowse else if (IN_CLASSB(addr)) 271375801Siedowse preflen = 16; 271475801Siedowse else if (IN_CLASSC(addr)) 271575801Siedowse preflen = 24; 271675801Siedowse else if (IN_CLASSD(addr)) 271775801Siedowse preflen = 28; 271875801Siedowse else 271975801Siedowse preflen = 32; /* XXX */ 272075801Siedowse 272175801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 272275801Siedowse makemask(&net->nt_mask, (int)preflen); 272375801Siedowse opt_flags |= OP_HAVEMASK; 272474462Salfred } 272574462Salfred } 272674462Salfred 272774462Salfred if (ai) 272874462Salfred freeaddrinfo(ai); 272974462Salfred return 0; 273074462Salfred 273174462Salfredfail: 273274462Salfred if (ai) 273374462Salfred freeaddrinfo(ai); 273474462Salfred return 1; 27351558Srgrimes} 27361558Srgrimes 27371558Srgrimes/* 27381558Srgrimes * Parse out the next white space separated field 27391558Srgrimes */ 27401558Srgrimesvoid 2741216587Scharniernextfield(char **cp, char **endcp) 27421558Srgrimes{ 27431558Srgrimes char *p; 27441558Srgrimes 27451558Srgrimes p = *cp; 27461558Srgrimes while (*p == ' ' || *p == '\t') 27471558Srgrimes p++; 27481558Srgrimes if (*p == '\n' || *p == '\0') 27491558Srgrimes *cp = *endcp = p; 27501558Srgrimes else { 27511558Srgrimes *cp = p++; 27521558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 27531558Srgrimes p++; 27541558Srgrimes *endcp = p; 27551558Srgrimes } 27561558Srgrimes} 27571558Srgrimes 27581558Srgrimes/* 27591558Srgrimes * Get an exports file line. Skip over blank lines and handle line 27601558Srgrimes * continuations. 27611558Srgrimes */ 27621558Srgrimesint 2763216587Scharnierget_line(void) 27641558Srgrimes{ 27651558Srgrimes char *p, *cp; 276696622Siedowse size_t len; 27671558Srgrimes int totlen, cont_line; 27681558Srgrimes 27691558Srgrimes /* 27701558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 27711558Srgrimes */ 27721558Srgrimes p = line; 27731558Srgrimes totlen = 0; 27741558Srgrimes do { 277596622Siedowse if ((p = fgetln(exp_file, &len)) == NULL) 27761558Srgrimes return (0); 27771558Srgrimes cp = p + len - 1; 27781558Srgrimes cont_line = 0; 27791558Srgrimes while (cp >= p && 27801558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 27811558Srgrimes if (*cp == '\\') 27821558Srgrimes cont_line = 1; 27831558Srgrimes cp--; 27841558Srgrimes len--; 27851558Srgrimes } 278679117Sdd if (cont_line) { 278779117Sdd *++cp = ' '; 278879117Sdd len++; 278979117Sdd } 279096622Siedowse if (linesize < len + totlen + 1) { 279196622Siedowse linesize = len + totlen + 1; 279296622Siedowse line = realloc(line, linesize); 279396622Siedowse if (line == NULL) 279496622Siedowse out_of_mem(); 27951558Srgrimes } 279696622Siedowse memcpy(line + totlen, p, len); 279796622Siedowse totlen += len; 279896622Siedowse line[totlen] = '\0'; 27991558Srgrimes } while (totlen == 0 || cont_line); 28001558Srgrimes return (1); 28011558Srgrimes} 28021558Srgrimes 28031558Srgrimes/* 28041558Srgrimes * Parse a description of a credential. 28051558Srgrimes */ 28061558Srgrimesvoid 2807216587Scharnierparsecred(char *namelist, struct xucred *cr) 28081558Srgrimes{ 28091558Srgrimes char *name; 28101558Srgrimes int cnt; 28111558Srgrimes char *names; 28121558Srgrimes struct passwd *pw; 28131558Srgrimes struct group *gr; 2814194498Sbrooks gid_t groups[XU_NGROUPS + 1]; 2815136051Sstefanf int ngroups; 28161558Srgrimes 281791354Sdd cr->cr_version = XUCRED_VERSION; 28181558Srgrimes /* 281937663Scharnier * Set up the unprivileged user. 28201558Srgrimes */ 28211558Srgrimes cr->cr_uid = -2; 28221558Srgrimes cr->cr_groups[0] = -2; 28231558Srgrimes cr->cr_ngroups = 1; 28241558Srgrimes /* 28251558Srgrimes * Get the user's password table entry. 28261558Srgrimes */ 28271558Srgrimes names = strsep(&namelist, " \t\n"); 28281558Srgrimes name = strsep(&names, ":"); 28291558Srgrimes if (isdigit(*name) || *name == '-') 28301558Srgrimes pw = getpwuid(atoi(name)); 28311558Srgrimes else 28321558Srgrimes pw = getpwnam(name); 28331558Srgrimes /* 28341558Srgrimes * Credentials specified as those of a user. 28351558Srgrimes */ 28361558Srgrimes if (names == NULL) { 28371558Srgrimes if (pw == NULL) { 283837663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 28391558Srgrimes return; 28401558Srgrimes } 28411558Srgrimes cr->cr_uid = pw->pw_uid; 2842194498Sbrooks ngroups = XU_NGROUPS + 1; 28431558Srgrimes if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 284437663Scharnier syslog(LOG_ERR, "too many groups"); 28451558Srgrimes /* 2846136051Sstefanf * Compress out duplicate. 28471558Srgrimes */ 28481558Srgrimes cr->cr_ngroups = ngroups - 1; 28491558Srgrimes cr->cr_groups[0] = groups[0]; 28501558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 28511558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 28521558Srgrimes return; 28531558Srgrimes } 28541558Srgrimes /* 28551558Srgrimes * Explicit credential specified as a colon separated list: 28561558Srgrimes * uid:gid:gid:... 28571558Srgrimes */ 28581558Srgrimes if (pw != NULL) 28591558Srgrimes cr->cr_uid = pw->pw_uid; 28601558Srgrimes else if (isdigit(*name) || *name == '-') 28611558Srgrimes cr->cr_uid = atoi(name); 28621558Srgrimes else { 286337663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 28641558Srgrimes return; 28651558Srgrimes } 28661558Srgrimes cr->cr_ngroups = 0; 2867194498Sbrooks while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) { 28681558Srgrimes name = strsep(&names, ":"); 28691558Srgrimes if (isdigit(*name) || *name == '-') { 28701558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 28711558Srgrimes } else { 28721558Srgrimes if ((gr = getgrnam(name)) == NULL) { 287337663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 28741558Srgrimes continue; 28751558Srgrimes } 28761558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 28771558Srgrimes } 28781558Srgrimes } 2879194498Sbrooks if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS) 288037663Scharnier syslog(LOG_ERR, "too many groups"); 28811558Srgrimes} 28821558Srgrimes 2883194880Sdfr#define STRSIZ (MNTNAMLEN+MNTPATHLEN+50) 28841558Srgrimes/* 28851558Srgrimes * Routines that maintain the remote mounttab 28861558Srgrimes */ 28871558Srgrimesvoid 2888216587Scharnierget_mountlist(void) 28891558Srgrimes{ 28901558Srgrimes struct mountlist *mlp, **mlpp; 289123681Speter char *host, *dirp, *cp; 28921558Srgrimes char str[STRSIZ]; 28931558Srgrimes FILE *mlfile; 28941558Srgrimes 28951558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 289653117Sbillf if (errno == ENOENT) 289753117Sbillf return; 289853117Sbillf else { 289953117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 290053117Sbillf return; 290153117Sbillf } 29021558Srgrimes } 29031558Srgrimes mlpp = &mlhead; 29041558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 290523681Speter cp = str; 290623681Speter host = strsep(&cp, " \t\n"); 290723681Speter dirp = strsep(&cp, " \t\n"); 290823681Speter if (host == NULL || dirp == NULL) 29091558Srgrimes continue; 29101558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 291137663Scharnier if (mlp == (struct mountlist *)NULL) 291237663Scharnier out_of_mem(); 2913194880Sdfr strncpy(mlp->ml_host, host, MNTNAMLEN); 2914194880Sdfr mlp->ml_host[MNTNAMLEN] = '\0'; 2915194880Sdfr strncpy(mlp->ml_dirp, dirp, MNTPATHLEN); 2916194880Sdfr mlp->ml_dirp[MNTPATHLEN] = '\0'; 29171558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 29181558Srgrimes *mlpp = mlp; 29191558Srgrimes mlpp = &mlp->ml_next; 29201558Srgrimes } 29211558Srgrimes fclose(mlfile); 29221558Srgrimes} 29231558Srgrimes 292475635Siedowsevoid 292575635Siedowsedel_mlist(char *hostp, char *dirp) 29261558Srgrimes{ 29271558Srgrimes struct mountlist *mlp, **mlpp; 29281558Srgrimes struct mountlist *mlp2; 29291558Srgrimes FILE *mlfile; 29301558Srgrimes int fnd = 0; 29311558Srgrimes 29321558Srgrimes mlpp = &mlhead; 29331558Srgrimes mlp = mlhead; 29341558Srgrimes while (mlp) { 29351558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 29361558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 29371558Srgrimes fnd = 1; 29381558Srgrimes mlp2 = mlp; 29391558Srgrimes *mlpp = mlp = mlp->ml_next; 29401558Srgrimes free((caddr_t)mlp2); 29411558Srgrimes } else { 29421558Srgrimes mlpp = &mlp->ml_next; 29431558Srgrimes mlp = mlp->ml_next; 29441558Srgrimes } 29451558Srgrimes } 29461558Srgrimes if (fnd) { 29471558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 294837663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 29491558Srgrimes return; 29501558Srgrimes } 29511558Srgrimes mlp = mlhead; 29521558Srgrimes while (mlp) { 29531558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 29541558Srgrimes mlp = mlp->ml_next; 29551558Srgrimes } 29561558Srgrimes fclose(mlfile); 29571558Srgrimes } 29581558Srgrimes} 29591558Srgrimes 29601558Srgrimesvoid 2961216587Scharnieradd_mlist(char *hostp, char *dirp) 29621558Srgrimes{ 29631558Srgrimes struct mountlist *mlp, **mlpp; 29641558Srgrimes FILE *mlfile; 29651558Srgrimes 29661558Srgrimes mlpp = &mlhead; 29671558Srgrimes mlp = mlhead; 29681558Srgrimes while (mlp) { 29691558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 29701558Srgrimes return; 29711558Srgrimes mlpp = &mlp->ml_next; 29721558Srgrimes mlp = mlp->ml_next; 29731558Srgrimes } 29741558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 297537663Scharnier if (mlp == (struct mountlist *)NULL) 297637663Scharnier out_of_mem(); 2977194880Sdfr strncpy(mlp->ml_host, hostp, MNTNAMLEN); 2978194880Sdfr mlp->ml_host[MNTNAMLEN] = '\0'; 2979194880Sdfr strncpy(mlp->ml_dirp, dirp, MNTPATHLEN); 2980194880Sdfr mlp->ml_dirp[MNTPATHLEN] = '\0'; 29811558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 29821558Srgrimes *mlpp = mlp; 29831558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 298437663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 29851558Srgrimes return; 29861558Srgrimes } 29871558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 29881558Srgrimes fclose(mlfile); 29891558Srgrimes} 29901558Srgrimes 29911558Srgrimes/* 29921558Srgrimes * Free up a group list. 29931558Srgrimes */ 29941558Srgrimesvoid 2995216587Scharnierfree_grp(struct grouplist *grp) 29961558Srgrimes{ 29971558Srgrimes if (grp->gr_type == GT_HOST) { 299874462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 299974462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 30001558Srgrimes } else if (grp->gr_type == GT_NET) { 30011558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 30021558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 30031558Srgrimes } 30041558Srgrimes free((caddr_t)grp); 30051558Srgrimes} 30061558Srgrimes 30071558Srgrimes#ifdef DEBUG 30081558Srgrimesvoid 30091558SrgrimesSYSLOG(int pri, const char *fmt, ...) 30101558Srgrimes{ 30111558Srgrimes va_list ap; 30121558Srgrimes 30131558Srgrimes va_start(ap, fmt); 30141558Srgrimes vfprintf(stderr, fmt, ap); 30151558Srgrimes va_end(ap); 30161558Srgrimes} 30171558Srgrimes#endif /* DEBUG */ 30181558Srgrimes 30191558Srgrimes/* 30201558Srgrimes * Check options for consistency. 30211558Srgrimes */ 30221558Srgrimesint 3023216587Scharniercheck_options(struct dirlist *dp) 30241558Srgrimes{ 30251558Srgrimes 3026192934Srmacklem if (v4root_phase == 0 && dp == NULL) 30271558Srgrimes return (1); 302883653Speter if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) { 302983653Speter syslog(LOG_ERR, "-mapall and -maproot mutually exclusive"); 30301558Srgrimes return (1); 30311558Srgrimes } 30321558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 303375801Siedowse syslog(LOG_ERR, "-mask requires -network"); 303475801Siedowse return (1); 30351558Srgrimes } 303675801Siedowse if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) { 303775801Siedowse syslog(LOG_ERR, "-network requires mask specification"); 303875801Siedowse return (1); 303975801Siedowse } 304075801Siedowse if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) { 304175801Siedowse syslog(LOG_ERR, "-mask and /masklen are mutually exclusive"); 304275801Siedowse return (1); 304375801Siedowse } 3044192934Srmacklem if (v4root_phase > 0 && 3045192934Srmacklem (opt_flags & 3046192934Srmacklem ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) { 3047192934Srmacklem syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:"); 3048192934Srmacklem return (1); 3049192934Srmacklem } 3050207689Srmacklem if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 3051207689Srmacklem syslog(LOG_ERR, "-alldirs has multiple directories"); 3052207689Srmacklem return (1); 3053207689Srmacklem } 30541558Srgrimes return (0); 30551558Srgrimes} 30561558Srgrimes 30571558Srgrimes/* 30581558Srgrimes * Check an absolute directory path for any symbolic links. Return true 30591558Srgrimes */ 30601558Srgrimesint 3061216587Scharniercheck_dirpath(char *dirp) 30621558Srgrimes{ 30631558Srgrimes char *cp; 30641558Srgrimes int ret = 1; 30651558Srgrimes struct stat sb; 30661558Srgrimes 30671558Srgrimes cp = dirp + 1; 30681558Srgrimes while (*cp && ret) { 30691558Srgrimes if (*cp == '/') { 30701558Srgrimes *cp = '\0'; 30719336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 30721558Srgrimes ret = 0; 30731558Srgrimes *cp = '/'; 30741558Srgrimes } 30751558Srgrimes cp++; 30761558Srgrimes } 30779336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 30781558Srgrimes ret = 0; 30791558Srgrimes return (ret); 30801558Srgrimes} 30819336Sdfr 308275801Siedowse/* 308375801Siedowse * Make a netmask according to the specified prefix length. The ss_family 308475801Siedowse * and other non-address fields must be initialised before calling this. 308575801Siedowse */ 308675801Siedowseint 308775801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen) 308874462Salfred{ 308975801Siedowse u_char *p; 309075801Siedowse int bits, i, len; 309174462Salfred 309275801Siedowse if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL) 309375801Siedowse return (-1); 3094103949Smike if (bitlen > len * CHAR_BIT) 309575801Siedowse return (-1); 309674462Salfred 309775801Siedowse for (i = 0; i < len; i++) { 3098103949Smike bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen; 3099219125Sru *p++ = (u_char)~0 << (CHAR_BIT - bits); 310075801Siedowse bitlen -= bits; 310174462Salfred } 310275801Siedowse return 0; 310374462Salfred} 310474462Salfred 310575801Siedowse/* 310675801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask 310775801Siedowse * is acceptable (i.e. of the form 1...10....0). 310875801Siedowse */ 310975801Siedowseint 311075801Siedowsecheckmask(struct sockaddr *sa) 311174462Salfred{ 311275801Siedowse u_char *mask; 311375801Siedowse int i, len; 311474462Salfred 311575801Siedowse if ((mask = sa_rawaddr(sa, &len)) == NULL) 311675801Siedowse return (-1); 311775801Siedowse 311875801Siedowse for (i = 0; i < len; i++) 311975801Siedowse if (mask[i] != 0xff) 312075801Siedowse break; 312175801Siedowse if (i < len) { 312275801Siedowse if (~mask[i] & (u_char)(~mask[i] + 1)) 312375801Siedowse return (-1); 312475801Siedowse i++; 312574462Salfred } 312675801Siedowse for (; i < len; i++) 312775801Siedowse if (mask[i] != 0) 312875801Siedowse return (-1); 312975801Siedowse return (0); 313074462Salfred} 313174462Salfred 313275801Siedowse/* 313375801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if 313475801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'. 3135228990Suqs * If samask is NULL, perform a full comparison. 313675801Siedowse */ 313775801Siedowseint 313875801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask) 313974462Salfred{ 314075801Siedowse unsigned char *p1, *p2, *mask; 314175801Siedowse int len, i; 314274462Salfred 314375801Siedowse if (sa1->sa_family != sa2->sa_family || 314475801Siedowse (p1 = sa_rawaddr(sa1, &len)) == NULL || 314575801Siedowse (p2 = sa_rawaddr(sa2, NULL)) == NULL) 314675801Siedowse return (1); 314775801Siedowse 314875801Siedowse switch (sa1->sa_family) { 314974462Salfred case AF_INET6: 315075801Siedowse if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 315175801Siedowse ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 315275801Siedowse return (1); 315374462Salfred break; 315474462Salfred } 315574462Salfred 315675801Siedowse /* Simple binary comparison if no mask specified. */ 315775801Siedowse if (samask == NULL) 315875801Siedowse return (memcmp(p1, p2, len)); 315974462Salfred 316075801Siedowse /* Set up the mask, and do a mask-based comparison. */ 316175801Siedowse if (sa1->sa_family != samask->sa_family || 316275801Siedowse (mask = sa_rawaddr(samask, NULL)) == NULL) 316375801Siedowse return (1); 316474462Salfred 316575801Siedowse for (i = 0; i < len; i++) 316675801Siedowse if ((p1[i] & mask[i]) != (p2[i] & mask[i])) 316775801Siedowse return (1); 316875801Siedowse return (0); 316974462Salfred} 317074462Salfred 317175801Siedowse/* 317275801Siedowse * Return a pointer to the part of the sockaddr that contains the 317375801Siedowse * raw address, and set *nbytes to its length in bytes. Returns 317475801Siedowse * NULL if the address family is unknown. 317575801Siedowse */ 317675801Siedowsevoid * 317775801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) { 317875801Siedowse void *p; 317974462Salfred int len; 318074462Salfred 318175801Siedowse switch (sa->sa_family) { 318274462Salfred case AF_INET: 318375801Siedowse len = sizeof(((struct sockaddr_in *)sa)->sin_addr); 318475801Siedowse p = &((struct sockaddr_in *)sa)->sin_addr; 318574462Salfred break; 318674462Salfred case AF_INET6: 318775801Siedowse len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); 318875801Siedowse p = &((struct sockaddr_in6 *)sa)->sin6_addr; 318974462Salfred break; 319074462Salfred default: 319175801Siedowse p = NULL; 319275801Siedowse len = 0; 319374462Salfred } 319474462Salfred 319575801Siedowse if (nbytes != NULL) 319675801Siedowse *nbytes = len; 319775801Siedowse return (p); 319874462Salfred} 319974462Salfred 320075754Siedowsevoid 3201216587Scharnierhuphandler(int sig __unused) 320275754Siedowse{ 320375754Siedowse got_sighup = 1; 320475754Siedowse} 320575754Siedowse 3206216587Scharniervoid terminate(int sig __unused) 320774462Salfred{ 3208149433Spjd pidfile_remove(pfh); 3209194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS, NULL); 3210194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL); 321174462Salfred exit (0); 321274462Salfred} 3213192934Srmacklem 3214