]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsmount.c
umount.nfs: eliminate a nearly empty header file.
[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 "nfs_mount.h"
63 #include "mount_constants.h"
64 #include "nls.h"
65 #include "error.h"
66 #include "network.h"
67
68 #ifndef NFS_PORT
69 #define NFS_PORT 2049
70 #endif
71 #ifndef NFS_FHSIZE
72 #define NFS_FHSIZE 32
73 #endif
74
75 #ifndef HAVE_INET_ATON
76 #define inet_aton(a,b) (0)
77 #endif
78
79 typedef dirpath mnt2arg_t;
80 typedef dirpath mnt3arg_t;
81 typedef dirpath mntarg_t;
82
83 typedef struct fhstatus  mnt2res_t;
84 typedef struct mountres3 mnt3res_t;
85 typedef union {
86         mnt2res_t nfsv2;
87         mnt3res_t nfsv3;
88 } mntres_t;
89
90 extern int nfs_mount_data_version;
91 extern char *progname;
92 extern int verbose;
93 extern int sloppy;
94
95 extern int linux_version_code(void);
96
97 static inline enum clnt_stat
98 nfs3_mount(CLIENT *clnt, mnt3arg_t *mnt3arg, mnt3res_t *mnt3res)
99 {
100         return clnt_call(clnt, MOUNTPROC3_MNT,
101                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt3arg,
102                          (xdrproc_t) xdr_mountres3, (caddr_t) mnt3res,
103                          TIMEOUT);
104 }
105
106 static inline enum clnt_stat
107 nfs2_mount(CLIENT *clnt, mnt2arg_t *mnt2arg, mnt2res_t *mnt2res)
108 {
109         return clnt_call(clnt, MOUNTPROC_MNT,
110                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt2arg,
111                          (xdrproc_t) xdr_fhstatus, (caddr_t) mnt2res,
112                          TIMEOUT);
113 }
114
115 static int
116 nfs_call_mount(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server,
117                mntarg_t *mntarg, mntres_t *mntres)
118 {
119         CLIENT *clnt;
120         enum clnt_stat stat;
121         int msock;
122
123         if (!probe_bothports(mnt_server, nfs_server))
124                 goto out_bad;
125
126         clnt = mnt_openclnt(mnt_server, &msock);
127         if (!clnt)
128                 goto out_bad;
129         /* make pointers in xdr_mountres3 NULL so
130          * that xdr_array allocates memory for us
131          */
132         memset(mntres, 0, sizeof(*mntres));
133         switch (mnt_server->pmap.pm_vers) {
134         case 3:
135                 stat = nfs3_mount(clnt, mntarg, &mntres->nfsv3);
136                 break;
137         case 2:
138         case 1:
139                 stat = nfs2_mount(clnt, mntarg, &mntres->nfsv2);
140                 break;
141         default:
142                 goto out_bad;
143         }
144         if (stat != RPC_SUCCESS) {
145                 clnt_geterr(clnt, &rpc_createerr.cf_error);
146                 rpc_createerr.cf_stat = stat;
147         }
148         mnt_closeclnt(clnt, msock);
149         if (stat == RPC_SUCCESS)
150                 return 1;
151  out_bad:
152         return 0;
153 }
154
155 static int
156 parse_options(char *old_opts, struct nfs_mount_data *data,
157               int *bg, int *retry, clnt_addr_t *mnt_server,
158               clnt_addr_t *nfs_server, char *new_opts, const int opt_size)
159 {
160         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
161         struct pmap *mnt_pmap = &mnt_server->pmap;
162         struct pmap *nfs_pmap = &nfs_server->pmap;
163         int len;
164         char *opt, *opteq, *p, *opt_b;
165         char *mounthost = NULL;
166         char cbuf[128];
167         int open_quote = 0;
168
169         data->flags = 0;
170         *bg = 0;
171
172         len = strlen(new_opts);
173         for (p=old_opts, opt_b=NULL; p && *p; p++) {
174                 if (!opt_b)
175                         opt_b = p;              /* begin of the option item */
176                 if (*p == '"')
177                         open_quote ^= 1;        /* reverse the status */
178                 if (open_quote)
179                         continue;               /* still in a quoted block */
180                 if (*p == ',')
181                         *p = '\0';              /* terminate the option item */
182                 if (*p == '\0' || *(p+1) == '\0') {
183                         opt = opt_b;            /* opt is useful now */
184                         opt_b = NULL;
185                 }
186                 else
187                         continue;               /* still somewhere in the option item */
188
189                 if (strlen(opt) >= sizeof(cbuf))
190                         goto bad_parameter;
191                 if ((opteq = strchr(opt, '=')) && isdigit(opteq[1])) {
192                         int val = atoi(opteq + 1);      
193                         *opteq = '\0';
194 /* printf("opt=%s\n", opt); */
195                         if (!strcmp(opt, "rsize"))
196                                 data->rsize = val;
197                         else if (!strcmp(opt, "wsize"))
198                                 data->wsize = val;
199                         else if (!strcmp(opt, "timeo"))
200                                 data->timeo = val;
201                         else if (!strcmp(opt, "retrans"))
202                                 data->retrans = val;
203                         else if (!strcmp(opt, "acregmin"))
204                                 data->acregmin = val;
205                         else if (!strcmp(opt, "acregmax"))
206                                 data->acregmax = val;
207                         else if (!strcmp(opt, "acdirmin"))
208                                 data->acdirmin = val;
209                         else if (!strcmp(opt, "acdirmax"))
210                                 data->acdirmax = val;
211                         else if (!strcmp(opt, "actimeo")) {
212                                 data->acregmin = val;
213                                 data->acregmax = val;
214                                 data->acdirmin = val;
215                                 data->acdirmax = val;
216                         }
217                         else if (!strcmp(opt, "retry"))
218                                 *retry = val;
219                         else if (!strcmp(opt, "port"))
220                                 nfs_pmap->pm_port = val;
221                         else if (!strcmp(opt, "mountport"))
222                                 mnt_pmap->pm_port = val;
223                         else if (!strcmp(opt, "mountprog"))
224                                 mnt_pmap->pm_prog = val;
225                         else if (!strcmp(opt, "mountvers"))
226                                 mnt_pmap->pm_vers = val;
227                         else if (!strcmp(opt, "mounthost"))
228                                 mounthost=xstrndup(opteq+1, strcspn(opteq+1," \t\n\r,"));
229                         else if (!strcmp(opt, "nfsprog"))
230                                 nfs_pmap->pm_prog = val;
231                         else if (!strcmp(opt, "nfsvers") ||
232                                  !strcmp(opt, "vers")) {
233                                 nfs_pmap->pm_vers = val;
234                                 opt = "nfsvers";
235 #if NFS_MOUNT_VERSION >= 2
236                         } else if (!strcmp(opt, "namlen")) {
237                                 if (nfs_mount_data_version >= 2)
238                                         data->namlen = val;
239                                 else if (sloppy)
240                                         continue;
241                                 else
242                                         goto bad_parameter;
243 #endif
244                         } else if (!strcmp(opt, "addr")) {
245                                 /* ignore */;
246                                 continue;
247                         } else if (sloppy)
248                                 continue;
249                         else
250                                 goto bad_parameter;
251                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
252                 } else if (opteq) {
253                         *opteq = '\0';
254                         if (!strcmp(opt, "proto")) {
255                                 if (!strcmp(opteq+1, "udp")) {
256                                         nfs_pmap->pm_prot = IPPROTO_UDP;
257                                         mnt_pmap->pm_prot = IPPROTO_UDP;
258 #if NFS_MOUNT_VERSION >= 2
259                                         data->flags &= ~NFS_MOUNT_TCP;
260                                 } else if (!strcmp(opteq+1, "tcp") &&
261                                            nfs_mount_data_version > 2) {
262                                         nfs_pmap->pm_prot = IPPROTO_TCP;
263                                         mnt_pmap->pm_prot = IPPROTO_TCP;
264                                         data->flags |= NFS_MOUNT_TCP;
265 #endif
266                                 } else if (sloppy)
267                                         continue;
268                                 else
269                                         goto bad_parameter;
270 #if NFS_MOUNT_VERSION >= 5
271                         } else if (!strcmp(opt, "sec")) {
272                                 char *secflavor = opteq+1;
273                                 /* see RFC 2623 */
274                                 if (nfs_mount_data_version < 5) {
275                                         printf(_("Warning: ignoring sec=%s option\n"),
276                                                         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                                         nfs_error(_("context parameter exceeds"
318                                                         " limit of %d"),
319                                                         NFS_MAX_CONTEXT_LEN);
320                                         goto bad_parameter;
321                                 }
322                                 /* The context string is in the format of
323                                  * "system_u:object_r:...".  We only want
324                                  * the context str between the quotes.
325                                  */
326                                 if (*context == '"')
327                                         strncpy(data->context, context+1,
328                                                         ctxlen-2);
329                                 else
330                                         strncpy(data->context, context,
331                                                         NFS_MAX_CONTEXT_LEN);
332                         } else if (sloppy)
333                                 continue;
334                         else
335                                 goto bad_parameter;
336                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
337                 } else {
338                         int val = 1;
339                         if (!strncmp(opt, "no", 2)) {
340                                 val = 0;
341                                 opt += 2;
342                         }
343                         if (!strcmp(opt, "bg"))
344                                 *bg = val;
345                         else if (!strcmp(opt, "fg"))
346                                 *bg = !val;
347                         else if (!strcmp(opt, "soft")) {
348                                 data->flags &= ~NFS_MOUNT_SOFT;
349                                 if (val)
350                                         data->flags |= NFS_MOUNT_SOFT;
351                         } else if (!strcmp(opt, "hard")) {
352                                 data->flags &= ~NFS_MOUNT_SOFT;
353                                 if (!val)
354                                         data->flags |= NFS_MOUNT_SOFT;
355                         } else if (!strcmp(opt, "intr")) {
356                                 data->flags &= ~NFS_MOUNT_INTR;
357                                 if (val)
358                                         data->flags |= NFS_MOUNT_INTR;
359                         } else if (!strcmp(opt, "posix")) {
360                                 data->flags &= ~NFS_MOUNT_POSIX;
361                                 if (val)
362                                         data->flags |= NFS_MOUNT_POSIX;
363                         } else if (!strcmp(opt, "cto")) {
364                                 data->flags &= ~NFS_MOUNT_NOCTO;
365                                 if (!val)
366                                         data->flags |= NFS_MOUNT_NOCTO;
367                         } else if (!strcmp(opt, "ac")) {
368                                 data->flags &= ~NFS_MOUNT_NOAC;
369                                 if (!val)
370                                         data->flags |= NFS_MOUNT_NOAC;
371 #if NFS_MOUNT_VERSION >= 2
372                         } else if (!strcmp(opt, "tcp")) {
373                                 data->flags &= ~NFS_MOUNT_TCP;
374                                 if (val) {
375                                         if (nfs_mount_data_version < 2)
376                                                 goto bad_option;
377                                         nfs_pmap->pm_prot = IPPROTO_TCP;
378                                         mnt_pmap->pm_prot = IPPROTO_TCP;
379                                         data->flags |= NFS_MOUNT_TCP;
380                                 } else {
381                                         mnt_pmap->pm_prot = IPPROTO_UDP;
382                                         nfs_pmap->pm_prot = IPPROTO_UDP;
383                                 }
384                         } else if (!strcmp(opt, "udp")) {
385                                 data->flags &= ~NFS_MOUNT_TCP;
386                                 if (!val) {
387                                         if (nfs_mount_data_version < 2)
388                                                 goto bad_option;
389                                         nfs_pmap->pm_prot = IPPROTO_TCP;
390                                         mnt_pmap->pm_prot = IPPROTO_TCP;
391                                         data->flags |= NFS_MOUNT_TCP;
392                                 } else {
393                                         nfs_pmap->pm_prot = IPPROTO_UDP;
394                                         mnt_pmap->pm_prot = IPPROTO_UDP;
395                                 }
396 #endif
397 #if NFS_MOUNT_VERSION >= 3
398                         } else if (!strcmp(opt, "lock")) {
399                                 data->flags &= ~NFS_MOUNT_NONLM;
400                                 if (!val) {
401                                         if (nfs_mount_data_version < 3)
402                                                 goto bad_option;
403                                         data->flags |= NFS_MOUNT_NONLM;
404                                 }
405 #endif
406 #if NFS_MOUNT_VERSION >= 4
407                         } else if (!strcmp(opt, "broken_suid")) {
408                                 data->flags &= ~NFS_MOUNT_BROKEN_SUID;
409                                 if (val) {
410                                         if (nfs_mount_data_version < 4)
411                                                 goto bad_option;
412                                         data->flags |= NFS_MOUNT_BROKEN_SUID;
413                                 }
414                         } else if (!strcmp(opt, "acl")) {
415                                 data->flags &= ~NFS_MOUNT_NOACL;
416                                 if (!val)
417                                         data->flags |= NFS_MOUNT_NOACL;
418                         } else if (!strcmp(opt, "rdirplus")) {
419                                 data->flags &= ~NFS_MOUNT_NORDIRPLUS;
420                                 if (!val)
421                                         data->flags |= NFS_MOUNT_NORDIRPLUS;
422                         } else if (!strcmp(opt, "sharecache")) {
423                                 data->flags &= ~NFS_MOUNT_UNSHARED;
424                                 if (!val)
425                                         data->flags |= NFS_MOUNT_UNSHARED;
426 #endif
427                         } else {
428                         bad_option:
429                                 if (sloppy)
430                                         continue;
431                                 nfs_error(_("%s: Unsupported nfs mount option:"
432                                                 " %s%s"), progname,
433                                                 val ? "" : "no", opt);
434                                 goto out_bad;
435                         }
436                         sprintf(cbuf, val ? "%s,":"no%s,", opt);
437                 }
438                 len += strlen(cbuf);
439                 if (len >= opt_size) {
440                         nfs_error(_("%s: excessively long option argument"),
441                                         progname);
442                         goto out_bad;
443                 }
444                 strcat(new_opts, cbuf);
445         }
446         /* See if the nfs host = mount host. */
447         if (mounthost) {
448                 if (!nfs_gethostbyname(mounthost, mnt_saddr))
449                         goto out_bad;
450                 *mnt_server->hostname = mounthost;
451         }
452         return 1;
453  bad_parameter:
454         nfs_error(_("%s: Bad nfs mount parameter: %s\n"), progname, opt);
455  out_bad:
456         return 0;
457 }
458
459 static int nfsmnt_check_compat(const struct pmap *nfs_pmap,
460                                 const struct pmap *mnt_pmap)
461 {
462         unsigned int max_nfs_vers = (nfs_mount_data_version >= 4) ? 3 : 2;
463         unsigned int max_mnt_vers = (nfs_mount_data_version >= 4) ? 3 : 2;
464
465         if (nfs_pmap->pm_vers == 4) {
466                 nfs_error(_("%s: Please use '-t nfs4' "
467                                 "instead of '-o vers=4'"), progname);
468                 goto out_bad;
469         }
470
471         if (nfs_pmap->pm_vers) {
472                 if (nfs_pmap->pm_vers > max_nfs_vers || nfs_pmap->pm_vers < 2) {
473                         nfs_error(_("%s: NFS version %ld is not supported"),
474                                         progname, nfs_pmap->pm_vers);
475                         goto out_bad;
476                 }
477         }
478
479         if (mnt_pmap->pm_vers > max_mnt_vers) {
480                 nfs_error(_("%s: NFS mount version %ld s not supported"),
481                                 progname, mnt_pmap->pm_vers);
482                 goto out_bad;
483         }
484
485         return 1;
486
487 out_bad:
488         return 0;
489 }
490
491 int
492 nfsmount(const char *spec, const char *node, int flags,
493          char **extra_opts, int fake)
494 {
495         static char *prev_bg_host;
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, running_bg = 0;
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 = 10000;          /* 10000 minutes ~ 1 week */
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 == 10000 && !bg)
588                 retry = 2; /* reset for fg mounts */
589         
590
591 #ifdef NFS_MOUNT_DEBUG
592         printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
593                data.rsize, data.wsize, data.timeo, data.retrans);
594         printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
595                data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
596         printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
597                nfs_pmap->pm_port, bg, retry, data.flags);
598         printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
599                mnt_pmap->pm_prog, mnt_pmap->pm_vers,
600                nfs_pmap->pm_prog, nfs_pmap->pm_vers);
601         printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d ",
602                (data.flags & NFS_MOUNT_SOFT) != 0,
603                (data.flags & NFS_MOUNT_INTR) != 0,
604                (data.flags & NFS_MOUNT_POSIX) != 0,
605                (data.flags & NFS_MOUNT_NOCTO) != 0,
606                (data.flags & NFS_MOUNT_NOAC) != 0);
607 #if NFS_MOUNT_VERSION >= 2
608         printf("tcp = %d ",
609                (data.flags & NFS_MOUNT_TCP) != 0);
610 #endif
611 #if NFS_MOUNT_VERSION >= 4
612         printf("noacl = %d ", (data.flags & NFS_MOUNT_NOACL) != 0);
613 #endif
614 #if NFS_MOUNT_VERSION >= 5
615         printf("sec = %u ", data.pseudoflavor);
616         printf("readdirplus = %d ", (data.flags & NFS_MOUNT_NORDIRPLUS) != 0);
617 #endif
618         printf("\n");
619 #endif
620
621         data.version = nfs_mount_data_version;
622
623         if (flags & MS_REMOUNT)
624                 goto out_ok;
625
626         /*
627          * If the previous mount operation on the same host was
628          * backgrounded, and the "bg" for this mount is also set,
629          * give up immediately, to avoid the initial timeout.
630          */
631         if (bg && !running_bg &&
632             prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
633                 if (retry > 0)
634                         retval = EX_BG;
635                 return retval;
636         }
637
638         /* create mount deamon client */
639
640         /*
641          * The following loop implements the mount retries. On the first
642          * call, "running_bg" is 0. When the mount times out, and the
643          * "bg" option is set, the exit status EX_BG will be returned.
644          * For a backgrounded mount, there will be a second call by the
645          * child process with "running_bg" set to 1.
646          *
647          * The case where the mount point is not present and the "bg"
648          * option is set, is treated as a timeout. This is done to
649          * support nested mounts.
650          *
651          * The "retry" count specified by the user is the number of
652          * minutes to retry before giving up.
653          *
654          * Only the first error message will be displayed.
655          */
656         timeout = time(NULL) + 60 * retry;
657         prevt = 0;
658         t = 30;
659         val = 1;
660
661         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
662         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
663         for (;;) {
664                 if (bg && stat(node, &statbuf) == -1) {
665                         /* no mount point yet - sleep */
666                         if (running_bg) {
667                                 sleep(val);     /* 1, 2, 4, 8, 16, 30, ... */
668                                 val *= 2;
669                                 if (val > 30)
670                                         val = 30;
671                         }
672                 } else {
673                         int stat;
674                         /* be careful not to use too many CPU cycles */
675                         if (t - prevt < 30)
676                                 sleep(30);
677
678                         stat = nfs_call_mount(&mnt_server, &nfs_server,
679                                               &dirname, &mntres);
680                         if (stat)
681                                 break;
682                         memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
683                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
684                         prevt = t;
685                 }
686                 if (!bg) {
687                         switch(rpc_createerr.cf_stat){
688                         case RPC_TIMEDOUT:
689                                 break;
690                         case RPC_SYSTEMERROR:
691                                 if (errno == ETIMEDOUT)
692                                         break;
693                         default:
694                                 mount_errors(*nfs_server.hostname, 0, bg);
695                         goto fail;
696                         }
697                         t = time(NULL);
698                         if (t >= timeout) {
699                                 mount_errors(*nfs_server.hostname, 0, bg);
700                                 goto fail;
701                         }
702                         mount_errors(*nfs_server.hostname, 1, bg);
703                         continue;
704                 }
705                 if (!running_bg) {
706                         prev_bg_host = xstrdup(hostname);
707                         if (retry > 0)
708                                 retval = EX_BG;
709                         goto fail;
710                 }
711                 t = time(NULL);
712                 if (t >= timeout) {
713                         mount_errors(*nfs_server.hostname, 0, bg);
714                         goto fail;
715                 }
716                 if (doonce++ < 1)
717                         mount_errors(*nfs_server.hostname, 1, bg);
718         }
719
720         if (nfs_pmap->pm_vers == 2) {
721                 if (mntres.nfsv2.fhs_status != 0) {
722                         nfs_error(_("%s: %s:%s failed, reason given by server: %s"),
723                                         progname, hostname, dirname,
724                                         nfs_strerror(mntres.nfsv2.fhs_status));
725                         goto fail;
726                 }
727                 memcpy(data.root.data,
728                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
729                        NFS_FHSIZE);
730 #if NFS_MOUNT_VERSION >= 4
731                 data.root.size = NFS_FHSIZE;
732                 memcpy(data.old_root.data,
733                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
734                        NFS_FHSIZE);
735 #endif
736         } else {
737 #if NFS_MOUNT_VERSION >= 4
738                 mountres3_ok *mountres;
739                 fhandle3 *fhandle;
740                 int i, *flavor, yum = 0;
741                 if (mntres.nfsv3.fhs_status != 0) {
742                         nfs_error(_("%s: %s:%s failed, reason given by server: %s"),
743                                         progname, 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                         nfs_error(_("%s: %s:%s failed, security flavor "
773                                         "not supported"),
774                                         progname, hostname, dirname);
775                         /* server has registered us in rmtab, send umount */
776                         nfs_call_umount(&mnt_server, &dirname);
777                         goto fail;
778                 }
779 noauth_flavors:
780 #endif
781                 fhandle = &mntres.nfsv3.mountres3_u.mountinfo.fhandle;
782                 memset(data.old_root.data, 0, NFS_FHSIZE);
783                 memset(&data.root, 0, sizeof(data.root));
784                 data.root.size = fhandle->fhandle3_len;
785                 memcpy(data.root.data,
786                        (char *) fhandle->fhandle3_val,
787                        fhandle->fhandle3_len);
788
789                 data.flags |= NFS_MOUNT_VER3;
790 #endif
791         }
792
793         if (nfs_mount_data_version == 1) {
794                 /* create nfs socket for kernel */
795                 if (nfs_pmap->pm_prot == IPPROTO_TCP)
796                         fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
797                 else
798                         fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
799                 if (fsock < 0) {
800                         perror(_("nfs socket"));
801                         goto fail;
802                 }
803                 if (bindresvport(fsock, 0) < 0) {
804                         perror(_("nfs bindresvport"));
805                         goto fail;
806                 }
807         }
808
809 #ifdef NFS_MOUNT_DEBUG
810         printf(_("using port %d for nfs deamon\n"), nfs_pmap->pm_port);
811 #endif
812         nfs_saddr->sin_port = htons(nfs_pmap->pm_port);
813         /*
814          * connect() the socket for kernels 1.3.10 and below only,
815          * to avoid problems with multihomed hosts.
816          * --Swen
817          */
818         if (linux_version_code() <= 0x01030a && fsock != -1
819             && connect(fsock, (struct sockaddr *) nfs_saddr,
820                        sizeof (*nfs_saddr)) < 0) {
821                 perror(_("nfs connect"));
822                 goto fail;
823         }
824
825 #if NFS_MOUNT_VERSION >= 2
826         if (nfs_pmap->pm_prot == IPPROTO_TCP)
827                 data.flags |= NFS_MOUNT_TCP;
828         else
829                 data.flags &= ~NFS_MOUNT_TCP;
830 #endif
831
832         /* prepare data structure for kernel */
833
834         data.fd = fsock;
835         memcpy((char *) &data.addr, (char *) nfs_saddr, sizeof(data.addr));
836         strncpy(data.hostname, hostname, sizeof(data.hostname));
837
838  out_ok:
839         /* Ensure we have enough padding for the following strcat()s */
840         if (strlen(new_opts) + strlen(s) + 30 >= sizeof(new_opts)) {
841                 nfs_error(_("%s: excessively long option argument"),
842                                 progname);
843                 goto fail;
844         }
845
846         snprintf(cbuf, sizeof(cbuf)-1, "addr=%s", s);
847         strcat(new_opts, cbuf);
848
849         *extra_opts = xstrdup(new_opts);
850
851         if (!fake && !(data.flags & NFS_MOUNT_NONLM)) {
852                 if (!start_statd()) {
853                         nfs_error(_("%s: rpc.statd is not running but is "
854                                 "required for remote locking.\n"
855                                 "   Either use '-o nolocks' to keep "
856                                 "locks local, or start statd."),
857                                         progname);
858                         goto fail;
859                 }
860         }
861
862         if (!fake) {
863                 if (mount(spec, node, "nfs",
864                                 flags & ~(MS_USER|MS_USERS), &data)) {
865                         mount_error(spec, node, errno);
866                         goto fail;
867                 }
868         }
869
870         return 0;
871
872         /* abort */
873  fail:
874         if (fsock != -1)
875                 close(fsock);
876         return retval;
877 }