4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1990 Jan-Simon Pendry
6 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1990 The Regents of the University of California.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgment:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * File: am-utils/fixmount/fixmount.c
48 #endif /* HAVE_CONFIG_H */
51 #define CREATE_TIMEOUT 2 /* seconds */
52 #define CALL_TIMEOUT 5 /* seconds */
64 extern int fixmount_check_mount(char *host
, struct in_addr hostaddr
, char *path
);
66 static char dir_path
[NFS_MAXPATHLEN
];
67 static char localhost
[] = "localhost";
68 static char thishost
[MAXHOSTNAMELEN
+ 1] = "";
69 static exports mntexports
;
72 static jmp_buf before_rpc
;
73 static mountlist mntdump
;
74 static struct in_addr thisaddr
;
75 static CLIENT
*clnt_create_timeout(char *, struct timeval
*);
77 RETSIGTYPE
create_timeout(int);
78 int is_same_host(char *, char *, struct in_addr
);
79 int main(int, char *[]);
80 int remove_all(CLIENT
*, char *);
81 int remove_mount(CLIENT
*, char *, mountlist
, int);
82 void fix_rmtab(CLIENT
*, char *, mountlist
, int, int);
83 void print_dump(mountlist
);
90 fprintf(stderr
, "usage: fixmount [-adervAqf] [-h hostname] host ...\n");
96 * Check hostname against other name and its IP address
99 is_same_host(char *name1
, char *name2
, struct in_addr addr2
)
101 if (strcasecmp(name1
, name2
) == 0) {
103 } else if (addr2
.s_addr
== INADDR_NONE
) {
106 static char lasthost
[MAXHOSTNAMELEN
] = "";
107 static struct in_addr addr1
;
111 * To save nameserver lookups, and because this function
112 * is typically called repeatedly on the same names,
113 * cache the last lookup result and reuse it if possible.
115 if (strcasecmp(name1
, lasthost
) == 0) {
116 return (addr1
.s_addr
== addr2
.s_addr
);
117 } else if (!(he
= gethostbyname(name1
))) {
120 xstrlcpy(lasthost
, name1
, sizeof(lasthost
));
121 memcpy(&addr1
, he
->h_addr
, sizeof(addr1
));
122 return (addr1
.s_addr
== addr2
.s_addr
);
129 * Print the binary tree in inorder so that output is sorted.
132 print_dump(mountlist mp
)
136 if (is_same_host(mp
->ml_hostname
, thishost
, thisaddr
)) {
139 printf("%s:%s\n", mp
->ml_hostname
, mp
->ml_directory
);
142 printf("%s\n", mp
->ml_directory
);
145 printf("%s\n", mp
->ml_hostname
);
150 print_dump(mp
->ml_next
);
155 * remove entry from remote rmtab
158 remove_mount(CLIENT
*client
, char *host
, mountlist ml
, int fixit
)
160 enum clnt_stat estat
;
162 char *pathp
= dir_path
;
164 xstrlcpy(dir_path
, ml
->ml_directory
, sizeof(dir_path
));
167 printf("%s: bogus mount %s:%s\n", host
, ml
->ml_hostname
, ml
->ml_directory
);
170 printf("%s: removing %s:%s\n", host
, ml
->ml_hostname
, ml
->ml_directory
);
173 tv
.tv_sec
= CALL_TIMEOUT
;
176 if ((estat
= clnt_call(client
,
178 (XDRPROC_T_TYPE
) xdr_dirpath
,
180 (XDRPROC_T_TYPE
) xdr_void
,
182 tv
)) != RPC_SUCCESS
) {
183 fprintf(stderr
, "%s:%s MOUNTPROC_UMNT: ",
184 host
, ml
->ml_directory
);
195 * fix mount list on remote host
198 fix_rmtab(CLIENT
*client
, char *host
, mountlist mp
, int fixit
, int force
)
202 struct in_addr hostaddr
;
205 * Obtain remote address for comparisons
207 if ((he
= gethostbyname(host
))) {
208 memcpy(&hostaddr
, he
->h_addr
, sizeof(hostaddr
));
210 hostaddr
.s_addr
= INADDR_NONE
;
213 for (p
= mp
; p
; p
= p
->ml_next
) {
214 if (is_same_host(p
->ml_hostname
, thishost
, thisaddr
)) {
215 if (force
|| !fixmount_check_mount(host
, hostaddr
, p
->ml_directory
))
216 remove_mount(client
, host
, p
, fixit
);
223 * remove all entries from remote rmtab
226 remove_all(CLIENT
*client
, char *host
)
228 enum clnt_stat estat
;
231 printf("%s: removing ALL\n", host
);
234 tv
.tv_sec
= CALL_TIMEOUT
;
237 if ((estat
= clnt_call(client
,
239 (XDRPROC_T_TYPE
) xdr_void
,
241 (XDRPROC_T_TYPE
) xdr_void
,
243 tv
)) != RPC_SUCCESS
) {
245 * RPC_SYSTEMERROR is returned even if all went well
247 if (estat
!= RPC_SYSTEMERROR
) {
248 fprintf(stderr
, "%s MOUNTPROC_UMNTALL: ", host
);
260 * This command queries the NFS mount daemon for it's mount list and/or
261 * it's exports list and prints them out.
262 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
263 * for detailed information on the protocol.
266 main(int argc
, char *argv
[])
271 enum clnt_stat estat
;
279 register int rpcs
= 0;
282 while ((ch
= getopt(argc
, argv
, "adervAqfh:")) != -1)
326 xstrlcpy(thishost
, optarg
, sizeof(thishost
));
343 if (gethostname(thishost
, sizeof(thishost
)) < 0) {
344 perror("gethostname");
347 thishost
[sizeof(thishost
) - 1] = '\0';
350 * We need the hostname as it appears to the other side's
351 * mountd, so get our own hostname by reverse address
354 if (!(he
= gethostbyname(thishost
))) {
355 fprintf(stderr
, "gethostbyname failed on %s\n",
359 memcpy(&thisaddr
, he
->h_addr
, sizeof(thisaddr
));
360 if (!(he
= gethostbyaddr((char *) &thisaddr
, sizeof(thisaddr
),
362 fprintf(stderr
, "gethostbyaddr failed on %s\n",
363 inet_ntoa(thisaddr
));
366 xstrlcpy(thishost
, he
->h_name
, sizeof(thishost
));
368 thisaddr
.s_addr
= INADDR_NONE
;
371 if (!(auth
= authunix_create_default())) {
372 fprintf(stderr
, "couldn't create authentication handle\n");
375 morethanone
= (optind
+ 1 < argc
);
377 for (; optind
< argc
; optind
++) {
380 tv
.tv_sec
= CREATE_TIMEOUT
;
383 if (!(client
= clnt_create_timeout(host
, &tv
)))
386 client
->cl_auth
= auth
;
387 tv
.tv_sec
= CALL_TIMEOUT
;
390 if (rpcs
& (DODUMP
| DOREMOVE
| DOVERIFY
))
391 if ((estat
= clnt_call(client
,
393 (XDRPROC_T_TYPE
) xdr_void
,
395 (XDRPROC_T_TYPE
) xdr_mountlist
,
397 tv
)) != RPC_SUCCESS
) {
398 fprintf(stderr
, "%s: MOUNTPROC_DUMP: ", host
);
404 if (rpcs
& DOEXPORTS
)
405 if ((estat
= clnt_call(client
,
407 (XDRPROC_T_TYPE
) xdr_void
,
409 (XDRPROC_T_TYPE
) xdr_exports
,
410 (char *) &mntexports
,
411 tv
)) != RPC_SUCCESS
) {
412 fprintf(stderr
, "%s: MOUNTPROC_EXPORT: ", host
);
419 /* Now just print out the results */
420 if ((rpcs
& (DODUMP
| DOEXPORTS
)) &&
422 printf(">>> %s <<<\n", host
);
430 if (rpcs
& DOEXPORTS
) {
433 printf("%-35s", exp
->ex_dir
);
434 grp
= exp
->ex_groups
;
436 printf("Everyone\n");
439 printf("%s ", grp
->gr_name
);
449 fix_rmtab(client
, host
, mntdump
, 0, force
);
452 fix_rmtab(client
, host
, mntdump
, 1, force
);
455 remove_all(client
, host
);
459 (void) clnt_freeres(client
,
460 (XDRPROC_T_TYPE
) xdr_mountlist
,
463 (void) clnt_freeres(client
,
464 (XDRPROC_T_TYPE
) xdr_exports
,
465 (char *) &mntexports
);
467 clnt_destroy(client
);
470 return 0; /* should never reach here */
475 create_timeout(int sig
)
477 signal(SIGALRM
, SIG_DFL
);
478 longjmp(before_rpc
, 1);
482 #ifndef HAVE_TRANSPORT_TYPE_TLI
484 * inetresport creates a datagram socket and attempts to bind it to a
486 * returns: The bound socket, or -1 to indicate an error.
492 struct sockaddr_in addr
;
495 memset(&addr
, 0, sizeof(addr
));
496 /* as per POSIX, sin_len need not be set (used internally by kernel) */
498 addr
.sin_family
= AF_INET
; /* use internet address family */
499 addr
.sin_addr
.s_addr
= INADDR_ANY
;
500 if ((fd
= socket(AF_INET
, ty
, 0)) < 0)
503 for (alport
= IPPORT_RESERVED
- 1; alport
> IPPORT_RESERVED
/ 2 + 1; alport
--) {
504 addr
.sin_port
= htons((u_short
) alport
);
505 if (bind(fd
, (struct sockaddr
*) &addr
, sizeof(addr
)) >= 0)
507 if (errno
!= EADDRINUSE
) {
519 * Privsock() calls inetresport() to attempt to bind a socket to a secure
520 * port. If inetresport() fails, privsock returns a magic socket number which
521 * indicates to RPC that it should make its own socket.
522 * returns: A privileged socket # or RPC_ANYSOCK.
527 int sock
= inetresport(ty
);
531 /* Couldn't get a secure port, let RPC make an insecure one */
536 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
540 clnt_create_timeout(char *host
, struct timeval
*tvp
)
543 struct sockaddr_in host_addr
;
545 #ifndef HAVE_TRANSPORT_TYPE_TLI
547 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
549 if (setjmp(before_rpc
)) {
551 fprintf(stderr
, "%s: ", host
);
552 clnt_perrno(RPC_TIMEDOUT
);
553 fprintf(stderr
, "\n");
558 signal(SIGALRM
, create_timeout
);
559 ualarm(tvp
->tv_sec
* 1000000 + tvp
->tv_usec
, 0);
562 * Get address of host
564 if ((hp
= gethostbyname(host
)) == 0 && !STREQ(host
, localhost
)) {
565 fprintf(stderr
, "can't get address of %s\n", host
);
568 memset(&host_addr
, 0, sizeof(host_addr
));
569 /* as per POSIX, sin_len need not be set (used internally by kernel) */
570 host_addr
.sin_family
= AF_INET
;
572 memmove((voidp
) &host_addr
.sin_addr
, (voidp
) hp
->h_addr
,
573 sizeof(host_addr
.sin_addr
));
575 /* fake "localhost" */
576 host_addr
.sin_addr
.s_addr
= htonl(0x7f000001);
579 #ifdef HAVE_TRANSPORT_TYPE_TLI
580 /* try TCP first (in case return data is large), then UDP */
581 clnt
= clnt_create(host
, MOUNTPROG
, MOUNTVERS
, "tcp");
583 clnt
= clnt_create(host
, MOUNTPROG
, MOUNTVERS
, "udp");
584 #else /* not HAVE_TRANSPORT_TYPE_TLI */
586 clnt
= clnttcp_create(&host_addr
, MOUNTPROG
, MOUNTVERS
, &s
, 0, 0);
588 /* XXX: do we need to close(s) ? */
589 s
= privsock(SOCK_DGRAM
);
590 clnt
= clntudp_create(&host_addr
, MOUNTPROG
, MOUNTVERS
, *tvp
, &s
);
592 #endif /* not HAVE_TRANSPORT_TYPE_TLI */
597 clnt_pcreateerror(host
);