libssh2.h 57 KB

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