OSDN Git Service

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