10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*249Sjwahlig * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate * showmount
440Sstevel@tonic-gate */
450Sstevel@tonic-gate #include <stdio.h>
460Sstevel@tonic-gate #include <stdarg.h>
470Sstevel@tonic-gate #include <rpc/rpc.h>
480Sstevel@tonic-gate #include <rpc/rpcb_clnt.h>
490Sstevel@tonic-gate #include <sys/socket.h>
500Sstevel@tonic-gate #include <netdb.h>
510Sstevel@tonic-gate #include <sys/time.h>
520Sstevel@tonic-gate #include <errno.h>
530Sstevel@tonic-gate #include <nfs/nfs.h>
540Sstevel@tonic-gate #include <rpcsvc/mount.h>
550Sstevel@tonic-gate #include <locale.h>
560Sstevel@tonic-gate
570Sstevel@tonic-gate int sorthost();
580Sstevel@tonic-gate int sortpath();
590Sstevel@tonic-gate void pr_err(char *, ...);
600Sstevel@tonic-gate void printex();
610Sstevel@tonic-gate void usage();
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * Dynamically-sized array of pointers to mountlist entries. Each element
650Sstevel@tonic-gate * points into the linked list returned by the RPC call. We use an array
660Sstevel@tonic-gate * so that we can conveniently sort the entries.
670Sstevel@tonic-gate */
680Sstevel@tonic-gate static struct mountbody **table;
690Sstevel@tonic-gate
700Sstevel@tonic-gate struct timeval rpc_totout_new = {15, 0};
710Sstevel@tonic-gate
72*249Sjwahlig int
main(int argc,char * argv[])73*249Sjwahlig main(int argc, char *argv[])
740Sstevel@tonic-gate {
750Sstevel@tonic-gate int aflg = 0, dflg = 0, eflg = 0;
760Sstevel@tonic-gate int err;
770Sstevel@tonic-gate struct mountbody *result_list = NULL;
780Sstevel@tonic-gate struct mountbody *ml = NULL;
790Sstevel@tonic-gate struct mountbody **tb; /* pointer into table */
800Sstevel@tonic-gate char *host, hostbuf[256];
810Sstevel@tonic-gate char *last;
820Sstevel@tonic-gate CLIENT *cl;
830Sstevel@tonic-gate extern int optind;
840Sstevel@tonic-gate extern char *optarg;
850Sstevel@tonic-gate int c;
860Sstevel@tonic-gate struct timeval tout, rpc_totout_old;
870Sstevel@tonic-gate int numentries;
880Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
890Sstevel@tonic-gate
900Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
910Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
940Sstevel@tonic-gate
950Sstevel@tonic-gate while ((c = getopt(argc, argv, "ade")) != EOF) {
960Sstevel@tonic-gate switch (c) {
970Sstevel@tonic-gate case 'a':
980Sstevel@tonic-gate aflg++;
990Sstevel@tonic-gate break;
1000Sstevel@tonic-gate case 'd':
1010Sstevel@tonic-gate dflg++;
1020Sstevel@tonic-gate break;
1030Sstevel@tonic-gate case 'e':
1040Sstevel@tonic-gate eflg++;
1050Sstevel@tonic-gate break;
1060Sstevel@tonic-gate default:
1070Sstevel@tonic-gate usage();
1080Sstevel@tonic-gate exit(1);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate switch (argc - optind) {
1130Sstevel@tonic-gate case 0: /* no args */
1140Sstevel@tonic-gate if (gethostname(hostbuf, sizeof (hostbuf)) < 0) {
1150Sstevel@tonic-gate pr_err("gethostname: %s\n", strerror(errno));
1160Sstevel@tonic-gate exit(1);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate host = hostbuf;
1190Sstevel@tonic-gate break;
1200Sstevel@tonic-gate case 1: /* one arg */
1210Sstevel@tonic-gate host = argv[optind];
1220Sstevel@tonic-gate break;
1230Sstevel@tonic-gate default: /* too many args */
1240Sstevel@tonic-gate usage();
1250Sstevel@tonic-gate exit(1);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate __rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
1290Sstevel@tonic-gate __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate * First try circuit, then drop back to datagram if
1330Sstevel@tonic-gate * circuit is unavailable (an old version of mountd perhaps)
1340Sstevel@tonic-gate * Using circuit is preferred because it can handle
1350Sstevel@tonic-gate * arbitrarily long export lists.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_n");
1380Sstevel@tonic-gate if (cl == NULL) {
1390Sstevel@tonic-gate if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
1400Sstevel@tonic-gate cl = clnt_create(host, MOUNTPROG, MOUNTVERS,
1410Sstevel@tonic-gate "datagram_n");
1420Sstevel@tonic-gate if (cl == NULL) {
1430Sstevel@tonic-gate pr_err("");
1440Sstevel@tonic-gate clnt_pcreateerror(host);
1450Sstevel@tonic-gate __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
1460Sstevel@tonic-gate exit(1);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if (eflg) {
1530Sstevel@tonic-gate printex(cl, host);
1540Sstevel@tonic-gate if (aflg + dflg == 0) {
1550Sstevel@tonic-gate exit(0);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate tout.tv_sec = 10;
1600Sstevel@tonic-gate tout.tv_usec = 0;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (err = clnt_call(cl, MOUNTPROC_DUMP,
1630Sstevel@tonic-gate xdr_void, 0, xdr_mountlist,
1640Sstevel@tonic-gate (caddr_t)&result_list, tout)) {
1650Sstevel@tonic-gate pr_err("%s\n", clnt_sperrno(err));
1660Sstevel@tonic-gate exit(1);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Count the number of entries in the list. If the list is empty,
1710Sstevel@tonic-gate * quit now.
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate numentries = 0;
1740Sstevel@tonic-gate for (ml = result_list; ml != NULL; ml = ml->ml_next)
1750Sstevel@tonic-gate numentries++;
1760Sstevel@tonic-gate if (numentries == 0)
1770Sstevel@tonic-gate exit(0);
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate * Allocate memory for the array and initialize the array.
1810Sstevel@tonic-gate */
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate table = (struct mountbody **)calloc(numentries,
1840Sstevel@tonic-gate sizeof (struct mountbody *));
1850Sstevel@tonic-gate if (table == NULL) {
1860Sstevel@tonic-gate pr_err(gettext("not enough memory for %d entries\n"),
1870Sstevel@tonic-gate numentries);
1880Sstevel@tonic-gate exit(1);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate for (ml = result_list, tb = &table[0];
1910Sstevel@tonic-gate ml != NULL;
1920Sstevel@tonic-gate ml = ml->ml_next, tb++) {
1930Sstevel@tonic-gate *tb = ml;
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * Sort the entries and print the results.
1980Sstevel@tonic-gate */
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate if (dflg)
2010Sstevel@tonic-gate qsort(table, numentries, sizeof (struct mountbody *), sortpath);
2020Sstevel@tonic-gate else
2030Sstevel@tonic-gate qsort(table, numentries, sizeof (struct mountbody *), sorthost);
2040Sstevel@tonic-gate if (aflg) {
2050Sstevel@tonic-gate for (tb = table; tb < table + numentries; tb++)
2060Sstevel@tonic-gate printf("%s:%s\n", (*tb)->ml_hostname,
2070Sstevel@tonic-gate (*tb)->ml_directory);
2080Sstevel@tonic-gate } else if (dflg) {
2090Sstevel@tonic-gate last = "";
2100Sstevel@tonic-gate for (tb = table; tb < table + numentries; tb++) {
2110Sstevel@tonic-gate if (strcmp(last, (*tb)->ml_directory))
2120Sstevel@tonic-gate printf("%s\n", (*tb)->ml_directory);
2130Sstevel@tonic-gate last = (*tb)->ml_directory;
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate } else {
2160Sstevel@tonic-gate last = "";
2170Sstevel@tonic-gate for (tb = table; tb < table + numentries; tb++) {
2180Sstevel@tonic-gate if (strcmp(last, (*tb)->ml_hostname))
2190Sstevel@tonic-gate printf("%s\n", (*tb)->ml_hostname);
2200Sstevel@tonic-gate last = (*tb)->ml_hostname;
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate return (0);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
226*249Sjwahlig int
sorthost(a,b)2270Sstevel@tonic-gate sorthost(a, b)
2280Sstevel@tonic-gate struct mountbody **a, **b;
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate return (strcmp((*a)->ml_hostname, (*b)->ml_hostname));
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
233*249Sjwahlig int
sortpath(a,b)2340Sstevel@tonic-gate sortpath(a, b)
2350Sstevel@tonic-gate struct mountbody **a, **b;
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate return (strcmp((*a)->ml_directory, (*b)->ml_directory));
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate void
usage()2410Sstevel@tonic-gate usage()
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate (void) fprintf(stderr,
2440Sstevel@tonic-gate gettext("Usage: showmount [-a] [-d] [-e] [host]\n"));
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate void
pr_err(char * fmt,...)2480Sstevel@tonic-gate pr_err(char *fmt, ...)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate va_list ap;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate va_start(ap, fmt);
2530Sstevel@tonic-gate (void) fprintf(stderr, "showmount: ");
2540Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
2550Sstevel@tonic-gate va_end(ap);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate void
printex(cl,host)2590Sstevel@tonic-gate printex(cl, host)
2600Sstevel@tonic-gate CLIENT *cl;
2610Sstevel@tonic-gate char *host;
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate struct exportnode *ex = NULL;
2640Sstevel@tonic-gate struct exportnode *e;
2650Sstevel@tonic-gate struct groupnode *gr;
2660Sstevel@tonic-gate enum clnt_stat err;
2670Sstevel@tonic-gate int max;
2680Sstevel@tonic-gate struct timeval tout;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate tout.tv_sec = 10;
2710Sstevel@tonic-gate tout.tv_usec = 0;
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate if (err = clnt_call(cl, MOUNTPROC_EXPORT,
274*249Sjwahlig xdr_void, 0, xdr_exports, (caddr_t)&ex, tout)) {
2750Sstevel@tonic-gate pr_err("%s\n", clnt_sperrno(err));
2760Sstevel@tonic-gate exit(1);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate if (ex == NULL) {
2800Sstevel@tonic-gate printf(gettext("no exported file systems for %s\n"), host);
2810Sstevel@tonic-gate } else {
2820Sstevel@tonic-gate printf(gettext("export list for %s:\n"), host);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate max = 0;
2850Sstevel@tonic-gate for (e = ex; e != NULL; e = e->ex_next) {
2860Sstevel@tonic-gate if (strlen(e->ex_dir) > max) {
2870Sstevel@tonic-gate max = strlen(e->ex_dir);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate while (ex) {
2910Sstevel@tonic-gate printf("%-*s ", max, ex->ex_dir);
2920Sstevel@tonic-gate gr = ex->ex_groups;
2930Sstevel@tonic-gate if (gr == NULL) {
2940Sstevel@tonic-gate printf(gettext("(everyone)"));
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate while (gr) {
2970Sstevel@tonic-gate printf("%s", gr->gr_name);
2980Sstevel@tonic-gate gr = gr->gr_next;
2990Sstevel@tonic-gate if (gr) {
3000Sstevel@tonic-gate printf(",");
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate printf("\n");
3040Sstevel@tonic-gate ex = ex->ex_next;
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate }
307