home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume30 / log_tcp / part02 / try.c < prev   
Encoding:
C/C++ Source or Header  |  1992-06-20  |  2.5 KB  |  115 lines

  1.  /*
  2.   * try - program to try out host access-control tables, including the
  3.   * optional shell commands.
  4.   * 
  5.   * usage: try process_name host_name_or_address
  6.   * 
  7.   * where process_name is a daemon process name (argv[0] value). If a host name
  8.   * is specified, both the name and address will be used to check the address
  9.   * control tables. If a host address is specified, the program pretends that
  10.   * host name lookup failed.
  11.   */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#) try.c 1.2 92/06/11 22:21:32";
  15. #endif
  16.  
  17. #include <sys/types.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. #include <netdb.h>
  21. #include <stdio.h>
  22. #include <syslog.h>
  23.  
  24. #ifdef HOSTS_ACCESS
  25.  
  26. #ifndef    INADDR_NONE
  27. #define    INADDR_NONE    (-1)        /* XXX should be 0xffffffff */
  28. #endif
  29.  
  30. #include "log_tcp.h"
  31.  
  32. /* Try out a (daemon,client) pair */
  33.  
  34. try(daemon, name, addr)
  35. char   *daemon;
  36. char   *name;
  37. char   *addr;
  38. {
  39.     printf("daemon %s: host name %s (address %s): ",
  40.        daemon, name, addr);
  41.     printf("access %s\n",
  42.        hosts_ctl(daemon, name, addr, "you") ? "granted" : "denied");
  43. }
  44.  
  45. /* function to intercept the real shell_cmd() */
  46.  
  47. void    shell_cmd(cmd, daemon, client)
  48. char   *cmd;
  49. char   *daemon;
  50. struct from_host *client;
  51. {
  52.     char    buf[BUFSIZ];
  53.     int     pid = getpid();
  54.  
  55.     percent_x(buf, sizeof(buf), cmd, daemon, client, pid);
  56.     printf("shell command: %s: ", buf);
  57. }
  58.  
  59. /* function to intercept the real process_options() */
  60.  
  61. process_options(options, daemon, client)
  62. char   *options;
  63. char   *daemon;
  64. struct from_host *client;
  65. {
  66.     char    buf[BUFSIZ];
  67.     int     pid = getpid();
  68.  
  69.     percent_x(buf, sizeof(buf), options, daemon, client, pid);
  70.     printf("options: %s: ", buf);
  71. }
  72.  
  73. main(argc, argv)
  74. int     argc;
  75. char  **argv;
  76. {
  77.     struct hostent *hp;
  78.  
  79. #ifdef LOG_MAIL
  80.     openlog(argv[0], LOG_PID, FACILITY);
  81. #else
  82.     openlog(argv[0], LOG_PID);
  83. #endif
  84.  
  85.     if (argc != 3) {
  86.     fprintf(stderr, "usage: %s process_name host_name_or_address\n",
  87.         argv[0]);
  88.     return (1);
  89.     } else {
  90.     if (inet_addr(argv[2]) != INADDR_NONE) {/* pretend host name unknown */
  91.         try(argv[1], FROM_UNKNOWN, argv[2]);
  92.     } else {
  93.         if ((hp = gethostbyname(argv[2])) == 0) {    /* bad host name */
  94.         fprintf(stderr, "warning: host unknown: %s\n", argv[2]);
  95.         try(argv[1], argv[2], "?.?.?.?");
  96.         } else {                /* use both name and address */
  97.         while (hp->h_addr_list[0])
  98.             try(argv[1], hp->h_name,
  99.             inet_ntoa(*(struct in_addr *) * hp->h_addr_list++));
  100.         }
  101.     }
  102.     return (0);
  103.     }
  104. }
  105.  
  106. #else
  107.  
  108. main()
  109. {
  110.     fprintf(stderr, "host access control is not enabled.\n");
  111.     return (1);
  112. }
  113.  
  114. #endif
  115.