Home | History | Annotate | Line # | Download | only in tcpdchk
scaffold.c revision 1.3
      1  1.3  christos /*	$NetBSD: scaffold.c,v 1.3 1997/11/16 21:30:25 christos Exp $	*/
      2  1.2  christos 
      3  1.1       cjs  /*
      4  1.1       cjs   * Routines for testing only. Not really industrial strength.
      5  1.1       cjs   *
      6  1.1       cjs   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
      7  1.1       cjs   */
      8  1.1       cjs 
      9  1.2  christos #include <sys/cdefs.h>
     10  1.1       cjs #ifndef lint
     11  1.2  christos #if 0
     12  1.1       cjs static char sccs_id[] = "@(#) scaffold.c 1.5 95/01/03 09:13:48";
     13  1.2  christos #else
     14  1.3  christos __RCSID("$NetBSD: scaffold.c,v 1.3 1997/11/16 21:30:25 christos Exp $");
     15  1.2  christos #endif
     16  1.1       cjs #endif
     17  1.1       cjs 
     18  1.1       cjs /* System libraries. */
     19  1.1       cjs 
     20  1.1       cjs #include <sys/types.h>
     21  1.1       cjs #include <sys/stat.h>
     22  1.1       cjs #include <sys/socket.h>
     23  1.1       cjs #include <netinet/in.h>
     24  1.1       cjs #include <arpa/inet.h>
     25  1.1       cjs #include <netdb.h>
     26  1.1       cjs #include <stdio.h>
     27  1.1       cjs #include <syslog.h>
     28  1.1       cjs #include <setjmp.h>
     29  1.1       cjs #include <string.h>
     30  1.2  christos #include <stdlib.h>
     31  1.1       cjs 
     32  1.1       cjs #ifndef INADDR_NONE
     33  1.1       cjs #define	INADDR_NONE	(-1)		/* XXX should be 0xffffffff */
     34  1.1       cjs #endif
     35  1.1       cjs 
     36  1.1       cjs /* Application-specific. */
     37  1.1       cjs 
     38  1.1       cjs #include "tcpd.h"
     39  1.1       cjs #include "scaffold.h"
     40  1.1       cjs 
     41  1.2  christos static struct hostent *dup_hostent __P((struct hostent *));
     42  1.2  christos 
     43  1.1       cjs  /*
     44  1.1       cjs   * These are referenced by the options module and by rfc931.c.
     45  1.1       cjs   */
     46  1.1       cjs int     allow_severity = SEVERITY;
     47  1.1       cjs int     deny_severity = LOG_WARNING;
     48  1.3  christos extern int rfc931_timeout; /* = RFC931_TIMEOUT; */
     49  1.1       cjs 
     50  1.1       cjs /* dup_hostent - create hostent in one memory block */
     51  1.1       cjs 
     52  1.1       cjs static struct hostent *dup_hostent(hp)
     53  1.1       cjs struct hostent *hp;
     54  1.1       cjs {
     55  1.1       cjs     struct hostent_block {
     56  1.1       cjs 	struct hostent host;
     57  1.1       cjs 	char   *addr_list[1];
     58  1.1       cjs     };
     59  1.1       cjs     struct hostent_block *hb;
     60  1.1       cjs     int     count;
     61  1.1       cjs     char   *data;
     62  1.1       cjs     char   *addr;
     63  1.1       cjs 
     64  1.1       cjs     for (count = 0; hp->h_addr_list[count] != 0; count++)
     65  1.1       cjs 	 /* void */ ;
     66  1.1       cjs 
     67  1.1       cjs     if ((hb = (struct hostent_block *) malloc(sizeof(struct hostent_block)
     68  1.1       cjs 			 + (hp->h_length + sizeof(char *)) * count)) == 0) {
     69  1.1       cjs 	fprintf(stderr, "Sorry, out of memory\n");
     70  1.1       cjs 	exit(1);
     71  1.1       cjs     }
     72  1.1       cjs     memset((char *) &hb->host, 0, sizeof(hb->host));
     73  1.1       cjs     hb->host.h_length = hp->h_length;
     74  1.1       cjs     hb->host.h_addr_list = hb->addr_list;
     75  1.1       cjs     hb->host.h_addr_list[count] = 0;
     76  1.1       cjs     data = (char *) (hb->host.h_addr_list + count + 1);
     77  1.1       cjs 
     78  1.1       cjs     for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) {
     79  1.1       cjs 	hb->host.h_addr_list[count] = data + hp->h_length * count;
     80  1.1       cjs 	memcpy(hb->host.h_addr_list[count], addr, hp->h_length);
     81  1.1       cjs     }
     82  1.1       cjs     return (&hb->host);
     83  1.1       cjs }
     84  1.1       cjs 
     85  1.1       cjs /* find_inet_addr - find all addresses for this host, result to free() */
     86  1.1       cjs 
     87  1.1       cjs struct hostent *find_inet_addr(host)
     88  1.1       cjs char   *host;
     89  1.1       cjs {
     90  1.1       cjs     struct in_addr addr;
     91  1.1       cjs     struct hostent *hp;
     92  1.1       cjs     static struct hostent h;
     93  1.1       cjs     static char *addr_list[2];
     94  1.1       cjs 
     95  1.1       cjs     /*
     96  1.1       cjs      * Host address: translate it to internal form.
     97  1.1       cjs      */
     98  1.1       cjs     if ((addr.s_addr = dot_quad_addr(host)) != INADDR_NONE) {
     99  1.1       cjs 	h.h_addr_list = addr_list;
    100  1.1       cjs 	h.h_addr_list[0] = (char *) &addr;
    101  1.1       cjs 	h.h_length = sizeof(addr);
    102  1.1       cjs 	return (dup_hostent(&h));
    103  1.1       cjs     }
    104  1.1       cjs 
    105  1.1       cjs     /*
    106  1.1       cjs      * Map host name to a series of addresses. Watch out for non-internet
    107  1.1       cjs      * forms or aliases. The NOT_INADDR() is here in case gethostbyname() has
    108  1.1       cjs      * been "enhanced" to accept numeric addresses. Make a copy of the
    109  1.1       cjs      * address list so that later gethostbyXXX() calls will not clobber it.
    110  1.1       cjs      */
    111  1.1       cjs     if (NOT_INADDR(host) == 0) {
    112  1.1       cjs 	tcpd_warn("%s: not an internet address", host);
    113  1.1       cjs 	return (0);
    114  1.1       cjs     }
    115  1.1       cjs     if ((hp = gethostbyname(host)) == 0) {
    116  1.1       cjs 	tcpd_warn("%s: host not found", host);
    117  1.1       cjs 	return (0);
    118  1.1       cjs     }
    119  1.1       cjs     if (hp->h_addrtype != AF_INET) {
    120  1.1       cjs 	tcpd_warn("%d: not an internet host", hp->h_addrtype);
    121  1.1       cjs 	return (0);
    122  1.1       cjs     }
    123  1.1       cjs     if (STR_NE(host, hp->h_name)) {
    124  1.1       cjs 	tcpd_warn("%s: hostname alias", host);
    125  1.1       cjs 	tcpd_warn("(official name: %s)", hp->h_name);
    126  1.1       cjs     }
    127  1.1       cjs     return (dup_hostent(hp));
    128  1.1       cjs }
    129  1.1       cjs 
    130  1.1       cjs /* check_dns - give each address thorough workout, return address count */
    131  1.1       cjs 
    132  1.1       cjs int     check_dns(host)
    133  1.1       cjs char   *host;
    134  1.1       cjs {
    135  1.1       cjs     struct request_info request;
    136  1.1       cjs     struct sockaddr_in sin;
    137  1.1       cjs     struct hostent *hp;
    138  1.1       cjs     int     count;
    139  1.1       cjs     char   *addr;
    140  1.1       cjs 
    141  1.1       cjs     if ((hp = find_inet_addr(host)) == 0)
    142  1.1       cjs 	return (0);
    143  1.1       cjs     request_init(&request, RQ_CLIENT_SIN, &sin, 0);
    144  1.1       cjs     sock_methods(&request);
    145  1.1       cjs     memset((char *) &sin, 0, sizeof(sin));
    146  1.1       cjs     sin.sin_family = AF_INET;
    147  1.1       cjs 
    148  1.1       cjs     for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) {
    149  1.1       cjs 	memcpy((char *) &sin.sin_addr, addr, sizeof(sin.sin_addr));
    150  1.1       cjs 
    151  1.1       cjs 	/*
    152  1.1       cjs 	 * Force host name and address conversions. Use the request structure
    153  1.1       cjs 	 * as a cache. Detect hostname lookup problems. Any name/name or
    154  1.1       cjs 	 * name/address conflicts will be reported while eval_hostname() does
    155  1.1       cjs 	 * its job.
    156  1.1       cjs 	 */
    157  1.1       cjs 	request_set(&request, RQ_CLIENT_ADDR, "", RQ_CLIENT_NAME, "", 0);
    158  1.1       cjs 	if (STR_EQ(eval_hostname(request.client), unknown))
    159  1.1       cjs 	    tcpd_warn("host address %s->name lookup failed",
    160  1.1       cjs 		      eval_hostaddr(request.client));
    161  1.1       cjs     }
    162  1.1       cjs     free((char *) hp);
    163  1.1       cjs     return (count);
    164  1.1       cjs }
    165  1.1       cjs 
    166  1.1       cjs /* dummy function to intercept the real shell_cmd() */
    167  1.1       cjs 
    168  1.1       cjs /* ARGSUSED */
    169  1.1       cjs 
    170  1.1       cjs void    shell_cmd(command)
    171  1.1       cjs char   *command;
    172  1.1       cjs {
    173  1.1       cjs     if (hosts_access_verbose)
    174  1.1       cjs 	printf("command: %s", command);
    175  1.1       cjs }
    176  1.1       cjs 
    177  1.1       cjs /* dummy function  to intercept the real clean_exit() */
    178  1.1       cjs 
    179  1.1       cjs /* ARGSUSED */
    180  1.1       cjs 
    181  1.1       cjs void    clean_exit(request)
    182  1.1       cjs struct request_info *request;
    183  1.1       cjs {
    184  1.1       cjs     exit(0);
    185  1.1       cjs }
    186  1.1       cjs 
    187  1.2  christos #if 0
    188  1.1       cjs /* dummy function  to intercept the real rfc931() */
    189  1.1       cjs 
    190  1.1       cjs /* ARGSUSED */
    191  1.1       cjs 
    192  1.1       cjs void    rfc931(request)
    193  1.1       cjs struct request_info *request;
    194  1.1       cjs {
    195  1.1       cjs     strcpy(request->user, unknown);
    196  1.1       cjs }
    197  1.2  christos #endif
    198  1.1       cjs 
    199  1.1       cjs /* check_path - examine accessibility */
    200  1.1       cjs 
    201  1.1       cjs int     check_path(path, st)
    202  1.1       cjs char   *path;
    203  1.1       cjs struct stat *st;
    204  1.1       cjs {
    205  1.1       cjs     struct stat stbuf;
    206  1.1       cjs     char    buf[BUFSIZ];
    207  1.1       cjs 
    208  1.1       cjs     if (stat(path, st) < 0)
    209  1.1       cjs 	return (-1);
    210  1.1       cjs #ifdef notdef
    211  1.1       cjs     if (st->st_uid != 0)
    212  1.1       cjs 	tcpd_warn("%s: not owned by root", path);
    213  1.1       cjs     if (st->st_mode & 020)
    214  1.1       cjs 	tcpd_warn("%s: group writable", path);
    215  1.1       cjs #endif
    216  1.1       cjs     if (st->st_mode & 002)
    217  1.1       cjs 	tcpd_warn("%s: world writable", path);
    218  1.1       cjs     if (path[0] == '/' && path[1] != 0) {
    219  1.1       cjs 	strrchr(strcpy(buf, path), '/')[0] = 0;
    220  1.1       cjs 	(void) check_path(buf[0] ? buf : "/", &stbuf);
    221  1.1       cjs     }
    222  1.1       cjs     return (0);
    223  1.1       cjs }
    224