Merge branch 'vendor/BINUTILS220' into bu220
[dragonfly.git] / contrib / amd / fixmount / fixmount.c
1 /*
2  * Copyright (c) 1997-1999 Erez Zadok
3  * Copyright (c) 1990 Jan-Simon Pendry
4  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry at Imperial College, London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgment:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      %W% (Berkeley) %G%
40  *
41  * $Id: fixmount.c,v 1.4 1999/02/04 07:24:42 ezk Exp $
42  * $FreeBSD: src/contrib/amd/fixmount/fixmount.c,v 1.5 1999/09/15 05:45:14 obrien Exp $
43  * $DragonFly: src/contrib/amd/fixmount/fixmount.c,v 1.2 2003/06/17 04:23:57 dillon Exp $
44  *
45  */
46
47 #ifdef HAVE_CONFIG_H
48 # include <config.h>
49 #endif /* HAVE_CONFIG_H */
50 #include <am_defs.h>
51
52 #define CREATE_TIMEOUT  2       /* seconds */
53 #define CALL_TIMEOUT    5       /* seconds */
54
55 #ifndef INADDR_NONE
56 # define INADDR_NONE    0xffffffff
57 #endif /* not INADDR_NONE */
58
59 /* Constant defs */
60 #define ALL             1
61 #define DIRS            2
62
63 #define DODUMP          0x1
64 #define DOEXPORTS       0x2
65 #define DOREMOVE        0x4
66 #define DOVERIFY        0x8
67 #define DOREMALL        0x10
68
69 extern int fixmount_check_mount(char *host, struct in_addr hostaddr, char *path);
70
71 static char dir_path[NFS_MAXPATHLEN];
72 static char localhost[] = "localhost";
73 static char thishost[MAXHOSTNAMELEN + 1] = "";
74 static exports mntexports;
75 static int quiet = 0;
76 static int type = 0;
77 static jmp_buf before_rpc;
78 static mountlist mntdump;
79 static struct in_addr thisaddr;
80 static CLIENT *clnt_create_timeout(char *, struct timeval *);
81
82 RETSIGTYPE create_timeout(int);
83 int is_same_host(char *, char *, struct in_addr);
84 int main(int, char *[]);
85 int remove_all(CLIENT *, char *);
86 int remove_mount(CLIENT *, char *, mountlist, int);
87 void fix_rmtab(CLIENT *, char *, mountlist, int, int);
88 void print_dump(mountlist);
89 void usage(void);
90
91
92 void
93 usage(void)
94 {
95   fprintf(stderr, "usage: fixmount [-adervAqf] [-h hostname] host ...\n");
96   exit(1);
97 }
98
99
100 /*
101  * Check hostname against other name and its IP address
102  */
103 int
104 is_same_host(char *name1, char *name2, struct in_addr addr2)
105 {
106   if (strcasecmp(name1, name2) == 0) {
107     return 1;
108   } else if (addr2.s_addr == INADDR_NONE) {
109     return 0;
110   } else {
111     static char lasthost[MAXHOSTNAMELEN] = "";
112     static struct in_addr addr1;
113     struct hostent *he;
114
115     /*
116      * To save nameserver lookups, and because this function
117      * is typically called repeatedly on the same names,
118      * cache the last lookup result and reuse it if possible.
119      */
120     if (strcasecmp(name1, lasthost) == 0) {
121       return (addr1.s_addr == addr2.s_addr);
122     } else if (!(he = gethostbyname(name1))) {
123       return 0;
124     } else {
125       strncpy(lasthost, name1, sizeof(lasthost) - 1);
126       memcpy(&addr1, he->h_addr, sizeof(addr1));
127       return (addr1.s_addr == addr2.s_addr);
128     }
129   }
130 }
131
132
133 /*
134  * Print the binary tree in inorder so that output is sorted.
135  */
136 void
137 print_dump(mountlist mp)
138 {
139   if (mp == NULL)
140     return;
141   if (is_same_host(mp->ml_hostname, thishost, thisaddr)) {
142     switch (type) {
143     case ALL:
144       printf("%s:%s\n", mp->ml_hostname, mp->ml_directory);
145       break;
146     case DIRS:
147       printf("%s\n", mp->ml_directory);
148       break;
149     default:
150       printf("%s\n", mp->ml_hostname);
151       break;
152     };
153   }
154   if (mp->ml_next)
155     print_dump(mp->ml_next);
156 }
157
158
159 /*
160  * remove entry from remote rmtab
161  */
162 int
163 remove_mount(CLIENT *client, char *host, mountlist ml, int fixit)
164 {
165   enum clnt_stat estat;
166   struct timeval tv;
167   char *pathp = dir_path;
168
169   strncpy(dir_path, ml->ml_directory, sizeof(dir_path));
170
171   if (!fixit) {
172     printf("%s: bogus mount %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
173     fflush(stdout);
174   } else {
175     printf("%s: removing %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
176     fflush(stdout);
177
178     tv.tv_sec = CALL_TIMEOUT;
179     tv.tv_usec = 0;
180
181     if ((estat = clnt_call(client,
182                            MOUNTPROC_UMNT,
183                            (XDRPROC_T_TYPE) xdr_dirpath,
184                            (char *) &pathp,
185                            (XDRPROC_T_TYPE) xdr_void,
186                            (char *) 0,
187                            tv)) != RPC_SUCCESS) {
188       fprintf(stderr, "%s:%s MOUNTPROC_UMNT: ",
189               host, ml->ml_directory);
190       clnt_perrno(estat);
191       fflush(stderr);
192       return -1;
193     }
194   }
195   return 0;
196 }
197
198
199 /*
200  * fix mount list on remote host
201  */
202 void
203 fix_rmtab(CLIENT *client, char *host, mountlist mp, int fixit, int force)
204 {
205   mountlist p;
206   struct hostent *he;
207   struct in_addr hostaddr;
208
209   /*
210    * Obtain remote address for comparisons
211    */
212   if ((he = gethostbyname(host))) {
213     memcpy(&hostaddr, he->h_addr, sizeof(hostaddr));
214   } else {
215     hostaddr.s_addr = INADDR_NONE;
216   }
217
218   for (p = mp; p; p = p->ml_next) {
219     if (is_same_host(p->ml_hostname, thishost, thisaddr)) {
220       if (force || !fixmount_check_mount(host, hostaddr, p->ml_directory))
221         remove_mount(client, host, p, fixit);
222     }
223   }
224 }
225
226
227 /*
228  * remove all entries from remote rmtab
229  */
230 int
231 remove_all(CLIENT *client, char *host)
232 {
233   enum clnt_stat estat;
234   struct timeval tv;
235
236   printf("%s: removing ALL\n", host);
237   fflush(stdout);
238
239   tv.tv_sec = CALL_TIMEOUT;
240   tv.tv_usec = 0;
241
242   if ((estat = clnt_call(client,
243                          MOUNTPROC_UMNTALL,
244                          (XDRPROC_T_TYPE) xdr_void,
245                          (char *) 0,
246                          (XDRPROC_T_TYPE) xdr_void,
247                          (char *) 0,
248                          tv)) != RPC_SUCCESS) {
249     /*
250      * RPC_SYSTEMERROR is returned even if all went well
251      */
252     if (estat != RPC_SYSTEMERROR) {
253       fprintf(stderr, "%s MOUNTPROC_UMNTALL: ", host);
254       clnt_perrno(estat);
255       fflush(stderr);
256       return -1;
257     }
258   }
259
260   return 0;
261 }
262
263
264 /*
265  * This command queries the NFS mount daemon for it's mount list and/or
266  * it's exports list and prints them out.
267  * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
268  * for detailed information on the protocol.
269  */
270 int
271 main(int argc, char *argv[])
272 {
273   AUTH *auth;
274   CLIENT *client;
275   char *host;
276   enum clnt_stat estat;
277   exports exp;
278   extern char *optarg;
279   extern int optind;
280   groups grp;
281   int ch;
282   int force = 0;
283   int morethanone;
284   register int rpcs = 0;
285   struct timeval tv;
286
287   while ((ch = getopt(argc, argv, "adervAqfh:")) != -1)
288     switch ((char) ch) {
289
290     case 'a':
291       if (type == 0) {
292         type = ALL;
293         rpcs |= DODUMP;
294       } else
295         usage();
296       break;
297
298     case 'd':
299       if (type == 0) {
300         type = DIRS;
301         rpcs |= DODUMP;
302       } else
303         usage();
304       break;
305
306     case 'e':
307       rpcs |= DOEXPORTS;
308       break;
309
310     case 'r':
311       rpcs |= DOREMOVE;
312       break;
313
314     case 'A':
315       rpcs |= DOREMALL;
316       break;
317
318     case 'v':
319       rpcs |= DOVERIFY;
320       break;
321
322     case 'q':
323       quiet = 1;
324       break;
325
326     case 'f':
327       force = 1;
328       break;
329
330     case 'h':
331       strncpy(thishost, optarg, sizeof(thishost));
332       thishost[sizeof(thishost) - 1] = '\0';
333       break;
334
335     case '?':
336     default:
337       usage();
338     }
339
340   if (optind == argc)
341     usage();
342
343   if (rpcs == 0)
344     rpcs = DODUMP;
345
346   if (!*thishost) {
347     struct hostent *he;
348
349     if (gethostname(thishost, sizeof(thishost)) < 0) {
350       perror("gethostname");
351       exit(1);
352     }
353     thishost[sizeof(thishost) - 1] = '\0';
354
355     /*
356      * We need the hostname as it appears to the other side's
357      * mountd, so get our own hostname by reverse address
358      * resolution.
359      */
360     if (!(he = gethostbyname(thishost))) {
361       fprintf(stderr, "gethostbyname failed on %s\n",
362               thishost);
363       exit(1);
364     }
365     memcpy(&thisaddr, he->h_addr, sizeof(thisaddr));
366     if (!(he = gethostbyaddr((char *) &thisaddr, sizeof(thisaddr),
367                              he->h_addrtype))) {
368       fprintf(stderr, "gethostbyaddr failed on %s\n",
369               inet_ntoa(thisaddr));
370       exit(1);
371     }
372     strncpy(thishost, he->h_name, sizeof(thishost));
373     thishost[sizeof(thishost) - 1] = '\0';
374   } else {
375     thisaddr.s_addr = INADDR_NONE;
376   }
377
378   if (!(auth = authunix_create_default())) {
379     fprintf(stderr, "couldn't create authentication handle\n");
380     exit(1);
381   }
382   morethanone = (optind + 1 < argc);
383
384   for (; optind < argc; optind++) {
385
386     host = argv[optind];
387     tv.tv_sec = CREATE_TIMEOUT;
388     tv.tv_usec = 0;
389
390     if (!(client = clnt_create_timeout(host, &tv)))
391         continue;
392
393     client->cl_auth = auth;
394     tv.tv_sec = CALL_TIMEOUT;
395     tv.tv_usec = 0;
396
397     if (rpcs & (DODUMP | DOREMOVE | DOVERIFY))
398       if ((estat = clnt_call(client,
399                              MOUNTPROC_DUMP,
400                              (XDRPROC_T_TYPE) xdr_void,
401                              (char *) 0,
402                              (XDRPROC_T_TYPE) xdr_mountlist,
403                              (char *) &mntdump,
404                              tv)) != RPC_SUCCESS) {
405         fprintf(stderr, "%s: MOUNTPROC_DUMP: ", host);
406         clnt_perrno(estat);
407         fflush(stderr);
408         mntdump = NULL;
409         goto next;
410       }
411     if (rpcs & DOEXPORTS)
412       if ((estat = clnt_call(client,
413                              MOUNTPROC_EXPORT,
414                              (XDRPROC_T_TYPE) xdr_void,
415                              (char *) 0,
416                              (XDRPROC_T_TYPE) xdr_exports,
417                              (char *) &mntexports,
418                              tv)) != RPC_SUCCESS) {
419         fprintf(stderr, "%s: MOUNTPROC_EXPORT: ", host);
420         clnt_perrno(estat);
421         fflush(stderr);
422         mntexports = NULL;
423         goto next;
424       }
425
426     /* Now just print out the results */
427     if ((rpcs & (DODUMP | DOEXPORTS)) &&
428         morethanone) {
429       printf(">>> %s <<<\n", host);
430       fflush(stdout);
431     }
432
433     if (rpcs & DODUMP) {
434       print_dump(mntdump);
435     }
436
437     if (rpcs & DOEXPORTS) {
438       exp = mntexports;
439       while (exp) {
440         printf("%-35s", exp->ex_dir);
441         grp = exp->ex_groups;
442         if (grp == NULL) {
443           printf("Everyone\n");
444         } else {
445           while (grp) {
446             printf("%s ", grp->gr_name);
447             grp = grp->gr_next;
448           }
449           printf("\n");
450         }
451         exp = exp->ex_next;
452       }
453     }
454
455     if (rpcs & DOVERIFY)
456       fix_rmtab(client, host, mntdump, 0, force);
457
458     if (rpcs & DOREMOVE)
459       fix_rmtab(client, host, mntdump, 1, force);
460
461     if (rpcs & DOREMALL)
462       remove_all(client, host);
463
464   next:
465     if (mntdump)
466       (void) clnt_freeres(client,
467                           (XDRPROC_T_TYPE) xdr_mountlist,
468                           (char *) &mntdump);
469     if (mntexports)
470       (void) clnt_freeres(client,
471                           (XDRPROC_T_TYPE) xdr_exports,
472                           (char *) &mntexports);
473
474     clnt_destroy(client);
475   }
476   exit(0);
477   return 0; /* should never reach here */
478 }
479
480
481 RETSIGTYPE
482 create_timeout(int sig)
483 {
484   signal(SIGALRM, SIG_DFL);
485   longjmp(before_rpc, 1);
486 }
487
488
489 #ifndef HAVE_TRANSPORT_TYPE_TLI
490 /*
491  * inetresport creates a datagram socket and attempts to bind it to a
492  * secure port.
493  * returns: The bound socket, or -1 to indicate an error.
494  */
495 static int
496 inetresport(int ty)
497 {
498   int alport;
499   struct sockaddr_in addr;
500   int fd;
501
502   /* Use internet address family */
503   addr.sin_family = AF_INET;
504   addr.sin_addr.s_addr = INADDR_ANY;
505   if ((fd = socket(AF_INET, ty, 0)) < 0)
506     return -1;
507
508   for (alport = IPPORT_RESERVED - 1; alport > IPPORT_RESERVED / 2 + 1; alport--) {
509     addr.sin_port = htons((u_short) alport);
510     if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) >= 0)
511       return fd;
512     if (errno != EADDRINUSE) {
513       close(fd);
514       return -1;
515     }
516   }
517   close(fd);
518   errno = EAGAIN;
519   return -1;
520 }
521
522
523 /*
524  * Privsock() calls inetresport() to attempt to bind a socket to a secure
525  * port.  If inetresport() fails, privsock returns a magic socket number which
526  * indicates to RPC that it should make its own socket.
527  * returns: A privileged socket # or RPC_ANYSOCK.
528  */
529 static int
530 privsock(int ty)
531 {
532   int sock = inetresport(ty);
533
534   if (sock < 0) {
535     errno = 0;
536     /* Couldn't get a secure port, let RPC make an insecure one */
537     sock = RPC_ANYSOCK;
538   }
539   return sock;
540 }
541 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
542
543
544 static CLIENT *
545 clnt_create_timeout(char *host, struct timeval *tvp)
546 {
547   CLIENT *clnt;
548   struct sockaddr_in host_addr;
549   struct hostent *hp;
550 #ifndef HAVE_TRANSPORT_TYPE_TLI
551   int s;
552 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
553
554   if (setjmp(before_rpc)) {
555     if (!quiet) {
556       fprintf(stderr, "%s: ", host);
557       clnt_perrno(RPC_TIMEDOUT);
558       fprintf(stderr, "\n");
559       fflush(stderr);
560     }
561     return NULL;
562   }
563   signal(SIGALRM, create_timeout);
564   ualarm(tvp->tv_sec * 1000000 + tvp->tv_usec, 0);
565
566   /*
567    * Get address of host
568    */
569   if ((hp = gethostbyname(host)) == 0 && !STREQ(host, localhost)) {
570     fprintf(stderr, "can't get address of %s\n", host);
571     return NULL;
572   }
573   memset(&host_addr, 0, sizeof host_addr);
574   host_addr.sin_family = AF_INET;
575   if (hp) {
576     memmove((voidp) &host_addr.sin_addr, (voidp) hp->h_addr,
577             sizeof(host_addr.sin_addr));
578   } else {
579     /* fake "localhost" */
580     host_addr.sin_addr.s_addr = htonl(0x7f000001);
581   }
582
583 #ifdef HAVE_TRANSPORT_TYPE_TLI
584   /* try TCP first (in case return data is large), then UDP */
585   clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "tcp");
586   if (!clnt)
587     clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "udp");
588 #else /* not HAVE_TRANSPORT_TYPE_TLI */
589   s = RPC_ANYSOCK;
590   clnt = clnttcp_create(&host_addr, MOUNTPROG, MOUNTVERS, &s, 0, 0);
591   if (!clnt) {
592     /* XXX: do we need to close(s) ? */
593     s = privsock(SOCK_DGRAM);
594     clnt = clntudp_create(&host_addr, MOUNTPROG, MOUNTVERS, *tvp, &s);
595   }
596 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
597
598   if (!clnt) {
599     ualarm(0, 0);
600     if (!quiet) {
601       clnt_pcreateerror(host);
602       fflush(stderr);
603     }
604     return NULL;
605   }
606
607   ualarm(0, 0);
608   return clnt;
609 }