]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/showmount/showmount.c
remove __P
[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(struct mountlist *);
99 static void usage(void);
100 int xdr_mntdump(XDR *, struct mountlist **);
101 int xdr_exports(XDR *, struct exportslist **);
102 int tcp_callrpc(const char *host, int prognum, int versnum, int procnum,
103                 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out);
104
105 /*
106  * This command queries the NFS mount daemon for it's mount list and/or
107  * it's exports list and prints them out.
108  * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
109  * and the "Network File System Protocol XXX.."
110  * for detailed information on the protocol.
111  */
112 int
113 main(argc, argv)
114         int argc;
115         char **argv;
116 {
117         register struct exportslist *exp;
118         register struct grouplist *grp;
119         register int rpcs = 0, mntvers = 1;
120         char ch;
121         const char *host;
122         int estat;
123
124         while ((ch = getopt(argc, argv, "ade3")) != -1)
125                 switch((char)ch) {
126                 case 'a':
127                         if (type == 0) {
128                                 type = ALL;
129                                 rpcs |= DODUMP;
130                         } else
131                                 usage();
132                         break;
133                 case 'd':
134                         if (type == 0) {
135                                 type = DIRS;
136                                 rpcs |= DODUMP;
137                         } else
138                                 usage();
139                         break;
140                 case 'e':
141                         rpcs |= DOEXPORTS;
142                         break;
143                 case '3':
144                         mntvers = 3;
145                         break;
146                 case '?':
147                 default:
148                         usage();
149                 }
150         argc -= optind;
151         argv += optind;
152
153         if (argc > 0)
154                 host = *argv;
155         else
156                 host = "localhost";
157
158         if (rpcs == 0)
159                 rpcs = DODUMP;
160
161         if (rpcs & DODUMP)
162                 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
163                         RPCMNT_DUMP, xdr_void, (char *)0,
164                         xdr_mntdump, (char *)&mntdump)) != 0) {
165                         clnt_perrno(estat);
166                         errx(1, "can't do mountdump rpc");
167                 }
168         if (rpcs & DOEXPORTS)
169                 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
170                         RPCMNT_EXPORT, xdr_void, (char *)0,
171                         xdr_exports, (char *)&exports)) != 0) {
172                         clnt_perrno(estat);
173                         errx(1, "can't do exports rpc");
174                 }
175
176         /* Now just print out the results */
177         if (rpcs & DODUMP) {
178                 switch (type) {
179                 case ALL:
180                         printf("All mount points on %s:\n", host);
181                         break;
182                 case DIRS:
183                         printf("Directories on %s:\n", host);
184                         break;
185                 default:
186                         printf("Hosts on %s:\n", host);
187                         break;
188                 };
189                 print_dump(mntdump);
190         }
191         if (rpcs & DOEXPORTS) {
192                 printf("Exports list on %s:\n", host);
193                 exp = exports;
194                 while (exp) {
195                         printf("%-35s", exp->ex_dirp);
196                         grp = exp->ex_groups;
197                         if (grp == NULL) {
198                                 printf("Everyone\n");
199                         } else {
200                                 while (grp) {
201                                         printf("%s ", grp->gr_name);
202                                         grp = grp->gr_next;
203                                 }
204                                 printf("\n");
205                         }
206                         exp = exp->ex_next;
207                 }
208         }
209         exit(0);
210 }
211
212 /*
213  * tcp_callrpc has the same interface as callrpc, but tries to
214  * use tcp as transport method in order to handle large replies.
215  */
216 int 
217 tcp_callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
218         const char *host;
219         int prognum;
220         int versnum;
221         int procnum;
222         xdrproc_t inproc;
223         char *in;
224         xdrproc_t outproc;
225         char *out;
226 {
227         CLIENT *client;
228         struct timeval timeout;
229         int rval;
230
231         if ((client = clnt_create(host, prognum, versnum, "tcp")) == NULL &&
232             (client = clnt_create(host, prognum, versnum, "udp")) == NULL)
233                 return ((int) rpc_createerr.cf_stat);
234
235         timeout.tv_sec = 25;
236         timeout.tv_usec = 0;
237         rval = (int) clnt_call(client, procnum, 
238                                inproc, in,
239                                outproc, out,
240                                timeout);
241         clnt_destroy(client);
242         return rval;
243 }
244
245 /*
246  * Xdr routine for retrieving the mount dump list
247  */
248 int
249 xdr_mntdump(xdrsp, mlp)
250         XDR *xdrsp;
251         struct mountlist **mlp;
252 {
253         register struct mountlist *mp;
254         register struct mountlist *tp;
255         register struct mountlist **otp;
256         int val, val2;
257         int bool;
258         char *strp;
259
260         *mlp = (struct mountlist *)0;
261         if (!xdr_bool(xdrsp, &bool))
262                 return (0);
263         while (bool) {
264                 mp = (struct mountlist *)malloc(sizeof(struct mountlist));
265                 if (mp == NULL)
266                         return (0);
267                 mp->ml_left = mp->ml_right = (struct mountlist *)0;
268                 strp = mp->ml_host;
269                 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
270                         return (0);
271                 strp = mp->ml_dirp;
272                 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
273                         return (0);
274
275                 /*
276                  * Build a binary tree on sorted order of either host or dirp.
277                  * Drop any duplications.
278                  */
279                 if (*mlp == NULL) {
280                         *mlp = mp;
281                 } else {
282                         tp = *mlp;
283                         while (tp) {
284                                 val = strcmp(mp->ml_host, tp->ml_host);
285                                 val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
286                                 switch (type) {
287                                 case ALL:
288                                         if (val == 0) {
289                                                 if (val2 == 0) {
290                                                         free((caddr_t)mp);
291                                                         goto next;
292                                                 }
293                                                 val = val2;
294                                         }
295                                         break;
296                                 case DIRS:
297                                         if (val2 == 0) {
298                                                 free((caddr_t)mp);
299                                                 goto next;
300                                         }
301                                         val = val2;
302                                         break;
303                                 default:
304                                         if (val == 0) {
305                                                 free((caddr_t)mp);
306                                                 goto next;
307                                         }
308                                         break;
309                                 };
310                                 if (val < 0) {
311                                         otp = &tp->ml_left;
312                                         tp = tp->ml_left;
313                                 } else {
314                                         otp = &tp->ml_right;
315                                         tp = tp->ml_right;
316                                 }
317                         }
318                         *otp = mp;
319                 }
320 next:
321                 if (!xdr_bool(xdrsp, &bool))
322                         return (0);
323         }
324         return (1);
325 }
326
327 /*
328  * Xdr routine to retrieve exports list
329  */
330 int
331 xdr_exports(xdrsp, exp)
332         XDR *xdrsp;
333         struct exportslist **exp;
334 {
335         register struct exportslist *ep;
336         register struct grouplist *gp;
337         int bool, grpbool;
338         char *strp;
339
340         *exp = (struct exportslist *)0;
341         if (!xdr_bool(xdrsp, &bool))
342                 return (0);
343         while (bool) {
344                 ep = (struct exportslist *)malloc(sizeof(struct exportslist));
345                 if (ep == NULL)
346                         return (0);
347                 ep->ex_groups = (struct grouplist *)0;
348                 strp = ep->ex_dirp;
349                 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
350                         return (0);
351                 if (!xdr_bool(xdrsp, &grpbool))
352                         return (0);
353                 while (grpbool) {
354                         gp = (struct grouplist *)malloc(sizeof(struct grouplist));
355                         if (gp == NULL)
356                                 return (0);
357                         strp = gp->gr_name;
358                         if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
359                                 return (0);
360                         gp->gr_next = ep->ex_groups;
361                         ep->ex_groups = gp;
362                         if (!xdr_bool(xdrsp, &grpbool))
363                                 return (0);
364                 }
365                 ep->ex_next = *exp;
366                 *exp = ep;
367                 if (!xdr_bool(xdrsp, &bool))
368                         return (0);
369         }
370         return (1);
371 }
372
373 static void
374 usage()
375 {
376         fprintf(stderr, "usage: showmount [-ade3] host\n");
377         exit(1);
378 }
379
380 /*
381  * Print the binary tree in inorder so that output is sorted.
382  */
383 void
384 print_dump(mp)
385         struct mountlist *mp;
386 {
387
388         if (mp == NULL)
389                 return;
390         if (mp->ml_left)
391                 print_dump(mp->ml_left);
392         switch (type) {
393         case ALL:
394                 printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
395                 break;
396         case DIRS:
397                 printf("%s\n", mp->ml_dirp);
398                 break;
399         default:
400                 printf("%s\n", mp->ml_host);
401                 break;
402         };
403         if (mp->ml_right)
404                 print_dump(mp->ml_right);
405 }