07d9aa13 |
1 | /* |
a673e210 |
2 | * scp.c - Scp (Secure Copy) client for PuTTY. |
3 | * Joris van Rantwijk, Simon Tatham |
07d9aa13 |
4 | * |
a673e210 |
5 | * This is mainly based on ssh-1.2.26/scp.c by Timo Rinne & Tatu Ylonen. |
6 | * They, in turn, used stuff from BSD rcp. |
7 | * |
8 | * (SGT, 2001-09-10: Joris van Rantwijk assures me that although |
9 | * this file as originally submitted was inspired by, and |
10 | * _structurally_ based on, ssh-1.2.26's scp.c, there wasn't any |
11 | * actual code duplicated, so the above comment shouldn't give rise |
12 | * to licensing issues.) |
07d9aa13 |
13 | */ |
14 | |
07d9aa13 |
15 | #include <stdlib.h> |
16 | #include <stdio.h> |
17 | #include <string.h> |
fd5e5847 |
18 | #include <limits.h> |
07d9aa13 |
19 | #include <time.h> |
feb7fdfe |
20 | #include <assert.h> |
07d9aa13 |
21 | |
22 | #define PUTTY_DO_GLOBALS |
23 | #include "putty.h" |
799dfcfa |
24 | #include "psftp.h" |
fd5e5847 |
25 | #include "ssh.h" |
26 | #include "sftp.h" |
a9422f39 |
27 | #include "storage.h" |
07d9aa13 |
28 | |
2bc6a386 |
29 | static int list = 0; |
fb09bf1c |
30 | static int verbose = 0; |
07d9aa13 |
31 | static int recursive = 0; |
32 | static int preserve = 0; |
33 | static int targetshouldbedirectory = 0; |
34 | static int statistics = 1; |
b1daf518 |
35 | static int prev_stats_len = 0; |
cd1f39ab |
36 | static int scp_unsafe_mode = 0; |
07d9aa13 |
37 | static int errs = 0; |
cc87246d |
38 | static int gui_mode = 0; |
728f4f4c |
39 | static int try_scp = 1; |
40 | static int try_sftp = 1; |
41 | static int main_cmd_is_sftp = 0; |
42 | static int fallback_cmd_is_sftp = 0; |
fd5e5847 |
43 | static int using_sftp = 0; |
07d9aa13 |
44 | |
6b78788a |
45 | static Backend *back; |
46 | static void *backhandle; |
3ea863a3 |
47 | static Config cfg; |
6b78788a |
48 | |
07d9aa13 |
49 | static void source(char *src); |
50 | static void rsource(char *src); |
ca2d5943 |
51 | static void sink(char *targ, char *src); |
07d9aa13 |
52 | |
5471d09a |
53 | /* |
54 | * The maximum amount of queued data we accept before we stop and |
55 | * wait for the server to process some. |
56 | */ |
57 | #define MAX_SCP_BUFSIZE 16384 |
58 | |
6b78788a |
59 | void ldisc_send(void *handle, char *buf, int len, int interactive) |
32874aea |
60 | { |
feb7fdfe |
61 | /* |
62 | * This is only here because of the calls to ldisc_send(NULL, |
63 | * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc |
64 | * as an ldisc. So if we get called with any real data, I want |
65 | * to know about it. |
66 | */ |
67 | assert(len == 0); |
68 | } |
69 | |
32874aea |
70 | static void tell_char(FILE * stream, char c) |
cc87246d |
71 | { |
72 | if (!gui_mode) |
73 | fputc(c, stream); |
799dfcfa |
74 | else |
75 | gui_send_char(stream == stderr, c); |
cc87246d |
76 | } |
77 | |
32874aea |
78 | static void tell_str(FILE * stream, char *str) |
cc87246d |
79 | { |
80 | unsigned int i; |
81 | |
32874aea |
82 | for (i = 0; i < strlen(str); ++i) |
cc87246d |
83 | tell_char(stream, str[i]); |
84 | } |
85 | |
32874aea |
86 | static void tell_user(FILE * stream, char *fmt, ...) |
cc87246d |
87 | { |
57356d63 |
88 | char *str, *str2; |
cc87246d |
89 | va_list ap; |
90 | va_start(ap, fmt); |
57356d63 |
91 | str = dupvprintf(fmt, ap); |
cc87246d |
92 | va_end(ap); |
57356d63 |
93 | str2 = dupcat(str, "\n", NULL); |
94 | sfree(str); |
95 | tell_str(stream, str2); |
96 | sfree(str2); |
cc87246d |
97 | } |
98 | |
fb09bf1c |
99 | /* |
07d9aa13 |
100 | * Print an error message and perform a fatal exit. |
101 | */ |
102 | void fatalbox(char *fmt, ...) |
103 | { |
57356d63 |
104 | char *str, *str2; |
c51a56e2 |
105 | va_list ap; |
106 | va_start(ap, fmt); |
57356d63 |
107 | str = dupvprintf(fmt, ap); |
108 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
109 | sfree(str); |
c51a56e2 |
110 | va_end(ap); |
57356d63 |
111 | tell_str(stderr, str2); |
112 | sfree(str2); |
2bc6a386 |
113 | errs++; |
114 | |
799dfcfa |
115 | if (gui_mode) |
116 | gui_send_errcount(list, errs); |
cc87246d |
117 | |
93b581bd |
118 | cleanup_exit(1); |
07d9aa13 |
119 | } |
1709795f |
120 | void modalfatalbox(char *fmt, ...) |
121 | { |
57356d63 |
122 | char *str, *str2; |
1709795f |
123 | va_list ap; |
124 | va_start(ap, fmt); |
57356d63 |
125 | str = dupvprintf(fmt, ap); |
126 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
127 | sfree(str); |
1709795f |
128 | va_end(ap); |
57356d63 |
129 | tell_str(stderr, str2); |
130 | sfree(str2); |
1709795f |
131 | errs++; |
132 | |
799dfcfa |
133 | if (gui_mode) |
134 | gui_send_errcount(list, errs); |
1709795f |
135 | |
136 | cleanup_exit(1); |
137 | } |
a8327734 |
138 | void connection_fatal(void *frontend, char *fmt, ...) |
8d5de777 |
139 | { |
57356d63 |
140 | char *str, *str2; |
8d5de777 |
141 | va_list ap; |
142 | va_start(ap, fmt); |
57356d63 |
143 | str = dupvprintf(fmt, ap); |
144 | str2 = dupcat("Fatal: ", str, "\n", NULL); |
145 | sfree(str); |
8d5de777 |
146 | va_end(ap); |
57356d63 |
147 | tell_str(stderr, str2); |
148 | sfree(str2); |
2bc6a386 |
149 | errs++; |
150 | |
799dfcfa |
151 | if (gui_mode) |
152 | gui_send_errcount(list, errs); |
8d5de777 |
153 | |
93b581bd |
154 | cleanup_exit(1); |
8d5de777 |
155 | } |
07d9aa13 |
156 | |
07d9aa13 |
157 | /* |
c44bf5bd |
158 | * In pscp, all agent requests should be synchronous, so this is a |
159 | * never-called stub. |
160 | */ |
161 | void agent_schedule_callback(void (*callback)(void *, void *, int), |
162 | void *callback_ctx, void *data, int len) |
163 | { |
164 | assert(!"We shouldn't be here"); |
165 | } |
166 | |
167 | /* |
3bdaf79d |
168 | * Receive a block of data from the SSH link. Block until all data |
169 | * is available. |
170 | * |
171 | * To do this, we repeatedly call the SSH protocol module, with our |
fe50e814 |
172 | * own trap in from_backend() to catch the data that comes back. We |
173 | * do this until we have enough data. |
3bdaf79d |
174 | */ |
8df7a775 |
175 | |
32874aea |
176 | static unsigned char *outptr; /* where to put the data */ |
177 | static unsigned outlen; /* how much data required */ |
3bdaf79d |
178 | static unsigned char *pending = NULL; /* any spare data */ |
32874aea |
179 | static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */ |
9fab77dc |
180 | int from_backend(void *frontend, int is_stderr, const char *data, int datalen) |
32874aea |
181 | { |
182 | unsigned char *p = (unsigned char *) data; |
183 | unsigned len = (unsigned) datalen; |
fe50e814 |
184 | |
3bdaf79d |
185 | /* |
fe50e814 |
186 | * stderr data is just spouted to local stderr and otherwise |
187 | * ignored. |
3bdaf79d |
188 | */ |
fe50e814 |
189 | if (is_stderr) { |
bfa5400d |
190 | if (len > 0) |
191 | fwrite(data, 1, len, stderr); |
5471d09a |
192 | return 0; |
fe50e814 |
193 | } |
3bdaf79d |
194 | |
3bdaf79d |
195 | /* |
196 | * If this is before the real session begins, just return. |
197 | */ |
198 | if (!outptr) |
5471d09a |
199 | return 0; |
3bdaf79d |
200 | |
bfa5400d |
201 | if ((outlen > 0) && (len > 0)) { |
32874aea |
202 | unsigned used = outlen; |
203 | if (used > len) |
204 | used = len; |
205 | memcpy(outptr, p, used); |
206 | outptr += used; |
207 | outlen -= used; |
208 | p += used; |
209 | len -= used; |
3bdaf79d |
210 | } |
211 | |
212 | if (len > 0) { |
32874aea |
213 | if (pendsize < pendlen + len) { |
214 | pendsize = pendlen + len + 4096; |
3d88e64d |
215 | pending = sresize(pending, pendsize, unsigned char); |
32874aea |
216 | if (!pending) |
217 | fatalbox("Out of memory"); |
218 | } |
219 | memcpy(pending + pendlen, p, len); |
220 | pendlen += len; |
3bdaf79d |
221 | } |
5471d09a |
222 | |
223 | return 0; |
224 | } |
32874aea |
225 | static int ssh_scp_recv(unsigned char *buf, int len) |
226 | { |
3bdaf79d |
227 | outptr = buf; |
228 | outlen = len; |
229 | |
230 | /* |
231 | * See if the pending-input block contains some of what we |
232 | * need. |
233 | */ |
234 | if (pendlen > 0) { |
32874aea |
235 | unsigned pendused = pendlen; |
236 | if (pendused > outlen) |
237 | pendused = outlen; |
3bdaf79d |
238 | memcpy(outptr, pending, pendused); |
32874aea |
239 | memmove(pending, pending + pendused, pendlen - pendused); |
3bdaf79d |
240 | outptr += pendused; |
241 | outlen -= pendused; |
32874aea |
242 | pendlen -= pendused; |
243 | if (pendlen == 0) { |
244 | pendsize = 0; |
245 | sfree(pending); |
246 | pending = NULL; |
247 | } |
248 | if (outlen == 0) |
249 | return len; |
3bdaf79d |
250 | } |
251 | |
252 | while (outlen > 0) { |
799dfcfa |
253 | if (ssh_sftp_loop_iteration() < 0) |
32874aea |
254 | return 0; /* doom */ |
3bdaf79d |
255 | } |
256 | |
257 | return len; |
258 | } |
259 | |
260 | /* |
261 | * Loop through the ssh connection and authentication process. |
262 | */ |
32874aea |
263 | static void ssh_scp_init(void) |
264 | { |
51470298 |
265 | while (!back->sendok(backhandle)) { |
799dfcfa |
266 | if (ssh_sftp_loop_iteration() < 0) |
32874aea |
267 | return; /* doom */ |
3bdaf79d |
268 | } |
728f4f4c |
269 | |
270 | /* Work out which backend we ended up using. */ |
271 | if (!ssh_fallback_cmd(backhandle)) |
272 | using_sftp = main_cmd_is_sftp; |
273 | else |
274 | using_sftp = fallback_cmd_is_sftp; |
275 | |
dc4a1fdd |
276 | if (verbose) { |
277 | if (using_sftp) |
278 | tell_user(stderr, "Using SFTP"); |
279 | else |
280 | tell_user(stderr, "Using SCP1"); |
281 | } |
3bdaf79d |
282 | } |
283 | |
284 | /* |
07d9aa13 |
285 | * Print an error message and exit after closing the SSH link. |
286 | */ |
287 | static void bump(char *fmt, ...) |
288 | { |
57356d63 |
289 | char *str, *str2; |
c51a56e2 |
290 | va_list ap; |
291 | va_start(ap, fmt); |
57356d63 |
292 | str = dupvprintf(fmt, ap); |
c51a56e2 |
293 | va_end(ap); |
57356d63 |
294 | str2 = dupcat(str, "\n", NULL); |
295 | sfree(str); |
296 | tell_str(stderr, str2); |
297 | sfree(str2); |
2bc6a386 |
298 | errs++; |
cc87246d |
299 | |
51470298 |
300 | if (back != NULL && back->socket(backhandle) != NULL) { |
c51a56e2 |
301 | char ch; |
51470298 |
302 | back->special(backhandle, TS_EOF); |
776792d7 |
303 | ssh_scp_recv((unsigned char *) &ch, 1); |
c51a56e2 |
304 | } |
2bc6a386 |
305 | |
799dfcfa |
306 | if (gui_mode) |
307 | gui_send_errcount(list, errs); |
2bc6a386 |
308 | |
93b581bd |
309 | cleanup_exit(1); |
07d9aa13 |
310 | } |
311 | |
07d9aa13 |
312 | /* |
313 | * Open an SSH connection to user@host and execute cmd. |
314 | */ |
315 | static void do_cmd(char *host, char *user, char *cmd) |
316 | { |
cbe2d68f |
317 | const char *err; |
318 | char *realhost; |
799dfcfa |
319 | void *logctx; |
c51a56e2 |
320 | |
321 | if (host == NULL || host[0] == '\0') |
322 | bump("Empty host name"); |
323 | |
324 | /* Try to load settings for this host */ |
a9422f39 |
325 | do_defaults(host, &cfg); |
c51a56e2 |
326 | if (cfg.host[0] == '\0') { |
327 | /* No settings for this host; use defaults */ |
32874aea |
328 | do_defaults(NULL, &cfg); |
329 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
330 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
4db4f6a6 |
331 | } |
332 | |
333 | /* |
334 | * Force use of SSH. (If they got the protocol wrong we assume the |
335 | * port is useless too.) |
336 | */ |
337 | if (cfg.protocol != PROT_SSH) { |
338 | cfg.protocol = PROT_SSH; |
339 | cfg.port = 22; |
c51a56e2 |
340 | } |
341 | |
449925a6 |
342 | /* |
c0a81592 |
343 | * Enact command-line overrides. |
344 | */ |
5555d393 |
345 | cmdline_run_saved(&cfg); |
c0a81592 |
346 | |
347 | /* |
449925a6 |
348 | * Trim leading whitespace off the hostname if it's there. |
349 | */ |
350 | { |
351 | int space = strspn(cfg.host, " \t"); |
352 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
353 | } |
354 | |
355 | /* See if host is of the form user@host */ |
356 | if (cfg.host[0] != '\0') { |
357 | char *atsign = strchr(cfg.host, '@'); |
358 | /* Make sure we're not overflowing the user field */ |
359 | if (atsign) { |
360 | if (atsign - cfg.host < sizeof cfg.username) { |
361 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
362 | cfg.username[atsign - cfg.host] = '\0'; |
363 | } |
364 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
365 | } |
366 | } |
367 | |
368 | /* |
369 | * Trim a colon suffix off the hostname if it's there. |
370 | */ |
371 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
372 | |
cae0c023 |
373 | /* |
374 | * Remove any remaining whitespace from the hostname. |
375 | */ |
376 | { |
377 | int p1 = 0, p2 = 0; |
378 | while (cfg.host[p2] != '\0') { |
379 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
380 | cfg.host[p1] = cfg.host[p2]; |
381 | p1++; |
382 | } |
383 | p2++; |
384 | } |
385 | cfg.host[p1] = '\0'; |
386 | } |
387 | |
c51a56e2 |
388 | /* Set username */ |
389 | if (user != NULL && user[0] != '\0') { |
32874aea |
390 | strncpy(cfg.username, user, sizeof(cfg.username) - 1); |
391 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
c51a56e2 |
392 | } else if (cfg.username[0] == '\0') { |
799dfcfa |
393 | user = get_username(); |
394 | if (!user) |
f5e6a5c6 |
395 | bump("Empty user name"); |
799dfcfa |
396 | else { |
397 | if (verbose) |
398 | tell_user(stderr, "Guessing user name: %s", user); |
399 | strncpy(cfg.username, user, sizeof(cfg.username) - 1); |
400 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
401 | sfree(user); |
402 | } |
c51a56e2 |
403 | } |
404 | |
fd5e5847 |
405 | /* |
d27b4a18 |
406 | * Disable scary things which shouldn't be enabled for simple |
407 | * things like SCP and SFTP: agent forwarding, port forwarding, |
408 | * X forwarding. |
409 | */ |
410 | cfg.x11_forward = 0; |
411 | cfg.agentfwd = 0; |
412 | cfg.portfwd[0] = cfg.portfwd[1] = '\0'; |
413 | |
414 | /* |
728f4f4c |
415 | * Set up main and possibly fallback command depending on |
416 | * options specified by user. |
fd5e5847 |
417 | * Attempt to start the SFTP subsystem as a first choice, |
418 | * falling back to the provided scp command if that fails. |
419 | */ |
728f4f4c |
420 | cfg.remote_cmd_ptr2 = NULL; |
421 | if (try_sftp) { |
422 | /* First choice is SFTP subsystem. */ |
423 | main_cmd_is_sftp = 1; |
424 | strcpy(cfg.remote_cmd, "sftp"); |
425 | cfg.ssh_subsys = TRUE; |
426 | if (try_scp) { |
427 | /* Fallback is to use the provided scp command. */ |
428 | fallback_cmd_is_sftp = 0; |
429 | cfg.remote_cmd_ptr2 = cmd; |
430 | cfg.ssh_subsys2 = FALSE; |
431 | } else { |
432 | /* Since we're not going to try SCP, we may as well try |
433 | * harder to find an SFTP server, since in the current |
434 | * implementation we have a spare slot. */ |
435 | fallback_cmd_is_sftp = 1; |
436 | /* see psftp.c for full explanation of this kludge */ |
437 | cfg.remote_cmd_ptr2 = |
438 | "test -x /usr/lib/sftp-server && exec /usr/lib/sftp-server\n" |
439 | "test -x /usr/local/lib/sftp-server && exec /usr/local/lib/sftp-server\n" |
440 | "exec sftp-server"; |
441 | cfg.ssh_subsys2 = FALSE; |
442 | } |
443 | } else { |
444 | /* Don't try SFTP at all; just try the scp command. */ |
445 | main_cmd_is_sftp = 0; |
446 | cfg.remote_cmd_ptr = cmd; |
447 | cfg.ssh_subsys = FALSE; |
448 | } |
3bdaf79d |
449 | cfg.nopty = TRUE; |
450 | |
451 | back = &ssh_backend; |
452 | |
86916870 |
453 | err = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, &realhost,0); |
c51a56e2 |
454 | if (err != NULL) |
455 | bump("ssh_init: %s", err); |
c229ef97 |
456 | logctx = log_init(NULL, &cfg); |
a8327734 |
457 | back->provide_logctx(backhandle, logctx); |
d3fef4a5 |
458 | console_provide_logctx(logctx); |
3bdaf79d |
459 | ssh_scp_init(); |
c51a56e2 |
460 | if (verbose && realhost != NULL) |
cc87246d |
461 | tell_user(stderr, "Connected to %s\n", realhost); |
6e1ebb76 |
462 | sfree(realhost); |
07d9aa13 |
463 | } |
464 | |
07d9aa13 |
465 | /* |
466 | * Update statistic information about current file. |
467 | */ |
468 | static void print_stats(char *name, unsigned long size, unsigned long done, |
32874aea |
469 | time_t start, time_t now) |
07d9aa13 |
470 | { |
c51a56e2 |
471 | float ratebs; |
472 | unsigned long eta; |
473 | char etastr[10]; |
474 | int pct; |
b1daf518 |
475 | int len; |
d524be1c |
476 | int elap; |
c51a56e2 |
477 | |
d524be1c |
478 | elap = (unsigned long) difftime(now, start); |
c51a56e2 |
479 | |
d524be1c |
480 | if (now > start) |
481 | ratebs = (float) done / elap; |
482 | else |
483 | ratebs = (float) done; |
484 | |
485 | if (ratebs < 1.0) |
486 | eta = size - done; |
487 | else |
488 | eta = (unsigned long) ((size - done) / ratebs); |
489 | sprintf(etastr, "%02ld:%02ld:%02ld", |
490 | eta / 3600, (eta % 3600) / 60, eta % 60); |
c51a56e2 |
491 | |
d524be1c |
492 | pct = (int) (100 * (done * 1.0 / size)); |
c51a56e2 |
493 | |
799dfcfa |
494 | if (gui_mode) { |
776792d7 |
495 | gui_update_stats(name, size, pct, elap, done, eta, |
d524be1c |
496 | (unsigned long) ratebs); |
799dfcfa |
497 | } else { |
b1daf518 |
498 | len = printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%", |
499 | name, done / 1024, ratebs / 1024.0, etastr, pct); |
500 | if (len < prev_stats_len) |
501 | printf("%*s", prev_stats_len - len, ""); |
502 | prev_stats_len = len; |
c51a56e2 |
503 | |
cc87246d |
504 | if (done == size) |
505 | printf("\n"); |
df163066 |
506 | |
507 | fflush(stdout); |
cc87246d |
508 | } |
07d9aa13 |
509 | } |
510 | |
07d9aa13 |
511 | /* |
512 | * Find a colon in str and return a pointer to the colon. |
39ddf0ff |
513 | * This is used to separate hostname from filename. |
07d9aa13 |
514 | */ |
32874aea |
515 | static char *colon(char *str) |
07d9aa13 |
516 | { |
c51a56e2 |
517 | /* We ignore a leading colon, since the hostname cannot be |
32874aea |
518 | empty. We also ignore a colon as second character because |
519 | of filenames like f:myfile.txt. */ |
520 | if (str[0] == '\0' || str[0] == ':' || str[1] == ':') |
c51a56e2 |
521 | return (NULL); |
32874aea |
522 | while (*str != '\0' && *str != ':' && *str != '/' && *str != '\\') |
c51a56e2 |
523 | str++; |
524 | if (*str == ':') |
525 | return (str); |
526 | else |
527 | return (NULL); |
07d9aa13 |
528 | } |
529 | |
07d9aa13 |
530 | /* |
03f64569 |
531 | * Return a pointer to the portion of str that comes after the last |
b3dcd9b2 |
532 | * slash (or backslash or colon, if `local' is TRUE). |
03f64569 |
533 | */ |
4eb24e3a |
534 | static char *stripslashes(char *str, int local) |
03f64569 |
535 | { |
536 | char *p; |
537 | |
b3dcd9b2 |
538 | if (local) { |
539 | p = strchr(str, ':'); |
540 | if (p) str = p+1; |
541 | } |
542 | |
03f64569 |
543 | p = strrchr(str, '/'); |
544 | if (p) str = p+1; |
545 | |
4eb24e3a |
546 | if (local) { |
547 | p = strrchr(str, '\\'); |
548 | if (p) str = p+1; |
549 | } |
03f64569 |
550 | |
551 | return str; |
552 | } |
553 | |
554 | /* |
fd5e5847 |
555 | * Determine whether a string is entirely composed of dots. |
556 | */ |
557 | static int is_dots(char *str) |
558 | { |
559 | return str[strspn(str, ".")] == '\0'; |
560 | } |
561 | |
562 | /* |
07d9aa13 |
563 | * Wait for a response from the other side. |
564 | * Return 0 if ok, -1 if error. |
565 | */ |
566 | static int response(void) |
567 | { |
c51a56e2 |
568 | char ch, resp, rbuf[2048]; |
569 | int p; |
570 | |
776792d7 |
571 | if (ssh_scp_recv((unsigned char *) &resp, 1) <= 0) |
c51a56e2 |
572 | bump("Lost connection"); |
573 | |
574 | p = 0; |
575 | switch (resp) { |
32874aea |
576 | case 0: /* ok */ |
c51a56e2 |
577 | return (0); |
578 | default: |
579 | rbuf[p++] = resp; |
580 | /* fallthrough */ |
32874aea |
581 | case 1: /* error */ |
582 | case 2: /* fatal error */ |
c51a56e2 |
583 | do { |
776792d7 |
584 | if (ssh_scp_recv((unsigned char *) &ch, 1) <= 0) |
c51a56e2 |
585 | bump("Protocol error: Lost connection"); |
586 | rbuf[p++] = ch; |
587 | } while (p < sizeof(rbuf) && ch != '\n'); |
32874aea |
588 | rbuf[p - 1] = '\0'; |
c51a56e2 |
589 | if (resp == 1) |
cc87246d |
590 | tell_user(stderr, "%s\n", rbuf); |
c51a56e2 |
591 | else |
592 | bump("%s", rbuf); |
593 | errs++; |
594 | return (-1); |
595 | } |
07d9aa13 |
596 | } |
597 | |
fd5e5847 |
598 | int sftp_recvdata(char *buf, int len) |
599 | { |
776792d7 |
600 | return ssh_scp_recv((unsigned char *) buf, len); |
fd5e5847 |
601 | } |
602 | int sftp_senddata(char *buf, int len) |
603 | { |
776792d7 |
604 | back->send(backhandle, buf, len); |
fd5e5847 |
605 | return 1; |
606 | } |
607 | |
608 | /* ---------------------------------------------------------------------- |
609 | * sftp-based replacement for the hacky `pscp -ls'. |
610 | */ |
611 | static int sftp_ls_compare(const void *av, const void *bv) |
612 | { |
613 | const struct fxp_name *a = (const struct fxp_name *) av; |
614 | const struct fxp_name *b = (const struct fxp_name *) bv; |
615 | return strcmp(a->filename, b->filename); |
616 | } |
617 | void scp_sftp_listdir(char *dirname) |
618 | { |
619 | struct fxp_handle *dirh; |
620 | struct fxp_names *names; |
621 | struct fxp_name *ournames; |
1bc24185 |
622 | struct sftp_packet *pktin; |
623 | struct sftp_request *req, *rreq; |
fd5e5847 |
624 | int nnames, namesize; |
fd5e5847 |
625 | int i; |
626 | |
9acdecb3 |
627 | if (!fxp_init()) { |
628 | tell_user(stderr, "unable to initialise SFTP: %s", fxp_error()); |
629 | errs++; |
630 | return; |
631 | } |
632 | |
fd5e5847 |
633 | printf("Listing directory %s\n", dirname); |
634 | |
1bc24185 |
635 | sftp_register(req = fxp_opendir_send(dirname)); |
636 | rreq = sftp_find_request(pktin = sftp_recv()); |
637 | assert(rreq == req); |
7b7de4f4 |
638 | dirh = fxp_opendir_recv(pktin, rreq); |
1bc24185 |
639 | |
fd5e5847 |
640 | if (dirh == NULL) { |
cdcbdf3b |
641 | printf("Unable to open %s: %s\n", dirname, fxp_error()); |
fd5e5847 |
642 | } else { |
643 | nnames = namesize = 0; |
644 | ournames = NULL; |
645 | |
646 | while (1) { |
647 | |
1bc24185 |
648 | sftp_register(req = fxp_readdir_send(dirh)); |
649 | rreq = sftp_find_request(pktin = sftp_recv()); |
650 | assert(rreq == req); |
7b7de4f4 |
651 | names = fxp_readdir_recv(pktin, rreq); |
1bc24185 |
652 | |
fd5e5847 |
653 | if (names == NULL) { |
654 | if (fxp_error_type() == SSH_FX_EOF) |
655 | break; |
cdcbdf3b |
656 | printf("Reading directory %s: %s\n", dirname, fxp_error()); |
fd5e5847 |
657 | break; |
658 | } |
659 | if (names->nnames == 0) { |
660 | fxp_free_names(names); |
661 | break; |
662 | } |
663 | |
664 | if (nnames + names->nnames >= namesize) { |
665 | namesize += names->nnames + 128; |
3d88e64d |
666 | ournames = sresize(ournames, namesize, struct fxp_name); |
fd5e5847 |
667 | } |
668 | |
669 | for (i = 0; i < names->nnames; i++) |
670 | ournames[nnames++] = names->names[i]; |
671 | |
672 | names->nnames = 0; /* prevent free_names */ |
673 | fxp_free_names(names); |
674 | } |
1bc24185 |
675 | sftp_register(req = fxp_close_send(dirh)); |
676 | rreq = sftp_find_request(pktin = sftp_recv()); |
677 | assert(rreq == req); |
7b7de4f4 |
678 | fxp_close_recv(pktin, rreq); |
fd5e5847 |
679 | |
680 | /* |
681 | * Now we have our filenames. Sort them by actual file |
682 | * name, and then output the longname parts. |
683 | */ |
684 | qsort(ournames, nnames, sizeof(*ournames), sftp_ls_compare); |
685 | |
686 | /* |
687 | * And print them. |
688 | */ |
689 | for (i = 0; i < nnames; i++) |
690 | printf("%s\n", ournames[i].longname); |
691 | } |
692 | } |
693 | |
120e4b40 |
694 | /* ---------------------------------------------------------------------- |
695 | * Helper routines that contain the actual SCP protocol elements, |
fd5e5847 |
696 | * implemented both as SCP1 and SFTP. |
120e4b40 |
697 | */ |
698 | |
fd5e5847 |
699 | static struct scp_sftp_dirstack { |
700 | struct scp_sftp_dirstack *next; |
701 | struct fxp_name *names; |
702 | int namepos, namelen; |
703 | char *dirpath; |
4eb24e3a |
704 | char *wildcard; |
825ec8ee |
705 | int matched_something; /* wildcard match set was non-empty */ |
fd5e5847 |
706 | } *scp_sftp_dirstack_head; |
707 | static char *scp_sftp_remotepath, *scp_sftp_currentname; |
4eb24e3a |
708 | static char *scp_sftp_wildcard; |
fd5e5847 |
709 | static int scp_sftp_targetisdir, scp_sftp_donethistarget; |
710 | static int scp_sftp_preserve, scp_sftp_recursive; |
711 | static unsigned long scp_sftp_mtime, scp_sftp_atime; |
712 | static int scp_has_times; |
713 | static struct fxp_handle *scp_sftp_filehandle; |
7fd264b2 |
714 | static struct fxp_xfer *scp_sftp_xfer; |
fd5e5847 |
715 | static uint64 scp_sftp_fileoffset; |
716 | |
717 | void scp_source_setup(char *target, int shouldbedir) |
718 | { |
719 | if (using_sftp) { |
720 | /* |
721 | * Find out whether the target filespec is in fact a |
722 | * directory. |
723 | */ |
1bc24185 |
724 | struct sftp_packet *pktin; |
725 | struct sftp_request *req, *rreq; |
fd5e5847 |
726 | struct fxp_attrs attrs; |
1bc24185 |
727 | int ret; |
fd5e5847 |
728 | |
02105c79 |
729 | if (!fxp_init()) { |
730 | tell_user(stderr, "unable to initialise SFTP: %s", fxp_error()); |
731 | errs++; |
51470298 |
732 | return; |
02105c79 |
733 | } |
734 | |
1bc24185 |
735 | sftp_register(req = fxp_stat_send(target)); |
736 | rreq = sftp_find_request(pktin = sftp_recv()); |
737 | assert(rreq == req); |
7b7de4f4 |
738 | ret = fxp_stat_recv(pktin, rreq, &attrs); |
1bc24185 |
739 | |
740 | if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) |
fd5e5847 |
741 | scp_sftp_targetisdir = 0; |
742 | else |
743 | scp_sftp_targetisdir = (attrs.permissions & 0040000) != 0; |
744 | |
745 | if (shouldbedir && !scp_sftp_targetisdir) { |
746 | bump("pscp: remote filespec %s: not a directory\n", target); |
747 | } |
748 | |
749 | scp_sftp_remotepath = dupstr(target); |
750 | |
751 | scp_has_times = 0; |
752 | } else { |
753 | (void) response(); |
754 | } |
755 | } |
756 | |
120e4b40 |
757 | int scp_send_errmsg(char *str) |
758 | { |
fd5e5847 |
759 | if (using_sftp) { |
760 | /* do nothing; we never need to send our errors to the server */ |
761 | } else { |
51470298 |
762 | back->send(backhandle, "\001", 1);/* scp protocol error prefix */ |
763 | back->send(backhandle, str, strlen(str)); |
fd5e5847 |
764 | } |
120e4b40 |
765 | return 0; /* can't fail */ |
766 | } |
767 | |
768 | int scp_send_filetimes(unsigned long mtime, unsigned long atime) |
769 | { |
fd5e5847 |
770 | if (using_sftp) { |
771 | scp_sftp_mtime = mtime; |
772 | scp_sftp_atime = atime; |
773 | scp_has_times = 1; |
774 | return 0; |
775 | } else { |
776 | char buf[80]; |
777 | sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime); |
51470298 |
778 | back->send(backhandle, buf, strlen(buf)); |
fd5e5847 |
779 | return response(); |
780 | } |
120e4b40 |
781 | } |
782 | |
783 | int scp_send_filename(char *name, unsigned long size, int modes) |
784 | { |
fd5e5847 |
785 | if (using_sftp) { |
786 | char *fullname; |
1bc24185 |
787 | struct sftp_packet *pktin; |
788 | struct sftp_request *req, *rreq; |
789 | |
fd5e5847 |
790 | if (scp_sftp_targetisdir) { |
791 | fullname = dupcat(scp_sftp_remotepath, "/", name, NULL); |
792 | } else { |
793 | fullname = dupstr(scp_sftp_remotepath); |
794 | } |
1bc24185 |
795 | |
796 | sftp_register(req = fxp_open_send(fullname, SSH_FXF_WRITE | |
797 | SSH_FXF_CREAT | SSH_FXF_TRUNC)); |
798 | rreq = sftp_find_request(pktin = sftp_recv()); |
799 | assert(rreq == req); |
7b7de4f4 |
800 | scp_sftp_filehandle = fxp_open_recv(pktin, rreq); |
1bc24185 |
801 | |
fd5e5847 |
802 | if (!scp_sftp_filehandle) { |
803 | tell_user(stderr, "pscp: unable to open %s: %s", |
804 | fullname, fxp_error()); |
805 | errs++; |
806 | return 1; |
807 | } |
808 | scp_sftp_fileoffset = uint64_make(0, 0); |
7fd264b2 |
809 | scp_sftp_xfer = xfer_upload_init(scp_sftp_filehandle, |
810 | scp_sftp_fileoffset); |
fd5e5847 |
811 | sfree(fullname); |
812 | return 0; |
813 | } else { |
814 | char buf[40]; |
815 | sprintf(buf, "C%04o %lu ", modes, size); |
51470298 |
816 | back->send(backhandle, buf, strlen(buf)); |
817 | back->send(backhandle, name, strlen(name)); |
818 | back->send(backhandle, "\n", 1); |
fd5e5847 |
819 | return response(); |
820 | } |
120e4b40 |
821 | } |
822 | |
823 | int scp_send_filedata(char *data, int len) |
824 | { |
fd5e5847 |
825 | if (using_sftp) { |
1bc24185 |
826 | int ret; |
827 | struct sftp_packet *pktin; |
1bc24185 |
828 | |
fd5e5847 |
829 | if (!scp_sftp_filehandle) { |
830 | return 1; |
831 | } |
1bc24185 |
832 | |
7fd264b2 |
833 | while (!xfer_upload_ready(scp_sftp_xfer)) { |
834 | pktin = sftp_recv(); |
835 | ret = xfer_upload_gotpkt(scp_sftp_xfer, pktin); |
836 | if (!ret) { |
837 | tell_user(stderr, "error while writing: %s\n", fxp_error()); |
838 | errs++; |
839 | return 1; |
840 | } |
fd5e5847 |
841 | } |
7fd264b2 |
842 | |
843 | xfer_upload_data(scp_sftp_xfer, data, len); |
844 | |
fd5e5847 |
845 | scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, len); |
846 | return 0; |
847 | } else { |
51470298 |
848 | int bufsize = back->send(backhandle, data, len); |
120e4b40 |
849 | |
fd5e5847 |
850 | /* |
851 | * If the network transfer is backing up - that is, the |
852 | * remote site is not accepting data as fast as we can |
853 | * produce it - then we must loop on network events until |
854 | * we have space in the buffer again. |
855 | */ |
856 | while (bufsize > MAX_SCP_BUFSIZE) { |
799dfcfa |
857 | if (ssh_sftp_loop_iteration() < 0) |
fd5e5847 |
858 | return 1; |
51470298 |
859 | bufsize = back->sendbuffer(backhandle); |
fd5e5847 |
860 | } |
861 | |
862 | return 0; |
863 | } |
864 | } |
865 | |
866 | int scp_send_finish(void) |
867 | { |
868 | if (using_sftp) { |
869 | struct fxp_attrs attrs; |
1bc24185 |
870 | struct sftp_packet *pktin; |
871 | struct sftp_request *req, *rreq; |
872 | int ret; |
873 | |
7fd264b2 |
874 | while (!xfer_done(scp_sftp_xfer)) { |
875 | pktin = sftp_recv(); |
876 | xfer_upload_gotpkt(scp_sftp_xfer, pktin); |
877 | } |
878 | xfer_cleanup(scp_sftp_xfer); |
879 | |
fd5e5847 |
880 | if (!scp_sftp_filehandle) { |
120e4b40 |
881 | return 1; |
fd5e5847 |
882 | } |
883 | if (scp_has_times) { |
884 | attrs.flags = SSH_FILEXFER_ATTR_ACMODTIME; |
885 | attrs.atime = scp_sftp_atime; |
886 | attrs.mtime = scp_sftp_mtime; |
1bc24185 |
887 | sftp_register(req = fxp_fsetstat_send(scp_sftp_filehandle, attrs)); |
888 | rreq = sftp_find_request(pktin = sftp_recv()); |
889 | assert(rreq == req); |
7b7de4f4 |
890 | ret = fxp_fsetstat_recv(pktin, rreq); |
1bc24185 |
891 | if (!ret) { |
fd5e5847 |
892 | tell_user(stderr, "unable to set file times: %s\n", fxp_error()); |
893 | errs++; |
894 | } |
895 | } |
1bc24185 |
896 | sftp_register(req = fxp_close_send(scp_sftp_filehandle)); |
897 | rreq = sftp_find_request(pktin = sftp_recv()); |
898 | assert(rreq == req); |
7b7de4f4 |
899 | fxp_close_recv(pktin, rreq); |
fd5e5847 |
900 | scp_has_times = 0; |
901 | return 0; |
902 | } else { |
51470298 |
903 | back->send(backhandle, "", 1); |
fd5e5847 |
904 | return response(); |
120e4b40 |
905 | } |
fd5e5847 |
906 | } |
120e4b40 |
907 | |
fd5e5847 |
908 | char *scp_save_remotepath(void) |
909 | { |
910 | if (using_sftp) |
911 | return scp_sftp_remotepath; |
912 | else |
913 | return NULL; |
120e4b40 |
914 | } |
915 | |
fd5e5847 |
916 | void scp_restore_remotepath(char *data) |
120e4b40 |
917 | { |
fd5e5847 |
918 | if (using_sftp) |
919 | scp_sftp_remotepath = data; |
120e4b40 |
920 | } |
921 | |
922 | int scp_send_dirname(char *name, int modes) |
923 | { |
fd5e5847 |
924 | if (using_sftp) { |
925 | char *fullname; |
926 | char const *err; |
927 | struct fxp_attrs attrs; |
1bc24185 |
928 | struct sftp_packet *pktin; |
929 | struct sftp_request *req, *rreq; |
930 | int ret; |
931 | |
fd5e5847 |
932 | if (scp_sftp_targetisdir) { |
933 | fullname = dupcat(scp_sftp_remotepath, "/", name, NULL); |
934 | } else { |
935 | fullname = dupstr(scp_sftp_remotepath); |
936 | } |
937 | |
938 | /* |
939 | * We don't worry about whether we managed to create the |
940 | * directory, because if it exists already it's OK just to |
941 | * use it. Instead, we will stat it afterwards, and if it |
942 | * exists and is a directory we will assume we were either |
943 | * successful or it didn't matter. |
944 | */ |
1bc24185 |
945 | sftp_register(req = fxp_mkdir_send(fullname)); |
946 | rreq = sftp_find_request(pktin = sftp_recv()); |
947 | assert(rreq == req); |
7b7de4f4 |
948 | ret = fxp_mkdir_recv(pktin, rreq); |
1bc24185 |
949 | |
950 | if (!ret) |
fd5e5847 |
951 | err = fxp_error(); |
952 | else |
953 | err = "server reported no error"; |
1bc24185 |
954 | |
955 | sftp_register(req = fxp_stat_send(fullname)); |
956 | rreq = sftp_find_request(pktin = sftp_recv()); |
957 | assert(rreq == req); |
7b7de4f4 |
958 | ret = fxp_stat_recv(pktin, rreq, &attrs); |
1bc24185 |
959 | |
960 | if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS) || |
fd5e5847 |
961 | !(attrs.permissions & 0040000)) { |
962 | tell_user(stderr, "unable to create directory %s: %s", |
963 | fullname, err); |
964 | errs++; |
965 | return 1; |
966 | } |
967 | |
968 | scp_sftp_remotepath = fullname; |
969 | |
970 | return 0; |
971 | } else { |
972 | char buf[40]; |
973 | sprintf(buf, "D%04o 0 ", modes); |
51470298 |
974 | back->send(backhandle, buf, strlen(buf)); |
975 | back->send(backhandle, name, strlen(name)); |
976 | back->send(backhandle, "\n", 1); |
fd5e5847 |
977 | return response(); |
978 | } |
120e4b40 |
979 | } |
980 | |
981 | int scp_send_enddir(void) |
982 | { |
fd5e5847 |
983 | if (using_sftp) { |
984 | sfree(scp_sftp_remotepath); |
985 | return 0; |
986 | } else { |
51470298 |
987 | back->send(backhandle, "E\n", 2); |
fd5e5847 |
988 | return response(); |
989 | } |
990 | } |
991 | |
992 | /* |
993 | * Yes, I know; I have an scp_sink_setup _and_ an scp_sink_init. |
994 | * That's bad. The difference is that scp_sink_setup is called once |
995 | * right at the start, whereas scp_sink_init is called to |
996 | * initialise every level of recursion in the protocol. |
997 | */ |
4eb24e3a |
998 | int scp_sink_setup(char *source, int preserve, int recursive) |
fd5e5847 |
999 | { |
1000 | if (using_sftp) { |
4eb24e3a |
1001 | char *newsource; |
02105c79 |
1002 | |
1003 | if (!fxp_init()) { |
1004 | tell_user(stderr, "unable to initialise SFTP: %s", fxp_error()); |
1005 | errs++; |
1006 | return 1; |
1007 | } |
4eb24e3a |
1008 | /* |
1009 | * It's possible that the source string we've been given |
1010 | * contains a wildcard. If so, we must split the directory |
1011 | * away from the wildcard itself (throwing an error if any |
1012 | * wildcardness comes before the final slash) and arrange |
1013 | * things so that a dirstack entry will be set up. |
1014 | */ |
3d88e64d |
1015 | newsource = snewn(1+strlen(source), char); |
4eb24e3a |
1016 | if (!wc_unescape(newsource, source)) { |
1017 | /* Yes, here we go; it's a wildcard. Bah. */ |
1018 | char *dupsource, *lastpart, *dirpart, *wildcard; |
1019 | dupsource = dupstr(source); |
1020 | lastpart = stripslashes(dupsource, 0); |
1021 | wildcard = dupstr(lastpart); |
1022 | *lastpart = '\0'; |
1023 | if (*dupsource && dupsource[1]) { |
1024 | /* |
1025 | * The remains of dupsource are at least two |
1026 | * characters long, meaning the pathname wasn't |
1027 | * empty or just `/'. Hence, we remove the trailing |
1028 | * slash. |
1029 | */ |
1030 | lastpart[-1] = '\0'; |
6b18a524 |
1031 | } else if (!*dupsource) { |
1032 | /* |
1033 | * The remains of dupsource are _empty_ - the whole |
1034 | * pathname was a wildcard. Hence we need to |
1035 | * replace it with ".". |
1036 | */ |
1037 | sfree(dupsource); |
1038 | dupsource = dupstr("."); |
4eb24e3a |
1039 | } |
1040 | |
1041 | /* |
1042 | * Now we have separated our string into dupsource (the |
1043 | * directory part) and wildcard. Both of these will |
1044 | * need freeing at some point. Next step is to remove |
1045 | * wildcard escapes from the directory part, throwing |
1046 | * an error if it contains a real wildcard. |
1047 | */ |
3d88e64d |
1048 | dirpart = snewn(1+strlen(dupsource), char); |
4eb24e3a |
1049 | if (!wc_unescape(dirpart, dupsource)) { |
1050 | tell_user(stderr, "%s: multiple-level wildcards unsupported", |
1051 | source); |
1052 | errs++; |
1053 | sfree(dirpart); |
1054 | sfree(wildcard); |
1055 | sfree(dupsource); |
1056 | return 1; |
1057 | } |
1058 | |
1059 | /* |
1060 | * Now we have dirpart (unescaped, ie a valid remote |
1061 | * path), and wildcard (a wildcard). This will be |
1062 | * sufficient to arrange a dirstack entry. |
1063 | */ |
1064 | scp_sftp_remotepath = dirpart; |
1065 | scp_sftp_wildcard = wildcard; |
1066 | sfree(dupsource); |
1067 | } else { |
1068 | scp_sftp_remotepath = newsource; |
1069 | scp_sftp_wildcard = NULL; |
1070 | } |
fd5e5847 |
1071 | scp_sftp_preserve = preserve; |
1072 | scp_sftp_recursive = recursive; |
1073 | scp_sftp_donethistarget = 0; |
1074 | scp_sftp_dirstack_head = NULL; |
1075 | } |
4eb24e3a |
1076 | return 0; |
120e4b40 |
1077 | } |
1078 | |
1079 | int scp_sink_init(void) |
1080 | { |
fd5e5847 |
1081 | if (!using_sftp) { |
51470298 |
1082 | back->send(backhandle, "", 1); |
fd5e5847 |
1083 | } |
120e4b40 |
1084 | return 0; |
1085 | } |
1086 | |
1087 | #define SCP_SINK_FILE 1 |
1088 | #define SCP_SINK_DIR 2 |
1089 | #define SCP_SINK_ENDDIR 3 |
4eb24e3a |
1090 | #define SCP_SINK_RETRY 4 /* not an action; just try again */ |
120e4b40 |
1091 | struct scp_sink_action { |
1092 | int action; /* FILE, DIR, ENDDIR */ |
1093 | char *buf; /* will need freeing after use */ |
1094 | char *name; /* filename or dirname (not ENDDIR) */ |
1095 | int mode; /* access mode (not ENDDIR) */ |
1096 | unsigned long size; /* file size (not ENDDIR) */ |
1097 | int settime; /* 1 if atime and mtime are filled */ |
1098 | unsigned long atime, mtime; /* access times for the file */ |
1099 | }; |
1100 | |
1101 | int scp_get_sink_action(struct scp_sink_action *act) |
1102 | { |
fd5e5847 |
1103 | if (using_sftp) { |
1104 | char *fname; |
1105 | int must_free_fname; |
1106 | struct fxp_attrs attrs; |
1bc24185 |
1107 | struct sftp_packet *pktin; |
1108 | struct sftp_request *req, *rreq; |
fd5e5847 |
1109 | int ret; |
1110 | |
1111 | if (!scp_sftp_dirstack_head) { |
1112 | if (!scp_sftp_donethistarget) { |
1113 | /* |
1114 | * Simple case: we are only dealing with one file. |
1115 | */ |
1116 | fname = scp_sftp_remotepath; |
1117 | must_free_fname = 0; |
1118 | scp_sftp_donethistarget = 1; |
1119 | } else { |
1120 | /* |
1121 | * Even simpler case: one file _which we've done_. |
1122 | * Return 1 (finished). |
1123 | */ |
1124 | return 1; |
1125 | } |
1126 | } else { |
1127 | /* |
1128 | * We're now in the middle of stepping through a list |
1129 | * of names returned from fxp_readdir(); so let's carry |
1130 | * on. |
1131 | */ |
1132 | struct scp_sftp_dirstack *head = scp_sftp_dirstack_head; |
1133 | while (head->namepos < head->namelen && |
4eb24e3a |
1134 | (is_dots(head->names[head->namepos].filename) || |
1135 | (head->wildcard && |
1136 | !wc_match(head->wildcard, |
1137 | head->names[head->namepos].filename)))) |
fd5e5847 |
1138 | head->namepos++; /* skip . and .. */ |
1139 | if (head->namepos < head->namelen) { |
825ec8ee |
1140 | head->matched_something = 1; |
fd5e5847 |
1141 | fname = dupcat(head->dirpath, "/", |
1142 | head->names[head->namepos++].filename, |
1143 | NULL); |
1144 | must_free_fname = 1; |
1145 | } else { |
1146 | /* |
1147 | * We've come to the end of the list; pop it off |
4eb24e3a |
1148 | * the stack and return an ENDDIR action (or RETRY |
1149 | * if this was a wildcard match). |
fd5e5847 |
1150 | */ |
4eb24e3a |
1151 | if (head->wildcard) { |
1152 | act->action = SCP_SINK_RETRY; |
825ec8ee |
1153 | if (!head->matched_something) { |
1154 | tell_user(stderr, "pscp: wildcard '%s' matched " |
1155 | "no files", head->wildcard); |
1156 | errs++; |
1157 | } |
4eb24e3a |
1158 | sfree(head->wildcard); |
825ec8ee |
1159 | |
4eb24e3a |
1160 | } else { |
1161 | act->action = SCP_SINK_ENDDIR; |
1162 | } |
1163 | |
fd5e5847 |
1164 | sfree(head->dirpath); |
1165 | sfree(head->names); |
1166 | scp_sftp_dirstack_head = head->next; |
1167 | sfree(head); |
1168 | |
fd5e5847 |
1169 | return 0; |
1170 | } |
1171 | } |
cd1f39ab |
1172 | |
fd5e5847 |
1173 | /* |
1174 | * Now we have a filename. Stat it, and see if it's a file |
1175 | * or a directory. |
1176 | */ |
1bc24185 |
1177 | sftp_register(req = fxp_stat_send(fname)); |
1178 | rreq = sftp_find_request(pktin = sftp_recv()); |
1179 | assert(rreq == req); |
7b7de4f4 |
1180 | ret = fxp_stat_recv(pktin, rreq, &attrs); |
1bc24185 |
1181 | |
fd5e5847 |
1182 | if (!ret || !(attrs.flags & SSH_FILEXFER_ATTR_PERMISSIONS)) { |
1183 | tell_user(stderr, "unable to identify %s: %s", fname, |
1184 | ret ? "file type not supplied" : fxp_error()); |
1185 | errs++; |
120e4b40 |
1186 | return 1; |
fd5e5847 |
1187 | } |
1188 | |
1189 | if (attrs.permissions & 0040000) { |
1190 | struct scp_sftp_dirstack *newitem; |
1191 | struct fxp_handle *dirhandle; |
1192 | int nnames, namesize; |
1193 | struct fxp_name *ournames; |
1194 | struct fxp_names *names; |
1195 | |
1196 | /* |
37dfb97a |
1197 | * It's a directory. If we're not in recursive mode, |
1198 | * this merits a complaint (which is fatal if the name |
1199 | * was specified directly, but not if it was matched by |
1200 | * a wildcard). |
1201 | * |
1202 | * We skip this complaint completely if |
1203 | * scp_sftp_wildcard is set, because that's an |
1204 | * indication that we're not actually supposed to |
1205 | * _recursively_ transfer the dir, just scan it for |
1206 | * things matching the wildcard. |
fd5e5847 |
1207 | */ |
4eb24e3a |
1208 | if (!scp_sftp_recursive && !scp_sftp_wildcard) { |
fd5e5847 |
1209 | tell_user(stderr, "pscp: %s: is a directory", fname); |
1210 | errs++; |
1211 | if (must_free_fname) sfree(fname); |
37dfb97a |
1212 | if (scp_sftp_dirstack_head) { |
1213 | act->action = SCP_SINK_RETRY; |
1214 | return 0; |
1215 | } else { |
1216 | return 1; |
1217 | } |
120e4b40 |
1218 | } |
fd5e5847 |
1219 | |
1220 | /* |
1221 | * Otherwise, the fun begins. We must fxp_opendir() the |
1222 | * directory, slurp the filenames into memory, return |
4eb24e3a |
1223 | * SCP_SINK_DIR (unless this is a wildcard match), and |
1224 | * set targetisdir. The next time we're called, we will |
1225 | * run through the list of filenames one by one, |
1226 | * matching them against a wildcard if present. |
fd5e5847 |
1227 | * |
1228 | * If targetisdir is _already_ set (meaning we're |
1229 | * already in the middle of going through another such |
1230 | * list), we must push the other (target,namelist) pair |
1231 | * on a stack. |
1232 | */ |
1bc24185 |
1233 | sftp_register(req = fxp_opendir_send(fname)); |
1234 | rreq = sftp_find_request(pktin = sftp_recv()); |
1235 | assert(rreq == req); |
7b7de4f4 |
1236 | dirhandle = fxp_opendir_recv(pktin, rreq); |
1bc24185 |
1237 | |
fd5e5847 |
1238 | if (!dirhandle) { |
1239 | tell_user(stderr, "scp: unable to open directory %s: %s", |
1240 | fname, fxp_error()); |
1241 | if (must_free_fname) sfree(fname); |
1242 | errs++; |
1243 | return 1; |
1244 | } |
1245 | nnames = namesize = 0; |
1246 | ournames = NULL; |
1247 | while (1) { |
1248 | int i; |
1249 | |
1bc24185 |
1250 | sftp_register(req = fxp_readdir_send(dirhandle)); |
1251 | rreq = sftp_find_request(pktin = sftp_recv()); |
1252 | assert(rreq == req); |
7b7de4f4 |
1253 | names = fxp_readdir_recv(pktin, rreq); |
1bc24185 |
1254 | |
fd5e5847 |
1255 | if (names == NULL) { |
1256 | if (fxp_error_type() == SSH_FX_EOF) |
1257 | break; |
1258 | tell_user(stderr, "scp: reading directory %s: %s\n", |
1259 | fname, fxp_error()); |
1260 | if (must_free_fname) sfree(fname); |
1261 | sfree(ournames); |
1262 | errs++; |
1263 | return 1; |
1264 | } |
1265 | if (names->nnames == 0) { |
1266 | fxp_free_names(names); |
1267 | break; |
1268 | } |
1269 | if (nnames + names->nnames >= namesize) { |
1270 | namesize += names->nnames + 128; |
3d88e64d |
1271 | ournames = sresize(ournames, namesize, struct fxp_name); |
fd5e5847 |
1272 | } |
1273 | for (i = 0; i < names->nnames; i++) |
1274 | ournames[nnames++] = names->names[i]; |
1275 | names->nnames = 0; /* prevent free_names */ |
1276 | fxp_free_names(names); |
1277 | } |
1bc24185 |
1278 | sftp_register(req = fxp_close_send(dirhandle)); |
1279 | rreq = sftp_find_request(pktin = sftp_recv()); |
1280 | assert(rreq == req); |
7b7de4f4 |
1281 | fxp_close_recv(pktin, rreq); |
fd5e5847 |
1282 | |
3d88e64d |
1283 | newitem = snew(struct scp_sftp_dirstack); |
fd5e5847 |
1284 | newitem->next = scp_sftp_dirstack_head; |
1285 | newitem->names = ournames; |
1286 | newitem->namepos = 0; |
1287 | newitem->namelen = nnames; |
1288 | if (must_free_fname) |
1289 | newitem->dirpath = fname; |
1290 | else |
1291 | newitem->dirpath = dupstr(fname); |
4eb24e3a |
1292 | if (scp_sftp_wildcard) { |
1293 | newitem->wildcard = scp_sftp_wildcard; |
825ec8ee |
1294 | newitem->matched_something = 0; |
4eb24e3a |
1295 | scp_sftp_wildcard = NULL; |
1296 | } else { |
1297 | newitem->wildcard = NULL; |
1298 | } |
fd5e5847 |
1299 | scp_sftp_dirstack_head = newitem; |
1300 | |
4eb24e3a |
1301 | if (newitem->wildcard) { |
1302 | act->action = SCP_SINK_RETRY; |
1303 | } else { |
1304 | act->action = SCP_SINK_DIR; |
1305 | act->buf = dupstr(stripslashes(fname, 0)); |
1306 | act->name = act->buf; |
1307 | act->size = 0; /* duhh, it's a directory */ |
1308 | act->mode = 07777 & attrs.permissions; |
1309 | if (scp_sftp_preserve && |
1310 | (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) { |
1311 | act->atime = attrs.atime; |
1312 | act->mtime = attrs.mtime; |
1313 | act->settime = 1; |
1314 | } else |
1315 | act->settime = 0; |
1316 | } |
120e4b40 |
1317 | return 0; |
fd5e5847 |
1318 | |
1319 | } else { |
1320 | /* |
1321 | * It's a file. Return SCP_SINK_FILE. |
1322 | */ |
1323 | act->action = SCP_SINK_FILE; |
4eb24e3a |
1324 | act->buf = dupstr(stripslashes(fname, 0)); |
fd5e5847 |
1325 | act->name = act->buf; |
1326 | if (attrs.flags & SSH_FILEXFER_ATTR_SIZE) { |
1327 | if (uint64_compare(attrs.size, |
1328 | uint64_make(0, ULONG_MAX)) > 0) { |
1329 | act->size = ULONG_MAX; /* *boggle* */ |
1330 | } else |
1331 | act->size = attrs.size.lo; |
1332 | } else |
1333 | act->size = ULONG_MAX; /* no idea */ |
1334 | act->mode = 07777 & attrs.permissions; |
1335 | if (scp_sftp_preserve && |
1336 | (attrs.flags & SSH_FILEXFER_ATTR_ACMODTIME)) { |
1337 | act->atime = attrs.atime; |
1338 | act->mtime = attrs.mtime; |
120e4b40 |
1339 | act->settime = 1; |
fd5e5847 |
1340 | } else |
1341 | act->settime = 0; |
1342 | if (must_free_fname) |
1343 | scp_sftp_currentname = fname; |
1344 | else |
1345 | scp_sftp_currentname = dupstr(fname); |
1346 | return 0; |
1347 | } |
1348 | |
1349 | } else { |
1350 | int done = 0; |
1351 | int i, bufsize; |
1352 | int action; |
1353 | char ch; |
1354 | |
1355 | act->settime = 0; |
1356 | act->buf = NULL; |
1357 | bufsize = 0; |
1358 | |
1359 | while (!done) { |
776792d7 |
1360 | if (ssh_scp_recv((unsigned char *) &ch, 1) <= 0) |
fd5e5847 |
1361 | return 1; |
1362 | if (ch == '\n') |
1363 | bump("Protocol error: Unexpected newline"); |
1364 | i = 0; |
1365 | action = ch; |
1366 | do { |
776792d7 |
1367 | if (ssh_scp_recv((unsigned char *) &ch, 1) <= 0) |
fd5e5847 |
1368 | bump("Lost connection"); |
1369 | if (i >= bufsize) { |
1370 | bufsize = i + 128; |
3d88e64d |
1371 | act->buf = sresize(act->buf, bufsize, char); |
fd5e5847 |
1372 | } |
1373 | act->buf[i++] = ch; |
1374 | } while (ch != '\n'); |
1375 | act->buf[i - 1] = '\0'; |
1376 | switch (action) { |
1377 | case '\01': /* error */ |
1378 | tell_user(stderr, "%s\n", act->buf); |
1379 | errs++; |
1380 | continue; /* go round again */ |
1381 | case '\02': /* fatal error */ |
1382 | bump("%s", act->buf); |
1383 | case 'E': |
51470298 |
1384 | back->send(backhandle, "", 1); |
fd5e5847 |
1385 | act->action = SCP_SINK_ENDDIR; |
1386 | return 0; |
1387 | case 'T': |
1388 | if (sscanf(act->buf, "%ld %*d %ld %*d", |
1389 | &act->mtime, &act->atime) == 2) { |
1390 | act->settime = 1; |
51470298 |
1391 | back->send(backhandle, "", 1); |
fd5e5847 |
1392 | continue; /* go round again */ |
1393 | } |
1394 | bump("Protocol error: Illegal time format"); |
1395 | case 'C': |
1396 | case 'D': |
1397 | act->action = (action == 'C' ? SCP_SINK_FILE : SCP_SINK_DIR); |
1398 | break; |
1399 | default: |
1400 | bump("Protocol error: Expected control record"); |
120e4b40 |
1401 | } |
fd5e5847 |
1402 | /* |
1403 | * We will go round this loop only once, unless we hit |
1404 | * `continue' above. |
1405 | */ |
1406 | done = 1; |
120e4b40 |
1407 | } |
fd5e5847 |
1408 | |
120e4b40 |
1409 | /* |
fd5e5847 |
1410 | * If we get here, we must have seen SCP_SINK_FILE or |
1411 | * SCP_SINK_DIR. |
120e4b40 |
1412 | */ |
fd5e5847 |
1413 | if (sscanf(act->buf, "%o %lu %n", &act->mode, &act->size, &i) != 2) |
1414 | bump("Protocol error: Illegal file descriptor format"); |
1415 | act->name = act->buf + i; |
1416 | return 0; |
120e4b40 |
1417 | } |
120e4b40 |
1418 | } |
1419 | |
1420 | int scp_accept_filexfer(void) |
1421 | { |
fd5e5847 |
1422 | if (using_sftp) { |
1bc24185 |
1423 | struct sftp_packet *pktin; |
1424 | struct sftp_request *req, *rreq; |
1425 | |
1426 | sftp_register(req = fxp_open_send(scp_sftp_currentname, SSH_FXF_READ)); |
1427 | rreq = sftp_find_request(pktin = sftp_recv()); |
1428 | assert(rreq == req); |
7b7de4f4 |
1429 | scp_sftp_filehandle = fxp_open_recv(pktin, rreq); |
1bc24185 |
1430 | |
fd5e5847 |
1431 | if (!scp_sftp_filehandle) { |
1432 | tell_user(stderr, "pscp: unable to open %s: %s", |
1433 | scp_sftp_currentname, fxp_error()); |
1434 | errs++; |
1435 | return 1; |
1436 | } |
1437 | scp_sftp_fileoffset = uint64_make(0, 0); |
7fd264b2 |
1438 | scp_sftp_xfer = xfer_download_init(scp_sftp_filehandle, |
1439 | scp_sftp_fileoffset); |
fd5e5847 |
1440 | sfree(scp_sftp_currentname); |
1441 | return 0; |
1442 | } else { |
51470298 |
1443 | back->send(backhandle, "", 1); |
fd5e5847 |
1444 | return 0; /* can't fail */ |
1445 | } |
120e4b40 |
1446 | } |
1447 | |
1448 | int scp_recv_filedata(char *data, int len) |
1449 | { |
fd5e5847 |
1450 | if (using_sftp) { |
1bc24185 |
1451 | struct sftp_packet *pktin; |
7fd264b2 |
1452 | int ret, actuallen; |
1453 | void *vbuf; |
1bc24185 |
1454 | |
7fd264b2 |
1455 | xfer_download_queue(scp_sftp_xfer); |
1456 | pktin = sftp_recv(); |
1457 | ret = xfer_download_gotpkt(scp_sftp_xfer, pktin); |
1bc24185 |
1458 | |
7fd264b2 |
1459 | if (ret < 0) { |
fd5e5847 |
1460 | tell_user(stderr, "pscp: error while reading: %s", fxp_error()); |
1461 | errs++; |
1462 | return -1; |
1463 | } |
7fd264b2 |
1464 | |
1465 | if (xfer_download_data(scp_sftp_xfer, &vbuf, &actuallen)) { |
1466 | /* |
1467 | * This assertion relies on the fact that the natural |
1468 | * block size used in the xfer manager is at most that |
1469 | * used in this module. I don't like crossing layers in |
1470 | * this way, but it'll do for now. |
1471 | */ |
1472 | assert(actuallen <= len); |
1473 | memcpy(data, vbuf, actuallen); |
1474 | sfree(vbuf); |
1475 | } else |
fd5e5847 |
1476 | actuallen = 0; |
1477 | |
1478 | scp_sftp_fileoffset = uint64_add32(scp_sftp_fileoffset, actuallen); |
1479 | |
1480 | return actuallen; |
1481 | } else { |
776792d7 |
1482 | return ssh_scp_recv((unsigned char *) data, len); |
fd5e5847 |
1483 | } |
120e4b40 |
1484 | } |
1485 | |
1486 | int scp_finish_filerecv(void) |
1487 | { |
fd5e5847 |
1488 | if (using_sftp) { |
1bc24185 |
1489 | struct sftp_packet *pktin; |
1490 | struct sftp_request *req, *rreq; |
1491 | |
7fd264b2 |
1492 | /* |
1493 | * Ensure that xfer_done() will work correctly, so we can |
1494 | * clean up any outstanding requests from the file |
1495 | * transfer. |
1496 | */ |
1497 | xfer_set_error(scp_sftp_xfer); |
1498 | while (!xfer_done(scp_sftp_xfer)) { |
1499 | void *vbuf; |
1500 | int len; |
1501 | |
1502 | pktin = sftp_recv(); |
1503 | xfer_download_gotpkt(scp_sftp_xfer, pktin); |
1504 | if (xfer_download_data(scp_sftp_xfer, &vbuf, &len)) |
1505 | sfree(vbuf); |
1506 | } |
1507 | xfer_cleanup(scp_sftp_xfer); |
1508 | |
1bc24185 |
1509 | sftp_register(req = fxp_close_send(scp_sftp_filehandle)); |
1510 | rreq = sftp_find_request(pktin = sftp_recv()); |
1511 | assert(rreq == req); |
7b7de4f4 |
1512 | fxp_close_recv(pktin, rreq); |
fd5e5847 |
1513 | return 0; |
1514 | } else { |
51470298 |
1515 | back->send(backhandle, "", 1); |
fd5e5847 |
1516 | return response(); |
1517 | } |
120e4b40 |
1518 | } |
1519 | |
1520 | /* ---------------------------------------------------------------------- |
07d9aa13 |
1521 | * Send an error message to the other side and to the screen. |
1522 | * Increment error counter. |
1523 | */ |
1524 | static void run_err(const char *fmt, ...) |
1525 | { |
57356d63 |
1526 | char *str, *str2; |
c51a56e2 |
1527 | va_list ap; |
1528 | va_start(ap, fmt); |
1529 | errs++; |
57356d63 |
1530 | str = dupvprintf(fmt, ap); |
1531 | str2 = dupcat("scp: ", str, "\n", NULL); |
1532 | sfree(str); |
1533 | scp_send_errmsg(str2); |
1534 | tell_user(stderr, "%s", str2); |
c51a56e2 |
1535 | va_end(ap); |
57356d63 |
1536 | sfree(str2); |
07d9aa13 |
1537 | } |
1538 | |
07d9aa13 |
1539 | /* |
1540 | * Execute the source part of the SCP protocol. |
1541 | */ |
1542 | static void source(char *src) |
1543 | { |
c51a56e2 |
1544 | unsigned long size; |
799dfcfa |
1545 | unsigned long mtime, atime; |
c51a56e2 |
1546 | char *last; |
799dfcfa |
1547 | RFile *f; |
1548 | int attr; |
c51a56e2 |
1549 | unsigned long i; |
1550 | unsigned long stat_bytes; |
1551 | time_t stat_starttime, stat_lasttime; |
1552 | |
799dfcfa |
1553 | attr = file_type(src); |
1554 | if (attr == FILE_TYPE_NONEXISTENT || |
1555 | attr == FILE_TYPE_WEIRD) { |
1556 | run_err("%s: %s file or directory", src, |
1557 | (attr == FILE_TYPE_WEIRD ? "Not a" : "No such")); |
c51a56e2 |
1558 | return; |
1559 | } |
1560 | |
799dfcfa |
1561 | if (attr == FILE_TYPE_DIRECTORY) { |
7f1f80de |
1562 | if (recursive) { |
32874aea |
1563 | /* |
1564 | * Avoid . and .. directories. |
1565 | */ |
1566 | char *p; |
1567 | p = strrchr(src, '/'); |
1568 | if (!p) |
1569 | p = strrchr(src, '\\'); |
1570 | if (!p) |
1571 | p = src; |
1572 | else |
1573 | p++; |
1574 | if (!strcmp(p, ".") || !strcmp(p, "..")) |
1575 | /* skip . and .. */ ; |
1576 | else |
1577 | rsource(src); |
1578 | } else { |
c51a56e2 |
1579 | run_err("%s: not a regular file", src); |
32874aea |
1580 | } |
c51a56e2 |
1581 | return; |
1582 | } |
1583 | |
1584 | if ((last = strrchr(src, '/')) == NULL) |
1585 | last = src; |
1586 | else |
1587 | last++; |
1588 | if (strrchr(last, '\\') != NULL) |
1589 | last = strrchr(last, '\\') + 1; |
1590 | if (last == src && strchr(src, ':') != NULL) |
1591 | last = strchr(src, ':') + 1; |
1592 | |
799dfcfa |
1593 | f = open_existing_file(src, &size, &mtime, &atime); |
1594 | if (f == NULL) { |
486543a1 |
1595 | run_err("%s: Cannot open file", src); |
c51a56e2 |
1596 | return; |
1597 | } |
c51a56e2 |
1598 | if (preserve) { |
120e4b40 |
1599 | if (scp_send_filetimes(mtime, atime)) |
c51a56e2 |
1600 | return; |
1601 | } |
1602 | |
c51a56e2 |
1603 | if (verbose) |
120e4b40 |
1604 | tell_user(stderr, "Sending file %s, size=%lu", last, size); |
1605 | if (scp_send_filename(last, size, 0644)) |
c51a56e2 |
1606 | return; |
1607 | |
2d466ffd |
1608 | stat_bytes = 0; |
1609 | stat_starttime = time(NULL); |
1610 | stat_lasttime = 0; |
c51a56e2 |
1611 | |
1612 | for (i = 0; i < size; i += 4096) { |
1613 | char transbuf[4096]; |
799dfcfa |
1614 | int j, k = 4096; |
5471d09a |
1615 | |
32874aea |
1616 | if (i + k > size) |
1617 | k = size - i; |
799dfcfa |
1618 | if ((j = read_from_file(f, transbuf, k)) != k) { |
32874aea |
1619 | if (statistics) |
1620 | printf("\n"); |
c51a56e2 |
1621 | bump("%s: Read error", src); |
07d9aa13 |
1622 | } |
120e4b40 |
1623 | if (scp_send_filedata(transbuf, k)) |
1624 | bump("%s: Network error occurred", src); |
1625 | |
c51a56e2 |
1626 | if (statistics) { |
1627 | stat_bytes += k; |
32874aea |
1628 | if (time(NULL) != stat_lasttime || i + k == size) { |
c51a56e2 |
1629 | stat_lasttime = time(NULL); |
1630 | print_stats(last, size, stat_bytes, |
1631 | stat_starttime, stat_lasttime); |
1632 | } |
07d9aa13 |
1633 | } |
5471d09a |
1634 | |
c51a56e2 |
1635 | } |
799dfcfa |
1636 | close_rfile(f); |
07d9aa13 |
1637 | |
120e4b40 |
1638 | (void) scp_send_finish(); |
07d9aa13 |
1639 | } |
1640 | |
07d9aa13 |
1641 | /* |
1642 | * Recursively send the contents of a directory. |
1643 | */ |
1644 | static void rsource(char *src) |
1645 | { |
799dfcfa |
1646 | char *last; |
fd5e5847 |
1647 | char *save_target; |
799dfcfa |
1648 | DirHandle *dir; |
c51a56e2 |
1649 | |
1650 | if ((last = strrchr(src, '/')) == NULL) |
1651 | last = src; |
1652 | else |
1653 | last++; |
1654 | if (strrchr(last, '\\') != NULL) |
1655 | last = strrchr(last, '\\') + 1; |
1656 | if (last == src && strchr(src, ':') != NULL) |
1657 | last = strchr(src, ':') + 1; |
1658 | |
1659 | /* maybe send filetime */ |
1660 | |
fd5e5847 |
1661 | save_target = scp_save_remotepath(); |
1662 | |
c51a56e2 |
1663 | if (verbose) |
120e4b40 |
1664 | tell_user(stderr, "Entering directory: %s", last); |
1665 | if (scp_send_dirname(last, 0755)) |
c51a56e2 |
1666 | return; |
1667 | |
799dfcfa |
1668 | dir = open_directory(src); |
1669 | if (dir != NULL) { |
1670 | char *filename; |
1671 | while ((filename = read_filename(dir)) != NULL) { |
1672 | char *foundfile = dupcat(src, "/", filename, NULL); |
03f64569 |
1673 | source(foundfile); |
1674 | sfree(foundfile); |
799dfcfa |
1675 | sfree(filename); |
07d9aa13 |
1676 | } |
c51a56e2 |
1677 | } |
799dfcfa |
1678 | close_directory(dir); |
07d9aa13 |
1679 | |
120e4b40 |
1680 | (void) scp_send_enddir(); |
fd5e5847 |
1681 | |
1682 | scp_restore_remotepath(save_target); |
07d9aa13 |
1683 | } |
1684 | |
07d9aa13 |
1685 | /* |
03f64569 |
1686 | * Execute the sink part of the SCP protocol. |
07d9aa13 |
1687 | */ |
ca2d5943 |
1688 | static void sink(char *targ, char *src) |
07d9aa13 |
1689 | { |
03f64569 |
1690 | char *destfname; |
c51a56e2 |
1691 | int targisdir = 0; |
c51a56e2 |
1692 | int exists; |
799dfcfa |
1693 | int attr; |
1694 | WFile *f; |
120e4b40 |
1695 | unsigned long received; |
c51a56e2 |
1696 | int wrerror = 0; |
1697 | unsigned long stat_bytes; |
1698 | time_t stat_starttime, stat_lasttime; |
1699 | char *stat_name; |
1700 | |
799dfcfa |
1701 | attr = file_type(targ); |
1702 | if (attr == FILE_TYPE_DIRECTORY) |
c51a56e2 |
1703 | targisdir = 1; |
1704 | |
1705 | if (targetshouldbedirectory && !targisdir) |
1706 | bump("%s: Not a directory", targ); |
1707 | |
120e4b40 |
1708 | scp_sink_init(); |
c51a56e2 |
1709 | while (1) { |
120e4b40 |
1710 | struct scp_sink_action act; |
1711 | if (scp_get_sink_action(&act)) |
c51a56e2 |
1712 | return; |
07d9aa13 |
1713 | |
120e4b40 |
1714 | if (act.action == SCP_SINK_ENDDIR) |
1715 | return; |
03f64569 |
1716 | |
4eb24e3a |
1717 | if (act.action == SCP_SINK_RETRY) |
1718 | continue; |
1719 | |
c51a56e2 |
1720 | if (targisdir) { |
03f64569 |
1721 | /* |
1722 | * Prevent the remote side from maliciously writing to |
1723 | * files outside the target area by sending a filename |
1724 | * containing `../'. In fact, it shouldn't be sending |
b3dcd9b2 |
1725 | * filenames with any slashes or colons in at all; so |
1726 | * we'll find the last slash, backslash or colon in the |
1727 | * filename and use only the part after that. (And |
1728 | * warn!) |
03f64569 |
1729 | * |
1730 | * In addition, we also ensure here that if we're |
1731 | * copying a single file and the target is a directory |
1732 | * (common usage: `pscp host:filename .') the remote |
1733 | * can't send us a _different_ file name. We can |
1734 | * distinguish this case because `src' will be non-NULL |
1735 | * and the last component of that will fail to match |
1736 | * (the last component of) the name sent. |
4eeae4a3 |
1737 | * |
cd1f39ab |
1738 | * Well, not always; if `src' is a wildcard, we do |
4eeae4a3 |
1739 | * expect to get back filenames that don't correspond |
cd1f39ab |
1740 | * exactly to it. Ideally in this case, we would like |
1741 | * to ensure that the returned filename actually |
1742 | * matches the wildcard pattern - but one of SCP's |
1743 | * protocol infelicities is that wildcard matching is |
1744 | * done at the server end _by the server's rules_ and |
1745 | * so in general this is infeasible. Hence, we only |
1746 | * accept filenames that don't correspond to `src' if |
1747 | * unsafe mode is enabled or we are using SFTP (which |
1748 | * resolves remote wildcards on the client side and can |
1749 | * be trusted). |
03f64569 |
1750 | */ |
1751 | char *striptarget, *stripsrc; |
1752 | |
4eb24e3a |
1753 | striptarget = stripslashes(act.name, 1); |
03f64569 |
1754 | if (striptarget != act.name) { |
1755 | tell_user(stderr, "warning: remote host sent a compound" |
b3dcd9b2 |
1756 | " pathname '%s'", act.name); |
1757 | tell_user(stderr, " renaming local file to '%s'", |
1758 | striptarget); |
03f64569 |
1759 | } |
1760 | |
1761 | /* |
1762 | * Also check to see if the target filename is '.' or |
1763 | * '..', or indeed '...' and so on because Windows |
1764 | * appears to interpret those like '..'. |
1765 | */ |
fd5e5847 |
1766 | if (is_dots(striptarget)) { |
03f64569 |
1767 | bump("security violation: remote host attempted to write to" |
1768 | " a '.' or '..' path!"); |
1769 | } |
1770 | |
1771 | if (src) { |
4eb24e3a |
1772 | stripsrc = stripslashes(src, 1); |
cd1f39ab |
1773 | if (strcmp(striptarget, stripsrc) && |
1774 | !using_sftp && !scp_unsafe_mode) { |
1775 | tell_user(stderr, "warning: remote host tried to write " |
1776 | "to a file called '%s'", striptarget); |
1777 | tell_user(stderr, " when we requested a file " |
1778 | "called '%s'.", stripsrc); |
1779 | tell_user(stderr, " If this is a wildcard, " |
1780 | "consider upgrading to SSH 2 or using"); |
1781 | tell_user(stderr, " the '-unsafe' option. Renaming" |
1782 | " of this file has been disallowed."); |
4eeae4a3 |
1783 | /* Override the name the server provided with our own. */ |
1784 | striptarget = stripsrc; |
03f64569 |
1785 | } |
03f64569 |
1786 | } |
1787 | |
c51a56e2 |
1788 | if (targ[0] != '\0') |
8c7d710c |
1789 | destfname = dir_file_cat(targ, striptarget); |
03f64569 |
1790 | else |
1791 | destfname = dupstr(striptarget); |
c51a56e2 |
1792 | } else { |
03f64569 |
1793 | /* |
1794 | * In this branch of the if, the target area is a |
1795 | * single file with an explicitly specified name in any |
1796 | * case, so there's no danger. |
1797 | */ |
1798 | destfname = dupstr(targ); |
c51a56e2 |
1799 | } |
799dfcfa |
1800 | attr = file_type(destfname); |
1801 | exists = (attr != FILE_TYPE_NONEXISTENT); |
c51a56e2 |
1802 | |
120e4b40 |
1803 | if (act.action == SCP_SINK_DIR) { |
799dfcfa |
1804 | if (exists && attr != FILE_TYPE_DIRECTORY) { |
03f64569 |
1805 | run_err("%s: Not a directory", destfname); |
c51a56e2 |
1806 | continue; |
1807 | } |
1808 | if (!exists) { |
799dfcfa |
1809 | if (!create_directory(destfname)) { |
03f64569 |
1810 | run_err("%s: Cannot create directory", destfname); |
c51a56e2 |
1811 | continue; |
1812 | } |
1813 | } |
03f64569 |
1814 | sink(destfname, NULL); |
c51a56e2 |
1815 | /* can we set the timestamp for directories ? */ |
1816 | continue; |
1817 | } |
07d9aa13 |
1818 | |
799dfcfa |
1819 | f = open_new_file(destfname); |
1820 | if (f == NULL) { |
03f64569 |
1821 | run_err("%s: Cannot create file", destfname); |
c51a56e2 |
1822 | continue; |
1823 | } |
07d9aa13 |
1824 | |
120e4b40 |
1825 | if (scp_accept_filexfer()) |
1826 | return; |
07d9aa13 |
1827 | |
2d466ffd |
1828 | stat_bytes = 0; |
1829 | stat_starttime = time(NULL); |
1830 | stat_lasttime = 0; |
4eb24e3a |
1831 | stat_name = stripslashes(destfname, 1); |
07d9aa13 |
1832 | |
120e4b40 |
1833 | received = 0; |
1834 | while (received < act.size) { |
c51a56e2 |
1835 | char transbuf[4096]; |
799dfcfa |
1836 | int blksize, read; |
120e4b40 |
1837 | blksize = 4096; |
799dfcfa |
1838 | if (blksize > (int)(act.size - received)) |
120e4b40 |
1839 | blksize = act.size - received; |
1840 | read = scp_recv_filedata(transbuf, blksize); |
1841 | if (read <= 0) |
c51a56e2 |
1842 | bump("Lost connection"); |
32874aea |
1843 | if (wrerror) |
1844 | continue; |
799dfcfa |
1845 | if (write_to_file(f, transbuf, read) != (int)read) { |
c51a56e2 |
1846 | wrerror = 1; |
120e4b40 |
1847 | /* FIXME: in sftp we can actually abort the transfer */ |
c51a56e2 |
1848 | if (statistics) |
1849 | printf("\r%-25.25s | %50s\n", |
1850 | stat_name, |
1851 | "Write error.. waiting for end of file"); |
1852 | continue; |
1853 | } |
1854 | if (statistics) { |
120e4b40 |
1855 | stat_bytes += read; |
1856 | if (time(NULL) > stat_lasttime || |
1857 | received + read == act.size) { |
c51a56e2 |
1858 | stat_lasttime = time(NULL); |
120e4b40 |
1859 | print_stats(stat_name, act.size, stat_bytes, |
c51a56e2 |
1860 | stat_starttime, stat_lasttime); |
07d9aa13 |
1861 | } |
c51a56e2 |
1862 | } |
120e4b40 |
1863 | received += read; |
c51a56e2 |
1864 | } |
120e4b40 |
1865 | if (act.settime) { |
799dfcfa |
1866 | set_file_times(f, act.mtime, act.atime); |
07d9aa13 |
1867 | } |
07d9aa13 |
1868 | |
799dfcfa |
1869 | close_wfile(f); |
c51a56e2 |
1870 | if (wrerror) { |
03f64569 |
1871 | run_err("%s: Write error", destfname); |
c51a56e2 |
1872 | continue; |
1873 | } |
120e4b40 |
1874 | (void) scp_finish_filerecv(); |
03f64569 |
1875 | sfree(destfname); |
d4aa8594 |
1876 | sfree(act.buf); |
c51a56e2 |
1877 | } |
1878 | } |
07d9aa13 |
1879 | |
1880 | /* |
120e4b40 |
1881 | * We will copy local files to a remote server. |
07d9aa13 |
1882 | */ |
1883 | static void toremote(int argc, char *argv[]) |
1884 | { |
c51a56e2 |
1885 | char *src, *targ, *host, *user; |
1886 | char *cmd; |
799dfcfa |
1887 | int i, wc_type; |
c51a56e2 |
1888 | |
32874aea |
1889 | targ = argv[argc - 1]; |
c51a56e2 |
1890 | |
39ddf0ff |
1891 | /* Separate host from filename */ |
c51a56e2 |
1892 | host = targ; |
1893 | targ = colon(targ); |
1894 | if (targ == NULL) |
1895 | bump("targ == NULL in toremote()"); |
1896 | *targ++ = '\0'; |
1897 | if (*targ == '\0') |
1898 | targ = "."; |
1899 | /* Substitute "." for emtpy target */ |
1900 | |
39ddf0ff |
1901 | /* Separate host and username */ |
c51a56e2 |
1902 | user = host; |
1903 | host = strrchr(host, '@'); |
1904 | if (host == NULL) { |
1905 | host = user; |
1906 | user = NULL; |
1907 | } else { |
1908 | *host++ = '\0'; |
1909 | if (*user == '\0') |
1910 | user = NULL; |
1911 | } |
1912 | |
1913 | if (argc == 2) { |
c51a56e2 |
1914 | if (colon(argv[0]) != NULL) |
1915 | bump("%s: Remote to remote not supported", argv[0]); |
799dfcfa |
1916 | |
1917 | wc_type = test_wildcard(argv[0], 1); |
1918 | if (wc_type == WCTYPE_NONEXISTENT) |
c51a56e2 |
1919 | bump("%s: No such file or directory\n", argv[0]); |
799dfcfa |
1920 | else if (wc_type == WCTYPE_WILDCARD) |
c51a56e2 |
1921 | targetshouldbedirectory = 1; |
c51a56e2 |
1922 | } |
1923 | |
57356d63 |
1924 | cmd = dupprintf("scp%s%s%s%s -t %s", |
1925 | verbose ? " -v" : "", |
1926 | recursive ? " -r" : "", |
1927 | preserve ? " -p" : "", |
1928 | targetshouldbedirectory ? " -d" : "", targ); |
c51a56e2 |
1929 | do_cmd(host, user, cmd); |
1930 | sfree(cmd); |
1931 | |
fd5e5847 |
1932 | scp_source_setup(targ, targetshouldbedirectory); |
c51a56e2 |
1933 | |
1934 | for (i = 0; i < argc - 1; i++) { |
c51a56e2 |
1935 | src = argv[i]; |
1936 | if (colon(src) != NULL) { |
cc87246d |
1937 | tell_user(stderr, "%s: Remote to remote not supported\n", src); |
c51a56e2 |
1938 | errs++; |
1939 | continue; |
07d9aa13 |
1940 | } |
03f64569 |
1941 | |
799dfcfa |
1942 | wc_type = test_wildcard(src, 1); |
1943 | if (wc_type == WCTYPE_NONEXISTENT) { |
c51a56e2 |
1944 | run_err("%s: No such file or directory", src); |
1945 | continue; |
799dfcfa |
1946 | } else if (wc_type == WCTYPE_FILENAME) { |
1947 | source(src); |
1948 | continue; |
1949 | } else { |
1950 | WildcardMatcher *wc; |
03f64569 |
1951 | char *filename; |
799dfcfa |
1952 | |
1953 | wc = begin_wildcard_matching(src); |
1954 | if (wc == NULL) { |
1955 | run_err("%s: No such file or directory", src); |
1956 | continue; |
7f266ffb |
1957 | } |
799dfcfa |
1958 | |
1959 | while ((filename = wildcard_get_filename(wc)) != NULL) { |
1960 | source(filename); |
1961 | sfree(filename); |
1962 | } |
1963 | |
1964 | finish_wildcard_matching(wc); |
1965 | } |
c51a56e2 |
1966 | } |
07d9aa13 |
1967 | } |
1968 | |
07d9aa13 |
1969 | /* |
1970 | * We will copy files from a remote server to the local machine. |
1971 | */ |
1972 | static void tolocal(int argc, char *argv[]) |
1973 | { |
c51a56e2 |
1974 | char *src, *targ, *host, *user; |
1975 | char *cmd; |
1976 | |
1977 | if (argc != 2) |
1978 | bump("More than one remote source not supported"); |
1979 | |
1980 | src = argv[0]; |
1981 | targ = argv[1]; |
1982 | |
39ddf0ff |
1983 | /* Separate host from filename */ |
c51a56e2 |
1984 | host = src; |
1985 | src = colon(src); |
1986 | if (src == NULL) |
1987 | bump("Local to local copy not supported"); |
1988 | *src++ = '\0'; |
1989 | if (*src == '\0') |
1990 | src = "."; |
1991 | /* Substitute "." for empty filename */ |
1992 | |
39ddf0ff |
1993 | /* Separate username and hostname */ |
c51a56e2 |
1994 | user = host; |
1995 | host = strrchr(host, '@'); |
1996 | if (host == NULL) { |
1997 | host = user; |
1998 | user = NULL; |
1999 | } else { |
2000 | *host++ = '\0'; |
2001 | if (*user == '\0') |
2002 | user = NULL; |
2003 | } |
2004 | |
57356d63 |
2005 | cmd = dupprintf("scp%s%s%s%s -f %s", |
2006 | verbose ? " -v" : "", |
2007 | recursive ? " -r" : "", |
2008 | preserve ? " -p" : "", |
2009 | targetshouldbedirectory ? " -d" : "", src); |
c51a56e2 |
2010 | do_cmd(host, user, cmd); |
2011 | sfree(cmd); |
2012 | |
4eb24e3a |
2013 | if (scp_sink_setup(src, preserve, recursive)) |
2014 | return; |
fd5e5847 |
2015 | |
ca2d5943 |
2016 | sink(targ, src); |
07d9aa13 |
2017 | } |
2018 | |
07d9aa13 |
2019 | /* |
39ddf0ff |
2020 | * We will issue a list command to get a remote directory. |
2021 | */ |
2022 | static void get_dir_list(int argc, char *argv[]) |
2023 | { |
2024 | char *src, *host, *user; |
2025 | char *cmd, *p, *q; |
2026 | char c; |
2027 | |
2028 | src = argv[0]; |
2029 | |
2030 | /* Separate host from filename */ |
2031 | host = src; |
2032 | src = colon(src); |
2033 | if (src == NULL) |
2034 | bump("Local to local copy not supported"); |
2035 | *src++ = '\0'; |
2036 | if (*src == '\0') |
2037 | src = "."; |
2038 | /* Substitute "." for empty filename */ |
2039 | |
2040 | /* Separate username and hostname */ |
2041 | user = host; |
2042 | host = strrchr(host, '@'); |
2043 | if (host == NULL) { |
2044 | host = user; |
2045 | user = NULL; |
2046 | } else { |
2047 | *host++ = '\0'; |
2048 | if (*user == '\0') |
2049 | user = NULL; |
2050 | } |
2051 | |
3d88e64d |
2052 | cmd = snewn(4 * strlen(src) + 100, char); |
39ddf0ff |
2053 | strcpy(cmd, "ls -la '"); |
2054 | p = cmd + strlen(cmd); |
2055 | for (q = src; *q; q++) { |
2056 | if (*q == '\'') { |
32874aea |
2057 | *p++ = '\''; |
2058 | *p++ = '\\'; |
2059 | *p++ = '\''; |
2060 | *p++ = '\''; |
39ddf0ff |
2061 | } else { |
2062 | *p++ = *q; |
2063 | } |
2064 | } |
2065 | *p++ = '\''; |
2066 | *p = '\0'; |
cc87246d |
2067 | |
39ddf0ff |
2068 | do_cmd(host, user, cmd); |
2069 | sfree(cmd); |
2070 | |
fd5e5847 |
2071 | if (using_sftp) { |
2072 | scp_sftp_listdir(src); |
2073 | } else { |
776792d7 |
2074 | while (ssh_scp_recv((unsigned char *) &c, 1) > 0) |
fd5e5847 |
2075 | tell_char(stdout, c); |
2076 | } |
39ddf0ff |
2077 | } |
2078 | |
2079 | /* |
07d9aa13 |
2080 | * Short description of parameters. |
2081 | */ |
996c8c3b |
2082 | static void usage(void) |
07d9aa13 |
2083 | { |
c51a56e2 |
2084 | printf("PuTTY Secure Copy client\n"); |
2085 | printf("%s\n", ver); |
a3e55ea1 |
2086 | printf("Usage: pscp [options] [user@]host:source target\n"); |
32874aea |
2087 | printf |
2088 | (" pscp [options] source [source...] [user@]host:target\n"); |
db77dfb8 |
2089 | printf(" pscp [options] -ls [user@]host:filespec\n"); |
b8a19193 |
2090 | printf("Options:\n"); |
2091 | printf(" -p preserve file attributes\n"); |
2092 | printf(" -q quiet, don't show statistics\n"); |
2093 | printf(" -r copy directories recursively\n"); |
2094 | printf(" -v show verbose messages\n"); |
e2a197cf |
2095 | printf(" -load sessname Load settings from saved session\n"); |
b8a19193 |
2096 | printf(" -P port connect to specified port\n"); |
e2a197cf |
2097 | printf(" -l user connect with specified username\n"); |
b8a19193 |
2098 | printf(" -pw passw login with specified password\n"); |
e2a197cf |
2099 | printf(" -1 -2 force use of particular SSH protocol version\n"); |
2100 | printf(" -C enable compression\n"); |
2101 | printf(" -i key private key file for authentication\n"); |
2102 | printf(" -batch disable all interactive prompts\n"); |
cd1f39ab |
2103 | printf(" -unsafe allow server-side wildcards (DANGEROUS)\n"); |
dc108ebc |
2104 | printf(" -V print version information\n"); |
728f4f4c |
2105 | printf(" -sftp force use of SFTP protocol\n"); |
2106 | printf(" -scp force use of SCP protocol\n"); |
ee8b0370 |
2107 | #if 0 |
2108 | /* |
2109 | * -gui is an internal option, used by GUI front ends to get |
2110 | * pscp to pass progress reports back to them. It's not an |
2111 | * ordinary user-accessible option, so it shouldn't be part of |
2112 | * the command-line help. The only people who need to know |
2113 | * about it are programmers, and they can read the source. |
2114 | */ |
32874aea |
2115 | printf |
2116 | (" -gui hWnd GUI mode with the windows handle for receiving messages\n"); |
ee8b0370 |
2117 | #endif |
93b581bd |
2118 | cleanup_exit(1); |
07d9aa13 |
2119 | } |
2120 | |
dc108ebc |
2121 | void version(void) |
2122 | { |
2123 | printf("pscp: %s\n", ver); |
2124 | cleanup_exit(1); |
2125 | } |
2126 | |
c0a81592 |
2127 | void cmdline_error(char *p, ...) |
2128 | { |
2129 | va_list ap; |
2130 | fprintf(stderr, "pscp: "); |
2131 | va_start(ap, p); |
2132 | vfprintf(stderr, p, ap); |
2133 | va_end(ap); |
86256dc6 |
2134 | fprintf(stderr, "\n try typing just \"pscp\" for help\n"); |
c0a81592 |
2135 | exit(1); |
2136 | } |
2137 | |
07d9aa13 |
2138 | /* |
799dfcfa |
2139 | * Main program. (Called `psftp_main' because it gets called from |
2140 | * *sftp.c; bit silly, I know, but it had to be called _something_.) |
07d9aa13 |
2141 | */ |
799dfcfa |
2142 | int psftp_main(int argc, char *argv[]) |
07d9aa13 |
2143 | { |
c51a56e2 |
2144 | int i; |
2145 | |
fb09bf1c |
2146 | default_protocol = PROT_TELNET; |
2147 | |
799dfcfa |
2148 | flags = FLAG_STDERR |
2149 | #ifdef FLAG_SYNCAGENT |
2150 | | FLAG_SYNCAGENT |
2151 | #endif |
2152 | ; |
c0a81592 |
2153 | cmdline_tooltype = TOOLTYPE_FILETRANSFER; |
ff2ae367 |
2154 | ssh_get_line = &console_get_line; |
8df7a775 |
2155 | sk_init(); |
c51a56e2 |
2156 | |
2157 | for (i = 1; i < argc; i++) { |
c0a81592 |
2158 | int ret; |
c51a56e2 |
2159 | if (argv[i][0] != '-') |
2160 | break; |
5555d393 |
2161 | ret = cmdline_process_param(argv[i], i+1<argc?argv[i+1]:NULL, 1, &cfg); |
c0a81592 |
2162 | if (ret == -2) { |
2163 | cmdline_error("option \"%s\" requires an argument", argv[i]); |
2164 | } else if (ret == 2) { |
2165 | i++; /* skip next argument */ |
2166 | } else if (ret == 1) { |
2167 | /* We have our own verbosity in addition to `flags'. */ |
2168 | if (flags & FLAG_VERBOSE) |
2169 | verbose = 1; |
2170 | } else if (strcmp(argv[i], "-r") == 0) { |
c51a56e2 |
2171 | recursive = 1; |
c0a81592 |
2172 | } else if (strcmp(argv[i], "-p") == 0) { |
c51a56e2 |
2173 | preserve = 1; |
c0a81592 |
2174 | } else if (strcmp(argv[i], "-q") == 0) { |
c51a56e2 |
2175 | statistics = 0; |
c0a81592 |
2176 | } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0) { |
c51a56e2 |
2177 | usage(); |
dc108ebc |
2178 | } else if (strcmp(argv[i], "-V") == 0) { |
2179 | version(); |
c0a81592 |
2180 | } else if (strcmp(argv[i], "-gui") == 0 && i + 1 < argc) { |
799dfcfa |
2181 | gui_enable(argv[++i]); |
cc87246d |
2182 | gui_mode = 1; |
ff2ae367 |
2183 | console_batch_mode = TRUE; |
c0a81592 |
2184 | } else if (strcmp(argv[i], "-ls") == 0) { |
32874aea |
2185 | list = 1; |
c0a81592 |
2186 | } else if (strcmp(argv[i], "-batch") == 0) { |
2187 | console_batch_mode = 1; |
2188 | } else if (strcmp(argv[i], "-unsafe") == 0) { |
cd1f39ab |
2189 | scp_unsafe_mode = 1; |
728f4f4c |
2190 | } else if (strcmp(argv[i], "-sftp") == 0) { |
2191 | try_scp = 0; try_sftp = 1; |
2192 | } else if (strcmp(argv[i], "-scp") == 0) { |
2193 | try_scp = 1; try_sftp = 0; |
c0a81592 |
2194 | } else if (strcmp(argv[i], "--") == 0) { |
32874aea |
2195 | i++; |
2196 | break; |
86256dc6 |
2197 | } else { |
2198 | cmdline_error("unknown option \"%s\"", argv[i]); |
2199 | } |
c51a56e2 |
2200 | } |
2201 | argc -= i; |
2202 | argv += i; |
eba78553 |
2203 | back = NULL; |
c51a56e2 |
2204 | |
39ddf0ff |
2205 | if (list) { |
2206 | if (argc != 1) |
2207 | usage(); |
2208 | get_dir_list(argc, argv); |
c51a56e2 |
2209 | |
39ddf0ff |
2210 | } else { |
2211 | |
2212 | if (argc < 2) |
2213 | usage(); |
2214 | if (argc > 2) |
2215 | targetshouldbedirectory = 1; |
2216 | |
32874aea |
2217 | if (colon(argv[argc - 1]) != NULL) |
39ddf0ff |
2218 | toremote(argc, argv); |
2219 | else |
2220 | tolocal(argc, argv); |
2221 | } |
c51a56e2 |
2222 | |
51470298 |
2223 | if (back != NULL && back->socket(backhandle) != NULL) { |
c51a56e2 |
2224 | char ch; |
51470298 |
2225 | back->special(backhandle, TS_EOF); |
776792d7 |
2226 | ssh_scp_recv((unsigned char *) &ch, 1); |
c51a56e2 |
2227 | } |
c51a56e2 |
2228 | random_save_seed(); |
07d9aa13 |
2229 | |
799dfcfa |
2230 | if (gui_mode) |
2231 | gui_send_errcount(list, errs); |
2232 | |
679539d7 |
2233 | cmdline_cleanup(); |
2234 | console_provide_logctx(NULL); |
2235 | back->free(backhandle); |
2236 | backhandle = NULL; |
2237 | back = NULL; |
2238 | sk_cleanup(); |
c51a56e2 |
2239 | return (errs == 0 ? 0 : 1); |
07d9aa13 |
2240 | } |
2241 | |
2242 | /* end */ |