libssh2.h 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /* Copyright (c) 2004-2009, Sara Golemon <sarag@libssh2.org>
  2. * Copyright (c) 2009-2015 Daniel Stenberg
  3. * Copyright (c) 2010 Simon Josefsson <simon@josefsson.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms,
  7. * with or without modification, are permitted provided
  8. * that the following conditions are met:
  9. *
  10. * Redistributions of source code must retain the above
  11. * copyright notice, this list of conditions and the
  12. * following disclaimer.
  13. *
  14. * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials
  17. * provided with the distribution.
  18. *
  19. * Neither the name of the copyright holder nor the names
  20. * of any other contributors may be used to endorse or
  21. * promote products derived from this software without
  22. * specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  25. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  26. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  34. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  37. * OF SUCH DAMAGE.
  38. */
  39. #ifndef LIBSSH2_H
  40. #define LIBSSH2_H 1
  41. #define LIBSSH2_COPYRIGHT "2004-2019 The libssh2 project and its contributors."
  42. /* We use underscore instead of dash when appending DEV in dev versions just
  43. to make the BANNER define (used by src/session.c) be a valid SSH
  44. banner. Release versions have no appended strings and may of course not
  45. have dashes either. */
  46. #define LIBSSH2_VERSION "1.9.0_DEV"
  47. /* The numeric version number is also available "in parts" by using these
  48. defines: */
  49. #define LIBSSH2_VERSION_MAJOR 1
  50. #define LIBSSH2_VERSION_MINOR 9
  51. #define LIBSSH2_VERSION_PATCH 0
  52. /* This is the numeric version of the libssh2 version number, meant for easier
  53. parsing and comparions by programs. The LIBSSH2_VERSION_NUM define will
  54. always follow this syntax:
  55. 0xXXYYZZ
  56. Where XX, YY and ZZ are the main version, release and patch numbers in
  57. hexadecimal (using 8 bits each). All three numbers are always represented
  58. using two digits. 1.2 would appear as "0x010200" while version 9.11.7
  59. appears as "0x090b07".
  60. This 6-digit (24 bits) hexadecimal number does not show pre-release number,
  61. and it is always a greater number in a more recent release. It makes
  62. comparisons with greater than and less than work.
  63. */
  64. #define LIBSSH2_VERSION_NUM 0x010900
  65. /*
  66. * This is the date and time when the full source package was created. The
  67. * timestamp is not stored in the source code repo, as the timestamp is
  68. * properly set in the tarballs by the maketgz script.
  69. *
  70. * The format of the date should follow this template:
  71. *
  72. * "Mon Feb 12 11:35:33 UTC 2007"
  73. */
  74. #define LIBSSH2_TIMESTAMP "DEV"
  75. #ifndef RC_INVOKED
  76. #ifdef __cplusplus
  77. extern "C" {
  78. #endif
  79. #ifdef _WIN32
  80. # include <basetsd.h>
  81. # include <winsock2.h>
  82. #endif
  83. #include <stddef.h>
  84. #include <string.h>
  85. #include <sys/stat.h>
  86. #include <sys/types.h>
  87. /* Allow alternate API prefix from CFLAGS or calling app */
  88. #ifndef LIBSSH2_API
  89. # ifdef LIBSSH2_WIN32
  90. # ifdef _WINDLL
  91. # ifdef LIBSSH2_LIBRARY
  92. # define LIBSSH2_API __declspec(dllexport)
  93. # else
  94. # define LIBSSH2_API __declspec(dllimport)
  95. # endif /* LIBSSH2_LIBRARY */
  96. # else
  97. # define LIBSSH2_API
  98. # endif
  99. # else /* !LIBSSH2_WIN32 */
  100. # define LIBSSH2_API
  101. # endif /* LIBSSH2_WIN32 */
  102. #endif /* LIBSSH2_API */
  103. #ifdef HAVE_SYS_UIO_H
  104. # include <sys/uio.h>
  105. #endif
  106. #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
  107. # include <sys/bsdskt.h>
  108. typedef unsigned char uint8_t;
  109. typedef unsigned short int uint16_t;
  110. typedef unsigned int uint32_t;
  111. typedef int int32_t;
  112. typedef unsigned long long uint64_t;
  113. typedef long long int64_t;
  114. #endif
  115. #ifdef _MSC_VER
  116. typedef unsigned char uint8_t;
  117. typedef unsigned short int uint16_t;
  118. typedef unsigned int uint32_t;
  119. typedef __int32 int32_t;
  120. typedef __int64 int64_t;
  121. typedef unsigned __int64 uint64_t;
  122. typedef unsigned __int64 libssh2_uint64_t;
  123. typedef __int64 libssh2_int64_t;
  124. #if (!defined(HAVE_SSIZE_T) && !defined(ssize_t))
  125. typedef SSIZE_T ssize_t;
  126. #define HAVE_SSIZE_T
  127. #endif
  128. #else
  129. #include <stdint.h>
  130. typedef unsigned long long libssh2_uint64_t;
  131. typedef long long libssh2_int64_t;
  132. #endif
  133. #ifdef WIN32
  134. typedef SOCKET libssh2_socket_t;
  135. #define LIBSSH2_INVALID_SOCKET INVALID_SOCKET
  136. #else /* !WIN32 */
  137. typedef int libssh2_socket_t;
  138. #define LIBSSH2_INVALID_SOCKET -1
  139. #endif /* WIN32 */
  140. /*
  141. * Determine whether there is small or large file support on windows.
  142. */
  143. #if defined(_MSC_VER) && !defined(_WIN32_WCE)
  144. # if (_MSC_VER >= 900) && (_INTEGRAL_MAX_BITS >= 64)
  145. # define LIBSSH2_USE_WIN32_LARGE_FILES
  146. # else
  147. # define LIBSSH2_USE_WIN32_SMALL_FILES
  148. # endif
  149. #endif
  150. #if defined(__MINGW32__) && !defined(LIBSSH2_USE_WIN32_LARGE_FILES)
  151. # define LIBSSH2_USE_WIN32_LARGE_FILES
  152. #endif
  153. #if defined(__WATCOMC__) && !defined(LIBSSH2_USE_WIN32_LARGE_FILES)
  154. # define LIBSSH2_USE_WIN32_LARGE_FILES
  155. #endif
  156. #if defined(__POCC__)
  157. # undef LIBSSH2_USE_WIN32_LARGE_FILES
  158. #endif
  159. #if defined(_WIN32) && !defined(LIBSSH2_USE_WIN32_LARGE_FILES) && \
  160. !defined(LIBSSH2_USE_WIN32_SMALL_FILES)
  161. # define LIBSSH2_USE_WIN32_SMALL_FILES
  162. #endif
  163. /*
  164. * Large file (>2Gb) support using WIN32 functions.
  165. */
  166. #ifdef LIBSSH2_USE_WIN32_LARGE_FILES
  167. # include <io.h>
  168. # include <sys/types.h>
  169. # include <sys/stat.h>
  170. # define LIBSSH2_STRUCT_STAT_SIZE_FORMAT "%I64d"
  171. typedef struct _stati64 libssh2_struct_stat;
  172. typedef __int64 libssh2_struct_stat_size;
  173. #endif
  174. /*
  175. * Small file (<2Gb) support using WIN32 functions.
  176. */
  177. #ifdef LIBSSH2_USE_WIN32_SMALL_FILES
  178. # include <sys/types.h>
  179. # include <sys/stat.h>
  180. # ifndef _WIN32_WCE
  181. # define LIBSSH2_STRUCT_STAT_SIZE_FORMAT "%d"
  182. typedef struct _stat libssh2_struct_stat;
  183. typedef off_t libssh2_struct_stat_size;
  184. # endif
  185. #endif
  186. #ifndef LIBSSH2_STRUCT_STAT_SIZE_FORMAT
  187. # ifdef __VMS
  188. /* We have to roll our own format here because %z is a C99-ism we don't
  189. have. */
  190. # if __USE_OFF64_T || __USING_STD_STAT
  191. # define LIBSSH2_STRUCT_STAT_SIZE_FORMAT "%Ld"
  192. # else
  193. # define LIBSSH2_STRUCT_STAT_SIZE_FORMAT "%d"
  194. # endif
  195. # else
  196. # define LIBSSH2_STRUCT_STAT_SIZE_FORMAT "%zd"
  197. # endif
  198. typedef struct stat libssh2_struct_stat;
  199. typedef off_t libssh2_struct_stat_size;
  200. #endif
  201. /* Part of every banner, user specified or not */
  202. #define LIBSSH2_SSH_BANNER "SSH-2.0-libssh2_" LIBSSH2_VERSION
  203. #define LIBSSH2_SSH_DEFAULT_BANNER LIBSSH2_SSH_BANNER
  204. #define LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF LIBSSH2_SSH_DEFAULT_BANNER "\r\n"
  205. /* Default generate and safe prime sizes for
  206. diffie-hellman-group-exchange-sha1 */
  207. #define LIBSSH2_DH_GEX_MINGROUP 1024
  208. #define LIBSSH2_DH_GEX_OPTGROUP 1536
  209. #define LIBSSH2_DH_GEX_MAXGROUP 2048
  210. /* Defaults for pty requests */
  211. #define LIBSSH2_TERM_WIDTH 80
  212. #define LIBSSH2_TERM_HEIGHT 24
  213. #define LIBSSH2_TERM_WIDTH_PX 0
  214. #define LIBSSH2_TERM_HEIGHT_PX 0
  215. /* 1/4 second */
  216. #define LIBSSH2_SOCKET_POLL_UDELAY 250000
  217. /* 0.25 * 120 == 30 seconds */
  218. #define LIBSSH2_SOCKET_POLL_MAXLOOPS 120
  219. /* Maximum size to allow a payload to compress to, plays it safe by falling
  220. short of spec limits */
  221. #define LIBSSH2_PACKET_MAXCOMP 32000
  222. /* Maximum size to allow a payload to deccompress to, plays it safe by
  223. allowing more than spec requires */
  224. #define LIBSSH2_PACKET_MAXDECOMP 40000
  225. /* Maximum size for an inbound compressed payload, plays it safe by
  226. overshooting spec limits */
  227. #define LIBSSH2_PACKET_MAXPAYLOAD 40000
  228. /* Malloc callbacks */
  229. #define LIBSSH2_ALLOC_FUNC(name) void *name(size_t count, void **abstract)
  230. #define LIBSSH2_REALLOC_FUNC(name) void *name(void *ptr, size_t count, \
  231. void **abstract)
  232. #define LIBSSH2_FREE_FUNC(name) void name(void *ptr, void **abstract)
  233. typedef struct _LIBSSH2_USERAUTH_KBDINT_PROMPT
  234. {
  235. char *text;
  236. unsigned int length;
  237. unsigned char echo;
  238. } LIBSSH2_USERAUTH_KBDINT_PROMPT;
  239. typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE
  240. {
  241. char *text;
  242. unsigned int length;
  243. } LIBSSH2_USERAUTH_KBDINT_RESPONSE;
  244. /* 'publickey' authentication callback */
  245. #define LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC(name) \
  246. int name(LIBSSH2_SESSION *session, unsigned char **sig, size_t *sig_len, \
  247. const unsigned char *data, size_t data_len, void **abstract)
  248. /* 'keyboard-interactive' authentication callback */
  249. #define LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC(name_) \
  250. void name_(const char *name, int name_len, const char *instruction, \
  251. int instruction_len, int num_prompts, \
  252. const LIBSSH2_USERAUTH_KBDINT_PROMPT *prompts, \
  253. LIBSSH2_USERAUTH_KBDINT_RESPONSE *responses, void **abstract)
  254. /* Callbacks for special SSH packets */
  255. #define LIBSSH2_IGNORE_FUNC(name) \
  256. void name(LIBSSH2_SESSION *session, const char *message, int message_len, \
  257. void **abstract)
  258. #define LIBSSH2_DEBUG_FUNC(name) \
  259. void name(LIBSSH2_SESSION *session, int always_display, const char *message, \
  260. int message_len, const char *language, int language_len, \
  261. void **abstract)
  262. #define LIBSSH2_DISCONNECT_FUNC(name) \
  263. void name(LIBSSH2_SESSION *session, int reason, const char *message, \
  264. int message_len, const char *language, int language_len, \
  265. void **abstract)
  266. #define LIBSSH2_PASSWD_CHANGEREQ_FUNC(name) \
  267. void name(LIBSSH2_SESSION *session, char **newpw, int *newpw_len, \
  268. void **abstract)
  269. #define LIBSSH2_MACERROR_FUNC(name) \
  270. int name(LIBSSH2_SESSION *session, const char *packet, int packet_len, \
  271. void **abstract)
  272. #define LIBSSH2_X11_OPEN_FUNC(name) \
  273. void name(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, \
  274. const char *shost, int sport, void **abstract)
  275. #define LIBSSH2_CHANNEL_CLOSE_FUNC(name) \
  276. void name(LIBSSH2_SESSION *session, void **session_abstract, \
  277. LIBSSH2_CHANNEL *channel, void **channel_abstract)
  278. /* I/O callbacks */
  279. #define LIBSSH2_RECV_FUNC(name) \
  280. ssize_t name(libssh2_socket_t socket, \
  281. void *buffer, size_t length, \
  282. int flags, void **abstract)
  283. #define LIBSSH2_SEND_FUNC(name) \
  284. ssize_t name(libssh2_socket_t socket, \
  285. const void *buffer, size_t length, \
  286. int flags, void **abstract)
  287. /* libssh2_session_callback_set() constants */
  288. #define LIBSSH2_CALLBACK_IGNORE 0
  289. #define LIBSSH2_CALLBACK_DEBUG 1
  290. #define LIBSSH2_CALLBACK_DISCONNECT 2
  291. #define LIBSSH2_CALLBACK_MACERROR 3
  292. #define LIBSSH2_CALLBACK_X11 4
  293. #define LIBSSH2_CALLBACK_SEND 5
  294. #define LIBSSH2_CALLBACK_RECV 6
  295. /* libssh2_session_method_pref() constants */
  296. #define LIBSSH2_METHOD_KEX 0
  297. #define LIBSSH2_METHOD_HOSTKEY 1
  298. #define LIBSSH2_METHOD_CRYPT_CS 2
  299. #define LIBSSH2_METHOD_CRYPT_SC 3
  300. #define LIBSSH2_METHOD_MAC_CS 4
  301. #define LIBSSH2_METHOD_MAC_SC 5
  302. #define LIBSSH2_METHOD_COMP_CS 6
  303. #define LIBSSH2_METHOD_COMP_SC 7
  304. #define LIBSSH2_METHOD_LANG_CS 8
  305. #define LIBSSH2_METHOD_LANG_SC 9
  306. /* flags */
  307. #define LIBSSH2_FLAG_SIGPIPE 1
  308. #define LIBSSH2_FLAG_COMPRESS 2
  309. typedef struct _LIBSSH2_SESSION LIBSSH2_SESSION;
  310. typedef struct _LIBSSH2_CHANNEL LIBSSH2_CHANNEL;
  311. typedef struct _LIBSSH2_LISTENER LIBSSH2_LISTENER;
  312. typedef struct _LIBSSH2_KNOWNHOSTS LIBSSH2_KNOWNHOSTS;
  313. typedef struct _LIBSSH2_AGENT LIBSSH2_AGENT;
  314. typedef struct _LIBSSH2_POLLFD {
  315. unsigned char type; /* LIBSSH2_POLLFD_* below */
  316. union {
  317. libssh2_socket_t socket; /* File descriptors -- examined with
  318. system select() call */
  319. LIBSSH2_CHANNEL *channel; /* Examined by checking internal state */
  320. LIBSSH2_LISTENER *listener; /* Read polls only -- are inbound
  321. connections waiting to be accepted? */
  322. } fd;
  323. unsigned long events; /* Requested Events */
  324. unsigned long revents; /* Returned Events */
  325. } LIBSSH2_POLLFD;
  326. /* Poll FD Descriptor Types */
  327. #define LIBSSH2_POLLFD_SOCKET 1
  328. #define LIBSSH2_POLLFD_CHANNEL 2
  329. #define LIBSSH2_POLLFD_LISTENER 3
  330. /* Note: Win32 Doesn't actually have a poll() implementation, so some of these
  331. values are faked with select() data */
  332. /* Poll FD events/revents -- Match sys/poll.h where possible */
  333. #define LIBSSH2_POLLFD_POLLIN 0x0001 /* Data available to be read or
  334. connection available --
  335. All */
  336. #define LIBSSH2_POLLFD_POLLPRI 0x0002 /* Priority data available to
  337. be read -- Socket only */
  338. #define LIBSSH2_POLLFD_POLLEXT 0x0002 /* Extended data available to
  339. be read -- Channel only */
  340. #define LIBSSH2_POLLFD_POLLOUT 0x0004 /* Can may be written --
  341. Socket/Channel */
  342. /* revents only */
  343. #define LIBSSH2_POLLFD_POLLERR 0x0008 /* Error Condition -- Socket */
  344. #define LIBSSH2_POLLFD_POLLHUP 0x0010 /* HangUp/EOF -- Socket */
  345. #define LIBSSH2_POLLFD_SESSION_CLOSED 0x0010 /* Session Disconnect */
  346. #define LIBSSH2_POLLFD_POLLNVAL 0x0020 /* Invalid request -- Socket
  347. Only */
  348. #define LIBSSH2_POLLFD_POLLEX 0x0040 /* Exception Condition --
  349. Socket/Win32 */
  350. #define LIBSSH2_POLLFD_CHANNEL_CLOSED 0x0080 /* Channel Disconnect */
  351. #define LIBSSH2_POLLFD_LISTENER_CLOSED 0x0080 /* Listener Disconnect */
  352. #define HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
  353. /* Block Direction Types */
  354. #define LIBSSH2_SESSION_BLOCK_INBOUND 0x0001
  355. #define LIBSSH2_SESSION_BLOCK_OUTBOUND 0x0002
  356. /* Hash Types */
  357. #define LIBSSH2_HOSTKEY_HASH_MD5 1
  358. #define LIBSSH2_HOSTKEY_HASH_SHA1 2
  359. #define LIBSSH2_HOSTKEY_HASH_SHA256 3
  360. /* Hostkey Types */
  361. #define LIBSSH2_HOSTKEY_TYPE_UNKNOWN 0
  362. #define LIBSSH2_HOSTKEY_TYPE_RSA 1
  363. #define LIBSSH2_HOSTKEY_TYPE_DSS 2
  364. #define LIBSSH2_HOSTKEY_TYPE_ECDSA_256 3
  365. #define LIBSSH2_HOSTKEY_TYPE_ECDSA_384 4
  366. #define LIBSSH2_HOSTKEY_TYPE_ECDSA_521 5
  367. #define LIBSSH2_HOSTKEY_TYPE_ED25519 6
  368. /* Disconnect Codes (defined by SSH protocol) */
  369. #define SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1
  370. #define SSH_DISCONNECT_PROTOCOL_ERROR 2
  371. #define SSH_DISCONNECT_KEY_EXCHANGE_FAILED 3
  372. #define SSH_DISCONNECT_RESERVED 4
  373. #define SSH_DISCONNECT_MAC_ERROR 5
  374. #define SSH_DISCONNECT_COMPRESSION_ERROR 6
  375. #define SSH_DISCONNECT_SERVICE_NOT_AVAILABLE 7
  376. #define SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8
  377. #define SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9
  378. #define SSH_DISCONNECT_CONNECTION_LOST 10
  379. #define SSH_DISCONNECT_BY_APPLICATION 11
  380. #define SSH_DISCONNECT_TOO_MANY_CONNECTIONS 12
  381. #define SSH_DISCONNECT_AUTH_CANCELLED_BY_USER 13
  382. #define SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14
  383. #define SSH_DISCONNECT_ILLEGAL_USER_NAME 15
  384. /* Error Codes (defined by libssh2) */
  385. #define LIBSSH2_ERROR_NONE 0
  386. /* The library once used -1 as a generic error return value on numerous places
  387. through the code, which subsequently was converted to
  388. LIBSSH2_ERROR_SOCKET_NONE uses over time. As this is a generic error code,
  389. the goal is to never ever return this code but instead make sure that a
  390. more accurate and descriptive error code is used. */
  391. #define LIBSSH2_ERROR_SOCKET_NONE -1
  392. #define LIBSSH2_ERROR_BANNER_RECV -2
  393. #define LIBSSH2_ERROR_BANNER_SEND -3
  394. #define LIBSSH2_ERROR_INVALID_MAC -4
  395. #define LIBSSH2_ERROR_KEX_FAILURE -5
  396. #define LIBSSH2_ERROR_ALLOC -6
  397. #define LIBSSH2_ERROR_SOCKET_SEND -7
  398. #define LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE -8
  399. #define LIBSSH2_ERROR_TIMEOUT -9
  400. #define LIBSSH2_ERROR_HOSTKEY_INIT -10
  401. #define LIBSSH2_ERROR_HOSTKEY_SIGN -11
  402. #define LIBSSH2_ERROR_DECRYPT -12
  403. #define LIBSSH2_ERROR_SOCKET_DISCONNECT -13
  404. #define LIBSSH2_ERROR_PROTO -14
  405. #define LIBSSH2_ERROR_PASSWORD_EXPIRED -15
  406. #define LIBSSH2_ERROR_FILE -16
  407. #define LIBSSH2_ERROR_METHOD_NONE -17
  408. #define LIBSSH2_ERROR_AUTHENTICATION_FAILED -18
  409. #define LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED \
  410. LIBSSH2_ERROR_AUTHENTICATION_FAILED
  411. #define LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED -19
  412. #define LIBSSH2_ERROR_CHANNEL_OUTOFORDER -20
  413. #define LIBSSH2_ERROR_CHANNEL_FAILURE -21
  414. #define LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED -22
  415. #define LIBSSH2_ERROR_CHANNEL_UNKNOWN -23
  416. #define LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED -24
  417. #define LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED -25
  418. #define LIBSSH2_ERROR_CHANNEL_CLOSED -26
  419. #define LIBSSH2_ERROR_CHANNEL_EOF_SENT -27
  420. #define LIBSSH2_ERROR_SCP_PROTOCOL -28
  421. #define LIBSSH2_ERROR_ZLIB -29
  422. #define LIBSSH2_ERROR_SOCKET_TIMEOUT -30
  423. #define LIBSSH2_ERROR_SFTP_PROTOCOL -31
  424. #define LIBSSH2_ERROR_REQUEST_DENIED -32
  425. #define LIBSSH2_ERROR_METHOD_NOT_SUPPORTED -33
  426. #define LIBSSH2_ERROR_INVAL -34
  427. #define LIBSSH2_ERROR_INVALID_POLL_TYPE -35
  428. #define LIBSSH2_ERROR_PUBLICKEY_PROTOCOL -36
  429. #define LIBSSH2_ERROR_EAGAIN -37
  430. #define LIBSSH2_ERROR_BUFFER_TOO_SMALL -38
  431. #define LIBSSH2_ERROR_BAD_USE -39
  432. #define LIBSSH2_ERROR_COMPRESS -40
  433. #define LIBSSH2_ERROR_OUT_OF_BOUNDARY -41
  434. #define LIBSSH2_ERROR_AGENT_PROTOCOL -42
  435. #define LIBSSH2_ERROR_SOCKET_RECV -43
  436. #define LIBSSH2_ERROR_ENCRYPT -44
  437. #define LIBSSH2_ERROR_BAD_SOCKET -45
  438. #define LIBSSH2_ERROR_KNOWN_HOSTS -46
  439. #define LIBSSH2_ERROR_CHANNEL_WINDOW_FULL -47
  440. #define LIBSSH2_ERROR_KEYFILE_AUTH_FAILED -48
  441. /* this is a define to provide the old (<= 1.2.7) name */
  442. #define LIBSSH2_ERROR_BANNER_NONE LIBSSH2_ERROR_BANNER_RECV
  443. /* Global API */
  444. #define LIBSSH2_INIT_NO_CRYPTO 0x0001
  445. /*
  446. * libssh2_init()
  447. *
  448. * Initialize the libssh2 functions. This typically initialize the
  449. * crypto library. It uses a global state, and is not thread safe --
  450. * you must make sure this function is not called concurrently.
  451. *
  452. * Flags can be:
  453. * 0: Normal initialize
  454. * LIBSSH2_INIT_NO_CRYPTO: Do not initialize the crypto library (ie.
  455. * OPENSSL_add_cipher_algoritms() for OpenSSL
  456. *
  457. * Returns 0 if succeeded, or a negative value for error.
  458. */
  459. LIBSSH2_API int libssh2_init(int flags);
  460. /*
  461. * libssh2_exit()
  462. *
  463. * Exit the libssh2 functions and free's all memory used internal.
  464. */
  465. LIBSSH2_API void libssh2_exit(void);
  466. /*
  467. * libssh2_free()
  468. *
  469. * Deallocate memory allocated by earlier call to libssh2 functions.
  470. */
  471. LIBSSH2_API void libssh2_free(LIBSSH2_SESSION *session, void *ptr);
  472. /*
  473. * libssh2_session_supported_algs()
  474. *
  475. * Fills algs with a list of supported acryptographic algorithms. Returns a
  476. * non-negative number (number of supported algorithms) on success or a
  477. * negative number (an eror code) on failure.
  478. *
  479. * NOTE: on success, algs must be deallocated (by calling libssh2_free) when
  480. * not needed anymore
  481. */
  482. LIBSSH2_API int libssh2_session_supported_algs(LIBSSH2_SESSION* session,
  483. int method_type,
  484. const char ***algs);
  485. /* Session API */
  486. LIBSSH2_API LIBSSH2_SESSION *
  487. libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC((*my_alloc)),
  488. LIBSSH2_FREE_FUNC((*my_free)),
  489. LIBSSH2_REALLOC_FUNC((*my_realloc)), void *abstract);
  490. #define libssh2_session_init() libssh2_session_init_ex(NULL, NULL, NULL, NULL)
  491. LIBSSH2_API void **libssh2_session_abstract(LIBSSH2_SESSION *session);
  492. LIBSSH2_API void *libssh2_session_callback_set(LIBSSH2_SESSION *session,
  493. int cbtype, void *callback);
  494. LIBSSH2_API int libssh2_session_banner_set(LIBSSH2_SESSION *session,
  495. const char *banner);
  496. LIBSSH2_API int libssh2_banner_set(LIBSSH2_SESSION *session,
  497. const char *banner);
  498. LIBSSH2_API int libssh2_session_startup(LIBSSH2_SESSION *session, int sock);
  499. LIBSSH2_API int libssh2_session_handshake(LIBSSH2_SESSION *session,
  500. libssh2_socket_t sock);
  501. LIBSSH2_API int libssh2_session_disconnect_ex(LIBSSH2_SESSION *session,
  502. int reason,
  503. const char *description,
  504. const char *lang);
  505. #define libssh2_session_disconnect(session, description) \
  506. libssh2_session_disconnect_ex((session), SSH_DISCONNECT_BY_APPLICATION, \
  507. (description), "")
  508. LIBSSH2_API int libssh2_session_free(LIBSSH2_SESSION *session);
  509. LIBSSH2_API const char *libssh2_hostkey_hash(LIBSSH2_SESSION *session,
  510. int hash_type);
  511. LIBSSH2_API const char *libssh2_session_hostkey(LIBSSH2_SESSION *session,
  512. size_t *len, int *type);
  513. LIBSSH2_API int libssh2_session_method_pref(LIBSSH2_SESSION *session,
  514. int method_type,
  515. const char *prefs);
  516. LIBSSH2_API const char *libssh2_session_methods(LIBSSH2_SESSION *session,
  517. int method_type);
  518. LIBSSH2_API int libssh2_session_last_error(LIBSSH2_SESSION *session,
  519. char **errmsg,
  520. int *errmsg_len, int want_buf);
  521. LIBSSH2_API int libssh2_session_last_errno(LIBSSH2_SESSION *session);
  522. LIBSSH2_API int libssh2_session_set_last_error(LIBSSH2_SESSION* session,
  523. int errcode,
  524. const char *errmsg);
  525. LIBSSH2_API int libssh2_session_block_directions(LIBSSH2_SESSION *session);
  526. LIBSSH2_API int libssh2_session_flag(LIBSSH2_SESSION *session, int flag,
  527. int value);
  528. LIBSSH2_API const char *libssh2_session_banner_get(LIBSSH2_SESSION *session);
  529. /* Userauth API */
  530. LIBSSH2_API char *libssh2_userauth_list(LIBSSH2_SESSION *session,
  531. const char *username,
  532. unsigned int username_len);
  533. LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session);
  534. LIBSSH2_API int
  535. libssh2_userauth_password_ex(LIBSSH2_SESSION *session,
  536. const char *username,
  537. unsigned int username_len,
  538. const char *password,
  539. unsigned int password_len,
  540. LIBSSH2_PASSWD_CHANGEREQ_FUNC
  541. ((*passwd_change_cb)));
  542. #define libssh2_userauth_password(session, username, password) \
  543. libssh2_userauth_password_ex((session), (username), \
  544. (unsigned int)strlen(username), \
  545. (password), (unsigned int)strlen(password), NULL)
  546. LIBSSH2_API int
  547. libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session,
  548. const char *username,
  549. unsigned int username_len,
  550. const char *publickey,
  551. const char *privatekey,
  552. const char *passphrase);
  553. #define libssh2_userauth_publickey_fromfile(session, username, publickey, \
  554. privatekey, passphrase) \
  555. libssh2_userauth_publickey_fromfile_ex((session), (username), \
  556. (unsigned int)strlen(username), \
  557. (publickey), \
  558. (privatekey), (passphrase))
  559. LIBSSH2_API int
  560. libssh2_userauth_publickey(LIBSSH2_SESSION *session,
  561. const char *username,
  562. const unsigned char *pubkeydata,
  563. size_t pubkeydata_len,
  564. LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC
  565. ((*sign_callback)),
  566. void **abstract);
  567. LIBSSH2_API int
  568. libssh2_userauth_hostbased_fromfile_ex(LIBSSH2_SESSION *session,
  569. const char *username,
  570. unsigned int username_len,
  571. const char *publickey,
  572. const char *privatekey,
  573. const char *passphrase,
  574. const char *hostname,
  575. unsigned int hostname_len,
  576. const char *local_username,
  577. unsigned int local_username_len);
  578. #define libssh2_userauth_hostbased_fromfile(session, username, publickey, \
  579. privatekey, passphrase, hostname) \
  580. libssh2_userauth_hostbased_fromfile_ex((session), (username), \
  581. (unsigned int)strlen(username), \
  582. (publickey), \
  583. (privatekey), (passphrase), \
  584. (hostname), \
  585. (unsigned int)strlen(hostname), \
  586. (username), \
  587. (unsigned int)strlen(username))
  588. LIBSSH2_API int
  589. libssh2_userauth_publickey_frommemory(LIBSSH2_SESSION *session,
  590. const char *username,
  591. size_t username_len,
  592. const char *publickeyfiledata,
  593. size_t publickeyfiledata_len,
  594. const char *privatekeyfiledata,
  595. size_t privatekeyfiledata_len,
  596. const char *passphrase);
  597. /*
  598. * response_callback is provided with filled by library prompts array,
  599. * but client must allocate and fill individual responses. Responses
  600. * array is already allocated. Responses data will be freed by libssh2
  601. * after callback return, but before subsequent callback invokation.
  602. */
  603. LIBSSH2_API int
  604. libssh2_userauth_keyboard_interactive_ex(LIBSSH2_SESSION* session,
  605. const char *username,
  606. unsigned int username_len,
  607. LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC(
  608. (*response_callback)));
  609. #define libssh2_userauth_keyboard_interactive(session, username, \
  610. response_callback) \
  611. libssh2_userauth_keyboard_interactive_ex((session), (username), \
  612. (unsigned int)strlen(username), \
  613. (response_callback))
  614. LIBSSH2_API int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds,
  615. long timeout);
  616. /* Channel API */
  617. #define LIBSSH2_CHANNEL_WINDOW_DEFAULT (2*1024*1024)
  618. #define LIBSSH2_CHANNEL_PACKET_DEFAULT 32768
  619. #define LIBSSH2_CHANNEL_MINADJUST 1024
  620. /* Extended Data Handling */
  621. #define LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL 0
  622. #define LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE 1
  623. #define LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE 2
  624. #define SSH_EXTENDED_DATA_STDERR 1
  625. /* Returned by any function that would block during a read/write opperation */
  626. #define LIBSSH2CHANNEL_EAGAIN LIBSSH2_ERROR_EAGAIN
  627. LIBSSH2_API LIBSSH2_CHANNEL *
  628. libssh2_channel_open_ex(LIBSSH2_SESSION *session, const char *channel_type,
  629. unsigned int channel_type_len,
  630. unsigned int window_size, unsigned int packet_size,
  631. const char *message, unsigned int message_len);
  632. #define libssh2_channel_open_session(session) \
  633. libssh2_channel_open_ex((session), "session", sizeof("session") - 1, \
  634. LIBSSH2_CHANNEL_WINDOW_DEFAULT, \
  635. LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0)
  636. LIBSSH2_API LIBSSH2_CHANNEL *
  637. libssh2_channel_direct_tcpip_ex(LIBSSH2_SESSION *session, const char *host,
  638. int port, const char *shost, int sport);
  639. #define libssh2_channel_direct_tcpip(session, host, port) \
  640. libssh2_channel_direct_tcpip_ex((session), (host), (port), "127.0.0.1", 22)
  641. LIBSSH2_API LIBSSH2_LISTENER *
  642. libssh2_channel_forward_listen_ex(LIBSSH2_SESSION *session, const char *host,
  643. int port, int *bound_port,
  644. int queue_maxsize);
  645. #define libssh2_channel_forward_listen(session, port) \
  646. libssh2_channel_forward_listen_ex((session), NULL, (port), NULL, 16)
  647. LIBSSH2_API int libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener);
  648. LIBSSH2_API LIBSSH2_CHANNEL *
  649. libssh2_channel_forward_accept(LIBSSH2_LISTENER *listener);
  650. LIBSSH2_API int libssh2_channel_setenv_ex(LIBSSH2_CHANNEL *channel,
  651. const char *varname,
  652. unsigned int varname_len,
  653. const char *value,
  654. unsigned int value_len);
  655. #define libssh2_channel_setenv(channel, varname, value) \
  656. libssh2_channel_setenv_ex((channel), (varname), \
  657. (unsigned int)strlen(varname), (value), \
  658. (unsigned int)strlen(value))
  659. LIBSSH2_API int libssh2_channel_request_pty_ex(LIBSSH2_CHANNEL *channel,
  660. const char *term,
  661. unsigned int term_len,
  662. const char *modes,
  663. unsigned int modes_len,
  664. int width, int height,
  665. int width_px, int height_px);
  666. #define libssh2_channel_request_pty(channel, term) \
  667. libssh2_channel_request_pty_ex((channel), (term), \
  668. (unsigned int)strlen(term), \
  669. NULL, 0, \
  670. LIBSSH2_TERM_WIDTH, \
  671. LIBSSH2_TERM_HEIGHT, \
  672. LIBSSH2_TERM_WIDTH_PX, \
  673. LIBSSH2_TERM_HEIGHT_PX)
  674. LIBSSH2_API int libssh2_channel_request_pty_size_ex(LIBSSH2_CHANNEL *channel,
  675. int width, int height,
  676. int width_px,
  677. int height_px);
  678. #define libssh2_channel_request_pty_size(channel, width, height) \
  679. libssh2_channel_request_pty_size_ex((channel), (width), (height), 0, 0)
  680. LIBSSH2_API int libssh2_channel_x11_req_ex(LIBSSH2_CHANNEL *channel,
  681. int single_connection,
  682. const char *auth_proto,
  683. const char *auth_cookie,
  684. int screen_number);
  685. #define libssh2_channel_x11_req(channel, screen_number) \
  686. libssh2_channel_x11_req_ex((channel), 0, NULL, NULL, (screen_number))
  687. LIBSSH2_API int libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
  688. const char *request,
  689. unsigned int request_len,
  690. const char *message,
  691. unsigned int message_len);
  692. #define libssh2_channel_shell(channel) \
  693. libssh2_channel_process_startup((channel), "shell", sizeof("shell") - 1, \
  694. NULL, 0)
  695. #define libssh2_channel_exec(channel, command) \
  696. libssh2_channel_process_startup((channel), "exec", sizeof("exec") - 1, \
  697. (command), (unsigned int)strlen(command))
  698. #define libssh2_channel_subsystem(channel, subsystem) \
  699. libssh2_channel_process_startup((channel), "subsystem", \
  700. sizeof("subsystem") - 1, (subsystem), \
  701. (unsigned int)strlen(subsystem))
  702. LIBSSH2_API ssize_t libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel,
  703. int stream_id, char *buf,
  704. size_t buflen);
  705. #define libssh2_channel_read(channel, buf, buflen) \
  706. libssh2_channel_read_ex((channel), 0, (buf), (buflen))
  707. #define libssh2_channel_read_stderr(channel, buf, buflen) \
  708. libssh2_channel_read_ex((channel), SSH_EXTENDED_DATA_STDERR, (buf), (buflen))
  709. LIBSSH2_API int libssh2_poll_channel_read(LIBSSH2_CHANNEL *channel,
  710. int extended);
  711. LIBSSH2_API unsigned long
  712. libssh2_channel_window_read_ex(LIBSSH2_CHANNEL *channel,
  713. unsigned long *read_avail,
  714. unsigned long *window_size_initial);
  715. #define libssh2_channel_window_read(channel) \
  716. libssh2_channel_window_read_ex((channel), NULL, NULL)
  717. /* libssh2_channel_receive_window_adjust is DEPRECATED, do not use! */
  718. LIBSSH2_API unsigned long
  719. libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel,
  720. unsigned long adjustment,
  721. unsigned char force);
  722. LIBSSH2_API int
  723. libssh2_channel_receive_window_adjust2(LIBSSH2_CHANNEL *channel,
  724. unsigned long adjustment,
  725. unsigned char force,
  726. unsigned int *storewindow);
  727. LIBSSH2_API ssize_t libssh2_channel_write_ex(LIBSSH2_CHANNEL *channel,
  728. int stream_id, const char *buf,
  729. size_t buflen);
  730. #define libssh2_channel_write(channel, buf, buflen) \
  731. libssh2_channel_write_ex((channel), 0, (buf), (buflen))
  732. #define libssh2_channel_write_stderr(channel, buf, buflen) \
  733. libssh2_channel_write_ex((channel), SSH_EXTENDED_DATA_STDERR, \
  734. (buf), (buflen))
  735. LIBSSH2_API unsigned long
  736. libssh2_channel_window_write_ex(LIBSSH2_CHANNEL *channel,
  737. unsigned long *window_size_initial);
  738. #define libssh2_channel_window_write(channel) \
  739. libssh2_channel_window_write_ex((channel), NULL)
  740. LIBSSH2_API void libssh2_session_set_blocking(LIBSSH2_SESSION* session,
  741. int blocking);
  742. LIBSSH2_API int libssh2_session_get_blocking(LIBSSH2_SESSION* session);
  743. LIBSSH2_API void libssh2_channel_set_blocking(LIBSSH2_CHANNEL *channel,
  744. int blocking);
  745. LIBSSH2_API void libssh2_session_set_timeout(LIBSSH2_SESSION* session,
  746. long timeout);
  747. LIBSSH2_API long libssh2_session_get_timeout(LIBSSH2_SESSION* session);
  748. /* libssh2_channel_handle_extended_data is DEPRECATED, do not use! */
  749. LIBSSH2_API void libssh2_channel_handle_extended_data(LIBSSH2_CHANNEL *channel,
  750. int ignore_mode);
  751. LIBSSH2_API int libssh2_channel_handle_extended_data2(LIBSSH2_CHANNEL *channel,
  752. int ignore_mode);
  753. /* libssh2_channel_ignore_extended_data() is defined below for BC with version
  754. * 0.1
  755. *
  756. * Future uses should use libssh2_channel_handle_extended_data() directly if
  757. * LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE is passed, extended data will be read
  758. * (FIFO) from the standard data channel
  759. */
  760. /* DEPRECATED */
  761. #define libssh2_channel_ignore_extended_data(channel, ignore) \
  762. libssh2_channel_handle_extended_data((channel), \
  763. (ignore) ? \
  764. LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE : \
  765. LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL)
  766. #define LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA -1
  767. #define LIBSSH2_CHANNEL_FLUSH_ALL -2
  768. LIBSSH2_API int libssh2_channel_flush_ex(LIBSSH2_CHANNEL *channel,
  769. int streamid);
  770. #define libssh2_channel_flush(channel) libssh2_channel_flush_ex((channel), 0)
  771. #define libssh2_channel_flush_stderr(channel) \
  772. libssh2_channel_flush_ex((channel), SSH_EXTENDED_DATA_STDERR)
  773. LIBSSH2_API int libssh2_channel_get_exit_status(LIBSSH2_CHANNEL* channel);
  774. LIBSSH2_API int libssh2_channel_get_exit_signal(LIBSSH2_CHANNEL* channel,
  775. char **exitsignal,
  776. size_t *exitsignal_len,
  777. char **errmsg,
  778. size_t *errmsg_len,
  779. char **langtag,
  780. size_t *langtag_len);
  781. LIBSSH2_API int libssh2_channel_send_eof(LIBSSH2_CHANNEL *channel);
  782. LIBSSH2_API int libssh2_channel_eof(LIBSSH2_CHANNEL *channel);
  783. LIBSSH2_API int libssh2_channel_wait_eof(LIBSSH2_CHANNEL *channel);
  784. LIBSSH2_API int libssh2_channel_close(LIBSSH2_CHANNEL *channel);
  785. LIBSSH2_API int libssh2_channel_wait_closed(LIBSSH2_CHANNEL *channel);
  786. LIBSSH2_API int libssh2_channel_free(LIBSSH2_CHANNEL *channel);
  787. /* libssh2_scp_recv is DEPRECATED, do not use! */
  788. LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_recv(LIBSSH2_SESSION *session,
  789. const char *path,
  790. struct stat *sb);
  791. /* Use libssh2_scp_recv2 for large (> 2GB) file support on windows */
  792. LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_recv2(LIBSSH2_SESSION *session,
  793. const char *path,
  794. libssh2_struct_stat *sb);
  795. LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_send_ex(LIBSSH2_SESSION *session,
  796. const char *path, int mode,
  797. size_t size, long mtime,
  798. long atime);
  799. LIBSSH2_API LIBSSH2_CHANNEL *
  800. libssh2_scp_send64(LIBSSH2_SESSION *session, const char *path, int mode,
  801. libssh2_int64_t size, time_t mtime, time_t atime);
  802. #define libssh2_scp_send(session, path, mode, size) \
  803. libssh2_scp_send_ex((session), (path), (mode), (size), 0, 0)
  804. LIBSSH2_API int libssh2_base64_decode(LIBSSH2_SESSION *session, char **dest,
  805. unsigned int *dest_len,
  806. const char *src, unsigned int src_len);
  807. LIBSSH2_API
  808. const char *libssh2_version(int req_version_num);
  809. #define HAVE_LIBSSH2_KNOWNHOST_API 0x010101 /* since 1.1.1 */
  810. #define HAVE_LIBSSH2_VERSION_API 0x010100 /* libssh2_version since 1.1 */
  811. struct libssh2_knownhost {
  812. unsigned int magic; /* magic stored by the library */
  813. void *node; /* handle to the internal representation of this host */
  814. char *name; /* this is NULL if no plain text host name exists */
  815. char *key; /* key in base64/printable format */
  816. int typemask;
  817. };
  818. /*
  819. * libssh2_knownhost_init
  820. *
  821. * Init a collection of known hosts. Returns the pointer to a collection.
  822. *
  823. */
  824. LIBSSH2_API LIBSSH2_KNOWNHOSTS *
  825. libssh2_knownhost_init(LIBSSH2_SESSION *session);
  826. /*
  827. * libssh2_knownhost_add
  828. *
  829. * Add a host and its associated key to the collection of known hosts.
  830. *
  831. * The 'type' argument specifies on what format the given host and keys are:
  832. *
  833. * plain - ascii "hostname.domain.tld"
  834. * sha1 - SHA1(<salt> <host>) base64-encoded!
  835. * custom - another hash
  836. *
  837. * If 'sha1' is selected as type, the salt must be provided to the salt
  838. * argument. This too base64 encoded.
  839. *
  840. * The SHA-1 hash is what OpenSSH can be told to use in known_hosts files. If
  841. * a custom type is used, salt is ignored and you must provide the host
  842. * pre-hashed when checking for it in the libssh2_knownhost_check() function.
  843. *
  844. * The keylen parameter may be omitted (zero) if the key is provided as a
  845. * NULL-terminated base64-encoded string.
  846. */
  847. /* host format (2 bits) */
  848. #define LIBSSH2_KNOWNHOST_TYPE_MASK 0xffff
  849. #define LIBSSH2_KNOWNHOST_TYPE_PLAIN 1
  850. #define LIBSSH2_KNOWNHOST_TYPE_SHA1 2 /* always base64 encoded */
  851. #define LIBSSH2_KNOWNHOST_TYPE_CUSTOM 3
  852. /* key format (2 bits) */
  853. #define LIBSSH2_KNOWNHOST_KEYENC_MASK (3<<16)
  854. #define LIBSSH2_KNOWNHOST_KEYENC_RAW (1<<16)
  855. #define LIBSSH2_KNOWNHOST_KEYENC_BASE64 (2<<16)
  856. /* type of key (3 bits) */
  857. #define LIBSSH2_KNOWNHOST_KEY_MASK (15<<18)
  858. #define LIBSSH2_KNOWNHOST_KEY_SHIFT 18
  859. #define LIBSSH2_KNOWNHOST_KEY_RSA1 (1<<18)
  860. #define LIBSSH2_KNOWNHOST_KEY_SSHRSA (2<<18)
  861. #define LIBSSH2_KNOWNHOST_KEY_SSHDSS (3<<18)
  862. #define LIBSSH2_KNOWNHOST_KEY_ECDSA_256 (4<<18)
  863. #define LIBSSH2_KNOWNHOST_KEY_ECDSA_384 (5<<18)
  864. #define LIBSSH2_KNOWNHOST_KEY_ECDSA_521 (6<<18)
  865. #define LIBSSH2_KNOWNHOST_KEY_ED25519 (7<<18)
  866. #define LIBSSH2_KNOWNHOST_KEY_UNKNOWN (15<<18)
  867. LIBSSH2_API int
  868. libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
  869. const char *host,
  870. const char *salt,
  871. const char *key, size_t keylen, int typemask,
  872. struct libssh2_knownhost **store);
  873. /*
  874. * libssh2_knownhost_addc
  875. *
  876. * Add a host and its associated key to the collection of known hosts.
  877. *
  878. * Takes a comment argument that may be NULL. A NULL comment indicates
  879. * there is no comment and the entry will end directly after the key
  880. * when written out to a file. An empty string "" comment will indicate an
  881. * empty comment which will cause a single space to be written after the key.
  882. *
  883. * The 'type' argument specifies on what format the given host and keys are:
  884. *
  885. * plain - ascii "hostname.domain.tld"
  886. * sha1 - SHA1(<salt> <host>) base64-encoded!
  887. * custom - another hash
  888. *
  889. * If 'sha1' is selected as type, the salt must be provided to the salt
  890. * argument. This too base64 encoded.
  891. *
  892. * The SHA-1 hash is what OpenSSH can be told to use in known_hosts files. If
  893. * a custom type is used, salt is ignored and you must provide the host
  894. * pre-hashed when checking for it in the libssh2_knownhost_check() function.
  895. *
  896. * The keylen parameter may be omitted (zero) if the key is provided as a
  897. * NULL-terminated base64-encoded string.
  898. */
  899. LIBSSH2_API int
  900. libssh2_knownhost_addc(LIBSSH2_KNOWNHOSTS *hosts,
  901. const char *host,
  902. const char *salt,
  903. const char *key, size_t keylen,
  904. const char *comment, size_t commentlen, int typemask,
  905. struct libssh2_knownhost **store);
  906. /*
  907. * libssh2_knownhost_check
  908. *
  909. * Check a host and its associated key against the collection of known hosts.
  910. *
  911. * The type is the type/format of the given host name.
  912. *
  913. * plain - ascii "hostname.domain.tld"
  914. * custom - prehashed base64 encoded. Note that this cannot use any salts.
  915. *
  916. *
  917. * 'knownhost' may be set to NULL if you don't care about that info.
  918. *
  919. * Returns:
  920. *
  921. * LIBSSH2_KNOWNHOST_CHECK_* values, see below
  922. *
  923. */
  924. #define LIBSSH2_KNOWNHOST_CHECK_MATCH 0
  925. #define LIBSSH2_KNOWNHOST_CHECK_MISMATCH 1
  926. #define LIBSSH2_KNOWNHOST_CHECK_NOTFOUND 2
  927. #define LIBSSH2_KNOWNHOST_CHECK_FAILURE 3
  928. LIBSSH2_API int
  929. libssh2_knownhost_check(LIBSSH2_KNOWNHOSTS *hosts,
  930. const char *host, const char *key, size_t keylen,
  931. int typemask,
  932. struct libssh2_knownhost **knownhost);
  933. /* this function is identital to the above one, but also takes a port
  934. argument that allows libssh2 to do a better check */
  935. LIBSSH2_API int
  936. libssh2_knownhost_checkp(LIBSSH2_KNOWNHOSTS *hosts,
  937. const char *host, int port,
  938. const char *key, size_t keylen,
  939. int typemask,
  940. struct libssh2_knownhost **knownhost);
  941. /*
  942. * libssh2_knownhost_del
  943. *
  944. * Remove a host from the collection of known hosts. The 'entry' struct is
  945. * retrieved by a call to libssh2_knownhost_check().
  946. *
  947. */
  948. LIBSSH2_API int
  949. libssh2_knownhost_del(LIBSSH2_KNOWNHOSTS *hosts,
  950. struct libssh2_knownhost *entry);
  951. /*
  952. * libssh2_knownhost_free
  953. *
  954. * Free an entire collection of known hosts.
  955. *
  956. */
  957. LIBSSH2_API void
  958. libssh2_knownhost_free(LIBSSH2_KNOWNHOSTS *hosts);
  959. /*
  960. * libssh2_knownhost_readline()
  961. *
  962. * Pass in a line of a file of 'type'. It makes libssh2 read this line.
  963. *
  964. * LIBSSH2_KNOWNHOST_FILE_OPENSSH is the only supported type.
  965. *
  966. */
  967. LIBSSH2_API int
  968. libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS *hosts,
  969. const char *line, size_t len, int type);
  970. /*
  971. * libssh2_knownhost_readfile
  972. *
  973. * Add hosts+key pairs from a given file.
  974. *
  975. * Returns a negative value for error or number of successfully added hosts.
  976. *
  977. * This implementation currently only knows one 'type' (openssh), all others
  978. * are reserved for future use.
  979. */
  980. #define LIBSSH2_KNOWNHOST_FILE_OPENSSH 1
  981. LIBSSH2_API int
  982. libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS *hosts,
  983. const char *filename, int type);
  984. /*
  985. * libssh2_knownhost_writeline()
  986. *
  987. * Ask libssh2 to convert a known host to an output line for storage.
  988. *
  989. * Note that this function returns LIBSSH2_ERROR_BUFFER_TOO_SMALL if the given
  990. * output buffer is too small to hold the desired output.
  991. *
  992. * This implementation currently only knows one 'type' (openssh), all others
  993. * are reserved for future use.
  994. *
  995. */
  996. LIBSSH2_API int
  997. libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
  998. struct libssh2_knownhost *known,
  999. char *buffer, size_t buflen,
  1000. size_t *outlen, /* the amount of written data */
  1001. int type);
  1002. /*
  1003. * libssh2_knownhost_writefile
  1004. *
  1005. * Write hosts+key pairs to a given file.
  1006. *
  1007. * This implementation currently only knows one 'type' (openssh), all others
  1008. * are reserved for future use.
  1009. */
  1010. LIBSSH2_API int
  1011. libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts,
  1012. const char *filename, int type);
  1013. /*
  1014. * libssh2_knownhost_get()
  1015. *
  1016. * Traverse the internal list of known hosts. Pass NULL to 'prev' to get
  1017. * the first one. Or pass a poiner to the previously returned one to get the
  1018. * next.
  1019. *
  1020. * Returns:
  1021. * 0 if a fine host was stored in 'store'
  1022. * 1 if end of hosts
  1023. * [negative] on errors
  1024. */
  1025. LIBSSH2_API int
  1026. libssh2_knownhost_get(LIBSSH2_KNOWNHOSTS *hosts,
  1027. struct libssh2_knownhost **store,
  1028. struct libssh2_knownhost *prev);
  1029. #define HAVE_LIBSSH2_AGENT_API 0x010202 /* since 1.2.2 */
  1030. struct libssh2_agent_publickey {
  1031. unsigned int magic; /* magic stored by the library */
  1032. void *node; /* handle to the internal representation of key */
  1033. unsigned char *blob; /* public key blob */
  1034. size_t blob_len; /* length of the public key blob */
  1035. char *comment; /* comment in printable format */
  1036. };
  1037. /*
  1038. * libssh2_agent_init
  1039. *
  1040. * Init an ssh-agent handle. Returns the pointer to the handle.
  1041. *
  1042. */
  1043. LIBSSH2_API LIBSSH2_AGENT *
  1044. libssh2_agent_init(LIBSSH2_SESSION *session);
  1045. /*
  1046. * libssh2_agent_connect()
  1047. *
  1048. * Connect to an ssh-agent.
  1049. *
  1050. * Returns 0 if succeeded, or a negative value for error.
  1051. */
  1052. LIBSSH2_API int
  1053. libssh2_agent_connect(LIBSSH2_AGENT *agent);
  1054. /*
  1055. * libssh2_agent_list_identities()
  1056. *
  1057. * Request an ssh-agent to list identities.
  1058. *
  1059. * Returns 0 if succeeded, or a negative value for error.
  1060. */
  1061. LIBSSH2_API int
  1062. libssh2_agent_list_identities(LIBSSH2_AGENT *agent);
  1063. /*
  1064. * libssh2_agent_get_identity()
  1065. *
  1066. * Traverse the internal list of public keys. Pass NULL to 'prev' to get
  1067. * the first one. Or pass a poiner to the previously returned one to get the
  1068. * next.
  1069. *
  1070. * Returns:
  1071. * 0 if a fine public key was stored in 'store'
  1072. * 1 if end of public keys
  1073. * [negative] on errors
  1074. */
  1075. LIBSSH2_API int
  1076. libssh2_agent_get_identity(LIBSSH2_AGENT *agent,
  1077. struct libssh2_agent_publickey **store,
  1078. struct libssh2_agent_publickey *prev);
  1079. /*
  1080. * libssh2_agent_userauth()
  1081. *
  1082. * Do publickey user authentication with the help of ssh-agent.
  1083. *
  1084. * Returns 0 if succeeded, or a negative value for error.
  1085. */
  1086. LIBSSH2_API int
  1087. libssh2_agent_userauth(LIBSSH2_AGENT *agent,
  1088. const char *username,
  1089. struct libssh2_agent_publickey *identity);
  1090. /*
  1091. * libssh2_agent_disconnect()
  1092. *
  1093. * Close a connection to an ssh-agent.
  1094. *
  1095. * Returns 0 if succeeded, or a negative value for error.
  1096. */
  1097. LIBSSH2_API int
  1098. libssh2_agent_disconnect(LIBSSH2_AGENT *agent);
  1099. /*
  1100. * libssh2_agent_free()
  1101. *
  1102. * Free an ssh-agent handle. This function also frees the internal
  1103. * collection of public keys.
  1104. */
  1105. LIBSSH2_API void
  1106. libssh2_agent_free(LIBSSH2_AGENT *agent);
  1107. /*
  1108. * libssh2_agent_set_identity_path()
  1109. *
  1110. * Allows a custom agent identity socket path beyond SSH_AUTH_SOCK env
  1111. *
  1112. */
  1113. LIBSSH2_API void
  1114. libssh2_agent_set_identity_path(LIBSSH2_AGENT *agent,
  1115. const char *path);
  1116. /*
  1117. * libssh2_agent_get_identity_path()
  1118. *
  1119. * Returns the custom agent identity socket path if set
  1120. *
  1121. */
  1122. LIBSSH2_API const char *
  1123. libssh2_agent_get_identity_path(LIBSSH2_AGENT *agent);
  1124. /*
  1125. * libssh2_keepalive_config()
  1126. *
  1127. * Set how often keepalive messages should be sent. WANT_REPLY
  1128. * indicates whether the keepalive messages should request a response
  1129. * from the server. INTERVAL is number of seconds that can pass
  1130. * without any I/O, use 0 (the default) to disable keepalives. To
  1131. * avoid some busy-loop corner-cases, if you specify an interval of 1
  1132. * it will be treated as 2.
  1133. *
  1134. * Note that non-blocking applications are responsible for sending the
  1135. * keepalive messages using libssh2_keepalive_send().
  1136. */
  1137. LIBSSH2_API void libssh2_keepalive_config(LIBSSH2_SESSION *session,
  1138. int want_reply,
  1139. unsigned interval);
  1140. /*
  1141. * libssh2_keepalive_send()
  1142. *
  1143. * Send a keepalive message if needed. SECONDS_TO_NEXT indicates how
  1144. * many seconds you can sleep after this call before you need to call
  1145. * it again. Returns 0 on success, or LIBSSH2_ERROR_SOCKET_SEND on
  1146. * I/O errors.
  1147. */
  1148. LIBSSH2_API int libssh2_keepalive_send(LIBSSH2_SESSION *session,
  1149. int *seconds_to_next);
  1150. /* NOTE NOTE NOTE
  1151. libssh2_trace() has no function in builds that aren't built with debug
  1152. enabled
  1153. */
  1154. LIBSSH2_API int libssh2_trace(LIBSSH2_SESSION *session, int bitmask);
  1155. #define LIBSSH2_TRACE_TRANS (1<<1)
  1156. #define LIBSSH2_TRACE_KEX (1<<2)
  1157. #define LIBSSH2_TRACE_AUTH (1<<3)
  1158. #define LIBSSH2_TRACE_CONN (1<<4)
  1159. #define LIBSSH2_TRACE_SCP (1<<5)
  1160. #define LIBSSH2_TRACE_SFTP (1<<6)
  1161. #define LIBSSH2_TRACE_ERROR (1<<7)
  1162. #define LIBSSH2_TRACE_PUBLICKEY (1<<8)
  1163. #define LIBSSH2_TRACE_SOCKET (1<<9)
  1164. typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*,
  1165. void *,
  1166. const char *,
  1167. size_t);
  1168. LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session,
  1169. void *context,
  1170. libssh2_trace_handler_func callback);
  1171. #ifdef __cplusplus
  1172. } /* extern "C" */
  1173. #endif
  1174. #endif /* !RC_INVOKED */
  1175. #endif /* LIBSSH2_H */