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