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>
57 #include <sys/linker.h>
58 #include <sys/module.h>
61 #include <rpc/pmap_clnt.h>
62 #include <rpc/pmap_prot.h>
63 #include <rpcsvc/mount.h>
64 #include <nfs/rpcv2.h>
65 #include <nfs/nfsproto.h>
66 #include <nfsserver/nfs.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <fs/msdosfs/msdosfsmount.h>
69 #include <fs/ntfs/ntfsmount.h>
70 #include <isofs/cd9660/cd9660_mount.h> /* XXX need isofs in include */
72 #include <arpa/inet.h>
85 #include "pathnames.h"
92 #define MOUNTDLOCK "/var/run/mountd.lock"
96 * Structures for keeping the mount list and export list
99 struct mountlist *ml_next;
100 char ml_host[RPCMNT_NAMELEN+1];
101 char ml_dirp[RPCMNT_PATHLEN+1];
105 struct dirlist *dp_left;
106 struct dirlist *dp_right;
108 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
109 char dp_dirp[1]; /* Actually malloc'd to size of dir */
112 #define DP_DEFSET 0x1
113 #define DP_HOSTSET 0x2
116 struct exportlist *ex_next;
117 struct dirlist *ex_dirl;
118 struct dirlist *ex_defdir;
125 #define EX_LINKED 0x1
128 struct sockaddr_storage nt_net;
129 struct sockaddr_storage nt_mask;
134 struct addrinfo *gt_addrinfo;
135 struct netmsk gt_net;
140 union grouptypes gr_ptr;
141 struct grouplist *gr_next;
147 #define GT_DEFAULT 0x3
148 #define GT_IGNORE 0x5
151 int ht_flag; /* Uses DP_xx bits */
152 struct grouplist *ht_grp;
153 struct hostlist *ht_next;
163 char *add_expdir(struct dirlist **, char *, int);
164 void add_dlist(struct dirlist **, struct dirlist *,
165 struct grouplist *, int);
166 void add_mlist(char *, char *);
167 int check_dirpath(char *);
168 int check_options(struct dirlist *);
169 int checkmask(struct sockaddr *sa);
170 int chk_host(struct dirlist *, struct sockaddr *, int *, int *);
171 void del_mlist(char *hostp, char *dirp);
172 struct dirlist *dirp_search(struct dirlist *, char *);
173 int do_mount(struct exportlist *, struct grouplist *, int,
174 struct xucred *, char *, int, struct statfs *);
175 int do_opt(char **, char **, struct exportlist *, struct grouplist *,
176 int *, int *, struct xucred *);
177 struct exportlist *ex_search(fsid_t *);
178 struct exportlist *get_exp(void);
179 void free_dir(struct dirlist *);
180 void free_exp(struct exportlist *);
181 void free_grp(struct grouplist *);
182 void free_host(struct hostlist *);
183 void get_exportlist(void);
184 int get_host(char *, struct grouplist *, struct grouplist *);
185 struct hostlist *get_ht(void);
187 void get_mountlist(void);
188 int get_net(char *, struct netmsk *, int);
189 void getexp_err(struct exportlist *, struct grouplist *);
190 struct grouplist *get_grp(void);
191 void hang_dirp(struct dirlist *, struct grouplist *,
192 struct exportlist *, int);
193 void huphandler(int sig);
194 int makemask(struct sockaddr_storage *ssp, int bitlen);
195 void mntsrv(struct svc_req *, SVCXPRT *);
196 void nextfield(char **, char **);
197 void out_of_mem(void);
198 void parsecred(char *, struct xucred *);
199 int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *);
200 void *sa_rawaddr(struct sockaddr *sa, int *nbytes);
201 int sacmp(struct sockaddr *sa1, struct sockaddr *sa2,
202 struct sockaddr *samask);
203 int scan_tree(struct dirlist *, struct sockaddr *);
204 static void usage(void);
205 int xdr_dir(XDR *, char *);
206 int xdr_explist(XDR *, caddr_t);
207 int xdr_fhs(XDR *, caddr_t);
208 int xdr_mlist(XDR *, caddr_t);
211 struct exportlist *exphead;
212 struct mountlist *mlhead;
213 struct grouplist *grphead;
214 char exname[MAXPATHLEN];
215 struct xucred def_anon = {
223 int resvport_only = 1;
229 static int have_v6 = 1;
230 #ifdef NI_WITHSCOPEID
231 static const int ninumeric = NI_NUMERICHOST | NI_WITHSCOPEID;
233 static const int ninumeric = NI_NUMERICHOST;
237 /* Bits for opt_flags above */
238 #define OP_MAPROOT 0x01
239 #define OP_MAPALL 0x02
243 #define OP_ALLDIRS 0x40
244 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */
245 #define OP_MASKLEN 0x200
249 void SYSLOG(int, const char *, ...) __printflike(2, 3);
250 #define syslog SYSLOG
256 * Mountd server for NFS mount protocol as described in:
257 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
258 * The optional arguments are the exports file name
259 * default: _PATH_EXPORTS
260 * and "-n" to allow nonroot mount.
268 SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
269 struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
270 int udpsock, tcpsock, udp6sock, tcp6sock;
275 udp6conf = tcp6conf = NULL;
276 udp6sock = tcp6sock = NULL;
278 /* Check that another mountd isn't already running. */
279 if ((mountdlockfd = (open(MOUNTDLOCK, O_RDONLY|O_CREAT, 0444))) == -1)
280 err(1, "%s", MOUNTDLOCK);
282 if(flock(mountdlockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK)
283 errx(1, "another rpc.mountd is already running. Aborting");
284 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
289 if (modfind("nfsserver") < 0) {
290 /* Not present in kernel, try loading it */
291 if (kldload("nfsserver") < 0 || modfind("nfsserver") < 0)
292 errx(1, "NFS server is not available or loadable");
295 while ((c = getopt(argc, argv, "2dlnr")) != -1)
307 debug = debug ? 0 : 1;
317 grphead = (struct grouplist *)NULL;
318 exphead = (struct exportlist *)NULL;
319 mlhead = (struct mountlist *)NULL;
321 strncpy(exname, *argv, MAXPATHLEN-1);
322 exname[MAXPATHLEN-1] = '\0';
324 strcpy(exname, _PATH_EXPORTS);
325 openlog("mountd", LOG_PID, LOG_DAEMON);
327 warnx("getting export list");
330 warnx("getting mount list");
336 signal(SIGINT, SIG_IGN);
337 signal(SIGQUIT, SIG_IGN);
339 signal(SIGHUP, huphandler);
340 signal(SIGTERM, terminate);
341 { FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
342 if (pidfile != NULL) {
343 fprintf(pidfile, "%d\n", getpid());
347 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
348 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
349 udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
350 tcpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
351 udpconf = getnetconfigent("udp");
352 tcpconf = getnetconfigent("tcp");
355 udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
356 tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
358 * We're doing host-based access checks here, so don't allow
359 * v4-in-v6 to confuse things. The kernel will disable it
360 * by default on NFS sockets too.
362 if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
363 IPV6_BINDV6ONLY, &one, sizeof one) < 0){
364 syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
367 if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
368 IPV6_BINDV6ONLY, &one, sizeof one) < 0){
369 syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
372 udp6conf = getnetconfigent("udp6");
373 tcp6conf = getnetconfigent("tcp6");
376 if (!resvport_only) {
377 if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL,
378 &resvport_only, sizeof(resvport_only)) != 0 &&
380 syslog(LOG_ERR, "sysctl: %m");
384 if (udpsock != -1 && udpconf != NULL) {
385 bindresvport(udpsock, NULL);
386 udptransp = svc_dg_create(udpsock, 0, 0);
387 if (udptransp != NULL) {
388 if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
390 syslog(LOG_WARNING, "can't register UDP RPCMNT_VER1 service");
394 if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
396 syslog(LOG_WARNING, "can't register UDP RPCMNT_VER3 service");
401 syslog(LOG_WARNING, "can't create UDP services");
404 if (tcpsock != -1 && tcpconf != NULL) {
405 bindresvport(tcpsock, NULL);
406 listen(tcpsock, SOMAXCONN);
407 tcptransp = svc_vc_create(tcpsock, 0, 0);
408 if (tcptransp != NULL) {
409 if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
411 syslog(LOG_WARNING, "can't register TCP RPCMNT_VER1 service");
415 if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
417 syslog(LOG_WARNING, "can't register TCP RPCMNT_VER3 service");
422 syslog(LOG_WARNING, "can't create TCP service");
425 if (have_v6 && udp6sock != -1 && udp6conf != NULL) {
426 bindresvport(udp6sock, NULL);
427 udp6transp = svc_dg_create(udp6sock, 0, 0);
428 if (udp6transp != NULL) {
429 if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
431 syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER1 service");
435 if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
437 syslog(LOG_WARNING, "can't register UDP6 RPCMNT_VER3 service");
442 syslog(LOG_WARNING, "can't create UDP6 service");
445 if (have_v6 && tcp6sock != -1 && tcp6conf != NULL) {
446 bindresvport(tcp6sock, NULL);
447 listen(tcp6sock, SOMAXCONN);
448 tcp6transp = svc_vc_create(tcp6sock, 0, 0);
449 if (tcp6transp != NULL) {
450 if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
452 syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER1 service");
456 if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
458 syslog(LOG_WARNING, "can't register TCP6 RPCMNT_VER3 service");
463 syslog(LOG_WARNING, "can't create TCP6 service");
467 syslog(LOG_ERR, "could not create any services");
471 /* Expand svc_run() here so that we can call get_exportlist(). */
478 switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) {
482 syslog(LOG_ERR, "mountd died: select: %m");
487 svc_getreqset(&readfds);
496 "usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
501 * The mount rpc service
504 mntsrv(rqstp, transp)
505 struct svc_req *rqstp;
508 struct exportlist *ep;
514 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
515 int lookup_failed = 1;
516 struct sockaddr *saddr;
518 char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
519 int bad = 0, defset, hostset;
520 sigset_t sighup_mask;
522 sigemptyset(&sighup_mask);
523 sigaddset(&sighup_mask, SIGHUP);
524 saddr = svc_getrpccaller(transp)->buf;
525 switch (saddr->sa_family) {
527 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
530 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
533 syslog(LOG_ERR, "request from unknown address family");
536 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
538 getnameinfo(saddr, saddr->sa_len, numerichost,
539 sizeof numerichost, NULL, 0, NI_NUMERICHOST);
541 switch (rqstp->rq_proc) {
543 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
544 syslog(LOG_ERR, "can't send reply");
547 if (sport >= IPPORT_RESERVED && resvport_only) {
549 "mount request from %s from unprivileged port",
551 svcerr_weakauth(transp);
554 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
555 syslog(LOG_NOTICE, "undecodable mount request from %s",
557 svcerr_decode(transp);
562 * Get the real pathname and make sure it is a directory
563 * or a regular file if the -r option was specified
566 if (realpath(rpcpath, dirpath) == NULL ||
567 stat(dirpath, &stb) < 0 ||
568 (!S_ISDIR(stb.st_mode) &&
569 (dir_only || !S_ISREG(stb.st_mode))) ||
570 statfs(dirpath, &fsb) < 0) {
571 chdir("/"); /* Just in case realpath doesn't */
573 "mount request from %s for non existent path %s",
574 numerichost, dirpath);
576 warnx("stat failed on %s", dirpath);
577 bad = ENOENT; /* We will send error reply later */
580 /* Check in the exports list */
581 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
582 ep = ex_search(&fsb.f_fsid);
583 hostset = defset = 0;
584 if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset) ||
585 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
586 chk_host(dp, saddr, &defset, &hostset)) ||
587 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
588 scan_tree(ep->ex_dirl, saddr) == 0))) {
590 if (!svc_sendreply(transp, xdr_long,
592 syslog(LOG_ERR, "can't send reply");
593 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
596 if (hostset & DP_HOSTSET)
597 fhr.fhr_flag = hostset;
599 fhr.fhr_flag = defset;
600 fhr.fhr_vers = rqstp->rq_vers;
601 /* Get the file handle */
602 memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
603 if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) {
605 syslog(LOG_ERR, "can't get fh for %s", dirpath);
606 if (!svc_sendreply(transp, xdr_long,
608 syslog(LOG_ERR, "can't send reply");
609 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
612 if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&fhr))
613 syslog(LOG_ERR, "can't send reply");
615 add_mlist(host, dirpath);
617 add_mlist(numerichost, dirpath);
619 warnx("mount successful");
622 "mount request succeeded from %s for %s",
623 numerichost, dirpath);
627 "mount request denied from %s for %s",
628 numerichost, dirpath);
631 if (bad && !svc_sendreply(transp, xdr_long, (caddr_t)&bad))
632 syslog(LOG_ERR, "can't send reply");
633 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
636 if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
637 syslog(LOG_ERR, "can't send reply");
640 "dump request succeeded from %s",
644 if (sport >= IPPORT_RESERVED && resvport_only) {
646 "umount request from %s from unprivileged port",
648 svcerr_weakauth(transp);
651 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
652 syslog(LOG_NOTICE, "undecodable umount request from %s",
654 svcerr_decode(transp);
657 if (realpath(rpcpath, dirpath) == NULL) {
658 syslog(LOG_NOTICE, "umount request from %s "
659 "for non existent path %s",
660 numerichost, dirpath);
662 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
663 syslog(LOG_ERR, "can't send reply");
665 del_mlist(host, dirpath);
666 del_mlist(numerichost, dirpath);
669 "umount request succeeded from %s for %s",
670 numerichost, dirpath);
673 if (sport >= IPPORT_RESERVED && resvport_only) {
675 "umountall request from %s from unprivileged port",
677 svcerr_weakauth(transp);
680 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
681 syslog(LOG_ERR, "can't send reply");
683 del_mlist(host, NULL);
684 del_mlist(numerichost, NULL);
687 "umountall request succeeded from %s",
691 if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
692 syslog(LOG_ERR, "can't send reply");
695 "export request succeeded from %s",
699 svcerr_noproc(transp);
705 * Xdr conversion for a dirpath string
712 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
716 * Xdr routine to generate file handle reply
723 struct fhreturn *fhrp = (struct fhreturn *)cp;
724 u_long ok = 0, len, auth;
726 if (!xdr_long(xdrsp, &ok))
728 switch (fhrp->fhr_vers) {
730 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
733 if (!xdr_long(xdrsp, &len))
735 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
739 if (!xdr_long(xdrsp, &len))
741 return (xdr_long(xdrsp, &auth));
751 struct mountlist *mlp;
758 if (!xdr_bool(xdrsp, &true))
760 strp = &mlp->ml_host[0];
761 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
763 strp = &mlp->ml_dirp[0];
764 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
768 if (!xdr_bool(xdrsp, &false))
774 * Xdr conversion for export list
777 xdr_explist(xdrsp, cp)
781 struct exportlist *ep;
784 sigset_t sighup_mask;
786 sigemptyset(&sighup_mask);
787 sigaddset(&sighup_mask, SIGHUP);
788 sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
792 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
794 if (ep->ex_defdir && putdef == 0 &&
795 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
800 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
801 if (!xdr_bool(xdrsp, &false))
805 sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
810 * Called from xdr_explist() to traverse the tree and export the
814 put_exlist(dp, xdrsp, adp, putdefp)
820 struct grouplist *grp;
828 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
830 if (!xdr_bool(xdrsp, &true))
833 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
835 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
839 if ((dp->dp_flag & DP_DEFSET) == 0 &&
840 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
844 if (grp->gr_type == GT_HOST) {
845 if (!xdr_bool(xdrsp, &true))
847 strp = grp->gr_ptr.gt_addrinfo->ai_canonname;
848 if (!xdr_string(xdrsp, &strp,
851 } else if (grp->gr_type == GT_NET) {
852 if (!xdr_bool(xdrsp, &true))
854 strp = grp->gr_ptr.gt_net.nt_name;
855 if (!xdr_string(xdrsp, &strp,
860 if (gotalldir && hp == (struct hostlist *)NULL) {
866 if (!xdr_bool(xdrsp, &false))
868 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
879 * Get the export list
884 struct exportlist *ep, *ep2;
885 struct grouplist *grp, *tgrp;
886 struct exportlist **epp;
887 struct dirlist *dirhead;
888 struct statfs fsb, *fsp;
890 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
891 int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
897 * First, get rid of the old list
905 exphead = (struct exportlist *)NULL;
913 grphead = (struct grouplist *)NULL;
916 * And delete exports that are in the kernel for all local
918 * XXX: Should know how to handle all local exportable filesystems
919 * instead of just "ufs".
921 num = getmntinfo(&fsp, MNT_NOWAIT);
922 for (i = 0; i < num; i++) {
926 struct msdosfs_args da;
930 if (!strcmp(fsp->f_fstypename, "ufs") ||
931 !strcmp(fsp->f_fstypename, "msdosfs") ||
932 !strcmp(fsp->f_fstypename, "ntfs") ||
933 !strcmp(fsp->f_fstypename, "cd9660")) {
934 targs.ua.fspec = NULL;
935 targs.ua.export.ex_flags = MNT_DELEXPORT;
936 if (mount(fsp->f_fstypename, fsp->f_mntonname,
937 fsp->f_flags | MNT_UPDATE, (caddr_t)&targs) < 0 &&
940 "can't delete exports for %s: %m",
947 * Read in the exports file and build the list, calling
948 * mount() as we go along to push the export rules into the kernel.
950 if ((exp_file = fopen(exname, "r")) == NULL) {
951 syslog(LOG_ERR, "can't open %s", exname);
954 dirhead = (struct dirlist *)NULL;
957 warnx("got line %s", line);
959 nextfield(&cp, &endcp);
968 exflags = MNT_EXPORTED;
971 ep = (struct exportlist *)NULL;
974 * Create new exports list entry
977 tgrp = grp = get_grp();
979 if (len > RPCMNT_NAMELEN) {
980 getexp_err(ep, tgrp);
984 if (ep == (struct exportlist *)NULL) {
985 getexp_err(ep, tgrp);
989 warnx("doing opt %s", cp);
991 if (do_opt(&cp, &endcp, ep, grp, &has_host,
993 getexp_err(ep, tgrp);
996 } else if (*cp == '/') {
999 if (check_dirpath(cp) &&
1000 statfs(cp, &fsb) >= 0) {
1002 syslog(LOG_ERR, "dirs must be first");
1003 getexp_err(ep, tgrp);
1007 if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
1008 ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
1009 getexp_err(ep, tgrp);
1014 * See if this directory is already
1017 ep = ex_search(&fsb.f_fsid);
1018 if (ep == (struct exportlist *)NULL) {
1020 ep->ex_fs = fsb.f_fsid;
1021 ep->ex_fsdir = (char *)
1022 malloc(strlen(fsb.f_mntonname) + 1);
1024 strcpy(ep->ex_fsdir,
1029 warnx("making new ep fs=0x%x,0x%x",
1033 warnx("found ep fs=0x%x,0x%x",
1039 * Add dirpath to export mount point.
1041 dirp = add_expdir(&dirhead, cp, len);
1044 getexp_err(ep, tgrp);
1052 if (ep == (struct exportlist *)NULL) {
1053 getexp_err(ep, tgrp);
1058 * Get the host or netgroup.
1061 netgrp = getnetgrent(&hst, &usr, &dom);
1064 grp->gr_next = get_grp();
1070 "null hostname in netgroup %s, skipping", cp);
1071 grp->gr_type = GT_IGNORE;
1072 } else if (get_host(hst, grp, tgrp)) {
1074 "bad host %s in netgroup %s, skipping", hst, cp);
1075 grp->gr_type = GT_IGNORE;
1077 } else if (get_host(cp, grp, tgrp)) {
1078 syslog(LOG_ERR, "bad host %s, skipping", cp);
1079 grp->gr_type = GT_IGNORE;
1082 } while (netgrp && getnetgrent(&hst, &usr, &dom));
1087 nextfield(&cp, &endcp);
1090 if (check_options(dirhead)) {
1091 getexp_err(ep, tgrp);
1095 grp->gr_type = GT_DEFAULT;
1097 warnx("adding a default entry");
1100 * Don't allow a network export coincide with a list of
1101 * host(s) on the same line.
1103 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1104 syslog(LOG_ERR, "network/host conflict");
1105 getexp_err(ep, tgrp);
1109 * If an export list was specified on this line, make sure
1110 * that we have at least one valid entry, otherwise skip it.
1114 while (grp && grp->gr_type == GT_IGNORE)
1117 getexp_err(ep, tgrp);
1123 * Loop through hosts, pushing the exports into the kernel.
1124 * After loop, tgrp points to the start of the list and
1125 * grp points to the last entry in the list.
1129 if (do_mount(ep, grp, exflags, &anon, dirp, dirplen,
1131 getexp_err(ep, tgrp);
1134 } while (grp->gr_next && (grp = grp->gr_next));
1137 * Success. Update the data structures.
1140 hang_dirp(dirhead, tgrp, ep, opt_flags);
1141 grp->gr_next = grphead;
1144 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
1148 dirhead = (struct dirlist *)NULL;
1149 if ((ep->ex_flag & EX_LINKED) == 0) {
1154 * Insert in the list in alphabetical order.
1156 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1157 epp = &ep2->ex_next;
1163 ep->ex_flag |= EX_LINKED;
1168 dirhead = (struct dirlist *)NULL;
1175 * Allocate an export list element
1180 struct exportlist *ep;
1182 ep = (struct exportlist *)malloc(sizeof (struct exportlist));
1183 if (ep == (struct exportlist *)NULL)
1185 memset(ep, 0, sizeof(struct exportlist));
1190 * Allocate a group list element
1195 struct grouplist *gp;
1197 gp = (struct grouplist *)malloc(sizeof (struct grouplist));
1198 if (gp == (struct grouplist *)NULL)
1200 memset(gp, 0, sizeof(struct grouplist));
1205 * Clean up upon an error in get_exportlist().
1209 struct exportlist *ep;
1210 struct grouplist *grp;
1212 struct grouplist *tgrp;
1214 syslog(LOG_ERR, "bad exports list line %s", line);
1215 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1225 * Search the export list for a matching fs.
1231 struct exportlist *ep;
1235 if (ep->ex_fs.val[0] == fsid->val[0] &&
1236 ep->ex_fs.val[1] == fsid->val[1])
1244 * Add a directory path to the list.
1247 add_expdir(dpp, cp, len)
1248 struct dirlist **dpp;
1254 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
1255 if (dp == (struct dirlist *)NULL)
1258 dp->dp_right = (struct dirlist *)NULL;
1260 dp->dp_hosts = (struct hostlist *)NULL;
1261 strcpy(dp->dp_dirp, cp);
1263 return (dp->dp_dirp);
1267 * Hang the dir list element off the dirpath binary tree as required
1268 * and update the entry for host.
1271 hang_dirp(dp, grp, ep, flags)
1273 struct grouplist *grp;
1274 struct exportlist *ep;
1277 struct hostlist *hp;
1278 struct dirlist *dp2;
1280 if (flags & OP_ALLDIRS) {
1285 if (grp == (struct grouplist *)NULL) {
1286 ep->ex_defdir->dp_flag |= DP_DEFSET;
1287 } else while (grp) {
1290 hp->ht_next = ep->ex_defdir->dp_hosts;
1291 ep->ex_defdir->dp_hosts = hp;
1297 * Loop through the directories adding them to the tree.
1301 add_dlist(&ep->ex_dirl, dp, grp, flags);
1308 * Traverse the binary tree either updating a node that is already there
1309 * for the new directory or adding the new node.
1312 add_dlist(dpp, newdp, grp, flags)
1313 struct dirlist **dpp;
1314 struct dirlist *newdp;
1315 struct grouplist *grp;
1319 struct hostlist *hp;
1324 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1326 add_dlist(&dp->dp_left, newdp, grp, flags);
1328 } else if (cmp < 0) {
1329 add_dlist(&dp->dp_right, newdp, grp, flags);
1332 free((caddr_t)newdp);
1335 dp->dp_left = (struct dirlist *)NULL;
1341 * Hang all of the host(s) off of the directory point.
1346 hp->ht_next = dp->dp_hosts;
1351 dp->dp_flag |= DP_DEFSET;
1356 * Search for a dirpath on the export point.
1359 dirp_search(dp, dirp)
1366 cmp = strcmp(dp->dp_dirp, dirp);
1368 return (dirp_search(dp->dp_left, dirp));
1370 return (dirp_search(dp->dp_right, dirp));
1378 * Scan for a host match in a directory tree.
1381 chk_host(dp, saddr, defsetp, hostsetp)
1383 struct sockaddr *saddr;
1387 struct hostlist *hp;
1388 struct grouplist *grp;
1389 struct addrinfo *ai;
1392 if (dp->dp_flag & DP_DEFSET)
1393 *defsetp = dp->dp_flag;
1397 switch (grp->gr_type) {
1399 ai = grp->gr_ptr.gt_addrinfo;
1400 for (; ai; ai = ai->ai_next) {
1401 if (!sacmp(ai->ai_addr, saddr, NULL)) {
1403 (hp->ht_flag | DP_HOSTSET);
1409 if (!sacmp(saddr, (struct sockaddr *)
1410 &grp->gr_ptr.gt_net.nt_net,
1412 &grp->gr_ptr.gt_net.nt_mask)) {
1413 *hostsetp = (hp->ht_flag | DP_HOSTSET);
1425 * Scan tree for a host that matches the address.
1428 scan_tree(dp, saddr)
1430 struct sockaddr *saddr;
1432 int defset, hostset;
1435 if (scan_tree(dp->dp_left, saddr))
1437 if (chk_host(dp, saddr, &defset, &hostset))
1439 if (scan_tree(dp->dp_right, saddr))
1446 * Traverse the dirlist tree and free it up.
1454 free_dir(dp->dp_left);
1455 free_dir(dp->dp_right);
1456 free_host(dp->dp_hosts);
1462 * Parse the option string and update fields.
1463 * Option arguments may either be -<option>=<value> or
1467 do_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
1468 char **cpp, **endcpp;
1469 struct exportlist *ep;
1470 struct grouplist *grp;
1475 char *cpoptarg, *cpoptend;
1476 char *cp, *endcp, *cpopt, savedc, savedc2;
1477 int allflag, usedarg;
1485 while (cpopt && *cpopt) {
1488 if ((cpoptend = strchr(cpopt, ','))) {
1490 if ((cpoptarg = strchr(cpopt, '=')))
1493 if ((cpoptarg = strchr(cpopt, '=')))
1497 nextfield(&cp, &endcp);
1499 if (endcp > cp && *cp != '-') {
1507 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1508 *exflagsp |= MNT_EXRDONLY;
1509 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1510 !(allflag = strcmp(cpopt, "mapall")) ||
1511 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1513 parsecred(cpoptarg, cr);
1515 *exflagsp |= MNT_EXPORTANON;
1516 opt_flags |= OP_MAPALL;
1518 opt_flags |= OP_MAPROOT;
1519 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1520 !strcmp(cpopt, "m"))) {
1521 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1522 syslog(LOG_ERR, "bad mask: %s", cpoptarg);
1526 opt_flags |= OP_MASK;
1527 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1528 !strcmp(cpopt, "n"))) {
1529 if (strchr(cpoptarg, '/') != NULL) {
1531 fprintf(stderr, "setting OP_MASKLEN\n");
1532 opt_flags |= OP_MASKLEN;
1534 if (grp->gr_type != GT_NULL) {
1535 syslog(LOG_ERR, "network/host conflict");
1537 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1538 syslog(LOG_ERR, "bad net: %s", cpoptarg);
1541 grp->gr_type = GT_NET;
1544 opt_flags |= OP_NET;
1545 } else if (!strcmp(cpopt, "alldirs")) {
1546 opt_flags |= OP_ALLDIRS;
1547 } else if (!strcmp(cpopt, "public")) {
1548 *exflagsp |= MNT_EXPUBLIC;
1549 } else if (!strcmp(cpopt, "webnfs")) {
1550 *exflagsp |= (MNT_EXPUBLIC|MNT_EXRDONLY|MNT_EXPORTANON);
1551 opt_flags |= OP_MAPALL;
1552 } else if (cpoptarg && !strcmp(cpopt, "index")) {
1553 ep->ex_indexfile = strdup(cpoptarg);
1555 syslog(LOG_ERR, "bad opt %s", cpopt);
1574 * Translate a character string to the corresponding list of network
1575 * addresses for a hostname.
1578 get_host(cp, grp, tgrp)
1580 struct grouplist *grp;
1581 struct grouplist *tgrp;
1583 struct grouplist *checkgrp;
1584 struct addrinfo *ai, *tai, hints;
1586 char host[NI_MAXHOST];
1588 if (grp->gr_type != GT_NULL) {
1589 syslog(LOG_ERR, "Bad netgroup type for ip host %s", cp);
1592 memset(&hints, 0, sizeof hints);
1593 hints.ai_flags = AI_CANONNAME;
1594 hints.ai_protocol = IPPROTO_UDP;
1595 ecode = getaddrinfo(cp, NULL, &hints, &ai);
1597 syslog(LOG_ERR,"can't get address info for host %s", cp);
1600 grp->gr_ptr.gt_addrinfo = ai;
1601 while (ai != NULL) {
1602 if (ai->ai_canonname == NULL) {
1603 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
1604 sizeof host, NULL, 0, ninumeric) != 0)
1605 strlcpy(host, "?", sizeof(host));
1606 ai->ai_canonname = strdup(host);
1607 ai->ai_flags |= AI_CANONNAME;
1610 fprintf(stderr, "got host %s\n", ai->ai_canonname);
1612 * Sanity check: make sure we don't already have an entry
1613 * for this host in the grouplist.
1615 for (checkgrp = tgrp; checkgrp != NULL;
1616 checkgrp = checkgrp->gr_next) {
1617 if (checkgrp->gr_type != GT_HOST)
1619 for (tai = checkgrp->gr_ptr.gt_addrinfo; tai != NULL;
1620 tai = tai->ai_next) {
1621 if (sacmp(tai->ai_addr, ai->ai_addr, NULL) != 0)
1625 "ignoring duplicate host %s\n",
1627 grp->gr_type = GT_IGNORE;
1633 grp->gr_type = GT_HOST;
1638 * Free up an exports list component
1642 struct exportlist *ep;
1645 if (ep->ex_defdir) {
1646 free_host(ep->ex_defdir->dp_hosts);
1647 free((caddr_t)ep->ex_defdir);
1651 if (ep->ex_indexfile)
1652 free(ep->ex_indexfile);
1653 free_dir(ep->ex_dirl);
1662 struct hostlist *hp;
1664 struct hostlist *hp2;
1676 struct hostlist *hp;
1678 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
1679 if (hp == (struct hostlist *)NULL)
1681 hp->ht_next = (struct hostlist *)NULL;
1687 * Out of memory, fatal
1693 syslog(LOG_ERR, "out of memory");
1698 * Do the mount syscall with the update flag to push the export info into
1702 do_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
1703 struct exportlist *ep;
1704 struct grouplist *grp;
1706 struct xucred *anoncrp;
1712 struct addrinfo *ai;
1713 struct export_args *eap;
1720 struct msdosfs_args da;
1721 struct ntfs_args na;
1724 bzero(&args, sizeof args);
1725 /* XXX, we assume that all xx_args look like ufs_args. */
1727 eap = &args.ua.export;
1729 eap->ex_flags = exflags;
1730 eap->ex_anon = *anoncrp;
1731 eap->ex_indexfile = ep->ex_indexfile;
1732 if (grp->gr_type == GT_HOST)
1733 ai = grp->gr_ptr.gt_addrinfo;
1738 switch (grp->gr_type) {
1740 if (ai->ai_addr->sa_family == AF_INET6 && have_v6 == 0)
1742 eap->ex_addr = ai->ai_addr;
1743 eap->ex_addrlen = ai->ai_addrlen;
1744 eap->ex_masklen = 0;
1747 if (grp->gr_ptr.gt_net.nt_net.ss_family == AF_INET6 &&
1751 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_net;
1752 eap->ex_addrlen = args.ua.export.ex_addr->sa_len;
1754 (struct sockaddr *)&grp->gr_ptr.gt_net.nt_mask;
1755 eap->ex_masklen = args.ua.export.ex_mask->sa_len;
1758 eap->ex_addr = NULL;
1759 eap->ex_addrlen = 0;
1760 eap->ex_mask = NULL;
1761 eap->ex_masklen = 0;
1767 syslog(LOG_ERR, "bad grouptype");
1775 * Maybe I should just use the fsb->f_mntonname path instead
1776 * of looping back up the dirp to the mount point??
1777 * Also, needs to know how to export all types of local
1778 * exportable filesystems and not just "ufs".
1780 while (mount(fsb->f_fstypename, dirp,
1781 fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
1785 cp = dirp + dirplen - 1;
1786 if (errno == EPERM) {
1788 warnx("can't change attributes for %s",
1791 "can't change attributes for %s", dirp);
1794 if (opt_flags & OP_ALLDIRS) {
1795 syslog(LOG_ERR, "could not remount %s: %m",
1799 /* back up over the last component */
1800 while (*cp == '/' && cp > dirp)
1802 while (*(cp - 1) != '/' && cp > dirp)
1806 warnx("mnt unsucc");
1807 syslog(LOG_ERR, "can't export %s", dirp);
1812 /* Check that we're still on the same filesystem. */
1813 if (statfs(dirp, &fsb1) != 0 || bcmp(&fsb1.f_fsid,
1814 &fsb->f_fsid, sizeof(fsb1.f_fsid)) != 0) {
1816 syslog(LOG_ERR, "can't export %s", dirp);
1832 * Translate a net address.
1834 * If `maskflg' is nonzero, then `cp' is a netmask, not a network address.
1837 get_net(cp, net, maskflg)
1842 struct netent *np = NULL;
1843 char *name, *p, *prefp;
1844 struct sockaddr_in sin;
1845 struct sockaddr *sa = NULL;
1846 struct addrinfo hints, *ai = NULL;
1847 char netname[NI_MAXHOST];
1851 if ((opt_flags & OP_MASKLEN) && !maskflg) {
1852 p = strchr(cp, '/');
1858 * Check for a numeric address first. We wish to avoid
1859 * possible DNS lookups in getnetbyname().
1861 if (isxdigit(*cp) || *cp == ':') {
1862 memset(&hints, 0, sizeof hints);
1863 /* Ensure the mask and the network have the same family. */
1864 if (maskflg && (opt_flags & OP_NET))
1865 hints.ai_family = net->nt_net.ss_family;
1866 else if (!maskflg && (opt_flags & OP_HAVEMASK))
1867 hints.ai_family = net->nt_mask.ss_family;
1869 hints.ai_family = AF_UNSPEC;
1870 hints.ai_flags = AI_NUMERICHOST;
1871 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
1873 if (sa != NULL && ai->ai_family == AF_INET) {
1875 * The address in `cp' is really a network address, so
1876 * use inet_network() to re-interpret this correctly.
1877 * e.g. "127.1" means 127.1.0.0, not 127.0.0.1.
1879 bzero(&sin, sizeof sin);
1880 sin.sin_family = AF_INET;
1881 sin.sin_len = sizeof sin;
1882 sin.sin_addr = inet_makeaddr(inet_network(cp), 0);
1884 fprintf(stderr, "get_net: v4 addr %s\n",
1885 inet_ntoa(sin.sin_addr));
1886 sa = (struct sockaddr *)&sin;
1889 if (sa == NULL && (np = getnetbyname(cp)) != NULL) {
1890 bzero(&sin, sizeof sin);
1891 sin.sin_family = AF_INET;
1892 sin.sin_len = sizeof sin;
1893 sin.sin_addr = inet_makeaddr(np->n_net, 0);
1894 sa = (struct sockaddr *)&sin;
1900 /* The specified sockaddr is a mask. */
1901 if (checkmask(sa) != 0)
1903 bcopy(sa, &net->nt_mask, sa->sa_len);
1904 opt_flags |= OP_HAVEMASK;
1906 /* The specified sockaddr is a network address. */
1907 bcopy(sa, &net->nt_net, sa->sa_len);
1909 /* Get a network name for the export list. */
1912 } else if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
1913 NULL, 0, ninumeric) == 0) {
1918 if ((net->nt_name = strdup(name)) == NULL)
1922 * Extract a mask from either a "/<masklen>" suffix, or
1923 * from the class of an IPv4 address.
1925 if (opt_flags & OP_MASKLEN) {
1926 preflen = strtol(prefp, NULL, 10);
1927 if (preflen < 0L || preflen == LONG_MAX)
1929 bcopy(sa, &net->nt_mask, sa->sa_len);
1930 if (makemask(&net->nt_mask, (int)preflen) != 0)
1932 opt_flags |= OP_HAVEMASK;
1934 } else if (sa->sa_family == AF_INET &&
1935 (opt_flags & OP_MASK) == 0) {
1938 addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
1939 if (IN_CLASSA(addr))
1941 else if (IN_CLASSB(addr))
1943 else if (IN_CLASSC(addr))
1945 else if (IN_CLASSD(addr))
1948 preflen = 32; /* XXX */
1950 bcopy(sa, &net->nt_mask, sa->sa_len);
1951 makemask(&net->nt_mask, (int)preflen);
1952 opt_flags |= OP_HAVEMASK;
1967 * Parse out the next white space separated field
1970 nextfield(cp, endcp)
1977 while (*p == ' ' || *p == '\t')
1979 if (*p == '\n' || *p == '\0')
1983 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
1990 * Get an exports file line. Skip over blank lines and handle line
1998 int totlen, cont_line;
2001 * Loop around ignoring blank lines and getting all continuation lines.
2006 if ((p = fgetln(exp_file, &len)) == NULL)
2011 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
2021 if (linesize < len + totlen + 1) {
2022 linesize = len + totlen + 1;
2023 line = realloc(line, linesize);
2027 memcpy(line + totlen, p, len);
2029 line[totlen] = '\0';
2030 } while (totlen == 0 || cont_line);
2035 * Parse a description of a credential.
2038 parsecred(namelist, cr)
2047 int ngroups, groups[NGROUPS + 1];
2049 cr->cr_version = XUCRED_VERSION;
2051 * Set up the unprivileged user.
2054 cr->cr_groups[0] = -2;
2057 * Get the user's password table entry.
2059 names = strsep(&namelist, " \t\n");
2060 name = strsep(&names, ":");
2061 if (isdigit(*name) || *name == '-')
2062 pw = getpwuid(atoi(name));
2064 pw = getpwnam(name);
2066 * Credentials specified as those of a user.
2068 if (names == NULL) {
2070 syslog(LOG_ERR, "unknown user: %s", name);
2073 cr->cr_uid = pw->pw_uid;
2074 ngroups = NGROUPS + 1;
2075 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2076 syslog(LOG_ERR, "too many groups");
2078 * Convert from int's to gid_t's and compress out duplicate
2080 cr->cr_ngroups = ngroups - 1;
2081 cr->cr_groups[0] = groups[0];
2082 for (cnt = 2; cnt < ngroups; cnt++)
2083 cr->cr_groups[cnt - 1] = groups[cnt];
2087 * Explicit credential specified as a colon separated list:
2091 cr->cr_uid = pw->pw_uid;
2092 else if (isdigit(*name) || *name == '-')
2093 cr->cr_uid = atoi(name);
2095 syslog(LOG_ERR, "unknown user: %s", name);
2099 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2100 name = strsep(&names, ":");
2101 if (isdigit(*name) || *name == '-') {
2102 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2104 if ((gr = getgrnam(name)) == NULL) {
2105 syslog(LOG_ERR, "unknown group: %s", name);
2108 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2111 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2112 syslog(LOG_ERR, "too many groups");
2115 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2117 * Routines that maintain the remote mounttab
2122 struct mountlist *mlp, **mlpp;
2123 char *host, *dirp, *cp;
2127 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2128 if (errno == ENOENT)
2131 syslog(LOG_ERR, "can't open %s", _PATH_RMOUNTLIST);
2136 while (fgets(str, STRSIZ, mlfile) != NULL) {
2138 host = strsep(&cp, " \t\n");
2139 dirp = strsep(&cp, " \t\n");
2140 if (host == NULL || dirp == NULL)
2142 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2143 if (mlp == (struct mountlist *)NULL)
2145 strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2146 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2147 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2148 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2149 mlp->ml_next = (struct mountlist *)NULL;
2151 mlpp = &mlp->ml_next;
2157 del_mlist(char *hostp, char *dirp)
2159 struct mountlist *mlp, **mlpp;
2160 struct mountlist *mlp2;
2167 if (!strcmp(mlp->ml_host, hostp) &&
2168 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2171 *mlpp = mlp = mlp->ml_next;
2172 free((caddr_t)mlp2);
2174 mlpp = &mlp->ml_next;
2179 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2180 syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
2185 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2193 add_mlist(hostp, dirp)
2196 struct mountlist *mlp, **mlpp;
2202 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2204 mlpp = &mlp->ml_next;
2207 mlp = (struct mountlist *)malloc(sizeof (*mlp));
2208 if (mlp == (struct mountlist *)NULL)
2210 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2211 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2212 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2213 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2214 mlp->ml_next = (struct mountlist *)NULL;
2216 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2217 syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
2220 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2225 * Free up a group list.
2229 struct grouplist *grp;
2231 if (grp->gr_type == GT_HOST) {
2232 if (grp->gr_ptr.gt_addrinfo != NULL)
2233 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2234 } else if (grp->gr_type == GT_NET) {
2235 if (grp->gr_ptr.gt_net.nt_name)
2236 free(grp->gr_ptr.gt_net.nt_name);
2243 SYSLOG(int pri, const char *fmt, ...)
2248 vfprintf(stderr, fmt, ap);
2254 * Check options for consistency.
2261 if (dp == (struct dirlist *)NULL)
2263 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL)) {
2264 syslog(LOG_ERR, "-mapall and -maproot mutually exclusive");
2267 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2268 syslog(LOG_ERR, "-mask requires -network");
2271 if ((opt_flags & OP_NET) && (opt_flags & OP_HAVEMASK) == 0) {
2272 syslog(LOG_ERR, "-network requires mask specification");
2275 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN)) {
2276 syslog(LOG_ERR, "-mask and /masklen are mutually exclusive");
2279 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2280 syslog(LOG_ERR, "-alldirs has multiple directories");
2287 * Check an absolute directory path for any symbolic links. Return true
2298 while (*cp && ret) {
2301 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2307 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
2313 * Make a netmask according to the specified prefix length. The ss_family
2314 * and other non-address fields must be initialised before calling this.
2317 makemask(struct sockaddr_storage *ssp, int bitlen)
2322 if ((p = sa_rawaddr((struct sockaddr *)ssp, &len)) == NULL)
2324 if (bitlen > len * NBBY)
2327 for (i = 0; i < len; i++) {
2328 bits = (bitlen > NBBY) ? NBBY : bitlen;
2329 *p++ = (1 << bits) - 1;
2336 * Check that the sockaddr is a valid netmask. Returns 0 if the mask
2337 * is acceptable (i.e. of the form 1...10....0).
2340 checkmask(struct sockaddr *sa)
2345 if ((mask = sa_rawaddr(sa, &len)) == NULL)
2348 for (i = 0; i < len; i++)
2349 if (mask[i] != 0xff)
2352 if (~mask[i] & (u_char)(~mask[i] + 1))
2356 for (; i < len; i++)
2363 * Compare two sockaddrs according to a specified mask. Return zero if
2364 * `sa1' matches `sa2' when filtered by the netmask in `samask'.
2365 * If samask is NULL, perform a full comparision.
2368 sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask)
2370 unsigned char *p1, *p2, *mask;
2373 if (sa1->sa_family != sa2->sa_family ||
2374 (p1 = sa_rawaddr(sa1, &len)) == NULL ||
2375 (p2 = sa_rawaddr(sa2, NULL)) == NULL)
2378 switch (sa1->sa_family) {
2380 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
2381 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
2386 /* Simple binary comparison if no mask specified. */
2388 return (memcmp(p1, p2, len));
2390 /* Set up the mask, and do a mask-based comparison. */
2391 if (sa1->sa_family != samask->sa_family ||
2392 (mask = sa_rawaddr(samask, NULL)) == NULL)
2395 for (i = 0; i < len; i++)
2396 if ((p1[i] & mask[i]) != (p2[i] & mask[i]))
2402 * Return a pointer to the part of the sockaddr that contains the
2403 * raw address, and set *nbytes to its length in bytes. Returns
2404 * NULL if the address family is unknown.
2407 sa_rawaddr(struct sockaddr *sa, int *nbytes) {
2411 switch (sa->sa_family) {
2413 len = sizeof(((struct sockaddr_in *)sa)->sin_addr);
2414 p = &((struct sockaddr_in *)sa)->sin_addr;
2417 len = sizeof(((struct sockaddr_in6 *)sa)->sin6_addr);
2418 p = &((struct sockaddr_in6 *)sa)->sin6_addr;
2439 close(mountdlockfd);
2441 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
2442 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);