ssh.h 8.5 KB

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