1 /* $NetBSD: tcpdchk.c,v 1.10 2002/06/06 21:28:51 itojun Exp $ */
4 * tcpdchk - examine all tcpd access control rules and inetd.conf entries
6 * Usage: tcpdchk [-a] [-d] [-i inet_conf] [-v]
8 * -a: complain about implicit "allow" at end of rule.
10 * -d: rules in current directory.
12 * -i: location of inetd.conf file.
16 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
19 #include <sys/cdefs.h>
22 static char sccsid
[] = "@(#) tcpdchk.c 1.8 97/02/12 02:13:25";
24 __RCSID("$NetBSD: tcpdchk.c,v 1.10 2002/06/06 21:28:51 itojun Exp $");
28 /* System libraries. */
30 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
44 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
48 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
51 /* Application-specific. */
58 /* SCO has no *netgrent() support */
61 # include <netgroup.h>
66 * Stolen from hosts_access.c...
68 static char sep
[] = ", \t\n";
73 int hosts_access_verbose
= 0;
74 char *hosts_allow_table
= HOSTS_ALLOW
;
75 char *hosts_deny_table
= HOSTS_DENY
;
76 extern jmp_buf tcpd_buf
;
81 static void usage
__P((void));
82 static void parse_table
__P((char *, struct request_info
*));
83 static void print_list
__P((char *, char *));
84 static void check_daemon_list
__P((char *));
85 static void check_client_list
__P((char *));
86 static void check_daemon
__P((char *));
87 static void check_user
__P((char *));
89 static int check_inet_addr
__P((char *));
91 static int check_host
__P((char *));
92 static int reserved_name
__P((char *));
94 int main
__P((int, char **));
102 static int defl_verdict
;
104 static int allow_check
;
111 struct request_info request
;
120 while ((c
= getopt(argc
, argv
, "adi:v")) != -1) {
126 hosts_allow_table
= "hosts.allow";
127 hosts_deny_table
= "hosts.deny";
133 hosts_access_verbose
++;
144 * When confusion really strikes...
146 if (check_path(REAL_DAEMON_DIR
, &st
) < 0) {
147 tcpd_warn("REAL_DAEMON_DIR %s: %m", REAL_DAEMON_DIR
);
148 } else if (!S_ISDIR(st
.st_mode
)) {
149 tcpd_warn("REAL_DAEMON_DIR %s is not a directory", REAL_DAEMON_DIR
);
153 * Process the inet configuration file (or its moral equivalent). This
154 * information is used later to find references in hosts.allow/deny to
155 * unwrapped services, and other possible problems.
157 inetcf
= inet_cfg(inetcf
);
158 if (hosts_access_verbose
)
159 printf("Using network configuration file: %s\n", inetcf
);
162 * These are not run from inetd but may have built-in access control.
164 inet_set("portmap", WR_NOT
);
165 inet_set("rpcbind", WR_NOT
);
168 * Check accessibility of access control files.
170 (void) check_path(hosts_allow_table
, &st
);
171 (void) check_path(hosts_deny_table
, &st
);
174 * Fake up an arbitrary service request.
176 request_init(&request
,
177 RQ_DAEMON
, "daemon_name",
178 RQ_SERVER_NAME
, "server_hostname",
179 RQ_SERVER_ADDR
, "server_addr",
180 RQ_USER
, "user_name",
181 RQ_CLIENT_NAME
, "client_hostname",
182 RQ_CLIENT_ADDR
, "client_addr",
187 * Examine all access-control rules.
189 defl_verdict
= PERMIT
;
190 parse_table(hosts_allow_table
, &request
);
192 parse_table(hosts_deny_table
, &request
);
196 /* usage - explain */
200 fprintf(stderr
, "usage: %s [-a] [-d] [-i inet_conf] [-v]\n", myname
);
201 fprintf(stderr
, " -a: report rules with implicit \"ALLOW\" at end\n");
202 fprintf(stderr
, " -d: use allow/deny files in current directory\n");
203 fprintf(stderr
, " -i: location of inetd.conf file\n");
204 fprintf(stderr
, " -v: list all rules\n");
208 /* parse_table - like table_match(), but examines _all_ entries */
210 static void parse_table(table
, request
)
212 struct request_info
*request
;
216 char sv_list
[BUFLEN
]; /* becomes list of daemons */
217 char *cl_list
; /* becomes list of requests */
218 char *sh_cmd
; /* becomes optional shell command */
220 struct tcpd_context saved_context
;
222 /* XXX hack to avoid gcc warnings */
223 (void) &real_verdict
;
224 (void) &saved_context
;
227 saved_context
= tcpd_context
; /* stupid compilers */
229 if ((fp
= fopen(table
, "r")) != NULL
) {
230 tcpd_context
.file
= table
;
231 tcpd_context
.line
= 0;
232 while (xgets(sv_list
, sizeof(sv_list
), fp
)) {
233 if (sv_list
[strlen(sv_list
) - 1] != '\n') {
234 tcpd_warn("missing newline or line too long");
237 if (sv_list
[0] == '#' || sv_list
[strspn(sv_list
, " \t\r\n")] == 0)
239 if ((cl_list
= split_at(sv_list
, ':')) == 0) {
240 tcpd_warn("missing \":\" separator");
243 sh_cmd
= split_at(cl_list
, ':');
245 if (hosts_access_verbose
)
246 printf("\n>>> Rule %s line %d:\n",
247 tcpd_context
.file
, tcpd_context
.line
);
249 if (hosts_access_verbose
)
250 print_list("daemons: ", sv_list
);
251 check_daemon_list(sv_list
);
253 if (hosts_access_verbose
)
254 print_list("clients: ", cl_list
);
255 check_client_list(cl_list
);
257 #ifdef PROCESS_OPTIONS
258 real_verdict
= defl_verdict
;
260 verdict
= setjmp(tcpd_buf
);
262 real_verdict
= (verdict
== AC_PERMIT
);
265 process_options(sh_cmd
, request
);
266 if (dry_run
== 1 && real_verdict
&& allow_check
)
267 tcpd_warn("implicit \"allow\" at end of rule");
269 } else if (defl_verdict
&& allow_check
) {
270 tcpd_warn("implicit \"allow\" at end of rule");
272 if (hosts_access_verbose
)
273 printf("access: %s\n", real_verdict
? "granted" : "denied");
276 shell_cmd(percent_x(buf
, sizeof(buf
), sh_cmd
, request
));
277 if (hosts_access_verbose
)
278 printf("access: %s\n", defl_verdict
? "granted" : "denied");
282 } else if (errno
!= ENOENT
) {
283 tcpd_warn("cannot open %s: %m", table
);
285 tcpd_context
= saved_context
;
288 /* print_list - pretty-print a list */
290 static void print_list(title
, list
)
298 fputs(title
, stdout
);
299 strlcpy(buf
, list
, sizeof(buf
));
301 for (cp
= strtok(buf
, sep
); cp
!= 0; cp
= next
) {
303 next
= strtok((char *) 0, sep
);
310 /* check_daemon_list - criticize daemon list */
312 static void check_daemon_list(list
)
320 strlcpy(buf
, list
, sizeof(buf
));
322 for (cp
= strtok(buf
, sep
); cp
!= 0; cp
= strtok((char *) 0, sep
)) {
323 if (STR_EQ(cp
, "EXCEPT")) {
327 if ((host
= split_at(cp
+ 1, '@')) != 0 && check_host(host
) > 1) {
328 tcpd_warn("host %s has more than one address", host
);
329 tcpd_warn("(consider using an address instead)");
335 tcpd_warn("daemon list is empty or ends in EXCEPT");
338 /* check_client_list - criticize client list */
340 static void check_client_list(list
)
351 strlcpy(buf
, list
, sizeof(buf
));
353 for (cp
= strtok(buf
, sep
); cp
!= 0; cp
= strtok((char *) 0, sep
)) {
356 if (cp
[0] == '[' && cp
[l
- 1] == ']') {
361 if (STR_EQ(cp
, "EXCEPT")) {
365 if ((host
= split_at(cp
+ 1, '@')) != NULL
) { /* user@host */
374 tcpd_warn("client list is empty or ends in EXCEPT");
377 /* check_daemon - criticize daemon pattern */
379 static void check_daemon(pat
)
383 tcpd_warn("%s: daemon name begins with \"@\"", pat
);
384 } else if (pat
[0] == '.') {
385 tcpd_warn("%s: daemon name begins with dot", pat
);
386 } else if (pat
[strlen(pat
) - 1] == '.') {
387 tcpd_warn("%s: daemon name ends in dot", pat
);
388 } else if (STR_EQ(pat
, "ALL") || STR_EQ(pat
, unknown
)) {
390 } else if (STR_EQ(pat
, "FAIL")) { /* obsolete */
391 tcpd_warn("FAIL is no longer recognized");
392 tcpd_warn("(use EXCEPT or DENY instead)");
393 } else if (reserved_name(pat
)) {
394 tcpd_warn("%s: daemon name may be reserved word", pat
);
396 switch (inet_get(pat
)) {
398 tcpd_warn("%s: no such process name in %s", pat
, inetcf
);
399 inet_set(pat
, WR_YES
); /* shut up next time */
402 tcpd_warn("%s: service possibly not wrapped", pat
);
403 inet_set(pat
, WR_YES
);
409 /* check_user - criticize user pattern */
411 static void check_user(pat
)
414 if (pat
[0] == '@') { /* @netgroup */
415 tcpd_warn("%s: user name begins with \"@\"", pat
);
416 } else if (pat
[0] == '.') {
417 tcpd_warn("%s: user name begins with dot", pat
);
418 } else if (pat
[strlen(pat
) - 1] == '.') {
419 tcpd_warn("%s: user name ends in dot", pat
);
420 } else if (STR_EQ(pat
, "ALL") || STR_EQ(pat
, unknown
)
421 || STR_EQ(pat
, "KNOWN")) {
423 } else if (STR_EQ(pat
, "FAIL")) { /* obsolete */
424 tcpd_warn("FAIL is no longer recognized");
425 tcpd_warn("(use EXCEPT or DENY instead)");
426 } else if (reserved_name(pat
)) {
427 tcpd_warn("%s: user name may be reserved word", pat
);
432 static int check_inet_addr(pat
)
435 struct addrinfo
*res
;
437 res
= find_inet_addr(pat
, AI_NUMERICHOST
);
446 /* check_host - criticize host pattern */
447 static int check_host(pat
)
453 if (pat
[0] == '@') { /* @netgroup */
455 /* SCO has no *netgrent() support */
458 const char *machinep
;
462 setnetgrent(pat
+ 1);
463 if (getnetgrent(&machinep
, &userp
, &domainp
) == 0)
464 tcpd_warn("%s: unknown or empty netgroup", pat
+ 1);
467 tcpd_warn("netgroup support disabled");
470 } else if ((mask
= split_at(pat
, '/')) != NULL
) { /* network/netmask */
474 if (dot_quad_addr(pat
, NULL
) != INADDR_NONE
475 || dot_quad_addr(mask
, NULL
) != INADDR_NONE
)
478 else if (check_inet_addr(pat
) && check_inet_addr(mask
))
480 else if (check_inet_addr(pat
) &&
481 (ep
= NULL
, strtoul(mask
, &ep
, 10), ep
&& !*ep
))
485 tcpd_warn("%s/%s: bad net/mask pattern", pat
, mask
);
486 } else if (STR_EQ(pat
, "FAIL")) { /* obsolete */
487 tcpd_warn("FAIL is no longer recognized");
488 tcpd_warn("(use EXCEPT or DENY instead)");
489 } else if (reserved_name(pat
)) { /* other reserved */
491 } else if (NOT_INADDR(pat
)) { /* internet name */
492 if (pat
[strlen(pat
) - 1] == '.') {
493 tcpd_warn("%s: domain or host name ends in dot", pat
);
494 } else if (pat
[0] != '.') {
495 addr_count
= check_dns(pat
);
497 } else { /* numeric form */
498 if (STR_EQ(pat
, "0.0.0.0") || STR_EQ(pat
, "255.255.255.255")) {
500 } else if (pat
[0] == '.') {
501 tcpd_warn("%s: network number begins with dot", pat
);
502 } else if (pat
[strlen(pat
) - 1] != '.') {
509 /* reserved_name - determine if name is reserved */
511 static int reserved_name(pat
)
514 return (STR_EQ(pat
, unknown
)
515 || STR_EQ(pat
, "KNOWN")
516 || STR_EQ(pat
, paranoid
)
517 || STR_EQ(pat
, "ALL")
518 || STR_EQ(pat
, "LOCAL"));