Deleted Added
full compact
mount_nfs.c (14024) mount_nfs.c (15770)
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
38static 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
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
38static 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/*
44static char sccsid[] = "@(#)mount_nfs.c 8.3 (Berkeley) 3/27/94";
45static char sccsid[] = "@(#)mount_nfs.c 8.3 (Berkeley) 3/27/94";
46*/
47static const char rcsid[] =
48 "$Id$";
45#endif /* not lint */
46
47#include <sys/param.h>
48#include <sys/mount.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/stat.h>
52#include <sys/syslog.h>
53
54#include <rpc/rpc.h>
55#include <rpc/pmap_clnt.h>
56#include <rpc/pmap_prot.h>
57
58#ifdef ISO
59#include <netiso/iso.h>
60#endif
61
62#ifdef NFSKERB
63#include <des.h>
64#include <kerberosIV/krb.h>
65#endif
66
67#include <nfs/rpcv2.h>
68#include <nfs/nfsproto.h>
69#define _KERNEL
70#include <nfs/nfs.h>
71#undef _KERNEL
72#include <nfs/nqnfs.h>
73
74#include <arpa/inet.h>
75
76#include <ctype.h>
77#include <err.h>
78#include <errno.h>
79#include <fcntl.h>
80#include <netdb.h>
81#include <signal.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <strings.h>
49#endif /* not lint */
50
51#include <sys/param.h>
52#include <sys/mount.h>
53#include <sys/socket.h>
54#include <sys/socketvar.h>
55#include <sys/stat.h>
56#include <sys/syslog.h>
57
58#include <rpc/rpc.h>
59#include <rpc/pmap_clnt.h>
60#include <rpc/pmap_prot.h>
61
62#ifdef ISO
63#include <netiso/iso.h>
64#endif
65
66#ifdef NFSKERB
67#include <des.h>
68#include <kerberosIV/krb.h>
69#endif
70
71#include <nfs/rpcv2.h>
72#include <nfs/nfsproto.h>
73#define _KERNEL
74#include <nfs/nfs.h>
75#undef _KERNEL
76#include <nfs/nqnfs.h>
77
78#include <arpa/inet.h>
79
80#include <ctype.h>
81#include <err.h>
82#include <errno.h>
83#include <fcntl.h>
84#include <netdb.h>
85#include <signal.h>
86#include <stdio.h>
87#include <stdlib.h>
88#include <strings.h>
89#include <sysexits.h>
85#include <unistd.h>
86
87#include "mntopts.h"
88
89#ifdef __FreeBSD__
90#define ALTF_BG 0x1
91#define ALTF_NOCONN 0x2
92#define ALTF_DUMBTIMR 0x4
93#define ALTF_INTR 0x8
94#define ALTF_KERB 0x10
95#define ALTF_NFSV3 0x20
96#define ALTF_RDIRPLUS 0x40
97#define ALTF_MNTUDP 0x80
98#define ALTF_RESVPORT 0x100
99#define ALTF_SEQPACKET 0x200
100#define ALTF_NQNFS 0x400
101#define ALTF_SOFT 0x800
102#define ALTF_TCP 0x1000
103#define ALTF_PORT 0x2000
104
105struct mntopt mopts[] = {
106 MOPT_STDOPTS,
107 MOPT_FORCE,
108 MOPT_UPDATE,
109 { "bg", 0, ALTF_BG, 1 },
110 { "conn", 1, ALTF_NOCONN, 1 },
111 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
112 { "intr", 0, ALTF_INTR, 1 },
113#ifdef NFSKERB
114 { "kerb", 0, ALTF_KERB, 1 },
115#endif
116 { "nfsv3", 0, ALTF_NFSV3, 1 },
117 { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
118 { "mntudp", 0, ALTF_MNTUDP, 1 },
119 { "resvport", 0, ALTF_RESVPORT, 1 },
120#ifdef ISO
121 { "seqpacket", 0, ALTF_SEQPACKET, 1 },
122#endif
123 { "nqnfs", 0, ALTF_NQNFS, 1 },
124 { "soft", 0, ALTF_SOFT, 1 },
125 { "tcp", 0, ALTF_TCP, 1 },
126 { "port=", 0, ALTF_PORT, 1 },
127 { NULL }
128};
129#else
130struct mntopt mopts[] = {
131 MOPT_STDOPTS,
132 MOPT_FORCE,
133 MOPT_UPDATE,
134 { NULL }
135};
136#endif
137
138struct nfs_args nfsdefargs = {
139 (struct sockaddr *)0,
140 sizeof (struct sockaddr_in),
141 SOCK_DGRAM,
142 0,
143 (u_char *)0,
144 0,
145 0,
146 NFS_WSIZE,
147 NFS_RSIZE,
148 NFS_READDIRSIZE,
149 10,
150 NFS_RETRANS,
151 NFS_MAXGRPS,
152 NFS_DEFRAHEAD,
153 NQ_DEFLEASE,
154 NQ_DEADTHRESH,
155 (char *)0,
156};
157
158struct nfhret {
159 u_long stat;
160 long vers;
161 long auth;
162 long fhsize;
163 u_char nfh[NFSX_V3FHMAX];
164};
165#define DEF_RETRY 10000
166#define BGRND 1
167#define ISBGRND 2
168int retrycnt = DEF_RETRY;
169int opflags = 0;
170int nfsproto = IPPROTO_UDP;
171int mnttcp_ok = 1;
172u_short port_no = 0;
173
174#ifdef NFSKERB
175char inst[INST_SZ];
176char realm[REALM_SZ];
177struct {
178 u_long kind;
179 KTEXT_ST kt;
180} ktick;
181struct nfsrpc_nickverf kverf;
182struct nfsrpc_fullblock kin, kout;
183NFSKERBKEY_T kivec;
184CREDENTIALS kcr;
185struct timeval ktv;
186NFSKERBKEYSCHED_T kerb_keysched;
187#endif
188
189int getnfsargs __P((char *, struct nfs_args *));
190#ifdef ISO
191struct iso_addr *iso_addr __P((const char *));
192#endif
193void set_rpc_maxgrouplist __P((int));
194__dead void usage __P((void));
195int xdr_dir __P((XDR *, char *));
196int xdr_fh __P((XDR *, struct nfhret *));
197
198int
199main(argc, argv)
200 int argc;
201 char *argv[];
202{
203 register int c;
204 register struct nfs_args *nfsargsp;
205 struct nfs_args nfsargs;
206 struct nfsd_cargs ncd;
207 int mntflags, altflags, i, nfssvc_flag, num;
208 char *name, *p, *spec;
209 struct vfsconf *vfc;
210#ifdef NFSKERB
211 uid_t last_ruid;
212
213 last_ruid = -1;
214 (void)strcpy(realm, KRB_REALM);
215 if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
216 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
217 ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
218 ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
219 fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
220#endif /* NFSKERB */
221 retrycnt = DEF_RETRY;
222
223 mntflags = 0;
224 altflags = 0;
225 nfsargs = nfsdefargs;
226 nfsargsp = &nfsargs;
227 while ((c = getopt(argc, argv,
228 "3a:bcdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:U")) != EOF)
229 switch (c) {
230 case '3':
231 nfsargsp->flags |= NFSMNT_NFSV3;
232 break;
233 case 'a':
234 num = strtol(optarg, &p, 10);
235 if (*p || num < 0)
236 errx(1, "illegal -a value -- %s", optarg);
237 nfsargsp->readahead = num;
238 nfsargsp->flags |= NFSMNT_READAHEAD;
239 break;
240 case 'b':
241 opflags |= BGRND;
242 break;
243 case 'c':
244 nfsargsp->flags |= NFSMNT_NOCONN;
245 break;
246 case 'D':
247 num = strtol(optarg, &p, 10);
248 if (*p || num <= 0)
249 errx(1, "illegal -D value -- %s", optarg);
250 nfsargsp->deadthresh = num;
251 nfsargsp->flags |= NFSMNT_DEADTHRESH;
252 break;
253 case 'd':
254 nfsargsp->flags |= NFSMNT_DUMBTIMR;
255 break;
256 case 'g':
257 num = strtol(optarg, &p, 10);
258 if (*p || num <= 0)
259 errx(1, "illegal -g value -- %s", optarg);
260#ifdef __FreeBSD__
261 set_rpc_maxgrouplist(num);
262#endif
263 nfsargsp->maxgrouplist = num;
264 nfsargsp->flags |= NFSMNT_MAXGRPS;
265 break;
266 case 'I':
267 num = strtol(optarg, &p, 10);
268 if (*p || num <= 0)
269 errx(1, "illegal -I value -- %s", optarg);
270 nfsargsp->readdirsize = num;
271 nfsargsp->flags |= NFSMNT_READDIRSIZE;
272 break;
273 case 'i':
274 nfsargsp->flags |= NFSMNT_INT;
275 break;
276#ifdef NFSKERB
277 case 'K':
278 nfsargsp->flags |= NFSMNT_KERB;
279 break;
280#endif
281 case 'L':
282 num = strtol(optarg, &p, 10);
283 if (*p || num < 2)
284 errx(1, "illegal -L value -- %s", optarg);
285 nfsargsp->leaseterm = num;
286 nfsargsp->flags |= NFSMNT_LEASETERM;
287 break;
288 case 'l':
289 nfsargsp->flags |= NFSMNT_RDIRPLUS;
290 break;
291#ifdef NFSKERB
292 case 'm':
293 (void)strncpy(realm, optarg, REALM_SZ - 1);
294 realm[REALM_SZ - 1] = '\0';
295 break;
296#endif
297 case 'o':
298#ifdef __FreeBSD__
299 getmntopts(optarg, mopts, &mntflags, &altflags);
300 if(altflags & ALTF_BG)
301 opflags |= BGRND;
302 if(altflags & ALTF_NOCONN)
303 nfsargsp->flags |= NFSMNT_NOCONN;
304 if(altflags & ALTF_DUMBTIMR)
305 nfsargsp->flags |= NFSMNT_DUMBTIMR;
306 if(altflags & ALTF_INTR)
307 nfsargsp->flags |= NFSMNT_INT;
308#ifdef NFSKERB
309 if(altflags & ALTF_KERB)
310 nfsargsp->flags |= NFSMNT_KERB;
311#endif
312 if(altflags & ALTF_NFSV3)
313 nfsargsp->flags |= NFSMNT_NFSV3;
314 if(altflags & ALTF_RDIRPLUS)
315 nfsargsp->flags |= NFSMNT_RDIRPLUS;
316 if(altflags & ALTF_MNTUDP)
317 mnttcp_ok = 0;
318 if(altflags & ALTF_RESVPORT)
319 nfsargsp->flags |= NFSMNT_RESVPORT;
320#ifdef ISO
321 if(altflags & ALTF_SEQPACKET)
322 nfsargsp->sotype = SOCK_SEQPACKET;
323#endif
324 if(altflags & ALTF_NQNFS)
325 nfsargsp->flags |= (NFSMNT_NQNFS|NFSMNT_NFSV3);
326 if(altflags & ALTF_SOFT)
327 nfsargsp->flags |= NFSMNT_SOFT;
328 if(altflags & ALTF_TCP) {
329 nfsargsp->sotype = SOCK_STREAM;
330 nfsproto = IPPROTO_TCP;
331 }
332 if(altflags & ALTF_PORT)
333 port_no = atoi(strstr(optarg, "port=") + 5);
334 altflags = 0;
335#else
336 getmntopts(optarg, mopts, &mntflags);
337#endif
338 break;
339 case 'P':
340 nfsargsp->flags |= NFSMNT_RESVPORT;
341 break;
342#ifdef ISO
343 case 'p':
344 nfsargsp->sotype = SOCK_SEQPACKET;
345 break;
346#endif
347 case 'q':
348 nfsargsp->flags |= (NFSMNT_NQNFS | NFSMNT_NFSV3);
349 break;
350 case 'R':
351 num = strtol(optarg, &p, 10);
352 if (*p || num <= 0)
353 errx(1, "illegal -R value -- %s", optarg);
354 retrycnt = num;
355 break;
356 case 'r':
357 num = strtol(optarg, &p, 10);
358 if (*p || num <= 0)
359 errx(1, "illegal -r value -- %s", optarg);
360 nfsargsp->rsize = num;
361 nfsargsp->flags |= NFSMNT_RSIZE;
362 break;
363 case 's':
364 nfsargsp->flags |= NFSMNT_SOFT;
365 break;
366 case 'T':
367 nfsargsp->sotype = SOCK_STREAM;
368 nfsproto = IPPROTO_TCP;
369 break;
370 case 't':
371 num = strtol(optarg, &p, 10);
372 if (*p || num <= 0)
373 errx(1, "illegal -t value -- %s", optarg);
374 nfsargsp->timeo = num;
375 nfsargsp->flags |= NFSMNT_TIMEO;
376 break;
377 case 'w':
378 num = strtol(optarg, &p, 10);
379 if (*p || num <= 0)
380 errx(1, "illegal -w value -- %s", optarg);
381 nfsargsp->wsize = num;
382 nfsargsp->flags |= NFSMNT_WSIZE;
383 break;
384 case 'x':
385 num = strtol(optarg, &p, 10);
386 if (*p || num <= 0)
387 errx(1, "illegal -x value -- %s", optarg);
388 nfsargsp->retrans = num;
389 nfsargsp->flags |= NFSMNT_RETRANS;
390 break;
391 case 'U':
392 mnttcp_ok = 0;
393 break;
394 default:
395 usage();
396 break;
397 }
398 argc -= optind;
399 argv += optind;
400
401 if (argc != 2)
402 usage();
403
404 spec = *argv++;
405 name = *argv;
406
407 if (!getnfsargs(spec, nfsargsp))
408 exit(1);
409
410#ifdef __FreeBSD__
411 vfc = getvfsbyname("nfs");
412 if(!vfc && vfsisloadable("nfs")) {
413 if(vfsload("nfs"))
90#include <unistd.h>
91
92#include "mntopts.h"
93
94#ifdef __FreeBSD__
95#define ALTF_BG 0x1
96#define ALTF_NOCONN 0x2
97#define ALTF_DUMBTIMR 0x4
98#define ALTF_INTR 0x8
99#define ALTF_KERB 0x10
100#define ALTF_NFSV3 0x20
101#define ALTF_RDIRPLUS 0x40
102#define ALTF_MNTUDP 0x80
103#define ALTF_RESVPORT 0x100
104#define ALTF_SEQPACKET 0x200
105#define ALTF_NQNFS 0x400
106#define ALTF_SOFT 0x800
107#define ALTF_TCP 0x1000
108#define ALTF_PORT 0x2000
109
110struct mntopt mopts[] = {
111 MOPT_STDOPTS,
112 MOPT_FORCE,
113 MOPT_UPDATE,
114 { "bg", 0, ALTF_BG, 1 },
115 { "conn", 1, ALTF_NOCONN, 1 },
116 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
117 { "intr", 0, ALTF_INTR, 1 },
118#ifdef NFSKERB
119 { "kerb", 0, ALTF_KERB, 1 },
120#endif
121 { "nfsv3", 0, ALTF_NFSV3, 1 },
122 { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
123 { "mntudp", 0, ALTF_MNTUDP, 1 },
124 { "resvport", 0, ALTF_RESVPORT, 1 },
125#ifdef ISO
126 { "seqpacket", 0, ALTF_SEQPACKET, 1 },
127#endif
128 { "nqnfs", 0, ALTF_NQNFS, 1 },
129 { "soft", 0, ALTF_SOFT, 1 },
130 { "tcp", 0, ALTF_TCP, 1 },
131 { "port=", 0, ALTF_PORT, 1 },
132 { NULL }
133};
134#else
135struct mntopt mopts[] = {
136 MOPT_STDOPTS,
137 MOPT_FORCE,
138 MOPT_UPDATE,
139 { NULL }
140};
141#endif
142
143struct nfs_args nfsdefargs = {
144 (struct sockaddr *)0,
145 sizeof (struct sockaddr_in),
146 SOCK_DGRAM,
147 0,
148 (u_char *)0,
149 0,
150 0,
151 NFS_WSIZE,
152 NFS_RSIZE,
153 NFS_READDIRSIZE,
154 10,
155 NFS_RETRANS,
156 NFS_MAXGRPS,
157 NFS_DEFRAHEAD,
158 NQ_DEFLEASE,
159 NQ_DEADTHRESH,
160 (char *)0,
161};
162
163struct nfhret {
164 u_long stat;
165 long vers;
166 long auth;
167 long fhsize;
168 u_char nfh[NFSX_V3FHMAX];
169};
170#define DEF_RETRY 10000
171#define BGRND 1
172#define ISBGRND 2
173int retrycnt = DEF_RETRY;
174int opflags = 0;
175int nfsproto = IPPROTO_UDP;
176int mnttcp_ok = 1;
177u_short port_no = 0;
178
179#ifdef NFSKERB
180char inst[INST_SZ];
181char realm[REALM_SZ];
182struct {
183 u_long kind;
184 KTEXT_ST kt;
185} ktick;
186struct nfsrpc_nickverf kverf;
187struct nfsrpc_fullblock kin, kout;
188NFSKERBKEY_T kivec;
189CREDENTIALS kcr;
190struct timeval ktv;
191NFSKERBKEYSCHED_T kerb_keysched;
192#endif
193
194int getnfsargs __P((char *, struct nfs_args *));
195#ifdef ISO
196struct iso_addr *iso_addr __P((const char *));
197#endif
198void set_rpc_maxgrouplist __P((int));
199__dead void usage __P((void));
200int xdr_dir __P((XDR *, char *));
201int xdr_fh __P((XDR *, struct nfhret *));
202
203int
204main(argc, argv)
205 int argc;
206 char *argv[];
207{
208 register int c;
209 register struct nfs_args *nfsargsp;
210 struct nfs_args nfsargs;
211 struct nfsd_cargs ncd;
212 int mntflags, altflags, i, nfssvc_flag, num;
213 char *name, *p, *spec;
214 struct vfsconf *vfc;
215#ifdef NFSKERB
216 uid_t last_ruid;
217
218 last_ruid = -1;
219 (void)strcpy(realm, KRB_REALM);
220 if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
221 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
222 ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
223 ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
224 fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
225#endif /* NFSKERB */
226 retrycnt = DEF_RETRY;
227
228 mntflags = 0;
229 altflags = 0;
230 nfsargs = nfsdefargs;
231 nfsargsp = &nfsargs;
232 while ((c = getopt(argc, argv,
233 "3a:bcdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:U")) != EOF)
234 switch (c) {
235 case '3':
236 nfsargsp->flags |= NFSMNT_NFSV3;
237 break;
238 case 'a':
239 num = strtol(optarg, &p, 10);
240 if (*p || num < 0)
241 errx(1, "illegal -a value -- %s", optarg);
242 nfsargsp->readahead = num;
243 nfsargsp->flags |= NFSMNT_READAHEAD;
244 break;
245 case 'b':
246 opflags |= BGRND;
247 break;
248 case 'c':
249 nfsargsp->flags |= NFSMNT_NOCONN;
250 break;
251 case 'D':
252 num = strtol(optarg, &p, 10);
253 if (*p || num <= 0)
254 errx(1, "illegal -D value -- %s", optarg);
255 nfsargsp->deadthresh = num;
256 nfsargsp->flags |= NFSMNT_DEADTHRESH;
257 break;
258 case 'd':
259 nfsargsp->flags |= NFSMNT_DUMBTIMR;
260 break;
261 case 'g':
262 num = strtol(optarg, &p, 10);
263 if (*p || num <= 0)
264 errx(1, "illegal -g value -- %s", optarg);
265#ifdef __FreeBSD__
266 set_rpc_maxgrouplist(num);
267#endif
268 nfsargsp->maxgrouplist = num;
269 nfsargsp->flags |= NFSMNT_MAXGRPS;
270 break;
271 case 'I':
272 num = strtol(optarg, &p, 10);
273 if (*p || num <= 0)
274 errx(1, "illegal -I value -- %s", optarg);
275 nfsargsp->readdirsize = num;
276 nfsargsp->flags |= NFSMNT_READDIRSIZE;
277 break;
278 case 'i':
279 nfsargsp->flags |= NFSMNT_INT;
280 break;
281#ifdef NFSKERB
282 case 'K':
283 nfsargsp->flags |= NFSMNT_KERB;
284 break;
285#endif
286 case 'L':
287 num = strtol(optarg, &p, 10);
288 if (*p || num < 2)
289 errx(1, "illegal -L value -- %s", optarg);
290 nfsargsp->leaseterm = num;
291 nfsargsp->flags |= NFSMNT_LEASETERM;
292 break;
293 case 'l':
294 nfsargsp->flags |= NFSMNT_RDIRPLUS;
295 break;
296#ifdef NFSKERB
297 case 'm':
298 (void)strncpy(realm, optarg, REALM_SZ - 1);
299 realm[REALM_SZ - 1] = '\0';
300 break;
301#endif
302 case 'o':
303#ifdef __FreeBSD__
304 getmntopts(optarg, mopts, &mntflags, &altflags);
305 if(altflags & ALTF_BG)
306 opflags |= BGRND;
307 if(altflags & ALTF_NOCONN)
308 nfsargsp->flags |= NFSMNT_NOCONN;
309 if(altflags & ALTF_DUMBTIMR)
310 nfsargsp->flags |= NFSMNT_DUMBTIMR;
311 if(altflags & ALTF_INTR)
312 nfsargsp->flags |= NFSMNT_INT;
313#ifdef NFSKERB
314 if(altflags & ALTF_KERB)
315 nfsargsp->flags |= NFSMNT_KERB;
316#endif
317 if(altflags & ALTF_NFSV3)
318 nfsargsp->flags |= NFSMNT_NFSV3;
319 if(altflags & ALTF_RDIRPLUS)
320 nfsargsp->flags |= NFSMNT_RDIRPLUS;
321 if(altflags & ALTF_MNTUDP)
322 mnttcp_ok = 0;
323 if(altflags & ALTF_RESVPORT)
324 nfsargsp->flags |= NFSMNT_RESVPORT;
325#ifdef ISO
326 if(altflags & ALTF_SEQPACKET)
327 nfsargsp->sotype = SOCK_SEQPACKET;
328#endif
329 if(altflags & ALTF_NQNFS)
330 nfsargsp->flags |= (NFSMNT_NQNFS|NFSMNT_NFSV3);
331 if(altflags & ALTF_SOFT)
332 nfsargsp->flags |= NFSMNT_SOFT;
333 if(altflags & ALTF_TCP) {
334 nfsargsp->sotype = SOCK_STREAM;
335 nfsproto = IPPROTO_TCP;
336 }
337 if(altflags & ALTF_PORT)
338 port_no = atoi(strstr(optarg, "port=") + 5);
339 altflags = 0;
340#else
341 getmntopts(optarg, mopts, &mntflags);
342#endif
343 break;
344 case 'P':
345 nfsargsp->flags |= NFSMNT_RESVPORT;
346 break;
347#ifdef ISO
348 case 'p':
349 nfsargsp->sotype = SOCK_SEQPACKET;
350 break;
351#endif
352 case 'q':
353 nfsargsp->flags |= (NFSMNT_NQNFS | NFSMNT_NFSV3);
354 break;
355 case 'R':
356 num = strtol(optarg, &p, 10);
357 if (*p || num <= 0)
358 errx(1, "illegal -R value -- %s", optarg);
359 retrycnt = num;
360 break;
361 case 'r':
362 num = strtol(optarg, &p, 10);
363 if (*p || num <= 0)
364 errx(1, "illegal -r value -- %s", optarg);
365 nfsargsp->rsize = num;
366 nfsargsp->flags |= NFSMNT_RSIZE;
367 break;
368 case 's':
369 nfsargsp->flags |= NFSMNT_SOFT;
370 break;
371 case 'T':
372 nfsargsp->sotype = SOCK_STREAM;
373 nfsproto = IPPROTO_TCP;
374 break;
375 case 't':
376 num = strtol(optarg, &p, 10);
377 if (*p || num <= 0)
378 errx(1, "illegal -t value -- %s", optarg);
379 nfsargsp->timeo = num;
380 nfsargsp->flags |= NFSMNT_TIMEO;
381 break;
382 case 'w':
383 num = strtol(optarg, &p, 10);
384 if (*p || num <= 0)
385 errx(1, "illegal -w value -- %s", optarg);
386 nfsargsp->wsize = num;
387 nfsargsp->flags |= NFSMNT_WSIZE;
388 break;
389 case 'x':
390 num = strtol(optarg, &p, 10);
391 if (*p || num <= 0)
392 errx(1, "illegal -x value -- %s", optarg);
393 nfsargsp->retrans = num;
394 nfsargsp->flags |= NFSMNT_RETRANS;
395 break;
396 case 'U':
397 mnttcp_ok = 0;
398 break;
399 default:
400 usage();
401 break;
402 }
403 argc -= optind;
404 argv += optind;
405
406 if (argc != 2)
407 usage();
408
409 spec = *argv++;
410 name = *argv;
411
412 if (!getnfsargs(spec, nfsargsp))
413 exit(1);
414
415#ifdef __FreeBSD__
416 vfc = getvfsbyname("nfs");
417 if(!vfc && vfsisloadable("nfs")) {
418 if(vfsload("nfs"))
414 err(1, "vfsload(nfs)");
419 err(EX_OSERR, "vfsload(nfs)");
415 endvfsent(); /* flush cache */
416 vfc = getvfsbyname("nfs");
417 }
420 endvfsent(); /* flush cache */
421 vfc = getvfsbyname("nfs");
422 }
423 if (!vfc)
424 errx(EX_OSERR, "nfs filesystem is not loadable");
418
425
419 if (mount(vfc ? vfc->vfc_index : MOUNT_NFS, name, mntflags, nfsargsp))
426 if (mount(vfc->vfc_index, name, mntflags, nfsargsp))
420#else
421 if (mount(MOUNT_NFS, name, mntflags, nfsargsp))
422#endif
423 err(1, "%s", name);
424 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
425 if ((opflags & ISBGRND) == 0) {
426 if (i = fork()) {
427 if (i == -1)
428 err(1, "nqnfs 1");
429 exit(0);
430 }
431 (void) setsid();
432 (void) close(STDIN_FILENO);
433 (void) close(STDOUT_FILENO);
434 (void) close(STDERR_FILENO);
435 (void) chdir("/");
436 }
437 openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
438 nfssvc_flag = NFSSVC_MNTD;
439 ncd.ncd_dirp = name;
440 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
441 if (errno != ENEEDAUTH) {
442 syslog(LOG_ERR, "nfssvc err %m");
443 continue;
444 }
445 nfssvc_flag =
446 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
447#ifdef NFSKERB
448 /*
449 * Set up as ncd_authuid for the kerberos call.
450 * Must set ruid to ncd_authuid and reset the
451 * ticket name iff ncd_authuid is not the same
452 * as last time, so that the right ticket file
453 * is found.
454 * Get the Kerberos credential structure so that
455 * we have the seesion key and get a ticket for
456 * this uid.
457 * For more info see the IETF Draft "Authentication
458 * in ONC RPC".
459 */
460 if (ncd.ncd_authuid != last_ruid) {
461 char buf[512];
462 (void)sprintf(buf, "%s%d",
463 TKT_ROOT, ncd.ncd_authuid);
464 krb_set_tkt_string(buf);
465 last_ruid = ncd.ncd_authuid;
466 }
467 setreuid(ncd.ncd_authuid, 0);
468 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
469 if (kret == RET_NOTKT) {
470 kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
471 DEFAULT_TKT_LIFE);
472 if (kret == KSUCCESS)
473 kret = krb_get_cred(NFS_KERBSRV, inst, realm,
474 &kcr);
475 }
476 if (kret == KSUCCESS)
477 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
478 realm, 0);
479
480 /*
481 * Fill in the AKN_FULLNAME authenticator and verfier.
482 * Along with the Kerberos ticket, we need to build
483 * the timestamp verifier and encrypt it in CBC mode.
484 */
485 if (kret == KSUCCESS &&
486 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
487 && gettimeofday(&ktv, (struct timezone *)0) == 0) {
488 ncd.ncd_authtype = RPCAUTH_KERB4;
489 ncd.ncd_authstr = (u_char *)&ktick;
490 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
491 3 * NFSX_UNSIGNED;
492 ncd.ncd_verfstr = (u_char *)&kverf;
493 ncd.ncd_verflen = sizeof (kverf);
494 bcopy((caddr_t)kcr.session, (caddr_t)ncd.ncd_key,
495 sizeof (kcr.session));
496 kin.t1 = htonl(ktv.tv_sec);
497 kin.t2 = htonl(ktv.tv_usec);
498 kin.w1 = htonl(NFS_KERBTTL);
499 kin.w2 = htonl(NFS_KERBTTL - 1);
500 bzero((caddr_t)kivec, sizeof (kivec));
501
502 /*
503 * Encrypt kin in CBC mode using the session
504 * key in kcr.
505 */
506 XXX
507
508 /*
509 * Finally, fill the timestamp verifier into the
510 * authenticator and verifier.
511 */
512 ktick.kind = htonl(RPCAKN_FULLNAME);
513 kverf.kind = htonl(RPCAKN_FULLNAME);
514 NFS_KERBW1(ktick.kt) = kout.w1;
515 ktick.kt.length = htonl(ktick.kt.length);
516 kverf.verf.t1 = kout.t1;
517 kverf.verf.t2 = kout.t2;
518 kverf.verf.w2 = kout.w2;
519 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
520 }
521 setreuid(0, 0);
522#endif /* NFSKERB */
523 }
524 }
525 exit(0);
526}
527
528int
529getnfsargs(spec, nfsargsp)
530 char *spec;
531 struct nfs_args *nfsargsp;
532{
533 register CLIENT *clp;
534 struct hostent *hp;
535 static struct sockaddr_in saddr;
536#ifdef ISO
537 static struct sockaddr_iso isoaddr;
538 struct iso_addr *isop;
539 int isoflag = 0;
540#endif
541 struct timeval pertry, try;
542 enum clnt_stat clnt_stat;
543 int so = RPC_ANYSOCK, i, nfsvers, mntvers;
544 char *hostp, *delimp;
545#ifdef NFSKERB
546 char *cp;
547#endif
548 u_short tport;
549 static struct nfhret nfhret;
550 static char nam[MNAMELEN + 1];
551
552 strncpy(nam, spec, MNAMELEN);
553 nam[MNAMELEN] = '\0';
554 if ((delimp = strchr(spec, '@')) != NULL) {
555 hostp = delimp + 1;
556 } else if ((delimp = strchr(spec, ':')) != NULL) {
557 hostp = spec;
558 spec = delimp + 1;
559 } else {
560 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
561 return (0);
562 }
563 *delimp = '\0';
564 /*
565 * DUMB!! Until the mount protocol works on iso transport, we must
566 * supply both an iso and an inet address for the host.
567 */
568#ifdef ISO
569 if (!strncmp(hostp, "iso=", 4)) {
570 u_short isoport;
571
572 hostp += 4;
573 isoflag++;
574 if ((delimp = strchr(hostp, '+')) == NULL) {
575 warnx("no iso+inet address");
576 return (0);
577 }
578 *delimp = '\0';
579 if ((isop = iso_addr(hostp)) == NULL) {
580 warnx("bad ISO address");
581 return (0);
582 }
583 bzero((caddr_t)&isoaddr, sizeof (isoaddr));
584 bcopy((caddr_t)isop, (caddr_t)&isoaddr.siso_addr,
585 sizeof (struct iso_addr));
586 isoaddr.siso_len = sizeof (isoaddr);
587 isoaddr.siso_family = AF_ISO;
588 isoaddr.siso_tlen = 2;
589 isoport = htons(NFS_PORT);
590 bcopy((caddr_t)&isoport, TSEL(&isoaddr), isoaddr.siso_tlen);
591 hostp = delimp + 1;
592 }
593#endif /* ISO */
594
595 /*
596 * Handle an internet host address and reverse resolve it if
597 * doing Kerberos.
598 */
599 if (isdigit(*hostp)) {
600 if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) {
601 warnx("bad net address %s", hostp);
602 return (0);
603 }
604 } else if ((hp = gethostbyname(hostp)) != NULL) {
605 bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
606 } else {
607 warnx("can't get net id for host");
608 return (0);
609 }
610#ifdef NFSKERB
611 if ((nfsargsp->flags & NFSMNT_KERB)) {
612 if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
613 sizeof (u_long), AF_INET)) == (struct hostent *)0) {
614 warnx("can't reverse resolve net address");
615 return (0);
616 }
617 bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
618 strncpy(inst, hp->h_name, INST_SZ);
619 inst[INST_SZ - 1] = '\0';
620 if (cp = strchr(inst, '.'))
621 *cp = '\0';
622 }
623#endif /* NFSKERB */
624
625 if (nfsargsp->flags & NFSMNT_NFSV3) {
626 nfsvers = 3;
627 mntvers = 3;
628 } else {
629 nfsvers = 2;
630 mntvers = 1;
631 }
632 nfhret.stat = EACCES; /* Mark not yet successful */
633 while (retrycnt > 0) {
634 saddr.sin_family = AF_INET;
635 saddr.sin_port = htons(PMAPPORT);
636 if ((tport = port_no ? port_no :
637 pmap_getport(&saddr, RPCPROG_NFS,
638 nfsvers, nfsproto)) == 0) {
639 if ((opflags & ISBGRND) == 0)
640 clnt_pcreateerror("NFS Portmap");
641 } else {
642 saddr.sin_port = 0;
643 pertry.tv_sec = 10;
644 pertry.tv_usec = 0;
645 if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
646 clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
647 &so, 0, 0);
648 else
649 clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
650 pertry, &so);
651 if (clp == NULL) {
652 if ((opflags & ISBGRND) == 0)
653 clnt_pcreateerror("Cannot MNT RPC");
654 } else {
655 clp->cl_auth = authunix_create_default();
656 try.tv_sec = 10;
657 try.tv_usec = 0;
658 if (nfsargsp->flags & NFSMNT_KERB)
659 nfhret.auth = RPCAUTH_KERB4;
660 else
661 nfhret.auth = RPCAUTH_UNIX;
662 nfhret.vers = mntvers;
663 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
664 xdr_dir, spec, xdr_fh, &nfhret, try);
665 if (clnt_stat != RPC_SUCCESS) {
666 if ((opflags & ISBGRND) == 0)
667 warnx("%s", clnt_sperror(clp,
668 "bad MNT RPC"));
669 } else {
670 auth_destroy(clp->cl_auth);
671 clnt_destroy(clp);
672 retrycnt = 0;
673 }
674 }
675 }
676 if (--retrycnt > 0) {
677 if (opflags & BGRND) {
678 opflags &= ~BGRND;
679 if (i = fork()) {
680 if (i == -1)
681 err(1, "nqnfs 2");
682 exit(0);
683 }
684 (void) setsid();
685 (void) close(STDIN_FILENO);
686 (void) close(STDOUT_FILENO);
687 (void) close(STDERR_FILENO);
688 (void) chdir("/");
689 opflags |= ISBGRND;
690 }
691 sleep(60);
692 }
693 }
694 if (nfhret.stat) {
695 if (opflags & ISBGRND)
696 exit(1);
697 errno = nfhret.stat;
698 warn("can't access %s", spec);
699 return (0);
700 }
701 saddr.sin_port = htons(tport);
702#ifdef ISO
703 if (isoflag) {
704 nfsargsp->addr = (struct sockaddr *) &isoaddr;
705 nfsargsp->addrlen = sizeof (isoaddr);
706 } else
707#endif /* ISO */
708 {
709 nfsargsp->addr = (struct sockaddr *) &saddr;
710 nfsargsp->addrlen = sizeof (saddr);
711 }
712 nfsargsp->fh = nfhret.nfh;
713 nfsargsp->fhsize = nfhret.fhsize;
714 nfsargsp->hostname = nam;
715 return (1);
716}
717
718/*
719 * xdr routines for mount rpc's
720 */
721int
722xdr_dir(xdrsp, dirp)
723 XDR *xdrsp;
724 char *dirp;
725{
726 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
727}
728
729int
730xdr_fh(xdrsp, np)
731 XDR *xdrsp;
732 register struct nfhret *np;
733{
734 register int i;
735 long auth, authcnt, authfnd = 0;
736
737 if (!xdr_u_long(xdrsp, &np->stat))
738 return (0);
739 if (np->stat)
740 return (1);
741 switch (np->vers) {
742 case 1:
743 np->fhsize = NFSX_V2FH;
744 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
745 case 3:
746 if (!xdr_long(xdrsp, &np->fhsize))
747 return (0);
748 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
749 return (0);
750 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
751 return (0);
752 if (!xdr_long(xdrsp, &authcnt))
753 return (0);
754 for (i = 0; i < authcnt; i++) {
755 if (!xdr_long(xdrsp, &auth))
756 return (0);
757 if (auth == np->auth)
758 authfnd++;
759 }
760
761 /*
762 * Some servers, such as DEC's OSF/1 return a nil authenticator
763 * list to indicate RPCAUTH_UNIX.
764 */
765 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
766 np->stat = EAUTH;
767 return (1);
768 };
769 return (0);
770}
771
772__dead void
773usage()
774{
775 (void)fprintf(stderr, "\
776usage: mount_nfs [-3KPTUbcdilqs] [-D deadthresh] [-I readdirsize]\n\
777 [-L leaseterm] [-R retrycnt] [-a maxreadahead] [-g maxgroups]\n\
778 [-m realm] [-o options] [-r readsize] [-t timeout] [-w writesize]\n\
779 [-x retrans] rhost:path node\n");
780 exit(1);
781}
427#else
428 if (mount(MOUNT_NFS, name, mntflags, nfsargsp))
429#endif
430 err(1, "%s", name);
431 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
432 if ((opflags & ISBGRND) == 0) {
433 if (i = fork()) {
434 if (i == -1)
435 err(1, "nqnfs 1");
436 exit(0);
437 }
438 (void) setsid();
439 (void) close(STDIN_FILENO);
440 (void) close(STDOUT_FILENO);
441 (void) close(STDERR_FILENO);
442 (void) chdir("/");
443 }
444 openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
445 nfssvc_flag = NFSSVC_MNTD;
446 ncd.ncd_dirp = name;
447 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
448 if (errno != ENEEDAUTH) {
449 syslog(LOG_ERR, "nfssvc err %m");
450 continue;
451 }
452 nfssvc_flag =
453 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
454#ifdef NFSKERB
455 /*
456 * Set up as ncd_authuid for the kerberos call.
457 * Must set ruid to ncd_authuid and reset the
458 * ticket name iff ncd_authuid is not the same
459 * as last time, so that the right ticket file
460 * is found.
461 * Get the Kerberos credential structure so that
462 * we have the seesion key and get a ticket for
463 * this uid.
464 * For more info see the IETF Draft "Authentication
465 * in ONC RPC".
466 */
467 if (ncd.ncd_authuid != last_ruid) {
468 char buf[512];
469 (void)sprintf(buf, "%s%d",
470 TKT_ROOT, ncd.ncd_authuid);
471 krb_set_tkt_string(buf);
472 last_ruid = ncd.ncd_authuid;
473 }
474 setreuid(ncd.ncd_authuid, 0);
475 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
476 if (kret == RET_NOTKT) {
477 kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
478 DEFAULT_TKT_LIFE);
479 if (kret == KSUCCESS)
480 kret = krb_get_cred(NFS_KERBSRV, inst, realm,
481 &kcr);
482 }
483 if (kret == KSUCCESS)
484 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
485 realm, 0);
486
487 /*
488 * Fill in the AKN_FULLNAME authenticator and verfier.
489 * Along with the Kerberos ticket, we need to build
490 * the timestamp verifier and encrypt it in CBC mode.
491 */
492 if (kret == KSUCCESS &&
493 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
494 && gettimeofday(&ktv, (struct timezone *)0) == 0) {
495 ncd.ncd_authtype = RPCAUTH_KERB4;
496 ncd.ncd_authstr = (u_char *)&ktick;
497 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
498 3 * NFSX_UNSIGNED;
499 ncd.ncd_verfstr = (u_char *)&kverf;
500 ncd.ncd_verflen = sizeof (kverf);
501 bcopy((caddr_t)kcr.session, (caddr_t)ncd.ncd_key,
502 sizeof (kcr.session));
503 kin.t1 = htonl(ktv.tv_sec);
504 kin.t2 = htonl(ktv.tv_usec);
505 kin.w1 = htonl(NFS_KERBTTL);
506 kin.w2 = htonl(NFS_KERBTTL - 1);
507 bzero((caddr_t)kivec, sizeof (kivec));
508
509 /*
510 * Encrypt kin in CBC mode using the session
511 * key in kcr.
512 */
513 XXX
514
515 /*
516 * Finally, fill the timestamp verifier into the
517 * authenticator and verifier.
518 */
519 ktick.kind = htonl(RPCAKN_FULLNAME);
520 kverf.kind = htonl(RPCAKN_FULLNAME);
521 NFS_KERBW1(ktick.kt) = kout.w1;
522 ktick.kt.length = htonl(ktick.kt.length);
523 kverf.verf.t1 = kout.t1;
524 kverf.verf.t2 = kout.t2;
525 kverf.verf.w2 = kout.w2;
526 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
527 }
528 setreuid(0, 0);
529#endif /* NFSKERB */
530 }
531 }
532 exit(0);
533}
534
535int
536getnfsargs(spec, nfsargsp)
537 char *spec;
538 struct nfs_args *nfsargsp;
539{
540 register CLIENT *clp;
541 struct hostent *hp;
542 static struct sockaddr_in saddr;
543#ifdef ISO
544 static struct sockaddr_iso isoaddr;
545 struct iso_addr *isop;
546 int isoflag = 0;
547#endif
548 struct timeval pertry, try;
549 enum clnt_stat clnt_stat;
550 int so = RPC_ANYSOCK, i, nfsvers, mntvers;
551 char *hostp, *delimp;
552#ifdef NFSKERB
553 char *cp;
554#endif
555 u_short tport;
556 static struct nfhret nfhret;
557 static char nam[MNAMELEN + 1];
558
559 strncpy(nam, spec, MNAMELEN);
560 nam[MNAMELEN] = '\0';
561 if ((delimp = strchr(spec, '@')) != NULL) {
562 hostp = delimp + 1;
563 } else if ((delimp = strchr(spec, ':')) != NULL) {
564 hostp = spec;
565 spec = delimp + 1;
566 } else {
567 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
568 return (0);
569 }
570 *delimp = '\0';
571 /*
572 * DUMB!! Until the mount protocol works on iso transport, we must
573 * supply both an iso and an inet address for the host.
574 */
575#ifdef ISO
576 if (!strncmp(hostp, "iso=", 4)) {
577 u_short isoport;
578
579 hostp += 4;
580 isoflag++;
581 if ((delimp = strchr(hostp, '+')) == NULL) {
582 warnx("no iso+inet address");
583 return (0);
584 }
585 *delimp = '\0';
586 if ((isop = iso_addr(hostp)) == NULL) {
587 warnx("bad ISO address");
588 return (0);
589 }
590 bzero((caddr_t)&isoaddr, sizeof (isoaddr));
591 bcopy((caddr_t)isop, (caddr_t)&isoaddr.siso_addr,
592 sizeof (struct iso_addr));
593 isoaddr.siso_len = sizeof (isoaddr);
594 isoaddr.siso_family = AF_ISO;
595 isoaddr.siso_tlen = 2;
596 isoport = htons(NFS_PORT);
597 bcopy((caddr_t)&isoport, TSEL(&isoaddr), isoaddr.siso_tlen);
598 hostp = delimp + 1;
599 }
600#endif /* ISO */
601
602 /*
603 * Handle an internet host address and reverse resolve it if
604 * doing Kerberos.
605 */
606 if (isdigit(*hostp)) {
607 if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) {
608 warnx("bad net address %s", hostp);
609 return (0);
610 }
611 } else if ((hp = gethostbyname(hostp)) != NULL) {
612 bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
613 } else {
614 warnx("can't get net id for host");
615 return (0);
616 }
617#ifdef NFSKERB
618 if ((nfsargsp->flags & NFSMNT_KERB)) {
619 if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
620 sizeof (u_long), AF_INET)) == (struct hostent *)0) {
621 warnx("can't reverse resolve net address");
622 return (0);
623 }
624 bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
625 strncpy(inst, hp->h_name, INST_SZ);
626 inst[INST_SZ - 1] = '\0';
627 if (cp = strchr(inst, '.'))
628 *cp = '\0';
629 }
630#endif /* NFSKERB */
631
632 if (nfsargsp->flags & NFSMNT_NFSV3) {
633 nfsvers = 3;
634 mntvers = 3;
635 } else {
636 nfsvers = 2;
637 mntvers = 1;
638 }
639 nfhret.stat = EACCES; /* Mark not yet successful */
640 while (retrycnt > 0) {
641 saddr.sin_family = AF_INET;
642 saddr.sin_port = htons(PMAPPORT);
643 if ((tport = port_no ? port_no :
644 pmap_getport(&saddr, RPCPROG_NFS,
645 nfsvers, nfsproto)) == 0) {
646 if ((opflags & ISBGRND) == 0)
647 clnt_pcreateerror("NFS Portmap");
648 } else {
649 saddr.sin_port = 0;
650 pertry.tv_sec = 10;
651 pertry.tv_usec = 0;
652 if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
653 clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
654 &so, 0, 0);
655 else
656 clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
657 pertry, &so);
658 if (clp == NULL) {
659 if ((opflags & ISBGRND) == 0)
660 clnt_pcreateerror("Cannot MNT RPC");
661 } else {
662 clp->cl_auth = authunix_create_default();
663 try.tv_sec = 10;
664 try.tv_usec = 0;
665 if (nfsargsp->flags & NFSMNT_KERB)
666 nfhret.auth = RPCAUTH_KERB4;
667 else
668 nfhret.auth = RPCAUTH_UNIX;
669 nfhret.vers = mntvers;
670 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
671 xdr_dir, spec, xdr_fh, &nfhret, try);
672 if (clnt_stat != RPC_SUCCESS) {
673 if ((opflags & ISBGRND) == 0)
674 warnx("%s", clnt_sperror(clp,
675 "bad MNT RPC"));
676 } else {
677 auth_destroy(clp->cl_auth);
678 clnt_destroy(clp);
679 retrycnt = 0;
680 }
681 }
682 }
683 if (--retrycnt > 0) {
684 if (opflags & BGRND) {
685 opflags &= ~BGRND;
686 if (i = fork()) {
687 if (i == -1)
688 err(1, "nqnfs 2");
689 exit(0);
690 }
691 (void) setsid();
692 (void) close(STDIN_FILENO);
693 (void) close(STDOUT_FILENO);
694 (void) close(STDERR_FILENO);
695 (void) chdir("/");
696 opflags |= ISBGRND;
697 }
698 sleep(60);
699 }
700 }
701 if (nfhret.stat) {
702 if (opflags & ISBGRND)
703 exit(1);
704 errno = nfhret.stat;
705 warn("can't access %s", spec);
706 return (0);
707 }
708 saddr.sin_port = htons(tport);
709#ifdef ISO
710 if (isoflag) {
711 nfsargsp->addr = (struct sockaddr *) &isoaddr;
712 nfsargsp->addrlen = sizeof (isoaddr);
713 } else
714#endif /* ISO */
715 {
716 nfsargsp->addr = (struct sockaddr *) &saddr;
717 nfsargsp->addrlen = sizeof (saddr);
718 }
719 nfsargsp->fh = nfhret.nfh;
720 nfsargsp->fhsize = nfhret.fhsize;
721 nfsargsp->hostname = nam;
722 return (1);
723}
724
725/*
726 * xdr routines for mount rpc's
727 */
728int
729xdr_dir(xdrsp, dirp)
730 XDR *xdrsp;
731 char *dirp;
732{
733 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
734}
735
736int
737xdr_fh(xdrsp, np)
738 XDR *xdrsp;
739 register struct nfhret *np;
740{
741 register int i;
742 long auth, authcnt, authfnd = 0;
743
744 if (!xdr_u_long(xdrsp, &np->stat))
745 return (0);
746 if (np->stat)
747 return (1);
748 switch (np->vers) {
749 case 1:
750 np->fhsize = NFSX_V2FH;
751 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
752 case 3:
753 if (!xdr_long(xdrsp, &np->fhsize))
754 return (0);
755 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
756 return (0);
757 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
758 return (0);
759 if (!xdr_long(xdrsp, &authcnt))
760 return (0);
761 for (i = 0; i < authcnt; i++) {
762 if (!xdr_long(xdrsp, &auth))
763 return (0);
764 if (auth == np->auth)
765 authfnd++;
766 }
767
768 /*
769 * Some servers, such as DEC's OSF/1 return a nil authenticator
770 * list to indicate RPCAUTH_UNIX.
771 */
772 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
773 np->stat = EAUTH;
774 return (1);
775 };
776 return (0);
777}
778
779__dead void
780usage()
781{
782 (void)fprintf(stderr, "\
783usage: mount_nfs [-3KPTUbcdilqs] [-D deadthresh] [-I readdirsize]\n\
784 [-L leaseterm] [-R retrycnt] [-a maxreadahead] [-g maxgroups]\n\
785 [-m realm] [-o options] [-r readsize] [-t timeout] [-w writesize]\n\
786 [-x retrans] rhost:path node\n");
787 exit(1);
788}