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