mount_nfs.c revision 1.6 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.6 1994/09/23 14:27:30 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 memset(&isoaddr, 0, sizeof (isoaddr));
410 memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
411 isoaddr.siso_len = sizeof (isoaddr);
412 isoaddr.siso_family = AF_ISO;
413 isoaddr.siso_tlen = 2;
414 isoport = htons(NFS_PORT);
415 memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
416 hostp = delimp + 1;
417 }
418 #endif /* ISO */
419
420 /*
421 * Handle an internet host address and reverse resolve it if
422 * doing Kerberos.
423 */
424 if (isdigit(*hostp)) {
425 if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) {
426 warnx("bad net address %s", hostp);
427 return (0);
428 }
429 if ((nfsargsp->flags & NFSMNT_KERB)) {
430 if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
431 sizeof (u_long), AF_INET)) == (struct hostent *)0) {
432 warnx("can't reverse resolve net address");
433 return (0);
434 }
435 memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
436 }
437 } else if ((hp = gethostbyname(hostp)) == NULL) {
438 warnx("can't get net id for host");
439 return (0);
440 } else
441 memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
442 #ifdef KERBEROS
443 if (nfsargsp->flags & NFSMNT_KERB) {
444 strncpy(inst, hp->h_name, INST_SZ);
445 inst[INST_SZ - 1] = '\0';
446 if (cp = strchr(inst, '.'))
447 *cp = '\0';
448 }
449 #endif /* KERBEROS */
450
451 nfhret.stat = EACCES; /* Mark not yet successful */
452 while (retrycnt > 0) {
453 saddr.sin_family = AF_INET;
454 saddr.sin_port = htons(PMAPPORT);
455 if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
456 NFS_VER2, nfsargsp->sotype == SOCK_STREAM ? IPPROTO_TCP :
457 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 = (nfsargsp->sotype == SOCK_STREAM ?
465 clnttcp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1,
466 &so, 0, 0) :
467 clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1,
468 pertry, &so))) == NULL) {
469 if ((opflags & ISBGRND) == 0)
470 clnt_pcreateerror("Cannot MNT PRC");
471 } else {
472 clp->cl_auth = authunix_create_default();
473 try.tv_sec = 10;
474 try.tv_usec = 0;
475 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
476 xdr_dir, spec, xdr_fh, &nfhret, try);
477 if (clnt_stat != RPC_SUCCESS) {
478 if ((opflags & ISBGRND) == 0)
479 warnx("%s", clnt_sperror(clp,
480 "bad MNT RPC"));
481 } else {
482 auth_destroy(clp->cl_auth);
483 clnt_destroy(clp);
484 retrycnt = 0;
485 }
486 }
487 }
488 if (--retrycnt > 0) {
489 if (opflags & BGRND) {
490 opflags &= ~BGRND;
491 if (i = fork()) {
492 if (i == -1)
493 err(1, "nqnfs 2");
494 exit(0);
495 }
496 (void) setsid();
497 (void) close(STDIN_FILENO);
498 (void) close(STDOUT_FILENO);
499 (void) close(STDERR_FILENO);
500 (void) chdir("/");
501 opflags |= ISBGRND;
502 }
503 sleep(60);
504 }
505 }
506 if (nfhret.stat) {
507 if (opflags & ISBGRND)
508 exit(1);
509 errno = nfhret.stat;
510 warn("can't access %s", spec);
511 return (0);
512 }
513 saddr.sin_port = htons(tport);
514 #ifdef ISO
515 if (isoflag) {
516 nfsargsp->addr = (struct sockaddr *) &isoaddr;
517 nfsargsp->addrlen = sizeof (isoaddr);
518 } else
519 #endif /* ISO */
520 {
521 nfsargsp->addr = (struct sockaddr *) &saddr;
522 nfsargsp->addrlen = sizeof (saddr);
523 }
524 nfsargsp->fh = &nfhret.nfh;
525 nfsargsp->hostname = nam;
526 return (1);
527 }
528
529 /*
530 * xdr routines for mount rpc's
531 */
532 int
533 xdr_dir(xdrsp, dirp)
534 XDR *xdrsp;
535 char *dirp;
536 {
537 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
538 }
539
540 int
541 xdr_fh(xdrsp, np)
542 XDR *xdrsp;
543 struct nfhret *np;
544 {
545 if (!xdr_u_long(xdrsp, &(np->stat)))
546 return (0);
547 if (np->stat)
548 return (1);
549 return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
550 }
551
552 __dead void
553 usage()
554 {
555 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n",
556 "[-bcdiKklMPqsT] [-a maxreadahead] [-D deadthresh]",
557 "\t[-g maxgroups] [-L leaseterm] [-m realm] [-o options] [-R retrycnt]",
558 "\t[-r readsize] [-t timeout] [-w writesize] [-x retrans]",
559 "\trhost:path node");
560 exit(1);
561 }
562