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 /*
41  * nfs dfshares
42  */
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <stdlib.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 <errno.h>
53 #include <nfs/nfs.h>
54 #include <rpcsvc/mount.h>
55 #include <unistd.h>
56 #include <clnt_subr.h>
57 
58 int hflg;
59 void pr_exports(char *);
60 void free_ex(struct exportnode *);
61 void usage(void);
62 
63 int
main(int argc,char * argv[])64 main(int argc, char *argv[])
65 {
66 	char hostbuf[256];
67 	int i, c;
68 
69 	while ((c = getopt(argc, argv, "h")) != EOF) {
70 		switch (c) {
71 		case 'h':
72 			hflg++;
73 			break;
74 		default:
75 			usage();
76 			exit(1);
77 		}
78 	}
79 
80 	if (optind < argc) {
81 		for (i = optind; i < argc; i++)
82 			pr_exports(argv[i]);
83 	} else {
84 		if (gethostname(hostbuf, sizeof (hostbuf)) < 0) {
85 			pr_err("gethostname: %s\n", strerror(errno));
86 			exit(1);
87 		}
88 		pr_exports(hostbuf);
89 	}
90 
91 	return (0);
92 }
93 
94 struct	timeval	rpc_totout_new = {15, 0};
95 
96 void
pr_exports(char * host)97 pr_exports(char *host)
98 {
99 	CLIENT *cl;
100 	struct exportnode *ex = NULL;
101 	enum clnt_stat err;
102 	struct	timeval	tout, rpc_totout_old;
103 
104 	(void) __rpc_control(CLCR_GET_RPCB_TIMEOUT, &rpc_totout_old);
105 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_new);
106 
107 	cl = mountprog_client_create(host, &rpc_totout_old);
108 	if (cl == NULL) {
109 		exit(1);
110 	}
111 
112 	(void) __rpc_control(CLCR_SET_RPCB_TIMEOUT, &rpc_totout_old);
113 	tout.tv_sec = 10;
114 	tout.tv_usec = 0;
115 
116 	err = clnt_call(cl, MOUNTPROC_EXPORT, xdr_void, 0, xdr_exports,
117 	    (caddr_t)&ex, tout);
118 	if (err != 0) {
119 		pr_err("%s\n", clnt_sperrno(err));
120 		clnt_destroy(cl);
121 		exit(1);
122 	}
123 
124 	if (ex == NULL) {
125 		clnt_destroy(cl);
126 		exit(1);
127 	}
128 
129 	if (!hflg) {
130 		printf("%-35s %12s %-8s  %s\n",
131 		    "RESOURCE", "SERVER", "ACCESS", "TRANSPORT");
132 		hflg++;
133 	}
134 
135 	while (ex) {
136 		printf("%10s:%-24s %12s %-8s  %s\n",
137 		    host, ex->ex_dir, host, " -", " -");
138 		ex = ex->ex_next;
139 	}
140 	free_ex(ex);
141 	clnt_destroy(cl);
142 }
143 
144 void
free_ex(struct exportnode * ex)145 free_ex(struct exportnode *ex)
146 {
147 	struct groupnode *gr, *tmpgr;
148 	struct exportnode *tmpex;
149 
150 	while (ex) {
151 		free(ex->ex_dir);
152 		gr = ex->ex_groups;
153 		while (gr) {
154 			tmpgr = gr->gr_next;
155 			free(gr);
156 			gr = tmpgr;
157 		}
158 		tmpex = ex;
159 		ex = ex->ex_next;
160 		free(tmpex);
161 	}
162 }
163 
164 void
usage(void)165 usage(void)
166 {
167 	(void) fprintf(stderr, "Usage: dfshares [-h] [host ...]\n");
168 }
169 
170 void
pr_err(char * fmt,...)171 pr_err(char *fmt, ...)
172 {
173 	va_list ap;
174 
175 	va_start(ap, fmt);
176 	(void) fprintf(stderr, "nfs dfshares: ");
177 	(void) vfprintf(stderr, fmt, ap);
178 	va_end(ap);
179 }
180