This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / usr.bin / showmount / showmount.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38char copyright[] =
39"@(#) Copyright (c) 1989 Regents of the University of California.\n\
40 All rights reserved.\n";
41#endif not lint
42
43#ifndef lint
44static char sccsid[] = "@(#)showmount.c 6.5 (Berkeley) 3/1/91";
45#endif not lint
46
47#include <sys/types.h>
48#include <sys/file.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <netdb.h>
52#include <rpc/rpc.h>
53#include <rpc/pmap_clnt.h>
54#include <rpc/pmap_prot.h>
55#include <nfs/rpcv2.h>
56#include <stdio.h>
57#include <string.h>
58
59/* Constant defs */
60#define ALL 1
61#define DIRS 2
62
63#define DODUMP 0x1
64#define DOEXPORTS 0x2
65
66struct mountlist {
67 struct mountlist *ml_left;
68 struct mountlist *ml_right;
69 char ml_host[RPCMNT_NAMELEN+1];
70 char ml_dirp[RPCMNT_PATHLEN+1];
71};
72
73struct grouplist {
74 struct grouplist *gr_next;
75 char gr_name[RPCMNT_NAMELEN+1];
76};
77
78struct exportslist {
79 struct exportslist *ex_next;
80 struct grouplist *ex_groups;
81 char ex_dirp[RPCMNT_PATHLEN+1];
82};
83
84static struct mountlist *mntdump;
85static struct exportslist *exports;
86static int type = 0;
87int xdr_mntdump(), xdr_exports();
88
89/*
90 * This command queries the NFS mount daemon for it's mount list and/or
91 * it's exports list and prints them out.
92 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
93 * for detailed information on the protocol.
94 */
95main(argc, argv)
96 int argc;
97 char **argv;
98{
99 register struct mountlist *mntp;
100 register struct exportslist *exp;
101 register struct grouplist *grp;
102 extern char *optarg;
103 extern int optind;
104 register int rpcs = 0;
105 char ch;
106 char *host;
107 int estat;
108
109 while ((ch = getopt(argc, argv, "ade")) != EOF)
110 switch((char)ch) {
111 case 'a':
112 if (type == 0) {
113 type = ALL;
114 rpcs |= DODUMP;
115 } else
116 usage();
117 break;
118 case 'd':
119 if (type == 0) {
120 type = DIRS;
121 rpcs |= DODUMP;
122 } else
123 usage();
124 break;
125 case 'e':
126 rpcs |= DOEXPORTS;
127 break;
128 case '?':
129 default:
130 usage();
131 }
132 argc -= optind;
133 argv += optind;
134
135 if (argc > 0)
136 host = *argv;
137 else
138 host = "localhost";
139
140 if (rpcs == 0)
141 rpcs = DODUMP;
142
143 if (rpcs & DODUMP)
144 if ((estat = callrpc(host, RPCPROG_MNT, RPCMNT_VER1,
145 RPCMNT_DUMP, xdr_void, (char *)0,
146 xdr_mntdump, (char *)&mntdump)) != 0) {
147 clnt_perrno(estat);
148 fprintf(stderr, "Can't do Mountdump rpc\n");
149 exit(1);
150 }
151 if (rpcs & DOEXPORTS)
152 if ((estat = callrpc(host, RPCPROG_MNT, RPCMNT_VER1,
153 RPCMNT_EXPORT, xdr_void, (char *)0,
154 xdr_exports, (char *)&exports)) != 0) {
155 clnt_perrno(estat);
156 fprintf(stderr, "Can't do Exports rpc\n");
157 exit(1);
158 }
159
160 /* Now just print out the results */
161 if (rpcs & DODUMP) {
162 switch (type) {
163 case ALL:
164 printf("All mount points on %s:\n", host);
165 break;
166 case DIRS:
167 printf("Directories on %s:\n", host);
168 break;
169 default:
170 printf("Hosts on %s:\n", host);
171 break;
172 };
173 print_dump(mntdump);
174 }
175 if (rpcs & DOEXPORTS) {
176 printf("Exports list on %s:\n", host);
177 exp = exports;
178 while (exp) {
179 printf("%-35s", exp->ex_dirp);
180 grp = exp->ex_groups;
181 if (grp == NULL) {
182 printf("Everyone\n");
183 } else {
184 while (grp) {
185 printf("%s ", grp->gr_name);
186 grp = grp->gr_next;
187 }
188 printf("\n");
189 }
190 exp = exp->ex_next;
191 }
192 }
193}
194
195/*
196 * Xdr routine for retrieving the mount dump list
197 */
198xdr_mntdump(xdrsp, mlp)
199 XDR *xdrsp;
200 struct mountlist **mlp;
201{
202 register struct mountlist *mp;
203 register struct mountlist *tp;
204 register struct mountlist **otp;
205 int val, val2;
206 int bool;
207 char *strp;
208
209 *mlp = (struct mountlist *)0;
210 if (!xdr_bool(xdrsp, &bool))
211 return (0);
212 while (bool) {
213 mp = (struct mountlist *)malloc(sizeof(struct mountlist));
214 if (mp == NULL)
215 return (0);
216 mp->ml_left = mp->ml_right = (struct mountlist *)0;
217 strp = mp->ml_host;
218 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
219 return (0);
220 strp = mp->ml_dirp;
221 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
222 return (0);
223
224 /*
225 * Build a binary tree on sorted order of either host or dirp.
226 * Drop any duplications.
227 */
228 if (*mlp == NULL) {
229 *mlp = mp;
230 } else {
231 tp = *mlp;
232 while (tp) {
233 val = strcmp(mp->ml_host, tp->ml_host);
234 val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
235 switch (type) {
236 case ALL:
237 if (val == 0) {
238 if (val2 == 0) {
239 free((caddr_t)mp);
240 goto next;
241 }
242 val = val2;
243 }
244 break;
245 case DIRS:
246 if (val2 == 0) {
247 free((caddr_t)mp);
248 goto next;
249 }
250 val = val2;
251 break;
252 default:
253 if (val == 0) {
254 free((caddr_t)mp);
255 goto next;
256 }
257 break;
258 };
259 if (val < 0) {
260 otp = &tp->ml_left;
261 tp = tp->ml_left;
262 } else {
263 otp = &tp->ml_right;
264 tp = tp->ml_right;
265 }
266 }
267 *otp = mp;
268 }
269next:
270 if (!xdr_bool(xdrsp, &bool))
271 return (0);
272 }
273 return (1);
274}
275
276/*
277 * Xdr routine to retrieve exports list
278 */
279xdr_exports(xdrsp, exp)
280 XDR *xdrsp;
281 struct exportslist **exp;
282{
283 register struct exportslist *ep;
284 register struct grouplist *gp;
285 int bool, grpbool;
286 char *strp;
287
288 *exp = (struct exportslist *)0;
289 if (!xdr_bool(xdrsp, &bool))
290 return (0);
291 while (bool) {
292 ep = (struct exportslist *)malloc(sizeof(struct exportslist));
293 if (ep == NULL)
294 return (0);
295 ep->ex_groups = (struct grouplist *)0;
296 strp = ep->ex_dirp;
297 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
298 return (0);
299 if (!xdr_bool(xdrsp, &grpbool))
300 return (0);
301 while (grpbool) {
302 gp = (struct grouplist *)malloc(sizeof(struct grouplist));
303 if (gp == NULL)
304 return (0);
305 strp = gp->gr_name;
306 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
307 return (0);
308 gp->gr_next = ep->ex_groups;
309 ep->ex_groups = gp;
310 if (!xdr_bool(xdrsp, &grpbool))
311 return (0);
312 }
313 ep->ex_next = *exp;
314 *exp = ep;
315 if (!xdr_bool(xdrsp, &bool))
316 return (0);
317 }
318 return (1);
319}
320
321usage()
322{
323 fprintf(stderr, "usage: showmount [-ade] host\n");
324 exit(1);
325}
326
327/*
328 * Print the binary tree in inorder so that output is sorted.
329 */
330print_dump(mp)
331 struct mountlist *mp;
332{
333
334 if (mp == NULL)
335 return;
336 if (mp->ml_left)
337 print_dump(mp->ml_left);
338 switch (type) {
339 case ALL:
340 printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
341 break;
342 case DIRS:
343 printf("%s\n", mp->ml_dirp);
344 break;
345 default:
346 printf("%s\n", mp->ml_host);
347 break;
348 };
349 if (mp->ml_right)
350 print_dump(mp->ml_right);
351}