1 /* 2 * This module implements a simple access control language that is based on 3 * host (or domain) names, NIS (host) netgroup names, IP addresses (or 4 * network numbers) and daemon process names. When a match is found the 5 * search is terminated, and depending on whether PROCESS_OPTIONS is defined, 6 * a list of options is executed or an optional shell command is executed. 7 * 8 * Host and user names are looked up on demand, provided that suitable endpoint 9 * information is available as sockaddr_in structures or TLI netbufs. As a 10 * side effect, the pattern matching process may change the contents of 11 * request structure fields. 12 * 13 * Diagnostics are reported through syslog(3). 14 * 15 * Compile with -DNETGROUP if your library provides support for netgroups. 16 * 17 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands. 18 * 19 * $FreeBSD: src/contrib/tcp_wrappers/hosts_access.c,v 1.3.2.1 2000/07/18 08:34:54 ume Exp $ 20 * $DragonFly: src/contrib/tcp_wrappers/hosts_access.c,v 1.2 2003/06/17 04:24:06 dillon Exp $ 21 */ 22 23 #ifndef lint 24 static char sccsid[] = "@(#) hosts_access.c 1.21 97/02/12 02:13:22"; 25 #endif 26 27 /* System libraries. */ 28 29 #include <sys/types.h> 30 #ifdef INT32_T 31 typedef uint32_t u_int32_t; 32 #endif 33 #include <sys/param.h> 34 #ifdef INET6 35 #include <sys/socket.h> 36 #endif 37 #include <netinet/in.h> 38 #include <arpa/inet.h> 39 #include <stdio.h> 40 #include <syslog.h> 41 #include <ctype.h> 42 #include <errno.h> 43 #include <setjmp.h> 44 #include <string.h> 45 #ifdef INET6 46 #include <netdb.h> 47 #endif 48 49 extern char *fgets(); 50 extern int errno; 51 52 #ifndef INADDR_NONE 53 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */ 54 #endif 55 56 /* Local stuff. */ 57 58 #include "tcpd.h" 59 60 /* Error handling. */ 61 62 extern jmp_buf tcpd_buf; 63 64 /* Delimiters for lists of daemons or clients. */ 65 66 static char sep[] = ", \t\r\n"; 67 68 /* Constants to be used in assignments only, not in comparisons... */ 69 70 #define YES 1 71 #define NO 0 72 73 /* 74 * These variables are globally visible so that they can be redirected in 75 * verification mode. 76 */ 77 78 char *hosts_allow_table = HOSTS_ALLOW; 79 char *hosts_deny_table = HOSTS_DENY; 80 int hosts_access_verbose = 0; 81 82 /* 83 * In a long-running process, we are not at liberty to just go away. 84 */ 85 86 int resident = (-1); /* -1, 0: unknown; +1: yes */ 87 88 /* Forward declarations. */ 89 90 static int table_match(); 91 static int list_match(); 92 static int server_match(); 93 static int client_match(); 94 static int host_match(); 95 static int string_match(); 96 static int masked_match(); 97 #ifdef INET6 98 static int masked_match4(); 99 static int masked_match6(); 100 #endif 101 102 /* Size of logical line buffer. */ 103 104 #define BUFLEN 2048 105 106 /* hosts_access - host access control facility */ 107 108 int hosts_access(request) 109 struct request_info *request; 110 { 111 int verdict; 112 113 /* 114 * If the (daemon, client) pair is matched by an entry in the file 115 * /etc/hosts.allow, access is granted. Otherwise, if the (daemon, 116 * client) pair is matched by an entry in the file /etc/hosts.deny, 117 * access is denied. Otherwise, access is granted. A non-existent 118 * access-control file is treated as an empty file. 119 * 120 * After a rule has been matched, the optional language extensions may 121 * decide to grant or refuse service anyway. Or, while a rule is being 122 * processed, a serious error is found, and it seems better to play safe 123 * and deny service. All this is done by jumping back into the 124 * hosts_access() routine, bypassing the regular return from the 125 * table_match() function calls below. 126 */ 127 128 if (resident <= 0) 129 resident++; 130 verdict = setjmp(tcpd_buf); 131 if (verdict != 0) 132 return (verdict == AC_PERMIT); 133 if (table_match(hosts_allow_table, request)) 134 return (YES); 135 if (table_match(hosts_deny_table, request)) 136 return (NO); 137 return (YES); 138 } 139 140 /* table_match - match table entries with (daemon, client) pair */ 141 142 static int table_match(table, request) 143 char *table; 144 struct request_info *request; 145 { 146 FILE *fp; 147 char sv_list[BUFLEN]; /* becomes list of daemons */ 148 char *cl_list; /* becomes list of clients */ 149 char *sh_cmd; /* becomes optional shell command */ 150 int match = NO; 151 struct tcpd_context saved_context; 152 153 saved_context = tcpd_context; /* stupid compilers */ 154 155 /* 156 * Between the fopen() and fclose() calls, avoid jumps that may cause 157 * file descriptor leaks. 158 */ 159 160 if ((fp = fopen(table, "r")) != 0) { 161 tcpd_context.file = table; 162 tcpd_context.line = 0; 163 while (match == NO && xgets(sv_list, sizeof(sv_list), fp) != 0) { 164 if (sv_list[strlen(sv_list) - 1] != '\n') { 165 tcpd_warn("missing newline or line too long"); 166 continue; 167 } 168 if (sv_list[0] == '#' || sv_list[strspn(sv_list, " \t\r\n")] == 0) 169 continue; 170 if ((cl_list = split_at(sv_list, ':')) == 0) { 171 tcpd_warn("missing \":\" separator"); 172 continue; 173 } 174 sh_cmd = split_at(cl_list, ':'); 175 match = list_match(sv_list, request, server_match) 176 && list_match(cl_list, request, client_match); 177 } 178 (void) fclose(fp); 179 } else if (errno != ENOENT) { 180 tcpd_warn("cannot open %s: %m", table); 181 } 182 if (match) { 183 if (hosts_access_verbose > 1) 184 syslog(LOG_DEBUG, "matched: %s line %d", 185 tcpd_context.file, tcpd_context.line); 186 if (sh_cmd) { 187 #ifdef PROCESS_OPTIONS 188 process_options(sh_cmd, request); 189 #else 190 char cmd[BUFSIZ]; 191 shell_cmd(percent_x(cmd, sizeof(cmd), sh_cmd, request)); 192 #endif 193 } 194 } 195 tcpd_context = saved_context; 196 return (match); 197 } 198 199 /* list_match - match a request against a list of patterns with exceptions */ 200 201 static int list_match(list, request, match_fn) 202 char *list; 203 struct request_info *request; 204 int (*match_fn) (); 205 { 206 char *tok; 207 208 /* 209 * Process tokens one at a time. We have exhausted all possible matches 210 * when we reach an "EXCEPT" token or the end of the list. If we do find 211 * a match, look for an "EXCEPT" list and recurse to determine whether 212 * the match is affected by any exceptions. 213 */ 214 215 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) { 216 if (STR_EQ(tok, "EXCEPT")) /* EXCEPT: give up */ 217 return (NO); 218 if (match_fn(tok, request)) { /* YES: look for exceptions */ 219 while ((tok = strtok((char *) 0, sep)) && STR_NE(tok, "EXCEPT")) 220 /* VOID */ ; 221 return (tok == 0 || list_match((char *) 0, request, match_fn) == 0); 222 } 223 } 224 return (NO); 225 } 226 227 /* server_match - match server information */ 228 229 static int server_match(tok, request) 230 char *tok; 231 struct request_info *request; 232 { 233 char *host; 234 235 if ((host = split_at(tok + 1, '@')) == 0) { /* plain daemon */ 236 return (string_match(tok, eval_daemon(request))); 237 } else { /* daemon@host */ 238 return (string_match(tok, eval_daemon(request)) 239 && host_match(host, request->server)); 240 } 241 } 242 243 /* client_match - match client information */ 244 245 static int client_match(tok, request) 246 char *tok; 247 struct request_info *request; 248 { 249 char *host; 250 251 if ((host = split_at(tok + 1, '@')) == 0) { /* plain host */ 252 return (host_match(tok, request->client)); 253 } else { /* user@host */ 254 return (host_match(host, request->client) 255 && string_match(tok, eval_user(request))); 256 } 257 } 258 259 /* hostfile_match - look up host patterns from file */ 260 261 static int hostfile_match(path, host) 262 char *path; 263 struct hosts_info *host; 264 { 265 char tok[BUFSIZ]; 266 int match = NO; 267 FILE *fp; 268 269 if ((fp = fopen(path, "r")) != 0) { 270 while (fscanf(fp, "%s", tok) == 1 && !(match = host_match(tok, host))) 271 /* void */ ; 272 fclose(fp); 273 } else if (errno != ENOENT) { 274 tcpd_warn("open %s: %m", path); 275 } 276 return (match); 277 } 278 279 /* host_match - match host name and/or address against pattern */ 280 281 static int host_match(tok, host) 282 char *tok; 283 struct host_info *host; 284 { 285 char *mask; 286 287 /* 288 * This code looks a little hairy because we want to avoid unnecessary 289 * hostname lookups. 290 * 291 * The KNOWN pattern requires that both address AND name be known; some 292 * patterns are specific to host names or to host addresses; all other 293 * patterns are satisfied when either the address OR the name match. 294 */ 295 296 if (tok[0] == '@') { /* netgroup: look it up */ 297 #ifdef NETGROUP 298 static char *mydomain = 0; 299 if (mydomain == 0) 300 yp_get_default_domain(&mydomain); 301 return (innetgr(tok + 1, eval_hostname(host), (char *) 0, mydomain)); 302 #else 303 tcpd_warn("netgroup support is disabled"); /* not tcpd_jump() */ 304 return (NO); 305 #endif 306 } else if (tok[0] == '/') { /* /file hack */ 307 return (hostfile_match(tok, host)); 308 } else if (STR_EQ(tok, "KNOWN")) { /* check address and name */ 309 char *name = eval_hostname(host); 310 return (STR_NE(eval_hostaddr(host), unknown) && HOSTNAME_KNOWN(name)); 311 } else if (STR_EQ(tok, "LOCAL")) { /* local: no dots in name */ 312 char *name = eval_hostname(host); 313 return (strchr(name, '.') == 0 && HOSTNAME_KNOWN(name)); 314 } else if ((mask = split_at(tok, '/')) != 0) { /* net/mask */ 315 return (masked_match(tok, mask, eval_hostaddr(host))); 316 } else { /* anything else */ 317 return (string_match(tok, eval_hostaddr(host)) 318 || (NOT_INADDR(tok) && string_match(tok, eval_hostname(host)))); 319 } 320 } 321 322 /* string_match - match string against pattern */ 323 324 static int string_match(tok, string) 325 char *tok; 326 char *string; 327 { 328 int n; 329 330 #ifdef INET6 331 /* convert IPv4 mapped IPv6 address to IPv4 address */ 332 if (STRN_EQ(string, "::ffff:", 7) 333 && dot_quad_addr(string + 7) != INADDR_NONE) { 334 string += 7; 335 } 336 #endif 337 if (tok[0] == '.') { /* suffix */ 338 n = strlen(string) - strlen(tok); 339 return (n > 0 && STR_EQ(tok, string + n)); 340 } else if (STR_EQ(tok, "ALL")) { /* all: match any */ 341 return (YES); 342 } else if (STR_EQ(tok, "KNOWN")) { /* not unknown */ 343 return (STR_NE(string, unknown)); 344 } else if (tok[(n = strlen(tok)) - 1] == '.') { /* prefix */ 345 return (STRN_EQ(tok, string, n)); 346 } else { /* exact match */ 347 #ifdef INET6 348 struct addrinfo hints, *res; 349 struct sockaddr_in6 pat, addr; 350 int len, ret; 351 char ch; 352 353 len = strlen(tok); 354 if (*tok == '[' && tok[len - 1] == ']') { 355 ch = tok[len - 1]; 356 tok[len - 1] = '\0'; 357 memset(&hints, 0, sizeof(hints)); 358 hints.ai_family = AF_INET6; 359 hints.ai_socktype = SOCK_STREAM; 360 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 361 if ((ret = getaddrinfo(tok + 1, NULL, &hints, &res)) == 0) { 362 memcpy(&pat, res->ai_addr, sizeof(pat)); 363 freeaddrinfo(res); 364 } 365 tok[len - 1] = ch; 366 if (ret != 0 || getaddrinfo(string, NULL, &hints, &res) != 0) 367 return NO; 368 memcpy(&addr, res->ai_addr, sizeof(addr)); 369 freeaddrinfo(res); 370 #ifdef NI_WITHSCOPEID 371 if (pat.sin6_scope_id != 0 && 372 addr.sin6_scope_id != pat.sin6_scope_id) 373 return NO; 374 #endif 375 return (!memcmp(&pat.sin6_addr, &addr.sin6_addr, 376 sizeof(struct in6_addr))); 377 return (ret); 378 } 379 #endif 380 return (STR_EQ(tok, string)); 381 } 382 } 383 384 /* masked_match - match address against netnumber/netmask */ 385 386 #ifdef INET6 387 static int masked_match(net_tok, mask_tok, string) 388 char *net_tok; 389 char *mask_tok; 390 char *string; 391 { 392 return (masked_match4(net_tok, mask_tok, string) || 393 masked_match6(net_tok, mask_tok, string)); 394 } 395 396 static int masked_match4(net_tok, mask_tok, string) 397 #else 398 static int masked_match(net_tok, mask_tok, string) 399 #endif 400 char *net_tok; 401 char *mask_tok; 402 char *string; 403 { 404 #ifdef INET6 405 u_int32_t net; 406 u_int32_t mask; 407 u_int32_t addr; 408 #else 409 unsigned long net; 410 unsigned long mask; 411 unsigned long addr; 412 #endif 413 414 /* 415 * Disallow forms other than dotted quad: the treatment that inet_addr() 416 * gives to forms with less than four components is inconsistent with the 417 * access control language. John P. Rouillard <rouilj@cs.umb.edu>. 418 */ 419 420 if ((addr = dot_quad_addr(string)) == INADDR_NONE) 421 return (NO); 422 if ((net = dot_quad_addr(net_tok)) == INADDR_NONE 423 || (mask = dot_quad_addr(mask_tok)) == INADDR_NONE) { 424 #ifndef INET6 425 tcpd_warn("bad net/mask expression: %s/%s", net_tok, mask_tok); 426 #endif 427 return (NO); /* not tcpd_jump() */ 428 } 429 return ((addr & mask) == net); 430 } 431 432 #ifdef INET6 433 static int masked_match6(net_tok, mask_tok, string) 434 char *net_tok; 435 char *mask_tok; 436 char *string; 437 { 438 struct addrinfo hints, *res; 439 struct sockaddr_in6 net, addr; 440 u_int32_t mask; 441 int len, mask_len, i = 0; 442 char ch; 443 444 memset(&hints, 0, sizeof(hints)); 445 hints.ai_family = AF_INET6; 446 hints.ai_socktype = SOCK_STREAM; 447 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 448 if (getaddrinfo(string, NULL, &hints, &res) != 0) 449 return NO; 450 memcpy(&addr, res->ai_addr, sizeof(addr)); 451 freeaddrinfo(res); 452 453 if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) { 454 if ((*(u_int32_t *)&net.sin6_addr.s6_addr[12] = dot_quad_addr(net_tok)) == INADDR_NONE 455 || (mask = dot_quad_addr(mask_tok)) == INADDR_NONE) 456 return (NO); 457 return ((*(u_int32_t *)&addr.sin6_addr.s6_addr[12] & mask) == *(u_int32_t *)&net.sin6_addr.s6_addr[12]); 458 } 459 460 /* match IPv6 address against netnumber/prefixlen */ 461 len = strlen(net_tok); 462 if (*net_tok != '[' || net_tok[len - 1] != ']') 463 return NO; 464 ch = net_tok[len - 1]; 465 net_tok[len - 1] = '\0'; 466 if (getaddrinfo(net_tok + 1, NULL, &hints, &res) != 0) { 467 net_tok[len - 1] = ch; 468 return NO; 469 } 470 memcpy(&net, res->ai_addr, sizeof(net)); 471 freeaddrinfo(res); 472 net_tok[len - 1] = ch; 473 if ((mask_len = atoi(mask_tok)) < 0 || mask_len > 128) 474 return NO; 475 476 #ifdef NI_WITHSCOPEID 477 if (net.sin6_scope_id != 0 && addr.sin6_scope_id != net.sin6_scope_id) 478 return NO; 479 #endif 480 while (mask_len > 0) { 481 if (mask_len < 32) { 482 mask = htonl(~(0xffffffff >> mask_len)); 483 if ((*(u_int32_t *)&addr.sin6_addr.s6_addr[i] & mask) != (*(u_int32_t *)&net.sin6_addr.s6_addr[i] & mask)) 484 return NO; 485 break; 486 } 487 if (*(u_int32_t *)&addr.sin6_addr.s6_addr[i] != *(u_int32_t *)&net.sin6_addr.s6_addr[i]) 488 return NO; 489 i += 4; 490 mask_len -= 32; 491 } 492 return YES; 493 } 494 #endif /* INET6 */ 495