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