07d9aa13 |
1 | /* |
2 | * scp.c - Scp (Secure Copy) client for PuTTY. |
fb09bf1c |
3 | * Joris van Rantwijk, Simon Tatham |
07d9aa13 |
4 | * |
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. |
cc87246d |
7 | * |
8 | * Adaptations to enable connecting a GUI by L. Gunnarsson - Sept 2000 |
07d9aa13 |
9 | */ |
10 | |
07d9aa13 |
11 | #include <windows.h> |
4d331a77 |
12 | #ifndef AUTO_WINSOCK |
13 | #ifdef WINSOCK_TWO |
14 | #include <winsock2.h> |
15 | #else |
07d9aa13 |
16 | #include <winsock.h> |
4d331a77 |
17 | #endif |
18 | #endif |
07d9aa13 |
19 | #include <stdlib.h> |
20 | #include <stdio.h> |
21 | #include <string.h> |
22 | #include <time.h> |
feb7fdfe |
23 | #include <assert.h> |
cc87246d |
24 | /* GUI Adaptation - Sept 2000 */ |
25 | #include <winuser.h> |
26 | #include <winbase.h> |
07d9aa13 |
27 | |
28 | #define PUTTY_DO_GLOBALS |
29 | #include "putty.h" |
8c3cd914 |
30 | #include "winstuff.h" |
a9422f39 |
31 | #include "storage.h" |
07d9aa13 |
32 | |
07d9aa13 |
33 | #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \ |
c51a56e2 |
34 | ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000) |
07d9aa13 |
35 | #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \ |
c51a56e2 |
36 | ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600)) |
07d9aa13 |
37 | |
cc87246d |
38 | /* GUI Adaptation - Sept 2000 */ |
39 | #define WM_APP_BASE 0x8000 |
40 | #define WM_STD_OUT_CHAR ( WM_APP_BASE+400 ) |
41 | #define WM_STD_ERR_CHAR ( WM_APP_BASE+401 ) |
42 | #define WM_STATS_CHAR ( WM_APP_BASE+402 ) |
43 | #define WM_STATS_SIZE ( WM_APP_BASE+403 ) |
44 | #define WM_STATS_PERCENT ( WM_APP_BASE+404 ) |
45 | #define WM_STATS_ELAPSED ( WM_APP_BASE+405 ) |
46 | #define WM_RET_ERR_CNT ( WM_APP_BASE+406 ) |
47 | #define WM_LS_RET_ERR_CNT ( WM_APP_BASE+407 ) |
48 | |
2bc6a386 |
49 | static int list = 0; |
fb09bf1c |
50 | static int verbose = 0; |
07d9aa13 |
51 | static int recursive = 0; |
52 | static int preserve = 0; |
53 | static int targetshouldbedirectory = 0; |
54 | static int statistics = 1; |
b8a19193 |
55 | static int portnumber = 0; |
b1daf518 |
56 | static int prev_stats_len = 0; |
b8a19193 |
57 | static char *password = NULL; |
07d9aa13 |
58 | static int errs = 0; |
cc87246d |
59 | /* GUI Adaptation - Sept 2000 */ |
60 | #define NAME_STR_MAX 2048 |
32874aea |
61 | static char statname[NAME_STR_MAX + 1]; |
cc87246d |
62 | static unsigned long statsize = 0; |
63 | static int statperct = 0; |
90a14a09 |
64 | static unsigned long statelapsed = 0; |
cc87246d |
65 | static int gui_mode = 0; |
66 | static char *gui_hwnd = NULL; |
07d9aa13 |
67 | |
68 | static void source(char *src); |
69 | static void rsource(char *src); |
ca2d5943 |
70 | static void sink(char *targ, char *src); |
cc87246d |
71 | /* GUI Adaptation - Sept 2000 */ |
32874aea |
72 | static void tell_char(FILE * stream, char c); |
73 | static void tell_str(FILE * stream, char *str); |
74 | static void tell_user(FILE * stream, char *fmt, ...); |
90a14a09 |
75 | static void gui_update_stats(char *name, unsigned long size, |
32874aea |
76 | int percentage, unsigned long elapsed); |
07d9aa13 |
77 | |
5471d09a |
78 | /* |
79 | * The maximum amount of queued data we accept before we stop and |
80 | * wait for the server to process some. |
81 | */ |
82 | #define MAX_SCP_BUFSIZE 16384 |
83 | |
32874aea |
84 | void logevent(char *string) |
85 | { |
86 | } |
a9422f39 |
87 | |
32874aea |
88 | void ldisc_send(char *buf, int len) |
89 | { |
feb7fdfe |
90 | /* |
91 | * This is only here because of the calls to ldisc_send(NULL, |
92 | * 0) in ssh.c. Nothing in PSCP actually needs to use the ldisc |
93 | * as an ldisc. So if we get called with any real data, I want |
94 | * to know about it. |
95 | */ |
96 | assert(len == 0); |
97 | } |
98 | |
a9422f39 |
99 | void verify_ssh_host_key(char *host, int port, char *keytype, |
32874aea |
100 | char *keystr, char *fingerprint) |
101 | { |
a9422f39 |
102 | int ret; |
d0718310 |
103 | HANDLE hin; |
104 | DWORD savemode, i; |
a9422f39 |
105 | |
106 | static const char absentmsg[] = |
32874aea |
107 | "The server's host key is not cached in the registry. You\n" |
108 | "have no guarantee that the server is the computer you\n" |
109 | "think it is.\n" |
110 | "The server's key fingerprint is:\n" |
111 | "%s\n" |
112 | "If you trust this host, enter \"y\" to add the key to\n" |
113 | "PuTTY's cache and carry on connecting.\n" |
d0718310 |
114 | "If you want to carry on connecting just once, without\n" |
115 | "adding the key to the cache, enter \"n\".\n" |
116 | "If you do not trust this host, press Return to abandon the\n" |
117 | "connection.\n" |
118 | "Store key in cache? (y/n) "; |
a9422f39 |
119 | |
120 | static const char wrongmsg[] = |
32874aea |
121 | "WARNING - POTENTIAL SECURITY BREACH!\n" |
122 | "The server's host key does not match the one PuTTY has\n" |
123 | "cached in the registry. This means that either the\n" |
124 | "server administrator has changed the host key, or you\n" |
125 | "have actually connected to another computer pretending\n" |
126 | "to be the server.\n" |
127 | "The new key fingerprint is:\n" |
128 | "%s\n" |
129 | "If you were expecting this change and trust the new key,\n" |
d0718310 |
130 | "enter \"y\" to update PuTTY's cache and continue connecting.\n" |
32874aea |
131 | "If you want to carry on connecting but without updating\n" |
d0718310 |
132 | "the cache, enter \"n\".\n" |
32874aea |
133 | "If you want to abandon the connection completely, press\n" |
134 | "Return to cancel. Pressing Return is the ONLY guaranteed\n" |
135 | "safe choice.\n" |
136 | "Update cached key? (y/n, Return cancels connection) "; |
a9422f39 |
137 | |
138 | static const char abandoned[] = "Connection abandoned.\n"; |
139 | |
140 | char line[32]; |
141 | |
142 | /* |
143 | * Verify the key against the registry. |
144 | */ |
145 | ret = verify_host_key(host, port, keytype, keystr); |
146 | |
32874aea |
147 | if (ret == 0) /* success - key matched OK */ |
148 | return; |
d0718310 |
149 | |
32874aea |
150 | if (ret == 2) { /* key was different */ |
151 | fprintf(stderr, wrongmsg, fingerprint); |
b4453f49 |
152 | fflush(stderr); |
a9422f39 |
153 | } |
32874aea |
154 | if (ret == 1) { /* key was absent */ |
155 | fprintf(stderr, absentmsg, fingerprint); |
d0718310 |
156 | fflush(stderr); |
157 | } |
158 | |
159 | hin = GetStdHandle(STD_INPUT_HANDLE); |
160 | GetConsoleMode(hin, &savemode); |
161 | SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT | |
162 | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT)); |
163 | ReadFile(hin, line, sizeof(line) - 1, &i, NULL); |
164 | SetConsoleMode(hin, savemode); |
165 | |
166 | if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') { |
167 | if (line[0] == 'y' || line[0] == 'Y') |
32874aea |
168 | store_host_key(host, port, keytype, keystr); |
d0718310 |
169 | } else { |
170 | fprintf(stderr, abandoned); |
171 | exit(0); |
a9422f39 |
172 | } |
173 | } |
fb09bf1c |
174 | |
ca20bfcf |
175 | /* |
176 | * Ask whether the selected cipher is acceptable (since it was |
177 | * below the configured 'warn' threshold). |
178 | * cs: 0 = both ways, 1 = client->server, 2 = server->client |
179 | */ |
180 | void askcipher(char *ciphername, int cs) |
181 | { |
182 | HANDLE hin; |
183 | DWORD savemode, i; |
184 | |
185 | static const char msg[] = |
186 | "The first %scipher supported by the server is\n" |
187 | "%s, which is below the configured warning threshold.\n" |
188 | "Continue with connection? (y/n) "; |
189 | static const char abandoned[] = "Connection abandoned.\n"; |
190 | |
191 | char line[32]; |
192 | |
193 | fprintf(stderr, msg, |
194 | (cs == 0) ? "" : |
195 | (cs == 1) ? "client-to-server " : |
196 | "server-to-client ", |
197 | ciphername); |
198 | fflush(stderr); |
199 | |
200 | hin = GetStdHandle(STD_INPUT_HANDLE); |
201 | GetConsoleMode(hin, &savemode); |
202 | SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT | |
203 | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT)); |
204 | ReadFile(hin, line, sizeof(line) - 1, &i, NULL); |
205 | SetConsoleMode(hin, savemode); |
206 | |
207 | if (line[0] == 'y' || line[0] == 'Y') { |
208 | return; |
209 | } else { |
210 | fprintf(stderr, abandoned); |
211 | exit(0); |
212 | } |
213 | } |
214 | |
cc87246d |
215 | /* GUI Adaptation - Sept 2000 */ |
75cab814 |
216 | static void send_msg(HWND h, UINT message, WPARAM wParam) |
cc87246d |
217 | { |
32874aea |
218 | while (!PostMessage(h, message, wParam, 0)) |
219 | SleepEx(1000, TRUE); |
cc87246d |
220 | } |
221 | |
32874aea |
222 | static void tell_char(FILE * stream, char c) |
cc87246d |
223 | { |
224 | if (!gui_mode) |
225 | fputc(c, stream); |
32874aea |
226 | else { |
cc87246d |
227 | unsigned int msg_id = WM_STD_OUT_CHAR; |
32874aea |
228 | if (stream == stderr) |
229 | msg_id = WM_STD_ERR_CHAR; |
230 | send_msg((HWND) atoi(gui_hwnd), msg_id, (WPARAM) c); |
cc87246d |
231 | } |
232 | } |
233 | |
32874aea |
234 | static void tell_str(FILE * stream, char *str) |
cc87246d |
235 | { |
236 | unsigned int i; |
237 | |
32874aea |
238 | for (i = 0; i < strlen(str); ++i) |
cc87246d |
239 | tell_char(stream, str[i]); |
240 | } |
241 | |
32874aea |
242 | static void tell_user(FILE * stream, char *fmt, ...) |
cc87246d |
243 | { |
32874aea |
244 | char str[0x100]; /* Make the size big enough */ |
cc87246d |
245 | va_list ap; |
246 | va_start(ap, fmt); |
247 | vsprintf(str, fmt, ap); |
248 | va_end(ap); |
249 | strcat(str, "\n"); |
250 | tell_str(stream, str); |
251 | } |
252 | |
32874aea |
253 | static void gui_update_stats(char *name, unsigned long size, |
254 | int percentage, unsigned long elapsed) |
cc87246d |
255 | { |
256 | unsigned int i; |
257 | |
32874aea |
258 | if (strcmp(name, statname) != 0) { |
259 | for (i = 0; i < strlen(name); ++i) |
260 | send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR, |
261 | (WPARAM) name[i]); |
262 | send_msg((HWND) atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM) '\n'); |
263 | strcpy(statname, name); |
cc87246d |
264 | } |
32874aea |
265 | if (statsize != size) { |
266 | send_msg((HWND) atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM) size); |
cc87246d |
267 | statsize = size; |
268 | } |
32874aea |
269 | if (statelapsed != elapsed) { |
270 | send_msg((HWND) atoi(gui_hwnd), WM_STATS_ELAPSED, |
271 | (WPARAM) elapsed); |
cc87246d |
272 | statelapsed = elapsed; |
273 | } |
32874aea |
274 | if (statperct != percentage) { |
275 | send_msg((HWND) atoi(gui_hwnd), WM_STATS_PERCENT, |
276 | (WPARAM) percentage); |
cc87246d |
277 | statperct = percentage; |
278 | } |
279 | } |
280 | |
fb09bf1c |
281 | /* |
07d9aa13 |
282 | * Print an error message and perform a fatal exit. |
283 | */ |
284 | void fatalbox(char *fmt, ...) |
285 | { |
32874aea |
286 | char str[0x100]; /* Make the size big enough */ |
c51a56e2 |
287 | va_list ap; |
288 | va_start(ap, fmt); |
120e4b40 |
289 | strcpy(str, "Fatal: "); |
32874aea |
290 | vsprintf(str + strlen(str), fmt, ap); |
c51a56e2 |
291 | va_end(ap); |
cc87246d |
292 | strcat(str, "\n"); |
293 | tell_str(stderr, str); |
2bc6a386 |
294 | errs++; |
295 | |
296 | if (gui_mode) { |
297 | unsigned int msg_id = WM_RET_ERR_CNT; |
298 | if (list) |
299 | msg_id = WM_LS_RET_ERR_CNT; |
300 | while (!PostMessage |
301 | ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs, |
302 | 0 /*lParam */ ))SleepEx(1000, TRUE); |
303 | } |
cc87246d |
304 | |
c51a56e2 |
305 | exit(1); |
07d9aa13 |
306 | } |
8d5de777 |
307 | void connection_fatal(char *fmt, ...) |
308 | { |
32874aea |
309 | char str[0x100]; /* Make the size big enough */ |
8d5de777 |
310 | va_list ap; |
311 | va_start(ap, fmt); |
120e4b40 |
312 | strcpy(str, "Fatal: "); |
32874aea |
313 | vsprintf(str + strlen(str), fmt, ap); |
8d5de777 |
314 | va_end(ap); |
315 | strcat(str, "\n"); |
316 | tell_str(stderr, str); |
2bc6a386 |
317 | errs++; |
318 | |
319 | if (gui_mode) { |
320 | unsigned int msg_id = WM_RET_ERR_CNT; |
321 | if (list) |
322 | msg_id = WM_LS_RET_ERR_CNT; |
323 | while (!PostMessage |
324 | ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs, |
325 | 0 /*lParam */ ))SleepEx(1000, TRUE); |
326 | } |
8d5de777 |
327 | |
328 | exit(1); |
329 | } |
07d9aa13 |
330 | |
07d9aa13 |
331 | /* |
8df7a775 |
332 | * Be told what socket we're supposed to be using. |
333 | */ |
334 | static SOCKET scp_ssh_socket; |
32874aea |
335 | char *do_select(SOCKET skt, int startup) |
336 | { |
8df7a775 |
337 | if (startup) |
338 | scp_ssh_socket = skt; |
339 | else |
340 | scp_ssh_socket = INVALID_SOCKET; |
341 | return NULL; |
342 | } |
343 | extern int select_result(WPARAM, LPARAM); |
344 | |
345 | /* |
3bdaf79d |
346 | * Receive a block of data from the SSH link. Block until all data |
347 | * is available. |
348 | * |
349 | * To do this, we repeatedly call the SSH protocol module, with our |
fe50e814 |
350 | * own trap in from_backend() to catch the data that comes back. We |
351 | * do this until we have enough data. |
3bdaf79d |
352 | */ |
8df7a775 |
353 | |
32874aea |
354 | static unsigned char *outptr; /* where to put the data */ |
355 | static unsigned outlen; /* how much data required */ |
3bdaf79d |
356 | static unsigned char *pending = NULL; /* any spare data */ |
32874aea |
357 | static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */ |
5471d09a |
358 | int from_backend(int is_stderr, char *data, int datalen) |
32874aea |
359 | { |
360 | unsigned char *p = (unsigned char *) data; |
361 | unsigned len = (unsigned) datalen; |
fe50e814 |
362 | |
3bdaf79d |
363 | /* |
fe50e814 |
364 | * stderr data is just spouted to local stderr and otherwise |
365 | * ignored. |
3bdaf79d |
366 | */ |
fe50e814 |
367 | if (is_stderr) { |
368 | fwrite(data, 1, len, stderr); |
5471d09a |
369 | return 0; |
fe50e814 |
370 | } |
3bdaf79d |
371 | |
372 | inbuf_head = 0; |
373 | |
374 | /* |
375 | * If this is before the real session begins, just return. |
376 | */ |
377 | if (!outptr) |
5471d09a |
378 | return 0; |
3bdaf79d |
379 | |
380 | if (outlen > 0) { |
32874aea |
381 | unsigned used = outlen; |
382 | if (used > len) |
383 | used = len; |
384 | memcpy(outptr, p, used); |
385 | outptr += used; |
386 | outlen -= used; |
387 | p += used; |
388 | len -= used; |
3bdaf79d |
389 | } |
390 | |
391 | if (len > 0) { |
32874aea |
392 | if (pendsize < pendlen + len) { |
393 | pendsize = pendlen + len + 4096; |
394 | pending = (pending ? srealloc(pending, pendsize) : |
395 | smalloc(pendsize)); |
396 | if (!pending) |
397 | fatalbox("Out of memory"); |
398 | } |
399 | memcpy(pending + pendlen, p, len); |
400 | pendlen += len; |
3bdaf79d |
401 | } |
5471d09a |
402 | |
403 | return 0; |
404 | } |
405 | static int scp_process_network_event(void) |
406 | { |
407 | fd_set readfds; |
408 | |
409 | FD_ZERO(&readfds); |
410 | FD_SET(scp_ssh_socket, &readfds); |
411 | if (select(1, &readfds, NULL, NULL, NULL) < 0) |
412 | return 0; /* doom */ |
413 | select_result((WPARAM) scp_ssh_socket, (LPARAM) FD_READ); |
414 | return 1; |
3bdaf79d |
415 | } |
32874aea |
416 | static int ssh_scp_recv(unsigned char *buf, int len) |
417 | { |
3bdaf79d |
418 | outptr = buf; |
419 | outlen = len; |
420 | |
421 | /* |
422 | * See if the pending-input block contains some of what we |
423 | * need. |
424 | */ |
425 | if (pendlen > 0) { |
32874aea |
426 | unsigned pendused = pendlen; |
427 | if (pendused > outlen) |
428 | pendused = outlen; |
3bdaf79d |
429 | memcpy(outptr, pending, pendused); |
32874aea |
430 | memmove(pending, pending + pendused, pendlen - pendused); |
3bdaf79d |
431 | outptr += pendused; |
432 | outlen -= pendused; |
32874aea |
433 | pendlen -= pendused; |
434 | if (pendlen == 0) { |
435 | pendsize = 0; |
436 | sfree(pending); |
437 | pending = NULL; |
438 | } |
439 | if (outlen == 0) |
440 | return len; |
3bdaf79d |
441 | } |
442 | |
443 | while (outlen > 0) { |
5471d09a |
444 | if (!scp_process_network_event()) |
32874aea |
445 | return 0; /* doom */ |
3bdaf79d |
446 | } |
447 | |
448 | return len; |
449 | } |
450 | |
451 | /* |
452 | * Loop through the ssh connection and authentication process. |
453 | */ |
32874aea |
454 | static void ssh_scp_init(void) |
455 | { |
8df7a775 |
456 | if (scp_ssh_socket == INVALID_SOCKET) |
3bdaf79d |
457 | return; |
458 | while (!back->sendok()) { |
32874aea |
459 | fd_set readfds; |
460 | FD_ZERO(&readfds); |
461 | FD_SET(scp_ssh_socket, &readfds); |
462 | if (select(1, &readfds, NULL, NULL, NULL) < 0) |
463 | return; /* doom */ |
464 | select_result((WPARAM) scp_ssh_socket, (LPARAM) FD_READ); |
3bdaf79d |
465 | } |
466 | } |
467 | |
468 | /* |
07d9aa13 |
469 | * Print an error message and exit after closing the SSH link. |
470 | */ |
471 | static void bump(char *fmt, ...) |
472 | { |
32874aea |
473 | char str[0x100]; /* Make the size big enough */ |
c51a56e2 |
474 | va_list ap; |
475 | va_start(ap, fmt); |
120e4b40 |
476 | strcpy(str, "Fatal: "); |
32874aea |
477 | vsprintf(str + strlen(str), fmt, ap); |
c51a56e2 |
478 | va_end(ap); |
cc87246d |
479 | strcat(str, "\n"); |
480 | tell_str(stderr, str); |
2bc6a386 |
481 | errs++; |
cc87246d |
482 | |
eba78553 |
483 | if (back != NULL && back->socket() != NULL) { |
c51a56e2 |
484 | char ch; |
3bdaf79d |
485 | back->special(TS_EOF); |
fb09bf1c |
486 | ssh_scp_recv(&ch, 1); |
c51a56e2 |
487 | } |
2bc6a386 |
488 | |
489 | if (gui_mode) { |
490 | unsigned int msg_id = WM_RET_ERR_CNT; |
491 | if (list) |
492 | msg_id = WM_LS_RET_ERR_CNT; |
493 | while (!PostMessage |
494 | ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs, |
495 | 0 /*lParam */ ))SleepEx(1000, TRUE); |
496 | } |
497 | |
c51a56e2 |
498 | exit(1); |
07d9aa13 |
499 | } |
500 | |
fa17a66e |
501 | static int get_line(const char *prompt, char *str, int maxlen, int is_pw) |
07d9aa13 |
502 | { |
c51a56e2 |
503 | HANDLE hin, hout; |
fa17a66e |
504 | DWORD savemode, newmode, i; |
b8a19193 |
505 | |
fa17a66e |
506 | if (is_pw && password) { |
32874aea |
507 | static int tried_once = 0; |
508 | |
509 | if (tried_once) { |
510 | return 0; |
511 | } else { |
512 | strncpy(str, password, maxlen); |
513 | str[maxlen - 1] = '\0'; |
514 | tried_once = 1; |
515 | return 1; |
516 | } |
b8a19193 |
517 | } |
07d9aa13 |
518 | |
cc87246d |
519 | /* GUI Adaptation - Sept 2000 */ |
520 | if (gui_mode) { |
32874aea |
521 | if (maxlen > 0) |
522 | str[0] = '\0'; |
cc87246d |
523 | } else { |
524 | hin = GetStdHandle(STD_INPUT_HANDLE); |
525 | hout = GetStdHandle(STD_OUTPUT_HANDLE); |
526 | if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) |
527 | bump("Cannot get standard input/output handles"); |
07d9aa13 |
528 | |
cc87246d |
529 | GetConsoleMode(hin, &savemode); |
32874aea |
530 | newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT; |
531 | if (is_pw) |
532 | newmode &= ~ENABLE_ECHO_INPUT; |
533 | else |
534 | newmode |= ENABLE_ECHO_INPUT; |
535 | SetConsoleMode(hin, newmode); |
07d9aa13 |
536 | |
cc87246d |
537 | WriteFile(hout, prompt, strlen(prompt), &i, NULL); |
32874aea |
538 | ReadFile(hin, str, maxlen - 1, &i, NULL); |
07d9aa13 |
539 | |
cc87246d |
540 | SetConsoleMode(hin, savemode); |
07d9aa13 |
541 | |
32874aea |
542 | if ((int) i > maxlen) |
543 | i = maxlen - 1; |
544 | else |
545 | i = i - 2; |
cc87246d |
546 | str[i] = '\0'; |
07d9aa13 |
547 | |
fa17a66e |
548 | if (is_pw) |
32874aea |
549 | WriteFile(hout, "\r\n", 2, &i, NULL); |
cc87246d |
550 | } |
85ee8208 |
551 | |
552 | return 1; |
07d9aa13 |
553 | } |
554 | |
07d9aa13 |
555 | /* |
556 | * Open an SSH connection to user@host and execute cmd. |
557 | */ |
558 | static void do_cmd(char *host, char *user, char *cmd) |
559 | { |
c51a56e2 |
560 | char *err, *realhost; |
f5e6a5c6 |
561 | DWORD namelen; |
c51a56e2 |
562 | |
563 | if (host == NULL || host[0] == '\0') |
564 | bump("Empty host name"); |
565 | |
566 | /* Try to load settings for this host */ |
a9422f39 |
567 | do_defaults(host, &cfg); |
c51a56e2 |
568 | if (cfg.host[0] == '\0') { |
569 | /* No settings for this host; use defaults */ |
32874aea |
570 | do_defaults(NULL, &cfg); |
571 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
572 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
c51a56e2 |
573 | cfg.port = 22; |
574 | } |
575 | |
576 | /* Set username */ |
577 | if (user != NULL && user[0] != '\0') { |
32874aea |
578 | strncpy(cfg.username, user, sizeof(cfg.username) - 1); |
579 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
c51a56e2 |
580 | } else if (cfg.username[0] == '\0') { |
f5e6a5c6 |
581 | namelen = 0; |
582 | if (GetUserName(user, &namelen) == FALSE) |
583 | bump("Empty user name"); |
93558b21 |
584 | user = smalloc(namelen * sizeof(char)); |
f5e6a5c6 |
585 | GetUserName(user, &namelen); |
32874aea |
586 | if (verbose) |
587 | tell_user(stderr, "Guessing user name: %s", user); |
588 | strncpy(cfg.username, user, sizeof(cfg.username) - 1); |
589 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
f5e6a5c6 |
590 | free(user); |
c51a56e2 |
591 | } |
592 | |
593 | if (cfg.protocol != PROT_SSH) |
594 | cfg.port = 22; |
595 | |
ed89e8a5 |
596 | if (portnumber) |
597 | cfg.port = portnumber; |
598 | |
3bdaf79d |
599 | strncpy(cfg.remote_cmd, cmd, sizeof(cfg.remote_cmd)); |
32874aea |
600 | cfg.remote_cmd[sizeof(cfg.remote_cmd) - 1] = '\0'; |
3bdaf79d |
601 | cfg.nopty = TRUE; |
602 | |
603 | back = &ssh_backend; |
604 | |
8df7a775 |
605 | err = back->init(cfg.host, cfg.port, &realhost); |
c51a56e2 |
606 | if (err != NULL) |
607 | bump("ssh_init: %s", err); |
3bdaf79d |
608 | ssh_scp_init(); |
c51a56e2 |
609 | if (verbose && realhost != NULL) |
cc87246d |
610 | tell_user(stderr, "Connected to %s\n", realhost); |
6e1ebb76 |
611 | sfree(realhost); |
07d9aa13 |
612 | } |
613 | |
07d9aa13 |
614 | /* |
615 | * Update statistic information about current file. |
616 | */ |
617 | static void print_stats(char *name, unsigned long size, unsigned long done, |
32874aea |
618 | time_t start, time_t now) |
07d9aa13 |
619 | { |
c51a56e2 |
620 | float ratebs; |
621 | unsigned long eta; |
622 | char etastr[10]; |
623 | int pct; |
b1daf518 |
624 | int len; |
c51a56e2 |
625 | |
cc87246d |
626 | /* GUI Adaptation - Sept 2000 */ |
627 | if (gui_mode) |
32874aea |
628 | gui_update_stats(name, size, (int) (100 * (done * 1.0 / size)), |
629 | (unsigned long) difftime(now, start)); |
cc87246d |
630 | else { |
631 | if (now > start) |
632 | ratebs = (float) done / (now - start); |
633 | else |
634 | ratebs = (float) done; |
c51a56e2 |
635 | |
cc87246d |
636 | if (ratebs < 1.0) |
637 | eta = size - done; |
638 | else |
639 | eta = (unsigned long) ((size - done) / ratebs); |
640 | sprintf(etastr, "%02ld:%02ld:%02ld", |
641 | eta / 3600, (eta % 3600) / 60, eta % 60); |
c51a56e2 |
642 | |
cc87246d |
643 | pct = (int) (100.0 * (float) done / size); |
c51a56e2 |
644 | |
b1daf518 |
645 | len = printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%", |
646 | name, done / 1024, ratebs / 1024.0, etastr, pct); |
647 | if (len < prev_stats_len) |
648 | printf("%*s", prev_stats_len - len, ""); |
649 | prev_stats_len = len; |
c51a56e2 |
650 | |
cc87246d |
651 | if (done == size) |
652 | printf("\n"); |
653 | } |
07d9aa13 |
654 | } |
655 | |
07d9aa13 |
656 | /* |
657 | * Find a colon in str and return a pointer to the colon. |
39ddf0ff |
658 | * This is used to separate hostname from filename. |
07d9aa13 |
659 | */ |
32874aea |
660 | static char *colon(char *str) |
07d9aa13 |
661 | { |
c51a56e2 |
662 | /* We ignore a leading colon, since the hostname cannot be |
32874aea |
663 | empty. We also ignore a colon as second character because |
664 | of filenames like f:myfile.txt. */ |
665 | if (str[0] == '\0' || str[0] == ':' || str[1] == ':') |
c51a56e2 |
666 | return (NULL); |
32874aea |
667 | while (*str != '\0' && *str != ':' && *str != '/' && *str != '\\') |
c51a56e2 |
668 | str++; |
669 | if (*str == ':') |
670 | return (str); |
671 | else |
672 | return (NULL); |
07d9aa13 |
673 | } |
674 | |
07d9aa13 |
675 | /* |
03f64569 |
676 | * Return a pointer to the portion of str that comes after the last |
677 | * slash or backslash. |
678 | */ |
679 | static char *stripslashes(char *str) |
680 | { |
681 | char *p; |
682 | |
683 | p = strrchr(str, '/'); |
684 | if (p) str = p+1; |
685 | |
686 | p = strrchr(str, '\\'); |
687 | if (p) str = p+1; |
688 | |
689 | return str; |
690 | } |
691 | |
692 | /* |
07d9aa13 |
693 | * Wait for a response from the other side. |
694 | * Return 0 if ok, -1 if error. |
695 | */ |
696 | static int response(void) |
697 | { |
c51a56e2 |
698 | char ch, resp, rbuf[2048]; |
699 | int p; |
700 | |
fb09bf1c |
701 | if (ssh_scp_recv(&resp, 1) <= 0) |
c51a56e2 |
702 | bump("Lost connection"); |
703 | |
704 | p = 0; |
705 | switch (resp) { |
32874aea |
706 | case 0: /* ok */ |
c51a56e2 |
707 | return (0); |
708 | default: |
709 | rbuf[p++] = resp; |
710 | /* fallthrough */ |
32874aea |
711 | case 1: /* error */ |
712 | case 2: /* fatal error */ |
c51a56e2 |
713 | do { |
fb09bf1c |
714 | if (ssh_scp_recv(&ch, 1) <= 0) |
c51a56e2 |
715 | bump("Protocol error: Lost connection"); |
716 | rbuf[p++] = ch; |
717 | } while (p < sizeof(rbuf) && ch != '\n'); |
32874aea |
718 | rbuf[p - 1] = '\0'; |
c51a56e2 |
719 | if (resp == 1) |
cc87246d |
720 | tell_user(stderr, "%s\n", rbuf); |
c51a56e2 |
721 | else |
722 | bump("%s", rbuf); |
723 | errs++; |
724 | return (-1); |
725 | } |
07d9aa13 |
726 | } |
727 | |
120e4b40 |
728 | /* ---------------------------------------------------------------------- |
729 | * Helper routines that contain the actual SCP protocol elements, |
730 | * so they can be switched to use SFTP. |
731 | */ |
732 | |
733 | int scp_send_errmsg(char *str) |
734 | { |
735 | back->send("\001", 1); /* scp protocol error prefix */ |
736 | back->send(str, strlen(str)); |
737 | return 0; /* can't fail */ |
738 | } |
739 | |
740 | int scp_send_filetimes(unsigned long mtime, unsigned long atime) |
741 | { |
742 | char buf[80]; |
743 | sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime); |
744 | back->send(buf, strlen(buf)); |
745 | return response(); |
746 | } |
747 | |
748 | int scp_send_filename(char *name, unsigned long size, int modes) |
749 | { |
750 | char buf[40]; |
751 | sprintf(buf, "C%04o %lu ", modes, size); |
752 | back->send(buf, strlen(buf)); |
753 | back->send(name, strlen(name)); |
754 | back->send("\n", 1); |
755 | return response(); |
756 | } |
757 | |
758 | int scp_send_filedata(char *data, int len) |
759 | { |
760 | int bufsize = back->send(data, len); |
761 | |
762 | /* |
763 | * If the network transfer is backing up - that is, the remote |
764 | * site is not accepting data as fast as we can produce it - |
765 | * then we must loop on network events until we have space in |
766 | * the buffer again. |
767 | */ |
768 | while (bufsize > MAX_SCP_BUFSIZE) { |
769 | if (!scp_process_network_event()) |
770 | return 1; |
771 | bufsize = back->sendbuffer(); |
772 | } |
773 | |
774 | return 0; |
775 | } |
776 | |
777 | int scp_send_finish(void) |
778 | { |
779 | back->send("", 1); |
780 | return response(); |
781 | } |
782 | |
783 | int scp_send_dirname(char *name, int modes) |
784 | { |
785 | char buf[40]; |
786 | sprintf(buf, "D%04o 0 ", modes); |
787 | back->send(buf, strlen(buf)); |
788 | back->send(name, strlen(name)); |
789 | back->send("\n", 1); |
790 | return response(); |
791 | } |
792 | |
793 | int scp_send_enddir(void) |
794 | { |
795 | back->send("E\n", 2); |
796 | return response(); |
797 | } |
798 | |
799 | int scp_sink_init(void) |
800 | { |
801 | back->send("", 1); |
802 | return 0; |
803 | } |
804 | |
805 | #define SCP_SINK_FILE 1 |
806 | #define SCP_SINK_DIR 2 |
807 | #define SCP_SINK_ENDDIR 3 |
808 | struct scp_sink_action { |
809 | int action; /* FILE, DIR, ENDDIR */ |
810 | char *buf; /* will need freeing after use */ |
811 | char *name; /* filename or dirname (not ENDDIR) */ |
812 | int mode; /* access mode (not ENDDIR) */ |
813 | unsigned long size; /* file size (not ENDDIR) */ |
814 | int settime; /* 1 if atime and mtime are filled */ |
815 | unsigned long atime, mtime; /* access times for the file */ |
816 | }; |
817 | |
818 | int scp_get_sink_action(struct scp_sink_action *act) |
819 | { |
820 | int done = 0; |
821 | int i, bufsize; |
822 | int action; |
823 | char ch; |
824 | |
825 | act->settime = 0; |
826 | act->buf = NULL; |
827 | bufsize = 0; |
828 | |
829 | while (!done) { |
830 | if (ssh_scp_recv(&ch, 1) <= 0) |
831 | return 1; |
832 | if (ch == '\n') |
833 | bump("Protocol error: Unexpected newline"); |
834 | i = 0; |
835 | action = ch; |
836 | do { |
837 | if (ssh_scp_recv(&ch, 1) <= 0) |
838 | bump("Lost connection"); |
839 | if (i >= bufsize) { |
840 | bufsize = i + 128; |
841 | act->buf = srealloc(act->buf, bufsize); |
842 | } |
843 | act->buf[i++] = ch; |
844 | } while (ch != '\n'); |
845 | act->buf[i - 1] = '\0'; |
846 | switch (action) { |
847 | case '\01': /* error */ |
848 | tell_user(stderr, "%s\n", act->buf); |
849 | errs++; |
850 | continue; /* go round again */ |
851 | case '\02': /* fatal error */ |
852 | bump("%s", act->buf); |
853 | case 'E': |
854 | back->send("", 1); |
855 | act->action = SCP_SINK_ENDDIR; |
856 | return 0; |
857 | case 'T': |
858 | if (sscanf(act->buf, "%ld %*d %ld %*d", |
859 | &act->mtime, &act->atime) == 2) { |
860 | act->settime = 1; |
861 | back->send("", 1); |
862 | continue; /* go round again */ |
863 | } |
864 | bump("Protocol error: Illegal time format"); |
865 | case 'C': |
866 | case 'D': |
867 | act->action = (action == 'C' ? SCP_SINK_FILE : SCP_SINK_DIR); |
868 | break; |
869 | default: |
870 | bump("Protocol error: Expected control record"); |
871 | } |
872 | /* |
873 | * We will go round this loop only once, unless we hit |
874 | * `continue' above. |
875 | */ |
876 | done = 1; |
877 | } |
878 | |
879 | /* |
880 | * If we get here, we must have seen SCP_SINK_FILE or |
881 | * SCP_SINK_DIR. |
882 | */ |
883 | if (sscanf(act->buf, "%o %lu %n", &act->mode, &act->size, &i) != 2) |
884 | bump("Protocol error: Illegal file descriptor format"); |
885 | act->name = act->buf + i; |
886 | return 0; |
887 | } |
888 | |
889 | int scp_accept_filexfer(void) |
890 | { |
891 | back->send("", 1); |
892 | return 0; /* can't fail */ |
893 | } |
894 | |
895 | int scp_recv_filedata(char *data, int len) |
896 | { |
897 | return ssh_scp_recv(data, len); |
898 | } |
899 | |
900 | int scp_finish_filerecv(void) |
901 | { |
902 | back->send("", 1); |
903 | return response(); |
904 | } |
905 | |
906 | /* ---------------------------------------------------------------------- |
07d9aa13 |
907 | * Send an error message to the other side and to the screen. |
908 | * Increment error counter. |
909 | */ |
910 | static void run_err(const char *fmt, ...) |
911 | { |
c51a56e2 |
912 | char str[2048]; |
913 | va_list ap; |
914 | va_start(ap, fmt); |
915 | errs++; |
9520eba8 |
916 | strcpy(str, "scp: "); |
32874aea |
917 | vsprintf(str + strlen(str), fmt, ap); |
c51a56e2 |
918 | strcat(str, "\n"); |
120e4b40 |
919 | scp_send_errmsg(str); |
32874aea |
920 | tell_user(stderr, "%s", str); |
c51a56e2 |
921 | va_end(ap); |
07d9aa13 |
922 | } |
923 | |
07d9aa13 |
924 | /* |
925 | * Execute the source part of the SCP protocol. |
926 | */ |
927 | static void source(char *src) |
928 | { |
c51a56e2 |
929 | unsigned long size; |
930 | char *last; |
931 | HANDLE f; |
932 | DWORD attr; |
933 | unsigned long i; |
934 | unsigned long stat_bytes; |
935 | time_t stat_starttime, stat_lasttime; |
936 | |
937 | attr = GetFileAttributes(src); |
32874aea |
938 | if (attr == (DWORD) - 1) { |
c51a56e2 |
939 | run_err("%s: No such file or directory", src); |
940 | return; |
941 | } |
942 | |
943 | if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
7f1f80de |
944 | if (recursive) { |
32874aea |
945 | /* |
946 | * Avoid . and .. directories. |
947 | */ |
948 | char *p; |
949 | p = strrchr(src, '/'); |
950 | if (!p) |
951 | p = strrchr(src, '\\'); |
952 | if (!p) |
953 | p = src; |
954 | else |
955 | p++; |
956 | if (!strcmp(p, ".") || !strcmp(p, "..")) |
957 | /* skip . and .. */ ; |
958 | else |
959 | rsource(src); |
960 | } else { |
c51a56e2 |
961 | run_err("%s: not a regular file", src); |
32874aea |
962 | } |
c51a56e2 |
963 | return; |
964 | } |
965 | |
966 | if ((last = strrchr(src, '/')) == NULL) |
967 | last = src; |
968 | else |
969 | last++; |
970 | if (strrchr(last, '\\') != NULL) |
971 | last = strrchr(last, '\\') + 1; |
972 | if (last == src && strchr(src, ':') != NULL) |
973 | last = strchr(src, ':') + 1; |
974 | |
975 | f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL, |
976 | OPEN_EXISTING, 0, 0); |
977 | if (f == INVALID_HANDLE_VALUE) { |
486543a1 |
978 | run_err("%s: Cannot open file", src); |
c51a56e2 |
979 | return; |
980 | } |
981 | |
982 | if (preserve) { |
983 | FILETIME actime, wrtime; |
984 | unsigned long mtime, atime; |
985 | GetFileTime(f, NULL, &actime, &wrtime); |
986 | TIME_WIN_TO_POSIX(actime, atime); |
987 | TIME_WIN_TO_POSIX(wrtime, mtime); |
120e4b40 |
988 | if (scp_send_filetimes(mtime, atime)) |
c51a56e2 |
989 | return; |
990 | } |
991 | |
992 | size = GetFileSize(f, NULL); |
c51a56e2 |
993 | if (verbose) |
120e4b40 |
994 | tell_user(stderr, "Sending file %s, size=%lu", last, size); |
995 | if (scp_send_filename(last, size, 0644)) |
c51a56e2 |
996 | return; |
997 | |
2d466ffd |
998 | stat_bytes = 0; |
999 | stat_starttime = time(NULL); |
1000 | stat_lasttime = 0; |
c51a56e2 |
1001 | |
1002 | for (i = 0; i < size; i += 4096) { |
1003 | char transbuf[4096]; |
1004 | DWORD j, k = 4096; |
5471d09a |
1005 | |
32874aea |
1006 | if (i + k > size) |
1007 | k = size - i; |
1008 | if (!ReadFile(f, transbuf, k, &j, NULL) || j != k) { |
1009 | if (statistics) |
1010 | printf("\n"); |
c51a56e2 |
1011 | bump("%s: Read error", src); |
07d9aa13 |
1012 | } |
120e4b40 |
1013 | if (scp_send_filedata(transbuf, k)) |
1014 | bump("%s: Network error occurred", src); |
1015 | |
c51a56e2 |
1016 | if (statistics) { |
1017 | stat_bytes += k; |
32874aea |
1018 | if (time(NULL) != stat_lasttime || i + k == size) { |
c51a56e2 |
1019 | stat_lasttime = time(NULL); |
1020 | print_stats(last, size, stat_bytes, |
1021 | stat_starttime, stat_lasttime); |
1022 | } |
07d9aa13 |
1023 | } |
5471d09a |
1024 | |
c51a56e2 |
1025 | } |
1026 | CloseHandle(f); |
07d9aa13 |
1027 | |
120e4b40 |
1028 | (void) scp_send_finish(); |
07d9aa13 |
1029 | } |
1030 | |
07d9aa13 |
1031 | /* |
1032 | * Recursively send the contents of a directory. |
1033 | */ |
1034 | static void rsource(char *src) |
1035 | { |
03f64569 |
1036 | char *last, *findfile; |
c51a56e2 |
1037 | HANDLE dir; |
1038 | WIN32_FIND_DATA fdat; |
1039 | int ok; |
1040 | |
1041 | if ((last = strrchr(src, '/')) == NULL) |
1042 | last = src; |
1043 | else |
1044 | last++; |
1045 | if (strrchr(last, '\\') != NULL) |
1046 | last = strrchr(last, '\\') + 1; |
1047 | if (last == src && strchr(src, ':') != NULL) |
1048 | last = strchr(src, ':') + 1; |
1049 | |
1050 | /* maybe send filetime */ |
1051 | |
c51a56e2 |
1052 | if (verbose) |
120e4b40 |
1053 | tell_user(stderr, "Entering directory: %s", last); |
1054 | if (scp_send_dirname(last, 0755)) |
c51a56e2 |
1055 | return; |
1056 | |
03f64569 |
1057 | findfile = dupcat(src, "/*", NULL); |
1058 | dir = FindFirstFile(findfile, &fdat); |
c51a56e2 |
1059 | ok = (dir != INVALID_HANDLE_VALUE); |
1060 | while (ok) { |
1061 | if (strcmp(fdat.cFileName, ".") == 0 || |
1062 | strcmp(fdat.cFileName, "..") == 0) { |
03f64569 |
1063 | /* ignore . and .. */ |
c51a56e2 |
1064 | } else { |
03f64569 |
1065 | char *foundfile = dupcat(src, "/", fdat.cFileName); |
1066 | source(foundfile); |
1067 | sfree(foundfile); |
07d9aa13 |
1068 | } |
c51a56e2 |
1069 | ok = FindNextFile(dir, &fdat); |
1070 | } |
1071 | FindClose(dir); |
03f64569 |
1072 | sfree(findfile); |
07d9aa13 |
1073 | |
120e4b40 |
1074 | (void) scp_send_enddir(); |
07d9aa13 |
1075 | } |
1076 | |
07d9aa13 |
1077 | /* |
03f64569 |
1078 | * Execute the sink part of the SCP protocol. |
07d9aa13 |
1079 | */ |
ca2d5943 |
1080 | static void sink(char *targ, char *src) |
07d9aa13 |
1081 | { |
03f64569 |
1082 | char *destfname; |
c51a56e2 |
1083 | char ch; |
1084 | int targisdir = 0; |
996c8c3b |
1085 | int settime; |
c51a56e2 |
1086 | int exists; |
1087 | DWORD attr; |
1088 | HANDLE f; |
120e4b40 |
1089 | unsigned long received; |
c51a56e2 |
1090 | int wrerror = 0; |
1091 | unsigned long stat_bytes; |
1092 | time_t stat_starttime, stat_lasttime; |
1093 | char *stat_name; |
1094 | |
1095 | attr = GetFileAttributes(targ); |
32874aea |
1096 | if (attr != (DWORD) - 1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0) |
c51a56e2 |
1097 | targisdir = 1; |
1098 | |
1099 | if (targetshouldbedirectory && !targisdir) |
1100 | bump("%s: Not a directory", targ); |
1101 | |
120e4b40 |
1102 | scp_sink_init(); |
c51a56e2 |
1103 | while (1) { |
120e4b40 |
1104 | struct scp_sink_action act; |
1105 | if (scp_get_sink_action(&act)) |
c51a56e2 |
1106 | return; |
07d9aa13 |
1107 | |
120e4b40 |
1108 | if (act.action == SCP_SINK_ENDDIR) |
1109 | return; |
03f64569 |
1110 | |
c51a56e2 |
1111 | if (targisdir) { |
03f64569 |
1112 | /* |
1113 | * Prevent the remote side from maliciously writing to |
1114 | * files outside the target area by sending a filename |
1115 | * containing `../'. In fact, it shouldn't be sending |
1116 | * filenames with any slashes in at all; so we'll find |
1117 | * the last slash or backslash in the filename and use |
1118 | * only the part after that. (And warn!) |
1119 | * |
1120 | * In addition, we also ensure here that if we're |
1121 | * copying a single file and the target is a directory |
1122 | * (common usage: `pscp host:filename .') the remote |
1123 | * can't send us a _different_ file name. We can |
1124 | * distinguish this case because `src' will be non-NULL |
1125 | * and the last component of that will fail to match |
1126 | * (the last component of) the name sent. |
4eeae4a3 |
1127 | * |
1128 | * (Well, not always; if `src' is a wildcard, we do |
1129 | * expect to get back filenames that don't correspond |
1130 | * exactly to it. So we skip this check if `src' |
1131 | * contains a *, a ? or a []. This is non-ideal - we |
1132 | * would like to ensure that the returned filename |
1133 | * actually matches the wildcard pattern - but one of |
1134 | * SCP's protocol infelicities is that wildcard |
1135 | * matching is done at the server end _by the server's |
1136 | * rules_ and so in general this is infeasible. Live |
1137 | * with it, or upgrade to SFTP.) |
03f64569 |
1138 | */ |
1139 | char *striptarget, *stripsrc; |
1140 | |
1141 | striptarget = stripslashes(act.name); |
1142 | if (striptarget != act.name) { |
1143 | tell_user(stderr, "warning: remote host sent a compound" |
1144 | " pathname - possibly malicious! (ignored)"); |
1145 | } |
1146 | |
1147 | /* |
1148 | * Also check to see if the target filename is '.' or |
1149 | * '..', or indeed '...' and so on because Windows |
1150 | * appears to interpret those like '..'. |
1151 | */ |
1152 | if (striptarget[strspn(striptarget, ".")] == '\0') { |
1153 | bump("security violation: remote host attempted to write to" |
1154 | " a '.' or '..' path!"); |
1155 | } |
1156 | |
1157 | if (src) { |
1158 | stripsrc = stripslashes(src); |
4eeae4a3 |
1159 | if (!stripsrc[strcspn(stripsrc, "*?[]")] && |
1160 | strcmp(striptarget, stripsrc)) { |
03f64569 |
1161 | tell_user(stderr, "warning: remote host attempted to" |
1162 | " write to a different filename: disallowing"); |
4eeae4a3 |
1163 | /* Override the name the server provided with our own. */ |
1164 | striptarget = stripsrc; |
03f64569 |
1165 | } |
03f64569 |
1166 | } |
1167 | |
c51a56e2 |
1168 | if (targ[0] != '\0') |
03f64569 |
1169 | destfname = dupcat(targ, "\\", striptarget, NULL); |
1170 | else |
1171 | destfname = dupstr(striptarget); |
c51a56e2 |
1172 | } else { |
03f64569 |
1173 | /* |
1174 | * In this branch of the if, the target area is a |
1175 | * single file with an explicitly specified name in any |
1176 | * case, so there's no danger. |
1177 | */ |
1178 | destfname = dupstr(targ); |
c51a56e2 |
1179 | } |
03f64569 |
1180 | attr = GetFileAttributes(destfname); |
32874aea |
1181 | exists = (attr != (DWORD) - 1); |
c51a56e2 |
1182 | |
120e4b40 |
1183 | if (act.action == SCP_SINK_DIR) { |
c51a56e2 |
1184 | if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
03f64569 |
1185 | run_err("%s: Not a directory", destfname); |
c51a56e2 |
1186 | continue; |
1187 | } |
1188 | if (!exists) { |
03f64569 |
1189 | if (!CreateDirectory(destfname, NULL)) { |
1190 | run_err("%s: Cannot create directory", destfname); |
c51a56e2 |
1191 | continue; |
1192 | } |
1193 | } |
03f64569 |
1194 | sink(destfname, NULL); |
c51a56e2 |
1195 | /* can we set the timestamp for directories ? */ |
1196 | continue; |
1197 | } |
07d9aa13 |
1198 | |
03f64569 |
1199 | f = CreateFile(destfname, GENERIC_WRITE, 0, NULL, |
c51a56e2 |
1200 | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); |
1201 | if (f == INVALID_HANDLE_VALUE) { |
03f64569 |
1202 | run_err("%s: Cannot create file", destfname); |
c51a56e2 |
1203 | continue; |
1204 | } |
07d9aa13 |
1205 | |
120e4b40 |
1206 | if (scp_accept_filexfer()) |
1207 | return; |
07d9aa13 |
1208 | |
2d466ffd |
1209 | stat_bytes = 0; |
1210 | stat_starttime = time(NULL); |
1211 | stat_lasttime = 0; |
03f64569 |
1212 | stat_name = stripslashes(destfname); |
07d9aa13 |
1213 | |
120e4b40 |
1214 | received = 0; |
1215 | while (received < act.size) { |
c51a56e2 |
1216 | char transbuf[4096]; |
120e4b40 |
1217 | DWORD blksize, read, written; |
1218 | blksize = 4096; |
1219 | if (blksize > act.size - received) |
1220 | blksize = act.size - received; |
1221 | read = scp_recv_filedata(transbuf, blksize); |
1222 | if (read <= 0) |
c51a56e2 |
1223 | bump("Lost connection"); |
32874aea |
1224 | if (wrerror) |
1225 | continue; |
120e4b40 |
1226 | if (!WriteFile(f, transbuf, read, &written, NULL) || |
1227 | written != read) { |
c51a56e2 |
1228 | wrerror = 1; |
120e4b40 |
1229 | /* FIXME: in sftp we can actually abort the transfer */ |
c51a56e2 |
1230 | if (statistics) |
1231 | printf("\r%-25.25s | %50s\n", |
1232 | stat_name, |
1233 | "Write error.. waiting for end of file"); |
1234 | continue; |
1235 | } |
1236 | if (statistics) { |
120e4b40 |
1237 | stat_bytes += read; |
1238 | if (time(NULL) > stat_lasttime || |
1239 | received + read == act.size) { |
c51a56e2 |
1240 | stat_lasttime = time(NULL); |
120e4b40 |
1241 | print_stats(stat_name, act.size, stat_bytes, |
c51a56e2 |
1242 | stat_starttime, stat_lasttime); |
07d9aa13 |
1243 | } |
c51a56e2 |
1244 | } |
120e4b40 |
1245 | received += read; |
c51a56e2 |
1246 | } |
120e4b40 |
1247 | if (act.settime) { |
c51a56e2 |
1248 | FILETIME actime, wrtime; |
120e4b40 |
1249 | TIME_POSIX_TO_WIN(act.atime, actime); |
1250 | TIME_POSIX_TO_WIN(act.mtime, wrtime); |
c51a56e2 |
1251 | SetFileTime(f, NULL, &actime, &wrtime); |
07d9aa13 |
1252 | } |
07d9aa13 |
1253 | |
c51a56e2 |
1254 | CloseHandle(f); |
1255 | if (wrerror) { |
03f64569 |
1256 | run_err("%s: Write error", destfname); |
c51a56e2 |
1257 | continue; |
1258 | } |
120e4b40 |
1259 | (void) scp_finish_filerecv(); |
03f64569 |
1260 | sfree(destfname); |
1261 | sfree(act.name); |
c51a56e2 |
1262 | } |
1263 | } |
07d9aa13 |
1264 | |
1265 | /* |
120e4b40 |
1266 | * We will copy local files to a remote server. |
07d9aa13 |
1267 | */ |
1268 | static void toremote(int argc, char *argv[]) |
1269 | { |
c51a56e2 |
1270 | char *src, *targ, *host, *user; |
1271 | char *cmd; |
1272 | int i; |
1273 | |
32874aea |
1274 | targ = argv[argc - 1]; |
c51a56e2 |
1275 | |
39ddf0ff |
1276 | /* Separate host from filename */ |
c51a56e2 |
1277 | host = targ; |
1278 | targ = colon(targ); |
1279 | if (targ == NULL) |
1280 | bump("targ == NULL in toremote()"); |
1281 | *targ++ = '\0'; |
1282 | if (*targ == '\0') |
1283 | targ = "."; |
1284 | /* Substitute "." for emtpy target */ |
1285 | |
39ddf0ff |
1286 | /* Separate host and username */ |
c51a56e2 |
1287 | user = host; |
1288 | host = strrchr(host, '@'); |
1289 | if (host == NULL) { |
1290 | host = user; |
1291 | user = NULL; |
1292 | } else { |
1293 | *host++ = '\0'; |
1294 | if (*user == '\0') |
1295 | user = NULL; |
1296 | } |
1297 | |
1298 | if (argc == 2) { |
1299 | /* Find out if the source filespec covers multiple files |
32874aea |
1300 | if so, we should set the targetshouldbedirectory flag */ |
c51a56e2 |
1301 | HANDLE fh; |
1302 | WIN32_FIND_DATA fdat; |
1303 | if (colon(argv[0]) != NULL) |
1304 | bump("%s: Remote to remote not supported", argv[0]); |
1305 | fh = FindFirstFile(argv[0], &fdat); |
1306 | if (fh == INVALID_HANDLE_VALUE) |
1307 | bump("%s: No such file or directory\n", argv[0]); |
1308 | if (FindNextFile(fh, &fdat)) |
1309 | targetshouldbedirectory = 1; |
1310 | FindClose(fh); |
1311 | } |
1312 | |
1313 | cmd = smalloc(strlen(targ) + 100); |
1314 | sprintf(cmd, "scp%s%s%s%s -t %s", |
1315 | verbose ? " -v" : "", |
1316 | recursive ? " -r" : "", |
1317 | preserve ? " -p" : "", |
32874aea |
1318 | targetshouldbedirectory ? " -d" : "", targ); |
c51a56e2 |
1319 | do_cmd(host, user, cmd); |
1320 | sfree(cmd); |
1321 | |
1322 | (void) response(); |
1323 | |
1324 | for (i = 0; i < argc - 1; i++) { |
03f64569 |
1325 | char *srcpath, *last; |
c51a56e2 |
1326 | HANDLE dir; |
1327 | WIN32_FIND_DATA fdat; |
1328 | src = argv[i]; |
1329 | if (colon(src) != NULL) { |
cc87246d |
1330 | tell_user(stderr, "%s: Remote to remote not supported\n", src); |
c51a56e2 |
1331 | errs++; |
1332 | continue; |
07d9aa13 |
1333 | } |
03f64569 |
1334 | |
1335 | /* |
1336 | * Trim off the last pathname component of `src', to |
1337 | * provide the base pathname which will be prepended to |
1338 | * filenames returned from Find{First,Next}File. |
1339 | */ |
1340 | srcpath = dupstr(src); |
1341 | last = stripslashes(srcpath); |
1342 | if (last == srcpath) { |
1343 | last = strchr(srcpath, ':'); |
1344 | if (last) |
1345 | last++; |
1346 | else |
1347 | last = srcpath; |
1348 | } |
03f64569 |
1349 | *last = '\0'; |
03f64569 |
1350 | |
c51a56e2 |
1351 | dir = FindFirstFile(src, &fdat); |
1352 | if (dir == INVALID_HANDLE_VALUE) { |
1353 | run_err("%s: No such file or directory", src); |
1354 | continue; |
07d9aa13 |
1355 | } |
c51a56e2 |
1356 | do { |
1357 | char *last; |
03f64569 |
1358 | char *filename; |
7f266ffb |
1359 | /* |
1360 | * Ensure that . and .. are never matched by wildcards, |
1361 | * but only by deliberate action. |
1362 | */ |
1363 | if (!strcmp(fdat.cFileName, ".") || |
1364 | !strcmp(fdat.cFileName, "..")) { |
1365 | /* |
1366 | * Find*File has returned a special dir. We require |
1367 | * that _either_ `src' ends in a backslash followed |
1368 | * by that string, _or_ `src' is precisely that |
1369 | * string. |
1370 | */ |
1371 | int len = strlen(src), dlen = strlen(fdat.cFileName); |
1372 | if (len == dlen && !strcmp(src, fdat.cFileName)) { |
32874aea |
1373 | /* ok */ ; |
1374 | } else if (len > dlen + 1 && src[len - dlen - 1] == '\\' && |
1375 | !strcmp(src + len - dlen, fdat.cFileName)) { |
1376 | /* ok */ ; |
7f266ffb |
1377 | } else |
1378 | continue; /* ignore this one */ |
1379 | } |
03f64569 |
1380 | filename = dupcat(srcpath, fdat.cFileName, NULL); |
1381 | source(filename); |
1382 | sfree(filename); |
c51a56e2 |
1383 | } while (FindNextFile(dir, &fdat)); |
1384 | FindClose(dir); |
03f64569 |
1385 | sfree(srcpath); |
c51a56e2 |
1386 | } |
07d9aa13 |
1387 | } |
1388 | |
07d9aa13 |
1389 | /* |
1390 | * We will copy files from a remote server to the local machine. |
1391 | */ |
1392 | static void tolocal(int argc, char *argv[]) |
1393 | { |
c51a56e2 |
1394 | char *src, *targ, *host, *user; |
1395 | char *cmd; |
1396 | |
1397 | if (argc != 2) |
1398 | bump("More than one remote source not supported"); |
1399 | |
1400 | src = argv[0]; |
1401 | targ = argv[1]; |
1402 | |
39ddf0ff |
1403 | /* Separate host from filename */ |
c51a56e2 |
1404 | host = src; |
1405 | src = colon(src); |
1406 | if (src == NULL) |
1407 | bump("Local to local copy not supported"); |
1408 | *src++ = '\0'; |
1409 | if (*src == '\0') |
1410 | src = "."; |
1411 | /* Substitute "." for empty filename */ |
1412 | |
39ddf0ff |
1413 | /* Separate username and hostname */ |
c51a56e2 |
1414 | user = host; |
1415 | host = strrchr(host, '@'); |
1416 | if (host == NULL) { |
1417 | host = user; |
1418 | user = NULL; |
1419 | } else { |
1420 | *host++ = '\0'; |
1421 | if (*user == '\0') |
1422 | user = NULL; |
1423 | } |
1424 | |
1425 | cmd = smalloc(strlen(src) + 100); |
1426 | sprintf(cmd, "scp%s%s%s%s -f %s", |
1427 | verbose ? " -v" : "", |
1428 | recursive ? " -r" : "", |
1429 | preserve ? " -p" : "", |
32874aea |
1430 | targetshouldbedirectory ? " -d" : "", src); |
c51a56e2 |
1431 | do_cmd(host, user, cmd); |
1432 | sfree(cmd); |
1433 | |
ca2d5943 |
1434 | sink(targ, src); |
07d9aa13 |
1435 | } |
1436 | |
07d9aa13 |
1437 | /* |
39ddf0ff |
1438 | * We will issue a list command to get a remote directory. |
1439 | */ |
1440 | static void get_dir_list(int argc, char *argv[]) |
1441 | { |
1442 | char *src, *host, *user; |
1443 | char *cmd, *p, *q; |
1444 | char c; |
1445 | |
1446 | src = argv[0]; |
1447 | |
1448 | /* Separate host from filename */ |
1449 | host = src; |
1450 | src = colon(src); |
1451 | if (src == NULL) |
1452 | bump("Local to local copy not supported"); |
1453 | *src++ = '\0'; |
1454 | if (*src == '\0') |
1455 | src = "."; |
1456 | /* Substitute "." for empty filename */ |
1457 | |
1458 | /* Separate username and hostname */ |
1459 | user = host; |
1460 | host = strrchr(host, '@'); |
1461 | if (host == NULL) { |
1462 | host = user; |
1463 | user = NULL; |
1464 | } else { |
1465 | *host++ = '\0'; |
1466 | if (*user == '\0') |
1467 | user = NULL; |
1468 | } |
1469 | |
32874aea |
1470 | cmd = smalloc(4 * strlen(src) + 100); |
39ddf0ff |
1471 | strcpy(cmd, "ls -la '"); |
1472 | p = cmd + strlen(cmd); |
1473 | for (q = src; *q; q++) { |
1474 | if (*q == '\'') { |
32874aea |
1475 | *p++ = '\''; |
1476 | *p++ = '\\'; |
1477 | *p++ = '\''; |
1478 | *p++ = '\''; |
39ddf0ff |
1479 | } else { |
1480 | *p++ = *q; |
1481 | } |
1482 | } |
1483 | *p++ = '\''; |
1484 | *p = '\0'; |
cc87246d |
1485 | |
39ddf0ff |
1486 | do_cmd(host, user, cmd); |
1487 | sfree(cmd); |
1488 | |
fb09bf1c |
1489 | while (ssh_scp_recv(&c, 1) > 0) |
cc87246d |
1490 | tell_char(stdout, c); |
39ddf0ff |
1491 | } |
1492 | |
1493 | /* |
07d9aa13 |
1494 | * Initialize the Win$ock driver. |
1495 | */ |
996c8c3b |
1496 | static void init_winsock(void) |
07d9aa13 |
1497 | { |
c51a56e2 |
1498 | WORD winsock_ver; |
1499 | WSADATA wsadata; |
1500 | |
1501 | winsock_ver = MAKEWORD(1, 1); |
1502 | if (WSAStartup(winsock_ver, &wsadata)) |
1503 | bump("Unable to initialise WinSock"); |
32874aea |
1504 | if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1) |
c51a56e2 |
1505 | bump("WinSock version is incompatible with 1.1"); |
07d9aa13 |
1506 | } |
1507 | |
07d9aa13 |
1508 | /* |
1509 | * Short description of parameters. |
1510 | */ |
996c8c3b |
1511 | static void usage(void) |
07d9aa13 |
1512 | { |
c51a56e2 |
1513 | printf("PuTTY Secure Copy client\n"); |
1514 | printf("%s\n", ver); |
a3e55ea1 |
1515 | printf("Usage: pscp [options] [user@]host:source target\n"); |
32874aea |
1516 | printf |
1517 | (" pscp [options] source [source...] [user@]host:target\n"); |
a3e55ea1 |
1518 | printf(" pscp [options] -ls user@host:filespec\n"); |
b8a19193 |
1519 | printf("Options:\n"); |
1520 | printf(" -p preserve file attributes\n"); |
1521 | printf(" -q quiet, don't show statistics\n"); |
1522 | printf(" -r copy directories recursively\n"); |
1523 | printf(" -v show verbose messages\n"); |
1524 | printf(" -P port connect to specified port\n"); |
1525 | printf(" -pw passw login with specified password\n"); |
ee8b0370 |
1526 | #if 0 |
1527 | /* |
1528 | * -gui is an internal option, used by GUI front ends to get |
1529 | * pscp to pass progress reports back to them. It's not an |
1530 | * ordinary user-accessible option, so it shouldn't be part of |
1531 | * the command-line help. The only people who need to know |
1532 | * about it are programmers, and they can read the source. |
1533 | */ |
32874aea |
1534 | printf |
1535 | (" -gui hWnd GUI mode with the windows handle for receiving messages\n"); |
ee8b0370 |
1536 | #endif |
c51a56e2 |
1537 | exit(1); |
07d9aa13 |
1538 | } |
1539 | |
07d9aa13 |
1540 | /* |
1541 | * Main program (no, really?) |
1542 | */ |
1543 | int main(int argc, char *argv[]) |
1544 | { |
c51a56e2 |
1545 | int i; |
1546 | |
fb09bf1c |
1547 | default_protocol = PROT_TELNET; |
1548 | |
67779be7 |
1549 | flags = FLAG_STDERR; |
fa17a66e |
1550 | ssh_get_line = &get_line; |
c51a56e2 |
1551 | init_winsock(); |
8df7a775 |
1552 | sk_init(); |
c51a56e2 |
1553 | |
1554 | for (i = 1; i < argc; i++) { |
1555 | if (argv[i][0] != '-') |
1556 | break; |
1557 | if (strcmp(argv[i], "-v") == 0) |
4017be6d |
1558 | verbose = 1, flags |= FLAG_VERBOSE; |
c51a56e2 |
1559 | else if (strcmp(argv[i], "-r") == 0) |
1560 | recursive = 1; |
1561 | else if (strcmp(argv[i], "-p") == 0) |
1562 | preserve = 1; |
1563 | else if (strcmp(argv[i], "-q") == 0) |
1564 | statistics = 0; |
32874aea |
1565 | else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-?") == 0) |
c51a56e2 |
1566 | usage(); |
32874aea |
1567 | else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) |
b8a19193 |
1568 | portnumber = atoi(argv[++i]); |
32874aea |
1569 | else if (strcmp(argv[i], "-pw") == 0 && i + 1 < argc) |
b8a19193 |
1570 | password = argv[++i]; |
32874aea |
1571 | else if (strcmp(argv[i], "-gui") == 0 && i + 1 < argc) { |
cc87246d |
1572 | gui_hwnd = argv[++i]; |
1573 | gui_mode = 1; |
1574 | } else if (strcmp(argv[i], "-ls") == 0) |
32874aea |
1575 | list = 1; |
1576 | else if (strcmp(argv[i], "--") == 0) { |
1577 | i++; |
1578 | break; |
1579 | } else |
c51a56e2 |
1580 | usage(); |
1581 | } |
1582 | argc -= i; |
1583 | argv += i; |
eba78553 |
1584 | back = NULL; |
c51a56e2 |
1585 | |
39ddf0ff |
1586 | if (list) { |
1587 | if (argc != 1) |
1588 | usage(); |
1589 | get_dir_list(argc, argv); |
c51a56e2 |
1590 | |
39ddf0ff |
1591 | } else { |
1592 | |
1593 | if (argc < 2) |
1594 | usage(); |
1595 | if (argc > 2) |
1596 | targetshouldbedirectory = 1; |
1597 | |
32874aea |
1598 | if (colon(argv[argc - 1]) != NULL) |
39ddf0ff |
1599 | toremote(argc, argv); |
1600 | else |
1601 | tolocal(argc, argv); |
1602 | } |
c51a56e2 |
1603 | |
eba78553 |
1604 | if (back != NULL && back->socket() != NULL) { |
c51a56e2 |
1605 | char ch; |
3bdaf79d |
1606 | back->special(TS_EOF); |
fb09bf1c |
1607 | ssh_scp_recv(&ch, 1); |
c51a56e2 |
1608 | } |
1609 | WSACleanup(); |
1610 | random_save_seed(); |
07d9aa13 |
1611 | |
cc87246d |
1612 | /* GUI Adaptation - August 2000 */ |
1613 | if (gui_mode) { |
1614 | unsigned int msg_id = WM_RET_ERR_CNT; |
32874aea |
1615 | if (list) |
1616 | msg_id = WM_LS_RET_ERR_CNT; |
1617 | while (!PostMessage |
1618 | ((HWND) atoi(gui_hwnd), msg_id, (WPARAM) errs, |
1619 | 0 /*lParam */ ))SleepEx(1000, TRUE); |
cc87246d |
1620 | } |
c51a56e2 |
1621 | return (errs == 0 ? 0 : 1); |
07d9aa13 |
1622 | } |
1623 | |
1624 | /* end */ |