Annotation of src/usr.bin/showmount/showmount.c, Revision 1.2
1.2 ! deraadt 1: /* $NetBSD: showmount.c,v 1.6 1996/04/01 21:32:19 mark Exp $ */
1.1 deraadt 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
1.2 ! deraadt 49: static char rcsid[] = "$NetBSD: showmount.c,v 1.6 1996/04/01 21:32:19 mark Exp $";
1.1 deraadt 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;
1.2 ! deraadt 117: char *host;
! 118: int ch;
1.1 deraadt 119:
1.2 ! deraadt 120: while ((ch = getopt(argc, argv, "ade3")) != -1)
1.1 deraadt 121: switch((char)ch) {
122: case 'a':
123: if (type == 0) {
124: type = ALL;
125: rpcs |= DODUMP;
126: } else
127: usage();
128: break;
129: case 'd':
130: if (type == 0) {
131: type = DIRS;
132: rpcs |= DODUMP;
133: } else
134: usage();
135: break;
136: case 'e':
137: rpcs |= DOEXPORTS;
138: break;
139: case '3':
140: mntvers = 3;
141: break;
142: case '?':
143: default:
144: usage();
145: }
146: argc -= optind;
147: argv += optind;
148:
149: if (argc > 0)
150: host = *argv;
151: else
152: host = "localhost";
153:
154: if (rpcs == 0)
155: rpcs = DODUMP;
156:
157: if (rpcs & DODUMP)
158: if ((estat = callrpc(host, RPCPROG_MNT, mntvers,
159: RPCMNT_DUMP, xdr_void, (char *)0,
160: xdr_mntdump, (char *)&mntdump)) != 0) {
161: clnt_perrno(estat);
162: fprintf(stderr, ": Can't do Mountdump rpc\n");
163: exit(1);
164: }
165: if (rpcs & DOEXPORTS)
166: if ((estat = callrpc(host, RPCPROG_MNT, mntvers,
167: RPCMNT_EXPORT, xdr_void, (char *)0,
168: xdr_exports, (char *)&exports)) != 0) {
169: clnt_perrno(estat);
170: fprintf(stderr, ": Can't do Exports rpc\n");
171: exit(1);
172: }
173:
174: /* Now just print out the results */
175: if (rpcs & DODUMP) {
176: switch (type) {
177: case ALL:
178: printf("All mount points on %s:\n", host);
179: break;
180: case DIRS:
181: printf("Directories on %s:\n", host);
182: break;
183: default:
184: printf("Hosts on %s:\n", host);
185: break;
186: };
187: print_dump(mntdump);
188: }
189: if (rpcs & DOEXPORTS) {
190: printf("Exports list on %s:\n", host);
191: exp = exports;
192: while (exp) {
193: printf("%-35s", exp->ex_dirp);
194: grp = exp->ex_groups;
195: if (grp == NULL) {
196: printf("Everyone\n");
197: } else {
198: while (grp) {
199: printf("%s ", grp->gr_name);
200: grp = grp->gr_next;
201: }
202: printf("\n");
203: }
204: exp = exp->ex_next;
205: }
206: }
207:
208: exit(0);
209: }
210:
211: /*
212: * Xdr routine for retrieving the mount dump list
213: */
214: int
215: xdr_mntdump(xdrsp, mlp)
216: XDR *xdrsp;
217: struct mountlist **mlp;
218: {
219: struct mountlist *mp, **otp, *tp;
220: int bool, val, val2;
221: char *strp;
222:
223: *mlp = (struct mountlist *)0;
224: if (!xdr_bool(xdrsp, &bool))
225: return (0);
226: while (bool) {
227: mp = (struct mountlist *)malloc(sizeof(struct mountlist));
228: if (mp == NULL)
229: return (0);
230: mp->ml_left = mp->ml_right = (struct mountlist *)0;
231: strp = mp->ml_host;
232: if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
233: return (0);
234: strp = mp->ml_dirp;
235: if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
236: return (0);
237:
238: /*
239: * Build a binary tree on sorted order of either host or dirp.
240: * Drop any duplications.
241: */
242: if (*mlp == NULL) {
243: *mlp = mp;
244: } else {
245: tp = *mlp;
246: while (tp) {
247: val = strcmp(mp->ml_host, tp->ml_host);
248: val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
249: switch (type) {
250: case ALL:
251: if (val == 0) {
252: if (val2 == 0) {
253: free((caddr_t)mp);
254: goto next;
255: }
256: val = val2;
257: }
258: break;
259: case DIRS:
260: if (val2 == 0) {
261: free((caddr_t)mp);
262: goto next;
263: }
264: val = val2;
265: break;
266: default:
267: if (val == 0) {
268: free((caddr_t)mp);
269: goto next;
270: }
271: break;
272: };
273: if (val < 0) {
274: otp = &tp->ml_left;
275: tp = tp->ml_left;
276: } else {
277: otp = &tp->ml_right;
278: tp = tp->ml_right;
279: }
280: }
281: *otp = mp;
282: }
283: next:
284: if (!xdr_bool(xdrsp, &bool))
285: return (0);
286: }
287: return (1);
288: }
289:
290: /*
291: * Xdr routine to retrieve exports list
292: */
293: int
294: xdr_exports(xdrsp, exp)
295: XDR *xdrsp;
296: struct exportslist **exp;
297: {
298: struct exportslist *ep;
299: struct grouplist *gp;
300: int bool, grpbool;
301: char *strp;
302:
303: *exp = (struct exportslist *)0;
304: if (!xdr_bool(xdrsp, &bool))
305: return (0);
306: while (bool) {
307: ep = (struct exportslist *)malloc(sizeof(struct exportslist));
308: if (ep == NULL)
309: return (0);
310: ep->ex_groups = (struct grouplist *)0;
311: strp = ep->ex_dirp;
312: if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
313: return (0);
314: if (!xdr_bool(xdrsp, &grpbool))
315: return (0);
316: while (grpbool) {
317: gp = (struct grouplist *)malloc(sizeof(struct grouplist));
318: if (gp == NULL)
319: return (0);
320: strp = gp->gr_name;
321: if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
322: return (0);
323: gp->gr_next = ep->ex_groups;
324: ep->ex_groups = gp;
325: if (!xdr_bool(xdrsp, &grpbool))
326: return (0);
327: }
328: ep->ex_next = *exp;
329: *exp = ep;
330: if (!xdr_bool(xdrsp, &bool))
331: return (0);
332: }
333: return (1);
334: }
335:
336: void
337: usage()
338: {
339:
340: fprintf(stderr, "usage: showmount [-ade] host\n");
341: exit(1);
342: }
343:
344: /*
345: * Print the binary tree in inorder so that output is sorted.
346: */
347: void
348: print_dump(mp)
349: struct mountlist *mp;
350: {
351:
352: if (mp == NULL)
353: return;
354: if (mp->ml_left)
355: print_dump(mp->ml_left);
356: switch (type) {
357: case ALL:
358: printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
359: break;
360: case DIRS:
361: printf("%s\n", mp->ml_dirp);
362: break;
363: default:
364: printf("%s\n", mp->ml_host);
365: break;
366: };
367: if (mp->ml_right)
368: print_dump(mp->ml_right);
369: }