mountd.c revision 1.28 1 /* $NetBSD: mountd.c,v 1.28 1995/11/06 07:00:07 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Herb Hasler and Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. 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.28 1995/11/06 07:00:07 thorpej 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 #include <stdarg.h>
86
87 /*
88 * Structures for keeping the mount list and export list
89 */
90 struct mountlist {
91 struct mountlist *ml_next;
92 char ml_host[RPCMNT_NAMELEN+1];
93 char ml_dirp[RPCMNT_PATHLEN+1];
94 };
95
96 struct dirlist {
97 struct dirlist *dp_left;
98 struct dirlist *dp_right;
99 int dp_flag;
100 struct hostlist *dp_hosts; /* List of hosts this dir exported to */
101 char dp_dirp[1]; /* Actually malloc'd to size of dir */
102 };
103 /* dp_flag bits */
104 #define DP_DEFSET 0x1
105
106 struct exportlist {
107 struct exportlist *ex_next;
108 struct dirlist *ex_dirl;
109 struct dirlist *ex_defdir;
110 int ex_flag;
111 fsid_t ex_fs;
112 char *ex_fsdir;
113 };
114 /* ex_flag bits */
115 #define EX_LINKED 0x1
116
117 struct netmsk {
118 u_long nt_net;
119 u_long nt_mask;
120 char *nt_name;
121 };
122
123 union grouptypes {
124 struct hostent *gt_hostent;
125 struct netmsk gt_net;
126 #ifdef ISO
127 struct sockaddr_iso *gt_isoaddr;
128 #endif
129 };
130
131 struct grouplist {
132 int gr_type;
133 union grouptypes gr_ptr;
134 struct grouplist *gr_next;
135 };
136 /* Group types */
137 #define GT_NULL 0x0
138 #define GT_HOST 0x1
139 #define GT_NET 0x2
140 #define GT_ISO 0x4
141
142 struct hostlist {
143 struct grouplist *ht_grp;
144 struct hostlist *ht_next;
145 };
146
147 /* Global defs */
148 char *add_expdir __P((struct dirlist **, char *, int));
149 void add_dlist __P((struct dirlist **, struct dirlist *,
150 struct grouplist *));
151 void add_mlist __P((char *, char *));
152 int check_dirpath __P((char *));
153 int check_options __P((struct dirlist *));
154 int chk_host __P((struct dirlist *, u_long, int *));
155 void del_mlist __P((char *, char *));
156 struct dirlist *dirp_search __P((struct dirlist *, char *));
157 int do_mount __P((struct exportlist *, struct grouplist *, int,
158 struct ucred *, char *, int, struct statfs *));
159 int do_opt __P((char **, char **, struct exportlist *, struct grouplist *,
160 int *, int *, struct ucred *));
161 struct exportlist *ex_search __P((fsid_t *));
162 struct exportlist *get_exp __P((void));
163 void free_dir __P((struct dirlist *));
164 void free_exp __P((struct exportlist *));
165 void free_grp __P((struct grouplist *));
166 void free_host __P((struct hostlist *));
167 void get_exportlist __P((void));
168 int get_host __P((char *, struct grouplist *));
169 struct hostlist *get_ht __P((void));
170 int get_line __P((void));
171 void get_mountlist __P((void));
172 int get_net __P((char *, struct netmsk *, int));
173 void getexp_err __P((struct exportlist *, struct grouplist *));
174 struct grouplist *get_grp __P((void));
175 void hang_dirp __P((struct dirlist *, struct grouplist *,
176 struct exportlist *, int));
177 void mntsrv __P((struct svc_req *, SVCXPRT *));
178 void nextfield __P((char **, char **));
179 void out_of_mem __P((void));
180 void parsecred __P((char *, struct ucred *));
181 int put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
182 int scan_tree __P((struct dirlist *, u_long));
183 void send_umntall __P((void));
184 int umntall_each __P((caddr_t, struct sockaddr_in *));
185 int xdr_dir __P((XDR *, char *));
186 int xdr_explist __P((XDR *, caddr_t));
187 int xdr_fhs __P((XDR *, nfsv2fh_t *));
188 int xdr_mlist __P((XDR *, caddr_t));
189
190 /* C library */
191 int getnetgrent();
192 void endnetgrent();
193 void setnetgrent();
194
195 #ifdef ISO
196 struct iso_addr *iso_addr();
197 #endif
198
199 struct exportlist *exphead;
200 struct mountlist *mlhead;
201 struct grouplist *grphead;
202 char exname[MAXPATHLEN];
203 struct ucred def_anon = {
204 1,
205 (uid_t) -2,
206 (gid_t) -2,
207 0,
208 { }
209 };
210 int resvport_only = 1;
211 int opt_flags;
212 /* Bits for above */
213 #define OP_MAPROOT 0x01
214 #define OP_MAPALL 0x02
215 #define OP_KERB 0x04
216 #define OP_MASK 0x08
217 #define OP_NET 0x10
218 #define OP_ISO 0x20
219 #define OP_ALLDIRS 0x40
220
221 int debug = 0;
222 void SYSLOG __P((int, const char *, ...));
223
224 /*
225 * Mountd server for NFS mount protocol as described in:
226 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
227 * The optional arguments are the exports file name
228 * default: _PATH_EXPORTS
229 * "-d" to enable debugging
230 * and "-n" to allow nonroot mount.
231 */
232 int
233 main(argc, argv)
234 int argc;
235 char **argv;
236 {
237 SVCXPRT *udptransp, *tcptransp;
238 int c;
239
240 while ((c = getopt(argc, argv, "dn")) != EOF)
241 switch (c) {
242 case 'd':
243 debug = 1;
244 break;
245 case 'n':
246 resvport_only = 0;
247 break;
248 default:
249 fprintf(stderr, "Usage: mountd [-dn] [export_file]\n");
250 exit(1);
251 };
252 argc -= optind;
253 argv += optind;
254 grphead = (struct grouplist *)NULL;
255 exphead = (struct exportlist *)NULL;
256 mlhead = (struct mountlist *)NULL;
257 if (argc == 1) {
258 strncpy(exname, *argv, MAXPATHLEN-1);
259 exname[MAXPATHLEN-1] = '\0';
260 } else
261 strcpy(exname, _PATH_EXPORTS);
262 openlog("mountd", LOG_PID, LOG_DAEMON);
263 if (debug)
264 fprintf(stderr,"Getting export list.\n");
265 get_exportlist();
266 if (debug)
267 fprintf(stderr,"Getting mount list.\n");
268 get_mountlist();
269 if (debug)
270 fprintf(stderr,"Here we go.\n");
271 if (debug == 0) {
272 daemon(0, 0);
273 signal(SIGINT, SIG_IGN);
274 signal(SIGQUIT, SIG_IGN);
275 }
276 signal(SIGHUP, (void (*) __P((int))) get_exportlist);
277 signal(SIGTERM, (void (*) __P((int))) send_umntall);
278 { FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
279 if (pidfile != NULL) {
280 fprintf(pidfile, "%d\n", getpid());
281 fclose(pidfile);
282 }
283 }
284 if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
285 (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
286 syslog(LOG_ERR, "Can't create socket");
287 exit(1);
288 }
289 pmap_unset(RPCPROG_MNT, RPCMNT_VER1);
290 if (!svc_register(udptransp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
291 IPPROTO_UDP) ||
292 !svc_register(tcptransp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
293 IPPROTO_TCP)) {
294 syslog(LOG_ERR, "Can't register mount");
295 exit(1);
296 }
297 svc_run();
298 syslog(LOG_ERR, "Mountd died");
299 exit(1);
300 }
301
302 /*
303 * The mount rpc service
304 */
305 void
306 mntsrv(rqstp, transp)
307 struct svc_req *rqstp;
308 SVCXPRT *transp;
309 {
310 struct exportlist *ep;
311 struct dirlist *dp;
312 nfsv2fh_t nfh;
313 struct stat stb;
314 struct statfs fsb;
315 struct hostent *hp;
316 u_long saddr;
317 u_short sport;
318 char rpcpath[RPCMNT_PATHLEN+1], dirpath[MAXPATHLEN];
319 int bad = ENOENT, defset;
320 sigset_t sigset, osigset;
321
322 saddr = transp->xp_raddr.sin_addr.s_addr;
323 sport = ntohs(transp->xp_raddr.sin_port);
324 hp = (struct hostent *)NULL;
325 switch (rqstp->rq_proc) {
326 case NULLPROC:
327 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
328 syslog(LOG_ERR, "Can't send reply");
329 return;
330 case RPCMNT_MOUNT:
331 if (sport >= IPPORT_RESERVED && resvport_only) {
332 svcerr_weakauth(transp);
333 return;
334 }
335 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
336 svcerr_decode(transp);
337 return;
338 }
339
340 /*
341 * Get the real pathname and make sure it is a file or
342 * directory that exists.
343 */
344 if (realpath(rpcpath, dirpath) == 0 ||
345 stat(dirpath, &stb) < 0 ||
346 (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) ||
347 statfs(dirpath, &fsb) < 0) {
348 chdir("/"); /* Just in case realpath doesn't */
349 if (debug)
350 fprintf(stderr, "stat failed on %s\n", dirpath);
351 if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
352 syslog(LOG_ERR, "Can't send reply");
353 return;
354 }
355
356 /* Check in the exports list */
357 sigemptyset(&sigset);
358 sigaddset(&sigset, SIGHUP);
359 sigprocmask(SIG_BLOCK, &sigset, &osigset);
360 ep = ex_search(&fsb.f_fsid);
361 defset = 0;
362 if (ep && (chk_host(ep->ex_defdir, saddr, &defset) ||
363 ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
364 chk_host(dp, saddr, &defset)) ||
365 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
366 scan_tree(ep->ex_dirl, saddr) == 0))) {
367 /* Get the file handle */
368 memset(&nfh, 0, sizeof(nfh));
369 if (getfh(dirpath, (fhandle_t *)&nfh) < 0) {
370 bad = errno;
371 syslog(LOG_ERR, "Can't get fh for %s", dirpath);
372 if (!svc_sendreply(transp, xdr_long,
373 (caddr_t)&bad))
374 syslog(LOG_ERR, "Can't send reply");
375 sigprocmask(SIG_SETMASK, &osigset, NULL);
376 return;
377 }
378 if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&nfh))
379 syslog(LOG_ERR, "Can't send reply");
380 if (hp == NULL)
381 hp = gethostbyaddr((caddr_t)&saddr,
382 sizeof(saddr), AF_INET);
383 if (hp)
384 add_mlist(hp->h_name, dirpath);
385 else
386 add_mlist(inet_ntoa(transp->xp_raddr.sin_addr),
387 dirpath);
388 if (debug)
389 fprintf(stderr,"Mount successfull.\n");
390 } else {
391 bad = EACCES;
392 if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
393 syslog(LOG_ERR, "Can't send reply");
394 }
395 sigprocmask(SIG_SETMASK, &osigset, NULL);
396 return;
397 case RPCMNT_DUMP:
398 if (!svc_sendreply(transp, xdr_mlist, (caddr_t)NULL))
399 syslog(LOG_ERR, "Can't send reply");
400 return;
401 case RPCMNT_UMOUNT:
402 if (sport >= IPPORT_RESERVED && resvport_only) {
403 svcerr_weakauth(transp);
404 return;
405 }
406 if (!svc_getargs(transp, xdr_dir, dirpath)) {
407 svcerr_decode(transp);
408 return;
409 }
410 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
411 syslog(LOG_ERR, "Can't send reply");
412 hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
413 if (hp)
414 del_mlist(hp->h_name, dirpath);
415 del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), dirpath);
416 return;
417 case RPCMNT_UMNTALL:
418 if (sport >= IPPORT_RESERVED && resvport_only) {
419 svcerr_weakauth(transp);
420 return;
421 }
422 if (!svc_sendreply(transp, xdr_void, (caddr_t)NULL))
423 syslog(LOG_ERR, "Can't send reply");
424 hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
425 if (hp)
426 del_mlist(hp->h_name, (char *)NULL);
427 del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), (char *)NULL);
428 return;
429 case RPCMNT_EXPORT:
430 if (!svc_sendreply(transp, xdr_explist, (caddr_t)NULL))
431 syslog(LOG_ERR, "Can't send reply");
432 return;
433 default:
434 svcerr_noproc(transp);
435 return;
436 }
437 }
438
439 /*
440 * Xdr conversion for a dirpath string
441 */
442 int
443 xdr_dir(xdrsp, dirp)
444 XDR *xdrsp;
445 char *dirp;
446 {
447 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
448 }
449
450 /*
451 * Xdr routine to generate fhstatus
452 */
453 int
454 xdr_fhs(xdrsp, nfh)
455 XDR *xdrsp;
456 nfsv2fh_t *nfh;
457 {
458 long ok = 0;
459
460 if (!xdr_long(xdrsp, &ok))
461 return (0);
462 return (xdr_opaque(xdrsp, (caddr_t)nfh, NFSX_FH));
463 }
464
465 int
466 xdr_mlist(xdrsp, cp)
467 XDR *xdrsp;
468 caddr_t cp;
469 {
470 struct mountlist *mlp;
471 int true = 1;
472 int false = 0;
473 char *strp;
474
475 mlp = mlhead;
476 while (mlp) {
477 if (!xdr_bool(xdrsp, &true))
478 return (0);
479 strp = &mlp->ml_host[0];
480 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
481 return (0);
482 strp = &mlp->ml_dirp[0];
483 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
484 return (0);
485 mlp = mlp->ml_next;
486 }
487 if (!xdr_bool(xdrsp, &false))
488 return (0);
489 return (1);
490 }
491
492 /*
493 * Xdr conversion for export list
494 */
495 int
496 xdr_explist(xdrsp, cp)
497 XDR *xdrsp;
498 caddr_t cp;
499 {
500 struct exportlist *ep;
501 int false = 0;
502 int putdef;
503 sigset_t sigset, osigset;
504
505 sigemptyset(&sigset);
506 sigaddset(&sigset, SIGHUP);
507 sigprocmask(SIG_BLOCK, &sigset, &osigset);
508 ep = exphead;
509 while (ep) {
510 putdef = 0;
511 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
512 goto errout;
513 if (ep->ex_defdir && putdef == 0 &&
514 put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL,
515 &putdef))
516 goto errout;
517 ep = ep->ex_next;
518 }
519 sigprocmask(SIG_SETMASK, &osigset, NULL);
520 if (!xdr_bool(xdrsp, &false))
521 return (0);
522 return (1);
523 errout:
524 sigprocmask(SIG_SETMASK, &osigset, NULL);
525 return (0);
526 }
527
528 /*
529 * Called from xdr_explist() to traverse the tree and export the
530 * directory paths.
531 */
532 int
533 put_exlist(dp, xdrsp, adp, putdefp)
534 struct dirlist *dp;
535 XDR *xdrsp;
536 struct dirlist *adp;
537 int *putdefp;
538 {
539 struct grouplist *grp;
540 struct hostlist *hp;
541 int true = 1;
542 int false = 0;
543 int gotalldir = 0;
544 char *strp;
545
546 if (dp) {
547 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
548 return (1);
549 if (!xdr_bool(xdrsp, &true))
550 return (1);
551 strp = dp->dp_dirp;
552 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
553 return (1);
554 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
555 gotalldir = 1;
556 *putdefp = 1;
557 }
558 if ((dp->dp_flag & DP_DEFSET) == 0 &&
559 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
560 hp = dp->dp_hosts;
561 while (hp) {
562 grp = hp->ht_grp;
563 if (grp->gr_type == GT_HOST) {
564 if (!xdr_bool(xdrsp, &true))
565 return (1);
566 strp = grp->gr_ptr.gt_hostent->h_name;
567 if (!xdr_string(xdrsp, &strp,
568 RPCMNT_NAMELEN))
569 return (1);
570 } else if (grp->gr_type == GT_NET) {
571 if (!xdr_bool(xdrsp, &true))
572 return (1);
573 strp = grp->gr_ptr.gt_net.nt_name;
574 if (!xdr_string(xdrsp, &strp,
575 RPCMNT_NAMELEN))
576 return (1);
577 }
578 hp = hp->ht_next;
579 if (gotalldir && hp == (struct hostlist *)NULL) {
580 hp = adp->dp_hosts;
581 gotalldir = 0;
582 }
583 }
584 }
585 if (!xdr_bool(xdrsp, &false))
586 return (1);
587 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
588 return (1);
589 }
590 return (0);
591 }
592
593 #define LINESIZ 10240
594 char line[LINESIZ];
595 FILE *exp_file;
596
597 /*
598 * Get the export list
599 */
600 void
601 get_exportlist()
602 {
603 struct exportlist *ep, *ep2;
604 struct grouplist *grp, *tgrp;
605 struct exportlist **epp;
606 struct dirlist *dirhead;
607 struct statfs fsb, *fsp;
608 struct hostent *hpe;
609 struct ucred anon;
610 char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc;
611 int len, has_host, exflags, got_nondir, dirplen, num, i, netgrp;
612
613 /*
614 * First, get rid of the old list
615 */
616 ep = exphead;
617 while (ep) {
618 ep2 = ep;
619 ep = ep->ex_next;
620 free_exp(ep2);
621 }
622 exphead = (struct exportlist *)NULL;
623
624 grp = grphead;
625 while (grp) {
626 tgrp = grp;
627 grp = grp->gr_next;
628 free_grp(tgrp);
629 }
630 grphead = (struct grouplist *)NULL;
631
632 /*
633 * And delete exports that are in the kernel for all local
634 * file systems.
635 * XXX: Should know how to handle all local exportable file systems
636 * instead of just MOUNT_UFS.
637 */
638 num = getmntinfo(&fsp, MNT_NOWAIT);
639 for (i = 0; i < num; i++) {
640 union {
641 struct ufs_args ua;
642 struct iso_args ia;
643 struct mfs_args ma;
644 struct msdosfs_args da;
645 struct adosfs_args aa;
646 } targs;
647
648 if (!strncmp(fsp->f_fstypename, MOUNT_MFS, MFSNAMELEN) ||
649 !strncmp(fsp->f_fstypename, MOUNT_UFS, MFSNAMELEN) ||
650 !strncmp(fsp->f_fstypename, MOUNT_MSDOS, MFSNAMELEN) ||
651 !strncmp(fsp->f_fstypename, MOUNT_ADOSFS, MFSNAMELEN) ||
652 !strncmp(fsp->f_fstypename, MOUNT_CD9660, MFSNAMELEN)) {
653 targs.ua.fspec = NULL;
654 targs.ua.export.ex_flags = MNT_DELEXPORT;
655 if (mount(fsp->f_fstypename, fsp->f_mntonname,
656 fsp->f_flags | MNT_UPDATE,
657 (caddr_t)&targs) < 0)
658 syslog(LOG_ERR, "Can't delete exports for %s",
659 fsp->f_mntonname);
660 }
661 fsp++;
662 }
663
664 /*
665 * Read in the exports file and build the list, calling
666 * mount() as we go along to push the export rules into the kernel.
667 */
668 if ((exp_file = fopen(exname, "r")) == NULL) {
669 syslog(LOG_ERR, "Can't open %s", exname);
670 exit(2);
671 }
672 dirhead = (struct dirlist *)NULL;
673 while (get_line()) {
674 if (debug)
675 fprintf(stderr,"Got line %s\n",line);
676 cp = line;
677 nextfield(&cp, &endcp);
678 if (*cp == '#')
679 goto nextline;
680
681 /*
682 * Set defaults.
683 */
684 has_host = FALSE;
685 anon = def_anon;
686 exflags = MNT_EXPORTED;
687 got_nondir = 0;
688 opt_flags = 0;
689 ep = (struct exportlist *)NULL;
690
691 /*
692 * Create new exports list entry
693 */
694 len = endcp-cp;
695 tgrp = grp = get_grp();
696 while (len > 0) {
697 if (len > RPCMNT_NAMELEN) {
698 getexp_err(ep, tgrp);
699 goto nextline;
700 }
701 if (*cp == '-') {
702 if (ep == (struct exportlist *)NULL) {
703 getexp_err(ep, tgrp);
704 goto nextline;
705 }
706 if (debug)
707 fprintf(stderr, "doing opt %s\n", cp);
708 got_nondir = 1;
709 if (do_opt(&cp, &endcp, ep, grp, &has_host,
710 &exflags, &anon)) {
711 getexp_err(ep, tgrp);
712 goto nextline;
713 }
714 } else if (*cp == '/') {
715 savedc = *endcp;
716 *endcp = '\0';
717 if (check_dirpath(cp) &&
718 statfs(cp, &fsb) >= 0) {
719 if (got_nondir) {
720 syslog(LOG_ERR, "Dirs must be first");
721 getexp_err(ep, tgrp);
722 goto nextline;
723 }
724 if (ep) {
725 if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] ||
726 ep->ex_fs.val[1] != fsb.f_fsid.val[1]) {
727 getexp_err(ep, tgrp);
728 goto nextline;
729 }
730 } else {
731 /*
732 * See if this directory is already
733 * in the list.
734 */
735 ep = ex_search(&fsb.f_fsid);
736 if (ep == (struct exportlist *)NULL) {
737 ep = get_exp();
738 ep->ex_fs = fsb.f_fsid;
739 ep->ex_fsdir = (char *)
740 malloc(strlen(fsb.f_mntonname) + 1);
741 if (ep->ex_fsdir)
742 strcpy(ep->ex_fsdir,
743 fsb.f_mntonname);
744 else
745 out_of_mem();
746 if (debug)
747 fprintf(stderr,
748 "Making new ep fs=0x%x,0x%x\n",
749 fsb.f_fsid.val[0],
750 fsb.f_fsid.val[1]);
751 } else if (debug)
752 fprintf(stderr,
753 "Found ep fs=0x%x,0x%x\n",
754 fsb.f_fsid.val[0],
755 fsb.f_fsid.val[1]);
756 }
757
758 /*
759 * Add dirpath to export mount point.
760 */
761 dirp = add_expdir(&dirhead, cp, len);
762 dirplen = len;
763 } else {
764 getexp_err(ep, tgrp);
765 goto nextline;
766 }
767 *endcp = savedc;
768 } else {
769 savedc = *endcp;
770 *endcp = '\0';
771 got_nondir = 1;
772 if (ep == (struct exportlist *)NULL) {
773 getexp_err(ep, tgrp);
774 goto nextline;
775 }
776
777 /*
778 * Get the host or netgroup.
779 */
780 setnetgrent(cp);
781 netgrp = getnetgrent(&hst, &usr, &dom);
782 do {
783 if (has_host) {
784 grp->gr_next = get_grp();
785 grp = grp->gr_next;
786 }
787 if (netgrp) {
788 if (get_host(hst, grp)) {
789 syslog(LOG_ERR, "Bad netgroup %s", cp);
790 getexp_err(ep, tgrp);
791 goto nextline;
792 }
793 } else if (get_host(cp, grp)) {
794 getexp_err(ep, tgrp);
795 goto nextline;
796 }
797 has_host = TRUE;
798 } while (netgrp && getnetgrent(&hst, &usr, &dom));
799 endnetgrent();
800 *endcp = savedc;
801 }
802 cp = endcp;
803 nextfield(&cp, &endcp);
804 len = endcp - cp;
805 }
806 if (check_options(dirhead)) {
807 getexp_err(ep, tgrp);
808 goto nextline;
809 }
810 if (!has_host) {
811 grp->gr_type = GT_HOST;
812 if (debug)
813 fprintf(stderr,"Adding a default entry\n");
814 /* add a default group and make the grp list NULL */
815 hpe = (struct hostent *)malloc(sizeof(struct hostent));
816 if (hpe == (struct hostent *)NULL)
817 out_of_mem();
818 hpe->h_name = "Default";
819 hpe->h_addrtype = AF_INET;
820 hpe->h_length = sizeof (u_long);
821 hpe->h_addr_list = (char **)NULL;
822 grp->gr_ptr.gt_hostent = hpe;
823
824 /*
825 * Don't allow a network export coincide with a list of
826 * host(s) on the same line.
827 */
828 } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
829 getexp_err(ep, tgrp);
830 goto nextline;
831 }
832
833 /*
834 * Loop through hosts, pushing the exports into the kernel.
835 * After loop, tgrp points to the start of the list and
836 * grp points to the last entry in the list.
837 */
838 grp = tgrp;
839 do {
840 if (do_mount(ep, grp, exflags, &anon, dirp,
841 dirplen, &fsb)) {
842 getexp_err(ep, tgrp);
843 goto nextline;
844 }
845 } while (grp->gr_next && (grp = grp->gr_next));
846
847 /*
848 * Success. Update the data structures.
849 */
850 if (has_host) {
851 hang_dirp(dirhead, tgrp, ep, (opt_flags & OP_ALLDIRS));
852 grp->gr_next = grphead;
853 grphead = tgrp;
854 } else {
855 hang_dirp(dirhead, (struct grouplist *)NULL, ep,
856 (opt_flags & OP_ALLDIRS));
857 free_grp(grp);
858 }
859 dirhead = (struct dirlist *)NULL;
860 if ((ep->ex_flag & EX_LINKED) == 0) {
861 ep2 = exphead;
862 epp = &exphead;
863
864 /*
865 * Insert in the list in alphabetical order.
866 */
867 while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
868 epp = &ep2->ex_next;
869 ep2 = ep2->ex_next;
870 }
871 if (ep2)
872 ep->ex_next = ep2;
873 *epp = ep;
874 ep->ex_flag |= EX_LINKED;
875 }
876 nextline:
877 if (dirhead) {
878 free_dir(dirhead);
879 dirhead = (struct dirlist *)NULL;
880 }
881 }
882 fclose(exp_file);
883 }
884
885 /*
886 * Allocate an export list element
887 */
888 struct exportlist *
889 get_exp()
890 {
891 struct exportlist *ep;
892
893 ep = (struct exportlist *)malloc(sizeof (struct exportlist));
894 if (ep == (struct exportlist *)NULL)
895 out_of_mem();
896 memset(ep, 0, sizeof(struct exportlist));
897 return (ep);
898 }
899
900 /*
901 * Allocate a group list element
902 */
903 struct grouplist *
904 get_grp()
905 {
906 struct grouplist *gp;
907
908 gp = (struct grouplist *)malloc(sizeof (struct grouplist));
909 if (gp == (struct grouplist *)NULL)
910 out_of_mem();
911 memset(gp, 0, sizeof(struct grouplist));
912 return (gp);
913 }
914
915 /*
916 * Clean up upon an error in get_exportlist().
917 */
918 void
919 getexp_err(ep, grp)
920 struct exportlist *ep;
921 struct grouplist *grp;
922 {
923 struct grouplist *tgrp;
924
925 syslog(LOG_ERR, "Bad exports list line %s", line);
926 if (ep && (ep->ex_flag & EX_LINKED) == 0)
927 free_exp(ep);
928 while (grp) {
929 tgrp = grp;
930 grp = grp->gr_next;
931 free_grp(tgrp);
932 }
933 }
934
935 /*
936 * Search the export list for a matching fs.
937 */
938 struct exportlist *
939 ex_search(fsid)
940 fsid_t *fsid;
941 {
942 struct exportlist *ep;
943
944 ep = exphead;
945 while (ep) {
946 if (ep->ex_fs.val[0] == fsid->val[0] &&
947 ep->ex_fs.val[1] == fsid->val[1])
948 return (ep);
949 ep = ep->ex_next;
950 }
951 return (ep);
952 }
953
954 /*
955 * Add a directory path to the list.
956 */
957 char *
958 add_expdir(dpp, cp, len)
959 struct dirlist **dpp;
960 char *cp;
961 int len;
962 {
963 struct dirlist *dp;
964
965 dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len);
966 dp->dp_left = *dpp;
967 dp->dp_right = (struct dirlist *)NULL;
968 dp->dp_flag = 0;
969 dp->dp_hosts = (struct hostlist *)NULL;
970 strcpy(dp->dp_dirp, cp);
971 *dpp = dp;
972 return (dp->dp_dirp);
973 }
974
975 /*
976 * Hang the dir list element off the dirpath binary tree as required
977 * and update the entry for host.
978 */
979 void
980 hang_dirp(dp, grp, ep, alldirs)
981 struct dirlist *dp;
982 struct grouplist *grp;
983 struct exportlist *ep;
984 int alldirs;
985 {
986 struct hostlist *hp;
987 struct dirlist *dp2;
988
989 if (alldirs) {
990 if (ep->ex_defdir)
991 free((caddr_t)dp);
992 else
993 ep->ex_defdir = dp;
994 if (grp == (struct grouplist *)NULL)
995 ep->ex_defdir->dp_flag |= DP_DEFSET;
996 else while (grp) {
997 hp = get_ht();
998 hp->ht_grp = grp;
999 hp->ht_next = ep->ex_defdir->dp_hosts;
1000 ep->ex_defdir->dp_hosts = hp;
1001 grp = grp->gr_next;
1002 }
1003 } else {
1004
1005 /*
1006 * Loop throught the directories adding them to the tree.
1007 */
1008 while (dp) {
1009 dp2 = dp->dp_left;
1010 add_dlist(&ep->ex_dirl, dp, grp);
1011 dp = dp2;
1012 }
1013 }
1014 }
1015
1016 /*
1017 * Traverse the binary tree either updating a node that is already there
1018 * for the new directory or adding the new node.
1019 */
1020 void
1021 add_dlist(dpp, newdp, grp)
1022 struct dirlist **dpp;
1023 struct dirlist *newdp;
1024 struct grouplist *grp;
1025 {
1026 struct dirlist *dp;
1027 struct hostlist *hp;
1028 int cmp;
1029
1030 dp = *dpp;
1031 if (dp) {
1032 cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1033 if (cmp > 0) {
1034 add_dlist(&dp->dp_left, newdp, grp);
1035 return;
1036 } else if (cmp < 0) {
1037 add_dlist(&dp->dp_right, newdp, grp);
1038 return;
1039 } else
1040 free((caddr_t)newdp);
1041 } else {
1042 dp = newdp;
1043 dp->dp_left = (struct dirlist *)NULL;
1044 *dpp = dp;
1045 }
1046 if (grp) {
1047
1048 /*
1049 * Hang all of the host(s) off of the directory point.
1050 */
1051 do {
1052 hp = get_ht();
1053 hp->ht_grp = grp;
1054 hp->ht_next = dp->dp_hosts;
1055 dp->dp_hosts = hp;
1056 grp = grp->gr_next;
1057 } while (grp);
1058 } else
1059 dp->dp_flag |= DP_DEFSET;
1060 }
1061
1062 /*
1063 * Search for a dirpath on the export point.
1064 */
1065 struct dirlist *
1066 dirp_search(dp, dirpath)
1067 struct dirlist *dp;
1068 char *dirpath;
1069 {
1070 int cmp;
1071
1072 if (dp) {
1073 cmp = strcmp(dp->dp_dirp, dirpath);
1074 if (cmp > 0)
1075 return (dirp_search(dp->dp_left, dirpath));
1076 else if (cmp < 0)
1077 return (dirp_search(dp->dp_right, dirpath));
1078 else
1079 return (dp);
1080 }
1081 return (dp);
1082 }
1083
1084 /*
1085 * Scan for a host match in a directory tree.
1086 */
1087 int
1088 chk_host(dp, saddr, defsetp)
1089 struct dirlist *dp;
1090 u_long saddr;
1091 int *defsetp;
1092 {
1093 struct hostlist *hp;
1094 struct grouplist *grp;
1095 u_long **addrp;
1096
1097 if (dp) {
1098 if (dp->dp_flag & DP_DEFSET)
1099 *defsetp = 1;
1100 hp = dp->dp_hosts;
1101 while (hp) {
1102 grp = hp->ht_grp;
1103 switch (grp->gr_type) {
1104 case GT_HOST:
1105 addrp = (u_long **)
1106 grp->gr_ptr.gt_hostent->h_addr_list;
1107 while (*addrp) {
1108 if (**addrp == saddr)
1109 return (1);
1110 addrp++;
1111 }
1112 break;
1113 case GT_NET:
1114 if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
1115 grp->gr_ptr.gt_net.nt_net)
1116 return (1);
1117 break;
1118 };
1119 hp = hp->ht_next;
1120 }
1121 }
1122 return (0);
1123 }
1124
1125 /*
1126 * Scan tree for a host that matches the address.
1127 */
1128 int
1129 scan_tree(dp, saddr)
1130 struct dirlist *dp;
1131 u_long saddr;
1132 {
1133 int defset;
1134
1135 if (dp) {
1136 if (scan_tree(dp->dp_left, saddr))
1137 return (1);
1138 if (chk_host(dp, saddr, &defset))
1139 return (1);
1140 if (scan_tree(dp->dp_right, saddr))
1141 return (1);
1142 }
1143 return (0);
1144 }
1145
1146 /*
1147 * Traverse the dirlist tree and free it up.
1148 */
1149 void
1150 free_dir(dp)
1151 struct dirlist *dp;
1152 {
1153
1154 if (dp) {
1155 free_dir(dp->dp_left);
1156 free_dir(dp->dp_right);
1157 free_host(dp->dp_hosts);
1158 free((caddr_t)dp);
1159 }
1160 }
1161
1162 /*
1163 * Parse the option string and update fields.
1164 * Option arguments may either be -<option>=<value> or
1165 * -<option> <value>
1166 */
1167 int
1168 do_opt(cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
1169 char **cpp, **endcpp;
1170 struct exportlist *ep;
1171 struct grouplist *grp;
1172 int *has_hostp;
1173 int *exflagsp;
1174 struct ucred *cr;
1175 {
1176 char *cpoptarg, *cpoptend;
1177 char *cp, *endcp, *cpopt, savedc, savedc2;
1178 int allflag, usedarg;
1179
1180 cpopt = *cpp;
1181 cpopt++;
1182 cp = *endcpp;
1183 savedc = *cp;
1184 *cp = '\0';
1185 while (cpopt && *cpopt) {
1186 allflag = 1;
1187 usedarg = -2;
1188 if (cpoptend = strchr(cpopt, ',')) {
1189 *cpoptend++ = '\0';
1190 if (cpoptarg = strchr(cpopt, '='))
1191 *cpoptarg++ = '\0';
1192 } else {
1193 if (cpoptarg = strchr(cpopt, '='))
1194 *cpoptarg++ = '\0';
1195 else {
1196 *cp = savedc;
1197 nextfield(&cp, &endcp);
1198 **endcpp = '\0';
1199 if (endcp > cp && *cp != '-') {
1200 cpoptarg = cp;
1201 savedc2 = *endcp;
1202 *endcp = '\0';
1203 usedarg = 0;
1204 }
1205 }
1206 }
1207 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1208 *exflagsp |= MNT_EXRDONLY;
1209 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1210 !(allflag = strcmp(cpopt, "mapall")) ||
1211 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1212 usedarg++;
1213 parsecred(cpoptarg, cr);
1214 if (allflag == 0) {
1215 *exflagsp |= MNT_EXPORTANON;
1216 opt_flags |= OP_MAPALL;
1217 } else
1218 opt_flags |= OP_MAPROOT;
1219 } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
1220 *exflagsp |= MNT_EXKERB;
1221 opt_flags |= OP_KERB;
1222 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1223 !strcmp(cpopt, "m"))) {
1224 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1225 syslog(LOG_ERR, "Bad mask: %s", cpoptarg);
1226 return (1);
1227 }
1228 usedarg++;
1229 opt_flags |= OP_MASK;
1230 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1231 !strcmp(cpopt, "n"))) {
1232 if (grp->gr_type != GT_NULL) {
1233 syslog(LOG_ERR, "Network/host conflict");
1234 return (1);
1235 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1236 syslog(LOG_ERR, "Bad net: %s", cpoptarg);
1237 return (1);
1238 }
1239 grp->gr_type = GT_NET;
1240 *has_hostp = 1;
1241 usedarg++;
1242 opt_flags |= OP_NET;
1243 } else if (!strcmp(cpopt, "alldirs")) {
1244 opt_flags |= OP_ALLDIRS;
1245 #ifdef ISO
1246 } else if (cpoptarg && !strcmp(cpopt, "iso")) {
1247 if (get_isoaddr(cpoptarg, grp)) {
1248 syslog(LOG_ERR, "Bad iso addr: %s", cpoptarg);
1249 return (1);
1250 }
1251 *has_hostp = 1;
1252 usedarg++;
1253 opt_flags |= OP_ISO;
1254 #endif /* ISO */
1255 } else {
1256 syslog(LOG_ERR, "Bad opt %s", cpopt);
1257 return (1);
1258 }
1259 if (usedarg >= 0) {
1260 *endcp = savedc2;
1261 **endcpp = savedc;
1262 if (usedarg > 0) {
1263 *cpp = cp;
1264 *endcpp = endcp;
1265 }
1266 return (0);
1267 }
1268 cpopt = cpoptend;
1269 }
1270 **endcpp = savedc;
1271 return (0);
1272 }
1273
1274 /*
1275 * Translate a character string to the corresponding list of network
1276 * addresses for a hostname.
1277 */
1278 int
1279 get_host(cp, grp)
1280 char *cp;
1281 struct grouplist *grp;
1282 {
1283 struct hostent *hp, *nhp;
1284 char **addrp, **naddrp;
1285 struct hostent t_host;
1286 int i;
1287 u_long saddr;
1288 char *aptr[2];
1289
1290 if (grp->gr_type != GT_NULL)
1291 return (1);
1292 if ((hp = gethostbyname(cp)) == NULL) {
1293 if (isdigit(*cp)) {
1294 saddr = inet_addr(cp);
1295 if (saddr == -1) {
1296 syslog(LOG_ERR, "Inet_addr failed");
1297 return (1);
1298 }
1299 if ((hp = gethostbyaddr((caddr_t)&saddr, sizeof (saddr),
1300 AF_INET)) == NULL) {
1301 hp = &t_host;
1302 hp->h_name = cp;
1303 hp->h_addrtype = AF_INET;
1304 hp->h_length = sizeof (u_long);
1305 hp->h_addr_list = aptr;
1306 aptr[0] = (char *)&saddr;
1307 aptr[1] = (char *)NULL;
1308 }
1309 } else {
1310 syslog(LOG_ERR, "Gethostbyname failed");
1311 return (1);
1312 }
1313 }
1314 grp->gr_type = GT_HOST;
1315 nhp = grp->gr_ptr.gt_hostent = (struct hostent *)
1316 malloc(sizeof(struct hostent));
1317 if (nhp == (struct hostent *)NULL)
1318 out_of_mem();
1319 memcpy(nhp, hp, sizeof(struct hostent));
1320 i = strlen(hp->h_name)+1;
1321 nhp->h_name = (char *)malloc(i);
1322 if (nhp->h_name == (char *)NULL)
1323 out_of_mem();
1324 memcpy(nhp->h_name, hp->h_name, i);
1325 addrp = hp->h_addr_list;
1326 i = 1;
1327 while (*addrp++)
1328 i++;
1329 naddrp = nhp->h_addr_list = (char **)
1330 malloc(i*sizeof(char *));
1331 if (naddrp == (char **)NULL)
1332 out_of_mem();
1333 addrp = hp->h_addr_list;
1334 while (*addrp) {
1335 *naddrp = (char *)
1336 malloc(hp->h_length);
1337 if (*naddrp == (char *)NULL)
1338 out_of_mem();
1339 memcpy(*naddrp, *addrp, hp->h_length);
1340 addrp++;
1341 naddrp++;
1342 }
1343 *naddrp = (char *)NULL;
1344 if (debug)
1345 fprintf(stderr, "got host %s\n", hp->h_name);
1346 return (0);
1347 }
1348
1349 /*
1350 * Free up an exports list component
1351 */
1352 void
1353 free_exp(ep)
1354 struct exportlist *ep;
1355 {
1356
1357 if (ep->ex_defdir) {
1358 free_host(ep->ex_defdir->dp_hosts);
1359 free((caddr_t)ep->ex_defdir);
1360 }
1361 if (ep->ex_fsdir)
1362 free(ep->ex_fsdir);
1363 free_dir(ep->ex_dirl);
1364 free((caddr_t)ep);
1365 }
1366
1367 /*
1368 * Free hosts.
1369 */
1370 void
1371 free_host(hp)
1372 struct hostlist *hp;
1373 {
1374 struct hostlist *hp2;
1375
1376 while (hp) {
1377 hp2 = hp;
1378 hp = hp->ht_next;
1379 free((caddr_t)hp2);
1380 }
1381 }
1382
1383 struct hostlist *
1384 get_ht()
1385 {
1386 struct hostlist *hp;
1387
1388 hp = (struct hostlist *)malloc(sizeof (struct hostlist));
1389 if (hp == (struct hostlist *)NULL)
1390 out_of_mem();
1391 hp->ht_next = (struct hostlist *)NULL;
1392 return (hp);
1393 }
1394
1395 #ifdef ISO
1396 /*
1397 * Translate an iso address.
1398 */
1399 get_isoaddr(cp, grp)
1400 char *cp;
1401 struct grouplist *grp;
1402 {
1403 struct iso_addr *isop;
1404 struct sockaddr_iso *isoaddr;
1405
1406 if (grp->gr_type != GT_NULL)
1407 return (1);
1408 if ((isop = iso_addr(cp)) == NULL) {
1409 syslog(LOG_ERR,
1410 "iso_addr failed, ignored");
1411 return (1);
1412 }
1413 isoaddr = (struct sockaddr_iso *)
1414 malloc(sizeof (struct sockaddr_iso));
1415 if (isoaddr == (struct sockaddr_iso *)NULL)
1416 out_of_mem();
1417 memset(isoaddr, 0, sizeof(struct sockaddr_iso));
1418 memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
1419 isoaddr->siso_len = sizeof(struct sockaddr_iso);
1420 isoaddr->siso_family = AF_ISO;
1421 grp->gr_type = GT_ISO;
1422 grp->gr_ptr.gt_isoaddr = isoaddr;
1423 return (0);
1424 }
1425 #endif /* ISO */
1426
1427 /*
1428 * Out of memory, fatal
1429 */
1430 void
1431 out_of_mem()
1432 {
1433
1434 syslog(LOG_ERR, "Out of memory");
1435 exit(2);
1436 }
1437
1438 /*
1439 * Do the mount syscall with the update flag to push the export info into
1440 * the kernel.
1441 */
1442 int
1443 do_mount(ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
1444 struct exportlist *ep;
1445 struct grouplist *grp;
1446 int exflags;
1447 struct ucred *anoncrp;
1448 char *dirp;
1449 int dirplen;
1450 struct statfs *fsb;
1451 {
1452 char *cp = (char *)NULL;
1453 u_long **addrp;
1454 int done;
1455 char savedc = '\0';
1456 struct sockaddr_in sin, imask;
1457 union {
1458 struct ufs_args ua;
1459 struct iso_args ia;
1460 struct mfs_args ma;
1461 struct msdosfs_args da;
1462 struct adosfs_args aa;
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 void
1928 SYSLOG(int pri, const char *fmt, ...)
1929 {
1930 va_list ap;
1931
1932 va_start(ap, fmt);
1933
1934 if (debug)
1935 vfprintf(stderr, fmt, ap);
1936 else
1937 vsyslog(pri, fmt, ap);
1938
1939 va_end(ap);
1940 }
1941
1942 /*
1943 * Check options for consistency.
1944 */
1945 int
1946 check_options(dp)
1947 struct dirlist *dp;
1948 {
1949
1950 if (dp == (struct dirlist *)NULL)
1951 return (1);
1952 if ((opt_flags & (OP_MAPROOT | OP_MAPALL)) == (OP_MAPROOT | OP_MAPALL) ||
1953 (opt_flags & (OP_MAPROOT | OP_KERB)) == (OP_MAPROOT | OP_KERB) ||
1954 (opt_flags & (OP_MAPALL | OP_KERB)) == (OP_MAPALL | OP_KERB)) {
1955 syslog(LOG_ERR, "-mapall, -maproot and -kerb mutually exclusive");
1956 return (1);
1957 }
1958 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
1959 syslog(LOG_ERR, "-mask requires -net");
1960 return (1);
1961 }
1962 if ((opt_flags & (OP_NET | OP_ISO)) == (OP_NET | OP_ISO)) {
1963 syslog(LOG_ERR, "-net and -iso mutually exclusive");
1964 return (1);
1965 }
1966 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
1967 syslog(LOG_ERR, "-alldir has multiple directories");
1968 return (1);
1969 }
1970 return (0);
1971 }
1972
1973 /*
1974 * Check an absolute directory path for any symbolic links. Return true
1975 * if no symbolic links are found.
1976 */
1977 int
1978 check_dirpath(dirp)
1979 char *dirp;
1980 {
1981 char *cp;
1982 int ret = 1;
1983 struct stat sb;
1984
1985 cp = dirp + 1;
1986 while (*cp && ret) {
1987 if (*cp == '/') {
1988 *cp = '\0';
1989 if (lstat(dirp, &sb) < 0 || !S_ISDIR(sb.st_mode))
1990 ret = 0;
1991 *cp = '/';
1992 }
1993 cp++;
1994 }
1995 if (lstat(dirp, &sb) < 0 ||
1996 (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)))
1997 ret = 0;
1998 return (ret);
1999 }
2000