]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
mount.nfs: Remove get_my_ipv4addr() from utils/mount/stropts.c
[nfs-utils.git] / utils / mount / stropts.c
1 /*
2  * stropts.c -- NFS mount using C string to pass options to kernel
3  *
4  * Copyright (C) 2007 Oracle.  All rights reserved.
5  * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 021110-1307, USA.
21  *
22  */
23
24 #include <ctype.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <time.h>
32 #include <sys/socket.h>
33 #include <sys/mount.h>
34
35 #include "xcommon.h"
36 #include "mount.h"
37 #include "nls.h"
38 #include "nfs_mount.h"
39 #include "mount_constants.h"
40 #include "stropts.h"
41 #include "error.h"
42 #include "network.h"
43
44 #ifdef HAVE_RPCSVC_NFS_PROT_H
45 #include <rpcsvc/nfs_prot.h>
46 #else
47 #include <linux/nfs.h>
48 #define nfsstat nfs_stat
49 #endif
50
51 #ifndef NFS_PORT
52 #define NFS_PORT 2049
53 #endif
54
55 #ifndef NFS_MAXHOSTNAME
56 #define NFS_MAXHOSTNAME         (255)
57 #endif
58
59 #ifndef NFS_MAXPATHNAME
60 #define NFS_MAXPATHNAME         (1024)
61 #endif
62
63 extern int nfs_mount_data_version;
64 extern char *progname;
65 extern int verbose;
66
67 static int retry_opt = 10000;           /* 10,000 minutes ~= 1 week */
68 static int bg_opt = 0;
69 static int addr_opt = 0;
70 static int ca_opt = 0;
71
72 static int parse_devname(const char *spec, char **hostname)
73 {
74         int ret = 0;
75         char *dev, *pathname, *s;
76
77         dev = xstrdup(spec);
78
79         if (!(pathname = strchr(dev, ':'))) {
80                 nfs_error(_("%s: remote share not in 'host:dir' format"),
81                                 progname);
82                 goto out;
83         }
84         *pathname = '\0';
85         pathname++;
86
87         /*
88          * We don't need a copy of the pathname, but let's
89          * sanity check it anyway.
90          */
91         if (strlen(pathname) > NFS_MAXPATHNAME) {
92                 nfs_error(_("%s: export pathname is too long"),
93                                 progname);
94                 goto out;
95         }
96
97         /*
98          * Ignore all but first hostname in replicated mounts
99          * until they can be fully supported. (mack@sgi.com)
100          */
101         if ((s = strchr(dev, ','))) {
102                 *s = '\0';
103                 nfs_error(_("%s: warning: multiple hostnames not supported"),
104                                 progname);
105                 nfs_error(_("%s: ignoring hostnames that follow the first one"),
106                                 progname);
107         }
108         *hostname = xstrdup(dev);
109         if (strlen(*hostname) > NFS_MAXHOSTNAME) {
110                 nfs_error(_("%s: server hostname is too long"),
111                                 progname);
112                 free(*hostname);
113                 goto out;
114         }
115
116         ret = 1;
117
118 out:
119         free(dev);
120         return ret;
121 }
122
123 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
124 {
125         struct hostent *hp;
126         addr->sin_family = AF_INET;
127
128         if (inet_aton(hostname, &addr->sin_addr))
129                 return 1;
130         if ((hp = gethostbyname(hostname)) == NULL) {
131                 nfs_error(_("%s: can't get address for %s\n"),
132                                 progname, hostname);
133                 return 0;
134         }
135         if (hp->h_length > sizeof(struct in_addr)) {
136                 nfs_error(_("%s: got bad hp->h_length"), progname);
137                 hp->h_length = sizeof(struct in_addr);
138         }
139         memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
140         return 1;
141 }
142
143 /*
144  * Walk through our mount options string, and indicate the presence
145  * of 'bg', 'retry=', 'addr=', and 'clientaddr='.
146  */
147 static void extract_interesting_options(char *opts)
148 {
149         char *opt, *opteq;
150         int val;
151
152         opts = xstrdup(opts);
153
154         for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ",")) {
155                 if ((opteq = strchr(opt, '='))) {
156                         val = atoi(opteq + 1);
157                         *opteq = '\0';
158                         if (strcmp(opt, "bg") == 0)
159                                 bg_opt++;
160                         else if (strcmp(opt, "retry") == 0)
161                                 retry_opt = val;
162                         else if (strcmp(opt, "addr") == 0)
163                                 addr_opt++;
164                         else if (strcmp(opt, "clientaddr") == 0)
165                                 ca_opt++;
166                 } else {
167                         if (strcmp(opt, "bg") == 0)
168                                 bg_opt++;
169                 }
170         }
171
172         free(opts);
173 }
174
175 /*
176  * Append the 'addr=' option to the options string.  The server
177  * address is added to /etc/mtab for use when unmounting.
178  *
179  * Returns 1 if 'addr=' option created successfully;
180  * otherwise zero.
181  */
182 static int append_addr_opt(struct sockaddr_in *saddr, char **extra_opts)
183 {
184         static char new_opts[1024];
185         char *s, *old_opts;
186
187         s = inet_ntoa(saddr->sin_addr);
188         old_opts = *extra_opts;
189         if (!old_opts)
190                 old_opts = "";
191         if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
192                 nfs_error(_("%s: too many mount options\n"),
193                                 progname);
194                 return 0;
195         }
196         snprintf(new_opts, sizeof(new_opts), "%s%saddr=%s",
197                  old_opts, *old_opts ? "," : "", s);
198         *extra_opts = xstrdup(new_opts);
199
200         return 1;
201 }
202
203 /*
204  * Append the 'clientaddr=' option to the options string.
205  *
206  * Returns 1 if 'clientaddr=' option created successfully;
207  * otherwise zero.
208  */
209 static int append_clientaddr_opt(struct sockaddr_in *saddr, char **extra_opts)
210 {
211         static char new_opts[2048], cbuf[256];
212         struct sockaddr_in my_addr;
213
214         if (!get_client_address(saddr, &my_addr))
215                 return 0;
216
217         if (strlen(*extra_opts) + 30 >= sizeof(new_opts)) {
218                 nfs_error(_("%s: too many mount options"),
219                                 progname);
220                 return 0;
221         }
222
223         strcat(new_opts, *extra_opts);
224
225         snprintf(cbuf, sizeof(cbuf) - 1, "%sclientaddr=%s",
226                         *extra_opts ? "," : "", inet_ntoa(my_addr.sin_addr));
227
228         strcat(new_opts, cbuf);
229
230         *extra_opts = xstrdup(new_opts);
231
232         return 1;
233 }
234
235 /*
236  * nfsmount_s - Mount an NFSv2 or v3 file system using C string options
237  *
238  * @spec:       C string hostname:path specifying remoteshare to mount
239  * @node:       C string pathname of local mounted on directory
240  * @flags:      MS_ style flags
241  * @extra_opts: pointer to C string containing fs-specific mount options
242  *              (possibly also a return argument)
243  * @fake:       flag indicating whether to carry out the whole operation
244  * @bg:         one if this is a backgrounded mount attempt
245  *
246  * XXX: need to handle bg, fg, and retry options.
247  */
248 int nfsmount_s(const char *spec, const char *node, int flags,
249                 char **extra_opts, int fake, int bg)
250 {
251         struct sockaddr_in saddr;
252         char *hostname;
253         int err;
254
255         if (!parse_devname(spec, &hostname))
256                 return EX_FAIL;
257         err = fill_ipv4_sockaddr(hostname, &saddr);
258         free(hostname);
259         if (!err)
260                 return EX_FAIL;
261
262         extract_interesting_options(*extra_opts);
263
264         if (!bg && addr_opt) {
265                 nfs_error(_("%s: Illegal option: 'addr='"), progname);
266                 return EX_FAIL;
267         }
268
269         if (!append_addr_opt(&saddr, extra_opts))
270                 return EX_FAIL;
271
272         if (verbose)
273                 printf(_("%s: text-based options: '%s'\n"),
274                         progname, *extra_opts);
275
276         if (!fake) {
277                 if (mount(spec, node, "nfs",
278                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
279                         mount_error(spec, node, errno);
280                         return EX_FAIL;
281                 }
282         }
283
284         return 0;
285 }
286
287 /*
288  * nfs4mount_s - Mount an NFSv4 file system using C string options
289  *
290  * @spec:       C string hostname:path specifying remoteshare to mount
291  * @node:       C string pathname of local mounted on directory
292  * @flags:      MS_ style flags
293  * @extra_opts: pointer to C string containing fs-specific mount options
294  *              (possibly also a return argument)
295  * @fake:       flag indicating whether to carry out the whole operation
296  * @child:      one if this is a backgrounded mount
297  *
298  * XXX: need to handle bg, fg, and retry options.
299  *
300  */
301 int nfs4mount_s(const char *spec, const char *node, int flags,
302                 char **extra_opts, int fake, int child)
303 {
304         struct sockaddr_in saddr;
305         char *hostname;
306         int err;
307
308         if (!parse_devname(spec, &hostname))
309                 return EX_FAIL;
310         err = fill_ipv4_sockaddr(hostname, &saddr);
311         free(hostname);
312         if (!err)
313                 return EX_FAIL;
314
315         extract_interesting_options(*extra_opts);
316
317         if (addr_opt) {
318                 nfs_error(_("%s: Illegal option: 'addr='"), progname);
319                 return EX_FAIL;
320         }
321
322         if (ca_opt) {
323                 nfs_error(_("%s: Illegal option: 'clientaddr='"), progname);
324                 return EX_FAIL;
325         }
326
327         if (!append_addr_opt(&saddr, extra_opts))
328                 return EX_FAIL;
329
330         if (!append_clientaddr_opt(&saddr, extra_opts))
331                 return EX_FAIL;
332
333         if (verbose)
334                 printf(_("%s: text-based options: '%s'\n"),
335                         progname, *extra_opts);
336
337         if (!fake) {
338                 if (mount(spec, node, "nfs4",
339                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
340                         mount_error(spec, node, errno);
341                         return EX_FAIL;
342                 }
343         }
344
345         return 0;
346 }