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