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.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
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 $
48 #endif /* HAVE_CONFIG_H */
51 #define CREATE_TIMEOUT 2 /* seconds */
52 #define CALL_TIMEOUT 5 /* seconds */
55 # define INADDR_NONE 0xffffffff
56 #endif /* not INADDR_NONE */
68 extern int fixmount_check_mount(char *host, struct in_addr hostaddr, char *path);
70 static char dir_path[NFS_MAXPATHLEN];
71 static char localhost[] = "localhost";
72 static char thishost[MAXHOSTNAMELEN + 1] = "";
73 static exports mntexports;
76 static jmp_buf before_rpc;
77 static mountlist mntdump;
78 static struct in_addr thisaddr;
79 static CLIENT *clnt_create_timeout(char *, struct timeval *);
81 RETSIGTYPE create_timeout(int);
82 int is_same_host(char *, char *, struct in_addr);
83 int main(int, char *[]);
84 int remove_all(CLIENT *, char *);
85 int remove_mount(CLIENT *, char *, mountlist, int);
86 void fix_rmtab(CLIENT *, char *, mountlist, int, int);
87 void print_dump(mountlist);
94 fprintf(stderr, "usage: fixmount [-adervAqf] [-h hostname] host ...\n");
100 * Check hostname against other name and its IP address
103 is_same_host(char *name1, char *name2, struct in_addr addr2)
105 if (strcasecmp(name1, name2) == 0) {
107 } else if (addr2.s_addr == INADDR_NONE) {
110 static char lasthost[MAXHOSTNAMELEN] = "";
111 static struct in_addr addr1;
115 * To save nameserver lookups, and because this function
116 * is typically called repeatedly on the same names,
117 * cache the last lookup result and reuse it if possible.
119 if (strcasecmp(name1, lasthost) == 0) {
120 return (addr1.s_addr == addr2.s_addr);
121 } else if (!(he = gethostbyname(name1))) {
124 strncpy(lasthost, name1, sizeof(lasthost) - 1);
125 memcpy(&addr1, he->h_addr, sizeof(addr1));
126 return (addr1.s_addr == addr2.s_addr);
133 * Print the binary tree in inorder so that output is sorted.
136 print_dump(mountlist mp)
140 if (is_same_host(mp->ml_hostname, thishost, thisaddr)) {
143 printf("%s:%s\n", mp->ml_hostname, mp->ml_directory);
146 printf("%s\n", mp->ml_directory);
149 printf("%s\n", mp->ml_hostname);
154 print_dump(mp->ml_next);
159 * remove entry from remote rmtab
162 remove_mount(CLIENT *client, char *host, mountlist ml, int fixit)
164 enum clnt_stat estat;
166 char *pathp = dir_path;
168 strncpy(dir_path, ml->ml_directory, sizeof(dir_path));
171 printf("%s: bogus mount %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
174 printf("%s: removing %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
177 tv.tv_sec = CALL_TIMEOUT;
180 if ((estat = clnt_call(client,
182 (XDRPROC_T_TYPE) xdr_dirpath,
184 (XDRPROC_T_TYPE) xdr_void,
186 tv)) != RPC_SUCCESS) {
187 fprintf(stderr, "%s:%s MOUNTPROC_UMNT: ",
188 host, ml->ml_directory);
199 * fix mount list on remote host
202 fix_rmtab(CLIENT *client, char *host, mountlist mp, int fixit, int force)
206 struct in_addr hostaddr;
209 * Obtain remote address for comparisons
211 if ((he = gethostbyname(host))) {
212 memcpy(&hostaddr, he->h_addr, sizeof(hostaddr));
214 hostaddr.s_addr = INADDR_NONE;
217 for (p = mp; p; p = p->ml_next) {
218 if (is_same_host(p->ml_hostname, thishost, thisaddr)) {
219 if (force || !fixmount_check_mount(host, hostaddr, p->ml_directory))
220 remove_mount(client, host, p, fixit);
227 * remove all entries from remote rmtab
230 remove_all(CLIENT *client, char *host)
232 enum clnt_stat estat;
235 printf("%s: removing ALL\n", host);
238 tv.tv_sec = CALL_TIMEOUT;
241 if ((estat = clnt_call(client,
243 (XDRPROC_T_TYPE) xdr_void,
245 (XDRPROC_T_TYPE) xdr_void,
247 tv)) != RPC_SUCCESS) {
249 * RPC_SYSTEMERROR is returned even if all went well
251 if (estat != RPC_SYSTEMERROR) {
252 fprintf(stderr, "%s MOUNTPROC_UMNTALL: ", host);
264 * This command queries the NFS mount daemon for it's mount list and/or
265 * it's exports list and prints them out.
266 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
267 * for detailed information on the protocol.
270 main(int argc, char *argv[])
275 enum clnt_stat estat;
283 register int rpcs = 0;
286 while ((ch = getopt(argc, argv, "adervAqfh:")) != -1)
330 strncpy(thishost, optarg, sizeof(thishost));
331 thishost[sizeof(thishost) - 1] = '\0';
348 if (gethostname(thishost, sizeof(thishost)) < 0) {
349 perror("gethostname");
352 thishost[sizeof(thishost) - 1] = '\0';
355 * We need the hostname as it appears to the other side's
356 * mountd, so get our own hostname by reverse address
359 if (!(he = gethostbyname(thishost))) {
360 fprintf(stderr, "gethostbyname failed on %s\n",
364 memcpy(&thisaddr, he->h_addr, sizeof(thisaddr));
365 if (!(he = gethostbyaddr((char *) &thisaddr, sizeof(thisaddr),
367 fprintf(stderr, "gethostbyaddr failed on %s\n",
368 inet_ntoa(thisaddr));
371 strncpy(thishost, he->h_name, sizeof(thishost));
372 thishost[sizeof(thishost) - 1] = '\0';
374 thisaddr.s_addr = INADDR_NONE;
377 if (!(auth = authunix_create_default())) {
378 fprintf(stderr, "couldn't create authentication handle\n");
381 morethanone = (optind + 1 < argc);
383 for (; optind < argc; optind++) {
386 tv.tv_sec = CREATE_TIMEOUT;
389 if (!(client = clnt_create_timeout(host, &tv)))
392 client->cl_auth = auth;
393 tv.tv_sec = CALL_TIMEOUT;
396 if (rpcs & (DODUMP | DOREMOVE | DOVERIFY))
397 if ((estat = clnt_call(client,
399 (XDRPROC_T_TYPE) xdr_void,
401 (XDRPROC_T_TYPE) xdr_mountlist,
403 tv)) != RPC_SUCCESS) {
404 fprintf(stderr, "%s: MOUNTPROC_DUMP: ", host);
410 if (rpcs & DOEXPORTS)
411 if ((estat = clnt_call(client,
413 (XDRPROC_T_TYPE) xdr_void,
415 (XDRPROC_T_TYPE) xdr_exports,
416 (char *) &mntexports,
417 tv)) != RPC_SUCCESS) {
418 fprintf(stderr, "%s: MOUNTPROC_EXPORT: ", host);
425 /* Now just print out the results */
426 if ((rpcs & (DODUMP | DOEXPORTS)) &&
428 printf(">>> %s <<<\n", host);
436 if (rpcs & DOEXPORTS) {
439 printf("%-35s", exp->ex_dir);
440 grp = exp->ex_groups;
442 printf("Everyone\n");
445 printf("%s ", grp->gr_name);
455 fix_rmtab(client, host, mntdump, 0, force);
458 fix_rmtab(client, host, mntdump, 1, force);
461 remove_all(client, host);
465 (void) clnt_freeres(client,
466 (XDRPROC_T_TYPE) xdr_mountlist,
469 (void) clnt_freeres(client,
470 (XDRPROC_T_TYPE) xdr_exports,
471 (char *) &mntexports);
473 clnt_destroy(client);
476 return 0; /* should never reach here */
481 create_timeout(int sig)
483 signal(SIGALRM, SIG_DFL);
484 longjmp(before_rpc, 1);
488 #ifndef HAVE_TRANSPORT_TYPE_TLI
490 * inetresport creates a datagram socket and attempts to bind it to a
492 * returns: The bound socket, or -1 to indicate an error.
498 struct sockaddr_in addr;
501 /* Use internet address family */
502 addr.sin_family = AF_INET;
503 addr.sin_addr.s_addr = INADDR_ANY;
504 if ((fd = socket(AF_INET, ty, 0)) < 0)
507 for (alport = IPPORT_RESERVED - 1; alport > IPPORT_RESERVED / 2 + 1; alport--) {
508 addr.sin_port = htons((u_short) alport);
509 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) >= 0)
511 if (errno != EADDRINUSE) {
523 * Privsock() calls inetresport() to attempt to bind a socket to a secure
524 * port. If inetresport() fails, privsock returns a magic socket number which
525 * indicates to RPC that it should make its own socket.
526 * returns: A privileged socket # or RPC_ANYSOCK.
531 int sock = inetresport(ty);
535 /* Couldn't get a secure port, let RPC make an insecure one */
540 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
544 clnt_create_timeout(char *host, struct timeval *tvp)
547 struct sockaddr_in host_addr;
549 #ifndef HAVE_TRANSPORT_TYPE_TLI
551 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
553 if (setjmp(before_rpc)) {
555 fprintf(stderr, "%s: ", host);
556 clnt_perrno(RPC_TIMEDOUT);
557 fprintf(stderr, "\n");
562 signal(SIGALRM, create_timeout);
563 ualarm(tvp->tv_sec * 1000000 + tvp->tv_usec, 0);
566 * Get address of host
568 if ((hp = gethostbyname(host)) == 0 && !STREQ(host, localhost)) {
569 fprintf(stderr, "can't get address of %s\n", host);
572 memset(&host_addr, 0, sizeof host_addr);
573 host_addr.sin_family = AF_INET;
575 memmove((voidp) &host_addr.sin_addr, (voidp) hp->h_addr,
576 sizeof(host_addr.sin_addr));
578 /* fake "localhost" */
579 host_addr.sin_addr.s_addr = htonl(0x7f000001);
582 #ifdef HAVE_TRANSPORT_TYPE_TLI
583 /* try TCP first (in case return data is large), then UDP */
584 clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "tcp");
586 clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "udp");
587 #else /* not HAVE_TRANSPORT_TYPE_TLI */
589 clnt = clnttcp_create(&host_addr, MOUNTPROG, MOUNTVERS, &s, 0, 0);
591 /* XXX: do we need to close(s) ? */
592 s = privsock(SOCK_DGRAM);
593 clnt = clntudp_create(&host_addr, MOUNTPROG, MOUNTVERS, *tvp, &s);
595 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
600 clnt_pcreateerror(host);