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