Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Modules::mod_auth_otp::sftp;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use Data::Dumper;
8 use File::Copy;
9 use File::Spec;
10 use IO::Handle;
11 use POSIX qw(:fcntl_h);
12
13 use ProFTPD::TestSuite::FTP;
14 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
15
16 $| = 1;
17
18 my $order = 0;
19
20 my $TESTS = {
21 # HOTP tests
22 auth_otp_sftp_hotp_login_ok_via_kbdint => {
23 order => ++$order,
24 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
25 },
26
27 auth_otp_sftp_hotp_login_ok_via_password => {
28 order => ++$order,
29 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
30 },
31
32 auth_otp_sftp_hotp_login_failed_bad_secret => {
33 order => ++$order,
34 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
35 },
36
37 auth_otp_sftp_hotp_login_failed_missing_otp_entry_issue1560 => {
38 order => ++$order,
39 test_class => [qw(
40 bug
41 forking
42 mod_auth_otp
43 mod_sftp
44 mod_sql
45 mod_sql_sqlite
46 )],
47 },
48
49 auth_otp_sftp_hotp_login_ok_missing_otp_entry_chained_auth_issue1560 => {
50 order => ++$order,
51 test_class => [qw(
52 bug
53 forking
54 mod_auth_otp
55 mod_sftp
56 mod_sql
57 mod_sql_sqlite
58 )],
59 },
60
61 # TOTP tests
62 auth_otp_sftp_totp_login_ok_via_kbdint => {
63 order => ++$order,
64 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
65 },
66
67 auth_otp_sftp_totp_login_ok_via_password => {
68 order => ++$order,
69 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
70 },
71
72 auth_otp_sftp_totp_login_failed_bad_secret => {
73 order => ++$order,
74 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
75 },
76
77 auth_otp_sftp_totp_login_failed_missing_otp_entry_issue1560 => {
78 order => ++$order,
79 test_class => [qw(
80 bug
81 forking
82 mod_auth_otp
83 mod_sftp
84 mod_sql
85 mod_sql_sqlite
86 )],
87 },
88
89 auth_otp_sftp_totp_login_ok_missing_otp_entry_chained_auth_issue1560 => {
90 order => ++$order,
91 test_class => [qw(
92 bug
93 forking
94 mod_auth_otp
95 mod_sftp
96 mod_sql
97 mod_sql_sqlite
98 )],
99 },
100
101 auth_otp_sftp_totp_login_authoritative => {
102 order => ++$order,
103 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
104 },
105
106 auth_otp_sftp_totp_login_authoritative_failed => {
107 order => ++$order,
108 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
109 },
110
111 # Other tests
112 auth_otp_sftp_password_failed => {
113 order => ++$order,
114 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
115 },
116
117 auth_otp_sftp_publickey_plus_totp_ok => {
118 order => ++$order,
119 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
120 },
121
122 auth_otp_sftp_password_plus_totp_ok => {
123 order => ++$order,
124 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
125 },
126
127 auth_otp_sftp_publickey_plus_totp_with_display_verification_code_opt => {
128 order => ++$order,
129 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
130 },
131
132 auth_otp_sftp_password_plus_totp_with_display_verification_code_opt => {
133 order => ++$order,
134 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
135 },
136
137 auth_otp_sftp_publickey_plus_totp_lacking_table_entry_issue1319 => {
138 order => ++$order,
139 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
140 },
141
142 auth_otp_sftp_password_plus_totp_lacking_table_entry_issue1319 => {
143 order => ++$order,
144 test_class => [qw(forking mod_auth_otp mod_sftp mod_sql mod_sql_sqlite)],
145 },
146
147 };
148
149 sub new {
150 return shift()->SUPER::new(@_);
151 }
152
153 sub list_tests {
154 # Check for the required Perl modules:
155 #
156 # Authen-OATH
157 # MIME-Base32
158
159 my $required = [qw(
160 Authen::OATH
161 MIME::Base32
162 Net::SSH2
163 )];
164
165 foreach my $req (@$required) {
166 eval "use $req";
167 if ($@) {
168 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n";
169
170 if ($ENV{TEST_VERBOSE}) {
171 print STDERR "Unable to load $req: $@\n";
172 }
173
174 return qw(testsuite_empty_test);
175 }
176 }
177
178 return testsuite_get_runnable_tests($TESTS);
179 }
180
181 sub set_up {
182 my $self = shift;
183 $self->SUPER::set_up(@_);
184
185 # Make sure that mod_sftp does not complain about permissions on the hostkey
186 # files.
187
188 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
189 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
190
191 unless (chmod(0400, $rsa_host_key, $dsa_host_key)) {
192 die("Can't set perms on $rsa_host_key, $dsa_host_key: $!");
193 }
194 }
195
196 # Support routines
197
198 sub build_db {
199 my $cmd = shift;
200 my $db_script = shift;
201 my $db_file = shift;
202 my $check_exit_status = shift;
203 $check_exit_status = 0 unless defined $check_exit_status;
204
205 if ($ENV{TEST_VERBOSE}) {
206 print STDERR "Executing sqlite3: $cmd\n";
207 }
208
209 my @output = `$cmd`;
210 my $exit_status = $?;
211
212 if ($ENV{TEST_VERBOSE}) {
213 print STDERR "Output: ", join('', @output), "\n";
214 }
215
216 if ($check_exit_status) {
217 if ($? != 0) {
218 croak("'$cmd' failed");
219 }
220 }
221
222 # Make sure that, if we're running as root, the database file has
223 # the permissions/privs set for use by proftpd
224 if ($< == 0) {
225 unless (chmod(0666, $db_file)) {
226 croak("Can't set perms on $db_file to 0666: $!");
227 }
228 }
229
230 unlink($db_script);
231 return 1;
232 }
233
234 # Tests
235
236 sub auth_otp_sftp_hotp_login_ok_via_kbdint {
237 my $self = shift;
238 my $tmpdir = $self->{tmpdir};
239 my $setup = test_setup($tmpdir, 'auth_otp');
240
241 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
242
243 # Build up sqlite3 command to create HOTP tables
244 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
245
246 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
247 # with Google Authenticator.
248 require MIME::Base32;
249
250 my $secret = 'Sup3rS3Cr3t';
251 my $base32_secret = MIME::Base32::encode_base32($secret);
252 my $counter = 777;
253 my $bad_secret = 'B@d1YK3pts3kr3T!';
254
255 if (open(my $fh, "> $db_script")) {
256 print $fh <<EOS;
257 CREATE TABLE auth_otp (
258 user TEXT PRIMARY KEY,
259 secret TEXT,
260 counter INTEGER
261 );
262 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
263
264 EOS
265
266 unless (close($fh)) {
267 die("Can't write $db_script: $!");
268 }
269
270 } else {
271 die("Can't open $db_script: $!");
272 }
273
274 my $cmd = "sqlite3 $db_file < $db_script";
275 build_db($cmd, $db_script, $db_file);
276
277 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
278 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
279
280 my $config = {
281 PidFile => $setup->{pid_file},
282 ScoreboardFile => $setup->{scoreboard_file},
283 SystemLog => $setup->{log_file},
284 TraceLog => $setup->{log_file},
285 Trace => 'ssh2:20 sftp:20 auth_otp:20',
286
287 AuthUserFile => $setup->{auth_user_file},
288 AuthGroupFile => $setup->{auth_group_file},
289
290 IfModules => {
291 'mod_delay.c' => {
292 DelayEngine => 'off',
293 },
294
295 'mod_auth_otp.c' => {
296 AuthOTPEngine => 'on',
297 AuthOTPLog => $setup->{log_file},
298 AuthOTPAlgorithm => 'hotp',
299
300 # Assumes default table names, column names
301 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
302 },
303
304 'mod_sftp.c' => [
305 "SFTPEngine on",
306 "SFTPLog $setup->{log_file}",
307 "SFTPHostKey $rsa_host_key",
308 "SFTPHostKey $dsa_host_key",
309
310 # Configure mod_sftp to only use the keyboard-interactive method.
311 # NOTE: How to handle this when both mod_auth_otp AND mod_sftp_pam
312 # are used/loaded?
313 'SFTPAuthMethods keyboard-interactive',
314 ],
315
316 'mod_sql.c' => [
317 'SQLEngine log',
318 'SQLBackend sqlite3',
319 "SQLConnectInfo $db_file",
320 "SQLLogFile $setup->{log_file}",
321
322 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
323 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
324 ],
325 },
326 };
327
328 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
329 $config);
330
331 # Open pipes, for use between the parent and child processes. Specifically,
332 # the child will indicate when it's done with its test by writing a message
333 # to the parent.
334 my ($rfh, $wfh);
335 unless (pipe($rfh, $wfh)) {
336 die("Can't open pipe: $!");
337 }
338
339 my $ex;
340
341 require Authen::OATH;
342 require Net::SSH2;
343
344 # Fork child
345 $self->handle_sigchld();
346 defined(my $pid = fork()) or die("Can't fork: $!");
347 if ($pid) {
348 eval {
349 sleep(2);
350
351 my $ssh2 = Net::SSH2->new();
352 unless ($ssh2->connect('127.0.0.1', $port)) {
353 my ($err_code, $err_name, $err_str) = $ssh2->error();
354 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
355 }
356
357 # Calculate HOTP
358 my $oath = Authen::OATH->new();
359 my $hotp = $oath->hotp($secret, $counter);
360 if ($ENV{TEST_VERBOSE}) {
361 print STDERR "# Generated HOTP $hotp for counter $counter\n";
362 }
363
364 unless ($ssh2->auth_keyboard($setup->{user}, $hotp)) {
365 my ($err_code, $err_name, $err_str) = $ssh2->error();
366 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
367 }
368
369 $ssh2->disconnect();
370 };
371 if ($@) {
372 $ex = $@;
373 }
374
375 $wfh->print("done\n");
376 $wfh->flush();
377
378 } else {
379 eval { server_wait($setup->{config_file}, $rfh) };
380 if ($@) {
381 warn($@);
382 exit 1;
383 }
384
385 exit 0;
386 }
387
388 # Stop server
389 server_stop($setup->{pid_file});
390 $self->assert_child_ok($pid);
391
392 test_cleanup($setup->{log_file}, $ex);
393 }
394
395 sub auth_otp_sftp_hotp_login_ok_via_password {
396 my $self = shift;
397 my $tmpdir = $self->{tmpdir};
398 my $setup = test_setup($tmpdir, 'auth_otp');
399
400 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
401
402 # Build up sqlite3 command to create HOTP tables
403 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
404
405 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
406 # with Google Authenticator.
407 require MIME::Base32;
408
409 my $secret = 'Sup3rS3Cr3t';
410 my $base32_secret = MIME::Base32::encode_base32($secret);
411 my $counter = 7624;
412
413 if (open(my $fh, "> $db_script")) {
414 print $fh <<EOS;
415 CREATE TABLE auth_otp (
416 user TEXT PRIMARY KEY,
417 secret TEXT,
418 counter INTEGER
419 );
420 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
421
422 EOS
423 unless (close($fh)) {
424 die("Can't write $db_script: $!");
425 }
426
427 } else {
428 die("Can't open $db_script: $!");
429 }
430
431 my $cmd = "sqlite3 $db_file < $db_script";
432 build_db($cmd, $db_script, $db_file);
433
434 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
435 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
436
437 my $config = {
438 PidFile => $setup->{pid_file},
439 ScoreboardFile => $setup->{scoreboard_file},
440 SystemLog => $setup->{log_file},
441 TraceLog => $setup->{log_file},
442 Trace => 'auth:20 ssh2:20 sftp:20 auth_otp:20',
443
444 AuthUserFile => $setup->{auth_user_file},
445 AuthGroupFile => $setup->{auth_group_file},
446
447 IfModules => {
448 'mod_auth_pam.c' => {
449 AuthPAM => 'off',
450 },
451
452 'mod_delay.c' => {
453 DelayEngine => 'off',
454 },
455
456 'mod_auth_otp.c' => {
457 AuthOTPEngine => 'on',
458 AuthOTPLog => $setup->{log_file},
459 AuthOTPAlgorithm => 'hotp',
460
461 # Assumes default table names, column names
462 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
463 },
464
465 'mod_sftp.c' => [
466 "SFTPEngine on",
467 "SFTPLog $setup->{log_file}",
468 "SFTPHostKey $rsa_host_key",
469 "SFTPHostKey $dsa_host_key",
470
471 'SFTPAuthMethods password',
472 ],
473
474 'mod_sql.c' => [
475 'SQLEngine log',
476 'SQLBackend sqlite3',
477 "SQLConnectInfo $db_file",
478 "SQLLogFile $setup->{log_file}",
479
480 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
481 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
482 ],
483 },
484 };
485
486 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
487 $config);
488
489 # Open pipes, for use between the parent and child processes. Specifically,
490 # the child will indicate when it's done with its test by writing a message
491 # to the parent.
492 my ($rfh, $wfh);
493 unless (pipe($rfh, $wfh)) {
494 die("Can't open pipe: $!");
495 }
496
497 my $ex;
498
499 require Authen::OATH;
500 require Net::SSH2;
501
502 # Fork child
503 $self->handle_sigchld();
504 defined(my $pid = fork()) or die("Can't fork: $!");
505 if ($pid) {
506 eval {
507 sleep(2);
508
509 my $ssh2 = Net::SSH2->new();
510 unless ($ssh2->connect('127.0.0.1', $port)) {
511 my ($err_code, $err_name, $err_str) = $ssh2->error();
512 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
513 }
514
515 # Calculate HOTP
516 my $oath = Authen::OATH->new();
517 my $hotp = $oath->hotp($secret, $counter);
518 if ($ENV{TEST_VERBOSE}) {
519 print STDERR "# Generated HOTP $hotp for counter $counter\n";
520 }
521
522 unless ($ssh2->auth_password($setup->{user}, $hotp)) {
523 my ($err_code, $err_name, $err_str) = $ssh2->error();
524 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
525 }
526
527 $ssh2->disconnect();
528 };
529 if ($@) {
530 $ex = $@;
531 }
532
533 $wfh->print("done\n");
534 $wfh->flush();
535
536 } else {
537 eval { server_wait($setup->{config_file}, $rfh) };
538 if ($@) {
539 warn($@);
540 exit 1;
541 }
542
543 exit 0;
544 }
545
546 # Stop server
547 server_stop($setup->{pid_file});
548 $self->assert_child_ok($pid);
549
550 test_cleanup($setup->{log_file}, $ex);
551 }
552
553 sub auth_otp_sftp_hotp_login_failed_bad_secret {
554 my $self = shift;
555 my $tmpdir = $self->{tmpdir};
556 my $setup = test_setup($tmpdir, 'auth_otp');
557
558 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
559
560 # Build up sqlite3 command to create HOTP tables
561 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
562
563 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
564 # with Google Authenticator.
565 require MIME::Base32;
566
567 my $secret = 'Sup3rS3Cr3t';
568 my $base32_secret = MIME::Base32::encode_base32($secret);
569 my $counter = 777;
570 my $bad_secret = 'B@d1YK3pts3kr3T!';
571
572 if (open(my $fh, "> $db_script")) {
573 print $fh <<EOS;
574 CREATE TABLE auth_otp (
575 user TEXT PRIMARY KEY,
576 secret TEXT,
577 counter INTEGER
578 );
579 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
580
581 EOS
582
583 unless (close($fh)) {
584 die("Can't write $db_script: $!");
585 }
586
587 } else {
588 die("Can't open $db_script: $!");
589 }
590
591 my $cmd = "sqlite3 $db_file < $db_script";
592 build_db($cmd, $db_script, $db_file);
593
594 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
595 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
596
597 my $config = {
598 PidFile => $setup->{pid_file},
599 ScoreboardFile => $setup->{scoreboard_file},
600 SystemLog => $setup->{log_file},
601 TraceLog => $setup->{log_file},
602 Trace => 'ssh2:20 auth_otp:20',
603
604 AuthUserFile => $setup->{auth_user_file},
605 AuthGroupFile => $setup->{auth_group_file},
606
607 IfModules => {
608 'mod_delay.c' => {
609 DelayEngine => 'off',
610 },
611
612 'mod_auth_otp.c' => {
613 AuthOTPEngine => 'on',
614 AuthOTPLog => $setup->{log_file},
615 AuthOTPAlgorithm => 'hotp',
616
617 # Assumes default table names, column names
618 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
619 },
620
621 'mod_sftp.c' => [
622 "SFTPEngine on",
623 "SFTPLog $setup->{log_file}",
624 "SFTPHostKey $rsa_host_key",
625 "SFTPHostKey $dsa_host_key",
626
627 # Configure mod_sftp to only use the keyboard-interactive method.
628 # NOTE: How to handle this when both mod_auth_otp AND mod_sftp_pam
629 # are used/loaded?
630 'SFTPAuthMethods keyboard-interactive',
631 ],
632
633 'mod_sql.c' => [
634 'SQLEngine log',
635 'SQLBackend sqlite3',
636 "SQLConnectInfo $db_file",
637 "SQLLogFile $setup->{log_file}",
638
639 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
640 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
641 ],
642 },
643 };
644
645 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
646 $config);
647
648 # Open pipes, for use between the parent and child processes. Specifically,
649 # the child will indicate when it's done with its test by writing a message
650 # to the parent.
651 my ($rfh, $wfh);
652 unless (pipe($rfh, $wfh)) {
653 die("Can't open pipe: $!");
654 }
655
656 my $ex;
657
658 require Authen::OATH;
659 require Net::SSH2;
660
661 # Fork child
662 $self->handle_sigchld();
663 defined(my $pid = fork()) or die("Can't fork: $!");
664 if ($pid) {
665 eval {
666 sleep(2);
667
668 my $ssh2 = Net::SSH2->new();
669 unless ($ssh2->connect('127.0.0.1', $port)) {
670 my ($err_code, $err_name, $err_str) = $ssh2->error();
671 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
672 }
673
674 # Calculate HOTP
675 my $oath = Authen::OATH->new();
676 my $hotp = $oath->hotp($bad_secret, $counter);
677 if ($ENV{TEST_VERBOSE}) {
678 print STDERR "# Generated HOTP $hotp for counter $counter\n";
679 }
680
681 if ($ssh2->auth_keyboard($setup->{user}, $hotp)) {
682 die("Login to SSH2 server succeeded unexpectedly");
683 }
684
685 my ($err_code, $err_name, $err_str) = $ssh2->error();
686 $self->assert($err_str, qr/Authentication failed \(keyboard-interactive\)/,
687 test_msg("Expected 'Authentication failed (keyboard-interactive), got '$err_str'"));
688
689 $ssh2->disconnect();
690 };
691 if ($@) {
692 $ex = $@;
693 }
694
695 $wfh->print("done\n");
696 $wfh->flush();
697
698 } else {
699 eval { server_wait($setup->{config_file}, $rfh) };
700 if ($@) {
701 warn($@);
702 exit 1;
703 }
704
705 exit 0;
706 }
707
708 # Stop server
709 server_stop($setup->{pid_file});
710
711 $self->assert_child_ok($pid);
712
713 test_cleanup($setup->{log_file}, $ex);
714 }
715
716 sub auth_otp_sftp_hotp_login_failed_missing_otp_entry_issue1560 {
717 my $self = shift;
718 my $tmpdir = $self->{tmpdir};
719 my $setup = test_setup($tmpdir, 'auth_otp');
720
721 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
722
723 # Build up sqlite3 command to create HOTP tables
724 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
725
726 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
727 # with Google Authenticator.
728 require MIME::Base32;
729
730 my $secret = 'Sup3rS3Cr3t';
731 my $base32_secret = MIME::Base32::encode_base32($secret);
732 my $counter = 777;
733
734 if (open(my $fh, "> $db_script")) {
735 # Note that we deliberately omit the row for this user from the OTP table.
736
737 print $fh <<EOS;
738 CREATE TABLE auth_otp (
739 user TEXT PRIMARY KEY,
740 secret TEXT,
741 counter INTEGER
742 );
743 EOS
744 unless (close($fh)) {
745 die("Can't write $db_script: $!");
746 }
747
748 } else {
749 die("Can't open $db_script: $!");
750 }
751
752 my $cmd = "sqlite3 $db_file < $db_script";
753 build_db($cmd, $db_script, $db_file);
754
755 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
756 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
757
758 my $config = {
759 PidFile => $setup->{pid_file},
760 ScoreboardFile => $setup->{scoreboard_file},
761 SystemLog => $setup->{log_file},
762 TraceLog => $setup->{log_file},
763 Trace => 'ssh2:20 auth_otp:20',
764
765 AuthUserFile => $setup->{auth_user_file},
766 AuthGroupFile => $setup->{auth_group_file},
767
768 IfModules => {
769 'mod_delay.c' => {
770 DelayEngine => 'off',
771 },
772
773 'mod_auth_otp.c' => {
774 AuthOTPEngine => 'on',
775 AuthOTPLog => $setup->{log_file},
776 AuthOTPAlgorithm => 'hotp',
777
778 # Assumes default table names, column names
779 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
780 },
781
782 'mod_sftp.c' => [
783 "SFTPEngine on",
784 "SFTPLog $setup->{log_file}",
785 "SFTPHostKey $rsa_host_key",
786 "SFTPHostKey $dsa_host_key",
787
788 # Configure mod_sftp to only use the keyboard-interactive method.
789 # NOTE: How to handle this when both mod_auth_otp AND mod_sftp_pam
790 # are used/loaded?
791 'SFTPAuthMethods keyboard-interactive',
792 ],
793
794 'mod_sql.c' => [
795 'SQLEngine log',
796 'SQLBackend sqlite3',
797 "SQLConnectInfo $db_file",
798 "SQLLogFile $setup->{log_file}",
799
800 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
801 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
802 ],
803 },
804 };
805
806 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
807 $config);
808
809 # Open pipes, for use between the parent and child processes. Specifically,
810 # the child will indicate when it's done with its test by writing a message
811 # to the parent.
812 my ($rfh, $wfh);
813 unless (pipe($rfh, $wfh)) {
814 die("Can't open pipe: $!");
815 }
816
817 my $ex;
818
819 require Authen::OATH;
820 require Net::SSH2;
821
822 # Fork child
823 $self->handle_sigchld();
824 defined(my $pid = fork()) or die("Can't fork: $!");
825 if ($pid) {
826 eval {
827 sleep(2);
828
829 my $ssh2 = Net::SSH2->new();
830 unless ($ssh2->connect('127.0.0.1', $port)) {
831 my ($err_code, $err_name, $err_str) = $ssh2->error();
832 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
833 }
834
835 # Calculate HOTP
836 my $oath = Authen::OATH->new();
837 my $hotp = $oath->hotp($secret, $counter);
838 if ($ENV{TEST_VERBOSE}) {
839 print STDERR "# Generated HOTP $hotp for counter $counter\n";
840 }
841
842 if ($ssh2->auth_keyboard($setup->{user}, $hotp)) {
843 die("Login to SSH2 server succeeded unexpectedly");
844 }
845
846 my ($err_code, $err_name, $err_str) = $ssh2->error();
847 $self->assert($err_str, qr/Authentication failed \(keyboard-interactive\)/,
848 test_msg("Expected 'Authentication failed (keyboard-interactive), got '$err_str'"));
849
850 $ssh2->disconnect();
851 };
852 if ($@) {
853 $ex = $@;
854 }
855
856 $wfh->print("done\n");
857 $wfh->flush();
858
859 } else {
860 eval { server_wait($setup->{config_file}, $rfh) };
861 if ($@) {
862 warn($@);
863 exit 1;
864 }
865
866 exit 0;
867 }
868
869 # Stop server
870 server_stop($setup->{pid_file});
871 $self->assert_child_ok($pid);
872
873 test_cleanup($setup->{log_file}, $ex);
874 }
875
876 sub auth_otp_sftp_hotp_login_ok_missing_otp_entry_chained_auth_issue1560 {
877 my $self = shift;
878 my $tmpdir = $self->{tmpdir};
879 my $setup = test_setup($tmpdir, 'auth_otp');
880
881 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
882
883 # Build up sqlite3 command to create HOTP tables
884 my $db_script = File::Spec->rel2abs("$tmpdir/hotp.sql");
885
886 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
887 # with Google Authenticator.
888 require MIME::Base32;
889
890 my $secret = 'Sup3rS3Cr3t';
891 my $base32_secret = MIME::Base32::encode_base32($secret);
892 my $counter = 777;
893
894 if (open(my $fh, "> $db_script")) {
895 # Note that we deliberately omit the row for this user from the OTP table.
896
897 print $fh <<EOS;
898 CREATE TABLE auth_otp (
899 user TEXT PRIMARY KEY,
900 secret TEXT,
901 counter INTEGER
902 );
903 EOS
904 unless (close($fh)) {
905 die("Can't write $db_script: $!");
906 }
907
908 } else {
909 die("Can't open $db_script: $!");
910 }
911
912 my $cmd = "sqlite3 $db_file < $db_script";
913 build_db($cmd, $db_script, $db_file);
914
915 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
916 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
917
918 my $config = {
919 PidFile => $setup->{pid_file},
920 ScoreboardFile => $setup->{scoreboard_file},
921 SystemLog => $setup->{log_file},
922 TraceLog => $setup->{log_file},
923 Trace => 'ssh2:20 auth_otp:20',
924
925 AuthUserFile => $setup->{auth_user_file},
926 AuthGroupFile => $setup->{auth_group_file},
927
928 IfModules => {
929 'mod_delay.c' => {
930 DelayEngine => 'off',
931 },
932
933 'mod_auth_otp.c' => {
934 AuthOTPEngine => 'on',
935 AuthOTPLog => $setup->{log_file},
936 AuthOTPAlgorithm => 'hotp',
937
938 # Assumes default table names, column names
939 AuthOTPTable => 'sql:/get-user-hotp/update-user-hotp',
940
941 # Table entries are now required by default, per Issue #1562
942 AuthOTPOptions => 'OptionalTableEntry',
943 },
944
945 'mod_sftp.c' => [
946 "SFTPEngine on",
947 "SFTPLog $setup->{log_file}",
948 "SFTPHostKey $rsa_host_key",
949 "SFTPHostKey $dsa_host_key",
950
951 'SFTPAuthMethods password+keyboard-interactive',
952 ],
953
954 'mod_sql.c' => [
955 'SQLEngine log',
956 'SQLBackend sqlite3',
957 "SQLConnectInfo $db_file",
958 "SQLLogFile $setup->{log_file}",
959
960 'SQLNamedQuery get-user-hotp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
961 'SQLNamedQuery update-user-hotp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
962 ],
963 },
964 };
965
966 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
967 $config);
968
969 # Open pipes, for use between the parent and child processes. Specifically,
970 # the child will indicate when it's done with its test by writing a message
971 # to the parent.
972 my ($rfh, $wfh);
973 unless (pipe($rfh, $wfh)) {
974 die("Can't open pipe: $!");
975 }
976
977 my $ex;
978
979 require Authen::OATH;
980 require Net::SSH2;
981
982 # Fork child
983 $self->handle_sigchld();
984 defined(my $pid = fork()) or die("Can't fork: $!");
985 if ($pid) {
986 eval {
987 sleep(2);
988
989 my $ssh2 = Net::SSH2->new();
990 unless ($ssh2->connect('127.0.0.1', $port)) {
991 my ($err_code, $err_name, $err_str) = $ssh2->error();
992 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
993 }
994
995 if ($ssh2->auth_password($setup->{user}, $setup->{passwd})) {
996 die("Password authentication succeeded unexpectedly");
997 }
998
999 # Calculate HOTP
1000 my $oath = Authen::OATH->new();
1001 my $hotp = $oath->hotp($secret, $counter);
1002 if ($ENV{TEST_VERBOSE}) {
1003 print STDERR "# Generated HOTP $hotp for counter $counter\n";
1004 }
1005
1006 unless ($ssh2->auth_keyboard($setup->{user}, $hotp)) {
1007 my ($err_code, $err_name, $err_str) = $ssh2->error();
1008 die("Keyboard-interactive authentication failed: [$err_name] ($err_code) $err_str");
1009 }
1010
1011 my $sftp = $ssh2->sftp();
1012 unless ($sftp) {
1013 my ($err_code, $err_name, $err_str) = $ssh2->error();
1014 die("Can't use SFTP on SSH2 server: [$err_name] ($err_code) $err_str");
1015 }
1016
1017 # To close the SFTP channel, we have to explicitly destroy the object
1018 $sftp = undef;
1019
1020 $ssh2->disconnect();
1021 };
1022 if ($@) {
1023 $ex = $@;
1024 }
1025
1026 $wfh->print("done\n");
1027 $wfh->flush();
1028
1029 } else {
1030 eval { server_wait($setup->{config_file}, $rfh) };
1031 if ($@) {
1032 warn($@);
1033 exit 1;
1034 }
1035
1036 exit 0;
1037 }
1038
1039 # Stop server
1040 server_stop($setup->{pid_file});
1041 $self->assert_child_ok($pid);
1042
1043 test_cleanup($setup->{log_file}, $ex);
1044 }
1045
1046 sub auth_otp_sftp_totp_login_ok_via_kbdint {
1047 my $self = shift;
1048 my $tmpdir = $self->{tmpdir};
1049 my $setup = test_setup($tmpdir, 'auth_otp');
1050
1051 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1052
1053 # Build up sqlite3 command to create TOTP tables
1054 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
1055
1056 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1057 # with Google Authenticator.
1058 require MIME::Base32;
1059
1060 my $secret = 'Sup3rS3Cr3t';
1061 my $base32_secret = MIME::Base32::encode_base32($secret);
1062 my $counter = 777;
1063
1064 if (open(my $fh, "> $db_script")) {
1065 print $fh <<EOS;
1066 CREATE TABLE auth_otp (
1067 user TEXT PRIMARY KEY,
1068 secret TEXT,
1069 counter INTEGER
1070 );
1071 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
1072
1073 EOS
1074 unless (close($fh)) {
1075 die("Can't write $db_script: $!");
1076 }
1077
1078 } else {
1079 die("Can't open $db_script: $!");
1080 }
1081
1082 my $cmd = "sqlite3 $db_file < $db_script";
1083 build_db($cmd, $db_script, $db_file);
1084
1085 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
1086 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
1087
1088 my $config = {
1089 PidFile => $setup->{pid_file},
1090 ScoreboardFile => $setup->{scoreboard_file},
1091 SystemLog => $setup->{log_file},
1092 TraceLog => $setup->{log_file},
1093 Trace => 'ssh2:20 auth_otp:20',
1094
1095 AuthUserFile => $setup->{auth_user_file},
1096 AuthGroupFile => $setup->{auth_group_file},
1097
1098 IfModules => {
1099 'mod_delay.c' => {
1100 DelayEngine => 'off',
1101 },
1102
1103 'mod_auth_otp.c' => {
1104 AuthOTPEngine => 'on',
1105 AuthOTPLog => $setup->{log_file},
1106 AuthOTPAlgorithm => 'totp',
1107
1108 # Assumes default table names, column names
1109 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
1110 },
1111
1112 'mod_sftp.c' => [
1113 "SFTPEngine on",
1114 "SFTPLog $setup->{log_file}",
1115 "SFTPHostKey $rsa_host_key",
1116 "SFTPHostKey $dsa_host_key",
1117
1118 # Configure mod_sftp to only use the keyboard-interactive method.
1119 # NOTE: How to handle this when both mod_auth_otp AND mod_sftp_pam
1120 # are used/loaded?
1121 'SFTPAuthMethods keyboard-interactive',
1122 ],
1123
1124 'mod_sql.c' => [
1125 'SQLEngine log',
1126 'SQLBackend sqlite3',
1127 "SQLConnectInfo $db_file",
1128 "SQLLogFile $setup->{log_file}",
1129
1130 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
1131 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1132 ],
1133 },
1134 };
1135
1136 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1137 $config);
1138
1139 # Open pipes, for use between the parent and child processes. Specifically,
1140 # the child will indicate when it's done with its test by writing a message
1141 # to the parent.
1142 my ($rfh, $wfh);
1143 unless (pipe($rfh, $wfh)) {
1144 die("Can't open pipe: $!");
1145 }
1146
1147 my $ex;
1148
1149 require Authen::OATH;
1150 require Net::SSH2;
1151
1152 # Fork child
1153 $self->handle_sigchld();
1154 defined(my $pid = fork()) or die("Can't fork: $!");
1155 if ($pid) {
1156 eval {
1157 sleep(2);
1158
1159 my $ssh2 = Net::SSH2->new();
1160 unless ($ssh2->connect('127.0.0.1', $port)) {
1161 my ($err_code, $err_name, $err_str) = $ssh2->error();
1162 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1163 }
1164
1165 # Calculate TOTP
1166 my $oath = Authen::OATH->new();
1167 my $totp = $oath->totp($secret);
1168 if ($ENV{TEST_VERBOSE}) {
1169 print STDERR "# Generated TOTP $totp for current time\n";
1170 }
1171
1172 unless ($ssh2->auth_keyboard($setup->{user}, $totp)) {
1173 my ($err_code, $err_name, $err_str) = $ssh2->error();
1174 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1175 }
1176
1177 $ssh2->disconnect();
1178 };
1179 if ($@) {
1180 $ex = $@;
1181 }
1182
1183 $wfh->print("done\n");
1184 $wfh->flush();
1185
1186 } else {
1187 eval { server_wait($setup->{config_file}, $rfh) };
1188 if ($@) {
1189 warn($@);
1190 exit 1;
1191 }
1192
1193 exit 0;
1194 }
1195
1196 # Stop server
1197 server_stop($setup->{pid_file});
1198 $self->assert_child_ok($pid);
1199
1200 test_cleanup($setup->{log_file}, $ex);
1201 }
1202
1203 sub auth_otp_sftp_totp_login_ok_via_password {
1204 my $self = shift;
1205 my $tmpdir = $self->{tmpdir};
1206 my $setup = test_setup($tmpdir, 'auth_otp');
1207
1208 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1209
1210 # Build up sqlite3 command to create TOTP tables
1211 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
1212
1213 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1214 # with Google Authenticator.
1215 require MIME::Base32;
1216
1217 my $secret = 'Sup3rS3Cr3t';
1218 my $base32_secret = MIME::Base32::encode_base32($secret);
1219 my $counter = 2476;
1220
1221 if (open(my $fh, "> $db_script")) {
1222 print $fh <<EOS;
1223 CREATE TABLE auth_otp (
1224 user TEXT PRIMARY KEY,
1225 secret TEXT,
1226 counter INTEGER
1227 );
1228 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
1229
1230 EOS
1231 unless (close($fh)) {
1232 die("Can't write $db_script: $!");
1233 }
1234
1235 } else {
1236 die("Can't open $db_script: $!");
1237 }
1238
1239 my $cmd = "sqlite3 $db_file < $db_script";
1240 build_db($cmd, $db_script, $db_file);
1241
1242 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
1243 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
1244
1245 my $config = {
1246 PidFile => $setup->{pid_file},
1247 ScoreboardFile => $setup->{scoreboard_file},
1248 SystemLog => $setup->{log_file},
1249 TraceLog => $setup->{log_file},
1250 Trace => 'auth:20 ssh2:20 auth_otp:20',
1251
1252 AuthUserFile => $setup->{auth_user_file},
1253 AuthGroupFile => $setup->{auth_group_file},
1254
1255 IfModules => {
1256 'mod_auth_pam.c' => {
1257 AuthPAM => 'off',
1258 },
1259
1260 'mod_delay.c' => {
1261 DelayEngine => 'off',
1262 },
1263
1264 'mod_auth_otp.c' => {
1265 AuthOTPEngine => 'on',
1266 AuthOTPLog => $setup->{log_file},
1267 AuthOTPAlgorithm => 'totp',
1268
1269 # Assumes default table names, column names
1270 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
1271 },
1272
1273 'mod_sftp.c' => [
1274 "SFTPEngine on",
1275 "SFTPLog $setup->{log_file}",
1276 "SFTPHostKey $rsa_host_key",
1277 "SFTPHostKey $dsa_host_key",
1278
1279 'SFTPAuthMethods password',
1280 ],
1281
1282 'mod_sql.c' => [
1283 'SQLEngine log',
1284 'SQLBackend sqlite3',
1285 "SQLConnectInfo $db_file",
1286 "SQLLogFile $setup->{log_file}",
1287
1288 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
1289 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1290 ],
1291 },
1292 };
1293
1294 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1295 $config);
1296
1297 # Open pipes, for use between the parent and child processes. Specifically,
1298 # the child will indicate when it's done with its test by writing a message
1299 # to the parent.
1300 my ($rfh, $wfh);
1301 unless (pipe($rfh, $wfh)) {
1302 die("Can't open pipe: $!");
1303 }
1304
1305 my $ex;
1306
1307 require Authen::OATH;
1308 require Net::SSH2;
1309
1310 # Fork child
1311 $self->handle_sigchld();
1312 defined(my $pid = fork()) or die("Can't fork: $!");
1313 if ($pid) {
1314 eval {
1315 sleep(2);
1316
1317 my $ssh2 = Net::SSH2->new();
1318 unless ($ssh2->connect('127.0.0.1', $port)) {
1319 my ($err_code, $err_name, $err_str) = $ssh2->error();
1320 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1321 }
1322
1323 # Calculate TOTP
1324 my $oath = Authen::OATH->new();
1325 my $totp = $oath->totp($secret);
1326 if ($ENV{TEST_VERBOSE}) {
1327 print STDERR "# Generated TOTP $totp for current time\n";
1328 }
1329
1330 unless ($ssh2->auth_password($setup->{user}, $totp)) {
1331 my ($err_code, $err_name, $err_str) = $ssh2->error();
1332 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1333 }
1334
1335 $ssh2->disconnect();
1336 };
1337 if ($@) {
1338 $ex = $@;
1339 }
1340
1341 $wfh->print("done\n");
1342 $wfh->flush();
1343
1344 } else {
1345 eval { server_wait($setup->{config_file}, $rfh) };
1346 if ($@) {
1347 warn($@);
1348 exit 1;
1349 }
1350
1351 exit 0;
1352 }
1353
1354 # Stop server
1355 server_stop($setup->{pid_file});
1356 $self->assert_child_ok($pid);
1357
1358 test_cleanup($setup->{log_file}, $ex);
1359 }
1360
1361 sub auth_otp_sftp_totp_login_failed_bad_secret {
1362 my $self = shift;
1363 my $tmpdir = $self->{tmpdir};
1364 my $setup = test_setup($tmpdir, 'auth_otp');
1365
1366 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1367
1368 # Build up sqlite3 command to create TOTP tables
1369 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
1370
1371 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1372 # with Google Authenticator.
1373 require MIME::Base32;
1374
1375 my $secret = 'Sup3rS3Cr3t';
1376 my $base32_secret = MIME::Base32::encode_base32($secret);
1377 my $counter = 777;
1378 my $bad_secret = 'B@d1YK3pts3kr3T!';
1379
1380 if (open(my $fh, "> $db_script")) {
1381 print $fh <<EOS;
1382 CREATE TABLE auth_otp (
1383 user TEXT PRIMARY KEY,
1384 secret TEXT,
1385 counter INTEGER
1386 );
1387 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
1388
1389 EOS
1390
1391 unless (close($fh)) {
1392 die("Can't write $db_script: $!");
1393 }
1394
1395 } else {
1396 die("Can't open $db_script: $!");
1397 }
1398
1399 my $cmd = "sqlite3 $db_file < $db_script";
1400 build_db($cmd, $db_script, $db_file);
1401
1402 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
1403 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
1404
1405 my $config = {
1406 PidFile => $setup->{pid_file},
1407 ScoreboardFile => $setup->{scoreboard_file},
1408 SystemLog => $setup->{log_file},
1409 TraceLog => $setup->{log_file},
1410 Trace => 'ssh2:20 auth_otp:20',
1411
1412 AuthUserFile => $setup->{auth_user_file},
1413 AuthGroupFile => $setup->{auth_group_file},
1414
1415 IfModules => {
1416 'mod_delay.c' => {
1417 DelayEngine => 'off',
1418 },
1419
1420 'mod_auth_otp.c' => {
1421 AuthOTPEngine => 'on',
1422 AuthOTPLog => $setup->{log_file},
1423 AuthOTPAlgorithm => 'totp',
1424
1425 # Assumes default table names, column names
1426 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
1427 },
1428
1429 'mod_sftp.c' => [
1430 "SFTPEngine on",
1431 "SFTPLog $setup->{log_file}",
1432 "SFTPHostKey $rsa_host_key",
1433 "SFTPHostKey $dsa_host_key",
1434
1435 # Configure mod_sftp to only use the keyboard-interactive method.
1436 # NOTE: How to handle this when both mod_auth_otp AND mod_sftp_pam
1437 # are used/loaded?
1438 'SFTPAuthMethods keyboard-interactive',
1439 ],
1440
1441 'mod_sql.c' => [
1442 'SQLEngine log',
1443 'SQLBackend sqlite3',
1444 "SQLConnectInfo $db_file",
1445 "SQLLogFile $setup->{log_file}",
1446
1447 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
1448 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1449 ],
1450 },
1451 };
1452
1453 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1454 $config);
1455
1456 # Open pipes, for use between the parent and child processes. Specifically,
1457 # the child will indicate when it's done with its test by writing a message
1458 # to the parent.
1459 my ($rfh, $wfh);
1460 unless (pipe($rfh, $wfh)) {
1461 die("Can't open pipe: $!");
1462 }
1463
1464 my $ex;
1465
1466 require Authen::OATH;
1467 require Net::SSH2;
1468
1469 # Fork child
1470 $self->handle_sigchld();
1471 defined(my $pid = fork()) or die("Can't fork: $!");
1472 if ($pid) {
1473 eval {
1474 sleep(2);
1475
1476 my $ssh2 = Net::SSH2->new();
1477 unless ($ssh2->connect('127.0.0.1', $port)) {
1478 my ($err_code, $err_name, $err_str) = $ssh2->error();
1479 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1480 }
1481
1482 # Calculate TOTP
1483 my $oath = Authen::OATH->new();
1484 my $totp = $oath->totp($bad_secret);
1485 if ($ENV{TEST_VERBOSE}) {
1486 print STDERR "# Generated TOTP $totp for current time\n";
1487 }
1488
1489 if ($ssh2->auth_keyboard($setup->{user}, $totp)) {
1490 die("Login to SSH2 server succeeded unexpectedly");
1491 }
1492
1493 my ($err_code, $err_name, $err_str) = $ssh2->error();
1494 $self->assert($err_str, qr/Authentication failed \(keyboard-interactive\)/,
1495 test_msg("Expected 'Authentication failed (keyboard-interactive), got '$err_str'"));
1496
1497 $ssh2->disconnect();
1498 };
1499 if ($@) {
1500 $ex = $@;
1501 }
1502
1503 $wfh->print("done\n");
1504 $wfh->flush();
1505
1506 } else {
1507 eval { server_wait($setup->{config_file}, $rfh) };
1508 if ($@) {
1509 warn($@);
1510 exit 1;
1511 }
1512
1513 exit 0;
1514 }
1515
1516 # Stop server
1517 server_stop($setup->{pid_file});
1518
1519 $self->assert_child_ok($pid);
1520
1521 test_cleanup($setup->{log_file}, $ex);
1522 }
1523
1524 sub auth_otp_sftp_totp_login_failed_missing_otp_entry_issue1560 {
1525 my $self = shift;
1526 my $tmpdir = $self->{tmpdir};
1527 my $setup = test_setup($tmpdir, 'auth_otp');
1528
1529 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1530
1531 # Build up sqlite3 command to create TOTP tables
1532 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
1533
1534 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1535 # with Google Authenticator.
1536 require MIME::Base32;
1537
1538 my $secret = 'Sup3rS3Cr3t';
1539 my $base32_secret = MIME::Base32::encode_base32($secret);
1540 my $counter = 777;
1541
1542 if (open(my $fh, "> $db_script")) {
1543 # Note that we deliberately omit the row for this user from the OTP table.
1544
1545 print $fh <<EOS;
1546 CREATE TABLE auth_otp (
1547 user TEXT PRIMARY KEY,
1548 secret TEXT,
1549 counter INTEGER
1550 );
1551 EOS
1552 unless (close($fh)) {
1553 die("Can't write $db_script: $!");
1554 }
1555
1556 } else {
1557 die("Can't open $db_script: $!");
1558 }
1559
1560 my $cmd = "sqlite3 $db_file < $db_script";
1561 build_db($cmd, $db_script, $db_file);
1562
1563 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
1564 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
1565
1566 my $config = {
1567 PidFile => $setup->{pid_file},
1568 ScoreboardFile => $setup->{scoreboard_file},
1569 SystemLog => $setup->{log_file},
1570 TraceLog => $setup->{log_file},
1571 Trace => 'ssh2:20 auth_otp:20',
1572
1573 AuthUserFile => $setup->{auth_user_file},
1574 AuthGroupFile => $setup->{auth_group_file},
1575
1576 IfModules => {
1577 'mod_delay.c' => {
1578 DelayEngine => 'off',
1579 },
1580
1581 'mod_auth_otp.c' => {
1582 AuthOTPEngine => 'on',
1583 AuthOTPLog => $setup->{log_file},
1584 AuthOTPAlgorithm => 'totp',
1585
1586 # Assumes default table names, column names
1587 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
1588 },
1589
1590 'mod_sftp.c' => [
1591 "SFTPEngine on",
1592 "SFTPLog $setup->{log_file}",
1593 "SFTPHostKey $rsa_host_key",
1594 "SFTPHostKey $dsa_host_key",
1595
1596 # Configure mod_sftp to only use the keyboard-interactive method.
1597 # NOTE: How to handle this when both mod_auth_otp AND mod_sftp_pam
1598 # are used/loaded?
1599 'SFTPAuthMethods keyboard-interactive',
1600 ],
1601
1602 'mod_sql.c' => [
1603 'SQLEngine log',
1604 'SQLBackend sqlite3',
1605 "SQLConnectInfo $db_file",
1606 "SQLLogFile $setup->{log_file}",
1607
1608 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
1609 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1610 ],
1611 },
1612 };
1613
1614 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1615 $config);
1616
1617 # Open pipes, for use between the parent and child processes. Specifically,
1618 # the child will indicate when it's done with its test by writing a message
1619 # to the parent.
1620 my ($rfh, $wfh);
1621 unless (pipe($rfh, $wfh)) {
1622 die("Can't open pipe: $!");
1623 }
1624
1625 my $ex;
1626
1627 require Authen::OATH;
1628 require Net::SSH2;
1629
1630 # Fork child
1631 $self->handle_sigchld();
1632 defined(my $pid = fork()) or die("Can't fork: $!");
1633 if ($pid) {
1634 eval {
1635 sleep(2);
1636
1637 my $ssh2 = Net::SSH2->new();
1638 unless ($ssh2->connect('127.0.0.1', $port)) {
1639 my ($err_code, $err_name, $err_str) = $ssh2->error();
1640 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1641 }
1642
1643 # Calculate TOTP
1644 my $oath = Authen::OATH->new();
1645 my $totp = $oath->totp($secret);
1646 if ($ENV{TEST_VERBOSE}) {
1647 print STDERR "# Generated TOTP $totp for current time\n";
1648 }
1649
1650 if ($ssh2->auth_keyboard($setup->{user}, $totp)) {
1651 die("Login to SSH2 server succeeded unexpectedly");
1652 }
1653
1654 my ($err_code, $err_name, $err_str) = $ssh2->error();
1655 $self->assert($err_str, qr/Authentication failed \(keyboard-interactive\)/,
1656 test_msg("Expected 'Authentication failed (keyboard-interactive), got '$err_str'"));
1657
1658 $ssh2->disconnect();
1659 };
1660 if ($@) {
1661 $ex = $@;
1662 }
1663
1664 $wfh->print("done\n");
1665 $wfh->flush();
1666
1667 } else {
1668 eval { server_wait($setup->{config_file}, $rfh) };
1669 if ($@) {
1670 warn($@);
1671 exit 1;
1672 }
1673
1674 exit 0;
1675 }
1676
1677 # Stop server
1678 server_stop($setup->{pid_file});
1679 $self->assert_child_ok($pid);
1680
1681 test_cleanup($setup->{log_file}, $ex);
1682 }
1683
1684 sub auth_otp_sftp_totp_login_ok_missing_otp_entry_chained_auth_issue1560 {
1685 my $self = shift;
1686 my $tmpdir = $self->{tmpdir};
1687 my $setup = test_setup($tmpdir, 'auth_otp');
1688
1689 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1690
1691 # Build up sqlite3 command to create TOTP tables
1692 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
1693
1694 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1695 # with Google Authenticator.
1696 require MIME::Base32;
1697
1698 my $secret = 'Sup3rS3Cr3t';
1699 my $base32_secret = MIME::Base32::encode_base32($secret);
1700 my $counter = 777;
1701
1702 if (open(my $fh, "> $db_script")) {
1703 # Note that we deliberately omit the row for this user from the OTP table.
1704
1705 print $fh <<EOS;
1706 CREATE TABLE auth_otp (
1707 user TEXT PRIMARY KEY,
1708 secret TEXT,
1709 counter INTEGER
1710 );
1711 EOS
1712 unless (close($fh)) {
1713 die("Can't write $db_script: $!");
1714 }
1715
1716 } else {
1717 die("Can't open $db_script: $!");
1718 }
1719
1720 my $cmd = "sqlite3 $db_file < $db_script";
1721 build_db($cmd, $db_script, $db_file);
1722
1723 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
1724 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
1725
1726 my $config = {
1727 PidFile => $setup->{pid_file},
1728 ScoreboardFile => $setup->{scoreboard_file},
1729 SystemLog => $setup->{log_file},
1730 TraceLog => $setup->{log_file},
1731 Trace => 'ssh2:20 auth_otp:20',
1732
1733 AuthUserFile => $setup->{auth_user_file},
1734 AuthGroupFile => $setup->{auth_group_file},
1735
1736 IfModules => {
1737 'mod_delay.c' => {
1738 DelayEngine => 'off',
1739 },
1740
1741 'mod_auth_otp.c' => {
1742 AuthOTPEngine => 'on',
1743 AuthOTPLog => $setup->{log_file},
1744 AuthOTPAlgorithm => 'totp',
1745
1746 # Assumes default table names, column names
1747 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
1748
1749 # Table entries are now required by default, per Issue #1562
1750 AuthOTPOptions => 'OptionalTableEntry',
1751 },
1752
1753 'mod_sftp.c' => [
1754 "SFTPEngine on",
1755 "SFTPLog $setup->{log_file}",
1756 "SFTPHostKey $rsa_host_key",
1757 "SFTPHostKey $dsa_host_key",
1758
1759 'SFTPAuthMethods password+keyboard-interactive',
1760 ],
1761
1762 'mod_sql.c' => [
1763 'SQLEngine log',
1764 'SQLBackend sqlite3',
1765 "SQLConnectInfo $db_file",
1766 "SQLLogFile $setup->{log_file}",
1767
1768 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
1769 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1770 ],
1771 },
1772 };
1773
1774 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1775 $config);
1776
1777 # Open pipes, for use between the parent and child processes. Specifically,
1778 # the child will indicate when it's done with its test by writing a message
1779 # to the parent.
1780 my ($rfh, $wfh);
1781 unless (pipe($rfh, $wfh)) {
1782 die("Can't open pipe: $!");
1783 }
1784
1785 my $ex;
1786
1787 require Authen::OATH;
1788 require Net::SSH2;
1789
1790 # Fork child
1791 $self->handle_sigchld();
1792 defined(my $pid = fork()) or die("Can't fork: $!");
1793 if ($pid) {
1794 eval {
1795 sleep(2);
1796
1797 my $ssh2 = Net::SSH2->new();
1798 unless ($ssh2->connect('127.0.0.1', $port)) {
1799 my ($err_code, $err_name, $err_str) = $ssh2->error();
1800 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1801 }
1802
1803 if ($ssh2->auth_password($setup->{user}, $setup->{passwd})) {
1804 die("Password authentication succeeded unexpectedly");
1805 }
1806
1807 # Calculate TOTP
1808 my $oath = Authen::OATH->new();
1809 my $totp = $oath->totp($secret);
1810 if ($ENV{TEST_VERBOSE}) {
1811 print STDERR "# Generated TOTP $totp for current time\n";
1812 }
1813
1814 unless ($ssh2->auth_keyboard($setup->{user}, $totp)) {
1815 my ($err_code, $err_name, $err_str) = $ssh2->error();
1816 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1817 }
1818
1819 my $sftp = $ssh2->sftp();
1820
1821 # To close the SFTP channel, we have to explicitly destroy the object
1822 $sftp = undef;
1823
1824 $ssh2->disconnect();
1825 };
1826 if ($@) {
1827 $ex = $@;
1828 }
1829
1830 $wfh->print("done\n");
1831 $wfh->flush();
1832
1833 } else {
1834 eval { server_wait($setup->{config_file}, $rfh) };
1835 if ($@) {
1836 warn($@);
1837 exit 1;
1838 }
1839
1840 exit 0;
1841 }
1842
1843 # Stop server
1844 server_stop($setup->{pid_file});
1845 $self->assert_child_ok($pid);
1846
1847 test_cleanup($setup->{log_file}, $ex);
1848 }
1849
1850 sub auth_otp_sftp_totp_login_authoritative {
1851 my $self = shift;
1852 my $tmpdir = $self->{tmpdir};
1853 my $setup = test_setup($tmpdir, 'auth_otp');
1854
1855 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
1856
1857 # Build up sqlite3 command to create TOTP tables
1858 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
1859
1860 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
1861 # with Google Authenticator.
1862 require MIME::Base32;
1863
1864 my $secret = 'Sup3rS3Cr3t';
1865 my $base32_secret = MIME::Base32::encode_base32($secret);
1866 my $counter = 777;
1867
1868 if (open(my $fh, "> $db_script")) {
1869 print $fh <<EOS;
1870 CREATE TABLE auth_otp (
1871 user TEXT PRIMARY KEY,
1872 secret TEXT,
1873 counter INTEGER
1874 );
1875 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
1876
1877 EOS
1878
1879 unless (close($fh)) {
1880 die("Can't write $db_script: $!");
1881 }
1882
1883 } else {
1884 die("Can't open $db_script: $!");
1885 }
1886
1887 my $cmd = "sqlite3 $db_file < $db_script";
1888 build_db($cmd, $db_script, $db_file);
1889
1890 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
1891 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
1892
1893 my $config = {
1894 PidFile => $setup->{pid_file},
1895 ScoreboardFile => $setup->{scoreboard_file},
1896 SystemLog => $setup->{log_file},
1897 TraceLog => $setup->{log_file},
1898 Trace => 'ssh2:20 auth_otp:20',
1899
1900 AuthUserFile => $setup->{auth_user_file},
1901 AuthGroupFile => $setup->{auth_group_file},
1902 AuthOrder => 'mod_auth_file.c mod_auth_otp.c*',
1903
1904 IfModules => {
1905 'mod_delay.c' => {
1906 DelayEngine => 'off',
1907 },
1908
1909 'mod_auth_otp.c' => {
1910 AuthOTPEngine => 'on',
1911 AuthOTPLog => $setup->{log_file},
1912 AuthOTPAlgorithm => 'totp',
1913
1914 # Assumes default table names, column names
1915 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
1916 },
1917
1918 'mod_sftp.c' => [
1919 "SFTPEngine on",
1920 "SFTPLog $setup->{log_file}",
1921 "SFTPHostKey $rsa_host_key",
1922 "SFTPHostKey $dsa_host_key",
1923
1924 # Configure mod_sftp to only use the keyboard-interactive method.
1925 # NOTE: How to handle this when both mod_auth_otp AND mod_sftp_pam
1926 # are used/loaded?
1927 'SFTPAuthMethods keyboard-interactive',
1928 ],
1929
1930 'mod_sql.c' => [
1931 'SQLEngine log',
1932 'SQLBackend sqlite3',
1933 "SQLConnectInfo $db_file",
1934 "SQLLogFile $setup->{log_file}",
1935
1936 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
1937 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
1938 ],
1939 },
1940 };
1941
1942 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
1943 $config);
1944
1945 # Open pipes, for use between the parent and child processes. Specifically,
1946 # the child will indicate when it's done with its test by writing a message
1947 # to the parent.
1948 my ($rfh, $wfh);
1949 unless (pipe($rfh, $wfh)) {
1950 die("Can't open pipe: $!");
1951 }
1952
1953 my $ex;
1954
1955 require Authen::OATH;
1956 require Net::SSH2;
1957
1958 # Fork child
1959 $self->handle_sigchld();
1960 defined(my $pid = fork()) or die("Can't fork: $!");
1961 if ($pid) {
1962 eval {
1963 sleep(2);
1964
1965 my $ssh2 = Net::SSH2->new();
1966 unless ($ssh2->connect('127.0.0.1', $port)) {
1967 my ($err_code, $err_name, $err_str) = $ssh2->error();
1968 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
1969 }
1970
1971 # Calculate TOTP
1972 my $oath = Authen::OATH->new();
1973 my $totp = $oath->totp($secret);
1974 if ($ENV{TEST_VERBOSE}) {
1975 print STDERR "# Generated TOTP $totp for current time\n";
1976 }
1977
1978 unless ($ssh2->auth_keyboard($setup->{user}, $totp)) {
1979 my ($err_code, $err_name, $err_str) = $ssh2->error();
1980 die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
1981 }
1982
1983 $ssh2->disconnect();
1984 };
1985 if ($@) {
1986 $ex = $@;
1987 }
1988
1989 $wfh->print("done\n");
1990 $wfh->flush();
1991
1992 } else {
1993 eval { server_wait($setup->{config_file}, $rfh) };
1994 if ($@) {
1995 warn($@);
1996 exit 1;
1997 }
1998
1999 exit 0;
2000 }
2001
2002 # Stop server
2003 server_stop($setup->{pid_file});
2004
2005 $self->assert_child_ok($pid);
2006
2007 test_cleanup($setup->{log_file}, $ex);
2008 }
2009
2010 sub auth_otp_sftp_totp_login_authoritative_failed {
2011 my $self = shift;
2012 my $tmpdir = $self->{tmpdir};
2013 my $setup = test_setup($tmpdir, 'auth_otp');
2014
2015 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2016
2017 # Build up sqlite3 command to create TOTP tables
2018 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
2019
2020 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
2021 # with Google Authenticator.
2022 require MIME::Base32;
2023
2024 my $secret = 'Sup3rS3Cr3t';
2025 my $base32_secret = MIME::Base32::encode_base32($secret);
2026 my $counter = 777;
2027 my $bad_secret = 'B@d1YK3pts3kr3T!';
2028
2029 if (open(my $fh, "> $db_script")) {
2030 print $fh <<EOS;
2031 CREATE TABLE auth_otp (
2032 user TEXT PRIMARY KEY,
2033 secret TEXT,
2034 counter INTEGER
2035 );
2036 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
2037
2038 EOS
2039
2040 unless (close($fh)) {
2041 die("Can't write $db_script: $!");
2042 }
2043
2044 } else {
2045 die("Can't open $db_script: $!");
2046 }
2047
2048 my $cmd = "sqlite3 $db_file < $db_script";
2049 build_db($cmd, $db_script, $db_file);
2050
2051 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
2052 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
2053
2054 my $config = {
2055 PidFile => $setup->{pid_file},
2056 ScoreboardFile => $setup->{scoreboard_file},
2057 SystemLog => $setup->{log_file},
2058 TraceLog => $setup->{log_file},
2059 Trace => 'ssh2:20 auth_otp:20',
2060
2061 AuthUserFile => $setup->{auth_user_file},
2062 AuthGroupFile => $setup->{auth_group_file},
2063 AuthOrder => 'mod_auth_file.c mod_auth_otp.c*',
2064
2065 IfModules => {
2066 'mod_delay.c' => {
2067 DelayEngine => 'off',
2068 },
2069
2070 'mod_auth_otp.c' => {
2071 AuthOTPEngine => 'on',
2072 AuthOTPLog => $setup->{log_file},
2073 AuthOTPAlgorithm => 'totp',
2074
2075 # Assumes default table names, column names
2076 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
2077 },
2078
2079 'mod_sftp.c' => [
2080 "SFTPEngine on",
2081 "SFTPLog $setup->{log_file}",
2082 "SFTPHostKey $rsa_host_key",
2083 "SFTPHostKey $dsa_host_key",
2084
2085 # Configure mod_sftp to only use the keyboard-interactive method.
2086 # NOTE: How to handle this when both mod_auth_otp AND mod_sftp_pam
2087 # are used/loaded?
2088 'SFTPAuthMethods keyboard-interactive',
2089 ],
2090
2091 'mod_sql.c' => [
2092 'SQLEngine log',
2093 'SQLBackend sqlite3',
2094 "SQLConnectInfo $db_file",
2095 "SQLLogFile $setup->{log_file}",
2096
2097 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
2098 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
2099 ],
2100 },
2101 };
2102
2103 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2104 $config);
2105
2106 # Open pipes, for use between the parent and child processes. Specifically,
2107 # the child will indicate when it's done with its test by writing a message
2108 # to the parent.
2109 my ($rfh, $wfh);
2110 unless (pipe($rfh, $wfh)) {
2111 die("Can't open pipe: $!");
2112 }
2113
2114 my $ex;
2115
2116 require Authen::OATH;
2117 require Net::SSH2;
2118
2119 # Fork child
2120 $self->handle_sigchld();
2121 defined(my $pid = fork()) or die("Can't fork: $!");
2122 if ($pid) {
2123 eval {
2124 sleep(2);
2125
2126 my $ssh2 = Net::SSH2->new();
2127 unless ($ssh2->connect('127.0.0.1', $port)) {
2128 my ($err_code, $err_name, $err_str) = $ssh2->error();
2129 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2130 }
2131
2132 # Calculate TOTP
2133 my $oath = Authen::OATH->new();
2134 my $totp = $oath->totp($bad_secret);
2135 if ($ENV{TEST_VERBOSE}) {
2136 print STDERR "# Generated TOTP $totp for current time\n";
2137 }
2138
2139 if ($ssh2->auth_keyboard($setup->{user}, $totp)) {
2140 die("Login to SSH2 server succeeded unexpectedly");
2141 }
2142
2143 my ($err_code, $err_name, $err_str) = $ssh2->error();
2144 $self->assert($err_str, qr/Authentication failed \(keyboard-interactive\)/,
2145 test_msg("Expected 'Authentication failed (keyboard-interactive), got '$err_str'"));
2146
2147 $ssh2->disconnect();
2148 };
2149 if ($@) {
2150 $ex = $@;
2151 }
2152
2153 $wfh->print("done\n");
2154 $wfh->flush();
2155
2156 } else {
2157 eval { server_wait($setup->{config_file}, $rfh) };
2158 if ($@) {
2159 warn($@);
2160 exit 1;
2161 }
2162
2163 exit 0;
2164 }
2165
2166 # Stop server
2167 server_stop($setup->{pid_file});
2168
2169 $self->assert_child_ok($pid);
2170
2171 test_cleanup($setup->{log_file}, $ex);
2172 }
2173
2174 sub auth_otp_sftp_password_failed {
2175 my $self = shift;
2176 my $tmpdir = $self->{tmpdir};
2177 my $setup = test_setup($tmpdir, 'auth_otp');
2178
2179 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2180
2181 # Build up sqlite3 command to create TOTP tables
2182 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
2183
2184 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
2185 # with Google Authenticator.
2186 require MIME::Base32;
2187
2188 my $secret = 'Sup3rS3Cr3t';
2189 my $base32_secret = MIME::Base32::encode_base32($secret);
2190 my $counter = 777;
2191 my $bad_secret = 'B@d1YK3pts3kr3T!';
2192
2193 if (open(my $fh, "> $db_script")) {
2194 print $fh <<EOS;
2195 CREATE TABLE auth_otp (
2196 user TEXT PRIMARY KEY,
2197 secret TEXT,
2198 counter INTEGER
2199 );
2200 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
2201
2202 EOS
2203
2204 unless (close($fh)) {
2205 die("Can't write $db_script: $!");
2206 }
2207
2208 } else {
2209 die("Can't open $db_script: $!");
2210 }
2211
2212 my $cmd = "sqlite3 $db_file < $db_script";
2213 build_db($cmd, $db_script, $db_file);
2214
2215 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
2216 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
2217
2218 my $config = {
2219 PidFile => $setup->{pid_file},
2220 ScoreboardFile => $setup->{scoreboard_file},
2221 SystemLog => $setup->{log_file},
2222 TraceLog => $setup->{log_file},
2223 Trace => 'auth:20 ssh2:20 auth_otp:20',
2224
2225 AuthUserFile => $setup->{auth_user_file},
2226 AuthGroupFile => $setup->{auth_group_file},
2227 AuthOrder => 'mod_auth_file.c mod_auth_otp.c*',
2228
2229 IfModules => {
2230 'mod_delay.c' => {
2231 DelayEngine => 'off',
2232 },
2233
2234 'mod_auth_otp.c' => {
2235 AuthOTPEngine => 'on',
2236 AuthOTPLog => $setup->{log_file},
2237 AuthOTPAlgorithm => 'totp',
2238
2239 # Assumes default table names, column names
2240 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
2241 },
2242
2243 'mod_sftp.c' => [
2244 "SFTPEngine on",
2245 "SFTPLog $setup->{log_file}",
2246 "SFTPHostKey $rsa_host_key",
2247 "SFTPHostKey $dsa_host_key",
2248 'SFTPAuthMethods password',
2249 ],
2250
2251 'mod_sql.c' => [
2252 'SQLEngine log',
2253 'SQLBackend sqlite3',
2254 "SQLConnectInfo $db_file",
2255 "SQLLogFile $setup->{log_file}",
2256
2257 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
2258 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
2259 ],
2260 },
2261 };
2262
2263 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2264 $config);
2265
2266 # Open pipes, for use between the parent and child processes. Specifically,
2267 # the child will indicate when it's done with its test by writing a message
2268 # to the parent.
2269 my ($rfh, $wfh);
2270 unless (pipe($rfh, $wfh)) {
2271 die("Can't open pipe: $!");
2272 }
2273
2274 my $ex;
2275
2276 require Authen::OATH;
2277 require Net::SSH2;
2278
2279 # Fork child
2280 $self->handle_sigchld();
2281 defined(my $pid = fork()) or die("Can't fork: $!");
2282 if ($pid) {
2283 eval {
2284 sleep(2);
2285
2286 my $ssh2 = Net::SSH2->new();
2287 unless ($ssh2->connect('127.0.0.1', $port)) {
2288 my ($err_code, $err_name, $err_str) = $ssh2->error();
2289 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2290 }
2291
2292 # Calculate TOTP
2293 my $oath = Authen::OATH->new();
2294 my $totp = $oath->totp($bad_secret);
2295 if ($ENV{TEST_VERBOSE}) {
2296 print STDERR "# Generated TOTP $totp for current time\n";
2297 }
2298
2299 if ($ssh2->auth_password($setup->{user}, $totp)) {
2300 die("Password authentication to SSH2 server succeeded unexpectedly");
2301 }
2302
2303 my ($err_code, $err_name, $err_str) = $ssh2->error();
2304 $self->assert($err_str, qr/Authentication failed \(password\)/,
2305 test_msg("Expected 'Authentication failed (password), got '$err_str'"));
2306
2307 $ssh2->disconnect();
2308 };
2309 if ($@) {
2310 $ex = $@;
2311 }
2312
2313 $wfh->print("done\n");
2314 $wfh->flush();
2315
2316 } else {
2317 eval { server_wait($setup->{config_file}, $rfh) };
2318 if ($@) {
2319 warn($@);
2320 exit 1;
2321 }
2322
2323 exit 0;
2324 }
2325
2326 # Stop server
2327 server_stop($setup->{pid_file});
2328 $self->assert_child_ok($pid);
2329
2330 test_cleanup($setup->{log_file}, $ex);
2331 }
2332
2333 sub auth_otp_sftp_publickey_plus_totp_ok {
2334 my $self = shift;
2335 my $tmpdir = $self->{tmpdir};
2336 my $setup = test_setup($tmpdir, 'auth_otp');
2337
2338 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2339
2340 # Build up sqlite3 command to create TOTP tables
2341 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
2342
2343 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
2344 # with Google Authenticator.
2345 require MIME::Base32;
2346
2347 my $secret = 'Sup3rS3Cr3t';
2348 my $base32_secret = MIME::Base32::encode_base32($secret);
2349 my $counter = 777;
2350
2351 if (open(my $fh, "> $db_script")) {
2352 print $fh <<EOS;
2353 CREATE TABLE auth_otp (
2354 user TEXT PRIMARY KEY,
2355 secret TEXT,
2356 counter INTEGER
2357 );
2358 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
2359
2360 EOS
2361
2362 unless (close($fh)) {
2363 die("Can't write $db_script: $!");
2364 }
2365
2366 } else {
2367 die("Can't open $db_script: $!");
2368 }
2369
2370 my $cmd = "sqlite3 $db_file < $db_script";
2371 build_db($cmd, $db_script, $db_file);
2372
2373 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
2374 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
2375
2376 my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key');
2377 my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub');
2378 my $rsa_rfc4716_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/authorized_rsa_keys');
2379
2380 my $authorized_keys = File::Spec->rel2abs("$tmpdir/.authorized_keys");
2381 unless (copy($rsa_rfc4716_key, $authorized_keys)) {
2382 die("Can't copy $rsa_rfc4716_key to $authorized_keys: $!");
2383 }
2384
2385 my $config = {
2386 PidFile => $setup->{pid_file},
2387 ScoreboardFile => $setup->{scoreboard_file},
2388 SystemLog => $setup->{log_file},
2389 TraceLog => $setup->{log_file},
2390 Trace => 'ssh2:20 auth_otp:20',
2391
2392 AuthUserFile => $setup->{auth_user_file},
2393 AuthGroupFile => $setup->{auth_group_file},
2394
2395 IfModules => {
2396 'mod_delay.c' => {
2397 DelayEngine => 'off',
2398 },
2399
2400 'mod_auth_otp.c' => {
2401 AuthOTPEngine => 'on',
2402 AuthOTPLog => $setup->{log_file},
2403 AuthOTPAlgorithm => 'totp',
2404
2405 # Assumes default table names, column names
2406 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
2407 },
2408
2409 'mod_sftp.c' => [
2410 "SFTPEngine on",
2411 "SFTPLog $setup->{log_file}",
2412 "SFTPHostKey $rsa_host_key",
2413 "SFTPHostKey $dsa_host_key",
2414
2415 "SFTPAuthorizedUserKeys file:~/.authorized_keys",
2416
2417 # Configure mod_sftp to require the publickey+keyboard-interactive
2418 # methods.
2419 'SFTPAuthMethods publickey+keyboard-interactive',
2420 ],
2421
2422 'mod_sql.c' => [
2423 'SQLEngine log',
2424 'SQLBackend sqlite3',
2425 "SQLConnectInfo $db_file",
2426 "SQLLogFile $setup->{log_file}",
2427
2428 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
2429 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
2430 ],
2431 },
2432 };
2433
2434 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2435 $config);
2436
2437 # Open pipes, for use between the parent and child processes. Specifically,
2438 # the child will indicate when it's done with its test by writing a message
2439 # to the parent.
2440 my ($rfh, $wfh);
2441 unless (pipe($rfh, $wfh)) {
2442 die("Can't open pipe: $!");
2443 }
2444
2445 my $ex;
2446
2447 require Authen::OATH;
2448 require Net::SSH2;
2449
2450 # Fork child
2451 $self->handle_sigchld();
2452 defined(my $pid = fork()) or die("Can't fork: $!");
2453 if ($pid) {
2454 eval {
2455 sleep(2);
2456
2457 my $ssh2 = Net::SSH2->new();
2458 unless ($ssh2->connect('127.0.0.1', $port)) {
2459 my ($err_code, $err_name, $err_str) = $ssh2->error();
2460 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2461 }
2462
2463 # NOTE: We deliberately ignore any failures here, since Net::SSH2 is not
2464 # sophisticated enough to deal with SSH servers that require multiple
2465 # different auth methods in success, as for 2FA.
2466 $ssh2->auth_publickey($setup->{user}, $rsa_pub_key, $rsa_priv_key);
2467
2468 # Calculate TOTP
2469 my $oath = Authen::OATH->new();
2470 my $totp = $oath->totp($secret);
2471 if ($ENV{TEST_VERBOSE}) {
2472 print STDERR "# Generated TOTP $totp for current time\n";
2473 }
2474
2475 unless ($ssh2->auth_keyboard($setup->{user}, $totp)) {
2476 my ($err_code, $err_name, $err_str) = $ssh2->error();
2477 die("Can't login to SSH2 server with TOTP: [$err_name] ($err_code) $err_str");
2478 }
2479
2480 my $sftp = $ssh2->sftp();
2481 $sftp = undef;
2482 $ssh2->disconnect();
2483 };
2484 if ($@) {
2485 $ex = $@;
2486 }
2487
2488 $wfh->print("done\n");
2489 $wfh->flush();
2490
2491 } else {
2492 eval { server_wait($setup->{config_file}, $rfh) };
2493 if ($@) {
2494 warn($@);
2495 exit 1;
2496 }
2497
2498 exit 0;
2499 }
2500
2501 # Stop server
2502 server_stop($setup->{pid_file});
2503 $self->assert_child_ok($pid);
2504
2505 test_cleanup($setup->{log_file}, $ex);
2506 }
2507
2508 sub auth_otp_sftp_password_plus_totp_ok {
2509 my $self = shift;
2510 my $tmpdir = $self->{tmpdir};
2511 my $setup = test_setup($tmpdir, 'auth_otp');
2512
2513 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2514
2515 # Build up sqlite3 command to create TOTP tables
2516 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
2517
2518 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
2519 # with Google Authenticator.
2520 require MIME::Base32;
2521
2522 my $secret = 'Sup3rS3Cr3t';
2523 my $base32_secret = MIME::Base32::encode_base32($secret);
2524 my $counter = 777;
2525
2526 if (open(my $fh, "> $db_script")) {
2527 print $fh <<EOS;
2528 CREATE TABLE auth_otp (
2529 user TEXT PRIMARY KEY,
2530 secret TEXT,
2531 counter INTEGER
2532 );
2533 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
2534
2535 EOS
2536
2537 unless (close($fh)) {
2538 die("Can't write $db_script: $!");
2539 }
2540
2541 } else {
2542 die("Can't open $db_script: $!");
2543 }
2544
2545 my $cmd = "sqlite3 $db_file < $db_script";
2546 build_db($cmd, $db_script, $db_file);
2547
2548 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
2549 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
2550
2551 my $config = {
2552 PidFile => $setup->{pid_file},
2553 ScoreboardFile => $setup->{scoreboard_file},
2554 SystemLog => $setup->{log_file},
2555 TraceLog => $setup->{log_file},
2556 Trace => 'ssh2:20 auth_otp:20 auth:20',
2557
2558 AuthUserFile => $setup->{auth_user_file},
2559 AuthGroupFile => $setup->{auth_group_file},
2560
2561 IfModules => {
2562 'mod_delay.c' => {
2563 DelayEngine => 'off',
2564 },
2565
2566 'mod_auth_otp.c' => {
2567 AuthOTPEngine => 'on',
2568 AuthOTPLog => $setup->{log_file},
2569 AuthOTPAlgorithm => 'totp',
2570
2571 # Assumes default table names, column names
2572 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
2573 },
2574
2575 'mod_sftp.c' => [
2576 "SFTPEngine on",
2577 "SFTPLog $setup->{log_file}",
2578 "SFTPHostKey $rsa_host_key",
2579 "SFTPHostKey $dsa_host_key",
2580
2581 # Configure mod_sftp to require the password+keyboard-interactive
2582 # methods.
2583 'SFTPAuthMethods password+keyboard-interactive',
2584 ],
2585
2586 'mod_sql.c' => [
2587 'SQLEngine log',
2588 'SQLBackend sqlite3',
2589 "SQLConnectInfo $db_file",
2590 "SQLLogFile $setup->{log_file}",
2591
2592 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
2593 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
2594 ],
2595 },
2596 };
2597
2598 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2599 $config);
2600
2601 # Open pipes, for use between the parent and child processes. Specifically,
2602 # the child will indicate when it's done with its test by writing a message
2603 # to the parent.
2604 my ($rfh, $wfh);
2605 unless (pipe($rfh, $wfh)) {
2606 die("Can't open pipe: $!");
2607 }
2608
2609 my $ex;
2610
2611 require Authen::OATH;
2612 require Net::SSH2;
2613
2614 # Fork child
2615 $self->handle_sigchld();
2616 defined(my $pid = fork()) or die("Can't fork: $!");
2617 if ($pid) {
2618 eval {
2619 sleep(2);
2620
2621 my $ssh2 = Net::SSH2->new();
2622 unless ($ssh2->connect('127.0.0.1', $port)) {
2623 my ($err_code, $err_name, $err_str) = $ssh2->error();
2624 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2625 }
2626
2627 # NOTE: We deliberately ignore any failures here, since Net::SSH2 is not
2628 # sophisticated enough to deal with SSH servers that require multiple
2629 # different auth methods in success, as for 2FA.
2630 $ssh2->auth_password($setup->{user}, $setup->{passwd});
2631
2632 # Calculate TOTP
2633 my $oath = Authen::OATH->new();
2634 my $totp = $oath->totp($secret);
2635 if ($ENV{TEST_VERBOSE}) {
2636 print STDERR "# Generated TOTP $totp for current time\n";
2637 }
2638
2639 unless ($ssh2->auth_keyboard($setup->{user}, $totp)) {
2640 my ($err_code, $err_name, $err_str) = $ssh2->error();
2641 die("Can't login to SSH2 server with TOTP: [$err_name] ($err_code) $err_str");
2642 }
2643
2644 my $sftp = $ssh2->sftp();
2645 $sftp = undef;
2646 $ssh2->disconnect();
2647 };
2648 if ($@) {
2649 $ex = $@;
2650 }
2651
2652 $wfh->print("done\n");
2653 $wfh->flush();
2654
2655 } else {
2656 eval { server_wait($setup->{config_file}, $rfh) };
2657 if ($@) {
2658 warn($@);
2659 exit 1;
2660 }
2661
2662 exit 0;
2663 }
2664
2665 # Stop server
2666 server_stop($setup->{pid_file});
2667 $self->assert_child_ok($pid);
2668
2669 test_cleanup($setup->{log_file}, $ex);
2670 }
2671
2672 sub auth_otp_sftp_publickey_plus_totp_with_display_verification_code_opt {
2673 my $self = shift;
2674 my $tmpdir = $self->{tmpdir};
2675 my $setup = test_setup($tmpdir, 'auth_otp');
2676
2677 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2678
2679 # Build up sqlite3 command to create TOTP tables
2680 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
2681
2682 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
2683 # with Google Authenticator.
2684 require MIME::Base32;
2685
2686 my $secret = 'Sup3rS3Cr3t';
2687 my $base32_secret = MIME::Base32::encode_base32($secret);
2688 my $counter = 777;
2689
2690 if (open(my $fh, "> $db_script")) {
2691 print $fh <<EOS;
2692 CREATE TABLE auth_otp (
2693 user TEXT PRIMARY KEY,
2694 secret TEXT,
2695 counter INTEGER
2696 );
2697 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
2698
2699 EOS
2700
2701 unless (close($fh)) {
2702 die("Can't write $db_script: $!");
2703 }
2704
2705 } else {
2706 die("Can't open $db_script: $!");
2707 }
2708
2709 my $cmd = "sqlite3 $db_file < $db_script";
2710 build_db($cmd, $db_script, $db_file);
2711
2712 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
2713 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
2714
2715 my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key');
2716 my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub');
2717 my $rsa_rfc4716_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/authorized_rsa_keys');
2718
2719 my $authorized_keys = File::Spec->rel2abs("$tmpdir/.authorized_keys");
2720 unless (copy($rsa_rfc4716_key, $authorized_keys)) {
2721 die("Can't copy $rsa_rfc4716_key to $authorized_keys: $!");
2722 }
2723
2724 my $config = {
2725 PidFile => $setup->{pid_file},
2726 ScoreboardFile => $setup->{scoreboard_file},
2727 SystemLog => $setup->{log_file},
2728 TraceLog => $setup->{log_file},
2729 Trace => 'ssh2:20 auth_otp:20',
2730
2731 AuthUserFile => $setup->{auth_user_file},
2732 AuthGroupFile => $setup->{auth_group_file},
2733
2734 IfModules => {
2735 'mod_delay.c' => {
2736 DelayEngine => 'off',
2737 },
2738
2739 'mod_auth_otp.c' => {
2740 AuthOTPEngine => 'on',
2741 AuthOTPLog => $setup->{log_file},
2742 AuthOTPAlgorithm => 'totp',
2743 AuthOTPOptions => 'DisplayVerificationCode',
2744
2745 # Assumes default table names, column names
2746 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
2747 },
2748
2749 'mod_sftp.c' => [
2750 "SFTPEngine on",
2751 "SFTPLog $setup->{log_file}",
2752 "SFTPHostKey $rsa_host_key",
2753 "SFTPHostKey $dsa_host_key",
2754
2755 "SFTPAuthorizedUserKeys file:~/.authorized_keys",
2756
2757 # Configure mod_sftp to require the publickey+keyboard-interactive
2758 # methods.
2759 'SFTPAuthMethods publickey+keyboard-interactive',
2760 ],
2761
2762 'mod_sql.c' => [
2763 'SQLEngine log',
2764 'SQLBackend sqlite3',
2765 "SQLConnectInfo $db_file",
2766 "SQLLogFile $setup->{log_file}",
2767
2768 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
2769 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
2770 ],
2771 },
2772 };
2773
2774 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2775 $config);
2776
2777 # Open pipes, for use between the parent and child processes. Specifically,
2778 # the child will indicate when it's done with its test by writing a message
2779 # to the parent.
2780 my ($rfh, $wfh);
2781 unless (pipe($rfh, $wfh)) {
2782 die("Can't open pipe: $!");
2783 }
2784
2785 my $ex;
2786
2787 require Authen::OATH;
2788 require Net::SSH2;
2789
2790 # Fork child
2791 $self->handle_sigchld();
2792 defined(my $pid = fork()) or die("Can't fork: $!");
2793 if ($pid) {
2794 eval {
2795 sleep(2);
2796
2797 my $ssh2 = Net::SSH2->new();
2798 unless ($ssh2->connect('127.0.0.1', $port)) {
2799 my ($err_code, $err_name, $err_str) = $ssh2->error();
2800 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2801 }
2802
2803 # NOTE: We deliberately ignore any failures here, since Net::SSH2 is not
2804 # sophisticated enough to deal with SSH servers that require multiple
2805 # different auth methods in success, as for 2FA.
2806 $ssh2->auth_publickey($setup->{user}, $rsa_pub_key, $rsa_priv_key);
2807
2808 # Calculate TOTP
2809 my $oath = Authen::OATH->new();
2810 my $totp = $oath->totp($secret);
2811 if ($ENV{TEST_VERBOSE}) {
2812 print STDERR "# Generated TOTP $totp for current time\n";
2813 }
2814
2815 unless ($ssh2->auth_keyboard($setup->{user},
2816 sub {
2817 my $self = shift;
2818 my $username = shift;
2819 my $name = shift;
2820 my $instruction = shift;
2821 my @prompt = shift;
2822
2823 # Note that in this case, `@prompt` is a one-element array,
2824 # whose element looks like this:
2825 # { 'echo' => 1, 'text' => 'Verification code: ' }
2826 #
2827 # which is the expected behavior/text from mod_auth_otp, when
2828 # the DisplayVerificationCode AuthOTPOption is in effect.
2829
2830 return ($totp);
2831 })) {
2832 my ($err_code, $err_name, $err_str) = $ssh2->error();
2833 die("Can't login to SSH2 server with TOTP: [$err_name] ($err_code) $err_str");
2834 }
2835
2836 my $sftp = $ssh2->sftp();
2837 $sftp = undef;
2838 $ssh2->disconnect();
2839 };
2840 if ($@) {
2841 $ex = $@;
2842 }
2843
2844 $wfh->print("done\n");
2845 $wfh->flush();
2846
2847 } else {
2848 eval { server_wait($setup->{config_file}, $rfh) };
2849 if ($@) {
2850 warn($@);
2851 exit 1;
2852 }
2853
2854 exit 0;
2855 }
2856
2857 # Stop server
2858 server_stop($setup->{pid_file});
2859 $self->assert_child_ok($pid);
2860
2861 test_cleanup($setup->{log_file}, $ex);
2862 }
2863
2864 sub auth_otp_sftp_password_plus_totp_with_display_verification_code_opt {
2865 my $self = shift;
2866 my $tmpdir = $self->{tmpdir};
2867 my $setup = test_setup($tmpdir, 'auth_otp');
2868
2869 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
2870
2871 # Build up sqlite3 command to create TOTP tables
2872 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
2873
2874 # mod_auth_otp wants this secret to be base32-encoded, for interoperability
2875 # with Google Authenticator.
2876 require MIME::Base32;
2877
2878 my $secret = 'Sup3rS3Cr3t';
2879 my $base32_secret = MIME::Base32::encode_base32($secret);
2880 my $counter = 777;
2881
2882 if (open(my $fh, "> $db_script")) {
2883 print $fh <<EOS;
2884 CREATE TABLE auth_otp (
2885 user TEXT PRIMARY KEY,
2886 secret TEXT,
2887 counter INTEGER
2888 );
2889 INSERT INTO auth_otp (user, secret, counter) VALUES ('$setup->{user}', '$base32_secret', $counter);
2890
2891 EOS
2892
2893 unless (close($fh)) {
2894 die("Can't write $db_script: $!");
2895 }
2896
2897 } else {
2898 die("Can't open $db_script: $!");
2899 }
2900
2901 my $cmd = "sqlite3 $db_file < $db_script";
2902 build_db($cmd, $db_script, $db_file);
2903
2904 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
2905 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
2906
2907 my $config = {
2908 PidFile => $setup->{pid_file},
2909 ScoreboardFile => $setup->{scoreboard_file},
2910 SystemLog => $setup->{log_file},
2911 TraceLog => $setup->{log_file},
2912 Trace => 'ssh2:20 auth_otp:20',
2913
2914 AuthUserFile => $setup->{auth_user_file},
2915 AuthGroupFile => $setup->{auth_group_file},
2916
2917 IfModules => {
2918 'mod_delay.c' => {
2919 DelayEngine => 'off',
2920 },
2921
2922 'mod_auth_otp.c' => {
2923 AuthOTPEngine => 'on',
2924 AuthOTPLog => $setup->{log_file},
2925 AuthOTPAlgorithm => 'totp',
2926 AuthOTPOptions => 'DisplayVerificationCode',
2927
2928 # Assumes default table names, column names
2929 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
2930 },
2931
2932 'mod_sftp.c' => [
2933 "SFTPEngine on",
2934 "SFTPLog $setup->{log_file}",
2935 "SFTPHostKey $rsa_host_key",
2936 "SFTPHostKey $dsa_host_key",
2937
2938 # Configure mod_sftp to require the password+keyboard-interactive
2939 # methods.
2940 'SFTPAuthMethods password+keyboard-interactive',
2941 ],
2942
2943 'mod_sql.c' => [
2944 'SQLEngine log',
2945 'SQLBackend sqlite3',
2946 "SQLConnectInfo $db_file",
2947 "SQLLogFile $setup->{log_file}",
2948
2949 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
2950 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
2951 ],
2952 },
2953 };
2954
2955 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
2956 $config);
2957
2958 # Open pipes, for use between the parent and child processes. Specifically,
2959 # the child will indicate when it's done with its test by writing a message
2960 # to the parent.
2961 my ($rfh, $wfh);
2962 unless (pipe($rfh, $wfh)) {
2963 die("Can't open pipe: $!");
2964 }
2965
2966 my $ex;
2967
2968 require Authen::OATH;
2969 require Net::SSH2;
2970
2971 # Fork child
2972 $self->handle_sigchld();
2973 defined(my $pid = fork()) or die("Can't fork: $!");
2974 if ($pid) {
2975 eval {
2976 sleep(2);
2977
2978 my $ssh2 = Net::SSH2->new();
2979 unless ($ssh2->connect('127.0.0.1', $port)) {
2980 my ($err_code, $err_name, $err_str) = $ssh2->error();
2981 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
2982 }
2983
2984 # NOTE: We deliberately ignore any failures here, since Net::SSH2 is not
2985 # sophisticated enough to deal with SSH servers that require multiple
2986 # different auth methods in success, as for 2FA.
2987 $ssh2->auth_password($setup->{user}, $setup->{passwd});
2988
2989 # Calculate TOTP
2990 my $oath = Authen::OATH->new();
2991 my $totp = $oath->totp($secret);
2992 if ($ENV{TEST_VERBOSE}) {
2993 print STDERR "# Generated TOTP $totp for current time\n";
2994 }
2995
2996 unless ($ssh2->auth_keyboard($setup->{user},
2997 sub {
2998 my $self = shift;
2999 my $username = shift;
3000 my $name = shift;
3001 my $instruction = shift;
3002 my @prompt = shift;
3003
3004 # Note that in this case, `@prompt` is a one-element array,
3005 # whose element looks like this:
3006 # { 'echo' => 1, 'text' => 'Verification code: ' }
3007 #
3008 # which is the expected behavior/text from mod_auth_otp, when
3009 # the DisplayVerificationCode AuthOTPOption is in effect.
3010
3011 return ($totp);
3012 })) {
3013 my ($err_code, $err_name, $err_str) = $ssh2->error();
3014 die("Can't login to SSH2 server with TOTP: [$err_name] ($err_code) $err_str");
3015 }
3016
3017 my $sftp = $ssh2->sftp();
3018 $sftp = undef;
3019 $ssh2->disconnect();
3020 };
3021 if ($@) {
3022 $ex = $@;
3023 }
3024
3025 $wfh->print("done\n");
3026 $wfh->flush();
3027
3028 } else {
3029 eval { server_wait($setup->{config_file}, $rfh) };
3030 if ($@) {
3031 warn($@);
3032 exit 1;
3033 }
3034
3035 exit 0;
3036 }
3037
3038 # Stop server
3039 server_stop($setup->{pid_file});
3040 $self->assert_child_ok($pid);
3041
3042 test_cleanup($setup->{log_file}, $ex);
3043 }
3044
3045 sub auth_otp_sftp_publickey_plus_totp_lacking_table_entry_issue1319 {
3046 my $self = shift;
3047 my $tmpdir = $self->{tmpdir};
3048 my $setup = test_setup($tmpdir, 'auth_otp');
3049
3050 my $secret = 'Sup3rS3Cr3t';
3051 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3052
3053 # Build up sqlite3 command to create TOTP tables
3054 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
3055
3056 if (open(my $fh, "> $db_script")) {
3057 print $fh <<EOS;
3058 CREATE TABLE auth_otp (
3059 user TEXT PRIMARY KEY,
3060 secret TEXT,
3061 counter INTEGER
3062 );
3063 EOS
3064
3065 unless (close($fh)) {
3066 die("Can't write $db_script: $!");
3067 }
3068
3069 } else {
3070 die("Can't open $db_script: $!");
3071 }
3072
3073 my $cmd = "sqlite3 $db_file < $db_script";
3074 build_db($cmd, $db_script, $db_file);
3075
3076 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
3077 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
3078
3079 my $rsa_priv_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key');
3080 my $rsa_pub_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/test_rsa_key.pub');
3081 my $rsa_rfc4716_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/authorized_rsa_keys');
3082
3083 my $authorized_keys = File::Spec->rel2abs("$tmpdir/.authorized_keys");
3084 unless (copy($rsa_rfc4716_key, $authorized_keys)) {
3085 die("Can't copy $rsa_rfc4716_key to $authorized_keys: $!");
3086 }
3087
3088 my $config = {
3089 PidFile => $setup->{pid_file},
3090 ScoreboardFile => $setup->{scoreboard_file},
3091 SystemLog => $setup->{log_file},
3092 TraceLog => $setup->{log_file},
3093 Trace => 'ssh2:20 auth_otp:20 auth:20',
3094
3095 AuthUserFile => $setup->{auth_user_file},
3096 AuthGroupFile => $setup->{auth_group_file},
3097
3098 IfModules => {
3099 'mod_delay.c' => {
3100 DelayEngine => 'off',
3101 },
3102
3103 'mod_auth_otp.c' => {
3104 AuthOTPEngine => 'on',
3105 AuthOTPLog => $setup->{log_file},
3106 AuthOTPAlgorithm => 'totp',
3107
3108 # Assumes default table names, column names
3109 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
3110
3111 # Table entries are now required by default, per Issue #1562
3112 AuthOTPOptions => 'OptionalTableEntry',
3113 },
3114
3115 'mod_sftp.c' => [
3116 "SFTPEngine on",
3117 "SFTPLog $setup->{log_file}",
3118 "SFTPHostKey $rsa_host_key",
3119 "SFTPHostKey $dsa_host_key",
3120 "SFTPAuthorizedUserKeys file:~/.authorized_keys",
3121
3122 # Configure mod_sftp to require the publickey+keyboard-interactive
3123 # methods.
3124 'SFTPAuthMethods publickey+keyboard-interactive',
3125 ],
3126
3127 'mod_sql.c' => [
3128 'SQLEngine log',
3129 'SQLBackend sqlite3',
3130 "SQLConnectInfo $db_file",
3131 "SQLLogFile $setup->{log_file}",
3132
3133 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
3134 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
3135 ],
3136 },
3137 };
3138
3139 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
3140 $config);
3141
3142 # Open pipes, for use between the parent and child processes. Specifically,
3143 # the child will indicate when it's done with its test by writing a message
3144 # to the parent.
3145 my ($rfh, $wfh);
3146 unless (pipe($rfh, $wfh)) {
3147 die("Can't open pipe: $!");
3148 }
3149
3150 my $ex;
3151
3152 require Authen::OATH;
3153 require Net::SSH2;
3154
3155 # Fork child
3156 $self->handle_sigchld();
3157 defined(my $pid = fork()) or die("Can't fork: $!");
3158 if ($pid) {
3159 eval {
3160 sleep(2);
3161
3162 my $ssh2 = Net::SSH2->new();
3163 unless ($ssh2->connect('127.0.0.1', $port)) {
3164 my ($err_code, $err_name, $err_str) = $ssh2->error();
3165 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
3166 }
3167
3168 # NOTE: We deliberately ignore any failures here, since Net::SSH2 is not
3169 # sophisticated enough to deal with SSH servers that require multiple
3170 # different auth methods in success, as for 2FA.
3171 $ssh2->auth_publickey($setup->{user}, $rsa_pub_key, $rsa_priv_key);
3172
3173 # Calculate TOTP
3174 my $oath = Authen::OATH->new();
3175 my $totp = $oath->totp($secret);
3176 if ($ENV{TEST_VERBOSE}) {
3177 print STDERR "# Generated TOTP $totp for current time\n";
3178 }
3179
3180 unless ($ssh2->auth_keyboard($setup->{user}, $totp)) {
3181 my ($err_code, $err_name, $err_str) = $ssh2->error();
3182 die("Can't login to SSH2 server with TOTP: [$err_name] ($err_code) $err_str");
3183 }
3184
3185 my $sftp = $ssh2->sftp();
3186 $sftp = undef;
3187 $ssh2->disconnect();
3188 };
3189 if ($@) {
3190 $ex = $@;
3191 }
3192
3193 $wfh->print("done\n");
3194 $wfh->flush();
3195
3196 } else {
3197 eval { server_wait($setup->{config_file}, $rfh) };
3198 if ($@) {
3199 warn($@);
3200 exit 1;
3201 }
3202
3203 exit 0;
3204 }
3205
3206 # Stop server
3207 server_stop($setup->{pid_file});
3208 $self->assert_child_ok($pid);
3209
3210 test_cleanup($setup->{log_file}, $ex);
3211 }
3212
3213 sub auth_otp_sftp_password_plus_totp_lacking_table_entry_issue1319 {
3214 my $self = shift;
3215 my $tmpdir = $self->{tmpdir};
3216 my $setup = test_setup($tmpdir, 'auth_otp');
3217
3218 my $secret = 'Sup3rS3Cr3t';
3219 my $db_file = File::Spec->rel2abs("$tmpdir/proftpd.db");
3220
3221 # Build up sqlite3 command to create TOTP tables
3222 my $db_script = File::Spec->rel2abs("$tmpdir/totp.sql");
3223
3224 if (open(my $fh, "> $db_script")) {
3225 print $fh <<EOS;
3226 CREATE TABLE auth_otp (
3227 user TEXT PRIMARY KEY,
3228 secret TEXT,
3229 counter INTEGER
3230 );
3231 EOS
3232
3233 unless (close($fh)) {
3234 die("Can't write $db_script: $!");
3235 }
3236
3237 } else {
3238 die("Can't open $db_script: $!");
3239 }
3240
3241 my $cmd = "sqlite3 $db_file < $db_script";
3242 build_db($cmd, $db_script, $db_file);
3243
3244 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_rsa_key');
3245 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_auth_otp/ssh_host_dsa_key');
3246
3247 my $config = {
3248 PidFile => $setup->{pid_file},
3249 ScoreboardFile => $setup->{scoreboard_file},
3250 SystemLog => $setup->{log_file},
3251 TraceLog => $setup->{log_file},
3252 Trace => 'ssh2:20 auth_otp:20 auth:20',
3253
3254 AuthUserFile => $setup->{auth_user_file},
3255 AuthGroupFile => $setup->{auth_group_file},
3256
3257 IfModules => {
3258 'mod_delay.c' => {
3259 DelayEngine => 'off',
3260 },
3261
3262 'mod_auth_otp.c' => {
3263 AuthOTPEngine => 'on',
3264 AuthOTPLog => $setup->{log_file},
3265 AuthOTPAlgorithm => 'totp',
3266
3267 # Assumes default table names, column names
3268 AuthOTPTable => 'sql:/get-user-totp/update-user-totp',
3269
3270 # Table entries are now required by default, per Issue #1562
3271 AuthOTPOptions => 'OptionalTableEntry',
3272 },
3273
3274 'mod_sftp.c' => [
3275 "SFTPEngine on",
3276 "SFTPLog $setup->{log_file}",
3277 "SFTPHostKey $rsa_host_key",
3278 "SFTPHostKey $dsa_host_key",
3279
3280 # Configure mod_sftp to require the password+keyboard-interactive
3281 # methods.
3282 'SFTPAuthMethods password+keyboard-interactive',
3283 ],
3284
3285 'mod_sql.c' => [
3286 'SQLEngine log',
3287 'SQLBackend sqlite3',
3288 "SQLConnectInfo $db_file",
3289 "SQLLogFile $setup->{log_file}",
3290
3291 'SQLNamedQuery get-user-totp SELECT "secret, counter FROM auth_otp WHERE user = \'%{0}\'"',
3292 'SQLNamedQuery update-user-totp UPDATE "counter = %{1} WHERE user = \'%{0}\'" auth_otp',
3293 ],
3294 },
3295 };
3296
3297 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
3298 $config);
3299
3300 # Open pipes, for use between the parent and child processes. Specifically,
3301 # the child will indicate when it's done with its test by writing a message
3302 # to the parent.
3303 my ($rfh, $wfh);
3304 unless (pipe($rfh, $wfh)) {
3305 die("Can't open pipe: $!");
3306 }
3307
3308 my $ex;
3309
3310 require Authen::OATH;
3311 require Net::SSH2;
3312
3313 # Fork child
3314 $self->handle_sigchld();
3315 defined(my $pid = fork()) or die("Can't fork: $!");
3316 if ($pid) {
3317 eval {
3318 sleep(2);
3319
3320 my $ssh2 = Net::SSH2->new();
3321 unless ($ssh2->connect('127.0.0.1', $port)) {
3322 my ($err_code, $err_name, $err_str) = $ssh2->error();
3323 die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
3324 }
3325
3326 # NOTE: We deliberately ignore any failures here, since Net::SSH2 is not
3327 # sophisticated enough to deal with SSH servers that require multiple
3328 # different auth methods in success, as for 2FA.
3329 $ssh2->auth_password($setup->{user}, $setup->{passwd});
3330
3331 # Calculate TOTP
3332 my $oath = Authen::OATH->new();
3333 my $totp = $oath->totp($secret);
3334 if ($ENV{TEST_VERBOSE}) {
3335 print STDERR "# Generated TOTP $totp for current time\n";
3336 }
3337
3338 unless ($ssh2->auth_keyboard($setup->{user}, $totp)) {
3339 my ($err_code, $err_name, $err_str) = $ssh2->error();
3340 die("Can't login to SSH2 server with TOTP: [$err_name] ($err_code) $err_str");
3341 }
3342
3343 my $sftp = $ssh2->sftp();
3344 $sftp = undef;
3345 $ssh2->disconnect();
3346 };
3347 if ($@) {
3348 $ex = $@;
3349 }
3350
3351 $wfh->print("done\n");
3352 $wfh->flush();
3353
3354 } else {
3355 eval { server_wait($setup->{config_file}, $rfh) };
3356 if ($@) {
3357 warn($@);
3358 exit 1;
3359 }
3360
3361 exit 0;
3362 }
3363
3364 # Stop server
3365 server_stop($setup->{pid_file});
3366 $self->assert_child_ok($pid);
3367
3368 test_cleanup($setup->{log_file}, $ex);
3369 }
3370
3371 1;