Changeset 778 for branches


Ignore:
Timestamp:
10/02/06 00:41:20 (6 years ago)
Author:
wd
Message:
  • Add library get_address_type function
  • Fix CGI:IRC support to actually work with CGI:IRC's twisted hostname supply method. :)
Location:
branches/ithildin-1.1
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/ithildin-1.1/include/ithildin/socket.h

    r578 r778  
    144144#endif 
    145145 
     146/* Functions for handling network address type detection */ 
     147int get_address_type(const char *); 
     148 
    146149/* if we don't have a system getaddrinfo, use the ones that will be included 
    147150 * from source/contrib/gailib.c */ 
  • branches/ithildin-1.1/modules/ircd/addons/hostmask.c

    r777 r778  
    2626HOOK_FUNCTION(hostmask_conf_hook); 
    2727HOOK_FUNCTION(hostmask_cc_hook); 
     28static int validate_cgiirc_host(const char *, char **, char **); 
    2829 
    2930MODULE_LOADER(hostmask) { 
     
    7273HOOK_FUNCTION(hostmask_cc_hook) { 
    7374    client_t *cli = (client_t *)data; 
    74     char *mask; 
     75    char *mask, *ip; 
    7576 
    7677    if (cli->conn == NULL) 
    7778        return NULL; /* nothing to do */ 
    7879 
     80    mask = ip = NULL; 
     81 
    7982    mask = mdext(cli->conn->cls, class_hostmask_mdi); 
    8083    if (mask != NULL && *mask != '\0') { 
    8184        if (!strcasecmp(mask, CGI_IRC_SPECIAL_MASK)) { 
    82             /* Special CGI:IRC hack.  We take the password, if supplied, and 
    83              * use it as a hostname (as long as it's valid) */ 
     85            /* Special CGI:IRC hack.  We take the password, if supplied, an 
     86             * use it as a hostname (as long as it's valid).  Good stuff. */ 
    8487            if (cli->conn->pass != NULL && *cli->conn->pass != '\0' && 
    85                     istr_okay(ircd.maps.host, cli->conn->pass)) 
    86                 mask = cli->conn->pass; 
    87             else { 
     88                    validate_cgiirc_host(cli->conn->pass, &ip, &mask)) { 
     89                log_debug("cgi:irc connection masked as %s / %s", ip, mask); 
     90                strcpy(cli->ip, ip); /* set the IP as well */ 
     91            } else { 
    8892                log_warn("cgi:irc hostmasked connection provided invalid " 
    8993                        "host (%s) (%s!%s@%s)", 
     
    102106} 
    103107 
     108/* CGIIRC(6)+__(2)+\0(1)+ip+host */ 
     109#define CGIIRC_HOST_SIZE 9 + IPADDR_MAXLEN + HOSTLEN 
     110 
     111/* validate the string from a CGI:IRC connection.  The string is of the form 
     112 * CGIIRC_<ip>_<host>.  We ensure that both the IP and hostname are valid.  */ 
     113static int validate_cgiirc_host(const char *in, char **ip, char **host) { 
     114    static char str[CGIIRC_HOST_SIZE]; 
     115    char *s_ip, *s_host; 
     116 
     117    *ip = *host = NULL; 
     118    strlcpy(str, in, CGIIRC_HOST_SIZE); 
     119     
     120    if (strncmp(str, "CGIIRC_", 7)) 
     121        return 0; 
     122 
     123    s_ip = str + 7; 
     124    if ((s_host = strchr(s_ip, '_')) == NULL) 
     125        return 0; /* no hostname */ 
     126    *s_host++ = '\0'; 
     127 
     128    /* now we have ip/host, let's validate */ 
     129    if (*s_ip == '\0' || *s_host == '\0') 
     130        return 0; 
     131 
     132    if (get_address_type(s_ip) == PF_UNSPEC) 
     133        return 0; /* bad IP */ 
     134 
     135    if (strchr(s_host, '.') == NULL || !istr_okay(ircd.maps.host, s_host)) 
     136        return 0; /* bogus host */ 
     137 
     138    *ip = s_ip; 
     139    *host = s_host; 
     140    return 1; 
     141} 
     142 
    104143/* vi:set ts=8 sts=4 sw=4 tw=76 et: */ 
  • branches/ithildin-1.1/modules/ircd/commands/pass.c

    r772 r778  
    3939    if (conn->pass != NULL) 
    4040        free(conn->pass); /* dump whatever was there before.. */ 
    41     conn->pass = malloc(HOSTLEN + 1); 
    42     strlcpy(conn->pass, pass, HOSTLEN + 1); 
     41    conn->pass = strdup(pass); 
    4342} 
    4443 
  • branches/ithildin-1.1/source/socket.c

    r610 r778  
    10081008#endif 
    10091009 
     1010/* Function to determine the type of an address.  We do cheap best effort 
     1011 * here.  If it looks IPv6-y we try inet_pton with AF_INET6, if it looks 
     1012 * IPv4-y we try inet_pton with AF_INET, and as long as one succeeds we 
     1013 * return that type. */ 
     1014int get_address_type(const char *addr) { 
     1015    const char *s = addr; 
     1016    unsigned char buf[IPADDR_SIZE]; 
     1017 
     1018#ifdef INET6 
     1019    if ((s = strchr(addr, ':') != NULL) && strchr(s + 1, ':') != NULL) { 
     1020        /* either IPv6 or nothing, let's see */ 
     1021        if (inet_pton(PF_INET6, addr, buf) == 1) 
     1022            return PF_INET6; 
     1023        return PF_UNSPEC; 
     1024    } 
     1025#endif 
     1026 
     1027    if (inet_pton(PF_INET, addr, buf) == 1) 
     1028        return PF_INET; 
     1029    return PF_UNSPEC; 
     1030} 
     1031 
    10101032/* vi:set ts=8 sts=4 sw=4 tw=76 et: */ 
  • branches/ithildin-1.1/source/string.c

    r592 r778  
    647647    int imask = 0; 
    648648    int i; 
    649     char *s; 
     649    char *mask; 
    650650 
    651651    memset(bitmask, 0xff, IPADDR_SIZE); 
    652652    strlcpy(addr, wild, IPADDR_MAXLEN + 1); 
    653     /* First figure out what kind of address we've got.  We no longer support 
    654      * IPv4 mask form (ip:mask), but IPv6 addresses need at least two colons. 
    655      * If we find only one, just return 0 right away. */ 
    656     if ((s = strchr(wild, ':')) != NULL) { 
    657         if (strchr(s + 1, ':') != NULL) 
    658             family = PF_INET6; /* actually, inet6.. */ 
    659         else 
    660             return 0; /* no mask support */ 
    661     } 
    662  
    663     if ((s = strchr(wild, '/')) != NULL) { 
     653 
     654    /* strip off the mask portion if it is there */ 
     655    if ((mask = strchr(wild, '/')) != NULL) { 
    664656        /* mask form.  mask specifies significant bits from left to right. 
    665          * simple enough. */ 
    666         if (IPADDR_MAXLEN > s - wild) 
    667             addr[s - wild] = '\0'; 
    668         s++; 
    669         imask = str_conv_int(s, 32); 
     657         * we must make sure to truncate addr at the point where the mask 
     658         * starts as well, if it starts before IPADDR_MAXLEN. */ 
     659        if (IPADDR_MAXLEN > mask - wild) 
     660            addr[mask - wild] = '\0'; 
     661        mask++; 
     662    } 
     663 
     664    /* now determine the address family */ 
     665    family = get_address_type(addr); 
     666    if (family != PF_INET && family != PF_INET6) 
     667        return 0; 
     668 
     669    /* and handle the bitmasking if need-be */ 
     670    if (mask != NULL) { 
     671        imask = str_conv_int(mask, 0); 
     672 
    670673        if (family == PF_INET6) { 
    671674            if (imask <= 0) 
     
    684687            bitmask[(i - 1) / 8] = 0xff << (i % 8 ? (8 - (i % 8)) : 0); 
    685688    } 
    686     addr[IPADDR_MAXLEN] = '\0'; 
    687689 
    688690    /* now copy our addresses into bitmasks.  if either pton fails, assume no 
Note: See TracChangeset for help on using the changeset viewer.