| 1 |
package ProFTPD::Tests::Modules::mod_geoip::sql; |
| 2 |
|
| 3 |
use lib qw(t/lib); |
| 4 |
use base qw(ProFTPD::TestSuite::Child); |
| 5 |
use strict; |
| 6 |
|
| 7 |
use File::Path qw(mkpath); |
| 8 |
use File::Spec; |
| 9 |
use IO::Handle; |
| 10 |
use POSIX qw(:fcntl_h); |
| 11 |
|
| 12 |
use ProFTPD::TestSuite::FTP; |
| 13 |
use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite); |
| 14 |
|
| 15 |
$| = 1; |
| 16 |
|
| 17 |
my $order = 0; |
| 18 |
|
| 19 |
my $TESTS = { |
| 20 |
geoip_sql_allow_filter => { |
| 21 |
order => ++$order, |
| 22 |
test_class => [qw(forking mod_sql_sqlite)], |
| 23 |
}, |
| 24 |
|
| 25 |
}; |
| 26 |
|
| 27 |
sub new { |
| 28 |
return shift()->SUPER::new(@_); |
| 29 |
} |
| 30 |
|
| 31 |
sub list_tests { |
| 32 |
return testsuite_get_runnable_tests($TESTS); |
| 33 |
} |
| 34 |
|
| 35 |
sub set_up { |
| 36 |
my $self = shift; |
| 37 |
$self->SUPER::set_up(@_); |
| 38 |
} |
| 39 |
|
| 40 |
sub build_db { |
| 41 |
my $cmd = shift; |
| 42 |
my $db_script = shift; |
| 43 |
my $db_file = shift; |
| 44 |
my $check_exit_status = shift; |
| 45 |
$check_exit_status = 0 unless defined $check_exit_status; |
| 46 |
|
| 47 |
if ($ENV{TEST_VERBOSE}) { |
| 48 |
print STDERR "Executing sqlite3: $cmd\n"; |
| 49 |
} |
| 50 |
|
| 51 |
my @output = `$cmd`; |
| 52 |
my $exit_status = $?; |
| 53 |
|
| 54 |
if ($ENV{TEST_VERBOSE}) { |
| 55 |
print STDERR "Output: ", join('', @output), "\n"; |
| 56 |
} |
| 57 |
|
| 58 |
if ($check_exit_status) { |
| 59 |
if ($? != 0) { |
| 60 |
croak("'$cmd' failed"); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
# Make sure that, if we're running as root, the database file has |
| 65 |
# the permissions/privs set for use by proftpd |
| 66 |
if ($< == 0) { |
| 67 |
unless (chmod(0666, $db_file)) { |
| 68 |
croak("Can't set perms on $db_file to 0666: $!"); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
unlink($db_script); |
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
sub geoip_sql_allow_filter { |
| 77 |
my $self = shift; |
| 78 |
my $tmpdir = $self->{tmpdir}; |
| 79 |
my $setup = test_setup($tmpdir, 'geoip'); |
| 80 |
|
| 81 |
my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db"); |
| 82 |
|
| 83 |
# Build up sqlite3 command to create users, groups tables and populate them |
| 84 |
my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql"); |
| 85 |
|
| 86 |
if (open(my $fh, "> $db_script")) { |
| 87 |
print $fh <<EOS; |
| 88 |
CREATE TABLE ftp_user_geoip ( |
| 89 |
user TEXT, |
| 90 |
regions TEXT, |
| 91 |
countries TEXT |
| 92 |
); |
| 93 |
INSERT INTO ftp_user_geoip (user, regions, countries) VALUES ('$setup->{user}', 'TX', 'US'); |
| 94 |
|
| 95 |
EOS |
| 96 |
|
| 97 |
unless (close($fh)) { |
| 98 |
die("Can't write $db_script: $!"); |
| 99 |
} |
| 100 |
|
| 101 |
} else { |
| 102 |
die("Can't open $db_script: $!"); |
| 103 |
} |
| 104 |
|
| 105 |
my $cmd = "sqlite3 $db_file < $db_script"; |
| 106 |
build_db($cmd, $db_script, $db_file); |
| 107 |
|
| 108 |
my $geoip_ip_table = File::Spec->rel2abs('t/etc/modules/mod_geoip/GeoIP.dat'); |
| 109 |
my $geoip_ipv6_table = File::Spec->rel2abs('t/etc/modules/mod_geoip/GeoIPv6.dat'); |
| 110 |
my $geoip_city_table = File::Spec->rel2abs('t/etc/modules/mod_geoip/GeoLiteCity.dat'); |
| 111 |
|
| 112 |
my $config = { |
| 113 |
PidFile => $setup->{pid_file}, |
| 114 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 115 |
SystemLog => $setup->{log_file}, |
| 116 |
TraceLog => $setup->{log_file}, |
| 117 |
Trace => 'geoip:20 sql:20', |
| 118 |
|
| 119 |
AuthUserFile => $setup->{auth_user_file}, |
| 120 |
AuthGroupFile => $setup->{auth_group_file}, |
| 121 |
AuthOrder => 'mod_auth_file.c', |
| 122 |
|
| 123 |
IfModules => { |
| 124 |
'mod_delay.c' => { |
| 125 |
DelayEngine => 'off', |
| 126 |
}, |
| 127 |
|
| 128 |
'mod_geoip.c' => [ |
| 129 |
'GeoIPEngine on', |
| 130 |
"GeoIPLog $setup->{log_file}", |
| 131 |
"GeoIPTable $geoip_ip_table", |
| 132 |
"GeoIPTable $geoip_ipv6_table", |
| 133 |
"GeoIPTable $geoip_city_table", |
| 134 |
|
| 135 |
'GeoIPPolicy allow,deny', |
| 136 |
'GeoIPAllowFilter sql:/get-user-geoip-regions', |
| 137 |
'GeoIPDenyFilter sql:/get-user-geoip-countries', |
| 138 |
], |
| 139 |
|
| 140 |
'mod_sql.c' => [ |
| 141 |
'SQLEngine log', |
| 142 |
'SQLBackend sqlite3', |
| 143 |
"SQLConnectInfo $db_file", |
| 144 |
"SQLLogFile $setup->{log_file}", |
| 145 |
"SQLNamedQuery get-user-geoip-regions SELECT \"'RegionCode', regions FROM ftp_user_geoip WHERE user = '%U'\"", |
| 146 |
"SQLNamedQuery get-user-geoip-countries SELECT \"'CountryCode', countries FROM ftp_user_geoip WHERE user = '%U'\"", |
| 147 |
], |
| 148 |
}, |
| 149 |
}; |
| 150 |
|
| 151 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 152 |
$config); |
| 153 |
|
| 154 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 155 |
# the child will indicate when it's done with its test by writing a message |
| 156 |
# to the parent. |
| 157 |
my ($rfh, $wfh); |
| 158 |
unless (pipe($rfh, $wfh)) { |
| 159 |
die("Can't open pipe: $!"); |
| 160 |
} |
| 161 |
|
| 162 |
my $ex; |
| 163 |
|
| 164 |
# Fork child |
| 165 |
$self->handle_sigchld(); |
| 166 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 167 |
if ($pid) { |
| 168 |
eval { |
| 169 |
# Give the server time to start up |
| 170 |
sleep(1); |
| 171 |
|
| 172 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1); |
| 173 |
$client->login($setup->{user}, $setup->{passwd}); |
| 174 |
$client->quit(); |
| 175 |
}; |
| 176 |
if ($@) { |
| 177 |
$ex = $@; |
| 178 |
} |
| 179 |
|
| 180 |
$wfh->print("done\n"); |
| 181 |
$wfh->flush(); |
| 182 |
|
| 183 |
} else { |
| 184 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 185 |
if ($@) { |
| 186 |
warn($@); |
| 187 |
exit 1; |
| 188 |
} |
| 189 |
|
| 190 |
exit 0; |
| 191 |
} |
| 192 |
|
| 193 |
# Stop server |
| 194 |
server_stop($setup->{pid_file}); |
| 195 |
$self->assert_child_ok($pid); |
| 196 |
|
| 197 |
test_cleanup($setup->{log_file}, $ex); |
| 198 |
} |
| 199 |
|
| 200 |
1; |