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