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