home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume30 / log_tcp / part02 / tcpd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-20  |  3.1 KB  |  119 lines

  1.  /*
  2.   * General front end for stream and datagram IP services. This program logs
  3.   * the remote host name and then invokes the real daemon. For example,
  4.   * install as /usr/etc/{tftpd,fingerd,telnetd,ftpd,rlogind,rshd,rexecd},
  5.   * after saving the real daemons in the directory "/usr/etc/...". This
  6.   * arrangement requires that the network daemons are started by inetd or
  7.   * something similar. Connections and diagnostics are logged through
  8.   * syslog(3).
  9.   * 
  10.   * Compile with -DHOSTS_ACCESS in order to enable access control. See the
  11.   * hosts_access(5) manual page for details.
  12.   * 
  13.   * Compile with -DPARANOID if service should be refused to hosts that pretend
  14.   * to have someone elses host name. This gives some protection against rsh
  15.   * and rlogin attacks that involve compromised domain name servers.
  16.   * 
  17.   * Compile with -DDAEMON_UMASK=nnn if daemons should run with a non-default
  18.   * umask value (the system default is 000, resulting in world-writable
  19.   * files).
  20.   * 
  21.   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  22.   */
  23.  
  24. #ifndef lint
  25. static char sccsid[] = "@(#) tcpd.c 1.3 92/06/11 22:21:22";
  26. #endif
  27.  
  28. /* System libraries. */
  29.  
  30. #include <sys/types.h>
  31. #include <sys/param.h>
  32. #include <sys/stat.h>
  33. #include <stdio.h>
  34. #include <syslog.h>
  35.  
  36. extern char *strrchr();
  37. extern char *strcpy();
  38.  
  39. #ifndef MAXPATHNAMELEN
  40. #define MAXPATHNAMELEN    BUFSIZ
  41. #endif
  42.  
  43. /* Local stuff. */
  44.  
  45. #include "log_tcp.h"
  46.  
  47. /* The following specifies where the vendor-provided daemons should go. */
  48.  
  49. #define REAL_DAEMON_DIR    "/usr/etc/..."
  50.  
  51. main(argc, argv)
  52. int     argc;
  53. char  **argv;
  54. {
  55.     struct from_host from;
  56.     int     from_stat;
  57.     char    path[MAXPATHNAMELEN];
  58.  
  59.     /* Attempt to prevent the creation of world-writable files. */
  60.  
  61. #ifdef DAEMON_UMASK
  62.     umask(DAEMON_UMASK);
  63. #endif
  64.  
  65.     /*
  66.      * If argv[0] is an absolute path name, ignore REAL_DAEMON_DIR, and strip
  67.      * argv[0] to its basename.
  68.      */
  69.  
  70.     if (argv[0][0] == '/') {
  71.     strcpy(path, argv[0]);
  72.     argv[0] = strrchr(argv[0], '/') + 1;
  73.     } else {
  74.     sprintf(path, "%s/%s", REAL_DAEMON_DIR, argv[0]);
  75.     }
  76.  
  77.     /*
  78.      * Open a channel to the syslog daemon. Older versions of openlog()
  79.      * require only two arguments.
  80.      */
  81.  
  82. #ifdef LOG_MAIL
  83.     (void) openlog(argv[0], LOG_PID, FACILITY);
  84. #else
  85.     (void) openlog(argv[0], LOG_PID);
  86. #endif
  87.  
  88.     /*
  89.      * Find out and verify the remote host name. Sites concerned with
  90.      * security may choose to refuse connections from hosts that pretend to
  91.      * have someone elses host name.
  92.      */
  93.  
  94.     from_stat = fromhost(&from);
  95. #ifdef PARANOID
  96.     if (from_stat == -1)
  97.     refuse(&from);
  98. #endif
  99.  
  100.     /*
  101.      * Check whether this host can access the service in argv[0]. The
  102.      * access-control code invokes optional shell commands as specified in
  103.      * the access-control tables.
  104.      */
  105.  
  106. #ifdef HOSTS_ACCESS
  107.     if (!hosts_access(argv[0], &from))
  108.     refuse(&from);
  109. #endif
  110.  
  111.     /* Report remote client and invoke the real daemon program. */
  112.  
  113.     syslog(LOG_INFO, "connect from %s", hosts_info(&from));
  114.     (void) execv(path, argv);
  115.     syslog(LOG_ERR, "%s: %m", path);
  116.     clean_exit(&from);
  117.     /* NOTREACHED */
  118. }
  119.