1 /* 2 * Routines for testing only. Not really industrial strength. 3 * 4 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 5 * 6 * $FreeBSD: stable/10/contrib/tcp_wrappers/scaffold.c 323744 2017-09-19 08:34:13Z avg $ 7 */ 8 9#ifndef lint 10static char sccs_id[] = "@(#) scaffold.c 1.6 97/03/21 19:27:24"; 11#endif 12 13/* System libraries. */ 14 15#include <sys/types.h> 16#include <sys/stat.h> 17#include <sys/socket.h> 18#include <netinet/in.h> 19#include <arpa/inet.h> 20#include <netdb.h> 21#include <stdio.h> 22#include <syslog.h> 23#include <setjmp.h> 24#include <string.h> 25 26#ifndef INADDR_NONE 27#define INADDR_NONE (-1) /* XXX should be 0xffffffff */ 28#endif 29 30/* Application-specific. */ 31 32#include "tcpd.h" 33#include "scaffold.h" 34 35 /* 36 * These are referenced by the options module and by rfc931.c. 37 */ 38int allow_severity = SEVERITY; 39int deny_severity = LOG_WARNING; 40int rfc931_timeout = RFC931_TIMEOUT; 41 42#ifndef INET6 43/* dup_hostent - create hostent in one memory block */ 44 45static struct hostent *dup_hostent(hp) 46struct hostent *hp; 47{ 48 struct hostent_block { 49 struct hostent host; 50 char *addr_list[1]; 51 }; 52 struct hostent_block *hb; 53 int count; 54 char *data; 55 char *addr; 56 57 for (count = 0; hp->h_addr_list[count] != 0; count++) 58 /* void */ ; 59 60 if ((hb = (struct hostent_block *) malloc(sizeof(struct hostent_block) 61 + (hp->h_length + sizeof(char *)) * count)) == 0) { 62 fprintf(stderr, "Sorry, out of memory\n"); 63 exit(1); 64 } 65 memset((char *) &hb->host, 0, sizeof(hb->host)); 66 hb->host.h_length = hp->h_length; 67 hb->host.h_addr_list = hb->addr_list; 68 hb->host.h_addr_list[count] = 0; 69 data = (char *) (hb->host.h_addr_list + count + 1); 70 71 for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) { 72 hb->host.h_addr_list[count] = data + hp->h_length * count; 73 memcpy(hb->host.h_addr_list[count], addr, hp->h_length); 74 } 75 return (&hb->host); 76} 77#endif 78 79/* find_inet_addr - find all addresses for this host, result to free() */ 80 81#ifdef INET6 82struct addrinfo *find_inet_addr(host) 83char *host; 84{ 85 struct addrinfo hints, *res; 86 87 memset(&hints, 0, sizeof(hints)); 88 hints.ai_family = PF_UNSPEC; 89 hints.ai_socktype = SOCK_STREAM; 90 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 91 if (getaddrinfo(host, NULL, &hints, &res) == 0) 92 return (res); 93 94 memset(&hints, 0, sizeof(hints)); 95 hints.ai_family = PF_UNSPEC; 96 hints.ai_socktype = SOCK_STREAM; 97 hints.ai_flags = AI_PASSIVE | AI_CANONNAME; 98 if (getaddrinfo(host, NULL, &hints, &res) != 0) { 99 tcpd_warn("%s: host not found", host); 100 return (0); 101 } 102 if (res->ai_family != AF_INET6 && res->ai_family != AF_INET) { 103 tcpd_warn("%d: not an internet host", res->ai_family); 104 freeaddrinfo(res); 105 return (0); 106 } 107 if (!res->ai_canonname) { 108 tcpd_warn("%s: hostname alias", host); 109 tcpd_warn("(cannot obtain official name)", res->ai_canonname); 110 } else if (STR_NE(host, res->ai_canonname)) { 111 tcpd_warn("%s: hostname alias", host); 112 tcpd_warn("(official name: %.*s)", STRING_LENGTH, res->ai_canonname); 113 } 114 return (res); 115} 116#else 117struct hostent *find_inet_addr(host) 118char *host; 119{ 120 struct in_addr addr; 121 struct hostent *hp; 122 static struct hostent h; 123 static char *addr_list[2]; 124 125 /* 126 * Host address: translate it to internal form. 127 */ 128 if ((addr.s_addr = dot_quad_addr(host)) != INADDR_NONE) { 129 h.h_addr_list = addr_list; 130 h.h_addr_list[0] = (char *) &addr; 131 h.h_length = sizeof(addr); 132 return (dup_hostent(&h)); 133 } 134 135 /* 136 * Map host name to a series of addresses. Watch out for non-internet 137 * forms or aliases. The NOT_INADDR() is here in case gethostbyname() has 138 * been "enhanced" to accept numeric addresses. Make a copy of the 139 * address list so that later gethostbyXXX() calls will not clobber it. 140 */ 141 if (NOT_INADDR(host) == 0) { 142 tcpd_warn("%s: not an internet address", host); 143 return (0); 144 } 145 if ((hp = gethostbyname(host)) == 0) { 146 tcpd_warn("%s: host not found", host); 147 return (0); 148 } 149 if (hp->h_addrtype != AF_INET) { 150 tcpd_warn("%d: not an internet host", hp->h_addrtype); 151 return (0); 152 } 153 if (STR_NE(host, hp->h_name)) { 154 tcpd_warn("%s: hostname alias", host); 155 tcpd_warn("(official name: %.*s)", STRING_LENGTH, hp->h_name); 156 } 157 return (dup_hostent(hp)); 158} 159#endif 160 161/* check_dns - give each address thorough workout, return address count */ 162 163int check_dns(host) 164char *host; 165{ 166 struct request_info request; 167#ifdef INET6 168 struct sockaddr_storage sin; 169 struct addrinfo *hp, *res; 170#else 171 struct sockaddr_in sin; 172 struct hostent *hp; 173#endif 174 int count; 175 char *addr; 176 177 if ((hp = find_inet_addr(host)) == 0) 178 return (0); 179 request_init(&request, RQ_CLIENT_SIN, &sin, 0); 180 sock_methods(&request); 181#ifndef INET6 182 memset((char *) &sin, 0, sizeof(sin)); 183 sin.sin_family = AF_INET; 184#endif 185 186#ifdef INET6 187 for (res = hp, count = 0; res; res = res->ai_next, count++) { 188 memcpy(&sin, res->ai_addr, res->ai_addrlen); 189#else 190 for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) { 191 memcpy((char *) &sin.sin_addr, addr, sizeof(sin.sin_addr)); 192#endif 193 194 /* 195 * Force host name and address conversions. Use the request structure 196 * as a cache. Detect hostname lookup problems. Any name/name or 197 * name/address conflicts will be reported while eval_hostname() does 198 * its job. 199 */ 200 request_set(&request, RQ_CLIENT_ADDR, "", RQ_CLIENT_NAME, "", 0); 201 if (STR_EQ(eval_hostname(request.client), unknown)) 202 tcpd_warn("host address %s->name lookup failed", 203 eval_hostaddr(request.client)); 204 } 205#ifdef INET6 206 freeaddrinfo(hp); 207#else 208 free((char *) hp); 209#endif 210 return (count); 211} 212 213/* dummy function to intercept the real shell_cmd() */ 214 215/* ARGSUSED */ 216 217void shell_cmd(command) 218char *command; 219{ 220 if (hosts_access_verbose) 221 printf("command: %s", command); 222} 223 224/* dummy function to intercept the real clean_exit() */ 225 226/* ARGSUSED */ 227 228void clean_exit(request) 229struct request_info *request; 230{ 231 exit(0); 232} 233 234/* check_path - examine accessibility */ 235 236int check_path(path, st) 237char *path; 238struct stat *st; 239{ 240 struct stat stbuf; 241 char buf[BUFSIZ]; 242 243 if (stat(path, st) < 0) 244 return (-1); 245#ifdef notdef 246 if (st->st_uid != 0) 247 tcpd_warn("%s: not owned by root", path); 248 if (st->st_mode & 020) 249 tcpd_warn("%s: group writable", path); 250#endif 251 if (st->st_mode & 002) 252 tcpd_warn("%s: world writable", path); 253 if (path[0] == '/' && path[1] != 0) { 254 strrchr(strcpy(buf, path), '/')[0] = 0; 255 (void) check_path(buf[0] ? buf : "/", &stbuf); 256 } 257 return (0); 258} 259