showmount.c revision 78130
1259412Sluigi/*
2260368Sluigi * Copyright (c) 1989, 1993, 1995
3259412Sluigi *	The Regents of the University of California.  All rights reserved.
4259412Sluigi *
5259412Sluigi * This code is derived from software contributed to Berkeley by
6259412Sluigi * Rick Macklem at The University of Guelph.
7259412Sluigi *
8259412Sluigi * Redistribution and use in source and binary forms, with or without
9259412Sluigi * modification, are permitted provided that the following conditions
10259412Sluigi * are met:
11259412Sluigi * 1. Redistributions of source code must retain the above copyright
12259412Sluigi *    notice, this list of conditions and the following disclaimer.
13259412Sluigi * 2. Redistributions in binary form must reproduce the above copyright
14259412Sluigi *    notice, this list of conditions and the following disclaimer in the
15259412Sluigi *    documentation and/or other materials provided with the distribution.
16259412Sluigi * 3. All advertising materials mentioning features or use of this software
17259412Sluigi *    must display the following acknowledgement:
18259412Sluigi *	This product includes software developed by the University of
19259412Sluigi *	California, Berkeley and its contributors.
20259412Sluigi * 4. Neither the name of the University nor the names of its contributors
21259412Sluigi *    may be used to endorse or promote products derived from this software
22259412Sluigi *    without specific prior written permission.
23259412Sluigi *
24259412Sluigi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25259412Sluigi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26259412Sluigi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27259412Sluigi * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28259412Sluigi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29259412Sluigi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30259412Sluigi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31259412Sluigi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32259412Sluigi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33259412Sluigi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34259412Sluigi * SUCH DAMAGE.
35259412Sluigi */
36259412Sluigi
37259412Sluigi#ifndef lint
38259412Sluigistatic const char copyright[] =
39259412Sluigi"@(#) Copyright (c) 1989, 1993, 1995\n\
40259412Sluigi	The Regents of the University of California.  All rights reserved.\n";
41259412Sluigi#endif /* not lint */
42259412Sluigi
43259412Sluigi#ifndef lint
44259412Sluigi#if 0
45259412Sluigistatic char sccsid[] = "@(#)showmount.c	8.3 (Berkeley) 3/29/95";
46259412Sluigi#endif
47259412Sluigistatic const char rcsid[] =
48259412Sluigi  "$FreeBSD: head/usr.bin/showmount/showmount.c 78130 2001-06-12 03:44:35Z dd $";
49259412Sluigi#endif /* not lint */
50260368Sluigi
51259412Sluigi#include <sys/types.h>
52259412Sluigi#include <sys/queue.h>
53259412Sluigi#include <sys/file.h>
54259412Sluigi#include <sys/socket.h>
55259412Sluigi#include <sys/socketvar.h>
56259412Sluigi
57260368Sluigi#include <err.h>
58259412Sluigi#include <netdb.h>
59259412Sluigi#include <rpc/rpc.h>
60259412Sluigi#include <rpc/pmap_clnt.h>
61259412Sluigi#include <rpc/pmap_prot.h>
62259412Sluigi#include <nfs/rpcv2.h>
63260368Sluigi
64259412Sluigi#include <stdio.h>
65259412Sluigi#include <stdlib.h>
66259412Sluigi#include <string.h>
67259412Sluigi#include <unistd.h>
68259412Sluigi
69259412Sluigi/* Constant defs */
70259412Sluigi#define	ALL	1
71259412Sluigi#define	DIRS	2
72259412Sluigi
73259412Sluigi#define	DODUMP		0x1
74259412Sluigi#define	DOEXPORTS	0x2
75259412Sluigi
76260368Sluigistruct mountlist {
77259412Sluigi	struct mountlist *ml_left;
78259412Sluigi	struct mountlist *ml_right;
79267177Sluigi	char	ml_host[RPCMNT_NAMELEN+1];
80259412Sluigi	char	ml_dirp[RPCMNT_PATHLEN+1];
81267177Sluigi};
82259412Sluigi
83259412Sluigistruct grouplist {
84260368Sluigi	struct grouplist *gr_next;
85259412Sluigi	char	gr_name[RPCMNT_NAMELEN+1];
86259412Sluigi};
87259412Sluigi
88259412Sluigistruct exportslist {
89259412Sluigi	struct exportslist *ex_next;
90260368Sluigi	struct grouplist *ex_groups;
91259412Sluigi	char	ex_dirp[RPCMNT_PATHLEN+1];
92259412Sluigi};
93259412Sluigi
94259412Sluigistatic struct mountlist *mntdump;
95259412Sluigistatic struct exportslist *exports;
96259412Sluigistatic int type = 0;
97259412Sluigi
98259412Sluigivoid print_dump __P((struct mountlist *));
99259412Sluigistatic void usage __P((void));
100259412Sluigiint xdr_mntdump __P((XDR *, struct mountlist **));
101259412Sluigiint xdr_exports __P((XDR *, struct exportslist **));
102259412Sluigiint tcp_callrpc __P((char *host,
103259412Sluigi		     int prognum, int versnum, int procnum,
104259412Sluigi		     xdrproc_t inproc, char *in,
105259412Sluigi		     xdrproc_t outproc, char *out));
106259412Sluigi
107259412Sluigi/*
108260368Sluigi * This command queries the NFS mount daemon for it's mount list and/or
109259412Sluigi * it's exports list and prints them out.
110259412Sluigi * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
111259412Sluigi * and the "Network File System Protocol XXX.."
112259412Sluigi * for detailed information on the protocol.
113267177Sluigi */
114259412Sluigiint
115267177Sluigimain(argc, argv)
116259412Sluigi	int argc;
117259412Sluigi	char **argv;
118259412Sluigi{
119259412Sluigi	register struct exportslist *exp;
120260368Sluigi	register struct grouplist *grp;
121259412Sluigi	register int rpcs = 0, mntvers = 1;
122259412Sluigi	char ch;
123259412Sluigi	char *host;
124259412Sluigi	int estat;
125259412Sluigi
126260368Sluigi	while ((ch = getopt(argc, argv, "ade3")) != -1)
127259412Sluigi		switch((char)ch) {
128259412Sluigi		case 'a':
129259412Sluigi			if (type == 0) {
130259412Sluigi				type = ALL;
131259412Sluigi				rpcs |= DODUMP;
132259412Sluigi			} else
133259412Sluigi				usage();
134259412Sluigi			break;
135259412Sluigi		case 'd':
136259412Sluigi			if (type == 0) {
137259412Sluigi				type = DIRS;
138259412Sluigi				rpcs |= DODUMP;
139259412Sluigi			} else
140259412Sluigi				usage();
141259412Sluigi			break;
142260368Sluigi		case 'e':
143259412Sluigi			rpcs |= DOEXPORTS;
144259412Sluigi			break;
145259412Sluigi		case '3':
146259412Sluigi			mntvers = 3;
147259412Sluigi			break;
148260368Sluigi		case '?':
149259412Sluigi		default:
150259412Sluigi			usage();
151259412Sluigi		}
152259412Sluigi	argc -= optind;
153259412Sluigi	argv += optind;
154260368Sluigi
155259412Sluigi	if (argc > 0)
156259412Sluigi		host = *argv;
157259412Sluigi	else
158259412Sluigi		host = "localhost";
159259412Sluigi
160259412Sluigi	if (rpcs == 0)
161259412Sluigi		rpcs = DODUMP;
162259412Sluigi
163259412Sluigi	if (rpcs & DODUMP)
164		if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
165			RPCMNT_DUMP, xdr_void, (char *)0,
166			xdr_mntdump, (char *)&mntdump)) != 0) {
167			clnt_perrno(estat);
168			errx(1, "can't do mountdump rpc");
169		}
170	if (rpcs & DOEXPORTS)
171		if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
172			RPCMNT_EXPORT, xdr_void, (char *)0,
173			xdr_exports, (char *)&exports)) != 0) {
174			clnt_perrno(estat);
175			errx(1, "can't do exports rpc");
176		}
177
178	/* Now just print out the results */
179	if (rpcs & DODUMP) {
180		switch (type) {
181		case ALL:
182			printf("All mount points on %s:\n", host);
183			break;
184		case DIRS:
185			printf("Directories on %s:\n", host);
186			break;
187		default:
188			printf("Hosts on %s:\n", host);
189			break;
190		};
191		print_dump(mntdump);
192	}
193	if (rpcs & DOEXPORTS) {
194		printf("Exports list on %s:\n", host);
195		exp = exports;
196		while (exp) {
197			printf("%-35s", exp->ex_dirp);
198			grp = exp->ex_groups;
199			if (grp == NULL) {
200				printf("Everyone\n");
201			} else {
202				while (grp) {
203					printf("%s ", grp->gr_name);
204					grp = grp->gr_next;
205				}
206				printf("\n");
207			}
208			exp = exp->ex_next;
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 */
218int
219tcp_callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
220	char *host;
221	int prognum;
222	int versnum;
223	int procnum;
224	xdrproc_t inproc;
225	char *in;
226	xdrproc_t outproc;
227	char *out;
228{
229	CLIENT *client;
230	struct timeval timeout;
231	int rval;
232
233	if ((client = clnt_create(host, prognum, versnum, "tcp")) == NULL &&
234	    (client = clnt_create(host, prognum, versnum, "udp")) == NULL)
235		return ((int) rpc_createerr.cf_stat);
236
237	timeout.tv_sec = 25;
238	timeout.tv_usec = 0;
239	rval = (int) clnt_call(client, procnum,
240			       inproc, in,
241			       outproc, out,
242			       timeout);
243	clnt_destroy(client);
244 	return rval;
245}
246
247/*
248 * Xdr routine for retrieving the mount dump list
249 */
250int
251xdr_mntdump(xdrsp, mlp)
252	XDR *xdrsp;
253	struct mountlist **mlp;
254{
255	register struct mountlist *mp;
256	register struct mountlist *tp;
257	register struct mountlist **otp;
258	int val, val2;
259	int bool;
260	char *strp;
261
262	*mlp = (struct mountlist *)0;
263	if (!xdr_bool(xdrsp, &bool))
264		return (0);
265	while (bool) {
266		mp = (struct mountlist *)malloc(sizeof(struct mountlist));
267		if (mp == NULL)
268			return (0);
269		mp->ml_left = mp->ml_right = (struct mountlist *)0;
270		strp = mp->ml_host;
271		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
272			return (0);
273		strp = mp->ml_dirp;
274		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
275			return (0);
276
277		/*
278		 * Build a binary tree on sorted order of either host or dirp.
279		 * Drop any duplications.
280		 */
281		if (*mlp == NULL) {
282			*mlp = mp;
283		} else {
284			tp = *mlp;
285			while (tp) {
286				val = strcmp(mp->ml_host, tp->ml_host);
287				val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
288				switch (type) {
289				case ALL:
290					if (val == 0) {
291						if (val2 == 0) {
292							free((caddr_t)mp);
293							goto next;
294						}
295						val = val2;
296					}
297					break;
298				case DIRS:
299					if (val2 == 0) {
300						free((caddr_t)mp);
301						goto next;
302					}
303					val = val2;
304					break;
305				default:
306					if (val == 0) {
307						free((caddr_t)mp);
308						goto next;
309					}
310					break;
311				};
312				if (val < 0) {
313					otp = &tp->ml_left;
314					tp = tp->ml_left;
315				} else {
316					otp = &tp->ml_right;
317					tp = tp->ml_right;
318				}
319			}
320			*otp = mp;
321		}
322next:
323		if (!xdr_bool(xdrsp, &bool))
324			return (0);
325	}
326	return (1);
327}
328
329/*
330 * Xdr routine to retrieve exports list
331 */
332int
333xdr_exports(xdrsp, exp)
334	XDR *xdrsp;
335	struct exportslist **exp;
336{
337	register struct exportslist *ep;
338	register struct grouplist *gp;
339	int bool, grpbool;
340	char *strp;
341
342	*exp = (struct exportslist *)0;
343	if (!xdr_bool(xdrsp, &bool))
344		return (0);
345	while (bool) {
346		ep = (struct exportslist *)malloc(sizeof(struct exportslist));
347		if (ep == NULL)
348			return (0);
349		ep->ex_groups = (struct grouplist *)0;
350		strp = ep->ex_dirp;
351		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
352			return (0);
353		if (!xdr_bool(xdrsp, &grpbool))
354			return (0);
355		while (grpbool) {
356			gp = (struct grouplist *)malloc(sizeof(struct grouplist));
357			if (gp == NULL)
358				return (0);
359			strp = gp->gr_name;
360			if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
361				return (0);
362			gp->gr_next = ep->ex_groups;
363			ep->ex_groups = gp;
364			if (!xdr_bool(xdrsp, &grpbool))
365				return (0);
366		}
367		ep->ex_next = *exp;
368		*exp = ep;
369		if (!xdr_bool(xdrsp, &bool))
370			return (0);
371	}
372	return (1);
373}
374
375static void
376usage()
377{
378	fprintf(stderr, "usage: showmount [-ade3] host\n");
379	exit(1);
380}
381
382/*
383 * Print the binary tree in inorder so that output is sorted.
384 */
385void
386print_dump(mp)
387	struct mountlist *mp;
388{
389
390	if (mp == NULL)
391		return;
392	if (mp->ml_left)
393		print_dump(mp->ml_left);
394	switch (type) {
395	case ALL:
396		printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
397		break;
398	case DIRS:
399		printf("%s\n", mp->ml_dirp);
400		break;
401	default:
402		printf("%s\n", mp->ml_host);
403		break;
404	};
405	if (mp->ml_right)
406		print_dump(mp->ml_right);
407}
408