Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / sbin / mount_nfs / getnfsargs.c
blobdbb45d9005c413fcccd3be2585e8a1bd0c6c6027
1 /* $NetBSD: getnfsargs.c,v 1.12 2008/10/15 19:06:45 pooka Exp $ */
3 /*
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
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.
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.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
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.12 2008/10/15 19:06:45 pooka Exp $");
46 #endif
47 #endif /* not lint */
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/socket.h>
52 #include <sys/stat.h>
53 #include <syslog.h>
55 #include <rpc/rpc.h>
56 #include <rpc/pmap_clnt.h>
57 #include <rpc/pmap_prot.h>
59 #ifdef ISO
60 #include <netiso/iso.h>
61 #endif
64 #include <nfs/rpcv2.h>
65 #include <nfs/nfsproto.h>
66 #include <nfs/nfs.h>
67 #include <nfs/nfsmount.h>
69 #include <arpa/inet.h>
71 #include <err.h>
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <netdb.h>
75 #include <signal.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
79 #include <unistd.h>
80 #include <util.h>
82 #include "mount_nfs.h"
84 struct nfhret {
85 u_long stat;
86 long vers;
87 long auth;
88 long fhsize;
89 u_char nfh[NFSX_V3FHMAX];
92 static int xdr_dir(XDR *, char *);
93 static int xdr_fh(XDR *, struct nfhret *);
95 int
96 getnfsargs(char *spec, struct nfs_args *nfsargsp)
98 CLIENT *clp;
99 struct addrinfo hints, *ai_nfs, *ai;
100 int ecode;
101 static struct netbuf nfs_nb;
102 static struct sockaddr_storage nfs_ss;
103 struct netconfig *nconf;
104 const char *netid;
105 #ifdef ISO
106 static struct sockaddr_iso isoaddr;
107 struct iso_addr *isop;
108 int isoflag = 0;
109 #endif
110 struct timeval pertry, try;
111 enum clnt_stat clnt_stat;
112 int i, nfsvers, mntvers;
113 int retryleft;
114 char *hostp, *delimp;
115 static struct nfhret nfhret;
116 static char nam[MNAMELEN + 1];
118 strlcpy(nam, spec, sizeof(nam));
119 if ((delimp = strchr(spec, '@')) != NULL) {
120 hostp = delimp + 1;
121 } else if ((delimp = strrchr(spec, ':')) != NULL) {
122 hostp = spec;
123 spec = delimp + 1;
124 } else {
125 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
126 return (0);
128 *delimp = '\0';
130 * DUMB!! Until the mount protocol works on iso transport, we must
131 * supply both an iso and an inet address for the host.
133 #ifdef ISO
134 if (!strncmp(hostp, "iso=", 4)) {
135 u_short isoport;
137 hostp += 4;
138 isoflag++;
139 if ((delimp = strchr(hostp, '+')) == NULL) {
140 warnx("no iso+inet address");
141 return (0);
143 *delimp = '\0';
144 if ((isop = iso_addr(hostp)) == NULL) {
145 warnx("bad ISO address");
146 return (0);
148 memset(&isoaddr, 0, sizeof (isoaddr));
149 memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
150 isoaddr.siso_len = sizeof (isoaddr);
151 isoaddr.siso_family = AF_ISO;
152 isoaddr.siso_tlen = 2;
153 isoport = htons(NFS_PORT);
154 memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
155 hostp = delimp + 1;
157 #endif /* ISO */
160 * Handle an internet host address.
162 memset(&hints, 0, sizeof hints);
163 hints.ai_flags = AI_NUMERICHOST;
164 hints.ai_socktype = nfsargsp->sotype;
165 if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) != 0) {
166 hints.ai_flags = 0;
167 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
168 warnx("can't get net id for host \"%s\": %s", hostp,
169 gai_strerror(ecode));
170 return (0);
174 if ((nfsargsp->flags & NFSMNT_NFSV3) != 0) {
175 nfsvers = NFS_VER3;
176 mntvers = RPCMNT_VER3;
177 } else {
178 nfsvers = NFS_VER2;
179 mntvers = RPCMNT_VER1;
181 nfhret.stat = EACCES; /* Mark not yet successful */
183 for (ai = ai_nfs; ai; ai = ai->ai_next) {
185 * XXX. Need a generic (family, type, proto) -> nconf interface.
186 * __rpc_*2nconf exist, maybe they should be exported.
188 if (nfsargsp->sotype == SOCK_STREAM) {
189 if (ai->ai_family == AF_INET6)
190 netid = "tcp6";
191 else
192 netid = "tcp";
193 } else {
194 if (ai->ai_family == AF_INET6)
195 netid = "udp6";
196 else
197 netid = "udp";
200 nconf = getnetconfigent(netid);
202 tryagain:
203 retryleft = retrycnt;
205 while (retryleft > 0) {
206 nfs_nb.buf = &nfs_ss;
207 nfs_nb.maxlen = sizeof nfs_ss;
208 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
209 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
210 nfhret.stat = rpc_createerr.cf_error.re_errno;
211 break;
213 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
214 nfhret.stat = EPROTONOSUPPORT;
215 break;
217 if ((opflags & ISBGRND) == 0) {
218 char buf[64];
220 snprintf(buf, sizeof(buf),
221 "%s: rpcbind to nfs on server",
222 getprogname());
223 clnt_pcreateerror(buf);
225 } else {
226 pertry.tv_sec = 30;
227 pertry.tv_usec = 0;
229 * XXX relies on clnt_tcp_create to bind to a reserved
230 * socket.
232 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
233 mnttcp_ok ? nconf : getnetconfigent("udp"));
234 if (clp == NULL) {
235 if ((opflags & ISBGRND) == 0) {
236 clnt_pcreateerror(
237 "Cannot MNT RPC (mountd)");
239 } else {
240 CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
241 (char *)&pertry);
242 clp->cl_auth = authsys_create_default();
243 try.tv_sec = 30;
244 try.tv_usec = 0;
245 nfhret.auth = RPCAUTH_UNIX;
246 nfhret.vers = mntvers;
247 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
248 xdr_dir, spec, xdr_fh, &nfhret, try);
249 switch (clnt_stat) {
250 case RPC_PROGVERSMISMATCH:
251 if (nfsvers == NFS_VER3 && !force3) {
252 nfsvers = NFS_VER2;
253 mntvers = RPCMNT_VER1;
254 nfsargsp->flags &=
255 ~NFSMNT_NFSV3;
256 goto tryagain;
257 } else {
258 errx(1, "%s", clnt_sperror(clp,
259 "MNT RPC"));
261 case RPC_SUCCESS:
262 auth_destroy(clp->cl_auth);
263 clnt_destroy(clp);
264 retryleft = 0;
265 break;
266 default:
267 /* XXX should give up on some errors */
268 if ((opflags & ISBGRND) == 0)
269 warnx("%s", clnt_sperror(clp,
270 "bad MNT RPC"));
271 break;
275 if (--retryleft > 0) {
276 if (opflags & BGRND) {
277 opflags &= ~BGRND;
278 if ((i = fork()) != 0) {
279 if (i == -1)
280 err(1, "fork");
281 exit(0);
283 (void) setsid();
284 (void) close(STDIN_FILENO);
285 (void) close(STDOUT_FILENO);
286 (void) close(STDERR_FILENO);
287 (void) chdir("/");
288 opflags |= ISBGRND;
290 sleep(60);
293 if (nfhret.stat == 0)
294 break;
296 freeaddrinfo(ai_nfs);
297 if (nfhret.stat) {
298 if (opflags & ISBGRND)
299 exit(1);
300 errno = nfhret.stat;
301 warnx("can't access %s: %s", spec, strerror(nfhret.stat));
302 return (0);
304 #ifdef ISO
305 if (isoflag) {
306 nfsargsp->addr = (struct sockaddr *) &isoaddr;
307 nfsargsp->addrlen = sizeof (isoaddr);
308 } else
309 #endif /* ISO */
311 nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
312 nfsargsp->addrlen = nfs_nb.len;
313 if (port != 0) {
314 struct sockaddr *sa = nfsargsp->addr;
315 switch (sa->sa_family) {
316 case AF_INET:
317 ((struct sockaddr_in *)sa)->sin_port = port;
318 break;
319 #ifdef INET6
320 case AF_INET6:
321 ((struct sockaddr_in6 *)sa)->sin6_port = port;
322 break;
323 #endif
324 default:
325 errx(1, "Unsupported socket family %d",
326 sa->sa_family);
330 nfsargsp->fh = nfhret.nfh;
331 nfsargsp->fhsize = nfhret.fhsize;
332 nfsargsp->hostname = nam;
333 return (1);
337 * xdr routines for mount rpc's
339 static int
340 xdr_dir(XDR *xdrsp, char *dirp)
342 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
345 static int
346 xdr_fh(XDR *xdrsp, struct nfhret *np)
348 int i;
349 long auth, authcnt, authfnd = 0;
351 if (!xdr_u_long(xdrsp, &np->stat))
352 return (0);
353 if (np->stat)
354 return (1);
355 switch (np->vers) {
356 case 1:
357 np->fhsize = NFSX_V2FH;
358 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
359 case 3:
360 if (!xdr_long(xdrsp, &np->fhsize))
361 return (0);
362 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
363 return (0);
364 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
365 return (0);
366 if (!xdr_long(xdrsp, &authcnt))
367 return (0);
368 for (i = 0; i < authcnt; i++) {
369 if (!xdr_long(xdrsp, &auth))
370 return (0);
371 if (auth == np->auth)
372 authfnd++;
375 * Some servers, such as DEC's OSF/1 return a nil authenticator
376 * list to indicate RPCAUTH_UNIX.
378 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
379 np->stat = EAUTH;
380 return (1);
382 return (0);