ssh.h 8.0 KB

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