2 * tcpdchk - examine all tcpd access control rules and inetd.conf entries
4 * Usage: tcpdchk [-a] [-d] [-i inet_conf] [-v]
6 * -a: complain about implicit "allow" at end of rule.
8 * -d: rules in current directory.
10 * -i: location of inetd.conf file.
14 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
16 * $FreeBSD: src/contrib/tcp_wrappers/tcpdchk.c,v 1.3.2.1 2000/07/18 08:34:55 ume Exp $
17 * $DragonFly: src/contrib/tcp_wrappers/tcpdchk.c,v 1.2 2003/06/17 04:24:06 dillon Exp $
21 static char sccsid[] = "@(#) tcpdchk.c 1.8 97/02/12 02:13:25";
24 /* System libraries. */
26 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
46 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
50 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
53 /* Application-specific. */
60 * Stolen from hosts_access.c...
62 static char sep[] = ", \t\n";
67 int hosts_access_verbose = 0;
68 char *hosts_allow_table = HOSTS_ALLOW;
69 char *hosts_deny_table = HOSTS_DENY;
70 extern jmp_buf tcpd_buf;
76 static void parse_table();
77 static void print_list();
78 static void check_daemon_list();
79 static void check_client_list();
80 static void check_daemon();
81 static void check_user();
82 static int check_host();
83 static int reserved_name();
91 static int defl_verdict;
93 static int allow_check;
100 struct request_info request;
109 while ((c = getopt(argc, argv, "adi:v")) != EOF) {
115 hosts_allow_table = "hosts.allow";
116 hosts_deny_table = "hosts.deny";
122 hosts_access_verbose++;
133 * When confusion really strikes...
135 if (check_path(REAL_DAEMON_DIR, &st) < 0) {
136 tcpd_warn("REAL_DAEMON_DIR %s: %m", REAL_DAEMON_DIR);
137 } else if (!S_ISDIR(st.st_mode)) {
138 tcpd_warn("REAL_DAEMON_DIR %s is not a directory", REAL_DAEMON_DIR);
142 * Process the inet configuration file (or its moral equivalent). This
143 * information is used later to find references in hosts.allow/deny to
144 * unwrapped services, and other possible problems.
146 inetcf = inet_cfg(inetcf);
147 if (hosts_access_verbose)
148 printf("Using network configuration file: %s\n", inetcf);
151 * These are not run from inetd but may have built-in access control.
153 inet_set("portmap", WR_NOT);
154 inet_set("rpcbind", WR_NOT);
157 * Check accessibility of access control files.
159 (void) check_path(hosts_allow_table, &st);
160 (void) check_path(hosts_deny_table, &st);
163 * Fake up an arbitrary service request.
165 request_init(&request,
166 RQ_DAEMON, "daemon_name",
167 RQ_SERVER_NAME, "server_hostname",
168 RQ_SERVER_ADDR, "server_addr",
169 RQ_USER, "user_name",
170 RQ_CLIENT_NAME, "client_hostname",
171 RQ_CLIENT_ADDR, "client_addr",
176 * Examine all access-control rules.
178 defl_verdict = PERMIT;
179 parse_table(hosts_allow_table, &request);
181 parse_table(hosts_deny_table, &request);
185 /* usage - explain */
189 fprintf(stderr, "usage: %s [-a] [-d] [-i inet_conf] [-v]\n", myname);
190 fprintf(stderr, " -a: report rules with implicit \"ALLOW\" at end\n");
191 fprintf(stderr, " -d: use allow/deny files in current directory\n");
192 fprintf(stderr, " -i: location of inetd.conf file\n");
193 fprintf(stderr, " -v: list all rules\n");
197 /* parse_table - like table_match(), but examines _all_ entries */
199 static void parse_table(table, request)
201 struct request_info *request;
205 char sv_list[BUFLEN]; /* becomes list of daemons */
206 char *cl_list; /* becomes list of requests */
207 char *sh_cmd; /* becomes optional shell command */
210 struct tcpd_context saved_context;
212 saved_context = tcpd_context; /* stupid compilers */
214 if (fp = fopen(table, "r")) {
215 tcpd_context.file = table;
216 tcpd_context.line = 0;
217 while (xgets(sv_list, sizeof(sv_list), fp)) {
218 if (sv_list[strlen(sv_list) - 1] != '\n') {
219 tcpd_warn("missing newline or line too long");
222 if (sv_list[0] == '#' || sv_list[strspn(sv_list, " \t\r\n")] == 0)
224 if ((cl_list = split_at(sv_list, ':')) == 0) {
225 tcpd_warn("missing \":\" separator");
228 sh_cmd = split_at(cl_list, ':');
230 if (hosts_access_verbose)
231 printf("\n>>> Rule %s line %d:\n",
232 tcpd_context.file, tcpd_context.line);
234 if (hosts_access_verbose)
235 print_list("daemons: ", sv_list);
236 check_daemon_list(sv_list);
238 if (hosts_access_verbose)
239 print_list("clients: ", cl_list);
240 check_client_list(cl_list);
242 #ifdef PROCESS_OPTIONS
243 real_verdict = defl_verdict;
245 verdict = setjmp(tcpd_buf);
247 real_verdict = (verdict == AC_PERMIT);
250 process_options(sh_cmd, request);
251 if (dry_run == 1 && real_verdict && allow_check)
252 tcpd_warn("implicit \"allow\" at end of rule");
254 } else if (defl_verdict && allow_check) {
255 tcpd_warn("implicit \"allow\" at end of rule");
257 if (hosts_access_verbose)
258 printf("access: %s\n", real_verdict ? "granted" : "denied");
261 shell_cmd(percent_x(buf, sizeof(buf), sh_cmd, request));
262 if (hosts_access_verbose)
263 printf("access: %s\n", defl_verdict ? "granted" : "denied");
267 } else if (errno != ENOENT) {
268 tcpd_warn("cannot open %s: %m", table);
270 tcpd_context = saved_context;
273 /* print_list - pretty-print a list */
275 static void print_list(title, list)
283 fputs(title, stdout);
286 for (cp = strtok(buf, sep); cp != 0; cp = next) {
288 next = strtok((char *) 0, sep);
295 /* check_daemon_list - criticize daemon list */
297 static void check_daemon_list(list)
307 for (cp = strtok(buf, sep); cp != 0; cp = strtok((char *) 0, sep)) {
308 if (STR_EQ(cp, "EXCEPT")) {
312 if ((host = split_at(cp + 1, '@')) != 0 && check_host(host) > 1) {
313 tcpd_warn("host %s has more than one address", host);
314 tcpd_warn("(consider using an address instead)");
320 tcpd_warn("daemon list is empty or ends in EXCEPT");
323 /* check_client_list - criticize client list */
325 static void check_client_list(list)
335 for (cp = strtok(buf, sep); cp != 0; cp = strtok((char *) 0, sep)) {
336 if (STR_EQ(cp, "EXCEPT")) {
340 if (host = split_at(cp + 1, '@')) { /* user@host */
349 tcpd_warn("client list is empty or ends in EXCEPT");
352 /* check_daemon - criticize daemon pattern */
354 static void check_daemon(pat)
358 tcpd_warn("%s: daemon name begins with \"@\"", pat);
359 } else if (pat[0] == '/') {
360 tcpd_warn("%s: daemon name begins with \"/\"", pat);
361 } else if (pat[0] == '.') {
362 tcpd_warn("%s: daemon name begins with dot", pat);
363 } else if (pat[strlen(pat) - 1] == '.') {
364 tcpd_warn("%s: daemon name ends in dot", pat);
365 } else if (STR_EQ(pat, "ALL") || STR_EQ(pat, unknown)) {
367 } else if (STR_EQ(pat, "FAIL")) { /* obsolete */
368 tcpd_warn("FAIL is no longer recognized");
369 tcpd_warn("(use EXCEPT or DENY instead)");
370 } else if (reserved_name(pat)) {
371 tcpd_warn("%s: daemon name may be reserved word", pat);
373 switch (inet_get(pat)) {
375 tcpd_warn("%s: no such process name in %s", pat, inetcf);
376 inet_set(pat, WR_YES); /* shut up next time */
379 tcpd_warn("%s: service possibly not wrapped", pat);
380 inet_set(pat, WR_YES);
386 /* check_user - criticize user pattern */
388 static void check_user(pat)
391 if (pat[0] == '@') { /* @netgroup */
392 tcpd_warn("%s: user name begins with \"@\"", pat);
393 } else if (pat[0] == '/') {
394 tcpd_warn("%s: user name begins with \"/\"", pat);
395 } else if (pat[0] == '.') {
396 tcpd_warn("%s: user name begins with dot", pat);
397 } else if (pat[strlen(pat) - 1] == '.') {
398 tcpd_warn("%s: user name ends in dot", pat);
399 } else if (STR_EQ(pat, "ALL") || STR_EQ(pat, unknown)
400 || STR_EQ(pat, "KNOWN")) {
402 } else if (STR_EQ(pat, "FAIL")) { /* obsolete */
403 tcpd_warn("FAIL is no longer recognized");
404 tcpd_warn("(use EXCEPT or DENY instead)");
405 } else if (reserved_name(pat)) {
406 tcpd_warn("%s: user name may be reserved word", pat);
411 static int is_inet6_addr(pat)
414 struct addrinfo hints, *res;
421 if ((ch = pat[len - 1]) != ']')
424 memset(&hints, 0, sizeof(hints));
425 hints.ai_family = AF_INET6;
426 hints.ai_socktype = SOCK_STREAM;
427 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
428 if ((ret = getaddrinfo(pat + 1, NULL, &hints, &res)) == 0)
435 /* check_host - criticize host pattern */
437 static int check_host(pat)
444 struct tcpd_context saved_context;
446 char *wsp = " \t\r\n";
448 if (pat[0] == '@') { /* @netgroup */
450 /* SCO has no *netgrent() support */
457 setnetgrent(pat + 1);
458 if (getnetgrent(&machinep, &userp, &domainp) == 0)
459 tcpd_warn("%s: unknown or empty netgroup", pat + 1);
462 tcpd_warn("netgroup support disabled");
465 } else if (pat[0] == '/') { /* /path/name */
466 if ((fp = fopen(pat, "r")) != 0) {
467 saved_context = tcpd_context;
468 tcpd_context.file = pat;
469 tcpd_context.line = 0;
470 while (fgets(buf, sizeof(buf), fp)) {
472 for (cp = strtok(buf, wsp); cp; cp = strtok((char *) 0, wsp))
475 tcpd_context = saved_context;
477 } else if (errno != ENOENT) {
478 tcpd_warn("open %s: %m", pat);
480 } else if (mask = split_at(pat, '/')) { /* network/netmask */
484 if ((dot_quad_addr(pat) == INADDR_NONE
485 || dot_quad_addr(mask) == INADDR_NONE)
486 && (!is_inet6_addr(pat)
487 || ((mask_len = atoi(mask)) < 0 || mask_len > 128)))
489 if (dot_quad_addr(pat) == INADDR_NONE
490 || dot_quad_addr(mask) == INADDR_NONE)
492 tcpd_warn("%s/%s: bad net/mask pattern", pat, mask);
493 } else if (STR_EQ(pat, "FAIL")) { /* obsolete */
494 tcpd_warn("FAIL is no longer recognized");
495 tcpd_warn("(use EXCEPT or DENY instead)");
496 } else if (reserved_name(pat)) { /* other reserved */
499 } else if (is_inet6_addr(pat)) { /* IPv6 address */
502 } else if (NOT_INADDR(pat)) { /* internet name */
503 if (pat[strlen(pat) - 1] == '.') {
504 tcpd_warn("%s: domain or host name ends in dot", pat);
505 } else if (pat[0] != '.') {
506 addr_count = check_dns(pat);
508 } else { /* numeric form */
509 if (STR_EQ(pat, "0.0.0.0") || STR_EQ(pat, "255.255.255.255")) {
511 } else if (pat[0] == '.') {
512 tcpd_warn("%s: network number begins with dot", pat);
513 } else if (pat[strlen(pat) - 1] != '.') {
520 /* reserved_name - determine if name is reserved */
522 static int reserved_name(pat)
525 return (STR_EQ(pat, unknown)
526 || STR_EQ(pat, "KNOWN")
527 || STR_EQ(pat, paranoid)
528 || STR_EQ(pat, "ALL")
529 || STR_EQ(pat, "LOCAL"));