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