Home | History | Annotate | Line # | Download | only in mountd
mountd.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1989 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Rick Macklem at The University of Guelph.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.1  cgd char copyright[] =
     39  1.1  cgd "@(#) Copyright (c) 1989 Regents of the University of California.\n\
     40  1.1  cgd  All rights reserved.\n";
     41  1.1  cgd #endif not lint
     42  1.1  cgd 
     43  1.1  cgd #ifndef lint
     44  1.1  cgd static char sccsid[] = "@(#)mountd.c	5.14 (Berkeley) 2/26/91";
     45  1.1  cgd #endif not lint
     46  1.1  cgd 
     47  1.1  cgd #include <sys/param.h>
     48  1.1  cgd #include <sys/ioctl.h>
     49  1.1  cgd #include <sys/stat.h>
     50  1.1  cgd #include <sys/file.h>
     51  1.1  cgd #include <sys/mount.h>
     52  1.1  cgd #include <sys/socket.h>
     53  1.1  cgd #include <sys/errno.h>
     54  1.1  cgd #include <sys/signal.h>
     55  1.1  cgd #include <stdio.h>
     56  1.1  cgd #include <string.h>
     57  1.1  cgd #include <syslog.h>
     58  1.1  cgd #include <netdb.h>
     59  1.1  cgd #include <rpc/rpc.h>
     60  1.1  cgd #include <rpc/pmap_clnt.h>
     61  1.1  cgd #include <rpc/pmap_prot.h>
     62  1.1  cgd #include <nfs/rpcv2.h>
     63  1.1  cgd #include <nfs/nfsv2.h>
     64  1.1  cgd #include "pathnames.h"
     65  1.1  cgd 
     66  1.1  cgd struct ufid {
     67  1.1  cgd 	u_short	ufid_len;
     68  1.1  cgd 	ino_t	ufid_ino;
     69  1.1  cgd 	long	ufid_gen;
     70  1.1  cgd };
     71  1.1  cgd /*
     72  1.1  cgd  * Structures for keeping the mount list and export list
     73  1.1  cgd  */
     74  1.1  cgd struct mountlist {
     75  1.1  cgd 	struct mountlist *ml_next;
     76  1.1  cgd 	char	ml_host[RPCMNT_NAMELEN+1];
     77  1.1  cgd 	char	ml_dirp[RPCMNT_PATHLEN+1];
     78  1.1  cgd };
     79  1.1  cgd 
     80  1.1  cgd struct exportlist {
     81  1.1  cgd 	struct exportlist *ex_next;
     82  1.1  cgd 	struct exportlist *ex_prev;
     83  1.1  cgd 	struct grouplist *ex_groups;
     84  1.1  cgd 	int	ex_rootuid;
     85  1.1  cgd 	int	ex_exflags;
     86  1.1  cgd 	dev_t	ex_dev;
     87  1.1  cgd 	char	ex_dirp[RPCMNT_PATHLEN+1];
     88  1.1  cgd };
     89  1.1  cgd 
     90  1.1  cgd struct grouplist {
     91  1.1  cgd 	struct grouplist *gr_next;
     92  1.1  cgd 	struct hostent *gr_hp;
     93  1.1  cgd };
     94  1.1  cgd 
     95  1.1  cgd /* Global defs */
     96  1.1  cgd int mntsrv(), umntall_each(), xdr_fhs(), xdr_mlist(), xdr_dir(), xdr_explist();
     97  1.1  cgd void add_mlist(), del_mlist(), get_exportlist(), get_mountlist();
     98  1.1  cgd void send_umntall();
     99  1.1  cgd struct exportlist exphead;
    100  1.1  cgd struct mountlist *mlhead;
    101  1.1  cgd char exname[MAXPATHLEN];
    102  1.1  cgd int def_rootuid = -2;
    103  1.1  cgd int root_only = 1;
    104  1.1  cgd extern int errno;
    105  1.1  cgd #ifdef DEBUG
    106  1.1  cgd int debug = 1;
    107  1.1  cgd #else
    108  1.1  cgd int debug = 0;
    109  1.1  cgd #endif
    110  1.1  cgd 
    111  1.1  cgd /*
    112  1.1  cgd  * Mountd server for NFS mount protocol as described in:
    113  1.1  cgd  * NFS: Network File System Protocol Specification, RFC1094, Appendix A
    114  1.1  cgd  * The optional arguments are the exports file name
    115  1.1  cgd  * default: _PATH_EXPORTS
    116  1.1  cgd  * and "-n" to allow nonroot mount.
    117  1.1  cgd  */
    118  1.1  cgd main(argc, argv)
    119  1.1  cgd 	int argc;
    120  1.1  cgd 	char **argv;
    121  1.1  cgd {
    122  1.1  cgd 	SVCXPRT *transp;
    123  1.1  cgd 	int c;
    124  1.1  cgd 	extern int optind;
    125  1.1  cgd 	extern char *optarg;
    126  1.1  cgd 
    127  1.1  cgd 	while ((c = getopt(argc, argv, "n")) != EOF)
    128  1.1  cgd 		switch (c) {
    129  1.1  cgd 		case 'n':
    130  1.1  cgd 			root_only = 0;
    131  1.1  cgd 			break;
    132  1.1  cgd 		default:
    133  1.1  cgd 			fprintf(stderr, "Usage: mountd [-n] [export_file]\n");
    134  1.1  cgd 			exit(1);
    135  1.1  cgd 		};
    136  1.1  cgd 	argc -= optind;
    137  1.1  cgd 	argv += optind;
    138  1.1  cgd 	exphead.ex_next = exphead.ex_prev = (struct exportlist *)0;
    139  1.1  cgd 	mlhead = (struct mountlist *)0;
    140  1.1  cgd 	if (argc == 1) {
    141  1.1  cgd 		strncpy(exname, *argv, MAXPATHLEN-1);
    142  1.1  cgd 		exname[MAXPATHLEN-1] = '\0';
    143  1.1  cgd 	} else
    144  1.1  cgd 		strcpy(exname, _PATH_EXPORTS);
    145  1.1  cgd 	openlog("mountd:", LOG_PID, LOG_DAEMON);
    146  1.1  cgd 	get_exportlist();
    147  1.1  cgd 	get_mountlist();
    148  1.1  cgd 	if (debug == 0) {
    149  1.1  cgd 		daemon(0, 0);
    150  1.1  cgd 		signal(SIGINT, SIG_IGN);
    151  1.1  cgd 		signal(SIGQUIT, SIG_IGN);
    152  1.1  cgd 	}
    153  1.1  cgd 	signal(SIGHUP, get_exportlist);
    154  1.1  cgd 	signal(SIGTERM, send_umntall);
    155  1.1  cgd 	{ FILE *pidfile = fopen(_PATH_MOUNTDPID, "w");
    156  1.1  cgd 	  if (pidfile != NULL) {
    157  1.1  cgd 		fprintf(pidfile, "%d\n", getpid());
    158  1.1  cgd 		fclose(pidfile);
    159  1.1  cgd 	  }
    160  1.1  cgd 	}
    161  1.1  cgd 	if ((transp = svcudp_create(RPC_ANYSOCK)) == NULL) {
    162  1.1  cgd 		syslog(LOG_ERR, "Can't create socket");
    163  1.1  cgd 		exit(1);
    164  1.1  cgd 	}
    165  1.1  cgd 	pmap_unset(RPCPROG_MNT, RPCMNT_VER1);
    166  1.1  cgd 	if (!svc_register(transp, RPCPROG_MNT, RPCMNT_VER1, mntsrv, IPPROTO_UDP)) {
    167  1.1  cgd 		syslog(LOG_ERR, "Can't register mount");
    168  1.1  cgd 		exit(1);
    169  1.1  cgd 	}
    170  1.1  cgd 	svc_run();
    171  1.1  cgd 	syslog(LOG_ERR, "Mountd died");
    172  1.1  cgd 	exit(1);
    173  1.1  cgd }
    174  1.1  cgd 
    175  1.1  cgd /*
    176  1.1  cgd  * The mount rpc service
    177  1.1  cgd  */
    178  1.1  cgd mntsrv(rqstp, transp)
    179  1.1  cgd 	register struct svc_req *rqstp;
    180  1.1  cgd 	register SVCXPRT *transp;
    181  1.1  cgd {
    182  1.1  cgd 	register struct grouplist *grp;
    183  1.1  cgd 	register u_long **addrp;
    184  1.1  cgd 	register struct exportlist *ep;
    185  1.1  cgd 	nfsv2fh_t nfh;
    186  1.1  cgd 	struct authunix_parms *ucr;
    187  1.1  cgd 	struct stat stb;
    188  1.1  cgd 	struct hostent *hp;
    189  1.1  cgd 	u_long saddr;
    190  1.1  cgd 	char dirpath[RPCMNT_PATHLEN+1];
    191  1.1  cgd 	int bad = ENOENT;
    192  1.1  cgd 	int omask;
    193  1.1  cgd 	uid_t uid = -2;
    194  1.1  cgd 
    195  1.1  cgd 	/* Get authorization */
    196  1.1  cgd 	switch (rqstp->rq_cred.oa_flavor) {
    197  1.1  cgd 	case AUTH_UNIX:
    198  1.1  cgd 		ucr = (struct authunix_parms *)rqstp->rq_clntcred;
    199  1.1  cgd 		uid = ucr->aup_uid;
    200  1.1  cgd 		break;
    201  1.1  cgd 	case AUTH_NULL:
    202  1.1  cgd 	default:
    203  1.1  cgd 		break;
    204  1.1  cgd 	}
    205  1.1  cgd 
    206  1.1  cgd 	saddr = transp->xp_raddr.sin_addr.s_addr;
    207  1.1  cgd 	hp = (struct hostent *)0;
    208  1.1  cgd 	switch (rqstp->rq_proc) {
    209  1.1  cgd 	case NULLPROC:
    210  1.1  cgd 		if (!svc_sendreply(transp, xdr_void, (caddr_t)0))
    211  1.1  cgd 			syslog(LOG_ERR, "Can't send reply");
    212  1.1  cgd 		return;
    213  1.1  cgd 	case RPCMNT_MOUNT:
    214  1.1  cgd 		if (uid != 0 && root_only) {
    215  1.1  cgd 			svcerr_weakauth(transp);
    216  1.1  cgd 			return;
    217  1.1  cgd 		}
    218  1.1  cgd 		if (!svc_getargs(transp, xdr_dir, dirpath)) {
    219  1.1  cgd 			svcerr_decode(transp);
    220  1.1  cgd 			return;
    221  1.1  cgd 		}
    222  1.1  cgd 
    223  1.1  cgd 		/* Check to see if it's a valid dirpath */
    224  1.1  cgd 		if (stat(dirpath, &stb) < 0 || (stb.st_mode&S_IFMT) !=
    225  1.1  cgd 			S_IFDIR) {
    226  1.1  cgd 			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
    227  1.1  cgd 				syslog(LOG_ERR, "Can't send reply");
    228  1.1  cgd 			return;
    229  1.1  cgd 		}
    230  1.1  cgd 
    231  1.1  cgd 		/* Check in the exports list */
    232  1.1  cgd 		omask = sigblock(sigmask(SIGHUP));
    233  1.1  cgd 		ep = exphead.ex_next;
    234  1.1  cgd 		while (ep != NULL) {
    235  1.1  cgd 			if (!strcmp(ep->ex_dirp, dirpath)) {
    236  1.1  cgd 				grp = ep->ex_groups;
    237  1.1  cgd 				if (grp == NULL)
    238  1.1  cgd 					break;
    239  1.1  cgd 
    240  1.1  cgd 				/* Check for a host match */
    241  1.1  cgd 				addrp = (u_long **)grp->gr_hp->h_addr_list;
    242  1.1  cgd 				for (;;) {
    243  1.1  cgd 					if (**addrp == saddr)
    244  1.1  cgd 						break;
    245  1.1  cgd 					if (*++addrp == NULL)
    246  1.1  cgd 						if (grp = grp->gr_next) {
    247  1.1  cgd 							addrp = (u_long **)
    248  1.1  cgd 								grp->gr_hp->h_addr_list;
    249  1.1  cgd 						} else {
    250  1.1  cgd 							bad = EACCES;
    251  1.1  cgd 							if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
    252  1.1  cgd 								syslog(LOG_ERR, "Can't send reply");
    253  1.1  cgd 							sigsetmask(omask);
    254  1.1  cgd 							return;
    255  1.1  cgd 						}
    256  1.1  cgd 				}
    257  1.1  cgd 				hp = grp->gr_hp;
    258  1.1  cgd 				break;
    259  1.1  cgd 			}
    260  1.1  cgd 			ep = ep->ex_next;
    261  1.1  cgd 		}
    262  1.1  cgd 		sigsetmask(omask);
    263  1.1  cgd 		if (ep == NULL) {
    264  1.1  cgd 			bad = EACCES;
    265  1.1  cgd 			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
    266  1.1  cgd 				syslog(LOG_ERR, "Can't send reply");
    267  1.1  cgd 			return;
    268  1.1  cgd 		}
    269  1.1  cgd 
    270  1.1  cgd 		/* Get the file handle */
    271  1.1  cgd 		bzero((caddr_t)&nfh, sizeof(nfh));
    272  1.1  cgd 		if (getfh(dirpath, (fhandle_t *)&nfh) < 0) {
    273  1.1  cgd 			bad = errno;
    274  1.1  cgd 			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
    275  1.1  cgd 				syslog(LOG_ERR, "Can't send reply");
    276  1.1  cgd 			return;
    277  1.1  cgd 		}
    278  1.1  cgd 		if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&nfh))
    279  1.1  cgd 			syslog(LOG_ERR, "Can't send reply");
    280  1.1  cgd 		if (hp == NULL)
    281  1.1  cgd 			hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
    282  1.1  cgd 		if (hp)
    283  1.1  cgd 			add_mlist(hp->h_name, dirpath);
    284  1.1  cgd 		return;
    285  1.1  cgd 	case RPCMNT_DUMP:
    286  1.1  cgd 		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)0))
    287  1.1  cgd 			syslog(LOG_ERR, "Can't send reply");
    288  1.1  cgd 		return;
    289  1.1  cgd 	case RPCMNT_UMOUNT:
    290  1.1  cgd 		if (uid != 0 && root_only) {
    291  1.1  cgd 			svcerr_weakauth(transp);
    292  1.1  cgd 			return;
    293  1.1  cgd 		}
    294  1.1  cgd 		if (!svc_getargs(transp, xdr_dir, dirpath)) {
    295  1.1  cgd 			svcerr_decode(transp);
    296  1.1  cgd 			return;
    297  1.1  cgd 		}
    298  1.1  cgd 		if (!svc_sendreply(transp, xdr_void, (caddr_t)0))
    299  1.1  cgd 			syslog(LOG_ERR, "Can't send reply");
    300  1.1  cgd 		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
    301  1.1  cgd 		if (hp)
    302  1.1  cgd 			del_mlist(hp->h_name, dirpath);
    303  1.1  cgd 		return;
    304  1.1  cgd 	case RPCMNT_UMNTALL:
    305  1.1  cgd 		if (uid != 0 && root_only) {
    306  1.1  cgd 			svcerr_weakauth(transp);
    307  1.1  cgd 			return;
    308  1.1  cgd 		}
    309  1.1  cgd 		if (!svc_sendreply(transp, xdr_void, (caddr_t)0))
    310  1.1  cgd 			syslog(LOG_ERR, "Can't send reply");
    311  1.1  cgd 		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
    312  1.1  cgd 		if (hp)
    313  1.1  cgd 			del_mlist(hp->h_name, (char *)0);
    314  1.1  cgd 		return;
    315  1.1  cgd 	case RPCMNT_EXPORT:
    316  1.1  cgd 		if (!svc_sendreply(transp, xdr_explist, (caddr_t)0))
    317  1.1  cgd 			syslog(LOG_ERR, "Can't send reply");
    318  1.1  cgd 		return;
    319  1.1  cgd 	default:
    320  1.1  cgd 		svcerr_noproc(transp);
    321  1.1  cgd 		return;
    322  1.1  cgd 	}
    323  1.1  cgd }
    324  1.1  cgd 
    325  1.1  cgd /*
    326  1.1  cgd  * Xdr conversion for a dirpath string
    327  1.1  cgd  */
    328  1.1  cgd xdr_dir(xdrsp, dirp)
    329  1.1  cgd 	XDR *xdrsp;
    330  1.1  cgd 	char *dirp;
    331  1.1  cgd {
    332  1.1  cgd 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    333  1.1  cgd }
    334  1.1  cgd 
    335  1.1  cgd /*
    336  1.1  cgd  * Xdr routine to generate fhstatus
    337  1.1  cgd  */
    338  1.1  cgd xdr_fhs(xdrsp, nfh)
    339  1.1  cgd 	XDR *xdrsp;
    340  1.1  cgd 	nfsv2fh_t *nfh;
    341  1.1  cgd {
    342  1.1  cgd 	int ok = 0;
    343  1.1  cgd 
    344  1.1  cgd 	if (!xdr_long(xdrsp, &ok))
    345  1.1  cgd 		return (0);
    346  1.1  cgd 	return (xdr_opaque(xdrsp, (caddr_t)nfh, NFSX_FH));
    347  1.1  cgd }
    348  1.1  cgd 
    349  1.1  cgd xdr_mlist(xdrsp, cp)
    350  1.1  cgd 	XDR *xdrsp;
    351  1.1  cgd 	caddr_t cp;
    352  1.1  cgd {
    353  1.1  cgd 	register struct mountlist *mlp;
    354  1.1  cgd 	int true = 1;
    355  1.1  cgd 	int false = 0;
    356  1.1  cgd 	char *strp;
    357  1.1  cgd 
    358  1.1  cgd 	mlp = mlhead;
    359  1.1  cgd 	while (mlp) {
    360  1.1  cgd 		if (!xdr_bool(xdrsp, &true))
    361  1.1  cgd 			return (0);
    362  1.1  cgd 		strp = &mlp->ml_host[0];
    363  1.1  cgd 		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
    364  1.1  cgd 			return (0);
    365  1.1  cgd 		strp = &mlp->ml_dirp[0];
    366  1.1  cgd 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
    367  1.1  cgd 			return (0);
    368  1.1  cgd 		mlp = mlp->ml_next;
    369  1.1  cgd 	}
    370  1.1  cgd 	if (!xdr_bool(xdrsp, &false))
    371  1.1  cgd 		return (0);
    372  1.1  cgd 	return (1);
    373  1.1  cgd }
    374  1.1  cgd 
    375  1.1  cgd /*
    376  1.1  cgd  * Xdr conversion for export list
    377  1.1  cgd  */
    378  1.1  cgd xdr_explist(xdrsp, cp)
    379  1.1  cgd 	XDR *xdrsp;
    380  1.1  cgd 	caddr_t cp;
    381  1.1  cgd {
    382  1.1  cgd 	register struct exportlist *ep;
    383  1.1  cgd 	register struct grouplist *grp;
    384  1.1  cgd 	int true = 1;
    385  1.1  cgd 	int false = 0;
    386  1.1  cgd 	char *strp;
    387  1.1  cgd 	int omask;
    388  1.1  cgd 
    389  1.1  cgd 	omask = sigblock(sigmask(SIGHUP));
    390  1.1  cgd 	ep = exphead.ex_next;
    391  1.1  cgd 	while (ep != NULL) {
    392  1.1  cgd 		if (!xdr_bool(xdrsp, &true))
    393  1.1  cgd 			goto errout;
    394  1.1  cgd 		strp = &ep->ex_dirp[0];
    395  1.1  cgd 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
    396  1.1  cgd 			goto errout;
    397  1.1  cgd 		grp = ep->ex_groups;
    398  1.1  cgd 		while (grp != NULL) {
    399  1.1  cgd 			if (!xdr_bool(xdrsp, &true))
    400  1.1  cgd 				goto errout;
    401  1.1  cgd 			strp = grp->gr_hp->h_name;
    402  1.1  cgd 			if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
    403  1.1  cgd 				goto errout;
    404  1.1  cgd 			grp = grp->gr_next;
    405  1.1  cgd 		}
    406  1.1  cgd 		if (!xdr_bool(xdrsp, &false))
    407  1.1  cgd 			goto errout;
    408  1.1  cgd 		ep = ep->ex_next;
    409  1.1  cgd 	}
    410  1.1  cgd 	sigsetmask(omask);
    411  1.1  cgd 	if (!xdr_bool(xdrsp, &false))
    412  1.1  cgd 		return (0);
    413  1.1  cgd 	return (1);
    414  1.1  cgd errout:
    415  1.1  cgd 	sigsetmask(omask);
    416  1.1  cgd 	return (0);
    417  1.1  cgd }
    418  1.1  cgd 
    419  1.1  cgd #define LINESIZ	10240
    420  1.1  cgd char line[LINESIZ];
    421  1.1  cgd 
    422  1.1  cgd /*
    423  1.1  cgd  * Get the export list
    424  1.1  cgd  */
    425  1.1  cgd void
    426  1.1  cgd get_exportlist()
    427  1.1  cgd {
    428  1.1  cgd 	register struct hostent *hp, *nhp;
    429  1.1  cgd 	register char **addrp, **naddrp;
    430  1.1  cgd 	register int i;
    431  1.1  cgd 	register struct grouplist *grp;
    432  1.1  cgd 	register struct exportlist *ep, *ep2;
    433  1.1  cgd 	struct statfs stfsbuf;
    434  1.1  cgd 	struct ufs_args args;
    435  1.1  cgd 	struct stat sb;
    436  1.1  cgd 	FILE *inf;
    437  1.1  cgd 	char *cp, *endcp;
    438  1.1  cgd 	char savedc;
    439  1.1  cgd 	int len, dirplen;
    440  1.1  cgd 	int rootuid, exflags;
    441  1.1  cgd 	u_long saddr;
    442  1.1  cgd 	struct exportlist *fep;
    443  1.1  cgd 
    444  1.1  cgd 	/*
    445  1.1  cgd 	 * First, get rid of the old list
    446  1.1  cgd 	 */
    447  1.1  cgd 	ep = exphead.ex_next;
    448  1.1  cgd 	while (ep != NULL) {
    449  1.1  cgd 		ep2 = ep;
    450  1.1  cgd 		ep = ep->ex_next;
    451  1.1  cgd 		free_exp(ep2);
    452  1.1  cgd 	}
    453  1.1  cgd 
    454  1.1  cgd 	/*
    455  1.1  cgd 	 * Read in the exports file and build the list, calling
    456  1.1  cgd 	 * exportfs() as we go along
    457  1.1  cgd 	 */
    458  1.1  cgd 	exphead.ex_next = exphead.ex_prev = (struct exportlist *)0;
    459  1.1  cgd 	if ((inf = fopen(exname, "r")) == NULL) {
    460  1.1  cgd 		syslog(LOG_ERR, "Can't open %s", exname);
    461  1.1  cgd 		exit(2);
    462  1.1  cgd 	}
    463  1.1  cgd 	while (fgets(line, LINESIZ, inf)) {
    464  1.1  cgd 		exflags = MNT_EXPORTED;
    465  1.1  cgd 		rootuid = def_rootuid;
    466  1.1  cgd 		cp = line;
    467  1.1  cgd 		nextfield(&cp, &endcp);
    468  1.1  cgd 
    469  1.1  cgd 		/*
    470  1.1  cgd 		 * Get file system devno and see if an entry for this
    471  1.1  cgd 		 * file system already exists.
    472  1.1  cgd 		 */
    473  1.1  cgd 		savedc = *endcp;
    474  1.1  cgd 		*endcp = '\0';
    475  1.1  cgd 		if (stat(cp, &sb) < 0 || (sb.st_mode & S_IFMT) != S_IFDIR) {
    476  1.1  cgd 			syslog(LOG_ERR,
    477  1.1  cgd 			    "Bad Exports File, %s: %s, mountd Failed",
    478  1.1  cgd 			    cp, "Not a directory");
    479  1.1  cgd 			exit(2);
    480  1.1  cgd 		}
    481  1.1  cgd 		fep = (struct exportlist *)0;
    482  1.1  cgd 		ep = exphead.ex_next;
    483  1.1  cgd 		while (ep) {
    484  1.1  cgd 			if (ep->ex_dev == sb.st_dev) {
    485  1.1  cgd 				fep = ep;
    486  1.1  cgd 				break;
    487  1.1  cgd 			}
    488  1.1  cgd 			ep = ep->ex_next;
    489  1.1  cgd 		}
    490  1.1  cgd 		*endcp = savedc;
    491  1.1  cgd 
    492  1.1  cgd 		/*
    493  1.1  cgd 		 * Create new exports list entry
    494  1.1  cgd 		 */
    495  1.1  cgd 		len = endcp-cp;
    496  1.1  cgd 		if (len <= RPCMNT_PATHLEN && len > 0) {
    497  1.1  cgd 			ep = (struct exportlist *)malloc(sizeof(*ep));
    498  1.1  cgd 			if (ep == NULL)
    499  1.1  cgd 				goto err;
    500  1.1  cgd 			ep->ex_next = ep->ex_prev = (struct exportlist *)0;
    501  1.1  cgd 			ep->ex_groups = (struct grouplist *)0;
    502  1.1  cgd 			bcopy(cp, ep->ex_dirp, len);
    503  1.1  cgd 			ep->ex_dirp[len] = '\0';
    504  1.1  cgd 			dirplen = len;
    505  1.1  cgd 		} else {
    506  1.1  cgd 			syslog(LOG_ERR, "Bad Exports File, mountd Failed");
    507  1.1  cgd 			exit(2);
    508  1.1  cgd 		}
    509  1.1  cgd 		cp = endcp;
    510  1.1  cgd 		nextfield(&cp, &endcp);
    511  1.1  cgd 		len = endcp-cp;
    512  1.1  cgd 		while (len > 0) {
    513  1.1  cgd 			savedc = *endcp;
    514  1.1  cgd 			*endcp = '\0';
    515  1.1  cgd 			if (len > RPCMNT_NAMELEN)
    516  1.1  cgd 				goto more;
    517  1.1  cgd 			if (*cp == '-') {
    518  1.1  cgd 				do_opt(cp + 1, fep, ep, &exflags, &rootuid);
    519  1.1  cgd 				goto more;
    520  1.1  cgd 			}
    521  1.1  cgd 			if (isdigit(*cp)) {
    522  1.1  cgd 				saddr = inet_addr(cp);
    523  1.1  cgd 				if (saddr == -1 ||
    524  1.1  cgd 				    (hp = gethostbyaddr((caddr_t)&saddr,
    525  1.1  cgd 				     sizeof(saddr), AF_INET)) == NULL) {
    526  1.1  cgd 					syslog(LOG_ERR,
    527  1.1  cgd 					    "Bad Exports File, %s: %s", cp,
    528  1.1  cgd 					    "Gethostbyaddr failed, ignored");
    529  1.1  cgd 					goto more;
    530  1.1  cgd 				}
    531  1.1  cgd 			} else if ((hp = gethostbyname(cp)) == NULL) {
    532  1.1  cgd 				syslog(LOG_ERR, "Bad Exports File, %s: %s",
    533  1.1  cgd 				    cp, "Gethostbyname failed, ignored");
    534  1.1  cgd 				goto more;
    535  1.1  cgd 			}
    536  1.1  cgd 			grp = (struct grouplist *)
    537  1.1  cgd 				malloc(sizeof(struct grouplist));
    538  1.1  cgd 			if (grp == NULL)
    539  1.1  cgd 				goto err;
    540  1.1  cgd 			nhp = grp->gr_hp = (struct hostent *)
    541  1.1  cgd 				malloc(sizeof(struct hostent));
    542  1.1  cgd 			if (nhp == NULL)
    543  1.1  cgd 				goto err;
    544  1.1  cgd 			bcopy((caddr_t)hp, (caddr_t)nhp,
    545  1.1  cgd 				sizeof(struct hostent));
    546  1.1  cgd 			i = strlen(hp->h_name)+1;
    547  1.1  cgd 			nhp->h_name = (char *)malloc(i);
    548  1.1  cgd 			if (nhp->h_name == NULL)
    549  1.1  cgd 				goto err;
    550  1.1  cgd 			bcopy(hp->h_name, nhp->h_name, i);
    551  1.1  cgd 			addrp = hp->h_addr_list;
    552  1.1  cgd 			i = 1;
    553  1.1  cgd 			while (*addrp++)
    554  1.1  cgd 				i++;
    555  1.1  cgd 			naddrp = nhp->h_addr_list = (char **)
    556  1.1  cgd 				malloc(i*sizeof(char *));
    557  1.1  cgd 			if (naddrp == NULL)
    558  1.1  cgd 				goto err;
    559  1.1  cgd 			addrp = hp->h_addr_list;
    560  1.1  cgd 			while (*addrp) {
    561  1.1  cgd 				*naddrp = (char *)
    562  1.1  cgd 				    malloc(hp->h_length);
    563  1.1  cgd 				if (*naddrp == NULL)
    564  1.1  cgd 				    goto err;
    565  1.1  cgd 				bcopy(*addrp, *naddrp,
    566  1.1  cgd 					hp->h_length);
    567  1.1  cgd 				addrp++;
    568  1.1  cgd 				naddrp++;
    569  1.1  cgd 			}
    570  1.1  cgd 			*naddrp = (char *)0;
    571  1.1  cgd 			grp->gr_next = ep->ex_groups;
    572  1.1  cgd 			ep->ex_groups = grp;
    573  1.1  cgd 		more:
    574  1.1  cgd 			cp = endcp;
    575  1.1  cgd 			*cp = savedc;
    576  1.1  cgd 			nextfield(&cp, &endcp);
    577  1.1  cgd 			len = endcp - cp;
    578  1.1  cgd 		}
    579  1.1  cgd 		if (fep == NULL) {
    580  1.1  cgd 			args.fspec = 0;
    581  1.1  cgd 			args.exflags = exflags;
    582  1.1  cgd 			args.exroot = rootuid;
    583  1.1  cgd 			cp = (char *)0;
    584  1.1  cgd 			while (statfs(ep->ex_dirp, &stfsbuf) < 0 ||
    585  1.1  cgd 			       mount(MOUNT_UFS, ep->ex_dirp,
    586  1.1  cgd 				     stfsbuf.f_flags|MNT_UPDATE, &args) < 0) {
    587  1.1  cgd 				if (cp == NULL)
    588  1.1  cgd 					cp = ep->ex_dirp + dirplen - 1;
    589  1.1  cgd 				else
    590  1.1  cgd 					*cp = savedc;
    591  1.1  cgd 				/* back up over the last component */
    592  1.1  cgd 				while (*cp == '/' && cp > ep->ex_dirp)
    593  1.1  cgd 					cp--;
    594  1.1  cgd 				while (*(cp - 1) != '/' && cp > ep->ex_dirp)
    595  1.1  cgd 					cp--;
    596  1.1  cgd 				if (cp == ep->ex_dirp) {
    597  1.1  cgd 					syslog(LOG_WARNING,
    598  1.1  cgd 					      "Can't export %s", ep->ex_dirp);
    599  1.1  cgd 					free_exp(ep);
    600  1.1  cgd 					goto nextline;
    601  1.1  cgd 				}
    602  1.1  cgd 				savedc = *cp;
    603  1.1  cgd 				*cp = '\0';
    604  1.1  cgd 			}
    605  1.1  cgd 			if (cp)
    606  1.1  cgd 				*cp = savedc;
    607  1.1  cgd 			ep->ex_rootuid = rootuid;
    608  1.1  cgd 			ep->ex_exflags = exflags;
    609  1.1  cgd 		} else {
    610  1.1  cgd 			ep->ex_rootuid = fep->ex_rootuid;
    611  1.1  cgd 			ep->ex_exflags = fep->ex_exflags;
    612  1.1  cgd 		}
    613  1.1  cgd 		ep->ex_dev = sb.st_dev;
    614  1.1  cgd 		ep->ex_next = exphead.ex_next;
    615  1.1  cgd 		ep->ex_prev = &exphead;
    616  1.1  cgd 		if (ep->ex_next != NULL)
    617  1.1  cgd 			ep->ex_next->ex_prev = ep;
    618  1.1  cgd 		exphead.ex_next = ep;
    619  1.1  cgd nextline:
    620  1.1  cgd 		;
    621  1.1  cgd 	}
    622  1.1  cgd 	fclose(inf);
    623  1.1  cgd 	return;
    624  1.1  cgd err:
    625  1.1  cgd 	syslog(LOG_ERR, "No more memory: mountd Failed");
    626  1.1  cgd 	exit(2);
    627  1.1  cgd }
    628  1.1  cgd 
    629  1.1  cgd /*
    630  1.1  cgd  * Parse out the next white space separated field
    631  1.1  cgd  */
    632  1.1  cgd nextfield(cp, endcp)
    633  1.1  cgd 	char **cp;
    634  1.1  cgd 	char **endcp;
    635  1.1  cgd {
    636  1.1  cgd 	register char *p;
    637  1.1  cgd 
    638  1.1  cgd 	p = *cp;
    639  1.1  cgd 	while (*p == ' ' || *p == '\t')
    640  1.1  cgd 		p++;
    641  1.1  cgd 	if (*p == '\n' || *p == '\0') {
    642  1.1  cgd 		*cp = *endcp = p;
    643  1.1  cgd 		return;
    644  1.1  cgd 	}
    645  1.1  cgd 	*cp = p++;
    646  1.1  cgd 	while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
    647  1.1  cgd 		p++;
    648  1.1  cgd 	*endcp = p;
    649  1.1  cgd }
    650  1.1  cgd 
    651  1.1  cgd /*
    652  1.1  cgd  * Parse the option string
    653  1.1  cgd  */
    654  1.1  cgd do_opt(cpopt, fep, ep, exflagsp, rootuidp)
    655  1.1  cgd 	register char *cpopt;
    656  1.1  cgd 	struct exportlist *fep, *ep;
    657  1.1  cgd 	int *exflagsp, *rootuidp;
    658  1.1  cgd {
    659  1.1  cgd 	register char *cpoptarg, *cpoptend;
    660  1.1  cgd 
    661  1.1  cgd 	while (cpopt && *cpopt) {
    662  1.1  cgd 		if (cpoptend = index(cpopt, ','))
    663  1.1  cgd 			*cpoptend++ = '\0';
    664  1.1  cgd 		if (cpoptarg = index(cpopt, '='))
    665  1.1  cgd 			*cpoptarg++ = '\0';
    666  1.1  cgd 		if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
    667  1.1  cgd 			if (fep && (fep->ex_exflags & MNT_EXRDONLY) == 0)
    668  1.1  cgd 				syslog(LOG_WARNING, "ro failed for %s",
    669  1.1  cgd 				       ep->ex_dirp);
    670  1.1  cgd 			else
    671  1.1  cgd 				*exflagsp |= MNT_EXRDONLY;
    672  1.1  cgd 		} else if (!strcmp(cpopt, "root") || !strcmp(cpopt, "r")) {
    673  1.1  cgd 			if (cpoptarg && isdigit(*cpoptarg)) {
    674  1.1  cgd 				*rootuidp = atoi(cpoptarg);
    675  1.1  cgd 				if (fep && fep->ex_rootuid != *rootuidp)
    676  1.1  cgd 					syslog(LOG_WARNING,
    677  1.1  cgd 					       "uid failed for %s",
    678  1.1  cgd 					       ep->ex_dirp);
    679  1.1  cgd 			} else
    680  1.1  cgd 				syslog(LOG_WARNING,
    681  1.1  cgd 				       "uid failed for %s",
    682  1.1  cgd 				       ep->ex_dirp);
    683  1.1  cgd 		} else
    684  1.1  cgd 			syslog(LOG_WARNING, "opt %s ignored for %s", cpopt,
    685  1.1  cgd 			       ep->ex_dirp);
    686  1.1  cgd 		cpopt = cpoptend;
    687  1.1  cgd 	}
    688  1.1  cgd }
    689  1.1  cgd 
    690  1.1  cgd #define	STRSIZ	(RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
    691  1.1  cgd /*
    692  1.1  cgd  * Routines that maintain the remote mounttab
    693  1.1  cgd  */
    694  1.1  cgd void get_mountlist()
    695  1.1  cgd {
    696  1.1  cgd 	register struct mountlist *mlp, **mlpp;
    697  1.1  cgd 	register char *eos, *dirp;
    698  1.1  cgd 	int len;
    699  1.1  cgd 	char str[STRSIZ];
    700  1.1  cgd 	FILE *mlfile;
    701  1.1  cgd 
    702  1.1  cgd 	if (((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) &&
    703  1.1  cgd 	    ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL)) {
    704  1.1  cgd 		syslog(LOG_WARNING, "Can't open %s", _PATH_RMOUNTLIST);
    705  1.1  cgd 		return;
    706  1.1  cgd 	}
    707  1.1  cgd 	mlpp = &mlhead;
    708  1.1  cgd 	while (fgets(str, STRSIZ, mlfile) != NULL) {
    709  1.1  cgd 		if ((dirp = index(str, '\t')) == NULL &&
    710  1.1  cgd 		    (dirp = index(str, ' ')) == NULL)
    711  1.1  cgd 			continue;
    712  1.1  cgd 		mlp = (struct mountlist *)malloc(sizeof (*mlp));
    713  1.1  cgd 		len = dirp-str;
    714  1.1  cgd 		if (len > RPCMNT_NAMELEN)
    715  1.1  cgd 			len = RPCMNT_NAMELEN;
    716  1.1  cgd 		bcopy(str, mlp->ml_host, len);
    717  1.1  cgd 		mlp->ml_host[len] = '\0';
    718  1.1  cgd 		while (*dirp == '\t' || *dirp == ' ')
    719  1.1  cgd 			dirp++;
    720  1.1  cgd 		if ((eos = index(dirp, '\t')) == NULL &&
    721  1.1  cgd 		    (eos = index(dirp, ' ')) == NULL &&
    722  1.1  cgd 		    (eos = index(dirp, '\n')) == NULL)
    723  1.1  cgd 			len = strlen(dirp);
    724  1.1  cgd 		else
    725  1.1  cgd 			len = eos-dirp;
    726  1.1  cgd 		if (len > RPCMNT_PATHLEN)
    727  1.1  cgd 			len = RPCMNT_PATHLEN;
    728  1.1  cgd 		bcopy(dirp, mlp->ml_dirp, len);
    729  1.1  cgd 		mlp->ml_dirp[len] = '\0';
    730  1.1  cgd 		mlp->ml_next = (struct mountlist *)0;
    731  1.1  cgd 		*mlpp = mlp;
    732  1.1  cgd 		mlpp = &mlp->ml_next;
    733  1.1  cgd 	}
    734  1.1  cgd 	fclose(mlfile);
    735  1.1  cgd }
    736  1.1  cgd 
    737  1.1  cgd void del_mlist(hostp, dirp)
    738  1.1  cgd 	register char *hostp, *dirp;
    739  1.1  cgd {
    740  1.1  cgd 	register struct mountlist *mlp, **mlpp;
    741  1.1  cgd 	FILE *mlfile;
    742  1.1  cgd 	int fnd = 0;
    743  1.1  cgd 
    744  1.1  cgd 	mlpp = &mlhead;
    745  1.1  cgd 	mlp = mlhead;
    746  1.1  cgd 	while (mlp) {
    747  1.1  cgd 		if (!strcmp(mlp->ml_host, hostp) &&
    748  1.1  cgd 		    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
    749  1.1  cgd 			fnd = 1;
    750  1.1  cgd 			*mlpp = mlp->ml_next;
    751  1.1  cgd 			free((caddr_t)mlp);
    752  1.1  cgd 		}
    753  1.1  cgd 		mlpp = &mlp->ml_next;
    754  1.1  cgd 		mlp = mlp->ml_next;
    755  1.1  cgd 	}
    756  1.1  cgd 	if (fnd) {
    757  1.1  cgd 		if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
    758  1.1  cgd 			syslog(LOG_WARNING, "Can't update %s", _PATH_RMOUNTLIST);
    759  1.1  cgd 			return;
    760  1.1  cgd 		}
    761  1.1  cgd 		mlp = mlhead;
    762  1.1  cgd 		while (mlp) {
    763  1.1  cgd 			fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
    764  1.1  cgd 			mlp = mlp->ml_next;
    765  1.1  cgd 		}
    766  1.1  cgd 		fclose(mlfile);
    767  1.1  cgd 	}
    768  1.1  cgd }
    769  1.1  cgd 
    770  1.1  cgd void add_mlist(hostp, dirp)
    771  1.1  cgd 	register char *hostp, *dirp;
    772  1.1  cgd {
    773  1.1  cgd 	register struct mountlist *mlp, **mlpp;
    774  1.1  cgd 	FILE *mlfile;
    775  1.1  cgd 
    776  1.1  cgd 	mlpp = &mlhead;
    777  1.1  cgd 	mlp = mlhead;
    778  1.1  cgd 	while (mlp) {
    779  1.1  cgd 		if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
    780  1.1  cgd 			return;
    781  1.1  cgd 		mlpp = &mlp->ml_next;
    782  1.1  cgd 		mlp = mlp->ml_next;
    783  1.1  cgd 	}
    784  1.1  cgd 	mlp = (struct mountlist *)malloc(sizeof (*mlp));
    785  1.1  cgd 	strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
    786  1.1  cgd 	mlp->ml_host[RPCMNT_NAMELEN] = '\0';
    787  1.1  cgd 	strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
    788  1.1  cgd 	mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
    789  1.1  cgd 	mlp->ml_next = (struct mountlist *)0;
    790  1.1  cgd 	*mlpp = mlp;
    791  1.1  cgd 	if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
    792  1.1  cgd 		syslog(LOG_WARNING, "Can't update %s", _PATH_RMOUNTLIST);
    793  1.1  cgd 		return;
    794  1.1  cgd 	}
    795  1.1  cgd 	fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
    796  1.1  cgd 	fclose(mlfile);
    797  1.1  cgd }
    798  1.1  cgd 
    799  1.1  cgd /*
    800  1.1  cgd  * This function is called via. SIGTERM when the system is going down.
    801  1.1  cgd  * It sends a broadcast RPCMNT_UMNTALL.
    802  1.1  cgd  */
    803  1.1  cgd void
    804  1.1  cgd send_umntall()
    805  1.1  cgd {
    806  1.1  cgd 	(void) clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
    807  1.1  cgd 		xdr_void, (caddr_t)0, xdr_void, (caddr_t)0, umntall_each);
    808  1.1  cgd 	exit();
    809  1.1  cgd }
    810  1.1  cgd 
    811  1.1  cgd umntall_each(resultsp, raddr)
    812  1.1  cgd 	caddr_t resultsp;
    813  1.1  cgd 	struct sockaddr_in *raddr;
    814  1.1  cgd {
    815  1.1  cgd 	return (1);
    816  1.1  cgd }
    817  1.1  cgd 
    818  1.1  cgd /*
    819  1.1  cgd  * Free up an exports list component
    820  1.1  cgd  */
    821  1.1  cgd free_exp(ep)
    822  1.1  cgd 	register struct exportlist *ep;
    823  1.1  cgd {
    824  1.1  cgd 	register struct grouplist *grp;
    825  1.1  cgd 	register char **addrp;
    826  1.1  cgd 	struct grouplist *grp2;
    827  1.1  cgd 
    828  1.1  cgd 	grp = ep->ex_groups;
    829  1.1  cgd 	while (grp != NULL) {
    830  1.1  cgd 		addrp = grp->gr_hp->h_addr_list;
    831  1.1  cgd 		while (*addrp)
    832  1.1  cgd 			free(*addrp++);
    833  1.1  cgd 		free((caddr_t)grp->gr_hp->h_addr_list);
    834  1.1  cgd 		free(grp->gr_hp->h_name);
    835  1.1  cgd 		free((caddr_t)grp->gr_hp);
    836  1.1  cgd 		grp2 = grp;
    837  1.1  cgd 		grp = grp->gr_next;
    838  1.1  cgd 		free((caddr_t)grp2);
    839  1.1  cgd 	}
    840  1.1  cgd 	free((caddr_t)ep);
    841  1.1  cgd }
    842