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