dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / tcpd / tcpd.c
blob20038742ef38671e4cce7d4604b3fc532eef01b9
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.
13 static char sccsid[] = "@(#) tcpd.c 1.10 96/02/11 17:01:32";
15 /* System libraries. */
17 #include <sys/types.h>
18 #include <sys/param.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <stdio.h>
23 #include <syslog.h>
24 #include <string.h>
26 #ifndef MAXPATHNAMELEN
27 #define MAXPATHNAMELEN BUFSIZ
28 #endif
30 #ifndef STDIN_FILENO
31 #define STDIN_FILENO 0
32 #endif
34 /* Local stuff. */
36 #include "patchlevel.h"
37 #include "tcpd.h"
39 int allow_severity = SEVERITY; /* run-time adjustable */
40 int deny_severity = LOG_WARNING; /* ditto */
42 int
43 main(argc, argv)
44 int argc;
45 char **argv;
47 struct request_info request;
48 char path[MAXPATHNAMELEN];
50 /* Attempt to prevent the creation of world-writable files. */
52 #ifdef DAEMON_UMASK
53 umask(DAEMON_UMASK);
54 #endif
57 * If argv[0] is an absolute path name, ignore REAL_DAEMON_DIR, and strip
58 * argv[0] to its basename.
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]);
69 * Open a channel to the syslog daemon. Older versions of openlog()
70 * require only two arguments.
73 #ifdef LOG_MAIL
74 (void) openlog(argv[0], LOG_PID, FACILITY);
75 #else
76 (void) openlog(argv[0], LOG_PID);
77 #endif
80 * Find out the endpoint addresses of this conversation. Host name
81 * lookups and double checks will be done on demand.
84 request_init(&request, RQ_DAEMON, argv[0], RQ_FILE, STDIN_FILENO, 0);
85 fromhost(&request);
88 * Optionally look up and double check the remote host name. Sites
89 * concerned with security may choose to refuse connections from hosts
90 * that pretend to have someone elses host name.
93 #ifdef PARANOID
94 if (STR_EQ(eval_hostname(request.client), paranoid))
95 refuse(&request);
96 #endif
99 * The BSD rlogin and rsh daemons that came out after 4.3 BSD disallow
100 * socket options at the IP level. They do so for a good reason.
101 * Unfortunately, we cannot use this with SunOS 4.1.x because the
102 * getsockopt() system call can panic the system.
105 #ifdef KILL_IP_OPTIONS
106 fix_options(&request);
107 #endif
110 * Check whether this host can access the service in argv[0]. The
111 * access-control code invokes optional shell commands as specified in
112 * the access-control tables.
115 #ifdef HOSTS_ACCESS
116 if (!hosts_access(&request))
117 refuse(&request);
118 #endif
120 /* Report request and invoke the real daemon program. */
122 syslog(allow_severity, "connect from %s", eval_client(&request));
123 closelog();
124 (void) execv(path, argv);
125 syslog(LOG_ERR, "error: cannot execute %s: %m", path);
126 clean_exit(&request);
127 /* NOTREACHED */