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