Update to changelog -- mostly from Larry Doolittle.
[oweals/busybox.git] / nfsmount.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * nfsmount.c -- Linux NFS mount
4  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * Wed Feb  8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
17  * numbers to be specified on the command line.
18  *
19  * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
20  * Omit the call to connect() for Linux version 1.3.11 or later.
21  *
22  * Wed Oct  1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
23  * Implemented the "bg", "fg" and "retry" mount options for NFS.
24  *
25  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
26  * - added Native Language Support
27  * 
28  * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
29  * plus NFSv3 stuff.
30  */
31
32 /*
33  * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
34  */
35
36 #include "busybox.h"
37 #undef FALSE
38 #undef TRUE
39 #include <unistd.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <netdb.h>
44 #include <rpc/rpc.h>
45 #include <rpc/pmap_prot.h>
46 #include <rpc/pmap_clnt.h>
47 #include <sys/socket.h>
48 #include <sys/time.h>
49 #include <sys/utsname.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52
53 #include "nfsmount.h"
54 #include <linux/nfs.h>  /* For the kernels nfs stuff */
55
56 #ifndef NFS_FHSIZE
57 #define NFS_FHSIZE      32
58 #endif
59 #ifndef NFS_PORT
60 #define NFS_PORT        2049
61 #endif
62
63 /* Disable the nls stuff */
64 # undef bindtextdomain
65 # define bindtextdomain(Domain, Directory) /* empty */
66 # undef textdomain
67 # define textdomain(Domain) /* empty */
68 # define _(Text) (Text)
69 # define N_(Text) (Text)
70
71 #define MS_MGC_VAL              0xc0ed0000 /* Magic number indicatng "new" flags */
72 #define MS_RDONLY        1      /* Mount read-only */
73 #define MS_NOSUID        2      /* Ignore suid and sgid bits */
74 #define MS_NODEV         4      /* Disallow access to device special files */
75 #define MS_NOEXEC        8      /* Disallow program execution */
76 #define MS_SYNCHRONOUS  16      /* Writes are synced at once */
77 #define MS_REMOUNT      32      /* Alter flags of a mounted FS */
78 #define MS_MANDLOCK     64      /* Allow mandatory locks on an FS */
79 #define S_QUOTA         128     /* Quota initialized for file/directory/symlink */
80 #define S_APPEND        256     /* Append-only file */
81 #define S_IMMUTABLE     512     /* Immutable file */
82 #define MS_NOATIME      1024    /* Do not update access times. */
83 #define MS_NODIRATIME   2048    /* Do not update directory access times */
84
85
86 /*
87  * We want to be able to compile mount on old kernels in such a way
88  * that the binary will work well on more recent kernels.
89  * Thus, if necessary we teach nfsmount.c the structure of new fields
90  * that will come later.
91  *
92  * Moreover, the new kernel includes conflict with glibc includes
93  * so it is easiest to ignore the kernel altogether (at compile time).
94  */
95
96 #define NFS_MOUNT_VERSION       4
97
98 struct nfs2_fh {
99         char                    data[32];
100 };
101 struct nfs3_fh {
102         unsigned short          size;
103         unsigned char           data[64];
104 };
105
106 struct nfs_mount_data {
107         int             version;                /* 1 */
108         int             fd;                     /* 1 */
109         struct nfs2_fh  old_root;               /* 1 */
110         int             flags;                  /* 1 */
111         int             rsize;                  /* 1 */
112         int             wsize;                  /* 1 */
113         int             timeo;                  /* 1 */
114         int             retrans;                /* 1 */
115         int             acregmin;               /* 1 */
116         int             acregmax;               /* 1 */
117         int             acdirmin;               /* 1 */
118         int             acdirmax;               /* 1 */
119         struct sockaddr_in addr;                /* 1 */
120         char            hostname[256];          /* 1 */
121         int             namlen;                 /* 2 */
122         unsigned int    bsize;                  /* 3 */
123         struct nfs3_fh  root;                   /* 4 */
124 };
125
126 /* bits in the flags field */
127
128 #define NFS_MOUNT_SOFT          0x0001  /* 1 */
129 #define NFS_MOUNT_INTR          0x0002  /* 1 */
130 #define NFS_MOUNT_SECURE        0x0004  /* 1 */
131 #define NFS_MOUNT_POSIX         0x0008  /* 1 */
132 #define NFS_MOUNT_NOCTO         0x0010  /* 1 */
133 #define NFS_MOUNT_NOAC          0x0020  /* 1 */
134 #define NFS_MOUNT_TCP           0x0040  /* 2 */
135 #define NFS_MOUNT_VER3          0x0080  /* 3 */
136 #define NFS_MOUNT_KERBEROS      0x0100  /* 3 */
137 #define NFS_MOUNT_NONLM         0x0200  /* 3 */
138
139
140 #define UTIL_LINUX_VERSION "2.10m"
141 #define util_linux_version "util-linux-2.10m"
142
143 #define HAVE_inet_aton
144 #define HAVE_scsi_h
145 #define HAVE_blkpg_h
146 #define HAVE_kd_h
147 #define HAVE_termcap
148 #define HAVE_locale_h
149 #define HAVE_libintl_h
150 #define ENABLE_NLS
151 #define HAVE_langinfo_h
152 #define HAVE_progname
153 #define HAVE_openpty
154 #define HAVE_nanosleep
155 #define HAVE_personality
156 #define HAVE_tm_gmtoff
157
158 static char *nfs_strerror(int stat);
159
160 #define MAKE_VERSION(p,q,r)     (65536*(p) + 256*(q) + (r))
161 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
162
163 #define EX_FAIL                 32       /* mount failure */
164 #define EX_BG                   256       /* retry in background (internal only) */
165
166
167 /*
168  * nfs_mount_version according to the sources seen at compile time.
169  */
170 int nfs_mount_version = NFS_MOUNT_VERSION;
171
172 /*
173  * Unfortunately, the kernel prints annoying console messages
174  * in case of an unexpected nfs mount version (instead of
175  * just returning some error).  Therefore we'll have to try
176  * and figure out what version the kernel expects.
177  *
178  * Variables:
179  *      KERNEL_NFS_MOUNT_VERSION: kernel sources at compile time
180  *      NFS_MOUNT_VERSION: these nfsmount sources at compile time
181  *      nfs_mount_version: version this source and running kernel can handle
182  */
183 static void
184 find_kernel_nfs_mount_version(void) {
185         static int kernel_version = 0;
186
187         if (kernel_version)
188                 return;
189
190         kernel_version = get_kernel_revision();
191
192         if (kernel_version) {
193              if (kernel_version < MAKE_VERSION(2,1,32))
194                   nfs_mount_version = 1;
195              else if (kernel_version < MAKE_VERSION(2,3,99))
196                   nfs_mount_version = 3;
197              else
198                   nfs_mount_version = 4; /* since 2.3.99pre4 */
199         }
200         if (nfs_mount_version > NFS_MOUNT_VERSION)
201              nfs_mount_version = NFS_MOUNT_VERSION;
202 }
203
204 static struct pmap *
205 get_mountport(struct sockaddr_in *server_addr,
206       long unsigned prog,
207       long unsigned version,
208       long unsigned proto,
209       long unsigned port)
210 {
211 struct pmaplist *pmap;
212 static struct pmap p = {0, 0, 0, 0};
213
214 server_addr->sin_port = PMAPPORT;
215 pmap = pmap_getmaps(server_addr);
216
217 if (version > MAX_NFSPROT)
218         version = MAX_NFSPROT;
219 if (!prog)
220         prog = MOUNTPROG;
221 p.pm_prog = prog;
222 p.pm_vers = version;
223 p.pm_prot = proto;
224 p.pm_port = port;
225
226 while (pmap) {
227         if (pmap->pml_map.pm_prog != prog)
228                 goto next;
229         if (!version && p.pm_vers > pmap->pml_map.pm_vers)
230                 goto next;
231         if (version > 2 && pmap->pml_map.pm_vers != version)
232                 goto next;
233         if (version && version <= 2 && pmap->pml_map.pm_vers > 2)
234                 goto next;
235         if (pmap->pml_map.pm_vers > MAX_NFSPROT ||
236             (proto && p.pm_prot && pmap->pml_map.pm_prot != proto) ||
237             (port && pmap->pml_map.pm_port != port))
238                 goto next;
239         memcpy(&p, &pmap->pml_map, sizeof(p));
240 next:
241         pmap = pmap->pml_next;
242 }
243 if (!p.pm_vers)
244         p.pm_vers = MOUNTVERS;
245 if (!p.pm_port)
246         p.pm_port = MOUNTPORT;
247 if (!p.pm_prot)
248         p.pm_prot = IPPROTO_TCP;
249 return &p;
250 }
251
252 int nfsmount(const char *spec, const char *node, int *flags,
253              char **extra_opts, char **mount_opts, int running_bg)
254 {
255         static char *prev_bg_host;
256         char hostdir[1024];
257         CLIENT *mclient;
258         char *hostname;
259         char *dirname;
260         char *old_opts;
261         char *mounthost=NULL;
262         char new_opts[1024];
263         struct timeval total_timeout;
264         enum clnt_stat clnt_stat;
265         static struct nfs_mount_data data;
266         char *opt, *opteq;
267         int val;
268         struct hostent *hp;
269         struct sockaddr_in server_addr;
270         struct sockaddr_in mount_server_addr;
271         struct pmap* pm_mnt;
272         int msock, fsock;
273         struct timeval retry_timeout;
274         union {
275                 struct fhstatus nfsv2;
276                 struct mountres3 nfsv3;
277         } status;
278         struct stat statbuf;
279         char *s;
280         int port;
281         int mountport;
282         int proto;
283         int bg;
284         int soft;
285         int intr;
286         int posix;
287         int nocto;
288         int noac;
289         int nolock;
290         int retry;
291         int tcp;
292         int mountprog;
293         int mountvers;
294         int nfsprog;
295         int nfsvers;
296         int retval;
297         time_t t;
298         time_t prevt;
299         time_t timeout;
300
301         find_kernel_nfs_mount_version();
302
303         retval = EX_FAIL;
304         msock = fsock = -1;
305         mclient = NULL;
306         if (strlen(spec) >= sizeof(hostdir)) {
307                 error_msg("excessively long host:dir argument\n");
308                 goto fail;
309         }
310         strcpy(hostdir, spec);
311         if ((s = strchr(hostdir, ':'))) {
312                 hostname = hostdir;
313                 dirname = s + 1;
314                 *s = '\0';
315                 /* Ignore all but first hostname in replicated mounts
316                    until they can be fully supported. (mack@sgi.com) */
317                 if ((s = strchr(hostdir, ','))) {
318                         *s = '\0';
319                         error_msg("warning: multiple hostnames not supported\n");
320                 }
321         } else {
322                 error_msg("directory to mount not in host:dir format\n");
323                 goto fail;
324         }
325
326         server_addr.sin_family = AF_INET;
327 #ifdef HAVE_inet_aton
328         if (!inet_aton(hostname, &server_addr.sin_addr))
329 #endif
330         {
331                 if ((hp = gethostbyname(hostname)) == NULL) {
332                         error_msg("can't get address for %s\n", hostname);
333                         goto fail;
334                 } else {
335                         if (hp->h_length > sizeof(struct in_addr)) {
336                                 error_msg("got bad hp->h_length\n");
337                                 hp->h_length = sizeof(struct in_addr);
338                         }
339                         memcpy(&server_addr.sin_addr,
340                                hp->h_addr, hp->h_length);
341                 }
342         }
343
344         memcpy (&mount_server_addr, &server_addr, sizeof (mount_server_addr));
345
346         /* add IP address to mtab options for use when unmounting */
347
348         s = inet_ntoa(server_addr.sin_addr);
349         old_opts = *extra_opts;
350         if (!old_opts)
351                 old_opts = "";
352         if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
353                 error_msg("excessively long option argument\n");
354                 goto fail;
355         }
356         sprintf(new_opts, "%s%saddr=%s",
357                 old_opts, *old_opts ? "," : "", s);
358         *extra_opts = xstrdup(new_opts);
359
360         /* Set default options.
361          * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
362          * let the kernel decide.
363          * timeo is filled in after we know whether it'll be TCP or UDP. */
364         memset(&data, 0, sizeof(data));
365         data.retrans    = 3;
366         data.acregmin   = 3;
367         data.acregmax   = 60;
368         data.acdirmin   = 30;
369         data.acdirmax   = 60;
370 #if NFS_MOUNT_VERSION >= 2
371         data.namlen     = NAME_MAX;
372 #endif
373
374         bg = 0;
375         soft = 0;
376         intr = 0;
377         posix = 0;
378         nocto = 0;
379         nolock = 0;
380         noac = 0;
381         retry = 10000;          /* 10000 minutes ~ 1 week */
382         tcp = 0;
383
384         mountprog = MOUNTPROG;
385         mountvers = 0;
386         port = 0;
387         mountport = 0;
388         nfsprog = NFS_PROGRAM;
389         nfsvers = 0;
390
391         /* parse options */
392
393         for (opt = strtok(old_opts, ","); opt; opt = strtok(NULL, ",")) {
394                 if ((opteq = strchr(opt, '='))) {
395                         val = atoi(opteq + 1);  
396                         *opteq = '\0';
397                         if (!strcmp(opt, "rsize"))
398                                 data.rsize = val;
399                         else if (!strcmp(opt, "wsize"))
400                                 data.wsize = val;
401                         else if (!strcmp(opt, "timeo"))
402                                 data.timeo = val;
403                         else if (!strcmp(opt, "retrans"))
404                                 data.retrans = val;
405                         else if (!strcmp(opt, "acregmin"))
406                                 data.acregmin = val;
407                         else if (!strcmp(opt, "acregmax"))
408                                 data.acregmax = val;
409                         else if (!strcmp(opt, "acdirmin"))
410                                 data.acdirmin = val;
411                         else if (!strcmp(opt, "acdirmax"))
412                                 data.acdirmax = val;
413                         else if (!strcmp(opt, "actimeo")) {
414                                 data.acregmin = val;
415                                 data.acregmax = val;
416                                 data.acdirmin = val;
417                                 data.acdirmax = val;
418                         }
419                         else if (!strcmp(opt, "retry"))
420                                 retry = val;
421                         else if (!strcmp(opt, "port"))
422                                 port = val;
423                         else if (!strcmp(opt, "mountport"))
424                                 mountport = val;
425                         else if (!strcmp(opt, "mounthost"))
426                                 mounthost=xstrndup(opteq+1,
427                                                   strcspn(opteq+1," \t\n\r,"));
428                         else if (!strcmp(opt, "mountprog"))
429                                 mountprog = val;
430                         else if (!strcmp(opt, "mountvers"))
431                                 mountvers = val;
432                         else if (!strcmp(opt, "nfsprog"))
433                                 nfsprog = val;
434                         else if (!strcmp(opt, "nfsvers") ||
435                                  !strcmp(opt, "vers"))
436                                 nfsvers = val;
437                         else if (!strcmp(opt, "proto")) {
438                                 if (!strncmp(opteq+1, "tcp", 3))
439                                         tcp = 1;
440                                 else if (!strncmp(opteq+1, "udp", 3))
441                                         tcp = 0;
442                                 else
443                                         printf(_("Warning: Unrecognized proto= option.\n"));
444                         } else if (!strcmp(opt, "namlen")) {
445 #if NFS_MOUNT_VERSION >= 2
446                                 if (nfs_mount_version >= 2)
447                                         data.namlen = val;
448                                 else
449 #endif
450                                 printf(_("Warning: Option namlen is not supported.\n"));
451                         } else if (!strcmp(opt, "addr"))
452                                 /* ignore */;
453                         else {
454                                 printf(_("unknown nfs mount parameter: "
455                                        "%s=%d\n"), opt, val);
456                                 goto fail;
457                         }
458                 }
459                 else {
460                         val = 1;
461                         if (!strncmp(opt, "no", 2)) {
462                                 val = 0;
463                                 opt += 2;
464                         }
465                         if (!strcmp(opt, "bg")) 
466                                 bg = val;
467                         else if (!strcmp(opt, "fg")) 
468                                 bg = !val;
469                         else if (!strcmp(opt, "soft"))
470                                 soft = val;
471                         else if (!strcmp(opt, "hard"))
472                                 soft = !val;
473                         else if (!strcmp(opt, "intr"))
474                                 intr = val;
475                         else if (!strcmp(opt, "posix"))
476                                 posix = val;
477                         else if (!strcmp(opt, "cto"))
478                                 nocto = !val;
479                         else if (!strcmp(opt, "ac"))
480                                 noac = !val;
481                         else if (!strcmp(opt, "tcp"))
482                                 tcp = val;
483                         else if (!strcmp(opt, "udp"))
484                                 tcp = !val;
485                         else if (!strcmp(opt, "lock")) {
486                                 if (nfs_mount_version >= 3)
487                                         nolock = !val;
488                                 else
489                                         printf(_("Warning: option nolock is not supported.\n"));
490                         } else {
491                                 printf(_("unknown nfs mount option: "
492                                            "%s%s\n"), val ? "" : "no", opt);
493                                 goto fail;
494                         }
495                 }
496         }
497         proto = (tcp) ? IPPROTO_TCP : IPPROTO_UDP;
498
499         data.flags = (soft ? NFS_MOUNT_SOFT : 0)
500                 | (intr ? NFS_MOUNT_INTR : 0)
501                 | (posix ? NFS_MOUNT_POSIX : 0)
502                 | (nocto ? NFS_MOUNT_NOCTO : 0)
503                 | (noac ? NFS_MOUNT_NOAC : 0);
504 #if NFS_MOUNT_VERSION >= 2
505         if (nfs_mount_version >= 2)
506                 data.flags |= (tcp ? NFS_MOUNT_TCP : 0);
507 #endif
508 #if NFS_MOUNT_VERSION >= 3
509         if (nfs_mount_version >= 3)
510                 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
511 #endif
512         if (nfsvers > MAX_NFSPROT) {
513                 error_msg("NFSv%d not supported!\n", nfsvers);
514                 return 0;
515         }
516         if (mountvers > MAX_NFSPROT) {
517                 error_msg("NFSv%d not supported!\n", nfsvers);
518                 return 0;
519         }
520         if (nfsvers && !mountvers)
521                 mountvers = (nfsvers < 3) ? 1 : nfsvers;
522         if (nfsvers && nfsvers < mountvers) {
523                 mountvers = nfsvers;
524         }
525
526         /* Adjust options if none specified */
527         if (!data.timeo)
528                 data.timeo = tcp ? 70 : 7;
529
530 #ifdef NFS_MOUNT_DEBUG
531         printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
532                 data.rsize, data.wsize, data.timeo, data.retrans);
533         printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
534                 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
535         printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
536                 port, bg, retry, data.flags);
537         printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
538                 mountprog, mountvers, nfsprog, nfsvers);
539         printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d\n",
540                 (data.flags & NFS_MOUNT_SOFT) != 0,
541                 (data.flags & NFS_MOUNT_INTR) != 0,
542                 (data.flags & NFS_MOUNT_POSIX) != 0,
543                 (data.flags & NFS_MOUNT_NOCTO) != 0,
544                 (data.flags & NFS_MOUNT_NOAC) != 0);
545 #if NFS_MOUNT_VERSION >= 2
546         printf("tcp = %d\n",
547                 (data.flags & NFS_MOUNT_TCP) != 0);
548 #endif
549 #endif
550
551         data.version = nfs_mount_version;
552         *mount_opts = (char *) &data;
553
554         if (*flags & MS_REMOUNT)
555                 return 0;
556
557         /*
558          * If the previous mount operation on the same host was
559          * backgrounded, and the "bg" for this mount is also set,
560          * give up immediately, to avoid the initial timeout.
561          */
562         if (bg && !running_bg &&
563             prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
564                 if (retry > 0)
565                         retval = EX_BG;
566                 return retval;
567         }
568
569         /* create mount deamon client */
570         /* See if the nfs host = mount host. */
571         if (mounthost) {
572           if (mounthost[0] >= '0' && mounthost[0] <= '9') {
573             mount_server_addr.sin_family = AF_INET;
574             mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
575           } else {
576                   if ((hp = gethostbyname(mounthost)) == NULL) {
577                           error_msg("can't get address for %s\n", hostname);
578                           goto fail;
579                   } else {
580                           if (hp->h_length > sizeof(struct in_addr)) {
581                                   error_msg("got bad hp->h_length?\n");
582                                   hp->h_length = sizeof(struct in_addr);
583                           }
584                           mount_server_addr.sin_family = AF_INET;
585                           memcpy(&mount_server_addr.sin_addr,
586                                  hp->h_addr, hp->h_length);
587                   }
588           }
589         }
590
591         /*
592          * The following loop implements the mount retries. On the first
593          * call, "running_bg" is 0. When the mount times out, and the
594          * "bg" option is set, the exit status EX_BG will be returned.
595          * For a backgrounded mount, there will be a second call by the
596          * child process with "running_bg" set to 1.
597          *
598          * The case where the mount point is not present and the "bg"
599          * option is set, is treated as a timeout. This is done to
600          * support nested mounts.
601          *
602          * The "retry" count specified by the user is the number of
603          * minutes to retry before giving up.
604          *
605          * Only the first error message will be displayed.
606          */
607         retry_timeout.tv_sec = 3;
608         retry_timeout.tv_usec = 0;
609         total_timeout.tv_sec = 20;
610         total_timeout.tv_usec = 0;
611         timeout = time(NULL) + 60 * retry;
612         prevt = 0;
613         t = 30;
614         val = 1;
615         for (;;) {
616                 if (bg && stat(node, &statbuf) == -1) {
617                         if (running_bg) {
618                                 sleep(val);     /* 1, 2, 4, 8, 16, 30, ... */
619                                 val *= 2;
620                                 if (val > 30)
621                                         val = 30;
622                         }
623                 } else {
624                         /* be careful not to use too many CPU cycles */
625                         if (t - prevt < 30)
626                                 sleep(30);
627
628                         pm_mnt = get_mountport(&mount_server_addr,
629                                        mountprog,
630                                        mountvers,
631                                        proto,
632                                        mountport);
633
634                         /* contact the mount daemon via TCP */
635                         mount_server_addr.sin_port = htons(pm_mnt->pm_port);
636                         msock = RPC_ANYSOCK;
637
638                         switch (pm_mnt->pm_prot) {
639                         case IPPROTO_UDP:
640                                 mclient = clntudp_create(&mount_server_addr,
641                                                  pm_mnt->pm_prog,
642                                                  pm_mnt->pm_vers,
643                                                  retry_timeout,
644                                                  &msock);
645                   if (mclient)
646                           break;
647                   mount_server_addr.sin_port = htons(pm_mnt->pm_port);
648                   msock = RPC_ANYSOCK;
649                 case IPPROTO_TCP:
650                         mclient = clnttcp_create(&mount_server_addr,
651                                                  pm_mnt->pm_prog,
652                                                  pm_mnt->pm_vers,
653                                                  &msock, 0, 0);
654                         break;
655                 default:
656                         mclient = 0;
657                         }
658                         if (mclient) {
659                                 /* try to mount hostname:dirname */
660                                 mclient->cl_auth = authunix_create_default();
661
662                         /* make pointers in xdr_mountres3 NULL so
663                          * that xdr_array allocates memory for us
664                          */
665                         memset(&status, 0, sizeof(status));
666
667                         if (pm_mnt->pm_vers == 3)
668                                 clnt_stat = clnt_call(mclient, MOUNTPROC3_MNT,
669                                                       (xdrproc_t) xdr_dirpath,
670                                                       (caddr_t) &dirname,
671                                                       (xdrproc_t) xdr_mountres3,
672                                                       (caddr_t) &status,
673                                         total_timeout);
674                         else
675                                 clnt_stat = clnt_call(mclient, MOUNTPROC_MNT,
676                                                       (xdrproc_t) xdr_dirpath,
677                                                       (caddr_t) &dirname,
678                                                       (xdrproc_t) xdr_fhstatus,
679                                                       (caddr_t) &status,
680                                                       total_timeout);
681
682                                 if (clnt_stat == RPC_SUCCESS)
683                                         break;          /* we're done */
684                                 if (errno != ECONNREFUSED) {
685                                         clnt_perror(mclient, "mount");
686                                         goto fail;      /* don't retry */
687                                 }
688                                 if (!running_bg && prevt == 0)
689                                         clnt_perror(mclient, "mount");
690                                 auth_destroy(mclient->cl_auth);
691                                 clnt_destroy(mclient);
692                                 mclient = 0;
693                                 close(msock);
694                         } else {
695                                 if (!running_bg && prevt == 0)
696                                         clnt_pcreateerror("mount");
697                         }
698                         prevt = t;
699                 }
700                 if (!bg)
701                         goto fail;
702                 if (!running_bg) {
703                         prev_bg_host = xstrdup(hostname);
704                         if (retry > 0)
705                                 retval = EX_BG;
706                         goto fail;
707                 }
708                 t = time(NULL);
709                 if (t >= timeout)
710                         goto fail;
711         }
712         nfsvers = (pm_mnt->pm_vers < 2) ? 2 : pm_mnt->pm_vers;
713
714         if (nfsvers == 2) {
715                 if (status.nfsv2.fhs_status != 0) {
716                         error_msg("%s:%s failed, reason given by server: %s\n",
717                                 hostname, dirname,
718                                 nfs_strerror(status.nfsv2.fhs_status));
719                         goto fail;
720                 }
721                 memcpy(data.root.data,
722                        (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
723                        NFS_FHSIZE);
724 #if NFS_MOUNT_VERSION >= 4
725                 data.root.size = NFS_FHSIZE;
726                 memcpy(data.old_root.data,
727                        (char *) status.nfsv2.fhstatus_u.fhs_fhandle,
728                        NFS_FHSIZE);
729 #endif
730         } else {
731 #if NFS_MOUNT_VERSION >= 4
732                 fhandle3 *fhandle;
733                 if (status.nfsv3.fhs_status != 0) {
734                         error_msg("%s:%s failed, reason given by server: %s\n",
735                                 hostname, dirname,
736                                 nfs_strerror(status.nfsv3.fhs_status));
737                         goto fail;
738                 }
739                 fhandle = &status.nfsv3.mountres3_u.mountinfo.fhandle;
740                 memset(data.old_root.data, 0, NFS_FHSIZE);
741                 memset(&data.root, 0, sizeof(data.root));
742                 data.root.size = fhandle->fhandle3_len;
743                 memcpy(data.root.data,
744                        (char *) fhandle->fhandle3_val,
745                        fhandle->fhandle3_len);
746
747                 data.flags |= NFS_MOUNT_VER3;
748 #endif
749         }
750
751         /* create nfs socket for kernel */
752
753         if (tcp) {
754                 if (nfs_mount_version < 3) {
755                         printf(_("NFS over TCP is not supported.\n"));
756                         goto fail;
757                 }
758                 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
759         } else
760                 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
761         if (fsock < 0) {
762                 perror(_("nfs socket"));
763                 goto fail;
764         }
765         if (bindresvport(fsock, 0) < 0) {
766                 perror(_("nfs bindresvport"));
767                 goto fail;
768         }
769         if (port == 0) {
770                 server_addr.sin_port = PMAPPORT;
771                 port = pmap_getport(&server_addr, nfsprog, nfsvers,
772                         tcp ? IPPROTO_TCP : IPPROTO_UDP);
773                 if (port == 0)
774                         port = NFS_PORT;
775 #ifdef NFS_MOUNT_DEBUG
776                 else
777                         printf(_("used portmapper to find NFS port\n"));
778 #endif
779         }
780 #ifdef NFS_MOUNT_DEBUG
781         printf(_("using port %d for nfs deamon\n"), port);
782 #endif
783         server_addr.sin_port = htons(port);
784          /*
785           * connect() the socket for kernels 1.3.10 and below only,
786           * to avoid problems with multihomed hosts.
787           * --Swen
788           */
789         if (get_kernel_revision() <= 66314
790             && connect(fsock, (struct sockaddr *) &server_addr,
791                        sizeof (server_addr)) < 0) {
792                 perror(_("nfs connect"));
793                 goto fail;
794         }
795
796         /* prepare data structure for kernel */
797
798         data.fd = fsock;
799         memcpy((char *) &data.addr, (char *) &server_addr, sizeof(data.addr));
800         strncpy(data.hostname, hostname, sizeof(data.hostname));
801
802         /* clean up */
803
804         auth_destroy(mclient->cl_auth);
805         clnt_destroy(mclient);
806         close(msock);
807         return 0;
808
809         /* abort */
810
811 fail:
812         if (msock != -1) {
813                 if (mclient) {
814                         auth_destroy(mclient->cl_auth);
815                         clnt_destroy(mclient);
816                 }
817                 close(msock);
818         }
819         if (fsock != -1)
820                 close(fsock);
821         return retval;
822 }       
823
824 /*
825  * We need to translate between nfs status return values and
826  * the local errno values which may not be the same.
827  *
828  * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
829  * "after #include <errno.h> the symbol errno is reserved for any use,
830  *  it cannot even be used as a struct tag or field name".
831  */
832
833 #ifndef EDQUOT
834 #define EDQUOT  ENOSPC
835 #endif
836
837 static struct {
838         enum nfs_stat stat;
839         int errnum;
840 } nfs_errtbl[] = {
841         { NFS_OK,               0               },
842         { NFSERR_PERM,          EPERM           },
843         { NFSERR_NOENT,         ENOENT          },
844         { NFSERR_IO,            EIO             },
845         { NFSERR_NXIO,          ENXIO           },
846         { NFSERR_ACCES,         EACCES          },
847         { NFSERR_EXIST,         EEXIST          },
848         { NFSERR_NODEV,         ENODEV          },
849         { NFSERR_NOTDIR,        ENOTDIR         },
850         { NFSERR_ISDIR,         EISDIR          },
851 #ifdef NFSERR_INVAL
852         { NFSERR_INVAL,         EINVAL          },      /* that Sun forgot */
853 #endif
854         { NFSERR_FBIG,          EFBIG           },
855         { NFSERR_NOSPC,         ENOSPC          },
856         { NFSERR_ROFS,          EROFS           },
857         { NFSERR_NAMETOOLONG,   ENAMETOOLONG    },
858         { NFSERR_NOTEMPTY,      ENOTEMPTY       },
859         { NFSERR_DQUOT,         EDQUOT          },
860         { NFSERR_STALE,         ESTALE          },
861 #ifdef EWFLUSH
862         { NFSERR_WFLUSH,        EWFLUSH         },
863 #endif
864         /* Throw in some NFSv3 values for even more fun (HP returns these) */
865         { 71,                   EREMOTE         },
866
867         { -1,                   EIO             }
868 };
869
870 static char *nfs_strerror(int stat)
871 {
872         int i;
873         static char buf[256];
874
875         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
876                 if (nfs_errtbl[i].stat == stat)
877                         return strerror(nfs_errtbl[i].errnum);
878         }
879         sprintf(buf, _("unknown nfs status return value: %d"), stat);
880         return buf;
881 }
882
883 bool_t
884 xdr_fhandle (XDR *xdrs, fhandle objp)
885 {
886         //register int32_t *buf;
887
888          if (!xdr_opaque (xdrs, objp, FHSIZE))
889                  return FALSE;
890         return TRUE;
891 }
892
893 bool_t
894 xdr_fhstatus (XDR *xdrs, fhstatus *objp)
895 {
896         //register int32_t *buf;
897
898          if (!xdr_u_int (xdrs, &objp->fhs_status))
899                  return FALSE;
900         switch (objp->fhs_status) {
901         case 0:
902                  if (!xdr_fhandle (xdrs, objp->fhstatus_u.fhs_fhandle))
903                          return FALSE;
904                 break;
905         default:
906                 break;
907         }
908         return TRUE;
909 }
910
911 bool_t
912 xdr_dirpath (XDR *xdrs, dirpath *objp)
913 {
914         //register int32_t *buf;
915
916          if (!xdr_string (xdrs, objp, MNTPATHLEN))
917                  return FALSE;
918         return TRUE;
919 }
920
921 bool_t
922 xdr_fhandle3 (XDR *xdrs, fhandle3 *objp)
923 {
924         //register int32_t *buf;
925
926          if (!xdr_bytes (xdrs, (char **)&objp->fhandle3_val, (u_int *) &objp->fhandle3_len, FHSIZE3))
927                  return FALSE;
928         return TRUE;
929 }
930
931 bool_t
932 xdr_mountres3_ok (XDR *xdrs, mountres3_ok *objp)
933 {
934         //register int32_t *buf;
935
936          if (!xdr_fhandle3 (xdrs, &objp->fhandle))
937                  return FALSE;
938          if (!xdr_array (xdrs, (char **)&objp->auth_flavours.auth_flavours_val, (u_int *) &objp->auth_flavours.auth_flavours_len, ~0,
939                 sizeof (int), (xdrproc_t) xdr_int))
940                  return FALSE;
941         return TRUE;
942 }
943
944 bool_t
945 xdr_mountstat3 (XDR *xdrs, mountstat3 *objp)
946 {
947         //register int32_t *buf;
948
949          if (!xdr_enum (xdrs, (enum_t *) objp))
950                  return FALSE;
951         return TRUE;
952 }
953
954 bool_t
955 xdr_mountres3 (XDR *xdrs, mountres3 *objp)
956 {
957         //register int32_t *buf;
958
959          if (!xdr_mountstat3 (xdrs, &objp->fhs_status))
960                  return FALSE;
961         switch (objp->fhs_status) {
962         case MNT_OK:
963                  if (!xdr_mountres3_ok (xdrs, &objp->mountres3_u.mountinfo))
964                          return FALSE;
965                 break;
966         default:
967                 break;
968         }
969         return TRUE;
970 }
971