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 $
43 * $DragonFly: src/contrib/amd/fixmount/fixmount.c,v 1.2 2003/06/17 04:23:57 dillon Exp $
49 #endif /* HAVE_CONFIG_H */
52 #define CREATE_TIMEOUT 2 /* seconds */
53 #define CALL_TIMEOUT 5 /* seconds */
56 # define INADDR_NONE 0xffffffff
57 #endif /* not INADDR_NONE */
69 extern int fixmount_check_mount(char *host, struct in_addr hostaddr, char *path);
71 static char dir_path[NFS_MAXPATHLEN];
72 static char localhost[] = "localhost";
73 static char thishost[MAXHOSTNAMELEN + 1] = "";
74 static exports mntexports;
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 *);
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);
95 fprintf(stderr, "usage: fixmount [-adervAqf] [-h hostname] host ...\n");
101 * Check hostname against other name and its IP address
104 is_same_host(char *name1, char *name2, struct in_addr addr2)
106 if (strcasecmp(name1, name2) == 0) {
108 } else if (addr2.s_addr == INADDR_NONE) {
111 static char lasthost[MAXHOSTNAMELEN] = "";
112 static struct in_addr addr1;
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.
120 if (strcasecmp(name1, lasthost) == 0) {
121 return (addr1.s_addr == addr2.s_addr);
122 } else if (!(he = gethostbyname(name1))) {
125 strncpy(lasthost, name1, sizeof(lasthost) - 1);
126 memcpy(&addr1, he->h_addr, sizeof(addr1));
127 return (addr1.s_addr == addr2.s_addr);
134 * Print the binary tree in inorder so that output is sorted.
137 print_dump(mountlist mp)
141 if (is_same_host(mp->ml_hostname, thishost, thisaddr)) {
144 printf("%s:%s\n", mp->ml_hostname, mp->ml_directory);
147 printf("%s\n", mp->ml_directory);
150 printf("%s\n", mp->ml_hostname);
155 print_dump(mp->ml_next);
160 * remove entry from remote rmtab
163 remove_mount(CLIENT *client, char *host, mountlist ml, int fixit)
165 enum clnt_stat estat;
167 char *pathp = dir_path;
169 strncpy(dir_path, ml->ml_directory, sizeof(dir_path));
172 printf("%s: bogus mount %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
175 printf("%s: removing %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
178 tv.tv_sec = CALL_TIMEOUT;
181 if ((estat = clnt_call(client,
183 (XDRPROC_T_TYPE) xdr_dirpath,
185 (XDRPROC_T_TYPE) xdr_void,
187 tv)) != RPC_SUCCESS) {
188 fprintf(stderr, "%s:%s MOUNTPROC_UMNT: ",
189 host, ml->ml_directory);
200 * fix mount list on remote host
203 fix_rmtab(CLIENT *client, char *host, mountlist mp, int fixit, int force)
207 struct in_addr hostaddr;
210 * Obtain remote address for comparisons
212 if ((he = gethostbyname(host))) {
213 memcpy(&hostaddr, he->h_addr, sizeof(hostaddr));
215 hostaddr.s_addr = INADDR_NONE;
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);
228 * remove all entries from remote rmtab
231 remove_all(CLIENT *client, char *host)
233 enum clnt_stat estat;
236 printf("%s: removing ALL\n", host);
239 tv.tv_sec = CALL_TIMEOUT;
242 if ((estat = clnt_call(client,
244 (XDRPROC_T_TYPE) xdr_void,
246 (XDRPROC_T_TYPE) xdr_void,
248 tv)) != RPC_SUCCESS) {
250 * RPC_SYSTEMERROR is returned even if all went well
252 if (estat != RPC_SYSTEMERROR) {
253 fprintf(stderr, "%s MOUNTPROC_UMNTALL: ", host);
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.
271 main(int argc, char *argv[])
276 enum clnt_stat estat;
284 register int rpcs = 0;
287 while ((ch = getopt(argc, argv, "adervAqfh:")) != -1)
331 strncpy(thishost, optarg, sizeof(thishost));
332 thishost[sizeof(thishost) - 1] = '\0';
349 if (gethostname(thishost, sizeof(thishost)) < 0) {
350 perror("gethostname");
353 thishost[sizeof(thishost) - 1] = '\0';
356 * We need the hostname as it appears to the other side's
357 * mountd, so get our own hostname by reverse address
360 if (!(he = gethostbyname(thishost))) {
361 fprintf(stderr, "gethostbyname failed on %s\n",
365 memcpy(&thisaddr, he->h_addr, sizeof(thisaddr));
366 if (!(he = gethostbyaddr((char *) &thisaddr, sizeof(thisaddr),
368 fprintf(stderr, "gethostbyaddr failed on %s\n",
369 inet_ntoa(thisaddr));
372 strncpy(thishost, he->h_name, sizeof(thishost));
373 thishost[sizeof(thishost) - 1] = '\0';
375 thisaddr.s_addr = INADDR_NONE;
378 if (!(auth = authunix_create_default())) {
379 fprintf(stderr, "couldn't create authentication handle\n");
382 morethanone = (optind + 1 < argc);
384 for (; optind < argc; optind++) {
387 tv.tv_sec = CREATE_TIMEOUT;
390 if (!(client = clnt_create_timeout(host, &tv)))
393 client->cl_auth = auth;
394 tv.tv_sec = CALL_TIMEOUT;
397 if (rpcs & (DODUMP | DOREMOVE | DOVERIFY))
398 if ((estat = clnt_call(client,
400 (XDRPROC_T_TYPE) xdr_void,
402 (XDRPROC_T_TYPE) xdr_mountlist,
404 tv)) != RPC_SUCCESS) {
405 fprintf(stderr, "%s: MOUNTPROC_DUMP: ", host);
411 if (rpcs & DOEXPORTS)
412 if ((estat = clnt_call(client,
414 (XDRPROC_T_TYPE) xdr_void,
416 (XDRPROC_T_TYPE) xdr_exports,
417 (char *) &mntexports,
418 tv)) != RPC_SUCCESS) {
419 fprintf(stderr, "%s: MOUNTPROC_EXPORT: ", host);
426 /* Now just print out the results */
427 if ((rpcs & (DODUMP | DOEXPORTS)) &&
429 printf(">>> %s <<<\n", host);
437 if (rpcs & DOEXPORTS) {
440 printf("%-35s", exp->ex_dir);
441 grp = exp->ex_groups;
443 printf("Everyone\n");
446 printf("%s ", grp->gr_name);
456 fix_rmtab(client, host, mntdump, 0, force);
459 fix_rmtab(client, host, mntdump, 1, force);
462 remove_all(client, host);
466 (void) clnt_freeres(client,
467 (XDRPROC_T_TYPE) xdr_mountlist,
470 (void) clnt_freeres(client,
471 (XDRPROC_T_TYPE) xdr_exports,
472 (char *) &mntexports);
474 clnt_destroy(client);
477 return 0; /* should never reach here */
482 create_timeout(int sig)
484 signal(SIGALRM, SIG_DFL);
485 longjmp(before_rpc, 1);
489 #ifndef HAVE_TRANSPORT_TYPE_TLI
491 * inetresport creates a datagram socket and attempts to bind it to a
493 * returns: The bound socket, or -1 to indicate an error.
499 struct sockaddr_in addr;
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)
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)
512 if (errno != EADDRINUSE) {
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.
532 int sock = inetresport(ty);
536 /* Couldn't get a secure port, let RPC make an insecure one */
541 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
545 clnt_create_timeout(char *host, struct timeval *tvp)
548 struct sockaddr_in host_addr;
550 #ifndef HAVE_TRANSPORT_TYPE_TLI
552 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
554 if (setjmp(before_rpc)) {
556 fprintf(stderr, "%s: ", host);
557 clnt_perrno(RPC_TIMEDOUT);
558 fprintf(stderr, "\n");
563 signal(SIGALRM, create_timeout);
564 ualarm(tvp->tv_sec * 1000000 + tvp->tv_usec, 0);
567 * Get address of host
569 if ((hp = gethostbyname(host)) == 0 && !STREQ(host, localhost)) {
570 fprintf(stderr, "can't get address of %s\n", host);
573 memset(&host_addr, 0, sizeof host_addr);
574 host_addr.sin_family = AF_INET;
576 memmove((voidp) &host_addr.sin_addr, (voidp) hp->h_addr,
577 sizeof(host_addr.sin_addr));
579 /* fake "localhost" */
580 host_addr.sin_addr.s_addr = htonl(0x7f000001);
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");
587 clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "udp");
588 #else /* not HAVE_TRANSPORT_TYPE_TLI */
590 clnt = clnttcp_create(&host_addr, MOUNTPROG, MOUNTVERS, &s, 0, 0);
592 /* XXX: do we need to close(s) ? */
593 s = privsock(SOCK_DGRAM);
594 clnt = clntudp_create(&host_addr, MOUNTPROG, MOUNTVERS, *tvp, &s);
596 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
601 clnt_pcreateerror(host);