1*18c826e1Schristos /* $NetBSD: getnfsargs_small.c,v 1.10 2013/06/29 22:56:26 christos Exp $ */
2a16ca184Sdsl
3a16ca184Sdsl /*-
4a16ca184Sdsl * Copyright (c) 2006 The NetBSD Foundation, Inc.
5a16ca184Sdsl * All rights reserved.
6a16ca184Sdsl *
7a16ca184Sdsl * This code is derived from software contributed to The NetBSD Foundation
8a16ca184Sdsl * by David Laight.
9a16ca184Sdsl *
10a16ca184Sdsl * Redistribution and use in source and binary forms, with or without
11a16ca184Sdsl * modification, are permitted provided that the following conditions
12a16ca184Sdsl * are met:
13a16ca184Sdsl * 1. Redistributions of source code must retain the above copyright
14a16ca184Sdsl * notice, this list of conditions and the following disclaimer.
15a16ca184Sdsl * 2. Redistributions in binary form must reproduce the above copyright
16a16ca184Sdsl * notice, this list of conditions and the following disclaimer in the
17a16ca184Sdsl * documentation and/or other materials provided with the distribution.
18a16ca184Sdsl *
19a16ca184Sdsl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a16ca184Sdsl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a16ca184Sdsl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a16ca184Sdsl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a16ca184Sdsl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a16ca184Sdsl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a16ca184Sdsl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a16ca184Sdsl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a16ca184Sdsl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a16ca184Sdsl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a16ca184Sdsl * POSSIBILITY OF SUCH DAMAGE.
30a16ca184Sdsl */
31a16ca184Sdsl
32248ca69bSyamt /*-
33248ca69bSyamt * Copyright (c) 1993 John Brezak
34248ca69bSyamt * All rights reserved.
35248ca69bSyamt *
36248ca69bSyamt * Redistribution and use in source and binary forms, with or without
37248ca69bSyamt * modification, are permitted provided that the following conditions
38248ca69bSyamt * are met:
39248ca69bSyamt * 1. Redistributions of source code must retain the above copyright
40248ca69bSyamt * notice, this list of conditions and the following disclaimer.
41248ca69bSyamt * 2. Redistributions in binary form must reproduce the above copyright
42248ca69bSyamt * notice, this list of conditions and the following disclaimer in the
43248ca69bSyamt * documentation and/or other materials provided with the distribution.
44248ca69bSyamt * 3. The name of the author may not be used to endorse or promote products
45248ca69bSyamt * derived from this software without specific prior written permission.
46248ca69bSyamt *
47248ca69bSyamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
48248ca69bSyamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
49248ca69bSyamt * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50248ca69bSyamt * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
51248ca69bSyamt * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
52248ca69bSyamt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53248ca69bSyamt * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54248ca69bSyamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
55248ca69bSyamt * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
56248ca69bSyamt * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57248ca69bSyamt * POSSIBILITY OF SUCH DAMAGE.
58248ca69bSyamt */
59248ca69bSyamt
60248ca69bSyamt /*
61248ca69bSyamt * Copyright (c) 1992, 1993, 1994
62248ca69bSyamt * The Regents of the University of California. All rights reserved.
63248ca69bSyamt *
64248ca69bSyamt * This code is derived from software contributed to Berkeley by
65248ca69bSyamt * Rick Macklem at The University of Guelph.
66248ca69bSyamt *
67248ca69bSyamt * Redistribution and use in source and binary forms, with or without
68248ca69bSyamt * modification, are permitted provided that the following conditions
69248ca69bSyamt * are met:
70248ca69bSyamt * 1. Redistributions of source code must retain the above copyright
71248ca69bSyamt * notice, this list of conditions and the following disclaimer.
72248ca69bSyamt * 2. Redistributions in binary form must reproduce the above copyright
73248ca69bSyamt * notice, this list of conditions and the following disclaimer in the
74248ca69bSyamt * documentation and/or other materials provided with the distribution.
75248ca69bSyamt * 3. Neither the name of the University nor the names of its contributors
76248ca69bSyamt * may be used to endorse or promote products derived from this software
77248ca69bSyamt * without specific prior written permission.
78248ca69bSyamt *
79248ca69bSyamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80248ca69bSyamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81248ca69bSyamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82248ca69bSyamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83248ca69bSyamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84248ca69bSyamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85248ca69bSyamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86248ca69bSyamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87248ca69bSyamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88248ca69bSyamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89248ca69bSyamt * SUCH DAMAGE.
90248ca69bSyamt */
91248ca69bSyamt
92a16ca184Sdsl #include <sys/cdefs.h>
93*18c826e1Schristos __RCSID("$NetBSD: getnfsargs_small.c,v 1.10 2013/06/29 22:56:26 christos Exp $");
94a16ca184Sdsl
95a16ca184Sdsl #include <sys/param.h>
96a16ca184Sdsl #include <sys/mount.h>
97a16ca184Sdsl #include <sys/socket.h>
98a16ca184Sdsl #include <sys/stat.h>
99a16ca184Sdsl #include <syslog.h>
100a16ca184Sdsl
101a16ca184Sdsl #include <nfs/rpcv2.h>
102a16ca184Sdsl #include <nfs/nfsproto.h>
103a16ca184Sdsl #include <nfs/nfs.h>
104a16ca184Sdsl #include <nfs/nfsmount.h>
105a16ca184Sdsl
106a16ca184Sdsl #include <arpa/inet.h>
107a16ca184Sdsl
108a16ca184Sdsl #include <err.h>
109a16ca184Sdsl #include <errno.h>
110a16ca184Sdsl #include <fcntl.h>
111a16ca184Sdsl #include <netdb.h>
112a16ca184Sdsl #include <signal.h>
113a16ca184Sdsl #include <stdio.h>
114a16ca184Sdsl #include <stdlib.h>
115a16ca184Sdsl #include <string.h>
116a16ca184Sdsl #include <unistd.h>
117a16ca184Sdsl #include <util.h>
118a16ca184Sdsl
119a16ca184Sdsl #include "mount_nfs.h"
120a16ca184Sdsl
121a16ca184Sdsl #include "iodesc.h"
122a16ca184Sdsl typedef int32_t n_long;
123a16ca184Sdsl #include "rpc.h"
124a16ca184Sdsl
125a16ca184Sdsl #define RPC_HEADER_WORDS 28 /* more than enough */
126a16ca184Sdsl #define FNAME_SIZE 512
127a16ca184Sdsl
128a16ca184Sdsl struct nfhret {
129a16ca184Sdsl long fhsize;
130a16ca184Sdsl u_char nfh[NFSX_V3FHMAX];
131a16ca184Sdsl };
132a16ca184Sdsl
133*18c826e1Schristos int retrycnt = DEF_RETRY;
134*18c826e1Schristos int opflags = 0;
135*18c826e1Schristos int force2 = 0;
136*18c826e1Schristos int force3 = 0;
137*18c826e1Schristos int mnttcp_ok = 1;
138*18c826e1Schristos int port = 0;
139*18c826e1Schristos
140a16ca184Sdsl /* Ripped from src/sys/arch/i386/stand/libsa/nfs.c */
141a16ca184Sdsl static int
nfs_getrootfh(struct iodesc * d,const char * path,int mntvers,struct nfhret * nfhret)142a16ca184Sdsl nfs_getrootfh(struct iodesc *d, const char *path, int mntvers, struct nfhret *nfhret)
143a16ca184Sdsl {
144a16ca184Sdsl size_t len;
145a16ca184Sdsl struct args {
146a16ca184Sdsl uint32_t len;
147a16ca184Sdsl char path[FNAME_SIZE];
148a16ca184Sdsl } *args;
149a16ca184Sdsl struct repl {
150a16ca184Sdsl uint32_t errval;
151a16ca184Sdsl u_char fh[NFSX_V3FHMAX];
152a16ca184Sdsl } *repl;
153a16ca184Sdsl struct {
154a16ca184Sdsl uint32_t h[RPC_HEADER_WORDS];
155a16ca184Sdsl struct args d;
156a16ca184Sdsl } sdata;
157a16ca184Sdsl struct {
158a16ca184Sdsl uint32_t h[RPC_HEADER_WORDS];
159a16ca184Sdsl struct repl d;
160a16ca184Sdsl } rdata;
161a16ca184Sdsl ssize_t cc;
162a16ca184Sdsl
163a16ca184Sdsl args = &sdata.d;
164a16ca184Sdsl repl = &rdata.d;
165a16ca184Sdsl
166a16ca184Sdsl memset(args, 0, sizeof(*args));
167a16ca184Sdsl len = strlen(path);
168a16ca184Sdsl if (len > sizeof(args->path))
169a16ca184Sdsl len = sizeof(args->path);
170a16ca184Sdsl args->len = htonl(len);
171a16ca184Sdsl memcpy(args->path, path, len);
172a16ca184Sdsl len = 4 + roundup(len, 4);
173a16ca184Sdsl
174a16ca184Sdsl cc = rpc_call(d, RPCPROG_MNT, mntvers, RPCMNT_MOUNT,
175a16ca184Sdsl args, len, repl, sizeof(*repl));
176a16ca184Sdsl if (cc == -1) {
177a16ca184Sdsl /* errno was set by rpc_call */
178a16ca184Sdsl return errno;
179a16ca184Sdsl }
180a16ca184Sdsl if (cc < 4)
181a16ca184Sdsl return EBADRPC;
182a16ca184Sdsl if (repl->errval)
183a16ca184Sdsl return ntohl(repl->errval);
184a16ca184Sdsl nfhret->fhsize = cc;
185a16ca184Sdsl memcpy(nfhret->nfh, repl->fh, sizeof(repl->fh));
186a16ca184Sdsl return 0;
187a16ca184Sdsl }
188a16ca184Sdsl
189a16ca184Sdsl int
getnfsargs(char * spec,struct nfs_args * nfsargsp)190a16ca184Sdsl getnfsargs(char *spec, struct nfs_args *nfsargsp)
191a16ca184Sdsl {
192a16ca184Sdsl struct addrinfo hints, *ai_nfs;
193a16ca184Sdsl int ecode;
194a16ca184Sdsl int nfsvers, mntvers;
195a16ca184Sdsl char *hostp, *delimp;
196a16ca184Sdsl static struct nfhret nfhret;
197a16ca184Sdsl static char nam[MNAMELEN + 1];
198a16ca184Sdsl struct iodesc d;
199a16ca184Sdsl int nfs_port;
200a16ca184Sdsl
20133bed52cShubertf strlcpy(nam, spec, sizeof(nam));
202a16ca184Sdsl if ((delimp = strchr(spec, '@')) != NULL) {
203a16ca184Sdsl hostp = delimp + 1;
204a16ca184Sdsl } else if ((delimp = strrchr(spec, ':')) != NULL) {
205a16ca184Sdsl hostp = spec;
206a16ca184Sdsl spec = delimp + 1;
207a16ca184Sdsl } else {
208a16ca184Sdsl warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
209a16ca184Sdsl return 0;
210a16ca184Sdsl }
211a16ca184Sdsl *delimp = '\0';
212a16ca184Sdsl
213a16ca184Sdsl memset(&hints, 0, sizeof hints);
214a16ca184Sdsl hints.ai_socktype = nfsargsp->sotype;
215a16ca184Sdsl
216a16ca184Sdsl if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
217a16ca184Sdsl warnx("can't get net id for host \"%s\": %s", hostp,
218a16ca184Sdsl gai_strerror(ecode));
219a16ca184Sdsl return 0;
220a16ca184Sdsl }
221a16ca184Sdsl
222ebeaf83fSyamt if ((nfsargsp->flags & NFSMNT_NFSV3) != 0) {
223a16ca184Sdsl nfsvers = NFS_VER3;
224a16ca184Sdsl mntvers = RPCMNT_VER3;
225ebeaf83fSyamt } else {
226ebeaf83fSyamt nfsvers = NFS_VER2;
227ebeaf83fSyamt mntvers = RPCMNT_VER1;
228a16ca184Sdsl }
229a16ca184Sdsl
230a16ca184Sdsl d.socket = -1;
231a16ca184Sdsl for (d.ai = ai_nfs; ; d.ai = d.ai->ai_next) {
232a16ca184Sdsl if (d.ai == NULL) {
233a16ca184Sdsl if (nfsvers == NFS_VER3 && !force3) {
234a16ca184Sdsl nfsvers = NFS_VER2;
235a16ca184Sdsl mntvers = RPCMNT_VER1;
236a16ca184Sdsl d.ai = ai_nfs;
237a16ca184Sdsl continue;
238a16ca184Sdsl }
239a16ca184Sdsl return 0;
240a16ca184Sdsl }
241a16ca184Sdsl nfs_port = rpc_getport(&d, RPCPROG_NFS, nfsvers);
242a16ca184Sdsl if (nfs_port == -1)
243a16ca184Sdsl continue;
244a16ca184Sdsl if (nfs_getrootfh(&d, spec, mntvers, &nfhret) == 0)
245a16ca184Sdsl break;
246a16ca184Sdsl }
247a16ca184Sdsl
248a16ca184Sdsl if (port != 0)
249a16ca184Sdsl nfs_port = port;
250a16ca184Sdsl set_port(d.ai->ai_addr, htons(nfs_port));
251a16ca184Sdsl
252a16ca184Sdsl nfsargsp->hostname = nam;
253a16ca184Sdsl nfsargsp->addr = d.ai->ai_addr;
254a16ca184Sdsl nfsargsp->addrlen = d.ai->ai_addrlen;
255a16ca184Sdsl
256a16ca184Sdsl nfsargsp->fh = nfhret.nfh;
257a16ca184Sdsl if (nfsvers == NFS_VER3) {
2584cd6566dSmrg nfsargsp->fhsize = be32dec(nfhret.nfh);
2594cd6566dSmrg nfsargsp->fh += sizeof(uint32_t);
260a16ca184Sdsl } else {
261ae4a1122Syamt nfsargsp->fhsize = NFSX_V2FH;
262a16ca184Sdsl nfsargsp->flags &= ~NFSMNT_NFSV3;
263a16ca184Sdsl }
264a16ca184Sdsl return 1;
265a16ca184Sdsl }
266