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