getnfsargs_small.c revision 1.5 1 /* $NetBSD: getnfsargs_small.c,v 1.5 2006/07/07 17:25:01 hubertf Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Laight.
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 NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*-
36 * Copyright (c) 1993 John Brezak
37 * All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. The name of the author may not be used to endorse or promote products
48 * derived from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
54 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
58 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
59 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 */
62
63 /*
64 * Copyright (c) 1992, 1993, 1994
65 * The Regents of the University of California. All rights reserved.
66 *
67 * This code is derived from software contributed to Berkeley by
68 * Rick Macklem at The University of Guelph.
69 *
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions
72 * are met:
73 * 1. Redistributions of source code must retain the above copyright
74 * notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright
76 * notice, this list of conditions and the following disclaimer in the
77 * documentation and/or other materials provided with the distribution.
78 * 3. Neither the name of the University nor the names of its contributors
79 * may be used to endorse or promote products derived from this software
80 * without specific prior written permission.
81 *
82 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 * SUCH DAMAGE.
93 */
94
95 #include <sys/cdefs.h>
96 __RCSID("$NetBSD: getnfsargs_small.c,v 1.5 2006/07/07 17:25:01 hubertf Exp $");
97
98 #include <sys/param.h>
99 #include <sys/mount.h>
100 #include <sys/socket.h>
101 #include <sys/stat.h>
102 #include <syslog.h>
103
104 #include <nfs/rpcv2.h>
105 #include <nfs/nfsproto.h>
106 #include <nfs/nfs.h>
107 #include <nfs/nqnfs.h>
108 #include <nfs/nfsmount.h>
109
110 #include <arpa/inet.h>
111
112 #include <ctype.h>
113 #include <err.h>
114 #include <errno.h>
115 #include <fcntl.h>
116 #include <netdb.h>
117 #include <signal.h>
118 #include <stdio.h>
119 #include <stdlib.h>
120 #include <string.h>
121 #include <unistd.h>
122 #include <util.h>
123
124 #include "mount_nfs.h"
125
126 #include "iodesc.h"
127 typedef int32_t n_long;
128 #include "rpc.h"
129
130 #define RPC_HEADER_WORDS 28 /* more than enough */
131 #define FNAME_SIZE 512
132
133 struct nfhret {
134 long fhsize;
135 u_char nfh[NFSX_V3FHMAX];
136 };
137
138 /* Ripped from src/sys/arch/i386/stand/libsa/nfs.c */
139 static int
140 nfs_getrootfh(struct iodesc *d, const char *path, int mntvers, struct nfhret *nfhret)
141 {
142 size_t len;
143 struct args {
144 uint32_t len;
145 char path[FNAME_SIZE];
146 } *args;
147 struct repl {
148 uint32_t errval;
149 u_char fh[NFSX_V3FHMAX];
150 } *repl;
151 struct {
152 uint32_t h[RPC_HEADER_WORDS];
153 struct args d;
154 } sdata;
155 struct {
156 uint32_t h[RPC_HEADER_WORDS];
157 struct repl d;
158 } rdata;
159 ssize_t cc;
160
161 args = &sdata.d;
162 repl = &rdata.d;
163
164 memset(args, 0, sizeof(*args));
165 len = strlen(path);
166 if (len > sizeof(args->path))
167 len = sizeof(args->path);
168 args->len = htonl(len);
169 memcpy(args->path, path, len);
170 len = 4 + roundup(len, 4);
171
172 cc = rpc_call(d, RPCPROG_MNT, mntvers, RPCMNT_MOUNT,
173 args, len, repl, sizeof(*repl));
174 if (cc == -1) {
175 /* errno was set by rpc_call */
176 return errno;
177 }
178 if (cc < 4)
179 return EBADRPC;
180 if (repl->errval)
181 return ntohl(repl->errval);
182 nfhret->fhsize = cc;
183 memcpy(nfhret->nfh, repl->fh, sizeof(repl->fh));
184 return 0;
185 }
186
187 int
188 getnfsargs(char *spec, struct nfs_args *nfsargsp)
189 {
190 struct addrinfo hints, *ai_nfs;
191 int ecode;
192 int nfsvers, mntvers;
193 char *hostp, *delimp;
194 static struct nfhret nfhret;
195 static char nam[MNAMELEN + 1];
196 struct iodesc d;
197 int nfs_port;
198
199 strlcpy(nam, spec, sizeof(nam));
200 if ((delimp = strchr(spec, '@')) != NULL) {
201 hostp = delimp + 1;
202 } else if ((delimp = strrchr(spec, ':')) != NULL) {
203 hostp = spec;
204 spec = delimp + 1;
205 } else {
206 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
207 return 0;
208 }
209 *delimp = '\0';
210
211 memset(&hints, 0, sizeof hints);
212 hints.ai_socktype = nfsargsp->sotype;
213
214 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
215 warnx("can't get net id for host \"%s\": %s", hostp,
216 gai_strerror(ecode));
217 return 0;
218 }
219
220 if ((nfsargsp->flags & NFSMNT_NFSV3) != 0) {
221 nfsvers = NFS_VER3;
222 mntvers = RPCMNT_VER3;
223 } else {
224 nfsvers = NFS_VER2;
225 mntvers = RPCMNT_VER1;
226 }
227
228 d.socket = -1;
229 for (d.ai = ai_nfs; ; d.ai = d.ai->ai_next) {
230 if (d.ai == NULL) {
231 if (nfsvers == NFS_VER3 && !force3) {
232 nfsvers = NFS_VER2;
233 mntvers = RPCMNT_VER1;
234 d.ai = ai_nfs;
235 continue;
236 }
237 return 0;
238 }
239 nfs_port = rpc_getport(&d, RPCPROG_NFS, nfsvers);
240 if (nfs_port == -1)
241 continue;
242 if (nfs_getrootfh(&d, spec, mntvers, &nfhret) == 0)
243 break;
244 }
245
246 if (port != 0)
247 nfs_port = port;
248 set_port(d.ai->ai_addr, htons(nfs_port));
249
250 nfsargsp->hostname = nam;
251 nfsargsp->addr = d.ai->ai_addr;
252 nfsargsp->addrlen = d.ai->ai_addrlen;
253
254 nfsargsp->fh = nfhret.nfh;
255 if (nfsvers == NFS_VER3) {
256 nfsargsp->fhsize = ntohl(*(uint32_t *)nfhret.nfh);
257 nfsargsp->fh += 4;
258 } else {
259 nfsargsp->fhsize = NFSX_V2FH;
260 nfsargsp->flags &= ~NFSMNT_NFSV3;
261 }
262 return 1;
263 }
264