xref: /illumos-gate/usr/src/cmd/tcpd/scaffold.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate  /*
27c478bd9Sstevel@tonic-gate   * Routines for testing only. Not really industrial strength.
3*55fea89dSDan Cross   *
47c478bd9Sstevel@tonic-gate   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
57c478bd9Sstevel@tonic-gate   */
67c478bd9Sstevel@tonic-gate 
77c478bd9Sstevel@tonic-gate #ifndef lint
87c478bd9Sstevel@tonic-gate static char sccs_id[] = "@(#) scaffold.c 1.6 97/03/21 19:27:24";
97c478bd9Sstevel@tonic-gate #endif
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate /* System libraries. */
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include <sys/types.h>
147c478bd9Sstevel@tonic-gate #include <sys/stat.h>
157c478bd9Sstevel@tonic-gate #include <sys/socket.h>
167c478bd9Sstevel@tonic-gate #include <netinet/in.h>
177c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
187c478bd9Sstevel@tonic-gate #include <netdb.h>
197c478bd9Sstevel@tonic-gate #include <stdio.h>
207c478bd9Sstevel@tonic-gate #include <syslog.h>
217c478bd9Sstevel@tonic-gate #include <setjmp.h>
227c478bd9Sstevel@tonic-gate #include <string.h>
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #ifndef INADDR_NONE
257c478bd9Sstevel@tonic-gate #define	INADDR_NONE	(-1)		/* XXX should be 0xffffffff */
267c478bd9Sstevel@tonic-gate #endif
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate extern char *malloc();
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /* Application-specific. */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "tcpd.h"
337c478bd9Sstevel@tonic-gate #include "scaffold.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate  /*
367c478bd9Sstevel@tonic-gate   * These are referenced by the options module and by rfc931.c.
377c478bd9Sstevel@tonic-gate   */
387c478bd9Sstevel@tonic-gate int     allow_severity = SEVERITY;
397c478bd9Sstevel@tonic-gate int     deny_severity = LOG_WARNING;
407c478bd9Sstevel@tonic-gate int     rfc931_timeout = RFC931_TIMEOUT;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /* dup_hostent - create hostent in one memory block */
437c478bd9Sstevel@tonic-gate 
dup_hostent(hp)447c478bd9Sstevel@tonic-gate static struct hostent *dup_hostent(hp)
457c478bd9Sstevel@tonic-gate struct hostent *hp;
467c478bd9Sstevel@tonic-gate {
477c478bd9Sstevel@tonic-gate     struct hostent_block {
487c478bd9Sstevel@tonic-gate 	struct hostent host;
497c478bd9Sstevel@tonic-gate 	char   *addr_list[1];
507c478bd9Sstevel@tonic-gate     };
517c478bd9Sstevel@tonic-gate     struct hostent_block *hb;
527c478bd9Sstevel@tonic-gate     int     count;
537c478bd9Sstevel@tonic-gate     char   *data;
547c478bd9Sstevel@tonic-gate     char   *addr;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate     for (count = 0; hp->h_addr_list[count] != 0; count++)
577c478bd9Sstevel@tonic-gate 	 /* void */ ;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate     if ((hb = (struct hostent_block *) malloc(sizeof(struct hostent_block)
607c478bd9Sstevel@tonic-gate 			 + (hp->h_length + sizeof(char *)) * count)) == 0) {
617c478bd9Sstevel@tonic-gate 	fprintf(stderr, "Sorry, out of memory\n");
627c478bd9Sstevel@tonic-gate 	exit(1);
637c478bd9Sstevel@tonic-gate     }
647c478bd9Sstevel@tonic-gate     memset((char *) &hb->host, 0, sizeof(hb->host));
657c478bd9Sstevel@tonic-gate     hb->host.h_addrtype = hp->h_addrtype;;
667c478bd9Sstevel@tonic-gate     hb->host.h_length = hp->h_length;
677c478bd9Sstevel@tonic-gate     hb->host.h_addr_list = hb->addr_list;
687c478bd9Sstevel@tonic-gate     hb->host.h_addr_list[count] = 0;
697c478bd9Sstevel@tonic-gate     data = (char *) (hb->host.h_addr_list + count + 1);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate     for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) {
727c478bd9Sstevel@tonic-gate 	hb->host.h_addr_list[count] = data + hp->h_length * count;
737c478bd9Sstevel@tonic-gate 	memcpy(hb->host.h_addr_list[count], addr, hp->h_length);
747c478bd9Sstevel@tonic-gate     }
757c478bd9Sstevel@tonic-gate     return (&hb->host);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* find_inet_addr - find all addresses for this host, result to free() */
797c478bd9Sstevel@tonic-gate 
find_inet_addr(host)807c478bd9Sstevel@tonic-gate struct hostent *find_inet_addr(host)
817c478bd9Sstevel@tonic-gate char   *host;
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate     union gen_addr addr;
847c478bd9Sstevel@tonic-gate     struct hostent *hp;
857c478bd9Sstevel@tonic-gate     static struct hostent h;
867c478bd9Sstevel@tonic-gate     static char *addr_list[2];
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate     /*
897c478bd9Sstevel@tonic-gate      * Host address: translate it to internal form.
907c478bd9Sstevel@tonic-gate      */
917c478bd9Sstevel@tonic-gate     if (numeric_addr(host, &addr, &h.h_addrtype, &h.h_length) != -1) {
927c478bd9Sstevel@tonic-gate 	h.h_addr_list = addr_list;
937c478bd9Sstevel@tonic-gate 	h.h_addr_list[0] = (char *) &addr;
947c478bd9Sstevel@tonic-gate 	return (dup_hostent(&h));
957c478bd9Sstevel@tonic-gate     }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate     /*
987c478bd9Sstevel@tonic-gate      * Map host name to a series of addresses. Watch out for non-internet
997c478bd9Sstevel@tonic-gate      * forms or aliases. The NOT_INADDR() is here in case gethostbyname() has
1007c478bd9Sstevel@tonic-gate      * been "enhanced" to accept numeric addresses. Make a copy of the
1017c478bd9Sstevel@tonic-gate      * address list so that later gethostbyXXX() calls will not clobber it.
1027c478bd9Sstevel@tonic-gate      */
1037c478bd9Sstevel@tonic-gate     if (NOT_INADDR(host) == 0) {
1047c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: not an internet address", host);
1057c478bd9Sstevel@tonic-gate 	return (0);
1067c478bd9Sstevel@tonic-gate     }
1077c478bd9Sstevel@tonic-gate     if ((hp = tcpd_gethostbyname(host, 0)) == 0) {
1087c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: host not found", host);
1097c478bd9Sstevel@tonic-gate 	return (0);
1107c478bd9Sstevel@tonic-gate     }
1117c478bd9Sstevel@tonic-gate     if (!VALID_ADDRTYPE(hp->h_addrtype)) {
1127c478bd9Sstevel@tonic-gate 	tcpd_warn("%d: not an internet host", hp->h_addrtype);
1137c478bd9Sstevel@tonic-gate 	return (0);
1147c478bd9Sstevel@tonic-gate     }
1157c478bd9Sstevel@tonic-gate     if (STR_NE(host, hp->h_name)) {
1167c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: hostname alias", host);
1177c478bd9Sstevel@tonic-gate 	tcpd_warn("(official name: %.*s)", STRING_LENGTH, hp->h_name);
1187c478bd9Sstevel@tonic-gate     }
1197c478bd9Sstevel@tonic-gate     return (dup_hostent(hp));
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /* check_dns - give each address thorough workout, return address count */
1237c478bd9Sstevel@tonic-gate 
check_dns(host)1247c478bd9Sstevel@tonic-gate int     check_dns(host)
1257c478bd9Sstevel@tonic-gate char   *host;
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate     struct request_info request;
1287c478bd9Sstevel@tonic-gate     struct sockaddr_gen sin;
1297c478bd9Sstevel@tonic-gate     struct hostent *hp;
1307c478bd9Sstevel@tonic-gate     int     count;
1317c478bd9Sstevel@tonic-gate     char   *addr;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate     if ((hp = find_inet_addr(host)) == 0)
1347c478bd9Sstevel@tonic-gate 	return (0);
1357c478bd9Sstevel@tonic-gate     request_init(&request, RQ_CLIENT_SIN, &sin, 0);
1367c478bd9Sstevel@tonic-gate     sock_methods(&request);
1377c478bd9Sstevel@tonic-gate     memset((char *) &sin, 0, sizeof(sin));
1387c478bd9Sstevel@tonic-gate     sin.sg_family = hp->h_addrtype;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate     for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) {
1417c478bd9Sstevel@tonic-gate 	memcpy((char *) SGADDRP(&sin), addr, SGADDRSZ(&sin));
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * Force host name and address conversions. Use the request structure
1457c478bd9Sstevel@tonic-gate 	 * as a cache. Detect hostname lookup problems. Any name/name or
1467c478bd9Sstevel@tonic-gate 	 * name/address conflicts will be reported while eval_hostname() does
1477c478bd9Sstevel@tonic-gate 	 * its job.
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	request_set(&request, RQ_CLIENT_ADDR, "", RQ_CLIENT_NAME, "", 0);
1507c478bd9Sstevel@tonic-gate 	if (STR_EQ(eval_hostname(request.client), unknown))
1517c478bd9Sstevel@tonic-gate 	    tcpd_warn("host address %s->name lookup failed",
1527c478bd9Sstevel@tonic-gate 		      eval_hostaddr(request.client));
1537c478bd9Sstevel@tonic-gate     }
1547c478bd9Sstevel@tonic-gate     free((char *) hp);
1557c478bd9Sstevel@tonic-gate     return (count);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /* dummy function to intercept the real shell_cmd() */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /* ARGSUSED */
1617c478bd9Sstevel@tonic-gate 
shell_cmd(command)1627c478bd9Sstevel@tonic-gate void    shell_cmd(command)
1637c478bd9Sstevel@tonic-gate char   *command;
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate     if (hosts_access_verbose)
1667c478bd9Sstevel@tonic-gate 	printf("command: %s", command);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /* dummy function  to intercept the real clean_exit() */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /* ARGSUSED */
1727c478bd9Sstevel@tonic-gate 
clean_exit(request)1737c478bd9Sstevel@tonic-gate void    clean_exit(request)
1747c478bd9Sstevel@tonic-gate struct request_info *request;
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate     exit(0);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /* dummy function  to intercept the real rfc931() */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /* ARGSUSED */
1827c478bd9Sstevel@tonic-gate 
rfc931(request)1837c478bd9Sstevel@tonic-gate void    rfc931(request)
1847c478bd9Sstevel@tonic-gate struct request_info *request;
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate     strcpy(request->user, unknown);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /* check_path - examine accessibility */
1907c478bd9Sstevel@tonic-gate 
check_path(path,st)1917c478bd9Sstevel@tonic-gate int     check_path(path, st)
1927c478bd9Sstevel@tonic-gate char   *path;
1937c478bd9Sstevel@tonic-gate struct stat *st;
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate     struct stat stbuf;
1967c478bd9Sstevel@tonic-gate     char    buf[BUFSIZ];
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate     if (stat(path, st) < 0)
1997c478bd9Sstevel@tonic-gate 	return (-1);
2007c478bd9Sstevel@tonic-gate #ifdef notdef
2017c478bd9Sstevel@tonic-gate     if (st->st_uid != 0)
2027c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: not owned by root", path);
2037c478bd9Sstevel@tonic-gate     if (st->st_mode & 020)
2047c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: group writable", path);
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate     if (st->st_mode & 002)
2077c478bd9Sstevel@tonic-gate 	tcpd_warn("%s: world writable", path);
2087c478bd9Sstevel@tonic-gate     if (path[0] == '/' && path[1] != 0) {
2097c478bd9Sstevel@tonic-gate 	strrchr(strcpy(buf, path), '/')[0] = 0;
2107c478bd9Sstevel@tonic-gate 	(void) check_path(buf[0] ? buf : "/", &stbuf);
2117c478bd9Sstevel@tonic-gate     }
2127c478bd9Sstevel@tonic-gate     return (0);
2137c478bd9Sstevel@tonic-gate }
214