xref: /netbsd-src/sbin/mount_nfs/mount_nfs.c (revision 6ea46cb5e46c49111a6ecf3bcbe3c7e2730fe9f6)
1 /*
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  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, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1992, 1993, 1994\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 /*static char sccsid[] = "from: @(#)mount_nfs.c	8.3 (Berkeley) 3/27/94";*/
45 static char *rcsid = "$Id: mount_nfs.c,v 1.4 1994/09/05 02:21:06 mycroft Exp $";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/mount.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/stat.h>
53 #include <sys/syslog.h>
54 
55 #include <rpc/rpc.h>
56 #include <rpc/pmap_clnt.h>
57 #include <rpc/pmap_prot.h>
58 
59 #ifdef ISO
60 #include <netiso/iso.h>
61 #endif
62 
63 #ifdef KERBEROS
64 #include <kerberosIV/des.h>
65 #include <kerberosIV/krb.h>
66 #endif
67 
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsv2.h>
70 #define KERNEL
71 #include <nfs/nfs.h>
72 #undef KERNEL
73 #include <nfs/nqnfs.h>
74 
75 #include <arpa/inet.h>
76 
77 #include <ctype.h>
78 #include <err.h>
79 #include <errno.h>
80 #include <fcntl.h>
81 #include <netdb.h>
82 #include <signal.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <strings.h>
86 #include <unistd.h>
87 
88 #include "mntopts.h"
89 
90 struct mntopt mopts[] = {
91 	MOPT_STDOPTS,
92 	MOPT_FORCE,
93 	MOPT_UPDATE,
94 	{ NULL }
95 };
96 
97 struct nfs_args nfsdefargs = {
98 	(struct sockaddr *)0,
99 	sizeof (struct sockaddr_in),
100 	SOCK_DGRAM,
101 	0,
102 	(nfsv2fh_t *)0,
103 	0,
104 	NFS_WSIZE,
105 	NFS_RSIZE,
106 	NFS_TIMEO,
107 	NFS_RETRANS,
108 	NFS_MAXGRPS,
109 	NFS_DEFRAHEAD,
110 	NQ_DEFLEASE,
111 	NQ_DEADTHRESH,
112 	(char *)0,
113 };
114 
115 struct nfhret {
116 	u_long	stat;
117 	nfsv2fh_t nfh;
118 };
119 #define	DEF_RETRY	10000
120 #define	BGRND	1
121 #define	ISBGRND	2
122 int retrycnt = DEF_RETRY;
123 int opflags = 0;
124 
125 #ifdef KERBEROS
126 char inst[INST_SZ];
127 char realm[REALM_SZ];
128 KTEXT_ST kt;
129 #endif
130 
131 int	getnfsargs __P((char *, struct nfs_args *));
132 #ifdef ISO
133 struct	iso_addr *iso_addr __P((const char *));
134 #endif
135 void	set_rpc_maxgrouplist __P((int));
136 __dead	void usage __P((void));
137 int	xdr_dir __P((XDR *, char *));
138 int	xdr_fh __P((XDR *, struct nfhret *));
139 
140 int
141 main(argc, argv)
142 	int argc;
143 	char *argv[];
144 {
145 	register int c;
146 	register struct nfs_args *nfsargsp;
147 	struct nfs_args nfsargs;
148 	struct nfsd_cargs ncd;
149 	int mntflags, i, nfssvc_flag, num;
150 	char *name, *p, *spec;
151 	int error = 0;
152 #ifdef KERBEROS
153 	uid_t last_ruid;
154 #endif
155 
156 #ifdef KERBEROS
157 	last_ruid = -1;
158 	if (krb_get_lrealm(realm, 0) != KSUCCESS)
159 	    (void)strcpy(realm, KRB_REALM);
160 #endif
161 	retrycnt = DEF_RETRY;
162 
163 	mntflags = 0;
164 	nfsargs = nfsdefargs;
165 	nfsargsp = &nfsargs;
166 	while ((c = getopt(argc, argv,
167 	    "a:bcdD:g:iKklL:Mm:o:PpqR:r:sTt:w:x:")) != EOF)
168 		switch (c) {
169 		case 'a':
170 			num = strtol(optarg, &p, 10);
171 			if (*p || num < 0)
172 				errx(1, "illegal -a value -- %s", optarg);
173 			nfsargsp->readahead = num;
174 			nfsargsp->flags |= NFSMNT_READAHEAD;
175 			break;
176 		case 'b':
177 			opflags |= BGRND;
178 			break;
179 		case 'c':
180 			nfsargsp->flags |= NFSMNT_NOCONN;
181 			break;
182 		case 'D':
183 			num = strtol(optarg, &p, 10);
184 			if (*p || num <= 0)
185 				errx(1, "illegal -D value -- %s", optarg);
186 			nfsargsp->deadthresh = num;
187 			nfsargsp->flags |= NFSMNT_DEADTHRESH;
188 			break;
189 		case 'd':
190 			nfsargsp->flags |= NFSMNT_DUMBTIMR;
191 			break;
192 #if 0 /* XXXX */
193 		case 'g':
194 			num = strtol(optarg, &p, 10);
195 			if (*p || num <= 0)
196 				errx(1, "illegal -g value -- %s", optarg);
197 			set_rpc_maxgrouplist(num);
198 			nfsargsp->maxgrouplist = num;
199 			nfsargsp->flags |= NFSMNT_MAXGRPS;
200 			break;
201 #endif
202 		case 'i':
203 			nfsargsp->flags |= NFSMNT_INT;
204 			break;
205 #ifdef KERBEROS
206 		case 'K':
207 			nfsargsp->flags |= NFSMNT_KERB;
208 			break;
209 #endif
210 		case 'k':
211 			nfsargsp->flags |= NFSMNT_NQLOOKLEASE;
212 			break;
213 		case 'L':
214 			num = strtol(optarg, &p, 10);
215 			if (*p || num < 2)
216 				errx(1, "illegal -L value -- %s", optarg);
217 			nfsargsp->leaseterm = num;
218 			nfsargsp->flags |= NFSMNT_LEASETERM;
219 			break;
220 		case 'l':
221 			nfsargsp->flags |= NFSMNT_RDIRALOOK;
222 			break;
223 		case 'M':
224 			nfsargsp->flags |= NFSMNT_MYWRITE;
225 			break;
226 #ifdef KERBEROS
227 		case 'm':
228 			(void)strncpy(realm, optarg, REALM_SZ - 1);
229 			realm[REALM_SZ - 1] = '\0';
230 			break;
231 #endif
232 		case 'o':
233 			getmntopts(optarg, mopts, &mntflags);
234 			break;
235 		case 'P':
236 			nfsargsp->flags |= NFSMNT_RESVPORT;
237 			break;
238 #ifdef ISO
239 		case 'p':
240 			nfsargsp->sotype = SOCK_SEQPACKET;
241 			break;
242 #endif
243 		case 'q':
244 			nfsargsp->flags |= NFSMNT_NQNFS;
245 			break;
246 		case 'R':
247 			num = strtol(optarg, &p, 10);
248 			if (*p || num <= 0)
249 				errx(1, "illegal -R value -- %s", optarg);
250 			retrycnt = num;
251 			break;
252 		case 'r':
253 			num = strtol(optarg, &p, 10);
254 			if (*p || num <= 0)
255 				errx(1, "illegal -r value -- %s", optarg);
256 			nfsargsp->rsize = num;
257 			nfsargsp->flags |= NFSMNT_RSIZE;
258 			break;
259 		case 's':
260 			nfsargsp->flags |= NFSMNT_SOFT;
261 			break;
262 		case 'T':
263 			nfsargsp->sotype = SOCK_STREAM;
264 			break;
265 		case 't':
266 			num = strtol(optarg, &p, 10);
267 			if (*p || num <= 0)
268 				errx(1, "illegal -t value -- %s", optarg);
269 			nfsargsp->timeo = num;
270 			nfsargsp->flags |= NFSMNT_TIMEO;
271 			break;
272 		case 'w':
273 			num = strtol(optarg, &p, 10);
274 			if (*p || num <= 0)
275 				errx(1, "illegal -w value -- %s", optarg);
276 			nfsargsp->wsize = num;
277 			nfsargsp->flags |= NFSMNT_WSIZE;
278 			break;
279 		case 'x':
280 			num = strtol(optarg, &p, 10);
281 			if (*p || num <= 0)
282 				errx(1, "illegal -x value -- %s", optarg);
283 			nfsargsp->retrans = num;
284 			nfsargsp->flags |= NFSMNT_RETRANS;
285 			break;
286 		default:
287 			usage();
288 			break;
289 		}
290 	argc -= optind;
291 	argv += optind;
292 
293 	if (argc != 2)
294 		error = 1;
295 
296 	spec = *argv++;
297 	name = *argv;
298 
299 	if (!getnfsargs(spec, nfsargsp))
300 		exit(1);
301 	if (mount(MOUNT_NFS, name, mntflags, nfsargsp))
302 		err(1, "%s", name);
303 	if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
304 		if ((opflags & ISBGRND) == 0) {
305 			if (i = fork()) {
306 				if (i == -1)
307 					err(1, "nqnfs 1");
308 				exit(0);
309 			}
310 			(void) setsid();
311 			(void) close(STDIN_FILENO);
312 			(void) close(STDOUT_FILENO);
313 			(void) close(STDERR_FILENO);
314 			(void) chdir("/");
315 		}
316 		openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
317 		nfssvc_flag = NFSSVC_MNTD;
318 		ncd.ncd_dirp = name;
319 		while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
320 			if (errno != ENEEDAUTH) {
321 				syslog(LOG_ERR, "nfssvc err %m");
322 				continue;
323 			}
324 			nfssvc_flag =
325 			    NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
326 #ifdef KERBEROS
327 			/*
328 			 * Set up as ncd_authuid for the kerberos call.
329 			 * Must set ruid to ncd_authuid and reset the
330 			 * ticket name iff ncd_authuid is not the same
331 			 * as last time, so that the right ticket file
332 			 * is found.
333 			 */
334 			if (ncd.ncd_authuid != last_ruid) {
335 				krb_set_tkt_string("");
336 				last_ruid = ncd.ncd_authuid;
337 			}
338 			setreuid(ncd.ncd_authuid, 0);
339 			if (krb_mk_req(&kt, "rcmd", inst, realm, 0) ==
340 			    KSUCCESS &&
341 			    kt.length <= (RPCAUTH_MAXSIZ - 2 * NFSX_UNSIGNED)) {
342 				ncd.ncd_authtype = RPCAUTH_NQNFS;
343 				ncd.ncd_authlen = kt.length;
344 				ncd.ncd_authstr = (char *)kt.dat;
345 				nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
346 			}
347 			setreuid(0, 0);
348 #endif /* KERBEROS */
349 		}
350 	}
351 	exit(0);
352 }
353 
354 int
355 getnfsargs(spec, nfsargsp)
356 	char *spec;
357 	struct nfs_args *nfsargsp;
358 {
359 	register CLIENT *clp;
360 	struct hostent *hp;
361 	static struct sockaddr_in saddr;
362 #ifdef ISO
363 	static struct sockaddr_iso isoaddr;
364 	struct iso_addr *isop;
365 	int isoflag = 0;
366 #endif
367 	struct timeval pertry, try;
368 	enum clnt_stat clnt_stat;
369 	int so = RPC_ANYSOCK, i;
370 	char *hostp, *delimp;
371 #ifdef KERBEROS
372 	char *cp;
373 #endif
374 	u_short tport;
375 	static struct nfhret nfhret;
376 	static char nam[MNAMELEN + 1];
377 
378 	strncpy(nam, spec, MNAMELEN);
379 	nam[MNAMELEN] = '\0';
380 	if ((delimp = strchr(spec, '@')) != NULL) {
381 		hostp = delimp + 1;
382 	} else if ((delimp = strchr(spec, ':')) != NULL) {
383 		hostp = spec;
384 		spec = delimp + 1;
385 	} else {
386 		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
387 		return (0);
388 	}
389 	*delimp = '\0';
390 	/*
391 	 * DUMB!! Until the mount protocol works on iso transport, we must
392 	 * supply both an iso and an inet address for the host.
393 	 */
394 #ifdef ISO
395 	if (!strncmp(hostp, "iso=", 4)) {
396 		u_short isoport;
397 
398 		hostp += 4;
399 		isoflag++;
400 		if ((delimp = strchr(hostp, '+')) == NULL) {
401 			warnx("no iso+inet address");
402 			return (0);
403 		}
404 		*delimp = '\0';
405 		if ((isop = iso_addr(hostp)) == NULL) {
406 			warnx("bad ISO address");
407 			return (0);
408 		}
409 		bzero((caddr_t)&isoaddr, sizeof (isoaddr));
410 		bcopy((caddr_t)isop, (caddr_t)&isoaddr.siso_addr,
411 			sizeof (struct iso_addr));
412 		isoaddr.siso_len = sizeof (isoaddr);
413 		isoaddr.siso_family = AF_ISO;
414 		isoaddr.siso_tlen = 2;
415 		isoport = htons(NFS_PORT);
416 		bcopy((caddr_t)&isoport, TSEL(&isoaddr), isoaddr.siso_tlen);
417 		hostp = delimp + 1;
418 	}
419 #endif /* ISO */
420 
421 	/*
422 	 * Handle an internet host address and reverse resolve it if
423 	 * doing Kerberos.
424 	 */
425 	if (isdigit(*hostp)) {
426 		if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) {
427 			warnx("bad net address %s", hostp);
428 			return (0);
429 		}
430 		if ((nfsargsp->flags & NFSMNT_KERB)) {
431 		    if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
432 			 sizeof (u_long), AF_INET)) == (struct hostent *)0) {
433 			warnx("can't reverse resolve net address");
434 			return (0);
435 		    }
436 		    bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
437 		}
438 	} else if ((hp = gethostbyname(hostp)) == NULL) {
439 		warnx("can't get net id for host");
440 		return (0);
441 	} else
442 		bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
443 #ifdef KERBEROS
444 	if (nfsargsp->flags & NFSMNT_KERB) {
445 		strncpy(inst, hp->h_name, INST_SZ);
446 		inst[INST_SZ - 1] = '\0';
447 		if (cp = strchr(inst, '.'))
448 			*cp = '\0';
449 	}
450 #endif /* KERBEROS */
451 
452 	nfhret.stat = EACCES;	/* Mark not yet successful */
453 	while (retrycnt > 0) {
454 		saddr.sin_family = AF_INET;
455 		saddr.sin_port = htons(PMAPPORT);
456 		if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
457 		    NFS_VER2, IPPROTO_UDP)) == 0) {
458 			if ((opflags & ISBGRND) == 0)
459 				clnt_pcreateerror("NFS Portmap");
460 		} else {
461 			saddr.sin_port = 0;
462 			pertry.tv_sec = 10;
463 			pertry.tv_usec = 0;
464 			if ((clp = clntudp_create(&saddr, RPCPROG_MNT,
465 			    RPCMNT_VER1, pertry, &so)) == NULL) {
466 				if ((opflags & ISBGRND) == 0)
467 					clnt_pcreateerror("Cannot MNT PRC");
468 			} else {
469 				clp->cl_auth = authunix_create_default();
470 				try.tv_sec = 10;
471 				try.tv_usec = 0;
472 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
473 				    xdr_dir, spec, xdr_fh, &nfhret, try);
474 				if (clnt_stat != RPC_SUCCESS) {
475 					if ((opflags & ISBGRND) == 0)
476 						warnx("%s", clnt_sperror(clp,
477 						    "bad MNT RPC"));
478 				} else {
479 					auth_destroy(clp->cl_auth);
480 					clnt_destroy(clp);
481 					retrycnt = 0;
482 				}
483 			}
484 		}
485 		if (--retrycnt > 0) {
486 			if (opflags & BGRND) {
487 				opflags &= ~BGRND;
488 				if (i = fork()) {
489 					if (i == -1)
490 						err(1, "nqnfs 2");
491 					exit(0);
492 				}
493 				(void) setsid();
494 				(void) close(STDIN_FILENO);
495 				(void) close(STDOUT_FILENO);
496 				(void) close(STDERR_FILENO);
497 				(void) chdir("/");
498 				opflags |= ISBGRND;
499 			}
500 			sleep(60);
501 		}
502 	}
503 	if (nfhret.stat) {
504 		if (opflags & ISBGRND)
505 			exit(1);
506 		errno = nfhret.stat;
507 		warn("can't access %s", spec);
508 		return (0);
509 	}
510 	saddr.sin_port = htons(tport);
511 #ifdef ISO
512 	if (isoflag) {
513 		nfsargsp->addr = (struct sockaddr *) &isoaddr;
514 		nfsargsp->addrlen = sizeof (isoaddr);
515 	} else
516 #endif /* ISO */
517 	{
518 		nfsargsp->addr = (struct sockaddr *) &saddr;
519 		nfsargsp->addrlen = sizeof (saddr);
520 	}
521 	nfsargsp->fh = &nfhret.nfh;
522 	nfsargsp->hostname = nam;
523 	return (1);
524 }
525 
526 /*
527  * xdr routines for mount rpc's
528  */
529 int
530 xdr_dir(xdrsp, dirp)
531 	XDR *xdrsp;
532 	char *dirp;
533 {
534 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
535 }
536 
537 int
538 xdr_fh(xdrsp, np)
539 	XDR *xdrsp;
540 	struct nfhret *np;
541 {
542 	if (!xdr_u_long(xdrsp, &(np->stat)))
543 		return (0);
544 	if (np->stat)
545 		return (1);
546 	return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
547 }
548 
549 __dead void
550 usage()
551 {
552 	(void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n",
553 "[-bcdiKklMPqsT] [-a maxreadahead] [-D deadthresh]",
554 "\t[-g maxgroups] [-L leaseterm] [-m realm] [-o options] [-R retrycnt]",
555 "\t[-r readsize] [-t timeout] [-w writesize] [-x retrans]",
556 "\trhost:path node");
557 	exit(1);
558 }
559