installer - Several improvements
[dragonfly.git] / contrib / tcp_wrappers / tcpd.c
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   * $FreeBSD: src/contrib/tcp_wrappers/tcpd.c,v 1.2 2000/02/03 10:26:59 shin Exp $
13   */
14
15 #ifndef lint
16 static char sccsid[] = "@(#) tcpd.c 1.10 96/02/11 17:01:32";
17 #endif
18
19 /* System libraries. */
20
21 #include <sys/types.h>
22 #include <sys/param.h>
23 #include <sys/stat.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <stdio.h>
27 #include <syslog.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #ifndef MAXPATHNAMELEN
32 #define MAXPATHNAMELEN  BUFSIZ
33 #endif
34
35 #ifndef STDIN_FILENO
36 #define STDIN_FILENO    0
37 #endif
38
39 /* Local stuff. */
40
41 #include "patchlevel.h"
42 #include "tcpd.h"
43
44 int     allow_severity = SEVERITY;      /* run-time adjustable */
45 int     deny_severity = LOG_WARNING;    /* ditto */
46
47 int
48 main(argc, argv)
49 int     argc;
50 char  **argv;
51 {
52     struct request_info request;
53     char    path[MAXPATHNAMELEN];
54
55     /* Attempt to prevent the creation of world-writable files. */
56
57 #ifdef DAEMON_UMASK
58     umask(DAEMON_UMASK);
59 #endif
60
61     /*
62      * If argv[0] is an absolute path name, ignore REAL_DAEMON_DIR, and strip
63      * argv[0] to its basename.
64      */
65
66     if (argv[0][0] == '/') {
67         strcpy(path, argv[0]);
68         argv[0] = strrchr(argv[0], '/') + 1;
69     } else {
70         sprintf(path, "%s/%s", REAL_DAEMON_DIR, argv[0]);
71     }
72
73     /*
74      * Open a channel to the syslog daemon. Older versions of openlog()
75      * require only two arguments.
76      */
77
78 #ifdef LOG_MAIL
79     (void) openlog(argv[0], LOG_PID, FACILITY);
80 #else
81     (void) openlog(argv[0], LOG_PID);
82 #endif
83
84     /*
85      * Find out the endpoint addresses of this conversation. Host name
86      * lookups and double checks will be done on demand.
87      */
88
89     request_init(&request, RQ_DAEMON, argv[0], RQ_FILE, STDIN_FILENO, 0);
90     fromhost(&request);
91
92     /*
93      * Optionally look up and double check the remote host name. Sites
94      * concerned with security may choose to refuse connections from hosts
95      * that pretend to have someone elses host name.
96      */
97
98 #ifdef PARANOID
99     if (STR_EQ(eval_hostname(request.client), paranoid))
100         refuse(&request);
101 #endif
102
103     /*
104      * The BSD rlogin and rsh daemons that came out after 4.3 BSD disallow
105      * socket options at the IP level. They do so for a good reason.
106      * Unfortunately, we cannot use this with SunOS 4.1.x because the
107      * getsockopt() system call can panic the system.
108      */
109
110 #ifdef KILL_IP_OPTIONS
111     fix_options(&request);
112 #endif
113
114     /*
115      * Check whether this host can access the service in argv[0]. The
116      * access-control code invokes optional shell commands as specified in
117      * the access-control tables.
118      */
119
120 #ifdef HOSTS_ACCESS
121     if (!hosts_access(&request))
122         refuse(&request);
123 #endif
124
125     /* Report request and invoke the real daemon program. */
126
127 #ifdef INET6
128     syslog(allow_severity, "connect from %s (%s)",
129            eval_client(&request), eval_hostaddr(request.client));
130 #else
131     syslog(allow_severity, "connect from %s", eval_client(&request));
132 #endif
133     closelog();
134     (void) execv(path, argv);
135     syslog(LOG_ERR, "error: cannot execute %s: %m", path);
136     clean_exit(&request);
137     /* NOTREACHED */
138 }