1
/* Copyright (c) 2004-2009, Sara Golemon <sarag@libssh2.org>
2
* Copyright (c) 2009 by 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
50
#include <sys/types.h>
52
/* Allow alternate API prefix from CFLAGS or calling app */
55
# ifdef LIBSSH2_LIBRARY
56
# define LIBSSH2_API __declspec(dllexport)
58
# define LIBSSH2_API __declspec(dllimport)
59
# endif /* LIBSSH2_LIBRARY */
60
# else /* !LIBSSH2_WIN32 */
62
# endif /* LIBSSH2_WIN32 */
63
#endif /* LIBSSH2_API */
65
#if defined(LIBSSH2_DARWIN) || (defined(LIBSSH2_WIN32) && \
66
!defined(_MSC_VER) && !defined(__MINGW32__))
70
#if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
71
# include <sys/bsdskt.h>
72
typedef unsigned char uint8_t;
73
typedef unsigned int uint32_t;
77
typedef unsigned char uint8_t;
78
typedef unsigned int uint32_t;
79
typedef unsigned __int64 libssh2_uint64_t;
80
typedef __int64 libssh2_int64_t;
81
# ifndef _SSIZE_T_DEFINED
83
# define _SSIZE_T_DEFINED
86
typedef unsigned long long libssh2_uint64_t;
87
typedef long long libssh2_int64_t;
90
/* We use underscore instead of dash when appending DEV in dev versions just
91
to make the BANNER define (used by src/session.c) be a valid SSH
92
banner. Release versions have no appended strings and may of course not
93
have dashes either. */
94
#define LIBSSH2_VERSION "1.2.7-20100810"
96
/* The numeric version number is also available "in parts" by using these
98
#define LIBSSH2_VERSION_MAJOR 1
99
#define LIBSSH2_VERSION_MINOR 2
100
#define LIBSSH2_VERSION_PATCH 7
102
/* This is the numeric version of the libssh2 version number, meant for easier
103
parsing and comparions by programs. The LIBSSH2_VERSION_NUM define will
104
always follow this syntax:
108
Where XX, YY and ZZ are the main version, release and patch numbers in
109
hexadecimal (using 8 bits each). All three numbers are always represented
110
using two digits. 1.2 would appear as "0x010200" while version 9.11.7
111
appears as "0x090b07".
113
This 6-digit (24 bits) hexadecimal number does not show pre-release number,
114
and it is always a greater number in a more recent release. It makes
115
comparisons with greater than and less than work.
117
#define LIBSSH2_VERSION_NUM 0x010207
120
* This is the date and time when the full source package was created. The
121
* timestamp is not stored in the source code repo, as the timestamp is
122
* properly set in the tarballs by the maketgz script.
124
* The format of the date should follow this template:
126
* "Mon Feb 12 11:35:33 UTC 2007"
128
#define LIBSSH2_TIMESTAMP "Tue Aug 10 04:06:23 UTC 2010"
130
/* Part of every banner, user specified or not */
131
#define LIBSSH2_SSH_BANNER "SSH-2.0-libssh2_" LIBSSH2_VERSION
133
/* We *could* add a comment here if we so chose */
134
#define LIBSSH2_SSH_DEFAULT_BANNER LIBSSH2_SSH_BANNER
135
#define LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF LIBSSH2_SSH_DEFAULT_BANNER "\r\n"
137
/* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 */
138
#define LIBSSH2_DH_GEX_MINGROUP 1024
139
#define LIBSSH2_DH_GEX_OPTGROUP 1536
140
#define LIBSSH2_DH_GEX_MAXGROUP 2048
142
/* Defaults for pty requests */
143
#define LIBSSH2_TERM_WIDTH 80
144
#define LIBSSH2_TERM_HEIGHT 24
145
#define LIBSSH2_TERM_WIDTH_PX 0
146
#define LIBSSH2_TERM_HEIGHT_PX 0
149
#define LIBSSH2_SOCKET_POLL_UDELAY 250000
150
/* 0.25 * 120 == 30 seconds */
151
#define LIBSSH2_SOCKET_POLL_MAXLOOPS 120
153
/* Maximum size to allow a payload to compress to, plays it safe by falling
154
short of spec limits */
155
#define LIBSSH2_PACKET_MAXCOMP 32000
157
/* Maximum size to allow a payload to deccompress to, plays it safe by
158
allowing more than spec requires */
159
#define LIBSSH2_PACKET_MAXDECOMP 40000
161
/* Maximum size for an inbound compressed payload, plays it safe by
162
overshooting spec limits */
163
#define LIBSSH2_PACKET_MAXPAYLOAD 40000
165
/* Malloc callbacks */
166
#define LIBSSH2_ALLOC_FUNC(name) void *name(size_t count, void **abstract)
167
#define LIBSSH2_REALLOC_FUNC(name) void *name(void *ptr, size_t count, \
169
#define LIBSSH2_FREE_FUNC(name) void name(void *ptr, void **abstract)
171
typedef struct _LIBSSH2_USERAUTH_KBDINT_PROMPT
176
} LIBSSH2_USERAUTH_KBDINT_PROMPT;
178
typedef struct _LIBSSH2_USERAUTH_KBDINT_RESPONSE
182
} LIBSSH2_USERAUTH_KBDINT_RESPONSE;
184
/* 'publickey' authentication callback */
185
#define LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC(name) \
186
int name(LIBSSH2_SESSION *session, unsigned char **sig, size_t *sig_len, \
187
const unsigned char *data, size_t data_len, void **abstract)
189
/* 'keyboard-interactive' authentication callback */
190
#define LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC(name_) \
191
void name_(const char* name, int name_len, const char* instruction, \
192
int instruction_len, int num_prompts, \
193
const LIBSSH2_USERAUTH_KBDINT_PROMPT* prompts, \
194
LIBSSH2_USERAUTH_KBDINT_RESPONSE* responses, void **abstract)
196
/* Callbacks for special SSH packets */
197
#define LIBSSH2_IGNORE_FUNC(name) \
198
void name(LIBSSH2_SESSION *session, const char *message, int message_len, \
201
#define LIBSSH2_DEBUG_FUNC(name) \
202
void name(LIBSSH2_SESSION *session, int always_display, const char *message, \
203
int message_len, const char *language, int language_len, \
206
#define LIBSSH2_DISCONNECT_FUNC(name) \
207
void name(LIBSSH2_SESSION *session, int reason, const char *message, \
208
int message_len, const char *language, int language_len, \
211
#define LIBSSH2_PASSWD_CHANGEREQ_FUNC(name) \
212
void name(LIBSSH2_SESSION *session, char **newpw, int *newpw_len, \
215
#define LIBSSH2_MACERROR_FUNC(name) \
216
int name(LIBSSH2_SESSION *session, const char *packet, int packet_len, \
219
#define LIBSSH2_X11_OPEN_FUNC(name) \
220
void name(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, \
221
const char *shost, int sport, void **abstract)
223
#define LIBSSH2_CHANNEL_CLOSE_FUNC(name) \
224
void name(LIBSSH2_SESSION *session, void **session_abstract, \
225
LIBSSH2_CHANNEL *channel, void **channel_abstract)
227
/* libssh2_session_callback_set() constants */
228
#define LIBSSH2_CALLBACK_IGNORE 0
229
#define LIBSSH2_CALLBACK_DEBUG 1
230
#define LIBSSH2_CALLBACK_DISCONNECT 2
231
#define LIBSSH2_CALLBACK_MACERROR 3
232
#define LIBSSH2_CALLBACK_X11 4
234
/* libssh2_session_method_pref() constants */
235
#define LIBSSH2_METHOD_KEX 0
236
#define LIBSSH2_METHOD_HOSTKEY 1
237
#define LIBSSH2_METHOD_CRYPT_CS 2
238
#define LIBSSH2_METHOD_CRYPT_SC 3
239
#define LIBSSH2_METHOD_MAC_CS 4
240
#define LIBSSH2_METHOD_MAC_SC 5
241
#define LIBSSH2_METHOD_COMP_CS 6
242
#define LIBSSH2_METHOD_COMP_SC 7
243
#define LIBSSH2_METHOD_LANG_CS 8
244
#define LIBSSH2_METHOD_LANG_SC 9
246
/* session.flags bits */
247
#define LIBSSH2_FLAG_SIGPIPE 0x00000001
249
typedef struct _LIBSSH2_SESSION LIBSSH2_SESSION;
250
typedef struct _LIBSSH2_CHANNEL LIBSSH2_CHANNEL;
251
typedef struct _LIBSSH2_LISTENER LIBSSH2_LISTENER;
252
typedef struct _LIBSSH2_KNOWNHOSTS LIBSSH2_KNOWNHOSTS;
253
typedef struct _LIBSSH2_AGENT LIBSSH2_AGENT;
255
typedef struct _LIBSSH2_POLLFD {
256
unsigned char type; /* LIBSSH2_POLLFD_* below */
259
int socket; /* File descriptors -- examined with system select() call */
260
LIBSSH2_CHANNEL *channel; /* Examined by checking internal state */
261
LIBSSH2_LISTENER *listener; /* Read polls only -- are inbound
262
connections waiting to be accepted? */
265
unsigned long events; /* Requested Events */
266
unsigned long revents; /* Returned Events */
269
/* Poll FD Descriptor Types */
270
#define LIBSSH2_POLLFD_SOCKET 1
271
#define LIBSSH2_POLLFD_CHANNEL 2
272
#define LIBSSH2_POLLFD_LISTENER 3
274
/* Note: Win32 Doesn't actually have a poll() implementation, so some of these
275
values are faked with select() data */
276
/* Poll FD events/revents -- Match sys/poll.h where possible */
277
#define LIBSSH2_POLLFD_POLLIN 0x0001 /* Data available to be read or
278
connection available --
280
#define LIBSSH2_POLLFD_POLLPRI 0x0002 /* Priority data available to
281
be read -- Socket only */
282
#define LIBSSH2_POLLFD_POLLEXT 0x0002 /* Extended data available to
283
be read -- Channel only */
284
#define LIBSSH2_POLLFD_POLLOUT 0x0004 /* Can may be written --
287
#define LIBSSH2_POLLFD_POLLERR 0x0008 /* Error Condition -- Socket */
288
#define LIBSSH2_POLLFD_POLLHUP 0x0010 /* HangUp/EOF -- Socket */
289
#define LIBSSH2_POLLFD_SESSION_CLOSED 0x0010 /* Session Disconnect */
290
#define LIBSSH2_POLLFD_POLLNVAL 0x0020 /* Invalid request -- Socket
292
#define LIBSSH2_POLLFD_POLLEX 0x0040 /* Exception Condition --
294
#define LIBSSH2_POLLFD_CHANNEL_CLOSED 0x0080 /* Channel Disconnect */
295
#define LIBSSH2_POLLFD_LISTENER_CLOSED 0x0080 /* Listener Disconnect */
297
#define HAVE_LIBSSH2_SESSION_BLOCK_DIRECTION
298
/* Block Direction Types */
299
#define LIBSSH2_SESSION_BLOCK_INBOUND 0x0001
300
#define LIBSSH2_SESSION_BLOCK_OUTBOUND 0x0002
303
#define LIBSSH2_HOSTKEY_HASH_MD5 1
304
#define LIBSSH2_HOSTKEY_HASH_SHA1 2
307
#define LIBSSH2_HOSTKEY_TYPE_UNKNOWN 0
308
#define LIBSSH2_HOSTKEY_TYPE_RSA 1
309
#define LIBSSH2_HOSTKEY_TYPE_DSS 2
311
/* Disconnect Codes (defined by SSH protocol) */
312
#define SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1
313
#define SSH_DISCONNECT_PROTOCOL_ERROR 2
314
#define SSH_DISCONNECT_KEY_EXCHANGE_FAILED 3
315
#define SSH_DISCONNECT_RESERVED 4
316
#define SSH_DISCONNECT_MAC_ERROR 5
317
#define SSH_DISCONNECT_COMPRESSION_ERROR 6
318
#define SSH_DISCONNECT_SERVICE_NOT_AVAILABLE 7
319
#define SSH_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED 8
320
#define SSH_DISCONNECT_HOST_KEY_NOT_VERIFIABLE 9
321
#define SSH_DISCONNECT_CONNECTION_LOST 10
322
#define SSH_DISCONNECT_BY_APPLICATION 11
323
#define SSH_DISCONNECT_TOO_MANY_CONNECTIONS 12
324
#define SSH_DISCONNECT_AUTH_CANCELLED_BY_USER 13
325
#define SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE 14
326
#define SSH_DISCONNECT_ILLEGAL_USER_NAME 15
328
/* Error Codes (defined by libssh2) */
329
#define LIBSSH2_ERROR_NONE 0
330
#define LIBSSH2_ERROR_SOCKET_NONE -1
331
#define LIBSSH2_ERROR_BANNER_NONE -2
332
#define LIBSSH2_ERROR_BANNER_SEND -3
333
#define LIBSSH2_ERROR_INVALID_MAC -4
334
#define LIBSSH2_ERROR_KEX_FAILURE -5
335
#define LIBSSH2_ERROR_ALLOC -6
336
#define LIBSSH2_ERROR_SOCKET_SEND -7
337
#define LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE -8
338
#define LIBSSH2_ERROR_TIMEOUT -9
339
#define LIBSSH2_ERROR_HOSTKEY_INIT -10
340
#define LIBSSH2_ERROR_HOSTKEY_SIGN -11
341
#define LIBSSH2_ERROR_DECRYPT -12
342
#define LIBSSH2_ERROR_SOCKET_DISCONNECT -13
343
#define LIBSSH2_ERROR_PROTO -14
344
#define LIBSSH2_ERROR_PASSWORD_EXPIRED -15
345
#define LIBSSH2_ERROR_FILE -16
346
#define LIBSSH2_ERROR_METHOD_NONE -17
347
#define LIBSSH2_ERROR_AUTHENTICATION_FAILED -18
348
#define LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED LIBSSH2_ERROR_AUTHENTICATION_FAILED
349
#define LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED -19
350
#define LIBSSH2_ERROR_CHANNEL_OUTOFORDER -20
351
#define LIBSSH2_ERROR_CHANNEL_FAILURE -21
352
#define LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED -22
353
#define LIBSSH2_ERROR_CHANNEL_UNKNOWN -23
354
#define LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED -24
355
#define LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED -25
356
#define LIBSSH2_ERROR_CHANNEL_CLOSED -26
357
#define LIBSSH2_ERROR_CHANNEL_EOF_SENT -27
358
#define LIBSSH2_ERROR_SCP_PROTOCOL -28
359
#define LIBSSH2_ERROR_ZLIB -29
360
#define LIBSSH2_ERROR_SOCKET_TIMEOUT -30
361
#define LIBSSH2_ERROR_SFTP_PROTOCOL -31
362
#define LIBSSH2_ERROR_REQUEST_DENIED -32
363
#define LIBSSH2_ERROR_METHOD_NOT_SUPPORTED -33
364
#define LIBSSH2_ERROR_INVAL -34
365
#define LIBSSH2_ERROR_INVALID_POLL_TYPE -35
366
#define LIBSSH2_ERROR_PUBLICKEY_PROTOCOL -36
367
#define LIBSSH2_ERROR_EAGAIN -37
368
#define LIBSSH2_ERROR_BUFFER_TOO_SMALL -38
369
#define LIBSSH2_ERROR_BAD_USE -39
370
#define LIBSSH2_ERROR_COMPRESS -40
371
#define LIBSSH2_ERROR_OUT_OF_BOUNDARY -41
372
#define LIBSSH2_ERROR_AGENT_PROTOCOL -42
375
#define LIBSSH2_INIT_NO_CRYPTO 0x0001
380
* Initialize the libssh2 functions. This typically initialize the
381
* crypto library. It uses a global state, and is not thread safe --
382
* you must make sure this function is not called concurrently.
385
* 0: Normal initialize
386
* LIBSSH2_INIT_NO_CRYPTO: Do not initialize the crypto library (ie.
387
* OPENSSL_add_cipher_algoritms() for OpenSSL
389
* Returns 0 if succeeded, or a negative value for error.
391
LIBSSH2_API int libssh2_init(int flags);
396
* Exit the libssh2 functions and free's all memory used internal.
398
LIBSSH2_API void libssh2_exit(void);
401
LIBSSH2_API LIBSSH2_SESSION *
402
libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC((*my_alloc)),
403
LIBSSH2_FREE_FUNC((*my_free)),
404
LIBSSH2_REALLOC_FUNC((*my_realloc)), void *abstract);
405
#define libssh2_session_init() libssh2_session_init_ex(NULL, NULL, NULL, NULL)
407
LIBSSH2_API void **libssh2_session_abstract(LIBSSH2_SESSION *session);
409
LIBSSH2_API void *libssh2_session_callback_set(LIBSSH2_SESSION *session,
410
int cbtype, void *callback);
411
LIBSSH2_API int libssh2_banner_set(LIBSSH2_SESSION *session,
414
LIBSSH2_API int libssh2_session_startup(LIBSSH2_SESSION *session, int sock);
415
LIBSSH2_API int libssh2_session_disconnect_ex(LIBSSH2_SESSION *session,
417
const char *description,
419
#define libssh2_session_disconnect(session, description) \
420
libssh2_session_disconnect_ex((session), SSH_DISCONNECT_BY_APPLICATION, \
423
LIBSSH2_API int libssh2_session_free(LIBSSH2_SESSION *session);
425
LIBSSH2_API const char *libssh2_hostkey_hash(LIBSSH2_SESSION *session,
428
LIBSSH2_API const char *libssh2_session_hostkey(LIBSSH2_SESSION *session,
429
size_t *len, int *type);
431
LIBSSH2_API int libssh2_session_method_pref(LIBSSH2_SESSION *session,
434
LIBSSH2_API const char *libssh2_session_methods(LIBSSH2_SESSION *session,
436
LIBSSH2_API int libssh2_session_last_error(LIBSSH2_SESSION *session,
438
int *errmsg_len, int want_buf);
439
LIBSSH2_API int libssh2_session_last_errno(LIBSSH2_SESSION *session);
440
LIBSSH2_API int libssh2_session_block_directions(LIBSSH2_SESSION *session);
442
LIBSSH2_API int libssh2_session_flag(LIBSSH2_SESSION *session, int flag,
446
LIBSSH2_API char *libssh2_userauth_list(LIBSSH2_SESSION *session,
447
const char *username,
448
unsigned int username_len);
449
LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session);
451
LIBSSH2_API int libssh2_userauth_password_ex(LIBSSH2_SESSION *session,
452
const char *username,
453
unsigned int username_len,
454
const char *password,
455
unsigned int password_len,
456
LIBSSH2_PASSWD_CHANGEREQ_FUNC((*passwd_change_cb)));
458
#define libssh2_userauth_password(session, username, password) \
459
libssh2_userauth_password_ex((session), (username), strlen(username), \
460
(password), strlen(password), NULL)
463
libssh2_userauth_publickey_fromfile_ex(LIBSSH2_SESSION *session,
464
const char *username,
465
unsigned int username_len,
466
const char *publickey,
467
const char *privatekey,
468
const char *passphrase);
470
#define libssh2_userauth_publickey_fromfile(session, username, publickey, \
471
privatekey, passphrase) \
472
libssh2_userauth_publickey_fromfile_ex((session), (username), \
473
strlen(username), (publickey), \
474
(privatekey), (passphrase))
477
libssh2_userauth_publickey(LIBSSH2_SESSION *session,
478
const char *username,
479
const unsigned char *pubkeydata,
480
size_t pubkeydata_len,
481
LIBSSH2_USERAUTH_PUBLICKEY_SIGN_FUNC((*sign_callback)),
485
libssh2_userauth_hostbased_fromfile_ex(LIBSSH2_SESSION *session,
486
const char *username,
487
unsigned int username_len,
488
const char *publickey,
489
const char *privatekey,
490
const char *passphrase,
491
const char *hostname,
492
unsigned int hostname_len,
493
const char *local_username,
494
unsigned int local_username_len);
496
#define libssh2_userauth_hostbased_fromfile(session, username, publickey, \
497
privatekey, passphrase, hostname) \
498
libssh2_userauth_hostbased_fromfile_ex((session), (username), \
499
strlen(username), (publickey), \
500
(privatekey), (passphrase), \
501
(hostname), strlen(hostname), \
502
(username), strlen(username))
505
* response_callback is provided with filled by library prompts array,
506
* but client must allocate and fill individual responses. Responses
507
* array is already allocated. Responses data will be freed by libssh2
508
* after callback return, but before subsequent callback invokation.
511
libssh2_userauth_keyboard_interactive_ex(LIBSSH2_SESSION* session,
512
const char *username,
513
unsigned int username_len,
514
LIBSSH2_USERAUTH_KBDINT_RESPONSE_FUNC((*response_callback)));
516
#define libssh2_userauth_keyboard_interactive(session, username, \
518
libssh2_userauth_keyboard_interactive_ex((session), (username), \
519
strlen(username), (response_callback))
521
LIBSSH2_API int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds,
525
#define LIBSSH2_CHANNEL_WINDOW_DEFAULT 65536
526
#define LIBSSH2_CHANNEL_PACKET_DEFAULT 32768
527
#define LIBSSH2_CHANNEL_MINADJUST 1024
529
/* Extended Data Handling */
530
#define LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL 0
531
#define LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE 1
532
#define LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE 2
534
#define SSH_EXTENDED_DATA_STDERR 1
536
/* Returned by any function that would block during a read/write opperation */
537
#define LIBSSH2CHANNEL_EAGAIN LIBSSH2_ERROR_EAGAIN
539
LIBSSH2_API LIBSSH2_CHANNEL *
540
libssh2_channel_open_ex(LIBSSH2_SESSION *session, const char *channel_type,
541
unsigned int channel_type_len,
542
unsigned int window_size, unsigned int packet_size,
543
const char *message, unsigned int message_len);
545
#define libssh2_channel_open_session(session) \
546
libssh2_channel_open_ex((session), "session", sizeof("session") - 1, \
547
LIBSSH2_CHANNEL_WINDOW_DEFAULT, \
548
LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0)
550
LIBSSH2_API LIBSSH2_CHANNEL *
551
libssh2_channel_direct_tcpip_ex(LIBSSH2_SESSION *session, const char *host,
552
int port, const char *shost, int sport);
553
#define libssh2_channel_direct_tcpip(session, host, port) \
554
libssh2_channel_direct_tcpip_ex((session), (host), (port), "127.0.0.1", 22)
556
LIBSSH2_API LIBSSH2_LISTENER *
557
libssh2_channel_forward_listen_ex(LIBSSH2_SESSION *session, const char *host,
558
int port, int *bound_port, int queue_maxsize);
559
#define libssh2_channel_forward_listen(session, port) \
560
libssh2_channel_forward_listen_ex((session), NULL, (port), NULL, 16)
562
LIBSSH2_API int libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener);
564
LIBSSH2_API LIBSSH2_CHANNEL *
565
libssh2_channel_forward_accept(LIBSSH2_LISTENER *listener);
567
LIBSSH2_API int libssh2_channel_setenv_ex(LIBSSH2_CHANNEL *channel,
569
unsigned int varname_len,
571
unsigned int value_len);
573
#define libssh2_channel_setenv(channel, varname, value) \
574
libssh2_channel_setenv_ex((channel), (varname), strlen(varname), (value), \
577
LIBSSH2_API int libssh2_channel_request_pty_ex(LIBSSH2_CHANNEL *channel,
579
unsigned int term_len,
581
unsigned int modes_len,
582
int width, int height,
583
int width_px, int height_px);
584
#define libssh2_channel_request_pty(channel, term) \
585
libssh2_channel_request_pty_ex((channel), (term), strlen(term), NULL, 0, \
586
LIBSSH2_TERM_WIDTH, LIBSSH2_TERM_HEIGHT, \
587
LIBSSH2_TERM_WIDTH_PX, LIBSSH2_TERM_HEIGHT_PX)
589
LIBSSH2_API int libssh2_channel_request_pty_size_ex(LIBSSH2_CHANNEL *channel,
590
int width, int height,
593
#define libssh2_channel_request_pty_size(channel, width, height) \
594
libssh2_channel_request_pty_size_ex( (channel), (width), (height), 0, 0)
596
LIBSSH2_API int libssh2_channel_x11_req_ex(LIBSSH2_CHANNEL *channel,
597
int single_connection,
598
const char *auth_proto,
599
const char *auth_cookie,
601
#define libssh2_channel_x11_req(channel, screen_number) \
602
libssh2_channel_x11_req_ex((channel), 0, NULL, NULL, (screen_number))
604
LIBSSH2_API int libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel,
606
unsigned int request_len,
608
unsigned int message_len);
609
#define libssh2_channel_shell(channel) \
610
libssh2_channel_process_startup((channel), "shell", sizeof("shell") - 1, \
612
#define libssh2_channel_exec(channel, command) \
613
libssh2_channel_process_startup((channel), "exec", sizeof("exec") - 1, \
614
(command), strlen(command))
615
#define libssh2_channel_subsystem(channel, subsystem) \
616
libssh2_channel_process_startup((channel), "subsystem", \
617
sizeof("subsystem") - 1, (subsystem), \
620
LIBSSH2_API ssize_t libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel,
621
int stream_id, char *buf,
623
#define libssh2_channel_read(channel, buf, buflen) \
624
libssh2_channel_read_ex((channel), 0, (buf), (buflen))
625
#define libssh2_channel_read_stderr(channel, buf, buflen) \
626
libssh2_channel_read_ex((channel), SSH_EXTENDED_DATA_STDERR, (buf), (buflen))
628
LIBSSH2_API int libssh2_poll_channel_read(LIBSSH2_CHANNEL *channel,
631
LIBSSH2_API unsigned long
632
libssh2_channel_window_read_ex(LIBSSH2_CHANNEL *channel,
633
unsigned long *read_avail,
634
unsigned long *window_size_initial);
635
#define libssh2_channel_window_read(channel) \
636
libssh2_channel_window_read_ex((channel), NULL, NULL)
638
/* libssh2_channel_receive_window_adjust is DEPRECATED, do not use! */
639
LIBSSH2_API unsigned long
640
libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel,
641
unsigned long adjustment,
642
unsigned char force);
645
libssh2_channel_receive_window_adjust2(LIBSSH2_CHANNEL *channel,
646
unsigned long adjustment,
648
unsigned int *storewindow);
650
LIBSSH2_API ssize_t libssh2_channel_write_ex(LIBSSH2_CHANNEL *channel,
651
int stream_id, const char *buf,
654
#define libssh2_channel_write(channel, buf, buflen) \
655
libssh2_channel_write_ex((channel), 0, (buf), (buflen))
656
#define libssh2_channel_write_stderr(channel, buf, buflen) \
657
libssh2_channel_write_ex((channel), SSH_EXTENDED_DATA_STDERR, (buf), (buflen))
659
LIBSSH2_API unsigned long
660
libssh2_channel_window_write_ex(LIBSSH2_CHANNEL *channel,
661
unsigned long *window_size_initial);
662
#define libssh2_channel_window_write(channel) \
663
libssh2_channel_window_write_ex((channel), NULL)
665
LIBSSH2_API void libssh2_session_set_blocking(LIBSSH2_SESSION* session,
667
LIBSSH2_API int libssh2_session_get_blocking(LIBSSH2_SESSION* session);
669
LIBSSH2_API void libssh2_channel_set_blocking(LIBSSH2_CHANNEL *channel,
672
/* libssh2_channel_handle_extended_data is DEPRECATED, do not use! */
673
LIBSSH2_API void libssh2_channel_handle_extended_data(LIBSSH2_CHANNEL *channel,
675
LIBSSH2_API int libssh2_channel_handle_extended_data2(LIBSSH2_CHANNEL *channel,
678
/* libssh2_channel_ignore_extended_data() is defined below for BC with version
681
* Future uses should use libssh2_channel_handle_extended_data() directly if
682
* LIBSSH2_CHANNEL_EXTENDED_DATA_MERGE is passed, extended data will be read
683
* (FIFO) from the standard data channel
686
#define libssh2_channel_ignore_extended_data(channel, ignore) \
687
libssh2_channel_handle_extended_data((channel), \
689
LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE : \
690
LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL )
692
#define LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA -1
693
#define LIBSSH2_CHANNEL_FLUSH_ALL -2
694
LIBSSH2_API int libssh2_channel_flush_ex(LIBSSH2_CHANNEL *channel,
696
#define libssh2_channel_flush(channel) libssh2_channel_flush_ex((channel), 0)
697
#define libssh2_channel_flush_stderr(channel) \
698
libssh2_channel_flush_ex((channel), SSH_EXTENDED_DATA_STDERR)
700
LIBSSH2_API int libssh2_channel_get_exit_status(LIBSSH2_CHANNEL* channel);
701
LIBSSH2_API int libssh2_channel_send_eof(LIBSSH2_CHANNEL *channel);
702
LIBSSH2_API int libssh2_channel_eof(LIBSSH2_CHANNEL *channel);
703
LIBSSH2_API int libssh2_channel_wait_eof(LIBSSH2_CHANNEL *channel);
704
LIBSSH2_API int libssh2_channel_close(LIBSSH2_CHANNEL *channel);
705
LIBSSH2_API int libssh2_channel_wait_closed(LIBSSH2_CHANNEL *channel);
706
LIBSSH2_API int libssh2_channel_free(LIBSSH2_CHANNEL *channel);
708
LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_recv(LIBSSH2_SESSION *session,
711
LIBSSH2_API LIBSSH2_CHANNEL *libssh2_scp_send_ex(LIBSSH2_SESSION *session,
712
const char *path, int mode,
713
size_t size, long mtime,
715
LIBSSH2_API LIBSSH2_CHANNEL *
716
libssh2_scp_send64(LIBSSH2_SESSION *session, const char *path, int mode,
717
libssh2_int64_t size, time_t mtime, time_t atime);
719
#define libssh2_scp_send(session, path, mode, size) \
720
libssh2_scp_send_ex((session), (path), (mode), (size), 0, 0)
722
LIBSSH2_API int libssh2_base64_decode(LIBSSH2_SESSION *session, char **dest,
723
unsigned int *dest_len,
724
const char *src, unsigned int src_len);
727
const char *libssh2_version(int req_version_num);
729
#define HAVE_LIBSSH2_KNOWNHOST_API 0x010101 /* since 1.1.1 */
730
#define HAVE_LIBSSH2_VERSION_API 0x010100 /* libssh2_version since 1.1 */
732
struct libssh2_knownhost {
733
unsigned int magic; /* magic stored by the library */
734
void *node; /* handle to the internal representation of this host */
735
char *name; /* this is NULL if no plain text host name exists */
736
char *key; /* key in base64/printable format */
741
* libssh2_knownhost_init
743
* Init a collection of known hosts. Returns the pointer to a collection.
746
LIBSSH2_API LIBSSH2_KNOWNHOSTS *
747
libssh2_knownhost_init(LIBSSH2_SESSION *session);
750
* libssh2_knownhost_add
752
* Add a host and its associated key to the collection of known hosts.
754
* The 'type' argument specifies on what format the given host and keys are:
756
* plain - ascii "hostname.domain.tld"
757
* sha1 - SHA1(<salt> <host>) base64-encoded!
758
* custom - another hash
760
* If 'sha1' is selected as type, the salt must be provided to the salt
761
* argument. This too base64 encoded.
763
* The SHA-1 hash is what OpenSSH can be told to use in known_hosts files. If
764
* a custom type is used, salt is ignored and you must provide the host
765
* pre-hashed when checking for it in the libssh2_knownhost_check() function.
767
* The keylen parameter may be omitted (zero) if the key is provided as a
768
* NULL-terminated base64-encoded string.
771
/* host format (2 bits) */
772
#define LIBSSH2_KNOWNHOST_TYPE_MASK 0xffff
773
#define LIBSSH2_KNOWNHOST_TYPE_PLAIN 1
774
#define LIBSSH2_KNOWNHOST_TYPE_SHA1 2 /* always base64 encoded */
775
#define LIBSSH2_KNOWNHOST_TYPE_CUSTOM 3
777
/* key format (2 bits) */
778
#define LIBSSH2_KNOWNHOST_KEYENC_MASK (3<<16)
779
#define LIBSSH2_KNOWNHOST_KEYENC_RAW (1<<16)
780
#define LIBSSH2_KNOWNHOST_KEYENC_BASE64 (2<<16)
782
/* type of key (2 bits) */
783
#define LIBSSH2_KNOWNHOST_KEY_MASK (3<<18)
784
#define LIBSSH2_KNOWNHOST_KEY_SHIFT 18
785
#define LIBSSH2_KNOWNHOST_KEY_RSA1 (1<<18)
786
#define LIBSSH2_KNOWNHOST_KEY_SSHRSA (2<<18)
787
#define LIBSSH2_KNOWNHOST_KEY_SSHDSS (3<<18)
790
libssh2_knownhost_add(LIBSSH2_KNOWNHOSTS *hosts,
793
const char *key, size_t keylen, int typemask,
794
struct libssh2_knownhost **store);
797
* libssh2_knownhost_addc
799
* Add a host and its associated key to the collection of known hosts.
801
* Takes a comment argument that may be NULL. A NULL comment indicates
802
* there is no comment and the entry will end directly after the key
803
* when written out to a file. An empty string "" comment will indicate an
804
* empty comment which will cause a single space to be written after the key.
806
* The 'type' argument specifies on what format the given host and keys are:
808
* plain - ascii "hostname.domain.tld"
809
* sha1 - SHA1(<salt> <host>) base64-encoded!
810
* custom - another hash
812
* If 'sha1' is selected as type, the salt must be provided to the salt
813
* argument. This too base64 encoded.
815
* The SHA-1 hash is what OpenSSH can be told to use in known_hosts files. If
816
* a custom type is used, salt is ignored and you must provide the host
817
* pre-hashed when checking for it in the libssh2_knownhost_check() function.
819
* The keylen parameter may be omitted (zero) if the key is provided as a
820
* NULL-terminated base64-encoded string.
824
libssh2_knownhost_addc(LIBSSH2_KNOWNHOSTS *hosts,
827
const char *key, size_t keylen,
828
const char *comment, size_t commentlen, int typemask,
829
struct libssh2_knownhost **store);
832
* libssh2_knownhost_check
834
* Check a host and its associated key against the collection of known hosts.
836
* The type is the type/format of the given host name.
838
* plain - ascii "hostname.domain.tld"
839
* custom - prehashed base64 encoded. Note that this cannot use any salts.
842
* 'knownhost' may be set to NULL if you don't care about that info.
846
* LIBSSH2_KNOWNHOST_CHECK_* values, see below
850
#define LIBSSH2_KNOWNHOST_CHECK_MATCH 0
851
#define LIBSSH2_KNOWNHOST_CHECK_MISMATCH 1
852
#define LIBSSH2_KNOWNHOST_CHECK_NOTFOUND 2
853
#define LIBSSH2_KNOWNHOST_CHECK_FAILURE 3
856
libssh2_knownhost_check(LIBSSH2_KNOWNHOSTS *hosts,
857
const char *host, const char *key, size_t keylen,
859
struct libssh2_knownhost **knownhost);
861
/* this function is identital to the above one, but also takes a port
862
argument that allows libssh2 to do a better check */
864
libssh2_knownhost_checkp(LIBSSH2_KNOWNHOSTS *hosts,
865
const char *host, int port,
866
const char *key, size_t keylen,
868
struct libssh2_knownhost **knownhost);
871
* libssh2_knownhost_del
873
* Remove a host from the collection of known hosts. The 'entry' struct is
874
* retrieved by a call to libssh2_knownhost_check().
878
libssh2_knownhost_del(LIBSSH2_KNOWNHOSTS *hosts,
879
struct libssh2_knownhost *entry);
882
* libssh2_knownhost_free
884
* Free an entire collection of known hosts.
888
libssh2_knownhost_free(LIBSSH2_KNOWNHOSTS *hosts);
891
* libssh2_knownhost_readline()
893
* Pass in a line of a file of 'type'. It makes libssh2 read this line.
895
* LIBSSH2_KNOWNHOST_FILE_OPENSSH is the only supported type.
899
libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS *hosts,
900
const char *line, size_t len, int type);
903
* libssh2_knownhost_readfile
905
* Add hosts+key pairs from a given file.
907
* Returns a negative value for error or number of successfully added hosts.
909
* This implementation currently only knows one 'type' (openssh), all others
910
* are reserved for future use.
913
#define LIBSSH2_KNOWNHOST_FILE_OPENSSH 1
916
libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS *hosts,
917
const char *filename, int type);
920
* libssh2_knownhost_writeline()
922
* Ask libssh2 to convert a known host to an output line for storage.
924
* Note that this function returns LIBSSH2_ERROR_BUFFER_TOO_SMALL if the given
925
* output buffer is too small to hold the desired output.
927
* This implementation currently only knows one 'type' (openssh), all others
928
* are reserved for future use.
932
libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts,
933
struct libssh2_knownhost *known,
934
char *buffer, size_t buflen,
935
size_t *outlen, /* the amount of written data */
939
* libssh2_knownhost_writefile
941
* Write hosts+key pairs to a given file.
943
* This implementation currently only knows one 'type' (openssh), all others
944
* are reserved for future use.
948
libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts,
949
const char *filename, int type);
952
* libssh2_knownhost_get()
954
* Traverse the internal list of known hosts. Pass NULL to 'prev' to get
955
* the first one. Or pass a poiner to the previously returned one to get the
959
* 0 if a fine host was stored in 'store'
961
* [negative] on errors
964
libssh2_knownhost_get(LIBSSH2_KNOWNHOSTS *hosts,
965
struct libssh2_knownhost **store,
966
struct libssh2_knownhost *prev);
968
#define HAVE_LIBSSH2_AGENT_API 0x010202 /* since 1.2.2 */
970
struct libssh2_agent_publickey {
971
unsigned int magic; /* magic stored by the library */
972
void *node; /* handle to the internal representation of key */
973
unsigned char *blob; /* public key blob */
974
size_t blob_len; /* length of the public key blob */
975
char *comment; /* comment in printable format */
981
* Init an ssh-agent handle. Returns the pointer to the handle.
984
LIBSSH2_API LIBSSH2_AGENT *
985
libssh2_agent_init(LIBSSH2_SESSION *session);
988
* libssh2_agent_connect()
990
* Connect to an ssh-agent.
992
* Returns 0 if succeeded, or a negative value for error.
995
libssh2_agent_connect(LIBSSH2_AGENT *agent);
998
* libssh2_agent_list_identities()
1000
* Request an ssh-agent to list identities.
1002
* Returns 0 if succeeded, or a negative value for error.
1005
libssh2_agent_list_identities(LIBSSH2_AGENT *agent);
1008
* libssh2_agent_get_identity()
1010
* Traverse the internal list of public keys. Pass NULL to 'prev' to get
1011
* the first one. Or pass a poiner to the previously returned one to get the
1015
* 0 if a fine public key was stored in 'store'
1016
* 1 if end of public keys
1017
* [negative] on errors
1020
libssh2_agent_get_identity(LIBSSH2_AGENT *agent,
1021
struct libssh2_agent_publickey **store,
1022
struct libssh2_agent_publickey *prev);
1025
* libssh2_agent_userauth()
1027
* Do publickey user authentication with the help of ssh-agent.
1029
* Returns 0 if succeeded, or a negative value for error.
1032
libssh2_agent_userauth(LIBSSH2_AGENT *agent,
1033
const char *username,
1034
struct libssh2_agent_publickey *identity);
1037
* libssh2_agent_disconnect()
1039
* Close a connection to an ssh-agent.
1041
* Returns 0 if succeeded, or a negative value for error.
1044
libssh2_agent_disconnect(LIBSSH2_AGENT *agent);
1047
* libssh2_agent_free()
1049
* Free an ssh-agent handle. This function also frees the internal
1050
* collection of public keys.
1053
libssh2_agent_free(LIBSSH2_AGENT *agent);
1057
* libssh2_keepalive_config()
1059
* Set how often keepalive messages should be sent. WANT_REPLY
1060
* indicates whether the keepalive messages should request a response
1061
* from the server. INTERVAL is number of seconds that can pass
1062
* without any I/O, use 0 (the default) to disable keepalives. To
1063
* avoid some busy-loop corner-cases, if you specify an interval of 1
1064
* it will be treated as 2.
1066
* Note that non-blocking applications are responsible for sending the
1067
* keepalive messages using libssh2_keepalive_send().
1069
LIBSSH2_API void libssh2_keepalive_config (LIBSSH2_SESSION *session,
1074
* libssh2_keepalive_send()
1076
* Send a keepalive message if needed. SECONDS_TO_NEXT indicates how
1077
* many seconds you can sleep after this call before you need to call
1078
* it again. Returns 0 on success, or LIBSSH2_ERROR_SOCKET_SEND on
1081
LIBSSH2_API int libssh2_keepalive_send (LIBSSH2_SESSION *session,
1082
int *seconds_to_next);
1085
libssh2_trace() has no function in builds that aren't built with debug
1088
LIBSSH2_API int libssh2_trace(LIBSSH2_SESSION *session, int bitmask);
1089
#define LIBSSH2_TRACE_TRANS (1<<1)
1090
#define LIBSSH2_TRACE_KEX (1<<2)
1091
#define LIBSSH2_TRACE_AUTH (1<<3)
1092
#define LIBSSH2_TRACE_CONN (1<<4)
1093
#define LIBSSH2_TRACE_SCP (1<<5)
1094
#define LIBSSH2_TRACE_SFTP (1<<6)
1095
#define LIBSSH2_TRACE_ERROR (1<<7)
1096
#define LIBSSH2_TRACE_PUBLICKEY (1<<8)
1097
#define LIBSSH2_TRACE_SOCKET (1<<9)
1099
typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*,
1103
LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session,
1105
libssh2_trace_handler_func callback);
1111
#endif /* LIBSSH2_H */