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