libssh2.h 53 KB

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