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