/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 MaxMind.com.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        MaxMind (http://www.maxmind.com/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "MaxMind" and "GeoIP" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact support@maxmind.com.
 *
 * 5. Products derived from this software may not be called "GeoIP",
 *    nor may "MaxMind" appear in their name, without prior written
 *    permission of the MaxMind.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 */

/*
 * Module definition information - the part between the -START and -END
 * lines below is used by Configure. This could be stored in a separate
 * instead.
 *
 * MODULE-DEFINITION-START
 * Name: geoip_module
 * ConfigStart
     GEOIP_LIB="-L/usr/local/lib -lGeoIP"
     if [ "X$GEOIP_LIB" != "X" ]; then
         LIBS="$LIBS $GEOIP_LIB"
         echo " + using $GEOIP_LIB for GeoIP support"
     fi
 * ConfigEnd
 * MODULE-DEFINITION-END
 */

/* geoip module
 *
 * Version 0.2.0
 *
 * This module sets an environment variable to the remote country
 * based on the requestor's IP address.  It uses the GeoIP library
 * to lookup the country by IP address.
 *
 * Copyright 2002, MaxMind.com
 * June 26th, 2002

To use the module you have to compile it into the frontend part of
your server, I usually copy the module to apache-1.3/src/modules/extra/
and use APACI like:

  ./configure --prefix=/usr/local/apache \
     --activate-module=src/modules/extra/mod_geoip.c \
     [... more apaci options ...]    

You should also be able to compile and use this module as a
dynamically loaded module (DSO).
 
 */


#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "GeoIP.h"

typedef struct {
  GeoIP *gip;
  char *GeoIPFilename;
  int GeoIPEnable;
} geoip_server_cfg;

module MODULE_VAR_EXPORT geoip_module;

module MODULE_VAR_EXPORT proxy_add_uri_module;

static void *geoip_server_config(pool *p, server_rec *s) {
    geoip_server_cfg *cfg = ap_pcalloc(p, sizeof(geoip_server_cfg));
    if (!cfg)
        return NULL;

    cfg->gip = NULL;
    cfg->GeoIPFilename = NULL;
    cfg->GeoIPEnable = 0;
    return (void *)cfg;
}

static const char *geoip_enable(cmd_parms *cmd, void *dummy, int flag) {
  server_rec *s = cmd->server;
  geoip_server_cfg *cfg = (geoip_server_cfg *)ap_get_module_config(s->module_config, &geoip_module);
  cfg->GeoIPEnable = flag;
  return NULL;
}

static const char *geoip_set_filename(cmd_parms *cmd, void *dummy, char *GeoIPFilename) {
  server_rec *s = cmd->server;
  geoip_server_cfg *cfg = (geoip_server_cfg *)ap_get_module_config(s->module_config, &geoip_module);
  cfg->GeoIPFilename = GeoIPFilename;
  return NULL;
}

static command_rec geoip_cmds[] = {
  { "GeoIPDBFile", geoip_set_filename, NULL,
    OR_ALL, TAKE1, "GeoIP Data File" },
  { "GeoIPEnable", geoip_enable, NULL,
    OR_ALL, FLAG, "turn on mod_geoip" },
  { NULL }
};

static int geoip_post_read_request (request_rec *r) {
  geoip_server_cfg *cfg = (geoip_server_cfg *)ap_get_module_config(r->server->module_config, &geoip_module);
  char *ipaddr;
  short int country_id;
  const char *country_code, *country_name;

  if(!cfg->GeoIPEnable)
    return DECLINED;

  ipaddr = r->connection->remote_ip;
  if(!ipaddr)
    return DECLINED;

  if(!cfg->gip) {
    if(cfg->GeoIPFilename != NULL) {
      cfg->gip = GeoIP_open(cfg->GeoIPFilename, GEOIP_STANDARD);
    } else {
      cfg->gip = GeoIP_new(GEOIP_STANDARD);
    }
    if(!cfg->gip) {
      ap_log_error(APLOG_MARK, APLOG_ERR, r->server, "[mod_geoip]: Error while opening data file");
      return DECLINED;
    }
  }

  country_id = GeoIP_country_id_by_addr(cfg->gip, ipaddr);
  country_code = GeoIP_country_code[country_id];
  country_name = GeoIP_country_name[country_id];
  ap_table_set(r->notes, "GEOIP_COUNTRY_CODE", country_code);
  ap_table_set(r->notes, "GEOIP_COUNTRY_NAME", country_name);
  ap_table_set(r->subprocess_env, "GEOIP_COUNTRY_CODE", country_code);
  ap_table_set(r->subprocess_env, "GEOIP_COUNTRY_NAME", country_name);
  return OK;
}

static void geoip_child_exit(server_rec *r, pool *p) {
  geoip_server_cfg *cfg = (geoip_server_cfg *)ap_get_module_config(r->module_config, &geoip_module);
  if(cfg->gip != NULL)
    GeoIP_delete(cfg->gip);
}

module MODULE_VAR_EXPORT geoip_module = {
    STANDARD_MODULE_STUFF,
    NULL,                       /* initializer */
    NULL,                       /* dir config creater */
    NULL,                       /* dir merger --- default is to override */
    geoip_server_config,        /* server config */
    NULL,                       /* merge server configs */
    geoip_cmds,                 /* command table */
    NULL,                       /* handlers */
    NULL,                       /* filename translation */
    NULL,                       /* check_user_id */
    NULL,                       /* check auth */
    NULL,                       /* check access */
    NULL,                       /* type_checker */
    NULL,                       /* fixups */
    NULL,                       /* logger */
    NULL,                       /* header parser */
    NULL,                       /* child_init */
    geoip_child_exit,           /* child_exit */
    geoip_post_read_request,    /* post read-request */
};
