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