getnfsargs.c revision 1.15 1 /* $NetBSD: getnfsargs.c,v 1.15 2013/03/01 18:25:17 joerg 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\
38 The Regents of the University of California. All rights reserved.");
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.15 2013/03/01 18:25:17 joerg 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 #include <nfs/rpcv2.h>
60 #include <nfs/nfsproto.h>
61 #include <nfs/nfs.h>
62 #include <nfs/nfsmount.h>
63
64 #include <arpa/inet.h>
65
66 #include <err.h>
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <netdb.h>
70 #include <signal.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include <util.h>
76
77 #include "mount_nfs.h"
78
79 struct nfhret {
80 u_long stat;
81 long vers;
82 long auth;
83 long fhsize;
84 u_char nfh[NFSX_V3FHMAX];
85 };
86
87 static int xdr_dir(XDR *, char *);
88 static int xdr_fh(XDR *, struct nfhret *);
89
90 #ifndef MOUNTNFS_RETRYRPC
91 #define MOUNTNFS_RETRYRPC 60
92 #endif
93
94 int
95 getnfsargs(char *spec, struct nfs_args *nfsargsp)
96 {
97 CLIENT *clp;
98 struct addrinfo hints, *ai_nfs, *ai;
99 int ecode;
100 static struct netbuf nfs_nb;
101 static struct sockaddr_storage nfs_ss;
102 struct netconfig *nconf;
103 const char *netid;
104 struct timeval pertry, try;
105 enum clnt_stat clnt_stat;
106 int i, nfsvers, mntvers;
107 int retryleft;
108 char *hostp, *delimp;
109 static struct nfhret nfhret;
110 static char nam[MNAMELEN + 1];
111
112 strlcpy(nam, spec, sizeof(nam));
113 if ((delimp = strchr(spec, '@')) != NULL) {
114 hostp = delimp + 1;
115 } else if ((delimp = strrchr(spec, ':')) != NULL) {
116 hostp = spec;
117 spec = delimp + 1;
118 } else {
119 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
120 return (0);
121 }
122 *delimp = '\0';
123
124 /*
125 * Handle an internet host address.
126 */
127 memset(&hints, 0, sizeof hints);
128 hints.ai_flags = AI_NUMERICHOST;
129 hints.ai_socktype = nfsargsp->sotype;
130 if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) != 0) {
131 hints.ai_flags = 0;
132 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
133 warnx("can't get net id for host \"%s\": %s", hostp,
134 gai_strerror(ecode));
135 return (0);
136 }
137 }
138
139 if ((nfsargsp->flags & NFSMNT_NFSV3) != 0) {
140 nfsvers = NFS_VER3;
141 mntvers = RPCMNT_VER3;
142 } else {
143 nfsvers = NFS_VER2;
144 mntvers = RPCMNT_VER1;
145 }
146 nfhret.stat = EACCES; /* Mark not yet successful */
147
148 for (ai = ai_nfs; ai; ai = ai->ai_next) {
149 /*
150 * XXX. Need a generic (family, type, proto) -> nconf interface.
151 * __rpc_*2nconf exist, maybe they should be exported.
152 */
153 if (nfsargsp->sotype == SOCK_STREAM) {
154 if (ai->ai_family == AF_INET6)
155 netid = "tcp6";
156 else
157 netid = "tcp";
158 } else {
159 if (ai->ai_family == AF_INET6)
160 netid = "udp6";
161 else
162 netid = "udp";
163 }
164
165 nconf = getnetconfigent(netid);
166
167 tryagain:
168 retryleft = retrycnt;
169
170 while (retryleft > 0) {
171 nfs_nb.buf = &nfs_ss;
172 nfs_nb.maxlen = sizeof nfs_ss;
173 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
174 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
175 nfhret.stat = rpc_createerr.cf_error.re_errno;
176 break;
177 }
178 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
179 nfhret.stat = EPROTONOSUPPORT;
180 break;
181 }
182 if ((opflags & ISBGRND) == 0) {
183 char buf[64];
184
185 snprintf(buf, sizeof(buf),
186 "%s: rpcbind to nfs on server",
187 getprogname());
188 clnt_pcreateerror(buf);
189 }
190 } else {
191 pertry.tv_sec = 30;
192 pertry.tv_usec = 0;
193 /*
194 * XXX relies on clnt_tcp_create to bind to a reserved
195 * socket.
196 */
197 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
198 mnttcp_ok ? nconf : getnetconfigent("udp"));
199 if (clp == NULL) {
200 if ((opflags & ISBGRND) == 0) {
201 clnt_pcreateerror(
202 "Cannot MNT RPC (mountd)");
203 }
204 } else {
205 CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
206 (char *)&pertry);
207 clp->cl_auth = authsys_create_default();
208 try.tv_sec = 30;
209 try.tv_usec = 0;
210 nfhret.auth = RPCAUTH_UNIX;
211 nfhret.vers = mntvers;
212 clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
213 xdr_dir, spec, xdr_fh, &nfhret, try);
214 switch (clnt_stat) {
215 case RPC_PROGVERSMISMATCH:
216 if (nfsvers == NFS_VER3 && !force3) {
217 nfsvers = NFS_VER2;
218 mntvers = RPCMNT_VER1;
219 nfsargsp->flags &=
220 ~NFSMNT_NFSV3;
221 goto tryagain;
222 } else {
223 errx(1, "%s", clnt_sperror(clp,
224 "MNT RPC"));
225 }
226 case RPC_SUCCESS:
227 auth_destroy(clp->cl_auth);
228 clnt_destroy(clp);
229 retryleft = 0;
230 break;
231 default:
232 /* XXX should give up on some errors */
233 if ((opflags & ISBGRND) == 0)
234 warnx("%s", clnt_sperror(clp,
235 "bad MNT RPC"));
236 break;
237 }
238 }
239 }
240 if (--retryleft > 0) {
241 if (opflags & BGRND) {
242 opflags &= ~BGRND;
243 if ((i = fork()) != 0) {
244 if (i == -1)
245 err(1, "fork");
246 exit(0);
247 }
248 (void) setsid();
249 (void) close(STDIN_FILENO);
250 (void) close(STDOUT_FILENO);
251 (void) close(STDERR_FILENO);
252 (void) chdir("/");
253 opflags |= ISBGRND;
254 }
255 sleep(MOUNTNFS_RETRYRPC);
256 }
257 }
258 if (nfhret.stat == 0)
259 break;
260 }
261 freeaddrinfo(ai_nfs);
262 if (nfhret.stat) {
263 if (opflags & ISBGRND)
264 exit(1);
265 errno = nfhret.stat;
266 warnx("can't access %s: %s", spec, strerror(nfhret.stat));
267 return (0);
268 } else {
269 nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
270 nfsargsp->addrlen = nfs_nb.len;
271 if (port != 0) {
272 struct sockaddr *sa = nfsargsp->addr;
273 switch (sa->sa_family) {
274 case AF_INET:
275 ((struct sockaddr_in *)sa)->sin_port = port;
276 break;
277 #ifdef INET6
278 case AF_INET6:
279 ((struct sockaddr_in6 *)sa)->sin6_port = port;
280 break;
281 #endif
282 default:
283 errx(1, "Unsupported socket family %d",
284 sa->sa_family);
285 }
286 }
287 }
288 nfsargsp->fh = nfhret.nfh;
289 nfsargsp->fhsize = nfhret.fhsize;
290 nfsargsp->hostname = nam;
291 return (1);
292 }
293
294 /*
295 * xdr routines for mount rpc's
296 */
297 static int
298 xdr_dir(XDR *xdrsp, char *dirp)
299 {
300 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
301 }
302
303 static int
304 xdr_fh(XDR *xdrsp, struct nfhret *np)
305 {
306 int i;
307 long auth, authcnt, authfnd = 0;
308
309 if (!xdr_u_long(xdrsp, &np->stat))
310 return (0);
311 if (np->stat)
312 return (1);
313 switch (np->vers) {
314 case 1:
315 np->fhsize = NFSX_V2FH;
316 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
317 case 3:
318 if (!xdr_long(xdrsp, &np->fhsize))
319 return (0);
320 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
321 return (0);
322 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
323 return (0);
324 if (!xdr_long(xdrsp, &authcnt))
325 return (0);
326 for (i = 0; i < authcnt; i++) {
327 if (!xdr_long(xdrsp, &auth))
328 return (0);
329 if (auth == np->auth)
330 authfnd++;
331 }
332 /*
333 * Some servers, such as DEC's OSF/1 return a nil authenticator
334 * list to indicate RPCAUTH_UNIX.
335 */
336 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
337 np->stat = EAUTH;
338 return (1);
339 };
340 return (0);
341 }
342