xref: /netbsd-src/usr.sbin/tcpdmatch/tcpdmatch.c (revision 421949a31fb0942d3d87278998c2d7d432d8b3cb)
1*421949a3Ssevan /*	$NetBSD: tcpdmatch.c,v 1.9 2018/01/23 21:06:26 sevan Exp $	*/
23a2d6828Schristos 
3fbb40475Scjs  /*
4fbb40475Scjs   * tcpdmatch - explain what tcpd would do in a specific case
5fbb40475Scjs   *
6fbb40475Scjs   * usage: tcpdmatch [-d] [-i inet_conf] daemon[@host] [user@]host
7fbb40475Scjs   *
8fbb40475Scjs   * -d: use the access control tables in the current directory.
9fbb40475Scjs   *
10fbb40475Scjs   * -i: location of inetd.conf file.
11fbb40475Scjs   *
12fbb40475Scjs   * All errors are reported to the standard error stream, including the errors
13fbb40475Scjs   * that would normally be reported via the syslog daemon.
14fbb40475Scjs   *
15fbb40475Scjs   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
16fbb40475Scjs   */
17fbb40475Scjs 
183a2d6828Schristos #include <sys/cdefs.h>
19fbb40475Scjs #ifndef lint
203a2d6828Schristos #if 0
21fbb40475Scjs static char sccsid[] = "@(#) tcpdmatch.c 1.5 96/02/11 17:01:36";
223a2d6828Schristos #else
23*421949a3Ssevan __RCSID("$NetBSD: tcpdmatch.c,v 1.9 2018/01/23 21:06:26 sevan Exp $");
243a2d6828Schristos #endif
25fbb40475Scjs #endif
26fbb40475Scjs 
27fbb40475Scjs /* System libraries. */
28fbb40475Scjs 
29fbb40475Scjs #include <sys/types.h>
30fbb40475Scjs #include <sys/stat.h>
31fbb40475Scjs #include <sys/socket.h>
32fbb40475Scjs #include <netinet/in.h>
33fbb40475Scjs #include <arpa/inet.h>
34fbb40475Scjs #include <netdb.h>
35fbb40475Scjs #include <stdio.h>
36fbb40475Scjs #include <syslog.h>
37fbb40475Scjs #include <setjmp.h>
38fbb40475Scjs #include <string.h>
3915a80299Schristos #include <stdlib.h>
4075a6e035Sperry #include <unistd.h>
41fbb40475Scjs 
42fbb40475Scjs #ifndef	INADDR_NONE
43fbb40475Scjs #define	INADDR_NONE	(-1)		/* XXX should be 0xffffffff */
44fbb40475Scjs #endif
45fbb40475Scjs 
46fbb40475Scjs #ifndef S_ISDIR
47fbb40475Scjs #define S_ISDIR(m)	(((m) & S_IFMT) == S_IFDIR)
48fbb40475Scjs #endif
49fbb40475Scjs 
50fbb40475Scjs /* Application-specific. */
51fbb40475Scjs 
52fbb40475Scjs #include "tcpd.h"
53fbb40475Scjs #include "inetcf.h"
54fbb40475Scjs #include "scaffold.h"
55fbb40475Scjs 
56*421949a3Ssevan static void usage(char *);
57*421949a3Ssevan static void expand(char *, char *, struct request_info *);
58*421949a3Ssevan static void tcpdmatch(struct request_info *);
59fbb40475Scjs 
60fbb40475Scjs /* The main program */
61fbb40475Scjs 
main(int argc,char * argv[])62*421949a3Ssevan int main(int argc, char *argv[])
63fbb40475Scjs {
642f7d82e6Sitojun     struct addrinfo *res, *res0;
65fbb40475Scjs     char   *myname = argv[0];
66fbb40475Scjs     char   *client;
67fbb40475Scjs     char   *server;
68fbb40475Scjs     char   *user;
69fbb40475Scjs     char   *daemon;
70fbb40475Scjs     struct request_info request;
71fbb40475Scjs     int     ch;
72fbb40475Scjs     char   *inetcf = 0;
73fbb40475Scjs     int     count;
742f7d82e6Sitojun     struct sockaddr_storage server_ss;
752f7d82e6Sitojun     struct sockaddr_storage client_ss;
76fbb40475Scjs     struct stat st;
77fbb40475Scjs 
78fbb40475Scjs     /*
79fbb40475Scjs      * Show what rule actually matched.
80fbb40475Scjs      */
81fbb40475Scjs     hosts_access_verbose = 2;
82fbb40475Scjs 
83fbb40475Scjs     /*
84fbb40475Scjs      * Parse the JCL.
85fbb40475Scjs      */
863a2d6828Schristos     while ((ch = getopt(argc, argv, "di:")) != -1) {
87fbb40475Scjs 	switch (ch) {
88fbb40475Scjs 	case 'd':
89fbb40475Scjs 	    hosts_allow_table = "hosts.allow";
90fbb40475Scjs 	    hosts_deny_table = "hosts.deny";
91fbb40475Scjs 	    break;
92fbb40475Scjs 	case 'i':
93fbb40475Scjs 	    inetcf = optarg;
94fbb40475Scjs 	    break;
95fbb40475Scjs 	default:
96fbb40475Scjs 	    usage(myname);
97fbb40475Scjs 	    /* NOTREACHED */
98fbb40475Scjs 	}
99fbb40475Scjs     }
100fbb40475Scjs     if (argc != optind + 2)
101fbb40475Scjs 	usage(myname);
102fbb40475Scjs 
103fbb40475Scjs     /*
104fbb40475Scjs      * When confusion really strikes...
105fbb40475Scjs      */
106fbb40475Scjs     if (check_path(REAL_DAEMON_DIR, &st) < 0) {
107fbb40475Scjs 	tcpd_warn("REAL_DAEMON_DIR %s: %m", REAL_DAEMON_DIR);
108fbb40475Scjs     } else if (!S_ISDIR(st.st_mode)) {
109fbb40475Scjs 	tcpd_warn("REAL_DAEMON_DIR %s is not a directory", REAL_DAEMON_DIR);
110fbb40475Scjs     }
111fbb40475Scjs 
112fbb40475Scjs     /*
113fbb40475Scjs      * Default is to specify a daemon process name. When daemon@host is
114fbb40475Scjs      * specified, separate the two parts.
115fbb40475Scjs      */
116fbb40475Scjs     if ((server = split_at(argv[optind], '@')) == 0)
117fbb40475Scjs 	server = unknown;
118fbb40475Scjs     if (argv[optind][0] == '/') {
119fbb40475Scjs 	daemon = strrchr(argv[optind], '/') + 1;
120fbb40475Scjs 	tcpd_warn("%s: daemon name normalized to: %s", argv[optind], daemon);
121fbb40475Scjs     } else {
122fbb40475Scjs 	daemon = argv[optind];
123fbb40475Scjs     }
124fbb40475Scjs 
125fbb40475Scjs     /*
126fbb40475Scjs      * Default is to specify a client hostname or address. When user@host is
127fbb40475Scjs      * specified, separate the two parts.
128fbb40475Scjs      */
129fbb40475Scjs     if ((client = split_at(argv[optind + 1], '@')) != 0) {
130fbb40475Scjs 	user = argv[optind + 1];
131fbb40475Scjs     } else {
132fbb40475Scjs 	client = argv[optind + 1];
133fbb40475Scjs 	user = unknown;
134fbb40475Scjs     }
135fbb40475Scjs 
136fbb40475Scjs     /*
137fbb40475Scjs      * Analyze the inetd (or tlid) configuration file, so that we can warn
138fbb40475Scjs      * the user about services that may not be wrapped, services that are not
139fbb40475Scjs      * configured, or services that are wrapped in an incorrect manner. Allow
140fbb40475Scjs      * for services that are not run from inetd, or that have tcpd access
141fbb40475Scjs      * control built into them.
142fbb40475Scjs      */
143fbb40475Scjs     inetcf = inet_cfg(inetcf);
144fbb40475Scjs     inet_set("portmap", WR_NOT);
145fbb40475Scjs     inet_set("rpcbind", WR_NOT);
146fbb40475Scjs     switch (inet_get(daemon)) {
147fbb40475Scjs     case WR_UNKNOWN:
148fbb40475Scjs 	tcpd_warn("%s: no such process name in %s", daemon, inetcf);
149fbb40475Scjs 	break;
150fbb40475Scjs     case WR_NOT:
151fbb40475Scjs 	tcpd_warn("%s: service possibly not wrapped", daemon);
152fbb40475Scjs 	break;
153fbb40475Scjs     }
154fbb40475Scjs 
155fbb40475Scjs     /*
156fbb40475Scjs      * Check accessibility of access control files.
157fbb40475Scjs      */
158fbb40475Scjs     (void) check_path(hosts_allow_table, &st);
159fbb40475Scjs     (void) check_path(hosts_deny_table, &st);
160fbb40475Scjs 
161fbb40475Scjs     /*
162fbb40475Scjs      * Fill in what we have figured out sofar. Use socket and DNS routines
163fbb40475Scjs      * for address and name conversions. We attach stdout to the request so
164fbb40475Scjs      * that banner messages will become visible.
165fbb40475Scjs      */
166fbb40475Scjs     request_init(&request, RQ_DAEMON, daemon, RQ_USER, user, RQ_FILE, 1, 0);
167fbb40475Scjs     sock_methods(&request);
168fbb40475Scjs 
169fbb40475Scjs     /*
170fbb40475Scjs      * If a server hostname is specified, insist that the name maps to at
171fbb40475Scjs      * most one address. eval_hostname() warns the user about name server
172fbb40475Scjs      * problems, while using the request.server structure as a cache for host
173fbb40475Scjs      * address and name conversion results.
174fbb40475Scjs      */
175fbb40475Scjs     if (NOT_INADDR(server) == 0 || HOSTNAME_KNOWN(server)) {
1762f7d82e6Sitojun 	if ((res0 = find_inet_addr(server, 0)) == NULL)
177fbb40475Scjs 	    exit(1);
1782f7d82e6Sitojun 	memset((char *) &server_ss, 0, sizeof(server_ss));
1792f7d82e6Sitojun 	request_set(&request, RQ_SERVER_SIN, &server_ss, 0);
180fbb40475Scjs 
1812f7d82e6Sitojun 	count = 0;
1822f7d82e6Sitojun 	for (res = res0; res; res = res->ai_next) {
1832f7d82e6Sitojun 	    count++;
1842f7d82e6Sitojun 	    if (res->ai_addrlen > sizeof(server_ss))
1852f7d82e6Sitojun 		continue;
1862f7d82e6Sitojun 	    memcpy(&server_ss, res->ai_addr, res->ai_addrlen);
187fbb40475Scjs 
188fbb40475Scjs 	    /*
189fbb40475Scjs 	     * Force evaluation of server host name and address. Host name
190fbb40475Scjs 	     * conflicts will be reported while eval_hostname() does its job.
191fbb40475Scjs 	     */
192fbb40475Scjs 	    request_set(&request, RQ_SERVER_NAME, "", RQ_SERVER_ADDR, "", 0);
193fbb40475Scjs 	    if (STR_EQ(eval_hostname(request.server), unknown))
194fbb40475Scjs 		tcpd_warn("host address %s->name lookup failed",
195fbb40475Scjs 			  eval_hostaddr(request.server));
196fbb40475Scjs 	}
197fbb40475Scjs 	if (count > 1) {
198fbb40475Scjs 	    fprintf(stderr, "Error: %s has more than one address\n", server);
199fbb40475Scjs 	    fprintf(stderr, "Please specify an address instead\n");
200fbb40475Scjs 	    exit(1);
201fbb40475Scjs 	}
2022f7d82e6Sitojun 	freeaddrinfo(res0);
203fbb40475Scjs     } else {
204fbb40475Scjs 	request_set(&request, RQ_SERVER_NAME, server, 0);
205fbb40475Scjs     }
206fbb40475Scjs 
207fbb40475Scjs     /*
208fbb40475Scjs      * If a client address is specified, we simulate the effect of client
209fbb40475Scjs      * hostname lookup failure.
210fbb40475Scjs      */
2112f7d82e6Sitojun     res0 = find_inet_addr(client, AI_NUMERICHOST);
2122f7d82e6Sitojun     if (res0 && !res0->ai_next) {
2132f7d82e6Sitojun 	request_set(&request, RQ_CLIENT_SIN, res0->ai_addr);
214fbb40475Scjs 	tcpdmatch(&request);
2152f7d82e6Sitojun 	freeaddrinfo(res0);
216fbb40475Scjs 	exit(0);
217fbb40475Scjs     }
2182ad320ffSitojun     if (res0)
2192f7d82e6Sitojun 	freeaddrinfo(res0);
220fbb40475Scjs 
221fbb40475Scjs     /*
222fbb40475Scjs      * Perhaps they are testing special client hostname patterns that aren't
223fbb40475Scjs      * really host names at all.
224fbb40475Scjs      */
225fbb40475Scjs     if (NOT_INADDR(client) && HOSTNAME_KNOWN(client) == 0) {
226fbb40475Scjs 	request_set(&request, RQ_CLIENT_NAME, client, 0);
227fbb40475Scjs 	tcpdmatch(&request);
228fbb40475Scjs 	exit(0);
229fbb40475Scjs     }
230fbb40475Scjs 
231fbb40475Scjs     /*
232fbb40475Scjs      * Otherwise, assume that a client hostname is specified, and insist that
233fbb40475Scjs      * the address can be looked up. The reason for this requirement is that
234fbb40475Scjs      * in real life the client address is available (at least with IP). Let
235fbb40475Scjs      * eval_hostname() figure out if this host is properly registered, while
236fbb40475Scjs      * using the request.client structure as a cache for host name and
237fbb40475Scjs      * address conversion results.
238fbb40475Scjs      */
2392f7d82e6Sitojun     if ((res0 = find_inet_addr(client, 0)) == NULL)
240fbb40475Scjs 	exit(1);
2412f7d82e6Sitojun     memset((char *) &client_ss, 0, sizeof(client_ss));
2422f7d82e6Sitojun     request_set(&request, RQ_CLIENT_SIN, &client_ss, 0);
243fbb40475Scjs 
2442f7d82e6Sitojun     count = 0;
2452f7d82e6Sitojun     for (res = res0; res; res = res->ai_next) {
2462f7d82e6Sitojun 	count++;
2472f7d82e6Sitojun 	if (res->ai_addrlen > sizeof(client_ss))
2482f7d82e6Sitojun 	    continue;
2492f7d82e6Sitojun 	memcpy(&client_ss, res->ai_addr, res->ai_addrlen);
250fbb40475Scjs 
251fbb40475Scjs 	/*
252fbb40475Scjs 	 * Force evaluation of client host name and address. Host name
253fbb40475Scjs 	 * conflicts will be reported while eval_hostname() does its job.
254fbb40475Scjs 	 */
255fbb40475Scjs 	request_set(&request, RQ_CLIENT_NAME, "", RQ_CLIENT_ADDR, "", 0);
256fbb40475Scjs 	if (STR_EQ(eval_hostname(request.client), unknown))
257fbb40475Scjs 	    tcpd_warn("host address %s->name lookup failed",
258fbb40475Scjs 		      eval_hostaddr(request.client));
259fbb40475Scjs 	tcpdmatch(&request);
2602f7d82e6Sitojun 	if (res->ai_next)
261fbb40475Scjs 	    printf("\n");
262fbb40475Scjs     }
2632f7d82e6Sitojun     freeaddrinfo(res0);
264fbb40475Scjs     exit(0);
265fbb40475Scjs }
266fbb40475Scjs 
267fbb40475Scjs /* Explain how to use this program */
268fbb40475Scjs 
usage(char * myname)269*421949a3Ssevan static void usage(char *myname)
270fbb40475Scjs {
271fbb40475Scjs     fprintf(stderr, "usage: %s [-d] [-i inet_conf] daemon[@host] [user@]host\n",
272fbb40475Scjs 	    myname);
273fbb40475Scjs     fprintf(stderr, "	-d: use allow/deny files in current directory\n");
274fbb40475Scjs     fprintf(stderr, "	-i: location of inetd.conf file\n");
275fbb40475Scjs     exit(1);
276fbb40475Scjs }
277fbb40475Scjs 
278fbb40475Scjs /* Print interesting expansions */
279fbb40475Scjs 
expand(char * text,char * pattern,struct request_info * request)280*421949a3Ssevan static void expand(char *text, char *pattern, struct request_info *request)
281fbb40475Scjs {
282fbb40475Scjs     char    buf[BUFSIZ];
283fbb40475Scjs 
284fbb40475Scjs     if (STR_NE(percent_x(buf, sizeof(buf), pattern, request), unknown))
285fbb40475Scjs 	printf("%s %s\n", text, buf);
286fbb40475Scjs }
287fbb40475Scjs 
288fbb40475Scjs /* Try out a (server,client) pair */
289fbb40475Scjs 
tcpdmatch(struct request_info * request)290*421949a3Ssevan static void tcpdmatch(struct request_info *request)
291fbb40475Scjs {
292fbb40475Scjs     int     verdict;
293fbb40475Scjs 
294fbb40475Scjs     /*
295fbb40475Scjs      * Show what we really know. Suppress uninteresting noise.
296fbb40475Scjs      */
297fbb40475Scjs     expand("client:   hostname", "%n", request);
298fbb40475Scjs     expand("client:   address ", "%a", request);
299fbb40475Scjs     expand("client:   username", "%u", request);
300fbb40475Scjs     expand("server:   hostname", "%N", request);
301fbb40475Scjs     expand("server:   address ", "%A", request);
302fbb40475Scjs     expand("server:   process ", "%d", request);
303fbb40475Scjs 
304fbb40475Scjs     /*
305fbb40475Scjs      * Reset stuff that might be changed by options handlers. In dry-run
306fbb40475Scjs      * mode, extension language routines that would not return should inform
307fbb40475Scjs      * us of their plan, by clearing the dry_run flag. This is a bit clumsy
308fbb40475Scjs      * but we must be able to verify hosts with more than one network
309fbb40475Scjs      * address.
310fbb40475Scjs      */
311fbb40475Scjs     rfc931_timeout = RFC931_TIMEOUT;
312fbb40475Scjs     allow_severity = SEVERITY;
313fbb40475Scjs     deny_severity = LOG_WARNING;
314fbb40475Scjs     dry_run = 1;
315fbb40475Scjs 
316fbb40475Scjs     /*
317fbb40475Scjs      * When paranoid mode is enabled, access is rejected no matter what the
318fbb40475Scjs      * access control rules say.
319fbb40475Scjs      */
320fbb40475Scjs #ifdef PARANOID
321fbb40475Scjs     if (STR_EQ(eval_hostname(request->client), paranoid)) {
322fbb40475Scjs 	printf("access:   denied (PARANOID mode)\n\n");
323fbb40475Scjs 	return;
324fbb40475Scjs     }
325fbb40475Scjs #endif
326fbb40475Scjs 
327fbb40475Scjs     /*
328fbb40475Scjs      * Report the access control verdict.
329fbb40475Scjs      */
330fbb40475Scjs     verdict = hosts_access(request);
331fbb40475Scjs     printf("access:   %s\n",
332fbb40475Scjs 	   dry_run == 0 ? "delegated" :
333fbb40475Scjs 	   verdict ? "granted" : "denied");
334fbb40475Scjs }
335