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