hosts_access.c revision 1.17.6.1 1 /* $NetBSD: hosts_access.c,v 1.17.6.1 2006/01/21 06:01:48 snj 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.17.6.1 2006/01/21 06:01:48 snj 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 /* 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 __P((char *, struct request_info *));
88 static int list_match __P((char *, struct request_info *,
89 int (*)(char *, struct request_info *)));
90 static int server_match __P((char *, struct request_info *));
91 static int client_match __P((char *, struct request_info *));
92 static int host_match __P((char *, struct host_info *));
93 static int rbl_match __P((char *, char *));
94 static int string_match __P((char *, char *));
95 static int masked_match __P((char *, char *, char *));
96 static int masked_match4 __P((char *, char *, char *));
97 #ifdef INET6
98 static int masked_match6 __P((char *, char *, char *));
99 #endif
100
101 /* Size of logical line buffer. */
102
103 #define BUFLEN 2048
104
105 /* hosts_access - host access control facility */
106
107 int hosts_access(request)
108 struct request_info *request;
109 {
110 int verdict;
111
112 /*
113 * If the (daemon, client) pair is matched by an entry in the file
114 * /etc/hosts.allow, access is granted. Otherwise, if the (daemon,
115 * client) pair is matched by an entry in the file /etc/hosts.deny,
116 * access is denied. Otherwise, access is granted. A non-existent
117 * access-control file is treated as an empty file.
118 *
119 * After a rule has been matched, the optional language extensions may
120 * decide to grant or refuse service anyway. Or, while a rule is being
121 * processed, a serious error is found, and it seems better to play safe
122 * and deny service. All this is done by jumping back into the
123 * hosts_access() routine, bypassing the regular return from the
124 * table_match() function calls below.
125 */
126
127 if (resident <= 0)
128 resident++;
129 verdict = setjmp(tcpd_buf);
130 if (verdict != 0)
131 return (verdict == AC_PERMIT);
132 if (table_match(hosts_allow_table, request))
133 return (YES);
134 if (table_match(hosts_deny_table, request))
135 return (NO);
136 return (YES);
137 }
138
139 /* table_match - match table entries with (daemon, client) pair */
140
141 static int table_match(table, request)
142 char *table;
143 struct request_info *request;
144 {
145 FILE *fp;
146 char sv_list[BUFLEN]; /* becomes list of daemons */
147 char *cl_list; /* becomes list of clients */
148 char *sh_cmd = NULL; /* becomes optional shell command */
149 int match = NO;
150 struct tcpd_context saved_context;
151
152 saved_context = tcpd_context; /* stupid compilers */
153
154 /*
155 * Between the fopen() and fclose() calls, avoid jumps that may cause
156 * file descriptor leaks.
157 */
158
159 if ((fp = fopen(table, "r")) != 0) {
160 tcpd_context.file = table;
161 tcpd_context.line = 0;
162 while (match == NO && xgets(sv_list, sizeof(sv_list), fp) != 0) {
163 if (sv_list[strlen(sv_list) - 1] != '\n') {
164 tcpd_warn("missing newline or line too long");
165 continue;
166 }
167 if (sv_list[0] == '#' || sv_list[strspn(sv_list, " \t\r\n")] == 0)
168 continue;
169 if ((cl_list = split_at(sv_list, ':')) == 0) {
170 tcpd_warn("missing \":\" separator");
171 continue;
172 }
173 sh_cmd = split_at(cl_list, ':');
174 match = list_match(sv_list, request, server_match)
175 && list_match(cl_list, request, client_match);
176 }
177 (void) fclose(fp);
178 } else if (errno != ENOENT) {
179 tcpd_warn("cannot open %s: %m", table);
180 }
181 if (match) {
182 if (hosts_access_verbose > 1)
183 syslog(LOG_DEBUG, "matched: %s line %d",
184 tcpd_context.file, tcpd_context.line);
185 if (sh_cmd) {
186 #ifdef PROCESS_OPTIONS
187 process_options(sh_cmd, request);
188 #else
189 char cmd[BUFSIZ];
190 shell_cmd(percent_x(cmd, sizeof(cmd), sh_cmd, request));
191 #endif
192 }
193 }
194 tcpd_context = saved_context;
195 return (match);
196 }
197
198 /* list_match - match a request against a list of patterns with exceptions */
199
200 static int list_match(list, request, match_fn)
201 char *list;
202 struct request_info *request;
203 int (*match_fn) __P((char *, struct request_info *));
204 {
205 char *tok;
206 static char *last;
207 int l;
208
209 /*
210 * Process tokens one at a time. We have exhausted all possible matches
211 * when we reach an "EXCEPT" token or the end of the list. If we do find
212 * a match, look for an "EXCEPT" list and recurse to determine whether
213 * the match is affected by any exceptions.
214 */
215
216 for (tok = strtok_r(list, sep, &last); tok != 0;
217 tok = strtok_r(NULL, sep, &last)) {
218 if (STR_EQ(tok, "EXCEPT")) /* EXCEPT: give up */
219 return (NO);
220 l = strlen(tok);
221 if (*tok == '[' && tok[l - 1] == ']') {
222 tok[l - 1] = '\0';
223 tok++;
224 }
225 if (match_fn(tok, request)) { /* YES: look for exceptions */
226 while ((tok = strtok_r(NULL, sep, &last)) && STR_NE(tok, "EXCEPT"))
227 /* VOID */ ;
228 return (tok == 0 || list_match(NULL, request, match_fn) == 0);
229 }
230 }
231 return (NO);
232 }
233
234 /* server_match - match server information */
235
236 static int server_match(tok, request)
237 char *tok;
238 struct request_info *request;
239 {
240 char *host;
241
242 if ((host = split_at(tok + 1, '@')) == 0) { /* plain daemon */
243 return (string_match(tok, eval_daemon(request)));
244 } else { /* daemon@host */
245 return (string_match(tok, eval_daemon(request))
246 && host_match(host, request->server));
247 }
248 }
249
250 /* client_match - match client information */
251
252 static int client_match(tok, request)
253 char *tok;
254 struct request_info *request;
255 {
256 char *host;
257
258 if ((host = split_at(tok + 1, '@')) == 0) { /* plain host */
259 return (host_match(tok, request->client));
260 } else { /* user@host */
261 return (host_match(host, request->client)
262 && string_match(tok, eval_user(request)));
263 }
264 }
265
266 /* host_match - match host name and/or address against pattern */
267
268 static int host_match(tok, host)
269 char *tok;
270 struct host_info *host;
271 {
272 char *mask;
273
274 /*
275 * This code looks a little hairy because we want to avoid unnecessary
276 * hostname lookups.
277 *
278 * The KNOWN pattern requires that both address AND name be known; some
279 * patterns are specific to host names or to host addresses; all other
280 * patterns are satisfied when either the address OR the name match.
281 */
282
283 if (tok[0] == '@') { /* netgroup: look it up */
284 #ifdef NETGROUP
285 static char *mydomain = 0;
286 if (mydomain == 0)
287 yp_get_default_domain(&mydomain);
288 return (innetgr(tok + 1, eval_hostname(host), NULL, mydomain));
289 #else
290 tcpd_warn("netgroup support is disabled"); /* not tcpd_jump() */
291 return (NO);
292 #endif
293 } else if (STR_EQ(tok, "KNOWN")) { /* check address and name */
294 char *name = eval_hostname(host);
295 return (STR_NE(eval_hostaddr(host), unknown) && HOSTNAME_KNOWN(name));
296 } else if (STR_EQ(tok, "LOCAL")) { /* local: no dots in name */
297 char *name = eval_hostname(host);
298 return (strchr(name, '.') == 0 && HOSTNAME_KNOWN(name));
299 } else if (strncmp(tok, "{RBL}.", 6) == 0) { /* RBL lookup in domain */
300 return rbl_match(tok+6, eval_hostaddr(host));
301 } else if ((mask = split_at(tok, '/')) != 0) { /* net/mask */
302 return (masked_match(tok, mask, eval_hostaddr(host)));
303 } else { /* anything else */
304 return (string_match(tok, eval_hostaddr(host))
305 || (NOT_INADDR(tok) && string_match(tok, eval_hostname(host))));
306 }
307 }
308
309 /* rbl_match() - match host by looking up in RBL domain */
310
311 static int rbl_match(rbl_domain, rbl_hostaddr)
312 char *rbl_domain; /* RBL domain */
313 char *rbl_hostaddr; /* hostaddr */
314 {
315 char *rbl_name;
316 unsigned long host_address;
317 int ret = NO;
318 size_t len = strlen(rbl_domain) + (4 * 4) + 2;
319
320 if (dot_quad_addr(rbl_hostaddr, &host_address) != 0) {
321 tcpd_warn("unable to convert %s to address", rbl_hostaddr);
322 return (NO);
323 }
324 host_address = ntohl(host_address);
325 /* construct the rbl name to look up */
326 if ((rbl_name = malloc(len)) == NULL) {
327 tcpd_jump("not enough memory to build RBL name for %s in %s", rbl_hostaddr, rbl_domain);
328 /* NOTREACHED */
329 }
330 snprintf(rbl_name, len, "%u.%u.%u.%u.%s",
331 (unsigned int) ((host_address) & 0xff),
332 (unsigned int) ((host_address >> 8) & 0xff),
333 (unsigned int) ((host_address >> 16) & 0xff),
334 (unsigned int) ((host_address >> 24) & 0xff),
335 rbl_domain);
336 /* look it up */
337 if (gethostbyname(rbl_name) != NULL) {
338 /* successful lookup - they're on the RBL list */
339 ret = YES;
340 }
341 free(rbl_name);
342
343 return ret;
344 }
345
346 /* string_match - match string against pattern */
347
348 static int string_match(tok, string)
349 char *tok;
350 char *string;
351 {
352 int n;
353
354 if (tok[0] == '.') { /* suffix */
355 n = strlen(string) - strlen(tok);
356 return (n > 0 && STR_EQ(tok, string + n));
357 } else if (STR_EQ(tok, "ALL")) { /* all: match any */
358 return (YES);
359 } else if (STR_EQ(tok, "KNOWN")) { /* not unknown */
360 return (STR_NE(string, unknown));
361 } else if (tok[(n = strlen(tok)) - 1] == '.') { /* prefix */
362 return (STRN_EQ(tok, string, n));
363 } else { /* exact match */
364 return (STR_EQ(tok, string));
365 }
366 }
367
368 /* masked_match - match address against netnumber/netmask */
369
370 static int masked_match(net_tok, mask_tok, string)
371 char *net_tok;
372 char *mask_tok;
373 char *string;
374 {
375 #ifndef INET6
376 return masked_match4(net_tok, mask_tok, string);
377 #else
378 /*
379 * masked_match4() is kept just for supporting shortened IPv4 address form.
380 * If we could get rid of shortened IPv4 form, we could just always use
381 * masked_match6().
382 */
383 if (dot_quad_addr(net_tok, NULL) != INADDR_NONE &&
384 dot_quad_addr(mask_tok, NULL) != INADDR_NONE &&
385 dot_quad_addr(string, NULL) != INADDR_NONE) {
386 return masked_match4(net_tok, mask_tok, string);
387 } else
388 return masked_match6(net_tok, mask_tok, string);
389 #endif
390 }
391
392 static int masked_match4(net_tok, mask_tok, string)
393 char *net_tok;
394 char *mask_tok;
395 char *string;
396 {
397 unsigned long net;
398 unsigned long mask;
399 unsigned long addr;
400
401 /*
402 * Disallow forms other than dotted quad: the treatment that inet_addr()
403 * gives to forms with less than four components is inconsistent with the
404 * access control language. John P. Rouillard <rouilj (at) cs.umb.edu>.
405 */
406
407 if (dot_quad_addr(string, &addr) != 0)
408 return (NO);
409 if (dot_quad_addr(net_tok, &net) != 0 ||
410 dot_quad_addr(mask_tok, &mask) != 0) {
411 tcpd_warn("bad net/mask expression: %s/%s", net_tok, mask_tok);
412 return (NO); /* not tcpd_jump() */
413 }
414
415 if ((net & ~mask) != 0)
416 tcpd_warn("host bits not all zero in %s/%s", net_tok, mask_tok);
417
418 return ((addr & mask) == net);
419 }
420
421 #ifdef INET6
422 static int masked_match6(net_tok, mask_tok, string)
423 char *net_tok;
424 char *mask_tok;
425 char *string;
426 {
427 union {
428 struct sockaddr sa;
429 struct sockaddr_in sin;
430 struct sockaddr_in6 sin6;
431 } net, mask, addr;
432 struct addrinfo hints, *res;
433 unsigned long masklen;
434 char *ep;
435 int i;
436 char *np, *mp, *ap;
437 int alen;
438
439 memset(&hints, 0, sizeof(hints));
440 hints.ai_family = PF_UNSPEC;
441 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
442 hints.ai_flags = AI_NUMERICHOST;
443 if (getaddrinfo(net_tok, "0", &hints, &res) == 0) {
444 if (res->ai_addrlen > sizeof(net) || res->ai_next) {
445 freeaddrinfo(res);
446 return NO;
447 }
448 memcpy(&net, res->ai_addr, res->ai_addrlen);
449 freeaddrinfo(res);
450 } else
451 return NO;
452
453 memset(&hints, 0, sizeof(hints));
454 hints.ai_family = net.sa.sa_family;
455 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
456 hints.ai_flags = AI_NUMERICHOST;
457 ep = NULL;
458 if (getaddrinfo(mask_tok, "0", &hints, &res) == 0) {
459 if (res->ai_family == AF_INET6 &&
460 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id) {
461 freeaddrinfo(res);
462 return NO;
463 }
464 if (res->ai_addrlen > sizeof(mask) || res->ai_next) {
465 freeaddrinfo(res);
466 return NO;
467 }
468 memcpy(&mask, res->ai_addr, res->ai_addrlen);
469 freeaddrinfo(res);
470 } else {
471 ep = NULL;
472 masklen = strtoul(mask_tok, &ep, 10);
473 if (ep && !*ep) {
474 memset(&mask, 0, sizeof(mask));
475 mask.sa.sa_family = net.sa.sa_family;
476 mask.sa.sa_len = net.sa.sa_len;
477 switch (mask.sa.sa_family) {
478 case AF_INET:
479 mp = (char *)&mask.sin.sin_addr;
480 alen = sizeof(mask.sin.sin_addr);
481 break;
482 case AF_INET6:
483 mp = (char *)&mask.sin6.sin6_addr;
484 alen = sizeof(mask.sin6.sin6_addr);
485 break;
486 default:
487 return NO;
488 }
489 if (masklen / 8 > alen)
490 return NO;
491 memset(mp, 0xff, masklen / 8);
492 if (masklen % 8)
493 mp[masklen / 8] = 0xff00 >> (masklen % 8);
494 } else
495 return NO;
496 }
497
498 memset(&hints, 0, sizeof(hints));
499 hints.ai_family = PF_UNSPEC;
500 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
501 hints.ai_flags = AI_NUMERICHOST;
502 if (getaddrinfo(string, "0", &hints, &res) == 0) {
503 if (res->ai_addrlen > sizeof(addr) || res->ai_next) {
504 freeaddrinfo(res);
505 return NO;
506 }
507 /* special case - IPv4 mapped address */
508 if (net.sa.sa_family == AF_INET && res->ai_family == AF_INET6 &&
509 IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr)) {
510 memset(&addr, 0, sizeof(addr));
511 addr.sa.sa_family = net.sa.sa_family;
512 addr.sa.sa_len = net.sa.sa_len;
513 memcpy(&addr.sin.sin_addr,
514 &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr.s6_addr[12],
515 sizeof(addr.sin.sin_addr));
516 } else
517 memcpy(&addr, res->ai_addr, res->ai_addrlen);
518 freeaddrinfo(res);
519 } else
520 return NO;
521
522 if (net.sa.sa_family != mask.sa.sa_family ||
523 net.sa.sa_family != addr.sa.sa_family) {
524 return NO;
525 }
526
527 switch (net.sa.sa_family) {
528 case AF_INET:
529 np = (char *)&net.sin.sin_addr;
530 mp = (char *)&mask.sin.sin_addr;
531 ap = (char *)&addr.sin.sin_addr;
532 alen = sizeof(net.sin.sin_addr);
533 break;
534 case AF_INET6:
535 np = (char *)&net.sin6.sin6_addr;
536 mp = (char *)&mask.sin6.sin6_addr;
537 ap = (char *)&addr.sin6.sin6_addr;
538 alen = sizeof(net.sin6.sin6_addr);
539 break;
540 default:
541 return NO;
542 }
543
544 for (i = 0; i < alen; i++)
545 if (np[i] & ~mp[i]) {
546 tcpd_warn("host bits not all zero in %s/%s", net_tok, mask_tok);
547 break;
548 }
549
550 for (i = 0; i < alen; i++)
551 ap[i] &= mp[i];
552
553 if (addr.sa.sa_family == AF_INET6 && addr.sin6.sin6_scope_id &&
554 addr.sin6.sin6_scope_id != net.sin6.sin6_scope_id)
555 return NO;
556 return (memcmp(ap, np, alen) == 0);
557 }
558 #endif
559