Debian Codegraph @

debian/patches/536608f849dddd2b551e7c1e4fd930a50a527ffe.diff

raw
1 From 536608f849dddd2b551e7c1e4fd930a50a527ffe Mon Sep 17 00:00:00 2001
2 From: TJ Saunders <[email protected]>
3 Date: Sun, 23 Mar 2025 12:47:04 -0700
4 Subject: [PATCH] Issue #1886: Fix the handling of SCP target paths that might
5 contain spaces.
6 Origin: https://github.com/proftpd/proftpd/commit/b8acdd8754a0dbb13af105c9ffe98e9be4118bbc
7
8 ---
9 contrib/mod_sftp/scp.c | 229 +++++-----
10 tests/t/lib/ProFTPD/Tests/Modules/mod_sftp.pm | 399 +++++++++++++++---
11 2 files changed, 475 insertions(+), 153 deletions(-)
12
13 diff --git a/contrib/mod_sftp/scp.c b/contrib/mod_sftp/scp.c
14 index 397cea41c..ea8154343 100644
15 --- a/contrib/mod_sftp/scp.c
16 +++ b/contrib/mod_sftp/scp.c
17 @@ -1,6 +1,6 @@
18 /*
19 * ProFTPD - mod_sftp SCP
20 - * Copyright (c) 2008-2023 TJ Saunders
21 + * Copyright (c) 2008-2025 TJ Saunders
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 @@ -2458,13 +2458,36 @@ int sftp_scp_handle_packet(pool *p, void *ssh2, uint32_t channel_id,
26 return 0;
27 }
28
29 +static char *scp_canonicalize_target(pool *p, char *target) {
30 + size_t target_len;
31 +
32 + target_len = strlen(target);
33 +
34 + /* Remove any enclosing shell quotations, e.g. single and double quotation
35 + * marks. Some SCP clients (i.e. newer libssh2) will quote the paths,
36 + * assuming that the handling server (us) uses a shell to handle the
37 + * command. Sigh.
38 + */
39 +
40 + if ((target[0] == '\'' &&
41 + target[target_len-1] == '\'') ||
42 + (target[0] == '"' &&
43 + target[target_len-1] == '"')) {
44 + target[target_len-1] = '\0';
45 + return pstrdup(p, target + 1);
46 + }
47 +
48 + return target;
49 +}
50 +
51 int sftp_scp_set_params(pool *p, uint32_t channel_id, array_header *req) {
52 register unsigned int i;
53 int optc, use_glob = TRUE;
54 - char **reqargv;
55 + char **reqargv, *target_path = NULL;
56 const char *opts = "dfprtv";
57 config_rec *c;
58 struct scp_paths *paths;
59 + struct scp_path *sp;
60
61 if (!(sftp_services & SFTP_SERVICE_FL_SCP)) {
62 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
63 @@ -2487,13 +2510,13 @@ int sftp_scp_set_params(pool *p, uint32_t channel_id, array_header *req) {
64
65 reqargv = (char **) req->elts;
66 for (i = 0; i < req->nelts; i++) {
67 - if (reqargv[i]) {
68 + if (reqargv[i] != NULL) {
69 pr_trace_msg(trace_channel, 5, "reqargv[%u] = '%s'", i, reqargv[i]);
70 }
71 }
72
73 c = find_config(main_server->conf, CONF_PARAM, "UseGlobbing", FALSE);
74 - if (c) {
75 + if (c != NULL) {
76 use_glob = *((unsigned char *) c->argv[0]);
77 }
78
79 @@ -2546,128 +2569,130 @@ int sftp_scp_set_params(pool *p, uint32_t channel_id, array_header *req) {
80 return -1;
81 }
82
83 - /* Make a copy of the remaining paths, for later handling. */
84 + /* Make a copy of the remaining paths, for later handling.
85 + *
86 + * Normally there's only one path, but we might be handling a glob which
87 + * matches multiple paths.
88 + */
89 paths->paths = make_array(paths->pool, 1, sizeof(struct scp_path *));
90 paths->path_idx = 0;
91
92 + /* First concatenate any remaining command-line arguments, using spaces,
93 + * in order to handle paths that contain spaces; see Issue #1886.
94 + */
95 for (i = optind; i < req->nelts; i++) {
96 pr_signals_handle();
97
98 - if (reqargv[i]) {
99 - struct scp_path *sp;
100 - size_t pathlen;
101 - char *glob_path;
102 + if (reqargv[i] != NULL) {
103 + size_t reqarg_len;
104
105 - if (use_glob &&
106 - (scp_opts & SFTP_SCP_OPT_ISSRC) &&
107 - strpbrk(reqargv[i], "{[*?") != NULL) {
108 - int res, xerrno;
109 - glob_t gl;
110 + /* Note that we might be dealing with an empty-length argument, which
111 + * we get from pr_str_get_word() parsing the client-provided string,
112 + * which might have spaces. Watch for them.
113 + */
114
115 - /* Whee, glob characters. Need to expand the pattern to the
116 - * list of matching files, just as the shell would do.
117 - */
118 + reqarg_len = strlen(reqargv[i]);
119 + if (reqarg_len == 0) {
120 + continue;
121 + }
122
123 - memset(&gl, 0, sizeof(gl));
124 + if (target_path != NULL) {
125 + target_path = pstrcat(p, target_path, " ", reqargv[i], NULL);
126
127 - glob_path = pstrdup(paths->pool, reqargv[i]);
128 - pathlen = strlen(glob_path);
129 + } else {
130 + target_path = pstrdup(p, reqargv[i]);
131 + }
132 + }
133 + }
134
135 - /* Remove any enclosing shell quotations, e.g. single and double
136 - * quotation marks. Some SCP clients (i.e. newer libssh2) will
137 - * quote the paths, assuming that the handling server (us) uses
138 - * a shell to handle the command. Sigh.
139 - */
140 - if ((glob_path[0] == '\'' &&
141 - glob_path[pathlen-1] == '\'') ||
142 - (glob_path[0] == '"' &&
143 - glob_path[pathlen-1] == '"')) {
144 - glob_path[pathlen-1] = '\0';
145 - glob_path = (glob_path + 1);
146 - }
147 + target_path = scp_canonicalize_target(p, target_path);
148
149 - res = pr_fs_glob(glob_path, GLOB_NOSORT|GLOB_BRACE, NULL, &gl);
150 - switch (res) {
151 - case 0: {
152 - register unsigned int j;
153 + if (use_glob == TRUE &&
154 + (scp_opts & SFTP_SCP_OPT_ISSRC) &&
155 + strpbrk(target_path, "{[*?") != NULL) {
156 + int res, xerrno;
157 + char *glob_path;
158 + size_t path_len;
159 + glob_t gl;
160
161 - for (j = 0; j < gl.gl_pathc; j++) {
162 - pr_signals_handle();
163 + /* Whee, glob characters. Need to expand the pattern to the
164 + * list of matching files, just as the shell would do.
165 + */
166
167 - sp = pcalloc(paths->pool, sizeof(struct scp_path));
168 - sp->path = pstrdup(paths->pool, gl.gl_pathv[j]);
169 - pathlen = strlen(sp->path);
170 + memset(&gl, 0, sizeof(gl));
171
172 - /* Trim any trailing path separators. It's important. */
173 - while (pathlen > 1 &&
174 - sp->path[pathlen-1] == '/') {
175 - pr_signals_handle();
176 - sp->path[--pathlen] = '\0';
177 - }
178 + glob_path = pstrdup(paths->pool, target_path);
179 + path_len = strlen(glob_path);
180
181 - sp->orig_path = pstrdup(paths->pool, sp->path);
182 + res = pr_fs_glob(glob_path, GLOB_NOSORT|GLOB_BRACE, NULL, &gl);
183 + switch (res) {
184 + case 0: {
185 + register unsigned int j;
186
187 - if (pathlen > 0) {
188 - *((struct scp_path **) push_array(paths->paths)) = sp;
189 - }
190 - }
191 + for (j = 0; j < gl.gl_pathc; j++) {
192 + pr_signals_handle();
193
194 - break;
195 + sp = pcalloc(paths->pool, sizeof(struct scp_path));
196 + sp->path = pstrdup(paths->pool, gl.gl_pathv[j]);
197 + path_len = strlen(sp->path);
198 +
199 + /* Trim any trailing path separators. It's important. */
200 + while (path_len > 1 &&
201 + sp->path[path_len-1] == '/') {
202 + pr_signals_handle();
203 + sp->path[--path_len] = '\0';
204 }
205
206 - case GLOB_NOSPACE:
207 - xerrno = errno;
208 - pr_trace_msg(trace_channel, 1, "error globbing '%s': Not "
209 - "enough memory (%s)", reqargv[i], strerror(xerrno));
210 - write_confirm(p, channel_id, 1, pstrcat(p, reqargv[i], ": ",
211 - strerror(xerrno), NULL));
212 - errno = xerrno;
213 - return 0;
214 -
215 - case GLOB_NOMATCH:
216 - xerrno = ENOENT;
217 - pr_trace_msg(trace_channel, 1, "error globbing '%s': No "
218 - "matches found (%s)", reqargv[i], strerror(xerrno));
219 - write_confirm(p, channel_id, 1, pstrcat(p, reqargv[i], ": ",
220 - strerror(xerrno), NULL));
221 - errno = xerrno;
222 - return 0;
223 + sp->orig_path = pstrdup(paths->pool, sp->path);
224 +
225 + if (path_len > 0) {
226 + *((struct scp_path **) push_array(paths->paths)) = sp;
227 + }
228 }
229
230 - pr_fs_globfree(&gl);
231 + break;
232 + }
233
234 - } else {
235 - sp = pcalloc(paths->pool, sizeof(struct scp_path));
236 - sp->path = pstrdup(paths->pool, reqargv[i]);
237 - pathlen = strlen(sp->path);
238 -
239 - /* Remove any enclosing shell quotations, e.g. single and double
240 - * quotation marks. Some SCP clients (i.e. newer libssh2) will
241 - * quote the paths, assuming that the handling server (us) uses
242 - * a shell to handle the command. Sigh.
243 - */
244 - if ((sp->path[0] == '\'' &&
245 - sp->path[pathlen-1] == '\'') ||
246 - (sp->path[0] == '"' &&
247 - sp->path[pathlen-1] == '"')) {
248 - sp->path[pathlen-1] = '\0';
249 - sp->path = (sp->path + 1);
250 - pathlen -= 2;
251 - }
252 + case GLOB_NOSPACE:
253 + xerrno = errno;
254 + pr_trace_msg(trace_channel, 1, "error globbing '%s': Not "
255 + "enough memory (%s)", reqargv[i], strerror(xerrno));
256 + write_confirm(p, channel_id, 1, pstrcat(p, target_path, ": ",
257 + strerror(xerrno), NULL));
258 + errno = xerrno;
259 + return 0;
260
261 - /* Trim any trailing path separators. It's important. */
262 - while (pathlen > 1 &&
263 - sp->path[pathlen-1] == '/') {
264 - pr_signals_handle();
265 - sp->path[--pathlen] = '\0';
266 - }
267 + case GLOB_NOMATCH:
268 + xerrno = ENOENT;
269 + pr_trace_msg(trace_channel, 1, "error globbing '%s': No "
270 + "matches found (%s)", reqargv[i], strerror(xerrno));
271 + write_confirm(p, channel_id, 1, pstrcat(p, target_path, ": ",
272 + strerror(xerrno), NULL));
273 + errno = xerrno;
274 + return 0;
275 + }
276
277 - sp->orig_path = pstrdup(paths->pool, sp->path);
278 + pr_fs_globfree(&gl);
279
280 - if (pathlen > 0) {
281 - *((struct scp_path **) push_array(paths->paths)) = sp;
282 - }
283 - }
284 + } else {
285 + size_t path_len;
286 +
287 + sp = pcalloc(paths->pool, sizeof(struct scp_path));
288 + sp->path = pstrdup(paths->pool, target_path);
289 +
290 + path_len = strlen(sp->path);
291 +
292 + /* Trim any trailing path separators. It's important. */
293 + while (path_len > 1 &&
294 + sp->path[path_len-1] == '/') {
295 + pr_signals_handle();
296 + sp->path[--path_len] = '\0';
297 + }
298 +
299 + sp->orig_path = pstrdup(paths->pool, sp->path);
300 +
301 + if (path_len > 0) {
302 + *((struct scp_path **) push_array(paths->paths)) = sp;
303 }
304 }
305
306 @@ -2687,8 +2712,8 @@ int sftp_scp_set_params(pool *p, uint32_t channel_id, array_header *req) {
307 struct scp_path *sp;
308
309 sp = ((struct scp_path **) paths->paths->elts)[i];
310 - if (sp) {
311 - pr_trace_msg(trace_channel, 5, "scp_path[%u] = '%s'", i, sp->path);
312 + if (sp != NULL) {
313 + pr_trace_msg(trace_channel, 5, "scp path[%u] = '%s'", i, sp->path);
314 }
315 }
316
317 diff --git a/tests/t/lib/ProFTPD/Tests/Modules/mod_sftp.pm b/tests/t/lib/ProFTPD/Tests/Modules/mod_sftp.pm
318 index be37f1eb3..c445399ff 100644
319 --- a/tests/t/lib/ProFTPD/Tests/Modules/mod_sftp.pm
320 +++ b/tests/t/lib/ProFTPD/Tests/Modules/mod_sftp.pm
321 @@ -1676,6 +1676,11 @@ my $TESTS = {
322 test_class => [qw(forking scp ssh2)],
323 },
324
325 + scp_upload_filename_with_spaces_issue1886 => {
326 + order => ++$order,
327 + test_class => [qw(bug forking scp ssh2)],
328 + },
329 +
330 scp_upload_abs_symlink => {
331 order => ++$order,
332 test_class => [qw(forking scp ssh2)],
333 @@ -1806,6 +1811,11 @@ my $TESTS = {
334 test_class => [qw(bug forking rootprivs scp ssh2)],
335 },
336
337 + scp_download_filename_with_spaces_issue1886 => {
338 + order => ++$order,
339 + test_class => [qw(bug forking scp ssh2)],
340 + },
341 +
342 scp_ext_download_bug3544 => {
343 order => ++$order,
344 test_class => [qw(bug forking scp ssh2)],
345 @@ -56731,6 +56741,166 @@ sub scp_upload_largefile {
346 test_cleanup($setup->{log_file}, $ex);
347 }
348
349 +sub scp_upload_filename_with_spaces_issue1886 {
350 + my $self = shift;
351 + my $tmpdir = $self->{tmpdir};
352 + my $setup = test_setup($tmpdir, 'scp');
353 +
354 + my $src_file = File::Spec->rel2abs("$tmpdir/src.txt");
355 + if (open(my $fh, "> $src_file")) {
356 + print $fh "ABCDefgh" x 32;
357 + unless (close($fh)) {
358 + die("Can't write $src_file: $!");
359 + }
360 +
361 + } else {
362 + die("Can't open $src_file: $!");
363 + }
364 +
365 + # Calculate the MD5 checksum of this file, for comparison with the
366 + # downloaded file.
367 + my $ctx = Digest::MD5->new();
368 + my $expected_md5;
369 +
370 + if (open(my $fh, "< $src_file")) {
371 + binmode($fh);
372 + $ctx->addfile($fh);
373 + $expected_md5 = $ctx->hexdigest();
374 + close($fh);
375 +
376 + } else {
377 + die("Can't read $src_file: $!");
378 + }
379 +
380 + my $dst_file = File::Spec->rel2abs("$tmpdir/test filename with spaces.txt");
381 +
382 + my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
383 + my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
384 +
385 + my $config = {
386 + PidFile => $setup->{pid_file},
387 + ScoreboardFile => $setup->{scoreboard_file},
388 + SystemLog => $setup->{log_file},
389 + TraceLog => $setup->{log_file},
390 + Trace => 'ssh2:10 scp:30',
391 +
392 + AuthUserFile => $setup->{auth_user_file},
393 + AuthGroupFile => $setup->{auth_group_file},
394 + AuthOrder => 'mod_auth_file.c',
395 +
396 + IfModules => {
397 + 'mod_delay.c' => {
398 + DelayEngine => 'off',
399 + },
400 +
401 + 'mod_sftp.c' => [
402 + "SFTPEngine on",
403 + "SFTPLog $setup->{log_file}",
404 + "SFTPHostKey $rsa_host_key",
405 + "SFTPHostKey $dsa_host_key",
406 + ],
407 + },
408 + };
409 +
410 + my ($port, $config_user, $config_group) = config_write($setup->{config_file},
411 + $config);
412 +
413 + # Open pipes, for use between the parent and child processes. Specifically,
414 + # the child will indicate when it's done with its test by writing a message
415 + # to the parent.
416 + my ($rfh, $wfh);
417 + unless (pipe($rfh, $wfh)) {
418 + die("Can't open pipe: $!");
419 + }
420 +
421 + require Net::SSH2;
422 +
423 + my $ex;
424 +
425 + # Ignore SIGPIPE
426 + local $SIG{PIPE} = sub { };
427 +
428 + # Fork child
429 + $self->handle_sigchld();
430 + defined(my $pid = fork()) or die("Can't fork: $!");
431 + if ($pid) {
432 + eval {
433 + my $ssh2 = Net::SSH2->new();
434 +
435 + sleep(1);
436 +
437 + unless ($ssh2->connect('127.0.0.1', $port)) {
438 + my ($err_code, $err_name, $err_str) = $ssh2->error();
439 + die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
440 + }
441 +
442 + unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) {
443 + my ($err_code, $err_name, $err_str) = $ssh2->error();
444 + die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
445 + }
446 +
447 + my $res = $ssh2->scp_put($src_file, 'test filename with spaces.txt');
448 + unless ($res) {
449 + my ($err_code, $err_name, $err_str) = $ssh2->error();
450 + die("Can't upload to server: [$err_name] ($err_code) $err_str");
451 + }
452 +
453 + $ssh2->disconnect();
454 +
455 + $self->assert(-f $dst_file,
456 + test_msg("File '$dst_file' does not exist as expected"));
457 + };
458 + if ($@) {
459 + $ex = $@;
460 + }
461 +
462 + $wfh->print("done\n");
463 + $wfh->flush();
464 +
465 + } else {
466 + eval { server_wait($setup->{config_file}, $rfh) };
467 + if ($@) {
468 + warn($@);
469 + exit 1;
470 + }
471 +
472 + exit 0;
473 + }
474 +
475 + # Stop server
476 + server_stop($setup->{pid_file});
477 + $self->assert_child_ok($pid);
478 +
479 + if ($ex) {
480 + test_cleanup($setup->{log_file}, $ex);
481 + }
482 +
483 + # Calculate the MD5 checksum of the uploaded file, for comparison with the
484 + # file that was uploaded.
485 + $ctx->reset();
486 + my $md5;
487 +
488 + eval {
489 + if (open(my $fh, "< $dst_file")) {
490 + binmode($fh);
491 + $ctx->addfile($fh);
492 + $md5 = $ctx->hexdigest();
493 + close($fh);
494 +
495 + } else {
496 + die("Can't read $dst_file: $!");
497 + }
498 +
499 + $self->assert($expected_md5 eq $md5,
500 + test_msg("Expected '$expected_md5', got '$md5'"));
501 + };
502 + if ($@) {
503 + $ex = $@;
504 + }
505 +
506 + test_cleanup($setup->{log_file}, $ex);
507 +}
508 +
509 sub scp_upload_abs_symlink {
510 my $self = shift;
511 my $tmpdir = $self->{tmpdir};
512 @@ -58019,7 +58189,8 @@ sub scp_ext_null_ptr_issue1043 {
513 '-oStrictHostKeyChecking=no',
514 '-oUserKnownHostsFile=/dev/null',
515 "$setup->{user}\@127.0.0.1",
516 - 'scp'
517 + 'scp',
518 + '-O'
519 );
520
521 my $scp_rh = IO::Handle->new();
522 @@ -58305,6 +58476,7 @@ sub scp_ext_upload_recursive_dir_bug3447 {
523 'scp',
524 '-r',
525 '-v',
526 + '-O',
527 '-oBatchMode=yes',
528 '-oCheckHostIP=no',
529 "-oPort=$port",
530 @@ -58606,6 +58778,7 @@ sub scp_ext_upload_recursive_dir_bug3792 {
531 '-r',
532 '-v',
533 '-p',
534 + '-O',
535 '-oBatchMode=yes',
536 '-oCheckHostIP=no',
537 "-oPort=$port",
538 @@ -58922,6 +59095,7 @@ sub scp_ext_upload_recursive_dir_bug4004 {
539 '-r',
540 '-p',
541 '-v',
542 + '-O',
543 '-oBatchMode=yes',
544 '-oCheckHostIP=no',
545 "-oPort=$port",
546 @@ -59183,6 +59357,7 @@ sub scp_ext_upload_recursive_dirs_bug4257 {
547 '-r',
548 '-p',
549 '-v',
550 + '-O',
551 '-oBatchMode=yes',
552 '-oCheckHostIP=no',
553 "-oPort=$port",
554 @@ -59407,6 +59582,7 @@ sub scp_ext_upload_different_name_bug3425 {
555 my @cmd = (
556 'scp',
557 '-v',
558 + '-O',
559 '-oBatchMode=yes',
560 '-oCheckHostIP=no',
561 "-oPort=$port",
562 @@ -59607,6 +59783,7 @@ sub scp_ext_upload_recursive_empty_dir {
563 'scp',
564 '-r',
565 '-v',
566 + '-O',
567 '-oBatchMode=yes',
568 '-oCheckHostIP=no',
569 "-oPort=$port",
570 @@ -59830,6 +60007,7 @@ sub scp_ext_upload_shorter_file_bug4013 {
571 my @cmd = (
572 'scp',
573 '-v',
574 + '-O',
575 '-oBatchMode=yes',
576 '-oCheckHostIP=no',
577 "-oPort=$port",
578 @@ -59928,22 +60106,7 @@ sub scp_ext_upload_shorter_file_bug4013 {
579 sub scp_ext_upload_file_with_timestamp_bug4026 {
580 my $self = shift;
581 my $tmpdir = $self->{tmpdir};
582 -
583 - my $config_file = "$tmpdir/sftp.conf";
584 - my $pid_file = File::Spec->rel2abs("$tmpdir/sftp.pid");
585 - my $scoreboard_file = File::Spec->rel2abs("$tmpdir/sftp.scoreboard");
586 -
587 - my $log_file = test_get_logfile();
588 -
589 - my $auth_user_file = File::Spec->rel2abs("$tmpdir/sftp.passwd");
590 - my $auth_group_file = File::Spec->rel2abs("$tmpdir/sftp.group");
591 -
592 - my $user = 'proftpd';
593 - my $passwd = 'test';
594 - my $group = 'ftpd';
595 - my $home_dir = File::Spec->rel2abs($tmpdir);
596 - my $uid = 500;
597 - my $gid = 500;
598 + my $setup = test_setup($tmpdir, 'scp');
599
600 my $sub_dir = File::Spec->rel2abs("$tmpdir/sub.d");
601 mkpath($sub_dir);
602 @@ -59951,19 +60114,15 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
603 # Make sure that, if we're running as root, that the home directory has
604 # permissions/privs set for the account we create
605 if ($< == 0) {
606 - unless (chmod(0755, $home_dir, $sub_dir)) {
607 - die("Can't set perms on $home_dir to 0755: $!");
608 + unless (chmod(0755, $sub_dir)) {
609 + die("Can't set perms on $sub_dir to 0755: $!");
610 }
611
612 - unless (chown($uid, $gid, $home_dir, $sub_dir)) {
613 - die("Can't set owner of $home_dir to $uid/$gid: $!");
614 + unless (chown($setup->{uid}, $setup->{gid}, $sub_dir)) {
615 + die("Can't set owner of $sub_dir to $setup->{uid}/$setup->{gid}: $!");
616 }
617 }
618
619 - auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
620 - '/bin/bash');
621 - auth_group_write($auth_group_file, $group, $gid, $user);
622 -
623 my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
624 my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
625
626 @@ -59994,18 +60153,18 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
627 my $dst_file = File::Spec->rel2abs("$sub_dir/dst.txt");
628
629 my $config = {
630 - PidFile => $pid_file,
631 - ScoreboardFile => $scoreboard_file,
632 - SystemLog => $log_file,
633 - TraceLog => $log_file,
634 - Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
635 + PidFile => $setup->{pid_file},
636 + ScoreboardFile => $setup->{scoreboard_file},
637 + SystemLog => $setup->{log_file},
638 + TraceLog => $setup->{log_file},
639 + Trace => 'ssh2:20 sftp:20 scp:20',
640
641 - AuthUserFile => $auth_user_file,
642 - AuthGroupFile => $auth_group_file,
643 + AuthUserFile => $setup->{auth_user_file},
644 + AuthGroupFile => $setup->{auth_group_file},
645 AuthOrder => 'mod_auth_file.c',
646
647 AllowOverwrite => 'on',
648 - DefaultRoot => '~',
649 +# DefaultRoot => '~',
650
651 IfModules => {
652 'mod_delay.c' => {
653 @@ -60014,7 +60173,7 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
654
655 'mod_sftp.c' => [
656 "SFTPEngine on",
657 - "SFTPLog $log_file",
658 + "SFTPLog $setup->{log_file}",
659 "SFTPHostKey $rsa_host_key",
660 "SFTPHostKey $dsa_host_key",
661 "SFTPAuthorizedUserKeys file:~/.authorized_keys",
662 @@ -60022,7 +60181,8 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
663 },
664 };
665
666 - my ($port, $config_user, $config_group) = config_write($config_file, $config);
667 + my ($port, $config_user, $config_group) = config_write($setup->{config_file},
668 + $config);
669
670 # Open pipes, for use between the parent and child processes. Specifically,
671 # the child will indicate when it's done with its test by writing a message
672 @@ -60046,8 +60206,9 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
673 eval {
674 my @cmd = (
675 'scp',
676 - '-v',
677 + '-vvv',
678 '-p',
679 + '-O',
680 '-oBatchMode=yes',
681 '-oCheckHostIP=no',
682 "-oPort=$port",
683 @@ -60056,7 +60217,7 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
684 '-oStrictHostKeyChecking=no',
685 '-oUserKnownHostsFile=/dev/null',
686 "$src_file",
687 - "$user\@127.0.0.1:sub.d/dst.txt",
688 + "$setup->{user}\@127.0.0.1:sub.d/dst.txt",
689 );
690
691 my $scp_rh = IO::Handle->new();
692 @@ -60102,6 +60263,10 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
693 $res = 1;
694 }
695
696 +if ($ENV{TEST_VERBOSE}) {
697 + print STDERR "# Errstr:\n$errstr\n";
698 +}
699 +
700 unless ($res == 0) {
701 die("Can't upload $src_file to server: $errstr");
702 }
703 @@ -60113,7 +60278,6 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
704 $self->assert($atime == 0, test_msg("Expected atime 0, got $atime"));
705 $self->assert($mtime == 0, test_msg("Expected mtime 0, got $mtime"));
706 };
707 -
708 if ($@) {
709 $ex = $@;
710 }
711 @@ -60122,7 +60286,7 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
712 $wfh->flush();
713
714 } else {
715 - eval { server_wait($config_file, $rfh) };
716 + eval { server_wait($setup->{config_file}, $rfh) };
717 if ($@) {
718 warn($@);
719 exit 1;
720 @@ -60132,18 +60296,10 @@ sub scp_ext_upload_file_with_timestamp_bug4026 {
721 }
722
723 # Stop server
724 - server_stop($pid_file);
725 -
726 + server_stop($setup->{pid_file});
727 $self->assert_child_ok($pid);
728
729 - if ($ex) {
730 - test_append_logfile($log_file, $ex);
731 - unlink($log_file);
732 -
733 - die($ex);
734 - }
735 -
736 - unlink($log_file);
737 + test_cleanup($setup->{log_file}, $ex);
738 }
739
740 sub scp_download {
741 @@ -60161,7 +60317,7 @@ sub scp_download {
742 ScoreboardFile => $setup->{scoreboard_file},
743 SystemLog => $setup->{log_file},
744 TraceLog => $setup->{log_file},
745 - Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
746 + Trace => 'ssh2:20 sftp:20 scp:20',
747
748 AuthUserFile => $setup->{auth_user_file},
749 AuthGroupFile => $setup->{auth_group_file},
750 @@ -61488,6 +61644,139 @@ sub scp_download_rel_symlink_chrooted_bug4219 {
751 test_cleanup($setup->{log_file}, $ex);
752 }
753
754 +sub scp_download_filename_with_spaces_issue1886 {
755 + my $self = shift;
756 + my $tmpdir = $self->{tmpdir};
757 + my $setup = test_setup($tmpdir, 'scp');
758 +
759 + my $src_file = File::Spec->rel2abs("$tmpdir/src.txt");
760 + if (open(my $fh, "> $src_file")) {
761 + print $fh "ABCDefgh" x 32;
762 + unless (close($fh)) {
763 + die("Can't write $src_file: $!");
764 + }
765 +
766 + } else {
767 + die("Can't open $src_file: $!");
768 + }
769 +
770 + # Calculate the MD5 checksum of this file, for comparison with the
771 + # downloaded file.
772 + my $ctx = Digest::MD5->new();
773 + my $expected_md5;
774 +
775 + if (open(my $fh, "< $src_file")) {
776 + binmode($fh);
777 + $ctx->addfile($fh);
778 + $expected_md5 = $ctx->hexdigest();
779 + close($fh);
780 +
781 + } else {
782 + die("Can't read $src_file: $!");
783 + }
784 +
785 + my $dst_file = File::Spec->rel2abs("$tmpdir/test filename with spaces.txt");
786 +
787 + my $rsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_rsa_key');
788 + my $dsa_host_key = File::Spec->rel2abs('t/etc/modules/mod_sftp/ssh_host_dsa_key');
789 +
790 + my $config = {
791 + PidFile => $setup->{pid_file},
792 + ScoreboardFile => $setup->{scoreboard_file},
793 + SystemLog => $setup->{log_file},
794 + TraceLog => $setup->{log_file},
795 + Trace => 'ssh2:10 scp:20',
796 +
797 + AuthUserFile => $setup->{auth_user_file},
798 + AuthGroupFile => $setup->{auth_group_file},
799 + AuthOrder => 'mod_auth_file.c',
800 +
801 + IfModules => {
802 + 'mod_delay.c' => {
803 + DelayEngine => 'off',
804 + },
805 +
806 + 'mod_sftp.c' => [
807 + "SFTPEngine on",
808 + "SFTPLog $setup->{log_file}",
809 + "SFTPHostKey $rsa_host_key",
810 + "SFTPHostKey $dsa_host_key",
811 + ],
812 + },
813 + };
814 +
815 + my ($port, $config_user, $config_group) = config_write($setup->{config_file},
816 + $config);
817 +
818 + # Open pipes, for use between the parent and child processes. Specifically,
819 + # the child will indicate when it's done with its test by writing a message
820 + # to the parent.
821 + my ($rfh, $wfh);
822 + unless (pipe($rfh, $wfh)) {
823 + die("Can't open pipe: $!");
824 + }
825 +
826 + require Net::SSH2;
827 +
828 + my $ex;
829 +
830 + # Ignore SIGPIPE
831 + local $SIG{PIPE} = sub { };
832 +
833 + # Fork child
834 + $self->handle_sigchld();
835 + defined(my $pid = fork()) or die("Can't fork: $!");
836 + if ($pid) {
837 + eval {
838 + my $ssh2 = Net::SSH2->new();
839 +
840 + sleep(1);
841 +
842 + unless ($ssh2->connect('127.0.0.1', $port)) {
843 + my ($err_code, $err_name, $err_str) = $ssh2->error();
844 + die("Can't connect to SSH2 server: [$err_name] ($err_code) $err_str");
845 + }
846 +
847 + unless ($ssh2->auth_password($setup->{user}, $setup->{passwd})) {
848 + my ($err_code, $err_name, $err_str) = $ssh2->error();
849 + die("Can't login to SSH2 server: [$err_name] ($err_code) $err_str");
850 + }
851 +
852 + my $res = $ssh2->scp_get('src.txt', $dst_file);
853 + unless ($res) {
854 + my ($err_code, $err_name, $err_str) = $ssh2->error();
855 + die("Can't download $src_file from server: [$err_name] ($err_code) $err_str");
856 + }
857 +
858 + $ssh2->disconnect();
859 +
860 + $self->assert(-f $dst_file,
861 + test_msg("File $dst_file file does not exist"));
862 + };
863 + if ($@) {
864 + $ex = $@;
865 + }
866 +
867 + $wfh->print("done\n");
868 + $wfh->flush();
869 +
870 + } else {
871 + eval { server_wait($setup->{config_file}, $rfh) };
872 + if ($@) {
873 + warn($@);
874 + exit 1;
875 + }
876 +
877 + exit 0;
878 + }
879 +
880 + # Stop server
881 + server_stop($setup->{pid_file});
882 + $self->assert_child_ok($pid);
883 +
884 + test_cleanup($setup->{log_file}, $ex);
885 +}
886 +
887 sub scp_ext_download_bug3544 {
888 my $self = shift;
889 my $tmpdir = $self->{tmpdir};
890 @@ -61545,7 +61834,7 @@ sub scp_ext_download_bug3544 {
891 ScoreboardFile => $scoreboard_file,
892 SystemLog => $log_file,
893 TraceLog => $log_file,
894 - Trace => 'DEFAULT:10 ssh2:20 sftp:20 scp:20',
895 + Trace => 'ssh2:20 sftp:20 scp:20',
896
897 AuthUserFile => $auth_user_file,
898 AuthGroupFile => $auth_group_file,
899 @@ -61562,6 +61851,7 @@ sub scp_ext_download_bug3544 {
900 "SFTPHostKey $rsa_host_key",
901 "SFTPHostKey $dsa_host_key",
902 "SFTPAuthorizedUserKeys file:~/.authorized_keys",
903 +'SFTPTrafficPolicy none',
904 ],
905 },
906 };
907 @@ -61591,6 +61881,7 @@ sub scp_ext_download_bug3544 {
908 my @cmd = (
909 'scp',
910 '-vvv',
911 + '-O',
912 '-oBatchMode=yes',
913 '-oCheckHostIP=no',
914 "-oPort=$port",
915 @@ -61789,6 +62080,7 @@ sub scp_ext_download_bug3798 {
916 my @cmd = (
917 'scp',
918 '-vvv',
919 + '-O',
920 '-oBatchMode=yes',
921 '-oCheckHostIP=no',
922 "-oPort=$port",
923 @@ -61988,6 +62280,7 @@ sub scp_ext_download_glob_single_match_bug3904 {
924 my @cmd = (
925 'scp',
926 '-vvv',
927 + '-O',
928 '-oBatchMode=yes',
929 '-oCheckHostIP=no',
930 "-oPort=$port",
931 @@ -62216,6 +62509,7 @@ sub scp_ext_download_glob_multiple_matches_bug3904 {
932 my @cmd = (
933 'scp',
934 '-vvv',
935 + '-O',
936 '-oBatchMode=yes',
937 '-oCheckHostIP=no',
938 "-oPort=$port",
939 @@ -62506,6 +62800,7 @@ sub scp_ext_download_recursive_dir_bug3456 {
940 'scp',
941 '-r',
942 '-vvv',
943 + '-O',
944 '-oBatchMode=yes',
945 '-oCheckHostIP=no',
946 "-oPort=$port",
947 @@ -62733,6 +63028,7 @@ sub scp_ext_download_recursive_empty_dir {
948 'scp',
949 '-r',
950 '-v',
951 + '-O',
952 '-oBatchMode=yes',
953 '-oCheckHostIP=no',
954 "-oPort=$port",
955 @@ -62935,6 +63231,7 @@ sub scp_ext_download_glob_no_matches_bug3935 {
956 my @cmd = (
957 'scp',
958 '-vvv',
959 + '-O',
960 '-oBatchMode=yes',
961 '-oCheckHostIP=no',
962 "-oPort=$port",