| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, et al. |
| 9 |
* |
| 10 |
* This software is licensed as described in the file COPYING, which |
| 11 |
* you should have received as part of this distribution. The terms |
| 12 |
* are also available at https://curl.haxx.se/docs/copyright.html. |
| 13 |
* |
| 14 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 |
* copies of the Software, and permit persons to whom the Software is |
| 16 |
* furnished to do so, under the terms of the COPYING file. |
| 17 |
* |
| 18 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 |
* KIND, either express or implied. |
| 20 |
* |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
/* #define CURL_LIBSSH2_DEBUG */ |
| 24 |
|
| 25 |
#include "curl_setup.h" |
| 26 |
|
| 27 |
#ifdef USE_LIBSSH2 |
| 28 |
|
| 29 |
#include <limits.h> |
| 30 |
|
| 31 |
#include <libssh2.h> |
| 32 |
#include <libssh2_sftp.h> |
| 33 |
|
| 34 |
#ifdef HAVE_FCNTL_H |
| 35 |
#include <fcntl.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef HAVE_NETINET_IN_H |
| 39 |
#include <netinet/in.h> |
| 40 |
#endif |
| 41 |
#ifdef HAVE_ARPA_INET_H |
| 42 |
#include <arpa/inet.h> |
| 43 |
#endif |
| 44 |
#ifdef HAVE_UTSNAME_H |
| 45 |
#include <sys/utsname.h> |
| 46 |
#endif |
| 47 |
#ifdef HAVE_NETDB_H |
| 48 |
#include <netdb.h> |
| 49 |
#endif |
| 50 |
#ifdef __VMS |
| 51 |
#include <in.h> |
| 52 |
#include <inet.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#if (defined(NETWARE) && defined(__NOVELL_LIBC__)) |
| 56 |
#undef in_addr_t |
| 57 |
#define in_addr_t unsigned long |
| 58 |
#endif |
| 59 |
|
| 60 |
#include <curl/curl.h> |
| 61 |
#include "urldata.h" |
| 62 |
#include "sendf.h" |
| 63 |
#include "hostip.h" |
| 64 |
#include "progress.h" |
| 65 |
#include "transfer.h" |
| 66 |
#include "escape.h" |
| 67 |
#include "http.h" /* for HTTP proxy tunnel stuff */ |
| 68 |
#include "ssh.h" |
| 69 |
#include "url.h" |
| 70 |
#include "speedcheck.h" |
| 71 |
#include "getinfo.h" |
| 72 |
#include "strdup.h" |
| 73 |
#include "strcase.h" |
| 74 |
#include "vtls/vtls.h" |
| 75 |
#include "connect.h" |
| 76 |
#include "strerror.h" |
| 77 |
#include "inet_ntop.h" |
| 78 |
#include "parsedate.h" /* for the week day and month names */ |
| 79 |
#include "sockaddr.h" /* required for Curl_sockaddr_storage */ |
| 80 |
#include "strtoofft.h" |
| 81 |
#include "multiif.h" |
| 82 |
#include "select.h" |
| 83 |
#include "warnless.h" |
| 84 |
#include "curl_path.h" |
| 85 |
|
| 86 |
/* The last 3 #include files should be in this order */ |
| 87 |
#include "curl_printf.h" |
| 88 |
#include "curl_memory.h" |
| 89 |
#include "memdebug.h" |
| 90 |
|
| 91 |
#if LIBSSH2_VERSION_NUM >= 0x010206 |
| 92 |
/* libssh2_sftp_statvfs and friends were added in 1.2.6 */ |
| 93 |
#define HAS_STATVFS_SUPPORT 1 |
| 94 |
#endif |
| 95 |
|
| 96 |
#define sftp_libssh2_last_error(s) curlx_ultosi(libssh2_sftp_last_error(s)) |
| 97 |
|
| 98 |
#define sftp_libssh2_realpath(s,p,t,m) \ |
| 99 |
libssh2_sftp_symlink_ex((s), (p), curlx_uztoui(strlen(p)), \ |
| 100 |
(t), (m), LIBSSH2_SFTP_REALPATH) |
| 101 |
|
| 102 |
|
| 103 |
/* Local functions: */ |
| 104 |
static const char *sftp_libssh2_strerror(int err); |
| 105 |
static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc); |
| 106 |
static LIBSSH2_REALLOC_FUNC(my_libssh2_realloc); |
| 107 |
static LIBSSH2_FREE_FUNC(my_libssh2_free); |
| 108 |
|
| 109 |
static CURLcode ssh_force_knownhost_key_type(struct connectdata *conn); |
| 110 |
static CURLcode ssh_connect(struct connectdata *conn, bool *done); |
| 111 |
static CURLcode ssh_multi_statemach(struct connectdata *conn, bool *done); |
| 112 |
static CURLcode ssh_do(struct connectdata *conn, bool *done); |
| 113 |
|
| 114 |
static CURLcode scp_done(struct connectdata *conn, |
| 115 |
CURLcode, bool premature); |
| 116 |
static CURLcode scp_doing(struct connectdata *conn, |
| 117 |
bool *dophase_done); |
| 118 |
static CURLcode scp_disconnect(struct connectdata *conn, bool dead_connection); |
| 119 |
|
| 120 |
static CURLcode sftp_done(struct connectdata *conn, |
| 121 |
CURLcode, bool premature); |
| 122 |
static CURLcode sftp_doing(struct connectdata *conn, |
| 123 |
bool *dophase_done); |
| 124 |
static CURLcode sftp_disconnect(struct connectdata *conn, bool dead); |
| 125 |
static |
| 126 |
CURLcode sftp_perform(struct connectdata *conn, |
| 127 |
bool *connected, |
| 128 |
bool *dophase_done); |
| 129 |
static int ssh_getsock(struct connectdata *conn, curl_socket_t *sock); |
| 130 |
static int ssh_perform_getsock(const struct connectdata *conn, |
| 131 |
curl_socket_t *sock); |
| 132 |
static CURLcode ssh_setup_connection(struct connectdata *conn); |
| 133 |
|
| 134 |
/* |
| 135 |
* SCP protocol handler. |
| 136 |
*/ |
| 137 |
|
| 138 |
const struct Curl_handler Curl_handler_scp = { |
| 139 |
"SCP", /* scheme */ |
| 140 |
ssh_setup_connection, /* setup_connection */ |
| 141 |
ssh_do, /* do_it */ |
| 142 |
scp_done, /* done */ |
| 143 |
ZERO_NULL, /* do_more */ |
| 144 |
ssh_connect, /* connect_it */ |
| 145 |
ssh_multi_statemach, /* connecting */ |
| 146 |
scp_doing, /* doing */ |
| 147 |
ssh_getsock, /* proto_getsock */ |
| 148 |
ssh_getsock, /* doing_getsock */ |
| 149 |
ZERO_NULL, /* domore_getsock */ |
| 150 |
ssh_perform_getsock, /* perform_getsock */ |
| 151 |
scp_disconnect, /* disconnect */ |
| 152 |
ZERO_NULL, /* readwrite */ |
| 153 |
ZERO_NULL, /* connection_check */ |
| 154 |
PORT_SSH, /* defport */ |
| 155 |
CURLPROTO_SCP, /* protocol */ |
| 156 |
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION |
| 157 |
| PROTOPT_NOURLQUERY /* flags */ |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
/* |
| 162 |
* SFTP protocol handler. |
| 163 |
*/ |
| 164 |
|
| 165 |
const struct Curl_handler Curl_handler_sftp = { |
| 166 |
"SFTP", /* scheme */ |
| 167 |
ssh_setup_connection, /* setup_connection */ |
| 168 |
ssh_do, /* do_it */ |
| 169 |
sftp_done, /* done */ |
| 170 |
ZERO_NULL, /* do_more */ |
| 171 |
ssh_connect, /* connect_it */ |
| 172 |
ssh_multi_statemach, /* connecting */ |
| 173 |
sftp_doing, /* doing */ |
| 174 |
ssh_getsock, /* proto_getsock */ |
| 175 |
ssh_getsock, /* doing_getsock */ |
| 176 |
ZERO_NULL, /* domore_getsock */ |
| 177 |
ssh_perform_getsock, /* perform_getsock */ |
| 178 |
sftp_disconnect, /* disconnect */ |
| 179 |
ZERO_NULL, /* readwrite */ |
| 180 |
ZERO_NULL, /* connection_check */ |
| 181 |
PORT_SSH, /* defport */ |
| 182 |
CURLPROTO_SFTP, /* protocol */ |
| 183 |
PROTOPT_DIRLOCK | PROTOPT_CLOSEACTION |
| 184 |
| PROTOPT_NOURLQUERY /* flags */ |
| 185 |
}; |
| 186 |
|
| 187 |
static void |
| 188 |
kbd_callback(const char *name, int name_len, const char *instruction, |
| 189 |
int instruction_len, int num_prompts, |
| 190 |
const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, |
| 191 |
LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, |
| 192 |
void **abstract) |
| 193 |
{ |
| 194 |
struct connectdata *conn = (struct connectdata *)*abstract; |
| 195 |
|
| 196 |
#ifdef CURL_LIBSSH2_DEBUG |
| 197 |
fprintf(stderr, "name=%s\n", name); |
| 198 |
fprintf(stderr, "name_len=%d\n", name_len); |
| 199 |
fprintf(stderr, "instruction=%s\n", instruction); |
| 200 |
fprintf(stderr, "instruction_len=%d\n", instruction_len); |
| 201 |
fprintf(stderr, "num_prompts=%d\n", num_prompts); |
| 202 |
#else |
| 203 |
(void)name; |
| 204 |
(void)name_len; |
| 205 |
(void)instruction; |
| 206 |
(void)instruction_len; |
| 207 |
#endif /* CURL_LIBSSH2_DEBUG */ |
| 208 |
if(num_prompts == 1) { |
| 209 |
responses[0].text = strdup(conn->passwd); |
| 210 |
responses[0].length = curlx_uztoui(strlen(conn->passwd)); |
| 211 |
} |
| 212 |
(void)prompts; |
| 213 |
(void)abstract; |
| 214 |
} /* kbd_callback */ |
| 215 |
|
| 216 |
static CURLcode sftp_libssh2_error_to_CURLE(int err) |
| 217 |
{ |
| 218 |
switch(err) { |
| 219 |
case LIBSSH2_FX_OK: |
| 220 |
return CURLE_OK; |
| 221 |
|
| 222 |
case LIBSSH2_FX_NO_SUCH_FILE: |
| 223 |
case LIBSSH2_FX_NO_SUCH_PATH: |
| 224 |
return CURLE_REMOTE_FILE_NOT_FOUND; |
| 225 |
|
| 226 |
case LIBSSH2_FX_PERMISSION_DENIED: |
| 227 |
case LIBSSH2_FX_WRITE_PROTECT: |
| 228 |
case LIBSSH2_FX_LOCK_CONFlICT: |
| 229 |
return CURLE_REMOTE_ACCESS_DENIED; |
| 230 |
|
| 231 |
case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM: |
| 232 |
case LIBSSH2_FX_QUOTA_EXCEEDED: |
| 233 |
return CURLE_REMOTE_DISK_FULL; |
| 234 |
|
| 235 |
case LIBSSH2_FX_FILE_ALREADY_EXISTS: |
| 236 |
return CURLE_REMOTE_FILE_EXISTS; |
| 237 |
|
| 238 |
case LIBSSH2_FX_DIR_NOT_EMPTY: |
| 239 |
return CURLE_QUOTE_ERROR; |
| 240 |
|
| 241 |
default: |
| 242 |
break; |
| 243 |
} |
| 244 |
|
| 245 |
return CURLE_SSH; |
| 246 |
} |
| 247 |
|
| 248 |
static CURLcode libssh2_session_error_to_CURLE(int err) |
| 249 |
{ |
| 250 |
switch(err) { |
| 251 |
/* Ordered by order of appearance in libssh2.h */ |
| 252 |
case LIBSSH2_ERROR_NONE: |
| 253 |
return CURLE_OK; |
| 254 |
|
| 255 |
/* This is the error returned by libssh2_scp_recv2 |
| 256 |
* on unknown file */ |
| 257 |
case LIBSSH2_ERROR_SCP_PROTOCOL: |
| 258 |
return CURLE_REMOTE_FILE_NOT_FOUND; |
| 259 |
|
| 260 |
case LIBSSH2_ERROR_SOCKET_NONE: |
| 261 |
return CURLE_COULDNT_CONNECT; |
| 262 |
|
| 263 |
case LIBSSH2_ERROR_ALLOC: |
| 264 |
return CURLE_OUT_OF_MEMORY; |
| 265 |
|
| 266 |
case LIBSSH2_ERROR_SOCKET_SEND: |
| 267 |
return CURLE_SEND_ERROR; |
| 268 |
|
| 269 |
case LIBSSH2_ERROR_HOSTKEY_INIT: |
| 270 |
case LIBSSH2_ERROR_HOSTKEY_SIGN: |
| 271 |
case LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED: |
| 272 |
case LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED: |
| 273 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 274 |
|
| 275 |
case LIBSSH2_ERROR_PASSWORD_EXPIRED: |
| 276 |
return CURLE_LOGIN_DENIED; |
| 277 |
|
| 278 |
case LIBSSH2_ERROR_SOCKET_TIMEOUT: |
| 279 |
case LIBSSH2_ERROR_TIMEOUT: |
| 280 |
return CURLE_OPERATION_TIMEDOUT; |
| 281 |
|
| 282 |
case LIBSSH2_ERROR_EAGAIN: |
| 283 |
return CURLE_AGAIN; |
| 284 |
} |
| 285 |
|
| 286 |
return CURLE_SSH; |
| 287 |
} |
| 288 |
|
| 289 |
static LIBSSH2_ALLOC_FUNC(my_libssh2_malloc) |
| 290 |
{ |
| 291 |
(void)abstract; /* arg not used */ |
| 292 |
return malloc(count); |
| 293 |
} |
| 294 |
|
| 295 |
static LIBSSH2_REALLOC_FUNC(my_libssh2_realloc) |
| 296 |
{ |
| 297 |
(void)abstract; /* arg not used */ |
| 298 |
return realloc(ptr, count); |
| 299 |
} |
| 300 |
|
| 301 |
static LIBSSH2_FREE_FUNC(my_libssh2_free) |
| 302 |
{ |
| 303 |
(void)abstract; /* arg not used */ |
| 304 |
if(ptr) /* ssh2 agent sometimes call free with null ptr */ |
| 305 |
free(ptr); |
| 306 |
} |
| 307 |
|
| 308 |
/* |
| 309 |
* SSH State machine related code |
| 310 |
*/ |
| 311 |
/* This is the ONLY way to change SSH state! */ |
| 312 |
static void state(struct connectdata *conn, sshstate nowstate) |
| 313 |
{ |
| 314 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 315 |
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS) |
| 316 |
/* for debug purposes */ |
| 317 |
static const char * const names[] = { |
| 318 |
"SSH_STOP", |
| 319 |
"SSH_INIT", |
| 320 |
"SSH_S_STARTUP", |
| 321 |
"SSH_HOSTKEY", |
| 322 |
"SSH_AUTHLIST", |
| 323 |
"SSH_AUTH_PKEY_INIT", |
| 324 |
"SSH_AUTH_PKEY", |
| 325 |
"SSH_AUTH_PASS_INIT", |
| 326 |
"SSH_AUTH_PASS", |
| 327 |
"SSH_AUTH_AGENT_INIT", |
| 328 |
"SSH_AUTH_AGENT_LIST", |
| 329 |
"SSH_AUTH_AGENT", |
| 330 |
"SSH_AUTH_HOST_INIT", |
| 331 |
"SSH_AUTH_HOST", |
| 332 |
"SSH_AUTH_KEY_INIT", |
| 333 |
"SSH_AUTH_KEY", |
| 334 |
"SSH_AUTH_GSSAPI", |
| 335 |
"SSH_AUTH_DONE", |
| 336 |
"SSH_SFTP_INIT", |
| 337 |
"SSH_SFTP_REALPATH", |
| 338 |
"SSH_SFTP_QUOTE_INIT", |
| 339 |
"SSH_SFTP_POSTQUOTE_INIT", |
| 340 |
"SSH_SFTP_QUOTE", |
| 341 |
"SSH_SFTP_NEXT_QUOTE", |
| 342 |
"SSH_SFTP_QUOTE_STAT", |
| 343 |
"SSH_SFTP_QUOTE_SETSTAT", |
| 344 |
"SSH_SFTP_QUOTE_SYMLINK", |
| 345 |
"SSH_SFTP_QUOTE_MKDIR", |
| 346 |
"SSH_SFTP_QUOTE_RENAME", |
| 347 |
"SSH_SFTP_QUOTE_RMDIR", |
| 348 |
"SSH_SFTP_QUOTE_UNLINK", |
| 349 |
"SSH_SFTP_QUOTE_STATVFS", |
| 350 |
"SSH_SFTP_GETINFO", |
| 351 |
"SSH_SFTP_FILETIME", |
| 352 |
"SSH_SFTP_TRANS_INIT", |
| 353 |
"SSH_SFTP_UPLOAD_INIT", |
| 354 |
"SSH_SFTP_CREATE_DIRS_INIT", |
| 355 |
"SSH_SFTP_CREATE_DIRS", |
| 356 |
"SSH_SFTP_CREATE_DIRS_MKDIR", |
| 357 |
"SSH_SFTP_READDIR_INIT", |
| 358 |
"SSH_SFTP_READDIR", |
| 359 |
"SSH_SFTP_READDIR_LINK", |
| 360 |
"SSH_SFTP_READDIR_BOTTOM", |
| 361 |
"SSH_SFTP_READDIR_DONE", |
| 362 |
"SSH_SFTP_DOWNLOAD_INIT", |
| 363 |
"SSH_SFTP_DOWNLOAD_STAT", |
| 364 |
"SSH_SFTP_CLOSE", |
| 365 |
"SSH_SFTP_SHUTDOWN", |
| 366 |
"SSH_SCP_TRANS_INIT", |
| 367 |
"SSH_SCP_UPLOAD_INIT", |
| 368 |
"SSH_SCP_DOWNLOAD_INIT", |
| 369 |
"SSH_SCP_DOWNLOAD", |
| 370 |
"SSH_SCP_DONE", |
| 371 |
"SSH_SCP_SEND_EOF", |
| 372 |
"SSH_SCP_WAIT_EOF", |
| 373 |
"SSH_SCP_WAIT_CLOSE", |
| 374 |
"SSH_SCP_CHANNEL_FREE", |
| 375 |
"SSH_SESSION_DISCONNECT", |
| 376 |
"SSH_SESSION_FREE", |
| 377 |
"QUIT" |
| 378 |
}; |
| 379 |
|
| 380 |
/* a precaution to make sure the lists are in sync */ |
| 381 |
DEBUGASSERT(sizeof(names)/sizeof(names[0]) == SSH_LAST); |
| 382 |
|
| 383 |
if(sshc->state != nowstate) { |
| 384 |
infof(conn->data, "SFTP %p state change from %s to %s\n", |
| 385 |
(void *)sshc, names[sshc->state], names[nowstate]); |
| 386 |
} |
| 387 |
#endif |
| 388 |
|
| 389 |
sshc->state = nowstate; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
#ifdef HAVE_LIBSSH2_KNOWNHOST_API |
| 394 |
static int sshkeycallback(struct Curl_easy *easy, |
| 395 |
const struct curl_khkey *knownkey, /* known */ |
| 396 |
const struct curl_khkey *foundkey, /* found */ |
| 397 |
enum curl_khmatch match, |
| 398 |
void *clientp) |
| 399 |
{ |
| 400 |
(void)easy; |
| 401 |
(void)knownkey; |
| 402 |
(void)foundkey; |
| 403 |
(void)clientp; |
| 404 |
|
| 405 |
/* we only allow perfect matches, and we reject everything else */ |
| 406 |
return (match != CURLKHMATCH_OK)?CURLKHSTAT_REJECT:CURLKHSTAT_FINE; |
| 407 |
} |
| 408 |
#endif |
| 409 |
|
| 410 |
/* |
| 411 |
* Earlier libssh2 versions didn't have the ability to seek to 64bit positions |
| 412 |
* with 32bit size_t. |
| 413 |
*/ |
| 414 |
#ifdef HAVE_LIBSSH2_SFTP_SEEK64 |
| 415 |
#define SFTP_SEEK(x,y) libssh2_sftp_seek64(x, (libssh2_uint64_t)y) |
| 416 |
#else |
| 417 |
#define SFTP_SEEK(x,y) libssh2_sftp_seek(x, (size_t)y) |
| 418 |
#endif |
| 419 |
|
| 420 |
/* |
| 421 |
* Earlier libssh2 versions didn't do SCP properly beyond 32bit sizes on 32bit |
| 422 |
* architectures so we check of the necessary function is present. |
| 423 |
*/ |
| 424 |
#ifndef HAVE_LIBSSH2_SCP_SEND64 |
| 425 |
#define SCP_SEND(a,b,c,d) libssh2_scp_send_ex(a, b, (int)(c), (size_t)d, 0, 0) |
| 426 |
#else |
| 427 |
#define SCP_SEND(a,b,c,d) libssh2_scp_send64(a, b, (int)(c), \ |
| 428 |
(libssh2_uint64_t)d, 0, 0) |
| 429 |
#endif |
| 430 |
|
| 431 |
/* |
| 432 |
* libssh2 1.2.8 fixed the problem with 32bit ints used for sockets on win64. |
| 433 |
*/ |
| 434 |
#ifdef HAVE_LIBSSH2_SESSION_HANDSHAKE |
| 435 |
#define libssh2_session_startup(x,y) libssh2_session_handshake(x,y) |
| 436 |
#endif |
| 437 |
|
| 438 |
static CURLcode ssh_knownhost(struct connectdata *conn) |
| 439 |
{ |
| 440 |
CURLcode result = CURLE_OK; |
| 441 |
|
| 442 |
#ifdef HAVE_LIBSSH2_KNOWNHOST_API |
| 443 |
struct Curl_easy *data = conn->data; |
| 444 |
|
| 445 |
if(data->set.str[STRING_SSH_KNOWNHOSTS]) { |
| 446 |
/* we're asked to verify the host against a file */ |
| 447 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 448 |
int rc; |
| 449 |
int keytype; |
| 450 |
size_t keylen; |
| 451 |
const char *remotekey = libssh2_session_hostkey(sshc->ssh_session, |
| 452 |
&keylen, &keytype); |
| 453 |
int keycheck = LIBSSH2_KNOWNHOST_CHECK_FAILURE; |
| 454 |
int keybit = 0; |
| 455 |
|
| 456 |
if(remotekey) { |
| 457 |
/* |
| 458 |
* A subject to figure out is what host name we need to pass in here. |
| 459 |
* What host name does OpenSSH store in its file if an IDN name is |
| 460 |
* used? |
| 461 |
*/ |
| 462 |
struct libssh2_knownhost *host; |
| 463 |
enum curl_khmatch keymatch; |
| 464 |
curl_sshkeycallback func = |
| 465 |
data->set.ssh_keyfunc?data->set.ssh_keyfunc:sshkeycallback; |
| 466 |
struct curl_khkey knownkey; |
| 467 |
struct curl_khkey *knownkeyp = NULL; |
| 468 |
struct curl_khkey foundkey; |
| 469 |
|
| 470 |
switch(keytype) { |
| 471 |
case LIBSSH2_HOSTKEY_TYPE_RSA: |
| 472 |
keybit = LIBSSH2_KNOWNHOST_KEY_SSHRSA; |
| 473 |
break; |
| 474 |
case LIBSSH2_HOSTKEY_TYPE_DSS: |
| 475 |
keybit = LIBSSH2_KNOWNHOST_KEY_SSHDSS; |
| 476 |
break; |
| 477 |
#ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_256 |
| 478 |
case LIBSSH2_HOSTKEY_TYPE_ECDSA_256: |
| 479 |
keybit = LIBSSH2_KNOWNHOST_KEY_ECDSA_256; |
| 480 |
break; |
| 481 |
#endif |
| 482 |
#ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_384 |
| 483 |
case LIBSSH2_HOSTKEY_TYPE_ECDSA_384: |
| 484 |
keybit = LIBSSH2_KNOWNHOST_KEY_ECDSA_384; |
| 485 |
break; |
| 486 |
#endif |
| 487 |
#ifdef LIBSSH2_HOSTKEY_TYPE_ECDSA_521 |
| 488 |
case LIBSSH2_HOSTKEY_TYPE_ECDSA_521: |
| 489 |
keybit = LIBSSH2_KNOWNHOST_KEY_ECDSA_521; |
| 490 |
break; |
| 491 |
#endif |
| 492 |
#ifdef LIBSSH2_HOSTKEY_TYPE_ED25519 |
| 493 |
case LIBSSH2_HOSTKEY_TYPE_ED25519: |
| 494 |
keybit = LIBSSH2_KNOWNHOST_KEY_ED25519; |
| 495 |
break; |
| 496 |
#endif |
| 497 |
default: |
| 498 |
infof(data, "unsupported key type, can't check knownhosts!\n"); |
| 499 |
keybit = 0; |
| 500 |
break; |
| 501 |
} |
| 502 |
if(!keybit) |
| 503 |
/* no check means failure! */ |
| 504 |
rc = CURLKHSTAT_REJECT; |
| 505 |
else { |
| 506 |
#ifdef HAVE_LIBSSH2_KNOWNHOST_CHECKP |
| 507 |
keycheck = libssh2_knownhost_checkp(sshc->kh, |
| 508 |
conn->host.name, |
| 509 |
(conn->remote_port != PORT_SSH)? |
| 510 |
conn->remote_port:-1, |
| 511 |
remotekey, keylen, |
| 512 |
LIBSSH2_KNOWNHOST_TYPE_PLAIN| |
| 513 |
LIBSSH2_KNOWNHOST_KEYENC_RAW| |
| 514 |
keybit, |
| 515 |
&host); |
| 516 |
#else |
| 517 |
keycheck = libssh2_knownhost_check(sshc->kh, |
| 518 |
conn->host.name, |
| 519 |
remotekey, keylen, |
| 520 |
LIBSSH2_KNOWNHOST_TYPE_PLAIN| |
| 521 |
LIBSSH2_KNOWNHOST_KEYENC_RAW| |
| 522 |
keybit, |
| 523 |
&host); |
| 524 |
#endif |
| 525 |
|
| 526 |
infof(data, "SSH host check: %d, key: %s\n", keycheck, |
| 527 |
(keycheck <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH)? |
| 528 |
host->key:"<none>"); |
| 529 |
|
| 530 |
/* setup 'knownkey' */ |
| 531 |
if(keycheck <= LIBSSH2_KNOWNHOST_CHECK_MISMATCH) { |
| 532 |
knownkey.key = host->key; |
| 533 |
knownkey.len = 0; |
| 534 |
knownkey.keytype = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)? |
| 535 |
CURLKHTYPE_RSA : CURLKHTYPE_DSS; |
| 536 |
knownkeyp = &knownkey; |
| 537 |
} |
| 538 |
|
| 539 |
/* setup 'foundkey' */ |
| 540 |
foundkey.key = remotekey; |
| 541 |
foundkey.len = keylen; |
| 542 |
foundkey.keytype = (keytype == LIBSSH2_HOSTKEY_TYPE_RSA)? |
| 543 |
CURLKHTYPE_RSA : CURLKHTYPE_DSS; |
| 544 |
|
| 545 |
/* |
| 546 |
* if any of the LIBSSH2_KNOWNHOST_CHECK_* defines and the |
| 547 |
* curl_khmatch enum are ever modified, we need to introduce a |
| 548 |
* translation table here! |
| 549 |
*/ |
| 550 |
keymatch = (enum curl_khmatch)keycheck; |
| 551 |
|
| 552 |
/* Ask the callback how to behave */ |
| 553 |
Curl_set_in_callback(data, true); |
| 554 |
rc = func(data, knownkeyp, /* from the knownhosts file */ |
| 555 |
&foundkey, /* from the remote host */ |
| 556 |
keymatch, data->set.ssh_keyfunc_userp); |
| 557 |
Curl_set_in_callback(data, false); |
| 558 |
} |
| 559 |
} |
| 560 |
else |
| 561 |
/* no remotekey means failure! */ |
| 562 |
rc = CURLKHSTAT_REJECT; |
| 563 |
|
| 564 |
switch(rc) { |
| 565 |
default: /* unknown return codes will equal reject */ |
| 566 |
/* FALLTHROUGH */ |
| 567 |
case CURLKHSTAT_REJECT: |
| 568 |
state(conn, SSH_SESSION_FREE); |
| 569 |
/* FALLTHROUGH */ |
| 570 |
case CURLKHSTAT_DEFER: |
| 571 |
/* DEFER means bail out but keep the SSH_HOSTKEY state */ |
| 572 |
result = sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; |
| 573 |
break; |
| 574 |
case CURLKHSTAT_FINE: |
| 575 |
case CURLKHSTAT_FINE_ADD_TO_FILE: |
| 576 |
/* proceed */ |
| 577 |
if(keycheck != LIBSSH2_KNOWNHOST_CHECK_MATCH) { |
| 578 |
/* the found host+key didn't match but has been told to be fine |
| 579 |
anyway so we add it in memory */ |
| 580 |
int addrc = libssh2_knownhost_add(sshc->kh, |
| 581 |
conn->host.name, NULL, |
| 582 |
remotekey, keylen, |
| 583 |
LIBSSH2_KNOWNHOST_TYPE_PLAIN| |
| 584 |
LIBSSH2_KNOWNHOST_KEYENC_RAW| |
| 585 |
keybit, NULL); |
| 586 |
if(addrc) |
| 587 |
infof(data, "Warning adding the known host %s failed!\n", |
| 588 |
conn->host.name); |
| 589 |
else if(rc == CURLKHSTAT_FINE_ADD_TO_FILE) { |
| 590 |
/* now we write the entire in-memory list of known hosts to the |
| 591 |
known_hosts file */ |
| 592 |
int wrc = |
| 593 |
libssh2_knownhost_writefile(sshc->kh, |
| 594 |
data->set.str[STRING_SSH_KNOWNHOSTS], |
| 595 |
LIBSSH2_KNOWNHOST_FILE_OPENSSH); |
| 596 |
if(wrc) { |
| 597 |
infof(data, "Warning, writing %s failed!\n", |
| 598 |
data->set.str[STRING_SSH_KNOWNHOSTS]); |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
break; |
| 603 |
} |
| 604 |
} |
| 605 |
#else /* HAVE_LIBSSH2_KNOWNHOST_API */ |
| 606 |
(void)conn; |
| 607 |
#endif |
| 608 |
return result; |
| 609 |
} |
| 610 |
|
| 611 |
static CURLcode ssh_check_fingerprint(struct connectdata *conn) |
| 612 |
{ |
| 613 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 614 |
struct Curl_easy *data = conn->data; |
| 615 |
const char *pubkey_md5 = data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5]; |
| 616 |
char md5buffer[33]; |
| 617 |
|
| 618 |
const char *fingerprint = libssh2_hostkey_hash(sshc->ssh_session, |
| 619 |
LIBSSH2_HOSTKEY_HASH_MD5); |
| 620 |
|
| 621 |
if(fingerprint) { |
| 622 |
/* The fingerprint points to static storage (!), don't free() it. */ |
| 623 |
int i; |
| 624 |
for(i = 0; i < 16; i++) |
| 625 |
msnprintf(&md5buffer[i*2], 3, "%02x", (unsigned char) fingerprint[i]); |
| 626 |
infof(data, "SSH MD5 fingerprint: %s\n", md5buffer); |
| 627 |
} |
| 628 |
|
| 629 |
/* Before we authenticate we check the hostkey's MD5 fingerprint |
| 630 |
* against a known fingerprint, if available. |
| 631 |
*/ |
| 632 |
if(pubkey_md5 && strlen(pubkey_md5) == 32) { |
| 633 |
if(!fingerprint || !strcasecompare(md5buffer, pubkey_md5)) { |
| 634 |
if(fingerprint) |
| 635 |
failf(data, |
| 636 |
"Denied establishing ssh session: mismatch md5 fingerprint. " |
| 637 |
"Remote %s is not equal to %s", md5buffer, pubkey_md5); |
| 638 |
else |
| 639 |
failf(data, |
| 640 |
"Denied establishing ssh session: md5 fingerprint not available"); |
| 641 |
state(conn, SSH_SESSION_FREE); |
| 642 |
sshc->actualcode = CURLE_PEER_FAILED_VERIFICATION; |
| 643 |
return sshc->actualcode; |
| 644 |
} |
| 645 |
infof(data, "MD5 checksum match!\n"); |
| 646 |
/* as we already matched, we skip the check for known hosts */ |
| 647 |
return CURLE_OK; |
| 648 |
} |
| 649 |
return ssh_knownhost(conn); |
| 650 |
} |
| 651 |
|
| 652 |
/* |
| 653 |
* ssh_force_knownhost_key_type() will check the known hosts file and try to |
| 654 |
* force a specific public key type from the server if an entry is found. |
| 655 |
*/ |
| 656 |
static CURLcode ssh_force_knownhost_key_type(struct connectdata *conn) |
| 657 |
{ |
| 658 |
CURLcode result = CURLE_OK; |
| 659 |
|
| 660 |
#ifdef HAVE_LIBSSH2_KNOWNHOST_API |
| 661 |
|
| 662 |
#ifdef LIBSSH2_KNOWNHOST_KEY_ED25519 |
| 663 |
static const char * const hostkey_method_ssh_ed25519 |
| 664 |
= "ssh-ed25519"; |
| 665 |
#endif |
| 666 |
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_521 |
| 667 |
static const char * const hostkey_method_ssh_ecdsa_521 |
| 668 |
= "ecdsa-sha2-nistp521"; |
| 669 |
#endif |
| 670 |
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_384 |
| 671 |
static const char * const hostkey_method_ssh_ecdsa_384 |
| 672 |
= "ecdsa-sha2-nistp384"; |
| 673 |
#endif |
| 674 |
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_256 |
| 675 |
static const char * const hostkey_method_ssh_ecdsa_256 |
| 676 |
= "ecdsa-sha2-nistp256"; |
| 677 |
#endif |
| 678 |
static const char * const hostkey_method_ssh_rsa |
| 679 |
= "ssh-rsa"; |
| 680 |
static const char * const hostkey_method_ssh_dss |
| 681 |
= "ssh-dss"; |
| 682 |
|
| 683 |
const char *hostkey_method = NULL; |
| 684 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 685 |
struct Curl_easy *data = conn->data; |
| 686 |
struct libssh2_knownhost* store = NULL; |
| 687 |
const char *kh_name_end = NULL; |
| 688 |
size_t kh_name_size = 0; |
| 689 |
int port = 0; |
| 690 |
bool found = false; |
| 691 |
|
| 692 |
if(sshc->kh && !data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5]) { |
| 693 |
/* lets try to find our host in the known hosts file */ |
| 694 |
while(!libssh2_knownhost_get(sshc->kh, &store, store)) { |
| 695 |
/* For non-standard ports, the name will be enclosed in */ |
| 696 |
/* square brackets, followed by a colon and the port */ |
| 697 |
if(store->name[0] == '[') { |
| 698 |
kh_name_end = strstr(store->name, "]:"); |
| 699 |
if(!kh_name_end) { |
| 700 |
infof(data, "Invalid host pattern %s in %s\n", |
| 701 |
store->name, data->set.str[STRING_SSH_KNOWNHOSTS]); |
| 702 |
continue; |
| 703 |
} |
| 704 |
port = atoi(kh_name_end + 2); |
| 705 |
if(kh_name_end && (port == conn->remote_port)) { |
| 706 |
kh_name_size = strlen(store->name) - 1 - strlen(kh_name_end); |
| 707 |
if(strncmp(store->name + 1, conn->host.name, kh_name_size) == 0) { |
| 708 |
found = true; |
| 709 |
break; |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
else if(strcmp(store->name, conn->host.name) == 0) { |
| 714 |
found = true; |
| 715 |
break; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
if(found) { |
| 720 |
infof(data, "Found host %s in %s\n", |
| 721 |
store->name, data->set.str[STRING_SSH_KNOWNHOSTS]); |
| 722 |
|
| 723 |
switch(store->typemask & LIBSSH2_KNOWNHOST_KEY_MASK) { |
| 724 |
#ifdef LIBSSH2_KNOWNHOST_KEY_ED25519 |
| 725 |
case LIBSSH2_KNOWNHOST_KEY_ED25519: |
| 726 |
hostkey_method = hostkey_method_ssh_ed25519; |
| 727 |
break; |
| 728 |
#endif |
| 729 |
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_521 |
| 730 |
case LIBSSH2_KNOWNHOST_KEY_ECDSA_521: |
| 731 |
hostkey_method = hostkey_method_ssh_ecdsa_521; |
| 732 |
break; |
| 733 |
#endif |
| 734 |
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_384 |
| 735 |
case LIBSSH2_KNOWNHOST_KEY_ECDSA_384: |
| 736 |
hostkey_method = hostkey_method_ssh_ecdsa_384; |
| 737 |
break; |
| 738 |
#endif |
| 739 |
#ifdef LIBSSH2_KNOWNHOST_KEY_ECDSA_256 |
| 740 |
case LIBSSH2_KNOWNHOST_KEY_ECDSA_256: |
| 741 |
hostkey_method = hostkey_method_ssh_ecdsa_256; |
| 742 |
break; |
| 743 |
#endif |
| 744 |
case LIBSSH2_KNOWNHOST_KEY_SSHRSA: |
| 745 |
hostkey_method = hostkey_method_ssh_rsa; |
| 746 |
break; |
| 747 |
case LIBSSH2_KNOWNHOST_KEY_SSHDSS: |
| 748 |
hostkey_method = hostkey_method_ssh_dss; |
| 749 |
break; |
| 750 |
case LIBSSH2_KNOWNHOST_KEY_RSA1: |
| 751 |
failf(data, "Found host key type RSA1 which is not supported\n"); |
| 752 |
return CURLE_SSH; |
| 753 |
default: |
| 754 |
failf(data, "Unknown host key type: %i\n", |
| 755 |
(store->typemask & LIBSSH2_KNOWNHOST_KEY_MASK)); |
| 756 |
return CURLE_SSH; |
| 757 |
} |
| 758 |
|
| 759 |
infof(data, "Set \"%s\" as SSH hostkey type\n", hostkey_method); |
| 760 |
result = libssh2_session_error_to_CURLE( |
| 761 |
libssh2_session_method_pref( |
| 762 |
sshc->ssh_session, LIBSSH2_METHOD_HOSTKEY, hostkey_method)); |
| 763 |
} |
| 764 |
else { |
| 765 |
infof(data, "Did not find host %s in %s\n", |
| 766 |
conn->host.name, data->set.str[STRING_SSH_KNOWNHOSTS]); |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
#endif /* HAVE_LIBSSH2_KNOWNHOST_API */ |
| 771 |
|
| 772 |
return result; |
| 773 |
} |
| 774 |
|
| 775 |
/* |
| 776 |
* ssh_statemach_act() runs the SSH state machine as far as it can without |
| 777 |
* blocking and without reaching the end. The data the pointer 'block' points |
| 778 |
* to will be set to TRUE if the libssh2 function returns LIBSSH2_ERROR_EAGAIN |
| 779 |
* meaning it wants to be called again when the socket is ready |
| 780 |
*/ |
| 781 |
|
| 782 |
static CURLcode ssh_statemach_act(struct connectdata *conn, bool *block) |
| 783 |
{ |
| 784 |
CURLcode result = CURLE_OK; |
| 785 |
struct Curl_easy *data = conn->data; |
| 786 |
struct SSHPROTO *sftp_scp = data->req.protop; |
| 787 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 788 |
curl_socket_t sock = conn->sock[FIRSTSOCKET]; |
| 789 |
char *new_readdir_line; |
| 790 |
int rc = LIBSSH2_ERROR_NONE; |
| 791 |
int err; |
| 792 |
int seekerr = CURL_SEEKFUNC_OK; |
| 793 |
*block = 0; /* we're not blocking by default */ |
| 794 |
|
| 795 |
do { |
| 796 |
|
| 797 |
switch(sshc->state) { |
| 798 |
case SSH_INIT: |
| 799 |
sshc->secondCreateDirs = 0; |
| 800 |
sshc->nextstate = SSH_NO_STATE; |
| 801 |
sshc->actualcode = CURLE_OK; |
| 802 |
|
| 803 |
/* Set libssh2 to non-blocking, since everything internally is |
| 804 |
non-blocking */ |
| 805 |
libssh2_session_set_blocking(sshc->ssh_session, 0); |
| 806 |
|
| 807 |
result = ssh_force_knownhost_key_type(conn); |
| 808 |
if(result) { |
| 809 |
state(conn, SSH_SESSION_FREE); |
| 810 |
break; |
| 811 |
} |
| 812 |
|
| 813 |
state(conn, SSH_S_STARTUP); |
| 814 |
/* FALLTHROUGH */ |
| 815 |
|
| 816 |
case SSH_S_STARTUP: |
| 817 |
rc = libssh2_session_startup(sshc->ssh_session, (int)sock); |
| 818 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 819 |
break; |
| 820 |
} |
| 821 |
if(rc) { |
| 822 |
char *err_msg = NULL; |
| 823 |
(void)libssh2_session_last_error(sshc->ssh_session, &err_msg, NULL, 0); |
| 824 |
failf(data, "Failure establishing ssh session: %d, %s", rc, err_msg); |
| 825 |
|
| 826 |
state(conn, SSH_SESSION_FREE); |
| 827 |
sshc->actualcode = CURLE_FAILED_INIT; |
| 828 |
break; |
| 829 |
} |
| 830 |
|
| 831 |
state(conn, SSH_HOSTKEY); |
| 832 |
|
| 833 |
/* FALLTHROUGH */ |
| 834 |
case SSH_HOSTKEY: |
| 835 |
/* |
| 836 |
* Before we authenticate we should check the hostkey's fingerprint |
| 837 |
* against our known hosts. How that is handled (reading from file, |
| 838 |
* whatever) is up to us. |
| 839 |
*/ |
| 840 |
result = ssh_check_fingerprint(conn); |
| 841 |
if(!result) |
| 842 |
state(conn, SSH_AUTHLIST); |
| 843 |
/* ssh_check_fingerprint sets state appropriately on error */ |
| 844 |
break; |
| 845 |
|
| 846 |
case SSH_AUTHLIST: |
| 847 |
/* |
| 848 |
* Figure out authentication methods |
| 849 |
* NB: As soon as we have provided a username to an openssh server we |
| 850 |
* must never change it later. Thus, always specify the correct username |
| 851 |
* here, even though the libssh2 docs kind of indicate that it should be |
| 852 |
* possible to get a 'generic' list (not user-specific) of authentication |
| 853 |
* methods, presumably with a blank username. That won't work in my |
| 854 |
* experience. |
| 855 |
* So always specify it here. |
| 856 |
*/ |
| 857 |
sshc->authlist = libssh2_userauth_list(sshc->ssh_session, |
| 858 |
conn->user, |
| 859 |
curlx_uztoui(strlen(conn->user))); |
| 860 |
|
| 861 |
if(!sshc->authlist) { |
| 862 |
if(libssh2_userauth_authenticated(sshc->ssh_session)) { |
| 863 |
sshc->authed = TRUE; |
| 864 |
infof(data, "SSH user accepted with no authentication\n"); |
| 865 |
state(conn, SSH_AUTH_DONE); |
| 866 |
break; |
| 867 |
} |
| 868 |
err = libssh2_session_last_errno(sshc->ssh_session); |
| 869 |
if(err == LIBSSH2_ERROR_EAGAIN) |
| 870 |
rc = LIBSSH2_ERROR_EAGAIN; |
| 871 |
else { |
| 872 |
state(conn, SSH_SESSION_FREE); |
| 873 |
sshc->actualcode = libssh2_session_error_to_CURLE(err); |
| 874 |
} |
| 875 |
break; |
| 876 |
} |
| 877 |
infof(data, "SSH authentication methods available: %s\n", |
| 878 |
sshc->authlist); |
| 879 |
|
| 880 |
state(conn, SSH_AUTH_PKEY_INIT); |
| 881 |
break; |
| 882 |
|
| 883 |
case SSH_AUTH_PKEY_INIT: |
| 884 |
/* |
| 885 |
* Check the supported auth types in the order I feel is most secure |
| 886 |
* with the requested type of authentication |
| 887 |
*/ |
| 888 |
sshc->authed = FALSE; |
| 889 |
|
| 890 |
if((data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) && |
| 891 |
(strstr(sshc->authlist, "publickey") != NULL)) { |
| 892 |
bool out_of_memory = FALSE; |
| 893 |
|
| 894 |
sshc->rsa_pub = sshc->rsa = NULL; |
| 895 |
|
| 896 |
if(data->set.str[STRING_SSH_PRIVATE_KEY]) |
| 897 |
sshc->rsa = strdup(data->set.str[STRING_SSH_PRIVATE_KEY]); |
| 898 |
else { |
| 899 |
/* To ponder about: should really the lib be messing about with the |
| 900 |
HOME environment variable etc? */ |
| 901 |
char *home = curl_getenv("HOME"); |
| 902 |
|
| 903 |
/* If no private key file is specified, try some common paths. */ |
| 904 |
if(home) { |
| 905 |
/* Try ~/.ssh first. */ |
| 906 |
sshc->rsa = aprintf("%s/.ssh/id_rsa", home); |
| 907 |
if(!sshc->rsa) |
| 908 |
out_of_memory = TRUE; |
| 909 |
else if(access(sshc->rsa, R_OK) != 0) { |
| 910 |
Curl_safefree(sshc->rsa); |
| 911 |
sshc->rsa = aprintf("%s/.ssh/id_dsa", home); |
| 912 |
if(!sshc->rsa) |
| 913 |
out_of_memory = TRUE; |
| 914 |
else if(access(sshc->rsa, R_OK) != 0) { |
| 915 |
Curl_safefree(sshc->rsa); |
| 916 |
} |
| 917 |
} |
| 918 |
free(home); |
| 919 |
} |
| 920 |
if(!out_of_memory && !sshc->rsa) { |
| 921 |
/* Nothing found; try the current dir. */ |
| 922 |
sshc->rsa = strdup("id_rsa"); |
| 923 |
if(sshc->rsa && access(sshc->rsa, R_OK) != 0) { |
| 924 |
Curl_safefree(sshc->rsa); |
| 925 |
sshc->rsa = strdup("id_dsa"); |
| 926 |
if(sshc->rsa && access(sshc->rsa, R_OK) != 0) { |
| 927 |
Curl_safefree(sshc->rsa); |
| 928 |
/* Out of guesses. Set to the empty string to avoid |
| 929 |
* surprising info messages. */ |
| 930 |
sshc->rsa = strdup(""); |
| 931 |
} |
| 932 |
} |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
/* |
| 937 |
* Unless the user explicitly specifies a public key file, let |
| 938 |
* libssh2 extract the public key from the private key file. |
| 939 |
* This is done by simply passing sshc->rsa_pub = NULL. |
| 940 |
*/ |
| 941 |
if(data->set.str[STRING_SSH_PUBLIC_KEY] |
| 942 |
/* treat empty string the same way as NULL */ |
| 943 |
&& data->set.str[STRING_SSH_PUBLIC_KEY][0]) { |
| 944 |
sshc->rsa_pub = strdup(data->set.str[STRING_SSH_PUBLIC_KEY]); |
| 945 |
if(!sshc->rsa_pub) |
| 946 |
out_of_memory = TRUE; |
| 947 |
} |
| 948 |
|
| 949 |
if(out_of_memory || sshc->rsa == NULL) { |
| 950 |
Curl_safefree(sshc->rsa); |
| 951 |
Curl_safefree(sshc->rsa_pub); |
| 952 |
state(conn, SSH_SESSION_FREE); |
| 953 |
sshc->actualcode = CURLE_OUT_OF_MEMORY; |
| 954 |
break; |
| 955 |
} |
| 956 |
|
| 957 |
sshc->passphrase = data->set.ssl.key_passwd; |
| 958 |
if(!sshc->passphrase) |
| 959 |
sshc->passphrase = ""; |
| 960 |
|
| 961 |
if(sshc->rsa_pub) |
| 962 |
infof(data, "Using SSH public key file '%s'\n", sshc->rsa_pub); |
| 963 |
infof(data, "Using SSH private key file '%s'\n", sshc->rsa); |
| 964 |
|
| 965 |
state(conn, SSH_AUTH_PKEY); |
| 966 |
} |
| 967 |
else { |
| 968 |
state(conn, SSH_AUTH_PASS_INIT); |
| 969 |
} |
| 970 |
break; |
| 971 |
|
| 972 |
case SSH_AUTH_PKEY: |
| 973 |
/* The function below checks if the files exists, no need to stat() here. |
| 974 |
*/ |
| 975 |
rc = libssh2_userauth_publickey_fromfile_ex(sshc->ssh_session, |
| 976 |
conn->user, |
| 977 |
curlx_uztoui( |
| 978 |
strlen(conn->user)), |
| 979 |
sshc->rsa_pub, |
| 980 |
sshc->rsa, sshc->passphrase); |
| 981 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 982 |
break; |
| 983 |
} |
| 984 |
|
| 985 |
Curl_safefree(sshc->rsa_pub); |
| 986 |
Curl_safefree(sshc->rsa); |
| 987 |
|
| 988 |
if(rc == 0) { |
| 989 |
sshc->authed = TRUE; |
| 990 |
infof(data, "Initialized SSH public key authentication\n"); |
| 991 |
state(conn, SSH_AUTH_DONE); |
| 992 |
} |
| 993 |
else { |
| 994 |
char *err_msg = NULL; |
| 995 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 996 |
&err_msg, NULL, 0); |
| 997 |
infof(data, "SSH public key authentication failed: %s\n", err_msg); |
| 998 |
state(conn, SSH_AUTH_PASS_INIT); |
| 999 |
rc = 0; /* clear rc and continue */ |
| 1000 |
} |
| 1001 |
break; |
| 1002 |
|
| 1003 |
case SSH_AUTH_PASS_INIT: |
| 1004 |
if((data->set.ssh_auth_types & CURLSSH_AUTH_PASSWORD) && |
| 1005 |
(strstr(sshc->authlist, "password") != NULL)) { |
| 1006 |
state(conn, SSH_AUTH_PASS); |
| 1007 |
} |
| 1008 |
else { |
| 1009 |
state(conn, SSH_AUTH_HOST_INIT); |
| 1010 |
rc = 0; /* clear rc and continue */ |
| 1011 |
} |
| 1012 |
break; |
| 1013 |
|
| 1014 |
case SSH_AUTH_PASS: |
| 1015 |
rc = libssh2_userauth_password_ex(sshc->ssh_session, conn->user, |
| 1016 |
curlx_uztoui(strlen(conn->user)), |
| 1017 |
conn->passwd, |
| 1018 |
curlx_uztoui(strlen(conn->passwd)), |
| 1019 |
NULL); |
| 1020 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1021 |
break; |
| 1022 |
} |
| 1023 |
if(rc == 0) { |
| 1024 |
sshc->authed = TRUE; |
| 1025 |
infof(data, "Initialized password authentication\n"); |
| 1026 |
state(conn, SSH_AUTH_DONE); |
| 1027 |
} |
| 1028 |
else { |
| 1029 |
state(conn, SSH_AUTH_HOST_INIT); |
| 1030 |
rc = 0; /* clear rc and continue */ |
| 1031 |
} |
| 1032 |
break; |
| 1033 |
|
| 1034 |
case SSH_AUTH_HOST_INIT: |
| 1035 |
if((data->set.ssh_auth_types & CURLSSH_AUTH_HOST) && |
| 1036 |
(strstr(sshc->authlist, "hostbased") != NULL)) { |
| 1037 |
state(conn, SSH_AUTH_HOST); |
| 1038 |
} |
| 1039 |
else { |
| 1040 |
state(conn, SSH_AUTH_AGENT_INIT); |
| 1041 |
} |
| 1042 |
break; |
| 1043 |
|
| 1044 |
case SSH_AUTH_HOST: |
| 1045 |
state(conn, SSH_AUTH_AGENT_INIT); |
| 1046 |
break; |
| 1047 |
|
| 1048 |
case SSH_AUTH_AGENT_INIT: |
| 1049 |
#ifdef HAVE_LIBSSH2_AGENT_API |
| 1050 |
if((data->set.ssh_auth_types & CURLSSH_AUTH_AGENT) |
| 1051 |
&& (strstr(sshc->authlist, "publickey") != NULL)) { |
| 1052 |
|
| 1053 |
/* Connect to the ssh-agent */ |
| 1054 |
/* The agent could be shared by a curl thread i believe |
| 1055 |
but nothing obvious as keys can be added/removed at any time */ |
| 1056 |
if(!sshc->ssh_agent) { |
| 1057 |
sshc->ssh_agent = libssh2_agent_init(sshc->ssh_session); |
| 1058 |
if(!sshc->ssh_agent) { |
| 1059 |
infof(data, "Could not create agent object\n"); |
| 1060 |
|
| 1061 |
state(conn, SSH_AUTH_KEY_INIT); |
| 1062 |
break; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
rc = libssh2_agent_connect(sshc->ssh_agent); |
| 1067 |
if(rc == LIBSSH2_ERROR_EAGAIN) |
| 1068 |
break; |
| 1069 |
if(rc < 0) { |
| 1070 |
infof(data, "Failure connecting to agent\n"); |
| 1071 |
state(conn, SSH_AUTH_KEY_INIT); |
| 1072 |
rc = 0; /* clear rc and continue */ |
| 1073 |
} |
| 1074 |
else { |
| 1075 |
state(conn, SSH_AUTH_AGENT_LIST); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
else |
| 1079 |
#endif /* HAVE_LIBSSH2_AGENT_API */ |
| 1080 |
state(conn, SSH_AUTH_KEY_INIT); |
| 1081 |
break; |
| 1082 |
|
| 1083 |
case SSH_AUTH_AGENT_LIST: |
| 1084 |
#ifdef HAVE_LIBSSH2_AGENT_API |
| 1085 |
rc = libssh2_agent_list_identities(sshc->ssh_agent); |
| 1086 |
|
| 1087 |
if(rc == LIBSSH2_ERROR_EAGAIN) |
| 1088 |
break; |
| 1089 |
if(rc < 0) { |
| 1090 |
infof(data, "Failure requesting identities to agent\n"); |
| 1091 |
state(conn, SSH_AUTH_KEY_INIT); |
| 1092 |
rc = 0; /* clear rc and continue */ |
| 1093 |
} |
| 1094 |
else { |
| 1095 |
state(conn, SSH_AUTH_AGENT); |
| 1096 |
sshc->sshagent_prev_identity = NULL; |
| 1097 |
} |
| 1098 |
#endif |
| 1099 |
break; |
| 1100 |
|
| 1101 |
case SSH_AUTH_AGENT: |
| 1102 |
#ifdef HAVE_LIBSSH2_AGENT_API |
| 1103 |
/* as prev_identity evolves only after an identity user auth finished we |
| 1104 |
can safely request it again as long as EAGAIN is returned here or by |
| 1105 |
libssh2_agent_userauth */ |
| 1106 |
rc = libssh2_agent_get_identity(sshc->ssh_agent, |
| 1107 |
&sshc->sshagent_identity, |
| 1108 |
sshc->sshagent_prev_identity); |
| 1109 |
if(rc == LIBSSH2_ERROR_EAGAIN) |
| 1110 |
break; |
| 1111 |
|
| 1112 |
if(rc == 0) { |
| 1113 |
rc = libssh2_agent_userauth(sshc->ssh_agent, conn->user, |
| 1114 |
sshc->sshagent_identity); |
| 1115 |
|
| 1116 |
if(rc < 0) { |
| 1117 |
if(rc != LIBSSH2_ERROR_EAGAIN) { |
| 1118 |
/* tried and failed? go to next identity */ |
| 1119 |
sshc->sshagent_prev_identity = sshc->sshagent_identity; |
| 1120 |
} |
| 1121 |
break; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
if(rc < 0) |
| 1126 |
infof(data, "Failure requesting identities to agent\n"); |
| 1127 |
else if(rc == 1) |
| 1128 |
infof(data, "No identity would match\n"); |
| 1129 |
|
| 1130 |
if(rc == LIBSSH2_ERROR_NONE) { |
| 1131 |
sshc->authed = TRUE; |
| 1132 |
infof(data, "Agent based authentication successful\n"); |
| 1133 |
state(conn, SSH_AUTH_DONE); |
| 1134 |
} |
| 1135 |
else { |
| 1136 |
state(conn, SSH_AUTH_KEY_INIT); |
| 1137 |
rc = 0; /* clear rc and continue */ |
| 1138 |
} |
| 1139 |
#endif |
| 1140 |
break; |
| 1141 |
|
| 1142 |
case SSH_AUTH_KEY_INIT: |
| 1143 |
if((data->set.ssh_auth_types & CURLSSH_AUTH_KEYBOARD) |
| 1144 |
&& (strstr(sshc->authlist, "keyboard-interactive") != NULL)) { |
| 1145 |
state(conn, SSH_AUTH_KEY); |
| 1146 |
} |
| 1147 |
else { |
| 1148 |
state(conn, SSH_AUTH_DONE); |
| 1149 |
} |
| 1150 |
break; |
| 1151 |
|
| 1152 |
case SSH_AUTH_KEY: |
| 1153 |
/* Authentication failed. Continue with keyboard-interactive now. */ |
| 1154 |
rc = libssh2_userauth_keyboard_interactive_ex(sshc->ssh_session, |
| 1155 |
conn->user, |
| 1156 |
curlx_uztoui( |
| 1157 |
strlen(conn->user)), |
| 1158 |
&kbd_callback); |
| 1159 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1160 |
break; |
| 1161 |
} |
| 1162 |
if(rc == 0) { |
| 1163 |
sshc->authed = TRUE; |
| 1164 |
infof(data, "Initialized keyboard interactive authentication\n"); |
| 1165 |
} |
| 1166 |
state(conn, SSH_AUTH_DONE); |
| 1167 |
break; |
| 1168 |
|
| 1169 |
case SSH_AUTH_DONE: |
| 1170 |
if(!sshc->authed) { |
| 1171 |
failf(data, "Authentication failure"); |
| 1172 |
state(conn, SSH_SESSION_FREE); |
| 1173 |
sshc->actualcode = CURLE_LOGIN_DENIED; |
| 1174 |
break; |
| 1175 |
} |
| 1176 |
|
| 1177 |
/* |
| 1178 |
* At this point we have an authenticated ssh session. |
| 1179 |
*/ |
| 1180 |
infof(data, "Authentication complete\n"); |
| 1181 |
|
| 1182 |
Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSH is connected */ |
| 1183 |
|
| 1184 |
conn->sockfd = sock; |
| 1185 |
conn->writesockfd = CURL_SOCKET_BAD; |
| 1186 |
|
| 1187 |
if(conn->handler->protocol == CURLPROTO_SFTP) { |
| 1188 |
state(conn, SSH_SFTP_INIT); |
| 1189 |
break; |
| 1190 |
} |
| 1191 |
infof(data, "SSH CONNECT phase done\n"); |
| 1192 |
state(conn, SSH_STOP); |
| 1193 |
break; |
| 1194 |
|
| 1195 |
case SSH_SFTP_INIT: |
| 1196 |
/* |
| 1197 |
* Start the libssh2 sftp session |
| 1198 |
*/ |
| 1199 |
sshc->sftp_session = libssh2_sftp_init(sshc->ssh_session); |
| 1200 |
if(!sshc->sftp_session) { |
| 1201 |
char *err_msg = NULL; |
| 1202 |
if(libssh2_session_last_errno(sshc->ssh_session) == |
| 1203 |
LIBSSH2_ERROR_EAGAIN) { |
| 1204 |
rc = LIBSSH2_ERROR_EAGAIN; |
| 1205 |
break; |
| 1206 |
} |
| 1207 |
|
| 1208 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 1209 |
&err_msg, NULL, 0); |
| 1210 |
failf(data, "Failure initializing sftp session: %s", err_msg); |
| 1211 |
state(conn, SSH_SESSION_FREE); |
| 1212 |
sshc->actualcode = CURLE_FAILED_INIT; |
| 1213 |
break; |
| 1214 |
} |
| 1215 |
state(conn, SSH_SFTP_REALPATH); |
| 1216 |
break; |
| 1217 |
|
| 1218 |
case SSH_SFTP_REALPATH: |
| 1219 |
{ |
| 1220 |
char tempHome[PATH_MAX]; |
| 1221 |
|
| 1222 |
/* |
| 1223 |
* Get the "home" directory |
| 1224 |
*/ |
| 1225 |
rc = sftp_libssh2_realpath(sshc->sftp_session, ".", |
| 1226 |
tempHome, PATH_MAX-1); |
| 1227 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1228 |
break; |
| 1229 |
} |
| 1230 |
if(rc > 0) { |
| 1231 |
/* It seems that this string is not always NULL terminated */ |
| 1232 |
tempHome[rc] = '\0'; |
| 1233 |
sshc->homedir = strdup(tempHome); |
| 1234 |
if(!sshc->homedir) { |
| 1235 |
state(conn, SSH_SFTP_CLOSE); |
| 1236 |
sshc->actualcode = CURLE_OUT_OF_MEMORY; |
| 1237 |
break; |
| 1238 |
} |
| 1239 |
conn->data->state.most_recent_ftp_entrypath = sshc->homedir; |
| 1240 |
} |
| 1241 |
else { |
| 1242 |
/* Return the error type */ |
| 1243 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1244 |
if(err) |
| 1245 |
result = sftp_libssh2_error_to_CURLE(err); |
| 1246 |
else |
| 1247 |
/* in this case, the error wasn't in the SFTP level but for example |
| 1248 |
a time-out or similar */ |
| 1249 |
result = CURLE_SSH; |
| 1250 |
sshc->actualcode = result; |
| 1251 |
DEBUGF(infof(data, "error = %d makes libcurl = %d\n", |
| 1252 |
err, (int)result)); |
| 1253 |
state(conn, SSH_STOP); |
| 1254 |
break; |
| 1255 |
} |
| 1256 |
} |
| 1257 |
/* This is the last step in the SFTP connect phase. Do note that while |
| 1258 |
we get the homedir here, we get the "workingpath" in the DO action |
| 1259 |
since the homedir will remain the same between request but the |
| 1260 |
working path will not. */ |
| 1261 |
DEBUGF(infof(data, "SSH CONNECT phase done\n")); |
| 1262 |
state(conn, SSH_STOP); |
| 1263 |
break; |
| 1264 |
|
| 1265 |
case SSH_SFTP_QUOTE_INIT: |
| 1266 |
|
| 1267 |
result = Curl_getworkingpath(conn, sshc->homedir, &sftp_scp->path); |
| 1268 |
if(result) { |
| 1269 |
sshc->actualcode = result; |
| 1270 |
state(conn, SSH_STOP); |
| 1271 |
break; |
| 1272 |
} |
| 1273 |
|
| 1274 |
if(data->set.quote) { |
| 1275 |
infof(data, "Sending quote commands\n"); |
| 1276 |
sshc->quote_item = data->set.quote; |
| 1277 |
state(conn, SSH_SFTP_QUOTE); |
| 1278 |
} |
| 1279 |
else { |
| 1280 |
state(conn, SSH_SFTP_GETINFO); |
| 1281 |
} |
| 1282 |
break; |
| 1283 |
|
| 1284 |
case SSH_SFTP_POSTQUOTE_INIT: |
| 1285 |
if(data->set.postquote) { |
| 1286 |
infof(data, "Sending quote commands\n"); |
| 1287 |
sshc->quote_item = data->set.postquote; |
| 1288 |
state(conn, SSH_SFTP_QUOTE); |
| 1289 |
} |
| 1290 |
else { |
| 1291 |
state(conn, SSH_STOP); |
| 1292 |
} |
| 1293 |
break; |
| 1294 |
|
| 1295 |
case SSH_SFTP_QUOTE: |
| 1296 |
/* Send any quote commands */ |
| 1297 |
{ |
| 1298 |
const char *cp; |
| 1299 |
|
| 1300 |
/* |
| 1301 |
* Support some of the "FTP" commands |
| 1302 |
* |
| 1303 |
* 'sshc->quote_item' is already verified to be non-NULL before it |
| 1304 |
* switched to this state. |
| 1305 |
*/ |
| 1306 |
char *cmd = sshc->quote_item->data; |
| 1307 |
sshc->acceptfail = FALSE; |
| 1308 |
|
| 1309 |
/* if a command starts with an asterisk, which a legal SFTP command never |
| 1310 |
can, the command will be allowed to fail without it causing any |
| 1311 |
aborts or cancels etc. It will cause libcurl to act as if the command |
| 1312 |
is successful, whatever the server reponds. */ |
| 1313 |
|
| 1314 |
if(cmd[0] == '*') { |
| 1315 |
cmd++; |
| 1316 |
sshc->acceptfail = TRUE; |
| 1317 |
} |
| 1318 |
|
| 1319 |
if(strcasecompare("pwd", cmd)) { |
| 1320 |
/* output debug output if that is requested */ |
| 1321 |
char *tmp = aprintf("257 \"%s\" is current directory.\n", |
| 1322 |
sftp_scp->path); |
| 1323 |
if(!tmp) { |
| 1324 |
result = CURLE_OUT_OF_MEMORY; |
| 1325 |
state(conn, SSH_SFTP_CLOSE); |
| 1326 |
sshc->nextstate = SSH_NO_STATE; |
| 1327 |
break; |
| 1328 |
} |
| 1329 |
if(data->set.verbose) { |
| 1330 |
Curl_debug(data, CURLINFO_HEADER_OUT, (char *)"PWD\n", 4); |
| 1331 |
Curl_debug(data, CURLINFO_HEADER_IN, tmp, strlen(tmp)); |
| 1332 |
} |
| 1333 |
/* this sends an FTP-like "header" to the header callback so that the |
| 1334 |
current directory can be read very similar to how it is read when |
| 1335 |
using ordinary FTP. */ |
| 1336 |
result = Curl_client_write(conn, CLIENTWRITE_HEADER, tmp, strlen(tmp)); |
| 1337 |
free(tmp); |
| 1338 |
if(result) { |
| 1339 |
state(conn, SSH_SFTP_CLOSE); |
| 1340 |
sshc->nextstate = SSH_NO_STATE; |
| 1341 |
sshc->actualcode = result; |
| 1342 |
} |
| 1343 |
else |
| 1344 |
state(conn, SSH_SFTP_NEXT_QUOTE); |
| 1345 |
break; |
| 1346 |
} |
| 1347 |
{ |
| 1348 |
/* |
| 1349 |
* the arguments following the command must be separated from the |
| 1350 |
* command with a space so we can check for it unconditionally |
| 1351 |
*/ |
| 1352 |
cp = strchr(cmd, ' '); |
| 1353 |
if(cp == NULL) { |
| 1354 |
failf(data, "Syntax error in SFTP command. Supply parameter(s)!"); |
| 1355 |
state(conn, SSH_SFTP_CLOSE); |
| 1356 |
sshc->nextstate = SSH_NO_STATE; |
| 1357 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1358 |
break; |
| 1359 |
} |
| 1360 |
|
| 1361 |
/* |
| 1362 |
* also, every command takes at least one argument so we get that |
| 1363 |
* first argument right now |
| 1364 |
*/ |
| 1365 |
result = Curl_get_pathname(&cp, &sshc->quote_path1, sshc->homedir); |
| 1366 |
if(result) { |
| 1367 |
if(result == CURLE_OUT_OF_MEMORY) |
| 1368 |
failf(data, "Out of memory"); |
| 1369 |
else |
| 1370 |
failf(data, "Syntax error: Bad first parameter"); |
| 1371 |
state(conn, SSH_SFTP_CLOSE); |
| 1372 |
sshc->nextstate = SSH_NO_STATE; |
| 1373 |
sshc->actualcode = result; |
| 1374 |
break; |
| 1375 |
} |
| 1376 |
|
| 1377 |
/* |
| 1378 |
* SFTP is a binary protocol, so we don't send text commands |
| 1379 |
* to the server. Instead, we scan for commands used by |
| 1380 |
* OpenSSH's sftp program and call the appropriate libssh2 |
| 1381 |
* functions. |
| 1382 |
*/ |
| 1383 |
if(strncasecompare(cmd, "chgrp ", 6) || |
| 1384 |
strncasecompare(cmd, "chmod ", 6) || |
| 1385 |
strncasecompare(cmd, "chown ", 6) ) { |
| 1386 |
/* attribute change */ |
| 1387 |
|
| 1388 |
/* sshc->quote_path1 contains the mode to set */ |
| 1389 |
/* get the destination */ |
| 1390 |
result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir); |
| 1391 |
if(result) { |
| 1392 |
if(result == CURLE_OUT_OF_MEMORY) |
| 1393 |
failf(data, "Out of memory"); |
| 1394 |
else |
| 1395 |
failf(data, "Syntax error in chgrp/chmod/chown: " |
| 1396 |
"Bad second parameter"); |
| 1397 |
Curl_safefree(sshc->quote_path1); |
| 1398 |
state(conn, SSH_SFTP_CLOSE); |
| 1399 |
sshc->nextstate = SSH_NO_STATE; |
| 1400 |
sshc->actualcode = result; |
| 1401 |
break; |
| 1402 |
} |
| 1403 |
memset(&sshc->quote_attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES)); |
| 1404 |
state(conn, SSH_SFTP_QUOTE_STAT); |
| 1405 |
break; |
| 1406 |
} |
| 1407 |
if(strncasecompare(cmd, "ln ", 3) || |
| 1408 |
strncasecompare(cmd, "symlink ", 8)) { |
| 1409 |
/* symbolic linking */ |
| 1410 |
/* sshc->quote_path1 is the source */ |
| 1411 |
/* get the destination */ |
| 1412 |
result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir); |
| 1413 |
if(result) { |
| 1414 |
if(result == CURLE_OUT_OF_MEMORY) |
| 1415 |
failf(data, "Out of memory"); |
| 1416 |
else |
| 1417 |
failf(data, |
| 1418 |
"Syntax error in ln/symlink: Bad second parameter"); |
| 1419 |
Curl_safefree(sshc->quote_path1); |
| 1420 |
state(conn, SSH_SFTP_CLOSE); |
| 1421 |
sshc->nextstate = SSH_NO_STATE; |
| 1422 |
sshc->actualcode = result; |
| 1423 |
break; |
| 1424 |
} |
| 1425 |
state(conn, SSH_SFTP_QUOTE_SYMLINK); |
| 1426 |
break; |
| 1427 |
} |
| 1428 |
else if(strncasecompare(cmd, "mkdir ", 6)) { |
| 1429 |
/* create dir */ |
| 1430 |
state(conn, SSH_SFTP_QUOTE_MKDIR); |
| 1431 |
break; |
| 1432 |
} |
| 1433 |
else if(strncasecompare(cmd, "rename ", 7)) { |
| 1434 |
/* rename file */ |
| 1435 |
/* first param is the source path */ |
| 1436 |
/* second param is the dest. path */ |
| 1437 |
result = Curl_get_pathname(&cp, &sshc->quote_path2, sshc->homedir); |
| 1438 |
if(result) { |
| 1439 |
if(result == CURLE_OUT_OF_MEMORY) |
| 1440 |
failf(data, "Out of memory"); |
| 1441 |
else |
| 1442 |
failf(data, "Syntax error in rename: Bad second parameter"); |
| 1443 |
Curl_safefree(sshc->quote_path1); |
| 1444 |
state(conn, SSH_SFTP_CLOSE); |
| 1445 |
sshc->nextstate = SSH_NO_STATE; |
| 1446 |
sshc->actualcode = result; |
| 1447 |
break; |
| 1448 |
} |
| 1449 |
state(conn, SSH_SFTP_QUOTE_RENAME); |
| 1450 |
break; |
| 1451 |
} |
| 1452 |
else if(strncasecompare(cmd, "rmdir ", 6)) { |
| 1453 |
/* delete dir */ |
| 1454 |
state(conn, SSH_SFTP_QUOTE_RMDIR); |
| 1455 |
break; |
| 1456 |
} |
| 1457 |
else if(strncasecompare(cmd, "rm ", 3)) { |
| 1458 |
state(conn, SSH_SFTP_QUOTE_UNLINK); |
| 1459 |
break; |
| 1460 |
} |
| 1461 |
#ifdef HAS_STATVFS_SUPPORT |
| 1462 |
else if(strncasecompare(cmd, "statvfs ", 8)) { |
| 1463 |
state(conn, SSH_SFTP_QUOTE_STATVFS); |
| 1464 |
break; |
| 1465 |
} |
| 1466 |
#endif |
| 1467 |
|
| 1468 |
failf(data, "Unknown SFTP command"); |
| 1469 |
Curl_safefree(sshc->quote_path1); |
| 1470 |
Curl_safefree(sshc->quote_path2); |
| 1471 |
state(conn, SSH_SFTP_CLOSE); |
| 1472 |
sshc->nextstate = SSH_NO_STATE; |
| 1473 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1474 |
break; |
| 1475 |
} |
| 1476 |
} |
| 1477 |
break; |
| 1478 |
|
| 1479 |
case SSH_SFTP_NEXT_QUOTE: |
| 1480 |
Curl_safefree(sshc->quote_path1); |
| 1481 |
Curl_safefree(sshc->quote_path2); |
| 1482 |
|
| 1483 |
sshc->quote_item = sshc->quote_item->next; |
| 1484 |
|
| 1485 |
if(sshc->quote_item) { |
| 1486 |
state(conn, SSH_SFTP_QUOTE); |
| 1487 |
} |
| 1488 |
else { |
| 1489 |
if(sshc->nextstate != SSH_NO_STATE) { |
| 1490 |
state(conn, sshc->nextstate); |
| 1491 |
sshc->nextstate = SSH_NO_STATE; |
| 1492 |
} |
| 1493 |
else { |
| 1494 |
state(conn, SSH_SFTP_GETINFO); |
| 1495 |
} |
| 1496 |
} |
| 1497 |
break; |
| 1498 |
|
| 1499 |
case SSH_SFTP_QUOTE_STAT: |
| 1500 |
{ |
| 1501 |
char *cmd = sshc->quote_item->data; |
| 1502 |
sshc->acceptfail = FALSE; |
| 1503 |
|
| 1504 |
/* if a command starts with an asterisk, which a legal SFTP command never |
| 1505 |
can, the command will be allowed to fail without it causing any |
| 1506 |
aborts or cancels etc. It will cause libcurl to act as if the command |
| 1507 |
is successful, whatever the server reponds. */ |
| 1508 |
|
| 1509 |
if(cmd[0] == '*') { |
| 1510 |
cmd++; |
| 1511 |
sshc->acceptfail = TRUE; |
| 1512 |
} |
| 1513 |
|
| 1514 |
if(!strncasecompare(cmd, "chmod", 5)) { |
| 1515 |
/* Since chown and chgrp only set owner OR group but libssh2 wants to |
| 1516 |
* set them both at once, we need to obtain the current ownership |
| 1517 |
* first. This takes an extra protocol round trip. |
| 1518 |
*/ |
| 1519 |
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2, |
| 1520 |
curlx_uztoui(strlen(sshc->quote_path2)), |
| 1521 |
LIBSSH2_SFTP_STAT, |
| 1522 |
&sshc->quote_attrs); |
| 1523 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1524 |
break; |
| 1525 |
} |
| 1526 |
if(rc != 0 && !sshc->acceptfail) { /* get those attributes */ |
| 1527 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1528 |
Curl_safefree(sshc->quote_path1); |
| 1529 |
Curl_safefree(sshc->quote_path2); |
| 1530 |
failf(data, "Attempt to get SFTP stats failed: %s", |
| 1531 |
sftp_libssh2_strerror(err)); |
| 1532 |
state(conn, SSH_SFTP_CLOSE); |
| 1533 |
sshc->nextstate = SSH_NO_STATE; |
| 1534 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1535 |
break; |
| 1536 |
} |
| 1537 |
} |
| 1538 |
|
| 1539 |
/* Now set the new attributes... */ |
| 1540 |
if(strncasecompare(cmd, "chgrp", 5)) { |
| 1541 |
sshc->quote_attrs.gid = strtoul(sshc->quote_path1, NULL, 10); |
| 1542 |
sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID; |
| 1543 |
if(sshc->quote_attrs.gid == 0 && !ISDIGIT(sshc->quote_path1[0]) && |
| 1544 |
!sshc->acceptfail) { |
| 1545 |
Curl_safefree(sshc->quote_path1); |
| 1546 |
Curl_safefree(sshc->quote_path2); |
| 1547 |
failf(data, "Syntax error: chgrp gid not a number"); |
| 1548 |
state(conn, SSH_SFTP_CLOSE); |
| 1549 |
sshc->nextstate = SSH_NO_STATE; |
| 1550 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1551 |
break; |
| 1552 |
} |
| 1553 |
} |
| 1554 |
else if(strncasecompare(cmd, "chmod", 5)) { |
| 1555 |
sshc->quote_attrs.permissions = strtoul(sshc->quote_path1, NULL, 8); |
| 1556 |
sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_PERMISSIONS; |
| 1557 |
/* permissions are octal */ |
| 1558 |
if(sshc->quote_attrs.permissions == 0 && |
| 1559 |
!ISDIGIT(sshc->quote_path1[0])) { |
| 1560 |
Curl_safefree(sshc->quote_path1); |
| 1561 |
Curl_safefree(sshc->quote_path2); |
| 1562 |
failf(data, "Syntax error: chmod permissions not a number"); |
| 1563 |
state(conn, SSH_SFTP_CLOSE); |
| 1564 |
sshc->nextstate = SSH_NO_STATE; |
| 1565 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1566 |
break; |
| 1567 |
} |
| 1568 |
} |
| 1569 |
else if(strncasecompare(cmd, "chown", 5)) { |
| 1570 |
sshc->quote_attrs.uid = strtoul(sshc->quote_path1, NULL, 10); |
| 1571 |
sshc->quote_attrs.flags = LIBSSH2_SFTP_ATTR_UIDGID; |
| 1572 |
if(sshc->quote_attrs.uid == 0 && !ISDIGIT(sshc->quote_path1[0]) && |
| 1573 |
!sshc->acceptfail) { |
| 1574 |
Curl_safefree(sshc->quote_path1); |
| 1575 |
Curl_safefree(sshc->quote_path2); |
| 1576 |
failf(data, "Syntax error: chown uid not a number"); |
| 1577 |
state(conn, SSH_SFTP_CLOSE); |
| 1578 |
sshc->nextstate = SSH_NO_STATE; |
| 1579 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1580 |
break; |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
/* Now send the completed structure... */ |
| 1585 |
state(conn, SSH_SFTP_QUOTE_SETSTAT); |
| 1586 |
break; |
| 1587 |
} |
| 1588 |
|
| 1589 |
case SSH_SFTP_QUOTE_SETSTAT: |
| 1590 |
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sshc->quote_path2, |
| 1591 |
curlx_uztoui(strlen(sshc->quote_path2)), |
| 1592 |
LIBSSH2_SFTP_SETSTAT, |
| 1593 |
&sshc->quote_attrs); |
| 1594 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1595 |
break; |
| 1596 |
} |
| 1597 |
if(rc != 0 && !sshc->acceptfail) { |
| 1598 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1599 |
Curl_safefree(sshc->quote_path1); |
| 1600 |
Curl_safefree(sshc->quote_path2); |
| 1601 |
failf(data, "Attempt to set SFTP stats failed: %s", |
| 1602 |
sftp_libssh2_strerror(err)); |
| 1603 |
state(conn, SSH_SFTP_CLOSE); |
| 1604 |
sshc->nextstate = SSH_NO_STATE; |
| 1605 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1606 |
break; |
| 1607 |
} |
| 1608 |
state(conn, SSH_SFTP_NEXT_QUOTE); |
| 1609 |
break; |
| 1610 |
|
| 1611 |
case SSH_SFTP_QUOTE_SYMLINK: |
| 1612 |
rc = libssh2_sftp_symlink_ex(sshc->sftp_session, sshc->quote_path1, |
| 1613 |
curlx_uztoui(strlen(sshc->quote_path1)), |
| 1614 |
sshc->quote_path2, |
| 1615 |
curlx_uztoui(strlen(sshc->quote_path2)), |
| 1616 |
LIBSSH2_SFTP_SYMLINK); |
| 1617 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1618 |
break; |
| 1619 |
} |
| 1620 |
if(rc != 0 && !sshc->acceptfail) { |
| 1621 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1622 |
Curl_safefree(sshc->quote_path1); |
| 1623 |
Curl_safefree(sshc->quote_path2); |
| 1624 |
failf(data, "symlink command failed: %s", |
| 1625 |
sftp_libssh2_strerror(err)); |
| 1626 |
state(conn, SSH_SFTP_CLOSE); |
| 1627 |
sshc->nextstate = SSH_NO_STATE; |
| 1628 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1629 |
break; |
| 1630 |
} |
| 1631 |
state(conn, SSH_SFTP_NEXT_QUOTE); |
| 1632 |
break; |
| 1633 |
|
| 1634 |
case SSH_SFTP_QUOTE_MKDIR: |
| 1635 |
rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sshc->quote_path1, |
| 1636 |
curlx_uztoui(strlen(sshc->quote_path1)), |
| 1637 |
data->set.new_directory_perms); |
| 1638 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1639 |
break; |
| 1640 |
} |
| 1641 |
if(rc != 0 && !sshc->acceptfail) { |
| 1642 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1643 |
Curl_safefree(sshc->quote_path1); |
| 1644 |
failf(data, "mkdir command failed: %s", sftp_libssh2_strerror(err)); |
| 1645 |
state(conn, SSH_SFTP_CLOSE); |
| 1646 |
sshc->nextstate = SSH_NO_STATE; |
| 1647 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1648 |
break; |
| 1649 |
} |
| 1650 |
state(conn, SSH_SFTP_NEXT_QUOTE); |
| 1651 |
break; |
| 1652 |
|
| 1653 |
case SSH_SFTP_QUOTE_RENAME: |
| 1654 |
rc = libssh2_sftp_rename_ex(sshc->sftp_session, sshc->quote_path1, |
| 1655 |
curlx_uztoui(strlen(sshc->quote_path1)), |
| 1656 |
sshc->quote_path2, |
| 1657 |
curlx_uztoui(strlen(sshc->quote_path2)), |
| 1658 |
LIBSSH2_SFTP_RENAME_OVERWRITE | |
| 1659 |
LIBSSH2_SFTP_RENAME_ATOMIC | |
| 1660 |
LIBSSH2_SFTP_RENAME_NATIVE); |
| 1661 |
|
| 1662 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1663 |
break; |
| 1664 |
} |
| 1665 |
if(rc != 0 && !sshc->acceptfail) { |
| 1666 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1667 |
Curl_safefree(sshc->quote_path1); |
| 1668 |
Curl_safefree(sshc->quote_path2); |
| 1669 |
failf(data, "rename command failed: %s", sftp_libssh2_strerror(err)); |
| 1670 |
state(conn, SSH_SFTP_CLOSE); |
| 1671 |
sshc->nextstate = SSH_NO_STATE; |
| 1672 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1673 |
break; |
| 1674 |
} |
| 1675 |
state(conn, SSH_SFTP_NEXT_QUOTE); |
| 1676 |
break; |
| 1677 |
|
| 1678 |
case SSH_SFTP_QUOTE_RMDIR: |
| 1679 |
rc = libssh2_sftp_rmdir_ex(sshc->sftp_session, sshc->quote_path1, |
| 1680 |
curlx_uztoui(strlen(sshc->quote_path1))); |
| 1681 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1682 |
break; |
| 1683 |
} |
| 1684 |
if(rc != 0 && !sshc->acceptfail) { |
| 1685 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1686 |
Curl_safefree(sshc->quote_path1); |
| 1687 |
failf(data, "rmdir command failed: %s", sftp_libssh2_strerror(err)); |
| 1688 |
state(conn, SSH_SFTP_CLOSE); |
| 1689 |
sshc->nextstate = SSH_NO_STATE; |
| 1690 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1691 |
break; |
| 1692 |
} |
| 1693 |
state(conn, SSH_SFTP_NEXT_QUOTE); |
| 1694 |
break; |
| 1695 |
|
| 1696 |
case SSH_SFTP_QUOTE_UNLINK: |
| 1697 |
rc = libssh2_sftp_unlink_ex(sshc->sftp_session, sshc->quote_path1, |
| 1698 |
curlx_uztoui(strlen(sshc->quote_path1))); |
| 1699 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1700 |
break; |
| 1701 |
} |
| 1702 |
if(rc != 0 && !sshc->acceptfail) { |
| 1703 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1704 |
Curl_safefree(sshc->quote_path1); |
| 1705 |
failf(data, "rm command failed: %s", sftp_libssh2_strerror(err)); |
| 1706 |
state(conn, SSH_SFTP_CLOSE); |
| 1707 |
sshc->nextstate = SSH_NO_STATE; |
| 1708 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1709 |
break; |
| 1710 |
} |
| 1711 |
state(conn, SSH_SFTP_NEXT_QUOTE); |
| 1712 |
break; |
| 1713 |
|
| 1714 |
#ifdef HAS_STATVFS_SUPPORT |
| 1715 |
case SSH_SFTP_QUOTE_STATVFS: |
| 1716 |
{ |
| 1717 |
LIBSSH2_SFTP_STATVFS statvfs; |
| 1718 |
rc = libssh2_sftp_statvfs(sshc->sftp_session, sshc->quote_path1, |
| 1719 |
curlx_uztoui(strlen(sshc->quote_path1)), |
| 1720 |
&statvfs); |
| 1721 |
|
| 1722 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1723 |
break; |
| 1724 |
} |
| 1725 |
if(rc != 0 && !sshc->acceptfail) { |
| 1726 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1727 |
Curl_safefree(sshc->quote_path1); |
| 1728 |
failf(data, "statvfs command failed: %s", sftp_libssh2_strerror(err)); |
| 1729 |
state(conn, SSH_SFTP_CLOSE); |
| 1730 |
sshc->nextstate = SSH_NO_STATE; |
| 1731 |
sshc->actualcode = CURLE_QUOTE_ERROR; |
| 1732 |
break; |
| 1733 |
} |
| 1734 |
else if(rc == 0) { |
| 1735 |
char *tmp = aprintf("statvfs:\n" |
| 1736 |
"f_bsize: %llu\n" "f_frsize: %llu\n" |
| 1737 |
"f_blocks: %llu\n" "f_bfree: %llu\n" |
| 1738 |
"f_bavail: %llu\n" "f_files: %llu\n" |
| 1739 |
"f_ffree: %llu\n" "f_favail: %llu\n" |
| 1740 |
"f_fsid: %llu\n" "f_flag: %llu\n" |
| 1741 |
"f_namemax: %llu\n", |
| 1742 |
statvfs.f_bsize, statvfs.f_frsize, |
| 1743 |
statvfs.f_blocks, statvfs.f_bfree, |
| 1744 |
statvfs.f_bavail, statvfs.f_files, |
| 1745 |
statvfs.f_ffree, statvfs.f_favail, |
| 1746 |
statvfs.f_fsid, statvfs.f_flag, |
| 1747 |
statvfs.f_namemax); |
| 1748 |
if(!tmp) { |
| 1749 |
result = CURLE_OUT_OF_MEMORY; |
| 1750 |
state(conn, SSH_SFTP_CLOSE); |
| 1751 |
sshc->nextstate = SSH_NO_STATE; |
| 1752 |
break; |
| 1753 |
} |
| 1754 |
|
| 1755 |
result = Curl_client_write(conn, CLIENTWRITE_HEADER, tmp, strlen(tmp)); |
| 1756 |
free(tmp); |
| 1757 |
if(result) { |
| 1758 |
state(conn, SSH_SFTP_CLOSE); |
| 1759 |
sshc->nextstate = SSH_NO_STATE; |
| 1760 |
sshc->actualcode = result; |
| 1761 |
} |
| 1762 |
} |
| 1763 |
state(conn, SSH_SFTP_NEXT_QUOTE); |
| 1764 |
break; |
| 1765 |
} |
| 1766 |
#endif |
| 1767 |
case SSH_SFTP_GETINFO: |
| 1768 |
{ |
| 1769 |
if(data->set.get_filetime) { |
| 1770 |
state(conn, SSH_SFTP_FILETIME); |
| 1771 |
} |
| 1772 |
else { |
| 1773 |
state(conn, SSH_SFTP_TRANS_INIT); |
| 1774 |
} |
| 1775 |
break; |
| 1776 |
} |
| 1777 |
|
| 1778 |
case SSH_SFTP_FILETIME: |
| 1779 |
{ |
| 1780 |
LIBSSH2_SFTP_ATTRIBUTES attrs; |
| 1781 |
|
| 1782 |
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path, |
| 1783 |
curlx_uztoui(strlen(sftp_scp->path)), |
| 1784 |
LIBSSH2_SFTP_STAT, &attrs); |
| 1785 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1786 |
break; |
| 1787 |
} |
| 1788 |
if(rc == 0) { |
| 1789 |
data->info.filetime = attrs.mtime; |
| 1790 |
} |
| 1791 |
|
| 1792 |
state(conn, SSH_SFTP_TRANS_INIT); |
| 1793 |
break; |
| 1794 |
} |
| 1795 |
|
| 1796 |
case SSH_SFTP_TRANS_INIT: |
| 1797 |
if(data->set.upload) |
| 1798 |
state(conn, SSH_SFTP_UPLOAD_INIT); |
| 1799 |
else { |
| 1800 |
if(sftp_scp->path[strlen(sftp_scp->path)-1] == '/') |
| 1801 |
state(conn, SSH_SFTP_READDIR_INIT); |
| 1802 |
else |
| 1803 |
state(conn, SSH_SFTP_DOWNLOAD_INIT); |
| 1804 |
} |
| 1805 |
break; |
| 1806 |
|
| 1807 |
case SSH_SFTP_UPLOAD_INIT: |
| 1808 |
{ |
| 1809 |
unsigned long flags; |
| 1810 |
/* |
| 1811 |
* NOTE!!! libssh2 requires that the destination path is a full path |
| 1812 |
* that includes the destination file and name OR ends in a "/" |
| 1813 |
* If this is not done the destination file will be named the |
| 1814 |
* same name as the last directory in the path. |
| 1815 |
*/ |
| 1816 |
|
| 1817 |
if(data->state.resume_from != 0) { |
| 1818 |
LIBSSH2_SFTP_ATTRIBUTES attrs; |
| 1819 |
if(data->state.resume_from < 0) { |
| 1820 |
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path, |
| 1821 |
curlx_uztoui(strlen(sftp_scp->path)), |
| 1822 |
LIBSSH2_SFTP_STAT, &attrs); |
| 1823 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 1824 |
break; |
| 1825 |
} |
| 1826 |
if(rc) { |
| 1827 |
data->state.resume_from = 0; |
| 1828 |
} |
| 1829 |
else { |
| 1830 |
curl_off_t size = attrs.filesize; |
| 1831 |
if(size < 0) { |
| 1832 |
failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size); |
| 1833 |
return CURLE_BAD_DOWNLOAD_RESUME; |
| 1834 |
} |
| 1835 |
data->state.resume_from = attrs.filesize; |
| 1836 |
} |
| 1837 |
} |
| 1838 |
} |
| 1839 |
|
| 1840 |
if(data->set.ftp_append) |
| 1841 |
/* Try to open for append, but create if nonexisting */ |
| 1842 |
flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_APPEND; |
| 1843 |
else if(data->state.resume_from > 0) |
| 1844 |
/* If we have restart position then open for append */ |
| 1845 |
flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_APPEND; |
| 1846 |
else |
| 1847 |
/* Clear file before writing (normal behaviour) */ |
| 1848 |
flags = LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC; |
| 1849 |
|
| 1850 |
sshc->sftp_handle = |
| 1851 |
libssh2_sftp_open_ex(sshc->sftp_session, sftp_scp->path, |
| 1852 |
curlx_uztoui(strlen(sftp_scp->path)), |
| 1853 |
flags, data->set.new_file_perms, |
| 1854 |
LIBSSH2_SFTP_OPENFILE); |
| 1855 |
|
| 1856 |
if(!sshc->sftp_handle) { |
| 1857 |
rc = libssh2_session_last_errno(sshc->ssh_session); |
| 1858 |
|
| 1859 |
if(LIBSSH2_ERROR_EAGAIN == rc) |
| 1860 |
break; |
| 1861 |
|
| 1862 |
if(LIBSSH2_ERROR_SFTP_PROTOCOL == rc) |
| 1863 |
/* only when there was an SFTP protocol error can we extract |
| 1864 |
the sftp error! */ |
| 1865 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 1866 |
else |
| 1867 |
err = -1; /* not an sftp error at all */ |
| 1868 |
|
| 1869 |
if(sshc->secondCreateDirs) { |
| 1870 |
state(conn, SSH_SFTP_CLOSE); |
| 1871 |
sshc->actualcode = err>= LIBSSH2_FX_OK? |
| 1872 |
sftp_libssh2_error_to_CURLE(err):CURLE_SSH; |
| 1873 |
failf(data, "Creating the dir/file failed: %s", |
| 1874 |
sftp_libssh2_strerror(err)); |
| 1875 |
break; |
| 1876 |
} |
| 1877 |
if(((err == LIBSSH2_FX_NO_SUCH_FILE) || |
| 1878 |
(err == LIBSSH2_FX_FAILURE) || |
| 1879 |
(err == LIBSSH2_FX_NO_SUCH_PATH)) && |
| 1880 |
(data->set.ftp_create_missing_dirs && |
| 1881 |
(strlen(sftp_scp->path) > 1))) { |
| 1882 |
/* try to create the path remotely */ |
| 1883 |
rc = 0; /* clear rc and continue */ |
| 1884 |
sshc->secondCreateDirs = 1; |
| 1885 |
state(conn, SSH_SFTP_CREATE_DIRS_INIT); |
| 1886 |
break; |
| 1887 |
} |
| 1888 |
state(conn, SSH_SFTP_CLOSE); |
| 1889 |
sshc->actualcode = err>= LIBSSH2_FX_OK? |
| 1890 |
sftp_libssh2_error_to_CURLE(err):CURLE_SSH; |
| 1891 |
if(!sshc->actualcode) { |
| 1892 |
/* Sometimes, for some reason libssh2_sftp_last_error() returns |
| 1893 |
zero even though libssh2_sftp_open() failed previously! We need |
| 1894 |
to work around that! */ |
| 1895 |
sshc->actualcode = CURLE_SSH; |
| 1896 |
err = -1; |
| 1897 |
} |
| 1898 |
failf(data, "Upload failed: %s (%d/%d)", |
| 1899 |
err>= LIBSSH2_FX_OK?sftp_libssh2_strerror(err):"ssh error", |
| 1900 |
err, rc); |
| 1901 |
break; |
| 1902 |
} |
| 1903 |
|
| 1904 |
/* If we have a restart point then we need to seek to the correct |
| 1905 |
position. */ |
| 1906 |
if(data->state.resume_from > 0) { |
| 1907 |
/* Let's read off the proper amount of bytes from the input. */ |
| 1908 |
if(conn->seek_func) { |
| 1909 |
Curl_set_in_callback(data, true); |
| 1910 |
seekerr = conn->seek_func(conn->seek_client, data->state.resume_from, |
| 1911 |
SEEK_SET); |
| 1912 |
Curl_set_in_callback(data, false); |
| 1913 |
} |
| 1914 |
|
| 1915 |
if(seekerr != CURL_SEEKFUNC_OK) { |
| 1916 |
curl_off_t passed = 0; |
| 1917 |
|
| 1918 |
if(seekerr != CURL_SEEKFUNC_CANTSEEK) { |
| 1919 |
failf(data, "Could not seek stream"); |
| 1920 |
return CURLE_FTP_COULDNT_USE_REST; |
| 1921 |
} |
| 1922 |
/* seekerr == CURL_SEEKFUNC_CANTSEEK (can't seek to offset) */ |
| 1923 |
do { |
| 1924 |
size_t readthisamountnow = |
| 1925 |
(data->state.resume_from - passed > data->set.buffer_size) ? |
| 1926 |
(size_t)data->set.buffer_size : |
| 1927 |
curlx_sotouz(data->state.resume_from - passed); |
| 1928 |
|
| 1929 |
size_t actuallyread; |
| 1930 |
Curl_set_in_callback(data, true); |
| 1931 |
actuallyread = data->state.fread_func(data->state.buffer, 1, |
| 1932 |
readthisamountnow, |
| 1933 |
data->state.in); |
| 1934 |
Curl_set_in_callback(data, false); |
| 1935 |
|
| 1936 |
passed += actuallyread; |
| 1937 |
if((actuallyread == 0) || (actuallyread > readthisamountnow)) { |
| 1938 |
/* this checks for greater-than only to make sure that the |
| 1939 |
CURL_READFUNC_ABORT return code still aborts */ |
| 1940 |
failf(data, "Failed to read data"); |
| 1941 |
return CURLE_FTP_COULDNT_USE_REST; |
| 1942 |
} |
| 1943 |
} while(passed < data->state.resume_from); |
| 1944 |
} |
| 1945 |
|
| 1946 |
/* now, decrease the size of the read */ |
| 1947 |
if(data->state.infilesize > 0) { |
| 1948 |
data->state.infilesize -= data->state.resume_from; |
| 1949 |
data->req.size = data->state.infilesize; |
| 1950 |
Curl_pgrsSetUploadSize(data, data->state.infilesize); |
| 1951 |
} |
| 1952 |
|
| 1953 |
SFTP_SEEK(sshc->sftp_handle, data->state.resume_from); |
| 1954 |
} |
| 1955 |
if(data->state.infilesize > 0) { |
| 1956 |
data->req.size = data->state.infilesize; |
| 1957 |
Curl_pgrsSetUploadSize(data, data->state.infilesize); |
| 1958 |
} |
| 1959 |
/* upload data */ |
| 1960 |
Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET); |
| 1961 |
|
| 1962 |
/* not set by Curl_setup_transfer to preserve keepon bits */ |
| 1963 |
conn->sockfd = conn->writesockfd; |
| 1964 |
|
| 1965 |
if(result) { |
| 1966 |
state(conn, SSH_SFTP_CLOSE); |
| 1967 |
sshc->actualcode = result; |
| 1968 |
} |
| 1969 |
else { |
| 1970 |
/* store this original bitmask setup to use later on if we can't |
| 1971 |
figure out a "real" bitmask */ |
| 1972 |
sshc->orig_waitfor = data->req.keepon; |
| 1973 |
|
| 1974 |
/* we want to use the _sending_ function even when the socket turns |
| 1975 |
out readable as the underlying libssh2 sftp send function will deal |
| 1976 |
with both accordingly */ |
| 1977 |
conn->cselect_bits = CURL_CSELECT_OUT; |
| 1978 |
|
| 1979 |
/* since we don't really wait for anything at this point, we want the |
| 1980 |
state machine to move on as soon as possible so we set a very short |
| 1981 |
timeout here */ |
| 1982 |
Curl_expire(data, 0, EXPIRE_RUN_NOW); |
| 1983 |
|
| 1984 |
state(conn, SSH_STOP); |
| 1985 |
} |
| 1986 |
break; |
| 1987 |
} |
| 1988 |
|
| 1989 |
case SSH_SFTP_CREATE_DIRS_INIT: |
| 1990 |
if(strlen(sftp_scp->path) > 1) { |
| 1991 |
sshc->slash_pos = sftp_scp->path + 1; /* ignore the leading '/' */ |
| 1992 |
state(conn, SSH_SFTP_CREATE_DIRS); |
| 1993 |
} |
| 1994 |
else { |
| 1995 |
state(conn, SSH_SFTP_UPLOAD_INIT); |
| 1996 |
} |
| 1997 |
break; |
| 1998 |
|
| 1999 |
case SSH_SFTP_CREATE_DIRS: |
| 2000 |
sshc->slash_pos = strchr(sshc->slash_pos, '/'); |
| 2001 |
if(sshc->slash_pos) { |
| 2002 |
*sshc->slash_pos = 0; |
| 2003 |
|
| 2004 |
infof(data, "Creating directory '%s'\n", sftp_scp->path); |
| 2005 |
state(conn, SSH_SFTP_CREATE_DIRS_MKDIR); |
| 2006 |
break; |
| 2007 |
} |
| 2008 |
state(conn, SSH_SFTP_UPLOAD_INIT); |
| 2009 |
break; |
| 2010 |
|
| 2011 |
case SSH_SFTP_CREATE_DIRS_MKDIR: |
| 2012 |
/* 'mode' - parameter is preliminary - default to 0644 */ |
| 2013 |
rc = libssh2_sftp_mkdir_ex(sshc->sftp_session, sftp_scp->path, |
| 2014 |
curlx_uztoui(strlen(sftp_scp->path)), |
| 2015 |
data->set.new_directory_perms); |
| 2016 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2017 |
break; |
| 2018 |
} |
| 2019 |
*sshc->slash_pos = '/'; |
| 2020 |
++sshc->slash_pos; |
| 2021 |
if(rc < 0) { |
| 2022 |
/* |
| 2023 |
* Abort if failure wasn't that the dir already exists or the |
| 2024 |
* permission was denied (creation might succeed further down the |
| 2025 |
* path) - retry on unspecific FAILURE also |
| 2026 |
*/ |
| 2027 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 2028 |
if((err != LIBSSH2_FX_FILE_ALREADY_EXISTS) && |
| 2029 |
(err != LIBSSH2_FX_FAILURE) && |
| 2030 |
(err != LIBSSH2_FX_PERMISSION_DENIED)) { |
| 2031 |
result = sftp_libssh2_error_to_CURLE(err); |
| 2032 |
state(conn, SSH_SFTP_CLOSE); |
| 2033 |
sshc->actualcode = result?result:CURLE_SSH; |
| 2034 |
break; |
| 2035 |
} |
| 2036 |
rc = 0; /* clear rc and continue */ |
| 2037 |
} |
| 2038 |
state(conn, SSH_SFTP_CREATE_DIRS); |
| 2039 |
break; |
| 2040 |
|
| 2041 |
case SSH_SFTP_READDIR_INIT: |
| 2042 |
Curl_pgrsSetDownloadSize(data, -1); |
| 2043 |
if(data->set.opt_no_body) { |
| 2044 |
state(conn, SSH_STOP); |
| 2045 |
break; |
| 2046 |
} |
| 2047 |
|
| 2048 |
/* |
| 2049 |
* This is a directory that we are trying to get, so produce a directory |
| 2050 |
* listing |
| 2051 |
*/ |
| 2052 |
sshc->sftp_handle = libssh2_sftp_open_ex(sshc->sftp_session, |
| 2053 |
sftp_scp->path, |
| 2054 |
curlx_uztoui( |
| 2055 |
strlen(sftp_scp->path)), |
| 2056 |
0, 0, LIBSSH2_SFTP_OPENDIR); |
| 2057 |
if(!sshc->sftp_handle) { |
| 2058 |
if(libssh2_session_last_errno(sshc->ssh_session) == |
| 2059 |
LIBSSH2_ERROR_EAGAIN) { |
| 2060 |
rc = LIBSSH2_ERROR_EAGAIN; |
| 2061 |
break; |
| 2062 |
} |
| 2063 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 2064 |
failf(data, "Could not open directory for reading: %s", |
| 2065 |
sftp_libssh2_strerror(err)); |
| 2066 |
state(conn, SSH_SFTP_CLOSE); |
| 2067 |
result = sftp_libssh2_error_to_CURLE(err); |
| 2068 |
sshc->actualcode = result?result:CURLE_SSH; |
| 2069 |
break; |
| 2070 |
} |
| 2071 |
sshc->readdir_filename = malloc(PATH_MAX + 1); |
| 2072 |
if(!sshc->readdir_filename) { |
| 2073 |
state(conn, SSH_SFTP_CLOSE); |
| 2074 |
sshc->actualcode = CURLE_OUT_OF_MEMORY; |
| 2075 |
break; |
| 2076 |
} |
| 2077 |
sshc->readdir_longentry = malloc(PATH_MAX + 1); |
| 2078 |
if(!sshc->readdir_longentry) { |
| 2079 |
Curl_safefree(sshc->readdir_filename); |
| 2080 |
state(conn, SSH_SFTP_CLOSE); |
| 2081 |
sshc->actualcode = CURLE_OUT_OF_MEMORY; |
| 2082 |
break; |
| 2083 |
} |
| 2084 |
state(conn, SSH_SFTP_READDIR); |
| 2085 |
break; |
| 2086 |
|
| 2087 |
case SSH_SFTP_READDIR: |
| 2088 |
rc = libssh2_sftp_readdir_ex(sshc->sftp_handle, |
| 2089 |
sshc->readdir_filename, |
| 2090 |
PATH_MAX, |
| 2091 |
sshc->readdir_longentry, |
| 2092 |
PATH_MAX, |
| 2093 |
&sshc->readdir_attrs); |
| 2094 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2095 |
break; |
| 2096 |
} |
| 2097 |
if(rc > 0) { |
| 2098 |
sshc->readdir_len = (size_t) rc; |
| 2099 |
sshc->readdir_filename[sshc->readdir_len] = '\0'; |
| 2100 |
|
| 2101 |
if(data->set.ftp_list_only) { |
| 2102 |
char *tmpLine; |
| 2103 |
|
| 2104 |
tmpLine = aprintf("%s\n", sshc->readdir_filename); |
| 2105 |
if(tmpLine == NULL) { |
| 2106 |
state(conn, SSH_SFTP_CLOSE); |
| 2107 |
sshc->actualcode = CURLE_OUT_OF_MEMORY; |
| 2108 |
break; |
| 2109 |
} |
| 2110 |
result = Curl_client_write(conn, CLIENTWRITE_BODY, |
| 2111 |
tmpLine, sshc->readdir_len + 1); |
| 2112 |
free(tmpLine); |
| 2113 |
|
| 2114 |
if(result) { |
| 2115 |
state(conn, SSH_STOP); |
| 2116 |
break; |
| 2117 |
} |
| 2118 |
/* since this counts what we send to the client, we include the |
| 2119 |
newline in this counter */ |
| 2120 |
data->req.bytecount += sshc->readdir_len + 1; |
| 2121 |
|
| 2122 |
/* output debug output if that is requested */ |
| 2123 |
if(data->set.verbose) { |
| 2124 |
Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_filename, |
| 2125 |
sshc->readdir_len); |
| 2126 |
} |
| 2127 |
} |
| 2128 |
else { |
| 2129 |
sshc->readdir_currLen = strlen(sshc->readdir_longentry); |
| 2130 |
sshc->readdir_totalLen = 80 + sshc->readdir_currLen; |
| 2131 |
sshc->readdir_line = calloc(sshc->readdir_totalLen, 1); |
| 2132 |
if(!sshc->readdir_line) { |
| 2133 |
Curl_safefree(sshc->readdir_filename); |
| 2134 |
Curl_safefree(sshc->readdir_longentry); |
| 2135 |
state(conn, SSH_SFTP_CLOSE); |
| 2136 |
sshc->actualcode = CURLE_OUT_OF_MEMORY; |
| 2137 |
break; |
| 2138 |
} |
| 2139 |
|
| 2140 |
memcpy(sshc->readdir_line, sshc->readdir_longentry, |
| 2141 |
sshc->readdir_currLen); |
| 2142 |
if((sshc->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) && |
| 2143 |
((sshc->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) == |
| 2144 |
LIBSSH2_SFTP_S_IFLNK)) { |
| 2145 |
sshc->readdir_linkPath = malloc(PATH_MAX + 1); |
| 2146 |
if(sshc->readdir_linkPath == NULL) { |
| 2147 |
Curl_safefree(sshc->readdir_filename); |
| 2148 |
Curl_safefree(sshc->readdir_longentry); |
| 2149 |
state(conn, SSH_SFTP_CLOSE); |
| 2150 |
sshc->actualcode = CURLE_OUT_OF_MEMORY; |
| 2151 |
break; |
| 2152 |
} |
| 2153 |
|
| 2154 |
msnprintf(sshc->readdir_linkPath, PATH_MAX, "%s%s", sftp_scp->path, |
| 2155 |
sshc->readdir_filename); |
| 2156 |
state(conn, SSH_SFTP_READDIR_LINK); |
| 2157 |
break; |
| 2158 |
} |
| 2159 |
state(conn, SSH_SFTP_READDIR_BOTTOM); |
| 2160 |
break; |
| 2161 |
} |
| 2162 |
} |
| 2163 |
else if(rc == 0) { |
| 2164 |
Curl_safefree(sshc->readdir_filename); |
| 2165 |
Curl_safefree(sshc->readdir_longentry); |
| 2166 |
state(conn, SSH_SFTP_READDIR_DONE); |
| 2167 |
break; |
| 2168 |
} |
| 2169 |
else if(rc < 0) { |
| 2170 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 2171 |
result = sftp_libssh2_error_to_CURLE(err); |
| 2172 |
sshc->actualcode = result?result:CURLE_SSH; |
| 2173 |
failf(data, "Could not open remote file for reading: %s :: %d", |
| 2174 |
sftp_libssh2_strerror(err), |
| 2175 |
libssh2_session_last_errno(sshc->ssh_session)); |
| 2176 |
Curl_safefree(sshc->readdir_filename); |
| 2177 |
Curl_safefree(sshc->readdir_longentry); |
| 2178 |
state(conn, SSH_SFTP_CLOSE); |
| 2179 |
break; |
| 2180 |
} |
| 2181 |
break; |
| 2182 |
|
| 2183 |
case SSH_SFTP_READDIR_LINK: |
| 2184 |
rc = |
| 2185 |
libssh2_sftp_symlink_ex(sshc->sftp_session, |
| 2186 |
sshc->readdir_linkPath, |
| 2187 |
curlx_uztoui(strlen(sshc->readdir_linkPath)), |
| 2188 |
sshc->readdir_filename, |
| 2189 |
PATH_MAX, LIBSSH2_SFTP_READLINK); |
| 2190 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2191 |
break; |
| 2192 |
} |
| 2193 |
sshc->readdir_len = (size_t) rc; |
| 2194 |
Curl_safefree(sshc->readdir_linkPath); |
| 2195 |
|
| 2196 |
/* get room for the filename and extra output */ |
| 2197 |
sshc->readdir_totalLen += 4 + sshc->readdir_len; |
| 2198 |
new_readdir_line = Curl_saferealloc(sshc->readdir_line, |
| 2199 |
sshc->readdir_totalLen); |
| 2200 |
if(!new_readdir_line) { |
| 2201 |
sshc->readdir_line = NULL; |
| 2202 |
Curl_safefree(sshc->readdir_filename); |
| 2203 |
Curl_safefree(sshc->readdir_longentry); |
| 2204 |
state(conn, SSH_SFTP_CLOSE); |
| 2205 |
sshc->actualcode = CURLE_OUT_OF_MEMORY; |
| 2206 |
break; |
| 2207 |
} |
| 2208 |
sshc->readdir_line = new_readdir_line; |
| 2209 |
|
| 2210 |
sshc->readdir_currLen += msnprintf(sshc->readdir_line + |
| 2211 |
sshc->readdir_currLen, |
| 2212 |
sshc->readdir_totalLen - |
| 2213 |
sshc->readdir_currLen, |
| 2214 |
" -> %s", |
| 2215 |
sshc->readdir_filename); |
| 2216 |
|
| 2217 |
state(conn, SSH_SFTP_READDIR_BOTTOM); |
| 2218 |
break; |
| 2219 |
|
| 2220 |
case SSH_SFTP_READDIR_BOTTOM: |
| 2221 |
sshc->readdir_currLen += msnprintf(sshc->readdir_line + |
| 2222 |
sshc->readdir_currLen, |
| 2223 |
sshc->readdir_totalLen - |
| 2224 |
sshc->readdir_currLen, "\n"); |
| 2225 |
result = Curl_client_write(conn, CLIENTWRITE_BODY, |
| 2226 |
sshc->readdir_line, |
| 2227 |
sshc->readdir_currLen); |
| 2228 |
|
| 2229 |
if(!result) { |
| 2230 |
|
| 2231 |
/* output debug output if that is requested */ |
| 2232 |
if(data->set.verbose) { |
| 2233 |
Curl_debug(data, CURLINFO_DATA_OUT, sshc->readdir_line, |
| 2234 |
sshc->readdir_currLen); |
| 2235 |
} |
| 2236 |
data->req.bytecount += sshc->readdir_currLen; |
| 2237 |
} |
| 2238 |
Curl_safefree(sshc->readdir_line); |
| 2239 |
if(result) { |
| 2240 |
state(conn, SSH_STOP); |
| 2241 |
} |
| 2242 |
else |
| 2243 |
state(conn, SSH_SFTP_READDIR); |
| 2244 |
break; |
| 2245 |
|
| 2246 |
case SSH_SFTP_READDIR_DONE: |
| 2247 |
if(libssh2_sftp_closedir(sshc->sftp_handle) == |
| 2248 |
LIBSSH2_ERROR_EAGAIN) { |
| 2249 |
rc = LIBSSH2_ERROR_EAGAIN; |
| 2250 |
break; |
| 2251 |
} |
| 2252 |
sshc->sftp_handle = NULL; |
| 2253 |
Curl_safefree(sshc->readdir_filename); |
| 2254 |
Curl_safefree(sshc->readdir_longentry); |
| 2255 |
|
| 2256 |
/* no data to transfer */ |
| 2257 |
Curl_setup_transfer(data, -1, -1, FALSE, -1); |
| 2258 |
state(conn, SSH_STOP); |
| 2259 |
break; |
| 2260 |
|
| 2261 |
case SSH_SFTP_DOWNLOAD_INIT: |
| 2262 |
/* |
| 2263 |
* Work on getting the specified file |
| 2264 |
*/ |
| 2265 |
sshc->sftp_handle = |
| 2266 |
libssh2_sftp_open_ex(sshc->sftp_session, sftp_scp->path, |
| 2267 |
curlx_uztoui(strlen(sftp_scp->path)), |
| 2268 |
LIBSSH2_FXF_READ, data->set.new_file_perms, |
| 2269 |
LIBSSH2_SFTP_OPENFILE); |
| 2270 |
if(!sshc->sftp_handle) { |
| 2271 |
if(libssh2_session_last_errno(sshc->ssh_session) == |
| 2272 |
LIBSSH2_ERROR_EAGAIN) { |
| 2273 |
rc = LIBSSH2_ERROR_EAGAIN; |
| 2274 |
break; |
| 2275 |
} |
| 2276 |
err = sftp_libssh2_last_error(sshc->sftp_session); |
| 2277 |
failf(data, "Could not open remote file for reading: %s", |
| 2278 |
sftp_libssh2_strerror(err)); |
| 2279 |
state(conn, SSH_SFTP_CLOSE); |
| 2280 |
result = sftp_libssh2_error_to_CURLE(err); |
| 2281 |
sshc->actualcode = result?result:CURLE_SSH; |
| 2282 |
break; |
| 2283 |
} |
| 2284 |
state(conn, SSH_SFTP_DOWNLOAD_STAT); |
| 2285 |
break; |
| 2286 |
|
| 2287 |
case SSH_SFTP_DOWNLOAD_STAT: |
| 2288 |
{ |
| 2289 |
LIBSSH2_SFTP_ATTRIBUTES attrs; |
| 2290 |
|
| 2291 |
rc = libssh2_sftp_stat_ex(sshc->sftp_session, sftp_scp->path, |
| 2292 |
curlx_uztoui(strlen(sftp_scp->path)), |
| 2293 |
LIBSSH2_SFTP_STAT, &attrs); |
| 2294 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2295 |
break; |
| 2296 |
} |
| 2297 |
if(rc || |
| 2298 |
!(attrs.flags & LIBSSH2_SFTP_ATTR_SIZE) || |
| 2299 |
(attrs.filesize == 0)) { |
| 2300 |
/* |
| 2301 |
* libssh2_sftp_open() didn't return an error, so maybe the server |
| 2302 |
* just doesn't support stat() |
| 2303 |
* OR the server doesn't return a file size with a stat() |
| 2304 |
* OR file size is 0 |
| 2305 |
*/ |
| 2306 |
data->req.size = -1; |
| 2307 |
data->req.maxdownload = -1; |
| 2308 |
Curl_pgrsSetDownloadSize(data, -1); |
| 2309 |
} |
| 2310 |
else { |
| 2311 |
curl_off_t size = attrs.filesize; |
| 2312 |
|
| 2313 |
if(size < 0) { |
| 2314 |
failf(data, "Bad file size (%" CURL_FORMAT_CURL_OFF_T ")", size); |
| 2315 |
return CURLE_BAD_DOWNLOAD_RESUME; |
| 2316 |
} |
| 2317 |
if(conn->data->state.use_range) { |
| 2318 |
curl_off_t from, to; |
| 2319 |
char *ptr; |
| 2320 |
char *ptr2; |
| 2321 |
CURLofft to_t; |
| 2322 |
CURLofft from_t; |
| 2323 |
|
| 2324 |
from_t = curlx_strtoofft(conn->data->state.range, &ptr, 0, &from); |
| 2325 |
if(from_t == CURL_OFFT_FLOW) |
| 2326 |
return CURLE_RANGE_ERROR; |
| 2327 |
while(*ptr && (ISSPACE(*ptr) || (*ptr == '-'))) |
| 2328 |
ptr++; |
| 2329 |
to_t = curlx_strtoofft(ptr, &ptr2, 0, &to); |
| 2330 |
if(to_t == CURL_OFFT_FLOW) |
| 2331 |
return CURLE_RANGE_ERROR; |
| 2332 |
if((to_t == CURL_OFFT_INVAL) /* no "to" value given */ |
| 2333 |
|| (to >= size)) { |
| 2334 |
to = size - 1; |
| 2335 |
} |
| 2336 |
if(from_t) { |
| 2337 |
/* from is relative to end of file */ |
| 2338 |
from = size - to; |
| 2339 |
to = size - 1; |
| 2340 |
} |
| 2341 |
if(from > size) { |
| 2342 |
failf(data, "Offset (%" |
| 2343 |
CURL_FORMAT_CURL_OFF_T ") was beyond file size (%" |
| 2344 |
CURL_FORMAT_CURL_OFF_T ")", from, attrs.filesize); |
| 2345 |
return CURLE_BAD_DOWNLOAD_RESUME; |
| 2346 |
} |
| 2347 |
if(from > to) { |
| 2348 |
from = to; |
| 2349 |
size = 0; |
| 2350 |
} |
| 2351 |
else { |
| 2352 |
size = to - from + 1; |
| 2353 |
} |
| 2354 |
|
| 2355 |
SFTP_SEEK(conn->proto.sshc.sftp_handle, from); |
| 2356 |
} |
| 2357 |
data->req.size = size; |
| 2358 |
data->req.maxdownload = size; |
| 2359 |
Curl_pgrsSetDownloadSize(data, size); |
| 2360 |
} |
| 2361 |
|
| 2362 |
/* We can resume if we can seek to the resume position */ |
| 2363 |
if(data->state.resume_from) { |
| 2364 |
if(data->state.resume_from < 0) { |
| 2365 |
/* We're supposed to download the last abs(from) bytes */ |
| 2366 |
if((curl_off_t)attrs.filesize < -data->state.resume_from) { |
| 2367 |
failf(data, "Offset (%" |
| 2368 |
CURL_FORMAT_CURL_OFF_T ") was beyond file size (%" |
| 2369 |
CURL_FORMAT_CURL_OFF_T ")", |
| 2370 |
data->state.resume_from, attrs.filesize); |
| 2371 |
return CURLE_BAD_DOWNLOAD_RESUME; |
| 2372 |
} |
| 2373 |
/* download from where? */ |
| 2374 |
data->state.resume_from += attrs.filesize; |
| 2375 |
} |
| 2376 |
else { |
| 2377 |
if((curl_off_t)attrs.filesize < data->state.resume_from) { |
| 2378 |
failf(data, "Offset (%" CURL_FORMAT_CURL_OFF_T |
| 2379 |
") was beyond file size (%" CURL_FORMAT_CURL_OFF_T ")", |
| 2380 |
data->state.resume_from, attrs.filesize); |
| 2381 |
return CURLE_BAD_DOWNLOAD_RESUME; |
| 2382 |
} |
| 2383 |
} |
| 2384 |
/* Now store the number of bytes we are expected to download */ |
| 2385 |
data->req.size = attrs.filesize - data->state.resume_from; |
| 2386 |
data->req.maxdownload = attrs.filesize - data->state.resume_from; |
| 2387 |
Curl_pgrsSetDownloadSize(data, |
| 2388 |
attrs.filesize - data->state.resume_from); |
| 2389 |
SFTP_SEEK(sshc->sftp_handle, data->state.resume_from); |
| 2390 |
} |
| 2391 |
} |
| 2392 |
|
| 2393 |
/* Setup the actual download */ |
| 2394 |
if(data->req.size == 0) { |
| 2395 |
/* no data to transfer */ |
| 2396 |
Curl_setup_transfer(data, -1, -1, FALSE, -1); |
| 2397 |
infof(data, "File already completely downloaded\n"); |
| 2398 |
state(conn, SSH_STOP); |
| 2399 |
break; |
| 2400 |
} |
| 2401 |
Curl_setup_transfer(data, FIRSTSOCKET, data->req.size, FALSE, -1); |
| 2402 |
|
| 2403 |
/* not set by Curl_setup_transfer to preserve keepon bits */ |
| 2404 |
conn->writesockfd = conn->sockfd; |
| 2405 |
|
| 2406 |
/* we want to use the _receiving_ function even when the socket turns |
| 2407 |
out writableable as the underlying libssh2 recv function will deal |
| 2408 |
with both accordingly */ |
| 2409 |
conn->cselect_bits = CURL_CSELECT_IN; |
| 2410 |
|
| 2411 |
if(result) { |
| 2412 |
/* this should never occur; the close state should be entered |
| 2413 |
at the time the error occurs */ |
| 2414 |
state(conn, SSH_SFTP_CLOSE); |
| 2415 |
sshc->actualcode = result; |
| 2416 |
} |
| 2417 |
else { |
| 2418 |
state(conn, SSH_STOP); |
| 2419 |
} |
| 2420 |
break; |
| 2421 |
|
| 2422 |
case SSH_SFTP_CLOSE: |
| 2423 |
if(sshc->sftp_handle) { |
| 2424 |
rc = libssh2_sftp_close(sshc->sftp_handle); |
| 2425 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2426 |
break; |
| 2427 |
} |
| 2428 |
if(rc < 0) { |
| 2429 |
char *err_msg = NULL; |
| 2430 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 2431 |
&err_msg, NULL, 0); |
| 2432 |
infof(data, "Failed to close libssh2 file: %d %s\n", rc, err_msg); |
| 2433 |
} |
| 2434 |
sshc->sftp_handle = NULL; |
| 2435 |
} |
| 2436 |
|
| 2437 |
Curl_safefree(sftp_scp->path); |
| 2438 |
|
| 2439 |
DEBUGF(infof(data, "SFTP DONE done\n")); |
| 2440 |
|
| 2441 |
/* Check if nextstate is set and move .nextstate could be POSTQUOTE_INIT |
| 2442 |
After nextstate is executed, the control should come back to |
| 2443 |
SSH_SFTP_CLOSE to pass the correct result back */ |
| 2444 |
if(sshc->nextstate != SSH_NO_STATE && |
| 2445 |
sshc->nextstate != SSH_SFTP_CLOSE) { |
| 2446 |
state(conn, sshc->nextstate); |
| 2447 |
sshc->nextstate = SSH_SFTP_CLOSE; |
| 2448 |
} |
| 2449 |
else { |
| 2450 |
state(conn, SSH_STOP); |
| 2451 |
result = sshc->actualcode; |
| 2452 |
} |
| 2453 |
break; |
| 2454 |
|
| 2455 |
case SSH_SFTP_SHUTDOWN: |
| 2456 |
/* during times we get here due to a broken transfer and then the |
| 2457 |
sftp_handle might not have been taken down so make sure that is done |
| 2458 |
before we proceed */ |
| 2459 |
|
| 2460 |
if(sshc->sftp_handle) { |
| 2461 |
rc = libssh2_sftp_close(sshc->sftp_handle); |
| 2462 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2463 |
break; |
| 2464 |
} |
| 2465 |
if(rc < 0) { |
| 2466 |
char *err_msg = NULL; |
| 2467 |
(void)libssh2_session_last_error(sshc->ssh_session, &err_msg, |
| 2468 |
NULL, 0); |
| 2469 |
infof(data, "Failed to close libssh2 file: %d %s\n", rc, err_msg); |
| 2470 |
} |
| 2471 |
sshc->sftp_handle = NULL; |
| 2472 |
} |
| 2473 |
if(sshc->sftp_session) { |
| 2474 |
rc = libssh2_sftp_shutdown(sshc->sftp_session); |
| 2475 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2476 |
break; |
| 2477 |
} |
| 2478 |
if(rc < 0) { |
| 2479 |
infof(data, "Failed to stop libssh2 sftp subsystem\n"); |
| 2480 |
} |
| 2481 |
sshc->sftp_session = NULL; |
| 2482 |
} |
| 2483 |
|
| 2484 |
Curl_safefree(sshc->homedir); |
| 2485 |
conn->data->state.most_recent_ftp_entrypath = NULL; |
| 2486 |
|
| 2487 |
state(conn, SSH_SESSION_DISCONNECT); |
| 2488 |
break; |
| 2489 |
|
| 2490 |
case SSH_SCP_TRANS_INIT: |
| 2491 |
result = Curl_getworkingpath(conn, sshc->homedir, &sftp_scp->path); |
| 2492 |
if(result) { |
| 2493 |
sshc->actualcode = result; |
| 2494 |
state(conn, SSH_STOP); |
| 2495 |
break; |
| 2496 |
} |
| 2497 |
|
| 2498 |
if(data->set.upload) { |
| 2499 |
if(data->state.infilesize < 0) { |
| 2500 |
failf(data, "SCP requires a known file size for upload"); |
| 2501 |
sshc->actualcode = CURLE_UPLOAD_FAILED; |
| 2502 |
state(conn, SSH_SCP_CHANNEL_FREE); |
| 2503 |
break; |
| 2504 |
} |
| 2505 |
state(conn, SSH_SCP_UPLOAD_INIT); |
| 2506 |
} |
| 2507 |
else { |
| 2508 |
state(conn, SSH_SCP_DOWNLOAD_INIT); |
| 2509 |
} |
| 2510 |
break; |
| 2511 |
|
| 2512 |
case SSH_SCP_UPLOAD_INIT: |
| 2513 |
/* |
| 2514 |
* libssh2 requires that the destination path is a full path that |
| 2515 |
* includes the destination file and name OR ends in a "/" . If this is |
| 2516 |
* not done the destination file will be named the same name as the last |
| 2517 |
* directory in the path. |
| 2518 |
*/ |
| 2519 |
sshc->ssh_channel = |
| 2520 |
SCP_SEND(sshc->ssh_session, sftp_scp->path, data->set.new_file_perms, |
| 2521 |
data->state.infilesize); |
| 2522 |
if(!sshc->ssh_channel) { |
| 2523 |
int ssh_err; |
| 2524 |
char *err_msg = NULL; |
| 2525 |
|
| 2526 |
if(libssh2_session_last_errno(sshc->ssh_session) == |
| 2527 |
LIBSSH2_ERROR_EAGAIN) { |
| 2528 |
rc = LIBSSH2_ERROR_EAGAIN; |
| 2529 |
break; |
| 2530 |
} |
| 2531 |
|
| 2532 |
ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session, |
| 2533 |
&err_msg, NULL, 0)); |
| 2534 |
failf(conn->data, "%s", err_msg); |
| 2535 |
state(conn, SSH_SCP_CHANNEL_FREE); |
| 2536 |
sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err); |
| 2537 |
/* Map generic errors to upload failed */ |
| 2538 |
if(sshc->actualcode == CURLE_SSH || |
| 2539 |
sshc->actualcode == CURLE_REMOTE_FILE_NOT_FOUND) |
| 2540 |
sshc->actualcode = CURLE_UPLOAD_FAILED; |
| 2541 |
break; |
| 2542 |
} |
| 2543 |
|
| 2544 |
/* upload data */ |
| 2545 |
Curl_setup_transfer(data, -1, data->req.size, FALSE, FIRSTSOCKET); |
| 2546 |
|
| 2547 |
/* not set by Curl_setup_transfer to preserve keepon bits */ |
| 2548 |
conn->sockfd = conn->writesockfd; |
| 2549 |
|
| 2550 |
if(result) { |
| 2551 |
state(conn, SSH_SCP_CHANNEL_FREE); |
| 2552 |
sshc->actualcode = result; |
| 2553 |
} |
| 2554 |
else { |
| 2555 |
/* store this original bitmask setup to use later on if we can't |
| 2556 |
figure out a "real" bitmask */ |
| 2557 |
sshc->orig_waitfor = data->req.keepon; |
| 2558 |
|
| 2559 |
/* we want to use the _sending_ function even when the socket turns |
| 2560 |
out readable as the underlying libssh2 scp send function will deal |
| 2561 |
with both accordingly */ |
| 2562 |
conn->cselect_bits = CURL_CSELECT_OUT; |
| 2563 |
|
| 2564 |
state(conn, SSH_STOP); |
| 2565 |
} |
| 2566 |
break; |
| 2567 |
|
| 2568 |
case SSH_SCP_DOWNLOAD_INIT: |
| 2569 |
{ |
| 2570 |
curl_off_t bytecount; |
| 2571 |
|
| 2572 |
/* |
| 2573 |
* We must check the remote file; if it is a directory no values will |
| 2574 |
* be set in sb |
| 2575 |
*/ |
| 2576 |
|
| 2577 |
/* |
| 2578 |
* If support for >2GB files exists, use it. |
| 2579 |
*/ |
| 2580 |
|
| 2581 |
/* get a fresh new channel from the ssh layer */ |
| 2582 |
#if LIBSSH2_VERSION_NUM < 0x010700 |
| 2583 |
struct stat sb; |
| 2584 |
memset(&sb, 0, sizeof(struct stat)); |
| 2585 |
sshc->ssh_channel = libssh2_scp_recv(sshc->ssh_session, |
| 2586 |
sftp_scp->path, &sb); |
| 2587 |
#else |
| 2588 |
libssh2_struct_stat sb; |
| 2589 |
memset(&sb, 0, sizeof(libssh2_struct_stat)); |
| 2590 |
sshc->ssh_channel = libssh2_scp_recv2(sshc->ssh_session, |
| 2591 |
sftp_scp->path, &sb); |
| 2592 |
#endif |
| 2593 |
|
| 2594 |
if(!sshc->ssh_channel) { |
| 2595 |
int ssh_err; |
| 2596 |
char *err_msg = NULL; |
| 2597 |
|
| 2598 |
if(libssh2_session_last_errno(sshc->ssh_session) == |
| 2599 |
LIBSSH2_ERROR_EAGAIN) { |
| 2600 |
rc = LIBSSH2_ERROR_EAGAIN; |
| 2601 |
break; |
| 2602 |
} |
| 2603 |
|
| 2604 |
|
| 2605 |
ssh_err = (int)(libssh2_session_last_error(sshc->ssh_session, |
| 2606 |
&err_msg, NULL, 0)); |
| 2607 |
failf(conn->data, "%s", err_msg); |
| 2608 |
state(conn, SSH_SCP_CHANNEL_FREE); |
| 2609 |
sshc->actualcode = libssh2_session_error_to_CURLE(ssh_err); |
| 2610 |
break; |
| 2611 |
} |
| 2612 |
|
| 2613 |
/* download data */ |
| 2614 |
bytecount = (curl_off_t)sb.st_size; |
| 2615 |
data->req.maxdownload = (curl_off_t)sb.st_size; |
| 2616 |
Curl_setup_transfer(data, FIRSTSOCKET, bytecount, FALSE, -1); |
| 2617 |
|
| 2618 |
/* not set by Curl_setup_transfer to preserve keepon bits */ |
| 2619 |
conn->writesockfd = conn->sockfd; |
| 2620 |
|
| 2621 |
/* we want to use the _receiving_ function even when the socket turns |
| 2622 |
out writableable as the underlying libssh2 recv function will deal |
| 2623 |
with both accordingly */ |
| 2624 |
conn->cselect_bits = CURL_CSELECT_IN; |
| 2625 |
|
| 2626 |
if(result) { |
| 2627 |
state(conn, SSH_SCP_CHANNEL_FREE); |
| 2628 |
sshc->actualcode = result; |
| 2629 |
} |
| 2630 |
else |
| 2631 |
state(conn, SSH_STOP); |
| 2632 |
} |
| 2633 |
break; |
| 2634 |
|
| 2635 |
case SSH_SCP_DONE: |
| 2636 |
if(data->set.upload) |
| 2637 |
state(conn, SSH_SCP_SEND_EOF); |
| 2638 |
else |
| 2639 |
state(conn, SSH_SCP_CHANNEL_FREE); |
| 2640 |
break; |
| 2641 |
|
| 2642 |
case SSH_SCP_SEND_EOF: |
| 2643 |
if(sshc->ssh_channel) { |
| 2644 |
rc = libssh2_channel_send_eof(sshc->ssh_channel); |
| 2645 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2646 |
break; |
| 2647 |
} |
| 2648 |
if(rc) { |
| 2649 |
char *err_msg = NULL; |
| 2650 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 2651 |
&err_msg, NULL, 0); |
| 2652 |
infof(data, "Failed to send libssh2 channel EOF: %d %s\n", |
| 2653 |
rc, err_msg); |
| 2654 |
} |
| 2655 |
} |
| 2656 |
state(conn, SSH_SCP_WAIT_EOF); |
| 2657 |
break; |
| 2658 |
|
| 2659 |
case SSH_SCP_WAIT_EOF: |
| 2660 |
if(sshc->ssh_channel) { |
| 2661 |
rc = libssh2_channel_wait_eof(sshc->ssh_channel); |
| 2662 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2663 |
break; |
| 2664 |
} |
| 2665 |
if(rc) { |
| 2666 |
char *err_msg = NULL; |
| 2667 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 2668 |
&err_msg, NULL, 0); |
| 2669 |
infof(data, "Failed to get channel EOF: %d %s\n", rc, err_msg); |
| 2670 |
} |
| 2671 |
} |
| 2672 |
state(conn, SSH_SCP_WAIT_CLOSE); |
| 2673 |
break; |
| 2674 |
|
| 2675 |
case SSH_SCP_WAIT_CLOSE: |
| 2676 |
if(sshc->ssh_channel) { |
| 2677 |
rc = libssh2_channel_wait_closed(sshc->ssh_channel); |
| 2678 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2679 |
break; |
| 2680 |
} |
| 2681 |
if(rc) { |
| 2682 |
char *err_msg = NULL; |
| 2683 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 2684 |
&err_msg, NULL, 0); |
| 2685 |
infof(data, "Channel failed to close: %d %s\n", rc, err_msg); |
| 2686 |
} |
| 2687 |
} |
| 2688 |
state(conn, SSH_SCP_CHANNEL_FREE); |
| 2689 |
break; |
| 2690 |
|
| 2691 |
case SSH_SCP_CHANNEL_FREE: |
| 2692 |
if(sshc->ssh_channel) { |
| 2693 |
rc = libssh2_channel_free(sshc->ssh_channel); |
| 2694 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2695 |
break; |
| 2696 |
} |
| 2697 |
if(rc < 0) { |
| 2698 |
char *err_msg = NULL; |
| 2699 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 2700 |
&err_msg, NULL, 0); |
| 2701 |
infof(data, "Failed to free libssh2 scp subsystem: %d %s\n", |
| 2702 |
rc, err_msg); |
| 2703 |
} |
| 2704 |
sshc->ssh_channel = NULL; |
| 2705 |
} |
| 2706 |
DEBUGF(infof(data, "SCP DONE phase complete\n")); |
| 2707 |
#if 0 /* PREV */ |
| 2708 |
state(conn, SSH_SESSION_DISCONNECT); |
| 2709 |
#endif |
| 2710 |
state(conn, SSH_STOP); |
| 2711 |
result = sshc->actualcode; |
| 2712 |
break; |
| 2713 |
|
| 2714 |
case SSH_SESSION_DISCONNECT: |
| 2715 |
/* during weird times when we've been prematurely aborted, the channel |
| 2716 |
is still alive when we reach this state and we MUST kill the channel |
| 2717 |
properly first */ |
| 2718 |
if(sshc->ssh_channel) { |
| 2719 |
rc = libssh2_channel_free(sshc->ssh_channel); |
| 2720 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2721 |
break; |
| 2722 |
} |
| 2723 |
if(rc < 0) { |
| 2724 |
char *err_msg = NULL; |
| 2725 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 2726 |
&err_msg, NULL, 0); |
| 2727 |
infof(data, "Failed to free libssh2 scp subsystem: %d %s\n", |
| 2728 |
rc, err_msg); |
| 2729 |
} |
| 2730 |
sshc->ssh_channel = NULL; |
| 2731 |
} |
| 2732 |
|
| 2733 |
if(sshc->ssh_session) { |
| 2734 |
rc = libssh2_session_disconnect(sshc->ssh_session, "Shutdown"); |
| 2735 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2736 |
break; |
| 2737 |
} |
| 2738 |
if(rc < 0) { |
| 2739 |
char *err_msg = NULL; |
| 2740 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 2741 |
&err_msg, NULL, 0); |
| 2742 |
infof(data, "Failed to disconnect libssh2 session: %d %s\n", |
| 2743 |
rc, err_msg); |
| 2744 |
} |
| 2745 |
} |
| 2746 |
|
| 2747 |
Curl_safefree(sshc->homedir); |
| 2748 |
conn->data->state.most_recent_ftp_entrypath = NULL; |
| 2749 |
|
| 2750 |
state(conn, SSH_SESSION_FREE); |
| 2751 |
break; |
| 2752 |
|
| 2753 |
case SSH_SESSION_FREE: |
| 2754 |
#ifdef HAVE_LIBSSH2_KNOWNHOST_API |
| 2755 |
if(sshc->kh) { |
| 2756 |
libssh2_knownhost_free(sshc->kh); |
| 2757 |
sshc->kh = NULL; |
| 2758 |
} |
| 2759 |
#endif |
| 2760 |
|
| 2761 |
#ifdef HAVE_LIBSSH2_AGENT_API |
| 2762 |
if(sshc->ssh_agent) { |
| 2763 |
rc = libssh2_agent_disconnect(sshc->ssh_agent); |
| 2764 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2765 |
break; |
| 2766 |
} |
| 2767 |
if(rc < 0) { |
| 2768 |
char *err_msg = NULL; |
| 2769 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 2770 |
&err_msg, NULL, 0); |
| 2771 |
infof(data, "Failed to disconnect from libssh2 agent: %d %s\n", |
| 2772 |
rc, err_msg); |
| 2773 |
} |
| 2774 |
libssh2_agent_free(sshc->ssh_agent); |
| 2775 |
sshc->ssh_agent = NULL; |
| 2776 |
|
| 2777 |
/* NB: there is no need to free identities, they are part of internal |
| 2778 |
agent stuff */ |
| 2779 |
sshc->sshagent_identity = NULL; |
| 2780 |
sshc->sshagent_prev_identity = NULL; |
| 2781 |
} |
| 2782 |
#endif |
| 2783 |
|
| 2784 |
if(sshc->ssh_session) { |
| 2785 |
rc = libssh2_session_free(sshc->ssh_session); |
| 2786 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2787 |
break; |
| 2788 |
} |
| 2789 |
if(rc < 0) { |
| 2790 |
char *err_msg = NULL; |
| 2791 |
(void)libssh2_session_last_error(sshc->ssh_session, |
| 2792 |
&err_msg, NULL, 0); |
| 2793 |
infof(data, "Failed to free libssh2 session: %d %s\n", rc, err_msg); |
| 2794 |
} |
| 2795 |
sshc->ssh_session = NULL; |
| 2796 |
} |
| 2797 |
|
| 2798 |
/* worst-case scenario cleanup */ |
| 2799 |
|
| 2800 |
DEBUGASSERT(sshc->ssh_session == NULL); |
| 2801 |
DEBUGASSERT(sshc->ssh_channel == NULL); |
| 2802 |
DEBUGASSERT(sshc->sftp_session == NULL); |
| 2803 |
DEBUGASSERT(sshc->sftp_handle == NULL); |
| 2804 |
#ifdef HAVE_LIBSSH2_KNOWNHOST_API |
| 2805 |
DEBUGASSERT(sshc->kh == NULL); |
| 2806 |
#endif |
| 2807 |
#ifdef HAVE_LIBSSH2_AGENT_API |
| 2808 |
DEBUGASSERT(sshc->ssh_agent == NULL); |
| 2809 |
#endif |
| 2810 |
|
| 2811 |
Curl_safefree(sshc->rsa_pub); |
| 2812 |
Curl_safefree(sshc->rsa); |
| 2813 |
|
| 2814 |
Curl_safefree(sshc->quote_path1); |
| 2815 |
Curl_safefree(sshc->quote_path2); |
| 2816 |
|
| 2817 |
Curl_safefree(sshc->homedir); |
| 2818 |
|
| 2819 |
Curl_safefree(sshc->readdir_filename); |
| 2820 |
Curl_safefree(sshc->readdir_longentry); |
| 2821 |
Curl_safefree(sshc->readdir_line); |
| 2822 |
Curl_safefree(sshc->readdir_linkPath); |
| 2823 |
|
| 2824 |
/* the code we are about to return */ |
| 2825 |
result = sshc->actualcode; |
| 2826 |
|
| 2827 |
memset(sshc, 0, sizeof(struct ssh_conn)); |
| 2828 |
|
| 2829 |
connclose(conn, "SSH session free"); |
| 2830 |
sshc->state = SSH_SESSION_FREE; /* current */ |
| 2831 |
sshc->nextstate = SSH_NO_STATE; |
| 2832 |
state(conn, SSH_STOP); |
| 2833 |
break; |
| 2834 |
|
| 2835 |
case SSH_QUIT: |
| 2836 |
/* fallthrough, just stop! */ |
| 2837 |
default: |
| 2838 |
/* internal error */ |
| 2839 |
sshc->nextstate = SSH_NO_STATE; |
| 2840 |
state(conn, SSH_STOP); |
| 2841 |
break; |
| 2842 |
} |
| 2843 |
|
| 2844 |
} while(!rc && (sshc->state != SSH_STOP)); |
| 2845 |
|
| 2846 |
if(rc == LIBSSH2_ERROR_EAGAIN) { |
| 2847 |
/* we would block, we need to wait for the socket to be ready (in the |
| 2848 |
right direction too)! */ |
| 2849 |
*block = TRUE; |
| 2850 |
} |
| 2851 |
|
| 2852 |
return result; |
| 2853 |
} |
| 2854 |
|
| 2855 |
/* called by the multi interface to figure out what socket(s) to wait for and |
| 2856 |
for what actions in the DO_DONE, PERFORM and WAITPERFORM states */ |
| 2857 |
static int ssh_perform_getsock(const struct connectdata *conn, |
| 2858 |
curl_socket_t *sock) |
| 2859 |
{ |
| 2860 |
#ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION |
| 2861 |
int bitmap = GETSOCK_BLANK; |
| 2862 |
|
| 2863 |
sock[0] = conn->sock[FIRSTSOCKET]; |
| 2864 |
|
| 2865 |
if(conn->waitfor & KEEP_RECV) |
| 2866 |
bitmap |= GETSOCK_READSOCK(FIRSTSOCKET); |
| 2867 |
|
| 2868 |
if(conn->waitfor & KEEP_SEND) |
| 2869 |
bitmap |= GETSOCK_WRITESOCK(FIRSTSOCKET); |
| 2870 |
|
| 2871 |
return bitmap; |
| 2872 |
#else |
| 2873 |
/* if we don't know the direction we can use the generic *_getsock() |
| 2874 |
function even for the protocol_connect and doing states */ |
| 2875 |
return Curl_single_getsock(conn, sock); |
| 2876 |
#endif |
| 2877 |
} |
| 2878 |
|
| 2879 |
/* Generic function called by the multi interface to figure out what socket(s) |
| 2880 |
to wait for and for what actions during the DOING and PROTOCONNECT states*/ |
| 2881 |
static int ssh_getsock(struct connectdata *conn, |
| 2882 |
curl_socket_t *sock) |
| 2883 |
{ |
| 2884 |
#ifndef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION |
| 2885 |
(void)conn; |
| 2886 |
(void)sock; |
| 2887 |
/* if we don't know any direction we can just play along as we used to and |
| 2888 |
not provide any sensible info */ |
| 2889 |
return GETSOCK_BLANK; |
| 2890 |
#else |
| 2891 |
/* if we know the direction we can use the generic *_getsock() function even |
| 2892 |
for the protocol_connect and doing states */ |
| 2893 |
return ssh_perform_getsock(conn, sock); |
| 2894 |
#endif |
| 2895 |
} |
| 2896 |
|
| 2897 |
#ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION |
| 2898 |
/* |
| 2899 |
* When one of the libssh2 functions has returned LIBSSH2_ERROR_EAGAIN this |
| 2900 |
* function is used to figure out in what direction and stores this info so |
| 2901 |
* that the multi interface can take advantage of it. Make sure to call this |
| 2902 |
* function in all cases so that when it _doesn't_ return EAGAIN we can |
| 2903 |
* restore the default wait bits. |
| 2904 |
*/ |
| 2905 |
static void ssh_block2waitfor(struct connectdata *conn, bool block) |
| 2906 |
{ |
| 2907 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 2908 |
int dir = 0; |
| 2909 |
if(block) { |
| 2910 |
dir = libssh2_session_block_directions(sshc->ssh_session); |
| 2911 |
if(dir) { |
| 2912 |
/* translate the libssh2 define bits into our own bit defines */ |
| 2913 |
conn->waitfor = ((dir&LIBSSH2_SESSION_BLOCK_INBOUND)?KEEP_RECV:0) | |
| 2914 |
((dir&LIBSSH2_SESSION_BLOCK_OUTBOUND)?KEEP_SEND:0); |
| 2915 |
} |
| 2916 |
} |
| 2917 |
if(!dir) |
| 2918 |
/* It didn't block or libssh2 didn't reveal in which direction, put back |
| 2919 |
the original set */ |
| 2920 |
conn->waitfor = sshc->orig_waitfor; |
| 2921 |
} |
| 2922 |
#else |
| 2923 |
/* no libssh2 directional support so we simply don't know */ |
| 2924 |
#define ssh_block2waitfor(x,y) Curl_nop_stmt |
| 2925 |
#endif |
| 2926 |
|
| 2927 |
/* called repeatedly until done from multi.c */ |
| 2928 |
static CURLcode ssh_multi_statemach(struct connectdata *conn, bool *done) |
| 2929 |
{ |
| 2930 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 2931 |
CURLcode result = CURLE_OK; |
| 2932 |
bool block; /* we store the status and use that to provide a ssh_getsock() |
| 2933 |
implementation */ |
| 2934 |
do { |
| 2935 |
result = ssh_statemach_act(conn, &block); |
| 2936 |
*done = (sshc->state == SSH_STOP) ? TRUE : FALSE; |
| 2937 |
/* if there's no error, it isn't done and it didn't EWOULDBLOCK, then |
| 2938 |
try again */ |
| 2939 |
} while(!result && !*done && !block); |
| 2940 |
ssh_block2waitfor(conn, block); |
| 2941 |
|
| 2942 |
return result; |
| 2943 |
} |
| 2944 |
|
| 2945 |
static CURLcode ssh_block_statemach(struct connectdata *conn, |
| 2946 |
bool disconnect) |
| 2947 |
{ |
| 2948 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 2949 |
CURLcode result = CURLE_OK; |
| 2950 |
struct Curl_easy *data = conn->data; |
| 2951 |
|
| 2952 |
while((sshc->state != SSH_STOP) && !result) { |
| 2953 |
bool block; |
| 2954 |
timediff_t left = 1000; |
| 2955 |
struct curltime now = Curl_now(); |
| 2956 |
|
| 2957 |
result = ssh_statemach_act(conn, &block); |
| 2958 |
if(result) |
| 2959 |
break; |
| 2960 |
|
| 2961 |
if(!disconnect) { |
| 2962 |
if(Curl_pgrsUpdate(conn)) |
| 2963 |
return CURLE_ABORTED_BY_CALLBACK; |
| 2964 |
|
| 2965 |
result = Curl_speedcheck(data, now); |
| 2966 |
if(result) |
| 2967 |
break; |
| 2968 |
|
| 2969 |
left = Curl_timeleft(data, NULL, FALSE); |
| 2970 |
if(left < 0) { |
| 2971 |
failf(data, "Operation timed out"); |
| 2972 |
return CURLE_OPERATION_TIMEDOUT; |
| 2973 |
} |
| 2974 |
} |
| 2975 |
|
| 2976 |
#ifdef HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION |
| 2977 |
if(block) { |
| 2978 |
int dir = libssh2_session_block_directions(sshc->ssh_session); |
| 2979 |
curl_socket_t sock = conn->sock[FIRSTSOCKET]; |
| 2980 |
curl_socket_t fd_read = CURL_SOCKET_BAD; |
| 2981 |
curl_socket_t fd_write = CURL_SOCKET_BAD; |
| 2982 |
if(LIBSSH2_SESSION_BLOCK_INBOUND & dir) |
| 2983 |
fd_read = sock; |
| 2984 |
if(LIBSSH2_SESSION_BLOCK_OUTBOUND & dir) |
| 2985 |
fd_write = sock; |
| 2986 |
/* wait for the socket to become ready */ |
| 2987 |
(void)Curl_socket_check(fd_read, CURL_SOCKET_BAD, fd_write, |
| 2988 |
left>1000?1000:(time_t)left); |
| 2989 |
} |
| 2990 |
#endif |
| 2991 |
|
| 2992 |
} |
| 2993 |
|
| 2994 |
return result; |
| 2995 |
} |
| 2996 |
|
| 2997 |
/* |
| 2998 |
* SSH setup and connection |
| 2999 |
*/ |
| 3000 |
static CURLcode ssh_setup_connection(struct connectdata *conn) |
| 3001 |
{ |
| 3002 |
struct SSHPROTO *ssh; |
| 3003 |
|
| 3004 |
conn->data->req.protop = ssh = calloc(1, sizeof(struct SSHPROTO)); |
| 3005 |
if(!ssh) |
| 3006 |
return CURLE_OUT_OF_MEMORY; |
| 3007 |
|
| 3008 |
return CURLE_OK; |
| 3009 |
} |
| 3010 |
|
| 3011 |
static Curl_recv scp_recv, sftp_recv; |
| 3012 |
static Curl_send scp_send, sftp_send; |
| 3013 |
|
| 3014 |
/* |
| 3015 |
* Curl_ssh_connect() gets called from Curl_protocol_connect() to allow us to |
| 3016 |
* do protocol-specific actions at connect-time. |
| 3017 |
*/ |
| 3018 |
static CURLcode ssh_connect(struct connectdata *conn, bool *done) |
| 3019 |
{ |
| 3020 |
#ifdef CURL_LIBSSH2_DEBUG |
| 3021 |
curl_socket_t sock; |
| 3022 |
#endif |
| 3023 |
struct ssh_conn *ssh; |
| 3024 |
CURLcode result; |
| 3025 |
struct Curl_easy *data = conn->data; |
| 3026 |
|
| 3027 |
/* initialize per-handle data if not already */ |
| 3028 |
if(!data->req.protop) |
| 3029 |
ssh_setup_connection(conn); |
| 3030 |
|
| 3031 |
/* We default to persistent connections. We set this already in this connect |
| 3032 |
function to make the re-use checks properly be able to check this bit. */ |
| 3033 |
connkeep(conn, "SSH default"); |
| 3034 |
|
| 3035 |
if(conn->handler->protocol & CURLPROTO_SCP) { |
| 3036 |
conn->recv[FIRSTSOCKET] = scp_recv; |
| 3037 |
conn->send[FIRSTSOCKET] = scp_send; |
| 3038 |
} |
| 3039 |
else { |
| 3040 |
conn->recv[FIRSTSOCKET] = sftp_recv; |
| 3041 |
conn->send[FIRSTSOCKET] = sftp_send; |
| 3042 |
} |
| 3043 |
ssh = &conn->proto.sshc; |
| 3044 |
|
| 3045 |
#ifdef CURL_LIBSSH2_DEBUG |
| 3046 |
if(conn->user) { |
| 3047 |
infof(data, "User: %s\n", conn->user); |
| 3048 |
} |
| 3049 |
if(conn->passwd) { |
| 3050 |
infof(data, "Password: %s\n", conn->passwd); |
| 3051 |
} |
| 3052 |
sock = conn->sock[FIRSTSOCKET]; |
| 3053 |
#endif /* CURL_LIBSSH2_DEBUG */ |
| 3054 |
|
| 3055 |
ssh->ssh_session = libssh2_session_init_ex(my_libssh2_malloc, |
| 3056 |
my_libssh2_free, |
| 3057 |
my_libssh2_realloc, conn); |
| 3058 |
if(ssh->ssh_session == NULL) { |
| 3059 |
failf(data, "Failure initialising ssh session"); |
| 3060 |
return CURLE_FAILED_INIT; |
| 3061 |
} |
| 3062 |
|
| 3063 |
if(data->set.ssh_compression) { |
| 3064 |
#if LIBSSH2_VERSION_NUM >= 0x010208 |
| 3065 |
if(libssh2_session_flag(ssh->ssh_session, LIBSSH2_FLAG_COMPRESS, 1) < 0) |
| 3066 |
#endif |
| 3067 |
infof(data, "Failed to enable compression for ssh session\n"); |
| 3068 |
} |
| 3069 |
|
| 3070 |
#ifdef HAVE_LIBSSH2_KNOWNHOST_API |
| 3071 |
if(data->set.str[STRING_SSH_KNOWNHOSTS]) { |
| 3072 |
int rc; |
| 3073 |
ssh->kh = libssh2_knownhost_init(ssh->ssh_session); |
| 3074 |
if(!ssh->kh) { |
| 3075 |
libssh2_session_free(ssh->ssh_session); |
| 3076 |
return CURLE_FAILED_INIT; |
| 3077 |
} |
| 3078 |
|
| 3079 |
/* read all known hosts from there */ |
| 3080 |
rc = libssh2_knownhost_readfile(ssh->kh, |
| 3081 |
data->set.str[STRING_SSH_KNOWNHOSTS], |
| 3082 |
LIBSSH2_KNOWNHOST_FILE_OPENSSH); |
| 3083 |
if(rc < 0) |
| 3084 |
infof(data, "Failed to read known hosts from %s\n", |
| 3085 |
data->set.str[STRING_SSH_KNOWNHOSTS]); |
| 3086 |
} |
| 3087 |
#endif /* HAVE_LIBSSH2_KNOWNHOST_API */ |
| 3088 |
|
| 3089 |
#ifdef CURL_LIBSSH2_DEBUG |
| 3090 |
libssh2_trace(ssh->ssh_session, ~0); |
| 3091 |
infof(data, "SSH socket: %d\n", (int)sock); |
| 3092 |
#endif /* CURL_LIBSSH2_DEBUG */ |
| 3093 |
|
| 3094 |
state(conn, SSH_INIT); |
| 3095 |
|
| 3096 |
result = ssh_multi_statemach(conn, done); |
| 3097 |
|
| 3098 |
return result; |
| 3099 |
} |
| 3100 |
|
| 3101 |
/* |
| 3102 |
*********************************************************************** |
| 3103 |
* |
| 3104 |
* scp_perform() |
| 3105 |
* |
| 3106 |
* This is the actual DO function for SCP. Get a file according to |
| 3107 |
* the options previously setup. |
| 3108 |
*/ |
| 3109 |
|
| 3110 |
static |
| 3111 |
CURLcode scp_perform(struct connectdata *conn, |
| 3112 |
bool *connected, |
| 3113 |
bool *dophase_done) |
| 3114 |
{ |
| 3115 |
CURLcode result = CURLE_OK; |
| 3116 |
|
| 3117 |
DEBUGF(infof(conn->data, "DO phase starts\n")); |
| 3118 |
|
| 3119 |
*dophase_done = FALSE; /* not done yet */ |
| 3120 |
|
| 3121 |
/* start the first command in the DO phase */ |
| 3122 |
state(conn, SSH_SCP_TRANS_INIT); |
| 3123 |
|
| 3124 |
/* run the state-machine */ |
| 3125 |
result = ssh_multi_statemach(conn, dophase_done); |
| 3126 |
|
| 3127 |
*connected = conn->bits.tcpconnect[FIRSTSOCKET]; |
| 3128 |
|
| 3129 |
if(*dophase_done) { |
| 3130 |
DEBUGF(infof(conn->data, "DO phase is complete\n")); |
| 3131 |
} |
| 3132 |
|
| 3133 |
return result; |
| 3134 |
} |
| 3135 |
|
| 3136 |
/* called from multi.c while DOing */ |
| 3137 |
static CURLcode scp_doing(struct connectdata *conn, |
| 3138 |
bool *dophase_done) |
| 3139 |
{ |
| 3140 |
CURLcode result; |
| 3141 |
result = ssh_multi_statemach(conn, dophase_done); |
| 3142 |
|
| 3143 |
if(*dophase_done) { |
| 3144 |
DEBUGF(infof(conn->data, "DO phase is complete\n")); |
| 3145 |
} |
| 3146 |
return result; |
| 3147 |
} |
| 3148 |
|
| 3149 |
/* |
| 3150 |
* The DO function is generic for both protocols. There was previously two |
| 3151 |
* separate ones but this way means less duplicated code. |
| 3152 |
*/ |
| 3153 |
|
| 3154 |
static CURLcode ssh_do(struct connectdata *conn, bool *done) |
| 3155 |
{ |
| 3156 |
CURLcode result; |
| 3157 |
bool connected = 0; |
| 3158 |
struct Curl_easy *data = conn->data; |
| 3159 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 3160 |
|
| 3161 |
*done = FALSE; /* default to false */ |
| 3162 |
|
| 3163 |
data->req.size = -1; /* make sure this is unknown at this point */ |
| 3164 |
|
| 3165 |
sshc->actualcode = CURLE_OK; /* reset error code */ |
| 3166 |
sshc->secondCreateDirs = 0; /* reset the create dir attempt state |
| 3167 |
variable */ |
| 3168 |
|
| 3169 |
Curl_pgrsSetUploadCounter(data, 0); |
| 3170 |
Curl_pgrsSetDownloadCounter(data, 0); |
| 3171 |
Curl_pgrsSetUploadSize(data, -1); |
| 3172 |
Curl_pgrsSetDownloadSize(data, -1); |
| 3173 |
|
| 3174 |
if(conn->handler->protocol & CURLPROTO_SCP) |
| 3175 |
result = scp_perform(conn, &connected, done); |
| 3176 |
else |
| 3177 |
result = sftp_perform(conn, &connected, done); |
| 3178 |
|
| 3179 |
return result; |
| 3180 |
} |
| 3181 |
|
| 3182 |
/* BLOCKING, but the function is using the state machine so the only reason |
| 3183 |
this is still blocking is that the multi interface code has no support for |
| 3184 |
disconnecting operations that takes a while */ |
| 3185 |
static CURLcode scp_disconnect(struct connectdata *conn, bool dead_connection) |
| 3186 |
{ |
| 3187 |
CURLcode result = CURLE_OK; |
| 3188 |
struct ssh_conn *ssh = &conn->proto.sshc; |
| 3189 |
(void) dead_connection; |
| 3190 |
|
| 3191 |
if(ssh->ssh_session) { |
| 3192 |
/* only if there's a session still around to use! */ |
| 3193 |
|
| 3194 |
state(conn, SSH_SESSION_DISCONNECT); |
| 3195 |
|
| 3196 |
result = ssh_block_statemach(conn, TRUE); |
| 3197 |
} |
| 3198 |
|
| 3199 |
return result; |
| 3200 |
} |
| 3201 |
|
| 3202 |
/* generic done function for both SCP and SFTP called from their specific |
| 3203 |
done functions */ |
| 3204 |
static CURLcode ssh_done(struct connectdata *conn, CURLcode status) |
| 3205 |
{ |
| 3206 |
CURLcode result = CURLE_OK; |
| 3207 |
struct SSHPROTO *sftp_scp = conn->data->req.protop; |
| 3208 |
|
| 3209 |
if(!status) { |
| 3210 |
/* run the state-machine */ |
| 3211 |
result = ssh_block_statemach(conn, FALSE); |
| 3212 |
} |
| 3213 |
else |
| 3214 |
result = status; |
| 3215 |
|
| 3216 |
if(sftp_scp) |
| 3217 |
Curl_safefree(sftp_scp->path); |
| 3218 |
if(Curl_pgrsDone(conn)) |
| 3219 |
return CURLE_ABORTED_BY_CALLBACK; |
| 3220 |
|
| 3221 |
conn->data->req.keepon = 0; /* clear all bits */ |
| 3222 |
return result; |
| 3223 |
} |
| 3224 |
|
| 3225 |
|
| 3226 |
static CURLcode scp_done(struct connectdata *conn, CURLcode status, |
| 3227 |
bool premature) |
| 3228 |
{ |
| 3229 |
(void)premature; /* not used */ |
| 3230 |
|
| 3231 |
if(!status) |
| 3232 |
state(conn, SSH_SCP_DONE); |
| 3233 |
|
| 3234 |
return ssh_done(conn, status); |
| 3235 |
|
| 3236 |
} |
| 3237 |
|
| 3238 |
static ssize_t scp_send(struct connectdata *conn, int sockindex, |
| 3239 |
const void *mem, size_t len, CURLcode *err) |
| 3240 |
{ |
| 3241 |
ssize_t nwrite; |
| 3242 |
(void)sockindex; /* we only support SCP on the fixed known primary socket */ |
| 3243 |
|
| 3244 |
/* libssh2_channel_write() returns int! */ |
| 3245 |
nwrite = (ssize_t) |
| 3246 |
libssh2_channel_write(conn->proto.sshc.ssh_channel, mem, len); |
| 3247 |
|
| 3248 |
ssh_block2waitfor(conn, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE); |
| 3249 |
|
| 3250 |
if(nwrite == LIBSSH2_ERROR_EAGAIN) { |
| 3251 |
*err = CURLE_AGAIN; |
| 3252 |
nwrite = 0; |
| 3253 |
} |
| 3254 |
else if(nwrite < LIBSSH2_ERROR_NONE) { |
| 3255 |
*err = libssh2_session_error_to_CURLE((int)nwrite); |
| 3256 |
nwrite = -1; |
| 3257 |
} |
| 3258 |
|
| 3259 |
return nwrite; |
| 3260 |
} |
| 3261 |
|
| 3262 |
static ssize_t scp_recv(struct connectdata *conn, int sockindex, |
| 3263 |
char *mem, size_t len, CURLcode *err) |
| 3264 |
{ |
| 3265 |
ssize_t nread; |
| 3266 |
(void)sockindex; /* we only support SCP on the fixed known primary socket */ |
| 3267 |
|
| 3268 |
/* libssh2_channel_read() returns int */ |
| 3269 |
nread = (ssize_t) |
| 3270 |
libssh2_channel_read(conn->proto.sshc.ssh_channel, mem, len); |
| 3271 |
|
| 3272 |
ssh_block2waitfor(conn, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE); |
| 3273 |
if(nread == LIBSSH2_ERROR_EAGAIN) { |
| 3274 |
*err = CURLE_AGAIN; |
| 3275 |
nread = -1; |
| 3276 |
} |
| 3277 |
|
| 3278 |
return nread; |
| 3279 |
} |
| 3280 |
|
| 3281 |
/* |
| 3282 |
* =============== SFTP =============== |
| 3283 |
*/ |
| 3284 |
|
| 3285 |
/* |
| 3286 |
*********************************************************************** |
| 3287 |
* |
| 3288 |
* sftp_perform() |
| 3289 |
* |
| 3290 |
* This is the actual DO function for SFTP. Get a file/directory according to |
| 3291 |
* the options previously setup. |
| 3292 |
*/ |
| 3293 |
|
| 3294 |
static |
| 3295 |
CURLcode sftp_perform(struct connectdata *conn, |
| 3296 |
bool *connected, |
| 3297 |
bool *dophase_done) |
| 3298 |
{ |
| 3299 |
CURLcode result = CURLE_OK; |
| 3300 |
|
| 3301 |
DEBUGF(infof(conn->data, "DO phase starts\n")); |
| 3302 |
|
| 3303 |
*dophase_done = FALSE; /* not done yet */ |
| 3304 |
|
| 3305 |
/* start the first command in the DO phase */ |
| 3306 |
state(conn, SSH_SFTP_QUOTE_INIT); |
| 3307 |
|
| 3308 |
/* run the state-machine */ |
| 3309 |
result = ssh_multi_statemach(conn, dophase_done); |
| 3310 |
|
| 3311 |
*connected = conn->bits.tcpconnect[FIRSTSOCKET]; |
| 3312 |
|
| 3313 |
if(*dophase_done) { |
| 3314 |
DEBUGF(infof(conn->data, "DO phase is complete\n")); |
| 3315 |
} |
| 3316 |
|
| 3317 |
return result; |
| 3318 |
} |
| 3319 |
|
| 3320 |
/* called from multi.c while DOing */ |
| 3321 |
static CURLcode sftp_doing(struct connectdata *conn, |
| 3322 |
bool *dophase_done) |
| 3323 |
{ |
| 3324 |
CURLcode result = ssh_multi_statemach(conn, dophase_done); |
| 3325 |
|
| 3326 |
if(*dophase_done) { |
| 3327 |
DEBUGF(infof(conn->data, "DO phase is complete\n")); |
| 3328 |
} |
| 3329 |
return result; |
| 3330 |
} |
| 3331 |
|
| 3332 |
/* BLOCKING, but the function is using the state machine so the only reason |
| 3333 |
this is still blocking is that the multi interface code has no support for |
| 3334 |
disconnecting operations that takes a while */ |
| 3335 |
static CURLcode sftp_disconnect(struct connectdata *conn, bool dead_connection) |
| 3336 |
{ |
| 3337 |
CURLcode result = CURLE_OK; |
| 3338 |
(void) dead_connection; |
| 3339 |
|
| 3340 |
DEBUGF(infof(conn->data, "SSH DISCONNECT starts now\n")); |
| 3341 |
|
| 3342 |
if(conn->proto.sshc.ssh_session) { |
| 3343 |
/* only if there's a session still around to use! */ |
| 3344 |
state(conn, SSH_SFTP_SHUTDOWN); |
| 3345 |
result = ssh_block_statemach(conn, TRUE); |
| 3346 |
} |
| 3347 |
|
| 3348 |
DEBUGF(infof(conn->data, "SSH DISCONNECT is done\n")); |
| 3349 |
|
| 3350 |
return result; |
| 3351 |
|
| 3352 |
} |
| 3353 |
|
| 3354 |
static CURLcode sftp_done(struct connectdata *conn, CURLcode status, |
| 3355 |
bool premature) |
| 3356 |
{ |
| 3357 |
struct ssh_conn *sshc = &conn->proto.sshc; |
| 3358 |
|
| 3359 |
if(!status) { |
| 3360 |
/* Post quote commands are executed after the SFTP_CLOSE state to avoid |
| 3361 |
errors that could happen due to open file handles during POSTQUOTE |
| 3362 |
operation */ |
| 3363 |
if(!premature && conn->data->set.postquote && !conn->bits.retry) |
| 3364 |
sshc->nextstate = SSH_SFTP_POSTQUOTE_INIT; |
| 3365 |
state(conn, SSH_SFTP_CLOSE); |
| 3366 |
} |
| 3367 |
return ssh_done(conn, status); |
| 3368 |
} |
| 3369 |
|
| 3370 |
/* return number of sent bytes */ |
| 3371 |
static ssize_t sftp_send(struct connectdata *conn, int sockindex, |
| 3372 |
const void *mem, size_t len, CURLcode *err) |
| 3373 |
{ |
| 3374 |
ssize_t nwrite; /* libssh2_sftp_write() used to return size_t in 0.14 |
| 3375 |
but is changed to ssize_t in 0.15. These days we don't |
| 3376 |
support libssh2 0.15*/ |
| 3377 |
(void)sockindex; |
| 3378 |
|
| 3379 |
nwrite = libssh2_sftp_write(conn->proto.sshc.sftp_handle, mem, len); |
| 3380 |
|
| 3381 |
ssh_block2waitfor(conn, (nwrite == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE); |
| 3382 |
|
| 3383 |
if(nwrite == LIBSSH2_ERROR_EAGAIN) { |
| 3384 |
*err = CURLE_AGAIN; |
| 3385 |
nwrite = 0; |
| 3386 |
} |
| 3387 |
else if(nwrite < LIBSSH2_ERROR_NONE) { |
| 3388 |
*err = libssh2_session_error_to_CURLE((int)nwrite); |
| 3389 |
nwrite = -1; |
| 3390 |
} |
| 3391 |
|
| 3392 |
return nwrite; |
| 3393 |
} |
| 3394 |
|
| 3395 |
/* |
| 3396 |
* Return number of received (decrypted) bytes |
| 3397 |
* or <0 on error |
| 3398 |
*/ |
| 3399 |
static ssize_t sftp_recv(struct connectdata *conn, int sockindex, |
| 3400 |
char *mem, size_t len, CURLcode *err) |
| 3401 |
{ |
| 3402 |
ssize_t nread; |
| 3403 |
(void)sockindex; |
| 3404 |
|
| 3405 |
nread = libssh2_sftp_read(conn->proto.sshc.sftp_handle, mem, len); |
| 3406 |
|
| 3407 |
ssh_block2waitfor(conn, (nread == LIBSSH2_ERROR_EAGAIN)?TRUE:FALSE); |
| 3408 |
|
| 3409 |
if(nread == LIBSSH2_ERROR_EAGAIN) { |
| 3410 |
*err = CURLE_AGAIN; |
| 3411 |
nread = -1; |
| 3412 |
|
| 3413 |
} |
| 3414 |
else if(nread < 0) { |
| 3415 |
*err = libssh2_session_error_to_CURLE((int)nread); |
| 3416 |
} |
| 3417 |
return nread; |
| 3418 |
} |
| 3419 |
|
| 3420 |
static const char *sftp_libssh2_strerror(int err) |
| 3421 |
{ |
| 3422 |
switch(err) { |
| 3423 |
case LIBSSH2_FX_NO_SUCH_FILE: |
| 3424 |
return "No such file or directory"; |
| 3425 |
|
| 3426 |
case LIBSSH2_FX_PERMISSION_DENIED: |
| 3427 |
return "Permission denied"; |
| 3428 |
|
| 3429 |
case LIBSSH2_FX_FAILURE: |
| 3430 |
return "Operation failed"; |
| 3431 |
|
| 3432 |
case LIBSSH2_FX_BAD_MESSAGE: |
| 3433 |
return "Bad message from SFTP server"; |
| 3434 |
|
| 3435 |
case LIBSSH2_FX_NO_CONNECTION: |
| 3436 |
return "Not connected to SFTP server"; |
| 3437 |
|
| 3438 |
case LIBSSH2_FX_CONNECTION_LOST: |
| 3439 |
return "Connection to SFTP server lost"; |
| 3440 |
|
| 3441 |
case LIBSSH2_FX_OP_UNSUPPORTED: |
| 3442 |
return "Operation not supported by SFTP server"; |
| 3443 |
|
| 3444 |
case LIBSSH2_FX_INVALID_HANDLE: |
| 3445 |
return "Invalid handle"; |
| 3446 |
|
| 3447 |
case LIBSSH2_FX_NO_SUCH_PATH: |
| 3448 |
return "No such file or directory"; |
| 3449 |
|
| 3450 |
case LIBSSH2_FX_FILE_ALREADY_EXISTS: |
| 3451 |
return "File already exists"; |
| 3452 |
|
| 3453 |
case LIBSSH2_FX_WRITE_PROTECT: |
| 3454 |
return "File is write protected"; |
| 3455 |
|
| 3456 |
case LIBSSH2_FX_NO_MEDIA: |
| 3457 |
return "No media"; |
| 3458 |
|
| 3459 |
case LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM: |
| 3460 |
return "Disk full"; |
| 3461 |
|
| 3462 |
case LIBSSH2_FX_QUOTA_EXCEEDED: |
| 3463 |
return "User quota exceeded"; |
| 3464 |
|
| 3465 |
case LIBSSH2_FX_UNKNOWN_PRINCIPLE: |
| 3466 |
return "Unknown principle"; |
| 3467 |
|
| 3468 |
case LIBSSH2_FX_LOCK_CONFlICT: |
| 3469 |
return "File lock conflict"; |
| 3470 |
|
| 3471 |
case LIBSSH2_FX_DIR_NOT_EMPTY: |
| 3472 |
return "Directory not empty"; |
| 3473 |
|
| 3474 |
case LIBSSH2_FX_NOT_A_DIRECTORY: |
| 3475 |
return "Not a directory"; |
| 3476 |
|
| 3477 |
case LIBSSH2_FX_INVALID_FILENAME: |
| 3478 |
return "Invalid filename"; |
| 3479 |
|
| 3480 |
case LIBSSH2_FX_LINK_LOOP: |
| 3481 |
return "Link points to itself"; |
| 3482 |
} |
| 3483 |
return "Unknown error in libssh2"; |
| 3484 |
} |
| 3485 |
|
| 3486 |
CURLcode Curl_ssh_init(void) |
| 3487 |
{ |
| 3488 |
#ifdef HAVE_LIBSSH2_INIT |
| 3489 |
if(libssh2_init(0)) { |
| 3490 |
DEBUGF(fprintf(stderr, "Error: libssh2_init failed\n")); |
| 3491 |
return CURLE_FAILED_INIT; |
| 3492 |
} |
| 3493 |
#endif |
| 3494 |
return CURLE_OK; |
| 3495 |
} |
| 3496 |
|
| 3497 |
void Curl_ssh_cleanup(void) |
| 3498 |
{ |
| 3499 |
#ifdef HAVE_LIBSSH2_EXIT |
| 3500 |
(void)libssh2_exit(); |
| 3501 |
#endif |
| 3502 |
} |
| 3503 |
|
| 3504 |
size_t Curl_ssh_version(char *buffer, size_t buflen) |
| 3505 |
{ |
| 3506 |
return msnprintf(buffer, buflen, "libssh2/%s", LIBSSH2_VERSION); |
| 3507 |
} |
| 3508 |
|
| 3509 |
#endif /* USE_LIBSSH2 */ |