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