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