mountd.c revision 1.90 1 /* $NetBSD: mountd.c,v 1.90 2005/01/14 16:12:46 thorpej 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.90 2005/01/14 16:12:46 thorpej 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 [-d]"
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, 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_MSDOS, MFSNAMELEN) ||
1022 !strncmp(fsp->f_fstypename, MOUNT_ADOSFS, MFSNAMELEN) ||
1023 !strncmp(fsp->f_fstypename, MOUNT_NULL, MFSNAMELEN) ||
1024 !strncmp(fsp->f_fstypename, MOUNT_UMAP, MFSNAMELEN) ||
1025 !strncmp(fsp->f_fstypename, MOUNT_UNION, MFSNAMELEN) ||
1026 !strncmp(fsp->f_fstypename, MOUNT_CD9660, MFSNAMELEN) ||
1027 !strncmp(fsp->f_fstypename, MOUNT_NTFS, MFSNAMELEN)) {
1028 bzero((char *) &targs, sizeof(targs));
1029 targs.ua.fspec = NULL;
1030 targs.ua.export.ex_flags = MNT_DELEXPORT;
1031 if (mount(fsp->f_fstypename, fsp->f_mntonname,
1032 fsp->f_flag | MNT_UPDATE, &targs) == -1)
1033 syslog(LOG_ERR, "Can't delete exports for %s",
1034 fsp->f_mntonname);
1035 }
1036 fsp++;
1037 }
1038
1039 /*
1040 * Read in the exports file and build the list, calling
1041 * mount() as we go along to push the export rules into the kernel.
1042 */
1043 if ((exp_file = fopen(exname, "r")) == NULL) {
1044 syslog(LOG_ERR, "Can't open %s: %m", exname);
1045 exit(2);
1046 }
1047 dirhead = NULL;
1048 while ((line = fparseln(exp_file, &len, &lineno, NULL, 0)) != NULL) {
1049 if (debug)
1050 (void)fprintf(stderr, "Got line %s\n", line);
1051 cp = line;
1052 nextfield(&cp, &endcp);
1053 if (cp == endcp)
1054 goto nextline; /* skip empty line */
1055 /*
1056 * Set defaults.
1057 */
1058 has_host = FALSE;
1059 anon = def_anon;
1060 exflags = MNT_EXPORTED;
1061 got_nondir = 0;
1062 opt_flags = 0;
1063 ep = NULL;
1064
1065 if (noprivports) {
1066 opt_flags |= OP_NORESMNT | OP_NORESPORT;
1067 exflags |= MNT_EXNORESPORT;
1068 }
1069
1070 /*
1071 * Create new exports list entry
1072 */
1073 len = endcp - cp;
1074 tgrp = grp = get_grp();
1075 while (len > 0) {
1076 if (len > RPCMNT_NAMELEN) {
1077 *endcp = '\0';
1078 syslog(LOG_ERR,
1079 "\"%s\", line %ld: name `%s' is too long",
1080 line, (unsigned long)lineno, cp);
1081 goto badline;
1082 }
1083 switch (*cp) {
1084 case '-':
1085 /*
1086 * Option
1087 */
1088 if (ep == NULL) {
1089 syslog(LOG_ERR,
1090 "\"%s\", line %ld: No current export list",
1091 line, (unsigned long)lineno);
1092 goto badline;
1093 }
1094 if (debug)
1095 (void)fprintf(stderr, "doing opt %s\n",
1096 cp);
1097 got_nondir = 1;
1098 if (do_opt(line, lineno, &cp, &endcp, ep, grp,
1099 &has_host, &exflags, &anon))
1100 goto badline;
1101 break;
1102
1103 case '/':
1104 /*
1105 * Directory
1106 */
1107 savedc = *endcp;
1108 *endcp = '\0';
1109
1110 if (!parse_directory(line, lineno, tgrp,
1111 got_nondir, cp, &ep, &fsb))
1112 goto badline;
1113 /*
1114 * Add dirpath to export mount point.
1115 */
1116 dirp = add_expdir(&dirhead, cp, len);
1117 dirplen = len;
1118
1119 *endcp = savedc;
1120 break;
1121
1122 default:
1123 /*
1124 * Host or netgroup.
1125 */
1126 savedc = *endcp;
1127 *endcp = '\0';
1128
1129 if (!parse_host_netgroup(line, lineno, ep,
1130 tgrp, cp, &has_host, &grp))
1131 goto badline;
1132
1133 got_nondir = 1;
1134
1135 *endcp = savedc;
1136 break;
1137 }
1138
1139 cp = endcp;
1140 nextfield(&cp, &endcp);
1141 len = endcp - cp;
1142 }
1143 if (check_options(line, lineno, dirhead))
1144 goto badline;
1145
1146 if (!has_host) {
1147 grp->gr_type = GT_HOST;
1148 if (debug)
1149 (void)fprintf(stderr,
1150 "Adding a default entry\n");
1151 /* add a default group and make the grp list NULL */
1152 ai = emalloc(sizeof(struct addrinfo));
1153 ai->ai_flags = 0;
1154 ai->ai_family = AF_INET; /* XXXX */
1155 ai->ai_socktype = SOCK_DGRAM;
1156 /* setting the length to 0 will match anything */
1157 ai->ai_addrlen = 0;
1158 ai->ai_flags = AI_CANONNAME;
1159 ai->ai_canonname = estrdup("Default");
1160 ai->ai_addr = NULL;
1161 ai->ai_next = NULL;
1162 grp->gr_ptr.gt_addrinfo = ai;
1163
1164 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
1165 /*
1166 * Don't allow a network export coincide with a list of
1167 * host(s) on the same line.
1168 */
1169 syslog(LOG_ERR,
1170 "\"%s\", line %ld: Mixed exporting of networks and hosts is disallowed",
1171 line, (unsigned long)lineno);
1172 goto badline;
1173 }
1174 /*
1175 * Loop through hosts, pushing the exports into the kernel.
1176 * After loop, tgrp points to the start of the list and
1177 * grp points to the last entry in the list.
1178 */
1179 grp = tgrp;
1180 do {
1181 if (do_mount(line, lineno, ep, grp, exflags, &anon,
1182 dirp, dirplen, &fsb))
1183 goto badline;
1184 } while (grp->gr_next && (grp = grp->gr_next));
1185
1186 /*
1187 * Success. Update the data structures.
1188 */
1189 if (has_host) {
1190 hang_dirp(dirhead, tgrp, ep, opt_flags);
1191 grp->gr_next = grphead;
1192 grphead = tgrp;
1193 } else {
1194 hang_dirp(dirhead, NULL, ep, opt_flags);
1195 free_grp(grp);
1196 }
1197 dirhead = NULL;
1198 if ((ep->ex_flag & EX_LINKED) == 0) {
1199 ep2 = exphead;
1200 epp = &exphead;
1201
1202 /*
1203 * Insert in the list in alphabetical order.
1204 */
1205 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1206 epp = &ep2->ex_next;
1207 ep2 = ep2->ex_next;
1208 }
1209 if (ep2)
1210 ep->ex_next = ep2;
1211 *epp = ep;
1212 ep->ex_flag |= EX_LINKED;
1213 }
1214 goto nextline;
1215 badline:
1216 free_exp_grp(ep, grp);
1217 nextline:
1218 if (dirhead) {
1219 free_dir(dirhead);
1220 dirhead = NULL;
1221 }
1222 free(line);
1223 }
1224 (void)fclose(exp_file);
1225 }
1226
1227 /*
1228 * Allocate an export list element
1229 */
1230 static struct exportlist *
1231 get_exp()
1232 {
1233 struct exportlist *ep;
1234
1235 ep = emalloc(sizeof(struct exportlist));
1236 (void)memset(ep, 0, sizeof(struct exportlist));
1237 return (ep);
1238 }
1239
1240 /*
1241 * Allocate a group list element
1242 */
1243 static struct grouplist *
1244 get_grp()
1245 {
1246 struct grouplist *gp;
1247
1248 gp = emalloc(sizeof(struct grouplist));
1249 (void)memset(gp, 0, sizeof(struct grouplist));
1250 return (gp);
1251 }
1252
1253 /*
1254 * Clean up upon an error in get_exportlist().
1255 */
1256 static void
1257 free_exp_grp(ep, grp)
1258 struct exportlist *ep;
1259 struct grouplist *grp;
1260 {
1261 struct grouplist *tgrp;
1262
1263 if (ep && (ep->ex_flag & EX_LINKED) == 0)
1264 free_exp(ep);
1265 while (grp) {
1266 tgrp = grp;
1267 grp = grp->gr_next;
1268 free_grp(tgrp);
1269 }
1270 }
1271
1272 /*
1273 * Search the export list for a matching fs.
1274 */
1275 static struct exportlist *
1276 ex_search(fsid)
1277 fsid_t *fsid;
1278 {
1279 struct exportlist *ep;
1280
1281 ep = exphead;
1282 while (ep) {
1283 if (ep->ex_fs.__fsid_val[0] == fsid->__fsid_val[0] &&
1284 ep->ex_fs.__fsid_val[1] == fsid->__fsid_val[1])
1285 return (ep);
1286 ep = ep->ex_next;
1287 }
1288 return (ep);
1289 }
1290
1291 /*
1292 * Add a directory path to the list.
1293 */
1294 static char *
1295 add_expdir(dpp, cp, len)
1296 struct dirlist **dpp;
1297 char *cp;
1298 int len;
1299 {
1300 struct dirlist *dp;
1301
1302 dp = emalloc(sizeof(struct dirlist) + len);
1303 dp->dp_left = *dpp;
1304 dp->dp_right = NULL;
1305 dp->dp_flag = 0;
1306 dp->dp_hosts = NULL;
1307 (void)strcpy(dp->dp_dirp, cp);
1308 *dpp = dp;
1309 return (dp->dp_dirp);
1310 }
1311
1312 /*
1313 * Hang the dir list element off the dirpath binary tree as required
1314 * and update the entry for host.
1315 */
1316 void
1317 hang_dirp(dp, grp, ep, flags)
1318 struct dirlist *dp;
1319 struct grouplist *grp;
1320 struct exportlist *ep;
1321 int flags;
1322 {
1323 struct hostlist *hp;
1324 struct dirlist *dp2;
1325
1326 if (flags & OP_ALLDIRS) {
1327 if (ep->ex_defdir)
1328 free(dp);
1329 else
1330 ep->ex_defdir = dp;
1331 if (grp == NULL) {
1332 ep->ex_defdir->dp_flag |= DP_DEFSET;
1333 if (flags & OP_KERB)
1334 ep->ex_defdir->dp_flag |= DP_KERB;
1335 if (flags & OP_NORESMNT)
1336 ep->ex_defdir->dp_flag |= DP_NORESMNT;
1337 } else
1338 while (grp) {
1339 hp = get_ht();
1340 if (flags & OP_KERB)
1341 hp->ht_flag |= DP_KERB;
1342 if (flags & OP_NORESMNT)
1343 hp->ht_flag |= DP_NORESMNT;
1344 hp->ht_grp = grp;
1345 hp->ht_next = ep->ex_defdir->dp_hosts;
1346 ep->ex_defdir->dp_hosts = hp;
1347 grp = grp->gr_next;
1348 }
1349 } else {
1350
1351 /*
1352 * Loop through the directories adding them to the tree.
1353 */
1354 while (dp) {
1355 dp2 = dp->dp_left;
1356 add_dlist(&ep->ex_dirl, dp, grp, flags);
1357 dp = dp2;
1358 }
1359 }
1360 }
1361
1362 /*
1363 * Traverse the binary tree either updating a node that is already there
1364 * for the new directory or adding the new node.
1365 */
1366 static void
1367 add_dlist(dpp, newdp, grp, flags)
1368 struct dirlist **dpp;
1369 struct dirlist *newdp;
1370 struct grouplist *grp;
1371 int flags;
1372 {
1373 struct dirlist *dp;
1374 struct hostlist *hp;
1375 int cmp;
1376
1377 dp = *dpp;
1378 if (dp) {
1379 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1380 if (cmp > 0) {
1381 add_dlist(&dp->dp_left, newdp, grp, flags);
1382 return;
1383 } else if (cmp < 0) {
1384 add_dlist(&dp->dp_right, newdp, grp, flags);
1385 return;
1386 } else
1387 free(newdp);
1388 } else {
1389 dp = newdp;
1390 dp->dp_left = NULL;
1391 *dpp = dp;
1392 }
1393 if (grp) {
1394
1395 /*
1396 * Hang all of the host(s) off of the directory point.
1397 */
1398 do {
1399 hp = get_ht();
1400 if (flags & OP_KERB)
1401 hp->ht_flag |= DP_KERB;
1402 if (flags & OP_NORESMNT)
1403 hp->ht_flag |= DP_NORESMNT;
1404 hp->ht_grp = grp;
1405 hp->ht_next = dp->dp_hosts;
1406 dp->dp_hosts = hp;
1407 grp = grp->gr_next;
1408 } while (grp);
1409 } else {
1410 dp->dp_flag |= DP_DEFSET;
1411 if (flags & OP_KERB)
1412 dp->dp_flag |= DP_KERB;
1413 if (flags & OP_NORESMNT)
1414 dp->dp_flag |= DP_NORESMNT;
1415 }
1416 }
1417
1418 /*
1419 * Search for a dirpath on the export point.
1420 */
1421 static struct dirlist *
1422 dirp_search(dp, dirp)
1423 struct dirlist *dp;
1424 char *dirp;
1425 {
1426 int cmp;
1427
1428 if (dp) {
1429 cmp = strcmp(dp->dp_dirp, dirp);
1430 if (cmp > 0)
1431 return (dirp_search(dp->dp_left, dirp));
1432 else if (cmp < 0)
1433 return (dirp_search(dp->dp_right, dirp));
1434 else
1435 return (dp);
1436 }
1437 return (dp);
1438 }
1439
1440 /*
1441 * Some helper functions for netmasks. They all assume masks in network
1442 * order (big endian).
1443 */
1444 static int
1445 bitcmp(void *dst, void *src, int bitlen)
1446 {
1447 int i;
1448 u_int8_t *p1 = dst, *p2 = src;
1449 u_int8_t bitmask;
1450 int bytelen, bitsleft;
1451
1452 bytelen = bitlen / 8;
1453 bitsleft = bitlen % 8;
1454
1455 if (debug) {
1456 printf("comparing:\n");
1457 for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
1458 printf("%02x", p1[i]);
1459 printf("\n");
1460 for (i = 0; i < (bitsleft ? bytelen + 1 : bytelen); i++)
1461 printf("%02x", p2[i]);
1462 printf("\n");
1463 }
1464
1465 for (i = 0; i < bytelen; i++) {
1466 if (*p1 != *p2)
1467 return 1;
1468 p1++;
1469 p2++;
1470 }
1471
1472 for (i = 0; i < bitsleft; i++) {
1473 bitmask = 1 << (7 - i);
1474 if ((*p1 & bitmask) != (*p2 & bitmask))
1475 return 1;
1476 }
1477
1478 return 0;
1479 }
1480
1481 static int
1482 netpartcmp(struct sockaddr *s1, struct sockaddr *s2, int bitlen)
1483 {
1484 void *src, *dst;
1485
1486 if (s1->sa_family != s2->sa_family)
1487 return 1;
1488
1489 switch (s1->sa_family) {
1490 case AF_INET:
1491 src = &((struct sockaddr_in *)s1)->sin_addr;
1492 dst = &((struct sockaddr_in *)s2)->sin_addr;
1493 if (bitlen > sizeof(((struct sockaddr_in *)s1)->sin_addr) * 8)
1494 return 1;
1495 break;
1496 case AF_INET6:
1497 src = &((struct sockaddr_in6 *)s1)->sin6_addr;
1498 dst = &((struct sockaddr_in6 *)s2)->sin6_addr;
1499 if (((struct sockaddr_in6 *)s1)->sin6_scope_id !=
1500 ((struct sockaddr_in6 *)s2)->sin6_scope_id)
1501 return 1;
1502 if (bitlen > sizeof(((struct sockaddr_in6 *)s1)->sin6_addr) * 8)
1503 return 1;
1504 break;
1505 default:
1506 return 1;
1507 }
1508
1509 return bitcmp(src, dst, bitlen);
1510 }
1511
1512 static int
1513 allones(struct sockaddr_storage *ssp, int bitlen)
1514 {
1515 u_int8_t *p;
1516 int bytelen, bitsleft, i;
1517 int zerolen;
1518
1519 switch (ssp->ss_family) {
1520 case AF_INET:
1521 p = (u_int8_t *)&((struct sockaddr_in *)ssp)->sin_addr;
1522 zerolen = sizeof (((struct sockaddr_in *)ssp)->sin_addr);
1523 break;
1524 case AF_INET6:
1525 p = (u_int8_t *)&((struct sockaddr_in6 *)ssp)->sin6_addr;
1526 zerolen = sizeof (((struct sockaddr_in6 *)ssp)->sin6_addr);
1527 break;
1528 default:
1529 return -1;
1530 }
1531
1532 memset(p, 0, zerolen);
1533
1534 bytelen = bitlen / 8;
1535 bitsleft = bitlen % 8;
1536
1537 if (bytelen > zerolen)
1538 return -1;
1539
1540 for (i = 0; i < bytelen; i++)
1541 *p++ = 0xff;
1542
1543 for (i = 0; i < bitsleft; i++)
1544 *p |= 1 << (7 - i);
1545
1546 return 0;
1547 }
1548
1549 static int
1550 countones(struct sockaddr *sa)
1551 {
1552 void *mask;
1553 int i, bits = 0, bytelen;
1554 u_int8_t *p;
1555
1556 switch (sa->sa_family) {
1557 case AF_INET:
1558 mask = (u_int8_t *)&((struct sockaddr_in *)sa)->sin_addr;
1559 bytelen = 4;
1560 break;
1561 case AF_INET6:
1562 mask = (u_int8_t *)&((struct sockaddr_in6 *)sa)->sin6_addr;
1563 bytelen = 16;
1564 break;
1565 default:
1566 return 0;
1567 }
1568
1569 p = mask;
1570
1571 for (i = 0; i < bytelen; i++, p++) {
1572 if (*p != 0xff) {
1573 for (bits = 0; bits < 8; bits++) {
1574 if (!(*p & (1 << (7 - bits))))
1575 break;
1576 }
1577 break;
1578 }
1579 }
1580
1581 return (i * 8 + bits);
1582 }
1583
1584 static int
1585 sacmp(struct sockaddr *sa1, struct sockaddr *sa2)
1586 {
1587 void *p1, *p2;
1588 int len;
1589
1590 if (sa1->sa_family != sa2->sa_family)
1591 return 1;
1592
1593 switch (sa1->sa_family) {
1594 case AF_INET:
1595 p1 = &((struct sockaddr_in *)sa1)->sin_addr;
1596 p2 = &((struct sockaddr_in *)sa2)->sin_addr;
1597 len = 4;
1598 break;
1599 case AF_INET6:
1600 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr;
1601 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr;
1602 len = 16;
1603 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id !=
1604 ((struct sockaddr_in6 *)sa2)->sin6_scope_id)
1605 return 1;
1606 break;
1607 default:
1608 return 1;
1609 }
1610
1611 return memcmp(p1, p2, len);
1612 }
1613
1614 /*
1615 * Scan for a host match in a directory tree.
1616 */
1617 static int
1618 chk_host(dp, saddr, defsetp, hostsetp)
1619 struct dirlist *dp;
1620 struct sockaddr *saddr;
1621 int *defsetp;
1622 int *hostsetp;
1623 {
1624 struct hostlist *hp;
1625 struct grouplist *grp;
1626 struct addrinfo *ai;
1627
1628 if (dp) {
1629 if (dp->dp_flag & DP_DEFSET)
1630 *defsetp = dp->dp_flag;
1631 hp = dp->dp_hosts;
1632 while (hp) {
1633 grp = hp->ht_grp;
1634 switch (grp->gr_type) {
1635 case GT_HOST:
1636 ai = grp->gr_ptr.gt_addrinfo;
1637 for (; ai; ai = ai->ai_next) {
1638 if (!sacmp(ai->ai_addr, saddr)) {
1639 *hostsetp =
1640 (hp->ht_flag | DP_HOSTSET);
1641 return (1);
1642 }
1643 }
1644 break;
1645 case GT_NET:
1646 if (!netpartcmp(saddr,
1647 (struct sockaddr *)
1648 &grp->gr_ptr.gt_net.nt_net,
1649 grp->gr_ptr.gt_net.nt_len)) {
1650 *hostsetp = (hp->ht_flag | DP_HOSTSET);
1651 return (1);
1652 }
1653 break;
1654 };
1655 hp = hp->ht_next;
1656 }
1657 }
1658 return (0);
1659 }
1660
1661 /*
1662 * Scan tree for a host that matches the address.
1663 */
1664 static int
1665 scan_tree(dp, saddr)
1666 struct dirlist *dp;
1667 struct sockaddr *saddr;
1668 {
1669 int defset, hostset;
1670
1671 if (dp) {
1672 if (scan_tree(dp->dp_left, saddr))
1673 return (1);
1674 if (chk_host(dp, saddr, &defset, &hostset))
1675 return (1);
1676 if (scan_tree(dp->dp_right, saddr))
1677 return (1);
1678 }
1679 return (0);
1680 }
1681
1682 /*
1683 * Traverse the dirlist tree and free it up.
1684 */
1685 static void
1686 free_dir(dp)
1687 struct dirlist *dp;
1688 {
1689
1690 if (dp) {
1691 free_dir(dp->dp_left);
1692 free_dir(dp->dp_right);
1693 free_host(dp->dp_hosts);
1694 free(dp);
1695 }
1696 }
1697
1698 /*
1699 * Parse the option string and update fields.
1700 * Option arguments may either be -<option>=<value> or
1701 * -<option> <value>
1702 */
1703 static int
1704 do_opt(line, lineno, cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
1705 const char *line;
1706 size_t lineno;
1707 char **cpp, **endcpp;
1708 struct exportlist *ep;
1709 struct grouplist *grp;
1710 int *has_hostp;
1711 int *exflagsp;
1712 struct uucred *cr;
1713 {
1714 char *cpoptarg, *cpoptend;
1715 char *cp, *endcp, *cpopt, savedc, savedc2;
1716 int allflag, usedarg;
1717
1718 cpopt = *cpp;
1719 cpopt++;
1720 cp = *endcpp;
1721 savedc = *cp;
1722 *cp = '\0';
1723 while (cpopt && *cpopt) {
1724 allflag = 1;
1725 usedarg = -2;
1726 savedc2 = '\0';
1727 if ((cpoptend = strchr(cpopt, ',')) != NULL) {
1728 *cpoptend++ = '\0';
1729 if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1730 *cpoptarg++ = '\0';
1731 } else {
1732 if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1733 *cpoptarg++ = '\0';
1734 else {
1735 *cp = savedc;
1736 nextfield(&cp, &endcp);
1737 **endcpp = '\0';
1738 if (endcp > cp && *cp != '-') {
1739 cpoptarg = cp;
1740 savedc2 = *endcp;
1741 *endcp = '\0';
1742 usedarg = 0;
1743 }
1744 }
1745 }
1746 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1747 *exflagsp |= MNT_EXRDONLY;
1748 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1749 !(allflag = strcmp(cpopt, "mapall")) ||
1750 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1751 usedarg++;
1752 parsecred(cpoptarg, cr);
1753 if (allflag == 0) {
1754 *exflagsp |= MNT_EXPORTANON;
1755 opt_flags |= OP_MAPALL;
1756 } else
1757 opt_flags |= OP_MAPROOT;
1758 } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
1759 *exflagsp |= MNT_EXKERB;
1760 opt_flags |= OP_KERB;
1761 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1762 !strcmp(cpopt, "m"))) {
1763 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1764 syslog(LOG_ERR,
1765 "\"%s\", line %ld: Bad mask: %s",
1766 line, (unsigned long)lineno, cpoptarg);
1767 return (1);
1768 }
1769 usedarg++;
1770 opt_flags |= OP_MASK;
1771 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1772 !strcmp(cpopt, "n"))) {
1773 if (strchr(cpoptarg, '/') != NULL) {
1774 if (debug)
1775 fprintf(stderr, "setting OP_MASKLEN\n");
1776 opt_flags |= OP_MASKLEN;
1777 }
1778 if (grp->gr_type != GT_NULL) {
1779 syslog(LOG_ERR,
1780 "\"%s\", line %ld: Network/host conflict",
1781 line, (unsigned long)lineno);
1782 return (1);
1783 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1784 syslog(LOG_ERR,
1785 "\"%s\", line %ld: Bad net: %s",
1786 line, (unsigned long)lineno, cpoptarg);
1787 return (1);
1788 }
1789 grp->gr_type = GT_NET;
1790 *has_hostp = 1;
1791 usedarg++;
1792 opt_flags |= OP_NET;
1793 } else if (!strcmp(cpopt, "alldirs")) {
1794 opt_flags |= OP_ALLDIRS;
1795 } else if (!strcmp(cpopt, "noresvmnt")) {
1796 opt_flags |= OP_NORESMNT;
1797 } else if (!strcmp(cpopt, "noresvport")) {
1798 opt_flags |= OP_NORESPORT;
1799 *exflagsp |= MNT_EXNORESPORT;
1800 } else if (!strcmp(cpopt, "public")) {
1801 *exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC);
1802 opt_flags |= OP_NORESPORT;
1803 } else if (!strcmp(cpopt, "webnfs")) {
1804 *exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC |
1805 MNT_EXRDONLY | MNT_EXPORTANON);
1806 opt_flags |= (OP_MAPALL | OP_NORESPORT);
1807 } else if (cpoptarg && !strcmp(cpopt, "index")) {
1808 ep->ex_indexfile = strdup(cpoptarg);
1809 #ifdef ISO
1810 } else if (cpoptarg && !strcmp(cpopt, "iso")) {
1811 if (get_isoaddr(line, lineno, cpoptarg, grp))
1812 return (1);
1813 *has_hostp = 1;
1814 usedarg++;
1815 opt_flags |= OP_ISO;
1816 #endif /* ISO */
1817 } else {
1818 syslog(LOG_ERR,
1819 "\"%s\", line %ld: Bad opt %s",
1820 line, (unsigned long)lineno, cpopt);
1821 return (1);
1822 }
1823 if (usedarg >= 0) {
1824 *endcp = savedc2;
1825 **endcpp = savedc;
1826 if (usedarg > 0) {
1827 *cpp = cp;
1828 *endcpp = endcp;
1829 }
1830 return (0);
1831 }
1832 cpopt = cpoptend;
1833 }
1834 **endcpp = savedc;
1835 return (0);
1836 }
1837
1838 /*
1839 * Translate a character string to the corresponding list of network
1840 * addresses for a hostname.
1841 */
1842 static int
1843 get_host(line, lineno, cp, grp)
1844 const char *line;
1845 size_t lineno;
1846 const char *cp;
1847 struct grouplist *grp;
1848 {
1849 struct addrinfo *ai, hints;
1850 int ecode;
1851 char host[NI_MAXHOST];
1852
1853 if (grp->gr_type != GT_NULL) {
1854 syslog(LOG_ERR,
1855 "\"%s\", line %ld: Bad netgroup type for ip host %s",
1856 line, (unsigned long)lineno, cp);
1857 return (1);
1858 }
1859 memset(&hints, 0, sizeof hints);
1860 hints.ai_flags = AI_CANONNAME;
1861 hints.ai_protocol = IPPROTO_UDP;
1862 ecode = getaddrinfo(cp, NULL, &hints, &ai);
1863 if (ecode != 0) {
1864 syslog(LOG_ERR, "\"%s\", line %ld: can't get address info for "
1865 "host %s",
1866 line, (long)lineno, cp);
1867 return 1;
1868 }
1869 grp->gr_type = GT_HOST;
1870 grp->gr_ptr.gt_addrinfo = ai;
1871 while (ai != NULL) {
1872 if (ai->ai_canonname == NULL) {
1873 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
1874 sizeof host, NULL, 0, ninumeric) != 0)
1875 strlcpy(host, "?", sizeof(host));
1876 ai->ai_canonname = estrdup(host);
1877 ai->ai_flags |= AI_CANONNAME;
1878 } else
1879 ai->ai_flags &= ~AI_CANONNAME;
1880 if (debug)
1881 (void)fprintf(stderr, "got host %s\n", ai->ai_canonname);
1882 ai = ai->ai_next;
1883 }
1884 return (0);
1885 }
1886
1887 /*
1888 * Free up an exports list component
1889 */
1890 static void
1891 free_exp(ep)
1892 struct exportlist *ep;
1893 {
1894
1895 if (ep->ex_defdir) {
1896 free_host(ep->ex_defdir->dp_hosts);
1897 free(ep->ex_defdir);
1898 }
1899 if (ep->ex_fsdir)
1900 free(ep->ex_fsdir);
1901 if (ep->ex_indexfile)
1902 free(ep->ex_indexfile);
1903 free_dir(ep->ex_dirl);
1904 free(ep);
1905 }
1906
1907 /*
1908 * Free hosts.
1909 */
1910 static void
1911 free_host(hp)
1912 struct hostlist *hp;
1913 {
1914 struct hostlist *hp2;
1915
1916 while (hp) {
1917 hp2 = hp;
1918 hp = hp->ht_next;
1919 free(hp2);
1920 }
1921 }
1922
1923 static struct hostlist *
1924 get_ht()
1925 {
1926 struct hostlist *hp;
1927
1928 hp = emalloc(sizeof(struct hostlist));
1929 hp->ht_next = NULL;
1930 hp->ht_flag = 0;
1931 return (hp);
1932 }
1933
1934 #ifdef ISO
1935 /*
1936 * Translate an iso address.
1937 */
1938 static int
1939 get_isoaddr(line, lineno, cp, grp)
1940 const char *line;
1941 size_t lineno;
1942 char *cp;
1943 struct grouplist *grp;
1944 {
1945 struct iso_addr *isop;
1946 struct sockaddr_iso *isoaddr;
1947
1948 if (grp->gr_type != GT_NULL) {
1949 syslog(LOG_ERR,
1950 "\"%s\", line %ld: Bad netgroup type for iso addr %s",
1951 line, (unsigned long)lineno, cp);
1952 return (1);
1953 }
1954 if ((isop = iso_addr(cp)) == NULL) {
1955 syslog(LOG_ERR,
1956 "\"%s\", line %ld: Bad iso addr %s",
1957 line, (unsigned long)lineno, cp);
1958 return (1);
1959 }
1960 isoaddr = emalloc(sizeof(struct sockaddr_iso));
1961 (void)memset(isoaddr, 0, sizeof(struct sockaddr_iso));
1962 (void)memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
1963 isoaddr->siso_len = sizeof(struct sockaddr_iso);
1964 isoaddr->siso_family = AF_ISO;
1965 grp->gr_type = GT_ISO;
1966 grp->gr_ptr.gt_isoaddr = isoaddr;
1967 return (0);
1968 }
1969 #endif /* ISO */
1970
1971 /*
1972 * error checked malloc and strdup
1973 */
1974 static void *
1975 emalloc(n)
1976 size_t n;
1977 {
1978 void *ptr = malloc(n);
1979
1980 if (ptr == NULL) {
1981 syslog(LOG_ERR, "%m");
1982 exit(2);
1983 }
1984 return ptr;
1985 }
1986
1987 static char *
1988 estrdup(s)
1989 const char *s;
1990 {
1991 char *n = strdup(s);
1992
1993 if (n == NULL) {
1994 syslog(LOG_ERR, "%m");
1995 exit(2);
1996 }
1997 return n;
1998 }
1999
2000 /*
2001 * Do the mount syscall with the update flag to push the export info into
2002 * the kernel.
2003 */
2004 static int
2005 do_mount(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
2006 const char *line;
2007 size_t lineno;
2008 struct exportlist *ep;
2009 struct grouplist *grp;
2010 int exflags;
2011 struct uucred *anoncrp;
2012 char *dirp;
2013 int dirplen;
2014 struct statvfs *fsb;
2015 {
2016 struct sockaddr *addrp;
2017 struct sockaddr_storage ss;
2018 struct addrinfo *ai;
2019 int addrlen;
2020 char *cp = NULL;
2021 int done;
2022 char savedc = '\0';
2023 union {
2024 struct ufs_args ua;
2025 struct iso_args ia;
2026 struct mfs_args ma;
2027 struct msdosfs_args da;
2028 struct adosfs_args aa;
2029 } args;
2030
2031 args.ua.fspec = 0;
2032 args.ua.export.ex_flags = exflags;
2033 args.ua.export.ex_anon = *anoncrp;
2034 args.ua.export.ex_indexfile = ep->ex_indexfile;
2035 if (grp->gr_type == GT_HOST) {
2036 ai = grp->gr_ptr.gt_addrinfo;
2037 addrp = ai->ai_addr;
2038 addrlen = ai->ai_addrlen;
2039 } else
2040 addrp = NULL;
2041 done = FALSE;
2042 while (!done) {
2043 switch (grp->gr_type) {
2044 case GT_HOST:
2045 if (addrp != NULL && addrp->sa_family == AF_INET6 &&
2046 have_v6 == 0)
2047 goto skip;
2048 args.ua.export.ex_addr = addrp;
2049 args.ua.export.ex_addrlen = addrlen;
2050 args.ua.export.ex_masklen = 0;
2051 break;
2052 case GT_NET:
2053 args.ua.export.ex_addr = (struct sockaddr *)
2054 &grp->gr_ptr.gt_net.nt_net;
2055 if (args.ua.export.ex_addr->sa_family == AF_INET6 &&
2056 have_v6 == 0)
2057 goto skip;
2058 args.ua.export.ex_addrlen =
2059 args.ua.export.ex_addr->sa_len;
2060 memset(&ss, 0, sizeof ss);
2061 ss.ss_family = args.ua.export.ex_addr->sa_family;
2062 ss.ss_len = args.ua.export.ex_addr->sa_len;
2063 if (allones(&ss, grp->gr_ptr.gt_net.nt_len) != 0) {
2064 syslog(LOG_ERR,
2065 "\"%s\", line %ld: Bad network flag",
2066 line, (unsigned long)lineno);
2067 if (cp)
2068 *cp = savedc;
2069 return (1);
2070 }
2071 args.ua.export.ex_mask = (struct sockaddr *)&ss;
2072 args.ua.export.ex_masklen = ss.ss_len;
2073 break;
2074 #ifdef ISO
2075 case GT_ISO:
2076 args.ua.export.ex_addr =
2077 (struct sockaddr *) grp->gr_ptr.gt_isoaddr;
2078 args.ua.export.ex_addrlen =
2079 sizeof(struct sockaddr_iso);
2080 args.ua.export.ex_masklen = 0;
2081 break;
2082 #endif /* ISO */
2083 default:
2084 syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
2085 line, (unsigned long)lineno);
2086 if (cp)
2087 *cp = savedc;
2088 return (1);
2089 };
2090
2091 /*
2092 * XXX:
2093 * Maybe I should just use the fsb->f_mntonname path instead
2094 * of looping back up the dirp to the mount point??
2095 * Also, needs to know how to export all types of local
2096 * exportable file systems and not just MOUNT_FFS.
2097 */
2098 while (mount(fsb->f_fstypename, dirp,
2099 fsb->f_flag | MNT_UPDATE, &args) == -1) {
2100 if (cp)
2101 *cp-- = savedc;
2102 else
2103 cp = dirp + dirplen - 1;
2104 if (errno == EPERM) {
2105 syslog(LOG_ERR,
2106 "\"%s\", line %ld: Can't change attributes for %s to %s: %m",
2107 line, (unsigned long)lineno,
2108 dirp, (grp->gr_type == GT_HOST) ?
2109 grp->gr_ptr.gt_addrinfo->ai_canonname :
2110 (grp->gr_type == GT_NET) ?
2111 grp->gr_ptr.gt_net.nt_name :
2112 "Unknown");
2113 return (1);
2114 }
2115 if (opt_flags & OP_ALLDIRS) {
2116 syslog(LOG_ERR,
2117 "\"%s\", line %ld: Could not remount %s: %m",
2118 line, (unsigned long)lineno,
2119 dirp);
2120 return (1);
2121 }
2122 /* back up over the last component */
2123 while (*cp == '/' && cp > dirp)
2124 cp--;
2125 while (*(cp - 1) != '/' && cp > dirp)
2126 cp--;
2127 if (cp == dirp) {
2128 if (debug)
2129 (void)fprintf(stderr, "mnt unsucc\n");
2130 syslog(LOG_ERR,
2131 "\"%s\", line %ld: Can't export %s: %m",
2132 line, (unsigned long)lineno, dirp);
2133 return (1);
2134 }
2135 savedc = *cp;
2136 *cp = '\0';
2137 }
2138 skip:
2139 if (addrp) {
2140 ai = ai->ai_next;
2141 if (ai == NULL)
2142 done = TRUE;
2143 else {
2144 addrp = ai->ai_addr;
2145 addrlen = ai->ai_addrlen;
2146 }
2147 } else
2148 done = TRUE;
2149 }
2150 if (cp)
2151 *cp = savedc;
2152 return (0);
2153 }
2154
2155 /*
2156 * Translate a net address.
2157 */
2158 static int
2159 get_net(cp, net, maskflg)
2160 char *cp;
2161 struct netmsk *net;
2162 int maskflg;
2163 {
2164 struct netent *np;
2165 char *name, *p, *prefp;
2166 struct sockaddr_in sin, *sinp;
2167 struct sockaddr *sa;
2168 struct addrinfo hints, *ai = NULL;
2169 char netname[NI_MAXHOST];
2170 long preflen;
2171 int ecode;
2172
2173 (void)memset(&sin, 0, sizeof(sin));
2174 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2175 p = strchr(cp, '/');
2176 *p = '\0';
2177 prefp = p + 1;
2178 }
2179
2180 if ((np = getnetbyname(cp)) != NULL) {
2181 sin.sin_family = AF_INET;
2182 sin.sin_len = sizeof sin;
2183 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2184 sa = (struct sockaddr *)&sin;
2185 } else if (isdigit((unsigned char)*cp)) {
2186 memset(&hints, 0, sizeof hints);
2187 hints.ai_family = AF_UNSPEC;
2188 hints.ai_flags = AI_NUMERICHOST;
2189 if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
2190 /*
2191 * If getaddrinfo() failed, try the inet4 network
2192 * notation with less than 3 dots.
2193 */
2194 sin.sin_family = AF_INET;
2195 sin.sin_len = sizeof sin;
2196 sin.sin_addr = inet_makeaddr(inet_network(cp),0);
2197 if (debug)
2198 fprintf(stderr, "get_net: v4 addr %x\n",
2199 sin.sin_addr.s_addr);
2200 sa = (struct sockaddr *)&sin;
2201 } else
2202 sa = ai->ai_addr;
2203 } else if (isxdigit((unsigned char)*cp) || *cp == ':') {
2204 memset(&hints, 0, sizeof hints);
2205 hints.ai_family = AF_UNSPEC;
2206 hints.ai_flags = AI_NUMERICHOST;
2207 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2208 sa = ai->ai_addr;
2209 else
2210 goto fail;
2211 } else
2212 goto fail;
2213
2214 /*
2215 * Only allow /pref notation for v6 addresses.
2216 */
2217 if (sa->sa_family == AF_INET6 && (!(opt_flags & OP_MASKLEN) || maskflg))
2218 return 1;
2219
2220 ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2221 NULL, 0, ninumeric);
2222 if (ecode != 0)
2223 goto fail;
2224
2225 if (maskflg)
2226 net->nt_len = countones(sa);
2227 else {
2228 if (opt_flags & OP_MASKLEN) {
2229 errno = 0;
2230 preflen = strtol(prefp, NULL, 10);
2231 if (preflen == LONG_MIN && errno == ERANGE)
2232 goto fail;
2233 net->nt_len = (int)preflen;
2234 *p = '/';
2235 }
2236
2237 if (np)
2238 name = np->n_name;
2239 else {
2240 if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2241 NULL, 0, ninumeric) != 0)
2242 strlcpy(netname, "?", sizeof(netname));
2243 name = netname;
2244 }
2245 net->nt_name = estrdup(name);
2246 memcpy(&net->nt_net, sa, sa->sa_len);
2247 }
2248
2249 if (!maskflg && sa->sa_family == AF_INET &&
2250 !(opt_flags & (OP_MASK|OP_MASKLEN))) {
2251 sinp = (struct sockaddr_in *)sa;
2252 if (IN_CLASSA(sinp->sin_addr.s_addr))
2253 net->nt_len = 8;
2254 else if (IN_CLASSB(sinp->sin_addr.s_addr))
2255 net->nt_len = 16;
2256 else if (IN_CLASSC(sinp->sin_addr.s_addr))
2257 net->nt_len = 24;
2258 else if (IN_CLASSD(sinp->sin_addr.s_addr))
2259 net->nt_len = 28;
2260 else
2261 net->nt_len = 32; /* XXX */
2262 }
2263
2264 if (ai)
2265 freeaddrinfo(ai);
2266 return 0;
2267
2268 fail:
2269 if (ai)
2270 freeaddrinfo(ai);
2271 return 1;
2272 }
2273
2274 /*
2275 * Parse out the next white space separated field
2276 */
2277 static void
2278 nextfield(cp, endcp)
2279 char **cp;
2280 char **endcp;
2281 {
2282 char *p;
2283
2284 p = *cp;
2285 while (*p == ' ' || *p == '\t')
2286 p++;
2287 if (*p == '\n' || *p == '\0')
2288 *cp = *endcp = p;
2289 else {
2290 *cp = p++;
2291 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2292 p++;
2293 *endcp = p;
2294 }
2295 }
2296
2297 /*
2298 * Parse a description of a credential.
2299 */
2300 static void
2301 parsecred(namelist, cr)
2302 char *namelist;
2303 struct uucred *cr;
2304 {
2305 char *name;
2306 int cnt;
2307 char *names;
2308 struct passwd *pw;
2309 struct group *gr;
2310 int ngroups, groups[NGROUPS + 1];
2311
2312 /*
2313 * Set up the unprivileged user.
2314 */
2315 *cr = def_anon;
2316 /*
2317 * Get the user's password table entry.
2318 */
2319 names = strsep(&namelist, " \t\n");
2320 name = strsep(&names, ":");
2321 if (isdigit((unsigned char)*name) || *name == '-')
2322 pw = getpwuid(atoi(name));
2323 else
2324 pw = getpwnam(name);
2325 /*
2326 * Credentials specified as those of a user.
2327 */
2328 if (names == NULL) {
2329 if (pw == NULL) {
2330 syslog(LOG_ERR, "Unknown user: %s", name);
2331 return;
2332 }
2333 cr->cr_uid = pw->pw_uid;
2334 ngroups = NGROUPS + 1;
2335 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2336 syslog(LOG_ERR, "Too many groups");
2337 /*
2338 * Convert from int's to gid_t's and compress out duplicate
2339 */
2340 cr->cr_ngroups = ngroups - 1;
2341 cr->cr_gid = groups[0];
2342 for (cnt = 1; cnt < ngroups; cnt++)
2343 cr->cr_groups[cnt - 1] = groups[cnt];
2344 return;
2345 }
2346 /*
2347 * Explicit credential specified as a colon separated list:
2348 * uid:gid:gid:...
2349 */
2350 if (pw != NULL)
2351 cr->cr_uid = pw->pw_uid;
2352 else if (isdigit((unsigned char)*name) || *name == '-')
2353 cr->cr_uid = atoi(name);
2354 else {
2355 syslog(LOG_ERR, "Unknown user: %s", name);
2356 return;
2357 }
2358 cr->cr_ngroups = 0;
2359 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2360 name = strsep(&names, ":");
2361 if (isdigit((unsigned char)*name) || *name == '-') {
2362 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2363 } else {
2364 if ((gr = getgrnam(name)) == NULL) {
2365 syslog(LOG_ERR, "Unknown group: %s", name);
2366 continue;
2367 }
2368 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2369 }
2370 }
2371 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2372 syslog(LOG_ERR, "Too many groups");
2373 }
2374
2375 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2376 /*
2377 * Routines that maintain the remote mounttab
2378 */
2379 static void
2380 get_mountlist()
2381 {
2382 struct mountlist *mlp, **mlpp;
2383 char *host, *dirp, *cp;
2384 char str[STRSIZ];
2385 FILE *mlfile;
2386
2387 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2388 syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST);
2389 return;
2390 }
2391 mlpp = &mlhead;
2392 while (fgets(str, STRSIZ, mlfile) != NULL) {
2393 cp = str;
2394 host = strsep(&cp, " \t\n");
2395 dirp = strsep(&cp, " \t\n");
2396 if (host == NULL || dirp == NULL)
2397 continue;
2398 mlp = emalloc(sizeof(*mlp));
2399 (void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2400 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2401 (void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2402 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2403 mlp->ml_next = NULL;
2404 *mlpp = mlp;
2405 mlpp = &mlp->ml_next;
2406 }
2407 (void)fclose(mlfile);
2408 }
2409
2410 static int
2411 del_mlist(hostp, dirp, saddr)
2412 char *hostp, *dirp;
2413 struct sockaddr *saddr;
2414 {
2415 struct mountlist *mlp, **mlpp;
2416 struct mountlist *mlp2;
2417 u_short sport;
2418 FILE *mlfile;
2419 int fnd = 0, ret = 0;
2420 char host[NI_MAXHOST];
2421
2422 switch (saddr->sa_family) {
2423 case AF_INET6:
2424 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
2425 break;
2426 case AF_INET:
2427 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
2428 break;
2429 default:
2430 return -1;
2431 }
2432 mlpp = &mlhead;
2433 mlp = mlhead;
2434 while (mlp) {
2435 if (!strcmp(mlp->ml_host, hostp) &&
2436 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2437 if (!(mlp->ml_flag & DP_NORESMNT) &&
2438 sport >= IPPORT_RESERVED) {
2439 if (getnameinfo(saddr, saddr->sa_len, host,
2440 sizeof host, NULL, 0, ninumeric) != 0)
2441 strlcpy(host, "?", sizeof(host));
2442 syslog(LOG_NOTICE,
2443 "Umount request for %s:%s from %s refused\n",
2444 mlp->ml_host, mlp->ml_dirp, host);
2445 ret = -1;
2446 goto cont;
2447 }
2448 fnd = 1;
2449 mlp2 = mlp;
2450 *mlpp = mlp = mlp->ml_next;
2451 free(mlp2);
2452 } else {
2453 cont:
2454 mlpp = &mlp->ml_next;
2455 mlp = mlp->ml_next;
2456 }
2457 }
2458 if (fnd) {
2459 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2460 syslog(LOG_ERR, "Can't update %s: %m",
2461 _PATH_RMOUNTLIST);
2462 return ret;
2463 }
2464 mlp = mlhead;
2465 while (mlp) {
2466 (void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
2467 mlp->ml_dirp);
2468 mlp = mlp->ml_next;
2469 }
2470 (void)fclose(mlfile);
2471 }
2472 return ret;
2473 }
2474
2475 static void
2476 add_mlist(hostp, dirp, flags)
2477 char *hostp, *dirp;
2478 int flags;
2479 {
2480 struct mountlist *mlp, **mlpp;
2481 FILE *mlfile;
2482
2483 mlpp = &mlhead;
2484 mlp = mlhead;
2485 while (mlp) {
2486 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2487 return;
2488 mlpp = &mlp->ml_next;
2489 mlp = mlp->ml_next;
2490 }
2491 mlp = emalloc(sizeof(*mlp));
2492 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2493 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2494 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2495 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2496 mlp->ml_flag = flags;
2497 mlp->ml_next = NULL;
2498 *mlpp = mlp;
2499 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2500 syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST);
2501 return;
2502 }
2503 (void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2504 (void)fclose(mlfile);
2505 }
2506
2507 /*
2508 * This function is called via. SIGTERM when the system is going down.
2509 * It sends a broadcast RPCMNT_UMNTALL.
2510 */
2511 /* ARGSUSED */
2512 static void
2513 send_umntall(n)
2514 int n;
2515 {
2516 (void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
2517 xdr_void, NULL, xdr_void, NULL, (resultproc_t)umntall_each);
2518 exit(0);
2519 }
2520
2521 static int
2522 umntall_each(resultsp, raddr)
2523 caddr_t resultsp;
2524 struct sockaddr_in *raddr;
2525 {
2526 return (1);
2527 }
2528
2529 /*
2530 * Free up a group list.
2531 */
2532 static void
2533 free_grp(grp)
2534 struct grouplist *grp;
2535 {
2536
2537 if (grp->gr_type == GT_HOST) {
2538 if (grp->gr_ptr.gt_addrinfo != NULL)
2539 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2540 } else if (grp->gr_type == GT_NET) {
2541 if (grp->gr_ptr.gt_net.nt_name)
2542 free(grp->gr_ptr.gt_net.nt_name);
2543 }
2544 #ifdef ISO
2545 else if (grp->gr_type == GT_ISO)
2546 free(grp->gr_ptr.gt_isoaddr);
2547 #endif
2548 free(grp);
2549 }
2550
2551 #if 0
2552 static void
2553 SYSLOG(int pri, const char *fmt,...)
2554 {
2555 va_list ap;
2556
2557 va_start(ap, fmt);
2558
2559 if (debug)
2560 vfprintf(stderr, fmt, ap);
2561 else
2562 vsyslog(pri, fmt, ap);
2563
2564 va_end(ap);
2565 }
2566 #endif
2567
2568 /*
2569 * Check options for consistency.
2570 */
2571 static int
2572 check_options(line, lineno, dp)
2573 const char *line;
2574 size_t lineno;
2575 struct dirlist *dp;
2576 {
2577
2578 if (dp == NULL) {
2579 syslog(LOG_ERR,
2580 "\"%s\", line %ld: missing directory list",
2581 line, (unsigned long)lineno);
2582 return (1);
2583 }
2584 if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
2585 (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
2586 (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
2587 syslog(LOG_ERR,
2588 "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
2589 line, (unsigned long)lineno);
2590 return (1);
2591 }
2592 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2593 syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
2594 line, (unsigned long)lineno);
2595 return (1);
2596 }
2597 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN) != 0) {
2598 syslog(LOG_ERR, "\"%s\", line %ld: /pref and -mask mutually"
2599 " exclusive",
2600 line, (unsigned long)lineno);
2601 return (1);
2602 }
2603 if ((opt_flags & (OP_NET|OP_ISO)) == (OP_NET|OP_ISO)) {
2604 syslog(LOG_ERR,
2605 "\"%s\", line %ld: -net and -iso mutually exclusive",
2606 line, (unsigned long)lineno);
2607 return (1);
2608 }
2609 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2610 syslog(LOG_ERR,
2611 "\"%s\", line %ld: -alldir has multiple directories",
2612 line, (unsigned long)lineno);
2613 return (1);
2614 }
2615 return (0);
2616 }
2617
2618 /*
2619 * Check an absolute directory path for any symbolic links. Return true
2620 * if no symbolic links are found.
2621 */
2622 static int
2623 check_dirpath(line, lineno, dirp)
2624 const char *line;
2625 size_t lineno;
2626 char *dirp;
2627 {
2628 char *cp;
2629 struct stat sb;
2630 char *file = "";
2631
2632 for (cp = dirp + 1; *cp; cp++) {
2633 if (*cp == '/') {
2634 *cp = '\0';
2635 if (lstat(dirp, &sb) == -1)
2636 goto bad;
2637 if (!S_ISDIR(sb.st_mode))
2638 goto bad1;
2639 *cp = '/';
2640 }
2641 }
2642
2643 cp = NULL;
2644 if (lstat(dirp, &sb) == -1)
2645 goto bad;
2646
2647 if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
2648 file = " file or a";
2649 goto bad1;
2650 }
2651
2652 return 1;
2653
2654 bad:
2655 syslog(LOG_ERR,
2656 "\"%s\", line %ld: lstat for `%s' failed: %m",
2657 line, (unsigned long)lineno, dirp);
2658 if (cp)
2659 *cp = '/';
2660 return 0;
2661
2662 bad1:
2663 syslog(LOG_ERR,
2664 "\"%s\", line %ld: `%s' is not a%s directory",
2665 line, (unsigned long)lineno, dirp, file);
2666 if (cp)
2667 *cp = '/';
2668 return 0;
2669 }
2670