xref: /netbsd-src/usr.bin/showmount/showmount.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: showmount.c,v 1.13 2004/08/06 16:10:54 mycroft Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993, 1995
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1995\n\
38 	The Regents of the University of California.  All rights reserved.\n");
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)showmount.c	8.3 (Berkeley) 3/29/95";
44 #endif
45 __RCSID("$NetBSD: showmount.c,v 1.13 2004/08/06 16:10:54 mycroft Exp $");
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 #include <sys/file.h>
50 #include <sys/socket.h>
51 
52 #include <netdb.h>
53 #include <rpc/rpc.h>
54 #include <rpc/pmap_clnt.h>
55 #include <rpc/pmap_prot.h>
56 #include <nfs/rpcv2.h>
57 
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 
63 /* Constant defs */
64 #define	ALL	1
65 #define	DIRS	2
66 
67 #define	DODUMP		0x1
68 #define	DOEXPORTS	0x2
69 
70 struct mountlist {
71 	struct mountlist *ml_left;
72 	struct mountlist *ml_right;
73 	char	ml_host[RPCMNT_NAMELEN+1];
74 	char	ml_dirp[RPCMNT_PATHLEN+1];
75 };
76 
77 struct grouplist {
78 	struct grouplist *gr_next;
79 	char	gr_name[RPCMNT_NAMELEN+1];
80 };
81 
82 struct exportslist {
83 	struct exportslist *ex_next;
84 	struct grouplist *ex_groups;
85 	char	ex_dirp[RPCMNT_PATHLEN+1];
86 };
87 
88 static struct mountlist *mntdump;
89 static struct exportslist *exports;
90 static int type = 0;
91 
92 int	main __P((int, char **));
93 void	print_dump __P((struct mountlist *));
94 void	usage __P((void));
95 int	xdr_mntdump __P((XDR *, struct mountlist **));
96 int	xdr_exports __P((XDR *, struct exportslist **));
97 int	tcp_callrpc __P((char *host,
98 		     int prognum, int versnum, int procnum,
99 		     xdrproc_t inproc, char *in,
100 		     xdrproc_t outproc, char *out));
101 
102 /*
103  * This command queries the NFS mount daemon for it's mount list and/or
104  * it's exports list and prints them out.
105  * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
106  * and the "Network File System Protocol XXX.."
107  * for detailed information on the protocol.
108  */
109 int
110 main(argc, argv)
111 	int argc;
112 	char **argv;
113 {
114 	struct exportslist *exp;
115 	struct grouplist *grp;
116 	int estat, rpcs = 0, mntvers = 1;
117 	char *host;
118 	int ch;
119 	int len;
120 
121 	while ((ch = getopt(argc, argv, "ade3")) != -1)
122 		switch((char)ch) {
123 		case 'a':
124 			if (type == 0) {
125 				type = ALL;
126 				rpcs |= DODUMP;
127 			} else
128 				usage();
129 			break;
130 		case 'd':
131 			if (type == 0) {
132 				type = DIRS;
133 				rpcs |= DODUMP;
134 			} else
135 				usage();
136 			break;
137 		case 'e':
138 			rpcs |= DOEXPORTS;
139 			break;
140 		case '3':
141 			mntvers = 3;
142 			break;
143 		case '?':
144 		default:
145 			usage();
146 		}
147 	argc -= optind;
148 	argv += optind;
149 
150 	if (argc > 0)
151 		host = *argv;
152 	else
153 		host = "localhost";
154 
155 	if (rpcs == 0)
156 		rpcs = DODUMP;
157 
158 	if (rpcs & DODUMP)
159 		if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
160 			RPCMNT_DUMP, xdr_void, (char *)0,
161 			xdr_mntdump, (char *)&mntdump)) != 0) {
162 			fprintf(stderr, "showmount: Can't do Mountdump rpc: ");
163 			clnt_perrno(estat);
164 			exit(1);
165 		}
166 	if (rpcs & DOEXPORTS)
167 		if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
168 			RPCMNT_EXPORT, xdr_void, (char *)0,
169 			xdr_exports, (char *)&exports)) != 0) {
170 			fprintf(stderr, "showmount: Can't do Exports rpc: ");
171 			clnt_perrno(estat);
172 			exit(1);
173 		}
174 
175 	/* Now just print out the results */
176 	if (rpcs & DODUMP) {
177 		switch (type) {
178 		case ALL:
179 			printf("All mount points on %s:\n", host);
180 			break;
181 		case DIRS:
182 			printf("Directories on %s:\n", host);
183 			break;
184 		default:
185 			printf("Hosts on %s:\n", host);
186 			break;
187 		};
188 		print_dump(mntdump);
189 	}
190 	if (rpcs & DOEXPORTS) {
191 		printf("Exports list on %s:\n", host);
192 		exp = exports;
193 		while (exp) {
194 			len = printf("%-35s", exp->ex_dirp);
195 			if (len > 35)
196 				printf("\t");
197 			grp = exp->ex_groups;
198 			if (grp == NULL) {
199 				printf("Everyone\n");
200 			} else {
201 				while (grp) {
202 					printf("%s ", grp->gr_name);
203 					grp = grp->gr_next;
204 				}
205 				printf("\n");
206 			}
207 			exp = exp->ex_next;
208 		}
209 	}
210 
211 	exit(0);
212 }
213 
214 /*
215  * tcp_callrpc has the same interface as callrpc, but tries to
216  * use tcp as transport method in order to handle large replies.
217  */
218 
219 int
220 tcp_callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
221 	char *host;
222 	int prognum;
223 	int versnum;
224 	int procnum;
225 	xdrproc_t inproc;
226 	char *in;
227 	xdrproc_t outproc;
228 	char *out;
229 {
230 	struct hostent *hp;
231 	struct sockaddr_in server_addr;
232 	CLIENT *client;
233 	int sock;
234 	struct timeval timeout;
235 	int rval;
236 
237 	hp = gethostbyname(host);
238 
239 	if (!hp)
240 		return ((int) RPC_UNKNOWNHOST);
241 
242 	memset(&server_addr,0,sizeof(server_addr));
243 	memcpy((char *) &server_addr.sin_addr,
244 	       hp->h_addr,
245 	       hp->h_length);
246 	server_addr.sin_len = sizeof(struct sockaddr_in);
247 	server_addr.sin_family =AF_INET;
248 	server_addr.sin_port = 0;
249 
250 	sock = RPC_ANYSOCK;
251 
252 	client = clnttcp_create(&server_addr,
253 				(u_long) prognum,
254 				(u_long) versnum, &sock, 0, 0);
255 	if (!client) {
256 		timeout.tv_sec = 5;
257 		timeout.tv_usec = 0;
258 		server_addr.sin_port = 0;
259 		sock = RPC_ANYSOCK;
260 		client = clntudp_create(&server_addr,
261 					(u_long) prognum,
262 					(u_long) versnum,
263 					timeout,
264 					&sock);
265 	}
266 	if (!client)
267 		return ((int) rpc_createerr.cf_stat);
268 
269 	timeout.tv_sec = 25;
270 	timeout.tv_usec = 0;
271 	rval = (int) clnt_call(client, procnum,
272 			       inproc, in,
273 			       outproc, out,
274 			       timeout);
275 	clnt_destroy(client);
276  	return rval;
277 }
278 
279 /*
280  * Xdr routine for retrieving the mount dump list
281  */
282 int
283 xdr_mntdump(xdrsp, mlp)
284 	XDR *xdrsp;
285 	struct mountlist **mlp;
286 {
287 	struct mountlist *mp, **otp, *tp;
288 	int bool, val, val2;
289 	char *strp;
290 
291 	otp = NULL;
292 	*mlp = (struct mountlist *)0;
293 	if (!xdr_bool(xdrsp, &bool))
294 		return (0);
295 	while (bool) {
296 		mp = (struct mountlist *)malloc(sizeof(struct mountlist));
297 		if (mp == NULL)
298 			return (0);
299 		mp->ml_left = mp->ml_right = (struct mountlist *)0;
300 		strp = mp->ml_host;
301 		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
302 			return (0);
303 		strp = mp->ml_dirp;
304 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
305 			return (0);
306 
307 		/*
308 		 * Build a binary tree on sorted order of either host or dirp.
309 		 * Drop any duplications.
310 		 */
311 		if (*mlp == NULL) {
312 			*mlp = mp;
313 		} else {
314 			tp = *mlp;
315 			while (tp) {
316 				val = strcmp(mp->ml_host, tp->ml_host);
317 				val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
318 				switch (type) {
319 				case ALL:
320 					if (val == 0) {
321 						if (val2 == 0) {
322 							free((caddr_t)mp);
323 							goto next;
324 						}
325 						val = val2;
326 					}
327 					break;
328 				case DIRS:
329 					if (val2 == 0) {
330 						free((caddr_t)mp);
331 						goto next;
332 					}
333 					val = val2;
334 					break;
335 				default:
336 					if (val == 0) {
337 						free((caddr_t)mp);
338 						goto next;
339 					}
340 					break;
341 				};
342 				if (val < 0) {
343 					otp = &tp->ml_left;
344 					tp = tp->ml_left;
345 				} else {
346 					otp = &tp->ml_right;
347 					tp = tp->ml_right;
348 				}
349 			}
350 			*otp = mp;
351 		}
352 next:
353 		if (!xdr_bool(xdrsp, &bool))
354 			return (0);
355 	}
356 	return (1);
357 }
358 
359 /*
360  * Xdr routine to retrieve exports list
361  */
362 int
363 xdr_exports(xdrsp, exp)
364 	XDR *xdrsp;
365 	struct exportslist **exp;
366 {
367 	struct exportslist *ep;
368 	struct grouplist *gp;
369 	int bool, grpbool;
370 	char *strp;
371 
372 	*exp = (struct exportslist *)0;
373 	if (!xdr_bool(xdrsp, &bool))
374 		return (0);
375 	while (bool) {
376 		ep = (struct exportslist *)malloc(sizeof(struct exportslist));
377 		if (ep == NULL)
378 			return (0);
379 		ep->ex_groups = (struct grouplist *)0;
380 		strp = ep->ex_dirp;
381 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
382 			return (0);
383 		if (!xdr_bool(xdrsp, &grpbool))
384 			return (0);
385 		while (grpbool) {
386 			gp = (struct grouplist *)malloc(sizeof(struct grouplist));
387 			if (gp == NULL)
388 				return (0);
389 			strp = gp->gr_name;
390 			if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
391 				return (0);
392 			gp->gr_next = ep->ex_groups;
393 			ep->ex_groups = gp;
394 			if (!xdr_bool(xdrsp, &grpbool))
395 				return (0);
396 		}
397 		ep->ex_next = *exp;
398 		*exp = ep;
399 		if (!xdr_bool(xdrsp, &bool))
400 			return (0);
401 	}
402 	return (1);
403 }
404 
405 void
406 usage()
407 {
408 
409 	fprintf(stderr, "usage: showmount [-ade3] host\n");
410 	exit(1);
411 }
412 
413 /*
414  * Print the binary tree in inorder so that output is sorted.
415  */
416 void
417 print_dump(mp)
418 	struct mountlist *mp;
419 {
420 
421 	if (mp == NULL)
422 		return;
423 	if (mp->ml_left)
424 		print_dump(mp->ml_left);
425 	switch (type) {
426 	case ALL:
427 		printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
428 		break;
429 	case DIRS:
430 		printf("%s\n", mp->ml_dirp);
431 		break;
432 	default:
433 		printf("%s\n", mp->ml_host);
434 		break;
435 	};
436 	if (mp->ml_right)
437 		print_dump(mp->ml_right);
438 }
439