Home | History | Annotate | Line # | Download | only in tcpdchk
scaffold.c revision 1.10
      1  1.10    itojun /*	$NetBSD: scaffold.c,v 1.10 2003/07/14 09:07:22 itojun 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.6    itojun static char sccs_id[] = "@(#) scaffold.c 1.6 97/03/21 19:27:24";
     13   1.2  christos #else
     14  1.10    itojun __RCSID("$NetBSD: scaffold.c,v 1.10 2003/07/14 09:07:22 itojun 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.1       cjs  /*
     42   1.1       cjs   * These are referenced by the options module and by rfc931.c.
     43   1.1       cjs   */
     44   1.1       cjs int     allow_severity = SEVERITY;
     45   1.1       cjs int     deny_severity = LOG_WARNING;
     46   1.3  christos extern int rfc931_timeout; /* = RFC931_TIMEOUT; */
     47   1.1       cjs 
     48   1.1       cjs /* find_inet_addr - find all addresses for this host, result to free() */
     49   1.1       cjs 
     50   1.8    itojun struct addrinfo *find_inet_addr(host, flags)
     51   1.1       cjs char   *host;
     52   1.8    itojun int	flags;
     53   1.1       cjs {
     54   1.8    itojun     struct addrinfo hints, *res;
     55   1.8    itojun     int error;
     56   1.1       cjs 
     57   1.8    itojun     memset(&hints, 0, sizeof(hints));
     58   1.8    itojun     hints.ai_socktype = SOCK_DGRAM;
     59   1.8    itojun     hints.ai_flags = AI_CANONNAME | flags;
     60   1.8    itojun     error = getaddrinfo(host, "0", &hints, &res);
     61   1.8    itojun     if (error) {
     62   1.8    itojun 	tcpd_warn("%s: %s", host, gai_strerror(error));
     63   1.8    itojun 	return (0);
     64   1.1       cjs     }
     65   1.1       cjs 
     66   1.8    itojun     if (res->ai_canonname && STR_NE(host, res->ai_canonname)) {
     67   1.1       cjs 	tcpd_warn("%s: hostname alias", host);
     68   1.8    itojun 	tcpd_warn("(official name: %.*s)", STRING_LENGTH, res->ai_canonname);
     69   1.1       cjs     }
     70   1.8    itojun     return (res);
     71   1.1       cjs }
     72   1.1       cjs 
     73   1.1       cjs /* check_dns - give each address thorough workout, return address count */
     74   1.1       cjs 
     75   1.1       cjs int     check_dns(host)
     76   1.1       cjs char   *host;
     77   1.1       cjs {
     78   1.1       cjs     struct request_info request;
     79   1.8    itojun     struct sockaddr_storage ss;
     80   1.8    itojun     struct addrinfo *res0, *res;
     81   1.1       cjs     int     count;
     82   1.1       cjs 
     83   1.8    itojun     if ((res0 = find_inet_addr(host, 0)) == NULL)
     84   1.1       cjs 	return (0);
     85   1.8    itojun     memset(&ss, 0, sizeof(ss));
     86   1.8    itojun     request_init(&request, RQ_CLIENT_SIN, &ss, 0);
     87   1.1       cjs     sock_methods(&request);
     88   1.1       cjs 
     89   1.8    itojun     count = 0;
     90   1.8    itojun     for (res = res0; res; res = res->ai_next) {
     91   1.8    itojun 	count++;
     92   1.8    itojun 	if (res->ai_addrlen > sizeof(ss))
     93   1.8    itojun 	    continue;
     94   1.8    itojun 	memcpy(&ss, res->ai_addr, res->ai_addrlen);
     95   1.1       cjs 
     96   1.1       cjs 	/*
     97   1.1       cjs 	 * Force host name and address conversions. Use the request structure
     98   1.1       cjs 	 * as a cache. Detect hostname lookup problems. Any name/name or
     99   1.1       cjs 	 * name/address conflicts will be reported while eval_hostname() does
    100   1.1       cjs 	 * its job.
    101   1.1       cjs 	 */
    102   1.1       cjs 	request_set(&request, RQ_CLIENT_ADDR, "", RQ_CLIENT_NAME, "", 0);
    103   1.1       cjs 	if (STR_EQ(eval_hostname(request.client), unknown))
    104   1.1       cjs 	    tcpd_warn("host address %s->name lookup failed",
    105   1.1       cjs 		      eval_hostaddr(request.client));
    106   1.1       cjs     }
    107   1.8    itojun     freeaddrinfo(res0);
    108   1.1       cjs     return (count);
    109   1.1       cjs }
    110   1.1       cjs 
    111   1.1       cjs /* dummy function to intercept the real shell_cmd() */
    112   1.1       cjs 
    113   1.1       cjs /* ARGSUSED */
    114   1.1       cjs 
    115   1.1       cjs void    shell_cmd(command)
    116   1.1       cjs char   *command;
    117   1.1       cjs {
    118   1.1       cjs     if (hosts_access_verbose)
    119   1.1       cjs 	printf("command: %s", command);
    120   1.1       cjs }
    121   1.1       cjs 
    122   1.1       cjs /* dummy function  to intercept the real clean_exit() */
    123   1.1       cjs 
    124   1.1       cjs /* ARGSUSED */
    125   1.1       cjs 
    126   1.1       cjs void    clean_exit(request)
    127   1.1       cjs struct request_info *request;
    128   1.1       cjs {
    129   1.1       cjs     exit(0);
    130   1.1       cjs }
    131   1.1       cjs 
    132   1.2  christos #if 0
    133   1.1       cjs /* dummy function  to intercept the real rfc931() */
    134   1.1       cjs 
    135   1.1       cjs /* ARGSUSED */
    136   1.1       cjs 
    137   1.1       cjs void    rfc931(request)
    138   1.1       cjs struct request_info *request;
    139   1.1       cjs {
    140  1.10    itojun     strlcpy(request->user, unknown, sizeof(request->user));
    141   1.1       cjs }
    142   1.2  christos #endif
    143   1.1       cjs 
    144   1.1       cjs /* check_path - examine accessibility */
    145   1.1       cjs 
    146   1.1       cjs int     check_path(path, st)
    147   1.1       cjs char   *path;
    148   1.1       cjs struct stat *st;
    149   1.1       cjs {
    150   1.1       cjs     struct stat stbuf;
    151   1.1       cjs     char    buf[BUFSIZ];
    152   1.1       cjs 
    153   1.1       cjs     if (stat(path, st) < 0)
    154   1.1       cjs 	return (-1);
    155   1.1       cjs #ifdef notdef
    156   1.1       cjs     if (st->st_uid != 0)
    157   1.1       cjs 	tcpd_warn("%s: not owned by root", path);
    158   1.1       cjs     if (st->st_mode & 020)
    159   1.1       cjs 	tcpd_warn("%s: group writable", path);
    160   1.1       cjs #endif
    161   1.1       cjs     if (st->st_mode & 002)
    162   1.1       cjs 	tcpd_warn("%s: world writable", path);
    163   1.1       cjs     if (path[0] == '/' && path[1] != 0) {
    164  1.10    itojun 	strlcpy(buf, path, sizeof(buf));
    165  1.10    itojun 	strrchr(buf, '/')[0] = 0;
    166   1.1       cjs 	(void) check_path(buf[0] ? buf : "/", &stbuf);
    167   1.1       cjs     }
    168   1.1       cjs     return (0);
    169   1.1       cjs }
    170