xref: /titanic_51/usr/src/cmd/tcpd/tcpd.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate  /*
2*7c478bd9Sstevel@tonic-gate   * General front end for stream and datagram IP services. This program logs
3*7c478bd9Sstevel@tonic-gate   * the remote host name and then invokes the real daemon. For example,
4*7c478bd9Sstevel@tonic-gate   * install as /usr/etc/{tftpd,fingerd,telnetd,ftpd,rlogind,rshd,rexecd},
5*7c478bd9Sstevel@tonic-gate   * after saving the real daemons in the directory specified with the
6*7c478bd9Sstevel@tonic-gate   * REAL_DAEMON_DIR macro. This arrangement requires that the network daemons
7*7c478bd9Sstevel@tonic-gate   * are started by inetd or something similar. Connections and diagnostics
8*7c478bd9Sstevel@tonic-gate   * are logged through syslog(3).
9*7c478bd9Sstevel@tonic-gate   *
10*7c478bd9Sstevel@tonic-gate   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
11*7c478bd9Sstevel@tonic-gate   */
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate #ifndef lint
14*7c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#) tcpd.c 1.10 96/02/11 17:01:32";
15*7c478bd9Sstevel@tonic-gate #endif
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate /* System libraries. */
18*7c478bd9Sstevel@tonic-gate 
19*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
20*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
21*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
22*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
23*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
24*7c478bd9Sstevel@tonic-gate #include <stdio.h>
25*7c478bd9Sstevel@tonic-gate #include <syslog.h>
26*7c478bd9Sstevel@tonic-gate #include <string.h>
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #ifndef MAXPATHNAMELEN
29*7c478bd9Sstevel@tonic-gate #define MAXPATHNAMELEN	BUFSIZ
30*7c478bd9Sstevel@tonic-gate #endif
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #ifndef STDIN_FILENO
33*7c478bd9Sstevel@tonic-gate #define STDIN_FILENO	0
34*7c478bd9Sstevel@tonic-gate #endif
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /* Local stuff. */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "patchlevel.h"
39*7c478bd9Sstevel@tonic-gate #include "tcpd.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate int     allow_severity = SEVERITY;	/* run-time adjustable */
42*7c478bd9Sstevel@tonic-gate int     deny_severity = LOG_WARNING;	/* ditto */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate main(argc, argv)
45*7c478bd9Sstevel@tonic-gate int     argc;
46*7c478bd9Sstevel@tonic-gate char  **argv;
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate     struct request_info request;
49*7c478bd9Sstevel@tonic-gate     char    path[MAXPATHNAMELEN];
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate     /* Attempt to prevent the creation of world-writable files. */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #ifdef DAEMON_UMASK
54*7c478bd9Sstevel@tonic-gate     umask(DAEMON_UMASK);
55*7c478bd9Sstevel@tonic-gate #endif
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate     /*
58*7c478bd9Sstevel@tonic-gate      * If argv[0] is an absolute path name, ignore REAL_DAEMON_DIR, and strip
59*7c478bd9Sstevel@tonic-gate      * argv[0] to its basename.
60*7c478bd9Sstevel@tonic-gate      */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate     if (argv[0][0] == '/') {
63*7c478bd9Sstevel@tonic-gate 	strcpy(path, argv[0]);
64*7c478bd9Sstevel@tonic-gate 	argv[0] = strrchr(argv[0], '/') + 1;
65*7c478bd9Sstevel@tonic-gate     } else {
66*7c478bd9Sstevel@tonic-gate 	sprintf(path, "%s/%s", REAL_DAEMON_DIR, argv[0]);
67*7c478bd9Sstevel@tonic-gate     }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate     /*
70*7c478bd9Sstevel@tonic-gate      * Open a channel to the syslog daemon. Older versions of openlog()
71*7c478bd9Sstevel@tonic-gate      * require only two arguments.
72*7c478bd9Sstevel@tonic-gate      */
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate #ifdef LOG_MAIL
75*7c478bd9Sstevel@tonic-gate     (void) openlog(argv[0], LOG_PID, FACILITY);
76*7c478bd9Sstevel@tonic-gate #else
77*7c478bd9Sstevel@tonic-gate     (void) openlog(argv[0], LOG_PID);
78*7c478bd9Sstevel@tonic-gate #endif
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate     /*
81*7c478bd9Sstevel@tonic-gate      * Find out the endpoint addresses of this conversation. Host name
82*7c478bd9Sstevel@tonic-gate      * lookups and double checks will be done on demand.
83*7c478bd9Sstevel@tonic-gate      */
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate     request_init(&request, RQ_DAEMON, argv[0], RQ_FILE, STDIN_FILENO, 0);
86*7c478bd9Sstevel@tonic-gate     fromhost(&request);
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate     /*
89*7c478bd9Sstevel@tonic-gate      * Optionally look up and double check the remote host name. Sites
90*7c478bd9Sstevel@tonic-gate      * concerned with security may choose to refuse connections from hosts
91*7c478bd9Sstevel@tonic-gate      * that pretend to have someone elses host name.
92*7c478bd9Sstevel@tonic-gate      */
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate #ifdef PARANOID
95*7c478bd9Sstevel@tonic-gate     if (STR_EQ(eval_hostname(request.client), paranoid))
96*7c478bd9Sstevel@tonic-gate 	refuse(&request);
97*7c478bd9Sstevel@tonic-gate #endif
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate     /*
100*7c478bd9Sstevel@tonic-gate      * The BSD rlogin and rsh daemons that came out after 4.3 BSD disallow
101*7c478bd9Sstevel@tonic-gate      * socket options at the IP level. They do so for a good reason.
102*7c478bd9Sstevel@tonic-gate      * Unfortunately, we cannot use this with SunOS 4.1.x because the
103*7c478bd9Sstevel@tonic-gate      * getsockopt() system call can panic the system.
104*7c478bd9Sstevel@tonic-gate      */
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate #ifdef KILL_IP_OPTIONS
107*7c478bd9Sstevel@tonic-gate     fix_options(&request);
108*7c478bd9Sstevel@tonic-gate #endif
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate     /*
111*7c478bd9Sstevel@tonic-gate      * Check whether this host can access the service in argv[0]. The
112*7c478bd9Sstevel@tonic-gate      * access-control code invokes optional shell commands as specified in
113*7c478bd9Sstevel@tonic-gate      * the access-control tables.
114*7c478bd9Sstevel@tonic-gate      */
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate #ifdef HOSTS_ACCESS
117*7c478bd9Sstevel@tonic-gate     if (!hosts_access(&request))
118*7c478bd9Sstevel@tonic-gate 	refuse(&request);
119*7c478bd9Sstevel@tonic-gate #endif
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate     /* Report request and invoke the real daemon program. */
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate     syslog(allow_severity, "connect from %s", eval_client(&request));
124*7c478bd9Sstevel@tonic-gate     closelog();
125*7c478bd9Sstevel@tonic-gate     (void) execv(path, argv);
126*7c478bd9Sstevel@tonic-gate     syslog(LOG_ERR, "error: cannot execute %s: %m", path);
127*7c478bd9Sstevel@tonic-gate     clean_exit(&request);
128*7c478bd9Sstevel@tonic-gate     /* NOTREACHED */
129*7c478bd9Sstevel@tonic-gate }
130