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