mount_nfs.c revision 1.49 1 1.49 christos /* $NetBSD: mount_nfs.c,v 1.49 2006/03/21 21:11:41 christos 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.45 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 mycroft * may be used to endorse or promote products derived from this software
20 1.1 mycroft * without specific prior written permission.
21 1.1 mycroft *
22 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 mycroft * SUCH DAMAGE.
33 1.1 mycroft */
34 1.1 mycroft
35 1.17 lukem #include <sys/cdefs.h>
36 1.1 mycroft #ifndef lint
37 1.17 lukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
38 1.17 lukem The Regents of the University of California. All rights reserved.\n");
39 1.1 mycroft #endif /* not lint */
40 1.1 mycroft
41 1.1 mycroft #ifndef lint
42 1.8 cgd #if 0
43 1.11 fvdl static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95";
44 1.8 cgd #else
45 1.49 christos __RCSID("$NetBSD: mount_nfs.c,v 1.49 2006/03/21 21:11:41 christos Exp $");
46 1.8 cgd #endif
47 1.1 mycroft #endif /* not lint */
48 1.1 mycroft
49 1.1 mycroft #include <sys/param.h>
50 1.1 mycroft #include <sys/mount.h>
51 1.1 mycroft #include <sys/socket.h>
52 1.1 mycroft #include <sys/stat.h>
53 1.9 jtc #include <syslog.h>
54 1.1 mycroft
55 1.1 mycroft #ifdef ISO
56 1.1 mycroft #include <netiso/iso.h>
57 1.1 mycroft #endif
58 1.1 mycroft
59 1.11 fvdl #ifdef NFSKERB
60 1.44 itojun #include <des.h>
61 1.1 mycroft #include <kerberosIV/krb.h>
62 1.1 mycroft #endif
63 1.1 mycroft
64 1.1 mycroft #include <nfs/rpcv2.h>
65 1.11 fvdl #include <nfs/nfsproto.h>
66 1.1 mycroft #include <nfs/nfs.h>
67 1.1 mycroft #include <nfs/nqnfs.h>
68 1.20 fvdl #include <nfs/nfsmount.h>
69 1.1 mycroft
70 1.1 mycroft #include <arpa/inet.h>
71 1.1 mycroft
72 1.1 mycroft #include <ctype.h>
73 1.1 mycroft #include <err.h>
74 1.1 mycroft #include <errno.h>
75 1.1 mycroft #include <fcntl.h>
76 1.1 mycroft #include <netdb.h>
77 1.1 mycroft #include <signal.h>
78 1.1 mycroft #include <stdio.h>
79 1.1 mycroft #include <stdlib.h>
80 1.19 perry #include <string.h>
81 1.1 mycroft #include <unistd.h>
82 1.34 christos #include <util.h>
83 1.1 mycroft
84 1.37 jdolecek #include <mntopts.h>
85 1.1 mycroft
86 1.48 dsl #include "mount_nfs.h"
87 1.48 dsl
88 1.40 christos #define ALTF_BG 0x00000001
89 1.40 christos #define ALTF_CONN 0x00000002
90 1.40 christos #define ALTF_DUMBTIMR 0x00000004
91 1.40 christos #define ALTF_INTR 0x00000008
92 1.40 christos #define ALTF_KERB 0x00000010
93 1.40 christos #define ALTF_NFSV3 0x00000020
94 1.40 christos #define ALTF_RDIRPLUS 0x00000040
95 1.40 christos #define ALTF_MNTUDP 0x00000080
96 1.40 christos #define ALTF_NORESPORT 0x00000100
97 1.40 christos #define ALTF_SEQPACKET 0x00000200
98 1.40 christos #define ALTF_NQNFS 0x00000400
99 1.40 christos #define ALTF_SOFT 0x00000800
100 1.40 christos #define ALTF_TCP 0x00001000
101 1.40 christos #define ALTF_NFSV2 0x00002000
102 1.40 christos #define ALTF_PORT 0x00004000
103 1.40 christos #define ALTF_RSIZE 0x00008000
104 1.40 christos #define ALTF_WSIZE 0x00010000
105 1.40 christos #define ALTF_RDIRSIZE 0x00020000
106 1.40 christos #define ALTF_MAXGRPS 0x00040000
107 1.40 christos #define ALTF_LEASETERM 0x00080000
108 1.40 christos #define ALTF_READAHEAD 0x00100000
109 1.40 christos #define ALTF_DEADTHRESH 0x00200000
110 1.40 christos #define ALTF_TIMEO 0x00400000
111 1.40 christos #define ALTF_RETRANS 0x00800000
112 1.11 fvdl
113 1.29 jdolecek static const struct mntopt mopts[] = {
114 1.1 mycroft MOPT_STDOPTS,
115 1.1 mycroft MOPT_FORCE,
116 1.1 mycroft MOPT_UPDATE,
117 1.34 christos MOPT_GETARGS,
118 1.11 fvdl { "bg", 0, ALTF_BG, 1 },
119 1.14 christos { "conn", 0, ALTF_CONN, 1 },
120 1.11 fvdl { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
121 1.11 fvdl { "intr", 0, ALTF_INTR, 1 },
122 1.11 fvdl #ifdef NFSKERB
123 1.11 fvdl { "kerb", 0, ALTF_KERB, 1 },
124 1.11 fvdl #endif
125 1.11 fvdl { "nfsv3", 0, ALTF_NFSV3, 1 },
126 1.11 fvdl { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
127 1.11 fvdl { "mntudp", 0, ALTF_MNTUDP, 1 },
128 1.15 fvdl { "noresport", 0, ALTF_NORESPORT, 1 },
129 1.11 fvdl #ifdef ISO
130 1.11 fvdl { "seqpacket", 0, ALTF_SEQPACKET, 1 },
131 1.11 fvdl #endif
132 1.11 fvdl { "nqnfs", 0, ALTF_NQNFS, 1 },
133 1.11 fvdl { "soft", 0, ALTF_SOFT, 1 },
134 1.11 fvdl { "tcp", 0, ALTF_TCP, 1 },
135 1.13 fvdl { "nfsv2", 0, ALTF_NFSV2, 1 },
136 1.40 christos { "port", 0, ALTF_PORT, 1 },
137 1.40 christos { "rsize", 0, ALTF_RSIZE, 1 },
138 1.40 christos { "wsize", 0, ALTF_WSIZE, 1 },
139 1.40 christos { "rdirsize", 0, ALTF_RDIRSIZE, 1 },
140 1.40 christos { "maxgrps", 0, ALTF_MAXGRPS, 1 },
141 1.40 christos { "leaseterm", 0, ALTF_LEASETERM, 1 },
142 1.40 christos { "readahead", 0, ALTF_READAHEAD, 1 },
143 1.40 christos { "deadthresh", 0, ALTF_DEADTHRESH, 1 },
144 1.40 christos { "timeo", 0, ALTF_TIMEO, 1 },
145 1.1 mycroft { NULL }
146 1.40 christos
147 1.1 mycroft };
148 1.1 mycroft
149 1.1 mycroft struct nfs_args nfsdefargs = {
150 1.11 fvdl NFS_ARGSVERSION,
151 1.1 mycroft (struct sockaddr *)0,
152 1.1 mycroft sizeof (struct sockaddr_in),
153 1.1 mycroft SOCK_DGRAM,
154 1.1 mycroft 0,
155 1.11 fvdl (u_char *)0,
156 1.1 mycroft 0,
157 1.15 fvdl NFSMNT_NFSV3|NFSMNT_NOCONN|NFSMNT_RESVPORT,
158 1.1 mycroft NFS_WSIZE,
159 1.1 mycroft NFS_RSIZE,
160 1.11 fvdl NFS_READDIRSIZE,
161 1.11 fvdl 10,
162 1.1 mycroft NFS_RETRANS,
163 1.1 mycroft NFS_MAXGRPS,
164 1.1 mycroft NFS_DEFRAHEAD,
165 1.1 mycroft NQ_DEFLEASE,
166 1.1 mycroft NQ_DEADTHRESH,
167 1.1 mycroft (char *)0,
168 1.1 mycroft };
169 1.1 mycroft
170 1.48 dsl int retrycnt = 0;
171 1.1 mycroft int opflags = 0;
172 1.11 fvdl int nfsproto = IPPROTO_UDP;
173 1.11 fvdl int force2 = 0;
174 1.11 fvdl int force3 = 0;
175 1.25 fvdl int mnttcp_ok = 1;
176 1.40 christos int port = 0;
177 1.1 mycroft
178 1.11 fvdl #ifdef NFSKERB
179 1.29 jdolecek static char inst[INST_SZ];
180 1.29 jdolecek static char realm[REALM_SZ];
181 1.29 jdolecek static struct {
182 1.11 fvdl u_long kind;
183 1.11 fvdl KTEXT_ST kt;
184 1.11 fvdl } ktick;
185 1.29 jdolecek static struct nfsrpc_nickverf kverf;
186 1.29 jdolecek static struct nfsrpc_fullblock kin, kout;
187 1.29 jdolecek static NFSKERBKEY_T kivec;
188 1.29 jdolecek static CREDENTIALS kcr;
189 1.29 jdolecek static struct timeval ktv;
190 1.29 jdolecek static NFSKERBKEYSCHED_T kerb_keysched;
191 1.1 mycroft #endif
192 1.1 mycroft
193 1.47 xtraeme static void shownfsargs(const struct nfs_args *);
194 1.1 mycroft #ifdef ISO
195 1.47 xtraeme static struct iso_addr *iso_addr(const char *);
196 1.1 mycroft #endif
197 1.47 xtraeme int mount_nfs(int argc, char **argv);
198 1.47 xtraeme /* void set_rpc_maxgrouplist(int); */
199 1.47 xtraeme static void usage(void);
200 1.1 mycroft
201 1.29 jdolecek #ifndef MOUNT_NOMAIN
202 1.1 mycroft int
203 1.47 xtraeme main(int argc, char **argv)
204 1.29 jdolecek {
205 1.29 jdolecek return mount_nfs(argc, argv);
206 1.29 jdolecek }
207 1.29 jdolecek #endif
208 1.29 jdolecek
209 1.29 jdolecek int
210 1.47 xtraeme mount_nfs(int argc, char *argv[])
211 1.1 mycroft {
212 1.33 wrstuden int c, retval;
213 1.17 lukem struct nfs_args *nfsargsp;
214 1.1 mycroft struct nfs_args nfsargs;
215 1.1 mycroft struct nfsd_cargs ncd;
216 1.36 enami struct sockaddr_storage sa;
217 1.11 fvdl int mntflags, altflags, i, nfssvc_flag, num;
218 1.46 erh char name[MAXPATHLEN], *p, *spec, *ospec;
219 1.40 christos mntoptparse_t mp;
220 1.11 fvdl #ifdef NFSKERB
221 1.1 mycroft uid_t last_ruid;
222 1.1 mycroft
223 1.1 mycroft last_ruid = -1;
224 1.3 brezak if (krb_get_lrealm(realm, 0) != KSUCCESS)
225 1.43 itojun (void)strlcpy(realm, KRB_REALM, sizeof(realm));
226 1.11 fvdl if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
227 1.11 fvdl sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
228 1.11 fvdl ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
229 1.11 fvdl ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
230 1.35 itojun warnx("Yikes! NFSKERB structs not packed!!\n");
231 1.1 mycroft #endif
232 1.1 mycroft retrycnt = DEF_RETRY;
233 1.1 mycroft
234 1.1 mycroft mntflags = 0;
235 1.11 fvdl altflags = 0;
236 1.1 mycroft nfsargs = nfsdefargs;
237 1.1 mycroft nfsargsp = &nfsargs;
238 1.1 mycroft while ((c = getopt(argc, argv,
239 1.18 fvdl "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
240 1.1 mycroft switch (c) {
241 1.11 fvdl case '3':
242 1.11 fvdl if (force2)
243 1.11 fvdl errx(1, "-2 and -3 are mutually exclusive");
244 1.11 fvdl force3 = 1;
245 1.11 fvdl break;
246 1.11 fvdl case '2':
247 1.11 fvdl if (force3)
248 1.11 fvdl errx(1, "-2 and -3 are mutually exclusive");
249 1.11 fvdl force2 = 1;
250 1.11 fvdl nfsargsp->flags &= ~NFSMNT_NFSV3;
251 1.11 fvdl break;
252 1.1 mycroft case 'a':
253 1.1 mycroft num = strtol(optarg, &p, 10);
254 1.1 mycroft if (*p || num < 0)
255 1.1 mycroft errx(1, "illegal -a value -- %s", optarg);
256 1.1 mycroft nfsargsp->readahead = num;
257 1.1 mycroft nfsargsp->flags |= NFSMNT_READAHEAD;
258 1.1 mycroft break;
259 1.1 mycroft case 'b':
260 1.1 mycroft opflags |= BGRND;
261 1.1 mycroft break;
262 1.1 mycroft case 'c':
263 1.1 mycroft nfsargsp->flags |= NFSMNT_NOCONN;
264 1.1 mycroft break;
265 1.14 christos case 'C':
266 1.14 christos nfsargsp->flags &= ~NFSMNT_NOCONN;
267 1.14 christos break;
268 1.1 mycroft case 'D':
269 1.1 mycroft num = strtol(optarg, &p, 10);
270 1.1 mycroft if (*p || num <= 0)
271 1.1 mycroft errx(1, "illegal -D value -- %s", optarg);
272 1.1 mycroft nfsargsp->deadthresh = num;
273 1.1 mycroft nfsargsp->flags |= NFSMNT_DEADTHRESH;
274 1.1 mycroft break;
275 1.1 mycroft case 'd':
276 1.1 mycroft nfsargsp->flags |= NFSMNT_DUMBTIMR;
277 1.1 mycroft break;
278 1.1 mycroft #if 0 /* XXXX */
279 1.1 mycroft case 'g':
280 1.1 mycroft num = strtol(optarg, &p, 10);
281 1.1 mycroft if (*p || num <= 0)
282 1.1 mycroft errx(1, "illegal -g value -- %s", optarg);
283 1.1 mycroft set_rpc_maxgrouplist(num);
284 1.1 mycroft nfsargsp->maxgrouplist = num;
285 1.1 mycroft nfsargsp->flags |= NFSMNT_MAXGRPS;
286 1.1 mycroft break;
287 1.1 mycroft #endif
288 1.11 fvdl case 'I':
289 1.11 fvdl num = strtol(optarg, &p, 10);
290 1.11 fvdl if (*p || num <= 0)
291 1.11 fvdl errx(1, "illegal -I value -- %s", optarg);
292 1.11 fvdl nfsargsp->readdirsize = num;
293 1.11 fvdl nfsargsp->flags |= NFSMNT_READDIRSIZE;
294 1.11 fvdl break;
295 1.1 mycroft case 'i':
296 1.1 mycroft nfsargsp->flags |= NFSMNT_INT;
297 1.1 mycroft break;
298 1.11 fvdl #ifdef NFSKERB
299 1.1 mycroft case 'K':
300 1.1 mycroft nfsargsp->flags |= NFSMNT_KERB;
301 1.1 mycroft break;
302 1.1 mycroft #endif
303 1.1 mycroft case 'L':
304 1.1 mycroft num = strtol(optarg, &p, 10);
305 1.1 mycroft if (*p || num < 2)
306 1.1 mycroft errx(1, "illegal -L value -- %s", optarg);
307 1.1 mycroft nfsargsp->leaseterm = num;
308 1.1 mycroft nfsargsp->flags |= NFSMNT_LEASETERM;
309 1.1 mycroft break;
310 1.1 mycroft case 'l':
311 1.11 fvdl nfsargsp->flags |= NFSMNT_RDIRPLUS;
312 1.1 mycroft break;
313 1.11 fvdl #ifdef NFSKERB
314 1.1 mycroft case 'm':
315 1.43 itojun (void)strlcpy(realm, optarg, sizeof(realm));
316 1.1 mycroft break;
317 1.1 mycroft #endif
318 1.1 mycroft case 'o':
319 1.40 christos mp = getmntopts(optarg, mopts, &mntflags, &altflags);
320 1.40 christos if (mp == NULL)
321 1.49 christos err(1, "getmntopts");
322 1.35 itojun if (altflags & ALTF_BG)
323 1.11 fvdl opflags |= BGRND;
324 1.35 itojun if (altflags & ALTF_CONN)
325 1.14 christos nfsargsp->flags &= ~NFSMNT_NOCONN;
326 1.35 itojun if (altflags & ALTF_DUMBTIMR)
327 1.11 fvdl nfsargsp->flags |= NFSMNT_DUMBTIMR;
328 1.35 itojun if (altflags & ALTF_INTR)
329 1.11 fvdl nfsargsp->flags |= NFSMNT_INT;
330 1.11 fvdl #ifdef NFSKERB
331 1.35 itojun if (altflags & ALTF_KERB)
332 1.11 fvdl nfsargsp->flags |= NFSMNT_KERB;
333 1.11 fvdl #endif
334 1.35 itojun if (altflags & ALTF_NFSV3) {
335 1.13 fvdl if (force2)
336 1.35 itojun errx(1, "conflicting version options");
337 1.13 fvdl force3 = 1;
338 1.13 fvdl }
339 1.35 itojun if (altflags & ALTF_NFSV2) {
340 1.13 fvdl if (force3)
341 1.35 itojun errx(1, "conflicting version options");
342 1.13 fvdl force2 = 1;
343 1.13 fvdl nfsargsp->flags &= ~NFSMNT_NFSV3;
344 1.13 fvdl }
345 1.35 itojun if (altflags & ALTF_RDIRPLUS)
346 1.11 fvdl nfsargsp->flags |= NFSMNT_RDIRPLUS;
347 1.35 itojun if (altflags & ALTF_MNTUDP)
348 1.11 fvdl mnttcp_ok = 0;
349 1.35 itojun if (altflags & ALTF_NORESPORT)
350 1.15 fvdl nfsargsp->flags &= ~NFSMNT_RESVPORT;
351 1.11 fvdl #ifdef ISO
352 1.35 itojun if (altflags & ALTF_SEQPACKET)
353 1.11 fvdl nfsargsp->sotype = SOCK_SEQPACKET;
354 1.11 fvdl #endif
355 1.35 itojun if (altflags & ALTF_NQNFS) {
356 1.13 fvdl if (force2)
357 1.35 itojun errx(1, "nqnfs only available with v3");
358 1.13 fvdl force3 = 1;
359 1.13 fvdl nfsargsp->flags |= NFSMNT_NQNFS;
360 1.13 fvdl }
361 1.35 itojun if (altflags & ALTF_SOFT)
362 1.11 fvdl nfsargsp->flags |= NFSMNT_SOFT;
363 1.35 itojun if (altflags & ALTF_TCP) {
364 1.11 fvdl nfsargsp->sotype = SOCK_STREAM;
365 1.11 fvdl nfsproto = IPPROTO_TCP;
366 1.11 fvdl }
367 1.40 christos if (altflags & ALTF_PORT) {
368 1.40 christos port = getmntoptnum(mp, "port");
369 1.40 christos }
370 1.40 christos if (altflags & ALTF_RSIZE) {
371 1.40 christos nfsargsp->rsize =
372 1.40 christos (int)getmntoptnum(mp, "rsize");
373 1.40 christos nfsargsp->flags |= NFSMNT_RSIZE;
374 1.40 christos }
375 1.40 christos if (altflags & ALTF_WSIZE) {
376 1.42 yamt nfsargsp->wsize =
377 1.40 christos (int)getmntoptnum(mp, "wsize");
378 1.40 christos nfsargsp->flags |= NFSMNT_WSIZE;
379 1.40 christos }
380 1.40 christos if (altflags & ALTF_RDIRSIZE) {
381 1.40 christos nfsargsp->rsize =
382 1.40 christos (int)getmntoptnum(mp, "rdirsize");
383 1.40 christos nfsargsp->flags |= NFSMNT_READDIRSIZE;
384 1.40 christos }
385 1.40 christos #if 0
386 1.40 christos if (altflags & ALTF_MAXGRPS) {
387 1.40 christos set_rpc_maxgrouplist(num);
388 1.40 christos nfsargsp->maxgrouplist =
389 1.40 christos (int)getmntoptnum(mp, "maxgrps");
390 1.40 christos nfsargsp->flags |= NFSMNT_MAXGRPS;
391 1.40 christos }
392 1.40 christos #endif
393 1.40 christos if (altflags & ALTF_LEASETERM) {
394 1.40 christos nfsargsp->leaseterm =
395 1.40 christos (int)getmntoptnum(mp, "leaseterm");
396 1.40 christos nfsargsp->flags |= NFSMNT_LEASETERM;
397 1.40 christos }
398 1.40 christos if (altflags & ALTF_READAHEAD) {
399 1.40 christos nfsargsp->readahead =
400 1.40 christos (int)getmntoptnum(mp, "readahead");
401 1.40 christos nfsargsp->flags |= NFSMNT_READAHEAD;
402 1.40 christos }
403 1.40 christos if (altflags & ALTF_DEADTHRESH) {
404 1.40 christos nfsargsp->deadthresh =
405 1.40 christos (int)getmntoptnum(mp, "deadthresh");
406 1.40 christos nfsargsp->flags |= NFSMNT_DEADTHRESH;
407 1.40 christos }
408 1.40 christos if (altflags & ALTF_TIMEO) {
409 1.40 christos nfsargsp->timeo =
410 1.40 christos (int)getmntoptnum(mp, "timeo");
411 1.40 christos nfsargsp->flags |= NFSMNT_TIMEO;
412 1.40 christos }
413 1.40 christos if (altflags & ALTF_RETRANS) {
414 1.40 christos nfsargsp->retrans =
415 1.40 christos (int)getmntoptnum(mp, "retrans");
416 1.40 christos nfsargsp->flags |= NFSMNT_RETRANS;
417 1.40 christos }
418 1.11 fvdl altflags = 0;
419 1.40 christos freemntopts(mp);
420 1.1 mycroft break;
421 1.1 mycroft case 'P':
422 1.1 mycroft nfsargsp->flags |= NFSMNT_RESVPORT;
423 1.1 mycroft break;
424 1.1 mycroft case 'p':
425 1.15 fvdl nfsargsp->flags &= ~NFSMNT_RESVPORT;
426 1.1 mycroft break;
427 1.1 mycroft case 'q':
428 1.13 fvdl if (force2)
429 1.35 itojun errx(1, "nqnfs only available with v3");
430 1.13 fvdl force3 = 1;
431 1.13 fvdl nfsargsp->flags |= NFSMNT_NQNFS;
432 1.1 mycroft break;
433 1.1 mycroft case 'R':
434 1.1 mycroft num = strtol(optarg, &p, 10);
435 1.1 mycroft if (*p || num <= 0)
436 1.1 mycroft errx(1, "illegal -R value -- %s", optarg);
437 1.1 mycroft retrycnt = num;
438 1.1 mycroft break;
439 1.1 mycroft case 'r':
440 1.1 mycroft num = strtol(optarg, &p, 10);
441 1.1 mycroft if (*p || num <= 0)
442 1.1 mycroft errx(1, "illegal -r value -- %s", optarg);
443 1.1 mycroft nfsargsp->rsize = num;
444 1.1 mycroft nfsargsp->flags |= NFSMNT_RSIZE;
445 1.1 mycroft break;
446 1.15 fvdl #ifdef ISO
447 1.15 fvdl case 'S':
448 1.15 fvdl nfsargsp->sotype = SOCK_SEQPACKET;
449 1.15 fvdl break;
450 1.15 fvdl #endif
451 1.1 mycroft case 's':
452 1.1 mycroft nfsargsp->flags |= NFSMNT_SOFT;
453 1.1 mycroft break;
454 1.1 mycroft case 'T':
455 1.1 mycroft nfsargsp->sotype = SOCK_STREAM;
456 1.11 fvdl nfsproto = IPPROTO_TCP;
457 1.1 mycroft break;
458 1.1 mycroft case 't':
459 1.1 mycroft num = strtol(optarg, &p, 10);
460 1.1 mycroft if (*p || num <= 0)
461 1.1 mycroft errx(1, "illegal -t value -- %s", optarg);
462 1.1 mycroft nfsargsp->timeo = num;
463 1.1 mycroft nfsargsp->flags |= NFSMNT_TIMEO;
464 1.1 mycroft break;
465 1.1 mycroft case 'w':
466 1.1 mycroft num = strtol(optarg, &p, 10);
467 1.1 mycroft if (*p || num <= 0)
468 1.1 mycroft errx(1, "illegal -w value -- %s", optarg);
469 1.1 mycroft nfsargsp->wsize = num;
470 1.1 mycroft nfsargsp->flags |= NFSMNT_WSIZE;
471 1.1 mycroft break;
472 1.1 mycroft case 'x':
473 1.1 mycroft num = strtol(optarg, &p, 10);
474 1.1 mycroft if (*p || num <= 0)
475 1.1 mycroft errx(1, "illegal -x value -- %s", optarg);
476 1.1 mycroft nfsargsp->retrans = num;
477 1.1 mycroft nfsargsp->flags |= NFSMNT_RETRANS;
478 1.18 fvdl break;
479 1.18 fvdl case 'X':
480 1.18 fvdl nfsargsp->flags |= NFSMNT_XLATECOOKIE;
481 1.1 mycroft break;
482 1.11 fvdl case 'U':
483 1.11 fvdl mnttcp_ok = 0;
484 1.11 fvdl break;
485 1.1 mycroft default:
486 1.1 mycroft usage();
487 1.1 mycroft break;
488 1.1 mycroft }
489 1.1 mycroft argc -= optind;
490 1.1 mycroft argv += optind;
491 1.1 mycroft
492 1.1 mycroft if (argc != 2)
493 1.7 cgd usage();
494 1.1 mycroft
495 1.1 mycroft spec = *argv++;
496 1.46 erh if (realpath(*argv, name) == NULL) /* Check mounton path */
497 1.46 erh err(1, "realpath %s", *argv);
498 1.46 erh if (strncmp(*argv, name, MAXPATHLEN)) {
499 1.46 erh warnx("\"%s\" is a relative path.", *argv);
500 1.46 erh warnx("using \"%s\" instead.", name);
501 1.46 erh }
502 1.46 erh
503 1.35 itojun if ((ospec = strdup(spec)) == NULL) {
504 1.35 itojun err(1, "strdup");
505 1.23 perseant }
506 1.1 mycroft
507 1.36 enami if ((mntflags & MNT_GETARGS) != 0) {
508 1.36 enami memset(&sa, 0, sizeof(sa));
509 1.36 enami nfsargsp->addr = (struct sockaddr *)&sa;
510 1.36 enami nfsargsp->addrlen = sizeof(sa);
511 1.36 enami } else {
512 1.34 christos if (!getnfsargs(spec, nfsargsp))
513 1.34 christos exit(1);
514 1.34 christos }
515 1.33 wrstuden if ((retval = mount(MOUNT_NFS, name, mntflags, nfsargsp))) {
516 1.33 wrstuden /* Did we just default to v3 on a v2-only kernel?
517 1.33 wrstuden * If so, default to v2 & try again */
518 1.33 wrstuden if ((errno == EPROGMISMATCH) && !force3) {
519 1.33 wrstuden nfsargsp->flags &= ~NFSMNT_NFSV3;
520 1.33 wrstuden retval = mount(MOUNT_NFS, name, mntflags, nfsargsp);
521 1.33 wrstuden }
522 1.33 wrstuden }
523 1.33 wrstuden if (retval)
524 1.23 perseant err(1, "%s on %s", ospec, name);
525 1.34 christos if (mntflags & MNT_GETARGS) {
526 1.34 christos shownfsargs(nfsargsp);
527 1.36 enami return (0);
528 1.34 christos }
529 1.34 christos
530 1.1 mycroft if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
531 1.1 mycroft if ((opflags & ISBGRND) == 0) {
532 1.17 lukem if ((i = fork()) != 0) {
533 1.1 mycroft if (i == -1)
534 1.1 mycroft err(1, "nqnfs 1");
535 1.1 mycroft exit(0);
536 1.1 mycroft }
537 1.1 mycroft (void) setsid();
538 1.1 mycroft (void) close(STDIN_FILENO);
539 1.1 mycroft (void) close(STDOUT_FILENO);
540 1.1 mycroft (void) close(STDERR_FILENO);
541 1.1 mycroft (void) chdir("/");
542 1.1 mycroft }
543 1.30 lukem openlog("mount_nfs", LOG_PID, LOG_DAEMON);
544 1.1 mycroft nfssvc_flag = NFSSVC_MNTD;
545 1.1 mycroft ncd.ncd_dirp = name;
546 1.1 mycroft while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
547 1.1 mycroft if (errno != ENEEDAUTH) {
548 1.1 mycroft syslog(LOG_ERR, "nfssvc err %m");
549 1.1 mycroft continue;
550 1.1 mycroft }
551 1.1 mycroft nfssvc_flag =
552 1.1 mycroft NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
553 1.11 fvdl #ifdef NFSKERB
554 1.1 mycroft /*
555 1.1 mycroft * Set up as ncd_authuid for the kerberos call.
556 1.1 mycroft * Must set ruid to ncd_authuid and reset the
557 1.1 mycroft * ticket name iff ncd_authuid is not the same
558 1.1 mycroft * as last time, so that the right ticket file
559 1.1 mycroft * is found.
560 1.11 fvdl * Get the Kerberos credential structure so that
561 1.11 fvdl * we have the seesion key and get a ticket for
562 1.11 fvdl * this uid.
563 1.11 fvdl * For more info see the IETF Draft "Authentication
564 1.11 fvdl * in ONC RPC".
565 1.1 mycroft */
566 1.1 mycroft if (ncd.ncd_authuid != last_ruid) {
567 1.1 mycroft krb_set_tkt_string("");
568 1.1 mycroft last_ruid = ncd.ncd_authuid;
569 1.1 mycroft }
570 1.1 mycroft setreuid(ncd.ncd_authuid, 0);
571 1.11 fvdl kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
572 1.11 fvdl if (kret == RET_NOTKT) {
573 1.11 fvdl kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
574 1.11 fvdl DEFAULT_TKT_LIFE);
575 1.11 fvdl if (kret == KSUCCESS)
576 1.11 fvdl kret = krb_get_cred(NFS_KERBSRV, inst, realm,
577 1.11 fvdl &kcr);
578 1.11 fvdl }
579 1.11 fvdl if (kret == KSUCCESS)
580 1.11 fvdl kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
581 1.11 fvdl realm, 0);
582 1.11 fvdl
583 1.11 fvdl /*
584 1.11 fvdl * Fill in the AKN_FULLNAME authenticator and verfier.
585 1.11 fvdl * Along with the Kerberos ticket, we need to build
586 1.11 fvdl * the timestamp verifier and encrypt it in CBC mode.
587 1.11 fvdl */
588 1.11 fvdl if (kret == KSUCCESS &&
589 1.11 fvdl ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
590 1.11 fvdl && gettimeofday(&ktv, (struct timezone *)0) == 0) {
591 1.11 fvdl ncd.ncd_authtype = RPCAUTH_KERB4;
592 1.11 fvdl ncd.ncd_authstr = (u_char *)&ktick;
593 1.11 fvdl ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
594 1.11 fvdl 3 * NFSX_UNSIGNED;
595 1.11 fvdl ncd.ncd_verfstr = (u_char *)&kverf;
596 1.11 fvdl ncd.ncd_verflen = sizeof (kverf);
597 1.11 fvdl memmove(ncd.ncd_key, kcr.session,
598 1.11 fvdl sizeof (kcr.session));
599 1.11 fvdl kin.t1 = htonl(ktv.tv_sec);
600 1.11 fvdl kin.t2 = htonl(ktv.tv_usec);
601 1.11 fvdl kin.w1 = htonl(NFS_KERBTTL);
602 1.11 fvdl kin.w2 = htonl(NFS_KERBTTL - 1);
603 1.17 lukem memset((caddr_t)kivec, 0, sizeof (kivec));
604 1.11 fvdl
605 1.11 fvdl /*
606 1.11 fvdl * Encrypt kin in CBC mode using the session
607 1.11 fvdl * key in kcr.
608 1.11 fvdl */
609 1.11 fvdl XXX
610 1.11 fvdl
611 1.11 fvdl /*
612 1.11 fvdl * Finally, fill the timestamp verifier into the
613 1.11 fvdl * authenticator and verifier.
614 1.11 fvdl */
615 1.11 fvdl ktick.kind = htonl(RPCAKN_FULLNAME);
616 1.11 fvdl kverf.kind = htonl(RPCAKN_FULLNAME);
617 1.11 fvdl NFS_KERBW1(ktick.kt) = kout.w1;
618 1.11 fvdl ktick.kt.length = htonl(ktick.kt.length);
619 1.11 fvdl kverf.verf.t1 = kout.t1;
620 1.11 fvdl kverf.verf.t2 = kout.t2;
621 1.11 fvdl kverf.verf.w2 = kout.w2;
622 1.11 fvdl nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
623 1.1 mycroft }
624 1.1 mycroft setreuid(0, 0);
625 1.11 fvdl #endif /* NFSKERB */
626 1.1 mycroft }
627 1.1 mycroft }
628 1.1 mycroft exit(0);
629 1.34 christos }
630 1.34 christos
631 1.34 christos static void
632 1.47 xtraeme shownfsargs(const struct nfs_args *nfsargsp)
633 1.34 christos {
634 1.34 christos char fbuf[2048];
635 1.36 enami char host[NI_MAXHOST], serv[NI_MAXSERV];
636 1.36 enami int error;
637 1.34 christos
638 1.34 christos (void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
639 1.36 enami if (nfsargsp->addr != NULL) {
640 1.36 enami error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
641 1.36 enami sizeof(host), serv, sizeof(serv),
642 1.36 enami NI_NUMERICHOST | NI_NUMERICSERV);
643 1.36 enami if (error != 0)
644 1.36 enami warnx("getnameinfo: %s", gai_strerror(error));
645 1.36 enami } else
646 1.36 enami error = -1;
647 1.36 enami
648 1.36 enami if (error == 0)
649 1.39 lukem printf("addr=%s, port=%s, addrlen=%d, ",
650 1.36 enami host, serv, nfsargsp->addrlen);
651 1.38 fvdl printf("sotype=%d, proto=%d, fhsize=%d, "
652 1.34 christos "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
653 1.34 christos "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
654 1.34 christos "deadthresh=%d\n",
655 1.34 christos nfsargsp->sotype,
656 1.34 christos nfsargsp->proto,
657 1.34 christos nfsargsp->fhsize,
658 1.34 christos fbuf,
659 1.34 christos nfsargsp->wsize,
660 1.34 christos nfsargsp->rsize,
661 1.34 christos nfsargsp->readdirsize,
662 1.34 christos nfsargsp->timeo,
663 1.34 christos nfsargsp->retrans,
664 1.34 christos nfsargsp->maxgrouplist,
665 1.34 christos nfsargsp->readahead,
666 1.34 christos nfsargsp->leaseterm,
667 1.34 christos nfsargsp->deadthresh);
668 1.1 mycroft }
669 1.1 mycroft
670 1.29 jdolecek static void
671 1.47 xtraeme usage(void)
672 1.1 mycroft {
673 1.24 mjl (void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
674 1.24 mjl "[-23bcCdiKlpPqsTUX] [-a maxreadahead] [-D deadthresh]",
675 1.24 mjl "\t[-g maxgroups] [-I readdirsize] [-L leaseterm] [-m realm]",
676 1.24 mjl "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
677 1.24 mjl "\t[-w writesize] [-x retrans]",
678 1.1 mycroft "\trhost:path node");
679 1.1 mycroft exit(1);
680 1.1 mycroft }
681