ssh.h (8771B)
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 /* in 0.10.0 or later, ignore deprecated warnings */ 34 #define SSH_SUPPRESS_DEPRECATED 35 #include <libssh/libssh.h> 36 #include <libssh/sftp.h> 37 #elif defined(USE_WOLFSSH) 38 #include <wolfssh/ssh.h> 39 #include <wolfssh/wolfsftp.h> 40 #endif 41 42 #include "curl_path.h" 43 44 /* meta key for storing protocol meta at easy handle */ 45 #define CURL_META_SSH_EASY "meta:proto:ssh:easy" 46 /* meta key for storing protocol meta at connection */ 47 #define CURL_META_SSH_CONN "meta:proto:ssh:conn" 48 49 /**************************************************************************** 50 * SSH unique setup 51 ***************************************************************************/ 52 typedef enum { 53 SSH_NO_STATE = -1, /* Used for "nextState" so say there is none */ 54 SSH_STOP = 0, /* do nothing state, stops the state machine */ 55 56 SSH_INIT, /* First state in SSH-CONNECT */ 57 SSH_S_STARTUP, /* Session startup */ 58 SSH_HOSTKEY, /* verify hostkey */ 59 SSH_AUTHLIST, 60 SSH_AUTH_PKEY_INIT, 61 SSH_AUTH_PKEY, 62 SSH_AUTH_PASS_INIT, 63 SSH_AUTH_PASS, 64 SSH_AUTH_AGENT_INIT, /* initialize then wait for connection to agent */ 65 SSH_AUTH_AGENT_LIST, /* ask for list then wait for entire list to come */ 66 SSH_AUTH_AGENT, /* attempt one key at a time */ 67 SSH_AUTH_HOST_INIT, 68 SSH_AUTH_HOST, 69 SSH_AUTH_KEY_INIT, 70 SSH_AUTH_KEY, 71 SSH_AUTH_GSSAPI, 72 SSH_AUTH_DONE, 73 SSH_SFTP_INIT, 74 SSH_SFTP_REALPATH, /* Last state in SSH-CONNECT */ 75 76 SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */ 77 SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */ 78 SSH_SFTP_QUOTE, 79 SSH_SFTP_NEXT_QUOTE, 80 SSH_SFTP_QUOTE_STAT, 81 SSH_SFTP_QUOTE_SETSTAT, 82 SSH_SFTP_QUOTE_SYMLINK, 83 SSH_SFTP_QUOTE_MKDIR, 84 SSH_SFTP_QUOTE_RENAME, 85 SSH_SFTP_QUOTE_RMDIR, 86 SSH_SFTP_QUOTE_UNLINK, 87 SSH_SFTP_QUOTE_STATVFS, 88 SSH_SFTP_GETINFO, 89 SSH_SFTP_FILETIME, 90 SSH_SFTP_TRANS_INIT, 91 SSH_SFTP_UPLOAD_INIT, 92 SSH_SFTP_CREATE_DIRS_INIT, 93 SSH_SFTP_CREATE_DIRS, 94 SSH_SFTP_CREATE_DIRS_MKDIR, 95 SSH_SFTP_READDIR_INIT, 96 SSH_SFTP_READDIR, 97 SSH_SFTP_READDIR_LINK, 98 SSH_SFTP_READDIR_BOTTOM, 99 SSH_SFTP_READDIR_DONE, 100 SSH_SFTP_DOWNLOAD_INIT, 101 SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */ 102 SSH_SFTP_CLOSE, /* Last state in SFTP-DONE */ 103 SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */ 104 SSH_SCP_TRANS_INIT, /* First state in SCP-DO */ 105 SSH_SCP_UPLOAD_INIT, 106 SSH_SCP_DOWNLOAD_INIT, 107 SSH_SCP_DOWNLOAD, 108 SSH_SCP_DONE, 109 SSH_SCP_SEND_EOF, 110 SSH_SCP_WAIT_EOF, 111 SSH_SCP_WAIT_CLOSE, 112 SSH_SCP_CHANNEL_FREE, /* Last state in SCP-DONE */ 113 SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */ 114 SSH_SESSION_FREE, /* Last state in SCP/SFTP-DISCONNECT */ 115 SSH_QUIT, 116 SSH_LAST /* never used */ 117 } sshstate; 118 119 #define CURL_PATH_MAX 1024 120 121 /* this struct is used in the HandleData struct which is part of the 122 Curl_easy, which means this is used on a per-easy handle basis. 123 Everything that is strictly related to a connection is banned from this 124 struct. */ 125 struct SSHPROTO { 126 char *path; /* the path we operate on */ 127 #ifdef USE_LIBSSH2 128 struct dynbuf readdir_link; 129 struct dynbuf readdir; 130 char readdir_filename[CURL_PATH_MAX + 1]; 131 char readdir_longentry[CURL_PATH_MAX + 1]; 132 133 LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */ 134 135 /* Here's a set of struct members used by the SFTP_READDIR state */ 136 LIBSSH2_SFTP_ATTRIBUTES readdir_attrs; 137 #endif 138 }; 139 140 /* ssh_conn is used for struct connection-oriented data in the connectdata 141 struct */ 142 struct ssh_conn { 143 const char *authlist; /* List of auth. methods, managed by libssh2 */ 144 145 /* common */ 146 const char *passphrase; /* pass-phrase to use */ 147 char *rsa_pub; /* strdup'ed public key file */ 148 char *rsa; /* strdup'ed private key file */ 149 sshstate state; /* always use ssh.c:state() to change state! */ 150 sshstate nextstate; /* the state to goto after stopping */ 151 struct curl_slist *quote_item; /* for the quote option */ 152 char *quote_path1; /* two generic pointers for the QUOTE stuff */ 153 char *quote_path2; 154 155 char *homedir; /* when doing SFTP we figure out home dir in the 156 connect phase */ 157 /* end of READDIR stuff */ 158 159 int secondCreateDirs; /* counter use by the code to see if the 160 second attempt has been made to change 161 to/create a directory */ 162 int orig_waitfor; /* default READ/WRITE bits wait for */ 163 char *slash_pos; /* used by the SFTP_CREATE_DIRS state */ 164 165 #if defined(USE_LIBSSH) 166 CURLcode actualcode; /* the actual error code */ 167 char *readdir_linkPath; 168 size_t readdir_len; 169 struct dynbuf readdir_buf; 170 /* our variables */ 171 unsigned kbd_state; /* 0 or 1 */ 172 ssh_key privkey; 173 ssh_key pubkey; 174 unsigned int auth_methods; 175 ssh_session ssh_session; 176 ssh_scp scp_session; 177 sftp_session sftp_session; 178 sftp_file sftp_file; 179 sftp_dir sftp_dir; 180 181 unsigned sftp_recv_state; /* 0 or 1 */ 182 #if LIBSSH_VERSION_INT > SSH_VERSION_INT(0, 11, 0) 183 sftp_aio sftp_aio; 184 unsigned sftp_send_state; /* 0 or 1 */ 185 #endif 186 int sftp_file_index; /* for async read */ 187 sftp_attributes readdir_attrs; /* used by the SFTP readdir actions */ 188 sftp_attributes readdir_link_attrs; /* used by the SFTP readdir actions */ 189 sftp_attributes quote_attrs; /* used by the SFTP_QUOTE state */ 190 191 const char *readdir_filename; /* points within readdir_attrs */ 192 const char *readdir_longentry; 193 char *readdir_tmp; 194 BIT(initialised); 195 #elif defined(USE_LIBSSH2) 196 LIBSSH2_SESSION *ssh_session; /* Secure Shell session */ 197 LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */ 198 LIBSSH2_SFTP *sftp_session; /* SFTP handle */ 199 LIBSSH2_SFTP_HANDLE *sftp_handle; 200 201 #ifndef CURL_DISABLE_PROXY 202 /* for HTTPS proxy storage */ 203 Curl_recv *tls_recv; 204 Curl_send *tls_send; 205 #endif 206 207 LIBSSH2_AGENT *ssh_agent; /* proxy to ssh-agent/pageant */ 208 struct libssh2_agent_publickey *sshagent_identity; 209 struct libssh2_agent_publickey *sshagent_prev_identity; 210 LIBSSH2_KNOWNHOSTS *kh; 211 #elif defined(USE_WOLFSSH) 212 CURLcode actualcode; /* the actual error code */ 213 WOLFSSH *ssh_session; 214 WOLFSSH_CTX *ctx; 215 word32 handleSz; 216 byte handle[WOLFSSH_MAX_HANDLE]; 217 curl_off_t offset; 218 BIT(initialised); 219 #endif /* USE_LIBSSH */ 220 BIT(authed); /* the connection has been authenticated fine */ 221 BIT(acceptfail); /* used by the SFTP_QUOTE (continue if 222 quote command fails) */ 223 }; 224 225 #ifdef USE_LIBSSH 226 #if LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 9, 0) 227 # error "SCP/SFTP protocols require libssh 0.9.0 or later" 228 #endif 229 #endif 230 231 #if defined(USE_LIBSSH2) 232 233 /* Feature detection based on version numbers to better work with 234 non-configure platforms */ 235 236 #if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x010208) 237 # error "SCP/SFTP protocols require libssh2 1.2.8 or later" 238 /* 1.2.8 was released on April 5 2011 */ 239 #endif 240 241 #endif /* USE_LIBSSH2 */ 242 243 #ifdef USE_SSH 244 245 extern const struct Curl_handler Curl_handler_scp; 246 extern const struct Curl_handler Curl_handler_sftp; 247 248 /* generic SSH backend functions */ 249 CURLcode Curl_ssh_init(void); 250 void Curl_ssh_cleanup(void); 251 void Curl_ssh_version(char *buffer, size_t buflen); 252 void Curl_ssh_attach(struct Curl_easy *data, 253 struct connectdata *conn); 254 #else 255 /* for non-SSH builds */ 256 #define Curl_ssh_cleanup() 257 #define Curl_ssh_attach(x,y) 258 #define Curl_ssh_init() 0 259 #endif 260 261 #endif /* HEADER_CURL_SSH_H */