Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_auth_otp;
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 auth_otp_hotp_host => {
20 order => ++$order,
21 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
22 },
23
24 # HOTP tests
25 auth_otp_hotp_login => {
26 order => ++$order,
27 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
28 },
29
30 auth_otp_consecutive_hotp_logins => {
31 order => ++$order,
32 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
33 },
34
35 auth_otp_hotp_unconfigured_user => {
36 order => ++$order,
37 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
38 },
39
40 auth_otp_hotp_authoritative => {
41 order => ++$order,
42 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
43 },
44
45 # TOTP tests
46 auth_otp_totp_login => {
47 order => ++$order,
48 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
49 },
50
51 auth_otp_consecutive_totp_logins => {
52 order => ++$order,
53 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
54 },
55
56 auth_otp_totp_unconfigured_user => {
57 order => ++$order,
58 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
59 },
60
61 auth_otp_totp_authoritative => {
62 order => ++$order,
63 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
64 },
65
66 # AuthOTPOptions tests
67 auth_otp_opt_ftp_std_response => {
68 order => ++$order,
69 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
70 },
71
72 auth_otp_opt_require_table_entry => {
73 order => ++$order,
74 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
75 },
76
77 auth_otp_opt_require_table_entry_authoritative => {
78 order => ++$order,
79 test_class => [qw(forking mod_auth_otp mod_sql mod_sql_sqlite)],
80 },
81
82 };
83
84 sub new {
85 return shift()->SUPER::new(@_);
86 }
87
88 sub list_tests {
89 # Check for the required Perl modules:
90 #
91 # Authen-OATH
92 # MIME-Base32
93
94 my $required = [qw(
95 Authen::OATH
96 MIME::Base32
97 )];
98
99 foreach my $req (@$required) {
100 eval "use $req";
101 if ($@) {
102 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n";
103
104 if ($ENV{TEST_VERBOSE}) {
105 print STDERR "Unable to load $req: $@\n";
106 }
107
108 return qw(testsuite_empty_test);
109 }
110 }
111
112 return testsuite_get_runnable_tests($TESTS);
113 }
114
115 sub build_db {
116 my $cmd = shift;
117 my $db_script = shift;
118 my $db_file = shift;
119 my $check_exit_status = shift;
120 $check_exit_status = 0 unless defined $check_exit_status;
121
122 if ($ENV{TEST_VERBOSE}) {
123 print STDERR "Executing sqlite3: $cmd\n";
124 }
125
126 my @output = `$cmd`;
127 my $exit_status = $?;
128
129 if ($ENV{TEST_VERBOSE}) {
130 print STDERR "Output: ", join('', @output), "\n";
131 }
132
133 if ($check_exit_status) {
134 if ($? != 0) {
135 croak("'$cmd' failed");
136 }
137 }
138
139 # Make sure that, if we're running as root, the database file has
140 # the permissions/privs set for use by proftpd
141 if ($< == 0) {
142 unless (chmod(0666, $db_file)) {
143 croak("Can't set perms on $db_file to 0666: $!");
144 }
145 }
146
147 unlink($db_script);
148 return 1;
149 }
150
151 sub auth_otp_hotp_host {
152 my $self = shift;
153 my $tmpdir = $self->{tmpdir};
154 my $setup = test_setup($tmpdir, 'auth_otp');
155
156 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
157
158 # Build up sqlite3 command to create HOTP tables
159 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
160
161 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
162 # with Google Authenticator.
163 require MIME::Base32;
164 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
165
166 my $secret = 'Sup3rS3Cr3t';
167 my $base32_secret = MIME::Base32::encode($secret);
168 my $counter = 777;
169
170 if (open(my $fh, "> $db_script")) {
171 print $fh <<EOS;
172 CREATE TABLE auth_otp (
173 user TEXT PRIMARY KEY,
174 secret TEXT,
175 counter INTEGER
176 );
177 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
178
179 EOS
180
181 unless (close($fh)) {
182 die("Can't write $db_script: $!");
183 }
184
185 } else {
186 die("Can't open $db_script: $!");
187 }
188
189 my $cmd = "sqlite3 $db_file < $db_script";
190 build_db($cmd, $db_script, $db_file);
191
192 my $config = {
193 PidFile => $setup->{pid_file},
194 ScoreboardFile => $setup->{scoreboard_file},
195 SystemLog => $setup->{log_file},
196 TraceLog => $setup->{log_file},
197 Trace => 'auth:20 auth_otp:20 events:20',
198
199 AuthUserFile => $setup->{auth_user_file},
200 AuthGroupFile => $setup->{auth_group_file},
201 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
202
203 DefaultServer => 'on',
204 ServerName => '"Default Server"',
205
206 IfModules => {
207 'mod_delay.c' => {
208 DelayEngine => 'off',
209 },
210
211 'mod_auth_otp.c' => {
212 AuthOTPEngine => 'off',
213 },
214
215 'mod_sql.c' => {
216 SQLEngine => 'off',
217 },
218 },
219 };
220
221 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
222 $config);
223
224 my $host = 'localhost';
225
226 if (open(my $fh, ">> $setup->{config_file}")) {
227 print $fh <<EOC;
228 # This virtual host is name-based
229 <VirtualHost 127.0.0.1>
230 Port $port
231 ServerAlias $host
232 ServerName "OTP Server"
233
234 AuthUserFile $setup->{auth_user_file}
235 AuthGroupFile $setup->{auth_group_file}
236 AuthOrder mod_auth_otp.c* mod_auth_file.c
237
238 <IfModule mod_delay.c>
239 DelayEngine off
240 </IfModule>
241
242 <IfModule mod_auth_otp.c>
243 AuthOTPEngine on
244 AuthOTPLog $setup->{log_file}
245 AuthOTPAlgorithm hotp
246 AuthOTPTable sql:/get-user-hotp/update-user-hotp
247 </IfModule>
248
249 <IfModule mod_sql.c>
250 SQLEngine log
251 SQLBackend sqlite3
252 SQLConnectInfo $db_file
253 SQLLogFile $setup->{log_file}
254
255 SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"
256 SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp
257 </IfModule>
258 </VirtualHost>
259 EOC
260 unless (close($fh)) {
261 die("Can't write $setup->{config_file}: $!");
262 }
263
264 } else {
265 die("Can't open $setup->{config_file}: $!");
266 }
267
268 # Open pipes, for use between the parent and child processes. Specifically,
269 # the child will indicate when it's done with its test by writing a message
270 # to the parent.
271 my ($rfh, $wfh);
272 unless (pipe($rfh, $wfh)) {
273 die("Can't open pipe: $!");
274 }
275
276 my $ex;
277
278 require Authen::OATH;
279
280 # Fork child
281 $self->handle_sigchld();
282 defined(my $pid = fork()) or die("Can't fork: $!");
283 if ($pid) {
284 eval {
285 sleep(2);
286
287 my $oath = Authen::OATH->new();
288 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
289
290 my ($resp_code, $resp_msg) = $client->host($host);
291 my $expected = 220;
292 $self->assert($expected == $resp_code,
293 test_msg("Expected response code $expected, got $resp_code"));
294
295 # Calculate HOTP
296 my $hotp = $oath->hotp($secret, $counter);
297 if ($ENV{TEST_VERBOSE}) {
298 print STDERR "# Generated HOTP $hotp for counter ", $counter, "\n";
299 }
300
301 $client->login($setup->{user}, $hotp);
302 $client->quit();
303 };
304
305 if ($@) {
306 $ex = $@;
307 }
308
309 $wfh->print("done\n");
310 $wfh->flush();
311
312 } else {
313 eval { server_wait($setup->{config_file}, $rfh) };
314 if ($@) {
315 warn($@);
316 exit 1;
317 }
318
319 exit 0;
320 }
321
322 # Stop server
323 server_stop($setup->{pid_file});
324
325 $self->assert_child_ok($pid);
326
327 test_cleanup($setup->{log_file}, $ex);
328 }
329
330 sub auth_otp_hotp_login {
331 my $self = shift;
332 my $tmpdir = $self->{tmpdir};
333 my $setup = test_setup($tmpdir, 'auth_otp');
334
335 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
336
337 # Build up sqlite3 command to create HOTP tables
338 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
339
340 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
341 # with Google Authenticator.
342 require MIME::Base32;
343 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
344
345 my $secret = 'Sup3rS3Cr3t';
346 my $base32_secret = MIME::Base32::encode($secret);
347 my $counter = 777;
348
349 if (open(my $fh, "> $db_script")) {
350 print $fh <<EOS;
351 CREATE TABLE auth_otp (
352 user TEXT PRIMARY KEY,
353 secret TEXT,
354 counter INTEGER
355 );
356 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
357
358 EOS
359
360 unless (close($fh)) {
361 die("Can't write $db_script: $!");
362 }
363
364 } else {
365 die("Can't open $db_script: $!");
366 }
367
368 my $cmd = "sqlite3 $db_file < $db_script";
369 build_db($cmd, $db_script, $db_file);
370
371 my $config = {
372 PidFile => $setup->{pid_file},
373 ScoreboardFile => $setup->{scoreboard_file},
374 SystemLog => $setup->{log_file},
375 TraceLog => $setup->{log_file},
376 Trace => 'auth:20 auth_otp:20',
377
378 AuthUserFile => $setup->{auth_user_file},
379 AuthGroupFile => $setup->{auth_group_file},
380 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
381
382 IfModules => {
383 'mod_delay.c' => {
384 DelayEngine => 'off',
385 },
386
387 'mod_auth_otp.c' => {
388 AuthOTPEngine => 'on',
389 AuthOTPLog => $setup->{log_file},
390 AuthOTPAlgorithm => 'hotp',
391
392 # Assumes default table names, column names
393 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
394 },
395
396 'mod_sql.c' => [
397 'SQLEngine log',
398 'SQLBackend sqlite3',
399 "SQLConnectInfo $db_file",
400 "SQLLogFile $setup->{log_file}",
401
402 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
403 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
404 ],
405 },
406 };
407
408 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
409 $config);
410
411 # Open pipes, for use between the parent and child processes. Specifically,
412 # the child will indicate when it's done with its test by writing a message
413 # to the parent.
414 my ($rfh, $wfh);
415 unless (pipe($rfh, $wfh)) {
416 die("Can't open pipe: $!");
417 }
418
419 my $ex;
420
421 require Authen::OATH;
422
423 # Fork child
424 $self->handle_sigchld();
425 defined(my $pid = fork()) or die("Can't fork: $!");
426 if ($pid) {
427 eval {
428 sleep(2);
429
430 my $oath = Authen::OATH->new();
431 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
432
433 # Calculate HOTP
434 my $hotp = $oath->hotp($secret, $counter);
435 if ($ENV{TEST_VERBOSE}) {
436 print STDERR "# Generated HOTP $hotp for counter ", $counter, "\n";
437 }
438
439 $client->login($setup->{user}, $hotp);
440 $client->quit();
441 };
442
443 if ($@) {
444 $ex = $@;
445 }
446
447 $wfh->print("done\n");
448 $wfh->flush();
449
450 } else {
451 eval { server_wait($setup->{config_file}, $rfh) };
452 if ($@) {
453 warn($@);
454 exit 1;
455 }
456
457 exit 0;
458 }
459
460 # Stop server
461 server_stop($setup->{pid_file});
462
463 $self->assert_child_ok($pid);
464
465 test_cleanup($setup->{log_file}, $ex);
466 }
467
468 sub auth_otp_consecutive_hotp_logins {
469 my $self = shift;
470 my $tmpdir = $self->{tmpdir};
471 my $setup = test_setup($tmpdir, 'auth_otp');
472
473 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
474
475 # Build up sqlite3 command to create HOTP tables
476 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
477
478 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
479 # with Google Authenticator.
480 require MIME::Base32;
481 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
482
483 my $secret = 'Sup3rS3Cr3t';
484 my $base32_secret = MIME::Base32::encode($secret);
485 my $counter = 777;
486
487 if (open(my $fh, "> $db_script")) {
488 print $fh <<EOS;
489 CREATE TABLE auth_otp (
490 user TEXT PRIMARY KEY,
491 secret TEXT,
492 counter INTEGER
493 );
494 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
495
496 EOS
497
498 unless (close($fh)) {
499 die("Can't write $db_script: $!");
500 }
501
502 } else {
503 die("Can't open $db_script: $!");
504 }
505
506 my $cmd = "sqlite3 $db_file < $db_script";
507 build_db($cmd, $db_script, $db_file);
508
509 my $config = {
510 PidFile => $setup->{pid_file},
511 ScoreboardFile => $setup->{scoreboard_file},
512 SystemLog => $setup->{log_file},
513
514 AuthUserFile => $setup->{auth_user_file},
515 AuthGroupFile => $setup->{auth_group_file},
516 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
517
518 IfModules => {
519 'mod_delay.c' => {
520 DelayEngine => 'off',
521 },
522
523 'mod_auth_otp.c' => {
524 AuthOTPEngine => 'on',
525 AuthOTPLog => $setup->{log_file},
526 AuthOTPAlgorithm => 'hotp',
527
528 # Assumes default table names, column names
529 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
530 },
531
532 'mod_sql.c' => [
533 'SQLEngine log',
534 'SQLBackend sqlite3',
535 "SQLConnectInfo $db_file",
536 "SQLLogFile $setup->{log_file}",
537
538 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
539 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
540 ],
541 },
542 };
543
544 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
545 $config);
546
547 # Open pipes, for use between the parent and child processes. Specifically,
548 # the child will indicate when it's done with its test by writing a message
549 # to the parent.
550 my ($rfh, $wfh);
551 unless (pipe($rfh, $wfh)) {
552 die("Can't open pipe: $!");
553 }
554
555 my $ex;
556
557 require Authen::OATH;
558
559 # Fork child
560 $self->handle_sigchld();
561 defined(my $pid = fork()) or die("Can't fork: $!");
562 if ($pid) {
563 eval {
564 sleep(2);
565
566 my $oath = Authen::OATH->new();
567 my $nattempts = 5;
568 my $ok = 0;
569
570 for (my $i = 0; $i < $nattempts; $i++) {
571 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
572
573 # Calculate HOTP
574 my $next_counter = $counter + $i;
575 my $hotp = $oath->hotp($secret, $next_counter);
576 if ($ENV{TEST_VERBOSE}) {
577 print STDERR "# Generated HOTP $hotp for counter $next_counter\n";
578 }
579
580 eval { $client->login($setup->{user}, $hotp) };
581 if ($@) {
582 next;
583 }
584
585 $client->quit();
586 $ok = 1;
587 last;
588 }
589
590 $self->assert($ok, test_msg("Failed to login successfully using HOTP"));
591 };
592
593 if ($@) {
594 $ex = $@;
595 }
596
597 $wfh->print("done\n");
598 $wfh->flush();
599
600 } else {
601 eval { server_wait($setup->{config_file}, $rfh) };
602 if ($@) {
603 warn($@);
604 exit 1;
605 }
606
607 exit 0;
608 }
609
610 # Stop server
611 server_stop($setup->{pid_file});
612
613 $self->assert_child_ok($pid);
614
615 test_cleanup($setup->{log_file}, $ex);
616 }
617
618 sub auth_otp_hotp_unconfigured_user {
619 my $self = shift;
620 my $tmpdir = $self->{tmpdir};
621 my $setup = test_setup($tmpdir, 'auth_otp');
622
623 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
624
625 # Build up sqlite3 command to create HOTP tables
626 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
627
628 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
629 # with Google Authenticator.
630 require MIME::Base32;
631 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
632
633 my $secret = 'Sup3rS3Cr3t';
634 my $base32_secret = MIME::Base32::encode($secret);
635 my $counter = 777;
636
637 if (open(my $fh, "> $db_script")) {
638 print $fh <<EOS;
639 CREATE TABLE auth_otp (
640 user TEXT PRIMARY KEY,
641 secret TEXT,
642 counter INTEGER
643 );
644
645 EOS
646
647 unless (close($fh)) {
648 die("Can't write $db_script: $!");
649 }
650
651 } else {
652 die("Can't open $db_script: $!");
653 }
654
655 my $cmd = "sqlite3 $db_file < $db_script";
656 build_db($cmd, $db_script, $db_file);
657
658 my $config = {
659 PidFile => $setup->{pid_file},
660 ScoreboardFile => $setup->{scoreboard_file},
661 SystemLog => $setup->{log_file},
662
663 AuthUserFile => $setup->{auth_user_file},
664 AuthGroupFile => $setup->{auth_group_file},
665 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
666
667 IfModules => {
668 'mod_delay.c' => {
669 DelayEngine => 'off',
670 },
671
672 'mod_auth_otp.c' => {
673 AuthOTPEngine => 'on',
674 AuthOTPLog => $setup->{log_file},
675 AuthOTPAlgorithm => 'hotp',
676
677 # Assumes default table names, column names
678 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
679
680 # Table entries are now required by default, per Issue #1562
681 AuthOTPOptions => 'OptionalTableEntry',
682 },
683
684 'mod_sql.c' => [
685 'SQLEngine log',
686 'SQLBackend sqlite3',
687 "SQLConnectInfo $db_file",
688 "SQLLogFile $setup->{log_file}",
689
690 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
691 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
692 ],
693 },
694 };
695
696 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
697 $config);
698
699 # Open pipes, for use between the parent and child processes. Specifically,
700 # the child will indicate when it's done with its test by writing a message
701 # to the parent.
702 my ($rfh, $wfh);
703 unless (pipe($rfh, $wfh)) {
704 die("Can't open pipe: $!");
705 }
706
707 my $ex;
708
709 require Authen::OATH;
710
711 # Fork child
712 $self->handle_sigchld();
713 defined(my $pid = fork()) or die("Can't fork: $!");
714 if ($pid) {
715 eval {
716 sleep(2);
717
718 my $oath = Authen::OATH->new();
719 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
720
721 # Calculate HOTP
722 my $hotp = $oath->hotp($secret, $counter);
723 if ($ENV{TEST_VERBOSE}) {
724 print STDERR "# Generated HOTP $hotp for counter ", $counter, "\n";
725 }
726
727 eval { $client->login($setup->{user}, $hotp) };
728 unless ($@) {
729 die("HOTP login for user $setup->{user} succeeded unexpectedly");
730 }
731
732 my $resp_code = $client->response_code();
733 my $resp_msg = $client->response_msg();
734
735 my $expected = 530;
736 $self->assert($resp_code == $expected,
737 test_msg("Expected response code $expected, got $resp_code"));
738
739 $expected = 'Login incorrect.';
740 $self->assert($resp_msg eq $expected,
741 test_msg("Expected response message '$expected', got '$resp_msg'"));
742
743 # Try again, this time using the real password. Login should
744 # succeed, since we have not configured mod_auth_otp to be authoritative.
745 $client->login($setup->{user}, $setup->{passwd});
746
747 $client->quit();
748 };
749
750 if ($@) {
751 $ex = $@;
752 }
753
754 $wfh->print("done\n");
755 $wfh->flush();
756
757 } else {
758 eval { server_wait($setup->{config_file}, $rfh) };
759 if ($@) {
760 warn($@);
761 exit 1;
762 }
763
764 exit 0;
765 }
766
767 # Stop server
768 server_stop($setup->{pid_file});
769 $self->assert_child_ok($pid);
770
771 test_cleanup($setup->{log_file}, $ex);
772 }
773
774 sub auth_otp_hotp_authoritative {
775 my $self = shift;
776 my $tmpdir = $self->{tmpdir};
777 my $setup = test_setup($tmpdir, 'auth_otp');
778
779 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
780
781 # Build up sqlite3 command to create HOTP tables
782 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
783
784 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
785 # with Google Authenticator.
786 require MIME::Base32;
787 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
788
789 my $secret = 'Sup3rS3Cr3t';
790 my $base32_secret = MIME::Base32::encode($secret);
791 my $counter = 777;
792
793 if (open(my $fh, "> $db_script")) {
794 print $fh <<EOS;
795 CREATE TABLE auth_otp (
796 user TEXT PRIMARY KEY,
797 secret TEXT,
798 counter INTEGER
799 );
800 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', 1);
801
802 EOS
803
804 unless (close($fh)) {
805 die("Can't write $db_script: $!");
806 }
807
808 } else {
809 die("Can't open $db_script: $!");
810 }
811
812 my $cmd = "sqlite3 $db_file < $db_script";
813 build_db($cmd, $db_script, $db_file);
814
815 my $config = {
816 PidFile => $setup->{pid_file},
817 ScoreboardFile => $setup->{scoreboard_file},
818 SystemLog => $setup->{log_file},
819
820 AuthUserFile => $setup->{auth_user_file},
821 AuthGroupFile => $setup->{auth_group_file},
822 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
823
824 IfModules => {
825 'mod_delay.c' => {
826 DelayEngine => 'off',
827 },
828
829 'mod_auth_otp.c' => {
830 AuthOTPEngine => 'on',
831 AuthOTPLog => $setup->{log_file},
832 AuthOTPAlgorithm => 'hotp',
833
834 # Assumes default table names, column names
835 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
836 },
837
838 'mod_sql.c' => [
839 'SQLEngine log',
840 'SQLBackend sqlite3',
841 "SQLConnectInfo $db_file",
842 "SQLLogFile $setup->{log_file}",
843
844 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
845 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
846 ],
847 },
848 };
849
850 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
851 $config);
852
853 # Open pipes, for use between the parent and child processes. Specifically,
854 # the child will indicate when it's done with its test by writing a message
855 # to the parent.
856 my ($rfh, $wfh);
857 unless (pipe($rfh, $wfh)) {
858 die("Can't open pipe: $!");
859 }
860
861 my $ex;
862
863 require Authen::OATH;
864
865 # Fork child
866 $self->handle_sigchld();
867 defined(my $pid = fork()) or die("Can't fork: $!");
868 if ($pid) {
869 eval {
870 sleep(2);
871
872 my $oath = Authen::OATH->new();
873 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
874
875 # Calculate HOTP
876 my $hotp = $oath->hotp($secret, $counter);
877 if ($ENV{TEST_VERBOSE}) {
878 print STDERR "# Generated HOTP $hotp for counter ", $counter, "\n";
879 }
880
881 eval { $client->login($setup->{user}, $hotp) };
882 unless ($@) {
883 die("HOTP login for user $setup->{user} succeeded unexpectedly");
884 }
885
886 my $resp_code = $client->response_code();
887 my $resp_msg = $client->response_msg();
888
889 my $expected = 530;
890 $self->assert($resp_code == $expected,
891 test_msg("Expected response code $expected, got $resp_code"));
892
893 $expected = 'Login incorrect.';
894 $self->assert($resp_msg eq $expected,
895 test_msg("Expected response message '$expected', got '$resp_msg'"));
896
897 # Try again, this time using the real password. Login should
898 # still fail, since we require the OTP to be valid.
899 eval { $client->login($setup->{user}, $setup->{passwd}) };
900 unless ($@) {
901 die("Password login for user $setup->{user} succeeded unexpectedly");
902 }
903
904 $client->quit();
905 };
906
907 if ($@) {
908 $ex = $@;
909 }
910
911 $wfh->print("done\n");
912 $wfh->flush();
913
914 } else {
915 eval { server_wait($setup->{config_file}, $rfh) };
916 if ($@) {
917 warn($@);
918 exit 1;
919 }
920
921 exit 0;
922 }
923
924 # Stop server
925 server_stop($setup->{pid_file});
926
927 $self->assert_child_ok($pid);
928
929 test_cleanup($setup->{log_file}, $ex);
930 }
931
932 sub auth_otp_totp_login {
933 my $self = shift;
934 my $tmpdir = $self->{tmpdir};
935 my $setup = test_setup($tmpdir, 'auth_otp');
936
937 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
938
939 # Build up sqlite3 command to create TOTP tables
940 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
941
942 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
943 # with Google Authenticator.
944 require MIME::Base32;
945 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
946
947 my $secret = 'Sup3rS3Cr3t';
948 my $base32_secret = MIME::Base32::encode($secret);
949
950 if (open(my $fh, "> $db_script")) {
951 print $fh <<EOS;
952 CREATE TABLE auth_otp (
953 user TEXT PRIMARY KEY,
954 secret TEXT,
955 counter INTEGER
956 );
957 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', 0);
958
959 EOS
960
961 unless (close($fh)) {
962 die("Can't write $db_script: $!");
963 }
964
965 } else {
966 die("Can't open $db_script: $!");
967 }
968
969 my $cmd = "sqlite3 $db_file < $db_script";
970 build_db($cmd, $db_script, $db_file);
971
972 my $config = {
973 PidFile => $setup->{pid_file},
974 ScoreboardFile => $setup->{scoreboard_file},
975 SystemLog => $setup->{log_file},
976
977 AuthUserFile => $setup->{auth_user_file},
978 AuthGroupFile => $setup->{auth_group_file},
979 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
980
981 IfModules => {
982 'mod_delay.c' => {
983 DelayEngine => 'off',
984 },
985
986 'mod_auth_otp.c' => {
987 AuthOTPEngine => 'on',
988 AuthOTPLog => $setup->{log_file},
989 AuthOTPAlgorithm => 'totp',
990
991 # Assumes default table names, column names
992 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
993 },
994
995 'mod_sql.c' => [
996 'SQLEngine log',
997 'SQLBackend sqlite3',
998 "SQLConnectInfo $db_file",
999 "SQLLogFile $setup->{log_file}",
1000
1001 'SQLNamedQuery get-user-totp SELECT "secret FROM auth_otp WHERE user = \'%{0}\'"',
1002 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1003 ],
1004 },
1005 };
1006
1007 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1008 $config);
1009
1010 # Open pipes, for use between the parent and child processes. Specifically,
1011 # the child will indicate when it's done with its test by writing a message
1012 # to the parent.
1013 my ($rfh, $wfh);
1014 unless (pipe($rfh, $wfh)) {
1015 die("Can't open pipe: $!");
1016 }
1017
1018 my $ex;
1019
1020 require Authen::OATH;
1021
1022 # Fork child
1023 $self->handle_sigchld();
1024 defined(my $pid = fork()) or die("Can't fork: $!");
1025 if ($pid) {
1026 eval {
1027 sleep(2);
1028
1029 my $oath = Authen::OATH->new();
1030 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1031
1032 # Calculate TOTP
1033 my $totp = $oath->totp($secret);
1034 if ($ENV{TEST_VERBOSE}) {
1035 print STDERR "# Generated TOTP $totp for current time\n";
1036 }
1037
1038 $client->login($setup->{user}, $totp);
1039 $client->quit();
1040 };
1041
1042 if ($@) {
1043 $ex = $@;
1044 }
1045
1046 $wfh->print("done\n");
1047 $wfh->flush();
1048
1049 } else {
1050 eval { server_wait($setup->{config_file}, $rfh) };
1051 if ($@) {
1052 warn($@);
1053 exit 1;
1054 }
1055
1056 exit 0;
1057 }
1058
1059 # Stop server
1060 server_stop($setup->{pid_file});
1061
1062 $self->assert_child_ok($pid);
1063
1064 test_cleanup($setup->{log_file}, $ex);
1065 }
1066
1067 sub auth_otp_consecutive_totp_logins {
1068 my $self = shift;
1069 my $tmpdir = $self->{tmpdir};
1070 my $setup = test_setup($tmpdir, 'auth_otp');
1071
1072 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1073
1074 # Build up sqlite3 command to create TOTP tables
1075 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
1076
1077 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1078 # with Google Authenticator.
1079 require MIME::Base32;
1080 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
1081
1082 my $secret = 'Sup3rS3Cr3t';
1083 my $base32_secret = MIME::Base32::encode($secret);
1084
1085 if (open(my $fh, "> $db_script")) {
1086 print $fh <<EOS;
1087 CREATE TABLE auth_otp (
1088 user TEXT PRIMARY KEY,
1089 secret TEXT,
1090 counter INTEGER
1091 );
1092 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', 0);
1093
1094 EOS
1095
1096 unless (close($fh)) {
1097 die("Can't write $db_script: $!");
1098 }
1099
1100 } else {
1101 die("Can't open $db_script: $!");
1102 }
1103
1104 my $cmd = "sqlite3 $db_file < $db_script";
1105 build_db($cmd, $db_script, $db_file);
1106
1107 my $config = {
1108 PidFile => $setup->{pid_file},
1109 ScoreboardFile => $setup->{scoreboard_file},
1110 SystemLog => $setup->{log_file},
1111
1112 AuthUserFile => $setup->{auth_user_file},
1113 AuthGroupFile => $setup->{auth_group_file},
1114 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
1115
1116 IfModules => {
1117 'mod_delay.c' => {
1118 DelayEngine => 'off',
1119 },
1120
1121 'mod_auth_otp.c' => {
1122 AuthOTPEngine => 'on',
1123 AuthOTPLog => $setup->{log_file},
1124 AuthOTPAlgorithm => 'totp',
1125
1126 # Assumes default table names, column names
1127 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
1128 },
1129
1130 'mod_sql.c' => [
1131 'SQLEngine log',
1132 'SQLBackend sqlite3',
1133 "SQLConnectInfo $db_file",
1134 "SQLLogFile $setup->{log_file}",
1135
1136 'SQLNamedQuery get-user-totp SELECT "secret FROM auth_otp WHERE user = \'%{0}\'"',
1137 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1138 ],
1139 },
1140 };
1141
1142 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1143 $config);
1144
1145 # Open pipes, for use between the parent and child processes. Specifically,
1146 # the child will indicate when it's done with its test by writing a message
1147 # to the parent.
1148 my ($rfh, $wfh);
1149 unless (pipe($rfh, $wfh)) {
1150 die("Can't open pipe: $!");
1151 }
1152
1153 my $ex;
1154
1155 require Authen::OATH;
1156
1157 # Fork child
1158 $self->handle_sigchld();
1159 defined(my $pid = fork()) or die("Can't fork: $!");
1160 if ($pid) {
1161 eval {
1162 sleep(2);
1163
1164 my $oath = Authen::OATH->new();
1165 my $nattempts = 3;
1166 my $now = time();
1167 my $ok = 0;
1168
1169 for (my $i = 0; $i < $nattempts; $i++) {
1170 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1171
1172 # Calculate TOTP
1173 my $ts = $now;
1174
1175 # Try one "time step" behind and ahead.
1176 if ($i == 0) {
1177 $ts = $now - 30;
1178
1179 } elsif ($i == 2) {
1180 $ts = $now + 30;
1181 }
1182
1183 my $totp = $oath->totp($secret, $ts);
1184 if ($ENV{TEST_VERBOSE}) {
1185 print STDERR "# Generated TOTP $totp for ", scalar(localtime($ts)), "\n";
1186 }
1187
1188 eval { $client->login($setup->{user}, $totp) };
1189 if ($@) {
1190 next;
1191 }
1192
1193 $client->quit();
1194 $ok = 1;
1195 last;
1196 }
1197
1198 $self->assert($ok, test_msg("Failed to login successfully using TOTP"));
1199 };
1200
1201 if ($@) {
1202 $ex = $@;
1203 }
1204
1205 $wfh->print("done\n");
1206 $wfh->flush();
1207
1208 } else {
1209 eval { server_wait($setup->{config_file}, $rfh) };
1210 if ($@) {
1211 warn($@);
1212 exit 1;
1213 }
1214
1215 exit 0;
1216 }
1217
1218 # Stop server
1219 server_stop($setup->{pid_file});
1220
1221 $self->assert_child_ok($pid);
1222
1223 test_cleanup($setup->{log_file}, $ex);
1224 }
1225
1226 sub auth_otp_totp_unconfigured_user {
1227 my $self = shift;
1228 my $tmpdir = $self->{tmpdir};
1229 my $setup = test_setup($tmpdir, 'auth_otp');
1230
1231 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1232
1233 # Build up sqlite3 command to create HOTP tables
1234 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
1235
1236 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1237 # with Google Authenticator.
1238 require MIME::Base32;
1239 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
1240
1241 my $secret = 'Sup3rS3Cr3t';
1242 my $base32_secret = MIME::Base32::encode($secret);
1243 my $counter = 777;
1244
1245 if (open(my $fh, "> $db_script")) {
1246 print $fh <<EOS;
1247 CREATE TABLE auth_otp (
1248 user TEXT PRIMARY KEY,
1249 secret TEXT,
1250 counter INTEGER
1251 );
1252
1253 EOS
1254
1255 unless (close($fh)) {
1256 die("Can't write $db_script: $!");
1257 }
1258
1259 } else {
1260 die("Can't open $db_script: $!");
1261 }
1262
1263 my $cmd = "sqlite3 $db_file < $db_script";
1264 build_db($cmd, $db_script, $db_file);
1265
1266 my $config = {
1267 PidFile => $setup->{pid_file},
1268 ScoreboardFile => $setup->{scoreboard_file},
1269 SystemLog => $setup->{log_file},
1270
1271 AuthUserFile => $setup->{auth_user_file},
1272 AuthGroupFile => $setup->{auth_group_file},
1273 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
1274
1275 IfModules => {
1276 'mod_delay.c' => {
1277 DelayEngine => 'off',
1278 },
1279
1280 'mod_auth_otp.c' => {
1281 AuthOTPEngine => 'on',
1282 AuthOTPLog => $setup->{log_file},
1283 AuthOTPAlgorithm => 'totp',
1284
1285 # Assumes default table names, column names
1286 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
1287
1288 # Table entries are now required by default, per Issue #1562
1289 AuthOTPOptions => 'OptionalTableEntry',
1290 },
1291
1292 'mod_sql.c' => [
1293 'SQLEngine log',
1294 'SQLBackend sqlite3',
1295 "SQLConnectInfo $db_file",
1296 "SQLLogFile $setup->{log_file}",
1297
1298 'SQLNamedQuery get-user-totp SELECT "secret FROM auth_otp WHERE user = \'%{0}\'"',
1299 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1300 ],
1301 },
1302 };
1303
1304 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1305 $config);
1306
1307 # Open pipes, for use between the parent and child processes. Specifically,
1308 # the child will indicate when it's done with its test by writing a message
1309 # to the parent.
1310 my ($rfh, $wfh);
1311 unless (pipe($rfh, $wfh)) {
1312 die("Can't open pipe: $!");
1313 }
1314
1315 my $ex;
1316
1317 require Authen::OATH;
1318
1319 # Fork child
1320 $self->handle_sigchld();
1321 defined(my $pid = fork()) or die("Can't fork: $!");
1322 if ($pid) {
1323 eval {
1324 sleep(2);
1325
1326 my $oath = Authen::OATH->new();
1327 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1328
1329 # Calculate TOTP
1330 my $totp = $oath->totp($secret);
1331 if ($ENV{TEST_VERBOSE}) {
1332 print STDERR "# Generated TOTP $totp for current time\n";
1333 }
1334
1335 eval { $client->login($setup->{user}, $totp) };
1336 unless ($@) {
1337 die("TOTP login for user $setup->{user} succeeded unexpectedly");
1338 }
1339
1340 my $resp_code = $client->response_code();
1341 my $resp_msg = $client->response_msg();
1342
1343 my $expected = 530;
1344 $self->assert($resp_code == $expected,
1345 test_msg("Expected response code $expected, got $resp_code"));
1346
1347 $expected = 'Login incorrect.';
1348 $self->assert($resp_msg eq $expected,
1349 test_msg("Expected response message '$expected', got '$resp_msg'"));
1350
1351 # Try again, this time using the real password. Login should
1352 # succeed, since we have not configured mod_auth_otp to be authoritative.
1353 $client->login($setup->{user}, $setup->{passwd});
1354
1355 $client->quit();
1356 };
1357
1358 if ($@) {
1359 $ex = $@;
1360 }
1361
1362 $wfh->print("done\n");
1363 $wfh->flush();
1364
1365 } else {
1366 eval { server_wait($setup->{config_file}, $rfh) };
1367 if ($@) {
1368 warn($@);
1369 exit 1;
1370 }
1371
1372 exit 0;
1373 }
1374
1375 # Stop server
1376 server_stop($setup->{pid_file});
1377 $self->assert_child_ok($pid);
1378
1379 test_cleanup($setup->{log_file}, $ex);
1380 }
1381
1382 sub auth_otp_totp_authoritative {
1383 my $self = shift;
1384 my $tmpdir = $self->{tmpdir};
1385 my $setup = test_setup($tmpdir, 'auth_otp');
1386
1387 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1388
1389 # Build up sqlite3 command to create HOTP tables
1390 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
1391
1392 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1393 # with Google Authenticator.
1394 require MIME::Base32;
1395 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
1396
1397 my $secret = 'Sup3rS3Cr3t';
1398 my $base32_secret = MIME::Base32::encode($secret);
1399 my $counter = 777;
1400
1401 if (open(my $fh, "> $db_script")) {
1402 print $fh <<EOS;
1403 CREATE TABLE auth_otp (
1404 user TEXT PRIMARY KEY,
1405 secret TEXT,
1406 counter INTEGER
1407 );
1408 INSERT INTO auth_otp (user, secret) VALUES ('$setup->{user}', '$base32_secret');
1409
1410 EOS
1411
1412 unless (close($fh)) {
1413 die("Can't write $db_script: $!");
1414 }
1415
1416 } else {
1417 die("Can't open $db_script: $!");
1418 }
1419
1420 my $cmd = "sqlite3 $db_file < $db_script";
1421 build_db($cmd, $db_script, $db_file);
1422
1423 my $config = {
1424 PidFile => $setup->{pid_file},
1425 ScoreboardFile => $setup->{scoreboard_file},
1426 SystemLog => $setup->{log_file},
1427
1428 AuthUserFile => $setup->{auth_user_file},
1429 AuthGroupFile => $setup->{auth_group_file},
1430 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
1431
1432 IfModules => {
1433 'mod_delay.c' => {
1434 DelayEngine => 'off',
1435 },
1436
1437 'mod_auth_otp.c' => {
1438 AuthOTPEngine => 'on',
1439 AuthOTPLog => $setup->{log_file},
1440 AuthOTPAlgorithm => 'totp',
1441
1442 # Assumes default table names, column names
1443 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
1444 },
1445
1446 'mod_sql.c' => [
1447 'SQLEngine log',
1448 'SQLBackend sqlite3',
1449 "SQLConnectInfo $db_file",
1450 "SQLLogFile $setup->{log_file}",
1451
1452 'SQLNamedQuery get-user-totp SELECT "secret FROM auth_otp WHERE user = \'%{0}\'"',
1453 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1454 ],
1455 },
1456 };
1457
1458 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1459 $config);
1460
1461 # Open pipes, for use between the parent and child processes. Specifically,
1462 # the child will indicate when it's done with its test by writing a message
1463 # to the parent.
1464 my ($rfh, $wfh);
1465 unless (pipe($rfh, $wfh)) {
1466 die("Can't open pipe: $!");
1467 }
1468
1469 my $ex;
1470
1471 require Authen::OATH;
1472
1473 # Fork child
1474 $self->handle_sigchld();
1475 defined(my $pid = fork()) or die("Can't fork: $!");
1476 if ($pid) {
1477 eval {
1478 sleep(2);
1479
1480 my $oath = Authen::OATH->new();
1481 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1482
1483 # Calculate TOTP
1484 my $ts = 10;
1485 my $totp = $oath->totp($secret, $ts);
1486 if ($ENV{TEST_VERBOSE}) {
1487 print STDERR "# Generated TOTP $totp for ", scalar(gmtime($ts)), "\n";
1488 }
1489
1490 eval { $client->login($setup->{user}, $totp) };
1491 unless ($@) {
1492 die("TOTP login for user $setup->{user} succeeded unexpectedly");
1493 }
1494
1495 my $resp_code = $client->response_code();
1496 my $resp_msg = $client->response_msg();
1497
1498 my $expected = 530;
1499 $self->assert($resp_code == $expected,
1500 test_msg("Expected response code $expected, got $resp_code"));
1501
1502 $expected = 'Login incorrect.';
1503 $self->assert($resp_msg eq $expected,
1504 test_msg("Expected response message '$expected', got '$resp_msg'"));
1505
1506 # Try again, this time using the real password. Login should still
1507 # fail since mod_auth_otp is authoritative.
1508 eval { $client->login($setup->{user}, $setup->{passwd}) };
1509 unless ($@) {
1510 die("Password login for user $setup->{user} succeeded unexpectedly");
1511 }
1512
1513 $client->quit();
1514 };
1515
1516 if ($@) {
1517 $ex = $@;
1518 }
1519
1520 $wfh->print("done\n");
1521 $wfh->flush();
1522
1523 } else {
1524 eval { server_wait($setup->{config_file}, $rfh) };
1525 if ($@) {
1526 warn($@);
1527 exit 1;
1528 }
1529
1530 exit 0;
1531 }
1532
1533 # Stop server
1534 server_stop($setup->{pid_file});
1535
1536 $self->assert_child_ok($pid);
1537
1538 test_cleanup($setup->{log_file}, $ex);
1539 }
1540
1541 sub auth_otp_opt_ftp_std_response {
1542 my $self = shift;
1543 my $tmpdir = $self->{tmpdir};
1544 my $setup = test_setup($tmpdir, 'auth_otp');
1545
1546 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1547
1548 # Build up sqlite3 command to create HOTP tables
1549 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
1550
1551 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1552 # with Google Authenticator.
1553 require MIME::Base32;
1554 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
1555
1556 my $secret = 'Sup3rS3Cr3t';
1557 my $base32_secret = MIME::Base32::encode($secret);
1558 my $counter = 777;
1559
1560 if (open(my $fh, "> $db_script")) {
1561 print $fh <<EOS;
1562 CREATE TABLE auth_otp (
1563 user TEXT PRIMARY KEY,
1564 secret TEXT,
1565 counter INTEGER
1566 );
1567 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
1568
1569 EOS
1570
1571 unless (close($fh)) {
1572 die("Can't write $db_script: $!");
1573 }
1574
1575 } else {
1576 die("Can't open $db_script: $!");
1577 }
1578
1579 my $cmd = "sqlite3 $db_file < $db_script";
1580 build_db($cmd, $db_script, $db_file);
1581
1582 my $config = {
1583 PidFile => $setup->{pid_file},
1584 ScoreboardFile => $setup->{scoreboard_file},
1585 SystemLog => $setup->{log_file},
1586
1587 AuthUserFile => $setup->{auth_user_file},
1588 AuthGroupFile => $setup->{auth_group_file},
1589 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
1590
1591 IfModules => {
1592 'mod_delay.c' => {
1593 DelayEngine => 'off',
1594 },
1595
1596 'mod_auth_otp.c' => {
1597 AuthOTPEngine => 'on',
1598 AuthOTPLog => $setup->{log_file},
1599 AuthOTPAlgorithm => 'hotp',
1600 AuthOTPOptions => 'FTPStandardResponse',
1601
1602 # Assumes default table names, column names
1603 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
1604 },
1605
1606 'mod_sql.c' => [
1607 'SQLEngine log',
1608 'SQLBackend sqlite3',
1609 "SQLConnectInfo $db_file",
1610 "SQLLogFile $setup->{log_file}",
1611
1612 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
1613 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1614 ],
1615 },
1616 };
1617
1618 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1619 $config);
1620
1621 # Open pipes, for use between the parent and child processes. Specifically,
1622 # the child will indicate when it's done with its test by writing a message
1623 # to the parent.
1624 my ($rfh, $wfh);
1625 unless (pipe($rfh, $wfh)) {
1626 die("Can't open pipe: $!");
1627 }
1628
1629 my $ex;
1630
1631 require Authen::OATH;
1632
1633 # Fork child
1634 $self->handle_sigchld();
1635 defined(my $pid = fork()) or die("Can't fork: $!");
1636 if ($pid) {
1637 eval {
1638 sleep(2);
1639
1640 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1641
1642 my ($resp_code, $resp_msg) = $client->user($setup->{user});
1643
1644 my $expected = 331;
1645 $self->assert($resp_code == $expected,
1646 test_msg("Expected response code $expected, got $resp_code"));
1647
1648 $expected = "Password required for $setup->{user}";
1649 $self->assert($resp_msg eq $expected,
1650 test_msg("Expected response message '$expected', got '$resp_msg'"));
1651
1652 $client->quit();
1653 };
1654
1655 if ($@) {
1656 $ex = $@;
1657 }
1658
1659 $wfh->print("done\n");
1660 $wfh->flush();
1661
1662 } else {
1663 eval { server_wait($setup->{config_file}, $rfh) };
1664 if ($@) {
1665 warn($@);
1666 exit 1;
1667 }
1668
1669 exit 0;
1670 }
1671
1672 # Stop server
1673 server_stop($setup->{pid_file});
1674
1675 $self->assert_child_ok($pid);
1676
1677 test_cleanup($setup->{log_file}, $ex);
1678 }
1679
1680 sub auth_otp_opt_require_table_entry {
1681 my $self = shift;
1682 my $tmpdir = $self->{tmpdir};
1683 my $setup = test_setup($tmpdir, 'auth_otp');
1684
1685 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1686
1687 # Build up sqlite3 command to create HOTP tables
1688 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
1689
1690 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1691 # with Google Authenticator.
1692 require MIME::Base32;
1693 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
1694
1695 my $secret = 'Sup3rS3Cr3t';
1696 my $base32_secret = MIME::Base32::encode($secret);
1697 my $counter = 777;
1698
1699 if (open(my $fh, "> $db_script")) {
1700 print $fh <<EOS;
1701 CREATE TABLE auth_otp (
1702 user TEXT PRIMARY KEY,
1703 secret TEXT,
1704 counter INTEGER
1705 );
1706 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
1707
1708 EOS
1709
1710 unless (close($fh)) {
1711 die("Can't write $db_script: $!");
1712 }
1713
1714 } else {
1715 die("Can't open $db_script: $!");
1716 }
1717
1718 my $cmd = "sqlite3 $db_file < $db_script";
1719 build_db($cmd, $db_script, $db_file);
1720
1721 my $config = {
1722 PidFile => $setup->{pid_file},
1723 ScoreboardFile => $setup->{scoreboard_file},
1724 SystemLog => $setup->{log_file},
1725
1726 AuthUserFile => $setup->{auth_user_file},
1727 AuthGroupFile => $setup->{auth_group_file},
1728 AuthOrder => 'mod_auth_otp.c mod_auth_file.c',
1729
1730 IfModules => {
1731 'mod_delay.c' => {
1732 DelayEngine => 'off',
1733 },
1734
1735 'mod_auth_otp.c' => {
1736 AuthOTPEngine => 'on',
1737 AuthOTPLog => $setup->{log_file},
1738 AuthOTPAlgorithm => 'hotp',
1739 AuthOTPOptions => 'RequireTableEntry',
1740
1741 # Assumes default table names, column names
1742 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
1743 },
1744
1745 'mod_sql.c' => [
1746 'SQLEngine log',
1747 'SQLBackend sqlite3',
1748 "SQLConnectInfo $db_file",
1749 "SQLLogFile $setup->{log_file}",
1750
1751 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
1752 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1753 ],
1754 },
1755 };
1756
1757 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1758 $config);
1759
1760 # Open pipes, for use between the parent and child processes. Specifically,
1761 # the child will indicate when it's done with its test by writing a message
1762 # to the parent.
1763 my ($rfh, $wfh);
1764 unless (pipe($rfh, $wfh)) {
1765 die("Can't open pipe: $!");
1766 }
1767
1768 my $ex;
1769
1770 require Authen::OATH;
1771
1772 # Fork child
1773 $self->handle_sigchld();
1774 defined(my $pid = fork()) or die("Can't fork: $!");
1775 if ($pid) {
1776 eval {
1777 sleep(2);
1778
1779 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1780
1781 # Even though we have not configured an entry in the OTP table
1782 # for this user, and even though we are using the RequireTableEntry
1783 # option, password-based login should still succeed, because we
1784 # did not make mod_auth_otp authoritative.
1785 $client->login($setup->{user}, $setup->{passwd});
1786
1787 $client->quit();
1788 };
1789
1790 if ($@) {
1791 $ex = $@;
1792 }
1793
1794 $wfh->print("done\n");
1795 $wfh->flush();
1796
1797 } else {
1798 eval { server_wait($setup->{config_file}, $rfh) };
1799 if ($@) {
1800 warn($@);
1801 exit 1;
1802 }
1803
1804 exit 0;
1805 }
1806
1807 # Stop server
1808 server_stop($setup->{pid_file});
1809
1810 $self->assert_child_ok($pid);
1811
1812 test_cleanup($setup->{log_file}, $ex);
1813 }
1814
1815 sub auth_otp_opt_require_table_entry_authoritative {
1816 my $self = shift;
1817 my $tmpdir = $self->{tmpdir};
1818 my $setup = test_setup($tmpdir, 'auth_otp');
1819
1820 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1821
1822 # Build up sqlite3 command to create HOTP tables
1823 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
1824
1825 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1826 # with Google Authenticator.
1827 require MIME::Base32;
1828 MIME::Base32->import('RFC') if $MIME::Base32::VERSION <= 1.02;
1829
1830 my $secret = 'Sup3rS3Cr3t';
1831 my $base32_secret = MIME::Base32::encode($secret);
1832 my $counter = 777;
1833
1834 if (open(my $fh, "> $db_script")) {
1835 print $fh <<EOS;
1836 CREATE TABLE auth_otp (
1837 user TEXT PRIMARY KEY,
1838 secret TEXT,
1839 counter INTEGER
1840 );
1841 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
1842
1843 EOS
1844
1845 unless (close($fh)) {
1846 die("Can't write $db_script: $!");
1847 }
1848
1849 } else {
1850 die("Can't open $db_script: $!");
1851 }
1852
1853 my $cmd = "sqlite3 $db_file < $db_script";
1854 build_db($cmd, $db_script, $db_file);
1855
1856 my $config = {
1857 PidFile => $setup->{pid_file},
1858 ScoreboardFile => $setup->{scoreboard_file},
1859 SystemLog => $setup->{log_file},
1860
1861 AuthUserFile => $setup->{auth_user_file},
1862 AuthGroupFile => $setup->{auth_group_file},
1863 AuthOrder => 'mod_auth_otp.c* mod_auth_file.c',
1864
1865 IfModules => {
1866 'mod_delay.c' => {
1867 DelayEngine => 'off',
1868 },
1869
1870 'mod_auth_otp.c' => {
1871 AuthOTPEngine => 'on',
1872 AuthOTPLog => $setup->{log_file},
1873 AuthOTPAlgorithm => 'hotp',
1874 AuthOTPOptions => 'RequireTableEntry',
1875
1876 # Assumes default table names, column names
1877 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
1878 },
1879
1880 'mod_sql.c' => [
1881 'SQLEngine log',
1882 'SQLBackend sqlite3',
1883 "SQLConnectInfo $db_file",
1884 "SQLLogFile $setup->{log_file}",
1885
1886 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
1887 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1888 ],
1889 },
1890 };
1891
1892 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1893 $config);
1894
1895 # Open pipes, for use between the parent and child processes. Specifically,
1896 # the child will indicate when it's done with its test by writing a message
1897 # to the parent.
1898 my ($rfh, $wfh);
1899 unless (pipe($rfh, $wfh)) {
1900 die("Can't open pipe: $!");
1901 }
1902
1903 my $ex;
1904
1905 require Authen::OATH;
1906
1907 # Fork child
1908 $self->handle_sigchld();
1909 defined(my $pid = fork()) or die("Can't fork: $!");
1910 if ($pid) {
1911 eval {
1912 sleep(2);
1913
1914 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
1915
1916 # We have not configured an entry in the OTP table for this user, and
1917 # we are using the RequireTableEntry option, AND we marked mod_auth_otp
1918 # as authoriative. Thus password-based login should fail.
1919 eval { $client->login($setup->{user}, $setup->{passwd}) };
1920 unless ($@) {
1921 die("Password login succeeded unexpectedly");
1922 }
1923
1924 $client->quit();
1925 };
1926
1927 if ($@) {
1928 $ex = $@;
1929 }
1930
1931 $wfh->print("done\n");
1932 $wfh->flush();
1933
1934 } else {
1935 eval { server_wait($setup->{config_file}, $rfh) };
1936 if ($@) {
1937 warn($@);
1938 exit 1;
1939 }
1940
1941 exit 0;
1942 }
1943
1944 # Stop server
1945 server_stop($setup->{pid_file});
1946
1947 $self->assert_child_ok($pid);
1948
1949 test_cleanup($setup->{log_file}, $ex);
1950 }
1951
1952 1;