Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * @(#) Copyright (c) 1989, 1993, 1995 The Regents of the University of California.  All rights reserved.
37  * @(#)showmount.c      8.3 (Berkeley) 3/29/95
38  * $FreeBSD: src/usr.bin/showmount/showmount.c,v 1.8 1999/08/28 01:05:43 peter Exp $
39  * $DragonFly: src/usr.bin/showmount/showmount.c,v 1.2 2003/06/17 04:29:31 dillon Exp $
40  */
41
42 #include <sys/types.h>
43 #include <sys/queue.h>
44 #include <sys/file.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47
48 #include <err.h>
49 #include <netdb.h>
50 #include <rpc/rpc.h>
51 #include <rpc/pmap_clnt.h>
52 #include <rpc/pmap_prot.h>
53 #include <nfs/rpcv2.h>
54
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 /* Constant defs */
61 #define ALL     1
62 #define DIRS    2
63
64 #define DODUMP          0x1
65 #define DOEXPORTS       0x2
66
67 struct mountlist {
68         struct mountlist *ml_left;
69         struct mountlist *ml_right;
70         char    ml_host[RPCMNT_NAMELEN+1];
71         char    ml_dirp[RPCMNT_PATHLEN+1];
72 };
73
74 struct grouplist {
75         struct grouplist *gr_next;
76         char    gr_name[RPCMNT_NAMELEN+1];
77 };
78
79 struct exportslist {
80         struct exportslist *ex_next;
81         struct grouplist *ex_groups;
82         char    ex_dirp[RPCMNT_PATHLEN+1];
83 };
84
85 static struct mountlist *mntdump;
86 static struct exportslist *exports;
87 static int type = 0;
88
89 void print_dump __P((struct mountlist *));
90 static void usage __P((void));
91 int xdr_mntdump __P((XDR *, struct mountlist **));
92 int xdr_exports __P((XDR *, struct exportslist **));
93 int tcp_callrpc __P((char *host,
94                      int prognum, int versnum, int procnum,
95                      xdrproc_t inproc, char *in,
96                      xdrproc_t outproc, char *out));
97
98 /*
99  * This command queries the NFS mount daemon for it's mount list and/or
100  * it's exports list and prints them out.
101  * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A"
102  * and the "Network File System Protocol XXX.."
103  * for detailed information on the protocol.
104  */
105 int
106 main(argc, argv)
107         int argc;
108         char **argv;
109 {
110         register struct exportslist *exp;
111         register struct grouplist *grp;
112         register int rpcs = 0, mntvers = 1;
113         char ch;
114         char *host;
115         int estat;
116
117         while ((ch = getopt(argc, argv, "ade3")) != -1)
118                 switch((char)ch) {
119                 case 'a':
120                         if (type == 0) {
121                                 type = ALL;
122                                 rpcs |= DODUMP;
123                         } else
124                                 usage();
125                         break;
126                 case 'd':
127                         if (type == 0) {
128                                 type = DIRS;
129                                 rpcs |= DODUMP;
130                         } else
131                                 usage();
132                         break;
133                 case 'e':
134                         rpcs |= DOEXPORTS;
135                         break;
136                 case '3':
137                         mntvers = 3;
138                         break;
139                 case '?':
140                 default:
141                         usage();
142                 }
143         argc -= optind;
144         argv += optind;
145
146         if (argc > 0)
147                 host = *argv;
148         else
149                 host = "localhost";
150
151         if (rpcs == 0)
152                 rpcs = DODUMP;
153
154         if (rpcs & DODUMP)
155                 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
156                         RPCMNT_DUMP, xdr_void, (char *)0,
157                         xdr_mntdump, (char *)&mntdump)) != 0) {
158                         clnt_perrno(estat);
159                         errx(1, "can't do mountdump rpc");
160                 }
161         if (rpcs & DOEXPORTS)
162                 if ((estat = tcp_callrpc(host, RPCPROG_MNT, mntvers,
163                         RPCMNT_EXPORT, xdr_void, (char *)0,
164                         xdr_exports, (char *)&exports)) != 0) {
165                         clnt_perrno(estat);
166                         errx(1, "can't do exports rpc");
167                 }
168
169         /* Now just print out the results */
170         if (rpcs & DODUMP) {
171                 switch (type) {
172                 case ALL:
173                         printf("All mount points on %s:\n", host);
174                         break;
175                 case DIRS:
176                         printf("Directories on %s:\n", host);
177                         break;
178                 default:
179                         printf("Hosts on %s:\n", host);
180                         break;
181                 };
182                 print_dump(mntdump);
183         }
184         if (rpcs & DOEXPORTS) {
185                 printf("Exports list on %s:\n", host);
186                 exp = exports;
187                 while (exp) {
188                         printf("%-35s", exp->ex_dirp);
189                         grp = exp->ex_groups;
190                         if (grp == NULL) {
191                                 printf("Everyone\n");
192                         } else {
193                                 while (grp) {
194                                         printf("%s ", grp->gr_name);
195                                         grp = grp->gr_next;
196                                 }
197                                 printf("\n");
198                         }
199                         exp = exp->ex_next;
200                 }
201         }
202         exit(0);
203 }
204
205 /*
206  * tcp_callrpc has the same interface as callrpc, but tries to
207  * use tcp as transport method in order to handle large replies.
208  */
209
210 int 
211 tcp_callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
212         char *host;
213         int prognum;
214         int versnum;
215         int procnum;
216         xdrproc_t inproc;
217         char *in;
218         xdrproc_t outproc;
219         char *out;
220 {
221         struct hostent *hp;
222         struct sockaddr_in server_addr;
223         CLIENT *client;
224         int sock;       
225         struct timeval timeout;
226         int rval;
227         
228         hp = gethostbyname(host);
229
230         if (!hp)
231                 return ((int) RPC_UNKNOWNHOST);
232
233         memset(&server_addr,0,sizeof(server_addr));
234         memcpy((char *) &server_addr.sin_addr,
235                hp->h_addr,
236                hp->h_length);
237         server_addr.sin_len = sizeof(struct sockaddr_in);
238         server_addr.sin_family =AF_INET;
239         server_addr.sin_port = 0;
240                         
241         sock = RPC_ANYSOCK;
242                         
243         client = clnttcp_create(&server_addr,
244                                 (u_long) prognum,
245                                 (u_long) versnum, &sock, 0, 0);
246         if (!client) {
247                 timeout.tv_sec = 5;
248                 timeout.tv_usec = 0;
249                 server_addr.sin_port = 0;
250                 sock = RPC_ANYSOCK;
251                 client = clntudp_create(&server_addr,
252                                         (u_long) prognum,
253                                         (u_long) versnum,
254                                         timeout,
255                                         &sock);
256         }
257         if (!client)
258                 return ((int) rpc_createerr.cf_stat);
259
260         timeout.tv_sec = 25;
261         timeout.tv_usec = 0;
262         rval = (int) clnt_call(client, procnum, 
263                                inproc, in,
264                                outproc, out,
265                                timeout);
266         clnt_destroy(client);
267         return rval;
268 }
269
270 /*
271  * Xdr routine for retrieving the mount dump list
272  */
273 int
274 xdr_mntdump(xdrsp, mlp)
275         XDR *xdrsp;
276         struct mountlist **mlp;
277 {
278         register struct mountlist *mp;
279         register struct mountlist *tp;
280         register struct mountlist **otp;
281         int val, val2;
282         int bool;
283         char *strp;
284
285         *mlp = (struct mountlist *)0;
286         if (!xdr_bool(xdrsp, &bool))
287                 return (0);
288         while (bool) {
289                 mp = (struct mountlist *)malloc(sizeof(struct mountlist));
290                 if (mp == NULL)
291                         return (0);
292                 mp->ml_left = mp->ml_right = (struct mountlist *)0;
293                 strp = mp->ml_host;
294                 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
295                         return (0);
296                 strp = mp->ml_dirp;
297                 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
298                         return (0);
299
300                 /*
301                  * Build a binary tree on sorted order of either host or dirp.
302                  * Drop any duplications.
303                  */
304                 if (*mlp == NULL) {
305                         *mlp = mp;
306                 } else {
307                         tp = *mlp;
308                         while (tp) {
309                                 val = strcmp(mp->ml_host, tp->ml_host);
310                                 val2 = strcmp(mp->ml_dirp, tp->ml_dirp);
311                                 switch (type) {
312                                 case ALL:
313                                         if (val == 0) {
314                                                 if (val2 == 0) {
315                                                         free((caddr_t)mp);
316                                                         goto next;
317                                                 }
318                                                 val = val2;
319                                         }
320                                         break;
321                                 case DIRS:
322                                         if (val2 == 0) {
323                                                 free((caddr_t)mp);
324                                                 goto next;
325                                         }
326                                         val = val2;
327                                         break;
328                                 default:
329                                         if (val == 0) {
330                                                 free((caddr_t)mp);
331                                                 goto next;
332                                         }
333                                         break;
334                                 };
335                                 if (val < 0) {
336                                         otp = &tp->ml_left;
337                                         tp = tp->ml_left;
338                                 } else {
339                                         otp = &tp->ml_right;
340                                         tp = tp->ml_right;
341                                 }
342                         }
343                         *otp = mp;
344                 }
345 next:
346                 if (!xdr_bool(xdrsp, &bool))
347                         return (0);
348         }
349         return (1);
350 }
351
352 /*
353  * Xdr routine to retrieve exports list
354  */
355 int
356 xdr_exports(xdrsp, exp)
357         XDR *xdrsp;
358         struct exportslist **exp;
359 {
360         register struct exportslist *ep;
361         register struct grouplist *gp;
362         int bool, grpbool;
363         char *strp;
364
365         *exp = (struct exportslist *)0;
366         if (!xdr_bool(xdrsp, &bool))
367                 return (0);
368         while (bool) {
369                 ep = (struct exportslist *)malloc(sizeof(struct exportslist));
370                 if (ep == NULL)
371                         return (0);
372                 ep->ex_groups = (struct grouplist *)0;
373                 strp = ep->ex_dirp;
374                 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
375                         return (0);
376                 if (!xdr_bool(xdrsp, &grpbool))
377                         return (0);
378                 while (grpbool) {
379                         gp = (struct grouplist *)malloc(sizeof(struct grouplist));
380                         if (gp == NULL)
381                                 return (0);
382                         strp = gp->gr_name;
383                         if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
384                                 return (0);
385                         gp->gr_next = ep->ex_groups;
386                         ep->ex_groups = gp;
387                         if (!xdr_bool(xdrsp, &grpbool))
388                                 return (0);
389                 }
390                 ep->ex_next = *exp;
391                 *exp = ep;
392                 if (!xdr_bool(xdrsp, &bool))
393                         return (0);
394         }
395         return (1);
396 }
397
398 static void
399 usage()
400 {
401         fprintf(stderr, "usage: showmount [-ade3] host\n");
402         exit(1);
403 }
404
405 /*
406  * Print the binary tree in inorder so that output is sorted.
407  */
408 void
409 print_dump(mp)
410         struct mountlist *mp;
411 {
412
413         if (mp == NULL)
414                 return;
415         if (mp->ml_left)
416                 print_dump(mp->ml_left);
417         switch (type) {
418         case ALL:
419                 printf("%s:%s\n", mp->ml_host, mp->ml_dirp);
420                 break;
421         case DIRS:
422                 printf("%s\n", mp->ml_dirp);
423                 break;
424         default:
425                 printf("%s\n", mp->ml_host);
426                 break;
427         };
428         if (mp->ml_right)
429                 print_dump(mp->ml_right);
430 }