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