]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
text-based mount.nfs: add parser init and destroy calls
[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         extract_interesting_options(*extra_opts);
321
322         options = po_split(*extra_opts);
323         if (!options) {
324                 nfs_error(_("%s: internal option parsing error"), progname);
325                 goto out;
326         }
327
328         if (!child && addr_opt) {
329                 nfs_error(_("%s: Illegal option: 'addr='"), progname);
330                 goto out;
331         }
332
333         if (!append_addr_opt(&saddr, extra_opts))
334                 goto out;
335
336         if (po_join(options, extra_opts) == PO_FAILED) {
337                 nfs_error(_("%s: internal option parsing error"), progname);
338                 goto out;
339         }
340
341         if (verbose)
342                 printf(_("%s: text-based options: '%s'\n"),
343                         progname, *extra_opts);
344
345         if (!fake) {
346                 if (mount(spec, node, "nfs",
347                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
348                         mount_error(spec, node, errno);
349                         goto out;
350                 }
351         }
352
353         retval = EX_SUCCESS;
354
355 out:
356         po_destroy(options);
357         return retval;
358 }
359
360 /*
361  * nfs4mount_s - Mount an NFSv4 file system using C string options
362  *
363  * @spec:       C string hostname:path specifying remoteshare to mount
364  * @node:       C string pathname of local mounted on directory
365  * @flags:      MS_ style flags
366  * @extra_opts: pointer to C string containing fs-specific mount options
367  *              (possibly also a return argument)
368  * @fake:       flag indicating whether to carry out the whole operation
369  * @child:      one if this is a backgrounded mount
370  *
371  * XXX: need to handle bg, fg, and retry options.
372  *
373  */
374 int nfs4mount_s(const char *spec, const char *node, int flags,
375                 char **extra_opts, int fake, int child)
376 {
377         struct mount_options *options = NULL;
378         struct sockaddr_in saddr;
379         char *hostname;
380         int err, retval = EX_FAIL;
381
382         if (!parse_devname(spec, &hostname))
383                 goto out;
384         err = fill_ipv4_sockaddr(hostname, &saddr);
385         free(hostname);
386         if (!err)
387                 goto out;
388
389         extract_interesting_options(*extra_opts);
390
391         options = po_split(*extra_opts);
392         if (!options) {
393                 nfs_error(_("%s: internal option parsing error"), progname);
394                 goto out;
395         }
396
397         if (addr_opt) {
398                 nfs_error(_("%s: Illegal option: 'addr='"), progname);
399                 goto out;
400         }
401
402         if (!append_addr_opt(&saddr, extra_opts))
403                 goto out;
404
405         if (!ca_opt && !append_clientaddr_opt(&saddr, extra_opts))
406                 goto out;
407
408         if (po_join(options, extra_opts) == PO_FAILED) {
409                 nfs_error(_("%s: internal option parsing error"), progname);
410                 goto out;
411         }
412
413         if (verbose)
414                 printf(_("%s: text-based options: '%s'\n"),
415                         progname, *extra_opts);
416
417         if (!fake) {
418                 if (mount(spec, node, "nfs4",
419                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
420                         mount_error(spec, node, errno);
421                         goto out;
422                 }
423         }
424
425         retval = EX_SUCCESS;
426
427 out:
428         po_destroy(options);
429         return retval;
430 }