Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / showmount / showmount.c
blob55602de4398231d2bfd361b410c617d162f93cf9
1 /* $NetBSD: showmount.c,v 1.16 2008/07/21 14:19:26 lukem Exp $ */
3 /*
4 * Copyright (c) 1989, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
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.
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.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1995\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
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.16 2008/07/21 14:19:26 lukem Exp $");
46 #endif /* not lint */
48 #include <sys/types.h>
49 #include <sys/file.h>
50 #include <sys/socket.h>
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>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
63 /* Constant defs */
64 #define ALL 1
65 #define DIRS 2
67 #define DODUMP 0x1
68 #define DOEXPORTS 0x2
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];
77 struct grouplist {
78 struct grouplist *gr_next;
79 char gr_name[RPCMNT_NAMELEN+1];
82 struct exportslist {
83 struct exportslist *ex_next;
84 struct grouplist *ex_groups;
85 char ex_dirp[RPCMNT_PATHLEN+1];
88 static struct mountlist *mntdump;
89 static struct exportslist *exports;
90 static int type = 0;
92 int main(int, char **);
93 void print_dump(struct mountlist *);
94 void usage(void);
95 int xdr_mntdump(XDR *, struct mountlist **);
96 int xdr_exports(XDR *, struct exportslist **);
97 int tcp_callrpc(const char *host, int prognum, int versnum, int procnum,
98 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
101 * This command queries the NFS mount daemon for it's mount list and/or
102 * it's exports list and prints them out.
103 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
104 * and the "Network File System Protocol XXX.."
105 * for detailed information on the protocol.
108 main(int argc, char **argv)
110 struct exportslist *exp;
111 struct grouplist *grp;
112 int estat, rpcs = 0, mntvers = 1;
113 const char *host;
114 int ch;
115 int len;
117 while ((ch = getopt(argc, argv, "ade3")) != -1)
118 switch((char)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 '3':
137 mntvers = 3;
138 break;
139 case '?':
140 default:
141 usage();
143 argc -= optind;
144 argv += optind;
146 if (argc > 0)
147 host = *argv;
148 else
149 host = "localhost";
151 if (rpcs == 0)
152 rpcs = DODUMP;
154 if (rpcs & DODUMP)
155 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
156 RPCMNT_DUMP, xdr_void, (char *)0,
157 xdr_mntdump, (char *)&mntdump)) != 0) {
158 fprintf(stderr, "showmount: Can't do Mountdump rpc: ");
159 clnt_perrno(estat);
160 exit(1);
162 if (rpcs & DOEXPORTS)
163 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
164 RPCMNT_EXPORT, xdr_void, (char *)0,
165 xdr_exports, (char *)&exports)) != 0) {
166 fprintf(stderr, "showmount: Can't do Exports rpc: ");
167 clnt_perrno(estat);
168 exit(1);
171 /* Now just print out the results */
172 if (rpcs & DODUMP) {
173 switch (type) {
174 case ALL:
175 printf("All mount points on %s:\n", host);
176 break;
177 case DIRS:
178 printf("Directories on %s:\n", host);
179 break;
180 default:
181 printf("Hosts on %s:\n", host);
182 break;
184 print_dump(mntdump);
186 if (rpcs & DOEXPORTS) {
187 printf("Exports list on %s:\n", host);
188 exp = exports;
189 while (exp) {
190 len = printf("%-35s", exp->ex_dirp);
191 if (len > 35)
192 printf("\t");
193 grp = exp->ex_groups;
194 if (grp == NULL) {
195 printf("Everyone\n");
196 } else {
197 while (grp) {
198 printf("%s ", grp->gr_name);
199 grp = grp->gr_next;
201 printf("\n");
203 exp = exp->ex_next;
207 exit(0);
211 * tcp_callrpc has the same interface as callrpc, but tries to
212 * use tcp as transport method in order to handle large replies.
215 int
216 tcp_callrpc(const char *host, int prognum, int versnum, int procnum,
217 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
219 CLIENT *client;
220 struct timeval timeout;
221 int rval;
223 if ((client = clnt_create(host, prognum, versnum, "tcp")) == NULL &&
224 (client = clnt_create(host, prognum, versnum, "udp")) == NULL)
225 return ((int) rpc_createerr.cf_stat);
227 timeout.tv_sec = 25;
228 timeout.tv_usec = 0;
229 rval = (int) clnt_call(client, procnum,
230 inproc, in,
231 outproc, out,
232 timeout);
233 clnt_destroy(client);
234 return rval;
238 * Xdr routine for retrieving the mount dump list
241 xdr_mntdump(XDR *xdrsp, struct mountlist **mlp)
243 struct mountlist *mp, **otp, *tp;
244 int bool, val, val2;
245 char *strp;
247 otp = NULL;
248 *mlp = (struct mountlist *)0;
249 if (!xdr_bool(xdrsp, &bool))
250 return (0);
251 while (bool) {
252 mp = (struct mountlist *)malloc(sizeof(struct mountlist));
253 if (mp == NULL)
254 return (0);
255 mp->ml_left = mp->ml_right = (struct mountlist *)0;
256 strp = mp->ml_host;
257 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
258 return (0);
259 strp = mp->ml_dirp;
260 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
261 return (0);
264 * Build a binary tree on sorted order of either host or dirp.
265 * Drop any duplications.
267 if (*mlp == NULL) {
268 *mlp = mp;
269 } else {
270 tp = *mlp;
271 while (tp) {
272 val = strcmp(mp->ml_host, tp->ml_host);
273 val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
274 switch (type) {
275 case ALL:
276 if (val == 0) {
277 if (val2 == 0) {
278 free((caddr_t)mp);
279 goto next;
281 val = val2;
283 break;
284 case DIRS:
285 if (val2 == 0) {
286 free((caddr_t)mp);
287 goto next;
289 val = val2;
290 break;
291 default:
292 if (val == 0) {
293 free((caddr_t)mp);
294 goto next;
296 break;
298 if (val < 0) {
299 otp = &tp->ml_left;
300 tp = tp->ml_left;
301 } else {
302 otp = &tp->ml_right;
303 tp = tp->ml_right;
306 *otp = mp;
308 next:
309 if (!xdr_bool(xdrsp, &bool))
310 return (0);
312 return (1);
316 * Xdr routine to retrieve exports list
319 xdr_exports(XDR *xdrsp, struct exportslist **exp)
321 struct exportslist *ep;
322 struct grouplist *gp;
323 int bool, grpbool;
324 char *strp;
326 *exp = (struct exportslist *)0;
327 if (!xdr_bool(xdrsp, &bool))
328 return (0);
329 while (bool) {
330 ep = (struct exportslist *)malloc(sizeof(struct exportslist));
331 if (ep == NULL)
332 return (0);
333 ep->ex_groups = (struct grouplist *)0;
334 strp = ep->ex_dirp;
335 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
336 return (0);
337 if (!xdr_bool(xdrsp, &grpbool))
338 return (0);
339 while (grpbool) {
340 gp = (struct grouplist *)malloc(sizeof(struct grouplist));
341 if (gp == NULL)
342 return (0);
343 strp = gp->gr_name;
344 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
345 return (0);
346 gp->gr_next = ep->ex_groups;
347 ep->ex_groups = gp;
348 if (!xdr_bool(xdrsp, &grpbool))
349 return (0);
351 ep->ex_next = *exp;
352 *exp = ep;
353 if (!xdr_bool(xdrsp, &bool))
354 return (0);
356 return (1);
359 void
360 usage()
363 fprintf(stderr, "usage: showmount [-ade3] host\n");
364 exit(1);
368 * Print the binary tree in inorder so that output is sorted.
370 void
371 print_dump(struct mountlist *mp)
374 if (mp == NULL)
375 return;
376 if (mp->ml_left)
377 print_dump(mp->ml_left);
378 switch (type) {
379 case ALL:
380 printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
381 break;
382 case DIRS:
383 printf("%s\n", mp->ml_dirp);
384 break;
385 default:
386 printf("%s\n", mp->ml_host);
387 break;
389 if (mp->ml_right)
390 print_dump(mp->ml_right);