forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoip2.c
404 lines (343 loc) · 11.4 KB
/
geoip2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/**
* geoip2.c -- implementation of GeoIP2
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2020 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_LIBMAXMINDDB
#include <maxminddb.h>
#endif
#include "geoip1.h"
#include "error.h"
#include "labels.h"
#include "util.h"
#include "xmalloc.h"
/* should be reused across lookups */
static int geoip_asn_type = 0;
static int geoip_city_type = 0;
static int geoip_country_type = 0;
static int mmdb_cnt = 0;
static MMDB_s *mmdbs = NULL;
/* Determine if we have a valid geoip resource.
*
* If the geoip resource is NULL, 0 is returned.
* If the geoip resource is valid and malloc'd, 1 is returned. */
int
is_geoip_resource (void) {
return mmdbs && mmdb_cnt;
}
/* Free up GeoIP resources */
void
geoip_free (void) {
int idx = 0;
if (!is_geoip_resource ())
return;
for (idx = 0; idx < mmdb_cnt; idx++)
MMDB_close (&mmdbs[idx]);
free (mmdbs);
mmdbs = NULL;
}
static void
set_geoip (const char *db) {
int status = 0;
MMDB_s *new_mmdbs = NULL, mmdb;
if (db == NULL || *db == '\0')
return;
if ((status = MMDB_open (db, MMDB_MODE_MMAP, &mmdb)) != MMDB_SUCCESS)
FATAL ("Unable to open GeoIP2 database %s: %s\n", db, MMDB_strerror (status));
mmdb_cnt++;
new_mmdbs = realloc (mmdbs, sizeof (*mmdbs) * mmdb_cnt);
if (new_mmdbs == NULL)
FATAL ("Unable to realloc GeoIP2 database %s\n", db);
mmdbs = new_mmdbs;
mmdbs[mmdb_cnt - 1] = mmdb;
if (strstr (mmdb.metadata.database_type, "-City") != NULL)
conf.has_geocountry = conf.has_geocity = geoip_country_type = geoip_city_type = 1;
if (strstr (mmdb.metadata.database_type, "-ASN") != NULL)
conf.has_geoasn = geoip_asn_type = 1;
if (strstr (mmdb.metadata.database_type, "-Country") != NULL)
conf.has_geocountry = geoip_country_type = 1;
}
/* Open the given GeoIP2 database.
*
* On error, it aborts.
* On success, a new geolocation structure is set. */
void
init_geoip (void) {
int i;
for (i = 0; i < conf.geoip_db_idx; ++i)
set_geoip (conf.geoip_databases[i]);
}
/* Look up an IP address that is passed in as a null-terminated string.
*
* On error, it aborts.
* If no entry is found, 1 is returned.
* On success, MMDB_lookup_result_s struct is set and 0 is returned. */
static int
geoip_lookup (MMDB_lookup_result_s *res, const char *ip, int is_asn) {
int gai_err, mmdb_err, idx = 0;
MMDB_s *mmdb = NULL;
for (idx = 0; idx < mmdb_cnt; idx++) {
if (is_asn && (!strstr (mmdbs[idx].metadata.database_type, "ASN")))
continue;
if (!is_asn && (strstr (mmdbs[idx].metadata.database_type, "ASN")))
continue;
mmdb = &mmdbs[idx];
*res = MMDB_lookup_string (mmdb, ip, &gai_err, &mmdb_err);
if (0 != gai_err)
return 1;
if (MMDB_SUCCESS != mmdb_err)
FATAL ("Error from libmaxminddb: %s\n", MMDB_strerror (mmdb_err));
if (!(*res).found_entry)
return 1;
break;
}
return 0;
}
/* Get continent name concatenated with code.
*
* If continent not found, "Unknown" is returned.
* On success, the continent code & name is returned . */
static const char *
get_continent_name_and_code (const char *continentid) {
if (memcmp (continentid, "NA", 2) == 0)
return "NA North America";
else if (memcmp (continentid, "OC", 2) == 0)
return "OC Oceania";
else if (memcmp (continentid, "EU", 2) == 0)
return "EU Europe";
else if (memcmp (continentid, "SA", 2) == 0)
return "SA South America";
else if (memcmp (continentid, "AF", 2) == 0)
return "AF Africa";
else if (memcmp (continentid, "AN", 2) == 0)
return "AN Antarctica";
else if (memcmp (continentid, "AS", 2) == 0)
return "AS Asia";
else
return "-- Unknown";
}
/* Compose a string with the country name and code and store it in the
* given buffer. */
static void
geoip_set_country (const char *country, const char *code, char *loc) {
if (country && code)
snprintf (loc, COUNTRY_LEN, "%s %s", code, country);
else
snprintf (loc, COUNTRY_LEN, "%s", "Unknown");
}
/* Compose a string with the ASN name and code and store it in the
* given buffer. */
static void
geoip_set_asn (MMDB_entry_data_s name, MMDB_entry_data_s code, char *asn, int status) {
if (status == 0)
snprintf (asn, ASN_LEN, "%05u: %.*s", code.uint32, name.data_size, name.utf8_string);
else
snprintf (asn, ASN_LEN, "%s", "00000: Unknown");
}
/* Compose a string with the city name and state/region and store it
* in the given buffer. */
static void
geoip_set_city (const char *city, const char *region, char *loc) {
snprintf (loc, CITY_LEN, "%s, %s", city ? city : "N/A City", region ? region : "N/A Region");
}
/* Compose a string with the continent name and store it in the given
* buffer. */
static void
geoip_set_continent (const char *continent, char *loc) {
if (continent)
snprintf (loc, CONTINENT_LEN, "%s", get_continent_name_and_code (continent));
else
snprintf (loc, CONTINENT_LEN, "%s", "Unknown");
}
/* A wrapper to fetch the looked up result set.
*
* On error, it aborts.
* If no data is found, NULL is returned.
* On success, the fetched value is returned. */
static char *
get_value (MMDB_lookup_result_s res, ...) {
MMDB_entry_data_s entry_data;
char *value = NULL;
int status = 0;
va_list keys;
va_start (keys, res);
status = MMDB_vget_value (&res.entry, &entry_data, keys);
va_end (keys);
if (status != MMDB_SUCCESS)
return NULL;
if (!entry_data.has_data)
return NULL;
if (entry_data.type != MMDB_DATA_TYPE_UTF8_STRING)
FATAL ("Invalid data UTF8 GeoIP2 data %d:\n", entry_data.type);
if ((value = strndup (entry_data.utf8_string, entry_data.data_size)) == NULL)
FATAL ("Unable to allocate buffer %s: ", strerror (errno));
return value;
}
/* A wrapper to fetch the looked up result and set the city and region.
*
* If no data is found, NULL is set.
* On success, the fetched value is set. */
static void
geoip_query_city (MMDB_lookup_result_s res, char *location) {
char *city = NULL, *region = NULL;
if (res.found_entry) {
city = get_value (res, "city", "names", DOC_LANG, NULL);
region = get_value (res, "subdivisions", "0", "names", DOC_LANG, NULL);
if (!city) {
city = get_value (res, "city", "names", "en", NULL);
}
if (!region) {
region = get_value (res, "subdivisions", "0", "names", "en", NULL);
}
}
geoip_set_city (city, region, location);
free (city);
free (region);
}
/* A wrapper to fetch the looked up result and set the country and code.
*
* If no data is found, NULL is set.
* On success, the fetched value is set. */
static void
geoip_query_country (MMDB_lookup_result_s res, char *location) {
char *country = NULL, *code = NULL;
if (res.found_entry) {
country = get_value (res, "country", "names", DOC_LANG, NULL);
code = get_value (res, "country", "iso_code", NULL);
if (!country) {
country = get_value (res, "country", "names", "en", NULL);
}
}
geoip_set_country (country, code, location);
free (code);
free (country);
}
/* Sets the value for the given array of strings as the lookup path.
*
* On error or not found, 1 is returned.
* On success, 0 is returned and the entry data is set. */
static int
geoip_query_asn_code (MMDB_lookup_result_s res, MMDB_entry_data_s *code) {
int status;
const char *key[] = { "autonomous_system_number", NULL };
if (res.found_entry) {
status = MMDB_aget_value (&res.entry, code, key);
if (status != MMDB_SUCCESS || !code->has_data)
return 1;
}
return 0;
}
/* Sets the value for the given array of strings as the lookup path.
*
* On error or not found, 1 is returned.
* On success, 0 is returned and the entry data is set. */
static int
geoip_query_asn_name (MMDB_lookup_result_s res, MMDB_entry_data_s *name) {
int status;
const char *key[] = { "autonomous_system_organization", NULL };
if (res.found_entry) {
status = MMDB_aget_value (&res.entry, name, key);
if (status != MMDB_SUCCESS || !name->has_data)
return 1;
}
return 0;
}
/* A wrapper to fetch the looked up result and set the ASN organization & code.
*
* If no data is found, "Unknown" is set.
* On success, the fetched value is set. */
void
geoip_asn (char *host, char *asn) {
MMDB_lookup_result_s res = { 0 };
MMDB_entry_data_s name = { 0 };
MMDB_entry_data_s code = { 0 };
int status = 1;
geoip_lookup (&res, host, 1);
if (!res.found_entry)
goto out;
if ((status &= geoip_query_asn_name (res, &name)))
goto out;
if ((status |= geoip_query_asn_code (res, &code)))
goto out;
out:
geoip_set_asn (name, code, asn, status);
}
/* A wrapper to fetch the looked up result and set the continent code.
*
* If no data is found, NULL is set.
* On success, the fetched value is set. */
static void
geoip_query_continent (MMDB_lookup_result_s res, char *location) {
char *code = NULL;
if (res.found_entry)
code = get_value (res, "continent", "code", NULL);
geoip_set_continent (code, location);
free (code);
}
/* Set country data by record into the given `location` buffer */
void
geoip_get_country (const char *ip, char *location, GO_UNUSED GTypeIP type_ip) {
MMDB_lookup_result_s res = { 0 };
geoip_lookup (&res, ip, 0);
geoip_query_country (res, location);
}
/* A wrapper to fetch the looked up result and set the continent. */
void
geoip_get_continent (const char *ip, char *location, GO_UNUSED GTypeIP type_ip) {
MMDB_lookup_result_s res = { 0 };
geoip_lookup (&res, ip, 0);
geoip_query_continent (res, location);
}
/* Entry point to set GeoIP location into the corresponding buffers,
* (continent, country, city).
*
* On error, 1 is returned
* On success, buffers are set and 0 is returned */
int
set_geolocation (char *host, char *continent, char *country, char *city, char *asn) {
MMDB_lookup_result_s res = { 0 };
if (!is_geoip_resource ())
return 1;
/* set ASN data */
if (geoip_asn_type)
geoip_asn (host, asn);
if (!geoip_city_type && !geoip_country_type)
return 0;
/* set Country/City data */
geoip_lookup (&res, host, 0);
geoip_query_country (res, country);
geoip_query_continent (res, continent);
if (geoip_city_type)
geoip_query_city (res, city);
return 0;
}