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