1 /* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org>
4 * Redistribution and use in source and binary forms,
5 * with or without modification, are permitted provided
6 * that the following conditions are met:
8 * Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the
10 * following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * Neither the name of the copyright holder nor the names
18 * of any other contributors may be used to endorse or
19 * promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38 #ifndef LIBSSH2_PRIV_H
39 #define LIBSSH2_PRIV_H 1
41 #define LIBSSH2_LIBRARY
42 #include "libssh2_config.h"
44 /* The following CPP block should really only be in session.c and
45 packet.c. However, AIX have #define's for 'events' and 'revents'
46 and we are using those names in libssh2.h, so we need to include
47 the AIX headers first, to make sure all code is compiled with
48 consistent names of these fields. While arguable the best would to
49 change libssh2.h to use other names, that would break backwards
50 compatibility. For more information, see:
51 http://www.mail-archive.com/libssh2-devel%40lists.sourceforge.net/msg00003.html
52 http://www.mail-archive.com/libssh2-devel%40lists.sourceforge.net/msg00224.html
57 # include <sys/poll.h>
60 # ifdef HAVE_SYS_SELECT_H
61 # include <sys/select.h>
63 # include <sys/time.h>
64 # include <sys/types.h>
70 #include "libssh2_publickey.h"
71 #include "libssh2_sftp.h"
73 /* Needed for struct iovec on some platforms */
78 #ifdef HAVE_SYS_SOCKET_H
79 # include <sys/socket.h>
81 #ifdef HAVE_SYS_IOCTL_H
82 # include <sys/ioctl.h>
84 #ifdef HAVE_INTTYPES_H
88 #ifdef LIBSSH2_LIBGCRYPT
89 #include "libgcrypt.h"
94 /* RFC4253 section 6.1 Maximum Packet Length says:
96 * "All implementations MUST be able to process packets with
97 * uncompressed payload length of 32768 bytes or less and
98 * total packet size of 35000 bytes or less (including length,
99 * padding length, payload, padding, and MAC.)."
101 #define MAX_SSH_PACKET_LEN 35000
103 #define LIBSSH2_ALLOC(session, count) session->alloc((count), &(session)->abstract)
104 #define LIBSSH2_REALLOC(session, ptr, count) ((ptr) ? session->realloc((ptr), (count), &(session)->abstract) : session->alloc((count), &(session)->abstract))
105 #define LIBSSH2_FREE(session, ptr) session->free((ptr), &(session)->abstract)
107 #define LIBSSH2_IGNORE(session, data, datalen) session->ssh_msg_ignore((session), (data), (datalen), &(session)->abstract)
108 #define LIBSSH2_DEBUG(session, always_display, message, message_len, language, language_len) \
109 session->ssh_msg_disconnect((session), (always_display), (message), (message_len), (language), (language_len), &(session)->abstract)
110 #define LIBSSH2_DISCONNECT(session, reason, message, message_len, language, language_len) \
111 session->ssh_msg_disconnect((session), (reason), (message), (message_len), (language), (language_len), &(session)->abstract)
113 #define LIBSSH2_MACERROR(session, data, datalen) session->macerror((session), (data), (datalen), &(session)->abstract)
114 #define LIBSSH2_X11_OPEN(channel, shost, sport) channel->session->x11(((channel)->session), (channel), (shost), (sport), (&(channel)->session->abstract))
116 #define LIBSSH2_CHANNEL_CLOSE(session, channel) channel->close_cb((session), &(session)->abstract, (channel), &(channel)->abstract)
118 typedef struct _LIBSSH2_KEX_METHOD LIBSSH2_KEX_METHOD
;
119 typedef struct _LIBSSH2_HOSTKEY_METHOD LIBSSH2_HOSTKEY_METHOD
;
120 typedef struct _LIBSSH2_MAC_METHOD LIBSSH2_MAC_METHOD
;
121 typedef struct _LIBSSH2_CRYPT_METHOD LIBSSH2_CRYPT_METHOD
;
122 typedef struct _LIBSSH2_COMP_METHOD LIBSSH2_COMP_METHOD
;
124 typedef struct _LIBSSH2_PACKET LIBSSH2_PACKET
;
125 typedef struct _LIBSSH2_PACKET_BRIGADE LIBSSH2_PACKET_BRIGADE
;
126 typedef struct _LIBSSH2_CHANNEL_BRIGADE LIBSSH2_CHANNEL_BRIGADE
;
128 typedef int libssh2pack_t
;
132 libssh2_NB_state_idle
= 0,
133 libssh2_NB_state_allocated
,
134 libssh2_NB_state_created
,
135 libssh2_NB_state_sent
,
136 libssh2_NB_state_sent1
,
137 libssh2_NB_state_sent2
,
138 libssh2_NB_state_sent3
,
139 libssh2_NB_state_sent4
,
140 libssh2_NB_state_sent5
,
141 libssh2_NB_state_sent6
,
142 libssh2_NB_state_sent7
,
143 libssh2_NB_state_jump1
,
144 libssh2_NB_state_jump2
,
145 libssh2_NB_state_jump3
146 } libssh2_nonblocking_states
;
148 typedef struct packet_require_state_t
150 libssh2_nonblocking_states state
;
152 } packet_require_state_t
;
154 typedef struct packet_requirev_state_t
157 } packet_requirev_state_t
;
159 typedef struct kmdhgGPsha1kex_state_t
161 libssh2_nonblocking_states state
;
162 unsigned char *e_packet
;
163 unsigned char *s_packet
;
165 unsigned char h_sig_comp
[SHA_DIGEST_LENGTH
];
167 unsigned long e_packet_len
;
168 unsigned long s_packet_len
;
169 unsigned long tmp_len
;
170 _libssh2_bn_ctx
*ctx
;
176 unsigned char *f_value
;
177 unsigned char *k_value
;
178 unsigned char *h_sig
;
179 unsigned long f_value_len
;
180 unsigned long k_value_len
;
181 unsigned long h_sig_len
;
182 libssh2_sha1_ctx exchange_hash
;
183 packet_require_state_t req_state
;
184 libssh2_nonblocking_states burn_state
;
185 } kmdhgGPsha1kex_state_t
;
187 typedef struct key_exchange_state_low_t
189 libssh2_nonblocking_states state
;
190 packet_require_state_t req_state
;
191 kmdhgGPsha1kex_state_t exchange_state
;
192 _libssh2_bn
*p
; /* SSH2 defined value (p_value) */
193 _libssh2_bn
*g
; /* SSH2 defined value (2) */
194 unsigned char request
[13];
196 unsigned long request_len
;
197 unsigned long data_len
;
198 } key_exchange_state_low_t
;
200 typedef struct key_exchange_state_t
202 libssh2_nonblocking_states state
;
203 packet_require_state_t req_state
;
204 key_exchange_state_low_t key_state_low
;
206 unsigned long data_len
;
207 unsigned char *oldlocal
;
208 unsigned long oldlocal_len
;
209 } key_exchange_state_t
;
211 #define FwdNotReq "Forward not requested"
213 typedef struct packet_queue_listener_state_t
215 libssh2_nonblocking_states state
;
216 unsigned char packet
[17 + (sizeof(FwdNotReq
) - 1)];
218 unsigned char *shost
;
219 uint32_t sender_channel
;
220 uint32_t initial_window_size
;
221 uint32_t packet_size
;
226 } packet_queue_listener_state_t
;
228 #define X11FwdUnAvil "X11 Forward Unavailable"
230 typedef struct packet_x11_open_state_t
232 libssh2_nonblocking_states state
;
233 unsigned char packet
[17 + (sizeof(X11FwdUnAvil
) - 1)];
234 unsigned char *shost
;
235 uint32_t sender_channel
;
236 uint32_t initial_window_size
;
237 uint32_t packet_size
;
240 } packet_x11_open_state_t
;
242 struct _LIBSSH2_PACKET
246 /* Unencrypted Payload (no type byte, no padding, just the facts ma'am) */
248 unsigned long data_len
;
250 /* Where to start reading data from,
251 * used for channel data that's been partially consumed */
252 unsigned long data_head
;
254 /* Can the message be confirmed? */
257 LIBSSH2_PACKET_BRIGADE
*brigade
;
259 LIBSSH2_PACKET
*next
, *prev
;
262 struct _LIBSSH2_PACKET_BRIGADE
264 LIBSSH2_PACKET
*head
, *tail
;
267 typedef struct _libssh2_channel_data
272 /* Limits and restrictions */
273 unsigned long window_size_initial
, window_size
, packet_size
;
275 /* Set to 1 when CHANNEL_CLOSE / CHANNEL_EOF sent/received */
276 char close
, eof
, extended_data_ignore_mode
;
277 } libssh2_channel_data
;
279 struct _LIBSSH2_CHANNEL
281 unsigned char *channel_type
;
282 unsigned channel_type_len
;
284 /* channel's program exit status */
287 libssh2_channel_data local
, remote
;
288 /* Amount of bytes to be refunded to receive window (but not yet sent) */
289 unsigned long adjust_queue
;
291 LIBSSH2_SESSION
*session
;
293 LIBSSH2_CHANNEL
*next
, *prev
;
296 LIBSSH2_CHANNEL_CLOSE_FUNC((*close_cb
));
298 /* State variables used in libssh2_channel_setenv_ex() */
299 libssh2_nonblocking_states setenv_state
;
300 unsigned char *setenv_packet
;
301 unsigned long setenv_packet_len
;
302 unsigned char setenv_local_channel
[4];
303 packet_requirev_state_t setenv_packet_requirev_state
;
305 /* State variables used in libssh2_channel_request_pty_ex() */
306 libssh2_nonblocking_states reqPTY_state
;
307 unsigned char *reqPTY_packet
;
308 unsigned long reqPTY_packet_len
;
309 unsigned char reqPTY_local_channel
[4];
310 packet_requirev_state_t reqPTY_packet_requirev_state
;
312 /* State variables used in libssh2_channel_x11_req_ex() */
313 libssh2_nonblocking_states reqX11_state
;
314 unsigned char *reqX11_packet
;
315 unsigned long reqX11_packet_len
;
316 unsigned char reqX11_local_channel
[4];
317 packet_requirev_state_t reqX11_packet_requirev_state
;
319 /* State variables used in libssh2_channel_process_startup() */
320 libssh2_nonblocking_states process_state
;
321 unsigned char *process_packet
;
322 unsigned long process_packet_len
;
323 unsigned char process_local_channel
[4];
324 packet_requirev_state_t process_packet_requirev_state
;
326 /* State variables used in libssh2_channel_flush_ex() */
327 libssh2_nonblocking_states flush_state
;
328 unsigned long flush_refund_bytes
;
329 unsigned long flush_flush_bytes
;
331 /* State variables used in libssh2_channel_receive_window_adjust() */
332 libssh2_nonblocking_states adjust_state
;
333 unsigned char adjust_adjust
[9]; /* packet_type(1) + channel(4) + adjustment(4) */
335 /* State variables used in libssh2_channel_read_ex() */
336 libssh2_nonblocking_states read_state
;
337 LIBSSH2_PACKET
*read_packet
;
338 LIBSSH2_PACKET
*read_next
;
341 uint32_t read_local_id
;
343 int read_unlink_packet
;
345 /* State variables used in libssh2_channel_write_ex() */
346 libssh2_nonblocking_states write_state
;
347 unsigned char *write_packet
;
348 unsigned char *write_s
;
349 unsigned long write_packet_len
;
350 unsigned long write_bufwrote
;
351 size_t write_bufwrite
;
353 /* State variables used in libssh2_channel_close() */
354 libssh2_nonblocking_states close_state
;
355 unsigned char close_packet
[5];
357 /* State variables used in libssh2_channel_wait_closedeof() */
358 libssh2_nonblocking_states wait_eof_state
;
360 /* State variables used in libssh2_channel_wait_closed() */
361 libssh2_nonblocking_states wait_closed_state
;
363 /* State variables used in libssh2_channel_free() */
364 libssh2_nonblocking_states free_state
;
366 /* State variables used in libssh2_channel_handle_extended_data2() */
367 libssh2_nonblocking_states extData2_state
;
370 struct _LIBSSH2_CHANNEL_BRIGADE
372 LIBSSH2_CHANNEL
*head
, *tail
;
375 struct _LIBSSH2_LISTENER
377 LIBSSH2_SESSION
*session
;
382 LIBSSH2_CHANNEL
*queue
;
386 LIBSSH2_LISTENER
*prev
, *next
;
388 /* State variables used in libssh2_channel_forward_cancel() */
389 libssh2_nonblocking_states chanFwdCncl_state
;
390 unsigned char *chanFwdCncl_data
;
391 size_t chanFwdCncl_data_len
;
394 typedef struct _libssh2_endpoint_data
396 unsigned char *banner
;
398 unsigned char *kexinit
;
399 unsigned long kexinit_len
;
401 const LIBSSH2_CRYPT_METHOD
*crypt
;
402 void *crypt_abstract
;
404 const LIBSSH2_MAC_METHOD
*mac
;
408 const LIBSSH2_COMP_METHOD
*comp
;
411 /* Method Preferences -- NULL yields "load order" */
416 } libssh2_endpoint_data
;
418 #define PACKETBUFSIZE 4096
420 struct transportpacket
422 /* ------------- for incoming data --------------- */
423 unsigned char buf
[PACKETBUFSIZE
];
424 unsigned char init
[5]; /* first 5 bytes of the incoming data stream,
426 int writeidx
; /* at what array index we do the next write into
428 int readidx
; /* at what array index we do the next read from
430 int packet_length
; /* the most recent packet_length as read from the
432 int padding_length
; /* the most recent padding_length as read from the
434 int data_num
; /* How much of the total package that has been read
436 int total_num
; /* How much a total package is supposed to be, in
437 number of bytes. A full package is
438 packet_length + padding_length + 4 +
440 unsigned char *payload
; /* this is a pointer to a LIBSSH2_ALLOC()
441 area to which we write decrypted data */
442 unsigned char *wptr
; /* write pointer into the payload to where we
443 are currently writing decrypted data */
445 /* ------------- for outgoing data --------------- */
446 unsigned char *outbuf
; /* pointer to a LIBSSH2_ALLOC() area for the
448 int ototal_num
; /* size of outbuf in number of bytes */
449 unsigned char *odata
; /* original pointer to the data we stored in
451 unsigned long olen
; /* original size of the data we stored in
453 unsigned long osent
; /* number of bytes already sent */
456 struct _LIBSSH2_PUBLICKEY
458 LIBSSH2_CHANNEL
*channel
;
459 unsigned long version
;
461 /* State variables used in libssh2_publickey_packet_receive() */
462 libssh2_nonblocking_states receive_state
;
463 unsigned char *receive_packet
;
464 unsigned long receive_packet_len
;
466 /* State variables used in libssh2_publickey_add_ex() */
467 libssh2_nonblocking_states add_state
;
468 unsigned char *add_packet
;
469 unsigned char *add_s
;
471 /* State variables used in libssh2_publickey_remove_ex() */
472 libssh2_nonblocking_states remove_state
;
473 unsigned char *remove_packet
;
474 unsigned char *remove_s
;
476 /* State variables used in libssh2_publickey_list_fetch() */
477 libssh2_nonblocking_states listFetch_state
;
478 unsigned char *listFetch_s
;
479 unsigned char listFetch_buffer
[12];
480 unsigned char *listFetch_data
;
481 unsigned long listFetch_data_len
;
484 struct _LIBSSH2_SFTP_HANDLE
487 LIBSSH2_SFTP_HANDLE
*prev
, *next
;
494 union _libssh2_sftp_handle_data
496 struct _libssh2_sftp_handle_file_data
498 libssh2_uint64_t offset
;
500 struct _libssh2_sftp_handle_dir_data
502 unsigned long names_left
;
508 /* State variables used in libssh2_sftp_close_handle() */
509 libssh2_nonblocking_states close_state
;
510 unsigned long close_request_id
;
511 unsigned char *close_packet
;
516 LIBSSH2_CHANNEL
*channel
;
518 unsigned long request_id
, version
;
520 LIBSSH2_PACKET_BRIGADE packets
;
522 LIBSSH2_SFTP_HANDLE
*handles
;
524 unsigned long last_errno
;
526 /* Holder for partial packet, use in libssh2_sftp_packet_read() */
527 unsigned char *partial_packet
; /* The data */
528 unsigned long partial_len
; /* Desired number of bytes */
529 unsigned long partial_received
; /* Bytes received so far */
531 /* Time that libssh2_sftp_packet_requirev() started reading */
532 time_t requirev_start
;
534 /* State variables used in libssh2_sftp_open_ex() */
535 libssh2_nonblocking_states open_state
;
536 unsigned char *open_packet
;
537 ssize_t open_packet_len
;
538 unsigned long open_request_id
;
540 /* State variables used in libssh2_sftp_read() */
541 libssh2_nonblocking_states read_state
;
542 unsigned char *read_packet
;
543 unsigned long read_request_id
;
544 size_t read_total_read
;
546 /* State variables used in libssh2_sftp_readdir() */
547 libssh2_nonblocking_states readdir_state
;
548 unsigned char *readdir_packet
;
549 unsigned long readdir_request_id
;
551 /* State variables used in libssh2_sftp_write() */
552 libssh2_nonblocking_states write_state
;
553 unsigned char *write_packet
;
554 unsigned long write_request_id
;
556 /* State variables used in libssh2_sftp_fstat_ex() */
557 libssh2_nonblocking_states fstat_state
;
558 unsigned char *fstat_packet
;
559 unsigned long fstat_request_id
;
561 /* State variables used in libssh2_sftp_unlink_ex() */
562 libssh2_nonblocking_states unlink_state
;
563 unsigned char *unlink_packet
;
564 unsigned long unlink_request_id
;
566 /* State variables used in libssh2_sftp_rename_ex() */
567 libssh2_nonblocking_states rename_state
;
568 unsigned char *rename_packet
;
569 unsigned char *rename_s
;
570 unsigned long rename_request_id
;
572 /* State variables used in libssh2_sftp_mkdir() */
573 libssh2_nonblocking_states mkdir_state
;
574 unsigned char *mkdir_packet
;
575 unsigned long mkdir_request_id
;
577 /* State variables used in libssh2_sftp_rmdir() */
578 libssh2_nonblocking_states rmdir_state
;
579 unsigned char *rmdir_packet
;
580 unsigned long rmdir_request_id
;
582 /* State variables used in libssh2_sftp_stat() */
583 libssh2_nonblocking_states stat_state
;
584 unsigned char *stat_packet
;
585 unsigned long stat_request_id
;
587 /* State variables used in libssh2_sftp_symlink() */
588 libssh2_nonblocking_states symlink_state
;
589 unsigned char *symlink_packet
;
590 unsigned long symlink_request_id
;
593 #define LIBSSH2_SCP_RESPONSE_BUFLEN 256
595 struct _LIBSSH2_SESSION
597 /* Memory management callbacks */
599 LIBSSH2_ALLOC_FUNC((*alloc
));
600 LIBSSH2_REALLOC_FUNC((*realloc
));
601 LIBSSH2_FREE_FUNC((*free
));
603 /* Other callbacks */
604 LIBSSH2_IGNORE_FUNC((*ssh_msg_ignore
));
605 LIBSSH2_DEBUG_FUNC((*ssh_msg_debug
));
606 LIBSSH2_DISCONNECT_FUNC((*ssh_msg_disconnect
));
607 LIBSSH2_MACERROR_FUNC((*macerror
));
608 LIBSSH2_X11_OPEN_FUNC((*x11
));
610 /* Method preferences -- NULL yields "load order" */
617 /* Agreed Key Exchange Method */
618 const LIBSSH2_KEX_METHOD
*kex
;
619 int burn_optimistic_kexinit
:1;
621 unsigned char *session_id
;
622 unsigned long session_id_len
;
624 /* Server's public key */
625 const LIBSSH2_HOSTKEY_METHOD
*hostkey
;
626 void *server_hostkey_abstract
;
628 /* Either set with libssh2_session_hostkey() (for server mode)
629 * Or read from server in (eg) KEXDH_INIT (for client mode)
631 unsigned char *server_hostkey
;
632 unsigned long server_hostkey_len
;
634 unsigned char server_hostkey_md5
[MD5_DIGEST_LENGTH
];
635 #endif /* ! LIBSSH2_MD5 */
636 unsigned char server_hostkey_sha1
[SHA_DIGEST_LENGTH
];
638 /* (remote as source of data -- packet_read ) */
639 libssh2_endpoint_data remote
;
641 /* (local as source of data -- packet_write ) */
642 libssh2_endpoint_data local
;
644 /* Inbound Data buffer -- Sometimes the packet that comes in isn't the packet we're ready for */
645 LIBSSH2_PACKET_BRIGADE packets
;
647 /* Active connection channels */
648 LIBSSH2_CHANNEL_BRIGADE channels
;
649 unsigned long next_channel
;
651 LIBSSH2_LISTENER
*listeners
;
653 /* Actual I/O socket */
660 unsigned long err_msglen
;
664 /* struct members for packet-level reading */
665 struct transportpacket packet
;
667 int showmask
; /* what debug/trace messages to display */
670 /* State variables used in libssh2_banner_send() */
671 libssh2_nonblocking_states banner_TxRx_state
;
672 char banner_TxRx_banner
[256];
673 ssize_t banner_TxRx_total_send
;
675 /* State variables used in libssh2_kexinit() */
676 libssh2_nonblocking_states kexinit_state
;
677 unsigned char *kexinit_data
;
678 size_t kexinit_data_len
;
680 /* State variables used in libssh2_session_startup() */
681 libssh2_nonblocking_states startup_state
;
682 unsigned char *startup_data
;
683 unsigned long startup_data_len
;
684 unsigned char startup_service
[sizeof("ssh-userauth") + 5 - 1];
685 unsigned long startup_service_length
;
686 packet_require_state_t startup_req_state
;
687 key_exchange_state_t startup_key_state
;
689 /* State variables used in libssh2_session_free() */
690 libssh2_nonblocking_states free_state
;
692 /* State variables used in libssh2_session_disconnect_ex() */
693 libssh2_nonblocking_states disconnect_state
;
694 unsigned char *disconnect_data
;
695 unsigned long disconnect_data_len
;
697 /* State variables used in libssh2_packet_read() */
698 libssh2_nonblocking_states readPack_state
;
699 int readPack_encrypted
;
701 /* State variables used in libssh2_userauth_list() */
702 libssh2_nonblocking_states userauth_list_state
;
703 unsigned char *userauth_list_data
;
704 unsigned long userauth_list_data_len
;
705 packet_requirev_state_t userauth_list_packet_requirev_state
;
707 /* State variables used in libssh2_userauth_password_ex() */
708 libssh2_nonblocking_states userauth_pswd_state
;
709 unsigned char *userauth_pswd_data
;
710 unsigned char userauth_pswd_data0
;
711 unsigned long userauth_pswd_data_len
;
712 char *userauth_pswd_newpw
;
713 int userauth_pswd_newpw_len
;
714 packet_requirev_state_t userauth_pswd_packet_requirev_state
;
716 /* State variables used in libssh2_userauth_hostbased_fromfile_ex() */
717 libssh2_nonblocking_states userauth_host_state
;
718 unsigned char *userauth_host_data
;
719 unsigned long userauth_host_data_len
;
720 unsigned char *userauth_host_packet
;
721 unsigned long userauth_host_packet_len
;
722 unsigned char *userauth_host_method
;
723 unsigned long userauth_host_method_len
;
724 unsigned char *userauth_host_s
;
725 packet_requirev_state_t userauth_host_packet_requirev_state
;
727 /* State variables used in libssh2_userauth_publickey_fromfile_ex() */
728 libssh2_nonblocking_states userauth_pblc_state
;
729 unsigned char *userauth_pblc_data
;
730 unsigned long userauth_pblc_data_len
;
731 unsigned char *userauth_pblc_packet
;
732 unsigned long userauth_pblc_packet_len
;
733 unsigned char *userauth_pblc_method
;
734 unsigned long userauth_pblc_method_len
;
735 unsigned char *userauth_pblc_s
;
736 unsigned char *userauth_pblc_b
;
737 packet_requirev_state_t userauth_pblc_packet_requirev_state
;
739 /* State variables used in llibssh2_userauth_keyboard_interactive_ex() */
740 libssh2_nonblocking_states userauth_kybd_state
;
741 unsigned char *userauth_kybd_data
;
742 unsigned long userauth_kybd_data_len
;
743 unsigned char *userauth_kybd_packet
;
744 unsigned long userauth_kybd_packet_len
;
745 unsigned int userauth_kybd_auth_name_len
;
746 char *userauth_kybd_auth_name
;
747 unsigned userauth_kybd_auth_instruction_len
;
748 char *userauth_kybd_auth_instruction
;
749 unsigned int userauth_kybd_num_prompts
;
750 int userauth_kybd_auth_failure
;
751 LIBSSH2_USERAUTH_KBDINT_PROMPT
*userauth_kybd_prompts
;
752 LIBSSH2_USERAUTH_KBDINT_RESPONSE
*userauth_kybd_responses
;
753 packet_requirev_state_t userauth_kybd_packet_requirev_state
;
755 /* State variables used in libssh2_channel_open_ex() */
756 libssh2_nonblocking_states open_state
;
757 packet_requirev_state_t open_packet_requirev_state
;
758 LIBSSH2_CHANNEL
*open_channel
;
759 unsigned char *open_packet
;
760 unsigned long open_packet_len
;
761 unsigned char *open_data
;
762 unsigned long open_data_len
;
763 unsigned long open_local_channel
;
765 /* State variables used in libssh2_channel_direct_tcpip_ex() */
766 libssh2_nonblocking_states direct_state
;
767 unsigned char *direct_message
;
768 unsigned long direct_host_len
;
769 unsigned long direct_shost_len
;
770 unsigned long direct_message_len
;
772 /* State variables used in libssh2_channel_forward_listen_ex() */
773 libssh2_nonblocking_states fwdLstn_state
;
774 unsigned char *fwdLstn_packet
;
775 unsigned long fwdLstn_host_len
;
776 unsigned long fwdLstn_packet_len
;
777 packet_requirev_state_t fwdLstn_packet_requirev_state
;
779 /* State variables used in libssh2_publickey_init() */
780 libssh2_nonblocking_states pkeyInit_state
;
781 LIBSSH2_PUBLICKEY
*pkeyInit_pkey
;
782 LIBSSH2_CHANNEL
*pkeyInit_channel
;
783 unsigned char *pkeyInit_data
;
784 unsigned long pkeyInit_data_len
;
786 /* State variables used in libssh2_packet_add() */
787 libssh2_nonblocking_states packAdd_state
;
788 LIBSSH2_PACKET
*packAdd_packet
;
789 LIBSSH2_CHANNEL
*packAdd_channel
;
790 unsigned long packAdd_data_head
;
791 key_exchange_state_t packAdd_key_state
;
792 packet_queue_listener_state_t packAdd_Qlstn_state
;
793 packet_x11_open_state_t packAdd_x11open_state
;
795 /* State variables used in fullpacket() */
796 libssh2_nonblocking_states fullpacket_state
;
797 int fullpacket_macstate
;
798 int fullpacket_payload_len
;
799 libssh2pack_t fullpacket_packet_type
;
801 /* State variables used in libssh2_sftp_init() */
802 libssh2_nonblocking_states sftpInit_state
;
803 LIBSSH2_SFTP
*sftpInit_sftp
;
804 LIBSSH2_CHANNEL
*sftpInit_channel
;
805 unsigned char sftpInit_buffer
[9]; /* sftp_header(5){excludes request_id} + version_id(4) */
807 /* State variables used in libssh2_scp_recv() */
808 libssh2_nonblocking_states scpRecv_state
;
809 unsigned char *scpRecv_command
;
810 unsigned long scpRecv_command_len
;
811 unsigned char scpRecv_response
[LIBSSH2_SCP_RESPONSE_BUFLEN
];
812 unsigned long scpRecv_response_len
;
817 char *scpRecv_err_msg
;
818 long scpRecv_err_len
;
819 LIBSSH2_CHANNEL
*scpRecv_channel
;
821 /* State variables used in libssh2_scp_send_ex() */
822 libssh2_nonblocking_states scpSend_state
;
823 unsigned char *scpSend_command
;
824 unsigned long scpSend_command_len
;
825 unsigned char scpSend_response
[LIBSSH2_SCP_RESPONSE_BUFLEN
];
826 unsigned long scpSend_response_len
;
827 char *scpSend_err_msg
;
828 long scpSend_err_len
;
829 LIBSSH2_CHANNEL
*scpSend_channel
;
832 /* session.state bits */
833 #define LIBSSH2_STATE_EXCHANGING_KEYS 0x00000001
834 #define LIBSSH2_STATE_NEWKEYS 0x00000002
835 #define LIBSSH2_STATE_AUTHENTICATED 0x00000004
837 /* session.flag helpers */
839 #define LIBSSH2_SOCKET_SEND_FLAGS(session) (((session)->flags & LIBSSH2_FLAG_SIGPIPE) ? 0 : MSG_NOSIGNAL)
840 #define LIBSSH2_SOCKET_RECV_FLAGS(session) (((session)->flags & LIBSSH2_FLAG_SIGPIPE) ? 0 : MSG_NOSIGNAL)
842 /* If MSG_NOSIGNAL isn't defined we're SOL on blocking SIGPIPE */
843 #define LIBSSH2_SOCKET_SEND_FLAGS(session) 0
844 #define LIBSSH2_SOCKET_RECV_FLAGS(session) 0
847 /* libssh2 extensible ssh api, ultimately I'd like to allow loading additional methods via .so/.dll */
849 struct _LIBSSH2_KEX_METHOD
853 /* Key exchange, populates session->* and returns 0 on success, non-0 on error */
854 int (*exchange_keys
) (LIBSSH2_SESSION
* session
,
855 key_exchange_state_low_t
* key_state
);
860 struct _LIBSSH2_HOSTKEY_METHOD
863 unsigned long hash_len
;
865 int (*init
) (LIBSSH2_SESSION
* session
, const unsigned char *hostkey_data
,
866 unsigned long hostkey_data_len
, void **abstract
);
867 int (*initPEM
) (LIBSSH2_SESSION
* session
, const char *privkeyfile
,
868 unsigned const char *passphrase
, void **abstract
);
869 int (*sig_verify
) (LIBSSH2_SESSION
* session
, const unsigned char *sig
,
870 unsigned long sig_len
, const unsigned char *m
,
871 unsigned long m_len
, void **abstract
);
872 int (*signv
) (LIBSSH2_SESSION
* session
, unsigned char **signature
,
873 unsigned long *signature_len
, unsigned long veccount
,
874 const struct iovec datavec
[], void **abstract
);
875 int (*encrypt
) (LIBSSH2_SESSION
* session
, unsigned char **dst
,
876 unsigned long *dst_len
, const unsigned char *src
,
877 unsigned long src_len
, void **abstract
);
878 int (*dtor
) (LIBSSH2_SESSION
* session
, void **abstract
);
881 struct _LIBSSH2_CRYPT_METHOD
887 /* iv and key sizes (-1 for variable length) */
893 int (*init
) (LIBSSH2_SESSION
* session
,
894 const LIBSSH2_CRYPT_METHOD
* method
, unsigned char *iv
,
895 int *free_iv
, unsigned char *secret
, int *free_secret
,
896 int encrypt
, void **abstract
);
897 int (*crypt
) (LIBSSH2_SESSION
* session
, unsigned char *block
,
899 int (*dtor
) (LIBSSH2_SESSION
* session
, void **abstract
);
901 _libssh2_cipher_type(algo
);
904 struct _LIBSSH2_COMP_METHOD
908 int (*init
) (LIBSSH2_SESSION
* session
, int compress
, void **abstract
);
909 int (*comp
) (LIBSSH2_SESSION
* session
, int compress
, unsigned char **dest
,
910 unsigned long *dest_len
, unsigned long payload_limit
,
911 int *free_dest
, const unsigned char *src
,
912 unsigned long src_len
, void **abstract
);
913 int (*dtor
) (LIBSSH2_SESSION
* session
, int compress
, void **abstract
);
916 struct _LIBSSH2_MAC_METHOD
920 /* The length of a given MAC packet */
923 /* integrity key length */
926 /* Message Authentication Code Hashing algo */
927 int (*init
) (LIBSSH2_SESSION
* session
, unsigned char *key
, int *free_key
,
929 int (*hash
) (LIBSSH2_SESSION
* session
, unsigned char *buf
,
930 unsigned long seqno
, const unsigned char *packet
,
931 unsigned long packet_len
, const unsigned char *addtl
,
932 unsigned long addtl_len
, void **abstract
);
933 int (*dtor
) (LIBSSH2_SESSION
* session
, void **abstract
);
936 #define LIBSSH2_DBG_TRANS 1
937 #define LIBSSH2_DBG_KEX 2
938 #define LIBSSH2_DBG_AUTH 3
939 #define LIBSSH2_DBG_CONN 4
940 #define LIBSSH2_DBG_SCP 5
941 #define LIBSSH2_DBG_SFTP 6
942 #define LIBSSH2_DBG_ERROR 7
943 #define LIBSSH2_DBG_PUBLICKEY 8
945 void _libssh2_debug(LIBSSH2_SESSION
* session
, int context
, const char *format
,
948 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
950 #define _libssh2_debug(x,y,z, __VA_ARGS__) do {} while (0)
951 #elif defined(__GNUC__)
953 #define _libssh2_debug(x,y,z,...) do {} while (0)
955 /* no gcc and not C99, do static and hopefully inline */
957 _libssh2_debug(LIBSSH2_SESSION
* session
, int context
, const char *format
, ...)
964 #define libssh2_error(session, errcode, errmsg, should_free) \
966 if (session->err_msg && session->err_should_free) { \
967 LIBSSH2_FREE(session, session->err_msg); \
969 session->err_msg = (char *)errmsg; \
970 session->err_msglen = strlen(errmsg); \
971 session->err_should_free = should_free; \
972 session->err_code = errcode; \
973 _libssh2_debug(session, LIBSSH2_DBG_ERROR, "%d - %s", session->err_code, session->err_msg); \
976 #else /* ! LIBSSH2DEBUG */
978 #define libssh2_error(session, errcode, errmsg, should_free) \
980 if (session->err_msg && session->err_should_free) { \
981 LIBSSH2_FREE(session, session->err_msg); \
983 session->err_msg = (char *)errmsg; \
984 session->err_msglen = strlen(errmsg); \
985 session->err_should_free = should_free; \
986 session->err_code = errcode; \
989 #endif /* ! LIBSSH2DEBUG */
992 #define LIBSSH2_SOCKET_UNKNOWN 1
993 #define LIBSSH2_SOCKET_CONNECTED 0
994 #define LIBSSH2_SOCKET_DISCONNECTED -1
996 /* Initial packet state, prior to MAC check */
997 #define LIBSSH2_MAC_UNCONFIRMED 1
998 /* When MAC type is "none" (proto initiation phase) all packets are deemed "confirmed" */
999 #define LIBSSH2_MAC_CONFIRMED 0
1000 /* Something very bad is going on */
1001 #define LIBSSH2_MAC_INVALID -1
1003 /* SSH Packet Types -- Defined by internet draft */
1004 /* Transport Layer */
1005 #define SSH_MSG_DISCONNECT 1
1006 #define SSH_MSG_IGNORE 2
1007 #define SSH_MSG_UNIMPLEMENTED 3
1008 #define SSH_MSG_DEBUG 4
1009 #define SSH_MSG_SERVICE_REQUEST 5
1010 #define SSH_MSG_SERVICE_ACCEPT 6
1012 #define SSH_MSG_KEXINIT 20
1013 #define SSH_MSG_NEWKEYS 21
1015 /* diffie-hellman-group1-sha1 */
1016 #define SSH_MSG_KEXDH_INIT 30
1017 #define SSH_MSG_KEXDH_REPLY 31
1019 /* diffie-hellman-group-exchange-sha1 */
1020 #define SSH_MSG_KEX_DH_GEX_REQUEST_OLD 30
1021 #define SSH_MSG_KEX_DH_GEX_REQUEST 34
1022 #define SSH_MSG_KEX_DH_GEX_GROUP 31
1023 #define SSH_MSG_KEX_DH_GEX_INIT 32
1024 #define SSH_MSG_KEX_DH_GEX_REPLY 33
1026 /* User Authentication */
1027 #define SSH_MSG_USERAUTH_REQUEST 50
1028 #define SSH_MSG_USERAUTH_FAILURE 51
1029 #define SSH_MSG_USERAUTH_SUCCESS 52
1030 #define SSH_MSG_USERAUTH_BANNER 53
1032 /* "public key" method */
1033 #define SSH_MSG_USERAUTH_PK_OK 60
1034 /* "password" method */
1035 #define SSH_MSG_USERAUTH_PASSWD_CHANGEREQ 60
1036 /* "keyboard-interactive" method */
1037 #define SSH_MSG_USERAUTH_INFO_REQUEST 60
1038 #define SSH_MSG_USERAUTH_INFO_RESPONSE 61
1041 #define SSH_MSG_GLOBAL_REQUEST 80
1042 #define SSH_MSG_REQUEST_SUCCESS 81
1043 #define SSH_MSG_REQUEST_FAILURE 82
1045 #define SSH_MSG_CHANNEL_OPEN 90
1046 #define SSH_MSG_CHANNEL_OPEN_CONFIRMATION 91
1047 #define SSH_MSG_CHANNEL_OPEN_FAILURE 92
1048 #define SSH_MSG_CHANNEL_WINDOW_ADJUST 93
1049 #define SSH_MSG_CHANNEL_DATA 94
1050 #define SSH_MSG_CHANNEL_EXTENDED_DATA 95
1051 #define SSH_MSG_CHANNEL_EOF 96
1052 #define SSH_MSG_CHANNEL_CLOSE 97
1053 #define SSH_MSG_CHANNEL_REQUEST 98
1054 #define SSH_MSG_CHANNEL_SUCCESS 99
1055 #define SSH_MSG_CHANNEL_FAILURE 100
1057 void libssh2_session_shutdown(LIBSSH2_SESSION
* session
);
1059 unsigned long libssh2_ntohu32(const unsigned char *buf
);
1060 libssh2_uint64_t
libssh2_ntohu64(const unsigned char *buf
);
1061 void libssh2_htonu32(unsigned char *buf
, unsigned long val
);
1062 void libssh2_htonu64(unsigned char *buf
, libssh2_uint64_t val
);
1064 #define LIBSSH2_READ_TIMEOUT 60 /* generic timeout in seconds used when
1065 waiting for more data to arrive */
1066 int libssh2_waitsocket(LIBSSH2_SESSION
* session
, long seconds
);
1069 /* CAUTION: some of these error codes are returned in the public API and is
1070 there known with other #defined names from the public header file. They
1071 should not be changed. */
1073 #define PACKET_TIMEOUT -7
1074 #define PACKET_BADUSE -6
1075 #define PACKET_COMPRESS -5
1076 #define PACKET_TOOBIG -4
1077 #define PACKET_ENOMEM -3
1078 #define PACKET_EAGAIN LIBSSH2_ERROR_EAGAIN
1079 #define PACKET_FAIL -1
1080 #define PACKET_NONE 0
1082 libssh2pack_t
libssh2_packet_read(LIBSSH2_SESSION
* session
);
1084 int libssh2_packet_ask_ex(LIBSSH2_SESSION
* session
, unsigned char packet_type
,
1085 unsigned char **data
, unsigned long *data_len
,
1086 unsigned long match_ofs
,
1087 const unsigned char *match_buf
,
1088 unsigned long match_len
, int poll_socket
);
1090 int libssh2_packet_askv_ex(LIBSSH2_SESSION
* session
,
1091 const unsigned char *packet_types
,
1092 unsigned char **data
, unsigned long *data_len
,
1093 unsigned long match_ofs
,
1094 const unsigned char *match_buf
,
1095 unsigned long match_len
, int poll_socket
);
1096 int libssh2_packet_require_ex(LIBSSH2_SESSION
* session
,
1097 unsigned char packet_type
, unsigned char **data
,
1098 unsigned long *data_len
, unsigned long match_ofs
,
1099 const unsigned char *match_buf
,
1100 unsigned long match_len
,
1101 packet_require_state_t
* state
);
1102 int libssh2_packet_requirev_ex(LIBSSH2_SESSION
* session
,
1103 const unsigned char *packet_types
,
1104 unsigned char **data
, unsigned long *data_len
,
1105 unsigned long match_ofs
,
1106 const unsigned char *match_buf
,
1107 unsigned long match_len
,
1108 packet_requirev_state_t
* state
);
1109 int libssh2_packet_burn(LIBSSH2_SESSION
* session
,
1110 libssh2_nonblocking_states
* state
);
1111 int libssh2_packet_write(LIBSSH2_SESSION
* session
, unsigned char *data
,
1112 unsigned long data_len
);
1113 int libssh2_packet_add(LIBSSH2_SESSION
* session
, unsigned char *data
,
1114 size_t datalen
, int macstate
);
1115 int libssh2_kex_exchange(LIBSSH2_SESSION
* session
, int reexchange
,
1116 key_exchange_state_t
* state
);
1117 unsigned long libssh2_channel_nextid(LIBSSH2_SESSION
* session
);
1118 LIBSSH2_CHANNEL
*libssh2_channel_locate(LIBSSH2_SESSION
* session
,
1119 unsigned long channel_id
);
1120 unsigned long libssh2_channel_packet_data_len(LIBSSH2_CHANNEL
* channel
,
1123 /* this is the lib-internal set blocking function */
1124 int _libssh2_session_set_blocking(LIBSSH2_SESSION
* session
, int blocking
);
1126 /* Let crypt.c/hostkey.c/comp.c/mac.c expose their method structs */
1127 const LIBSSH2_CRYPT_METHOD
**libssh2_crypt_methods(void);
1128 const LIBSSH2_HOSTKEY_METHOD
**libssh2_hostkey_methods(void);
1129 const LIBSSH2_COMP_METHOD
**libssh2_comp_methods(void);
1130 const LIBSSH2_MAC_METHOD
**libssh2_mac_methods(void);
1132 /* Language API doesn't exist yet. Just act like we've agreed on a language */
1133 #define libssh2_kex_agree_lang(session, endpoint, str, str_len) 0
1136 int _libssh2_pem_parse(LIBSSH2_SESSION
* session
,
1137 const char *headerbegin
,
1138 const char *headerend
,
1139 FILE * fp
, char **data
, unsigned int *datalen
);
1140 int _libssh2_pem_decode_sequence(unsigned char **data
, unsigned int *datalen
);
1141 int _libssh2_pem_decode_integer(unsigned char **data
, unsigned int *datalen
,
1142 unsigned char **i
, unsigned int *ilen
);
1144 #endif /* LIBSSH2_H */