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