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