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