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 2005 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 int
62 main(int argc, char *argv[])
63 {
64 
65 	char hostbuf[256];
66 	extern int optind;
67 	extern char *optarg;
68 	int i, c;
69 
70 	while ((c = getopt(argc, argv, "h")) != EOF) {
71 		switch (c) {
72 		case 'h':
73 			hflg++;
74 			break;
75 		default:
76 			usage();
77 			exit(1);
78 		}
79 	}
80 
81 	if (optind < argc) {
82 		for (i = optind; i < argc; i++)
83 			pr_exports(argv[i]);
84 	} else {
85 		if (gethostname(hostbuf, sizeof (hostbuf)) < 0) {
86 			perror("nfs dfshares: gethostname");
87 			exit(1);
88 		}
89 		pr_exports(hostbuf);
90 	}
91 
92 	return (0);
93 }
94 
95 struct	timeval	rpc_totout_new = {15, 0};
96 
97 void
98 pr_exports(host)
99 	char *host;
100 {
101 	CLIENT *cl;
102 	struct exportnode *ex = NULL;
103 	enum clnt_stat err;
104 	struct	timeval	tout, rpc_totout_old;
105 
106 	(void) __rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
107 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
108 
109 	/*
110 	 * First try circuit, then drop back to datagram if
111 	 * circuit is unavailable (an old version of mountd perhaps)
112 	 * Using circuit is preferred because it can handle
113 	 * arbitrarily long export lists.
114 	 */
115 	cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_n");
116 	if (cl == NULL) {
117 		if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED)
118 			cl = clnt_create(host, MOUNTPROG, MOUNTVERS,
119 					"datagram_n");
120 		if (cl == NULL) {
121 			(void) fprintf(stderr, "nfs dfshares:");
122 			clnt_pcreateerror(host);
123 			(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT,
124 				&rpc_totout_old);
125 			exit(1);
126 		}
127 	}
128 
129 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
130 	tout.tv_sec = 10;
131 	tout.tv_usec = 0;
132 
133 	if (err = clnt_call(cl, MOUNTPROC_EXPORT, xdr_void,
134 	    0, xdr_exports, (caddr_t)&ex, tout)) {
135 		(void) fprintf(stderr, "nfs dfshares: %s\n", clnt_sperrno(err));
136 		clnt_destroy(cl);
137 		exit(1);
138 	}
139 
140 	if (ex == NULL) {
141 		clnt_destroy(cl);
142 		exit(1);
143 	}
144 
145 	if (!hflg) {
146 		printf("%-35s %12s %-8s  %s\n",
147 			"RESOURCE", "SERVER", "ACCESS", "TRANSPORT");
148 		hflg++;
149 	}
150 
151 	while (ex) {
152 		printf("%10s:%-24s %12s %-8s  %s\n",
153 			host, ex->ex_dir, host, " -", " -");
154 		ex = ex->ex_next;
155 	}
156 	free_ex(ex);
157 	clnt_destroy(cl);
158 }
159 
160 void
161 free_ex(ex)
162 	struct exportnode *ex;
163 {
164 	struct groupnode *gr, *tmpgr;
165 	struct exportnode *tmpex;
166 
167 	while (ex) {
168 		free(ex->ex_dir);
169 		gr = ex->ex_groups;
170 		while (gr) {
171 			tmpgr = gr->gr_next;
172 			free((char *)gr);
173 			gr = tmpgr;
174 		}
175 		tmpex = ex;
176 		ex = ex->ex_next;
177 		free((char *)tmpex);
178 	}
179 }
180 
181 void
182 usage()
183 {
184 	(void) fprintf(stderr, "Usage: dfshares [-h] [host ...]\n");
185 }
186