Project

General

Profile

Docs ModGeoip » mod_geoip_for_1.4.39.c

mod_geoip for lighttpd 1.4.39 - gstrauss, 2016-02-29 22:52

 
1
#include <GeoIP.h>
2
#include <GeoIPCity.h>
3

    
4
#include <ctype.h>
5
#include <stdlib.h>
6
#include <string.h>
7

    
8
#include "base.h"
9
#include "log.h"
10
#include "buffer.h"
11

    
12
#include "plugin.h"
13

    
14
#ifdef HAVE_CONFIG_H
15
#include "config.h"
16
#endif
17

    
18
/**
19
 * 
20
 * $mod_geoip.c (v2.0) (13.09.2006 00:29:11)
21
 *
22
 * Name:
23
 * 	mod_geoip.c
24
 *
25
 * Description:
26
 * 	GeoIP module (plugin) for lighttpd.
27
 *	the module loads a geoip database of type "country" or "city" and
28
 *	sets new ENV vars based on ip record lookups.
29
 *
30
 *	country db env's:
31
 *		GEOIP_COUNTRY_CODE
32
 *		GEOIP_COUNTRY_CODE3
33
 *		GEOIP_COUNTRY_NAME
34
 *	
35
 *	city db env's:
36
 *		GEOIP_COUNTRY_CODE
37
 *		GEOIP_COUNTRY_CODE3
38
 *		GEOIP_COUNTRY_NAME
39
 *		GEOIP_CITY_NAME
40
 *		GEOIP_CITY_POSTAL_CODE
41
 *		GEOIP_CITY_LATITUDE
42
 *		GEOIP_CITY_LONG_LATITUDE
43
 *		GEOIP_CITY_DMA_CODE
44
 *		GEOIP_CITY_AREA_CODE
45
 * 
46
 * Usage (configuration options):
47
 *	geoip.db-filename = <path to the geoip or geocity database>
48
 *	geoip.memory-cache = <enable|disable> : default disabled
49
 *		if enabled, mod_geoip will load the database binary file to
50
 *		memory for very fast lookups. the only penalty is memory usage.
51
 *
52
 * Author:
53
 * 	Ami E. Bizamcher (amix)
54
 *	duke.amix@gmail.com
55
 * 
56
 * Note:
57
 * 	GeoIP Library and API must be installed!
58
 */
59

    
60

    
61
/* plugin config for all request/connections */
62

    
63
typedef struct {
64
	unsigned short mem_cache;
65
	buffer	*db_name;
66
	GeoIP   *gi;
67
} plugin_config;
68

    
69
typedef struct {
70
	PLUGIN_DATA;
71
	
72
	plugin_config **config_storage;
73
	
74
	plugin_config conf; 
75
} plugin_data;
76

    
77
/* init the plugin data */
78
INIT_FUNC(mod_geoip_init) {
79
	plugin_data *p;
80
	
81
	p = calloc(1, sizeof(*p));
82
	
83
	return p;
84
}
85

    
86
/* destroy the plugin data */
87
FREE_FUNC(mod_geoip_free) {
88
	plugin_data *p = p_d;
89
	
90
	UNUSED(srv);
91

    
92
	if (!p) return HANDLER_GO_ON;
93
	
94
	if (p->config_storage) {
95
		size_t i;
96

    
97
		for (i = 0; i < srv->config_context->used; i++) {
98
			plugin_config *s = p->config_storage[i];
99

    
100
			if (!s) continue;
101
			
102
			buffer_free(s->db_name);
103
			
104
			/* clean up */
105
			if (s->gi) GeoIP_delete(s->gi);
106

    
107
			free(s);
108
		}
109
		free(p->config_storage);
110
	}
111

    
112
	free(p);
113
	
114
	return HANDLER_GO_ON;
115
}
116

    
117
/* handle plugin config and check values */
118

    
119
SETDEFAULTS_FUNC(mod_geoip_set_defaults) {
120
	plugin_data *p = p_d;
121
	size_t i = 0;
122
	
123
	config_values_t cv[] = { 
124
		{ "geoip.db-filename",	NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },	/* 0 */
125
		{ "geoip.memory-cache",	NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },    /* 1 */
126
		{ NULL,			NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
127
	};
128
	
129
	if (!p) return HANDLER_ERROR;
130
	
131
	p->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *));
132
	
133
	for (i = 0; i < srv->config_context->used; i++) {
134
		data_config const* config = (data_config const*)srv->config_context->data[i];
135
		plugin_config *s;
136
		int mode;
137
	
138
		s = calloc(1, sizeof(plugin_config));
139
		
140
		s->db_name = buffer_init();
141
		s->mem_cache = 0; /* default: do not load db to cache */
142
		s->gi = NULL;
143

    
144
		cv[0].destination = s->db_name;
145
		cv[1].destination = &(s->mem_cache);
146

    
147
		p->config_storage[i] = s;
148
	
149
		if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
150
			return HANDLER_ERROR;
151
		}
152

    
153
		mode = GEOIP_STANDARD | GEOIP_CHECK_CACHE;
154

    
155
		/* country db filename is requeried! */
156
		if (!buffer_is_empty(s->db_name)) {
157

    
158
			/* let's start cooking */
159
			if (s->mem_cache != 0)
160
				mode = GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE;
161

    
162
			if (NULL == (s->gi = GeoIP_open(s->db_name->ptr, mode))) {
163
				log_error_write(srv, __FILE__, __LINE__, "s",
164
					"failed to open GeoIP database!!!");
165

    
166
				return HANDLER_ERROR;
167
			}
168

    
169
			/* is the db supported ? */
170
			if (s->gi->databaseType != GEOIP_COUNTRY_EDITION &&
171
				s->gi->databaseType != GEOIP_CITY_EDITION_REV0 &&
172
				s->gi->databaseType != GEOIP_CITY_EDITION_REV1) {
173
				log_error_write(srv, __FILE__, __LINE__, "s",
174
					"GeoIP database is of unsupported type!!!");
175
			}
176
		}
177
	}
178
	
179
	return HANDLER_GO_ON;
180
}
181

    
182
#define PATCH(x) \
183
	p->conf.x = s->x;
184
static int mod_geoip_patch_connection(server *srv, connection *con, plugin_data *p) {
185
	size_t i, j;
186
	plugin_config *s = p->config_storage[0];
187
	
188
	PATCH(db_name);
189
	PATCH(mem_cache);
190
        PATCH(gi);
191

    
192
	/* skip the first, the global context */
193
	for (i = 1; i < srv->config_context->used; i++) {
194
		data_config *dc = (data_config *)srv->config_context->data[i];
195
		s = p->config_storage[i];
196

    
197
		/* condition didn't match */
198
		if (!config_check_cond(srv, con, dc)) continue;
199
		
200
		/* merge config */
201
		for (j = 0; j < dc->value->used; j++) {
202
			data_unset *du = dc->value->data[j];
203

    
204
			if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.db-filename"))) {
205
				PATCH(db_name);
206
			}
207

    
208
			if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.memory-cache"))) {
209
                                PATCH(mem_cache);
210
                        }
211
		}
212
	}
213
	
214
	return 0;
215
}
216
#undef PATCH
217

    
218
URIHANDLER_FUNC(mod_geoip_subrequest) {
219
        plugin_data *p = p_d;
220

    
221
        mod_geoip_patch_connection(srv, con, p);
222

    
223
        if (!buffer_is_empty(p->conf.db_name)) {
224
		const char *remote_ip;
225
		data_string *ds;
226
		GeoIPRecord *gir;
227
		const char *returnedCountry;
228

    
229
		remote_ip = con->dst_addr_buf->ptr;
230

    
231
		if (p->conf.gi->databaseType == GEOIP_COUNTRY_EDITION) {
232
			/* get the country code 2 chars */
233
			if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) {
234
				if (NULL != (returnedCountry = GeoIP_country_code_by_addr(p->conf.gi, remote_ip))) {
235
	        	        	if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
236
        	        	        	ds = data_string_init();
237
	        	        	}
238

    
239
					buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
240
					buffer_copy_string(ds->value, returnedCountry);
241
					array_insert_unique(con->environment, (data_unset *)ds);
242
				}
243
			}
244

    
245
			/* get the country code 3 chars */
246
			if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE3"))) {
247
				if (NULL != (returnedCountry = GeoIP_country_code3_by_addr(p->conf.gi, remote_ip))) {
248
					if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
249
						ds = data_string_init();
250
					}
251

    
252
					buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
253
					buffer_copy_string(ds->value, returnedCountry);
254
					array_insert_unique(con->environment, (data_unset *)ds);
255
				}
256
			}
257

    
258
			/* get the country name */
259
			if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_NAME"))) {
260
				if (NULL != (returnedCountry = GeoIP_country_name_by_addr(p->conf.gi, remote_ip))) {
261
					if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
262
						ds = data_string_init();
263
					}
264

    
265
					buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
266
					buffer_copy_string(ds->value, returnedCountry);
267
					array_insert_unique(con->environment, (data_unset *)ds);
268
				}
269
			}
270
			
271
			/* go on... */
272
			return HANDLER_GO_ON;
273
		}
274

    
275
		/* if we are here, geo city is in use */
276

    
277
		if (NULL != (gir = GeoIP_record_by_addr(p->conf.gi, remote_ip))) {
278
			/* get the country code 2 chars */
279
                        if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE"))) {
280
                                if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
281
                                        ds = data_string_init();
282
                                }
283

    
284
                                buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE");
285
                                buffer_copy_string(ds->value, gir->country_code);
286
                                array_insert_unique(con->environment, (data_unset *)ds);
287
                        }
288

    
289
			/* get the country code 3 chars */
290
			if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_CODE3"))) {
291
				if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
292
					ds = data_string_init();
293
				}
294

    
295
				buffer_copy_string(ds->key, "GEOIP_COUNTRY_CODE3");
296
				buffer_copy_string(ds->value, gir->country_code3);
297
				array_insert_unique(con->environment, (data_unset *)ds);
298
			}
299

    
300
			/* get the country name */
301
			if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_COUNTRY_NAME"))) {
302
				if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
303
					ds = data_string_init();
304
				}
305

    
306
				buffer_copy_string(ds->key, "GEOIP_COUNTRY_NAME");
307
				buffer_copy_string(ds->value, gir->country_name);
308
				array_insert_unique(con->environment, (data_unset *)ds);
309
			}
310

    
311
			/* get the city region */
312
                        if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_REGION"))) {
313
                                if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
314
                                        ds = data_string_init();
315
                                }
316

    
317
                                buffer_copy_string(ds->key, "GEOIP_CITY_REGION");
318
                                buffer_copy_string(ds->value, gir->region);
319
                                array_insert_unique(con->environment, (data_unset *)ds);
320
                        }
321

    
322
                        /* get the city */
323
                        if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_NAME"))) {
324
                                if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
325
                                        ds = data_string_init();
326
                                }
327

    
328
                                buffer_copy_string(ds->key, "GEOIP_CITY_NAME");
329
                                buffer_copy_string(ds->value, gir->city);
330
                                array_insert_unique(con->environment, (data_unset *)ds);
331
                        }
332

    
333
                        /* get the postal code */
334
                        if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_POSTAL_CODE"))) {
335
                                if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
336
                                        ds = data_string_init();
337
                                }
338

    
339
                                buffer_copy_string(ds->key, "GEOIP_CITY_POSTAL_CODE");
340
                                buffer_copy_string(ds->value, gir->postal_code);
341
                                array_insert_unique(con->environment, (data_unset *)ds);
342
                        }
343

    
344
			/* get the latitude */
345
			if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LATITUDE"))) {
346
				char latitude[32];
347
				if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
348
					ds = data_string_init();
349
				}
350

    
351
				snprintf(latitude, sizeof(latitude), "%f", gir->latitude);
352
				buffer_copy_string(ds->key, "GEOIP_CITY_LATITUDE");
353
				buffer_copy_string(ds->value, latitude);
354
				array_insert_unique(con->environment, (data_unset *)ds);
355
			}
356

    
357
			/* get the long latitude */
358
			if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LONG_LATITUDE"))) {
359
                                char long_latitude[32];
360
                                if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
361
                                        ds = data_string_init();
362
                                }
363

    
364
                                snprintf(long_latitude, sizeof(long_latitude), "%f", gir->longitude);
365
                                buffer_copy_string(ds->key, "GEOIP_CITY_LONG_LATITUDE");
366
                                buffer_copy_string(ds->value, long_latitude);
367
                                array_insert_unique(con->environment, (data_unset *)ds);
368
                        }
369

    
370
			/* get the dma code */
371
                        if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_DMA_CODE"))) {
372
                                char dc[5];
373
                                if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
374
                                        ds = data_string_init();
375
                                }
376

    
377
                                snprintf(dc, sizeof(dc), "%i", gir->dma_code);
378
                                buffer_copy_string(ds->key, "GEOIP_CITY_DMA_CODE");
379
                                buffer_copy_string(ds->value, dc);
380
                                array_insert_unique(con->environment, (data_unset *)ds);
381
                        }
382

    
383
			/* get the area code */
384
                        if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_AREA_CODE"))) {
385
                                char ac[5];
386
                                if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
387
                                        ds = data_string_init();
388
                                }
389

    
390
                                snprintf(ac, sizeof(ac), "%i", gir->area_code);
391
                                buffer_copy_string(ds->key, "GEOIP_CITY_AREA_CODE");
392
                                buffer_copy_string(ds->value, ac);
393
                                array_insert_unique(con->environment, (data_unset *)ds);
394
                        }
395

    
396
			GeoIPRecord_delete(gir);
397
		}		
398
	}
399

    
400
        /* keep walking... (johnnie walker style ;) */
401
        return HANDLER_GO_ON;
402
}
403

    
404
/* this function is called at dlopen() time and inits the callbacks */
405

    
406
int mod_geoip_plugin_init(plugin *p);
407
int mod_geoip_plugin_init(plugin *p) {
408
	p->version     = LIGHTTPD_VERSION_ID;
409
	p->name        = buffer_init_string("geoip");
410
	
411
	p->init        = mod_geoip_init;
412
	p->handle_subrequest_start = mod_geoip_subrequest;
413
	p->set_defaults  = mod_geoip_set_defaults;
414
	p->cleanup     = mod_geoip_free;
415
	
416
	p->data        = NULL;
417
	
418
	return 0;
419
}
(4-4/5)