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 / ptx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-27  |  3.5 KB  |  124 lines

  1.  /*
  2.   * The Dynix/PTX TLI implementation is not quite compatible with System V
  3.   * Release 4. Some important functions are not present so we are limited to
  4.   * IP-based services.
  5.   * 
  6.   * This module takes a TLI endpoint, and tries to determine the local IP
  7.   * address, the client IP address, and the remote username if username
  8.   * lookups are done irrespective of client. All results are in static memory
  9.   * and will be overwritten upon the next call.
  10.   * 
  11.   * The return status is (-1) if the remote host pretends to have someone elses
  12.   * name, or if the remote host name is available but could not be verified;
  13.   * in either case the hostname will be ignored. The return status is zero in
  14.   * all other cases (the hostname is unavailable, or the host name double
  15.   * check succeeds).
  16.   * 
  17.   * Diagnostics are reported through syslog(3).
  18.   * 
  19.   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  20.   */
  21.  
  22. #ifndef lint
  23. static char sccsid[] = "@(#) ptx.c 1.2 94/03/23 16:51:56";
  24. #endif
  25.  
  26. #ifdef PTX
  27.  
  28. /* System libraries. */
  29.  
  30. #include <sys/types.h>
  31. #include <sys/tiuser.h>
  32. #include <sys/socket.h>
  33. #include <stropts.h>
  34. #include <netinet/in.h>
  35. #include <netdb.h>
  36. #include <stdio.h>
  37. #include <syslog.h>
  38.  
  39. /* Local stuff. */
  40.  
  41. #include "log_tcp.h"
  42.  
  43. /* Forward declarations. */
  44.  
  45. static void ptx_sink();
  46.  
  47. /* tli_host - determine TLI endpoint info, PTX version */
  48.  
  49. int     tli_host(client, fd)
  50. struct client_info *client;
  51. int     fd;
  52. {
  53.     static struct sockaddr_in rmt_sin;
  54.     static struct sockaddr_in our_sin;
  55.     int     ret;
  56.  
  57.     /*
  58.      * Initialize the result with suitable defaults.
  59.      */
  60.  
  61.     init_client(client);
  62.     client->fd = fd;
  63.  
  64.     /*
  65.      * getpeerinaddr() was suggested by someone at Sequent. It seems to work
  66.      * with connection-oriented (TCP) services such as rlogind and telnetd,
  67.      * but it returns 0.0.0.0 with datagram (UDP) services. No problem: UDP
  68.      * needs special treatment anyway, in case we must refuse service.
  69.      */
  70.  
  71.     if (getpeerinaddr(client->fd, &rmt_sin, sizeof(rmt_sin)) == 0
  72.     && rmt_sin.sin_addr.s_addr != 0) {
  73.     client->rmt_sin = &rmt_sin;
  74.     if (getmyinaddr(client->fd, &our_sin, sizeof(our_sin)) == 0) {
  75.         client->our_sin = &our_sin;
  76.     } else {
  77.         syslog(LOG_ERR, "getmyinaddr: %m");
  78.     }
  79.     return (sock_names(client));
  80.     }
  81.  
  82.     /*
  83.      * Another suggestion was to temporarily switch to the socket interface,
  84.      * identify the client name/address with socket calls, then to switch
  85.      * back to TLI. This seems to works OK with UDP services, but utterly
  86.      * messes up rlogind and telnetd. No problem, rlogind and telnetd are
  87.      * taken care of by the code above.
  88.      */
  89.  
  90. #define SWAP_MODULE(f, old, new) (ioctl(f, I_POP, old), ioctl(f, I_PUSH, new))
  91.  
  92.     if (SWAP_MODULE(client->fd, "timod", "sockmod") != 0)
  93.     syslog(LOG_ERR, "replace timod by sockmod: %m");
  94.     ret = sock_host(client, client->fd);
  95.     if (SWAP_MODULE(client->fd, "sockmod", "timod") != 0)
  96.     syslog(LOG_ERR, "replace sockmod by timod: %m");
  97.     if (client->sink != 0)
  98.     client->sink = ptx_sink;
  99.     return (ret);
  100. }
  101.  
  102. /* ptx_sink - absorb unreceived IP datagram */
  103.  
  104. static void ptx_sink(fd)
  105. int     fd;
  106. {
  107.     char    buf[BUFSIZ];
  108.     struct sockaddr sa;
  109.     int     size = sizeof(sa);
  110.  
  111.     /*
  112.      * Eat up the not-yet received datagram. Where needed, switch to the
  113.      * socket programming interface.
  114.      */
  115.  
  116.     if (ioctl(fd, I_FIND, "timod") != 0)
  117.     ioctl(fd, I_POP, "timod");
  118.     if (ioctl(fd, I_FIND, "sockmod") == 0)
  119.     ioctl(fd, I_PUSH, "sockmod");
  120.     (void) recvfrom(fd, buf, sizeof(buf), 0, &sa, &size);
  121. }
  122.  
  123. #endif /* PTX */
  124.