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$"; 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 <rpcsvc/mount.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[MNTNAMLEN+1]; 80 char ml_dirp[MNTPATHLEN+1]; 81 }; 82 83 struct grouplist { 84 struct grouplist *gr_next; 85 char gr_name[MNTNAMLEN+1]; 86 }; 87 88 struct exportslist { 89 struct exportslist *ex_next; 90 struct grouplist *ex_groups; 91 char ex_dirp[MNTPATHLEN+1]; 92 }; 93 94 static struct mountlist *mntdump; 95 static struct exportslist *exportslist; 96 static int type = 0; 97 98 void print_dump(struct mountlist *); 99 static void usage(void); 100 int xdr_mntdump(XDR *, struct mountlist **); 101 int xdr_exportslist(XDR *, struct exportslist **); 102 int tcp_callrpc(const char *host, int prognum, int versnum, int procnum, 103 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out); 104 105 /* 106 * This command queries the NFS mount daemon for it's mount list and/or 107 * it's exports list and prints them out. 108 * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A" 109 * and the "Network File System Protocol XXX.." 110 * for detailed information on the protocol. 111 */ 112 int 113 main(int argc, char **argv) 114 { 115 register struct exportslist *exp; 116 register struct grouplist *grp; 117 register int rpcs = 0, mntvers = 1; 118 const char *host; 119 int ch, estat; 120 121 while ((ch = getopt(argc, argv, "ade3")) != -1) 122 switch (ch) { 123 case 'a': 124 if (type == 0) { 125 type = ALL; 126 rpcs |= DODUMP; 127 } else 128 usage(); 129 break; 130 case 'd': 131 if (type == 0) { 132 type = DIRS; 133 rpcs |= DODUMP; 134 } else 135 usage(); 136 break; 137 case 'e': 138 rpcs |= DOEXPORTS; 139 break; 140 case '3': 141 mntvers = 3; 142 break; 143 case '?': 144 default: 145 usage(); 146 } 147 argc -= optind; 148 argv += optind; 149 150 if (argc > 0) 151 host = *argv; 152 else 153 host = "localhost"; 154 155 if (rpcs == 0) 156 rpcs = DODUMP; 157 158 if (rpcs & DODUMP) 159 if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers, 160 MOUNTPROC_DUMP, (xdrproc_t)xdr_void, (char *)0, 161 (xdrproc_t)xdr_mntdump, (char *)&mntdump)) != 0) { 162 clnt_perrno(estat); 163 errx(1, "can't do mountdump rpc"); 164 } 165 if (rpcs & DOEXPORTS) 166 if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers, 167 MOUNTPROC_EXPORT, (xdrproc_t)xdr_void, (char *)0, 168 (xdrproc_t)xdr_exportslist, (char *)&exportslist)) != 0) { 169 clnt_perrno(estat); 170 errx(1, "can't do exports rpc"); 171 } 172 173 /* Now just print out the results */ 174 if (rpcs & DODUMP) { 175 switch (type) { 176 case ALL: 177 printf("All mount points on %s:\n", host); 178 break; 179 case DIRS: 180 printf("Directories on %s:\n", host); 181 break; 182 default: 183 printf("Hosts on %s:\n", host); 184 break; 185 }; 186 print_dump(mntdump); 187 } 188 if (rpcs & DOEXPORTS) { 189 printf("Exports list on %s:\n", host); 190 exp = exportslist; 191 while (exp) { 192 printf("%-35s", exp->ex_dirp); 193 grp = exp->ex_groups; 194 if (grp == NULL) { 195 printf("Everyone\n"); 196 } else { 197 while (grp) { 198 printf("%s ", grp->gr_name); 199 grp = grp->gr_next; 200 } 201 printf("\n"); 202 } 203 exp = exp->ex_next; 204 } 205 } 206 exit(0); 207 } 208 209 /* 210 * tcp_callrpc has the same interface as callrpc, but tries to 211 * use tcp as transport method in order to handle large replies. 212 */ 213 int 214 tcp_callrpc(const char *host, int prognum, int versnum, int procnum, 215 xdrproc_t inproc, char *in, xdrproc_t outproc, char *out) 216 { 217 CLIENT *client; 218 struct timeval timeout; 219 int rval; 220 221 if ((client = clnt_create(host, prognum, versnum, "tcp")) == NULL && 222 (client = clnt_create(host, prognum, versnum, "udp")) == NULL) 223 return ((int) rpc_createerr.cf_stat); 224 225 timeout.tv_sec = 25; 226 timeout.tv_usec = 0; 227 rval = (int) clnt_call(client, procnum, 228 inproc, in, 229 outproc, out, 230 timeout); 231 clnt_destroy(client); 232 return rval; 233 } 234 235 /* 236 * Xdr routine for retrieving the mount dump list 237 */ 238 int 239 xdr_mntdump(XDR *xdrsp, struct mountlist **mlp) 240 { 241 register struct mountlist *mp; 242 register struct mountlist *tp; 243 register struct mountlist **otp; 244 int val, val2; 245 int bool; 246 char *strp; 247 248 *mlp = (struct mountlist *)0; 249 if (!xdr_bool(xdrsp, &bool)) 250 return (0); 251 while (bool) { 252 mp = (struct mountlist *)malloc(sizeof(struct mountlist)); 253 if (mp == NULL) 254 return (0); 255 mp->ml_left = mp->ml_right = (struct mountlist *)0; 256 strp = mp->ml_host; 257 if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) 258 return (0); 259 strp = mp->ml_dirp; 260 if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 261 return (0); 262 263 /* 264 * Build a binary tree on sorted order of either host or dirp. 265 * Drop any duplications. 266 */ 267 if (*mlp == NULL) { 268 *mlp = mp; 269 } else { 270 tp = *mlp; 271 while (tp) { 272 val = strcmp(mp->ml_host, tp->ml_host); 273 val2 = strcmp(mp->ml_dirp, tp->ml_dirp); 274 switch (type) { 275 case ALL: 276 if (val == 0) { 277 if (val2 == 0) { 278 free((caddr_t)mp); 279 goto next; 280 } 281 val = val2; 282 } 283 break; 284 case DIRS: 285 if (val2 == 0) { 286 free((caddr_t)mp); 287 goto next; 288 } 289 val = val2; 290 break; 291 default: 292 if (val == 0) { 293 free((caddr_t)mp); 294 goto next; 295 } 296 break; 297 }; 298 if (val < 0) { 299 otp = &tp->ml_left; 300 tp = tp->ml_left; 301 } else { 302 otp = &tp->ml_right; 303 tp = tp->ml_right; 304 } 305 } 306 *otp = mp; 307 } 308 next: 309 if (!xdr_bool(xdrsp, &bool)) 310 return (0); 311 } 312 return (1); 313 } 314 315 /* 316 * Xdr routine to retrieve exports list 317 */ 318 int 319 xdr_exportslist(XDR *xdrsp, struct exportslist **exp) 320 { 321 register struct exportslist *ep; 322 register struct grouplist *gp; 323 int bool, grpbool; 324 char *strp; 325 326 *exp = (struct exportslist *)0; 327 if (!xdr_bool(xdrsp, &bool)) 328 return (0); 329 while (bool) { 330 ep = (struct exportslist *)malloc(sizeof(struct exportslist)); 331 if (ep == NULL) 332 return (0); 333 ep->ex_groups = (struct grouplist *)0; 334 strp = ep->ex_dirp; 335 if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) 336 return (0); 337 if (!xdr_bool(xdrsp, &grpbool)) 338 return (0); 339 while (grpbool) { 340 gp = (struct grouplist *)malloc(sizeof(struct grouplist)); 341 if (gp == NULL) 342 return (0); 343 strp = gp->gr_name; 344 if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) 345 return (0); 346 gp->gr_next = ep->ex_groups; 347 ep->ex_groups = gp; 348 if (!xdr_bool(xdrsp, &grpbool)) 349 return (0); 350 } 351 ep->ex_next = *exp; 352 *exp = ep; 353 if (!xdr_bool(xdrsp, &bool)) 354 return (0); 355 } 356 return (1); 357 } 358 359 static void 360 usage(void) 361 { 362 fprintf(stderr, "usage: showmount [-a | -d] [-e3] [host]\n"); 363 exit(1); 364 } 365 366 /* 367 * Print the binary tree in inorder so that output is sorted. 368 */ 369 void 370 print_dump(struct mountlist *mp) 371 { 372 373 if (mp == NULL) 374 return; 375 if (mp->ml_left) 376 print_dump(mp->ml_left); 377 switch (type) { 378 case ALL: 379 printf("%s:%s\n", mp->ml_host, mp->ml_dirp); 380 break; 381 case DIRS: 382 printf("%s\n", mp->ml_dirp); 383 break; 384 default: 385 printf("%s\n", mp->ml_host); 386 break; 387 }; 388 if (mp->ml_right) 389 print_dump(mp->ml_right); 390 } 391