xref: /csrg-svn/sbin/mountd/mountd.c (revision 39681)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  */
20 
21 #ifndef lint
22 char copyright[] =
23 "@(#) Copyright (c) 1989 Regents of the University of California.\n\
24  All rights reserved.\n";
25 #endif not lint
26 
27 #ifndef lint
28 static char sccsid[] = "@(#)mountd.c	5.2 (Berkeley) 11/30/89";
29 #endif not lint
30 
31 #include <stdio.h>
32 #include <strings.h>
33 #include <syslog.h>
34 #include <signal.h>
35 #include <fcntl.h>
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
40 #include <sys/file.h>
41 #include <sys/dir.h>
42 #include <sys/uio.h>
43 #include <sys/namei.h>
44 #include <sys/mount.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/errno.h>
48 #include <netdb.h>
49 #include <rpc/rpc.h>
50 #include <rpc/pmap_clnt.h>
51 #include <rpc/pmap_prot.h>
52 #include <nfs/rpcv2.h>
53 #include <nfs/nfsv2.h>
54 #include "pathnames.h"
55 
56 struct ufid {
57 	u_short	ufid_len;
58 	ino_t	ufid_ino;
59 	long	ufid_gen;
60 };
61 /*
62  * Structures for keeping the mount list and export list
63  */
64 struct mountlist {
65 	char	ml_host[RPCMNT_NAMELEN+1];
66 	char	ml_dirp[RPCMNT_PATHLEN+1];
67 };
68 
69 struct exportlist {
70 	struct exportlist *ex_next;
71 	struct exportlist *ex_prev;
72 	struct grouplist *ex_groups;
73 	int	ex_rootuid;
74 	int	ex_exflags;
75 	char	ex_dirp[RPCMNT_PATHLEN+1];
76 };
77 
78 struct grouplist {
79 	struct grouplist *gr_next;
80 	struct hostent *gr_hp;
81 };
82 
83 /* Global defs */
84 int xdr_fhs(), xdr_mlist(), xdr_dir(), xdr_explist();
85 int mntsrv(), get_exportlist();
86 struct exportlist exphead;
87 int mlfile;
88 char exname[MAXPATHLEN];
89 int def_rootuid = -2;
90 extern int errno;
91 #ifdef DEBUG
92 int debug = 1;
93 #else
94 int debug = 0;
95 #endif
96 
97 /*
98  * Mountd server for NFS mount protocol as described in:
99  * NFS: Network File System Protocol Specification, RFC1094, Appendix A
100  * The optional argument is the exports file name
101  * default: _PATH_EXPORTS
102  */
103 main(argc, argv)
104 	int argc;
105 	char *argv[];
106 {
107 	SVCXPRT *transp;
108 
109 	if (debug == 0) {
110 		if (fork())
111 			exit(0);
112 		{ int s;
113 		for (s = 0; s < 10; s++)
114 			(void) close(s);
115 		}
116 		(void) open("/", O_RDONLY);
117 		(void) dup2(0, 1);
118 		(void) dup2(0, 2);
119 		{ int tt = open("/dev/tty", O_RDWR);
120 		  if (tt > 0) {
121 			ioctl(tt, TIOCNOTTY, (char *)0);
122 			close(tt);
123 		  }
124 		}
125 		(void) setpgrp(0, 0);
126 		signal(SIGTSTP, SIG_IGN);
127 		signal(SIGTTIN, SIG_IGN);
128 		signal(SIGTTOU, SIG_IGN);
129 		signal(SIGINT, SIG_IGN);
130 		signal(SIGQUIT, SIG_IGN);
131 		signal(SIGTERM, SIG_IGN);
132 	}
133 	openlog("mountd:", LOG_PID, LOG_DAEMON);
134 	exphead.ex_next = exphead.ex_prev = (struct exportlist *)0;
135 	if (argc == 2) {
136 		strncpy(exname, argv[1], MAXPATHLEN-1);
137 		exname[MAXPATHLEN-1] = '\0';
138 	} else
139 		strcpy(exname, _PATH_EXPORTS);
140 	get_exportlist();
141 	signal(SIGHUP, get_exportlist);
142 	if ((mlfile = open(_PATH_RMOUNTLIST, (O_RDWR|O_CREAT), 0600)) < 0) {
143 		syslog(LOG_ERR, "Can't open mountlist file");
144 		exit(1);
145 	}
146 	if ((transp = svcudp_create(RPC_ANYSOCK)) == NULL) {
147 		syslog(LOG_ERR, "Can't create socket");
148 		exit(1);
149 	}
150 	pmap_unset(RPCPROG_MNT, RPCMNT_VER1);
151 	if (!svc_register(transp, RPCPROG_MNT, RPCMNT_VER1, mntsrv, IPPROTO_UDP)) {
152 		syslog(LOG_ERR, "Can't register mount");
153 		exit(1);
154 	}
155 	svc_run();
156 	syslog(LOG_ERR, "Mountd died");
157 }
158 
159 /*
160  * The mount rpc service
161  */
162 mntsrv(rqstp, transp)
163 	register struct svc_req *rqstp;
164 	register SVCXPRT *transp;
165 {
166 	register struct grouplist *grp;
167 	register u_long **addrp;
168 	register struct exportlist *ep;
169 	struct mountlist ml;
170 	nfsv2fh_t nfh;
171 	struct authunix_parms *ucr;
172 	struct stat stb;
173 	struct hostent *hp;
174 	u_long saddr;
175 	char dirpath[RPCMNT_PATHLEN+1];
176 	int ok = 0;
177 	int bad = ENOENT;
178 	int omask;
179 	uid_t uid = -2;
180 
181 	/* Get authorization */
182 	switch (rqstp->rq_cred.oa_flavor) {
183 	case AUTH_UNIX:
184 		ucr = (struct authunix_parms *)rqstp->rq_clntcred;
185 		uid = ucr->aup_uid;
186 		break;
187 	case AUTH_NULL:
188 	default:
189 		break;
190 	}
191 
192 	saddr = transp->xp_raddr.sin_addr.s_addr;
193 	hp = (struct hostent *)0;
194 	switch (rqstp->rq_proc) {
195 	case NULLPROC:
196 		if (!svc_sendreply(transp, xdr_void, (caddr_t)0))
197 			syslog(LOG_ERR, "Can't send reply");
198 		return;
199 	case RPCMNT_MOUNT:
200 		if (uid != 0) {
201 			svcerr_weakauth(transp);
202 			return;
203 		}
204 		if (!svc_getargs(transp, xdr_dir, dirpath)) {
205 			svcerr_decode(transp);
206 			return;
207 		}
208 
209 		/* Check to see if it's a valid dirpath */
210 		if (stat(dirpath, &stb) < 0 || (stb.st_mode&S_IFMT) !=
211 			S_IFDIR) {
212 			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
213 				syslog(LOG_ERR, "Can't send reply");
214 			return;
215 		}
216 
217 		/* Check in the exports list */
218 		omask = sigblock(sigmask(SIGHUP));
219 		ep = exphead.ex_next;
220 		while (ep != NULL) {
221 			if (!strcmp(ep->ex_dirp, dirpath)) {
222 				grp = ep->ex_groups;
223 				if (grp == NULL)
224 					break;
225 
226 				/* Check for a host match */
227 				addrp = (u_long **)grp->gr_hp->h_addr_list;
228 				for (;;) {
229 					if (**addrp == saddr)
230 						break;
231 					if (*++addrp == NULL)
232 						if (grp = grp->gr_next) {
233 							addrp = (u_long **)
234 								grp->gr_hp->h_addr_list;
235 						} else {
236 							bad = EACCES;
237 							if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
238 								syslog(LOG_ERR, "Can't send reply");
239 							sigsetmask(omask);
240 							return;
241 						}
242 				}
243 				hp = grp->gr_hp;
244 				break;
245 			}
246 			ep = ep->ex_next;
247 		}
248 		sigsetmask(omask);
249 		if (ep == NULL) {
250 			bad = EACCES;
251 			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
252 				syslog(LOG_ERR, "Can't send reply");
253 			return;
254 		}
255 
256 		/* Get the file handle */
257 		bzero((caddr_t)&nfh, sizeof(nfh));
258 		if (getfh(dirpath, (fhandle_t *)&nfh) < 0) {
259 			bad = errno;
260 			if (!svc_sendreply(transp, xdr_long, (caddr_t)&bad))
261 				syslog(LOG_ERR, "Can't send reply");
262 			return;
263 		}
264 #ifdef notnow
265 nfh.fh_generic.fh_fid.fid_gen = htonl(nfh.fh_generic.fh_fid.fid_gen);
266 #endif
267 		if (!svc_sendreply(transp, xdr_fhs, (caddr_t)&nfh))
268 			syslog(LOG_ERR, "Can't send reply");
269 		if (hp == NULL)
270 			hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
271 		if (hp) {
272 			if (!fnd_mnt(hp->h_name, dirpath)) {
273 				strcpy(ml.ml_host, hp->h_name);
274 				strcpy(ml.ml_dirp, dirpath);
275 				write(mlfile, (caddr_t)&ml, sizeof(ml));
276 			}
277 		}
278 		return;
279 	case RPCMNT_DUMP:
280 		if (!svc_sendreply(transp, xdr_mlist, (caddr_t)0))
281 			syslog(LOG_ERR, "Can't send reply");
282 		return;
283 	case RPCMNT_UMOUNT:
284 		if (uid != 0) {
285 			svcerr_weakauth(transp);
286 			return;
287 		}
288 		if (!svc_getargs(transp, xdr_dir, dirpath)) {
289 			svcerr_decode(transp);
290 			return;
291 		}
292 		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
293 		if (hp && fnd_mnt(hp->h_name, dirpath)) {
294 			ml.ml_host[0] = '\0';
295 			write(mlfile, (caddr_t)&ml, sizeof(ml));
296 		}
297 		if (!svc_sendreply(transp, xdr_void, (caddr_t)0))
298 			syslog(LOG_ERR, "Can't send reply");
299 		return;
300 	case RPCMNT_UMNTALL:
301 		if (uid != 0) {
302 			svcerr_weakauth(transp);
303 			return;
304 		}
305 		hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
306 		if (hp) {
307 			lseek(mlfile, (off_t)0, L_SET);
308 			while (read(mlfile, (caddr_t)&ml, sizeof(ml)) ==
309 				sizeof(ml)) {
310 				if (!strcmp(hp->h_name, ml.ml_host)) {
311 					lseek(mlfile, (off_t)-sizeof(ml),
312 						L_INCR);
313 					ml.ml_host[0] = '\0';
314 					write(mlfile, (caddr_t)&ml, sizeof(ml));
315 				}
316 			}
317 		}
318 		if (!svc_sendreply(transp, xdr_void, (caddr_t)0))
319 			syslog(LOG_ERR, "Can't send reply");
320 		return;
321 	case RPCMNT_EXPORT:
322 		if (!svc_sendreply(transp, xdr_explist, (caddr_t)0))
323 			syslog(LOG_ERR, "Can't send reply");
324 		return;
325 	default:
326 		svcerr_noproc(transp);
327 		return;
328 	}
329 }
330 
331 /*
332  * Xdr conversion for a dirpath string
333  */
334 xdr_dir(xdrsp, dirp)
335 	XDR *xdrsp;
336 	char *dirp;
337 {
338 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
339 }
340 
341 /*
342  * Xdr routine to generate fhstatus
343  */
344 xdr_fhs(xdrsp, nfh)
345 	XDR *xdrsp;
346 	nfsv2fh_t *nfh;
347 {
348 	int ok = 0;
349 
350 	if (!xdr_long(xdrsp, &ok))
351 		return (0);
352 	return (xdr_opaque(xdrsp, (caddr_t)nfh, NFSX_FH));
353 }
354 
355 xdr_mlist(xdrsp, cp)
356 	XDR *xdrsp;
357 	caddr_t cp;
358 {
359 	struct mountlist ml;
360 	int true = 1;
361 	int false = 0;
362 	char *strp;
363 
364 	lseek(mlfile, (off_t)0, L_SET);
365 	while (read(mlfile, (caddr_t)&ml, sizeof(ml)) == sizeof(ml)) {
366 		if (ml.ml_host[0] != '\0') {
367 			if (!xdr_bool(xdrsp, &true))
368 				return (0);
369 			strp = &ml.ml_host[0];
370 			if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
371 				return (0);
372 			strp = &ml.ml_dirp[0];
373 			if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
374 				return (0);
375 		}
376 	}
377 	if (!xdr_bool(xdrsp, &false))
378 		return (0);
379 	return (1);
380 }
381 
382 /*
383  * Xdr conversion for export list
384  */
385 xdr_explist(xdrsp, cp)
386 	XDR *xdrsp;
387 	caddr_t cp;
388 {
389 	register struct exportlist *ep;
390 	register struct grouplist *grp;
391 	int true = 1;
392 	int false = 0;
393 	char *strp;
394 	int omask;
395 
396 	omask = sigblock(sigmask(SIGHUP));
397 	ep = exphead.ex_next;
398 	while (ep != NULL) {
399 		if (!xdr_bool(xdrsp, &true))
400 			goto errout;
401 		strp = &ep->ex_dirp[0];
402 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
403 			goto errout;
404 		grp = ep->ex_groups;
405 		while (grp != NULL) {
406 			if (!xdr_bool(xdrsp, &true))
407 				goto errout;
408 			strp = grp->gr_hp->h_name;
409 			if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
410 				goto errout;
411 			grp = grp->gr_next;
412 		}
413 		if (!xdr_bool(xdrsp, &false))
414 			goto errout;
415 		ep = ep->ex_next;
416 	}
417 	sigsetmask(omask);
418 	if (!xdr_bool(xdrsp, &false))
419 		return (0);
420 	return (1);
421 errout:
422 	sigsetmask(omask);
423 	return (0);
424 }
425 
426 #define LINESIZ	10240
427 char line[LINESIZ];
428 
429 /*
430  * Get the export list
431  */
432 get_exportlist()
433 {
434 	register struct hostent *hp, *nhp;
435 	register char **addrp, **naddrp;
436 	register int i;
437 	register struct grouplist *grp, *grp2;
438 	register struct exportlist *ep, *ep2;
439 	FILE *inf;
440 	char *cp, *endcp;
441 	char savedc;
442 	int len;
443 	int rootuid, exflags;
444 
445 	/*
446 	 * First, get rid of the old list
447 	 */
448 	ep = exphead.ex_next;
449 	while (ep != NULL) {
450 		grp = ep->ex_groups;
451 		while (grp != NULL) {
452 			addrp = grp->gr_hp->h_addr_list;
453 			while (*addrp)
454 				free(*addrp++);
455 			free((caddr_t)grp->gr_hp->h_addr_list);
456 			free(grp->gr_hp->h_name);
457 			free((caddr_t)grp->gr_hp);
458 			grp2 = grp;
459 			grp = grp->gr_next;
460 			free((caddr_t)grp2);
461 		}
462 		ep2 = ep;
463 		ep = ep->ex_next;
464 		free((caddr_t)ep2);
465 	}
466 
467 	/*
468 	 * Read in the exports file and build the list, calling
469 	 * exportfs() as we go along
470 	 */
471 	exphead.ex_next = exphead.ex_prev = (struct exportlist *)0;
472 	if ((inf = fopen(exname, "r")) == NULL) {
473 		syslog(LOG_ERR, "Can't open %s", exname);
474 		exit(2);
475 	}
476 	while (fgets(line, LINESIZ, inf)) {
477 		exflags = 0;
478 		rootuid = def_rootuid;
479 		cp = line;
480 		nextfield(&cp, &endcp);
481 		len = endcp-cp;
482 		if (len <= RPCMNT_PATHLEN && len > 0) {
483 			ep = (struct exportlist *)malloc(sizeof(*ep));
484 			ep->ex_next = ep->ex_prev = (struct exportlist *)0;
485 			ep->ex_groups = (struct grouplist *)0;
486 			bcopy(cp, ep->ex_dirp, len);
487 			ep->ex_dirp[len] = '\0';
488 		} else
489 			goto err;
490 		cp = endcp;
491 		nextfield(&cp, &endcp);
492 		len = endcp-cp;
493 		while (len > 0) {
494 			savedc = *endcp;
495 			*endcp = '\0';
496 			if (len <= RPCMNT_NAMELEN) {
497 				if (*cp == '-') {
498 					cp++;
499 					switch (*cp) {
500 					case 'o':
501 						exflags |= M_EXRDONLY;
502 						break;
503 					case 'r':
504 						if (*++cp == '=')
505 							rootuid = atoi(++cp);
506 						break;
507 					default:
508 						syslog(LOG_WARNING,
509 						  "Bad -%c option in %s",
510 						  *cp, exname);
511 						break;
512 					};
513 				} else if (hp = gethostbyname(cp)) {
514 					grp = (struct grouplist *)
515 						malloc(sizeof(struct grouplist));
516 					if (grp == NULL)
517 						goto err;
518 					nhp = grp->gr_hp = (struct hostent *)
519 						malloc(sizeof(struct hostent));
520 					if (nhp == NULL)
521 						goto err;
522 					bcopy((caddr_t)hp, (caddr_t)nhp,
523 						sizeof(struct hostent));
524 					i = strlen(hp->h_name)+1;
525 					nhp->h_name = (char *)malloc(i);
526 					if (nhp->h_name == NULL)
527 						goto err;
528 					bcopy(hp->h_name, nhp->h_name, i);
529 					addrp = hp->h_addr_list;
530 					i = 1;
531 					while (*addrp++)
532 						i++;
533 					naddrp = nhp->h_addr_list = (char **)
534 						malloc(i*sizeof(char *));
535 					if (naddrp == NULL)
536 						goto err;
537 					addrp = hp->h_addr_list;
538 					while (*addrp) {
539 						*naddrp = (char *)
540 						    malloc(hp->h_length);
541 						bcopy(*addrp, *naddrp,
542 							hp->h_length);
543 						addrp++;
544 						naddrp++;
545 					}
546 					*naddrp = (char *)0;
547 					grp->gr_next = ep->ex_groups;
548 					ep->ex_groups = grp;
549 				}
550 			}
551 			cp = endcp;
552 			*cp = savedc;
553 			nextfield(&cp, &endcp);
554 			len = endcp-cp;
555 		}
556 		if (exportfs(ep->ex_dirp, rootuid, exflags) < 0) {
557 			syslog(LOG_WARNING, "Can't export %s", ep->ex_dirp);
558 			free((caddr_t)ep);
559 		} else {
560 			ep->ex_rootuid = rootuid;
561 			ep->ex_exflags = exflags;
562 			ep->ex_next = exphead.ex_next;
563 			ep->ex_prev = &exphead;
564 			if (ep->ex_next != NULL)
565 				ep->ex_next->ex_prev = ep;
566 			exphead.ex_next = ep;
567 		}
568 	}
569 	fclose(inf);
570 	return;
571 err:
572 	syslog(LOG_ERR, "Bad Exports File, mountd Failed");
573 	exit(2);
574 }
575 
576 /*
577  * Parse out the next white space separated field
578  */
579 nextfield(cp, endcp)
580 	char **cp;
581 	char **endcp;
582 {
583 	register char *p;
584 
585 	p = *cp;
586 	while (*p == ' ' || *p == '\t')
587 		p++;
588 	if (*p == '\n' || *p == '\0') {
589 		*cp = *endcp = p;
590 		return;
591 	}
592 	*cp = p++;
593 	while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
594 		p++;
595 	*endcp = p;
596 }
597 
598 /*
599  * Search the remote mounts file for a match.
600  * Iff found
601  *	- set file offset to record and return TRUE
602  * else
603  *	- set file offset to an unused record if found or eof otherwise
604  *	  and return FALSE
605  */
606 fnd_mnt(host, dirp)
607 	char *host;
608 	char *dirp;
609 {
610 	struct mountlist ml;
611 	off_t off, pos;
612 
613 	off = -1;
614 	pos = 0;
615 	lseek(mlfile, (off_t)0, L_SET);
616 	while (read(mlfile, (caddr_t)&ml, sizeof(ml)) == sizeof(ml)) {
617 		if (!strcmp(host, ml.ml_host) && !strcmp(dirp, ml.ml_dirp)) {
618 			lseek(mlfile, (off_t)-sizeof(ml), L_INCR);
619 			return (TRUE);
620 		} else if (ml.ml_host[0] == '\0') {
621 			off = pos;
622 		}
623 		pos += sizeof(ml);
624 	}
625 	if (off != -1)
626 		lseek(mlfile, off, L_SET);
627 	else
628 		lseek(mlfile, (off_t)0, L_XTND);
629 	return (FALSE);
630 }
631