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