From 922a9be87d5f57f4640ffb27cff9054c83eed6ea Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Sat, 24 Jul 2010 11:14:14 +0200 Subject: [PATCH] geoip: import scripts for building the xt_geoip database --- Makefile.am | 2 +- configure.ac | 2 +- doc/changelog.txt | 1 + extensions/libxt_geoip.man | 6 ++--- geoip/.gitignore | 2 ++ geoip/Makefile.am | 3 +++ geoip/geoip_csv_iv0.pl | 45 ++++++++++++++++++++++++++++++++++++++ geoip/geoip_download.sh | 8 +++++++ 8 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 geoip/.gitignore create mode 100644 geoip/Makefile.am create mode 100755 geoip/geoip_csv_iv0.pl create mode 100755 geoip/geoip_download.sh diff --git a/Makefile.am b/Makefile.am index dab1ca6..27bf6ad 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,7 @@ # -*- Makefile -*- ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = extensions +SUBDIRS = extensions geoip man_MANS := xtables-addons.8 diff --git a/configure.ac b/configure.ac index 3184437..ff40db3 100644 --- a/configure.ac +++ b/configure.ac @@ -74,7 +74,7 @@ fi; AC_SUBST([regular_CFLAGS]) AC_SUBST([kbuilddir]) AC_SUBST([xtlibdir]) -AC_CONFIG_FILES([Makefile Makefile.iptrules Makefile.mans +AC_CONFIG_FILES([Makefile Makefile.iptrules Makefile.mans geoip/Makefile extensions/Makefile extensions/ACCOUNT/Makefile extensions/ipset/Makefile extensions/pknock/Makefile]) AC_OUTPUT diff --git a/doc/changelog.txt b/doc/changelog.txt index 802d9eb..2089139 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -4,6 +4,7 @@ HEAD - RAWNAT: IPv6 variants erroneously rejected masks /33-/128 - new target xt_CHECKSUM - xt_length2: add support for IPv6 jumbograms +- import xt_geoip database scripts Xtables-addons 1.27 (May 16 2010) diff --git a/extensions/libxt_geoip.man b/extensions/libxt_geoip.man index 593fb93..007caa4 100644 --- a/extensions/libxt_geoip.man +++ b/extensions/libxt_geoip.man @@ -10,8 +10,8 @@ NOTE: The country is inputed by its ISO-3166 code. .PP The extra files you will need is the binary database files. They are generated -from a country-subnet database with the geoip_csv_iv0.pl tool, available at -http://jengelh.medozas.de/files/geoip/ . The files MUST be moved to -/usr/share/xt_geoip/ +from a country-subnet database with the geoip_csv_iv0.pl tool that should be +available in /usr/lib(exec)/xtables-addons/ . The resulting files MUST be moved +to /usr/share/xt_geoip/ as the shared library is statically looking for this pathname (e.g. /usr/share/xt_geoip/LE/de.iv0). diff --git a/geoip/.gitignore b/geoip/.gitignore new file mode 100644 index 0000000..46ece0d --- /dev/null +++ b/geoip/.gitignore @@ -0,0 +1,2 @@ +/BE +/LE diff --git a/geoip/Makefile.am b/geoip/Makefile.am new file mode 100644 index 0000000..8dd9cf9 --- /dev/null +++ b/geoip/Makefile.am @@ -0,0 +1,3 @@ +# -*- Makefile -*- + +pkglibexec_SCRIPTS = geoip_csv_iv0.pl geoip_download.sh diff --git a/geoip/geoip_csv_iv0.pl b/geoip/geoip_csv_iv0.pl new file mode 100755 index 0000000..435593e --- /dev/null +++ b/geoip/geoip_csv_iv0.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl +# +# Converter for MaxMind CSV database to binary, for xt_geoip +# Copyright © Jan Engelhardt , 2008 +# +# Use -b argument to create big-endian tables. +# +use Getopt::Long; +use IO::Handle; +use Text::CSV_XS; # or trade for Text::CSV +use strict; + +my %country; +my %names; +my $csv = Text::CSV_XS->new({binary => 0, eol => $/}); # or Text::CSV +my $mode = "VV"; + +&Getopt::Long::Configure(qw(bundling)); +&GetOptions("b" => sub { $mode = "NN"; }); + +while (my $row = $csv->getline(*ARGV)) { + if (!defined($country{$row->[4]})) { + $country{$row->[4]} = []; + $names{$row->[4]} = $row->[5]; + } + my $c = $country{$row->[4]}; + push(@$c, [$row->[2], $row->[3]]); + if ($. % 4096 == 0) { + print STDERR "\r\e[2K$. entries"; + } +} + +print STDERR "\r\e[2K$. entries total\n"; + +foreach my $iso_code (sort keys %country) { + printf "%5u ranges for %s %s\n", + scalar(@{$country{$iso_code}}), + $iso_code, $names{$iso_code}; + + open(my $fh, ">".uc($iso_code).".iv0"); + foreach my $range (@{$country{$iso_code}}) { + print $fh pack($mode, $range->[0], $range->[1]); + } + close $fh; +} diff --git a/geoip/geoip_download.sh b/geoip/geoip_download.sh new file mode 100755 index 0000000..c4a05a2 --- /dev/null +++ b/geoip/geoip_download.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +rm -f GeoIPv6.csv{,.gz} GeoIPCountryCSV.zip GeoIPCountryWhois.csv; +wget \ + http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz \ + http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip; +gzip -d GeoIPv6.csv.gz; +unzip GeoIPCountryCSV.zip;