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