2006-11-03 00:56:40 +03:00
|
|
|
/***************************************************************************
|
2007-07-11 02:26:32 +04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2012-03-09 00:47:54 +04:00
|
|
|
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2007-07-11 02: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
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* 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-03 00:56:40 +03:00
|
|
|
|
2007-01-28 15:35:39 +03:00
|
|
|
/* #define CURL_LIBSSH2_DEBUG */
|
2006-11-03 00:56:40 +03:00
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
|
|
|
|
#ifdef USE_LIBSSH2
|
2011-07-25 07:30:14 +04:00
|
|
|
|
2009-04-14 17:50:32 +04:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
# include <limits.h>
|
|
|
|
#endif
|
2006-11-03 00:56:40 +03: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 20:59:56 +03:00
|
|
|
#ifdef __VMS
|
2006-11-03 00:56:40 +03: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>
|
|
|
|
#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"
|
|
|
|
|
|
|
|
#include "strequal.h"
|
|
|
|
#include "sslgen.h"
|
|
|
|
#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 */
|
2008-09-29 17:21:21 +04:00
|
|
|
#include "strtoofft.h"
|
2006-11-03 00:56:40 +03:00
|
|
|
#include "multiif.h"
|
2008-11-24 16:59:51 +03:00
|
|
|
#include "select.h"
|
2010-12-04 07:53:07 +03:00
|
|
|
#include "warnless.h"
|
2006-11-03 00:56:40 +03:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
2009-04-21 15:46:16 +04:00
|
|
|
#include "curl_memory.h"
|
2006-11-03 00:56:40 +03:00
|
|
|
/* The last #include file should be: */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2011-08-19 23:25:39 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
# undef PATH_MAX
|
|
|
|
# define PATH_MAX MAX_PATH
|
|
|
|
#endif
|
|
|
|
|
2007-06-19 15:50:08 +04:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 1024 /* just an extra precaution since there are systems that
|
|
|
|
have their definition hidden well */
|
|
|
|
#endif
|
|
|
|
|
2012-03-25 19:51:59 +04:00
|
|
|
#define sftp_libssh2_last_error(s) curlx_ultosi(libssh2_sftp_last_error(s))
|
|
|
|
|
2012-03-22 05:40:19 +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)
|
|
|
|
|
2007-04-03 01:24:05 +04:00
|
|
|
/* Local functions: */
|
2012-03-25 19:51:59 +04:00
|
|
|
static const char *sftp_libssh2_strerror(int err);
|
2010-10-21 01: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-03 00:56:40 +03:00
|
|
|
|
2008-02-18 16:05:46 +03:00
|
|
|
static CURLcode get_pathname(const char **cpp, char **path);
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
static CURLcode ssh_connect(struct connectdata *conn, bool *done);
|
|
|
|
static CURLcode ssh_multi_statemach(struct connectdata *conn, bool *done);
|
|
|
|
static CURLcode ssh_do(struct connectdata *conn, bool *done);
|
|
|
|
|
|
|
|
static CURLcode ssh_getworkingpath(struct connectdata *conn,
|
|
|
|
char *homedir, /* when SFTP is used */
|
|
|
|
char **path);
|
|
|
|
|
|
|
|
static CURLcode scp_done(struct connectdata *conn,
|
|
|
|
CURLcode, bool premature);
|
|
|
|
static CURLcode scp_doing(struct connectdata *conn,
|
|
|
|
bool *dophase_done);
|
2010-11-19 15:43:20 +03:00
|
|
|
static CURLcode scp_disconnect(struct connectdata *conn, bool dead_connection);
|
2007-11-08 13:22:49 +03:00
|
|
|
|
|
|
|
static CURLcode sftp_done(struct connectdata *conn,
|
|
|
|
CURLcode, bool premature);
|
|
|
|
static CURLcode sftp_doing(struct connectdata *conn,
|
|
|
|
bool *dophase_done);
|
2011-04-20 17:17:42 +04:00
|
|
|
static CURLcode sftp_disconnect(struct connectdata *conn, bool dead);
|
2007-11-08 13:22:49 +03:00
|
|
|
static
|
|
|
|
CURLcode sftp_perform(struct connectdata *conn,
|
|
|
|
bool *connected,
|
|
|
|
bool *dophase_done);
|
2008-12-20 00:14:52 +03:00
|
|
|
|
|
|
|
static int ssh_getsock(struct connectdata *conn,
|
|
|
|
curl_socket_t *sock, /* points to numsocks number
|
|
|
|
of sockets */
|
|
|
|
int numsocks);
|
|
|
|
|
|
|
|
static int ssh_perform_getsock(const struct connectdata *conn,
|
|
|
|
curl_socket_t *sock, /* points to numsocks
|
|
|
|
number of sockets */
|
|
|
|
int numsocks);
|
|
|
|
|
2007-10-12 17:36:37 +04:00
|
|
|
/*
|
|
|
|
* SCP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_scp = {
|
|
|
|
"SCP", /* scheme */
|
2007-10-17 20:58:32 +04:00
|
|
|
ZERO_NULL, /* setup_connection */
|
2007-11-08 13:22:49 +03:00
|
|
|
ssh_do, /* do_it */
|
|
|
|
scp_done, /* done */
|
2007-10-17 20:58:32 +04:00
|
|
|
ZERO_NULL, /* do_more */
|
2007-11-08 13:22:49 +03:00
|
|
|
ssh_connect, /* connect_it */
|
|
|
|
ssh_multi_statemach, /* connecting */
|
|
|
|
scp_doing, /* doing */
|
2008-12-20 00:14:52 +03:00
|
|
|
ssh_getsock, /* proto_getsock */
|
|
|
|
ssh_getsock, /* doing_getsock */
|
2011-10-22 01:36:54 +04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2008-12-20 00:14:52 +03:00
|
|
|
ssh_perform_getsock, /* perform_getsock */
|
2007-11-08 13:22:49 +03:00
|
|
|
scp_disconnect, /* disconnect */
|
2011-05-05 18:27:03 +04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2007-10-12 17:36:37 +04:00
|
|
|
PORT_SSH, /* defport */
|
2011-03-15 00:52:14 +03:00
|
|
|
CURLPROTO_SCP, /* protocol */
|
2011-11-25 02:28:54 +04:00
|
|
|
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
|
|
|
|
| PROTOPT_NOURLQUERY /* flags */
|
2007-10-12 17:36:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SFTP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_sftp = {
|
|
|
|
"SFTP", /* scheme */
|
2007-10-17 20:58:32 +04:00
|
|
|
ZERO_NULL, /* setup_connection */
|
2007-11-08 13:22:49 +03:00
|
|
|
ssh_do, /* do_it */
|
|
|
|
sftp_done, /* done */
|
2007-10-17 20:58:32 +04:00
|
|
|
ZERO_NULL, /* do_more */
|
2007-11-08 13:22:49 +03:00
|
|
|
ssh_connect, /* connect_it */
|
|
|
|
ssh_multi_statemach, /* connecting */
|
|
|
|
sftp_doing, /* doing */
|
2008-12-20 00:14:52 +03:00
|
|
|
ssh_getsock, /* proto_getsock */
|
|
|
|
ssh_getsock, /* doing_getsock */
|
2011-10-22 01:36:54 +04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2008-12-20 00:14:52 +03:00
|
|
|
ssh_perform_getsock, /* perform_getsock */
|
2007-11-08 13:22:49 +03:00
|
|
|
sftp_disconnect, /* disconnect */
|
2011-05-05 18:27:03 +04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2007-10-12 17:36:37 +04:00
|
|
|
PORT_SSH, /* defport */
|
2011-03-15 00:52:14 +03:00
|
|
|
CURLPROTO_SFTP, /* protocol */
|
2011-11-25 02:28:54 +04:00
|
|
|
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION
|
|
|
|
| PROTOPT_NOURLQUERY /* flags */
|
2007-10-12 17:36:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-11-03 00:56:40 +03: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)
|
|
|
|
{
|
2007-11-08 13:22:49 +03:00
|
|
|
struct connectdata *conn = (struct connectdata *)*abstract;
|
2006-11-25 01:14:39 +03:00
|
|
|
|
2006-11-03 00:56:40 +03: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-29 01:36:23 +03:00
|
|
|
#else
|
|
|
|
(void)name;
|
|
|
|
(void)name_len;
|
|
|
|
(void)instruction;
|
|
|
|
(void)instruction_len;
|
2006-11-03 00:56:40 +03:00
|
|
|
#endif /* CURL_LIBSSH2_DEBUG */
|
2007-11-05 12:30:45 +03:00
|
|
|
if(num_prompts == 1) {
|
2007-11-08 13:22:49 +03:00
|
|
|
responses[0].text = strdup(conn->passwd);
|
2012-03-20 21:28:24 +04:00
|
|
|
responses[0].length = curlx_uztoui(strlen(conn->passwd));
|
2006-11-03 00:56:40 +03:00
|
|
|
}
|
|
|
|
(void)prompts;
|
|
|
|
(void)abstract;
|
|
|
|
} /* kbd_callback */
|
|
|
|
|
2009-09-03 01:05:47 +04:00
|
|
|
static CURLcode sftp_libssh2_error_to_CURLE(int err)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
2007-08-31 03:03:59 +04:00
|
|
|
switch (err) {
|
|
|
|
case LIBSSH2_FX_OK:
|
|
|
|
return CURLE_OK;
|
2006-11-03 00:56:40 +03:00
|
|
|
|
2007-08-31 03: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-03 00:56:40 +03:00
|
|
|
|
2007-08-31 03:03:59 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2007-04-09 02:44:21 +04:00
|
|
|
|
|
|
|
return CURLE_SSH;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode libssh2_session_error_to_CURLE(int err)
|
|
|
|
{
|
2010-01-26 15:25:03 +03:00
|
|
|
switch (err) {
|
|
|
|
/* Ordered by order of appearance in libssh2.h */
|
|
|
|
case LIBSSH2_ERROR_NONE:
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
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-31 03:03:59 +04:00
|
|
|
|
|
|
|
/* TODO: map some more of the libssh2 errors to the more appropriate CURLcode
|
|
|
|
error code, and possibly add a few new SSH-related one. We must however
|
|
|
|
not return or even depend on libssh2 errors in the public libcurl API */
|
|
|
|
|
2006-11-03 00:56:40 +03:00
|
|
|
return CURLE_SSH;
|
|
|
|
}
|
|
|
|
|
2010-10-21 01:29:37 +04:00
|
|
|
static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
2008-02-19 20:25:19 +03:00
|
|
|
(void)abstract; /* arg not used */
|
2006-11-03 00:56:40 +03:00
|
|
|
return malloc(count);
|
|
|
|
}
|
|
|
|
|
2010-10-21 01:29:37 +04:00
|
|
|
static LIBSSH2_REALLOC_FUNC(my_libssh2_realloc)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
2008-02-19 20:25:19 +03:00
|
|
|
(void)abstract; /* arg not used */
|
2006-11-03 00:56:40 +03:00
|
|
|
return realloc(ptr, count);
|
|
|
|
}
|
|
|
|
|
2010-10-21 01:29:37 +04:00
|
|
|
static LIBSSH2_FREE_FUNC(my_libssh2_free)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
2008-02-19 20:25:19 +03:00
|
|
|
(void)abstract; /* arg not used */
|
2012-03-09 20:24:42 +04:00
|
|
|
if(ptr) /* ssh2 agent sometimes call free with null ptr */
|
|
|
|
free(ptr);
|
2006-11-03 00:56:40 +03:00
|
|
|
}
|
|
|
|
|
2007-06-12 16:31:10 +04:00
|
|
|
/*
|
2007-06-13 16:15:23 +04:00
|
|
|
* SSH State machine related code
|
2007-06-12 16:31:10 +04:00
|
|
|
*/
|
|
|
|
/* This is the ONLY way to change SSH state! */
|
2007-09-29 01:45:05 +04:00
|
|
|
static void state(struct connectdata *conn, sshstate nowstate)
|
2007-06-12 16:31:10 +04:00
|
|
|
{
|
2009-06-10 06:49:42 +04:00
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
2007-06-12 16:31:10 +04:00
|
|
|
/* for debug purposes */
|
2007-07-21 06:08:17 +04:00
|
|
|
static const char * const names[] = {
|
2007-07-11 02:26:32 +04:00
|
|
|
"SSH_STOP",
|
2011-05-02 01:01:31 +04:00
|
|
|
"SSH_INIT",
|
2007-06-12 16:31:10 +04:00
|
|
|
"SSH_S_STARTUP",
|
2009-07-23 02:49:01 +04:00
|
|
|
"SSH_HOSTKEY",
|
2007-06-12 16:31:10 +04:00
|
|
|
"SSH_AUTHLIST",
|
|
|
|
"SSH_AUTH_PKEY_INIT",
|
|
|
|
"SSH_AUTH_PKEY",
|
|
|
|
"SSH_AUTH_PASS_INIT",
|
|
|
|
"SSH_AUTH_PASS",
|
2012-03-09 20:24:42 +04:00
|
|
|
"SSH_AUTH_AGENT_INIT",
|
|
|
|
"SSH_AUTH_AGENT_LIST",
|
|
|
|
"SSH_AUTH_AGENT",
|
2007-06-12 16:31:10 +04:00
|
|
|
"SSH_AUTH_HOST_INIT",
|
|
|
|
"SSH_AUTH_HOST",
|
|
|
|
"SSH_AUTH_KEY_INIT",
|
|
|
|
"SSH_AUTH_KEY",
|
|
|
|
"SSH_AUTH_DONE",
|
|
|
|
"SSH_SFTP_INIT",
|
|
|
|
"SSH_SFTP_REALPATH",
|
2007-07-11 02: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",
|
|
|
|
"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-13 01:32:45 +04:00
|
|
|
"SSH_SFTP_SHUTDOWN",
|
2007-07-11 02:26:32 +04:00
|
|
|
"SSH_SCP_TRANS_INIT",
|
|
|
|
"SSH_SCP_UPLOAD_INIT",
|
|
|
|
"SSH_SCP_DOWNLOAD_INIT",
|
|
|
|
"SSH_SCP_DONE",
|
|
|
|
"SSH_SCP_SEND_EOF",
|
|
|
|
"SSH_SCP_WAIT_EOF",
|
|
|
|
"SSH_SCP_WAIT_CLOSE",
|
|
|
|
"SSH_SCP_CHANNEL_FREE",
|
2007-11-08 13:22:49 +03:00
|
|
|
"SSH_SESSION_DISCONNECT",
|
2007-06-13 01:32:45 +04:00
|
|
|
"SSH_SESSION_FREE",
|
2007-06-12 16:31:10 +04:00
|
|
|
"QUIT"
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2009-06-10 06:49:42 +04:00
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
2007-11-05 12:30:45 +03:00
|
|
|
if(sshc->state != nowstate) {
|
2007-07-11 02:26:32 +04:00
|
|
|
infof(conn->data, "SFTP %p state change from %s to %s\n",
|
2007-09-29 01:45:05 +04:00
|
|
|
sshc, names[sshc->state], names[nowstate]);
|
2007-06-12 16:31:10 +04:00
|
|
|
}
|
|
|
|
#endif
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2007-09-29 01:45:05 +04:00
|
|
|
sshc->state = nowstate;
|
2007-06-12 16:31:10 +04:00
|
|
|
}
|
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
/* figure out the path to work with in this particular request */
|
|
|
|
static CURLcode ssh_getworkingpath(struct connectdata *conn,
|
|
|
|
char *homedir, /* when SFTP is used */
|
|
|
|
char **path) /* returns the allocated
|
|
|
|
real path to work with */
|
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
2007-11-08 19:43:01 +03:00
|
|
|
char *real_path = NULL;
|
2007-11-08 13:22:49 +03:00
|
|
|
char *working_path;
|
|
|
|
int working_path_len;
|
|
|
|
|
2007-11-25 02:16:55 +03:00
|
|
|
working_path = curl_easy_unescape(data, data->state.path, 0,
|
2007-11-08 13:22:49 +03:00
|
|
|
&working_path_len);
|
|
|
|
if(!working_path)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Check for /~/ , indicating relative to the user's home directory */
|
2011-03-15 00:52:14 +03:00
|
|
|
if(conn->handler->protocol & CURLPROTO_SCP) {
|
2008-09-06 09:29:05 +04:00
|
|
|
real_path = malloc(working_path_len+1);
|
2007-11-08 13:22:49 +03:00
|
|
|
if(real_path == NULL) {
|
|
|
|
free(working_path);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
if((working_path_len > 1) && (working_path[1] == '~'))
|
|
|
|
/* It is referenced to the home directory, so strip the leading '/' */
|
|
|
|
memcpy(real_path, working_path+1, 1 + working_path_len-1);
|
|
|
|
else
|
|
|
|
memcpy(real_path, working_path, 1 + working_path_len);
|
|
|
|
}
|
2011-03-15 00:52:14 +03:00
|
|
|
else if(conn->handler->protocol & CURLPROTO_SFTP) {
|
2007-11-08 13:22:49 +03:00
|
|
|
if((working_path_len > 1) && (working_path[1] == '~')) {
|
|
|
|
size_t homelen = strlen(homedir);
|
2008-09-06 09:29:05 +04:00
|
|
|
real_path = malloc(homelen + working_path_len + 1);
|
2007-11-08 13:22:49 +03:00
|
|
|
if(real_path == NULL) {
|
|
|
|
free(working_path);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
/* It is referenced to the home directory, so strip the
|
|
|
|
leading '/' */
|
|
|
|
memcpy(real_path, homedir, homelen);
|
|
|
|
real_path[homelen] = '/';
|
|
|
|
real_path[homelen+1] = '\0';
|
|
|
|
if(working_path_len > 3) {
|
|
|
|
memcpy(real_path+homelen+1, working_path + 3,
|
|
|
|
1 + working_path_len -3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-09-06 09:29:05 +04:00
|
|
|
real_path = malloc(working_path_len+1);
|
2007-11-08 13:22:49 +03:00
|
|
|
if(real_path == NULL) {
|
|
|
|
free(working_path);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(real_path, working_path, 1+working_path_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(working_path);
|
|
|
|
|
|
|
|
/* store the pointer for the caller to receive */
|
|
|
|
*path = real_path;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2009-07-23 06:15:00 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
2009-07-23 02:49:01 +04:00
|
|
|
static int sshkeycallback(CURL *easy,
|
|
|
|
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-23 06:15:00 +04:00
|
|
|
#endif
|
2009-07-23 02:49:01 +04:00
|
|
|
|
2008-12-22 16:12:36 +03:00
|
|
|
/*
|
|
|
|
* Earlier libssh2 versions didn't have the ability to seek to 64bit positions
|
|
|
|
* with 32bit size_t.
|
|
|
|
*/
|
2008-12-22 16:21:23 +03:00
|
|
|
#ifdef HAVE_LIBSSH2_SFTP_SEEK64
|
|
|
|
#define SFTP_SEEK(x,y) libssh2_sftp_seek64(x, (libssh2_uint64_t)y)
|
2008-12-22 16:12:36 +03:00
|
|
|
#else
|
2009-09-17 19:06:34 +04:00
|
|
|
#define SFTP_SEEK(x,y) libssh2_sftp_seek(x, (size_t)y)
|
2008-12-22 16:12:36 +03:00
|
|
|
#endif
|
|
|
|
|
2010-08-04 19: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 18:27:39 +04:00
|
|
|
#ifndef HAVE_LIBSSH2_SCP_SEND64
|
2010-08-04 19: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-21 01: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
|
|
|
|
|
2011-04-21 00:43:41 +04:00
|
|
|
static CURLcode ssh_knownhost(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
2011-05-23 14:57:23 +04:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
|
2011-04-21 00:43:41 +04:00
|
|
|
if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
|
|
|
|
/* we're asked to verify the host against a file */
|
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
int rc;
|
|
|
|
int keytype;
|
|
|
|
size_t keylen;
|
|
|
|
const char *remotekey = libssh2_session_hostkey(sshc->ssh_session,
|
|
|
|
&keylen, &keytype);
|
2011-05-23 21:37:41 +04:00
|
|
|
int keycheck = LIBSSH2_KNOWNHOST_CHECK_FAILURE;
|
|
|
|
int keybit = 0;
|
2011-04-21 00: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?
|
|
|
|
*/
|
|
|
|
struct libssh2_knownhost *host;
|
|
|
|
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;
|
|
|
|
|
|
|
|
keybit = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)?
|
|
|
|
LIBSSH2_KNOWNHOST_KEY_SSHRSA:LIBSSH2_KNOWNHOST_KEY_SSHDSS;
|
|
|
|
|
|
|
|
keycheck = libssh2_knownhost_check(sshc->kh,
|
|
|
|
conn->host.name,
|
|
|
|
remotekey, keylen,
|
|
|
|
LIBSSH2_KNOWNHOST_TYPE_PLAIN|
|
|
|
|
LIBSSH2_KNOWNHOST_KEYENC_RAW|
|
|
|
|
keybit,
|
|
|
|
&host);
|
|
|
|
|
|
|
|
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)?
|
|
|
|
CURLKHTYPE_RSA : CURLKHTYPE_DSS;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 */
|
|
|
|
rc = func(data, knownkeyp, /* from the knownhosts file */
|
|
|
|
&foundkey, /* from the remote host */
|
|
|
|
keymatch, data->set.ssh_keyfunc_userp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* no remotekey means failure! */
|
|
|
|
rc = CURLKHSTAT_REJECT;
|
|
|
|
|
|
|
|
switch(rc) {
|
|
|
|
default: /* unknown return codes will equal reject */
|
|
|
|
case CURLKHSTAT_REJECT:
|
|
|
|
state(conn, SSH_SESSION_FREE);
|
|
|
|
case CURLKHSTAT_DEFER:
|
|
|
|
/* DEFER means bail out but keep the SSH_HOSTKEY state */
|
|
|
|
result = sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
break;
|
|
|
|
case CURLKHSTAT_FINE:
|
|
|
|
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);
|
|
|
|
else if(rc == CURLKHSTAT_FINE_ADD_TO_FILE) {
|
|
|
|
/* 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 */
|
|
|
|
(void)conn;
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-09-14 16:48:55 +04:00
|
|
|
static CURLcode ssh_check_fingerprint(struct connectdata *conn)
|
2012-09-12 18:06:18 +04:00
|
|
|
{
|
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
const char *pubkey_md5 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5];
|
|
|
|
char md5buffer[33];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
const char *fingerprint = libssh2_hostkey_hash(sshc->ssh_session,
|
|
|
|
LIBSSH2_HOSTKEY_HASH_MD5);
|
|
|
|
|
2012-09-12 18:18:36 +04:00
|
|
|
if(fingerprint) {
|
|
|
|
/* The fingerprint points to static storage (!), don't free() it. */
|
|
|
|
for(i = 0; i < 16; i++)
|
|
|
|
snprintf(&md5buffer[i*2], 3, "%02x", (unsigned char) fingerprint[i]);
|
|
|
|
infof(data, "SSH MD5 fingerprint: %s\n", md5buffer);
|
|
|
|
}
|
2012-09-12 18: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) {
|
2012-09-12 18:18:36 +04:00
|
|
|
if(!fingerprint || !strequal(md5buffer, pubkey_md5)) {
|
|
|
|
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");
|
2012-09-12 18:06:18 +04:00
|
|
|
state(conn, SSH_SESSION_FREE);
|
|
|
|
sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
return sshc->actualcode;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
infof(data, "MD5 checksum match!\n");
|
|
|
|
/* as we already matched, we skip the check for known hosts */
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return ssh_knownhost(conn);
|
|
|
|
}
|
2011-04-21 00:43:41 +04:00
|
|
|
|
2008-11-24 16:59:51 +03:00
|
|
|
/*
|
2010-01-12 02:10:53 +03: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 16:59:51 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block)
|
2007-06-12 16:31:10 +04:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2007-07-11 02:26:32 +04:00
|
|
|
struct SessionHandle *data = conn->data;
|
2007-11-25 02:16:55 +03:00
|
|
|
struct SSHPROTO *sftp_scp = data->state.proto.ssh;
|
2007-06-12 16:31:10 +04:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
curl_socket_t sock = conn->sock[FIRSTSOCKET];
|
2011-08-31 20:15:29 +04:00
|
|
|
char *new_readdir_line;
|
2012-09-12 18:06:18 +04:00
|
|
|
int rc = LIBSSH2_ERROR_NONE;
|
2007-11-08 13:22:49 +03:00
|
|
|
int err;
|
2009-04-28 15:19:10 +04:00
|
|
|
int seekerr = CURL_SEEKFUNC_OK;
|
2008-11-24 16:59:51 +03:00
|
|
|
*block = 0; /* we're not blocking by default */
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
do {
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
switch(sshc->state) {
|
2011-05-02 01:01:31 +04:00
|
|
|
case SSH_INIT:
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->secondCreateDirs = 0;
|
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
sshc->actualcode = CURLE_OK;
|
|
|
|
|
2011-05-02 01:01:31 +04:00
|
|
|
/* Set libssh2 to non-blocking, since everything internally is
|
|
|
|
non-blocking */
|
|
|
|
libssh2_session_set_blocking(sshc->ssh_session, 0);
|
|
|
|
|
|
|
|
state(conn, SSH_S_STARTUP);
|
|
|
|
/* fall-through */
|
|
|
|
|
|
|
|
case SSH_S_STARTUP:
|
2012-01-13 15:57:09 +04:00
|
|
|
rc = libssh2_session_startup(sshc->ssh_session, (int)sock);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc) {
|
|
|
|
failf(data, "Failure establishing ssh session");
|
|
|
|
state(conn, SSH_SESSION_FREE);
|
|
|
|
sshc->actualcode = CURLE_FAILED_INIT;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_HOSTKEY);
|
2009-07-23 02:49:01 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* fall-through */
|
|
|
|
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-06 01:20:48 +04:00
|
|
|
* whatever) is up to us.
|
2010-01-12 02:10:53 +03:00
|
|
|
*/
|
2012-09-12 18:06:18 +04:00
|
|
|
result = ssh_check_fingerprint(conn);
|
2012-09-14 16:48:55 +04:00
|
|
|
if(result == CURLE_OK)
|
2011-04-21 00:43:41 +04:00
|
|
|
state(conn, SSH_AUTHLIST);
|
2010-01-12 02:10:53 +03:00
|
|
|
break;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03: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 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(conn->user)));
|
2010-01-12 02:10:53 +03:00
|
|
|
|
|
|
|
if(!sshc->authlist) {
|
|
|
|
if((err = libssh2_session_last_errno(sshc->ssh_session)) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_SESSION_FREE);
|
|
|
|
sshc->actualcode = libssh2_session_error_to_CURLE(err);
|
|
|
|
break;
|
|
|
|
}
|
2007-06-12 16:31:10 +04:00
|
|
|
}
|
2010-02-21 00:41:25 +03:00
|
|
|
infof(data, "SSH authentication methods available: %s\n",
|
|
|
|
sshc->authlist);
|
2007-06-12 16:31:10 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_AUTH_PKEY_INIT);
|
|
|
|
break;
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03: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 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if((data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) &&
|
|
|
|
(strstr(sshc->authlist, "publickey") != NULL)) {
|
2012-03-25 20:10:09 +04:00
|
|
|
char *home = NULL;
|
2012-04-01 22:22:37 +04:00
|
|
|
bool rsa_pub_empty_but_ok = FALSE;
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->rsa_pub = sshc->rsa = NULL;
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* To ponder about: should really the lib be messing about with the
|
|
|
|
HOME environment variable etc? */
|
|
|
|
home = curl_getenv("HOME");
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2012-04-01 22:22:37 +04:00
|
|
|
if(data->set.str[STRING_SSH_PUBLIC_KEY] &&
|
|
|
|
!*data->set.str[STRING_SSH_PUBLIC_KEY])
|
|
|
|
rsa_pub_empty_but_ok = true;
|
|
|
|
else if(data->set.str[STRING_SSH_PUBLIC_KEY])
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->rsa_pub = aprintf("%s", data->set.str[STRING_SSH_PUBLIC_KEY]);
|
|
|
|
else if(home)
|
|
|
|
sshc->rsa_pub = aprintf("%s/.ssh/id_dsa.pub", home);
|
|
|
|
else
|
|
|
|
/* as a final resort, try current dir! */
|
|
|
|
sshc->rsa_pub = strdup("id_dsa.pub");
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2012-04-01 22:22:37 +04:00
|
|
|
if(!rsa_pub_empty_but_ok && (sshc->rsa_pub == NULL)) {
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(home);
|
|
|
|
state(conn, SSH_SESSION_FREE);
|
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-19 15:31:33 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(data->set.str[STRING_SSH_PRIVATE_KEY])
|
|
|
|
sshc->rsa = aprintf("%s", data->set.str[STRING_SSH_PRIVATE_KEY]);
|
|
|
|
else if(home)
|
|
|
|
sshc->rsa = aprintf("%s/.ssh/id_dsa", home);
|
|
|
|
else
|
|
|
|
/* as a final resort, try current dir! */
|
|
|
|
sshc->rsa = strdup("id_dsa");
|
|
|
|
|
|
|
|
if(sshc->rsa == NULL) {
|
|
|
|
Curl_safefree(home);
|
|
|
|
Curl_safefree(sshc->rsa_pub);
|
|
|
|
state(conn, SSH_SESSION_FREE);
|
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sshc->passphrase = data->set.str[STRING_KEY_PASSWD];
|
|
|
|
if(!sshc->passphrase)
|
|
|
|
sshc->passphrase = "";
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
Curl_safefree(home);
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
infof(data, "Using ssh public key file %s\n", sshc->rsa_pub);
|
|
|
|
infof(data, "Using ssh private key file %s\n", sshc->rsa);
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_AUTH_PKEY);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_AUTH_PASS_INIT);
|
|
|
|
}
|
2007-06-12 16:31:10 +04:00
|
|
|
break;
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_AUTH_PKEY:
|
|
|
|
/* The function below checks if the files exists, no need to stat() here.
|
|
|
|
*/
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_userauth_publickey_fromfile_ex(sshc->ssh_session,
|
|
|
|
conn->user,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(
|
|
|
|
strlen(conn->user)),
|
2010-03-28 10:45:17 +04:00
|
|
|
sshc->rsa_pub,
|
|
|
|
sshc->rsa, sshc->passphrase);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->rsa_pub);
|
|
|
|
Curl_safefree(sshc->rsa);
|
2007-06-12 16:31:10 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == 0) {
|
|
|
|
sshc->authed = TRUE;
|
|
|
|
infof(data, "Initialized SSH public key authentication\n");
|
|
|
|
state(conn, SSH_AUTH_DONE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *err_msg;
|
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
infof(data, "SSH public key authentication failed: %s\n", err_msg);
|
|
|
|
state(conn, SSH_AUTH_PASS_INIT);
|
|
|
|
}
|
2007-06-12 16:31:10 +04:00
|
|
|
break;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_AUTH_PASS_INIT:
|
|
|
|
if((data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD) &&
|
|
|
|
(strstr(sshc->authlist, "password") != NULL)) {
|
|
|
|
state(conn, SSH_AUTH_PASS);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_AUTH_HOST_INIT);
|
|
|
|
}
|
2007-06-12 16:31:10 +04:00
|
|
|
break;
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_AUTH_PASS:
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_userauth_password_ex(sshc->ssh_session, conn->user,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(conn->user)),
|
2010-03-28 10:45:17 +04:00
|
|
|
conn->passwd,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(conn->passwd)),
|
2010-03-28 10:45:17 +04:00
|
|
|
NULL);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc == 0) {
|
|
|
|
sshc->authed = TRUE;
|
|
|
|
infof(data, "Initialized password authentication\n");
|
|
|
|
state(conn, SSH_AUTH_DONE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_AUTH_HOST_INIT);
|
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_AUTH_HOST_INIT:
|
|
|
|
if((data->set.ssh_auth_types & CURLSSH_AUTH_HOST) &&
|
|
|
|
(strstr(sshc->authlist, "hostbased") != NULL)) {
|
|
|
|
state(conn, SSH_AUTH_HOST);
|
|
|
|
}
|
|
|
|
else {
|
2012-03-09 20:24:42 +04:00
|
|
|
state(conn, SSH_AUTH_AGENT_INIT);
|
2010-01-12 02:10:53 +03:00
|
|
|
}
|
|
|
|
break;
|
2008-07-03 10:56:03 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_AUTH_HOST:
|
2012-03-09 20:24:42 +04:00
|
|
|
state(conn, SSH_AUTH_AGENT_INIT);
|
|
|
|
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");
|
|
|
|
|
|
|
|
state(conn, SSH_AUTH_KEY_INIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = libssh2_agent_connect(sshc->ssh_agent);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN)
|
|
|
|
break;
|
|
|
|
if(rc < 0) {
|
|
|
|
infof(data, "Failure connecting to agent\n");
|
|
|
|
state(conn, SSH_AUTH_KEY_INIT);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_AUTH_AGENT_LIST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* HAVE_LIBSSH2_AGENT_API */
|
|
|
|
state(conn, SSH_AUTH_KEY_INIT);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_AUTH_AGENT_LIST:
|
2012-08-10 10:57:27 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_AGENT_API
|
2012-03-09 20:24:42 +04: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");
|
|
|
|
state(conn, SSH_AUTH_KEY_INIT);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_AUTH_AGENT);
|
|
|
|
sshc->sshagent_prev_identity = NULL;
|
|
|
|
}
|
2012-08-10 10:57:27 +04:00
|
|
|
#endif
|
2012-03-09 20:24:42 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_AUTH_AGENT:
|
2012-08-10 10:57:27 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_AGENT_API
|
2012-03-09 20:24:42 +04: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) {
|
|
|
|
if(rc != LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
/* tried and failed? go to next identity */
|
|
|
|
sshc->sshagent_prev_identity = sshc->sshagent_identity;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
state(conn, SSH_AUTH_DONE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
state(conn, SSH_AUTH_KEY_INIT);
|
2012-08-10 10:57:27 +04:00
|
|
|
#endif
|
2010-01-12 02:10:53 +03:00
|
|
|
break;
|
2007-06-12 20:15:20 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_AUTH_KEY_INIT:
|
|
|
|
if((data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD)
|
|
|
|
&& (strstr(sshc->authlist, "keyboard-interactive") != NULL)) {
|
|
|
|
state(conn, SSH_AUTH_KEY);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_AUTH_DONE);
|
|
|
|
}
|
2007-06-12 16:31:10 +04:00
|
|
|
break;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03: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 21:28:24 +04:00
|
|
|
curlx_uztoui(
|
|
|
|
strlen(conn->user)),
|
2010-01-12 02:10:53 +03:00
|
|
|
&kbd_callback);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-11-05 12:30:45 +03:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
else if(rc == 0) {
|
|
|
|
sshc->authed = TRUE;
|
|
|
|
infof(data, "Initialized keyboard interactive authentication\n");
|
|
|
|
}
|
|
|
|
state(conn, SSH_AUTH_DONE);
|
|
|
|
break;
|
2008-01-22 20:26:42 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_AUTH_DONE:
|
|
|
|
if(!sshc->authed) {
|
|
|
|
failf(data, "Authentication failure");
|
2007-11-08 13:22:49 +03:00
|
|
|
state(conn, SSH_SESSION_FREE);
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = CURLE_LOGIN_DENIED;
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/*
|
|
|
|
* At this point we have an authenticated ssh session.
|
|
|
|
*/
|
|
|
|
infof(data, "Authentication complete\n");
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSH is connected */
|
|
|
|
|
|
|
|
conn->sockfd = sock;
|
|
|
|
conn->writesockfd = CURL_SOCKET_BAD;
|
|
|
|
|
2011-03-15 00:52:14 +03:00
|
|
|
if(conn->handler->protocol == CURLPROTO_SFTP) {
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_INIT);
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
infof(data, "SSH CONNECT phase done\n");
|
2007-11-08 13:22:49 +03:00
|
|
|
state(conn, SSH_STOP);
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_INIT:
|
|
|
|
/*
|
|
|
|
* Start the libssh2 sftp session
|
|
|
|
*/
|
|
|
|
sshc->sftp_session = libssh2_sftp_init(sshc->ssh_session);
|
|
|
|
if(!sshc->sftp_session) {
|
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *err_msg;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
(void)libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0);
|
|
|
|
failf(data, "Failure initializing sftp session: %s", err_msg);
|
|
|
|
state(conn, SSH_SESSION_FREE);
|
|
|
|
sshc->actualcode = CURLE_FAILED_INIT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state(conn, SSH_SFTP_REALPATH);
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_REALPATH:
|
|
|
|
{
|
|
|
|
char tempHome[PATH_MAX];
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
/*
|
2010-01-12 02:10:53 +03:00
|
|
|
* Get the "home" directory
|
2007-11-08 13:22:49 +03:00
|
|
|
*/
|
2012-03-22 05:40:19 +04:00
|
|
|
rc = sftp_libssh2_realpath(sshc->sftp_session, ".",
|
2010-01-12 02:10:53 +03:00
|
|
|
tempHome, PATH_MAX-1);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc > 0) {
|
|
|
|
/* It seems that this string is not always NULL terminated */
|
|
|
|
tempHome[rc] = '\0';
|
|
|
|
sshc->homedir = strdup(tempHome);
|
|
|
|
if(!sshc->homedir) {
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2010-12-30 04:43:45 +03:00
|
|
|
conn->data->state.most_recent_ftp_entrypath = sshc->homedir;
|
2010-01-12 02:10:53 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Return the error type */
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2010-01-12 02:10:53 +03:00
|
|
|
result = sftp_libssh2_error_to_CURLE(err);
|
|
|
|
sshc->actualcode = result?result:CURLE_SSH;
|
2010-02-04 22:44:31 +03:00
|
|
|
DEBUGF(infof(data, "error = %d makes libcurl = %d\n",
|
|
|
|
err, (int)result));
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_STOP);
|
2007-06-13 01:32:45 +04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03: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"));
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
break;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_QUOTE_INIT:
|
|
|
|
|
|
|
|
result = ssh_getworkingpath(conn, sshc->homedir, &sftp_scp->path);
|
2008-02-18 16:05:46 +03:00
|
|
|
if(result) {
|
|
|
|
sshc->actualcode = result;
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_STOP);
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(data->set.quote) {
|
|
|
|
infof(data, "Sending quote commands\n");
|
|
|
|
sshc->quote_item = data->set.quote;
|
|
|
|
state(conn, SSH_SFTP_QUOTE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_SFTP_TRANS_INIT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_POSTQUOTE_INIT:
|
|
|
|
if(data->set.postquote) {
|
|
|
|
infof(data, "Sending quote commands\n");
|
|
|
|
sshc->quote_item = data->set.postquote;
|
|
|
|
state(conn, SSH_SFTP_QUOTE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_QUOTE:
|
|
|
|
/* Send any quote commands */
|
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
/*
|
2010-01-12 02:10:53 +03:00
|
|
|
* Support some of the "FTP" commands
|
2007-11-08 13:22:49 +03:00
|
|
|
*/
|
2011-11-18 18:55:09 +04: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(curl_strequal("pwd", cmd)) {
|
2010-01-12 02:10:53 +03:00
|
|
|
/* output debug output if that is requested */
|
2011-01-03 15:34:13 +03:00
|
|
|
char *tmp = aprintf("257 \"%s\" is current directory.\n",
|
|
|
|
sftp_scp->path);
|
|
|
|
if(!tmp) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2011-01-03 15:34:13 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-01-01 17:35:53 +03:00
|
|
|
if(data->set.verbose) {
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_debug(data, CURLINFO_HEADER_OUT, (char *)"PWD\n", 4, conn);
|
|
|
|
Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp), conn);
|
|
|
|
}
|
2011-01-01 17:35:53 +03: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. */
|
|
|
|
result = Curl_client_write(conn, CLIENTWRITE_HEADER, tmp, strlen(tmp));
|
2011-01-03 15:34:13 +03:00
|
|
|
free(tmp);
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_NEXT_QUOTE);
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(cmd) {
|
2010-01-12 02:10:53 +03: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 18:55:09 +04:00
|
|
|
cp = strchr(cmd, ' ');
|
2010-01-12 02:10:53 +03:00
|
|
|
if(cp == NULL) {
|
|
|
|
failf(data, "Syntax error in SFTP command. Supply parameter(s)!");
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* also, every command takes at least one argument so we get that
|
|
|
|
* first argument right now
|
|
|
|
*/
|
|
|
|
result = get_pathname(&cp, &sshc->quote_path1);
|
2008-02-18 16:05:46 +03:00
|
|
|
if(result) {
|
|
|
|
if(result == CURLE_OUT_OF_MEMORY)
|
2007-11-08 13:22:49 +03:00
|
|
|
failf(data, "Out of memory");
|
|
|
|
else
|
2010-01-12 02:10:53 +03:00
|
|
|
failf(data, "Syntax error: Bad first parameter");
|
2007-07-11 02:26:32 +04:00
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2008-02-18 16:05:46 +03:00
|
|
|
sshc->actualcode = result;
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SFTP is a binary protocol, so we don't send text commands to
|
|
|
|
* the server. Instead, we scan for commands for commands used by
|
|
|
|
* OpenSSH's sftp program and call the appropriate libssh2
|
|
|
|
* functions.
|
|
|
|
*/
|
2011-11-18 18:55:09 +04:00
|
|
|
if(curl_strnequal(cmd, "chgrp ", 6) ||
|
|
|
|
curl_strnequal(cmd, "chmod ", 6) ||
|
|
|
|
curl_strnequal(cmd, "chown ", 6) ) {
|
2010-01-12 02:10:53 +03:00
|
|
|
/* attribute change */
|
|
|
|
|
|
|
|
/* sshc->quote_path1 contains the mode to set */
|
|
|
|
/* get the destination */
|
|
|
|
result = get_pathname(&cp, &sshc->quote_path2);
|
|
|
|
if(result) {
|
|
|
|
if(result == CURLE_OUT_OF_MEMORY)
|
|
|
|
failf(data, "Out of memory");
|
|
|
|
else
|
|
|
|
failf(data, "Syntax error in chgrp/chmod/chown: "
|
|
|
|
"Bad second parameter");
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memset(&sshc->quote_attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
|
|
|
|
state(conn, SSH_SFTP_QUOTE_STAT);
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(curl_strnequal(cmd, "ln ", 3) ||
|
|
|
|
curl_strnequal(cmd, "symlink ", 8)) {
|
2010-01-12 02:10:53 +03:00
|
|
|
/* symbolic linking */
|
|
|
|
/* sshc->quote_path1 is the source */
|
|
|
|
/* get the destination */
|
|
|
|
result = get_pathname(&cp, &sshc->quote_path2);
|
|
|
|
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);
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state(conn, SSH_SFTP_QUOTE_SYMLINK);
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(curl_strnequal(cmd, "mkdir ", 6)) {
|
2010-01-12 02:10:53 +03:00
|
|
|
/* create dir */
|
|
|
|
state(conn, SSH_SFTP_QUOTE_MKDIR);
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(curl_strnequal(cmd, "rename ", 7)) {
|
2010-01-12 02:10:53 +03:00
|
|
|
/* rename file */
|
|
|
|
/* first param is the source path */
|
|
|
|
/* second param is the dest. path */
|
|
|
|
result = get_pathname(&cp, &sshc->quote_path2);
|
|
|
|
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);
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state(conn, SSH_SFTP_QUOTE_RENAME);
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(curl_strnequal(cmd, "rmdir ", 6)) {
|
2010-01-12 02:10:53 +03:00
|
|
|
/* delete dir */
|
|
|
|
state(conn, SSH_SFTP_QUOTE_RMDIR);
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(curl_strnequal(cmd, "rm ", 3)) {
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_QUOTE_UNLINK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
failf(data, "Unknown SFTP command");
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
}
|
|
|
|
if(!sshc->quote_item) {
|
|
|
|
state(conn, SSH_SFTP_TRANS_INIT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_NEXT_QUOTE:
|
2012-03-25 20:10:09 +04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
2010-01-12 02:10:53 +03:00
|
|
|
|
|
|
|
sshc->quote_item = sshc->quote_item->next;
|
|
|
|
|
|
|
|
if(sshc->quote_item) {
|
|
|
|
state(conn, SSH_SFTP_QUOTE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(sshc->nextstate != SSH_NO_STATE) {
|
|
|
|
state(conn, sshc->nextstate);
|
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_SFTP_TRANS_INIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_QUOTE_STAT:
|
2011-11-18 18:55:09 +04: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!curl_strnequal(cmd, "chmod", 5)) {
|
2010-01-12 02:10:53 +03:00
|
|
|
/* Since chown and chgrp only set owner OR group but libssh2 wants to
|
2010-02-21 00:41:25 +03:00
|
|
|
* set them both at once, we need to obtain the current ownership
|
|
|
|
* first. This takes an extra protocol round trip.
|
2010-01-12 02:10:53 +03:00
|
|
|
*/
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path2)),
|
2010-03-28 10:45:17 +04:00
|
|
|
LIBSSH2_SFTP_STAT,
|
|
|
|
&sshc->quote_attrs);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(rc != 0 && !sshc->acceptfail) { /* get those attributes */
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2007-07-11 02:26:32 +04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Attempt to get SFTP stats failed: %s",
|
|
|
|
sftp_libssh2_strerror(err));
|
2007-07-11 02:26:32 +04:00
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
|
|
|
|
/* Now set the new attributes... */
|
2011-11-18 18:55:09 +04:00
|
|
|
if(curl_strnequal(cmd, "chgrp", 5)) {
|
2010-02-24 03:03:06 +03:00
|
|
|
sshc->quote_attrs.gid = strtoul(sshc->quote_path1, NULL, 10);
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
|
2011-11-18 18:55:09 +04:00
|
|
|
if(sshc->quote_attrs.gid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
|
|
|
|
!sshc->acceptfail) {
|
2007-07-11 02:26:32 +04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Syntax error: chgrp gid not a number");
|
2007-07-11 02:26:32 +04:00
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(curl_strnequal(cmd, "chmod", 5)) {
|
2010-02-24 03:03:06 +03:00
|
|
|
sshc->quote_attrs.permissions = strtoul(sshc->quote_path1, NULL, 8);
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_PERMISSIONS;
|
|
|
|
/* permissions are octal */
|
|
|
|
if(sshc->quote_attrs.permissions == 0 &&
|
|
|
|
!ISDIGIT(sshc->quote_path1[0])) {
|
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Syntax error: chmod permissions not a number");
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-13 21:13:44 +04:00
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(curl_strnequal(cmd, "chown", 5)) {
|
2010-02-24 03:03:06 +03:00
|
|
|
sshc->quote_attrs.uid = strtoul(sshc->quote_path1, NULL, 10);
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID;
|
2011-11-18 18:55:09 +04:00
|
|
|
if(sshc->quote_attrs.uid == 0 && !ISDIGIT(sshc->quote_path1[0]) &&
|
|
|
|
!sshc->acceptfail) {
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
|
|
|
failf(data, "Syntax error: chown uid not a number");
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* Now send the completed structure... */
|
|
|
|
state(conn, SSH_SFTP_QUOTE_SETSTAT);
|
2008-03-19 01:59:04 +03:00
|
|
|
break;
|
2011-11-18 18:55:09 +04:00
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_QUOTE_SETSTAT:
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path2)),
|
2010-03-28 10:45:17 +04:00
|
|
|
LIBSSH2_SFTP_SETSTAT,
|
2010-01-12 02:10:53 +03:00
|
|
|
&sshc->quote_attrs);
|
2008-07-11 08:38:11 +04:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
2008-11-24 16:59:51 +03:00
|
|
|
break;
|
2008-07-11 08:38:11 +04:00
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(rc != 0 && !sshc->acceptfail) {
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2008-11-24 16:59:51 +03:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
2010-01-12 02:10:53 +03:00
|
|
|
failf(data, "Attempt to set SFTP stats failed: %s",
|
2008-11-24 16:59:51 +03:00
|
|
|
sftp_libssh2_strerror(err));
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2008-11-24 16:59:51 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
2008-07-11 08:38:11 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_NEXT_QUOTE);
|
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_QUOTE_SYMLINK:
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_symlink_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)),
|
2010-03-28 10:45:17 +04:00
|
|
|
sshc->quote_path2,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path2)),
|
2010-03-28 10:45:17 +04:00
|
|
|
LIBSSH2_SFTP_SYMLINK);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(rc != 0 && !sshc->acceptfail) {
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2007-07-11 02:26:32 +04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
Curl_safefree(sshc->quote_path2);
|
2010-01-12 02:10:53 +03:00
|
|
|
failf(data, "symlink command failed: %s",
|
|
|
|
sftp_libssh2_strerror(err));
|
2007-07-11 02:26:32 +04:00
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2007-11-08 13:22:49 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_NEXT_QUOTE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_QUOTE_MKDIR:
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)),
|
2011-12-24 02:54:13 +04:00
|
|
|
data->set.new_directory_perms);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(rc != 0 && !sshc->acceptfail) {
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2007-07-11 02:26:32 +04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2010-01-12 02:10:53 +03:00
|
|
|
failf(data, "mkdir command failed: %s", sftp_libssh2_strerror(err));
|
2007-07-11 02:26:32 +04:00
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2007-11-08 13:22:49 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_NEXT_QUOTE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_QUOTE_RENAME:
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_rename_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)),
|
2010-03-28 10:45:17 +04:00
|
|
|
sshc->quote_path2,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path2)),
|
2010-03-28 10:45:17 +04:00
|
|
|
LIBSSH2_SFTP_RENAME_OVERWRITE |
|
|
|
|
LIBSSH2_SFTP_RENAME_ATOMIC |
|
|
|
|
LIBSSH2_SFTP_RENAME_NATIVE);
|
2011-04-21 01:37:29 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(rc != 0 && !sshc->acceptfail) {
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2007-07-11 02:26:32 +04:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
2007-11-08 13:22:49 +03:00
|
|
|
Curl_safefree(sshc->quote_path2);
|
2010-01-12 02:10:53 +03:00
|
|
|
failf(data, "rename command failed: %s", sftp_libssh2_strerror(err));
|
2007-07-11 02:26:32 +04:00
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2007-11-08 13:22:49 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_NEXT_QUOTE);
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_QUOTE_RMDIR:
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_rmdir_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)));
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(rc != 0 && !sshc->acceptfail) {
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
failf(data, "rmdir command failed: %s", sftp_libssh2_strerror(err));
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state(conn, SSH_SFTP_NEXT_QUOTE);
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_QUOTE_UNLINK:
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_unlink_ex(sshc->sftp_session, sshc->quote_path1,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->quote_path1)));
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
2011-11-18 18:55:09 +04:00
|
|
|
else if(rc != 0 && !sshc->acceptfail) {
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->quote_path1);
|
|
|
|
failf(data, "rm command failed: %s", sftp_libssh2_strerror(err));
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = CURLE_QUOTE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state(conn, SSH_SFTP_NEXT_QUOTE);
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_TRANS_INIT:
|
|
|
|
if(data->set.upload)
|
|
|
|
state(conn, SSH_SFTP_UPLOAD_INIT);
|
|
|
|
else {
|
|
|
|
if(data->set.opt_no_body)
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
else if(sftp_scp->path[strlen(sftp_scp->path)-1] == '/')
|
|
|
|
state(conn, SSH_SFTP_READDIR_INIT);
|
|
|
|
else
|
|
|
|
state(conn, SSH_SFTP_DOWNLOAD_INIT);
|
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03: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 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(data->state.resume_from != 0) {
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs;
|
2010-12-06 07:20:05 +03:00
|
|
|
if(data->state.resume_from < 0) {
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sftp_scp->path)),
|
2010-03-28 10:45:17 +04:00
|
|
|
LIBSSH2_SFTP_STAT, &attrs);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc) {
|
|
|
|
data->state.resume_from = 0;
|
|
|
|
}
|
|
|
|
else {
|
2010-10-12 02:01:40 +04:00
|
|
|
curl_off_t size = attrs.filesize;
|
|
|
|
if(size < 0) {
|
|
|
|
failf(data, "Bad file size (%" FORMAT_OFF_T ")", size);
|
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
data->state.resume_from = attrs.filesize;
|
|
|
|
}
|
2008-03-04 14:53:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(data->set.ftp_append)
|
|
|
|
/* Try to open for append, but create if nonexisting */
|
|
|
|
flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_APPEND;
|
2011-04-20 17:17:42 +04:00
|
|
|
else if(data->state.resume_from > 0)
|
2010-01-12 02:10:53 +03:00
|
|
|
/* If we have restart position then open for append */
|
|
|
|
flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_APPEND;
|
|
|
|
else
|
|
|
|
/* Clear file before writing (normal behaviour) */
|
|
|
|
flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC;
|
2008-08-01 22:41:14 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->sftp_handle =
|
2010-03-28 10:45:17 +04:00
|
|
|
libssh2_sftp_open_ex(sshc->sftp_session, sftp_scp->path,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sftp_scp->path)),
|
2010-03-28 10:45:17 +04:00
|
|
|
flags, data->set.new_file_perms,
|
|
|
|
LIBSSH2_SFTP_OPENFILE);
|
2008-03-04 14:53:15 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(!sshc->sftp_handle) {
|
|
|
|
rc = libssh2_session_last_errno(sshc->ssh_session);
|
2008-12-16 02:04:51 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(LIBSSH2_ERROR_EAGAIN == rc)
|
|
|
|
break;
|
|
|
|
else {
|
|
|
|
if(LIBSSH2_ERROR_SFTP_PROTOCOL == rc)
|
|
|
|
/* only when there was an SFTP protocol error can we extract
|
|
|
|
the sftp error! */
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2010-01-12 02:10:53 +03:00
|
|
|
else
|
|
|
|
err = -1; /* not an sftp error at all */
|
2008-12-16 02:04:51 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(sshc->secondCreateDirs) {
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
sshc->actualcode = err>= LIBSSH2_FX_OK?
|
|
|
|
sftp_libssh2_error_to_CURLE(err):CURLE_SSH;
|
|
|
|
failf(data, "Creating the dir/file failed: %s",
|
|
|
|
sftp_libssh2_strerror(err));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(((err == LIBSSH2_FX_NO_SUCH_FILE) ||
|
|
|
|
(err == LIBSSH2_FX_FAILURE) ||
|
|
|
|
(err == LIBSSH2_FX_NO_SUCH_PATH)) &&
|
|
|
|
(data->set.ftp_create_missing_dirs &&
|
|
|
|
(strlen(sftp_scp->path) > 1))) {
|
|
|
|
/* try to create the path remotely */
|
|
|
|
sshc->secondCreateDirs = 1;
|
|
|
|
state(conn, SSH_SFTP_CREATE_DIRS_INIT);
|
|
|
|
break;
|
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2008-12-16 02:04:51 +03:00
|
|
|
sshc->actualcode = err>= LIBSSH2_FX_OK?
|
|
|
|
sftp_libssh2_error_to_CURLE(err):CURLE_SSH;
|
2010-01-12 02:10:53 +03:00
|
|
|
if(!sshc->actualcode) {
|
|
|
|
/* Sometimes, for some reason libssh2_sftp_last_error() returns
|
|
|
|
zero even though libssh2_sftp_open() failed previously! We need
|
|
|
|
to work around that! */
|
|
|
|
sshc->actualcode = CURLE_SSH;
|
|
|
|
err=-1;
|
|
|
|
}
|
|
|
|
failf(data, "Upload failed: %s (%d/%d)",
|
|
|
|
err>= LIBSSH2_FX_OK?sftp_libssh2_strerror(err):"ssh error",
|
|
|
|
err, rc);
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* If we have restart point then we need to seek to the correct
|
|
|
|
position. */
|
|
|
|
if(data->state.resume_from > 0) {
|
|
|
|
/* Let's read off the proper amount of bytes from the input. */
|
|
|
|
if(conn->seek_func) {
|
|
|
|
seekerr = conn->seek_func(conn->seek_client, data->state.resume_from,
|
|
|
|
SEEK_SET);
|
|
|
|
}
|
2009-04-28 15:19:10 +04:00
|
|
|
|
2011-07-05 00:10:32 +04:00
|
|
|
if(seekerr != CURL_SEEKFUNC_OK) {
|
2008-03-04 14:53:15 +03:00
|
|
|
|
2010-01-12 02:10:53 +03: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) */
|
|
|
|
else {
|
|
|
|
curl_off_t passed=0;
|
|
|
|
do {
|
2010-12-04 07:53:07 +03:00
|
|
|
size_t readthisamountnow =
|
|
|
|
(data->state.resume_from - passed > CURL_OFF_T_C(BUFSIZE)) ?
|
|
|
|
BUFSIZE : curlx_sotouz(data->state.resume_from - passed);
|
2010-01-12 02:10:53 +03:00
|
|
|
|
2010-12-04 07:53:07 +03:00
|
|
|
size_t actuallyread =
|
|
|
|
conn->fread_func(data->state.buffer, 1, readthisamountnow,
|
|
|
|
conn->fread_in);
|
2010-01-12 02:10:53 +03:00
|
|
|
|
|
|
|
passed += actuallyread;
|
2010-12-04 07:53:07 +03:00
|
|
|
if((actuallyread == 0) || (actuallyread > readthisamountnow)) {
|
2010-01-12 02:10:53 +03:00
|
|
|
/* 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 14:53:15 +03:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
|
|
|
|
/* now, decrease the size of the read */
|
2010-12-06 07:20:05 +03:00
|
|
|
if(data->set.infilesize > 0) {
|
2010-01-12 02:10:53 +03:00
|
|
|
data->set.infilesize -= data->state.resume_from;
|
|
|
|
data->req.size = data->set.infilesize;
|
|
|
|
Curl_pgrsSetUploadSize(data, data->set.infilesize);
|
2009-04-28 15:19:10 +04:00
|
|
|
}
|
2008-03-04 14:53:15 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
SFTP_SEEK(sshc->sftp_handle, data->state.resume_from);
|
|
|
|
}
|
2010-12-06 07:20:05 +03:00
|
|
|
if(data->set.infilesize > 0) {
|
2008-03-04 14:53:15 +03:00
|
|
|
data->req.size = data->set.infilesize;
|
|
|
|
Curl_pgrsSetUploadSize(data, data->set.infilesize);
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
/* upload data */
|
2010-04-17 01:43:04 +04:00
|
|
|
Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
|
2008-03-04 14:53:15 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* not set by Curl_setup_transfer to preserve keepon bits */
|
|
|
|
conn->sockfd = conn->writesockfd;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(result) {
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
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 13:22:49 +03:00
|
|
|
|
2011-03-13 16:10:27 +03: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-13 01:02:04 +03: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 */
|
|
|
|
Curl_expire(data, 1);
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_STOP);
|
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_CREATE_DIRS_INIT:
|
|
|
|
if(strlen(sftp_scp->path) > 1) {
|
|
|
|
sshc->slash_pos = sftp_scp->path + 1; /* ignore the leading '/' */
|
|
|
|
state(conn, SSH_SFTP_CREATE_DIRS);
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
else {
|
|
|
|
state(conn, SSH_SFTP_UPLOAD_INIT);
|
|
|
|
}
|
|
|
|
break;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_CREATE_DIRS:
|
|
|
|
if((sshc->slash_pos = strchr(sshc->slash_pos, '/')) != NULL) {
|
|
|
|
*sshc->slash_pos = 0;
|
|
|
|
|
|
|
|
infof(data, "Creating directory '%s'\n", sftp_scp->path);
|
|
|
|
state(conn, SSH_SFTP_CREATE_DIRS_MKDIR);
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
else {
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_UPLOAD_INIT);
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_CREATE_DIRS_MKDIR:
|
|
|
|
/* 'mode' - parameter is preliminary - default to 0644 */
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sftp_scp->path,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sftp_scp->path)),
|
2010-03-28 10:45:17 +04:00
|
|
|
data->set.new_directory_perms);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*sshc->slash_pos = '/';
|
|
|
|
++sshc->slash_pos;
|
|
|
|
if(rc == -1) {
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
|
|
|
if((err != LIBSSH2_FX_FILE_ALREADY_EXISTS) &&
|
|
|
|
(err != LIBSSH2_FX_FAILURE) &&
|
|
|
|
(err != LIBSSH2_FX_PERMISSION_DENIED)) {
|
|
|
|
result = sftp_libssh2_error_to_CURLE(err);
|
2007-11-08 13:22:49 +03:00
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->actualcode = result?result:CURLE_SSH;
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
}
|
|
|
|
state(conn, SSH_SFTP_CREATE_DIRS);
|
|
|
|
break;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_READDIR_INIT:
|
|
|
|
/*
|
|
|
|
* This is a directory that we are trying to get, so produce a directory
|
|
|
|
* listing
|
|
|
|
*/
|
2010-03-28 10:45:17 +04:00
|
|
|
sshc->sftp_handle = libssh2_sftp_open_ex(sshc->sftp_session,
|
|
|
|
sftp_scp->path,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(
|
|
|
|
strlen(sftp_scp->path)),
|
2010-03-28 10:45:17 +04:00
|
|
|
0, 0, LIBSSH2_SFTP_OPENDIR);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(!sshc->sftp_handle) {
|
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
2007-11-13 02:04:44 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
else {
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2010-01-12 02:10:53 +03:00
|
|
|
failf(data, "Could not open directory for reading: %s",
|
|
|
|
sftp_libssh2_strerror(err));
|
2007-11-08 13:22:49 +03:00
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2010-01-12 02:10:53 +03:00
|
|
|
result = sftp_libssh2_error_to_CURLE(err);
|
|
|
|
sshc->actualcode = result?result:CURLE_SSH;
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
}
|
|
|
|
if((sshc->readdir_filename = malloc(PATH_MAX+1)) == NULL) {
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if((sshc->readdir_longentry = malloc(PATH_MAX+1)) == NULL) {
|
|
|
|
Curl_safefree(sshc->readdir_filename);
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state(conn, SSH_SFTP_READDIR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SFTP_READDIR:
|
|
|
|
sshc->readdir_len = libssh2_sftp_readdir_ex(sshc->sftp_handle,
|
|
|
|
sshc->readdir_filename,
|
|
|
|
PATH_MAX,
|
|
|
|
sshc->readdir_longentry,
|
|
|
|
PATH_MAX,
|
|
|
|
&sshc->readdir_attrs);
|
|
|
|
if(sshc->readdir_len == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(sshc->readdir_len > 0) {
|
|
|
|
sshc->readdir_filename[sshc->readdir_len] = '\0';
|
|
|
|
|
|
|
|
if(data->set.ftp_list_only) {
|
|
|
|
char *tmpLine;
|
|
|
|
|
|
|
|
tmpLine = aprintf("%s\n", sshc->readdir_filename);
|
|
|
|
if(tmpLine == NULL) {
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = Curl_client_write(conn, CLIENTWRITE_BODY,
|
|
|
|
tmpLine, sshc->readdir_len+1);
|
|
|
|
Curl_safefree(tmpLine);
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(result) {
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* since this counts what we send to the client, we include the
|
|
|
|
newline in this counter */
|
|
|
|
data->req.bytecount += sshc->readdir_len+1;
|
|
|
|
|
|
|
|
/* output debug output if that is requested */
|
|
|
|
if(data->set.verbose) {
|
|
|
|
Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_filename,
|
|
|
|
sshc->readdir_len, conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2010-03-28 10:45:17 +04:00
|
|
|
sshc->readdir_currLen = (int)strlen(sshc->readdir_longentry);
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->readdir_totalLen = 80 + sshc->readdir_currLen;
|
|
|
|
sshc->readdir_line = calloc(sshc->readdir_totalLen, 1);
|
|
|
|
if(!sshc->readdir_line) {
|
2007-07-11 02:26:32 +04:00
|
|
|
Curl_safefree(sshc->readdir_filename);
|
|
|
|
Curl_safefree(sshc->readdir_longentry);
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2007-11-08 13:22:49 +03:00
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
memcpy(sshc->readdir_line, sshc->readdir_longentry,
|
|
|
|
sshc->readdir_currLen);
|
|
|
|
if((sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
|
|
|
|
((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
|
|
|
|
LIBSSH2_SFTP_S_IFLNK)) {
|
|
|
|
sshc->readdir_linkPath = malloc(PATH_MAX + 1);
|
|
|
|
if(sshc->readdir_linkPath == NULL) {
|
|
|
|
Curl_safefree(sshc->readdir_filename);
|
|
|
|
Curl_safefree(sshc->readdir_longentry);
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(sshc->readdir_linkPath, PATH_MAX, "%s%s", sftp_scp->path,
|
|
|
|
sshc->readdir_filename);
|
|
|
|
state(conn, SSH_SFTP_READDIR_LINK);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state(conn, SSH_SFTP_READDIR_BOTTOM);
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
}
|
|
|
|
else if(sshc->readdir_len == 0) {
|
|
|
|
Curl_safefree(sshc->readdir_filename);
|
|
|
|
Curl_safefree(sshc->readdir_longentry);
|
|
|
|
state(conn, SSH_SFTP_READDIR_DONE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(sshc->readdir_len <= 0) {
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2010-01-12 02:10:53 +03:00
|
|
|
result = sftp_libssh2_error_to_CURLE(err);
|
|
|
|
sshc->actualcode = result?result:CURLE_SSH;
|
|
|
|
failf(data, "Could not open remote file for reading: %s :: %d",
|
|
|
|
sftp_libssh2_strerror(err),
|
|
|
|
libssh2_session_last_errno(sshc->ssh_session));
|
|
|
|
Curl_safefree(sshc->readdir_filename);
|
|
|
|
Curl_safefree(sshc->readdir_longentry);
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_READDIR_LINK:
|
2010-03-28 10:45:17 +04:00
|
|
|
sshc->readdir_len =
|
|
|
|
libssh2_sftp_symlink_ex(sshc->sftp_session,
|
|
|
|
sshc->readdir_linkPath,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sshc->readdir_linkPath)),
|
2010-03-28 10:45:17 +04:00
|
|
|
sshc->readdir_filename,
|
|
|
|
PATH_MAX, LIBSSH2_SFTP_READLINK);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(sshc->readdir_len == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Curl_safefree(sshc->readdir_linkPath);
|
2011-08-31 20:15:29 +04:00
|
|
|
|
2011-12-28 01:58:03 +04:00
|
|
|
/* get room for the filename and extra output */
|
2012-01-01 02:53:52 +04:00
|
|
|
sshc->readdir_totalLen += 4 + sshc->readdir_len;
|
2011-12-28 01:58:03 +04:00
|
|
|
new_readdir_line = realloc(sshc->readdir_line, sshc->readdir_totalLen);
|
2011-08-31 20:15:29 +04:00
|
|
|
if(!new_readdir_line) {
|
|
|
|
Curl_safefree(sshc->readdir_line);
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->readdir_filename);
|
|
|
|
Curl_safefree(sshc->readdir_longentry);
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
sshc->actualcode = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2011-08-31 20:15:29 +04:00
|
|
|
sshc->readdir_line = new_readdir_line;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->readdir_currLen += snprintf(sshc->readdir_line +
|
|
|
|
sshc->readdir_currLen,
|
|
|
|
sshc->readdir_totalLen -
|
|
|
|
sshc->readdir_currLen,
|
|
|
|
" -> %s",
|
|
|
|
sshc->readdir_filename);
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_READDIR_BOTTOM);
|
|
|
|
break;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_READDIR_BOTTOM:
|
|
|
|
sshc->readdir_currLen += snprintf(sshc->readdir_line +
|
|
|
|
sshc->readdir_currLen,
|
|
|
|
sshc->readdir_totalLen -
|
|
|
|
sshc->readdir_currLen, "\n");
|
|
|
|
result = Curl_client_write(conn, CLIENTWRITE_BODY,
|
|
|
|
sshc->readdir_line,
|
|
|
|
sshc->readdir_currLen);
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(result == CURLE_OK) {
|
2007-11-13 02:04:44 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* output debug output if that is requested */
|
|
|
|
if(data->set.verbose) {
|
|
|
|
Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_line,
|
|
|
|
sshc->readdir_currLen, conn);
|
|
|
|
}
|
|
|
|
data->req.bytecount += sshc->readdir_currLen;
|
2007-11-13 02:04:44 +03:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->readdir_line);
|
|
|
|
if(result) {
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
state(conn, SSH_SFTP_READDIR);
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_READDIR_DONE:
|
|
|
|
if(libssh2_sftp_closedir(sshc->sftp_handle) ==
|
2007-11-08 13:22:49 +03:00
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
2008-11-24 16:59:51 +03:00
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->sftp_handle = NULL;
|
|
|
|
Curl_safefree(sshc->readdir_filename);
|
|
|
|
Curl_safefree(sshc->readdir_longentry);
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* no data to transfer */
|
2010-04-17 01:43:04 +04:00
|
|
|
Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_STOP);
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
2010-01-12 02:10:53 +03:00
|
|
|
|
|
|
|
case SSH_SFTP_DOWNLOAD_INIT:
|
2007-11-08 13:22:49 +03:00
|
|
|
/*
|
2010-01-12 02:10:53 +03:00
|
|
|
* Work on getting the specified file
|
2007-11-08 13:22:49 +03:00
|
|
|
*/
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->sftp_handle =
|
2010-03-28 10:45:17 +04:00
|
|
|
libssh2_sftp_open_ex(sshc->sftp_session, sftp_scp->path,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sftp_scp->path)),
|
2010-03-28 10:45:17 +04:00
|
|
|
LIBSSH2_FXF_READ, data->set.new_file_perms,
|
|
|
|
LIBSSH2_SFTP_OPENFILE);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(!sshc->sftp_handle) {
|
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
2008-09-29 17:21:21 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-03-25 19:51:59 +04:00
|
|
|
err = sftp_libssh2_last_error(sshc->sftp_session);
|
2010-01-12 02:10:53 +03:00
|
|
|
failf(data, "Could not open remote file for reading: %s",
|
|
|
|
sftp_libssh2_strerror(err));
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
result = sftp_libssh2_error_to_CURLE(err);
|
|
|
|
sshc->actualcode = result?result:CURLE_SSH;
|
|
|
|
break;
|
2008-09-29 17:21:21 +04:00
|
|
|
}
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SFTP_DOWNLOAD_STAT);
|
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_DOWNLOAD_STAT:
|
|
|
|
{
|
|
|
|
LIBSSH2_SFTP_ATTRIBUTES attrs;
|
|
|
|
|
2010-03-28 10:45:17 +04:00
|
|
|
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path,
|
2012-03-20 21:28:24 +04:00
|
|
|
curlx_uztoui(strlen(sftp_scp->path)),
|
2010-03-28 10:45:17 +04:00
|
|
|
LIBSSH2_SFTP_STAT, &attrs);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc) {
|
|
|
|
/*
|
|
|
|
* libssh2_sftp_open() didn't return an error, so maybe the server
|
|
|
|
* just doesn't support stat()
|
|
|
|
*/
|
|
|
|
data->req.size = -1;
|
|
|
|
data->req.maxdownload = -1;
|
2008-03-04 14:53:15 +03:00
|
|
|
}
|
|
|
|
else {
|
2010-10-01 01:08:37 +04:00
|
|
|
curl_off_t size = attrs.filesize;
|
2010-01-12 02:10:53 +03:00
|
|
|
|
2010-10-01 01:08:37 +04:00
|
|
|
if(size < 0) {
|
|
|
|
failf(data, "Bad file size (%" FORMAT_OFF_T ")", size);
|
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
if(conn->data->state.use_range) {
|
|
|
|
curl_off_t from, to;
|
|
|
|
char *ptr;
|
|
|
|
char *ptr2;
|
|
|
|
|
|
|
|
from=curlx_strtoofft(conn->data->state.range, &ptr, 0);
|
2010-11-26 15:59:01 +03:00
|
|
|
while(*ptr && (ISSPACE(*ptr) || (*ptr=='-')))
|
2010-01-12 02:10:53 +03:00
|
|
|
ptr++;
|
|
|
|
to=curlx_strtoofft(ptr, &ptr2, 0);
|
|
|
|
if((ptr == ptr2) /* no "to" value given */
|
|
|
|
|| (to >= size)) {
|
|
|
|
to = size - 1;
|
|
|
|
}
|
|
|
|
if(from < 0) {
|
|
|
|
/* from is relative to end of file */
|
|
|
|
from += size;
|
|
|
|
}
|
|
|
|
if(from >= size) {
|
|
|
|
failf(data, "Offset (%"
|
|
|
|
FORMAT_OFF_T ") was beyond file size (%" FORMAT_OFF_T ")",
|
|
|
|
from, attrs.filesize);
|
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
|
|
|
if(from > to) {
|
|
|
|
from = to;
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size = to - from + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SFTP_SEEK(conn->proto.sshc.sftp_handle, from);
|
|
|
|
}
|
|
|
|
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-06 07:20:05 +03:00
|
|
|
if(data->state.resume_from < 0) {
|
2010-01-12 02:10:53 +03:00
|
|
|
/* We're supposed to download the last abs(from) bytes */
|
|
|
|
if((curl_off_t)attrs.filesize < -data->state.resume_from) {
|
|
|
|
failf(data, "Offset (%"
|
|
|
|
FORMAT_OFF_T ") was beyond file size (%" FORMAT_OFF_T ")",
|
|
|
|
data->state.resume_from, attrs.filesize);
|
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
|
|
|
/* download from where? */
|
2010-12-06 07:20:05 +03:00
|
|
|
data->state.resume_from += attrs.filesize;
|
2010-01-12 02:10:53 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if((curl_off_t)attrs.filesize < data->state.resume_from) {
|
|
|
|
failf(data, "Offset (%" FORMAT_OFF_T
|
|
|
|
") was beyond file size (%" FORMAT_OFF_T ")",
|
|
|
|
data->state.resume_from, attrs.filesize);
|
|
|
|
return CURLE_BAD_DOWNLOAD_RESUME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Does a completed file need to be seeked and started or closed ? */
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Setup the actual download */
|
|
|
|
if(data->req.size == 0) {
|
|
|
|
/* no data to transfer */
|
2010-04-17 01:43:04 +04:00
|
|
|
Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
|
2010-01-12 02:10:53 +03:00
|
|
|
infof(data, "File already completely downloaded\n");
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
break;
|
2008-03-04 14:53:15 +03:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
else {
|
2010-04-17 01:43:04 +04:00
|
|
|
Curl_setup_transfer(conn, FIRSTSOCKET, data->req.size,
|
|
|
|
FALSE, NULL, -1, NULL);
|
2009-09-26 12:31:48 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* not set by Curl_setup_transfer to preserve keepon bits */
|
|
|
|
conn->writesockfd = conn->sockfd;
|
2009-09-26 12:31:48 +04:00
|
|
|
|
2011-03-14 01:21:03 +03: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-12 02:10:53 +03:00
|
|
|
conn->cselect_bits = CURL_CSELECT_IN;
|
|
|
|
}
|
|
|
|
if(result) {
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
|
|
|
sshc->actualcode = result;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
}
|
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SFTP_CLOSE:
|
|
|
|
if(sshc->sftp_handle) {
|
|
|
|
rc = libssh2_sftp_close(sshc->sftp_handle);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc < 0) {
|
|
|
|
infof(data, "Failed to close libssh2 file\n");
|
|
|
|
}
|
|
|
|
sshc->sftp_handle = NULL;
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
2012-03-25 20:10:09 +04:00
|
|
|
if(sftp_scp)
|
2011-03-13 02:15:59 +03:00
|
|
|
Curl_safefree(sftp_scp->path);
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
DEBUGF(infof(data, "SFTP DONE done\n"));
|
2011-04-21 01:37:29 +04:00
|
|
|
|
|
|
|
/* Check if nextstate is set and move .nextstate could be POSTQUOTE_INIT
|
|
|
|
After nextstate is executed,the control should come back to
|
|
|
|
SSH_SFTP_CLOSE to pass the correct result back */
|
|
|
|
if(sshc->nextstate != SSH_NO_STATE) {
|
|
|
|
state(conn, sshc->nextstate);
|
|
|
|
sshc->nextstate = SSH_SFTP_CLOSE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
result = sshc->actualcode;
|
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03: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-29 00:31:28 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(sshc->sftp_handle) {
|
|
|
|
rc = libssh2_sftp_close(sshc->sftp_handle);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc < 0) {
|
|
|
|
infof(data, "Failed to close libssh2 file\n");
|
|
|
|
}
|
|
|
|
sshc->sftp_handle = NULL;
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
if(sshc->sftp_session) {
|
|
|
|
rc = libssh2_sftp_shutdown(sshc->sftp_session);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc < 0) {
|
|
|
|
infof(data, "Failed to stop libssh2 sftp subsystem\n");
|
|
|
|
}
|
|
|
|
sshc->sftp_session = NULL;
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->homedir);
|
2010-12-30 04:43:45 +03:00
|
|
|
conn->data->state.most_recent_ftp_entrypath = NULL;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SESSION_DISCONNECT);
|
2007-07-11 02:26:32 +04:00
|
|
|
break;
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SCP_TRANS_INIT:
|
|
|
|
result = ssh_getworkingpath(conn, sshc->homedir, &sftp_scp->path);
|
|
|
|
if(result) {
|
|
|
|
sshc->actualcode = result;
|
|
|
|
state(conn, SSH_STOP);
|
2007-11-08 13:22:49 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(data->set.upload) {
|
|
|
|
if(data->set.infilesize < 0) {
|
|
|
|
failf(data, "SCP requires a known file size for upload");
|
|
|
|
sshc->actualcode = CURLE_UPLOAD_FAILED;
|
|
|
|
state(conn, SSH_SCP_CHANNEL_FREE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state(conn, SSH_SCP_UPLOAD_INIT);
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
else {
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SCP_DOWNLOAD_INIT);
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03: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 =
|
2010-08-04 19:23:38 +04:00
|
|
|
SCP_SEND(sshc->ssh_session, sftp_scp->path, data->set.new_file_perms,
|
|
|
|
data->set.infilesize);
|
2010-01-12 02:10:53 +03:00
|
|
|
if(!sshc->ssh_channel) {
|
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int ssh_err;
|
|
|
|
char *err_msg;
|
|
|
|
|
2010-03-28 10:45:17 +04:00
|
|
|
ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0));
|
2010-01-12 02:10:53 +03:00
|
|
|
failf(conn->data, "%s", err_msg);
|
|
|
|
state(conn, SSH_SCP_CHANNEL_FREE);
|
|
|
|
sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* upload data */
|
2010-04-17 01:43:04 +04:00
|
|
|
Curl_setup_transfer(conn, -1, data->req.size, FALSE, NULL,
|
|
|
|
FIRSTSOCKET, NULL);
|
2009-09-26 12:31:48 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* not set by Curl_setup_transfer to preserve keepon bits */
|
|
|
|
conn->sockfd = conn->writesockfd;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(result) {
|
|
|
|
state(conn, SSH_SCP_CHANNEL_FREE);
|
|
|
|
sshc->actualcode = result;
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
else {
|
2011-03-14 01:21:03 +03: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;
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_STOP);
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SCP_DOWNLOAD_INIT:
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We must check the remote file; if it is a directory no values will
|
|
|
|
* be set in sb
|
|
|
|
*/
|
|
|
|
struct stat sb;
|
|
|
|
curl_off_t bytecount;
|
|
|
|
|
|
|
|
/* clear the struct scp recv will fill in */
|
|
|
|
memset(&sb, 0, sizeof(struct stat));
|
|
|
|
|
|
|
|
/* get a fresh new channel from the ssh layer */
|
|
|
|
sshc->ssh_channel = libssh2_scp_recv(sshc->ssh_session,
|
|
|
|
sftp_scp->path, &sb);
|
|
|
|
if(!sshc->ssh_channel) {
|
|
|
|
if(libssh2_session_last_errno(sshc->ssh_session) ==
|
|
|
|
LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
rc = LIBSSH2_ERROR_EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int ssh_err;
|
|
|
|
char *err_msg;
|
|
|
|
|
2010-03-28 10:45:17 +04:00
|
|
|
ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session,
|
|
|
|
&err_msg, NULL, 0));
|
2010-01-12 02:10:53 +03:00
|
|
|
failf(conn->data, "%s", err_msg);
|
|
|
|
state(conn, SSH_SCP_CHANNEL_FREE);
|
|
|
|
sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-09-26 12:31:48 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* download data */
|
|
|
|
bytecount = (curl_off_t)sb.st_size;
|
|
|
|
data->req.maxdownload = (curl_off_t)sb.st_size;
|
2010-04-17 01:43:04 +04:00
|
|
|
Curl_setup_transfer(conn, FIRSTSOCKET, bytecount, FALSE, NULL, -1, NULL);
|
2009-09-26 12:31:48 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
/* not set by Curl_setup_transfer to preserve keepon bits */
|
|
|
|
conn->writesockfd = conn->sockfd;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2011-03-14 01:21:03 +03: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-12 02:10:53 +03:00
|
|
|
conn->cselect_bits = CURL_CSELECT_IN;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(result) {
|
|
|
|
state(conn, SSH_SCP_CHANNEL_FREE);
|
|
|
|
sshc->actualcode = result;
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
else
|
|
|
|
state(conn, SSH_STOP);
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SCP_DONE:
|
|
|
|
if(data->set.upload)
|
|
|
|
state(conn, SSH_SCP_SEND_EOF);
|
|
|
|
else
|
|
|
|
state(conn, SSH_SCP_CHANNEL_FREE);
|
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SCP_SEND_EOF:
|
|
|
|
if(sshc->ssh_channel) {
|
|
|
|
rc = libssh2_channel_send_eof(sshc->ssh_channel);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc) {
|
|
|
|
infof(data, "Failed to send libssh2 channel EOF\n");
|
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SCP_WAIT_EOF);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SCP_WAIT_EOF:
|
|
|
|
if(sshc->ssh_channel) {
|
|
|
|
rc = libssh2_channel_wait_eof(sshc->ssh_channel);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc) {
|
|
|
|
infof(data, "Failed to get channel EOF: %d\n", rc);
|
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SCP_WAIT_CLOSE);
|
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SCP_WAIT_CLOSE:
|
|
|
|
if(sshc->ssh_channel) {
|
|
|
|
rc = libssh2_channel_wait_closed(sshc->ssh_channel);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc) {
|
|
|
|
infof(data, "Channel failed to close: %d\n", rc);
|
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SCP_CHANNEL_FREE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_SCP_CHANNEL_FREE:
|
|
|
|
if(sshc->ssh_channel) {
|
|
|
|
rc = libssh2_channel_free(sshc->ssh_channel);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc < 0) {
|
|
|
|
infof(data, "Failed to free libssh2 scp subsystem\n");
|
|
|
|
}
|
|
|
|
sshc->ssh_channel = NULL;
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2010-01-12 02:10:53 +03:00
|
|
|
DEBUGF(infof(data, "SCP DONE phase complete\n"));
|
2007-11-08 13:22:49 +03:00
|
|
|
#if 0 /* PREV */
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SESSION_DISCONNECT);
|
2007-11-08 13:22:49 +03:00
|
|
|
#endif
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_STOP);
|
|
|
|
result = sshc->actualcode;
|
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03: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;
|
|
|
|
}
|
|
|
|
else if(rc < 0) {
|
|
|
|
infof(data, "Failed to free libssh2 scp subsystem\n");
|
|
|
|
}
|
|
|
|
sshc->ssh_channel = NULL;
|
2008-05-29 00:31:28 +04:00
|
|
|
}
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
if(sshc->ssh_session) {
|
|
|
|
rc = libssh2_session_disconnect(sshc->ssh_session, "Shutdown");
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc < 0) {
|
|
|
|
infof(data, "Failed to disconnect libssh2 session\n");
|
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
Curl_safefree(sshc->homedir);
|
2010-12-30 04:43:45 +03:00
|
|
|
conn->data->state.most_recent_ftp_entrypath = NULL;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
state(conn, SSH_SESSION_FREE);
|
|
|
|
break;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
case SSH_SESSION_FREE:
|
2009-10-18 05:11:25 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
2010-01-12 02:10:53 +03:00
|
|
|
if(sshc->kh) {
|
|
|
|
libssh2_knownhost_free(sshc->kh);
|
|
|
|
sshc->kh = NULL;
|
|
|
|
}
|
2009-10-18 05:11:25 +04:00
|
|
|
#endif
|
|
|
|
|
2012-03-09 20:24:42 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_AGENT_API
|
|
|
|
if(sshc->ssh_agent) {
|
|
|
|
rc = libssh2_agent_disconnect(sshc->ssh_agent);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc < 0) {
|
|
|
|
infof(data, "Failed to disconnect from libssh2 agent\n");
|
|
|
|
}
|
|
|
|
libssh2_agent_free (sshc->ssh_agent);
|
|
|
|
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-12 02:10:53 +03:00
|
|
|
if(sshc->ssh_session) {
|
|
|
|
rc = libssh2_session_free(sshc->ssh_session);
|
|
|
|
if(rc == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(rc < 0) {
|
|
|
|
infof(data, "Failed to free libssh2 session\n");
|
|
|
|
}
|
|
|
|
sshc->ssh_session = NULL;
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
2011-09-07 03: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 22:52:11 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_AGENT_API
|
|
|
|
DEBUGASSERT(sshc->ssh_agent == NULL);
|
|
|
|
#endif
|
2011-09-07 03: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_filename);
|
|
|
|
Curl_safefree(sshc->readdir_longentry);
|
|
|
|
Curl_safefree(sshc->readdir_line);
|
|
|
|
Curl_safefree(sshc->readdir_linkPath);
|
|
|
|
|
|
|
|
/* the code we are about to return */
|
|
|
|
result = sshc->actualcode;
|
|
|
|
|
|
|
|
memset(sshc, 0, sizeof(struct ssh_conn));
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
conn->bits.close = TRUE;
|
2011-09-07 03:26:43 +04:00
|
|
|
sshc->state = SSH_SESSION_FREE; /* current */
|
2010-01-12 02:10:53 +03:00
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SSH_QUIT:
|
|
|
|
/* fallthrough, just stop! */
|
|
|
|
default:
|
|
|
|
/* internal error */
|
|
|
|
sshc->nextstate = SSH_NO_STATE;
|
|
|
|
state(conn, SSH_STOP);
|
|
|
|
break;
|
2007-11-08 13:22:49 +03:00
|
|
|
}
|
|
|
|
|
2010-01-12 02:10:53 +03:00
|
|
|
} while(!rc && (sshc->state != SSH_STOP));
|
2007-06-12 16:31:10 +04:00
|
|
|
|
2008-11-24 16:59:51 +03: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 16:31:10 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-12-20 00:14:52 +03: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 */
|
|
|
|
static int ssh_perform_getsock(const struct connectdata *conn,
|
|
|
|
curl_socket_t *sock, /* points to numsocks
|
|
|
|
number of sockets */
|
|
|
|
int numsocks)
|
|
|
|
{
|
2009-05-20 03:21:25 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
|
2008-12-20 00:14:52 +03:00
|
|
|
int bitmap = GETSOCK_BLANK;
|
|
|
|
(void)numsocks;
|
|
|
|
|
|
|
|
sock[0] = conn->sock[FIRSTSOCKET];
|
|
|
|
|
2009-09-26 12:31:48 +04:00
|
|
|
if(conn->waitfor & KEEP_RECV)
|
2008-12-20 00:14:52 +03:00
|
|
|
bitmap |= GETSOCK_READSOCK(FIRSTSOCKET);
|
|
|
|
|
2009-09-26 12:31:48 +04:00
|
|
|
if(conn->waitfor & KEEP_SEND)
|
2008-12-20 00:14:52 +03:00
|
|
|
bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET);
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
#else
|
|
|
|
/* if we don't know the direction we can use the generic *_getsock()
|
|
|
|
function even for the protocol_connect and doing states */
|
|
|
|
return Curl_single_getsock(conn, sock, numsocks);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generic function called by the multi interface to figure out what socket(s)
|
|
|
|
to wait for and for what actions during the DOING and PROTOCONNECT states*/
|
|
|
|
static int ssh_getsock(struct connectdata *conn,
|
|
|
|
curl_socket_t *sock, /* points to numsocks number
|
|
|
|
of sockets */
|
|
|
|
int numsocks)
|
|
|
|
{
|
2009-05-20 03:21:25 +04:00
|
|
|
#ifndef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
|
2008-12-28 08:49:39 +03:00
|
|
|
(void)conn;
|
|
|
|
(void)sock;
|
|
|
|
(void)numsocks;
|
2008-12-20 00:14:52 +03:00
|
|
|
/* if we don't know any direction we can just play along as we used to and
|
|
|
|
not provide any sensible info */
|
|
|
|
return GETSOCK_BLANK;
|
|
|
|
#else
|
|
|
|
/* if we know the direction we can use the generic *_getsock() function even
|
|
|
|
for the protocol_connect and doing states */
|
|
|
|
return ssh_perform_getsock(conn, sock, numsocks);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-05-20 03:21:25 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
|
2008-12-20 00:14:52 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static void ssh_block2waitfor(struct connectdata *conn, bool block)
|
|
|
|
{
|
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
int dir;
|
2009-09-26 12:31:48 +04:00
|
|
|
if(!block)
|
|
|
|
conn->waitfor = 0;
|
|
|
|
else if((dir = libssh2_session_block_directions(sshc->ssh_session))) {
|
2008-12-20 00:14:52 +03:00
|
|
|
/* translate the libssh2 define bits into our own bit defines */
|
2009-09-26 12:31:48 +04:00
|
|
|
conn->waitfor = ((dir&LIBSSH2_SESSION_BLOCK_INBOUND)?KEEP_RECV:0) |
|
2009-05-11 11:53:38 +04:00
|
|
|
((dir&LIBSSH2_SESSION_BLOCK_OUTBOUND)?KEEP_SEND:0);
|
2008-12-20 00:14:52 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* It didn't block or libssh2 didn't reveal in which direction, put back
|
|
|
|
the original set */
|
2009-09-26 12:31:48 +04:00
|
|
|
conn->waitfor = sshc->orig_waitfor;
|
2008-12-20 00:14:52 +03:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* no libssh2 directional support so we simply don't know */
|
2011-09-03 18:06:10 +04:00
|
|
|
#define ssh_block2waitfor(x,y) Curl_nop_stmt
|
2008-12-20 00:14:52 +03:00
|
|
|
#endif
|
|
|
|
|
2007-06-12 16:31:10 +04:00
|
|
|
/* called repeatedly until done from multi.c */
|
2007-11-08 13:22:49 +03:00
|
|
|
static CURLcode ssh_multi_statemach(struct connectdata *conn, bool *done)
|
2007-06-12 16:31:10 +04:00
|
|
|
{
|
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
CURLcode result = CURLE_OK;
|
2008-12-20 00:14:52 +03:00
|
|
|
bool block; /* we store the status and use that to provide a ssh_getsock()
|
|
|
|
implementation */
|
2007-06-12 16:31:10 +04:00
|
|
|
|
2008-12-20 00:14:52 +03:00
|
|
|
result = ssh_statemach_act(conn, &block);
|
2011-09-05 22:46:09 +04:00
|
|
|
*done = (sshc->state == SSH_STOP) ? TRUE : FALSE;
|
2008-12-20 00:14:52 +03:00
|
|
|
ssh_block2waitfor(conn, block);
|
2007-06-12 16:31:10 +04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-06-03 01:33:51 +04:00
|
|
|
static CURLcode ssh_easy_statemach(struct connectdata *conn,
|
|
|
|
bool duringconnect)
|
2007-06-12 16:31:10 +04:00
|
|
|
{
|
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
CURLcode result = CURLE_OK;
|
2010-06-03 01:33:51 +04:00
|
|
|
struct SessionHandle *data = conn->data;
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2008-11-24 16:59:51 +03:00
|
|
|
while((sshc->state != SSH_STOP) && !result) {
|
|
|
|
bool block;
|
2010-06-03 01:33:51 +04:00
|
|
|
long left;
|
|
|
|
|
2008-11-24 16:59:51 +03:00
|
|
|
result = ssh_statemach_act(conn, &block);
|
2011-01-06 02:19:17 +03:00
|
|
|
if(result)
|
|
|
|
break;
|
2008-11-24 16:59:51 +03:00
|
|
|
|
2010-06-03 01:33:51 +04:00
|
|
|
if(Curl_pgrsUpdate(conn))
|
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
2011-01-05 16:09:53 +03:00
|
|
|
else {
|
|
|
|
struct timeval now = Curl_tvnow();
|
|
|
|
result = Curl_speedcheck(data, now);
|
|
|
|
if(result)
|
2011-01-06 02:19:17 +03:00
|
|
|
break;
|
2011-01-05 16:09:53 +03:00
|
|
|
}
|
2010-06-03 01:33:51 +04:00
|
|
|
|
2011-01-05 01:07:58 +03:00
|
|
|
left = Curl_timeleft(data, NULL, duringconnect);
|
2010-06-03 01:33:51 +04:00
|
|
|
if(left < 0) {
|
2012-06-14 15:32:05 +04:00
|
|
|
failf(data, "Operation timed out");
|
2010-06-03 01:33:51 +04:00
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
2009-05-20 03:21:25 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
|
2008-11-24 16:59:51 +03:00
|
|
|
if((CURLE_OK == result) && block) {
|
|
|
|
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 17:17:42 +04:00
|
|
|
if(LIBSSH2_SESSION_BLOCK_INBOUND & dir)
|
2008-11-24 16:59:51 +03:00
|
|
|
fd_read = sock;
|
2011-04-20 17:17:42 +04:00
|
|
|
if(LIBSSH2_SESSION_BLOCK_OUTBOUND & dir)
|
2008-11-24 16:59:51 +03:00
|
|
|
fd_write = sock;
|
|
|
|
/* wait for the socket to become ready */
|
2010-06-03 01:33:51 +04:00
|
|
|
Curl_socket_ready(fd_read, fd_write,
|
2011-06-04 23:19:14 +04:00
|
|
|
left>1000?1000:left); /* ignore result */
|
2008-11-24 16:59:51 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
2007-06-12 16:31:10 +04:00
|
|
|
|
2007-07-11 02:26:32 +04:00
|
|
|
return result;
|
2007-06-12 16:31:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SSH setup and connection
|
|
|
|
*/
|
2006-11-25 01:14:39 +03:00
|
|
|
static CURLcode ssh_init(struct connectdata *conn)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
|
|
|
struct SessionHandle *data = conn->data;
|
2006-11-25 01:14:39 +03:00
|
|
|
struct SSHPROTO *ssh;
|
2008-03-14 00:43:30 +03:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
2008-03-09 14:37:48 +03:00
|
|
|
|
2008-03-14 00:43:30 +03:00
|
|
|
sshc->actualcode = CURLE_OK; /* reset error code */
|
2010-02-21 00:41:25 +03:00
|
|
|
sshc->secondCreateDirs =0; /* reset the create dir attempt state
|
|
|
|
variable */
|
2008-03-09 14:37:48 +03:00
|
|
|
|
2007-11-25 02:16:55 +03:00
|
|
|
if(data->state.proto.ssh)
|
2006-11-03 00:56:40 +03:00
|
|
|
return CURLE_OK;
|
|
|
|
|
2009-11-18 13:33:54 +03:00
|
|
|
ssh = calloc(1, sizeof(struct SSHPROTO));
|
2007-11-05 12:30:45 +03:00
|
|
|
if(!ssh)
|
2006-11-03 00:56:40 +03:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2007-11-25 02:16:55 +03:00
|
|
|
data->state.proto.ssh = ssh;
|
2006-11-03 00:56:40 +03:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-07 17:05:34 +04:00
|
|
|
static Curl_recv scp_recv, sftp_recv;
|
|
|
|
static Curl_send scp_send, sftp_send;
|
|
|
|
|
2006-11-03 00:56:40 +03:00
|
|
|
/*
|
2006-11-25 01:14:39 +03:00
|
|
|
* Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to
|
2006-11-03 00:56:40 +03:00
|
|
|
* do protocol-specific actions at connect-time.
|
|
|
|
*/
|
2007-11-08 13:22:49 +03:00
|
|
|
static CURLcode ssh_connect(struct connectdata *conn, bool *done)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
2008-02-18 16:05:46 +03:00
|
|
|
#ifdef CURL_LIBSSH2_DEBUG
|
2006-11-03 00:56:40 +03:00
|
|
|
curl_socket_t sock;
|
2008-02-18 16:05:46 +03:00
|
|
|
#endif
|
|
|
|
struct ssh_conn *ssh;
|
2006-11-03 00:56:40 +03:00
|
|
|
CURLcode result;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
|
2007-11-05 12:30:45 +03: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. */
|
|
|
|
conn->bits.close = FALSE;
|
|
|
|
|
2007-10-22 19:05:35 +04:00
|
|
|
/* If there already is a protocol-specific struct allocated for this
|
|
|
|
sessionhandle, deal with it */
|
|
|
|
Curl_reset_reqproto(conn);
|
2007-09-30 01:34:34 +04:00
|
|
|
|
2006-11-25 01:14:39 +03:00
|
|
|
result = ssh_init(conn);
|
2007-11-05 12:30:45 +03:00
|
|
|
if(result)
|
2006-11-03 00:56:40 +03:00
|
|
|
return result;
|
|
|
|
|
2011-03-15 00:52:14 +03:00
|
|
|
if(conn->handler->protocol & CURLPROTO_SCP) {
|
2010-05-12 00:48:38 +04:00
|
|
|
conn->recv[FIRSTSOCKET] = scp_recv;
|
|
|
|
conn->send[FIRSTSOCKET] = scp_send;
|
2011-04-20 17:17:42 +04:00
|
|
|
}
|
|
|
|
else {
|
2010-05-12 00:48:38 +04:00
|
|
|
conn->recv[FIRSTSOCKET] = sftp_recv;
|
|
|
|
conn->send[FIRSTSOCKET] = sftp_send;
|
2010-05-07 17:05:34 +04:00
|
|
|
}
|
2007-11-05 12:30:45 +03:00
|
|
|
ssh = &conn->proto.sshc;
|
2006-11-03 00:56:40 +03:00
|
|
|
|
|
|
|
#ifdef CURL_LIBSSH2_DEBUG
|
2007-11-12 12:24:05 +03:00
|
|
|
if(conn->user) {
|
|
|
|
infof(data, "User: %s\n", conn->user);
|
2006-11-03 00:56:40 +03:00
|
|
|
}
|
2007-11-12 12:24:05 +03:00
|
|
|
if(conn->passwd) {
|
|
|
|
infof(data, "Password: %s\n", conn->passwd);
|
2006-11-03 00:56:40 +03:00
|
|
|
}
|
|
|
|
sock = conn->sock[FIRSTSOCKET];
|
2008-02-18 16:05:46 +03:00
|
|
|
#endif /* CURL_LIBSSH2_DEBUG */
|
|
|
|
|
2011-09-10 02:52:13 +04:00
|
|
|
ssh->ssh_session = libssh2_session_init_ex(my_libssh2_malloc,
|
|
|
|
my_libssh2_free,
|
|
|
|
my_libssh2_realloc, conn);
|
2007-11-05 12:30:45 +03:00
|
|
|
if(ssh->ssh_session == NULL) {
|
2007-04-09 02:44:21 +04:00
|
|
|
failf(data, "Failure initialising ssh session");
|
2006-11-03 00:56:40 +03:00
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
}
|
2007-06-11 08:07:51 +04:00
|
|
|
|
2009-07-23 02:49:01 +04:00
|
|
|
#ifdef HAVE_LIBSSH2_KNOWNHOST_API
|
|
|
|
if(data->set.str[STRING_SSH_KNOWNHOSTS]) {
|
|
|
|
int rc;
|
|
|
|
ssh->kh = libssh2_knownhost_init(ssh->ssh_session);
|
|
|
|
if(!ssh->kh) {
|
|
|
|
/* eeek. TODO: free the ssh_session! */
|
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read all known hosts from there */
|
|
|
|
rc = libssh2_knownhost_readfile(ssh->kh,
|
|
|
|
data->set.str[STRING_SSH_KNOWNHOSTS],
|
|
|
|
LIBSSH2_KNOWNHOST_FILE_OPENSSH);
|
2011-02-26 12:59:03 +03:00
|
|
|
if(rc < 0)
|
2009-07-23 02: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-03 00:56:40 +03:00
|
|
|
#ifdef CURL_LIBSSH2_DEBUG
|
2008-12-20 00:14:52 +03:00
|
|
|
libssh2_trace(ssh->ssh_session, ~0);
|
2010-02-04 22:44:31 +03:00
|
|
|
infof(data, "SSH socket: %d\n", (int)sock);
|
2006-11-03 00:56:40 +03:00
|
|
|
#endif /* CURL_LIBSSH2_DEBUG */
|
|
|
|
|
2011-05-02 01:01:31 +04:00
|
|
|
state(conn, SSH_INIT);
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
if(data->state.used_interface == Curl_if_multi)
|
2007-11-08 13:22:49 +03:00
|
|
|
result = ssh_multi_statemach(conn, done);
|
2007-06-12 16:31:10 +04:00
|
|
|
else {
|
2010-06-03 01:33:51 +04:00
|
|
|
result = ssh_easy_statemach(conn, TRUE);
|
2007-11-05 12:30:45 +03:00
|
|
|
if(!result)
|
2007-06-12 16:31:10 +04:00
|
|
|
*done = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2006-11-03 00:56:40 +03:00
|
|
|
}
|
|
|
|
|
2007-07-11 02:26:32 +04:00
|
|
|
/*
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
* scp_perform()
|
|
|
|
*
|
|
|
|
* This is the actual DO function for SCP. Get a file according to
|
|
|
|
* the options previously setup.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static
|
|
|
|
CURLcode scp_perform(struct connectdata *conn,
|
|
|
|
bool *connected,
|
|
|
|
bool *dophase_done)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
DEBUGF(infof(conn->data, "DO phase starts\n"));
|
|
|
|
|
|
|
|
*dophase_done = FALSE; /* not done yet */
|
|
|
|
|
|
|
|
/* start the first command in the DO phase */
|
|
|
|
state(conn, SSH_SCP_TRANS_INIT);
|
|
|
|
|
|
|
|
/* run the state-machine */
|
2007-11-05 12:30:45 +03:00
|
|
|
if(conn->data->state.used_interface == Curl_if_multi) {
|
2007-11-08 13:22:49 +03:00
|
|
|
result = ssh_multi_statemach(conn, dophase_done);
|
|
|
|
}
|
|
|
|
else {
|
2010-06-03 01:33:51 +04:00
|
|
|
result = ssh_easy_statemach(conn, FALSE);
|
2007-07-11 02:26:32 +04:00
|
|
|
*dophase_done = TRUE; /* with the easy interface we are done here */
|
|
|
|
}
|
2011-08-20 01:38:45 +04:00
|
|
|
*connected = conn->bits.tcpconnect[FIRSTSOCKET];
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
if(*dophase_done) {
|
2007-07-11 02:26:32 +04:00
|
|
|
DEBUGF(infof(conn->data, "DO phase is complete\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called from multi.c while DOing */
|
2007-11-08 13:22:49 +03:00
|
|
|
static CURLcode scp_doing(struct connectdata *conn,
|
2007-10-12 17:36:37 +04:00
|
|
|
bool *dophase_done)
|
2007-07-11 02:26:32 +04:00
|
|
|
{
|
|
|
|
CURLcode result;
|
2007-11-08 13:22:49 +03:00
|
|
|
result = ssh_multi_statemach(conn, dophase_done);
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
if(*dophase_done) {
|
2007-07-11 02:26:32 +04:00
|
|
|
DEBUGF(infof(conn->data, "DO phase is complete\n"));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
/*
|
|
|
|
* The DO function is generic for both protocols. There was previously two
|
|
|
|
* separate ones but this way means less duplicated code.
|
|
|
|
*/
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
static CURLcode ssh_do(struct connectdata *conn, bool *done)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
2007-07-11 02:26:32 +04:00
|
|
|
CURLcode res;
|
|
|
|
bool connected = 0;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
|
|
|
|
*done = FALSE; /* default to false */
|
|
|
|
|
2008-03-09 14:37:48 +03:00
|
|
|
/*
|
|
|
|
Since connections can be re-used between SessionHandles, this might be a
|
|
|
|
connection already existing but on a fresh SessionHandle struct so we must
|
|
|
|
make sure we have a good 'struct SSHPROTO' to play with. For new
|
|
|
|
connections, the struct SSHPROTO is allocated and setup in the
|
|
|
|
ssh_connect() function.
|
|
|
|
*/
|
|
|
|
Curl_reset_reqproto(conn);
|
|
|
|
res = ssh_init(conn);
|
|
|
|
if(res)
|
|
|
|
return res;
|
|
|
|
|
2007-11-25 02:16:55 +03:00
|
|
|
data->req.size = -1; /* make sure this is unknown at this point */
|
2007-07-11 02:26:32 +04:00
|
|
|
|
|
|
|
Curl_pgrsSetUploadCounter(data, 0);
|
|
|
|
Curl_pgrsSetDownloadCounter(data, 0);
|
|
|
|
Curl_pgrsSetUploadSize(data, 0);
|
|
|
|
Curl_pgrsSetDownloadSize(data, 0);
|
|
|
|
|
2011-03-15 00:52:14 +03:00
|
|
|
if(conn->handler->protocol & CURLPROTO_SCP)
|
2007-11-08 13:22:49 +03:00
|
|
|
res = scp_perform(conn, &connected, done);
|
|
|
|
else
|
|
|
|
res = sftp_perform(conn, &connected, done);
|
2007-07-11 02:26:32 +04:00
|
|
|
|
|
|
|
return res;
|
2006-11-03 00:56:40 +03:00
|
|
|
}
|
|
|
|
|
2010-02-21 00:41:25 +03: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 13:22:49 +03:00
|
|
|
disconnecting operations that takes a while */
|
2010-11-19 15:43:20 +03:00
|
|
|
static CURLcode scp_disconnect(struct connectdata *conn, bool dead_connection)
|
2007-11-08 13:22:49 +03:00
|
|
|
{
|
2008-05-31 05:37:21 +04:00
|
|
|
CURLcode result = CURLE_OK;
|
2009-10-18 05:11:25 +04:00
|
|
|
struct ssh_conn *ssh = &conn->proto.sshc;
|
2010-11-19 15:43:20 +03:00
|
|
|
(void) dead_connection;
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2007-11-25 02:16:55 +03:00
|
|
|
Curl_safefree(conn->data->state.proto.ssh);
|
2007-11-13 02:04:44 +03:00
|
|
|
|
2009-10-18 05:11:25 +04:00
|
|
|
if(ssh->ssh_session) {
|
2008-05-29 00:31:28 +04:00
|
|
|
/* only if there's a session still around to use! */
|
|
|
|
|
|
|
|
state(conn, SSH_SESSION_DISCONNECT);
|
2007-11-08 13:22:49 +03:00
|
|
|
|
2010-06-03 01:33:51 +04:00
|
|
|
result = ssh_easy_statemach(conn, FALSE);
|
2008-05-29 00:31:28 +04:00
|
|
|
}
|
2007-11-08 13:22:49 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-05-29 00:31:28 +04:00
|
|
|
/* generic done function for both SCP and SFTP called from their specific
|
|
|
|
done functions */
|
|
|
|
static CURLcode ssh_done(struct connectdata *conn, CURLcode status)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
2007-07-11 02:26:32 +04:00
|
|
|
CURLcode result = CURLE_OK;
|
2008-12-17 15:32:41 +03:00
|
|
|
struct SSHPROTO *sftp_scp = conn->data->state.proto.ssh;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
if(status == CURLE_OK) {
|
2008-12-17 15:32:41 +03:00
|
|
|
/* run the state-machine
|
|
|
|
|
2010-02-21 00:41:25 +03:00
|
|
|
TODO: when the multi interface is used, this _really_ should be using
|
|
|
|
the ssh_multi_statemach function but we have no general support for
|
2008-12-17 15:32:41 +03:00
|
|
|
non-blocking DONE operations, not in the multi state machine and with
|
|
|
|
Curl_done() invokes on several places in the code!
|
|
|
|
*/
|
2010-06-03 01:33:51 +04:00
|
|
|
result = ssh_easy_statemach(conn, FALSE);
|
2007-11-05 12:30:45 +03:00
|
|
|
}
|
2008-12-17 15:32:41 +03:00
|
|
|
else
|
2007-07-11 02:26:32 +04:00
|
|
|
result = status;
|
2008-05-29 00:31:28 +04:00
|
|
|
|
2012-03-25 20:10:09 +04:00
|
|
|
if(sftp_scp)
|
2011-03-13 02:15:59 +03:00
|
|
|
Curl_safefree(sftp_scp->path);
|
2012-06-11 01:39:04 +04:00
|
|
|
if(Curl_pgrsDone(conn))
|
|
|
|
return CURLE_ABORTED_BY_CALLBACK;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2008-12-20 00:14:52 +03:00
|
|
|
conn->data->req.keepon = 0; /* clear all bits */
|
2007-07-11 02:26:32 +04:00
|
|
|
return result;
|
2008-05-29 00:31:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CURLcode scp_done(struct connectdata *conn, CURLcode status,
|
|
|
|
bool premature)
|
|
|
|
{
|
|
|
|
(void)premature; /* not used */
|
|
|
|
|
|
|
|
if(status == CURLE_OK)
|
|
|
|
state(conn, SSH_SCP_DONE);
|
|
|
|
|
|
|
|
return ssh_done(conn, status);
|
2006-11-03 00:56:40 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return number of received (decrypted) bytes */
|
2010-05-07 17:05:34 +04:00
|
|
|
static ssize_t scp_send(struct connectdata *conn, int sockindex,
|
|
|
|
const void *mem, size_t len, CURLcode *err)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
|
|
|
ssize_t nwrite;
|
2007-11-05 12:30:45 +03:00
|
|
|
(void)sockindex; /* we only support SCP on the fixed known primary socket */
|
2006-11-03 00:56:40 +03:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
/* libssh2_channel_write() returns int! */
|
2006-11-25 01:14:39 +03:00
|
|
|
nwrite = (ssize_t)
|
2007-11-05 12:30:45 +03:00
|
|
|
libssh2_channel_write(conn->proto.sshc.ssh_channel, mem, len);
|
2008-12-20 00:14:52 +03:00
|
|
|
|
|
|
|
ssh_block2waitfor(conn, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
|
|
|
|
|
2010-05-07 17:05:34 +04:00
|
|
|
if(nwrite == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
nwrite = 0;
|
|
|
|
}
|
2012-11-12 13:04:44 +04:00
|
|
|
else if(nwrite < LIBSSH2_ERROR_NONE) {
|
2012-11-20 23:57:18 +04:00
|
|
|
*err = libssh2_session_error_to_CURLE((int)nwrite);
|
2012-11-12 13:04:44 +04:00
|
|
|
nwrite = -1;
|
|
|
|
}
|
2007-11-05 12:30:45 +03:00
|
|
|
|
2006-11-03 00:56:40 +03:00
|
|
|
return nwrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
|
|
|
|
* a regular CURLcode value.
|
|
|
|
*/
|
2010-05-07 17:05:34 +04:00
|
|
|
static ssize_t scp_recv(struct connectdata *conn, int sockindex,
|
|
|
|
char *mem, size_t len, CURLcode *err)
|
2006-11-03 00:56:40 +03:00
|
|
|
{
|
|
|
|
ssize_t nread;
|
2007-02-02 18:26:57 +03:00
|
|
|
(void)sockindex; /* we only support SCP on the fixed known primary socket */
|
2006-11-03 00:56:40 +03:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
/* libssh2_channel_read() returns int */
|
2006-11-25 01:14:39 +03:00
|
|
|
nread = (ssize_t)
|
2007-11-05 12:30:45 +03:00
|
|
|
libssh2_channel_read(conn->proto.sshc.ssh_channel, mem, len);
|
2008-12-20 00:14:52 +03:00
|
|
|
|
|
|
|
ssh_block2waitfor(conn, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
|
2011-04-20 17:17:42 +04:00
|
|
|
if(nread == LIBSSH2_ERROR_EAGAIN) {
|
2010-05-07 17:05:34 +04:00
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
nread = -1;
|
|
|
|
}
|
2008-12-20 00:14:52 +03:00
|
|
|
|
2006-11-25 01:14:39 +03:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* =============== SFTP ===============
|
|
|
|
*/
|
|
|
|
|
2007-07-11 02: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
|
|
|
|
CURLcode sftp_perform(struct connectdata *conn,
|
|
|
|
bool *connected,
|
|
|
|
bool *dophase_done)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
DEBUGF(infof(conn->data, "DO phase starts\n"));
|
|
|
|
|
|
|
|
*dophase_done = FALSE; /* not done yet */
|
|
|
|
|
|
|
|
/* start the first command in the DO phase */
|
|
|
|
state(conn, SSH_SFTP_QUOTE_INIT);
|
|
|
|
|
|
|
|
/* run the state-machine */
|
2007-11-05 12:30:45 +03:00
|
|
|
if(conn->data->state.used_interface == Curl_if_multi) {
|
2007-11-08 13:22:49 +03:00
|
|
|
result = ssh_multi_statemach(conn, dophase_done);
|
2007-11-05 12:30:45 +03:00
|
|
|
}
|
|
|
|
else {
|
2010-06-03 01:33:51 +04:00
|
|
|
result = ssh_easy_statemach(conn, FALSE);
|
2007-07-11 02:26:32 +04:00
|
|
|
*dophase_done = TRUE; /* with the easy interface we are done here */
|
|
|
|
}
|
2011-08-20 01:38:45 +04:00
|
|
|
*connected = conn->bits.tcpconnect[FIRSTSOCKET];
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
if(*dophase_done) {
|
2007-07-11 02:26:32 +04:00
|
|
|
DEBUGF(infof(conn->data, "DO phase is complete\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called from multi.c while DOing */
|
2007-11-08 13:22:49 +03:00
|
|
|
static CURLcode sftp_doing(struct connectdata *conn,
|
|
|
|
bool *dophase_done)
|
2007-07-11 02:26:32 +04:00
|
|
|
{
|
|
|
|
CURLcode result;
|
2007-11-08 13:22:49 +03:00
|
|
|
result = ssh_multi_statemach(conn, dophase_done);
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
if(*dophase_done) {
|
2007-07-11 02:26:32 +04:00
|
|
|
DEBUGF(infof(conn->data, "DO phase is complete\n"));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-02-21 00:41:25 +03: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 13:22:49 +03:00
|
|
|
disconnecting operations that takes a while */
|
2010-11-19 15:43:20 +03:00
|
|
|
static CURLcode sftp_disconnect(struct connectdata *conn, bool dead_connection)
|
2006-11-25 01:14:39 +03:00
|
|
|
{
|
2008-05-31 05:37:21 +04:00
|
|
|
CURLcode result = CURLE_OK;
|
2010-11-19 15:43:20 +03:00
|
|
|
(void) dead_connection;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
DEBUGF(infof(conn->data, "SSH DISCONNECT starts now\n"));
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-25 02:16:55 +03:00
|
|
|
Curl_safefree(conn->data->state.proto.ssh);
|
2007-11-13 02:04:44 +03:00
|
|
|
|
2008-05-29 00:31:28 +04:00
|
|
|
if(conn->proto.sshc.ssh_session) {
|
|
|
|
/* only if there's a session still around to use! */
|
|
|
|
state(conn, SSH_SFTP_SHUTDOWN);
|
2010-06-03 01:33:51 +04:00
|
|
|
result = ssh_easy_statemach(conn, FALSE);
|
2008-05-29 00:31:28 +04:00
|
|
|
}
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
DEBUGF(infof(conn->data, "SSH DISCONNECT is done\n"));
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
return result;
|
2007-07-11 02:26:32 +04:00
|
|
|
|
2006-11-25 01:14:39 +03:00
|
|
|
}
|
|
|
|
|
2007-11-08 13:22:49 +03:00
|
|
|
static CURLcode sftp_done(struct connectdata *conn, CURLcode status,
|
2007-10-12 17:36:37 +04:00
|
|
|
bool premature)
|
2006-11-25 01:14:39 +03:00
|
|
|
{
|
2007-07-11 02:26:32 +04:00
|
|
|
struct ssh_conn *sshc = &conn->proto.sshc;
|
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
if(status == CURLE_OK) {
|
2011-04-21 01: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 */
|
2007-11-05 12:30:45 +03:00
|
|
|
if(!status && !premature && conn->data->set.postquote) {
|
2011-04-21 01:37:29 +04:00
|
|
|
sshc->nextstate = SSH_SFTP_POSTQUOTE_INIT;
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2007-11-05 12:30:45 +03:00
|
|
|
else
|
|
|
|
state(conn, SSH_SFTP_CLOSE);
|
2007-07-11 02:26:32 +04:00
|
|
|
}
|
2008-05-29 00:31:28 +04:00
|
|
|
return ssh_done(conn, status);
|
2006-11-25 01:14:39 +03:00
|
|
|
}
|
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
/* return number of sent bytes */
|
2010-05-07 17:05:34 +04:00
|
|
|
static ssize_t sftp_send(struct connectdata *conn, int sockindex,
|
|
|
|
const void *mem, size_t len, CURLcode *err)
|
2006-11-25 01:14:39 +03:00
|
|
|
{
|
2007-04-19 00:15:22 +04:00
|
|
|
ssize_t nwrite; /* libssh2_sftp_write() used to return size_t in 0.14
|
2007-11-05 12:30:45 +03:00
|
|
|
but is changed to ssize_t in 0.15. These days we don't
|
|
|
|
support libssh2 0.15*/
|
|
|
|
(void)sockindex;
|
2006-11-25 01:14:39 +03:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
nwrite = libssh2_sftp_write(conn->proto.sshc.sftp_handle, mem, len);
|
2008-12-20 00:14:52 +03:00
|
|
|
|
|
|
|
ssh_block2waitfor(conn, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
|
|
|
|
|
2010-05-07 17:05:34 +04:00
|
|
|
if(nwrite == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
nwrite = 0;
|
|
|
|
}
|
2012-11-12 13:04:44 +04:00
|
|
|
else if(nwrite < LIBSSH2_ERROR_NONE) {
|
2012-11-20 23:57:18 +04:00
|
|
|
*err = libssh2_session_error_to_CURLE((int)nwrite);
|
2012-11-12 13:04:44 +04:00
|
|
|
nwrite = -1;
|
|
|
|
}
|
2007-08-03 12:14:51 +04:00
|
|
|
|
2006-11-25 01:14:39 +03:00
|
|
|
return nwrite;
|
|
|
|
}
|
|
|
|
|
2007-06-13 01:32:45 +04:00
|
|
|
/*
|
2007-11-05 12:30:45 +03:00
|
|
|
* Return number of received (decrypted) bytes
|
2007-06-13 01:32:45 +04:00
|
|
|
*/
|
2010-05-07 17:05:34 +04:00
|
|
|
static ssize_t sftp_recv(struct connectdata *conn, int sockindex,
|
|
|
|
char *mem, size_t len, CURLcode *err)
|
2007-06-13 01:32:45 +04:00
|
|
|
{
|
|
|
|
ssize_t nread;
|
|
|
|
(void)sockindex;
|
2007-06-13 16:15:23 +04:00
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
nread = libssh2_sftp_read(conn->proto.sshc.sftp_handle, mem, len);
|
2007-08-03 12:14:51 +04:00
|
|
|
|
2008-12-20 00:14:52 +03:00
|
|
|
ssh_block2waitfor(conn, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE);
|
|
|
|
|
2010-05-07 17:05:34 +04:00
|
|
|
if(nread == LIBSSH2_ERROR_EAGAIN) {
|
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
nread = -1;
|
|
|
|
}
|
2007-06-13 01:32:45 +04:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2007-04-03 01:24:05 +04:00
|
|
|
/* The get_pathname() function is being borrowed from OpenSSH sftp.c
|
|
|
|
version 4.6p1. */
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2008-02-18 16:05:46 +03:00
|
|
|
static CURLcode
|
2007-04-03 01:24:05 +04:00
|
|
|
get_pathname(const char **cpp, char **path)
|
|
|
|
{
|
|
|
|
const char *cp = *cpp, *end;
|
|
|
|
char quot;
|
2008-09-10 17:39:35 +04:00
|
|
|
unsigned int i, j;
|
2008-10-22 09:46:29 +04:00
|
|
|
static const char WHITESPACE[] = " \t\r\n";
|
2007-04-03 01:24:05 +04:00
|
|
|
|
|
|
|
cp += strspn(cp, WHITESPACE);
|
2007-11-05 12:30:45 +03:00
|
|
|
if(!*cp) {
|
2007-04-03 01:24:05 +04:00
|
|
|
*cpp = cp;
|
|
|
|
*path = NULL;
|
2007-08-31 00:34:57 +04:00
|
|
|
return CURLE_QUOTE_ERROR;
|
2007-04-03 01:24:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*path = malloc(strlen(cp) + 1);
|
2007-11-05 12:30:45 +03:00
|
|
|
if(*path == NULL)
|
2007-04-03 01:24:05 +04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Check for quoted filenames */
|
2007-11-05 12:30:45 +03:00
|
|
|
if(*cp == '\"' || *cp == '\'') {
|
2007-04-03 01:24:05 +04:00
|
|
|
quot = *cp++;
|
|
|
|
|
|
|
|
/* Search for terminating quote, unescape some chars */
|
2011-04-20 17:17:42 +04:00
|
|
|
for(i = j = 0; i <= strlen(cp); i++) {
|
2007-11-05 12:30:45 +03:00
|
|
|
if(cp[i] == quot) { /* Found quote */
|
2007-04-03 01:24:05 +04:00
|
|
|
i++;
|
|
|
|
(*path)[j] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
2007-11-05 12:30:45 +03:00
|
|
|
if(cp[i] == '\0') { /* End of string */
|
2007-04-03 01:24:05 +04:00
|
|
|
/*error("Unterminated quote");*/
|
|
|
|
goto fail;
|
|
|
|
}
|
2007-11-05 12:30:45 +03:00
|
|
|
if(cp[i] == '\\') { /* Escaped characters */
|
2007-04-03 01:24:05 +04:00
|
|
|
i++;
|
2007-11-05 12:30:45 +03:00
|
|
|
if(cp[i] != '\'' && cp[i] != '\"' &&
|
2007-04-03 01:24:05 +04:00
|
|
|
cp[i] != '\\') {
|
|
|
|
/*error("Bad escaped character '\\%c'",
|
|
|
|
cp[i]);*/
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*path)[j++] = cp[i];
|
|
|
|
}
|
|
|
|
|
2007-11-05 12:30:45 +03:00
|
|
|
if(j == 0) {
|
2007-04-03 01:24:05 +04:00
|
|
|
/*error("Empty quotes");*/
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
*cpp = cp + i + strspn(cp + i, WHITESPACE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Read to end of filename */
|
|
|
|
end = strpbrk(cp, WHITESPACE);
|
2007-11-05 12:30:45 +03:00
|
|
|
if(end == NULL)
|
2007-04-03 01:24:05 +04:00
|
|
|
end = strchr(cp, '\0');
|
|
|
|
*cpp = end + strspn(end, WHITESPACE);
|
|
|
|
|
|
|
|
memcpy(*path, cp, end - cp);
|
|
|
|
(*path)[end - cp] = '\0';
|
|
|
|
}
|
2008-02-18 16:05:46 +03:00
|
|
|
return CURLE_OK;
|
2007-04-03 01:24:05 +04:00
|
|
|
|
|
|
|
fail:
|
2007-07-11 02:26:32 +04:00
|
|
|
Curl_safefree(*path);
|
2007-08-31 00:34:57 +04:00
|
|
|
return CURLE_QUOTE_ERROR;
|
2007-04-03 01:24:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-25 19:51:59 +04:00
|
|
|
static const char *sftp_libssh2_strerror(int err)
|
2007-04-03 01:24:05 +04:00
|
|
|
{
|
|
|
|
switch (err) {
|
2007-07-11 02: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-03 01:24:05 +04:00
|
|
|
}
|
|
|
|
return "Unknown error in libssh2";
|
|
|
|
}
|
|
|
|
|
2006-11-03 00:56:40 +03:00
|
|
|
#endif /* USE_LIBSSH2 */
|