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