mount_nfs.c revision 1.2 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.2 1994/06/24 12:04:53 pk 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 (void)strcpy(realm, KRB_REALM);
159 #endif
160 retrycnt = DEF_RETRY;
161
162 mntflags = 0;
163 nfsargs = nfsdefargs;
164 nfsargsp = &nfsargs;
165 while ((c = getopt(argc, argv,
166 "a:bcdD:g:iKklL:Mm:o:PpqR:r:sTt:w:x:")) != EOF)
167 switch (c) {
168 case 'a':
169 num = strtol(optarg, &p, 10);
170 if (*p || num < 0)
171 errx(1, "illegal -a value -- %s", optarg);
172 nfsargsp->readahead = num;
173 nfsargsp->flags |= NFSMNT_READAHEAD;
174 break;
175 case 'b':
176 opflags |= BGRND;
177 break;
178 case 'c':
179 nfsargsp->flags |= NFSMNT_NOCONN;
180 break;
181 case 'D':
182 num = strtol(optarg, &p, 10);
183 if (*p || num <= 0)
184 errx(1, "illegal -D value -- %s", optarg);
185 nfsargsp->deadthresh = num;
186 nfsargsp->flags |= NFSMNT_DEADTHRESH;
187 break;
188 case 'd':
189 nfsargsp->flags |= NFSMNT_DUMBTIMR;
190 break;
191 #if 0 /* XXXX */
192 case 'g':
193 num = strtol(optarg, &p, 10);
194 if (*p || num <= 0)
195 errx(1, "illegal -g value -- %s", optarg);
196 set_rpc_maxgrouplist(num);
197 nfsargsp->maxgrouplist = num;
198 nfsargsp->flags |= NFSMNT_MAXGRPS;
199 break;
200 #endif
201 case 'i':
202 nfsargsp->flags |= NFSMNT_INT;
203 break;
204 #ifdef KERBEROS
205 case 'K':
206 nfsargsp->flags |= NFSMNT_KERB;
207 break;
208 #endif
209 case 'k':
210 nfsargsp->flags |= NFSMNT_NQLOOKLEASE;
211 break;
212 case 'L':
213 num = strtol(optarg, &p, 10);
214 if (*p || num < 2)
215 errx(1, "illegal -L value -- %s", optarg);
216 nfsargsp->leaseterm = num;
217 nfsargsp->flags |= NFSMNT_LEASETERM;
218 break;
219 case 'l':
220 nfsargsp->flags |= NFSMNT_RDIRALOOK;
221 break;
222 case 'M':
223 nfsargsp->flags |= NFSMNT_MYWRITE;
224 break;
225 #ifdef KERBEROS
226 case 'm':
227 (void)strncpy(realm, optarg, REALM_SZ - 1);
228 realm[REALM_SZ - 1] = '\0';
229 break;
230 #endif
231 case 'o':
232 getmntopts(optarg, mopts, &mntflags);
233 break;
234 case 'P':
235 nfsargsp->flags |= NFSMNT_RESVPORT;
236 break;
237 #ifdef ISO
238 case 'p':
239 nfsargsp->sotype = SOCK_SEQPACKET;
240 break;
241 #endif
242 case 'q':
243 nfsargsp->flags |= NFSMNT_NQNFS;
244 break;
245 case 'R':
246 num = strtol(optarg, &p, 10);
247 if (*p || num <= 0)
248 errx(1, "illegal -R value -- %s", optarg);
249 retrycnt = num;
250 break;
251 case 'r':
252 num = strtol(optarg, &p, 10);
253 if (*p || num <= 0)
254 errx(1, "illegal -r value -- %s", optarg);
255 nfsargsp->rsize = num;
256 nfsargsp->flags |= NFSMNT_RSIZE;
257 break;
258 case 's':
259 nfsargsp->flags |= NFSMNT_SOFT;
260 break;
261 case 'T':
262 nfsargsp->sotype = SOCK_STREAM;
263 break;
264 case 't':
265 num = strtol(optarg, &p, 10);
266 if (*p || num <= 0)
267 errx(1, "illegal -t value -- %s", optarg);
268 nfsargsp->timeo = num;
269 nfsargsp->flags |= NFSMNT_TIMEO;
270 break;
271 case 'w':
272 num = strtol(optarg, &p, 10);
273 if (*p || num <= 0)
274 errx(1, "illegal -w value -- %s", optarg);
275 nfsargsp->wsize = num;
276 nfsargsp->flags |= NFSMNT_WSIZE;
277 break;
278 case 'x':
279 num = strtol(optarg, &p, 10);
280 if (*p || num <= 0)
281 errx(1, "illegal -x value -- %s", optarg);
282 nfsargsp->retrans = num;
283 nfsargsp->flags |= NFSMNT_RETRANS;
284 break;
285 default:
286 usage();
287 break;
288 }
289 argc -= optind;
290 argv += optind;
291
292 if (argc != 2)
293 error = 1;
294
295 spec = *argv++;
296 name = *argv;
297
298 if (!getnfsargs(spec, nfsargsp))
299 exit(1);
300 if (mount(MOUNT_NFS, name, mntflags, nfsargsp))
301 err(1, "%s", name);
302 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
303 if ((opflags & ISBGRND) == 0) {
304 if (i = fork()) {
305 if (i == -1)
306 err(1, "nqnfs 1");
307 exit(0);
308 }
309 (void) setsid();
310 (void) close(STDIN_FILENO);
311 (void) close(STDOUT_FILENO);
312 (void) close(STDERR_FILENO);
313 (void) chdir("/");
314 }
315 openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
316 nfssvc_flag = NFSSVC_MNTD;
317 ncd.ncd_dirp = name;
318 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
319 if (errno != ENEEDAUTH) {
320 syslog(LOG_ERR, "nfssvc err %m");
321 continue;
322 }
323 nfssvc_flag =
324 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
325 #ifdef KERBEROS
326 /*
327 * Set up as ncd_authuid for the kerberos call.
328 * Must set ruid to ncd_authuid and reset the
329 * ticket name iff ncd_authuid is not the same
330 * as last time, so that the right ticket file
331 * is found.
332 */
333 if (ncd.ncd_authuid != last_ruid) {
334 krb_set_tkt_string("");
335 last_ruid = ncd.ncd_authuid;
336 }
337 setreuid(ncd.ncd_authuid, 0);
338 if (krb_mk_req(&kt, "rcmd", inst, realm, 0) ==
339 KSUCCESS &&
340 kt.length <= (RPCAUTH_MAXSIZ - 2 * NFSX_UNSIGNED)) {
341 ncd.ncd_authtype = RPCAUTH_NQNFS;
342 ncd.ncd_authlen = kt.length;
343 ncd.ncd_authstr = (char *)kt.dat;
344 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
345 }
346 setreuid(0, 0);
347 #endif /* KERBEROS */
348 }
349 }
350 exit(0);
351 }
352
353 int
354 getnfsargs(spec, nfsargsp)
355 char *spec;
356 struct nfs_args *nfsargsp;
357 {
358 register CLIENT *clp;
359 struct hostent *hp;
360 static struct sockaddr_in saddr;
361 #ifdef ISO
362 static struct sockaddr_iso isoaddr;
363 struct iso_addr *isop;
364 int isoflag = 0;
365 #endif
366 struct timeval pertry, try;
367 enum clnt_stat clnt_stat;
368 int so = RPC_ANYSOCK, i;
369 char *hostp, *delimp;
370 #ifdef KERBEROS
371 char *cp;
372 #endif
373 u_short tport;
374 static struct nfhret nfhret;
375 static char nam[MNAMELEN + 1];
376
377 strncpy(nam, spec, MNAMELEN);
378 nam[MNAMELEN] = '\0';
379 if ((delimp = strchr(spec, '@')) != NULL) {
380 hostp = delimp + 1;
381 } else if ((delimp = strchr(spec, ':')) != NULL) {
382 hostp = spec;
383 spec = delimp + 1;
384 } else {
385 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
386 return (0);
387 }
388 *delimp = '\0';
389 /*
390 * DUMB!! Until the mount protocol works on iso transport, we must
391 * supply both an iso and an inet address for the host.
392 */
393 #ifdef ISO
394 if (!strncmp(hostp, "iso=", 4)) {
395 u_short isoport;
396
397 hostp += 4;
398 isoflag++;
399 if ((delimp = strchr(hostp, '+')) == NULL) {
400 warnx("no iso+inet address");
401 return (0);
402 }
403 *delimp = '\0';
404 if ((isop = iso_addr(hostp)) == NULL) {
405 warnx("bad ISO address");
406 return (0);
407 }
408 bzero((caddr_t)&isoaddr, sizeof (isoaddr));
409 bcopy((caddr_t)isop, (caddr_t)&isoaddr.siso_addr,
410 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 bcopy((caddr_t)&isoport, TSEL(&isoaddr), 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 bcopy(hp->h_addr, (caddr_t)&saddr.sin_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 bcopy(hp->h_addr, (caddr_t)&saddr.sin_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, IPPROTO_UDP)) == 0) {
457 if ((opflags & ISBGRND) == 0)
458 clnt_pcreateerror("NFS Portmap");
459 } else {
460 saddr.sin_port = 0;
461 pertry.tv_sec = 10;
462 pertry.tv_usec = 0;
463 if ((clp = clntudp_create(&saddr, RPCPROG_MNT,
464 RPCMNT_VER1, pertry, &so)) == NULL) {
465 if ((opflags & ISBGRND) == 0)
466 clnt_pcreateerror("Cannot MNT PRC");
467 } else {
468 clp->cl_auth = authunix_create_default();
469 try.tv_sec = 10;
470 try.tv_usec = 0;
471 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
472 xdr_dir, spec, xdr_fh, &nfhret, try);
473 if (clnt_stat != RPC_SUCCESS) {
474 if ((opflags & ISBGRND) == 0)
475 warnx("%s", clnt_sperror(clp,
476 "bad MNT RPC"));
477 } else {
478 auth_destroy(clp->cl_auth);
479 clnt_destroy(clp);
480 retrycnt = 0;
481 }
482 }
483 }
484 if (--retrycnt > 0) {
485 if (opflags & BGRND) {
486 opflags &= ~BGRND;
487 if (i = fork()) {
488 if (i == -1)
489 err(1, "nqnfs 2");
490 exit(0);
491 }
492 (void) setsid();
493 (void) close(STDIN_FILENO);
494 (void) close(STDOUT_FILENO);
495 (void) close(STDERR_FILENO);
496 (void) chdir("/");
497 opflags |= ISBGRND;
498 }
499 sleep(60);
500 }
501 }
502 if (nfhret.stat) {
503 if (opflags & ISBGRND)
504 exit(1);
505 warn("can't access %s", spec);
506 return (0);
507 }
508 saddr.sin_port = htons(tport);
509 #ifdef ISO
510 if (isoflag) {
511 nfsargsp->addr = (struct sockaddr *) &isoaddr;
512 nfsargsp->addrlen = sizeof (isoaddr);
513 } else
514 #endif /* ISO */
515 {
516 nfsargsp->addr = (struct sockaddr *) &saddr;
517 nfsargsp->addrlen = sizeof (saddr);
518 }
519 nfsargsp->fh = &nfhret.nfh;
520 nfsargsp->hostname = nam;
521 return (1);
522 }
523
524 /*
525 * xdr routines for mount rpc's
526 */
527 int
528 xdr_dir(xdrsp, dirp)
529 XDR *xdrsp;
530 char *dirp;
531 {
532 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
533 }
534
535 int
536 xdr_fh(xdrsp, np)
537 XDR *xdrsp;
538 struct nfhret *np;
539 {
540 if (!xdr_u_long(xdrsp, &(np->stat)))
541 return (0);
542 if (np->stat)
543 return (1);
544 return (xdr_opaque(xdrsp, (caddr_t)&(np->nfh), NFSX_FH));
545 }
546
547 __dead void
548 usage()
549 {
550 (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n",
551 "[-bcdiKklMPqsT] [-a maxreadahead] [-D deadthresh]",
552 "\t[-g maxgroups] [-L leaseterm] [-m realm] [-o options] [-R retrycnt]",
553 "\t[-r readsize] [-t timeout] [-w writesize] [-x retrans]",
554 "\trhost:path node");
555 exit(1);
556 }
557