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