Annotation of src/usr.bin/showmount/showmount.c, Revision 1.22
1.22 ! mpi 1: /* $OpenBSD: showmount.c,v 1.21 2017/01/21 11:32:04 guenther 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: #include <sys/types.h>
37: #include <sys/file.h>
38: #include <sys/socket.h>
39:
40: #include <netdb.h>
41: #include <rpc/rpc.h>
42: #include <rpc/pmap_clnt.h>
43: #include <rpc/pmap_prot.h>
44: #include <nfs/rpcv2.h>
45:
46: #include <stdio.h>
47: #include <stdlib.h>
48: #include <string.h>
49: #include <unistd.h>
1.5 deraadt 50: #include <vis.h>
1.19 deraadt 51: #include <err.h>
1.1 deraadt 52:
53: /* Constant defs */
54: #define ALL 1
55: #define DIRS 2
56:
57: #define DODUMP 0x1
58: #define DOEXPORTS 0x2
59:
60: struct mountlist {
61: struct mountlist *ml_left;
62: struct mountlist *ml_right;
63: char ml_host[RPCMNT_NAMELEN+1];
64: char ml_dirp[RPCMNT_PATHLEN+1];
65: };
66:
67: struct grouplist {
68: struct grouplist *gr_next;
69: char gr_name[RPCMNT_NAMELEN+1];
70: };
71:
72: struct exportslist {
73: struct exportslist *ex_next;
74: struct grouplist *ex_groups;
75: char ex_dirp[RPCMNT_PATHLEN+1];
76: };
77:
78: static struct mountlist *mntdump;
79: static struct exportslist *exports;
80: static int type = 0;
81:
1.10 millert 82: void print_dump(struct mountlist *);
83: void usage(void);
84: int xdr_mntdump(XDR *, struct mountlist **);
85: int xdr_exports(XDR *, struct exportslist **);
1.1 deraadt 86:
87: /*
88: * This command queries the NFS mount daemon for it's mount list and/or
89: * it's exports list and prints them out.
90: * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
91: * and the "Network File System Protocol XXX.."
92: * for detailed information on the protocol.
93: */
94: int
1.13 deraadt 95: main(int argc, char *argv[])
1.1 deraadt 96: {
97: struct exportslist *exp;
98: struct grouplist *grp;
1.6 deraadt 99: struct sockaddr_in clnt_sin;
100: struct hostent *hp;
101: struct timeval timeout;
102: int rpcs = 0, mntvers = 1;
103: enum clnt_stat estat;
104: CLIENT *client;
1.2 deraadt 105: char *host;
1.6 deraadt 106: int ch, clnt_sock;
1.1 deraadt 107:
1.19 deraadt 108: if (pledge("stdio rpath inet dns", NULL) == -1)
109: err(1, "pledge");
110:
1.2 deraadt 111: while ((ch = getopt(argc, argv, "ade3")) != -1)
1.14 deraadt 112: switch (ch) {
1.1 deraadt 113: case 'a':
114: if (type == 0) {
115: type = ALL;
116: rpcs |= DODUMP;
117: } else
118: usage();
119: break;
120: case 'd':
121: if (type == 0) {
122: type = DIRS;
123: rpcs |= DODUMP;
124: } else
125: usage();
126: break;
127: case 'e':
128: rpcs |= DOEXPORTS;
129: break;
130: case '3':
131: mntvers = 3;
132: break;
133: default:
134: usage();
135: }
136: argc -= optind;
137: argv += optind;
138:
139: if (argc > 0)
140: host = *argv;
141: else
142: host = "localhost";
143:
144: if (rpcs == 0)
145: rpcs = DODUMP;
146:
1.6 deraadt 147: if ((hp = gethostbyname(host)) == NULL) {
148: fprintf(stderr, "showmount: unknown host %s\n", host);
149: exit(1);
150: }
151: bzero(&clnt_sin, sizeof clnt_sin);
152: clnt_sin.sin_family = AF_INET;
153: bcopy(hp->h_addr, (char *)&clnt_sin.sin_addr, hp->h_length);
154: clnt_sock = RPC_ANYSOCK;
155: client = clnttcp_create(&clnt_sin, RPCPROG_MNT, mntvers,
156: &clnt_sock, 0, 0);
157: if (client == NULL) {
158: clnt_pcreateerror("showmount: clnttcp_create");
159: exit(1);
160: }
161: timeout.tv_sec = 30;
162: timeout.tv_usec = 0;
1.19 deraadt 163:
164: if (pledge("stdio rpath", NULL) == -1)
165: err(1, "pledge");
1.6 deraadt 166:
167: if (rpcs & DODUMP) {
1.20 krw 168: estat = clnt_call(client, RPCMNT_DUMP, xdr_void, NULL,
1.6 deraadt 169: xdr_mntdump, (char *)&mntdump, timeout);
170: if (estat != RPC_SUCCESS) {
1.3 deraadt 171: fprintf(stderr, "showmount: Can't do Mountdump rpc: ");
1.1 deraadt 172: clnt_perrno(estat);
173: exit(1);
174: }
1.6 deraadt 175: }
176: if (rpcs & DOEXPORTS) {
1.20 krw 177: estat = clnt_call(client, RPCMNT_EXPORT, xdr_void, NULL,
1.6 deraadt 178: xdr_exports, (char *)&exports, timeout);
179: if (estat != RPC_SUCCESS) {
1.3 deraadt 180: fprintf(stderr, "showmount: Can't do Exports rpc: ");
1.1 deraadt 181: clnt_perrno(estat);
182: exit(1);
183: }
1.6 deraadt 184: }
1.1 deraadt 185:
186: /* Now just print out the results */
187: if (rpcs & DODUMP) {
188: switch (type) {
189: case ALL:
190: printf("All mount points on %s:\n", host);
191: break;
192: case DIRS:
193: printf("Directories on %s:\n", host);
194: break;
195: default:
196: printf("Hosts on %s:\n", host);
197: break;
1.9 deraadt 198: }
1.1 deraadt 199: print_dump(mntdump);
200: }
201: if (rpcs & DOEXPORTS) {
1.5 deraadt 202: char vp[(RPCMNT_PATHLEN+1)*4];
203: char vn[(RPCMNT_NAMELEN+1)*4];
204:
1.1 deraadt 205: printf("Exports list on %s:\n", host);
206: exp = exports;
207: while (exp) {
1.11 vincent 208: strnvis(vp, exp->ex_dirp, sizeof vp, VIS_CSTYLE);
1.15 deraadt 209: printf("%-34s ", vp);
1.1 deraadt 210: grp = exp->ex_groups;
211: if (grp == NULL) {
212: printf("Everyone\n");
213: } else {
214: while (grp) {
1.11 vincent 215: strnvis(vn, grp->gr_name, sizeof vn,
216: VIS_CSTYLE);
1.5 deraadt 217: printf("%s ", vn);
1.1 deraadt 218: grp = grp->gr_next;
219: }
220: printf("\n");
221: }
222: exp = exp->ex_next;
223: }
224: }
225:
226: exit(0);
227: }
228:
229: /*
230: * Xdr routine for retrieving the mount dump list
231: */
232: int
1.13 deraadt 233: xdr_mntdump(XDR *xdrsp, struct mountlist **mlp)
1.1 deraadt 234: {
1.7 deraadt 235: struct mountlist *mp, **otp = NULL, *tp;
1.1 deraadt 236: int bool, val, val2;
237: char *strp;
238:
1.20 krw 239: *mlp = NULL;
1.1 deraadt 240: if (!xdr_bool(xdrsp, &bool))
241: return (0);
242: while (bool) {
1.18 deraadt 243: mp = malloc(sizeof(struct mountlist));
1.1 deraadt 244: if (mp == NULL)
245: return (0);
1.20 krw 246: mp->ml_left = mp->ml_right = NULL;
1.1 deraadt 247: strp = mp->ml_host;
248: if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
249: return (0);
250: strp = mp->ml_dirp;
251: if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
252: return (0);
253:
254: /*
255: * Build a binary tree on sorted order of either host or dirp.
256: * Drop any duplications.
257: */
258: if (*mlp == NULL) {
259: *mlp = mp;
260: } else {
261: tp = *mlp;
262: while (tp) {
263: val = strcmp(mp->ml_host, tp->ml_host);
264: val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
265: switch (type) {
266: case ALL:
267: if (val == 0) {
268: if (val2 == 0) {
269: free((caddr_t)mp);
270: goto next;
271: }
272: val = val2;
273: }
274: break;
275: case DIRS:
276: if (val2 == 0) {
277: free((caddr_t)mp);
278: goto next;
279: }
280: val = val2;
281: break;
282: default:
283: if (val == 0) {
284: free((caddr_t)mp);
285: goto next;
286: }
287: break;
1.7 deraadt 288: }
1.1 deraadt 289: if (val < 0) {
290: otp = &tp->ml_left;
291: tp = tp->ml_left;
292: } else {
293: otp = &tp->ml_right;
294: tp = tp->ml_right;
295: }
296: }
297: *otp = mp;
298: }
299: next:
300: if (!xdr_bool(xdrsp, &bool))
301: return (0);
302: }
303: return (1);
304: }
305:
306: /*
307: * Xdr routine to retrieve exports list
308: */
309: int
1.13 deraadt 310: xdr_exports(XDR *xdrsp, struct exportslist **exp)
1.1 deraadt 311: {
312: struct exportslist *ep;
313: struct grouplist *gp;
314: int bool, grpbool;
315: char *strp;
316:
1.20 krw 317: *exp = NULL;
1.1 deraadt 318: if (!xdr_bool(xdrsp, &bool))
319: return (0);
320: while (bool) {
1.18 deraadt 321: ep = malloc(sizeof(struct exportslist));
1.1 deraadt 322: if (ep == NULL)
323: return (0);
1.20 krw 324: ep->ex_groups = NULL;
1.1 deraadt 325: strp = ep->ex_dirp;
326: if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
327: return (0);
328: if (!xdr_bool(xdrsp, &grpbool))
329: return (0);
330: while (grpbool) {
1.18 deraadt 331: gp = malloc(sizeof(struct grouplist));
1.1 deraadt 332: if (gp == NULL)
333: return (0);
334: strp = gp->gr_name;
335: if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
336: return (0);
337: gp->gr_next = ep->ex_groups;
338: ep->ex_groups = gp;
339: if (!xdr_bool(xdrsp, &grpbool))
340: return (0);
341: }
342: ep->ex_next = *exp;
343: *exp = ep;
344: if (!xdr_bool(xdrsp, &bool))
345: return (0);
346: }
347: return (1);
348: }
349:
350: void
1.13 deraadt 351: usage(void)
1.1 deraadt 352: {
353:
1.16 jmc 354: fprintf(stderr, "usage: showmount [-3ade] [host]\n");
1.1 deraadt 355: exit(1);
356: }
357:
358: /*
359: * Print the binary tree in inorder so that output is sorted.
360: */
361: void
1.13 deraadt 362: print_dump(struct mountlist *mp)
1.1 deraadt 363: {
1.5 deraadt 364: char vn[(RPCMNT_NAMELEN+1)*4];
365: char vp[(RPCMNT_PATHLEN+1)*4];
1.1 deraadt 366:
367: if (mp == NULL)
368: return;
369: if (mp->ml_left)
370: print_dump(mp->ml_left);
371: switch (type) {
372: case ALL:
1.5 deraadt 373: strvis(vn, mp->ml_host, VIS_CSTYLE);
374: strvis(vp, mp->ml_dirp, VIS_CSTYLE);
375: printf("%s:%s\n", vn, vp);
1.1 deraadt 376: break;
377: case DIRS:
1.5 deraadt 378: strvis(vp, mp->ml_dirp, VIS_CSTYLE);
379: printf("%s\n", vp);
1.1 deraadt 380: break;
381: default:
1.5 deraadt 382: strvis(vn, mp->ml_host, VIS_CSTYLE);
383: printf("%s\n", vn);
1.1 deraadt 384: break;
1.9 deraadt 385: }
1.1 deraadt 386: if (mp->ml_right)
387: print_dump(mp->ml_right);
388: }