]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfs4mount.c
Clean up: remove unused IPv4-only functions used by the text-based mount
[nfs-utils.git] / utils / mount / nfs4mount.c
1 /*
2  * nfs4mount.c -- Linux NFS mount
3  * Copyright (C) 2002 Trond Myklebust <trond.myklebust@fys.uio.no>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * Note: this file based on the original nfsmount.c
16  *
17  * 2006-06-06 Amit Gud <agud@redhat.com>
18  * - Moved to nfs-utils/utils/mount from util-linux/mount.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <netdb.h>
30 #include <time.h>
31 #include <sys/stat.h>
32 #include <sys/mount.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <rpc/auth.h>
36 #include <rpc/rpc.h>
37 #ifdef HAVE_RPCSVC_NFS_PROT_H
38 #include <rpcsvc/nfs_prot.h>
39 #else
40 #include <linux/nfs.h>
41 #define nfsstat nfs_stat
42 #endif
43
44 #include "pseudoflavors.h"
45 #include "nls.h"
46 #include "xcommon.h"
47
48 #include "mount_constants.h"
49 #include "nfs4_mount.h"
50 #include "nfs_mount.h"
51 #include "error.h"
52 #include "network.h"
53
54 #if defined(VAR_LOCK_DIR)
55 #define DEFAULT_DIR VAR_LOCK_DIR
56 #else
57 #define DEFAULT_DIR "/var/lock/subsys"
58 #endif
59
60 extern char *progname;
61 extern int verbose;
62 extern int sloppy;
63
64 char *IDMAPLCK = DEFAULT_DIR "/rpcidmapd";
65 #define idmapd_check() do { \
66         if (access(IDMAPLCK, F_OK)) { \
67                 printf(_("Warning: rpc.idmapd appears not to be running.\n" \
68                         "         All uids will be mapped to the nobody uid.\n")); \
69         } \
70 } while(0);
71
72 char *GSSDLCK = DEFAULT_DIR "/rpcgssd";
73 #define gssd_check() do { \
74                 if (access(GSSDLCK, F_OK)) { \
75                         printf(_("Warning: rpc.gssd appears not to be running.\n")); \
76                 } \
77 } while(0);
78
79 #ifndef NFS_PORT
80 #define NFS_PORT 2049
81 #endif
82
83 #define MAX_USER_FLAVOUR        16
84
85 static int parse_sec(char *sec, int *pseudoflavour)
86 {
87         int i, num_flavour = 0;
88
89         for (sec = strtok(sec, ":"); sec; sec = strtok(NULL, ":")) {
90                 if (num_flavour >= MAX_USER_FLAVOUR) {
91                         nfs_error(_("%s: maximum number of security flavors "
92                                   "exceeded"), progname);
93                         return 0;
94                 }
95                 for (i = 0; i < flav_map_size; i++) {
96                         if (strcmp(sec, flav_map[i].flavour) == 0) {
97                                 pseudoflavour[num_flavour++] = flav_map[i].fnum;
98                                 break;
99                         }
100                 }
101                 if (i == flav_map_size) {
102                         nfs_error(_("%s: unknown security type %s\n"),
103                                         progname, sec);
104                         return 0;
105                 }
106         }
107         if (!num_flavour)
108                 nfs_error(_("%s: no security flavors passed to sec= option"),
109                                 progname);
110         return num_flavour;
111 }
112
113 static int parse_devname(char *hostdir, char **hostname, char **dirname)
114 {
115         char *s;
116
117         if (!(s = strchr(hostdir, ':'))) {
118                 nfs_error(_("%s: directory to mount not in host:dir format"),
119                                 progname);
120                 return -1;
121         }
122         *hostname = hostdir;
123         *dirname = s + 1;
124         *s = '\0';
125         /* Ignore all but first hostname in replicated mounts
126            until they can be fully supported. (mack@sgi.com) */
127         if ((s = strchr(hostdir, ','))) {
128                 *s = '\0';
129                 nfs_error(_("%s: warning: multiple hostnames not supported"),
130                                 progname);
131         }
132         return 0;
133 }
134
135 static int fill_ipv4_sockaddr(const char *hostname, struct sockaddr_in *addr)
136 {
137         struct hostent *hp;
138         addr->sin_family = AF_INET;
139
140         if (inet_aton(hostname, &addr->sin_addr))
141                 return 0;
142         if ((hp = gethostbyname(hostname)) == NULL) {
143                 nfs_error(_("%s: can't get address for %s\n"),
144                                 progname, hostname);
145                 return -1;
146         }
147         if (hp->h_length > sizeof(struct in_addr)) {
148                 nfs_error(_("%s: got bad hp->h_length"), progname);
149                 hp->h_length = sizeof(struct in_addr);
150         }
151         memcpy(&addr->sin_addr, hp->h_addr, hp->h_length);
152         return 0;
153 }
154
155 static int get_my_ipv4addr(char *ip_addr, int len)
156 {
157         char myname[1024];
158         struct sockaddr_in myaddr;
159
160         if (gethostname(myname, sizeof(myname))) {
161                 nfs_error(_("%s: can't determine client address\n"),
162                                 progname);
163                 return -1;
164         }
165         if (fill_ipv4_sockaddr(myname, &myaddr))
166                 return -1;
167         snprintf(ip_addr, len, "%s", inet_ntoa(myaddr.sin_addr));
168         ip_addr[len-1] = '\0';
169         return 0;
170 }
171
172 int nfs4mount(const char *spec, const char *node, int flags,
173               char **extra_opts, int fake, int running_bg)
174 {
175         static struct nfs4_mount_data data;
176         static char hostdir[1024];
177         static char ip_addr[16] = "127.0.0.1";
178         static struct sockaddr_in server_addr, client_addr;
179         static int pseudoflavour[MAX_USER_FLAVOUR];
180         int num_flavour = 0;
181         int ip_addr_in_opts = 0;
182
183         char *hostname, *dirname, *old_opts;
184         char new_opts[1024];
185         char *opt, *opteq;
186         char *s;
187         int val;
188         int bg, soft, intr;
189         int nocto, noac, unshared;
190         int retry;
191         int retval = EX_FAIL;
192         time_t timeout, t;
193
194         if (strlen(spec) >= sizeof(hostdir)) {
195                 nfs_error(_("%s: excessively long host:dir argument\n"),
196                                 progname);
197                 goto fail;
198         }
199         strcpy(hostdir, spec);
200         if (parse_devname(hostdir, &hostname, &dirname))
201                 goto fail;
202
203         if (fill_ipv4_sockaddr(hostname, &server_addr))
204                 goto fail;
205         if (get_my_ipv4addr(ip_addr, sizeof(ip_addr)))
206                 goto fail;
207
208         /* add IP address to mtab options for use when unmounting */
209         s = inet_ntoa(server_addr.sin_addr);
210         old_opts = *extra_opts;
211         if (!old_opts)
212                 old_opts = "";
213         if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
214                 nfs_error(_("%s: excessively long option argument\n"),
215                                 progname);
216                 goto fail;
217         }
218         snprintf(new_opts, sizeof(new_opts), "%s%saddr=%s",
219                  old_opts, *old_opts ? "," : "", s);
220         *extra_opts = xstrdup(new_opts);
221
222         /* Set default options.
223          * rsize/wsize and timeo are left 0 in order to
224          * let the kernel decide.
225          */
226         memset(&data, 0, sizeof(data));
227         data.retrans    = 3;
228         data.acregmin   = 3;
229         data.acregmax   = 60;
230         data.acdirmin   = 30;
231         data.acdirmax   = 60;
232         data.proto      = IPPROTO_TCP;
233
234         bg = 0;
235         soft = 0;
236         intr = NFS4_MOUNT_INTR;
237         nocto = 0;
238         noac = 0;
239         unshared = 0;
240         retry = -1;
241
242         /*
243          * NFSv4 specifies that the default port should be 2049
244          */
245         server_addr.sin_port = htons(NFS_PORT);
246
247         /* parse options */
248
249         for (opt = strtok(old_opts, ","); opt; opt = strtok(NULL, ",")) {
250                 if ((opteq = strchr(opt, '='))) {
251                         val = atoi(opteq + 1);  
252                         *opteq = '\0';
253                         if (!strcmp(opt, "rsize"))
254                                 data.rsize = val;
255                         else if (!strcmp(opt, "wsize"))
256                                 data.wsize = val;
257                         else if (!strcmp(opt, "timeo"))
258                                 data.timeo = val;
259                         else if (!strcmp(opt, "retrans"))
260                                 data.retrans = val;
261                         else if (!strcmp(opt, "acregmin"))
262                                 data.acregmin = val;
263                         else if (!strcmp(opt, "acregmax"))
264                                 data.acregmax = val;
265                         else if (!strcmp(opt, "acdirmin"))
266                                 data.acdirmin = val;
267                         else if (!strcmp(opt, "acdirmax"))
268                                 data.acdirmax = val;
269                         else if (!strcmp(opt, "actimeo")) {
270                                 data.acregmin = val;
271                                 data.acregmax = val;
272                                 data.acdirmin = val;
273                                 data.acdirmax = val;
274                         }
275                         else if (!strcmp(opt, "retry"))
276                                 retry = val;
277                         else if (!strcmp(opt, "port"))
278                                 server_addr.sin_port = htons(val);
279                         else if (!strcmp(opt, "proto")) {
280                                 if (!strncmp(opteq+1, "tcp", 3))
281                                         data.proto = IPPROTO_TCP;
282                                 else if (!strncmp(opteq+1, "udp", 3))
283                                         data.proto = IPPROTO_UDP;
284                                 else
285                                         printf(_("Warning: Unrecognized proto= option.\n"));
286                         } else if (!strcmp(opt, "clientaddr")) {
287                                 if (strlen(opteq+1) >= sizeof(ip_addr))
288                                         printf(_("Invalid client address %s"),
289                                                                 opteq+1);
290                                 strncpy(ip_addr,opteq+1, sizeof(ip_addr));
291                                 ip_addr[sizeof(ip_addr)-1] = '\0';
292                                 ip_addr_in_opts = 1;
293                         } else if (!strcmp(opt, "sec")) {
294                                 num_flavour = parse_sec(opteq+1, pseudoflavour);
295                                 if (!num_flavour)
296                                         goto fail;
297                         } else if (!strcmp(opt, "addr") || sloppy) {
298                                 /* ignore */;
299                         } else {
300                                 printf(_("unknown nfs mount parameter: "
301                                          "%s=%d\n"), opt, val);
302                                 goto fail;
303                         }
304                 } else {
305                         val = 1;
306                         if (!strncmp(opt, "no", 2)) {
307                                 val = 0;
308                                 opt += 2;
309                         }
310                         if (!strcmp(opt, "bg"))
311                                 bg = val;
312                         else if (!strcmp(opt, "fg"))
313                                 bg = !val;
314                         else if (!strcmp(opt, "soft"))
315                                 soft = val;
316                         else if (!strcmp(opt, "hard"))
317                                 soft = !val;
318                         else if (!strcmp(opt, "intr"))
319                                 intr = val;
320                         else if (!strcmp(opt, "cto"))
321                                 nocto = !val;
322                         else if (!strcmp(opt, "ac"))
323                                 noac = !val;
324                         else if (!strcmp(opt, "sharecache"))
325                                 unshared = !val;
326                         else if (!sloppy) {
327                                 printf(_("unknown nfs mount option: %s%s\n"),
328                                                 val ? "" : "no", opt);
329                                 goto fail;
330                         }
331                 }
332         }
333
334         /* if retry is still -1, then it wasn't set via an option */
335         if (retry == -1) {
336                 if (bg)
337                         retry = 10000;  /* 10000 mins == ~1 week */
338                 else
339                         retry = 2;      /* 2 min default on fg mounts */
340         }
341
342         data.flags = (soft ? NFS4_MOUNT_SOFT : 0)
343                 | (intr ? NFS4_MOUNT_INTR : 0)
344                 | (nocto ? NFS4_MOUNT_NOCTO : 0)
345                 | (noac ? NFS4_MOUNT_NOAC : 0)
346                 | (unshared ? NFS4_MOUNT_UNSHARED : 0);
347
348         /*
349          * Give a warning if the rpc.idmapd daemon is not running
350          */
351 #if 0
352         /* We shouldn't have these checks as nothing in this package
353          * creates the files that are checked
354          */
355         idmapd_check();
356
357         if (num_flavour == 0)
358                 pseudoflavour[num_flavour++] = AUTH_UNIX;
359         else {
360                 /*
361                  * ditto with rpc.gssd daemon
362                  */
363                 gssd_check();
364         }
365 #endif
366         data.auth_flavourlen = num_flavour;
367         data.auth_flavours = pseudoflavour;
368
369         data.client_addr.data = ip_addr;
370         data.client_addr.len = strlen(ip_addr);
371
372         data.mnt_path.data = dirname;
373         data.mnt_path.len = strlen(dirname);
374
375         data.hostname.data = hostname;
376         data.hostname.len = strlen(hostname);
377         data.host_addr = (struct sockaddr *)&server_addr;
378         data.host_addrlen = sizeof(server_addr);
379
380 #ifdef NFS_MOUNT_DEBUG
381         printf(_("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n"),
382                data.rsize, data.wsize, data.timeo, data.retrans);
383         printf(_("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n"),
384                data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
385         printf(_("port = %d, bg = %d, retry = %d, flags = %.8x\n"),
386                ntohs(server_addr.sin_port), bg, retry, data.flags);
387         printf(_("soft = %d, intr = %d, nocto = %d, noac = %d, "
388                "nosharecache = %d\n"),
389                (data.flags & NFS4_MOUNT_SOFT) != 0,
390                (data.flags & NFS4_MOUNT_INTR) != 0,
391                (data.flags & NFS4_MOUNT_NOCTO) != 0,
392                (data.flags & NFS4_MOUNT_NOAC) != 0,
393                (data.flags & NFS4_MOUNT_UNSHARED) != 0);
394
395         if (num_flavour > 0) {
396                 int pf_cnt, i;
397
398                 printf(_("sec = "));
399                 for (pf_cnt = 0; pf_cnt < num_flavour; pf_cnt++) {
400                         for (i = 0; i < flav_map_size; i++) {
401                                 if (flav_map[i].fnum == pseudoflavour[pf_cnt]) {
402                                         printf("%s", flav_map[i].flavour);
403                                         break;
404                                 }
405                         }
406                         printf("%s", (pf_cnt < num_flavour-1) ? ":" : "\n");
407                 }
408         }
409         printf(_("proto = %s\n"), (data.proto == IPPROTO_TCP) ? _("tcp") : _("udp"));
410 #endif
411
412         timeout = time(NULL) + 60 * retry;
413         data.version = NFS4_MOUNT_VERSION;
414         for (;;) {
415                 if (verbose) {
416                         printf(_("%s: pinging: prog %d vers %d prot %s port %d\n"),
417                                 progname, NFS_PROGRAM, 4,
418                                 data.proto == IPPROTO_UDP ? "udp" : "tcp",
419                                 ntohs(server_addr.sin_port));
420                 }
421                 client_addr.sin_family = 0;
422                 client_addr.sin_addr.s_addr = 0;
423                 clnt_ping(&server_addr, NFS_PROGRAM, 4, data.proto, &client_addr);
424                 if (rpc_createerr.cf_stat == RPC_SUCCESS) {
425                         if (!ip_addr_in_opts &&
426                             client_addr.sin_family != 0 &&
427                             client_addr.sin_addr.s_addr != 0) {
428                                 snprintf(ip_addr, sizeof(ip_addr), "%s",
429                                          inet_ntoa(client_addr.sin_addr));
430                                 data.client_addr.len = strlen(ip_addr);
431                         }
432                         break;
433                 }
434
435                 switch(rpc_createerr.cf_stat){
436                 case RPC_TIMEDOUT:
437                         break;
438                 case RPC_SYSTEMERROR:
439                         if (errno == ETIMEDOUT)
440                                 break;
441                 default:
442                         rpc_mount_errors(hostname, 0, bg);
443                         goto fail;
444                 }
445
446                 if (bg && !running_bg) {
447                         if (retry > 0)
448                                 retval = EX_BG;
449                         goto fail;
450                 }
451
452                 t = time(NULL);
453                 if (t >= timeout) {
454                         rpc_mount_errors(hostname, 0, bg);
455                         goto fail;
456                 }
457                 rpc_mount_errors(hostname, 1, bg);
458                 continue;
459         }
460
461         if (!fake) {
462                 if (mount(spec, node, "nfs4",
463                                 flags & ~(MS_USER|MS_USERS), &data)) {
464                         mount_error(spec, node, errno);
465                         goto fail;
466                 }
467         }
468
469         return EX_SUCCESS;
470
471 fail:
472         return retval;
473 }