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