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>
45 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
49 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
52 /* Application-specific. */
59 * Stolen from hosts_access.c...
61 static char sep
[] = ", \t\n";
66 int hosts_access_verbose
= 0;
67 char *hosts_allow_table
= HOSTS_ALLOW
;
68 char *hosts_deny_table
= HOSTS_DENY
;
69 extern jmp_buf tcpd_buf
;
75 static void parse_table();
76 static void print_list();
77 static void check_daemon_list();
78 static void check_client_list();
79 static void check_daemon();
80 static void check_user();
81 static int check_host();
82 static int reserved_name();
90 static int defl_verdict
;
92 static int allow_check
;
99 struct request_info request
;
108 while ((c
= getopt(argc
, argv
, "adi:v")) != EOF
) {
114 hosts_allow_table
= "hosts.allow";
115 hosts_deny_table
= "hosts.deny";
121 hosts_access_verbose
++;
132 * When confusion really strikes...
134 if (check_path(REAL_DAEMON_DIR
, &st
) < 0) {
135 tcpd_warn("REAL_DAEMON_DIR %s: %m", REAL_DAEMON_DIR
);
136 } else if (!S_ISDIR(st
.st_mode
)) {
137 tcpd_warn("REAL_DAEMON_DIR %s is not a directory", REAL_DAEMON_DIR
);
141 * Process the inet configuration file (or its moral equivalent). This
142 * information is used later to find references in hosts.allow/deny to
143 * unwrapped services, and other possible problems.
145 inetcf
= inet_cfg(inetcf
);
146 if (hosts_access_verbose
)
147 printf("Using network configuration file: %s\n", inetcf
);
150 * These are not run from inetd but may have built-in access control.
152 inet_set("portmap", WR_NOT
);
153 inet_set("rpcbind", WR_NOT
);
156 * Check accessibility of access control files.
158 (void) check_path(hosts_allow_table
, &st
);
159 (void) check_path(hosts_deny_table
, &st
);
162 * Fake up an arbitrary service request.
164 request_init(&request
,
165 RQ_DAEMON
, "daemon_name",
166 RQ_SERVER_NAME
, "server_hostname",
167 RQ_SERVER_ADDR
, "server_addr",
168 RQ_USER
, "user_name",
169 RQ_CLIENT_NAME
, "client_hostname",
170 RQ_CLIENT_ADDR
, "client_addr",
175 * Examine all access-control rules.
177 defl_verdict
= PERMIT
;
178 parse_table(hosts_allow_table
, &request
);
180 parse_table(hosts_deny_table
, &request
);
184 /* usage - explain */
188 fprintf(stderr
, "usage: %s [-a] [-d] [-i inet_conf] [-v]\n", myname
);
189 fprintf(stderr
, " -a: report rules with implicit \"ALLOW\" at end\n");
190 fprintf(stderr
, " -d: use allow/deny files in current directory\n");
191 fprintf(stderr
, " -i: location of inetd.conf file\n");
192 fprintf(stderr
, " -v: list all rules\n");
196 /* parse_table - like table_match(), but examines _all_ entries */
198 static void parse_table(table
, request
)
200 struct request_info
*request
;
204 char sv_list
[BUFLEN
]; /* becomes list of daemons */
205 char *cl_list
; /* becomes list of requests */
206 char *sh_cmd
; /* becomes optional shell command */
209 struct tcpd_context saved_context
;
211 saved_context
= tcpd_context
; /* stupid compilers */
213 if (fp
= fopen(table
, "r")) {
214 tcpd_context
.file
= table
;
215 tcpd_context
.line
= 0;
216 while (xgets(sv_list
, sizeof(sv_list
), fp
)) {
217 if (sv_list
[strlen(sv_list
) - 1] != '\n') {
218 tcpd_warn("missing newline or line too long");
221 if (sv_list
[0] == '#' || sv_list
[strspn(sv_list
, " \t\r\n")] == 0)
223 if ((cl_list
= split_at(sv_list
, ':')) == 0) {
224 tcpd_warn("missing \":\" separator");
227 sh_cmd
= split_at(cl_list
, ':');
229 if (hosts_access_verbose
)
230 printf("\n>>> Rule %s line %d:\n",
231 tcpd_context
.file
, tcpd_context
.line
);
233 if (hosts_access_verbose
)
234 print_list("daemons: ", sv_list
);
235 check_daemon_list(sv_list
);
237 if (hosts_access_verbose
)
238 print_list("clients: ", cl_list
);
239 check_client_list(cl_list
);
241 #ifdef PROCESS_OPTIONS
242 real_verdict
= defl_verdict
;
244 verdict
= setjmp(tcpd_buf
);
246 real_verdict
= (verdict
== AC_PERMIT
);
249 process_options(sh_cmd
, request
);
250 if (dry_run
== 1 && real_verdict
&& allow_check
)
251 tcpd_warn("implicit \"allow\" at end of rule");
253 } else if (defl_verdict
&& allow_check
) {
254 tcpd_warn("implicit \"allow\" at end of rule");
256 if (hosts_access_verbose
)
257 printf("access: %s\n", real_verdict
? "granted" : "denied");
260 shell_cmd(percent_x(buf
, sizeof(buf
), sh_cmd
, request
));
261 if (hosts_access_verbose
)
262 printf("access: %s\n", defl_verdict
? "granted" : "denied");
266 } else if (errno
!= ENOENT
) {
267 tcpd_warn("cannot open %s: %m", table
);
269 tcpd_context
= saved_context
;
272 /* print_list - pretty-print a list */
274 static void print_list(title
, list
)
282 fputs(title
, stdout
);
285 for (cp
= strtok(buf
, sep
); cp
!= 0; cp
= next
) {
287 next
= strtok((char *) 0, sep
);
294 /* check_daemon_list - criticize daemon list */
296 static void check_daemon_list(list
)
306 for (cp
= strtok(buf
, sep
); cp
!= 0; cp
= strtok((char *) 0, sep
)) {
307 if (STR_EQ(cp
, "EXCEPT")) {
311 if ((host
= split_at(cp
+ 1, '@')) != 0 && check_host(host
) > 1) {
312 tcpd_warn("host %s has more than one address", host
);
313 tcpd_warn("(consider using an address instead)");
319 tcpd_warn("daemon list is empty or ends in EXCEPT");
322 /* check_client_list - criticize client list */
324 static void check_client_list(list
)
334 for (cp
= strtok(buf
, sep
); cp
!= 0; cp
= strtok((char *) 0, sep
)) {
335 if (STR_EQ(cp
, "EXCEPT")) {
339 if (host
= split_at(cp
+ 1, '@')) { /* user@host */
348 tcpd_warn("client list is empty or ends in EXCEPT");
351 /* check_daemon - criticize daemon pattern */
353 static void check_daemon(pat
)
357 tcpd_warn("%s: daemon name begins with \"@\"", pat
);
358 } else if (pat
[0] == '/') {
359 tcpd_warn("%s: daemon name begins with \"/\"", pat
);
360 } else if (pat
[0] == '.') {
361 tcpd_warn("%s: daemon name begins with dot", pat
);
362 } else if (pat
[strlen(pat
) - 1] == '.') {
363 tcpd_warn("%s: daemon name ends in dot", pat
);
364 } else if (STR_EQ(pat
, "ALL") || STR_EQ(pat
, unknown
)) {
366 } else if (STR_EQ(pat
, "FAIL")) { /* obsolete */
367 tcpd_warn("FAIL is no longer recognized");
368 tcpd_warn("(use EXCEPT or DENY instead)");
369 } else if (reserved_name(pat
)) {
370 tcpd_warn("%s: daemon name may be reserved word", pat
);
372 switch (inet_get(pat
)) {
374 tcpd_warn("%s: no such process name in %s", pat
, inetcf
);
375 inet_set(pat
, WR_YES
); /* shut up next time */
378 tcpd_warn("%s: service possibly not wrapped", pat
);
379 inet_set(pat
, WR_YES
);
385 /* check_user - criticize user pattern */
387 static void check_user(pat
)
390 if (pat
[0] == '@') { /* @netgroup */
391 tcpd_warn("%s: user name begins with \"@\"", pat
);
392 } else if (pat
[0] == '/') {
393 tcpd_warn("%s: user name begins with \"/\"", pat
);
394 } else if (pat
[0] == '.') {
395 tcpd_warn("%s: user name begins with dot", pat
);
396 } else if (pat
[strlen(pat
) - 1] == '.') {
397 tcpd_warn("%s: user name ends in dot", pat
);
398 } else if (STR_EQ(pat
, "ALL") || STR_EQ(pat
, unknown
)
399 || STR_EQ(pat
, "KNOWN")) {
401 } else if (STR_EQ(pat
, "FAIL")) { /* obsolete */
402 tcpd_warn("FAIL is no longer recognized");
403 tcpd_warn("(use EXCEPT or DENY instead)");
404 } else if (reserved_name(pat
)) {
405 tcpd_warn("%s: user name may be reserved word", pat
);
410 static int is_inet6_addr(pat
)
413 struct addrinfo hints
, *res
;
420 if ((ch
= pat
[len
- 1]) != ']')
423 memset(&hints
, 0, sizeof(hints
));
424 hints
.ai_family
= AF_INET6
;
425 hints
.ai_socktype
= SOCK_STREAM
;
426 hints
.ai_flags
= AI_PASSIVE
| AI_NUMERICHOST
;
427 if ((ret
= getaddrinfo(pat
+ 1, NULL
, &hints
, &res
)) == 0)
434 /* check_host - criticize host pattern */
436 static int check_host(pat
)
443 struct tcpd_context saved_context
;
445 char *wsp
= " \t\r\n";
447 if (pat
[0] == '@') { /* @netgroup */
449 /* SCO has no *netgrent() support */
456 setnetgrent(pat
+ 1);
457 if (getnetgrent(&machinep
, &userp
, &domainp
) == 0)
458 tcpd_warn("%s: unknown or empty netgroup", pat
+ 1);
461 tcpd_warn("netgroup support disabled");
464 } else if (pat
[0] == '/') { /* /path/name */
465 if ((fp
= fopen(pat
, "r")) != 0) {
466 saved_context
= tcpd_context
;
467 tcpd_context
.file
= pat
;
468 tcpd_context
.line
= 0;
469 while (fgets(buf
, sizeof(buf
), fp
)) {
471 for (cp
= strtok(buf
, wsp
); cp
; cp
= strtok((char *) 0, wsp
))
474 tcpd_context
= saved_context
;
476 } else if (errno
!= ENOENT
) {
477 tcpd_warn("open %s: %m", pat
);
479 } else if (mask
= split_at(pat
, '/')) { /* network/netmask */
483 if ((dot_quad_addr(pat
) == INADDR_NONE
484 || dot_quad_addr(mask
) == INADDR_NONE
)
485 && (!is_inet6_addr(pat
)
486 || ((mask_len
= atoi(mask
)) < 0 || mask_len
> 128)))
488 if (dot_quad_addr(pat
) == INADDR_NONE
489 || dot_quad_addr(mask
) == INADDR_NONE
)
491 tcpd_warn("%s/%s: bad net/mask pattern", pat
, mask
);
492 } else if (STR_EQ(pat
, "FAIL")) { /* obsolete */
493 tcpd_warn("FAIL is no longer recognized");
494 tcpd_warn("(use EXCEPT or DENY instead)");
495 } else if (reserved_name(pat
)) { /* other reserved */
498 } else if (is_inet6_addr(pat
)) { /* IPv6 address */
501 } else if (NOT_INADDR(pat
)) { /* internet name */
502 if (pat
[strlen(pat
) - 1] == '.') {
503 tcpd_warn("%s: domain or host name ends in dot", pat
);
504 } else if (pat
[0] != '.') {
505 addr_count
= check_dns(pat
);
507 } else { /* numeric form */
508 if (STR_EQ(pat
, "0.0.0.0") || STR_EQ(pat
, "255.255.255.255")) {
510 } else if (pat
[0] == '.') {
511 tcpd_warn("%s: network number begins with dot", pat
);
512 } else if (pat
[strlen(pat
) - 1] != '.') {
519 /* reserved_name - determine if name is reserved */
521 static int reserved_name(pat
)
524 return (STR_EQ(pat
, unknown
)
525 || STR_EQ(pat
, "KNOWN")
526 || STR_EQ(pat
, paranoid
)
527 || STR_EQ(pat
, "ALL")
528 || STR_EQ(pat
, "LOCAL"));