2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Herb Hasler and Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
47 static const char rcsid[] =
51 #include <sys/param.h>
52 #include <sys/mount.h>
53 #include <sys/fcntl.h>
55 #include <sys/syslog.h>
56 #include <sys/sysctl.h>
59 #include <rpc/pmap_clnt.h>
60 #include <rpc/pmap_prot.h>
61 #include <rpcsvc/mount.h>
62 #include <nfs/rpcv2.h>
63 #include <nfs/nfsproto.h>
64 #include <nfsserver/nfs.h>
65 #include <ufs/ufs/ufsmount.h>
66 #include <fs/msdosfs/msdosfsmount.h>
67 #include <fs/ntfs/ntfsmount.h>
68 #include <isofs/cd9660/cd9660_mount.h> /* XXX need isofs in include */
70 #include <arpa/inet.h>
83 #include "pathnames.h"
90 #define MOUNTDLOCK "/var/run/mountd.lock"
94 * Structures for keeping the mount list and export list
97 struct mountlist *ml_next;
98 char ml_host[RPCMNT_NAMELEN+1];
99 char ml_dirp[RPCMNT_PATHLEN+1];
103 struct dirlist *dp_left;
104 struct dirlist *dp_right;
106 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
107 char dp_dirp[1]; /* Actually malloc'd to size of dir */
110 #define DP_DEFSET 0x1
111 #define DP_HOSTSET 0x2
114 struct exportlist *ex_next;
115 struct dirlist *ex_dirl;
116 struct dirlist *ex_defdir;
123 #define EX_LINKED 0x1
126 struct sockaddr_storage nt_net;
127 struct sockaddr_storage nt_mask;
132 struct addrinfo *gt_addrinfo;
133 struct netmsk gt_net;
138 union grouptypes gr_ptr;
139 struct grouplist *gr_next;
145 #define GT_DEFAULT 0x3
146 #define GT_IGNORE 0x5
149 int ht_flag; /* Uses DP_xx bits */
150 struct grouplist *ht_grp;
151 struct hostlist *ht_next;
161 char *add_expdir(struct dirlist **, char *, int);
162 void add_dlist(struct dirlist **, struct dirlist *,
163 struct grouplist *, int);
164 void add_mlist(char *, char *);
165 int check_dirpath(char *);
166 int check_options(struct dirlist *);
167 int checkmask(struct sockaddr *sa);
168 int chk_host(struct dirlist *, struct sockaddr *, int *, int *);
169 void del_mlist(char *hostp, char *dirp);
170 struct dirlist *dirp_search(struct dirlist *, char *);
171 int do_mount(struct exportlist *, struct grouplist *, int,
172 struct xucred *, char *, int, struct statfs *);
173 int do_opt(char **, char **, struct exportlist *, struct grouplist *,
174 int *, int *, struct xucred *);
175 struct exportlist *ex_search(fsid_t *);
176 struct exportlist *get_exp(void);
177 void free_dir(struct dirlist *);
178 void free_exp(struct exportlist *);
179 void free_grp(struct grouplist *);
180 void free_host(struct hostlist *);
181 void get_exportlist(void);
182 int get_host(char *, struct grouplist *, struct grouplist *);
183 struct hostlist *get_ht(void);
185 void get_mountlist(void);
186 int get_net(char *, struct netmsk *, int);
187 void getexp_err(struct exportlist *, struct grouplist *);
188 struct grouplist *get_grp(void);
189 void hang_dirp(struct dirlist *, struct grouplist *,
190 struct exportlist *, int);
191 void huphandler(int sig);
192 int makemask(struct sockaddr_storage *ssp, int bitlen);
193 void mntsrv(struct svc_req *, SVCXPRT *);
194 void nextfield(char **, char **);
195 void out_of_mem(void);
196 void parsecred(char *, struct xucred *);
197 int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *);
198 void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
199 int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
200 struct sockaddr *samask);
201 int scan_tree(struct dirlist *, struct sockaddr *);
202 static void usage(void);
203 int xdr_dir(XDR *, char *);
204 int xdr_explist(XDR *, caddr_t);
205 int xdr_fhs(XDR *, caddr_t);
206 int xdr_mlist(XDR *, caddr_t);
209 struct exportlist *exphead;
210 struct mountlist *mlhead;
211 struct grouplist *grphead;
212 char exname[MAXPATHLEN];
213 struct xucred def_anon = {
221 int resvport_only = 1;
227 static int have_v6 = 1;
228 #ifdef NI_WITHSCOPEID
229 static const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID;
231 static const int ninumeric = NI_NUMERICHOST;
235 /* Bits for opt_flags above */
236 #define OP_MAPROOT 0x01
237 #define OP_MAPALL 0x02
241 #define OP_ALLDIRS 0x40
242 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
243 #define OP_MASKLEN 0x200
247 void SYSLOG(int, const char *, ...) __printflike(2, 3);
248 #define syslog SYSLOG
254 * Mountd server for NFS mount protocol as described in:
255 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
256 * The optional arguments are the exports file name
257 * default: _PATH_EXPORTS
258 * and "-n" to allow nonroot mount.
266 SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
267 struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
268 int udpsock, tcpsock, udp6sock, tcp6sock;
273 udp6conf = tcp6conf = NULL;
274 udp6sock = tcp6sock = NULL;
276 /* Check that another mountd isn't already running. */
277 if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
278 err(1, "%s", MOUNTDLOCK);
280 if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
281 errx(1, "another rpc.mountd is already running. Aborting");
282 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
287 if (modfind("nfsserver") < 0) {
288 /* Not present in kernel, try loading it */
289 if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
290 errx(1, "NFS server is not available or loadable");
293 while ((c = getopt(argc, argv, "2dlnr")) != -1)
305 debug = debug ? 0 : 1;
315 grphead = (struct grouplist *)NULL;
316 exphead = (struct exportlist *)NULL;
317 mlhead = (struct mountlist *)NULL;
319 strncpy(exname, *argv, MAXPATHLEN-1);
320 exname[MAXPATHLEN-1] = '\0';
322 strcpy(exname, _PATH_EXPORTS);
323 openlog("mountd", LOG_PID, LOG_DAEMON);
325 warnx("getting export list");
328 warnx("getting mount list");
334 signal(SIGINT, SIG_IGN);
335 signal(SIGQUIT, SIG_IGN);
337 signal(SIGHUP, huphandler);
338 signal(SIGTERM, terminate);
339 { FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
340 if (pidfile != NULL) {
341 fprintf(pidfile, "%d\n", getpid());
345 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
346 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
347 udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
348 tcpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
349 udpconf = getnetconfigent("udp");
350 tcpconf = getnetconfigent("tcp");
353 udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
354 tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
356 * We're doing host-based access checks here, so don't allow
357 * v4-in-v6 to confuse things. The kernel will disable it
358 * by default on NFS sockets too.
360 if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
361 IPV6_BINDV6ONLY, &one, sizeof one) < 0){
362 syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
365 if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
366 IPV6_BINDV6ONLY, &one, sizeof one) < 0){
367 syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
370 udp6conf = getnetconfigent("udp6");
371 tcp6conf = getnetconfigent("tcp6");
374 if (!resvport_only) {
375 if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
376 &resvport_only, sizeof(resvport_only)) != 0 &&
378 syslog(LOG_ERR, "sysctl: %m");
382 if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
383 (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
384 syslog(LOG_ERR, "can't create socket");
387 if (udpsock != -1 && udpconf != NULL) {
388 bindresvport(udpsock, NULL);
389 udptransp = svc_dg_create(udpsock, 0, 0);
390 if (udptransp != NULL) {
391 if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
393 syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service");
397 if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
399 syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service");
404 syslog(LOG_WARNING, "can't create UDP services");
407 if (tcpsock != -1 && tcpconf != NULL) {
408 bindresvport(tcpsock, NULL);
409 listen(tcpsock, SOMAXCONN);
410 tcptransp = svc_vc_create(tcpsock, 0, 0);
411 if (tcptransp != NULL) {
412 if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
414 syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service");
418 if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
420 syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service");
425 syslog(LOG_WARNING, "can't create TCP service");
428 if (have_v6 && udp6sock != -1 && udp6conf != NULL) {
429 bindresvport(udp6sock, NULL);
430 udp6transp = svc_dg_create(udp6sock, 0, 0);
431 if (udp6transp != NULL) {
432 if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
434 syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service");
438 if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
440 syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service");
445 syslog(LOG_WARNING, "can't create UDP6 service");
448 if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) {
449 bindresvport(tcp6sock, NULL);
450 listen(tcp6sock, SOMAXCONN);
451 tcp6transp = svc_vc_create(tcp6sock, 0, 0);
452 if (tcp6transp != NULL) {
453 if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
455 syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service");
459 if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
461 syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service");
466 syslog(LOG_WARNING, "can't create TCP6 service");
470 syslog(LOG_ERR, "could not create any services");
474 /* Expand svc_run() here so that we can call get_exportlist(). */
481 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
485 syslog(LOG_ERR, "mountd died: select: %m");
490 svc_getreqset(&readfds);
499 "usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
504 * The mount rpc service
507 mntsrv(rqstp, transp)
508 struct svc_req *rqstp;
511 struct exportlist *ep;
517 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
518 int lookup_failed = 1;
519 struct sockaddr *saddr;
521 char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
522 int bad = 0, defset, hostset;
523 sigset_t sighup_mask;
525 sigemptyset(&sighup_mask);
526 sigaddset(&sighup_mask, SIGHUP);
527 saddr = svc_getrpccaller(transp)->buf;
528 switch (saddr->sa_family) {
530 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
533 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
536 syslog(LOG_ERR, "request from unknown address family");
539 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
541 getnameinfo(saddr, saddr->sa_len, numerichost,
542 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
544 switch (rqstp->rq_proc) {
546 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
547 syslog(LOG_ERR, "can't send reply");
550 if (sport >= IPPORT_RESERVED && resvport_only) {
552 "mount request from %s from unprivileged port",
554 svcerr_weakauth(transp);
557 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
558 syslog(LOG_NOTICE, "undecodable mount request from %s",
560 svcerr_decode(transp);
565 * Get the real pathname and make sure it is a directory
566 * or a regular file if the -r option was specified
569 if (realpath(rpcpath, dirpath) == NULL ||
570 stat(dirpath, &stb) < 0 ||
571 (!S_ISDIR(stb.st_mode) &&
572 (dir_only || !S_ISREG(stb.st_mode))) ||
573 statfs(dirpath, &fsb) < 0) {
574 chdir("/"); /* Just in case realpath doesn't */
576 "mount request from %s for non existent path %s",
577 numerichost, dirpath);
579 warnx("stat failed on %s", dirpath);
580 bad = ENOENT; /* We will send error reply later */
583 /* Check in the exports list */
584 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
585 ep = ex_search(&fsb.f_fsid);
586 hostset = defset = 0;
587 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
588 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
589 chk_host(dp, saddr, &defset, &hostset)) ||
590 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
591 scan_tree(ep->ex_dirl, saddr) == 0))) {
593 if (!svc_sendreply(transp, xdr_long,
595 syslog(LOG_ERR, "can't send reply");
596 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
599 if (hostset & DP_HOSTSET)
600 fhr.fhr_flag = hostset;
602 fhr.fhr_flag = defset;
603 fhr.fhr_vers = rqstp->rq_vers;
604 /* Get the file handle */
605 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
606 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
608 syslog(LOG_ERR, "can't get fh for %s", dirpath);
609 if (!svc_sendreply(transp, xdr_long,
611 syslog(LOG_ERR, "can't send reply");
612 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
615 if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
616 syslog(LOG_ERR, "can't send reply");
618 add_mlist(host, dirpath);
620 add_mlist(numerichost, dirpath);
622 warnx("mount successful");
625 "mount request succeeded from %s for %s",
626 numerichost, dirpath);
630 "mount request denied from %s for %s",
631 numerichost, dirpath);
634 if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
635 syslog(LOG_ERR, "can't send reply");
636 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
639 if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
640 syslog(LOG_ERR, "can't send reply");
643 "dump request succeeded from %s",
647 if (sport >= IPPORT_RESERVED && resvport_only) {
649 "umount request from %s from unprivileged port",
651 svcerr_weakauth(transp);
654 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
655 syslog(LOG_NOTICE, "undecodable umount request from %s",
657 svcerr_decode(transp);
660 if (realpath(rpcpath, dirpath) == NULL) {
661 syslog(LOG_NOTICE, "umount request from %s "
662 "for non existent path %s",
663 numerichost, dirpath);
665 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
666 syslog(LOG_ERR, "can't send reply");
668 del_mlist(host, dirpath);
669 del_mlist(numerichost, dirpath);
672 "umount request succeeded from %s for %s",
673 numerichost, dirpath);
676 if (sport >= IPPORT_RESERVED && resvport_only) {
678 "umountall request from %s from unprivileged port",
680 svcerr_weakauth(transp);
683 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
684 syslog(LOG_ERR, "can't send reply");
686 del_mlist(host, NULL);
687 del_mlist(numerichost, NULL);
690 "umountall request succeeded from %s",
694 if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
695 syslog(LOG_ERR, "can't send reply");
698 "export request succeeded from %s",
702 svcerr_noproc(transp);
708 * Xdr conversion for a dirpath string
715 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
719 * Xdr routine to generate file handle reply
726 struct fhreturn *fhrp = (struct fhreturn *)cp;
727 u_long ok = 0, len, auth;
729 if (!xdr_long(xdrsp, &ok))
731 switch (fhrp->fhr_vers) {
733 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
736 if (!xdr_long(xdrsp, &len))
738 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
742 if (!xdr_long(xdrsp, &len))
744 return (xdr_long(xdrsp, &auth));
754 struct mountlist *mlp;
761 if (!xdr_bool(xdrsp, &true))
763 strp = &mlp->ml_host[0];
764 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
766 strp = &mlp->ml_dirp[0];
767 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
771 if (!xdr_bool(xdrsp, &false))
777 * Xdr conversion for export list
780 xdr_explist(xdrsp, cp)
784 struct exportlist *ep;
787 sigset_t sighup_mask;
789 sigemptyset(&sighup_mask);
790 sigaddset(&sighup_mask, SIGHUP);
791 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
795 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
797 if (ep->ex_defdir && putdef == 0 &&
798 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
803 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
804 if (!xdr_bool(xdrsp, &false))
808 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
813 * Called from xdr_explist() to traverse the tree and export the
817 put_exlist(dp, xdrsp, adp, putdefp)
823 struct grouplist *grp;
831 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
833 if (!xdr_bool(xdrsp, &true))
836 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
838 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
842 if ((dp->dp_flag & DP_DEFSET) == 0 &&
843 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
847 if (grp->gr_type == GT_HOST) {
848 if (!xdr_bool(xdrsp, &true))
850 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
851 if (!xdr_string(xdrsp, &strp,
854 } else if (grp->gr_type == GT_NET) {
855 if (!xdr_bool(xdrsp, &true))
857 strp = grp->gr_ptr.gt_net.nt_name;
858 if (!xdr_string(xdrsp, &strp,
863 if (gotalldir && hp == (struct hostlist *)NULL) {
869 if (!xdr_bool(xdrsp, &false))
871 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
877 #define LINESIZ 10240
882 * Get the export list
887 struct exportlist *ep, *ep2;
888 struct grouplist *grp, *tgrp;
889 struct exportlist **epp;
890 struct dirlist *dirhead;
891 struct statfs fsb, *fsp;
893 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
894 int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
900 * First, get rid of the old list
908 exphead = (struct exportlist *)NULL;
916 grphead = (struct grouplist *)NULL;
919 * And delete exports that are in the kernel for all local
921 * XXX: Should know how to handle all local exportable file systems
922 * instead of just "ufs".
924 num = getmntinfo(&fsp, MNT_NOWAIT);
925 for (i = 0; i < num; i++) {
929 struct msdosfs_args da;
933 if (!strcmp(fsp->f_fstypename, "ufs") ||
934 !strcmp(fsp->f_fstypename, "msdosfs") ||
935 !strcmp(fsp->f_fstypename, "ntfs") ||
936 !strcmp(fsp->f_fstypename, "cd9660")) {
937 targs.ua.fspec = NULL;
938 targs.ua.export.ex_flags = MNT_DELEXPORT;
939 if (mount(fsp->f_fstypename, fsp->f_mntonname,
940 fsp->f_flags | MNT_UPDATE, (caddr_t)&targs) < 0 &&
943 "can't delete exports for %s: %m",
950 * Read in the exports file and build the list, calling
951 * mount() as we go along to push the export rules into the kernel.
953 if ((exp_file = fopen(exname, "r")) == NULL) {
954 syslog(LOG_ERR, "can't open %s", exname);
957 dirhead = (struct dirlist *)NULL;
960 warnx("got line %s", line);
962 nextfield(&cp, &endcp);
971 exflags = MNT_EXPORTED;
974 ep = (struct exportlist *)NULL;
977 * Create new exports list entry
980 tgrp = grp = get_grp();
982 if (len > RPCMNT_NAMELEN) {
983 getexp_err(ep, tgrp);
987 if (ep == (struct exportlist *)NULL) {
988 getexp_err(ep, tgrp);
992 warnx("doing opt %s", cp);
994 if (do_opt(&cp, &endcp, ep, grp, &has_host,
996 getexp_err(ep, tgrp);
999 } else if (*cp == '/') {
1002 if (check_dirpath(cp) &&
1003 statfs(cp, &fsb) >= 0) {
1005 syslog(LOG_ERR, "dirs must be first");
1006 getexp_err(ep, tgrp);
1010 if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
1011 ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
1012 getexp_err(ep, tgrp);
1017 * See if this directory is already
1020 ep = ex_search(&fsb.f_fsid);
1021 if (ep == (struct exportlist *)NULL) {
1023 ep->ex_fs = fsb.f_fsid;
1024 ep->ex_fsdir = (char *)
1025 malloc(strlen(fsb.f_mntonname) + 1);
1027 strcpy(ep->ex_fsdir,
1032 warnx("making new ep fs=0x%x,0x%x",
1036 warnx("found ep fs=0x%x,0x%x",
1042 * Add dirpath to export mount point.
1044 dirp = add_expdir(&dirhead, cp, len);
1047 getexp_err(ep, tgrp);
1055 if (ep == (struct exportlist *)NULL) {
1056 getexp_err(ep, tgrp);
1061 * Get the host or netgroup.
1064 netgrp = getnetgrent(&hst, &usr, &dom);
1067 grp->gr_next = get_grp();
1073 "null hostname in netgroup %s, skipping", cp);
1074 grp->gr_type = GT_IGNORE;
1075 } else if (get_host(hst, grp, tgrp)) {
1077 "bad host %s in netgroup %s, skipping", hst, cp);
1078 grp->gr_type = GT_IGNORE;
1080 } else if (get_host(cp, grp, tgrp)) {
1081 syslog(LOG_ERR, "bad host %s, skipping", cp);
1082 grp->gr_type = GT_IGNORE;
1085 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1090 nextfield(&cp, &endcp);
1093 if (check_options(dirhead)) {
1094 getexp_err(ep, tgrp);
1098 grp->gr_type = GT_DEFAULT;
1100 warnx("adding a default entry");
1103 * Don't allow a network export coincide with a list of
1104 * host(s) on the same line.
1106 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1107 syslog(LOG_ERR, "network/host conflict");
1108 getexp_err(ep, tgrp);
1112 * If an export list was specified on this line, make sure
1113 * that we have at least one valid entry, otherwise skip it.
1117 while (grp && grp->gr_type == GT_IGNORE)
1120 getexp_err(ep, tgrp);
1126 * Loop through hosts, pushing the exports into the kernel.
1127 * After loop, tgrp points to the start of the list and
1128 * grp points to the last entry in the list.
1132 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1134 getexp_err(ep, tgrp);
1137 } while (grp->gr_next && (grp = grp->gr_next));
1140 * Success. Update the data structures.
1143 hang_dirp(dirhead, tgrp, ep, opt_flags);
1144 grp->gr_next = grphead;
1147 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1151 dirhead = (struct dirlist *)NULL;
1152 if ((ep->ex_flag & EX_LINKED) == 0) {
1157 * Insert in the list in alphabetical order.
1159 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1160 epp = &ep2->ex_next;
1166 ep->ex_flag |= EX_LINKED;
1171 dirhead = (struct dirlist *)NULL;
1178 * Allocate an export list element
1183 struct exportlist *ep;
1185 ep = (struct exportlist *)malloc(sizeof (struct exportlist));
1186 if (ep == (struct exportlist *)NULL)
1188 memset(ep, 0, sizeof(struct exportlist));
1193 * Allocate a group list element
1198 struct grouplist *gp;
1200 gp = (struct grouplist *)malloc(sizeof (struct grouplist));
1201 if (gp == (struct grouplist *)NULL)
1203 memset(gp, 0, sizeof(struct grouplist));
1208 * Clean up upon an error in get_exportlist().
1212 struct exportlist *ep;
1213 struct grouplist *grp;
1215 struct grouplist *tgrp;
1217 syslog(LOG_ERR, "bad exports list line %s", line);
1218 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1228 * Search the export list for a matching fs.
1234 struct exportlist *ep;
1238 if (ep->ex_fs.val[0] == fsid->val[0] &&
1239 ep->ex_fs.val[1] == fsid->val[1])
1247 * Add a directory path to the list.
1250 add_expdir(dpp, cp, len)
1251 struct dirlist **dpp;
1257 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1258 if (dp == (struct dirlist *)NULL)
1261 dp->dp_right = (struct dirlist *)NULL;
1263 dp->dp_hosts = (struct hostlist *)NULL;
1264 strcpy(dp->dp_dirp, cp);
1266 return (dp->dp_dirp);
1270 * Hang the dir list element off the dirpath binary tree as required
1271 * and update the entry for host.
1274 hang_dirp(dp, grp, ep, flags)
1276 struct grouplist *grp;
1277 struct exportlist *ep;
1280 struct hostlist *hp;
1281 struct dirlist *dp2;
1283 if (flags & OP_ALLDIRS) {
1288 if (grp == (struct grouplist *)NULL) {
1289 ep->ex_defdir->dp_flag |= DP_DEFSET;
1290 } else while (grp) {
1293 hp->ht_next = ep->ex_defdir->dp_hosts;
1294 ep->ex_defdir->dp_hosts = hp;
1300 * Loop through the directories adding them to the tree.
1304 add_dlist(&ep->ex_dirl, dp, grp, flags);
1311 * Traverse the binary tree either updating a node that is already there
1312 * for the new directory or adding the new node.
1315 add_dlist(dpp, newdp, grp, flags)
1316 struct dirlist **dpp;
1317 struct dirlist *newdp;
1318 struct grouplist *grp;
1322 struct hostlist *hp;
1327 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1329 add_dlist(&dp->dp_left, newdp, grp, flags);
1331 } else if (cmp < 0) {
1332 add_dlist(&dp->dp_right, newdp, grp, flags);
1335 free((caddr_t)newdp);
1338 dp->dp_left = (struct dirlist *)NULL;
1344 * Hang all of the host(s) off of the directory point.
1349 hp->ht_next = dp->dp_hosts;
1354 dp->dp_flag |= DP_DEFSET;
1359 * Search for a dirpath on the export point.
1362 dirp_search(dp, dirp)
1369 cmp = strcmp(dp->dp_dirp, dirp);
1371 return (dirp_search(dp->dp_left, dirp));
1373 return (dirp_search(dp->dp_right, dirp));
1381 * Scan for a host match in a directory tree.
1384 chk_host(dp, saddr, defsetp, hostsetp)
1386 struct sockaddr *saddr;
1390 struct hostlist *hp;
1391 struct grouplist *grp;
1392 struct addrinfo *ai;
1395 if (dp->dp_flag & DP_DEFSET)
1396 *defsetp = dp->dp_flag;
1400 switch (grp->gr_type) {
1402 ai = grp->gr_ptr.gt_addrinfo;
1403 for (; ai; ai = ai->ai_next) {
1404 if (!sacmp(ai->ai_addr, saddr, NULL)) {
1406 (hp->ht_flag | DP_HOSTSET);
1412 if (!sacmp(saddr, (struct sockaddr *)
1413 &grp->gr_ptr.gt_net.nt_net,
1415 &grp->gr_ptr.gt_net.nt_mask)) {
1416 *hostsetp = (hp->ht_flag | DP_HOSTSET);
1428 * Scan tree for a host that matches the address.
1431 scan_tree(dp, saddr)
1433 struct sockaddr *saddr;
1435 int defset, hostset;
1438 if (scan_tree(dp->dp_left, saddr))
1440 if (chk_host(dp, saddr, &defset, &hostset))
1442 if (scan_tree(dp->dp_right, saddr))
1449 * Traverse the dirlist tree and free it up.
1457 free_dir(dp->dp_left);
1458 free_dir(dp->dp_right);
1459 free_host(dp->dp_hosts);
1465 * Parse the option string and update fields.
1466 * Option arguments may either be -<option>=<value> or
1470 do_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
1471 char **cpp, **endcpp;
1472 struct exportlist *ep;
1473 struct grouplist *grp;
1478 char *cpoptarg, *cpoptend;
1479 char *cp, *endcp, *cpopt, savedc, savedc2;
1480 int allflag, usedarg;
1488 while (cpopt && *cpopt) {
1491 if ((cpoptend = strchr(cpopt, ','))) {
1493 if ((cpoptarg = strchr(cpopt, '=')))
1496 if ((cpoptarg = strchr(cpopt, '=')))
1500 nextfield(&cp, &endcp);
1502 if (endcp > cp && *cp != '-') {
1510 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1511 *exflagsp |= MNT_EXRDONLY;
1512 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1513 !(allflag = strcmp(cpopt, "mapall")) ||
1514 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1516 parsecred(cpoptarg, cr);
1518 *exflagsp |= MNT_EXPORTANON;
1519 opt_flags |= OP_MAPALL;
1521 opt_flags |= OP_MAPROOT;
1522 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1523 !strcmp(cpopt, "m"))) {
1524 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1525 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
1529 opt_flags |= OP_MASK;
1530 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1531 !strcmp(cpopt, "n"))) {
1532 if (strchr(cpoptarg, '/') != NULL) {
1534 fprintf(stderr, "setting OP_MASKLEN\n");
1535 opt_flags |= OP_MASKLEN;
1537 if (grp->gr_type != GT_NULL) {
1538 syslog(LOG_ERR, "network/host conflict");
1540 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1541 syslog(LOG_ERR, "bad net: %s", cpoptarg);
1544 grp->gr_type = GT_NET;
1547 opt_flags |= OP_NET;
1548 } else if (!strcmp(cpopt, "alldirs")) {
1549 opt_flags |= OP_ALLDIRS;
1550 } else if (!strcmp(cpopt, "public")) {
1551 *exflagsp |= MNT_EXPUBLIC;
1552 } else if (!strcmp(cpopt, "webnfs")) {
1553 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
1554 opt_flags |= OP_MAPALL;
1555 } else if (cpoptarg && !strcmp(cpopt, "index")) {
1556 ep->ex_indexfile = strdup(cpoptarg);
1558 syslog(LOG_ERR, "bad opt %s", cpopt);
1577 * Translate a character string to the corresponding list of network
1578 * addresses for a hostname.
1581 get_host(cp, grp, tgrp)
1583 struct grouplist *grp;
1584 struct grouplist *tgrp;
1586 struct grouplist *checkgrp;
1587 struct addrinfo *ai, *tai, hints;
1589 char host[NI_MAXHOST];
1591 if (grp->gr_type != GT_NULL) {
1592 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
1595 memset(&hints, 0, sizeof hints);
1596 hints.ai_flags = AI_CANONNAME;
1597 hints.ai_protocol = IPPROTO_UDP;
1598 ecode = getaddrinfo(cp, NULL, &hints, &ai);
1600 syslog(LOG_ERR,"can't get address info for host %s", cp);
1603 grp->gr_ptr.gt_addrinfo = ai;
1604 while (ai != NULL) {
1605 if (ai->ai_canonname == NULL) {
1606 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
1607 sizeof host, NULL, 0, ninumeric) != 0)
1608 strlcpy(host, "?", sizeof(host));
1609 ai->ai_canonname = strdup(host);
1610 ai->ai_flags |= AI_CANONNAME;
1613 fprintf(stderr, "got host %s\n", ai->ai_canonname);
1615 * Sanity check: make sure we don't already have an entry
1616 * for this host in the grouplist.
1618 for (checkgrp = tgrp; checkgrp != NULL;
1619 checkgrp = checkgrp->gr_next) {
1620 if (checkgrp->gr_type != GT_HOST)
1622 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
1623 tai = tai->ai_next) {
1624 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
1628 "ignoring duplicate host %s\n",
1630 grp->gr_type = GT_IGNORE;
1636 grp->gr_type = GT_HOST;
1641 * Free up an exports list component
1645 struct exportlist *ep;
1648 if (ep->ex_defdir) {
1649 free_host(ep->ex_defdir->dp_hosts);
1650 free((caddr_t)ep->ex_defdir);
1654 if (ep->ex_indexfile)
1655 free(ep->ex_indexfile);
1656 free_dir(ep->ex_dirl);
1665 struct hostlist *hp;
1667 struct hostlist *hp2;
1679 struct hostlist *hp;
1681 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
1682 if (hp == (struct hostlist *)NULL)
1684 hp->ht_next = (struct hostlist *)NULL;
1690 * Out of memory, fatal
1696 syslog(LOG_ERR, "out of memory");
1701 * Do the mount syscall with the update flag to push the export info into
1705 do_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
1706 struct exportlist *ep;
1707 struct grouplist *grp;
1709 struct xucred *anoncrp;
1715 struct addrinfo *ai;
1716 struct export_args *eap;
1723 struct msdosfs_args da;
1724 struct ntfs_args na;
1727 bzero(&args, sizeof args);
1728 /* XXX, we assume that all xx_args look like ufs_args. */
1730 eap = &args.ua.export;
1732 eap->ex_flags = exflags;
1733 eap->ex_anon = *anoncrp;
1734 eap->ex_indexfile = ep->ex_indexfile;
1735 if (grp->gr_type == GT_HOST)
1736 ai = grp->gr_ptr.gt_addrinfo;
1741 switch (grp->gr_type) {
1743 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
1745 eap->ex_addr = ai->ai_addr;
1746 eap->ex_addrlen = ai->ai_addrlen;
1747 eap->ex_masklen = 0;
1750 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
1754 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
1755 eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
1757 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
1758 eap->ex_masklen = args.ua.export.ex_mask->sa_len;
1761 eap->ex_addr = NULL;
1762 eap->ex_addrlen = 0;
1763 eap->ex_mask = NULL;
1764 eap->ex_masklen = 0;
1770 syslog(LOG_ERR, "bad grouptype");
1778 * Maybe I should just use the fsb->f_mntonname path instead
1779 * of looping back up the dirp to the mount point??
1780 * Also, needs to know how to export all types of local
1781 * exportable file systems and not just "ufs".
1783 while (mount(fsb->f_fstypename, dirp,
1784 fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
1788 cp = dirp + dirplen - 1;
1789 if (errno == EPERM) {
1791 warnx("can't change attributes for %s",
1794 "can't change attributes for %s", dirp);
1797 if (opt_flags & OP_ALLDIRS) {
1798 syslog(LOG_ERR, "could not remount %s: %m",
1802 /* back up over the last component */
1803 while (*cp == '/' && cp > dirp)
1805 while (*(cp - 1) != '/' && cp > dirp)
1809 warnx("mnt unsucc");
1810 syslog(LOG_ERR, "can't export %s", dirp);
1815 /* Check that we're still on the same filesystem. */
1816 if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
1817 &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
1819 syslog(LOG_ERR, "can't export %s", dirp);
1835 * Translate a net address.
1837 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
1840 get_net(cp, net, maskflg)
1845 struct netent *np = NULL;
1846 char *name, *p, *prefp;
1847 struct sockaddr_in sin;
1848 struct sockaddr *sa = NULL;
1849 struct addrinfo hints, *ai = NULL;
1850 char netname[NI_MAXHOST];
1854 if ((opt_flags & OP_MASKLEN) && !maskflg) {
1855 p = strchr(cp, '/');
1861 * Check for a numeric address first. We wish to avoid
1862 * possible DNS lookups in getnetbyname().
1864 if (isxdigit(*cp) || *cp == ':') {
1865 memset(&hints, 0, sizeof hints);
1866 /* Ensure the mask and the network have the same family. */
1867 if (maskflg && (opt_flags & OP_NET))
1868 hints.ai_family = net->nt_net.ss_family;
1869 else if (!maskflg && (opt_flags & OP_HAVEMASK))
1870 hints.ai_family = net->nt_mask.ss_family;
1872 hints.ai_family = AF_UNSPEC;
1873 hints.ai_flags = AI_NUMERICHOST;
1874 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
1876 if (sa != NULL && ai->ai_family == AF_INET) {
1878 * The address in `cp' is really a network address, so
1879 * use inet_network() to re-interpret this correctly.
1880 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
1882 bzero(&sin, sizeof sin);
1883 sin.sin_family = AF_INET;
1884 sin.sin_len = sizeof sin;
1885 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
1887 fprintf(stderr, "get_net: v4 addr %s\n",
1888 inet_ntoa(sin.sin_addr));
1889 sa = (struct sockaddr *)&sin;
1892 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
1893 bzero(&sin, sizeof sin);
1894 sin.sin_family = AF_INET;
1895 sin.sin_len = sizeof sin;
1896 sin.sin_addr = inet_makeaddr(np->n_net, 0);
1897 sa = (struct sockaddr *)&sin;
1903 /* The specified sockaddr is a mask. */
1904 if (checkmask(sa) != 0)
1906 bcopy(sa, &net->nt_mask, sa->sa_len);
1907 opt_flags |= OP_HAVEMASK;
1909 /* The specified sockaddr is a network address. */
1910 bcopy(sa, &net->nt_net, sa->sa_len);
1912 /* Get a network name for the export list. */
1915 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
1916 NULL, 0, ninumeric) == 0) {
1921 if ((net->nt_name = strdup(name)) == NULL)
1925 * Extract a mask from either a "/<masklen>" suffix, or
1926 * from the class of an IPv4 address.
1928 if (opt_flags & OP_MASKLEN) {
1929 preflen = strtol(prefp, NULL, 10);
1930 if (preflen < 0L || preflen == LONG_MAX)
1932 bcopy(sa, &net->nt_mask, sa->sa_len);
1933 if (makemask(&net->nt_mask, (int)preflen) != 0)
1935 opt_flags |= OP_HAVEMASK;
1937 } else if (sa->sa_family == AF_INET &&
1938 (opt_flags & OP_MASK) == 0) {
1941 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
1942 if (IN_CLASSA(addr))
1944 else if (IN_CLASSB(addr))
1946 else if (IN_CLASSC(addr))
1948 else if (IN_CLASSD(addr))
1951 preflen = 32; /* XXX */
1953 bcopy(sa, &net->nt_mask, sa->sa_len);
1954 makemask(&net->nt_mask, (int)preflen);
1955 opt_flags |= OP_HAVEMASK;
1970 * Parse out the next white space separated field
1973 nextfield(cp, endcp)
1980 while (*p == ' ' || *p == '\t')
1982 if (*p == '\n' || *p == '\0')
1986 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
1993 * Get an exports file line. Skip over blank lines and handle line
2001 int totlen, cont_line;
2004 * Loop around ignoring blank lines and getting all continuation lines.
2009 if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
2015 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2028 if (totlen >= LINESIZ) {
2029 syslog(LOG_ERR, "exports line too long");
2034 } while (totlen == 0 || cont_line);
2039 * Parse a description of a credential.
2042 parsecred(namelist, cr)
2051 int ngroups, groups[NGROUPS + 1];
2053 cr->cr_version = XUCRED_VERSION;
2055 * Set up the unprivileged user.
2058 cr->cr_groups[0] = -2;
2061 * Get the user's password table entry.
2063 names = strsep(&namelist, " \t\n");
2064 name = strsep(&names, ":");
2065 if (isdigit(*name) || *name == '-')
2066 pw = getpwuid(atoi(name));
2068 pw = getpwnam(name);
2070 * Credentials specified as those of a user.
2072 if (names == NULL) {
2074 syslog(LOG_ERR, "unknown user: %s", name);
2077 cr->cr_uid = pw->pw_uid;
2078 ngroups = NGROUPS + 1;
2079 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2080 syslog(LOG_ERR, "too many groups");
2082 * Convert from int's to gid_t's and compress out duplicate
2084 cr->cr_ngroups = ngroups - 1;
2085 cr->cr_groups[0] = groups[0];
2086 for (cnt = 2; cnt < ngroups; cnt++)
2087 cr->cr_groups[cnt - 1] = groups[cnt];
2091 * Explicit credential specified as a colon separated list:
2095 cr->cr_uid = pw->pw_uid;
2096 else if (isdigit(*name) || *name == '-')
2097 cr->cr_uid = atoi(name);
2099 syslog(LOG_ERR, "unknown user: %s", name);
2103 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2104 name = strsep(&names, ":");
2105 if (isdigit(*name) || *name == '-') {
2106 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2108 if ((gr = getgrnam(name)) == NULL) {
2109 syslog(LOG_ERR, "unknown group: %s", name);
2112 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2115 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2116 syslog(LOG_ERR, "too many groups");
2119 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2121 * Routines that maintain the remote mounttab
2126 struct mountlist *mlp, **mlpp;
2127 char *host, *dirp, *cp;
2131 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2132 if (errno == ENOENT)
2135 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2140 while (fgets(str, STRSIZ, mlfile) != NULL) {
2142 host = strsep(&cp, " \t\n");
2143 dirp = strsep(&cp, " \t\n");
2144 if (host == NULL || dirp == NULL)
2146 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2147 if (mlp == (struct mountlist *)NULL)
2149 strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2150 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2151 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2152 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2153 mlp->ml_next = (struct mountlist *)NULL;
2155 mlpp = &mlp->ml_next;
2161 del_mlist(char *hostp, char *dirp)
2163 struct mountlist *mlp, **mlpp;
2164 struct mountlist *mlp2;
2171 if (!strcmp(mlp->ml_host, hostp) &&
2172 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2175 *mlpp = mlp = mlp->ml_next;
2176 free((caddr_t)mlp2);
2178 mlpp = &mlp->ml_next;
2183 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2184 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
2189 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2197 add_mlist(hostp, dirp)
2200 struct mountlist *mlp, **mlpp;
2206 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2208 mlpp = &mlp->ml_next;
2211 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2212 if (mlp == (struct mountlist *)NULL)
2214 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2215 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2216 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2217 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2218 mlp->ml_next = (struct mountlist *)NULL;
2220 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2221 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
2224 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2229 * Free up a group list.
2233 struct grouplist *grp;
2235 if (grp->gr_type == GT_HOST) {
2236 if (grp->gr_ptr.gt_addrinfo != NULL)
2237 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2238 } else if (grp->gr_type == GT_NET) {
2239 if (grp->gr_ptr.gt_net.nt_name)
2240 free(grp->gr_ptr.gt_net.nt_name);
2247 SYSLOG(int pri, const char *fmt, ...)
2252 vfprintf(stderr, fmt, ap);
2258 * Check options for consistency.
2265 if (dp == (struct dirlist *)NULL)
2267 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
2268 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
2271 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2272 syslog(LOG_ERR, "-mask requires -network");
2275 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
2276 syslog(LOG_ERR, "-network requires mask specification");
2279 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
2280 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
2283 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2284 syslog(LOG_ERR, "-alldirs has multiple directories");
2291 * Check an absolute directory path for any symbolic links. Return true
2302 while (*cp && ret) {
2305 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2311 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2317 * Make a netmask according to the specified prefix length. The ss_family
2318 * and other non-address fields must be initialised before calling this.
2321 makemask(struct sockaddr_storage *ssp, int bitlen)
2326 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
2328 if (bitlen > len * NBBY)
2331 for (i = 0; i < len; i++) {
2332 bits = (bitlen > NBBY) ? NBBY : bitlen;
2333 *p++ = (1 << bits) - 1;
2340 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
2341 * is acceptable (i.e. of the form 1...10....0).
2344 checkmask(struct sockaddr *sa)
2349 if ((mask = sa_rawaddr(sa, &len)) == NULL)
2352 for (i = 0; i < len; i++)
2353 if (mask[i] != 0xff)
2356 if (~mask[i] & (u_char)(~mask[i] + 1))
2360 for (; i < len; i++)
2367 * Compare two sockaddrs according to a specified mask. Return zero if
2368 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
2369 * If samask is NULL, perform a full comparision.
2372 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
2374 unsigned char *p1, *p2, *mask;
2377 if (sa1->sa_family != sa2->sa_family ||
2378 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
2379 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
2382 switch (sa1->sa_family) {
2384 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
2385 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
2390 /* Simple binary comparison if no mask specified. */
2392 return (memcmp(p1, p2, len));
2394 /* Set up the mask, and do a mask-based comparison. */
2395 if (sa1->sa_family != samask->sa_family ||
2396 (mask = sa_rawaddr(samask, NULL)) == NULL)
2399 for (i = 0; i < len; i++)
2400 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
2406 * Return a pointer to the part of the sockaddr that contains the
2407 * raw address, and set *nbytes to its length in bytes. Returns
2408 * NULL if the address family is unknown.
2411 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
2415 switch (sa->sa_family) {
2417 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2418 p = &((struct sockaddr_in *)sa)->sin_addr;
2421 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2422 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
2443 close(mountdlockfd);
2445 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
2446 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);