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