2 * SPDX-License-Identifier: BSD-3-Clause
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
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
36 static const char copyright[] =
37 "@(#) Copyright (c) 1989, 1993, 1995\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)showmount.c 8.3 (Berkeley) 3/29/95";
47 #include <sys/types.h>
48 #include <sys/queue.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
57 #include <rpc/pmap_clnt.h>
58 #include <rpc/pmap_prot.h>
59 #include <rpcsvc/mount.h>
73 #define DOPARSABLEEXPORTS 0x4
76 struct mountlist *ml_left;
77 struct mountlist *ml_right;
78 char ml_host[MNTNAMLEN+1];
79 char ml_dirp[MNTPATHLEN+1];
83 struct grouplist *gr_next;
84 char gr_name[MNTNAMLEN+1];
88 struct exportslist *ex_next;
89 struct grouplist *ex_groups;
90 char ex_dirp[MNTPATHLEN+1];
93 static struct mountlist *mntdump;
94 static struct exportslist *exportslist;
97 void print_dump(struct mountlist *);
98 static void usage(void) __dead2;
99 int xdr_mntdump(XDR *, struct mountlist **);
100 int xdr_exportslist(XDR *, struct exportslist **);
101 int tcp_callrpc(const char *host, int prognum, int versnum, int procnum,
102 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
104 static const struct option long_opts[] = {
105 { "all", no_argument, NULL, 'a' },
106 { "directories", no_argument, NULL, 'd' },
107 { "exports-script", no_argument, NULL, 'E' },
108 { "exports", no_argument, NULL, 'e' },
109 { NULL, 0, NULL, 0 },
113 * This command queries the NFS mount daemon for it's mount list and/or
114 * it's exports list and prints them out.
115 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
116 * and the "Network File System Protocol XXX.."
117 * for detailed information on the protocol.
120 main(int argc, char **argv)
122 char strvised[MNTPATHLEN * 4 + 1];
123 register struct exportslist *exp;
124 register struct grouplist *grp;
125 register int rpcs = 0, mntvers = 3;
127 int ch, estat, nbytes;
129 while ((ch = getopt_long(argc, argv, "+adEe13", long_opts, NULL)) != -1)
146 rpcs |= DOPARSABLEEXPORTS;
164 if ((rpcs & DOPARSABLEEXPORTS) != 0) {
165 if ((rpcs & DOEXPORTS) != 0)
166 errx(1, "-E cannot be used with -e");
167 if ((rpcs & DODUMP) != 0)
168 errx(1, "-E cannot be used with -a or -d");
180 if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers,
181 MOUNTPROC_DUMP, (xdrproc_t)xdr_void, (char *)0,
182 (xdrproc_t)xdr_mntdump, (char *)&mntdump)) != 0) {
184 errx(1, "can't do mountdump rpc");
186 if (rpcs & (DOEXPORTS | DOPARSABLEEXPORTS))
187 if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers,
188 MOUNTPROC_EXPORT, (xdrproc_t)xdr_void, (char *)0,
189 (xdrproc_t)xdr_exportslist, (char *)&exportslist)) != 0) {
191 errx(1, "can't do exports rpc");
194 /* Now just print out the results */
198 printf("All mount points on %s:\n", host);
201 printf("Directories on %s:\n", host);
204 printf("Hosts on %s:\n", host);
209 if (rpcs & DOEXPORTS) {
210 printf("Exports list on %s:\n", host);
213 printf("%-34s ", exp->ex_dirp);
214 grp = exp->ex_groups;
216 printf("Everyone\n");
219 printf("%s ", grp->gr_name);
227 if (rpcs & DOPARSABLEEXPORTS) {
230 nbytes = strsnvis(strvised, sizeof(strvised),
231 exp->ex_dirp, VIS_GLOB | VIS_NL, "\"'$");
234 printf("%s\n", strvised);
242 * tcp_callrpc has the same interface as callrpc, but tries to
243 * use tcp as transport method in order to handle large replies.
246 tcp_callrpc(const char *host, int prognum, int versnum, int procnum,
247 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out)
250 struct timeval timeout;
253 if ((client = clnt_create(host, prognum, versnum, "tcp")) == NULL &&
254 (client = clnt_create(host, prognum, versnum, "udp")) == NULL)
255 return ((int) rpc_createerr.cf_stat);
259 rval = (int) clnt_call(client, procnum,
263 clnt_destroy(client);
268 * Xdr routine for retrieving the mount dump list
271 xdr_mntdump(XDR *xdrsp, struct mountlist **mlp)
273 register struct mountlist *mp;
274 register struct mountlist *tp;
275 register struct mountlist **otp;
280 *mlp = (struct mountlist *)0;
281 if (!xdr_bool(xdrsp, &bool))
284 mp = (struct mountlist *)malloc(sizeof(struct mountlist));
287 mp->ml_left = mp->ml_right = (struct mountlist *)0;
289 if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) {
294 if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) {
300 * Build a binary tree on sorted order of either host or dirp.
301 * Drop any duplications.
308 val = strcmp(mp->ml_host, tp->ml_host);
309 val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
345 if (!xdr_bool(xdrsp, &bool))
352 * Xdr routine to retrieve exports list
355 xdr_exportslist(XDR *xdrsp, struct exportslist **exp)
357 register struct exportslist *ep;
358 register struct grouplist *gp;
362 *exp = (struct exportslist *)0;
363 if (!xdr_bool(xdrsp, &bool))
366 ep = (struct exportslist *)malloc(sizeof(struct exportslist));
369 ep->ex_groups = (struct grouplist *)0;
371 if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
373 if (!xdr_bool(xdrsp, &grpbool))
376 gp = (struct grouplist *)malloc(sizeof(struct grouplist));
380 if (!xdr_string(xdrsp, &strp, MNTNAMLEN))
382 gp->gr_next = ep->ex_groups;
384 if (!xdr_bool(xdrsp, &grpbool))
389 if (!xdr_bool(xdrsp, &bool))
398 fprintf(stderr, "usage: showmount [-a | -d] [-e3] [host]\n");
403 * Print the binary tree in inorder so that output is sorted.
406 print_dump(struct mountlist *mp)
412 print_dump(mp->ml_left);
415 printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
418 printf("%s\n", mp->ml_dirp);
421 printf("%s\n", mp->ml_host);
425 print_dump(mp->ml_right);