home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume39 / tcp_wrappers / part04 / tcpd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-29  |  3.0 KB  |  121 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 specified with the
  6.   * REAL_DAEMON_DIR macro. This arrangement requires that the network daemons
  7.   * are started by inetd or something similar. Connections and diagnostics
  8.   * are logged through syslog(3).
  9.   * 
  10.   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  11.   */
  12.  
  13. #ifndef lint
  14. static char sccsid[] = "@(#) tcpd.c 1.8 93/09/27 18:59:22";
  15. #endif
  16.  
  17. /* System libraries. */
  18.  
  19. #include <sys/types.h>
  20. #include <sys/param.h>
  21. #include <sys/stat.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <stdio.h>
  25. #include <syslog.h>
  26.  
  27. extern char *strrchr();
  28. extern char *strcpy();
  29.  
  30. #ifndef MAXPATHNAMELEN
  31. #define MAXPATHNAMELEN    BUFSIZ
  32. #endif
  33.  
  34. /* Local stuff. */
  35.  
  36. #include "patchlevel.h"
  37. #include "log_tcp.h"
  38.  
  39. int     allow_severity = SEVERITY;    /* run-time adjustable */
  40. int     deny_severity = LOG_WARNING;    /* ditto */
  41.  
  42. main(argc, argv)
  43. int     argc;
  44. char  **argv;
  45. {
  46.     struct client_info client;
  47.     int     from_stat;
  48.     char    path[MAXPATHNAMELEN];
  49.  
  50.     /* Attempt to prevent the creation of world-writable files. */
  51.  
  52. #ifdef DAEMON_UMASK
  53.     umask(DAEMON_UMASK);
  54. #endif
  55.  
  56.     /*
  57.      * If argv[0] is an absolute path name, ignore REAL_DAEMON_DIR, and strip
  58.      * argv[0] to its basename.
  59.      */
  60.  
  61.     if (argv[0][0] == '/') {
  62.     strcpy(path, argv[0]);
  63.     argv[0] = strrchr(argv[0], '/') + 1;
  64.     } else {
  65.     sprintf(path, "%s/%s", REAL_DAEMON_DIR, argv[0]);
  66.     }
  67.  
  68.     /*
  69.      * Open a channel to the syslog daemon. Older versions of openlog()
  70.      * require only two arguments.
  71.      */
  72.  
  73. #ifdef LOG_MAIL
  74.     (void) openlog(argv[0], LOG_PID, FACILITY);
  75. #else
  76.     (void) openlog(argv[0], LOG_PID);
  77. #endif
  78.  
  79.     /*
  80.      * Find out and verify the remote host name. Sites concerned with
  81.      * security may choose to refuse connections from hosts that pretend to
  82.      * have someone elses host name.
  83.      */
  84.  
  85.     from_stat = fromhost(&client);
  86. #ifdef PARANOID
  87.     if (from_stat == -1)
  88.     refuse(&client);
  89. #endif
  90.  
  91.     /*
  92.      * The BSD rlogin and rsh daemons that came out after 4.3 BSD disallow
  93.      * socket options at the IP level. They do so for a good reason.
  94.      * Unfortunately, we cannot use this with SunOS 4.1.x because the
  95.      * getsockopt() system call can panic the system.
  96.      */
  97.  
  98. #ifdef KILL_IP_OPTIONS
  99.     fix_options(&client);
  100. #endif
  101.  
  102.     /*
  103.      * Check whether this host can access the service in argv[0]. The
  104.      * access-control code invokes optional shell commands as specified in
  105.      * the access-control tables.
  106.      */
  107.  
  108. #ifdef HOSTS_ACCESS
  109.     if (!hosts_access(argv[0], &client))
  110.     refuse(&client);
  111. #endif
  112.  
  113.     /* Report remote client and invoke the real daemon program. */
  114.  
  115.     syslog(allow_severity, "connect from %s", hosts_info(&client));
  116.     (void) execv(path, argv);
  117.     syslog(LOG_ERR, "error: cannot execute %s: %m", path);
  118.     clean_exit(&client);
  119.     /* NOTREACHED */
  120. }
  121.