hosts_access.c revision 51495
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 $ 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#include <sys/param.h> 30#include <netinet/in.h> 31#include <arpa/inet.h> 32#include <stdio.h> 33#include <syslog.h> 34#include <ctype.h> 35#include <errno.h> 36#include <setjmp.h> 37#include <string.h> 38 39extern char *fgets(); 40extern int errno; 41 42#ifndef INADDR_NONE 43#define INADDR_NONE (-1) /* XXX should be 0xffffffff */ 44#endif 45 46/* Local stuff. */ 47 48#include "tcpd.h" 49 50/* Error handling. */ 51 52extern jmp_buf tcpd_buf; 53 54/* Delimiters for lists of daemons or clients. */ 55 56static char sep[] = ", \t\r\n"; 57 58/* Constants to be used in assignments only, not in comparisons... */ 59 60#define YES 1 61#define NO 0 62 63 /* 64 * These variables are globally visible so that they can be redirected in 65 * verification mode. 66 */ 67 68char *hosts_allow_table = HOSTS_ALLOW; 69char *hosts_deny_table = HOSTS_DENY; 70int hosts_access_verbose = 0; 71 72 /* 73 * In a long-running process, we are not at liberty to just go away. 74 */ 75 76int resident = (-1); /* -1, 0: unknown; +1: yes */ 77 78/* Forward declarations. */ 79 80static int table_match(); 81static int list_match(); 82static int server_match(); 83static int client_match(); 84static int host_match(); 85static int string_match(); 86static int masked_match(); 87 88/* Size of logical line buffer. */ 89 90#define BUFLEN 2048 91 92/* hosts_access - host access control facility */ 93 94int hosts_access(request) 95struct request_info *request; 96{ 97 int verdict; 98 99 /* 100 * If the (daemon, client) pair is matched by an entry in the file 101 * /etc/hosts.allow, access is granted. Otherwise, if the (daemon, 102 * client) pair is matched by an entry in the file /etc/hosts.deny, 103 * access is denied. Otherwise, access is granted. A non-existent 104 * access-control file is treated as an empty file. 105 * 106 * After a rule has been matched, the optional language extensions may 107 * decide to grant or refuse service anyway. Or, while a rule is being 108 * processed, a serious error is found, and it seems better to play safe 109 * and deny service. All this is done by jumping back into the 110 * hosts_access() routine, bypassing the regular return from the 111 * table_match() function calls below. 112 */ 113 114 if (resident <= 0) 115 resident++; 116 verdict = setjmp(tcpd_buf); 117 if (verdict != 0) 118 return (verdict == AC_PERMIT); 119 if (table_match(hosts_allow_table, request)) 120 return (YES); 121 if (table_match(hosts_deny_table, request)) 122 return (NO); 123 return (YES); 124} 125 126/* table_match - match table entries with (daemon, client) pair */ 127 128static int table_match(table, request) 129char *table; 130struct request_info *request; 131{ 132 FILE *fp; 133 char sv_list[BUFLEN]; /* becomes list of daemons */ 134 char *cl_list; /* becomes list of clients */ 135 char *sh_cmd; /* becomes optional shell command */ 136 int match = NO; 137 struct tcpd_context saved_context; 138 139 saved_context = tcpd_context; /* stupid compilers */ 140 141 /* 142 * Between the fopen() and fclose() calls, avoid jumps that may cause 143 * file descriptor leaks. 144 */ 145 146 if ((fp = fopen(table, "r")) != 0) { 147 tcpd_context.file = table; 148 tcpd_context.line = 0; 149 while (match == NO && xgets(sv_list, sizeof(sv_list), fp) != 0) { 150 if (sv_list[strlen(sv_list) - 1] != '\n') { 151 tcpd_warn("missing newline or line too long"); 152 continue; 153 } 154 if (sv_list[0] == '#' || sv_list[strspn(sv_list, " \t\r\n")] == 0) 155 continue; 156 if ((cl_list = split_at(sv_list, ':')) == 0) { 157 tcpd_warn("missing \":\" separator"); 158 continue; 159 } 160 sh_cmd = split_at(cl_list, ':'); 161 match = list_match(sv_list, request, server_match) 162 && list_match(cl_list, request, client_match); 163 } 164 (void) fclose(fp); 165 } else if (errno != ENOENT) { 166 tcpd_warn("cannot open %s: %m", table); 167 } 168 if (match) { 169 if (hosts_access_verbose > 1) 170 syslog(LOG_DEBUG, "matched: %s line %d", 171 tcpd_context.file, tcpd_context.line); 172 if (sh_cmd) { 173#ifdef PROCESS_OPTIONS 174 process_options(sh_cmd, request); 175#else 176 char cmd[BUFSIZ]; 177 shell_cmd(percent_x(cmd, sizeof(cmd), sh_cmd, request)); 178#endif 179 } 180 } 181 tcpd_context = saved_context; 182 return (match); 183} 184 185/* list_match - match a request against a list of patterns with exceptions */ 186 187static int list_match(list, request, match_fn) 188char *list; 189struct request_info *request; 190int (*match_fn) (); 191{ 192 char *tok; 193 194 /* 195 * Process tokens one at a time. We have exhausted all possible matches 196 * when we reach an "EXCEPT" token or the end of the list. If we do find 197 * a match, look for an "EXCEPT" list and recurse to determine whether 198 * the match is affected by any exceptions. 199 */ 200 201 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) { 202 if (STR_EQ(tok, "EXCEPT")) /* EXCEPT: give up */ 203 return (NO); 204 if (match_fn(tok, request)) { /* YES: look for exceptions */ 205 while ((tok = strtok((char *) 0, sep)) && STR_NE(tok, "EXCEPT")) 206 /* VOID */ ; 207 return (tok == 0 || list_match((char *) 0, request, match_fn) == 0); 208 } 209 } 210 return (NO); 211} 212 213/* server_match - match server information */ 214 215static int server_match(tok, request) 216char *tok; 217struct request_info *request; 218{ 219 char *host; 220 221 if ((host = split_at(tok + 1, '@')) == 0) { /* plain daemon */ 222 return (string_match(tok, eval_daemon(request))); 223 } else { /* daemon@host */ 224 return (string_match(tok, eval_daemon(request)) 225 && host_match(host, request->server)); 226 } 227} 228 229/* client_match - match client information */ 230 231static int client_match(tok, request) 232char *tok; 233struct request_info *request; 234{ 235 char *host; 236 237 if ((host = split_at(tok + 1, '@')) == 0) { /* plain host */ 238 return (host_match(tok, request->client)); 239 } else { /* user@host */ 240 return (host_match(host, request->client) 241 && string_match(tok, eval_user(request))); 242 } 243} 244 245/* hostfile_match - look up host patterns from file */ 246 247static int hostfile_match(path, host) 248char *path; 249struct hosts_info *host; 250{ 251 char tok[BUFSIZ]; 252 int match = NO; 253 FILE *fp; 254 255 if ((fp = fopen(path, "r")) != 0) { 256 while (fscanf(fp, "%s", tok) == 1 && !(match = host_match(tok, host))) 257 /* void */ ; 258 fclose(fp); 259 } else if (errno != ENOENT) { 260 tcpd_warn("open %s: %m", path); 261 } 262 return (match); 263} 264 265/* host_match - match host name and/or address against pattern */ 266 267static int host_match(tok, host) 268char *tok; 269struct host_info *host; 270{ 271 char *mask; 272 273 /* 274 * This code looks a little hairy because we want to avoid unnecessary 275 * hostname lookups. 276 * 277 * The KNOWN pattern requires that both address AND name be known; some 278 * patterns are specific to host names or to host addresses; all other 279 * patterns are satisfied when either the address OR the name match. 280 */ 281 282 if (tok[0] == '@') { /* netgroup: look it up */ 283#ifdef NETGROUP 284 static char *mydomain = 0; 285 if (mydomain == 0) 286 yp_get_default_domain(&mydomain); 287 return (innetgr(tok + 1, eval_hostname(host), (char *) 0, mydomain)); 288#else 289 tcpd_warn("netgroup support is disabled"); /* not tcpd_jump() */ 290 return (NO); 291#endif 292 } else if (tok[0] == '/') { /* /file hack */ 293 return (hostfile_match(tok, host)); 294 } else if (STR_EQ(tok, "KNOWN")) { /* check address and name */ 295 char *name = eval_hostname(host); 296 return (STR_NE(eval_hostaddr(host), unknown) && HOSTNAME_KNOWN(name)); 297 } else if (STR_EQ(tok, "LOCAL")) { /* local: no dots in name */ 298 char *name = eval_hostname(host); 299 return (strchr(name, '.') == 0 && HOSTNAME_KNOWN(name)); 300 } else if ((mask = split_at(tok, '/')) != 0) { /* net/mask */ 301 return (masked_match(tok, mask, eval_hostaddr(host))); 302 } else { /* anything else */ 303 return (string_match(tok, eval_hostaddr(host)) 304 || (NOT_INADDR(tok) && string_match(tok, eval_hostname(host)))); 305 } 306} 307 308/* string_match - match string against pattern */ 309 310static int string_match(tok, string) 311char *tok; 312char *string; 313{ 314 int n; 315 316 if (tok[0] == '.') { /* suffix */ 317 n = strlen(string) - strlen(tok); 318 return (n > 0 && STR_EQ(tok, string + n)); 319 } else if (STR_EQ(tok, "ALL")) { /* all: match any */ 320 return (YES); 321 } else if (STR_EQ(tok, "KNOWN")) { /* not unknown */ 322 return (STR_NE(string, unknown)); 323 } else if (tok[(n = strlen(tok)) - 1] == '.') { /* prefix */ 324 return (STRN_EQ(tok, string, n)); 325 } else { /* exact match */ 326 return (STR_EQ(tok, string)); 327 } 328} 329 330/* masked_match - match address against netnumber/netmask */ 331 332static int masked_match(net_tok, mask_tok, string) 333char *net_tok; 334char *mask_tok; 335char *string; 336{ 337 unsigned long net; 338 unsigned long mask; 339 unsigned long addr; 340 341 /* 342 * Disallow forms other than dotted quad: the treatment that inet_addr() 343 * gives to forms with less than four components is inconsistent with the 344 * access control language. John P. Rouillard <rouilj@cs.umb.edu>. 345 */ 346 347 if ((addr = dot_quad_addr(string)) == INADDR_NONE) 348 return (NO); 349 if ((net = dot_quad_addr(net_tok)) == INADDR_NONE 350 || (mask = dot_quad_addr(mask_tok)) == INADDR_NONE) { 351 tcpd_warn("bad net/mask expression: %s/%s", net_tok, mask_tok); 352 return (NO); /* not tcpd_jump() */ 353 } 354 return ((addr & mask) == net); 355} 356