/** Apply GeoIP country data to a client from a MARK. * @param[in] cptr Client to apply GeoIP country data to. * @param[in] country Country code from MARK. * @param[in] continent Continent code from MARK. */ void geoip_apply_mark(struct Client* cptr, char* country, char* continent) { ircd_strncpy((char *)&cli_countrycode(cptr), (!country ? "--" : country), 3); ircd_strncpy((char *)&cli_continentcode(cptr), (!continent ? "--" : continent), 3); #ifdef USE_GEOIP if (!country || !strcmp(country, "--")) #endif /* USE_GEOIP */ ircd_strncpy((char *)&cli_countryname(cptr), "Unknown", 8); #ifdef USE_GEOIP else ircd_strncpy((char *)&cli_countryname(cptr), GeoIP_name_by_id(GeoIP_id_by_code(country)), 256); if (!continent || !strcmp(continent, "--")) #endif /* USE_GEOIP */ ircd_strncpy((char *)&cli_continentname(cptr), "Unknown", 8); #ifdef USE_GEOIP else ircd_strncpy((char *)&cli_continentname(cptr), geoip_continent_name_by_code(continent), 256); #endif /* USE_GEOIP */ SetGeoIP(cptr); }
static int geoip_header_parser(request_rec * r) { char *orgorisp; char *ipaddr; char *free_me = NULL; short int country_id; const char *continent_code; const char *country_code; const char *country_name; const char *region_name; geoip_server_config_rec *cfg; unsigned char databaseType; GeoIPRecord *gir; GeoIPRegion *giregion; int i; int netspeed; /* For splitting proxy headers */ char *ipaddr_ptr = NULL; char *comma_ptr; cfg = ap_get_module_config(r->server->module_config, &geoip_module); if (!cfg) return DECLINED; if (!cfg->scanProxyHeaders) { ipaddr = _get_client_ip(r); } else { ap_add_common_vars(r); if (apr_table_get(r->subprocess_env, "HTTP_CLIENT_IP")) { ipaddr_ptr = (char *)apr_table_get(r->subprocess_env, "HTTP_CLIENT_IP"); } else if (apr_table_get(r->subprocess_env, "HTTP_X_FORWARDED_FOR")) { ipaddr_ptr = (char *)apr_table_get(r->subprocess_env, "HTTP_X_FORWARDED_FOR"); } else if (apr_table_get(r->headers_in, "X-Forwarded-For")) { ipaddr_ptr = (char *)apr_table_get(r->headers_in, "X-Forwarded-For"); } else if (apr_table_get(r->subprocess_env, "HTTP_REMOTE_ADDR")) { ipaddr_ptr = (char *)apr_table_get(r->subprocess_env, "HTTP_REMOTE_ADDR"); } if (!ipaddr_ptr) { ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server, "[mod_geoip]: Error while getting ipaddr from proxy headers. Using REMOTE_ADDR."); ipaddr = _get_client_ip(r); } else { /* Found XFF like header */ ap_log_error(APLOG_MARK, APLOG_DEBUG | APLOG_NOERRNO, 0, r->server, "[mod_geoip]: IPADDR_PTR: %s", ipaddr_ptr); if (cfg->proxyHeaderMode == GEOIP_PROXY_HEADER_MODE_FIRST_NON_PRIVATE_IP) { ipaddr = free_me = _get_ip_from_xff(ipaddr_ptr); if (!ipaddr) ipaddr = _get_client_ip(r); } else { ipaddr = free_me = (char *)calloc(8 * 4 + 7 + 1, sizeof(char)); /* proxyHeaderMode is * GEOIP_PROXY_HEADER_MODE_LAST_IP or GEOIP_PROXY_HEADER_MODE_FIRST_IP */ /* * Check to ensure that the HTTP_CLIENT_IP or * X-Forwarded-For header is not a comma separated * list of addresses, which would cause mod_geoip to * return no country code. If the header is a comma * separated list, return the first IP address in the * list, which is (hopefully!) the real client IP. */ if (cfg->proxyHeaderMode == GEOIP_PROXY_HEADER_MODE_LAST_IP) { comma_ptr = strrchr(ipaddr_ptr, ','); if (comma_ptr) { /* skip over whitespace */ ipaddr_ptr = comma_ptr + strspn(comma_ptr, ", \t"); } } strncpy(ipaddr, ipaddr_ptr, 8 * 4 + 7); comma_ptr = strchr(ipaddr, ','); if (comma_ptr != 0) *comma_ptr = '\0'; } } } /* this block should be removed! */ #if 1 if (!cfg->gips) { if (cfg->GeoIPFilenames != NULL) { cfg->gips = malloc(sizeof(GeoIP *) * cfg->numGeoIPFiles); for (i = 0; i < cfg->numGeoIPFiles; i++) { cfg->gips[i] = GeoIP_open(cfg->GeoIPFilenames[i], (cfg->GeoIPFlags2[i] == GEOIP_UNKNOWN) ? cfg->GeoIPFlags : cfg->GeoIPFlags2[i]); if (cfg->gips[i]) { if (cfg->GeoIPEnableUTF8) { GeoIP_set_charset(cfg->gips[i], GEOIP_CHARSET_UTF8); } } else { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "[mod_geoip]: Error while opening data file %s", cfg->GeoIPFilenames[i]); return DECLINED; } } } else { cfg->gips = malloc(sizeof(GeoIP *)); cfg->gips[0] = GeoIP_new(GEOIP_STANDARD); if (!cfg->gips[0]) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "[mod_geoip]: Error while opening data file"); return DECLINED; } cfg->numGeoIPFiles = 1; } } #endif set_geoip_output(cfg, r, "GEOIP_ADDR", ipaddr); for (i = 0; i < cfg->numGeoIPFiles; i++) { /* * skip database handles that can not be opned for some * reason */ if (cfg->gips[i] == NULL) continue; databaseType = cfg->gips[i] ? GeoIP_database_edition(cfg->gips[i]) : -1; /* -1 is "magic value" * in case file not * found */ switch (databaseType) { case GEOIP_NETSPEED_EDITION_REV1: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); setn_geoip_output(cfg, r, "GEOIP_NETSPEED", orgorisp); break; case GEOIP_NETSPEED_EDITION: netspeed = GeoIP_id_by_addr(cfg->gips[i], ipaddr); if (netspeed == GEOIP_UNKNOWN_SPEED) { netspeedstring = "unknown"; } else if (netspeed == GEOIP_DIALUP_SPEED) { netspeedstring = "dialup"; } else if (netspeed == GEOIP_CABLEDSL_SPEED) { netspeedstring = "cabledsl"; } else if (netspeed == GEOIP_CORPORATE_SPEED) { netspeedstring = "corporate"; } setn_geoip_output(cfg, r, "GEOIP_NETSPEED", netspeedstring); break; case GEOIP_COUNTRY_EDITION_V6: /* Get the Country ID */ country_id = GeoIP_id_by_addr_v6(cfg->gips[i], ipaddr); if (country_id > 0) { /* Lookup the Code and the Name with the ID */ continent_code = GeoIP_country_continent[country_id]; country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (cfg->numGeoIPFiles == 0) { cfg->numGeoIPFiles = 0; } if (cfg->GeoIPFilenames == 0) { cfg->GeoIPFilenames = 0; } /* Set it for our user */ setn_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE_V6", continent_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE_V6", country_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME_V6", country_name); } break; case GEOIP_COUNTRY_EDITION: /* Get the Country ID */ country_id = GeoIP_country_id_by_addr(cfg->gips[i], ipaddr); if (country_id > 0) { /* Lookup the Code and the Name with the ID */ continent_code = GeoIP_country_continent[country_id]; country_code = GeoIP_country_code[country_id]; country_name = GeoIP_country_name[country_id]; if (cfg->numGeoIPFiles == 0) { cfg->numGeoIPFiles = 0; } if (cfg->GeoIPFilenames == 0) { cfg->GeoIPFilenames = 0; } /* Set it for our user */ setn_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE", continent_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE", country_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME", country_name); } break; case GEOIP_REGION_EDITION_REV0: case GEOIP_REGION_EDITION_REV1: giregion = GeoIP_region_by_name(cfg->gips[i], ipaddr); if (giregion != NULL) { region_name = NULL; if (giregion->country_code[0]) { region_name = GeoIP_region_name_by_code (giregion->country_code, giregion->region); set_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE", giregion->country_code); country_id = GeoIP_id_by_code(giregion->country_code); setn_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME", GeoIP_country_name[country_id]); setn_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE", GeoIP_country_continent[country_id]); } if (giregion->region[0]) { set_geoip_output(cfg, r, "GEOIP_REGION", giregion->region); } if (region_name != NULL) { set_geoip_output(cfg, r, "GEOIP_REGION_NAME", region_name); } GeoIPRegion_delete(giregion); } break; case GEOIP_CITY_EDITION_REV0_V6: case GEOIP_CITY_EDITION_REV1_V6: gir = GeoIP_record_by_addr_v6(cfg->gips[i], ipaddr); if (gir != NULL) { if (gir->country_code != NULL) { region_name = GeoIP_region_name_by_code(gir->country_code, gir->region); } sprintf(metrocodestr, "%d", gir->dma_code); sprintf(areacodestr, "%d", gir->area_code); set_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE_V6", gir->continent_code); set_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE_V6", gir->country_code); set_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME_V6", gir->country_name); set_geoip_output(cfg, r, "GEOIP_REGION_V6", gir->region); set_geoip_output(cfg, r, "GEOIP_REGION_NAME_V6", region_name); set_geoip_output(cfg, r, "GEOIP_CITY_V6", gir->city); set_geoip_output(cfg, r, "GEOIP_DMA_CODE_V6", metrocodestr); set_geoip_output(cfg, r, "GEOIP_METRO_CODE_V6", metrocodestr); set_geoip_output(cfg, r, "GEOIP_AREA_CODE_V6", areacodestr); sprintf(latstr, "%f", gir->latitude); sprintf(lonstr, "%f", gir->longitude); set_geoip_output(cfg, r, "GEOIP_LATITUDE_V6", latstr); set_geoip_output(cfg, r, "GEOIP_LONGITUDE_V6", lonstr); set_geoip_output(cfg, r, "GEOIP_POSTAL_CODE_V6", gir->postal_code); GeoIPRecord_delete(gir); } break; case GEOIP_CITY_EDITION_REV0: case GEOIP_CITY_EDITION_REV1: gir = GeoIP_record_by_addr(cfg->gips[i], ipaddr); if (gir != NULL) { if (gir->country_code != NULL) { region_name = GeoIP_region_name_by_code(gir->country_code, gir->region); } sprintf(metrocodestr, "%d", gir->dma_code); sprintf(areacodestr, "%d", gir->area_code); set_geoip_output(cfg, r, "GEOIP_CONTINENT_CODE", gir->continent_code); set_geoip_output(cfg, r, "GEOIP_COUNTRY_CODE", gir->country_code); set_geoip_output(cfg, r, "GEOIP_COUNTRY_NAME", gir->country_name); set_geoip_output(cfg, r, "GEOIP_REGION", gir->region); set_geoip_output(cfg, r, "GEOIP_REGION_NAME", region_name); set_geoip_output(cfg, r, "GEOIP_CITY", gir->city); set_geoip_output(cfg, r, "GEOIP_DMA_CODE", metrocodestr); set_geoip_output(cfg, r, "GEOIP_METRO_CODE", metrocodestr); set_geoip_output(cfg, r, "GEOIP_AREA_CODE", areacodestr); sprintf(latstr, "%f", gir->latitude); sprintf(lonstr, "%f", gir->longitude); set_geoip_output(cfg, r, "GEOIP_LATITUDE", latstr); set_geoip_output(cfg, r, "GEOIP_LONGITUDE", lonstr); set_geoip_output(cfg, r, "GEOIP_POSTAL_CODE", gir->postal_code); GeoIPRecord_delete(gir); } break; case GEOIP_ORG_EDITION: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); setn_geoip_output(cfg, r, "GEOIP_ORGANIZATION", orgorisp); break; case GEOIP_ISP_EDITION: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); setn_geoip_output(cfg, r, "GEOIP_ISP", orgorisp); break; case GEOIP_DOMAIN_EDITION: orgorisp = GeoIP_name_by_addr(cfg->gips[i], ipaddr); setn_geoip_output(cfg, r, "GEOIP_DOMAIN", orgorisp); break; } } if (free_me) free(free_me); return OK; }