getnfsargs.c revision 1.3 1 /* $NetBSD: getnfsargs.c,v 1.3 2005/11/12 20:30:21 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95";
44 #else
45 __RCSID("$NetBSD: getnfsargs.c,v 1.3 2005/11/12 20:30:21 dsl Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <syslog.h>
54
55 #include <rpc/rpc.h>
56 #include <rpc/pmap_clnt.h>
57 #include <rpc/pmap_prot.h>
58
59 #ifdef ISO
60 #include <netiso/iso.h>
61 #endif
62
63 #ifdef NFSKERB
64 #include <des.h>
65 #include <kerberosIV/krb.h>
66 #endif
67
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfs/nfs.h>
71 #include <nfs/nqnfs.h>
72 #include <nfs/nfsmount.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 <string.h>
85 #include <unistd.h>
86 #include <util.h>
87
88 #include "mount_nfs.h"
89
90 struct nfhret {
91 u_long stat;
92 long vers;
93 long auth;
94 long fhsize;
95 u_char nfh[NFSX_V3FHMAX];
96 };
97
98 static int xdr_dir(XDR *, char *);
99 static int xdr_fh(XDR *, struct nfhret *);
100
101 int
102 getnfsargs(char *spec, struct nfs_args *nfsargsp)
103 {
104 CLIENT *clp;
105 struct addrinfo hints, *ai_nfs, *ai;
106 int ecode;
107 char host[NI_MAXHOST], serv[NI_MAXSERV];
108 static struct netbuf nfs_nb;
109 static struct sockaddr_storage nfs_ss;
110 struct netconfig *nconf;
111 const char *netid;
112 #ifdef ISO
113 static struct sockaddr_iso isoaddr;
114 struct iso_addr *isop;
115 int isoflag = 0;
116 #endif
117 struct timeval pertry, try;
118 enum clnt_stat clnt_stat;
119 int i, nfsvers, mntvers, orgcnt;
120 char *hostp, *delimp;
121 #ifdef NFSKERB
122 char *cp;
123 #endif
124 static struct nfhret nfhret;
125 static char nam[MNAMELEN + 1];
126
127 strncpy(nam, spec, MNAMELEN);
128 nam[MNAMELEN] = '\0';
129 if ((delimp = strchr(spec, '@')) != NULL) {
130 hostp = delimp + 1;
131 } else if ((delimp = strrchr(spec, ':')) != NULL) {
132 hostp = spec;
133 spec = delimp + 1;
134 } else {
135 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
136 return (0);
137 }
138 *delimp = '\0';
139 /*
140 * DUMB!! Until the mount protocol works on iso transport, we must
141 * supply both an iso and an inet address for the host.
142 */
143 #ifdef ISO
144 if (!strncmp(hostp, "iso=", 4)) {
145 u_short isoport;
146
147 hostp += 4;
148 isoflag++;
149 if ((delimp = strchr(hostp, '+')) == NULL) {
150 warnx("no iso+inet address");
151 return (0);
152 }
153 *delimp = '\0';
154 if ((isop = iso_addr(hostp)) == NULL) {
155 warnx("bad ISO address");
156 return (0);
157 }
158 memset(&isoaddr, 0, sizeof (isoaddr));
159 memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
160 isoaddr.siso_len = sizeof (isoaddr);
161 isoaddr.siso_family = AF_ISO;
162 isoaddr.siso_tlen = 2;
163 isoport = htons(NFS_PORT);
164 memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
165 hostp = delimp + 1;
166 }
167 #endif /* ISO */
168
169 /*
170 * Handle an internet host address and reverse resolve it if
171 * doing Kerberos.
172 */
173 memset(&hints, 0, sizeof hints);
174 hints.ai_flags = AI_NUMERICHOST;
175 hints.ai_socktype = nfsargsp->sotype;
176 if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) == 0) {
177 if ((nfsargsp->flags & NFSMNT_KERB)) {
178 hints.ai_flags = 0;
179 if ((ecode = getnameinfo(ai_nfs->ai_addr,
180 ai_nfs->ai_addrlen, host, sizeof(host),
181 serv, sizeof serv, 0)) != 0) {
182 warnx("can't reverse resolve net address for "
183 "host \"%s\": %s", hostp,
184 gai_strerror(ecode));
185 return (0);
186 }
187 hostp = host;
188 }
189 } else {
190 hints.ai_flags = 0;
191 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
192 warnx("can't get net id for host \"%s\": %s", hostp,
193 gai_strerror(ecode));
194 return (0);
195 }
196 }
197 #ifdef NFSKERB
198 if (nfsargsp->flags & NFSMNT_KERB) {
199 strncpy(inst, hp->h_name, INST_SZ);
200 inst[INST_SZ - 1] = '\0';
201 if (cp = strchr(inst, '.'))
202 *cp = '\0';
203 }
204 #endif /* NFSKERB */
205
206 if (force2) {
207 nfsvers = NFS_VER2;
208 mntvers = RPCMNT_VER1;
209 } else {
210 nfsvers = NFS_VER3;
211 mntvers = RPCMNT_VER3;
212 }
213 orgcnt = retrycnt;
214 nfhret.stat = EACCES; /* Mark not yet successful */
215
216 for (ai = ai_nfs; ai; ai = ai->ai_next) {
217 /*
218 * XXX. Nead a generic (family, type, proto) -> nconf interface.
219 * __rpc_*2nconf exist, maybe they should be exported.
220 */
221 if (nfsargsp->sotype == SOCK_STREAM) {
222 if (ai->ai_family == AF_INET6)
223 netid = "tcp6";
224 else
225 netid = "tcp";
226 } else {
227 if (ai->ai_family == AF_INET6)
228 netid = "udp6";
229 else
230 netid = "udp";
231 }
232
233 nconf = getnetconfigent(netid);
234
235 tryagain:
236 retrycnt = orgcnt;
237
238 while (retrycnt > 0) {
239 nfs_nb.buf = &nfs_ss;
240 nfs_nb.maxlen = sizeof nfs_ss;
241 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
242 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
243 nfhret.stat = rpc_createerr.cf_error.re_errno;
244 break;
245 }
246 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
247 nfhret.stat = EPROTONOSUPPORT;
248 break;
249 }
250 if ((opflags & ISBGRND) == 0)
251 clnt_pcreateerror(
252 "mount_nfs: rpcbind to nfs on server");
253 } else {
254 pertry.tv_sec = 10;
255 pertry.tv_usec = 0;
256 /*
257 * XXX relies on clnt_tcp_create to bind to a reserved
258 * socket.
259 */
260 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
261 mnttcp_ok ? nconf : getnetconfigent("udp"));
262 if (clp == NULL) {
263 if ((opflags & ISBGRND) == 0) {
264 clnt_pcreateerror(
265 "Cannot MNT RPC (mountd)");
266 }
267 } else {
268 CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
269 (char *)&pertry);
270 clp->cl_auth = authsys_create_default();
271 try.tv_sec = 10;
272 try.tv_usec = 0;
273 if (nfsargsp->flags & NFSMNT_KERB)
274 nfhret.auth = RPCAUTH_KERB4;
275 else
276 nfhret.auth = RPCAUTH_UNIX;
277 nfhret.vers = mntvers;
278 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
279 xdr_dir, spec, xdr_fh, &nfhret, try);
280 switch (clnt_stat) {
281 case RPC_PROGVERSMISMATCH:
282 if (nfsvers == NFS_VER3 && !force3) {
283 nfsvers = NFS_VER2;
284 mntvers = RPCMNT_VER1;
285 nfsargsp->flags &=
286 ~NFSMNT_NFSV3;
287 goto tryagain;
288 } else {
289 errx(1, "%s", clnt_sperror(clp,
290 "MNT RPC"));
291 }
292 case RPC_SUCCESS:
293 auth_destroy(clp->cl_auth);
294 clnt_destroy(clp);
295 retrycnt = 0;
296 break;
297 default:
298 /* XXX should give up on some errors */
299 if ((opflags & ISBGRND) == 0)
300 warnx("%s", clnt_sperror(clp,
301 "bad MNT RPC"));
302 break;
303 }
304 }
305 }
306 if (--retrycnt > 0) {
307 if (opflags & BGRND) {
308 opflags &= ~BGRND;
309 if ((i = fork()) != 0) {
310 if (i == -1)
311 err(1, "nqnfs 2");
312 exit(0);
313 }
314 (void) setsid();
315 (void) close(STDIN_FILENO);
316 (void) close(STDOUT_FILENO);
317 (void) close(STDERR_FILENO);
318 (void) chdir("/");
319 opflags |= ISBGRND;
320 }
321 sleep(60);
322 }
323 }
324 if (nfhret.stat == 0)
325 break;
326 }
327 freeaddrinfo(ai_nfs);
328 if (nfhret.stat) {
329 if (opflags & ISBGRND)
330 exit(1);
331 errno = nfhret.stat;
332 warnx("can't access %s: %s", spec, strerror(nfhret.stat));
333 return (0);
334 }
335 #ifdef ISO
336 if (isoflag) {
337 nfsargsp->addr = (struct sockaddr *) &isoaddr;
338 nfsargsp->addrlen = sizeof (isoaddr);
339 } else
340 #endif /* ISO */
341 {
342 nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
343 nfsargsp->addrlen = nfs_nb.len;
344 if (port != 0) {
345 struct sockaddr *sa = nfsargsp->addr;
346 switch (sa->sa_family) {
347 case AF_INET:
348 ((struct sockaddr_in *)sa)->sin_port = port;
349 break;
350 #ifdef INET6
351 case AF_INET6:
352 ((struct sockaddr_in6 *)sa)->sin6_port = port;
353 break;
354 #endif
355 default:
356 errx(1, "Unsupported socket family %d",
357 sa->sa_family);
358 }
359 }
360 }
361 nfsargsp->fh = nfhret.nfh;
362 nfsargsp->fhsize = nfhret.fhsize;
363 nfsargsp->hostname = nam;
364 return (1);
365 }
366
367 /*
368 * xdr routines for mount rpc's
369 */
370 static int
371 xdr_dir(XDR *xdrsp, char *dirp)
372 {
373 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
374 }
375
376 static int
377 xdr_fh(XDR *xdrsp, struct nfhret *np)
378 {
379 int i;
380 long auth, authcnt, authfnd = 0;
381
382 if (!xdr_u_long(xdrsp, &np->stat))
383 return (0);
384 if (np->stat)
385 return (1);
386 switch (np->vers) {
387 case 1:
388 np->fhsize = NFSX_V2FH;
389 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
390 case 3:
391 if (!xdr_long(xdrsp, &np->fhsize))
392 return (0);
393 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
394 return (0);
395 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
396 return (0);
397 if (!xdr_long(xdrsp, &authcnt))
398 return (0);
399 for (i = 0; i < authcnt; i++) {
400 if (!xdr_long(xdrsp, &auth))
401 return (0);
402 if (auth == np->auth)
403 authfnd++;
404 }
405 /*
406 * Some servers, such as DEC's OSF/1 return a nil authenticator
407 * list to indicate RPCAUTH_UNIX.
408 */
409 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
410 np->stat = EAUTH;
411 return (1);
412 };
413 return (0);
414 }
415