1 /*
2  * This file is part of the SSH Library
3  *
4  * Copyright (c) 2009 by Aris Adamantiadis
5  *
6  * The SSH Library is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or (at your
9  * option) any later version.
10  *
11  * The SSH Library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with the SSH Library; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19  * MA 02111-1307, USA.
20  */
21 
22 #ifndef SESSION_H_
23 #define SESSION_H_
24 #include "libssh/priv.h"
25 #include "libssh/kex.h"
26 #include "libssh/packet.h"
27 #include "libssh/pcap.h"
28 #include "libssh/auth.h"
29 #include "libssh/channels.h"
30 #include "libssh/poll.h"
31 
32 /* These are the different states a SSH session can be into its life */
33 enum ssh_session_state_e {
34 	SSH_SESSION_STATE_NONE=0,
35 	SSH_SESSION_STATE_CONNECTING,
36 	SSH_SESSION_STATE_SOCKET_CONNECTED,
37 	SSH_SESSION_STATE_BANNER_RECEIVED,
38 	SSH_SESSION_STATE_INITIAL_KEX,
39 	SSH_SESSION_STATE_KEXINIT_RECEIVED,
40 	SSH_SESSION_STATE_DH,
41 	SSH_SESSION_STATE_AUTHENTICATING,
42 	SSH_SESSION_STATE_AUTHENTICATED,
43 	SSH_SESSION_STATE_ERROR,
44 	SSH_SESSION_STATE_DISCONNECTED
45 };
46 
47 enum ssh_dh_state_e {
48   DH_STATE_INIT=0,
49   DH_STATE_INIT_SENT,
50   DH_STATE_NEWKEYS_SENT,
51   DH_STATE_FINISHED
52 };
53 
54 enum ssh_pending_call_e {
55 	SSH_PENDING_CALL_NONE = 0,
56 	SSH_PENDING_CALL_CONNECT,
57 	SSH_PENDING_CALL_AUTH_NONE,
58 	SSH_PENDING_CALL_AUTH_PASSWORD,
59 	SSH_PENDING_CALL_AUTH_OFFER_PUBKEY,
60 	SSH_PENDING_CALL_AUTH_PUBKEY,
61 	SSH_PENDING_CALL_AUTH_AGENT,
62 	SSH_PENDING_CALL_AUTH_KBDINT_INIT,
63 	SSH_PENDING_CALL_AUTH_KBDINT_SEND
64 };
65 
66 /* libssh calls may block an undefined amount of time */
67 #define SSH_SESSION_FLAG_BLOCKING 1
68 
69 /* codes to use with ssh_handle_packets*() */
70 #define SSH_TIMEOUT_INFINITE -1
71 #define SSH_TIMEOUT_USER -2
72 #define SSH_TIMEOUT_NONBLOCKING 0
73 
74 /* members that are common to ssh_session and ssh_bind */
75 struct ssh_common_struct {
76     struct error_struct error;
77     ssh_callbacks callbacks; /* Callbacks to user functions */
78     int log_verbosity; /* verbosity of the log functions */
79     int log_indent; /* indentation level in enter_function logs */
80 };
81 
82 struct ssh_session_struct {
83     struct ssh_common_struct common;
84     struct ssh_socket_struct *socket;
85     char *serverbanner;
86     char *clientbanner;
87     int protoversion;
88     int server;
89     int client;
90     int openssh;
91     uint32_t send_seq;
92     uint32_t recv_seq;
93 /* status flags */
94     int closed;
95     int closed_by_except;
96 
97     int connected;
98     /* !=0 when the user got a session handle */
99     int alive;
100     /* two previous are deprecated */
101     /* int auth_service_asked; */
102 
103     /* session flags (SSH_SESSION_FLAG_*) */
104     int flags;
105 
106     ssh_string banner; /* that's the issue banner from
107                        the server */
108     char *discon_msg; /* disconnect message from
109                          the remote host */
110     ssh_buffer in_buffer;
111     PACKET in_packet;
112     ssh_buffer out_buffer;
113 
114     /* the states are used by the nonblocking stuff to remember */
115     /* where it was before being interrupted */
116     enum ssh_pending_call_e pending_call_state;
117     enum ssh_session_state_e session_state;
118     int packet_state;
119     enum ssh_dh_state_e dh_handshake_state;
120     enum ssh_auth_service_state_e auth_service_state;
121     enum ssh_auth_state_e auth_state;
122     enum ssh_channel_request_state_e global_req_state;
123     struct ssh_agent_state_struct *agent_state;
124     struct ssh_auth_auto_state_struct *auth_auto_state;
125 
126     ssh_buffer in_hashbuf;
127     ssh_buffer out_hashbuf;
128     struct ssh_crypto_struct *current_crypto;
129     struct ssh_crypto_struct *next_crypto;  /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */
130 
131     struct ssh_list *channels; /* linked list of channels */
132     int maxchannel;
133     int exec_channel_opened; /* version 1 only. more
134                                 info in channels1.c */
135     ssh_agent agent; /* ssh agent */
136 
137 /* keyb interactive data */
138     struct ssh_kbdint_struct *kbdint;
139     int version; /* 1 or 2 */
140     /* server host keys */
141     struct {
142         ssh_key rsa_key;
143         ssh_key dsa_key;
144 
145         /* The type of host key wanted by client */
146         enum ssh_keytypes_e hostkey;
147     } srv;
148     /* auths accepted by server */
149     int auth_methods;
150     struct ssh_list *ssh_message_list; /* list of delayed SSH messages */
151     int (*ssh_message_callback)( struct ssh_session_struct *session, ssh_message msg, void *userdata);
152     void *ssh_message_callback_data;
153 
154     void (*ssh_connection_callback)( struct ssh_session_struct *session);
155     struct ssh_packet_callbacks_struct default_packet_callbacks;
156     struct ssh_list *packet_callbacks;
157     struct ssh_socket_callbacks_struct socket_callbacks;
158     ssh_poll_ctx default_poll_ctx;
159     /* options */
160 #ifdef WITH_PCAP
161     ssh_pcap_context pcap_ctx; /* pcap debugging context */
162 #endif
163     char *username;
164     char *host;
165     char *bindaddr; /* bind the client to an ip addr */
166     char *xbanner; /* TODO: looks like it is not needed */
167     struct ssh_list *identity;
168     char *sshdir;
169     char *knownhosts;
170     char *wanted_methods[10];
171     char compressionlevel;
172     unsigned long timeout; /* seconds */
173     unsigned long timeout_usec;
174     unsigned int port;
175     socket_t fd;
176     int ssh2;
177     int ssh1;
178     int StrictHostKeyChecking;
179     char *ProxyCommand;
180 };
181 
182 /** @internal
183  * @brief a termination function evaluates the status of an object
184  * @param user[in] object to evaluate
185  * @returns 1 if the polling routine should terminate, 0 instead
186  */
187 typedef int (*ssh_termination_function)(void *user);
188 int ssh_handle_packets(ssh_session session, int timeout);
189 int ssh_handle_packets_termination(ssh_session session, int timeout,
190     ssh_termination_function fct, void *user);
191 void ssh_socket_exception_callback(int code, int errno_code, void *user);
192 
193 #endif /* SESSION_H_ */
194