showmount.c revision 28068
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"; 411590Srgrimes#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[] = 4828068Scharnier "$Id$"; 491590Srgrimes#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> 581590Srgrimes#include <netdb.h> 591590Srgrimes#include <rpc/rpc.h> 601590Srgrimes#include <rpc/pmap_clnt.h> 611590Srgrimes#include <rpc/pmap_prot.h> 621590Srgrimes#include <nfs/rpcv2.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; 791590Srgrimes char ml_host[RPCMNT_NAMELEN+1]; 801590Srgrimes char ml_dirp[RPCMNT_PATHLEN+1]; 811590Srgrimes}; 821590Srgrimes 831590Srgrimesstruct grouplist { 841590Srgrimes struct grouplist *gr_next; 851590Srgrimes char gr_name[RPCMNT_NAMELEN+1]; 861590Srgrimes}; 871590Srgrimes 881590Srgrimesstruct exportslist { 891590Srgrimes struct exportslist *ex_next; 901590Srgrimes struct grouplist *ex_groups; 911590Srgrimes char ex_dirp[RPCMNT_PATHLEN+1]; 921590Srgrimes}; 931590Srgrimes 941590Srgrimesstatic struct mountlist *mntdump; 951590Srgrimesstatic struct exportslist *exports; 961590Srgrimesstatic int type = 0; 971590Srgrimes 9828068Scharniervoid print_dump __P((struct mountlist *)); 9928068Scharnierstatic void usage __P((void)); 10028068Scharnierint xdr_mntdump __P((XDR *, struct mountlist **)); 10128068Scharnierint xdr_exports __P((XDR *, struct exportslist **)); 10228068Scharnier 1031590Srgrimes/* 1041590Srgrimes * This command queries the NFS mount daemon for it's mount list and/or 1051590Srgrimes * it's exports list and prints them out. 1061590Srgrimes * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A" 1079336Sdfr * and the "Network File System Protocol XXX.." 1081590Srgrimes * for detailed information on the protocol. 1091590Srgrimes */ 11028068Scharnierint 1111590Srgrimesmain(argc, argv) 1121590Srgrimes int argc; 1131590Srgrimes char **argv; 1141590Srgrimes{ 1151590Srgrimes register struct exportslist *exp; 1161590Srgrimes register struct grouplist *grp; 1179336Sdfr register int rpcs = 0, mntvers = 1; 1181590Srgrimes char ch; 1191590Srgrimes char *host; 1201590Srgrimes int estat; 1211590Srgrimes 12224360Simp while ((ch = getopt(argc, argv, "ade3")) != -1) 1231590Srgrimes switch((char)ch) { 1241590Srgrimes case 'a': 1251590Srgrimes if (type == 0) { 1261590Srgrimes type = ALL; 1271590Srgrimes rpcs |= DODUMP; 1281590Srgrimes } else 1291590Srgrimes usage(); 1301590Srgrimes break; 1311590Srgrimes case 'd': 1321590Srgrimes if (type == 0) { 1331590Srgrimes type = DIRS; 1341590Srgrimes rpcs |= DODUMP; 1351590Srgrimes } else 1361590Srgrimes usage(); 1371590Srgrimes break; 1381590Srgrimes case 'e': 1391590Srgrimes rpcs |= DOEXPORTS; 1401590Srgrimes break; 1419336Sdfr case '3': 1429336Sdfr mntvers = 3; 1439336Sdfr break; 1441590Srgrimes case '?': 1451590Srgrimes default: 1461590Srgrimes usage(); 1471590Srgrimes } 1481590Srgrimes argc -= optind; 1491590Srgrimes argv += optind; 1501590Srgrimes 1511590Srgrimes if (argc > 0) 1521590Srgrimes host = *argv; 1531590Srgrimes else 1541590Srgrimes host = "localhost"; 1551590Srgrimes 1561590Srgrimes if (rpcs == 0) 1571590Srgrimes rpcs = DODUMP; 1581590Srgrimes 1591590Srgrimes if (rpcs & DODUMP) 1609336Sdfr if ((estat = callrpc(host, RPCPROG_MNT, mntvers, 1611590Srgrimes RPCMNT_DUMP, xdr_void, (char *)0, 1621590Srgrimes xdr_mntdump, (char *)&mntdump)) != 0) { 1631590Srgrimes clnt_perrno(estat); 16428068Scharnier errx(1, "can't do mountdump rpc"); 1651590Srgrimes } 1661590Srgrimes if (rpcs & DOEXPORTS) 1679336Sdfr if ((estat = callrpc(host, RPCPROG_MNT, mntvers, 1681590Srgrimes RPCMNT_EXPORT, xdr_void, (char *)0, 1691590Srgrimes xdr_exports, (char *)&exports)) != 0) { 1701590Srgrimes clnt_perrno(estat); 17128068Scharnier errx(1, "can't do exports rpc"); 1721590Srgrimes } 1731590Srgrimes 1741590Srgrimes /* Now just print out the results */ 1751590Srgrimes if (rpcs & DODUMP) { 1761590Srgrimes switch (type) { 1771590Srgrimes case ALL: 1781590Srgrimes printf("All mount points on %s:\n", host); 1791590Srgrimes break; 1801590Srgrimes case DIRS: 1811590Srgrimes printf("Directories on %s:\n", host); 1821590Srgrimes break; 1831590Srgrimes default: 1841590Srgrimes printf("Hosts on %s:\n", host); 1851590Srgrimes break; 1861590Srgrimes }; 1871590Srgrimes print_dump(mntdump); 1881590Srgrimes } 1891590Srgrimes if (rpcs & DOEXPORTS) { 1901590Srgrimes printf("Exports list on %s:\n", host); 1911590Srgrimes exp = exports; 1921590Srgrimes while (exp) { 1931590Srgrimes printf("%-35s", exp->ex_dirp); 1941590Srgrimes grp = exp->ex_groups; 1951590Srgrimes if (grp == NULL) { 1961590Srgrimes printf("Everyone\n"); 1971590Srgrimes } else { 1981590Srgrimes while (grp) { 1991590Srgrimes printf("%s ", grp->gr_name); 2001590Srgrimes grp = grp->gr_next; 2011590Srgrimes } 2021590Srgrimes printf("\n"); 2031590Srgrimes } 2041590Srgrimes exp = exp->ex_next; 2051590Srgrimes } 2061590Srgrimes } 20728068Scharnier exit(0); 2081590Srgrimes} 2091590Srgrimes 2101590Srgrimes/* 2111590Srgrimes * Xdr routine for retrieving the mount dump list 2121590Srgrimes */ 21328068Scharnierint 2141590Srgrimesxdr_mntdump(xdrsp, mlp) 2151590Srgrimes XDR *xdrsp; 2161590Srgrimes struct mountlist **mlp; 2171590Srgrimes{ 2181590Srgrimes register struct mountlist *mp; 2191590Srgrimes register struct mountlist *tp; 2201590Srgrimes register struct mountlist **otp; 2211590Srgrimes int val, val2; 2221590Srgrimes int bool; 2231590Srgrimes char *strp; 2241590Srgrimes 2251590Srgrimes *mlp = (struct mountlist *)0; 2261590Srgrimes if (!xdr_bool(xdrsp, &bool)) 2271590Srgrimes return (0); 2281590Srgrimes while (bool) { 2291590Srgrimes mp = (struct mountlist *)malloc(sizeof(struct mountlist)); 2301590Srgrimes if (mp == NULL) 2311590Srgrimes return (0); 2321590Srgrimes mp->ml_left = mp->ml_right = (struct mountlist *)0; 2331590Srgrimes strp = mp->ml_host; 2341590Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN)) 2351590Srgrimes return (0); 2361590Srgrimes strp = mp->ml_dirp; 2371590Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 2381590Srgrimes return (0); 2391590Srgrimes 2401590Srgrimes /* 2411590Srgrimes * Build a binary tree on sorted order of either host or dirp. 2421590Srgrimes * Drop any duplications. 2431590Srgrimes */ 2441590Srgrimes if (*mlp == NULL) { 2451590Srgrimes *mlp = mp; 2461590Srgrimes } else { 2471590Srgrimes tp = *mlp; 2481590Srgrimes while (tp) { 2491590Srgrimes val = strcmp(mp->ml_host, tp->ml_host); 2501590Srgrimes val2 = strcmp(mp->ml_dirp, tp->ml_dirp); 2511590Srgrimes switch (type) { 2521590Srgrimes case ALL: 2531590Srgrimes if (val == 0) { 2541590Srgrimes if (val2 == 0) { 2551590Srgrimes free((caddr_t)mp); 2561590Srgrimes goto next; 2571590Srgrimes } 2581590Srgrimes val = val2; 2591590Srgrimes } 2601590Srgrimes break; 2611590Srgrimes case DIRS: 2621590Srgrimes if (val2 == 0) { 2631590Srgrimes free((caddr_t)mp); 2641590Srgrimes goto next; 2651590Srgrimes } 2661590Srgrimes val = val2; 2671590Srgrimes break; 2681590Srgrimes default: 2691590Srgrimes if (val == 0) { 2701590Srgrimes free((caddr_t)mp); 2711590Srgrimes goto next; 2721590Srgrimes } 2731590Srgrimes break; 2741590Srgrimes }; 2751590Srgrimes if (val < 0) { 2761590Srgrimes otp = &tp->ml_left; 2771590Srgrimes tp = tp->ml_left; 2781590Srgrimes } else { 2791590Srgrimes otp = &tp->ml_right; 2801590Srgrimes tp = tp->ml_right; 2811590Srgrimes } 2821590Srgrimes } 2831590Srgrimes *otp = mp; 2841590Srgrimes } 2851590Srgrimesnext: 2861590Srgrimes if (!xdr_bool(xdrsp, &bool)) 2871590Srgrimes return (0); 2881590Srgrimes } 2891590Srgrimes return (1); 2901590Srgrimes} 2911590Srgrimes 2921590Srgrimes/* 2931590Srgrimes * Xdr routine to retrieve exports list 2941590Srgrimes */ 29528068Scharnierint 2961590Srgrimesxdr_exports(xdrsp, exp) 2971590Srgrimes XDR *xdrsp; 2981590Srgrimes struct exportslist **exp; 2991590Srgrimes{ 3001590Srgrimes register struct exportslist *ep; 3011590Srgrimes register struct grouplist *gp; 3021590Srgrimes int bool, grpbool; 3031590Srgrimes char *strp; 3041590Srgrimes 3051590Srgrimes *exp = (struct exportslist *)0; 3061590Srgrimes if (!xdr_bool(xdrsp, &bool)) 3071590Srgrimes return (0); 3081590Srgrimes while (bool) { 3091590Srgrimes ep = (struct exportslist *)malloc(sizeof(struct exportslist)); 3101590Srgrimes if (ep == NULL) 3111590Srgrimes return (0); 3121590Srgrimes ep->ex_groups = (struct grouplist *)0; 3131590Srgrimes strp = ep->ex_dirp; 3141590Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN)) 3151590Srgrimes return (0); 3161590Srgrimes if (!xdr_bool(xdrsp, &grpbool)) 3171590Srgrimes return (0); 3181590Srgrimes while (grpbool) { 3191590Srgrimes gp = (struct grouplist *)malloc(sizeof(struct grouplist)); 3201590Srgrimes if (gp == NULL) 3211590Srgrimes return (0); 3221590Srgrimes strp = gp->gr_name; 3231590Srgrimes if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN)) 3241590Srgrimes return (0); 3251590Srgrimes gp->gr_next = ep->ex_groups; 3261590Srgrimes ep->ex_groups = gp; 3271590Srgrimes if (!xdr_bool(xdrsp, &grpbool)) 3281590Srgrimes return (0); 3291590Srgrimes } 3301590Srgrimes ep->ex_next = *exp; 3311590Srgrimes *exp = ep; 3321590Srgrimes if (!xdr_bool(xdrsp, &bool)) 3331590Srgrimes return (0); 3341590Srgrimes } 3351590Srgrimes return (1); 3361590Srgrimes} 3371590Srgrimes 33828068Scharnierstatic void 3391590Srgrimesusage() 3401590Srgrimes{ 34124825Sobrien fprintf(stderr, "usage: showmount [-ade3] host\n"); 3421590Srgrimes exit(1); 3431590Srgrimes} 3441590Srgrimes 3451590Srgrimes/* 3461590Srgrimes * Print the binary tree in inorder so that output is sorted. 3471590Srgrimes */ 34828068Scharniervoid 3491590Srgrimesprint_dump(mp) 3501590Srgrimes struct mountlist *mp; 3511590Srgrimes{ 3521590Srgrimes 3531590Srgrimes if (mp == NULL) 3541590Srgrimes return; 3551590Srgrimes if (mp->ml_left) 3561590Srgrimes print_dump(mp->ml_left); 3571590Srgrimes switch (type) { 3581590Srgrimes case ALL: 3591590Srgrimes printf("%s:%s\n", mp->ml_host, mp->ml_dirp); 3601590Srgrimes break; 3611590Srgrimes case DIRS: 3621590Srgrimes printf("%s\n", mp->ml_dirp); 3631590Srgrimes break; 3641590Srgrimes default: 3651590Srgrimes printf("%s\n", mp->ml_host); 3661590Srgrimes break; 3671590Srgrimes }; 3681590Srgrimes if (mp->ml_right) 3691590Srgrimes print_dump(mp->ml_right); 3701590Srgrimes} 371