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