home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2173 / tcpd.c < prev   
C/C++ Source or Header  |  1990-12-28  |  2KB  |  92 lines

  1.  /*
  2.   * Wrapper around connection-oriented services. This program logs all
  3.   * connections and invokes the real daemon. For example, install as
  4.   * /usr/etc/{fingerd,telnetd,ftpd,rlogind,rshd,rexecd}, after saving the
  5.   * real daemons in the directory "/usr/etc/...".
  6.   * 
  7.   * Optionally refuses connections from hosts or domains specified in an
  8.   * external file.
  9.   * 
  10.   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  11.   */
  12.  
  13. #define    REAL_DAEMON_DIR    "/usr/etc/..."
  14.  
  15. #include <stdio.h>
  16. #include <sys/types.h>
  17. #include <sys/param.h>
  18. #include <sys/socket.h>
  19. #include <syslog.h>
  20. #include <netinet/in.h>
  21. #include <netdb.h>
  22.  
  23. #ifndef    MAXHOSTNAMELEN
  24. #define    MAXHOSTNAMELEN    BUFSIZ
  25. #endif
  26.  
  27. main(argc, argv)
  28. int     argc;
  29. char  **argv;
  30. {
  31.     int     sockt;
  32.     int     length;
  33.     struct sockaddr sa;
  34.     struct sockaddr_in *sin = (struct sockaddr_in *) (&sa);
  35.     char    host_name[MAXHOSTNAMELEN];
  36.     struct hostent *hp;
  37.     char   *strcpy();
  38.     char   *inet_ntoa();
  39.     void    exit();
  40.     void    syslog();
  41.     char    path[BUFSIZ];
  42.  
  43.     sockt = fileno(stdin);
  44.     length = sizeof(sa);
  45.  
  46. #ifdef    LOG_MAIL
  47.     (void) openlog(argv[0], LOG_PID, LOG_MAIL);
  48. #else
  49.     (void) openlog(argv[0], LOG_PID);
  50. #endif
  51.  
  52.     if (getpeername(sockt, &sa, &length) < 0) {
  53.     if (isatty(sockt)) {
  54.         (void) strcpy(host_name, "stdin");
  55.     } else {
  56.         syslog(LOG_ERR, "getpeername: %m");
  57.         (void) strcpy(host_name, "unknown");
  58.     }
  59.     } else {
  60.     switch (sa.sa_family) {
  61.     case AF_INET:
  62.         hp = gethostbyaddr((char *) &sin->sin_addr.s_addr,
  63.                    sizeof(sin->sin_addr.s_addr), AF_INET);
  64.         if (hp != NULL)
  65.         (void) strcpy(host_name, hp->h_name);
  66.         else
  67.         (void) strcpy(host_name, inet_ntoa(sin->sin_addr));
  68.         break;
  69.  
  70.     default:
  71.         syslog(LOG_ERR, "unknown address family %ld", sa.sa_family);
  72.         (void) strcpy(host_name, "unknown");
  73.     }
  74.     }
  75.  
  76.     syslog(LOG_INFO, "connect from %s", host_name);
  77.  
  78. #ifdef    HOSTS_DENY
  79.  
  80.     /* Deny access if host or domain is listed in hosts.deny file. */
  81.  
  82.     hosts_deny(host_name);
  83.  
  84. #endif
  85.  
  86.     /* Construct path to real daemon and invoke it */
  87.  
  88.     (void) sprintf(path, "%s/%s", REAL_DAEMON_DIR, argv[0]);
  89.     (void) execv(path, argv);
  90.     syslog(LOG_ERR, "%s: %m", path);
  91. }
  92.