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