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