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