ssh.h 8.7 KB

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