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