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