mountd.c revision 1.22 1 /* $NetBSD: mountd.c,v 1.22 1995/06/02 21:04:24 mycroft 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 static char copyright[] =
41 "@(#) Copyright (c) 1989, 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
43 #endif not lint
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)mountd.c 8.8 (Berkeley) 2/20/94";
48 #else
49 static char rcsid[] = "$NetBSD: mountd.c,v 1.22 1995/06/02 21:04:24 mycroft Exp $";
50 #endif
51 #endif not lint
52
53 #include <sys/param.h>
54 #include <sys/file.h>
55 #include <sys/ioctl.h>
56 #include <sys/mount.h>
57 #include <sys/socket.h>
58 #include <sys/stat.h>
59 #include <syslog.h>
60 #include <sys/ucred.h>
61
62 #include <rpc/rpc.h>
63 #include <rpc/pmap_clnt.h>
64 #include <rpc/pmap_prot.h>
65 #ifdef ISO
66 #include <netiso/iso.h>
67 #endif
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsv2.h>
70
71 #include <arpa/inet.h>
72
73 #include <ctype.h>
74 #include <errno.h>
75 #include <grp.h>
76 #include <netdb.h>
77 #include <pwd.h>
78 #include <signal.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include "pathnames.h"
84
85 #ifdef DEBUG
86 #include <stdarg.h>
87 #endif
88
89 /*
90 * Structures for keeping the mount list and export list
91 */
92 struct mountlist {
93 struct mountlist *ml_next;
94 char ml_host[RPCMNT_NAMELEN+1];
95 char ml_dirp[RPCMNT_PATHLEN+1];
96 };
97
98 struct dirlist {
99 struct dirlist *dp_left;
100 struct dirlist *dp_right;
101 int dp_flag;
102 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
103 char dp_dirp[1]; /* Actually malloc'd to size of dir */
104 };
105 /* dp_flag bits */
106 #define DP_DEFSET 0x1
107
108 struct exportlist {
109 struct exportlist *ex_next;
110 struct dirlist *ex_dirl;
111 struct dirlist *ex_defdir;
112 int ex_flag;
113 fsid_t ex_fs;
114 char *ex_fsdir;
115 };
116 /* ex_flag bits */
117 #define EX_LINKED 0x1
118
119 struct netmsk {
120 u_long nt_net;
121 u_long nt_mask;
122 char *nt_name;
123 };
124
125 union grouptypes {
126 struct hostent *gt_hostent;
127 struct netmsk gt_net;
128 #ifdef ISO
129 struct sockaddr_iso *gt_isoaddr;
130 #endif
131 };
132
133 struct grouplist {
134 int gr_type;
135 union grouptypes gr_ptr;
136 struct grouplist *gr_next;
137 };
138 /* Group types */
139 #define GT_NULL 0x0
140 #define GT_HOST 0x1
141 #define GT_NET 0x2
142 #define GT_ISO 0x4
143
144 struct hostlist {
145 struct grouplist *ht_grp;
146 struct hostlist *ht_next;
147 };
148
149 /* Global defs */
150 char *add_expdir __P((struct dirlist **, char *, int));
151 void add_dlist __P((struct dirlist **, struct dirlist *,
152 struct grouplist *));
153 void add_mlist __P((char *, char *));
154 int check_dirpath __P((char *));
155 int check_options __P((struct dirlist *));
156 int chk_host __P((struct dirlist *, u_long, int *));
157 void del_mlist __P((char *, char *));
158 struct dirlist *dirp_search __P((struct dirlist *, char *));
159 int do_mount __P((struct exportlist *, struct grouplist *, int,
160 struct ucred *, char *, int, struct statfs *));
161 int do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
162 int *, int *, struct ucred *));
163 struct exportlist *ex_search __P((fsid_t *));
164 struct exportlist *get_exp __P((void));
165 void free_dir __P((struct dirlist *));
166 void free_exp __P((struct exportlist *));
167 void free_grp __P((struct grouplist *));
168 void free_host __P((struct hostlist *));
169 void get_exportlist __P((void));
170 int get_host __P((char *, struct grouplist *));
171 struct hostlist *get_ht __P((void));
172 int get_line __P((void));
173 void get_mountlist __P((void));
174 int get_net __P((char *, struct netmsk *, int));
175 void getexp_err __P((struct exportlist *, struct grouplist *));
176 struct grouplist *get_grp __P((void));
177 void hang_dirp __P((struct dirlist *, struct grouplist *,
178 struct exportlist *, int));
179 void mntsrv __P((struct svc_req *, SVCXPRT *));
180 void nextfield __P((char **, char **));
181 void out_of_mem __P((void));
182 void parsecred __P((char *, struct ucred *));
183 int put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
184 int scan_tree __P((struct dirlist *, u_long));
185 void send_umntall __P((void));
186 int umntall_each __P((caddr_t, struct sockaddr_in *));
187 int xdr_dir __P((XDR *, char *));
188 int xdr_explist __P((XDR *, caddr_t));
189 int xdr_fhs __P((XDR *, nfsv2fh_t *));
190 int xdr_mlist __P((XDR *, caddr_t));
191
192 /* C library */
193 int getnetgrent();
194 void endnetgrent();
195 void setnetgrent();
196
197 #ifdef ISO
198 struct iso_addr *iso_addr();
199 #endif
200
201 struct exportlist *exphead;
202 struct mountlist *mlhead;
203 struct grouplist *grphead;
204 char exname[MAXPATHLEN];
205 struct ucred def_anon = {
206 1,
207 (uid_t) -2,
208 1,
209 { (gid_t) -2 }
210 };
211 int resvport_only = 1;
212 int opt_flags;
213 /* Bits for above */
214 #define OP_MAPROOT 0x01
215 #define OP_MAPALL 0x02
216 #define OP_KERB 0x04
217 #define OP_MASK 0x08
218 #define OP_NET 0x10
219 #define OP_ISO 0x20
220 #define OP_ALLDIRS 0x40
221
222 #ifdef DEBUG
223 int debug = 1;
224 void SYSLOG __P((int, const char *, ...));
225 #define syslog SYSLOG
226 #else
227 int debug = 0;
228 #endif
229
230 /*
231 * Mountd server for NFS mount protocol as described in:
232 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
233 * The optional arguments are the exports file name
234 * default: _PATH_EXPORTS
235 * and "-n" to allow nonroot mount.
236 */
237 int
238 main(argc, argv)
239 int argc;
240 char **argv;
241 {
242 SVCXPRT *udptransp, *tcptransp;
243 int c;
244
245 while ((c = getopt(argc, argv, "n")) != EOF)
246 switch (c) {
247 case 'n':
248 resvport_only = 0;
249 break;
250 default:
251 fprintf(stderr, "Usage: mountd [-n] [export_file]\n");
252 exit(1);
253 };
254 argc -= optind;
255 argv += optind;
256 grphead = (struct grouplist *)NULL;
257 exphead = (struct exportlist *)NULL;
258 mlhead = (struct mountlist *)NULL;
259 if (argc == 1) {
260 strncpy(exname, *argv, MAXPATHLEN-1);
261 exname[MAXPATHLEN-1] = '\0';
262 } else
263 strcpy(exname, _PATH_EXPORTS);
264 openlog("mountd", LOG_PID, LOG_DAEMON);
265 if (debug)
266 fprintf(stderr,"Getting export list.\n");
267 get_exportlist();
268 if (debug)
269 fprintf(stderr,"Getting mount list.\n");
270 get_mountlist();
271 if (debug)
272 fprintf(stderr,"Here we go.\n");
273 if (debug == 0) {
274 daemon(0, 0);
275 signal(SIGINT, SIG_IGN);
276 signal(SIGQUIT, SIG_IGN);
277 }
278 signal(SIGHUP, (void (*) __P((int))) get_exportlist);
279 signal(SIGTERM, (void (*) __P((int))) send_umntall);
280 { FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
281 if (pidfile != NULL) {
282 fprintf(pidfile, "%d\n", getpid());
283 fclose(pidfile);
284 }
285 }
286 if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
287 (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
288 syslog(LOG_ERR, "Can't create socket");
289 exit(1);
290 }
291 pmap_unset(RPCPROG_MNT, RPCMNT_VER1);
292 if (!svc_register(udptransp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
293 IPPROTO_UDP) ||
294 !svc_register(tcptransp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
295 IPPROTO_TCP)) {
296 syslog(LOG_ERR, "Can't register mount");
297 exit(1);
298 }
299 svc_run();
300 syslog(LOG_ERR, "Mountd died");
301 exit(1);
302 }
303
304 /*
305 * The mount rpc service
306 */
307 void
308 mntsrv(rqstp, transp)
309 struct svc_req *rqstp;
310 SVCXPRT *transp;
311 {
312 struct exportlist *ep;
313 struct dirlist *dp;
314 nfsv2fh_t nfh;
315 struct stat stb;
316 struct statfs fsb;
317 struct hostent *hp;
318 u_long saddr;
319 u_short sport;
320 char rpcpath[RPCMNT_PATHLEN+1], dirpath[MAXPATHLEN];
321 int bad = ENOENT, defset;
322 sigset_t sigset, osigset;
323
324 saddr = transp->xp_raddr.sin_addr.s_addr;
325 sport = ntohs(transp->xp_raddr.sin_port);
326 hp = (struct hostent *)NULL;
327 switch (rqstp->rq_proc) {
328 case NULLPROC:
329 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
330 syslog(LOG_ERR, "Can't send reply");
331 return;
332 case RPCMNT_MOUNT:
333 if (sport >= IPPORT_RESERVED && resvport_only) {
334 svcerr_weakauth(transp);
335 return;
336 }
337 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
338 svcerr_decode(transp);
339 return;
340 }
341
342 /*
343 * Get the real pathname and make sure it is a file or
344 * directory that exists.
345 */
346 if (realpath(rpcpath, dirpath) == 0 ||
347 stat(dirpath, &stb) < 0 ||
348 (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) ||
349 statfs(dirpath, &fsb) < 0) {
350 chdir("/"); /* Just in case realpath doesn't */
351 if (debug)
352 fprintf(stderr, "stat failed on %s\n", dirpath);
353 if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
354 syslog(LOG_ERR, "Can't send reply");
355 return;
356 }
357
358 /* Check in the exports list */
359 sigemptyset(&sigset);
360 sigaddset(&sigset, SIGHUP);
361 sigprocmask(SIG_BLOCK, &sigset, &osigset);
362 ep = ex_search(&fsb.f_fsid);
363 defset = 0;
364 if (ep && (chk_host(ep->ex_defdir, saddr, &defset) ||
365 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
366 chk_host(dp, saddr, &defset)) ||
367 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
368 scan_tree(ep->ex_dirl, saddr) == 0))) {
369 /* Get the file handle */
370 memset(&nfh, 0, sizeof(nfh));
371 if (getfh(dirpath, (fhandle_t *)&nfh) < 0) {
372 bad = errno;
373 syslog(LOG_ERR, "Can't get fh for %s", dirpath);
374 if (!svc_sendreply(transp, xdr_long,
375 (caddr_t)&bad))
376 syslog(LOG_ERR, "Can't send reply");
377 sigprocmask(SIG_SETMASK, &osigset, NULL);
378 return;
379 }
380 if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&nfh))
381 syslog(LOG_ERR, "Can't send reply");
382 if (hp == NULL)
383 hp = gethostbyaddr((caddr_t)&saddr,
384 sizeof(saddr), AF_INET);
385 if (hp)
386 add_mlist(hp->h_name, dirpath);
387 else
388 add_mlist(inet_ntoa(transp->xp_raddr.sin_addr),
389 dirpath);
390 if (debug)
391 fprintf(stderr,"Mount successfull.\n");
392 } else {
393 bad = EACCES;
394 if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
395 syslog(LOG_ERR, "Can't send reply");
396 }
397 sigprocmask(SIG_SETMASK, &osigset, NULL);
398 return;
399 case RPCMNT_DUMP:
400 if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
401 syslog(LOG_ERR, "Can't send reply");
402 return;
403 case RPCMNT_UMOUNT:
404 if (sport >= IPPORT_RESERVED && resvport_only) {
405 svcerr_weakauth(transp);
406 return;
407 }
408 if (!svc_getargs(transp, xdr_dir, dirpath)) {
409 svcerr_decode(transp);
410 return;
411 }
412 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
413 syslog(LOG_ERR, "Can't send reply");
414 hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
415 if (hp)
416 del_mlist(hp->h_name, dirpath);
417 del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), dirpath);
418 return;
419 case RPCMNT_UMNTALL:
420 if (sport >= IPPORT_RESERVED && resvport_only) {
421 svcerr_weakauth(transp);
422 return;
423 }
424 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
425 syslog(LOG_ERR, "Can't send reply");
426 hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
427 if (hp)
428 del_mlist(hp->h_name, (char *)NULL);
429 del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), (char *)NULL);
430 return;
431 case RPCMNT_EXPORT:
432 if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
433 syslog(LOG_ERR, "Can't send reply");
434 return;
435 default:
436 svcerr_noproc(transp);
437 return;
438 }
439 }
440
441 /*
442 * Xdr conversion for a dirpath string
443 */
444 int
445 xdr_dir(xdrsp, dirp)
446 XDR *xdrsp;
447 char *dirp;
448 {
449 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
450 }
451
452 /*
453 * Xdr routine to generate fhstatus
454 */
455 int
456 xdr_fhs(xdrsp, nfh)
457 XDR *xdrsp;
458 nfsv2fh_t *nfh;
459 {
460 long ok = 0;
461
462 if (!xdr_long(xdrsp, &ok))
463 return (0);
464 return (xdr_opaque(xdrsp, (caddr_t)nfh, NFSX_FH));
465 }
466
467 int
468 xdr_mlist(xdrsp, cp)
469 XDR *xdrsp;
470 caddr_t cp;
471 {
472 struct mountlist *mlp;
473 int true = 1;
474 int false = 0;
475 char *strp;
476
477 mlp = mlhead;
478 while (mlp) {
479 if (!xdr_bool(xdrsp, &true))
480 return (0);
481 strp = &mlp->ml_host[0];
482 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
483 return (0);
484 strp = &mlp->ml_dirp[0];
485 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
486 return (0);
487 mlp = mlp->ml_next;
488 }
489 if (!xdr_bool(xdrsp, &false))
490 return (0);
491 return (1);
492 }
493
494 /*
495 * Xdr conversion for export list
496 */
497 int
498 xdr_explist(xdrsp, cp)
499 XDR *xdrsp;
500 caddr_t cp;
501 {
502 struct exportlist *ep;
503 int false = 0;
504 int putdef;
505 sigset_t sigset, osigset;
506
507 sigemptyset(&sigset);
508 sigaddset(&sigset, SIGHUP);
509 sigprocmask(SIG_BLOCK, &sigset, &osigset);
510 ep = exphead;
511 while (ep) {
512 putdef = 0;
513 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
514 goto errout;
515 if (ep->ex_defdir && putdef == 0 &&
516 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
517 &putdef))
518 goto errout;
519 ep = ep->ex_next;
520 }
521 sigprocmask(SIG_SETMASK, &osigset, NULL);
522 if (!xdr_bool(xdrsp, &false))
523 return (0);
524 return (1);
525 errout:
526 sigprocmask(SIG_SETMASK, &osigset, NULL);
527 return (0);
528 }
529
530 /*
531 * Called from xdr_explist() to traverse the tree and export the
532 * directory paths.
533 */
534 int
535 put_exlist(dp, xdrsp, adp, putdefp)
536 struct dirlist *dp;
537 XDR *xdrsp;
538 struct dirlist *adp;
539 int *putdefp;
540 {
541 struct grouplist *grp;
542 struct hostlist *hp;
543 int true = 1;
544 int false = 0;
545 int gotalldir = 0;
546 char *strp;
547
548 if (dp) {
549 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
550 return (1);
551 if (!xdr_bool(xdrsp, &true))
552 return (1);
553 strp = dp->dp_dirp;
554 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
555 return (1);
556 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
557 gotalldir = 1;
558 *putdefp = 1;
559 }
560 if ((dp->dp_flag & DP_DEFSET) == 0 &&
561 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
562 hp = dp->dp_hosts;
563 while (hp) {
564 grp = hp->ht_grp;
565 if (grp->gr_type == GT_HOST) {
566 if (!xdr_bool(xdrsp, &true))
567 return (1);
568 strp = grp->gr_ptr.gt_hostent->h_name;
569 if (!xdr_string(xdrsp, &strp,
570 RPCMNT_NAMELEN))
571 return (1);
572 } else if (grp->gr_type == GT_NET) {
573 if (!xdr_bool(xdrsp, &true))
574 return (1);
575 strp = grp->gr_ptr.gt_net.nt_name;
576 if (!xdr_string(xdrsp, &strp,
577 RPCMNT_NAMELEN))
578 return (1);
579 }
580 hp = hp->ht_next;
581 if (gotalldir && hp == (struct hostlist *)NULL) {
582 hp = adp->dp_hosts;
583 gotalldir = 0;
584 }
585 }
586 }
587 if (!xdr_bool(xdrsp, &false))
588 return (1);
589 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
590 return (1);
591 }
592 return (0);
593 }
594
595 #define LINESIZ 10240
596 char line[LINESIZ];
597 FILE *exp_file;
598
599 /*
600 * Get the export list
601 */
602 void
603 get_exportlist()
604 {
605 struct exportlist *ep, *ep2;
606 struct grouplist *grp, *tgrp;
607 struct exportlist **epp;
608 struct dirlist *dirhead;
609 struct statfs fsb, *fsp;
610 struct hostent *hpe;
611 struct ucred anon;
612 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
613 int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
614
615 /*
616 * First, get rid of the old list
617 */
618 ep = exphead;
619 while (ep) {
620 ep2 = ep;
621 ep = ep->ex_next;
622 free_exp(ep2);
623 }
624 exphead = (struct exportlist *)NULL;
625
626 grp = grphead;
627 while (grp) {
628 tgrp = grp;
629 grp = grp->gr_next;
630 free_grp(tgrp);
631 }
632 grphead = (struct grouplist *)NULL;
633
634 /*
635 * And delete exports that are in the kernel for all local
636 * file systems.
637 * XXX: Should know how to handle all local exportable file systems
638 * instead of just MOUNT_UFS.
639 */
640 num = getmntinfo(&fsp, MNT_NOWAIT);
641 for (i = 0; i < num; i++) {
642 union {
643 struct ufs_args ua;
644 struct iso_args ia;
645 struct mfs_args ma;
646 struct msdosfs_args da;
647 } targs;
648
649 if (!strcmp(fsp->f_fstypename, MOUNT_MFS) ||
650 !strcmp(fsp->f_fstypename, MOUNT_UFS) ||
651 !strcmp(fsp->f_fstypename, MOUNT_MSDOS) ||
652 !strcmp(fsp->f_fstypename, MOUNT_CD9660)) {
653 targs.ua.fspec = NULL;
654 targs.ua.export.ex_flags = MNT_DELEXPORT;
655 if (mount(fsp->f_fstypename, fsp->f_mntonname,
656 fsp->f_flags | MNT_UPDATE,
657 (caddr_t)&targs) < 0)
658 syslog(LOG_ERR, "Can't delete exports for %s",
659 fsp->f_mntonname);
660 }
661 fsp++;
662 }
663
664 /*
665 * Read in the exports file and build the list, calling
666 * mount() as we go along to push the export rules into the kernel.
667 */
668 if ((exp_file = fopen(exname, "r")) == NULL) {
669 syslog(LOG_ERR, "Can't open %s", exname);
670 exit(2);
671 }
672 dirhead = (struct dirlist *)NULL;
673 while (get_line()) {
674 if (debug)
675 fprintf(stderr,"Got line %s\n",line);
676 cp = line;
677 nextfield(&cp, &endcp);
678 if (*cp == '#')
679 goto nextline;
680
681 /*
682 * Set defaults.
683 */
684 has_host = FALSE;
685 anon = def_anon;
686 exflags = MNT_EXPORTED;
687 got_nondir = 0;
688 opt_flags = 0;
689 ep = (struct exportlist *)NULL;
690
691 /*
692 * Create new exports list entry
693 */
694 len = endcp-cp;
695 tgrp = grp = get_grp();
696 while (len > 0) {
697 if (len > RPCMNT_NAMELEN) {
698 getexp_err(ep, tgrp);
699 goto nextline;
700 }
701 if (*cp == '-') {
702 if (ep == (struct exportlist *)NULL) {
703 getexp_err(ep, tgrp);
704 goto nextline;
705 }
706 if (debug)
707 fprintf(stderr, "doing opt %s\n", cp);
708 got_nondir = 1;
709 if (do_opt(&cp, &endcp, ep, grp, &has_host,
710 &exflags, &anon)) {
711 getexp_err(ep, tgrp);
712 goto nextline;
713 }
714 } else if (*cp == '/') {
715 savedc = *endcp;
716 *endcp = '\0';
717 if (check_dirpath(cp) &&
718 statfs(cp, &fsb) >= 0) {
719 if (got_nondir) {
720 syslog(LOG_ERR, "Dirs must be first");
721 getexp_err(ep, tgrp);
722 goto nextline;
723 }
724 if (ep) {
725 if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
726 ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
727 getexp_err(ep, tgrp);
728 goto nextline;
729 }
730 } else {
731 /*
732 * See if this directory is already
733 * in the list.
734 */
735 ep = ex_search(&fsb.f_fsid);
736 if (ep == (struct exportlist *)NULL) {
737 ep = get_exp();
738 ep->ex_fs = fsb.f_fsid;
739 ep->ex_fsdir = (char *)
740 malloc(strlen(fsb.f_mntonname) + 1);
741 if (ep->ex_fsdir)
742 strcpy(ep->ex_fsdir,
743 fsb.f_mntonname);
744 else
745 out_of_mem();
746 if (debug)
747 fprintf(stderr,
748 "Making new ep fs=0x%x,0x%x\n",
749 fsb.f_fsid.val[0],
750 fsb.f_fsid.val[1]);
751 } else if (debug)
752 fprintf(stderr,
753 "Found ep fs=0x%x,0x%x\n",
754 fsb.f_fsid.val[0],
755 fsb.f_fsid.val[1]);
756 }
757
758 /*
759 * Add dirpath to export mount point.
760 */
761 dirp = add_expdir(&dirhead, cp, len);
762 dirplen = len;
763 } else {
764 getexp_err(ep, tgrp);
765 goto nextline;
766 }
767 *endcp = savedc;
768 } else {
769 savedc = *endcp;
770 *endcp = '\0';
771 got_nondir = 1;
772 if (ep == (struct exportlist *)NULL) {
773 getexp_err(ep, tgrp);
774 goto nextline;
775 }
776
777 /*
778 * Get the host or netgroup.
779 */
780 setnetgrent(cp);
781 netgrp = getnetgrent(&hst, &usr, &dom);
782 do {
783 if (has_host) {
784 grp->gr_next = get_grp();
785 grp = grp->gr_next;
786 }
787 if (netgrp) {
788 if (get_host(hst, grp)) {
789 syslog(LOG_ERR, "Bad netgroup %s", cp);
790 getexp_err(ep, tgrp);
791 goto nextline;
792 }
793 } else if (get_host(cp, grp)) {
794 getexp_err(ep, tgrp);
795 goto nextline;
796 }
797 has_host = TRUE;
798 } while (netgrp && getnetgrent(&hst, &usr, &dom));
799 endnetgrent();
800 *endcp = savedc;
801 }
802 cp = endcp;
803 nextfield(&cp, &endcp);
804 len = endcp - cp;
805 }
806 if (check_options(dirhead)) {
807 getexp_err(ep, tgrp);
808 goto nextline;
809 }
810 if (!has_host) {
811 grp->gr_type = GT_HOST;
812 if (debug)
813 fprintf(stderr,"Adding a default entry\n");
814 /* add a default group and make the grp list NULL */
815 hpe = (struct hostent *)malloc(sizeof(struct hostent));
816 if (hpe == (struct hostent *)NULL)
817 out_of_mem();
818 hpe->h_name = "Default";
819 hpe->h_addrtype = AF_INET;
820 hpe->h_length = sizeof (u_long);
821 hpe->h_addr_list = (char **)NULL;
822 grp->gr_ptr.gt_hostent = hpe;
823
824 /*
825 * Don't allow a network export coincide with a list of
826 * host(s) on the same line.
827 */
828 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
829 getexp_err(ep, tgrp);
830 goto nextline;
831 }
832
833 /*
834 * Loop through hosts, pushing the exports into the kernel.
835 * After loop, tgrp points to the start of the list and
836 * grp points to the last entry in the list.
837 */
838 grp = tgrp;
839 do {
840 if (do_mount(ep, grp, exflags, &anon, dirp,
841 dirplen, &fsb)) {
842 getexp_err(ep, tgrp);
843 goto nextline;
844 }
845 } while (grp->gr_next && (grp = grp->gr_next));
846
847 /*
848 * Success. Update the data structures.
849 */
850 if (has_host) {
851 hang_dirp(dirhead, tgrp, ep, (opt_flags & OP_ALLDIRS));
852 grp->gr_next = grphead;
853 grphead = tgrp;
854 } else {
855 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
856 (opt_flags & OP_ALLDIRS));
857 free_grp(grp);
858 }
859 dirhead = (struct dirlist *)NULL;
860 if ((ep->ex_flag & EX_LINKED) == 0) {
861 ep2 = exphead;
862 epp = &exphead;
863
864 /*
865 * Insert in the list in alphabetical order.
866 */
867 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
868 epp = &ep2->ex_next;
869 ep2 = ep2->ex_next;
870 }
871 if (ep2)
872 ep->ex_next = ep2;
873 *epp = ep;
874 ep->ex_flag |= EX_LINKED;
875 }
876 nextline:
877 if (dirhead) {
878 free_dir(dirhead);
879 dirhead = (struct dirlist *)NULL;
880 }
881 }
882 fclose(exp_file);
883 }
884
885 /*
886 * Allocate an export list element
887 */
888 struct exportlist *
889 get_exp()
890 {
891 struct exportlist *ep;
892
893 ep = (struct exportlist *)malloc(sizeof (struct exportlist));
894 if (ep == (struct exportlist *)NULL)
895 out_of_mem();
896 memset(ep, 0, sizeof(struct exportlist));
897 return (ep);
898 }
899
900 /*
901 * Allocate a group list element
902 */
903 struct grouplist *
904 get_grp()
905 {
906 struct grouplist *gp;
907
908 gp = (struct grouplist *)malloc(sizeof (struct grouplist));
909 if (gp == (struct grouplist *)NULL)
910 out_of_mem();
911 memset(gp, 0, sizeof(struct grouplist));
912 return (gp);
913 }
914
915 /*
916 * Clean up upon an error in get_exportlist().
917 */
918 void
919 getexp_err(ep, grp)
920 struct exportlist *ep;
921 struct grouplist *grp;
922 {
923 struct grouplist *tgrp;
924
925 syslog(LOG_ERR, "Bad exports list line %s", line);
926 if (ep && (ep->ex_flag & EX_LINKED) == 0)
927 free_exp(ep);
928 while (grp) {
929 tgrp = grp;
930 grp = grp->gr_next;
931 free_grp(tgrp);
932 }
933 }
934
935 /*
936 * Search the export list for a matching fs.
937 */
938 struct exportlist *
939 ex_search(fsid)
940 fsid_t *fsid;
941 {
942 struct exportlist *ep;
943
944 ep = exphead;
945 while (ep) {
946 if (ep->ex_fs.val[0] == fsid->val[0] &&
947 ep->ex_fs.val[1] == fsid->val[1])
948 return (ep);
949 ep = ep->ex_next;
950 }
951 return (ep);
952 }
953
954 /*
955 * Add a directory path to the list.
956 */
957 char *
958 add_expdir(dpp, cp, len)
959 struct dirlist **dpp;
960 char *cp;
961 int len;
962 {
963 struct dirlist *dp;
964
965 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
966 dp->dp_left = *dpp;
967 dp->dp_right = (struct dirlist *)NULL;
968 dp->dp_flag = 0;
969 dp->dp_hosts = (struct hostlist *)NULL;
970 strcpy(dp->dp_dirp, cp);
971 *dpp = dp;
972 return (dp->dp_dirp);
973 }
974
975 /*
976 * Hang the dir list element off the dirpath binary tree as required
977 * and update the entry for host.
978 */
979 void
980 hang_dirp(dp, grp, ep, alldirs)
981 struct dirlist *dp;
982 struct grouplist *grp;
983 struct exportlist *ep;
984 int alldirs;
985 {
986 struct hostlist *hp;
987 struct dirlist *dp2;
988
989 if (alldirs) {
990 if (ep->ex_defdir)
991 free((caddr_t)dp);
992 else
993 ep->ex_defdir = dp;
994 if (grp == (struct grouplist *)NULL)
995 ep->ex_defdir->dp_flag |= DP_DEFSET;
996 else while (grp) {
997 hp = get_ht();
998 hp->ht_grp = grp;
999 hp->ht_next = ep->ex_defdir->dp_hosts;
1000 ep->ex_defdir->dp_hosts = hp;
1001 grp = grp->gr_next;
1002 }
1003 } else {
1004
1005 /*
1006 * Loop throught the directories adding them to the tree.
1007 */
1008 while (dp) {
1009 dp2 = dp->dp_left;
1010 add_dlist(&ep->ex_dirl, dp, grp);
1011 dp = dp2;
1012 }
1013 }
1014 }
1015
1016 /*
1017 * Traverse the binary tree either updating a node that is already there
1018 * for the new directory or adding the new node.
1019 */
1020 void
1021 add_dlist(dpp, newdp, grp)
1022 struct dirlist **dpp;
1023 struct dirlist *newdp;
1024 struct grouplist *grp;
1025 {
1026 struct dirlist *dp;
1027 struct hostlist *hp;
1028 int cmp;
1029
1030 dp = *dpp;
1031 if (dp) {
1032 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1033 if (cmp > 0) {
1034 add_dlist(&dp->dp_left, newdp, grp);
1035 return;
1036 } else if (cmp < 0) {
1037 add_dlist(&dp->dp_right, newdp, grp);
1038 return;
1039 } else
1040 free((caddr_t)newdp);
1041 } else {
1042 dp = newdp;
1043 dp->dp_left = (struct dirlist *)NULL;
1044 *dpp = dp;
1045 }
1046 if (grp) {
1047
1048 /*
1049 * Hang all of the host(s) off of the directory point.
1050 */
1051 do {
1052 hp = get_ht();
1053 hp->ht_grp = grp;
1054 hp->ht_next = dp->dp_hosts;
1055 dp->dp_hosts = hp;
1056 grp = grp->gr_next;
1057 } while (grp);
1058 } else
1059 dp->dp_flag |= DP_DEFSET;
1060 }
1061
1062 /*
1063 * Search for a dirpath on the export point.
1064 */
1065 struct dirlist *
1066 dirp_search(dp, dirpath)
1067 struct dirlist *dp;
1068 char *dirpath;
1069 {
1070 int cmp;
1071
1072 if (dp) {
1073 cmp = strcmp(dp->dp_dirp, dirpath);
1074 if (cmp > 0)
1075 return (dirp_search(dp->dp_left, dirpath));
1076 else if (cmp < 0)
1077 return (dirp_search(dp->dp_right, dirpath));
1078 else
1079 return (dp);
1080 }
1081 return (dp);
1082 }
1083
1084 /*
1085 * Scan for a host match in a directory tree.
1086 */
1087 int
1088 chk_host(dp, saddr, defsetp)
1089 struct dirlist *dp;
1090 u_long saddr;
1091 int *defsetp;
1092 {
1093 struct hostlist *hp;
1094 struct grouplist *grp;
1095 u_long **addrp;
1096
1097 if (dp) {
1098 if (dp->dp_flag & DP_DEFSET)
1099 *defsetp = 1;
1100 hp = dp->dp_hosts;
1101 while (hp) {
1102 grp = hp->ht_grp;
1103 switch (grp->gr_type) {
1104 case GT_HOST:
1105 addrp = (u_long **)
1106 grp->gr_ptr.gt_hostent->h_addr_list;
1107 while (*addrp) {
1108 if (**addrp == saddr)
1109 return (1);
1110 addrp++;
1111 }
1112 break;
1113 case GT_NET:
1114 if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
1115 grp->gr_ptr.gt_net.nt_net)
1116 return (1);
1117 break;
1118 };
1119 hp = hp->ht_next;
1120 }
1121 }
1122 return (0);
1123 }
1124
1125 /*
1126 * Scan tree for a host that matches the address.
1127 */
1128 int
1129 scan_tree(dp, saddr)
1130 struct dirlist *dp;
1131 u_long saddr;
1132 {
1133 int defset;
1134
1135 if (dp) {
1136 if (scan_tree(dp->dp_left, saddr))
1137 return (1);
1138 if (chk_host(dp, saddr, &defset))
1139 return (1);
1140 if (scan_tree(dp->dp_right, saddr))
1141 return (1);
1142 }
1143 return (0);
1144 }
1145
1146 /*
1147 * Traverse the dirlist tree and free it up.
1148 */
1149 void
1150 free_dir(dp)
1151 struct dirlist *dp;
1152 {
1153
1154 if (dp) {
1155 free_dir(dp->dp_left);
1156 free_dir(dp->dp_right);
1157 free_host(dp->dp_hosts);
1158 free((caddr_t)dp);
1159 }
1160 }
1161
1162 /*
1163 * Parse the option string and update fields.
1164 * Option arguments may either be -<option>=<value> or
1165 * -<option> <value>
1166 */
1167 int
1168 do_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
1169 char **cpp, **endcpp;
1170 struct exportlist *ep;
1171 struct grouplist *grp;
1172 int *has_hostp;
1173 int *exflagsp;
1174 struct ucred *cr;
1175 {
1176 char *cpoptarg, *cpoptend;
1177 char *cp, *endcp, *cpopt, savedc, savedc2;
1178 int allflag, usedarg;
1179
1180 cpopt = *cpp;
1181 cpopt++;
1182 cp = *endcpp;
1183 savedc = *cp;
1184 *cp = '\0';
1185 while (cpopt && *cpopt) {
1186 allflag = 1;
1187 usedarg = -2;
1188 if (cpoptend = strchr(cpopt, ',')) {
1189 *cpoptend++ = '\0';
1190 if (cpoptarg = strchr(cpopt, '='))
1191 *cpoptarg++ = '\0';
1192 } else {
1193 if (cpoptarg = strchr(cpopt, '='))
1194 *cpoptarg++ = '\0';
1195 else {
1196 *cp = savedc;
1197 nextfield(&cp, &endcp);
1198 **endcpp = '\0';
1199 if (endcp > cp && *cp != '-') {
1200 cpoptarg = cp;
1201 savedc2 = *endcp;
1202 *endcp = '\0';
1203 usedarg = 0;
1204 }
1205 }
1206 }
1207 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1208 *exflagsp |= MNT_EXRDONLY;
1209 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1210 !(allflag = strcmp(cpopt, "mapall")) ||
1211 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1212 usedarg++;
1213 parsecred(cpoptarg, cr);
1214 if (allflag == 0) {
1215 *exflagsp |= MNT_EXPORTANON;
1216 opt_flags |= OP_MAPALL;
1217 } else
1218 opt_flags |= OP_MAPROOT;
1219 } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
1220 *exflagsp |= MNT_EXKERB;
1221 opt_flags |= OP_KERB;
1222 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1223 !strcmp(cpopt, "m"))) {
1224 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1225 syslog(LOG_ERR, "Bad mask: %s", cpoptarg);
1226 return (1);
1227 }
1228 usedarg++;
1229 opt_flags |= OP_MASK;
1230 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1231 !strcmp(cpopt, "n"))) {
1232 if (grp->gr_type != GT_NULL) {
1233 syslog(LOG_ERR, "Network/host conflict");
1234 return (1);
1235 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1236 syslog(LOG_ERR, "Bad net: %s", cpoptarg);
1237 return (1);
1238 }
1239 grp->gr_type = GT_NET;
1240 *has_hostp = 1;
1241 usedarg++;
1242 opt_flags |= OP_NET;
1243 } else if (!strcmp(cpopt, "alldirs")) {
1244 opt_flags |= OP_ALLDIRS;
1245 #ifdef ISO
1246 } else if (cpoptarg && !strcmp(cpopt, "iso")) {
1247 if (get_isoaddr(cpoptarg, grp)) {
1248 syslog(LOG_ERR, "Bad iso addr: %s", cpoptarg);
1249 return (1);
1250 }
1251 *has_hostp = 1;
1252 usedarg++;
1253 opt_flags |= OP_ISO;
1254 #endif /* ISO */
1255 } else {
1256 syslog(LOG_ERR, "Bad opt %s", cpopt);
1257 return (1);
1258 }
1259 if (usedarg >= 0) {
1260 *endcp = savedc2;
1261 **endcpp = savedc;
1262 if (usedarg > 0) {
1263 *cpp = cp;
1264 *endcpp = endcp;
1265 }
1266 return (0);
1267 }
1268 cpopt = cpoptend;
1269 }
1270 **endcpp = savedc;
1271 return (0);
1272 }
1273
1274 /*
1275 * Translate a character string to the corresponding list of network
1276 * addresses for a hostname.
1277 */
1278 int
1279 get_host(cp, grp)
1280 char *cp;
1281 struct grouplist *grp;
1282 {
1283 struct hostent *hp, *nhp;
1284 char **addrp, **naddrp;
1285 struct hostent t_host;
1286 int i;
1287 u_long saddr;
1288 char *aptr[2];
1289
1290 if (grp->gr_type != GT_NULL)
1291 return (1);
1292 if ((hp = gethostbyname(cp)) == NULL) {
1293 if (isdigit(*cp)) {
1294 saddr = inet_addr(cp);
1295 if (saddr == -1) {
1296 syslog(LOG_ERR, "Inet_addr failed");
1297 return (1);
1298 }
1299 if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
1300 AF_INET)) == NULL) {
1301 hp = &t_host;
1302 hp->h_name = cp;
1303 hp->h_addrtype = AF_INET;
1304 hp->h_length = sizeof (u_long);
1305 hp->h_addr_list = aptr;
1306 aptr[0] = (char *)&saddr;
1307 aptr[1] = (char *)NULL;
1308 }
1309 } else {
1310 syslog(LOG_ERR, "Gethostbyname failed");
1311 return (1);
1312 }
1313 }
1314 grp->gr_type = GT_HOST;
1315 nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
1316 malloc(sizeof(struct hostent));
1317 if (nhp == (struct hostent *)NULL)
1318 out_of_mem();
1319 memcpy(nhp, hp, sizeof(struct hostent));
1320 i = strlen(hp->h_name)+1;
1321 nhp->h_name = (char *)malloc(i);
1322 if (nhp->h_name == (char *)NULL)
1323 out_of_mem();
1324 memcpy(nhp->h_name, hp->h_name, i);
1325 addrp = hp->h_addr_list;
1326 i = 1;
1327 while (*addrp++)
1328 i++;
1329 naddrp = nhp->h_addr_list = (char **)
1330 malloc(i*sizeof(char *));
1331 if (naddrp == (char **)NULL)
1332 out_of_mem();
1333 addrp = hp->h_addr_list;
1334 while (*addrp) {
1335 *naddrp = (char *)
1336 malloc(hp->h_length);
1337 if (*naddrp == (char *)NULL)
1338 out_of_mem();
1339 memcpy(*naddrp, *addrp, hp->h_length);
1340 addrp++;
1341 naddrp++;
1342 }
1343 *naddrp = (char *)NULL;
1344 if (debug)
1345 fprintf(stderr, "got host %s\n", hp->h_name);
1346 return (0);
1347 }
1348
1349 /*
1350 * Free up an exports list component
1351 */
1352 void
1353 free_exp(ep)
1354 struct exportlist *ep;
1355 {
1356
1357 if (ep->ex_defdir) {
1358 free_host(ep->ex_defdir->dp_hosts);
1359 free((caddr_t)ep->ex_defdir);
1360 }
1361 if (ep->ex_fsdir)
1362 free(ep->ex_fsdir);
1363 free_dir(ep->ex_dirl);
1364 free((caddr_t)ep);
1365 }
1366
1367 /*
1368 * Free hosts.
1369 */
1370 void
1371 free_host(hp)
1372 struct hostlist *hp;
1373 {
1374 struct hostlist *hp2;
1375
1376 while (hp) {
1377 hp2 = hp;
1378 hp = hp->ht_next;
1379 free((caddr_t)hp2);
1380 }
1381 }
1382
1383 struct hostlist *
1384 get_ht()
1385 {
1386 struct hostlist *hp;
1387
1388 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
1389 if (hp == (struct hostlist *)NULL)
1390 out_of_mem();
1391 hp->ht_next = (struct hostlist *)NULL;
1392 return (hp);
1393 }
1394
1395 #ifdef ISO
1396 /*
1397 * Translate an iso address.
1398 */
1399 get_isoaddr(cp, grp)
1400 char *cp;
1401 struct grouplist *grp;
1402 {
1403 struct iso_addr *isop;
1404 struct sockaddr_iso *isoaddr;
1405
1406 if (grp->gr_type != GT_NULL)
1407 return (1);
1408 if ((isop = iso_addr(cp)) == NULL) {
1409 syslog(LOG_ERR,
1410 "iso_addr failed, ignored");
1411 return (1);
1412 }
1413 isoaddr = (struct sockaddr_iso *)
1414 malloc(sizeof (struct sockaddr_iso));
1415 if (isoaddr == (struct sockaddr_iso *)NULL)
1416 out_of_mem();
1417 memset(isoaddr, 0, sizeof(struct sockaddr_iso));
1418 memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
1419 isoaddr->siso_len = sizeof(struct sockaddr_iso);
1420 isoaddr->siso_family = AF_ISO;
1421 grp->gr_type = GT_ISO;
1422 grp->gr_ptr.gt_isoaddr = isoaddr;
1423 return (0);
1424 }
1425 #endif /* ISO */
1426
1427 /*
1428 * Out of memory, fatal
1429 */
1430 void
1431 out_of_mem()
1432 {
1433
1434 syslog(LOG_ERR, "Out of memory");
1435 exit(2);
1436 }
1437
1438 /*
1439 * Do the mount syscall with the update flag to push the export info into
1440 * the kernel.
1441 */
1442 int
1443 do_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
1444 struct exportlist *ep;
1445 struct grouplist *grp;
1446 int exflags;
1447 struct ucred *anoncrp;
1448 char *dirp;
1449 int dirplen;
1450 struct statfs *fsb;
1451 {
1452 char *cp = (char *)NULL;
1453 u_long **addrp;
1454 int done;
1455 char savedc = '\0';
1456 struct sockaddr_in sin, imask;
1457 union {
1458 struct ufs_args ua;
1459 struct iso_args ia;
1460 struct mfs_args ma;
1461 struct msdosfs_args da;
1462 } args;
1463 u_long net;
1464
1465 args.ua.fspec = 0;
1466 args.ua.export.ex_flags = exflags;
1467 args.ua.export.ex_anon = *anoncrp;
1468 memset(&sin, 0, sizeof(sin));
1469 memset(&imask, 0, sizeof(imask));
1470 sin.sin_family = AF_INET;
1471 sin.sin_len = sizeof(sin);
1472 imask.sin_family = AF_INET;
1473 imask.sin_len = sizeof(sin);
1474 if (grp->gr_type == GT_HOST)
1475 addrp = (u_long **)grp->gr_ptr.gt_hostent->h_addr_list;
1476 else
1477 addrp = (u_long **)NULL;
1478 done = FALSE;
1479 while (!done) {
1480 switch (grp->gr_type) {
1481 case GT_HOST:
1482 if (addrp) {
1483 sin.sin_addr.s_addr = **addrp;
1484 args.ua.export.ex_addrlen = sizeof(sin);
1485 } else
1486 args.ua.export.ex_addrlen = 0;
1487 args.ua.export.ex_addr = (struct sockaddr *)&sin;
1488 args.ua.export.ex_masklen = 0;
1489 break;
1490 case GT_NET:
1491 if (grp->gr_ptr.gt_net.nt_mask)
1492 imask.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_mask;
1493 else {
1494 net = ntohl(grp->gr_ptr.gt_net.nt_net);
1495 if (IN_CLASSA(net))
1496 imask.sin_addr.s_addr = inet_addr("255.0.0.0");
1497 else if (IN_CLASSB(net))
1498 imask.sin_addr.s_addr =
1499 inet_addr("255.255.0.0");
1500 else
1501 imask.sin_addr.s_addr =
1502 inet_addr("255.255.255.0");
1503 grp->gr_ptr.gt_net.nt_mask = imask.sin_addr.s_addr;
1504 }
1505 sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
1506 args.ua.export.ex_addr = (struct sockaddr *)&sin;
1507 args.ua.export.ex_addrlen = sizeof (sin);
1508 args.ua.export.ex_mask = (struct sockaddr *)&imask;
1509 args.ua.export.ex_masklen = sizeof (imask);
1510 break;
1511 #ifdef ISO
1512 case GT_ISO:
1513 args.ua.export.ex_addr =
1514 (struct sockaddr *)grp->gr_ptr.gt_isoaddr;
1515 args.ua.export.ex_addrlen =
1516 sizeof(struct sockaddr_iso);
1517 args.ua.export.ex_masklen = 0;
1518 break;
1519 #endif /* ISO */
1520 default:
1521 syslog(LOG_ERR, "Bad grouptype");
1522 if (cp)
1523 *cp = savedc;
1524 return (1);
1525 };
1526
1527 /*
1528 * XXX:
1529 * Maybe I should just use the fsb->f_mntonname path instead
1530 * of looping back up the dirp to the mount point??
1531 * Also, needs to know how to export all types of local
1532 * exportable file systems and not just MOUNT_UFS.
1533 */
1534 while (mount(fsb->f_fstypename, dirp,
1535 fsb->f_flags | MNT_UPDATE, (caddr_t)&args) < 0) {
1536 if (cp)
1537 *cp-- = savedc;
1538 else
1539 cp = dirp + dirplen - 1;
1540 if (errno == EPERM) {
1541 syslog(LOG_ERR,
1542 "Can't change attributes for %s.\n", dirp);
1543 return (1);
1544 }
1545 if (opt_flags & OP_ALLDIRS) {
1546 syslog(LOG_ERR, "Not root dir");
1547 return (1);
1548 }
1549 /* back up over the last component */
1550 while (*cp == '/' && cp > dirp)
1551 cp--;
1552 while (*(cp - 1) != '/' && cp > dirp)
1553 cp--;
1554 if (cp == dirp) {
1555 if (debug)
1556 fprintf(stderr,"mnt unsucc\n");
1557 syslog(LOG_ERR, "Can't export %s", dirp);
1558 return (1);
1559 }
1560 savedc = *cp;
1561 *cp = '\0';
1562 }
1563 if (addrp) {
1564 ++addrp;
1565 if (*addrp == (u_long *)NULL)
1566 done = TRUE;
1567 } else
1568 done = TRUE;
1569 }
1570 if (cp)
1571 *cp = savedc;
1572 return (0);
1573 }
1574
1575 /*
1576 * Translate a net address.
1577 */
1578 int
1579 get_net(cp, net, maskflg)
1580 char *cp;
1581 struct netmsk *net;
1582 int maskflg;
1583 {
1584 struct netent *np;
1585 long netaddr;
1586 struct in_addr inetaddr, inetaddr2;
1587 char *name;
1588
1589 if (np = getnetbyname(cp))
1590 inetaddr = inet_makeaddr(np->n_net, 0);
1591 else if (isdigit(*cp)) {
1592 if ((netaddr = inet_network(cp)) == -1)
1593 return (1);
1594 inetaddr = inet_makeaddr(netaddr, 0);
1595 /*
1596 * Due to arbritrary subnet masks, you don't know how many
1597 * bits to shift the address to make it into a network,
1598 * however you do know how to make a network address into
1599 * a host with host == 0 and then compare them.
1600 * (What a pest)
1601 */
1602 if (!maskflg) {
1603 setnetent(0);
1604 while (np = getnetent()) {
1605 inetaddr2 = inet_makeaddr(np->n_net, 0);
1606 if (inetaddr2.s_addr == inetaddr.s_addr)
1607 break;
1608 }
1609 endnetent();
1610 }
1611 } else
1612 return (1);
1613 if (maskflg)
1614 net->nt_mask = inetaddr.s_addr;
1615 else {
1616 if (np)
1617 name = np->n_name;
1618 else
1619 name = inet_ntoa(inetaddr);
1620 net->nt_name = (char *)malloc(strlen(name) + 1);
1621 if (net->nt_name == (char *)NULL)
1622 out_of_mem();
1623 strcpy(net->nt_name, name);
1624 net->nt_net = inetaddr.s_addr;
1625 }
1626 return (0);
1627 }
1628
1629 /*
1630 * Parse out the next white space separated field
1631 */
1632 void
1633 nextfield(cp, endcp)
1634 char **cp;
1635 char **endcp;
1636 {
1637 char *p;
1638
1639 p = *cp;
1640 while (*p == ' ' || *p == '\t')
1641 p++;
1642 if (*p == '\n' || *p == '\0')
1643 *cp = *endcp = p;
1644 else {
1645 *cp = p++;
1646 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
1647 p++;
1648 *endcp = p;
1649 }
1650 }
1651
1652 /*
1653 * Get an exports file line. Skip over blank lines and handle line
1654 * continuations.
1655 */
1656 int
1657 get_line()
1658 {
1659 char *p, *cp;
1660 int len;
1661 int totlen, cont_line;
1662
1663 /*
1664 * Loop around ignoring blank lines and getting all continuation lines.
1665 */
1666 p = line;
1667 totlen = 0;
1668 do {
1669 if (fgets(p, LINESIZ - totlen, exp_file) == NULL)
1670 return (0);
1671 len = strlen(p);
1672 cp = p + len - 1;
1673 cont_line = 0;
1674 while (cp >= p &&
1675 (*cp == ' ' || *cp == '\t' || *cp == '\n' || *cp == '\\')) {
1676 if (*cp == '\\')
1677 cont_line = 1;
1678 cp--;
1679 len--;
1680 }
1681 *++cp = '\0';
1682 if (len > 0) {
1683 totlen += len;
1684 if (totlen >= LINESIZ) {
1685 syslog(LOG_ERR, "Exports line too long");
1686 exit(2);
1687 }
1688 p = cp;
1689 }
1690 } while (totlen == 0 || cont_line);
1691 return (1);
1692 }
1693
1694 /*
1695 * Parse a description of a credential.
1696 */
1697 void
1698 parsecred(namelist, cr)
1699 char *namelist;
1700 struct ucred *cr;
1701 {
1702 char *name;
1703 int cnt;
1704 char *names;
1705 struct passwd *pw;
1706 struct group *gr;
1707 int ngroups, groups[NGROUPS + 1];
1708
1709 /*
1710 * Set up the unpriviledged user.
1711 */
1712 cr->cr_ref = 1;
1713 cr->cr_uid = -2;
1714 cr->cr_gid = -2;
1715 cr->cr_ngroups = 0;
1716 /*
1717 * Get the user's password table entry.
1718 */
1719 names = strsep(&namelist, " \t\n");
1720 name = strsep(&names, ":");
1721 if (isdigit(*name) || *name == '-')
1722 pw = getpwuid(atoi(name));
1723 else
1724 pw = getpwnam(name);
1725 /*
1726 * Credentials specified as those of a user.
1727 */
1728 if (names == NULL) {
1729 if (pw == NULL) {
1730 syslog(LOG_ERR, "Unknown user: %s", name);
1731 return;
1732 }
1733 cr->cr_uid = pw->pw_uid;
1734 ngroups = NGROUPS + 1;
1735 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
1736 syslog(LOG_ERR, "Too many groups");
1737 /*
1738 * Convert from int's to gid_t's and compress out duplicate
1739 */
1740 cr->cr_ngroups = ngroups - 1;
1741 cr->cr_gid = groups[0];
1742 for (cnt = 1; cnt < ngroups; cnt++)
1743 cr->cr_groups[cnt - 1] = groups[cnt];
1744 return;
1745 }
1746 /*
1747 * Explicit credential specified as a colon separated list:
1748 * uid:gid:gid:...
1749 */
1750 if (pw != NULL)
1751 cr->cr_uid = pw->pw_uid;
1752 else if (isdigit(*name) || *name == '-')
1753 cr->cr_uid = atoi(name);
1754 else {
1755 syslog(LOG_ERR, "Unknown user: %s", name);
1756 return;
1757 }
1758 cr->cr_ngroups = 0;
1759 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
1760 name = strsep(&names, ":");
1761 if (isdigit(*name) || *name == '-') {
1762 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
1763 } else {
1764 if ((gr = getgrnam(name)) == NULL) {
1765 syslog(LOG_ERR, "Unknown group: %s", name);
1766 continue;
1767 }
1768 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
1769 }
1770 }
1771 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
1772 syslog(LOG_ERR, "Too many groups");
1773 }
1774
1775 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
1776 /*
1777 * Routines that maintain the remote mounttab
1778 */
1779 void
1780 get_mountlist()
1781 {
1782 struct mountlist *mlp, **mlpp;
1783 char *host, *dirp, *cp;
1784 char str[STRSIZ];
1785 FILE *mlfile;
1786
1787 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
1788 syslog(LOG_ERR, "Can't open %s", _PATH_RMOUNTLIST);
1789 return;
1790 }
1791 mlpp = &mlhead;
1792 while (fgets(str, STRSIZ, mlfile) != NULL) {
1793 cp = str;
1794 host = strsep(&cp, " \t\n");
1795 dirp = strsep(&cp, " \t\n");
1796 if (host == NULL || dirp == NULL)
1797 continue;
1798 mlp = (struct mountlist *)malloc(sizeof (*mlp));
1799 strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
1800 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
1801 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
1802 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
1803 mlp->ml_next = (struct mountlist *)NULL;
1804 *mlpp = mlp;
1805 mlpp = &mlp->ml_next;
1806 }
1807 fclose(mlfile);
1808 }
1809
1810 void
1811 del_mlist(hostp, dirp)
1812 char *hostp, *dirp;
1813 {
1814 struct mountlist *mlp, **mlpp;
1815 struct mountlist *mlp2;
1816 FILE *mlfile;
1817 int fnd = 0;
1818
1819 mlpp = &mlhead;
1820 mlp = mlhead;
1821 while (mlp) {
1822 if (!strcmp(mlp->ml_host, hostp) &&
1823 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
1824 fnd = 1;
1825 mlp2 = mlp;
1826 *mlpp = mlp = mlp->ml_next;
1827 free((caddr_t)mlp2);
1828 } else {
1829 mlpp = &mlp->ml_next;
1830 mlp = mlp->ml_next;
1831 }
1832 }
1833 if (fnd) {
1834 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
1835 syslog(LOG_ERR,"Can't update %s", _PATH_RMOUNTLIST);
1836 return;
1837 }
1838 mlp = mlhead;
1839 while (mlp) {
1840 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
1841 mlp = mlp->ml_next;
1842 }
1843 fclose(mlfile);
1844 }
1845 }
1846
1847 void
1848 add_mlist(hostp, dirp)
1849 char *hostp, *dirp;
1850 {
1851 struct mountlist *mlp, **mlpp;
1852 FILE *mlfile;
1853
1854 mlpp = &mlhead;
1855 mlp = mlhead;
1856 while (mlp) {
1857 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
1858 return;
1859 mlpp = &mlp->ml_next;
1860 mlp = mlp->ml_next;
1861 }
1862 mlp = (struct mountlist *)malloc(sizeof (*mlp));
1863 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
1864 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
1865 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
1866 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
1867 mlp->ml_next = (struct mountlist *)NULL;
1868 *mlpp = mlp;
1869 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
1870 syslog(LOG_ERR, "Can't update %s", _PATH_RMOUNTLIST);
1871 return;
1872 }
1873 fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
1874 fclose(mlfile);
1875 }
1876
1877 /*
1878 * This function is called via. SIGTERM when the system is going down.
1879 * It sends a broadcast RPCMNT_UMNTALL.
1880 */
1881 void
1882 send_umntall()
1883 {
1884 (void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
1885 xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
1886 exit(0);
1887 }
1888
1889 int
1890 umntall_each(resultsp, raddr)
1891 caddr_t resultsp;
1892 struct sockaddr_in *raddr;
1893 {
1894 return (1);
1895 }
1896
1897 /*
1898 * Free up a group list.
1899 */
1900 void
1901 free_grp(grp)
1902 struct grouplist *grp;
1903 {
1904 char **addrp;
1905
1906 if (grp->gr_type == GT_HOST) {
1907 if (grp->gr_ptr.gt_hostent->h_name) {
1908 addrp = grp->gr_ptr.gt_hostent->h_addr_list;
1909 while (addrp && *addrp)
1910 free(*addrp++);
1911 free((caddr_t)grp->gr_ptr.gt_hostent->h_addr_list);
1912 free(grp->gr_ptr.gt_hostent->h_name);
1913 }
1914 free((caddr_t)grp->gr_ptr.gt_hostent);
1915 } else if (grp->gr_type == GT_NET) {
1916 if (grp->gr_ptr.gt_net.nt_name)
1917 free(grp->gr_ptr.gt_net.nt_name);
1918 }
1919 #ifdef ISO
1920 else if (grp->gr_type == GT_ISO)
1921 free((caddr_t)grp->gr_ptr.gt_isoaddr);
1922 #endif
1923 free((caddr_t)grp);
1924 }
1925
1926 #ifdef DEBUG
1927 void
1928 SYSLOG(int pri, const char *fmt, ...)
1929 {
1930 va_list ap;
1931
1932 va_start(ap, fmt);
1933 vfprintf(stderr, fmt, ap);
1934 va_end(ap);
1935 }
1936 #endif /* DEBUG */
1937
1938 /*
1939 * Check options for consistency.
1940 */
1941 int
1942 check_options(dp)
1943 struct dirlist *dp;
1944 {
1945
1946 if (dp == (struct dirlist *)NULL)
1947 return (1);
1948 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
1949 (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
1950 (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
1951 syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
1952 return (1);
1953 }
1954 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
1955 syslog(LOG_ERR, "-mask requires -net");
1956 return (1);
1957 }
1958 if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
1959 syslog(LOG_ERR, "-net and -iso mutually exclusive");
1960 return (1);
1961 }
1962 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
1963 syslog(LOG_ERR, "-alldir has multiple directories");
1964 return (1);
1965 }
1966 return (0);
1967 }
1968
1969 /*
1970 * Check an absolute directory path for any symbolic links. Return true
1971 * if no symbolic links are found.
1972 */
1973 int
1974 check_dirpath(dirp)
1975 char *dirp;
1976 {
1977 char *cp;
1978 int ret = 1;
1979 struct stat sb;
1980
1981 cp = dirp + 1;
1982 while (*cp && ret) {
1983 if (*cp == '/') {
1984 *cp = '\0';
1985 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
1986 ret = 0;
1987 *cp = '/';
1988 }
1989 cp++;
1990 }
1991 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
1992 ret = 0;
1993 return (ret);
1994 }
1995