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