Debian Codegraph @

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

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