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