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