1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Herb Hasler and Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifndef lint
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #endif /*not lint*/
40
41 #if 0
42 #ifndef lint
43 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
44 #endif /*not lint*/
45 #endif
46
47 #include <sys/cdefs.h>
48 #include <sys/param.h>
49 #include <sys/conf.h>
50 #include <sys/fcntl.h>
51 #include <sys/fnv_hash.h>
52 #include <sys/linker.h>
53 #include <sys/module.h>
54 #include <sys/mount.h>
55 #include <sys/queue.h>
56 #include <sys/stat.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59
60 #include <rpc/rpc.h>
61 #include <rpc/rpc_com.h>
62 #include <rpc/pmap_clnt.h>
63 #include <rpc/pmap_prot.h>
64 #include <rpcsvc/mount.h>
65 #include <nfs/nfsproto.h>
66 #include <nfs/nfssvc.h>
67 #include <nfsserver/nfs.h>
68
69 #include <fs/nfs/nfsport.h>
70
71 #include <arpa/inet.h>
72
73 #include <assert.h>
74 #include <ctype.h>
75 #include <err.h>
76 #include <errno.h>
77 #include <grp.h>
78 #include <libutil.h>
79 #include <limits.h>
80 #include <netdb.h>
81 #include <pwd.h>
82 #include <signal.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87 #include "pathnames.h"
88 #include "mntopts.h"
89
90 #ifdef DEBUG
91 #include <stdarg.h>
92 #endif
93
94 /*
95 * Structures for keeping the mount list and export list
96 */
97 struct mountlist {
98 char ml_host[MNTNAMLEN+1];
99 char ml_dirp[MNTPATHLEN+1];
100
101 SLIST_ENTRY(mountlist) next;
102 };
103
104 struct dirlist {
105 struct dirlist *dp_left;
106 struct dirlist *dp_right;
107 int dp_flag;
108 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
109 char *dp_dirp;
110 };
111 /* dp_flag bits */
112 #define DP_DEFSET 0x1
113 #define DP_HOSTSET 0x2
114
115 /*
116 * maproot/mapall credentials.
117 * cr_smallgrps can be used for a group list up to SMALLNGROUPS in size.
118 * Larger group lists are malloc'd/free'd.
119 */
120 #define SMALLNGROUPS 32
121 struct expcred {
122 uid_t cr_uid;
123 int cr_ngroups;
124 gid_t cr_smallgrps[SMALLNGROUPS];
125 gid_t *cr_groups;
126 };
127
128 struct exportlist {
129 struct dirlist *ex_dirl;
130 struct dirlist *ex_defdir;
131 struct grouplist *ex_grphead;
132 int ex_flag;
133 fsid_t ex_fs;
134 char *ex_fsdir;
135 char *ex_indexfile;
136 struct expcred ex_defanon;
137 uint64_t ex_defexflags;
138 int ex_numsecflavors;
139 int ex_secflavors[MAXSECFLAVORS];
140 int ex_defnumsecflavors;
141 int ex_defsecflavors[MAXSECFLAVORS];
142
143 SLIST_ENTRY(exportlist) entries;
144 };
145 /* ex_flag bits */
146 #define EX_LINKED 0x1
147 #define EX_DONE 0x2
148 #define EX_DEFSET 0x4
149 #define EX_PUBLICFH 0x8
150
151 SLIST_HEAD(exportlisthead, exportlist);
152
153 struct netmsk {
154 struct sockaddr_storage nt_net;
155 struct sockaddr_storage nt_mask;
156 char *nt_name;
157 };
158
159 union grouptypes {
160 struct addrinfo *gt_addrinfo;
161 struct netmsk gt_net;
162 };
163
164 struct grouplist {
165 int gr_type;
166 union grouptypes gr_ptr;
167 struct grouplist *gr_next;
168 struct expcred gr_anon;
169 uint64_t gr_exflags;
170 int gr_flag;
171 int gr_numsecflavors;
172 int gr_secflavors[MAXSECFLAVORS];
173 };
174 /* Group types */
175 #define GT_NULL 0x0
176 #define GT_HOST 0x1
177 #define GT_NET 0x2
178 #define GT_DEFAULT 0x3
179 #define GT_IGNORE 0x5
180
181 /* Group flags */
182 #define GR_FND 0x1
183
184 struct hostlist {
185 int ht_flag; /* Uses DP_xx bits */
186 struct grouplist *ht_grp;
187 struct hostlist *ht_next;
188 };
189
190 struct fhreturn {
191 int fhr_flag;
192 int fhr_vers;
193 nfsfh_t fhr_fh;
194 int fhr_numsecflavors;
195 int *fhr_secflavors;
196 };
197
198 #define GETPORT_MAXTRY 20 /* Max tries to get a port # */
199
200 /*
201 * How long to delay a reload of exports when there are RPC request(s)
202 * to process, in usec. Must be less than 1second.
203 */
204 #define RELOADDELAY 250000
205
206 /* Global defs */
207 static char *add_expdir(struct dirlist **, char *, int);
208 static void add_dlist(struct dirlist **, struct dirlist *,
209 struct grouplist *, int, struct exportlist *,
210 struct expcred *, uint64_t);
211 static void add_mlist(char *, char *);
212 static int check_dirpath(char *);
213 static int check_options(struct dirlist *);
214 static int checkmask(struct sockaddr *sa);
215 static int chk_host(struct dirlist *, struct sockaddr *, int *, int *,
216 int *, int **);
217 static char *strsep_quote(char **stringp, const char *delim);
218 static int create_service(struct netconfig *nconf);
219 static void complete_service(struct netconfig *nconf, char *port_str);
220 static void clearout_service(void);
221 static void del_mlist(char *hostp, char *dirp);
222 static struct dirlist *dirp_search(struct dirlist *, char *);
223 static int do_export_mount(struct exportlist *, struct statfs *);
224 static int do_mount(struct exportlist *, struct grouplist *, uint64_t,
225 struct expcred *, char *, int, struct statfs *, int, int *);
226 static int do_opt(char **, char **, struct exportlist *,
227 struct grouplist *, int *, uint64_t *, struct expcred *);
228 static struct exportlist *ex_search(fsid_t *, struct exportlisthead *);
229 static struct exportlist *get_exp(void);
230 static void free_dir(struct dirlist *);
231 static void free_exp(struct exportlist *);
232 static void free_grp(struct grouplist *);
233 static void free_host(struct hostlist *);
234 static void free_v4rootexp(void);
235 static void get_exportlist_one(int);
236 static void get_exportlist(int);
237 static void insert_exports(struct exportlist *, struct exportlisthead *);
238 static void free_exports(struct exportlisthead *);
239 static void read_exportfile(int);
240 static int compare_nmount_exportlist(struct iovec *, int, char *);
241 static int compare_export(struct exportlist *, struct exportlist *);
242 static int compare_addr(struct grouplist *, struct grouplist *);
243 static int compare_cred(struct expcred *, struct expcred *);
244 static int compare_secflavor(int *, int *, int);
245 static void delete_export(struct iovec *, int, struct statfs *, char *);
246 static int get_host(char *, struct grouplist *, struct grouplist *);
247 static struct hostlist *get_ht(void);
248 static int get_line(void);
249 static void get_mountlist(void);
250 static int get_net(char *, struct netmsk *, int);
251 static void getexp_err(struct exportlist *, struct grouplist *, const char *);
252 static struct grouplist *get_grp(void);
253 static void hang_dirp(struct dirlist *, struct grouplist *,
254 struct exportlist *, int, struct expcred *, uint64_t);
255 static void huphandler(int sig);
256 static int makemask(struct sockaddr_storage *ssp, int bitlen);
257 static void mntsrv(struct svc_req *, SVCXPRT *);
258 static void nextfield(char **, char **);
259 static void out_of_mem(void) __dead2;
260 static void parsecred(char *, struct expcred *);
261 static int parsesec(char *, struct exportlist *);
262 static int put_exlist(struct dirlist *, XDR *, struct dirlist *,
263 int *, int);
264 static void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
265 static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
266 struct sockaddr *samask);
267 static int scan_tree(struct dirlist *, struct sockaddr *);
268 static void usage(void);
269 static int xdr_dir(XDR *, char *);
270 static int xdr_explist(XDR *, caddr_t);
271 static int xdr_explist_brief(XDR *, caddr_t);
272 static int xdr_explist_common(XDR *, caddr_t, int);
273 static int xdr_fhs(XDR *, caddr_t);
274 static int xdr_mlist(XDR *, caddr_t);
275 static void terminate(int);
276 static void cp_cred(struct expcred *, struct expcred *);
277
278 static gid_t nogroup();
279
280 #define EXPHASH(f) (fnv_32_buf((f), sizeof(fsid_t), 0) % exphashsize)
281 static struct exportlisthead *exphead = NULL;
282 static struct exportlisthead *oldexphead = NULL;
283 static int exphashsize = 0;
284 static SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(&mlhead);
285 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
286 static char **exnames;
287 static char **hosts = NULL;
288 static int force_v2 = 0;
289 static int resvport_only = 1;
290 static int nhosts = 0;
291 static int dir_only = 1;
292 static int dolog = 0;
293 static int got_sighup = 0;
294 static int xcreated = 0;
295
296 static char *svcport_str = NULL;
297 static int mallocd_svcport = 0;
298 static int *sock_fd;
299 static int sock_fdcnt;
300 static int sock_fdpos;
301 static int suspend_nfsd = 0;
302
303 static int opt_flags;
304 static int have_v6 = 1;
305
306 static int v4root_phase = 0;
307 static char v4root_dirpath[PATH_MAX + 1];
308 static struct exportlist *v4root_ep = NULL;
309 static int has_publicfh = 0;
310 static int has_set_publicfh = 0;
311
312 static struct pidfh *pfh = NULL;
313
314 /* Temporary storage for credentials' groups. */
315 static long tngroups_max;
316 static gid_t *tmp_groups = NULL;
317
318 /* Bits for opt_flags above */
319 #define OP_MAPROOT 0x01
320 #define OP_MAPALL 0x02
321 /* 0x4 free */
322 #define OP_MASK 0x08
323 #define OP_NET 0x10
324 #define OP_ALLDIRS 0x40
325 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
326 #define OP_QUIET 0x100
327 #define OP_MASKLEN 0x200
328 #define OP_SEC 0x400
329 #define OP_CLASSMASK 0x800 /* mask not specified, is Class A/B/C default */
330
331 #ifdef DEBUG
332 static int debug = 1;
333 static void SYSLOG(int, const char *, ...) __printflike(2, 3);
334 #define syslog SYSLOG
335 #else
336 static int debug = 0;
337 #endif
338
339 /*
340 * The LOGDEBUG() syslog() calls are always compiled into the daemon.
341 * To enable them, create a file at _PATH_MOUNTDDEBUG. This file can be empty.
342 * To disable the logging, just delete the file at _PATH_MOUNTDDEBUG.
343 */
344 static int logdebug = 0;
345 #define LOGDEBUG(format, ...) \
346 (logdebug ? syslog(LOG_DEBUG, format, ## __VA_ARGS__) : 0)
347
348 /*
349 * Similar to strsep(), but it allows for quoted strings
350 * and escaped characters.
351 *
352 * It returns the string (or NULL, if *stringp is NULL),
353 * which is a de-quoted version of the string if necessary.
354 *
355 * It modifies *stringp in place.
356 */
357 static char *
strsep_quote(char ** stringp,const char * delim)358 strsep_quote(char **stringp, const char *delim)
359 {
360 char *srcptr, *dstptr, *retval;
361 char quot = 0;
362
363 if (stringp == NULL || *stringp == NULL)
364 return (NULL);
365
366 srcptr = dstptr = retval = *stringp;
367
368 while (*srcptr) {
369 /*
370 * We're looking for several edge cases here.
371 * First: if we're in quote state (quot != 0),
372 * then we ignore the delim characters, but otherwise
373 * process as normal, unless it is the quote character.
374 * Second: if the current character is a backslash,
375 * we take the next character as-is, without checking
376 * for delim, quote, or backslash. Exception: if the
377 * next character is a NUL, that's the end of the string.
378 * Third: if the character is a quote character, we toggle
379 * quote state.
380 * Otherwise: check the current character for NUL, or
381 * being in delim, and end the string if either is true.
382 */
383 if (*srcptr == '\\') {
384 srcptr++;
385 /*
386 * The edge case here is if the next character
387 * is NUL, we want to stop processing. But if
388 * it's not NUL, then we simply want to copy it.
389 */
390 if (*srcptr) {
391 *dstptr++ = *srcptr++;
392 }
393 continue;
394 }
395 if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) {
396 quot = *srcptr++;
397 continue;
398 }
399 if (quot && *srcptr == quot) {
400 /* End of the quoted part */
401 quot = 0;
402 srcptr++;
403 continue;
404 }
405 if (!quot && strchr(delim, *srcptr))
406 break;
407 *dstptr++ = *srcptr++;
408 }
409
410 *stringp = (*srcptr == '\0') ? NULL : srcptr + 1;
411 *dstptr = 0; /* Terminate the string */
412 return (retval);
413 }
414
415 /*
416 * Mountd server for NFS mount protocol as described in:
417 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
418 * The optional arguments are the exports file name
419 * default: _PATH_EXPORTS
420 * and "-n" to allow nonroot mount.
421 */
422 int
main(int argc,char ** argv)423 main(int argc, char **argv)
424 {
425 fd_set readfds;
426 struct netconfig *nconf;
427 char *endptr, **hosts_bak;
428 void *nc_handle;
429 pid_t otherpid;
430 in_port_t svcport;
431 int c, k, s;
432 int maxrec = RPC_MAXDATASIZE;
433 int attempt_cnt, port_len, port_pos, ret;
434 char **port_list;
435 uint64_t curtime, nexttime;
436 struct timeval tv;
437 struct timespec tp;
438 sigset_t sig_mask, sighup_mask;
439 int enable_rpcbind;
440
441 enable_rpcbind = 1;
442 /* Check that another mountd isn't already running. */
443 pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid);
444 if (pfh == NULL) {
445 if (errno == EEXIST)
446 errx(1, "mountd already running, pid: %d.", otherpid);
447 warn("cannot open or create pidfile");
448 }
449
450 openlog("mountd", LOG_PID, LOG_DAEMON);
451
452 /* How many groups do we support? */
453 tngroups_max = sysconf(_SC_NGROUPS_MAX);
454 if (tngroups_max == -1)
455 tngroups_max = NGROUPS_MAX;
456 /* Add space for the effective GID. */
457 ++tngroups_max;
458 tmp_groups = malloc(tngroups_max);
459 if (tmp_groups == NULL)
460 out_of_mem();
461
462 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
463 if (s < 0)
464 have_v6 = 0;
465 else
466 close(s);
467
468 while ((c = getopt(argc, argv, "2deh:lnp:RrS")) != -1)
469 switch (c) {
470 case '2':
471 force_v2 = 1;
472 break;
473 case 'e':
474 /* now a no-op, since this is the default */
475 break;
476 case 'n':
477 resvport_only = 0;
478 break;
479 case 'R':
480 /* Do not support Mount protocol */
481 enable_rpcbind = 0;
482 break;
483 case 'r':
484 dir_only = 0;
485 break;
486 case 'd':
487 debug = debug ? 0 : 1;
488 break;
489 case 'l':
490 dolog = 1;
491 break;
492 case 'p':
493 endptr = NULL;
494 svcport = (in_port_t)strtoul(optarg, &endptr, 10);
495 if (endptr == NULL || *endptr != '\0' ||
496 svcport == 0 || svcport >= IPPORT_MAX)
497 usage();
498 svcport_str = strdup(optarg);
499 break;
500 case 'h':
501 ++nhosts;
502 hosts_bak = hosts;
503 hosts_bak = realloc(hosts, nhosts * sizeof(char *));
504 if (hosts_bak == NULL) {
505 if (hosts != NULL) {
506 for (k = 0; k < nhosts; k++)
507 free(hosts[k]);
508 free(hosts);
509 out_of_mem();
510 }
511 }
512 hosts = hosts_bak;
513 hosts[nhosts - 1] = strdup(optarg);
514 if (hosts[nhosts - 1] == NULL) {
515 for (k = 0; k < (nhosts - 1); k++)
516 free(hosts[k]);
517 free(hosts);
518 out_of_mem();
519 }
520 break;
521 case 'S':
522 suspend_nfsd = 1;
523 break;
524 default:
525 usage();
526 }
527 if (enable_rpcbind == 0) {
528 if (svcport_str != NULL) {
529 warnx("-p option not compatible with -R, ignored");
530 free(svcport_str);
531 svcport_str = NULL;
532 }
533 if (nhosts > 0) {
534 warnx("-h option not compatible with -R, ignored");
535 for (k = 0; k < nhosts; k++)
536 free(hosts[k]);
537 free(hosts);
538 hosts = NULL;
539 nhosts = 0;
540 }
541 }
542
543 if (modfind("nfsd") < 0) {
544 /* Not present in kernel, try loading it */
545 if (kldload("nfsd") < 0 || modfind("nfsd") < 0)
546 errx(1, "NFS server is not available");
547 }
548
549 argc -= optind;
550 argv += optind;
551 if (argc > 0)
552 exnames = argv;
553 else
554 exnames = exnames_default;
555 if (debug)
556 warnx("getting export list");
557 get_exportlist(0);
558 if (debug)
559 warnx("getting mount list");
560 get_mountlist();
561 if (debug)
562 warnx("here we go");
563 if (debug == 0) {
564 daemon(0, 0);
565 signal(SIGINT, SIG_IGN);
566 signal(SIGQUIT, SIG_IGN);
567 }
568 signal(SIGHUP, huphandler);
569 signal(SIGTERM, terminate);
570 signal(SIGPIPE, SIG_IGN);
571
572 pidfile_write(pfh);
573
574 if (enable_rpcbind != 0) {
575 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
576 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
577 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
578
579 if (!resvport_only) {
580 if (sysctlbyname("vfs.nfsd.nfs_privport", NULL, NULL,
581 &resvport_only, sizeof(resvport_only)) != 0 &&
582 errno != ENOENT) {
583 syslog(LOG_ERR, "sysctl: %m");
584 exit(1);
585 }
586 }
587
588 /*
589 * If no hosts were specified, add a wildcard entry to bind to
590 * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added
591 * to the list.
592 */
593 if (nhosts == 0) {
594 hosts = malloc(sizeof(char *));
595 if (hosts == NULL)
596 out_of_mem();
597 hosts[0] = "*";
598 nhosts = 1;
599 } else {
600 hosts_bak = hosts;
601 if (have_v6) {
602 hosts_bak = realloc(hosts, (nhosts + 2) *
603 sizeof(char *));
604 if (hosts_bak == NULL) {
605 for (k = 0; k < nhosts; k++)
606 free(hosts[k]);
607 free(hosts);
608 out_of_mem();
609 } else
610 hosts = hosts_bak;
611 nhosts += 2;
612 hosts[nhosts - 2] = "::1";
613 } else {
614 hosts_bak = realloc(hosts, (nhosts + 1) *
615 sizeof(char *));
616 if (hosts_bak == NULL) {
617 for (k = 0; k < nhosts; k++)
618 free(hosts[k]);
619 free(hosts);
620 out_of_mem();
621 } else {
622 nhosts += 1;
623 hosts = hosts_bak;
624 }
625 }
626
627 hosts[nhosts - 1] = "127.0.0.1";
628 }
629 }
630
631 attempt_cnt = 1;
632 sock_fdcnt = 0;
633 sock_fd = NULL;
634 port_list = NULL;
635 port_len = 0;
636 if (enable_rpcbind != 0) {
637 nc_handle = setnetconfig();
638 while ((nconf = getnetconfig(nc_handle))) {
639 if (nconf->nc_flag & NC_VISIBLE) {
640 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
641 "inet6") == 0) {
642 /* DO NOTHING */
643 } else {
644 ret = create_service(nconf);
645 if (ret == 1)
646 /* Ignore this call */
647 continue;
648 if (ret < 0) {
649 /*
650 * Failed to bind port, so close
651 * off all sockets created and
652 * try again if the port# was
653 * dynamically assigned via
654 * bind(2).
655 */
656 clearout_service();
657 if (mallocd_svcport != 0 &&
658 attempt_cnt <
659 GETPORT_MAXTRY) {
660 free(svcport_str);
661 svcport_str = NULL;
662 mallocd_svcport = 0;
663 } else {
664 errno = EADDRINUSE;
665 syslog(LOG_ERR,
666 "bindresvport_sa:"
667 " %m");
668 exit(1);
669 }
670
671 /*
672 * Start over at the first
673 * service.
674 */
675 free(sock_fd);
676 sock_fdcnt = 0;
677 sock_fd = NULL;
678 nc_handle = setnetconfig();
679 attempt_cnt++;
680 } else if (mallocd_svcport != 0 &&
681 attempt_cnt == GETPORT_MAXTRY) {
682 /*
683 * For the last attempt, allow
684 * different port #s for each
685 * nconf by saving the
686 * svcport_str setting it back
687 * to NULL.
688 */
689 port_list = realloc(port_list,
690 (port_len + 1) *
691 sizeof(char *));
692 if (port_list == NULL)
693 out_of_mem();
694 port_list[port_len++] =
695 svcport_str;
696 svcport_str = NULL;
697 mallocd_svcport = 0;
698 }
699 }
700 }
701 }
702
703 /*
704 * Successfully bound the ports, so call complete_service() to
705 * do the rest of the setup on the service(s).
706 */
707 sock_fdpos = 0;
708 port_pos = 0;
709 nc_handle = setnetconfig();
710 while ((nconf = getnetconfig(nc_handle))) {
711 if (nconf->nc_flag & NC_VISIBLE) {
712 if (have_v6 == 0 && strcmp(nconf->nc_protofmly,
713 "inet6") == 0) {
714 /* DO NOTHING */
715 } else if (port_list != NULL) {
716 if (port_pos >= port_len) {
717 syslog(LOG_ERR, "too many"
718 " port#s");
719 exit(1);
720 }
721 complete_service(nconf,
722 port_list[port_pos++]);
723 } else
724 complete_service(nconf, svcport_str);
725 }
726 }
727 endnetconfig(nc_handle);
728 free(sock_fd);
729 if (port_list != NULL) {
730 for (port_pos = 0; port_pos < port_len; port_pos++)
731 free(port_list[port_pos]);
732 free(port_list);
733 }
734
735 if (xcreated == 0) {
736 syslog(LOG_ERR, "could not create any services");
737 exit(1);
738 }
739 }
740
741 /* Expand svc_run() here so that we can call get_exportlist(). */
742 curtime = nexttime = 0;
743 sigemptyset(&sighup_mask);
744 sigaddset(&sighup_mask, SIGHUP);
745 for (;;) {
746 clock_gettime(CLOCK_MONOTONIC, &tp);
747 curtime = tp.tv_sec;
748 curtime = curtime * 1000000 + tp.tv_nsec / 1000;
749 sigprocmask(SIG_BLOCK, &sighup_mask, &sig_mask);
750 if (got_sighup && curtime >= nexttime) {
751 got_sighup = 0;
752 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
753 get_exportlist(1);
754 clock_gettime(CLOCK_MONOTONIC, &tp);
755 nexttime = tp.tv_sec;
756 nexttime = nexttime * 1000000 + tp.tv_nsec / 1000 +
757 RELOADDELAY;
758 } else
759 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
760
761 /*
762 * If a reload is pending, poll for received request(s),
763 * otherwise set a RELOADDELAY timeout, since a SIGHUP
764 * could be processed between the got_sighup test and
765 * the select() system call.
766 */
767 tv.tv_sec = 0;
768 if (got_sighup)
769 tv.tv_usec = 0;
770 else
771 tv.tv_usec = RELOADDELAY;
772 if (enable_rpcbind != 0) {
773 readfds = svc_fdset;
774 switch (select(svc_maxfd + 1, &readfds, NULL, NULL,
775 &tv)) {
776 case -1:
777 if (errno == EINTR) {
778 /* Allow a reload now. */
779 nexttime = 0;
780 continue;
781 }
782 syslog(LOG_ERR, "mountd died: select: %m");
783 exit(1);
784 case 0:
785 /* Allow a reload now. */
786 nexttime = 0;
787 continue;
788 default:
789 svc_getreqset(&readfds);
790 }
791 } else {
792 /* Simply wait for a signal. */
793 sigsuspend(&sig_mask);
794 }
795 }
796 }
797
798 /*
799 * This routine creates and binds sockets on the appropriate
800 * addresses. It gets called one time for each transport.
801 * It returns 0 upon success, 1 for ignore the call and -1 to indicate
802 * bind failed with EADDRINUSE.
803 * Any file descriptors that have been created are stored in sock_fd and
804 * the total count of them is maintained in sock_fdcnt.
805 */
806 static int
create_service(struct netconfig * nconf)807 create_service(struct netconfig *nconf)
808 {
809 struct addrinfo hints, *res = NULL;
810 struct sockaddr_in *sin;
811 struct sockaddr_in6 *sin6;
812 struct __rpc_sockinfo si;
813 int aicode;
814 int fd;
815 int nhostsbak;
816 int one = 1;
817 int r;
818 u_int32_t host_addr[4]; /* IPv4 or IPv6 */
819 int mallocd_res;
820
821 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
822 (nconf->nc_semantics != NC_TPI_COTS) &&
823 (nconf->nc_semantics != NC_TPI_COTS_ORD))
824 return (1); /* not my type */
825
826 /*
827 * XXX - using RPC library internal functions.
828 */
829 if (!__rpc_nconf2sockinfo(nconf, &si)) {
830 syslog(LOG_ERR, "cannot get information for %s",
831 nconf->nc_netid);
832 return (1);
833 }
834
835 /* Get mountd's address on this transport */
836 memset(&hints, 0, sizeof hints);
837 hints.ai_family = si.si_af;
838 hints.ai_socktype = si.si_socktype;
839 hints.ai_protocol = si.si_proto;
840
841 /*
842 * Bind to specific IPs if asked to
843 */
844 nhostsbak = nhosts;
845 while (nhostsbak > 0) {
846 --nhostsbak;
847 sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int));
848 if (sock_fd == NULL)
849 out_of_mem();
850 sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */
851 mallocd_res = 0;
852
853 hints.ai_flags = AI_PASSIVE;
854
855 /*
856 * XXX - using RPC library internal functions.
857 */
858 if ((fd = __rpc_nconf2fd(nconf)) < 0) {
859 int non_fatal = 0;
860 if (errno == EAFNOSUPPORT &&
861 nconf->nc_semantics != NC_TPI_CLTS)
862 non_fatal = 1;
863
864 syslog(non_fatal ? LOG_DEBUG : LOG_ERR,
865 "cannot create socket for %s", nconf->nc_netid);
866 if (non_fatal != 0)
867 continue;
868 exit(1);
869 }
870
871 switch (hints.ai_family) {
872 case AF_INET:
873 if (inet_pton(AF_INET, hosts[nhostsbak],
874 host_addr) == 1) {
875 hints.ai_flags |= AI_NUMERICHOST;
876 } else {
877 /*
878 * Skip if we have an AF_INET6 address.
879 */
880 if (inet_pton(AF_INET6, hosts[nhostsbak],
881 host_addr) == 1) {
882 close(fd);
883 continue;
884 }
885 }
886 break;
887 case AF_INET6:
888 if (inet_pton(AF_INET6, hosts[nhostsbak],
889 host_addr) == 1) {
890 hints.ai_flags |= AI_NUMERICHOST;
891 } else {
892 /*
893 * Skip if we have an AF_INET address.
894 */
895 if (inet_pton(AF_INET, hosts[nhostsbak],
896 host_addr) == 1) {
897 close(fd);
898 continue;
899 }
900 }
901
902 /*
903 * We're doing host-based access checks here, so don't
904 * allow v4-in-v6 to confuse things. The kernel will
905 * disable it by default on NFS sockets too.
906 */
907 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one,
908 sizeof one) < 0) {
909 syslog(LOG_ERR,
910 "can't disable v4-in-v6 on IPv6 socket");
911 exit(1);
912 }
913 break;
914 default:
915 break;
916 }
917
918 /*
919 * If no hosts were specified, just bind to INADDR_ANY
920 */
921 if (strcmp("*", hosts[nhostsbak]) == 0) {
922 if (svcport_str == NULL) {
923 res = malloc(sizeof(struct addrinfo));
924 if (res == NULL)
925 out_of_mem();
926 mallocd_res = 1;
927 res->ai_flags = hints.ai_flags;
928 res->ai_family = hints.ai_family;
929 res->ai_protocol = hints.ai_protocol;
930 switch (res->ai_family) {
931 case AF_INET:
932 sin = malloc(sizeof(struct sockaddr_in));
933 if (sin == NULL)
934 out_of_mem();
935 sin->sin_family = AF_INET;
936 sin->sin_port = htons(0);
937 sin->sin_addr.s_addr = htonl(INADDR_ANY);
938 res->ai_addr = (struct sockaddr*) sin;
939 res->ai_addrlen = (socklen_t)
940 sizeof(struct sockaddr_in);
941 break;
942 case AF_INET6:
943 sin6 = malloc(sizeof(struct sockaddr_in6));
944 if (sin6 == NULL)
945 out_of_mem();
946 sin6->sin6_family = AF_INET6;
947 sin6->sin6_port = htons(0);
948 sin6->sin6_addr = in6addr_any;
949 res->ai_addr = (struct sockaddr*) sin6;
950 res->ai_addrlen = (socklen_t)
951 sizeof(struct sockaddr_in6);
952 break;
953 default:
954 syslog(LOG_ERR, "bad addr fam %d",
955 res->ai_family);
956 exit(1);
957 }
958 } else {
959 if ((aicode = getaddrinfo(NULL, svcport_str,
960 &hints, &res)) != 0) {
961 syslog(LOG_ERR,
962 "cannot get local address for %s: %s",
963 nconf->nc_netid,
964 gai_strerror(aicode));
965 close(fd);
966 continue;
967 }
968 }
969 } else {
970 if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str,
971 &hints, &res)) != 0) {
972 syslog(LOG_ERR,
973 "cannot get local address for %s: %s",
974 nconf->nc_netid, gai_strerror(aicode));
975 close(fd);
976 continue;
977 }
978 }
979
980 /* Store the fd. */
981 sock_fd[sock_fdcnt - 1] = fd;
982
983 /* Now, attempt the bind. */
984 r = bindresvport_sa(fd, res->ai_addr);
985 if (r != 0) {
986 if (errno == EADDRINUSE && mallocd_svcport != 0) {
987 if (mallocd_res != 0) {
988 free(res->ai_addr);
989 free(res);
990 } else
991 freeaddrinfo(res);
992 return (-1);
993 }
994 syslog(LOG_ERR, "bindresvport_sa: %m");
995 exit(1);
996 }
997
998 if (svcport_str == NULL) {
999 svcport_str = malloc(NI_MAXSERV * sizeof(char));
1000 if (svcport_str == NULL)
1001 out_of_mem();
1002 mallocd_svcport = 1;
1003
1004 if (getnameinfo(res->ai_addr,
1005 res->ai_addr->sa_len, NULL, NI_MAXHOST,
1006 svcport_str, NI_MAXSERV * sizeof(char),
1007 NI_NUMERICHOST | NI_NUMERICSERV))
1008 errx(1, "Cannot get port number");
1009 }
1010 if (mallocd_res != 0) {
1011 free(res->ai_addr);
1012 free(res);
1013 } else
1014 freeaddrinfo(res);
1015 res = NULL;
1016 }
1017 return (0);
1018 }
1019
1020 /*
1021 * Called after all the create_service() calls have succeeded, to complete
1022 * the setup and registration.
1023 */
1024 static void
complete_service(struct netconfig * nconf,char * port_str)1025 complete_service(struct netconfig *nconf, char *port_str)
1026 {
1027 struct addrinfo hints, *res = NULL;
1028 struct __rpc_sockinfo si;
1029 struct netbuf servaddr;
1030 SVCXPRT *transp = NULL;
1031 int aicode, fd, nhostsbak;
1032 int registered = 0;
1033
1034 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
1035 (nconf->nc_semantics != NC_TPI_COTS) &&
1036 (nconf->nc_semantics != NC_TPI_COTS_ORD))
1037 return; /* not my type */
1038
1039 /*
1040 * XXX - using RPC library internal functions.
1041 */
1042 if (!__rpc_nconf2sockinfo(nconf, &si)) {
1043 syslog(LOG_ERR, "cannot get information for %s",
1044 nconf->nc_netid);
1045 return;
1046 }
1047
1048 nhostsbak = nhosts;
1049 while (nhostsbak > 0) {
1050 --nhostsbak;
1051 if (sock_fdpos >= sock_fdcnt) {
1052 /* Should never happen. */
1053 syslog(LOG_ERR, "Ran out of socket fd's");
1054 return;
1055 }
1056 fd = sock_fd[sock_fdpos++];
1057 if (fd < 0)
1058 continue;
1059
1060 /*
1061 * Using -1 tells listen(2) to use
1062 * kern.ipc.soacceptqueue for the backlog.
1063 */
1064 if (nconf->nc_semantics != NC_TPI_CLTS)
1065 listen(fd, -1);
1066
1067 if (nconf->nc_semantics == NC_TPI_CLTS )
1068 transp = svc_dg_create(fd, 0, 0);
1069 else
1070 transp = svc_vc_create(fd, RPC_MAXDATASIZE,
1071 RPC_MAXDATASIZE);
1072
1073 if (transp != (SVCXPRT *) NULL) {
1074 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv,
1075 NULL))
1076 syslog(LOG_ERR,
1077 "can't register %s MOUNTVERS service",
1078 nconf->nc_netid);
1079 if (!force_v2) {
1080 if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3,
1081 mntsrv, NULL))
1082 syslog(LOG_ERR,
1083 "can't register %s MOUNTVERS3 service",
1084 nconf->nc_netid);
1085 }
1086 } else
1087 syslog(LOG_WARNING, "can't create %s services",
1088 nconf->nc_netid);
1089
1090 if (registered == 0) {
1091 registered = 1;
1092 memset(&hints, 0, sizeof hints);
1093 hints.ai_flags = AI_PASSIVE;
1094 hints.ai_family = si.si_af;
1095 hints.ai_socktype = si.si_socktype;
1096 hints.ai_protocol = si.si_proto;
1097
1098 if ((aicode = getaddrinfo(NULL, port_str, &hints,
1099 &res)) != 0) {
1100 syslog(LOG_ERR, "cannot get local address: %s",
1101 gai_strerror(aicode));
1102 exit(1);
1103 }
1104
1105 servaddr.buf = malloc(res->ai_addrlen);
1106 memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen);
1107 servaddr.len = res->ai_addrlen;
1108
1109 rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr);
1110 rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr);
1111
1112 xcreated++;
1113 freeaddrinfo(res);
1114 }
1115 } /* end while */
1116 }
1117
1118 /*
1119 * Clear out sockets after a failure to bind one of them, so that the
1120 * cycle of socket creation/binding can start anew.
1121 */
1122 static void
clearout_service(void)1123 clearout_service(void)
1124 {
1125 int i;
1126
1127 for (i = 0; i < sock_fdcnt; i++) {
1128 if (sock_fd[i] >= 0) {
1129 shutdown(sock_fd[i], SHUT_RDWR);
1130 close(sock_fd[i]);
1131 }
1132 }
1133 }
1134
1135 static void
usage(void)1136 usage(void)
1137 {
1138 fprintf(stderr,
1139 "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p <port>] [-r] "
1140 "[-S] [-h <bindip>] [export_file ...]\n");
1141 exit(1);
1142 }
1143
1144 /*
1145 * The mount rpc service
1146 */
1147 void
mntsrv(struct svc_req * rqstp,SVCXPRT * transp)1148 mntsrv(struct svc_req *rqstp, SVCXPRT *transp)
1149 {
1150 struct exportlist *ep;
1151 struct dirlist *dp;
1152 struct fhreturn fhr;
1153 struct stat stb;
1154 struct statfs fsb;
1155 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
1156 int lookup_failed = 1;
1157 struct sockaddr *saddr;
1158 u_short sport;
1159 char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN];
1160 int defset, hostset;
1161 long bad = 0;
1162 sigset_t sighup_mask;
1163 int numsecflavors, *secflavorsp;
1164
1165 sigemptyset(&sighup_mask);
1166 sigaddset(&sighup_mask, SIGHUP);
1167 saddr = svc_getrpccaller(transp)->buf;
1168 switch (saddr->sa_family) {
1169 case AF_INET6:
1170 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
1171 break;
1172 case AF_INET:
1173 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
1174 break;
1175 default:
1176 syslog(LOG_ERR, "request from unknown address family");
1177 return;
1178 }
1179 switch (rqstp->rq_proc) {
1180 case MOUNTPROC_MNT:
1181 case MOUNTPROC_UMNT:
1182 case MOUNTPROC_UMNTALL:
1183 lookup_failed = getnameinfo(saddr, saddr->sa_len, host,
1184 sizeof host, NULL, 0, 0);
1185 }
1186 getnameinfo(saddr, saddr->sa_len, numerichost,
1187 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
1188 switch (rqstp->rq_proc) {
1189 case NULLPROC:
1190 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL))
1191 syslog(LOG_ERR, "can't send reply");
1192 return;
1193 case MOUNTPROC_MNT:
1194 if (sport >= IPPORT_RESERVED && resvport_only) {
1195 syslog(LOG_NOTICE,
1196 "mount request from %s from unprivileged port",
1197 numerichost);
1198 svcerr_weakauth(transp);
1199 return;
1200 }
1201 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1202 syslog(LOG_NOTICE, "undecodable mount request from %s",
1203 numerichost);
1204 svcerr_decode(transp);
1205 return;
1206 }
1207
1208 /*
1209 * Get the real pathname and make sure it is a directory
1210 * or a regular file if the -r option was specified
1211 * and it exists.
1212 */
1213 if (realpath(rpcpath, dirpath) == NULL ||
1214 stat(dirpath, &stb) < 0 ||
1215 statfs(dirpath, &fsb) < 0) {
1216 chdir("/"); /* Just in case realpath doesn't */
1217 syslog(LOG_NOTICE,
1218 "mount request from %s for non existent path %s",
1219 numerichost, dirpath);
1220 if (debug)
1221 warnx("stat failed on %s", dirpath);
1222 bad = ENOENT; /* We will send error reply later */
1223 }
1224 if (!bad &&
1225 !S_ISDIR(stb.st_mode) &&
1226 (dir_only || !S_ISREG(stb.st_mode))) {
1227 syslog(LOG_NOTICE,
1228 "mount request from %s for non-directory path %s",
1229 numerichost, dirpath);
1230 if (debug)
1231 warnx("mounting non-directory %s", dirpath);
1232 bad = ENOTDIR; /* We will send error reply later */
1233 }
1234
1235 /* Check in the exports list */
1236 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1237 if (bad)
1238 ep = NULL;
1239 else
1240 ep = ex_search(&fsb.f_fsid, exphead);
1241 hostset = defset = 0;
1242 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset,
1243 &numsecflavors, &secflavorsp) ||
1244 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
1245 chk_host(dp, saddr, &defset, &hostset, &numsecflavors,
1246 &secflavorsp)) ||
1247 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
1248 scan_tree(ep->ex_dirl, saddr) == 0))) {
1249 if (bad) {
1250 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1251 (caddr_t)&bad))
1252 syslog(LOG_ERR, "can't send reply");
1253 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1254 return;
1255 }
1256 if (hostset & DP_HOSTSET) {
1257 fhr.fhr_flag = hostset;
1258 fhr.fhr_numsecflavors = numsecflavors;
1259 fhr.fhr_secflavors = secflavorsp;
1260 } else {
1261 fhr.fhr_flag = defset;
1262 fhr.fhr_numsecflavors = ep->ex_defnumsecflavors;
1263 fhr.fhr_secflavors = ep->ex_defsecflavors;
1264 }
1265 fhr.fhr_vers = rqstp->rq_vers;
1266 /* Get the file handle */
1267 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
1268 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
1269 bad = errno;
1270 syslog(LOG_ERR, "can't get fh for %s", dirpath);
1271 if (!svc_sendreply(transp, (xdrproc_t)xdr_long,
1272 (caddr_t)&bad))
1273 syslog(LOG_ERR, "can't send reply");
1274 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1275 return;
1276 }
1277 if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs,
1278 (caddr_t)&fhr))
1279 syslog(LOG_ERR, "can't send reply");
1280 if (!lookup_failed)
1281 add_mlist(host, dirpath);
1282 else
1283 add_mlist(numerichost, dirpath);
1284 if (debug)
1285 warnx("mount successful");
1286 if (dolog)
1287 syslog(LOG_NOTICE,
1288 "mount request succeeded from %s for %s",
1289 numerichost, dirpath);
1290 } else {
1291 if (!bad)
1292 bad = EACCES;
1293 syslog(LOG_NOTICE,
1294 "mount request denied from %s for %s",
1295 numerichost, dirpath);
1296 }
1297
1298 if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long,
1299 (caddr_t)&bad))
1300 syslog(LOG_ERR, "can't send reply");
1301 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1302 return;
1303 case MOUNTPROC_DUMP:
1304 if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL))
1305 syslog(LOG_ERR, "can't send reply");
1306 else if (dolog)
1307 syslog(LOG_NOTICE,
1308 "dump request succeeded from %s",
1309 numerichost);
1310 return;
1311 case MOUNTPROC_UMNT:
1312 if (sport >= IPPORT_RESERVED && resvport_only) {
1313 syslog(LOG_NOTICE,
1314 "umount request from %s from unprivileged port",
1315 numerichost);
1316 svcerr_weakauth(transp);
1317 return;
1318 }
1319 if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) {
1320 syslog(LOG_NOTICE, "undecodable umount request from %s",
1321 numerichost);
1322 svcerr_decode(transp);
1323 return;
1324 }
1325 if (realpath(rpcpath, dirpath) == NULL) {
1326 syslog(LOG_NOTICE, "umount request from %s "
1327 "for non existent path %s",
1328 numerichost, dirpath);
1329 }
1330 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1331 syslog(LOG_ERR, "can't send reply");
1332 if (!lookup_failed)
1333 del_mlist(host, dirpath);
1334 del_mlist(numerichost, dirpath);
1335 if (dolog)
1336 syslog(LOG_NOTICE,
1337 "umount request succeeded from %s for %s",
1338 numerichost, dirpath);
1339 return;
1340 case MOUNTPROC_UMNTALL:
1341 if (sport >= IPPORT_RESERVED && resvport_only) {
1342 syslog(LOG_NOTICE,
1343 "umountall request from %s from unprivileged port",
1344 numerichost);
1345 svcerr_weakauth(transp);
1346 return;
1347 }
1348 if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL))
1349 syslog(LOG_ERR, "can't send reply");
1350 if (!lookup_failed)
1351 del_mlist(host, NULL);
1352 del_mlist(numerichost, NULL);
1353 if (dolog)
1354 syslog(LOG_NOTICE,
1355 "umountall request succeeded from %s",
1356 numerichost);
1357 return;
1358 case MOUNTPROC_EXPORT:
1359 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL))
1360 if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief,
1361 (caddr_t)NULL))
1362 syslog(LOG_ERR, "can't send reply");
1363 if (dolog)
1364 syslog(LOG_NOTICE,
1365 "export request succeeded from %s",
1366 numerichost);
1367 return;
1368 default:
1369 svcerr_noproc(transp);
1370 return;
1371 }
1372 }
1373
1374 /*
1375 * Xdr conversion for a dirpath string
1376 */
1377 static int
xdr_dir(XDR * xdrsp,char * dirp)1378 xdr_dir(XDR *xdrsp, char *dirp)
1379 {
1380 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1381 }
1382
1383 /*
1384 * Xdr routine to generate file handle reply
1385 */
1386 static int
xdr_fhs(XDR * xdrsp,caddr_t cp)1387 xdr_fhs(XDR *xdrsp, caddr_t cp)
1388 {
1389 struct fhreturn *fhrp = (struct fhreturn *)cp;
1390 u_long ok = 0, len, auth;
1391 int i;
1392
1393 if (!xdr_long(xdrsp, &ok))
1394 return (0);
1395 switch (fhrp->fhr_vers) {
1396 case 1:
1397 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
1398 case 3:
1399 len = NFSX_V3FH;
1400 if (!xdr_long(xdrsp, &len))
1401 return (0);
1402 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
1403 return (0);
1404 if (fhrp->fhr_numsecflavors) {
1405 if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors))
1406 return (0);
1407 for (i = 0; i < fhrp->fhr_numsecflavors; i++)
1408 if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i]))
1409 return (0);
1410 return (1);
1411 } else {
1412 auth = AUTH_SYS;
1413 len = 1;
1414 if (!xdr_long(xdrsp, &len))
1415 return (0);
1416 return (xdr_long(xdrsp, &auth));
1417 }
1418 }
1419 return (0);
1420 }
1421
1422 static int
xdr_mlist(XDR * xdrsp,caddr_t cp __unused)1423 xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
1424 {
1425 struct mountlist *mlp;
1426 int true = 1;
1427 int false = 0;
1428 char *strp;
1429
1430 SLIST_FOREACH(mlp, &mlhead, next) {
1431 if (!xdr_bool(xdrsp, &true))
1432 return (0);
1433 strp = &mlp->ml_host[0];
1434 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
1435 return (0);
1436 strp = &mlp->ml_dirp[0];
1437 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1438 return (0);
1439 }
1440 if (!xdr_bool(xdrsp, &false))
1441 return (0);
1442 return (1);
1443 }
1444
1445 /*
1446 * Xdr conversion for export list
1447 */
1448 static int
xdr_explist_common(XDR * xdrsp,caddr_t cp __unused,int brief)1449 xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief)
1450 {
1451 struct exportlist *ep;
1452 int false = 0;
1453 int putdef;
1454 sigset_t sighup_mask;
1455 int i;
1456
1457 sigemptyset(&sighup_mask);
1458 sigaddset(&sighup_mask, SIGHUP);
1459 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
1460
1461 for (i = 0; i < exphashsize; i++)
1462 SLIST_FOREACH(ep, &exphead[i], entries) {
1463 putdef = 0;
1464 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir,
1465 &putdef, brief))
1466 goto errout;
1467 if (ep->ex_defdir && putdef == 0 &&
1468 put_exlist(ep->ex_defdir, xdrsp, NULL,
1469 &putdef, brief))
1470 goto errout;
1471 }
1472 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1473 if (!xdr_bool(xdrsp, &false))
1474 return (0);
1475 return (1);
1476 errout:
1477 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
1478 return (0);
1479 }
1480
1481 /*
1482 * Called from xdr_explist() to traverse the tree and export the
1483 * directory paths.
1484 */
1485 static int
put_exlist(struct dirlist * dp,XDR * xdrsp,struct dirlist * adp,int * putdefp,int brief)1486 put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp,
1487 int brief)
1488 {
1489 struct grouplist *grp;
1490 struct hostlist *hp;
1491 int true = 1;
1492 int false = 0;
1493 int gotalldir = 0;
1494 char *strp;
1495
1496 if (dp) {
1497 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief))
1498 return (1);
1499 if (!xdr_bool(xdrsp, &true))
1500 return (1);
1501 strp = dp->dp_dirp;
1502 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1503 return (1);
1504 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
1505 gotalldir = 1;
1506 *putdefp = 1;
1507 }
1508 if (brief) {
1509 if (!xdr_bool(xdrsp, &true))
1510 return (1);
1511 strp = "(...)";
1512 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
1513 return (1);
1514 } else if ((dp->dp_flag & DP_DEFSET) == 0 &&
1515 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
1516 hp = dp->dp_hosts;
1517 while (hp) {
1518 grp = hp->ht_grp;
1519 if (grp->gr_type == GT_HOST) {
1520 if (!xdr_bool(xdrsp, &true))
1521 return (1);
1522 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
1523 if (!xdr_string(xdrsp, &strp,
1524 MNTNAMLEN))
1525 return (1);
1526 } else if (grp->gr_type == GT_NET) {
1527 if (!xdr_bool(xdrsp, &true))
1528 return (1);
1529 strp = grp->gr_ptr.gt_net.nt_name;
1530 if (!xdr_string(xdrsp, &strp,
1531 MNTNAMLEN))
1532 return (1);
1533 }
1534 hp = hp->ht_next;
1535 if (gotalldir && hp == (struct hostlist *)NULL) {
1536 hp = adp->dp_hosts;
1537 gotalldir = 0;
1538 }
1539 }
1540 }
1541 if (!xdr_bool(xdrsp, &false))
1542 return (1);
1543 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief))
1544 return (1);
1545 }
1546 return (0);
1547 }
1548
1549 static int
xdr_explist(XDR * xdrsp,caddr_t cp)1550 xdr_explist(XDR *xdrsp, caddr_t cp)
1551 {
1552
1553 return xdr_explist_common(xdrsp, cp, 0);
1554 }
1555
1556 static int
xdr_explist_brief(XDR * xdrsp,caddr_t cp)1557 xdr_explist_brief(XDR *xdrsp, caddr_t cp)
1558 {
1559
1560 return xdr_explist_common(xdrsp, cp, 1);
1561 }
1562
1563 static char *line;
1564 static size_t linesize;
1565 static FILE *exp_file;
1566
1567 /*
1568 * Get the export list from one, currently open file
1569 */
1570 static void
get_exportlist_one(int passno)1571 get_exportlist_one(int passno)
1572 {
1573 struct exportlist *ep;
1574 struct grouplist *grp, *tgrp, *savgrp;
1575 struct dirlist *dirhead;
1576 struct statfs fsb;
1577 struct expcred anon;
1578 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
1579 int len, has_host, got_nondir, dirplen, netgrp;
1580 uint64_t exflags;
1581
1582 v4root_phase = 0;
1583 dirhead = (struct dirlist *)NULL;
1584 while (get_line()) {
1585 if (debug)
1586 warnx("got line %s", line);
1587 cp = line;
1588 nextfield(&cp, &endcp);
1589 if (*cp == '#')
1590 goto nextline;
1591
1592 /*
1593 * Set defaults.
1594 */
1595 has_host = FALSE;
1596 anon.cr_uid = UID_NOBODY;
1597 anon.cr_ngroups = 1;
1598 anon.cr_groups = tmp_groups;
1599 anon.cr_groups[0] = nogroup();
1600 exflags = MNT_EXPORTED;
1601 got_nondir = 0;
1602 opt_flags = 0;
1603 ep = (struct exportlist *)NULL;
1604 dirp = NULL;
1605
1606 /*
1607 * Handle the V4 root dir.
1608 */
1609 if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') {
1610 /*
1611 * V4: just indicates that it is the v4 root point,
1612 * so skip over that and set v4root_phase.
1613 */
1614 if (v4root_phase > 0) {
1615 syslog(LOG_ERR, "V4:duplicate line, ignored");
1616 goto nextline;
1617 }
1618 v4root_phase = 1;
1619 cp += 3;
1620 nextfield(&cp, &endcp);
1621 }
1622
1623 /*
1624 * Create new exports list entry
1625 */
1626 len = endcp-cp;
1627 tgrp = grp = get_grp();
1628 while (len > 0) {
1629 if (len > MNTNAMLEN) {
1630 getexp_err(ep, tgrp, "mountpoint too long");
1631 goto nextline;
1632 }
1633 if (*cp == '-') {
1634 if (ep == (struct exportlist *)NULL) {
1635 getexp_err(ep, tgrp,
1636 "flag before export path definition");
1637 goto nextline;
1638 }
1639 if (debug)
1640 warnx("doing opt %s", cp);
1641 got_nondir = 1;
1642 if (do_opt(&cp, &endcp, ep, grp, &has_host,
1643 &exflags, &anon)) {
1644 getexp_err(ep, tgrp, NULL);
1645 goto nextline;
1646 }
1647 } else if (*cp == '/') {
1648 savedc = *endcp;
1649 *endcp = '\0';
1650 if (v4root_phase > 1) {
1651 if (dirp != NULL) {
1652 getexp_err(ep, tgrp, "Multiple V4 dirs");
1653 goto nextline;
1654 }
1655 }
1656 if (check_dirpath(cp) &&
1657 statfs(cp, &fsb) >= 0) {
1658 if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0)
1659 syslog(LOG_ERR, "Warning: exporting of "
1660 "automounted fs %s not supported", cp);
1661 if (got_nondir) {
1662 getexp_err(ep, tgrp, "dirs must be first");
1663 goto nextline;
1664 }
1665 if (v4root_phase == 1) {
1666 if (dirp != NULL) {
1667 getexp_err(ep, tgrp, "Multiple V4 dirs");
1668 goto nextline;
1669 }
1670 if (strlen(v4root_dirpath) == 0) {
1671 strlcpy(v4root_dirpath, cp,
1672 sizeof (v4root_dirpath));
1673 } else if (strcmp(v4root_dirpath, cp)
1674 != 0) {
1675 syslog(LOG_ERR,
1676 "different V4 dirpath %s", cp);
1677 getexp_err(ep, tgrp, NULL);
1678 goto nextline;
1679 }
1680 dirp = cp;
1681 v4root_phase = 2;
1682 got_nondir = 1;
1683 ep = get_exp();
1684 } else {
1685 if (ep) {
1686 if (fsidcmp(&ep->ex_fs, &fsb.f_fsid)
1687 != 0) {
1688 getexp_err(ep, tgrp,
1689 "fsid mismatch");
1690 goto nextline;
1691 }
1692 } else {
1693 /*
1694 * See if this directory is already
1695 * in the list.
1696 */
1697 ep = ex_search(&fsb.f_fsid, exphead);
1698 if (ep == (struct exportlist *)NULL) {
1699 ep = get_exp();
1700 ep->ex_fs = fsb.f_fsid;
1701 ep->ex_fsdir = strdup(fsb.f_mntonname);
1702 if (ep->ex_fsdir == NULL)
1703 out_of_mem();
1704 if (debug)
1705 warnx(
1706 "making new ep fs=0x%x,0x%x",
1707 fsb.f_fsid.val[0],
1708 fsb.f_fsid.val[1]);
1709 } else if (debug)
1710 warnx("found ep fs=0x%x,0x%x",
1711 fsb.f_fsid.val[0],
1712 fsb.f_fsid.val[1]);
1713 }
1714
1715 /*
1716 * Add dirpath to export mount point.
1717 */
1718 dirp = add_expdir(&dirhead, cp, len);
1719 dirplen = len;
1720 }
1721 } else {
1722 getexp_err(ep, tgrp,
1723 "symbolic link in export path or statfs failed");
1724 goto nextline;
1725 }
1726 *endcp = savedc;
1727 } else {
1728 savedc = *endcp;
1729 *endcp = '\0';
1730 got_nondir = 1;
1731 if (ep == (struct exportlist *)NULL) {
1732 getexp_err(ep, tgrp,
1733 "host(s) before export path definition");
1734 goto nextline;
1735 }
1736
1737 /*
1738 * Get the host or netgroup.
1739 */
1740 setnetgrent(cp);
1741 netgrp = getnetgrent(&hst, &usr, &dom);
1742 do {
1743 if (has_host) {
1744 grp->gr_next = get_grp();
1745 grp = grp->gr_next;
1746 }
1747 if (netgrp) {
1748 if (hst == 0) {
1749 syslog(LOG_ERR,
1750 "null hostname in netgroup %s, skipping", cp);
1751 grp->gr_type = GT_IGNORE;
1752 } else if (get_host(hst, grp, tgrp)) {
1753 syslog(LOG_ERR,
1754 "bad host %s in netgroup %s, skipping", hst, cp);
1755 grp->gr_type = GT_IGNORE;
1756 }
1757 } else if (get_host(cp, grp, tgrp)) {
1758 syslog(LOG_ERR, "bad host %s, skipping", cp);
1759 grp->gr_type = GT_IGNORE;
1760 }
1761 has_host = TRUE;
1762 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1763 endnetgrent();
1764 *endcp = savedc;
1765 }
1766 cp = endcp;
1767 nextfield(&cp, &endcp);
1768 len = endcp - cp;
1769 }
1770 if (opt_flags & OP_CLASSMASK)
1771 syslog(LOG_WARNING,
1772 "WARNING: No mask specified for %s, "
1773 "using out-of-date default",
1774 (&grp->gr_ptr.gt_net)->nt_name);
1775 if (check_options(dirhead)) {
1776 getexp_err(ep, tgrp, NULL);
1777 goto nextline;
1778 }
1779 if (!has_host) {
1780 grp->gr_type = GT_DEFAULT;
1781 if (debug)
1782 warnx("adding a default entry");
1783
1784 /*
1785 * Don't allow a network export coincide with a list of
1786 * host(s) on the same line.
1787 */
1788 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1789 getexp_err(ep, tgrp, "network/host conflict");
1790 goto nextline;
1791
1792 /*
1793 * If an export list was specified on this line, make sure
1794 * that we have at least one valid entry, otherwise skip it.
1795 */
1796 } else {
1797 grp = tgrp;
1798 while (grp && grp->gr_type == GT_IGNORE)
1799 grp = grp->gr_next;
1800 if (! grp) {
1801 getexp_err(ep, tgrp, "no valid entries");
1802 goto nextline;
1803 }
1804 }
1805
1806 if (v4root_phase == 1) {
1807 getexp_err(ep, tgrp, "V4:root, no dirp, ignored");
1808 goto nextline;
1809 }
1810
1811 /*
1812 * Loop through hosts, pushing the exports into the kernel.
1813 * After loop, tgrp points to the start of the list and
1814 * grp points to the last entry in the list.
1815 * Do not do the do_mount() for passno == 1, since the
1816 * second pass will do it, as required.
1817 */
1818 grp = tgrp;
1819 do {
1820 grp->gr_exflags = exflags;
1821 cp_cred(&grp->gr_anon, &anon);
1822 if (v4root_phase == 2 && passno == 0)
1823 LOGDEBUG("do_mount v4root");
1824 if (passno == 0 && do_mount(ep, grp, exflags, &anon,
1825 dirp, dirplen, &fsb, ep->ex_numsecflavors,
1826 ep->ex_secflavors)) {
1827 getexp_err(ep, tgrp, NULL);
1828 goto nextline;
1829 }
1830 } while (grp->gr_next && (grp = grp->gr_next));
1831
1832 /*
1833 * For V4: don't enter in mount lists.
1834 */
1835 if (v4root_phase > 0 && v4root_phase <= 2) {
1836 /*
1837 * These structures are used for the reload,
1838 * so save them for that case. Otherwise, just
1839 * free them up now.
1840 */
1841 if (passno == 1 && ep != NULL) {
1842 savgrp = tgrp;
1843 while (tgrp != NULL) {
1844 /*
1845 * Save the security flavors and exflags
1846 * for this host set in the groups.
1847 */
1848 tgrp->gr_numsecflavors =
1849 ep->ex_numsecflavors;
1850 if (ep->ex_numsecflavors > 0)
1851 memcpy(tgrp->gr_secflavors,
1852 ep->ex_secflavors,
1853 sizeof(ep->ex_secflavors));
1854 tgrp = tgrp->gr_next;
1855 }
1856 if (v4root_ep == NULL) {
1857 v4root_ep = ep;
1858 ep = NULL; /* Don't free below. */
1859 }
1860 grp->gr_next = v4root_ep->ex_grphead;
1861 v4root_ep->ex_grphead = savgrp;
1862 }
1863 if (ep != NULL)
1864 free_exp(ep);
1865 while (tgrp != NULL) {
1866 grp = tgrp;
1867 tgrp = tgrp->gr_next;
1868 free_grp(grp);
1869 }
1870 goto nextline;
1871 }
1872
1873 /*
1874 * Success. Update the data structures.
1875 */
1876 if (has_host) {
1877 hang_dirp(dirhead, tgrp, ep, opt_flags, &anon, exflags);
1878 grp->gr_next = ep->ex_grphead;
1879 ep->ex_grphead = tgrp;
1880 } else {
1881 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1882 opt_flags, &anon, exflags);
1883 free_grp(grp);
1884 }
1885 dirhead = (struct dirlist *)NULL;
1886 if ((ep->ex_flag & EX_LINKED) == 0) {
1887 insert_exports(ep, exphead);
1888
1889 ep->ex_flag |= EX_LINKED;
1890 }
1891 nextline:
1892 v4root_phase = 0;
1893 if (dirhead) {
1894 free_dir(dirhead);
1895 dirhead = (struct dirlist *)NULL;
1896 }
1897 }
1898 }
1899
1900 /*
1901 * Get the export list from all specified files
1902 */
1903 static void
get_exportlist(int passno)1904 get_exportlist(int passno)
1905 {
1906 struct export_args export;
1907 struct iovec *iov;
1908 struct statfs *mntbufp;
1909 char errmsg[255];
1910 int error, i, nfs_maxvers, num;
1911 int iovlen;
1912 struct nfsex_args eargs;
1913 FILE *debug_file;
1914 size_t nfs_maxvers_size;
1915
1916 if ((debug_file = fopen(_PATH_MOUNTDDEBUG, "r")) != NULL) {
1917 fclose(debug_file);
1918 logdebug = 1;
1919 } else
1920 logdebug = 0;
1921 LOGDEBUG("passno=%d", passno);
1922 v4root_dirpath[0] = '\0';
1923 free_v4rootexp();
1924 if (passno == 1) {
1925 /*
1926 * Save the current lists as old ones, so that the new lists
1927 * can be compared with the old ones in the 2nd pass.
1928 */
1929 for (i = 0; i < exphashsize; i++) {
1930 SLIST_FIRST(&oldexphead[i]) = SLIST_FIRST(&exphead[i]);
1931 SLIST_INIT(&exphead[i]);
1932 }
1933
1934 /* Note that the public fh has not yet been set. */
1935 has_set_publicfh = 0;
1936
1937 /* Read the export file(s) and process them */
1938 read_exportfile(passno);
1939 } else {
1940 /*
1941 * Just make the old lists empty.
1942 * exphashsize == 0 for the first call, before oldexphead
1943 * has been initialized-->loop won't be executed.
1944 */
1945 for (i = 0; i < exphashsize; i++)
1946 SLIST_INIT(&oldexphead[i]);
1947 }
1948
1949 bzero(&export, sizeof(export));
1950 export.ex_flags = MNT_DELEXPORT;
1951 iov = NULL;
1952 iovlen = 0;
1953 bzero(errmsg, sizeof(errmsg));
1954
1955 if (suspend_nfsd != 0)
1956 (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL);
1957 /*
1958 * Delete the old V4 root dir.
1959 */
1960 bzero(&eargs, sizeof (eargs));
1961 eargs.export.ex_flags = MNT_DELEXPORT;
1962 if (nfssvc(NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT, (caddr_t)&eargs) < 0 &&
1963 errno != ENOENT)
1964 syslog(LOG_ERR, "Can't delete exports for V4:");
1965
1966 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
1967 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
1968 build_iovec(&iov, &iovlen, "from", NULL, 0);
1969 build_iovec(&iov, &iovlen, "update", NULL, 0);
1970 build_iovec(&iov, &iovlen, "export", &export,
1971 sizeof(export));
1972 build_iovec(&iov, &iovlen, "errmsg", errmsg,
1973 sizeof(errmsg));
1974
1975 /*
1976 * For passno == 1, compare the old and new lists updating the kernel
1977 * exports for any cases that have changed.
1978 * This call is doing the second pass through the lists.
1979 * If it fails, fall back on the bulk reload.
1980 */
1981 if (passno == 1 && compare_nmount_exportlist(iov, iovlen, errmsg) ==
1982 0) {
1983 LOGDEBUG("compareok");
1984 /* Free up the old lists. */
1985 free_exports(oldexphead);
1986 } else {
1987 LOGDEBUG("doing passno=0");
1988 /*
1989 * Clear flag that notes if a public fh has been exported.
1990 * It is set by do_mount() if MNT_EXPUBLIC is set for the entry.
1991 */
1992 has_publicfh = 0;
1993
1994 /* exphead == NULL if not yet allocated (first call). */
1995 if (exphead != NULL) {
1996 /*
1997 * First, get rid of the old lists.
1998 */
1999 free_exports(exphead);
2000 free_exports(oldexphead);
2001 }
2002
2003 /*
2004 * And delete exports that are in the kernel for all local
2005 * filesystems.
2006 * XXX: Should know how to handle all local exportable
2007 * filesystems.
2008 */
2009 num = getmntinfo(&mntbufp, MNT_NOWAIT);
2010
2011 /* Allocate hash tables, for first call. */
2012 if (exphead == NULL) {
2013 /* Target an average linked list length of 10. */
2014 exphashsize = num / 10;
2015 if (exphashsize < 1)
2016 exphashsize = 1;
2017 else if (exphashsize > 100000)
2018 exphashsize = 100000;
2019 exphead = malloc(exphashsize * sizeof(*exphead));
2020 oldexphead = malloc(exphashsize * sizeof(*oldexphead));
2021 if (exphead == NULL || oldexphead == NULL)
2022 errx(1, "Can't malloc hash tables");
2023
2024 for (i = 0; i < exphashsize; i++) {
2025 SLIST_INIT(&exphead[i]);
2026 SLIST_INIT(&oldexphead[i]);
2027 }
2028 }
2029
2030 for (i = 0; i < num; i++)
2031 delete_export(iov, iovlen, &mntbufp[i], errmsg);
2032
2033
2034 /* Read the export file(s) and process them */
2035 read_exportfile(0);
2036 }
2037
2038 if (strlen(v4root_dirpath) == 0) {
2039 /* Check to see if a V4: line is needed. */
2040 nfs_maxvers_size = sizeof(nfs_maxvers);
2041 error = sysctlbyname("vfs.nfsd.server_max_nfsvers",
2042 &nfs_maxvers, &nfs_maxvers_size, NULL, 0);
2043 if (error != 0 || nfs_maxvers < NFS_VER2 || nfs_maxvers >
2044 NFS_VER4) {
2045 syslog(LOG_ERR, "sysctlbyname(vfs.nfsd."
2046 "server_max_nfsvers) failed, defaulting to NFSv3");
2047 nfs_maxvers = NFS_VER3;
2048 }
2049 if (nfs_maxvers == NFS_VER4)
2050 syslog(LOG_ERR, "NFSv4 requires at least one V4: line");
2051 }
2052
2053 if (iov != NULL) {
2054 /* Free strings allocated by strdup() in getmntopts.c */
2055 free(iov[0].iov_base); /* fstype */
2056 free(iov[2].iov_base); /* fspath */
2057 free(iov[4].iov_base); /* from */
2058 free(iov[6].iov_base); /* update */
2059 free(iov[8].iov_base); /* export */
2060 free(iov[10].iov_base); /* errmsg */
2061
2062 /* free iov, allocated by realloc() */
2063 free(iov);
2064 iovlen = 0;
2065 }
2066
2067 /*
2068 * If there was no public fh, clear any previous one set.
2069 */
2070 if (has_publicfh == 0) {
2071 LOGDEBUG("clear public fh");
2072 (void) nfssvc(NFSSVC_NOPUBLICFH, NULL);
2073 }
2074
2075 /* Resume the nfsd. If they weren't suspended, this is harmless. */
2076 (void)nfssvc(NFSSVC_RESUMENFSD, NULL);
2077 LOGDEBUG("eo get_exportlist");
2078 }
2079
2080 /*
2081 * Insert an export entry in the appropriate list.
2082 */
2083 static void
insert_exports(struct exportlist * ep,struct exportlisthead * exhp)2084 insert_exports(struct exportlist *ep, struct exportlisthead *exhp)
2085 {
2086 uint32_t i;
2087
2088 i = EXPHASH(&ep->ex_fs);
2089 LOGDEBUG("fs=%s hash=%i", ep->ex_fsdir, i);
2090 SLIST_INSERT_HEAD(&exhp[i], ep, entries);
2091 }
2092
2093 /*
2094 * Free up the exports lists passed in as arguments.
2095 */
2096 static void
free_exports(struct exportlisthead * exhp)2097 free_exports(struct exportlisthead *exhp)
2098 {
2099 struct exportlist *ep, *ep2;
2100 int i;
2101
2102 for (i = 0; i < exphashsize; i++) {
2103 SLIST_FOREACH_SAFE(ep, &exhp[i], entries, ep2) {
2104 SLIST_REMOVE(&exhp[i], ep, exportlist, entries);
2105 free_exp(ep);
2106 }
2107 SLIST_INIT(&exhp[i]);
2108 }
2109 }
2110
2111 /*
2112 * Read the exports file(s) and call get_exportlist_one() for each line.
2113 */
2114 static void
read_exportfile(int passno)2115 read_exportfile(int passno)
2116 {
2117 int done, i;
2118
2119 /*
2120 * Read in the exports file and build the list, calling
2121 * nmount() as we go along to push the export rules into the kernel.
2122 */
2123 done = 0;
2124 for (i = 0; exnames[i] != NULL; i++) {
2125 if (debug)
2126 warnx("reading exports from %s", exnames[i]);
2127 if ((exp_file = fopen(exnames[i], "r")) == NULL) {
2128 syslog(LOG_WARNING, "can't open %s", exnames[i]);
2129 continue;
2130 }
2131 get_exportlist_one(passno);
2132 fclose(exp_file);
2133 done++;
2134 }
2135 if (done == 0) {
2136 syslog(LOG_ERR, "can't open any exports file");
2137 exit(2);
2138 }
2139 }
2140
2141 /*
2142 * Compare the export lists against the old ones and do nmount() operations
2143 * for any cases that have changed. This avoids doing nmount() for entries
2144 * that have not changed.
2145 * Return 0 upon success, 1 otherwise.
2146 */
2147 static int
compare_nmount_exportlist(struct iovec * iov,int iovlen,char * errmsg)2148 compare_nmount_exportlist(struct iovec *iov, int iovlen, char *errmsg)
2149 {
2150 struct exportlist *ep, *oep;
2151 struct grouplist *grp;
2152 struct statfs fs, ofs;
2153 int i, ret;
2154
2155 /*
2156 * Loop through the current list and look for an entry in the old
2157 * list.
2158 * If found, check to see if it the same.
2159 * If it is not the same, delete and re-export.
2160 * Then mark it done on the old list.
2161 * else (not found)
2162 * export it.
2163 * Any entries left in the old list after processing must have their
2164 * exports deleted.
2165 */
2166 for (i = 0; i < exphashsize; i++)
2167 SLIST_FOREACH(ep, &exphead[i], entries) {
2168 LOGDEBUG("foreach ep=%s", ep->ex_fsdir);
2169 oep = ex_search(&ep->ex_fs, oldexphead);
2170 if (oep != NULL) {
2171 /*
2172 * Check the mount paths are the same.
2173 * If not, return 1 so that the reload of the
2174 * exports will be done in bulk, the
2175 * passno == 0 way.
2176 */
2177 LOGDEBUG("found old exp");
2178 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2179 return (1);
2180 LOGDEBUG("same fsdir");
2181 /*
2182 * Test to see if the entry is the same.
2183 * If not the same delete exports and
2184 * re-export.
2185 */
2186 if (compare_export(ep, oep) != 0) {
2187 /*
2188 * Clear has_publicfh if if was set
2189 * in the old exports, but only if it
2190 * has not been set during processing of
2191 * the exports for this pass, as
2192 * indicated by has_set_publicfh.
2193 */
2194 if (has_set_publicfh == 0 &&
2195 (oep->ex_flag & EX_PUBLICFH) != 0)
2196 has_publicfh = 0;
2197
2198 /* Delete and re-export. */
2199 if (statfs(ep->ex_fsdir, &fs) < 0)
2200 return (1);
2201 delete_export(iov, iovlen, &fs, errmsg);
2202 ret = do_export_mount(ep, &fs);
2203 if (ret != 0)
2204 return (ret);
2205 }
2206 oep->ex_flag |= EX_DONE;
2207 LOGDEBUG("exdone");
2208 } else {
2209 LOGDEBUG("not found so export");
2210 /* Not found, so do export. */
2211 if (statfs(ep->ex_fsdir, &fs) < 0)
2212 return (1);
2213 ret = do_export_mount(ep, &fs);
2214 if (ret != 0)
2215 return (ret);
2216 }
2217 }
2218
2219 /* Delete exports not done. */
2220 for (i = 0; i < exphashsize; i++)
2221 SLIST_FOREACH(oep, &oldexphead[i], entries) {
2222 if ((oep->ex_flag & EX_DONE) == 0) {
2223 LOGDEBUG("not done delete=%s", oep->ex_fsdir);
2224 if (statfs(oep->ex_fsdir, &ofs) >= 0 &&
2225 fsidcmp(&oep->ex_fs, &ofs.f_fsid) == 0) {
2226 LOGDEBUG("do delete");
2227 /*
2228 * Clear has_publicfh if if was set
2229 * in the old exports, but only if it
2230 * has not been set during processing of
2231 * the exports for this pass, as
2232 * indicated by has_set_publicfh.
2233 */
2234 if (has_set_publicfh == 0 &&
2235 (oep->ex_flag & EX_PUBLICFH) != 0)
2236 has_publicfh = 0;
2237
2238 delete_export(iov, iovlen, &ofs,
2239 errmsg);
2240 }
2241 }
2242 }
2243
2244 /* Do the V4 root exports, as required. */
2245 grp = NULL;
2246 if (v4root_ep != NULL)
2247 grp = v4root_ep->ex_grphead;
2248 v4root_phase = 2;
2249 while (v4root_ep != NULL && grp != NULL) {
2250 LOGDEBUG("v4root expath=%s", v4root_dirpath);
2251 ret = do_mount(v4root_ep, grp, grp->gr_exflags, &grp->gr_anon,
2252 v4root_dirpath, strlen(v4root_dirpath), &fs,
2253 grp->gr_numsecflavors, grp->gr_secflavors);
2254 if (ret != 0) {
2255 v4root_phase = 0;
2256 return (ret);
2257 }
2258 grp = grp->gr_next;
2259 }
2260 v4root_phase = 0;
2261 free_v4rootexp();
2262 return (0);
2263 }
2264
2265 /*
2266 * Compare old and current exportlist entries for the fsid and return 0
2267 * if they are the same, 1 otherwise.
2268 */
2269 static int
compare_export(struct exportlist * ep,struct exportlist * oep)2270 compare_export(struct exportlist *ep, struct exportlist *oep)
2271 {
2272 struct grouplist *grp, *ogrp;
2273
2274 if (strcmp(ep->ex_fsdir, oep->ex_fsdir) != 0)
2275 return (1);
2276 if ((ep->ex_flag & EX_DEFSET) != (oep->ex_flag & EX_DEFSET))
2277 return (1);
2278 if ((ep->ex_defdir != NULL && oep->ex_defdir == NULL) ||
2279 (ep->ex_defdir == NULL && oep->ex_defdir != NULL))
2280 return (1);
2281 if (ep->ex_defdir != NULL && (ep->ex_defdir->dp_flag & DP_DEFSET) !=
2282 (oep->ex_defdir->dp_flag & DP_DEFSET))
2283 return (1);
2284 if ((ep->ex_flag & EX_DEFSET) != 0 && (ep->ex_defnumsecflavors !=
2285 oep->ex_defnumsecflavors || ep->ex_defexflags !=
2286 oep->ex_defexflags || compare_cred(&ep->ex_defanon,
2287 &oep->ex_defanon) != 0 || compare_secflavor(ep->ex_defsecflavors,
2288 oep->ex_defsecflavors, ep->ex_defnumsecflavors) != 0))
2289 return (1);
2290
2291 /* Now, check all the groups. */
2292 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2293 ogrp->gr_flag = 0;
2294 for (grp = ep->ex_grphead; grp != NULL; grp = grp->gr_next) {
2295 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp =
2296 ogrp->gr_next)
2297 if ((ogrp->gr_flag & GR_FND) == 0 &&
2298 grp->gr_numsecflavors == ogrp->gr_numsecflavors &&
2299 grp->gr_exflags == ogrp->gr_exflags &&
2300 compare_cred(&grp->gr_anon, &ogrp->gr_anon) == 0 &&
2301 compare_secflavor(grp->gr_secflavors,
2302 ogrp->gr_secflavors, grp->gr_numsecflavors) == 0 &&
2303 compare_addr(grp, ogrp) == 0)
2304 break;
2305 if (ogrp != NULL)
2306 ogrp->gr_flag |= GR_FND;
2307 else
2308 return (1);
2309 }
2310 for (ogrp = oep->ex_grphead; ogrp != NULL; ogrp = ogrp->gr_next)
2311 if ((ogrp->gr_flag & GR_FND) == 0)
2312 return (1);
2313 return (0);
2314 }
2315
2316 /*
2317 * Compare the addresses in the group. It is safe to return they are not
2318 * the same when the are, so only return they are the same when they are
2319 * exactly the same.
2320 */
2321 static int
compare_addr(struct grouplist * grp,struct grouplist * ogrp)2322 compare_addr(struct grouplist *grp, struct grouplist *ogrp)
2323 {
2324 struct addrinfo *ai, *oai;
2325
2326 if (grp->gr_type != ogrp->gr_type)
2327 return (1);
2328 switch (grp->gr_type) {
2329 case GT_HOST:
2330 ai = grp->gr_ptr.gt_addrinfo;
2331 oai = ogrp->gr_ptr.gt_addrinfo;
2332 for (; ai != NULL && oai != NULL; ai = ai->ai_next,
2333 oai = oai->ai_next) {
2334 if (sacmp(ai->ai_addr, oai->ai_addr, NULL) != 0)
2335 return (1);
2336 }
2337 if (ai != NULL || oai != NULL)
2338 return (1);
2339 break;
2340 case GT_NET:
2341 /* First compare the masks and then the nets. */
2342 if (sacmp((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask,
2343 (struct sockaddr *)&ogrp->gr_ptr.gt_net.nt_mask, NULL) != 0)
2344 return (1);
2345 if (sacmp((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net,
2346 (struct sockaddr *)&ogrp->gr_ptr.gt_net.nt_net,
2347 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask) != 0)
2348 return (1);
2349 break;
2350 default:
2351 return (1);
2352 }
2353 return (0);
2354 }
2355
2356 /*
2357 * This algorithm compares two arrays of "n" items. It returns 0 if they are
2358 * the "same" and 1 otherwise. Although suboptimal, it is always safe to
2359 * return 1, which makes compare_nmount_export() reload the exports entry.
2360 * "same" refers to having the same set of values in the two arrays.
2361 * The arrays are in no particular order and duplicates (multiple entries
2362 * in an array with the same value) is allowed.
2363 * The algorithm is inefficient, but the common case of indentical arrays is
2364 * handled first and "n" is normally fairly small.
2365 * Since the two functions need the same algorithm but for arrays of
2366 * different types (gid_t vs int), this is done as a macro.
2367 */
2368 #define COMPARE_ARRAYS(a1, a2, n) \
2369 do { \
2370 int fnd, fndarray[(n)], i, j; \
2371 /* Handle common case of identical arrays. */ \
2372 for (i = 0; i < (n); i++) \
2373 if ((a1)[i] != (a2)[i]) \
2374 break; \
2375 if (i == (n)) \
2376 return (0); \
2377 for (i = 0; i < (n); i++) \
2378 fndarray[i] = 0; \
2379 for (i = 0; i < (n); i++) { \
2380 fnd = 0; \
2381 for (j = 0; j < (n); j++) { \
2382 if ((a1)[i] == (a2)[j]) { \
2383 fndarray[j] = 1; \
2384 fnd = 1; \
2385 } \
2386 } \
2387 if (fnd == 0) \
2388 return (1); \
2389 } \
2390 for (i = 0; i < (n); i++) \
2391 if (fndarray[i] == 0) \
2392 return (1); \
2393 return (0); \
2394 } while (0)
2395
2396 /*
2397 * Compare two struct expcred's. Return 0 if the same and 1 otherwise.
2398 */
2399 static int
compare_cred(struct expcred * cr0,struct expcred * cr1)2400 compare_cred(struct expcred *cr0, struct expcred *cr1)
2401 {
2402
2403 if (cr0->cr_uid != cr1->cr_uid || cr0->cr_ngroups != cr1->cr_ngroups)
2404 return (1);
2405
2406 COMPARE_ARRAYS(cr0->cr_groups, cr1->cr_groups, cr0->cr_ngroups);
2407 }
2408
2409 /*
2410 * Compare two lists of security flavors. Return 0 if the same and 1 otherwise.
2411 */
2412 static int
compare_secflavor(int * sec1,int * sec2,int nsec)2413 compare_secflavor(int *sec1, int *sec2, int nsec)
2414 {
2415
2416 COMPARE_ARRAYS(sec1, sec2, nsec);
2417 }
2418
2419 /*
2420 * Delete an exports entry.
2421 */
2422 static void
delete_export(struct iovec * iov,int iovlen,struct statfs * fsp,char * errmsg)2423 delete_export(struct iovec *iov, int iovlen, struct statfs *fsp, char *errmsg)
2424 {
2425 struct xvfsconf vfc;
2426
2427 if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) {
2428 syslog(LOG_ERR, "getvfsbyname() failed for %s",
2429 fsp->f_fstypename);
2430 return;
2431 }
2432
2433 /*
2434 * We do not need to delete "export" flag from
2435 * filesystems that do not have it set.
2436 */
2437 if (!(fsp->f_flags & MNT_EXPORTED))
2438 return;
2439 /*
2440 * Do not delete export for network filesystem by
2441 * passing "export" arg to nmount().
2442 * It only makes sense to do this for local filesystems.
2443 */
2444 if (vfc.vfc_flags & VFCF_NETWORK)
2445 return;
2446
2447 iov[1].iov_base = fsp->f_fstypename;
2448 iov[1].iov_len = strlen(fsp->f_fstypename) + 1;
2449 iov[3].iov_base = fsp->f_mntonname;
2450 iov[3].iov_len = strlen(fsp->f_mntonname) + 1;
2451 iov[5].iov_base = fsp->f_mntfromname;
2452 iov[5].iov_len = strlen(fsp->f_mntfromname) + 1;
2453 errmsg[0] = '\0';
2454
2455 /*
2456 * EXDEV is returned when path exists but is not a
2457 * mount point. May happens if raced with unmount.
2458 */
2459 if (nmount(iov, iovlen, fsp->f_flags) < 0 && errno != ENOENT &&
2460 errno != ENOTSUP && errno != EXDEV) {
2461 syslog(LOG_ERR,
2462 "can't delete exports for %s: %m %s",
2463 fsp->f_mntonname, errmsg);
2464 }
2465 }
2466
2467 /*
2468 * Allocate an export list element
2469 */
2470 static struct exportlist *
get_exp(void)2471 get_exp(void)
2472 {
2473 struct exportlist *ep;
2474
2475 ep = (struct exportlist *)calloc(1, sizeof (struct exportlist));
2476 if (ep == (struct exportlist *)NULL)
2477 out_of_mem();
2478 return (ep);
2479 }
2480
2481 /*
2482 * Allocate a group list element
2483 */
2484 static struct grouplist *
get_grp(void)2485 get_grp(void)
2486 {
2487 struct grouplist *gp;
2488
2489 gp = (struct grouplist *)calloc(1, sizeof (struct grouplist));
2490 if (gp == (struct grouplist *)NULL)
2491 out_of_mem();
2492 return (gp);
2493 }
2494
2495 /*
2496 * Clean up upon an error in get_exportlist().
2497 */
2498 static void
getexp_err(struct exportlist * ep,struct grouplist * grp,const char * reason)2499 getexp_err(struct exportlist *ep, struct grouplist *grp, const char *reason)
2500 {
2501 struct grouplist *tgrp;
2502
2503 if (!(opt_flags & OP_QUIET)) {
2504 if (reason != NULL)
2505 syslog(LOG_ERR, "bad exports list line '%s': %s", line,
2506 reason);
2507 else
2508 syslog(LOG_ERR, "bad exports list line '%s'", line);
2509 }
2510 if (ep && (ep->ex_flag & EX_LINKED) == 0)
2511 free_exp(ep);
2512 while (grp) {
2513 tgrp = grp;
2514 grp = grp->gr_next;
2515 free_grp(tgrp);
2516 }
2517 }
2518
2519 /*
2520 * Search the export list for a matching fs.
2521 */
2522 static struct exportlist *
ex_search(fsid_t * fsid,struct exportlisthead * exhp)2523 ex_search(fsid_t *fsid, struct exportlisthead *exhp)
2524 {
2525 struct exportlist *ep;
2526 uint32_t i;
2527
2528 i = EXPHASH(fsid);
2529 SLIST_FOREACH(ep, &exhp[i], entries) {
2530 if (fsidcmp(&ep->ex_fs, fsid) == 0)
2531 return (ep);
2532 }
2533
2534 return (ep);
2535 }
2536
2537 /*
2538 * Add a directory path to the list.
2539 */
2540 static char *
add_expdir(struct dirlist ** dpp,char * cp,int len)2541 add_expdir(struct dirlist **dpp, char *cp, int len)
2542 {
2543 struct dirlist *dp;
2544
2545 dp = malloc(sizeof (struct dirlist));
2546 if (dp == (struct dirlist *)NULL)
2547 out_of_mem();
2548 dp->dp_left = *dpp;
2549 dp->dp_right = (struct dirlist *)NULL;
2550 dp->dp_flag = 0;
2551 dp->dp_hosts = (struct hostlist *)NULL;
2552 dp->dp_dirp = strndup(cp, len);
2553 if (dp->dp_dirp == NULL)
2554 out_of_mem();
2555 *dpp = dp;
2556 return (dp->dp_dirp);
2557 }
2558
2559 /*
2560 * Hang the dir list element off the dirpath binary tree as required
2561 * and update the entry for host.
2562 */
2563 static void
hang_dirp(struct dirlist * dp,struct grouplist * grp,struct exportlist * ep,int flags,struct expcred * anoncrp,uint64_t exflags)2564 hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep,
2565 int flags, struct expcred *anoncrp, uint64_t exflags)
2566 {
2567 struct hostlist *hp;
2568 struct dirlist *dp2;
2569
2570 if (flags & OP_ALLDIRS) {
2571 if (ep->ex_defdir)
2572 free((caddr_t)dp);
2573 else
2574 ep->ex_defdir = dp;
2575 if (grp == (struct grouplist *)NULL) {
2576 ep->ex_flag |= EX_DEFSET;
2577 ep->ex_defdir->dp_flag |= DP_DEFSET;
2578 /* Save the default security flavors list. */
2579 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2580 if (ep->ex_numsecflavors > 0)
2581 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2582 sizeof(ep->ex_secflavors));
2583 cp_cred(&ep->ex_defanon, anoncrp);
2584 ep->ex_defexflags = exflags;
2585 } else while (grp) {
2586 hp = get_ht();
2587 hp->ht_grp = grp;
2588 hp->ht_next = ep->ex_defdir->dp_hosts;
2589 ep->ex_defdir->dp_hosts = hp;
2590 /* Save the security flavors list for this host set. */
2591 grp->gr_numsecflavors = ep->ex_numsecflavors;
2592 if (ep->ex_numsecflavors > 0)
2593 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2594 sizeof(ep->ex_secflavors));
2595 grp = grp->gr_next;
2596 }
2597 } else {
2598
2599 /*
2600 * Loop through the directories adding them to the tree.
2601 */
2602 while (dp) {
2603 dp2 = dp->dp_left;
2604 add_dlist(&ep->ex_dirl, dp, grp, flags, ep, anoncrp,
2605 exflags);
2606 dp = dp2;
2607 }
2608 }
2609 }
2610
2611 /*
2612 * Traverse the binary tree either updating a node that is already there
2613 * for the new directory or adding the new node.
2614 */
2615 static void
add_dlist(struct dirlist ** dpp,struct dirlist * newdp,struct grouplist * grp,int flags,struct exportlist * ep,struct expcred * anoncrp,uint64_t exflags)2616 add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp,
2617 int flags, struct exportlist *ep, struct expcred *anoncrp,
2618 uint64_t exflags)
2619 {
2620 struct dirlist *dp;
2621 struct hostlist *hp;
2622 int cmp;
2623
2624 dp = *dpp;
2625 if (dp) {
2626 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
2627 if (cmp > 0) {
2628 add_dlist(&dp->dp_left, newdp, grp, flags, ep, anoncrp,
2629 exflags);
2630 return;
2631 } else if (cmp < 0) {
2632 add_dlist(&dp->dp_right, newdp, grp, flags, ep, anoncrp,
2633 exflags);
2634 return;
2635 } else
2636 free((caddr_t)newdp);
2637 } else {
2638 dp = newdp;
2639 dp->dp_left = (struct dirlist *)NULL;
2640 *dpp = dp;
2641 }
2642 if (grp) {
2643
2644 /*
2645 * Hang all of the host(s) off of the directory point.
2646 */
2647 do {
2648 hp = get_ht();
2649 hp->ht_grp = grp;
2650 hp->ht_next = dp->dp_hosts;
2651 dp->dp_hosts = hp;
2652 /* Save the security flavors list for this host set. */
2653 grp->gr_numsecflavors = ep->ex_numsecflavors;
2654 if (ep->ex_numsecflavors > 0)
2655 memcpy(grp->gr_secflavors, ep->ex_secflavors,
2656 sizeof(ep->ex_secflavors));
2657 grp = grp->gr_next;
2658 } while (grp);
2659 } else {
2660 ep->ex_flag |= EX_DEFSET;
2661 dp->dp_flag |= DP_DEFSET;
2662 /* Save the default security flavors list. */
2663 ep->ex_defnumsecflavors = ep->ex_numsecflavors;
2664 if (ep->ex_numsecflavors > 0)
2665 memcpy(ep->ex_defsecflavors, ep->ex_secflavors,
2666 sizeof(ep->ex_secflavors));
2667 cp_cred(&ep->ex_defanon, anoncrp);
2668 ep->ex_defexflags = exflags;
2669 }
2670 }
2671
2672 /*
2673 * Search for a dirpath on the export point.
2674 */
2675 static struct dirlist *
dirp_search(struct dirlist * dp,char * dirp)2676 dirp_search(struct dirlist *dp, char *dirp)
2677 {
2678 int cmp;
2679
2680 if (dp) {
2681 cmp = strcmp(dp->dp_dirp, dirp);
2682 if (cmp > 0)
2683 return (dirp_search(dp->dp_left, dirp));
2684 else if (cmp < 0)
2685 return (dirp_search(dp->dp_right, dirp));
2686 else
2687 return (dp);
2688 }
2689 return (dp);
2690 }
2691
2692 /*
2693 * Scan for a host match in a directory tree.
2694 */
2695 static int
chk_host(struct dirlist * dp,struct sockaddr * saddr,int * defsetp,int * hostsetp,int * numsecflavors,int ** secflavorsp)2696 chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp,
2697 int *hostsetp, int *numsecflavors, int **secflavorsp)
2698 {
2699 struct hostlist *hp;
2700 struct grouplist *grp;
2701 struct addrinfo *ai;
2702
2703 if (dp) {
2704 if (dp->dp_flag & DP_DEFSET)
2705 *defsetp = dp->dp_flag;
2706 hp = dp->dp_hosts;
2707 while (hp) {
2708 grp = hp->ht_grp;
2709 switch (grp->gr_type) {
2710 case GT_HOST:
2711 ai = grp->gr_ptr.gt_addrinfo;
2712 for (; ai; ai = ai->ai_next) {
2713 if (!sacmp(ai->ai_addr, saddr, NULL)) {
2714 *hostsetp =
2715 (hp->ht_flag | DP_HOSTSET);
2716 if (numsecflavors != NULL) {
2717 *numsecflavors =
2718 grp->gr_numsecflavors;
2719 *secflavorsp =
2720 grp->gr_secflavors;
2721 }
2722 return (1);
2723 }
2724 }
2725 break;
2726 case GT_NET:
2727 if (!sacmp(saddr, (struct sockaddr *)
2728 &grp->gr_ptr.gt_net.nt_net,
2729 (struct sockaddr *)
2730 &grp->gr_ptr.gt_net.nt_mask)) {
2731 *hostsetp = (hp->ht_flag | DP_HOSTSET);
2732 if (numsecflavors != NULL) {
2733 *numsecflavors =
2734 grp->gr_numsecflavors;
2735 *secflavorsp =
2736 grp->gr_secflavors;
2737 }
2738 return (1);
2739 }
2740 break;
2741 }
2742 hp = hp->ht_next;
2743 }
2744 }
2745 return (0);
2746 }
2747
2748 /*
2749 * Scan tree for a host that matches the address.
2750 */
2751 static int
scan_tree(struct dirlist * dp,struct sockaddr * saddr)2752 scan_tree(struct dirlist *dp, struct sockaddr *saddr)
2753 {
2754 int defset, hostset;
2755
2756 if (dp) {
2757 if (scan_tree(dp->dp_left, saddr))
2758 return (1);
2759 if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL))
2760 return (1);
2761 if (scan_tree(dp->dp_right, saddr))
2762 return (1);
2763 }
2764 return (0);
2765 }
2766
2767 /*
2768 * Traverse the dirlist tree and free it up.
2769 */
2770 static void
free_dir(struct dirlist * dp)2771 free_dir(struct dirlist *dp)
2772 {
2773
2774 if (dp) {
2775 free_dir(dp->dp_left);
2776 free_dir(dp->dp_right);
2777 free_host(dp->dp_hosts);
2778 free(dp->dp_dirp);
2779 free(dp);
2780 }
2781 }
2782
2783 /*
2784 * Parse a colon separated list of security flavors
2785 */
2786 static int
parsesec(char * seclist,struct exportlist * ep)2787 parsesec(char *seclist, struct exportlist *ep)
2788 {
2789 char *cp, savedc;
2790 int flavor;
2791
2792 ep->ex_numsecflavors = 0;
2793 for (;;) {
2794 cp = strchr(seclist, ':');
2795 if (cp) {
2796 savedc = *cp;
2797 *cp = '\0';
2798 }
2799
2800 if (!strcmp(seclist, "sys"))
2801 flavor = AUTH_SYS;
2802 else if (!strcmp(seclist, "krb5"))
2803 flavor = RPCSEC_GSS_KRB5;
2804 else if (!strcmp(seclist, "krb5i"))
2805 flavor = RPCSEC_GSS_KRB5I;
2806 else if (!strcmp(seclist, "krb5p"))
2807 flavor = RPCSEC_GSS_KRB5P;
2808 else {
2809 if (cp)
2810 *cp = savedc;
2811 syslog(LOG_ERR, "bad sec flavor: %s", seclist);
2812 return (1);
2813 }
2814 if (ep->ex_numsecflavors == MAXSECFLAVORS) {
2815 if (cp)
2816 *cp = savedc;
2817 syslog(LOG_ERR, "too many sec flavors: %s", seclist);
2818 return (1);
2819 }
2820 ep->ex_secflavors[ep->ex_numsecflavors] = flavor;
2821 ep->ex_numsecflavors++;
2822 if (cp) {
2823 *cp = savedc;
2824 seclist = cp + 1;
2825 } else {
2826 break;
2827 }
2828 }
2829 return (0);
2830 }
2831
2832 /*
2833 * Parse the option string and update fields.
2834 * Option arguments may either be -<option>=<value> or
2835 * -<option> <value>
2836 */
2837 static int
do_opt(char ** cpp,char ** endcpp,struct exportlist * ep,struct grouplist * grp,int * has_hostp,uint64_t * exflagsp,struct expcred * cr)2838 do_opt(char **cpp, char **endcpp, struct exportlist *ep, struct grouplist *grp,
2839 int *has_hostp, uint64_t *exflagsp, struct expcred *cr)
2840 {
2841 char *cpoptarg, *cpoptend;
2842 char *cp, *endcp, *cpopt, savedc, savedc2;
2843 int allflag, usedarg, fnd_equal;
2844
2845 savedc2 = '\0';
2846 cpopt = *cpp;
2847 cpopt++;
2848 cp = *endcpp;
2849 savedc = *cp;
2850 *cp = '\0';
2851 while (cpopt && *cpopt) {
2852 allflag = 1;
2853 usedarg = -2;
2854 fnd_equal = 0;
2855 if ((cpoptend = strchr(cpopt, ','))) {
2856 *cpoptend++ = '\0';
2857 if ((cpoptarg = strchr(cpopt, '='))) {
2858 *cpoptarg++ = '\0';
2859 fnd_equal = 1;
2860 }
2861 } else {
2862 if ((cpoptarg = strchr(cpopt, '='))) {
2863 *cpoptarg++ = '\0';
2864 fnd_equal = 1;
2865 } else {
2866 *cp = savedc;
2867 nextfield(&cp, &endcp);
2868 **endcpp = '\0';
2869 if (endcp > cp && *cp != '-') {
2870 cpoptarg = cp;
2871 savedc2 = *endcp;
2872 *endcp = '\0';
2873 usedarg = 0;
2874 }
2875 }
2876 }
2877 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
2878 if (fnd_equal == 1) {
2879 syslog(LOG_ERR, "= after op: %s", cpopt);
2880 return (1);
2881 }
2882 *exflagsp |= MNT_EXRDONLY;
2883 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
2884 !(allflag = strcmp(cpopt, "mapall")) ||
2885 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
2886 usedarg++;
2887 parsecred(cpoptarg, cr);
2888 if (allflag == 0) {
2889 *exflagsp |= MNT_EXPORTANON;
2890 opt_flags |= OP_MAPALL;
2891 } else
2892 opt_flags |= OP_MAPROOT;
2893 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
2894 !strcmp(cpopt, "m"))) {
2895 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
2896 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
2897 return (1);
2898 }
2899 usedarg++;
2900 opt_flags |= OP_MASK;
2901 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
2902 !strcmp(cpopt, "n"))) {
2903 if (strchr(cpoptarg, '/') != NULL) {
2904 if (debug)
2905 fprintf(stderr, "setting OP_MASKLEN\n");
2906 opt_flags |= OP_MASKLEN;
2907 }
2908 if (grp->gr_type != GT_NULL) {
2909 syslog(LOG_ERR, "network/host conflict");
2910 return (1);
2911 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
2912 syslog(LOG_ERR, "bad net: %s", cpoptarg);
2913 return (1);
2914 }
2915 grp->gr_type = GT_NET;
2916 *has_hostp = 1;
2917 usedarg++;
2918 opt_flags |= OP_NET;
2919 } else if (!strcmp(cpopt, "alldirs")) {
2920 if (fnd_equal == 1) {
2921 syslog(LOG_ERR, "= after op: %s", cpopt);
2922 return (1);
2923 }
2924 opt_flags |= OP_ALLDIRS;
2925 } else if (!strcmp(cpopt, "public")) {
2926 if (fnd_equal == 1) {
2927 syslog(LOG_ERR, "= after op: %s", cpopt);
2928 return (1);
2929 }
2930 *exflagsp |= MNT_EXPUBLIC;
2931 } else if (!strcmp(cpopt, "webnfs")) {
2932 if (fnd_equal == 1) {
2933 syslog(LOG_ERR, "= after op: %s", cpopt);
2934 return (1);
2935 }
2936 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
2937 opt_flags |= OP_MAPALL;
2938 } else if (cpoptarg && !strcmp(cpopt, "index")) {
2939 ep->ex_indexfile = strdup(cpoptarg);
2940 } else if (!strcmp(cpopt, "quiet")) {
2941 if (fnd_equal == 1) {
2942 syslog(LOG_ERR, "= after op: %s", cpopt);
2943 return (1);
2944 }
2945 opt_flags |= OP_QUIET;
2946 } else if (cpoptarg && !strcmp(cpopt, "sec")) {
2947 if (parsesec(cpoptarg, ep))
2948 return (1);
2949 opt_flags |= OP_SEC;
2950 usedarg++;
2951 } else if (!strcmp(cpopt, "tls")) {
2952 if (fnd_equal == 1) {
2953 syslog(LOG_ERR, "= after op: %s", cpopt);
2954 return (1);
2955 }
2956 *exflagsp |= MNT_EXTLS;
2957 } else if (!strcmp(cpopt, "tlscert")) {
2958 if (fnd_equal == 1) {
2959 syslog(LOG_ERR, "= after op: %s", cpopt);
2960 return (1);
2961 }
2962 *exflagsp |= (MNT_EXTLS | MNT_EXTLSCERT);
2963 } else if (!strcmp(cpopt, "tlscertuser")) {
2964 if (fnd_equal == 1) {
2965 syslog(LOG_ERR, "= after op: %s", cpopt);
2966 return (1);
2967 }
2968 *exflagsp |= (MNT_EXTLS | MNT_EXTLSCERT |
2969 MNT_EXTLSCERTUSER);
2970 } else {
2971 syslog(LOG_ERR, "bad opt %s", cpopt);
2972 return (1);
2973 }
2974 if (usedarg >= 0) {
2975 *endcp = savedc2;
2976 **endcpp = savedc;
2977 if (usedarg > 0) {
2978 *cpp = cp;
2979 *endcpp = endcp;
2980 }
2981 return (0);
2982 }
2983 cpopt = cpoptend;
2984 }
2985 **endcpp = savedc;
2986 return (0);
2987 }
2988
2989 /*
2990 * Translate a character string to the corresponding list of network
2991 * addresses for a hostname.
2992 */
2993 static int
get_host(char * cp,struct grouplist * grp,struct grouplist * tgrp)2994 get_host(char *cp, struct grouplist *grp, struct grouplist *tgrp)
2995 {
2996 struct grouplist *checkgrp;
2997 struct addrinfo *ai, *tai, hints;
2998 int ecode;
2999 char host[NI_MAXHOST];
3000
3001 if (grp->gr_type != GT_NULL) {
3002 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
3003 return (1);
3004 }
3005 memset(&hints, 0, sizeof hints);
3006 hints.ai_flags = AI_CANONNAME;
3007 hints.ai_protocol = IPPROTO_UDP;
3008 ecode = getaddrinfo(cp, NULL, &hints, &ai);
3009 if (ecode != 0) {
3010 syslog(LOG_ERR,"can't get address info for host %s", cp);
3011 return 1;
3012 }
3013 grp->gr_ptr.gt_addrinfo = ai;
3014 while (ai != NULL) {
3015 if (ai->ai_canonname == NULL) {
3016 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
3017 sizeof host, NULL, 0, NI_NUMERICHOST) != 0)
3018 strlcpy(host, "?", sizeof(host));
3019 ai->ai_canonname = strdup(host);
3020 ai->ai_flags |= AI_CANONNAME;
3021 }
3022 if (debug)
3023 fprintf(stderr, "got host %s\n", ai->ai_canonname);
3024 /*
3025 * Sanity check: make sure we don't already have an entry
3026 * for this host in the grouplist.
3027 */
3028 for (checkgrp = tgrp; checkgrp != NULL;
3029 checkgrp = checkgrp->gr_next) {
3030 if (checkgrp->gr_type != GT_HOST)
3031 continue;
3032 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
3033 tai = tai->ai_next) {
3034 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
3035 continue;
3036 if (debug)
3037 fprintf(stderr,
3038 "ignoring duplicate host %s\n",
3039 ai->ai_canonname);
3040 grp->gr_type = GT_IGNORE;
3041 return (0);
3042 }
3043 }
3044 ai = ai->ai_next;
3045 }
3046 grp->gr_type = GT_HOST;
3047 return (0);
3048 }
3049
3050 /*
3051 * Free up an exports list component
3052 */
3053 static void
free_exp(struct exportlist * ep)3054 free_exp(struct exportlist *ep)
3055 {
3056 struct grouplist *grp, *tgrp;
3057
3058 if (ep->ex_defdir) {
3059 free_host(ep->ex_defdir->dp_hosts);
3060 free((caddr_t)ep->ex_defdir);
3061 }
3062 if (ep->ex_fsdir)
3063 free(ep->ex_fsdir);
3064 if (ep->ex_indexfile)
3065 free(ep->ex_indexfile);
3066 free_dir(ep->ex_dirl);
3067 grp = ep->ex_grphead;
3068 while (grp) {
3069 tgrp = grp;
3070 grp = grp->gr_next;
3071 free_grp(tgrp);
3072 }
3073 if (ep->ex_defanon.cr_groups != ep->ex_defanon.cr_smallgrps)
3074 free(ep->ex_defanon.cr_groups);
3075 free((caddr_t)ep);
3076 }
3077
3078 /*
3079 * Free up the v4root exports.
3080 */
3081 static void
free_v4rootexp(void)3082 free_v4rootexp(void)
3083 {
3084
3085 if (v4root_ep != NULL) {
3086 free_exp(v4root_ep);
3087 v4root_ep = NULL;
3088 }
3089 }
3090
3091 /*
3092 * Free hosts.
3093 */
3094 static void
free_host(struct hostlist * hp)3095 free_host(struct hostlist *hp)
3096 {
3097 struct hostlist *hp2;
3098
3099 while (hp) {
3100 hp2 = hp;
3101 hp = hp->ht_next;
3102 free((caddr_t)hp2);
3103 }
3104 }
3105
3106 static struct hostlist *
get_ht(void)3107 get_ht(void)
3108 {
3109 struct hostlist *hp;
3110
3111 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
3112 if (hp == (struct hostlist *)NULL)
3113 out_of_mem();
3114 hp->ht_next = (struct hostlist *)NULL;
3115 hp->ht_flag = 0;
3116 return (hp);
3117 }
3118
3119 /*
3120 * Out of memory, fatal
3121 */
3122 static void
out_of_mem(void)3123 out_of_mem(void)
3124 {
3125
3126 syslog(LOG_ERR, "out of memory");
3127 exit(2);
3128 }
3129
3130 /*
3131 * Call do_mount() from the struct exportlist, for each case needed.
3132 */
3133 static int
do_export_mount(struct exportlist * ep,struct statfs * fsp)3134 do_export_mount(struct exportlist *ep, struct statfs *fsp)
3135 {
3136 struct grouplist *grp, defgrp;
3137 int ret;
3138 size_t dirlen;
3139
3140 LOGDEBUG("do_export_mount=%s", ep->ex_fsdir);
3141 dirlen = strlen(ep->ex_fsdir);
3142 if ((ep->ex_flag & EX_DEFSET) != 0) {
3143 defgrp.gr_type = GT_DEFAULT;
3144 defgrp.gr_next = NULL;
3145 /* We have an entry for all other hosts/nets. */
3146 LOGDEBUG("ex_defexflags=0x%jx", (uintmax_t)ep->ex_defexflags);
3147 ret = do_mount(ep, &defgrp, ep->ex_defexflags, &ep->ex_defanon,
3148 ep->ex_fsdir, dirlen, fsp, ep->ex_defnumsecflavors,
3149 ep->ex_defsecflavors);
3150 if (ret != 0)
3151 return (ret);
3152 }
3153
3154 /* Do a mount for each group. */
3155 grp = ep->ex_grphead;
3156 while (grp != NULL) {
3157 LOGDEBUG("do mount gr_type=0x%x gr_exflags=0x%jx",
3158 grp->gr_type, (uintmax_t)grp->gr_exflags);
3159 ret = do_mount(ep, grp, grp->gr_exflags, &grp->gr_anon,
3160 ep->ex_fsdir, dirlen, fsp, grp->gr_numsecflavors,
3161 grp->gr_secflavors);
3162 if (ret != 0)
3163 return (ret);
3164 grp = grp->gr_next;
3165 }
3166 return (0);
3167 }
3168
3169 /*
3170 * Do the nmount() syscall with the update flag to push the export info into
3171 * the kernel.
3172 */
3173 static int
do_mount(struct exportlist * ep,struct grouplist * grp,uint64_t exflags,struct expcred * anoncrp,char * dirp,int dirplen,struct statfs * fsb,int numsecflavors,int * secflavors)3174 do_mount(struct exportlist *ep, struct grouplist *grp, uint64_t exflags,
3175 struct expcred *anoncrp, char *dirp, int dirplen, struct statfs *fsb,
3176 int numsecflavors, int *secflavors)
3177 {
3178 struct statfs fsb1;
3179 struct addrinfo *ai;
3180 struct export_args *eap;
3181 char errmsg[255];
3182 char *cp;
3183 int done;
3184 char savedc;
3185 struct iovec *iov;
3186 int i, iovlen;
3187 int ret;
3188 struct nfsex_args nfsea;
3189
3190 eap = &nfsea.export;
3191
3192 cp = NULL;
3193 savedc = '\0';
3194 iov = NULL;
3195 iovlen = 0;
3196 ret = 0;
3197
3198 bzero(eap, sizeof (struct export_args));
3199 bzero(errmsg, sizeof(errmsg));
3200 eap->ex_flags = exflags;
3201 eap->ex_uid = anoncrp->cr_uid;
3202 eap->ex_ngroups = anoncrp->cr_ngroups;
3203 /*
3204 * Use the memory pointed to by 'anoncrp', as it outlives 'eap' which is
3205 * local to this function.
3206 */
3207 eap->ex_groups = anoncrp->cr_groups;
3208 LOGDEBUG("do_mount exflags=0x%jx", (uintmax_t)exflags);
3209 eap->ex_indexfile = ep->ex_indexfile;
3210 if (grp->gr_type == GT_HOST)
3211 ai = grp->gr_ptr.gt_addrinfo;
3212 else
3213 ai = NULL;
3214 eap->ex_numsecflavors = numsecflavors;
3215 LOGDEBUG("do_mount numsec=%d", numsecflavors);
3216 for (i = 0; i < eap->ex_numsecflavors; i++)
3217 eap->ex_secflavors[i] = secflavors[i];
3218 if (eap->ex_numsecflavors == 0) {
3219 eap->ex_numsecflavors = 1;
3220 eap->ex_secflavors[0] = AUTH_SYS;
3221 }
3222 done = FALSE;
3223
3224 if (v4root_phase == 0) {
3225 build_iovec(&iov, &iovlen, "fstype", NULL, 0);
3226 build_iovec(&iov, &iovlen, "fspath", NULL, 0);
3227 build_iovec(&iov, &iovlen, "from", NULL, 0);
3228 build_iovec(&iov, &iovlen, "update", NULL, 0);
3229 build_iovec(&iov, &iovlen, "export", eap,
3230 sizeof (struct export_args));
3231 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
3232 }
3233
3234 while (!done) {
3235 switch (grp->gr_type) {
3236 case GT_HOST:
3237 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
3238 goto skip;
3239 eap->ex_addr = ai->ai_addr;
3240 eap->ex_addrlen = ai->ai_addrlen;
3241 eap->ex_masklen = 0;
3242 break;
3243 case GT_NET:
3244 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
3245 have_v6 == 0)
3246 goto skip;
3247 eap->ex_addr =
3248 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
3249 eap->ex_addrlen =
3250 ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_net)->sa_len;
3251 eap->ex_mask =
3252 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
3253 eap->ex_masklen = ((struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask)->sa_len;
3254 break;
3255 case GT_DEFAULT:
3256 eap->ex_addr = NULL;
3257 eap->ex_addrlen = 0;
3258 eap->ex_mask = NULL;
3259 eap->ex_masklen = 0;
3260 break;
3261 case GT_IGNORE:
3262 ret = 0;
3263 goto error_exit;
3264 break;
3265 default:
3266 syslog(LOG_ERR, "bad grouptype");
3267 if (cp)
3268 *cp = savedc;
3269 ret = 1;
3270 goto error_exit;
3271 }
3272
3273 /*
3274 * For V4:, use the nfssvc() syscall, instead of mount().
3275 */
3276 if (v4root_phase == 2) {
3277 nfsea.fspec = v4root_dirpath;
3278 if (nfssvc(NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT,
3279 (caddr_t)&nfsea) < 0) {
3280 syslog(LOG_ERR, "Exporting V4: failed");
3281 ret = 2;
3282 goto error_exit;
3283 }
3284 } else {
3285 /*
3286 * XXX:
3287 * Maybe I should just use the fsb->f_mntonname path
3288 * instead of looping back up the dirp to the mount
3289 * point??
3290 * Also, needs to know how to export all types of local
3291 * exportable filesystems and not just "ufs".
3292 */
3293 iov[1].iov_base = fsb->f_fstypename; /* "fstype" */
3294 iov[1].iov_len = strlen(fsb->f_fstypename) + 1;
3295 iov[3].iov_base = fsb->f_mntonname; /* "fspath" */
3296 iov[3].iov_len = strlen(fsb->f_mntonname) + 1;
3297 iov[5].iov_base = fsb->f_mntfromname; /* "from" */
3298 iov[5].iov_len = strlen(fsb->f_mntfromname) + 1;
3299 errmsg[0] = '\0';
3300
3301 while (nmount(iov, iovlen, fsb->f_flags) < 0) {
3302 if (cp)
3303 *cp-- = savedc;
3304 else
3305 cp = dirp + dirplen - 1;
3306 if (opt_flags & OP_QUIET) {
3307 ret = 1;
3308 goto error_exit;
3309 }
3310 if (errno == EPERM) {
3311 if (debug)
3312 warnx("can't change attributes for %s: %s",
3313 dirp, errmsg);
3314 syslog(LOG_ERR,
3315 "can't change attributes for %s: %s",
3316 dirp, errmsg);
3317 ret = 1;
3318 goto error_exit;
3319 }
3320 if (opt_flags & OP_ALLDIRS) {
3321 if (errno == EINVAL)
3322 syslog(LOG_ERR,
3323 "-alldirs requested but %s is not a filesystem mountpoint",
3324 dirp);
3325 else
3326 syslog(LOG_ERR,
3327 "could not remount %s: %m",
3328 dirp);
3329 ret = 1;
3330 goto error_exit;
3331 }
3332 /* back up over the last component */
3333 while (cp > dirp && *cp == '/')
3334 cp--;
3335 while (cp > dirp && *(cp - 1) != '/')
3336 cp--;
3337 if (cp == dirp) {
3338 if (debug)
3339 warnx("mnt unsucc");
3340 syslog(LOG_ERR, "can't export %s %s",
3341 dirp, errmsg);
3342 ret = 1;
3343 goto error_exit;
3344 }
3345 savedc = *cp;
3346 *cp = '\0';
3347 /*
3348 * Check that we're still on the same
3349 * filesystem.
3350 */
3351 if (statfs(dirp, &fsb1) != 0 ||
3352 fsidcmp(&fsb1.f_fsid, &fsb->f_fsid) != 0) {
3353 *cp = savedc;
3354 syslog(LOG_ERR,
3355 "can't export %s %s", dirp,
3356 errmsg);
3357 ret = 1;
3358 goto error_exit;
3359 }
3360 }
3361 }
3362
3363 /*
3364 * For the experimental server:
3365 * If this is the public directory, get the file handle
3366 * and load it into the kernel via the nfssvc() syscall.
3367 */
3368 if ((exflags & MNT_EXPUBLIC) != 0) {
3369 fhandle_t fh;
3370 char *public_name;
3371
3372 if (eap->ex_indexfile != NULL)
3373 public_name = eap->ex_indexfile;
3374 else
3375 public_name = dirp;
3376 if (getfh(public_name, &fh) < 0)
3377 syslog(LOG_ERR,
3378 "Can't get public fh for %s", public_name);
3379 else if (nfssvc(NFSSVC_PUBLICFH, (caddr_t)&fh) < 0)
3380 syslog(LOG_ERR,
3381 "Can't set public fh for %s", public_name);
3382 else {
3383 has_publicfh = 1;
3384 has_set_publicfh = 1;
3385 ep->ex_flag |= EX_PUBLICFH;
3386 }
3387 }
3388 skip:
3389 if (ai != NULL)
3390 ai = ai->ai_next;
3391 if (ai == NULL)
3392 done = TRUE;
3393 }
3394 if (cp)
3395 *cp = savedc;
3396 error_exit:
3397 /* free strings allocated by strdup() in getmntopts.c */
3398 if (iov != NULL) {
3399 free(iov[0].iov_base); /* fstype */
3400 free(iov[2].iov_base); /* fspath */
3401 free(iov[4].iov_base); /* from */
3402 free(iov[6].iov_base); /* update */
3403 free(iov[8].iov_base); /* export */
3404 free(iov[10].iov_base); /* errmsg */
3405
3406 /* free iov, allocated by realloc() */
3407 free(iov);
3408 }
3409 return (ret);
3410 }
3411
3412 /*
3413 * Translate a net address.
3414 *
3415 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
3416 */
3417 static int
get_net(char * cp,struct netmsk * net,int maskflg)3418 get_net(char *cp, struct netmsk *net, int maskflg)
3419 {
3420 struct netent *np = NULL;
3421 char *name, *p, *prefp;
3422 struct sockaddr_in sin;
3423 struct sockaddr *sa = NULL;
3424 struct addrinfo hints, *ai = NULL;
3425 char netname[NI_MAXHOST];
3426 long preflen;
3427
3428 p = prefp = NULL;
3429 if ((opt_flags & OP_MASKLEN) && !maskflg) {
3430 p = strchr(cp, '/');
3431 *p = '\0';
3432 prefp = p + 1;
3433 }
3434
3435 /*
3436 * Check for a numeric address first. We wish to avoid
3437 * possible DNS lookups in getnetbyname().
3438 */
3439 if (isxdigit(*cp) || *cp == ':') {
3440 memset(&hints, 0, sizeof hints);
3441 /* Ensure the mask and the network have the same family. */
3442 if (maskflg && (opt_flags & OP_NET))
3443 hints.ai_family = net->nt_net.ss_family;
3444 else if (!maskflg && (opt_flags & OP_HAVEMASK))
3445 hints.ai_family = net->nt_mask.ss_family;
3446 else
3447 hints.ai_family = AF_UNSPEC;
3448 hints.ai_flags = AI_NUMERICHOST;
3449 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
3450 sa = ai->ai_addr;
3451 if (sa != NULL && ai->ai_family == AF_INET) {
3452 /*
3453 * The address in `cp' is really a network address, so
3454 * use inet_network() to re-interpret this correctly.
3455 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
3456 */
3457 bzero(&sin, sizeof sin);
3458 sin.sin_family = AF_INET;
3459 sin.sin_len = sizeof sin;
3460 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
3461 if (debug)
3462 fprintf(stderr, "get_net: v4 addr %s\n",
3463 inet_ntoa(sin.sin_addr));
3464 sa = (struct sockaddr *)&sin;
3465 }
3466 }
3467 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
3468 bzero(&sin, sizeof sin);
3469 sin.sin_family = AF_INET;
3470 sin.sin_len = sizeof sin;
3471 sin.sin_addr = inet_makeaddr(np->n_net, 0);
3472 sa = (struct sockaddr *)&sin;
3473 }
3474 if (sa == NULL)
3475 goto fail;
3476
3477 if (maskflg) {
3478 /* The specified sockaddr is a mask. */
3479 if (checkmask(sa) != 0)
3480 goto fail;
3481 bcopy(sa, &net->nt_mask, sa->sa_len);
3482 opt_flags |= OP_HAVEMASK;
3483 opt_flags &= ~OP_CLASSMASK;
3484 } else {
3485 /* The specified sockaddr is a network address. */
3486 bcopy(sa, &net->nt_net, sa->sa_len);
3487
3488 /* Get a network name for the export list. */
3489 if (np) {
3490 name = np->n_name;
3491 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
3492 NULL, 0, NI_NUMERICHOST) == 0) {
3493 name = netname;
3494 } else {
3495 goto fail;
3496 }
3497 if ((net->nt_name = strdup(name)) == NULL)
3498 out_of_mem();
3499
3500 /*
3501 * Extract a mask from either a "/<masklen>" suffix, or
3502 * from the class of an IPv4 address.
3503 */
3504 if (opt_flags & OP_MASKLEN) {
3505 preflen = strtol(prefp, NULL, 10);
3506 if (preflen < 0L || preflen == LONG_MAX)
3507 goto fail;
3508 bcopy(sa, &net->nt_mask, sa->sa_len);
3509 if (makemask(&net->nt_mask, (int)preflen) != 0)
3510 goto fail;
3511 opt_flags |= OP_HAVEMASK;
3512 *p = '/';
3513 } else if (sa->sa_family == AF_INET &&
3514 (opt_flags & OP_MASK) == 0) {
3515 in_addr_t addr;
3516
3517 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
3518 if (IN_CLASSA(addr))
3519 preflen = 8;
3520 else if (IN_CLASSB(addr))
3521 preflen = 16;
3522 else if (IN_CLASSC(addr))
3523 preflen = 24;
3524 else if (IN_CLASSD(addr)) /* XXX Multicast??? */
3525 preflen = 28;
3526 else
3527 preflen = 32; /* XXX */
3528
3529 bcopy(sa, &net->nt_mask, sa->sa_len);
3530 makemask(&net->nt_mask, (int)preflen);
3531 opt_flags |= OP_HAVEMASK | OP_CLASSMASK;
3532 }
3533 }
3534
3535 if (ai)
3536 freeaddrinfo(ai);
3537 return 0;
3538
3539 fail:
3540 if (ai)
3541 freeaddrinfo(ai);
3542 return 1;
3543 }
3544
3545 /*
3546 * Parse out the next white space separated field
3547 */
3548 static void
nextfield(char ** cp,char ** endcp)3549 nextfield(char **cp, char **endcp)
3550 {
3551 char *p;
3552 char quot = 0;
3553
3554 p = *cp;
3555 while (*p == ' ' || *p == '\t')
3556 p++;
3557 *cp = p;
3558 while (*p != '\0') {
3559 if (quot) {
3560 if (*p == quot)
3561 quot = 0;
3562 } else {
3563 if (*p == '\\' && *(p + 1) != '\0')
3564 p++;
3565 else if (*p == '\'' || *p == '"')
3566 quot = *p;
3567 else if (*p == ' ' || *p == '\t')
3568 break;
3569 }
3570 p++;
3571 };
3572 *endcp = p;
3573 }
3574
3575 /*
3576 * Get an exports file line. Skip over blank lines and handle line
3577 * continuations.
3578 */
3579 static int
get_line(void)3580 get_line(void)
3581 {
3582 char *p, *cp;
3583 size_t len;
3584 int totlen, cont_line;
3585
3586 /*
3587 * Loop around ignoring blank lines and getting all continuation lines.
3588 */
3589 p = line;
3590 totlen = 0;
3591 do {
3592 if ((p = fgetln(exp_file, &len)) == NULL)
3593 return (0);
3594 cp = p + len - 1;
3595 cont_line = 0;
3596 while (cp >= p &&
3597 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
3598 if (*cp == '\\')
3599 cont_line = 1;
3600 cp--;
3601 len--;
3602 }
3603 if (cont_line) {
3604 *++cp = ' ';
3605 len++;
3606 }
3607 if (linesize < len + totlen + 1) {
3608 linesize = len + totlen + 1;
3609 line = realloc(line, linesize);
3610 if (line == NULL)
3611 out_of_mem();
3612 }
3613 memcpy(line + totlen, p, len);
3614 totlen += len;
3615 line[totlen] = '\0';
3616 } while (totlen == 0 || cont_line);
3617 return (1);
3618 }
3619
3620 /*
3621 * Parse a description of a credential.
3622 */
3623 static void
parsecred(char * names,struct expcred * cr)3624 parsecred(char *names, struct expcred *cr)
3625 {
3626 char *name;
3627 struct passwd *pw;
3628 unsigned long name_ul;
3629 char *end = NULL;
3630
3631 assert(cr->cr_groups == tmp_groups);
3632
3633 /*
3634 * Parse the user and if possible get its password table entry.
3635 * 'cr_uid' is filled when exiting this block.
3636 */
3637 name = strsep_quote(&names, ":");
3638 name_ul = strtoul(name, &end, 10);
3639 if (*end != '\0' || end == name)
3640 pw = getpwnam(name);
3641 else
3642 pw = getpwuid((uid_t)name_ul);
3643 if (pw != NULL) {
3644 cr->cr_uid = pw->pw_uid;
3645 } else if (*end != '\0' || end == name) {
3646 syslog(LOG_ERR, "unknown user: %s", name);
3647 cr->cr_uid = UID_NOBODY;
3648 goto nogroup;
3649 } else {
3650 cr->cr_uid = name_ul;
3651 }
3652
3653 /*
3654 * Credentials specified as those of a user (i.e., use its associated
3655 * groups as specified in the password database).
3656 */
3657 if (names == NULL) {
3658 if (pw == NULL) {
3659 syslog(LOG_ERR, "no passwd entry for user: %s, "
3660 "can't determine groups", name);
3661 goto nogroup;
3662 }
3663
3664 cr->cr_ngroups = tngroups_max;
3665 if (getgrouplist(pw->pw_name, pw->pw_gid,
3666 cr->cr_groups, &cr->cr_ngroups) != 0) {
3667 syslog(LOG_ERR, "too many groups");
3668 cr->cr_ngroups = tngroups_max;
3669 }
3670 return;
3671 }
3672
3673 /*
3674 * Explicit credentials specified as a colon separated list:
3675 * uid:gid:gid:...
3676 */
3677 cr->cr_ngroups = 0;
3678 while (names != NULL && *names != '\0') {
3679 const struct group *gr;
3680 gid_t group;
3681
3682 name = strsep_quote(&names, ":");
3683 name_ul = strtoul(name, &end, 10);
3684 if (*end != '\0' || end == name) {
3685 if ((gr = getgrnam(name)) == NULL) {
3686 syslog(LOG_ERR, "unknown group: %s", name);
3687 continue;
3688 }
3689 group = gr->gr_gid;
3690 } else {
3691 group = name_ul;
3692 }
3693 if (cr->cr_ngroups == tngroups_max) {
3694 syslog(LOG_ERR, "too many groups");
3695 break;
3696 }
3697 cr->cr_groups[cr->cr_ngroups++] = group;
3698 }
3699 if (cr->cr_ngroups == 0)
3700 goto nogroup;
3701 return;
3702
3703 nogroup:
3704 cr->cr_ngroups = 1;
3705 cr->cr_groups[0] = nogroup();
3706 }
3707
3708 #define STRSIZ (MNTNAMLEN+MNTPATHLEN+50)
3709 /*
3710 * Routines that maintain the remote mounttab
3711 */
3712 static void
get_mountlist(void)3713 get_mountlist(void)
3714 {
3715 struct mountlist *mlp;
3716 char *host, *dirp, *cp;
3717 char str[STRSIZ];
3718 FILE *mlfile;
3719
3720 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
3721 if (errno == ENOENT)
3722 return;
3723 else {
3724 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
3725 return;
3726 }
3727 }
3728 while (fgets(str, STRSIZ, mlfile) != NULL) {
3729 cp = str;
3730 host = strsep(&cp, " \t\n");
3731 dirp = strsep(&cp, " \t\n");
3732 if (host == NULL || dirp == NULL)
3733 continue;
3734 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3735 if (mlp == (struct mountlist *)NULL)
3736 out_of_mem();
3737 strncpy(mlp->ml_host, host, MNTNAMLEN);
3738 mlp->ml_host[MNTNAMLEN] = '\0';
3739 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3740 mlp->ml_dirp[MNTPATHLEN] = '\0';
3741
3742 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3743 }
3744 fclose(mlfile);
3745 }
3746
3747 static void
del_mlist(char * hostp,char * dirp)3748 del_mlist(char *hostp, char *dirp)
3749 {
3750 struct mountlist *mlp, *mlp2;
3751 FILE *mlfile;
3752 int fnd = 0;
3753
3754 SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
3755 if (!strcmp(mlp->ml_host, hostp) &&
3756 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
3757 fnd = 1;
3758 SLIST_REMOVE(&mlhead, mlp, mountlist, next);
3759 free((caddr_t)mlp);
3760 }
3761 }
3762 if (fnd) {
3763 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
3764 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
3765 return;
3766 }
3767 SLIST_FOREACH(mlp, &mlhead, next) {
3768 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3769 }
3770 fclose(mlfile);
3771 }
3772 }
3773
3774 static void
add_mlist(char * hostp,char * dirp)3775 add_mlist(char *hostp, char *dirp)
3776 {
3777 struct mountlist *mlp;
3778 FILE *mlfile;
3779
3780 SLIST_FOREACH(mlp, &mlhead, next) {
3781 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
3782 return;
3783 }
3784
3785 mlp = (struct mountlist *)malloc(sizeof (*mlp));
3786 if (mlp == (struct mountlist *)NULL)
3787 out_of_mem();
3788 strncpy(mlp->ml_host, hostp, MNTNAMLEN);
3789 mlp->ml_host[MNTNAMLEN] = '\0';
3790 strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
3791 mlp->ml_dirp[MNTPATHLEN] = '\0';
3792 SLIST_INSERT_HEAD(&mlhead, mlp, next);
3793 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
3794 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
3795 return;
3796 }
3797 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
3798 fclose(mlfile);
3799 }
3800
3801 /*
3802 * Free up a group list.
3803 */
3804 static void
free_grp(struct grouplist * grp)3805 free_grp(struct grouplist *grp)
3806 {
3807 if (grp->gr_type == GT_HOST) {
3808 if (grp->gr_ptr.gt_addrinfo != NULL)
3809 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
3810 } else if (grp->gr_type == GT_NET) {
3811 if (grp->gr_ptr.gt_net.nt_name)
3812 free(grp->gr_ptr.gt_net.nt_name);
3813 }
3814 if (grp->gr_anon.cr_groups != grp->gr_anon.cr_smallgrps)
3815 free(grp->gr_anon.cr_groups);
3816 free((caddr_t)grp);
3817 }
3818
3819 #ifdef DEBUG
3820 static void
SYSLOG(int pri,const char * fmt,...)3821 SYSLOG(int pri, const char *fmt, ...)
3822 {
3823 va_list ap;
3824
3825 va_start(ap, fmt);
3826 vfprintf(stderr, fmt, ap);
3827 va_end(ap);
3828 }
3829 #endif /* DEBUG */
3830
3831 /*
3832 * Check options for consistency.
3833 */
3834 static int
check_options(struct dirlist * dp)3835 check_options(struct dirlist *dp)
3836 {
3837
3838 if (v4root_phase == 0 && dp == NULL)
3839 return (1);
3840 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
3841 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
3842 return (1);
3843 }
3844 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
3845 syslog(LOG_ERR, "-mask requires -network");
3846 return (1);
3847 }
3848 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
3849 syslog(LOG_ERR, "-network requires mask specification");
3850 return (1);
3851 }
3852 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
3853 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
3854 return (1);
3855 }
3856 if (v4root_phase > 0 &&
3857 (opt_flags &
3858 ~(OP_SEC | OP_MASK | OP_NET | OP_HAVEMASK | OP_MASKLEN)) != 0) {
3859 syslog(LOG_ERR,"only -sec,-net,-mask options allowed on V4:");
3860 return (1);
3861 }
3862 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
3863 syslog(LOG_ERR, "-alldirs has multiple directories");
3864 return (1);
3865 }
3866 return (0);
3867 }
3868
3869 /*
3870 * Check an absolute directory path for any symbolic links. Return true
3871 */
3872 static int
check_dirpath(char * dirp)3873 check_dirpath(char *dirp)
3874 {
3875 char *cp;
3876 int ret = 1;
3877 struct stat sb;
3878
3879 cp = dirp + 1;
3880 while (*cp && ret) {
3881 if (*cp == '/') {
3882 *cp = '\0';
3883 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3884 ret = 0;
3885 *cp = '/';
3886 }
3887 cp++;
3888 }
3889 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
3890 ret = 0;
3891 return (ret);
3892 }
3893
3894 /*
3895 * Make a netmask according to the specified prefix length. The ss_family
3896 * and other non-address fields must be initialised before calling this.
3897 */
3898 static int
makemask(struct sockaddr_storage * ssp,int bitlen)3899 makemask(struct sockaddr_storage *ssp, int bitlen)
3900 {
3901 u_char *p;
3902 int bits, i, len;
3903
3904 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
3905 return (-1);
3906 if (bitlen > len * CHAR_BIT)
3907 return (-1);
3908
3909 for (i = 0; i < len; i++) {
3910 bits = MIN(CHAR_BIT, bitlen);
3911 *p++ = (u_char)~0 << (CHAR_BIT - bits);
3912 bitlen -= bits;
3913 }
3914 return 0;
3915 }
3916
3917 /*
3918 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
3919 * is acceptable (i.e. of the form 1...10....0).
3920 */
3921 static int
checkmask(struct sockaddr * sa)3922 checkmask(struct sockaddr *sa)
3923 {
3924 u_char *mask;
3925 int i, len;
3926
3927 if ((mask = sa_rawaddr(sa, &len)) == NULL)
3928 return (-1);
3929
3930 for (i = 0; i < len; i++)
3931 if (mask[i] != 0xff)
3932 break;
3933 if (i < len) {
3934 if (~mask[i] & (u_char)(~mask[i] + 1))
3935 return (-1);
3936 i++;
3937 }
3938 for (; i < len; i++)
3939 if (mask[i] != 0)
3940 return (-1);
3941 return (0);
3942 }
3943
3944 /*
3945 * Compare two sockaddrs according to a specified mask. Return zero if
3946 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
3947 * If samask is NULL, perform a full comparison.
3948 */
3949 static int
sacmp(struct sockaddr * sa1,struct sockaddr * sa2,struct sockaddr * samask)3950 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
3951 {
3952 unsigned char *p1, *p2, *mask;
3953 int len, i;
3954
3955 if (sa1->sa_family != sa2->sa_family ||
3956 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
3957 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
3958 return (1);
3959
3960 switch (sa1->sa_family) {
3961 case AF_INET6:
3962 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
3963 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
3964 return (1);
3965 break;
3966 }
3967
3968 /* Simple binary comparison if no mask specified. */
3969 if (samask == NULL)
3970 return (memcmp(p1, p2, len));
3971
3972 /* Set up the mask, and do a mask-based comparison. */
3973 if (sa1->sa_family != samask->sa_family ||
3974 (mask = sa_rawaddr(samask, NULL)) == NULL)
3975 return (1);
3976
3977 for (i = 0; i < len; i++)
3978 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
3979 return (1);
3980 return (0);
3981 }
3982
3983 /*
3984 * Return a pointer to the part of the sockaddr that contains the
3985 * raw address, and set *nbytes to its length in bytes. Returns
3986 * NULL if the address family is unknown.
3987 */
3988 static void *
sa_rawaddr(struct sockaddr * sa,int * nbytes)3989 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
3990 void *p;
3991 int len;
3992
3993 switch (sa->sa_family) {
3994 case AF_INET:
3995 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
3996 p = &((struct sockaddr_in *)sa)->sin_addr;
3997 break;
3998 case AF_INET6:
3999 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
4000 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
4001 break;
4002 default:
4003 p = NULL;
4004 len = 0;
4005 }
4006
4007 if (nbytes != NULL)
4008 *nbytes = len;
4009 return (p);
4010 }
4011
4012 static void
huphandler(int sig __unused)4013 huphandler(int sig __unused)
4014 {
4015
4016 got_sighup = 1;
4017 }
4018
4019 static void
terminate(int sig __unused)4020 terminate(int sig __unused)
4021 {
4022 free(tmp_groups);
4023 pidfile_remove(pfh);
4024 rpcb_unset(MOUNTPROG, MOUNTVERS, NULL);
4025 rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL);
4026 exit (0);
4027 }
4028
4029 static void
cp_cred(struct expcred * outcr,struct expcred * incr)4030 cp_cred(struct expcred *outcr, struct expcred *incr)
4031 {
4032
4033 outcr->cr_uid = incr->cr_uid;
4034 outcr->cr_ngroups = incr->cr_ngroups;
4035 if (outcr->cr_ngroups > SMALLNGROUPS)
4036 outcr->cr_groups = malloc(outcr->cr_ngroups * sizeof(gid_t));
4037 else
4038 outcr->cr_groups = outcr->cr_smallgrps;
4039 memcpy(outcr->cr_groups, incr->cr_groups, incr->cr_ngroups *
4040 sizeof(gid_t));
4041 }
4042
4043 static gid_t
nogroup()4044 nogroup()
4045 {
4046 static gid_t nogroup = 0; /* 0 means unset. */
4047
4048 if (nogroup == 0) {
4049 const struct group *gr = getgrnam("nogroup");
4050
4051 if (gr != NULL && gr->gr_gid != 0)
4052 nogroup = gr->gr_gid;
4053 else
4054 nogroup = GID_NOGROUP;
4055 }
4056 return (nogroup);
4057 }
4058