#!/usr/bin/perl use strict; $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin'; my $APNIC_URI = 'http://ftp.apnic.net/stats/apnic/delegated-apnic-latest'; my $CIDR_TABLE = '/etc/postfix/check_client_country'; my %MASK_BITS = ( '256'=>'24','512'=>'23','1024'=>'22','2048'=>'21','4096'=>'20','8192'=>'19','16384'=>'18','32768'=>'17', '65536'=>'16','131072'=>'15','262144'=>'14','524288'=>'13','1048576'=>'12','2097152'=>'11','4194304'=>'10', '8388608'=>'9','16777216'=>'8','33554432'=>'7','67108864'=>'6','134217728'=>'5','268435456'=>'4', '536870912'=>'3','1073741824'=>'2','2147483648'=>'1' ); my $FLAG = 0; if (!open(CMD,"wget $APNIC_URI -O - 2>/dev/null |")) { print "Wget process didn't function.\n"; exit(1); } if (!open(TMP,">$CIDR_TABLE.tmp")) { print "Can't open $CIDR_TABLE.tmp.\n"; close(CMD); exit(1); } eval'flock(TMP,2);'; print TMP '# LastUpdate: ' . localtime(time) . "\n"; print TMP '# [Japan Network]' . "\n"; while (my $LINE = ) { my $CIDR; my $TABS; if ($LINE =~ /^apnic\|JP\|ipv4\|([0-9\.]+)\|([0-9]+)\|[0-9]+\|.+/i) { if ($2) { $CIDR = $1 . '/' . $MASK_BITS{$2}; } } if ($CIDR =~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]+$/) { $FLAG += 1; if (length($CIDR) > 15) { $TABS = "\t"; } elsif (length($CIDR) > 11) { $TABS = "\t\t"; } else { $TABS = "\t\t\t"; } print TMP $CIDR . $TABS . "check_client_jp\n"; } } print TMP "133.0.0.0/8\t\t\tcheck_client_jp\n"; close(TMP); close(CMD); if ($FLAG > 500) { system("cp $CIDR_TABLE $CIDR_TABLE.old") if -f $CIDR_TABLE; system("mv $CIDR_TABLE.tmp $CIDR_TABLE && postmap $CIDR_TABLE"); } else { unlink("$CIDR_TABLE.tmp"); } print "Successful in update of $CIDR_TABLE.\n"; exit(0);