mountd.c revision 346769
1331722Seadler/* 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: stable/11/usr.sbin/mountd/mountd.c 346769 2019-04-26 21:44:51Z mav $"); 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> 53324955Smanu#include <sys/queue.h> 541558Srgrimes#include <sys/stat.h> 55192934Srmacklem#include <sys/sysctl.h> 561558Srgrimes#include <sys/syslog.h> 571558Srgrimes 581558Srgrimes#include <rpc/rpc.h> 59109363Smbr#include <rpc/rpc_com.h> 601558Srgrimes#include <rpc/pmap_clnt.h> 6174462Salfred#include <rpc/pmap_prot.h> 6274462Salfred#include <rpcsvc/mount.h> 639336Sdfr#include <nfs/nfsproto.h> 64192934Srmacklem#include <nfs/nfssvc.h> 6583653Speter#include <nfsserver/nfs.h> 661558Srgrimes 67192934Srmacklem#include <fs/nfs/nfsport.h> 68192934Srmacklem 691558Srgrimes#include <arpa/inet.h> 701558Srgrimes 711558Srgrimes#include <ctype.h> 7237663Scharnier#include <err.h> 731558Srgrimes#include <errno.h> 741558Srgrimes#include <grp.h> 75149433Spjd#include <libutil.h> 76103949Smike#include <limits.h> 771558Srgrimes#include <netdb.h> 781558Srgrimes#include <pwd.h> 791558Srgrimes#include <signal.h> 801558Srgrimes#include <stdio.h> 811558Srgrimes#include <stdlib.h> 821558Srgrimes#include <string.h> 831558Srgrimes#include <unistd.h> 841558Srgrimes#include "pathnames.h" 85158857Srodrigc#include "mntopts.h" 861558Srgrimes 871558Srgrimes#ifdef DEBUG 881558Srgrimes#include <stdarg.h> 891558Srgrimes#endif 901558Srgrimes 911558Srgrimes/* 921558Srgrimes * Structures for keeping the mount list and export list 931558Srgrimes */ 941558Srgrimesstruct mountlist { 95194880Sdfr char ml_host[MNTNAMLEN+1]; 96194880Sdfr char ml_dirp[MNTPATHLEN+1]; 97324955Smanu 98324955Smanu SLIST_ENTRY(mountlist) next; 991558Srgrimes}; 1001558Srgrimes 1011558Srgrimesstruct dirlist { 1021558Srgrimes struct dirlist *dp_left; 1031558Srgrimes struct dirlist *dp_right; 1041558Srgrimes int dp_flag; 1051558Srgrimes struct hostlist *dp_hosts; /* List of hosts this dir exported to */ 106324234Smanu char *dp_dirp; 1071558Srgrimes}; 1081558Srgrimes/* dp_flag bits */ 1091558Srgrimes#define DP_DEFSET 0x1 1109336Sdfr#define DP_HOSTSET 0x2 1111558Srgrimes 1121558Srgrimesstruct exportlist { 1131558Srgrimes struct dirlist *ex_dirl; 1141558Srgrimes struct dirlist *ex_defdir; 1151558Srgrimes int ex_flag; 1161558Srgrimes fsid_t ex_fs; 1171558Srgrimes char *ex_fsdir; 11827447Sdfr char *ex_indexfile; 119184588Sdfr int ex_numsecflavors; 120184588Sdfr int ex_secflavors[MAXSECFLAVORS]; 121240902Srmacklem int ex_defnumsecflavors; 122240902Srmacklem int ex_defsecflavors[MAXSECFLAVORS]; 123324955Smanu 124324955Smanu SLIST_ENTRY(exportlist) entries; 1251558Srgrimes}; 1261558Srgrimes/* ex_flag bits */ 1271558Srgrimes#define EX_LINKED 0x1 1281558Srgrimes 1291558Srgrimesstruct netmsk { 13074462Salfred struct sockaddr_storage nt_net; 13175801Siedowse struct sockaddr_storage nt_mask; 13242144Sdfr char *nt_name; 1331558Srgrimes}; 1341558Srgrimes 1351558Srgrimesunion grouptypes { 13674462Salfred struct addrinfo *gt_addrinfo; 1371558Srgrimes struct netmsk gt_net; 1381558Srgrimes}; 1391558Srgrimes 1401558Srgrimesstruct grouplist { 1411558Srgrimes int gr_type; 1421558Srgrimes union grouptypes gr_ptr; 1431558Srgrimes struct grouplist *gr_next; 144240902Srmacklem int gr_numsecflavors; 145240902Srmacklem int gr_secflavors[MAXSECFLAVORS]; 1461558Srgrimes}; 1471558Srgrimes/* Group types */ 1481558Srgrimes#define GT_NULL 0x0 1491558Srgrimes#define GT_HOST 0x1 1501558Srgrimes#define GT_NET 0x2 15175641Siedowse#define GT_DEFAULT 0x3 1527401Swpaul#define GT_IGNORE 0x5 1531558Srgrimes 1541558Srgrimesstruct hostlist { 1559336Sdfr int ht_flag; /* Uses DP_xx bits */ 1561558Srgrimes struct grouplist *ht_grp; 1571558Srgrimes struct hostlist *ht_next; 1581558Srgrimes}; 1591558Srgrimes 1609336Sdfrstruct fhreturn { 1619336Sdfr int fhr_flag; 1629336Sdfr int fhr_vers; 1639336Sdfr nfsfh_t fhr_fh; 164184588Sdfr int fhr_numsecflavors; 165184588Sdfr int *fhr_secflavors; 1669336Sdfr}; 1679336Sdfr 168222623Srmacklem#define GETPORT_MAXTRY 20 /* Max tries to get a port # */ 169222623Srmacklem 1701558Srgrimes/* Global defs */ 171285128Straszstatic char *add_expdir(struct dirlist **, char *, int); 172285128Straszstatic void add_dlist(struct dirlist **, struct dirlist *, 173285128Strasz struct grouplist *, int, struct exportlist *); 174285128Straszstatic void add_mlist(char *, char *); 175285128Straszstatic int check_dirpath(char *); 176285128Straszstatic int check_options(struct dirlist *); 177285128Straszstatic int checkmask(struct sockaddr *sa); 178285128Straszstatic int chk_host(struct dirlist *, struct sockaddr *, int *, int *, 179285128Strasz int *, int **); 180293305Sjpaetzelstatic char *strsep_quote(char **stringp, const char *delim); 181222623Srmacklemstatic int create_service(struct netconfig *nconf); 182222623Srmacklemstatic void complete_service(struct netconfig *nconf, char *port_str); 183222623Srmacklemstatic void clearout_service(void); 184285128Straszstatic void del_mlist(char *hostp, char *dirp); 185285128Straszstatic struct dirlist *dirp_search(struct dirlist *, char *); 186285128Straszstatic int do_mount(struct exportlist *, struct grouplist *, int, 187285128Strasz struct xucred *, char *, int, struct statfs *); 188285128Straszstatic int do_opt(char **, char **, struct exportlist *, 189285128Strasz struct grouplist *, int *, int *, struct xucred *); 190285128Straszstatic struct exportlist *ex_search(fsid_t *); 191285128Straszstatic struct exportlist *get_exp(void); 192285128Straszstatic void free_dir(struct dirlist *); 193285128Straszstatic void free_exp(struct exportlist *); 194285128Straszstatic void free_grp(struct grouplist *); 195285128Straszstatic void free_host(struct hostlist *); 196285128Straszstatic void get_exportlist(void); 197285128Straszstatic int get_host(char *, struct grouplist *, struct grouplist *); 198285128Straszstatic struct hostlist *get_ht(void); 199285128Straszstatic int get_line(void); 200285128Straszstatic void get_mountlist(void); 201285128Straszstatic int get_net(char *, struct netmsk *, int); 202329392Sbrdstatic void getexp_err(struct exportlist *, struct grouplist *, const char *); 203285128Straszstatic struct grouplist *get_grp(void); 204285128Straszstatic void hang_dirp(struct dirlist *, struct grouplist *, 20592882Simp struct exportlist *, int); 206285128Straszstatic void huphandler(int sig); 207285128Straszstatic int makemask(struct sockaddr_storage *ssp, int bitlen); 208285128Straszstatic void mntsrv(struct svc_req *, SVCXPRT *); 209285128Straszstatic void nextfield(char **, char **); 210285128Straszstatic void out_of_mem(void); 211285128Straszstatic void parsecred(char *, struct xucred *); 212285128Straszstatic int parsesec(char *, struct exportlist *); 213285128Straszstatic int put_exlist(struct dirlist *, XDR *, struct dirlist *, 214285128Strasz int *, int); 215285128Straszstatic void *sa_rawaddr(struct sockaddr *sa, int *nbytes); 216285128Straszstatic int sacmp(struct sockaddr *sa1, struct sockaddr *sa2, 217285128Strasz struct sockaddr *samask); 218285128Straszstatic int scan_tree(struct dirlist *, struct sockaddr *); 219285128Straszstatic void usage(void); 220285128Straszstatic int xdr_dir(XDR *, char *); 221285128Straszstatic int xdr_explist(XDR *, caddr_t); 222285128Straszstatic int xdr_explist_brief(XDR *, caddr_t); 223285128Straszstatic int xdr_explist_common(XDR *, caddr_t, int); 224285128Straszstatic int xdr_fhs(XDR *, caddr_t); 225285128Straszstatic int xdr_mlist(XDR *, caddr_t); 226285128Straszstatic void terminate(int); 2271558Srgrimes 228324955Smanustatic SLIST_HEAD(, exportlist) exphead = SLIST_HEAD_INITIALIZER(exphead); 229324955Smanustatic SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(mlhead); 230285128Straszstatic struct grouplist *grphead; 231285128Straszstatic char *exnames_default[2] = { _PATH_EXPORTS, NULL }; 232285128Straszstatic char **exnames; 233285128Straszstatic char **hosts = NULL; 234285128Straszstatic struct xucred def_anon = { 23591354Sdd XUCRED_VERSION, 23672650Sgreen (uid_t)-2, 2371558Srgrimes 1, 23872650Sgreen { (gid_t)-2 }, 23972650Sgreen NULL 2401558Srgrimes}; 241285128Straszstatic int force_v2 = 0; 242285128Straszstatic int resvport_only = 1; 243285128Straszstatic int nhosts = 0; 244285128Straszstatic int dir_only = 1; 245285128Straszstatic int dolog = 0; 246285128Straszstatic int got_sighup = 0; 247285128Straszstatic int xcreated = 0; 24874462Salfred 249285128Straszstatic char *svcport_str = NULL; 250285128Straszstatic int mallocd_svcport = 0; 251285128Straszstatic int *sock_fd; 252285128Straszstatic int sock_fdcnt; 253285128Straszstatic int sock_fdpos; 254285128Straszstatic int suspend_nfsd = 0; 255172827Smatteo 256285128Straszstatic int opt_flags; 25774462Salfredstatic int have_v6 = 1; 25874462Salfred 259285128Straszstatic int v4root_phase = 0; 260285128Straszstatic char v4root_dirpath[PATH_MAX + 1]; 261285128Straszstatic int has_publicfh = 0; 262192934Srmacklem 263285128Straszstatic struct pidfh *pfh = NULL; 26475801Siedowse/* Bits for opt_flags above */ 2651558Srgrimes#define OP_MAPROOT 0x01 2661558Srgrimes#define OP_MAPALL 0x02 26783653Speter/* 0x4 free */ 2681558Srgrimes#define OP_MASK 0x08 2691558Srgrimes#define OP_NET 0x10 2701558Srgrimes#define OP_ALLDIRS 0x40 27175801Siedowse#define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */ 272100336Sjoerg#define OP_QUIET 0x100 27374462Salfred#define OP_MASKLEN 0x200 274184588Sdfr#define OP_SEC 0x400 2751558Srgrimes 2761558Srgrimes#ifdef DEBUG 277285128Straszstatic int debug = 1; 278285128Straszstatic void SYSLOG(int, const char *, ...) __printflike(2, 3); 2791558Srgrimes#define syslog SYSLOG 2801558Srgrimes#else 281285128Straszstatic int debug = 0; 2821558Srgrimes#endif 2831558Srgrimes 2841558Srgrimes/* 285293305Sjpaetzel * Similar to strsep(), but it allows for quoted strings 286293305Sjpaetzel * and escaped characters. 287293305Sjpaetzel * 288293305Sjpaetzel * It returns the string (or NULL, if *stringp is NULL), 289293305Sjpaetzel * which is a de-quoted version of the string if necessary. 290293305Sjpaetzel * 291293305Sjpaetzel * It modifies *stringp in place. 292293305Sjpaetzel */ 293293305Sjpaetzelstatic char * 294293305Sjpaetzelstrsep_quote(char **stringp, const char *delim) 295293305Sjpaetzel{ 296293305Sjpaetzel char *srcptr, *dstptr, *retval; 297293305Sjpaetzel char quot = 0; 298293305Sjpaetzel 299293305Sjpaetzel if (stringp == NULL || *stringp == NULL) 300293305Sjpaetzel return (NULL); 301293305Sjpaetzel 302293305Sjpaetzel srcptr = dstptr = retval = *stringp; 303293305Sjpaetzel 304293305Sjpaetzel while (*srcptr) { 305293305Sjpaetzel /* 306293305Sjpaetzel * We're looking for several edge cases here. 307293305Sjpaetzel * First: if we're in quote state (quot != 0), 308293305Sjpaetzel * then we ignore the delim characters, but otherwise 309293305Sjpaetzel * process as normal, unless it is the quote character. 310293305Sjpaetzel * Second: if the current character is a backslash, 311293305Sjpaetzel * we take the next character as-is, without checking 312293305Sjpaetzel * for delim, quote, or backslash. Exception: if the 313293305Sjpaetzel * next character is a NUL, that's the end of the string. 314293305Sjpaetzel * Third: if the character is a quote character, we toggle 315293305Sjpaetzel * quote state. 316293305Sjpaetzel * Otherwise: check the current character for NUL, or 317293305Sjpaetzel * being in delim, and end the string if either is true. 318293305Sjpaetzel */ 319293305Sjpaetzel if (*srcptr == '\\') { 320293305Sjpaetzel srcptr++; 321293305Sjpaetzel /* 322293305Sjpaetzel * The edge case here is if the next character 323293305Sjpaetzel * is NUL, we want to stop processing. But if 324293305Sjpaetzel * it's not NUL, then we simply want to copy it. 325293305Sjpaetzel */ 326293305Sjpaetzel if (*srcptr) { 327293305Sjpaetzel *dstptr++ = *srcptr++; 328293305Sjpaetzel } 329293305Sjpaetzel continue; 330293305Sjpaetzel } 331293305Sjpaetzel if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) { 332293305Sjpaetzel quot = *srcptr++; 333293305Sjpaetzel continue; 334293305Sjpaetzel } 335293305Sjpaetzel if (quot && *srcptr == quot) { 336293305Sjpaetzel /* End of the quoted part */ 337293305Sjpaetzel quot = 0; 338293305Sjpaetzel srcptr++; 339293305Sjpaetzel continue; 340293305Sjpaetzel } 341293305Sjpaetzel if (!quot && strchr(delim, *srcptr)) 342293305Sjpaetzel break; 343293305Sjpaetzel *dstptr++ = *srcptr++; 344293305Sjpaetzel } 345293305Sjpaetzel 346293305Sjpaetzel *dstptr = 0; /* Terminate the string */ 347293305Sjpaetzel *stringp = (*srcptr == '\0') ? NULL : srcptr + 1; 348293305Sjpaetzel return (retval); 349293305Sjpaetzel} 350293305Sjpaetzel 351293305Sjpaetzel/* 3521558Srgrimes * Mountd server for NFS mount protocol as described in: 3531558Srgrimes * NFS: Network File System Protocol Specification, RFC1094, Appendix A 3541558Srgrimes * The optional arguments are the exports file name 3551558Srgrimes * default: _PATH_EXPORTS 3561558Srgrimes * and "-n" to allow nonroot mount. 3571558Srgrimes */ 3581558Srgrimesint 359216587Scharniermain(int argc, char **argv) 3601558Srgrimes{ 36175754Siedowse fd_set readfds; 362172827Smatteo struct netconfig *nconf; 363172827Smatteo char *endptr, **hosts_bak; 364172827Smatteo void *nc_handle; 365149433Spjd pid_t otherpid; 366172827Smatteo in_port_t svcport; 367172827Smatteo int c, k, s; 368109363Smbr int maxrec = RPC_MAXDATASIZE; 369222623Srmacklem int attempt_cnt, port_len, port_pos, ret; 370222623Srmacklem char **port_list; 3711558Srgrimes 37274462Salfred /* Check that another mountd isn't already running. */ 373150214Spjd pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid); 374149433Spjd if (pfh == NULL) { 375149433Spjd if (errno == EEXIST) 376149433Spjd errx(1, "mountd already running, pid: %d.", otherpid); 377149433Spjd warn("cannot open or create pidfile"); 378149433Spjd } 37974462Salfred 38074462Salfred s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); 38174462Salfred if (s < 0) 38274462Salfred have_v6 = 0; 38374462Salfred else 38474462Salfred close(s); 3852999Swollman 386282214Strasz while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1) 3871558Srgrimes switch (c) { 38825087Sdfr case '2': 38925087Sdfr force_v2 = 1; 39025087Sdfr break; 391192993Srmacklem case 'e': 392220980Srmacklem /* now a no-op, since this is the default */ 393192934Srmacklem break; 3949336Sdfr case 'n': 3959336Sdfr resvport_only = 0; 3969336Sdfr break; 3979336Sdfr case 'r': 3989336Sdfr dir_only = 0; 3999336Sdfr break; 4008688Sphk case 'd': 4018688Sphk debug = debug ? 0 : 1; 4028688Sphk break; 40331656Sguido case 'l': 404121767Speter dolog = 1; 40531656Sguido break; 406126572Sbms case 'p': 407126572Sbms endptr = NULL; 408126572Sbms svcport = (in_port_t)strtoul(optarg, &endptr, 10); 409126572Sbms if (endptr == NULL || *endptr != '\0' || 410126572Sbms svcport == 0 || svcport >= IPPORT_MAX) 411126572Sbms usage(); 412172827Smatteo svcport_str = strdup(optarg); 413126572Sbms break; 414172827Smatteo case 'h': 415172827Smatteo ++nhosts; 416172827Smatteo hosts_bak = hosts; 417172827Smatteo hosts_bak = realloc(hosts, nhosts * sizeof(char *)); 418172827Smatteo if (hosts_bak == NULL) { 419172827Smatteo if (hosts != NULL) { 420172827Smatteo for (k = 0; k < nhosts; k++) 421172827Smatteo free(hosts[k]); 422172827Smatteo free(hosts); 423172827Smatteo out_of_mem(); 424172827Smatteo } 425172827Smatteo } 426172827Smatteo hosts = hosts_bak; 427172827Smatteo hosts[nhosts - 1] = strdup(optarg); 428172827Smatteo if (hosts[nhosts - 1] == NULL) { 429172827Smatteo for (k = 0; k < (nhosts - 1); k++) 430172827Smatteo free(hosts[k]); 431172827Smatteo free(hosts); 432172827Smatteo out_of_mem(); 433172827Smatteo } 434172827Smatteo break; 435241568Srmacklem case 'S': 436241568Srmacklem suspend_nfsd = 1; 437241568Srmacklem break; 4381558Srgrimes default: 43937663Scharnier usage(); 440298089Spfg } 441192934Srmacklem 442282214Strasz if (modfind("nfsd") < 0) { 443192934Srmacklem /* Not present in kernel, try loading it */ 444282214Strasz if (kldload("nfsd") < 0 || modfind("nfsd") < 0) 445192934Srmacklem errx(1, "NFS server is not available"); 446192934Srmacklem } 447192934Srmacklem 4481558Srgrimes argc -= optind; 4491558Srgrimes argv += optind; 4501558Srgrimes grphead = (struct grouplist *)NULL; 451166440Spjd if (argc > 0) 452166440Spjd exnames = argv; 453166440Spjd else 454166440Spjd exnames = exnames_default; 4551558Srgrimes openlog("mountd", LOG_PID, LOG_DAEMON); 4561558Srgrimes if (debug) 45737663Scharnier warnx("getting export list"); 4581558Srgrimes get_exportlist(); 4591558Srgrimes if (debug) 46037663Scharnier warnx("getting mount list"); 4611558Srgrimes get_mountlist(); 4621558Srgrimes if (debug) 46337663Scharnier warnx("here we go"); 4641558Srgrimes if (debug == 0) { 4651558Srgrimes daemon(0, 0); 4661558Srgrimes signal(SIGINT, SIG_IGN); 4671558Srgrimes signal(SIGQUIT, SIG_IGN); 4681558Srgrimes } 46975754Siedowse signal(SIGHUP, huphandler); 47074462Salfred signal(SIGTERM, terminate); 471164394Srodrigc signal(SIGPIPE, SIG_IGN); 472149433Spjd 473149433Spjd pidfile_write(pfh); 474149433Spjd 475194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS, NULL); 476194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL); 477109363Smbr rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); 478109363Smbr 47924759Sguido if (!resvport_only) { 480308449Srmacklem if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL, 48183687Speter &resvport_only, sizeof(resvport_only)) != 0 && 48283687Speter errno != ENOENT) { 48324759Sguido syslog(LOG_ERR, "sysctl: %m"); 48424759Sguido exit(1); 48524759Sguido } 48624330Sguido } 487126572Sbms 488172827Smatteo /* 489172827Smatteo * If no hosts were specified, add a wildcard entry to bind to 490172827Smatteo * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the 491172827Smatteo * list. 492172827Smatteo */ 493172827Smatteo if (nhosts == 0) { 494292864Suqs hosts = malloc(sizeof(char *)); 495172827Smatteo if (hosts == NULL) 496172827Smatteo out_of_mem(); 497172827Smatteo hosts[0] = "*"; 498172827Smatteo nhosts = 1; 499172827Smatteo } else { 500172827Smatteo hosts_bak = hosts; 501172827Smatteo if (have_v6) { 502172827Smatteo hosts_bak = realloc(hosts, (nhosts + 2) * 503172827Smatteo sizeof(char *)); 504172827Smatteo if (hosts_bak == NULL) { 505172827Smatteo for (k = 0; k < nhosts; k++) 506172827Smatteo free(hosts[k]); 507172827Smatteo free(hosts); 508172827Smatteo out_of_mem(); 509172827Smatteo } else 510172827Smatteo hosts = hosts_bak; 511172827Smatteo nhosts += 2; 512172827Smatteo hosts[nhosts - 2] = "::1"; 513172827Smatteo } else { 514172827Smatteo hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *)); 515172827Smatteo if (hosts_bak == NULL) { 516172827Smatteo for (k = 0; k < nhosts; k++) 517172827Smatteo free(hosts[k]); 518172827Smatteo free(hosts); 519172827Smatteo out_of_mem(); 520172827Smatteo } else { 521172827Smatteo nhosts += 1; 522172827Smatteo hosts = hosts_bak; 523126572Sbms } 524172827Smatteo } 52574462Salfred 526172827Smatteo hosts[nhosts - 1] = "127.0.0.1"; 52774462Salfred } 52874462Salfred 529222623Srmacklem attempt_cnt = 1; 530222623Srmacklem sock_fdcnt = 0; 531222623Srmacklem sock_fd = NULL; 532222623Srmacklem port_list = NULL; 533222623Srmacklem port_len = 0; 534172827Smatteo nc_handle = setnetconfig(); 535172827Smatteo while ((nconf = getnetconfig(nc_handle))) { 536172827Smatteo if (nconf->nc_flag & NC_VISIBLE) { 537172827Smatteo if (have_v6 == 0 && strcmp(nconf->nc_protofmly, 538172827Smatteo "inet6") == 0) { 539172827Smatteo /* DO NOTHING */ 540222623Srmacklem } else { 541222623Srmacklem ret = create_service(nconf); 542222623Srmacklem if (ret == 1) 543222623Srmacklem /* Ignore this call */ 544222623Srmacklem continue; 545222623Srmacklem if (ret < 0) { 546222623Srmacklem /* 547222623Srmacklem * Failed to bind port, so close off 548222623Srmacklem * all sockets created and try again 549222623Srmacklem * if the port# was dynamically 550222623Srmacklem * assigned via bind(2). 551222623Srmacklem */ 552222623Srmacklem clearout_service(); 553222623Srmacklem if (mallocd_svcport != 0 && 554222623Srmacklem attempt_cnt < GETPORT_MAXTRY) { 555222623Srmacklem free(svcport_str); 556222623Srmacklem svcport_str = NULL; 557222623Srmacklem mallocd_svcport = 0; 558222623Srmacklem } else { 559222623Srmacklem errno = EADDRINUSE; 560222623Srmacklem syslog(LOG_ERR, 561222623Srmacklem "bindresvport_sa: %m"); 562222623Srmacklem exit(1); 563222623Srmacklem } 564222623Srmacklem 565222623Srmacklem /* Start over at the first service. */ 566222623Srmacklem free(sock_fd); 567222623Srmacklem sock_fdcnt = 0; 568222623Srmacklem sock_fd = NULL; 569222623Srmacklem nc_handle = setnetconfig(); 570222623Srmacklem attempt_cnt++; 571222623Srmacklem } else if (mallocd_svcport != 0 && 572222623Srmacklem attempt_cnt == GETPORT_MAXTRY) { 573222623Srmacklem /* 574222623Srmacklem * For the last attempt, allow 575222623Srmacklem * different port #s for each nconf 576222623Srmacklem * by saving the svcport_str and 577222623Srmacklem * setting it back to NULL. 578222623Srmacklem */ 579222623Srmacklem port_list = realloc(port_list, 580222623Srmacklem (port_len + 1) * sizeof(char *)); 581222623Srmacklem if (port_list == NULL) 582222623Srmacklem out_of_mem(); 583222623Srmacklem port_list[port_len++] = svcport_str; 584222623Srmacklem svcport_str = NULL; 585222623Srmacklem mallocd_svcport = 0; 586222623Srmacklem } 587222623Srmacklem } 588222623Srmacklem } 589222623Srmacklem } 590222623Srmacklem 591222623Srmacklem /* 592222623Srmacklem * Successfully bound the ports, so call complete_service() to 593222623Srmacklem * do the rest of the setup on the service(s). 594222623Srmacklem */ 595222623Srmacklem sock_fdpos = 0; 596222623Srmacklem port_pos = 0; 597222623Srmacklem nc_handle = setnetconfig(); 598222623Srmacklem while ((nconf = getnetconfig(nc_handle))) { 599222623Srmacklem if (nconf->nc_flag & NC_VISIBLE) { 600222623Srmacklem if (have_v6 == 0 && strcmp(nconf->nc_protofmly, 601222623Srmacklem "inet6") == 0) { 602222623Srmacklem /* DO NOTHING */ 603222623Srmacklem } else if (port_list != NULL) { 604222623Srmacklem if (port_pos >= port_len) { 605222623Srmacklem syslog(LOG_ERR, "too many port#s"); 606222623Srmacklem exit(1); 607222623Srmacklem } 608222623Srmacklem complete_service(nconf, port_list[port_pos++]); 609172827Smatteo } else 610222623Srmacklem complete_service(nconf, svcport_str); 611172827Smatteo } 61274462Salfred } 613172827Smatteo endnetconfig(nc_handle); 614222623Srmacklem free(sock_fd); 615222623Srmacklem if (port_list != NULL) { 616222623Srmacklem for (port_pos = 0; port_pos < port_len; port_pos++) 617222623Srmacklem free(port_list[port_pos]); 618222623Srmacklem free(port_list); 619222623Srmacklem } 62074462Salfred 62174462Salfred if (xcreated == 0) { 62274462Salfred syslog(LOG_ERR, "could not create any services"); 6231558Srgrimes exit(1); 6241558Srgrimes } 62575754Siedowse 62675754Siedowse /* Expand svc_run() here so that we can call get_exportlist(). */ 62775754Siedowse for (;;) { 62875754Siedowse if (got_sighup) { 62975754Siedowse get_exportlist(); 63075754Siedowse got_sighup = 0; 63175754Siedowse } 63275754Siedowse readfds = svc_fdset; 63375754Siedowse switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) { 63475754Siedowse case -1: 63575754Siedowse if (errno == EINTR) 63675754Siedowse continue; 63775754Siedowse syslog(LOG_ERR, "mountd died: select: %m"); 63875754Siedowse exit(1); 63975754Siedowse case 0: 64075754Siedowse continue; 64175754Siedowse default: 64275754Siedowse svc_getreqset(&readfds); 64375754Siedowse } 64475754Siedowse } 645172827Smatteo} 646172827Smatteo 647172827Smatteo/* 648172827Smatteo * This routine creates and binds sockets on the appropriate 649222623Srmacklem * addresses. It gets called one time for each transport. 650222623Srmacklem * It returns 0 upon success, 1 for ingore the call and -1 to indicate 651222623Srmacklem * bind failed with EADDRINUSE. 652222623Srmacklem * Any file descriptors that have been created are stored in sock_fd and 653222623Srmacklem * the total count of them is maintained in sock_fdcnt. 654172827Smatteo */ 655222623Srmacklemstatic int 656172827Smatteocreate_service(struct netconfig *nconf) 657172827Smatteo{ 658172827Smatteo struct addrinfo hints, *res = NULL; 659172827Smatteo struct sockaddr_in *sin; 660172827Smatteo struct sockaddr_in6 *sin6; 661172827Smatteo struct __rpc_sockinfo si; 662172827Smatteo int aicode; 663172827Smatteo int fd; 664172827Smatteo int nhostsbak; 665172827Smatteo int one = 1; 666172827Smatteo int r; 667172827Smatteo u_int32_t host_addr[4]; /* IPv4 or IPv6 */ 668222623Srmacklem int mallocd_res; 669172827Smatteo 670172827Smatteo if ((nconf->nc_semantics != NC_TPI_CLTS) && 671172827Smatteo (nconf->nc_semantics != NC_TPI_COTS) && 672172827Smatteo (nconf->nc_semantics != NC_TPI_COTS_ORD)) 673222623Srmacklem return (1); /* not my type */ 674172827Smatteo 675172827Smatteo /* 676172827Smatteo * XXX - using RPC library internal functions. 677172827Smatteo */ 678172827Smatteo if (!__rpc_nconf2sockinfo(nconf, &si)) { 679172827Smatteo syslog(LOG_ERR, "cannot get information for %s", 680172827Smatteo nconf->nc_netid); 681222623Srmacklem return (1); 682172827Smatteo } 683172827Smatteo 684172827Smatteo /* Get mountd's address on this transport */ 685172827Smatteo memset(&hints, 0, sizeof hints); 686172827Smatteo hints.ai_family = si.si_af; 687172827Smatteo hints.ai_socktype = si.si_socktype; 688172827Smatteo hints.ai_protocol = si.si_proto; 689172827Smatteo 690172827Smatteo /* 691172827Smatteo * Bind to specific IPs if asked to 692172827Smatteo */ 693172827Smatteo nhostsbak = nhosts; 694172827Smatteo while (nhostsbak > 0) { 695172827Smatteo --nhostsbak; 696222623Srmacklem sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int)); 697222623Srmacklem if (sock_fd == NULL) 698222623Srmacklem out_of_mem(); 699222623Srmacklem sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */ 700222623Srmacklem mallocd_res = 0; 701222623Srmacklem 702277352Srstone hints.ai_flags = AI_PASSIVE; 703277352Srstone 704172827Smatteo /* 705172827Smatteo * XXX - using RPC library internal functions. 706172827Smatteo */ 707172827Smatteo if ((fd = __rpc_nconf2fd(nconf)) < 0) { 708172827Smatteo int non_fatal = 0; 709244538Skevlo if (errno == EAFNOSUPPORT && 710172827Smatteo nconf->nc_semantics != NC_TPI_CLTS) 711172827Smatteo non_fatal = 1; 712172827Smatteo 713172827Smatteo syslog(non_fatal ? LOG_DEBUG : LOG_ERR, 714172827Smatteo "cannot create socket for %s", nconf->nc_netid); 715222623Srmacklem if (non_fatal != 0) 716222623Srmacklem continue; 717222623Srmacklem exit(1); 718172827Smatteo } 719172827Smatteo 720172827Smatteo switch (hints.ai_family) { 721172827Smatteo case AF_INET: 722172827Smatteo if (inet_pton(AF_INET, hosts[nhostsbak], 723172827Smatteo host_addr) == 1) { 724222623Srmacklem hints.ai_flags |= AI_NUMERICHOST; 725172827Smatteo } else { 726172827Smatteo /* 727172827Smatteo * Skip if we have an AF_INET6 address. 728172827Smatteo */ 729172827Smatteo if (inet_pton(AF_INET6, hosts[nhostsbak], 730172827Smatteo host_addr) == 1) { 731172827Smatteo close(fd); 732172827Smatteo continue; 733172827Smatteo } 734172827Smatteo } 735172827Smatteo break; 736172827Smatteo case AF_INET6: 737172827Smatteo if (inet_pton(AF_INET6, hosts[nhostsbak], 738172827Smatteo host_addr) == 1) { 739222623Srmacklem hints.ai_flags |= AI_NUMERICHOST; 740172827Smatteo } else { 741172827Smatteo /* 742172827Smatteo * Skip if we have an AF_INET address. 743172827Smatteo */ 744172827Smatteo if (inet_pton(AF_INET, hosts[nhostsbak], 745172827Smatteo host_addr) == 1) { 746172827Smatteo close(fd); 747172827Smatteo continue; 748172827Smatteo } 749172827Smatteo } 750172827Smatteo 751172827Smatteo /* 752172827Smatteo * We're doing host-based access checks here, so don't 753172827Smatteo * allow v4-in-v6 to confuse things. The kernel will 754172827Smatteo * disable it by default on NFS sockets too. 755172827Smatteo */ 756172827Smatteo if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, 757172827Smatteo sizeof one) < 0) { 758172827Smatteo syslog(LOG_ERR, 759172827Smatteo "can't disable v4-in-v6 on IPv6 socket"); 760172827Smatteo exit(1); 761172827Smatteo } 762172827Smatteo break; 763172827Smatteo default: 764172827Smatteo break; 765172827Smatteo } 766172827Smatteo 767172827Smatteo /* 768172827Smatteo * If no hosts were specified, just bind to INADDR_ANY 769172827Smatteo */ 770172827Smatteo if (strcmp("*", hosts[nhostsbak]) == 0) { 771172827Smatteo if (svcport_str == NULL) { 772172827Smatteo res = malloc(sizeof(struct addrinfo)); 773172827Smatteo if (res == NULL) 774172827Smatteo out_of_mem(); 775222623Srmacklem mallocd_res = 1; 776172827Smatteo res->ai_flags = hints.ai_flags; 777172827Smatteo res->ai_family = hints.ai_family; 778172827Smatteo res->ai_protocol = hints.ai_protocol; 779172827Smatteo switch (res->ai_family) { 780172827Smatteo case AF_INET: 781172827Smatteo sin = malloc(sizeof(struct sockaddr_in)); 782172827Smatteo if (sin == NULL) 783172827Smatteo out_of_mem(); 784172827Smatteo sin->sin_family = AF_INET; 785172827Smatteo sin->sin_port = htons(0); 786172827Smatteo sin->sin_addr.s_addr = htonl(INADDR_ANY); 787172827Smatteo res->ai_addr = (struct sockaddr*) sin; 788172827Smatteo res->ai_addrlen = (socklen_t) 789222623Srmacklem sizeof(struct sockaddr_in); 790172827Smatteo break; 791172827Smatteo case AF_INET6: 792172827Smatteo sin6 = malloc(sizeof(struct sockaddr_in6)); 793173056Ssimon if (sin6 == NULL) 794172827Smatteo out_of_mem(); 795172827Smatteo sin6->sin6_family = AF_INET6; 796172827Smatteo sin6->sin6_port = htons(0); 797172827Smatteo sin6->sin6_addr = in6addr_any; 798172827Smatteo res->ai_addr = (struct sockaddr*) sin6; 799172827Smatteo res->ai_addrlen = (socklen_t) 800222623Srmacklem sizeof(struct sockaddr_in6); 801222623Srmacklem break; 802172827Smatteo default: 803222623Srmacklem syslog(LOG_ERR, "bad addr fam %d", 804222623Srmacklem res->ai_family); 805222623Srmacklem exit(1); 806172827Smatteo } 807172827Smatteo } else { 808172827Smatteo if ((aicode = getaddrinfo(NULL, svcport_str, 809172827Smatteo &hints, &res)) != 0) { 810172827Smatteo syslog(LOG_ERR, 811172827Smatteo "cannot get local address for %s: %s", 812172827Smatteo nconf->nc_netid, 813172827Smatteo gai_strerror(aicode)); 814222623Srmacklem close(fd); 815172827Smatteo continue; 816172827Smatteo } 817172827Smatteo } 818172827Smatteo } else { 819172827Smatteo if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str, 820172827Smatteo &hints, &res)) != 0) { 821172827Smatteo syslog(LOG_ERR, 822172827Smatteo "cannot get local address for %s: %s", 823172827Smatteo nconf->nc_netid, gai_strerror(aicode)); 824222623Srmacklem close(fd); 825172827Smatteo continue; 826172827Smatteo } 827172827Smatteo } 828172827Smatteo 829222623Srmacklem /* Store the fd. */ 830222623Srmacklem sock_fd[sock_fdcnt - 1] = fd; 831222623Srmacklem 832222623Srmacklem /* Now, attempt the bind. */ 833172827Smatteo r = bindresvport_sa(fd, res->ai_addr); 834172827Smatteo if (r != 0) { 835222623Srmacklem if (errno == EADDRINUSE && mallocd_svcport != 0) { 836222623Srmacklem if (mallocd_res != 0) { 837222623Srmacklem free(res->ai_addr); 838222623Srmacklem free(res); 839222623Srmacklem } else 840222623Srmacklem freeaddrinfo(res); 841222623Srmacklem return (-1); 842222623Srmacklem } 843172827Smatteo syslog(LOG_ERR, "bindresvport_sa: %m"); 844172827Smatteo exit(1); 845172827Smatteo } 846172827Smatteo 847222623Srmacklem if (svcport_str == NULL) { 848222623Srmacklem svcport_str = malloc(NI_MAXSERV * sizeof(char)); 849222623Srmacklem if (svcport_str == NULL) 850222623Srmacklem out_of_mem(); 851222623Srmacklem mallocd_svcport = 1; 852222623Srmacklem 853222623Srmacklem if (getnameinfo(res->ai_addr, 854222623Srmacklem res->ai_addr->sa_len, NULL, NI_MAXHOST, 855222623Srmacklem svcport_str, NI_MAXSERV * sizeof(char), 856222623Srmacklem NI_NUMERICHOST | NI_NUMERICSERV)) 857222623Srmacklem errx(1, "Cannot get port number"); 858222623Srmacklem } 859222623Srmacklem if (mallocd_res != 0) { 860222623Srmacklem free(res->ai_addr); 861222623Srmacklem free(res); 862222623Srmacklem } else 863222623Srmacklem freeaddrinfo(res); 864222623Srmacklem res = NULL; 865222623Srmacklem } 866222623Srmacklem return (0); 867222623Srmacklem} 868222623Srmacklem 869222623Srmacklem/* 870222623Srmacklem * Called after all the create_service() calls have succeeded, to complete 871222623Srmacklem * the setup and registration. 872222623Srmacklem */ 873222623Srmacklemstatic void 874222623Srmacklemcomplete_service(struct netconfig *nconf, char *port_str) 875222623Srmacklem{ 876222623Srmacklem struct addrinfo hints, *res = NULL; 877222623Srmacklem struct __rpc_sockinfo si; 878222623Srmacklem struct netbuf servaddr; 879222623Srmacklem SVCXPRT *transp = NULL; 880222623Srmacklem int aicode, fd, nhostsbak; 881222623Srmacklem int registered = 0; 882222623Srmacklem 883222623Srmacklem if ((nconf->nc_semantics != NC_TPI_CLTS) && 884222623Srmacklem (nconf->nc_semantics != NC_TPI_COTS) && 885222623Srmacklem (nconf->nc_semantics != NC_TPI_COTS_ORD)) 886222623Srmacklem return; /* not my type */ 887222623Srmacklem 888222623Srmacklem /* 889222623Srmacklem * XXX - using RPC library internal functions. 890222623Srmacklem */ 891222623Srmacklem if (!__rpc_nconf2sockinfo(nconf, &si)) { 892222623Srmacklem syslog(LOG_ERR, "cannot get information for %s", 893222623Srmacklem nconf->nc_netid); 894222623Srmacklem return; 895222623Srmacklem } 896222623Srmacklem 897222623Srmacklem nhostsbak = nhosts; 898222623Srmacklem while (nhostsbak > 0) { 899222623Srmacklem --nhostsbak; 900222623Srmacklem if (sock_fdpos >= sock_fdcnt) { 901222623Srmacklem /* Should never happen. */ 902222623Srmacklem syslog(LOG_ERR, "Ran out of socket fd's"); 903222623Srmacklem return; 904222623Srmacklem } 905222623Srmacklem fd = sock_fd[sock_fdpos++]; 906222623Srmacklem if (fd < 0) 907222623Srmacklem continue; 908222623Srmacklem 909341171Ssef /* 910341171Ssef * Using -1 tells listen(2) to use 911341171Ssef * kern.ipc.soacceptqueue for the backlog. 912341171Ssef */ 913172827Smatteo if (nconf->nc_semantics != NC_TPI_CLTS) 914341171Ssef listen(fd, -1); 915172827Smatteo 916172827Smatteo if (nconf->nc_semantics == NC_TPI_CLTS ) 917172827Smatteo transp = svc_dg_create(fd, 0, 0); 918172827Smatteo else 919172827Smatteo transp = svc_vc_create(fd, RPC_MAXDATASIZE, 920172827Smatteo RPC_MAXDATASIZE); 921172827Smatteo 922172827Smatteo if (transp != (SVCXPRT *) NULL) { 923194880Sdfr if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv, 924172827Smatteo NULL)) 925172827Smatteo syslog(LOG_ERR, 926194880Sdfr "can't register %s MOUNTVERS service", 927172827Smatteo nconf->nc_netid); 928172827Smatteo if (!force_v2) { 929194880Sdfr if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3, 930172827Smatteo mntsrv, NULL)) 931172827Smatteo syslog(LOG_ERR, 932194880Sdfr "can't register %s MOUNTVERS3 service", 933172827Smatteo nconf->nc_netid); 934172827Smatteo } 935172827Smatteo } else 936172827Smatteo syslog(LOG_WARNING, "can't create %s services", 937172827Smatteo nconf->nc_netid); 938172827Smatteo 939172827Smatteo if (registered == 0) { 940172827Smatteo registered = 1; 941172827Smatteo memset(&hints, 0, sizeof hints); 942172827Smatteo hints.ai_flags = AI_PASSIVE; 943172827Smatteo hints.ai_family = si.si_af; 944172827Smatteo hints.ai_socktype = si.si_socktype; 945172827Smatteo hints.ai_protocol = si.si_proto; 946172827Smatteo 947222623Srmacklem if ((aicode = getaddrinfo(NULL, port_str, &hints, 948172827Smatteo &res)) != 0) { 949172827Smatteo syslog(LOG_ERR, "cannot get local address: %s", 950172827Smatteo gai_strerror(aicode)); 951172827Smatteo exit(1); 952172827Smatteo } 953172827Smatteo 954172827Smatteo servaddr.buf = malloc(res->ai_addrlen); 955172827Smatteo memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen); 956172827Smatteo servaddr.len = res->ai_addrlen; 957172827Smatteo 958194880Sdfr rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr); 959194880Sdfr rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr); 960172827Smatteo 961172827Smatteo xcreated++; 962172827Smatteo freeaddrinfo(res); 963172827Smatteo } 964172827Smatteo } /* end while */ 9651558Srgrimes} 9661558Srgrimes 967222623Srmacklem/* 968222623Srmacklem * Clear out sockets after a failure to bind one of them, so that the 969222623Srmacklem * cycle of socket creation/binding can start anew. 970222623Srmacklem */ 97137663Scharnierstatic void 972222623Srmacklemclearout_service(void) 973222623Srmacklem{ 974222623Srmacklem int i; 975222623Srmacklem 976222623Srmacklem for (i = 0; i < sock_fdcnt; i++) { 977222623Srmacklem if (sock_fd[i] >= 0) { 978222623Srmacklem shutdown(sock_fd[i], SHUT_RDWR); 979222623Srmacklem close(sock_fd[i]); 980222623Srmacklem } 981222623Srmacklem } 982222623Srmacklem} 983222623Srmacklem 984222623Srmacklemstatic void 985216587Scharnierusage(void) 98637663Scharnier{ 98737663Scharnier fprintf(stderr, 988192993Srmacklem "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] " 989241568Srmacklem "[-S] [-h <bindip>] [export_file ...]\n"); 99037663Scharnier exit(1); 99137663Scharnier} 99237663Scharnier 9931558Srgrimes/* 9941558Srgrimes * The mount rpc service 9951558Srgrimes */ 9961558Srgrimesvoid 997216587Scharniermntsrv(struct svc_req *rqstp, SVCXPRT *transp) 9981558Srgrimes{ 9991558Srgrimes struct exportlist *ep; 10001558Srgrimes struct dirlist *dp; 10019336Sdfr struct fhreturn fhr; 10021558Srgrimes struct stat stb; 10031558Srgrimes struct statfs fsb; 100474462Salfred char host[NI_MAXHOST], numerichost[NI_MAXHOST]; 100574462Salfred int lookup_failed = 1; 100674462Salfred struct sockaddr *saddr; 10079336Sdfr u_short sport; 1008194880Sdfr char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN]; 100928911Sguido int bad = 0, defset, hostset; 10109336Sdfr sigset_t sighup_mask; 1011240902Srmacklem int numsecflavors, *secflavorsp; 10121558Srgrimes 10139336Sdfr sigemptyset(&sighup_mask); 10149336Sdfr sigaddset(&sighup_mask, SIGHUP); 101574462Salfred saddr = svc_getrpccaller(transp)->buf; 101674462Salfred switch (saddr->sa_family) { 101774462Salfred case AF_INET6: 101875635Siedowse sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); 101974462Salfred break; 102074462Salfred case AF_INET: 102175635Siedowse sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); 102274462Salfred break; 102374462Salfred default: 102474462Salfred syslog(LOG_ERR, "request from unknown address family"); 102574462Salfred return; 102674462Salfred } 1027346769Smav switch (rqstp->rq_proc) { 1028346769Smav case MOUNTPROC_MNT: 1029346769Smav case MOUNTPROC_UMNT: 1030346769Smav case MOUNTPROC_UMNTALL: 1031346769Smav lookup_failed = getnameinfo(saddr, saddr->sa_len, host, 1032346769Smav sizeof host, NULL, 0, 0); 1033346769Smav } 103474462Salfred getnameinfo(saddr, saddr->sa_len, numerichost, 103574462Salfred sizeof numerichost, NULL, 0, NI_NUMERICHOST); 10361558Srgrimes switch (rqstp->rq_proc) { 10371558Srgrimes case NULLPROC: 1038121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL)) 103937663Scharnier syslog(LOG_ERR, "can't send reply"); 10401558Srgrimes return; 1041194880Sdfr case MOUNTPROC_MNT: 10429336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 104331656Sguido syslog(LOG_NOTICE, 104431656Sguido "mount request from %s from unprivileged port", 104574462Salfred numerichost); 10461558Srgrimes svcerr_weakauth(transp); 10471558Srgrimes return; 10481558Srgrimes } 1049121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 105031656Sguido syslog(LOG_NOTICE, "undecodable mount request from %s", 105174462Salfred numerichost); 10521558Srgrimes svcerr_decode(transp); 10531558Srgrimes return; 10541558Srgrimes } 10551558Srgrimes 10561558Srgrimes /* 10571558Srgrimes * Get the real pathname and make sure it is a directory 10589336Sdfr * or a regular file if the -r option was specified 10599336Sdfr * and it exists. 10601558Srgrimes */ 106151968Salfred if (realpath(rpcpath, dirpath) == NULL || 10621558Srgrimes stat(dirpath, &stb) < 0 || 10631558Srgrimes statfs(dirpath, &fsb) < 0) { 10641558Srgrimes chdir("/"); /* Just in case realpath doesn't */ 106531656Sguido syslog(LOG_NOTICE, 106637663Scharnier "mount request from %s for non existent path %s", 106774462Salfred numerichost, dirpath); 10681558Srgrimes if (debug) 106937663Scharnier warnx("stat failed on %s", dirpath); 107028911Sguido bad = ENOENT; /* We will send error reply later */ 10711558Srgrimes } 1072330092Srpokala if (!bad && 1073330092Srpokala !S_ISDIR(stb.st_mode) && 1074330092Srpokala (dir_only || !S_ISREG(stb.st_mode))) { 1075330092Srpokala syslog(LOG_NOTICE, 1076330092Srpokala "mount request from %s for non-directory path %s", 1077330092Srpokala numerichost, dirpath); 1078330092Srpokala if (debug) 1079330092Srpokala warnx("mounting non-directory %s", dirpath); 1080330092Srpokala bad = ENOTDIR; /* We will send error reply later */ 1081330092Srpokala } 10821558Srgrimes 10831558Srgrimes /* Check in the exports list */ 10849336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 1085330092Srpokala if (bad) 1086330092Srpokala ep = NULL; 1087330092Srpokala else 1088330092Srpokala ep = ex_search(&fsb.f_fsid); 10899336Sdfr hostset = defset = 0; 1090240902Srmacklem if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset, 1091240902Srmacklem &numsecflavors, &secflavorsp) || 10921558Srgrimes ((dp = dirp_search(ep->ex_dirl, dirpath)) && 1093240902Srmacklem chk_host(dp, saddr, &defset, &hostset, &numsecflavors, 1094240902Srmacklem &secflavorsp)) || 109574462Salfred (defset && scan_tree(ep->ex_defdir, saddr) == 0 && 109674462Salfred scan_tree(ep->ex_dirl, saddr) == 0))) { 109728911Sguido if (bad) { 1098121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 109928911Sguido (caddr_t)&bad)) 110037663Scharnier syslog(LOG_ERR, "can't send reply"); 110128911Sguido sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 110228911Sguido return; 110328911Sguido } 1104240902Srmacklem if (hostset & DP_HOSTSET) { 11059336Sdfr fhr.fhr_flag = hostset; 1106240902Srmacklem fhr.fhr_numsecflavors = numsecflavors; 1107240902Srmacklem fhr.fhr_secflavors = secflavorsp; 1108240902Srmacklem } else { 11099336Sdfr fhr.fhr_flag = defset; 1110240902Srmacklem fhr.fhr_numsecflavors = ep->ex_defnumsecflavors; 1111240902Srmacklem fhr.fhr_secflavors = ep->ex_defsecflavors; 1112240902Srmacklem } 11139336Sdfr fhr.fhr_vers = rqstp->rq_vers; 11141558Srgrimes /* Get the file handle */ 111523681Speter memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); 11169336Sdfr if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { 11171558Srgrimes bad = errno; 111837663Scharnier syslog(LOG_ERR, "can't get fh for %s", dirpath); 1119121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_long, 11201558Srgrimes (caddr_t)&bad)) 112137663Scharnier syslog(LOG_ERR, "can't send reply"); 11229336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 11231558Srgrimes return; 11241558Srgrimes } 1125121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, 1126121556Speter (caddr_t)&fhr)) 112737663Scharnier syslog(LOG_ERR, "can't send reply"); 112874462Salfred if (!lookup_failed) 112974462Salfred add_mlist(host, dirpath); 11301558Srgrimes else 113174462Salfred add_mlist(numerichost, dirpath); 11321558Srgrimes if (debug) 113337663Scharnier warnx("mount successful"); 1134121767Speter if (dolog) 113531656Sguido syslog(LOG_NOTICE, 113631656Sguido "mount request succeeded from %s for %s", 113774462Salfred numerichost, dirpath); 113831656Sguido } else { 1139330092Srpokala if (!bad) 1140330092Srpokala bad = EACCES; 114131656Sguido syslog(LOG_NOTICE, 114231656Sguido "mount request denied from %s for %s", 114374462Salfred numerichost, dirpath); 114431656Sguido } 114528911Sguido 1146121556Speter if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, 1147121556Speter (caddr_t)&bad)) 114837663Scharnier syslog(LOG_ERR, "can't send reply"); 11499336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 11501558Srgrimes return; 1151194880Sdfr case MOUNTPROC_DUMP: 1152121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL)) 115337663Scharnier syslog(LOG_ERR, "can't send reply"); 1154121767Speter else if (dolog) 115531656Sguido syslog(LOG_NOTICE, 115631656Sguido "dump request succeeded from %s", 115774462Salfred numerichost); 11581558Srgrimes return; 1159194880Sdfr case MOUNTPROC_UMNT: 11609336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 116131656Sguido syslog(LOG_NOTICE, 116231656Sguido "umount request from %s from unprivileged port", 116374462Salfred numerichost); 11641558Srgrimes svcerr_weakauth(transp); 11651558Srgrimes return; 11661558Srgrimes } 1167121556Speter if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { 116831656Sguido syslog(LOG_NOTICE, "undecodable umount request from %s", 116974462Salfred numerichost); 11701558Srgrimes svcerr_decode(transp); 11711558Srgrimes return; 11721558Srgrimes } 117351968Salfred if (realpath(rpcpath, dirpath) == NULL) { 117451968Salfred syslog(LOG_NOTICE, "umount request from %s " 117551968Salfred "for non existent path %s", 117674462Salfred numerichost, dirpath); 117751968Salfred } 1178121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 117937663Scharnier syslog(LOG_ERR, "can't send reply"); 118074462Salfred if (!lookup_failed) 118175635Siedowse del_mlist(host, dirpath); 118275635Siedowse del_mlist(numerichost, dirpath); 1183121767Speter if (dolog) 118431656Sguido syslog(LOG_NOTICE, 118531656Sguido "umount request succeeded from %s for %s", 118674462Salfred numerichost, dirpath); 11871558Srgrimes return; 1188194880Sdfr case MOUNTPROC_UMNTALL: 11899336Sdfr if (sport >= IPPORT_RESERVED && resvport_only) { 119031656Sguido syslog(LOG_NOTICE, 119131656Sguido "umountall request from %s from unprivileged port", 119274462Salfred numerichost); 11931558Srgrimes svcerr_weakauth(transp); 11941558Srgrimes return; 11951558Srgrimes } 1196121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) 119737663Scharnier syslog(LOG_ERR, "can't send reply"); 119874462Salfred if (!lookup_failed) 119975635Siedowse del_mlist(host, NULL); 120075635Siedowse del_mlist(numerichost, NULL); 1201121767Speter if (dolog) 120231656Sguido syslog(LOG_NOTICE, 120331656Sguido "umountall request succeeded from %s", 120474462Salfred numerichost); 12051558Srgrimes return; 1206194880Sdfr case MOUNTPROC_EXPORT: 1207121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL)) 1208121556Speter if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, 1209121556Speter (caddr_t)NULL)) 1210100117Salfred syslog(LOG_ERR, "can't send reply"); 1211121767Speter if (dolog) 121231656Sguido syslog(LOG_NOTICE, 121331656Sguido "export request succeeded from %s", 121474462Salfred numerichost); 12151558Srgrimes return; 12161558Srgrimes default: 12171558Srgrimes svcerr_noproc(transp); 12181558Srgrimes return; 12191558Srgrimes } 12201558Srgrimes} 12211558Srgrimes 12221558Srgrimes/* 12231558Srgrimes * Xdr conversion for a dirpath string 12241558Srgrimes */ 1225285128Straszstatic int 1226216587Scharnierxdr_dir(XDR *xdrsp, char *dirp) 12271558Srgrimes{ 1228194880Sdfr return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); 12291558Srgrimes} 12301558Srgrimes 12311558Srgrimes/* 12329336Sdfr * Xdr routine to generate file handle reply 12331558Srgrimes */ 1234285128Straszstatic int 1235216587Scharnierxdr_fhs(XDR *xdrsp, caddr_t cp) 12361558Srgrimes{ 123792806Sobrien struct fhreturn *fhrp = (struct fhreturn *)cp; 12389336Sdfr u_long ok = 0, len, auth; 1239184588Sdfr int i; 12401558Srgrimes 12411558Srgrimes if (!xdr_long(xdrsp, &ok)) 12421558Srgrimes return (0); 12439336Sdfr switch (fhrp->fhr_vers) { 12449336Sdfr case 1: 12459336Sdfr return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); 12469336Sdfr case 3: 12479336Sdfr len = NFSX_V3FH; 12489336Sdfr if (!xdr_long(xdrsp, &len)) 12499336Sdfr return (0); 12509336Sdfr if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) 12519336Sdfr return (0); 1252184588Sdfr if (fhrp->fhr_numsecflavors) { 1253184588Sdfr if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors)) 1254184588Sdfr return (0); 1255184588Sdfr for (i = 0; i < fhrp->fhr_numsecflavors; i++) 1256184588Sdfr if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i])) 1257184588Sdfr return (0); 1258184588Sdfr return (1); 1259184588Sdfr } else { 1260184588Sdfr auth = AUTH_SYS; 1261184588Sdfr len = 1; 1262184588Sdfr if (!xdr_long(xdrsp, &len)) 1263184588Sdfr return (0); 1264184588Sdfr return (xdr_long(xdrsp, &auth)); 1265184588Sdfr } 1266298089Spfg } 12679336Sdfr return (0); 12681558Srgrimes} 12691558Srgrimes 1270285128Straszstatic int 1271216587Scharnierxdr_mlist(XDR *xdrsp, caddr_t cp __unused) 12721558Srgrimes{ 12731558Srgrimes struct mountlist *mlp; 12741558Srgrimes int true = 1; 12751558Srgrimes int false = 0; 12761558Srgrimes char *strp; 12771558Srgrimes 1278324955Smanu SLIST_FOREACH(mlp, &mlhead, next) { 12791558Srgrimes if (!xdr_bool(xdrsp, &true)) 12801558Srgrimes return (0); 12811558Srgrimes strp = &mlp->ml_host[0]; 1282194880Sdfr if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) 12831558Srgrimes return (0); 12841558Srgrimes strp = &mlp->ml_dirp[0]; 1285194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 12861558Srgrimes return (0); 12871558Srgrimes } 12881558Srgrimes if (!xdr_bool(xdrsp, &false)) 12891558Srgrimes return (0); 12901558Srgrimes return (1); 12911558Srgrimes} 12921558Srgrimes 12931558Srgrimes/* 12941558Srgrimes * Xdr conversion for export list 12951558Srgrimes */ 1296285128Straszstatic int 1297216587Scharnierxdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief) 12981558Srgrimes{ 12991558Srgrimes struct exportlist *ep; 13001558Srgrimes int false = 0; 13019336Sdfr int putdef; 13029336Sdfr sigset_t sighup_mask; 13031558Srgrimes 13049336Sdfr sigemptyset(&sighup_mask); 13059336Sdfr sigaddset(&sighup_mask, SIGHUP); 13069336Sdfr sigprocmask(SIG_BLOCK, &sighup_mask, NULL); 1307324955Smanu 1308324955Smanu SLIST_FOREACH(ep, &exphead, entries) { 13091558Srgrimes putdef = 0; 1310100117Salfred if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, 1311100117Salfred &putdef, brief)) 13121558Srgrimes goto errout; 13131558Srgrimes if (ep->ex_defdir && putdef == 0 && 13141558Srgrimes put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, 1315100117Salfred &putdef, brief)) 13161558Srgrimes goto errout; 13171558Srgrimes } 13189336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 13191558Srgrimes if (!xdr_bool(xdrsp, &false)) 13201558Srgrimes return (0); 13211558Srgrimes return (1); 13221558Srgrimeserrout: 13239336Sdfr sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); 13241558Srgrimes return (0); 13251558Srgrimes} 13261558Srgrimes 13271558Srgrimes/* 13281558Srgrimes * Called from xdr_explist() to traverse the tree and export the 13291558Srgrimes * directory paths. 13301558Srgrimes */ 1331285128Straszstatic int 1332216587Scharnierput_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp, 1333216587Scharnier int brief) 13341558Srgrimes{ 13351558Srgrimes struct grouplist *grp; 13361558Srgrimes struct hostlist *hp; 13371558Srgrimes int true = 1; 13381558Srgrimes int false = 0; 13391558Srgrimes int gotalldir = 0; 13401558Srgrimes char *strp; 13411558Srgrimes 13421558Srgrimes if (dp) { 1343100117Salfred if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief)) 13441558Srgrimes return (1); 13451558Srgrimes if (!xdr_bool(xdrsp, &true)) 13461558Srgrimes return (1); 13471558Srgrimes strp = dp->dp_dirp; 1348194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 13491558Srgrimes return (1); 13501558Srgrimes if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { 13511558Srgrimes gotalldir = 1; 13521558Srgrimes *putdefp = 1; 13531558Srgrimes } 1354100117Salfred if (brief) { 1355100117Salfred if (!xdr_bool(xdrsp, &true)) 1356100117Salfred return (1); 1357100117Salfred strp = "(...)"; 1358194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 1359100117Salfred return (1); 1360100117Salfred } else if ((dp->dp_flag & DP_DEFSET) == 0 && 13611558Srgrimes (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { 13621558Srgrimes hp = dp->dp_hosts; 13631558Srgrimes while (hp) { 13641558Srgrimes grp = hp->ht_grp; 13651558Srgrimes if (grp->gr_type == GT_HOST) { 13661558Srgrimes if (!xdr_bool(xdrsp, &true)) 13671558Srgrimes return (1); 136874462Salfred strp = grp->gr_ptr.gt_addrinfo->ai_canonname; 13698871Srgrimes if (!xdr_string(xdrsp, &strp, 1370194880Sdfr MNTNAMLEN)) 13711558Srgrimes return (1); 13721558Srgrimes } else if (grp->gr_type == GT_NET) { 13731558Srgrimes if (!xdr_bool(xdrsp, &true)) 13741558Srgrimes return (1); 13751558Srgrimes strp = grp->gr_ptr.gt_net.nt_name; 13768871Srgrimes if (!xdr_string(xdrsp, &strp, 1377194880Sdfr MNTNAMLEN)) 13781558Srgrimes return (1); 13791558Srgrimes } 13801558Srgrimes hp = hp->ht_next; 13811558Srgrimes if (gotalldir && hp == (struct hostlist *)NULL) { 13821558Srgrimes hp = adp->dp_hosts; 13831558Srgrimes gotalldir = 0; 13841558Srgrimes } 13851558Srgrimes } 13861558Srgrimes } 13871558Srgrimes if (!xdr_bool(xdrsp, &false)) 13881558Srgrimes return (1); 1389100117Salfred if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief)) 13901558Srgrimes return (1); 13911558Srgrimes } 13921558Srgrimes return (0); 13931558Srgrimes} 13941558Srgrimes 1395285128Straszstatic int 1396216587Scharnierxdr_explist(XDR *xdrsp, caddr_t cp) 1397100117Salfred{ 1398100117Salfred 1399100117Salfred return xdr_explist_common(xdrsp, cp, 0); 1400100117Salfred} 1401100117Salfred 1402285128Straszstatic int 1403216587Scharnierxdr_explist_brief(XDR *xdrsp, caddr_t cp) 1404100117Salfred{ 1405100117Salfred 1406100117Salfred return xdr_explist_common(xdrsp, cp, 1); 1407100117Salfred} 1408100117Salfred 1409285128Straszstatic char *line; 1410285128Straszstatic size_t linesize; 1411285128Straszstatic FILE *exp_file; 14121558Srgrimes 14131558Srgrimes/* 1414166440Spjd * Get the export list from one, currently open file 14151558Srgrimes */ 1416166440Spjdstatic void 1417216587Scharnierget_exportlist_one(void) 14181558Srgrimes{ 1419324955Smanu struct exportlist *ep; 14201558Srgrimes struct grouplist *grp, *tgrp; 14211558Srgrimes struct dirlist *dirhead; 1422166440Spjd struct statfs fsb; 142372650Sgreen struct xucred anon; 14241558Srgrimes char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; 1425166440Spjd int len, has_host, exflags, got_nondir, dirplen, netgrp; 14261558Srgrimes 1427192934Srmacklem v4root_phase = 0; 14281558Srgrimes dirhead = (struct dirlist *)NULL; 14291558Srgrimes while (get_line()) { 14301558Srgrimes if (debug) 143137663Scharnier warnx("got line %s", line); 14321558Srgrimes cp = line; 14331558Srgrimes nextfield(&cp, &endcp); 14341558Srgrimes if (*cp == '#') 14351558Srgrimes goto nextline; 14361558Srgrimes 14371558Srgrimes /* 14381558Srgrimes * Set defaults. 14391558Srgrimes */ 14401558Srgrimes has_host = FALSE; 14411558Srgrimes anon = def_anon; 14421558Srgrimes exflags = MNT_EXPORTED; 14431558Srgrimes got_nondir = 0; 14441558Srgrimes opt_flags = 0; 14451558Srgrimes ep = (struct exportlist *)NULL; 1446192934Srmacklem dirp = NULL; 14471558Srgrimes 14481558Srgrimes /* 1449192934Srmacklem * Handle the V4 root dir. 1450192934Srmacklem */ 1451192934Srmacklem if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') { 1452192934Srmacklem /* 1453192934Srmacklem * V4: just indicates that it is the v4 root point, 1454192934Srmacklem * so skip over that and set v4root_phase. 1455192934Srmacklem */ 1456192934Srmacklem if (v4root_phase > 0) { 1457192934Srmacklem syslog(LOG_ERR, "V4:duplicate line, ignored"); 1458192934Srmacklem goto nextline; 1459192934Srmacklem } 1460192934Srmacklem v4root_phase = 1; 1461192934Srmacklem cp += 3; 1462192934Srmacklem nextfield(&cp, &endcp); 1463192934Srmacklem } 1464192934Srmacklem 1465192934Srmacklem /* 14661558Srgrimes * Create new exports list entry 14671558Srgrimes */ 14681558Srgrimes len = endcp-cp; 14691558Srgrimes tgrp = grp = get_grp(); 14701558Srgrimes while (len > 0) { 1471194880Sdfr if (len > MNTNAMLEN) { 1472329392Sbrd getexp_err(ep, tgrp, "mountpoint too long"); 14731558Srgrimes goto nextline; 14741558Srgrimes } 14751558Srgrimes if (*cp == '-') { 14761558Srgrimes if (ep == (struct exportlist *)NULL) { 1477329392Sbrd getexp_err(ep, tgrp, 1478329392Sbrd "flag before export path definition"); 14791558Srgrimes goto nextline; 14801558Srgrimes } 14811558Srgrimes if (debug) 148237663Scharnier warnx("doing opt %s", cp); 14831558Srgrimes got_nondir = 1; 14841558Srgrimes if (do_opt(&cp, &endcp, ep, grp, &has_host, 14851558Srgrimes &exflags, &anon)) { 1486329392Sbrd getexp_err(ep, tgrp, NULL); 14871558Srgrimes goto nextline; 14881558Srgrimes } 14891558Srgrimes } else if (*cp == '/') { 14901558Srgrimes savedc = *endcp; 14911558Srgrimes *endcp = '\0'; 1492192934Srmacklem if (v4root_phase > 1) { 1493192934Srmacklem if (dirp != NULL) { 1494329392Sbrd getexp_err(ep, tgrp, "Multiple V4 dirs"); 1495192934Srmacklem goto nextline; 1496192934Srmacklem } 1497192934Srmacklem } 14981558Srgrimes if (check_dirpath(cp) && 14991558Srgrimes statfs(cp, &fsb) >= 0) { 1500283008Srmacklem if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0) 1501283008Srmacklem syslog(LOG_ERR, "Warning: exporting of " 1502283008Srmacklem "automounted fs %s not supported", cp); 15031558Srgrimes if (got_nondir) { 1504329392Sbrd getexp_err(ep, tgrp, "dirs must be first"); 15051558Srgrimes goto nextline; 15061558Srgrimes } 1507192934Srmacklem if (v4root_phase == 1) { 1508192934Srmacklem if (dirp != NULL) { 1509329392Sbrd getexp_err(ep, tgrp, "Multiple V4 dirs"); 15101558Srgrimes goto nextline; 15111558Srgrimes } 1512192934Srmacklem if (strlen(v4root_dirpath) == 0) { 1513192934Srmacklem strlcpy(v4root_dirpath, cp, 1514192934Srmacklem sizeof (v4root_dirpath)); 1515192934Srmacklem } else if (strcmp(v4root_dirpath, cp) 1516192934Srmacklem != 0) { 1517192934Srmacklem syslog(LOG_ERR, 1518192934Srmacklem "different V4 dirpath %s", cp); 1519329392Sbrd getexp_err(ep, tgrp, NULL); 1520192934Srmacklem goto nextline; 1521192934Srmacklem } 1522192934Srmacklem dirp = cp; 1523192934Srmacklem v4root_phase = 2; 1524192934Srmacklem got_nondir = 1; 1525192934Srmacklem ep = get_exp(); 15261558Srgrimes } else { 1527192934Srmacklem if (ep) { 1528192934Srmacklem if (ep->ex_fs.val[0] != 1529192934Srmacklem fsb.f_fsid.val[0] || 1530192934Srmacklem ep->ex_fs.val[1] != 1531192934Srmacklem fsb.f_fsid.val[1]) { 1532329392Sbrd getexp_err(ep, tgrp, 1533329392Sbrd "fsid mismatch"); 1534192934Srmacklem goto nextline; 1535192934Srmacklem } 1536192934Srmacklem } else { 1537192934Srmacklem /* 1538192934Srmacklem * See if this directory is already 1539192934Srmacklem * in the list. 1540192934Srmacklem */ 1541192934Srmacklem ep = ex_search(&fsb.f_fsid); 1542192934Srmacklem if (ep == (struct exportlist *)NULL) { 1543192934Srmacklem ep = get_exp(); 1544192934Srmacklem ep->ex_fs = fsb.f_fsid; 1545324234Smanu ep->ex_fsdir = strdup(fsb.f_mntonname); 1546324234Smanu if (ep->ex_fsdir == NULL) 1547192934Srmacklem out_of_mem(); 1548192934Srmacklem if (debug) 1549192934Srmacklem warnx( 1550192934Srmacklem "making new ep fs=0x%x,0x%x", 1551192934Srmacklem fsb.f_fsid.val[0], 1552192934Srmacklem fsb.f_fsid.val[1]); 1553192934Srmacklem } else if (debug) 1554192934Srmacklem warnx("found ep fs=0x%x,0x%x", 1555192934Srmacklem fsb.f_fsid.val[0], 1556192934Srmacklem fsb.f_fsid.val[1]); 1557192934Srmacklem } 1558192934Srmacklem 15591558Srgrimes /* 1560192934Srmacklem * Add dirpath to export mount point. 15611558Srgrimes */ 1562192934Srmacklem dirp = add_expdir(&dirhead, cp, len); 1563192934Srmacklem dirplen = len; 15641558Srgrimes } 15651558Srgrimes } else { 1566329392Sbrd getexp_err(ep, tgrp, 1567329392Sbrd "symbolic link in export path or statfs failed"); 15681558Srgrimes goto nextline; 15691558Srgrimes } 15701558Srgrimes *endcp = savedc; 15711558Srgrimes } else { 15721558Srgrimes savedc = *endcp; 15731558Srgrimes *endcp = '\0'; 15741558Srgrimes got_nondir = 1; 15751558Srgrimes if (ep == (struct exportlist *)NULL) { 1576329392Sbrd getexp_err(ep, tgrp, 1577329392Sbrd "host(s) before export path definition"); 15781558Srgrimes goto nextline; 15791558Srgrimes } 15801558Srgrimes 15811558Srgrimes /* 15821558Srgrimes * Get the host or netgroup. 15831558Srgrimes */ 15841558Srgrimes setnetgrent(cp); 15851558Srgrimes netgrp = getnetgrent(&hst, &usr, &dom); 15861558Srgrimes do { 15871558Srgrimes if (has_host) { 15881558Srgrimes grp->gr_next = get_grp(); 15891558Srgrimes grp = grp->gr_next; 15901558Srgrimes } 15911558Srgrimes if (netgrp) { 159237003Sjoerg if (hst == 0) { 159337663Scharnier syslog(LOG_ERR, 159437663Scharnier "null hostname in netgroup %s, skipping", cp); 159537004Sjoerg grp->gr_type = GT_IGNORE; 159637003Sjoerg } else if (get_host(hst, grp, tgrp)) { 159737663Scharnier syslog(LOG_ERR, 159837663Scharnier "bad host %s in netgroup %s, skipping", hst, cp); 159929317Sjlemon grp->gr_type = GT_IGNORE; 16001558Srgrimes } 16017401Swpaul } else if (get_host(cp, grp, tgrp)) { 160237663Scharnier syslog(LOG_ERR, "bad host %s, skipping", cp); 160329317Sjlemon grp->gr_type = GT_IGNORE; 16041558Srgrimes } 16051558Srgrimes has_host = TRUE; 16061558Srgrimes } while (netgrp && getnetgrent(&hst, &usr, &dom)); 16071558Srgrimes endnetgrent(); 16081558Srgrimes *endcp = savedc; 16091558Srgrimes } 16101558Srgrimes cp = endcp; 16111558Srgrimes nextfield(&cp, &endcp); 16121558Srgrimes len = endcp - cp; 16131558Srgrimes } 16141558Srgrimes if (check_options(dirhead)) { 1615329392Sbrd getexp_err(ep, tgrp, NULL); 16161558Srgrimes goto nextline; 16171558Srgrimes } 16181558Srgrimes if (!has_host) { 161975641Siedowse grp->gr_type = GT_DEFAULT; 16201558Srgrimes if (debug) 162137663Scharnier warnx("adding a default entry"); 16221558Srgrimes 16231558Srgrimes /* 16241558Srgrimes * Don't allow a network export coincide with a list of 16251558Srgrimes * host(s) on the same line. 16261558Srgrimes */ 16271558Srgrimes } else if ((opt_flags & OP_NET) && tgrp->gr_next) { 1628329392Sbrd getexp_err(ep, tgrp, "network/host conflict"); 16291558Srgrimes goto nextline; 163029317Sjlemon 163174462Salfred /* 163274462Salfred * If an export list was specified on this line, make sure 163329317Sjlemon * that we have at least one valid entry, otherwise skip it. 163429317Sjlemon */ 163529317Sjlemon } else { 163629317Sjlemon grp = tgrp; 163774462Salfred while (grp && grp->gr_type == GT_IGNORE) 163829317Sjlemon grp = grp->gr_next; 163929317Sjlemon if (! grp) { 1640329392Sbrd getexp_err(ep, tgrp, "no valid entries"); 164129317Sjlemon goto nextline; 164229317Sjlemon } 16431558Srgrimes } 16441558Srgrimes 1645192934Srmacklem if (v4root_phase == 1) { 1646329392Sbrd getexp_err(ep, tgrp, "V4:root, no dirp, ignored"); 1647192934Srmacklem goto nextline; 1648192934Srmacklem } 1649192934Srmacklem 16501558Srgrimes /* 16511558Srgrimes * Loop through hosts, pushing the exports into the kernel. 16521558Srgrimes * After loop, tgrp points to the start of the list and 16531558Srgrimes * grp points to the last entry in the list. 16541558Srgrimes */ 16551558Srgrimes grp = tgrp; 16561558Srgrimes do { 165775635Siedowse if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, 165875635Siedowse &fsb)) { 1659329392Sbrd getexp_err(ep, tgrp, NULL); 166075635Siedowse goto nextline; 166175635Siedowse } 16621558Srgrimes } while (grp->gr_next && (grp = grp->gr_next)); 16631558Srgrimes 16641558Srgrimes /* 1665192934Srmacklem * For V4: don't enter in mount lists. 1666192934Srmacklem */ 1667194773Srmacklem if (v4root_phase > 0 && v4root_phase <= 2) { 1668194773Srmacklem /* 1669194773Srmacklem * Since these structures aren't used by mountd, 1670194773Srmacklem * free them up now. 1671194773Srmacklem */ 1672194773Srmacklem if (ep != NULL) 1673194773Srmacklem free_exp(ep); 1674194773Srmacklem while (tgrp != NULL) { 1675194773Srmacklem grp = tgrp; 1676194773Srmacklem tgrp = tgrp->gr_next; 1677194773Srmacklem free_grp(grp); 1678194773Srmacklem } 1679192934Srmacklem goto nextline; 1680194773Srmacklem } 1681192934Srmacklem 1682192934Srmacklem /* 16831558Srgrimes * Success. Update the data structures. 16841558Srgrimes */ 16851558Srgrimes if (has_host) { 16869336Sdfr hang_dirp(dirhead, tgrp, ep, opt_flags); 16871558Srgrimes grp->gr_next = grphead; 16881558Srgrimes grphead = tgrp; 16891558Srgrimes } else { 16901558Srgrimes hang_dirp(dirhead, (struct grouplist *)NULL, ep, 16919336Sdfr opt_flags); 16921558Srgrimes free_grp(grp); 16931558Srgrimes } 16941558Srgrimes dirhead = (struct dirlist *)NULL; 16951558Srgrimes if ((ep->ex_flag & EX_LINKED) == 0) { 1696324955Smanu SLIST_INSERT_HEAD(&exphead, ep, entries); 16971558Srgrimes 16981558Srgrimes ep->ex_flag |= EX_LINKED; 16991558Srgrimes } 17001558Srgrimesnextline: 1701192934Srmacklem v4root_phase = 0; 17021558Srgrimes if (dirhead) { 17031558Srgrimes free_dir(dirhead); 17041558Srgrimes dirhead = (struct dirlist *)NULL; 17051558Srgrimes } 17061558Srgrimes } 17071558Srgrimes} 17081558Srgrimes 17091558Srgrimes/* 1710166440Spjd * Get the export list from all specified files 1711166440Spjd */ 1712285128Straszstatic void 1713216587Scharnierget_exportlist(void) 1714166440Spjd{ 1715166440Spjd struct exportlist *ep, *ep2; 1716166440Spjd struct grouplist *grp, *tgrp; 1717166440Spjd struct export_args export; 1718166440Spjd struct iovec *iov; 1719166440Spjd struct statfs *fsp, *mntbufp; 1720166440Spjd struct xvfsconf vfc; 1721166440Spjd char errmsg[255]; 1722230352Seadler int num, i; 1723166440Spjd int iovlen; 1724168684Spjd int done; 1725192934Srmacklem struct nfsex_args eargs; 1726166440Spjd 1727241568Srmacklem if (suspend_nfsd != 0) 1728241568Srmacklem (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL); 1729192934Srmacklem v4root_dirpath[0] = '\0'; 1730166440Spjd bzero(&export, sizeof(export)); 1731166440Spjd export.ex_flags = MNT_DELEXPORT; 1732166440Spjd iov = NULL; 1733166440Spjd iovlen = 0; 1734166440Spjd bzero(errmsg, sizeof(errmsg)); 1735166440Spjd 1736166440Spjd /* 1737166440Spjd * First, get rid of the old list 1738166440Spjd */ 1739324955Smanu SLIST_FOREACH_SAFE(ep, &exphead, entries, ep2) { 1740324955Smanu SLIST_REMOVE(&exphead, ep, exportlist, entries); 1741324955Smanu free_exp(ep); 1742166440Spjd } 1743166440Spjd 1744166440Spjd grp = grphead; 1745166440Spjd while (grp) { 1746166440Spjd tgrp = grp; 1747166440Spjd grp = grp->gr_next; 1748166440Spjd free_grp(tgrp); 1749166440Spjd } 1750166440Spjd grphead = (struct grouplist *)NULL; 1751166440Spjd 1752166440Spjd /* 1753192934Srmacklem * and the old V4 root dir. 1754192934Srmacklem */ 1755192934Srmacklem bzero(&eargs, sizeof (eargs)); 1756192934Srmacklem eargs.export.ex_flags = MNT_DELEXPORT; 1757282214Strasz if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 && 1758192934Srmacklem errno != ENOENT) 1759192934Srmacklem syslog(LOG_ERR, "Can't delete exports for V4:"); 1760192934Srmacklem 1761192934Srmacklem /* 1762192934Srmacklem * and clear flag that notes if a public fh has been exported. 1763192934Srmacklem */ 1764192934Srmacklem has_publicfh = 0; 1765192934Srmacklem 1766192934Srmacklem /* 1767166440Spjd * And delete exports that are in the kernel for all local 1768166440Spjd * filesystems. 1769166440Spjd * XXX: Should know how to handle all local exportable filesystems. 1770166440Spjd */ 1771166440Spjd num = getmntinfo(&mntbufp, MNT_NOWAIT); 1772166440Spjd 1773166440Spjd if (num > 0) { 1774166440Spjd build_iovec(&iov, &iovlen, "fstype", NULL, 0); 1775166440Spjd build_iovec(&iov, &iovlen, "fspath", NULL, 0); 1776166440Spjd build_iovec(&iov, &iovlen, "from", NULL, 0); 1777166440Spjd build_iovec(&iov, &iovlen, "update", NULL, 0); 1778166440Spjd build_iovec(&iov, &iovlen, "export", &export, sizeof(export)); 1779166440Spjd build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 1780166440Spjd } 1781166440Spjd 1782166440Spjd for (i = 0; i < num; i++) { 1783166440Spjd fsp = &mntbufp[i]; 1784166440Spjd if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) { 1785166440Spjd syslog(LOG_ERR, "getvfsbyname() failed for %s", 1786166440Spjd fsp->f_fstypename); 1787166440Spjd continue; 1788166440Spjd } 1789166440Spjd 1790166440Spjd /* 1791281699Ssjg * We do not need to delete "export" flag from 1792281699Ssjg * filesystems that do not have it set. 1793281699Ssjg */ 1794281699Ssjg if (!(fsp->f_flags & MNT_EXPORTED)) 1795281699Ssjg continue; 1796281699Ssjg /* 1797166440Spjd * Do not delete export for network filesystem by 1798166440Spjd * passing "export" arg to nmount(). 1799166440Spjd * It only makes sense to do this for local filesystems. 1800166440Spjd */ 1801166440Spjd if (vfc.vfc_flags & VFCF_NETWORK) 1802166440Spjd continue; 1803166440Spjd 1804166440Spjd iov[1].iov_base = fsp->f_fstypename; 1805166440Spjd iov[1].iov_len = strlen(fsp->f_fstypename) + 1; 1806166440Spjd iov[3].iov_base = fsp->f_mntonname; 1807166440Spjd iov[3].iov_len = strlen(fsp->f_mntonname) + 1; 1808166440Spjd iov[5].iov_base = fsp->f_mntfromname; 1809166440Spjd iov[5].iov_len = strlen(fsp->f_mntfromname) + 1; 1810270183Sbdrewery errmsg[0] = '\0'; 1811166440Spjd 1812278523Skib /* 1813278523Skib * EXDEV is returned when path exists but is not a 1814278523Skib * mount point. May happens if raced with unmount. 1815278523Skib */ 1816166440Spjd if (nmount(iov, iovlen, fsp->f_flags) < 0 && 1817278523Skib errno != ENOENT && errno != ENOTSUP && errno != EXDEV) { 1818166440Spjd syslog(LOG_ERR, 1819166440Spjd "can't delete exports for %s: %m %s", 1820166440Spjd fsp->f_mntonname, errmsg); 1821166440Spjd } 1822166440Spjd } 1823166440Spjd 1824166440Spjd if (iov != NULL) { 1825166440Spjd /* Free strings allocated by strdup() in getmntopts.c */ 1826166440Spjd free(iov[0].iov_base); /* fstype */ 1827166440Spjd free(iov[2].iov_base); /* fspath */ 1828166440Spjd free(iov[4].iov_base); /* from */ 1829166440Spjd free(iov[6].iov_base); /* update */ 1830166440Spjd free(iov[8].iov_base); /* export */ 1831166440Spjd free(iov[10].iov_base); /* errmsg */ 1832166440Spjd 1833166440Spjd /* free iov, allocated by realloc() */ 1834166440Spjd free(iov); 1835166440Spjd iovlen = 0; 1836166440Spjd } 1837166440Spjd 1838166440Spjd /* 1839166440Spjd * Read in the exports file and build the list, calling 1840166440Spjd * nmount() as we go along to push the export rules into the kernel. 1841166440Spjd */ 1842168684Spjd done = 0; 1843166440Spjd for (i = 0; exnames[i] != NULL; i++) { 1844166440Spjd if (debug) 1845166440Spjd warnx("reading exports from %s", exnames[i]); 1846166440Spjd if ((exp_file = fopen(exnames[i], "r")) == NULL) { 1847168684Spjd syslog(LOG_WARNING, "can't open %s", exnames[i]); 1848168684Spjd continue; 1849166440Spjd } 1850166440Spjd get_exportlist_one(); 1851166440Spjd fclose(exp_file); 1852168684Spjd done++; 1853166440Spjd } 1854168684Spjd if (done == 0) { 1855168684Spjd syslog(LOG_ERR, "can't open any exports file"); 1856168684Spjd exit(2); 1857168684Spjd } 1858192934Srmacklem 1859192934Srmacklem /* 1860192934Srmacklem * If there was no public fh, clear any previous one set. 1861192934Srmacklem */ 1862282214Strasz if (has_publicfh == 0) 1863192934Srmacklem (void) nfssvc(NFSSVC_NOPUBLICFH, NULL); 1864241568Srmacklem 1865241568Srmacklem /* Resume the nfsd. If they weren't suspended, this is harmless. */ 1866241568Srmacklem (void)nfssvc(NFSSVC_RESUMENFSD, NULL); 1867166440Spjd} 1868166440Spjd 1869166440Spjd/* 18701558Srgrimes * Allocate an export list element 18711558Srgrimes */ 1872285128Straszstatic struct exportlist * 1873216587Scharnierget_exp(void) 18741558Srgrimes{ 18751558Srgrimes struct exportlist *ep; 18761558Srgrimes 1877224003Sdelphij ep = (struct exportlist *)calloc(1, sizeof (struct exportlist)); 18781558Srgrimes if (ep == (struct exportlist *)NULL) 18791558Srgrimes out_of_mem(); 18801558Srgrimes return (ep); 18811558Srgrimes} 18821558Srgrimes 18831558Srgrimes/* 18841558Srgrimes * Allocate a group list element 18851558Srgrimes */ 1886285128Straszstatic struct grouplist * 1887216587Scharnierget_grp(void) 18881558Srgrimes{ 18891558Srgrimes struct grouplist *gp; 18901558Srgrimes 1891224003Sdelphij gp = (struct grouplist *)calloc(1, sizeof (struct grouplist)); 18921558Srgrimes if (gp == (struct grouplist *)NULL) 18931558Srgrimes out_of_mem(); 18941558Srgrimes return (gp); 18951558Srgrimes} 18961558Srgrimes 18971558Srgrimes/* 18981558Srgrimes * Clean up upon an error in get_exportlist(). 18991558Srgrimes */ 1900285128Straszstatic void 1901329392Sbrdgetexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason) 19021558Srgrimes{ 19031558Srgrimes struct grouplist *tgrp; 19041558Srgrimes 1905329392Sbrd if (!(opt_flags & OP_QUIET)) { 1906329392Sbrd if (reason != NULL) 1907329392Sbrd syslog(LOG_ERR, "bad exports list line '%s': %s", line, 1908329392Sbrd reason); 1909329392Sbrd else 1910329392Sbrd syslog(LOG_ERR, "bad exports list line '%s'", line); 1911329392Sbrd } 19121558Srgrimes if (ep && (ep->ex_flag & EX_LINKED) == 0) 19131558Srgrimes free_exp(ep); 19141558Srgrimes while (grp) { 19151558Srgrimes tgrp = grp; 19161558Srgrimes grp = grp->gr_next; 19171558Srgrimes free_grp(tgrp); 19181558Srgrimes } 19191558Srgrimes} 19201558Srgrimes 19211558Srgrimes/* 19221558Srgrimes * Search the export list for a matching fs. 19231558Srgrimes */ 1924285128Straszstatic struct exportlist * 1925216587Scharnierex_search(fsid_t *fsid) 19261558Srgrimes{ 19271558Srgrimes struct exportlist *ep; 19281558Srgrimes 1929324955Smanu SLIST_FOREACH(ep, &exphead, entries) { 19301558Srgrimes if (ep->ex_fs.val[0] == fsid->val[0] && 19311558Srgrimes ep->ex_fs.val[1] == fsid->val[1]) 19321558Srgrimes return (ep); 19331558Srgrimes } 1934324955Smanu 19351558Srgrimes return (ep); 19361558Srgrimes} 19371558Srgrimes 19381558Srgrimes/* 19391558Srgrimes * Add a directory path to the list. 19401558Srgrimes */ 1941285128Straszstatic char * 1942216587Scharnieradd_expdir(struct dirlist **dpp, char *cp, int len) 19431558Srgrimes{ 19441558Srgrimes struct dirlist *dp; 19451558Srgrimes 1946324234Smanu dp = malloc(sizeof (struct dirlist)); 194737663Scharnier if (dp == (struct dirlist *)NULL) 194837663Scharnier out_of_mem(); 19491558Srgrimes dp->dp_left = *dpp; 19501558Srgrimes dp->dp_right = (struct dirlist *)NULL; 19511558Srgrimes dp->dp_flag = 0; 19521558Srgrimes dp->dp_hosts = (struct hostlist *)NULL; 1953324234Smanu dp->dp_dirp = strndup(cp, len); 1954324234Smanu if (dp->dp_dirp == NULL) 1955324234Smanu out_of_mem(); 19561558Srgrimes *dpp = dp; 19571558Srgrimes return (dp->dp_dirp); 19581558Srgrimes} 19591558Srgrimes 19601558Srgrimes/* 19611558Srgrimes * Hang the dir list element off the dirpath binary tree as required 19621558Srgrimes * and update the entry for host. 19631558Srgrimes */ 1964285128Straszstatic void 1965216587Scharnierhang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep, 1966216587Scharnier int flags) 19671558Srgrimes{ 19681558Srgrimes struct hostlist *hp; 19691558Srgrimes struct dirlist *dp2; 19701558Srgrimes 19719336Sdfr if (flags & OP_ALLDIRS) { 19721558Srgrimes if (ep->ex_defdir) 19731558Srgrimes free((caddr_t)dp); 19741558Srgrimes else 19751558Srgrimes ep->ex_defdir = dp; 19769336Sdfr if (grp == (struct grouplist *)NULL) { 19771558Srgrimes ep->ex_defdir->dp_flag |= DP_DEFSET; 1978240902Srmacklem /* Save the default security flavors list. */ 1979240902Srmacklem ep->ex_defnumsecflavors = ep->ex_numsecflavors; 1980240902Srmacklem if (ep->ex_numsecflavors > 0) 1981240902Srmacklem memcpy(ep->ex_defsecflavors, ep->ex_secflavors, 1982240902Srmacklem sizeof(ep->ex_secflavors)); 19839336Sdfr } else while (grp) { 19841558Srgrimes hp = get_ht(); 19851558Srgrimes hp->ht_grp = grp; 19861558Srgrimes hp->ht_next = ep->ex_defdir->dp_hosts; 19871558Srgrimes ep->ex_defdir->dp_hosts = hp; 1988240902Srmacklem /* Save the security flavors list for this host set. */ 1989240902Srmacklem grp->gr_numsecflavors = ep->ex_numsecflavors; 1990240902Srmacklem if (ep->ex_numsecflavors > 0) 1991240902Srmacklem memcpy(grp->gr_secflavors, ep->ex_secflavors, 1992240902Srmacklem sizeof(ep->ex_secflavors)); 19931558Srgrimes grp = grp->gr_next; 19941558Srgrimes } 19951558Srgrimes } else { 19961558Srgrimes 19971558Srgrimes /* 199837663Scharnier * Loop through the directories adding them to the tree. 19991558Srgrimes */ 20001558Srgrimes while (dp) { 20011558Srgrimes dp2 = dp->dp_left; 2002240902Srmacklem add_dlist(&ep->ex_dirl, dp, grp, flags, ep); 20031558Srgrimes dp = dp2; 20041558Srgrimes } 20051558Srgrimes } 20061558Srgrimes} 20071558Srgrimes 20081558Srgrimes/* 20091558Srgrimes * Traverse the binary tree either updating a node that is already there 20101558Srgrimes * for the new directory or adding the new node. 20111558Srgrimes */ 2012285128Straszstatic void 2013216587Scharnieradd_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp, 2014240902Srmacklem int flags, struct exportlist *ep) 20151558Srgrimes{ 20161558Srgrimes struct dirlist *dp; 20171558Srgrimes struct hostlist *hp; 20181558Srgrimes int cmp; 20191558Srgrimes 20201558Srgrimes dp = *dpp; 20211558Srgrimes if (dp) { 20221558Srgrimes cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); 20231558Srgrimes if (cmp > 0) { 2024240902Srmacklem add_dlist(&dp->dp_left, newdp, grp, flags, ep); 20251558Srgrimes return; 20261558Srgrimes } else if (cmp < 0) { 2027240902Srmacklem add_dlist(&dp->dp_right, newdp, grp, flags, ep); 20281558Srgrimes return; 20291558Srgrimes } else 20301558Srgrimes free((caddr_t)newdp); 20311558Srgrimes } else { 20321558Srgrimes dp = newdp; 20331558Srgrimes dp->dp_left = (struct dirlist *)NULL; 20341558Srgrimes *dpp = dp; 20351558Srgrimes } 20361558Srgrimes if (grp) { 20371558Srgrimes 20381558Srgrimes /* 20391558Srgrimes * Hang all of the host(s) off of the directory point. 20401558Srgrimes */ 20411558Srgrimes do { 20421558Srgrimes hp = get_ht(); 20431558Srgrimes hp->ht_grp = grp; 20441558Srgrimes hp->ht_next = dp->dp_hosts; 20451558Srgrimes dp->dp_hosts = hp; 2046240902Srmacklem /* Save the security flavors list for this host set. */ 2047240902Srmacklem grp->gr_numsecflavors = ep->ex_numsecflavors; 2048240902Srmacklem if (ep->ex_numsecflavors > 0) 2049240902Srmacklem memcpy(grp->gr_secflavors, ep->ex_secflavors, 2050240902Srmacklem sizeof(ep->ex_secflavors)); 20511558Srgrimes grp = grp->gr_next; 20521558Srgrimes } while (grp); 20539336Sdfr } else { 20541558Srgrimes dp->dp_flag |= DP_DEFSET; 2055240902Srmacklem /* Save the default security flavors list. */ 2056240902Srmacklem ep->ex_defnumsecflavors = ep->ex_numsecflavors; 2057240902Srmacklem if (ep->ex_numsecflavors > 0) 2058240902Srmacklem memcpy(ep->ex_defsecflavors, ep->ex_secflavors, 2059240902Srmacklem sizeof(ep->ex_secflavors)); 20609336Sdfr } 20611558Srgrimes} 20621558Srgrimes 20631558Srgrimes/* 20641558Srgrimes * Search for a dirpath on the export point. 20651558Srgrimes */ 2066285128Straszstatic struct dirlist * 2067216587Scharnierdirp_search(struct dirlist *dp, char *dirp) 20681558Srgrimes{ 20691558Srgrimes int cmp; 20701558Srgrimes 20711558Srgrimes if (dp) { 207274462Salfred cmp = strcmp(dp->dp_dirp, dirp); 20731558Srgrimes if (cmp > 0) 207474462Salfred return (dirp_search(dp->dp_left, dirp)); 20751558Srgrimes else if (cmp < 0) 207674462Salfred return (dirp_search(dp->dp_right, dirp)); 20771558Srgrimes else 20781558Srgrimes return (dp); 20791558Srgrimes } 20801558Srgrimes return (dp); 20811558Srgrimes} 20821558Srgrimes 20831558Srgrimes/* 20841558Srgrimes * Scan for a host match in a directory tree. 20851558Srgrimes */ 2086285128Straszstatic int 2087216587Scharnierchk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp, 2088240902Srmacklem int *hostsetp, int *numsecflavors, int **secflavorsp) 20891558Srgrimes{ 20901558Srgrimes struct hostlist *hp; 20911558Srgrimes struct grouplist *grp; 209274462Salfred struct addrinfo *ai; 20931558Srgrimes 20941558Srgrimes if (dp) { 20951558Srgrimes if (dp->dp_flag & DP_DEFSET) 20969336Sdfr *defsetp = dp->dp_flag; 20971558Srgrimes hp = dp->dp_hosts; 20981558Srgrimes while (hp) { 20991558Srgrimes grp = hp->ht_grp; 21001558Srgrimes switch (grp->gr_type) { 21011558Srgrimes case GT_HOST: 210274462Salfred ai = grp->gr_ptr.gt_addrinfo; 210374462Salfred for (; ai; ai = ai->ai_next) { 210475801Siedowse if (!sacmp(ai->ai_addr, saddr, NULL)) { 210574462Salfred *hostsetp = 210674462Salfred (hp->ht_flag | DP_HOSTSET); 2107240902Srmacklem if (numsecflavors != NULL) { 2108240902Srmacklem *numsecflavors = 2109240902Srmacklem grp->gr_numsecflavors; 2110240902Srmacklem *secflavorsp = 2111240902Srmacklem grp->gr_secflavors; 2112240902Srmacklem } 211374462Salfred return (1); 211474462Salfred } 21159336Sdfr } 211675801Siedowse break; 21171558Srgrimes case GT_NET: 211875801Siedowse if (!sacmp(saddr, (struct sockaddr *) 211975801Siedowse &grp->gr_ptr.gt_net.nt_net, 212075801Siedowse (struct sockaddr *) 212175801Siedowse &grp->gr_ptr.gt_net.nt_mask)) { 212274462Salfred *hostsetp = (hp->ht_flag | DP_HOSTSET); 2123240902Srmacklem if (numsecflavors != NULL) { 2124240902Srmacklem *numsecflavors = 2125240902Srmacklem grp->gr_numsecflavors; 2126240902Srmacklem *secflavorsp = 2127240902Srmacklem grp->gr_secflavors; 2128240902Srmacklem } 212974462Salfred return (1); 213074462Salfred } 213175801Siedowse break; 213275801Siedowse } 21331558Srgrimes hp = hp->ht_next; 21341558Srgrimes } 21351558Srgrimes } 21361558Srgrimes return (0); 21371558Srgrimes} 21381558Srgrimes 21391558Srgrimes/* 21401558Srgrimes * Scan tree for a host that matches the address. 21411558Srgrimes */ 2142285128Straszstatic int 2143216587Scharnierscan_tree(struct dirlist *dp, struct sockaddr *saddr) 21441558Srgrimes{ 21459336Sdfr int defset, hostset; 21461558Srgrimes 21471558Srgrimes if (dp) { 21481558Srgrimes if (scan_tree(dp->dp_left, saddr)) 21491558Srgrimes return (1); 2150240902Srmacklem if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL)) 21511558Srgrimes return (1); 21521558Srgrimes if (scan_tree(dp->dp_right, saddr)) 21531558Srgrimes return (1); 21541558Srgrimes } 21551558Srgrimes return (0); 21561558Srgrimes} 21571558Srgrimes 21581558Srgrimes/* 21591558Srgrimes * Traverse the dirlist tree and free it up. 21601558Srgrimes */ 2161285128Straszstatic void 2162216587Scharnierfree_dir(struct dirlist *dp) 21631558Srgrimes{ 21641558Srgrimes 21651558Srgrimes if (dp) { 21661558Srgrimes free_dir(dp->dp_left); 21671558Srgrimes free_dir(dp->dp_right); 21681558Srgrimes free_host(dp->dp_hosts); 2169324234Smanu free(dp->dp_dirp); 2170324234Smanu free(dp); 21711558Srgrimes } 21721558Srgrimes} 21731558Srgrimes 21741558Srgrimes/* 2175184588Sdfr * Parse a colon separated list of security flavors 2176184588Sdfr */ 2177285128Straszstatic int 2178216587Scharnierparsesec(char *seclist, struct exportlist *ep) 2179184588Sdfr{ 2180184588Sdfr char *cp, savedc; 2181184588Sdfr int flavor; 2182184588Sdfr 2183184588Sdfr ep->ex_numsecflavors = 0; 2184184588Sdfr for (;;) { 2185184588Sdfr cp = strchr(seclist, ':'); 2186184588Sdfr if (cp) { 2187184588Sdfr savedc = *cp; 2188184588Sdfr *cp = '\0'; 2189184588Sdfr } 2190184588Sdfr 2191184588Sdfr if (!strcmp(seclist, "sys")) 2192184588Sdfr flavor = AUTH_SYS; 2193184588Sdfr else if (!strcmp(seclist, "krb5")) 2194184588Sdfr flavor = RPCSEC_GSS_KRB5; 2195184588Sdfr else if (!strcmp(seclist, "krb5i")) 2196184588Sdfr flavor = RPCSEC_GSS_KRB5I; 2197184588Sdfr else if (!strcmp(seclist, "krb5p")) 2198184588Sdfr flavor = RPCSEC_GSS_KRB5P; 2199184588Sdfr else { 2200184588Sdfr if (cp) 2201184588Sdfr *cp = savedc; 2202184588Sdfr syslog(LOG_ERR, "bad sec flavor: %s", seclist); 2203184588Sdfr return (1); 2204184588Sdfr } 2205184588Sdfr if (ep->ex_numsecflavors == MAXSECFLAVORS) { 2206184588Sdfr if (cp) 2207184588Sdfr *cp = savedc; 2208184588Sdfr syslog(LOG_ERR, "too many sec flavors: %s", seclist); 2209184588Sdfr return (1); 2210184588Sdfr } 2211184588Sdfr ep->ex_secflavors[ep->ex_numsecflavors] = flavor; 2212184588Sdfr ep->ex_numsecflavors++; 2213184588Sdfr if (cp) { 2214184588Sdfr *cp = savedc; 2215184588Sdfr seclist = cp + 1; 2216184588Sdfr } else { 2217184588Sdfr break; 2218184588Sdfr } 2219184588Sdfr } 2220184588Sdfr return (0); 2221184588Sdfr} 2222184588Sdfr 2223184588Sdfr/* 22241558Srgrimes * Parse the option string and update fields. 22251558Srgrimes * Option arguments may either be -<option>=<value> or 22261558Srgrimes * -<option> <value> 22271558Srgrimes */ 2228285128Straszstatic int 2229216587Scharnierdo_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp, 2230216587Scharnier int *has_hostp, int *exflagsp, struct xucred *cr) 22311558Srgrimes{ 22321558Srgrimes char *cpoptarg, *cpoptend; 22331558Srgrimes char *cp, *endcp, *cpopt, savedc, savedc2; 22341558Srgrimes int allflag, usedarg; 22351558Srgrimes 223651968Salfred savedc2 = '\0'; 22371558Srgrimes cpopt = *cpp; 22381558Srgrimes cpopt++; 22391558Srgrimes cp = *endcpp; 22401558Srgrimes savedc = *cp; 22411558Srgrimes *cp = '\0'; 22421558Srgrimes while (cpopt && *cpopt) { 22431558Srgrimes allflag = 1; 22441558Srgrimes usedarg = -2; 224537663Scharnier if ((cpoptend = strchr(cpopt, ','))) { 22461558Srgrimes *cpoptend++ = '\0'; 224737663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 22481558Srgrimes *cpoptarg++ = '\0'; 22491558Srgrimes } else { 225037663Scharnier if ((cpoptarg = strchr(cpopt, '='))) 22511558Srgrimes *cpoptarg++ = '\0'; 22521558Srgrimes else { 22531558Srgrimes *cp = savedc; 22541558Srgrimes nextfield(&cp, &endcp); 22551558Srgrimes **endcpp = '\0'; 22561558Srgrimes if (endcp > cp && *cp != '-') { 22571558Srgrimes cpoptarg = cp; 22581558Srgrimes savedc2 = *endcp; 22591558Srgrimes *endcp = '\0'; 22601558Srgrimes usedarg = 0; 22611558Srgrimes } 22621558Srgrimes } 22631558Srgrimes } 22641558Srgrimes if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) { 22651558Srgrimes *exflagsp |= MNT_EXRDONLY; 22661558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "maproot") || 22671558Srgrimes !(allflag = strcmp(cpopt, "mapall")) || 22681558Srgrimes !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) { 22691558Srgrimes usedarg++; 22701558Srgrimes parsecred(cpoptarg, cr); 22711558Srgrimes if (allflag == 0) { 22721558Srgrimes *exflagsp |= MNT_EXPORTANON; 22731558Srgrimes opt_flags |= OP_MAPALL; 22741558Srgrimes } else 22751558Srgrimes opt_flags |= OP_MAPROOT; 22761558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "mask") || 227775801Siedowse !strcmp(cpopt, "m"))) { 22781558Srgrimes if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) { 227937663Scharnier syslog(LOG_ERR, "bad mask: %s", cpoptarg); 22801558Srgrimes return (1); 22811558Srgrimes } 22821558Srgrimes usedarg++; 22831558Srgrimes opt_flags |= OP_MASK; 22841558Srgrimes } else if (cpoptarg && (!strcmp(cpopt, "network") || 22851558Srgrimes !strcmp(cpopt, "n"))) { 228674462Salfred if (strchr(cpoptarg, '/') != NULL) { 228774462Salfred if (debug) 228874462Salfred fprintf(stderr, "setting OP_MASKLEN\n"); 228974462Salfred opt_flags |= OP_MASKLEN; 229074462Salfred } 22911558Srgrimes if (grp->gr_type != GT_NULL) { 229237663Scharnier syslog(LOG_ERR, "network/host conflict"); 22931558Srgrimes return (1); 22941558Srgrimes } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) { 229537663Scharnier syslog(LOG_ERR, "bad net: %s", cpoptarg); 22961558Srgrimes return (1); 22971558Srgrimes } 22981558Srgrimes grp->gr_type = GT_NET; 22991558Srgrimes *has_hostp = 1; 23001558Srgrimes usedarg++; 23011558Srgrimes opt_flags |= OP_NET; 23021558Srgrimes } else if (!strcmp(cpopt, "alldirs")) { 23031558Srgrimes opt_flags |= OP_ALLDIRS; 230427447Sdfr } else if (!strcmp(cpopt, "public")) { 230527447Sdfr *exflagsp |= MNT_EXPUBLIC; 230627447Sdfr } else if (!strcmp(cpopt, "webnfs")) { 230727447Sdfr *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON); 230827447Sdfr opt_flags |= OP_MAPALL; 230927447Sdfr } else if (cpoptarg && !strcmp(cpopt, "index")) { 231027447Sdfr ep->ex_indexfile = strdup(cpoptarg); 2311100336Sjoerg } else if (!strcmp(cpopt, "quiet")) { 2312100336Sjoerg opt_flags |= OP_QUIET; 2313247034Spluknet } else if (cpoptarg && !strcmp(cpopt, "sec")) { 2314184588Sdfr if (parsesec(cpoptarg, ep)) 2315184588Sdfr return (1); 2316184588Sdfr opt_flags |= OP_SEC; 2317184588Sdfr usedarg++; 23181558Srgrimes } else { 231937663Scharnier syslog(LOG_ERR, "bad opt %s", cpopt); 23201558Srgrimes return (1); 23211558Srgrimes } 23221558Srgrimes if (usedarg >= 0) { 23231558Srgrimes *endcp = savedc2; 23241558Srgrimes **endcpp = savedc; 23251558Srgrimes if (usedarg > 0) { 23261558Srgrimes *cpp = cp; 23271558Srgrimes *endcpp = endcp; 23281558Srgrimes } 23291558Srgrimes return (0); 23301558Srgrimes } 23311558Srgrimes cpopt = cpoptend; 23321558Srgrimes } 23331558Srgrimes **endcpp = savedc; 23341558Srgrimes return (0); 23351558Srgrimes} 23361558Srgrimes 23371558Srgrimes/* 23381558Srgrimes * Translate a character string to the corresponding list of network 23391558Srgrimes * addresses for a hostname. 23401558Srgrimes */ 2341285128Straszstatic int 2342216587Scharnierget_host(char *cp, struct grouplist *grp, struct grouplist *tgrp) 23431558Srgrimes{ 23447401Swpaul struct grouplist *checkgrp; 234575635Siedowse struct addrinfo *ai, *tai, hints; 234674462Salfred int ecode; 234774462Salfred char host[NI_MAXHOST]; 23481558Srgrimes 234974462Salfred if (grp->gr_type != GT_NULL) { 235074462Salfred syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp); 23511558Srgrimes return (1); 23521558Srgrimes } 235374462Salfred memset(&hints, 0, sizeof hints); 235474462Salfred hints.ai_flags = AI_CANONNAME; 235574462Salfred hints.ai_protocol = IPPROTO_UDP; 235674462Salfred ecode = getaddrinfo(cp, NULL, &hints, &ai); 235774462Salfred if (ecode != 0) { 235875635Siedowse syslog(LOG_ERR,"can't get address info for host %s", cp); 235974462Salfred return 1; 236074462Salfred } 236174462Salfred grp->gr_ptr.gt_addrinfo = ai; 236274462Salfred while (ai != NULL) { 236374462Salfred if (ai->ai_canonname == NULL) { 236474462Salfred if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 2365146187Sume sizeof host, NULL, 0, NI_NUMERICHOST) != 0) 236674462Salfred strlcpy(host, "?", sizeof(host)); 236774462Salfred ai->ai_canonname = strdup(host); 236874462Salfred ai->ai_flags |= AI_CANONNAME; 236975641Siedowse } 237074462Salfred if (debug) 237175635Siedowse fprintf(stderr, "got host %s\n", ai->ai_canonname); 237275635Siedowse /* 237375635Siedowse * Sanity check: make sure we don't already have an entry 237475635Siedowse * for this host in the grouplist. 237575635Siedowse */ 237675635Siedowse for (checkgrp = tgrp; checkgrp != NULL; 237775635Siedowse checkgrp = checkgrp->gr_next) { 237875635Siedowse if (checkgrp->gr_type != GT_HOST) 237975635Siedowse continue; 238075635Siedowse for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL; 238175635Siedowse tai = tai->ai_next) { 238275801Siedowse if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0) 238375635Siedowse continue; 238475635Siedowse if (debug) 238575635Siedowse fprintf(stderr, 238675635Siedowse "ignoring duplicate host %s\n", 238775635Siedowse ai->ai_canonname); 238875635Siedowse grp->gr_type = GT_IGNORE; 238975635Siedowse return (0); 239075635Siedowse } 239175635Siedowse } 239274462Salfred ai = ai->ai_next; 23931558Srgrimes } 239475635Siedowse grp->gr_type = GT_HOST; 23951558Srgrimes return (0); 23961558Srgrimes} 23971558Srgrimes 23981558Srgrimes/* 23991558Srgrimes * Free up an exports list component 24001558Srgrimes */ 2401285128Straszstatic void 2402216587Scharnierfree_exp(struct exportlist *ep) 24031558Srgrimes{ 24041558Srgrimes 24051558Srgrimes if (ep->ex_defdir) { 24061558Srgrimes free_host(ep->ex_defdir->dp_hosts); 24071558Srgrimes free((caddr_t)ep->ex_defdir); 24081558Srgrimes } 24091558Srgrimes if (ep->ex_fsdir) 24101558Srgrimes free(ep->ex_fsdir); 241127447Sdfr if (ep->ex_indexfile) 241227447Sdfr free(ep->ex_indexfile); 24131558Srgrimes free_dir(ep->ex_dirl); 24141558Srgrimes free((caddr_t)ep); 24151558Srgrimes} 24161558Srgrimes 24171558Srgrimes/* 24181558Srgrimes * Free hosts. 24191558Srgrimes */ 2420285128Straszstatic void 2421216587Scharnierfree_host(struct hostlist *hp) 24221558Srgrimes{ 24231558Srgrimes struct hostlist *hp2; 24241558Srgrimes 24251558Srgrimes while (hp) { 24261558Srgrimes hp2 = hp; 24271558Srgrimes hp = hp->ht_next; 24281558Srgrimes free((caddr_t)hp2); 24291558Srgrimes } 24301558Srgrimes} 24311558Srgrimes 2432285128Straszstatic struct hostlist * 2433216587Scharnierget_ht(void) 24341558Srgrimes{ 24351558Srgrimes struct hostlist *hp; 24361558Srgrimes 24371558Srgrimes hp = (struct hostlist *)malloc(sizeof (struct hostlist)); 24381558Srgrimes if (hp == (struct hostlist *)NULL) 24391558Srgrimes out_of_mem(); 24401558Srgrimes hp->ht_next = (struct hostlist *)NULL; 24419336Sdfr hp->ht_flag = 0; 24421558Srgrimes return (hp); 24431558Srgrimes} 24441558Srgrimes 24451558Srgrimes/* 24461558Srgrimes * Out of memory, fatal 24471558Srgrimes */ 2448285128Straszstatic void 2449216587Scharnierout_of_mem(void) 24501558Srgrimes{ 24511558Srgrimes 245237663Scharnier syslog(LOG_ERR, "out of memory"); 24531558Srgrimes exit(2); 24541558Srgrimes} 24551558Srgrimes 24561558Srgrimes/* 2457158857Srodrigc * Do the nmount() syscall with the update flag to push the export info into 24581558Srgrimes * the kernel. 24591558Srgrimes */ 2460285128Straszstatic int 2461158857Srodrigcdo_mount(struct exportlist *ep, struct grouplist *grp, int exflags, 2462158857Srodrigc struct xucred *anoncrp, char *dirp, int dirplen, struct statfs *fsb) 24631558Srgrimes{ 246475841Siedowse struct statfs fsb1; 246574462Salfred struct addrinfo *ai; 2466282214Strasz struct export_args *eap; 2467158857Srodrigc char errmsg[255]; 2468158857Srodrigc char *cp; 24691558Srgrimes int done; 2470158857Srodrigc char savedc; 2471158857Srodrigc struct iovec *iov; 2472184588Sdfr int i, iovlen; 2473158857Srodrigc int ret; 2474192934Srmacklem struct nfsex_args nfsea; 24751558Srgrimes 2476282214Strasz eap = &nfsea.export; 2477192934Srmacklem 2478158857Srodrigc cp = NULL; 2479158857Srodrigc savedc = '\0'; 2480158857Srodrigc iov = NULL; 2481158857Srodrigc iovlen = 0; 2482158857Srodrigc ret = 0; 248375801Siedowse 2484192934Srmacklem bzero(eap, sizeof (struct export_args)); 2485158857Srodrigc bzero(errmsg, sizeof(errmsg)); 2486192934Srmacklem eap->ex_flags = exflags; 2487192934Srmacklem eap->ex_anon = *anoncrp; 2488192934Srmacklem eap->ex_indexfile = ep->ex_indexfile; 248975641Siedowse if (grp->gr_type == GT_HOST) 249074462Salfred ai = grp->gr_ptr.gt_addrinfo; 249175641Siedowse else 249275641Siedowse ai = NULL; 2493192934Srmacklem eap->ex_numsecflavors = ep->ex_numsecflavors; 2494192934Srmacklem for (i = 0; i < eap->ex_numsecflavors; i++) 2495192934Srmacklem eap->ex_secflavors[i] = ep->ex_secflavors[i]; 2496192934Srmacklem if (eap->ex_numsecflavors == 0) { 2497192934Srmacklem eap->ex_numsecflavors = 1; 2498192934Srmacklem eap->ex_secflavors[0] = AUTH_SYS; 2499184588Sdfr } 25001558Srgrimes done = FALSE; 2501158857Srodrigc 2502192934Srmacklem if (v4root_phase == 0) { 2503192934Srmacklem build_iovec(&iov, &iovlen, "fstype", NULL, 0); 2504192934Srmacklem build_iovec(&iov, &iovlen, "fspath", NULL, 0); 2505192934Srmacklem build_iovec(&iov, &iovlen, "from", NULL, 0); 2506192934Srmacklem build_iovec(&iov, &iovlen, "update", NULL, 0); 2507192934Srmacklem build_iovec(&iov, &iovlen, "export", eap, 2508192934Srmacklem sizeof (struct export_args)); 2509192934Srmacklem build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 2510192934Srmacklem } 2511158857Srodrigc 25121558Srgrimes while (!done) { 25131558Srgrimes switch (grp->gr_type) { 25141558Srgrimes case GT_HOST: 251575641Siedowse if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0) 251674462Salfred goto skip; 2517192934Srmacklem eap->ex_addr = ai->ai_addr; 2518192934Srmacklem eap->ex_addrlen = ai->ai_addrlen; 2519192934Srmacklem eap->ex_masklen = 0; 25201558Srgrimes break; 25211558Srgrimes case GT_NET: 252275801Siedowse if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 && 252374462Salfred have_v6 == 0) 252474462Salfred goto skip; 2525192934Srmacklem eap->ex_addr = 252675801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net; 2527192934Srmacklem eap->ex_addrlen = 2528158857Srodrigc ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len; 2529192934Srmacklem eap->ex_mask = 253075801Siedowse (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask; 2531192934Srmacklem eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len; 25321558Srgrimes break; 253375641Siedowse case GT_DEFAULT: 2534192934Srmacklem eap->ex_addr = NULL; 2535192934Srmacklem eap->ex_addrlen = 0; 2536192934Srmacklem eap->ex_mask = NULL; 2537192934Srmacklem eap->ex_masklen = 0; 253875641Siedowse break; 25397401Swpaul case GT_IGNORE: 2540158857Srodrigc ret = 0; 2541158857Srodrigc goto error_exit; 25427401Swpaul break; 25431558Srgrimes default: 254437663Scharnier syslog(LOG_ERR, "bad grouptype"); 25451558Srgrimes if (cp) 25461558Srgrimes *cp = savedc; 2547158857Srodrigc ret = 1; 2548158857Srodrigc goto error_exit; 2549298089Spfg } 25501558Srgrimes 25511558Srgrimes /* 2552192934Srmacklem * For V4:, use the nfssvc() syscall, instead of mount(). 25531558Srgrimes */ 2554192934Srmacklem if (v4root_phase == 2) { 2555192934Srmacklem nfsea.fspec = v4root_dirpath; 2556282214Strasz if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&nfsea) < 0) { 2557192934Srmacklem syslog(LOG_ERR, "Exporting V4: failed"); 2558192934Srmacklem return (2); 2559158857Srodrigc } 2560192934Srmacklem } else { 2561192934Srmacklem /* 2562192934Srmacklem * XXX: 2563192934Srmacklem * Maybe I should just use the fsb->f_mntonname path 2564192934Srmacklem * instead of looping back up the dirp to the mount 2565192934Srmacklem * point?? 2566192934Srmacklem * Also, needs to know how to export all types of local 2567192934Srmacklem * exportable filesystems and not just "ufs". 2568192934Srmacklem */ 2569192934Srmacklem iov[1].iov_base = fsb->f_fstypename; /* "fstype" */ 2570192934Srmacklem iov[1].iov_len = strlen(fsb->f_fstypename) + 1; 2571192934Srmacklem iov[3].iov_base = fsb->f_mntonname; /* "fspath" */ 2572192934Srmacklem iov[3].iov_len = strlen(fsb->f_mntonname) + 1; 2573192934Srmacklem iov[5].iov_base = fsb->f_mntfromname; /* "from" */ 2574192934Srmacklem iov[5].iov_len = strlen(fsb->f_mntfromname) + 1; 2575270183Sbdrewery errmsg[0] = '\0'; 2576192934Srmacklem 2577192934Srmacklem while (nmount(iov, iovlen, fsb->f_flags) < 0) { 2578192934Srmacklem if (cp) 2579192934Srmacklem *cp-- = savedc; 2580192934Srmacklem else 2581192934Srmacklem cp = dirp + dirplen - 1; 2582192934Srmacklem if (opt_flags & OP_QUIET) { 2583192934Srmacklem ret = 1; 2584192934Srmacklem goto error_exit; 2585192934Srmacklem } 2586192934Srmacklem if (errno == EPERM) { 2587192934Srmacklem if (debug) 2588239744Sdelphij warnx("can't change attributes for %s: %s", 2589239744Sdelphij dirp, errmsg); 2590192934Srmacklem syslog(LOG_ERR, 2591239744Sdelphij "can't change attributes for %s: %s", 2592239744Sdelphij dirp, errmsg); 2593192934Srmacklem ret = 1; 2594192934Srmacklem goto error_exit; 2595192934Srmacklem } 2596192934Srmacklem if (opt_flags & OP_ALLDIRS) { 2597192934Srmacklem if (errno == EINVAL) 2598192934Srmacklem syslog(LOG_ERR, 2599100336Sjoerg "-alldirs requested but %s is not a filesystem mountpoint", 2600192934Srmacklem dirp); 2601192934Srmacklem else 2602192934Srmacklem syslog(LOG_ERR, 2603192934Srmacklem "could not remount %s: %m", 2604192934Srmacklem dirp); 2605192934Srmacklem ret = 1; 2606192934Srmacklem goto error_exit; 2607192934Srmacklem } 2608192934Srmacklem /* back up over the last component */ 2609192934Srmacklem while (*cp == '/' && cp > dirp) 2610192934Srmacklem cp--; 2611192934Srmacklem while (*(cp - 1) != '/' && cp > dirp) 2612192934Srmacklem cp--; 2613192934Srmacklem if (cp == dirp) { 2614192934Srmacklem if (debug) 2615192934Srmacklem warnx("mnt unsucc"); 2616192934Srmacklem syslog(LOG_ERR, "can't export %s %s", 2617192934Srmacklem dirp, errmsg); 2618192934Srmacklem ret = 1; 2619192934Srmacklem goto error_exit; 2620192934Srmacklem } 2621192934Srmacklem savedc = *cp; 2622192934Srmacklem *cp = '\0'; 2623192934Srmacklem /* 2624192934Srmacklem * Check that we're still on the same 2625192934Srmacklem * filesystem. 2626192934Srmacklem */ 2627192934Srmacklem if (statfs(dirp, &fsb1) != 0 || 2628192934Srmacklem bcmp(&fsb1.f_fsid, &fsb->f_fsid, 2629192934Srmacklem sizeof (fsb1.f_fsid)) != 0) { 2630192934Srmacklem *cp = savedc; 2631100336Sjoerg syslog(LOG_ERR, 2632192934Srmacklem "can't export %s %s", dirp, 2633192934Srmacklem errmsg); 2634192934Srmacklem ret = 1; 2635192934Srmacklem goto error_exit; 2636192934Srmacklem } 26371558Srgrimes } 26381558Srgrimes } 2639192934Srmacklem 2640192934Srmacklem /* 2641192934Srmacklem * For the experimental server: 2642192934Srmacklem * If this is the public directory, get the file handle 2643192934Srmacklem * and load it into the kernel via the nfssvc() syscall. 2644192934Srmacklem */ 2645282214Strasz if ((exflags & MNT_EXPUBLIC) != 0) { 2646192934Srmacklem fhandle_t fh; 2647192934Srmacklem char *public_name; 2648192934Srmacklem 2649192934Srmacklem if (eap->ex_indexfile != NULL) 2650192934Srmacklem public_name = eap->ex_indexfile; 2651192934Srmacklem else 2652192934Srmacklem public_name = dirp; 2653192934Srmacklem if (getfh(public_name, &fh) < 0) 2654192934Srmacklem syslog(LOG_ERR, 2655192934Srmacklem "Can't get public fh for %s", public_name); 2656192934Srmacklem else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0) 2657192934Srmacklem syslog(LOG_ERR, 2658192934Srmacklem "Can't set public fh for %s", public_name); 2659192934Srmacklem else 2660192934Srmacklem has_publicfh = 1; 2661192934Srmacklem } 266274462Salfredskip: 266375641Siedowse if (ai != NULL) 266474462Salfred ai = ai->ai_next; 266575641Siedowse if (ai == NULL) 26661558Srgrimes done = TRUE; 26671558Srgrimes } 26681558Srgrimes if (cp) 26691558Srgrimes *cp = savedc; 2670158857Srodrigcerror_exit: 2671158857Srodrigc /* free strings allocated by strdup() in getmntopts.c */ 2672158857Srodrigc if (iov != NULL) { 2673158857Srodrigc free(iov[0].iov_base); /* fstype */ 2674158857Srodrigc free(iov[2].iov_base); /* fspath */ 2675158857Srodrigc free(iov[4].iov_base); /* from */ 2676158857Srodrigc free(iov[6].iov_base); /* update */ 2677158857Srodrigc free(iov[8].iov_base); /* export */ 2678158857Srodrigc free(iov[10].iov_base); /* errmsg */ 2679158857Srodrigc 2680158857Srodrigc /* free iov, allocated by realloc() */ 2681158857Srodrigc free(iov); 2682158857Srodrigc } 2683158857Srodrigc return (ret); 26841558Srgrimes} 26851558Srgrimes 26861558Srgrimes/* 26871558Srgrimes * Translate a net address. 268875801Siedowse * 268975801Siedowse * If `maskflg' is nonzero, then `cp' is a netmask, not a network address. 26901558Srgrimes */ 2691285128Straszstatic int 2692216587Scharnierget_net(char *cp, struct netmsk *net, int maskflg) 26931558Srgrimes{ 269475861Siedowse struct netent *np = NULL; 269574462Salfred char *name, *p, *prefp; 269675801Siedowse struct sockaddr_in sin; 269775861Siedowse struct sockaddr *sa = NULL; 269874462Salfred struct addrinfo hints, *ai = NULL; 269974462Salfred char netname[NI_MAXHOST]; 270074462Salfred long preflen; 27011558Srgrimes 270275635Siedowse p = prefp = NULL; 270374462Salfred if ((opt_flags & OP_MASKLEN) && !maskflg) { 270474462Salfred p = strchr(cp, '/'); 270574462Salfred *p = '\0'; 270674462Salfred prefp = p + 1; 270774462Salfred } 270874462Salfred 270975861Siedowse /* 271075861Siedowse * Check for a numeric address first. We wish to avoid 271175861Siedowse * possible DNS lookups in getnetbyname(). 271275861Siedowse */ 271375861Siedowse if (isxdigit(*cp) || *cp == ':') { 271474462Salfred memset(&hints, 0, sizeof hints); 271575801Siedowse /* Ensure the mask and the network have the same family. */ 271675801Siedowse if (maskflg && (opt_flags & OP_NET)) 271775801Siedowse hints.ai_family = net->nt_net.ss_family; 271875801Siedowse else if (!maskflg && (opt_flags & OP_HAVEMASK)) 271975801Siedowse hints.ai_family = net->nt_mask.ss_family; 272075801Siedowse else 272175801Siedowse hints.ai_family = AF_UNSPEC; 272274462Salfred hints.ai_flags = AI_NUMERICHOST; 272375861Siedowse if (getaddrinfo(cp, NULL, &hints, &ai) == 0) 272475861Siedowse sa = ai->ai_addr; 272575861Siedowse if (sa != NULL && ai->ai_family == AF_INET) { 272674462Salfred /* 272775801Siedowse * The address in `cp' is really a network address, so 272875801Siedowse * use inet_network() to re-interpret this correctly. 272975801Siedowse * e.g. "127.1" means 127.1.0.0, not 127.0.0.1. 273074462Salfred */ 273175801Siedowse bzero(&sin, sizeof sin); 273274462Salfred sin.sin_family = AF_INET; 273374462Salfred sin.sin_len = sizeof sin; 273475801Siedowse sin.sin_addr = inet_makeaddr(inet_network(cp), 0); 273574462Salfred if (debug) 273675801Siedowse fprintf(stderr, "get_net: v4 addr %s\n", 273775801Siedowse inet_ntoa(sin.sin_addr)); 273874462Salfred sa = (struct sockaddr *)&sin; 273975861Siedowse } 274075861Siedowse } 274175861Siedowse if (sa == NULL && (np = getnetbyname(cp)) != NULL) { 274275861Siedowse bzero(&sin, sizeof sin); 274375861Siedowse sin.sin_family = AF_INET; 274475861Siedowse sin.sin_len = sizeof sin; 274575861Siedowse sin.sin_addr = inet_makeaddr(np->n_net, 0); 274675861Siedowse sa = (struct sockaddr *)&sin; 274775861Siedowse } 274875861Siedowse if (sa == NULL) 274974462Salfred goto fail; 275025318Spst 275175801Siedowse if (maskflg) { 275275801Siedowse /* The specified sockaddr is a mask. */ 275375801Siedowse if (checkmask(sa) != 0) 275475801Siedowse goto fail; 275575801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 275675801Siedowse opt_flags |= OP_HAVEMASK; 275775801Siedowse } else { 275875801Siedowse /* The specified sockaddr is a network address. */ 275975801Siedowse bcopy(sa, &net->nt_net, sa->sa_len); 276074462Salfred 276175801Siedowse /* Get a network name for the export list. */ 276275801Siedowse if (np) { 276375801Siedowse name = np->n_name; 276475801Siedowse } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname, 2765146187Sume NULL, 0, NI_NUMERICHOST) == 0) { 276675801Siedowse name = netname; 276775801Siedowse } else { 276875801Siedowse goto fail; 276975801Siedowse } 277075801Siedowse if ((net->nt_name = strdup(name)) == NULL) 277175801Siedowse out_of_mem(); 277275801Siedowse 277375801Siedowse /* 277475801Siedowse * Extract a mask from either a "/<masklen>" suffix, or 277575801Siedowse * from the class of an IPv4 address. 277675801Siedowse */ 277774462Salfred if (opt_flags & OP_MASKLEN) { 277874462Salfred preflen = strtol(prefp, NULL, 10); 277975801Siedowse if (preflen < 0L || preflen == LONG_MAX) 278074462Salfred goto fail; 278175801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 278275801Siedowse if (makemask(&net->nt_mask, (int)preflen) != 0) 278375801Siedowse goto fail; 278475801Siedowse opt_flags |= OP_HAVEMASK; 278574462Salfred *p = '/'; 278675801Siedowse } else if (sa->sa_family == AF_INET && 278775801Siedowse (opt_flags & OP_MASK) == 0) { 278875801Siedowse in_addr_t addr; 278974462Salfred 279075801Siedowse addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr; 279175801Siedowse if (IN_CLASSA(addr)) 279275801Siedowse preflen = 8; 279375801Siedowse else if (IN_CLASSB(addr)) 279475801Siedowse preflen = 16; 279575801Siedowse else if (IN_CLASSC(addr)) 279675801Siedowse preflen = 24; 279775801Siedowse else if (IN_CLASSD(addr)) 279875801Siedowse preflen = 28; 279975801Siedowse else 280075801Siedowse preflen = 32; /* XXX */ 280175801Siedowse 280275801Siedowse bcopy(sa, &net->nt_mask, sa->sa_len); 280375801Siedowse makemask(&net->nt_mask, (int)preflen); 280475801Siedowse opt_flags |= OP_HAVEMASK; 280574462Salfred } 280674462Salfred } 280774462Salfred 280874462Salfred if (ai) 280974462Salfred freeaddrinfo(ai); 281074462Salfred return 0; 281174462Salfred 281274462Salfredfail: 281374462Salfred if (ai) 281474462Salfred freeaddrinfo(ai); 281574462Salfred return 1; 28161558Srgrimes} 28171558Srgrimes 28181558Srgrimes/* 28191558Srgrimes * Parse out the next white space separated field 28201558Srgrimes */ 2821285128Straszstatic void 2822216587Scharniernextfield(char **cp, char **endcp) 28231558Srgrimes{ 28241558Srgrimes char *p; 28251558Srgrimes 28261558Srgrimes p = *cp; 28271558Srgrimes while (*p == ' ' || *p == '\t') 28281558Srgrimes p++; 28291558Srgrimes if (*p == '\n' || *p == '\0') 28301558Srgrimes *cp = *endcp = p; 28311558Srgrimes else { 28321558Srgrimes *cp = p++; 28331558Srgrimes while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0') 28341558Srgrimes p++; 28351558Srgrimes *endcp = p; 28361558Srgrimes } 28371558Srgrimes} 28381558Srgrimes 28391558Srgrimes/* 28401558Srgrimes * Get an exports file line. Skip over blank lines and handle line 28411558Srgrimes * continuations. 28421558Srgrimes */ 2843285128Straszstatic int 2844216587Scharnierget_line(void) 28451558Srgrimes{ 28461558Srgrimes char *p, *cp; 284796622Siedowse size_t len; 28481558Srgrimes int totlen, cont_line; 28491558Srgrimes 28501558Srgrimes /* 28511558Srgrimes * Loop around ignoring blank lines and getting all continuation lines. 28521558Srgrimes */ 28531558Srgrimes p = line; 28541558Srgrimes totlen = 0; 28551558Srgrimes do { 285696622Siedowse if ((p = fgetln(exp_file, &len)) == NULL) 28571558Srgrimes return (0); 28581558Srgrimes cp = p + len - 1; 28591558Srgrimes cont_line = 0; 28601558Srgrimes while (cp >= p && 28611558Srgrimes (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) { 28621558Srgrimes if (*cp == '\\') 28631558Srgrimes cont_line = 1; 28641558Srgrimes cp--; 28651558Srgrimes len--; 28661558Srgrimes } 286779117Sdd if (cont_line) { 286879117Sdd *++cp = ' '; 286979117Sdd len++; 287079117Sdd } 287196622Siedowse if (linesize < len + totlen + 1) { 287296622Siedowse linesize = len + totlen + 1; 287396622Siedowse line = realloc(line, linesize); 287496622Siedowse if (line == NULL) 287596622Siedowse out_of_mem(); 28761558Srgrimes } 287796622Siedowse memcpy(line + totlen, p, len); 287896622Siedowse totlen += len; 287996622Siedowse line[totlen] = '\0'; 28801558Srgrimes } while (totlen == 0 || cont_line); 28811558Srgrimes return (1); 28821558Srgrimes} 28831558Srgrimes 28841558Srgrimes/* 28851558Srgrimes * Parse a description of a credential. 28861558Srgrimes */ 2887285128Straszstatic void 2888216587Scharnierparsecred(char *namelist, struct xucred *cr) 28891558Srgrimes{ 28901558Srgrimes char *name; 28911558Srgrimes int cnt; 28921558Srgrimes char *names; 28931558Srgrimes struct passwd *pw; 28941558Srgrimes struct group *gr; 2895194498Sbrooks gid_t groups[XU_NGROUPS + 1]; 2896136051Sstefanf int ngroups; 28971558Srgrimes 289891354Sdd cr->cr_version = XUCRED_VERSION; 28991558Srgrimes /* 290037663Scharnier * Set up the unprivileged user. 29011558Srgrimes */ 29021558Srgrimes cr->cr_uid = -2; 29031558Srgrimes cr->cr_groups[0] = -2; 29041558Srgrimes cr->cr_ngroups = 1; 29051558Srgrimes /* 29061558Srgrimes * Get the user's password table entry. 29071558Srgrimes */ 2908293305Sjpaetzel names = strsep_quote(&namelist, " \t\n"); 29091558Srgrimes name = strsep(&names, ":"); 2910293305Sjpaetzel /* Bug? name could be NULL here */ 29111558Srgrimes if (isdigit(*name) || *name == '-') 29121558Srgrimes pw = getpwuid(atoi(name)); 29131558Srgrimes else 29141558Srgrimes pw = getpwnam(name); 29151558Srgrimes /* 29161558Srgrimes * Credentials specified as those of a user. 29171558Srgrimes */ 29181558Srgrimes if (names == NULL) { 29191558Srgrimes if (pw == NULL) { 292037663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 29211558Srgrimes return; 29221558Srgrimes } 29231558Srgrimes cr->cr_uid = pw->pw_uid; 2924194498Sbrooks ngroups = XU_NGROUPS + 1; 2925333197Savg if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups)) { 292637663Scharnier syslog(LOG_ERR, "too many groups"); 2927333197Savg ngroups = XU_NGROUPS + 1; 2928333197Savg } 2929333197Savg 29301558Srgrimes /* 2931136051Sstefanf * Compress out duplicate. 29321558Srgrimes */ 29331558Srgrimes cr->cr_ngroups = ngroups - 1; 29341558Srgrimes cr->cr_groups[0] = groups[0]; 29351558Srgrimes for (cnt = 2; cnt < ngroups; cnt++) 29361558Srgrimes cr->cr_groups[cnt - 1] = groups[cnt]; 29371558Srgrimes return; 29381558Srgrimes } 29391558Srgrimes /* 29401558Srgrimes * Explicit credential specified as a colon separated list: 29411558Srgrimes * uid:gid:gid:... 29421558Srgrimes */ 29431558Srgrimes if (pw != NULL) 29441558Srgrimes cr->cr_uid = pw->pw_uid; 29451558Srgrimes else if (isdigit(*name) || *name == '-') 29461558Srgrimes cr->cr_uid = atoi(name); 29471558Srgrimes else { 294837663Scharnier syslog(LOG_ERR, "unknown user: %s", name); 29491558Srgrimes return; 29501558Srgrimes } 29511558Srgrimes cr->cr_ngroups = 0; 2952194498Sbrooks while (names != NULL && *names != '\0' && cr->cr_ngroups < XU_NGROUPS) { 29531558Srgrimes name = strsep(&names, ":"); 29541558Srgrimes if (isdigit(*name) || *name == '-') { 29551558Srgrimes cr->cr_groups[cr->cr_ngroups++] = atoi(name); 29561558Srgrimes } else { 29571558Srgrimes if ((gr = getgrnam(name)) == NULL) { 295837663Scharnier syslog(LOG_ERR, "unknown group: %s", name); 29591558Srgrimes continue; 29601558Srgrimes } 29611558Srgrimes cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid; 29621558Srgrimes } 29631558Srgrimes } 2964194498Sbrooks if (names != NULL && *names != '\0' && cr->cr_ngroups == XU_NGROUPS) 296537663Scharnier syslog(LOG_ERR, "too many groups"); 29661558Srgrimes} 29671558Srgrimes 2968194880Sdfr#define STRSIZ (MNTNAMLEN+MNTPATHLEN+50) 29691558Srgrimes/* 29701558Srgrimes * Routines that maintain the remote mounttab 29711558Srgrimes */ 2972285128Straszstatic void 2973216587Scharnierget_mountlist(void) 29741558Srgrimes{ 2975324955Smanu struct mountlist *mlp; 297623681Speter char *host, *dirp, *cp; 29771558Srgrimes char str[STRSIZ]; 29781558Srgrimes FILE *mlfile; 29791558Srgrimes 29801558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) { 298153117Sbillf if (errno == ENOENT) 298253117Sbillf return; 298353117Sbillf else { 298453117Sbillf syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST); 298553117Sbillf return; 298653117Sbillf } 29871558Srgrimes } 29881558Srgrimes while (fgets(str, STRSIZ, mlfile) != NULL) { 298923681Speter cp = str; 299023681Speter host = strsep(&cp, " \t\n"); 299123681Speter dirp = strsep(&cp, " \t\n"); 299223681Speter if (host == NULL || dirp == NULL) 29931558Srgrimes continue; 29941558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 299537663Scharnier if (mlp == (struct mountlist *)NULL) 299637663Scharnier out_of_mem(); 2997194880Sdfr strncpy(mlp->ml_host, host, MNTNAMLEN); 2998194880Sdfr mlp->ml_host[MNTNAMLEN] = '\0'; 2999194880Sdfr strncpy(mlp->ml_dirp, dirp, MNTPATHLEN); 3000194880Sdfr mlp->ml_dirp[MNTPATHLEN] = '\0'; 3001324955Smanu 3002324955Smanu SLIST_INSERT_HEAD(&mlhead, mlp, next); 30031558Srgrimes } 30041558Srgrimes fclose(mlfile); 30051558Srgrimes} 30061558Srgrimes 3007285128Straszstatic void 300875635Siedowsedel_mlist(char *hostp, char *dirp) 30091558Srgrimes{ 3010324955Smanu struct mountlist *mlp, *mlp2; 30111558Srgrimes FILE *mlfile; 30121558Srgrimes int fnd = 0; 30131558Srgrimes 3014324955Smanu SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) { 30151558Srgrimes if (!strcmp(mlp->ml_host, hostp) && 30161558Srgrimes (!dirp || !strcmp(mlp->ml_dirp, dirp))) { 30171558Srgrimes fnd = 1; 3018324955Smanu SLIST_REMOVE(&mlhead, mlp, mountlist, next); 3019324955Smanu free((caddr_t)mlp); 30201558Srgrimes } 30211558Srgrimes } 30221558Srgrimes if (fnd) { 30231558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) { 302437663Scharnier syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST); 30251558Srgrimes return; 30261558Srgrimes } 3027324955Smanu SLIST_FOREACH(mlp, &mlhead, next) { 30281558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 30291558Srgrimes } 30301558Srgrimes fclose(mlfile); 30311558Srgrimes } 30321558Srgrimes} 30331558Srgrimes 3034285128Straszstatic void 3035216587Scharnieradd_mlist(char *hostp, char *dirp) 30361558Srgrimes{ 3037324955Smanu struct mountlist *mlp; 30381558Srgrimes FILE *mlfile; 30391558Srgrimes 3040324955Smanu SLIST_FOREACH(mlp, &mlhead, next) { 30411558Srgrimes if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp)) 30421558Srgrimes return; 30431558Srgrimes } 3044324955Smanu 30451558Srgrimes mlp = (struct mountlist *)malloc(sizeof (*mlp)); 304637663Scharnier if (mlp == (struct mountlist *)NULL) 304737663Scharnier out_of_mem(); 3048194880Sdfr strncpy(mlp->ml_host, hostp, MNTNAMLEN); 3049194880Sdfr mlp->ml_host[MNTNAMLEN] = '\0'; 3050194880Sdfr strncpy(mlp->ml_dirp, dirp, MNTPATHLEN); 3051194880Sdfr mlp->ml_dirp[MNTPATHLEN] = '\0'; 3052324955Smanu SLIST_INSERT_HEAD(&mlhead, mlp, next); 30531558Srgrimes if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) { 305437663Scharnier syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST); 30551558Srgrimes return; 30561558Srgrimes } 30571558Srgrimes fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp); 30581558Srgrimes fclose(mlfile); 30591558Srgrimes} 30601558Srgrimes 30611558Srgrimes/* 30621558Srgrimes * Free up a group list. 30631558Srgrimes */ 3064285128Straszstatic void 3065216587Scharnierfree_grp(struct grouplist *grp) 30661558Srgrimes{ 30671558Srgrimes if (grp->gr_type == GT_HOST) { 306874462Salfred if (grp->gr_ptr.gt_addrinfo != NULL) 306974462Salfred freeaddrinfo(grp->gr_ptr.gt_addrinfo); 30701558Srgrimes } else if (grp->gr_type == GT_NET) { 30711558Srgrimes if (grp->gr_ptr.gt_net.nt_name) 30721558Srgrimes free(grp->gr_ptr.gt_net.nt_name); 30731558Srgrimes } 30741558Srgrimes free((caddr_t)grp); 30751558Srgrimes} 30761558Srgrimes 30771558Srgrimes#ifdef DEBUG 3078285128Straszstatic void 30791558SrgrimesSYSLOG(int pri, const char *fmt, ...) 30801558Srgrimes{ 30811558Srgrimes va_list ap; 30821558Srgrimes 30831558Srgrimes va_start(ap, fmt); 30841558Srgrimes vfprintf(stderr, fmt, ap); 30851558Srgrimes va_end(ap); 30861558Srgrimes} 30871558Srgrimes#endif /* DEBUG */ 30881558Srgrimes 30891558Srgrimes/* 30901558Srgrimes * Check options for consistency. 30911558Srgrimes */ 3092285128Straszstatic int 3093216587Scharniercheck_options(struct dirlist *dp) 30941558Srgrimes{ 30951558Srgrimes 3096192934Srmacklem if (v4root_phase == 0 && dp == NULL) 30971558Srgrimes return (1); 309883653Speter if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) { 309983653Speter syslog(LOG_ERR, "-mapall and -maproot mutually exclusive"); 31001558Srgrimes return (1); 31011558Srgrimes } 31021558Srgrimes if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) { 310375801Siedowse syslog(LOG_ERR, "-mask requires -network"); 310475801Siedowse return (1); 31051558Srgrimes } 310675801Siedowse if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) { 310775801Siedowse syslog(LOG_ERR, "-network requires mask specification"); 310875801Siedowse return (1); 310975801Siedowse } 311075801Siedowse if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) { 311175801Siedowse syslog(LOG_ERR, "-mask and /masklen are mutually exclusive"); 311275801Siedowse return (1); 311375801Siedowse } 3114192934Srmacklem if (v4root_phase > 0 && 3115192934Srmacklem (opt_flags & 3116192934Srmacklem ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) { 3117192934Srmacklem syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:"); 3118192934Srmacklem return (1); 3119192934Srmacklem } 3120207689Srmacklem if ((opt_flags & OP_ALLDIRS) && dp->dp_left) { 3121207689Srmacklem syslog(LOG_ERR, "-alldirs has multiple directories"); 3122207689Srmacklem return (1); 3123207689Srmacklem } 31241558Srgrimes return (0); 31251558Srgrimes} 31261558Srgrimes 31271558Srgrimes/* 31281558Srgrimes * Check an absolute directory path for any symbolic links. Return true 31291558Srgrimes */ 3130285128Straszstatic int 3131216587Scharniercheck_dirpath(char *dirp) 31321558Srgrimes{ 31331558Srgrimes char *cp; 31341558Srgrimes int ret = 1; 31351558Srgrimes struct stat sb; 31361558Srgrimes 31371558Srgrimes cp = dirp + 1; 31381558Srgrimes while (*cp && ret) { 31391558Srgrimes if (*cp == '/') { 31401558Srgrimes *cp = '\0'; 31419336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 31421558Srgrimes ret = 0; 31431558Srgrimes *cp = '/'; 31441558Srgrimes } 31451558Srgrimes cp++; 31461558Srgrimes } 31479336Sdfr if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode)) 31481558Srgrimes ret = 0; 31491558Srgrimes return (ret); 31501558Srgrimes} 31519336Sdfr 315275801Siedowse/* 315375801Siedowse * Make a netmask according to the specified prefix length. The ss_family 315475801Siedowse * and other non-address fields must be initialised before calling this. 315575801Siedowse */ 3156285128Straszstatic int 315775801Siedowsemakemask(struct sockaddr_storage *ssp, int bitlen) 315874462Salfred{ 315975801Siedowse u_char *p; 316075801Siedowse int bits, i, len; 316174462Salfred 316275801Siedowse if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL) 316375801Siedowse return (-1); 3164103949Smike if (bitlen > len * CHAR_BIT) 316575801Siedowse return (-1); 316674462Salfred 316775801Siedowse for (i = 0; i < len; i++) { 3168298912Saraujo bits = MIN(CHAR_BIT, bitlen); 3169219125Sru *p++ = (u_char)~0 << (CHAR_BIT - bits); 317075801Siedowse bitlen -= bits; 317174462Salfred } 317275801Siedowse return 0; 317374462Salfred} 317474462Salfred 317575801Siedowse/* 317675801Siedowse * Check that the sockaddr is a valid netmask. Returns 0 if the mask 317775801Siedowse * is acceptable (i.e. of the form 1...10....0). 317875801Siedowse */ 3179285128Straszstatic int 318075801Siedowsecheckmask(struct sockaddr *sa) 318174462Salfred{ 318275801Siedowse u_char *mask; 318375801Siedowse int i, len; 318474462Salfred 318575801Siedowse if ((mask = sa_rawaddr(sa, &len)) == NULL) 318675801Siedowse return (-1); 318775801Siedowse 318875801Siedowse for (i = 0; i < len; i++) 318975801Siedowse if (mask[i] != 0xff) 319075801Siedowse break; 319175801Siedowse if (i < len) { 319275801Siedowse if (~mask[i] & (u_char)(~mask[i] + 1)) 319375801Siedowse return (-1); 319475801Siedowse i++; 319574462Salfred } 319675801Siedowse for (; i < len; i++) 319775801Siedowse if (mask[i] != 0) 319875801Siedowse return (-1); 319975801Siedowse return (0); 320074462Salfred} 320174462Salfred 320275801Siedowse/* 320375801Siedowse * Compare two sockaddrs according to a specified mask. Return zero if 320475801Siedowse * `sa1' matches `sa2' when filtered by the netmask in `samask'. 3205228990Suqs * If samask is NULL, perform a full comparison. 320675801Siedowse */ 3207285128Straszstatic int 320875801Siedowsesacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask) 320974462Salfred{ 321075801Siedowse unsigned char *p1, *p2, *mask; 321175801Siedowse int len, i; 321274462Salfred 321375801Siedowse if (sa1->sa_family != sa2->sa_family || 321475801Siedowse (p1 = sa_rawaddr(sa1, &len)) == NULL || 321575801Siedowse (p2 = sa_rawaddr(sa2, NULL)) == NULL) 321675801Siedowse return (1); 321775801Siedowse 321875801Siedowse switch (sa1->sa_family) { 321974462Salfred case AF_INET6: 322075801Siedowse if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 322175801Siedowse ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 322275801Siedowse return (1); 322374462Salfred break; 322474462Salfred } 322574462Salfred 322675801Siedowse /* Simple binary comparison if no mask specified. */ 322775801Siedowse if (samask == NULL) 322875801Siedowse return (memcmp(p1, p2, len)); 322974462Salfred 323075801Siedowse /* Set up the mask, and do a mask-based comparison. */ 323175801Siedowse if (sa1->sa_family != samask->sa_family || 323275801Siedowse (mask = sa_rawaddr(samask, NULL)) == NULL) 323375801Siedowse return (1); 323474462Salfred 323575801Siedowse for (i = 0; i < len; i++) 323675801Siedowse if ((p1[i] & mask[i]) != (p2[i] & mask[i])) 323775801Siedowse return (1); 323875801Siedowse return (0); 323974462Salfred} 324074462Salfred 324175801Siedowse/* 324275801Siedowse * Return a pointer to the part of the sockaddr that contains the 324375801Siedowse * raw address, and set *nbytes to its length in bytes. Returns 324475801Siedowse * NULL if the address family is unknown. 324575801Siedowse */ 3246285128Straszstatic void * 324775801Siedowsesa_rawaddr(struct sockaddr *sa, int *nbytes) { 324875801Siedowse void *p; 324974462Salfred int len; 325074462Salfred 325175801Siedowse switch (sa->sa_family) { 325274462Salfred case AF_INET: 325375801Siedowse len = sizeof(((struct sockaddr_in *)sa)->sin_addr); 325475801Siedowse p = &((struct sockaddr_in *)sa)->sin_addr; 325574462Salfred break; 325674462Salfred case AF_INET6: 325775801Siedowse len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr); 325875801Siedowse p = &((struct sockaddr_in6 *)sa)->sin6_addr; 325974462Salfred break; 326074462Salfred default: 326175801Siedowse p = NULL; 326275801Siedowse len = 0; 326374462Salfred } 326474462Salfred 326575801Siedowse if (nbytes != NULL) 326675801Siedowse *nbytes = len; 326775801Siedowse return (p); 326874462Salfred} 326974462Salfred 3270285128Straszstatic void 3271216587Scharnierhuphandler(int sig __unused) 327275754Siedowse{ 3273285128Strasz 327475754Siedowse got_sighup = 1; 327575754Siedowse} 327675754Siedowse 3277285128Straszstatic void 3278285128Straszterminate(int sig __unused) 327974462Salfred{ 3280149433Spjd pidfile_remove(pfh); 3281194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS, NULL); 3282194880Sdfr rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL); 328374462Salfred exit (0); 328474462Salfred} 3285