2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Herb Hasler and Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)mountd.c 8.15 (Berkeley) 5/1/95
38 * $FreeBSD: src/sbin/mountd/mountd.c,v 1.39.2.5 2002/09/13 15:57:43 joerg Exp $
39 * $DragonFly: src/sbin/mountd/mountd.c,v 1.8 2005/11/06 13:43:47 swildner Exp $
42 #include <sys/param.h>
43 #include <sys/mount.h>
45 #include <sys/syslog.h>
46 #include <sys/sysctl.h>
49 #include <rpc/pmap_clnt.h>
50 #include <vfs/nfs/rpcv2.h>
51 #include <vfs/nfs/nfsproto.h>
52 #include <vfs/nfs/nfs.h>
53 #include <vfs/ufs/ufsmount.h>
54 #include <vfs/msdosfs/msdosfsmount.h>
55 #include <vfs/ntfs/ntfsmount.h>
56 #include <vfs/isofs/cd9660/cd9660_mount.h> /* XXX need isofs in include */
58 #include <arpa/inet.h>
71 #include "pathnames.h"
78 * Structures for keeping the mount list and export list
81 struct mountlist
*ml_next
;
82 char ml_host
[RPCMNT_NAMELEN
+1];
83 char ml_dirp
[RPCMNT_PATHLEN
+1];
87 struct dirlist
*dp_left
;
88 struct dirlist
*dp_right
;
90 struct hostlist
*dp_hosts
; /* List of hosts this dir exported to */
91 char dp_dirp
[1]; /* Actually malloc'd to size of dir */
95 #define DP_HOSTSET 0x2
99 struct exportlist
*ex_next
;
100 struct dirlist
*ex_dirl
;
101 struct dirlist
*ex_defdir
;
108 #define EX_LINKED 0x1
117 struct hostent
*gt_hostent
;
118 struct netmsk gt_net
;
123 union grouptypes gr_ptr
;
124 struct grouplist
*gr_next
;
130 #define GT_IGNORE 0x5
133 int ht_flag
; /* Uses DP_xx bits */
134 struct grouplist
*ht_grp
;
135 struct hostlist
*ht_next
;
145 char *add_expdir(struct dirlist
**, char *, int);
146 void add_dlist(struct dirlist
**, struct dirlist
*,
147 struct grouplist
*, int);
148 void add_mlist(char *, char *);
149 int check_dirpath(char *);
150 int check_options(struct dirlist
*);
151 int chk_host(struct dirlist
*, u_int32_t
, int *, int *);
152 void del_mlist(char *, char *);
153 struct dirlist
*dirp_search(struct dirlist
*, char *);
154 int do_mount(struct exportlist
*, struct grouplist
*, int,
155 struct ucred
*, char *, int, struct statfs
*);
156 int do_opt(char **, char **, struct exportlist
*, struct grouplist
*,
157 int *, int *, struct ucred
*);
158 struct exportlist
*ex_search(fsid_t
*);
159 struct exportlist
*get_exp(void);
160 void free_dir(struct dirlist
*);
161 void free_exp(struct exportlist
*);
162 void free_grp(struct grouplist
*);
163 void free_host(struct hostlist
*);
164 void get_exportlist(void);
165 int get_host(char *, struct grouplist
*, struct grouplist
*);
167 struct hostlist
*get_ht(void);
169 void get_mountlist(void);
170 int get_net(char *, struct netmsk
*, int);
171 void getexp_err(struct exportlist
*, struct grouplist
*);
172 struct grouplist
*get_grp(void);
173 void hang_dirp(struct dirlist
*, struct grouplist
*,
174 struct exportlist
*, int);
175 void mntsrv(struct svc_req
*, SVCXPRT
*);
176 void nextfield(char **, char **);
177 void out_of_mem(void);
178 void parsecred(char *, struct ucred
*);
179 int put_exlist(struct dirlist
*, XDR
*, struct dirlist
*, int *);
180 int scan_tree(struct dirlist
*, u_int32_t
);
181 static void usage(void);
182 int xdr_dir(XDR
*, char *);
183 int xdr_explist(XDR
*, caddr_t
);
184 int xdr_fhs(XDR
*, caddr_t
);
185 int xdr_mlist(XDR
*, caddr_t
);
187 struct exportlist
*exphead
;
188 struct mountlist
*mlhead
;
189 struct grouplist
*grphead
;
190 char exname
[MAXPATHLEN
];
191 struct ucred def_anon
= {
198 int resvport_only
= 1;
203 #define OP_MAPROOT 0x01
204 #define OP_MAPALL 0x02
208 #define OP_ALLDIRS 0x40
209 /* 0x80 is OP_HAVEMASK in FreeBSD 5+ */
210 #define OP_QUIET 0x100
214 void SYSLOG(int, const char *, ...);
215 #define syslog SYSLOG
221 * Mountd server for NFS mount protocol as described in:
222 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
223 * The optional arguments are the exports file name
224 * default: _PATH_EXPORTS
225 * and "-n" to allow nonroot mount.
228 main(int argc
, char **argv
)
230 SVCXPRT
*udptransp
, *tcptransp
;
231 int c
, error
, mib
[3];
234 error
= getvfsbyname("nfs", &vfc
);
235 if (error
&& vfsisloadable("nfs")) {
237 err(1, "vfsload(nfs)");
238 endvfsent(); /* flush cache */
239 error
= getvfsbyname("nfs", &vfc
);
242 errx(1, "NFS support is not available in the running kernel");
244 while ((c
= getopt(argc
, argv
, "2dlnr")) != -1)
256 debug
= debug
? 0 : 1;
266 grphead
= (struct grouplist
*)NULL
;
267 exphead
= (struct exportlist
*)NULL
;
268 mlhead
= (struct mountlist
*)NULL
;
270 strncpy(exname
, *argv
, MAXPATHLEN
-1);
271 exname
[MAXPATHLEN
-1] = '\0';
273 strcpy(exname
, _PATH_EXPORTS
);
274 openlog("mountd", LOG_PID
, LOG_DAEMON
);
276 warnx("getting export list");
279 warnx("getting mount list");
285 signal(SIGINT
, SIG_IGN
);
286 signal(SIGQUIT
, SIG_IGN
);
288 signal(SIGHUP
, (void (*)(int)) get_exportlist
);
289 { FILE *pidfile
= fopen(_PATH_MOUNTDPID
, "w");
290 if (pidfile
!= NULL
) {
291 fprintf(pidfile
, "%d\n", getpid());
295 if (!resvport_only
) {
297 mib
[1] = vfc
.vfc_typenum
;
298 mib
[2] = NFS_NFSPRIVPORT
;
299 if (sysctl(mib
, 3, NULL
, NULL
, &resvport_only
,
300 sizeof(resvport_only
)) != 0 && errno
!= ENOENT
) {
301 syslog(LOG_ERR
, "sysctl: %m");
305 if ((udptransp
= svcudp_create(RPC_ANYSOCK
)) == NULL
||
306 (tcptransp
= svctcp_create(RPC_ANYSOCK
, 0, 0)) == NULL
) {
307 syslog(LOG_ERR
, "can't create socket");
310 pmap_unset(RPCPROG_MNT
, 1);
311 pmap_unset(RPCPROG_MNT
, 3);
313 if (!svc_register(udptransp
, RPCPROG_MNT
, 3, mntsrv
, IPPROTO_UDP
) ||
314 !svc_register(tcptransp
, RPCPROG_MNT
, 3, mntsrv
, IPPROTO_TCP
)) {
315 syslog(LOG_ERR
, "can't register mount");
318 if (!svc_register(udptransp
, RPCPROG_MNT
, 1, mntsrv
, IPPROTO_UDP
) ||
319 !svc_register(tcptransp
, RPCPROG_MNT
, 1, mntsrv
, IPPROTO_TCP
)) {
320 syslog(LOG_ERR
, "can't register mount");
324 syslog(LOG_ERR
, "mountd died");
332 "usage: mountd [-2] [-d] [-l] [-n] [-r] [export_file]\n");
337 * The mount rpc service
340 mntsrv(struct svc_req
*rqstp
, SVCXPRT
*transp
)
342 struct exportlist
*ep
;
348 struct in_addr saddrin
;
351 char rpcpath
[RPCMNT_PATHLEN
+ 1], dirpath
[MAXPATHLEN
];
352 int bad
= 0, defset
, hostset
;
353 sigset_t sighup_mask
;
355 sigemptyset(&sighup_mask
);
356 sigaddset(&sighup_mask
, SIGHUP
);
357 saddr
= transp
->xp_raddr
.sin_addr
.s_addr
;
358 saddrin
= transp
->xp_raddr
.sin_addr
;
359 sport
= ntohs(transp
->xp_raddr
.sin_port
);
360 hp
= (struct hostent
*)NULL
;
361 switch (rqstp
->rq_proc
) {
363 if (!svc_sendreply(transp
, xdr_void
, (caddr_t
)NULL
))
364 syslog(LOG_ERR
, "can't send reply");
367 if (sport
>= IPPORT_RESERVED
&& resvport_only
) {
369 "mount request from %s from unprivileged port",
371 svcerr_weakauth(transp
);
374 if (!svc_getargs(transp
, xdr_dir
, rpcpath
)) {
375 syslog(LOG_NOTICE
, "undecodable mount request from %s",
377 svcerr_decode(transp
);
382 * Get the real pathname and make sure it is a directory
383 * or a regular file if the -r option was specified
386 if (realpath(rpcpath
, dirpath
) == NULL
||
387 stat(dirpath
, &stb
) < 0 ||
388 (!S_ISDIR(stb
.st_mode
) &&
389 (dir_only
|| !S_ISREG(stb
.st_mode
))) ||
390 statfs(dirpath
, &fsb
) < 0) {
391 chdir("/"); /* Just in case realpath doesn't */
393 "mount request from %s for non existent path %s",
394 inet_ntoa(saddrin
), dirpath
);
396 warnx("stat failed on %s", dirpath
);
397 bad
= ENOENT
; /* We will send error reply later */
400 /* Check in the exports list */
401 sigprocmask(SIG_BLOCK
, &sighup_mask
, NULL
);
402 ep
= ex_search(&fsb
.f_fsid
);
403 hostset
= defset
= 0;
404 if (ep
&& (chk_host(ep
->ex_defdir
, saddr
, &defset
, &hostset
) ||
405 ((dp
= dirp_search(ep
->ex_dirl
, dirpath
)) &&
406 chk_host(dp
, saddr
, &defset
, &hostset
)) ||
407 (defset
&& scan_tree(ep
->ex_defdir
, saddr
) == 0 &&
408 scan_tree(ep
->ex_dirl
, saddr
) == 0))) {
410 if (!svc_sendreply(transp
, xdr_long
,
412 syslog(LOG_ERR
, "can't send reply");
413 sigprocmask(SIG_UNBLOCK
, &sighup_mask
, NULL
);
416 if (hostset
& DP_HOSTSET
)
417 fhr
.fhr_flag
= hostset
;
419 fhr
.fhr_flag
= defset
;
420 fhr
.fhr_vers
= rqstp
->rq_vers
;
421 /* Get the file handle */
422 memset(&fhr
.fhr_fh
, 0, sizeof(nfsfh_t
));
423 if (getfh(dirpath
, (fhandle_t
*)&fhr
.fhr_fh
) < 0) {
425 syslog(LOG_ERR
, "can't get fh for %s", dirpath
);
426 if (!svc_sendreply(transp
, xdr_long
,
428 syslog(LOG_ERR
, "can't send reply");
429 sigprocmask(SIG_UNBLOCK
, &sighup_mask
, NULL
);
432 if (!svc_sendreply(transp
, xdr_fhs
, (caddr_t
)&fhr
))
433 syslog(LOG_ERR
, "can't send reply");
435 hp
= gethostbyaddr((caddr_t
)&saddr
,
436 sizeof(saddr
), AF_INET
);
438 add_mlist(hp
->h_name
, dirpath
);
440 add_mlist(inet_ntoa(saddrin
),
443 warnx("mount successful");
446 "mount request succeeded from %s for %s",
447 inet_ntoa(saddrin
), dirpath
);
451 "mount request denied from %s for %s",
452 inet_ntoa(saddrin
), dirpath
);
455 if (bad
&& !svc_sendreply(transp
, xdr_long
, (caddr_t
)&bad
))
456 syslog(LOG_ERR
, "can't send reply");
457 sigprocmask(SIG_UNBLOCK
, &sighup_mask
, NULL
);
460 if (!svc_sendreply(transp
, xdr_mlist
, (caddr_t
)NULL
))
461 syslog(LOG_ERR
, "can't send reply");
464 "dump request succeeded from %s",
468 if (sport
>= IPPORT_RESERVED
&& resvport_only
) {
470 "umount request from %s from unprivileged port",
472 svcerr_weakauth(transp
);
475 if (!svc_getargs(transp
, xdr_dir
, rpcpath
)) {
476 syslog(LOG_NOTICE
, "undecodable umount request from %s",
478 svcerr_decode(transp
);
481 if (realpath(rpcpath
, dirpath
) == NULL
) {
482 syslog(LOG_NOTICE
, "umount request from %s "
483 "for non existent path %s",
484 inet_ntoa(saddrin
), dirpath
);
486 if (!svc_sendreply(transp
, xdr_void
, (caddr_t
)NULL
))
487 syslog(LOG_ERR
, "can't send reply");
488 hp
= gethostbyaddr((caddr_t
)&saddr
, sizeof(saddr
), AF_INET
);
490 del_mlist(hp
->h_name
, dirpath
);
491 del_mlist(inet_ntoa(saddrin
), dirpath
);
494 "umount request succeeded from %s for %s",
495 inet_ntoa(saddrin
), dirpath
);
498 if (sport
>= IPPORT_RESERVED
&& resvport_only
) {
500 "umountall request from %s from unprivileged port",
502 svcerr_weakauth(transp
);
505 if (!svc_sendreply(transp
, xdr_void
, (caddr_t
)NULL
))
506 syslog(LOG_ERR
, "can't send reply");
507 hp
= gethostbyaddr((caddr_t
)&saddr
, sizeof(saddr
), AF_INET
);
509 del_mlist(hp
->h_name
, (char *)NULL
);
510 del_mlist(inet_ntoa(saddrin
), (char *)NULL
);
513 "umountall request succeeded from %s",
517 if (!svc_sendreply(transp
, xdr_explist
, (caddr_t
)NULL
))
518 syslog(LOG_ERR
, "can't send reply");
521 "export request succeeded from %s",
525 svcerr_noproc(transp
);
531 * Xdr conversion for a dirpath string
534 xdr_dir(XDR
*xdrsp
, char *dirp
)
536 return (xdr_string(xdrsp
, &dirp
, RPCMNT_PATHLEN
));
540 * Xdr routine to generate file handle reply
543 xdr_fhs(XDR
*xdrsp
, caddr_t cp
)
545 struct fhreturn
*fhrp
= (struct fhreturn
*)cp
;
546 u_long ok
= 0, len
, auth
;
548 if (!xdr_long(xdrsp
, &ok
))
550 switch (fhrp
->fhr_vers
) {
552 return (xdr_opaque(xdrsp
, (caddr_t
)&fhrp
->fhr_fh
, NFSX_V2FH
));
555 if (!xdr_long(xdrsp
, &len
))
557 if (!xdr_opaque(xdrsp
, (caddr_t
)&fhrp
->fhr_fh
, len
))
559 if (fhrp
->fhr_flag
& DP_KERB
)
560 auth
= RPCAUTH_KERB4
;
564 if (!xdr_long(xdrsp
, &len
))
566 return (xdr_long(xdrsp
, &auth
));
572 xdr_mlist(XDR
*xdrsp
, caddr_t cp
)
574 struct mountlist
*mlp
;
581 if (!xdr_bool(xdrsp
, &true))
583 strp
= &mlp
->ml_host
[0];
584 if (!xdr_string(xdrsp
, &strp
, RPCMNT_NAMELEN
))
586 strp
= &mlp
->ml_dirp
[0];
587 if (!xdr_string(xdrsp
, &strp
, RPCMNT_PATHLEN
))
591 if (!xdr_bool(xdrsp
, &false))
597 * Xdr conversion for export list
600 xdr_explist(XDR
*xdrsp
, caddr_t cp
)
602 struct exportlist
*ep
;
605 sigset_t sighup_mask
;
607 sigemptyset(&sighup_mask
);
608 sigaddset(&sighup_mask
, SIGHUP
);
609 sigprocmask(SIG_BLOCK
, &sighup_mask
, NULL
);
613 if (put_exlist(ep
->ex_dirl
, xdrsp
, ep
->ex_defdir
, &putdef
))
615 if (ep
->ex_defdir
&& putdef
== 0 &&
616 put_exlist(ep
->ex_defdir
, xdrsp
, (struct dirlist
*)NULL
,
621 sigprocmask(SIG_UNBLOCK
, &sighup_mask
, NULL
);
622 if (!xdr_bool(xdrsp
, &false))
626 sigprocmask(SIG_UNBLOCK
, &sighup_mask
, NULL
);
631 * Called from xdr_explist() to traverse the tree and export the
635 put_exlist(struct dirlist
*dp
, XDR
*xdrsp
, struct dirlist
*adp
, int *putdefp
)
637 struct grouplist
*grp
;
645 if (put_exlist(dp
->dp_left
, xdrsp
, adp
, putdefp
))
647 if (!xdr_bool(xdrsp
, &true))
650 if (!xdr_string(xdrsp
, &strp
, RPCMNT_PATHLEN
))
652 if (adp
&& !strcmp(dp
->dp_dirp
, adp
->dp_dirp
)) {
656 if ((dp
->dp_flag
& DP_DEFSET
) == 0 &&
657 (gotalldir
== 0 || (adp
->dp_flag
& DP_DEFSET
) == 0)) {
661 if (grp
->gr_type
== GT_HOST
) {
662 if (!xdr_bool(xdrsp
, &true))
664 strp
= grp
->gr_ptr
.gt_hostent
->h_name
;
665 if (!xdr_string(xdrsp
, &strp
,
668 } else if (grp
->gr_type
== GT_NET
) {
669 if (!xdr_bool(xdrsp
, &true))
671 strp
= grp
->gr_ptr
.gt_net
.nt_name
;
672 if (!xdr_string(xdrsp
, &strp
,
677 if (gotalldir
&& hp
== (struct hostlist
*)NULL
) {
683 if (!xdr_bool(xdrsp
, &false))
685 if (put_exlist(dp
->dp_right
, xdrsp
, adp
, putdefp
))
691 #define LINESIZ 10240
696 * Get the export list
701 struct exportlist
*ep
, *ep2
;
702 struct grouplist
*grp
, *tgrp
;
703 struct exportlist
**epp
;
704 struct dirlist
*dirhead
;
705 struct statfs fsb
, *fsp
;
708 char *cp
, *endcp
, *dirp
, *hst
, *usr
, *dom
, savedc
;
709 int len
, has_host
, exflags
, got_nondir
, dirplen
, num
, i
, netgrp
;
715 * First, get rid of the old list
723 exphead
= (struct exportlist
*)NULL
;
731 grphead
= (struct grouplist
*)NULL
;
734 * And delete exports that are in the kernel for all local
736 * XXX: Should know how to handle all local exportable file systems
737 * instead of just "ufs".
739 num
= getmntinfo(&fsp
, MNT_NOWAIT
);
740 for (i
= 0; i
< num
; i
++) {
745 struct msdosfs_args da
;
749 if (!strcmp(fsp
->f_fstypename
, "mfs") ||
750 !strcmp(fsp
->f_fstypename
, "ufs") ||
751 !strcmp(fsp
->f_fstypename
, "msdos") ||
752 !strcmp(fsp
->f_fstypename
, "ntfs") ||
753 !strcmp(fsp
->f_fstypename
, "cd9660")) {
754 targs
.ua
.fspec
= NULL
;
755 targs
.ua
.export
.ex_flags
= MNT_DELEXPORT
;
756 if (mount(fsp
->f_fstypename
, fsp
->f_mntonname
,
757 fsp
->f_flags
| MNT_UPDATE
,
758 (caddr_t
)&targs
) < 0)
759 syslog(LOG_ERR
, "can't delete exports for %s",
766 * Read in the exports file and build the list, calling
767 * mount() as we go along to push the export rules into the kernel.
769 if ((exp_file
= fopen(exname
, "r")) == NULL
) {
770 syslog(LOG_ERR
, "can't open %s", exname
);
773 dirhead
= (struct dirlist
*)NULL
;
776 warnx("got line %s", line
);
778 nextfield(&cp
, &endcp
);
787 exflags
= MNT_EXPORTED
;
790 ep
= (struct exportlist
*)NULL
;
793 * Create new exports list entry
796 tgrp
= grp
= get_grp();
798 if (len
> RPCMNT_NAMELEN
) {
799 getexp_err(ep
, tgrp
);
803 if (ep
== (struct exportlist
*)NULL
) {
804 getexp_err(ep
, tgrp
);
808 warnx("doing opt %s", cp
);
810 if (do_opt(&cp
, &endcp
, ep
, grp
, &has_host
,
812 getexp_err(ep
, tgrp
);
815 } else if (*cp
== '/') {
818 if (check_dirpath(cp
) &&
819 statfs(cp
, &fsb
) >= 0) {
821 syslog(LOG_ERR
, "dirs must be first");
822 getexp_err(ep
, tgrp
);
826 if (ep
->ex_fs
.val
[0] != fsb
.f_fsid
.val
[0] ||
827 ep
->ex_fs
.val
[1] != fsb
.f_fsid
.val
[1]) {
828 getexp_err(ep
, tgrp
);
833 * See if this directory is already
836 ep
= ex_search(&fsb
.f_fsid
);
837 if (ep
== (struct exportlist
*)NULL
) {
839 ep
->ex_fs
= fsb
.f_fsid
;
840 ep
->ex_fsdir
= (char *)
841 malloc(strlen(fsb
.f_mntonname
) + 1);
848 warnx("making new ep fs=0x%x,0x%x",
852 warnx("found ep fs=0x%x,0x%x",
858 * Add dirpath to export mount point.
860 dirp
= add_expdir(&dirhead
, cp
, len
);
863 getexp_err(ep
, tgrp
);
871 if (ep
== (struct exportlist
*)NULL
) {
872 getexp_err(ep
, tgrp
);
877 * Get the host or netgroup.
880 netgrp
= getnetgrent(&hst
, &usr
, &dom
);
883 grp
->gr_next
= get_grp();
889 "null hostname in netgroup %s, skipping", cp
);
890 grp
->gr_type
= GT_IGNORE
;
891 } else if (get_host(hst
, grp
, tgrp
)) {
893 "bad host %s in netgroup %s, skipping", hst
, cp
);
894 grp
->gr_type
= GT_IGNORE
;
896 } else if (get_host(cp
, grp
, tgrp
)) {
897 syslog(LOG_ERR
, "bad host %s, skipping", cp
);
898 grp
->gr_type
= GT_IGNORE
;
901 } while (netgrp
&& getnetgrent(&hst
, &usr
, &dom
));
906 nextfield(&cp
, &endcp
);
909 if (check_options(dirhead
)) {
910 getexp_err(ep
, tgrp
);
914 grp
->gr_type
= GT_HOST
;
916 warnx("adding a default entry");
917 /* add a default group and make the grp list NULL */
918 hpe
= (struct hostent
*)malloc(sizeof(struct hostent
));
919 if (hpe
== (struct hostent
*)NULL
)
921 hpe
->h_name
= strdup("Default");
922 hpe
->h_addrtype
= AF_INET
;
923 hpe
->h_length
= sizeof (u_int32_t
);
924 hpe
->h_addr_list
= (char **)NULL
;
925 grp
->gr_ptr
.gt_hostent
= hpe
;
928 * Don't allow a network export coincide with a list of
929 * host(s) on the same line.
931 } else if ((opt_flags
& OP_NET
) && tgrp
->gr_next
) {
932 getexp_err(ep
, tgrp
);
936 * If an export list was specified on this line, make sure
937 * that we have at least one valid entry, otherwise skip it.
941 while (grp
&& grp
->gr_type
== GT_IGNORE
)
944 getexp_err(ep
, tgrp
);
950 * Loop through hosts, pushing the exports into the kernel.
951 * After loop, tgrp points to the start of the list and
952 * grp points to the last entry in the list.
956 if (do_mount(ep
, grp
, exflags
, &anon
, dirp
,
958 getexp_err(ep
, tgrp
);
961 } while (grp
->gr_next
&& (grp
= grp
->gr_next
));
964 * Success. Update the data structures.
967 hang_dirp(dirhead
, tgrp
, ep
, opt_flags
);
968 grp
->gr_next
= grphead
;
971 hang_dirp(dirhead
, (struct grouplist
*)NULL
, ep
,
975 dirhead
= (struct dirlist
*)NULL
;
976 if ((ep
->ex_flag
& EX_LINKED
) == 0) {
981 * Insert in the list in alphabetical order.
983 while (ep2
&& strcmp(ep2
->ex_fsdir
, ep
->ex_fsdir
) < 0) {
990 ep
->ex_flag
|= EX_LINKED
;
995 dirhead
= (struct dirlist
*)NULL
;
1002 * Allocate an export list element
1007 struct exportlist
*ep
;
1009 ep
= (struct exportlist
*)malloc(sizeof (struct exportlist
));
1010 if (ep
== (struct exportlist
*)NULL
)
1012 memset(ep
, 0, sizeof(struct exportlist
));
1017 * Allocate a group list element
1022 struct grouplist
*gp
;
1024 gp
= (struct grouplist
*)malloc(sizeof (struct grouplist
));
1025 if (gp
== (struct grouplist
*)NULL
)
1027 memset(gp
, 0, sizeof(struct grouplist
));
1032 * Clean up upon an error in get_exportlist().
1035 getexp_err(struct exportlist
*ep
, struct grouplist
*grp
)
1037 struct grouplist
*tgrp
;
1039 if (!(opt_flags
& OP_QUIET
))
1040 syslog(LOG_ERR
, "bad exports list line %s", line
);
1041 if (ep
&& (ep
->ex_flag
& EX_LINKED
) == 0)
1051 * Search the export list for a matching fs.
1054 ex_search(fsid_t
*fsid
)
1056 struct exportlist
*ep
;
1060 if (ep
->ex_fs
.val
[0] == fsid
->val
[0] &&
1061 ep
->ex_fs
.val
[1] == fsid
->val
[1])
1069 * Add a directory path to the list.
1072 add_expdir(struct dirlist
**dpp
, char *cp
, int len
)
1076 dp
= (struct dirlist
*)malloc(sizeof (struct dirlist
) + len
);
1077 if (dp
== (struct dirlist
*)NULL
)
1080 dp
->dp_right
= (struct dirlist
*)NULL
;
1082 dp
->dp_hosts
= (struct hostlist
*)NULL
;
1083 strcpy(dp
->dp_dirp
, cp
);
1085 return (dp
->dp_dirp
);
1089 * Hang the dir list element off the dirpath binary tree as required
1090 * and update the entry for host.
1093 hang_dirp(struct dirlist
*dp
, struct grouplist
*grp
, struct exportlist
*ep
,
1096 struct hostlist
*hp
;
1097 struct dirlist
*dp2
;
1099 if (flags
& OP_ALLDIRS
) {
1104 if (grp
== (struct grouplist
*)NULL
) {
1105 ep
->ex_defdir
->dp_flag
|= DP_DEFSET
;
1106 if (flags
& OP_KERB
)
1107 ep
->ex_defdir
->dp_flag
|= DP_KERB
;
1108 } else while (grp
) {
1110 if (flags
& OP_KERB
)
1111 hp
->ht_flag
|= DP_KERB
;
1113 hp
->ht_next
= ep
->ex_defdir
->dp_hosts
;
1114 ep
->ex_defdir
->dp_hosts
= hp
;
1120 * Loop through the directories adding them to the tree.
1124 add_dlist(&ep
->ex_dirl
, dp
, grp
, flags
);
1131 * Traverse the binary tree either updating a node that is already there
1132 * for the new directory or adding the new node.
1135 add_dlist(struct dirlist
**dpp
, struct dirlist
*newdp
, struct grouplist
*grp
,
1139 struct hostlist
*hp
;
1144 cmp
= strcmp(dp
->dp_dirp
, newdp
->dp_dirp
);
1146 add_dlist(&dp
->dp_left
, newdp
, grp
, flags
);
1148 } else if (cmp
< 0) {
1149 add_dlist(&dp
->dp_right
, newdp
, grp
, flags
);
1152 free((caddr_t
)newdp
);
1155 dp
->dp_left
= (struct dirlist
*)NULL
;
1161 * Hang all of the host(s) off of the directory point.
1165 if (flags
& OP_KERB
)
1166 hp
->ht_flag
|= DP_KERB
;
1168 hp
->ht_next
= dp
->dp_hosts
;
1173 dp
->dp_flag
|= DP_DEFSET
;
1174 if (flags
& OP_KERB
)
1175 dp
->dp_flag
|= DP_KERB
;
1180 * Search for a dirpath on the export point.
1183 dirp_search(struct dirlist
*dp
, char *dirpath
)
1188 cmp
= strcmp(dp
->dp_dirp
, dirpath
);
1190 return (dirp_search(dp
->dp_left
, dirpath
));
1192 return (dirp_search(dp
->dp_right
, dirpath
));
1200 * Scan for a host match in a directory tree.
1203 chk_host(struct dirlist
*dp
, u_int32_t saddr
, int *defsetp
, int *hostsetp
)
1205 struct hostlist
*hp
;
1206 struct grouplist
*grp
;
1210 if (dp
->dp_flag
& DP_DEFSET
)
1211 *defsetp
= dp
->dp_flag
;
1215 switch (grp
->gr_type
) {
1217 addrp
= (u_int32_t
**)
1218 grp
->gr_ptr
.gt_hostent
->h_addr_list
;
1220 if (**addrp
== saddr
) {
1221 *hostsetp
= (hp
->ht_flag
| DP_HOSTSET
);
1228 if ((saddr
& grp
->gr_ptr
.gt_net
.nt_mask
) ==
1229 grp
->gr_ptr
.gt_net
.nt_net
) {
1230 *hostsetp
= (hp
->ht_flag
| DP_HOSTSET
);
1242 * Scan tree for a host that matches the address.
1245 scan_tree(struct dirlist
*dp
, u_int32_t saddr
)
1247 int defset
, hostset
;
1250 if (scan_tree(dp
->dp_left
, saddr
))
1252 if (chk_host(dp
, saddr
, &defset
, &hostset
))
1254 if (scan_tree(dp
->dp_right
, saddr
))
1261 * Traverse the dirlist tree and free it up.
1264 free_dir(struct dirlist
*dp
)
1268 free_dir(dp
->dp_left
);
1269 free_dir(dp
->dp_right
);
1270 free_host(dp
->dp_hosts
);
1276 * Parse the option string and update fields.
1277 * Option arguments may either be -<option>=<value> or
1281 do_opt(char **cpp
, char **endcpp
, struct exportlist
*ep
, struct grouplist
*grp
,
1282 int *has_hostp
, int *exflagsp
, struct ucred
*cr
)
1284 char *cpoptarg
, *cpoptend
;
1285 char *cp
, *endcp
, *cpopt
, savedc
, savedc2
;
1286 int allflag
, usedarg
;
1294 while (cpopt
&& *cpopt
) {
1297 if ((cpoptend
= strchr(cpopt
, ','))) {
1299 if ((cpoptarg
= strchr(cpopt
, '=')))
1302 if ((cpoptarg
= strchr(cpopt
, '=')))
1306 nextfield(&cp
, &endcp
);
1308 if (endcp
> cp
&& *cp
!= '-') {
1316 if (!strcmp(cpopt
, "ro") || !strcmp(cpopt
, "o")) {
1317 *exflagsp
|= MNT_EXRDONLY
;
1318 } else if (cpoptarg
&& (!strcmp(cpopt
, "maproot") ||
1319 !(allflag
= strcmp(cpopt
, "mapall")) ||
1320 !strcmp(cpopt
, "root") || !strcmp(cpopt
, "r"))) {
1322 parsecred(cpoptarg
, cr
);
1324 *exflagsp
|= MNT_EXPORTANON
;
1325 opt_flags
|= OP_MAPALL
;
1327 opt_flags
|= OP_MAPROOT
;
1328 } else if (!strcmp(cpopt
, "kerb") || !strcmp(cpopt
, "k")) {
1329 *exflagsp
|= MNT_EXKERB
;
1330 opt_flags
|= OP_KERB
;
1331 } else if (cpoptarg
&& (!strcmp(cpopt
, "mask") ||
1332 !strcmp(cpopt
, "m"))) {
1333 if (get_net(cpoptarg
, &grp
->gr_ptr
.gt_net
, 1)) {
1334 syslog(LOG_ERR
, "bad mask: %s", cpoptarg
);
1338 opt_flags
|= OP_MASK
;
1339 } else if (cpoptarg
&& (!strcmp(cpopt
, "network") ||
1340 !strcmp(cpopt
, "n"))) {
1341 if (grp
->gr_type
!= GT_NULL
) {
1342 syslog(LOG_ERR
, "network/host conflict");
1344 } else if (get_net(cpoptarg
, &grp
->gr_ptr
.gt_net
, 0)) {
1345 syslog(LOG_ERR
, "bad net: %s", cpoptarg
);
1348 grp
->gr_type
= GT_NET
;
1351 opt_flags
|= OP_NET
;
1352 } else if (!strcmp(cpopt
, "alldirs")) {
1353 opt_flags
|= OP_ALLDIRS
;
1354 } else if (!strcmp(cpopt
, "public")) {
1355 *exflagsp
|= MNT_EXPUBLIC
;
1356 } else if (!strcmp(cpopt
, "webnfs")) {
1357 *exflagsp
|= (MNT_EXPUBLIC
|MNT_EXRDONLY
|MNT_EXPORTANON
);
1358 opt_flags
|= OP_MAPALL
;
1359 } else if (cpoptarg
&& !strcmp(cpopt
, "index")) {
1360 ep
->ex_indexfile
= strdup(cpoptarg
);
1361 } else if (!strcmp(cpopt
, "quiet")) {
1362 opt_flags
|= OP_QUIET
;
1364 syslog(LOG_ERR
, "bad opt %s", cpopt
);
1383 * Translate a character string to the corresponding list of network
1384 * addresses for a hostname.
1387 get_host(char *cp
, struct grouplist
*grp
, struct grouplist
*tgrp
)
1389 struct grouplist
*checkgrp
;
1390 struct hostent
*hp
, *nhp
;
1391 char **addrp
, **naddrp
;
1392 struct hostent t_host
;
1397 if (grp
->gr_type
!= GT_NULL
)
1399 if ((hp
= gethostbyname(cp
)) == NULL
) {
1401 saddr
= inet_addr(cp
);
1403 syslog(LOG_ERR
, "inet_addr failed for %s", cp
);
1406 if ((hp
= gethostbyaddr((caddr_t
)&saddr
, sizeof (saddr
),
1407 AF_INET
)) == NULL
) {
1410 hp
->h_addrtype
= AF_INET
;
1411 hp
->h_length
= sizeof (u_int32_t
);
1412 hp
->h_addr_list
= aptr
;
1413 aptr
[0] = (char *)&saddr
;
1414 aptr
[1] = (char *)NULL
;
1417 syslog(LOG_ERR
, "gethostbyname failed for %s", cp
);
1422 * Sanity check: make sure we don't already have an entry
1423 * for this host in the grouplist.
1426 while (checkgrp
!= NULL
) {
1427 if (checkgrp
->gr_type
== GT_HOST
&&
1428 checkgrp
->gr_ptr
.gt_hostent
!= NULL
&&
1429 (!strcmp(checkgrp
->gr_ptr
.gt_hostent
->h_name
, hp
->h_name
)
1430 || *(u_int32_t
*)checkgrp
->gr_ptr
.gt_hostent
->h_addr
==
1431 *(u_int32_t
*)hp
->h_addr
)) {
1432 grp
->gr_type
= GT_IGNORE
;
1435 checkgrp
= checkgrp
->gr_next
;
1438 grp
->gr_type
= GT_HOST
;
1439 nhp
= grp
->gr_ptr
.gt_hostent
= (struct hostent
*)
1440 malloc(sizeof(struct hostent
));
1441 if (nhp
== (struct hostent
*)NULL
)
1443 memmove(nhp
, hp
, sizeof(struct hostent
));
1444 i
= strlen(hp
->h_name
)+1;
1445 nhp
->h_name
= (char *)malloc(i
);
1446 if (nhp
->h_name
== (char *)NULL
)
1448 memmove(nhp
->h_name
, hp
->h_name
, i
);
1449 addrp
= hp
->h_addr_list
;
1453 naddrp
= nhp
->h_addr_list
= (char **)malloc(i
*sizeof(char *));
1454 if (naddrp
== (char **)NULL
)
1456 addrp
= hp
->h_addr_list
;
1458 *naddrp
= (char *)malloc(hp
->h_length
);
1459 if (*naddrp
== (char *)NULL
)
1461 memmove(*naddrp
, *addrp
, hp
->h_length
);
1465 *naddrp
= (char *)NULL
;
1467 warnx("got host %s", hp
->h_name
);
1472 * Free up an exports list component
1475 free_exp(struct exportlist
*ep
)
1478 if (ep
->ex_defdir
) {
1479 free_host(ep
->ex_defdir
->dp_hosts
);
1480 free((caddr_t
)ep
->ex_defdir
);
1484 if (ep
->ex_indexfile
)
1485 free(ep
->ex_indexfile
);
1486 free_dir(ep
->ex_dirl
);
1494 free_host(struct hostlist
*hp
)
1496 struct hostlist
*hp2
;
1508 struct hostlist
*hp
;
1510 hp
= (struct hostlist
*)malloc(sizeof (struct hostlist
));
1511 if (hp
== (struct hostlist
*)NULL
)
1513 hp
->ht_next
= (struct hostlist
*)NULL
;
1519 * Out of memory, fatal
1525 syslog(LOG_ERR
, "out of memory");
1530 * Do the mount syscall with the update flag to push the export info into
1534 do_mount(struct exportlist
*ep
, struct grouplist
*grp
, int exflags
,
1535 struct ucred
*anoncrp
, char *dirp
, int dirplen
, struct statfs
*fsb
)
1542 struct sockaddr_in sin
, imask
;
1548 struct msdosfs_args da
;
1550 struct ntfs_args na
;
1555 args
.ua
.export
.ex_flags
= exflags
;
1556 args
.ua
.export
.ex_anon
= *anoncrp
;
1557 args
.ua
.export
.ex_indexfile
= ep
->ex_indexfile
;
1558 memset(&sin
, 0, sizeof(sin
));
1559 memset(&imask
, 0, sizeof(imask
));
1560 sin
.sin_family
= AF_INET
;
1561 sin
.sin_len
= sizeof(sin
);
1562 imask
.sin_family
= AF_INET
;
1563 imask
.sin_len
= sizeof(sin
);
1564 if (grp
->gr_type
== GT_HOST
)
1565 addrp
= (u_int32_t
**)grp
->gr_ptr
.gt_hostent
->h_addr_list
;
1567 addrp
= (u_int32_t
**)NULL
;
1570 switch (grp
->gr_type
) {
1573 sin
.sin_addr
.s_addr
= **addrp
;
1574 args
.ua
.export
.ex_addrlen
= sizeof(sin
);
1576 args
.ua
.export
.ex_addrlen
= 0;
1577 args
.ua
.export
.ex_addr
= (struct sockaddr
*)&sin
;
1578 args
.ua
.export
.ex_masklen
= 0;
1581 if (grp
->gr_ptr
.gt_net
.nt_mask
)
1582 imask
.sin_addr
.s_addr
= grp
->gr_ptr
.gt_net
.nt_mask
;
1584 net
= ntohl(grp
->gr_ptr
.gt_net
.nt_net
);
1586 imask
.sin_addr
.s_addr
= inet_addr("255.0.0.0");
1587 else if (IN_CLASSB(net
))
1588 imask
.sin_addr
.s_addr
=
1589 inet_addr("255.255.0.0");
1591 imask
.sin_addr
.s_addr
=
1592 inet_addr("255.255.255.0");
1593 grp
->gr_ptr
.gt_net
.nt_mask
= imask
.sin_addr
.s_addr
;
1595 sin
.sin_addr
.s_addr
= grp
->gr_ptr
.gt_net
.nt_net
;
1596 args
.ua
.export
.ex_addr
= (struct sockaddr
*)&sin
;
1597 args
.ua
.export
.ex_addrlen
= sizeof (sin
);
1598 args
.ua
.export
.ex_mask
= (struct sockaddr
*)&imask
;
1599 args
.ua
.export
.ex_masklen
= sizeof (imask
);
1605 syslog(LOG_ERR
, "bad grouptype");
1613 * Maybe I should just use the fsb->f_mntonname path instead
1614 * of looping back up the dirp to the mount point??
1615 * Also, needs to know how to export all types of local
1616 * exportable file systems and not just "ufs".
1618 while (mount(fsb
->f_fstypename
, dirp
,
1619 fsb
->f_flags
| MNT_UPDATE
, (caddr_t
)&args
) < 0) {
1623 cp
= dirp
+ dirplen
- 1;
1624 if (opt_flags
& OP_QUIET
)
1626 if (errno
== EPERM
) {
1628 "can't change attributes for %s", dirp
);
1631 if (opt_flags
& OP_ALLDIRS
) {
1632 if (errno
== EINVAL
)
1634 "-alldirs requested but %s is not a filesystem mountpoint",
1638 "could not remount %s: %m",
1642 /* back up over the last component */
1643 while (*cp
== '/' && cp
> dirp
)
1645 while (*(cp
- 1) != '/' && cp
> dirp
)
1649 warnx("mnt unsucc");
1650 syslog(LOG_ERR
, "can't export %s", dirp
);
1655 /* Check that we're still on the same filesystem. */
1656 if (statfs(dirp
, &fsb1
) != 0 || bcmp(&fsb1
.f_fsid
,
1657 &fsb
->f_fsid
, sizeof(fsb1
.f_fsid
)) != 0) {
1659 syslog(LOG_ERR
, "can't export %s", dirp
);
1665 if (*addrp
== (u_int32_t
*)NULL
)
1676 * Translate a net address.
1679 get_net(char *cp
, struct netmsk
*net
, int maskflg
)
1683 struct in_addr inetaddr
, inetaddr2
;
1686 if (isdigit(*cp
) && ((netaddr
= inet_network(cp
)) != -1)) {
1687 inetaddr
= inet_makeaddr(netaddr
, 0);
1689 * Due to arbitrary subnet masks, you don't know how many
1690 * bits to shift the address to make it into a network,
1691 * however you do know how to make a network address into
1692 * a host with host == 0 and then compare them.
1697 while ((np
= getnetent())) {
1698 inetaddr2
= inet_makeaddr(np
->n_net
, 0);
1699 if (inetaddr2
.s_addr
== inetaddr
.s_addr
)
1704 } else if ((np
= getnetbyname(cp
)) != NULL
) {
1705 inetaddr
= inet_makeaddr(np
->n_net
, 0);
1710 net
->nt_mask
= inetaddr
.s_addr
;
1715 name
= inet_ntoa(inetaddr
);
1716 net
->nt_name
= (char *)malloc(strlen(name
) + 1);
1717 if (net
->nt_name
== (char *)NULL
)
1719 strcpy(net
->nt_name
, name
);
1720 net
->nt_net
= inetaddr
.s_addr
;
1726 * Parse out the next white space separated field
1729 nextfield(char **cp
, char **endcp
)
1734 while (*p
== ' ' || *p
== '\t')
1736 if (*p
== '\n' || *p
== '\0')
1740 while (*p
!= ' ' && *p
!= '\t' && *p
!= '\n' && *p
!= '\0')
1747 * Get an exports file line. Skip over blank lines and handle line
1755 int totlen
, cont_line
;
1758 * Loop around ignoring blank lines and getting all continuation lines.
1763 if (fgets(p
, LINESIZ
- totlen
, exp_file
) == NULL
)
1769 (*cp
== ' ' || *cp
== '\t' || *cp
== '\n' || *cp
== '\\')) {
1782 if (totlen
>= LINESIZ
) {
1783 syslog(LOG_ERR
, "exports line too long");
1788 } while (totlen
== 0 || cont_line
);
1793 * Parse a description of a credential.
1796 parsecred(char *namelist
, struct ucred
*cr
)
1803 int ngroups
, groups
[NGROUPS
+ 1];
1806 * Set up the unprivileged user.
1810 cr
->cr_groups
[0] = -2;
1813 * Get the user's password table entry.
1815 names
= strsep(&namelist
, " \t\n");
1816 name
= strsep(&names
, ":");
1817 if (isdigit(*name
) || *name
== '-')
1818 pw
= getpwuid(atoi(name
));
1820 pw
= getpwnam(name
);
1822 * Credentials specified as those of a user.
1824 if (names
== NULL
) {
1826 syslog(LOG_ERR
, "unknown user: %s", name
);
1829 cr
->cr_uid
= pw
->pw_uid
;
1830 ngroups
= NGROUPS
+ 1;
1831 if (getgrouplist(pw
->pw_name
, pw
->pw_gid
, groups
, &ngroups
))
1832 syslog(LOG_ERR
, "too many groups");
1834 * Convert from int's to gid_t's and compress out duplicate
1836 cr
->cr_ngroups
= ngroups
- 1;
1837 cr
->cr_groups
[0] = groups
[0];
1838 for (cnt
= 2; cnt
< ngroups
; cnt
++)
1839 cr
->cr_groups
[cnt
- 1] = groups
[cnt
];
1843 * Explicit credential specified as a colon separated list:
1847 cr
->cr_uid
= pw
->pw_uid
;
1848 else if (isdigit(*name
) || *name
== '-')
1849 cr
->cr_uid
= atoi(name
);
1851 syslog(LOG_ERR
, "unknown user: %s", name
);
1855 while (names
!= NULL
&& *names
!= '\0' && cr
->cr_ngroups
< NGROUPS
) {
1856 name
= strsep(&names
, ":");
1857 if (isdigit(*name
) || *name
== '-') {
1858 cr
->cr_groups
[cr
->cr_ngroups
++] = atoi(name
);
1860 if ((gr
= getgrnam(name
)) == NULL
) {
1861 syslog(LOG_ERR
, "unknown group: %s", name
);
1864 cr
->cr_groups
[cr
->cr_ngroups
++] = gr
->gr_gid
;
1867 if (names
!= NULL
&& *names
!= '\0' && cr
->cr_ngroups
== NGROUPS
)
1868 syslog(LOG_ERR
, "too many groups");
1871 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
1873 * Routines that maintain the remote mounttab
1878 struct mountlist
*mlp
, **mlpp
;
1879 char *host
, *dirp
, *cp
;
1883 if ((mlfile
= fopen(_PATH_RMOUNTLIST
, "r")) == NULL
) {
1884 if (errno
== ENOENT
)
1887 syslog(LOG_ERR
, "can't open %s", _PATH_RMOUNTLIST
);
1892 while (fgets(str
, STRSIZ
, mlfile
) != NULL
) {
1894 host
= strsep(&cp
, " \t\n");
1895 dirp
= strsep(&cp
, " \t\n");
1896 if (host
== NULL
|| dirp
== NULL
)
1898 mlp
= (struct mountlist
*)malloc(sizeof (*mlp
));
1899 if (mlp
== (struct mountlist
*)NULL
)
1901 strncpy(mlp
->ml_host
, host
, RPCMNT_NAMELEN
);
1902 mlp
->ml_host
[RPCMNT_NAMELEN
] = '\0';
1903 strncpy(mlp
->ml_dirp
, dirp
, RPCMNT_PATHLEN
);
1904 mlp
->ml_dirp
[RPCMNT_PATHLEN
] = '\0';
1905 mlp
->ml_next
= (struct mountlist
*)NULL
;
1907 mlpp
= &mlp
->ml_next
;
1913 del_mlist(char *hostp
, char *dirp
)
1915 struct mountlist
*mlp
, **mlpp
;
1916 struct mountlist
*mlp2
;
1923 if (!strcmp(mlp
->ml_host
, hostp
) &&
1924 (!dirp
|| !strcmp(mlp
->ml_dirp
, dirp
))) {
1927 *mlpp
= mlp
= mlp
->ml_next
;
1928 free((caddr_t
)mlp2
);
1930 mlpp
= &mlp
->ml_next
;
1935 if ((mlfile
= fopen(_PATH_RMOUNTLIST
, "w")) == NULL
) {
1936 syslog(LOG_ERR
,"can't update %s", _PATH_RMOUNTLIST
);
1941 fprintf(mlfile
, "%s %s\n", mlp
->ml_host
, mlp
->ml_dirp
);
1949 add_mlist(char *hostp
, char *dirp
)
1951 struct mountlist
*mlp
, **mlpp
;
1957 if (!strcmp(mlp
->ml_host
, hostp
) && !strcmp(mlp
->ml_dirp
, dirp
))
1959 mlpp
= &mlp
->ml_next
;
1962 mlp
= (struct mountlist
*)malloc(sizeof (*mlp
));
1963 if (mlp
== (struct mountlist
*)NULL
)
1965 strncpy(mlp
->ml_host
, hostp
, RPCMNT_NAMELEN
);
1966 mlp
->ml_host
[RPCMNT_NAMELEN
] = '\0';
1967 strncpy(mlp
->ml_dirp
, dirp
, RPCMNT_PATHLEN
);
1968 mlp
->ml_dirp
[RPCMNT_PATHLEN
] = '\0';
1969 mlp
->ml_next
= (struct mountlist
*)NULL
;
1971 if ((mlfile
= fopen(_PATH_RMOUNTLIST
, "a")) == NULL
) {
1972 syslog(LOG_ERR
, "can't update %s", _PATH_RMOUNTLIST
);
1975 fprintf(mlfile
, "%s %s\n", mlp
->ml_host
, mlp
->ml_dirp
);
1980 * Free up a group list.
1983 free_grp(struct grouplist
*grp
)
1987 if (grp
->gr_type
== GT_HOST
) {
1988 if (grp
->gr_ptr
.gt_hostent
->h_name
) {
1989 addrp
= grp
->gr_ptr
.gt_hostent
->h_addr_list
;
1990 while (addrp
&& *addrp
)
1992 free((caddr_t
)grp
->gr_ptr
.gt_hostent
->h_addr_list
);
1993 free(grp
->gr_ptr
.gt_hostent
->h_name
);
1995 free((caddr_t
)grp
->gr_ptr
.gt_hostent
);
1996 } else if (grp
->gr_type
== GT_NET
) {
1997 if (grp
->gr_ptr
.gt_net
.nt_name
)
1998 free(grp
->gr_ptr
.gt_net
.nt_name
);
2005 SYSLOG(int pri
, const char *fmt
, ...)
2010 vfprintf(stderr
, fmt
, ap
);
2016 * Check options for consistency.
2019 check_options(struct dirlist
*dp
)
2022 if (dp
== (struct dirlist
*)NULL
)
2024 if ((opt_flags
& (OP_MAPROOT
| OP_MAPALL
)) == (OP_MAPROOT
| OP_MAPALL
) ||
2025 (opt_flags
& (OP_MAPROOT
| OP_KERB
)) == (OP_MAPROOT
| OP_KERB
) ||
2026 (opt_flags
& (OP_MAPALL
| OP_KERB
)) == (OP_MAPALL
| OP_KERB
)) {
2027 syslog(LOG_ERR
, "-mapall, -maproot and -kerb mutually exclusive");
2030 if ((opt_flags
& OP_MASK
) && (opt_flags
& OP_NET
) == 0) {
2031 syslog(LOG_ERR
, "-mask requires -network");
2034 if ((opt_flags
& OP_ALLDIRS
) && dp
->dp_left
) {
2035 syslog(LOG_ERR
, "-alldirs has multiple directories");
2042 * Check an absolute directory path for any symbolic links. Return true
2043 * if no symbolic links are found.
2046 check_dirpath(char *dirp
)
2053 while (*cp
&& ret
) {
2056 if (lstat(dirp
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
))
2062 if (lstat(dirp
, &sb
) < 0 || !S_ISDIR(sb
.st_mode
))
2068 * Just translate an ascii string to an integer.
2076 if (*cp
< '0' || *cp
> '9')
2078 res
= res
* 10 + (*cp
++ - '0');