Using GeoIP with PHP - Part 1

For the pass few months, I noticed there seems a large number of forum spam bots scanning for loophole on something that I am working on. While we managed to capture all these IP addresses and blocked them, I was curious about the locations (countries or cities) of these IP addresses. The only way is to add country-to-IP resolver or GeoIP support. Below are the installation steps in Ubuntu 12.10.

Install the GeoIP extension and the need database (only can find IP by country)
$ sudo apt-get install php5-geoip geoip-database

Check our installation
$ php -m | grep geoip
geoip

Check the available databases.
$ php -r "print_r(geoip_db_get_all_info());"
Segmentation fault (core dumped)

However, default packages seemed to be buggy. Let’s do a backtrace.
$ strace php -r "print_r(geoip_db_get_all_info());"
......
stat("/usr/share/GeoIP/GeoIP.dat", {st_mode=S_IFREG|0644, st_size=1773423, ...}) = 0
stat("/usr/share/GeoIP/GeoIPv6.dat", {st_mode=S_IFREG|0644, st_size=1226717, ...}) = 0
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)

Unfortunately there is no core dump file. Why? No worry, let’s check the core dump default behaviour.
$ cat /proc/sys/kernel/core_pattern | /usr/share/apport/apport %p %s %c

Apport ? Yes, The automatic crash report generator in Ubuntu. Let’s read the crash reports which are located in /var/crash. Get the title or summary of the report so we can google for answer.
$ cd /var/crash
$ cat _usr_bin_php5.1000.crash | grep Title
Title: php5 crashed with SIGSEGV in add_assoc_string_ex()

Google around for the title message, no luck, nothing related to geoip extension. Back to the question, where is the core file? Let’s try checking the ulimit
$ ulimit -c
0

Ok. Let’s change that to unlimited and rerun the sample code again.
$ ulimit -c unlimited
$ ulimit -c
unlimited

$ php -r "print_r(geoip_db_get_all_info());"
$ ls -l core
-rw-r----- 1 kianmeng kianmeng 15409152 Mar  9 20:07 core

Now let’s backtrace it using gdb, the GNU debugger.
$ gdb /usr/bin/php core
(gdb) bt
#0  __strlen_sse2_pminub () at ../sysdeps/x86_64/multiarch/strlen-sse2-pminub.S:39
#1  0x00000000006bc17e in add_assoc_string_ex ()
#2  0x00007fb7f6404bda in zif_geoip_db_get_all_info () from /usr/lib/php5/20100525/geoip.so

No comments:

Post a Comment