6c0c3655b5c23768825c812fc99b13cf043712ec
[nfs-utils.git] / utils / mount / nfsmount.c
1 /*
2  * nfsmount.c -- Linux NFS mount
3  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
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  * Wed Feb  8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
16  * numbers to be specified on the command line.
17  *
18  * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
19  * Omit the call to connect() for Linux version 1.3.11 or later.
20  *
21  * Wed Oct  1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
22  * Implemented the "bg", "fg" and "retry" mount options for NFS.
23  *
24  * 1999-02-22 Arkadiusz Mi¬∂kiewicz <misiek@pld.ORG.PL>
25  * - added Native Language Support
26  *
27  * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
28  * plus NFSv3 stuff.
29  *
30  * 2006-06-06 Amit Gud <agud@redhat.com>
31  * - Moved with modifcations to nfs-utils/utils/mount from util-linux/mount.
32  */
33
34 /*
35  * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <ctype.h>
43 #include <unistd.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 #include <netdb.h>
49 #include <time.h>
50 #include <rpc/rpc.h>
51 #include <rpc/pmap_prot.h>
52 #include <rpc/pmap_clnt.h>
53 #include <sys/socket.h>
54 #include <sys/time.h>
55 #include <sys/stat.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58 #include <mntent.h>
59 #include <sys/mount.h>
60 #include <paths.h>
61 #include <syslog.h>
62
63 #include "xcommon.h"
64 #include "mount.h"
65 #include "nfs_mount.h"
66 #include "mount_constants.h"
67 #include "nls.h"
68 #include "error.h"
69 #include "network.h"
70
71 #ifndef NFS_PORT
72 #define NFS_PORT 2049
73 #endif
74 #ifndef NFS_FHSIZE
75 #define NFS_FHSIZE 32
76 #endif
77
78 #ifndef HAVE_INET_ATON
79 #define inet_aton(a,b) (0)
80 #endif
81
82 typedef dirpath mnt2arg_t;
83 typedef dirpath mnt3arg_t;
84 typedef dirpath mntarg_t;
85
86 typedef struct fhstatus  mnt2res_t;
87 typedef struct mountres3 mnt3res_t;
88 typedef union {
89         mnt2res_t nfsv2;
90         mnt3res_t nfsv3;
91 } mntres_t;
92
93 extern int nfs_mount_data_version;
94 extern char *progname;
95 extern int verbose;
96 extern int sloppy;
97
98 extern int linux_version_code(void);
99
100 static inline enum clnt_stat
101 nfs3_mount(CLIENT *clnt, mnt3arg_t *mnt3arg, mnt3res_t *mnt3res)
102 {
103         return clnt_call(clnt, MOUNTPROC3_MNT,
104                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt3arg,
105                          (xdrproc_t) xdr_mountres3, (caddr_t) mnt3res,
106                          TIMEOUT);
107 }
108
109 static inline enum clnt_stat
110 nfs2_mount(CLIENT *clnt, mnt2arg_t *mnt2arg, mnt2res_t *mnt2res)
111 {
112         return clnt_call(clnt, MOUNTPROC_MNT,
113                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt2arg,
114                          (xdrproc_t) xdr_fhstatus, (caddr_t) mnt2res,
115                          TIMEOUT);
116 }
117
118 static int
119 nfs_call_mount(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server,
120                mntarg_t *mntarg, mntres_t *mntres)
121 {
122         CLIENT *clnt;
123         enum clnt_stat stat;
124         int msock;
125
126         if (!probe_bothports(mnt_server, nfs_server))
127                 goto out_bad;
128
129         clnt = mnt_openclnt(mnt_server, &msock);
130         if (!clnt)
131                 goto out_bad;
132         /* make pointers in xdr_mountres3 NULL so
133          * that xdr_array allocates memory for us
134          */
135         memset(mntres, 0, sizeof(*mntres));
136         switch (mnt_server->pmap.pm_vers) {
137         case 3:
138                 stat = nfs3_mount(clnt, mntarg, &mntres->nfsv3);
139                 break;
140         case 2:
141         case 1:
142                 stat = nfs2_mount(clnt, mntarg, &mntres->nfsv2);
143                 break;
144         default:
145                 goto out_bad;
146         }
147         if (stat != RPC_SUCCESS) {
148                 clnt_geterr(clnt, &rpc_createerr.cf_error);
149                 rpc_createerr.cf_stat = stat;
150         }
151         mnt_closeclnt(clnt, msock);
152         if (stat == RPC_SUCCESS)
153                 return 1;
154  out_bad:
155         return 0;
156 }
157
158 static int
159 parse_options(char *old_opts, struct nfs_mount_data *data,
160               int *bg, int *retry, clnt_addr_t *mnt_server,
161               clnt_addr_t *nfs_server, char *new_opts, const int opt_size)
162 {
163         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
164         struct pmap *mnt_pmap = &mnt_server->pmap;
165         struct pmap *nfs_pmap = &nfs_server->pmap;
166         int len;
167         char *opt, *opteq, *p, *opt_b;
168         char *mounthost = NULL;
169         char cbuf[128];
170         int open_quote = 0;
171
172         data->flags = 0;
173         *bg = 0;
174
175         len = strlen(new_opts);
176         for (p=old_opts, opt_b=NULL; p && *p; p++) {
177                 if (!opt_b)
178                         opt_b = p;              /* begin of the option item */
179                 if (*p == '"')
180                         open_quote ^= 1;        /* reverse the status */
181                 if (open_quote)
182                         continue;               /* still in a quoted block */
183                 if (*p == ',')
184                         *p = '\0';              /* terminate the option item */
185                 if (*p == '\0' || *(p+1) == '\0') {
186                         opt = opt_b;            /* opt is useful now */
187                         opt_b = NULL;
188                 }
189                 else
190                         continue;               /* still somewhere in the option item */
191
192                 if (strlen(opt) >= sizeof(cbuf))
193                         goto bad_parameter;
194                 if ((opteq = strchr(opt, '=')) && isdigit(opteq[1])) {
195                         int val = atoi(opteq + 1);      
196                         *opteq = '\0';
197                         if (!strcmp(opt, "rsize"))
198                                 data->rsize = val;
199                         else if (!strcmp(opt, "wsize"))
200                                 data->wsize = val;
201                         else if (!strcmp(opt, "timeo"))
202                                 data->timeo = val;
203                         else if (!strcmp(opt, "retrans"))
204                                 data->retrans = val;
205                         else if (!strcmp(opt, "acregmin"))
206                                 data->acregmin = val;
207                         else if (!strcmp(opt, "acregmax"))
208                                 data->acregmax = val;
209                         else if (!strcmp(opt, "acdirmin"))
210                                 data->acdirmin = val;
211                         else if (!strcmp(opt, "acdirmax"))
212                                 data->acdirmax = val;
213                         else if (!strcmp(opt, "actimeo")) {
214                                 data->acregmin = val;
215                                 data->acregmax = val;
216                                 data->acdirmin = val;
217                                 data->acdirmax = val;
218                         }
219                         else if (!strcmp(opt, "retry"))
220                                 *retry = val;
221                         else if (!strcmp(opt, "port"))
222                                 nfs_pmap->pm_port = val;
223                         else if (!strcmp(opt, "mountport"))
224                                 mnt_pmap->pm_port = val;
225                         else if (!strcmp(opt, "mountprog"))
226                                 mnt_pmap->pm_prog = val;
227                         else if (!strcmp(opt, "mountvers"))
228                                 mnt_pmap->pm_vers = val;
229                         else if (!strcmp(opt, "mounthost"))
230                                 mounthost=xstrndup(opteq+1, strcspn(opteq+1," \t\n\r,"));
231                         else if (!strcmp(opt, "nfsprog"))
232                                 nfs_pmap->pm_prog = val;
233                         else if (!strcmp(opt, "nfsvers") ||
234                                  !strcmp(opt, "vers")) {
235                                 nfs_pmap->pm_vers = val;
236                                 opt = "nfsvers";
237 #if NFS_MOUNT_VERSION >= 2
238                         } else if (!strcmp(opt, "namlen")) {
239                                 if (nfs_mount_data_version >= 2)
240                                         data->namlen = val;
241                                 else if (sloppy)
242                                         continue;
243                                 else
244                                         goto bad_parameter;
245 #endif
246                         } else if (!strcmp(opt, "addr")) {
247                                 /* ignore */;
248                                 continue;
249                         } else if (sloppy)
250                                 continue;
251                         else
252                                 goto bad_parameter;
253                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
254                 } else if (opteq) {
255                         *opteq = '\0';
256                         if (!strcmp(opt, "proto")) {
257                                 if (!strcmp(opteq+1, "udp")) {
258                                         nfs_pmap->pm_prot = IPPROTO_UDP;
259                                         mnt_pmap->pm_prot = IPPROTO_UDP;
260 #if NFS_MOUNT_VERSION >= 2
261                                         data->flags &= ~NFS_MOUNT_TCP;
262                                 } else if (!strcmp(opteq+1, "tcp") &&
263                                            nfs_mount_data_version > 2) {
264                                         nfs_pmap->pm_prot = IPPROTO_TCP;
265                                         mnt_pmap->pm_prot = IPPROTO_TCP;
266                                         data->flags |= NFS_MOUNT_TCP;
267 #endif
268                                 } else if (sloppy)
269                                         continue;
270                                 else
271                                         goto bad_parameter;
272 #if NFS_MOUNT_VERSION >= 5
273                         } else if (!strcmp(opt, "sec")) {
274                                 char *secflavor = opteq+1;
275                                 /* see RFC 2623 */
276                                 if (nfs_mount_data_version < 5) {
277                                         printf(_("Warning: ignoring sec=%s option\n"),
278                                                         secflavor);
279                                         continue;
280                                 } else if (!strcmp(secflavor, "none"))
281                                         data->pseudoflavor = AUTH_NONE;
282                                 else if (!strcmp(secflavor, "sys"))
283                                         data->pseudoflavor = AUTH_SYS;
284                                 else if (!strcmp(secflavor, "krb5"))
285                                         data->pseudoflavor = AUTH_GSS_KRB5;
286                                 else if (!strcmp(secflavor, "krb5i"))
287                                         data->pseudoflavor = AUTH_GSS_KRB5I;
288                                 else if (!strcmp(secflavor, "krb5p"))
289                                         data->pseudoflavor = AUTH_GSS_KRB5P;
290                                 else if (!strcmp(secflavor, "lipkey"))
291                                         data->pseudoflavor = AUTH_GSS_LKEY;
292                                 else if (!strcmp(secflavor, "lipkey-i"))
293                                         data->pseudoflavor = AUTH_GSS_LKEYI;
294                                 else if (!strcmp(secflavor, "lipkey-p"))
295                                         data->pseudoflavor = AUTH_GSS_LKEYP;
296                                 else if (!strcmp(secflavor, "spkm3"))
297                                         data->pseudoflavor = AUTH_GSS_SPKM;
298                                 else if (!strcmp(secflavor, "spkm3i"))
299                                         data->pseudoflavor = AUTH_GSS_SPKMI;
300                                 else if (!strcmp(secflavor, "spkm3p"))
301                                         data->pseudoflavor = AUTH_GSS_SPKMP;
302                                 else if (sloppy)
303                                         continue;
304                                 else {
305                                         printf(_("Warning: Unrecognized security flavor %s.\n"),
306                                                 secflavor);
307                                         goto bad_parameter;
308                                 }
309                                 data->flags |= NFS_MOUNT_SECFLAVOUR;
310 #endif
311                         } else if (!strcmp(opt, "mounthost"))
312                                 mounthost=xstrndup(opteq+1,
313                                                    strcspn(opteq+1," \t\n\r,"));
314                          else if (!strcmp(opt, "context")) {
315                                 char *context = opteq + 1;
316                                 int ctxlen = strlen(context);
317
318                                 if (ctxlen > NFS_MAX_CONTEXT_LEN) {
319                                         nfs_error(_("context parameter exceeds"
320                                                         " limit of %d"),
321                                                         NFS_MAX_CONTEXT_LEN);
322                                         goto bad_parameter;
323                                 }
324                                 /* The context string is in the format of
325                                  * "system_u:object_r:...".  We only want
326                                  * the context str between the quotes.
327                                  */
328                                 if (*context == '"')
329                                         strncpy(data->context, context+1,
330                                                         ctxlen-2);
331                                 else
332                                         strncpy(data->context, context,
333                                                         NFS_MAX_CONTEXT_LEN);
334                         } else if (sloppy)
335                                 continue;
336                         else
337                                 goto bad_parameter;
338                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
339                 } else {
340                         int val = 1;
341                         if (!strncmp(opt, "no", 2)) {
342                                 val = 0;
343                                 opt += 2;
344                         }
345                         if (!strcmp(opt, "bg"))
346                                 *bg = val;
347                         else if (!strcmp(opt, "fg"))
348                                 *bg = !val;
349                         else if (!strcmp(opt, "soft")) {
350                                 data->flags &= ~NFS_MOUNT_SOFT;
351                                 if (val)
352                                         data->flags |= NFS_MOUNT_SOFT;
353                         } else if (!strcmp(opt, "hard")) {
354                                 data->flags &= ~NFS_MOUNT_SOFT;
355                                 if (!val)
356                                         data->flags |= NFS_MOUNT_SOFT;
357                         } else if (!strcmp(opt, "intr")) {
358                                 data->flags &= ~NFS_MOUNT_INTR;
359                                 if (val)
360                                         data->flags |= NFS_MOUNT_INTR;
361                         } else if (!strcmp(opt, "posix")) {
362                                 data->flags &= ~NFS_MOUNT_POSIX;
363                                 if (val)
364                                         data->flags |= NFS_MOUNT_POSIX;
365                         } else if (!strcmp(opt, "cto")) {
366                                 data->flags &= ~NFS_MOUNT_NOCTO;
367                                 if (!val)
368                                         data->flags |= NFS_MOUNT_NOCTO;
369                         } else if (!strcmp(opt, "ac")) {
370                                 data->flags &= ~NFS_MOUNT_NOAC;
371                                 if (!val)
372                                         data->flags |= NFS_MOUNT_NOAC;
373 #if NFS_MOUNT_VERSION >= 2
374                         } else if (!strcmp(opt, "tcp")) {
375                                 data->flags &= ~NFS_MOUNT_TCP;
376                                 if (val) {
377                                         if (nfs_mount_data_version < 2)
378                                                 goto bad_option;
379                                         nfs_pmap->pm_prot = IPPROTO_TCP;
380                                         mnt_pmap->pm_prot = IPPROTO_TCP;
381                                         data->flags |= NFS_MOUNT_TCP;
382                                 } else {
383                                         mnt_pmap->pm_prot = IPPROTO_UDP;
384                                         nfs_pmap->pm_prot = IPPROTO_UDP;
385                                 }
386                         } else if (!strcmp(opt, "udp")) {
387                                 data->flags &= ~NFS_MOUNT_TCP;
388                                 if (!val) {
389                                         if (nfs_mount_data_version < 2)
390                                                 goto bad_option;
391                                         nfs_pmap->pm_prot = IPPROTO_TCP;
392                                         mnt_pmap->pm_prot = IPPROTO_TCP;
393                                         data->flags |= NFS_MOUNT_TCP;
394                                 } else {
395                                         nfs_pmap->pm_prot = IPPROTO_UDP;
396                                         mnt_pmap->pm_prot = IPPROTO_UDP;
397                                 }
398 #endif
399 #if NFS_MOUNT_VERSION >= 3
400                         } else if (!strcmp(opt, "lock")) {
401                                 data->flags &= ~NFS_MOUNT_NONLM;
402                                 if (!val) {
403                                         if (nfs_mount_data_version < 3)
404                                                 goto bad_option;
405                                         data->flags |= NFS_MOUNT_NONLM;
406                                 }
407 #endif
408 #if NFS_MOUNT_VERSION >= 4
409                         } else if (!strcmp(opt, "broken_suid")) {
410                                 data->flags &= ~NFS_MOUNT_BROKEN_SUID;
411                                 if (val) {
412                                         if (nfs_mount_data_version < 4)
413                                                 goto bad_option;
414                                         data->flags |= NFS_MOUNT_BROKEN_SUID;
415                                 }
416                         } else if (!strcmp(opt, "acl")) {
417                                 data->flags &= ~NFS_MOUNT_NOACL;
418                                 if (!val)
419                                         data->flags |= NFS_MOUNT_NOACL;
420                         } else if (!strcmp(opt, "rdirplus")) {
421                                 data->flags &= ~NFS_MOUNT_NORDIRPLUS;
422                                 if (!val)
423                                         data->flags |= NFS_MOUNT_NORDIRPLUS;
424                         } else if (!strcmp(opt, "sharecache")) {
425                                 data->flags &= ~NFS_MOUNT_UNSHARED;
426                                 if (!val)
427                                         data->flags |= NFS_MOUNT_UNSHARED;
428 #endif
429                         } else {
430                         bad_option:
431                                 if (sloppy)
432                                         continue;
433                                 nfs_error(_("%s: Unsupported nfs mount option:"
434                                                 " %s%s"), progname,
435                                                 val ? "" : "no", opt);
436                                 goto out_bad;
437                         }
438                         sprintf(cbuf, val ? "%s," : "no%s,", opt);
439                 }
440                 len += strlen(cbuf);
441                 if (len >= opt_size) {
442                         nfs_error(_("%s: excessively long option argument"),
443                                         progname);
444                         goto out_bad;
445                 }
446                 strcat(new_opts, cbuf);
447         }
448         /* See if the nfs host = mount host. */
449         if (mounthost) {
450                 if (!nfs_gethostbyname(mounthost, mnt_saddr))
451                         goto out_bad;
452                 *mnt_server->hostname = mounthost;
453         }
454         return 1;
455  bad_parameter:
456         nfs_error(_("%s: Bad nfs mount parameter: %s\n"), progname, opt);
457  out_bad:
458         return 0;
459 }
460
461 static int nfsmnt_check_compat(const struct pmap *nfs_pmap,
462                                 const struct pmap *mnt_pmap)
463 {
464         unsigned int max_nfs_vers = (nfs_mount_data_version >= 4) ? 3 : 2;
465         unsigned int max_mnt_vers = (nfs_mount_data_version >= 4) ? 3 : 2;
466
467         if (nfs_pmap->pm_vers == 4) {
468                 nfs_error(_("%s: Please use '-t nfs4' "
469                                 "instead of '-o vers=4'"), progname);
470                 goto out_bad;
471         }
472
473         if (nfs_pmap->pm_vers) {
474                 if (nfs_pmap->pm_vers > max_nfs_vers || nfs_pmap->pm_vers < 2) {
475                         nfs_error(_("%s: NFS version %ld is not supported"),
476                                         progname, nfs_pmap->pm_vers);
477                         goto out_bad;
478                 }
479         }
480
481         if (mnt_pmap->pm_vers > max_mnt_vers) {
482                 nfs_error(_("%s: NFS mount version %ld is not supported"),
483                                 progname, mnt_pmap->pm_vers);
484                 goto out_bad;
485         }
486
487         return 1;
488
489 out_bad:
490         return 0;
491 }
492
493 int
494 nfsmount(const char *spec, const char *node, int flags,
495          char **extra_opts, int fake, int running_bg)
496 {
497         static char *prev_bg_host;
498         char hostdir[1024];
499         char *hostname, *dirname, *old_opts, *mounthost = NULL;
500         char new_opts[1024], cbuf[1024];
501         static struct nfs_mount_data data;
502         int val;
503         static int doonce = 0;
504
505         clnt_addr_t mnt_server = { &mounthost, };
506         clnt_addr_t nfs_server = { &hostname, };
507         struct sockaddr_in *nfs_saddr = &nfs_server.saddr;
508         struct pmap  *mnt_pmap = &mnt_server.pmap,
509                      *nfs_pmap = &nfs_server.pmap;
510         struct pmap  save_mnt, save_nfs;
511
512         int fsock = -1;
513
514         mntres_t mntres;
515
516         struct stat statbuf;
517         char *s;
518         int bg, retry;
519         int retval = EX_FAIL;
520         time_t t;
521         time_t prevt;
522         time_t timeout;
523
524         if (strlen(spec) >= sizeof(hostdir)) {
525                 nfs_error(_("%s: excessively long host:dir argument"),
526                                 progname);
527                 goto fail;
528         }
529         strcpy(hostdir, spec);
530         if ((s = strchr(hostdir, ':'))) {
531                 hostname = hostdir;
532                 dirname = s + 1;
533                 *s = '\0';
534                 /* Ignore all but first hostname in replicated mounts
535                    until they can be fully supported. (mack@sgi.com) */
536                 if ((s = strchr(hostdir, ','))) {
537                         *s = '\0';
538                         nfs_error(_("%s: warning: "
539                                   "multiple hostnames not supported"),
540                                         progname);
541                 }
542         } else {
543                 nfs_error(_("%s: directory to mount not in host:dir format"),
544                                 progname);
545                 goto fail;
546         }
547
548         if (!nfs_gethostbyname(hostname, nfs_saddr))
549                 goto fail;
550         mounthost = hostname;
551         memcpy (&mnt_server.saddr, nfs_saddr, sizeof (mnt_server.saddr));
552
553         /* add IP address to mtab options for use when unmounting */
554
555         s = inet_ntoa(nfs_saddr->sin_addr);
556         old_opts = *extra_opts;
557         if (!old_opts)
558                 old_opts = "";
559
560         /* Set default options.
561          * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
562          * let the kernel decide.
563          * timeo is filled in after we know whether it'll be TCP or UDP. */
564         memset(&data, 0, sizeof(data));
565         data.acregmin   = 3;
566         data.acregmax   = 60;
567         data.acdirmin   = 30;
568         data.acdirmax   = 60;
569 #if NFS_MOUNT_VERSION >= 2
570         data.namlen     = NAME_MAX;
571 #endif
572
573         bg = 0;
574         retry = 10000;          /* 10000 minutes ~ 1 week */
575
576         memset(mnt_pmap, 0, sizeof(*mnt_pmap));
577         mnt_pmap->pm_prog = MOUNTPROG;
578         memset(nfs_pmap, 0, sizeof(*nfs_pmap));
579         nfs_pmap->pm_prog = NFS_PROGRAM;
580
581         /* parse options */
582         new_opts[0] = 0;
583         if (!parse_options(old_opts, &data, &bg, &retry, &mnt_server, &nfs_server,
584                            new_opts, sizeof(new_opts)))
585                 goto fail;
586         if (!nfsmnt_check_compat(nfs_pmap, mnt_pmap))
587                 goto fail;
588         
589         if (retry == 10000 && !bg)
590                 retry = 2; /* reset for fg mounts */
591
592 #ifdef NFS_MOUNT_DEBUG
593         printf(_("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n"),
594                data.rsize, data.wsize, data.timeo, data.retrans);
595         printf(_("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n"),
596                data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
597         printf(_("port = %lu, bg = %d, retry = %d, flags = %.8x\n"),
598                nfs_pmap->pm_port, bg, retry, data.flags);
599         printf(_("mountprog = %lu, mountvers = %lu, nfsprog = %lu, nfsvers = %lu\n"),
600                mnt_pmap->pm_prog, mnt_pmap->pm_vers,
601                nfs_pmap->pm_prog, nfs_pmap->pm_vers);
602         printf(_("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d"),
603                (data.flags & NFS_MOUNT_SOFT) != 0,
604                (data.flags & NFS_MOUNT_INTR) != 0,
605                (data.flags & NFS_MOUNT_POSIX) != 0,
606                (data.flags & NFS_MOUNT_NOCTO) != 0,
607                (data.flags & NFS_MOUNT_NOAC) != 0);
608 #if NFS_MOUNT_VERSION >= 2
609         printf(_(", tcp = %d"),
610                (data.flags & NFS_MOUNT_TCP) != 0);
611 #endif
612 #if NFS_MOUNT_VERSION >= 4
613         printf(_(", noacl = %d"), (data.flags & NFS_MOUNT_NOACL) != 0);
614 #endif
615 #if NFS_MOUNT_VERSION >= 5
616         printf(_(", sec = %u"), data.pseudoflavor);
617         printf(_(", readdirplus = %d"), (data.flags & NFS_MOUNT_NORDIRPLUS) != 0);
618 #endif
619         printf("\n");
620 #endif
621
622         data.version = nfs_mount_data_version;
623
624         if (flags & MS_REMOUNT)
625                 goto out_ok;
626
627         /*
628          * If the previous mount operation on the same host was
629          * backgrounded, and the "bg" for this mount is also set,
630          * give up immediately, to avoid the initial timeout.
631          */
632         if (bg && !running_bg &&
633             prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
634                 if (retry > 0)
635                         retval = EX_BG;
636                 return retval;
637         }
638
639         /* create mount deamon client */
640
641         /*
642          * The following loop implements the mount retries. On the first
643          * call, "running_bg" is 0. When the mount times out, and the
644          * "bg" option is set, the exit status EX_BG will be returned.
645          * For a backgrounded mount, there will be a second call by the
646          * child process with "running_bg" set to 1.
647          *
648          * The case where the mount point is not present and the "bg"
649          * option is set, is treated as a timeout. This is done to
650          * support nested mounts.
651          *
652          * The "retry" count specified by the user is the number of
653          * minutes to retry before giving up.
654          *
655          * Only the first error message will be displayed.
656          */
657         timeout = time(NULL) + 60 * retry;
658         prevt = 0;
659         t = 30;
660         val = 1;
661
662         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
663         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
664         for (;;) {
665                 if (bg && stat(node, &statbuf) == -1) {
666                         /* no mount point yet - sleep */
667                         if (running_bg) {
668                                 sleep(val);     /* 1, 2, 4, 8, 16, 30, ... */
669                                 val *= 2;
670                                 if (val > 30)
671                                         val = 30;
672                         }
673                 } else {
674                         int stat;
675                         /* be careful not to use too many CPU cycles */
676                         if (t - prevt < 30)
677                                 sleep(30);
678
679                         stat = nfs_call_mount(&mnt_server, &nfs_server,
680                                               &dirname, &mntres);
681                         if (stat)
682                                 break;
683                         memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
684                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
685                         prevt = t;
686                 }
687                 if (!bg) {
688                         switch(rpc_createerr.cf_stat){
689                         case RPC_TIMEDOUT:
690                                 break;
691                         case RPC_SYSTEMERROR:
692                                 if (errno == ETIMEDOUT)
693                                         break;
694                         default:
695                                 rpc_mount_errors(*nfs_server.hostname, 0, bg);
696                         goto fail;
697                         }
698                         t = time(NULL);
699                         if (t >= timeout) {
700                                 rpc_mount_errors(*nfs_server.hostname, 0, bg);
701                                 goto fail;
702                         }
703                         rpc_mount_errors(*nfs_server.hostname, 1, bg);
704                         continue;
705                 }
706                 if (!running_bg) {
707                         prev_bg_host = xstrdup(hostname);
708                         if (retry > 0)
709                                 retval = EX_BG;
710                         goto fail;
711                 }
712                 t = time(NULL);
713                 if (t >= timeout) {
714                         rpc_mount_errors(*nfs_server.hostname, 0, bg);
715                         goto fail;
716                 }
717                 if (doonce++ < 1)
718                         rpc_mount_errors(*nfs_server.hostname, 1, bg);
719         }
720
721         if (mnt_pmap->pm_vers <= 2) {
722                 if (mntres.nfsv2.fhs_status != 0) {
723                         nfs_error(_("%s: %s:%s failed, reason given by server: %s"),
724                                         progname, hostname, dirname,
725                                         nfs_strerror(mntres.nfsv2.fhs_status));
726                         goto fail;
727                 }
728                 memcpy(data.root.data,
729                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
730                        NFS_FHSIZE);
731 #if NFS_MOUNT_VERSION >= 4
732                 data.root.size = NFS_FHSIZE;
733                 memcpy(data.old_root.data,
734                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
735                        NFS_FHSIZE);
736 #endif
737         } else {
738 #if NFS_MOUNT_VERSION >= 4
739                 mountres3_ok *mountres;
740                 fhandle3 *fhandle;
741                 int i,  n_flavors, *flavor, yum = 0;
742                 if (mntres.nfsv3.fhs_status != 0) {
743                         nfs_error(_("%s: %s:%s failed, reason given by server: %s"),
744                                         progname, hostname, dirname,
745                                         nfs_strerror(mntres.nfsv3.fhs_status));
746                         goto fail;
747                 }
748 #if NFS_MOUNT_VERSION >= 5
749                 mountres = &mntres.nfsv3.mountres3_u.mountinfo;
750                 n_flavors = mountres->auth_flavors.auth_flavors_len;
751                 if (n_flavors <= 0)
752                         goto noauth_flavors;
753
754                 flavor = mountres->auth_flavors.auth_flavors_val;
755                 for (i = 0; i < n_flavors; ++i) {
756                         /*
757                          * Per RFC2623, section 2.7, we should prefer the
758                          * flavour listed first.
759                          * If no flavour requested, use the first simple
760                          * flavour that is offered.
761                          */
762                         if (! (data.flags & NFS_MOUNT_SECFLAVOUR) &&
763                             (flavor[i] == AUTH_SYS ||
764                              flavor[i] == AUTH_NONE)) {
765                                 data.pseudoflavor = flavor[i];
766                                 data.flags |= NFS_MOUNT_SECFLAVOUR;
767                         }
768                         if (flavor[i] == data.pseudoflavor)
769                                 yum = 1;
770 #ifdef NFS_MOUNT_DEBUG
771                         printf(_("auth flavor %d: %d\n"), i, flavor[i]);
772 #endif
773                 }
774                 if (!yum) {
775                         nfs_error(_("%s: %s:%s failed, security flavor "
776                                         "not supported"),
777                                         progname, hostname, dirname);
778                         /* server has registered us in rmtab, send umount */
779                         nfs_call_umount(&mnt_server, &dirname);
780                         goto fail;
781                 }
782 noauth_flavors:
783 #endif
784                 fhandle = &mntres.nfsv3.mountres3_u.mountinfo.fhandle;
785                 memset(data.old_root.data, 0, NFS_FHSIZE);
786                 memset(&data.root, 0, sizeof(data.root));
787                 data.root.size = fhandle->fhandle3_len;
788                 memcpy(data.root.data,
789                        (char *) fhandle->fhandle3_val,
790                        fhandle->fhandle3_len);
791
792                 data.flags |= NFS_MOUNT_VER3;
793 #endif
794         }
795
796         if (nfs_mount_data_version == 1) {
797                 /* create nfs socket for kernel */
798                 if (nfs_pmap->pm_prot == IPPROTO_TCP)
799                         fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
800                 else
801                         fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
802                 if (fsock < 0) {
803                         perror(_("nfs socket"));
804                         goto fail;
805                 }
806                 if (bindresvport(fsock, 0) < 0) {
807                         perror(_("nfs bindresvport"));
808                         goto fail;
809                 }
810         }
811
812 #ifdef NFS_MOUNT_DEBUG
813         printf(_("using port %lu for nfs deamon\n"), nfs_pmap->pm_port);
814 #endif
815         nfs_saddr->sin_port = htons(nfs_pmap->pm_port);
816         /*
817          * connect() the socket for kernels 1.3.10 and below only,
818          * to avoid problems with multihomed hosts.
819          * --Swen
820          */
821         if (linux_version_code() <= 0x01030a && fsock != -1
822             && connect(fsock, (struct sockaddr *) nfs_saddr,
823                        sizeof (*nfs_saddr)) < 0) {
824                 perror(_("nfs connect"));
825                 goto fail;
826         }
827
828 #if NFS_MOUNT_VERSION >= 2
829         if (nfs_pmap->pm_prot == IPPROTO_TCP)
830                 data.flags |= NFS_MOUNT_TCP;
831         else
832                 data.flags &= ~NFS_MOUNT_TCP;
833 #endif
834
835         /* prepare data structure for kernel */
836
837         data.fd = fsock;
838         memcpy((char *) &data.addr, (char *) nfs_saddr, sizeof(data.addr));
839         strncpy(data.hostname, hostname, sizeof(data.hostname));
840
841  out_ok:
842         /* Ensure we have enough padding for the following strcat()s */
843         if (strlen(new_opts) + strlen(s) + 30 >= sizeof(new_opts)) {
844                 nfs_error(_("%s: excessively long option argument"),
845                                 progname);
846                 goto fail;
847         }
848
849         snprintf(cbuf, sizeof(cbuf)-1, "addr=%s", s);
850         strcat(new_opts, cbuf);
851
852         *extra_opts = xstrdup(new_opts);
853
854         if (!fake && !(data.flags & NFS_MOUNT_NONLM)) {
855                 if (!start_statd()) {
856                         nfs_error(_("%s: rpc.statd is not running but is "
857                                 "required for remote locking.\n"
858                                 "   Either use '-o nolock' to keep "
859                                 "locks local, or start statd."),
860                                         progname);
861                         goto fail;
862                 }
863         }
864
865         if (!fake) {
866                 if (mount(spec, node, "nfs",
867                                 flags & ~(MS_USER|MS_USERS), &data)) {
868                         mount_error(spec, node, errno);
869                         goto fail;
870                 }
871         }
872
873         return EX_SUCCESS;
874
875         /* abort */
876  fail:
877         if (fsock != -1)
878                 close(fsock);
879         return retval;
880 }