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