libssh2.h 51 KB

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