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: stable/12/contrib/tcp_wrappers/hosts_access.c 370737 2021-10-06 07:17:11Z kevans $
20 */
21
22 #ifndef lint
23 static 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 #ifdef INET6
45 #include <netdb.h>
46 #endif
47 #include <stdlib.h>
48
49 extern int errno;
50
51 #ifndef INADDR_NONE
52 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
53 #endif
54
55 /* Local stuff. */
56
57 #include "tcpd.h"
58
59 /* Error handling. */
60
61 extern jmp_buf tcpd_buf;
62
63 /* Delimiters for lists of daemons or clients. */
64
65 static char sep[] = ", \t\r\n";
66
67 /* Constants to be used in assignments only, not in comparisons... */
68
69 #define YES 1
70 #define NO 0
71
72 /*
73 * These variables are globally visible so that they can be redirected in
74 * verification mode.
75 */
76
77 char *hosts_allow_table = HOSTS_ALLOW;
78 char *hosts_deny_table = HOSTS_DENY;
79 int hosts_access_verbose = 0;
80
81 /*
82 * In a long-running process, we are not at liberty to just go away.
83 */
84
85 int resident = (-1); /* -1, 0: unknown; +1: yes */
86
87 /* Forward declarations. */
88
89 static int table_match(char *table, struct request_info *request);
90 static int list_match(char *list, struct request_info *request,
91 int (*match_fn)(char *, struct request_info *));
92 static int server_match(char *tok, struct request_info *request);
93 static int client_match(char *tok, struct request_info *request);
94 static int host_match(char *tok, struct host_info *host);
95 static int string_match(char *tok, char *string);
96 static int masked_match(char *net_tok, char *mask_tok, char *string);
97 #ifdef INET6
98 static int masked_match4(char *net_tok, char *mask_tok, char *string);
99 static int masked_match6(char *net_tok, char *mask_tok, char *string);
100 #endif
101
102 /* Size of logical line buffer. */
103
104 #define BUFLEN 2048
105
106 /* definition to be used from workarounds.c */
107 #ifdef NETGROUP
108 int yp_get_default_domain(char **);
109 #endif
110
111 /* hosts_access - host access control facility */
112
hosts_access(request)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
table_match(table,request)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; /* becomes optional shell command */
155 int match = NO;
156 struct tcpd_context saved_context;
157 char *cp;
158
159 saved_context = tcpd_context; /* stupid compilers */
160
161 /*
162 * Between the fopen() and fclose() calls, avoid jumps that may cause
163 * file descriptor leaks.
164 */
165
166 if ((fp = fopen(table, "r")) != 0) {
167 tcpd_context.file = table;
168 tcpd_context.line = 0;
169 while (match == NO && xgets(sv_list, sizeof(sv_list), fp) != 0) {
170 if (sv_list[strlen(sv_list) - 1] != '\n') {
171 tcpd_warn("missing newline or line too long");
172 continue;
173 }
174 /* Ignore anything after unescaped # character */
175 for (cp = strchr(sv_list, '#'); cp != NULL;) {
176 if (cp > sv_list && cp[-1] == '\\') {
177 cp = strchr(cp + 1, '#');
178 continue;
179 }
180 *cp = '\0';
181 break;
182 }
183 if (sv_list[strspn(sv_list, " \t\r\n")] == 0)
184 continue;
185 if ((cl_list = split_at(sv_list, ':')) == 0) {
186 tcpd_warn("missing \":\" separator");
187 continue;
188 }
189 sh_cmd = split_at(cl_list, ':');
190 match = list_match(sv_list, request, server_match)
191 && list_match(cl_list, request, client_match);
192 }
193 (void) fclose(fp);
194 } else if (errno != ENOENT) {
195 tcpd_warn("cannot open %s: %m", table);
196 }
197 if (match) {
198 if (hosts_access_verbose > 1)
199 syslog(LOG_DEBUG, "matched: %s line %d",
200 tcpd_context.file, tcpd_context.line);
201 if (sh_cmd) {
202 #ifdef PROCESS_OPTIONS
203 process_options(sh_cmd, request);
204 #else
205 char cmd[BUFSIZ];
206 shell_cmd(percent_x(cmd, sizeof(cmd), sh_cmd, request));
207 #endif
208 }
209 }
210 tcpd_context = saved_context;
211 return (match);
212 }
213
214 /* list_match - match a request against a list of patterns with exceptions */
215
list_match(char * list,struct request_info * request,int (* match_fn)(char *,struct request_info *))216 static int list_match(char *list, struct request_info *request,
217 int (*match_fn)(char *, struct request_info *))
218 {
219 char *tok;
220
221 /*
222 * Process tokens one at a time. We have exhausted all possible matches
223 * when we reach an "EXCEPT" token or the end of the list. If we do find
224 * a match, look for an "EXCEPT" list and recurse to determine whether
225 * the match is affected by any exceptions.
226 */
227
228 for (tok = strtok(list, sep); tok != 0; tok = strtok((char *) 0, sep)) {
229 if (STR_EQ(tok, "EXCEPT")) /* EXCEPT: give up */
230 return (NO);
231 if (match_fn(tok, request)) { /* YES: look for exceptions */
232 while ((tok = strtok((char *) 0, sep)) && STR_NE(tok, "EXCEPT"))
233 /* VOID */ ;
234 return (tok == 0 || list_match((char *) 0, request, match_fn) == 0);
235 }
236 }
237 return (NO);
238 }
239
240 /* server_match - match server information */
241
server_match(tok,request)242 static int server_match(tok, request)
243 char *tok;
244 struct request_info *request;
245 {
246 char *host;
247
248 if ((host = split_at(tok + 1, '@')) == 0) { /* plain daemon */
249 return (string_match(tok, eval_daemon(request)));
250 } else { /* daemon@host */
251 return (string_match(tok, eval_daemon(request))
252 && host_match(host, request->server));
253 }
254 }
255
256 /* client_match - match client information */
257
client_match(tok,request)258 static int client_match(tok, request)
259 char *tok;
260 struct request_info *request;
261 {
262 char *host;
263
264 if ((host = split_at(tok + 1, '@')) == 0) { /* plain host */
265 return (host_match(tok, request->client));
266 } else { /* user@host */
267 return (host_match(host, request->client)
268 && string_match(tok, eval_user(request)));
269 }
270 }
271
272 /* hostfile_match - look up host patterns from file */
273
hostfile_match(path,host)274 static int hostfile_match(path, host)
275 char *path;
276 struct host_info *host;
277 {
278 char tok[BUFSIZ];
279 int match = NO;
280 FILE *fp;
281
282 if ((fp = fopen(path, "r")) != 0) {
283 while (fscanf(fp, "%s", tok) == 1 && !(match = host_match(tok, host)))
284 /* void */ ;
285 fclose(fp);
286 } else if (errno != ENOENT) {
287 tcpd_warn("open %s: %m", path);
288 }
289 return (match);
290 }
291
292 /* host_match - match host name and/or address against pattern */
293
host_match(tok,host)294 static int host_match(tok, host)
295 char *tok;
296 struct host_info *host;
297 {
298 char *mask;
299
300 /*
301 * This code looks a little hairy because we want to avoid unnecessary
302 * hostname lookups.
303 *
304 * The KNOWN pattern requires that both address AND name be known; some
305 * patterns are specific to host names or to host addresses; all other
306 * patterns are satisfied when either the address OR the name match.
307 */
308
309 if (tok[0] == '@') { /* netgroup: look it up */
310 #ifdef NETGROUP
311 static char *mydomain = 0;
312 if (mydomain == 0)
313 yp_get_default_domain(&mydomain);
314 return (innetgr(tok + 1, eval_hostname(host), (char *) 0, mydomain));
315 #else
316 tcpd_warn("netgroup support is disabled"); /* not tcpd_jump() */
317 return (NO);
318 #endif
319 } else if (tok[0] == '/') { /* /file hack */
320 return (hostfile_match(tok, host));
321 } else if (STR_EQ(tok, "KNOWN")) { /* check address and name */
322 char *name = eval_hostname(host);
323 return (STR_NE(eval_hostaddr(host), unknown) && HOSTNAME_KNOWN(name));
324 } else if (STR_EQ(tok, "LOCAL")) { /* local: no dots in name */
325 char *name = eval_hostname(host);
326 return (strchr(name, '.') == 0 && HOSTNAME_KNOWN(name));
327 } else if ((mask = split_at(tok, '/')) != 0) { /* net/mask */
328 return (masked_match(tok, mask, eval_hostaddr(host)));
329 } else { /* anything else */
330 return (string_match(tok, eval_hostaddr(host))
331 || (NOT_INADDR(tok) && string_match(tok, eval_hostname(host))));
332 }
333 }
334
335 /* string_match - match string against pattern */
336
string_match(tok,string)337 static int string_match(tok, string)
338 char *tok;
339 char *string;
340 {
341 int n;
342
343 #ifdef INET6
344 /* convert IPv4 mapped IPv6 address to IPv4 address */
345 if (STRN_EQ(string, "::ffff:", 7)
346 && dot_quad_addr(string + 7) != INADDR_NONE) {
347 string += 7;
348 }
349 #endif
350 if (tok[0] == '.') { /* suffix */
351 n = strlen(string) - strlen(tok);
352 return (n > 0 && STR_EQ(tok, string + n));
353 } else if (STR_EQ(tok, "ALL")) { /* all: match any */
354 return (YES);
355 } else if (STR_EQ(tok, "KNOWN")) { /* not unknown */
356 return (STR_NE(string, unknown));
357 } else if (tok[(n = strlen(tok)) - 1] == '.') { /* prefix */
358 return (STRN_EQ(tok, string, n));
359 } else { /* exact match */
360 #ifdef INET6
361 struct addrinfo hints, *res;
362 struct sockaddr_in6 pat, addr;
363 int len, ret;
364 char ch;
365
366 len = strlen(tok);
367 if (*tok == '[' && tok[len - 1] == ']') {
368 ch = tok[len - 1];
369 tok[len - 1] = '\0';
370 memset(&hints, 0, sizeof(hints));
371 hints.ai_family = AF_INET6;
372 hints.ai_socktype = SOCK_STREAM;
373 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
374 if ((ret = getaddrinfo(tok + 1, NULL, &hints, &res)) == 0) {
375 memcpy(&pat, res->ai_addr, sizeof(pat));
376 freeaddrinfo(res);
377 }
378 tok[len - 1] = ch;
379 if (ret != 0 || getaddrinfo(string, NULL, &hints, &res) != 0)
380 return NO;
381 memcpy(&addr, res->ai_addr, sizeof(addr));
382 freeaddrinfo(res);
383 if (pat.sin6_scope_id != 0 &&
384 addr.sin6_scope_id != pat.sin6_scope_id)
385 return NO;
386 return (!memcmp(&pat.sin6_addr, &addr.sin6_addr,
387 sizeof(struct in6_addr)));
388 return (ret);
389 }
390 #endif
391 return (STR_EQ(tok, string));
392 }
393 }
394
395 /* masked_match - match address against netnumber/netmask */
396
397 #ifdef INET6
masked_match(net_tok,mask_tok,string)398 static int masked_match(net_tok, mask_tok, string)
399 char *net_tok;
400 char *mask_tok;
401 char *string;
402 {
403 return (masked_match4(net_tok, mask_tok, string) ||
404 masked_match6(net_tok, mask_tok, string));
405 }
406
masked_match4(net_tok,mask_tok,string)407 static int masked_match4(net_tok, mask_tok, string)
408 #else
409 static int masked_match(net_tok, mask_tok, string)
410 #endif
411 char *net_tok;
412 char *mask_tok;
413 char *string;
414 {
415 #ifdef INET6
416 u_int32_t net;
417 u_int32_t mask;
418 u_int32_t addr;
419 #else
420 unsigned long net;
421 unsigned long mask;
422 unsigned long addr;
423 #endif
424
425 /*
426 * Disallow forms other than dotted quad: the treatment that inet_addr()
427 * gives to forms with less than four components is inconsistent with the
428 * access control language. John P. Rouillard <rouilj@cs.umb.edu>.
429 */
430
431 if ((addr = dot_quad_addr(string)) == INADDR_NONE)
432 return (NO);
433 if ((net = dot_quad_addr(net_tok)) == INADDR_NONE
434 || (mask = dot_quad_addr(mask_tok)) == INADDR_NONE) {
435 #ifndef INET6
436 tcpd_warn("bad net/mask expression: %s/%s", net_tok, mask_tok);
437 #endif
438 return (NO); /* not tcpd_jump() */
439 }
440 return ((addr & mask) == net);
441 }
442
443 #ifdef INET6
masked_match6(net_tok,mask_tok,string)444 static int masked_match6(net_tok, mask_tok, string)
445 char *net_tok;
446 char *mask_tok;
447 char *string;
448 {
449 struct addrinfo hints, *res;
450 struct sockaddr_in6 net, addr;
451 u_int32_t mask;
452 int len, mask_len, i = 0;
453 char ch;
454
455 memset(&hints, 0, sizeof(hints));
456 hints.ai_family = AF_INET6;
457 hints.ai_socktype = SOCK_STREAM;
458 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
459 if (getaddrinfo(string, NULL, &hints, &res) != 0)
460 return NO;
461 memcpy(&addr, res->ai_addr, sizeof(addr));
462 freeaddrinfo(res);
463
464 if (IN6_IS_ADDR_V4MAPPED(&addr.sin6_addr)) {
465 if ((*(u_int32_t *)&net.sin6_addr.s6_addr[12] = dot_quad_addr(net_tok)) == INADDR_NONE
466 || (mask = dot_quad_addr(mask_tok)) == INADDR_NONE)
467 return (NO);
468 return ((*(u_int32_t *)&addr.sin6_addr.s6_addr[12] & mask) == *(u_int32_t *)&net.sin6_addr.s6_addr[12]);
469 }
470
471 /* match IPv6 address against netnumber/prefixlen */
472 len = strlen(net_tok);
473 if (*net_tok != '[' || net_tok[len - 1] != ']')
474 return NO;
475 ch = net_tok[len - 1];
476 net_tok[len - 1] = '\0';
477 if (getaddrinfo(net_tok + 1, NULL, &hints, &res) != 0) {
478 net_tok[len - 1] = ch;
479 return NO;
480 }
481 memcpy(&net, res->ai_addr, sizeof(net));
482 freeaddrinfo(res);
483 net_tok[len - 1] = ch;
484 if ((mask_len = atoi(mask_tok)) < 0 || mask_len > 128)
485 return NO;
486
487 if (net.sin6_scope_id != 0 && addr.sin6_scope_id != net.sin6_scope_id)
488 return NO;
489 while (mask_len > 0) {
490 if (mask_len < 32) {
491 mask = htonl(~(0xffffffff >> mask_len));
492 if ((*(u_int32_t *)&addr.sin6_addr.s6_addr[i] & mask) != (*(u_int32_t *)&net.sin6_addr.s6_addr[i] & mask))
493 return NO;
494 break;
495 }
496 if (*(u_int32_t *)&addr.sin6_addr.s6_addr[i] != *(u_int32_t *)&net.sin6_addr.s6_addr[i])
497 return NO;
498 i += 4;
499 mask_len -= 32;
500 }
501 return YES;
502 }
503 #endif /* INET6 */
504