1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24
25 #include "curl_setup.h"
26
27 #ifdef USE_WOLFSSH
28
29 #include <limits.h>
30
31 #include "urldata.h"
32 #include "cfilters.h"
33 #include "connect.h"
34 #include "sendf.h"
35 #include "progress.h"
36 #include "curl_path.h"
37 #include "strtoofft.h"
38 #include "transfer.h"
39 #include "speedcheck.h"
40 #include "select.h"
41 #include "multiif.h"
42 #include "warnless.h"
43 #include "strdup.h"
44
45 /* The last 3 #include files should be in this order */
46 #include "curl_printf.h"
47 #include "curl_memory.h"
48 #include "memdebug.h"
49
50 static CURLcode wssh_connect(struct Curl_easy *data, bool *done);
51 static CURLcode wssh_multi_statemach(struct Curl_easy *data, bool *done);
52 static CURLcode wssh_do(struct Curl_easy *data, bool *done);
53 #if 0
54 static CURLcode wscp_done(struct Curl_easy *data,
55 CURLcode, bool premature);
56 static CURLcode wscp_doing(struct Curl_easy *data,
57 bool *dophase_done);
58 static CURLcode wscp_disconnect(struct Curl_easy *data,
59 struct connectdata *conn,
60 bool dead_connection);
61 #endif
62 static CURLcode wsftp_done(struct Curl_easy *data,
63 CURLcode, bool premature);
64 static CURLcode wsftp_doing(struct Curl_easy *data,
65 bool *dophase_done);
66 static CURLcode wsftp_disconnect(struct Curl_easy *data,
67 struct connectdata *conn,
68 bool dead);
69 static int wssh_getsock(struct Curl_easy *data,
70 struct connectdata *conn,
71 curl_socket_t *sock);
72 static CURLcode wssh_setup_connection(struct Curl_easy *data,
73 struct connectdata *conn);
74
75 #if 0
76 /*
77 * SCP protocol handler.
78 */
79
80 const struct Curl_handler Curl_handler_scp = {
81 "SCP", /* scheme */
82 wssh_setup_connection, /* setup_connection */
83 wssh_do, /* do_it */
84 wscp_done, /* done */
85 ZERO_NULL, /* do_more */
86 wssh_connect, /* connect_it */
87 wssh_multi_statemach, /* connecting */
88 wscp_doing, /* doing */
89 wssh_getsock, /* proto_getsock */
90 wssh_getsock, /* doing_getsock */
91 ZERO_NULL, /* domore_getsock */
92 wssh_getsock, /* perform_getsock */
93 wscp_disconnect, /* disconnect */
94 ZERO_NULL, /* write_resp */
95 ZERO_NULL, /* write_resp_hd */
96 ZERO_NULL, /* connection_check */
97 ZERO_NULL, /* attach connection */
98 PORT_SSH, /* defport */
99 CURLPROTO_SCP, /* protocol */
100 PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
101 | PROTOPT_NOURLQUERY /* flags */
102 };
103
104 #endif
105
106 /*
107 * SFTP protocol handler.
108 */
109
110 const struct Curl_handler Curl_handler_sftp = {
111 "SFTP", /* scheme */
112 wssh_setup_connection, /* setup_connection */
113 wssh_do, /* do_it */
114 wsftp_done, /* done */
115 ZERO_NULL, /* do_more */
116 wssh_connect, /* connect_it */
117 wssh_multi_statemach, /* connecting */
118 wsftp_doing, /* doing */
119 wssh_getsock, /* proto_getsock */
120 wssh_getsock, /* doing_getsock */
121 ZERO_NULL, /* domore_getsock */
122 wssh_getsock, /* perform_getsock */
123 wsftp_disconnect, /* disconnect */
124 ZERO_NULL, /* write_resp */
125 ZERO_NULL, /* write_resp_hd */
126 ZERO_NULL, /* connection_check */
127 ZERO_NULL, /* attach connection */
128 PORT_SSH, /* defport */
129 CURLPROTO_SFTP, /* protocol */
130 CURLPROTO_SFTP, /* family */
131 PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
132 | PROTOPT_NOURLQUERY /* flags */
133 };
134
135 /*
136 * SSH State machine related code
137 */
138 /* This is the ONLY way to change SSH state! */
state(struct Curl_easy * data,sshstate nowstate)139 static void state(struct Curl_easy *data, sshstate nowstate)
140 {
141 struct connectdata *conn = data->conn;
142 struct ssh_conn *sshc = &conn->proto.sshc;
143 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
144 /* for debug purposes */
145 static const char * const names[] = {
146 "SSH_STOP",
147 "SSH_INIT",
148 "SSH_S_STARTUP",
149 "SSH_HOSTKEY",
150 "SSH_AUTHLIST",
151 "SSH_AUTH_PKEY_INIT",
152 "SSH_AUTH_PKEY",
153 "SSH_AUTH_PASS_INIT",
154 "SSH_AUTH_PASS",
155 "SSH_AUTH_AGENT_INIT",
156 "SSH_AUTH_AGENT_LIST",
157 "SSH_AUTH_AGENT",
158 "SSH_AUTH_HOST_INIT",
159 "SSH_AUTH_HOST",
160 "SSH_AUTH_KEY_INIT",
161 "SSH_AUTH_KEY",
162 "SSH_AUTH_GSSAPI",
163 "SSH_AUTH_DONE",
164 "SSH_SFTP_INIT",
165 "SSH_SFTP_REALPATH",
166 "SSH_SFTP_QUOTE_INIT",
167 "SSH_SFTP_POSTQUOTE_INIT",
168 "SSH_SFTP_QUOTE",
169 "SSH_SFTP_NEXT_QUOTE",
170 "SSH_SFTP_QUOTE_STAT",
171 "SSH_SFTP_QUOTE_SETSTAT",
172 "SSH_SFTP_QUOTE_SYMLINK",
173 "SSH_SFTP_QUOTE_MKDIR",
174 "SSH_SFTP_QUOTE_RENAME",
175 "SSH_SFTP_QUOTE_RMDIR",
176 "SSH_SFTP_QUOTE_UNLINK",
177 "SSH_SFTP_QUOTE_STATVFS",
178 "SSH_SFTP_GETINFO",
179 "SSH_SFTP_FILETIME",
180 "SSH_SFTP_TRANS_INIT",
181 "SSH_SFTP_UPLOAD_INIT",
182 "SSH_SFTP_CREATE_DIRS_INIT",
183 "SSH_SFTP_CREATE_DIRS",
184 "SSH_SFTP_CREATE_DIRS_MKDIR",
185 "SSH_SFTP_READDIR_INIT",
186 "SSH_SFTP_READDIR",
187 "SSH_SFTP_READDIR_LINK",
188 "SSH_SFTP_READDIR_BOTTOM",
189 "SSH_SFTP_READDIR_DONE",
190 "SSH_SFTP_DOWNLOAD_INIT",
191 "SSH_SFTP_DOWNLOAD_STAT",
192 "SSH_SFTP_CLOSE",
193 "SSH_SFTP_SHUTDOWN",
194 "SSH_SCP_TRANS_INIT",
195 "SSH_SCP_UPLOAD_INIT",
196 "SSH_SCP_DOWNLOAD_INIT",
197 "SSH_SCP_DOWNLOAD",
198 "SSH_SCP_DONE",
199 "SSH_SCP_SEND_EOF",
200 "SSH_SCP_WAIT_EOF",
201 "SSH_SCP_WAIT_CLOSE",
202 "SSH_SCP_CHANNEL_FREE",
203 "SSH_SESSION_DISCONNECT",
204 "SSH_SESSION_FREE",
205 "QUIT"
206 };
207
208 /* a precaution to make sure the lists are in sync */
209 DEBUGASSERT(sizeof(names)/sizeof(names[0]) == SSH_LAST);
210
211 if(sshc->state != nowstate) {
212 infof(data, "wolfssh %p state change from %s to %s",
213 (void *)sshc, names[sshc->state], names[nowstate]);
214 }
215 #endif
216
217 sshc->state = nowstate;
218 }
219
wscp_send(struct Curl_easy * data,int sockindex,const void * mem,size_t len,bool eos,CURLcode * err)220 static ssize_t wscp_send(struct Curl_easy *data, int sockindex,
221 const void *mem, size_t len, bool eos,
222 CURLcode *err)
223 {
224 ssize_t nwrite = 0;
225 (void)data;
226 (void)sockindex; /* we only support SCP on the fixed known primary socket */
227 (void)mem;
228 (void)len;
229 (void)eos;
230 (void)err;
231
232 return nwrite;
233 }
234
wscp_recv(struct Curl_easy * data,int sockindex,char * mem,size_t len,CURLcode * err)235 static ssize_t wscp_recv(struct Curl_easy *data, int sockindex,
236 char *mem, size_t len, CURLcode *err)
237 {
238 ssize_t nread = 0;
239 (void)data;
240 (void)sockindex; /* we only support SCP on the fixed known primary socket */
241 (void)mem;
242 (void)len;
243 (void)err;
244
245 return nread;
246 }
247
248 /* return number of sent bytes */
wsftp_send(struct Curl_easy * data,int sockindex,const void * mem,size_t len,bool eos,CURLcode * err)249 static ssize_t wsftp_send(struct Curl_easy *data, int sockindex,
250 const void *mem, size_t len, bool eos, CURLcode *err)
251 {
252 struct connectdata *conn = data->conn;
253 struct ssh_conn *sshc = &conn->proto.sshc;
254 word32 offset[2];
255 int rc;
256 (void)sockindex;
257 (void)eos;
258
259 offset[0] = (word32)sshc->offset & 0xFFFFFFFF;
260 offset[1] = (word32)(sshc->offset >> 32) & 0xFFFFFFFF;
261
262 rc = wolfSSH_SFTP_SendWritePacket(sshc->ssh_session, sshc->handle,
263 sshc->handleSz,
264 &offset[0],
265 (byte *)mem, (word32)len);
266
267 if(rc == WS_FATAL_ERROR)
268 rc = wolfSSH_get_error(sshc->ssh_session);
269 if(rc == WS_WANT_READ) {
270 conn->waitfor = KEEP_RECV;
271 *err = CURLE_AGAIN;
272 return -1;
273 }
274 else if(rc == WS_WANT_WRITE) {
275 conn->waitfor = KEEP_SEND;
276 *err = CURLE_AGAIN;
277 return -1;
278 }
279 if(rc < 0) {
280 failf(data, "wolfSSH_SFTP_SendWritePacket returned %d", rc);
281 return -1;
282 }
283 DEBUGASSERT(rc == (int)len);
284 infof(data, "sent %zu bytes SFTP from offset %" FMT_OFF_T,
285 len, sshc->offset);
286 sshc->offset += len;
287 return (ssize_t)rc;
288 }
289
290 /*
291 * Return number of received (decrypted) bytes
292 * or <0 on error
293 */
wsftp_recv(struct Curl_easy * data,int sockindex,char * mem,size_t len,CURLcode * err)294 static ssize_t wsftp_recv(struct Curl_easy *data, int sockindex,
295 char *mem, size_t len, CURLcode *err)
296 {
297 int rc;
298 struct connectdata *conn = data->conn;
299 struct ssh_conn *sshc = &conn->proto.sshc;
300 word32 offset[2];
301 (void)sockindex;
302
303 offset[0] = (word32)sshc->offset & 0xFFFFFFFF;
304 offset[1] = (word32)(sshc->offset >> 32) & 0xFFFFFFFF;
305
306 rc = wolfSSH_SFTP_SendReadPacket(sshc->ssh_session, sshc->handle,
307 sshc->handleSz,
308 &offset[0],
309 (byte *)mem, (word32)len);
310 if(rc == WS_FATAL_ERROR)
311 rc = wolfSSH_get_error(sshc->ssh_session);
312 if(rc == WS_WANT_READ) {
313 conn->waitfor = KEEP_RECV;
314 *err = CURLE_AGAIN;
315 return -1;
316 }
317 else if(rc == WS_WANT_WRITE) {
318 conn->waitfor = KEEP_SEND;
319 *err = CURLE_AGAIN;
320 return -1;
321 }
322
323 DEBUGASSERT(rc <= (int)len);
324
325 if(rc < 0) {
326 failf(data, "wolfSSH_SFTP_SendReadPacket returned %d", rc);
327 return -1;
328 }
329 sshc->offset += len;
330
331 return (ssize_t)rc;
332 }
333
334 /*
335 * SSH setup and connection
336 */
wssh_setup_connection(struct Curl_easy * data,struct connectdata * conn)337 static CURLcode wssh_setup_connection(struct Curl_easy *data,
338 struct connectdata *conn)
339 {
340 struct SSHPROTO *ssh;
341 (void)conn;
342
343 data->req.p.ssh = ssh = calloc(1, sizeof(struct SSHPROTO));
344 if(!ssh)
345 return CURLE_OUT_OF_MEMORY;
346
347 return CURLE_OK;
348 }
349
userauth(byte authtype,WS_UserAuthData * authdata,void * ctx)350 static int userauth(byte authtype,
351 WS_UserAuthData* authdata,
352 void *ctx)
353 {
354 struct Curl_easy *data = ctx;
355 DEBUGF(infof(data, "wolfssh callback: type %s",
356 authtype == WOLFSSH_USERAUTH_PASSWORD ? "PASSWORD" :
357 "PUBLICCKEY"));
358 if(authtype == WOLFSSH_USERAUTH_PASSWORD) {
359 authdata->sf.password.password = (byte *)data->conn->passwd;
360 authdata->sf.password.passwordSz = (word32) strlen(data->conn->passwd);
361 }
362
363 return 0;
364 }
365
wssh_connect(struct Curl_easy * data,bool * done)366 static CURLcode wssh_connect(struct Curl_easy *data, bool *done)
367 {
368 struct connectdata *conn = data->conn;
369 struct ssh_conn *sshc;
370 curl_socket_t sock = conn->sock[FIRSTSOCKET];
371 int rc;
372
373 /* initialize per-handle data if not already */
374 if(!data->req.p.ssh)
375 wssh_setup_connection(data, conn);
376
377 /* We default to persistent connections. We set this already in this connect
378 function to make the reuse checks properly be able to check this bit. */
379 connkeep(conn, "SSH default");
380
381 if(conn->handler->protocol & CURLPROTO_SCP) {
382 conn->recv[FIRSTSOCKET] = wscp_recv;
383 conn->send[FIRSTSOCKET] = wscp_send;
384 }
385 else {
386 conn->recv[FIRSTSOCKET] = wsftp_recv;
387 conn->send[FIRSTSOCKET] = wsftp_send;
388 }
389 sshc = &conn->proto.sshc;
390 sshc->ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_CLIENT, NULL);
391 if(!sshc->ctx) {
392 failf(data, "No wolfSSH context");
393 goto error;
394 }
395
396 sshc->ssh_session = wolfSSH_new(sshc->ctx);
397 if(!sshc->ssh_session) {
398 failf(data, "No wolfSSH session");
399 goto error;
400 }
401
402 rc = wolfSSH_SetUsername(sshc->ssh_session, conn->user);
403 if(rc != WS_SUCCESS) {
404 failf(data, "wolfSSH failed to set username");
405 goto error;
406 }
407
408 /* set callback for authentication */
409 wolfSSH_SetUserAuth(sshc->ctx, userauth);
410 wolfSSH_SetUserAuthCtx(sshc->ssh_session, data);
411
412 rc = wolfSSH_set_fd(sshc->ssh_session, (int)sock);
413 if(rc) {
414 failf(data, "wolfSSH failed to set socket");
415 goto error;
416 }
417
418 #if 0
419 wolfSSH_Debugging_ON();
420 #endif
421
422 *done = TRUE;
423 if(conn->handler->protocol & CURLPROTO_SCP)
424 state(data, SSH_INIT);
425 else
426 state(data, SSH_SFTP_INIT);
427
428 return wssh_multi_statemach(data, done);
429 error:
430 wolfSSH_free(sshc->ssh_session);
431 wolfSSH_CTX_free(sshc->ctx);
432 return CURLE_FAILED_INIT;
433 }
434
435 /*
436 * wssh_statemach_act() runs the SSH state machine as far as it can without
437 * blocking and without reaching the end. The data the pointer 'block' points
438 * to will be set to TRUE if the wolfssh function returns EAGAIN meaning it
439 * wants to be called again when the socket is ready
440 */
441
wssh_statemach_act(struct Curl_easy * data,bool * block)442 static CURLcode wssh_statemach_act(struct Curl_easy *data, bool *block)
443 {
444 CURLcode result = CURLE_OK;
445 struct connectdata *conn = data->conn;
446 struct ssh_conn *sshc = &conn->proto.sshc;
447 struct SSHPROTO *sftp_scp = data->req.p.ssh;
448 WS_SFTPNAME *name;
449 int rc = 0;
450 *block = FALSE; /* we are not blocking by default */
451
452 do {
453 switch(sshc->state) {
454 case SSH_INIT:
455 state(data, SSH_S_STARTUP);
456 break;
457
458 case SSH_S_STARTUP:
459 rc = wolfSSH_connect(sshc->ssh_session);
460 if(rc != WS_SUCCESS)
461 rc = wolfSSH_get_error(sshc->ssh_session);
462 if(rc == WS_WANT_READ) {
463 *block = TRUE;
464 conn->waitfor = KEEP_RECV;
465 return CURLE_OK;
466 }
467 else if(rc == WS_WANT_WRITE) {
468 *block = TRUE;
469 conn->waitfor = KEEP_SEND;
470 return CURLE_OK;
471 }
472 else if(rc != WS_SUCCESS) {
473 state(data, SSH_STOP);
474 return CURLE_SSH;
475 }
476 infof(data, "wolfssh connected");
477 state(data, SSH_STOP);
478 break;
479 case SSH_STOP:
480 break;
481
482 case SSH_SFTP_INIT:
483 rc = wolfSSH_SFTP_connect(sshc->ssh_session);
484 if(rc != WS_SUCCESS)
485 rc = wolfSSH_get_error(sshc->ssh_session);
486 if(rc == WS_WANT_READ) {
487 *block = TRUE;
488 conn->waitfor = KEEP_RECV;
489 return CURLE_OK;
490 }
491 else if(rc == WS_WANT_WRITE) {
492 *block = TRUE;
493 conn->waitfor = KEEP_SEND;
494 return CURLE_OK;
495 }
496 else if(rc == WS_SUCCESS) {
497 infof(data, "wolfssh SFTP connected");
498 state(data, SSH_SFTP_REALPATH);
499 }
500 else {
501 failf(data, "wolfssh SFTP connect error %d", rc);
502 return CURLE_SSH;
503 }
504 break;
505 case SSH_SFTP_REALPATH:
506 name = wolfSSH_SFTP_RealPath(sshc->ssh_session, (char *)".");
507 rc = wolfSSH_get_error(sshc->ssh_session);
508 if(rc == WS_WANT_READ) {
509 *block = TRUE;
510 conn->waitfor = KEEP_RECV;
511 return CURLE_OK;
512 }
513 else if(rc == WS_WANT_WRITE) {
514 *block = TRUE;
515 conn->waitfor = KEEP_SEND;
516 return CURLE_OK;
517 }
518 else if(name && (rc == WS_SUCCESS)) {
519 sshc->homedir = Curl_memdup0(name->fName, name->fSz);
520 if(!sshc->homedir)
521 sshc->actualcode = CURLE_OUT_OF_MEMORY;
522 wolfSSH_SFTPNAME_list_free(name);
523 state(data, SSH_STOP);
524 return CURLE_OK;
525 }
526 failf(data, "wolfssh SFTP realpath %d", rc);
527 return CURLE_SSH;
528
529 case SSH_SFTP_QUOTE_INIT:
530 result = Curl_getworkingpath(data, sshc->homedir, &sftp_scp->path);
531 if(result) {
532 sshc->actualcode = result;
533 state(data, SSH_STOP);
534 break;
535 }
536
537 if(data->set.quote) {
538 infof(data, "Sending quote commands");
539 sshc->quote_item = data->set.quote;
540 state(data, SSH_SFTP_QUOTE);
541 }
542 else {
543 state(data, SSH_SFTP_GETINFO);
544 }
545 break;
546 case SSH_SFTP_GETINFO:
547 if(data->set.get_filetime) {
548 state(data, SSH_SFTP_FILETIME);
549 }
550 else {
551 state(data, SSH_SFTP_TRANS_INIT);
552 }
553 break;
554 case SSH_SFTP_TRANS_INIT:
555 if(data->state.upload)
556 state(data, SSH_SFTP_UPLOAD_INIT);
557 else {
558 if(sftp_scp->path[strlen(sftp_scp->path)-1] == '/')
559 state(data, SSH_SFTP_READDIR_INIT);
560 else
561 state(data, SSH_SFTP_DOWNLOAD_INIT);
562 }
563 break;
564 case SSH_SFTP_UPLOAD_INIT: {
565 word32 flags;
566 WS_SFTP_FILEATRB createattrs;
567 if(data->state.resume_from) {
568 WS_SFTP_FILEATRB attrs;
569 if(data->state.resume_from < 0) {
570 rc = wolfSSH_SFTP_STAT(sshc->ssh_session, sftp_scp->path,
571 &attrs);
572 if(rc != WS_SUCCESS)
573 break;
574
575 if(rc) {
576 data->state.resume_from = 0;
577 }
578 else {
579 curl_off_t size = ((curl_off_t)attrs.sz[1] << 32) | attrs.sz[0];
580 if(size < 0) {
581 failf(data, "Bad file size (%" FMT_OFF_T ")", size);
582 return CURLE_BAD_DOWNLOAD_RESUME;
583 }
584 data->state.resume_from = size;
585 }
586 }
587 }
588
589 if(data->set.remote_append)
590 /* Try to open for append, but create if nonexisting */
591 flags = WOLFSSH_FXF_WRITE|WOLFSSH_FXF_CREAT|WOLFSSH_FXF_APPEND;
592 else if(data->state.resume_from > 0)
593 /* If we have restart position then open for append */
594 flags = WOLFSSH_FXF_WRITE|WOLFSSH_FXF_APPEND;
595 else
596 /* Clear file before writing (normal behavior) */
597 flags = WOLFSSH_FXF_WRITE|WOLFSSH_FXF_CREAT|WOLFSSH_FXF_TRUNC;
598
599 memset(&createattrs, 0, sizeof(createattrs));
600 createattrs.per = (word32)data->set.new_file_perms;
601 sshc->handleSz = sizeof(sshc->handle);
602 rc = wolfSSH_SFTP_Open(sshc->ssh_session, sftp_scp->path,
603 flags, &createattrs,
604 sshc->handle, &sshc->handleSz);
605 if(rc == WS_FATAL_ERROR)
606 rc = wolfSSH_get_error(sshc->ssh_session);
607 if(rc == WS_WANT_READ) {
608 *block = TRUE;
609 conn->waitfor = KEEP_RECV;
610 return CURLE_OK;
611 }
612 else if(rc == WS_WANT_WRITE) {
613 *block = TRUE;
614 conn->waitfor = KEEP_SEND;
615 return CURLE_OK;
616 }
617 else if(rc == WS_SUCCESS) {
618 infof(data, "wolfssh SFTP open succeeded");
619 }
620 else {
621 failf(data, "wolfssh SFTP upload open failed: %d", rc);
622 return CURLE_SSH;
623 }
624 state(data, SSH_SFTP_DOWNLOAD_STAT);
625
626 /* If we have a restart point then we need to seek to the correct
627 position. */
628 if(data->state.resume_from > 0) {
629 /* Let's read off the proper amount of bytes from the input. */
630 int seekerr = CURL_SEEKFUNC_OK;
631 if(data->set.seek_func) {
632 Curl_set_in_callback(data, TRUE);
633 seekerr = data->set.seek_func(data->set.seek_client,
634 data->state.resume_from, SEEK_SET);
635 Curl_set_in_callback(data, FALSE);
636 }
637
638 if(seekerr != CURL_SEEKFUNC_OK) {
639 curl_off_t passed = 0;
640
641 if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
642 failf(data, "Could not seek stream");
643 return CURLE_FTP_COULDNT_USE_REST;
644 }
645 /* seekerr == CURL_SEEKFUNC_CANTSEEK (cannot seek to offset) */
646 do {
647 char scratch[4*1024];
648 size_t readthisamountnow =
649 (data->state.resume_from - passed >
650 (curl_off_t)sizeof(scratch)) ?
651 sizeof(scratch) : curlx_sotouz(data->state.resume_from - passed);
652
653 size_t actuallyread;
654 Curl_set_in_callback(data, TRUE);
655 actuallyread = data->state.fread_func(scratch, 1,
656 readthisamountnow,
657 data->state.in);
658 Curl_set_in_callback(data, FALSE);
659
660 passed += actuallyread;
661 if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
662 /* this checks for greater-than only to make sure that the
663 CURL_READFUNC_ABORT return code still aborts */
664 failf(data, "Failed to read data");
665 return CURLE_FTP_COULDNT_USE_REST;
666 }
667 } while(passed < data->state.resume_from);
668 }
669
670 /* now, decrease the size of the read */
671 if(data->state.infilesize > 0) {
672 data->state.infilesize -= data->state.resume_from;
673 data->req.size = data->state.infilesize;
674 Curl_pgrsSetUploadSize(data, data->state.infilesize);
675 }
676
677 sshc->offset += data->state.resume_from;
678 }
679 if(data->state.infilesize > 0) {
680 data->req.size = data->state.infilesize;
681 Curl_pgrsSetUploadSize(data, data->state.infilesize);
682 }
683 /* upload data */
684 Curl_xfer_setup1(data, CURL_XFER_SEND, -1, FALSE);
685
686 /* not set by Curl_xfer_setup to preserve keepon bits */
687 conn->sockfd = conn->writesockfd;
688
689 if(result) {
690 state(data, SSH_SFTP_CLOSE);
691 sshc->actualcode = result;
692 }
693 else {
694 /* store this original bitmask setup to use later on if we cannot
695 figure out a "real" bitmask */
696 sshc->orig_waitfor = data->req.keepon;
697
698 /* we want to use the _sending_ function even when the socket turns
699 out readable as the underlying libssh2 sftp send function will deal
700 with both accordingly */
701 data->state.select_bits = CURL_CSELECT_OUT;
702
703 /* since we do not really wait for anything at this point, we want the
704 state machine to move on as soon as possible so we set a very short
705 timeout here */
706 Curl_expire(data, 0, EXPIRE_RUN_NOW);
707
708 state(data, SSH_STOP);
709 }
710 break;
711 }
712 case SSH_SFTP_DOWNLOAD_INIT:
713 sshc->handleSz = sizeof(sshc->handle);
714 rc = wolfSSH_SFTP_Open(sshc->ssh_session, sftp_scp->path,
715 WOLFSSH_FXF_READ, NULL,
716 sshc->handle, &sshc->handleSz);
717 if(rc == WS_FATAL_ERROR)
718 rc = wolfSSH_get_error(sshc->ssh_session);
719 if(rc == WS_WANT_READ) {
720 *block = TRUE;
721 conn->waitfor = KEEP_RECV;
722 return CURLE_OK;
723 }
724 else if(rc == WS_WANT_WRITE) {
725 *block = TRUE;
726 conn->waitfor = KEEP_SEND;
727 return CURLE_OK;
728 }
729 else if(rc == WS_SUCCESS) {
730 infof(data, "wolfssh SFTP open succeeded");
731 state(data, SSH_SFTP_DOWNLOAD_STAT);
732 return CURLE_OK;
733 }
734
735 failf(data, "wolfssh SFTP open failed: %d", rc);
736 return CURLE_SSH;
737
738 case SSH_SFTP_DOWNLOAD_STAT: {
739 WS_SFTP_FILEATRB attrs;
740 curl_off_t size;
741
742 rc = wolfSSH_SFTP_STAT(sshc->ssh_session, sftp_scp->path, &attrs);
743 if(rc == WS_FATAL_ERROR)
744 rc = wolfSSH_get_error(sshc->ssh_session);
745 if(rc == WS_WANT_READ) {
746 *block = TRUE;
747 conn->waitfor = KEEP_RECV;
748 return CURLE_OK;
749 }
750 else if(rc == WS_WANT_WRITE) {
751 *block = TRUE;
752 conn->waitfor = KEEP_SEND;
753 return CURLE_OK;
754 }
755 else if(rc == WS_SUCCESS) {
756 infof(data, "wolfssh STAT succeeded");
757 }
758 else {
759 failf(data, "wolfssh SFTP open failed: %d", rc);
760 data->req.size = -1;
761 data->req.maxdownload = -1;
762 Curl_pgrsSetDownloadSize(data, -1);
763 return CURLE_SSH;
764 }
765
766 size = ((curl_off_t)attrs.sz[1] << 32) | attrs.sz[0];
767
768 data->req.size = size;
769 data->req.maxdownload = size;
770 Curl_pgrsSetDownloadSize(data, size);
771
772 infof(data, "SFTP download %" FMT_OFF_T " bytes", size);
773
774 /* We cannot seek with wolfSSH so resuming and range requests are not
775 possible */
776 if(data->state.use_range || data->state.resume_from) {
777 infof(data, "wolfSSH cannot do range/seek on SFTP");
778 return CURLE_BAD_DOWNLOAD_RESUME;
779 }
780
781 /* Setup the actual download */
782 if(data->req.size == 0) {
783 /* no data to transfer */
784 Curl_xfer_setup_nop(data);
785 infof(data, "File already completely downloaded");
786 state(data, SSH_STOP);
787 break;
788 }
789 Curl_xfer_setup1(data, CURL_XFER_RECV, data->req.size, FALSE);
790
791 /* not set by Curl_xfer_setup to preserve keepon bits */
792 conn->writesockfd = conn->sockfd;
793
794 /* we want to use the _receiving_ function even when the socket turns
795 out writableable as the underlying libssh2 recv function will deal
796 with both accordingly */
797 data->state.select_bits = CURL_CSELECT_IN;
798
799 if(result) {
800 /* this should never occur; the close state should be entered
801 at the time the error occurs */
802 state(data, SSH_SFTP_CLOSE);
803 sshc->actualcode = result;
804 }
805 else {
806 state(data, SSH_STOP);
807 }
808 break;
809 }
810 case SSH_SFTP_CLOSE:
811 if(sshc->handleSz)
812 rc = wolfSSH_SFTP_Close(sshc->ssh_session, sshc->handle,
813 sshc->handleSz);
814 else
815 rc = WS_SUCCESS; /* directory listing */
816 if(rc == WS_WANT_READ) {
817 *block = TRUE;
818 conn->waitfor = KEEP_RECV;
819 return CURLE_OK;
820 }
821 else if(rc == WS_WANT_WRITE) {
822 *block = TRUE;
823 conn->waitfor = KEEP_SEND;
824 return CURLE_OK;
825 }
826 else if(rc == WS_SUCCESS) {
827 state(data, SSH_STOP);
828 return CURLE_OK;
829 }
830
831 failf(data, "wolfssh SFTP CLOSE failed: %d", rc);
832 return CURLE_SSH;
833
834 case SSH_SFTP_READDIR_INIT:
835 Curl_pgrsSetDownloadSize(data, -1);
836 if(data->req.no_body) {
837 state(data, SSH_STOP);
838 break;
839 }
840 state(data, SSH_SFTP_READDIR);
841 break;
842
843 case SSH_SFTP_READDIR:
844 name = wolfSSH_SFTP_LS(sshc->ssh_session, sftp_scp->path);
845 if(!name)
846 rc = wolfSSH_get_error(sshc->ssh_session);
847 else
848 rc = WS_SUCCESS;
849
850 if(rc == WS_WANT_READ) {
851 *block = TRUE;
852 conn->waitfor = KEEP_RECV;
853 return CURLE_OK;
854 }
855 else if(rc == WS_WANT_WRITE) {
856 *block = TRUE;
857 conn->waitfor = KEEP_SEND;
858 return CURLE_OK;
859 }
860 else if(name && (rc == WS_SUCCESS)) {
861 WS_SFTPNAME *origname = name;
862 result = CURLE_OK;
863 while(name) {
864 char *line = aprintf("%s\n",
865 data->set.list_only ?
866 name->fName : name->lName);
867 if(!line) {
868 state(data, SSH_SFTP_CLOSE);
869 sshc->actualcode = CURLE_OUT_OF_MEMORY;
870 break;
871 }
872 result = Curl_client_write(data, CLIENTWRITE_BODY,
873 line, strlen(line));
874 free(line);
875 if(result) {
876 sshc->actualcode = result;
877 break;
878 }
879 name = name->next;
880 }
881 wolfSSH_SFTPNAME_list_free(origname);
882 state(data, SSH_STOP);
883 return result;
884 }
885 failf(data, "wolfssh SFTP ls failed: %d", rc);
886 return CURLE_SSH;
887
888 case SSH_SFTP_SHUTDOWN:
889 Curl_safefree(sshc->homedir);
890 wolfSSH_free(sshc->ssh_session);
891 wolfSSH_CTX_free(sshc->ctx);
892 state(data, SSH_STOP);
893 return CURLE_OK;
894 default:
895 break;
896 }
897 } while(!rc && (sshc->state != SSH_STOP));
898 return result;
899 }
900
901 /* called repeatedly until done from multi.c */
wssh_multi_statemach(struct Curl_easy * data,bool * done)902 static CURLcode wssh_multi_statemach(struct Curl_easy *data, bool *done)
903 {
904 struct connectdata *conn = data->conn;
905 struct ssh_conn *sshc = &conn->proto.sshc;
906 CURLcode result = CURLE_OK;
907 bool block; /* we store the status and use that to provide a ssh_getsock()
908 implementation */
909 do {
910 result = wssh_statemach_act(data, &block);
911 *done = (sshc->state == SSH_STOP);
912 /* if there is no error, it is not done and it did not EWOULDBLOCK, then
913 try again */
914 if(*done) {
915 DEBUGF(infof(data, "wssh_statemach_act says DONE"));
916 }
917 } while(!result && !*done && !block);
918
919 return result;
920 }
921
922 static
wscp_perform(struct Curl_easy * data,bool * connected,bool * dophase_done)923 CURLcode wscp_perform(struct Curl_easy *data,
924 bool *connected,
925 bool *dophase_done)
926 {
927 (void)data;
928 (void)connected;
929 (void)dophase_done;
930 return CURLE_OK;
931 }
932
933 static
wsftp_perform(struct Curl_easy * data,bool * connected,bool * dophase_done)934 CURLcode wsftp_perform(struct Curl_easy *data,
935 bool *connected,
936 bool *dophase_done)
937 {
938 CURLcode result = CURLE_OK;
939
940 DEBUGF(infof(data, "DO phase starts"));
941
942 *dophase_done = FALSE; /* not done yet */
943
944 /* start the first command in the DO phase */
945 state(data, SSH_SFTP_QUOTE_INIT);
946
947 /* run the state-machine */
948 result = wssh_multi_statemach(data, dophase_done);
949
950 *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
951
952 if(*dophase_done) {
953 DEBUGF(infof(data, "DO phase is complete"));
954 }
955
956 return result;
957 }
958
959 /*
960 * The DO function is generic for both protocols.
961 */
wssh_do(struct Curl_easy * data,bool * done)962 static CURLcode wssh_do(struct Curl_easy *data, bool *done)
963 {
964 CURLcode result;
965 bool connected = FALSE;
966 struct connectdata *conn = data->conn;
967 struct ssh_conn *sshc = &conn->proto.sshc;
968
969 *done = FALSE; /* default to false */
970 data->req.size = -1; /* make sure this is unknown at this point */
971 sshc->actualcode = CURLE_OK; /* reset error code */
972 sshc->secondCreateDirs = 0; /* reset the create dir attempt state
973 variable */
974
975 Curl_pgrsSetUploadCounter(data, 0);
976 Curl_pgrsSetDownloadCounter(data, 0);
977 Curl_pgrsSetUploadSize(data, -1);
978 Curl_pgrsSetDownloadSize(data, -1);
979
980 if(conn->handler->protocol & CURLPROTO_SCP)
981 result = wscp_perform(data, &connected, done);
982 else
983 result = wsftp_perform(data, &connected, done);
984
985 return result;
986 }
987
wssh_block_statemach(struct Curl_easy * data,bool disconnect)988 static CURLcode wssh_block_statemach(struct Curl_easy *data,
989 bool disconnect)
990 {
991 struct connectdata *conn = data->conn;
992 struct ssh_conn *sshc = &conn->proto.sshc;
993 CURLcode result = CURLE_OK;
994
995 while((sshc->state != SSH_STOP) && !result) {
996 bool block;
997 timediff_t left = 1000;
998 struct curltime now = Curl_now();
999
1000 result = wssh_statemach_act(data, &block);
1001 if(result)
1002 break;
1003
1004 if(!disconnect) {
1005 if(Curl_pgrsUpdate(data))
1006 return CURLE_ABORTED_BY_CALLBACK;
1007
1008 result = Curl_speedcheck(data, now);
1009 if(result)
1010 break;
1011
1012 left = Curl_timeleft(data, NULL, FALSE);
1013 if(left < 0) {
1014 failf(data, "Operation timed out");
1015 return CURLE_OPERATION_TIMEDOUT;
1016 }
1017 }
1018
1019 if(!result) {
1020 int dir = conn->waitfor;
1021 curl_socket_t sock = conn->sock[FIRSTSOCKET];
1022 curl_socket_t fd_read = CURL_SOCKET_BAD;
1023 curl_socket_t fd_write = CURL_SOCKET_BAD;
1024 if(dir == KEEP_RECV)
1025 fd_read = sock;
1026 else if(dir == KEEP_SEND)
1027 fd_write = sock;
1028
1029 /* wait for the socket to become ready */
1030 (void)Curl_socket_check(fd_read, CURL_SOCKET_BAD, fd_write,
1031 left > 1000 ? 1000 : left); /* ignore result */
1032 }
1033 }
1034
1035 return result;
1036 }
1037
1038 /* generic done function for both SCP and SFTP called from their specific
1039 done functions */
wssh_done(struct Curl_easy * data,CURLcode status)1040 static CURLcode wssh_done(struct Curl_easy *data, CURLcode status)
1041 {
1042 CURLcode result = CURLE_OK;
1043 struct SSHPROTO *sftp_scp = data->req.p.ssh;
1044
1045 if(!status) {
1046 /* run the state-machine */
1047 result = wssh_block_statemach(data, FALSE);
1048 }
1049 else
1050 result = status;
1051
1052 if(sftp_scp)
1053 Curl_safefree(sftp_scp->path);
1054 if(Curl_pgrsDone(data))
1055 return CURLE_ABORTED_BY_CALLBACK;
1056
1057 data->req.keepon = 0; /* clear all bits */
1058 return result;
1059 }
1060
1061 #if 0
1062 static CURLcode wscp_done(struct Curl_easy *data,
1063 CURLcode code, bool premature)
1064 {
1065 CURLcode result = CURLE_OK;
1066 (void)conn;
1067 (void)code;
1068 (void)premature;
1069
1070 return result;
1071 }
1072
1073 static CURLcode wscp_doing(struct Curl_easy *data,
1074 bool *dophase_done)
1075 {
1076 CURLcode result = CURLE_OK;
1077 (void)conn;
1078 (void)dophase_done;
1079
1080 return result;
1081 }
1082
1083 static CURLcode wscp_disconnect(struct Curl_easy *data,
1084 struct connectdata *conn, bool dead_connection)
1085 {
1086 CURLcode result = CURLE_OK;
1087 (void)data;
1088 (void)conn;
1089 (void)dead_connection;
1090
1091 return result;
1092 }
1093 #endif
1094
wsftp_done(struct Curl_easy * data,CURLcode code,bool premature)1095 static CURLcode wsftp_done(struct Curl_easy *data,
1096 CURLcode code, bool premature)
1097 {
1098 (void)premature;
1099 state(data, SSH_SFTP_CLOSE);
1100
1101 return wssh_done(data, code);
1102 }
1103
wsftp_doing(struct Curl_easy * data,bool * dophase_done)1104 static CURLcode wsftp_doing(struct Curl_easy *data,
1105 bool *dophase_done)
1106 {
1107 CURLcode result = wssh_multi_statemach(data, dophase_done);
1108
1109 if(*dophase_done) {
1110 DEBUGF(infof(data, "DO phase is complete"));
1111 }
1112 return result;
1113 }
1114
wsftp_disconnect(struct Curl_easy * data,struct connectdata * conn,bool dead)1115 static CURLcode wsftp_disconnect(struct Curl_easy *data,
1116 struct connectdata *conn,
1117 bool dead)
1118 {
1119 CURLcode result = CURLE_OK;
1120 (void)dead;
1121
1122 DEBUGF(infof(data, "SSH DISCONNECT starts now"));
1123
1124 if(conn->proto.sshc.ssh_session) {
1125 /* only if there is a session still around to use! */
1126 state(data, SSH_SFTP_SHUTDOWN);
1127 result = wssh_block_statemach(data, TRUE);
1128 }
1129
1130 DEBUGF(infof(data, "SSH DISCONNECT is done"));
1131 return result;
1132 }
1133
wssh_getsock(struct Curl_easy * data,struct connectdata * conn,curl_socket_t * sock)1134 static int wssh_getsock(struct Curl_easy *data,
1135 struct connectdata *conn,
1136 curl_socket_t *sock)
1137 {
1138 int bitmap = GETSOCK_BLANK;
1139 int dir = conn->waitfor;
1140 (void)data;
1141 sock[0] = conn->sock[FIRSTSOCKET];
1142
1143 if(dir == KEEP_RECV)
1144 bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
1145 else if(dir == KEEP_SEND)
1146 bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
1147
1148 return bitmap;
1149 }
1150
Curl_ssh_version(char * buffer,size_t buflen)1151 void Curl_ssh_version(char *buffer, size_t buflen)
1152 {
1153 (void)msnprintf(buffer, buflen, "wolfssh/%s", LIBWOLFSSH_VERSION_STRING);
1154 }
1155
Curl_ssh_init(void)1156 CURLcode Curl_ssh_init(void)
1157 {
1158 if(WS_SUCCESS != wolfSSH_Init()) {
1159 DEBUGF(fprintf(stderr, "Error: wolfSSH_Init failed\n"));
1160 return CURLE_FAILED_INIT;
1161 }
1162
1163 return CURLE_OK;
1164 }
Curl_ssh_cleanup(void)1165 void Curl_ssh_cleanup(void)
1166 {
1167 (void)wolfSSH_Cleanup();
1168 }
1169
1170 #endif /* USE_WOLFSSH */
1171