2 * Copyright (c) 1997-2006 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
40 * File: am-utils/fixmount/fixmount.c
46 #endif /* HAVE_CONFIG_H */
49 #define CREATE_TIMEOUT 2 /* seconds */
50 #define CALL_TIMEOUT 5 /* seconds */
62 extern int fixmount_check_mount(char *host, struct in_addr hostaddr, char *path);
64 static char dir_path[NFS_MAXPATHLEN];
65 static char localhost[] = "localhost";
66 static char thishost[MAXHOSTNAMELEN + 1] = "";
67 static exports mntexports;
70 static jmp_buf before_rpc;
71 static mountlist mntdump;
72 static struct in_addr thisaddr;
73 static CLIENT *clnt_create_timeout(char *, struct timeval *);
75 RETSIGTYPE create_timeout(int);
76 int is_same_host(char *, char *, struct in_addr);
77 int main(int, char *[]);
78 int remove_all(CLIENT *, char *);
79 int remove_mount(CLIENT *, char *, mountlist, int);
80 void fix_rmtab(CLIENT *, char *, mountlist, int, int);
81 void print_dump(mountlist);
88 fprintf(stderr, "usage: fixmount [-adervAqf] [-h hostname] host ...\n");
94 * Check hostname against other name and its IP address
97 is_same_host(char *name1, char *name2, struct in_addr addr2)
99 if (strcasecmp(name1, name2) == 0) {
101 } else if (addr2.s_addr == INADDR_NONE) {
104 static char lasthost[MAXHOSTNAMELEN] = "";
105 static struct in_addr addr1;
109 * To save nameserver lookups, and because this function
110 * is typically called repeatedly on the same names,
111 * cache the last lookup result and reuse it if possible.
113 if (strcasecmp(name1, lasthost) == 0) {
114 return (addr1.s_addr == addr2.s_addr);
115 } else if (!(he = gethostbyname(name1))) {
118 xstrlcpy(lasthost, name1, MAXHOSTNAMELEN);
119 memcpy(&addr1, he->h_addr, sizeof(addr1));
120 return (addr1.s_addr == addr2.s_addr);
127 * Print the binary tree in inorder so that output is sorted.
130 print_dump(mountlist mp)
134 if (is_same_host(mp->ml_hostname, thishost, thisaddr)) {
137 printf("%s:%s\n", mp->ml_hostname, mp->ml_directory);
140 printf("%s\n", mp->ml_directory);
143 printf("%s\n", mp->ml_hostname);
148 print_dump(mp->ml_next);
153 * remove entry from remote rmtab
156 remove_mount(CLIENT *client, char *host, mountlist ml, int fixit)
158 enum clnt_stat estat;
160 char *pathp = dir_path;
162 xstrlcpy(dir_path, ml->ml_directory, sizeof(dir_path));
165 printf("%s: bogus mount %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
168 printf("%s: removing %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
171 tv.tv_sec = CALL_TIMEOUT;
174 if ((estat = clnt_call(client,
176 (XDRPROC_T_TYPE) xdr_dirpath,
178 (XDRPROC_T_TYPE) xdr_void,
180 tv)) != RPC_SUCCESS) {
181 fprintf(stderr, "%s:%s MOUNTPROC_UMNT: ",
182 host, ml->ml_directory);
193 * fix mount list on remote host
196 fix_rmtab(CLIENT *client, char *host, mountlist mp, int fixit, int force)
200 struct in_addr hostaddr;
203 * Obtain remote address for comparisons
205 if ((he = gethostbyname(host))) {
206 memcpy(&hostaddr, he->h_addr, sizeof(hostaddr));
208 hostaddr.s_addr = INADDR_NONE;
211 for (p = mp; p; p = p->ml_next) {
212 if (is_same_host(p->ml_hostname, thishost, thisaddr)) {
213 if (force || !fixmount_check_mount(host, hostaddr, p->ml_directory))
214 remove_mount(client, host, p, fixit);
221 * remove all entries from remote rmtab
224 remove_all(CLIENT *client, char *host)
226 enum clnt_stat estat;
229 printf("%s: removing ALL\n", host);
232 tv.tv_sec = CALL_TIMEOUT;
235 if ((estat = clnt_call(client,
237 (XDRPROC_T_TYPE) xdr_void,
239 (XDRPROC_T_TYPE) xdr_void,
241 tv)) != RPC_SUCCESS) {
243 * RPC_SYSTEMERROR is returned even if all went well
245 if (estat != RPC_SYSTEMERROR) {
246 fprintf(stderr, "%s MOUNTPROC_UMNTALL: ", host);
258 * This command queries the NFS mount daemon for it's mount list and/or
259 * it's exports list and prints them out.
260 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
261 * for detailed information on the protocol.
264 main(int argc, char *argv[])
269 enum clnt_stat estat;
277 register int rpcs = 0;
280 while ((ch = getopt(argc, argv, "adervAqfh:")) != -1)
324 xstrlcpy(thishost, optarg, sizeof(thishost));
341 if (gethostname(thishost, sizeof(thishost)) < 0) {
342 perror("gethostname");
345 thishost[sizeof(thishost) - 1] = '\0';
348 * We need the hostname as it appears to the other side's
349 * mountd, so get our own hostname by reverse address
352 if (!(he = gethostbyname(thishost))) {
353 fprintf(stderr, "gethostbyname failed on %s\n",
357 memcpy(&thisaddr, he->h_addr, sizeof(thisaddr));
358 if (!(he = gethostbyaddr((char *) &thisaddr, sizeof(thisaddr),
360 fprintf(stderr, "gethostbyaddr failed on %s\n",
361 inet_ntoa(thisaddr));
364 xstrlcpy(thishost, he->h_name, sizeof(thishost));
366 thisaddr.s_addr = INADDR_NONE;
369 if (!(auth = authunix_create_default())) {
370 fprintf(stderr, "couldn't create authentication handle\n");
373 morethanone = (optind + 1 < argc);
375 for (; optind < argc; optind++) {
378 tv.tv_sec = CREATE_TIMEOUT;
381 if (!(client = clnt_create_timeout(host, &tv)))
384 client->cl_auth = auth;
385 tv.tv_sec = CALL_TIMEOUT;
388 if (rpcs & (DODUMP | DOREMOVE | DOVERIFY))
389 if ((estat = clnt_call(client,
391 (XDRPROC_T_TYPE) xdr_void,
393 (XDRPROC_T_TYPE) xdr_mountlist,
395 tv)) != RPC_SUCCESS) {
396 fprintf(stderr, "%s: MOUNTPROC_DUMP: ", host);
402 if (rpcs & DOEXPORTS)
403 if ((estat = clnt_call(client,
405 (XDRPROC_T_TYPE) xdr_void,
407 (XDRPROC_T_TYPE) xdr_exports,
408 (char *) &mntexports,
409 tv)) != RPC_SUCCESS) {
410 fprintf(stderr, "%s: MOUNTPROC_EXPORT: ", host);
417 /* Now just print out the results */
418 if ((rpcs & (DODUMP | DOEXPORTS)) &&
420 printf(">>> %s <<<\n", host);
428 if (rpcs & DOEXPORTS) {
431 printf("%-35s", exp->ex_dir);
432 grp = exp->ex_groups;
434 printf("Everyone\n");
437 printf("%s ", grp->gr_name);
447 fix_rmtab(client, host, mntdump, 0, force);
450 fix_rmtab(client, host, mntdump, 1, force);
453 remove_all(client, host);
457 (void) clnt_freeres(client,
458 (XDRPROC_T_TYPE) xdr_mountlist,
461 (void) clnt_freeres(client,
462 (XDRPROC_T_TYPE) xdr_exports,
463 (char *) &mntexports);
465 clnt_destroy(client);
468 return 0; /* should never reach here */
473 create_timeout(int sig)
475 signal(SIGALRM, SIG_DFL);
476 longjmp(before_rpc, 1);
480 #ifndef HAVE_TRANSPORT_TYPE_TLI
482 * inetresport creates a datagram socket and attempts to bind it to a
484 * returns: The bound socket, or -1 to indicate an error.
490 struct sockaddr_in addr;
493 memset(&addr, 0, sizeof(addr));
494 /* as per POSIX, sin_len need not be set (used internally by kernel) */
496 addr.sin_family = AF_INET; /* use internet address family */
497 addr.sin_addr.s_addr = INADDR_ANY;
498 if ((fd = socket(AF_INET, ty, 0)) < 0)
501 for (alport = IPPORT_RESERVED - 1; alport > IPPORT_RESERVED / 2 + 1; alport--) {
502 addr.sin_port = htons((u_short) alport);
503 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) >= 0)
505 if (errno != EADDRINUSE) {
517 * Privsock() calls inetresport() to attempt to bind a socket to a secure
518 * port. If inetresport() fails, privsock returns a magic socket number which
519 * indicates to RPC that it should make its own socket.
520 * returns: A privileged socket # or RPC_ANYSOCK.
525 int sock = inetresport(ty);
529 /* Couldn't get a secure port, let RPC make an insecure one */
534 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
538 clnt_create_timeout(char *host, struct timeval *tvp)
541 struct sockaddr_in host_addr;
543 #ifndef HAVE_TRANSPORT_TYPE_TLI
545 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
547 if (setjmp(before_rpc)) {
549 fprintf(stderr, "%s: ", host);
550 clnt_perrno(RPC_TIMEDOUT);
551 fprintf(stderr, "\n");
556 signal(SIGALRM, create_timeout);
557 ualarm(tvp->tv_sec * 1000000 + tvp->tv_usec, 0);
560 * Get address of host
562 if ((hp = gethostbyname(host)) == 0 && !STREQ(host, localhost)) {
563 fprintf(stderr, "can't get address of %s\n", host);
566 memset(&host_addr, 0, sizeof(host_addr));
567 /* as per POSIX, sin_len need not be set (used internally by kernel) */
568 host_addr.sin_family = AF_INET;
570 memmove((voidp) &host_addr.sin_addr, (voidp) hp->h_addr,
571 sizeof(host_addr.sin_addr));
573 /* fake "localhost" */
574 host_addr.sin_addr.s_addr = htonl(0x7f000001);
577 #ifdef HAVE_TRANSPORT_TYPE_TLI
578 /* try TCP first (in case return data is large), then UDP */
579 clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "tcp");
581 clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "udp");
582 #else /* not HAVE_TRANSPORT_TYPE_TLI */
584 clnt = clnttcp_create(&host_addr, MOUNTPROG, MOUNTVERS, &s, 0, 0);
586 /* XXX: do we need to close(s) ? */
587 s = privsock(SOCK_DGRAM);
588 clnt = clntudp_create(&host_addr, MOUNTPROG, MOUNTVERS, *tvp, &s);
590 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
595 clnt_pcreateerror(host);