1 #ifndef HEADER_CURL_SSH_H 2 #define HEADER_CURL_SSH_H 3 /*************************************************************************** 4 * _ _ ____ _ 5 * Project ___| | | | _ \| | 6 * / __| | | | |_) | | 7 * | (__| |_| | _ <| |___ 8 * \___|\___/|_| \_\_____| 9 * 10 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 11 * 12 * This software is licensed as described in the file COPYING, which 13 * you should have received as part of this distribution. The terms 14 * are also available at https://curl.se/docs/copyright.html. 15 * 16 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 17 * copies of the Software, and permit persons to whom the Software is 18 * furnished to do so, under the terms of the COPYING file. 19 * 20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 21 * KIND, either express or implied. 22 * 23 * SPDX-License-Identifier: curl 24 * 25 ***************************************************************************/ 26 27 #include "curl_setup.h" 28 29 #if defined(USE_LIBSSH2) 30 #include <libssh2.h> 31 #include <libssh2_sftp.h> 32 #elif defined(USE_LIBSSH) 33 #include <libssh/libssh.h> 34 #include <libssh/sftp.h> 35 #elif defined(USE_WOLFSSH) 36 #include <wolfssh/ssh.h> 37 #include <wolfssh/wolfsftp.h> 38 #endif 39 40 /**************************************************************************** 41 * SSH unique setup 42 ***************************************************************************/ 43 typedef enum { 44 SSH_NO_STATE = -1, /* Used for "nextState" so say there is none */ 45 SSH_STOP = 0, /* do nothing state, stops the state machine */ 46 47 SSH_INIT, /* First state in SSH-CONNECT */ 48 SSH_S_STARTUP, /* Session startup */ 49 SSH_HOSTKEY, /* verify hostkey */ 50 SSH_AUTHLIST, 51 SSH_AUTH_PKEY_INIT, 52 SSH_AUTH_PKEY, 53 SSH_AUTH_PASS_INIT, 54 SSH_AUTH_PASS, 55 SSH_AUTH_AGENT_INIT, /* initialize then wait for connection to agent */ 56 SSH_AUTH_AGENT_LIST, /* ask for list then wait for entire list to come */ 57 SSH_AUTH_AGENT, /* attempt one key at a time */ 58 SSH_AUTH_HOST_INIT, 59 SSH_AUTH_HOST, 60 SSH_AUTH_KEY_INIT, 61 SSH_AUTH_KEY, 62 SSH_AUTH_GSSAPI, 63 SSH_AUTH_DONE, 64 SSH_SFTP_INIT, 65 SSH_SFTP_REALPATH, /* Last state in SSH-CONNECT */ 66 67 SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */ 68 SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */ 69 SSH_SFTP_QUOTE, 70 SSH_SFTP_NEXT_QUOTE, 71 SSH_SFTP_QUOTE_STAT, 72 SSH_SFTP_QUOTE_SETSTAT, 73 SSH_SFTP_QUOTE_SYMLINK, 74 SSH_SFTP_QUOTE_MKDIR, 75 SSH_SFTP_QUOTE_RENAME, 76 SSH_SFTP_QUOTE_RMDIR, 77 SSH_SFTP_QUOTE_UNLINK, 78 SSH_SFTP_QUOTE_STATVFS, 79 SSH_SFTP_GETINFO, 80 SSH_SFTP_FILETIME, 81 SSH_SFTP_TRANS_INIT, 82 SSH_SFTP_UPLOAD_INIT, 83 SSH_SFTP_CREATE_DIRS_INIT, 84 SSH_SFTP_CREATE_DIRS, 85 SSH_SFTP_CREATE_DIRS_MKDIR, 86 SSH_SFTP_READDIR_INIT, 87 SSH_SFTP_READDIR, 88 SSH_SFTP_READDIR_LINK, 89 SSH_SFTP_READDIR_BOTTOM, 90 SSH_SFTP_READDIR_DONE, 91 SSH_SFTP_DOWNLOAD_INIT, 92 SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */ 93 SSH_SFTP_CLOSE, /* Last state in SFTP-DONE */ 94 SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */ 95 SSH_SCP_TRANS_INIT, /* First state in SCP-DO */ 96 SSH_SCP_UPLOAD_INIT, 97 SSH_SCP_DOWNLOAD_INIT, 98 SSH_SCP_DOWNLOAD, 99 SSH_SCP_DONE, 100 SSH_SCP_SEND_EOF, 101 SSH_SCP_WAIT_EOF, 102 SSH_SCP_WAIT_CLOSE, 103 SSH_SCP_CHANNEL_FREE, /* Last state in SCP-DONE */ 104 SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */ 105 SSH_SESSION_FREE, /* Last state in SCP/SFTP-DISCONNECT */ 106 SSH_QUIT, 107 SSH_LAST /* never used */ 108 } sshstate; 109 110 /* this struct is used in the HandleData struct which is part of the 111 Curl_easy, which means this is used on a per-easy handle basis. 112 Everything that is strictly related to a connection is banned from this 113 struct. */ 114 struct SSHPROTO { 115 char *path; /* the path we operate on */ 116 #ifdef USE_LIBSSH2 117 struct dynbuf readdir_link; 118 struct dynbuf readdir; 119 char *readdir_filename; 120 char *readdir_longentry; 121 122 LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */ 123 124 /* Here's a set of struct members used by the SFTP_READDIR state */ 125 LIBSSH2_SFTP_ATTRIBUTES readdir_attrs; 126 #endif 127 }; 128 129 /* ssh_conn is used for struct connection-oriented data in the connectdata 130 struct */ 131 struct ssh_conn { 132 const char *authlist; /* List of auth. methods, managed by libssh2 */ 133 134 /* common */ 135 const char *passphrase; /* pass-phrase to use */ 136 char *rsa_pub; /* strdup'ed public key file */ 137 char *rsa; /* strdup'ed private key file */ 138 bool authed; /* the connection has been authenticated fine */ 139 bool acceptfail; /* used by the SFTP_QUOTE (continue if 140 quote command fails) */ 141 sshstate state; /* always use ssh.c:state() to change state! */ 142 sshstate nextstate; /* the state to goto after stopping */ 143 CURLcode actualcode; /* the actual error code */ 144 struct curl_slist *quote_item; /* for the quote option */ 145 char *quote_path1; /* two generic pointers for the QUOTE stuff */ 146 char *quote_path2; 147 148 char *homedir; /* when doing SFTP we figure out home dir in the 149 connect phase */ 150 /* end of READDIR stuff */ 151 152 int secondCreateDirs; /* counter use by the code to see if the 153 second attempt has been made to change 154 to/create a directory */ 155 int orig_waitfor; /* default READ/WRITE bits wait for */ 156 char *slash_pos; /* used by the SFTP_CREATE_DIRS state */ 157 158 #if defined(USE_LIBSSH) 159 char *readdir_linkPath; 160 size_t readdir_len; 161 struct dynbuf readdir_buf; 162 /* our variables */ 163 unsigned kbd_state; /* 0 or 1 */ 164 ssh_key privkey; 165 ssh_key pubkey; 166 int auth_methods; 167 ssh_session ssh_session; 168 ssh_scp scp_session; 169 sftp_session sftp_session; 170 sftp_file sftp_file; 171 sftp_dir sftp_dir; 172 173 unsigned sftp_recv_state; /* 0 or 1 */ 174 int sftp_file_index; /* for async read */ 175 sftp_attributes readdir_attrs; /* used by the SFTP readdir actions */ 176 sftp_attributes readdir_link_attrs; /* used by the SFTP readdir actions */ 177 sftp_attributes quote_attrs; /* used by the SFTP_QUOTE state */ 178 179 const char *readdir_filename; /* points within readdir_attrs */ 180 const char *readdir_longentry; 181 char *readdir_tmp; 182 #elif defined(USE_LIBSSH2) 183 LIBSSH2_SESSION *ssh_session; /* Secure Shell session */ 184 LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */ 185 LIBSSH2_SFTP *sftp_session; /* SFTP handle */ 186 LIBSSH2_SFTP_HANDLE *sftp_handle; 187 188 #ifndef CURL_DISABLE_PROXY 189 /* for HTTPS proxy storage */ 190 Curl_recv *tls_recv; 191 Curl_send *tls_send; 192 #endif 193 194 #ifdef HAVE_LIBSSH2_AGENT_API 195 LIBSSH2_AGENT *ssh_agent; /* proxy to ssh-agent/pageant */ 196 struct libssh2_agent_publickey *sshagent_identity, 197 *sshagent_prev_identity; 198 #endif 199 200 /* note that HAVE_LIBSSH2_KNOWNHOST_API is a define set in the libssh2.h 201 header */ 202 #ifdef HAVE_LIBSSH2_KNOWNHOST_API 203 LIBSSH2_KNOWNHOSTS *kh; 204 #endif 205 #elif defined(USE_WOLFSSH) 206 WOLFSSH *ssh_session; 207 WOLFSSH_CTX *ctx; 208 word32 handleSz; 209 byte handle[WOLFSSH_MAX_HANDLE]; 210 curl_off_t offset; 211 #endif /* USE_LIBSSH */ 212 }; 213 214 #if defined(USE_LIBSSH2) 215 216 /* Feature detection based on version numbers to better work with 217 non-configure platforms */ 218 219 #if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x001000) 220 # error "SCP/SFTP protocols require libssh2 0.16 or later" 221 #endif 222 223 #if LIBSSH2_VERSION_NUM >= 0x010000 224 #define HAVE_LIBSSH2_SFTP_SEEK64 1 225 #endif 226 227 #if LIBSSH2_VERSION_NUM >= 0x010100 228 #define HAVE_LIBSSH2_VERSION 1 229 #endif 230 231 #if LIBSSH2_VERSION_NUM >= 0x010205 232 #define HAVE_LIBSSH2_INIT 1 233 #define HAVE_LIBSSH2_EXIT 1 234 #endif 235 236 #if LIBSSH2_VERSION_NUM >= 0x010206 237 #define HAVE_LIBSSH2_KNOWNHOST_CHECKP 1 238 #define HAVE_LIBSSH2_SCP_SEND64 1 239 #endif 240 241 #if LIBSSH2_VERSION_NUM >= 0x010208 242 #define HAVE_LIBSSH2_SESSION_HANDSHAKE 1 243 #endif 244 245 #ifdef HAVE_LIBSSH2_VERSION 246 /* get it run-time if possible */ 247 #define CURL_LIBSSH2_VERSION libssh2_version(0) 248 #else 249 /* use build-time if run-time not possible */ 250 #define CURL_LIBSSH2_VERSION LIBSSH2_VERSION 251 #endif 252 253 #endif /* USE_LIBSSH2 */ 254 255 #ifdef USE_SSH 256 257 extern const struct Curl_handler Curl_handler_scp; 258 extern const struct Curl_handler Curl_handler_sftp; 259 260 /* generic SSH backend functions */ 261 CURLcode Curl_ssh_init(void); 262 void Curl_ssh_cleanup(void); 263 void Curl_ssh_version(char *buffer, size_t buflen); 264 void Curl_ssh_attach(struct Curl_easy *data, 265 struct connectdata *conn); 266 #else 267 /* for non-SSH builds */ 268 #define Curl_ssh_cleanup() 269 #define Curl_ssh_attach(x,y) 270 #endif 271 272 #endif /* HEADER_CURL_SSH_H */ 273