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