]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
57d8dad0d66d32b09490063510bfe111fceb8978
[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  * XXX: This should really use the technique neil recently added
145  * to get the address off the local end of a socket connected to
146  * the server -- to get the right address to use on multi-homed
147  * clients
148  */
149 static int get_my_ipv4addr(char *ip_addr, int len)
150 {
151         char myname[1024];
152         struct sockaddr_in myaddr;
153
154         if (gethostname(myname, sizeof(myname))) {
155                 nfs_error(_("%s: can't determine client address\n"),
156                                 progname);
157                 return 0;
158         }
159         if (!fill_ipv4_sockaddr(myname, &myaddr))
160                 return 0;
161
162         snprintf(ip_addr, len, "%s", inet_ntoa(myaddr.sin_addr));
163         ip_addr[len - 1] = '\0';
164
165         return 1;
166 }
167
168 /*
169  * Walk through our mount options string, and indicate the presence
170  * of 'bg', 'retry=', 'addr=', and 'clientaddr='.
171  */
172 static void extract_interesting_options(char *opts)
173 {
174         char *opt, *opteq;
175         int val;
176
177         opts = xstrdup(opts);
178
179         for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ",")) {
180                 if ((opteq = strchr(opt, '='))) {
181                         val = atoi(opteq + 1);
182                         *opteq = '\0';
183                         if (strcmp(opt, "bg") == 0)
184                                 bg_opt++;
185                         else if (strcmp(opt, "retry") == 0)
186                                 retry_opt = val;
187                         else if (strcmp(opt, "addr") == 0)
188                                 addr_opt++;
189                         else if (strcmp(opt, "clientaddr") == 0)
190                                 ca_opt++;
191                 } else {
192                         if (strcmp(opt, "bg") == 0)
193                                 bg_opt++;
194                 }
195         }
196
197         free(opts);
198 }
199
200 /*
201  * Append the 'addr=' option to the options string.  The server
202  * address is added to /etc/mtab for use when unmounting.
203  *
204  * Returns 1 if 'addr=' option created successfully;
205  * otherwise zero.
206  */
207 static int append_addr_opt(struct sockaddr_in *saddr, char **extra_opts)
208 {
209         static char new_opts[1024];
210         char *s, *old_opts;
211
212         s = inet_ntoa(saddr->sin_addr);
213         old_opts = *extra_opts;
214         if (!old_opts)
215                 old_opts = "";
216         if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
217                 nfs_error(_("%s: too many mount options\n"),
218                                 progname);
219                 return 0;
220         }
221         snprintf(new_opts, sizeof(new_opts), "%s%saddr=%s",
222                  old_opts, *old_opts ? "," : "", s);
223         *extra_opts = xstrdup(new_opts);
224
225         return 1;
226 }
227
228 /*
229  * Append the 'clientaddr=' option to the options string.
230  *
231  * Returns 1 if 'clientaddr=' option created successfully;
232  * otherwise zero.
233  */
234 static int append_clientaddr_opt(const char *spec, char **extra_opts)
235 {
236         static char new_opts[1024], cbuf[1024];
237         static char ip_addr[16] = "127.0.0.1";
238
239         if (!get_my_ipv4addr(ip_addr, sizeof(ip_addr)))
240                 return 0;
241
242         /* Ensure we have enough padding for the following strcat()s */
243         if (strlen(*extra_opts) + strlen(ip_addr) + 10 >= sizeof(new_opts)) {
244                 nfs_error(_("%s: excessively long option argument"),
245                                 progname);
246                 return 0;
247         }
248
249         strcat(new_opts, *extra_opts);
250
251         snprintf(cbuf, sizeof(cbuf) - 1, "%sclientaddr=%s",
252                         *extra_opts ? "," : "", ip_addr);
253         strcat(new_opts, cbuf);
254
255         *extra_opts = xstrdup(new_opts);
256
257         return 1;
258 }
259
260 /*
261  * nfsmount_s - Mount an NFSv2 or v3 file system using C string options
262  *
263  * @spec:       C string hostname:path specifying remoteshare to mount
264  * @node:       C string pathname of local mounted on directory
265  * @flags:      MS_ style flags
266  * @extra_opts: pointer to C string containing fs-specific mount options
267  *              (possibly also a return argument)
268  * @fake:       flag indicating whether to carry out the whole operation
269  * @bg:         one if this is a backgrounded mount attempt
270  *
271  * XXX: need to handle bg, fg, and retry options.
272  */
273 int nfsmount_s(const char *spec, const char *node, int flags,
274                 char **extra_opts, int fake, int bg)
275 {
276         struct sockaddr_in saddr;
277         char *hostname;
278         int err;
279
280         if (!parse_devname(spec, &hostname))
281                 return EX_FAIL;
282         err = fill_ipv4_sockaddr(hostname, &saddr);
283         free(hostname);
284         if (!err)
285                 return EX_FAIL;
286
287         extract_interesting_options(*extra_opts);
288
289         if (!bg && addr_opt) {
290                 nfs_error(_("%s: Illegal option: 'addr='"), progname);
291                 return EX_FAIL;
292         }
293
294         if (!append_addr_opt(&saddr, extra_opts))
295                 return EX_FAIL;
296
297         if (verbose)
298                 printf(_("%s: text-based options: '%s'\n"),
299                         progname, *extra_opts);
300
301         if (!fake) {
302                 if (mount(spec, node, "nfs",
303                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
304                         mount_error(spec, node, errno);
305                         return EX_FAIL;
306                 }
307         }
308
309         return 0;
310 }
311
312 /*
313  * nfs4mount_s - Mount an NFSv4 file system using C string options
314  *
315  * @spec:       C string hostname:path specifying remoteshare to mount
316  * @node:       C string pathname of local mounted on directory
317  * @flags:      MS_ style flags
318  * @extra_opts: pointer to C string containing fs-specific mount options
319  *              (possibly also a return argument)
320  * @fake:       flag indicating whether to carry out the whole operation
321  * @child:      one if this is a backgrounded mount
322  *
323  * XXX: need to handle bg, fg, and retry options.
324  *
325  */
326 int nfs4mount_s(const char *spec, const char *node, int flags,
327                 char **extra_opts, int fake, int child)
328 {
329         struct sockaddr_in saddr;
330         char *hostname;
331         int err;
332
333         if (!parse_devname(spec, &hostname))
334                 return EX_FAIL;
335         err = fill_ipv4_sockaddr(hostname, &saddr);
336         free(hostname);
337         if (!err)
338                 return EX_FAIL;
339
340         extract_interesting_options(*extra_opts);
341
342         if (addr_opt) {
343                 nfs_error(_("%s: Illegal option: 'addr='"), progname);
344                 return EX_FAIL;
345         }
346
347         if (ca_opt) {
348                 nfs_error(_("%s: Illegal option: 'clientaddr='"), progname);
349                 return EX_FAIL;
350         }
351
352         if (!append_addr_opt(&saddr, extra_opts))
353                 return EX_FAIL;
354
355         if (!append_clientaddr_opt(spec, extra_opts))
356                 return EX_FAIL;
357
358         if (verbose)
359                 printf(_("%s: text-based options: '%s'\n"),
360                         progname, *extra_opts);
361
362         if (!fake) {
363                 if (mount(spec, node, "nfs4",
364                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
365                         mount_error(spec, node, errno);
366                         return EX_FAIL;
367                 }
368         }
369
370         return 0;
371 }