1
/* Copyright (c) 2004-2009, Sara Golemon <sarag@libssh2.org>
2
* Copyright (c) 2009-2012 Daniel Stenberg
3
* Copyright (c) 2010 Simon Josefsson <simon@josefsson.org>
6
* Redistribution and use in source and binary forms,
7
* with or without modification, are permitted provided
8
* that the following conditions are met:
10
* Redistributions of source code must retain the above
11
* copyright notice, this list of conditions and the
12
* following disclaimer.
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.
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.
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
43
#define LIBSSH2_COPYRIGHT "2004-2012 The libssh2 project and its contributors."
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.4.3"
51
/* The numeric version number is also available "in parts" by using these
53
#define LIBSSH2_VERSION_MAJOR 1
54
#define LIBSSH2_VERSION_MINOR 4
55
#define LIBSSH2_VERSION_PATCH 3
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:
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".
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.
72
#define LIBSSH2_VERSION_NUM 0x010403
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.
79
* The format of the date should follow this template:
81
* "Mon Feb 12 11:35:33 UTC 2007"
83
#define LIBSSH2_TIMESTAMP "Tue Nov 27 21:45:20 UTC 2012"
92
# include <winsock2.h>
98
#include <sys/types.h>
100
/* Allow alternate API prefix from CFLAGS or calling app */
102
# ifdef LIBSSH2_WIN32
103
# ifdef LIBSSH2_LIBRARY
104
# define LIBSSH2_API __declspec(dllexport)
106
# define LIBSSH2_API __declspec(dllimport)
107
# endif /* LIBSSH2_LIBRARY */
108
# else /* !LIBSSH2_WIN32 */
110
# endif /* LIBSSH2_WIN32 */
111
#endif /* LIBSSH2_API */
113
#if defined(LIBSSH2_DARWIN)
114
# include <sys/uio.h>
117
#if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
118
# include <sys/bsdskt.h>
119
typedef unsigned char uint8_t;
120
typedef unsigned int uint32_t;
124
typedef unsigned char uint8_t;
125
typedef unsigned int uint32_t;
126
typedef unsigned __int64 libssh2_uint64_t;
127
typedef __int64 libssh2_int64_t;
129
typedef SSIZE_T ssize_t;
132
typedef unsigned long long libssh2_uint64_t;
133
typedef long long libssh2_int64_t;
137
typedef SOCKET libssh2_socket_t;
138
#define LIBSSH2_INVALID_SOCKET INVALID_SOCKET
140
typedef int libssh2_socket_t;
141
#define LIBSSH2_INVALID_SOCKET -1
144
/* Part of every banner, user specified or not */
145
#define LIBSSH2_SSH_BANNER "SSH-2.0-libssh2_" LIBSSH2_VERSION
147
/* We *could* add a comment here if we so chose */
148
#define LIBSSH2_SSH_DEFAULT_BANNER LIBSSH2_SSH_BANNER
149
#define LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF LIBSSH2_SSH_DEFAULT_BANNER "\r\n"
151
/* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 */
152
#define LIBSSH2_DH_GEX_MINGROUP 1024
153
#define LIBSSH2_DH_GEX_OPTGROUP 1536
154
#define LIBSSH2_DH_GEX_MAXGROUP 2048
156
/* Defaults for pty requests */
157
#define LIBSSH2_TERM_WIDTH 80
158
#define LIBSSH2_TERM_HEIGHT 24
159
#define LIBSSH2_TERM_WIDTH_PX 0
160
#define LIBSSH2_TERM_HEIGHT_PX 0
163
#define LIBSSH2_SOCKET_POLL_UDELAY 250000
164
/* 0.25 * 120 == 30 seconds */
165
#define LIBSSH2_SOCKET_POLL_MAXLOOPS 120
167
/* Maximum size to allow a payload to compress to, plays it safe by falling
168
short of spec limits */
169
#define LIBSSH2_PACKET_MAXCOMP 32000
171
/* Maximum size to allow a payload to deccompress to, plays it safe by
172
allowing more than spec requires */
173
#define LIBSSH2_PACKET_MAXDECOMP 40000
175
/* Maximum size for an inbound compressed payload, plays it safe by
176
overshooting spec limits */
177
#define LIBSSH2_PACKET_MAXPAYLOAD 40000
179
/* Malloc callbacks */
180
#define LIBSSH2_ALLOC_FUNC(name) void *name(size_t count, void **abstract)
181
#define LIBSSH2_REALLOC_FUNC(name) void *name(void *ptr, size_t count, \
183
#define LIBSSH2_FREE_FUNC(name) void name(void *ptr, void **abstract)
185
typedef struct _LIBSSH2_USERAUTH_KBDINT_PROMPT
190
} LIBSSH2_USERAUTH_KBDINT_PROMPT;
192
typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE
196
} LIBSSH2_USERAUTH_KBDINT_RESPONSE;
198
/* 'publickey' authentication callback */
199
#define LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC(name) \
200
int name(LIBSSH2_SESSION *session, unsigned char **sig, size_t *sig_len, \
201
const unsigned char *data, size_t data_len, void **abstract)
203
/* 'keyboard-interactive' authentication callback */
204
#define LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC(name_) \
205
void name_(const char* name, int name_len, const char* instruction, \
206
int instruction_len, int num_prompts, \
207
const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts, \
208
LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, void **abstract)
210
/* Callbacks for special SSH packets */
211
#define LIBSSH2_IGNORE_FUNC(name) \
212
void name(LIBSSH2_SESSION *session, const char *message, int message_len, \
215
#define LIBSSH2_DEBUG_FUNC(name) \
216
void name(LIBSSH2_SESSION *session, int always_display, const char *message, \
217
int message_len, const char *language, int language_len, \
220
#define LIBSSH2_DISCONNECT_FUNC(name) \
221
void name(LIBSSH2_SESSION *session, int reason, const char *message, \
222
int message_len, const char *language, int language_len, \
225
#define LIBSSH2_PASSWD_CHANGEREQ_FUNC(name) \
226
void name(LIBSSH2_SESSION *session, char **newpw, int *newpw_len, \
229
#define LIBSSH2_MACERROR_FUNC(name) \
230
int name(LIBSSH2_SESSION *session, const char *packet, int packet_len, \
233
#define LIBSSH2_X11_OPEN_FUNC(name) \
234
void name(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, \
235
const char *shost, int sport, void **abstract)
237
#define LIBSSH2_CHANNEL_CLOSE_FUNC(name) \
238
void name(LIBSSH2_SESSION *session, void **session_abstract, \
239
LIBSSH2_CHANNEL *channel, void **channel_abstract)
242
#define LIBSSH2_RECV_FUNC(name) ssize_t name(libssh2_socket_t socket, \
243
void *buffer, size_t length, \
244
int flags, void **abstract)
245
#define LIBSSH2_SEND_FUNC(name) ssize_t name(libssh2_socket_t socket, \
246
const void *buffer, size_t length,\
247
int flags, void **abstract)
249
/* libssh2_session_callback_set() constants */
250
#define LIBSSH2_CALLBACK_IGNORE 0
251
#define LIBSSH2_CALLBACK_DEBUG 1
252
#define LIBSSH2_CALLBACK_DISCONNECT 2
253
#define LIBSSH2_CALLBACK_MACERROR 3
254
#define LIBSSH2_CALLBACK_X11 4
255
#define LIBSSH2_CALLBACK_SEND 5
256
#define LIBSSH2_CALLBACK_RECV 6
258
/* libssh2_session_method_pref() constants */
259
#define LIBSSH2_METHOD_KEX 0
260
#define LIBSSH2_METHOD_HOSTKEY 1
261
#define LIBSSH2_METHOD_CRYPT_CS 2
262
#define LIBSSH2_METHOD_CRYPT_SC 3
263
#define LIBSSH2_METHOD_MAC_CS 4
264
#define LIBSSH2_METHOD_MAC_SC 5
265
#define LIBSSH2_METHOD_COMP_CS 6
266
#define LIBSSH2_METHOD_COMP_SC 7
267
#define LIBSSH2_METHOD_LANG_CS 8
268
#define LIBSSH2_METHOD_LANG_SC 9
271
#define LIBSSH2_FLAG_SIGPIPE 1
272
#define LIBSSH2_FLAG_COMPRESS 2
274
typedef struct _LIBSSH2_SESSION LIBSSH2_SESSION;
275
typedef struct _LIBSSH2_CHANNEL LIBSSH2_CHANNEL;
276
typedef struct _LIBSSH2_LISTENER LIBSSH2_LISTENER;
277
typedef struct _LIBSSH2_KNOWNHOSTS LIBSSH2_KNOWNHOSTS;
278
typedef struct _LIBSSH2_AGENT LIBSSH2_AGENT;
280
typedef struct _LIBSSH2_POLLFD {
281
unsigned char type; /* LIBSSH2_POLLFD_* below */
284
int socket; /* File descriptors -- examined with system select() call */
285
LIBSSH2_CHANNEL *channel; /* Examined by checking internal state */
286
LIBSSH2_LISTENER *listener; /* Read polls only -- are inbound
287
connections waiting to be accepted? */
290
unsigned long events; /* Requested Events */
291
unsigned long revents; /* Returned Events */
294
/* Poll FD Descriptor Types */
295
#define LIBSSH2_POLLFD_SOCKET 1
296
#define LIBSSH2_POLLFD_CHANNEL 2
297
#define LIBSSH2_POLLFD_LISTENER 3
299
/* Note: Win32 Doesn't actually have a poll() implementation, so some of these
300
values are faked with select() data */
301
/* Poll FD events/revents -- Match sys/poll.h where possible */
302
#define LIBSSH2_POLLFD_POLLIN 0x0001 /* Data available to be read or
303
connection available --
305
#define LIBSSH2_POLLFD_POLLPRI 0x0002 /* Priority data available to
306
be read -- Socket only */
307
#define LIBSSH2_POLLFD_POLLEXT 0x0002 /* Extended data available to
308
be read -- Channel only */
309
#define LIBSSH2_POLLFD_POLLOUT 0x0004 /* Can may be written --
312
#define LIBSSH2_POLLFD_POLLERR 0x0008 /* Error Condition -- Socket */
313
#define LIBSSH2_POLLFD_POLLHUP 0x0010 /* HangUp/EOF -- Socket */
314
#define LIBSSH2_POLLFD_SESSION_CLOSED 0x0010 /* Session Disconnect */
315
#define LIBSSH2_POLLFD_POLLNVAL 0x0020 /* Invalid request -- Socket
317
#define LIBSSH2_POLLFD_POLLEX 0x0040 /* Exception Condition --
319
#define LIBSSH2_POLLFD_CHANNEL_CLOSED 0x0080 /* Channel Disconnect */
320
#define LIBSSH2_POLLFD_LISTENER_CLOSED 0x0080 /* Listener Disconnect */
322
#define HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
323
/* Block Direction Types */
324
#define LIBSSH2_SESSION_BLOCK_INBOUND 0x0001
325
#define LIBSSH2_SESSION_BLOCK_OUTBOUND 0x0002
328
#define LIBSSH2_HOSTKEY_HASH_MD5 1
329
#define LIBSSH2_HOSTKEY_HASH_SHA1 2
332
#define LIBSSH2_HOSTKEY_TYPE_UNKNOWN 0
333
#define LIBSSH2_HOSTKEY_TYPE_RSA 1
334
#define LIBSSH2_HOSTKEY_TYPE_DSS 2
336
/* Disconnect Codes (defined by SSH protocol) */
337
#define SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1
338
#define SSH_DISCONNECT_PROTOCOL_ERROR 2
339
#define SSH_DISCONNECT_KEY_EXCHANGE_FAILED 3
340
#define SSH_DISCONNECT_RESERVED 4
341
#define SSH_DISCONNECT_MAC_ERROR 5
342
#define SSH_DISCONNECT_COMPRESSION_ERROR 6
343
#define SSH_DISCONNECT_SERVICE_NOT_AVAILABLE 7
344
#define SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8
345
#define SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9
346
#define SSH_DISCONNECT_CONNECTION_LOST 10
347
#define SSH_DISCONNECT_BY_APPLICATION 11
348
#define SSH_DISCONNECT_TOO_MANY_CONNECTIONS 12
349
#define SSH_DISCONNECT_AUTH_CANCELLED_BY_USER 13
350
#define SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14
351
#define SSH_DISCONNECT_ILLEGAL_USER_NAME 15
353
/* Error Codes (defined by libssh2) */
354
#define LIBSSH2_ERROR_NONE 0
356
/* The library once used -1 as a generic error return value on numerous places
357
through the code, which subsequently was converted to
358
LIBSSH2_ERROR_SOCKET_NONE uses over time. As this is a generic error code,
359
the goal is to never ever return this code but instead make sure that a
360
more accurate and descriptive error code is used. */
361
#define LIBSSH2_ERROR_SOCKET_NONE -1
363
#define LIBSSH2_ERROR_BANNER_RECV -2
364
#define LIBSSH2_ERROR_BANNER_SEND -3
365
#define LIBSSH2_ERROR_INVALID_MAC -4
366
#define LIBSSH2_ERROR_KEX_FAILURE -5
367
#define LIBSSH2_ERROR_ALLOC -6
368
#define LIBSSH2_ERROR_SOCKET_SEND -7
369
#define LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE -8
370
#define LIBSSH2_ERROR_TIMEOUT -9
371
#define LIBSSH2_ERROR_HOSTKEY_INIT -10
372
#define LIBSSH2_ERROR_HOSTKEY_SIGN -11
373
#define LIBSSH2_ERROR_DECRYPT -12
374
#define LIBSSH2_ERROR_SOCKET_DISCONNECT -13
375
#define LIBSSH2_ERROR_PROTO -14
376
#define LIBSSH2_ERROR_PASSWORD_EXPIRED -15
377
#define LIBSSH2_ERROR_FILE -16
378
#define LIBSSH2_ERROR_METHOD_NONE -17
379
#define LIBSSH2_ERROR_AUTHENTICATION_FAILED -18
380
#define LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED LIBSSH2_ERROR_AUTHENTICATION_FAILED
381
#define LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED -19
382
#define LIBSSH2_ERROR_CHANNEL_OUTOFORDER -20
383
#define LIBSSH2_ERROR_CHANNEL_FAILURE -21
384
#define LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED -22
385
#define LIBSSH2_ERROR_CHANNEL_UNKNOWN -23
386
#define LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED -24
387
#define LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED -25
388
#define LIBSSH2_ERROR_CHANNEL_CLOSED -26
389
#define LIBSSH2_ERROR_CHANNEL_EOF_SENT -27
390
#define LIBSSH2_ERROR_SCP_PROTOCOL -28
391
#define LIBSSH2_ERROR_ZLIB -29
392
#define LIBSSH2_ERROR_SOCKET_TIMEOUT -30
393
#define LIBSSH2_ERROR_SFTP_PROTOCOL -31
394
#define LIBSSH2_ERROR_REQUEST_DENIED -32
395
#define LIBSSH2_ERROR_METHOD_NOT_SUPPORTED -33
396
#define LIBSSH2_ERROR_INVAL -34
397
#define LIBSSH2_ERROR_INVALID_POLL_TYPE -35
398
#define LIBSSH2_ERROR_PUBLICKEY_PROTOCOL -36
399
#define LIBSSH2_ERROR_EAGAIN -37
400
#define LIBSSH2_ERROR_BUFFER_TOO_SMALL -38
401
#define LIBSSH2_ERROR_BAD_USE -39
402
#define LIBSSH2_ERROR_COMPRESS -40
403
#define LIBSSH2_ERROR_OUT_OF_BOUNDARY -41
404
#define LIBSSH2_ERROR_AGENT_PROTOCOL -42
405
#define LIBSSH2_ERROR_SOCKET_RECV -43
406
#define LIBSSH2_ERROR_ENCRYPT -44
407
#define LIBSSH2_ERROR_BAD_SOCKET -45
408
#define LIBSSH2_ERROR_KNOWN_HOSTS -46
410
/* this is a define to provide the old (<= 1.2.7) name */
411
#define LIBSSH2_ERROR_BANNER_NONE LIBSSH2_ERROR_BANNER_RECV
414
#define LIBSSH2_INIT_NO_CRYPTO 0x0001
419
* Initialize the libssh2 functions. This typically initialize the
420
* crypto library. It uses a global state, and is not thread safe --
421
* you must make sure this function is not called concurrently.
424
* 0: Normal initialize
425
* LIBSSH2_INIT_NO_CRYPTO: Do not initialize the crypto library (ie.
426
* OPENSSL_add_cipher_algoritms() for OpenSSL
428
* Returns 0 if succeeded, or a negative value for error.
430
LIBSSH2_API int libssh2_init(int flags);
435
* Exit the libssh2 functions and free's all memory used internal.
437
LIBSSH2_API void libssh2_exit(void);
442
* Deallocate memory allocated by earlier call to libssh2 functions.
444
LIBSSH2_API void libssh2_free(LIBSSH2_SESSION *session, void *ptr);
447
* libssh2_session_supported_algs()
449
* Fills algs with a list of supported acryptographic algorithms. Returns a
450
* non-negative number (number of supported algorithms) on success or a
451
* negative number (an eror code) on failure.
453
* NOTE: on success, algs must be deallocated (by calling libssh2_free) when
456
LIBSSH2_API int libssh2_session_supported_algs(LIBSSH2_SESSION* session,
461
LIBSSH2_API LIBSSH2_SESSION *
462
libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC((*my_alloc)),
463
LIBSSH2_FREE_FUNC((*my_free)),
464
LIBSSH2_REALLOC_FUNC((*my_realloc)), void *abstract);
465
#define libssh2_session_init() libssh2_session_init_ex(NULL, NULL, NULL, NULL)
467
LIBSSH2_API void **libssh2_session_abstract(LIBSSH2_SESSION *session);
469
LIBSSH2_API void *libssh2_session_callback_set(LIBSSH2_SESSION *session,
470
int cbtype, void *callback);
471
LIBSSH2_API int libssh2_session_banner_set(LIBSSH2_SESSION *session,
473
LIBSSH2_API int libssh2_banner_set(LIBSSH2_SESSION *session,
476
LIBSSH2_API int libssh2_session_startup(LIBSSH2_SESSION *session, int sock);
477
LIBSSH2_API int libssh2_session_handshake(LIBSSH2_SESSION *session,
478
libssh2_socket_t sock);
479
LIBSSH2_API int libssh2_session_disconnect_ex(LIBSSH2_SESSION *session,
481
const char *description,
483
#define libssh2_session_disconnect(session, description) \
484
libssh2_session_disconnect_ex((session), SSH_DISCONNECT_BY_APPLICATION, \
487
LIBSSH2_API int libssh2_session_free(LIBSSH2_SESSION *session);
489
LIBSSH2_API const char *libssh2_hostkey_hash(LIBSSH2_SESSION *session,
492
LIBSSH2_API const char *libssh2_session_hostkey(LIBSSH2_SESSION *session,
493
size_t *len, int *type);
495
LIBSSH2_API int libssh2_session_method_pref(LIBSSH2_SESSION *session,
498
LIBSSH2_API const char *libssh2_session_methods(LIBSSH2_SESSION *session,
500
LIBSSH2_API int libssh2_session_last_error(LIBSSH2_SESSION *session,
502
int *errmsg_len, int want_buf);
503
LIBSSH2_API int libssh2_session_last_errno(LIBSSH2_SESSION *session);
504
LIBSSH2_API int libssh2_session_block_directions(LIBSSH2_SESSION *session);
506
LIBSSH2_API int libssh2_session_flag(LIBSSH2_SESSION *session, int flag,
508
LIBSSH2_API const char *libssh2_session_banner_get(LIBSSH2_SESSION *session);
511
LIBSSH2_API char *libssh2_userauth_list(LIBSSH2_SESSION *session,
512
const char *username,
513
unsigned int username_len);
514
LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session);
516
LIBSSH2_API int libssh2_userauth_password_ex(LIBSSH2_SESSION *session,
517
const char *username,
518
unsigned int username_len,
519
const char *password,
520
unsigned int password_len,
521
LIBSSH2_PASSWD_CHANGEREQ_FUNC((*passwd_change_cb)));
523
#define libssh2_userauth_password(session, username, password) \
524
libssh2_userauth_password_ex((session), (username), strlen(username), \
525
(password), strlen(password), NULL)
528
libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session,
529
const char *username,
530
unsigned int username_len,
531
const char *publickey,
532
const char *privatekey,
533
const char *passphrase);
535
#define libssh2_userauth_publickey_fromfile(session, username, publickey, \
536
privatekey, passphrase) \
537
libssh2_userauth_publickey_fromfile_ex((session), (username), \
538
strlen(username), (publickey), \
539
(privatekey), (passphrase))
542
libssh2_userauth_publickey(LIBSSH2_SESSION *session,
543
const char *username,
544
const unsigned char *pubkeydata,
545
size_t pubkeydata_len,
546
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)),
550
libssh2_userauth_hostbased_fromfile_ex(LIBSSH2_SESSION *session,
551
const char *username,
552
unsigned int username_len,
553
const char *publickey,
554
const char *privatekey,
555
const char *passphrase,
556
const char *hostname,
557
unsigned int hostname_len,
558
const char *local_username,
559
unsigned int local_username_len);
561
#define libssh2_userauth_hostbased_fromfile(session, username, publickey, \
562
privatekey, passphrase, hostname) \
563
libssh2_userauth_hostbased_fromfile_ex((session), (username), \
564
strlen(username), (publickey), \
565
(privatekey), (passphrase), \
566
(hostname), strlen(hostname), \
567
(username), strlen(username))
570
* response_callback is provided with filled by library prompts array,
571
* but client must allocate and fill individual responses. Responses
572
* array is already allocated. Responses data will be freed by libssh2
573
* after callback return, but before subsequent callback invokation.
576
libssh2_userauth_keyboard_interactive_ex(LIBSSH2_SESSION* session,
577
const char *username,
578
unsigned int username_len,
579
LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC((*response_callback)));
581
#define libssh2_userauth_keyboard_interactive(session, username, \
583
libssh2_userauth_keyboard_interactive_ex((session), (username), \
584
strlen(username), (response_callback))
586
LIBSSH2_API int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds,
590
#define LIBSSH2_CHANNEL_WINDOW_DEFAULT (256*1024)
591
#define LIBSSH2_CHANNEL_PACKET_DEFAULT 32768
592
#define LIBSSH2_CHANNEL_MINADJUST 1024
594
/* Extended Data Handling */
595
#define LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL 0
596
#define LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE 1
597
#define LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE 2
599
#define SSH_EXTENDED_DATA_STDERR 1
601
/* Returned by any function that would block during a read/write opperation */
602
#define LIBSSH2CHANNEL_EAGAIN LIBSSH2_ERROR_EAGAIN
604
LIBSSH2_API LIBSSH2_CHANNEL *
605
libssh2_channel_open_ex(LIBSSH2_SESSION *session, const char *channel_type,
606
unsigned int channel_type_len,
607
unsigned int window_size, unsigned int packet_size,
608
const char *message, unsigned int message_len);
610
#define libssh2_channel_open_session(session) \
611
libssh2_channel_open_ex((session), "session", sizeof("session") - 1, \
612
LIBSSH2_CHANNEL_WINDOW_DEFAULT, \
613
LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0)
615
LIBSSH2_API LIBSSH2_CHANNEL *
616
libssh2_channel_direct_tcpip_ex(LIBSSH2_SESSION *session, const char *host,
617
int port, const char *shost, int sport);
618
#define libssh2_channel_direct_tcpip(session, host, port) \
619
libssh2_channel_direct_tcpip_ex((session), (host), (port), "127.0.0.1", 22)
621
LIBSSH2_API LIBSSH2_LISTENER *
622
libssh2_channel_forward_listen_ex(LIBSSH2_SESSION *session, const char *host,
623
int port, int *bound_port, int queue_maxsize);
624
#define libssh2_channel_forward_listen(session, port) \
625
libssh2_channel_forward_listen_ex((session), NULL, (port), NULL, 16)
627
LIBSSH2_API int libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener);
629
LIBSSH2_API LIBSSH2_CHANNEL *
630
libssh2_channel_forward_accept(LIBSSH2_LISTENER *listener);
632
LIBSSH2_API int libssh2_channel_setenv_ex(LIBSSH2_CHANNEL *channel,
634
unsigned int varname_len,
636
unsigned int value_len);
638
#define libssh2_channel_setenv(channel, varname, value) \
639
libssh2_channel_setenv_ex((channel), (varname), strlen(varname), (value), \
642
LIBSSH2_API int libssh2_channel_request_pty_ex(LIBSSH2_CHANNEL *channel,
644
unsigned int term_len,
646
unsigned int modes_len,
647
int width, int height,
648
int width_px, int height_px);
649
#define libssh2_channel_request_pty(channel, term) \
650
libssh2_channel_request_pty_ex((channel), (term), strlen(term), NULL, 0, \
651
LIBSSH2_TERM_WIDTH, LIBSSH2_TERM_HEIGHT, \
652
LIBSSH2_TERM_WIDTH_PX, LIBSSH2_TERM_HEIGHT_PX)
654
LIBSSH2_API int libssh2_channel_request_pty_size_ex(LIBSSH2_CHANNEL *channel,
655
int width, int height,
658
#define libssh2_channel_request_pty_size(channel, width, height) \
659
libssh2_channel_request_pty_size_ex( (channel), (width), (height), 0, 0)
661
LIBSSH2_API int libssh2_channel_x11_req_ex(LIBSSH2_CHANNEL *channel,
662
int single_connection,
663
const char *auth_proto,
664
const char *auth_cookie,
666
#define libssh2_channel_x11_req(channel, screen_number) \
667
libssh2_channel_x11_req_ex((channel), 0, NULL, NULL, (screen_number))
669
LIBSSH2_API int libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
671
unsigned int request_len,
673
unsigned int message_len);
674
#define libssh2_channel_shell(channel) \
675
libssh2_channel_process_startup((channel), "shell", sizeof("shell") - 1, \
677
#define libssh2_channel_exec(channel, command) \
678
libssh2_channel_process_startup((channel), "exec", sizeof("exec") - 1, \
679
(command), strlen(command))
680
#define libssh2_channel_subsystem(channel, subsystem) \
681
libssh2_channel_process_startup((channel), "subsystem", \
682
sizeof("subsystem") - 1, (subsystem), \
685
LIBSSH2_API ssize_t libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel,
686
int stream_id, char *buf,
688
#define libssh2_channel_read(channel, buf, buflen) \
689
libssh2_channel_read_ex((channel), 0, (buf), (buflen))
690
#define libssh2_channel_read_stderr(channel, buf, buflen) \
691
libssh2_channel_read_ex((channel), SSH_EXTENDED_DATA_STDERR, (buf), (buflen))
693
LIBSSH2_API int libssh2_poll_channel_read(LIBSSH2_CHANNEL *channel,
696
LIBSSH2_API unsigned long
697
libssh2_channel_window_read_ex(LIBSSH2_CHANNEL *channel,
698
unsigned long *read_avail,
699
unsigned long *window_size_initial);
700
#define libssh2_channel_window_read(channel) \
701
libssh2_channel_window_read_ex((channel), NULL, NULL)
703
/* libssh2_channel_receive_window_adjust is DEPRECATED, do not use! */
704
LIBSSH2_API unsigned long
705
libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel,
706
unsigned long adjustment,
707
unsigned char force);
710
libssh2_channel_receive_window_adjust2(LIBSSH2_CHANNEL *channel,
711
unsigned long adjustment,
713
unsigned int *storewindow);
715
LIBSSH2_API ssize_t libssh2_channel_write_ex(LIBSSH2_CHANNEL *channel,
716
int stream_id, const char *buf,
719
#define libssh2_channel_write(channel, buf, buflen) \
720
libssh2_channel_write_ex((channel), 0, (buf), (buflen))
721
#define libssh2_channel_write_stderr(channel, buf, buflen) \
722
libssh2_channel_write_ex((channel), SSH_EXTENDED_DATA_STDERR, (buf), (buflen))
724
LIBSSH2_API unsigned long
725
libssh2_channel_window_write_ex(LIBSSH2_CHANNEL *channel,
726
unsigned long *window_size_initial);
727
#define libssh2_channel_window_write(channel) \
728
libssh2_channel_window_write_ex((channel), NULL)
730
LIBSSH2_API void libssh2_session_set_blocking(LIBSSH2_SESSION* session,
732
LIBSSH2_API int libssh2_session_get_blocking(LIBSSH2_SESSION* session);
734
LIBSSH2_API void libssh2_channel_set_blocking(LIBSSH2_CHANNEL *channel,
737
LIBSSH2_API void libssh2_session_set_timeout(LIBSSH2_SESSION* session,
739
LIBSSH2_API long libssh2_session_get_timeout(LIBSSH2_SESSION* session);
741
/* libssh2_channel_handle_extended_data is DEPRECATED, do not use! */
742
LIBSSH2_API void libssh2_channel_handle_extended_data(LIBSSH2_CHANNEL *channel,
744
LIBSSH2_API int libssh2_channel_handle_extended_data2(LIBSSH2_CHANNEL *channel,
747
/* libssh2_channel_ignore_extended_data() is defined below for BC with version
750
* Future uses should use libssh2_channel_handle_extended_data() directly if
751
* LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE is passed, extended data will be read
752
* (FIFO) from the standard data channel
755
#define libssh2_channel_ignore_extended_data(channel, ignore) \
756
libssh2_channel_handle_extended_data((channel), \
758
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE : \
759
LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL )
761
#define LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA -1
762
#define LIBSSH2_CHANNEL_FLUSH_ALL -2
763
LIBSSH2_API int libssh2_channel_flush_ex(LIBSSH2_CHANNEL *channel,
765
#define libssh2_channel_flush(channel) libssh2_channel_flush_ex((channel), 0)
766
#define libssh2_channel_flush_stderr(channel) \
767
libssh2_channel_flush_ex((channel), SSH_EXTENDED_DATA_STDERR)
769
LIBSSH2_API int libssh2_channel_get_exit_status(LIBSSH2_CHANNEL* channel);
770
LIBSSH2_API int libssh2_channel_get_exit_signal(LIBSSH2_CHANNEL* channel,
772
size_t *exitsignal_len,
776
size_t *langtag_len);
777
LIBSSH2_API int libssh2_channel_send_eof(LIBSSH2_CHANNEL *channel);
778
LIBSSH2_API int libssh2_channel_eof(LIBSSH2_CHANNEL *channel);
779
LIBSSH2_API int libssh2_channel_wait_eof(LIBSSH2_CHANNEL *channel);
780
LIBSSH2_API int libssh2_channel_close(LIBSSH2_CHANNEL *channel);
781
LIBSSH2_API int libssh2_channel_wait_closed(LIBSSH2_CHANNEL *channel);
782
LIBSSH2_API int libssh2_channel_free(LIBSSH2_CHANNEL *channel);
784
LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_recv(LIBSSH2_SESSION *session,
787
LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_send_ex(LIBSSH2_SESSION *session,
788
const char *path, int mode,
789
size_t size, long mtime,
791
LIBSSH2_API LIBSSH2_CHANNEL *
792
libssh2_scp_send64(LIBSSH2_SESSION *session, const char *path, int mode,
793
libssh2_int64_t size, time_t mtime, time_t atime);
795
#define libssh2_scp_send(session, path, mode, size) \
796
libssh2_scp_send_ex((session), (path), (mode), (size), 0, 0)
798
LIBSSH2_API int libssh2_base64_decode(LIBSSH2_SESSION *session, char **dest,
799
unsigned int *dest_len,
800
const char *src, unsigned int src_len);
803
const char *libssh2_version(int req_version_num);
805
#define HAVE_LIBSSH2_KNOWNHOST_API 0x010101 /* since 1.1.1 */
806
#define HAVE_LIBSSH2_VERSION_API 0x010100 /* libssh2_version since 1.1 */
808
struct libssh2_knownhost {
809
unsigned int magic; /* magic stored by the library */
810
void *node; /* handle to the internal representation of this host */
811
char *name; /* this is NULL if no plain text host name exists */
812
char *key; /* key in base64/printable format */
817
* libssh2_knownhost_init
819
* Init a collection of known hosts. Returns the pointer to a collection.
822
LIBSSH2_API LIBSSH2_KNOWNHOSTS *
823
libssh2_knownhost_init(LIBSSH2_SESSION *session);
826
* libssh2_knownhost_add
828
* Add a host and its associated key to the collection of known hosts.
830
* The 'type' argument specifies on what format the given host and keys are:
832
* plain - ascii "hostname.domain.tld"
833
* sha1 - SHA1(<salt> <host>) base64-encoded!
834
* custom - another hash
836
* If 'sha1' is selected as type, the salt must be provided to the salt
837
* argument. This too base64 encoded.
839
* The SHA-1 hash is what OpenSSH can be told to use in known_hosts files. If
840
* a custom type is used, salt is ignored and you must provide the host
841
* pre-hashed when checking for it in the libssh2_knownhost_check() function.
843
* The keylen parameter may be omitted (zero) if the key is provided as a
844
* NULL-terminated base64-encoded string.
847
/* host format (2 bits) */
848
#define LIBSSH2_KNOWNHOST_TYPE_MASK 0xffff
849
#define LIBSSH2_KNOWNHOST_TYPE_PLAIN 1
850
#define LIBSSH2_KNOWNHOST_TYPE_SHA1 2 /* always base64 encoded */
851
#define LIBSSH2_KNOWNHOST_TYPE_CUSTOM 3
853
/* key format (2 bits) */
854
#define LIBSSH2_KNOWNHOST_KEYENC_MASK (3<<16)
855
#define LIBSSH2_KNOWNHOST_KEYENC_RAW (1<<16)
856
#define LIBSSH2_KNOWNHOST_KEYENC_BASE64 (2<<16)
858
/* type of key (2 bits) */
859
#define LIBSSH2_KNOWNHOST_KEY_MASK (3<<18)
860
#define LIBSSH2_KNOWNHOST_KEY_SHIFT 18
861
#define LIBSSH2_KNOWNHOST_KEY_RSA1 (1<<18)
862
#define LIBSSH2_KNOWNHOST_KEY_SSHRSA (2<<18)
863
#define LIBSSH2_KNOWNHOST_KEY_SSHDSS (3<<18)
866
libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
869
const char *key, size_t keylen, int typemask,
870
struct libssh2_knownhost **store);
873
* libssh2_knownhost_addc
875
* Add a host and its associated key to the collection of known hosts.
877
* Takes a comment argument that may be NULL. A NULL comment indicates
878
* there is no comment and the entry will end directly after the key
879
* when written out to a file. An empty string "" comment will indicate an
880
* empty comment which will cause a single space to be written after the key.
882
* The 'type' argument specifies on what format the given host and keys are:
884
* plain - ascii "hostname.domain.tld"
885
* sha1 - SHA1(<salt> <host>) base64-encoded!
886
* custom - another hash
888
* If 'sha1' is selected as type, the salt must be provided to the salt
889
* argument. This too base64 encoded.
891
* The SHA-1 hash is what OpenSSH can be told to use in known_hosts files. If
892
* a custom type is used, salt is ignored and you must provide the host
893
* pre-hashed when checking for it in the libssh2_knownhost_check() function.
895
* The keylen parameter may be omitted (zero) if the key is provided as a
896
* NULL-terminated base64-encoded string.
900
libssh2_knownhost_addc(LIBSSH2_KNOWNHOSTS *hosts,
903
const char *key, size_t keylen,
904
const char *comment, size_t commentlen, int typemask,
905
struct libssh2_knownhost **store);
908
* libssh2_knownhost_check
910
* Check a host and its associated key against the collection of known hosts.
912
* The type is the type/format of the given host name.
914
* plain - ascii "hostname.domain.tld"
915
* custom - prehashed base64 encoded. Note that this cannot use any salts.
918
* 'knownhost' may be set to NULL if you don't care about that info.
922
* LIBSSH2_KNOWNHOST_CHECK_* values, see below
926
#define LIBSSH2_KNOWNHOST_CHECK_MATCH 0
927
#define LIBSSH2_KNOWNHOST_CHECK_MISMATCH 1
928
#define LIBSSH2_KNOWNHOST_CHECK_NOTFOUND 2
929
#define LIBSSH2_KNOWNHOST_CHECK_FAILURE 3
932
libssh2_knownhost_check(LIBSSH2_KNOWNHOSTS *hosts,
933
const char *host, const char *key, size_t keylen,
935
struct libssh2_knownhost **knownhost);
937
/* this function is identital to the above one, but also takes a port
938
argument that allows libssh2 to do a better check */
940
libssh2_knownhost_checkp(LIBSSH2_KNOWNHOSTS *hosts,
941
const char *host, int port,
942
const char *key, size_t keylen,
944
struct libssh2_knownhost **knownhost);
947
* libssh2_knownhost_del
949
* Remove a host from the collection of known hosts. The 'entry' struct is
950
* retrieved by a call to libssh2_knownhost_check().
954
libssh2_knownhost_del(LIBSSH2_KNOWNHOSTS *hosts,
955
struct libssh2_knownhost *entry);
958
* libssh2_knownhost_free
960
* Free an entire collection of known hosts.
964
libssh2_knownhost_free(LIBSSH2_KNOWNHOSTS *hosts);
967
* libssh2_knownhost_readline()
969
* Pass in a line of a file of 'type'. It makes libssh2 read this line.
971
* LIBSSH2_KNOWNHOST_FILE_OPENSSH is the only supported type.
975
libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS *hosts,
976
const char *line, size_t len, int type);
979
* libssh2_knownhost_readfile
981
* Add hosts+key pairs from a given file.
983
* Returns a negative value for error or number of successfully added hosts.
985
* This implementation currently only knows one 'type' (openssh), all others
986
* are reserved for future use.
989
#define LIBSSH2_KNOWNHOST_FILE_OPENSSH 1
992
libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS *hosts,
993
const char *filename, int type);
996
* libssh2_knownhost_writeline()
998
* Ask libssh2 to convert a known host to an output line for storage.
1000
* Note that this function returns LIBSSH2_ERROR_BUFFER_TOO_SMALL if the given
1001
* output buffer is too small to hold the desired output.
1003
* This implementation currently only knows one 'type' (openssh), all others
1004
* are reserved for future use.
1008
libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
1009
struct libssh2_knownhost *known,
1010
char *buffer, size_t buflen,
1011
size_t *outlen, /* the amount of written data */
1015
* libssh2_knownhost_writefile
1017
* Write hosts+key pairs to a given file.
1019
* This implementation currently only knows one 'type' (openssh), all others
1020
* are reserved for future use.
1024
libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts,
1025
const char *filename, int type);
1028
* libssh2_knownhost_get()
1030
* Traverse the internal list of known hosts. Pass NULL to 'prev' to get
1031
* the first one. Or pass a poiner to the previously returned one to get the
1035
* 0 if a fine host was stored in 'store'
1037
* [negative] on errors
1040
libssh2_knownhost_get(LIBSSH2_KNOWNHOSTS *hosts,
1041
struct libssh2_knownhost **store,
1042
struct libssh2_knownhost *prev);
1044
#define HAVE_LIBSSH2_AGENT_API 0x010202 /* since 1.2.2 */
1046
struct libssh2_agent_publickey {
1047
unsigned int magic; /* magic stored by the library */
1048
void *node; /* handle to the internal representation of key */
1049
unsigned char *blob; /* public key blob */
1050
size_t blob_len; /* length of the public key blob */
1051
char *comment; /* comment in printable format */
1055
* libssh2_agent_init
1057
* Init an ssh-agent handle. Returns the pointer to the handle.
1060
LIBSSH2_API LIBSSH2_AGENT *
1061
libssh2_agent_init(LIBSSH2_SESSION *session);
1064
* libssh2_agent_connect()
1066
* Connect to an ssh-agent.
1068
* Returns 0 if succeeded, or a negative value for error.
1071
libssh2_agent_connect(LIBSSH2_AGENT *agent);
1074
* libssh2_agent_list_identities()
1076
* Request an ssh-agent to list identities.
1078
* Returns 0 if succeeded, or a negative value for error.
1081
libssh2_agent_list_identities(LIBSSH2_AGENT *agent);
1084
* libssh2_agent_get_identity()
1086
* Traverse the internal list of public keys. Pass NULL to 'prev' to get
1087
* the first one. Or pass a poiner to the previously returned one to get the
1091
* 0 if a fine public key was stored in 'store'
1092
* 1 if end of public keys
1093
* [negative] on errors
1096
libssh2_agent_get_identity(LIBSSH2_AGENT *agent,
1097
struct libssh2_agent_publickey **store,
1098
struct libssh2_agent_publickey *prev);
1101
* libssh2_agent_userauth()
1103
* Do publickey user authentication with the help of ssh-agent.
1105
* Returns 0 if succeeded, or a negative value for error.
1108
libssh2_agent_userauth(LIBSSH2_AGENT *agent,
1109
const char *username,
1110
struct libssh2_agent_publickey *identity);
1113
* libssh2_agent_disconnect()
1115
* Close a connection to an ssh-agent.
1117
* Returns 0 if succeeded, or a negative value for error.
1120
libssh2_agent_disconnect(LIBSSH2_AGENT *agent);
1123
* libssh2_agent_free()
1125
* Free an ssh-agent handle. This function also frees the internal
1126
* collection of public keys.
1129
libssh2_agent_free(LIBSSH2_AGENT *agent);
1133
* libssh2_keepalive_config()
1135
* Set how often keepalive messages should be sent. WANT_REPLY
1136
* indicates whether the keepalive messages should request a response
1137
* from the server. INTERVAL is number of seconds that can pass
1138
* without any I/O, use 0 (the default) to disable keepalives. To
1139
* avoid some busy-loop corner-cases, if you specify an interval of 1
1140
* it will be treated as 2.
1142
* Note that non-blocking applications are responsible for sending the
1143
* keepalive messages using libssh2_keepalive_send().
1145
LIBSSH2_API void libssh2_keepalive_config (LIBSSH2_SESSION *session,
1150
* libssh2_keepalive_send()
1152
* Send a keepalive message if needed. SECONDS_TO_NEXT indicates how
1153
* many seconds you can sleep after this call before you need to call
1154
* it again. Returns 0 on success, or LIBSSH2_ERROR_SOCKET_SEND on
1157
LIBSSH2_API int libssh2_keepalive_send (LIBSSH2_SESSION *session,
1158
int *seconds_to_next);
1161
libssh2_trace() has no function in builds that aren't built with debug
1164
LIBSSH2_API int libssh2_trace(LIBSSH2_SESSION *session, int bitmask);
1165
#define LIBSSH2_TRACE_TRANS (1<<1)
1166
#define LIBSSH2_TRACE_KEX (1<<2)
1167
#define LIBSSH2_TRACE_AUTH (1<<3)
1168
#define LIBSSH2_TRACE_CONN (1<<4)
1169
#define LIBSSH2_TRACE_SCP (1<<5)
1170
#define LIBSSH2_TRACE_SFTP (1<<6)
1171
#define LIBSSH2_TRACE_ERROR (1<<7)
1172
#define LIBSSH2_TRACE_PUBLICKEY (1<<8)
1173
#define LIBSSH2_TRACE_SOCKET (1<<9)
1175
typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*,
1179
LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session,
1181
libssh2_trace_handler_func callback);
1187
#endif /* !RC_INVOKED */
1189
#endif /* LIBSSH2_H */