2 * Copyright (c) 1997-2014 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/fixmount/fixmount.c
42 #endif /* HAVE_CONFIG_H */
45 #define CREATE_TIMEOUT 2 /* seconds */
46 #define CALL_TIMEOUT 5 /* seconds */
58 extern int fixmount_check_mount(char *host, struct in_addr hostaddr, char *path);
60 static char dir_path[NFS_MAXPATHLEN];
61 static char localhost[] = "localhost";
62 static char thishost[MAXHOSTNAMELEN + 1] = "";
63 static exports mntexports;
66 static jmp_buf before_rpc;
67 static mountlist mntdump;
68 static struct in_addr thisaddr;
69 static CLIENT *clnt_create_timeout(char *, struct timeval *);
71 RETSIGTYPE create_timeout(int);
72 int is_same_host(char *, char *, struct in_addr);
73 int main(int, char *[]);
74 int remove_all(CLIENT *, char *);
75 int remove_mount(CLIENT *, char *, mountlist, int);
76 void fix_rmtab(CLIENT *, char *, mountlist, int, int);
77 void print_dump(mountlist);
84 fprintf(stderr, "usage: fixmount [-adervAqf] [-h hostname] host ...\n");
90 * Check hostname against other name and its IP address
93 is_same_host(char *name1, char *name2, struct in_addr addr2)
95 if (strcasecmp(name1, name2) == 0) {
97 } else if (addr2.s_addr == INADDR_NONE) {
100 static char lasthost[MAXHOSTNAMELEN] = "";
101 static struct in_addr addr1;
105 * To save nameserver lookups, and because this function
106 * is typically called repeatedly on the same names,
107 * cache the last lookup result and reuse it if possible.
109 if (strcasecmp(name1, lasthost) == 0) {
110 return (addr1.s_addr == addr2.s_addr);
111 } else if (!(he = gethostbyname(name1))) {
114 xstrlcpy(lasthost, name1, sizeof(lasthost));
115 memcpy(&addr1, he->h_addr, sizeof(addr1));
116 return (addr1.s_addr == addr2.s_addr);
123 * Print the binary tree in inorder so that output is sorted.
126 print_dump(mountlist mp)
130 if (is_same_host(mp->ml_hostname, thishost, thisaddr)) {
133 printf("%s:%s\n", mp->ml_hostname, mp->ml_directory);
136 printf("%s\n", mp->ml_directory);
139 printf("%s\n", mp->ml_hostname);
144 print_dump(mp->ml_next);
149 * remove entry from remote rmtab
152 remove_mount(CLIENT *client, char *host, mountlist ml, int fixit)
154 enum clnt_stat estat;
156 char *pathp = dir_path;
158 xstrlcpy(dir_path, ml->ml_directory, sizeof(dir_path));
161 printf("%s: bogus mount %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
164 printf("%s: removing %s:%s\n", host, ml->ml_hostname, ml->ml_directory);
167 tv.tv_sec = CALL_TIMEOUT;
170 if ((estat = clnt_call(client,
172 (XDRPROC_T_TYPE) xdr_dirpath,
174 (XDRPROC_T_TYPE) xdr_void,
176 tv)) != RPC_SUCCESS) {
177 fprintf(stderr, "%s:%s MOUNTPROC_UMNT: ",
178 host, ml->ml_directory);
189 * fix mount list on remote host
192 fix_rmtab(CLIENT *client, char *host, mountlist mp, int fixit, int force)
196 struct in_addr hostaddr;
199 * Obtain remote address for comparisons
201 if ((he = gethostbyname(host))) {
202 memcpy(&hostaddr, he->h_addr, sizeof(hostaddr));
204 hostaddr.s_addr = INADDR_NONE;
207 for (p = mp; p; p = p->ml_next) {
208 if (is_same_host(p->ml_hostname, thishost, thisaddr)) {
209 if (force || !fixmount_check_mount(host, hostaddr, p->ml_directory))
210 remove_mount(client, host, p, fixit);
217 * remove all entries from remote rmtab
220 remove_all(CLIENT *client, char *host)
222 enum clnt_stat estat;
225 printf("%s: removing ALL\n", host);
228 tv.tv_sec = CALL_TIMEOUT;
231 if ((estat = clnt_call(client,
233 (XDRPROC_T_TYPE) xdr_void,
235 (XDRPROC_T_TYPE) xdr_void,
237 tv)) != RPC_SUCCESS) {
239 * RPC_SYSTEMERROR is returned even if all went well
241 if (estat != RPC_SYSTEMERROR) {
242 fprintf(stderr, "%s MOUNTPROC_UMNTALL: ", host);
254 * This command queries the NFS mount daemon for it's mount list and/or
255 * it's exports list and prints them out.
256 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
257 * for detailed information on the protocol.
260 main(int argc, char *argv[])
265 enum clnt_stat estat;
273 register int rpcs = 0;
276 while ((ch = getopt(argc, argv, "adervAqfh:")) != -1)
320 xstrlcpy(thishost, optarg, sizeof(thishost));
337 if (gethostname(thishost, sizeof(thishost)) < 0) {
338 perror("gethostname");
341 thishost[sizeof(thishost) - 1] = '\0';
344 * We need the hostname as it appears to the other side's
345 * mountd, so get our own hostname by reverse address
348 if (!(he = gethostbyname(thishost))) {
349 fprintf(stderr, "gethostbyname failed on %s\n",
353 memcpy(&thisaddr, he->h_addr, sizeof(thisaddr));
354 if (!(he = gethostbyaddr((char *) &thisaddr, sizeof(thisaddr),
356 fprintf(stderr, "gethostbyaddr failed on %s\n",
357 inet_ntoa(thisaddr));
360 xstrlcpy(thishost, he->h_name, sizeof(thishost));
362 thisaddr.s_addr = INADDR_NONE;
365 if (!(auth = authunix_create_default())) {
366 fprintf(stderr, "couldn't create authentication handle\n");
369 morethanone = (optind + 1 < argc);
371 for (; optind < argc; optind++) {
374 tv.tv_sec = CREATE_TIMEOUT;
377 if (!(client = clnt_create_timeout(host, &tv)))
380 client->cl_auth = auth;
381 tv.tv_sec = CALL_TIMEOUT;
384 if (rpcs & (DODUMP | DOREMOVE | DOVERIFY))
385 if ((estat = clnt_call(client,
387 (XDRPROC_T_TYPE) xdr_void,
389 (XDRPROC_T_TYPE) xdr_mountlist,
391 tv)) != RPC_SUCCESS) {
392 fprintf(stderr, "%s: MOUNTPROC_DUMP: ", host);
398 if (rpcs & DOEXPORTS)
399 if ((estat = clnt_call(client,
401 (XDRPROC_T_TYPE) xdr_void,
403 (XDRPROC_T_TYPE) xdr_exports,
404 (char *) &mntexports,
405 tv)) != RPC_SUCCESS) {
406 fprintf(stderr, "%s: MOUNTPROC_EXPORT: ", host);
413 /* Now just print out the results */
414 if ((rpcs & (DODUMP | DOEXPORTS)) &&
416 printf(">>> %s <<<\n", host);
424 if (rpcs & DOEXPORTS) {
427 printf("%-35s", exp->ex_dir);
428 grp = exp->ex_groups;
430 printf("Everyone\n");
433 printf("%s ", grp->gr_name);
443 fix_rmtab(client, host, mntdump, 0, force);
446 fix_rmtab(client, host, mntdump, 1, force);
449 remove_all(client, host);
453 (void) clnt_freeres(client,
454 (XDRPROC_T_TYPE) xdr_mountlist,
457 (void) clnt_freeres(client,
458 (XDRPROC_T_TYPE) xdr_exports,
459 (char *) &mntexports);
461 clnt_destroy(client);
464 return 0; /* should never reach here */
469 create_timeout(int sig)
471 signal(SIGALRM, SIG_DFL);
472 longjmp(before_rpc, 1);
476 #ifndef HAVE_TRANSPORT_TYPE_TLI
478 * inetresport creates a datagram socket and attempts to bind it to a
480 * returns: The bound socket, or -1 to indicate an error.
486 struct sockaddr_in addr;
489 memset(&addr, 0, sizeof(addr));
490 /* as per POSIX, sin_len need not be set (used internally by kernel) */
492 addr.sin_family = AF_INET; /* use internet address family */
493 addr.sin_addr.s_addr = INADDR_ANY;
494 if ((fd = socket(AF_INET, ty, 0)) < 0)
497 for (alport = IPPORT_RESERVED - 1; alport > IPPORT_RESERVED / 2 + 1; alport--) {
498 addr.sin_port = htons((u_short) alport);
499 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) >= 0)
501 if (errno != EADDRINUSE) {
513 * Privsock() calls inetresport() to attempt to bind a socket to a secure
514 * port. If inetresport() fails, privsock returns a magic socket number which
515 * indicates to RPC that it should make its own socket.
516 * returns: A privileged socket # or RPC_ANYSOCK.
521 int sock = inetresport(ty);
525 /* Couldn't get a secure port, let RPC make an insecure one */
530 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
534 clnt_create_timeout(char *host, struct timeval *tvp)
537 struct sockaddr_in host_addr;
539 #ifndef HAVE_TRANSPORT_TYPE_TLI
541 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
543 if (setjmp(before_rpc)) {
545 fprintf(stderr, "%s: ", host);
546 clnt_perrno(RPC_TIMEDOUT);
547 fprintf(stderr, "\n");
552 signal(SIGALRM, create_timeout);
553 ualarm(tvp->tv_sec * 1000000 + tvp->tv_usec, 0);
556 * Get address of host
558 if ((hp = gethostbyname(host)) == 0 && !STREQ(host, localhost)) {
559 fprintf(stderr, "can't get address of %s\n", host);
562 memset(&host_addr, 0, sizeof(host_addr));
563 /* as per POSIX, sin_len need not be set (used internally by kernel) */
564 host_addr.sin_family = AF_INET;
566 memmove((voidp) &host_addr.sin_addr, (voidp) hp->h_addr,
567 sizeof(host_addr.sin_addr));
569 /* fake "localhost" */
570 host_addr.sin_addr.s_addr = htonl(0x7f000001);
573 #ifdef HAVE_TRANSPORT_TYPE_TLI
574 /* try TCP first (in case return data is large), then UDP */
575 clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "tcp");
577 clnt = clnt_create(host, MOUNTPROG, MOUNTVERS, "udp");
578 #else /* not HAVE_TRANSPORT_TYPE_TLI */
580 clnt = clnttcp_create(&host_addr, MOUNTPROG, MOUNTVERS, &s, 0, 0);
582 /* XXX: do we need to close(s) ? */
583 s = privsock(SOCK_DGRAM);
584 clnt = clntudp_create(&host_addr, MOUNTPROG, MOUNTVERS, *tvp, &s);
586 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
591 clnt_pcreateerror(host);