]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
text-based mount.nfs: start using new mount option parsing facility
[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 #include "parse_opt.h"
44
45 #ifdef HAVE_RPCSVC_NFS_PROT_H
46 #include <rpcsvc/nfs_prot.h>
47 #else
48 #include <linux/nfs.h>
49 #define nfsstat nfs_stat
50 #endif
51
52 #ifndef NFS_PORT
53 #define NFS_PORT 2049
54 #endif
55
56 #ifndef NFS_MAXHOSTNAME
57 #define NFS_MAXHOSTNAME         (255)
58 #endif
59
60 #ifndef NFS_MAXPATHNAME
61 #define NFS_MAXPATHNAME         (1024)
62 #endif
63
64 extern int nfs_mount_data_version;
65 extern char *progname;
66 extern int verbose;
67
68 static int retry_opt = 10000;           /* 10,000 minutes ~= 1 week */
69 static int bg_opt = 0;
70 static int addr_opt = 0;
71 static int ca_opt = 0;
72
73 static int parse_devname(const char *spec, char **hostname)
74 {
75         int ret = 0;
76         char *dev, *pathname, *s;
77
78         dev = xstrdup(spec);
79
80         if (!(pathname = strchr(dev, ':'))) {
81                 nfs_error(_("%s: remote share not in 'host:dir' format"),
82                                 progname);
83                 goto out;
84         }
85         *pathname = '\0';
86         pathname++;
87
88         /*
89          * We don't need a copy of the pathname, but let's
90          * sanity check it anyway.
91          */
92         if (strlen(pathname) > NFS_MAXPATHNAME) {
93                 nfs_error(_("%s: export pathname is too long"),
94                                 progname);
95                 goto out;
96         }
97
98         /*
99          * Ignore all but first hostname in replicated mounts
100          * until they can be fully supported. (mack@sgi.com)
101          */
102         if ((s = strchr(dev, ','))) {
103                 *s = '\0';
104                 nfs_error(_("%s: warning: multiple hostnames not supported"),
105                                 progname);
106                 nfs_error(_("%s: ignoring hostnames that follow the first one"),
107                                 progname);
108         }
109         *hostname = xstrdup(dev);
110         if (strlen(*hostname) > NFS_MAXHOSTNAME) {
111                 nfs_error(_("%s: server hostname is too long"),
112                                 progname);
113                 free(*hostname);
114                 goto out;
115         }
116
117         ret = 1;
118
119 out:
120         free(dev);
121         return ret;
122 }
123
124 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
125 {
126         struct hostent *hp;
127         addr->sin_family = AF_INET;
128
129         if (inet_aton(hostname, &addr->sin_addr))
130                 return 1;
131         if ((hp = gethostbyname(hostname)) == NULL) {
132                 nfs_error(_("%s: can't get address for %s\n"),
133                                 progname, hostname);
134                 return 0;
135         }
136         if (hp->h_length > sizeof(struct in_addr)) {
137                 nfs_error(_("%s: got bad hp->h_length"), progname);
138                 hp->h_length = sizeof(struct in_addr);
139         }
140         memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
141         return 1;
142 }
143
144 /*
145  * Walk through our mount options string, and indicate the presence
146  * of 'bg', 'retry=', 'addr=', and 'clientaddr='.
147  */
148 static void extract_interesting_options(char *opts)
149 {
150         char *opt, *opteq;
151         int val;
152
153         opts = xstrdup(opts);
154
155         for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ",")) {
156                 if ((opteq = strchr(opt, '='))) {
157                         val = atoi(opteq + 1);
158                         *opteq = '\0';
159                         if (strcmp(opt, "bg") == 0)
160                                 bg_opt++;
161                         else if (strcmp(opt, "retry") == 0)
162                                 retry_opt = val;
163                         else if (strcmp(opt, "addr") == 0)
164                                 addr_opt++;
165                         else if (strcmp(opt, "clientaddr") == 0)
166                                 ca_opt++;
167                 } else {
168                         if (strcmp(opt, "bg") == 0)
169                                 bg_opt++;
170                 }
171         }
172
173         free(opts);
174 }
175
176 /*
177  * Append the 'addr=' option to the options string.  The server
178  * address is added to /etc/mtab for use when unmounting.
179  *
180  * Returns 1 if 'addr=' option created successfully;
181  * otherwise zero.
182  */
183 static int append_addr_opt(struct sockaddr_in *saddr, char **extra_opts)
184 {
185         static char new_opts[1024];
186         char *s, *old_opts;
187
188         s = inet_ntoa(saddr->sin_addr);
189         old_opts = *extra_opts;
190         if (!old_opts)
191                 old_opts = "";
192         if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
193                 nfs_error(_("%s: too many mount options\n"),
194                                 progname);
195                 return 0;
196         }
197         snprintf(new_opts, sizeof(new_opts), "%s%saddr=%s",
198                  old_opts, *old_opts ? "," : "", s);
199         *extra_opts = xstrdup(new_opts);
200
201         return 1;
202 }
203
204 /*
205  * Called if no 'clientaddr=' option was specified in the options string
206  * to discover our address and append an appropriate 'clientaddr=' option
207  * to the options string.
208  *
209  * Returns 1 if 'clientaddr=' option created successfully;
210  * otherwise zero.
211  */
212 static int append_clientaddr_opt(struct sockaddr_in *saddr, char **extra_opts)
213 {
214         static char new_opts[2048], cbuf[256];
215         struct sockaddr_in my_addr;
216
217         if (!get_client_address(saddr, &my_addr))
218                 return 0;
219
220         if (strlen(*extra_opts) + 30 >= sizeof(new_opts)) {
221                 nfs_error(_("%s: too many mount options"),
222                                 progname);
223                 return 0;
224         }
225
226         strcat(new_opts, *extra_opts);
227
228         snprintf(cbuf, sizeof(cbuf) - 1, "%sclientaddr=%s",
229                         *extra_opts ? "," : "", inet_ntoa(my_addr.sin_addr));
230
231         strcat(new_opts, cbuf);
232
233         *extra_opts = xstrdup(new_opts);
234
235         return 1;
236 }
237
238 /*
239  * Append the 'addr=' option to the options string to pass a resolved
240  * server address to the kernel.  After a successful mount, this address
241  * is also added to /etc/mtab for use when unmounting.
242  *
243  * If 'addr=' is already present, we strip it out.  This prevents users
244  * from setting a bogus 'addr=' option themselves, and also allows bg
245  * retries to recompute the server's address, in case it has changed.
246  *
247  * Returns 1 if 'addr=' option appended successfully;
248  * otherwise zero.
249  */
250 static int append_addr_option(struct sockaddr_in *saddr,
251                            struct mount_options *options)
252 {
253         char new_option[24];
254
255         po_remove_all(options, "addr");
256
257         snprintf(new_option, sizeof(new_option) - 1,
258                         "addr=%s", inet_ntoa(saddr->sin_addr));
259
260         if (po_append(options, new_option) == PO_SUCCEEDED)
261                 return 1;
262         return 0;
263 }
264
265 /*
266  * Called to discover our address and append an appropriate 'clientaddr='
267  * option to the options string.
268  *
269  * Returns 1 if 'clientaddr=' option created successfully or if
270  * 'clientaddr=' option is already present; otherwise zero.
271  */
272 static int append_clientaddr_option(struct sockaddr_in *saddr,
273                                     struct mount_options *options)
274 {
275         struct sockaddr_in my_addr;
276         char new_option[32];
277
278         if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
279                 return 1;
280
281         if (!get_client_address(saddr, &my_addr))
282                 return 0;
283
284         snprintf(new_option, sizeof(new_option) - 1,
285                         "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
286
287         if (po_append(options, new_option) == PO_SUCCEEDED)
288                 return 1;
289         return 0;
290 }
291
292 /*
293  * nfsmount_s - Mount an NFSv2 or v3 file system using C string options
294  *
295  * @spec:       C string hostname:path specifying remoteshare to mount
296  * @node:       C string pathname of local mounted on directory
297  * @flags:      MS_ style flags
298  * @extra_opts: pointer to C string containing fs-specific mount options
299  *              (possibly also a return argument)
300  * @fake:       flag indicating whether to carry out the whole operation
301  * @child:      one if this is a backgrounded mount
302  *
303  * XXX: need to handle bg, fg, and retry options.
304  */
305 int nfsmount_s(const char *spec, const char *node, int flags,
306                 char **extra_opts, int fake, int child)
307 {
308         struct mount_options *options = NULL;
309         struct sockaddr_in saddr;
310         char *hostname;
311         int err, retval = EX_FAIL;
312
313         if (!parse_devname(spec, &hostname))
314                 goto out;
315         err = fill_ipv4_sockaddr(hostname, &saddr);
316         free(hostname);
317         if (!err)
318                 goto out;
319
320         options = po_split(*extra_opts);
321         if (!options) {
322                 nfs_error(_("%s: internal option parsing error"), progname);
323                 goto out;
324         }
325
326         if (!append_addr_option(&saddr, options))
327                 goto out;
328
329         if (po_join(options, extra_opts) == PO_FAILED) {
330                 nfs_error(_("%s: internal option parsing error"), progname);
331                 goto out;
332         }
333
334         if (verbose)
335                 printf(_("%s: text-based options: '%s'\n"),
336                         progname, *extra_opts);
337
338         if (!fake) {
339                 if (mount(spec, node, "nfs",
340                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
341                         mount_error(spec, node, errno);
342                         goto out;
343                 }
344         }
345
346         retval = EX_SUCCESS;
347
348 out:
349         po_destroy(options);
350         return retval;
351 }
352
353 /*
354  * nfs4mount_s - Mount an NFSv4 file system using C string options
355  *
356  * @spec:       C string hostname:path specifying remoteshare to mount
357  * @node:       C string pathname of local mounted on directory
358  * @flags:      MS_ style flags
359  * @extra_opts: pointer to C string containing fs-specific mount options
360  *              (possibly also a return argument)
361  * @fake:       flag indicating whether to carry out the whole operation
362  * @child:      one if this is a backgrounded mount
363  *
364  * XXX: need to handle bg, fg, and retry options.
365  *
366  */
367 int nfs4mount_s(const char *spec, const char *node, int flags,
368                 char **extra_opts, int fake, int child)
369 {
370         struct mount_options *options = NULL;
371         struct sockaddr_in saddr;
372         char *hostname;
373         int err, retval = EX_FAIL;
374
375         if (!parse_devname(spec, &hostname))
376                 goto out;
377         err = fill_ipv4_sockaddr(hostname, &saddr);
378         free(hostname);
379         if (!err)
380                 goto out;
381
382         options = po_split(*extra_opts);
383         if (!options) {
384                 nfs_error(_("%s: internal option parsing error"), progname);
385                 goto out;
386         }
387
388         if (!append_addr_option(&saddr, options))
389                 goto out;
390
391         if (!append_clientaddr_option(&saddr, options))
392                 goto out;
393
394         if (po_join(options, extra_opts) == PO_FAILED) {
395                 nfs_error(_("%s: internal option parsing error"), progname);
396                 goto out;
397         }
398
399         if (verbose)
400                 printf(_("%s: text-based options: '%s'\n"),
401                         progname, *extra_opts);
402
403         if (!fake) {
404                 if (mount(spec, node, "nfs4",
405                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
406                         mount_error(spec, node, errno);
407                         goto out;
408                 }
409         }
410
411         retval = EX_SUCCESS;
412
413 out:
414         po_destroy(options);
415         return retval;
416 }