113498266Sopenharmony_ci#ifndef HEADER_CURL_SSH_H 213498266Sopenharmony_ci#define HEADER_CURL_SSH_H 313498266Sopenharmony_ci/*************************************************************************** 413498266Sopenharmony_ci * _ _ ____ _ 513498266Sopenharmony_ci * Project ___| | | | _ \| | 613498266Sopenharmony_ci * / __| | | | |_) | | 713498266Sopenharmony_ci * | (__| |_| | _ <| |___ 813498266Sopenharmony_ci * \___|\___/|_| \_\_____| 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 1113498266Sopenharmony_ci * 1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1513498266Sopenharmony_ci * 1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1913498266Sopenharmony_ci * 2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 2113498266Sopenharmony_ci * KIND, either express or implied. 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci * SPDX-License-Identifier: curl 2413498266Sopenharmony_ci * 2513498266Sopenharmony_ci ***************************************************************************/ 2613498266Sopenharmony_ci 2713498266Sopenharmony_ci#include "curl_setup.h" 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#if defined(USE_LIBSSH2) 3013498266Sopenharmony_ci#include <libssh2.h> 3113498266Sopenharmony_ci#include <libssh2_sftp.h> 3213498266Sopenharmony_ci#elif defined(USE_LIBSSH) 3313498266Sopenharmony_ci#include <libssh/libssh.h> 3413498266Sopenharmony_ci#include <libssh/sftp.h> 3513498266Sopenharmony_ci#elif defined(USE_WOLFSSH) 3613498266Sopenharmony_ci#include <wolfssh/ssh.h> 3713498266Sopenharmony_ci#include <wolfssh/wolfsftp.h> 3813498266Sopenharmony_ci#endif 3913498266Sopenharmony_ci 4013498266Sopenharmony_ci/**************************************************************************** 4113498266Sopenharmony_ci * SSH unique setup 4213498266Sopenharmony_ci ***************************************************************************/ 4313498266Sopenharmony_citypedef enum { 4413498266Sopenharmony_ci SSH_NO_STATE = -1, /* Used for "nextState" so say there is none */ 4513498266Sopenharmony_ci SSH_STOP = 0, /* do nothing state, stops the state machine */ 4613498266Sopenharmony_ci 4713498266Sopenharmony_ci SSH_INIT, /* First state in SSH-CONNECT */ 4813498266Sopenharmony_ci SSH_S_STARTUP, /* Session startup */ 4913498266Sopenharmony_ci SSH_HOSTKEY, /* verify hostkey */ 5013498266Sopenharmony_ci SSH_AUTHLIST, 5113498266Sopenharmony_ci SSH_AUTH_PKEY_INIT, 5213498266Sopenharmony_ci SSH_AUTH_PKEY, 5313498266Sopenharmony_ci SSH_AUTH_PASS_INIT, 5413498266Sopenharmony_ci SSH_AUTH_PASS, 5513498266Sopenharmony_ci SSH_AUTH_AGENT_INIT, /* initialize then wait for connection to agent */ 5613498266Sopenharmony_ci SSH_AUTH_AGENT_LIST, /* ask for list then wait for entire list to come */ 5713498266Sopenharmony_ci SSH_AUTH_AGENT, /* attempt one key at a time */ 5813498266Sopenharmony_ci SSH_AUTH_HOST_INIT, 5913498266Sopenharmony_ci SSH_AUTH_HOST, 6013498266Sopenharmony_ci SSH_AUTH_KEY_INIT, 6113498266Sopenharmony_ci SSH_AUTH_KEY, 6213498266Sopenharmony_ci SSH_AUTH_GSSAPI, 6313498266Sopenharmony_ci SSH_AUTH_DONE, 6413498266Sopenharmony_ci SSH_SFTP_INIT, 6513498266Sopenharmony_ci SSH_SFTP_REALPATH, /* Last state in SSH-CONNECT */ 6613498266Sopenharmony_ci 6713498266Sopenharmony_ci SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */ 6813498266Sopenharmony_ci SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */ 6913498266Sopenharmony_ci SSH_SFTP_QUOTE, 7013498266Sopenharmony_ci SSH_SFTP_NEXT_QUOTE, 7113498266Sopenharmony_ci SSH_SFTP_QUOTE_STAT, 7213498266Sopenharmony_ci SSH_SFTP_QUOTE_SETSTAT, 7313498266Sopenharmony_ci SSH_SFTP_QUOTE_SYMLINK, 7413498266Sopenharmony_ci SSH_SFTP_QUOTE_MKDIR, 7513498266Sopenharmony_ci SSH_SFTP_QUOTE_RENAME, 7613498266Sopenharmony_ci SSH_SFTP_QUOTE_RMDIR, 7713498266Sopenharmony_ci SSH_SFTP_QUOTE_UNLINK, 7813498266Sopenharmony_ci SSH_SFTP_QUOTE_STATVFS, 7913498266Sopenharmony_ci SSH_SFTP_GETINFO, 8013498266Sopenharmony_ci SSH_SFTP_FILETIME, 8113498266Sopenharmony_ci SSH_SFTP_TRANS_INIT, 8213498266Sopenharmony_ci SSH_SFTP_UPLOAD_INIT, 8313498266Sopenharmony_ci SSH_SFTP_CREATE_DIRS_INIT, 8413498266Sopenharmony_ci SSH_SFTP_CREATE_DIRS, 8513498266Sopenharmony_ci SSH_SFTP_CREATE_DIRS_MKDIR, 8613498266Sopenharmony_ci SSH_SFTP_READDIR_INIT, 8713498266Sopenharmony_ci SSH_SFTP_READDIR, 8813498266Sopenharmony_ci SSH_SFTP_READDIR_LINK, 8913498266Sopenharmony_ci SSH_SFTP_READDIR_BOTTOM, 9013498266Sopenharmony_ci SSH_SFTP_READDIR_DONE, 9113498266Sopenharmony_ci SSH_SFTP_DOWNLOAD_INIT, 9213498266Sopenharmony_ci SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */ 9313498266Sopenharmony_ci SSH_SFTP_CLOSE, /* Last state in SFTP-DONE */ 9413498266Sopenharmony_ci SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */ 9513498266Sopenharmony_ci SSH_SCP_TRANS_INIT, /* First state in SCP-DO */ 9613498266Sopenharmony_ci SSH_SCP_UPLOAD_INIT, 9713498266Sopenharmony_ci SSH_SCP_DOWNLOAD_INIT, 9813498266Sopenharmony_ci SSH_SCP_DOWNLOAD, 9913498266Sopenharmony_ci SSH_SCP_DONE, 10013498266Sopenharmony_ci SSH_SCP_SEND_EOF, 10113498266Sopenharmony_ci SSH_SCP_WAIT_EOF, 10213498266Sopenharmony_ci SSH_SCP_WAIT_CLOSE, 10313498266Sopenharmony_ci SSH_SCP_CHANNEL_FREE, /* Last state in SCP-DONE */ 10413498266Sopenharmony_ci SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */ 10513498266Sopenharmony_ci SSH_SESSION_FREE, /* Last state in SCP/SFTP-DISCONNECT */ 10613498266Sopenharmony_ci SSH_QUIT, 10713498266Sopenharmony_ci SSH_LAST /* never used */ 10813498266Sopenharmony_ci} sshstate; 10913498266Sopenharmony_ci 11013498266Sopenharmony_ci/* this struct is used in the HandleData struct which is part of the 11113498266Sopenharmony_ci Curl_easy, which means this is used on a per-easy handle basis. 11213498266Sopenharmony_ci Everything that is strictly related to a connection is banned from this 11313498266Sopenharmony_ci struct. */ 11413498266Sopenharmony_cistruct SSHPROTO { 11513498266Sopenharmony_ci char *path; /* the path we operate on */ 11613498266Sopenharmony_ci#ifdef USE_LIBSSH2 11713498266Sopenharmony_ci struct dynbuf readdir_link; 11813498266Sopenharmony_ci struct dynbuf readdir; 11913498266Sopenharmony_ci char *readdir_filename; 12013498266Sopenharmony_ci char *readdir_longentry; 12113498266Sopenharmony_ci 12213498266Sopenharmony_ci LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */ 12313498266Sopenharmony_ci 12413498266Sopenharmony_ci /* Here's a set of struct members used by the SFTP_READDIR state */ 12513498266Sopenharmony_ci LIBSSH2_SFTP_ATTRIBUTES readdir_attrs; 12613498266Sopenharmony_ci#endif 12713498266Sopenharmony_ci}; 12813498266Sopenharmony_ci 12913498266Sopenharmony_ci/* ssh_conn is used for struct connection-oriented data in the connectdata 13013498266Sopenharmony_ci struct */ 13113498266Sopenharmony_cistruct ssh_conn { 13213498266Sopenharmony_ci const char *authlist; /* List of auth. methods, managed by libssh2 */ 13313498266Sopenharmony_ci 13413498266Sopenharmony_ci /* common */ 13513498266Sopenharmony_ci const char *passphrase; /* pass-phrase to use */ 13613498266Sopenharmony_ci char *rsa_pub; /* strdup'ed public key file */ 13713498266Sopenharmony_ci char *rsa; /* strdup'ed private key file */ 13813498266Sopenharmony_ci bool authed; /* the connection has been authenticated fine */ 13913498266Sopenharmony_ci bool acceptfail; /* used by the SFTP_QUOTE (continue if 14013498266Sopenharmony_ci quote command fails) */ 14113498266Sopenharmony_ci sshstate state; /* always use ssh.c:state() to change state! */ 14213498266Sopenharmony_ci sshstate nextstate; /* the state to goto after stopping */ 14313498266Sopenharmony_ci CURLcode actualcode; /* the actual error code */ 14413498266Sopenharmony_ci struct curl_slist *quote_item; /* for the quote option */ 14513498266Sopenharmony_ci char *quote_path1; /* two generic pointers for the QUOTE stuff */ 14613498266Sopenharmony_ci char *quote_path2; 14713498266Sopenharmony_ci 14813498266Sopenharmony_ci char *homedir; /* when doing SFTP we figure out home dir in the 14913498266Sopenharmony_ci connect phase */ 15013498266Sopenharmony_ci /* end of READDIR stuff */ 15113498266Sopenharmony_ci 15213498266Sopenharmony_ci int secondCreateDirs; /* counter use by the code to see if the 15313498266Sopenharmony_ci second attempt has been made to change 15413498266Sopenharmony_ci to/create a directory */ 15513498266Sopenharmony_ci int orig_waitfor; /* default READ/WRITE bits wait for */ 15613498266Sopenharmony_ci char *slash_pos; /* used by the SFTP_CREATE_DIRS state */ 15713498266Sopenharmony_ci 15813498266Sopenharmony_ci#if defined(USE_LIBSSH) 15913498266Sopenharmony_ci char *readdir_linkPath; 16013498266Sopenharmony_ci size_t readdir_len; 16113498266Sopenharmony_ci struct dynbuf readdir_buf; 16213498266Sopenharmony_ci/* our variables */ 16313498266Sopenharmony_ci unsigned kbd_state; /* 0 or 1 */ 16413498266Sopenharmony_ci ssh_key privkey; 16513498266Sopenharmony_ci ssh_key pubkey; 16613498266Sopenharmony_ci int auth_methods; 16713498266Sopenharmony_ci ssh_session ssh_session; 16813498266Sopenharmony_ci ssh_scp scp_session; 16913498266Sopenharmony_ci sftp_session sftp_session; 17013498266Sopenharmony_ci sftp_file sftp_file; 17113498266Sopenharmony_ci sftp_dir sftp_dir; 17213498266Sopenharmony_ci 17313498266Sopenharmony_ci unsigned sftp_recv_state; /* 0 or 1 */ 17413498266Sopenharmony_ci int sftp_file_index; /* for async read */ 17513498266Sopenharmony_ci sftp_attributes readdir_attrs; /* used by the SFTP readdir actions */ 17613498266Sopenharmony_ci sftp_attributes readdir_link_attrs; /* used by the SFTP readdir actions */ 17713498266Sopenharmony_ci sftp_attributes quote_attrs; /* used by the SFTP_QUOTE state */ 17813498266Sopenharmony_ci 17913498266Sopenharmony_ci const char *readdir_filename; /* points within readdir_attrs */ 18013498266Sopenharmony_ci const char *readdir_longentry; 18113498266Sopenharmony_ci char *readdir_tmp; 18213498266Sopenharmony_ci#elif defined(USE_LIBSSH2) 18313498266Sopenharmony_ci LIBSSH2_SESSION *ssh_session; /* Secure Shell session */ 18413498266Sopenharmony_ci LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */ 18513498266Sopenharmony_ci LIBSSH2_SFTP *sftp_session; /* SFTP handle */ 18613498266Sopenharmony_ci LIBSSH2_SFTP_HANDLE *sftp_handle; 18713498266Sopenharmony_ci 18813498266Sopenharmony_ci#ifndef CURL_DISABLE_PROXY 18913498266Sopenharmony_ci /* for HTTPS proxy storage */ 19013498266Sopenharmony_ci Curl_recv *tls_recv; 19113498266Sopenharmony_ci Curl_send *tls_send; 19213498266Sopenharmony_ci#endif 19313498266Sopenharmony_ci 19413498266Sopenharmony_ci#ifdef HAVE_LIBSSH2_AGENT_API 19513498266Sopenharmony_ci LIBSSH2_AGENT *ssh_agent; /* proxy to ssh-agent/pageant */ 19613498266Sopenharmony_ci struct libssh2_agent_publickey *sshagent_identity, 19713498266Sopenharmony_ci *sshagent_prev_identity; 19813498266Sopenharmony_ci#endif 19913498266Sopenharmony_ci 20013498266Sopenharmony_ci /* note that HAVE_LIBSSH2_KNOWNHOST_API is a define set in the libssh2.h 20113498266Sopenharmony_ci header */ 20213498266Sopenharmony_ci#ifdef HAVE_LIBSSH2_KNOWNHOST_API 20313498266Sopenharmony_ci LIBSSH2_KNOWNHOSTS *kh; 20413498266Sopenharmony_ci#endif 20513498266Sopenharmony_ci#elif defined(USE_WOLFSSH) 20613498266Sopenharmony_ci WOLFSSH *ssh_session; 20713498266Sopenharmony_ci WOLFSSH_CTX *ctx; 20813498266Sopenharmony_ci word32 handleSz; 20913498266Sopenharmony_ci byte handle[WOLFSSH_MAX_HANDLE]; 21013498266Sopenharmony_ci curl_off_t offset; 21113498266Sopenharmony_ci#endif /* USE_LIBSSH */ 21213498266Sopenharmony_ci}; 21313498266Sopenharmony_ci 21413498266Sopenharmony_ci#if defined(USE_LIBSSH2) 21513498266Sopenharmony_ci 21613498266Sopenharmony_ci/* Feature detection based on version numbers to better work with 21713498266Sopenharmony_ci non-configure platforms */ 21813498266Sopenharmony_ci 21913498266Sopenharmony_ci#if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x001000) 22013498266Sopenharmony_ci# error "SCP/SFTP protocols require libssh2 0.16 or later" 22113498266Sopenharmony_ci#endif 22213498266Sopenharmony_ci 22313498266Sopenharmony_ci#if LIBSSH2_VERSION_NUM >= 0x010000 22413498266Sopenharmony_ci#define HAVE_LIBSSH2_SFTP_SEEK64 1 22513498266Sopenharmony_ci#endif 22613498266Sopenharmony_ci 22713498266Sopenharmony_ci#if LIBSSH2_VERSION_NUM >= 0x010100 22813498266Sopenharmony_ci#define HAVE_LIBSSH2_VERSION 1 22913498266Sopenharmony_ci#endif 23013498266Sopenharmony_ci 23113498266Sopenharmony_ci#if LIBSSH2_VERSION_NUM >= 0x010205 23213498266Sopenharmony_ci#define HAVE_LIBSSH2_INIT 1 23313498266Sopenharmony_ci#define HAVE_LIBSSH2_EXIT 1 23413498266Sopenharmony_ci#endif 23513498266Sopenharmony_ci 23613498266Sopenharmony_ci#if LIBSSH2_VERSION_NUM >= 0x010206 23713498266Sopenharmony_ci#define HAVE_LIBSSH2_KNOWNHOST_CHECKP 1 23813498266Sopenharmony_ci#define HAVE_LIBSSH2_SCP_SEND64 1 23913498266Sopenharmony_ci#endif 24013498266Sopenharmony_ci 24113498266Sopenharmony_ci#if LIBSSH2_VERSION_NUM >= 0x010208 24213498266Sopenharmony_ci#define HAVE_LIBSSH2_SESSION_HANDSHAKE 1 24313498266Sopenharmony_ci#endif 24413498266Sopenharmony_ci 24513498266Sopenharmony_ci#ifdef HAVE_LIBSSH2_VERSION 24613498266Sopenharmony_ci/* get it run-time if possible */ 24713498266Sopenharmony_ci#define CURL_LIBSSH2_VERSION libssh2_version(0) 24813498266Sopenharmony_ci#else 24913498266Sopenharmony_ci/* use build-time if run-time not possible */ 25013498266Sopenharmony_ci#define CURL_LIBSSH2_VERSION LIBSSH2_VERSION 25113498266Sopenharmony_ci#endif 25213498266Sopenharmony_ci 25313498266Sopenharmony_ci#endif /* USE_LIBSSH2 */ 25413498266Sopenharmony_ci 25513498266Sopenharmony_ci#ifdef USE_SSH 25613498266Sopenharmony_ci 25713498266Sopenharmony_ciextern const struct Curl_handler Curl_handler_scp; 25813498266Sopenharmony_ciextern const struct Curl_handler Curl_handler_sftp; 25913498266Sopenharmony_ci 26013498266Sopenharmony_ci/* generic SSH backend functions */ 26113498266Sopenharmony_ciCURLcode Curl_ssh_init(void); 26213498266Sopenharmony_civoid Curl_ssh_cleanup(void); 26313498266Sopenharmony_civoid Curl_ssh_version(char *buffer, size_t buflen); 26413498266Sopenharmony_civoid Curl_ssh_attach(struct Curl_easy *data, 26513498266Sopenharmony_ci struct connectdata *conn); 26613498266Sopenharmony_ci#else 26713498266Sopenharmony_ci/* for non-SSH builds */ 26813498266Sopenharmony_ci#define Curl_ssh_cleanup() 26913498266Sopenharmony_ci#define Curl_ssh_attach(x,y) 27013498266Sopenharmony_ci#define Curl_ssh_init() 0 27113498266Sopenharmony_ci#endif 27213498266Sopenharmony_ci 27313498266Sopenharmony_ci#endif /* HEADER_CURL_SSH_H */ 274