mount.nfs: Always preset nfs_mount_version
[nfs-utils.git] / utils / mount / nfsmount.c
1 /*
2  * nfsmount.c -- Linux NFS mount
3  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * Wed Feb  8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
16  * numbers to be specified on the command line.
17  *
18  * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
19  * Omit the call to connect() for Linux version 1.3.11 or later.
20  *
21  * Wed Oct  1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
22  * Implemented the "bg", "fg" and "retry" mount options for NFS.
23  *
24  * 1999-02-22 Arkadiusz Mi¬∂kiewicz <misiek@pld.ORG.PL>
25  * - added Native Language Support
26  * 
27  * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
28  * plus NFSv3 stuff.
29  *
30  * 2006-06-06 Amit Gud <agud@redhat.com>
31  * - Moved with modifcations to nfs-utils/utils/mount from util-linux/mount.
32  */
33
34 /*
35  * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
36  */
37
38 #include <ctype.h>
39 #include <unistd.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <errno.h>
44 #include <netdb.h>
45 #include <time.h>
46 #include <rpc/rpc.h>
47 #include <rpc/pmap_prot.h>
48 #include <rpc/pmap_clnt.h>
49 #include <sys/socket.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <mntent.h>
55 #include <sys/mount.h>
56 #include <paths.h>
57 #include <syslog.h>
58
59 #include "conn.h"
60 #include "xcommon.h"
61 #include "mount.h"
62 #include "nfsumount.h"
63 #include "nfs_mount.h"
64 #include "mount_constants.h"
65 #include "nls.h"
66 #include "error.h"
67
68 #ifndef NFS_PORT
69 #define NFS_PORT 2049
70 #endif
71 #ifndef NFS_FHSIZE
72 #define NFS_FHSIZE 32
73 #endif
74
75 #define MAX_NFSPROT ((nfs_mount_data_version >= 4) ? 3 : 2)
76 #define MAX_MNTPROT ((nfs_mount_data_version >= 4) ? 3 : 2)
77 #define HAVE_RELIABLE_TCP (nfs_mount_data_version >= 4)
78
79 #ifndef HAVE_INET_ATON
80 #define inet_aton(a,b) (0)
81 #endif
82
83 typedef dirpath mnt2arg_t;
84 typedef dirpath mnt3arg_t;
85 typedef dirpath mntarg_t;
86
87 typedef struct fhstatus  mnt2res_t;
88 typedef struct mountres3 mnt3res_t;
89 typedef union {
90         mnt2res_t nfsv2;
91         mnt3res_t nfsv3;
92 } mntres_t;
93
94 extern int nfs_mount_data_version;
95 extern int verbose;
96 extern int sloppy;
97
98 extern int linux_version_code();
99
100 /* Define the order in which to probe for UDP/TCP services */
101 enum plist {
102         use_tcp = 0,
103         udp_tcp,
104         udp_only,
105 };
106 static const u_int *
107 proto_probelist(enum plist list)
108 {
109         static const u_int probe_udp_tcp[] = { IPPROTO_UDP, IPPROTO_TCP, 0 };
110         static const u_int probe_both[] = { IPPROTO_TCP, IPPROTO_UDP, 0 };
111         static const u_int probe_udponly[] = { IPPROTO_UDP, 0 };
112
113         if (list == use_tcp)
114                 return probe_both;
115         if (list == udp_tcp)
116                 return probe_udp_tcp;
117         return probe_udponly;
118 }
119
120 /* Define the order in which NFS versions are probed on portmapper */
121 static const u_long *
122 nfs_probelist(const int vers)
123 {
124         static const u_long nfs2_probe[] = { 2, 0};
125         static const u_long nfs3_probe[] = { 3, 2, 0};
126         switch (vers) {
127         case 3:
128                 return nfs3_probe;
129         default:
130                 return nfs2_probe;
131         }
132 }
133
134 /* Define the order in which Mountd versions are probed on portmapper */
135 static const u_long *
136 mnt_probelist(const int vers)
137 {
138         static const u_long mnt1_probe[] = { 1, 2, 0 };
139         static const u_long mnt3_probe[] = { 3, 1, 2, 0 };
140         switch (vers) {
141         case 3:
142                 return mnt3_probe;
143         default:
144                 return mnt1_probe;
145         }
146 }
147
148 int nfs_gethostbyname(const char *, struct sockaddr_in *);
149 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
150 {
151         struct hostent *hp;
152
153         saddr->sin_family = AF_INET;
154         if (!inet_aton(hostname, &saddr->sin_addr)) {
155                 if ((hp = gethostbyname(hostname)) == NULL) {
156                         fprintf(stderr, _("mount: can't get address for %s\n"),
157                                 hostname);
158                         return 0;
159                 } else {
160                         if (hp->h_length > sizeof(*saddr)) {
161                                 fprintf(stderr,
162                                         _("mount: got bad hp->h_length\n"));
163                                 hp->h_length = sizeof(*saddr);
164                         }
165                         memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
166                 }
167         }
168         return 1;
169 }
170
171 /*
172  * getport() is very similar to pmap_getport() with
173  * the exception this version uses a non-reserve ports 
174  * instead of reserve ports since reserve ports
175  * are not needed for pmap requests.
176  */
177 u_short
178 getport(
179         struct sockaddr_in *saddr, 
180         u_long prog, 
181         u_long vers, 
182         u_int prot)
183 {
184         u_short port = 0;
185         int    socket;
186         CLIENT *clnt = NULL;
187         struct pmap parms;
188         enum clnt_stat stat;
189
190         saddr->sin_port = htons (PMAPPORT);
191         socket = get_socket(saddr, prot, FALSE, FALSE);
192
193         switch (prot) {
194         case IPPROTO_UDP:
195                 clnt = clntudp_bufcreate(saddr,
196                                          PMAPPROG, PMAPVERS, TIMEOUT, &socket,
197                                          UDPMSGSIZE, UDPMSGSIZE);
198                 break;
199         case IPPROTO_TCP:
200                 clnt = clnttcp_create(saddr,
201                         PMAPPROG, PMAPVERS, &socket, 50, 500);
202                 break;
203         }
204         if (clnt != NULL) {
205                 parms.pm_prog = prog;
206                 parms.pm_vers = vers;
207                 parms.pm_prot = prot;
208                 parms.pm_port = 0;    /* not needed or used */
209
210                 stat = clnt_call(clnt, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
211                         (caddr_t)&parms, (xdrproc_t)xdr_u_short, (caddr_t)&port, TIMEOUT);
212                 if (stat) {
213                         clnt_geterr(clnt, &rpc_createerr.cf_error);
214                         rpc_createerr.cf_stat = stat;
215                 }
216                 clnt_destroy(clnt);
217                 if (stat != RPC_SUCCESS)
218                         port = 0;
219                 else if (port == 0)
220                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
221         }
222         if (socket != 1)
223                 close(socket);
224
225         return port;
226 }
227
228 /*
229  * Use the portmapper to discover whether or not the service we want is
230  * available. The lists 'versions' and 'protos' define ordered sequences
231  * of service versions and udp/tcp protocols to probe for.
232  */
233 static int
234 probe_port(clnt_addr_t *server, 
235            const u_long *versions,
236            const u_int *protos)
237 {
238         struct sockaddr_in *saddr = &server->saddr;
239         struct pmap *pmap = &server->pmap;
240         const u_long prog = pmap->pm_prog, *p_vers;
241         const u_int prot = (u_int)pmap->pm_prot,
242                 *p_prot;
243         const u_short port = (u_short) pmap->pm_port;
244         u_long vers = pmap->pm_vers;
245         u_short p_port;
246         p_prot = prot ? &prot : protos;
247         p_vers = vers ? &vers : versions;
248         rpc_createerr.cf_stat = 0;
249         for (;;) {
250                 saddr->sin_port = htons(PMAPPORT);
251                 p_port = getport(saddr, prog, *p_vers, *p_prot);
252                 if (p_port) {
253                         if (!port || port == p_port) {
254                                 saddr->sin_port = htons(p_port);
255                                 if (verbose) {
256                                         fprintf(stderr, 
257                                                 "mount: trying %s prog %ld vers %ld prot %s port %d\n", 
258                                                 inet_ntoa(saddr->sin_addr), prog, *p_vers,
259                                                 *p_prot == IPPROTO_UDP ? "udp" : "tcp", p_port);
260                                 }
261                                 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
262                                         goto out_ok;
263                                 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
264                                         goto out_bad;
265                         }
266                 }
267                 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED) 
268                         goto out_bad;
269
270                 if (!prot) {
271                         if (*++p_prot)
272                                 continue;
273                         p_prot = protos;
274                 }
275                 if (vers == pmap->pm_vers) {
276                         p_vers = versions;
277                         vers = 0;
278                 }
279                 if (vers || !*++p_vers)
280                         break;
281         }
282 out_bad:
283         return 0;
284
285  out_ok:
286         if (!vers)
287                 pmap->pm_vers = *p_vers;
288         if (!prot)
289                 pmap->pm_prot = *p_prot;
290         if (!port)
291                 pmap->pm_port = p_port;
292         rpc_createerr.cf_stat = 0;
293         return 1;
294 }
295
296 static int
297 probe_nfsport(clnt_addr_t *nfs_server)
298 {
299         const struct pmap *pmap = &nfs_server->pmap;
300         const u_long *probe_vers;
301         const u_int *probe_prot;
302
303         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
304                 return 1;
305         probe_vers = nfs_probelist(MAX_NFSPROT);
306         probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? use_tcp : udp_only);
307         return probe_port(nfs_server, probe_vers, probe_prot);
308 }
309
310 int probe_mntport(clnt_addr_t *mnt_server)
311 {
312         const struct pmap *pmap = &mnt_server->pmap;
313         const u_long *probe_vers;
314         const u_int *probe_prot;
315
316         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
317                 return 1;
318         probe_vers = mnt_probelist(MAX_MNTPROT);
319         probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? udp_tcp : udp_only);
320         return probe_port(mnt_server, probe_vers, probe_prot);
321 }
322
323 static int
324 probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
325 {
326         struct pmap *nfs_pmap = &nfs_server->pmap;
327         struct pmap *mnt_pmap = &mnt_server->pmap;
328         struct pmap save_nfs, save_mnt;
329         int res;
330         const u_long *probe_vers;
331
332         if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
333                 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
334         else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
335                 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
336         if (nfs_pmap->pm_vers)
337                 goto version_fixed;
338         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
339         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
340         for (probe_vers = mnt_probelist(MAX_MNTPROT); *probe_vers; probe_vers++) {
341                 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
342                 if ((res = probe_nfsport(nfs_server) != 0)) {
343                         mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
344                         if ((res = probe_mntport(mnt_server)) != 0)
345                                 return 1;
346                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
347                 }
348                 switch (rpc_createerr.cf_stat) {
349                 case RPC_PROGVERSMISMATCH:
350                 case RPC_PROGNOTREGISTERED:
351                         break;
352                 default:
353                         goto out_bad;
354                 }
355                 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
356         }
357  out_bad:
358         return 0;
359  version_fixed:
360         if (!probe_nfsport(nfs_server))
361                 goto out_bad;
362         return probe_mntport(mnt_server);
363 }
364
365 static inline enum clnt_stat
366 nfs3_mount(CLIENT *clnt, mnt3arg_t *mnt3arg, mnt3res_t *mnt3res)
367 {
368         return clnt_call(clnt, MOUNTPROC3_MNT,
369                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt3arg,
370                          (xdrproc_t) xdr_mountres3, (caddr_t) mnt3res,
371                          TIMEOUT);
372 }
373
374 static inline enum clnt_stat
375 nfs2_mount(CLIENT *clnt, mnt2arg_t *mnt2arg, mnt2res_t *mnt2res)
376 {
377         return clnt_call(clnt, MOUNTPROC_MNT,
378                          (xdrproc_t) xdr_dirpath, (caddr_t) mnt2arg,
379                          (xdrproc_t) xdr_fhstatus, (caddr_t) mnt2res,
380                          TIMEOUT);
381 }
382
383 static int
384 nfs_call_mount(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server,
385                mntarg_t *mntarg, mntres_t *mntres)
386 {
387         CLIENT *clnt;
388         enum clnt_stat stat;
389         int msock;
390
391         if (!probe_bothports(mnt_server, nfs_server))
392                 goto out_bad;
393
394         clnt = mnt_openclnt(mnt_server, &msock);
395         if (!clnt)
396                 goto out_bad;
397         /* make pointers in xdr_mountres3 NULL so
398          * that xdr_array allocates memory for us
399          */
400         memset(mntres, 0, sizeof(*mntres));
401         switch (mnt_server->pmap.pm_vers) {
402         case 3:
403                 stat = nfs3_mount(clnt, mntarg, &mntres->nfsv3);
404                 break;
405         case 2:
406         case 1:
407                 stat = nfs2_mount(clnt, mntarg, &mntres->nfsv2);
408                 break;
409         default:
410                 goto out_bad;
411         }
412         if (stat != RPC_SUCCESS) {
413                 clnt_geterr(clnt, &rpc_createerr.cf_error);
414                 rpc_createerr.cf_stat = stat;
415         }
416         mnt_closeclnt(clnt, msock);
417         if (stat == RPC_SUCCESS)
418                 return 1;
419  out_bad:
420         return 0;
421 }
422
423 static int
424 parse_options(char *old_opts, struct nfs_mount_data *data,
425               int *bg, int *retry, clnt_addr_t *mnt_server,
426               clnt_addr_t *nfs_server, char *new_opts, const int opt_size)
427 {
428         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
429         struct pmap *mnt_pmap = &mnt_server->pmap;
430         struct pmap *nfs_pmap = &nfs_server->pmap;
431         int len;
432         char *opt, *opteq, *p, *opt_b;
433         char *mounthost = NULL;
434         char cbuf[128];
435         int open_quote = 0;
436
437         data->flags = 0;
438         *bg = 0;
439
440         len = strlen(new_opts);
441         for (p=old_opts, opt_b=NULL; p && *p; p++) {
442                 if (!opt_b)
443                         opt_b = p;              /* begin of the option item */
444                 if (*p == '"')
445                         open_quote ^= 1;        /* reverse the status */
446                 if (open_quote)
447                         continue;               /* still in a quoted block */
448                 if (*p == ',')
449                         *p = '\0';              /* terminate the option item */
450                 if (*p == '\0' || *(p+1) == '\0') {
451                         opt = opt_b;            /* opt is useful now */
452                         opt_b = NULL;
453                 }
454                 else
455                         continue;               /* still somewhere in the option item */
456
457                 if (strlen(opt) >= sizeof(cbuf))
458                         goto bad_parameter;
459                 if ((opteq = strchr(opt, '=')) && isdigit(opteq[1])) {
460                         int val = atoi(opteq + 1);      
461                         *opteq = '\0';
462 /* printf("opt=%s\n", opt); */
463                         if (!strcmp(opt, "rsize"))
464                                 data->rsize = val;
465                         else if (!strcmp(opt, "wsize"))
466                                 data->wsize = val;
467                         else if (!strcmp(opt, "timeo"))
468                                 data->timeo = val;
469                         else if (!strcmp(opt, "retrans"))
470                                 data->retrans = val;
471                         else if (!strcmp(opt, "acregmin"))
472                                 data->acregmin = val;
473                         else if (!strcmp(opt, "acregmax"))
474                                 data->acregmax = val;
475                         else if (!strcmp(opt, "acdirmin"))
476                                 data->acdirmin = val;
477                         else if (!strcmp(opt, "acdirmax"))
478                                 data->acdirmax = val;
479                         else if (!strcmp(opt, "actimeo")) {
480                                 data->acregmin = val;
481                                 data->acregmax = val;
482                                 data->acdirmin = val;
483                                 data->acdirmax = val;
484                         }
485                         else if (!strcmp(opt, "retry"))
486                                 *retry = val;
487                         else if (!strcmp(opt, "port"))
488                                 nfs_pmap->pm_port = val;
489                         else if (!strcmp(opt, "mountport"))
490                                 mnt_pmap->pm_port = val;
491                         else if (!strcmp(opt, "mountprog"))
492                                 mnt_pmap->pm_prog = val;
493                         else if (!strcmp(opt, "mountvers"))
494                                 mnt_pmap->pm_vers = val;
495                         else if (!strcmp(opt, "mounthost"))
496                                 mounthost=xstrndup(opteq+1, strcspn(opteq+1," \t\n\r,"));
497                         else if (!strcmp(opt, "nfsprog"))
498                                 nfs_pmap->pm_prog = val;
499                         else if (!strcmp(opt, "nfsvers") ||
500                                  !strcmp(opt, "vers")) {
501                                 nfs_pmap->pm_vers = val;
502                                 opt = "nfsvers";
503 #if NFS_MOUNT_VERSION >= 2
504                         } else if (!strcmp(opt, "namlen")) {
505                                 if (nfs_mount_data_version >= 2)
506                                         data->namlen = val;
507                                 else if (sloppy)
508                                         continue;
509                                 else
510                                         goto bad_parameter;
511 #endif
512                         } else if (!strcmp(opt, "addr")) {
513                                 /* ignore */;
514                                 continue;
515                         } else if (sloppy)
516                                 continue;
517                         else
518                                 goto bad_parameter;
519                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
520                 } else if (opteq) {
521                         *opteq = '\0';
522                         if (!strcmp(opt, "proto")) {
523                                 if (!strcmp(opteq+1, "udp")) {
524                                         nfs_pmap->pm_prot = IPPROTO_UDP;
525                                         mnt_pmap->pm_prot = IPPROTO_UDP;
526 #if NFS_MOUNT_VERSION >= 2
527                                         data->flags &= ~NFS_MOUNT_TCP;
528                                 } else if (!strcmp(opteq+1, "tcp") &&
529                                            nfs_mount_data_version > 2) {
530                                         nfs_pmap->pm_prot = IPPROTO_TCP;
531                                         mnt_pmap->pm_prot = IPPROTO_TCP;
532                                         data->flags |= NFS_MOUNT_TCP;
533 #endif
534                                 } else if (sloppy)
535                                         continue;
536                                 else
537                                         goto bad_parameter;
538 #if NFS_MOUNT_VERSION >= 5
539                         } else if (!strcmp(opt, "sec")) {
540                                 char *secflavor = opteq+1;
541                                 /* see RFC 2623 */
542                                 if (nfs_mount_data_version < 5) {
543                                         printf(_("Warning: ignoring sec=%s option\n"), secflavor);
544                                         continue;
545                                 } else if (!strcmp(secflavor, "none"))
546                                         data->pseudoflavor = AUTH_NONE;
547                                 else if (!strcmp(secflavor, "sys"))
548                                         data->pseudoflavor = AUTH_SYS;
549                                 else if (!strcmp(secflavor, "krb5"))
550                                         data->pseudoflavor = AUTH_GSS_KRB5;
551                                 else if (!strcmp(secflavor, "krb5i"))
552                                         data->pseudoflavor = AUTH_GSS_KRB5I;
553                                 else if (!strcmp(secflavor, "krb5p"))
554                                         data->pseudoflavor = AUTH_GSS_KRB5P;
555                                 else if (!strcmp(secflavor, "lipkey"))
556                                         data->pseudoflavor = AUTH_GSS_LKEY;
557                                 else if (!strcmp(secflavor, "lipkey-i"))
558                                         data->pseudoflavor = AUTH_GSS_LKEYI;
559                                 else if (!strcmp(secflavor, "lipkey-p"))
560                                         data->pseudoflavor = AUTH_GSS_LKEYP;
561                                 else if (!strcmp(secflavor, "spkm3"))
562                                         data->pseudoflavor = AUTH_GSS_SPKM;
563                                 else if (!strcmp(secflavor, "spkm3i"))
564                                         data->pseudoflavor = AUTH_GSS_SPKMI;
565                                 else if (!strcmp(secflavor, "spkm3p"))
566                                         data->pseudoflavor = AUTH_GSS_SPKMP;
567                                 else if (sloppy)
568                                         continue;
569                                 else {
570                                         printf(_("Warning: Unrecognized security flavor %s.\n"),
571                                                 secflavor);
572                                         goto bad_parameter;
573                                 }
574                                 data->flags |= NFS_MOUNT_SECFLAVOUR;
575 #endif
576                         } else if (!strcmp(opt, "mounthost"))
577                                 mounthost=xstrndup(opteq+1,
578                                                    strcspn(opteq+1," \t\n\r,"));
579                          else if (!strcmp(opt, "context")) {
580                                 char *context = opteq + 1;
581                                 int ctxlen = strlen(context);
582
583                                 if (ctxlen > NFS_MAX_CONTEXT_LEN) {
584                                         printf(_("context parameter exceeds limit of %d\n"),
585                                                  NFS_MAX_CONTEXT_LEN);
586                                         goto bad_parameter;
587                                 }
588                                 /* The context string is in the format of
589                                  * "system_u:object_r:...".  We only want
590                                  * the context str between the quotes.
591                                  */
592                                 if (*context == '"')
593                                         strncpy(data->context, context+1,
594                                                         ctxlen-2);
595                                 else
596                                         strncpy(data->context, context,
597                                                         NFS_MAX_CONTEXT_LEN);
598                         } else if (sloppy)
599                                 continue;
600                         else
601                                 goto bad_parameter;
602                         sprintf(cbuf, "%s=%s,", opt, opteq+1);
603                 } else {
604                         int val = 1;
605                         if (!strncmp(opt, "no", 2)) {
606                                 val = 0;
607                                 opt += 2;
608                         }
609                         if (!strcmp(opt, "bg")) 
610                                 *bg = val;
611                         else if (!strcmp(opt, "fg")) 
612                                 *bg = !val;
613                         else if (!strcmp(opt, "soft")) {
614                                 data->flags &= ~NFS_MOUNT_SOFT;
615                                 if (val)
616                                         data->flags |= NFS_MOUNT_SOFT;
617                         } else if (!strcmp(opt, "hard")) {
618                                 data->flags &= ~NFS_MOUNT_SOFT;
619                                 if (!val)
620                                         data->flags |= NFS_MOUNT_SOFT;
621                         } else if (!strcmp(opt, "intr")) {
622                                 data->flags &= ~NFS_MOUNT_INTR;
623                                 if (val)
624                                         data->flags |= NFS_MOUNT_INTR;
625                         } else if (!strcmp(opt, "posix")) {
626                                 data->flags &= ~NFS_MOUNT_POSIX;
627                                 if (val)
628                                         data->flags |= NFS_MOUNT_POSIX;
629                         } else if (!strcmp(opt, "cto")) {
630                                 data->flags &= ~NFS_MOUNT_NOCTO;
631                                 if (!val)
632                                         data->flags |= NFS_MOUNT_NOCTO;
633                         } else if (!strcmp(opt, "ac")) {
634                                 data->flags &= ~NFS_MOUNT_NOAC;
635                                 if (!val)
636                                         data->flags |= NFS_MOUNT_NOAC;
637 #if NFS_MOUNT_VERSION >= 2
638                         } else if (!strcmp(opt, "tcp")) {
639                                 data->flags &= ~NFS_MOUNT_TCP;
640                                 if (val) {
641                                         if (nfs_mount_data_version < 2)
642                                                 goto bad_option;
643                                         nfs_pmap->pm_prot = IPPROTO_TCP;
644                                         mnt_pmap->pm_prot = IPPROTO_TCP;
645                                         data->flags |= NFS_MOUNT_TCP;
646                                 } else {
647                                         mnt_pmap->pm_prot = IPPROTO_UDP;
648                                         nfs_pmap->pm_prot = IPPROTO_UDP;
649                                 }
650                         } else if (!strcmp(opt, "udp")) {
651                                 data->flags &= ~NFS_MOUNT_TCP;
652                                 if (!val) {
653                                         if (nfs_mount_data_version < 2)
654                                                 goto bad_option;
655                                         nfs_pmap->pm_prot = IPPROTO_TCP;
656                                         mnt_pmap->pm_prot = IPPROTO_TCP;
657                                         data->flags |= NFS_MOUNT_TCP;
658                                 } else {
659                                         nfs_pmap->pm_prot = IPPROTO_UDP;
660                                         mnt_pmap->pm_prot = IPPROTO_UDP;
661                                 }
662 #endif
663 #if NFS_MOUNT_VERSION >= 3
664                         } else if (!strcmp(opt, "lock")) {
665                                 data->flags &= ~NFS_MOUNT_NONLM;
666                                 if (!val) {
667                                         if (nfs_mount_data_version < 3)
668                                                 goto bad_option;
669                                         data->flags |= NFS_MOUNT_NONLM;
670                                 }
671 #endif
672 #if NFS_MOUNT_VERSION >= 4
673                         } else if (!strcmp(opt, "broken_suid")) {
674                                 data->flags &= ~NFS_MOUNT_BROKEN_SUID;
675                                 if (val) {
676                                         if (nfs_mount_data_version < 4)
677                                                 goto bad_option;
678                                         data->flags |= NFS_MOUNT_BROKEN_SUID;
679                                 }
680                         } else if (!strcmp(opt, "acl")) {
681                                 data->flags &= ~NFS_MOUNT_NOACL;
682                                 if (!val)
683                                         data->flags |= NFS_MOUNT_NOACL;
684                         } else if (!strcmp(opt, "rdirplus")) {
685                                 data->flags &= ~NFS_MOUNT_NORDIRPLUS;
686                                 if (!val)
687                                         data->flags |= NFS_MOUNT_NORDIRPLUS;
688                         } else if (!strcmp(opt, "sharecache")) {
689                                 data->flags &= ~NFS_MOUNT_UNSHARED;
690                                 if (!val)
691                                         data->flags |= NFS_MOUNT_UNSHARED;
692 #endif
693                         } else {
694                         bad_option:
695                                 if (sloppy)
696                                         continue;
697                                 printf(_("Unsupported nfs mount option: "
698                                          "%s%s\n"), val ? "" : "no", opt);
699                                 goto out_bad;
700                         }
701                         sprintf(cbuf, val ? "%s,":"no%s,", opt);
702                 }
703                 len += strlen(cbuf);
704                 if (len >= opt_size) {
705                         printf(_("mount: excessively long option argument\n"));
706                         goto out_bad;
707                 }
708                 strcat(new_opts, cbuf);
709         }
710         /* See if the nfs host = mount host. */
711         if (mounthost) {
712                 if (!nfs_gethostbyname(mounthost, mnt_saddr))
713                         goto out_bad;
714                 *mnt_server->hostname = mounthost;
715         }
716         return 1;
717  bad_parameter:
718         printf(_("Bad nfs mount parameter: %s\n"), opt);
719  out_bad:
720         return 0;
721 }
722
723 static inline int
724 nfsmnt_check_compat(const struct pmap *nfs_pmap, const struct pmap *mnt_pmap)
725 {
726         if (nfs_pmap->pm_vers && 
727                 (nfs_pmap->pm_vers > MAX_NFSPROT || nfs_pmap->pm_vers < 2)) {
728                 if (nfs_pmap->pm_vers == 4)
729                         fprintf(stderr, _("'vers=4' is not supported.  "
730                                 "Use '-t nfs4' instead.\n"));
731                 else
732                         fprintf(stderr, _("NFS version %ld is not supported.\n"), 
733                                 nfs_pmap->pm_vers);
734                 goto out_bad;
735         }
736         if (mnt_pmap->pm_vers > MAX_MNTPROT) {
737                 fprintf(stderr, _("NFS mount version %ld s not supported.\n"), 
738                         mnt_pmap->pm_vers);
739                 goto out_bad;
740         }
741         return 1;
742  out_bad:
743         return 0;
744 }
745
746 int
747 nfsmount(const char *spec, const char *node, int *flags,
748          char **extra_opts, char **mount_opts,
749          int running_bg, int *need_statd)
750 {
751         static char *prev_bg_host;
752         char hostdir[1024];
753         char *hostname, *dirname, *old_opts, *mounthost = NULL;
754         char new_opts[1024], cbuf[1024];
755         static struct nfs_mount_data data;
756         int val;
757         static int doonce = 0;
758
759         clnt_addr_t mnt_server = { &mounthost, };
760         clnt_addr_t nfs_server = { &hostname, };
761         struct sockaddr_in *nfs_saddr = &nfs_server.saddr;
762         struct pmap  *mnt_pmap = &mnt_server.pmap, 
763                      *nfs_pmap = &nfs_server.pmap;
764         struct pmap  save_mnt, save_nfs;
765
766         int fsock = -1;
767
768         mntres_t mntres;
769
770         struct stat statbuf;
771         char *s;
772         int bg, retry;
773         int retval = EX_FAIL;
774         time_t t;
775         time_t prevt;
776         time_t timeout;
777
778         if (strlen(spec) >= sizeof(hostdir)) {
779                 fprintf(stderr, _("mount: "
780                                   "excessively long host:dir argument\n"));
781                 goto fail;
782         }
783         strcpy(hostdir, spec);
784         if ((s = strchr(hostdir, ':'))) {
785                 hostname = hostdir;
786                 dirname = s + 1;
787                 *s = '\0';
788                 /* Ignore all but first hostname in replicated mounts
789                    until they can be fully supported. (mack@sgi.com) */
790                 if ((s = strchr(hostdir, ','))) {
791                         *s = '\0';
792                         fprintf(stderr,
793                                 _("mount: warning: "
794                                   "multiple hostnames not supported\n"));
795                 }
796         } else {
797                 fprintf(stderr,
798                         _("mount: "
799                           "directory to mount not in host:dir format\n"));
800                 goto fail;
801         }
802
803         if (!nfs_gethostbyname(hostname, nfs_saddr))
804                 goto fail;
805         mounthost = hostname;
806         memcpy (&mnt_server.saddr, nfs_saddr, sizeof (mnt_server.saddr));
807
808         /* add IP address to mtab options for use when unmounting */
809
810         s = inet_ntoa(nfs_saddr->sin_addr);
811         old_opts = *extra_opts;
812         if (!old_opts)
813                 old_opts = "";
814
815         /* Set default options.
816          * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
817          * let the kernel decide.
818          * timeo is filled in after we know whether it'll be TCP or UDP. */
819         memset(&data, 0, sizeof(data));
820         data.acregmin   = 3;
821         data.acregmax   = 60;
822         data.acdirmin   = 30;
823         data.acdirmax   = 60;
824 #if NFS_MOUNT_VERSION >= 2
825         data.namlen     = NAME_MAX;
826 #endif
827
828         bg = 0;
829         retry = 10000;          /* 10000 minutes ~ 1 week */
830
831         memset(mnt_pmap, 0, sizeof(*mnt_pmap));
832         mnt_pmap->pm_prog = MOUNTPROG;
833         memset(nfs_pmap, 0, sizeof(*nfs_pmap));
834         nfs_pmap->pm_prog = NFS_PROGRAM;
835
836         /* parse options */
837         new_opts[0] = 0;
838         if (!parse_options(old_opts, &data, &bg, &retry, &mnt_server, &nfs_server,
839                            new_opts, sizeof(new_opts)))
840                 goto fail;
841         if (!nfsmnt_check_compat(nfs_pmap, mnt_pmap))
842                 goto fail;
843         
844         if (retry == 10000 && !bg)
845                 retry = 2; /* reset for fg mounts */
846         
847
848 #ifdef NFS_MOUNT_DEBUG
849         printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
850                data.rsize, data.wsize, data.timeo, data.retrans);
851         printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
852                data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
853         printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
854                nfs_pmap->pm_port, bg, retry, data.flags);
855         printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
856                mnt_pmap->pm_prog, mnt_pmap->pm_vers,
857                nfs_pmap->pm_prog, nfs_pmap->pm_vers);
858         printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d ",
859                (data.flags & NFS_MOUNT_SOFT) != 0,
860                (data.flags & NFS_MOUNT_INTR) != 0,
861                (data.flags & NFS_MOUNT_POSIX) != 0,
862                (data.flags & NFS_MOUNT_NOCTO) != 0,
863                (data.flags & NFS_MOUNT_NOAC) != 0);
864 #if NFS_MOUNT_VERSION >= 2
865         printf("tcp = %d ",
866                (data.flags & NFS_MOUNT_TCP) != 0);
867 #endif
868 #if NFS_MOUNT_VERSION >= 4
869         printf("noacl = %d ", (data.flags & NFS_MOUNT_NOACL) != 0);
870 #endif
871 #if NFS_MOUNT_VERSION >= 5
872         printf("sec = %u ", data.pseudoflavor);
873         printf("readdirplus = %d ", (data.flags & NFS_MOUNT_NORDIRPLUS) != 0);
874 #endif
875         printf("\n");
876 #endif
877
878         data.version = nfs_mount_data_version;
879         *mount_opts = (char *) &data;
880
881         if (*flags & MS_REMOUNT)
882                 goto out_ok;
883
884         /*
885          * If the previous mount operation on the same host was
886          * backgrounded, and the "bg" for this mount is also set,
887          * give up immediately, to avoid the initial timeout.
888          */
889         if (bg && !running_bg &&
890             prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
891                 if (retry > 0)
892                         retval = EX_BG;
893                 return retval;
894         }
895
896         /* create mount deamon client */
897
898         /*
899          * The following loop implements the mount retries. On the first
900          * call, "running_bg" is 0. When the mount times out, and the
901          * "bg" option is set, the exit status EX_BG will be returned.
902          * For a backgrounded mount, there will be a second call by the
903          * child process with "running_bg" set to 1.
904          *
905          * The case where the mount point is not present and the "bg"
906          * option is set, is treated as a timeout. This is done to
907          * support nested mounts.
908          *
909          * The "retry" count specified by the user is the number of
910          * minutes to retry before giving up.
911          *
912          * Only the first error message will be displayed.
913          */
914         timeout = time(NULL) + 60 * retry;
915         prevt = 0;
916         t = 30;
917         val = 1;
918
919         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
920         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
921         for (;;) {
922                 if (bg && stat(node, &statbuf) == -1) {
923                         /* no mount point yet - sleep */
924                         if (running_bg) {
925                                 sleep(val);     /* 1, 2, 4, 8, 16, 30, ... */
926                                 val *= 2;
927                                 if (val > 30)
928                                         val = 30;
929                         }
930                 } else {
931                         int stat;
932                         /* be careful not to use too many CPU cycles */
933                         if (t - prevt < 30)
934                                 sleep(30);
935
936                         stat = nfs_call_mount(&mnt_server, &nfs_server,
937                                               &dirname, &mntres);
938                         if (stat)
939                                 break;
940                         memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
941                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
942                         prevt = t;
943                 }
944                 if (!bg) {
945                         switch(rpc_createerr.cf_stat){
946                         case RPC_TIMEDOUT:
947                                 break;
948                         case RPC_SYSTEMERROR:
949                                 if (errno == ETIMEDOUT)
950                                         break;
951                         default:
952                                 mount_errors(*nfs_server.hostname, 0, bg);
953                         goto fail;
954                         }
955                         t = time(NULL);
956                         if (t >= timeout) {
957                                 mount_errors(*nfs_server.hostname, 0, bg);
958                                 goto fail;
959                         }
960                         mount_errors(*nfs_server.hostname, 1, bg);
961                         continue;
962                 }
963                 if (!running_bg) {
964                         prev_bg_host = xstrdup(hostname);
965                         if (retry > 0)
966                                 retval = EX_BG;
967                         goto fail;
968                 }
969                 t = time(NULL);
970                 if (t >= timeout) {
971                         mount_errors(*nfs_server.hostname, 0, bg);
972                         goto fail;
973                 }
974                 if (doonce++ < 1)
975                         mount_errors(*nfs_server.hostname, 1, bg);
976         }
977
978         if (nfs_pmap->pm_vers == 2) {
979                 if (mntres.nfsv2.fhs_status != 0) {
980                         fprintf(stderr,
981                                 _("mount: %s:%s failed, reason given by server: %s\n"),
982                                 hostname, dirname,
983                                 nfs_strerror(mntres.nfsv2.fhs_status));
984                         goto fail;
985                 }
986                 memcpy(data.root.data,
987                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
988                        NFS_FHSIZE);
989 #if NFS_MOUNT_VERSION >= 4
990                 data.root.size = NFS_FHSIZE;
991                 memcpy(data.old_root.data,
992                        (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
993                        NFS_FHSIZE);
994 #endif
995         } else {
996 #if NFS_MOUNT_VERSION >= 4
997                 mountres3_ok *mountres;
998                 fhandle3 *fhandle;
999                 int i, *flavor, yum = 0;
1000                 if (mntres.nfsv3.fhs_status != 0) {
1001                         fprintf(stderr,
1002                                 _("mount: %s:%s failed, reason given by server: %s\n"),
1003                                 hostname, dirname,
1004                                 nfs_strerror(mntres.nfsv3.fhs_status));
1005                         goto fail;
1006                 }
1007 #if NFS_MOUNT_VERSION >= 5
1008                 mountres = &mntres.nfsv3.mountres3_u.mountinfo;
1009                 i = mountres->auth_flavors.auth_flavors_len;
1010                 if (i <= 0) 
1011                         goto noauth_flavors;
1012
1013                 flavor = mountres->auth_flavors.auth_flavors_val;
1014                 while (--i >= 0) {
1015                         /* If no flavour requested, use first simple
1016                          * flavour that is offered.
1017                          */
1018                         if (! (data.flags & NFS_MOUNT_SECFLAVOUR) &&
1019                             (flavor[i] == AUTH_SYS ||
1020                              flavor[i] == AUTH_NONE)) {
1021                                 data.pseudoflavor = flavor[i];
1022                                 data.flags |= NFS_MOUNT_SECFLAVOUR;
1023                         }
1024                         if (flavor[i] == data.pseudoflavor)
1025                                 yum = 1;
1026 #ifdef NFS_MOUNT_DEBUG
1027                         printf("auth flavor %d: %d\n",
1028                                 i, flavor[i]);
1029 #endif
1030                 }
1031                 if (!yum) {
1032                         fprintf(stderr,
1033                                 "mount: %s:%s failed, "
1034                                 "security flavor not supported\n",
1035                                 hostname, dirname);
1036                         /* server has registered us in rmtab, send umount */
1037                         nfs_call_umount(&mnt_server, &dirname);
1038                         goto fail;
1039                 }
1040 noauth_flavors:
1041 #endif
1042                 fhandle = &mntres.nfsv3.mountres3_u.mountinfo.fhandle;
1043                 memset(data.old_root.data, 0, NFS_FHSIZE);
1044                 memset(&data.root, 0, sizeof(data.root));
1045                 data.root.size = fhandle->fhandle3_len;
1046                 memcpy(data.root.data,
1047                        (char *) fhandle->fhandle3_val,
1048                        fhandle->fhandle3_len);
1049
1050                 data.flags |= NFS_MOUNT_VER3;
1051 #endif
1052         }
1053
1054         if (nfs_mount_data_version == 1) {
1055                 /* create nfs socket for kernel */
1056                 if (nfs_pmap->pm_prot == IPPROTO_TCP)
1057                         fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1058                 else
1059                         fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1060                 if (fsock < 0) {
1061                         perror(_("nfs socket"));
1062                         goto fail;
1063                 }
1064                 if (bindresvport(fsock, 0) < 0) {
1065                         perror(_("nfs bindresvport"));
1066                         goto fail;
1067                 }
1068         }
1069
1070 #ifdef NFS_MOUNT_DEBUG
1071         printf(_("using port %d for nfs deamon\n"), nfs_pmap->pm_port);
1072 #endif
1073         nfs_saddr->sin_port = htons(nfs_pmap->pm_port);
1074         /*
1075          * connect() the socket for kernels 1.3.10 and below only,
1076          * to avoid problems with multihomed hosts.
1077          * --Swen
1078          */
1079         if (linux_version_code() <= 0x01030a && fsock != -1
1080             && connect(fsock, (struct sockaddr *) nfs_saddr,
1081                        sizeof (*nfs_saddr)) < 0) {
1082                 perror(_("nfs connect"));
1083                 goto fail;
1084         }
1085
1086 #if NFS_MOUNT_VERSION >= 2
1087         if (nfs_pmap->pm_prot == IPPROTO_TCP)
1088                 data.flags |= NFS_MOUNT_TCP;
1089         else
1090                 data.flags &= ~NFS_MOUNT_TCP;
1091 #endif
1092
1093         /* prepare data structure for kernel */
1094
1095         data.fd = fsock;
1096         memcpy((char *) &data.addr, (char *) nfs_saddr, sizeof(data.addr));
1097         strncpy(data.hostname, hostname, sizeof(data.hostname));
1098
1099  out_ok:
1100         /* Ensure we have enough padding for the following strcat()s */
1101         if (strlen(new_opts) + strlen(s) + 30 >= sizeof(new_opts)) {
1102                 fprintf(stderr, _("mount: "
1103                                   "excessively long option argument\n"));
1104                 goto fail;
1105         }
1106
1107         snprintf(cbuf, sizeof(cbuf)-1, "addr=%s", s);
1108         strcat(new_opts, cbuf);
1109
1110         *extra_opts = xstrdup(new_opts);
1111         *need_statd = ! (data.flags & NFS_MOUNT_NONLM);
1112         return 0;
1113
1114         /* abort */
1115  fail:
1116         if (fsock != -1)
1117                 close(fsock);
1118         return retval;
1119 }