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>
42 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
46 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
49 /* Application-specific. */
56 * Stolen from hosts_access.c...
58 static char sep[] = ", \t\n";
63 int hosts_access_verbose = 0;
64 char *hosts_allow_table = HOSTS_ALLOW;
65 char *hosts_deny_table = HOSTS_DENY;
66 extern jmp_buf tcpd_buf;
71 static void usage(void);
72 static void parse_table(char *table, struct request_info *request);
73 static void print_list(char *title, char *list);
74 static void check_daemon_list(char *list);
75 static void check_client_list(char *list);
76 static void check_daemon(char *pat);
77 static void check_user(char *pat);
78 static int check_host(char *pat);
79 static int reserved_name(char *pat);
87 static int defl_verdict;
89 static int allow_check;
96 struct request_info request;
105 while ((c = getopt(argc, argv, "adi:v")) != EOF) {
111 hosts_allow_table = "hosts.allow";
112 hosts_deny_table = "hosts.deny";
118 hosts_access_verbose++;
129 * When confusion really strikes...
131 if (check_path(REAL_DAEMON_DIR, &st) < 0) {
132 tcpd_warn("REAL_DAEMON_DIR %s: %m", REAL_DAEMON_DIR);
133 } else if (!S_ISDIR(st.st_mode)) {
134 tcpd_warn("REAL_DAEMON_DIR %s is not a directory", REAL_DAEMON_DIR);
138 * Process the inet configuration file (or its moral equivalent). This
139 * information is used later to find references in hosts.allow/deny to
140 * unwrapped services, and other possible problems.
142 inetcf = inet_cfg(inetcf);
143 if (hosts_access_verbose)
144 printf("Using network configuration file: %s\n", inetcf);
147 * These are not run from inetd but may have built-in access control.
149 inet_set("portmap", WR_NOT);
150 inet_set("rpcbind", WR_NOT);
153 * Check accessibility of access control files.
155 (void) check_path(hosts_allow_table, &st);
156 (void) check_path(hosts_deny_table, &st);
159 * Fake up an arbitrary service request.
161 request_init(&request,
162 RQ_DAEMON, "daemon_name",
163 RQ_SERVER_NAME, "server_hostname",
164 RQ_SERVER_ADDR, "server_addr",
165 RQ_USER, "user_name",
166 RQ_CLIENT_NAME, "client_hostname",
167 RQ_CLIENT_ADDR, "client_addr",
172 * Examine all access-control rules.
174 defl_verdict = PERMIT;
175 parse_table(hosts_allow_table, &request);
177 parse_table(hosts_deny_table, &request);
181 /* usage - explain */
183 static void usage(void)
185 fprintf(stderr, "usage: %s [-a] [-d] [-i inet_conf] [-v]\n", myname);
186 fprintf(stderr, " -a: report rules with implicit \"ALLOW\" at end\n");
187 fprintf(stderr, " -d: use allow/deny files in current directory\n");
188 fprintf(stderr, " -i: location of inetd.conf file\n");
189 fprintf(stderr, " -v: list all rules\n");
193 /* parse_table - like table_match(), but examines _all_ entries */
195 static void parse_table(table, request)
197 struct request_info *request;
201 char sv_list[BUFLEN]; /* becomes list of daemons */
202 char *cl_list; /* becomes list of requests */
203 char *sh_cmd; /* becomes optional shell command */
206 struct tcpd_context saved_context;
208 saved_context = tcpd_context; /* stupid compilers */
210 if (fp = fopen(table, "r")) {
211 tcpd_context.file = table;
212 tcpd_context.line = 0;
213 while (xgets(sv_list, sizeof(sv_list), fp)) {
214 if (sv_list[strlen(sv_list) - 1] != '\n') {
215 tcpd_warn("missing newline or line too long");
218 if (sv_list[0] == '#' || sv_list[strspn(sv_list, " \t\r\n")] == 0)
220 if ((cl_list = split_at(sv_list, ':')) == 0) {
221 tcpd_warn("missing \":\" separator");
224 sh_cmd = split_at(cl_list, ':');
226 if (hosts_access_verbose)
227 printf("\n>>> Rule %s line %d:\n",
228 tcpd_context.file, tcpd_context.line);
230 if (hosts_access_verbose)
231 print_list("daemons: ", sv_list);
232 check_daemon_list(sv_list);
234 if (hosts_access_verbose)
235 print_list("clients: ", cl_list);
236 check_client_list(cl_list);
238 #ifdef PROCESS_OPTIONS
239 real_verdict = defl_verdict;
241 verdict = setjmp(tcpd_buf);
243 real_verdict = (verdict == AC_PERMIT);
246 process_options(sh_cmd, request);
247 if (dry_run == 1 && real_verdict && allow_check)
248 tcpd_warn("implicit \"allow\" at end of rule");
250 } else if (defl_verdict && allow_check) {
251 tcpd_warn("implicit \"allow\" at end of rule");
253 if (hosts_access_verbose)
254 printf("access: %s\n", real_verdict ? "granted" : "denied");
257 shell_cmd(percent_x(buf, sizeof(buf), sh_cmd, request));
258 if (hosts_access_verbose)
259 printf("access: %s\n", defl_verdict ? "granted" : "denied");
263 } else if (errno != ENOENT) {
264 tcpd_warn("cannot open %s: %m", table);
266 tcpd_context = saved_context;
269 /* print_list - pretty-print a list */
271 static void print_list(title, list)
279 fputs(title, stdout);
282 for (cp = strtok(buf, sep); cp != 0; cp = next) {
284 next = strtok((char *) 0, sep);
291 /* check_daemon_list - criticize daemon list */
293 static void check_daemon_list(list)
303 for (cp = strtok(buf, sep); cp != 0; cp = strtok((char *) 0, sep)) {
304 if (STR_EQ(cp, "EXCEPT")) {
308 if ((host = split_at(cp + 1, '@')) != 0 && check_host(host) > 1) {
309 tcpd_warn("host %s has more than one address", host);
310 tcpd_warn("(consider using an address instead)");
316 tcpd_warn("daemon list is empty or ends in EXCEPT");
319 /* check_client_list - criticize client list */
321 static void check_client_list(list)
331 for (cp = strtok(buf, sep); cp != 0; cp = strtok((char *) 0, sep)) {
332 if (STR_EQ(cp, "EXCEPT")) {
336 if (host = split_at(cp + 1, '@')) { /* user@host */
345 tcpd_warn("client list is empty or ends in EXCEPT");
348 /* check_daemon - criticize daemon pattern */
350 static void check_daemon(pat)
354 tcpd_warn("%s: daemon name begins with \"@\"", pat);
355 } else if (pat[0] == '/') {
356 tcpd_warn("%s: daemon name begins with \"/\"", pat);
357 } else if (pat[0] == '.') {
358 tcpd_warn("%s: daemon name begins with dot", pat);
359 } else if (pat[strlen(pat) - 1] == '.') {
360 tcpd_warn("%s: daemon name ends in dot", pat);
361 } else if (STR_EQ(pat, "ALL") || STR_EQ(pat, unknown)) {
363 } else if (STR_EQ(pat, "FAIL")) { /* obsolete */
364 tcpd_warn("FAIL is no longer recognized");
365 tcpd_warn("(use EXCEPT or DENY instead)");
366 } else if (reserved_name(pat)) {
367 tcpd_warn("%s: daemon name may be reserved word", pat);
369 switch (inet_get(pat)) {
371 tcpd_warn("%s: no such process name in %s", pat, inetcf);
372 inet_set(pat, WR_YES); /* shut up next time */
375 tcpd_warn("%s: service possibly not wrapped", pat);
376 inet_set(pat, WR_YES);
382 /* check_user - criticize user pattern */
384 static void check_user(pat)
387 if (pat[0] == '@') { /* @netgroup */
388 tcpd_warn("%s: user name begins with \"@\"", pat);
389 } else if (pat[0] == '/') {
390 tcpd_warn("%s: user name begins with \"/\"", pat);
391 } else if (pat[0] == '.') {
392 tcpd_warn("%s: user name begins with dot", pat);
393 } else if (pat[strlen(pat) - 1] == '.') {
394 tcpd_warn("%s: user name ends in dot", pat);
395 } else if (STR_EQ(pat, "ALL") || STR_EQ(pat, unknown)
396 || STR_EQ(pat, "KNOWN")) {
398 } else if (STR_EQ(pat, "FAIL")) { /* obsolete */
399 tcpd_warn("FAIL is no longer recognized");
400 tcpd_warn("(use EXCEPT or DENY instead)");
401 } else if (reserved_name(pat)) {
402 tcpd_warn("%s: user name may be reserved word", pat);
407 static int is_inet6_addr(pat)
410 struct addrinfo hints, *res;
417 if ((ch = pat[len - 1]) != ']')
420 memset(&hints, 0, sizeof(hints));
421 hints.ai_family = AF_INET6;
422 hints.ai_socktype = SOCK_STREAM;
423 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
424 if ((ret = getaddrinfo(pat + 1, NULL, &hints, &res)) == 0)
431 /* check_host - criticize host pattern */
433 static int check_host(pat)
440 struct tcpd_context saved_context;
442 char *wsp = " \t\r\n";
444 if (pat[0] == '@') { /* @netgroup */
446 /* SCO has no *netgrent() support */
453 setnetgrent(pat + 1);
454 if (getnetgrent(&machinep, &userp, &domainp) == 0)
455 tcpd_warn("%s: unknown or empty netgroup", pat + 1);
458 tcpd_warn("netgroup support disabled");
461 } else if (pat[0] == '/') { /* /path/name */
462 if ((fp = fopen(pat, "r")) != 0) {
463 saved_context = tcpd_context;
464 tcpd_context.file = pat;
465 tcpd_context.line = 0;
466 while (fgets(buf, sizeof(buf), fp)) {
468 for (cp = strtok(buf, wsp); cp; cp = strtok((char *) 0, wsp))
471 tcpd_context = saved_context;
473 } else if (errno != ENOENT) {
474 tcpd_warn("open %s: %m", pat);
476 } else if (mask = split_at(pat, '/')) { /* network/netmask */
480 if ((dot_quad_addr(pat) == INADDR_NONE
481 || dot_quad_addr(mask) == INADDR_NONE)
482 && (!is_inet6_addr(pat)
483 || ((mask_len = atoi(mask)) < 0 || mask_len > 128)))
485 if (dot_quad_addr(pat) == INADDR_NONE
486 || dot_quad_addr(mask) == INADDR_NONE)
488 tcpd_warn("%s/%s: bad net/mask pattern", pat, mask);
489 } else if (STR_EQ(pat, "FAIL")) { /* obsolete */
490 tcpd_warn("FAIL is no longer recognized");
491 tcpd_warn("(use EXCEPT or DENY instead)");
492 } else if (reserved_name(pat)) { /* other reserved */
495 } else if (is_inet6_addr(pat)) { /* IPv6 address */
498 } else if (NOT_INADDR(pat)) { /* internet name */
499 if (pat[strlen(pat) - 1] == '.') {
500 tcpd_warn("%s: domain or host name ends in dot", pat);
501 } else if (pat[0] != '.') {
502 addr_count = check_dns(pat);
504 } else { /* numeric form */
505 if (STR_EQ(pat, "0.0.0.0") || STR_EQ(pat, "255.255.255.255")) {
507 } else if (pat[0] == '.') {
508 tcpd_warn("%s: network number begins with dot", pat);
509 } else if (pat[strlen(pat) - 1] != '.') {
516 /* reserved_name - determine if name is reserved */
518 static int reserved_name(pat)
521 return (STR_EQ(pat, unknown)
522 || STR_EQ(pat, "KNOWN")
523 || STR_EQ(pat, paranoid)
524 || STR_EQ(pat, "ALL")
525 || STR_EQ(pat, "LOCAL"));