ssh.h 6.8 KB

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