2006-11-02 16:56:40 -05:00
|
|
|
/***************************************************************************
|
2007-07-10 18:26:32 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-12-31 04:11:49 -05:00
|
|
|
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2007-07-10 18:26:32 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 08:02:01 -05:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2007-07-10 18:26:32 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2007-01-28 07:35:39 -05:00
|
|
|
/* #define CURL_LIBSSH2_DEBUG */
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2006-11-02 16:56:40 -05:00
|
|
|
|
|
|
|
#ifdef USE_LIBSSH2
|
2011-07-24 23:30:14 -04:00
|
|
|
|
2018-01-03 14:40:52 -05:00
|
|
|
#include <limits.h>
|
2006-11-02 16:56:40 -05:00
|
|
|
|
|
|
|
#include <libssh2.h>
|
|
|
|
#include <libssh2_sftp.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UTSNAME_H
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2009-12-30 12:59:56 -05:00
|
|
|
#ifdef __VMS
|
2006-11-02 16:56:40 -05:00
|
|
|
#include <in.h>
|
|
|
|
#include <inet.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined(NETWARE) && defined(__NOVELL_LIBC__))
|
|
|
|
#undef in_addr_t
|
|
|
|
#define in_addr_t unsigned long
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "hostip.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "transfer.h"
|
|
|
|
#include "escape.h"
|
|
|
|
#include "http.h" /* for HTTP proxy tunnel stuff */
|
|
|
|
#include "ssh.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "speedcheck.h"
|
|
|
|
#include "getinfo.h"
|
2016-11-07 04:55:25 -05:00
|
|
|
#include "strdup.h"
|
2016-09-30 12:54:02 -04:00
|
|
|
#include "strcase.h"
|
2013-12-17 17:32:47 -05:00
|
|
|
#include "vtls/vtls.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "connect.h"
|
|
|
|
#include "strerror.h"
|
|
|
|
#include "inet_ntop.h"
|
|
|
|
#include "parsedate.h" /* for the week day and month names */
|
|
|
|
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
|
|
|
|
#include "strtoofft.h"
|
|
|
|
#include "multiif.h"
|
|
|
|
#include "select.h"
|
|
|
|
#include "warnless.h"
|
2019-03-04 10:17:10 -05:00
|
|
|
#include "curl_path.h"
|
2016-04-29 09:46:40 -04:00
|
|
|
|
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2016-05-30 02:14:13 -04:00
|
|
|
#if LIBSSH2_VERSION_NUM >= 0x010206
|
2016-05-28 18:20:14 -04:00
|
|
|
/* libssh2_sftp_statvfs and friends were added in 1.2.6 */
|
|
|
|
#define HAS_STATVFS_SUPPORT 1
|
|
|
|
#endif
|
|
|
|
|
2020-06-06 18:30:50 -04:00
|
|
|
#define sftp_libssh2_realpath(s,p,t,m) \
|
|
|
|
libssh2_sftp_symlink_ex((s), (p), curlx_uztoui(strlen(p)), \
|
|
|
|
(t), (m), LIBSSH2_SFTP_REALPATH)
|
2012-03-21 21:40:19 -04:00
|
|
|
|
2007-04-02 17:24:05 -04:00
|
|
|
/* Local functions: */
|
2020-06-06 18:30:50 -04:00
|
|
|
static const char *sftp_libssh2_strerror(unsigned long err);
|
2010-10-20 17:29:37 -04:00
|
|
|
static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc);
|
|
|
|
static LIBSSH2_REALLOC_FUNC(my_libssh2_realloc);
|
|
|
|
static LIBSSH2_FREE_FUNC(my_libssh2_free);
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
static CURLcode ssh_force_knownhost_key_type(struct Curl_easy *data);
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode ssh_connect(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode ssh_multi_statemach(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode ssh_do(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode scp_done(struct Curl_easy *data, CURLcode c, bool premature);
|
|
|
|
static CURLcode scp_doing(struct Curl_easy *data, bool *dophase_done);
|
|
|
|
static CURLcode scp_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, bool dead_connection);
|
|
|
|
static CURLcode sftp_done(struct Curl_easy *data, CURLcode, bool premature);
|
|
|
|
static CURLcode sftp_doing(struct Curl_easy *data, bool *dophase_done);
|
|
|
|
static CURLcode sftp_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, bool dead);
|
|
|
|
static CURLcode sftp_perform(struct Curl_easy *data, bool *connected,
|
|
|
|
bool *dophase_done);
|
|
|
|
static int ssh_getsock(struct Curl_easy *data, struct connectdata *conn,
|
|
|
|
curl_socket_t *sock);
|
|
|
|
static CURLcode ssh_setup_connection(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn);
|
2013-08-04 13:34:16 -04:00
|
|
|
|
2007-10-12 09:36:37 -04:00
|
|
|
/*
|
|
|
|
* SCP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_scp = {
|
|
|
|
"SCP", /* scheme */
|
2013-08-04 13:34:16 -04:00
|
|
|
ssh_setup_connection, /* setup_connection */
|
2007-11-08 05:22:49 -05:00
|
|
|
ssh_do, /* do_it */
|
|
|
|
scp_done, /* done */
|
2007-10-17 12:58:32 -04:00
|
|
|
ZERO_NULL, /* do_more */
|
2007-11-08 05:22:49 -05:00
|
|
|
ssh_connect, /* connect_it */
|
|
|
|
ssh_multi_statemach, /* connecting */
|
|
|
|
scp_doing, /* doing */
|
2008-12-19 16:14:52 -05:00
|
|
|
ssh_getsock, /* proto_getsock */
|
|
|
|
ssh_getsock, /* doing_getsock */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2021-01-08 11:58:15 -05:00
|
|
|
ssh_getsock, /* perform_getsock */
|
2007-11-08 05:22:49 -05:00
|
|
|
scp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2007-10-12 09:36:37 -04:00
|
|
|
PORT_SSH, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_SCP, /* protocol */
|
2020-09-21 07:45:24 -04:00
|
|
|
CURLPROTO_SCP, /* family */
|
2011-11-24 17:28:54 -05:00
|
|
|
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
|
|
|
|
| PROTOPT_NOURLQUERY /* flags */
|
2007-10-12 09:36:37 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SFTP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_sftp = {
|
|
|
|
"SFTP", /* scheme */
|
2013-08-04 13:34:16 -04:00
|
|
|
ssh_setup_connection, /* setup_connection */
|
2007-11-08 05:22:49 -05:00
|
|
|
ssh_do, /* do_it */
|
|
|
|
sftp_done, /* done */
|
2007-10-17 12:58:32 -04:00
|
|
|
ZERO_NULL, /* do_more */
|
2007-11-08 05:22:49 -05:00
|
|
|
ssh_connect, /* connect_it */
|
|
|
|
ssh_multi_statemach, /* connecting */
|
|
|
|
sftp_doing, /* doing */
|
2008-12-19 16:14:52 -05:00
|
|
|
ssh_getsock, /* proto_getsock */
|
|
|
|
ssh_getsock, /* doing_getsock */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2021-01-08 11:58:15 -05:00
|
|
|
ssh_getsock, /* perform_getsock */
|
2007-11-08 05:22:49 -05:00
|
|
|
sftp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2007-10-12 09:36:37 -04:00
|
|
|
PORT_SSH, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_SFTP, /* protocol */
|
2020-09-21 07:45:24 -04:00
|
|
|
CURLPROTO_SFTP, /* family */
|
2011-11-24 17:28:54 -05:00
|
|
|
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
|
|
|
|
| PROTOPT_NOURLQUERY /* flags */
|
2007-10-12 09:36:37 -04:00
|
|
|
};
|
|
|
|
|
2006-11-02 16:56:40 -05:00
|
|
|
static void
|
|
|
|
kbd_callback(const char *name, int name_len, const char *instruction,
|
|
|
|
int instruction_len, int num_prompts,
|
|
|
|
const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts,
|
|
|
|
LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses,
|
|
|
|
void **abstract)
|
|
|
|
{
|
2021-03-23 06:17:32 -04:00
|
|
|
struct Curl_easy *data = (struct Curl_easy *)*abstract;
|
2006-11-24 17:14:39 -05:00
|
|
|
|
2006-11-02 16:56:40 -05:00
|
|
|
#ifdef CURL_LIBSSH2_DEBUG
|
|
|
|
fprintf(stderr, "name=%s\n", name);
|
|
|
|
fprintf(stderr, "name_len=%d\n", name_len);
|
|
|
|
fprintf(stderr, "instruction=%s\n", instruction);
|
|
|
|
fprintf(stderr, "instruction_len=%d\n", instruction_len);
|
|
|
|
fprintf(stderr, "num_prompts=%d\n", num_prompts);
|
2007-01-28 17:36:23 -05:00
|
|
|
#else
|
|
|
|
(void)name;
|
|
|
|
(void)name_len;
|
|
|
|
(void)instruction;
|
|
|
|
(void)instruction_len;
|
2006-11-02 16:56:40 -05:00
|
|
|
#endif /* CURL_LIBSSH2_DEBUG */
|
2007-11-05 04:30:45 -05:00
|
|
|
if(num_prompts == 1) {
|
2021-03-23 06:17:32 -04:00
|
|
|
struct connectdata *conn = data->conn;
|
2007-11-08 05:22:49 -05:00
|
|
|
responses[0].text = strdup(conn->passwd);
|
2012-03-20 13:28:24 -04:00
|
|
|
responses[0].length = curlx_uztoui(strlen(conn->passwd));
|
2006-11-02 16:56:40 -05:00
|
|
|
}
|
|
|
|
(void)prompts;
|
|
|
|
} /* kbd_callback */
|
|
|
|
|
2020-06-06 18:30:50 -04:00
|
|
|
static CURLcode sftp_libssh2_error_to_CURLE(unsigned long err)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(err) {
|
2007-08-30 19:03:59 -04:00
|
|
|
case LIBSSH2_FX_OK:
|
|
|
|
return CURLE_OK;
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2007-08-30 19:03:59 -04:00
|
|
|
case LIBSSH2_FX_NO_SUCH_FILE:
|
|
|
|
case LIBSSH2_FX_NO_SUCH_PATH:
|
|
|
|
return CURLE_REMOTE_FILE_NOT_FOUND;
|
|
|
|
|
|
|
|
case LIBSSH2_FX_PERMISSION_DENIED:
|
|
|
|
case LIBSSH2_FX_WRITE_PROTECT:
|
|
|
|
case LIBSSH2_FX_LOCK_CONFlICT:
|
|
|
|
return CURLE_REMOTE_ACCESS_DENIED;
|
|
|
|
|
|
|
|
case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM:
|
|
|
|
case LIBSSH2_FX_QUOTA_EXCEEDED:
|
|
|
|
return CURLE_REMOTE_DISK_FULL;
|
|
|
|
|
|
|
|
case LIBSSH2_FX_FILE_ALREADY_EXISTS:
|
|
|
|
return CURLE_REMOTE_FILE_EXISTS;
|
|
|
|
|
|
|
|
case LIBSSH2_FX_DIR_NOT_EMPTY:
|
|
|
|
return CURLE_QUOTE_ERROR;
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2007-08-30 19:03:59 -04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-04-08 18:44:21 -04:00
|
|
|
|
|
|
|
return CURLE_SSH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode libssh2_session_error_to_CURLE(int err)
|
|
|
|
{
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(err) {
|
2010-01-26 07:25:03 -05:00
|
|
|
/* Ordered by order of appearance in libssh2.h */
|
|
|
|
case LIBSSH2_ERROR_NONE:
|
|
|
|
return CURLE_OK;
|
|
|
|
|
2017-10-26 05:08:43 -04:00
|
|
|
/* This is the error returned by libssh2_scp_recv2
|
|
|
|
* on unknown file */
|
|
|
|
case LIBSSH2_ERROR_SCP_PROTOCOL:
|
|
|
|
return CURLE_REMOTE_FILE_NOT_FOUND;
|
|
|
|
|
2010-01-26 07:25:03 -05:00
|
|
|
case LIBSSH2_ERROR_SOCKET_NONE:
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
|
|
|
|
case LIBSSH2_ERROR_ALLOC:
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
case LIBSSH2_ERROR_SOCKET_SEND:
|
|
|
|
return CURLE_SEND_ERROR;
|
|
|
|
|
|
|
|
case LIBSSH2_ERROR_HOSTKEY_INIT:
|
|
|
|
case LIBSSH2_ERROR_HOSTKEY_SIGN:
|
|
|
|
case LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED:
|
|
|
|
case LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED:
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
|
|
|
|
case LIBSSH2_ERROR_PASSWORD_EXPIRED:
|
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
|
|
|
|
case LIBSSH2_ERROR_SOCKET_TIMEOUT:
|
|
|
|
case LIBSSH2_ERROR_TIMEOUT:
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
|
|
|
|
case LIBSSH2_ERROR_EAGAIN:
|
|
|
|
return CURLE_AGAIN;
|
|
|
|
}
|
2007-08-30 19:03:59 -04:00
|
|
|
|
2006-11-02 16:56:40 -05:00
|
|
|
return CURLE_SSH;
|
|
|
|
}
|
|
|
|
|
2010-10-20 17:29:37 -04:00
|
|
|
static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
2008-02-19 12:25:19 -05:00
|
|
|
(void)abstract; /* arg not used */
|
2006-11-02 16:56:40 -05:00
|
|
|
return malloc(count);
|
|
|
|
}
|
|
|
|
|
2010-10-20 17:29:37 -04:00
|
|
|
static LIBSSH2_REALLOC_FUNC(my_libssh2_realloc)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
2008-02-19 12:25:19 -05:00
|
|
|
(void)abstract; /* arg not used */
|
2006-11-02 16:56:40 -05:00
|
|
|
return realloc(ptr, count);
|
|
|
|
}
|
|
|
|
|
2010-10-20 17:29:37 -04:00
|
|
|
static LIBSSH2_FREE_FUNC(my_libssh2_free)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
2008-02-19 12:25:19 -05:00
|
|
|
(void)abstract; /* arg not used */
|
2012-03-09 11:24:42 -05:00
|
|
|
if(ptr) /* ssh2 agent sometimes call free with null ptr */
|
|
|
|
free(ptr);
|
2006-11-02 16:56:40 -05:00
|
|
|
}
|
|
|
|
|
2007-06-12 08:31:10 -04:00
|
|
|
/*
|
2007-06-13 08:15:23 -04:00
|
|
|
* SSH State machine related code
|
2007-06-12 08:31:10 -04:00
|
|
|
*/
|
|
|
|
/* This is the ONLY way to change SSH state! */
|
2021-01-18 05:56:50 -05:00
|
|
|
static void state(struct Curl_easy *data, sshstate nowstate)
|
2007-06-12 08:31:10 -04:00
|
|
|
{
|
2021-01-18 05:56:50 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2014-03-06 07:39:22 -05:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2009-06-09 22:49:42 -04:00
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
2007-06-12 08:31:10 -04:00
|
|
|
/* for debug purposes */
|
2007-07-20 22:08:17 -04:00
|
|
|
static const char * const names[] = {
|
2007-07-10 18:26:32 -04:00
|
|
|
"SSH_STOP",
|
2011-05-01 17:01:31 -04:00
|
|
|
"SSH_INIT",
|
2007-06-12 08:31:10 -04:00
|
|
|
"SSH_S_STARTUP",
|
2009-07-22 18:49:01 -04:00
|
|
|
"SSH_HOSTKEY",
|
2007-06-12 08:31:10 -04:00
|
|
|
"SSH_AUTHLIST",
|
|
|
|
"SSH_AUTH_PKEY_INIT",
|
|
|
|
"SSH_AUTH_PKEY",
|
|
|
|
"SSH_AUTH_PASS_INIT",
|
|
|
|
"SSH_AUTH_PASS",
|
2012-03-09 11:24:42 -05:00
|
|
|
"SSH_AUTH_AGENT_INIT",
|
|
|
|
"SSH_AUTH_AGENT_LIST",
|
|
|
|
"SSH_AUTH_AGENT",
|
2007-06-12 08:31:10 -04:00
|
|
|
"SSH_AUTH_HOST_INIT",
|
|
|
|
"SSH_AUTH_HOST",
|
|
|
|
"SSH_AUTH_KEY_INIT",
|
|
|
|
"SSH_AUTH_KEY",
|
2018-02-15 07:40:35 -05:00
|
|
|
"SSH_AUTH_GSSAPI",
|
2007-06-12 08:31:10 -04:00
|
|
|
"SSH_AUTH_DONE",
|
|
|
|
"SSH_SFTP_INIT",
|
|
|
|
"SSH_SFTP_REALPATH",
|
2007-07-10 18:26:32 -04:00
|
|
|
"SSH_SFTP_QUOTE_INIT",
|
|
|
|
"SSH_SFTP_POSTQUOTE_INIT",
|
|
|
|
"SSH_SFTP_QUOTE",
|
|
|
|
"SSH_SFTP_NEXT_QUOTE",
|
|
|
|
"SSH_SFTP_QUOTE_STAT",
|
|
|
|
"SSH_SFTP_QUOTE_SETSTAT",
|
|
|
|
"SSH_SFTP_QUOTE_SYMLINK",
|
|
|
|
"SSH_SFTP_QUOTE_MKDIR",
|
|
|
|
"SSH_SFTP_QUOTE_RENAME",
|
|
|
|
"SSH_SFTP_QUOTE_RMDIR",
|
|
|
|
"SSH_SFTP_QUOTE_UNLINK",
|
2016-02-23 17:37:14 -05:00
|
|
|
"SSH_SFTP_QUOTE_STATVFS",
|
2016-02-23 17:18:31 -05:00
|
|
|
"SSH_SFTP_GETINFO",
|
|
|
|
"SSH_SFTP_FILETIME",
|
2007-07-10 18:26:32 -04:00
|
|
|
"SSH_SFTP_TRANS_INIT",
|
|
|
|
"SSH_SFTP_UPLOAD_INIT",
|
|
|
|
"SSH_SFTP_CREATE_DIRS_INIT",
|
|
|
|
"SSH_SFTP_CREATE_DIRS",
|
|
|
|
"SSH_SFTP_CREATE_DIRS_MKDIR",
|
|
|
|
"SSH_SFTP_READDIR_INIT",
|
|
|
|
"SSH_SFTP_READDIR",
|
|
|
|
"SSH_SFTP_READDIR_LINK",
|
|
|
|
"SSH_SFTP_READDIR_BOTTOM",
|
|
|
|
"SSH_SFTP_READDIR_DONE",
|
|
|
|
"SSH_SFTP_DOWNLOAD_INIT",
|
|
|
|
"SSH_SFTP_DOWNLOAD_STAT",
|
|
|
|
"SSH_SFTP_CLOSE",
|
2007-06-12 17:32:45 -04:00
|
|
|
"SSH_SFTP_SHUTDOWN",
|
2007-07-10 18:26:32 -04:00
|
|
|
"SSH_SCP_TRANS_INIT",
|
|
|
|
"SSH_SCP_UPLOAD_INIT",
|
|
|
|
"SSH_SCP_DOWNLOAD_INIT",
|
2018-02-15 07:40:35 -05:00
|
|
|
"SSH_SCP_DOWNLOAD",
|
2007-07-10 18:26:32 -04:00
|
|
|
"SSH_SCP_DONE",
|
|
|
|
"SSH_SCP_SEND_EOF",
|
|
|
|
"SSH_SCP_WAIT_EOF",
|
|
|
|
"SSH_SCP_WAIT_CLOSE",
|
|
|
|
"SSH_SCP_CHANNEL_FREE",
|
2007-11-08 05:22:49 -05:00
|
|
|
"SSH_SESSION_DISCONNECT",
|
2007-06-12 17:32:45 -04:00
|
|
|
"SSH_SESSION_FREE",
|
2007-06-12 08:31:10 -04:00
|
|
|
"QUIT"
|
|
|
|
};
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2018-02-15 07:40:35 -05:00
|
|
|
/* a precaution to make sure the lists are in sync */
|
|
|
|
DEBUGASSERT(sizeof(names)/sizeof(names[0]) == SSH_LAST);
|
|
|
|
|
2007-11-05 04:30:45 -05:00
|
|
|
if(sshc->state != nowstate) {
|
2021-01-18 05:56:50 -05:00
|
|
|
infof(data, "SFTP %p state change from %s to %s\n",
|
2013-07-23 09:59:07 -04:00
|
|
|
(void *)sshc, names[sshc->state], names[nowstate]);
|
2007-06-12 08:31:10 -04:00
|
|
|
}
|
|
|
|
#endif
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2007-09-28 17:45:05 -04:00
|
|
|
sshc->state = nowstate;
|
2007-06-12 08:31:10 -04:00
|
|
|
}
|
|
|
|
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2009-07-22 22:15:00 -04:00
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
2016-06-21 13:31:24 -04:00
|
|
|
static int sshkeycallback(struct Curl_easy *easy,
|
2009-07-22 18:49:01 -04:00
|
|
|
const struct curl_khkey *knownkey, /* known */
|
|
|
|
const struct curl_khkey *foundkey, /* found */
|
|
|
|
enum curl_khmatch match,
|
|
|
|
void *clientp)
|
|
|
|
{
|
|
|
|
(void)easy;
|
|
|
|
(void)knownkey;
|
|
|
|
(void)foundkey;
|
|
|
|
(void)clientp;
|
|
|
|
|
|
|
|
/* we only allow perfect matches, and we reject everything else */
|
|
|
|
return (match != CURLKHMATCH_OK)?CURLKHSTAT_REJECT:CURLKHSTAT_FINE;
|
|
|
|
}
|
2009-07-22 22:15:00 -04:00
|
|
|
#endif
|
2009-07-22 18:49:01 -04:00
|
|
|
|
2008-12-22 08:12:36 -05:00
|
|
|
/*
|
|
|
|
* Earlier libssh2 versions didn't have the ability to seek to 64bit positions
|
|
|
|
* with 32bit size_t.
|
|
|
|
*/
|
2008-12-22 08:21:23 -05:00
|
|
|
#ifdef HAVE_LIBSSH2_SFTP_SEEK64
|
|
|
|
#define SFTP_SEEK(x,y) libssh2_sftp_seek64(x, (libssh2_uint64_t)y)
|
2008-12-22 08:12:36 -05:00
|
|
|
#else
|
2009-09-17 11:06:34 -04:00
|
|
|
#define SFTP_SEEK(x,y) libssh2_sftp_seek(x, (size_t)y)
|
2008-12-22 08:12:36 -05:00
|
|
|
#endif
|
|
|
|
|
2010-08-04 11:23:38 -04:00
|
|
|
/*
|
|
|
|
* Earlier libssh2 versions didn't do SCP properly beyond 32bit sizes on 32bit
|
|
|
|
* architectures so we check of the necessary function is present.
|
|
|
|
*/
|
2010-08-05 10:27:39 -04:00
|
|
|
#ifndef HAVE_LIBSSH2_SCP_SEND64
|
2010-08-04 11:23:38 -04:00
|
|
|
#define SCP_SEND(a,b,c,d) libssh2_scp_send_ex(a, b, (int)(c), (size_t)d, 0, 0)
|
|
|
|
#else
|
|
|
|
#define SCP_SEND(a,b,c,d) libssh2_scp_send64(a, b, (int)(c), \
|
|
|
|
(libssh2_uint64_t)d, 0, 0)
|
|
|
|
#endif
|
|
|
|
|
2010-10-20 17:31:34 -04:00
|
|
|
/*
|
|
|
|
* libssh2 1.2.8 fixed the problem with 32bit ints used for sockets on win64.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_LIBSSH2_SESSION_HANDSHAKE
|
|
|
|
#define libssh2_session_startup(x,y) libssh2_session_handshake(x,y)
|
|
|
|
#endif
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
static CURLcode ssh_knownhost(struct Curl_easy *data)
|
2011-04-20 16:43:41 -04:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
|
|
|
if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
|
|
|
|
/* we're asked to verify the host against a file */
|
2021-01-18 05:56:50 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2011-04-20 16:43:41 -04:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2020-07-15 10:39:40 -04:00
|
|
|
struct libssh2_knownhost *host = NULL;
|
2011-04-20 16:43:41 -04:00
|
|
|
int rc;
|
|
|
|
int keytype;
|
|
|
|
size_t keylen;
|
|
|
|
const char *remotekey = libssh2_session_hostkey(sshc->ssh_session,
|
|
|
|
&keylen, &keytype);
|
2011-05-23 13:37:41 -04:00
|
|
|
int keycheck = LIBSSH2_KNOWNHOST_CHECK_FAILURE;
|
|
|
|
int keybit = 0;
|
2011-04-20 16:43:41 -04:00
|
|
|
|
|
|
|
if(remotekey) {
|
|
|
|
/*
|
|
|
|
* A subject to figure out is what host name we need to pass in here.
|
|
|
|
* What host name does OpenSSH store in its file if an IDN name is
|
|
|
|
* used?
|
|
|
|
*/
|
|
|
|
enum curl_khmatch keymatch;
|
|
|
|
curl_sshkeycallback func =
|
|
|
|
data->set.ssh_keyfunc?data->set.ssh_keyfunc:sshkeycallback;
|
|
|
|
struct curl_khkey knownkey;
|
|
|
|
struct curl_khkey *knownkeyp = NULL;
|
|
|
|
struct curl_khkey foundkey;
|
|
|
|
|
2019-12-13 16:55:18 -05:00
|
|
|
switch(keytype) {
|
|
|
|
case LIBSSH2_HOSTKEY_TYPE_RSA:
|
|
|
|
keybit = LIBSSH2_KNOWNHOST_KEY_SSHRSA;
|
|
|
|
break;
|
|
|
|
case LIBSSH2_HOSTKEY_TYPE_DSS:
|
|
|
|
keybit = LIBSSH2_KNOWNHOST_KEY_SSHDSS;
|
|
|
|
break;
|
|
|
|
#ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_256
|
|
|
|
case LIBSSH2_HOSTKEY_TYPE_ECDSA_256:
|
|
|
|
keybit = LIBSSH2_KNOWNHOST_KEY_ECDSA_256;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_384
|
|
|
|
case LIBSSH2_HOSTKEY_TYPE_ECDSA_384:
|
|
|
|
keybit = LIBSSH2_KNOWNHOST_KEY_ECDSA_384;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_521
|
|
|
|
case LIBSSH2_HOSTKEY_TYPE_ECDSA_521:
|
|
|
|
keybit = LIBSSH2_KNOWNHOST_KEY_ECDSA_521;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSSH2_HOSTKEY_TYPE_ED25519
|
|
|
|
case LIBSSH2_HOSTKEY_TYPE_ED25519:
|
|
|
|
keybit = LIBSSH2_KNOWNHOST_KEY_ED25519;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
infof(data, "unsupported key type, can't check knownhosts!\n");
|
|
|
|
keybit = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!keybit)
|
|
|
|
/* no check means failure! */
|
|
|
|
rc = CURLKHSTAT_REJECT;
|
|
|
|
else {
|
2014-11-09 09:30:06 -05:00
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_CHECKP
|
2019-12-13 16:55:18 -05:00
|
|
|
keycheck = libssh2_knownhost_checkp(sshc->kh,
|
|
|
|
conn->host.name,
|
|
|
|
(conn->remote_port != PORT_SSH)?
|
|
|
|
conn->remote_port:-1,
|
|
|
|
remotekey, keylen,
|
|
|
|
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
|
|
|
|
LIBSSH2_KNOWNHOST_KEYENC_RAW|
|
|
|
|
keybit,
|
|
|
|
&host);
|
2014-11-09 09:30:06 -05:00
|
|
|
#else
|
2019-12-13 16:55:18 -05:00
|
|
|
keycheck = libssh2_knownhost_check(sshc->kh,
|
|
|
|
conn->host.name,
|
|
|
|
remotekey, keylen,
|
|
|
|
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
|
|
|
|
LIBSSH2_KNOWNHOST_KEYENC_RAW|
|
|
|
|
keybit,
|
|
|
|
&host);
|
2014-11-09 09:30:06 -05:00
|
|
|
#endif
|
2011-04-20 16:43:41 -04:00
|
|
|
|
2019-12-13 16:55:18 -05:00
|
|
|
infof(data, "SSH host check: %d, key: %s\n", keycheck,
|
|
|
|
(keycheck <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH)?
|
|
|
|
host->key:"<none>");
|
|
|
|
|
|
|
|
/* setup 'knownkey' */
|
|
|
|
if(keycheck <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH) {
|
|
|
|
knownkey.key = host->key;
|
|
|
|
knownkey.len = 0;
|
|
|
|
knownkey.keytype = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)?
|
|
|
|
CURLKHTYPE_RSA : CURLKHTYPE_DSS;
|
|
|
|
knownkeyp = &knownkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup 'foundkey' */
|
|
|
|
foundkey.key = remotekey;
|
|
|
|
foundkey.len = keylen;
|
|
|
|
foundkey.keytype = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)?
|
2011-04-20 16:43:41 -04:00
|
|
|
CURLKHTYPE_RSA : CURLKHTYPE_DSS;
|
|
|
|
|
2019-12-13 16:55:18 -05:00
|
|
|
/*
|
|
|
|
* if any of the LIBSSH2_KNOWNHOST_CHECK_* defines and the
|
|
|
|
* curl_khmatch enum are ever modified, we need to introduce a
|
|
|
|
* translation table here!
|
|
|
|
*/
|
|
|
|
keymatch = (enum curl_khmatch)keycheck;
|
|
|
|
|
|
|
|
/* Ask the callback how to behave */
|
|
|
|
Curl_set_in_callback(data, true);
|
|
|
|
rc = func(data, knownkeyp, /* from the knownhosts file */
|
|
|
|
&foundkey, /* from the remote host */
|
|
|
|
keymatch, data->set.ssh_keyfunc_userp);
|
|
|
|
Curl_set_in_callback(data, false);
|
|
|
|
}
|
2011-04-20 16:43:41 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* no remotekey means failure! */
|
|
|
|
rc = CURLKHSTAT_REJECT;
|
|
|
|
|
|
|
|
switch(rc) {
|
|
|
|
default: /* unknown return codes will equal reject */
|
2014-10-03 17:30:05 -04:00
|
|
|
/* FALLTHROUGH */
|
2011-04-20 16:43:41 -04:00
|
|
|
case CURLKHSTAT_REJECT:
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_FREE);
|
2014-10-03 17:30:05 -04:00
|
|
|
/* FALLTHROUGH */
|
2011-04-20 16:43:41 -04:00
|
|
|
case CURLKHSTAT_DEFER:
|
|
|
|
/* DEFER means bail out but keep the SSH_HOSTKEY state */
|
|
|
|
result = sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
break;
|
2020-07-15 10:39:40 -04:00
|
|
|
case CURLKHSTAT_FINE_REPLACE:
|
|
|
|
/* remove old host+key that doesn't match */
|
|
|
|
if(host)
|
|
|
|
libssh2_knownhost_del(sshc->kh, host);
|
|
|
|
/*FALLTHROUGH*/
|
2011-04-20 16:43:41 -04:00
|
|
|
case CURLKHSTAT_FINE:
|
2020-07-15 10:39:40 -04:00
|
|
|
/*FALLTHROUGH*/
|
2011-04-20 16:43:41 -04:00
|
|
|
case CURLKHSTAT_FINE_ADD_TO_FILE:
|
|
|
|
/* proceed */
|
|
|
|
if(keycheck != LIBSSH2_KNOWNHOST_CHECK_MATCH) {
|
|
|
|
/* the found host+key didn't match but has been told to be fine
|
|
|
|
anyway so we add it in memory */
|
|
|
|
int addrc = libssh2_knownhost_add(sshc->kh,
|
|
|
|
conn->host.name, NULL,
|
|
|
|
remotekey, keylen,
|
|
|
|
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
|
|
|
|
LIBSSH2_KNOWNHOST_KEYENC_RAW|
|
|
|
|
keybit, NULL);
|
|
|
|
if(addrc)
|
|
|
|
infof(data, "Warning adding the known host %s failed!\n",
|
|
|
|
conn->host.name);
|
2020-07-15 10:39:40 -04:00
|
|
|
else if(rc == CURLKHSTAT_FINE_ADD_TO_FILE ||
|
|
|
|
rc == CURLKHSTAT_FINE_REPLACE) {
|
2011-04-20 16:43:41 -04:00
|
|
|
/* now we write the entire in-memory list of known hosts to the
|
|
|
|
known_hosts file */
|
|
|
|
int wrc =
|
|
|
|
libssh2_knownhost_writefile(sshc->kh,
|
|
|
|
data->set.str[STRING_SSH_KNOWNHOSTS],
|
|
|
|
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
|
|
|
|
if(wrc) {
|
|
|
|
infof(data, "Warning, writing %s failed!\n",
|
|
|
|
data->set.str[STRING_SSH_KNOWNHOSTS]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* HAVE_LIBSSH2_KNOWNHOST_API */
|
2021-01-18 05:56:50 -05:00
|
|
|
(void)data;
|
2011-04-20 16:43:41 -04:00
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
static CURLcode ssh_check_fingerprint(struct Curl_easy *data)
|
2012-09-12 10:06:18 -04:00
|
|
|
{
|
2021-01-18 05:56:50 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2012-09-12 10:06:18 -04:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
const char *pubkey_md5 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5];
|
|
|
|
char md5buffer[33];
|
|
|
|
|
|
|
|
const char *fingerprint = libssh2_hostkey_hash(sshc->ssh_session,
|
|
|
|
LIBSSH2_HOSTKEY_HASH_MD5);
|
|
|
|
|
2012-09-12 10:18:36 -04:00
|
|
|
if(fingerprint) {
|
|
|
|
/* The fingerprint points to static storage (!), don't free() it. */
|
2019-05-11 15:42:48 -04:00
|
|
|
int i;
|
2012-09-12 10:18:36 -04:00
|
|
|
for(i = 0; i < 16; i++)
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(&md5buffer[i*2], 3, "%02x", (unsigned char) fingerprint[i]);
|
2012-09-12 10:18:36 -04:00
|
|
|
infof(data, "SSH MD5 fingerprint: %s\n", md5buffer);
|
|
|
|
}
|
2012-09-12 10:06:18 -04:00
|
|
|
|
|
|
|
/* Before we authenticate we check the hostkey's MD5 fingerprint
|
|
|
|
* against a known fingerprint, if available.
|
|
|
|
*/
|
|
|
|
if(pubkey_md5 && strlen(pubkey_md5) == 32) {
|
2016-11-07 06:54:40 -05:00
|
|
|
if(!fingerprint || !strcasecompare(md5buffer, pubkey_md5)) {
|
2012-09-12 10:18:36 -04:00
|
|
|
if(fingerprint)
|
|
|
|
failf(data,
|
|
|
|
"Denied establishing ssh session: mismatch md5 fingerprint. "
|
|
|
|
"Remote %s is not equal to %s", md5buffer, pubkey_md5);
|
|
|
|
else
|
|
|
|
failf(data,
|
|
|
|
"Denied establishing ssh session: md5 fingerprint not available");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_FREE);
|
2012-09-12 10:06:18 -04:00
|
|
|
sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
return sshc->actualcode;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
infof(data, "MD5 checksum match!\n");
|
|
|
|
/* as we already matched, we skip the check for known hosts */
|
|
|
|
return CURLE_OK;
|
2012-09-12 10:06:18 -04:00
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
return ssh_knownhost(data);
|
2012-09-12 10:06:18 -04:00
|
|
|
}
|
2011-04-20 16:43:41 -04:00
|
|
|
|
2019-12-20 07:37:20 -05:00
|
|
|
/*
|
|
|
|
* ssh_force_knownhost_key_type() will check the known hosts file and try to
|
|
|
|
* force a specific public key type from the server if an entry is found.
|
|
|
|
*/
|
2021-01-18 05:56:50 -05:00
|
|
|
static CURLcode ssh_force_knownhost_key_type(struct Curl_easy *data)
|
2019-12-20 07:37:20 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
|
|
|
|
|
|
|
#ifdef LIBSSH2_KNOWNHOST_KEY_ED25519
|
|
|
|
static const char * const hostkey_method_ssh_ed25519
|
|
|
|
= "ssh-ed25519";
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_521
|
|
|
|
static const char * const hostkey_method_ssh_ecdsa_521
|
|
|
|
= "ecdsa-sha2-nistp521";
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_384
|
|
|
|
static const char * const hostkey_method_ssh_ecdsa_384
|
|
|
|
= "ecdsa-sha2-nistp384";
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_256
|
|
|
|
static const char * const hostkey_method_ssh_ecdsa_256
|
|
|
|
= "ecdsa-sha2-nistp256";
|
|
|
|
#endif
|
|
|
|
static const char * const hostkey_method_ssh_rsa
|
|
|
|
= "ssh-rsa";
|
|
|
|
static const char * const hostkey_method_ssh_dss
|
|
|
|
= "ssh-dss";
|
|
|
|
|
|
|
|
const char *hostkey_method = NULL;
|
2021-01-18 05:56:50 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2019-12-20 07:37:20 -05:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
struct libssh2_knownhost* store = NULL;
|
|
|
|
const char *kh_name_end = NULL;
|
2020-01-15 14:28:01 -05:00
|
|
|
size_t kh_name_size = 0;
|
2019-12-20 07:37:20 -05:00
|
|
|
int port = 0;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
if(sshc->kh && !data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5]) {
|
|
|
|
/* lets try to find our host in the known hosts file */
|
|
|
|
while(!libssh2_knownhost_get(sshc->kh, &store, store)) {
|
|
|
|
/* For non-standard ports, the name will be enclosed in */
|
|
|
|
/* square brackets, followed by a colon and the port */
|
2020-03-08 13:35:21 -04:00
|
|
|
if(store) {
|
|
|
|
if(store->name) {
|
|
|
|
if(store->name[0] == '[') {
|
|
|
|
kh_name_end = strstr(store->name, "]:");
|
|
|
|
if(!kh_name_end) {
|
|
|
|
infof(data, "Invalid host pattern %s in %s\n",
|
|
|
|
store->name, data->set.str[STRING_SSH_KNOWNHOSTS]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
port = atoi(kh_name_end + 2);
|
|
|
|
if(kh_name_end && (port == conn->remote_port)) {
|
|
|
|
kh_name_size = strlen(store->name) - 1 - strlen(kh_name_end);
|
|
|
|
if(strncmp(store->name + 1,
|
|
|
|
conn->host.name, kh_name_size) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(strcmp(store->name, conn->host.name) == 0) {
|
2019-12-20 07:37:20 -05:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 13:35:21 -04:00
|
|
|
else {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-12-20 07:37:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(found) {
|
|
|
|
infof(data, "Found host %s in %s\n",
|
2020-03-08 13:35:21 -04:00
|
|
|
conn->host.name, data->set.str[STRING_SSH_KNOWNHOSTS]);
|
2019-12-20 07:37:20 -05:00
|
|
|
|
|
|
|
switch(store->typemask & LIBSSH2_KNOWNHOST_KEY_MASK) {
|
|
|
|
#ifdef LIBSSH2_KNOWNHOST_KEY_ED25519
|
|
|
|
case LIBSSH2_KNOWNHOST_KEY_ED25519:
|
|
|
|
hostkey_method = hostkey_method_ssh_ed25519;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_521
|
|
|
|
case LIBSSH2_KNOWNHOST_KEY_ECDSA_521:
|
|
|
|
hostkey_method = hostkey_method_ssh_ecdsa_521;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_384
|
|
|
|
case LIBSSH2_KNOWNHOST_KEY_ECDSA_384:
|
|
|
|
hostkey_method = hostkey_method_ssh_ecdsa_384;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_256
|
|
|
|
case LIBSSH2_KNOWNHOST_KEY_ECDSA_256:
|
|
|
|
hostkey_method = hostkey_method_ssh_ecdsa_256;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case LIBSSH2_KNOWNHOST_KEY_SSHRSA:
|
|
|
|
hostkey_method = hostkey_method_ssh_rsa;
|
|
|
|
break;
|
|
|
|
case LIBSSH2_KNOWNHOST_KEY_SSHDSS:
|
|
|
|
hostkey_method = hostkey_method_ssh_dss;
|
|
|
|
break;
|
|
|
|
case LIBSSH2_KNOWNHOST_KEY_RSA1:
|
2020-12-23 17:41:13 -05:00
|
|
|
failf(data, "Found host key type RSA1 which is not supported");
|
2019-12-20 07:37:20 -05:00
|
|
|
return CURLE_SSH;
|
|
|
|
default:
|
2020-12-23 17:41:13 -05:00
|
|
|
failf(data, "Unknown host key type: %i",
|
2019-12-20 07:37:20 -05:00
|
|
|
(store->typemask & LIBSSH2_KNOWNHOST_KEY_MASK));
|
|
|
|
return CURLE_SSH;
|
|
|
|
}
|
|
|
|
|
|
|
|
infof(data, "Set \"%s\" as SSH hostkey type\n", hostkey_method);
|
|
|
|
result = libssh2_session_error_to_CURLE(
|
2021-01-25 04:44:30 -05:00
|
|
|
libssh2_session_method_pref(
|
|
|
|
sshc->ssh_session, LIBSSH2_METHOD_HOSTKEY, hostkey_method));
|
2019-12-20 07:37:20 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
infof(data, "Did not find host %s in %s\n",
|
|
|
|
conn->host.name, data->set.str[STRING_SSH_KNOWNHOSTS]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_LIBSSH2_KNOWNHOST_API */
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-11-24 08:59:51 -05:00
|
|
|
/*
|
2010-01-11 18:10:53 -05:00
|
|
|
* ssh_statemach_act() runs the SSH state machine as far as it can without
|
|
|
|
* blocking and without reaching the end. The data the pointer 'block' points
|
|
|
|
* to will be set to TRUE if the libssh2 function returns LIBSSH2_ERROR_EAGAIN
|
|
|
|
* meaning it wants to be called again when the socket is ready
|
2008-11-24 08:59:51 -05:00
|
|
|
*/
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode ssh_statemach_act(struct Curl_easy *data, bool *block)
|
2007-06-12 08:31:10 -04:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2021-01-25 04:44:30 -05:00
|
|
|
struct SSHPROTO *sshp = data->req.p.ssh;
|
2007-06-12 08:31:10 -04:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
curl_socket_t sock = conn->sock[FIRSTSOCKET];
|
2012-09-12 10:06:18 -04:00
|
|
|
int rc = LIBSSH2_ERROR_NONE;
|
2020-06-06 18:30:50 -04:00
|
|
|
int ssherr;
|
|
|
|
unsigned long sftperr;
|
2009-04-28 07:19:10 -04:00
|
|
|
int seekerr = CURL_SEEKFUNC_OK;
|
2020-05-04 08:44:07 -04:00
|
|
|
size_t readdir_len;
|
2008-11-24 08:59:51 -05:00
|
|
|
*block = 0; /* we're not blocking by default */
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
do {
|
|
|
|
switch(sshc->state) {
|
2011-05-01 17:01:31 -04:00
|
|
|
case SSH_INIT:
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->secondCreateDirs = 0;
|
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
sshc->actualcode = CURLE_OK;
|
|
|
|
|
2011-05-01 17:01:31 -04:00
|
|
|
/* Set libssh2 to non-blocking, since everything internally is
|
|
|
|
non-blocking */
|
|
|
|
libssh2_session_set_blocking(sshc->ssh_session, 0);
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
result = ssh_force_knownhost_key_type(data);
|
2019-12-20 07:37:20 -05:00
|
|
|
if(result) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_FREE);
|
2020-09-03 09:52:54 -04:00
|
|
|
sshc->actualcode = result;
|
2019-12-20 07:37:20 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_S_STARTUP);
|
2018-07-09 12:52:05 -04:00
|
|
|
/* FALLTHROUGH */
|
2011-05-01 17:01:31 -04:00
|
|
|
|
|
|
|
case SSH_S_STARTUP:
|
2012-01-13 06:57:09 -05:00
|
|
|
rc = libssh2_session_startup(sshc->ssh_session, (int)sock);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc) {
|
2019-01-16 20:18:20 -05:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session, &err_msg, NULL, 0);
|
|
|
|
failf(data, "Failure establishing ssh session: %d, %s", rc, err_msg);
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_FREE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_FAILED_INIT;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_HOSTKEY);
|
2009-07-22 18:49:01 -04:00
|
|
|
|
2018-07-09 12:52:05 -04:00
|
|
|
/* FALLTHROUGH */
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_HOSTKEY:
|
|
|
|
/*
|
|
|
|
* Before we authenticate we should check the hostkey's fingerprint
|
|
|
|
* against our known hosts. How that is handled (reading from file,
|
2011-12-05 16:20:48 -05:00
|
|
|
* whatever) is up to us.
|
2010-01-11 18:10:53 -05:00
|
|
|
*/
|
2021-01-18 05:56:50 -05:00
|
|
|
result = ssh_check_fingerprint(data);
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!result)
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTHLIST);
|
2014-03-12 22:02:38 -04:00
|
|
|
/* ssh_check_fingerprint sets state appropriately on error */
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTHLIST:
|
|
|
|
/*
|
|
|
|
* Figure out authentication methods
|
|
|
|
* NB: As soon as we have provided a username to an openssh server we
|
|
|
|
* must never change it later. Thus, always specify the correct username
|
|
|
|
* here, even though the libssh2 docs kind of indicate that it should be
|
|
|
|
* possible to get a 'generic' list (not user-specific) of authentication
|
|
|
|
* methods, presumably with a blank username. That won't work in my
|
|
|
|
* experience.
|
|
|
|
* So always specify it here.
|
|
|
|
*/
|
|
|
|
sshc->authlist = libssh2_userauth_list(sshc->ssh_session,
|
|
|
|
conn->user,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(conn->user)));
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
if(!sshc->authlist) {
|
2013-10-14 16:24:17 -04:00
|
|
|
if(libssh2_userauth_authenticated(sshc->ssh_session)) {
|
|
|
|
sshc->authed = TRUE;
|
|
|
|
infof(data, "SSH user accepted with no authentication\n");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_DONE);
|
2013-10-14 16:24:17 -04:00
|
|
|
break;
|
|
|
|
}
|
2020-06-06 18:30:50 -04:00
|
|
|
ssherr = libssh2_session_last_errno(sshc->ssh_session);
|
|
|
|
if(ssherr == LIBSSH2_ERROR_EAGAIN)
|
2017-03-10 08:28:37 -05:00
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
2010-01-11 18:10:53 -05:00
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_FREE);
|
2020-06-06 18:30:50 -04:00
|
|
|
sshc->actualcode = libssh2_session_error_to_CURLE(ssherr);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
break;
|
2007-06-12 08:31:10 -04:00
|
|
|
}
|
2010-02-20 16:41:25 -05:00
|
|
|
infof(data, "SSH authentication methods available: %s\n",
|
|
|
|
sshc->authlist);
|
2007-06-12 08:31:10 -04:00
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_PKEY_INIT);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTH_PKEY_INIT:
|
|
|
|
/*
|
|
|
|
* Check the supported auth types in the order I feel is most secure
|
|
|
|
* with the requested type of authentication
|
|
|
|
*/
|
|
|
|
sshc->authed = FALSE;
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if((data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) &&
|
|
|
|
(strstr(sshc->authlist, "publickey") != NULL)) {
|
ssh: improve key file search
For private keys, use the first match from: user-specified key file
(if provided), ~/.ssh/id_rsa, ~/.ssh/id_dsa, ./id_rsa, ./id_dsa
Note that the previous code only looked for id_dsa files. id_rsa is
now generally preferred, as it supports larger key sizes.
For public keys, use the user-specified key file, if provided.
Otherwise, try to extract the public key from the private key file.
This means that passing --pubkey is typically no longer required,
and makes the key-handling behavior more like OpenSSH.
2014-09-16 00:16:46 -04:00
|
|
|
bool out_of_memory = FALSE;
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->rsa_pub = sshc->rsa = NULL;
|
2007-06-13 08:15:23 -04:00
|
|
|
|
ssh: improve key file search
For private keys, use the first match from: user-specified key file
(if provided), ~/.ssh/id_rsa, ~/.ssh/id_dsa, ./id_rsa, ./id_dsa
Note that the previous code only looked for id_dsa files. id_rsa is
now generally preferred, as it supports larger key sizes.
For public keys, use the user-specified key file, if provided.
Otherwise, try to extract the public key from the private key file.
This means that passing --pubkey is typically no longer required,
and makes the key-handling behavior more like OpenSSH.
2014-09-16 00:16:46 -04:00
|
|
|
if(data->set.str[STRING_SSH_PRIVATE_KEY])
|
|
|
|
sshc->rsa = strdup(data->set.str[STRING_SSH_PRIVATE_KEY]);
|
|
|
|
else {
|
2019-05-12 08:30:03 -04:00
|
|
|
/* To ponder about: should really the lib be messing about with the
|
|
|
|
HOME environment variable etc? */
|
|
|
|
char *home = curl_getenv("HOME");
|
|
|
|
|
ssh: improve key file search
For private keys, use the first match from: user-specified key file
(if provided), ~/.ssh/id_rsa, ~/.ssh/id_dsa, ./id_rsa, ./id_dsa
Note that the previous code only looked for id_dsa files. id_rsa is
now generally preferred, as it supports larger key sizes.
For public keys, use the user-specified key file, if provided.
Otherwise, try to extract the public key from the private key file.
This means that passing --pubkey is typically no longer required,
and makes the key-handling behavior more like OpenSSH.
2014-09-16 00:16:46 -04:00
|
|
|
/* If no private key file is specified, try some common paths. */
|
|
|
|
if(home) {
|
|
|
|
/* Try ~/.ssh first. */
|
|
|
|
sshc->rsa = aprintf("%s/.ssh/id_rsa", home);
|
|
|
|
if(!sshc->rsa)
|
|
|
|
out_of_memory = TRUE;
|
|
|
|
else if(access(sshc->rsa, R_OK) != 0) {
|
|
|
|
Curl_safefree(sshc->rsa);
|
|
|
|
sshc->rsa = aprintf("%s/.ssh/id_dsa", home);
|
|
|
|
if(!sshc->rsa)
|
|
|
|
out_of_memory = TRUE;
|
|
|
|
else if(access(sshc->rsa, R_OK) != 0) {
|
|
|
|
Curl_safefree(sshc->rsa);
|
|
|
|
}
|
|
|
|
}
|
2019-05-12 08:30:03 -04:00
|
|
|
free(home);
|
ssh: improve key file search
For private keys, use the first match from: user-specified key file
(if provided), ~/.ssh/id_rsa, ~/.ssh/id_dsa, ./id_rsa, ./id_dsa
Note that the previous code only looked for id_dsa files. id_rsa is
now generally preferred, as it supports larger key sizes.
For public keys, use the user-specified key file, if provided.
Otherwise, try to extract the public key from the private key file.
This means that passing --pubkey is typically no longer required,
and makes the key-handling behavior more like OpenSSH.
2014-09-16 00:16:46 -04:00
|
|
|
}
|
|
|
|
if(!out_of_memory && !sshc->rsa) {
|
|
|
|
/* Nothing found; try the current dir. */
|
|
|
|
sshc->rsa = strdup("id_rsa");
|
|
|
|
if(sshc->rsa && access(sshc->rsa, R_OK) != 0) {
|
|
|
|
Curl_safefree(sshc->rsa);
|
|
|
|
sshc->rsa = strdup("id_dsa");
|
|
|
|
if(sshc->rsa && access(sshc->rsa, R_OK) != 0) {
|
|
|
|
Curl_safefree(sshc->rsa);
|
|
|
|
/* Out of guesses. Set to the empty string to avoid
|
|
|
|
* surprising info messages. */
|
|
|
|
sshc->rsa = strdup("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-06-19 07:31:33 -04:00
|
|
|
|
ssh: improve key file search
For private keys, use the first match from: user-specified key file
(if provided), ~/.ssh/id_rsa, ~/.ssh/id_dsa, ./id_rsa, ./id_dsa
Note that the previous code only looked for id_dsa files. id_rsa is
now generally preferred, as it supports larger key sizes.
For public keys, use the user-specified key file, if provided.
Otherwise, try to extract the public key from the private key file.
This means that passing --pubkey is typically no longer required,
and makes the key-handling behavior more like OpenSSH.
2014-09-16 00:16:46 -04:00
|
|
|
/*
|
|
|
|
* Unless the user explicitly specifies a public key file, let
|
|
|
|
* libssh2 extract the public key from the private key file.
|
|
|
|
* This is done by simply passing sshc->rsa_pub = NULL.
|
|
|
|
*/
|
2016-01-15 04:27:33 -05:00
|
|
|
if(data->set.str[STRING_SSH_PUBLIC_KEY]
|
2018-04-15 04:49:30 -04:00
|
|
|
/* treat empty string the same way as NULL */
|
|
|
|
&& data->set.str[STRING_SSH_PUBLIC_KEY][0]) {
|
ssh: improve key file search
For private keys, use the first match from: user-specified key file
(if provided), ~/.ssh/id_rsa, ~/.ssh/id_dsa, ./id_rsa, ./id_dsa
Note that the previous code only looked for id_dsa files. id_rsa is
now generally preferred, as it supports larger key sizes.
For public keys, use the user-specified key file, if provided.
Otherwise, try to extract the public key from the private key file.
This means that passing --pubkey is typically no longer required,
and makes the key-handling behavior more like OpenSSH.
2014-09-16 00:16:46 -04:00
|
|
|
sshc->rsa_pub = strdup(data->set.str[STRING_SSH_PUBLIC_KEY]);
|
|
|
|
if(!sshc->rsa_pub)
|
|
|
|
out_of_memory = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(out_of_memory || sshc->rsa == NULL) {
|
|
|
|
Curl_safefree(sshc->rsa);
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->rsa_pub);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_FREE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
sshc->passphrase = data->set.ssl.key_passwd;
|
2010-01-11 18:10:53 -05:00
|
|
|
if(!sshc->passphrase)
|
|
|
|
sshc->passphrase = "";
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2016-01-15 04:27:33 -05:00
|
|
|
if(sshc->rsa_pub)
|
|
|
|
infof(data, "Using SSH public key file '%s'\n", sshc->rsa_pub);
|
ssh: improve key file search
For private keys, use the first match from: user-specified key file
(if provided), ~/.ssh/id_rsa, ~/.ssh/id_dsa, ./id_rsa, ./id_dsa
Note that the previous code only looked for id_dsa files. id_rsa is
now generally preferred, as it supports larger key sizes.
For public keys, use the user-specified key file, if provided.
Otherwise, try to extract the public key from the private key file.
This means that passing --pubkey is typically no longer required,
and makes the key-handling behavior more like OpenSSH.
2014-09-16 00:16:46 -04:00
|
|
|
infof(data, "Using SSH private key file '%s'\n", sshc->rsa);
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_PKEY);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_PASS_INIT);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-06-12 08:31:10 -04:00
|
|
|
break;
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTH_PKEY:
|
|
|
|
/* The function below checks if the files exists, no need to stat() here.
|
|
|
|
*/
|
2010-03-28 02:45:17 -04:00
|
|
|
rc = libssh2_userauth_publickey_fromfile_ex(sshc->ssh_session,
|
|
|
|
conn->user,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(
|
|
|
|
strlen(conn->user)),
|
2010-03-28 02:45:17 -04:00
|
|
|
sshc->rsa_pub,
|
|
|
|
sshc->rsa, sshc->passphrase);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->rsa_pub);
|
|
|
|
Curl_safefree(sshc->rsa);
|
2007-06-12 08:31:10 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == 0) {
|
|
|
|
sshc->authed = TRUE;
|
|
|
|
infof(data, "Initialized SSH public key authentication\n");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_DONE);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
2010-01-11 18:10:53 -05:00
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "SSH public key authentication failed: %s\n", err_msg);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_PASS_INIT);
|
2017-02-19 05:02:15 -05:00
|
|
|
rc = 0; /* clear rc and continue */
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-06-12 08:31:10 -04:00
|
|
|
break;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTH_PASS_INIT:
|
|
|
|
if((data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD) &&
|
|
|
|
(strstr(sshc->authlist, "password") != NULL)) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_PASS);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_HOST_INIT);
|
2017-02-19 05:02:15 -05:00
|
|
|
rc = 0; /* clear rc and continue */
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-06-12 08:31:10 -04:00
|
|
|
break;
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTH_PASS:
|
2010-03-28 02:45:17 -04:00
|
|
|
rc = libssh2_userauth_password_ex(sshc->ssh_session, conn->user,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(conn->user)),
|
2010-03-28 02:45:17 -04:00
|
|
|
conn->passwd,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(conn->passwd)),
|
2010-03-28 02:45:17 -04:00
|
|
|
NULL);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc == 0) {
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->authed = TRUE;
|
|
|
|
infof(data, "Initialized password authentication\n");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_DONE);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_HOST_INIT);
|
2015-08-02 16:50:31 -04:00
|
|
|
rc = 0; /* clear rc and continue */
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTH_HOST_INIT:
|
|
|
|
if((data->set.ssh_auth_types & CURLSSH_AUTH_HOST) &&
|
|
|
|
(strstr(sshc->authlist, "hostbased") != NULL)) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_HOST);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_AGENT_INIT);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
break;
|
2008-07-03 02:56:03 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTH_HOST:
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_AGENT_INIT);
|
2012-03-09 11:24:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_AUTH_AGENT_INIT:
|
|
|
|
#ifdef HAVE_LIBSSH2_AGENT_API
|
|
|
|
if((data->set.ssh_auth_types & CURLSSH_AUTH_AGENT)
|
|
|
|
&& (strstr(sshc->authlist, "publickey") != NULL)) {
|
|
|
|
|
|
|
|
/* Connect to the ssh-agent */
|
|
|
|
/* The agent could be shared by a curl thread i believe
|
|
|
|
but nothing obvious as keys can be added/removed at any time */
|
|
|
|
if(!sshc->ssh_agent) {
|
|
|
|
sshc->ssh_agent = libssh2_agent_init(sshc->ssh_session);
|
|
|
|
if(!sshc->ssh_agent) {
|
|
|
|
infof(data, "Could not create agent object\n");
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_KEY_INIT);
|
2014-02-06 17:53:48 -05:00
|
|
|
break;
|
2012-03-09 11:24:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = libssh2_agent_connect(sshc->ssh_agent);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN)
|
|
|
|
break;
|
|
|
|
if(rc < 0) {
|
|
|
|
infof(data, "Failure connecting to agent\n");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_KEY_INIT);
|
2017-02-19 05:02:15 -05:00
|
|
|
rc = 0; /* clear rc and continue */
|
2012-03-09 11:24:42 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_AGENT_LIST);
|
2012-03-09 11:24:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* HAVE_LIBSSH2_AGENT_API */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_KEY_INIT);
|
2012-03-09 11:24:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_AUTH_AGENT_LIST:
|
2012-08-10 02:57:27 -04:00
|
|
|
#ifdef HAVE_LIBSSH2_AGENT_API
|
2012-03-09 11:24:42 -05:00
|
|
|
rc = libssh2_agent_list_identities(sshc->ssh_agent);
|
|
|
|
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN)
|
|
|
|
break;
|
|
|
|
if(rc < 0) {
|
|
|
|
infof(data, "Failure requesting identities to agent\n");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_KEY_INIT);
|
2017-02-19 05:02:15 -05:00
|
|
|
rc = 0; /* clear rc and continue */
|
2012-03-09 11:24:42 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_AGENT);
|
2012-03-09 11:24:42 -05:00
|
|
|
sshc->sshagent_prev_identity = NULL;
|
|
|
|
}
|
2012-08-10 02:57:27 -04:00
|
|
|
#endif
|
2012-03-09 11:24:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_AUTH_AGENT:
|
2012-08-10 02:57:27 -04:00
|
|
|
#ifdef HAVE_LIBSSH2_AGENT_API
|
2012-03-09 11:24:42 -05:00
|
|
|
/* as prev_identity evolves only after an identity user auth finished we
|
|
|
|
can safely request it again as long as EAGAIN is returned here or by
|
|
|
|
libssh2_agent_userauth */
|
|
|
|
rc = libssh2_agent_get_identity(sshc->ssh_agent,
|
|
|
|
&sshc->sshagent_identity,
|
|
|
|
sshc->sshagent_prev_identity);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(rc == 0) {
|
|
|
|
rc = libssh2_agent_userauth(sshc->ssh_agent, conn->user,
|
|
|
|
sshc->sshagent_identity);
|
|
|
|
|
|
|
|
if(rc < 0) {
|
2018-01-17 19:28:19 -05:00
|
|
|
if(rc != LIBSSH2_ERROR_EAGAIN) {
|
2012-03-09 11:24:42 -05:00
|
|
|
/* tried and failed? go to next identity */
|
|
|
|
sshc->sshagent_prev_identity = sshc->sshagent_identity;
|
2018-01-17 19:28:19 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-03-09 11:24:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rc < 0)
|
|
|
|
infof(data, "Failure requesting identities to agent\n");
|
|
|
|
else if(rc == 1)
|
|
|
|
infof(data, "No identity would match\n");
|
|
|
|
|
|
|
|
if(rc == LIBSSH2_ERROR_NONE) {
|
|
|
|
sshc->authed = TRUE;
|
|
|
|
infof(data, "Agent based authentication successful\n");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_DONE);
|
2012-03-09 11:24:42 -05:00
|
|
|
}
|
2015-08-02 16:50:31 -04:00
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_KEY_INIT);
|
2015-08-02 16:50:31 -04:00
|
|
|
rc = 0; /* clear rc and continue */
|
|
|
|
}
|
2012-08-10 02:57:27 -04:00
|
|
|
#endif
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-06-12 12:15:20 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTH_KEY_INIT:
|
|
|
|
if((data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD)
|
|
|
|
&& (strstr(sshc->authlist, "keyboard-interactive") != NULL)) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_KEY);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_DONE);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-06-12 08:31:10 -04:00
|
|
|
break;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTH_KEY:
|
|
|
|
/* Authentication failed. Continue with keyboard-interactive now. */
|
|
|
|
rc = libssh2_userauth_keyboard_interactive_ex(sshc->ssh_session,
|
|
|
|
conn->user,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(
|
|
|
|
strlen(conn->user)),
|
2010-01-11 18:10:53 -05:00
|
|
|
&kbd_callback);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-11-05 04:30:45 -05:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc == 0) {
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->authed = TRUE;
|
|
|
|
infof(data, "Initialized keyboard interactive authentication\n");
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_AUTH_DONE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2008-01-22 12:26:42 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_AUTH_DONE:
|
|
|
|
if(!sshc->authed) {
|
|
|
|
failf(data, "Authentication failure");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_FREE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_LOGIN_DENIED;
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/*
|
|
|
|
* At this point we have an authenticated ssh session.
|
|
|
|
*/
|
|
|
|
infof(data, "Authentication complete\n");
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
Curl_pgrsTime(data, TIMER_APPCONNECT); /* SSH is connected */
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
conn->sockfd = sock;
|
|
|
|
conn->writesockfd = CURL_SOCKET_BAD;
|
|
|
|
|
2011-03-14 17:52:14 -04:00
|
|
|
if(conn->handler->protocol == CURLPROTO_SFTP) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_INIT);
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
infof(data, "SSH CONNECT phase done\n");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_INIT:
|
|
|
|
/*
|
|
|
|
* Start the libssh2 sftp session
|
|
|
|
*/
|
|
|
|
sshc->sftp_session = libssh2_sftp_init(sshc->ssh_session);
|
|
|
|
if(!sshc->sftp_session) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
2010-01-11 18:10:53 -05:00
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2017-03-10 08:28:37 -05:00
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
failf(data, "Failure initializing sftp session: %s", err_msg);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_FREE);
|
2017-03-10 08:28:37 -05:00
|
|
|
sshc->actualcode = CURLE_FAILED_INIT;
|
|
|
|
break;
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_REALPATH);
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_REALPATH:
|
|
|
|
{
|
|
|
|
char tempHome[PATH_MAX];
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2007-11-08 05:22:49 -05:00
|
|
|
/*
|
2010-01-11 18:10:53 -05:00
|
|
|
* Get the "home" directory
|
2007-11-08 05:22:49 -05:00
|
|
|
*/
|
2012-03-21 21:40:19 -04:00
|
|
|
rc = sftp_libssh2_realpath(sshc->sftp_session, ".",
|
2010-01-11 18:10:53 -05:00
|
|
|
tempHome, PATH_MAX-1);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc > 0) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* It seems that this string is not always NULL terminated */
|
|
|
|
tempHome[rc] = '\0';
|
|
|
|
sshc->homedir = strdup(tempHome);
|
|
|
|
if(!sshc->homedir) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-08 11:58:15 -05:00
|
|
|
data->state.most_recent_ftp_entrypath = sshc->homedir;
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Return the error type */
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
|
|
|
if(sftperr)
|
|
|
|
result = sftp_libssh2_error_to_CURLE(sftperr);
|
2016-06-28 09:28:04 -04:00
|
|
|
else
|
|
|
|
/* in this case, the error wasn't in the SFTP level but for example
|
|
|
|
a time-out or similar */
|
|
|
|
result = CURLE_SSH;
|
|
|
|
sshc->actualcode = result;
|
2020-11-23 16:42:07 -05:00
|
|
|
DEBUGF(infof(data, "error = %lu makes libcurl = %d\n",
|
2020-08-10 06:59:16 -04:00
|
|
|
sftperr, (int)result));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2007-06-12 17:32:45 -04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
/* This is the last step in the SFTP connect phase. Do note that while
|
|
|
|
we get the homedir here, we get the "workingpath" in the DO action
|
|
|
|
since the homedir will remain the same between request but the
|
|
|
|
working path will not. */
|
|
|
|
DEBUGF(infof(data, "SSH CONNECT phase done\n"));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_QUOTE_INIT:
|
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
result = Curl_getworkingpath(data, sshc->homedir, &sshp->path);
|
2008-02-18 08:05:46 -05:00
|
|
|
if(result) {
|
|
|
|
sshc->actualcode = result;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(data->set.quote) {
|
|
|
|
infof(data, "Sending quote commands\n");
|
|
|
|
sshc->quote_item = data->set.quote;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_GETINFO);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_POSTQUOTE_INIT:
|
|
|
|
if(data->set.postquote) {
|
|
|
|
infof(data, "Sending quote commands\n");
|
|
|
|
sshc->quote_item = data->set.postquote;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_QUOTE:
|
|
|
|
/* Send any quote commands */
|
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
|
2007-11-08 05:22:49 -05:00
|
|
|
/*
|
2010-01-11 18:10:53 -05:00
|
|
|
* Support some of the "FTP" commands
|
2017-12-04 03:00:34 -05:00
|
|
|
*
|
|
|
|
* 'sshc->quote_item' is already verified to be non-NULL before it
|
|
|
|
* switched to this state.
|
2007-11-08 05:22:49 -05:00
|
|
|
*/
|
2011-11-18 09:55:09 -05:00
|
|
|
char *cmd = sshc->quote_item->data;
|
|
|
|
sshc->acceptfail = FALSE;
|
|
|
|
|
|
|
|
/* if a command starts with an asterisk, which a legal SFTP command never
|
|
|
|
can, the command will be allowed to fail without it causing any
|
|
|
|
aborts or cancels etc. It will cause libcurl to act as if the command
|
|
|
|
is successful, whatever the server reponds. */
|
|
|
|
|
|
|
|
if(cmd[0] == '*') {
|
|
|
|
cmd++;
|
|
|
|
sshc->acceptfail = TRUE;
|
|
|
|
}
|
|
|
|
|
2016-09-30 11:15:05 -04:00
|
|
|
if(strcasecompare("pwd", cmd)) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* output debug output if that is requested */
|
2011-01-03 07:34:13 -05:00
|
|
|
char *tmp = aprintf("257 \"%s\" is current directory.\n",
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->path);
|
2011-01-03 07:34:13 -05:00
|
|
|
if(!tmp) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2011-01-03 07:34:13 -05:00
|
|
|
break;
|
|
|
|
}
|
2020-11-02 11:34:04 -05:00
|
|
|
Curl_debug(data, CURLINFO_HEADER_OUT, (char *)"PWD\n", 4);
|
|
|
|
Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp));
|
|
|
|
|
2011-01-01 09:35:53 -05:00
|
|
|
/* this sends an FTP-like "header" to the header callback so that the
|
|
|
|
current directory can be read very similar to how it is read when
|
|
|
|
using ordinary FTP. */
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
|
2011-01-03 07:34:13 -05:00
|
|
|
free(tmp);
|
2014-03-12 21:59:24 -04:00
|
|
|
if(result) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2014-03-12 21:59:24 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
sshc->actualcode = result;
|
|
|
|
}
|
|
|
|
else
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_NEXT_QUOTE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2017-12-04 03:00:34 -05:00
|
|
|
{
|
2010-01-11 18:10:53 -05:00
|
|
|
/*
|
|
|
|
* the arguments following the command must be separated from the
|
|
|
|
* command with a space so we can check for it unconditionally
|
|
|
|
*/
|
2011-11-18 09:55:09 -05:00
|
|
|
cp = strchr(cmd, ' ');
|
2010-01-11 18:10:53 -05:00
|
|
|
if(cp == NULL) {
|
2020-05-28 14:34:20 -04:00
|
|
|
failf(data, "Syntax error command '%s'. Missing parameter!",
|
|
|
|
cmd);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* also, every command takes at least one argument so we get that
|
|
|
|
* first argument right now
|
|
|
|
*/
|
2017-12-08 11:31:01 -05:00
|
|
|
result = Curl_get_pathname(&cp, &sshc->quote_path1, sshc->homedir);
|
2008-02-18 08:05:46 -05:00
|
|
|
if(result) {
|
|
|
|
if(result == CURLE_OUT_OF_MEMORY)
|
2007-11-08 05:22:49 -05:00
|
|
|
failf(data, "Out of memory");
|
|
|
|
else
|
2020-05-28 14:34:20 -04:00
|
|
|
failf(data, "Syntax error: Bad first parameter to '%s'", cmd);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2008-02-18 08:05:46 -05:00
|
|
|
sshc->actualcode = result;
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
/*
|
2014-03-06 07:39:22 -05:00
|
|
|
* SFTP is a binary protocol, so we don't send text commands
|
|
|
|
* to the server. Instead, we scan for commands used by
|
2010-01-11 18:10:53 -05:00
|
|
|
* OpenSSH's sftp program and call the appropriate libssh2
|
|
|
|
* functions.
|
|
|
|
*/
|
2016-09-30 12:54:02 -04:00
|
|
|
if(strncasecompare(cmd, "chgrp ", 6) ||
|
|
|
|
strncasecompare(cmd, "chmod ", 6) ||
|
2020-08-13 19:38:49 -04:00
|
|
|
strncasecompare(cmd, "chown ", 6) ||
|
|
|
|
strncasecompare(cmd, "atime ", 6) ||
|
|
|
|
strncasecompare(cmd, "mtime ", 6)) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* attribute change */
|
|
|
|
|
|
|
|
/* sshc->quote_path1 contains the mode to set */
|
|
|
|
/* get the destination */
|
2017-12-08 11:31:01 -05:00
|
|
|
result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
|
|
|
if(result == CURLE_OUT_OF_MEMORY)
|
|
|
|
failf(data, "Out of memory");
|
|
|
|
else
|
2020-05-28 14:34:20 -04:00
|
|
|
failf(data, "Syntax error in %s: Bad second parameter", cmd);
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = result;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-25 04:44:30 -05:00
|
|
|
memset(&sshp->quote_attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE_STAT);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(strncasecompare(cmd, "ln ", 3) ||
|
2018-04-15 04:49:30 -04:00
|
|
|
strncasecompare(cmd, "symlink ", 8)) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* symbolic linking */
|
|
|
|
/* sshc->quote_path1 is the source */
|
|
|
|
/* get the destination */
|
2017-12-08 11:31:01 -05:00
|
|
|
result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
|
|
|
if(result == CURLE_OUT_OF_MEMORY)
|
|
|
|
failf(data, "Out of memory");
|
|
|
|
else
|
|
|
|
failf(data,
|
|
|
|
"Syntax error in ln/symlink: Bad second parameter");
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = result;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE_SYMLINK);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strncasecompare(cmd, "mkdir ", 6)) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* create dir */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE_MKDIR);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strncasecompare(cmd, "rename ", 7)) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* rename file */
|
|
|
|
/* first param is the source path */
|
|
|
|
/* second param is the dest. path */
|
2017-12-08 11:31:01 -05:00
|
|
|
result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
|
|
|
if(result == CURLE_OUT_OF_MEMORY)
|
|
|
|
failf(data, "Out of memory");
|
|
|
|
else
|
|
|
|
failf(data, "Syntax error in rename: Bad second parameter");
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = result;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE_RENAME);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strncasecompare(cmd, "rmdir ", 6)) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* delete dir */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE_RMDIR);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strncasecompare(cmd, "rm ", 3)) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE_UNLINK);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-05-28 18:20:14 -04:00
|
|
|
#ifdef HAS_STATVFS_SUPPORT
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strncasecompare(cmd, "statvfs ", 8)) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE_STATVFS);
|
2016-02-23 17:37:14 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-05-28 18:20:14 -04:00
|
|
|
#endif
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
failf(data, "Unknown SFTP command");
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_NEXT_QUOTE:
|
2012-03-25 12:10:09 -04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
sshc->quote_item = sshc->quote_item->next;
|
|
|
|
|
|
|
|
if(sshc->quote_item) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(sshc->nextstate != SSH_NO_STATE) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, sshc->nextstate);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_GETINFO);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_QUOTE_STAT:
|
2011-11-18 09:55:09 -05:00
|
|
|
{
|
|
|
|
char *cmd = sshc->quote_item->data;
|
|
|
|
sshc->acceptfail = FALSE;
|
|
|
|
|
|
|
|
/* if a command starts with an asterisk, which a legal SFTP command never
|
|
|
|
can, the command will be allowed to fail without it causing any
|
|
|
|
aborts or cancels etc. It will cause libcurl to act as if the command
|
|
|
|
is successful, whatever the server reponds. */
|
|
|
|
|
|
|
|
if(cmd[0] == '*') {
|
|
|
|
cmd++;
|
|
|
|
sshc->acceptfail = TRUE;
|
|
|
|
}
|
|
|
|
|
2016-09-30 12:54:02 -04:00
|
|
|
if(!strncasecompare(cmd, "chmod", 5)) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* Since chown and chgrp only set owner OR group but libssh2 wants to
|
2010-02-20 16:41:25 -05:00
|
|
|
* set them both at once, we need to obtain the current ownership
|
|
|
|
* first. This takes an extra protocol round trip.
|
2010-01-11 18:10:53 -05:00
|
|
|
*/
|
2010-03-28 02:45:17 -04:00
|
|
|
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path2)),
|
2010-03-28 02:45:17 -04:00
|
|
|
LIBSSH2_SFTP_STAT,
|
2021-01-25 04:44:30 -05:00
|
|
|
&sshp->quote_attrs);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc != 0 && !sshc->acceptfail) { /* get those attributes */
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2007-07-10 18:26:32 -04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Attempt to get SFTP stats failed: %s",
|
2020-06-06 18:30:50 -04:00
|
|
|
sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
/* Now set the new attributes... */
|
2016-09-30 12:54:02 -04:00
|
|
|
if(strncasecompare(cmd, "chgrp", 5)) {
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->quote_attrs.gid = strtoul(sshc->quote_path1, NULL, 10);
|
|
|
|
sshp->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
|
|
|
|
if(sshp->quote_attrs.gid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
|
2011-11-18 09:55:09 -05:00
|
|
|
!sshc->acceptfail) {
|
2007-07-10 18:26:32 -04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Syntax error: chgrp gid not a number");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strncasecompare(cmd, "chmod", 5)) {
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->quote_attrs.permissions = strtoul(sshc->quote_path1, NULL, 8);
|
|
|
|
sshp->quote_attrs.flags = LIBSSH2_SFTP_ATTR_PERMISSIONS;
|
2010-01-11 18:10:53 -05:00
|
|
|
/* permissions are octal */
|
2021-01-25 04:44:30 -05:00
|
|
|
if(sshp->quote_attrs.permissions == 0 &&
|
2010-01-11 18:10:53 -05:00
|
|
|
!ISDIGIT(sshc->quote_path1[0])) {
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Syntax error: chmod permissions not a number");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-13 13:13:44 -04:00
|
|
|
}
|
2016-09-30 12:54:02 -04:00
|
|
|
else if(strncasecompare(cmd, "chown", 5)) {
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->quote_attrs.uid = strtoul(sshc->quote_path1, NULL, 10);
|
|
|
|
sshp->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
|
|
|
|
if(sshp->quote_attrs.uid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
|
2011-11-18 09:55:09 -05:00
|
|
|
!sshc->acceptfail) {
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Syntax error: chown uid not a number");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2020-08-13 19:38:49 -04:00
|
|
|
else if(strncasecompare(cmd, "atime", 5)) {
|
|
|
|
time_t date = Curl_getdate_capped(sshc->quote_path1);
|
|
|
|
if(date == -1) {
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Syntax error: incorrect access date format");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2020-08-13 19:38:49 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->quote_attrs.atime = (unsigned long)date;
|
|
|
|
sshp->quote_attrs.flags = LIBSSH2_SFTP_ATTR_ACMODTIME;
|
2020-08-13 19:38:49 -04:00
|
|
|
}
|
|
|
|
else if(strncasecompare(cmd, "mtime", 5)) {
|
|
|
|
time_t date = Curl_getdate_capped(sshc->quote_path1);
|
|
|
|
if(date == -1) {
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Syntax error: incorrect modification date format");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2020-08-13 19:38:49 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->quote_attrs.mtime = (unsigned long)date;
|
|
|
|
sshp->quote_attrs.flags = LIBSSH2_SFTP_ATTR_ACMODTIME;
|
2020-08-13 19:38:49 -04:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/* Now send the completed structure... */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE_SETSTAT);
|
2008-03-18 18:59:04 -04:00
|
|
|
break;
|
2011-11-18 09:55:09 -05:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_QUOTE_SETSTAT:
|
2010-03-28 02:45:17 -04:00
|
|
|
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path2)),
|
2010-03-28 02:45:17 -04:00
|
|
|
LIBSSH2_SFTP_SETSTAT,
|
2021-01-25 04:44:30 -05:00
|
|
|
&sshp->quote_attrs);
|
2008-07-11 00:38:11 -04:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
2008-11-24 08:59:51 -05:00
|
|
|
break;
|
2008-07-11 00:38:11 -04:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc != 0 && !sshc->acceptfail) {
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2008-11-24 08:59:51 -05:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
2010-01-11 18:10:53 -05:00
|
|
|
failf(data, "Attempt to set SFTP stats failed: %s",
|
2020-06-06 18:30:50 -04:00
|
|
|
sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2008-11-24 08:59:51 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
2008-07-11 00:38:11 -04:00
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_NEXT_QUOTE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_QUOTE_SYMLINK:
|
2010-03-28 02:45:17 -04:00
|
|
|
rc = libssh2_sftp_symlink_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)),
|
2010-03-28 02:45:17 -04:00
|
|
|
sshc->quote_path2,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path2)),
|
2010-03-28 02:45:17 -04:00
|
|
|
LIBSSH2_SFTP_SYMLINK);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc != 0 && !sshc->acceptfail) {
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2007-07-10 18:26:32 -04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
2010-01-11 18:10:53 -05:00
|
|
|
failf(data, "symlink command failed: %s",
|
2020-06-06 18:30:50 -04:00
|
|
|
sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2007-11-08 05:22:49 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_NEXT_QUOTE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_QUOTE_MKDIR:
|
2010-03-28 02:45:17 -04:00
|
|
|
rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)),
|
2011-12-23 17:54:13 -05:00
|
|
|
data->set.new_directory_perms);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc != 0 && !sshc->acceptfail) {
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2007-07-10 18:26:32 -04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2020-06-06 18:30:50 -04:00
|
|
|
failf(data, "mkdir command failed: %s",
|
|
|
|
sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2007-11-08 05:22:49 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_NEXT_QUOTE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_QUOTE_RENAME:
|
2010-03-28 02:45:17 -04:00
|
|
|
rc = libssh2_sftp_rename_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)),
|
2010-03-28 02:45:17 -04:00
|
|
|
sshc->quote_path2,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path2)),
|
2010-03-28 02:45:17 -04:00
|
|
|
LIBSSH2_SFTP_RENAME_OVERWRITE |
|
|
|
|
LIBSSH2_SFTP_RENAME_ATOMIC |
|
|
|
|
LIBSSH2_SFTP_RENAME_NATIVE);
|
2011-04-20 17:37:29 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc != 0 && !sshc->acceptfail) {
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2007-07-10 18:26:32 -04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2007-11-08 05:22:49 -05:00
|
|
|
Curl_safefree(sshc->quote_path2);
|
2020-06-06 18:30:50 -04:00
|
|
|
failf(data, "rename command failed: %s",
|
|
|
|
sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2007-11-08 05:22:49 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_NEXT_QUOTE);
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_QUOTE_RMDIR:
|
2010-03-28 02:45:17 -04:00
|
|
|
rc = libssh2_sftp_rmdir_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)));
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc != 0 && !sshc->acceptfail) {
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2020-06-06 18:30:50 -04:00
|
|
|
failf(data, "rmdir command failed: %s",
|
|
|
|
sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_NEXT_QUOTE);
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_QUOTE_UNLINK:
|
2010-03-28 02:45:17 -04:00
|
|
|
rc = libssh2_sftp_unlink_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)));
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc != 0 && !sshc->acceptfail) {
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2020-06-06 18:30:50 -04:00
|
|
|
failf(data, "rm command failed: %s", sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_NEXT_QUOTE);
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2016-05-28 18:20:14 -04:00
|
|
|
#ifdef HAS_STATVFS_SUPPORT
|
2016-02-23 17:37:14 -05:00
|
|
|
case SSH_SFTP_QUOTE_STATVFS:
|
|
|
|
{
|
|
|
|
LIBSSH2_SFTP_STATVFS statvfs;
|
|
|
|
rc = libssh2_sftp_statvfs(sshc->sftp_session, sshc->quote_path1,
|
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)),
|
|
|
|
&statvfs);
|
|
|
|
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc != 0 && !sshc->acceptfail) {
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2016-02-23 17:37:14 -05:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2020-06-06 18:30:50 -04:00
|
|
|
failf(data, "statvfs command failed: %s",
|
|
|
|
sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2016-02-23 17:37:14 -05:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc == 0) {
|
|
|
|
char *tmp = aprintf("statvfs:\n"
|
|
|
|
"f_bsize: %llu\n" "f_frsize: %llu\n"
|
|
|
|
"f_blocks: %llu\n" "f_bfree: %llu\n"
|
|
|
|
"f_bavail: %llu\n" "f_files: %llu\n"
|
|
|
|
"f_ffree: %llu\n" "f_favail: %llu\n"
|
|
|
|
"f_fsid: %llu\n" "f_flag: %llu\n"
|
|
|
|
"f_namemax: %llu\n",
|
|
|
|
statvfs.f_bsize, statvfs.f_frsize,
|
|
|
|
statvfs.f_blocks, statvfs.f_bfree,
|
|
|
|
statvfs.f_bavail, statvfs.f_files,
|
|
|
|
statvfs.f_ffree, statvfs.f_favail,
|
|
|
|
statvfs.f_fsid, statvfs.f_flag,
|
|
|
|
statvfs.f_namemax);
|
|
|
|
if(!tmp) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2016-02-23 17:37:14 -05:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_client_write(data, CLIENTWRITE_HEADER, tmp, strlen(tmp));
|
2016-02-23 17:37:14 -05:00
|
|
|
free(tmp);
|
|
|
|
if(result) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2016-02-23 17:37:14 -05:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
sshc->actualcode = result;
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_NEXT_QUOTE);
|
2016-02-23 17:37:14 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-05-28 18:20:14 -04:00
|
|
|
#endif
|
2016-02-23 17:18:31 -05:00
|
|
|
case SSH_SFTP_GETINFO:
|
|
|
|
{
|
|
|
|
if(data->set.get_filetime) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_FILETIME);
|
2016-02-23 17:18:31 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_TRANS_INIT);
|
2016-02-23 17:18:31 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SSH_SFTP_FILETIME:
|
|
|
|
{
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs;
|
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshp->path,
|
|
|
|
curlx_uztoui(strlen(sshp->path)),
|
2016-02-23 17:18:31 -05:00
|
|
|
LIBSSH2_SFTP_STAT, &attrs);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc == 0) {
|
2018-01-31 18:24:19 -05:00
|
|
|
data->info.filetime = attrs.mtime;
|
2016-02-23 17:18:31 -05:00
|
|
|
}
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_TRANS_INIT);
|
2016-02-23 17:18:31 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_TRANS_INIT:
|
|
|
|
if(data->set.upload)
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_UPLOAD_INIT);
|
2010-01-11 18:10:53 -05:00
|
|
|
else {
|
2021-01-25 04:44:30 -05:00
|
|
|
if(sshp->path[strlen(sshp->path)-1] == '/')
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_READDIR_INIT);
|
2010-01-11 18:10:53 -05:00
|
|
|
else
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_DOWNLOAD_INIT);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_UPLOAD_INIT:
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
/*
|
|
|
|
* NOTE!!! libssh2 requires that the destination path is a full path
|
|
|
|
* that includes the destination file and name OR ends in a "/"
|
|
|
|
* If this is not done the destination file will be named the
|
|
|
|
* same name as the last directory in the path.
|
|
|
|
*/
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(data->state.resume_from != 0) {
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs;
|
2010-12-05 23:20:05 -05:00
|
|
|
if(data->state.resume_from < 0) {
|
2021-01-25 04:44:30 -05:00
|
|
|
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshp->path,
|
|
|
|
curlx_uztoui(strlen(sshp->path)),
|
2010-03-28 02:45:17 -04:00
|
|
|
LIBSSH2_SFTP_STAT, &attrs);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc) {
|
2010-01-11 18:10:53 -05:00
|
|
|
data->state.resume_from = 0;
|
|
|
|
}
|
|
|
|
else {
|
2010-10-11 18:01:40 -04:00
|
|
|
curl_off_t size = attrs.filesize;
|
|
|
|
if(size < 0) {
|
2013-12-31 06:10:25 -05:00
|
|
|
failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size);
|
2010-10-11 18:01:40 -04:00
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
data->state.resume_from = attrs.filesize;
|
|
|
|
}
|
2008-03-04 06:53:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:21:31 -05:00
|
|
|
if(data->set.remote_append)
|
2010-01-11 18:10:53 -05:00
|
|
|
/* Try to open for append, but create if nonexisting */
|
|
|
|
flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_APPEND;
|
2011-04-20 09:17:42 -04:00
|
|
|
else if(data->state.resume_from > 0)
|
2010-01-11 18:10:53 -05:00
|
|
|
/* If we have restart position then open for append */
|
|
|
|
flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_APPEND;
|
|
|
|
else
|
2020-12-31 04:11:49 -05:00
|
|
|
/* Clear file before writing (normal behavior) */
|
2010-01-11 18:10:53 -05:00
|
|
|
flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC;
|
2008-08-01 14:41:14 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->sftp_handle =
|
2021-01-25 04:44:30 -05:00
|
|
|
libssh2_sftp_open_ex(sshc->sftp_session, sshp->path,
|
|
|
|
curlx_uztoui(strlen(sshp->path)),
|
2010-03-28 02:45:17 -04:00
|
|
|
flags, data->set.new_file_perms,
|
|
|
|
LIBSSH2_SFTP_OPENFILE);
|
2008-03-04 06:53:15 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(!sshc->sftp_handle) {
|
|
|
|
rc = libssh2_session_last_errno(sshc->ssh_session);
|
2008-12-15 18:04:51 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(LIBSSH2_ERROR_EAGAIN == rc)
|
|
|
|
break;
|
2008-12-15 18:04:51 -05:00
|
|
|
|
2017-03-10 08:28:37 -05:00
|
|
|
if(LIBSSH2_ERROR_SFTP_PROTOCOL == rc)
|
|
|
|
/* only when there was an SFTP protocol error can we extract
|
|
|
|
the sftp error! */
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2017-03-10 08:28:37 -05:00
|
|
|
else
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = LIBSSH2_FX_OK; /* not an sftp error at all */
|
2017-03-10 08:28:37 -05:00
|
|
|
|
|
|
|
if(sshc->secondCreateDirs) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2020-06-06 18:30:50 -04:00
|
|
|
sshc->actualcode = sftperr != LIBSSH2_FX_OK ?
|
|
|
|
sftp_libssh2_error_to_CURLE(sftperr):CURLE_SSH;
|
2017-03-10 08:28:37 -05:00
|
|
|
failf(data, "Creating the dir/file failed: %s",
|
2020-06-06 18:30:50 -04:00
|
|
|
sftp_libssh2_strerror(sftperr));
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
|
|
|
}
|
2020-06-06 18:30:50 -04:00
|
|
|
if(((sftperr == LIBSSH2_FX_NO_SUCH_FILE) ||
|
|
|
|
(sftperr == LIBSSH2_FX_FAILURE) ||
|
|
|
|
(sftperr == LIBSSH2_FX_NO_SUCH_PATH)) &&
|
2017-03-10 08:28:37 -05:00
|
|
|
(data->set.ftp_create_missing_dirs &&
|
2021-01-25 04:44:30 -05:00
|
|
|
(strlen(sshp->path) > 1))) {
|
2017-03-10 08:28:37 -05:00
|
|
|
/* try to create the path remotely */
|
|
|
|
rc = 0; /* clear rc and continue */
|
|
|
|
sshc->secondCreateDirs = 1;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CREATE_DIRS_INIT);
|
2017-03-10 08:28:37 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2020-06-06 18:30:50 -04:00
|
|
|
sshc->actualcode = sftperr != LIBSSH2_FX_OK ?
|
|
|
|
sftp_libssh2_error_to_CURLE(sftperr):CURLE_SSH;
|
2017-03-10 08:28:37 -05:00
|
|
|
if(!sshc->actualcode) {
|
2020-06-06 18:30:50 -04:00
|
|
|
/* Sometimes, for some reason libssh2_sftp_last_error() returns zero
|
|
|
|
even though libssh2_sftp_open() failed previously! We need to
|
|
|
|
work around that! */
|
2017-03-10 08:28:37 -05:00
|
|
|
sshc->actualcode = CURLE_SSH;
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = LIBSSH2_FX_OK;
|
2017-03-10 08:28:37 -05:00
|
|
|
}
|
2020-11-23 16:42:07 -05:00
|
|
|
failf(data, "Upload failed: %s (%lu/%d)",
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr != LIBSSH2_FX_OK ?
|
|
|
|
sftp_libssh2_strerror(sftperr):"ssh error",
|
|
|
|
sftperr, rc);
|
2017-03-10 08:28:37 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2014-03-12 22:02:38 -04:00
|
|
|
/* If we have a restart point then we need to seek to the correct
|
2010-01-11 18:10:53 -05:00
|
|
|
position. */
|
|
|
|
if(data->state.resume_from > 0) {
|
|
|
|
/* Let's read off the proper amount of bytes from the input. */
|
|
|
|
if(conn->seek_func) {
|
2018-02-10 09:13:15 -05:00
|
|
|
Curl_set_in_callback(data, true);
|
2010-01-11 18:10:53 -05:00
|
|
|
seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
|
|
|
|
SEEK_SET);
|
2018-02-10 09:13:15 -05:00
|
|
|
Curl_set_in_callback(data, false);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2009-04-28 07:19:10 -04:00
|
|
|
|
2011-07-04 16:10:32 -04:00
|
|
|
if(seekerr != CURL_SEEKFUNC_OK) {
|
2017-09-09 17:09:06 -04:00
|
|
|
curl_off_t passed = 0;
|
2008-03-04 06:53:15 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(seekerr != CURL_SEEKFUNC_CANTSEEK) {
|
|
|
|
failf(data, "Could not seek stream");
|
|
|
|
return CURLE_FTP_COULDNT_USE_REST;
|
|
|
|
}
|
|
|
|
/* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */
|
2017-03-10 08:28:37 -05:00
|
|
|
do {
|
|
|
|
size_t readthisamountnow =
|
2017-04-25 08:38:34 -04:00
|
|
|
(data->state.resume_from - passed > data->set.buffer_size) ?
|
2017-05-02 02:32:04 -04:00
|
|
|
(size_t)data->set.buffer_size :
|
2017-04-25 08:38:34 -04:00
|
|
|
curlx_sotouz(data->state.resume_from - passed);
|
2017-03-10 08:28:37 -05:00
|
|
|
|
2018-02-10 09:13:15 -05:00
|
|
|
size_t actuallyread;
|
|
|
|
Curl_set_in_callback(data, true);
|
|
|
|
actuallyread = data->state.fread_func(data->state.buffer, 1,
|
|
|
|
readthisamountnow,
|
|
|
|
data->state.in);
|
|
|
|
Curl_set_in_callback(data, false);
|
2017-03-10 08:28:37 -05:00
|
|
|
|
|
|
|
passed += actuallyread;
|
|
|
|
if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
|
|
|
|
/* this checks for greater-than only to make sure that the
|
|
|
|
CURL_READFUNC_ABORT return code still aborts */
|
|
|
|
failf(data, "Failed to read data");
|
|
|
|
return CURLE_FTP_COULDNT_USE_REST;
|
|
|
|
}
|
|
|
|
} while(passed < data->state.resume_from);
|
2008-03-04 06:53:15 -05:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
/* now, decrease the size of the read */
|
2014-04-15 07:49:18 -04:00
|
|
|
if(data->state.infilesize > 0) {
|
|
|
|
data->state.infilesize -= data->state.resume_from;
|
|
|
|
data->req.size = data->state.infilesize;
|
|
|
|
Curl_pgrsSetUploadSize(data, data->state.infilesize);
|
2009-04-28 07:19:10 -04:00
|
|
|
}
|
2008-03-04 06:53:15 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
SFTP_SEEK(sshc->sftp_handle, data->state.resume_from);
|
|
|
|
}
|
2014-04-15 07:49:18 -04:00
|
|
|
if(data->state.infilesize > 0) {
|
|
|
|
data->req.size = data->state.infilesize;
|
|
|
|
Curl_pgrsSetUploadSize(data, data->state.infilesize);
|
2008-03-04 06:53:15 -05:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
/* upload data */
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
|
2008-03-04 06:53:15 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/* not set by Curl_setup_transfer to preserve keepon bits */
|
|
|
|
conn->sockfd = conn->writesockfd;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = result;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* store this original bitmask setup to use later on if we can't
|
|
|
|
figure out a "real" bitmask */
|
|
|
|
sshc->orig_waitfor = data->req.keepon;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2011-03-13 09:10:27 -04:00
|
|
|
/* we want to use the _sending_ function even when the socket turns
|
|
|
|
out readable as the underlying libssh2 sftp send function will deal
|
|
|
|
with both accordingly */
|
|
|
|
conn->cselect_bits = CURL_CSELECT_OUT;
|
|
|
|
|
2011-03-12 17:02:04 -05:00
|
|
|
/* since we don't really wait for anything at this point, we want the
|
|
|
|
state machine to move on as soon as possible so we set a very short
|
|
|
|
timeout here */
|
2017-05-09 06:47:49 -04:00
|
|
|
Curl_expire(data, 0, EXPIRE_RUN_NOW);
|
2011-03-12 17:02:04 -05:00
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_CREATE_DIRS_INIT:
|
2021-01-25 04:44:30 -05:00
|
|
|
if(strlen(sshp->path) > 1) {
|
|
|
|
sshc->slash_pos = sshp->path + 1; /* ignore the leading '/' */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CREATE_DIRS);
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_UPLOAD_INIT);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
break;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_CREATE_DIRS:
|
2014-10-06 02:49:43 -04:00
|
|
|
sshc->slash_pos = strchr(sshc->slash_pos, '/');
|
|
|
|
if(sshc->slash_pos) {
|
2010-01-11 18:10:53 -05:00
|
|
|
*sshc->slash_pos = 0;
|
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
infof(data, "Creating directory '%s'\n", sshp->path);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CREATE_DIRS_MKDIR);
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_UPLOAD_INIT);
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_CREATE_DIRS_MKDIR:
|
|
|
|
/* 'mode' - parameter is preliminary - default to 0644 */
|
2021-01-25 04:44:30 -05:00
|
|
|
rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sshp->path,
|
|
|
|
curlx_uztoui(strlen(sshp->path)),
|
2010-03-28 02:45:17 -04:00
|
|
|
data->set.new_directory_perms);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*sshc->slash_pos = '/';
|
|
|
|
++sshc->slash_pos;
|
2017-02-19 05:02:15 -05:00
|
|
|
if(rc < 0) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/*
|
|
|
|
* Abort if failure wasn't that the dir already exists or the
|
|
|
|
* permission was denied (creation might succeed further down the
|
|
|
|
* path) - retry on unspecific FAILURE also
|
|
|
|
*/
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
|
|
|
if((sftperr != LIBSSH2_FX_FILE_ALREADY_EXISTS) &&
|
|
|
|
(sftperr != LIBSSH2_FX_FAILURE) &&
|
|
|
|
(sftperr != LIBSSH2_FX_PERMISSION_DENIED)) {
|
|
|
|
result = sftp_libssh2_error_to_CURLE(sftperr);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = result?result:CURLE_SSH;
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
rc = 0; /* clear rc and continue */
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CREATE_DIRS);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_READDIR_INIT:
|
2014-01-20 16:14:04 -05:00
|
|
|
Curl_pgrsSetDownloadSize(data, -1);
|
2014-02-18 05:21:13 -05:00
|
|
|
if(data->set.opt_no_body) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2014-02-18 05:21:13 -05:00
|
|
|
break;
|
|
|
|
}
|
2014-01-20 16:14:04 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/*
|
|
|
|
* This is a directory that we are trying to get, so produce a directory
|
|
|
|
* listing
|
|
|
|
*/
|
2010-03-28 02:45:17 -04:00
|
|
|
sshc->sftp_handle = libssh2_sftp_open_ex(sshc->sftp_session,
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->path,
|
2012-03-20 13:28:24 -04:00
|
|
|
curlx_uztoui(
|
2021-01-25 04:44:30 -05:00
|
|
|
strlen(sshp->path)),
|
2010-03-28 02:45:17 -04:00
|
|
|
0, 0, LIBSSH2_SFTP_OPENDIR);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(!sshc->sftp_handle) {
|
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
2007-11-12 18:04:44 -05:00
|
|
|
break;
|
|
|
|
}
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2017-03-10 08:28:37 -05:00
|
|
|
failf(data, "Could not open directory for reading: %s",
|
2020-06-06 18:30:50 -04:00
|
|
|
sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2020-06-06 18:30:50 -04:00
|
|
|
result = sftp_libssh2_error_to_CURLE(sftperr);
|
2017-03-10 08:28:37 -05:00
|
|
|
sshc->actualcode = result?result:CURLE_SSH;
|
|
|
|
break;
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->readdir_filename = malloc(PATH_MAX + 1);
|
|
|
|
if(!sshp->readdir_filename) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->readdir_longentry = malloc(PATH_MAX + 1);
|
|
|
|
if(!sshp->readdir_longentry) {
|
|
|
|
Curl_safefree(sshp->readdir_filename);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_dyn_init(&sshp->readdir, PATH_MAX * 2);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_READDIR);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_READDIR:
|
2018-08-31 04:17:40 -04:00
|
|
|
rc = libssh2_sftp_readdir_ex(sshc->sftp_handle,
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->readdir_filename,
|
2018-08-31 04:17:40 -04:00
|
|
|
PATH_MAX,
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->readdir_longentry,
|
2018-08-31 04:17:40 -04:00
|
|
|
PATH_MAX,
|
2021-01-25 04:44:30 -05:00
|
|
|
&sshp->readdir_attrs);
|
2018-08-31 04:17:40 -04:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2018-08-31 04:17:40 -04:00
|
|
|
if(rc > 0) {
|
2020-05-04 08:44:07 -04:00
|
|
|
readdir_len = (size_t) rc;
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->readdir_filename[readdir_len] = '\0';
|
2010-01-11 18:10:53 -05:00
|
|
|
|
2021-02-08 10:40:34 -05:00
|
|
|
if(data->set.list_only) {
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY,
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->readdir_filename,
|
2020-05-04 08:44:07 -04:00
|
|
|
readdir_len);
|
|
|
|
if(!result)
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY,
|
2020-05-04 08:44:07 -04:00
|
|
|
(char *)"\n", 1);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* since this counts what we send to the client, we include the
|
|
|
|
newline in this counter */
|
2020-05-04 08:44:07 -04:00
|
|
|
data->req.bytecount += readdir_len + 1;
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
/* output debug output if that is requested */
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_debug(data, CURLINFO_DATA_IN, sshp->readdir_filename,
|
2020-11-02 11:34:04 -05:00
|
|
|
readdir_len);
|
|
|
|
Curl_debug(data, CURLINFO_DATA_IN, (char *)"\n", 1);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
2021-01-25 04:44:30 -05:00
|
|
|
result = Curl_dyn_add(&sshp->readdir, sshp->readdir_longentry);
|
2020-05-04 08:44:07 -04:00
|
|
|
|
|
|
|
if(!result) {
|
2021-01-25 04:44:30 -05:00
|
|
|
if((sshp->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
|
|
|
|
((sshp->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
|
2020-05-04 08:44:07 -04:00
|
|
|
LIBSSH2_SFTP_S_IFLNK)) {
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_dyn_init(&sshp->readdir_link, PATH_MAX);
|
|
|
|
result = Curl_dyn_add(&sshp->readdir_link, sshp->path);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_READDIR_LINK);
|
2020-05-04 08:44:07 -04:00
|
|
|
if(!result)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_READDIR_BOTTOM);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 08:44:07 -04:00
|
|
|
sshc->actualcode = result;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2018-08-31 04:17:40 -04:00
|
|
|
else if(rc == 0) {
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_safefree(sshp->readdir_filename);
|
|
|
|
Curl_safefree(sshp->readdir_longentry);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_READDIR_DONE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2018-08-31 04:17:40 -04:00
|
|
|
else if(rc < 0) {
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
|
|
|
result = sftp_libssh2_error_to_CURLE(sftperr);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = result?result:CURLE_SSH;
|
|
|
|
failf(data, "Could not open remote file for reading: %s :: %d",
|
2020-06-06 18:30:50 -04:00
|
|
|
sftp_libssh2_strerror(sftperr),
|
2010-01-11 18:10:53 -05:00
|
|
|
libssh2_session_last_errno(sshc->ssh_session));
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_safefree(sshp->readdir_filename);
|
|
|
|
Curl_safefree(sshp->readdir_longentry);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_READDIR_LINK:
|
2018-08-31 04:17:40 -04:00
|
|
|
rc =
|
2010-03-28 02:45:17 -04:00
|
|
|
libssh2_sftp_symlink_ex(sshc->sftp_session,
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_dyn_ptr(&sshp->readdir_link),
|
|
|
|
(int)Curl_dyn_len(&sshp->readdir_link),
|
|
|
|
sshp->readdir_filename,
|
2010-03-28 02:45:17 -04:00
|
|
|
PATH_MAX, LIBSSH2_SFTP_READLINK);
|
2018-08-31 04:17:40 -04:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_dyn_free(&sshp->readdir_link);
|
2020-05-04 08:44:07 -04:00
|
|
|
|
|
|
|
/* append filename and extra output */
|
2021-01-25 04:44:30 -05:00
|
|
|
result = Curl_dyn_addf(&sshp->readdir, " -> %s", sshp->readdir_filename);
|
2011-08-31 12:15:29 -04:00
|
|
|
|
2020-05-04 08:44:07 -04:00
|
|
|
if(result) {
|
2016-11-07 04:55:25 -05:00
|
|
|
sshc->readdir_line = NULL;
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_safefree(sshp->readdir_filename);
|
|
|
|
Curl_safefree(sshp->readdir_longentry);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2020-05-04 08:44:07 -04:00
|
|
|
sshc->actualcode = result;
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_READDIR_BOTTOM);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_READDIR_BOTTOM:
|
2021-01-25 04:44:30 -05:00
|
|
|
result = Curl_dyn_addn(&sshp->readdir, "\n", 1);
|
2020-05-04 08:44:07 -04:00
|
|
|
if(!result)
|
2021-01-08 11:58:15 -05:00
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY,
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_dyn_ptr(&sshp->readdir),
|
|
|
|
Curl_dyn_len(&sshp->readdir));
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!result) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* output debug output if that is requested */
|
2020-11-02 11:34:04 -05:00
|
|
|
Curl_debug(data, CURLINFO_DATA_IN,
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_dyn_ptr(&sshp->readdir),
|
|
|
|
Curl_dyn_len(&sshp->readdir));
|
|
|
|
data->req.bytecount += Curl_dyn_len(&sshp->readdir);
|
2007-11-12 18:04:44 -05:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_dyn_free(&sshp->readdir);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2020-05-04 08:44:07 -04:00
|
|
|
else {
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_dyn_reset(&sshp->readdir);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_READDIR);
|
2020-05-04 08:44:07 -04:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_READDIR_DONE:
|
|
|
|
if(libssh2_sftp_closedir(sshc->sftp_handle) ==
|
2007-11-08 05:22:49 -05:00
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
2008-11-24 08:59:51 -05:00
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->sftp_handle = NULL;
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_safefree(sshp->readdir_filename);
|
|
|
|
Curl_safefree(sshp->readdir_longentry);
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/* no data to transfer */
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, -1);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
case SSH_SFTP_DOWNLOAD_INIT:
|
2007-11-08 05:22:49 -05:00
|
|
|
/*
|
2010-01-11 18:10:53 -05:00
|
|
|
* Work on getting the specified file
|
2007-11-08 05:22:49 -05:00
|
|
|
*/
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->sftp_handle =
|
2021-01-25 04:44:30 -05:00
|
|
|
libssh2_sftp_open_ex(sshc->sftp_session, sshp->path,
|
|
|
|
curlx_uztoui(strlen(sshp->path)),
|
2010-03-28 02:45:17 -04:00
|
|
|
LIBSSH2_FXF_READ, data->set.new_file_perms,
|
|
|
|
LIBSSH2_SFTP_OPENFILE);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(!sshc->sftp_handle) {
|
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
2008-09-29 09:21:21 -04:00
|
|
|
}
|
2020-06-06 18:30:50 -04:00
|
|
|
sftperr = libssh2_sftp_last_error(sshc->sftp_session);
|
2017-03-10 08:28:37 -05:00
|
|
|
failf(data, "Could not open remote file for reading: %s",
|
2020-06-06 18:30:50 -04:00
|
|
|
sftp_libssh2_strerror(sftperr));
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2020-06-06 18:30:50 -04:00
|
|
|
result = sftp_libssh2_error_to_CURLE(sftperr);
|
2017-03-10 08:28:37 -05:00
|
|
|
sshc->actualcode = result?result:CURLE_SSH;
|
|
|
|
break;
|
2008-09-29 09:21:21 -04:00
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_DOWNLOAD_STAT);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_DOWNLOAD_STAT:
|
|
|
|
{
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs;
|
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshp->path,
|
|
|
|
curlx_uztoui(strlen(sshp->path)),
|
2010-03-28 02:45:17 -04:00
|
|
|
LIBSSH2_SFTP_STAT, &attrs);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc ||
|
2018-04-15 04:49:30 -04:00
|
|
|
!(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) ||
|
|
|
|
(attrs.filesize == 0)) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/*
|
|
|
|
* libssh2_sftp_open() didn't return an error, so maybe the server
|
|
|
|
* just doesn't support stat()
|
2014-12-15 16:52:30 -05:00
|
|
|
* OR the server doesn't return a file size with a stat()
|
|
|
|
* OR file size is 0
|
2010-01-11 18:10:53 -05:00
|
|
|
*/
|
|
|
|
data->req.size = -1;
|
|
|
|
data->req.maxdownload = -1;
|
2014-01-20 16:14:04 -05:00
|
|
|
Curl_pgrsSetDownloadSize(data, -1);
|
2008-03-04 06:53:15 -05:00
|
|
|
}
|
|
|
|
else {
|
2010-09-30 17:08:37 -04:00
|
|
|
curl_off_t size = attrs.filesize;
|
2010-01-11 18:10:53 -05:00
|
|
|
|
2010-09-30 17:08:37 -04:00
|
|
|
if(size < 0) {
|
2013-12-31 06:10:25 -05:00
|
|
|
failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size);
|
2010-09-30 17:08:37 -04:00
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
2021-01-08 11:58:15 -05:00
|
|
|
if(data->state.use_range) {
|
2010-01-11 18:10:53 -05:00
|
|
|
curl_off_t from, to;
|
|
|
|
char *ptr;
|
|
|
|
char *ptr2;
|
2017-08-14 17:33:23 -04:00
|
|
|
CURLofft to_t;
|
|
|
|
CURLofft from_t;
|
2010-01-11 18:10:53 -05:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
from_t = curlx_strtoofft(data->state.range, &ptr, 0, &from);
|
2017-08-14 17:33:23 -04:00
|
|
|
if(from_t == CURL_OFFT_FLOW)
|
|
|
|
return CURLE_RANGE_ERROR;
|
2017-09-09 17:09:06 -04:00
|
|
|
while(*ptr && (ISSPACE(*ptr) || (*ptr == '-')))
|
2010-01-11 18:10:53 -05:00
|
|
|
ptr++;
|
2017-08-14 17:33:23 -04:00
|
|
|
to_t = curlx_strtoofft(ptr, &ptr2, 0, &to);
|
|
|
|
if(to_t == CURL_OFFT_FLOW)
|
|
|
|
return CURLE_RANGE_ERROR;
|
|
|
|
if((to_t == CURL_OFFT_INVAL) /* no "to" value given */
|
2010-01-11 18:10:53 -05:00
|
|
|
|| (to >= size)) {
|
|
|
|
to = size - 1;
|
|
|
|
}
|
2017-08-14 17:33:23 -04:00
|
|
|
if(from_t) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* from is relative to end of file */
|
2017-08-14 17:33:23 -04:00
|
|
|
from = size - to;
|
|
|
|
to = size - 1;
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2015-08-06 09:29:42 -04:00
|
|
|
if(from > size) {
|
2010-01-11 18:10:53 -05:00
|
|
|
failf(data, "Offset (%"
|
2013-12-31 06:10:25 -05:00
|
|
|
CURL_FORMAT_CURL_OFF_T ") was beyond file size (%"
|
|
|
|
CURL_FORMAT_CURL_OFF_T ")", from, attrs.filesize);
|
2010-01-11 18:10:53 -05:00
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
|
|
|
if(from > to) {
|
|
|
|
from = to;
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size = to - from + 1;
|
|
|
|
}
|
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
SFTP_SEEK(sshc->sftp_handle, from);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
data->req.size = size;
|
|
|
|
data->req.maxdownload = size;
|
|
|
|
Curl_pgrsSetDownloadSize(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can resume if we can seek to the resume position */
|
|
|
|
if(data->state.resume_from) {
|
2010-12-05 23:20:05 -05:00
|
|
|
if(data->state.resume_from < 0) {
|
2010-01-11 18:10:53 -05:00
|
|
|
/* We're supposed to download the last abs(from) bytes */
|
|
|
|
if((curl_off_t)attrs.filesize < -data->state.resume_from) {
|
|
|
|
failf(data, "Offset (%"
|
2013-12-31 06:10:25 -05:00
|
|
|
CURL_FORMAT_CURL_OFF_T ") was beyond file size (%"
|
|
|
|
CURL_FORMAT_CURL_OFF_T ")",
|
2010-01-11 18:10:53 -05:00
|
|
|
data->state.resume_from, attrs.filesize);
|
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
|
|
|
/* download from where? */
|
2010-12-05 23:20:05 -05:00
|
|
|
data->state.resume_from += attrs.filesize;
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if((curl_off_t)attrs.filesize < data->state.resume_from) {
|
2013-12-31 06:10:25 -05:00
|
|
|
failf(data, "Offset (%" CURL_FORMAT_CURL_OFF_T
|
|
|
|
") was beyond file size (%" CURL_FORMAT_CURL_OFF_T ")",
|
2010-01-11 18:10:53 -05:00
|
|
|
data->state.resume_from, attrs.filesize);
|
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Now store the number of bytes we are expected to download */
|
|
|
|
data->req.size = attrs.filesize - data->state.resume_from;
|
|
|
|
data->req.maxdownload = attrs.filesize - data->state.resume_from;
|
|
|
|
Curl_pgrsSetDownloadSize(data,
|
|
|
|
attrs.filesize - data->state.resume_from);
|
|
|
|
SFTP_SEEK(sshc->sftp_handle, data->state.resume_from);
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 16:14:04 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/* Setup the actual download */
|
|
|
|
if(data->req.size == 0) {
|
|
|
|
/* no data to transfer */
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, -1);
|
2010-01-11 18:10:53 -05:00
|
|
|
infof(data, "File already completely downloaded\n");
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2008-03-04 06:53:15 -05:00
|
|
|
}
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(data, FIRSTSOCKET, data->req.size, FALSE, -1);
|
2009-09-26 04:31:48 -04:00
|
|
|
|
2017-03-10 08:28:37 -05:00
|
|
|
/* not set by Curl_setup_transfer to preserve keepon bits */
|
|
|
|
conn->writesockfd = conn->sockfd;
|
|
|
|
|
|
|
|
/* we want to use the _receiving_ function even when the socket turns
|
|
|
|
out writableable as the underlying libssh2 recv function will deal
|
|
|
|
with both accordingly */
|
|
|
|
conn->cselect_bits = CURL_CSELECT_IN;
|
2009-09-26 04:31:48 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
2014-03-12 21:59:24 -04:00
|
|
|
/* this should never occur; the close state should be entered
|
|
|
|
at the time the error occurs */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = result;
|
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_CLOSE:
|
|
|
|
if(sshc->sftp_handle) {
|
|
|
|
rc = libssh2_sftp_close(sshc->sftp_handle);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc < 0) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "Failed to close libssh2 file: %d %s\n", rc, err_msg);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
sshc->sftp_handle = NULL;
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
2017-11-24 18:33:59 -05:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_safefree(sshp->path);
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
DEBUGF(infof(data, "SFTP DONE done\n"));
|
2011-04-20 17:37:29 -04:00
|
|
|
|
|
|
|
/* Check if nextstate is set and move .nextstate could be POSTQUOTE_INIT
|
2015-03-17 08:41:49 -04:00
|
|
|
After nextstate is executed, the control should come back to
|
2011-04-20 17:37:29 -04:00
|
|
|
SSH_SFTP_CLOSE to pass the correct result back */
|
2014-03-15 04:57:41 -04:00
|
|
|
if(sshc->nextstate != SSH_NO_STATE &&
|
|
|
|
sshc->nextstate != SSH_SFTP_CLOSE) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, sshc->nextstate);
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_SFTP_CLOSE;
|
|
|
|
}
|
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2011-04-20 17:37:29 -04:00
|
|
|
result = sshc->actualcode;
|
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SFTP_SHUTDOWN:
|
|
|
|
/* during times we get here due to a broken transfer and then the
|
|
|
|
sftp_handle might not have been taken down so make sure that is done
|
|
|
|
before we proceed */
|
2008-05-28 16:31:28 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(sshc->sftp_handle) {
|
|
|
|
rc = libssh2_sftp_close(sshc->sftp_handle);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc < 0) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session, &err_msg,
|
|
|
|
NULL, 0);
|
|
|
|
infof(data, "Failed to close libssh2 file: %d %s\n", rc, err_msg);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
sshc->sftp_handle = NULL;
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
if(sshc->sftp_session) {
|
|
|
|
rc = libssh2_sftp_shutdown(sshc->sftp_session);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc < 0) {
|
2010-01-11 18:10:53 -05:00
|
|
|
infof(data, "Failed to stop libssh2 sftp subsystem\n");
|
|
|
|
}
|
|
|
|
sshc->sftp_session = NULL;
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->homedir);
|
2021-01-08 11:58:15 -05:00
|
|
|
data->state.most_recent_ftp_entrypath = NULL;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_DISCONNECT);
|
2007-07-10 18:26:32 -04:00
|
|
|
break;
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SCP_TRANS_INIT:
|
2021-01-25 04:44:30 -05:00
|
|
|
result = Curl_getworkingpath(data, sshc->homedir, &sshp->path);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
|
|
|
sshc->actualcode = result;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2007-11-08 05:22:49 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(data->set.upload) {
|
2014-04-15 07:49:18 -04:00
|
|
|
if(data->state.infilesize < 0) {
|
2010-01-11 18:10:53 -05:00
|
|
|
failf(data, "SCP requires a known file size for upload");
|
|
|
|
sshc->actualcode = CURLE_UPLOAD_FAILED;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_CHANNEL_FREE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_UPLOAD_INIT);
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
else {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_DOWNLOAD_INIT);
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SCP_UPLOAD_INIT:
|
|
|
|
/*
|
|
|
|
* libssh2 requires that the destination path is a full path that
|
|
|
|
* includes the destination file and name OR ends in a "/" . If this is
|
|
|
|
* not done the destination file will be named the same name as the last
|
|
|
|
* directory in the path.
|
|
|
|
*/
|
|
|
|
sshc->ssh_channel =
|
2021-01-25 04:44:30 -05:00
|
|
|
SCP_SEND(sshc->ssh_session, sshp->path, data->set.new_file_perms,
|
2014-04-15 07:49:18 -04:00
|
|
|
data->state.infilesize);
|
2010-01-11 18:10:53 -05:00
|
|
|
if(!sshc->ssh_channel) {
|
2017-03-10 08:28:37 -05:00
|
|
|
int ssh_err;
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
2017-03-10 08:28:37 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-10 08:28:37 -05:00
|
|
|
ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0));
|
2021-01-08 11:58:15 -05:00
|
|
|
failf(data, "%s", err_msg);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_CHANNEL_FREE);
|
2017-03-10 08:28:37 -05:00
|
|
|
sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
|
2017-11-05 09:06:30 -05:00
|
|
|
/* Map generic errors to upload failed */
|
|
|
|
if(sshc->actualcode == CURLE_SSH ||
|
|
|
|
sshc->actualcode == CURLE_REMOTE_FILE_NOT_FOUND)
|
|
|
|
sshc->actualcode = CURLE_UPLOAD_FAILED;
|
2017-03-10 08:28:37 -05:00
|
|
|
break;
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/* upload data */
|
2020-05-01 11:42:19 -04:00
|
|
|
data->req.size = data->state.infilesize;
|
|
|
|
Curl_pgrsSetUploadSize(data, data->state.infilesize);
|
|
|
|
Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
|
2009-09-26 04:31:48 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/* not set by Curl_setup_transfer to preserve keepon bits */
|
|
|
|
conn->sockfd = conn->writesockfd;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_CHANNEL_FREE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = result;
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
else {
|
2013-10-07 10:07:50 -04:00
|
|
|
/* store this original bitmask setup to use later on if we can't
|
|
|
|
figure out a "real" bitmask */
|
|
|
|
sshc->orig_waitfor = data->req.keepon;
|
|
|
|
|
2011-03-13 18:21:03 -04:00
|
|
|
/* we want to use the _sending_ function even when the socket turns
|
|
|
|
out readable as the underlying libssh2 scp send function will deal
|
|
|
|
with both accordingly */
|
|
|
|
conn->cselect_bits = CURL_CSELECT_OUT;
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SCP_DOWNLOAD_INIT:
|
|
|
|
{
|
2015-07-20 23:27:12 -04:00
|
|
|
curl_off_t bytecount;
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/*
|
|
|
|
* We must check the remote file; if it is a directory no values will
|
|
|
|
* be set in sb
|
|
|
|
*/
|
|
|
|
|
2018-04-15 04:49:30 -04:00
|
|
|
/*
|
|
|
|
* If support for >2GB files exists, use it.
|
|
|
|
*/
|
2010-01-11 18:10:53 -05:00
|
|
|
|
|
|
|
/* get a fresh new channel from the ssh layer */
|
2015-07-20 23:27:12 -04:00
|
|
|
#if LIBSSH2_VERSION_NUM < 0x010700
|
|
|
|
struct stat sb;
|
|
|
|
memset(&sb, 0, sizeof(struct stat));
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->ssh_channel = libssh2_scp_recv(sshc->ssh_session,
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->path, &sb);
|
2015-07-20 23:27:12 -04:00
|
|
|
#else
|
|
|
|
libssh2_struct_stat sb;
|
|
|
|
memset(&sb, 0, sizeof(libssh2_struct_stat));
|
|
|
|
sshc->ssh_channel = libssh2_scp_recv2(sshc->ssh_session,
|
2021-01-25 04:44:30 -05:00
|
|
|
sshp->path, &sb);
|
2015-07-20 23:27:12 -04:00
|
|
|
#endif
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(!sshc->ssh_channel) {
|
2017-03-10 08:28:37 -05:00
|
|
|
int ssh_err;
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
2017-03-10 08:28:37 -05:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-10 08:28:37 -05:00
|
|
|
|
|
|
|
ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0));
|
2021-01-08 11:58:15 -05:00
|
|
|
failf(data, "%s", err_msg);
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_CHANNEL_FREE);
|
2017-03-10 08:28:37 -05:00
|
|
|
sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
|
|
|
|
break;
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2009-09-26 04:31:48 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/* download data */
|
|
|
|
bytecount = (curl_off_t)sb.st_size;
|
2020-05-26 02:26:20 -04:00
|
|
|
data->req.maxdownload = (curl_off_t)sb.st_size;
|
2019-02-28 05:36:26 -05:00
|
|
|
Curl_setup_transfer(data, FIRSTSOCKET, bytecount, FALSE, -1);
|
2009-09-26 04:31:48 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
/* not set by Curl_setup_transfer to preserve keepon bits */
|
|
|
|
conn->writesockfd = conn->sockfd;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2011-03-13 18:21:03 -04:00
|
|
|
/* we want to use the _receiving_ function even when the socket turns
|
|
|
|
out writableable as the underlying libssh2 recv function will deal
|
|
|
|
with both accordingly */
|
2010-01-11 18:10:53 -05:00
|
|
|
conn->cselect_bits = CURL_CSELECT_IN;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(result) {
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_CHANNEL_FREE);
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->actualcode = result;
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
else
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SCP_DONE:
|
|
|
|
if(data->set.upload)
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_SEND_EOF);
|
2010-01-11 18:10:53 -05:00
|
|
|
else
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_CHANNEL_FREE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SCP_SEND_EOF:
|
|
|
|
if(sshc->ssh_channel) {
|
|
|
|
rc = libssh2_channel_send_eof(sshc->ssh_channel);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "Failed to send libssh2 channel EOF: %d %s\n",
|
|
|
|
rc, err_msg);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_WAIT_EOF);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SCP_WAIT_EOF:
|
|
|
|
if(sshc->ssh_channel) {
|
|
|
|
rc = libssh2_channel_wait_eof(sshc->ssh_channel);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "Failed to get channel EOF: %d %s\n", rc, err_msg);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_WAIT_CLOSE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SCP_WAIT_CLOSE:
|
|
|
|
if(sshc->ssh_channel) {
|
|
|
|
rc = libssh2_channel_wait_closed(sshc->ssh_channel);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "Channel failed to close: %d %s\n", rc, err_msg);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_CHANNEL_FREE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SCP_CHANNEL_FREE:
|
|
|
|
if(sshc->ssh_channel) {
|
|
|
|
rc = libssh2_channel_free(sshc->ssh_channel);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc < 0) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "Failed to free libssh2 scp subsystem: %d %s\n",
|
|
|
|
rc, err_msg);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
sshc->ssh_channel = NULL;
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2010-01-11 18:10:53 -05:00
|
|
|
DEBUGF(infof(data, "SCP DONE phase complete\n"));
|
2007-11-08 05:22:49 -05:00
|
|
|
#if 0 /* PREV */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_DISCONNECT);
|
2007-11-08 05:22:49 -05:00
|
|
|
#endif
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
result = sshc->actualcode;
|
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SESSION_DISCONNECT:
|
|
|
|
/* during weird times when we've been prematurely aborted, the channel
|
|
|
|
is still alive when we reach this state and we MUST kill the channel
|
|
|
|
properly first */
|
|
|
|
if(sshc->ssh_channel) {
|
|
|
|
rc = libssh2_channel_free(sshc->ssh_channel);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc < 0) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "Failed to free libssh2 scp subsystem: %d %s\n",
|
|
|
|
rc, err_msg);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
sshc->ssh_channel = NULL;
|
2008-05-28 16:31:28 -04:00
|
|
|
}
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(sshc->ssh_session) {
|
|
|
|
rc = libssh2_session_disconnect(sshc->ssh_session, "Shutdown");
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc < 0) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "Failed to disconnect libssh2 session: %d %s\n",
|
|
|
|
rc, err_msg);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
Curl_safefree(sshc->homedir);
|
2021-01-08 11:58:15 -05:00
|
|
|
data->state.most_recent_ftp_entrypath = NULL;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_FREE);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
case SSH_SESSION_FREE:
|
2009-10-17 21:11:25 -04:00
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
2010-01-11 18:10:53 -05:00
|
|
|
if(sshc->kh) {
|
|
|
|
libssh2_knownhost_free(sshc->kh);
|
|
|
|
sshc->kh = NULL;
|
|
|
|
}
|
2009-10-17 21:11:25 -04:00
|
|
|
#endif
|
|
|
|
|
2012-03-09 11:24:42 -05:00
|
|
|
#ifdef HAVE_LIBSSH2_AGENT_API
|
|
|
|
if(sshc->ssh_agent) {
|
|
|
|
rc = libssh2_agent_disconnect(sshc->ssh_agent);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc < 0) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "Failed to disconnect from libssh2 agent: %d %s\n",
|
|
|
|
rc, err_msg);
|
2012-03-09 11:24:42 -05:00
|
|
|
}
|
2016-12-13 17:34:59 -05:00
|
|
|
libssh2_agent_free(sshc->ssh_agent);
|
2012-03-09 11:24:42 -05:00
|
|
|
sshc->ssh_agent = NULL;
|
|
|
|
|
|
|
|
/* NB: there is no need to free identities, they are part of internal
|
|
|
|
agent stuff */
|
|
|
|
sshc->sshagent_identity = NULL;
|
|
|
|
sshc->sshagent_prev_identity = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
if(sshc->ssh_session) {
|
|
|
|
rc = libssh2_session_free(sshc->ssh_session);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
if(rc < 0) {
|
2018-04-15 04:49:30 -04:00
|
|
|
char *err_msg = NULL;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "Failed to free libssh2 session: %d %s\n", rc, err_msg);
|
2010-01-11 18:10:53 -05:00
|
|
|
}
|
|
|
|
sshc->ssh_session = NULL;
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
2011-09-06 19:26:43 -04:00
|
|
|
|
|
|
|
/* worst-case scenario cleanup */
|
|
|
|
|
|
|
|
DEBUGASSERT(sshc->ssh_session == NULL);
|
|
|
|
DEBUGASSERT(sshc->ssh_channel == NULL);
|
|
|
|
DEBUGASSERT(sshc->sftp_session == NULL);
|
|
|
|
DEBUGASSERT(sshc->sftp_handle == NULL);
|
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
|
|
|
DEBUGASSERT(sshc->kh == NULL);
|
|
|
|
#endif
|
2012-12-19 13:52:11 -05:00
|
|
|
#ifdef HAVE_LIBSSH2_AGENT_API
|
|
|
|
DEBUGASSERT(sshc->ssh_agent == NULL);
|
|
|
|
#endif
|
2011-09-06 19:26:43 -04:00
|
|
|
|
|
|
|
Curl_safefree(sshc->rsa_pub);
|
|
|
|
Curl_safefree(sshc->rsa);
|
|
|
|
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
|
|
|
|
Curl_safefree(sshc->homedir);
|
|
|
|
Curl_safefree(sshc->readdir_line);
|
|
|
|
|
|
|
|
/* the code we are about to return */
|
|
|
|
result = sshc->actualcode;
|
|
|
|
|
|
|
|
memset(sshc, 0, sizeof(struct ssh_conn));
|
|
|
|
|
2014-05-20 04:32:23 -04:00
|
|
|
connclose(conn, "SSH session free");
|
2011-09-06 19:26:43 -04:00
|
|
|
sshc->state = SSH_SESSION_FREE; /* current */
|
2010-01-11 18:10:53 -05:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_QUIT:
|
|
|
|
/* fallthrough, just stop! */
|
|
|
|
default:
|
|
|
|
/* internal error */
|
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_STOP);
|
2010-01-11 18:10:53 -05:00
|
|
|
break;
|
2007-11-08 05:22:49 -05:00
|
|
|
}
|
|
|
|
|
2010-01-11 18:10:53 -05:00
|
|
|
} while(!rc && (sshc->state != SSH_STOP));
|
2007-06-12 08:31:10 -04:00
|
|
|
|
2008-11-24 08:59:51 -05:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
/* we would block, we need to wait for the socket to be ready (in the
|
|
|
|
right direction too)! */
|
|
|
|
*block = TRUE;
|
|
|
|
}
|
|
|
|
|
2007-06-12 08:31:10 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-12-19 16:14:52 -05:00
|
|
|
/* called by the multi interface to figure out what socket(s) to wait for and
|
|
|
|
for what actions in the DO_DONE, PERFORM and WAITPERFORM states */
|
2021-01-08 11:58:15 -05:00
|
|
|
static int ssh_getsock(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn,
|
|
|
|
curl_socket_t *sock)
|
2008-12-19 16:14:52 -05:00
|
|
|
{
|
|
|
|
int bitmap = GETSOCK_BLANK;
|
2021-01-08 11:58:15 -05:00
|
|
|
(void)data;
|
2008-12-19 16:14:52 -05:00
|
|
|
|
|
|
|
sock[0] = conn->sock[FIRSTSOCKET];
|
|
|
|
|
2009-09-26 04:31:48 -04:00
|
|
|
if(conn->waitfor & KEEP_RECV)
|
2008-12-19 16:14:52 -05:00
|
|
|
bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
|
|
|
|
|
2009-09-26 04:31:48 -04:00
|
|
|
if(conn->waitfor & KEEP_SEND)
|
2008-12-19 16:14:52 -05:00
|
|
|
bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When one of the libssh2 functions has returned LIBSSH2_ERROR_EAGAIN this
|
|
|
|
* function is used to figure out in what direction and stores this info so
|
|
|
|
* that the multi interface can take advantage of it. Make sure to call this
|
|
|
|
* function in all cases so that when it _doesn't_ return EAGAIN we can
|
|
|
|
* restore the default wait bits.
|
|
|
|
*/
|
2021-01-25 04:44:30 -05:00
|
|
|
static void ssh_block2waitfor(struct Curl_easy *data, bool block)
|
2008-12-19 16:14:52 -05:00
|
|
|
{
|
2021-01-25 04:44:30 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2008-12-19 16:14:52 -05:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2016-12-13 19:29:44 -05:00
|
|
|
int dir = 0;
|
|
|
|
if(block) {
|
|
|
|
dir = libssh2_session_block_directions(sshc->ssh_session);
|
|
|
|
if(dir) {
|
|
|
|
/* translate the libssh2 define bits into our own bit defines */
|
|
|
|
conn->waitfor = ((dir&LIBSSH2_SESSION_BLOCK_INBOUND)?KEEP_RECV:0) |
|
|
|
|
((dir&LIBSSH2_SESSION_BLOCK_OUTBOUND)?KEEP_SEND:0);
|
|
|
|
}
|
2008-12-19 16:14:52 -05:00
|
|
|
}
|
2016-12-13 19:29:44 -05:00
|
|
|
if(!dir)
|
2008-12-19 16:14:52 -05:00
|
|
|
/* It didn't block or libssh2 didn't reveal in which direction, put back
|
|
|
|
the original set */
|
2009-09-26 04:31:48 -04:00
|
|
|
conn->waitfor = sshc->orig_waitfor;
|
2008-12-19 16:14:52 -05:00
|
|
|
}
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
/* called repeatedly until done from multi.c */
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode ssh_multi_statemach(struct Curl_easy *data, bool *done)
|
2007-06-12 08:31:10 -04:00
|
|
|
{
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2007-06-12 08:31:10 -04:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
CURLcode result = CURLE_OK;
|
2008-12-19 16:14:52 -05:00
|
|
|
bool block; /* we store the status and use that to provide a ssh_getsock()
|
|
|
|
implementation */
|
2019-03-04 10:17:10 -05:00
|
|
|
do {
|
2021-01-08 11:58:15 -05:00
|
|
|
result = ssh_statemach_act(data, &block);
|
2019-03-04 10:17:10 -05:00
|
|
|
*done = (sshc->state == SSH_STOP) ? TRUE : FALSE;
|
|
|
|
/* if there's no error, it isn't done and it didn't EWOULDBLOCK, then
|
|
|
|
try again */
|
|
|
|
} while(!result && !*done && !block);
|
2021-01-25 04:44:30 -05:00
|
|
|
ssh_block2waitfor(data, block);
|
2007-06-12 08:31:10 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode ssh_block_statemach(struct Curl_easy *data,
|
2021-01-25 04:44:30 -05:00
|
|
|
struct connectdata *conn,
|
|
|
|
bool duringconnect)
|
2007-06-12 08:31:10 -04:00
|
|
|
{
|
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
CURLcode result = CURLE_OK;
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2008-11-24 08:59:51 -05:00
|
|
|
while((sshc->state != SSH_STOP) && !result) {
|
|
|
|
bool block;
|
2017-10-26 09:24:50 -04:00
|
|
|
timediff_t left = 1000;
|
2017-10-25 05:59:43 -04:00
|
|
|
struct curltime now = Curl_now();
|
2010-06-02 17:33:51 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = ssh_statemach_act(data, &block);
|
2011-01-05 18:19:17 -05:00
|
|
|
if(result)
|
|
|
|
break;
|
2008-11-24 08:59:51 -05:00
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
if(Curl_pgrsUpdate(data))
|
2020-05-26 18:59:33 -04:00
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
2017-03-10 08:28:37 -05:00
|
|
|
|
2020-05-26 18:59:33 -04:00
|
|
|
result = Curl_speedcheck(data, now);
|
|
|
|
if(result)
|
|
|
|
break;
|
2010-06-02 17:33:51 -04:00
|
|
|
|
2020-05-26 18:59:33 -04:00
|
|
|
left = Curl_timeleft(data, NULL, duringconnect);
|
|
|
|
if(left < 0) {
|
|
|
|
failf(data, "Operation timed out");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
2010-06-02 17:33:51 -04:00
|
|
|
}
|
|
|
|
|
2019-09-19 04:25:15 -04:00
|
|
|
if(block) {
|
2008-11-24 08:59:51 -05:00
|
|
|
int dir = libssh2_session_block_directions(sshc->ssh_session);
|
|
|
|
curl_socket_t sock = conn->sock[FIRSTSOCKET];
|
|
|
|
curl_socket_t fd_read = CURL_SOCKET_BAD;
|
|
|
|
curl_socket_t fd_write = CURL_SOCKET_BAD;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(LIBSSH2_SESSION_BLOCK_INBOUND & dir)
|
2008-11-24 08:59:51 -05:00
|
|
|
fd_read = sock;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(LIBSSH2_SESSION_BLOCK_OUTBOUND & dir)
|
2008-11-24 08:59:51 -05:00
|
|
|
fd_write = sock;
|
|
|
|
/* wait for the socket to become ready */
|
2016-12-20 08:46:47 -05:00
|
|
|
(void)Curl_socket_check(fd_read, CURL_SOCKET_BAD, fd_write,
|
2020-05-28 18:08:03 -04:00
|
|
|
left>1000?1000:left);
|
2008-11-24 08:59:51 -05:00
|
|
|
}
|
|
|
|
}
|
2007-06-12 08:31:10 -04:00
|
|
|
|
2007-07-10 18:26:32 -04:00
|
|
|
return result;
|
2007-06-12 08:31:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SSH setup and connection
|
|
|
|
*/
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode ssh_setup_connection(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
2006-11-24 17:14:39 -05:00
|
|
|
struct SSHPROTO *ssh;
|
2021-01-08 11:58:15 -05:00
|
|
|
(void)conn;
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
data->req.p.ssh = ssh = calloc(1, sizeof(struct SSHPROTO));
|
2007-11-05 04:30:45 -05:00
|
|
|
if(!ssh)
|
2006-11-02 16:56:40 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static Curl_recv scp_recv, sftp_recv;
|
|
|
|
static Curl_send scp_send, sftp_send;
|
|
|
|
|
2020-10-26 00:56:49 -04:00
|
|
|
#ifndef CURL_DISABLE_PROXY
|
2020-09-28 08:02:27 -04:00
|
|
|
static ssize_t ssh_tls_recv(libssh2_socket_t sock, void *buffer,
|
|
|
|
size_t length, int flags, void **abstract)
|
|
|
|
{
|
2021-01-18 05:56:50 -05:00
|
|
|
struct Curl_easy *data = (struct Curl_easy *)*abstract;
|
2020-09-28 08:02:27 -04:00
|
|
|
ssize_t nread;
|
|
|
|
CURLcode result;
|
2021-01-18 05:56:50 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-10-26 06:10:49 -04:00
|
|
|
Curl_recv *backup = conn->recv[0];
|
|
|
|
struct ssh_conn *ssh = &conn->proto.sshc;
|
2020-09-28 08:02:27 -04:00
|
|
|
(void)flags;
|
|
|
|
|
2020-10-26 06:10:49 -04:00
|
|
|
/* swap in the TLS reader function for this call only, and then swap back
|
|
|
|
the SSH one again */
|
|
|
|
conn->recv[0] = ssh->tls_recv;
|
2021-01-18 05:56:50 -05:00
|
|
|
result = Curl_read(data, sock, buffer, length, &nread);
|
2020-10-26 06:10:49 -04:00
|
|
|
conn->recv[0] = backup;
|
2020-09-28 08:02:27 -04:00
|
|
|
if(result == CURLE_AGAIN)
|
|
|
|
return -EAGAIN; /* magic return code for libssh2 */
|
|
|
|
else if(result)
|
|
|
|
return -1; /* generic error */
|
2021-01-18 05:56:50 -05:00
|
|
|
Curl_debug(data, CURLINFO_DATA_IN, (char *)buffer, (size_t)nread);
|
2020-09-28 08:02:27 -04:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ssh_tls_send(libssh2_socket_t sock, const void *buffer,
|
|
|
|
size_t length, int flags, void **abstract)
|
|
|
|
{
|
2021-01-18 05:56:50 -05:00
|
|
|
struct Curl_easy *data = (struct Curl_easy *)*abstract;
|
2020-09-28 08:02:27 -04:00
|
|
|
ssize_t nwrite;
|
|
|
|
CURLcode result;
|
2021-01-18 05:56:50 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2020-10-26 06:10:49 -04:00
|
|
|
Curl_send *backup = conn->send[0];
|
|
|
|
struct ssh_conn *ssh = &conn->proto.sshc;
|
2020-09-28 08:02:27 -04:00
|
|
|
(void)flags;
|
|
|
|
|
2020-10-26 06:10:49 -04:00
|
|
|
/* swap in the TLS writer function for this call only, and then swap back
|
|
|
|
the SSH one again */
|
|
|
|
conn->send[0] = ssh->tls_send;
|
2021-01-18 05:56:50 -05:00
|
|
|
result = Curl_write(data, sock, buffer, length, &nwrite);
|
2020-10-26 06:10:49 -04:00
|
|
|
conn->send[0] = backup;
|
2020-09-28 08:02:27 -04:00
|
|
|
if(result == CURLE_AGAIN)
|
|
|
|
return -EAGAIN; /* magic return code for libssh2 */
|
|
|
|
else if(result)
|
|
|
|
return -1; /* error */
|
2021-01-18 05:56:50 -05:00
|
|
|
Curl_debug(data, CURLINFO_DATA_OUT, (char *)buffer, (size_t)nwrite);
|
2020-09-28 08:02:27 -04:00
|
|
|
return nwrite;
|
|
|
|
}
|
2020-10-26 00:56:49 -04:00
|
|
|
#endif
|
2020-09-28 08:02:27 -04:00
|
|
|
|
2006-11-02 16:56:40 -05:00
|
|
|
/*
|
2006-11-24 17:14:39 -05:00
|
|
|
* Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to
|
2006-11-02 16:56:40 -05:00
|
|
|
* do protocol-specific actions at connect-time.
|
|
|
|
*/
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode ssh_connect(struct Curl_easy *data, bool *done)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
2008-02-18 08:05:46 -05:00
|
|
|
#ifdef CURL_LIBSSH2_DEBUG
|
2006-11-02 16:56:40 -05:00
|
|
|
curl_socket_t sock;
|
2008-02-18 08:05:46 -05:00
|
|
|
#endif
|
2021-01-25 04:44:30 -05:00
|
|
|
struct SSHPROTO *sshp = data->req.p.ssh;
|
|
|
|
struct ssh_conn *sshc;
|
2006-11-02 16:56:40 -05:00
|
|
|
CURLcode result;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2013-10-21 12:47:54 -04:00
|
|
|
/* initialize per-handle data if not already */
|
2021-01-25 04:44:30 -05:00
|
|
|
if(!sshp) {
|
|
|
|
result = ssh_setup_connection(data, conn);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
sshp = data->req.p.ssh;
|
|
|
|
}
|
2013-10-21 12:47:54 -04:00
|
|
|
|
2007-11-05 04:30:45 -05:00
|
|
|
/* We default to persistent connections. We set this already in this connect
|
|
|
|
function to make the re-use checks properly be able to check this bit. */
|
2014-05-20 04:32:23 -04:00
|
|
|
connkeep(conn, "SSH default");
|
2007-11-05 04:30:45 -05:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
sshc = &conn->proto.sshc;
|
2006-11-02 16:56:40 -05:00
|
|
|
|
|
|
|
#ifdef CURL_LIBSSH2_DEBUG
|
2007-11-12 04:24:05 -05:00
|
|
|
if(conn->user) {
|
|
|
|
infof(data, "User: %s\n", conn->user);
|
2006-11-02 16:56:40 -05:00
|
|
|
}
|
2007-11-12 04:24:05 -05:00
|
|
|
if(conn->passwd) {
|
|
|
|
infof(data, "Password: %s\n", conn->passwd);
|
2006-11-02 16:56:40 -05:00
|
|
|
}
|
|
|
|
sock = conn->sock[FIRSTSOCKET];
|
2008-02-18 08:05:46 -05:00
|
|
|
#endif /* CURL_LIBSSH2_DEBUG */
|
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
sshc->ssh_session = libssh2_session_init_ex(my_libssh2_malloc,
|
|
|
|
my_libssh2_free,
|
|
|
|
my_libssh2_realloc, data);
|
|
|
|
if(sshc->ssh_session == NULL) {
|
2007-04-08 18:44:21 -04:00
|
|
|
failf(data, "Failure initialising ssh session");
|
2006-11-02 16:56:40 -05:00
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
}
|
2007-06-11 00:07:51 -04:00
|
|
|
|
2020-10-26 00:56:49 -04:00
|
|
|
#ifndef CURL_DISABLE_PROXY
|
2020-09-28 08:02:27 -04:00
|
|
|
if(conn->http_proxy.proxytype == CURLPROXY_HTTPS) {
|
|
|
|
/*
|
|
|
|
* This crazy union dance is here to avoid assigning a void pointer a
|
|
|
|
* function pointer as it is invalid C. The problem is of course that
|
|
|
|
* libssh2 has such an API...
|
|
|
|
*/
|
|
|
|
union receive {
|
|
|
|
void *recvp;
|
|
|
|
ssize_t (*recvptr)(libssh2_socket_t, void *, size_t, int, void **);
|
|
|
|
};
|
|
|
|
union transfer {
|
|
|
|
void *sendp;
|
|
|
|
ssize_t (*sendptr)(libssh2_socket_t, const void *, size_t, int, void **);
|
|
|
|
};
|
|
|
|
union receive sshrecv;
|
|
|
|
union transfer sshsend;
|
|
|
|
|
|
|
|
sshrecv.recvptr = ssh_tls_recv;
|
|
|
|
sshsend.sendptr = ssh_tls_send;
|
|
|
|
|
|
|
|
infof(data, "Uses HTTPS proxy!\n");
|
|
|
|
/*
|
|
|
|
Setup libssh2 callbacks to make it read/write TLS from the socket.
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
recvcb(libssh2_socket_t sock, void *buffer, size_t length,
|
|
|
|
int flags, void **abstract);
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
sendcb(libssh2_socket_t sock, const void *buffer, size_t length,
|
|
|
|
int flags, void **abstract);
|
|
|
|
|
|
|
|
*/
|
2021-01-25 04:44:30 -05:00
|
|
|
libssh2_session_callback_set(sshc->ssh_session,
|
2020-09-28 08:02:27 -04:00
|
|
|
LIBSSH2_CALLBACK_RECV, sshrecv.recvp);
|
2021-01-25 04:44:30 -05:00
|
|
|
libssh2_session_callback_set(sshc->ssh_session,
|
2020-09-28 08:02:27 -04:00
|
|
|
LIBSSH2_CALLBACK_SEND, sshsend.sendp);
|
2020-10-26 06:10:49 -04:00
|
|
|
|
|
|
|
/* Store the underlying TLS recv/send function pointers to be used when
|
|
|
|
reading from the proxy */
|
2021-01-25 04:44:30 -05:00
|
|
|
sshc->tls_recv = conn->recv[FIRSTSOCKET];
|
|
|
|
sshc->tls_send = conn->send[FIRSTSOCKET];
|
2020-09-28 08:02:27 -04:00
|
|
|
}
|
2020-10-26 06:10:49 -04:00
|
|
|
|
2020-10-26 00:56:49 -04:00
|
|
|
#endif /* CURL_DISABLE_PROXY */
|
|
|
|
if(conn->handler->protocol & CURLPROTO_SCP) {
|
2020-09-28 08:02:27 -04:00
|
|
|
conn->recv[FIRSTSOCKET] = scp_recv;
|
|
|
|
conn->send[FIRSTSOCKET] = scp_send;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
conn->recv[FIRSTSOCKET] = sftp_recv;
|
|
|
|
conn->send[FIRSTSOCKET] = sftp_send;
|
|
|
|
}
|
|
|
|
|
2017-08-05 05:26:04 -04:00
|
|
|
if(data->set.ssh_compression) {
|
|
|
|
#if LIBSSH2_VERSION_NUM >= 0x010208
|
2021-01-25 04:44:30 -05:00
|
|
|
if(libssh2_session_flag(sshc->ssh_session, LIBSSH2_FLAG_COMPRESS, 1) < 0)
|
2017-08-05 05:26:04 -04:00
|
|
|
#endif
|
|
|
|
infof(data, "Failed to enable compression for ssh session\n");
|
|
|
|
}
|
|
|
|
|
2009-07-22 18:49:01 -04:00
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
|
|
|
if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
|
|
|
|
int rc;
|
2021-01-25 04:44:30 -05:00
|
|
|
sshc->kh = libssh2_knownhost_init(sshc->ssh_session);
|
|
|
|
if(!sshc->kh) {
|
|
|
|
libssh2_session_free(sshc->ssh_session);
|
2021-03-20 19:10:04 -04:00
|
|
|
sshc->ssh_session = NULL;
|
2009-07-22 18:49:01 -04:00
|
|
|
return CURLE_FAILED_INIT;
|
2015-11-24 03:32:42 -05:00
|
|
|
}
|
2009-07-22 18:49:01 -04:00
|
|
|
|
|
|
|
/* read all known hosts from there */
|
2021-01-25 04:44:30 -05:00
|
|
|
rc = libssh2_knownhost_readfile(sshc->kh,
|
2009-07-22 18:49:01 -04:00
|
|
|
data->set.str[STRING_SSH_KNOWNHOSTS],
|
|
|
|
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
|
2011-02-26 04:59:03 -05:00
|
|
|
if(rc < 0)
|
2009-07-22 18:49:01 -04:00
|
|
|
infof(data, "Failed to read known hosts from %s\n",
|
|
|
|
data->set.str[STRING_SSH_KNOWNHOSTS]);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LIBSSH2_KNOWNHOST_API */
|
|
|
|
|
2006-11-02 16:56:40 -05:00
|
|
|
#ifdef CURL_LIBSSH2_DEBUG
|
2021-01-27 05:43:21 -05:00
|
|
|
libssh2_trace(sshc->ssh_session, ~0);
|
2010-02-04 14:44:31 -05:00
|
|
|
infof(data, "SSH socket: %d\n", (int)sock);
|
2006-11-02 16:56:40 -05:00
|
|
|
#endif /* CURL_LIBSSH2_DEBUG */
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_INIT);
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
result = ssh_multi_statemach(data, done);
|
2007-06-12 08:31:10 -04:00
|
|
|
|
|
|
|
return result;
|
2006-11-02 16:56:40 -05:00
|
|
|
}
|
|
|
|
|
2007-07-10 18:26:32 -04:00
|
|
|
/*
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
* scp_perform()
|
|
|
|
*
|
|
|
|
* This is the actual DO function for SCP. Get a file according to
|
|
|
|
* the options previously setup.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static
|
2021-01-08 11:58:15 -05:00
|
|
|
CURLcode scp_perform(struct Curl_easy *data,
|
|
|
|
bool *connected,
|
|
|
|
bool *dophase_done)
|
2007-07-10 18:26:32 -04:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
DEBUGF(infof(data, "DO phase starts\n"));
|
2007-07-10 18:26:32 -04:00
|
|
|
|
|
|
|
*dophase_done = FALSE; /* not done yet */
|
|
|
|
|
|
|
|
/* start the first command in the DO phase */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_TRANS_INIT);
|
2007-07-10 18:26:32 -04:00
|
|
|
|
|
|
|
/* run the state-machine */
|
2021-01-08 11:58:15 -05:00
|
|
|
result = ssh_multi_statemach(data, dophase_done);
|
2013-01-17 06:59:23 -05:00
|
|
|
|
2011-08-19 17:38:45 -04:00
|
|
|
*connected = conn->bits.tcpconnect[FIRSTSOCKET];
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2007-11-05 04:30:45 -05:00
|
|
|
if(*dophase_done) {
|
2021-01-18 05:56:50 -05:00
|
|
|
DEBUGF(infof(data, "DO phase is complete\n"));
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
/* called from multi.c while DOing */
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode scp_doing(struct Curl_easy *data,
|
|
|
|
bool *dophase_done)
|
2007-07-10 18:26:32 -04:00
|
|
|
{
|
|
|
|
CURLcode result;
|
2021-01-08 11:58:15 -05:00
|
|
|
result = ssh_multi_statemach(data, dophase_done);
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2007-11-05 04:30:45 -05:00
|
|
|
if(*dophase_done) {
|
2021-01-08 11:58:15 -05:00
|
|
|
DEBUGF(infof(data, "DO phase is complete\n"));
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-11-08 05:22:49 -05:00
|
|
|
/*
|
|
|
|
* The DO function is generic for both protocols. There was previously two
|
|
|
|
* separate ones but this way means less duplicated code.
|
|
|
|
*/
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode ssh_do(struct Curl_easy *data, bool *done)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
2014-10-23 16:56:35 -04:00
|
|
|
CURLcode result;
|
2007-07-10 18:26:32 -04:00
|
|
|
bool connected = 0;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2013-08-04 13:34:16 -04:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
|
|
|
*done = FALSE; /* default to false */
|
|
|
|
|
2007-11-24 18:16:55 -05:00
|
|
|
data->req.size = -1; /* make sure this is unknown at this point */
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2013-08-04 13:34:16 -04:00
|
|
|
sshc->actualcode = CURLE_OK; /* reset error code */
|
2017-09-09 17:09:06 -04:00
|
|
|
sshc->secondCreateDirs = 0; /* reset the create dir attempt state
|
|
|
|
variable */
|
2013-08-04 13:34:16 -04:00
|
|
|
|
2007-07-10 18:26:32 -04:00
|
|
|
Curl_pgrsSetUploadCounter(data, 0);
|
|
|
|
Curl_pgrsSetDownloadCounter(data, 0);
|
2014-08-29 17:48:03 -04:00
|
|
|
Curl_pgrsSetUploadSize(data, -1);
|
|
|
|
Curl_pgrsSetDownloadSize(data, -1);
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2011-03-14 17:52:14 -04:00
|
|
|
if(conn->handler->protocol & CURLPROTO_SCP)
|
2021-01-08 11:58:15 -05:00
|
|
|
result = scp_perform(data, &connected, done);
|
2007-11-08 05:22:49 -05:00
|
|
|
else
|
2021-01-08 11:58:15 -05:00
|
|
|
result = sftp_perform(data, &connected, done);
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
return result;
|
2006-11-02 16:56:40 -05:00
|
|
|
}
|
|
|
|
|
2010-02-20 16:41:25 -05:00
|
|
|
/* BLOCKING, but the function is using the state machine so the only reason
|
|
|
|
this is still blocking is that the multi interface code has no support for
|
2007-11-08 05:22:49 -05:00
|
|
|
disconnecting operations that takes a while */
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode scp_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn,
|
|
|
|
bool dead_connection)
|
2007-11-08 05:22:49 -05:00
|
|
|
{
|
2008-05-30 21:37:21 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-25 04:44:30 -05:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2010-11-19 07:43:20 -05:00
|
|
|
(void) dead_connection;
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
if(sshc->ssh_session) {
|
2008-05-28 16:31:28 -04:00
|
|
|
/* only if there's a session still around to use! */
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SESSION_DISCONNECT);
|
2007-11-08 05:22:49 -05:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
result = ssh_block_statemach(data, conn, FALSE);
|
2008-05-28 16:31:28 -04:00
|
|
|
}
|
2007-11-08 05:22:49 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-05-28 16:31:28 -04:00
|
|
|
/* generic done function for both SCP and SFTP called from their specific
|
|
|
|
done functions */
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode ssh_done(struct Curl_easy *data, CURLcode status)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
2007-07-10 18:26:32 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-25 04:44:30 -05:00
|
|
|
struct SSHPROTO *sshp = data->req.p.ssh;
|
|
|
|
struct connectdata *conn = data->conn;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!status) {
|
2019-05-14 10:36:15 -04:00
|
|
|
/* run the state-machine */
|
2021-01-25 04:44:30 -05:00
|
|
|
result = ssh_block_statemach(data, conn, FALSE);
|
2007-11-05 04:30:45 -05:00
|
|
|
}
|
2008-12-17 07:32:41 -05:00
|
|
|
else
|
2007-07-10 18:26:32 -04:00
|
|
|
result = status;
|
2008-05-28 16:31:28 -04:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
Curl_safefree(sshp->path);
|
|
|
|
Curl_safefree(sshp->readdir_filename);
|
|
|
|
Curl_safefree(sshp->readdir_longentry);
|
|
|
|
Curl_dyn_free(&sshp->readdir);
|
|
|
|
|
2021-01-18 05:56:50 -05:00
|
|
|
if(Curl_pgrsDone(data))
|
2012-06-10 17:39:04 -04:00
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
data->req.keepon = 0; /* clear all bits */
|
2007-07-10 18:26:32 -04:00
|
|
|
return result;
|
2008-05-28 16:31:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode scp_done(struct Curl_easy *data, CURLcode status,
|
2008-05-28 16:31:28 -04:00
|
|
|
bool premature)
|
|
|
|
{
|
|
|
|
(void)premature; /* not used */
|
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!status)
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SCP_DONE);
|
2008-05-28 16:31:28 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
return ssh_done(data, status);
|
2006-11-02 16:56:40 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static ssize_t scp_send(struct Curl_easy *data, int sockindex,
|
2010-05-07 09:05:34 -04:00
|
|
|
const void *mem, size_t len, CURLcode *err)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
|
|
|
ssize_t nwrite;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2021-01-25 04:44:30 -05:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2007-11-05 04:30:45 -05:00
|
|
|
(void)sockindex; /* we only support SCP on the fixed known primary socket */
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2007-11-05 04:30:45 -05:00
|
|
|
/* libssh2_channel_write() returns int! */
|
2021-01-25 04:44:30 -05:00
|
|
|
nwrite = (ssize_t) libssh2_channel_write(sshc->ssh_channel, mem, len);
|
2008-12-19 16:14:52 -05:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
ssh_block2waitfor(data, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
|
2008-12-19 16:14:52 -05:00
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
if(nwrite == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
nwrite = 0;
|
|
|
|
}
|
2012-11-12 04:04:44 -05:00
|
|
|
else if(nwrite < LIBSSH2_ERROR_NONE) {
|
2012-11-20 14:57:18 -05:00
|
|
|
*err = libssh2_session_error_to_CURLE((int)nwrite);
|
2012-11-12 04:04:44 -05:00
|
|
|
nwrite = -1;
|
|
|
|
}
|
2007-11-05 04:30:45 -05:00
|
|
|
|
2006-11-02 16:56:40 -05:00
|
|
|
return nwrite;
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static ssize_t scp_recv(struct Curl_easy *data, int sockindex,
|
2010-05-07 09:05:34 -04:00
|
|
|
char *mem, size_t len, CURLcode *err)
|
2006-11-02 16:56:40 -05:00
|
|
|
{
|
|
|
|
ssize_t nread;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2021-01-25 04:44:30 -05:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2007-02-02 10:26:57 -05:00
|
|
|
(void)sockindex; /* we only support SCP on the fixed known primary socket */
|
2006-11-02 16:56:40 -05:00
|
|
|
|
2007-11-05 04:30:45 -05:00
|
|
|
/* libssh2_channel_read() returns int */
|
2021-01-25 04:44:30 -05:00
|
|
|
nread = (ssize_t) libssh2_channel_read(sshc->ssh_channel, mem, len);
|
2008-12-19 16:14:52 -05:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
ssh_block2waitfor(data, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(nread == LIBSSH2_ERROR_EAGAIN) {
|
2010-05-07 09:05:34 -04:00
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
nread = -1;
|
|
|
|
}
|
2008-12-19 16:14:52 -05:00
|
|
|
|
2006-11-24 17:14:39 -05:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* =============== SFTP ===============
|
|
|
|
*/
|
|
|
|
|
2007-07-10 18:26:32 -04:00
|
|
|
/*
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
* sftp_perform()
|
|
|
|
*
|
|
|
|
* This is the actual DO function for SFTP. Get a file/directory according to
|
|
|
|
* the options previously setup.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static
|
2021-01-08 11:58:15 -05:00
|
|
|
CURLcode sftp_perform(struct Curl_easy *data,
|
2007-07-10 18:26:32 -04:00
|
|
|
bool *connected,
|
|
|
|
bool *dophase_done)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
DEBUGF(infof(data, "DO phase starts\n"));
|
2007-07-10 18:26:32 -04:00
|
|
|
|
|
|
|
*dophase_done = FALSE; /* not done yet */
|
|
|
|
|
|
|
|
/* start the first command in the DO phase */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_QUOTE_INIT);
|
2007-07-10 18:26:32 -04:00
|
|
|
|
|
|
|
/* run the state-machine */
|
2021-01-08 11:58:15 -05:00
|
|
|
result = ssh_multi_statemach(data, dophase_done);
|
2013-01-17 06:59:23 -05:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
*connected = data->conn->bits.tcpconnect[FIRSTSOCKET];
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2007-11-05 04:30:45 -05:00
|
|
|
if(*dophase_done) {
|
2021-01-08 11:58:15 -05:00
|
|
|
DEBUGF(infof(data, "DO phase is complete\n"));
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
/* called from multi.c while DOing */
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode sftp_doing(struct Curl_easy *data,
|
2007-11-08 05:22:49 -05:00
|
|
|
bool *dophase_done)
|
2007-07-10 18:26:32 -04:00
|
|
|
{
|
2021-01-08 11:58:15 -05:00
|
|
|
CURLcode result = ssh_multi_statemach(data, dophase_done);
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2007-11-05 04:30:45 -05:00
|
|
|
if(*dophase_done) {
|
2021-01-08 11:58:15 -05:00
|
|
|
DEBUGF(infof(data, "DO phase is complete\n"));
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-02-20 16:41:25 -05:00
|
|
|
/* BLOCKING, but the function is using the state machine so the only reason
|
|
|
|
this is still blocking is that the multi interface code has no support for
|
2007-11-08 05:22:49 -05:00
|
|
|
disconnecting operations that takes a while */
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode sftp_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, bool dead_connection)
|
2006-11-24 17:14:39 -05:00
|
|
|
{
|
2008-05-30 21:37:21 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2021-01-25 04:44:30 -05:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2010-11-19 07:43:20 -05:00
|
|
|
(void) dead_connection;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
DEBUGF(infof(data, "SSH DISCONNECT starts now\n"));
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
if(sshc->ssh_session) {
|
2008-05-28 16:31:28 -04:00
|
|
|
/* only if there's a session still around to use! */
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_SHUTDOWN);
|
2021-01-25 04:44:30 -05:00
|
|
|
result = ssh_block_statemach(data, conn, FALSE);
|
2008-05-28 16:31:28 -04:00
|
|
|
}
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
DEBUGF(infof(data, "SSH DISCONNECT is done\n"));
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2007-11-08 05:22:49 -05:00
|
|
|
return result;
|
2007-07-10 18:26:32 -04:00
|
|
|
|
2006-11-24 17:14:39 -05:00
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static CURLcode sftp_done(struct Curl_easy *data, CURLcode status,
|
2007-10-12 09:36:37 -04:00
|
|
|
bool premature)
|
2006-11-24 17:14:39 -05:00
|
|
|
{
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2007-07-10 18:26:32 -04:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
|
2014-10-23 16:56:35 -04:00
|
|
|
if(!status) {
|
2011-04-20 17:37:29 -04:00
|
|
|
/* Post quote commands are executed after the SFTP_CLOSE state to avoid
|
|
|
|
errors that could happen due to open file handles during POSTQUOTE
|
|
|
|
operation */
|
2021-01-18 05:56:50 -05:00
|
|
|
if(!premature && data->set.postquote && !conn->bits.retry)
|
2011-04-20 17:37:29 -04:00
|
|
|
sshc->nextstate = SSH_SFTP_POSTQUOTE_INIT;
|
2021-01-18 05:56:50 -05:00
|
|
|
state(data, SSH_SFTP_CLOSE);
|
2007-07-10 18:26:32 -04:00
|
|
|
}
|
2021-01-08 11:58:15 -05:00
|
|
|
return ssh_done(data, status);
|
2006-11-24 17:14:39 -05:00
|
|
|
}
|
|
|
|
|
2007-11-05 04:30:45 -05:00
|
|
|
/* return number of sent bytes */
|
2021-01-08 11:58:15 -05:00
|
|
|
static ssize_t sftp_send(struct Curl_easy *data, int sockindex,
|
2010-05-07 09:05:34 -04:00
|
|
|
const void *mem, size_t len, CURLcode *err)
|
2006-11-24 17:14:39 -05:00
|
|
|
{
|
2021-01-08 11:58:15 -05:00
|
|
|
ssize_t nwrite;
|
|
|
|
struct connectdata *conn = data->conn;
|
2021-01-25 04:44:30 -05:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2007-11-05 04:30:45 -05:00
|
|
|
(void)sockindex;
|
2006-11-24 17:14:39 -05:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
nwrite = libssh2_sftp_write(sshc->sftp_handle, mem, len);
|
2008-12-19 16:14:52 -05:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
ssh_block2waitfor(data, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
|
2008-12-19 16:14:52 -05:00
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
if(nwrite == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
nwrite = 0;
|
|
|
|
}
|
2012-11-12 04:04:44 -05:00
|
|
|
else if(nwrite < LIBSSH2_ERROR_NONE) {
|
2012-11-20 14:57:18 -05:00
|
|
|
*err = libssh2_session_error_to_CURLE((int)nwrite);
|
2012-11-12 04:04:44 -05:00
|
|
|
nwrite = -1;
|
|
|
|
}
|
2007-08-03 04:14:51 -04:00
|
|
|
|
2006-11-24 17:14:39 -05:00
|
|
|
return nwrite;
|
|
|
|
}
|
|
|
|
|
2007-06-12 17:32:45 -04:00
|
|
|
/*
|
2007-11-05 04:30:45 -05:00
|
|
|
* Return number of received (decrypted) bytes
|
2014-03-06 06:48:33 -05:00
|
|
|
* or <0 on error
|
2007-06-12 17:32:45 -04:00
|
|
|
*/
|
2021-01-08 11:58:15 -05:00
|
|
|
static ssize_t sftp_recv(struct Curl_easy *data, int sockindex,
|
2010-05-07 09:05:34 -04:00
|
|
|
char *mem, size_t len, CURLcode *err)
|
2007-06-12 17:32:45 -04:00
|
|
|
{
|
|
|
|
ssize_t nread;
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2021-01-25 04:44:30 -05:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2007-06-12 17:32:45 -04:00
|
|
|
(void)sockindex;
|
2007-06-13 08:15:23 -04:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
nread = libssh2_sftp_read(sshc->sftp_handle, mem, len);
|
2007-08-03 04:14:51 -04:00
|
|
|
|
2021-01-25 04:44:30 -05:00
|
|
|
ssh_block2waitfor(data, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
|
2008-12-19 16:14:52 -05:00
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
if(nread == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
nread = -1;
|
2014-03-06 06:48:33 -05:00
|
|
|
|
2014-03-06 07:39:22 -05:00
|
|
|
}
|
|
|
|
else if(nread < 0) {
|
2014-03-06 08:25:59 -05:00
|
|
|
*err = libssh2_session_error_to_CURLE((int)nread);
|
2010-05-07 09:05:34 -04:00
|
|
|
}
|
2007-06-12 17:32:45 -04:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2020-06-06 18:30:50 -04:00
|
|
|
static const char *sftp_libssh2_strerror(unsigned long err)
|
2007-04-02 17:24:05 -04:00
|
|
|
{
|
2016-12-13 17:34:59 -05:00
|
|
|
switch(err) {
|
2007-07-10 18:26:32 -04:00
|
|
|
case LIBSSH2_FX_NO_SUCH_FILE:
|
|
|
|
return "No such file or directory";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_PERMISSION_DENIED:
|
|
|
|
return "Permission denied";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_FAILURE:
|
|
|
|
return "Operation failed";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_BAD_MESSAGE:
|
|
|
|
return "Bad message from SFTP server";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_NO_CONNECTION:
|
|
|
|
return "Not connected to SFTP server";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_CONNECTION_LOST:
|
|
|
|
return "Connection to SFTP server lost";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_OP_UNSUPPORTED:
|
|
|
|
return "Operation not supported by SFTP server";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_INVALID_HANDLE:
|
|
|
|
return "Invalid handle";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_NO_SUCH_PATH:
|
|
|
|
return "No such file or directory";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_FILE_ALREADY_EXISTS:
|
|
|
|
return "File already exists";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_WRITE_PROTECT:
|
|
|
|
return "File is write protected";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_NO_MEDIA:
|
|
|
|
return "No media";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM:
|
|
|
|
return "Disk full";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_QUOTA_EXCEEDED:
|
|
|
|
return "User quota exceeded";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_UNKNOWN_PRINCIPLE:
|
|
|
|
return "Unknown principle";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_LOCK_CONFlICT:
|
|
|
|
return "File lock conflict";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_DIR_NOT_EMPTY:
|
|
|
|
return "Directory not empty";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_NOT_A_DIRECTORY:
|
|
|
|
return "Not a directory";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_INVALID_FILENAME:
|
|
|
|
return "Invalid filename";
|
|
|
|
|
|
|
|
case LIBSSH2_FX_LINK_LOOP:
|
|
|
|
return "Link points to itself";
|
2007-04-02 17:24:05 -04:00
|
|
|
}
|
|
|
|
return "Unknown error in libssh2";
|
|
|
|
}
|
|
|
|
|
2019-08-16 10:16:33 -04:00
|
|
|
CURLcode Curl_ssh_init(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBSSH2_INIT
|
|
|
|
if(libssh2_init(0)) {
|
|
|
|
DEBUGF(fprintf(stderr, "Error: libssh2_init failed\n"));
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_ssh_cleanup(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBSSH2_EXIT
|
|
|
|
(void)libssh2_exit();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:32:05 -04:00
|
|
|
size_t Curl_ssh_version(char *buffer, size_t buflen)
|
|
|
|
{
|
|
|
|
return msnprintf(buffer, buflen, "libssh2/%s", LIBSSH2_VERSION);
|
|
|
|
}
|
2019-08-16 10:16:33 -04:00
|
|
|
|
2006-11-02 16:56:40 -05:00
|
|
|
#endif /* USE_LIBSSH2 */
|