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$"); 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; 249241568Srmacklemstatic int suspend_nfsd = 0; 250172827Smatteo 2511558Srgrimesint opt_flags; 25274462Salfredstatic int have_v6 = 1; 25374462Salfred 254192934Srmacklemint v4root_phase = 0; 255192934Srmacklemchar v4root_dirpath[PATH_MAX + 1]; 256220980Srmacklemint run_v4server = 1; 257192934Srmacklemint has_publicfh = 0; 258192934Srmacklem 259149433Spjdstruct pidfh *pfh = NULL; 26075801Siedowse/* Bits for opt_flags above */ 2611558Srgrimes#define OP_MAPROOT 0x01 2621558Srgrimes#define OP_MAPALL 0x02 26383653Speter/* 0x4 free */ 2641558Srgrimes#define OP_MASK 0x08 2651558Srgrimes#define OP_NET 0x10 2661558Srgrimes#define OP_ALLDIRS 0x40 26775801Siedowse#define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */ 268100336Sjoerg#define OP_QUIET 0x100 26974462Salfred#define OP_MASKLEN 0x200 270184588Sdfr#define OP_SEC 0x400 2711558Srgrimes 2721558Srgrimes#ifdef DEBUG 2731558Srgrimesint debug = 1; 27492882Simpvoid SYSLOG(int, const char *, ...) __printflike(2, 3); 2751558Srgrimes#define syslog SYSLOG 2761558Srgrimes#else 2771558Srgrimesint debug = 0; 2781558Srgrimes#endif 2791558Srgrimes 2801558Srgrimes/* 2811558Srgrimes * Mountd server for NFS mount protocol as described in: 2821558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A 2831558Srgrimes * The optional arguments are the exports file name 2841558Srgrimes * default: _PATH_EXPORTS 2851558Srgrimes * and "-n" to allow nonroot mount. 2861558Srgrimes */ 2871558Srgrimesint 288216587Scharniermain(int argc, char **argv) 2891558Srgrimes{ 29075754Siedowse fd_set readfds; 291172827Smatteo struct netconfig *nconf; 292172827Smatteo char *endptr, **hosts_bak; 293172827Smatteo void *nc_handle; 294149433Spjd pid_t otherpid; 295172827Smatteo in_port_t svcport; 296172827Smatteo int c, k, s; 297109363Smbr int maxrec = RPC_MAXDATASIZE; 298222623Srmacklem int attempt_cnt, port_len, port_pos, ret; 299222623Srmacklem char **port_list; 3001558Srgrimes 30174462Salfred /* Check that another mountd isn't already running. */ 302150214Spjd pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid); 303149433Spjd if (pfh == NULL) { 304149433Spjd if (errno == EEXIST) 305149433Spjd errx(1, "mountd already running, pid: %d.", otherpid); 306149433Spjd warn("cannot open or create pidfile"); 307149433Spjd } 30874462Salfred 30974462Salfred s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 31074462Salfred if (s < 0) 31174462Salfred have_v6 = 0; 31274462Salfred else 31374462Salfred close(s); 3142999Swollman 315241568Srmacklem while ((c = getopt(argc, argv, "2deh:lnop:rS")) != -1) 3161558Srgrimes switch (c) { 31725087Sdfr case '2': 31825087Sdfr force_v2 = 1; 31925087Sdfr break; 320192993Srmacklem case 'e': 321220980Srmacklem /* now a no-op, since this is the default */ 322192934Srmacklem break; 3239336Sdfr case 'n': 3249336Sdfr resvport_only = 0; 3259336Sdfr break; 3269336Sdfr case 'r': 3279336Sdfr dir_only = 0; 3289336Sdfr break; 3298688Sphk case 'd': 3308688Sphk debug = debug ? 0 : 1; 3318688Sphk break; 33231656Sguido case 'l': 333121767Speter dolog = 1; 33431656Sguido break; 335220980Srmacklem case 'o': 336220980Srmacklem run_v4server = 0; 337220980Srmacklem break; 338126572Sbms case 'p': 339126572Sbms endptr = NULL; 340126572Sbms svcport = (in_port_t)strtoul(optarg, &endptr, 10); 341126572Sbms if (endptr == NULL || *endptr != '\0' || 342126572Sbms svcport == 0 || svcport >= IPPORT_MAX) 343126572Sbms usage(); 344172827Smatteo svcport_str = strdup(optarg); 345126572Sbms break; 346172827Smatteo case 'h': 347172827Smatteo ++nhosts; 348172827Smatteo hosts_bak = hosts; 349172827Smatteo hosts_bak = realloc(hosts, nhosts * sizeof(char *)); 350172827Smatteo if (hosts_bak == NULL) { 351172827Smatteo if (hosts != NULL) { 352172827Smatteo for (k = 0; k < nhosts; k++) 353172827Smatteo free(hosts[k]); 354172827Smatteo free(hosts); 355172827Smatteo out_of_mem(); 356172827Smatteo } 357172827Smatteo } 358172827Smatteo hosts = hosts_bak; 359172827Smatteo hosts[nhosts - 1] = strdup(optarg); 360172827Smatteo if (hosts[nhosts - 1] == NULL) { 361172827Smatteo for (k = 0; k < (nhosts - 1); k++) 362172827Smatteo free(hosts[k]); 363172827Smatteo free(hosts); 364172827Smatteo out_of_mem(); 365172827Smatteo } 366172827Smatteo break; 367241568Srmacklem case 'S': 368241568Srmacklem suspend_nfsd = 1; 369241568Srmacklem break; 3701558Srgrimes default: 37137663Scharnier usage(); 3721558Srgrimes }; 373192934Srmacklem 374192934Srmacklem /* 375220980Srmacklem * Unless the "-o" option was specified, try and run "nfsd". 376220980Srmacklem * If "-o" was specified, try and run "nfsserver". 377192934Srmacklem */ 378192934Srmacklem if (run_v4server > 0) { 379192934Srmacklem if (modfind("nfsd") < 0) { 380192934Srmacklem /* Not present in kernel, try loading it */ 381192934Srmacklem if (kldload("nfsd") < 0 || modfind("nfsd") < 0) 382192934Srmacklem errx(1, "NFS server is not available"); 383192934Srmacklem } 384192934Srmacklem } else if (modfind("nfsserver") < 0) { 385192934Srmacklem /* Not present in kernel, try loading it */ 386192934Srmacklem if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0) 387192934Srmacklem errx(1, "NFS server is not available"); 388192934Srmacklem } 389192934Srmacklem 3901558Srgrimes argc -= optind; 3911558Srgrimes argv += optind; 3921558Srgrimes grphead = (struct grouplist *)NULL; 3931558Srgrimes exphead = (struct exportlist *)NULL; 3941558Srgrimes mlhead = (struct mountlist *)NULL; 395166440Spjd if (argc > 0) 396166440Spjd exnames = argv; 397166440Spjd else 398166440Spjd exnames = exnames_default; 3991558Srgrimes openlog("mountd", LOG_PID, LOG_DAEMON); 4001558Srgrimes if (debug) 40137663Scharnier warnx("getting export list"); 4021558Srgrimes get_exportlist(); 4031558Srgrimes if (debug) 40437663Scharnier warnx("getting mount list"); 4051558Srgrimes get_mountlist(); 4061558Srgrimes if (debug) 40737663Scharnier warnx("here we go"); 4081558Srgrimes if (debug == 0) { 4091558Srgrimes daemon(0, 0); 4101558Srgrimes signal(SIGINT, SIG_IGN); 4111558Srgrimes signal(SIGQUIT, SIG_IGN); 4121558Srgrimes } 41375754Siedowse signal(SIGHUP, huphandler); 41474462Salfred signal(SIGTERM, terminate); 415164394Srodrigc signal(SIGPIPE, SIG_IGN); 416149433Spjd 417149433Spjd pidfile_write(pfh); 418149433Spjd 419194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS, NULL); 420194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL); 421109363Smbr rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); 422109363Smbr 42324759Sguido if (!resvport_only) { 42483687Speter if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL, 42583687Speter &resvport_only, sizeof(resvport_only)) != 0 && 42683687Speter errno != ENOENT) { 42724759Sguido syslog(LOG_ERR, "sysctl: %m"); 42824759Sguido exit(1); 42924759Sguido } 43024330Sguido } 431126572Sbms 432172827Smatteo /* 433172827Smatteo * If no hosts were specified, add a wildcard entry to bind to 434172827Smatteo * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the 435172827Smatteo * list. 436172827Smatteo */ 437172827Smatteo if (nhosts == 0) { 438172827Smatteo hosts = malloc(sizeof(char**)); 439172827Smatteo if (hosts == NULL) 440172827Smatteo out_of_mem(); 441172827Smatteo hosts[0] = "*"; 442172827Smatteo nhosts = 1; 443172827Smatteo } else { 444172827Smatteo hosts_bak = hosts; 445172827Smatteo if (have_v6) { 446172827Smatteo hosts_bak = realloc(hosts, (nhosts + 2) * 447172827Smatteo sizeof(char *)); 448172827Smatteo if (hosts_bak == NULL) { 449172827Smatteo for (k = 0; k < nhosts; k++) 450172827Smatteo free(hosts[k]); 451172827Smatteo free(hosts); 452172827Smatteo out_of_mem(); 453172827Smatteo } else 454172827Smatteo hosts = hosts_bak; 455172827Smatteo nhosts += 2; 456172827Smatteo hosts[nhosts - 2] = "::1"; 457172827Smatteo } else { 458172827Smatteo hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *)); 459172827Smatteo if (hosts_bak == NULL) { 460172827Smatteo for (k = 0; k < nhosts; k++) 461172827Smatteo free(hosts[k]); 462172827Smatteo free(hosts); 463172827Smatteo out_of_mem(); 464172827Smatteo } else { 465172827Smatteo nhosts += 1; 466172827Smatteo hosts = hosts_bak; 467126572Sbms } 468172827Smatteo } 46974462Salfred 470172827Smatteo hosts[nhosts - 1] = "127.0.0.1"; 47174462Salfred } 47274462Salfred 473222623Srmacklem attempt_cnt = 1; 474222623Srmacklem sock_fdcnt = 0; 475222623Srmacklem sock_fd = NULL; 476222623Srmacklem port_list = NULL; 477222623Srmacklem port_len = 0; 478172827Smatteo nc_handle = setnetconfig(); 479172827Smatteo while ((nconf = getnetconfig(nc_handle))) { 480172827Smatteo if (nconf->nc_flag & NC_VISIBLE) { 481172827Smatteo if (have_v6 == 0 && strcmp(nconf->nc_protofmly, 482172827Smatteo "inet6") == 0) { 483172827Smatteo /* DO NOTHING */ 484222623Srmacklem } else { 485222623Srmacklem ret = create_service(nconf); 486222623Srmacklem if (ret == 1) 487222623Srmacklem /* Ignore this call */ 488222623Srmacklem continue; 489222623Srmacklem if (ret < 0) { 490222623Srmacklem /* 491222623Srmacklem * Failed to bind port, so close off 492222623Srmacklem * all sockets created and try again 493222623Srmacklem * if the port# was dynamically 494222623Srmacklem * assigned via bind(2). 495222623Srmacklem */ 496222623Srmacklem clearout_service(); 497222623Srmacklem if (mallocd_svcport != 0 && 498222623Srmacklem attempt_cnt < GETPORT_MAXTRY) { 499222623Srmacklem free(svcport_str); 500222623Srmacklem svcport_str = NULL; 501222623Srmacklem mallocd_svcport = 0; 502222623Srmacklem } else { 503222623Srmacklem errno = EADDRINUSE; 504222623Srmacklem syslog(LOG_ERR, 505222623Srmacklem "bindresvport_sa: %m"); 506222623Srmacklem exit(1); 507222623Srmacklem } 508222623Srmacklem 509222623Srmacklem /* Start over at the first service. */ 510222623Srmacklem free(sock_fd); 511222623Srmacklem sock_fdcnt = 0; 512222623Srmacklem sock_fd = NULL; 513222623Srmacklem nc_handle = setnetconfig(); 514222623Srmacklem attempt_cnt++; 515222623Srmacklem } else if (mallocd_svcport != 0 && 516222623Srmacklem attempt_cnt == GETPORT_MAXTRY) { 517222623Srmacklem /* 518222623Srmacklem * For the last attempt, allow 519222623Srmacklem * different port #s for each nconf 520222623Srmacklem * by saving the svcport_str and 521222623Srmacklem * setting it back to NULL. 522222623Srmacklem */ 523222623Srmacklem port_list = realloc(port_list, 524222623Srmacklem (port_len + 1) * sizeof(char *)); 525222623Srmacklem if (port_list == NULL) 526222623Srmacklem out_of_mem(); 527222623Srmacklem port_list[port_len++] = svcport_str; 528222623Srmacklem svcport_str = NULL; 529222623Srmacklem mallocd_svcport = 0; 530222623Srmacklem } 531222623Srmacklem } 532222623Srmacklem } 533222623Srmacklem } 534222623Srmacklem 535222623Srmacklem /* 536222623Srmacklem * Successfully bound the ports, so call complete_service() to 537222623Srmacklem * do the rest of the setup on the service(s). 538222623Srmacklem */ 539222623Srmacklem sock_fdpos = 0; 540222623Srmacklem port_pos = 0; 541222623Srmacklem nc_handle = setnetconfig(); 542222623Srmacklem while ((nconf = getnetconfig(nc_handle))) { 543222623Srmacklem if (nconf->nc_flag & NC_VISIBLE) { 544222623Srmacklem if (have_v6 == 0 && strcmp(nconf->nc_protofmly, 545222623Srmacklem "inet6") == 0) { 546222623Srmacklem /* DO NOTHING */ 547222623Srmacklem } else if (port_list != NULL) { 548222623Srmacklem if (port_pos >= port_len) { 549222623Srmacklem syslog(LOG_ERR, "too many port#s"); 550222623Srmacklem exit(1); 551222623Srmacklem } 552222623Srmacklem complete_service(nconf, port_list[port_pos++]); 553172827Smatteo } else 554222623Srmacklem complete_service(nconf, svcport_str); 555172827Smatteo } 55674462Salfred } 557172827Smatteo endnetconfig(nc_handle); 558222623Srmacklem free(sock_fd); 559222623Srmacklem if (port_list != NULL) { 560222623Srmacklem for (port_pos = 0; port_pos < port_len; port_pos++) 561222623Srmacklem free(port_list[port_pos]); 562222623Srmacklem free(port_list); 563222623Srmacklem } 56474462Salfred 56574462Salfred if (xcreated == 0) { 56674462Salfred syslog(LOG_ERR, "could not create any services"); 5671558Srgrimes exit(1); 5681558Srgrimes } 56975754Siedowse 57075754Siedowse /* Expand svc_run() here so that we can call get_exportlist(). */ 57175754Siedowse for (;;) { 57275754Siedowse if (got_sighup) { 57375754Siedowse get_exportlist(); 57475754Siedowse got_sighup = 0; 57575754Siedowse } 57675754Siedowse readfds = svc_fdset; 57775754Siedowse switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) { 57875754Siedowse case -1: 57975754Siedowse if (errno == EINTR) 58075754Siedowse continue; 58175754Siedowse syslog(LOG_ERR, "mountd died: select: %m"); 58275754Siedowse exit(1); 58375754Siedowse case 0: 58475754Siedowse continue; 58575754Siedowse default: 58675754Siedowse svc_getreqset(&readfds); 58775754Siedowse } 58875754Siedowse } 589172827Smatteo} 590172827Smatteo 591172827Smatteo/* 592172827Smatteo * This routine creates and binds sockets on the appropriate 593222623Srmacklem * addresses. It gets called one time for each transport. 594222623Srmacklem * It returns 0 upon success, 1 for ingore the call and -1 to indicate 595222623Srmacklem * bind failed with EADDRINUSE. 596222623Srmacklem * Any file descriptors that have been created are stored in sock_fd and 597222623Srmacklem * the total count of them is maintained in sock_fdcnt. 598172827Smatteo */ 599222623Srmacklemstatic int 600172827Smatteocreate_service(struct netconfig *nconf) 601172827Smatteo{ 602172827Smatteo struct addrinfo hints, *res = NULL; 603172827Smatteo struct sockaddr_in *sin; 604172827Smatteo struct sockaddr_in6 *sin6; 605172827Smatteo struct __rpc_sockinfo si; 606172827Smatteo int aicode; 607172827Smatteo int fd; 608172827Smatteo int nhostsbak; 609172827Smatteo int one = 1; 610172827Smatteo int r; 611172827Smatteo u_int32_t host_addr[4]; /* IPv4 or IPv6 */ 612222623Srmacklem int mallocd_res; 613172827Smatteo 614172827Smatteo if ((nconf->nc_semantics != NC_TPI_CLTS) && 615172827Smatteo (nconf->nc_semantics != NC_TPI_COTS) && 616172827Smatteo (nconf->nc_semantics != NC_TPI_COTS_ORD)) 617222623Srmacklem return (1); /* not my type */ 618172827Smatteo 619172827Smatteo /* 620172827Smatteo * XXX - using RPC library internal functions. 621172827Smatteo */ 622172827Smatteo if (!__rpc_nconf2sockinfo(nconf, &si)) { 623172827Smatteo syslog(LOG_ERR, "cannot get information for %s", 624172827Smatteo nconf->nc_netid); 625222623Srmacklem return (1); 626172827Smatteo } 627172827Smatteo 628172827Smatteo /* Get mountd's address on this transport */ 629172827Smatteo memset(&hints, 0, sizeof hints); 630172827Smatteo hints.ai_flags = AI_PASSIVE; 631172827Smatteo hints.ai_family = si.si_af; 632172827Smatteo hints.ai_socktype = si.si_socktype; 633172827Smatteo hints.ai_protocol = si.si_proto; 634172827Smatteo 635172827Smatteo /* 636172827Smatteo * Bind to specific IPs if asked to 637172827Smatteo */ 638172827Smatteo nhostsbak = nhosts; 639172827Smatteo while (nhostsbak > 0) { 640172827Smatteo --nhostsbak; 641222623Srmacklem sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int)); 642222623Srmacklem if (sock_fd == NULL) 643222623Srmacklem out_of_mem(); 644222623Srmacklem sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */ 645222623Srmacklem mallocd_res = 0; 646222623Srmacklem 647172827Smatteo /* 648172827Smatteo * XXX - using RPC library internal functions. 649172827Smatteo */ 650172827Smatteo if ((fd = __rpc_nconf2fd(nconf)) < 0) { 651172827Smatteo int non_fatal = 0; 652244538Skevlo if (errno == EAFNOSUPPORT && 653172827Smatteo nconf->nc_semantics != NC_TPI_CLTS) 654172827Smatteo non_fatal = 1; 655172827Smatteo 656172827Smatteo syslog(non_fatal ? LOG_DEBUG : LOG_ERR, 657172827Smatteo "cannot create socket for %s", nconf->nc_netid); 658222623Srmacklem if (non_fatal != 0) 659222623Srmacklem continue; 660222623Srmacklem exit(1); 661172827Smatteo } 662172827Smatteo 663172827Smatteo switch (hints.ai_family) { 664172827Smatteo case AF_INET: 665172827Smatteo if (inet_pton(AF_INET, hosts[nhostsbak], 666172827Smatteo host_addr) == 1) { 667222623Srmacklem hints.ai_flags |= AI_NUMERICHOST; 668172827Smatteo } else { 669172827Smatteo /* 670172827Smatteo * Skip if we have an AF_INET6 address. 671172827Smatteo */ 672172827Smatteo if (inet_pton(AF_INET6, hosts[nhostsbak], 673172827Smatteo host_addr) == 1) { 674172827Smatteo close(fd); 675172827Smatteo continue; 676172827Smatteo } 677172827Smatteo } 678172827Smatteo break; 679172827Smatteo case AF_INET6: 680172827Smatteo if (inet_pton(AF_INET6, hosts[nhostsbak], 681172827Smatteo host_addr) == 1) { 682222623Srmacklem hints.ai_flags |= AI_NUMERICHOST; 683172827Smatteo } else { 684172827Smatteo /* 685172827Smatteo * Skip if we have an AF_INET address. 686172827Smatteo */ 687172827Smatteo if (inet_pton(AF_INET, hosts[nhostsbak], 688172827Smatteo host_addr) == 1) { 689172827Smatteo close(fd); 690172827Smatteo continue; 691172827Smatteo } 692172827Smatteo } 693172827Smatteo 694172827Smatteo /* 695172827Smatteo * We're doing host-based access checks here, so don't 696172827Smatteo * allow v4-in-v6 to confuse things. The kernel will 697172827Smatteo * disable it by default on NFS sockets too. 698172827Smatteo */ 699172827Smatteo if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, 700172827Smatteo sizeof one) < 0) { 701172827Smatteo syslog(LOG_ERR, 702172827Smatteo "can't disable v4-in-v6 on IPv6 socket"); 703172827Smatteo exit(1); 704172827Smatteo } 705172827Smatteo break; 706172827Smatteo default: 707172827Smatteo break; 708172827Smatteo } 709172827Smatteo 710172827Smatteo /* 711172827Smatteo * If no hosts were specified, just bind to INADDR_ANY 712172827Smatteo */ 713172827Smatteo if (strcmp("*", hosts[nhostsbak]) == 0) { 714172827Smatteo if (svcport_str == NULL) { 715172827Smatteo res = malloc(sizeof(struct addrinfo)); 716172827Smatteo if (res == NULL) 717172827Smatteo out_of_mem(); 718222623Srmacklem mallocd_res = 1; 719172827Smatteo res->ai_flags = hints.ai_flags; 720172827Smatteo res->ai_family = hints.ai_family; 721172827Smatteo res->ai_protocol = hints.ai_protocol; 722172827Smatteo switch (res->ai_family) { 723172827Smatteo case AF_INET: 724172827Smatteo sin = malloc(sizeof(struct sockaddr_in)); 725172827Smatteo if (sin == NULL) 726172827Smatteo out_of_mem(); 727172827Smatteo sin->sin_family = AF_INET; 728172827Smatteo sin->sin_port = htons(0); 729172827Smatteo sin->sin_addr.s_addr = htonl(INADDR_ANY); 730172827Smatteo res->ai_addr = (struct sockaddr*) sin; 731172827Smatteo res->ai_addrlen = (socklen_t) 732222623Srmacklem sizeof(struct sockaddr_in); 733172827Smatteo break; 734172827Smatteo case AF_INET6: 735172827Smatteo sin6 = malloc(sizeof(struct sockaddr_in6)); 736173056Ssimon if (sin6 == NULL) 737172827Smatteo out_of_mem(); 738172827Smatteo sin6->sin6_family = AF_INET6; 739172827Smatteo sin6->sin6_port = htons(0); 740172827Smatteo sin6->sin6_addr = in6addr_any; 741172827Smatteo res->ai_addr = (struct sockaddr*) sin6; 742172827Smatteo res->ai_addrlen = (socklen_t) 743222623Srmacklem sizeof(struct sockaddr_in6); 744222623Srmacklem break; 745172827Smatteo default: 746222623Srmacklem syslog(LOG_ERR, "bad addr fam %d", 747222623Srmacklem res->ai_family); 748222623Srmacklem exit(1); 749172827Smatteo } 750172827Smatteo } else { 751172827Smatteo if ((aicode = getaddrinfo(NULL, svcport_str, 752172827Smatteo &hints, &res)) != 0) { 753172827Smatteo syslog(LOG_ERR, 754172827Smatteo "cannot get local address for %s: %s", 755172827Smatteo nconf->nc_netid, 756172827Smatteo gai_strerror(aicode)); 757222623Srmacklem close(fd); 758172827Smatteo continue; 759172827Smatteo } 760172827Smatteo } 761172827Smatteo } else { 762172827Smatteo if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str, 763172827Smatteo &hints, &res)) != 0) { 764172827Smatteo syslog(LOG_ERR, 765172827Smatteo "cannot get local address for %s: %s", 766172827Smatteo nconf->nc_netid, gai_strerror(aicode)); 767222623Srmacklem close(fd); 768172827Smatteo continue; 769172827Smatteo } 770172827Smatteo } 771172827Smatteo 772222623Srmacklem /* Store the fd. */ 773222623Srmacklem sock_fd[sock_fdcnt - 1] = fd; 774222623Srmacklem 775222623Srmacklem /* Now, attempt the bind. */ 776172827Smatteo r = bindresvport_sa(fd, res->ai_addr); 777172827Smatteo if (r != 0) { 778222623Srmacklem if (errno == EADDRINUSE && mallocd_svcport != 0) { 779222623Srmacklem if (mallocd_res != 0) { 780222623Srmacklem free(res->ai_addr); 781222623Srmacklem free(res); 782222623Srmacklem } else 783222623Srmacklem freeaddrinfo(res); 784222623Srmacklem return (-1); 785222623Srmacklem } 786172827Smatteo syslog(LOG_ERR, "bindresvport_sa: %m"); 787172827Smatteo exit(1); 788172827Smatteo } 789172827Smatteo 790222623Srmacklem if (svcport_str == NULL) { 791222623Srmacklem svcport_str = malloc(NI_MAXSERV * sizeof(char)); 792222623Srmacklem if (svcport_str == NULL) 793222623Srmacklem out_of_mem(); 794222623Srmacklem mallocd_svcport = 1; 795222623Srmacklem 796222623Srmacklem if (getnameinfo(res->ai_addr, 797222623Srmacklem res->ai_addr->sa_len, NULL, NI_MAXHOST, 798222623Srmacklem svcport_str, NI_MAXSERV * sizeof(char), 799222623Srmacklem NI_NUMERICHOST | NI_NUMERICSERV)) 800222623Srmacklem errx(1, "Cannot get port number"); 801222623Srmacklem } 802222623Srmacklem if (mallocd_res != 0) { 803222623Srmacklem free(res->ai_addr); 804222623Srmacklem free(res); 805222623Srmacklem } else 806222623Srmacklem freeaddrinfo(res); 807222623Srmacklem res = NULL; 808222623Srmacklem } 809222623Srmacklem return (0); 810222623Srmacklem} 811222623Srmacklem 812222623Srmacklem/* 813222623Srmacklem * Called after all the create_service() calls have succeeded, to complete 814222623Srmacklem * the setup and registration. 815222623Srmacklem */ 816222623Srmacklemstatic void 817222623Srmacklemcomplete_service(struct netconfig *nconf, char *port_str) 818222623Srmacklem{ 819222623Srmacklem struct addrinfo hints, *res = NULL; 820222623Srmacklem struct __rpc_sockinfo si; 821222623Srmacklem struct netbuf servaddr; 822222623Srmacklem SVCXPRT *transp = NULL; 823222623Srmacklem int aicode, fd, nhostsbak; 824222623Srmacklem int registered = 0; 825222623Srmacklem 826222623Srmacklem if ((nconf->nc_semantics != NC_TPI_CLTS) && 827222623Srmacklem (nconf->nc_semantics != NC_TPI_COTS) && 828222623Srmacklem (nconf->nc_semantics != NC_TPI_COTS_ORD)) 829222623Srmacklem return; /* not my type */ 830222623Srmacklem 831222623Srmacklem /* 832222623Srmacklem * XXX - using RPC library internal functions. 833222623Srmacklem */ 834222623Srmacklem if (!__rpc_nconf2sockinfo(nconf, &si)) { 835222623Srmacklem syslog(LOG_ERR, "cannot get information for %s", 836222623Srmacklem nconf->nc_netid); 837222623Srmacklem return; 838222623Srmacklem } 839222623Srmacklem 840222623Srmacklem nhostsbak = nhosts; 841222623Srmacklem while (nhostsbak > 0) { 842222623Srmacklem --nhostsbak; 843222623Srmacklem if (sock_fdpos >= sock_fdcnt) { 844222623Srmacklem /* Should never happen. */ 845222623Srmacklem syslog(LOG_ERR, "Ran out of socket fd's"); 846222623Srmacklem return; 847222623Srmacklem } 848222623Srmacklem fd = sock_fd[sock_fdpos++]; 849222623Srmacklem if (fd < 0) 850222623Srmacklem continue; 851222623Srmacklem 852172827Smatteo if (nconf->nc_semantics != NC_TPI_CLTS) 853172827Smatteo listen(fd, SOMAXCONN); 854172827Smatteo 855172827Smatteo if (nconf->nc_semantics == NC_TPI_CLTS ) 856172827Smatteo transp = svc_dg_create(fd, 0, 0); 857172827Smatteo else 858172827Smatteo transp = svc_vc_create(fd, RPC_MAXDATASIZE, 859172827Smatteo RPC_MAXDATASIZE); 860172827Smatteo 861172827Smatteo if (transp != (SVCXPRT *) NULL) { 862194880Sdfr if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv, 863172827Smatteo NULL)) 864172827Smatteo syslog(LOG_ERR, 865194880Sdfr "can't register %s MOUNTVERS service", 866172827Smatteo nconf->nc_netid); 867172827Smatteo if (!force_v2) { 868194880Sdfr if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3, 869172827Smatteo mntsrv, NULL)) 870172827Smatteo syslog(LOG_ERR, 871194880Sdfr "can't register %s MOUNTVERS3 service", 872172827Smatteo nconf->nc_netid); 873172827Smatteo } 874172827Smatteo } else 875172827Smatteo syslog(LOG_WARNING, "can't create %s services", 876172827Smatteo nconf->nc_netid); 877172827Smatteo 878172827Smatteo if (registered == 0) { 879172827Smatteo registered = 1; 880172827Smatteo memset(&hints, 0, sizeof hints); 881172827Smatteo hints.ai_flags = AI_PASSIVE; 882172827Smatteo hints.ai_family = si.si_af; 883172827Smatteo hints.ai_socktype = si.si_socktype; 884172827Smatteo hints.ai_protocol = si.si_proto; 885172827Smatteo 886222623Srmacklem if ((aicode = getaddrinfo(NULL, port_str, &hints, 887172827Smatteo &res)) != 0) { 888172827Smatteo syslog(LOG_ERR, "cannot get local address: %s", 889172827Smatteo gai_strerror(aicode)); 890172827Smatteo exit(1); 891172827Smatteo } 892172827Smatteo 893172827Smatteo servaddr.buf = malloc(res->ai_addrlen); 894172827Smatteo memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen); 895172827Smatteo servaddr.len = res->ai_addrlen; 896172827Smatteo 897194880Sdfr rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr); 898194880Sdfr rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr); 899172827Smatteo 900172827Smatteo xcreated++; 901172827Smatteo freeaddrinfo(res); 902172827Smatteo } 903172827Smatteo } /* end while */ 9041558Srgrimes} 9051558Srgrimes 906222623Srmacklem/* 907222623Srmacklem * Clear out sockets after a failure to bind one of them, so that the 908222623Srmacklem * cycle of socket creation/binding can start anew. 909222623Srmacklem */ 91037663Scharnierstatic void 911222623Srmacklemclearout_service(void) 912222623Srmacklem{ 913222623Srmacklem int i; 914222623Srmacklem 915222623Srmacklem for (i = 0; i < sock_fdcnt; i++) { 916222623Srmacklem if (sock_fd[i] >= 0) { 917222623Srmacklem shutdown(sock_fd[i], SHUT_RDWR); 918222623Srmacklem close(sock_fd[i]); 919222623Srmacklem } 920222623Srmacklem } 921222623Srmacklem} 922222623Srmacklem 923222623Srmacklemstatic void 924216587Scharnierusage(void) 92537663Scharnier{ 92637663Scharnier fprintf(stderr, 927192993Srmacklem "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] " 928241568Srmacklem "[-S] [-h <bindip>] [export_file ...]\n"); 92937663Scharnier exit(1); 93037663Scharnier} 93137663Scharnier 9321558Srgrimes/* 9331558Srgrimes * The mount rpc service 9341558Srgrimes */ 9351558Srgrimesvoid 936216587Scharniermntsrv(struct svc_req *rqstp, SVCXPRT *transp) 9371558Srgrimes{ 9381558Srgrimes struct exportlist *ep; 9391558Srgrimes struct dirlist *dp; 9409336Sdfr struct fhreturn fhr; 9411558Srgrimes struct stat stb; 9421558Srgrimes struct statfs fsb; 94374462Salfred char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 94474462Salfred int lookup_failed = 1; 94574462Salfred struct sockaddr *saddr; 9469336Sdfr u_short sport; 947194880Sdfr char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN]; 94828911Sguido int bad = 0, defset, hostset; 9499336Sdfr sigset_t sighup_mask; 950240902Srmacklem int numsecflavors, *secflavorsp; 9511558Srgrimes 9529336Sdfr sigemptyset(&sighup_mask); 9539336Sdfr sigaddset(&sighup_mask, SIGHUP); 95474462Salfred saddr = svc_getrpccaller(transp)->buf; 95574462Salfred switch (saddr->sa_family) { 95674462Salfred case AF_INET6: 95775635Siedowse sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 95874462Salfred break; 95974462Salfred case AF_INET: 96075635Siedowse sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 96174462Salfred break; 96274462Salfred default: 96374462Salfred syslog(LOG_ERR, "request from unknown address family"); 96474462Salfred return; 96574462Salfred } 96674462Salfred lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, 96774462Salfred NULL, 0, 0); 96874462Salfred getnameinfo(saddr, saddr->sa_len, numerichost, 96974462Salfred sizeof numerichost, NULL, 0, NI_NUMERICHOST); 9701558Srgrimes switch (rqstp->rq_proc) { 9711558Srgrimes case NULLPROC: 972121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL)) 97337663Scharnier syslog(LOG_ERR, "can't send reply"); 9741558Srgrimes return; 975194880Sdfr case MOUNTPROC_MNT: 9769336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 97731656Sguido syslog(LOG_NOTICE, 97831656Sguido "mount request from %s from unprivileged port", 97974462Salfred numerichost); 9801558Srgrimes svcerr_weakauth(transp); 9811558Srgrimes return; 9821558Srgrimes } 983121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 98431656Sguido syslog(LOG_NOTICE, "undecodable mount request from %s", 98574462Salfred numerichost); 9861558Srgrimes svcerr_decode(transp); 9871558Srgrimes return; 9881558Srgrimes } 9891558Srgrimes 9901558Srgrimes /* 9911558Srgrimes * Get the real pathname and make sure it is a directory 9929336Sdfr * or a regular file if the -r option was specified 9939336Sdfr * and it exists. 9941558Srgrimes */ 99551968Salfred if (realpath(rpcpath, dirpath) == NULL || 9961558Srgrimes stat(dirpath, &stb) < 0 || 9979336Sdfr (!S_ISDIR(stb.st_mode) && 99874462Salfred (dir_only || !S_ISREG(stb.st_mode))) || 9991558Srgrimes statfs(dirpath, &fsb) < 0) { 10001558Srgrimes chdir("/"); /* Just in case realpath doesn't */ 100131656Sguido syslog(LOG_NOTICE, 100237663Scharnier "mount request from %s for non existent path %s", 100374462Salfred numerichost, dirpath); 10041558Srgrimes if (debug) 100537663Scharnier warnx("stat failed on %s", dirpath); 100628911Sguido bad = ENOENT; /* We will send error reply later */ 10071558Srgrimes } 10081558Srgrimes 10091558Srgrimes /* Check in the exports list */ 10109336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 10111558Srgrimes ep = ex_search(&fsb.f_fsid); 10129336Sdfr hostset = defset = 0; 1013240902Srmacklem if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset, 1014240902Srmacklem &numsecflavors, &secflavorsp) || 10151558Srgrimes ((dp = dirp_search(ep->ex_dirl, dirpath)) && 1016240902Srmacklem chk_host(dp, saddr, &defset, &hostset, &numsecflavors, 1017240902Srmacklem &secflavorsp)) || 101874462Salfred (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 101974462Salfred scan_tree(ep->ex_dirl, saddr) == 0))) { 102028911Sguido if (bad) { 1021121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 102228911Sguido (caddr_t)&bad)) 102337663Scharnier syslog(LOG_ERR, "can't send reply"); 102428911Sguido sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 102528911Sguido return; 102628911Sguido } 1027240902Srmacklem if (hostset & DP_HOSTSET) { 10289336Sdfr fhr.fhr_flag = hostset; 1029240902Srmacklem fhr.fhr_numsecflavors = numsecflavors; 1030240902Srmacklem fhr.fhr_secflavors = secflavorsp; 1031240902Srmacklem } else { 10329336Sdfr fhr.fhr_flag = defset; 1033240902Srmacklem fhr.fhr_numsecflavors = ep->ex_defnumsecflavors; 1034240902Srmacklem fhr.fhr_secflavors = ep->ex_defsecflavors; 1035240902Srmacklem } 10369336Sdfr fhr.fhr_vers = rqstp->rq_vers; 10371558Srgrimes /* Get the file handle */ 103823681Speter memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 10399336Sdfr if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 10401558Srgrimes bad = errno; 104137663Scharnier syslog(LOG_ERR, "can't get fh for %s", dirpath); 1042121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 10431558Srgrimes (caddr_t)&bad)) 104437663Scharnier syslog(LOG_ERR, "can't send reply"); 10459336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 10461558Srgrimes return; 10471558Srgrimes } 1048121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, 1049121556Speter (caddr_t)&fhr)) 105037663Scharnier syslog(LOG_ERR, "can't send reply"); 105174462Salfred if (!lookup_failed) 105274462Salfred add_mlist(host, dirpath); 10531558Srgrimes else 105474462Salfred add_mlist(numerichost, dirpath); 10551558Srgrimes if (debug) 105637663Scharnier warnx("mount successful"); 1057121767Speter if (dolog) 105831656Sguido syslog(LOG_NOTICE, 105931656Sguido "mount request succeeded from %s for %s", 106074462Salfred numerichost, dirpath); 106131656Sguido } else { 10621558Srgrimes bad = EACCES; 106331656Sguido syslog(LOG_NOTICE, 106431656Sguido "mount request denied from %s for %s", 106574462Salfred numerichost, dirpath); 106631656Sguido } 106728911Sguido 1068121556Speter if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, 1069121556Speter (caddr_t)&bad)) 107037663Scharnier syslog(LOG_ERR, "can't send reply"); 10719336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 10721558Srgrimes return; 1073194880Sdfr case MOUNTPROC_DUMP: 1074121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL)) 107537663Scharnier syslog(LOG_ERR, "can't send reply"); 1076121767Speter else if (dolog) 107731656Sguido syslog(LOG_NOTICE, 107831656Sguido "dump request succeeded from %s", 107974462Salfred numerichost); 10801558Srgrimes return; 1081194880Sdfr case MOUNTPROC_UMNT: 10829336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 108331656Sguido syslog(LOG_NOTICE, 108431656Sguido "umount request from %s from unprivileged port", 108574462Salfred numerichost); 10861558Srgrimes svcerr_weakauth(transp); 10871558Srgrimes return; 10881558Srgrimes } 1089121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 109031656Sguido syslog(LOG_NOTICE, "undecodable umount request from %s", 109174462Salfred numerichost); 10921558Srgrimes svcerr_decode(transp); 10931558Srgrimes return; 10941558Srgrimes } 109551968Salfred if (realpath(rpcpath, dirpath) == NULL) { 109651968Salfred syslog(LOG_NOTICE, "umount request from %s " 109751968Salfred "for non existent path %s", 109874462Salfred numerichost, dirpath); 109951968Salfred } 1100121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 110137663Scharnier syslog(LOG_ERR, "can't send reply"); 110274462Salfred if (!lookup_failed) 110375635Siedowse del_mlist(host, dirpath); 110475635Siedowse del_mlist(numerichost, dirpath); 1105121767Speter if (dolog) 110631656Sguido syslog(LOG_NOTICE, 110731656Sguido "umount request succeeded from %s for %s", 110874462Salfred numerichost, dirpath); 11091558Srgrimes return; 1110194880Sdfr case MOUNTPROC_UMNTALL: 11119336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 111231656Sguido syslog(LOG_NOTICE, 111331656Sguido "umountall request from %s from unprivileged port", 111474462Salfred numerichost); 11151558Srgrimes svcerr_weakauth(transp); 11161558Srgrimes return; 11171558Srgrimes } 1118121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 111937663Scharnier syslog(LOG_ERR, "can't send reply"); 112074462Salfred if (!lookup_failed) 112175635Siedowse del_mlist(host, NULL); 112275635Siedowse del_mlist(numerichost, NULL); 1123121767Speter if (dolog) 112431656Sguido syslog(LOG_NOTICE, 112531656Sguido "umountall request succeeded from %s", 112674462Salfred numerichost); 11271558Srgrimes return; 1128194880Sdfr case MOUNTPROC_EXPORT: 1129121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL)) 1130121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, 1131121556Speter (caddr_t)NULL)) 1132100117Salfred syslog(LOG_ERR, "can't send reply"); 1133121767Speter if (dolog) 113431656Sguido syslog(LOG_NOTICE, 113531656Sguido "export request succeeded from %s", 113674462Salfred numerichost); 11371558Srgrimes return; 11381558Srgrimes default: 11391558Srgrimes svcerr_noproc(transp); 11401558Srgrimes return; 11411558Srgrimes } 11421558Srgrimes} 11431558Srgrimes 11441558Srgrimes/* 11451558Srgrimes * Xdr conversion for a dirpath string 11461558Srgrimes */ 11471558Srgrimesint 1148216587Scharnierxdr_dir(XDR *xdrsp, char *dirp) 11491558Srgrimes{ 1150194880Sdfr return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); 11511558Srgrimes} 11521558Srgrimes 11531558Srgrimes/* 11549336Sdfr * Xdr routine to generate file handle reply 11551558Srgrimes */ 11561558Srgrimesint 1157216587Scharnierxdr_fhs(XDR *xdrsp, caddr_t cp) 11581558Srgrimes{ 115992806Sobrien struct fhreturn *fhrp = (struct fhreturn *)cp; 11609336Sdfr u_long ok = 0, len, auth; 1161184588Sdfr int i; 11621558Srgrimes 11631558Srgrimes if (!xdr_long(xdrsp, &ok)) 11641558Srgrimes return (0); 11659336Sdfr switch (fhrp->fhr_vers) { 11669336Sdfr case 1: 11679336Sdfr return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 11689336Sdfr case 3: 11699336Sdfr len = NFSX_V3FH; 11709336Sdfr if (!xdr_long(xdrsp, &len)) 11719336Sdfr return (0); 11729336Sdfr if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 11739336Sdfr return (0); 1174184588Sdfr if (fhrp->fhr_numsecflavors) { 1175184588Sdfr if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors)) 1176184588Sdfr return (0); 1177184588Sdfr for (i = 0; i < fhrp->fhr_numsecflavors; i++) 1178184588Sdfr if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i])) 1179184588Sdfr return (0); 1180184588Sdfr return (1); 1181184588Sdfr } else { 1182184588Sdfr auth = AUTH_SYS; 1183184588Sdfr len = 1; 1184184588Sdfr if (!xdr_long(xdrsp, &len)) 1185184588Sdfr return (0); 1186184588Sdfr return (xdr_long(xdrsp, &auth)); 1187184588Sdfr } 11889336Sdfr }; 11899336Sdfr return (0); 11901558Srgrimes} 11911558Srgrimes 11921558Srgrimesint 1193216587Scharnierxdr_mlist(XDR *xdrsp, caddr_t cp __unused) 11941558Srgrimes{ 11951558Srgrimes struct mountlist *mlp; 11961558Srgrimes int true = 1; 11971558Srgrimes int false = 0; 11981558Srgrimes char *strp; 11991558Srgrimes 12001558Srgrimes mlp = mlhead; 12011558Srgrimes while (mlp) { 12021558Srgrimes if (!xdr_bool(xdrsp, &true)) 12031558Srgrimes return (0); 12041558Srgrimes strp = &mlp->ml_host[0]; 1205194880Sdfr if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) 12061558Srgrimes return (0); 12071558Srgrimes strp = &mlp->ml_dirp[0]; 1208194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 12091558Srgrimes return (0); 12101558Srgrimes mlp = mlp->ml_next; 12111558Srgrimes } 12121558Srgrimes if (!xdr_bool(xdrsp, &false)) 12131558Srgrimes return (0); 12141558Srgrimes return (1); 12151558Srgrimes} 12161558Srgrimes 12171558Srgrimes/* 12181558Srgrimes * Xdr conversion for export list 12191558Srgrimes */ 12201558Srgrimesint 1221216587Scharnierxdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief) 12221558Srgrimes{ 12231558Srgrimes struct exportlist *ep; 12241558Srgrimes int false = 0; 12259336Sdfr int putdef; 12269336Sdfr sigset_t sighup_mask; 12271558Srgrimes 12289336Sdfr sigemptyset(&sighup_mask); 12299336Sdfr sigaddset(&sighup_mask, SIGHUP); 12309336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 12311558Srgrimes ep = exphead; 12321558Srgrimes while (ep) { 12331558Srgrimes putdef = 0; 1234100117Salfred if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, 1235100117Salfred &putdef, brief)) 12361558Srgrimes goto errout; 12371558Srgrimes if (ep->ex_defdir && putdef == 0 && 12381558Srgrimes put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 1239100117Salfred &putdef, brief)) 12401558Srgrimes goto errout; 12411558Srgrimes ep = ep->ex_next; 12421558Srgrimes } 12439336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 12441558Srgrimes if (!xdr_bool(xdrsp, &false)) 12451558Srgrimes return (0); 12461558Srgrimes return (1); 12471558Srgrimeserrout: 12489336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 12491558Srgrimes return (0); 12501558Srgrimes} 12511558Srgrimes 12521558Srgrimes/* 12531558Srgrimes * Called from xdr_explist() to traverse the tree and export the 12541558Srgrimes * directory paths. 12551558Srgrimes */ 12561558Srgrimesint 1257216587Scharnierput_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp, 1258216587Scharnier int brief) 12591558Srgrimes{ 12601558Srgrimes struct grouplist *grp; 12611558Srgrimes struct hostlist *hp; 12621558Srgrimes int true = 1; 12631558Srgrimes int false = 0; 12641558Srgrimes int gotalldir = 0; 12651558Srgrimes char *strp; 12661558Srgrimes 12671558Srgrimes if (dp) { 1268100117Salfred if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief)) 12691558Srgrimes return (1); 12701558Srgrimes if (!xdr_bool(xdrsp, &true)) 12711558Srgrimes return (1); 12721558Srgrimes strp = dp->dp_dirp; 1273194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 12741558Srgrimes return (1); 12751558Srgrimes if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 12761558Srgrimes gotalldir = 1; 12771558Srgrimes *putdefp = 1; 12781558Srgrimes } 1279100117Salfred if (brief) { 1280100117Salfred if (!xdr_bool(xdrsp, &true)) 1281100117Salfred return (1); 1282100117Salfred strp = "(...)"; 1283194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 1284100117Salfred return (1); 1285100117Salfred } else if ((dp->dp_flag & DP_DEFSET) == 0 && 12861558Srgrimes (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 12871558Srgrimes hp = dp->dp_hosts; 12881558Srgrimes while (hp) { 12891558Srgrimes grp = hp->ht_grp; 12901558Srgrimes if (grp->gr_type == GT_HOST) { 12911558Srgrimes if (!xdr_bool(xdrsp, &true)) 12921558Srgrimes return (1); 129374462Salfred strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 12948871Srgrimes if (!xdr_string(xdrsp, &strp, 1295194880Sdfr MNTNAMLEN)) 12961558Srgrimes return (1); 12971558Srgrimes } else if (grp->gr_type == GT_NET) { 12981558Srgrimes if (!xdr_bool(xdrsp, &true)) 12991558Srgrimes return (1); 13001558Srgrimes strp = grp->gr_ptr.gt_net.nt_name; 13018871Srgrimes if (!xdr_string(xdrsp, &strp, 1302194880Sdfr MNTNAMLEN)) 13031558Srgrimes return (1); 13041558Srgrimes } 13051558Srgrimes hp = hp->ht_next; 13061558Srgrimes if (gotalldir && hp == (struct hostlist *)NULL) { 13071558Srgrimes hp = adp->dp_hosts; 13081558Srgrimes gotalldir = 0; 13091558Srgrimes } 13101558Srgrimes } 13111558Srgrimes } 13121558Srgrimes if (!xdr_bool(xdrsp, &false)) 13131558Srgrimes return (1); 1314100117Salfred if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief)) 13151558Srgrimes return (1); 13161558Srgrimes } 13171558Srgrimes return (0); 13181558Srgrimes} 13191558Srgrimes 1320100117Salfredint 1321216587Scharnierxdr_explist(XDR *xdrsp, caddr_t cp) 1322100117Salfred{ 1323100117Salfred 1324100117Salfred return xdr_explist_common(xdrsp, cp, 0); 1325100117Salfred} 1326100117Salfred 1327100117Salfredint 1328216587Scharnierxdr_explist_brief(XDR *xdrsp, caddr_t cp) 1329100117Salfred{ 1330100117Salfred 1331100117Salfred return xdr_explist_common(xdrsp, cp, 1); 1332100117Salfred} 1333100117Salfred 133496622Siedowsechar *line; 133596622Siedowseint linesize; 13361558SrgrimesFILE *exp_file; 13371558Srgrimes 13381558Srgrimes/* 1339166440Spjd * Get the export list from one, currently open file 13401558Srgrimes */ 1341166440Spjdstatic void 1342216587Scharnierget_exportlist_one(void) 13431558Srgrimes{ 13441558Srgrimes struct exportlist *ep, *ep2; 13451558Srgrimes struct grouplist *grp, *tgrp; 13461558Srgrimes struct exportlist **epp; 13471558Srgrimes struct dirlist *dirhead; 1348166440Spjd struct statfs fsb; 134972650Sgreen struct xucred anon; 13501558Srgrimes char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 1351166440Spjd int len, has_host, exflags, got_nondir, dirplen, netgrp; 13521558Srgrimes 1353192934Srmacklem v4root_phase = 0; 13541558Srgrimes dirhead = (struct dirlist *)NULL; 13551558Srgrimes while (get_line()) { 13561558Srgrimes if (debug) 135737663Scharnier warnx("got line %s", line); 13581558Srgrimes cp = line; 13591558Srgrimes nextfield(&cp, &endcp); 13601558Srgrimes if (*cp == '#') 13611558Srgrimes goto nextline; 13621558Srgrimes 13631558Srgrimes /* 13641558Srgrimes * Set defaults. 13651558Srgrimes */ 13661558Srgrimes has_host = FALSE; 13671558Srgrimes anon = def_anon; 13681558Srgrimes exflags = MNT_EXPORTED; 13691558Srgrimes got_nondir = 0; 13701558Srgrimes opt_flags = 0; 13711558Srgrimes ep = (struct exportlist *)NULL; 1372192934Srmacklem dirp = NULL; 13731558Srgrimes 13741558Srgrimes /* 1375192934Srmacklem * Handle the V4 root dir. 1376192934Srmacklem */ 1377192934Srmacklem if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') { 1378192934Srmacklem /* 1379192934Srmacklem * V4: just indicates that it is the v4 root point, 1380192934Srmacklem * so skip over that and set v4root_phase. 1381192934Srmacklem */ 1382192934Srmacklem if (v4root_phase > 0) { 1383192934Srmacklem syslog(LOG_ERR, "V4:duplicate line, ignored"); 1384192934Srmacklem goto nextline; 1385192934Srmacklem } 1386192934Srmacklem v4root_phase = 1; 1387192934Srmacklem cp += 3; 1388192934Srmacklem nextfield(&cp, &endcp); 1389192934Srmacklem } 1390192934Srmacklem 1391192934Srmacklem /* 13921558Srgrimes * Create new exports list entry 13931558Srgrimes */ 13941558Srgrimes len = endcp-cp; 13951558Srgrimes tgrp = grp = get_grp(); 13961558Srgrimes while (len > 0) { 1397194880Sdfr if (len > MNTNAMLEN) { 13981558Srgrimes getexp_err(ep, tgrp); 13991558Srgrimes goto nextline; 14001558Srgrimes } 14011558Srgrimes if (*cp == '-') { 14021558Srgrimes if (ep == (struct exportlist *)NULL) { 14031558Srgrimes getexp_err(ep, tgrp); 14041558Srgrimes goto nextline; 14051558Srgrimes } 14061558Srgrimes if (debug) 140737663Scharnier warnx("doing opt %s", cp); 14081558Srgrimes got_nondir = 1; 14091558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 14101558Srgrimes &exflags, &anon)) { 14111558Srgrimes getexp_err(ep, tgrp); 14121558Srgrimes goto nextline; 14131558Srgrimes } 14141558Srgrimes } else if (*cp == '/') { 14151558Srgrimes savedc = *endcp; 14161558Srgrimes *endcp = '\0'; 1417192934Srmacklem if (v4root_phase > 1) { 1418192934Srmacklem if (dirp != NULL) { 1419192934Srmacklem syslog(LOG_ERR, "Multiple V4 dirs"); 1420192934Srmacklem getexp_err(ep, tgrp); 1421192934Srmacklem goto nextline; 1422192934Srmacklem } 1423192934Srmacklem } 14241558Srgrimes if (check_dirpath(cp) && 14251558Srgrimes statfs(cp, &fsb) >= 0) { 14261558Srgrimes if (got_nondir) { 142737663Scharnier syslog(LOG_ERR, "dirs must be first"); 14281558Srgrimes getexp_err(ep, tgrp); 14291558Srgrimes goto nextline; 14301558Srgrimes } 1431192934Srmacklem if (v4root_phase == 1) { 1432192934Srmacklem if (dirp != NULL) { 1433192934Srmacklem syslog(LOG_ERR, "Multiple V4 dirs"); 14341558Srgrimes getexp_err(ep, tgrp); 14351558Srgrimes goto nextline; 14361558Srgrimes } 1437192934Srmacklem if (strlen(v4root_dirpath) == 0) { 1438192934Srmacklem strlcpy(v4root_dirpath, cp, 1439192934Srmacklem sizeof (v4root_dirpath)); 1440192934Srmacklem } else if (strcmp(v4root_dirpath, cp) 1441192934Srmacklem != 0) { 1442192934Srmacklem syslog(LOG_ERR, 1443192934Srmacklem "different V4 dirpath %s", cp); 1444192934Srmacklem getexp_err(ep, tgrp); 1445192934Srmacklem goto nextline; 1446192934Srmacklem } 1447192934Srmacklem dirp = cp; 1448192934Srmacklem v4root_phase = 2; 1449192934Srmacklem got_nondir = 1; 1450192934Srmacklem ep = get_exp(); 14511558Srgrimes } else { 1452192934Srmacklem if (ep) { 1453192934Srmacklem if (ep->ex_fs.val[0] != 1454192934Srmacklem fsb.f_fsid.val[0] || 1455192934Srmacklem ep->ex_fs.val[1] != 1456192934Srmacklem fsb.f_fsid.val[1]) { 1457192934Srmacklem getexp_err(ep, tgrp); 1458192934Srmacklem goto nextline; 1459192934Srmacklem } 1460192934Srmacklem } else { 1461192934Srmacklem /* 1462192934Srmacklem * See if this directory is already 1463192934Srmacklem * in the list. 1464192934Srmacklem */ 1465192934Srmacklem ep = ex_search(&fsb.f_fsid); 1466192934Srmacklem if (ep == (struct exportlist *)NULL) { 1467192934Srmacklem ep = get_exp(); 1468192934Srmacklem ep->ex_fs = fsb.f_fsid; 1469192934Srmacklem ep->ex_fsdir = (char *)malloc 1470192934Srmacklem (strlen(fsb.f_mntonname) + 1); 1471192934Srmacklem if (ep->ex_fsdir) 1472192934Srmacklem strcpy(ep->ex_fsdir, 1473192934Srmacklem fsb.f_mntonname); 1474192934Srmacklem else 1475192934Srmacklem out_of_mem(); 1476192934Srmacklem if (debug) 1477192934Srmacklem warnx( 1478192934Srmacklem "making new ep fs=0x%x,0x%x", 1479192934Srmacklem fsb.f_fsid.val[0], 1480192934Srmacklem fsb.f_fsid.val[1]); 1481192934Srmacklem } else if (debug) 1482192934Srmacklem warnx("found ep fs=0x%x,0x%x", 1483192934Srmacklem fsb.f_fsid.val[0], 1484192934Srmacklem fsb.f_fsid.val[1]); 1485192934Srmacklem } 1486192934Srmacklem 14871558Srgrimes /* 1488192934Srmacklem * Add dirpath to export mount point. 14891558Srgrimes */ 1490192934Srmacklem dirp = add_expdir(&dirhead, cp, len); 1491192934Srmacklem dirplen = len; 14921558Srgrimes } 14931558Srgrimes } else { 14941558Srgrimes getexp_err(ep, tgrp); 14951558Srgrimes goto nextline; 14961558Srgrimes } 14971558Srgrimes *endcp = savedc; 14981558Srgrimes } else { 14991558Srgrimes savedc = *endcp; 15001558Srgrimes *endcp = '\0'; 15011558Srgrimes got_nondir = 1; 15021558Srgrimes if (ep == (struct exportlist *)NULL) { 15031558Srgrimes getexp_err(ep, tgrp); 15041558Srgrimes goto nextline; 15051558Srgrimes } 15061558Srgrimes 15071558Srgrimes /* 15081558Srgrimes * Get the host or netgroup. 15091558Srgrimes */ 15101558Srgrimes setnetgrent(cp); 15111558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 15121558Srgrimes do { 15131558Srgrimes if (has_host) { 15141558Srgrimes grp->gr_next = get_grp(); 15151558Srgrimes grp = grp->gr_next; 15161558Srgrimes } 15171558Srgrimes if (netgrp) { 151837003Sjoerg if (hst == 0) { 151937663Scharnier syslog(LOG_ERR, 152037663Scharnier "null hostname in netgroup %s, skipping", cp); 152137004Sjoerg grp->gr_type = GT_IGNORE; 152237003Sjoerg } else if (get_host(hst, grp, tgrp)) { 152337663Scharnier syslog(LOG_ERR, 152437663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 152529317Sjlemon grp->gr_type = GT_IGNORE; 15261558Srgrimes } 15277401Swpaul } else if (get_host(cp, grp, tgrp)) { 152837663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 152929317Sjlemon grp->gr_type = GT_IGNORE; 15301558Srgrimes } 15311558Srgrimes has_host = TRUE; 15321558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 15331558Srgrimes endnetgrent(); 15341558Srgrimes *endcp = savedc; 15351558Srgrimes } 15361558Srgrimes cp = endcp; 15371558Srgrimes nextfield(&cp, &endcp); 15381558Srgrimes len = endcp - cp; 15391558Srgrimes } 15401558Srgrimes if (check_options(dirhead)) { 15411558Srgrimes getexp_err(ep, tgrp); 15421558Srgrimes goto nextline; 15431558Srgrimes } 15441558Srgrimes if (!has_host) { 154575641Siedowse grp->gr_type = GT_DEFAULT; 15461558Srgrimes if (debug) 154737663Scharnier warnx("adding a default entry"); 15481558Srgrimes 15491558Srgrimes /* 15501558Srgrimes * Don't allow a network export coincide with a list of 15511558Srgrimes * host(s) on the same line. 15521558Srgrimes */ 15531558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 155475801Siedowse syslog(LOG_ERR, "network/host conflict"); 15551558Srgrimes getexp_err(ep, tgrp); 15561558Srgrimes goto nextline; 155729317Sjlemon 155874462Salfred /* 155974462Salfred * If an export list was specified on this line, make sure 156029317Sjlemon * that we have at least one valid entry, otherwise skip it. 156129317Sjlemon */ 156229317Sjlemon } else { 156329317Sjlemon grp = tgrp; 156474462Salfred while (grp && grp->gr_type == GT_IGNORE) 156529317Sjlemon grp = grp->gr_next; 156629317Sjlemon if (! grp) { 156729317Sjlemon getexp_err(ep, tgrp); 156829317Sjlemon goto nextline; 156929317Sjlemon } 15701558Srgrimes } 15711558Srgrimes 1572192934Srmacklem if (v4root_phase == 1) { 1573192934Srmacklem syslog(LOG_ERR, "V4:root, no dirp, ignored"); 1574192934Srmacklem getexp_err(ep, tgrp); 1575192934Srmacklem goto nextline; 1576192934Srmacklem } 1577192934Srmacklem 15781558Srgrimes /* 15791558Srgrimes * Loop through hosts, pushing the exports into the kernel. 15801558Srgrimes * After loop, tgrp points to the start of the list and 15811558Srgrimes * grp points to the last entry in the list. 15821558Srgrimes */ 15831558Srgrimes grp = tgrp; 15841558Srgrimes do { 158575635Siedowse if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 158675635Siedowse &fsb)) { 158775635Siedowse getexp_err(ep, tgrp); 158875635Siedowse goto nextline; 158975635Siedowse } 15901558Srgrimes } while (grp->gr_next && (grp = grp->gr_next)); 15911558Srgrimes 15921558Srgrimes /* 1593192934Srmacklem * For V4: don't enter in mount lists. 1594192934Srmacklem */ 1595194773Srmacklem if (v4root_phase > 0 && v4root_phase <= 2) { 1596194773Srmacklem /* 1597194773Srmacklem * Since these structures aren't used by mountd, 1598194773Srmacklem * free them up now. 1599194773Srmacklem */ 1600194773Srmacklem if (ep != NULL) 1601194773Srmacklem free_exp(ep); 1602194773Srmacklem while (tgrp != NULL) { 1603194773Srmacklem grp = tgrp; 1604194773Srmacklem tgrp = tgrp->gr_next; 1605194773Srmacklem free_grp(grp); 1606194773Srmacklem } 1607192934Srmacklem goto nextline; 1608194773Srmacklem } 1609192934Srmacklem 1610192934Srmacklem /* 16111558Srgrimes * Success. Update the data structures. 16121558Srgrimes */ 16131558Srgrimes if (has_host) { 16149336Sdfr hang_dirp(dirhead, tgrp, ep, opt_flags); 16151558Srgrimes grp->gr_next = grphead; 16161558Srgrimes grphead = tgrp; 16171558Srgrimes } else { 16181558Srgrimes hang_dirp(dirhead, (struct grouplist *)NULL, ep, 16199336Sdfr opt_flags); 16201558Srgrimes free_grp(grp); 16211558Srgrimes } 16221558Srgrimes dirhead = (struct dirlist *)NULL; 16231558Srgrimes if ((ep->ex_flag & EX_LINKED) == 0) { 16241558Srgrimes ep2 = exphead; 16251558Srgrimes epp = &exphead; 16261558Srgrimes 16271558Srgrimes /* 16281558Srgrimes * Insert in the list in alphabetical order. 16291558Srgrimes */ 16301558Srgrimes while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { 16311558Srgrimes epp = &ep2->ex_next; 16321558Srgrimes ep2 = ep2->ex_next; 16331558Srgrimes } 16341558Srgrimes if (ep2) 16351558Srgrimes ep->ex_next = ep2; 16361558Srgrimes *epp = ep; 16371558Srgrimes ep->ex_flag |= EX_LINKED; 16381558Srgrimes } 16391558Srgrimesnextline: 1640192934Srmacklem v4root_phase = 0; 16411558Srgrimes if (dirhead) { 16421558Srgrimes free_dir(dirhead); 16431558Srgrimes dirhead = (struct dirlist *)NULL; 16441558Srgrimes } 16451558Srgrimes } 16461558Srgrimes} 16471558Srgrimes 16481558Srgrimes/* 1649166440Spjd * Get the export list from all specified files 1650166440Spjd */ 1651166440Spjdvoid 1652216587Scharnierget_exportlist(void) 1653166440Spjd{ 1654166440Spjd struct exportlist *ep, *ep2; 1655166440Spjd struct grouplist *grp, *tgrp; 1656166440Spjd struct export_args export; 1657166440Spjd struct iovec *iov; 1658166440Spjd struct statfs *fsp, *mntbufp; 1659166440Spjd struct xvfsconf vfc; 1660166440Spjd char errmsg[255]; 1661230352Seadler int num, i; 1662166440Spjd int iovlen; 1663168684Spjd int done; 1664192934Srmacklem struct nfsex_args eargs; 1665166440Spjd 1666241568Srmacklem if (suspend_nfsd != 0) 1667241568Srmacklem (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL); 1668192934Srmacklem v4root_dirpath[0] = '\0'; 1669166440Spjd bzero(&export, sizeof(export)); 1670166440Spjd export.ex_flags = MNT_DELEXPORT; 1671166440Spjd iov = NULL; 1672166440Spjd iovlen = 0; 1673166440Spjd bzero(errmsg, sizeof(errmsg)); 1674166440Spjd 1675166440Spjd /* 1676166440Spjd * First, get rid of the old list 1677166440Spjd */ 1678166440Spjd ep = exphead; 1679166440Spjd while (ep) { 1680166440Spjd ep2 = ep; 1681166440Spjd ep = ep->ex_next; 1682166440Spjd free_exp(ep2); 1683166440Spjd } 1684166440Spjd exphead = (struct exportlist *)NULL; 1685166440Spjd 1686166440Spjd grp = grphead; 1687166440Spjd while (grp) { 1688166440Spjd tgrp = grp; 1689166440Spjd grp = grp->gr_next; 1690166440Spjd free_grp(tgrp); 1691166440Spjd } 1692166440Spjd grphead = (struct grouplist *)NULL; 1693166440Spjd 1694166440Spjd /* 1695192934Srmacklem * and the old V4 root dir. 1696192934Srmacklem */ 1697192934Srmacklem bzero(&eargs, sizeof (eargs)); 1698192934Srmacklem eargs.export.ex_flags = MNT_DELEXPORT; 1699192934Srmacklem if (run_v4server > 0 && 1700192934Srmacklem nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 && 1701192934Srmacklem errno != ENOENT) 1702192934Srmacklem syslog(LOG_ERR, "Can't delete exports for V4:"); 1703192934Srmacklem 1704192934Srmacklem /* 1705192934Srmacklem * and clear flag that notes if a public fh has been exported. 1706192934Srmacklem */ 1707192934Srmacklem has_publicfh = 0; 1708192934Srmacklem 1709192934Srmacklem /* 1710166440Spjd * And delete exports that are in the kernel for all local 1711166440Spjd * filesystems. 1712166440Spjd * XXX: Should know how to handle all local exportable filesystems. 1713166440Spjd */ 1714166440Spjd num = getmntinfo(&mntbufp, MNT_NOWAIT); 1715166440Spjd 1716166440Spjd if (num > 0) { 1717166440Spjd build_iovec(&iov, &iovlen, "fstype", NULL, 0); 1718166440Spjd build_iovec(&iov, &iovlen, "fspath", NULL, 0); 1719166440Spjd build_iovec(&iov, &iovlen, "from", NULL, 0); 1720166440Spjd build_iovec(&iov, &iovlen, "update", NULL, 0); 1721166440Spjd build_iovec(&iov, &iovlen, "export", &export, sizeof(export)); 1722166440Spjd build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 1723166440Spjd } 1724166440Spjd 1725166440Spjd for (i = 0; i < num; i++) { 1726166440Spjd fsp = &mntbufp[i]; 1727166440Spjd if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) { 1728166440Spjd syslog(LOG_ERR, "getvfsbyname() failed for %s", 1729166440Spjd fsp->f_fstypename); 1730166440Spjd continue; 1731166440Spjd } 1732166440Spjd 1733166440Spjd /* 1734166440Spjd * Do not delete export for network filesystem by 1735166440Spjd * passing "export" arg to nmount(). 1736166440Spjd * It only makes sense to do this for local filesystems. 1737166440Spjd */ 1738166440Spjd if (vfc.vfc_flags & VFCF_NETWORK) 1739166440Spjd continue; 1740166440Spjd 1741166440Spjd iov[1].iov_base = fsp->f_fstypename; 1742166440Spjd iov[1].iov_len = strlen(fsp->f_fstypename) + 1; 1743166440Spjd iov[3].iov_base = fsp->f_mntonname; 1744166440Spjd iov[3].iov_len = strlen(fsp->f_mntonname) + 1; 1745166440Spjd iov[5].iov_base = fsp->f_mntfromname; 1746166440Spjd iov[5].iov_len = strlen(fsp->f_mntfromname) + 1; 1747166440Spjd 1748166440Spjd if (nmount(iov, iovlen, fsp->f_flags) < 0 && 1749166440Spjd errno != ENOENT && errno != ENOTSUP) { 1750166440Spjd syslog(LOG_ERR, 1751166440Spjd "can't delete exports for %s: %m %s", 1752166440Spjd fsp->f_mntonname, errmsg); 1753166440Spjd } 1754166440Spjd } 1755166440Spjd 1756166440Spjd if (iov != NULL) { 1757166440Spjd /* Free strings allocated by strdup() in getmntopts.c */ 1758166440Spjd free(iov[0].iov_base); /* fstype */ 1759166440Spjd free(iov[2].iov_base); /* fspath */ 1760166440Spjd free(iov[4].iov_base); /* from */ 1761166440Spjd free(iov[6].iov_base); /* update */ 1762166440Spjd free(iov[8].iov_base); /* export */ 1763166440Spjd free(iov[10].iov_base); /* errmsg */ 1764166440Spjd 1765166440Spjd /* free iov, allocated by realloc() */ 1766166440Spjd free(iov); 1767166440Spjd iovlen = 0; 1768166440Spjd } 1769166440Spjd 1770166440Spjd /* 1771166440Spjd * Read in the exports file and build the list, calling 1772166440Spjd * nmount() as we go along to push the export rules into the kernel. 1773166440Spjd */ 1774168684Spjd done = 0; 1775166440Spjd for (i = 0; exnames[i] != NULL; i++) { 1776166440Spjd if (debug) 1777166440Spjd warnx("reading exports from %s", exnames[i]); 1778166440Spjd if ((exp_file = fopen(exnames[i], "r")) == NULL) { 1779168684Spjd syslog(LOG_WARNING, "can't open %s", exnames[i]); 1780168684Spjd continue; 1781166440Spjd } 1782166440Spjd get_exportlist_one(); 1783166440Spjd fclose(exp_file); 1784168684Spjd done++; 1785166440Spjd } 1786168684Spjd if (done == 0) { 1787168684Spjd syslog(LOG_ERR, "can't open any exports file"); 1788168684Spjd exit(2); 1789168684Spjd } 1790192934Srmacklem 1791192934Srmacklem /* 1792192934Srmacklem * If there was no public fh, clear any previous one set. 1793192934Srmacklem */ 1794192934Srmacklem if (run_v4server > 0 && has_publicfh == 0) 1795192934Srmacklem (void) nfssvc(NFSSVC_NOPUBLICFH, NULL); 1796241568Srmacklem 1797241568Srmacklem /* Resume the nfsd. If they weren't suspended, this is harmless. */ 1798241568Srmacklem (void)nfssvc(NFSSVC_RESUMENFSD, NULL); 1799166440Spjd} 1800166440Spjd 1801166440Spjd/* 18021558Srgrimes * Allocate an export list element 18031558Srgrimes */ 18041558Srgrimesstruct exportlist * 1805216587Scharnierget_exp(void) 18061558Srgrimes{ 18071558Srgrimes struct exportlist *ep; 18081558Srgrimes 1809224003Sdelphij ep = (struct exportlist *)calloc(1, sizeof (struct exportlist)); 18101558Srgrimes if (ep == (struct exportlist *)NULL) 18111558Srgrimes out_of_mem(); 18121558Srgrimes return (ep); 18131558Srgrimes} 18141558Srgrimes 18151558Srgrimes/* 18161558Srgrimes * Allocate a group list element 18171558Srgrimes */ 18181558Srgrimesstruct grouplist * 1819216587Scharnierget_grp(void) 18201558Srgrimes{ 18211558Srgrimes struct grouplist *gp; 18221558Srgrimes 1823224003Sdelphij gp = (struct grouplist *)calloc(1, sizeof (struct grouplist)); 18241558Srgrimes if (gp == (struct grouplist *)NULL) 18251558Srgrimes out_of_mem(); 18261558Srgrimes return (gp); 18271558Srgrimes} 18281558Srgrimes 18291558Srgrimes/* 18301558Srgrimes * Clean up upon an error in get_exportlist(). 18311558Srgrimes */ 18321558Srgrimesvoid 1833216587Scharniergetexp_err(struct exportlist *ep, struct grouplist *grp) 18341558Srgrimes{ 18351558Srgrimes struct grouplist *tgrp; 18361558Srgrimes 1837100336Sjoerg if (!(opt_flags & OP_QUIET)) 1838100336Sjoerg syslog(LOG_ERR, "bad exports list line %s", line); 18391558Srgrimes if (ep && (ep->ex_flag & EX_LINKED) == 0) 18401558Srgrimes free_exp(ep); 18411558Srgrimes while (grp) { 18421558Srgrimes tgrp = grp; 18431558Srgrimes grp = grp->gr_next; 18441558Srgrimes free_grp(tgrp); 18451558Srgrimes } 18461558Srgrimes} 18471558Srgrimes 18481558Srgrimes/* 18491558Srgrimes * Search the export list for a matching fs. 18501558Srgrimes */ 18511558Srgrimesstruct exportlist * 1852216587Scharnierex_search(fsid_t *fsid) 18531558Srgrimes{ 18541558Srgrimes struct exportlist *ep; 18551558Srgrimes 18561558Srgrimes ep = exphead; 18571558Srgrimes while (ep) { 18581558Srgrimes if (ep->ex_fs.val[0] == fsid->val[0] && 18591558Srgrimes ep->ex_fs.val[1] == fsid->val[1]) 18601558Srgrimes return (ep); 18611558Srgrimes ep = ep->ex_next; 18621558Srgrimes } 18631558Srgrimes return (ep); 18641558Srgrimes} 18651558Srgrimes 18661558Srgrimes/* 18671558Srgrimes * Add a directory path to the list. 18681558Srgrimes */ 18691558Srgrimeschar * 1870216587Scharnieradd_expdir(struct dirlist **dpp, char *cp, int len) 18711558Srgrimes{ 18721558Srgrimes struct dirlist *dp; 18731558Srgrimes 18741558Srgrimes dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); 187537663Scharnier if (dp == (struct dirlist *)NULL) 187637663Scharnier out_of_mem(); 18771558Srgrimes dp->dp_left = *dpp; 18781558Srgrimes dp->dp_right = (struct dirlist *)NULL; 18791558Srgrimes dp->dp_flag = 0; 18801558Srgrimes dp->dp_hosts = (struct hostlist *)NULL; 18811558Srgrimes strcpy(dp->dp_dirp, cp); 18821558Srgrimes *dpp = dp; 18831558Srgrimes return (dp->dp_dirp); 18841558Srgrimes} 18851558Srgrimes 18861558Srgrimes/* 18871558Srgrimes * Hang the dir list element off the dirpath binary tree as required 18881558Srgrimes * and update the entry for host. 18891558Srgrimes */ 18901558Srgrimesvoid 1891216587Scharnierhang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep, 1892216587Scharnier int flags) 18931558Srgrimes{ 18941558Srgrimes struct hostlist *hp; 18951558Srgrimes struct dirlist *dp2; 18961558Srgrimes 18979336Sdfr if (flags & OP_ALLDIRS) { 18981558Srgrimes if (ep->ex_defdir) 18991558Srgrimes free((caddr_t)dp); 19001558Srgrimes else 19011558Srgrimes ep->ex_defdir = dp; 19029336Sdfr if (grp == (struct grouplist *)NULL) { 19031558Srgrimes ep->ex_defdir->dp_flag |= DP_DEFSET; 1904240902Srmacklem /* Save the default security flavors list. */ 1905240902Srmacklem ep->ex_defnumsecflavors = ep->ex_numsecflavors; 1906240902Srmacklem if (ep->ex_numsecflavors > 0) 1907240902Srmacklem memcpy(ep->ex_defsecflavors, ep->ex_secflavors, 1908240902Srmacklem sizeof(ep->ex_secflavors)); 19099336Sdfr } else while (grp) { 19101558Srgrimes hp = get_ht(); 19111558Srgrimes hp->ht_grp = grp; 19121558Srgrimes hp->ht_next = ep->ex_defdir->dp_hosts; 19131558Srgrimes ep->ex_defdir->dp_hosts = hp; 1914240902Srmacklem /* Save the security flavors list for this host set. */ 1915240902Srmacklem grp->gr_numsecflavors = ep->ex_numsecflavors; 1916240902Srmacklem if (ep->ex_numsecflavors > 0) 1917240902Srmacklem memcpy(grp->gr_secflavors, ep->ex_secflavors, 1918240902Srmacklem sizeof(ep->ex_secflavors)); 19191558Srgrimes grp = grp->gr_next; 19201558Srgrimes } 19211558Srgrimes } else { 19221558Srgrimes 19231558Srgrimes /* 192437663Scharnier * Loop through the directories adding them to the tree. 19251558Srgrimes */ 19261558Srgrimes while (dp) { 19271558Srgrimes dp2 = dp->dp_left; 1928240902Srmacklem add_dlist(&ep->ex_dirl, dp, grp, flags, ep); 19291558Srgrimes dp = dp2; 19301558Srgrimes } 19311558Srgrimes } 19321558Srgrimes} 19331558Srgrimes 19341558Srgrimes/* 19351558Srgrimes * Traverse the binary tree either updating a node that is already there 19361558Srgrimes * for the new directory or adding the new node. 19371558Srgrimes */ 19381558Srgrimesvoid 1939216587Scharnieradd_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp, 1940240902Srmacklem int flags, struct exportlist *ep) 19411558Srgrimes{ 19421558Srgrimes struct dirlist *dp; 19431558Srgrimes struct hostlist *hp; 19441558Srgrimes int cmp; 19451558Srgrimes 19461558Srgrimes dp = *dpp; 19471558Srgrimes if (dp) { 19481558Srgrimes cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 19491558Srgrimes if (cmp > 0) { 1950240902Srmacklem add_dlist(&dp->dp_left, newdp, grp, flags, ep); 19511558Srgrimes return; 19521558Srgrimes } else if (cmp < 0) { 1953240902Srmacklem add_dlist(&dp->dp_right, newdp, grp, flags, ep); 19541558Srgrimes return; 19551558Srgrimes } else 19561558Srgrimes free((caddr_t)newdp); 19571558Srgrimes } else { 19581558Srgrimes dp = newdp; 19591558Srgrimes dp->dp_left = (struct dirlist *)NULL; 19601558Srgrimes *dpp = dp; 19611558Srgrimes } 19621558Srgrimes if (grp) { 19631558Srgrimes 19641558Srgrimes /* 19651558Srgrimes * Hang all of the host(s) off of the directory point. 19661558Srgrimes */ 19671558Srgrimes do { 19681558Srgrimes hp = get_ht(); 19691558Srgrimes hp->ht_grp = grp; 19701558Srgrimes hp->ht_next = dp->dp_hosts; 19711558Srgrimes dp->dp_hosts = hp; 1972240902Srmacklem /* Save the security flavors list for this host set. */ 1973240902Srmacklem grp->gr_numsecflavors = ep->ex_numsecflavors; 1974240902Srmacklem if (ep->ex_numsecflavors > 0) 1975240902Srmacklem memcpy(grp->gr_secflavors, ep->ex_secflavors, 1976240902Srmacklem sizeof(ep->ex_secflavors)); 19771558Srgrimes grp = grp->gr_next; 19781558Srgrimes } while (grp); 19799336Sdfr } else { 19801558Srgrimes dp->dp_flag |= DP_DEFSET; 1981240902Srmacklem /* Save the default security flavors list. */ 1982240902Srmacklem ep->ex_defnumsecflavors = ep->ex_numsecflavors; 1983240902Srmacklem if (ep->ex_numsecflavors > 0) 1984240902Srmacklem memcpy(ep->ex_defsecflavors, ep->ex_secflavors, 1985240902Srmacklem sizeof(ep->ex_secflavors)); 19869336Sdfr } 19871558Srgrimes} 19881558Srgrimes 19891558Srgrimes/* 19901558Srgrimes * Search for a dirpath on the export point. 19911558Srgrimes */ 19921558Srgrimesstruct dirlist * 1993216587Scharnierdirp_search(struct dirlist *dp, char *dirp) 19941558Srgrimes{ 19951558Srgrimes int cmp; 19961558Srgrimes 19971558Srgrimes if (dp) { 199874462Salfred cmp = strcmp(dp->dp_dirp, dirp); 19991558Srgrimes if (cmp > 0) 200074462Salfred return (dirp_search(dp->dp_left, dirp)); 20011558Srgrimes else if (cmp < 0) 200274462Salfred return (dirp_search(dp->dp_right, dirp)); 20031558Srgrimes else 20041558Srgrimes return (dp); 20051558Srgrimes } 20061558Srgrimes return (dp); 20071558Srgrimes} 20081558Srgrimes 20091558Srgrimes/* 20101558Srgrimes * Scan for a host match in a directory tree. 20111558Srgrimes */ 20121558Srgrimesint 2013216587Scharnierchk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp, 2014240902Srmacklem int *hostsetp, int *numsecflavors, int **secflavorsp) 20151558Srgrimes{ 20161558Srgrimes struct hostlist *hp; 20171558Srgrimes struct grouplist *grp; 201874462Salfred struct addrinfo *ai; 20191558Srgrimes 20201558Srgrimes if (dp) { 20211558Srgrimes if (dp->dp_flag & DP_DEFSET) 20229336Sdfr *defsetp = dp->dp_flag; 20231558Srgrimes hp = dp->dp_hosts; 20241558Srgrimes while (hp) { 20251558Srgrimes grp = hp->ht_grp; 20261558Srgrimes switch (grp->gr_type) { 20271558Srgrimes case GT_HOST: 202874462Salfred ai = grp->gr_ptr.gt_addrinfo; 202974462Salfred for (; ai; ai = ai->ai_next) { 203075801Siedowse if (!sacmp(ai->ai_addr, saddr, NULL)) { 203174462Salfred *hostsetp = 203274462Salfred (hp->ht_flag | DP_HOSTSET); 2033240902Srmacklem if (numsecflavors != NULL) { 2034240902Srmacklem *numsecflavors = 2035240902Srmacklem grp->gr_numsecflavors; 2036240902Srmacklem *secflavorsp = 2037240902Srmacklem grp->gr_secflavors; 2038240902Srmacklem } 203974462Salfred return (1); 204074462Salfred } 20419336Sdfr } 204275801Siedowse break; 20431558Srgrimes case GT_NET: 204475801Siedowse if (!sacmp(saddr, (struct sockaddr *) 204575801Siedowse &grp->gr_ptr.gt_net.nt_net, 204675801Siedowse (struct sockaddr *) 204775801Siedowse &grp->gr_ptr.gt_net.nt_mask)) { 204874462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 2049240902Srmacklem if (numsecflavors != NULL) { 2050240902Srmacklem *numsecflavors = 2051240902Srmacklem grp->gr_numsecflavors; 2052240902Srmacklem *secflavorsp = 2053240902Srmacklem grp->gr_secflavors; 2054240902Srmacklem } 205574462Salfred return (1); 205674462Salfred } 205775801Siedowse break; 205875801Siedowse } 20591558Srgrimes hp = hp->ht_next; 20601558Srgrimes } 20611558Srgrimes } 20621558Srgrimes return (0); 20631558Srgrimes} 20641558Srgrimes 20651558Srgrimes/* 20661558Srgrimes * Scan tree for a host that matches the address. 20671558Srgrimes */ 20681558Srgrimesint 2069216587Scharnierscan_tree(struct dirlist *dp, struct sockaddr *saddr) 20701558Srgrimes{ 20719336Sdfr int defset, hostset; 20721558Srgrimes 20731558Srgrimes if (dp) { 20741558Srgrimes if (scan_tree(dp->dp_left, saddr)) 20751558Srgrimes return (1); 2076240902Srmacklem if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL)) 20771558Srgrimes return (1); 20781558Srgrimes if (scan_tree(dp->dp_right, saddr)) 20791558Srgrimes return (1); 20801558Srgrimes } 20811558Srgrimes return (0); 20821558Srgrimes} 20831558Srgrimes 20841558Srgrimes/* 20851558Srgrimes * Traverse the dirlist tree and free it up. 20861558Srgrimes */ 20871558Srgrimesvoid 2088216587Scharnierfree_dir(struct dirlist *dp) 20891558Srgrimes{ 20901558Srgrimes 20911558Srgrimes if (dp) { 20921558Srgrimes free_dir(dp->dp_left); 20931558Srgrimes free_dir(dp->dp_right); 20941558Srgrimes free_host(dp->dp_hosts); 20951558Srgrimes free((caddr_t)dp); 20961558Srgrimes } 20971558Srgrimes} 20981558Srgrimes 20991558Srgrimes/* 2100184588Sdfr * Parse a colon separated list of security flavors 2101184588Sdfr */ 2102184588Sdfrint 2103216587Scharnierparsesec(char *seclist, struct exportlist *ep) 2104184588Sdfr{ 2105184588Sdfr char *cp, savedc; 2106184588Sdfr int flavor; 2107184588Sdfr 2108184588Sdfr ep->ex_numsecflavors = 0; 2109184588Sdfr for (;;) { 2110184588Sdfr cp = strchr(seclist, ':'); 2111184588Sdfr if (cp) { 2112184588Sdfr savedc = *cp; 2113184588Sdfr *cp = '\0'; 2114184588Sdfr } 2115184588Sdfr 2116184588Sdfr if (!strcmp(seclist, "sys")) 2117184588Sdfr flavor = AUTH_SYS; 2118184588Sdfr else if (!strcmp(seclist, "krb5")) 2119184588Sdfr flavor = RPCSEC_GSS_KRB5; 2120184588Sdfr else if (!strcmp(seclist, "krb5i")) 2121184588Sdfr flavor = RPCSEC_GSS_KRB5I; 2122184588Sdfr else if (!strcmp(seclist, "krb5p")) 2123184588Sdfr flavor = RPCSEC_GSS_KRB5P; 2124184588Sdfr else { 2125184588Sdfr if (cp) 2126184588Sdfr *cp = savedc; 2127184588Sdfr syslog(LOG_ERR, "bad sec flavor: %s", seclist); 2128184588Sdfr return (1); 2129184588Sdfr } 2130184588Sdfr if (ep->ex_numsecflavors == MAXSECFLAVORS) { 2131184588Sdfr if (cp) 2132184588Sdfr *cp = savedc; 2133184588Sdfr syslog(LOG_ERR, "too many sec flavors: %s", seclist); 2134184588Sdfr return (1); 2135184588Sdfr } 2136184588Sdfr ep->ex_secflavors[ep->ex_numsecflavors] = flavor; 2137184588Sdfr ep->ex_numsecflavors++; 2138184588Sdfr if (cp) { 2139184588Sdfr *cp = savedc; 2140184588Sdfr seclist = cp + 1; 2141184588Sdfr } else { 2142184588Sdfr break; 2143184588Sdfr } 2144184588Sdfr } 2145184588Sdfr return (0); 2146184588Sdfr} 2147184588Sdfr 2148184588Sdfr/* 21491558Srgrimes * Parse the option string and update fields. 21501558Srgrimes * Option arguments may either be -<option>=<value> or 21511558Srgrimes * -<option> <value> 21521558Srgrimes */ 21531558Srgrimesint 2154216587Scharnierdo_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp, 2155216587Scharnier int *has_hostp, int *exflagsp, struct xucred *cr) 21561558Srgrimes{ 21571558Srgrimes char *cpoptarg, *cpoptend; 21581558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 21591558Srgrimes int allflag, usedarg; 21601558Srgrimes 216151968Salfred savedc2 = '\0'; 21621558Srgrimes cpopt = *cpp; 21631558Srgrimes cpopt++; 21641558Srgrimes cp = *endcpp; 21651558Srgrimes savedc = *cp; 21661558Srgrimes *cp = '\0'; 21671558Srgrimes while (cpopt && *cpopt) { 21681558Srgrimes allflag = 1; 21691558Srgrimes usedarg = -2; 217037663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 21711558Srgrimes *cpoptend++ = '\0'; 217237663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 21731558Srgrimes *cpoptarg++ = '\0'; 21741558Srgrimes } else { 217537663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 21761558Srgrimes *cpoptarg++ = '\0'; 21771558Srgrimes else { 21781558Srgrimes *cp = savedc; 21791558Srgrimes nextfield(&cp, &endcp); 21801558Srgrimes **endcpp = '\0'; 21811558Srgrimes if (endcp > cp && *cp != '-') { 21821558Srgrimes cpoptarg = cp; 21831558Srgrimes savedc2 = *endcp; 21841558Srgrimes *endcp = '\0'; 21851558Srgrimes usedarg = 0; 21861558Srgrimes } 21871558Srgrimes } 21881558Srgrimes } 21891558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 21901558Srgrimes *exflagsp |= MNT_EXRDONLY; 21911558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 21921558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 21931558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 21941558Srgrimes usedarg++; 21951558Srgrimes parsecred(cpoptarg, cr); 21961558Srgrimes if (allflag == 0) { 21971558Srgrimes *exflagsp |= MNT_EXPORTANON; 21981558Srgrimes opt_flags |= OP_MAPALL; 21991558Srgrimes } else 22001558Srgrimes opt_flags |= OP_MAPROOT; 22011558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 220275801Siedowse !strcmp(cpopt, "m"))) { 22031558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 220437663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 22051558Srgrimes return (1); 22061558Srgrimes } 22071558Srgrimes usedarg++; 22081558Srgrimes opt_flags |= OP_MASK; 22091558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 22101558Srgrimes !strcmp(cpopt, "n"))) { 221174462Salfred if (strchr(cpoptarg, '/') != NULL) { 221274462Salfred if (debug) 221374462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 221474462Salfred opt_flags |= OP_MASKLEN; 221574462Salfred } 22161558Srgrimes if (grp->gr_type != GT_NULL) { 221737663Scharnier syslog(LOG_ERR, "network/host conflict"); 22181558Srgrimes return (1); 22191558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 222037663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 22211558Srgrimes return (1); 22221558Srgrimes } 22231558Srgrimes grp->gr_type = GT_NET; 22241558Srgrimes *has_hostp = 1; 22251558Srgrimes usedarg++; 22261558Srgrimes opt_flags |= OP_NET; 22271558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 22281558Srgrimes opt_flags |= OP_ALLDIRS; 222927447Sdfr } else if (!strcmp(cpopt, "public")) { 223027447Sdfr *exflagsp |= MNT_EXPUBLIC; 223127447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 223227447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 223327447Sdfr opt_flags |= OP_MAPALL; 223427447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 223527447Sdfr ep->ex_indexfile = strdup(cpoptarg); 2236100336Sjoerg } else if (!strcmp(cpopt, "quiet")) { 2237100336Sjoerg opt_flags |= OP_QUIET; 2238247034Spluknet } else if (cpoptarg && !strcmp(cpopt, "sec")) { 2239184588Sdfr if (parsesec(cpoptarg, ep)) 2240184588Sdfr return (1); 2241184588Sdfr opt_flags |= OP_SEC; 2242184588Sdfr usedarg++; 22431558Srgrimes } else { 224437663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 22451558Srgrimes return (1); 22461558Srgrimes } 22471558Srgrimes if (usedarg >= 0) { 22481558Srgrimes *endcp = savedc2; 22491558Srgrimes **endcpp = savedc; 22501558Srgrimes if (usedarg > 0) { 22511558Srgrimes *cpp = cp; 22521558Srgrimes *endcpp = endcp; 22531558Srgrimes } 22541558Srgrimes return (0); 22551558Srgrimes } 22561558Srgrimes cpopt = cpoptend; 22571558Srgrimes } 22581558Srgrimes **endcpp = savedc; 22591558Srgrimes return (0); 22601558Srgrimes} 22611558Srgrimes 22621558Srgrimes/* 22631558Srgrimes * Translate a character string to the corresponding list of network 22641558Srgrimes * addresses for a hostname. 22651558Srgrimes */ 22661558Srgrimesint 2267216587Scharnierget_host(char *cp, struct grouplist *grp, struct grouplist *tgrp) 22681558Srgrimes{ 22697401Swpaul struct grouplist *checkgrp; 227075635Siedowse struct addrinfo *ai, *tai, hints; 227174462Salfred int ecode; 227274462Salfred char host[NI_MAXHOST]; 22731558Srgrimes 227474462Salfred if (grp->gr_type != GT_NULL) { 227574462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 22761558Srgrimes return (1); 22771558Srgrimes } 227874462Salfred memset(&hints, 0, sizeof hints); 227974462Salfred hints.ai_flags = AI_CANONNAME; 228074462Salfred hints.ai_protocol = IPPROTO_UDP; 228174462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 228274462Salfred if (ecode != 0) { 228375635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 228474462Salfred return 1; 228574462Salfred } 228674462Salfred grp->gr_ptr.gt_addrinfo = ai; 228774462Salfred while (ai != NULL) { 228874462Salfred if (ai->ai_canonname == NULL) { 228974462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 2290146187Sume sizeof host, NULL, 0, NI_NUMERICHOST) != 0) 229174462Salfred strlcpy(host, "?", sizeof(host)); 229274462Salfred ai->ai_canonname = strdup(host); 229374462Salfred ai->ai_flags |= AI_CANONNAME; 229475641Siedowse } 229574462Salfred if (debug) 229675635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 229775635Siedowse /* 229875635Siedowse * Sanity check: make sure we don't already have an entry 229975635Siedowse * for this host in the grouplist. 230075635Siedowse */ 230175635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 230275635Siedowse checkgrp = checkgrp->gr_next) { 230375635Siedowse if (checkgrp->gr_type != GT_HOST) 230475635Siedowse continue; 230575635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 230675635Siedowse tai = tai->ai_next) { 230775801Siedowse if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0) 230875635Siedowse continue; 230975635Siedowse if (debug) 231075635Siedowse fprintf(stderr, 231175635Siedowse "ignoring duplicate host %s\n", 231275635Siedowse ai->ai_canonname); 231375635Siedowse grp->gr_type = GT_IGNORE; 231475635Siedowse return (0); 231575635Siedowse } 231675635Siedowse } 231774462Salfred ai = ai->ai_next; 23181558Srgrimes } 231975635Siedowse grp->gr_type = GT_HOST; 23201558Srgrimes return (0); 23211558Srgrimes} 23221558Srgrimes 23231558Srgrimes/* 23241558Srgrimes * Free up an exports list component 23251558Srgrimes */ 23261558Srgrimesvoid 2327216587Scharnierfree_exp(struct exportlist *ep) 23281558Srgrimes{ 23291558Srgrimes 23301558Srgrimes if (ep->ex_defdir) { 23311558Srgrimes free_host(ep->ex_defdir->dp_hosts); 23321558Srgrimes free((caddr_t)ep->ex_defdir); 23331558Srgrimes } 23341558Srgrimes if (ep->ex_fsdir) 23351558Srgrimes free(ep->ex_fsdir); 233627447Sdfr if (ep->ex_indexfile) 233727447Sdfr free(ep->ex_indexfile); 23381558Srgrimes free_dir(ep->ex_dirl); 23391558Srgrimes free((caddr_t)ep); 23401558Srgrimes} 23411558Srgrimes 23421558Srgrimes/* 23431558Srgrimes * Free hosts. 23441558Srgrimes */ 23451558Srgrimesvoid 2346216587Scharnierfree_host(struct hostlist *hp) 23471558Srgrimes{ 23481558Srgrimes struct hostlist *hp2; 23491558Srgrimes 23501558Srgrimes while (hp) { 23511558Srgrimes hp2 = hp; 23521558Srgrimes hp = hp->ht_next; 23531558Srgrimes free((caddr_t)hp2); 23541558Srgrimes } 23551558Srgrimes} 23561558Srgrimes 23571558Srgrimesstruct hostlist * 2358216587Scharnierget_ht(void) 23591558Srgrimes{ 23601558Srgrimes struct hostlist *hp; 23611558Srgrimes 23621558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 23631558Srgrimes if (hp == (struct hostlist *)NULL) 23641558Srgrimes out_of_mem(); 23651558Srgrimes hp->ht_next = (struct hostlist *)NULL; 23669336Sdfr hp->ht_flag = 0; 23671558Srgrimes return (hp); 23681558Srgrimes} 23691558Srgrimes 23701558Srgrimes/* 23711558Srgrimes * Out of memory, fatal 23721558Srgrimes */ 23731558Srgrimesvoid 2374216587Scharnierout_of_mem(void) 23751558Srgrimes{ 23761558Srgrimes 237737663Scharnier syslog(LOG_ERR, "out of memory"); 23781558Srgrimes exit(2); 23791558Srgrimes} 23801558Srgrimes 23811558Srgrimes/* 2382158857Srodrigc * Do the nmount() syscall with the update flag to push the export info into 23831558Srgrimes * the kernel. 23841558Srgrimes */ 23851558Srgrimesint 2386158857Srodrigcdo_mount(struct exportlist *ep, struct grouplist *grp, int exflags, 2387158857Srodrigc struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb) 23881558Srgrimes{ 238975841Siedowse struct statfs fsb1; 239074462Salfred struct addrinfo *ai; 2391192934Srmacklem struct export_args ea, *eap; 2392158857Srodrigc char errmsg[255]; 2393158857Srodrigc char *cp; 23941558Srgrimes int done; 2395158857Srodrigc char savedc; 2396158857Srodrigc struct iovec *iov; 2397184588Sdfr int i, iovlen; 2398158857Srodrigc int ret; 2399192934Srmacklem struct nfsex_args nfsea; 24001558Srgrimes 2401192934Srmacklem if (run_v4server > 0) 2402192934Srmacklem eap = &nfsea.export; 2403192934Srmacklem else 2404192934Srmacklem eap = &ea; 2405192934Srmacklem 2406158857Srodrigc cp = NULL; 2407158857Srodrigc savedc = '\0'; 2408158857Srodrigc iov = NULL; 2409158857Srodrigc iovlen = 0; 2410158857Srodrigc ret = 0; 241175801Siedowse 2412192934Srmacklem bzero(eap, sizeof (struct export_args)); 2413158857Srodrigc bzero(errmsg, sizeof(errmsg)); 2414192934Srmacklem eap->ex_flags = exflags; 2415192934Srmacklem eap->ex_anon = *anoncrp; 2416192934Srmacklem eap->ex_indexfile = ep->ex_indexfile; 241775641Siedowse if (grp->gr_type == GT_HOST) 241874462Salfred ai = grp->gr_ptr.gt_addrinfo; 241975641Siedowse else 242075641Siedowse ai = NULL; 2421192934Srmacklem eap->ex_numsecflavors = ep->ex_numsecflavors; 2422192934Srmacklem for (i = 0; i < eap->ex_numsecflavors; i++) 2423192934Srmacklem eap->ex_secflavors[i] = ep->ex_secflavors[i]; 2424192934Srmacklem if (eap->ex_numsecflavors == 0) { 2425192934Srmacklem eap->ex_numsecflavors = 1; 2426192934Srmacklem eap->ex_secflavors[0] = AUTH_SYS; 2427184588Sdfr } 24281558Srgrimes done = FALSE; 2429158857Srodrigc 2430192934Srmacklem if (v4root_phase == 0) { 2431192934Srmacklem build_iovec(&iov, &iovlen, "fstype", NULL, 0); 2432192934Srmacklem build_iovec(&iov, &iovlen, "fspath", NULL, 0); 2433192934Srmacklem build_iovec(&iov, &iovlen, "from", NULL, 0); 2434192934Srmacklem build_iovec(&iov, &iovlen, "update", NULL, 0); 2435192934Srmacklem build_iovec(&iov, &iovlen, "export", eap, 2436192934Srmacklem sizeof (struct export_args)); 2437192934Srmacklem build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 2438192934Srmacklem } 2439158857Srodrigc 24401558Srgrimes while (!done) { 24411558Srgrimes switch (grp->gr_type) { 24421558Srgrimes case GT_HOST: 244375641Siedowse if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 244474462Salfred goto skip; 2445192934Srmacklem eap->ex_addr = ai->ai_addr; 2446192934Srmacklem eap->ex_addrlen = ai->ai_addrlen; 2447192934Srmacklem eap->ex_masklen = 0; 24481558Srgrimes break; 24491558Srgrimes case GT_NET: 245075801Siedowse if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 && 245174462Salfred have_v6 == 0) 245274462Salfred goto skip; 2453192934Srmacklem eap->ex_addr = 245475801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net; 2455192934Srmacklem eap->ex_addrlen = 2456158857Srodrigc ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len; 2457192934Srmacklem eap->ex_mask = 245875801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask; 2459192934Srmacklem eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len; 24601558Srgrimes break; 246175641Siedowse case GT_DEFAULT: 2462192934Srmacklem eap->ex_addr = NULL; 2463192934Srmacklem eap->ex_addrlen = 0; 2464192934Srmacklem eap->ex_mask = NULL; 2465192934Srmacklem eap->ex_masklen = 0; 246675641Siedowse break; 24677401Swpaul case GT_IGNORE: 2468158857Srodrigc ret = 0; 2469158857Srodrigc goto error_exit; 24707401Swpaul break; 24711558Srgrimes default: 247237663Scharnier syslog(LOG_ERR, "bad grouptype"); 24731558Srgrimes if (cp) 24741558Srgrimes *cp = savedc; 2475158857Srodrigc ret = 1; 2476158857Srodrigc goto error_exit; 24771558Srgrimes }; 24781558Srgrimes 24791558Srgrimes /* 2480192934Srmacklem * For V4:, use the nfssvc() syscall, instead of mount(). 24811558Srgrimes */ 2482192934Srmacklem if (v4root_phase == 2) { 2483192934Srmacklem nfsea.fspec = v4root_dirpath; 2484192934Srmacklem if (run_v4server > 0 && 2485192934Srmacklem nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) { 2486192934Srmacklem syslog(LOG_ERR, "Exporting V4: failed"); 2487192934Srmacklem return (2); 2488158857Srodrigc } 2489192934Srmacklem } else { 2490192934Srmacklem /* 2491192934Srmacklem * XXX: 2492192934Srmacklem * Maybe I should just use the fsb->f_mntonname path 2493192934Srmacklem * instead of looping back up the dirp to the mount 2494192934Srmacklem * point?? 2495192934Srmacklem * Also, needs to know how to export all types of local 2496192934Srmacklem * exportable filesystems and not just "ufs". 2497192934Srmacklem */ 2498192934Srmacklem iov[1].iov_base = fsb->f_fstypename; /* "fstype" */ 2499192934Srmacklem iov[1].iov_len = strlen(fsb->f_fstypename) + 1; 2500192934Srmacklem iov[3].iov_base = fsb->f_mntonname; /* "fspath" */ 2501192934Srmacklem iov[3].iov_len = strlen(fsb->f_mntonname) + 1; 2502192934Srmacklem iov[5].iov_base = fsb->f_mntfromname; /* "from" */ 2503192934Srmacklem iov[5].iov_len = strlen(fsb->f_mntfromname) + 1; 2504192934Srmacklem 2505192934Srmacklem while (nmount(iov, iovlen, fsb->f_flags) < 0) { 2506192934Srmacklem if (cp) 2507192934Srmacklem *cp-- = savedc; 2508192934Srmacklem else 2509192934Srmacklem cp = dirp + dirplen - 1; 2510192934Srmacklem if (opt_flags & OP_QUIET) { 2511192934Srmacklem ret = 1; 2512192934Srmacklem goto error_exit; 2513192934Srmacklem } 2514192934Srmacklem if (errno == EPERM) { 2515192934Srmacklem if (debug) 2516239744Sdelphij warnx("can't change attributes for %s: %s", 2517239744Sdelphij dirp, errmsg); 2518192934Srmacklem syslog(LOG_ERR, 2519239744Sdelphij "can't change attributes for %s: %s", 2520239744Sdelphij dirp, errmsg); 2521192934Srmacklem ret = 1; 2522192934Srmacklem goto error_exit; 2523192934Srmacklem } 2524192934Srmacklem if (opt_flags & OP_ALLDIRS) { 2525192934Srmacklem if (errno == EINVAL) 2526192934Srmacklem syslog(LOG_ERR, 2527100336Sjoerg "-alldirs requested but %s is not a filesystem mountpoint", 2528192934Srmacklem dirp); 2529192934Srmacklem else 2530192934Srmacklem syslog(LOG_ERR, 2531192934Srmacklem "could not remount %s: %m", 2532192934Srmacklem dirp); 2533192934Srmacklem ret = 1; 2534192934Srmacklem goto error_exit; 2535192934Srmacklem } 2536192934Srmacklem /* back up over the last component */ 2537192934Srmacklem while (*cp == '/' && cp > dirp) 2538192934Srmacklem cp--; 2539192934Srmacklem while (*(cp - 1) != '/' && cp > dirp) 2540192934Srmacklem cp--; 2541192934Srmacklem if (cp == dirp) { 2542192934Srmacklem if (debug) 2543192934Srmacklem warnx("mnt unsucc"); 2544192934Srmacklem syslog(LOG_ERR, "can't export %s %s", 2545192934Srmacklem dirp, errmsg); 2546192934Srmacklem ret = 1; 2547192934Srmacklem goto error_exit; 2548192934Srmacklem } 2549192934Srmacklem savedc = *cp; 2550192934Srmacklem *cp = '\0'; 2551192934Srmacklem /* 2552192934Srmacklem * Check that we're still on the same 2553192934Srmacklem * filesystem. 2554192934Srmacklem */ 2555192934Srmacklem if (statfs(dirp, &fsb1) != 0 || 2556192934Srmacklem bcmp(&fsb1.f_fsid, &fsb->f_fsid, 2557192934Srmacklem sizeof (fsb1.f_fsid)) != 0) { 2558192934Srmacklem *cp = savedc; 2559100336Sjoerg syslog(LOG_ERR, 2560192934Srmacklem "can't export %s %s", dirp, 2561192934Srmacklem errmsg); 2562192934Srmacklem ret = 1; 2563192934Srmacklem goto error_exit; 2564192934Srmacklem } 25651558Srgrimes } 25661558Srgrimes } 2567192934Srmacklem 2568192934Srmacklem /* 2569192934Srmacklem * For the experimental server: 2570192934Srmacklem * If this is the public directory, get the file handle 2571192934Srmacklem * and load it into the kernel via the nfssvc() syscall. 2572192934Srmacklem */ 2573192934Srmacklem if (run_v4server > 0 && (exflags & MNT_EXPUBLIC) != 0) { 2574192934Srmacklem fhandle_t fh; 2575192934Srmacklem char *public_name; 2576192934Srmacklem 2577192934Srmacklem if (eap->ex_indexfile != NULL) 2578192934Srmacklem public_name = eap->ex_indexfile; 2579192934Srmacklem else 2580192934Srmacklem public_name = dirp; 2581192934Srmacklem if (getfh(public_name, &fh) < 0) 2582192934Srmacklem syslog(LOG_ERR, 2583192934Srmacklem "Can't get public fh for %s", public_name); 2584192934Srmacklem else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0) 2585192934Srmacklem syslog(LOG_ERR, 2586192934Srmacklem "Can't set public fh for %s", public_name); 2587192934Srmacklem else 2588192934Srmacklem has_publicfh = 1; 2589192934Srmacklem } 259074462Salfredskip: 259175641Siedowse if (ai != NULL) 259274462Salfred ai = ai->ai_next; 259375641Siedowse if (ai == NULL) 25941558Srgrimes done = TRUE; 25951558Srgrimes } 25961558Srgrimes if (cp) 25971558Srgrimes *cp = savedc; 2598158857Srodrigcerror_exit: 2599158857Srodrigc /* free strings allocated by strdup() in getmntopts.c */ 2600158857Srodrigc if (iov != NULL) { 2601158857Srodrigc free(iov[0].iov_base); /* fstype */ 2602158857Srodrigc free(iov[2].iov_base); /* fspath */ 2603158857Srodrigc free(iov[4].iov_base); /* from */ 2604158857Srodrigc free(iov[6].iov_base); /* update */ 2605158857Srodrigc free(iov[8].iov_base); /* export */ 2606158857Srodrigc free(iov[10].iov_base); /* errmsg */ 2607158857Srodrigc 2608158857Srodrigc /* free iov, allocated by realloc() */ 2609158857Srodrigc free(iov); 2610158857Srodrigc } 2611158857Srodrigc return (ret); 26121558Srgrimes} 26131558Srgrimes 26141558Srgrimes/* 26151558Srgrimes * Translate a net address. 261675801Siedowse * 261775801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address. 26181558Srgrimes */ 26191558Srgrimesint 2620216587Scharnierget_net(char *cp, struct netmsk *net, int maskflg) 26211558Srgrimes{ 262275861Siedowse struct netent *np = NULL; 262374462Salfred char *name, *p, *prefp; 262475801Siedowse struct sockaddr_in sin; 262575861Siedowse struct sockaddr *sa = NULL; 262674462Salfred struct addrinfo hints, *ai = NULL; 262774462Salfred char netname[NI_MAXHOST]; 262874462Salfred long preflen; 26291558Srgrimes 263075635Siedowse p = prefp = NULL; 263174462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 263274462Salfred p = strchr(cp, '/'); 263374462Salfred *p = '\0'; 263474462Salfred prefp = p + 1; 263574462Salfred } 263674462Salfred 263775861Siedowse /* 263875861Siedowse * Check for a numeric address first. We wish to avoid 263975861Siedowse * possible DNS lookups in getnetbyname(). 264075861Siedowse */ 264175861Siedowse if (isxdigit(*cp) || *cp == ':') { 264274462Salfred memset(&hints, 0, sizeof hints); 264375801Siedowse /* Ensure the mask and the network have the same family. */ 264475801Siedowse if (maskflg && (opt_flags & OP_NET)) 264575801Siedowse hints.ai_family = net->nt_net.ss_family; 264675801Siedowse else if (!maskflg && (opt_flags & OP_HAVEMASK)) 264775801Siedowse hints.ai_family = net->nt_mask.ss_family; 264875801Siedowse else 264975801Siedowse hints.ai_family = AF_UNSPEC; 265074462Salfred hints.ai_flags = AI_NUMERICHOST; 265175861Siedowse if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 265275861Siedowse sa = ai->ai_addr; 265375861Siedowse if (sa != NULL && ai->ai_family == AF_INET) { 265474462Salfred /* 265575801Siedowse * The address in `cp' is really a network address, so 265675801Siedowse * use inet_network() to re-interpret this correctly. 265775801Siedowse * e.g. "127.1" means 127.1.0.0, not 127.0.0.1. 265874462Salfred */ 265975801Siedowse bzero(&sin, sizeof sin); 266074462Salfred sin.sin_family = AF_INET; 266174462Salfred sin.sin_len = sizeof sin; 266275801Siedowse sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 266374462Salfred if (debug) 266475801Siedowse fprintf(stderr, "get_net: v4 addr %s\n", 266575801Siedowse inet_ntoa(sin.sin_addr)); 266674462Salfred sa = (struct sockaddr *)&sin; 266775861Siedowse } 266875861Siedowse } 266975861Siedowse if (sa == NULL && (np = getnetbyname(cp)) != NULL) { 267075861Siedowse bzero(&sin, sizeof sin); 267175861Siedowse sin.sin_family = AF_INET; 267275861Siedowse sin.sin_len = sizeof sin; 267375861Siedowse sin.sin_addr = inet_makeaddr(np->n_net, 0); 267475861Siedowse sa = (struct sockaddr *)&sin; 267575861Siedowse } 267675861Siedowse if (sa == NULL) 267774462Salfred goto fail; 267825318Spst 267975801Siedowse if (maskflg) { 268075801Siedowse /* The specified sockaddr is a mask. */ 268175801Siedowse if (checkmask(sa) != 0) 268275801Siedowse goto fail; 268375801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 268475801Siedowse opt_flags |= OP_HAVEMASK; 268575801Siedowse } else { 268675801Siedowse /* The specified sockaddr is a network address. */ 268775801Siedowse bcopy(sa, &net->nt_net, sa->sa_len); 268874462Salfred 268975801Siedowse /* Get a network name for the export list. */ 269075801Siedowse if (np) { 269175801Siedowse name = np->n_name; 269275801Siedowse } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 2693146187Sume NULL, 0, NI_NUMERICHOST) == 0) { 269475801Siedowse name = netname; 269575801Siedowse } else { 269675801Siedowse goto fail; 269775801Siedowse } 269875801Siedowse if ((net->nt_name = strdup(name)) == NULL) 269975801Siedowse out_of_mem(); 270075801Siedowse 270175801Siedowse /* 270275801Siedowse * Extract a mask from either a "/<masklen>" suffix, or 270375801Siedowse * from the class of an IPv4 address. 270475801Siedowse */ 270574462Salfred if (opt_flags & OP_MASKLEN) { 270674462Salfred preflen = strtol(prefp, NULL, 10); 270775801Siedowse if (preflen < 0L || preflen == LONG_MAX) 270874462Salfred goto fail; 270975801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 271075801Siedowse if (makemask(&net->nt_mask, (int)preflen) != 0) 271175801Siedowse goto fail; 271275801Siedowse opt_flags |= OP_HAVEMASK; 271374462Salfred *p = '/'; 271475801Siedowse } else if (sa->sa_family == AF_INET && 271575801Siedowse (opt_flags & OP_MASK) == 0) { 271675801Siedowse in_addr_t addr; 271774462Salfred 271875801Siedowse addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 271975801Siedowse if (IN_CLASSA(addr)) 272075801Siedowse preflen = 8; 272175801Siedowse else if (IN_CLASSB(addr)) 272275801Siedowse preflen = 16; 272375801Siedowse else if (IN_CLASSC(addr)) 272475801Siedowse preflen = 24; 272575801Siedowse else if (IN_CLASSD(addr)) 272675801Siedowse preflen = 28; 272775801Siedowse else 272875801Siedowse preflen = 32; /* XXX */ 272975801Siedowse 273075801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 273175801Siedowse makemask(&net->nt_mask, (int)preflen); 273275801Siedowse opt_flags |= OP_HAVEMASK; 273374462Salfred } 273474462Salfred } 273574462Salfred 273674462Salfred if (ai) 273774462Salfred freeaddrinfo(ai); 273874462Salfred return 0; 273974462Salfred 274074462Salfredfail: 274174462Salfred if (ai) 274274462Salfred freeaddrinfo(ai); 274374462Salfred return 1; 27441558Srgrimes} 27451558Srgrimes 27461558Srgrimes/* 27471558Srgrimes * Parse out the next white space separated field 27481558Srgrimes */ 27491558Srgrimesvoid 2750216587Scharniernextfield(char **cp, char **endcp) 27511558Srgrimes{ 27521558Srgrimes char *p; 27531558Srgrimes 27541558Srgrimes p = *cp; 27551558Srgrimes while (*p == ' ' || *p == '\t') 27561558Srgrimes p++; 27571558Srgrimes if (*p == '\n' || *p == '\0') 27581558Srgrimes *cp = *endcp = p; 27591558Srgrimes else { 27601558Srgrimes *cp = p++; 27611558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 27621558Srgrimes p++; 27631558Srgrimes *endcp = p; 27641558Srgrimes } 27651558Srgrimes} 27661558Srgrimes 27671558Srgrimes/* 27681558Srgrimes * Get an exports file line. Skip over blank lines and handle line 27691558Srgrimes * continuations. 27701558Srgrimes */ 27711558Srgrimesint 2772216587Scharnierget_line(void) 27731558Srgrimes{ 27741558Srgrimes char *p, *cp; 277596622Siedowse size_t len; 27761558Srgrimes int totlen, cont_line; 27771558Srgrimes 27781558Srgrimes /* 27791558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 27801558Srgrimes */ 27811558Srgrimes p = line; 27821558Srgrimes totlen = 0; 27831558Srgrimes do { 278496622Siedowse if ((p = fgetln(exp_file, &len)) == NULL) 27851558Srgrimes return (0); 27861558Srgrimes cp = p + len - 1; 27871558Srgrimes cont_line = 0; 27881558Srgrimes while (cp >= p && 27891558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 27901558Srgrimes if (*cp == '\\') 27911558Srgrimes cont_line = 1; 27921558Srgrimes cp--; 27931558Srgrimes len--; 27941558Srgrimes } 279579117Sdd if (cont_line) { 279679117Sdd *++cp = ' '; 279779117Sdd len++; 279879117Sdd } 279996622Siedowse if (linesize < len + totlen + 1) { 280096622Siedowse linesize = len + totlen + 1; 280196622Siedowse line = realloc(line, linesize); 280296622Siedowse if (line == NULL) 280396622Siedowse out_of_mem(); 28041558Srgrimes } 280596622Siedowse memcpy(line + totlen, p, len); 280696622Siedowse totlen += len; 280796622Siedowse line[totlen] = '\0'; 28081558Srgrimes } while (totlen == 0 || cont_line); 28091558Srgrimes return (1); 28101558Srgrimes} 28111558Srgrimes 28121558Srgrimes/* 28131558Srgrimes * Parse a description of a credential. 28141558Srgrimes */ 28151558Srgrimesvoid 2816216587Scharnierparsecred(char *namelist, struct xucred *cr) 28171558Srgrimes{ 28181558Srgrimes char *name; 28191558Srgrimes int cnt; 28201558Srgrimes char *names; 28211558Srgrimes struct passwd *pw; 28221558Srgrimes struct group *gr; 2823194498Sbrooks gid_t groups[XU_NGROUPS + 1]; 2824136051Sstefanf int ngroups; 28251558Srgrimes 282691354Sdd cr->cr_version = XUCRED_VERSION; 28271558Srgrimes /* 282837663Scharnier * Set up the unprivileged user. 28291558Srgrimes */ 28301558Srgrimes cr->cr_uid = -2; 28311558Srgrimes cr->cr_groups[0] = -2; 28321558Srgrimes cr->cr_ngroups = 1; 28331558Srgrimes /* 28341558Srgrimes * Get the user's password table entry. 28351558Srgrimes */ 28361558Srgrimes names = strsep(&namelist, " \t\n"); 28371558Srgrimes name = strsep(&names, ":"); 28381558Srgrimes if (isdigit(*name) || *name == '-') 28391558Srgrimes pw = getpwuid(atoi(name)); 28401558Srgrimes else 28411558Srgrimes pw = getpwnam(name); 28421558Srgrimes /* 28431558Srgrimes * Credentials specified as those of a user. 28441558Srgrimes */ 28451558Srgrimes if (names == NULL) { 28461558Srgrimes if (pw == NULL) { 284737663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 28481558Srgrimes return; 28491558Srgrimes } 28501558Srgrimes cr->cr_uid = pw->pw_uid; 2851194498Sbrooks ngroups = XU_NGROUPS + 1; 28521558Srgrimes if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) 285337663Scharnier syslog(LOG_ERR, "too many groups"); 28541558Srgrimes /* 2855136051Sstefanf * Compress out duplicate. 28561558Srgrimes */ 28571558Srgrimes cr->cr_ngroups = ngroups - 1; 28581558Srgrimes cr->cr_groups[0] = groups[0]; 28591558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 28601558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 28611558Srgrimes return; 28621558Srgrimes } 28631558Srgrimes /* 28641558Srgrimes * Explicit credential specified as a colon separated list: 28651558Srgrimes * uid:gid:gid:... 28661558Srgrimes */ 28671558Srgrimes if (pw != NULL) 28681558Srgrimes cr->cr_uid = pw->pw_uid; 28691558Srgrimes else if (isdigit(*name) || *name == '-') 28701558Srgrimes cr->cr_uid = atoi(name); 28711558Srgrimes else { 287237663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 28731558Srgrimes return; 28741558Srgrimes } 28751558Srgrimes cr->cr_ngroups = 0; 2876194498Sbrooks while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) { 28771558Srgrimes name = strsep(&names, ":"); 28781558Srgrimes if (isdigit(*name) || *name == '-') { 28791558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 28801558Srgrimes } else { 28811558Srgrimes if ((gr = getgrnam(name)) == NULL) { 288237663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 28831558Srgrimes continue; 28841558Srgrimes } 28851558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 28861558Srgrimes } 28871558Srgrimes } 2888194498Sbrooks if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS) 288937663Scharnier syslog(LOG_ERR, "too many groups"); 28901558Srgrimes} 28911558Srgrimes 2892194880Sdfr#define STRSIZ (MNTNAMLEN+MNTPATHLEN+50) 28931558Srgrimes/* 28941558Srgrimes * Routines that maintain the remote mounttab 28951558Srgrimes */ 28961558Srgrimesvoid 2897216587Scharnierget_mountlist(void) 28981558Srgrimes{ 28991558Srgrimes struct mountlist *mlp, **mlpp; 290023681Speter char *host, *dirp, *cp; 29011558Srgrimes char str[STRSIZ]; 29021558Srgrimes FILE *mlfile; 29031558Srgrimes 29041558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 290553117Sbillf if (errno == ENOENT) 290653117Sbillf return; 290753117Sbillf else { 290853117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 290953117Sbillf return; 291053117Sbillf } 29111558Srgrimes } 29121558Srgrimes mlpp = &mlhead; 29131558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 291423681Speter cp = str; 291523681Speter host = strsep(&cp, " \t\n"); 291623681Speter dirp = strsep(&cp, " \t\n"); 291723681Speter if (host == NULL || dirp == NULL) 29181558Srgrimes continue; 29191558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 292037663Scharnier if (mlp == (struct mountlist *)NULL) 292137663Scharnier out_of_mem(); 2922194880Sdfr strncpy(mlp->ml_host, host, MNTNAMLEN); 2923194880Sdfr mlp->ml_host[MNTNAMLEN] = '\0'; 2924194880Sdfr strncpy(mlp->ml_dirp, dirp, MNTPATHLEN); 2925194880Sdfr mlp->ml_dirp[MNTPATHLEN] = '\0'; 29261558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 29271558Srgrimes *mlpp = mlp; 29281558Srgrimes mlpp = &mlp->ml_next; 29291558Srgrimes } 29301558Srgrimes fclose(mlfile); 29311558Srgrimes} 29321558Srgrimes 293375635Siedowsevoid 293475635Siedowsedel_mlist(char *hostp, char *dirp) 29351558Srgrimes{ 29361558Srgrimes struct mountlist *mlp, **mlpp; 29371558Srgrimes struct mountlist *mlp2; 29381558Srgrimes FILE *mlfile; 29391558Srgrimes int fnd = 0; 29401558Srgrimes 29411558Srgrimes mlpp = &mlhead; 29421558Srgrimes mlp = mlhead; 29431558Srgrimes while (mlp) { 29441558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 29451558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 29461558Srgrimes fnd = 1; 29471558Srgrimes mlp2 = mlp; 29481558Srgrimes *mlpp = mlp = mlp->ml_next; 29491558Srgrimes free((caddr_t)mlp2); 29501558Srgrimes } else { 29511558Srgrimes mlpp = &mlp->ml_next; 29521558Srgrimes mlp = mlp->ml_next; 29531558Srgrimes } 29541558Srgrimes } 29551558Srgrimes if (fnd) { 29561558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 295737663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 29581558Srgrimes return; 29591558Srgrimes } 29601558Srgrimes mlp = mlhead; 29611558Srgrimes while (mlp) { 29621558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 29631558Srgrimes mlp = mlp->ml_next; 29641558Srgrimes } 29651558Srgrimes fclose(mlfile); 29661558Srgrimes } 29671558Srgrimes} 29681558Srgrimes 29691558Srgrimesvoid 2970216587Scharnieradd_mlist(char *hostp, char *dirp) 29711558Srgrimes{ 29721558Srgrimes struct mountlist *mlp, **mlpp; 29731558Srgrimes FILE *mlfile; 29741558Srgrimes 29751558Srgrimes mlpp = &mlhead; 29761558Srgrimes mlp = mlhead; 29771558Srgrimes while (mlp) { 29781558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 29791558Srgrimes return; 29801558Srgrimes mlpp = &mlp->ml_next; 29811558Srgrimes mlp = mlp->ml_next; 29821558Srgrimes } 29831558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 298437663Scharnier if (mlp == (struct mountlist *)NULL) 298537663Scharnier out_of_mem(); 2986194880Sdfr strncpy(mlp->ml_host, hostp, MNTNAMLEN); 2987194880Sdfr mlp->ml_host[MNTNAMLEN] = '\0'; 2988194880Sdfr strncpy(mlp->ml_dirp, dirp, MNTPATHLEN); 2989194880Sdfr mlp->ml_dirp[MNTPATHLEN] = '\0'; 29901558Srgrimes mlp->ml_next = (struct mountlist *)NULL; 29911558Srgrimes *mlpp = mlp; 29921558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 299337663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 29941558Srgrimes return; 29951558Srgrimes } 29961558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 29971558Srgrimes fclose(mlfile); 29981558Srgrimes} 29991558Srgrimes 30001558Srgrimes/* 30011558Srgrimes * Free up a group list. 30021558Srgrimes */ 30031558Srgrimesvoid 3004216587Scharnierfree_grp(struct grouplist *grp) 30051558Srgrimes{ 30061558Srgrimes if (grp->gr_type == GT_HOST) { 300774462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 300874462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 30091558Srgrimes } else if (grp->gr_type == GT_NET) { 30101558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 30111558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 30121558Srgrimes } 30131558Srgrimes free((caddr_t)grp); 30141558Srgrimes} 30151558Srgrimes 30161558Srgrimes#ifdef DEBUG 30171558Srgrimesvoid 30181558SrgrimesSYSLOG(int pri, const char *fmt, ...) 30191558Srgrimes{ 30201558Srgrimes va_list ap; 30211558Srgrimes 30221558Srgrimes va_start(ap, fmt); 30231558Srgrimes vfprintf(stderr, fmt, ap); 30241558Srgrimes va_end(ap); 30251558Srgrimes} 30261558Srgrimes#endif /* DEBUG */ 30271558Srgrimes 30281558Srgrimes/* 30291558Srgrimes * Check options for consistency. 30301558Srgrimes */ 30311558Srgrimesint 3032216587Scharniercheck_options(struct dirlist *dp) 30331558Srgrimes{ 30341558Srgrimes 3035192934Srmacklem if (v4root_phase == 0 && dp == NULL) 30361558Srgrimes return (1); 303783653Speter if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) { 303883653Speter syslog(LOG_ERR, "-mapall and -maproot mutually exclusive"); 30391558Srgrimes return (1); 30401558Srgrimes } 30411558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 304275801Siedowse syslog(LOG_ERR, "-mask requires -network"); 304375801Siedowse return (1); 30441558Srgrimes } 304575801Siedowse if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) { 304675801Siedowse syslog(LOG_ERR, "-network requires mask specification"); 304775801Siedowse return (1); 304875801Siedowse } 304975801Siedowse if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) { 305075801Siedowse syslog(LOG_ERR, "-mask and /masklen are mutually exclusive"); 305175801Siedowse return (1); 305275801Siedowse } 3053192934Srmacklem if (v4root_phase > 0 && 3054192934Srmacklem (opt_flags & 3055192934Srmacklem ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) { 3056192934Srmacklem syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:"); 3057192934Srmacklem return (1); 3058192934Srmacklem } 3059207689Srmacklem if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 3060207689Srmacklem syslog(LOG_ERR, "-alldirs has multiple directories"); 3061207689Srmacklem return (1); 3062207689Srmacklem } 30631558Srgrimes return (0); 30641558Srgrimes} 30651558Srgrimes 30661558Srgrimes/* 30671558Srgrimes * Check an absolute directory path for any symbolic links. Return true 30681558Srgrimes */ 30691558Srgrimesint 3070216587Scharniercheck_dirpath(char *dirp) 30711558Srgrimes{ 30721558Srgrimes char *cp; 30731558Srgrimes int ret = 1; 30741558Srgrimes struct stat sb; 30751558Srgrimes 30761558Srgrimes cp = dirp + 1; 30771558Srgrimes while (*cp && ret) { 30781558Srgrimes if (*cp == '/') { 30791558Srgrimes *cp = '\0'; 30809336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 30811558Srgrimes ret = 0; 30821558Srgrimes *cp = '/'; 30831558Srgrimes } 30841558Srgrimes cp++; 30851558Srgrimes } 30869336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 30871558Srgrimes ret = 0; 30881558Srgrimes return (ret); 30891558Srgrimes} 30909336Sdfr 309175801Siedowse/* 309275801Siedowse * Make a netmask according to the specified prefix length. The ss_family 309375801Siedowse * and other non-address fields must be initialised before calling this. 309475801Siedowse */ 309575801Siedowseint 309675801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen) 309774462Salfred{ 309875801Siedowse u_char *p; 309975801Siedowse int bits, i, len; 310074462Salfred 310175801Siedowse if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL) 310275801Siedowse return (-1); 3103103949Smike if (bitlen > len * CHAR_BIT) 310475801Siedowse return (-1); 310574462Salfred 310675801Siedowse for (i = 0; i < len; i++) { 3107103949Smike bits = (bitlen > CHAR_BIT) ? CHAR_BIT : bitlen; 3108219125Sru *p++ = (u_char)~0 << (CHAR_BIT - bits); 310975801Siedowse bitlen -= bits; 311074462Salfred } 311175801Siedowse return 0; 311274462Salfred} 311374462Salfred 311475801Siedowse/* 311575801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask 311675801Siedowse * is acceptable (i.e. of the form 1...10....0). 311775801Siedowse */ 311875801Siedowseint 311975801Siedowsecheckmask(struct sockaddr *sa) 312074462Salfred{ 312175801Siedowse u_char *mask; 312275801Siedowse int i, len; 312374462Salfred 312475801Siedowse if ((mask = sa_rawaddr(sa, &len)) == NULL) 312575801Siedowse return (-1); 312675801Siedowse 312775801Siedowse for (i = 0; i < len; i++) 312875801Siedowse if (mask[i] != 0xff) 312975801Siedowse break; 313075801Siedowse if (i < len) { 313175801Siedowse if (~mask[i] & (u_char)(~mask[i] + 1)) 313275801Siedowse return (-1); 313375801Siedowse i++; 313474462Salfred } 313575801Siedowse for (; i < len; i++) 313675801Siedowse if (mask[i] != 0) 313775801Siedowse return (-1); 313875801Siedowse return (0); 313974462Salfred} 314074462Salfred 314175801Siedowse/* 314275801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if 314375801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'. 3144228990Suqs * If samask is NULL, perform a full comparison. 314575801Siedowse */ 314675801Siedowseint 314775801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask) 314874462Salfred{ 314975801Siedowse unsigned char *p1, *p2, *mask; 315075801Siedowse int len, i; 315174462Salfred 315275801Siedowse if (sa1->sa_family != sa2->sa_family || 315375801Siedowse (p1 = sa_rawaddr(sa1, &len)) == NULL || 315475801Siedowse (p2 = sa_rawaddr(sa2, NULL)) == NULL) 315575801Siedowse return (1); 315675801Siedowse 315775801Siedowse switch (sa1->sa_family) { 315874462Salfred case AF_INET6: 315975801Siedowse if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 316075801Siedowse ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 316175801Siedowse return (1); 316274462Salfred break; 316374462Salfred } 316474462Salfred 316575801Siedowse /* Simple binary comparison if no mask specified. */ 316675801Siedowse if (samask == NULL) 316775801Siedowse return (memcmp(p1, p2, len)); 316874462Salfred 316975801Siedowse /* Set up the mask, and do a mask-based comparison. */ 317075801Siedowse if (sa1->sa_family != samask->sa_family || 317175801Siedowse (mask = sa_rawaddr(samask, NULL)) == NULL) 317275801Siedowse return (1); 317374462Salfred 317475801Siedowse for (i = 0; i < len; i++) 317575801Siedowse if ((p1[i] & mask[i]) != (p2[i] & mask[i])) 317675801Siedowse return (1); 317775801Siedowse return (0); 317874462Salfred} 317974462Salfred 318075801Siedowse/* 318175801Siedowse * Return a pointer to the part of the sockaddr that contains the 318275801Siedowse * raw address, and set *nbytes to its length in bytes. Returns 318375801Siedowse * NULL if the address family is unknown. 318475801Siedowse */ 318575801Siedowsevoid * 318675801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) { 318775801Siedowse void *p; 318874462Salfred int len; 318974462Salfred 319075801Siedowse switch (sa->sa_family) { 319174462Salfred case AF_INET: 319275801Siedowse len = sizeof(((struct sockaddr_in *)sa)->sin_addr); 319375801Siedowse p = &((struct sockaddr_in *)sa)->sin_addr; 319474462Salfred break; 319574462Salfred case AF_INET6: 319675801Siedowse len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); 319775801Siedowse p = &((struct sockaddr_in6 *)sa)->sin6_addr; 319874462Salfred break; 319974462Salfred default: 320075801Siedowse p = NULL; 320175801Siedowse len = 0; 320274462Salfred } 320374462Salfred 320475801Siedowse if (nbytes != NULL) 320575801Siedowse *nbytes = len; 320675801Siedowse return (p); 320774462Salfred} 320874462Salfred 320975754Siedowsevoid 3210216587Scharnierhuphandler(int sig __unused) 321175754Siedowse{ 321275754Siedowse got_sighup = 1; 321375754Siedowse} 321475754Siedowse 3215216587Scharniervoid terminate(int sig __unused) 321674462Salfred{ 3217149433Spjd pidfile_remove(pfh); 3218194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS, NULL); 3219194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL); 322074462Salfred exit (0); 322174462Salfred} 3222192934Srmacklem 3223