[mod_cgi] fix pipe_cloexec() when no O_CLOEXEC
[lighttpd.git] / src / mod_geoip.c
blob5c373551e2259cd294ac29cfb9875614ae8596ad
1 #include "first.h"
3 #include "plugin.h"
5 #ifdef HAVE_GEOIP
7 #include <GeoIP.h>
8 #include <GeoIPCity.h>
10 #include "base.h"
11 #include "log.h"
12 #include "buffer.h"
14 #include <stdlib.h>
15 #include <string.h>
17 /**
19 * $mod_geoip.c (v2.0) (13.09.2006 00:29:11)
21 * Name:
22 * mod_geoip.c
24 * Description:
25 * GeoIP module (plugin) for lighttpd.
26 * the module loads a geoip database of type "country" or "city" and
27 * sets new ENV vars based on ip record lookups.
29 * country db env's:
30 * GEOIP_COUNTRY_CODE
31 * GEOIP_COUNTRY_CODE3
32 * GEOIP_COUNTRY_NAME
34 * city db env's:
35 * GEOIP_COUNTRY_CODE
36 * GEOIP_COUNTRY_CODE3
37 * GEOIP_COUNTRY_NAME
38 * GEOIP_CITY_NAME
39 * GEOIP_CITY_POSTAL_CODE
40 * GEOIP_CITY_LATITUDE
41 * GEOIP_CITY_LONG_LATITUDE
42 * GEOIP_CITY_DMA_CODE
43 * GEOIP_CITY_AREA_CODE
45 * Usage (configuration options):
46 * geoip.db-filename = <path to the geoip or geocity database>
47 * geoip.memory-cache = <enable|disable> : default disabled
48 * if enabled, mod_geoip will load the database binary file to
49 * memory for very fast lookups. the only penalty is memory usage.
51 * Author:
52 * Ami E. Bizamcher (amix)
53 * duke.amix@gmail.com
55 * Note:
56 * GeoIP Library and API must be installed!
60 /* plugin config for all request/connections */
62 typedef struct {
63 unsigned short mem_cache;
64 buffer *db_name;
65 GeoIP *gi;
66 } plugin_config;
68 typedef struct {
69 PLUGIN_DATA;
71 plugin_config **config_storage;
73 plugin_config conf;
74 } plugin_data;
76 /* init the plugin data */
77 INIT_FUNC(mod_geoip_init) {
78 plugin_data *p;
80 p = calloc(1, sizeof(*p));
82 return p;
85 /* destroy the plugin data */
86 FREE_FUNC(mod_geoip_free) {
87 plugin_data *p = p_d;
89 UNUSED(srv);
91 if (!p) return HANDLER_GO_ON;
93 if (p->config_storage) {
94 size_t i;
96 for (i = 0; i < srv->config_context->used; i++) {
97 plugin_config *s = p->config_storage[i];
99 if (!s) continue;
101 buffer_free(s->db_name);
103 /* clean up */
104 if (s->gi) GeoIP_delete(s->gi);
106 free(s);
108 free(p->config_storage);
111 free(p);
113 return HANDLER_GO_ON;
116 /* handle plugin config and check values */
118 SETDEFAULTS_FUNC(mod_geoip_set_defaults) {
119 plugin_data *p = p_d;
120 size_t i = 0;
122 config_values_t cv[] = {
123 { "geoip.db-filename", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
124 { "geoip.memory-cache", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
125 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
128 if (!p) return HANDLER_ERROR;
130 p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
132 for (i = 0; i < srv->config_context->used; i++) {
133 data_config const* config = (data_config const*)srv->config_context->data[i];
134 plugin_config *s;
135 int mode;
137 s = calloc(1, sizeof(plugin_config));
139 s->db_name = buffer_init();
140 s->mem_cache = 0; /* default: do not load db to cache */
141 s->gi = NULL;
143 cv[0].destination = s->db_name;
144 cv[1].destination = &(s->mem_cache);
146 p->config_storage[i] = s;
148 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
149 return HANDLER_ERROR;
152 mode = GEOIP_STANDARD | GEOIP_CHECK_CACHE;
154 /* country db filename is requeried! */
155 if (!buffer_is_empty(s->db_name)) {
157 /* let's start cooking */
158 if (s->mem_cache != 0)
159 mode = GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE;
161 if (NULL == (s->gi = GeoIP_open(s->db_name->ptr, mode))) {
162 log_error_write(srv, __FILE__, __LINE__, "s",
163 "failed to open GeoIP database!!!");
165 return HANDLER_ERROR;
168 /* is the db supported ? */
169 if (s->gi->databaseType != GEOIP_COUNTRY_EDITION &&
170 s->gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
171 s->gi->databaseType != GEOIP_CITY_EDITION_REV1) {
172 log_error_write(srv, __FILE__, __LINE__, "s",
173 "GeoIP database is of unsupported type!!!");
178 return HANDLER_GO_ON;
181 #define PATCH(x) \
182 p->conf.x = s->x;
183 static int mod_geoip_patch_connection(server *srv, connection *con, plugin_data *p) {
184 size_t i, j;
185 plugin_config *s = p->config_storage[0];
187 PATCH(db_name);
188 PATCH(mem_cache);
189 PATCH(gi);
191 /* skip the first, the global context */
192 for (i = 1; i < srv->config_context->used; i++) {
193 data_config *dc = (data_config *)srv->config_context->data[i];
194 s = p->config_storage[i];
196 /* condition didn't match */
197 if (!config_check_cond(srv, con, dc)) continue;
199 /* merge config */
200 for (j = 0; j < dc->value->used; j++) {
201 data_unset *du = dc->value->data[j];
203 if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.db-filename"))) {
204 PATCH(db_name);
207 if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.memory-cache"))) {
208 PATCH(mem_cache);
213 return 0;
215 #undef PATCH
217 URIHANDLER_FUNC(mod_geoip_subrequest) {
218 plugin_data *p = p_d;
220 mod_geoip_patch_connection(srv, con, p);
222 if (!buffer_is_empty(p->conf.db_name)) {
223 const char *remote_ip;
224 data_string *ds;
225 GeoIPRecord *gir;
226 const char *returnedCountry;
228 remote_ip = con->dst_addr_buf->ptr;
230 if (p->conf.gi->databaseType == GEOIP_COUNTRY_EDITION) {
231 /* get the country code 2 chars */
232 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) {
233 if (NULL != (returnedCountry = GeoIP_country_code_by_addr(p->conf.gi, remote_ip))) {
234 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
235 ds = data_string_init();
238 buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
239 buffer_copy_string(ds->value, returnedCountry);
240 array_insert_unique(con->environment, (data_unset *)ds);
244 /* get the country code 3 chars */
245 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE3"))) {
246 if (NULL != (returnedCountry = GeoIP_country_code3_by_addr(p->conf.gi, remote_ip))) {
247 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
248 ds = data_string_init();
251 buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
252 buffer_copy_string(ds->value, returnedCountry);
253 array_insert_unique(con->environment, (data_unset *)ds);
257 /* get the country name */
258 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_NAME"))) {
259 if (NULL != (returnedCountry = GeoIP_country_name_by_addr(p->conf.gi, remote_ip))) {
260 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
261 ds = data_string_init();
264 buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
265 buffer_copy_string(ds->value, returnedCountry);
266 array_insert_unique(con->environment, (data_unset *)ds);
270 /* go on... */
271 return HANDLER_GO_ON;
274 /* if we are here, geo city is in use */
276 if (NULL != (gir = GeoIP_record_by_addr(p->conf.gi, remote_ip))) {
277 /* get the country code 2 chars */
278 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) {
279 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
280 ds = data_string_init();
283 buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
284 buffer_copy_string(ds->value, gir->country_code);
285 array_insert_unique(con->environment, (data_unset *)ds);
288 /* get the country code 3 chars */
289 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE3"))) {
290 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
291 ds = data_string_init();
294 buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
295 buffer_copy_string(ds->value, gir->country_code3);
296 array_insert_unique(con->environment, (data_unset *)ds);
299 /* get the country name */
300 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_NAME"))) {
301 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
302 ds = data_string_init();
305 buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
306 buffer_copy_string(ds->value, gir->country_name);
307 array_insert_unique(con->environment, (data_unset *)ds);
310 /* get the city region */
311 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_REGION"))) {
312 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
313 ds = data_string_init();
316 buffer_copy_string(ds->key, "GEOIP_CITY_REGION");
317 buffer_copy_string(ds->value, gir->region);
318 array_insert_unique(con->environment, (data_unset *)ds);
321 /* get the city */
322 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_NAME"))) {
323 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
324 ds = data_string_init();
327 buffer_copy_string(ds->key, "GEOIP_CITY_NAME");
328 buffer_copy_string(ds->value, gir->city);
329 array_insert_unique(con->environment, (data_unset *)ds);
332 /* get the postal code */
333 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_POSTAL_CODE"))) {
334 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
335 ds = data_string_init();
338 buffer_copy_string(ds->key, "GEOIP_CITY_POSTAL_CODE");
339 buffer_copy_string(ds->value, gir->postal_code);
340 array_insert_unique(con->environment, (data_unset *)ds);
343 /* get the latitude */
344 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LATITUDE"))) {
345 char latitude[32];
346 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
347 ds = data_string_init();
350 snprintf(latitude, sizeof(latitude), "%f", gir->latitude);
351 buffer_copy_string(ds->key, "GEOIP_CITY_LATITUDE");
352 buffer_copy_string(ds->value, latitude);
353 array_insert_unique(con->environment, (data_unset *)ds);
356 /* get the long latitude */
357 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LONG_LATITUDE"))) {
358 char long_latitude[32];
359 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
360 ds = data_string_init();
363 snprintf(long_latitude, sizeof(long_latitude), "%f", gir->longitude);
364 buffer_copy_string(ds->key, "GEOIP_CITY_LONG_LATITUDE");
365 buffer_copy_string(ds->value, long_latitude);
366 array_insert_unique(con->environment, (data_unset *)ds);
369 /* get the dma code */
370 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_DMA_CODE"))) {
371 char dc[5];
372 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
373 ds = data_string_init();
376 snprintf(dc, sizeof(dc), "%i", gir->dma_code);
377 buffer_copy_string(ds->key, "GEOIP_CITY_DMA_CODE");
378 buffer_copy_string(ds->value, dc);
379 array_insert_unique(con->environment, (data_unset *)ds);
382 /* get the area code */
383 if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_AREA_CODE"))) {
384 char ac[5];
385 if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
386 ds = data_string_init();
389 snprintf(ac, sizeof(ac), "%i", gir->area_code);
390 buffer_copy_string(ds->key, "GEOIP_CITY_AREA_CODE");
391 buffer_copy_string(ds->value, ac);
392 array_insert_unique(con->environment, (data_unset *)ds);
395 GeoIPRecord_delete(gir);
399 /* keep walking... (johnnie walker style ;) */
400 return HANDLER_GO_ON;
403 /* this function is called at dlopen() time and inits the callbacks */
405 int mod_geoip_plugin_init(plugin *p);
406 int mod_geoip_plugin_init(plugin *p) {
407 p->version = LIGHTTPD_VERSION_ID;
408 p->name = buffer_init_string("geoip");
410 p->init = mod_geoip_init;
411 p->handle_subrequest_start = mod_geoip_subrequest;
412 p->set_defaults = mod_geoip_set_defaults;
413 p->cleanup = mod_geoip_free;
415 p->data = NULL;
417 return 0;
420 #else
422 int mod_geoip_plugin_init(plugin *p);
423 int mod_geoip_plugin_init(plugin *p) {
424 UNUSED(p);
425 return -1;
428 #endif