Initial import from FreeBSD RELENG_4:
[dragonfly.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: src/usr.bin/showmount/showmount.c,v 1.8 1999/08/28 01:05:43 peter Exp $";
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
219 int 
220 tcp_callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
221         char *host;
222         int prognum;
223         int versnum;
224         int procnum;
225         xdrproc_t inproc;
226         char *in;
227         xdrproc_t outproc;
228         char *out;
229 {
230         struct hostent *hp;
231         struct sockaddr_in server_addr;
232         CLIENT *client;
233         int sock;       
234         struct timeval timeout;
235         int rval;
236         
237         hp = gethostbyname(host);
238
239         if (!hp)
240                 return ((int) RPC_UNKNOWNHOST);
241
242         memset(&server_addr,0,sizeof(server_addr));
243         memcpy((char *) &server_addr.sin_addr,
244                hp->h_addr,
245                hp->h_length);
246         server_addr.sin_len = sizeof(struct sockaddr_in);
247         server_addr.sin_family =AF_INET;
248         server_addr.sin_port = 0;
249                         
250         sock = RPC_ANYSOCK;
251                         
252         client = clnttcp_create(&server_addr,
253                                 (u_long) prognum,
254                                 (u_long) versnum, &sock, 0, 0);
255         if (!client) {
256                 timeout.tv_sec = 5;
257                 timeout.tv_usec = 0;
258                 server_addr.sin_port = 0;
259                 sock = RPC_ANYSOCK;
260                 client = clntudp_create(&server_addr,
261                                         (u_long) prognum,
262                                         (u_long) versnum,
263                                         timeout,
264                                         &sock);
265         }
266         if (!client)
267                 return ((int) rpc_createerr.cf_stat);
268
269         timeout.tv_sec = 25;
270         timeout.tv_usec = 0;
271         rval = (int) clnt_call(client, procnum, 
272                                inproc, in,
273                                outproc, out,
274                                timeout);
275         clnt_destroy(client);
276         return rval;
277 }
278
279 /*
280  * Xdr routine for retrieving the mount dump list
281  */
282 int
283 xdr_mntdump(xdrsp, mlp)
284         XDR *xdrsp;
285         struct mountlist **mlp;
286 {
287         register struct mountlist *mp;
288         register struct mountlist *tp;
289         register struct mountlist **otp;
290         int val, val2;
291         int bool;
292         char *strp;
293
294         *mlp = (struct mountlist *)0;
295         if (!xdr_bool(xdrsp, &bool))
296                 return (0);
297         while (bool) {
298                 mp = (struct mountlist *)malloc(sizeof(struct mountlist));
299                 if (mp == NULL)
300                         return (0);
301                 mp->ml_left = mp->ml_right = (struct mountlist *)0;
302                 strp = mp->ml_host;
303                 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
304                         return (0);
305                 strp = mp->ml_dirp;
306                 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
307                         return (0);
308
309                 /*
310                  * Build a binary tree on sorted order of either host or dirp.
311                  * Drop any duplications.
312                  */
313                 if (*mlp == NULL) {
314                         *mlp = mp;
315                 } else {
316                         tp = *mlp;
317                         while (tp) {
318                                 val = strcmp(mp->ml_host, tp->ml_host);
319                                 val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
320                                 switch (type) {
321                                 case ALL:
322                                         if (val == 0) {
323                                                 if (val2 == 0) {
324                                                         free((caddr_t)mp);
325                                                         goto next;
326                                                 }
327                                                 val = val2;
328                                         }
329                                         break;
330                                 case DIRS:
331                                         if (val2 == 0) {
332                                                 free((caddr_t)mp);
333                                                 goto next;
334                                         }
335                                         val = val2;
336                                         break;
337                                 default:
338                                         if (val == 0) {
339                                                 free((caddr_t)mp);
340                                                 goto next;
341                                         }
342                                         break;
343                                 };
344                                 if (val < 0) {
345                                         otp = &tp->ml_left;
346                                         tp = tp->ml_left;
347                                 } else {
348                                         otp = &tp->ml_right;
349                                         tp = tp->ml_right;
350                                 }
351                         }
352                         *otp = mp;
353                 }
354 next:
355                 if (!xdr_bool(xdrsp, &bool))
356                         return (0);
357         }
358         return (1);
359 }
360
361 /*
362  * Xdr routine to retrieve exports list
363  */
364 int
365 xdr_exports(xdrsp, exp)
366         XDR *xdrsp;
367         struct exportslist **exp;
368 {
369         register struct exportslist *ep;
370         register struct grouplist *gp;
371         int bool, grpbool;
372         char *strp;
373
374         *exp = (struct exportslist *)0;
375         if (!xdr_bool(xdrsp, &bool))
376                 return (0);
377         while (bool) {
378                 ep = (struct exportslist *)malloc(sizeof(struct exportslist));
379                 if (ep == NULL)
380                         return (0);
381                 ep->ex_groups = (struct grouplist *)0;
382                 strp = ep->ex_dirp;
383                 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
384                         return (0);
385                 if (!xdr_bool(xdrsp, &grpbool))
386                         return (0);
387                 while (grpbool) {
388                         gp = (struct grouplist *)malloc(sizeof(struct grouplist));
389                         if (gp == NULL)
390                                 return (0);
391                         strp = gp->gr_name;
392                         if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
393                                 return (0);
394                         gp->gr_next = ep->ex_groups;
395                         ep->ex_groups = gp;
396                         if (!xdr_bool(xdrsp, &grpbool))
397                                 return (0);
398                 }
399                 ep->ex_next = *exp;
400                 *exp = ep;
401                 if (!xdr_bool(xdrsp, &bool))
402                         return (0);
403         }
404         return (1);
405 }
406
407 static void
408 usage()
409 {
410         fprintf(stderr, "usage: showmount [-ade3] host\n");
411         exit(1);
412 }
413
414 /*
415  * Print the binary tree in inorder so that output is sorted.
416  */
417 void
418 print_dump(mp)
419         struct mountlist *mp;
420 {
421
422         if (mp == NULL)
423                 return;
424         if (mp->ml_left)
425                 print_dump(mp->ml_left);
426         switch (type) {
427         case ALL:
428                 printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
429                 break;
430         case DIRS:
431                 printf("%s\n", mp->ml_dirp);
432                 break;
433         default:
434                 printf("%s\n", mp->ml_host);
435                 break;
436         };
437         if (mp->ml_right)
438                 print_dump(mp->ml_right);
439 }