home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-A.06 / NETKIT-A / NetKit-A-0.06 / tcp_wrapper-6.3 / try.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-27  |  6.2 KB  |  254 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.11 94/03/23 17:03:14";
  21. #endif
  22.  
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #include <netdb.h>
  28. #include <stdio.h>
  29. #include <syslog.h>
  30. #include <setjmp.h>
  31.  
  32. extern void exit();
  33. extern char *strchr();
  34. extern char *strcpy();
  35.  
  36. #ifndef HOSTS_ACCESS
  37.  
  38. main()
  39. {
  40.     fprintf(stderr, "host access control is not enabled.\n");
  41.     return (1);
  42. }
  43.  
  44. #else
  45.  
  46. #ifndef    INADDR_NONE
  47. #define    INADDR_NONE    (-1)        /* XXX should be 0xffffffff */
  48. #endif
  49.  
  50. #include "log_tcp.h"
  51. #include "options.h"
  52.  
  53. int     allow_severity = SEVERITY;    /* run-time adjustable */
  54. int     deny_severity = LOG_WARNING;    /* ditto */
  55. int     rfc931_timeout = RFC931_TIMEOUT;/* ditto */
  56.  
  57. /* usage - explain */
  58.  
  59. void    usage(myname)
  60. char   *myname;
  61. {
  62.     fprintf(stderr,
  63.         "usage: %s [-d] process_name [user@]host_name_or_address\n",
  64.         myname);
  65.     exit(1);
  66. }
  67.  
  68. /* Try out a (daemon,client) pair */
  69.  
  70. void    try(daemon, name, addr, user)
  71. char   *daemon;
  72. char   *name;
  73. char   *addr;
  74. char   *user;
  75. {
  76.     int     verdict;
  77.  
  78.     /*
  79.      * The dry_run flag informs the optional extension language routines that
  80.      * they are being run in verification mode, and that they should not
  81.      * perform any real action. Extension language routines that would not
  82.      * return should inform us of their plan, by clearing the dry_run flag.
  83.      * This is a bit clumsy but we must be able to verify hosts with more
  84.      * than one network address; just terminating the program isn't
  85.      * acceptable.
  86.      */
  87.  
  88.     dry_run = 1;
  89.  
  90.     /* Reset other stuff that might be changed by options handlers. */
  91.  
  92.     rfc931_timeout = RFC931_TIMEOUT;
  93.     allow_severity = SEVERITY;
  94.     deny_severity = LOG_WARNING;
  95.  
  96.     printf(" Daemon:   %s\n", daemon);
  97.     printf(" Hostname: %s\n", name);
  98.     printf(" Address:  %s\n", addr);
  99.  
  100.     if (user[0] && strcasecmp(user, FROM_UNKNOWN))
  101.     printf(" Username: %s\n", user);
  102.  
  103.     verdict = hosts_ctl(daemon, name, addr, user);
  104.  
  105.     printf(" Access:   %s\n",
  106.        dry_run == 0 ? "delegated" :
  107.        verdict ? "granted" : "denied");
  108. }
  109.  
  110. int     main(argc, argv)
  111. int     argc;
  112. char  **argv;
  113. {
  114.     struct hostent *hp;
  115.     char   *myname = argv[0];
  116.     char   *client;
  117.     char   *server;
  118.     char   *at;
  119.     char   *user;
  120.     char   *host;
  121.     char    reverse_name[BUFSIZ];
  122.     struct in_addr addr;
  123.  
  124.     /*
  125.      * Parse the JCL.
  126.      */
  127.     while (--argc && *++argv && **argv == '-') {
  128.     if (strcmp(*argv, "-d") == 0) {        /* use tables in . */
  129.         hosts_allow_table = "hosts.allow";
  130.         hosts_deny_table = "hosts.deny";
  131.     } else {
  132.         usage(myname);
  133.     }
  134.     }
  135.     if (argc != 2)
  136.     usage(myname);
  137.  
  138.     server = argv[0];
  139.     client = argv[1];
  140.  
  141.     /*
  142.      * Default is to specify just a host name or address. If user@host is
  143.      * specified, separate the two parts.
  144.      */
  145.     if ((at = strchr(client, '@')) != 0) {
  146.     user = client;
  147.     *at = 0;
  148.     host = at + 1;
  149.     } else {
  150.     user = FROM_UNKNOWN;
  151.     host = client;
  152.     }
  153.  
  154.     /*
  155.      * Note: all syslog stuff will be going to stderr, so the next calls are
  156.      * entirely superfluous.
  157.      */
  158. #ifdef LOG_MAIL
  159.     openlog(argv[0], LOG_PID, FACILITY);
  160. #else
  161.     openlog(argv[0], LOG_PID);
  162. #endif
  163.  
  164.     /*
  165.      * If a host address is specified, we simulate the effect of host name
  166.      * lookup failures.
  167.      */
  168.     if (inet_addr(host) != INADDR_NONE) {
  169.     try(server, FROM_UNKNOWN, host, user);
  170.     return (0);
  171.     }
  172.  
  173.     /*
  174.      * Otherwise, assume that a host name is specified, and insist that the
  175.      * address is known. The reason is that in real life, the host address is
  176.      * always available (at least with IP).
  177.      */
  178.     if ((hp = gethostbyname(host)) == 0) {
  179.     fprintf(stderr, "host %s: address lookup failed\n", host);
  180.     return (1);
  181.     }
  182.     if (hp->h_addrtype != 0 && hp->h_addrtype != AF_INET) {
  183.     fprintf(stderr,
  184.         "Sorry, this test program cannot handle address family %d\n",
  185.         hp->h_addrtype);
  186.     return (1);
  187.     }
  188.     memcpy((char *) &addr, hp->h_addr_list[0], sizeof(addr));
  189.  
  190.     /*
  191.      * Use the hostname that gethostbyaddr() would give us. On systems with
  192.      * NIS this may be an unqualified name, even when an FQDN was given on
  193.      * the command line.
  194.      */
  195.     if ((hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET)) == 0) {
  196.     fprintf(stderr, "host %s: address->name lookup failed\n", host);
  197.     return (1);
  198.     }
  199.     strcpy(reverse_name, hp->h_name);
  200.     while ((hp = gethostbyname(reverse_name)) == 0)    /* XXX */
  201.     /* void */ ;
  202.  
  203.     /*
  204.      * Iterate over all known addresses for this host. This way we find out
  205.      * if different addresses for the same host have different permissions,
  206.      * something that we may not want.
  207.      */
  208.     while (hp->h_addr_list[0]) {
  209.     try(server, reverse_name,
  210.         inet_ntoa(*(struct in_addr *) * hp->h_addr_list++), user);
  211.     if (hp->h_addr_list[0])
  212.         putchar('\n');
  213.     }
  214.     return (0);
  215. }
  216.  
  217. /* dummy function to intercept the real shell_cmd() */
  218.  
  219. void    shell_cmd(cmd, daemon, client)
  220. char   *cmd;
  221. char   *daemon;
  222. struct client_info *client;
  223. {
  224.     char    buf[BUFSIZ];
  225.     int     pid = getpid();
  226.  
  227.     percent_x(buf, sizeof(buf), cmd, daemon, client, pid);
  228.     printf(" Command:  %s\n", buf);
  229. }
  230.  
  231. /* dummy function  to intercept the real clean_exit() */
  232.  
  233. /* ARGSUSED */
  234.  
  235. void    clean_exit(client)
  236. struct client_info *client;
  237. {
  238.     exit(0);
  239. }
  240.  
  241. /* dummy function  to intercept the real rfc931() */
  242.  
  243. /* ARGSUSED */
  244.  
  245. char   *rfc931(rmt_sin, our_sin)
  246. struct sockaddr_in *rmt_sin;
  247. struct sockaddr_in *our_sin;
  248. {
  249.     fprintf(stderr, "Oops - cannot do username lookups in verification mode\n");
  250.     return (0);
  251. }
  252.  
  253. #endif
  254.