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.
20 static char sccsid[] = "@(#) tcpdchk.c 1.8 97/02/12 02:13:25";
23 /* System libraries. */
25 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
47 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
51 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
54 /* Application-specific. */
61 * Stolen from hosts_access.c...
63 static char sep[] = ", \t\n";
68 int hosts_access_verbose = 0;
69 char *hosts_allow_table = HOSTS_ALLOW;
70 char *hosts_deny_table = HOSTS_DENY;
71 extern jmp_buf tcpd_buf;
77 static void parse_table();
78 static void print_list();
79 static void check_daemon_list();
80 static void check_client_list();
81 static void check_daemon();
82 static void check_user();
83 static int check_host();
84 static int reserved_name();
92 static int defl_verdict;
94 static int allow_check;
101 struct request_info request;
110 while ((c = getopt(argc, argv, "adi:v")) != EOF) {
116 hosts_allow_table = "hosts.allow";
117 hosts_deny_table = "hosts.deny";
123 hosts_access_verbose++;
134 * When confusion really strikes...
136 if (check_path(REAL_DAEMON_DIR, &st) < 0) {
137 tcpd_warn("REAL_DAEMON_DIR %s: %m", REAL_DAEMON_DIR);
138 } else if (!S_ISDIR(st.st_mode)) {
139 tcpd_warn("REAL_DAEMON_DIR %s is not a directory", REAL_DAEMON_DIR);
143 * Process the inet configuration file (or its moral equivalent). This
144 * information is used later to find references in hosts.allow/deny to
145 * unwrapped services, and other possible problems.
147 inetcf = inet_cfg(inetcf);
148 if (hosts_access_verbose)
149 printf("Using network configuration file: %s\n", inetcf);
152 * These are not run from inetd but may have built-in access control.
154 inet_set("portmap", WR_NOT);
155 inet_set("rpcbind", WR_NOT);
158 * Check accessibility of access control files.
160 (void) check_path(hosts_allow_table, &st);
161 (void) check_path(hosts_deny_table, &st);
164 * Fake up an arbitrary service request.
166 request_init(&request,
167 RQ_DAEMON, "daemon_name",
168 RQ_SERVER_NAME, "server_hostname",
169 RQ_SERVER_ADDR, "server_addr",
170 RQ_USER, "user_name",
171 RQ_CLIENT_NAME, "client_hostname",
172 RQ_CLIENT_ADDR, "client_addr",
177 * Examine all access-control rules.
179 defl_verdict = PERMIT;
180 parse_table(hosts_allow_table, &request);
182 parse_table(hosts_deny_table, &request);
186 /* usage - explain */
190 fprintf(stderr, "usage: %s [-a] [-d] [-i inet_conf] [-v]\n", myname);
191 fprintf(stderr, " -a: report rules with implicit \"ALLOW\" at end\n");
192 fprintf(stderr, " -d: use allow/deny files in current directory\n");
193 fprintf(stderr, " -i: location of inetd.conf file\n");
194 fprintf(stderr, " -v: list all rules\n");
198 /* parse_table - like table_match(), but examines _all_ entries */
200 static void parse_table(table, request)
202 struct request_info *request;
206 char sv_list[BUFLEN]; /* becomes list of daemons */
207 char *cl_list; /* becomes list of requests */
208 char *sh_cmd; /* becomes optional shell command */
211 struct tcpd_context saved_context;
213 saved_context = tcpd_context; /* stupid compilers */
215 if (fp = fopen(table, "r")) {
216 tcpd_context.file = table;
217 tcpd_context.line = 0;
218 while (xgets(sv_list, sizeof(sv_list), fp)) {
219 if (sv_list[strlen(sv_list) - 1] != '\n') {
220 tcpd_warn("missing newline or line too long");
223 if (sv_list[0] == '#' || sv_list[strspn(sv_list, " \t\r\n")] == 0)
225 if ((cl_list = split_at(sv_list, ':')) == 0) {
226 tcpd_warn("missing \":\" separator");
229 sh_cmd = split_at(cl_list, ':');
231 if (hosts_access_verbose)
232 printf("\n>>> Rule %s line %d:\n",
233 tcpd_context.file, tcpd_context.line);
235 if (hosts_access_verbose)
236 print_list("daemons: ", sv_list);
237 check_daemon_list(sv_list);
239 if (hosts_access_verbose)
240 print_list("clients: ", cl_list);
241 check_client_list(cl_list);
243 #ifdef PROCESS_OPTIONS
244 real_verdict = defl_verdict;
246 verdict = setjmp(tcpd_buf);
248 real_verdict = (verdict == AC_PERMIT);
251 process_options(sh_cmd, request);
252 if (dry_run == 1 && real_verdict && allow_check)
253 tcpd_warn("implicit \"allow\" at end of rule");
255 } else if (defl_verdict && allow_check) {
256 tcpd_warn("implicit \"allow\" at end of rule");
258 if (hosts_access_verbose)
259 printf("access: %s\n", real_verdict ? "granted" : "denied");
262 shell_cmd(percent_x(buf, sizeof(buf), sh_cmd, request));
263 if (hosts_access_verbose)
264 printf("access: %s\n", defl_verdict ? "granted" : "denied");
268 } else if (errno != ENOENT) {
269 tcpd_warn("cannot open %s: %m", table);
271 tcpd_context = saved_context;
274 /* print_list - pretty-print a list */
276 static void print_list(title, list)
284 fputs(title, stdout);
287 for (cp = strtok(buf, sep); cp != 0; cp = next) {
289 next = strtok((char *) 0, sep);
296 /* check_daemon_list - criticize daemon list */
298 static void check_daemon_list(list)
308 for (cp = strtok(buf, sep); cp != 0; cp = strtok((char *) 0, sep)) {
309 if (STR_EQ(cp, "EXCEPT")) {
313 if ((host = split_at(cp + 1, '@')) != 0 && check_host(host) > 1) {
314 tcpd_warn("host %s has more than one address", host);
315 tcpd_warn("(consider using an address instead)");
321 tcpd_warn("daemon list is empty or ends in EXCEPT");
324 /* check_client_list - criticize client list */
326 static void check_client_list(list)
336 for (cp = strtok(buf, sep); cp != 0; cp = strtok((char *) 0, sep)) {
337 if (STR_EQ(cp, "EXCEPT")) {
341 if (host = split_at(cp + 1, '@')) { /* user@host */
350 tcpd_warn("client list is empty or ends in EXCEPT");
353 /* check_daemon - criticize daemon pattern */
355 static void check_daemon(pat)
359 tcpd_warn("%s: daemon name begins with \"@\"", pat);
360 } else if (pat[0] == '/') {
361 tcpd_warn("%s: daemon name begins with \"/\"", pat);
362 } else if (pat[0] == '.') {
363 tcpd_warn("%s: daemon name begins with dot", pat);
364 } else if (pat[strlen(pat) - 1] == '.') {
365 tcpd_warn("%s: daemon name ends in dot", pat);
366 } else if (STR_EQ(pat, "ALL") || STR_EQ(pat, unknown)) {
368 } else if (STR_EQ(pat, "FAIL")) { /* obsolete */
369 tcpd_warn("FAIL is no longer recognized");
370 tcpd_warn("(use EXCEPT or DENY instead)");
371 } else if (reserved_name(pat)) {
372 tcpd_warn("%s: daemon name may be reserved word", pat);
374 switch (inet_get(pat)) {
376 tcpd_warn("%s: no such process name in %s", pat, inetcf);
377 inet_set(pat, WR_YES); /* shut up next time */
380 tcpd_warn("%s: service possibly not wrapped", pat);
381 inet_set(pat, WR_YES);
387 /* check_user - criticize user pattern */
389 static void check_user(pat)
392 if (pat[0] == '@') { /* @netgroup */
393 tcpd_warn("%s: user name begins with \"@\"", pat);
394 } else if (pat[0] == '/') {
395 tcpd_warn("%s: user name begins with \"/\"", pat);
396 } else if (pat[0] == '.') {
397 tcpd_warn("%s: user name begins with dot", pat);
398 } else if (pat[strlen(pat) - 1] == '.') {
399 tcpd_warn("%s: user name ends in dot", pat);
400 } else if (STR_EQ(pat, "ALL") || STR_EQ(pat, unknown)
401 || STR_EQ(pat, "KNOWN")) {
403 } else if (STR_EQ(pat, "FAIL")) { /* obsolete */
404 tcpd_warn("FAIL is no longer recognized");
405 tcpd_warn("(use EXCEPT or DENY instead)");
406 } else if (reserved_name(pat)) {
407 tcpd_warn("%s: user name may be reserved word", pat);
412 static int is_inet6_addr(pat)
415 struct addrinfo hints, *res;
422 if ((ch = pat[len - 1]) != ']')
425 memset(&hints, 0, sizeof(hints));
426 hints.ai_family = AF_INET6;
427 hints.ai_socktype = SOCK_STREAM;
428 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
429 if ((ret = getaddrinfo(pat + 1, NULL, &hints, &res)) == 0)
436 /* check_host - criticize host pattern */
438 static int check_host(pat)
445 struct tcpd_context saved_context;
447 char *wsp = " \t\r\n";
449 if (pat[0] == '@') { /* @netgroup */
451 /* SCO has no *netgrent() support */
458 setnetgrent(pat + 1);
459 if (getnetgrent(&machinep, &userp, &domainp) == 0)
460 tcpd_warn("%s: unknown or empty netgroup", pat + 1);
463 tcpd_warn("netgroup support disabled");
466 } else if (pat[0] == '/') { /* /path/name */
467 if ((fp = fopen(pat, "r")) != 0) {
468 saved_context = tcpd_context;
469 tcpd_context.file = pat;
470 tcpd_context.line = 0;
471 while (fgets(buf, sizeof(buf), fp)) {
473 for (cp = strtok(buf, wsp); cp; cp = strtok((char *) 0, wsp))
476 tcpd_context = saved_context;
478 } else if (errno != ENOENT) {
479 tcpd_warn("open %s: %m", pat);
481 } else if (mask = split_at(pat, '/')) { /* network/netmask */
485 if ((dot_quad_addr(pat) == INADDR_NONE
486 || dot_quad_addr(mask) == INADDR_NONE)
487 && (!is_inet6_addr(pat)
488 || ((mask_len = atoi(mask)) < 0 || mask_len > 128)))
490 if (dot_quad_addr(pat) == INADDR_NONE
491 || dot_quad_addr(mask) == INADDR_NONE)
493 tcpd_warn("%s/%s: bad net/mask pattern", pat, mask);
494 } else if (STR_EQ(pat, "FAIL")) { /* obsolete */
495 tcpd_warn("FAIL is no longer recognized");
496 tcpd_warn("(use EXCEPT or DENY instead)");
497 } else if (reserved_name(pat)) { /* other reserved */
500 } else if (is_inet6_addr(pat)) { /* IPv6 address */
503 } else if (NOT_INADDR(pat)) { /* internet name */
504 if (pat[strlen(pat) - 1] == '.') {
505 tcpd_warn("%s: domain or host name ends in dot", pat);
506 } else if (pat[0] != '.') {
507 addr_count = check_dns(pat);
509 } else { /* numeric form */
510 if (STR_EQ(pat, "0.0.0.0") || STR_EQ(pat, "255.255.255.255")) {
512 } else if (pat[0] == '.') {
513 tcpd_warn("%s: network number begins with dot", pat);
514 } else if (pat[strlen(pat) - 1] != '.') {
521 /* reserved_name - determine if name is reserved */
523 static int reserved_name(pat)
526 return (STR_EQ(pat, unknown)
527 || STR_EQ(pat, "KNOWN")
528 || STR_EQ(pat, paranoid)
529 || STR_EQ(pat, "ALL")
530 || STR_EQ(pat, "LOCAL"));