mount_nfs.c revision 1.20 1 1.20 fvdl /* $NetBSD: mount_nfs.c,v 1.20 1998/03/01 02:20:49 fvdl Exp $ */
2 1.8 cgd
3 1.1 mycroft /*
4 1.1 mycroft * Copyright (c) 1992, 1993, 1994
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * This code is derived from software contributed to Berkeley by
8 1.1 mycroft * Rick Macklem at The University of Guelph.
9 1.1 mycroft *
10 1.1 mycroft * Redistribution and use in source and binary forms, with or without
11 1.1 mycroft * modification, are permitted provided that the following conditions
12 1.1 mycroft * are met:
13 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.1 mycroft * notice, this list of conditions and the following disclaimer.
15 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.1 mycroft * documentation and/or other materials provided with the distribution.
18 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
19 1.1 mycroft * must display the following acknowledgement:
20 1.1 mycroft * This product includes software developed by the University of
21 1.1 mycroft * California, Berkeley and its contributors.
22 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
23 1.1 mycroft * may be used to endorse or promote products derived from this software
24 1.1 mycroft * without specific prior written permission.
25 1.1 mycroft *
26 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 mycroft * SUCH DAMAGE.
37 1.1 mycroft */
38 1.1 mycroft
39 1.17 lukem #include <sys/cdefs.h>
40 1.1 mycroft #ifndef lint
41 1.17 lukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
42 1.17 lukem The Regents of the University of California. All rights reserved.\n");
43 1.1 mycroft #endif /* not lint */
44 1.1 mycroft
45 1.1 mycroft #ifndef lint
46 1.8 cgd #if 0
47 1.11 fvdl static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95";
48 1.8 cgd #else
49 1.20 fvdl __RCSID("$NetBSD: mount_nfs.c,v 1.20 1998/03/01 02:20:49 fvdl Exp $");
50 1.8 cgd #endif
51 1.1 mycroft #endif /* not lint */
52 1.1 mycroft
53 1.1 mycroft #include <sys/param.h>
54 1.1 mycroft #include <sys/mount.h>
55 1.1 mycroft #include <sys/socket.h>
56 1.1 mycroft #include <sys/socketvar.h>
57 1.1 mycroft #include <sys/stat.h>
58 1.9 jtc #include <syslog.h>
59 1.1 mycroft
60 1.1 mycroft #include <rpc/rpc.h>
61 1.1 mycroft #include <rpc/pmap_clnt.h>
62 1.1 mycroft #include <rpc/pmap_prot.h>
63 1.1 mycroft
64 1.1 mycroft #ifdef ISO
65 1.1 mycroft #include <netiso/iso.h>
66 1.1 mycroft #endif
67 1.1 mycroft
68 1.11 fvdl #ifdef NFSKERB
69 1.1 mycroft #include <kerberosIV/des.h>
70 1.1 mycroft #include <kerberosIV/krb.h>
71 1.1 mycroft #endif
72 1.1 mycroft
73 1.1 mycroft #include <nfs/rpcv2.h>
74 1.11 fvdl #include <nfs/nfsproto.h>
75 1.9 jtc #define _KERNEL
76 1.1 mycroft #include <nfs/nfs.h>
77 1.9 jtc #undef _KERNEL
78 1.1 mycroft #include <nfs/nqnfs.h>
79 1.20 fvdl #include <nfs/nfsmount.h>
80 1.1 mycroft
81 1.1 mycroft #include <arpa/inet.h>
82 1.1 mycroft
83 1.1 mycroft #include <ctype.h>
84 1.1 mycroft #include <err.h>
85 1.1 mycroft #include <errno.h>
86 1.1 mycroft #include <fcntl.h>
87 1.1 mycroft #include <netdb.h>
88 1.1 mycroft #include <signal.h>
89 1.1 mycroft #include <stdio.h>
90 1.1 mycroft #include <stdlib.h>
91 1.19 perry #include <string.h>
92 1.1 mycroft #include <unistd.h>
93 1.1 mycroft
94 1.1 mycroft #include "mntopts.h"
95 1.1 mycroft
96 1.11 fvdl #define ALTF_BG 0x1
97 1.14 christos #define ALTF_CONN 0x2
98 1.11 fvdl #define ALTF_DUMBTIMR 0x4
99 1.11 fvdl #define ALTF_INTR 0x8
100 1.11 fvdl #define ALTF_KERB 0x10
101 1.11 fvdl #define ALTF_NFSV3 0x20
102 1.11 fvdl #define ALTF_RDIRPLUS 0x40
103 1.11 fvdl #define ALTF_MNTUDP 0x80
104 1.15 fvdl #define ALTF_NORESPORT 0x100
105 1.11 fvdl #define ALTF_SEQPACKET 0x200
106 1.11 fvdl #define ALTF_NQNFS 0x400
107 1.11 fvdl #define ALTF_SOFT 0x800
108 1.11 fvdl #define ALTF_TCP 0x1000
109 1.13 fvdl #define ALTF_NFSV2 0x2000
110 1.11 fvdl
111 1.12 jtc const struct mntopt mopts[] = {
112 1.1 mycroft MOPT_STDOPTS,
113 1.1 mycroft MOPT_FORCE,
114 1.1 mycroft MOPT_UPDATE,
115 1.11 fvdl { "bg", 0, ALTF_BG, 1 },
116 1.14 christos { "conn", 0, ALTF_CONN, 1 },
117 1.11 fvdl { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
118 1.11 fvdl { "intr", 0, ALTF_INTR, 1 },
119 1.11 fvdl #ifdef NFSKERB
120 1.11 fvdl { "kerb", 0, ALTF_KERB, 1 },
121 1.11 fvdl #endif
122 1.11 fvdl { "nfsv3", 0, ALTF_NFSV3, 1 },
123 1.11 fvdl { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
124 1.11 fvdl { "mntudp", 0, ALTF_MNTUDP, 1 },
125 1.15 fvdl { "noresport", 0, ALTF_NORESPORT, 1 },
126 1.11 fvdl #ifdef ISO
127 1.11 fvdl { "seqpacket", 0, ALTF_SEQPACKET, 1 },
128 1.11 fvdl #endif
129 1.11 fvdl { "nqnfs", 0, ALTF_NQNFS, 1 },
130 1.11 fvdl { "soft", 0, ALTF_SOFT, 1 },
131 1.11 fvdl { "tcp", 0, ALTF_TCP, 1 },
132 1.13 fvdl { "nfsv2", 0, ALTF_NFSV2, 1 },
133 1.1 mycroft { NULL }
134 1.1 mycroft };
135 1.1 mycroft
136 1.1 mycroft struct nfs_args nfsdefargs = {
137 1.11 fvdl NFS_ARGSVERSION,
138 1.1 mycroft (struct sockaddr *)0,
139 1.1 mycroft sizeof (struct sockaddr_in),
140 1.1 mycroft SOCK_DGRAM,
141 1.1 mycroft 0,
142 1.11 fvdl (u_char *)0,
143 1.1 mycroft 0,
144 1.15 fvdl NFSMNT_NFSV3|NFSMNT_NOCONN|NFSMNT_RESVPORT,
145 1.1 mycroft NFS_WSIZE,
146 1.1 mycroft NFS_RSIZE,
147 1.11 fvdl NFS_READDIRSIZE,
148 1.11 fvdl 10,
149 1.1 mycroft NFS_RETRANS,
150 1.1 mycroft NFS_MAXGRPS,
151 1.1 mycroft NFS_DEFRAHEAD,
152 1.1 mycroft NQ_DEFLEASE,
153 1.1 mycroft NQ_DEADTHRESH,
154 1.1 mycroft (char *)0,
155 1.1 mycroft };
156 1.1 mycroft
157 1.1 mycroft struct nfhret {
158 1.11 fvdl u_long stat;
159 1.11 fvdl long vers;
160 1.11 fvdl long auth;
161 1.11 fvdl long fhsize;
162 1.11 fvdl u_char nfh[NFSX_V3FHMAX];
163 1.1 mycroft };
164 1.1 mycroft #define DEF_RETRY 10000
165 1.1 mycroft #define BGRND 1
166 1.1 mycroft #define ISBGRND 2
167 1.11 fvdl int retrycnt;
168 1.1 mycroft int opflags = 0;
169 1.11 fvdl int nfsproto = IPPROTO_UDP;
170 1.11 fvdl int mnttcp_ok = 1;
171 1.11 fvdl int force2 = 0;
172 1.11 fvdl int force3 = 0;
173 1.1 mycroft
174 1.11 fvdl #ifdef NFSKERB
175 1.1 mycroft char inst[INST_SZ];
176 1.1 mycroft char realm[REALM_SZ];
177 1.11 fvdl struct {
178 1.11 fvdl u_long kind;
179 1.11 fvdl KTEXT_ST kt;
180 1.11 fvdl } ktick;
181 1.11 fvdl struct nfsrpc_nickverf kverf;
182 1.11 fvdl struct nfsrpc_fullblock kin, kout;
183 1.11 fvdl NFSKERBKEY_T kivec;
184 1.11 fvdl CREDENTIALS kcr;
185 1.11 fvdl struct timeval ktv;
186 1.11 fvdl NFSKERBKEYSCHED_T kerb_keysched;
187 1.1 mycroft #endif
188 1.1 mycroft
189 1.1 mycroft int getnfsargs __P((char *, struct nfs_args *));
190 1.1 mycroft #ifdef ISO
191 1.1 mycroft struct iso_addr *iso_addr __P((const char *));
192 1.1 mycroft #endif
193 1.17 lukem int main __P((int, char *[]));
194 1.1 mycroft void set_rpc_maxgrouplist __P((int));
195 1.17 lukem void usage __P((void));
196 1.1 mycroft int xdr_dir __P((XDR *, char *));
197 1.1 mycroft int xdr_fh __P((XDR *, struct nfhret *));
198 1.1 mycroft
199 1.1 mycroft int
200 1.1 mycroft main(argc, argv)
201 1.1 mycroft int argc;
202 1.1 mycroft char *argv[];
203 1.1 mycroft {
204 1.17 lukem int c;
205 1.17 lukem struct nfs_args *nfsargsp;
206 1.1 mycroft struct nfs_args nfsargs;
207 1.1 mycroft struct nfsd_cargs ncd;
208 1.11 fvdl int mntflags, altflags, i, nfssvc_flag, num;
209 1.1 mycroft char *name, *p, *spec;
210 1.11 fvdl #ifdef NFSKERB
211 1.1 mycroft uid_t last_ruid;
212 1.1 mycroft
213 1.1 mycroft last_ruid = -1;
214 1.3 brezak if (krb_get_lrealm(realm, 0) != KSUCCESS)
215 1.3 brezak (void)strcpy(realm, KRB_REALM);
216 1.11 fvdl if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
217 1.11 fvdl sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
218 1.11 fvdl ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
219 1.11 fvdl ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
220 1.11 fvdl fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
221 1.11 fvdl
222 1.1 mycroft #endif
223 1.1 mycroft retrycnt = DEF_RETRY;
224 1.1 mycroft
225 1.1 mycroft mntflags = 0;
226 1.11 fvdl altflags = 0;
227 1.1 mycroft nfsargs = nfsdefargs;
228 1.1 mycroft nfsargsp = &nfsargs;
229 1.1 mycroft while ((c = getopt(argc, argv,
230 1.18 fvdl "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
231 1.1 mycroft switch (c) {
232 1.11 fvdl case '3':
233 1.11 fvdl if (force2)
234 1.11 fvdl errx(1, "-2 and -3 are mutually exclusive");
235 1.11 fvdl force3 = 1;
236 1.11 fvdl break;
237 1.11 fvdl case '2':
238 1.11 fvdl if (force3)
239 1.11 fvdl errx(1, "-2 and -3 are mutually exclusive");
240 1.11 fvdl force2 = 1;
241 1.11 fvdl nfsargsp->flags &= ~NFSMNT_NFSV3;
242 1.11 fvdl break;
243 1.1 mycroft case 'a':
244 1.1 mycroft num = strtol(optarg, &p, 10);
245 1.1 mycroft if (*p || num < 0)
246 1.1 mycroft errx(1, "illegal -a value -- %s", optarg);
247 1.1 mycroft nfsargsp->readahead = num;
248 1.1 mycroft nfsargsp->flags |= NFSMNT_READAHEAD;
249 1.1 mycroft break;
250 1.1 mycroft case 'b':
251 1.1 mycroft opflags |= BGRND;
252 1.1 mycroft break;
253 1.1 mycroft case 'c':
254 1.1 mycroft nfsargsp->flags |= NFSMNT_NOCONN;
255 1.1 mycroft break;
256 1.14 christos case 'C':
257 1.14 christos nfsargsp->flags &= ~NFSMNT_NOCONN;
258 1.14 christos break;
259 1.1 mycroft case 'D':
260 1.1 mycroft num = strtol(optarg, &p, 10);
261 1.1 mycroft if (*p || num <= 0)
262 1.1 mycroft errx(1, "illegal -D value -- %s", optarg);
263 1.1 mycroft nfsargsp->deadthresh = num;
264 1.1 mycroft nfsargsp->flags |= NFSMNT_DEADTHRESH;
265 1.1 mycroft break;
266 1.1 mycroft case 'd':
267 1.1 mycroft nfsargsp->flags |= NFSMNT_DUMBTIMR;
268 1.1 mycroft break;
269 1.1 mycroft #if 0 /* XXXX */
270 1.1 mycroft case 'g':
271 1.1 mycroft num = strtol(optarg, &p, 10);
272 1.1 mycroft if (*p || num <= 0)
273 1.1 mycroft errx(1, "illegal -g value -- %s", optarg);
274 1.1 mycroft set_rpc_maxgrouplist(num);
275 1.1 mycroft nfsargsp->maxgrouplist = num;
276 1.1 mycroft nfsargsp->flags |= NFSMNT_MAXGRPS;
277 1.1 mycroft break;
278 1.1 mycroft #endif
279 1.11 fvdl case 'I':
280 1.11 fvdl num = strtol(optarg, &p, 10);
281 1.11 fvdl if (*p || num <= 0)
282 1.11 fvdl errx(1, "illegal -I value -- %s", optarg);
283 1.11 fvdl nfsargsp->readdirsize = num;
284 1.11 fvdl nfsargsp->flags |= NFSMNT_READDIRSIZE;
285 1.11 fvdl break;
286 1.1 mycroft case 'i':
287 1.1 mycroft nfsargsp->flags |= NFSMNT_INT;
288 1.1 mycroft break;
289 1.11 fvdl #ifdef NFSKERB
290 1.1 mycroft case 'K':
291 1.1 mycroft nfsargsp->flags |= NFSMNT_KERB;
292 1.1 mycroft break;
293 1.1 mycroft #endif
294 1.1 mycroft case 'L':
295 1.1 mycroft num = strtol(optarg, &p, 10);
296 1.1 mycroft if (*p || num < 2)
297 1.1 mycroft errx(1, "illegal -L value -- %s", optarg);
298 1.1 mycroft nfsargsp->leaseterm = num;
299 1.1 mycroft nfsargsp->flags |= NFSMNT_LEASETERM;
300 1.1 mycroft break;
301 1.1 mycroft case 'l':
302 1.11 fvdl nfsargsp->flags |= NFSMNT_RDIRPLUS;
303 1.1 mycroft break;
304 1.11 fvdl #ifdef NFSKERB
305 1.1 mycroft case 'm':
306 1.1 mycroft (void)strncpy(realm, optarg, REALM_SZ - 1);
307 1.1 mycroft realm[REALM_SZ - 1] = '\0';
308 1.1 mycroft break;
309 1.1 mycroft #endif
310 1.1 mycroft case 'o':
311 1.11 fvdl getmntopts(optarg, mopts, &mntflags, &altflags);
312 1.11 fvdl if(altflags & ALTF_BG)
313 1.11 fvdl opflags |= BGRND;
314 1.14 christos if(altflags & ALTF_CONN)
315 1.14 christos nfsargsp->flags &= ~NFSMNT_NOCONN;
316 1.11 fvdl if(altflags & ALTF_DUMBTIMR)
317 1.11 fvdl nfsargsp->flags |= NFSMNT_DUMBTIMR;
318 1.11 fvdl if(altflags & ALTF_INTR)
319 1.11 fvdl nfsargsp->flags |= NFSMNT_INT;
320 1.11 fvdl #ifdef NFSKERB
321 1.11 fvdl if(altflags & ALTF_KERB)
322 1.11 fvdl nfsargsp->flags |= NFSMNT_KERB;
323 1.11 fvdl #endif
324 1.13 fvdl if(altflags & ALTF_NFSV3) {
325 1.13 fvdl if (force2)
326 1.13 fvdl errx(1,"conflicting version options");
327 1.13 fvdl force3 = 1;
328 1.13 fvdl }
329 1.13 fvdl if(altflags & ALTF_NFSV2) {
330 1.13 fvdl if (force3)
331 1.13 fvdl errx(1,"conflicting version options");
332 1.13 fvdl force2 = 1;
333 1.13 fvdl nfsargsp->flags &= ~NFSMNT_NFSV3;
334 1.13 fvdl }
335 1.11 fvdl if(altflags & ALTF_RDIRPLUS)
336 1.11 fvdl nfsargsp->flags |= NFSMNT_RDIRPLUS;
337 1.11 fvdl if(altflags & ALTF_MNTUDP)
338 1.11 fvdl mnttcp_ok = 0;
339 1.15 fvdl if(altflags & ALTF_NORESPORT)
340 1.15 fvdl nfsargsp->flags &= ~NFSMNT_RESVPORT;
341 1.11 fvdl #ifdef ISO
342 1.11 fvdl if(altflags & ALTF_SEQPACKET)
343 1.11 fvdl nfsargsp->sotype = SOCK_SEQPACKET;
344 1.11 fvdl #endif
345 1.13 fvdl if(altflags & ALTF_NQNFS) {
346 1.13 fvdl if (force2)
347 1.13 fvdl errx(1,"nqnfs only available with v3");
348 1.13 fvdl force3 = 1;
349 1.13 fvdl nfsargsp->flags |= NFSMNT_NQNFS;
350 1.13 fvdl }
351 1.11 fvdl if(altflags & ALTF_SOFT)
352 1.11 fvdl nfsargsp->flags |= NFSMNT_SOFT;
353 1.11 fvdl if(altflags & ALTF_TCP) {
354 1.11 fvdl nfsargsp->sotype = SOCK_STREAM;
355 1.11 fvdl nfsproto = IPPROTO_TCP;
356 1.11 fvdl }
357 1.11 fvdl altflags = 0;
358 1.1 mycroft break;
359 1.1 mycroft case 'P':
360 1.1 mycroft nfsargsp->flags |= NFSMNT_RESVPORT;
361 1.1 mycroft break;
362 1.1 mycroft case 'p':
363 1.15 fvdl nfsargsp->flags &= ~NFSMNT_RESVPORT;
364 1.1 mycroft break;
365 1.1 mycroft case 'q':
366 1.13 fvdl if (force2)
367 1.13 fvdl errx(1,"nqnfs only available with v3");
368 1.13 fvdl force3 = 1;
369 1.13 fvdl nfsargsp->flags |= NFSMNT_NQNFS;
370 1.1 mycroft break;
371 1.1 mycroft case 'R':
372 1.1 mycroft num = strtol(optarg, &p, 10);
373 1.1 mycroft if (*p || num <= 0)
374 1.1 mycroft errx(1, "illegal -R value -- %s", optarg);
375 1.1 mycroft retrycnt = num;
376 1.1 mycroft break;
377 1.1 mycroft case 'r':
378 1.1 mycroft num = strtol(optarg, &p, 10);
379 1.1 mycroft if (*p || num <= 0)
380 1.1 mycroft errx(1, "illegal -r value -- %s", optarg);
381 1.1 mycroft nfsargsp->rsize = num;
382 1.1 mycroft nfsargsp->flags |= NFSMNT_RSIZE;
383 1.1 mycroft break;
384 1.15 fvdl #ifdef ISO
385 1.15 fvdl case 'S':
386 1.15 fvdl nfsargsp->sotype = SOCK_SEQPACKET;
387 1.15 fvdl break;
388 1.15 fvdl #endif
389 1.1 mycroft case 's':
390 1.1 mycroft nfsargsp->flags |= NFSMNT_SOFT;
391 1.1 mycroft break;
392 1.1 mycroft case 'T':
393 1.1 mycroft nfsargsp->sotype = SOCK_STREAM;
394 1.11 fvdl nfsproto = IPPROTO_TCP;
395 1.1 mycroft break;
396 1.1 mycroft case 't':
397 1.1 mycroft num = strtol(optarg, &p, 10);
398 1.1 mycroft if (*p || num <= 0)
399 1.1 mycroft errx(1, "illegal -t value -- %s", optarg);
400 1.1 mycroft nfsargsp->timeo = num;
401 1.1 mycroft nfsargsp->flags |= NFSMNT_TIMEO;
402 1.1 mycroft break;
403 1.1 mycroft case 'w':
404 1.1 mycroft num = strtol(optarg, &p, 10);
405 1.1 mycroft if (*p || num <= 0)
406 1.1 mycroft errx(1, "illegal -w value -- %s", optarg);
407 1.1 mycroft nfsargsp->wsize = num;
408 1.1 mycroft nfsargsp->flags |= NFSMNT_WSIZE;
409 1.1 mycroft break;
410 1.1 mycroft case 'x':
411 1.1 mycroft num = strtol(optarg, &p, 10);
412 1.1 mycroft if (*p || num <= 0)
413 1.1 mycroft errx(1, "illegal -x value -- %s", optarg);
414 1.1 mycroft nfsargsp->retrans = num;
415 1.1 mycroft nfsargsp->flags |= NFSMNT_RETRANS;
416 1.18 fvdl break;
417 1.18 fvdl case 'X':
418 1.18 fvdl nfsargsp->flags |= NFSMNT_XLATECOOKIE;
419 1.1 mycroft break;
420 1.11 fvdl case 'U':
421 1.11 fvdl mnttcp_ok = 0;
422 1.11 fvdl break;
423 1.1 mycroft default:
424 1.1 mycroft usage();
425 1.1 mycroft break;
426 1.1 mycroft }
427 1.1 mycroft argc -= optind;
428 1.1 mycroft argv += optind;
429 1.1 mycroft
430 1.1 mycroft if (argc != 2)
431 1.7 cgd usage();
432 1.1 mycroft
433 1.1 mycroft spec = *argv++;
434 1.1 mycroft name = *argv;
435 1.1 mycroft
436 1.1 mycroft if (!getnfsargs(spec, nfsargsp))
437 1.1 mycroft exit(1);
438 1.1 mycroft if (mount(MOUNT_NFS, name, mntflags, nfsargsp))
439 1.1 mycroft err(1, "%s", name);
440 1.1 mycroft if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
441 1.1 mycroft if ((opflags & ISBGRND) == 0) {
442 1.17 lukem if ((i = fork()) != 0) {
443 1.1 mycroft if (i == -1)
444 1.1 mycroft err(1, "nqnfs 1");
445 1.1 mycroft exit(0);
446 1.1 mycroft }
447 1.1 mycroft (void) setsid();
448 1.1 mycroft (void) close(STDIN_FILENO);
449 1.1 mycroft (void) close(STDOUT_FILENO);
450 1.1 mycroft (void) close(STDERR_FILENO);
451 1.1 mycroft (void) chdir("/");
452 1.1 mycroft }
453 1.1 mycroft openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
454 1.1 mycroft nfssvc_flag = NFSSVC_MNTD;
455 1.1 mycroft ncd.ncd_dirp = name;
456 1.1 mycroft while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
457 1.1 mycroft if (errno != ENEEDAUTH) {
458 1.1 mycroft syslog(LOG_ERR, "nfssvc err %m");
459 1.1 mycroft continue;
460 1.1 mycroft }
461 1.1 mycroft nfssvc_flag =
462 1.1 mycroft NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
463 1.11 fvdl #ifdef NFSKERB
464 1.1 mycroft /*
465 1.1 mycroft * Set up as ncd_authuid for the kerberos call.
466 1.1 mycroft * Must set ruid to ncd_authuid and reset the
467 1.1 mycroft * ticket name iff ncd_authuid is not the same
468 1.1 mycroft * as last time, so that the right ticket file
469 1.1 mycroft * is found.
470 1.11 fvdl * Get the Kerberos credential structure so that
471 1.11 fvdl * we have the seesion key and get a ticket for
472 1.11 fvdl * this uid.
473 1.11 fvdl * For more info see the IETF Draft "Authentication
474 1.11 fvdl * in ONC RPC".
475 1.1 mycroft */
476 1.1 mycroft if (ncd.ncd_authuid != last_ruid) {
477 1.1 mycroft krb_set_tkt_string("");
478 1.1 mycroft last_ruid = ncd.ncd_authuid;
479 1.1 mycroft }
480 1.1 mycroft setreuid(ncd.ncd_authuid, 0);
481 1.11 fvdl kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
482 1.11 fvdl if (kret == RET_NOTKT) {
483 1.11 fvdl kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
484 1.11 fvdl DEFAULT_TKT_LIFE);
485 1.11 fvdl if (kret == KSUCCESS)
486 1.11 fvdl kret = krb_get_cred(NFS_KERBSRV, inst, realm,
487 1.11 fvdl &kcr);
488 1.11 fvdl }
489 1.11 fvdl if (kret == KSUCCESS)
490 1.11 fvdl kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
491 1.11 fvdl realm, 0);
492 1.11 fvdl
493 1.11 fvdl /*
494 1.11 fvdl * Fill in the AKN_FULLNAME authenticator and verfier.
495 1.11 fvdl * Along with the Kerberos ticket, we need to build
496 1.11 fvdl * the timestamp verifier and encrypt it in CBC mode.
497 1.11 fvdl */
498 1.11 fvdl if (kret == KSUCCESS &&
499 1.11 fvdl ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
500 1.11 fvdl && gettimeofday(&ktv, (struct timezone *)0) == 0) {
501 1.11 fvdl ncd.ncd_authtype = RPCAUTH_KERB4;
502 1.11 fvdl ncd.ncd_authstr = (u_char *)&ktick;
503 1.11 fvdl ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
504 1.11 fvdl 3 * NFSX_UNSIGNED;
505 1.11 fvdl ncd.ncd_verfstr = (u_char *)&kverf;
506 1.11 fvdl ncd.ncd_verflen = sizeof (kverf);
507 1.11 fvdl memmove(ncd.ncd_key, kcr.session,
508 1.11 fvdl sizeof (kcr.session));
509 1.11 fvdl kin.t1 = htonl(ktv.tv_sec);
510 1.11 fvdl kin.t2 = htonl(ktv.tv_usec);
511 1.11 fvdl kin.w1 = htonl(NFS_KERBTTL);
512 1.11 fvdl kin.w2 = htonl(NFS_KERBTTL - 1);
513 1.17 lukem memset((caddr_t)kivec, 0, sizeof (kivec));
514 1.11 fvdl
515 1.11 fvdl /*
516 1.11 fvdl * Encrypt kin in CBC mode using the session
517 1.11 fvdl * key in kcr.
518 1.11 fvdl */
519 1.11 fvdl XXX
520 1.11 fvdl
521 1.11 fvdl /*
522 1.11 fvdl * Finally, fill the timestamp verifier into the
523 1.11 fvdl * authenticator and verifier.
524 1.11 fvdl */
525 1.11 fvdl ktick.kind = htonl(RPCAKN_FULLNAME);
526 1.11 fvdl kverf.kind = htonl(RPCAKN_FULLNAME);
527 1.11 fvdl NFS_KERBW1(ktick.kt) = kout.w1;
528 1.11 fvdl ktick.kt.length = htonl(ktick.kt.length);
529 1.11 fvdl kverf.verf.t1 = kout.t1;
530 1.11 fvdl kverf.verf.t2 = kout.t2;
531 1.11 fvdl kverf.verf.w2 = kout.w2;
532 1.11 fvdl nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
533 1.1 mycroft }
534 1.1 mycroft setreuid(0, 0);
535 1.11 fvdl #endif /* NFSKERB */
536 1.1 mycroft }
537 1.1 mycroft }
538 1.1 mycroft exit(0);
539 1.1 mycroft }
540 1.1 mycroft
541 1.1 mycroft int
542 1.1 mycroft getnfsargs(spec, nfsargsp)
543 1.1 mycroft char *spec;
544 1.1 mycroft struct nfs_args *nfsargsp;
545 1.1 mycroft {
546 1.17 lukem CLIENT *clp;
547 1.1 mycroft struct hostent *hp;
548 1.1 mycroft static struct sockaddr_in saddr;
549 1.1 mycroft #ifdef ISO
550 1.1 mycroft static struct sockaddr_iso isoaddr;
551 1.1 mycroft struct iso_addr *isop;
552 1.1 mycroft int isoflag = 0;
553 1.1 mycroft #endif
554 1.1 mycroft struct timeval pertry, try;
555 1.1 mycroft enum clnt_stat clnt_stat;
556 1.11 fvdl int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt;
557 1.1 mycroft char *hostp, *delimp;
558 1.11 fvdl #ifdef NFSKERB
559 1.1 mycroft char *cp;
560 1.1 mycroft #endif
561 1.17 lukem u_short tport = 0;
562 1.1 mycroft static struct nfhret nfhret;
563 1.1 mycroft static char nam[MNAMELEN + 1];
564 1.1 mycroft
565 1.1 mycroft strncpy(nam, spec, MNAMELEN);
566 1.1 mycroft nam[MNAMELEN] = '\0';
567 1.1 mycroft if ((delimp = strchr(spec, '@')) != NULL) {
568 1.1 mycroft hostp = delimp + 1;
569 1.1 mycroft } else if ((delimp = strchr(spec, ':')) != NULL) {
570 1.1 mycroft hostp = spec;
571 1.1 mycroft spec = delimp + 1;
572 1.1 mycroft } else {
573 1.1 mycroft warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
574 1.1 mycroft return (0);
575 1.1 mycroft }
576 1.1 mycroft *delimp = '\0';
577 1.1 mycroft /*
578 1.1 mycroft * DUMB!! Until the mount protocol works on iso transport, we must
579 1.1 mycroft * supply both an iso and an inet address for the host.
580 1.1 mycroft */
581 1.1 mycroft #ifdef ISO
582 1.1 mycroft if (!strncmp(hostp, "iso=", 4)) {
583 1.1 mycroft u_short isoport;
584 1.1 mycroft
585 1.1 mycroft hostp += 4;
586 1.1 mycroft isoflag++;
587 1.1 mycroft if ((delimp = strchr(hostp, '+')) == NULL) {
588 1.1 mycroft warnx("no iso+inet address");
589 1.1 mycroft return (0);
590 1.1 mycroft }
591 1.1 mycroft *delimp = '\0';
592 1.1 mycroft if ((isop = iso_addr(hostp)) == NULL) {
593 1.1 mycroft warnx("bad ISO address");
594 1.1 mycroft return (0);
595 1.1 mycroft }
596 1.6 mycroft memset(&isoaddr, 0, sizeof (isoaddr));
597 1.6 mycroft memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
598 1.1 mycroft isoaddr.siso_len = sizeof (isoaddr);
599 1.1 mycroft isoaddr.siso_family = AF_ISO;
600 1.1 mycroft isoaddr.siso_tlen = 2;
601 1.1 mycroft isoport = htons(NFS_PORT);
602 1.6 mycroft memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
603 1.1 mycroft hostp = delimp + 1;
604 1.1 mycroft }
605 1.1 mycroft #endif /* ISO */
606 1.1 mycroft
607 1.1 mycroft /*
608 1.1 mycroft * Handle an internet host address and reverse resolve it if
609 1.1 mycroft * doing Kerberos.
610 1.1 mycroft */
611 1.10 mycroft if (inet_aton(hostp, &saddr.sin_addr) != 0) {
612 1.10 mycroft if ((nfsargsp->flags & NFSMNT_KERB)) {
613 1.10 mycroft if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
614 1.10 mycroft sizeof (u_long), AF_INET)) == (struct hostent *)0) {
615 1.10 mycroft warnx("can't reverse resolve net address");
616 1.10 mycroft return (0);
617 1.10 mycroft }
618 1.1 mycroft }
619 1.10 mycroft } else {
620 1.10 mycroft hp = gethostbyname(hostp);
621 1.10 mycroft if (hp == NULL) {
622 1.10 mycroft warnx("can't get net id for host");
623 1.1 mycroft return (0);
624 1.1 mycroft }
625 1.6 mycroft memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
626 1.10 mycroft }
627 1.11 fvdl #ifdef NFSKERB
628 1.1 mycroft if (nfsargsp->flags & NFSMNT_KERB) {
629 1.1 mycroft strncpy(inst, hp->h_name, INST_SZ);
630 1.1 mycroft inst[INST_SZ - 1] = '\0';
631 1.1 mycroft if (cp = strchr(inst, '.'))
632 1.1 mycroft *cp = '\0';
633 1.1 mycroft }
634 1.11 fvdl #endif /* NFSKERB */
635 1.1 mycroft
636 1.11 fvdl if (force2) {
637 1.11 fvdl nfsvers = NFS_VER2;
638 1.11 fvdl mntvers = RPCMNT_VER1;
639 1.11 fvdl } else {
640 1.11 fvdl nfsvers = NFS_VER3;
641 1.11 fvdl mntvers = RPCMNT_VER3;
642 1.11 fvdl }
643 1.11 fvdl orgcnt = retrycnt;
644 1.11 fvdl tryagain:
645 1.1 mycroft nfhret.stat = EACCES; /* Mark not yet successful */
646 1.1 mycroft while (retrycnt > 0) {
647 1.1 mycroft saddr.sin_family = AF_INET;
648 1.1 mycroft saddr.sin_port = htons(PMAPPORT);
649 1.1 mycroft if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
650 1.11 fvdl nfsvers, nfsargsp->sotype == SOCK_STREAM ? IPPROTO_TCP :
651 1.5 mycroft IPPROTO_UDP)) == 0) {
652 1.1 mycroft if ((opflags & ISBGRND) == 0)
653 1.1 mycroft clnt_pcreateerror("NFS Portmap");
654 1.1 mycroft } else {
655 1.1 mycroft saddr.sin_port = 0;
656 1.1 mycroft pertry.tv_sec = 10;
657 1.1 mycroft pertry.tv_usec = 0;
658 1.11 fvdl if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
659 1.11 fvdl clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
660 1.11 fvdl &so, 0, 0);
661 1.11 fvdl else
662 1.11 fvdl clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
663 1.11 fvdl pertry, &so);
664 1.11 fvdl if (clp == NULL) {
665 1.1 mycroft if ((opflags & ISBGRND) == 0)
666 1.16 jtk clnt_pcreateerror("Cannot MNT RPC");
667 1.1 mycroft } else {
668 1.1 mycroft clp->cl_auth = authunix_create_default();
669 1.1 mycroft try.tv_sec = 10;
670 1.1 mycroft try.tv_usec = 0;
671 1.11 fvdl if (nfsargsp->flags & NFSMNT_KERB)
672 1.11 fvdl nfhret.auth = RPCAUTH_KERB4;
673 1.11 fvdl else
674 1.11 fvdl nfhret.auth = RPCAUTH_UNIX;
675 1.11 fvdl nfhret.vers = mntvers;
676 1.1 mycroft clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
677 1.1 mycroft xdr_dir, spec, xdr_fh, &nfhret, try);
678 1.15 fvdl switch (clnt_stat) {
679 1.15 fvdl case RPC_PROGVERSMISMATCH:
680 1.15 fvdl if (nfsvers == NFS_VER3 && !force3) {
681 1.15 fvdl retrycnt = orgcnt;
682 1.15 fvdl nfsvers = NFS_VER2;
683 1.15 fvdl mntvers = RPCMNT_VER1;
684 1.15 fvdl nfsargsp->flags &=
685 1.15 fvdl ~NFSMNT_NFSV3;
686 1.15 fvdl goto tryagain;
687 1.15 fvdl } else {
688 1.15 fvdl errx(1, "%s", clnt_sperror(clp,
689 1.15 fvdl "MNT RPC"));
690 1.11 fvdl }
691 1.15 fvdl case RPC_SUCCESS:
692 1.15 fvdl auth_destroy(clp->cl_auth);
693 1.15 fvdl clnt_destroy(clp);
694 1.15 fvdl retrycnt = 0;
695 1.15 fvdl break;
696 1.15 fvdl default:
697 1.15 fvdl /* XXX should give up on some errors */
698 1.1 mycroft if ((opflags & ISBGRND) == 0)
699 1.1 mycroft warnx("%s", clnt_sperror(clp,
700 1.1 mycroft "bad MNT RPC"));
701 1.15 fvdl break;
702 1.1 mycroft }
703 1.1 mycroft }
704 1.1 mycroft }
705 1.1 mycroft if (--retrycnt > 0) {
706 1.1 mycroft if (opflags & BGRND) {
707 1.1 mycroft opflags &= ~BGRND;
708 1.17 lukem if ((i = fork()) != 0) {
709 1.1 mycroft if (i == -1)
710 1.1 mycroft err(1, "nqnfs 2");
711 1.1 mycroft exit(0);
712 1.1 mycroft }
713 1.1 mycroft (void) setsid();
714 1.1 mycroft (void) close(STDIN_FILENO);
715 1.1 mycroft (void) close(STDOUT_FILENO);
716 1.1 mycroft (void) close(STDERR_FILENO);
717 1.1 mycroft (void) chdir("/");
718 1.1 mycroft opflags |= ISBGRND;
719 1.1 mycroft }
720 1.1 mycroft sleep(60);
721 1.1 mycroft }
722 1.1 mycroft }
723 1.1 mycroft if (nfhret.stat) {
724 1.1 mycroft if (opflags & ISBGRND)
725 1.1 mycroft exit(1);
726 1.4 mycroft errno = nfhret.stat;
727 1.11 fvdl warnx("can't access %s: %s", spec, strerror(nfhret.stat));
728 1.1 mycroft return (0);
729 1.1 mycroft }
730 1.1 mycroft saddr.sin_port = htons(tport);
731 1.1 mycroft #ifdef ISO
732 1.1 mycroft if (isoflag) {
733 1.1 mycroft nfsargsp->addr = (struct sockaddr *) &isoaddr;
734 1.1 mycroft nfsargsp->addrlen = sizeof (isoaddr);
735 1.1 mycroft } else
736 1.1 mycroft #endif /* ISO */
737 1.1 mycroft {
738 1.1 mycroft nfsargsp->addr = (struct sockaddr *) &saddr;
739 1.1 mycroft nfsargsp->addrlen = sizeof (saddr);
740 1.1 mycroft }
741 1.11 fvdl nfsargsp->fh = nfhret.nfh;
742 1.11 fvdl nfsargsp->fhsize = nfhret.fhsize;
743 1.1 mycroft nfsargsp->hostname = nam;
744 1.1 mycroft return (1);
745 1.1 mycroft }
746 1.1 mycroft
747 1.1 mycroft /*
748 1.1 mycroft * xdr routines for mount rpc's
749 1.1 mycroft */
750 1.1 mycroft int
751 1.1 mycroft xdr_dir(xdrsp, dirp)
752 1.1 mycroft XDR *xdrsp;
753 1.1 mycroft char *dirp;
754 1.1 mycroft {
755 1.1 mycroft return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
756 1.1 mycroft }
757 1.1 mycroft
758 1.1 mycroft int
759 1.1 mycroft xdr_fh(xdrsp, np)
760 1.1 mycroft XDR *xdrsp;
761 1.17 lukem struct nfhret *np;
762 1.1 mycroft {
763 1.17 lukem int i;
764 1.11 fvdl long auth, authcnt, authfnd = 0;
765 1.11 fvdl
766 1.11 fvdl if (!xdr_u_long(xdrsp, &np->stat))
767 1.1 mycroft return (0);
768 1.1 mycroft if (np->stat)
769 1.1 mycroft return (1);
770 1.11 fvdl switch (np->vers) {
771 1.11 fvdl case 1:
772 1.11 fvdl np->fhsize = NFSX_V2FH;
773 1.11 fvdl return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
774 1.11 fvdl case 3:
775 1.11 fvdl if (!xdr_long(xdrsp, &np->fhsize))
776 1.11 fvdl return (0);
777 1.11 fvdl if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
778 1.11 fvdl return (0);
779 1.11 fvdl if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
780 1.11 fvdl return (0);
781 1.11 fvdl if (!xdr_long(xdrsp, &authcnt))
782 1.11 fvdl return (0);
783 1.11 fvdl for (i = 0; i < authcnt; i++) {
784 1.11 fvdl if (!xdr_long(xdrsp, &auth))
785 1.11 fvdl return (0);
786 1.11 fvdl if (auth == np->auth)
787 1.11 fvdl authfnd++;
788 1.11 fvdl }
789 1.11 fvdl /*
790 1.11 fvdl * Some servers, such as DEC's OSF/1 return a nil authenticator
791 1.11 fvdl * list to indicate RPCAUTH_UNIX.
792 1.11 fvdl */
793 1.11 fvdl if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
794 1.11 fvdl np->stat = EAUTH;
795 1.11 fvdl return (1);
796 1.11 fvdl };
797 1.11 fvdl return (0);
798 1.1 mycroft }
799 1.1 mycroft
800 1.17 lukem void
801 1.1 mycroft usage()
802 1.1 mycroft {
803 1.1 mycroft (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n",
804 1.14 christos "[-23bcCdiKklMPqsT] [-a maxreadahead] [-D deadthresh]",
805 1.1 mycroft "\t[-g maxgroups] [-L leaseterm] [-m realm] [-o options] [-R retrycnt]",
806 1.1 mycroft "\t[-r readsize] [-t timeout] [-w writesize] [-x retrans]",
807 1.1 mycroft "\trhost:path node");
808 1.1 mycroft exit(1);
809 1.1 mycroft }
810