1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*
43  * nfs dfshares
44  */
45 #include <stdio.h>
46 #include <string.h>
47 #include <rpc/rpc.h>
48 #include <rpc/rpcb_clnt.h>
49 #include <sys/socket.h>
50 #include <netdb.h>
51 #include <sys/time.h>
52 #include <sys/errno.h>
53 #include <nfs/nfs.h>
54 #include <rpcsvc/mount.h>
55 
56 int hflg;
57 void pr_exports();
58 void free_ex();
59 void usage();
60 
61 main(argc, argv)
62 	int argc;
63 	char **argv;
64 {
65 
66 	char hostbuf[256];
67 	extern int optind;
68 	extern char *optarg;
69 	int i, c;
70 
71 	while ((c = getopt(argc, argv, "h")) != EOF) {
72 		switch (c) {
73 		case 'h':
74 			hflg++;
75 			break;
76 		default:
77 			usage();
78 			exit(1);
79 		}
80 	}
81 
82 	if (optind < argc) {
83 		for (i = optind; i < argc; i++)
84 			pr_exports(argv[i]);
85 	} else {
86 		if (gethostname(hostbuf, sizeof (hostbuf)) < 0) {
87 			perror("nfs dfshares: gethostname");
88 			exit(1);
89 		}
90 		pr_exports(hostbuf);
91 	}
92 
93 	exit(0);
94 }
95 
96 struct	timeval	rpc_totout_new = {15, 0};
97 
98 void
99 pr_exports(host)
100 	char *host;
101 {
102 	CLIENT *cl;
103 	struct exportnode *ex = NULL;
104 	enum clnt_stat err;
105 	struct	timeval	tout, rpc_totout_old;
106 
107 	(void) __rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
108 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
109 
110 	/*
111 	 * First try circuit, then drop back to datagram if
112 	 * circuit is unavailable (an old version of mountd perhaps)
113 	 * Using circuit is preferred because it can handle
114 	 * arbitrarily long export lists.
115 	 */
116 	cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_n");
117 	if (cl == NULL) {
118 		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
119 			cl = clnt_create(host, MOUNTPROG, MOUNTVERS,
120 					"datagram_n");
121 		if (cl == NULL) {
122 			(void) fprintf(stderr, "nfs dfshares:");
123 			clnt_pcreateerror(host);
124 			(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT,
125 				&rpc_totout_old);
126 			exit(1);
127 		}
128 	}
129 
130 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
131 	tout.tv_sec = 10;
132 	tout.tv_usec = 0;
133 
134 	if (err = clnt_call(cl, MOUNTPROC_EXPORT, xdr_void,
135 	    0, xdr_exports, (caddr_t) &ex, tout)) {
136 		(void) fprintf(stderr, "nfs dfshares: %s\n", clnt_sperrno(err));
137 		clnt_destroy(cl);
138 		exit(1);
139 	}
140 
141 	if (ex == NULL) {
142 		clnt_destroy(cl);
143 		exit(1);
144 	}
145 
146 	if (!hflg) {
147 		printf("%-35s %12s %-8s  %s\n",
148 			"RESOURCE", "SERVER", "ACCESS", "TRANSPORT");
149 		hflg++;
150 	}
151 
152 	while (ex) {
153 		printf("%10s:%-24s %12s %-8s  %s\n",
154 			host, ex->ex_dir, host, " -", " -");
155 		ex = ex->ex_next;
156 	}
157 	free_ex(ex);
158 	clnt_destroy(cl);
159 }
160 
161 void
162 free_ex(ex)
163 	struct exportnode *ex;
164 {
165 	struct groupnode *gr, *tmpgr;
166 	struct exportnode *tmpex;
167 
168 	while (ex) {
169 		free(ex->ex_dir);
170 		gr = ex->ex_groups;
171 		while (gr) {
172 			tmpgr = gr->gr_next;
173 			free((char *)gr);
174 			gr = tmpgr;
175 		}
176 		tmpex = ex;
177 		ex = ex->ex_next;
178 		free((char *)tmpex);
179 	}
180 }
181 
182 void
183 usage()
184 {
185 	(void) fprintf(stderr, "Usage: dfshares [-h] [host ...]\n");
186 }
187