libssh  0.7.2
include/libssh/session.h
00001 /*
00002  * This file is part of the SSH Library
00003  *
00004  * Copyright (c) 2009 by Aris Adamantiadis
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #ifndef SESSION_H_
00022 #define SESSION_H_
00023 #include "libssh/priv.h"
00024 #include "libssh/kex.h"
00025 #include "libssh/packet.h"
00026 #include "libssh/pcap.h"
00027 #include "libssh/auth.h"
00028 #include "libssh/channels.h"
00029 #include "libssh/poll.h"
00030 
00031 /* These are the different states a SSH session can be into its life */
00032 enum ssh_session_state_e {
00033   SSH_SESSION_STATE_NONE=0,
00034   SSH_SESSION_STATE_CONNECTING,
00035   SSH_SESSION_STATE_SOCKET_CONNECTED,
00036   SSH_SESSION_STATE_BANNER_RECEIVED,
00037   SSH_SESSION_STATE_INITIAL_KEX,
00038   SSH_SESSION_STATE_KEXINIT_RECEIVED,
00039   SSH_SESSION_STATE_DH,
00040   SSH_SESSION_STATE_AUTHENTICATING,
00041   SSH_SESSION_STATE_AUTHENTICATED,
00042   SSH_SESSION_STATE_ERROR,
00043   SSH_SESSION_STATE_DISCONNECTED
00044 };
00045 
00046 enum ssh_dh_state_e {
00047   DH_STATE_INIT=0,
00048   DH_STATE_INIT_SENT,
00049   DH_STATE_NEWKEYS_SENT,
00050   DH_STATE_FINISHED
00051 };
00052 
00053 enum ssh_pending_call_e {
00054   SSH_PENDING_CALL_NONE = 0,
00055   SSH_PENDING_CALL_CONNECT,
00056   SSH_PENDING_CALL_AUTH_NONE,
00057   SSH_PENDING_CALL_AUTH_PASSWORD,
00058   SSH_PENDING_CALL_AUTH_OFFER_PUBKEY,
00059   SSH_PENDING_CALL_AUTH_PUBKEY,
00060   SSH_PENDING_CALL_AUTH_AGENT,
00061   SSH_PENDING_CALL_AUTH_KBDINT_INIT,
00062   SSH_PENDING_CALL_AUTH_KBDINT_SEND,
00063   SSH_PENDING_CALL_AUTH_GSSAPI_MIC
00064 };
00065 
00066 /* libssh calls may block an undefined amount of time */
00067 #define SSH_SESSION_FLAG_BLOCKING 1
00068 
00069 /* Client successfully authenticated */
00070 #define SSH_SESSION_FLAG_AUTHENTICATED 2
00071 
00072 /* codes to use with ssh_handle_packets*() */
00073 /* Infinite timeout */
00074 #define SSH_TIMEOUT_INFINITE -1
00075 /* Use the timeout defined by user if any. Mostly used with new connections */
00076 #define SSH_TIMEOUT_USER -2
00077 /* Use the default timeout, depending on ssh_is_blocking() */
00078 #define SSH_TIMEOUT_DEFAULT -3
00079 /* Don't block at all */
00080 #define SSH_TIMEOUT_NONBLOCKING 0
00081 
00082 /* members that are common to ssh_session and ssh_bind */
00083 struct ssh_common_struct {
00084     struct error_struct error;
00085     ssh_callbacks callbacks; /* Callbacks to user functions */
00086     int log_verbosity; /* verbosity of the log functions */
00087 };
00088 
00089 struct ssh_session_struct {
00090     struct ssh_common_struct common;
00091     struct ssh_socket_struct *socket;
00092     char *serverbanner;
00093     char *clientbanner;
00094     int protoversion;
00095     int server;
00096     int client;
00097     int openssh;
00098     uint32_t send_seq;
00099     uint32_t recv_seq;
00100 
00101     int connected;
00102     /* !=0 when the user got a session handle */
00103     int alive;
00104     /* two previous are deprecated */
00105     /* int auth_service_asked; */
00106 
00107     /* session flags (SSH_SESSION_FLAG_*) */
00108     int flags;
00109 
00110     ssh_string banner; /* that's the issue banner from
00111                        the server */
00112     char *discon_msg; /* disconnect message from
00113                          the remote host */
00114     ssh_buffer in_buffer;
00115     PACKET in_packet;
00116     ssh_buffer out_buffer;
00117 
00118     /* the states are used by the nonblocking stuff to remember */
00119     /* where it was before being interrupted */
00120     enum ssh_pending_call_e pending_call_state;
00121     enum ssh_session_state_e session_state;
00122     int packet_state;
00123     enum ssh_dh_state_e dh_handshake_state;
00124     enum ssh_auth_service_state_e auth_service_state;
00125     enum ssh_auth_state_e auth_state;
00126     enum ssh_channel_request_state_e global_req_state;
00127     struct ssh_agent_state_struct *agent_state;
00128     struct ssh_auth_auto_state_struct *auth_auto_state;
00129 
00130     /*
00131      * RFC 4253, 7.1: if the first_kex_packet_follows flag was set in
00132      * the received SSH_MSG_KEXINIT, but the guess was wrong, this
00133      * field will be set such that the following guessed packet will
00134      * be ignored.  Once that packet has been received and ignored,
00135      * this field is cleared.
00136      */
00137     int first_kex_follows_guess_wrong;
00138 
00139     ssh_buffer in_hashbuf;
00140     ssh_buffer out_hashbuf;
00141     struct ssh_crypto_struct *current_crypto;
00142     struct ssh_crypto_struct *next_crypto;  /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */
00143 
00144     struct ssh_list *channels; /* linked list of channels */
00145     int maxchannel;
00146     int exec_channel_opened; /* version 1 only. more
00147                                 info in channels1.c */
00148     ssh_agent agent; /* ssh agent */
00149 
00150 /* keyb interactive data */
00151     struct ssh_kbdint_struct *kbdint;
00152     struct ssh_gssapi_struct *gssapi;
00153     int version; /* 1 or 2 */
00154     /* server host keys */
00155     struct {
00156         ssh_key rsa_key;
00157         ssh_key dsa_key;
00158         ssh_key ecdsa_key;
00159         ssh_key ed25519_key;
00160         /* The type of host key wanted by client */
00161         enum ssh_keytypes_e hostkey;
00162     } srv;
00163     /* auths accepted by server */
00164     int auth_methods;
00165     struct ssh_list *ssh_message_list; /* list of delayed SSH messages */
00166     int (*ssh_message_callback)( struct ssh_session_struct *session, ssh_message msg, void *userdata);
00167     void *ssh_message_callback_data;
00168     ssh_server_callbacks server_callbacks;
00169     void (*ssh_connection_callback)( struct ssh_session_struct *session);
00170     struct ssh_packet_callbacks_struct default_packet_callbacks;
00171     struct ssh_list *packet_callbacks;
00172     struct ssh_socket_callbacks_struct socket_callbacks;
00173     ssh_poll_ctx default_poll_ctx;
00174     /* options */
00175 #ifdef WITH_PCAP
00176     ssh_pcap_context pcap_ctx; /* pcap debugging context */
00177 #endif
00178     struct {
00179         struct ssh_list *identity;
00180         char *username;
00181         char *host;
00182         char *bindaddr; /* bind the client to an ip addr */
00183         char *sshdir;
00184         char *knownhosts;
00185         char *wanted_methods[10];
00186         char *ProxyCommand;
00187         char *custombanner;
00188         unsigned long timeout; /* seconds */
00189         unsigned long timeout_usec;
00190         unsigned int port;
00191         socket_t fd;
00192         int StrictHostKeyChecking;
00193         int ssh2;
00194         int ssh1;
00195         char compressionlevel;
00196         char *gss_server_identity;
00197         char *gss_client_identity;
00198         int gss_delegate_creds;
00199     } opts;
00200     /* counters */
00201     ssh_counter socket_counter;
00202     ssh_counter raw_counter;
00203 };
00204 
00210 typedef int (*ssh_termination_function)(void *user);
00211 int ssh_handle_packets(ssh_session session, int timeout);
00212 int ssh_handle_packets_termination(ssh_session session, int timeout,
00213     ssh_termination_function fct, void *user);
00214 void ssh_socket_exception_callback(int code, int errno_code, void *user);
00215 
00216 #endif /* SESSION_H_ */