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
|
/*
* Copyright (c) 2008-2024 OARC, Inc.
* Copyright (c) 2007-2008, Internet Systems Consortium, Inc.
* Copyright (c) 2003-2007, The Measurement Factory, Inc.
* 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. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS 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
* COPYRIGHT HOLDER OR 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.
*/
#include "config.h"
#include "country_index.h"
#include "xmalloc.h"
#include "hashtbl.h"
#include "syslog_debug.h"
#include "geoip.h"
#if defined(HAVE_LIBGEOIP) && defined(HAVE_GEOIP_H)
#define HAVE_GEOIP 1
#include <GeoIP.h>
#endif
#if defined(HAVE_LIBMAXMINDDB) && defined(HAVE_MAXMINDDB_H)
#define HAVE_MAXMINDDB 1
#include <maxminddb.h>
#endif
#ifdef HAVE_MAXMINDDB
#include "compat.h"
#endif
#ifdef HAVE_MAXMINDDB
#include <errno.h>
#endif
#include <string.h>
#include <strings.h>
#include <stdlib.h>
extern int debug_flag;
extern char* geoip_v4_dat;
extern int geoip_v4_options;
extern char* geoip_v6_dat;
extern int geoip_v6_options;
extern enum geoip_backend country_indexer_backend;
extern char* maxminddb_country;
static hashfunc country_hashfunc;
static hashkeycmp country_cmpfunc;
#define MAX_ARRAY_SZ 65536
static hashtbl* theHash = NULL;
static int next_idx = 0;
#ifdef HAVE_GEOIP
static GeoIP* geoip = NULL;
static GeoIP* geoip6 = NULL;
#endif
#ifdef HAVE_MAXMINDDB
static MMDB_s mmdb;
static char _mmcountry[32];
static int have_mmdb = 0;
#endif
static char ipstr[81];
static char* unknown = "??";
static char* unknown_v4 = "?4";
static char* unknown_v6 = "?6";
typedef struct
{
char* country;
int index;
} countryobj;
const char*
country_get_from_message(dns_message* m)
{
transport_message* tm = m->tm;
const char* cc = unknown;
if (country_indexer_backend == geoip_backend_libgeoip) {
if (!inXaddr_ntop(&tm->src_ip_addr, ipstr, sizeof(ipstr) - 1)) {
dfprint(0, "country_index: Error converting IP address");
return (unknown);
}
}
switch (tm->ip_version) {
case 4:
switch (country_indexer_backend) {
case geoip_backend_libgeoip:
#ifdef HAVE_GEOIP
if (geoip) {
cc = GeoIP_country_code_by_addr(geoip, ipstr);
if (cc == NULL) {
cc = unknown_v4;
}
}
#endif
break;
case geoip_backend_libmaxminddb:
#ifdef HAVE_MAXMINDDB
if (have_mmdb) {
struct sockaddr_in s;
int ret;
MMDB_lookup_result_s r;
s.sin_family = AF_INET;
s.sin_addr = tm->src_ip_addr.in4;
r = MMDB_lookup_sockaddr(&mmdb, (struct sockaddr*)&s, &ret);
if (ret == MMDB_SUCCESS && r.found_entry) {
MMDB_entry_data_s entry_data;
if (MMDB_get_value(&r.entry, &entry_data, "country", "iso_code", 0) == MMDB_SUCCESS
&& entry_data.type == MMDB_DATA_TYPE_UTF8_STRING) {
size_t len = entry_data.data_size > (sizeof(_mmcountry) - 1) ? (sizeof(_mmcountry) - 1) : entry_data.data_size;
memcpy(_mmcountry, entry_data.utf8_string, len);
_mmcountry[len] = 0;
cc = _mmcountry;
break;
}
}
}
cc = unknown_v4;
#endif
break;
default:
break;
}
break;
case 6:
switch (country_indexer_backend) {
case geoip_backend_libgeoip:
#ifdef HAVE_GEOIP
if (geoip6) {
cc = GeoIP_country_code_by_addr_v6(geoip6, ipstr);
if (cc == NULL) {
cc = unknown_v6;
}
}
#endif
break;
case geoip_backend_libmaxminddb:
#ifdef HAVE_MAXMINDDB
if (have_mmdb) {
struct sockaddr_in6 s;
int ret;
MMDB_lookup_result_s r;
s.sin6_family = AF_INET;
s.sin6_addr = tm->src_ip_addr.in6;
r = MMDB_lookup_sockaddr(&mmdb, (struct sockaddr*)&s, &ret);
if (ret == MMDB_SUCCESS && r.found_entry) {
MMDB_entry_data_s entry_data;
if (MMDB_get_value(&r.entry, &entry_data, "country", "iso_code", 0) == MMDB_SUCCESS
&& entry_data.type == MMDB_DATA_TYPE_UTF8_STRING) {
size_t len = entry_data.data_size > (sizeof(_mmcountry) - 1) ? (sizeof(_mmcountry) - 1) : entry_data.data_size;
memcpy(_mmcountry, entry_data.utf8_string, len);
_mmcountry[len] = 0;
cc = _mmcountry;
break;
}
}
}
cc = unknown_v6;
#endif
break;
default:
break;
}
break;
default:
break;
}
dfprintf(1, "country_index: country code: %s", cc);
return cc;
}
int country_indexer(const dns_message* m)
{
const char* country;
countryobj* obj;
if (m->malformed)
return -1;
country = country_get_from_message((dns_message*)m);
if (NULL == theHash) {
theHash = hash_create(MAX_ARRAY_SZ, country_hashfunc, country_cmpfunc, 1, afree, afree);
if (NULL == theHash)
return -1;
}
if ((obj = hash_find(country, theHash)))
return obj->index;
obj = acalloc(1, sizeof(*obj));
if (NULL == obj)
return -1;
obj->country = astrdup(country);
if (NULL == obj->country) {
afree(obj);
return -1;
}
obj->index = next_idx;
if (0 != hash_add(obj->country, obj, theHash)) {
afree(obj->country);
afree(obj);
return -1;
}
next_idx++;
return obj->index;
}
int country_iterator(const char** label)
{
countryobj* obj;
static char label_buf[MAX_QNAME_SZ];
if (0 == next_idx)
return -1;
if (NULL == label) {
/* initialize and tell caller how big the array is */
hash_iter_init(theHash);
return next_idx;
}
if ((obj = hash_iterate(theHash)) == NULL)
return -1;
snprintf(label_buf, sizeof(label_buf), "%s", obj->country);
*label = label_buf;
return obj->index;
}
void country_reset()
{
theHash = NULL;
next_idx = 0;
}
static unsigned int
country_hashfunc(const void* key)
{
return hashendian(key, strlen(key), 0);
}
static int
country_cmpfunc(const void* a, const void* b)
{
return strcasecmp(a, b);
}
void country_init(void)
{
switch (country_indexer_backend) {
case geoip_backend_libgeoip:
#ifdef HAVE_GEOIP
if (geoip_v4_dat) {
geoip = GeoIP_open(geoip_v4_dat, geoip_v4_options);
if (geoip == NULL) {
dsyslog(LOG_ERR, "country_index: Error opening IPv4 Country DB. Make sure libgeoip's GeoIP.dat file is available");
exit(1);
}
}
if (geoip_v6_dat) {
geoip6 = GeoIP_open(geoip_v6_dat, geoip_v6_options);
if (geoip6 == NULL) {
dsyslog(LOG_ERR, "country_index: Error opening IPv6 Country DB. Make sure libgeoip's GeoIPv6.dat file is available");
exit(1);
}
}
memset(ipstr, 0, sizeof(ipstr));
if (geoip || geoip6) {
dsyslog(LOG_INFO, "country_index: Sucessfully initialized GeoIP");
} else {
dsyslog(LOG_INFO, "country_index: No database loaded for GeoIP");
}
#endif
break;
case geoip_backend_libmaxminddb:
#ifdef HAVE_MAXMINDDB
if (maxminddb_country) {
int ret;
char errbuf[512];
ret = MMDB_open(maxminddb_country, 0, &mmdb);
if (ret == MMDB_IO_ERROR) {
dsyslogf(LOG_ERR, "country_index: Error opening MaxMind Country, IO error: %s", dsc_strerror(errno, errbuf, sizeof(errbuf)));
exit(1);
} else if (ret != MMDB_SUCCESS) {
dsyslogf(LOG_ERR, "country_index: Error opening MaxMind Country: %s", MMDB_strerror(ret));
exit(1);
}
dsyslog(LOG_INFO, "country_index: Sucessfully initialized MaxMind Country");
have_mmdb = 1;
} else {
dsyslog(LOG_INFO, "country_index: No database loaded for MaxMind Country");
}
#endif
break;
default:
break;
}
}
|