|
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
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
|
|
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
|
|
|
78
|
INIT_FUNC(mod_geoip_init) {
|
|
79
|
plugin_data *p;
|
|
80
|
|
|
81
|
p = calloc(1, sizeof(*p));
|
|
82
|
|
|
83
|
return p;
|
|
84
|
}
|
|
85
|
|
|
86
|
|
|
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
|
|
|
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
|
|
|
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 },
|
|
125
|
{ "geoip.memory-cache", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION },
|
|
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
|
plugin_config *s;
|
|
135
|
int mode;
|
|
136
|
|
|
137
|
s = calloc(1, sizeof(plugin_config));
|
|
138
|
|
|
139
|
s->db_name = buffer_init();
|
|
140
|
s->mem_cache = 0;
|
|
141
|
s->gi = NULL;
|
|
142
|
|
|
143
|
cv[0].destination = s->db_name;
|
|
144
|
cv[1].destination = &(s->mem_cache);
|
|
145
|
|
|
146
|
p->config_storage[i] = s;
|
|
147
|
|
|
148
|
if (0 != config_insert_values_global(srv, ((data_config *)srv->config_context->data[i])->value, cv)) {
|
|
149
|
return HANDLER_ERROR;
|
|
150
|
}
|
|
151
|
|
|
152
|
mode = GEOIP_STANDARD | GEOIP_CHECK_CACHE;
|
|
153
|
|
|
154
|
|
|
155
|
if (!buffer_is_empty(s->db_name)) {
|
|
156
|
|
|
157
|
|
|
158
|
if (s->mem_cache != 0)
|
|
159
|
mode = GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE;
|
|
160
|
|
|
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!!!");
|
|
164
|
|
|
165
|
return HANDLER_ERROR;
|
|
166
|
}
|
|
167
|
|
|
168
|
|
|
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!!!");
|
|
174
|
}
|
|
175
|
}
|
|
176
|
}
|
|
177
|
|
|
178
|
return HANDLER_GO_ON;
|
|
179
|
}
|
|
180
|
|
|
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];
|
|
186
|
|
|
187
|
PATCH(db_name);
|
|
188
|
PATCH(mem_cache);
|
|
189
|
PATCH(gi);
|
|
190
|
|
|
191
|
|
|
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];
|
|
195
|
|
|
196
|
|
|
197
|
if (!config_check_cond(srv, con, dc)) continue;
|
|
198
|
|
|
199
|
|
|
200
|
for (j = 0; j < dc->value->used; j++) {
|
|
201
|
data_unset *du = dc->value->data[j];
|
|
202
|
|
|
203
|
if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.db-filename"))) {
|
|
204
|
PATCH(db_name);
|
|
205
|
}
|
|
206
|
|
|
207
|
if (buffer_is_equal_string(du->key, CONST_STR_LEN("geoip.memory-cache"))) {
|
|
208
|
PATCH(mem_cache);
|
|
209
|
}
|
|
210
|
}
|
|
211
|
}
|
|
212
|
|
|
213
|
return 0;
|
|
214
|
}
|
|
215
|
#undef PATCH
|
|
216
|
|
|
217
|
URIHANDLER_FUNC(mod_geoip_subrequest) {
|
|
218
|
plugin_data *p = p_d;
|
|
219
|
|
|
220
|
mod_geoip_patch_connection(srv, con, p);
|
|
221
|
|
|
222
|
if (!buffer_is_empty(p->conf.db_name)) {
|
|
223
|
const char *remote_ip;
|
|
224
|
GeoIP *gi;
|
|
225
|
data_string *ds;
|
|
226
|
const char *returnedCountry;
|
|
227
|
|
|
228
|
remote_ip = inet_ntop_cache_get_ip(srv, &(con->dst_addr));
|
|
229
|
|
|
230
|
if (p->conf.gi->databaseType == GEOIP_COUNTRY_EDITION) {
|
|
231
|
|
|
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();
|
|
236
|
}
|
|
237
|
|
|
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);
|
|
241
|
}
|
|
242
|
}
|
|
243
|
|
|
244
|
|
|
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();
|
|
249
|
}
|
|
250
|
|
|
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);
|
|
254
|
}
|
|
255
|
}
|
|
256
|
|
|
257
|
|
|
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();
|
|
262
|
}
|
|
263
|
|
|
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);
|
|
267
|
}
|
|
268
|
}
|
|
269
|
|
|
270
|
|
|
271
|
return HANDLER_GO_ON;
|
|
272
|
}
|
|
273
|
|
|
274
|
|
|
275
|
GeoIPRecord * gir;
|
|
276
|
|
|
277
|
if (NULL != (gir = GeoIP_record_by_addr(p->conf.gi, remote_ip))) {
|
|
278
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
345
|
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LATITUDE"))) {
|
|
346
|
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
|
|
347
|
ds = data_string_init();
|
|
348
|
}
|
|
349
|
|
|
350
|
char latitude[32];
|
|
351
|
sprintf(&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
|
|
|
358
|
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_LONG_LATITUDE"))) {
|
|
359
|
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
|
|
360
|
ds = data_string_init();
|
|
361
|
}
|
|
362
|
|
|
363
|
char long_latitude[32];
|
|
364
|
sprintf(&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
|
|
|
371
|
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_DMA_CODE"))) {
|
|
372
|
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
|
|
373
|
ds = data_string_init();
|
|
374
|
}
|
|
375
|
|
|
376
|
char dc[5];
|
|
377
|
sprintf(&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
|
|
|
384
|
if (NULL == (ds = (data_string *)array_get_element(con->environment, "GEOIP_CITY_AREA_CODE"))) {
|
|
385
|
if (NULL == (ds = (data_string *)array_get_unused_element(con->environment, TYPE_STRING))) {
|
|
386
|
ds = data_string_init();
|
|
387
|
}
|
|
388
|
|
|
389
|
char ac[5];
|
|
390
|
sprintf(&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
|
}
|
|
397
|
|
|
398
|
|
|
399
|
return HANDLER_GO_ON;
|
|
400
|
}
|
|
401
|
|
|
402
|
|
|
403
|
|
|
404
|
int mod_geoip_plugin_init(plugin *p) {
|
|
405
|
p->version = LIGHTTPD_VERSION_ID;
|
|
406
|
p->name = buffer_init_string("geoip");
|
|
407
|
|
|
408
|
p->init = mod_geoip_init;
|
|
409
|
p->handle_subrequest_start = mod_geoip_subrequest;
|
|
410
|
p->set_defaults = mod_geoip_set_defaults;
|
|
411
|
p->cleanup = mod_geoip_free;
|
|
412
|
|
|
413
|
p->data = NULL;
|
|
414
|
|
|
415
|
return 0;
|
|
416
|
}
|