]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
text-based mount.nfs: Use helpers for invoking mount(2) system call
[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 parse_devname(const char *spec, char **hostname)
69 {
70         int ret = 0;
71         char *dev, *pathname, *s;
72
73         dev = xstrdup(spec);
74
75         if (!(pathname = strchr(dev, ':'))) {
76                 nfs_error(_("%s: remote share not in 'host:dir' format"),
77                                 progname);
78                 goto out;
79         }
80         *pathname = '\0';
81         pathname++;
82
83         /*
84          * We don't need a copy of the pathname, but let's
85          * sanity check it anyway.
86          */
87         if (strlen(pathname) > NFS_MAXPATHNAME) {
88                 nfs_error(_("%s: export pathname is too long"),
89                                 progname);
90                 goto out;
91         }
92
93         /*
94          * Ignore all but first hostname in replicated mounts
95          * until they can be fully supported. (mack@sgi.com)
96          */
97         if ((s = strchr(dev, ','))) {
98                 *s = '\0';
99                 nfs_error(_("%s: warning: multiple hostnames not supported"),
100                                 progname);
101                 nfs_error(_("%s: ignoring hostnames that follow the first one"),
102                                 progname);
103         }
104         *hostname = xstrdup(dev);
105         if (strlen(*hostname) > NFS_MAXHOSTNAME) {
106                 nfs_error(_("%s: server hostname is too long"),
107                                 progname);
108                 free(*hostname);
109                 goto out;
110         }
111
112         ret = 1;
113
114 out:
115         free(dev);
116         return ret;
117 }
118
119 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
120 {
121         struct hostent *hp;
122         addr->sin_family = AF_INET;
123
124         if (inet_aton(hostname, &addr->sin_addr))
125                 return 1;
126         if ((hp = gethostbyname(hostname)) == NULL) {
127                 nfs_error(_("%s: can't get address for %s\n"),
128                                 progname, hostname);
129                 return 0;
130         }
131         if (hp->h_length > sizeof(struct in_addr)) {
132                 nfs_error(_("%s: got bad hp->h_length"), progname);
133                 hp->h_length = sizeof(struct in_addr);
134         }
135         memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
136         return 1;
137 }
138
139 /*
140  * Append the 'addr=' option to the options string to pass a resolved
141  * server address to the kernel.  After a successful mount, this address
142  * is also added to /etc/mtab for use when unmounting.
143  *
144  * If 'addr=' is already present, we strip it out.  This prevents users
145  * from setting a bogus 'addr=' option themselves, and also allows bg
146  * retries to recompute the server's address, in case it has changed.
147  *
148  * Returns 1 if 'addr=' option appended successfully;
149  * otherwise zero.
150  */
151 static int append_addr_option(struct sockaddr_in *saddr,
152                            struct mount_options *options)
153 {
154         char new_option[24];
155
156         po_remove_all(options, "addr");
157
158         snprintf(new_option, sizeof(new_option) - 1,
159                         "addr=%s", inet_ntoa(saddr->sin_addr));
160
161         if (po_append(options, new_option) == PO_SUCCEEDED)
162                 return 1;
163         return 0;
164 }
165
166 /*
167  * Called to discover our address and append an appropriate 'clientaddr='
168  * option to the options string.
169  *
170  * Returns 1 if 'clientaddr=' option created successfully or if
171  * 'clientaddr=' option is already present; otherwise zero.
172  */
173 static int append_clientaddr_option(struct sockaddr_in *saddr,
174                                     struct mount_options *options)
175 {
176         struct sockaddr_in my_addr;
177         char new_option[32];
178
179         if (po_contains(options, "clientaddr") == PO_SUCCEEDED)
180                 return 1;
181
182         if (!get_client_address(saddr, &my_addr))
183                 return 0;
184
185         snprintf(new_option, sizeof(new_option) - 1,
186                         "clientaddr=%s", inet_ntoa(my_addr.sin_addr));
187
188         if (po_append(options, new_option) == PO_SUCCEEDED)
189                 return 1;
190         return 0;
191 }
192
193 /*
194  * Resolve the 'mounthost=' hostname and append a new option using
195  * the resulting IPv4 address.
196  */
197 static int fix_mounthost_option(struct mount_options *options)
198 {
199         struct sockaddr_in maddr;
200         char *mounthost, new_option[32];
201
202         mounthost = po_get(options, "mounthost");
203         if (!mounthost)
204                 return 1;
205
206         if (!fill_ipv4_sockaddr(mounthost, &maddr))
207                 return 0;
208
209         snprintf(new_option, sizeof(new_option) - 1,
210                         "mountaddr=%s", inet_ntoa(maddr.sin_addr));
211
212         if (po_append(options, new_option) == PO_SUCCEEDED)
213                 return 1;
214         return 0;
215 }
216
217 /*
218  * Set up mandatory mount options.
219  *
220  * Returns 1 if successful; otherwise zero.
221  */
222 static int set_mandatory_options(const char *type,
223                                  struct sockaddr_in *saddr,
224                                  struct mount_options *options)
225 {
226         if (!append_addr_option(saddr, options))
227                 return 0;
228
229         if (strncmp(type, "nfs4", 4) == 0) {
230                 if (!append_clientaddr_option(saddr, options))
231                         return 0;
232         } else {
233                 if (!fix_mounthost_option(options))
234                         return 0;
235         }
236
237         return 1;
238 }
239
240 /*
241  * Reconstruct the mount option string based on a portmapper probe
242  * of the server.  Returns one if the server's portmapper returned
243  * something we can use, otherwise zero.
244  *
245  * To handle version and transport protocol fallback properly, we
246  * need to parse some of the mount options in order to set up a
247  * portmap probe.  Mount options that rewrite_mount_options()
248  * doesn't recognize are left alone.
249  *
250  * Returns a new group of mount options if successful; otherwise
251  * NULL is returned if some failure occurred.
252  */
253 static struct mount_options *rewrite_mount_options(char *str)
254 {
255         struct mount_options *options;
256         char *option, new_option[64];
257         clnt_addr_t mnt_server = { };
258         clnt_addr_t nfs_server = { };
259         int p;
260
261         options = po_split(str);
262         if (!options)
263                 return NULL;
264
265         option = po_get(options, "addr");
266         if (option) {
267                 nfs_server.saddr.sin_family = AF_INET;
268                 if (!inet_aton((const char *)option, &nfs_server.saddr.sin_addr))
269                         goto err;
270         } else
271                 goto err;
272
273         option = po_get(options, "mountaddr");
274         if (option) {
275                 mnt_server.saddr.sin_family = AF_INET;
276                 if (!inet_aton((const char *)option, &mnt_server.saddr.sin_addr))
277                         goto err;
278         } else
279                 memcpy(&mnt_server.saddr, &nfs_server.saddr,
280                                 sizeof(mnt_server.saddr));
281
282         option = po_get(options, "mountport");
283         if (option)
284                 mnt_server.pmap.pm_port = atoi(option);
285         mnt_server.pmap.pm_prog = MOUNTPROG;
286         option = po_get(options, "mountprog");
287         if (option)
288                 mnt_server.pmap.pm_prog = atoi(option);
289         option = po_get(options, "mountvers");
290         if (option)
291                 mnt_server.pmap.pm_vers = atoi(option);
292
293         option = po_get(options, "port");
294         if (option) {
295                 nfs_server.pmap.pm_port = atoi(option);
296                 po_remove_all(options, "port");
297         }
298         nfs_server.pmap.pm_prog = NFS_PROGRAM;
299         option = po_get(options, "nfsprog");
300         if (option)
301                 nfs_server.pmap.pm_prog = atoi(option);
302
303         option = po_get(options, "nfsvers");
304         if (option) {
305                 nfs_server.pmap.pm_vers = atoi(option);
306                 po_remove_all(options, "nfsvers");
307         }
308         option = po_get(options, "vers");
309         if (option) {
310                 nfs_server.pmap.pm_vers = atoi(option);
311                 po_remove_all(options, "vers");
312         }
313         option = po_get(options, "proto");
314         if (option) {
315                 if (strcmp(option, "tcp") == 0) {
316                         nfs_server.pmap.pm_prot = IPPROTO_TCP;
317                         po_remove_all(options, "proto");
318                 }
319                 if (strcmp(option, "udp") == 0) {
320                         nfs_server.pmap.pm_prot = IPPROTO_UDP;
321                         po_remove_all(options, "proto");
322                 }
323         }
324         p = po_rightmost(options, "tcp", "udp");
325         switch (p) {
326         case PO_KEY2_RIGHTMOST:
327                 nfs_server.pmap.pm_prot = IPPROTO_UDP;
328                 break;
329         case PO_KEY1_RIGHTMOST:
330                 nfs_server.pmap.pm_prot = IPPROTO_TCP;
331                 break;
332         }
333         po_remove_all(options, "tcp");
334         po_remove_all(options, "udp");
335
336         if (!probe_bothports(&mnt_server, &nfs_server)) {
337                 rpc_mount_errors("rpcbind", 0, 0);
338                 goto err;
339         }
340
341         snprintf(new_option, sizeof(new_option) - 1,
342                  "nfsvers=%lu", nfs_server.pmap.pm_vers);
343         if (po_append(options, new_option) == PO_FAILED)
344                 goto err;
345
346         if (nfs_server.pmap.pm_prot == IPPROTO_TCP)
347                 snprintf(new_option, sizeof(new_option) - 1,
348                          "proto=tcp");
349         else
350                 snprintf(new_option, sizeof(new_option) - 1,
351                          "proto=udp");
352         if (po_append(options, new_option) == PO_FAILED)
353                 goto err;
354
355         if (nfs_server.pmap.pm_port != NFS_PORT) {
356                 snprintf(new_option, sizeof(new_option) - 1,
357                          "port=%lu", nfs_server.pmap.pm_port);
358                 if (po_append(options, new_option) == PO_FAILED)
359                         goto err;
360
361         }
362
363         return options;
364
365 err:
366         po_destroy(options);
367         return NULL;
368 }
369
370 /*
371  * Retry an NFS mount that failed because the requested service isn't
372  * available on the server.
373  *
374  * Returns 1 if successful.  Otherwise, returns zero.
375  * "errno" is set to reflect the individual error.
376  *
377  * Side effect: If the retry is successful, both 'options' and
378  * 'extra_opts' are updated to reflect the mount options that worked.
379  * If the retry fails, 'options' and 'extra_opts' are left unchanged.
380  */
381 static int retry_nfsmount(const char *spec, const char *node,
382                         int flags, struct mount_options *options,
383                         int fake, char **extra_opts)
384 {
385         struct mount_options *retry_options;
386         char *retry_str = NULL;
387
388         retry_options = rewrite_mount_options(*extra_opts);
389         if (!retry_options) {
390                 errno = EIO;
391                 return 0;
392         }
393
394         if (po_join(retry_options, &retry_str) == PO_FAILED) {
395                 po_destroy(retry_options);
396                 errno = EIO;
397                 return 0;
398         }
399
400         if (verbose)
401                 printf(_("%s: text-based options (retry): '%s'\n"),
402                         progname, retry_str);
403
404         if (!mount(spec, node, "nfs",
405                                 flags & ~(MS_USER|MS_USERS), retry_str)) {
406                 free(*extra_opts);
407                 *extra_opts = retry_str;
408                 po_replace(options, retry_options);
409                 return 1;
410         }
411
412         po_destroy(retry_options);
413         free(retry_str);
414         return 0;
415 }
416
417 /*
418  * Attempt an NFSv2/3 mount via a mount(2) system call.  If the kernel
419  * claims the requested service isn't supported on the server, probe
420  * the server to see what's supported, rewrite the mount options,
421  * and retry the request.
422  *
423  * Returns 1 if successful.  Otherwise, returns zero.
424  * "errno" is set to reflect the individual error.
425  *
426  * Side effect: If the retry is successful, both 'options' and
427  * 'extra_opts' are updated to reflect the mount options that worked.
428  * If the retry fails, 'options' and 'extra_opts' are left unchanged.
429  */
430 static int try_nfs23mount(const char *spec, const char *node,
431                           int flags, struct mount_options *options,
432                           int fake, char **extra_opts)
433 {
434         if (po_join(options, extra_opts) == PO_FAILED) {
435                 errno = EIO;
436                 return 0;
437         }
438
439         if (verbose)
440                 printf(_("%s: text-based options: '%s'\n"),
441                         progname, *extra_opts);
442
443         if (fake)
444                 return 1;
445
446         if (!mount(spec, node, "nfs",
447                                 flags & ~(MS_USER|MS_USERS), *extra_opts))
448                 return 1;
449
450         /*
451          * The kernel returns EOPNOTSUPP if the RPC bind failed,
452          * and EPROTONOSUPPORT if the version isn't supported.
453          */
454         if (errno != EOPNOTSUPP && errno != EPROTONOSUPPORT)
455                 return 0;
456
457         return retry_nfsmount(spec, node, flags, options, fake, extra_opts);
458 }
459
460 /*
461  * Attempt an NFS v4 mount via a mount(2) system call.
462  *
463  * Returns 1 if successful.  Otherwise, returns zero.
464  * "errno" is set to reflect the individual error.
465  */
466 static int try_nfs4mount(const char *spec, const char *node,
467                          int flags, struct mount_options *options,
468                          int fake, char **extra_opts)
469 {
470         if (po_join(options, extra_opts) == PO_FAILED) {
471                 errno = EIO;
472                 return 0;
473         }
474
475         if (verbose)
476                 printf(_("%s: text-based options: '%s'\n"),
477                         progname, *extra_opts);
478
479         if (fake)
480                 return 1;
481
482         if (!mount(spec, node, "nfs4",
483                                 flags & ~(MS_USER|MS_USERS), *extra_opts))
484                 return 1;
485         return 0;
486 }
487
488 /*
489  * Try the mount(2) system call.
490  *
491  * Returns 1 if successful.  Otherwise, returns zero.
492  * "errno" is set to reflect the individual error.
493  */
494 static int try_mount(const char *spec, const char *node, const char *type,
495                      int flags, struct mount_options *options, int fake,
496                      char **extra_opts)
497 {
498         if (strncmp(type, "nfs4", 4) == 0)
499                 return try_nfs4mount(spec, node, flags,
500                                         options, fake, extra_opts);
501         else
502                 return try_nfs23mount(spec, node, flags,
503                                         options, fake, extra_opts);
504 }
505
506 /**
507  * nfsmount_string - Mount an NFS file system using C string options
508  * @spec: C string specifying remote share to mount ("hostname:path")
509  * @node: C string pathname of local mounted-on directory
510  * @type: C string that represents file system type ("nfs" or "nfs4")
511  * @flags: MS_ style mount flags
512  * @extra_opts: pointer to C string containing fs-specific mount options
513  *              (input and output argument)
514  * @fake: flag indicating whether to carry out the whole operation
515  * @child: one if this is a mount daemon (bg)
516  */
517 int nfsmount_string(const char *spec, const char *node, const char *type,
518                     int flags, char **extra_opts, int fake, int child)
519 {
520         struct mount_options *options = NULL;
521         struct sockaddr_in saddr;
522         char *hostname;
523         int err, retval = EX_FAIL;
524
525         if (!parse_devname(spec, &hostname))
526                 goto out;
527         err = fill_ipv4_sockaddr(hostname, &saddr);
528         free(hostname);
529         if (!err)
530                 goto out;
531
532         options = po_split(*extra_opts);
533         if (!options) {
534                 nfs_error(_("%s: internal option parsing error"), progname);
535                 goto out;
536         }
537
538         if (!set_mandatory_options(type, &saddr, options))
539                 goto out;
540
541         if (try_mount(spec, node, type, flags, options, fake, extra_opts)) {
542                 mount_error(spec, node, errno);
543                 goto out;
544         }
545
546         retval = EX_SUCCESS;
547
548 out:
549         po_destroy(options);
550         return retval;
551 }