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> |
cc87246d |
23 | /* GUI Adaptation - Sept 2000 */ |
24 | #include <winuser.h> |
25 | #include <winbase.h> |
07d9aa13 |
26 | |
27 | #define PUTTY_DO_GLOBALS |
28 | #include "putty.h" |
07d9aa13 |
29 | |
07d9aa13 |
30 | #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \ |
c51a56e2 |
31 | ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000) |
07d9aa13 |
32 | #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \ |
c51a56e2 |
33 | ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600)) |
07d9aa13 |
34 | |
cc87246d |
35 | /* GUI Adaptation - Sept 2000 */ |
36 | #define WM_APP_BASE 0x8000 |
37 | #define WM_STD_OUT_CHAR ( WM_APP_BASE+400 ) |
38 | #define WM_STD_ERR_CHAR ( WM_APP_BASE+401 ) |
39 | #define WM_STATS_CHAR ( WM_APP_BASE+402 ) |
40 | #define WM_STATS_SIZE ( WM_APP_BASE+403 ) |
41 | #define WM_STATS_PERCENT ( WM_APP_BASE+404 ) |
42 | #define WM_STATS_ELAPSED ( WM_APP_BASE+405 ) |
43 | #define WM_RET_ERR_CNT ( WM_APP_BASE+406 ) |
44 | #define WM_LS_RET_ERR_CNT ( WM_APP_BASE+407 ) |
45 | |
fb09bf1c |
46 | static int verbose = 0; |
07d9aa13 |
47 | static int recursive = 0; |
48 | static int preserve = 0; |
49 | static int targetshouldbedirectory = 0; |
50 | static int statistics = 1; |
b8a19193 |
51 | static int portnumber = 0; |
52 | static char *password = NULL; |
07d9aa13 |
53 | static int errs = 0; |
54 | static int connection_open = 0; |
cc87246d |
55 | /* GUI Adaptation - Sept 2000 */ |
56 | #define NAME_STR_MAX 2048 |
57 | static char statname[NAME_STR_MAX+1]; |
58 | static unsigned long statsize = 0; |
59 | static int statperct = 0; |
60 | static time_t statelapsed = 0; |
61 | static int gui_mode = 0; |
62 | static char *gui_hwnd = NULL; |
07d9aa13 |
63 | |
64 | static void source(char *src); |
65 | static void rsource(char *src); |
66 | static void sink(char *targ); |
cc87246d |
67 | /* GUI Adaptation - Sept 2000 */ |
68 | static void tell_char(FILE *stream, char c); |
69 | static void tell_str(FILE *stream, char *str); |
70 | static void tell_user(FILE *stream, char *fmt, ...); |
71 | static void send_char_msg(unsigned int msg_id, char c); |
72 | static void send_str_msg(unsigned int msg_id, char *str); |
73 | static void gui_update_stats(char *name, unsigned long size, int percentage, time_t elapsed); |
07d9aa13 |
74 | |
07d9aa13 |
75 | /* |
989b10e9 |
76 | * These functions are needed to link with other modules, but |
77 | * (should) never get called. |
fb09bf1c |
78 | */ |
989b10e9 |
79 | void begin_session(void) { } |
80 | void write_clip (void *data, int len) { } |
81 | void term_deselect(void) { } |
fb09bf1c |
82 | |
cc87246d |
83 | /* GUI Adaptation - Sept 2000 */ |
84 | void send_msg(HWND h, UINT message, WPARAM wParam) |
85 | { |
86 | while (!PostMessage( h, message, wParam, 0)) |
87 | SleepEx(1000,TRUE); |
88 | } |
89 | |
90 | void tell_char(FILE *stream, char c) |
91 | { |
92 | if (!gui_mode) |
93 | fputc(c, stream); |
94 | else |
95 | { |
96 | unsigned int msg_id = WM_STD_OUT_CHAR; |
3a913813 |
97 | if (stream == stderr) msg_id = WM_STD_ERR_CHAR; |
cc87246d |
98 | send_msg( (HWND)atoi(gui_hwnd), msg_id, (WPARAM)c ); |
99 | } |
100 | } |
101 | |
102 | void tell_str(FILE *stream, char *str) |
103 | { |
104 | unsigned int i; |
105 | |
106 | for( i = 0; i < strlen(str); ++i ) |
107 | tell_char(stream, str[i]); |
108 | } |
109 | |
110 | void tell_user(FILE *stream, char *fmt, ...) |
111 | { |
112 | char str[0x100]; /* Make the size big enough */ |
113 | va_list ap; |
114 | va_start(ap, fmt); |
115 | vsprintf(str, fmt, ap); |
116 | va_end(ap); |
117 | strcat(str, "\n"); |
118 | tell_str(stream, str); |
119 | } |
120 | |
121 | void gui_update_stats(char *name, unsigned long size, int percentage, time_t elapsed) |
122 | { |
123 | unsigned int i; |
124 | |
125 | if (strcmp(name,statname) != 0) |
126 | { |
127 | for( i = 0; i < strlen(name); ++i ) |
128 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM)name[i]); |
129 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_CHAR, (WPARAM)'\n' ); |
130 | strcpy(statname,name); |
131 | } |
132 | if (statsize != size) |
133 | { |
134 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_SIZE, (WPARAM)size ); |
135 | statsize = size; |
136 | } |
137 | if (statelapsed != elapsed) |
138 | { |
139 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_ELAPSED, (WPARAM)elapsed ); |
140 | statelapsed = elapsed; |
141 | } |
142 | if (statperct != percentage) |
143 | { |
144 | send_msg( (HWND)atoi(gui_hwnd), WM_STATS_PERCENT, (WPARAM)percentage ); |
145 | statperct = percentage; |
146 | } |
147 | } |
148 | |
fb09bf1c |
149 | /* |
07d9aa13 |
150 | * Print an error message and perform a fatal exit. |
151 | */ |
152 | void fatalbox(char *fmt, ...) |
153 | { |
cc87246d |
154 | char str[0x100]; /* Make the size big enough */ |
c51a56e2 |
155 | va_list ap; |
156 | va_start(ap, fmt); |
cc87246d |
157 | strcpy(str, "Fatal:"); |
158 | vsprintf(str+strlen(str), fmt, ap); |
c51a56e2 |
159 | va_end(ap); |
cc87246d |
160 | strcat(str, "\n"); |
161 | tell_str(stderr, str); |
162 | |
c51a56e2 |
163 | exit(1); |
07d9aa13 |
164 | } |
8d5de777 |
165 | void connection_fatal(char *fmt, ...) |
166 | { |
167 | char str[0x100]; /* Make the size big enough */ |
168 | va_list ap; |
169 | va_start(ap, fmt); |
170 | strcpy(str, "Fatal:"); |
171 | vsprintf(str+strlen(str), fmt, ap); |
172 | va_end(ap); |
173 | strcat(str, "\n"); |
174 | tell_str(stderr, str); |
175 | |
176 | exit(1); |
177 | } |
07d9aa13 |
178 | |
07d9aa13 |
179 | /* |
3bdaf79d |
180 | * Receive a block of data from the SSH link. Block until all data |
181 | * is available. |
182 | * |
183 | * To do this, we repeatedly call the SSH protocol module, with our |
184 | * own trap in term_out() to catch the data that comes back. We do |
185 | * this until we have enough data. |
186 | */ |
187 | static unsigned char *outptr; /* where to put the data */ |
188 | static unsigned outlen; /* how much data required */ |
189 | static unsigned char *pending = NULL; /* any spare data */ |
190 | static unsigned pendlen=0, pendsize=0; /* length and phys. size of buffer */ |
191 | void term_out(void) { |
192 | /* |
193 | * Here we must deal with a block of data, in `inbuf', size |
194 | * `inbuf_head'. |
195 | */ |
196 | unsigned char *p = inbuf; |
197 | unsigned len = inbuf_head; |
198 | |
199 | inbuf_head = 0; |
200 | |
201 | /* |
202 | * If this is before the real session begins, just return. |
203 | */ |
204 | if (!outptr) |
205 | return; |
206 | |
207 | if (outlen > 0) { |
208 | unsigned used = outlen; |
209 | if (used > len) used = len; |
210 | memcpy(outptr, p, used); |
211 | outptr += used; outlen -= used; |
212 | p += used; len -= used; |
213 | } |
214 | |
215 | if (len > 0) { |
216 | if (pendsize < pendlen + len) { |
217 | pendsize = pendlen + len + 4096; |
218 | pending = (pending ? realloc(pending, pendsize) : |
219 | malloc(pendsize)); |
220 | if (!pending) |
221 | fatalbox("Out of memory"); |
222 | } |
223 | memcpy(pending+pendlen, p, len); |
224 | pendlen += len; |
225 | } |
226 | } |
227 | static int ssh_scp_recv(unsigned char *buf, int len) { |
228 | SOCKET s; |
229 | |
230 | outptr = buf; |
231 | outlen = len; |
232 | |
233 | /* |
234 | * See if the pending-input block contains some of what we |
235 | * need. |
236 | */ |
237 | if (pendlen > 0) { |
238 | unsigned pendused = pendlen; |
239 | if (pendused > outlen) |
240 | pendused = outlen; |
241 | memcpy(outptr, pending, pendused); |
242 | memmove(pending, pending+pendused, pendlen-pendused); |
243 | outptr += pendused; |
244 | outlen -= pendused; |
245 | pendlen -= pendused; |
246 | if (pendlen == 0) { |
247 | pendsize = 0; |
248 | free(pending); |
249 | pending = NULL; |
250 | } |
251 | if (outlen == 0) |
252 | return len; |
253 | } |
254 | |
255 | while (outlen > 0) { |
256 | fd_set readfds; |
257 | s = back->socket(); |
258 | if (s == INVALID_SOCKET) { |
259 | connection_open = FALSE; |
260 | return 0; |
261 | } |
262 | FD_ZERO(&readfds); |
263 | FD_SET(s, &readfds); |
264 | if (select(1, &readfds, NULL, NULL, NULL) < 0) |
265 | return 0; /* doom */ |
266 | back->msg(0, FD_READ); |
267 | term_out(); |
268 | } |
269 | |
270 | return len; |
271 | } |
272 | |
273 | /* |
274 | * Loop through the ssh connection and authentication process. |
275 | */ |
276 | static void ssh_scp_init(void) { |
277 | SOCKET s; |
278 | |
279 | s = back->socket(); |
280 | if (s == INVALID_SOCKET) |
281 | return; |
282 | while (!back->sendok()) { |
283 | fd_set readfds; |
284 | FD_ZERO(&readfds); |
285 | FD_SET(s, &readfds); |
286 | if (select(1, &readfds, NULL, NULL, NULL) < 0) |
287 | return; /* doom */ |
288 | back->msg(0, FD_READ); |
289 | term_out(); |
290 | } |
291 | } |
292 | |
293 | /* |
07d9aa13 |
294 | * Print an error message and exit after closing the SSH link. |
295 | */ |
296 | static void bump(char *fmt, ...) |
297 | { |
cc87246d |
298 | char str[0x100]; /* Make the size big enough */ |
c51a56e2 |
299 | va_list ap; |
300 | va_start(ap, fmt); |
cc87246d |
301 | strcpy(str, "Fatal:"); |
302 | vsprintf(str+strlen(str), fmt, ap); |
c51a56e2 |
303 | va_end(ap); |
cc87246d |
304 | strcat(str, "\n"); |
305 | tell_str(stderr, str); |
306 | |
c51a56e2 |
307 | if (connection_open) { |
308 | char ch; |
3bdaf79d |
309 | back->special(TS_EOF); |
fb09bf1c |
310 | ssh_scp_recv(&ch, 1); |
c51a56e2 |
311 | } |
312 | exit(1); |
07d9aa13 |
313 | } |
314 | |
85ee8208 |
315 | static int get_password(const char *prompt, char *str, int maxlen) |
07d9aa13 |
316 | { |
c51a56e2 |
317 | HANDLE hin, hout; |
b8a19193 |
318 | DWORD savemode, i; |
319 | |
320 | if (password) { |
85ee8208 |
321 | static int tried_once = 0; |
322 | |
323 | if (tried_once) { |
324 | return 0; |
325 | } else { |
326 | strncpy(str, password, maxlen); |
327 | str[maxlen-1] = '\0'; |
328 | tried_once = 1; |
329 | return 1; |
330 | } |
b8a19193 |
331 | } |
07d9aa13 |
332 | |
cc87246d |
333 | /* GUI Adaptation - Sept 2000 */ |
334 | if (gui_mode) { |
335 | if (maxlen>0) str[0] = '\0'; |
336 | } else { |
337 | hin = GetStdHandle(STD_INPUT_HANDLE); |
338 | hout = GetStdHandle(STD_OUTPUT_HANDLE); |
339 | if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) |
340 | bump("Cannot get standard input/output handles"); |
07d9aa13 |
341 | |
cc87246d |
342 | GetConsoleMode(hin, &savemode); |
343 | SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) | |
344 | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT); |
07d9aa13 |
345 | |
cc87246d |
346 | WriteFile(hout, prompt, strlen(prompt), &i, NULL); |
347 | ReadFile(hin, str, maxlen-1, &i, NULL); |
07d9aa13 |
348 | |
cc87246d |
349 | SetConsoleMode(hin, savemode); |
07d9aa13 |
350 | |
cc87246d |
351 | if ((int)i > maxlen) i = maxlen-1; else i = i - 2; |
352 | str[i] = '\0'; |
07d9aa13 |
353 | |
cc87246d |
354 | WriteFile(hout, "\r\n", 2, &i, NULL); |
355 | } |
85ee8208 |
356 | |
357 | return 1; |
07d9aa13 |
358 | } |
359 | |
07d9aa13 |
360 | /* |
361 | * Open an SSH connection to user@host and execute cmd. |
362 | */ |
363 | static void do_cmd(char *host, char *user, char *cmd) |
364 | { |
c51a56e2 |
365 | char *err, *realhost; |
366 | |
367 | if (host == NULL || host[0] == '\0') |
368 | bump("Empty host name"); |
369 | |
370 | /* Try to load settings for this host */ |
371 | do_defaults(host); |
372 | if (cfg.host[0] == '\0') { |
373 | /* No settings for this host; use defaults */ |
374 | strncpy(cfg.host, host, sizeof(cfg.host)-1); |
375 | cfg.host[sizeof(cfg.host)-1] = '\0'; |
376 | cfg.port = 22; |
377 | } |
378 | |
379 | /* Set username */ |
380 | if (user != NULL && user[0] != '\0') { |
381 | strncpy(cfg.username, user, sizeof(cfg.username)-1); |
382 | cfg.username[sizeof(cfg.username)-1] = '\0'; |
c51a56e2 |
383 | } else if (cfg.username[0] == '\0') { |
384 | bump("Empty user name"); |
385 | } |
386 | |
387 | if (cfg.protocol != PROT_SSH) |
388 | cfg.port = 22; |
389 | |
ed89e8a5 |
390 | if (portnumber) |
391 | cfg.port = portnumber; |
392 | |
3bdaf79d |
393 | strncpy(cfg.remote_cmd, cmd, sizeof(cfg.remote_cmd)); |
394 | cfg.remote_cmd[sizeof(cfg.remote_cmd)-1] = '\0'; |
395 | cfg.nopty = TRUE; |
396 | |
397 | back = &ssh_backend; |
398 | |
399 | err = back->init(NULL, cfg.host, cfg.port, &realhost); |
c51a56e2 |
400 | if (err != NULL) |
401 | bump("ssh_init: %s", err); |
3bdaf79d |
402 | ssh_scp_init(); |
c51a56e2 |
403 | if (verbose && realhost != NULL) |
cc87246d |
404 | tell_user(stderr, "Connected to %s\n", realhost); |
c51a56e2 |
405 | |
406 | connection_open = 1; |
07d9aa13 |
407 | } |
408 | |
07d9aa13 |
409 | /* |
410 | * Update statistic information about current file. |
411 | */ |
412 | static void print_stats(char *name, unsigned long size, unsigned long done, |
c51a56e2 |
413 | time_t start, time_t now) |
07d9aa13 |
414 | { |
c51a56e2 |
415 | float ratebs; |
416 | unsigned long eta; |
417 | char etastr[10]; |
418 | int pct; |
419 | |
cc87246d |
420 | /* GUI Adaptation - Sept 2000 */ |
421 | if (gui_mode) |
422 | gui_update_stats(name, size, ((done *100) / size), now-start); |
423 | else { |
424 | if (now > start) |
425 | ratebs = (float) done / (now - start); |
426 | else |
427 | ratebs = (float) done; |
c51a56e2 |
428 | |
cc87246d |
429 | if (ratebs < 1.0) |
430 | eta = size - done; |
431 | else |
432 | eta = (unsigned long) ((size - done) / ratebs); |
433 | sprintf(etastr, "%02ld:%02ld:%02ld", |
434 | eta / 3600, (eta % 3600) / 60, eta % 60); |
c51a56e2 |
435 | |
cc87246d |
436 | pct = (int) (100.0 * (float) done / size); |
c51a56e2 |
437 | |
cc87246d |
438 | printf("\r%-25.25s | %10ld kB | %5.1f kB/s | ETA: %8s | %3d%%", |
439 | name, done / 1024, ratebs / 1024.0, |
440 | etastr, pct); |
c51a56e2 |
441 | |
cc87246d |
442 | if (done == size) |
443 | printf("\n"); |
444 | } |
07d9aa13 |
445 | } |
446 | |
07d9aa13 |
447 | /* |
448 | * Find a colon in str and return a pointer to the colon. |
39ddf0ff |
449 | * This is used to separate hostname from filename. |
07d9aa13 |
450 | */ |
451 | static char * colon(char *str) |
452 | { |
c51a56e2 |
453 | /* We ignore a leading colon, since the hostname cannot be |
454 | empty. We also ignore a colon as second character because |
455 | of filenames like f:myfile.txt. */ |
456 | if (str[0] == '\0' || |
457 | str[0] == ':' || |
458 | str[1] == ':') |
459 | return (NULL); |
460 | while (*str != '\0' && |
461 | *str != ':' && |
462 | *str != '/' && |
463 | *str != '\\') |
464 | str++; |
465 | if (*str == ':') |
466 | return (str); |
467 | else |
468 | return (NULL); |
07d9aa13 |
469 | } |
470 | |
07d9aa13 |
471 | /* |
472 | * Wait for a response from the other side. |
473 | * Return 0 if ok, -1 if error. |
474 | */ |
475 | static int response(void) |
476 | { |
c51a56e2 |
477 | char ch, resp, rbuf[2048]; |
478 | int p; |
479 | |
fb09bf1c |
480 | if (ssh_scp_recv(&resp, 1) <= 0) |
c51a56e2 |
481 | bump("Lost connection"); |
482 | |
483 | p = 0; |
484 | switch (resp) { |
485 | case 0: /* ok */ |
486 | return (0); |
487 | default: |
488 | rbuf[p++] = resp; |
489 | /* fallthrough */ |
490 | case 1: /* error */ |
491 | case 2: /* fatal error */ |
492 | do { |
fb09bf1c |
493 | if (ssh_scp_recv(&ch, 1) <= 0) |
c51a56e2 |
494 | bump("Protocol error: Lost connection"); |
495 | rbuf[p++] = ch; |
496 | } while (p < sizeof(rbuf) && ch != '\n'); |
497 | rbuf[p-1] = '\0'; |
498 | if (resp == 1) |
cc87246d |
499 | tell_user(stderr, "%s\n", rbuf); |
c51a56e2 |
500 | else |
501 | bump("%s", rbuf); |
502 | errs++; |
503 | return (-1); |
504 | } |
07d9aa13 |
505 | } |
506 | |
07d9aa13 |
507 | /* |
508 | * Send an error message to the other side and to the screen. |
509 | * Increment error counter. |
510 | */ |
511 | static void run_err(const char *fmt, ...) |
512 | { |
c51a56e2 |
513 | char str[2048]; |
514 | va_list ap; |
515 | va_start(ap, fmt); |
516 | errs++; |
517 | strcpy(str, "\01scp: "); |
518 | vsprintf(str+strlen(str), fmt, ap); |
519 | strcat(str, "\n"); |
3bdaf79d |
520 | back->send(str, strlen(str)); |
cc87246d |
521 | tell_user(stderr, "%s",str); |
c51a56e2 |
522 | va_end(ap); |
07d9aa13 |
523 | } |
524 | |
07d9aa13 |
525 | /* |
526 | * Execute the source part of the SCP protocol. |
527 | */ |
528 | static void source(char *src) |
529 | { |
c51a56e2 |
530 | char buf[2048]; |
531 | unsigned long size; |
532 | char *last; |
533 | HANDLE f; |
534 | DWORD attr; |
535 | unsigned long i; |
536 | unsigned long stat_bytes; |
537 | time_t stat_starttime, stat_lasttime; |
538 | |
539 | attr = GetFileAttributes(src); |
996c8c3b |
540 | if (attr == (DWORD)-1) { |
c51a56e2 |
541 | run_err("%s: No such file or directory", src); |
542 | return; |
543 | } |
544 | |
545 | if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
7f1f80de |
546 | if (recursive) { |
547 | /* |
548 | * Avoid . and .. directories. |
549 | */ |
550 | char *p; |
551 | p = strrchr(src, '/'); |
552 | if (!p) |
553 | p = strrchr(src, '\\'); |
554 | if (!p) |
555 | p = src; |
556 | else |
557 | p++; |
558 | if (!strcmp(p, ".") || !strcmp(p, "..")) |
559 | /* skip . and .. */; |
560 | else |
561 | rsource(src); |
562 | } else { |
c51a56e2 |
563 | run_err("%s: not a regular file", src); |
7f1f80de |
564 | } |
c51a56e2 |
565 | return; |
566 | } |
567 | |
568 | if ((last = strrchr(src, '/')) == NULL) |
569 | last = src; |
570 | else |
571 | last++; |
572 | if (strrchr(last, '\\') != NULL) |
573 | last = strrchr(last, '\\') + 1; |
574 | if (last == src && strchr(src, ':') != NULL) |
575 | last = strchr(src, ':') + 1; |
576 | |
577 | f = CreateFile(src, GENERIC_READ, FILE_SHARE_READ, NULL, |
578 | OPEN_EXISTING, 0, 0); |
579 | if (f == INVALID_HANDLE_VALUE) { |
486543a1 |
580 | run_err("%s: Cannot open file", src); |
c51a56e2 |
581 | return; |
582 | } |
583 | |
584 | if (preserve) { |
585 | FILETIME actime, wrtime; |
586 | unsigned long mtime, atime; |
587 | GetFileTime(f, NULL, &actime, &wrtime); |
588 | TIME_WIN_TO_POSIX(actime, atime); |
589 | TIME_WIN_TO_POSIX(wrtime, mtime); |
590 | sprintf(buf, "T%lu 0 %lu 0\n", mtime, atime); |
3bdaf79d |
591 | back->send(buf, strlen(buf)); |
07d9aa13 |
592 | if (response()) |
c51a56e2 |
593 | return; |
594 | } |
595 | |
596 | size = GetFileSize(f, NULL); |
597 | sprintf(buf, "C0644 %lu %s\n", size, last); |
598 | if (verbose) |
cc87246d |
599 | tell_user(stderr, "Sending file modes: %s", buf); |
3bdaf79d |
600 | back->send(buf, strlen(buf)); |
c51a56e2 |
601 | if (response()) |
602 | return; |
603 | |
604 | if (statistics) { |
605 | stat_bytes = 0; |
606 | stat_starttime = time(NULL); |
607 | stat_lasttime = 0; |
608 | } |
609 | |
610 | for (i = 0; i < size; i += 4096) { |
611 | char transbuf[4096]; |
612 | DWORD j, k = 4096; |
613 | if (i + k > size) k = size - i; |
614 | if (! ReadFile(f, transbuf, k, &j, NULL) || j != k) { |
615 | if (statistics) printf("\n"); |
616 | bump("%s: Read error", src); |
07d9aa13 |
617 | } |
3bdaf79d |
618 | back->send(transbuf, k); |
c51a56e2 |
619 | if (statistics) { |
620 | stat_bytes += k; |
621 | if (time(NULL) != stat_lasttime || |
622 | i + k == size) { |
623 | stat_lasttime = time(NULL); |
624 | print_stats(last, size, stat_bytes, |
625 | stat_starttime, stat_lasttime); |
626 | } |
07d9aa13 |
627 | } |
c51a56e2 |
628 | } |
629 | CloseHandle(f); |
07d9aa13 |
630 | |
3bdaf79d |
631 | back->send("", 1); |
c51a56e2 |
632 | (void) response(); |
07d9aa13 |
633 | } |
634 | |
07d9aa13 |
635 | /* |
636 | * Recursively send the contents of a directory. |
637 | */ |
638 | static void rsource(char *src) |
639 | { |
c51a56e2 |
640 | char buf[2048]; |
641 | char *last; |
642 | HANDLE dir; |
643 | WIN32_FIND_DATA fdat; |
644 | int ok; |
645 | |
646 | if ((last = strrchr(src, '/')) == NULL) |
647 | last = src; |
648 | else |
649 | last++; |
650 | if (strrchr(last, '\\') != NULL) |
651 | last = strrchr(last, '\\') + 1; |
652 | if (last == src && strchr(src, ':') != NULL) |
653 | last = strchr(src, ':') + 1; |
654 | |
655 | /* maybe send filetime */ |
656 | |
657 | sprintf(buf, "D0755 0 %s\n", last); |
658 | if (verbose) |
cc87246d |
659 | tell_user(stderr, "Entering directory: %s", buf); |
3bdaf79d |
660 | back->send(buf, strlen(buf)); |
c51a56e2 |
661 | if (response()) |
662 | return; |
663 | |
664 | sprintf(buf, "%s/*", src); |
665 | dir = FindFirstFile(buf, &fdat); |
666 | ok = (dir != INVALID_HANDLE_VALUE); |
667 | while (ok) { |
668 | if (strcmp(fdat.cFileName, ".") == 0 || |
669 | strcmp(fdat.cFileName, "..") == 0) { |
670 | } else if (strlen(src) + 1 + strlen(fdat.cFileName) >= |
671 | sizeof(buf)) { |
672 | run_err("%s/%s: Name too long", src, fdat.cFileName); |
673 | } else { |
674 | sprintf(buf, "%s/%s", src, fdat.cFileName); |
675 | source(buf); |
07d9aa13 |
676 | } |
c51a56e2 |
677 | ok = FindNextFile(dir, &fdat); |
678 | } |
679 | FindClose(dir); |
07d9aa13 |
680 | |
c51a56e2 |
681 | sprintf(buf, "E\n"); |
3bdaf79d |
682 | back->send(buf, strlen(buf)); |
c51a56e2 |
683 | (void) response(); |
07d9aa13 |
684 | } |
685 | |
07d9aa13 |
686 | /* |
687 | * Execute the sink part of the SCP protocol. |
688 | */ |
689 | static void sink(char *targ) |
690 | { |
c51a56e2 |
691 | char buf[2048]; |
692 | char namebuf[2048]; |
693 | char ch; |
694 | int targisdir = 0; |
996c8c3b |
695 | int settime; |
c51a56e2 |
696 | int exists; |
697 | DWORD attr; |
698 | HANDLE f; |
699 | unsigned long mtime, atime; |
700 | unsigned int mode; |
701 | unsigned long size, i; |
702 | int wrerror = 0; |
703 | unsigned long stat_bytes; |
704 | time_t stat_starttime, stat_lasttime; |
705 | char *stat_name; |
706 | |
707 | attr = GetFileAttributes(targ); |
996c8c3b |
708 | if (attr != (DWORD)-1 && (attr & FILE_ATTRIBUTE_DIRECTORY) != 0) |
c51a56e2 |
709 | targisdir = 1; |
710 | |
711 | if (targetshouldbedirectory && !targisdir) |
712 | bump("%s: Not a directory", targ); |
713 | |
3bdaf79d |
714 | back->send("", 1); |
c51a56e2 |
715 | while (1) { |
716 | settime = 0; |
717 | gottime: |
fb09bf1c |
718 | if (ssh_scp_recv(&ch, 1) <= 0) |
c51a56e2 |
719 | return; |
720 | if (ch == '\n') |
721 | bump("Protocol error: Unexpected newline"); |
722 | i = 0; |
723 | buf[i++] = ch; |
724 | do { |
fb09bf1c |
725 | if (ssh_scp_recv(&ch, 1) <= 0) |
c51a56e2 |
726 | bump("Lost connection"); |
727 | buf[i++] = ch; |
728 | } while (i < sizeof(buf) && ch != '\n'); |
729 | buf[i-1] = '\0'; |
730 | switch (buf[0]) { |
731 | case '\01': /* error */ |
cc87246d |
732 | tell_user(stderr, "%s\n", buf+1); |
c51a56e2 |
733 | errs++; |
734 | continue; |
735 | case '\02': /* fatal error */ |
736 | bump("%s", buf+1); |
737 | case 'E': |
3bdaf79d |
738 | back->send("", 1); |
c51a56e2 |
739 | return; |
740 | case 'T': |
1d470ad2 |
741 | if (sscanf(buf, "T%ld %*d %ld %*d", |
c51a56e2 |
742 | &mtime, &atime) == 2) { |
743 | settime = 1; |
3bdaf79d |
744 | back->send("", 1); |
c51a56e2 |
745 | goto gottime; |
746 | } |
747 | bump("Protocol error: Illegal time format"); |
748 | case 'C': |
749 | case 'D': |
750 | break; |
751 | default: |
752 | bump("Protocol error: Expected control record"); |
753 | } |
07d9aa13 |
754 | |
1d470ad2 |
755 | if (sscanf(buf+1, "%u %lu %[^\n]", &mode, &size, namebuf) != 3) |
c51a56e2 |
756 | bump("Protocol error: Illegal file descriptor format"); |
757 | if (targisdir) { |
758 | char t[2048]; |
759 | strcpy(t, targ); |
760 | if (targ[0] != '\0') |
761 | strcat(t, "/"); |
762 | strcat(t, namebuf); |
763 | strcpy(namebuf, t); |
764 | } else { |
765 | strcpy(namebuf, targ); |
766 | } |
767 | attr = GetFileAttributes(namebuf); |
996c8c3b |
768 | exists = (attr != (DWORD)-1); |
c51a56e2 |
769 | |
770 | if (buf[0] == 'D') { |
771 | if (exists && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0) { |
772 | run_err("%s: Not a directory", namebuf); |
773 | continue; |
774 | } |
775 | if (!exists) { |
776 | if (! CreateDirectory(namebuf, NULL)) { |
777 | run_err("%s: Cannot create directory", |
778 | namebuf); |
779 | continue; |
780 | } |
781 | } |
782 | sink(namebuf); |
783 | /* can we set the timestamp for directories ? */ |
784 | continue; |
785 | } |
07d9aa13 |
786 | |
c51a56e2 |
787 | f = CreateFile(namebuf, GENERIC_WRITE, 0, NULL, |
788 | CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); |
789 | if (f == INVALID_HANDLE_VALUE) { |
790 | run_err("%s: Cannot create file", namebuf); |
791 | continue; |
792 | } |
07d9aa13 |
793 | |
3bdaf79d |
794 | back->send("", 1); |
07d9aa13 |
795 | |
c51a56e2 |
796 | if (statistics) { |
797 | stat_bytes = 0; |
798 | stat_starttime = time(NULL); |
799 | stat_lasttime = 0; |
800 | if ((stat_name = strrchr(namebuf, '/')) == NULL) |
801 | stat_name = namebuf; |
802 | else |
803 | stat_name++; |
804 | if (strrchr(stat_name, '\\') != NULL) |
805 | stat_name = strrchr(stat_name, '\\') + 1; |
806 | } |
07d9aa13 |
807 | |
c51a56e2 |
808 | for (i = 0; i < size; i += 4096) { |
809 | char transbuf[4096]; |
996c8c3b |
810 | DWORD j, k = 4096; |
c51a56e2 |
811 | if (i + k > size) k = size - i; |
fb09bf1c |
812 | if (ssh_scp_recv(transbuf, k) == 0) |
c51a56e2 |
813 | bump("Lost connection"); |
814 | if (wrerror) continue; |
815 | if (! WriteFile(f, transbuf, k, &j, NULL) || j != k) { |
816 | wrerror = 1; |
817 | if (statistics) |
818 | printf("\r%-25.25s | %50s\n", |
819 | stat_name, |
820 | "Write error.. waiting for end of file"); |
821 | continue; |
822 | } |
823 | if (statistics) { |
824 | stat_bytes += k; |
825 | if (time(NULL) > stat_lasttime || |
826 | i + k == size) { |
827 | stat_lasttime = time(NULL); |
828 | print_stats(stat_name, size, stat_bytes, |
829 | stat_starttime, stat_lasttime); |
07d9aa13 |
830 | } |
c51a56e2 |
831 | } |
832 | } |
833 | (void) response(); |
07d9aa13 |
834 | |
c51a56e2 |
835 | if (settime) { |
836 | FILETIME actime, wrtime; |
837 | TIME_POSIX_TO_WIN(atime, actime); |
838 | TIME_POSIX_TO_WIN(mtime, wrtime); |
839 | SetFileTime(f, NULL, &actime, &wrtime); |
07d9aa13 |
840 | } |
07d9aa13 |
841 | |
c51a56e2 |
842 | CloseHandle(f); |
843 | if (wrerror) { |
844 | run_err("%s: Write error", namebuf); |
845 | continue; |
846 | } |
3bdaf79d |
847 | back->send("", 1); |
c51a56e2 |
848 | } |
849 | } |
07d9aa13 |
850 | |
851 | /* |
852 | * We will copy local files to a remote server. |
853 | */ |
854 | static void toremote(int argc, char *argv[]) |
855 | { |
c51a56e2 |
856 | char *src, *targ, *host, *user; |
857 | char *cmd; |
858 | int i; |
859 | |
860 | targ = argv[argc-1]; |
861 | |
39ddf0ff |
862 | /* Separate host from filename */ |
c51a56e2 |
863 | host = targ; |
864 | targ = colon(targ); |
865 | if (targ == NULL) |
866 | bump("targ == NULL in toremote()"); |
867 | *targ++ = '\0'; |
868 | if (*targ == '\0') |
869 | targ = "."; |
870 | /* Substitute "." for emtpy target */ |
871 | |
39ddf0ff |
872 | /* Separate host and username */ |
c51a56e2 |
873 | user = host; |
874 | host = strrchr(host, '@'); |
875 | if (host == NULL) { |
876 | host = user; |
877 | user = NULL; |
878 | } else { |
879 | *host++ = '\0'; |
880 | if (*user == '\0') |
881 | user = NULL; |
882 | } |
883 | |
884 | if (argc == 2) { |
885 | /* Find out if the source filespec covers multiple files |
886 | if so, we should set the targetshouldbedirectory flag */ |
887 | HANDLE fh; |
888 | WIN32_FIND_DATA fdat; |
889 | if (colon(argv[0]) != NULL) |
890 | bump("%s: Remote to remote not supported", argv[0]); |
891 | fh = FindFirstFile(argv[0], &fdat); |
892 | if (fh == INVALID_HANDLE_VALUE) |
893 | bump("%s: No such file or directory\n", argv[0]); |
894 | if (FindNextFile(fh, &fdat)) |
895 | targetshouldbedirectory = 1; |
896 | FindClose(fh); |
897 | } |
898 | |
899 | cmd = smalloc(strlen(targ) + 100); |
900 | sprintf(cmd, "scp%s%s%s%s -t %s", |
901 | verbose ? " -v" : "", |
902 | recursive ? " -r" : "", |
903 | preserve ? " -p" : "", |
904 | targetshouldbedirectory ? " -d" : "", |
905 | targ); |
906 | do_cmd(host, user, cmd); |
907 | sfree(cmd); |
908 | |
909 | (void) response(); |
910 | |
911 | for (i = 0; i < argc - 1; i++) { |
912 | HANDLE dir; |
913 | WIN32_FIND_DATA fdat; |
914 | src = argv[i]; |
915 | if (colon(src) != NULL) { |
cc87246d |
916 | tell_user(stderr, "%s: Remote to remote not supported\n", src); |
c51a56e2 |
917 | errs++; |
918 | continue; |
07d9aa13 |
919 | } |
c51a56e2 |
920 | dir = FindFirstFile(src, &fdat); |
921 | if (dir == INVALID_HANDLE_VALUE) { |
922 | run_err("%s: No such file or directory", src); |
923 | continue; |
07d9aa13 |
924 | } |
c51a56e2 |
925 | do { |
926 | char *last; |
927 | char namebuf[2048]; |
928 | if (strlen(src) + strlen(fdat.cFileName) >= |
929 | sizeof(namebuf)) { |
cc87246d |
930 | tell_user(stderr, "%s: Name too long", src); |
c51a56e2 |
931 | continue; |
932 | } |
933 | strcpy(namebuf, src); |
934 | if ((last = strrchr(namebuf, '/')) == NULL) |
935 | last = namebuf; |
936 | else |
937 | last++; |
938 | if (strrchr(last, '\\') != NULL) |
939 | last = strrchr(last, '\\') + 1; |
940 | if (last == namebuf && strrchr(namebuf, ':') != NULL) |
941 | last = strchr(namebuf, ':') + 1; |
942 | strcpy(last, fdat.cFileName); |
943 | source(namebuf); |
944 | } while (FindNextFile(dir, &fdat)); |
945 | FindClose(dir); |
946 | } |
07d9aa13 |
947 | } |
948 | |
07d9aa13 |
949 | /* |
950 | * We will copy files from a remote server to the local machine. |
951 | */ |
952 | static void tolocal(int argc, char *argv[]) |
953 | { |
c51a56e2 |
954 | char *src, *targ, *host, *user; |
955 | char *cmd; |
956 | |
957 | if (argc != 2) |
958 | bump("More than one remote source not supported"); |
959 | |
960 | src = argv[0]; |
961 | targ = argv[1]; |
962 | |
39ddf0ff |
963 | /* Separate host from filename */ |
c51a56e2 |
964 | host = src; |
965 | src = colon(src); |
966 | if (src == NULL) |
967 | bump("Local to local copy not supported"); |
968 | *src++ = '\0'; |
969 | if (*src == '\0') |
970 | src = "."; |
971 | /* Substitute "." for empty filename */ |
972 | |
39ddf0ff |
973 | /* Separate username and hostname */ |
c51a56e2 |
974 | user = host; |
975 | host = strrchr(host, '@'); |
976 | if (host == NULL) { |
977 | host = user; |
978 | user = NULL; |
979 | } else { |
980 | *host++ = '\0'; |
981 | if (*user == '\0') |
982 | user = NULL; |
983 | } |
984 | |
985 | cmd = smalloc(strlen(src) + 100); |
986 | sprintf(cmd, "scp%s%s%s%s -f %s", |
987 | verbose ? " -v" : "", |
988 | recursive ? " -r" : "", |
989 | preserve ? " -p" : "", |
990 | targetshouldbedirectory ? " -d" : "", |
991 | src); |
992 | do_cmd(host, user, cmd); |
993 | sfree(cmd); |
994 | |
995 | sink(targ); |
07d9aa13 |
996 | } |
997 | |
07d9aa13 |
998 | /* |
39ddf0ff |
999 | * We will issue a list command to get a remote directory. |
1000 | */ |
1001 | static void get_dir_list(int argc, char *argv[]) |
1002 | { |
1003 | char *src, *host, *user; |
1004 | char *cmd, *p, *q; |
1005 | char c; |
1006 | |
1007 | src = argv[0]; |
1008 | |
1009 | /* Separate host from filename */ |
1010 | host = src; |
1011 | src = colon(src); |
1012 | if (src == NULL) |
1013 | bump("Local to local copy not supported"); |
1014 | *src++ = '\0'; |
1015 | if (*src == '\0') |
1016 | src = "."; |
1017 | /* Substitute "." for empty filename */ |
1018 | |
1019 | /* Separate username and hostname */ |
1020 | user = host; |
1021 | host = strrchr(host, '@'); |
1022 | if (host == NULL) { |
1023 | host = user; |
1024 | user = NULL; |
1025 | } else { |
1026 | *host++ = '\0'; |
1027 | if (*user == '\0') |
1028 | user = NULL; |
1029 | } |
1030 | |
1031 | cmd = smalloc(4*strlen(src) + 100); |
1032 | strcpy(cmd, "ls -la '"); |
1033 | p = cmd + strlen(cmd); |
1034 | for (q = src; *q; q++) { |
1035 | if (*q == '\'') { |
1036 | *p++ = '\''; *p++ = '\\'; *p++ = '\''; *p++ = '\''; |
1037 | } else { |
1038 | *p++ = *q; |
1039 | } |
1040 | } |
1041 | *p++ = '\''; |
1042 | *p = '\0'; |
cc87246d |
1043 | |
39ddf0ff |
1044 | do_cmd(host, user, cmd); |
1045 | sfree(cmd); |
1046 | |
fb09bf1c |
1047 | while (ssh_scp_recv(&c, 1) > 0) |
cc87246d |
1048 | tell_char(stdout, c); |
39ddf0ff |
1049 | } |
1050 | |
1051 | /* |
07d9aa13 |
1052 | * Initialize the Win$ock driver. |
1053 | */ |
996c8c3b |
1054 | static void init_winsock(void) |
07d9aa13 |
1055 | { |
c51a56e2 |
1056 | WORD winsock_ver; |
1057 | WSADATA wsadata; |
1058 | |
1059 | winsock_ver = MAKEWORD(1, 1); |
1060 | if (WSAStartup(winsock_ver, &wsadata)) |
1061 | bump("Unable to initialise WinSock"); |
1062 | if (LOBYTE(wsadata.wVersion) != 1 || |
1063 | HIBYTE(wsadata.wVersion) != 1) |
1064 | bump("WinSock version is incompatible with 1.1"); |
07d9aa13 |
1065 | } |
1066 | |
07d9aa13 |
1067 | /* |
1068 | * Short description of parameters. |
1069 | */ |
996c8c3b |
1070 | static void usage(void) |
07d9aa13 |
1071 | { |
c51a56e2 |
1072 | printf("PuTTY Secure Copy client\n"); |
1073 | printf("%s\n", ver); |
a3e55ea1 |
1074 | printf("Usage: pscp [options] [user@]host:source target\n"); |
1075 | printf(" pscp [options] source [source...] [user@]host:target\n"); |
1076 | printf(" pscp [options] -ls user@host:filespec\n"); |
b8a19193 |
1077 | printf("Options:\n"); |
1078 | printf(" -p preserve file attributes\n"); |
1079 | printf(" -q quiet, don't show statistics\n"); |
1080 | printf(" -r copy directories recursively\n"); |
1081 | printf(" -v show verbose messages\n"); |
1082 | printf(" -P port connect to specified port\n"); |
1083 | printf(" -pw passw login with specified password\n"); |
cc87246d |
1084 | /* GUI Adaptation - Sept 2000 */ |
1085 | printf(" -gui hWnd GUI mode with the windows handle for receiving messages\n"); |
c51a56e2 |
1086 | exit(1); |
07d9aa13 |
1087 | } |
1088 | |
07d9aa13 |
1089 | /* |
1090 | * Main program (no, really?) |
1091 | */ |
1092 | int main(int argc, char *argv[]) |
1093 | { |
c51a56e2 |
1094 | int i; |
39ddf0ff |
1095 | int list = 0; |
c51a56e2 |
1096 | |
fb09bf1c |
1097 | default_protocol = PROT_TELNET; |
1098 | |
67779be7 |
1099 | flags = FLAG_STDERR; |
fb09bf1c |
1100 | ssh_get_password = &get_password; |
c51a56e2 |
1101 | init_winsock(); |
1102 | |
1103 | for (i = 1; i < argc; i++) { |
1104 | if (argv[i][0] != '-') |
1105 | break; |
1106 | if (strcmp(argv[i], "-v") == 0) |
4017be6d |
1107 | verbose = 1, flags |= FLAG_VERBOSE; |
c51a56e2 |
1108 | else if (strcmp(argv[i], "-r") == 0) |
1109 | recursive = 1; |
1110 | else if (strcmp(argv[i], "-p") == 0) |
1111 | preserve = 1; |
1112 | else if (strcmp(argv[i], "-q") == 0) |
1113 | statistics = 0; |
1114 | else if (strcmp(argv[i], "-h") == 0 || |
1115 | strcmp(argv[i], "-?") == 0) |
1116 | usage(); |
b8a19193 |
1117 | else if (strcmp(argv[i], "-P") == 0 && i+1 < argc) |
1118 | portnumber = atoi(argv[++i]); |
1119 | else if (strcmp(argv[i], "-pw") == 0 && i+1 < argc) |
1120 | password = argv[++i]; |
cc87246d |
1121 | else if (strcmp(argv[i], "-gui") == 0 && i+1 < argc) { |
1122 | gui_hwnd = argv[++i]; |
1123 | gui_mode = 1; |
1124 | } else if (strcmp(argv[i], "-ls") == 0) |
1125 | list = 1; |
c51a56e2 |
1126 | else if (strcmp(argv[i], "--") == 0) |
1127 | { i++; break; } |
07d9aa13 |
1128 | else |
c51a56e2 |
1129 | usage(); |
1130 | } |
1131 | argc -= i; |
1132 | argv += i; |
1133 | |
39ddf0ff |
1134 | if (list) { |
1135 | if (argc != 1) |
1136 | usage(); |
1137 | get_dir_list(argc, argv); |
c51a56e2 |
1138 | |
39ddf0ff |
1139 | } else { |
1140 | |
1141 | if (argc < 2) |
1142 | usage(); |
1143 | if (argc > 2) |
1144 | targetshouldbedirectory = 1; |
1145 | |
1146 | if (colon(argv[argc-1]) != NULL) |
1147 | toremote(argc, argv); |
1148 | else |
1149 | tolocal(argc, argv); |
1150 | } |
c51a56e2 |
1151 | |
1152 | if (connection_open) { |
1153 | char ch; |
3bdaf79d |
1154 | back->special(TS_EOF); |
fb09bf1c |
1155 | ssh_scp_recv(&ch, 1); |
c51a56e2 |
1156 | } |
1157 | WSACleanup(); |
1158 | random_save_seed(); |
07d9aa13 |
1159 | |
cc87246d |
1160 | /* GUI Adaptation - August 2000 */ |
1161 | if (gui_mode) { |
1162 | unsigned int msg_id = WM_RET_ERR_CNT; |
1163 | if (list) msg_id = WM_LS_RET_ERR_CNT; |
1164 | while (!PostMessage( (HWND)atoi(gui_hwnd), msg_id, (WPARAM)errs, 0/*lParam*/ ) ) |
1165 | SleepEx(1000,TRUE); |
1166 | } |
c51a56e2 |
1167 | return (errs == 0 ? 0 : 1); |
07d9aa13 |
1168 | } |
1169 | |
1170 | /* end */ |