-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_geoip2.c
1438 lines (1152 loc) · 38.3 KB
/
mod_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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ProFTPD: mod_geoip2 -- a module for looking up country/city/etc for clients
* Copyright (c) 2019-2023 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*
* This is mod_geoip2, contrib software for proftpd 1.3.x and above.
* For more information contact TJ Saunders <tj@castaglia.org>.
*
* --- DO NOT DELETE BELOW THIS LINE ----
* $Libraries: -lmaxminddb$
*/
#include "conf.h"
#include "privs.h"
/* A lot of ideas for this module were liberally borrowed from the mod_geoip
* module for Apache.
*/
#define MOD_GEOIP2_VERSION "mod_geoip2/0.1.1"
/* Make sure the version of proftpd is as necessary. */
#if PROFTPD_VERSION_NUMBER < 0x0001030402
# error "ProFTPD 1.3.4rc2 or later required"
#endif
#include <maxminddb.h>
module geoip2_module;
static int geoip2_engine = FALSE;
static int geoip2_logfd = -1;
static pool *geoip2_pool = NULL;
static array_header *geoip2_mmdbs = NULL;
/* The types of data that GeoIP can provide, and that we care about. */
static const char *geoip_city = NULL;
static const char *geoip_postal_code = NULL;
static const char *geoip_latitude = NULL;
static const char *geoip_longitude = NULL;
static const char *geoip_org = NULL;
static const char *geoip_country_code2 = NULL;
static const char *geoip_country_name = NULL;
static const char *geoip_region_code = NULL;
static const char *geoip_region_name = NULL;
static const char *geoip_continent_code = NULL;
static const char *geoip_asn = NULL;
static const char *geoip_timezone = NULL;
/* Names of supported GeoIP values */
#define GEOIP_FILTER_KEY_COUNTRY_CODE 100
#define GEOIP_FILTER_KEY_COUNTRY_NAME 101
#define GEOIP_FILTER_KEY_REGION_CODE 102
#define GEOIP_FILTER_KEY_REGION_NAME 103
#define GEOIP_FILTER_KEY_CONTINENT 104
#define GEOIP_FILTER_KEY_ORGANIZATION 105
#define GEOIP_FILTER_KEY_CITY 106
#define GEOIP_FILTER_KEY_POSTAL_CODE 107
#define GEOIP_FILTER_KEY_LATITUDE 108
#define GEOIP_FILTER_KEY_LONGITUDE 109
#define GEOIP_FILTER_KEY_ASN 110
#define GEOIP_FILTER_KEY_TIMEZONE 111
struct geoip_filter_key {
const char *filter_name;
int filter_id;
};
static struct geoip_filter_key geoip_filter_keys[] = {
{ "CountryCode", GEOIP_FILTER_KEY_COUNTRY_CODE },
{ "CountryName", GEOIP_FILTER_KEY_COUNTRY_NAME },
{ "RegionCode", GEOIP_FILTER_KEY_REGION_CODE },
{ "RegionName", GEOIP_FILTER_KEY_REGION_NAME },
{ "Continent", GEOIP_FILTER_KEY_CONTINENT },
{ "Organization", GEOIP_FILTER_KEY_ORGANIZATION },
{ "City", GEOIP_FILTER_KEY_CITY },
{ "PostalCode", GEOIP_FILTER_KEY_POSTAL_CODE },
{ "Latitude", GEOIP_FILTER_KEY_LATITUDE },
{ "Longitude", GEOIP_FILTER_KEY_LONGITUDE },
{ "ASN", GEOIP_FILTER_KEY_ASN },
{ "Timezone", GEOIP_FILTER_KEY_TIMEZONE },
{ NULL, -1 }
};
#if PR_USE_REGEX
/* GeoIP filter */
struct geoip_filter {
int filter_id;
const char *filter_pattern;
pr_regex_t *filter_re;
};
#endif /* PR_USE_REGEX */
/* GeoIP policies */
typedef enum {
GEOIP_POLICY_ALLOW_DENY,
GEOIP_POLICY_DENY_ALLOW
} geoip_policy_e;
static geoip_policy_e geoip_policy = GEOIP_POLICY_ALLOW_DENY;
static const char *trace_channel = "geoip2";
static const char *get_geoip_filter_name(int);
static const char *get_geoip_filter_value(int);
static int get_filter_id(const char *filter_name) {
register unsigned int i;
int filter_id = -1;
for (i = 0; geoip_filter_keys[i].filter_name != NULL; i++) {
if (strcasecmp(filter_name, geoip_filter_keys[i].filter_name) == 0) {
filter_id = geoip_filter_keys[i].filter_id;
break;
}
}
return filter_id;
}
#if PR_USE_REGEX
static int get_filter(pool *p, const char *pattern, pr_regex_t **pre) {
int res;
*pre = pr_regexp_alloc(&geoip2_module);
res = pr_regexp_compile(*pre, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE);
if (res != 0) {
char errstr[256];
memset(errstr, '\0', sizeof(errstr));
pr_regexp_error(res, *pre, errstr, sizeof(errstr)-1);
pr_regexp_free(&geoip2_module, *pre);
*pre = NULL;
pr_log_pri(PR_LOG_DEBUG, MOD_GEOIP2_VERSION
": pattern '%s' failed regex compilation: %s", pattern, errstr);
errno = EINVAL;
return -1;
}
return res;
}
static struct geoip_filter *make_filter(pool *p, const char *filter_name,
const char *pattern) {
struct geoip_filter *filter;
int filter_id;
pr_regex_t *pre = NULL;
filter_id = get_filter_id(filter_name);
if (filter_id < 0) {
pr_log_debug(DEBUG0, MOD_GEOIP2_VERSION ": unknown GeoIP filter name '%s'",
filter_name);
return NULL;
}
if (get_filter(p, pattern, &pre) < 0) {
return NULL;
}
filter = pcalloc(p, sizeof(struct geoip_filter));
filter->filter_id = filter_id;
filter->filter_pattern = pstrdup(p, pattern);
filter->filter_re = pre;
return filter;
}
static array_header *get_sql_filters(pool *p, const char *query_name) {
register unsigned int i;
cmdtable *sql_cmdtab = NULL;
cmd_rec *sql_cmd = NULL;
modret_t *sql_res = NULL;
array_header *sql_data = NULL;
const char **values = NULL;
array_header *sql_filters = NULL;
sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_lookup", NULL, NULL,
NULL);
if (sql_cmdtab == NULL) {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"unable to execute SQLNamedQuery '%s': mod_sql not loaded", query_name);
errno = EPERM;
return NULL;
}
sql_cmd = pr_cmd_alloc(p, 2, "sql_lookup", query_name);
sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd);
if (sql_res == NULL ||
MODRET_ISERROR(sql_res)) {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"error processing SQLNamedQuery '%s'; check mod_sql logs for details",
query_name);
errno = EPERM;
return NULL;
}
sql_data = sql_res->data;
pr_trace_msg(trace_channel, 9, "SQLNamedQuery '%s' returned item count %d",
query_name, sql_data->nelts);
if (sql_data->nelts == 0) {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"SQLNamedQuery '%s' returned no values", query_name);
errno = ENOENT;
return NULL;
}
if (sql_data->nelts % 2 == 1) {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"SQLNamedQuery '%s' returned odd number of values (%d), "
"expected even number", query_name, sql_data->nelts);
errno = EINVAL;
return NULL;
}
values = sql_data->elts;
sql_filters = make_array(p, 0, sizeof(struct geoip_filter));
for (i = 0; i < sql_data->nelts; i += 2) {
const char *filter_name, *pattern = NULL;
struct geoip_filter *filter;
filter_name = values[i];
pattern = values[i+1];
filter = make_filter(p, filter_name, pattern);
if (filter == NULL) {
pr_trace_msg(trace_channel, 3, "unable to use '%s %s' as filter: %s",
filter_name, pattern, strerror(errno));
continue;
}
*((struct geoip_filter **) push_array(sql_filters)) = filter;
}
return sql_filters;
}
#endif /* PR_USE_REGEX */
static void resolve_deferred_patterns(pool *p, const char *directive) {
#if PR_USE_REGEX
config_rec *c;
c = find_config(main_server->conf, CONF_PARAM, directive, FALSE);
while (c != NULL) {
register unsigned int i;
array_header *deferred_filters, *filters;
pr_signals_handle();
filters = c->argv[0];
deferred_filters = c->argv[1];
for (i = 0; i < deferred_filters->nelts; i++) {
const char *query_name;
array_header *sql_filters;
query_name = ((const char **) deferred_filters->elts)[i];
sql_filters = get_sql_filters(p, query_name);
if (sql_filters == NULL) {
continue;
}
array_cat(filters, sql_filters);
}
c = find_config_next(c, c->next, CONF_PARAM, directive, FALSE);
}
#endif /* PR_USE_REGEX */
}
static void resolve_deferred_filters(pool *p) {
resolve_deferred_patterns(p, "GeoIPAllowFilter");
resolve_deferred_patterns(p, "GeoIPDenyFilter");
}
static int check_geoip_filters(geoip_policy_e policy) {
int allow_conn = 0, matched_allow_filter = -1, matched_deny_filter = -1;
#if PR_USE_REGEX
config_rec *c;
c = find_config(main_server->conf, CONF_PARAM, "GeoIPAllowFilter", FALSE);
while (c != NULL) {
register unsigned int i;
int matched = TRUE;
array_header *filters;
pr_signals_handle();
if (matched_allow_filter == -1) {
matched_allow_filter = FALSE;
}
filters = c->argv[0];
for (i = 0; i < filters->nelts; i++) {
int filter_id, res;
struct geoip_filter *filter;
pr_regex_t *filter_re;
const char *filter_name, *filter_pattern, *filter_value;
filter = ((struct geoip_filter **) filters->elts)[i];
filter_id = filter->filter_id;
filter_pattern = filter->filter_pattern;
filter_re = filter->filter_re;
filter_value = get_geoip_filter_value(filter_id);
if (filter_value == NULL) {
matched = FALSE;
break;
}
filter_name = get_geoip_filter_name(filter_id);
res = pr_regexp_exec(filter_re, filter_value, 0, NULL, 0, 0, 0);
pr_trace_msg(trace_channel, 12,
"%s filter value %s %s GeoIPAllowFilter pattern '%s'",
filter_name, filter_value, res == 0 ? "matched" : "did not match",
filter_pattern);
if (res == 0) {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"%s filter value '%s' matched GeoIPAllowFilter pattern '%s'",
filter_name, filter_value, filter_pattern);
} else {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"%s filter value '%s' did not match GeoIPAllowFilter pattern '%s'",
filter_name, filter_value, filter_pattern);
matched = FALSE;
break;
}
}
if (matched == TRUE) {
matched_allow_filter = TRUE;
break;
}
c = find_config_next(c, c->next, CONF_PARAM, "GeoIPAllowFilter", FALSE);
}
c = find_config(main_server->conf, CONF_PARAM, "GeoIPDenyFilter", FALSE);
while (c != NULL) {
register unsigned int i;
int matched = TRUE;
array_header *filters;
pr_signals_handle();
if (matched_deny_filter == -1) {
matched_deny_filter = FALSE;
}
filters = c->argv[0];
for (i = 0; i < filters->nelts; i++) {
int filter_id, res;
struct geoip_filter *filter;
pr_regex_t *filter_re;
const char *filter_name, *filter_pattern, *filter_value;
filter = ((struct geoip_filter **) filters->elts)[i];
filter_id = filter->filter_id;
filter_pattern = filter->filter_pattern;
filter_re = filter->filter_re;
filter_value = get_geoip_filter_value(filter_id);
if (filter_value == NULL) {
matched = FALSE;
break;
}
filter_name = get_geoip_filter_name(filter_id);
res = pr_regexp_exec(filter_re, filter_value, 0, NULL, 0, 0, 0);
pr_trace_msg(trace_channel, 12,
"%s filter value %s %s GeoIPDenyFilter pattern '%s'",
filter_name, filter_value, res == 0 ? "matched" : "did not match",
filter_pattern);
if (res == 0) {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"%s filter value '%s' matched GeoIPDenyFilter pattern '%s'",
filter_name, filter_value, filter_pattern);
} else {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"%s filter value '%s' did not match GeoIPDenyFilter pattern '%s'",
filter_name, filter_value, filter_pattern);
matched = FALSE;
break;
}
}
if (matched == TRUE) {
matched_deny_filter = TRUE;
break;
}
c = find_config_next(c, c->next, CONF_PARAM, "GeoIPDenyFilter", FALSE);
}
#endif /* !HAVE_REGEX_H or !HAVE_REGCOMP */
switch (policy) {
case GEOIP_POLICY_ALLOW_DENY:
if (matched_deny_filter == TRUE &&
matched_allow_filter != TRUE) {
/* If we explicitly matched any deny filters AND have NOT explicitly
* matched any allow filters, the connection is rejected, otherwise,
* it is allowed.
*/
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"client matched GeoIPDenyFilter, rejecting connection");
allow_conn = -1;
} else {
pr_trace_msg(trace_channel, 9,
"allowing client connection (policy 'allow,deny')");
}
break;
case GEOIP_POLICY_DENY_ALLOW:
if (matched_allow_filter == FALSE) {
/* If we have not explicitly matched any allow filters, then
* reject the connection.
*/
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"client did not match any GeoIPAllowFilters, rejecting connection");
allow_conn = -1;
} else {
pr_trace_msg(trace_channel, 9,
"allowing client connection (policy 'deny,allow')");
}
break;
}
return allow_conn;
}
static const char *get_geoip_filter_name(int filter_id) {
register unsigned int i;
for (i = 0; geoip_filter_keys[i].filter_name != NULL; i++) {
if (geoip_filter_keys[i].filter_id == filter_id) {
return geoip_filter_keys[i].filter_name;
}
}
errno = ENOENT;
return NULL;
}
static const char *get_geoip_filter_value(int filter_id) {
switch (filter_id) {
case GEOIP_FILTER_KEY_COUNTRY_CODE:
if (geoip_country_code2 != NULL) {
return geoip_country_code2;
}
break;
case GEOIP_FILTER_KEY_COUNTRY_NAME:
if (geoip_country_name != NULL) {
return geoip_country_name;
}
break;
case GEOIP_FILTER_KEY_REGION_CODE:
if (geoip_region_code != NULL) {
return geoip_region_code;
}
break;
case GEOIP_FILTER_KEY_REGION_NAME:
if (geoip_region_name != NULL) {
return geoip_region_name;
}
break;
case GEOIP_FILTER_KEY_CONTINENT:
if (geoip_continent_code != NULL) {
return geoip_continent_code;
}
break;
case GEOIP_FILTER_KEY_ORGANIZATION:
if (geoip_org != NULL) {
return geoip_org;
}
break;
case GEOIP_FILTER_KEY_CITY:
if (geoip_city != NULL) {
return geoip_city;
}
break;
case GEOIP_FILTER_KEY_POSTAL_CODE:
if (geoip_postal_code != NULL) {
return geoip_postal_code;
}
break;
case GEOIP_FILTER_KEY_LATITUDE:
if (geoip_latitude != NULL) {
return geoip_latitude;
}
break;
case GEOIP_FILTER_KEY_LONGITUDE:
if (geoip_longitude != NULL) {
return geoip_longitude;
}
break;
case GEOIP_FILTER_KEY_ASN:
if (geoip_asn != NULL) {
return geoip_asn;
}
break;
case GEOIP_FILTER_KEY_TIMEZONE:
if (geoip_timezone != NULL) {
return geoip_timezone;
}
break;
}
errno = ENOENT;
return NULL;
}
static void get_geoip_tables(void) {
config_rec *c;
c = find_config(main_server->conf, CONF_PARAM, "GeoIPTable", FALSE);
while (c != NULL) {
MMDB_s *mmdb = NULL;
const char *path;
uint32_t flags;
int res, xerrno = 0;
pr_signals_handle();
path = c->argv[0];
flags = *((uint32_t *) c->argv[1]);
mmdb = pcalloc(geoip2_pool, sizeof(MMDB_s));
PRIVS_ROOT
res = MMDB_open(path, flags, mmdb);
xerrno = errno;
PRIVS_RELINQUISH
if (res == MMDB_SUCCESS) {
char build_date[64];
time_t build_epoch;
*((MMDB_s **) push_array(geoip2_mmdbs)) = mmdb;
build_epoch = mmdb->metadata.build_epoch;
strftime(build_date, sizeof(build_date), "%F %T UTC",
gmtime(&build_epoch));
pr_trace_msg(trace_channel, 15,
"loaded GeoIP table '%s': %s (IP version = IPv%d, format version = "
"%d.%d, built = %s)", path, mmdb->metadata.database_type,
mmdb->metadata.ip_version, mmdb->metadata.binary_format_major_version,
mmdb->metadata.binary_format_minor_version, build_date);
} else {
if (res != MMDB_IO_ERROR) {
pr_log_pri(PR_LOG_WARNING, MOD_GEOIP2_VERSION
": warning: unable to open/use GeoIPTable '%s': %s", path,
MMDB_strerror(res));
} else {
pr_log_pri(PR_LOG_WARNING, MOD_GEOIP2_VERSION
": warning: unable to open/use GeoIPTable '%s': %s (%s)", path,
MMDB_strerror(res), strerror(xerrno));
}
}
c = find_config_next(c, c->next, CONF_PARAM, "GeoIPTable", FALSE);
}
}
static void remove_geoip_tables(void) {
register unsigned int i;
MMDB_s **mmdbs;
if (geoip2_mmdbs == NULL ||
geoip2_mmdbs->nelts == 0) {
return;
}
mmdbs = geoip2_mmdbs->elts;
for (i = 0; i < geoip2_mmdbs->nelts; i++) {
if (mmdbs[i] != NULL) {
MMDB_close(mmdbs[i]);
mmdbs[i] = NULL;
}
}
}
static const char *get_geoip_data_text(pool *p, MMDB_lookup_result_s *lookup,
const char **lookup_path, int filter_id) {
int res, xerrno = 0;
const char *text = NULL;
MMDB_entry_data_s entry_data;
res = MMDB_aget_value(&(lookup->entry), &entry_data, lookup_path);
xerrno = errno;
if (res != MMDB_SUCCESS) {
const char *lookup_name;
lookup_name = get_geoip_filter_name(filter_id);
switch (res) {
case MMDB_LOOKUP_PATH_DOES_NOT_MATCH_DATA_ERROR:
/* Ignored. */
errno = ENOENT;
break;
case MMDB_IO_ERROR:
pr_trace_msg(trace_channel, 3, "error getting data for %s: %s (%s)",
lookup_name, MMDB_strerror(res), strerror(xerrno));
errno = xerrno;
break;
default:
pr_trace_msg(trace_channel, 3, "error getting data for %s: %s",
lookup_name, MMDB_strerror(res));
errno = EPERM;
break;
}
return NULL;
}
if (!entry_data.has_data) {
errno = ENOENT;
return NULL;
}
switch (entry_data.type) {
case MMDB_DATA_TYPE_UTF8_STRING:
text = pstrndup(p, entry_data.utf8_string, entry_data.data_size);
break;
case MMDB_DATA_TYPE_UINT32: {
char buf[64];
memset(buf, '\0', sizeof(buf));
#if PROFTPD_VERSION_NUMBER >= 0x0001030705
pr_snprintf(buf, sizeof(buf)-1, "%lu", (unsigned long) entry_data.uint32);
#else
snprintf(buf, sizeof(buf)-1, "%lu", (unsigned long) entry_data.uint32);
#endif /* ProFTPD 1.3.7 */
text = pstrdup(p, buf);
break;
}
case MMDB_DATA_TYPE_DOUBLE: {
char buf[64];
memset(buf, '\0', sizeof(buf));
#if PROFTPD_VERSION_NUMBER >= 0x0001030705
pr_snprintf(buf, sizeof(buf)-1, "%f", entry_data.double_value);
#else
snprintf(buf, sizeof(buf)-1, "%f", entry_data.double_value);
#endif /* ProFTPD 1.3.7 */
text = pstrdup(p, buf);
break;
}
default:
pr_trace_msg(trace_channel, 3,
"unknown/unsupported entry data type (%lu), ignoring",
(unsigned long) entry_data.type);
errno = EINVAL;
return NULL;
}
return text;
}
static void get_geoip_data(void) {
register unsigned int i;
const char *ip_addr, *text;
const char *lookup_path[5] = { NULL, NULL, NULL, NULL, NULL };
MMDB_s **mmdbs;
ip_addr = pr_netaddr_get_ipstr(session.c->remote_addr);
mmdbs = geoip2_mmdbs->elts;
for (i = 0; i < geoip2_mmdbs->nelts; i++) {
MMDB_s *mmdb;
MMDB_lookup_result_s lookup;
int gai_error = 0, mmdb_error = 0;
pr_signals_handle();
if (mmdbs[i] == NULL) {
continue;
}
mmdb = mmdbs[i];
lookup = MMDB_lookup_string(mmdb, ip_addr, &gai_error, &mmdb_error);
if (mmdb_error != MMDB_SUCCESS) {
pr_trace_msg(trace_channel, 2,
"error looking up '%s' in GeoIPTable '%s': %s", ip_addr,
mmdb->filename, MMDB_strerror(mmdb_error));
continue;
}
if (!lookup.found_entry) {
pr_trace_msg(trace_channel, 2,
"no entry found for '%s' in GeoIPTable '%s'", ip_addr, mmdb->filename);
continue;
}
/* XXX This cries out to be done in a table-driven fashion. */
lookup_path[0] = "country";
lookup_path[1] = "iso_code";
lookup_path[2] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_COUNTRY_CODE);
if (text != NULL) {
geoip_country_code2 = text;
}
/* "country" already set as first element above; no need to duplicate
* it again.
*/
lookup_path[1] = "names";
lookup_path[2] = "en";
lookup_path[3] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_COUNTRY_NAME);
if (text != NULL) {
geoip_country_name = text;
}
lookup_path[0] = "continent";
lookup_path[1] = "code";
lookup_path[2] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_CONTINENT);
if (text != NULL) {
geoip_continent_code = text;
}
lookup_path[0] = "subdivisions";
lookup_path[1] = "0";
lookup_path[2] = "iso_code";
lookup_path[3] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_REGION_CODE);
if (text != NULL) {
geoip_region_code = text;
}
/* "subdivisions" already set as first element above; no need to duplicate
* it again.
*/
lookup_path[1] = "0";
lookup_path[2] = "names";
lookup_path[3] = "en";
lookup_path[4] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_REGION_NAME);
if (text != NULL) {
geoip_region_name = text;
}
lookup_path[0] = "city";
lookup_path[1] = "names";
lookup_path[2] = "en";
lookup_path[3] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_CITY);
if (text != NULL) {
geoip_city = text;
}
lookup_path[0] = "postal";
lookup_path[1] = "code";
lookup_path[2] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_POSTAL_CODE);
if (text != NULL) {
geoip_postal_code = text;
}
lookup_path[0] = "locations";
lookup_path[1] = "latitude";
lookup_path[2] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_LATITUDE);
if (text != NULL) {
geoip_latitude = text;
}
/* "locations" already set as first element above; no need to duplicate
* it again.
*/
lookup_path[1] = "longitude";
lookup_path[2] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_LONGITUDE);
if (text != NULL) {
geoip_longitude = text;
}
/* "locations" already set as first element above; no need to duplicate
* it again.
*/
lookup_path[1] = "time_zone";
lookup_path[2] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_TIMEZONE);
if (text != NULL) {
geoip_longitude = text;
}
lookup_path[0] = "autonomous_system_number";
lookup_path[1] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_ASN);
if (text != NULL) {
geoip_asn = text;
}
lookup_path[0] = "autonomous_system_organization";
lookup_path[1] = NULL;
text = get_geoip_data_text(geoip2_pool, &lookup, lookup_path,
GEOIP_FILTER_KEY_ORGANIZATION);
if (text != NULL) {
geoip_org = text;
}
}
}
static void get_geoip_info(void) {
const char *ip_addr;
get_geoip_data();
ip_addr = pr_netaddr_get_ipstr(session.c->remote_addr);
if (geoip_country_code2 != NULL) {
pr_trace_msg(trace_channel, 8, "%s: 2-Letter country code: %s", ip_addr,
geoip_country_code2);
}
if (geoip_country_name != NULL) {
pr_trace_msg(trace_channel, 8, "%s: Country name: %s", ip_addr,
geoip_country_name);
}
if (geoip_region_code != NULL) {
pr_trace_msg(trace_channel, 8, "%s: Region code: %s", ip_addr,
geoip_region_code);
}
if (geoip_region_name != NULL) {
pr_trace_msg(trace_channel, 8, "%s: Region name: %s", ip_addr,
geoip_region_name);
}
if (geoip_timezone != NULL) {
pr_trace_msg(trace_channel, 8, "%s: Timezone: %s", ip_addr, geoip_timezone);
}
if (geoip_continent_code != NULL) {
pr_trace_msg(trace_channel, 8, "%s: Continent code: %s", ip_addr,
geoip_continent_code);
}
if (geoip_org != NULL) {
pr_trace_msg(trace_channel, 8, "%s: Organization: %s", ip_addr, geoip_org);
}
if (geoip_city != NULL) {
pr_trace_msg(trace_channel, 8, "%s: City: %s", ip_addr, geoip_city);
}
if (geoip_postal_code != NULL) {
pr_trace_msg(trace_channel, 8, "%s: Postal code: %s", ip_addr,
geoip_postal_code);
}
if (geoip_latitude != NULL) {
pr_trace_msg(trace_channel, 8, "%s: Latitude: %s", ip_addr,
geoip_latitude);
}
if (geoip_longitude != NULL) {
pr_trace_msg(trace_channel, 8, "%s: Longitude: %s", ip_addr,
geoip_longitude);
}
if (geoip_asn != NULL) {
pr_trace_msg(trace_channel, 8, "%s: ASN: %s", ip_addr, geoip_asn);
}
}
static void set_geoip_value(const char *key, const char *value) {
int res;
res = pr_env_set(session.pool, key, value);
if (res < 0) {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"error setting %s environment variable: %s", key, strerror(errno));
}
res = pr_table_add_dup(session.notes, pstrdup(session.pool, key),
(char *) value, 0);
if (res < 0) {
(void) pr_log_writefile(geoip2_logfd, MOD_GEOIP2_VERSION,
"error adding %s session note: %s", key, strerror(errno));
}
}
static void set_geoip_values(void) {
if (geoip_country_code2 != NULL) {
set_geoip_value("GEOIP_COUNTRY_CODE", geoip_country_code2);
}
if (geoip_country_name != NULL) {
set_geoip_value("GEOIP_COUNTRY_NAME", geoip_country_name);
}
if (geoip_region_code != NULL) {
set_geoip_value("GEOIP_REGION", geoip_region_code);
}
if (geoip_region_name != NULL) {
set_geoip_value("GEOIP_REGION_NAME", geoip_region_name);
}
if (geoip_continent_code != NULL) {
set_geoip_value("GEOIP_CONTINENT_CODE", geoip_continent_code);
}
if (geoip_org != NULL) {
set_geoip_value("GEOIP_ORGANIZATION", geoip_org);
}
if (geoip_city != NULL) {
set_geoip_value("GEOIP_CITY", geoip_city);
}
if (geoip_postal_code != NULL) {
set_geoip_value("GEOIP_POSTAL_CODE", geoip_postal_code);
}
if (geoip_latitude != NULL) {
set_geoip_value("GEOIP_LATITUDE", geoip_latitude);
}
if (geoip_longitude != NULL) {
set_geoip_value("GEOIP_LONGITUDE", geoip_longitude);
}
if (geoip_asn != NULL) {
set_geoip_value("GEOIP_ASN", geoip_asn);
}
if (geoip_timezone != NULL) {
set_geoip_value("GEOIP_TIMEZONE", geoip_timezone);
}
}