]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/stropts.c
text-based mount.nfs: combine nfsmount_s() and nfs4mount_s() paths
[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  * nfsmount_s - Mount an NFSv2 or v3 file system using C string options
242  *
243  * @spec:       C string hostname:path specifying remoteshare to mount
244  * @node:       C string pathname of local mounted on directory
245  * @flags:      MS_ style flags
246  * @extra_opts: pointer to C string containing fs-specific mount options
247  *              (possibly also a return argument)
248  * @fake:       flag indicating whether to carry out the whole operation
249  * @child:      one if this is a backgrounded mount
250  *
251  * XXX: need to handle bg, fg, and retry options.
252  */
253 int nfsmount_s(const char *spec, const char *node, int flags,
254                 char **extra_opts, int fake, int child)
255 {
256         struct mount_options *options = NULL;
257         struct sockaddr_in saddr;
258         char *hostname;
259         int err, retval = EX_FAIL;
260
261         if (!parse_devname(spec, &hostname))
262                 goto out;
263         err = fill_ipv4_sockaddr(hostname, &saddr);
264         free(hostname);
265         if (!err)
266                 goto out;
267
268         options = po_split(*extra_opts);
269         if (!options) {
270                 nfs_error(_("%s: internal option parsing error"), progname);
271                 goto out;
272         }
273
274         if (!append_addr_option(&saddr, options))
275                 goto out;
276
277         if (!fix_mounthost_option(options))
278                 goto out;
279
280         if (po_join(options, extra_opts) == PO_FAILED) {
281                 nfs_error(_("%s: internal option parsing error"), progname);
282                 goto out;
283         }
284
285         if (verbose)
286                 printf(_("%s: text-based options: '%s'\n"),
287                         progname, *extra_opts);
288
289         if (!fake) {
290                 if (mount(spec, node, "nfs",
291                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
292                         mount_error(spec, node, errno);
293                         goto out;
294                 }
295         }
296
297         retval = EX_SUCCESS;
298
299 out:
300         po_destroy(options);
301         return retval;
302 }
303
304 /*
305  * nfs4mount_s - Mount an NFSv4 file system using C string options
306  *
307  * @spec:       C string hostname:path specifying remoteshare to mount
308  * @node:       C string pathname of local mounted on directory
309  * @flags:      MS_ style flags
310  * @extra_opts: pointer to C string containing fs-specific mount options
311  *              (possibly also a return argument)
312  * @fake:       flag indicating whether to carry out the whole operation
313  * @child:      one if this is a backgrounded mount
314  *
315  * XXX: need to handle bg, fg, and retry options.
316  *
317  */
318 int nfs4mount_s(const char *spec, const char *node, int flags,
319                 char **extra_opts, int fake, int child)
320 {
321         struct mount_options *options = NULL;
322         struct sockaddr_in saddr;
323         char *hostname;
324         int err, retval = EX_FAIL;
325
326         if (!parse_devname(spec, &hostname))
327                 goto out;
328         err = fill_ipv4_sockaddr(hostname, &saddr);
329         free(hostname);
330         if (!err)
331                 goto out;
332
333         options = po_split(*extra_opts);
334         if (!options) {
335                 nfs_error(_("%s: internal option parsing error"), progname);
336                 goto out;
337         }
338
339         if (!append_addr_option(&saddr, options))
340                 goto out;
341
342         if (!append_clientaddr_option(&saddr, options))
343                 goto out;
344
345         if (po_join(options, extra_opts) == PO_FAILED) {
346                 nfs_error(_("%s: internal option parsing error"), progname);
347                 goto out;
348         }
349
350         if (verbose)
351                 printf(_("%s: text-based options: '%s'\n"),
352                         progname, *extra_opts);
353
354         if (!fake) {
355                 if (mount(spec, node, "nfs4",
356                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
357                         mount_error(spec, node, errno);
358                         goto out;
359                 }
360         }
361
362         retval = EX_SUCCESS;
363
364 out:
365         po_destroy(options);
366         return retval;
367 }
368
369 /**
370  * nfsmount_string - Mount an NFS file system using C string options
371  * @spec: C string specifying remote share to mount ("hostname:path")
372  * @node: C string pathname of local mounted-on directory
373  * @type: C string that represents file system type ("nfs" or "nfs4")
374  * @flags: MS_ style mount flags
375  * @extra_opts: pointer to C string containing fs-specific mount options
376  *              (input and output argument)
377  * @fake: flag indicating whether to carry out the whole operation
378  * @child: one if this is a mount daemon (bg)
379  */
380 int nfsmount_string(const char *spec, const char *node, const char *type,
381                     int flags, char **extra_opts, int fake, int child)
382 {
383         struct mount_options *options = NULL;
384         struct sockaddr_in saddr;
385         char *hostname;
386         int err, retval = EX_FAIL;
387
388         if (!parse_devname(spec, &hostname))
389                 goto out;
390         err = fill_ipv4_sockaddr(hostname, &saddr);
391         free(hostname);
392         if (!err)
393                 goto out;
394
395         options = po_split(*extra_opts);
396         if (!options) {
397                 nfs_error(_("%s: internal option parsing error"), progname);
398                 goto out;
399         }
400
401         if (!set_mandatory_options(type, &saddr, options))
402                 goto out;
403
404         if (po_join(options, extra_opts) == PO_FAILED) {
405                 nfs_error(_("%s: internal option parsing error"), progname);
406                 goto out;
407         }
408
409         if (verbose)
410                 printf(_("%s: text-based options: '%s'\n"),
411                         progname, *extra_opts);
412
413         if (!fake) {
414                 if (mount(spec, node, type,
415                                 flags & ~(MS_USER|MS_USERS), *extra_opts)) {
416                         mount_error(spec, node, errno);
417                         goto out;
418                 }
419         }
420
421         retval = EX_SUCCESS;
422
423 out:
424         po_destroy(options);
425         return retval;
426 }