getnfsargs_small.c revision 1.6 1 /* $NetBSD: getnfsargs_small.c,v 1.6 2006/12/27 12:13:55 yamt 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.6 2006/12/27 12:13:55 yamt 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/nfsmount.h>
108
109 #include <arpa/inet.h>
110
111 #include <ctype.h>
112 #include <err.h>
113 #include <errno.h>
114 #include <fcntl.h>
115 #include <netdb.h>
116 #include <signal.h>
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <string.h>
120 #include <unistd.h>
121 #include <util.h>
122
123 #include "mount_nfs.h"
124
125 #include "iodesc.h"
126 typedef int32_t n_long;
127 #include "rpc.h"
128
129 #define RPC_HEADER_WORDS 28 /* more than enough */
130 #define FNAME_SIZE 512
131
132 struct nfhret {
133 long fhsize;
134 u_char nfh[NFSX_V3FHMAX];
135 };
136
137 /* Ripped from src/sys/arch/i386/stand/libsa/nfs.c */
138 static int
139 nfs_getrootfh(struct iodesc *d, const char *path, int mntvers, struct nfhret *nfhret)
140 {
141 size_t len;
142 struct args {
143 uint32_t len;
144 char path[FNAME_SIZE];
145 } *args;
146 struct repl {
147 uint32_t errval;
148 u_char fh[NFSX_V3FHMAX];
149 } *repl;
150 struct {
151 uint32_t h[RPC_HEADER_WORDS];
152 struct args d;
153 } sdata;
154 struct {
155 uint32_t h[RPC_HEADER_WORDS];
156 struct repl d;
157 } rdata;
158 ssize_t cc;
159
160 args = &sdata.d;
161 repl = &rdata.d;
162
163 memset(args, 0, sizeof(*args));
164 len = strlen(path);
165 if (len > sizeof(args->path))
166 len = sizeof(args->path);
167 args->len = htonl(len);
168 memcpy(args->path, path, len);
169 len = 4 + roundup(len, 4);
170
171 cc = rpc_call(d, RPCPROG_MNT, mntvers, RPCMNT_MOUNT,
172 args, len, repl, sizeof(*repl));
173 if (cc == -1) {
174 /* errno was set by rpc_call */
175 return errno;
176 }
177 if (cc < 4)
178 return EBADRPC;
179 if (repl->errval)
180 return ntohl(repl->errval);
181 nfhret->fhsize = cc;
182 memcpy(nfhret->nfh, repl->fh, sizeof(repl->fh));
183 return 0;
184 }
185
186 int
187 getnfsargs(char *spec, struct nfs_args *nfsargsp)
188 {
189 struct addrinfo hints, *ai_nfs;
190 int ecode;
191 int nfsvers, mntvers;
192 char *hostp, *delimp;
193 static struct nfhret nfhret;
194 static char nam[MNAMELEN + 1];
195 struct iodesc d;
196 int nfs_port;
197
198 strlcpy(nam, spec, sizeof(nam));
199 if ((delimp = strchr(spec, '@')) != NULL) {
200 hostp = delimp + 1;
201 } else if ((delimp = strrchr(spec, ':')) != NULL) {
202 hostp = spec;
203 spec = delimp + 1;
204 } else {
205 warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
206 return 0;
207 }
208 *delimp = '\0';
209
210 memset(&hints, 0, sizeof hints);
211 hints.ai_socktype = nfsargsp->sotype;
212
213 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
214 warnx("can't get net id for host \"%s\": %s", hostp,
215 gai_strerror(ecode));
216 return 0;
217 }
218
219 if ((nfsargsp->flags & NFSMNT_NFSV3) != 0) {
220 nfsvers = NFS_VER3;
221 mntvers = RPCMNT_VER3;
222 } else {
223 nfsvers = NFS_VER2;
224 mntvers = RPCMNT_VER1;
225 }
226
227 d.socket = -1;
228 for (d.ai = ai_nfs; ; d.ai = d.ai->ai_next) {
229 if (d.ai == NULL) {
230 if (nfsvers == NFS_VER3 && !force3) {
231 nfsvers = NFS_VER2;
232 mntvers = RPCMNT_VER1;
233 d.ai = ai_nfs;
234 continue;
235 }
236 return 0;
237 }
238 nfs_port = rpc_getport(&d, RPCPROG_NFS, nfsvers);
239 if (nfs_port == -1)
240 continue;
241 if (nfs_getrootfh(&d, spec, mntvers, &nfhret) == 0)
242 break;
243 }
244
245 if (port != 0)
246 nfs_port = port;
247 set_port(d.ai->ai_addr, htons(nfs_port));
248
249 nfsargsp->hostname = nam;
250 nfsargsp->addr = d.ai->ai_addr;
251 nfsargsp->addrlen = d.ai->ai_addrlen;
252
253 nfsargsp->fh = nfhret.nfh;
254 if (nfsvers == NFS_VER3) {
255 nfsargsp->fhsize = ntohl(*(uint32_t *)nfhret.nfh);
256 nfsargsp->fh += 4;
257 } else {
258 nfsargsp->fhsize = NFSX_V2FH;
259 nfsargsp->flags &= ~NFSMNT_NFSV3;
260 }
261 return 1;
262 }
263