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