PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/libs/libcurl/curl-7/lib/vssh/ssh.h

https://gitlab.com/thanhnhat041/padavan-ng
C Header | 271 lines | 190 code | 34 blank | 47 comment | 1 complexity | 237eaa15640d0c52e2b2029ed873c818 MD5 | raw file
  1. #ifndef HEADER_CURL_SSH_H
  2. #define HEADER_CURL_SSH_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2021, 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. #ifdef USE_LIBSSH2
  109. struct dynbuf readdir_link;
  110. struct dynbuf readdir;
  111. char *readdir_filename;
  112. char *readdir_longentry;
  113. LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */
  114. /* Here's a set of struct members used by the SFTP_READDIR state */
  115. LIBSSH2_SFTP_ATTRIBUTES readdir_attrs;
  116. #endif
  117. };
  118. /* ssh_conn is used for struct connection-oriented data in the connectdata
  119. struct */
  120. struct ssh_conn {
  121. const char *authlist; /* List of auth. methods, managed by libssh2 */
  122. /* common */
  123. const char *passphrase; /* pass-phrase to use */
  124. char *rsa_pub; /* path name */
  125. char *rsa; /* path name */
  126. bool authed; /* the connection has been authenticated fine */
  127. bool acceptfail; /* used by the SFTP_QUOTE (continue if
  128. quote command fails) */
  129. sshstate state; /* always use ssh.c:state() to change state! */
  130. sshstate nextstate; /* the state to goto after stopping */
  131. CURLcode actualcode; /* the actual error code */
  132. struct curl_slist *quote_item; /* for the quote option */
  133. char *quote_path1; /* two generic pointers for the QUOTE stuff */
  134. char *quote_path2;
  135. char *homedir; /* when doing SFTP we figure out home dir in the
  136. connect phase */
  137. char *readdir_line;
  138. /* end of READDIR stuff */
  139. int secondCreateDirs; /* counter use by the code to see if the
  140. second attempt has been made to change
  141. to/create a directory */
  142. int orig_waitfor; /* default READ/WRITE bits wait for */
  143. char *slash_pos; /* used by the SFTP_CREATE_DIRS state */
  144. #if defined(USE_LIBSSH)
  145. char *readdir_linkPath;
  146. size_t readdir_len, readdir_totalLen, readdir_currLen;
  147. /* our variables */
  148. unsigned kbd_state; /* 0 or 1 */
  149. ssh_key privkey;
  150. ssh_key pubkey;
  151. int auth_methods;
  152. ssh_session ssh_session;
  153. ssh_scp scp_session;
  154. sftp_session sftp_session;
  155. sftp_file sftp_file;
  156. sftp_dir sftp_dir;
  157. unsigned sftp_recv_state; /* 0 or 1 */
  158. int sftp_file_index; /* for async read */
  159. sftp_attributes readdir_attrs; /* used by the SFTP readdir actions */
  160. sftp_attributes readdir_link_attrs; /* used by the SFTP readdir actions */
  161. sftp_attributes quote_attrs; /* used by the SFTP_QUOTE state */
  162. const char *readdir_filename; /* points within readdir_attrs */
  163. const char *readdir_longentry;
  164. char *readdir_tmp;
  165. #elif defined(USE_LIBSSH2)
  166. LIBSSH2_SESSION *ssh_session; /* Secure Shell session */
  167. LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */
  168. LIBSSH2_SFTP *sftp_session; /* SFTP handle */
  169. LIBSSH2_SFTP_HANDLE *sftp_handle;
  170. #ifndef CURL_DISABLE_PROXY
  171. /* for HTTPS proxy storage */
  172. Curl_recv *tls_recv;
  173. Curl_send *tls_send;
  174. #endif
  175. #ifdef HAVE_LIBSSH2_AGENT_API
  176. LIBSSH2_AGENT *ssh_agent; /* proxy to ssh-agent/pageant */
  177. struct libssh2_agent_publickey *sshagent_identity,
  178. *sshagent_prev_identity;
  179. #endif
  180. /* note that HAVE_LIBSSH2_KNOWNHOST_API is a define set in the libssh2.h
  181. header */
  182. #ifdef HAVE_LIBSSH2_KNOWNHOST_API
  183. LIBSSH2_KNOWNHOSTS *kh;
  184. #endif
  185. #elif defined(USE_WOLFSSH)
  186. WOLFSSH *ssh_session;
  187. WOLFSSH_CTX *ctx;
  188. word32 handleSz;
  189. byte handle[WOLFSSH_MAX_HANDLE];
  190. curl_off_t offset;
  191. #endif /* USE_LIBSSH */
  192. };
  193. #if defined(USE_LIBSSH)
  194. #define CURL_LIBSSH_VERSION ssh_version(0)
  195. #elif defined(USE_LIBSSH2)
  196. /* Feature detection based on version numbers to better work with
  197. non-configure platforms */
  198. #if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x001000)
  199. # error "SCP/SFTP protocols require libssh2 0.16 or later"
  200. #endif
  201. #if LIBSSH2_VERSION_NUM >= 0x010000
  202. #define HAVE_LIBSSH2_SFTP_SEEK64 1
  203. #endif
  204. #if LIBSSH2_VERSION_NUM >= 0x010100
  205. #define HAVE_LIBSSH2_VERSION 1
  206. #endif
  207. #if LIBSSH2_VERSION_NUM >= 0x010205
  208. #define HAVE_LIBSSH2_INIT 1
  209. #define HAVE_LIBSSH2_EXIT 1
  210. #endif
  211. #if LIBSSH2_VERSION_NUM >= 0x010206
  212. #define HAVE_LIBSSH2_KNOWNHOST_CHECKP 1
  213. #define HAVE_LIBSSH2_SCP_SEND64 1
  214. #endif
  215. #if LIBSSH2_VERSION_NUM >= 0x010208
  216. #define HAVE_LIBSSH2_SESSION_HANDSHAKE 1
  217. #endif
  218. #ifdef HAVE_LIBSSH2_VERSION
  219. /* get it run-time if possible */
  220. #define CURL_LIBSSH2_VERSION libssh2_version(0)
  221. #else
  222. /* use build-time if run-time not possible */
  223. #define CURL_LIBSSH2_VERSION LIBSSH2_VERSION
  224. #endif
  225. #endif /* USE_LIBSSH2 */
  226. #ifdef USE_SSH
  227. extern const struct Curl_handler Curl_handler_scp;
  228. extern const struct Curl_handler Curl_handler_sftp;
  229. /* generic SSH backend functions */
  230. CURLcode Curl_ssh_init(void);
  231. void Curl_ssh_cleanup(void);
  232. size_t Curl_ssh_version(char *buffer, size_t buflen);
  233. #else
  234. /* for non-SSH builds */
  235. #define Curl_ssh_cleanup()
  236. #endif
  237. #endif /* HEADER_CURL_SSH_H */