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