1
/* Copyright (c) 2004-2008, 2010, Sara Golemon <sarag@libssh2.org>
2
* Copyright (c) 2009-2011 by Daniel Stenberg
3
* Copyright (c) 2010 Simon Josefsson
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
40
#ifndef LIBSSH2_PRIV_H
41
#define LIBSSH2_PRIV_H 1
43
#define LIBSSH2_LIBRARY
44
#include "libssh2_config.h"
47
#ifndef WIN32_LEAN_AND_MEAN
48
#define WIN32_LEAN_AND_MEAN
51
#undef WIN32_LEAN_AND_MEAN
54
#ifdef HAVE_WS2TCPIP_H
61
/* The following CPP block should really only be in session.c and
62
packet.c. However, AIX have #define's for 'events' and 'revents'
63
and we are using those names in libssh2.h, so we need to include
64
the AIX headers first, to make sure all code is compiled with
65
consistent names of these fields. While arguable the best would to
66
change libssh2.h to use other names, that would break backwards
67
compatibility. For more information, see:
68
http://www.mail-archive.com/libssh2-devel%40lists.sourceforge.net/msg00003.html
69
http://www.mail-archive.com/libssh2-devel%40lists.sourceforge.net/msg00224.html
72
# include <sys/poll.h>
74
# if defined(HAVE_SELECT) && !defined(WIN32)
75
# ifdef HAVE_SYS_SELECT_H
76
# include <sys/select.h>
78
# include <sys/time.h>
79
# include <sys/types.h>
84
/* Needed for struct iovec on some platforms */
89
#ifdef HAVE_SYS_SOCKET_H
90
# include <sys/socket.h>
92
#ifdef HAVE_SYS_IOCTL_H
93
# include <sys/ioctl.h>
95
#ifdef HAVE_INTTYPES_H
100
#include "libssh2_publickey.h"
101
#include "libssh2_sftp.h"
102
#include "misc.h" /* for the linked list stuff */
111
/* Provide iovec / writev on WIN32 platform. */
119
#define inline __inline
121
static inline int writev(int sock, struct iovec *iov, int nvecs)
124
if (WSASend(sock, (LPWSABUF)iov, nvecs, &ret, 0, NULL, NULL) == 0) {
134
#ifdef HAVE_WINSOCK2_H
136
#include <winsock2.h>
138
#include <ws2tcpip.h>
141
/* "inline" keyword is valid only with C++ engine! */
142
#define inline __inline
147
/* RFC4253 section 6.1 Maximum Packet Length says:
149
* "All implementations MUST be able to process packets with
150
* uncompressed payload length of 32768 bytes or less and
151
* total packet size of 35000 bytes or less (including length,
152
* padding length, payload, padding, and MAC.)."
154
#define MAX_SSH_PACKET_LEN 35000
156
#define LIBSSH2_ALLOC(session, count) \
157
session->alloc((count), &(session)->abstract)
158
#define LIBSSH2_REALLOC(session, ptr, count) \
159
((ptr) ? session->realloc((ptr), (count), &(session)->abstract) : \
160
session->alloc((count), &(session)->abstract))
161
#define LIBSSH2_FREE(session, ptr) \
162
session->free((ptr), &(session)->abstract)
163
#define LIBSSH2_IGNORE(session, data, datalen) \
164
session->ssh_msg_ignore((session), (data), (datalen), &(session)->abstract)
165
#define LIBSSH2_DEBUG(session, always_display, message, message_len, \
166
language, language_len) \
167
session->ssh_msg_debug((session), (always_display), (message), \
168
(message_len), (language), (language_len), \
169
&(session)->abstract)
170
#define LIBSSH2_DISCONNECT(session, reason, message, message_len, \
171
language, language_len) \
172
session->ssh_msg_disconnect((session), (reason), (message), \
173
(message_len), (language), (language_len), \
174
&(session)->abstract)
176
#define LIBSSH2_MACERROR(session, data, datalen) \
177
session->macerror((session), (data), (datalen), &(session)->abstract)
178
#define LIBSSH2_X11_OPEN(channel, shost, sport) \
179
channel->session->x11(((channel)->session), (channel), \
180
(shost), (sport), (&(channel)->session->abstract))
182
#define LIBSSH2_CHANNEL_CLOSE(session, channel) \
183
channel->close_cb((session), &(session)->abstract, \
184
(channel), &(channel)->abstract)
186
#define LIBSSH2_SEND_FD(session, fd, buffer, length, flags) \
187
session->send(fd, buffer, length, flags, &session->abstract)
188
#define LIBSSH2_RECV_FD(session, fd, buffer, length, flags) \
189
session->recv(fd, buffer, length, flags, &session->abstract)
191
#define LIBSSH2_SEND(session, buffer, length, flags) \
192
LIBSSH2_SEND_FD(session, session->socket_fd, buffer, length, flags)
193
#define LIBSSH2_RECV(session, buffer, length, flags) \
194
LIBSSH2_RECV_FD(session, session->socket_fd, buffer, length, flags)
196
typedef struct _LIBSSH2_KEX_METHOD LIBSSH2_KEX_METHOD;
197
typedef struct _LIBSSH2_HOSTKEY_METHOD LIBSSH2_HOSTKEY_METHOD;
198
typedef struct _LIBSSH2_CRYPT_METHOD LIBSSH2_CRYPT_METHOD;
199
typedef struct _LIBSSH2_COMP_METHOD LIBSSH2_COMP_METHOD;
201
typedef struct _LIBSSH2_PACKET LIBSSH2_PACKET;
205
libssh2_NB_state_idle = 0,
206
libssh2_NB_state_allocated,
207
libssh2_NB_state_created,
208
libssh2_NB_state_sent,
209
libssh2_NB_state_sent1,
210
libssh2_NB_state_sent2,
211
libssh2_NB_state_sent3,
212
libssh2_NB_state_sent4,
213
libssh2_NB_state_sent5,
214
libssh2_NB_state_sent6,
215
libssh2_NB_state_sent7,
216
libssh2_NB_state_jump1,
217
libssh2_NB_state_jump2,
218
libssh2_NB_state_jump3,
219
libssh2_NB_state_jump4,
220
libssh2_NB_state_jump5
221
} libssh2_nonblocking_states;
223
typedef struct packet_require_state_t
225
libssh2_nonblocking_states state;
227
} packet_require_state_t;
229
typedef struct packet_requirev_state_t
232
} packet_requirev_state_t;
234
typedef struct kmdhgGPsha1kex_state_t
236
libssh2_nonblocking_states state;
237
unsigned char *e_packet;
238
unsigned char *s_packet;
240
unsigned char h_sig_comp[SHA_DIGEST_LENGTH];
245
_libssh2_bn_ctx *ctx;
251
unsigned char *f_value;
252
unsigned char *k_value;
253
unsigned char *h_sig;
257
libssh2_sha1_ctx exchange_hash;
258
packet_require_state_t req_state;
259
libssh2_nonblocking_states burn_state;
260
} kmdhgGPsha1kex_state_t;
262
typedef struct key_exchange_state_low_t
264
libssh2_nonblocking_states state;
265
packet_require_state_t req_state;
266
kmdhgGPsha1kex_state_t exchange_state;
267
_libssh2_bn *p; /* SSH2 defined value (p_value) */
268
_libssh2_bn *g; /* SSH2 defined value (2) */
269
unsigned char request[13];
273
} key_exchange_state_low_t;
275
typedef struct key_exchange_state_t
277
libssh2_nonblocking_states state;
278
packet_require_state_t req_state;
279
key_exchange_state_low_t key_state_low;
282
unsigned char *oldlocal;
284
} key_exchange_state_t;
286
#define FwdNotReq "Forward not requested"
288
typedef struct packet_queue_listener_state_t
290
libssh2_nonblocking_states state;
291
unsigned char packet[17 + (sizeof(FwdNotReq) - 1)];
293
unsigned char *shost;
294
uint32_t sender_channel;
295
uint32_t initial_window_size;
296
uint32_t packet_size;
301
LIBSSH2_CHANNEL *channel;
302
} packet_queue_listener_state_t;
304
#define X11FwdUnAvil "X11 Forward Unavailable"
306
typedef struct packet_x11_open_state_t
308
libssh2_nonblocking_states state;
309
unsigned char packet[17 + (sizeof(X11FwdUnAvil) - 1)];
310
unsigned char *shost;
311
uint32_t sender_channel;
312
uint32_t initial_window_size;
313
uint32_t packet_size;
316
LIBSSH2_CHANNEL *channel;
317
} packet_x11_open_state_t;
319
struct _LIBSSH2_PACKET
321
struct list_node node; /* linked list header */
323
/* the raw unencrypted payload */
327
/* Where to start reading data from,
328
* used for channel data that's been partially consumed */
332
typedef struct _libssh2_channel_data
337
/* Limits and restrictions */
338
uint32_t window_size_initial, window_size, packet_size;
340
/* Set to 1 when CHANNEL_CLOSE / CHANNEL_EOF sent/received */
341
char close, eof, extended_data_ignore_mode;
342
} libssh2_channel_data;
344
struct _LIBSSH2_CHANNEL
346
struct list_node node;
348
unsigned char *channel_type;
349
unsigned channel_type_len;
351
/* channel's program exit status */
354
/* channel's program exit signal (without the SIG prefix) */
357
libssh2_channel_data local, remote;
358
/* Amount of bytes to be refunded to receive window (but not yet sent) */
359
uint32_t adjust_queue;
361
LIBSSH2_SESSION *session;
364
LIBSSH2_CHANNEL_CLOSE_FUNC((*close_cb));
366
/* State variables used in libssh2_channel_setenv_ex() */
367
libssh2_nonblocking_states setenv_state;
368
unsigned char *setenv_packet;
369
size_t setenv_packet_len;
370
unsigned char setenv_local_channel[4];
371
packet_requirev_state_t setenv_packet_requirev_state;
373
/* State variables used in libssh2_channel_request_pty_ex()
374
libssh2_channel_request_pty_size_ex() */
375
libssh2_nonblocking_states reqPTY_state;
376
unsigned char reqPTY_packet[41 + 256];
377
size_t reqPTY_packet_len;
378
unsigned char reqPTY_local_channel[4];
379
packet_requirev_state_t reqPTY_packet_requirev_state;
381
/* State variables used in libssh2_channel_x11_req_ex() */
382
libssh2_nonblocking_states reqX11_state;
383
unsigned char *reqX11_packet;
384
size_t reqX11_packet_len;
385
unsigned char reqX11_local_channel[4];
386
packet_requirev_state_t reqX11_packet_requirev_state;
388
/* State variables used in libssh2_channel_process_startup() */
389
libssh2_nonblocking_states process_state;
390
unsigned char *process_packet;
391
size_t process_packet_len;
392
unsigned char process_local_channel[4];
393
packet_requirev_state_t process_packet_requirev_state;
395
/* State variables used in libssh2_channel_flush_ex() */
396
libssh2_nonblocking_states flush_state;
397
size_t flush_refund_bytes;
398
size_t flush_flush_bytes;
400
/* State variables used in libssh2_channel_receive_window_adjust() */
401
libssh2_nonblocking_states adjust_state;
402
unsigned char adjust_adjust[9]; /* packet_type(1) + channel(4) + adjustment(4) */
404
/* State variables used in libssh2_channel_read_ex() */
405
libssh2_nonblocking_states read_state;
407
uint32_t read_local_id;
409
/* State variables used in libssh2_channel_write_ex() */
410
libssh2_nonblocking_states write_state;
411
unsigned char write_packet[13];
412
size_t write_packet_len;
413
size_t write_bufwrite;
415
/* State variables used in libssh2_channel_close() */
416
libssh2_nonblocking_states close_state;
417
unsigned char close_packet[5];
419
/* State variables used in libssh2_channel_wait_closedeof() */
420
libssh2_nonblocking_states wait_eof_state;
422
/* State variables used in libssh2_channel_wait_closed() */
423
libssh2_nonblocking_states wait_closed_state;
425
/* State variables used in libssh2_channel_free() */
426
libssh2_nonblocking_states free_state;
428
/* State variables used in libssh2_channel_handle_extended_data2() */
429
libssh2_nonblocking_states extData2_state;
433
struct _LIBSSH2_LISTENER
435
struct list_node node; /* linked list header */
437
LIBSSH2_SESSION *session;
442
/* a list of CHANNELs for this listener */
443
struct list_head queue;
448
/* State variables used in libssh2_channel_forward_cancel() */
449
libssh2_nonblocking_states chanFwdCncl_state;
450
unsigned char *chanFwdCncl_data;
451
size_t chanFwdCncl_data_len;
454
typedef struct _libssh2_endpoint_data
456
unsigned char *banner;
458
unsigned char *kexinit;
461
const LIBSSH2_CRYPT_METHOD *crypt;
462
void *crypt_abstract;
464
const struct _LIBSSH2_MAC_METHOD *mac;
468
const LIBSSH2_COMP_METHOD *comp;
471
/* Method Preferences -- NULL yields "load order" */
476
} libssh2_endpoint_data;
478
#define PACKETBUFSIZE (1024*16)
480
struct transportpacket
482
/* ------------- for incoming data --------------- */
483
unsigned char buf[PACKETBUFSIZE];
484
unsigned char init[5]; /* first 5 bytes of the incoming data stream,
486
size_t writeidx; /* at what array index we do the next write into
488
size_t readidx; /* at what array index we do the next read from
490
uint32_t packet_length; /* the most recent packet_length as read from the
492
uint8_t padding_length; /* the most recent padding_length as read from the
494
size_t data_num; /* How much of the total package that has been read
496
size_t total_num; /* How much a total package is supposed to be, in
497
number of bytes. A full package is
498
packet_length + padding_length + 4 +
500
unsigned char *payload; /* this is a pointer to a LIBSSH2_ALLOC()
501
area to which we write decrypted data */
502
unsigned char *wptr; /* write pointer into the payload to where we
503
are currently writing decrypted data */
505
/* ------------- for outgoing data --------------- */
506
unsigned char outbuf[MAX_SSH_PACKET_LEN]; /* area for the outgoing data */
508
int ototal_num; /* size of outbuf in number of bytes */
509
const unsigned char *odata; /* original pointer to the data */
510
size_t olen; /* original size of the data we stored in
512
size_t osent; /* number of bytes already sent */
515
struct _LIBSSH2_PUBLICKEY
517
LIBSSH2_CHANNEL *channel;
520
/* State variables used in libssh2_publickey_packet_receive() */
521
libssh2_nonblocking_states receive_state;
522
unsigned char *receive_packet;
523
size_t receive_packet_len;
525
/* State variables used in libssh2_publickey_add_ex() */
526
libssh2_nonblocking_states add_state;
527
unsigned char *add_packet;
528
unsigned char *add_s;
530
/* State variables used in libssh2_publickey_remove_ex() */
531
libssh2_nonblocking_states remove_state;
532
unsigned char *remove_packet;
533
unsigned char *remove_s;
535
/* State variables used in libssh2_publickey_list_fetch() */
536
libssh2_nonblocking_states listFetch_state;
537
unsigned char *listFetch_s;
538
unsigned char listFetch_buffer[12];
539
unsigned char *listFetch_data;
540
size_t listFetch_data_len;
543
#define LIBSSH2_SCP_RESPONSE_BUFLEN 256
546
int sigpipe; /* LIBSSH2_FLAG_SIGPIPE */
547
int compress; /* LIBSSH2_FLAG_COMPRESS */
550
struct _LIBSSH2_SESSION
552
/* Memory management callbacks */
554
LIBSSH2_ALLOC_FUNC((*alloc));
555
LIBSSH2_REALLOC_FUNC((*realloc));
556
LIBSSH2_FREE_FUNC((*free));
558
/* Other callbacks */
559
LIBSSH2_IGNORE_FUNC((*ssh_msg_ignore));
560
LIBSSH2_DEBUG_FUNC((*ssh_msg_debug));
561
LIBSSH2_DISCONNECT_FUNC((*ssh_msg_disconnect));
562
LIBSSH2_MACERROR_FUNC((*macerror));
563
LIBSSH2_X11_OPEN_FUNC((*x11));
564
LIBSSH2_SEND_FUNC((*send));
565
LIBSSH2_RECV_FUNC((*recv));
567
/* Method preferences -- NULL yields "load order" */
576
/* Agreed Key Exchange Method */
577
const LIBSSH2_KEX_METHOD *kex;
578
int burn_optimistic_kexinit:1;
580
unsigned char *session_id;
581
uint32_t session_id_len;
583
/* this is set to TRUE if a blocking API behavior is requested */
586
/* Timeout used when blocking API behavior is active */
589
/* Server's public key */
590
const LIBSSH2_HOSTKEY_METHOD *hostkey;
591
void *server_hostkey_abstract;
593
/* Either set with libssh2_session_hostkey() (for server mode)
594
* Or read from server in (eg) KEXDH_INIT (for client mode)
596
unsigned char *server_hostkey;
597
uint32_t server_hostkey_len;
599
unsigned char server_hostkey_md5[MD5_DIGEST_LENGTH];
600
int server_hostkey_md5_valid;
601
#endif /* ! LIBSSH2_MD5 */
602
unsigned char server_hostkey_sha1[SHA_DIGEST_LENGTH];
604
/* (remote as source of data -- packet_read ) */
605
libssh2_endpoint_data remote;
607
/* (local as source of data -- packet_write ) */
608
libssh2_endpoint_data local;
610
/* Inbound Data linked list -- Sometimes the packet that comes in isn't the
611
packet we're ready for */
612
struct list_head packets;
614
/* Active connection channels */
615
struct list_head channels;
617
uint32_t next_channel;
619
struct list_head listeners; /* list of LIBSSH2_LISTENER structs */
621
/* Actual I/O socket */
622
libssh2_socket_t socket_fd;
624
int socket_block_directions;
625
int socket_prev_blockstate; /* stores the state of the socket blockiness
626
when libssh2_session_startup() is called */
632
/* struct members for packet-level reading */
633
struct transportpacket packet;
635
int showmask; /* what debug/trace messages to display */
636
libssh2_trace_handler_func tracehandler; /* callback to display trace messages */
637
void* tracehandler_context; /* context for the trace handler */
640
/* State variables used in libssh2_banner_send() */
641
libssh2_nonblocking_states banner_TxRx_state;
642
char banner_TxRx_banner[256];
643
ssize_t banner_TxRx_total_send;
645
/* State variables used in libssh2_kexinit() */
646
libssh2_nonblocking_states kexinit_state;
647
unsigned char *kexinit_data;
648
size_t kexinit_data_len;
650
/* State variables used in libssh2_session_startup() */
651
libssh2_nonblocking_states startup_state;
652
unsigned char *startup_data;
653
size_t startup_data_len;
654
unsigned char startup_service[sizeof("ssh-userauth") + 5 - 1];
655
size_t startup_service_length;
656
packet_require_state_t startup_req_state;
657
key_exchange_state_t startup_key_state;
659
/* State variables used in libssh2_session_free() */
660
libssh2_nonblocking_states free_state;
662
/* State variables used in libssh2_session_disconnect_ex() */
663
libssh2_nonblocking_states disconnect_state;
664
unsigned char disconnect_data[256 + 13];
665
size_t disconnect_data_len;
667
/* State variables used in libssh2_packet_read() */
668
libssh2_nonblocking_states readPack_state;
669
int readPack_encrypted;
671
/* State variables used in libssh2_userauth_list() */
672
libssh2_nonblocking_states userauth_list_state;
673
unsigned char *userauth_list_data;
674
size_t userauth_list_data_len;
675
packet_requirev_state_t userauth_list_packet_requirev_state;
677
/* State variables used in libssh2_userauth_password_ex() */
678
libssh2_nonblocking_states userauth_pswd_state;
679
unsigned char *userauth_pswd_data;
680
unsigned char userauth_pswd_data0;
681
size_t userauth_pswd_data_len;
682
char *userauth_pswd_newpw;
683
int userauth_pswd_newpw_len;
684
packet_requirev_state_t userauth_pswd_packet_requirev_state;
686
/* State variables used in libssh2_userauth_hostbased_fromfile_ex() */
687
libssh2_nonblocking_states userauth_host_state;
688
unsigned char *userauth_host_data;
689
size_t userauth_host_data_len;
690
unsigned char *userauth_host_packet;
691
size_t userauth_host_packet_len;
692
unsigned char *userauth_host_method;
693
size_t userauth_host_method_len;
694
unsigned char *userauth_host_s;
695
packet_requirev_state_t userauth_host_packet_requirev_state;
697
/* State variables used in libssh2_userauth_publickey_fromfile_ex() */
698
libssh2_nonblocking_states userauth_pblc_state;
699
unsigned char *userauth_pblc_data;
700
size_t userauth_pblc_data_len;
701
unsigned char *userauth_pblc_packet;
702
size_t userauth_pblc_packet_len;
703
unsigned char *userauth_pblc_method;
704
size_t userauth_pblc_method_len;
705
unsigned char *userauth_pblc_s;
706
unsigned char *userauth_pblc_b;
707
packet_requirev_state_t userauth_pblc_packet_requirev_state;
709
/* State variables used in libssh2_userauth_keyboard_interactive_ex() */
710
libssh2_nonblocking_states userauth_kybd_state;
711
unsigned char *userauth_kybd_data;
712
size_t userauth_kybd_data_len;
713
unsigned char *userauth_kybd_packet;
714
size_t userauth_kybd_packet_len;
715
unsigned int userauth_kybd_auth_name_len;
716
char *userauth_kybd_auth_name;
717
unsigned userauth_kybd_auth_instruction_len;
718
char *userauth_kybd_auth_instruction;
719
unsigned int userauth_kybd_num_prompts;
720
int userauth_kybd_auth_failure;
721
LIBSSH2_USERAUTH_KBDINT_PROMPT *userauth_kybd_prompts;
722
LIBSSH2_USERAUTH_KBDINT_RESPONSE *userauth_kybd_responses;
723
packet_requirev_state_t userauth_kybd_packet_requirev_state;
725
/* State variables used in libssh2_channel_open_ex() */
726
libssh2_nonblocking_states open_state;
727
packet_requirev_state_t open_packet_requirev_state;
728
LIBSSH2_CHANNEL *open_channel;
729
unsigned char *open_packet;
730
size_t open_packet_len;
731
unsigned char *open_data;
732
size_t open_data_len;
733
uint32_t open_local_channel;
735
/* State variables used in libssh2_channel_direct_tcpip_ex() */
736
libssh2_nonblocking_states direct_state;
737
unsigned char *direct_message;
738
size_t direct_host_len;
739
size_t direct_shost_len;
740
size_t direct_message_len;
742
/* State variables used in libssh2_channel_forward_listen_ex() */
743
libssh2_nonblocking_states fwdLstn_state;
744
unsigned char *fwdLstn_packet;
745
uint32_t fwdLstn_host_len;
746
uint32_t fwdLstn_packet_len;
747
packet_requirev_state_t fwdLstn_packet_requirev_state;
749
/* State variables used in libssh2_publickey_init() */
750
libssh2_nonblocking_states pkeyInit_state;
751
LIBSSH2_PUBLICKEY *pkeyInit_pkey;
752
LIBSSH2_CHANNEL *pkeyInit_channel;
753
unsigned char *pkeyInit_data;
754
size_t pkeyInit_data_len;
755
/* 19 = packet_len(4) + version_len(4) + "version"(7) + version_num(4) */
756
unsigned char pkeyInit_buffer[19];
757
size_t pkeyInit_buffer_sent; /* how much of buffer that has been sent */
759
/* State variables used in libssh2_packet_add() */
760
libssh2_nonblocking_states packAdd_state;
761
LIBSSH2_CHANNEL *packAdd_channelp; /* keeper of the channel during EAGAIN
763
packet_queue_listener_state_t packAdd_Qlstn_state;
764
packet_x11_open_state_t packAdd_x11open_state;
766
/* State variables used in fullpacket() */
767
libssh2_nonblocking_states fullpacket_state;
768
int fullpacket_macstate;
769
size_t fullpacket_payload_len;
770
int fullpacket_packet_type;
772
/* State variables used in libssh2_sftp_init() */
773
libssh2_nonblocking_states sftpInit_state;
774
LIBSSH2_SFTP *sftpInit_sftp;
775
LIBSSH2_CHANNEL *sftpInit_channel;
776
unsigned char sftpInit_buffer[9]; /* sftp_header(5){excludes request_id}
778
int sftpInit_sent; /* number of bytes from the buffer that have been
781
/* State variables used in libssh2_scp_recv() */
782
libssh2_nonblocking_states scpRecv_state;
783
unsigned char *scpRecv_command;
784
size_t scpRecv_command_len;
785
unsigned char scpRecv_response[LIBSSH2_SCP_RESPONSE_BUFLEN];
786
size_t scpRecv_response_len;
788
#if defined(HAVE_LONGLONG) && defined(HAVE_STRTOLL)
789
/* we have the type and we can parse such numbers */
790
long long scpRecv_size;
791
#define scpsize_strtol strtoll
794
#define scpsize_strtol strtol
798
LIBSSH2_CHANNEL *scpRecv_channel;
800
/* State variables used in libssh2_scp_send_ex() */
801
libssh2_nonblocking_states scpSend_state;
802
unsigned char *scpSend_command;
803
size_t scpSend_command_len;
804
unsigned char scpSend_response[LIBSSH2_SCP_RESPONSE_BUFLEN];
805
size_t scpSend_response_len;
806
LIBSSH2_CHANNEL *scpSend_channel;
808
/* Keepalive variables used by keepalive.c. */
809
int keepalive_interval;
810
int keepalive_want_reply;
811
time_t keepalive_last_sent;
814
/* session.state bits */
815
#define LIBSSH2_STATE_EXCHANGING_KEYS 0x00000001
816
#define LIBSSH2_STATE_NEWKEYS 0x00000002
817
#define LIBSSH2_STATE_AUTHENTICATED 0x00000004
818
#define LIBSSH2_STATE_KEX_ACTIVE 0x00000008
820
/* session.flag helpers */
822
#define LIBSSH2_SOCKET_SEND_FLAGS(session) \
823
(((session)->flag.sigpipe) ? 0 : MSG_NOSIGNAL)
824
#define LIBSSH2_SOCKET_RECV_FLAGS(session) \
825
(((session)->flag.sigpipe) ? 0 : MSG_NOSIGNAL)
827
/* If MSG_NOSIGNAL isn't defined we're SOL on blocking SIGPIPE */
828
#define LIBSSH2_SOCKET_SEND_FLAGS(session) 0
829
#define LIBSSH2_SOCKET_RECV_FLAGS(session) 0
834
/* libssh2 extensible ssh api, ultimately I'd like to allow loading additional
835
methods via .so/.dll */
837
struct _LIBSSH2_KEX_METHOD
841
/* Key exchange, populates session->* and returns 0 on success, non-0 on error */
842
int (*exchange_keys) (LIBSSH2_SESSION * session,
843
key_exchange_state_low_t * key_state);
848
struct _LIBSSH2_HOSTKEY_METHOD
851
unsigned long hash_len;
853
int (*init) (LIBSSH2_SESSION * session, const unsigned char *hostkey_data,
854
size_t hostkey_data_len, void **abstract);
855
int (*initPEM) (LIBSSH2_SESSION * session, const char *privkeyfile,
856
unsigned const char *passphrase, void **abstract);
857
int (*sig_verify) (LIBSSH2_SESSION * session, const unsigned char *sig,
858
size_t sig_len, const unsigned char *m,
859
size_t m_len, void **abstract);
860
int (*signv) (LIBSSH2_SESSION * session, unsigned char **signature,
861
size_t *signature_len, int veccount,
862
const struct iovec datavec[], void **abstract);
863
int (*encrypt) (LIBSSH2_SESSION * session, unsigned char **dst,
864
size_t *dst_len, const unsigned char *src,
865
size_t src_len, void **abstract);
866
int (*dtor) (LIBSSH2_SESSION * session, void **abstract);
869
struct _LIBSSH2_CRYPT_METHOD
875
/* iv and key sizes (-1 for variable length) */
881
int (*init) (LIBSSH2_SESSION * session,
882
const LIBSSH2_CRYPT_METHOD * method, unsigned char *iv,
883
int *free_iv, unsigned char *secret, int *free_secret,
884
int encrypt, void **abstract);
885
int (*crypt) (LIBSSH2_SESSION * session, unsigned char *block,
886
size_t blocksize, void **abstract);
887
int (*dtor) (LIBSSH2_SESSION * session, void **abstract);
889
_libssh2_cipher_type(algo);
892
struct _LIBSSH2_COMP_METHOD
895
int compress; /* 1 if it does compress, 0 if it doesn't */
896
int use_in_auth; /* 1 if compression should be used in userauth */
897
int (*init) (LIBSSH2_SESSION *session, int compress, void **abstract);
898
int (*comp) (LIBSSH2_SESSION *session,
901
const unsigned char *src,
904
int (*decomp) (LIBSSH2_SESSION *session,
905
unsigned char **dest,
907
size_t payload_limit,
908
const unsigned char *src,
911
int (*dtor) (LIBSSH2_SESSION * session, int compress, void **abstract);
915
void _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format,
918
#if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || defined(__GNUC__)
919
/* C99 supported and also by older GCC */
920
#define _libssh2_debug(x,y,z,...) do {} while (0)
922
/* no gcc and not C99, do static and hopefully inline */
924
_libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
930
#define LIBSSH2_SOCKET_UNKNOWN 1
931
#define LIBSSH2_SOCKET_CONNECTED 0
932
#define LIBSSH2_SOCKET_DISCONNECTED -1
934
/* Initial packet state, prior to MAC check */
935
#define LIBSSH2_MAC_UNCONFIRMED 1
936
/* When MAC type is "none" (proto initiation phase) all packets are deemed "confirmed" */
937
#define LIBSSH2_MAC_CONFIRMED 0
938
/* Something very bad is going on */
939
#define LIBSSH2_MAC_INVALID -1
941
/* SSH Packet Types -- Defined by internet draft */
942
/* Transport Layer */
943
#define SSH_MSG_DISCONNECT 1
944
#define SSH_MSG_IGNORE 2
945
#define SSH_MSG_UNIMPLEMENTED 3
946
#define SSH_MSG_DEBUG 4
947
#define SSH_MSG_SERVICE_REQUEST 5
948
#define SSH_MSG_SERVICE_ACCEPT 6
950
#define SSH_MSG_KEXINIT 20
951
#define SSH_MSG_NEWKEYS 21
953
/* diffie-hellman-group1-sha1 */
954
#define SSH_MSG_KEXDH_INIT 30
955
#define SSH_MSG_KEXDH_REPLY 31
957
/* diffie-hellman-group-exchange-sha1 */
958
#define SSH_MSG_KEX_DH_GEX_REQUEST_OLD 30
959
#define SSH_MSG_KEX_DH_GEX_REQUEST 34
960
#define SSH_MSG_KEX_DH_GEX_GROUP 31
961
#define SSH_MSG_KEX_DH_GEX_INIT 32
962
#define SSH_MSG_KEX_DH_GEX_REPLY 33
964
/* User Authentication */
965
#define SSH_MSG_USERAUTH_REQUEST 50
966
#define SSH_MSG_USERAUTH_FAILURE 51
967
#define SSH_MSG_USERAUTH_SUCCESS 52
968
#define SSH_MSG_USERAUTH_BANNER 53
970
/* "public key" method */
971
#define SSH_MSG_USERAUTH_PK_OK 60
972
/* "password" method */
973
#define SSH_MSG_USERAUTH_PASSWD_CHANGEREQ 60
974
/* "keyboard-interactive" method */
975
#define SSH_MSG_USERAUTH_INFO_REQUEST 60
976
#define SSH_MSG_USERAUTH_INFO_RESPONSE 61
979
#define SSH_MSG_GLOBAL_REQUEST 80
980
#define SSH_MSG_REQUEST_SUCCESS 81
981
#define SSH_MSG_REQUEST_FAILURE 82
983
#define SSH_MSG_CHANNEL_OPEN 90
984
#define SSH_MSG_CHANNEL_OPEN_CONFIRMATION 91
985
#define SSH_MSG_CHANNEL_OPEN_FAILURE 92
986
#define SSH_MSG_CHANNEL_WINDOW_ADJUST 93
987
#define SSH_MSG_CHANNEL_DATA 94
988
#define SSH_MSG_CHANNEL_EXTENDED_DATA 95
989
#define SSH_MSG_CHANNEL_EOF 96
990
#define SSH_MSG_CHANNEL_CLOSE 97
991
#define SSH_MSG_CHANNEL_REQUEST 98
992
#define SSH_MSG_CHANNEL_SUCCESS 99
993
#define SSH_MSG_CHANNEL_FAILURE 100
995
/* Error codes returned in SSH_MSG_CHANNEL_OPEN_FAILURE message
997
#define SSH_OPEN_ADMINISTRATIVELY_PROHIBITED 1
998
#define SSH_OPEN_CONNECT_FAILED 2
999
#define SSH_OPEN_UNKNOWN_CHANNELTYPE 3
1000
#define SSH_OPEN_RESOURCE_SHORTAGE 4
1002
ssize_t _libssh2_recv(libssh2_socket_t socket, void *buffer,
1003
size_t length, int flags, void **abstract);
1004
ssize_t _libssh2_send(libssh2_socket_t socket, const void *buffer,
1005
size_t length, int flags, void **abstract);
1007
#define LIBSSH2_READ_TIMEOUT 60 /* generic timeout in seconds used when
1008
waiting for more data to arrive */
1011
int _libssh2_kex_exchange(LIBSSH2_SESSION * session, int reexchange,
1012
key_exchange_state_t * state);
1014
/* Let crypt.c/hostkey.c expose their method structs */
1015
const LIBSSH2_CRYPT_METHOD **libssh2_crypt_methods(void);
1016
const LIBSSH2_HOSTKEY_METHOD **libssh2_hostkey_methods(void);
1019
int _libssh2_pem_parse(LIBSSH2_SESSION * session,
1020
const char *headerbegin,
1021
const char *headerend,
1022
FILE * fp, unsigned char **data, unsigned int *datalen);
1023
int _libssh2_pem_decode_sequence(unsigned char **data, unsigned int *datalen);
1024
int _libssh2_pem_decode_integer(unsigned char **data, unsigned int *datalen,
1025
unsigned char **i, unsigned int *ilen);
1028
void _libssh2_init_if_needed (void);
1031
#define ARRAY_SIZE(a) (sizeof ((a)) / sizeof ((a)[0]))
1033
/* define to output the libssh2_int64_t type in a *printf() */
1034
#if defined( __BORLANDC__ ) || defined( _MSC_VER ) || defined( __MINGW32__ )
1035
#define LIBSSH2_INT64_T_FORMAT "I64d"
1037
#define LIBSSH2_INT64_T_FORMAT "lld"
1040
#endif /* LIBSSH2_H */