mountd.c revision 1.95 1 /* $NetBSD: mountd.c,v 1.95 2005/06/02 06:01:09 lukem Exp $ */
2
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
36 /*
37 * XXX The ISO support can't possibly work..
38 */
39
40 #include <sys/cdefs.h>
41 #ifndef lint
42 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
44 #endif /* not lint */
45
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
49 #else
50 __RCSID("$NetBSD: mountd.c,v 1.95 2005/06/02 06:01:09 lukem Exp $");
51 #endif
52 #endif /* not lint */
53
54 #include <sys/param.h>
55 #include <sys/file.h>
56 #include <sys/ioctl.h>
57 #include <sys/mount.h>
58 #include <sys/socket.h>
59 #include <sys/stat.h>
60 #include <syslog.h>
61 #include <sys/ucred.h>
62
63 #include <rpc/rpc.h>
64 #include <rpc/pmap_clnt.h>
65 #include <rpc/pmap_prot.h>
66 #include <rpcsvc/mount.h>
67 #ifdef ISO
68 #include <netiso/iso.h>
69 #endif
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfs/nfs.h>
73 #include <nfs/nfsmount.h>
74
75 #include <ufs/ufs/ufsmount.h>
76 #include <isofs/cd9660/cd9660_mount.h>
77 #include <msdosfs/msdosfsmount.h>
78 #include <adosfs/adosfs.h>
79
80 #include <arpa/inet.h>
81
82 #include <ctype.h>
83 #include <errno.h>
84 #include <grp.h>
85 #include <netdb.h>
86 #include <pwd.h>
87 #include <netgroup.h>
88 #include <signal.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <unistd.h>
93 #include <netgroup.h>
94 #include <err.h>
95 #include <util.h>
96 #include "pathnames.h"
97 #ifdef KERBEROS
98 #include <kerberosIV/krb.h>
99 #include "kuid.h"
100 #endif
101
102 #ifdef IPSEC
103 #include <netinet6/ipsec.h>
104 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
105 #undef IPSEC
106 #endif
107 #include "ipsec.h"
108 #endif
109
110 #include <stdarg.h>
111
112 /*
113 * Structures for keeping the mount list and export list
114 */
115 struct mountlist {
116 struct mountlist *ml_next;
117 char ml_host[RPCMNT_NAMELEN + 1];
118 char ml_dirp[RPCMNT_PATHLEN + 1];
119 int ml_flag;/* XXX more flags (same as dp_flag) */
120 };
121
122 struct dirlist {
123 struct dirlist *dp_left;
124 struct dirlist *dp_right;
125 int dp_flag;
126 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
127 char dp_dirp[1]; /* Actually malloc'd to size of dir */
128 };
129 /* dp_flag bits */
130 #define DP_DEFSET 0x1
131 #define DP_HOSTSET 0x2
132 #define DP_KERB 0x4
133 #define DP_NORESMNT 0x8
134
135 struct exportlist {
136 struct exportlist *ex_next;
137 struct dirlist *ex_dirl;
138 struct dirlist *ex_defdir;
139 int ex_flag;
140 fsid_t ex_fs;
141 char *ex_fsdir;
142 char *ex_indexfile;
143 };
144 /* ex_flag bits */
145 #define EX_LINKED 0x1
146
147 struct netmsk {
148 struct sockaddr_storage nt_net;
149 int nt_len;
150 char *nt_name;
151 };
152
153 union grouptypes {
154 struct addrinfo *gt_addrinfo;
155 struct netmsk gt_net;
156 #ifdef ISO
157 struct sockaddr_iso *gt_isoaddr;
158 #endif
159 };
160
161 struct grouplist {
162 int gr_type;
163 union grouptypes gr_ptr;
164 struct grouplist *gr_next;
165 };
166 /* Group types */
167 #define GT_NULL 0x0
168 #define GT_HOST 0x1
169 #define GT_NET 0x2
170 #define GT_ISO 0x4
171
172 struct hostlist {
173 int ht_flag;/* Uses DP_xx bits */
174 struct grouplist *ht_grp;
175 struct hostlist *ht_next;
176 };
177
178 struct fhreturn {
179 int fhr_flag;
180 int fhr_vers;
181 nfsfh_t fhr_fh;
182 };
183
184 /* Global defs */
185 static char *add_expdir __P((struct dirlist **, char *, int));
186 static void add_dlist __P((struct dirlist **, struct dirlist *,
187 struct grouplist *, int));
188 static void add_mlist __P((char *, char *, int));
189 static int check_dirpath __P((const char *, size_t, char *));
190 static int check_options __P((const char *, size_t, struct dirlist *));
191 static int chk_host __P((struct dirlist *, struct sockaddr *, int *, int *));
192 static int del_mlist __P((char *, char *, struct sockaddr *));
193 static struct dirlist *dirp_search __P((struct dirlist *, char *));
194 static int do_mount __P((const char *, size_t, struct exportlist *,
195 struct grouplist *, int, struct uucred *, char *, int, struct statvfs *));
196 static int do_opt __P((const char *, size_t, char **, char **,
197 struct exportlist *, struct grouplist *, int *, int *, struct uucred *));
198 static struct exportlist *ex_search __P((fsid_t *));
199 static int parse_directory __P((const char *, size_t, struct grouplist *,
200 int, char *, struct exportlist **, struct statvfs *));
201 static int parse_host_netgroup __P((const char *, size_t, struct exportlist *,
202 struct grouplist *, char *, int *, struct grouplist **));
203 static struct exportlist *get_exp __P((void));
204 static void free_dir __P((struct dirlist *));
205 static void free_exp __P((struct exportlist *));
206 static void free_grp __P((struct grouplist *));
207 static void free_host __P((struct hostlist *));
208 static void get_exportlist __P((int));
209 static int get_host __P((const char *, size_t, const char *,
210 struct grouplist *));
211 static struct hostlist *get_ht __P((void));
212 static void get_mountlist __P((void));
213 static int get_net __P((char *, struct netmsk *, int));
214 static void free_exp_grp __P((struct exportlist *, struct grouplist *));
215 static struct grouplist *get_grp __P((void));
216 static void hang_dirp __P((struct dirlist *, struct grouplist *,
217 struct exportlist *, int));
218 static void mntsrv __P((struct svc_req *, SVCXPRT *));
219 static void nextfield __P((char **, char **));
220 static void parsecred __P((char *, struct uucred *));
221 static int put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
222 static int scan_tree __P((struct dirlist *, struct sockaddr *));
223 static void send_umntall __P((int));
224 static int umntall_each __P((caddr_t, struct sockaddr_in *));
225 static int xdr_dir __P((XDR *, char *));
226 static int xdr_explist __P((XDR *, caddr_t));
227 static int xdr_fhs __P((XDR *, caddr_t));
228 static int xdr_mlist __P((XDR *, caddr_t));
229 static void *emalloc __P((size_t));
230 static char *estrdup __P((const char *));
231 static int bitcmp __P((void *, void *, int));
232 static int netpartcmp __P((struct sockaddr *, struct sockaddr *, int));
233 static int sacmp __P((struct sockaddr *, struct sockaddr *));
234 static int allones __P((struct sockaddr_storage *, int));
235 static int countones __P((struct sockaddr *));
236 #ifdef ISO
237 static int get_isoaddr __P((const char *, size_t, char *, struct grouplist *));
238 #endif
239 static struct exportlist *exphead;
240 static struct mountlist *mlhead;
241 static struct grouplist *grphead;
242 static char *exname;
243 static struct uucred def_anon = {
244 1,
245 (uid_t) -2,
246 (gid_t) -2,
247 0,
248 {}
249 };
250
251 static int opt_flags;
252 static int have_v6 = 1;
253 static const int ninumeric = NI_NUMERICHOST;
254
255 /* Bits for above */
256 #define OP_MAPROOT 0x001
257 #define OP_MAPALL 0x002
258 #define OP_KERB 0x004
259 #define OP_MASK 0x008
260 #define OP_NET 0x010
261 #define OP_ISO 0x020
262 #define OP_ALLDIRS 0x040
263 #define OP_NORESPORT 0x080
264 #define OP_NORESMNT 0x100
265 #define OP_MASKLEN 0x200
266
267 static int debug = 0;
268 #if 0
269 static void SYSLOG __P((int, const char *,...));
270 #endif
271 int main __P((int, char *[]));
272
273 /*
274 * If this is non-zero, -noresvport and -noresvmnt are implied for
275 * each export.
276 */
277 static int noprivports;
278
279 /*
280 * Mountd server for NFS mount protocol as described in:
281 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
282 * The optional arguments are the exports file name
283 * default: _PATH_EXPORTS
284 * "-d" to enable debugging
285 * and "-n" to allow nonroot mount.
286 */
287 int
288 main(argc, argv)
289 int argc;
290 char **argv;
291 {
292 SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
293 struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
294 int udpsock, tcpsock, udp6sock, tcp6sock;
295 int xcreated = 0, s;
296 int c, one = 1;
297 int maxrec = RPC_MAXDATASIZE;
298 #ifdef IPSEC
299 char *policy = NULL;
300 #define ADDOPTS "P:"
301 #else
302 #define ADDOPTS
303 #endif
304
305 while ((c = getopt(argc, argv, "dNnr" ADDOPTS)) != -1)
306 switch (c) {
307 #ifdef IPSEC
308 case 'P':
309 if (ipsecsetup_test(policy = optarg))
310 errx(1, "Invalid ipsec policy `%s'", policy);
311 break;
312 #endif
313 case 'd':
314 debug = 1;
315 break;
316 case 'N':
317 noprivports = 1;
318 break;
319 /* Compatibility */
320 case 'n':
321 case 'r':
322 break;
323 default:
324 fprintf(stderr, "usage: %s [-dNn]"
325 #ifdef IPSEC
326 " [-P ipsec policy]"
327 #endif
328 " [export_file]\n", getprogname());
329 exit(1);
330 };
331 argc -= optind;
332 argv += optind;
333 grphead = NULL;
334 exphead = NULL;
335 mlhead = NULL;
336 if (argc == 1)
337 exname = *argv;
338 else
339 exname = _PATH_EXPORTS;
340 openlog("mountd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
341
342 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
343 if (s < 0)
344 have_v6 = 0;
345 else
346 close(s);
347
348 if (debug)
349 (void)fprintf(stderr, "Getting export list.\n");
350 get_exportlist(0);
351 if (debug)
352 (void)fprintf(stderr, "Getting mount list.\n");
353 get_mountlist();
354 if (debug)
355 (void)fprintf(stderr, "Here we go.\n");
356 if (debug == 0) {
357 daemon(0, 0);
358 (void)signal(SIGINT, SIG_IGN);
359 (void)signal(SIGQUIT, SIG_IGN);
360 }
361 (void)signal(SIGHUP, get_exportlist);
362 (void)signal(SIGTERM, send_umntall);
363 pidfile(NULL);
364
365 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
366 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
367
368 udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
369 tcpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
370 udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
371 tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
372
373 /*
374 * We're doing host-based access checks here, so don't allow
375 * v4-in-v6 to confuse things. The kernel will disable it
376 * by default on NFS sockets too.
377 */
378 if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
379 IPV6_V6ONLY, &one, sizeof one) < 0){
380 syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
381 exit(1);
382 }
383 if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
384 IPV6_V6ONLY, &one, sizeof one) < 0){
385 syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
386 exit(1);
387 }
388
389 udpconf = getnetconfigent("udp");
390 tcpconf = getnetconfigent("tcp");
391 udp6conf = getnetconfigent("udp6");
392 tcp6conf = getnetconfigent("tcp6");
393
394 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
395
396 if (udpsock != -1 && udpconf != NULL) {
397 bindresvport(udpsock, NULL);
398 #ifdef IPSEC
399 if (policy)
400 ipsecsetup(AF_INET, udpsock, policy);
401 #endif
402 udptransp = svc_dg_create(udpsock, 0, 0);
403 if (udptransp != NULL) {
404 if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
405 mntsrv, udpconf) ||
406 !svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
407 mntsrv, udpconf))
408 syslog(LOG_WARNING, "can't register UDP service");
409 else
410 xcreated++;
411 } else
412 syslog(LOG_WARNING, "can't create UDP service");
413
414 }
415
416 if (tcpsock != -1 && tcpconf != NULL) {
417 bindresvport(tcpsock, NULL);
418 #ifdef IPSEC
419 if (policy)
420 ipsecsetup(AF_INET, tcpsock, policy);
421 #endif
422 listen(tcpsock, SOMAXCONN);
423 tcptransp = svc_vc_create(tcpsock, RPC_MAXDATASIZE,
424 RPC_MAXDATASIZE);
425 if (tcptransp != NULL) {
426 if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
427 mntsrv, tcpconf) ||
428 !svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
429 mntsrv, tcpconf))
430 syslog(LOG_WARNING, "can't register TCP service");
431 else
432 xcreated++;
433 } else
434 syslog(LOG_WARNING, "can't create TCP service");
435
436 }
437
438 if (udp6sock != -1 && udp6conf != NULL) {
439 bindresvport(udp6sock, NULL);
440 #ifdef IPSEC
441 if (policy)
442 ipsecsetup(AF_INET6, tcpsock, policy);
443 #endif
444 udp6transp = svc_dg_create(udp6sock, 0, 0);
445 if (udp6transp != NULL) {
446 if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
447 mntsrv, udp6conf) ||
448 !svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
449 mntsrv, udp6conf))
450 syslog(LOG_WARNING, "can't register UDP6 service");
451 else
452 xcreated++;
453 } else
454 syslog(LOG_WARNING, "can't create UDP6 service");
455
456 }
457
458 if (tcp6sock != -1 && tcp6conf != NULL) {
459 bindresvport(tcp6sock, NULL);
460 #ifdef IPSEC
461 if (policy)
462 ipsecsetup(AF_INET6, tcpsock, policy);
463 #endif
464 listen(tcp6sock, SOMAXCONN);
465 tcp6transp = svc_vc_create(tcp6sock, RPC_MAXDATASIZE,
466 RPC_MAXDATASIZE);
467 if (tcp6transp != NULL) {
468 if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
469 mntsrv, tcp6conf) ||
470 !svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
471 mntsrv, tcp6conf))
472 syslog(LOG_WARNING, "can't register TCP6 service");
473 else
474 xcreated++;
475 } else
476 syslog(LOG_WARNING, "can't create TCP6 service");
477
478 }
479
480 if (xcreated == 0) {
481 syslog(LOG_ERR, "could not create any services");
482 exit(1);
483 }
484
485 #ifdef KERBEROS
486 kuidinit();
487 #endif
488 svc_run();
489 syslog(LOG_ERR, "Mountd died");
490 exit(1);
491 }
492
493 /*
494 * The mount rpc service
495 */
496 void
497 mntsrv(rqstp, transp)
498 struct svc_req *rqstp;
499 SVCXPRT *transp;
500 {
501 struct exportlist *ep;
502 struct dirlist *dp;
503 struct fhreturn fhr;
504 struct stat stb;
505 struct statvfs fsb;
506 struct addrinfo *ai;
507 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
508 int lookup_failed = 1;
509 struct sockaddr *saddr;
510 u_short sport;
511 char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
512 long bad = EACCES;
513 int defset, hostset, ret;
514 sigset_t sighup_mask;
515 struct sockaddr_in6 *sin6;
516 struct sockaddr_in *sin;
517
518 (void)sigemptyset(&sighup_mask);
519 (void)sigaddset(&sighup_mask, SIGHUP);
520 saddr = svc_getrpccaller(transp)->buf;
521 switch (saddr->sa_family) {
522 case AF_INET6:
523 sin6 = (struct sockaddr_in6 *)saddr;
524 sport = ntohs(sin6->sin6_port);
525 break;
526 case AF_INET:
527 sin = (struct sockaddr_in *)saddr;
528 sport = ntohs(sin->sin_port);
529 break;
530 default:
531 syslog(LOG_ERR, "request from unknown address family");
532 return;
533 }
534 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
535 NULL, 0, 0);
536 if (getnameinfo(saddr, saddr->sa_len, numerichost,
537 sizeof numerichost, NULL, 0, ninumeric) != 0)
538 strlcpy(numerichost, "?", sizeof(numerichost));
539 ai = NULL;
540 #ifdef KERBEROS
541 kuidreset();
542 #endif
543 ret = 0;
544 switch (rqstp->rq_proc) {
545 case NULLPROC:
546 if (!svc_sendreply(transp, xdr_void, NULL))
547 syslog(LOG_ERR, "Can't send reply");
548 return;
549 case MOUNTPROC_MNT:
550 if (debug)
551 fprintf(stderr,
552 "got mount request from %s\n", numerichost);
553 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
554 if (debug)
555 fprintf(stderr, "-> garbage args\n");
556 svcerr_decode(transp);
557 return;
558 }
559 if (debug)
560 fprintf(stderr,
561 "-> rpcpath: %s\n", rpcpath);
562 /*
563 * Get the real pathname and make sure it is a file or
564 * directory that exists.
565 */
566 if (realpath(rpcpath, dirpath) == 0 ||
567 stat(dirpath, &stb) < 0 ||
568 (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) ||
569 statvfs(dirpath, &fsb) < 0) {
570 (void)chdir("/"); /* Just in case realpath doesn't */
571 if (debug)
572 (void)fprintf(stderr, "-> stat failed on %s\n",
573 dirpath);
574 if (!svc_sendreply(transp, xdr_long, (caddr_t) &bad))
575 syslog(LOG_ERR, "Can't send reply");
576 return;
577 }
578 if (debug)
579 fprintf(stderr,
580 "-> dirpath: %s\n", dirpath);
581 /* Check in the exports list */
582 (void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
583 ep = ex_search(&fsb.f_fsidx);
584 hostset = defset = 0;
585 if (ep && (chk_host(ep->ex_defdir, saddr, &defset,
586 &hostset) || ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
587 chk_host(dp, saddr, &defset, &hostset)) ||
588 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
589 scan_tree(ep->ex_dirl, saddr) == 0))) {
590 if (sport >= IPPORT_RESERVED &&
591 !(hostset & DP_NORESMNT)) {
592 syslog(LOG_NOTICE,
593 "Refused mount RPC from host %s port %d",
594 numerichost, sport);
595 svcerr_weakauth(transp);
596 goto out;
597 }
598 if (hostset & DP_HOSTSET)
599 fhr.fhr_flag = hostset;
600 else
601 fhr.fhr_flag = defset;
602 fhr.fhr_vers = rqstp->rq_vers;
603 /* Get the file handle */
604 (void)memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
605 if (getfh(dirpath, (fhandle_t *) &fhr.fhr_fh) < 0) {
606 bad = errno;
607 syslog(LOG_ERR, "Can't get fh for %s", dirpath);
608 if (!svc_sendreply(transp, xdr_long,
609 (char *)&bad))
610 syslog(LOG_ERR, "Can't send reply");
611 goto out;
612 }
613 if (!svc_sendreply(transp, xdr_fhs, (char *) &fhr))
614 syslog(LOG_ERR, "Can't send reply");
615 if (!lookup_failed)
616 add_mlist(host, dirpath, hostset);
617 else
618 add_mlist(numerichost, dirpath, hostset);
619 if (debug)
620 (void)fprintf(stderr, "Mount successful.\n");
621 } else {
622 if (!svc_sendreply(transp, xdr_long, (caddr_t) &bad))
623 syslog(LOG_ERR, "Can't send reply");
624 }
625 out:
626 (void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
627 return;
628 case MOUNTPROC_DUMP:
629 if (!svc_sendreply(transp, xdr_mlist, NULL))
630 syslog(LOG_ERR, "Can't send reply");
631 return;
632 case MOUNTPROC_UMNT:
633 if (!svc_getargs(transp, xdr_dir, dirpath)) {
634 svcerr_decode(transp);
635 return;
636 }
637 if (!lookup_failed)
638 ret = del_mlist(host, dirpath, saddr);
639 ret |= del_mlist(numerichost, dirpath, saddr);
640 if (ret) {
641 svcerr_weakauth(transp);
642 return;
643 }
644 if (!svc_sendreply(transp, xdr_void, NULL))
645 syslog(LOG_ERR, "Can't send reply");
646 return;
647 case MOUNTPROC_UMNTALL:
648 if (!lookup_failed)
649 ret = del_mlist(host, NULL, saddr);
650 ret |= del_mlist(numerichost, NULL, saddr);
651 if (ret) {
652 svcerr_weakauth(transp);
653 return;
654 }
655 if (!svc_sendreply(transp, xdr_void, NULL))
656 syslog(LOG_ERR, "Can't send reply");
657 return;
658 case MOUNTPROC_EXPORT:
659 case MOUNTPROC_EXPORTALL:
660 if (!svc_sendreply(transp, xdr_explist, NULL))
661 syslog(LOG_ERR, "Can't send reply");
662 return;
663
664 #ifdef KERBEROS
665 case MOUNTPROC_KUIDMAP:
666 case MOUNTPROC_KUIDUMAP:
667 case MOUNTPROC_KUIDPURGE:
668 case MOUNTPROC_KUIDUPURGE:
669 kuidops(rqstp, transp);
670 return;
671 #endif
672
673 default:
674 svcerr_noproc(transp);
675 return;
676 }
677 }
678
679 /*
680 * Xdr conversion for a dirpath string
681 */
682 static int
683 xdr_dir(xdrsp, dirp)
684 XDR *xdrsp;
685 char *dirp;
686 {
687
688 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
689 }
690
691 /*
692 * Xdr routine to generate file handle reply
693 */
694 static int
695 xdr_fhs(xdrsp, cp)
696 XDR *xdrsp;
697 caddr_t cp;
698 {
699 struct fhreturn *fhrp = (struct fhreturn *) cp;
700 long ok = 0, len, auth;
701
702 if (!xdr_long(xdrsp, &ok))
703 return (0);
704 switch (fhrp->fhr_vers) {
705 case 1:
706 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
707 case 3:
708 len = NFSX_V3FH;
709 if (!xdr_long(xdrsp, &len))
710 return (0);
711 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
712 return (0);
713 if (fhrp->fhr_flag & DP_KERB)
714 auth = RPCAUTH_KERB4;
715 else
716 auth = RPCAUTH_UNIX;
717 len = 1;
718 if (!xdr_long(xdrsp, &len))
719 return (0);
720 return (xdr_long(xdrsp, &auth));
721 };
722 return (0);
723 }
724
725 int
726 xdr_mlist(xdrsp, cp)
727 XDR *xdrsp;
728 caddr_t cp;
729 {
730 struct mountlist *mlp;
731 int true = 1;
732 int false = 0;
733 char *strp;
734
735 mlp = mlhead;
736 while (mlp) {
737 if (!xdr_bool(xdrsp, &true))
738 return (0);
739 strp = &mlp->ml_host[0];
740 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
741 return (0);
742 strp = &mlp->ml_dirp[0];
743 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
744 return (0);
745 mlp = mlp->ml_next;
746 }
747 if (!xdr_bool(xdrsp, &false))
748 return (0);
749 return (1);
750 }
751
752 /*
753 * Xdr conversion for export list
754 */
755 int
756 xdr_explist(xdrsp, cp)
757 XDR *xdrsp;
758 caddr_t cp;
759 {
760 struct exportlist *ep;
761 int false = 0;
762 int putdef;
763 sigset_t sighup_mask;
764
765 (void)sigemptyset(&sighup_mask);
766 (void)sigaddset(&sighup_mask, SIGHUP);
767 (void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
768 ep = exphead;
769 while (ep) {
770 putdef = 0;
771 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
772 goto errout;
773 if (ep->ex_defdir && putdef == 0 &&
774 put_exlist(ep->ex_defdir, xdrsp, NULL, &putdef))
775 goto errout;
776 ep = ep->ex_next;
777 }
778 (void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
779 if (!xdr_bool(xdrsp, &false))
780 return (0);
781 return (1);
782 errout:
783 (void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
784 return (0);
785 }
786
787 /*
788 * Called from xdr_explist() to traverse the tree and export the
789 * directory paths. Assumes SIGHUP has already been masked.
790 */
791 int
792 put_exlist(dp, xdrsp, adp, putdefp)
793 struct dirlist *dp;
794 XDR *xdrsp;
795 struct dirlist *adp;
796 int *putdefp;
797 {
798 struct grouplist *grp;
799 struct hostlist *hp;
800 int true = 1;
801 int false = 0;
802 int gotalldir = 0;
803 char *strp;
804
805 if (dp) {
806 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
807 return (1);
808 if (!xdr_bool(xdrsp, &true))
809 return (1);
810 strp = dp->dp_dirp;
811 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
812 return (1);
813 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
814 gotalldir = 1;
815 *putdefp = 1;
816 }
817 if ((dp->dp_flag & DP_DEFSET) == 0 &&
818 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
819 hp = dp->dp_hosts;
820 while (hp) {
821 grp = hp->ht_grp;
822 if (grp->gr_type == GT_HOST) {
823 if (!xdr_bool(xdrsp, &true))
824 return (1);
825 strp =
826 grp->gr_ptr.gt_addrinfo->ai_canonname;
827 if (!xdr_string(xdrsp, &strp,
828 RPCMNT_NAMELEN))
829 return (1);
830 } else if (grp->gr_type == GT_NET) {
831 if (!xdr_bool(xdrsp, &true))
832 return (1);
833 strp = grp->gr_ptr.gt_net.nt_name;
834 if (!xdr_string(xdrsp, &strp,
835 RPCMNT_NAMELEN))
836 return (1);
837 }
838 hp = hp->ht_next;
839 if (gotalldir && hp == NULL) {
840 hp = adp->dp_hosts;
841 gotalldir = 0;
842 }
843 }
844 }
845 if (!xdr_bool(xdrsp, &false))
846 return (1);
847 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
848 return (1);
849 }
850 return (0);
851 }
852
853 static int
854 parse_host_netgroup(line, lineno, ep, tgrp, cp, has_host, grp)
855 const char *line;
856 size_t lineno;
857 struct exportlist *ep;
858 struct grouplist *tgrp;
859 char *cp;
860 int *has_host;
861 struct grouplist **grp;
862 {
863 const char *hst, *usr, *dom;
864 int netgrp;
865
866 if (ep == NULL) {
867 syslog(LOG_ERR, "\"%s\", line %ld: No current export",
868 line, (unsigned long)lineno);
869 return 0;
870 }
871 setnetgrent(cp);
872 netgrp = getnetgrent(&hst, &usr, &dom);
873 do {
874 if (*has_host) {
875 (*grp)->gr_next = get_grp();
876 *grp = (*grp)->gr_next;
877 }
878 if (netgrp) {
879 if (hst == NULL) {
880 syslog(LOG_ERR,
881 "\"%s\", line %ld: No host in netgroup %s",
882 line, (unsigned long)lineno, cp);
883 goto bad;
884 }
885 if (get_host(line, lineno, hst, *grp))
886 goto bad;
887 } else if (get_host(line, lineno, cp, *grp))
888 goto bad;
889 *has_host = TRUE;
890 } while (netgrp && getnetgrent(&hst, &usr, &dom));
891
892 endnetgrent();
893 return 1;
894 bad:
895 endnetgrent();
896 return 0;
897
898 }
899
900 static int
901 parse_directory(line, lineno, tgrp, got_nondir, cp, ep, fsp)
902 const char *line;
903 size_t lineno;
904 struct grouplist *tgrp;
905 int got_nondir;
906 char *cp;
907 struct exportlist **ep;
908 struct statvfs *fsp;
909 {
910 if (!check_dirpath(line, lineno, cp))
911 return 0;
912
913 if (statvfs(cp, fsp) == -1) {
914 syslog(LOG_ERR, "\"%s\", line %ld: statvfs for `%s' failed: %m",
915 line, (unsigned long)lineno, cp);
916 return 0;
917 }
918
919 if (got_nondir) {
920 syslog(LOG_ERR,
921 "\"%s\", line %ld: Directories must precede files",
922 line, (unsigned long)lineno);
923 return 0;
924 }
925 if (*ep) {
926 if ((*ep)->ex_fs.__fsid_val[0] != fsp->f_fsidx.__fsid_val[0] ||
927 (*ep)->ex_fs.__fsid_val[1] != fsp->f_fsidx.__fsid_val[1]) {
928 syslog(LOG_ERR,
929 "\"%s\", line %ld: filesystem ids disagree",
930 line, (unsigned long)lineno);
931 return 0;
932 }
933 } else {
934 /*
935 * See if this directory is already
936 * in the list.
937 */
938 *ep = ex_search(&fsp->f_fsidx);
939 if (*ep == NULL) {
940 *ep = get_exp();
941 (*ep)->ex_fs = fsp->f_fsidx;
942 (*ep)->ex_fsdir = estrdup(fsp->f_mntonname);
943 if (debug)
944 (void)fprintf(stderr,
945 "Making new ep fs=0x%x,0x%x\n",
946 fsp->f_fsidx.__fsid_val[0], fsp->f_fsidx.__fsid_val[1]);
947 } else {
948 if (debug)
949 (void)fprintf(stderr,
950 "Found ep fs=0x%x,0x%x\n",
951 fsp->f_fsidx.__fsid_val[0], fsp->f_fsidx.__fsid_val[1]);
952 }
953 }
954
955 return 1;
956 }
957
958
959 /*
960 * Get the export list
961 */
962 /* ARGSUSED */
963 void
964 get_exportlist(n)
965 int n;
966 {
967 struct exportlist *ep, *ep2;
968 struct grouplist *grp, *tgrp;
969 struct exportlist **epp;
970 struct dirlist *dirhead;
971 struct statvfs fsb, *fsp;
972 struct addrinfo *ai;
973 struct uucred anon;
974 char *cp, *endcp, *dirp, savedc;
975 int has_host, exflags, got_nondir, dirplen, num, i;
976 FILE *exp_file;
977 char *line;
978 size_t lineno = 0, len;
979
980
981 /*
982 * First, get rid of the old list
983 */
984 ep = exphead;
985 while (ep) {
986 ep2 = ep;
987 ep = ep->ex_next;
988 free_exp(ep2);
989 }
990 exphead = NULL;
991
992 dirp = NULL;
993 dirplen = 0;
994 grp = grphead;
995 while (grp) {
996 tgrp = grp;
997 grp = grp->gr_next;
998 free_grp(tgrp);
999 }
1000 grphead = NULL;
1001
1002 /*
1003 * And delete exports that are in the kernel for all local
1004 * file systems.
1005 * XXX: Should know how to handle all local exportable file systems
1006 * instead of just MOUNT_FFS.
1007 */
1008 num = getmntinfo(&fsp, MNT_NOWAIT);
1009 for (i = 0; i < num; i++) {
1010 union {
1011 struct ufs_args ua;
1012 struct iso_args ia;
1013 struct mfs_args ma;
1014 struct msdosfs_args da;
1015 struct adosfs_args aa;
1016 } targs;
1017
1018 if (!strncmp(fsp->f_fstypename, MOUNT_MFS, MFSNAMELEN) ||
1019 !strncmp(fsp->f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
1020 !strncmp(fsp->f_fstypename, MOUNT_EXT2FS, MFSNAMELEN) ||
1021 !strncmp(fsp->f_fstypename, MOUNT_LFS, MFSNAMELEN) ||
1022 !strncmp(fsp->f_fstypename, MOUNT_MSDOS, MFSNAMELEN) ||
1023 !strncmp(fsp->f_fstypename, MOUNT_ADOSFS, MFSNAMELEN) ||
1024 !strncmp(fsp->f_fstypename, MOUNT_NULL, MFSNAMELEN) ||
1025 !strncmp(fsp->f_fstypename, MOUNT_UMAP, MFSNAMELEN) ||
1026 !strncmp(fsp->f_fstypename, MOUNT_UNION, MFSNAMELEN) ||
1027 !strncmp(fsp->f_fstypename, MOUNT_CD9660, MFSNAMELEN) ||
1028 !strncmp(fsp->f_fstypename, MOUNT_NTFS, MFSNAMELEN)) {
1029 bzero((char *) &targs, sizeof(targs));
1030 targs.ua.fspec = NULL;
1031 targs.ua.export.ex_flags = MNT_DELEXPORT;
1032 if (mount(fsp->f_fstypename, fsp->f_mntonname,
1033 fsp->f_flag | MNT_UPDATE, &targs) == -1)
1034 syslog(LOG_ERR, "Can't delete exports for %s",
1035 fsp->f_mntonname);
1036 }
1037 fsp++;
1038 }
1039
1040 /*
1041 * Read in the exports file and build the list, calling
1042 * mount() as we go along to push the export rules into the kernel.
1043 */
1044 if ((exp_file = fopen(exname, "r")) == NULL) {
1045 /*
1046 * Don't exit here; we can still reload the config
1047 * after a SIGHUP.
1048 */
1049 if (debug)
1050 (void)fprintf(stderr, "Can't open %s: %s\n", exname,
1051 strerror(errno));
1052 return;
1053 }
1054 dirhead = NULL;
1055 while ((line = fparseln(exp_file, &len, &lineno, NULL, 0)) != NULL) {
1056 if (debug)
1057 (void)fprintf(stderr, "Got line %s\n", line);
1058 cp = line;
1059 nextfield(&cp, &endcp);
1060 if (cp == endcp)
1061 goto nextline; /* skip empty line */
1062 /*
1063 * Set defaults.
1064 */
1065 has_host = FALSE;
1066 anon = def_anon;
1067 exflags = MNT_EXPORTED;
1068 got_nondir = 0;
1069 opt_flags = 0;
1070 ep = NULL;
1071
1072 if (noprivports) {
1073 opt_flags |= OP_NORESMNT | OP_NORESPORT;
1074 exflags |= MNT_EXNORESPORT;
1075 }
1076
1077 /*
1078 * Create new exports list entry
1079 */
1080 len = endcp - cp;
1081 tgrp = grp = get_grp();
1082 while (len > 0) {
1083 if (len > RPCMNT_NAMELEN) {
1084 *endcp = '\0';
1085 syslog(LOG_ERR,
1086 "\"%s\", line %ld: name `%s' is too long",
1087 line, (unsigned long)lineno, cp);
1088 goto badline;
1089 }
1090 switch (*cp) {
1091 case '-':
1092 /*
1093 * Option
1094 */
1095 if (ep == NULL) {
1096 syslog(LOG_ERR,
1097 "\"%s\", line %ld: No current export list",
1098 line, (unsigned long)lineno);
1099 goto badline;
1100 }
1101 if (debug)
1102 (void)fprintf(stderr, "doing opt %s\n",
1103 cp);
1104 got_nondir = 1;
1105 if (do_opt(line, lineno, &cp, &endcp, ep, grp,
1106 &has_host, &exflags, &anon))
1107 goto badline;
1108 break;
1109
1110 case '/':
1111 /*
1112 * Directory
1113 */
1114 savedc = *endcp;
1115 *endcp = '\0';
1116
1117 if (!parse_directory(line, lineno, tgrp,
1118 got_nondir, cp, &ep, &fsb))
1119 goto badline;
1120 /*
1121 * Add dirpath to export mount point.
1122 */
1123 dirp = add_expdir(&dirhead, cp, len);
1124 dirplen = len;
1125
1126 *endcp = savedc;
1127 break;
1128
1129 default:
1130 /*
1131 * Host or netgroup.
1132 */
1133 savedc = *endcp;
1134 *endcp = '\0';
1135
1136 if (!parse_host_netgroup(line, lineno, ep,
1137 tgrp, cp, &has_host, &grp))
1138 goto badline;
1139
1140 got_nondir = 1;
1141
1142 *endcp = savedc;
1143 break;
1144 }
1145
1146 cp = endcp;
1147 nextfield(&cp, &endcp);
1148 len = endcp - cp;
1149 }
1150 if (check_options(line, lineno, dirhead))
1151 goto badline;
1152
1153 if (!has_host) {
1154 grp->gr_type = GT_HOST;
1155 if (debug)
1156 (void)fprintf(stderr,
1157 "Adding a default entry\n");
1158 /* add a default group and make the grp list NULL */
1159 ai = emalloc(sizeof(struct addrinfo));
1160 ai->ai_flags = 0;
1161 ai->ai_family = AF_INET; /* XXXX */
1162 ai->ai_socktype = SOCK_DGRAM;
1163 /* setting the length to 0 will match anything */
1164 ai->ai_addrlen = 0;
1165 ai->ai_flags = AI_CANONNAME;
1166 ai->ai_canonname = estrdup("Default");
1167 ai->ai_addr = NULL;
1168 ai->ai_next = NULL;
1169 grp->gr_ptr.gt_addrinfo = ai;
1170
1171 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1172 /*
1173 * Don't allow a network export coincide with a list of
1174 * host(s) on the same line.
1175 */
1176 syslog(LOG_ERR,
1177 "\"%s\", line %ld: Mixed exporting of networks and hosts is disallowed",
1178 line, (unsigned long)lineno);
1179 goto badline;
1180 }
1181 /*
1182 * Loop through hosts, pushing the exports into the kernel.
1183 * After loop, tgrp points to the start of the list and
1184 * grp points to the last entry in the list.
1185 */
1186 grp = tgrp;
1187 do {
1188 if (do_mount(line, lineno, ep, grp, exflags, &anon,
1189 dirp, dirplen, &fsb))
1190 goto badline;
1191 } while (grp->gr_next && (grp = grp->gr_next));
1192
1193 /*
1194 * Success. Update the data structures.
1195 */
1196 if (has_host) {
1197 hang_dirp(dirhead, tgrp, ep, opt_flags);
1198 grp->gr_next = grphead;
1199 grphead = tgrp;
1200 } else {
1201 hang_dirp(dirhead, NULL, ep, opt_flags);
1202 free_grp(grp);
1203 }
1204 dirhead = NULL;
1205 if ((ep->ex_flag & EX_LINKED) == 0) {
1206 ep2 = exphead;
1207 epp = &exphead;
1208
1209 /*
1210 * Insert in the list in alphabetical order.
1211 */
1212 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1213 epp = &ep2->ex_next;
1214 ep2 = ep2->ex_next;
1215 }
1216 if (ep2)
1217 ep->ex_next = ep2;
1218 *epp = ep;
1219 ep->ex_flag |= EX_LINKED;
1220 }
1221 goto nextline;
1222 badline:
1223 free_exp_grp(ep, grp);
1224 nextline:
1225 if (dirhead) {
1226 free_dir(dirhead);
1227 dirhead = NULL;
1228 }
1229 free(line);
1230 }
1231 (void)fclose(exp_file);
1232 }
1233
1234 /*
1235 * Allocate an export list element
1236 */
1237 static struct exportlist *
1238 get_exp()
1239 {
1240 struct exportlist *ep;
1241
1242 ep = emalloc(sizeof(struct exportlist));
1243 (void)memset(ep, 0, sizeof(struct exportlist));
1244 return (ep);
1245 }
1246
1247 /*
1248 * Allocate a group list element
1249 */
1250 static struct grouplist *
1251 get_grp()
1252 {
1253 struct grouplist *gp;
1254
1255 gp = emalloc(sizeof(struct grouplist));
1256 (void)memset(gp, 0, sizeof(struct grouplist));
1257 return (gp);
1258 }
1259
1260 /*
1261 * Clean up upon an error in get_exportlist().
1262 */
1263 static void
1264 free_exp_grp(ep, grp)
1265 struct exportlist *ep;
1266 struct grouplist *grp;
1267 {
1268 struct grouplist *tgrp;
1269
1270 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1271 free_exp(ep);
1272 while (grp) {
1273 tgrp = grp;
1274 grp = grp->gr_next;
1275 free_grp(tgrp);
1276 }
1277 }
1278
1279 /*
1280 * Search the export list for a matching fs.
1281 */
1282 static struct exportlist *
1283 ex_search(fsid)
1284 fsid_t *fsid;
1285 {
1286 struct exportlist *ep;
1287
1288 ep = exphead;
1289 while (ep) {
1290 if (ep->ex_fs.__fsid_val[0] == fsid->__fsid_val[0] &&
1291 ep->ex_fs.__fsid_val[1] == fsid->__fsid_val[1])
1292 return (ep);
1293 ep = ep->ex_next;
1294 }
1295 return (ep);
1296 }
1297
1298 /*
1299 * Add a directory path to the list.
1300 */
1301 static char *
1302 add_expdir(dpp, cp, len)
1303 struct dirlist **dpp;
1304 char *cp;
1305 int len;
1306 {
1307 struct dirlist *dp;
1308
1309 dp = emalloc(sizeof(struct dirlist) + len);
1310 dp->dp_left = *dpp;
1311 dp->dp_right = NULL;
1312 dp->dp_flag = 0;
1313 dp->dp_hosts = NULL;
1314 (void)strcpy(dp->dp_dirp, cp);
1315 *dpp = dp;
1316 return (dp->dp_dirp);
1317 }
1318
1319 /*
1320 * Hang the dir list element off the dirpath binary tree as required
1321 * and update the entry for host.
1322 */
1323 void
1324 hang_dirp(dp, grp, ep, flags)
1325 struct dirlist *dp;
1326 struct grouplist *grp;
1327 struct exportlist *ep;
1328 int flags;
1329 {
1330 struct hostlist *hp;
1331 struct dirlist *dp2;
1332
1333 if (flags & OP_ALLDIRS) {
1334 if (ep->ex_defdir)
1335 free(dp);
1336 else
1337 ep->ex_defdir = dp;
1338 if (grp == NULL) {
1339 ep->ex_defdir->dp_flag |= DP_DEFSET;
1340 if (flags & OP_KERB)
1341 ep->ex_defdir->dp_flag |= DP_KERB;
1342 if (flags & OP_NORESMNT)
1343 ep->ex_defdir->dp_flag |= DP_NORESMNT;
1344 } else
1345 while (grp) {
1346 hp = get_ht();
1347 if (flags & OP_KERB)
1348 hp->ht_flag |= DP_KERB;
1349 if (flags & OP_NORESMNT)
1350 hp->ht_flag |= DP_NORESMNT;
1351 hp->ht_grp = grp;
1352 hp->ht_next = ep->ex_defdir->dp_hosts;
1353 ep->ex_defdir->dp_hosts = hp;
1354 grp = grp->gr_next;
1355 }
1356 } else {
1357
1358 /*
1359 * Loop through the directories adding them to the tree.
1360 */
1361 while (dp) {
1362 dp2 = dp->dp_left;
1363 add_dlist(&ep->ex_dirl, dp, grp, flags);
1364 dp = dp2;
1365 }
1366 }
1367 }
1368
1369 /*
1370 * Traverse the binary tree either updating a node that is already there
1371 * for the new directory or adding the new node.
1372 */
1373 static void
1374 add_dlist(dpp, newdp, grp, flags)
1375 struct dirlist **dpp;
1376 struct dirlist *newdp;
1377 struct grouplist *grp;
1378 int flags;
1379 {
1380 struct dirlist *dp;
1381 struct hostlist *hp;
1382 int cmp;
1383
1384 dp = *dpp;
1385 if (dp) {
1386 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1387 if (cmp > 0) {
1388 add_dlist(&dp->dp_left, newdp, grp, flags);
1389 return;
1390 } else if (cmp < 0) {
1391 add_dlist(&dp->dp_right, newdp, grp, flags);
1392 return;
1393 } else
1394 free(newdp);
1395 } else {
1396 dp = newdp;
1397 dp->dp_left = NULL;
1398 *dpp = dp;
1399 }
1400 if (grp) {
1401
1402 /*
1403 * Hang all of the host(s) off of the directory point.
1404 */
1405 do {
1406 hp = get_ht();
1407 if (flags & OP_KERB)
1408 hp->ht_flag |= DP_KERB;
1409 if (flags & OP_NORESMNT)
1410 hp->ht_flag |= DP_NORESMNT;
1411 hp->ht_grp = grp;
1412 hp->ht_next = dp->dp_hosts;
1413 dp->dp_hosts = hp;
1414 grp = grp->gr_next;
1415 } while (grp);
1416 } else {
1417 dp->dp_flag |= DP_DEFSET;
1418 if (flags & OP_KERB)
1419 dp->dp_flag |= DP_KERB;
1420 if (flags & OP_NORESMNT)
1421 dp->dp_flag |= DP_NORESMNT;
1422 }
1423 }
1424
1425 /*
1426 * Search for a dirpath on the export point.
1427 */
1428 static struct dirlist *
1429 dirp_search(dp, dirp)
1430 struct dirlist *dp;
1431 char *dirp;
1432 {
1433 int cmp;
1434
1435 if (dp) {
1436 cmp = strcmp(dp->dp_dirp, dirp);
1437 if (cmp > 0)
1438 return (dirp_search(dp->dp_left, dirp));
1439 else if (cmp < 0)
1440 return (dirp_search(dp->dp_right, dirp));
1441 else
1442 return (dp);
1443 }
1444 return (dp);
1445 }
1446
1447 /*
1448 * Some helper functions for netmasks. They all assume masks in network
1449 * order (big endian).
1450 */
1451 static int
1452 bitcmp(void *dst, void *src, int bitlen)
1453 {
1454 int i;
1455 u_int8_t *p1 = dst, *p2 = src;
1456 u_int8_t bitmask;
1457 int bytelen, bitsleft;
1458
1459 bytelen = bitlen / 8;
1460 bitsleft = bitlen % 8;
1461
1462 if (debug) {
1463 printf("comparing:\n");
1464 for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
1465 printf("%02x", p1[i]);
1466 printf("\n");
1467 for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
1468 printf("%02x", p2[i]);
1469 printf("\n");
1470 }
1471
1472 for (i = 0; i < bytelen; i++) {
1473 if (*p1 != *p2)
1474 return 1;
1475 p1++;
1476 p2++;
1477 }
1478
1479 for (i = 0; i < bitsleft; i++) {
1480 bitmask = 1 << (7 - i);
1481 if ((*p1 & bitmask) != (*p2 & bitmask))
1482 return 1;
1483 }
1484
1485 return 0;
1486 }
1487
1488 static int
1489 netpartcmp(struct sockaddr *s1, struct sockaddr *s2, int bitlen)
1490 {
1491 void *src, *dst;
1492
1493 if (s1->sa_family != s2->sa_family)
1494 return 1;
1495
1496 switch (s1->sa_family) {
1497 case AF_INET:
1498 src = &((struct sockaddr_in *)s1)->sin_addr;
1499 dst = &((struct sockaddr_in *)s2)->sin_addr;
1500 if (bitlen > sizeof(((struct sockaddr_in *)s1)->sin_addr) * 8)
1501 return 1;
1502 break;
1503 case AF_INET6:
1504 src = &((struct sockaddr_in6 *)s1)->sin6_addr;
1505 dst = &((struct sockaddr_in6 *)s2)->sin6_addr;
1506 if (((struct sockaddr_in6 *)s1)->sin6_scope_id !=
1507 ((struct sockaddr_in6 *)s2)->sin6_scope_id)
1508 return 1;
1509 if (bitlen > sizeof(((struct sockaddr_in6 *)s1)->sin6_addr) * 8)
1510 return 1;
1511 break;
1512 default:
1513 return 1;
1514 }
1515
1516 return bitcmp(src, dst, bitlen);
1517 }
1518
1519 static int
1520 allones(struct sockaddr_storage *ssp, int bitlen)
1521 {
1522 u_int8_t *p;
1523 int bytelen, bitsleft, i;
1524 int zerolen;
1525
1526 switch (ssp->ss_family) {
1527 case AF_INET:
1528 p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr;
1529 zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr);
1530 break;
1531 case AF_INET6:
1532 p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr;
1533 zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr);
1534 break;
1535 default:
1536 return -1;
1537 }
1538
1539 memset(p, 0, zerolen);
1540
1541 bytelen = bitlen / 8;
1542 bitsleft = bitlen % 8;
1543
1544 if (bytelen > zerolen)
1545 return -1;
1546
1547 for (i = 0; i < bytelen; i++)
1548 *p++ = 0xff;
1549
1550 for (i = 0; i < bitsleft; i++)
1551 *p |= 1 << (7 - i);
1552
1553 return 0;
1554 }
1555
1556 static int
1557 countones(struct sockaddr *sa)
1558 {
1559 void *mask;
1560 int i, bits = 0, bytelen;
1561 u_int8_t *p;
1562
1563 switch (sa->sa_family) {
1564 case AF_INET:
1565 mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
1566 bytelen = 4;
1567 break;
1568 case AF_INET6:
1569 mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr;
1570 bytelen = 16;
1571 break;
1572 default:
1573 return 0;
1574 }
1575
1576 p = mask;
1577
1578 for (i = 0; i < bytelen; i++, p++) {
1579 if (*p != 0xff) {
1580 for (bits = 0; bits < 8; bits++) {
1581 if (!(*p & (1 << (7 - bits))))
1582 break;
1583 }
1584 break;
1585 }
1586 }
1587
1588 return (i * 8 + bits);
1589 }
1590
1591 static int
1592 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
1593 {
1594 void *p1, *p2;
1595 int len;
1596
1597 if (sa1->sa_family != sa2->sa_family)
1598 return 1;
1599
1600 switch (sa1->sa_family) {
1601 case AF_INET:
1602 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
1603 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
1604 len = 4;
1605 break;
1606 case AF_INET6:
1607 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
1608 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
1609 len = 16;
1610 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
1611 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
1612 return 1;
1613 break;
1614 default:
1615 return 1;
1616 }
1617
1618 return memcmp(p1, p2, len);
1619 }
1620
1621 /*
1622 * Scan for a host match in a directory tree.
1623 */
1624 static int
1625 chk_host(dp, saddr, defsetp, hostsetp)
1626 struct dirlist *dp;
1627 struct sockaddr *saddr;
1628 int *defsetp;
1629 int *hostsetp;
1630 {
1631 struct hostlist *hp;
1632 struct grouplist *grp;
1633 struct addrinfo *ai;
1634
1635 if (dp) {
1636 if (dp->dp_flag & DP_DEFSET)
1637 *defsetp = dp->dp_flag;
1638 hp = dp->dp_hosts;
1639 while (hp) {
1640 grp = hp->ht_grp;
1641 switch (grp->gr_type) {
1642 case GT_HOST:
1643 ai = grp->gr_ptr.gt_addrinfo;
1644 for (; ai; ai = ai->ai_next) {
1645 if (!sacmp(ai->ai_addr, saddr)) {
1646 *hostsetp =
1647 (hp->ht_flag | DP_HOSTSET);
1648 return (1);
1649 }
1650 }
1651 break;
1652 case GT_NET:
1653 if (!netpartcmp(saddr,
1654 (struct sockaddr *)
1655 &grp->gr_ptr.gt_net.nt_net,
1656 grp->gr_ptr.gt_net.nt_len)) {
1657 *hostsetp = (hp->ht_flag | DP_HOSTSET);
1658 return (1);
1659 }
1660 break;
1661 };
1662 hp = hp->ht_next;
1663 }
1664 }
1665 return (0);
1666 }
1667
1668 /*
1669 * Scan tree for a host that matches the address.
1670 */
1671 static int
1672 scan_tree(dp, saddr)
1673 struct dirlist *dp;
1674 struct sockaddr *saddr;
1675 {
1676 int defset, hostset;
1677
1678 if (dp) {
1679 if (scan_tree(dp->dp_left, saddr))
1680 return (1);
1681 if (chk_host(dp, saddr, &defset, &hostset))
1682 return (1);
1683 if (scan_tree(dp->dp_right, saddr))
1684 return (1);
1685 }
1686 return (0);
1687 }
1688
1689 /*
1690 * Traverse the dirlist tree and free it up.
1691 */
1692 static void
1693 free_dir(dp)
1694 struct dirlist *dp;
1695 {
1696
1697 if (dp) {
1698 free_dir(dp->dp_left);
1699 free_dir(dp->dp_right);
1700 free_host(dp->dp_hosts);
1701 free(dp);
1702 }
1703 }
1704
1705 /*
1706 * Parse the option string and update fields.
1707 * Option arguments may either be -<option>=<value> or
1708 * -<option> <value>
1709 */
1710 static int
1711 do_opt(line, lineno, cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
1712 const char *line;
1713 size_t lineno;
1714 char **cpp, **endcpp;
1715 struct exportlist *ep;
1716 struct grouplist *grp;
1717 int *has_hostp;
1718 int *exflagsp;
1719 struct uucred *cr;
1720 {
1721 char *cpoptarg, *cpoptend;
1722 char *cp, *endcp, *cpopt, savedc, savedc2;
1723 int allflag, usedarg;
1724
1725 cpopt = *cpp;
1726 cpopt++;
1727 cp = *endcpp;
1728 savedc = *cp;
1729 *cp = '\0';
1730 while (cpopt && *cpopt) {
1731 allflag = 1;
1732 usedarg = -2;
1733 savedc2 = '\0';
1734 if ((cpoptend = strchr(cpopt, ',')) != NULL) {
1735 *cpoptend++ = '\0';
1736 if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1737 *cpoptarg++ = '\0';
1738 } else {
1739 if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1740 *cpoptarg++ = '\0';
1741 else {
1742 *cp = savedc;
1743 nextfield(&cp, &endcp);
1744 **endcpp = '\0';
1745 if (endcp > cp && *cp != '-') {
1746 cpoptarg = cp;
1747 savedc2 = *endcp;
1748 *endcp = '\0';
1749 usedarg = 0;
1750 }
1751 }
1752 }
1753 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1754 *exflagsp |= MNT_EXRDONLY;
1755 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1756 !(allflag = strcmp(cpopt, "mapall")) ||
1757 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1758 usedarg++;
1759 parsecred(cpoptarg, cr);
1760 if (allflag == 0) {
1761 *exflagsp |= MNT_EXPORTANON;
1762 opt_flags |= OP_MAPALL;
1763 } else
1764 opt_flags |= OP_MAPROOT;
1765 } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
1766 *exflagsp |= MNT_EXKERB;
1767 opt_flags |= OP_KERB;
1768 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1769 !strcmp(cpopt, "m"))) {
1770 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1771 syslog(LOG_ERR,
1772 "\"%s\", line %ld: Bad mask: %s",
1773 line, (unsigned long)lineno, cpoptarg);
1774 return (1);
1775 }
1776 usedarg++;
1777 opt_flags |= OP_MASK;
1778 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1779 !strcmp(cpopt, "n"))) {
1780 if (strchr(cpoptarg, '/') != NULL) {
1781 if (debug)
1782 fprintf(stderr, "setting OP_MASKLEN\n");
1783 opt_flags |= OP_MASKLEN;
1784 }
1785 if (grp->gr_type != GT_NULL) {
1786 syslog(LOG_ERR,
1787 "\"%s\", line %ld: Network/host conflict",
1788 line, (unsigned long)lineno);
1789 return (1);
1790 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1791 syslog(LOG_ERR,
1792 "\"%s\", line %ld: Bad net: %s",
1793 line, (unsigned long)lineno, cpoptarg);
1794 return (1);
1795 }
1796 grp->gr_type = GT_NET;
1797 *has_hostp = 1;
1798 usedarg++;
1799 opt_flags |= OP_NET;
1800 } else if (!strcmp(cpopt, "alldirs")) {
1801 opt_flags |= OP_ALLDIRS;
1802 } else if (!strcmp(cpopt, "noresvmnt")) {
1803 opt_flags |= OP_NORESMNT;
1804 } else if (!strcmp(cpopt, "noresvport")) {
1805 opt_flags |= OP_NORESPORT;
1806 *exflagsp |= MNT_EXNORESPORT;
1807 } else if (!strcmp(cpopt, "public")) {
1808 *exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC);
1809 opt_flags |= OP_NORESPORT;
1810 } else if (!strcmp(cpopt, "webnfs")) {
1811 *exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC |
1812 MNT_EXRDONLY | MNT_EXPORTANON);
1813 opt_flags |= (OP_MAPALL | OP_NORESPORT);
1814 } else if (cpoptarg && !strcmp(cpopt, "index")) {
1815 ep->ex_indexfile = strdup(cpoptarg);
1816 #ifdef ISO
1817 } else if (cpoptarg && !strcmp(cpopt, "iso")) {
1818 if (get_isoaddr(line, lineno, cpoptarg, grp))
1819 return (1);
1820 *has_hostp = 1;
1821 usedarg++;
1822 opt_flags |= OP_ISO;
1823 #endif /* ISO */
1824 } else {
1825 syslog(LOG_ERR,
1826 "\"%s\", line %ld: Bad opt %s",
1827 line, (unsigned long)lineno, cpopt);
1828 return (1);
1829 }
1830 if (usedarg >= 0) {
1831 *endcp = savedc2;
1832 **endcpp = savedc;
1833 if (usedarg > 0) {
1834 *cpp = cp;
1835 *endcpp = endcp;
1836 }
1837 return (0);
1838 }
1839 cpopt = cpoptend;
1840 }
1841 **endcpp = savedc;
1842 return (0);
1843 }
1844
1845 /*
1846 * Translate a character string to the corresponding list of network
1847 * addresses for a hostname.
1848 */
1849 static int
1850 get_host(line, lineno, cp, grp)
1851 const char *line;
1852 size_t lineno;
1853 const char *cp;
1854 struct grouplist *grp;
1855 {
1856 struct addrinfo *ai, hints;
1857 int ecode;
1858 char host[NI_MAXHOST];
1859
1860 if (grp->gr_type != GT_NULL) {
1861 syslog(LOG_ERR,
1862 "\"%s\", line %ld: Bad netgroup type for ip host %s",
1863 line, (unsigned long)lineno, cp);
1864 return (1);
1865 }
1866 memset(&hints, 0, sizeof hints);
1867 hints.ai_flags = AI_CANONNAME;
1868 hints.ai_protocol = IPPROTO_UDP;
1869 ecode = getaddrinfo(cp, NULL, &hints, &ai);
1870 if (ecode != 0) {
1871 syslog(LOG_ERR, "\"%s\", line %ld: can't get address info for "
1872 "host %s",
1873 line, (long)lineno, cp);
1874 return 1;
1875 }
1876 grp->gr_type = GT_HOST;
1877 grp->gr_ptr.gt_addrinfo = ai;
1878 while (ai != NULL) {
1879 if (ai->ai_canonname == NULL) {
1880 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
1881 sizeof host, NULL, 0, ninumeric) != 0)
1882 strlcpy(host, "?", sizeof(host));
1883 ai->ai_canonname = estrdup(host);
1884 ai->ai_flags |= AI_CANONNAME;
1885 } else
1886 ai->ai_flags &= ~AI_CANONNAME;
1887 if (debug)
1888 (void)fprintf(stderr, "got host %s\n", ai->ai_canonname);
1889 ai = ai->ai_next;
1890 }
1891 return (0);
1892 }
1893
1894 /*
1895 * Free up an exports list component
1896 */
1897 static void
1898 free_exp(ep)
1899 struct exportlist *ep;
1900 {
1901
1902 if (ep->ex_defdir) {
1903 free_host(ep->ex_defdir->dp_hosts);
1904 free(ep->ex_defdir);
1905 }
1906 if (ep->ex_fsdir)
1907 free(ep->ex_fsdir);
1908 if (ep->ex_indexfile)
1909 free(ep->ex_indexfile);
1910 free_dir(ep->ex_dirl);
1911 free(ep);
1912 }
1913
1914 /*
1915 * Free hosts.
1916 */
1917 static void
1918 free_host(hp)
1919 struct hostlist *hp;
1920 {
1921 struct hostlist *hp2;
1922
1923 while (hp) {
1924 hp2 = hp;
1925 hp = hp->ht_next;
1926 free(hp2);
1927 }
1928 }
1929
1930 static struct hostlist *
1931 get_ht()
1932 {
1933 struct hostlist *hp;
1934
1935 hp = emalloc(sizeof(struct hostlist));
1936 hp->ht_next = NULL;
1937 hp->ht_flag = 0;
1938 return (hp);
1939 }
1940
1941 #ifdef ISO
1942 /*
1943 * Translate an iso address.
1944 */
1945 static int
1946 get_isoaddr(line, lineno, cp, grp)
1947 const char *line;
1948 size_t lineno;
1949 char *cp;
1950 struct grouplist *grp;
1951 {
1952 struct iso_addr *isop;
1953 struct sockaddr_iso *isoaddr;
1954
1955 if (grp->gr_type != GT_NULL) {
1956 syslog(LOG_ERR,
1957 "\"%s\", line %ld: Bad netgroup type for iso addr %s",
1958 line, (unsigned long)lineno, cp);
1959 return (1);
1960 }
1961 if ((isop = iso_addr(cp)) == NULL) {
1962 syslog(LOG_ERR,
1963 "\"%s\", line %ld: Bad iso addr %s",
1964 line, (unsigned long)lineno, cp);
1965 return (1);
1966 }
1967 isoaddr = emalloc(sizeof(struct sockaddr_iso));
1968 (void)memset(isoaddr, 0, sizeof(struct sockaddr_iso));
1969 (void)memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
1970 isoaddr->siso_len = sizeof(struct sockaddr_iso);
1971 isoaddr->siso_family = AF_ISO;
1972 grp->gr_type = GT_ISO;
1973 grp->gr_ptr.gt_isoaddr = isoaddr;
1974 return (0);
1975 }
1976 #endif /* ISO */
1977
1978 /*
1979 * error checked malloc and strdup
1980 */
1981 static void *
1982 emalloc(n)
1983 size_t n;
1984 {
1985 void *ptr = malloc(n);
1986
1987 if (ptr == NULL) {
1988 syslog(LOG_ERR, "%m");
1989 exit(2);
1990 }
1991 return ptr;
1992 }
1993
1994 static char *
1995 estrdup(s)
1996 const char *s;
1997 {
1998 char *n = strdup(s);
1999
2000 if (n == NULL) {
2001 syslog(LOG_ERR, "%m");
2002 exit(2);
2003 }
2004 return n;
2005 }
2006
2007 /*
2008 * Do the mount syscall with the update flag to push the export info into
2009 * the kernel.
2010 */
2011 static int
2012 do_mount(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
2013 const char *line;
2014 size_t lineno;
2015 struct exportlist *ep;
2016 struct grouplist *grp;
2017 int exflags;
2018 struct uucred *anoncrp;
2019 char *dirp;
2020 int dirplen;
2021 struct statvfs *fsb;
2022 {
2023 struct sockaddr *addrp;
2024 struct sockaddr_storage ss;
2025 struct addrinfo *ai;
2026 int addrlen;
2027 char *cp = NULL;
2028 int done;
2029 char savedc = '\0';
2030 union {
2031 struct ufs_args ua;
2032 struct iso_args ia;
2033 struct mfs_args ma;
2034 struct msdosfs_args da;
2035 struct adosfs_args aa;
2036 } args;
2037
2038 args.ua.fspec = 0;
2039 args.ua.export.ex_flags = exflags;
2040 args.ua.export.ex_anon = *anoncrp;
2041 args.ua.export.ex_indexfile = ep->ex_indexfile;
2042 if (grp->gr_type == GT_HOST) {
2043 ai = grp->gr_ptr.gt_addrinfo;
2044 addrp = ai->ai_addr;
2045 addrlen = ai->ai_addrlen;
2046 } else {
2047 addrp = NULL;
2048 ai = NULL; /* XXXGCC -Wuninitialized */
2049 addrlen = 0; /* XXXGCC -Wuninitialized */
2050 }
2051 done = FALSE;
2052 while (!done) {
2053 switch (grp->gr_type) {
2054 case GT_HOST:
2055 if (addrp != NULL && addrp->sa_family == AF_INET6 &&
2056 have_v6 == 0)
2057 goto skip;
2058 args.ua.export.ex_addr = addrp;
2059 args.ua.export.ex_addrlen = addrlen;
2060 args.ua.export.ex_masklen = 0;
2061 break;
2062 case GT_NET:
2063 args.ua.export.ex_addr = (struct sockaddr *)
2064 &grp->gr_ptr.gt_net.nt_net;
2065 if (args.ua.export.ex_addr->sa_family == AF_INET6 &&
2066 have_v6 == 0)
2067 goto skip;
2068 args.ua.export.ex_addrlen =
2069 args.ua.export.ex_addr->sa_len;
2070 memset(&ss, 0, sizeof ss);
2071 ss.ss_family = args.ua.export.ex_addr->sa_family;
2072 ss.ss_len = args.ua.export.ex_addr->sa_len;
2073 if (allones(&ss, grp->gr_ptr.gt_net.nt_len) != 0) {
2074 syslog(LOG_ERR,
2075 "\"%s\", line %ld: Bad network flag",
2076 line, (unsigned long)lineno);
2077 if (cp)
2078 *cp = savedc;
2079 return (1);
2080 }
2081 args.ua.export.ex_mask = (struct sockaddr *)&ss;
2082 args.ua.export.ex_masklen = ss.ss_len;
2083 break;
2084 #ifdef ISO
2085 case GT_ISO:
2086 args.ua.export.ex_addr =
2087 (struct sockaddr *) grp->gr_ptr.gt_isoaddr;
2088 args.ua.export.ex_addrlen =
2089 sizeof(struct sockaddr_iso);
2090 args.ua.export.ex_masklen = 0;
2091 break;
2092 #endif /* ISO */
2093 default:
2094 syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
2095 line, (unsigned long)lineno);
2096 if (cp)
2097 *cp = savedc;
2098 return (1);
2099 };
2100
2101 /*
2102 * XXX:
2103 * Maybe I should just use the fsb->f_mntonname path instead
2104 * of looping back up the dirp to the mount point??
2105 * Also, needs to know how to export all types of local
2106 * exportable file systems and not just MOUNT_FFS.
2107 */
2108 while (mount(fsb->f_fstypename, dirp,
2109 fsb->f_flag | MNT_UPDATE, &args) == -1) {
2110 if (cp)
2111 *cp-- = savedc;
2112 else
2113 cp = dirp + dirplen - 1;
2114 if (errno == EPERM) {
2115 syslog(LOG_ERR,
2116 "\"%s\", line %ld: Can't change attributes for %s to %s: %m",
2117 line, (unsigned long)lineno,
2118 dirp, (grp->gr_type == GT_HOST) ?
2119 grp->gr_ptr.gt_addrinfo->ai_canonname :
2120 (grp->gr_type == GT_NET) ?
2121 grp->gr_ptr.gt_net.nt_name :
2122 "Unknown");
2123 return (1);
2124 }
2125 if (opt_flags & OP_ALLDIRS) {
2126 syslog(LOG_ERR,
2127 "\"%s\", line %ld: Could not remount %s: %m",
2128 line, (unsigned long)lineno,
2129 dirp);
2130 return (1);
2131 }
2132 /* back up over the last component */
2133 while (*cp == '/' && cp > dirp)
2134 cp--;
2135 while (*(cp - 1) != '/' && cp > dirp)
2136 cp--;
2137 if (cp == dirp) {
2138 if (debug)
2139 (void)fprintf(stderr, "mnt unsucc\n");
2140 syslog(LOG_ERR,
2141 "\"%s\", line %ld: Can't export %s: %m",
2142 line, (unsigned long)lineno, dirp);
2143 return (1);
2144 }
2145 savedc = *cp;
2146 *cp = '\0';
2147 }
2148 skip:
2149 if (addrp) {
2150 ai = ai->ai_next;
2151 if (ai == NULL)
2152 done = TRUE;
2153 else {
2154 addrp = ai->ai_addr;
2155 addrlen = ai->ai_addrlen;
2156 }
2157 } else
2158 done = TRUE;
2159 }
2160 if (cp)
2161 *cp = savedc;
2162 return (0);
2163 }
2164
2165 /*
2166 * Translate a net address.
2167 */
2168 static int
2169 get_net(cp, net, maskflg)
2170 char *cp;
2171 struct netmsk *net;
2172 int maskflg;
2173 {
2174 struct netent *np;
2175 char *name, *p, *prefp;
2176 struct sockaddr_in sin, *sinp;
2177 struct sockaddr *sa;
2178 struct addrinfo hints, *ai = NULL;
2179 char netname[NI_MAXHOST];
2180 long preflen;
2181 int ecode;
2182
2183 (void)memset(&sin, 0, sizeof(sin));
2184 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2185 p = strchr(cp, '/');
2186 *p = '\0';
2187 prefp = p + 1;
2188 } else {
2189 p = NULL; /* XXXGCC -Wuninitialized */
2190 prefp = NULL; /* XXXGCC -Wuninitialized */
2191 }
2192
2193 if ((np = getnetbyname(cp)) != NULL) {
2194 sin.sin_family = AF_INET;
2195 sin.sin_len = sizeof sin;
2196 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2197 sa = (struct sockaddr *)&sin;
2198 } else if (isdigit((unsigned char)*cp)) {
2199 memset(&hints, 0, sizeof hints);
2200 hints.ai_family = AF_UNSPEC;
2201 hints.ai_flags = AI_NUMERICHOST;
2202 if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
2203 /*
2204 * If getaddrinfo() failed, try the inet4 network
2205 * notation with less than 3 dots.
2206 */
2207 sin.sin_family = AF_INET;
2208 sin.sin_len = sizeof sin;
2209 sin.sin_addr = inet_makeaddr(inet_network(cp),0);
2210 if (debug)
2211 fprintf(stderr, "get_net: v4 addr %x\n",
2212 sin.sin_addr.s_addr);
2213 sa = (struct sockaddr *)&sin;
2214 } else
2215 sa = ai->ai_addr;
2216 } else if (isxdigit((unsigned char)*cp) || *cp == ':') {
2217 memset(&hints, 0, sizeof hints);
2218 hints.ai_family = AF_UNSPEC;
2219 hints.ai_flags = AI_NUMERICHOST;
2220 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2221 sa = ai->ai_addr;
2222 else
2223 goto fail;
2224 } else
2225 goto fail;
2226
2227 /*
2228 * Only allow /pref notation for v6 addresses.
2229 */
2230 if (sa->sa_family == AF_INET6 && (!(opt_flags & OP_MASKLEN) || maskflg))
2231 return 1;
2232
2233 ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2234 NULL, 0, ninumeric);
2235 if (ecode != 0)
2236 goto fail;
2237
2238 if (maskflg)
2239 net->nt_len = countones(sa);
2240 else {
2241 if (opt_flags & OP_MASKLEN) {
2242 errno = 0;
2243 preflen = strtol(prefp, NULL, 10);
2244 if (preflen == LONG_MIN && errno == ERANGE)
2245 goto fail;
2246 net->nt_len = (int)preflen;
2247 *p = '/';
2248 }
2249
2250 if (np)
2251 name = np->n_name;
2252 else {
2253 if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2254 NULL, 0, ninumeric) != 0)
2255 strlcpy(netname, "?", sizeof(netname));
2256 name = netname;
2257 }
2258 net->nt_name = estrdup(name);
2259 memcpy(&net->nt_net, sa, sa->sa_len);
2260 }
2261
2262 if (!maskflg && sa->sa_family == AF_INET &&
2263 !(opt_flags & (OP_MASK|OP_MASKLEN))) {
2264 sinp = (struct sockaddr_in *)sa;
2265 if (IN_CLASSA(sinp->sin_addr.s_addr))
2266 net->nt_len = 8;
2267 else if (IN_CLASSB(sinp->sin_addr.s_addr))
2268 net->nt_len = 16;
2269 else if (IN_CLASSC(sinp->sin_addr.s_addr))
2270 net->nt_len = 24;
2271 else if (IN_CLASSD(sinp->sin_addr.s_addr))
2272 net->nt_len = 28;
2273 else
2274 net->nt_len = 32; /* XXX */
2275 }
2276
2277 if (ai)
2278 freeaddrinfo(ai);
2279 return 0;
2280
2281 fail:
2282 if (ai)
2283 freeaddrinfo(ai);
2284 return 1;
2285 }
2286
2287 /*
2288 * Parse out the next white space separated field
2289 */
2290 static void
2291 nextfield(cp, endcp)
2292 char **cp;
2293 char **endcp;
2294 {
2295 char *p;
2296
2297 p = *cp;
2298 while (*p == ' ' || *p == '\t')
2299 p++;
2300 if (*p == '\n' || *p == '\0')
2301 *cp = *endcp = p;
2302 else {
2303 *cp = p++;
2304 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2305 p++;
2306 *endcp = p;
2307 }
2308 }
2309
2310 /*
2311 * Parse a description of a credential.
2312 */
2313 static void
2314 parsecred(namelist, cr)
2315 char *namelist;
2316 struct uucred *cr;
2317 {
2318 char *name;
2319 int cnt;
2320 char *names;
2321 struct passwd *pw;
2322 struct group *gr;
2323 int ngroups, groups[NGROUPS + 1];
2324
2325 /*
2326 * Set up the unprivileged user.
2327 */
2328 *cr = def_anon;
2329 /*
2330 * Get the user's password table entry.
2331 */
2332 names = strsep(&namelist, " \t\n");
2333 name = strsep(&names, ":");
2334 if (isdigit((unsigned char)*name) || *name == '-')
2335 pw = getpwuid(atoi(name));
2336 else
2337 pw = getpwnam(name);
2338 /*
2339 * Credentials specified as those of a user.
2340 */
2341 if (names == NULL) {
2342 if (pw == NULL) {
2343 syslog(LOG_ERR, "Unknown user: %s", name);
2344 return;
2345 }
2346 cr->cr_uid = pw->pw_uid;
2347 ngroups = NGROUPS + 1;
2348 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2349 syslog(LOG_ERR, "Too many groups");
2350 /*
2351 * Convert from int's to gid_t's and compress out duplicate
2352 */
2353 cr->cr_ngroups = ngroups - 1;
2354 cr->cr_gid = groups[0];
2355 for (cnt = 1; cnt < ngroups; cnt++)
2356 cr->cr_groups[cnt - 1] = groups[cnt];
2357 return;
2358 }
2359 /*
2360 * Explicit credential specified as a colon separated list:
2361 * uid:gid:gid:...
2362 */
2363 if (pw != NULL)
2364 cr->cr_uid = pw->pw_uid;
2365 else if (isdigit((unsigned char)*name) || *name == '-')
2366 cr->cr_uid = atoi(name);
2367 else {
2368 syslog(LOG_ERR, "Unknown user: %s", name);
2369 return;
2370 }
2371 cr->cr_ngroups = 0;
2372 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2373 name = strsep(&names, ":");
2374 if (isdigit((unsigned char)*name) || *name == '-') {
2375 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2376 } else {
2377 if ((gr = getgrnam(name)) == NULL) {
2378 syslog(LOG_ERR, "Unknown group: %s", name);
2379 continue;
2380 }
2381 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2382 }
2383 }
2384 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2385 syslog(LOG_ERR, "Too many groups");
2386 }
2387
2388 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2389 /*
2390 * Routines that maintain the remote mounttab
2391 */
2392 static void
2393 get_mountlist()
2394 {
2395 struct mountlist *mlp, **mlpp;
2396 char *host, *dirp, *cp;
2397 char str[STRSIZ];
2398 FILE *mlfile;
2399
2400 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2401 syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST);
2402 return;
2403 }
2404 mlpp = &mlhead;
2405 while (fgets(str, STRSIZ, mlfile) != NULL) {
2406 cp = str;
2407 host = strsep(&cp, " \t\n");
2408 dirp = strsep(&cp, " \t\n");
2409 if (host == NULL || dirp == NULL)
2410 continue;
2411 mlp = emalloc(sizeof(*mlp));
2412 (void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2413 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2414 (void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2415 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2416 mlp->ml_next = NULL;
2417 *mlpp = mlp;
2418 mlpp = &mlp->ml_next;
2419 }
2420 (void)fclose(mlfile);
2421 }
2422
2423 static int
2424 del_mlist(hostp, dirp, saddr)
2425 char *hostp, *dirp;
2426 struct sockaddr *saddr;
2427 {
2428 struct mountlist *mlp, **mlpp;
2429 struct mountlist *mlp2;
2430 u_short sport;
2431 FILE *mlfile;
2432 int fnd = 0, ret = 0;
2433 char host[NI_MAXHOST];
2434
2435 switch (saddr->sa_family) {
2436 case AF_INET6:
2437 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
2438 break;
2439 case AF_INET:
2440 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
2441 break;
2442 default:
2443 return -1;
2444 }
2445 mlpp = &mlhead;
2446 mlp = mlhead;
2447 while (mlp) {
2448 if (!strcmp(mlp->ml_host, hostp) &&
2449 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2450 if (!(mlp->ml_flag & DP_NORESMNT) &&
2451 sport >= IPPORT_RESERVED) {
2452 if (getnameinfo(saddr, saddr->sa_len, host,
2453 sizeof host, NULL, 0, ninumeric) != 0)
2454 strlcpy(host, "?", sizeof(host));
2455 syslog(LOG_NOTICE,
2456 "Umount request for %s:%s from %s refused\n",
2457 mlp->ml_host, mlp->ml_dirp, host);
2458 ret = -1;
2459 goto cont;
2460 }
2461 fnd = 1;
2462 mlp2 = mlp;
2463 *mlpp = mlp = mlp->ml_next;
2464 free(mlp2);
2465 } else {
2466 cont:
2467 mlpp = &mlp->ml_next;
2468 mlp = mlp->ml_next;
2469 }
2470 }
2471 if (fnd) {
2472 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2473 syslog(LOG_ERR, "Can't update %s: %m",
2474 _PATH_RMOUNTLIST);
2475 return ret;
2476 }
2477 mlp = mlhead;
2478 while (mlp) {
2479 (void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
2480 mlp->ml_dirp);
2481 mlp = mlp->ml_next;
2482 }
2483 (void)fclose(mlfile);
2484 }
2485 return ret;
2486 }
2487
2488 static void
2489 add_mlist(hostp, dirp, flags)
2490 char *hostp, *dirp;
2491 int flags;
2492 {
2493 struct mountlist *mlp, **mlpp;
2494 FILE *mlfile;
2495
2496 mlpp = &mlhead;
2497 mlp = mlhead;
2498 while (mlp) {
2499 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2500 return;
2501 mlpp = &mlp->ml_next;
2502 mlp = mlp->ml_next;
2503 }
2504 mlp = emalloc(sizeof(*mlp));
2505 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2506 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2507 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2508 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2509 mlp->ml_flag = flags;
2510 mlp->ml_next = NULL;
2511 *mlpp = mlp;
2512 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2513 syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST);
2514 return;
2515 }
2516 (void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2517 (void)fclose(mlfile);
2518 }
2519
2520 /*
2521 * This function is called via. SIGTERM when the system is going down.
2522 * It sends a broadcast RPCMNT_UMNTALL.
2523 */
2524 /* ARGSUSED */
2525 static void
2526 send_umntall(n)
2527 int n;
2528 {
2529 (void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
2530 xdr_void, NULL, xdr_void, NULL, (resultproc_t)umntall_each);
2531 exit(0);
2532 }
2533
2534 static int
2535 umntall_each(resultsp, raddr)
2536 caddr_t resultsp;
2537 struct sockaddr_in *raddr;
2538 {
2539 return (1);
2540 }
2541
2542 /*
2543 * Free up a group list.
2544 */
2545 static void
2546 free_grp(grp)
2547 struct grouplist *grp;
2548 {
2549
2550 if (grp->gr_type == GT_HOST) {
2551 if (grp->gr_ptr.gt_addrinfo != NULL)
2552 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2553 } else if (grp->gr_type == GT_NET) {
2554 if (grp->gr_ptr.gt_net.nt_name)
2555 free(grp->gr_ptr.gt_net.nt_name);
2556 }
2557 #ifdef ISO
2558 else if (grp->gr_type == GT_ISO)
2559 free(grp->gr_ptr.gt_isoaddr);
2560 #endif
2561 free(grp);
2562 }
2563
2564 #if 0
2565 static void
2566 SYSLOG(int pri, const char *fmt,...)
2567 {
2568 va_list ap;
2569
2570 va_start(ap, fmt);
2571
2572 if (debug)
2573 vfprintf(stderr, fmt, ap);
2574 else
2575 vsyslog(pri, fmt, ap);
2576
2577 va_end(ap);
2578 }
2579 #endif
2580
2581 /*
2582 * Check options for consistency.
2583 */
2584 static int
2585 check_options(line, lineno, dp)
2586 const char *line;
2587 size_t lineno;
2588 struct dirlist *dp;
2589 {
2590
2591 if (dp == NULL) {
2592 syslog(LOG_ERR,
2593 "\"%s\", line %ld: missing directory list",
2594 line, (unsigned long)lineno);
2595 return (1);
2596 }
2597 if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
2598 (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
2599 (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
2600 syslog(LOG_ERR,
2601 "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
2602 line, (unsigned long)lineno);
2603 return (1);
2604 }
2605 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2606 syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
2607 line, (unsigned long)lineno);
2608 return (1);
2609 }
2610 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN) != 0) {
2611 syslog(LOG_ERR, "\"%s\", line %ld: /pref and -mask mutually"
2612 " exclusive",
2613 line, (unsigned long)lineno);
2614 return (1);
2615 }
2616 if ((opt_flags & (OP_NET|OP_ISO)) == (OP_NET|OP_ISO)) {
2617 syslog(LOG_ERR,
2618 "\"%s\", line %ld: -net and -iso mutually exclusive",
2619 line, (unsigned long)lineno);
2620 return (1);
2621 }
2622 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2623 syslog(LOG_ERR,
2624 "\"%s\", line %ld: -alldir has multiple directories",
2625 line, (unsigned long)lineno);
2626 return (1);
2627 }
2628 return (0);
2629 }
2630
2631 /*
2632 * Check an absolute directory path for any symbolic links. Return true
2633 * if no symbolic links are found.
2634 */
2635 static int
2636 check_dirpath(line, lineno, dirp)
2637 const char *line;
2638 size_t lineno;
2639 char *dirp;
2640 {
2641 char *cp;
2642 struct stat sb;
2643 char *file = "";
2644
2645 for (cp = dirp + 1; *cp; cp++) {
2646 if (*cp == '/') {
2647 *cp = '\0';
2648 if (lstat(dirp, &sb) == -1)
2649 goto bad;
2650 if (!S_ISDIR(sb.st_mode))
2651 goto bad1;
2652 *cp = '/';
2653 }
2654 }
2655
2656 cp = NULL;
2657 if (lstat(dirp, &sb) == -1)
2658 goto bad;
2659
2660 if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
2661 file = " file or a";
2662 goto bad1;
2663 }
2664
2665 return 1;
2666
2667 bad:
2668 syslog(LOG_ERR,
2669 "\"%s\", line %ld: lstat for `%s' failed: %m",
2670 line, (unsigned long)lineno, dirp);
2671 if (cp)
2672 *cp = '/';
2673 return 0;
2674
2675 bad1:
2676 syslog(LOG_ERR,
2677 "\"%s\", line %ld: `%s' is not a%s directory",
2678 line, (unsigned long)lineno, dirp, file);
2679 if (cp)
2680 *cp = '/';
2681 return 0;
2682 }
2683