showmount.c revision 200462
11590Srgrimes/* 228068Scharnier * Copyright (c) 1989, 1993, 1995 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Rick Macklem at The University of Guelph. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 371590Srgrimes#ifndef lint 3828068Scharnierstatic const char copyright[] = 3928068Scharnier"@(#) Copyright (c) 1989, 1993, 1995\n\ 401590Srgrimes The Regents of the University of California. All rights reserved.\n"; 4178130Sdd#endif /* not lint */ 421590Srgrimes 431590Srgrimes#ifndef lint 4428068Scharnier#if 0 4528068Scharnierstatic char sccsid[] = "@(#)showmount.c 8.3 (Berkeley) 3/29/95"; 4628068Scharnier#endif 4728068Scharnierstatic const char rcsid[] = 4850477Speter "$FreeBSD: head/usr.bin/showmount/showmount.c 200462 2009-12-13 03:14:06Z delphij $"; 4978130Sdd#endif /* not lint */ 501590Srgrimes 511590Srgrimes#include <sys/types.h> 5214542Sdg#include <sys/queue.h> 531590Srgrimes#include <sys/file.h> 541590Srgrimes#include <sys/socket.h> 551590Srgrimes#include <sys/socketvar.h> 5628068Scharnier 5728068Scharnier#include <err.h> 58200462Sdelphij#include <netdb.h> 591590Srgrimes#include <rpc/rpc.h> 601590Srgrimes#include <rpc/pmap_clnt.h> 611590Srgrimes#include <rpc/pmap_prot.h> 62194880Sdfr#include <rpcsvc/mount.h> 6328068Scharnier 641590Srgrimes#include <stdio.h> 6528068Scharnier#include <stdlib.h> 661590Srgrimes#include <string.h> 6728068Scharnier#include <unistd.h> 681590Srgrimes 691590Srgrimes/* Constant defs */ 701590Srgrimes#define ALL 1 711590Srgrimes#define DIRS 2 721590Srgrimes 731590Srgrimes#define DODUMP 0x1 741590Srgrimes#define DOEXPORTS 0x2 751590Srgrimes 761590Srgrimesstruct mountlist { 771590Srgrimes struct mountlist *ml_left; 781590Srgrimes struct mountlist *ml_right; 79194880Sdfr char ml_host[MNTNAMLEN+1]; 80194880Sdfr char ml_dirp[MNTPATHLEN+1]; 811590Srgrimes}; 821590Srgrimes 831590Srgrimesstruct grouplist { 841590Srgrimes struct grouplist *gr_next; 85194880Sdfr char gr_name[MNTNAMLEN+1]; 861590Srgrimes}; 871590Srgrimes 881590Srgrimesstruct exportslist { 891590Srgrimes struct exportslist *ex_next; 901590Srgrimes struct grouplist *ex_groups; 91194880Sdfr char ex_dirp[MNTPATHLEN+1]; 921590Srgrimes}; 931590Srgrimes 941590Srgrimesstatic struct mountlist *mntdump; 95194880Sdfrstatic struct exportslist *exportslist; 961590Srgrimesstatic int type = 0; 971590Srgrimes 9892922Simpvoid print_dump(struct mountlist *); 9992922Simpstatic void usage(void); 10092922Simpint xdr_mntdump(XDR *, struct mountlist **); 101194880Sdfrint xdr_exportslist(XDR *, struct exportslist **); 10292922Simpint tcp_callrpc(const char *host, int prognum, int versnum, int procnum, 10392922Simp xdrproc_t inproc, char *in, xdrproc_t outproc, char *out); 10428068Scharnier 1051590Srgrimes/* 1061590Srgrimes * This command queries the NFS mount daemon for it's mount list and/or 1071590Srgrimes * it's exports list and prints them out. 1081590Srgrimes * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A" 1099336Sdfr * and the "Network File System Protocol XXX.." 1101590Srgrimes * for detailed information on the protocol. 1111590Srgrimes */ 11228068Scharnierint 1131590Srgrimesmain(argc, argv) 1141590Srgrimes int argc; 1151590Srgrimes char **argv; 1161590Srgrimes{ 1171590Srgrimes register struct exportslist *exp; 1181590Srgrimes register struct grouplist *grp; 1199336Sdfr register int rpcs = 0, mntvers = 1; 12087297Sdwmalone const char *host; 121132660Sstefanf int ch, estat; 1221590Srgrimes 12324360Simp while ((ch = getopt(argc, argv, "ade3")) != -1) 124132660Sstefanf switch (ch) { 1251590Srgrimes case 'a': 1261590Srgrimes if (type == 0) { 1271590Srgrimes type = ALL; 1281590Srgrimes rpcs |= DODUMP; 1291590Srgrimes } else 1301590Srgrimes usage(); 1311590Srgrimes break; 1321590Srgrimes case 'd': 1331590Srgrimes if (type == 0) { 1341590Srgrimes type = DIRS; 1351590Srgrimes rpcs |= DODUMP; 1361590Srgrimes } else 1371590Srgrimes usage(); 1381590Srgrimes break; 1391590Srgrimes case 'e': 1401590Srgrimes rpcs |= DOEXPORTS; 1411590Srgrimes break; 1429336Sdfr case '3': 1439336Sdfr mntvers = 3; 1449336Sdfr break; 1451590Srgrimes case '?': 1461590Srgrimes default: 1471590Srgrimes usage(); 1481590Srgrimes } 1491590Srgrimes argc -= optind; 1501590Srgrimes argv += optind; 1511590Srgrimes 1521590Srgrimes if (argc > 0) 1531590Srgrimes host = *argv; 1541590Srgrimes else 1551590Srgrimes host = "localhost"; 1561590Srgrimes 1571590Srgrimes if (rpcs == 0) 1581590Srgrimes rpcs = DODUMP; 1591590Srgrimes 1601590Srgrimes if (rpcs & DODUMP) 161194880Sdfr if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers, 162194880Sdfr MOUNTPROC_DUMP, (xdrproc_t)xdr_void, (char *)0, 163121546Speter (xdrproc_t)xdr_mntdump, (char *)&mntdump)) != 0) { 1641590Srgrimes clnt_perrno(estat); 16528068Scharnier errx(1, "can't do mountdump rpc"); 1661590Srgrimes } 1671590Srgrimes if (rpcs & DOEXPORTS) 168194880Sdfr if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers, 169194880Sdfr MOUNTPROC_EXPORT, (xdrproc_t)xdr_void, (char *)0, 170194880Sdfr (xdrproc_t)xdr_exportslist, (char *)&exportslist)) != 0) { 1711590Srgrimes clnt_perrno(estat); 17228068Scharnier errx(1, "can't do exports rpc"); 1731590Srgrimes } 1741590Srgrimes 1751590Srgrimes /* Now just print out the results */ 1761590Srgrimes if (rpcs & DODUMP) { 1771590Srgrimes switch (type) { 1781590Srgrimes case ALL: 1791590Srgrimes printf("All mount points on %s:\n", host); 1801590Srgrimes break; 1811590Srgrimes case DIRS: 1821590Srgrimes printf("Directories on %s:\n", host); 1831590Srgrimes break; 1841590Srgrimes default: 1851590Srgrimes printf("Hosts on %s:\n", host); 1861590Srgrimes break; 1871590Srgrimes }; 1881590Srgrimes print_dump(mntdump); 1891590Srgrimes } 1901590Srgrimes if (rpcs & DOEXPORTS) { 1911590Srgrimes printf("Exports list on %s:\n", host); 192194880Sdfr exp = exportslist; 1931590Srgrimes while (exp) { 1941590Srgrimes printf("%-35s", exp->ex_dirp); 1951590Srgrimes grp = exp->ex_groups; 1961590Srgrimes if (grp == NULL) { 1971590Srgrimes printf("Everyone\n"); 1981590Srgrimes } else { 1991590Srgrimes while (grp) { 2001590Srgrimes printf("%s ", grp->gr_name); 2011590Srgrimes grp = grp->gr_next; 2021590Srgrimes } 2031590Srgrimes printf("\n"); 2041590Srgrimes } 2051590Srgrimes exp = exp->ex_next; 2061590Srgrimes } 2071590Srgrimes } 20828068Scharnier exit(0); 2091590Srgrimes} 2101590Srgrimes 2111590Srgrimes/* 21229117Stegge * tcp_callrpc has the same interface as callrpc, but tries to 21329117Stegge * use tcp as transport method in order to handle large replies. 21429117Stegge */ 21529117Steggeint 21629117Steggetcp_callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) 21787297Sdwmalone const char *host; 21829117Stegge int prognum; 21929117Stegge int versnum; 22029117Stegge int procnum; 22129117Stegge xdrproc_t inproc; 22229117Stegge char *in; 22329117Stegge xdrproc_t outproc; 22429117Stegge char *out; 22529117Stegge{ 22629117Stegge CLIENT *client; 22729117Stegge struct timeval timeout; 22829117Stegge int rval; 22929117Stegge 23075239Siedowse if ((client = clnt_create(host, prognum, versnum, "tcp")) == NULL && 23175239Siedowse (client = clnt_create(host, prognum, versnum, "udp")) == NULL) 23229117Stegge return ((int) rpc_createerr.cf_stat); 23329117Stegge 23429117Stegge timeout.tv_sec = 25; 23529117Stegge timeout.tv_usec = 0; 23629117Stegge rval = (int) clnt_call(client, procnum, 23729117Stegge inproc, in, 23829117Stegge outproc, out, 23929117Stegge timeout); 24029117Stegge clnt_destroy(client); 24129117Stegge return rval; 24229117Stegge} 24329117Stegge 24429117Stegge/* 2451590Srgrimes * Xdr routine for retrieving the mount dump list 2461590Srgrimes */ 24728068Scharnierint 2481590Srgrimesxdr_mntdump(xdrsp, mlp) 2491590Srgrimes XDR *xdrsp; 2501590Srgrimes struct mountlist **mlp; 2511590Srgrimes{ 2521590Srgrimes register struct mountlist *mp; 2531590Srgrimes register struct mountlist *tp; 2541590Srgrimes register struct mountlist **otp; 2551590Srgrimes int val, val2; 2561590Srgrimes int bool; 2571590Srgrimes char *strp; 2581590Srgrimes 2591590Srgrimes *mlp = (struct mountlist *)0; 2601590Srgrimes if (!xdr_bool(xdrsp, &bool)) 2611590Srgrimes return (0); 2621590Srgrimes while (bool) { 2631590Srgrimes mp = (struct mountlist *)malloc(sizeof(struct mountlist)); 2641590Srgrimes if (mp == NULL) 2651590Srgrimes return (0); 2661590Srgrimes mp->ml_left = mp->ml_right = (struct mountlist *)0; 2671590Srgrimes strp = mp->ml_host; 268194880Sdfr if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) 2691590Srgrimes return (0); 2701590Srgrimes strp = mp->ml_dirp; 271194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 2721590Srgrimes return (0); 2731590Srgrimes 2741590Srgrimes /* 2751590Srgrimes * Build a binary tree on sorted order of either host or dirp. 2761590Srgrimes * Drop any duplications. 2771590Srgrimes */ 2781590Srgrimes if (*mlp == NULL) { 2791590Srgrimes *mlp = mp; 2801590Srgrimes } else { 2811590Srgrimes tp = *mlp; 2821590Srgrimes while (tp) { 2831590Srgrimes val = strcmp(mp->ml_host, tp->ml_host); 2841590Srgrimes val2 = strcmp(mp->ml_dirp, tp->ml_dirp); 2851590Srgrimes switch (type) { 2861590Srgrimes case ALL: 2871590Srgrimes if (val == 0) { 2881590Srgrimes if (val2 == 0) { 2891590Srgrimes free((caddr_t)mp); 2901590Srgrimes goto next; 2911590Srgrimes } 2921590Srgrimes val = val2; 2931590Srgrimes } 2941590Srgrimes break; 2951590Srgrimes case DIRS: 2961590Srgrimes if (val2 == 0) { 2971590Srgrimes free((caddr_t)mp); 2981590Srgrimes goto next; 2991590Srgrimes } 3001590Srgrimes val = val2; 3011590Srgrimes break; 3021590Srgrimes default: 3031590Srgrimes if (val == 0) { 3041590Srgrimes free((caddr_t)mp); 3051590Srgrimes goto next; 3061590Srgrimes } 3071590Srgrimes break; 3081590Srgrimes }; 3091590Srgrimes if (val < 0) { 3101590Srgrimes otp = &tp->ml_left; 3111590Srgrimes tp = tp->ml_left; 3121590Srgrimes } else { 3131590Srgrimes otp = &tp->ml_right; 3141590Srgrimes tp = tp->ml_right; 3151590Srgrimes } 3161590Srgrimes } 3171590Srgrimes *otp = mp; 3181590Srgrimes } 3191590Srgrimesnext: 3201590Srgrimes if (!xdr_bool(xdrsp, &bool)) 3211590Srgrimes return (0); 3221590Srgrimes } 3231590Srgrimes return (1); 3241590Srgrimes} 3251590Srgrimes 3261590Srgrimes/* 3271590Srgrimes * Xdr routine to retrieve exports list 3281590Srgrimes */ 32928068Scharnierint 330194880Sdfrxdr_exportslist(xdrsp, exp) 3311590Srgrimes XDR *xdrsp; 3321590Srgrimes struct exportslist **exp; 3331590Srgrimes{ 3341590Srgrimes register struct exportslist *ep; 3351590Srgrimes register struct grouplist *gp; 3361590Srgrimes int bool, grpbool; 3371590Srgrimes char *strp; 3381590Srgrimes 3391590Srgrimes *exp = (struct exportslist *)0; 3401590Srgrimes if (!xdr_bool(xdrsp, &bool)) 3411590Srgrimes return (0); 3421590Srgrimes while (bool) { 3431590Srgrimes ep = (struct exportslist *)malloc(sizeof(struct exportslist)); 3441590Srgrimes if (ep == NULL) 3451590Srgrimes return (0); 3461590Srgrimes ep->ex_groups = (struct grouplist *)0; 3471590Srgrimes strp = ep->ex_dirp; 348194880Sdfr if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 3491590Srgrimes return (0); 3501590Srgrimes if (!xdr_bool(xdrsp, &grpbool)) 3511590Srgrimes return (0); 3521590Srgrimes while (grpbool) { 3531590Srgrimes gp = (struct grouplist *)malloc(sizeof(struct grouplist)); 3541590Srgrimes if (gp == NULL) 3551590Srgrimes return (0); 3561590Srgrimes strp = gp->gr_name; 357194880Sdfr if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) 3581590Srgrimes return (0); 3591590Srgrimes gp->gr_next = ep->ex_groups; 3601590Srgrimes ep->ex_groups = gp; 3611590Srgrimes if (!xdr_bool(xdrsp, &grpbool)) 3621590Srgrimes return (0); 3631590Srgrimes } 3641590Srgrimes ep->ex_next = *exp; 3651590Srgrimes *exp = ep; 3661590Srgrimes if (!xdr_bool(xdrsp, &bool)) 3671590Srgrimes return (0); 3681590Srgrimes } 3691590Srgrimes return (1); 3701590Srgrimes} 3711590Srgrimes 37228068Scharnierstatic void 3731590Srgrimesusage() 3741590Srgrimes{ 375146466Sru fprintf(stderr, "usage: showmount [-a | -d] [-e3] [host]\n"); 3761590Srgrimes exit(1); 3771590Srgrimes} 3781590Srgrimes 3791590Srgrimes/* 3801590Srgrimes * Print the binary tree in inorder so that output is sorted. 3811590Srgrimes */ 38228068Scharniervoid 3831590Srgrimesprint_dump(mp) 3841590Srgrimes struct mountlist *mp; 3851590Srgrimes{ 3861590Srgrimes 3871590Srgrimes if (mp == NULL) 3881590Srgrimes return; 3891590Srgrimes if (mp->ml_left) 3901590Srgrimes print_dump(mp->ml_left); 3911590Srgrimes switch (type) { 3921590Srgrimes case ALL: 3931590Srgrimes printf("%s:%s\n", mp->ml_host, mp->ml_dirp); 3941590Srgrimes break; 3951590Srgrimes case DIRS: 3961590Srgrimes printf("%s\n", mp->ml_dirp); 3971590Srgrimes break; 3981590Srgrimes default: 3991590Srgrimes printf("%s\n", mp->ml_host); 4001590Srgrimes break; 4011590Srgrimes }; 4021590Srgrimes if (mp->ml_right) 4031590Srgrimes print_dump(mp->ml_right); 4041590Srgrimes} 405