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