xref: /illumos-gate/usr/src/cmd/tcpd/tcpd.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate  /*
27c478bd9Sstevel@tonic-gate   * General front end for stream and datagram IP services. This program logs
37c478bd9Sstevel@tonic-gate   * the remote host name and then invokes the real daemon. For example,
47c478bd9Sstevel@tonic-gate   * install as /usr/etc/{tftpd,fingerd,telnetd,ftpd,rlogind,rshd,rexecd},
57c478bd9Sstevel@tonic-gate   * after saving the real daemons in the directory specified with the
67c478bd9Sstevel@tonic-gate   * REAL_DAEMON_DIR macro. This arrangement requires that the network daemons
77c478bd9Sstevel@tonic-gate   * are started by inetd or something similar. Connections and diagnostics
87c478bd9Sstevel@tonic-gate   * are logged through syslog(3).
9*55fea89dSDan Cross   *
107c478bd9Sstevel@tonic-gate   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
117c478bd9Sstevel@tonic-gate   */
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #ifndef lint
147c478bd9Sstevel@tonic-gate static char sccsid[] = "@(#) tcpd.c 1.10 96/02/11 17:01:32";
157c478bd9Sstevel@tonic-gate #endif
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate /* System libraries. */
187c478bd9Sstevel@tonic-gate 
197c478bd9Sstevel@tonic-gate #include <sys/types.h>
207c478bd9Sstevel@tonic-gate #include <sys/param.h>
217c478bd9Sstevel@tonic-gate #include <sys/stat.h>
227c478bd9Sstevel@tonic-gate #include <sys/socket.h>
237c478bd9Sstevel@tonic-gate #include <netinet/in.h>
247c478bd9Sstevel@tonic-gate #include <stdio.h>
257c478bd9Sstevel@tonic-gate #include <syslog.h>
267c478bd9Sstevel@tonic-gate #include <string.h>
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #ifndef MAXPATHNAMELEN
297c478bd9Sstevel@tonic-gate #define MAXPATHNAMELEN	BUFSIZ
307c478bd9Sstevel@tonic-gate #endif
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #ifndef STDIN_FILENO
337c478bd9Sstevel@tonic-gate #define STDIN_FILENO	0
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* Local stuff. */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "patchlevel.h"
397c478bd9Sstevel@tonic-gate #include "tcpd.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate int     allow_severity = SEVERITY;	/* run-time adjustable */
427c478bd9Sstevel@tonic-gate int     deny_severity = LOG_WARNING;	/* ditto */
437c478bd9Sstevel@tonic-gate 
449455584cSIgor Kozhukhov int
main(argc,argv)457c478bd9Sstevel@tonic-gate main(argc, argv)
467c478bd9Sstevel@tonic-gate int     argc;
477c478bd9Sstevel@tonic-gate char  **argv;
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate     struct request_info request;
507c478bd9Sstevel@tonic-gate     char    path[MAXPATHNAMELEN];
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate     /* Attempt to prevent the creation of world-writable files. */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #ifdef DAEMON_UMASK
557c478bd9Sstevel@tonic-gate     umask(DAEMON_UMASK);
567c478bd9Sstevel@tonic-gate #endif
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate     /*
597c478bd9Sstevel@tonic-gate      * If argv[0] is an absolute path name, ignore REAL_DAEMON_DIR, and strip
607c478bd9Sstevel@tonic-gate      * argv[0] to its basename.
617c478bd9Sstevel@tonic-gate      */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate     if (argv[0][0] == '/') {
647c478bd9Sstevel@tonic-gate 	strcpy(path, argv[0]);
657c478bd9Sstevel@tonic-gate 	argv[0] = strrchr(argv[0], '/') + 1;
667c478bd9Sstevel@tonic-gate     } else {
677c478bd9Sstevel@tonic-gate 	sprintf(path, "%s/%s", REAL_DAEMON_DIR, argv[0]);
687c478bd9Sstevel@tonic-gate     }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate     /*
717c478bd9Sstevel@tonic-gate      * Open a channel to the syslog daemon. Older versions of openlog()
727c478bd9Sstevel@tonic-gate      * require only two arguments.
737c478bd9Sstevel@tonic-gate      */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #ifdef LOG_MAIL
767c478bd9Sstevel@tonic-gate     (void) openlog(argv[0], LOG_PID, FACILITY);
777c478bd9Sstevel@tonic-gate #else
787c478bd9Sstevel@tonic-gate     (void) openlog(argv[0], LOG_PID);
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate     /*
827c478bd9Sstevel@tonic-gate      * Find out the endpoint addresses of this conversation. Host name
837c478bd9Sstevel@tonic-gate      * lookups and double checks will be done on demand.
847c478bd9Sstevel@tonic-gate      */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate     request_init(&request, RQ_DAEMON, argv[0], RQ_FILE, STDIN_FILENO, 0);
877c478bd9Sstevel@tonic-gate     fromhost(&request);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate     /*
907c478bd9Sstevel@tonic-gate      * Optionally look up and double check the remote host name. Sites
917c478bd9Sstevel@tonic-gate      * concerned with security may choose to refuse connections from hosts
927c478bd9Sstevel@tonic-gate      * that pretend to have someone elses host name.
937c478bd9Sstevel@tonic-gate      */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #ifdef PARANOID
967c478bd9Sstevel@tonic-gate     if (STR_EQ(eval_hostname(request.client), paranoid))
977c478bd9Sstevel@tonic-gate 	refuse(&request);
987c478bd9Sstevel@tonic-gate #endif
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate     /*
1017c478bd9Sstevel@tonic-gate      * The BSD rlogin and rsh daemons that came out after 4.3 BSD disallow
1027c478bd9Sstevel@tonic-gate      * socket options at the IP level. They do so for a good reason.
1037c478bd9Sstevel@tonic-gate      * Unfortunately, we cannot use this with SunOS 4.1.x because the
1047c478bd9Sstevel@tonic-gate      * getsockopt() system call can panic the system.
1057c478bd9Sstevel@tonic-gate      */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #ifdef KILL_IP_OPTIONS
1087c478bd9Sstevel@tonic-gate     fix_options(&request);
1097c478bd9Sstevel@tonic-gate #endif
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate     /*
1127c478bd9Sstevel@tonic-gate      * Check whether this host can access the service in argv[0]. The
1137c478bd9Sstevel@tonic-gate      * access-control code invokes optional shell commands as specified in
1147c478bd9Sstevel@tonic-gate      * the access-control tables.
1157c478bd9Sstevel@tonic-gate      */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #ifdef HOSTS_ACCESS
1187c478bd9Sstevel@tonic-gate     if (!hosts_access(&request))
1197c478bd9Sstevel@tonic-gate 	refuse(&request);
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate     /* Report request and invoke the real daemon program. */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate     syslog(allow_severity, "connect from %s", eval_client(&request));
1257c478bd9Sstevel@tonic-gate     closelog();
1267c478bd9Sstevel@tonic-gate     (void) execv(path, argv);
1277c478bd9Sstevel@tonic-gate     syslog(LOG_ERR, "error: cannot execute %s: %m", path);
1287c478bd9Sstevel@tonic-gate     clean_exit(&request);
1297c478bd9Sstevel@tonic-gate     /* NOTREACHED */
1307c478bd9Sstevel@tonic-gate }
131