Newer
Older
/***************************************************************************
James Housley
committed
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* 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.
*
* $Id$
***************************************************************************/
/* #define CURL_LIBSSH2_DEBUG */
#include "setup.h"
#ifdef USE_LIBSSH2
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#include <libssh2.h>
#include <libssh2_sftp.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifndef WIN32
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.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
#ifdef VMS
#include <in.h>
#include <inet.h>
#endif
#endif /* !WIN32 */
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#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 "easyif.h" /* for Curl_convert_... prototypes */
#include "if2ip.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 "strtoofft.h"
#include "strequal.h"
#include "sslgen.h"
#include "connect.h"
#include "strerror.h"
#include "memory.h"
#include "inet_ntop.h"
#include "select.h"
#include "parsedate.h" /* for the week day and month names */
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
#include "multiif.h"
#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
#include "inet_ntoa_r.h"
#endif
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
/* The last #include file should be: */
#ifdef CURLDEBUG
#include "memdebug.h"
#endif
#ifndef PATH_MAX
#define PATH_MAX 1024 /* just an extra precaution since there are systems that
have their definition hidden well */
#endif
#ifndef LIBSSH2_SFTP_S_IRUSR
/* Here's a work-around for those of you who happend to run a libssh2 version
that is 0.14 or older. We should remove this kludge as soon as we can
require a more recent libssh2 release. */
#ifndef S_IRGRP
#define S_IRGRP 0
#endif
#ifndef S_IROTH
#define S_IROTH 0
#endif
/* File mode */
/* Read, write, execute/search by owner */
#define LIBSSH2_SFTP_S_IRWXU S_IRWXU /* RWX mask for owner */
#define LIBSSH2_SFTP_S_IRUSR S_IRUSR /* R for owner */
#define LIBSSH2_SFTP_S_IWUSR S_IWUSR /* W for owner */
#define LIBSSH2_SFTP_S_IXUSR S_IXUSR /* X for owner */
/* Read, write, execute/search by group */
#define LIBSSH2_SFTP_S_IRWXG S_IRWXG /* RWX mask for group */
#define LIBSSH2_SFTP_S_IRGRP S_IRGRP /* R for group */
#define LIBSSH2_SFTP_S_IWGRP S_IWGRP /* W for group */
#define LIBSSH2_SFTP_S_IXGRP S_IXGRP /* X for group */
/* Read, write, execute/search by others */
#define LIBSSH2_SFTP_S_IRWXO S_IRWXO /* RWX mask for other */
#define LIBSSH2_SFTP_S_IROTH S_IROTH /* R for other */
#define LIBSSH2_SFTP_S_IWOTH S_IWOTH /* W for other */
#define LIBSSH2_SFTP_S_IXOTH S_IXOTH /* X for other */
/* File type */
#define LIBSSH2_SFTP_S_IFMT S_IFMT /* type of file mask */
#define LIBSSH2_SFTP_S_IFDIR S_IFDIR /* directory */
#define LIBSSH2_SFTP_S_IFLNK S_IFLNK /* symbolic link */
#define LIBSSH2_SFTP_S_IFSOCK S_IFSOCK /* socket */
#define LIBSSH2_SFTP_S_IFCHR S_IFCHR /* character special */
#define LIBSSH2_SFTP_S_IFBLK S_IFBLK /* block special */
#endif
/* Local functions: */
static const char *sftp_libssh2_strerror(unsigned long err);
Guenter Knauf
committed
#if (LIBSSH2_APINO < 200706012030L)
static CURLcode sftp_sendquote(struct connectdata *conn,
struct curl_slist *quote);
static CURLcode sftp_create_dirs(struct connectdata *conn);
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO < 200706012030L) */
static LIBSSH2_ALLOC_FUNC(libssh2_malloc);
static LIBSSH2_REALLOC_FUNC(libssh2_realloc);
static LIBSSH2_FREE_FUNC(libssh2_free);
James Housley
committed
static int get_pathname(const char **cpp, char **path);
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)
{
struct SSHPROTO *ssh = (struct SSHPROTO *)*abstract;
#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);
#else
(void)name;
(void)name_len;
(void)instruction;
(void)instruction_len;
#endif /* CURL_LIBSSH2_DEBUG */
if (num_prompts == 1) {
responses[0].text = strdup(ssh->passwd);
responses[0].length = strlen(ssh->passwd);
}
(void)prompts;
(void)abstract;
} /* kbd_callback */
static CURLcode sftp_libssh2_error_to_CURLE(unsigned long err)
return CURLE_OK;
/* TODO: map some 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 */
if (err == LIBSSH2_FX_NO_SUCH_FILE)
return CURLE_REMOTE_FILE_NOT_FOUND;
return CURLE_SSH;
}
static CURLcode libssh2_session_error_to_CURLE(int err)
{
(void)err;
return CURLE_SSH;
}
static LIBSSH2_ALLOC_FUNC(libssh2_malloc)
{
return malloc(count);
(void)abstract;
}
static LIBSSH2_REALLOC_FUNC(libssh2_realloc)
{
return realloc(ptr, count);
(void)abstract;
}
static LIBSSH2_FREE_FUNC(libssh2_free)
{
free(ptr);
(void)abstract;
}
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
* SSH State machine related code
*/
/* This is the ONLY way to change SSH state! */
James Housley
committed
static void state(struct connectdata *conn, sshstate state)
{
#if defined(CURLDEBUG) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
/* for debug purposes */
static const char * const names[] = {
James Housley
committed
"SSH_STOP",
"SSH_S_STARTUP",
"SSH_AUTHLIST",
"SSH_AUTH_PKEY_INIT",
"SSH_AUTH_PKEY",
"SSH_AUTH_PASS_INIT",
"SSH_AUTH_PASS",
"SSH_AUTH_HOST_INIT",
"SSH_AUTH_HOST",
"SSH_AUTH_KEY_INIT",
"SSH_AUTH_KEY",
"SSH_AUTH_DONE",
"SSH_SFTP_INIT",
"SSH_SFTP_REALPATH",
"SSH_GET_WORKINGPATH",
James Housley
committed
"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",
"SSH_SFTP_SHUTDOWN",
James Housley
committed
"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",
"SSH_CHANNEL_CLOSE",
"SSH_SESSION_DISCONECT",
"SSH_SESSION_FREE",
"QUIT"
};
#endif
struct ssh_conn *sshc = &conn->proto.sshc;
#if defined(CURLDEBUG) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
if (sshc->state != state) {
James Housley
committed
infof(conn->data, "SFTP %p state change from %s to %s\n",
sshc, names[sshc->state], names[state]);
}
#endif
sshc->state = state;
}
static CURLcode ssh_statemach_act(struct connectdata *conn)
{
CURLcode result = CURLE_OK;
James Housley
committed
struct SessionHandle *data = conn->data;
struct SSHPROTO *sftp_scp = data->reqdata.proto.ssh;
struct ssh_conn *sshc = &conn->proto.sshc;
curl_socket_t sock = conn->sock[FIRSTSOCKET];
#ifdef CURL_LIBSSH2_DEBUG
const char *fingerprint;
#endif /* CURL_LIBSSH2_DEBUG */
int rc;
James Housley
committed
long err;
switch(sshc->state) {
case SSH_S_STARTUP:
James Housley
committed
sshc->secondCreateDirs = 0;
sshc->nextState = SSH_NO_STATE;
sshc->actualCode = CURLE_OK;
rc = libssh2_session_startup(sftp_scp->ssh_session, sock);
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;
/* Set libssh2 to non-blocking, since cURL is all non-blocking */
James Housley
committed
libssh2_session_set_blocking(sftp_scp->ssh_session, 0);
#ifdef CURL_LIBSSH2_DEBUG
/*
* Before we authenticate we should check the hostkey's fingerprint
* against our known hosts. How that is handled (reading from file,
* whatever) is up to us. As for know not much is implemented, besides
* showing how to get the fingerprint.
*/
James Housley
committed
fingerprint = libssh2_hostkey_hash(sftp_scp->ssh_session,
LIBSSH2_HOSTKEY_HASH_MD5);
/* The fingerprint points to static storage (!), don't free() it. */
infof(data, "Fingerprint: ");
for (rc = 0; rc < 16; rc++) {
infof(data, "%02X ", (unsigned char) fingerprint[rc]);
}
infof(data, "\n");
#endif /* CURL_LIBSSH2_DEBUG */
state(conn, SSH_AUTHLIST);
break;
case SSH_AUTHLIST:
/* TBD - methods to check the host keys need to be done */
/*
* 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.
*/
James Housley
committed
sshc->authlist = libssh2_userauth_list(sftp_scp->ssh_session,
sftp_scp->user,
strlen(sftp_scp->user));
if (!sshc->authlist) {
James Housley
committed
if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
LIBSSH2_ERROR_EAGAIN) {
break;
} else {
state(conn, SSH_SESSION_FREE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
}
infof(data, "SSH authentication methods available: %s\n", sshc->authlist);
state(conn, SSH_AUTH_PKEY_INIT);
break;
case SSH_AUTH_PKEY_INIT:
/*
James Housley
committed
* Check the supported auth types in the order I feel is most secure
* with the requested type of authentication
*/
sshc->authed = FALSE;
if ((data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) &&
(strstr(sshc->authlist, "publickey") != NULL)) {
char *home;
sshc->rsa_pub = sshc->rsa = NULL;
/* To ponder about: should really the lib be messing about with the
HOME environment variable etc? */
home = curl_getenv("HOME");
Daniel Stenberg
committed
if (data->set.str[STRING_SSH_PUBLIC_KEY])
sshc->rsa_pub = aprintf("%s", data->set.str[STRING_SSH_PUBLIC_KEY]);
sshc->rsa_pub = aprintf("%s/.ssh/id_dsa.pub", home);
if (sshc->rsa_pub == NULL) {
James Housley
committed
Curl_safefree(home);
home = NULL;
state(conn, SSH_SESSION_FREE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
Daniel Stenberg
committed
if (data->set.str[STRING_SSH_PRIVATE_KEY])
sshc->rsa = aprintf("%s", data->set.str[STRING_SSH_PRIVATE_KEY]);
sshc->rsa = aprintf("%s/.ssh/id_dsa", home);
if (sshc->rsa == NULL) {
James Housley
committed
Curl_safefree(home);
home = NULL;
Curl_safefree(sshc->rsa_pub);
sshc->rsa_pub = NULL;
state(conn, SSH_SESSION_FREE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
Daniel Stenberg
committed
sshc->passphrase = data->set.str[STRING_KEY_PASSWD];
if (!sshc->passphrase)
sshc->passphrase = "";
James Housley
committed
Curl_safefree(home);
home = NULL;
James Housley
committed
infof(data, "Using ssh public key file %s\n", sshc->rsa_pub);
infof(data, "Using ssh private key file %s\n", sshc->rsa);
state(conn, SSH_AUTH_PKEY);
} else {
state(conn, SSH_AUTH_PASS_INIT);
}
break;
case SSH_AUTH_PKEY:
/* The function below checks if the files exists, no need to stat() here.
*/
James Housley
committed
rc = libssh2_userauth_publickey_fromfile(sftp_scp->ssh_session,
sftp_scp->user, sshc->rsa_pub,
sshc->rsa, sshc->passphrase);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
James Housley
committed
Curl_safefree(sshc->rsa_pub);
sshc->rsa_pub = NULL;
Curl_safefree(sshc->rsa);
sshc->rsa = NULL;
if (rc == 0) {
sshc->authed = TRUE;
James Housley
committed
infof(data, "Initialized SSH public key authentication\n");
state(conn, SSH_AUTH_DONE);
} else {
state(conn, SSH_AUTH_PASS_INIT);
}
break;
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);
}
break;
case SSH_AUTH_PASS:
James Housley
committed
rc = libssh2_userauth_password(sftp_scp->ssh_session, sftp_scp->user,
sftp_scp->passwd);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc == 0) {
sshc->authed = TRUE;
James Housley
committed
infof(data, "Initialized password authentication\n");
state(conn, SSH_AUTH_DONE);
} else {
state(conn, SSH_AUTH_HOST_INIT);
}
break;
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 {
state(conn, SSH_AUTH_KEY_INIT);
case SSH_AUTH_HOST:
state(conn, SSH_AUTH_KEY_INIT);
break;
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);
case SSH_AUTH_KEY:
/* Authentication failed. Continue with keyboard-interactive now. */
James Housley
committed
rc = libssh2_userauth_keyboard_interactive_ex(sftp_scp->ssh_session,
sftp_scp->user,
strlen(sftp_scp->user),
&kbd_callback);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc == 0) {
sshc->authed = TRUE;
James Housley
committed
infof(data, "Initialized keyboard interactive authentication\n");
}
state(conn, SSH_AUTH_DONE);
break;
case SSH_AUTH_DONE:
if (!sshc->authed) {
failf(data, "Authentication failure");
state(conn, SSH_SESSION_FREE);
sshc->actualCode = CURLE_LOGIN_DENIED;
/*
* At this point we have an authenticated ssh session.
*/
James Housley
committed
infof(data, "Authentication complete\n");
conn->sockfd = sock;
conn->writesockfd = CURL_SOCKET_BAD;
if (conn->protocol == PROT_SFTP) {
state(conn, SSH_SFTP_INIT);
break;
}
state(conn, SSH_GET_WORKINGPATH);
break;
case SSH_SFTP_INIT:
/*
* Start the libssh2 sftp session
*/
James Housley
committed
sftp_scp->sftp_session = libssh2_sftp_init(sftp_scp->ssh_session);
if (!sftp_scp->sftp_session) {
if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
LIBSSH2_ERROR_EAGAIN) {
break;
} else {
failf(data, "Failure initialising sftp session\n");
state(conn, SSH_SESSION_FREE);
sshc->actualCode = CURLE_FAILED_INIT;
break;
}
}
state(conn, SSH_SFTP_REALPATH);
break;
case SSH_SFTP_REALPATH:
{
char tempHome[PATH_MAX];
/*
* Get the "home" directory
*/
James Housley
committed
rc = libssh2_sftp_realpath(sftp_scp->sftp_session, ".",
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';
James Housley
committed
sftp_scp->homedir = (char *)strdup(tempHome);
if (!sftp_scp->homedir) {
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
} else {
/* Return the error type */
James Housley
committed
result = libssh2_sftp_last_error(sftp_scp->sftp_session);
DEBUGF(infof(data, "error = %d\n", result));
state(conn, SSH_STOP);
break;
}
state(conn, SSH_GET_WORKINGPATH);
}
break;
case SSH_GET_WORKINGPATH:
{
char *real_path;
char *working_path;
int working_path_len;
working_path = curl_easy_unescape(data, data->reqdata.path, 0,
&working_path_len);
if (!working_path) {
result = CURLE_OUT_OF_MEMORY;
James Housley
committed
state(conn, SSH_STOP);
/* Check for /~/ , indicating relative to the user's home directory */
if (conn->protocol == PROT_SCP) {
real_path = (char *)malloc(working_path_len+1);
if (real_path == NULL) {
Curl_safefree(working_path);
James Housley
committed
working_path = NULL;
state(conn, SSH_SESSION_FREE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
if (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);
}
else if (conn->protocol == PROT_SFTP) {
if (working_path[1] == '~') {
James Housley
committed
real_path = (char *)malloc(strlen(sftp_scp->homedir) +
working_path_len + 1);
if (real_path == NULL) {
James Housley
committed
Curl_safefree(sftp_scp->homedir);
sftp_scp->homedir = NULL;
Curl_safefree(working_path);
James Housley
committed
working_path = NULL;
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
/* It is referenced to the home directory, so strip the
leading '/' */
James Housley
committed
memcpy(real_path, sftp_scp->homedir, strlen(sftp_scp->homedir));
real_path[strlen(sftp_scp->homedir)] = '/';
real_path[strlen(sftp_scp->homedir)+1] = '\0';
if (working_path_len > 3) {
James Housley
committed
memcpy(real_path+strlen(sftp_scp->homedir)+1, working_path + 3,
1 + working_path_len -3);
}
}
else {
real_path = (char *)malloc(working_path_len+1);
if (real_path == NULL) {
James Housley
committed
Curl_safefree(sftp_scp->homedir);
sftp_scp->homedir = NULL;
Curl_safefree(working_path);
James Housley
committed
working_path = NULL;
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
memcpy(real_path, working_path, 1+working_path_len);
}
}
else {
Curl_safefree(working_path);
James Housley
committed
working_path = NULL;
state(conn, SSH_SESSION_FREE);
sshc->actualCode = CURLE_FAILED_INIT;
Curl_safefree(working_path);
James Housley
committed
working_path = NULL;
sftp_scp->path = real_path;
/* Connect is all done */
state(conn, SSH_STOP);
}
break;
James Housley
committed
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
case SSH_SFTP_QUOTE_INIT:
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;
/*
* Support some of the "FTP" commands
*/
if (curl_strnequal(sshc->quote_item->data, "PWD", 3)) {
/* output debug output if that is requested */
if (data->set.verbose) {
char tmp[PATH_MAX+1];
Curl_debug(data, CURLINFO_HEADER_OUT, (char *)"PWD\n", 4, conn);
snprintf(tmp, PATH_MAX, "257 \"%s\" is current directory.\n",
sftp_scp->path);
Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp), conn);
}
James Housley
committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
}
else if (sshc->quote_item->data) {
fprintf(stderr, "data: %s\n", sshc->quote_item->data);
/*
* the arguments following the command must be separated from the
* command with a space so we can check for it unconditionally
*/
cp = strchr(sshc->quote_item->data, ' ');
if (cp == NULL) {
failf(data, "Syntax error in SFTP command. Supply parameter(s)!");
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
/*
* also, every command takes at least one argument so we get that
* first argument right now
*/
err = get_pathname(&cp, &sshc->quote_path1);
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data, "Syntax error: Bad first parameter");
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = err;
break;
}
/*
* 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.
*/
if (curl_strnequal(sshc->quote_item->data, "chgrp ", 6) ||
curl_strnequal(sshc->quote_item->data, "chmod ", 6) ||
curl_strnequal(sshc->quote_item->data, "chown ", 6) ) {
/* attribute change */
/* sshc->quote_path1 contains the mode to set */
/* get the destination */
err = get_pathname(&cp, &sshc->quote_path2);
if (err) {
if (err == 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);
sshc->quote_path1 = NULL;
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = err;
break;
}
memset(&sshc->quote_attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
state(conn, SSH_SFTP_QUOTE_STAT);
break;
}
else if (curl_strnequal(sshc->quote_item->data, "ln ", 3) ||
curl_strnequal(sshc->quote_item->data, "symlink ", 8)) {
/* symbolic linking */
/* sshc->quote_path1 is the source */
/* get the destination */
err = get_pathname(&cp, &sshc->quote_path2);
if (err) {
if (err == 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);
sshc->quote_path1 = NULL;
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = err;
break;
}
state(conn, SSH_SFTP_QUOTE_SYMLINK);
break;
}
else if (curl_strnequal(sshc->quote_item->data, "mkdir ", 6)) {
/* create dir */
state(conn, SSH_SFTP_QUOTE_MKDIR);
break;
}
else if (curl_strnequal(sshc->quote_item->data, "rename ", 7)) {
/* rename file */
/* first param is the source path */
/* second param is the dest. path */
err = get_pathname(&cp, &sshc->quote_path2);
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data, "Syntax error in rename: Bad second parameter");
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = err;
break;
}
state(conn, SSH_SFTP_QUOTE_RENAME);
break;
}
else if (curl_strnequal(sshc->quote_item->data, "rmdir ", 6)) {
/* delete dir */
state(conn, SSH_SFTP_QUOTE_RMDIR);
break;
}
else if (curl_strnequal(sshc->quote_item->data, "rm ", 3)) {
state(conn, SSH_SFTP_QUOTE_UNLINK);
break;
}
if (sshc->quote_path1) {
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
}
if (sshc->quote_path2) {
Curl_safefree(sshc->quote_path2);
sshc->quote_path2 = NULL;
}
}
}
if (!sshc->quote_item) {
state(conn, SSH_SFTP_TRANS_INIT);
}
break;
case SSH_SFTP_NEXT_QUOTE:
if (sshc->quote_path1) {
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
}
if (sshc->quote_path2) {
Curl_safefree(sshc->quote_path2);
sshc->quote_path2 = NULL;
}
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:
rc = libssh2_sftp_stat(sftp_scp->sftp_session, sshc->quote_path2,
&sshc->quote_attrs);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
James Housley
committed
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
else if (rc != 0) { /* get those attributes */
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
Curl_safefree(sshc->quote_path2);
sshc->quote_path2 = NULL;
failf(data, "Attempt to get SFTP stats failed: %s",
sftp_libssh2_strerror(err));
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
/* Now set the new attributes... */
if (curl_strnequal(sshc->quote_item->data, "chgrp", 5)) {
sshc->quote_attrs.gid = strtol(sshc->quote_path1, NULL, 10);
if (sshc->quote_attrs.gid == 0 && !ISDIGIT(sshc->quote_path1[0])) {
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
Curl_safefree(sshc->quote_path2);
sshc->quote_path2 = NULL;
failf(data, "Syntax error: chgrp gid not a number");
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
}
else if (curl_strnequal(sshc->quote_item->data, "chmod", 5)) {
sshc->quote_attrs.permissions = strtol(sshc->quote_path1, NULL, 8);
/* permissions are octal */
if (sshc->quote_attrs.permissions == 0 &&
!ISDIGIT(sshc->quote_path1[0])) {
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
Curl_safefree(sshc->quote_path2);
sshc->quote_path2 = NULL;
failf(data, "Syntax error: chmod permissions not a number");
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
}
else if (curl_strnequal(sshc->quote_item->data, "chown", 5)) {
sshc->quote_attrs.uid = strtol(sshc->quote_path1, NULL, 10);
if (sshc->quote_attrs.uid == 0 && !ISDIGIT(sshc->quote_path1[0])) {
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
Curl_safefree(sshc->quote_path2);
sshc->quote_path2 = NULL;
failf(data, "Syntax error: chown uid not a number");
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
}
/* Now send the completed structure... */
state(conn, SSH_SFTP_QUOTE_SETSTAT);
James Housley
committed
case SSH_SFTP_QUOTE_SETSTAT:
rc = libssh2_sftp_setstat(sftp_scp->sftp_session, sshc->quote_path2,
&sshc->quote_attrs);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
James Housley
committed
} else if (rc != 0) {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
Curl_safefree(sshc->quote_path2);
sshc->quote_path2 = NULL;
failf(data, "Attempt to set SFTP stats failed: %s",
sftp_libssh2_strerror(err));
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
James Housley
committed
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
state(conn, SSH_SFTP_NEXT_QUOTE);
break;
case SSH_SFTP_QUOTE_SYMLINK:
rc = libssh2_sftp_symlink(sftp_scp->sftp_session, sshc->quote_path1,
sshc->quote_path2);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc != 0) {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
Curl_safefree(sshc->quote_path2);
sshc->quote_path2 = NULL;
failf(data, "symlink command failed: %s",
sftp_libssh2_strerror(err));
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
state(conn, SSH_SFTP_NEXT_QUOTE);
break;
case SSH_SFTP_QUOTE_MKDIR:
rc = libssh2_sftp_mkdir(sftp_scp->sftp_session, sshc->quote_path1, 0755);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc != 0) {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
failf(data, "mkdir command failed: %s", sftp_libssh2_strerror(err));
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
state(conn, SSH_SFTP_NEXT_QUOTE);
break;
case SSH_SFTP_QUOTE_RENAME:
rc = libssh2_sftp_rename(sftp_scp->sftp_session, sshc->quote_path1,
sshc->quote_path2);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
} else if (rc != 0) {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
Curl_safefree(sshc->quote_path2);
sshc->quote_path2 = NULL;
failf(data, "rename command failed: %s", sftp_libssh2_strerror(err));
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
state(conn, SSH_SFTP_NEXT_QUOTE);
break;
case SSH_SFTP_QUOTE_RMDIR:
rc = libssh2_sftp_rmdir(sftp_scp->sftp_session, sshc->quote_path1);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc != 0) {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
failf(data, "rmdir command failed: %s", sftp_libssh2_strerror(err));
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
state(conn, SSH_SFTP_NEXT_QUOTE);
break;
case SSH_SFTP_QUOTE_UNLINK:
rc = libssh2_sftp_unlink(sftp_scp->sftp_session, sshc->quote_path1);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc != 0) {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
Curl_safefree(sshc->quote_path1);
sshc->quote_path1 = NULL;
failf(data, "rm command failed: %s", sftp_libssh2_strerror(err));
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_FTP_QUOTE_ERROR;
break;
}
state(conn, SSH_SFTP_NEXT_QUOTE);
break;
case SSH_SFTP_TRANS_INIT:
if (data->set.upload) {
state(conn, SSH_SFTP_UPLOAD_INIT);
break;
} else {
if (sftp_scp->path[strlen(sftp_scp->path)-1] == '/') {
state(conn, SSH_SFTP_READDIR_INIT);
break;
} else {
state(conn, SSH_SFTP_DOWNLOAD_INIT);
break;
}
}
break;
case SSH_SFTP_UPLOAD_INIT:
/*
* 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.
*/
sftp_scp->sftp_handle =
libssh2_sftp_open(sftp_scp->sftp_session, sftp_scp->path,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
data->set.new_file_perms);
if (!sftp_scp->sftp_handle) {
if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
LIBSSH2_ERROR_EAGAIN) {
break;
} else {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
if (sshc->secondCreateDirs) {
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = 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;
}
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = sftp_libssh2_error_to_CURLE(err);
break;
}
}
/* upload data */
result = Curl_setup_transfer(conn, -1, -1, FALSE, NULL,
FIRSTSOCKET, NULL);
if (result) {
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = result;
} else {
state(conn, SSH_STOP);
}
break;
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);
} else {
state(conn, SSH_SFTP_UPLOAD_INIT);
}
break;
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);
break;
} else {
state(conn, SSH_SFTP_UPLOAD_INIT);
}
break;
case SSH_SFTP_CREATE_DIRS_MKDIR:
/* 'mode' - parameter is preliminary - default to 0644 */
rc = libssh2_sftp_mkdir(sftp_scp->sftp_session, sftp_scp->path,
data->set.new_directory_perms);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
*sshc->slash_pos = '/';
++sshc->slash_pos;
if (rc == -1) {
unsigned int sftp_err = 0;
/*
* 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
*/
sftp_err = libssh2_sftp_last_error(sftp_scp->sftp_session);
if ((sftp_err != LIBSSH2_FX_FILE_ALREADY_EXISTS) &&
(sftp_err != LIBSSH2_FX_FAILURE) &&
(sftp_err != LIBSSH2_FX_PERMISSION_DENIED)) {
result = sftp_libssh2_error_to_CURLE(sftp_err);
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = result;
break;
}
}
state(conn, SSH_SFTP_CREATE_DIRS);
break;
case SSH_SFTP_READDIR_INIT:
/*
* This is a directory that we are trying to get, so produce a
* directory listing
*/
sftp_scp->sftp_handle = libssh2_sftp_opendir(sftp_scp->sftp_session,
sftp_scp->path);
if (!sftp_scp->sftp_handle) {
if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
LIBSSH2_ERROR_EAGAIN) {
break;
} else {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
failf(data, "Could not open directory for reading: %s",
sftp_libssh2_strerror(err));
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = sftp_libssh2_error_to_CURLE(err);
break;
}
}
if ((sshc->readdir_filename = (char *)malloc(PATH_MAX+1)) == NULL) {
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
if ((sshc->readdir_longentry = (char *)malloc(PATH_MAX+1)) == NULL) {
Curl_safefree(sshc->readdir_filename);
sshc->readdir_filename = NULL;
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
state(conn, SSH_SFTP_READDIR);
break;
case SSH_SFTP_READDIR:
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706151200L)
James Housley
committed
sshc->readdir_len = libssh2_sftp_readdir_ex(sftp_scp->sftp_handle,
sshc->readdir_filename,
PATH_MAX,
sshc->readdir_longentry,
PATH_MAX,
&sshc->readdir_attrs);
Guenter Knauf
committed
#else /* !(LIBSSH2_APINO >= 200706151200L) */
James Housley
committed
sshc->readdir_len = libssh2_sftp_readdir(sftp_scp->sftp_handle,
sshc->readdir_filename,
PATH_MAX,
&sshc->readdir_attrs);
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO >= 200706151200L) */
James Housley
committed
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
if (sshc->readdir_len == 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, 0);
Curl_safefree(tmpLine);
/* 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 {
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706151200L)
James Housley
committed
sshc->readdir_currLen = strlen(sshc->readdir_longentry);
sshc->readdir_totalLen = 80 + sshc->readdir_currLen;
sshc->readdir_line = (char *)calloc(sshc->readdir_totalLen, 1);
if (!sshc->readdir_line) {
Curl_safefree(sshc->readdir_filename);
sshc->readdir_filename = NULL;
Curl_safefree(sshc->readdir_longentry);
sshc->readdir_longentry = NULL;
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
memcpy(sshc->readdir_line, sshc->readdir_longentry,
sshc->readdir_currLen);
Guenter Knauf
committed
#else /* !(LIBSSH2_APINO >= 200706151200L) */
James Housley
committed
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
sshc->readdir_totalLen = 80 + sshc->readdir_len;
sshc->readdir_line = (char *)malloc(sshc->readdir_totalLen);
if (!sshc->readdir_line) {
Curl_safefree(sshc->readdir_filename);
sshc->readdir_filename = NULL;
Curl_safefree(sshc->readdir_longentry);
sshc->readdir_longentry = NULL;
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
if (!(sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID)) {
sshc->readdir_attrs.uid = sshc->readdir_attrs.gid = 0;
}
sshc->readdir_currLen = snprintf(sshc->readdir_line,
sshc->readdir_totalLen,
"---------- 1 %5d %5d",
sshc->readdir_attrs.uid,
sshc->readdir_attrs.gid);
if (sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
if ((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFDIR) {
sshc->readdir_line[0] = 'd';
}
else if ((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFLNK) {
sshc->readdir_line[0] = 'l';
}
else if ((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFSOCK) {
sshc->readdir_line[0] = 's';
}
else if ((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFCHR) {
sshc->readdir_line[0] = 'c';
}
else if ((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFBLK) {
sshc->readdir_line[0] = 'b';
}
if (sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IRUSR) {
sshc->readdir_line[1] = 'r';
}
if (sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IWUSR) {
sshc->readdir_line[2] = 'w';
}
if (sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IXUSR) {
sshc->readdir_line[3] = 'x';
}
if (sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IRGRP) {
sshc->readdir_line[4] = 'r';
}
if (sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IWGRP) {
sshc->readdir_line[5] = 'w';
}
if (sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IXGRP) {
sshc->readdir_line[6] = 'x';
}
if (sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IROTH) {
sshc->readdir_line[7] = 'r';
}
if (sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IWOTH) {
sshc->readdir_line[8] = 'w';
}
if (sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IXOTH) {
sshc->readdir_line[9] = 'x';
}
}
if (sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
sshc->readdir_currLen += snprintf(sshc->readdir_line +
sshc->readdir_currLen,
sshc->readdir_totalLen -
sshc->readdir_currLen,
"%11lld",
sshc->readdir_attrs.filesize);
}
if (sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
static const char * const months[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
struct tm *nowParts;
time_t now, remoteTime;
now = time(NULL);
remoteTime = (time_t)sshc->readdir_attrs.mtime;
nowParts = localtime(&remoteTime);
if ((time_t)sshc->readdir_attrs.mtime > (now - (86400 * 180))) {
sshc->readdir_currLen += snprintf(sshc->readdir_line +
sshc->readdir_currLen,
sshc->readdir_totalLen -
sshc->readdir_currLen,
" %s %2d %2d:%02d",
months[nowParts->tm_mon],
nowParts->tm_mday,
nowParts->tm_hour,
nowParts->tm_min);
} else {
sshc->readdir_currLen += snprintf(sshc->readdir_line +
sshc->readdir_currLen,
sshc->readdir_totalLen -
sshc->readdir_currLen,
" %s %2d %5d",
months[nowParts->tm_mon],
nowParts->tm_mday,
1900+nowParts->tm_year);
}
}
sshc->readdir_currLen += snprintf(sshc->readdir_line +
sshc->readdir_currLen,
sshc->readdir_totalLen -
sshc->readdir_currLen, " %s",
sshc->readdir_filename);
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO >= 200706151200L) */
James Housley
committed
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
if ((sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFLNK)) {
sshc->readdir_linkPath = (char *)malloc(PATH_MAX + 1);
if (sshc->readdir_linkPath == NULL) {
Curl_safefree(sshc->readdir_filename);
sshc->readdir_filename = NULL;
Curl_safefree(sshc->readdir_longentry);
sshc->readdir_longentry = NULL;
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);
break;
}
}
else if (sshc->readdir_len == 0) {
Curl_safefree(sshc->readdir_filename);
sshc->readdir_filename = NULL;
Curl_safefree(sshc->readdir_longentry);
sshc->readdir_longentry = NULL;
state(conn, SSH_SFTP_READDIR_DONE);
break;
}
else if (sshc->readdir_len <= 0) {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
sshc->actualCode = err;
failf(data, "Could not open remote file for reading: %s :: %d",
sftp_libssh2_strerror(err),
libssh2_session_last_errno(sftp_scp->ssh_session));
Curl_safefree(sshc->readdir_filename);
sshc->readdir_filename = NULL;
Curl_safefree(sshc->readdir_longentry);
sshc->readdir_longentry = NULL;
state(conn, SSH_SFTP_CLOSE);
break;
}
break;
case SSH_SFTP_READDIR_LINK:
sshc->readdir_len = libssh2_sftp_readlink(sftp_scp->sftp_session,
sshc->readdir_linkPath,
sshc->readdir_filename,
PATH_MAX);
if (sshc->readdir_len == LIBSSH2_ERROR_EAGAIN) {
break;
}
Curl_safefree(sshc->readdir_linkPath);
sshc->readdir_linkPath = NULL;
sshc->readdir_line = realloc(sshc->readdir_line,
sshc->readdir_totalLen + 4 +
sshc->readdir_len);
if (!sshc->readdir_line) {
Curl_safefree(sshc->readdir_filename);
sshc->readdir_filename = NULL;
Curl_safefree(sshc->readdir_longentry);
sshc->readdir_longentry = NULL;
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = CURLE_OUT_OF_MEMORY;
break;
}
sshc->readdir_currLen += snprintf(sshc->readdir_line +
sshc->readdir_currLen,
sshc->readdir_totalLen -
sshc->readdir_currLen,
" -> %s",
sshc->readdir_filename);
state(conn, SSH_SFTP_READDIR_BOTTOM);
break;
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, 0);
/* output debug output if that is requested */
if (data->set.verbose) {
Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_line,
sshc->readdir_currLen, conn);
}
Curl_safefree(sshc->readdir_line);
sshc->readdir_line = NULL;
state(conn, SSH_SFTP_READDIR);
break;
case SSH_SFTP_READDIR_DONE:
if (libssh2_sftp_closedir(sftp_scp->sftp_handle) ==
LIBSSH2_ERROR_EAGAIN) {
break;
}
sftp_scp->sftp_handle = NULL;
Curl_safefree(sshc->readdir_filename);
sshc->readdir_filename = NULL;
Curl_safefree(sshc->readdir_longentry);
sshc->readdir_longentry = NULL;
/* no data to transfer */
result = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
state(conn, SSH_STOP);
break;
case SSH_SFTP_DOWNLOAD_INIT:
/*
* Work on getting the specified file
*/
sftp_scp->sftp_handle =
libssh2_sftp_open(sftp_scp->sftp_session, sftp_scp->path,
LIBSSH2_FXF_READ, data->set.new_file_perms);
if (!sftp_scp->sftp_handle) {
if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
LIBSSH2_ERROR_EAGAIN) {
break;
} else {
err = libssh2_sftp_last_error(sftp_scp->sftp_session);
failf(data, "Could not open remote file for reading: %s",
sftp_libssh2_strerror(err));
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = sftp_libssh2_error_to_CURLE(err);
break;
}
}
state(conn, SSH_SFTP_DOWNLOAD_STAT);
break;
case SSH_SFTP_DOWNLOAD_STAT:
{
LIBSSH2_SFTP_ATTRIBUTES attrs;
rc = libssh2_sftp_stat(sftp_scp->sftp_session, sftp_scp->path, &attrs);
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->reqdata.size = -1;
data->reqdata.maxdownload = -1;
} else {
data->reqdata.size = attrs.filesize;
data->reqdata.maxdownload = attrs.filesize;
Curl_pgrsSetDownloadSize(data, attrs.filesize);
}
}
/* Setup the actual download */
result = Curl_setup_transfer(conn, FIRSTSOCKET, data->reqdata.size,
FALSE, NULL, -1, NULL);
if (result) {
state(conn, SSH_SFTP_CLOSE);
sshc->actualCode = result;
} else {
state(conn, SSH_STOP);
}
break;
case SSH_SFTP_CLOSE:
if (sftp_scp->sftp_handle) {
rc = libssh2_sftp_close(sftp_scp->sftp_handle);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc < 0) {
infof(data, "Failed to close libssh2 file\n");
}
sftp_scp->sftp_handle = NULL;
}
state(conn, SSH_SFTP_SHUTDOWN);
break;
case SSH_SFTP_SHUTDOWN:
if (sftp_scp->sftp_session) {
rc = libssh2_sftp_shutdown(sftp_scp->sftp_session);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc < 0) {
infof(data, "Failed to stop libssh2 sftp subsystem\n");
}
sftp_scp->sftp_session = NULL;
}
Curl_safefree(sftp_scp->path);
sftp_scp->path = NULL;
Curl_safefree(sftp_scp->homedir);
sftp_scp->homedir = NULL;
state(conn, SSH_CHANNEL_CLOSE);
break;
case SSH_SCP_TRANS_INIT:
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);
} else {
state(conn, SSH_SCP_DOWNLOAD_INIT);
}
break;
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.
*/
sftp_scp->ssh_channel =
libssh2_scp_send_ex(sftp_scp->ssh_session, sftp_scp->path,
data->set.new_file_perms,
data->set.infilesize, 0, 0);
if (!sftp_scp->ssh_channel) {
if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
LIBSSH2_ERROR_EAGAIN) {
break;
} else {
int ssh_err;
char *err_msg;
ssh_err = libssh2_session_last_error(sftp_scp->ssh_session,
&err_msg, NULL, 0);
err = libssh2_session_error_to_CURLE(ssh_err);
failf(conn->data, "%s", err_msg);
state(conn, SSH_SCP_CHANNEL_FREE);
sshc->actualCode = err;
break;
}
}
/* upload data */
result = Curl_setup_transfer(conn, -1, data->reqdata.size, FALSE, NULL,
FIRSTSOCKET, NULL);
if (result) {
state(conn, SSH_SCP_CHANNEL_FREE);
sshc->actualCode = result;
} else {
state(conn, SSH_STOP);
}
break;
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;
memset(&sb, 0, sizeof(struct stat));
sftp_scp->ssh_channel = libssh2_scp_recv(sftp_scp->ssh_session,
sftp_scp->path, &sb);
if (!sftp_scp->ssh_channel) {
if (libssh2_session_last_errno(sftp_scp->ssh_session) ==
LIBSSH2_ERROR_EAGAIN) {
break;
} else {
int ssh_err;
char *err_msg;
ssh_err = libssh2_session_last_error(sftp_scp->ssh_session,
&err_msg, NULL, 0);
err = libssh2_session_error_to_CURLE(ssh_err);
failf(conn->data, "%s", err_msg);
state(conn, SSH_SCP_CHANNEL_FREE);
sshc->actualCode = err;
break;
}
}
/* download data */
bytecount = (curl_off_t)sb.st_size;
data->reqdata.maxdownload = (curl_off_t)sb.st_size;
result = Curl_setup_transfer(conn, FIRSTSOCKET,
bytecount, FALSE, NULL, -1, NULL);
if (result) {
state(conn, SSH_SCP_CHANNEL_FREE);
sshc->actualCode = result;
} else {
state(conn, SSH_STOP);
}
}
break;
case SSH_SCP_DONE:
if (data->set.upload) {
state(conn, SSH_SCP_SEND_EOF);
} else {
state(conn, SSH_SCP_CHANNEL_FREE);
}
break;
case SSH_SCP_SEND_EOF:
if (sftp_scp->ssh_channel) {
rc = libssh2_channel_send_eof(sftp_scp->ssh_channel);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc) {
infof(data, "Failed to send libssh2 channel EOF\n");
}
}
state(conn, SSH_SCP_WAIT_EOF);
break;
case SSH_SCP_WAIT_EOF:
if (sftp_scp->ssh_channel) {
rc = libssh2_channel_wait_eof(sftp_scp->ssh_channel);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc) {
infof(data, "Failed to get channel EOF\n");
}
}
state(conn, SSH_SCP_WAIT_CLOSE);
break;
case SSH_SCP_WAIT_CLOSE:
if (sftp_scp->ssh_channel) {
rc = libssh2_channel_wait_closed(sftp_scp->ssh_channel);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc) {
infof(data, "Channel failed to close\n");
}
}
state(conn, SSH_SCP_CHANNEL_FREE);
break;
case SSH_SCP_CHANNEL_FREE:
if (sftp_scp->ssh_channel) {
rc = libssh2_channel_free(sftp_scp->ssh_channel);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc < 0) {
infof(data, "Failed to free libssh2 scp subsystem\n");
}
sftp_scp->ssh_channel = NULL;
}
state(conn, SSH_SESSION_DISCONECT);
break;
case SSH_CHANNEL_CLOSE:
if (sftp_scp->ssh_channel) {
rc = libssh2_channel_close(sftp_scp->ssh_channel);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc < 0) {
infof(data, "Failed to stop libssh2 channel subsystem\n");
}
sftp_scp->ssh_channel = NULL;
}
state(conn, SSH_SESSION_DISCONECT);
break;
case SSH_SESSION_DISCONECT:
if (sftp_scp->ssh_session) {
rc = libssh2_session_disconnect(sftp_scp->ssh_session, "Shutdown");
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc < 0) {
infof(data, "Failed to disconnect libssh2 session\n");
}
}
Curl_safefree(sftp_scp->path);
sftp_scp->path = NULL;
Curl_safefree(sftp_scp->homedir);
sftp_scp->homedir = NULL;
state(conn, SSH_SESSION_FREE);
break;
case SSH_SESSION_FREE:
if (sftp_scp->ssh_session) {
rc = libssh2_session_free(sftp_scp->ssh_session);
if (rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if (rc < 0) {
infof(data, "Failed to free libssh2 session\n");
}
sftp_scp->ssh_session = NULL;
}
sshc->nextState = SSH_NO_STATE;
state(conn, SSH_STOP);
result = sshc->actualCode;
break;
case SSH_QUIT:
/* fallthrough, just stop! */
default:
/* internal error */
James Housley
committed
sshc->nextState = SSH_NO_STATE;
state(conn, SSH_STOP);
break;
}
return result;
}
/* called repeatedly until done from multi.c */
CURLcode Curl_ssh_multi_statemach(struct connectdata *conn,
bool *done)
{
struct ssh_conn *sshc = &conn->proto.sshc;
CURLcode result = CURLE_OK;
James Housley
committed
result = ssh_statemach_act(conn);
*done = (bool)(sshc->state == SSH_STOP);
return result;
}
static CURLcode ssh_easy_statemach(struct connectdata *conn)
{
struct ssh_conn *sshc = &conn->proto.sshc;
CURLcode result = CURLE_OK;
James Housley
committed
while (sshc->state != SSH_STOP) {
result = ssh_statemach_act(conn);
if (result) {
break;
}
}
James Housley
committed
return result;
Guenter Knauf
committed
#endif /* (LIBSSH2_APINO >= 200706012030L) */
/*
* SSH setup and connection
*/
static CURLcode ssh_init(struct connectdata *conn)
{
struct SessionHandle *data = conn->data;
struct SSHPROTO *ssh;
if (data->reqdata.proto.ssh)
return CURLE_OK;
ssh = (struct SSHPROTO *)calloc(sizeof(struct SSHPROTO), 1);
if (!ssh)
return CURLE_OUT_OF_MEMORY;
data->reqdata.proto.ssh = ssh;
/* get some initial data into the ssh struct */
ssh->bytecountp = &data->reqdata.keep.bytecount;
/* no need to duplicate them, this connectdata struct won't change */
ssh->user = conn->user;
ssh->passwd = conn->passwd;
ssh->errorstr = NULL;
ssh->ssh_session = NULL;
ssh->ssh_channel = NULL;
ssh->sftp_session = NULL;
ssh->sftp_handle = NULL;
return CURLE_OK;
}
/*
* Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to
* do protocol-specific actions at connect-time.
*/
CURLcode Curl_ssh_connect(struct connectdata *conn, bool *done)
struct SSHPROTO *ssh;
const char *fingerprint;
const char *authlist;
char tempHome[PATH_MAX];
curl_socket_t sock;
char *real_path;
char *working_path;
int working_path_len;
bool authed = FALSE;
CURLcode result;
struct SessionHandle *data = conn->data;
result = ssh_init(conn);
if (result)
return result;
ssh = data->reqdata.proto.ssh;
#ifdef CURL_LIBSSH2_DEBUG
if (ssh->user) {
infof(data, "User: %s\n", ssh->user);
if (ssh->passwd) {
infof(data, "Password: %s\n", ssh->passwd);
}
#endif /* CURL_LIBSSH2_DEBUG */
sock = conn->sock[FIRSTSOCKET];
ssh->ssh_session = libssh2_session_init_ex(libssh2_malloc, libssh2_free,
James Housley
committed
libssh2_realloc, ssh);
if (ssh->ssh_session == NULL) {
failf(data, "Failure initialising ssh session");
return CURLE_FAILED_INIT;
}
#ifdef CURL_LIBSSH2_DEBUG
libssh2_trace(ssh->ssh_session, LIBSSH2_TRACE_CONN|LIBSSH2_TRACE_TRANS|
LIBSSH2_TRACE_KEX|LIBSSH2_TRACE_AUTH|LIBSSH2_TRACE_SCP|
LIBSSH2_TRACE_SFTP|LIBSSH2_TRACE_ERROR|
LIBSSH2_TRACE_PUBLICKEY);
infof(data, "SSH socket: %d\n", sock);
#endif /* CURL_LIBSSH2_DEBUG */
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
state(conn, SSH_S_STARTUP);
if (data->state.used_interface == Curl_if_multi)
result = Curl_ssh_multi_statemach(conn, done);
else {
result = ssh_easy_statemach(conn);
if (!result)
*done = TRUE;
}
return result;
(void)authed; /* not used */
(void)working_path; /* not used */
(void)working_path_len; /* not used */
(void)real_path; /* not used */
(void)tempHome; /* not used */
(void)authlist; /* not used */
(void)fingerprint; /* not used */
(void)i; /* not used */
Guenter Knauf
committed
#else /* !(LIBSSH2_APINO >= 200706012030L) */
if (libssh2_session_startup(ssh->ssh_session, sock)) {
failf(data, "Failure establishing ssh session");
libssh2_session_free(ssh->ssh_session);
ssh->ssh_session = NULL;
return CURLE_FAILED_INIT;
}
/*
* Before we authenticate we should check the hostkey's fingerprint against
* our known hosts. How that is handled (reading from file, whatever) is
* up to us. As for know not much is implemented, besides showing how to
* get the fingerprint.
*/
fingerprint = libssh2_hostkey_hash(ssh->ssh_session,
LIBSSH2_HOSTKEY_HASH_MD5);
#ifdef CURL_LIBSSH2_DEBUG
/* The fingerprint points to static storage (!), don't free() it. */
for (i = 0; i < 16; i++) {
infof(data, "%02X ", (unsigned char) fingerprint[i]);
}
infof(data, "\n");
#endif /* CURL_LIBSSH2_DEBUG */
/* TBD - methods to check the host keys need to be done */
/*
* 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.
*/
authlist = libssh2_userauth_list(ssh->ssh_session, ssh->user,
strlen(ssh->user));
if (!authlist) {
libssh2_session_free(ssh->ssh_session);
ssh->ssh_session = NULL;
return CURLE_OUT_OF_MEMORY;
}
infof(data, "SSH authentication methods available: %s\n", authlist);
James Housley
committed
* Check the supported auth types in the order I feel is most secure with
* the requested type of authentication
*/
if ((data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) &&
(strstr(authlist, "publickey") != NULL)) {
const char *passphrase;
char rsa_pub[PATH_MAX];
char rsa[PATH_MAX];
rsa_pub[0] = rsa[0] = '\0';
/* To ponder about: should really the lib be messing about with the HOME
environment variable etc? */
home = curl_getenv("HOME");
if (data->set.ssh_public_key)
snprintf(rsa_pub, sizeof(rsa_pub), "%s", data->set.ssh_public_key);
else if (home)
snprintf(rsa_pub, sizeof(rsa_pub), "%s/.ssh/id_dsa.pub", home);
if (data->set.ssh_private_key)
snprintf(rsa, sizeof(rsa), "%s", data->set.ssh_private_key);
else if (home)
snprintf(rsa, sizeof(rsa), "%s/.ssh/id_dsa", home);
passphrase = data->set.key_passwd;
if (!passphrase)
passphrase = "";
James Housley
committed
Curl_safefree(home);
James Housley
committed
infof(data, "Using ssh public key file %s\n", rsa_pub);
infof(data, "Using ssh private key file %s\n", rsa);
if (rsa_pub[0]) {
/* The function below checks if the files exists, no need to stat() here.
if (libssh2_userauth_publickey_fromfile(ssh->ssh_session, ssh->user,
rsa_pub, rsa, passphrase) == 0) {
James Housley
committed
infof(data, "Initialized SSH public key authentication\n");
}
}
}
if (!authed &&
(data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD) &&
(strstr(authlist, "password") != NULL)) {
if (!libssh2_userauth_password(ssh->ssh_session, ssh->user, ssh->passwd)) {
James Housley
committed
infof(data, "Initialized password authentication\n");
}
if (!authed && (data->set.ssh_auth_types & CURLSSH_AUTH_HOST) &&
(strstr(authlist, "hostbased") != NULL)) {
}
if (!authed && (data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD)
&& (strstr(authlist, "keyboard-interactive") != NULL)) {
/* Authentication failed. Continue with keyboard-interactive now. */
if (!libssh2_userauth_keyboard_interactive_ex(ssh->ssh_session, ssh->user,
strlen(ssh->user),
&kbd_callback)) {
James Housley
committed
infof(data, "Initialized keyboard interactive authentication\n");
Curl_safefree((void *)authlist);
authlist = NULL;
failf(data, "Authentication failure");
libssh2_session_free(ssh->ssh_session);
ssh->ssh_session = NULL;
}
/*
* At this point we have an authenticated ssh session.
*/
James Housley
committed
infof(data, "Authentication complete\n");
conn->sockfd = sock;
conn->writesockfd = CURL_SOCKET_BAD;
if (conn->protocol == PROT_SFTP) {
/*
* Start the libssh2 sftp session
*/
ssh->sftp_session = libssh2_sftp_init(ssh->ssh_session);
if (ssh->sftp_session == NULL) {
failf(data, "Failure initialising sftp session\n");
libssh2_session_free(ssh->ssh_session);
ssh->ssh_session = NULL;
return CURLE_FAILED_INIT;
}
/*
* Get the "home" directory
*/
i = libssh2_sftp_realpath(ssh->sftp_session, ".", tempHome, PATH_MAX-1);
if (i > 0) {
/* It seems that this string is not always NULL terminated */
tempHome[i] = '\0';
ssh->homedir = (char *)strdup(tempHome);
if (!ssh->homedir) {
libssh2_sftp_shutdown(ssh->sftp_session);
ssh->sftp_session = NULL;
libssh2_session_free(ssh->ssh_session);
ssh->ssh_session = NULL;
return CURLE_OUT_OF_MEMORY;
}
}
else {
/* Return the error type */
i = libssh2_sftp_last_error(ssh->sftp_session);
DEBUGF(infof(data, "error = %d\n", i));
}
}
working_path = curl_easy_unescape(data, data->reqdata.path, 0,
&working_path_len);
if (!working_path)
return CURLE_OUT_OF_MEMORY;
/* Check for /~/ , indicating relative to the user's home directory */
if (conn->protocol == PROT_SCP) {
real_path = (char *)malloc(working_path_len+1);
if (real_path == NULL) {
libssh2_session_free(ssh->ssh_session);
ssh->ssh_session = NULL;
Curl_safefree(working_path);
return CURLE_OUT_OF_MEMORY;
}
if (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);
}
else if (conn->protocol == PROT_SFTP) {
if (working_path[1] == '~') {
real_path = (char *)malloc(strlen(ssh->homedir) +
working_path_len + 1);
if (real_path == NULL) {
libssh2_sftp_shutdown(ssh->sftp_session);
ssh->sftp_session = NULL;
libssh2_session_free(ssh->ssh_session);
ssh->ssh_session = NULL;
Curl_safefree(ssh->homedir);
ssh->homedir = NULL;
Curl_safefree(working_path);
return CURLE_OUT_OF_MEMORY;
}
/* It is referenced to the home directory, so strip the leading '/' */
memcpy(real_path, ssh->homedir, strlen(ssh->homedir));
real_path[strlen(ssh->homedir)] = '/';
real_path[strlen(ssh->homedir)+1] = '\0';
if (working_path_len > 3) {
memcpy(real_path+strlen(ssh->homedir)+1, working_path + 3,
1 + working_path_len -3);
}
}
else {
real_path = (char *)malloc(working_path_len+1);
if (real_path == NULL) {
libssh2_sftp_shutdown(ssh->sftp_session);
ssh->sftp_session = NULL;
libssh2_session_free(ssh->ssh_session);
ssh->ssh_session = NULL;
Curl_safefree(ssh->homedir);
ssh->homedir = NULL;
Curl_safefree(working_path);
return CURLE_OUT_OF_MEMORY;
}
memcpy(real_path, working_path, 1+working_path_len);
}
}
else {
libssh2_session_free(ssh->ssh_session);
ssh->ssh_session = NULL;
Curl_safefree(working_path);
return CURLE_FAILED_INIT;
Curl_safefree(working_path);
ssh->path = real_path;
*done = TRUE;
return CURLE_OK;
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO >= 200706012030L) */
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
James Housley
committed
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
/*
***********************************************************************
*
* 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 */
if (conn->data->state.used_interface == Curl_if_multi) {
result = Curl_ssh_multi_statemach(conn, dophase_done);
} else {
result = ssh_easy_statemach(conn);
*dophase_done = TRUE; /* with the easy interface we are done here */
}
*connected = conn->bits.tcpconnect;
if (*dophase_done) {
DEBUGF(infof(conn->data, "DO phase is complete\n"));
}
return result;
}
/* called from multi.c while DOing */
CURLcode Curl_scp_doing(struct connectdata *conn,
bool *dophase_done)
{
CURLcode result;
result = Curl_ssh_multi_statemach(conn, dophase_done);
if (*dophase_done) {
DEBUGF(infof(conn->data, "DO phase is complete\n"));
}
return result;
}
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO >= 200706012030L) */
James Housley
committed
CURLcode Curl_scp_do(struct connectdata *conn, bool *done)
{
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
James Housley
committed
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
CURLcode res;
bool connected = 0;
struct SessionHandle *data = conn->data;
*done = FALSE; /* default to false */
/*
* 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
* Curl_ssh_connect() function.
*/
res = ssh_init(conn);
if (res) {
return res;
}
data->reqdata.size = -1; /* make sure this is unknown at this point */
Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0);
Curl_pgrsSetUploadSize(data, 0);
Curl_pgrsSetDownloadSize(data, 0);
res = scp_perform(conn, &connected, done);
if (CURLE_OK == res) {
if (!done) {
/* the DO phase has not completed yet */
return CURLE_OK;
}
}
return res;
Guenter Knauf
committed
#else /* !(LIBSSH2_APINO >= 200706012030L) */
James Housley
committed
struct SSHPROTO *scp = conn->data->reqdata.proto.ssh;
CURLcode res = CURLE_OK;
*done = TRUE; /* unconditionally */
if (conn->data->set.upload) {
James Housley
committed
if (conn->data->set.infilesize < 0) {
failf(conn->data, "SCP requires a known file size for upload");
return CURLE_UPLOAD_FAILED;
}
* 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
James Housley
committed
* in the path.
*/
scp->ssh_channel = libssh2_scp_send_ex(scp->ssh_session, scp->path,
conn->data->set.new_file_perms,
if (!scp->ssh_channel)
return CURLE_FAILED_INIT;
/* upload data */
res = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
James Housley
committed
} else {
* We must check the remote file; if it is a directory no values will
* be set in sb
curl_off_t bytecount;
memset(&sb, 0, sizeof(struct stat));
scp->ssh_channel = libssh2_scp_recv(scp->ssh_session, scp->path, &sb);
if (!scp->ssh_channel) {
if ((sb.st_mode == 0) && (sb.st_atime == 0) && (sb.st_mtime == 0) &&
(sb.st_size == 0)) {
/* Since sb is still empty, it is likely the file was not found */
return CURLE_REMOTE_FILE_NOT_FOUND;
}
return libssh2_session_error_to_CURLE(
libssh2_session_last_error(scp->ssh_session, NULL, NULL, 0));
/* download data */
bytecount = (curl_off_t) sb.st_size;
conn->data->reqdata.maxdownload = (curl_off_t) sb.st_size;
res = Curl_setup_transfer(conn, FIRSTSOCKET,
bytecount, FALSE, NULL, -1, NULL);
}
return res;
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO >= 200706012030L) */
Daniel Stenberg
committed
CURLcode Curl_scp_done(struct connectdata *conn, CURLcode status,
bool premature)
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
James Housley
committed
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
CURLcode result = CURLE_OK;
bool done = FALSE;
if (status == CURLE_OK) {
state(conn, SSH_SCP_DONE);
/* run the state-machine */
if (conn->data->state.used_interface == Curl_if_multi) {
result = Curl_ssh_multi_statemach(conn, &done);
} else {
result = ssh_easy_statemach(conn);
done = TRUE;
}
} else {
result = status;
done = TRUE;
}
if (done) {
Curl_safefree(conn->data->reqdata.proto.ssh);
conn->data->reqdata.proto.ssh = NULL;
Curl_pgrsDone(conn);
}
return result;
Guenter Knauf
committed
#else /* !(LIBSSH2_APINO >= 200706012030L) */
James Housley
committed
struct SSHPROTO *scp = conn->data->reqdata.proto.ssh;
Curl_safefree(scp->path);
scp->path = NULL;
if (scp->ssh_channel) {
if (conn->data->set.upload &&
libssh2_channel_send_eof(scp->ssh_channel) < 0) {
infof(conn->data, "Failed to send libssh2 channel EOF\n");
}
if (libssh2_channel_close(scp->ssh_channel) < 0) {
infof(conn->data, "Failed to stop libssh2 channel subsystem\n");
libssh2_channel_free(scp->ssh_channel);
if (scp->ssh_session) {
libssh2_session_disconnect(scp->ssh_session, "Shutdown");
libssh2_session_free(scp->ssh_session);
scp->ssh_session = NULL;
James Housley
committed
Curl_safefree(conn->data->reqdata.proto.ssh);
conn->data->reqdata.proto.ssh = NULL;
Curl_pgrsDone(conn);
James Housley
committed
return CURLE_OK;
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO >= 200706012030L) */
James Housley
committed
(void)premature; /* not used */
(void)status; /* unused */
}
/* return number of received (decrypted) bytes */
ssize_t Curl_scp_send(struct connectdata *conn, int sockindex,
void *mem, size_t len)
{
ssize_t nwrite;
/* libssh2_channel_write() returns int
*
* NOTE: we should not store nor rely on connection-related data to be
* in the SessionHandle struct
*/
Guenter Knauf
committed
#if defined(LIBSSH2CHANNEL_EAGAIN) && (LIBSSH2_APINO < 200706012030L)
nwrite = (ssize_t)
libssh2_channel_writenb(conn->data->reqdata.proto.ssh->ssh_channel,
mem, len);
#else
nwrite = (ssize_t)
libssh2_channel_write(conn->data->reqdata.proto.ssh->ssh_channel,
mem, len);
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
if (nwrite == LIBSSH2_ERROR_EAGAIN) {
return 0;
}
#endif
(void)sockindex;
return nwrite;
}
/*
* If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
* a regular CURLcode value.
*/
ssize_t Curl_scp_recv(struct connectdata *conn, int sockindex,
char *mem, size_t len)
{
ssize_t nread;
(void)sockindex; /* we only support SCP on the fixed known primary socket */
/* libssh2_channel_read() returns int
*
* NOTE: we should not store nor rely on connection-related data to be
* in the SessionHandle struct
*/
Guenter Knauf
committed
#if defined(LIBSSH2CHANNEL_EAGAIN) && (LIBSSH2_APINO < 200706012030L)
/* we prefer the non-blocking API but that didn't exist previously */
nread = (ssize_t)
libssh2_channel_readnb(conn->data->reqdata.proto.ssh->ssh_channel,
mem, len);
#else
nread = (ssize_t)
libssh2_channel_read(conn->data->reqdata.proto.ssh->ssh_channel,
mem, len);
return nread;
}
/*
* =============== SFTP ===============
*/
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
James Housley
committed
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
/*
***********************************************************************
*
* 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 */
if (conn->data->state.used_interface == Curl_if_multi) {
result = Curl_ssh_multi_statemach(conn, dophase_done);
} else {
result = ssh_easy_statemach(conn);
*dophase_done = TRUE; /* with the easy interface we are done here */
}
*connected = conn->bits.tcpconnect;
if (*dophase_done) {
DEBUGF(infof(conn->data, "DO phase is complete\n"));
}
return result;
}
/* called from multi.c while DOing */
CURLcode Curl_sftp_doing(struct connectdata *conn,
bool *dophase_done)
{
CURLcode result;
result = Curl_ssh_multi_statemach(conn, dophase_done);
if (*dophase_done) {
DEBUGF(infof(conn->data, "DO phase is complete\n"));
}
return result;
}
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO >= 200706012030L) */
James Housley
committed
CURLcode Curl_sftp_do(struct connectdata *conn, bool *done)
{
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
James Housley
committed
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
CURLcode res;
bool connected = 0;
struct SessionHandle *data = conn->data;
*done = FALSE; /* default to false */
/*
* 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
* Curl_ssh_connect() function.
*/
res = ssh_init(conn);
if (res) {
return res;
}
data->reqdata.size = -1; /* make sure this is unknown at this point */
Curl_pgrsSetUploadCounter(data, 0);
Curl_pgrsSetDownloadCounter(data, 0);
Curl_pgrsSetUploadSize(data, 0);
Curl_pgrsSetDownloadSize(data, 0);
res = sftp_perform(conn, &connected, done);
if (CURLE_OK == res) {
if (!done) {
/* the DO phase has not completed yet */
return CURLE_OK;
}
}
return res;
Guenter Knauf
committed
#else /* !(LIBSSH2_APINO >= 200706012030L) */
James Housley
committed
LIBSSH2_SFTP_ATTRIBUTES attrs;
struct SSHPROTO *sftp = conn->data->reqdata.proto.ssh;
CURLcode res = CURLE_OK;
struct SessionHandle *data = conn->data;
curl_off_t bytecount = 0;
char *buf = data->state.buffer;
*done = TRUE; /* unconditionally */
/* Send any quote commands */
James Housley
committed
if (conn->data->set.quote) {
infof(conn->data, "Sending quote commands\n");
res = sftp_sendquote(conn, conn->data->set.quote);
if (res != CURLE_OK)
return res;
}
if (data->set.upload) {
/*
* 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.
*/
sftp->sftp_handle =
libssh2_sftp_open(sftp->sftp_session, sftp->path,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
James Housley
committed
conn->data->set.new_file_perms);
if (!sftp->sftp_handle) {
err = libssh2_sftp_last_error(sftp->sftp_session);
if (((err == LIBSSH2_FX_NO_SUCH_FILE) ||
(err == LIBSSH2_FX_FAILURE) ||
(err == LIBSSH2_FX_NO_SUCH_PATH)) &&
(conn->data->set.ftp_create_missing_dirs &&
(strlen(sftp->path) > 1))) {
/* try to create the path remotely */
res = sftp_create_dirs(conn);
if (res == 0) {
sftp->sftp_handle = libssh2_sftp_open(sftp->sftp_session, sftp->path,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
James Housley
committed
conn->data->set.new_file_perms);
}
}
if (!sftp->sftp_handle) {
err = libssh2_sftp_last_error(sftp->sftp_session);
failf(conn->data, "Could not open remote file for writing: %s",
sftp_libssh2_strerror(err));
return sftp_libssh2_error_to_CURLE(err);
}
/* upload data */
res = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
}
else {
if (sftp->path[strlen(sftp->path)-1] == '/') {
/*
* This is a directory that we are trying to get, so produce a
* directory listing
*
* **BLOCKING behaviour** This should be made into a state machine and
* get a separate function called from Curl_sftp_recv() when there is
* data to read from the network, instead of "hanging" here.
*/
char filename[PATH_MAX+1];
int len, totalLen, currLen;
char *line;
sftp->sftp_handle =
libssh2_sftp_opendir(sftp->sftp_session, sftp->path);
if (!sftp->sftp_handle) {
err = libssh2_sftp_last_error(sftp->sftp_session);
failf(conn->data, "Could not open directory for reading: %s",
sftp_libssh2_strerror(err));
return sftp_libssh2_error_to_CURLE(err);
}
do {
len = libssh2_sftp_readdir(sftp->sftp_handle, filename,
PATH_MAX, &attrs);
if (len > 0) {
filename[len] = '\0';
if (data->set.ftp_list_only) {
char *tmpLine;
tmpLine = aprintf("%s\n", filename);
if (tmpLine == NULL) {
return CURLE_OUT_OF_MEMORY;
}
res = Curl_client_write(conn, CLIENTWRITE_BODY, tmpLine, 0);
Curl_safefree(tmpLine);
James Housley
committed
} else {
totalLen = 80 + len;
line = (char *)malloc(totalLen);
if (!line)
return CURLE_OUT_OF_MEMORY;
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
if (!(attrs.flags & LIBSSH2_SFTP_ATTR_UIDGID))
attrs.uid = attrs.gid =0;
currLen = snprintf(line, totalLen, "---------- 1 %5d %5d",
attrs.uid, attrs.gid);
if (attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) {
if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFDIR) {
line[0] = 'd';
}
else if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFLNK) {
line[0] = 'l';
}
else if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFSOCK) {
line[0] = 's';
}
else if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFCHR) {
line[0] = 'c';
}
else if ((attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFBLK) {
line[0] = 'b';
}
if (attrs.permissions & LIBSSH2_SFTP_S_IRUSR) {
line[1] = 'r';
}
if (attrs.permissions & LIBSSH2_SFTP_S_IWUSR) {
line[2] = 'w';
}
if (attrs.permissions & LIBSSH2_SFTP_S_IXUSR) {
line[3] = 'x';
}
if (attrs.permissions & LIBSSH2_SFTP_S_IRGRP) {
line[4] = 'r';
}
if (attrs.permissions & LIBSSH2_SFTP_S_IWGRP) {
line[5] = 'w';
}
if (attrs.permissions & LIBSSH2_SFTP_S_IXGRP) {
line[6] = 'x';
}
if (attrs.permissions & LIBSSH2_SFTP_S_IROTH) {
line[7] = 'r';
}
if (attrs.permissions & LIBSSH2_SFTP_S_IWOTH) {
line[8] = 'w';
}
if (attrs.permissions & LIBSSH2_SFTP_S_IXOTH) {
line[9] = 'x';
}
if (attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) {
currLen += snprintf(line+currLen, totalLen-currLen, "%11lld",
attrs.filesize);
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
if (attrs.flags & LIBSSH2_SFTP_ATTR_ACMODTIME) {
static const char * const months[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
struct tm *nowParts;
time_t now, remoteTime;
now = time(NULL);
remoteTime = (time_t)attrs.mtime;
nowParts = localtime(&remoteTime);
if ((time_t)attrs.mtime > (now - (3600 * 24 * 180))) {
currLen += snprintf(line+currLen, totalLen-currLen,
" %s %2d %2d:%02d",
months[nowParts->tm_mon],
nowParts->tm_mday, nowParts->tm_hour,
nowParts->tm_min);
}
else {
currLen += snprintf(line+currLen, totalLen-currLen,
" %s %2d %5d", months[nowParts->tm_mon],
nowParts->tm_mday, 1900+nowParts->tm_year);
}
currLen += snprintf(line+currLen, totalLen-currLen, " %s",
filename);
if ((attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
((attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFLNK)) {
char linkPath[PATH_MAX + 1];
snprintf(linkPath, PATH_MAX, "%s%s", sftp->path, filename);
len = libssh2_sftp_readlink(sftp->sftp_session, linkPath,
filename, PATH_MAX);
line = realloc(line, totalLen + 4 + len);
if (!line)
return CURLE_OUT_OF_MEMORY;
currLen += snprintf(line+currLen, totalLen-currLen, " -> %s",
filename);
currLen += snprintf(line+currLen, totalLen-currLen, "\n");
res = Curl_client_write(conn, CLIENTWRITE_BODY, line, 0);
James Housley
committed
Curl_safefree(line);
}
}
else if (len <= 0) {
break;
}
} while (1);
libssh2_sftp_closedir(sftp->sftp_handle);
sftp->sftp_handle = NULL;
/* no data to transfer */
res = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
}
else {
/*
* Work on getting the specified file
*/
sftp->sftp_handle =
libssh2_sftp_open(sftp->sftp_session, sftp->path, LIBSSH2_FXF_READ,
James Housley
committed
conn->data->set.new_file_perms);
if (!sftp->sftp_handle) {
err = libssh2_sftp_last_error(sftp->sftp_session);
failf(conn->data, "Could not open remote file for reading: %s",
sftp_libssh2_strerror(err));
return sftp_libssh2_error_to_CURLE(err);
}
rc = libssh2_sftp_stat(sftp->sftp_session, sftp->path, &attrs);
if (rc) {
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
/*
* libssh2_sftp_open() didn't return an error, so maybe the server
* just doesn't support stat()
*/
data->reqdata.size = -1;
data->reqdata.maxdownload = -1;
}
else {
data->reqdata.size = attrs.filesize;
data->reqdata.maxdownload = attrs.filesize;
Curl_pgrsSetDownloadSize(data, attrs.filesize);
}
Curl_pgrsTime(data, TIMER_STARTTRANSFER);
/* Now download data. The libssh2 0.14 doesn't offer any way to do this
without using this BLOCKING approach, so here's room for improvement
once libssh2 can return EWOULDBLOCK to us. */
while (res == CURLE_OK) {
size_t nread;
/* NOTE: most *read() functions return ssize_t but this returns size_t
which normally is unsigned! */
nread = libssh2_sftp_read(data->reqdata.proto.ssh->sftp_handle,
buf, BUFSIZE-1);
if (nread > 0)
buf[nread] = 0;
/* this check can be changed to a <= 0 when nread is changed to a
signed variable type */
if ((nread == 0) || (nread == (size_t)~0))
break;
bytecount += nread;
res = Curl_client_write(conn, CLIENTWRITE_BODY, buf, nread);
James Housley
committed
if (res)
return res;
Curl_pgrsSetDownloadCounter(data, bytecount);
James Housley
committed
if (Curl_pgrsUpdate(conn))
res = CURLE_ABORTED_BY_CALLBACK;
else {
struct timeval now = Curl_tvnow();
res = Curl_speedcheck(data, now);
}
}
James Housley
committed
if (Curl_pgrsUpdate(conn))
res = CURLE_ABORTED_BY_CALLBACK;
/* no (more) data to transfer */
res = Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
}
}
return res;
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO >= 200706012030L) */
Daniel Stenberg
committed
CURLcode Curl_sftp_done(struct connectdata *conn, CURLcode status,
bool premature)
James Housley
committed
CURLcode result = CURLE_OK;
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
James Housley
committed
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
bool done = FALSE;
struct ssh_conn *sshc = &conn->proto.sshc;
if (status == CURLE_OK) {
/* Before we shut down, see if there are any post-quote commands to send: */
if (!status && !premature && conn->data->set.postquote) {
sshc->nextState = SSH_SFTP_CLOSE;
state(conn, SSH_SFTP_POSTQUOTE_INIT);
} else {
state(conn, SSH_SFTP_CLOSE);
}
/* run the state-machine */
if (conn->data->state.used_interface == Curl_if_multi) {
result = Curl_ssh_multi_statemach(conn, &done);
} else {
result = ssh_easy_statemach(conn);
done = TRUE;
}
} else {
result = status;
done = TRUE;
}
if (done) {
Curl_safefree(conn->data->reqdata.proto.ssh);
conn->data->reqdata.proto.ssh = NULL;
Curl_pgrsDone(conn);
}
Guenter Knauf
committed
#else /* !(LIBSSH2_APINO >= 200706012030L) */
James Housley
committed
struct SSHPROTO *sftp = conn->data->reqdata.proto.ssh;
James Housley
committed
/* Before we shut down, see if there are any post-quote commands to send: */
if (!status && !premature && conn->data->set.postquote) {
infof(conn->data, "Sending postquote commands\n");
result = sftp_sendquote(conn, conn->data->set.postquote);
}
Curl_safefree(sftp->path);
sftp->path = NULL;
Curl_safefree(sftp->homedir);
sftp->homedir = NULL;
if (sftp->sftp_handle) {
if (libssh2_sftp_close(sftp->sftp_handle) < 0) {
infof(conn->data, "Failed to close libssh2 file\n");
}
}
if (sftp->sftp_session) {
if (libssh2_sftp_shutdown(sftp->sftp_session) < 0) {
infof(conn->data, "Failed to stop libssh2 sftp subsystem\n");
}
}
if (sftp->ssh_channel) {
if (libssh2_channel_close(sftp->ssh_channel) < 0) {
infof(conn->data, "Failed to stop libssh2 channel subsystem\n");
}
}
if (sftp->ssh_session) {
libssh2_session_disconnect(sftp->ssh_session, "Shutdown");
libssh2_session_free(sftp->ssh_session);
sftp->ssh_session = NULL;
}
James Housley
committed
Curl_safefree(conn->data->reqdata.proto.ssh);
conn->data->reqdata.proto.ssh = NULL;
Curl_pgrsDone(conn);
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO >= 200706012030L) */
(void)status; /* unused */
James Housley
committed
return result;
}
/* return number of received (decrypted) bytes */
ssize_t Curl_sftp_send(struct connectdata *conn, int sockindex,
void *mem, size_t len)
{
ssize_t nwrite; /* libssh2_sftp_write() used to return size_t in 0.14
but is changed to ssize_t in 0.15! */
Guenter Knauf
committed
#if defined(LIBSSH2SFTP_EAGAIN) && (LIBSSH2_APINO < 200706012030L)
Daniel Stenberg
committed
/* we prefer the non-blocking API but that didn't exist previously */
nwrite = (ssize_t)
libssh2_sftp_writenb(conn->data->reqdata.proto.ssh->sftp_handle, mem, len);
#else
nwrite = (ssize_t)
libssh2_sftp_write(conn->data->reqdata.proto.ssh->sftp_handle, mem, len);
Guenter Knauf
committed
#if (LIBSSH2_APINO >= 200706012030L)
if (nwrite == LIBSSH2_ERROR_EAGAIN) {
return 0;
}
#endif
Daniel Stenberg
committed
#endif
(void)sockindex;
return nwrite;
}
/*
* If the read would block (EWOULDBLOCK) we return -1. Otherwise we return
* a regular CURLcode value.
*/
ssize_t Curl_sftp_recv(struct connectdata *conn, int sockindex,
char *mem, size_t len)
{
ssize_t nread;
(void)sockindex;
/* libssh2_sftp_read() returns size_t !*/
Guenter Knauf
committed
#if defined(LIBSSH2SFTP_EAGAIN) && (LIBSSH2_APINO < 200706012030L)
/* we prefer the non-blocking API but that didn't exist previously */
nread = (ssize_t)
libssh2_sftp_readnb(conn->data->reqdata.proto.ssh->sftp_handle, mem, len);
#else
nread = (ssize_t)
libssh2_sftp_read(conn->data->reqdata.proto.ssh->sftp_handle, mem, len);
#endif
return nread;
}
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
/* 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.
*/
static int
get_pathname(const char **cpp, char **path)
{
const char *cp = *cpp, *end;
char quot;
u_int i, j;
static const char * const WHITESPACE = " \t\r\n";
cp += strspn(cp, WHITESPACE);
if (!*cp) {
*cpp = cp;
*path = NULL;
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
}
*path = malloc(strlen(cp) + 1);
if (*path == NULL)
return CURLE_OUT_OF_MEMORY;
/* Check for quoted filenames */
if (*cp == '\"' || *cp == '\'') {
quot = *cp++;
/* Search for terminating quote, unescape some chars */
for (i = j = 0; i <= strlen(cp); i++) {
if (cp[i] == quot) { /* Found quote */
i++;
(*path)[j] = '\0';
break;
}
if (cp[i] == '\0') { /* End of string */
/*error("Unterminated quote");*/
goto fail;
}
if (cp[i] == '\\') { /* Escaped characters */
i++;
if (cp[i] != '\'' && cp[i] != '\"' &&
cp[i] != '\\') {
/*error("Bad escaped character '\\%c'",
cp[i]);*/
goto fail;
}
}
(*path)[j++] = cp[i];
}
if (j == 0) {
/*error("Empty quotes");*/
goto fail;
}
*cpp = cp + i + strspn(cp + i, WHITESPACE);
}
else {
/* Read to end of filename */
end = strpbrk(cp, WHITESPACE);
if (end == NULL)
end = strchr(cp, '\0');
*cpp = end + strspn(end, WHITESPACE);
memcpy(*path, cp, end - cp);
(*path)[end - cp] = '\0';
}
return (0);
fail:
James Housley
committed
Curl_safefree(*path);
*path = NULL;
return CURLE_FTP_QUOTE_ERROR;
}
static const char *sftp_libssh2_strerror(unsigned long err)
{
switch (err) {
James Housley
committed
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
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";
}
return "Unknown error in libssh2";
}
Guenter Knauf
committed
#if (LIBSSH2_APINO < 200706012030L)
/* BLOCKING */
static CURLcode sftp_sendquote(struct connectdata *conn,
struct curl_slist *quote)
{
James Housley
committed
struct curl_slist *item = quote;
const char *cp;
long err;
struct SessionHandle *data = conn->data;
LIBSSH2_SFTP *sftp_session = data->reqdata.proto.ssh->sftp_session;
while (item) {
James Housley
committed
/*
* Support some of the "FTP" commands
*/
if (curl_strnequal(item->data, "PWD", 3)) {
/* output debug output if that is requested */
if (data->set.verbose) {
char tmp[PATH_MAX+1];
Curl_debug(data, CURLINFO_HEADER_OUT, (char *)"PWD\n", 4, conn);
snprintf(tmp, PATH_MAX, "257 \"%s\" is current directory.\n",
data->reqdata.proto.ssh->path);
Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp), conn);
}
}
else if (item->data) {
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
char *path1 = NULL;
char *path2 = NULL;
/* the arguments following the command must be separated from the
command with a space so we can check for it unconditionally */
cp = strchr(item->data, ' ');
if (cp == NULL) {
failf(data, "Syntax error in SFTP command. Supply parameter(s)!");
return CURLE_FTP_QUOTE_ERROR;
}
/* also, every command takes at least one argument so we get that first
argument right now */
err = get_pathname(&cp, &path1);
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data, "Syntax error: Bad first parameter");
return err;
}
/* 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. */
if (curl_strnequal(item->data, "chgrp ", 6) ||
curl_strnequal(item->data, "chmod ", 6) ||
curl_strnequal(item->data, "chown ", 6) ) { /* attribute change */
LIBSSH2_SFTP_ATTRIBUTES attrs;
/* path1 contains the mode to set */
err = get_pathname(&cp, &path2); /* get the destination */
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data,
"Syntax error in chgrp/chmod/chown: Bad second parameter");
James Housley
committed
Curl_safefree(path1);
return err;
}
memset(&attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES));
if (libssh2_sftp_stat(sftp_session,
path2, &attrs) != 0) { /* get those attributes */
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Attempt to get SFTP stats failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
/* Now set the new attributes... */
if (curl_strnequal(item->data, "chgrp", 5)) {
attrs.gid = strtol(path1, NULL, 10);
if (attrs.gid == 0 && !ISDIGIT(path1[0])) {
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Syntax error: chgrp gid not a number");
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "chmod", 5)) {
attrs.permissions = strtol(path1, NULL, 8);/* permissions are octal */
if (attrs.permissions == 0 && !ISDIGIT(path1[0])) {
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Syntax error: chmod permissions not a number");
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "chown", 5)) {
attrs.uid = strtol(path1, NULL, 10);
if (attrs.uid == 0 && !ISDIGIT(path1[0])) {
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Syntax error: chown uid not a number");
return CURLE_FTP_QUOTE_ERROR;
}
}
/* Now send the completed structure... */
if (libssh2_sftp_setstat(sftp_session, path2, &attrs) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "Attempt to set SFTP stats failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "ln ", 3) ||
curl_strnequal(item->data, "symlink ", 8)) {
/* symbolic linking */
/* path1 is the source */
err = get_pathname(&cp, &path2); /* get the destination */
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data,
"Syntax error in ln/symlink: Bad second parameter");
James Housley
committed
Curl_safefree(path1);
return err;
}
if (libssh2_sftp_symlink(sftp_session, path1, path2) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "symlink command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "mkdir ", 6)) { /* create dir */
if (libssh2_sftp_mkdir(sftp_session, path1, 0744) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
failf(data, "mkdir command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "rename ", 7)) { /* rename file */
/* first param is the source path */
err = get_pathname(&cp, &path2); /* second param is the dest. path */
if (err) {
if (err == CURLE_OUT_OF_MEMORY)
failf(data, "Out of memory");
else
failf(data,
"Syntax error in rename: Bad second parameter");
James Housley
committed
Curl_safefree(path1);
return err;
}
James Housley
committed
if (libssh2_sftp_rename(sftp_session, path1, path2) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
Curl_safefree(path2);
failf(data, "rename command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "rmdir ", 6)) { /* delete dir */
if (libssh2_sftp_rmdir(sftp_session,
path1) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
failf(data, "rmdir command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
else if (curl_strnequal(item->data, "rm ", 3)) { /* delete file */
if (libssh2_sftp_unlink(sftp_session, path1) != 0) {
err = libssh2_sftp_last_error(sftp_session);
James Housley
committed
Curl_safefree(path1);
failf(data, "rm command failed: %s",
sftp_libssh2_strerror(err));
return CURLE_FTP_QUOTE_ERROR;
}
}
if (path1)
James Housley
committed
Curl_safefree(path1);
if (path2)
James Housley
committed
Curl_safefree(path2);
}
item = item->next;
}
(void)ret; /* possibly unused */
return CURLE_OK;
}
* Create the path sftp->path on the remote site
* returns CURL_OK on success, -1 on failure
James Housley
committed
*
* NOTE: This version of sftp_create_dirs() is only used when
Guenter Knauf
committed
* LIBSSH2_APINO < 200706012030L. After that version the code is in
James Housley
committed
* ssh_statemachine_act()
static CURLcode sftp_create_dirs(struct connectdata *conn) {
CURLcode result = CURLE_OK;
unsigned int sftp_err = 0;
int rc;
struct SSHPROTO *sftp = conn->data->reqdata.proto.ssh;
if (strlen(sftp->path) > 1) {
char *slash_pos = sftp->path + 1; /* ignore the leading '/' */
while ((slash_pos = strchr(slash_pos, '/')) != NULL) {
*slash_pos = 0;
infof(conn->data, "Creating directory '%s'\n", sftp->path);
/* 'mode' - parameter is preliminary - default to 0644 */
rc = libssh2_sftp_mkdir(sftp->sftp_session, sftp->path,
conn->data->set.new_directory_perms);
*slash_pos = '/';
++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
*/
sftp_err = libssh2_sftp_last_error(sftp->sftp_session);
if ((sftp_err != LIBSSH2_FX_FILE_ALREADY_EXISTS) &&
(sftp_err != LIBSSH2_FX_FAILURE) &&
(sftp_err != LIBSSH2_FX_PERMISSION_DENIED)) {
result = -1;
break;
}
}
}
}
return result;
Guenter Knauf
committed
#endif /* !(LIBSSH2_APINO < 200706012030L) */
#endif /* USE_LIBSSH2 */