home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume39 / tcp_wrappers / part04 / try.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-29  |  5.1 KB  |  221 lines

  1.  /*
  2.   * try - program to try out host access-control tables, including the
  3.   * optional shell commands and the optional language extensions.
  4.   * 
  5.   * usage: try [-d] process_name [user@]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 name and address will be checked against the address
  9.   * control tables. If a host address is specified, the program pretends that
  10.   * host name lookup failed.
  11.   * 
  12.   * The -d option forces the program to use the access control tables in the
  13.   * current directory.
  14.   * 
  15.   * All errors are written to the standard error stream, including the errors
  16.   * that would normally be reported via the syslog daemon.
  17.   */
  18.  
  19. #ifndef lint
  20. static char sccsid[] = "@(#) try.c 1.8 93/09/27 18:59:28";
  21. #endif
  22.  
  23. #include <sys/types.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <netdb.h>
  27. #include <stdio.h>
  28. #include <syslog.h>
  29. #include <setjmp.h>
  30.  
  31. extern void exit();
  32. extern char *strchr();
  33.  
  34. #ifndef HOSTS_ACCESS
  35.  
  36. main()
  37. {
  38.     fprintf(stderr, "host access control is not enabled.\n");
  39.     return (1);
  40. }
  41.  
  42. #else
  43.  
  44. #ifndef    INADDR_NONE
  45. #define    INADDR_NONE    (-1)        /* XXX should be 0xffffffff */
  46. #endif
  47.  
  48. #include "log_tcp.h"
  49. #include "options.h"
  50.  
  51. int     allow_severity = SEVERITY;    /* run-time adjustable */
  52. int     deny_severity = LOG_WARNING;    /* ditto */
  53.  
  54. /* usage - explain */
  55.  
  56. void    usage(myname)
  57. char   *myname;
  58. {
  59.     fprintf(stderr,
  60.         "usage: %s [-d] process_name [user@]host_name_or_address\n",
  61.         myname);
  62.     exit(1);
  63. }
  64.  
  65. /* Try out a (daemon,client) pair */
  66.  
  67. void    try(daemon, name, addr, user)
  68. char   *daemon;
  69. char   *name;
  70. char   *addr;
  71. char   *user;
  72. {
  73.     int     verdict;
  74.  
  75.     /*
  76.      * The dry_run flag informs the optional extension language routines that
  77.      * they are being run in verification mode, and that they should not
  78.      * perform any real action. Extension language routines that would not
  79.      * return should inform us of their plan, by clearing the dry_run flag.
  80.      * This is a bit clumsy but we must be able to verify hosts with more
  81.      * than one network address; just terminating the program isn't
  82.      * acceptable.
  83.      */
  84.  
  85.     dry_run = 1;
  86.  
  87.     printf(" Daemon:   %s\n", daemon);
  88.     printf(" Hostname: %s\n", name);
  89.     printf(" Address:  %s\n", addr);
  90.  
  91.     if (user[0] && strcasecmp(user, FROM_UNKNOWN))
  92.     printf(" Username: %s\n", user);
  93.  
  94.     verdict = hosts_ctl(daemon, name, addr, user);
  95.  
  96.     printf(" Access:   %s\n",
  97.        dry_run == 0 ? "delegated" :
  98.        verdict ? "granted" : "denied");
  99. }
  100.  
  101. int     main(argc, argv)
  102. int     argc;
  103. char  **argv;
  104. {
  105.     struct hostent *hp;
  106.     char   *myname = argv[0];
  107.     char   *client;
  108.     char   *server;
  109.     char   *at;
  110.     char   *user;
  111.     char   *host;
  112.  
  113.     /*
  114.      * Parse the JCL.
  115.      */
  116.     while (--argc && *++argv && **argv == '-') {
  117.     if (strcmp(*argv, "-d") == 0) {        /* use tables in . */
  118.         hosts_allow_table = "hosts.allow";
  119.         hosts_deny_table = "hosts.deny";
  120.     } else {
  121.         usage(myname);
  122.     }
  123.     }
  124.     if (argc != 2)
  125.     usage(myname);
  126.  
  127.     server = argv[0];
  128.     client = argv[1];
  129.  
  130.     /*
  131.      * Default is to specify just a host name or address. If user@host is
  132.      * specified, separate the two parts.
  133.      */
  134.     if ((at = strchr(client, '@')) != 0) {
  135.     user = client;
  136.     *at = 0;
  137.     host = at + 1;
  138.     } else {
  139.     user = FROM_UNKNOWN;
  140.     host = client;
  141.     }
  142.  
  143.     /*
  144.      * Note: all syslog stuff will be going to stderr, so the next calls are
  145.      * entirely superfluous.
  146.      */
  147. #ifdef LOG_MAIL
  148.     openlog(argv[0], LOG_PID, FACILITY);
  149. #else
  150.     openlog(argv[0], LOG_PID);
  151. #endif
  152.  
  153.     /*
  154.      * If a host address is specified, we simulate the effect of host name
  155.      * lookup failures.
  156.      */
  157.     if (inet_addr(host) != INADDR_NONE) {
  158.     try(server, FROM_UNKNOWN, host, user);
  159.     return (0);
  160.     }
  161.  
  162.     /*
  163.      * Otherwise, assume that a host name is specified, and insist that the
  164.      * address is known. The reason is that in real life, the host address is
  165.      * always available.
  166.      */
  167.     if ((hp = gethostbyname(host)) == 0) {
  168.     fprintf(stderr, "host %s: address lookup failed\n", host);
  169.     return (1);
  170.     }
  171.  
  172.     /*
  173.      * Iterate over all known addresses for this host. This way we find out
  174.      * if different addresses for the same host have different permissions,
  175.      * something that we probably do not want.
  176.      */
  177.     while (hp->h_addr_list[0])
  178.     try(server, hp->h_name,
  179.         inet_ntoa(*(struct in_addr *) * hp->h_addr_list++), user);
  180.  
  181.     return (0);
  182. }
  183.  
  184. /* dummy function to intercept the real shell_cmd() */
  185.  
  186. void    shell_cmd(cmd, daemon, client)
  187. char   *cmd;
  188. char   *daemon;
  189. struct client_info *client;
  190. {
  191.     char    buf[BUFSIZ];
  192.     int     pid = getpid();
  193.  
  194.     percent_x(buf, sizeof(buf), cmd, daemon, client, pid);
  195.     printf(" Command:  %s\n", buf);
  196. }
  197.  
  198. /* dummy function  to intercept the real clean_exit() */
  199.  
  200. /* ARGSUSED */
  201.  
  202. void    clean_exit(client)
  203. struct client_info *client;
  204. {
  205.     exit(0);
  206. }
  207.  
  208. /* dummy function  to intercept the real rfc931() */
  209.  
  210. /* ARGSUSED */
  211.  
  212. char   *rfc931(rmt_sin, our_sin)
  213. struct sockaddr_in *rmt_sin;
  214. struct sockaddr_in *our_sin;
  215. {
  216.     fprintf(stderr, "Oops - cannot do username lookups in verification mode\n");
  217.     return (0);
  218. }
  219.  
  220. #endif
  221.