xref: /freebsd/contrib/tcp_wrappers/tcpd.c (revision c697fb7f)
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$
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 main(argc, argv)
48 int     argc;
49 char  **argv;
50 {
51     struct request_info request;
52     char    path[MAXPATHNAMELEN];
53 
54     /* Attempt to prevent the creation of world-writable files. */
55 
56 #ifdef DAEMON_UMASK
57     umask(DAEMON_UMASK);
58 #endif
59 
60     /*
61      * If argv[0] is an absolute path name, ignore REAL_DAEMON_DIR, and strip
62      * argv[0] to its basename.
63      */
64 
65     if (argv[0][0] == '/') {
66 	strlcpy(path, argv[0], sizeof(path));
67 	argv[0] = strrchr(argv[0], '/') + 1;
68     } else {
69 	snprintf(path, sizeof(path), "%s/%s", REAL_DAEMON_DIR, argv[0]);
70     }
71 
72     /*
73      * Open a channel to the syslog daemon. Older versions of openlog()
74      * require only two arguments.
75      */
76 
77 #ifdef LOG_MAIL
78     (void) openlog(argv[0], LOG_PID, FACILITY);
79 #else
80     (void) openlog(argv[0], LOG_PID);
81 #endif
82 
83     /*
84      * Find out the endpoint addresses of this conversation. Host name
85      * lookups and double checks will be done on demand.
86      */
87 
88     request_init(&request, RQ_DAEMON, argv[0], RQ_FILE, STDIN_FILENO, 0);
89     fromhost(&request);
90 
91     /*
92      * Optionally look up and double check the remote host name. Sites
93      * concerned with security may choose to refuse connections from hosts
94      * that pretend to have someone elses host name.
95      */
96 
97 #ifdef PARANOID
98     if (STR_EQ(eval_hostname(request.client), paranoid))
99 	refuse(&request);
100 #endif
101 
102     /*
103      * The BSD rlogin and rsh daemons that came out after 4.3 BSD disallow
104      * socket options at the IP level. They do so for a good reason.
105      * Unfortunately, we cannot use this with SunOS 4.1.x because the
106      * getsockopt() system call can panic the system.
107      */
108 
109 #ifdef KILL_IP_OPTIONS
110     fix_options(&request);
111 #endif
112 
113     /*
114      * Check whether this host can access the service in argv[0]. The
115      * access-control code invokes optional shell commands as specified in
116      * the access-control tables.
117      */
118 
119 #ifdef HOSTS_ACCESS
120     if (!hosts_access(&request))
121 	refuse(&request);
122 #endif
123 
124     /* Report request and invoke the real daemon program. */
125 
126 #ifdef INET6
127     syslog(allow_severity, "connect from %s (%s)",
128 	   eval_client(&request), eval_hostaddr(request.client));
129 #else
130     syslog(allow_severity, "connect from %s", eval_client(&request));
131 #endif
132     closelog();
133     (void) execv(path, argv);
134     syslog(LOG_ERR, "error: cannot execute %s: %m", path);
135     clean_exit(&request);
136     /* NOTREACHED */
137 }
138