Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Modules/mod_sql_odbc.pm

raw
1 package ProFTPD::Tests::Modules::mod_sql_odbc;
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
11 use ProFTPD::TestSuite::FTP;
12 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
13
14 $| = 1;
15
16 my $order = 0;
17
18 my $TESTS = {
19 sql_sqllog => {
20 order => ++$order,
21 test_class => [qw(forking)],
22 },
23
24 sql_custom_user_info => {
25 order => ++$order,
26 test_class => [qw(forking)],
27 },
28
29 };
30
31 sub new {
32 return shift()->SUPER::new(@_);
33 }
34
35 sub list_tests {
36 return testsuite_get_runnable_tests($TESTS);
37 }
38
39 sub get_sessions {
40 my $db_file = shift;
41 my $where = shift;
42
43 my $sql = "SELECT user, ip_addr, timestamp FROM ftpsessions";
44 if ($where) {
45 $sql .= " WHERE $where";
46 }
47
48 my $cmd = "sqlite3 $db_file \"$sql\"";
49
50 if ($ENV{TEST_VERBOSE}) {
51 print STDERR "Executing sqlite3: $cmd\n";
52 }
53
54 my $res = join('', `$cmd`);
55 chomp($res);
56
57 # The default sqlite3 delimiter is '|'
58 return split(/\|/, $res);
59 }
60
61 sub sql_sqllog {
62 my $self = shift;
63 my $tmpdir = $self->{tmpdir};
64
65 my $config_file = "$tmpdir/odbc.conf";
66 my $pid_file = File::Spec->rel2abs("$tmpdir/odbc.pid");
67 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/odbc.scoreboard");
68
69 my $log_file = File::Spec->rel2abs('tests.log');
70
71 my $auth_user_file = File::Spec->rel2abs("$tmpdir/odbc.passwd");
72 my $auth_group_file = File::Spec->rel2abs("$tmpdir/odbc.group");
73
74 my $user = 'proftpd';
75 my $passwd = 'test';
76 my $group = 'ftpd';
77 my $home_dir = File::Spec->rel2abs($tmpdir);
78 my $uid = 500;
79 my $gid = 500;
80
81 # Make sure that, if we're running as root, that the home directory has
82 # permissions/privs set for the account we create
83 if ($< == 0) {
84 unless (chmod(0755, $home_dir)) {
85 die("Can't set perms on $home_dir to 0755: $!");
86 }
87
88 unless (chown($uid, $gid, $home_dir)) {
89 die("Can't set owner of $home_dir to $uid/$gid: $!");
90 }
91 }
92
93 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
94 '/bin/bash');
95 auth_group_write($auth_group_file, $group, $gid, $user);
96
97 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
98
99 # Build up sqlite3 command to create users, groups tables and populate them
100 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
101
102 if (open(my $fh, "> $db_script")) {
103 print $fh <<EOS;
104 CREATE TABLE ftpsessions (
105 user TEXT,
106 ip_addr TEXT,
107 timestamp TEXT
108 );
109 EOS
110
111 unless (close($fh)) {
112 die("Can't write $db_script: $!");
113 }
114
115 } else {
116 die("Can't open $db_script: $!");
117 }
118
119 my $cmd = "sqlite3 $db_file < $db_script";
120
121 if ($ENV{TEST_VERBOSE}) {
122 print STDERR "Executing sqlite3: $cmd\n";
123 }
124
125 my @output = `$cmd`;
126 if (scalar(@output) &&
127 $ENV{TEST_VERBOSE}) {
128 print STDERR "Output: ", join('', @output), "\n";
129 }
130
131 # Make sure that, if we're running as root, the database file has
132 # the permissions/privs set for use by proftpd
133 if ($< == 0) {
134 unless (chmod(0666, $db_file)) {
135 die("Can't set perms on $db_file to 0666: $!");
136 }
137 }
138
139 my $odbc_config = 'sqliteodbc';
140
141 my $ini_file = File::Spec->rel2abs("$tmpdir/odbc.ini");
142 if (open(my $fh, "> $ini_file")) {
143 print $fh <<EOI;
144 [$odbc_config]
145 Description = SqliteODBC
146 Driver = SQLite3
147 Database = $db_file
148 TraceFile = $log_file
149 Trace = off
150 EOI
151 unless (close($fh)) {
152 die("Can't write $ini_file: $!");
153 }
154
155 } else {
156 die("Can't open $ini_file: $!");
157 }
158
159 my $config = {
160 PidFile => $pid_file,
161 ScoreboardFile => $scoreboard_file,
162 SystemLog => $log_file,
163
164 AuthUserFile => $auth_user_file,
165 AuthGroupFile => $auth_group_file,
166 AuthOrder => 'mod_auth_file.c',
167
168 IfModules => {
169 'mod_delay.c' => {
170 DelayEngine => 'off',
171 },
172
173 'mod_sql.c' => {
174 SQLEngine => 'log',
175 SQLBackend => 'odbc',
176 SQLConnectInfo => $odbc_config,
177 SQLLogFile => $log_file,
178 SQLNamedQuery => 'session_start INSERT "\'%u\', \'%L\', \'%{time:%Y-%m-%d %H:%M:%S}\'" ftpsessions',
179 SQLLog => 'PASS session_start',
180 },
181 },
182 };
183
184 # We need to specify the ODBCINI environment variable prior to starting
185 # proftpd
186 $ENV{ODBCINI}=$ini_file;
187
188 my ($port, $config_user, $config_group) = config_write($config_file, $config);
189
190 # Open pipes, for use between the parent and child processes. Specifically,
191 # the child will indicate when it's done with its test by writing a message
192 # to the parent.
193 my ($rfh, $wfh);
194 unless (pipe($rfh, $wfh)) {
195 die("Can't open pipe: $!");
196 }
197
198 my $ex;
199
200 # Fork child
201 $self->handle_sigchld();
202 defined(my $pid = fork()) or die("Can't fork: $!");
203 if ($pid) {
204 eval {
205 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
206
207 $client->login($user, $passwd);
208 };
209
210 if ($@) {
211 $ex = $@;
212 }
213
214 $wfh->print("done\n");
215 $wfh->flush();
216
217 } else {
218 eval { server_wait($config_file, $rfh) };
219 if ($@) {
220 warn($@);
221 exit 1;
222 }
223
224 exit 0;
225 }
226
227 # Stop server
228 server_stop($pid_file);
229
230 $self->assert_child_ok($pid);
231
232 if ($ex) {
233 die($ex);
234 }
235
236 my ($login, $ip_addr, $timestamp) = get_sessions($db_file,
237 "user = \'$user\'");
238
239 my $expected;
240
241 $expected = $user;
242 $self->assert($expected eq $login,
243 test_msg("Expected '$expected', got '$login'"));
244
245 $expected = '127.0.0.1';
246 $self->assert($expected eq $ip_addr,
247 test_msg("Expected '$expected', got '$ip_addr'"));
248
249 $expected = '\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}';
250 $self->assert(qr/$expected/, $timestamp,
251 test_msg("Expected '$expected', got '$timestamp'"));
252
253 unlink($log_file);
254 }
255
256 sub sql_custom_user_info {
257 my $self = shift;
258 my $tmpdir = $self->{tmpdir};
259
260 my $config_file = "$tmpdir/odbc.conf";
261 my $pid_file = File::Spec->rel2abs("$tmpdir/odbc.pid");
262 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/odbc.scoreboard");
263
264 my $log_file = File::Spec->rel2abs('tests.log');
265
266 my $user = 'proftpd';
267 my $passwd = 'test';
268 my $group = 'ftpd';
269 my $home_dir = File::Spec->rel2abs($tmpdir);
270 my $uid = 500;
271 my $gid = 500;
272
273 # Make sure that, if we're running as root, that the home directory has
274 # permissions/privs set for the account we create
275 if ($< == 0) {
276 unless (chmod(0755, $home_dir)) {
277 die("Can't set perms on $home_dir to 0755: $!");
278 }
279
280 unless (chown($uid, $gid, $home_dir)) {
281 die("Can't set owner of $home_dir to $uid/$gid: $!");
282 }
283 }
284
285 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
286
287 # Build up sqlite3 command to create users, groups tables and populate them
288 my $db_script = File::Spec->rel2abs("$tmpdir/proftpd.sql");
289
290 if (open(my $fh, "> $db_script")) {
291 print $fh <<EOS;
292 CREATE TABLE ftpusers (
293 userid TEXT,
294 passwd TEXT,
295 uid INTEGER,
296 gid INTEGER,
297 homedir TEXT,
298 shell TEXT,
299 lastdir TEXT
300 );
301 INSERT INTO ftpusers (userid, passwd, uid, gid, homedir, shell) VALUES ('$user', '$passwd', $uid, $gid, '$home_dir', '/bin/bash');
302
303 CREATE TABLE groups (
304 groupname TEXT,
305 gid INTEGER,
306 members TEXT
307 );
308 INSERT INTO groups (groupname, gid, members) VALUES ('$group', $gid, '$user');
309 EOS
310
311 unless (close($fh)) {
312 die("Can't write $db_script: $!");
313 }
314
315 } else {
316 die("Can't open $db_script: $!");
317 }
318
319 my $cmd = "sqlite3 $db_file < $db_script";
320
321 if ($ENV{TEST_VERBOSE}) {
322 print STDERR "Executing sqlite3: $cmd\n";
323 }
324
325 my @output = `$cmd`;
326 if (scalar(@output) &&
327 $ENV{TEST_VERBOSE}) {
328 print STDERR "Output: ", join('', @output), "\n";
329 }
330
331 # Make sure that, if we're running as root, the database file has
332 # the permissions/privs set for use by proftpd
333 if ($< == 0) {
334 unless (chmod(0666, $db_file)) {
335 die("Can't set perms on $db_file to 0666: $!");
336 }
337 }
338
339 my $odbc_config = 'sqliteodbc';
340
341 my $ini_file = File::Spec->rel2abs("$tmpdir/odbc.ini");
342 if (open(my $fh, "> $ini_file")) {
343 print $fh <<EOI;
344 [$odbc_config]
345 Description = SqliteODBC
346 Driver = SQLite3
347 Database = $db_file
348 TraceFile = $log_file
349 Trace = off
350 EOI
351 unless (close($fh)) {
352 die("Can't write $ini_file: $!");
353 }
354
355 } else {
356 die("Can't open $ini_file: $!");
357 }
358
359 my $config = {
360 PidFile => $pid_file,
361 ScoreboardFile => $scoreboard_file,
362 SystemLog => $log_file,
363
364 IfModules => {
365 'mod_delay.c' => {
366 DelayEngine => 'off',
367 },
368
369 'mod_sql.c' => [
370 'SQLAuthTypes plaintext',
371 'SQLBackend odbc',
372 "SQLConnectInfo $odbc_config",
373 "SQLLogFile $log_file",
374 'SQLNamedQuery get-user-by-name SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE userid = \'%U\'"',
375 'SQLNamedQuery get-user-by-name SELECT "userid, passwd, uid, gid, homedir, shell FROM ftpusers WHERE uid = %{0}"',
376 'SQLUserInfo custom:/get-user-by-name/get-user-by-id',
377 ],
378 },
379 };
380
381 # We need to specify the ODBCINI environment variable prior to starting
382 # proftpd
383 $ENV{ODBCINI}=$ini_file;
384
385 my ($port, $config_user, $config_group) = config_write($config_file, $config);
386
387 # Open pipes, for use between the parent and child processes. Specifically,
388 # the child will indicate when it's done with its test by writing a message
389 # to the parent.
390 my ($rfh, $wfh);
391 unless (pipe($rfh, $wfh)) {
392 die("Can't open pipe: $!");
393 }
394
395 my $ex;
396
397 # Fork child
398 $self->handle_sigchld();
399 defined(my $pid = fork()) or die("Can't fork: $!");
400 if ($pid) {
401 eval {
402 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
403
404 $client->login($user, $passwd);
405
406 my $resp_msgs = $client->response_msgs();
407 my $nmsgs = scalar(@$resp_msgs);
408
409 my $expected;
410
411 $expected = 1;
412 $self->assert($expected == $nmsgs,
413 test_msg("Expected $expected, got $nmsgs"));
414
415 $expected = "User proftpd logged in";
416 $self->assert($expected eq $resp_msgs->[0],
417 test_msg("Expected '$expected', got '$resp_msgs->[0]'"));
418
419 };
420
421 if ($@) {
422 $ex = $@;
423 }
424
425 $wfh->print("done\n");
426 $wfh->flush();
427
428 } else {
429 eval { server_wait($config_file, $rfh) };
430 if ($@) {
431 warn($@);
432 exit 1;
433 }
434
435 exit 0;
436 }
437
438 # Stop server
439 server_stop($pid_file);
440
441 $self->assert_child_ok($pid);
442
443 if ($ex) {
444 die($ex);
445 }
446
447 unlink($log_file);
448 }
449
450 1;