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