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