ssh.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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, <[email protected]>, 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. #ifdef 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. #endif
  36. #include "curl_path.h"
  37. /* meta key for storing protocol meta at easy handle */
  38. #define CURL_META_SSH_EASY "meta:proto:ssh:easy"
  39. /* meta key for storing protocol meta at connection */
  40. #define CURL_META_SSH_CONN "meta:proto:ssh:conn"
  41. /****************************************************************************
  42. * SSH unique setup
  43. ***************************************************************************/
  44. typedef enum {
  45. SSH_NO_STATE = -1, /* Used for "nextState" so say there is none */
  46. SSH_STOP = 0, /* do nothing state, stops the state machine */
  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. SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */
  67. SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */
  68. SSH_SFTP_QUOTE,
  69. SSH_SFTP_NEXT_QUOTE,
  70. SSH_SFTP_QUOTE_STAT,
  71. SSH_SFTP_QUOTE_SETSTAT,
  72. SSH_SFTP_QUOTE_SYMLINK,
  73. SSH_SFTP_QUOTE_MKDIR,
  74. SSH_SFTP_QUOTE_RENAME,
  75. SSH_SFTP_QUOTE_RMDIR,
  76. SSH_SFTP_QUOTE_UNLINK,
  77. SSH_SFTP_QUOTE_STATVFS,
  78. SSH_SFTP_GETINFO,
  79. SSH_SFTP_FILETIME,
  80. SSH_SFTP_TRANS_INIT,
  81. SSH_SFTP_UPLOAD_INIT,
  82. SSH_SFTP_CREATE_DIRS_INIT,
  83. SSH_SFTP_CREATE_DIRS,
  84. SSH_SFTP_CREATE_DIRS_MKDIR,
  85. SSH_SFTP_READDIR_INIT,
  86. SSH_SFTP_READDIR,
  87. SSH_SFTP_READDIR_LINK,
  88. SSH_SFTP_READDIR_BOTTOM,
  89. SSH_SFTP_READDIR_DONE,
  90. SSH_SFTP_DOWNLOAD_INIT,
  91. SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */
  92. SSH_SFTP_CLOSE, /* Last state in SFTP-DONE */
  93. SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */
  94. SSH_SCP_TRANS_INIT, /* First state in SCP-DO */
  95. SSH_SCP_UPLOAD_INIT,
  96. SSH_SCP_DOWNLOAD_INIT,
  97. SSH_SCP_DOWNLOAD,
  98. SSH_SCP_DONE,
  99. SSH_SCP_SEND_EOF,
  100. SSH_SCP_WAIT_EOF,
  101. SSH_SCP_WAIT_CLOSE,
  102. SSH_SCP_CHANNEL_FREE, /* Last state in SCP-DONE */
  103. SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */
  104. SSH_SESSION_FREE, /* Last state in SCP/SFTP-DISCONNECT */
  105. SSH_QUIT,
  106. SSH_LAST /* never used */
  107. } sshstate;
  108. #define CURL_PATH_MAX 1024
  109. /* this struct is used in the HandleData struct which is part of the
  110. Curl_easy, which means this is used on a per-easy handle basis.
  111. Everything that is strictly related to a connection is banned from this
  112. struct. */
  113. struct SSHPROTO {
  114. char *path; /* the path we operate on, at least one byte long */
  115. #ifdef USE_LIBSSH2
  116. struct dynbuf readdir_link;
  117. struct dynbuf readdir;
  118. char readdir_filename[CURL_PATH_MAX + 1];
  119. char readdir_longentry[CURL_PATH_MAX + 1];
  120. LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */
  121. /* Here's a set of struct members used by the SFTP_READDIR state */
  122. LIBSSH2_SFTP_ATTRIBUTES readdir_attrs;
  123. #endif
  124. };
  125. /* ssh_conn is used for struct connection-oriented data in the connectdata
  126. struct */
  127. struct ssh_conn {
  128. const char *authlist; /* List of auth. methods, managed by libssh2 */
  129. /* common */
  130. const char *passphrase; /* pass-phrase to use */
  131. char *rsa_pub; /* strdup'ed public key file */
  132. char *rsa; /* strdup'ed private key file */
  133. sshstate state; /* always use ssh.c:state() to change state! */
  134. sshstate nextstate; /* the state to goto after stopping */
  135. struct curl_slist *quote_item; /* for the quote option */
  136. char *quote_path1; /* two generic pointers for the QUOTE stuff */
  137. char *quote_path2;
  138. char *homedir; /* when doing SFTP we figure out home dir in the
  139. connect phase */
  140. /* end of READDIR stuff */
  141. int secondCreateDirs; /* counter use by the code to see if the
  142. second attempt has been made to change
  143. to/create a directory */
  144. int orig_waitfor; /* default READ/WRITE bits wait for */
  145. char *slash_pos; /* used by the SFTP_CREATE_DIRS state */
  146. #ifdef USE_LIBSSH
  147. CURLcode actualcode; /* the actual error code */
  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. #if LIBSSH_VERSION_INT > SSH_VERSION_INT(0, 11, 0)
  163. sftp_aio sftp_recv_aio;
  164. sftp_aio sftp_send_aio;
  165. unsigned sftp_send_state; /* 0 or 1 */
  166. #else
  167. int sftp_file_index; /* for async read */
  168. #endif
  169. sftp_attributes readdir_attrs; /* used by the SFTP readdir actions */
  170. sftp_attributes readdir_link_attrs; /* used by the SFTP readdir actions */
  171. sftp_attributes quote_attrs; /* used by the SFTP_QUOTE state */
  172. const char *readdir_filename; /* points within readdir_attrs */
  173. const char *readdir_longentry;
  174. char *readdir_tmp;
  175. BIT(initialised);
  176. #elif defined(USE_LIBSSH2)
  177. LIBSSH2_SESSION *ssh_session; /* Secure Shell session */
  178. LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */
  179. LIBSSH2_SFTP *sftp_session; /* SFTP handle */
  180. LIBSSH2_SFTP_HANDLE *sftp_handle;
  181. #ifndef CURL_DISABLE_PROXY
  182. /* for HTTPS proxy storage */
  183. Curl_recv *tls_recv;
  184. Curl_send *tls_send;
  185. #endif
  186. LIBSSH2_AGENT *ssh_agent; /* proxy to ssh-agent/pageant */
  187. struct libssh2_agent_publickey *sshagent_identity;
  188. struct libssh2_agent_publickey *sshagent_prev_identity;
  189. LIBSSH2_KNOWNHOSTS *kh;
  190. #endif /* USE_LIBSSH */
  191. BIT(authed); /* the connection has been authenticated fine */
  192. BIT(acceptfail); /* used by the SFTP_QUOTE (continue if
  193. quote command fails) */
  194. };
  195. #ifdef USE_LIBSSH
  196. #if LIBSSH_VERSION_INT < SSH_VERSION_INT(0, 9, 0)
  197. # error "SCP/SFTP protocols require libssh 0.9.0 or later"
  198. #endif
  199. #endif
  200. #ifdef USE_LIBSSH2
  201. /* Feature detection based on version numbers to better work with
  202. non-configure platforms */
  203. #if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x010208)
  204. # error "SCP/SFTP protocols require libssh2 1.2.8 or later"
  205. /* 1.2.8 was released on April 5 2011 */
  206. #endif
  207. #endif /* USE_LIBSSH2 */
  208. #ifdef USE_SSH
  209. extern const struct Curl_handler Curl_handler_scp;
  210. extern const struct Curl_handler Curl_handler_sftp;
  211. /* generic SSH backend functions */
  212. CURLcode Curl_ssh_init(void);
  213. void Curl_ssh_cleanup(void);
  214. void Curl_ssh_version(char *buffer, size_t buflen);
  215. void Curl_ssh_attach(struct Curl_easy *data,
  216. struct connectdata *conn);
  217. #else
  218. /* for non-SSH builds */
  219. #define Curl_ssh_cleanup()
  220. #define Curl_ssh_attach(x,y)
  221. #define Curl_ssh_init() 0
  222. #endif
  223. #endif /* HEADER_CURL_SSH_H */