1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * showmount
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate #include <stdio.h>
46*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
47*7c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
48*7c478bd9Sstevel@tonic-gate #include <rpc/rpcb_clnt.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
50*7c478bd9Sstevel@tonic-gate #include <netdb.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
52*7c478bd9Sstevel@tonic-gate #include <errno.h>
53*7c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
54*7c478bd9Sstevel@tonic-gate #include <rpcsvc/mount.h>
55*7c478bd9Sstevel@tonic-gate #include <locale.h>
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate int sorthost();
58*7c478bd9Sstevel@tonic-gate int sortpath();
59*7c478bd9Sstevel@tonic-gate void pr_err(char *, ...);
60*7c478bd9Sstevel@tonic-gate void printex();
61*7c478bd9Sstevel@tonic-gate void usage();
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * Dynamically-sized array of pointers to mountlist entries.  Each element
65*7c478bd9Sstevel@tonic-gate  * points into the linked list returned by the RPC call.  We use an array
66*7c478bd9Sstevel@tonic-gate  * so that we can conveniently sort the entries.
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate static struct mountbody **table;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate struct	timeval	rpc_totout_new = {15, 0};
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate main(argc, argv)
73*7c478bd9Sstevel@tonic-gate 	int argc;
74*7c478bd9Sstevel@tonic-gate 	char **argv;
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	int aflg = 0, dflg = 0, eflg = 0;
77*7c478bd9Sstevel@tonic-gate 	int err;
78*7c478bd9Sstevel@tonic-gate 	struct mountbody *result_list = NULL;
79*7c478bd9Sstevel@tonic-gate 	struct mountbody *ml = NULL;
80*7c478bd9Sstevel@tonic-gate 	struct mountbody **tb;		/* pointer into table */
81*7c478bd9Sstevel@tonic-gate 	char *host, hostbuf[256];
82*7c478bd9Sstevel@tonic-gate 	char *last;
83*7c478bd9Sstevel@tonic-gate 	CLIENT *cl;
84*7c478bd9Sstevel@tonic-gate 	extern int optind;
85*7c478bd9Sstevel@tonic-gate 	extern char *optarg;
86*7c478bd9Sstevel@tonic-gate 	int c;
87*7c478bd9Sstevel@tonic-gate 	struct	timeval	tout, rpc_totout_old;
88*7c478bd9Sstevel@tonic-gate 	int	numentries;
89*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
92*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
93*7c478bd9Sstevel@tonic-gate #endif
94*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ade")) != EOF) {
97*7c478bd9Sstevel@tonic-gate 		switch (c) {
98*7c478bd9Sstevel@tonic-gate 		case 'a':
99*7c478bd9Sstevel@tonic-gate 			aflg++;
100*7c478bd9Sstevel@tonic-gate 			break;
101*7c478bd9Sstevel@tonic-gate 		case 'd':
102*7c478bd9Sstevel@tonic-gate 			dflg++;
103*7c478bd9Sstevel@tonic-gate 			break;
104*7c478bd9Sstevel@tonic-gate 		case 'e':
105*7c478bd9Sstevel@tonic-gate 			eflg++;
106*7c478bd9Sstevel@tonic-gate 			break;
107*7c478bd9Sstevel@tonic-gate 		default:
108*7c478bd9Sstevel@tonic-gate 			usage();
109*7c478bd9Sstevel@tonic-gate 			exit(1);
110*7c478bd9Sstevel@tonic-gate 		}
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	switch (argc - optind) {
114*7c478bd9Sstevel@tonic-gate 	case 0:		/* no args */
115*7c478bd9Sstevel@tonic-gate 		if (gethostname(hostbuf, sizeof (hostbuf)) < 0) {
116*7c478bd9Sstevel@tonic-gate 			pr_err("gethostname: %s\n", strerror(errno));
117*7c478bd9Sstevel@tonic-gate 			exit(1);
118*7c478bd9Sstevel@tonic-gate 		}
119*7c478bd9Sstevel@tonic-gate 		host = hostbuf;
120*7c478bd9Sstevel@tonic-gate 		break;
121*7c478bd9Sstevel@tonic-gate 	case 1:		/* one arg */
122*7c478bd9Sstevel@tonic-gate 		host = argv[optind];
123*7c478bd9Sstevel@tonic-gate 		break;
124*7c478bd9Sstevel@tonic-gate 	default:	/* too many args */
125*7c478bd9Sstevel@tonic-gate 		usage();
126*7c478bd9Sstevel@tonic-gate 		exit(1);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	__rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
130*7c478bd9Sstevel@tonic-gate 	__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	/*
133*7c478bd9Sstevel@tonic-gate 	 * First try circuit, then drop back to datagram if
134*7c478bd9Sstevel@tonic-gate 	 * circuit is unavailable (an old version of mountd perhaps)
135*7c478bd9Sstevel@tonic-gate 	 * Using circuit is preferred because it can handle
136*7c478bd9Sstevel@tonic-gate 	 * arbitrarily long export lists.
137*7c478bd9Sstevel@tonic-gate 	 */
138*7c478bd9Sstevel@tonic-gate 	cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_n");
139*7c478bd9Sstevel@tonic-gate 	if (cl == NULL) {
140*7c478bd9Sstevel@tonic-gate 		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
141*7c478bd9Sstevel@tonic-gate 			cl = clnt_create(host, MOUNTPROG, MOUNTVERS,
142*7c478bd9Sstevel@tonic-gate 					"datagram_n");
143*7c478bd9Sstevel@tonic-gate 		if (cl == NULL) {
144*7c478bd9Sstevel@tonic-gate 			pr_err("");
145*7c478bd9Sstevel@tonic-gate 			clnt_pcreateerror(host);
146*7c478bd9Sstevel@tonic-gate 			__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
147*7c478bd9Sstevel@tonic-gate 			exit(1);
148*7c478bd9Sstevel@tonic-gate 		}
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	__rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	if (eflg) {
154*7c478bd9Sstevel@tonic-gate 		printex(cl, host);
155*7c478bd9Sstevel@tonic-gate 		if (aflg + dflg == 0) {
156*7c478bd9Sstevel@tonic-gate 			exit(0);
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	tout.tv_sec = 10;
161*7c478bd9Sstevel@tonic-gate 	tout.tv_usec = 0;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	if (err = clnt_call(cl, MOUNTPROC_DUMP,
164*7c478bd9Sstevel@tonic-gate 			    xdr_void, 0, xdr_mountlist,
165*7c478bd9Sstevel@tonic-gate 				(caddr_t)&result_list, tout)) {
166*7c478bd9Sstevel@tonic-gate 		pr_err("%s\n", clnt_sperrno(err));
167*7c478bd9Sstevel@tonic-gate 		exit(1);
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	/*
171*7c478bd9Sstevel@tonic-gate 	 * Count the number of entries in the list.  If the list is empty,
172*7c478bd9Sstevel@tonic-gate 	 * quit now.
173*7c478bd9Sstevel@tonic-gate 	 */
174*7c478bd9Sstevel@tonic-gate 	numentries = 0;
175*7c478bd9Sstevel@tonic-gate 	for (ml = result_list; ml != NULL; ml = ml->ml_next)
176*7c478bd9Sstevel@tonic-gate 		numentries++;
177*7c478bd9Sstevel@tonic-gate 	if (numentries == 0)
178*7c478bd9Sstevel@tonic-gate 		exit(0);
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	/*
181*7c478bd9Sstevel@tonic-gate 	 * Allocate memory for the array and initialize the array.
182*7c478bd9Sstevel@tonic-gate 	 */
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	table = (struct mountbody **)calloc(numentries,
185*7c478bd9Sstevel@tonic-gate 						sizeof (struct mountbody *));
186*7c478bd9Sstevel@tonic-gate 	if (table == NULL) {
187*7c478bd9Sstevel@tonic-gate 		pr_err(gettext("not enough memory for %d entries\n"),
188*7c478bd9Sstevel@tonic-gate 		    numentries);
189*7c478bd9Sstevel@tonic-gate 		exit(1);
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 	for (ml = result_list, tb = &table[0];
192*7c478bd9Sstevel@tonic-gate 	    ml != NULL;
193*7c478bd9Sstevel@tonic-gate 	    ml = ml->ml_next, tb++) {
194*7c478bd9Sstevel@tonic-gate 		*tb = ml;
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	/*
198*7c478bd9Sstevel@tonic-gate 	 * Sort the entries and print the results.
199*7c478bd9Sstevel@tonic-gate 	 */
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	if (dflg)
202*7c478bd9Sstevel@tonic-gate 	    qsort(table, numentries, sizeof (struct mountbody *), sortpath);
203*7c478bd9Sstevel@tonic-gate 	else
204*7c478bd9Sstevel@tonic-gate 	    qsort(table, numentries, sizeof (struct mountbody *), sorthost);
205*7c478bd9Sstevel@tonic-gate 	if (aflg) {
206*7c478bd9Sstevel@tonic-gate 		for (tb = table; tb < table + numentries; tb++)
207*7c478bd9Sstevel@tonic-gate 			printf("%s:%s\n", (*tb)->ml_hostname,
208*7c478bd9Sstevel@tonic-gate 			    (*tb)->ml_directory);
209*7c478bd9Sstevel@tonic-gate 	} else if (dflg) {
210*7c478bd9Sstevel@tonic-gate 		last = "";
211*7c478bd9Sstevel@tonic-gate 		for (tb = table; tb < table + numentries; tb++) {
212*7c478bd9Sstevel@tonic-gate 			if (strcmp(last, (*tb)->ml_directory))
213*7c478bd9Sstevel@tonic-gate 				printf("%s\n", (*tb)->ml_directory);
214*7c478bd9Sstevel@tonic-gate 			last = (*tb)->ml_directory;
215*7c478bd9Sstevel@tonic-gate 		}
216*7c478bd9Sstevel@tonic-gate 	} else {
217*7c478bd9Sstevel@tonic-gate 		last = "";
218*7c478bd9Sstevel@tonic-gate 		for (tb = table; tb < table + numentries; tb++) {
219*7c478bd9Sstevel@tonic-gate 			if (strcmp(last, (*tb)->ml_hostname))
220*7c478bd9Sstevel@tonic-gate 				printf("%s\n", (*tb)->ml_hostname);
221*7c478bd9Sstevel@tonic-gate 			last = (*tb)->ml_hostname;
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 	}
224*7c478bd9Sstevel@tonic-gate 	return (0);
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate sorthost(a, b)
228*7c478bd9Sstevel@tonic-gate 	struct mountbody **a, **b;
229*7c478bd9Sstevel@tonic-gate {
230*7c478bd9Sstevel@tonic-gate 	return (strcmp((*a)->ml_hostname, (*b)->ml_hostname));
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate sortpath(a, b)
234*7c478bd9Sstevel@tonic-gate 	struct mountbody **a, **b;
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate 	return (strcmp((*a)->ml_directory, (*b)->ml_directory));
237*7c478bd9Sstevel@tonic-gate }
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate void
240*7c478bd9Sstevel@tonic-gate usage()
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
243*7c478bd9Sstevel@tonic-gate 			gettext("Usage: showmount [-a] [-d] [-e] [host]\n"));
244*7c478bd9Sstevel@tonic-gate }
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate void
247*7c478bd9Sstevel@tonic-gate pr_err(char *fmt, ...)
248*7c478bd9Sstevel@tonic-gate {
249*7c478bd9Sstevel@tonic-gate 	va_list ap;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
252*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "showmount: ");
253*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
254*7c478bd9Sstevel@tonic-gate 	va_end(ap);
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate void
258*7c478bd9Sstevel@tonic-gate printex(cl, host)
259*7c478bd9Sstevel@tonic-gate 	CLIENT *cl;
260*7c478bd9Sstevel@tonic-gate 	char *host;
261*7c478bd9Sstevel@tonic-gate {
262*7c478bd9Sstevel@tonic-gate 	struct exportnode *ex = NULL;
263*7c478bd9Sstevel@tonic-gate 	struct exportnode *e;
264*7c478bd9Sstevel@tonic-gate 	struct groupnode *gr;
265*7c478bd9Sstevel@tonic-gate 	enum clnt_stat err;
266*7c478bd9Sstevel@tonic-gate 	int max;
267*7c478bd9Sstevel@tonic-gate 	struct	timeval	tout;
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	tout.tv_sec = 10;
270*7c478bd9Sstevel@tonic-gate 	tout.tv_usec = 0;
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	if (err = clnt_call(cl, MOUNTPROC_EXPORT,
273*7c478bd9Sstevel@tonic-gate 	    xdr_void, 0, xdr_exports, (caddr_t) &ex, tout)) {
274*7c478bd9Sstevel@tonic-gate 		pr_err("%s\n", clnt_sperrno(err));
275*7c478bd9Sstevel@tonic-gate 		exit(1);
276*7c478bd9Sstevel@tonic-gate 	}
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	if (ex == NULL) {
279*7c478bd9Sstevel@tonic-gate 		printf(gettext("no exported file systems for %s\n"), host);
280*7c478bd9Sstevel@tonic-gate 	} else {
281*7c478bd9Sstevel@tonic-gate 		printf(gettext("export list for %s:\n"), host);
282*7c478bd9Sstevel@tonic-gate 	}
283*7c478bd9Sstevel@tonic-gate 	max = 0;
284*7c478bd9Sstevel@tonic-gate 	for (e = ex; e != NULL; e = e->ex_next) {
285*7c478bd9Sstevel@tonic-gate 		if (strlen(e->ex_dir) > max) {
286*7c478bd9Sstevel@tonic-gate 			max = strlen(e->ex_dir);
287*7c478bd9Sstevel@tonic-gate 		}
288*7c478bd9Sstevel@tonic-gate 	}
289*7c478bd9Sstevel@tonic-gate 	while (ex) {
290*7c478bd9Sstevel@tonic-gate 		printf("%-*s ", max, ex->ex_dir);
291*7c478bd9Sstevel@tonic-gate 		gr = ex->ex_groups;
292*7c478bd9Sstevel@tonic-gate 		if (gr == NULL) {
293*7c478bd9Sstevel@tonic-gate 			printf(gettext("(everyone)"));
294*7c478bd9Sstevel@tonic-gate 		}
295*7c478bd9Sstevel@tonic-gate 		while (gr) {
296*7c478bd9Sstevel@tonic-gate 			printf("%s", gr->gr_name);
297*7c478bd9Sstevel@tonic-gate 			gr = gr->gr_next;
298*7c478bd9Sstevel@tonic-gate 			if (gr) {
299*7c478bd9Sstevel@tonic-gate 				printf(",");
300*7c478bd9Sstevel@tonic-gate 			}
301*7c478bd9Sstevel@tonic-gate 		}
302*7c478bd9Sstevel@tonic-gate 		printf("\n");
303*7c478bd9Sstevel@tonic-gate 		ex = ex->ex_next;
304*7c478bd9Sstevel@tonic-gate 	}
305*7c478bd9Sstevel@tonic-gate }
306