2 * Copyright (c) 1989, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1989, 1993, 1995 The Regents of the University of California. All rights reserved.
37 * @(#)showmount.c 8.3 (Berkeley) 3/29/95
38 * $FreeBSD: src/usr.bin/showmount/showmount.c,v 1.8 1999/08/28 01:05:43 peter Exp $
39 * $DragonFly: src/usr.bin/showmount/showmount.c,v 1.5 2005/09/05 04:22:07 swildner Exp $
42 #include <sys/types.h>
43 #include <sys/queue.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
51 #include <rpc/pmap_clnt.h>
52 #include <rpc/pmap_prot.h>
53 #include <nfs/rpcv2.h>
68 struct mountlist *ml_left;
69 struct mountlist *ml_right;
70 char ml_host[RPCMNT_NAMELEN+1];
71 char ml_dirp[RPCMNT_PATHLEN+1];
75 struct grouplist *gr_next;
76 char gr_name[RPCMNT_NAMELEN+1];
80 struct exportslist *ex_next;
81 struct grouplist *ex_groups;
82 char ex_dirp[RPCMNT_PATHLEN+1];
85 static struct mountlist *mntdump;
86 static struct exportslist *exports;
89 void print_dump(struct mountlist *);
90 static void usage(void);
91 int xdr_mntdump(XDR *, struct mountlist **);
92 int xdr_exports(XDR *, struct exportslist **);
93 int tcp_callrpc(const char *host,
94 int prognum, int versnum, int procnum,
95 xdrproc_t inproc, char *in,
96 xdrproc_t outproc, char *out);
99 * This command queries the NFS mount daemon for it's mount list and/or
100 * it's exports list and prints them out.
101 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
102 * and the "Network File System Protocol XXX.."
103 * for detailed information on the protocol.
106 main(int argc, char **argv)
108 struct exportslist *exp;
109 struct grouplist *grp;
110 int rpcs = 0, mntvers = 1;
115 while ((ch = getopt(argc, argv, "ade3")) != -1)
153 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
154 RPCMNT_DUMP, xdr_void, (char *)0,
155 xdr_mntdump, (char *)&mntdump)) != 0) {
157 errx(1, "can't do mountdump rpc");
159 if (rpcs & DOEXPORTS)
160 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
161 RPCMNT_EXPORT, xdr_void, (char *)0,
162 xdr_exports, (char *)&exports)) != 0) {
164 errx(1, "can't do exports rpc");
167 /* Now just print out the results */
171 printf("All mount points on %s:\n", host);
174 printf("Directories on %s:\n", host);
177 printf("Hosts on %s:\n", host);
182 if (rpcs & DOEXPORTS) {
183 printf("Exports list on %s:\n", host);
186 printf("%-35s", exp->ex_dirp);
187 grp = exp->ex_groups;
189 printf("Everyone\n");
192 printf("%s ", grp->gr_name);
204 * tcp_callrpc has the same interface as callrpc, but tries to
205 * use tcp as transport method in order to handle large replies.
209 tcp_callrpc(const char *host, int prognum, int versnum, int procnum,
210 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
213 struct sockaddr_in server_addr;
216 struct timeval timeout;
219 hp = gethostbyname(host);
222 return ((int) RPC_UNKNOWNHOST);
224 memset(&server_addr,0,sizeof(server_addr));
225 memcpy((char *) &server_addr.sin_addr,
228 server_addr.sin_len = sizeof(struct sockaddr_in);
229 server_addr.sin_family =AF_INET;
230 server_addr.sin_port = 0;
234 client = clnttcp_create(&server_addr,
236 (u_long) versnum, &sock, 0, 0);
240 server_addr.sin_port = 0;
242 client = clntudp_create(&server_addr,
249 return ((int) rpc_createerr.cf_stat);
253 rval = (int) clnt_call(client, procnum,
257 clnt_destroy(client);
262 * Xdr routine for retrieving the mount dump list
265 xdr_mntdump(XDR *xdrsp, struct mountlist **mlp)
267 struct mountlist *mp;
268 struct mountlist *tp;
269 struct mountlist **otp = NULL;
274 *mlp = (struct mountlist *)0;
275 if (!xdr_bool(xdrsp, &bool))
278 mp = (struct mountlist *)malloc(sizeof(struct mountlist));
281 mp->ml_left = mp->ml_right = (struct mountlist *)0;
283 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
286 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
290 * Build a binary tree on sorted order of either host or dirp.
291 * Drop any duplications.
298 val = strcmp(mp->ml_host, tp->ml_host);
299 val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
335 if (!xdr_bool(xdrsp, &bool))
342 * Xdr routine to retrieve exports list
345 xdr_exports(XDR *xdrsp, struct exportslist **exp)
347 struct exportslist *ep;
348 struct grouplist *gp;
352 *exp = (struct exportslist *)0;
353 if (!xdr_bool(xdrsp, &bool))
356 ep = (struct exportslist *)malloc(sizeof(struct exportslist));
359 ep->ex_groups = (struct grouplist *)0;
361 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
363 if (!xdr_bool(xdrsp, &grpbool))
366 gp = (struct grouplist *)malloc(sizeof(struct grouplist));
370 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
372 gp->gr_next = ep->ex_groups;
374 if (!xdr_bool(xdrsp, &grpbool))
379 if (!xdr_bool(xdrsp, &bool))
388 fprintf(stderr, "usage: showmount [-ade3] host\n");
393 * Print the binary tree in inorder so that output is sorted.
396 print_dump(struct mountlist *mp)
401 print_dump(mp->ml_left);
404 printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
407 printf("%s\n", mp->ml_dirp);
410 printf("%s\n", mp->ml_host);
414 print_dump(mp->ml_right);