home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / n / tcpip / netkit-a.06 / netkit-a / NetKit-A-0.06 / nfs-server-2.0 / showmount.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-13  |  6.2 KB  |  301 lines

  1. /*
  2.  * showmount.c -- show mount information for an NFS server
  3.  * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  */
  15.  
  16. #ifdef HAVE_CONFIG_H
  17. #include <config.h>
  18. #endif
  19.  
  20. #include <stdio.h>
  21. #include <rpc/rpc.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <sys/time.h>
  25. #ifdef HAVE_STRING_H
  26. #include <string.h>
  27. #else
  28. #include <strings.h>
  29. #endif
  30. #include <sys/types.h>
  31. #ifdef STDC_HEADERS
  32. #include <unistd.h>
  33. #include <memory.h>
  34. #include <stdlib.h>
  35. #endif
  36.  
  37. #include <netdb.h>
  38. #include <arpa/inet.h>
  39. #include <errno.h>
  40. #include "getopt.h"
  41.  
  42. #include "mount.h"
  43.  
  44. int headers = 1;
  45. int hflag = 0;
  46. int aflag = 0;
  47. int dflag = 0;
  48. int eflag = 0;
  49.  
  50. extern char version[];
  51. char *program_name;
  52. static struct option longopts[] =
  53. {
  54.     { "all", 0, 0, 'a' },
  55.     { "directories", 0, 0, 'd' },
  56.     { "exports", 0, 0, 'e' },
  57.     { "no-headers", 0, &headers, 0 },
  58.     { "version", 0, 0, 'v' },
  59.     { "help", 0, 0, 'h' },
  60.     { NULL, 0, 0, 0 }
  61. };
  62.  
  63. #define MAXHOSTLEN 256
  64.  
  65. int dump_cmp(p, q)
  66. char **p;
  67. char **q;
  68. {
  69.     return strcmp(*p, *q);
  70. }
  71.  
  72. static void usage(fp, n)
  73. FILE *fp;
  74. int n;
  75. {
  76.     fprintf(fp, "Usage: %s [-adehv]\n", program_name);
  77.     fprintf(fp, "       [--all] [--directories] [--exports]\n");
  78.     fprintf(fp, "       [--no-headers] [--help] [--version] [host]\n");
  79.     exit(n);
  80. }
  81.  
  82. int main(argc, argv)
  83. int argc;
  84. char **argv;
  85. {
  86.     char hostname_buf[MAXHOSTLEN];
  87.     char *hostname;
  88.     enum clnt_stat clnt_stat;
  89.     struct hostent *hp;
  90.     struct sockaddr_in server_addr;
  91.     int msock;
  92.     struct timeval total_timeout;
  93.     struct timeval pertry_timeout;
  94.     int c;
  95.     CLIENT *mclient;
  96.     groups grouplist;
  97.     exports exportlist, exl;
  98.     mountlist dumplist;
  99.     mountlist list;
  100.     int i;
  101.     int n;
  102.     int maxlen;
  103.     char **dumpv;
  104.  
  105.     program_name = argv[0];
  106.     while ((c = getopt_long(argc, argv, "adehv", longopts, NULL)) != EOF) {
  107.         switch (c) {
  108.         case 'a':
  109.             aflag = 1;
  110.             break;
  111.         case 'd':
  112.             dflag = 1;
  113.             break;
  114.         case 'e':
  115.             eflag = 1;
  116.             break;
  117.         case 'h':
  118.             usage(stdout, 0);
  119.             break;
  120.         case 'v':
  121.             printf("%s\n", version);
  122.             exit(0);
  123.         case 0:
  124.             break;
  125.         case '?':
  126.         default:
  127.             usage(stderr, 1);
  128.             break;
  129.         }
  130.     }
  131.     argc -= optind;
  132.     argv += optind;
  133.  
  134.     switch (aflag + dflag + eflag) {
  135.     case 0:
  136.         hflag = 1;
  137.         break;
  138.     case 1:
  139.         break;
  140.     default:
  141.         fprintf(stderr, "%s: only one of -a, -d or -e is allowed\n",
  142.             program_name);
  143.         exit(1);
  144.         break;
  145.     }
  146.  
  147.     switch (argc) {
  148.     case 0:
  149. #if 0
  150.         hostname = "localhost";
  151. #else
  152.         if (gethostname(hostname_buf, MAXHOSTLEN) < 0) {
  153.             perror("getting hostname");
  154.             exit(1);
  155.         }
  156.         hostname = hostname_buf;
  157. #endif
  158.         break;
  159.     case 1:
  160.         hostname = argv[0];
  161.         break;
  162.     default:
  163.         fprintf(stderr, "%s: only one hostname is allowed\n",
  164.             program_name);
  165.         exit(1);
  166.         break;
  167.     }
  168.  
  169.     if (hostname[0] >= '0' && hostname[0] <= '9') {
  170.         server_addr.sin_family = AF_INET;
  171.         server_addr.sin_addr.s_addr = inet_addr(hostname);
  172.     }
  173.     else {
  174.         if ((hp = gethostbyname(hostname)) == NULL) {
  175.             fprintf(stderr, "%s: can't get address for %s\n",
  176.                 program_name, hostname);
  177.             exit(1);
  178.         }
  179.         server_addr.sin_family = AF_INET;
  180.         memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length);
  181.     }
  182.  
  183.     /* create mount deamon client */
  184.  
  185.     server_addr.sin_port = 0;
  186.     msock = RPC_ANYSOCK;
  187.     if ((mclient = clnttcp_create(&server_addr,
  188.         MOUNTPROG, MOUNTVERS, &msock, 0, 0)) == NULL) {
  189.         server_addr.sin_port = 0;
  190.         msock = RPC_ANYSOCK;
  191.         pertry_timeout.tv_sec = 3;
  192.         pertry_timeout.tv_usec = 0;
  193.         if ((mclient = clntudp_create(&server_addr,
  194.             MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) {
  195.             clnt_pcreateerror("mount clntudp_create");
  196.             exit(1);
  197.         }
  198.     }
  199.     mclient->cl_auth = authunix_create_default();
  200.     total_timeout.tv_sec = 20;
  201.     total_timeout.tv_usec = 0;
  202.  
  203.     if (eflag) {
  204.         memset(&exportlist, '\0', sizeof(exportlist));
  205.         clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT,
  206.             xdr_void, NULL, xdr_exports, &exportlist,
  207.             total_timeout);
  208.         if (clnt_stat != RPC_SUCCESS) {
  209.             clnt_perror(mclient, "rpc mount export");
  210.             exit(1);
  211.         }
  212.         if (headers)
  213.             printf("Export list for %s:\n", hostname);
  214.         maxlen = 0;
  215.         for (exl = exportlist; exl; exl = exl->ex_next) {
  216.             if ((n = strlen(exl->ex_dir)) > maxlen)
  217.                 maxlen = n;
  218.         }
  219.         while (exportlist) {
  220.             printf("%-*s ", maxlen, exportlist->ex_dir);
  221.             grouplist = exportlist->ex_groups;
  222.             if (grouplist)
  223.                 while (grouplist) {
  224.                     printf("%s%s", grouplist->gr_name,
  225.                         grouplist->gr_next ? "," : "");
  226.                     grouplist = grouplist->gr_next;
  227.                 }
  228.             else
  229.                 printf("(everyone)");
  230.             printf("\n");
  231.             exportlist = exportlist->ex_next;
  232.         }
  233.         exit(0);
  234.     }
  235.  
  236.     memset(&dumplist, '\0', sizeof(dumplist));
  237.     clnt_stat = clnt_call(mclient, MOUNTPROC_DUMP,
  238.         xdr_void, NULL, xdr_mountlist, &dumplist,
  239.         total_timeout);
  240.     if (clnt_stat != RPC_SUCCESS) {
  241.         clnt_perror(mclient, "rpc mount dump");
  242.         exit(1);
  243.     }
  244.  
  245.     n = 0;
  246.     for (list = dumplist; list; list = list->ml_next)
  247.         n++;
  248.     dumpv = (char **) calloc(n, sizeof (char *));
  249.     if (n && !dumpv) {
  250.         fprintf(stderr, "%s: out of memory\n", program_name);
  251.         exit(1);
  252.     }
  253.     i = 0;
  254.  
  255.     if (hflag) {
  256.         if (headers)
  257.             printf("Hosts on %s:\n", hostname);
  258.         while (dumplist) {
  259.             dumpv[i++] = dumplist->ml_hostname;
  260.             dumplist = dumplist->ml_next;
  261.         }
  262.     }
  263.     else if (aflag) {
  264.         if (headers)
  265.             printf("All mount points on %s:\n", hostname);
  266.         while (dumplist) {
  267.             char s[1024];
  268.             char *t;
  269.  
  270.             sprintf(s, "%s:%s", dumplist->ml_hostname,
  271.                 dumplist->ml_directory);
  272.             t = malloc(strlen(s) + 1);
  273.             if (t)
  274.                 strcpy(t, s);
  275.             else {
  276.                 printf("%s: out of memory\n", program_name);
  277.                 exit(1);
  278.             }
  279.             dumpv[i++] = t;
  280.             dumplist = dumplist->ml_next;
  281.         }
  282.     }
  283.     else if (dflag) {
  284.         if (headers)
  285.             printf("Directories on %s:\n", hostname);
  286.         while (dumplist) {
  287.             dumpv[i++] = dumplist->ml_directory;
  288.             dumplist = dumplist->ml_next;
  289.         }
  290.     }
  291.  
  292.     qsort(dumpv, n, sizeof (char *), dump_cmp);
  293.     
  294.     for (i = 0; i < n; i++) {
  295.         if (i == 0 || strcmp(dumpv[i], dumpv[i - 1]) != 0)
  296.             printf("%s\n", dumpv[i]);
  297.     }
  298.     exit(0);
  299. }
  300.  
  301.