xref: /netbsd-src/usr.sbin/tcpdchk/tcpdchk.c (revision 037708cbd4616ccd0d7d0381ebd3964d6696c188)
1  /*
2   * tcpdchk - examine all tcpd access control rules and inetd.conf entries
3   *
4   * Usage: tcpdchk [-a] [-d] [-i inet_conf] [-v]
5   *
6   * -a: complain about implicit "allow" at end of rule.
7   *
8   * -d: rules in current directory.
9   *
10   * -i: location of inetd.conf file.
11   *
12   * -v: show all rules.
13   *
14   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
15   */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#) tcpdchk.c 1.7 96/02/11 17:01:34";
19 #endif
20 
21 /* System libraries. */
22 
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <stdio.h>
28 #include <syslog.h>
29 #include <setjmp.h>
30 #include <errno.h>
31 #include <netdb.h>
32 #include <string.h>
33 
34 extern int errno;
35 extern void exit();
36 extern int optind;
37 extern char *optarg;
38 
39 #ifndef INADDR_NONE
40 #define INADDR_NONE     (-1)		/* XXX should be 0xffffffff */
41 #endif
42 
43 #ifndef S_ISDIR
44 #define S_ISDIR(m)	(((m) & S_IFMT) == S_IFDIR)
45 #endif
46 
47 /* Application-specific. */
48 
49 #include "tcpd.h"
50 #include "inetcf.h"
51 #include "scaffold.h"
52 
53  /*
54   * Stolen from hosts_access.c...
55   */
56 static char sep[] = ", \t\n";
57 
58 #define	BUFLEN 2048
59 
60 int     resident = 0;
61 int     hosts_access_verbose = 0;
62 char   *hosts_allow_table = HOSTS_ALLOW;
63 char   *hosts_deny_table = HOSTS_DENY;
64 extern jmp_buf tcpd_buf;
65 
66  /*
67   * Local stuff.
68   */
69 static void usage();
70 static void parse_table();
71 static void print_list();
72 static void check_daemon_list();
73 static void check_client_list();
74 static void check_daemon();
75 static void check_user();
76 static int check_host();
77 static int reserved_name();
78 
79 #define PERMIT	1
80 #define DENY	0
81 
82 #define YES	1
83 #define	NO	0
84 
85 static int defl_verdict;
86 static char *myname;
87 static int allow_check;
88 static char *inetcf;
89 
90 int     main(argc, argv)
91 int     argc;
92 char  **argv;
93 {
94     struct request_info request;
95     struct stat st;
96     int     c;
97 
98     myname = argv[0];
99 
100     /*
101      * Parse the JCL.
102      */
103     while ((c = getopt(argc, argv, "adi:v")) != EOF) {
104 	switch (c) {
105 	case 'a':
106 	    allow_check = 1;
107 	    break;
108 	case 'd':
109 	    hosts_allow_table = "hosts.allow";
110 	    hosts_deny_table = "hosts.deny";
111 	    break;
112 	case 'i':
113 	    inetcf = optarg;
114 	    break;
115 	case 'v':
116 	    hosts_access_verbose++;
117 	    break;
118 	default:
119 	    usage();
120 	    /* NOTREACHED */
121 	}
122     }
123     if (argc != optind)
124 	usage();
125 
126     /*
127      * When confusion really strikes...
128      */
129     if (check_path(REAL_DAEMON_DIR, &st) < 0) {
130 	tcpd_warn("REAL_DAEMON_DIR %s: %m", REAL_DAEMON_DIR);
131     } else if (!S_ISDIR(st.st_mode)) {
132 	tcpd_warn("REAL_DAEMON_DIR %s is not a directory", REAL_DAEMON_DIR);
133     }
134 
135     /*
136      * Process the inet configuration file (or its moral equivalent). This
137      * information is used later to find references in hosts.allow/deny to
138      * unwrapped services, and other possible problems.
139      */
140     inetcf = inet_cfg(inetcf);
141     if (hosts_access_verbose)
142 	printf("Using network configuration file: %s\n", inetcf);
143 
144     /*
145      * These are not run from inetd but may have built-in access control.
146      */
147     inet_set("portmap", WR_NOT);
148     inet_set("rpcbind", WR_NOT);
149 
150     /*
151      * Check accessibility of access control files.
152      */
153     (void) check_path(hosts_allow_table, &st);
154     (void) check_path(hosts_deny_table, &st);
155 
156     /*
157      * Fake up an arbitrary service request.
158      */
159     request_init(&request,
160 		 RQ_DAEMON, "daemon_name",
161 		 RQ_SERVER_NAME, "server_hostname",
162 		 RQ_SERVER_ADDR, "server_addr",
163 		 RQ_USER, "user_name",
164 		 RQ_CLIENT_NAME, "client_hostname",
165 		 RQ_CLIENT_ADDR, "client_addr",
166 		 RQ_FILE, 1,
167 		 0);
168 
169     /*
170      * Examine all access-control rules.
171      */
172     defl_verdict = PERMIT;
173     parse_table(hosts_allow_table, &request);
174     defl_verdict = DENY;
175     parse_table(hosts_deny_table, &request);
176     return (0);
177 }
178 
179 /* usage - explain */
180 
181 static void usage()
182 {
183     fprintf(stderr, "usage: %s [-a] [-d] [-i inet_conf] [-v]\n", myname);
184     fprintf(stderr, "	-a: report rules with implicit \"ALLOW\" at end\n");
185     fprintf(stderr, "	-d: use allow/deny files in current directory\n");
186     fprintf(stderr, "	-i: location of inetd.conf file\n");
187     fprintf(stderr, "	-v: list all rules\n");
188     exit(1);
189 }
190 
191 /* parse_table - like table_match(), but examines _all_ entries */
192 
193 static void parse_table(table, request)
194 char   *table;
195 struct request_info *request;
196 {
197     FILE   *fp;
198     int     real_verdict;
199     char    sv_list[BUFLEN];		/* becomes list of daemons */
200     char   *cl_list;			/* becomes list of requests */
201     char   *sh_cmd;			/* becomes optional shell command */
202     char    buf[BUFSIZ];
203     int     verdict;
204     struct tcpd_context saved_context;
205 
206     saved_context = tcpd_context;		/* stupid compilers */
207 
208     if (fp = fopen(table, "r")) {
209 	tcpd_context.file = table;
210 	tcpd_context.line = 0;
211 	while (xgets(sv_list, sizeof(sv_list), fp)) {
212 	    if (sv_list[strlen(sv_list) - 1] != '\n') {
213 		tcpd_warn("missing newline or line too long");
214 		continue;
215 	    }
216 	    if (sv_list[0] == '#' || sv_list[strspn(sv_list, " \t\r\n")] == 0)
217 		continue;
218 	    if ((cl_list = split_at(sv_list, ':')) == 0) {
219 		tcpd_warn("missing \":\" separator");
220 		continue;
221 	    }
222 	    sh_cmd = split_at(cl_list, ':');
223 
224 	    if (hosts_access_verbose)
225 		printf("\n>>> Rule %s line %d:\n",
226 		       tcpd_context.file, tcpd_context.line);
227 
228 	    if (hosts_access_verbose)
229 		print_list("daemons:  ", sv_list);
230 	    check_daemon_list(sv_list);
231 
232 	    if (hosts_access_verbose)
233 		print_list("clients:  ", cl_list);
234 	    check_client_list(cl_list);
235 
236 #ifdef PROCESS_OPTIONS
237 	    real_verdict = defl_verdict;
238 	    if (sh_cmd) {
239 		if ((verdict = setjmp(tcpd_buf)) != 0) {
240 		    real_verdict = (verdict == AC_PERMIT);
241 		} else {
242 		    dry_run = 1;
243 		    process_options(sh_cmd, request);
244 		    if (dry_run == 1 && real_verdict && allow_check)
245 			tcpd_warn("implicit \"allow\" at end of rule");
246 		}
247 	    } else if (defl_verdict && allow_check) {
248 		tcpd_warn("implicit \"allow\" at end of rule");
249 	    }
250 	    if (hosts_access_verbose)
251 		printf("access:   %s\n", real_verdict ? "granted" : "denied");
252 #else
253 	    if (sh_cmd)
254 		shell_cmd(percent_x(buf, sizeof(buf), sh_cmd, request));
255 	    if (hosts_access_verbose)
256 		printf("access:   %s\n", defl_verdict ? "granted" : "denied");
257 #endif
258 	}
259 	(void) fclose(fp);
260     } else if (errno != ENOENT) {
261 	tcpd_warn("cannot open %s: %m", table);
262     }
263     tcpd_context = saved_context;
264 }
265 
266 /* print_list - pretty-print a list */
267 
268 static void print_list(title, list)
269 char   *title;
270 char   *list;
271 {
272     char    buf[BUFLEN];
273     char   *cp;
274     char   *next;
275 
276     fputs(title, stdout);
277     strcpy(buf, list);
278 
279     for (cp = strtok(buf, sep); cp != 0; cp = next) {
280 	fputs(cp, stdout);
281 	next = strtok((char *) 0, sep);
282 	if (next != 0)
283 	    fputs(" ", stdout);
284     }
285     fputs("\n", stdout);
286 }
287 
288 /* check_daemon_list - criticize daemon list */
289 
290 static void check_daemon_list(list)
291 char   *list;
292 {
293     char    buf[BUFLEN];
294     char   *cp;
295     char   *host;
296     int     daemons = 0;
297 
298     strcpy(buf, list);
299 
300     for (cp = strtok(buf, sep); cp != 0; cp = strtok((char *) 0, sep)) {
301 	if (STR_EQ(cp, "EXCEPT")) {
302 	    daemons = 0;
303 	} else {
304 	    daemons++;
305 	    if ((host = split_at(cp + 1, '@')) != 0 && check_host(host) > 1) {
306 		tcpd_warn("host %s has more than one address", host);
307 		tcpd_warn("(consider using an address instead)");
308 	    }
309 	    check_daemon(cp);
310 	}
311     }
312     if (daemons == 0)
313 	tcpd_warn("daemon list is empty or ends in EXCEPT");
314 }
315 
316 /* check_client_list - criticize client list */
317 
318 static void check_client_list(list)
319 char   *list;
320 {
321     char    buf[BUFLEN];
322     char   *cp;
323     char   *host;
324     int     clients = 0;
325 
326     strcpy(buf, list);
327 
328     for (cp = strtok(buf, sep); cp != 0; cp = strtok((char *) 0, sep)) {
329 	if (STR_EQ(cp, "EXCEPT")) {
330 	    clients = 0;
331 	} else {
332 	    clients++;
333 	    if (host = split_at(cp + 1, '@')) {	/* user@host */
334 		check_user(cp);
335 		check_host(host);
336 	    } else {
337 		check_host(cp);
338 	    }
339 	}
340     }
341     if (clients == 0)
342 	tcpd_warn("client list is empty or ends in EXCEPT");
343 }
344 
345 /* check_daemon - criticize daemon pattern */
346 
347 static void check_daemon(pat)
348 char   *pat;
349 {
350     if (pat[0] == '@') {
351 	tcpd_warn("%s: daemon name begins with \"@\"", pat);
352     } else if (pat[0] == '.') {
353 	tcpd_warn("%s: daemon name begins with dot", pat);
354     } else if (pat[strlen(pat) - 1] == '.') {
355 	tcpd_warn("%s: daemon name ends in dot", pat);
356     } else if (STR_EQ(pat, "ALL") || STR_EQ(pat, unknown)) {
357 	 /* void */ ;
358     } else if (STR_EQ(pat, "FAIL")) {		/* obsolete */
359 	tcpd_warn("FAIL is no longer recognized");
360 	tcpd_warn("(use EXCEPT or DENY instead)");
361     } else if (reserved_name(pat)) {
362 	tcpd_warn("%s: daemon name may be reserved word", pat);
363     } else {
364 	switch (inet_get(pat)) {
365 	case WR_UNKNOWN:
366 	    tcpd_warn("%s: no such process name in %s", pat, inetcf);
367 	    inet_set(pat, WR_YES);		/* shut up next time */
368 	    break;
369 	case WR_NOT:
370 	    tcpd_warn("%s: service possibly not wrapped", pat);
371 	    inet_set(pat, WR_YES);
372 	    break;
373 	}
374     }
375 }
376 
377 /* check_user - criticize user pattern */
378 
379 static void check_user(pat)
380 char   *pat;
381 {
382     if (pat[0] == '@') {			/* @netgroup */
383 	tcpd_warn("%s: user name begins with \"@\"", pat);
384     } else if (pat[0] == '.') {
385 	tcpd_warn("%s: user name begins with dot", pat);
386     } else if (pat[strlen(pat) - 1] == '.') {
387 	tcpd_warn("%s: user name ends in dot", pat);
388     } else if (STR_EQ(pat, "ALL") || STR_EQ(pat, unknown)
389 	       || STR_EQ(pat, "KNOWN")) {
390 	 /* void */ ;
391     } else if (STR_EQ(pat, "FAIL")) {		/* obsolete */
392 	tcpd_warn("FAIL is no longer recognized");
393 	tcpd_warn("(use EXCEPT or DENY instead)");
394     } else if (reserved_name(pat)) {
395 	tcpd_warn("%s: user name may be reserved word", pat);
396     }
397 }
398 
399 /* check_host - criticize host pattern */
400 
401 static int check_host(pat)
402 char   *pat;
403 {
404     char   *mask;
405     int     addr_count = 1;
406 
407     if (pat[0] == '@') {			/* @netgroup */
408 #ifdef NO_NETGRENT
409 	/* SCO has no *netgrent() support */
410 #else
411 #ifdef NETGROUP
412 	char   *machinep;
413 	char   *userp;
414 	char   *domainp;
415 
416 	setnetgrent(pat + 1);
417 	if (getnetgrent(&machinep, &userp, &domainp) == 0)
418 	    tcpd_warn("%s: unknown or empty netgroup", pat + 1);
419 	endnetgrent();
420 #else
421 	tcpd_warn("netgroup support disabled");
422 #endif
423 #endif
424     } else if (mask = split_at(pat, '/')) {	/* network/netmask */
425 	if (dot_quad_addr(pat) == INADDR_NONE
426 	    || dot_quad_addr(mask) == INADDR_NONE)
427 	    tcpd_warn("%s/%s: bad net/mask pattern", pat, mask);
428     } else if (STR_EQ(pat, "FAIL")) {		/* obsolete */
429 	tcpd_warn("FAIL is no longer recognized");
430 	tcpd_warn("(use EXCEPT or DENY instead)");
431     } else if (reserved_name(pat)) {		/* other reserved */
432 	 /* void */ ;
433     } else if (NOT_INADDR(pat)) {		/* internet name */
434 	if (pat[strlen(pat) - 1] == '.') {
435 	    tcpd_warn("%s: domain or host name ends in dot", pat);
436 	} else if (pat[0] != '.') {
437 	    addr_count = check_dns(pat);
438 	}
439     } else {					/* numeric form */
440 	if (STR_EQ(pat, "0.0.0.0") || STR_EQ(pat, "255.255.255.255")) {
441 	    /* void */ ;
442 	} else if (pat[0] == '.') {
443 	    tcpd_warn("%s: network number begins with dot", pat);
444 	} else if (pat[strlen(pat) - 1] != '.') {
445 	    check_dns(pat);
446 	}
447     }
448     return (addr_count);
449 }
450 
451 /* reserved_name - determine if name is reserved */
452 
453 static int reserved_name(pat)
454 char   *pat;
455 {
456     return (STR_EQ(pat, unknown)
457 	    || STR_EQ(pat, "KNOWN")
458 	    || STR_EQ(pat, paranoid)
459 	    || STR_EQ(pat, "ALL")
460 	    || STR_EQ(pat, "LOCAL"));
461 }
462