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