home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2173 / miscd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.1 KB  |  89 lines

  1.  /*
  2.   * Wrapper around the ULTRIX miscd daemon. This program logs all connections
  3.   * and invokes the real daemon. For example, install as "/usr/etc/miscd",
  4.   * after saving the real daemon as "/usr/etc/.../miscd". miscd implements
  5.   * the systat service, which pipes the output from who(1) to stdout. This
  6.   * information is potentially useful to systems crackers.
  7.   * 
  8.   * Optionally refuses connections from hosts or domains specified in an
  9.   * external file.
  10.   * 
  11.   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  12.   */
  13.  
  14. #define    REAL_DAEMON    "/usr/etc/.../miscd"
  15.  
  16. #include <stdio.h>
  17. #include <sys/types.h>
  18. #include <sys/param.h>
  19. #include <sys/socket.h>
  20. #include <syslog.h>
  21. #include <netinet/in.h>
  22. #include <netdb.h>
  23.  
  24. #ifndef    MAXHOSTNAMELEN
  25. #define    MAXHOSTNAMELEN    BUFSIZ
  26. #endif
  27.  
  28. main(argc, argv)
  29. int     argc;
  30. char  **argv;
  31. {
  32.     int     sockt;
  33.     int     length;
  34.     struct sockaddr sa;
  35.     struct sockaddr_in *sin = (struct sockaddr_in *) (&sa);
  36.     char    host_name[MAXHOSTNAMELEN];
  37.     struct hostent *hp;
  38.     char   *strcpy();
  39.     char   *inet_ntoa();
  40.     void    exit();
  41.     void    syslog();
  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.     (void) execv(REAL_DAEMON, argv);
  87.     syslog(LOG_ERR, "%s: %m", REAL_DAEMON);
  88. }
  89.