xref: /netbsd/usr.bin/showmount/showmount.c (revision bf9ec67e)
1 /*	$NetBSD: showmount.c,v 1.11 2001/01/16 02:50:30 cgd 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. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1995\n\
42 	The Regents of the University of California.  All rights reserved.\n");
43 #endif /* not lint */
44 
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)showmount.c	8.3 (Berkeley) 3/29/95";
48 #endif
49 __RCSID("$NetBSD: showmount.c,v 1.11 2001/01/16 02:50:30 cgd Exp $");
50 #endif /* not lint */
51 
52 #include <sys/types.h>
53 #include <sys/file.h>
54 #include <sys/socket.h>
55 
56 #include <netdb.h>
57 #include <rpc/rpc.h>
58 #include <rpc/pmap_clnt.h>
59 #include <rpc/pmap_prot.h>
60 #include <nfs/rpcv2.h>
61 
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 
67 /* Constant defs */
68 #define	ALL	1
69 #define	DIRS	2
70 
71 #define	DODUMP		0x1
72 #define	DOEXPORTS	0x2
73 
74 struct mountlist {
75 	struct mountlist *ml_left;
76 	struct mountlist *ml_right;
77 	char	ml_host[RPCMNT_NAMELEN+1];
78 	char	ml_dirp[RPCMNT_PATHLEN+1];
79 };
80 
81 struct grouplist {
82 	struct grouplist *gr_next;
83 	char	gr_name[RPCMNT_NAMELEN+1];
84 };
85 
86 struct exportslist {
87 	struct exportslist *ex_next;
88 	struct grouplist *ex_groups;
89 	char	ex_dirp[RPCMNT_PATHLEN+1];
90 };
91 
92 static struct mountlist *mntdump;
93 static struct exportslist *exports;
94 static int type = 0;
95 
96 int	main __P((int, char **));
97 void	print_dump __P((struct mountlist *));
98 void	usage __P((void));
99 int	xdr_mntdump __P((XDR *, struct mountlist **));
100 int	xdr_exports __P((XDR *, struct exportslist **));
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 = 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 = 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  * Xdr routine for retrieving the mount dump list
216  */
217 int
218 xdr_mntdump(xdrsp, mlp)
219 	XDR *xdrsp;
220 	struct mountlist **mlp;
221 {
222 	struct mountlist *mp, **otp, *tp;
223 	int bool, val, val2;
224 	char *strp;
225 
226 	otp = NULL;
227 	*mlp = (struct mountlist *)0;
228 	if (!xdr_bool(xdrsp, &bool))
229 		return (0);
230 	while (bool) {
231 		mp = (struct mountlist *)malloc(sizeof(struct mountlist));
232 		if (mp == NULL)
233 			return (0);
234 		mp->ml_left = mp->ml_right = (struct mountlist *)0;
235 		strp = mp->ml_host;
236 		if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
237 			return (0);
238 		strp = mp->ml_dirp;
239 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
240 			return (0);
241 
242 		/*
243 		 * Build a binary tree on sorted order of either host or dirp.
244 		 * Drop any duplications.
245 		 */
246 		if (*mlp == NULL) {
247 			*mlp = mp;
248 		} else {
249 			tp = *mlp;
250 			while (tp) {
251 				val = strcmp(mp->ml_host, tp->ml_host);
252 				val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
253 				switch (type) {
254 				case ALL:
255 					if (val == 0) {
256 						if (val2 == 0) {
257 							free((caddr_t)mp);
258 							goto next;
259 						}
260 						val = val2;
261 					}
262 					break;
263 				case DIRS:
264 					if (val2 == 0) {
265 						free((caddr_t)mp);
266 						goto next;
267 					}
268 					val = val2;
269 					break;
270 				default:
271 					if (val == 0) {
272 						free((caddr_t)mp);
273 						goto next;
274 					}
275 					break;
276 				};
277 				if (val < 0) {
278 					otp = &tp->ml_left;
279 					tp = tp->ml_left;
280 				} else {
281 					otp = &tp->ml_right;
282 					tp = tp->ml_right;
283 				}
284 			}
285 			*otp = mp;
286 		}
287 next:
288 		if (!xdr_bool(xdrsp, &bool))
289 			return (0);
290 	}
291 	return (1);
292 }
293 
294 /*
295  * Xdr routine to retrieve exports list
296  */
297 int
298 xdr_exports(xdrsp, exp)
299 	XDR *xdrsp;
300 	struct exportslist **exp;
301 {
302 	struct exportslist *ep;
303 	struct grouplist *gp;
304 	int bool, grpbool;
305 	char *strp;
306 
307 	*exp = (struct exportslist *)0;
308 	if (!xdr_bool(xdrsp, &bool))
309 		return (0);
310 	while (bool) {
311 		ep = (struct exportslist *)malloc(sizeof(struct exportslist));
312 		if (ep == NULL)
313 			return (0);
314 		ep->ex_groups = (struct grouplist *)0;
315 		strp = ep->ex_dirp;
316 		if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
317 			return (0);
318 		if (!xdr_bool(xdrsp, &grpbool))
319 			return (0);
320 		while (grpbool) {
321 			gp = (struct grouplist *)malloc(sizeof(struct grouplist));
322 			if (gp == NULL)
323 				return (0);
324 			strp = gp->gr_name;
325 			if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
326 				return (0);
327 			gp->gr_next = ep->ex_groups;
328 			ep->ex_groups = gp;
329 			if (!xdr_bool(xdrsp, &grpbool))
330 				return (0);
331 		}
332 		ep->ex_next = *exp;
333 		*exp = ep;
334 		if (!xdr_bool(xdrsp, &bool))
335 			return (0);
336 	}
337 	return (1);
338 }
339 
340 void
341 usage()
342 {
343 
344 	fprintf(stderr, "usage: showmount [-ade3] host\n");
345 	exit(1);
346 }
347 
348 /*
349  * Print the binary tree in inorder so that output is sorted.
350  */
351 void
352 print_dump(mp)
353 	struct mountlist *mp;
354 {
355 
356 	if (mp == NULL)
357 		return;
358 	if (mp->ml_left)
359 		print_dump(mp->ml_left);
360 	switch (type) {
361 	case ALL:
362 		printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
363 		break;
364 	case DIRS:
365 		printf("%s\n", mp->ml_dirp);
366 		break;
367 	default:
368 		printf("%s\n", mp->ml_host);
369 		break;
370 	};
371 	if (mp->ml_right)
372 		print_dump(mp->ml_right);
373 }
374