libssh  0.9.8
The SSH library
session.h
1 /*
2  * This file is part of the SSH Library
3  *
4  * Copyright (c) 2009 by Aris Adamantiadis
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef SESSION_H_
22 #define SESSION_H_
23 #include <stdbool.h>
24 
25 #include "libssh/priv.h"
26 #include "libssh/kex.h"
27 #include "libssh/packet.h"
28 #include "libssh/pcap.h"
29 #include "libssh/auth.h"
30 #include "libssh/channels.h"
31 #include "libssh/poll.h"
32 #include "libssh/config.h"
33 #include "libssh/misc.h"
34 
35 /* These are the different states a SSH session can be into its life */
36 enum ssh_session_state_e {
37  SSH_SESSION_STATE_NONE=0,
38  SSH_SESSION_STATE_CONNECTING,
39  SSH_SESSION_STATE_SOCKET_CONNECTED,
40  SSH_SESSION_STATE_BANNER_RECEIVED,
41  SSH_SESSION_STATE_INITIAL_KEX,
42  SSH_SESSION_STATE_KEXINIT_RECEIVED,
43  SSH_SESSION_STATE_DH,
44  SSH_SESSION_STATE_AUTHENTICATING,
45  SSH_SESSION_STATE_AUTHENTICATED,
46  SSH_SESSION_STATE_ERROR,
47  SSH_SESSION_STATE_DISCONNECTED
48 };
49 
50 enum ssh_dh_state_e {
51  DH_STATE_INIT=0,
52  DH_STATE_GROUP_SENT,
53  DH_STATE_REQUEST_SENT,
54  DH_STATE_INIT_SENT,
55  DH_STATE_NEWKEYS_SENT,
56  DH_STATE_FINISHED
57 };
58 
59 enum ssh_pending_call_e {
60  SSH_PENDING_CALL_NONE = 0,
61  SSH_PENDING_CALL_CONNECT,
62  SSH_PENDING_CALL_AUTH_NONE,
63  SSH_PENDING_CALL_AUTH_PASSWORD,
64  SSH_PENDING_CALL_AUTH_OFFER_PUBKEY,
65  SSH_PENDING_CALL_AUTH_PUBKEY,
66  SSH_PENDING_CALL_AUTH_AGENT,
67  SSH_PENDING_CALL_AUTH_KBDINT_INIT,
68  SSH_PENDING_CALL_AUTH_KBDINT_SEND,
69  SSH_PENDING_CALL_AUTH_GSSAPI_MIC
70 };
71 
72 /* libssh calls may block an undefined amount of time */
73 #define SSH_SESSION_FLAG_BLOCKING 1
74 
75 /* Client successfully authenticated */
76 #define SSH_SESSION_FLAG_AUTHENTICATED 2
77 
78 /* The KEXINIT message can be sent first by either of the parties so this flag
79  * indicates that the message was already sent to make sure it is sent and avoid
80  * sending it twice during key exchange to simplify the state machine. */
81 #define SSH_SESSION_FLAG_KEXINIT_SENT 4
82 
83 /* The current SSH2 session implements the "strict KEX" feature and should behave
84  * differently on SSH2_MSG_NEWKEYS. */
85 #define SSH_SESSION_FLAG_KEX_STRICT 0x0010
86 /* Unexpected packets have been sent while the session was still unencrypted */
87 #define SSH_SESSION_FLAG_KEX_TAINTED 0x0020
88 
89 /* codes to use with ssh_handle_packets*() */
90 /* Infinite timeout */
91 #define SSH_TIMEOUT_INFINITE -1
92 /* Use the timeout defined by user if any. Mostly used with new connections */
93 #define SSH_TIMEOUT_USER -2
94 /* Use the default timeout, depending on ssh_is_blocking() */
95 #define SSH_TIMEOUT_DEFAULT -3
96 /* Don't block at all */
97 #define SSH_TIMEOUT_NONBLOCKING 0
98 
99 /* options flags */
100 /* Authentication with *** allowed */
101 #define SSH_OPT_FLAG_PASSWORD_AUTH 0x1
102 #define SSH_OPT_FLAG_PUBKEY_AUTH 0x2
103 #define SSH_OPT_FLAG_KBDINT_AUTH 0x4
104 #define SSH_OPT_FLAG_GSSAPI_AUTH 0x8
105 
106 /* extensions flags */
107 /* negotiation enabled */
108 #define SSH_EXT_NEGOTIATION 0x01
109 /* server-sig-algs extension */
110 #define SSH_EXT_SIG_RSA_SHA256 0x02
111 #define SSH_EXT_SIG_RSA_SHA512 0x04
112 
113 /* members that are common to ssh_session and ssh_bind */
115  struct error_struct error;
116  ssh_callbacks callbacks; /* Callbacks to user functions */
117  int log_verbosity; /* verbosity of the log functions */
118 };
119 
121  struct ssh_common_struct common;
122  struct ssh_socket_struct *socket;
123  char *serverbanner;
124  char *clientbanner;
125  int protoversion;
126  int server;
127  int client;
128  int openssh;
129  uint32_t send_seq;
130  uint32_t recv_seq;
131  struct ssh_timestamp last_rekey_time;
132 
133  int connected;
134  /* !=0 when the user got a session handle */
135  int alive;
136  /* two previous are deprecated */
137  /* int auth_service_asked; */
138 
139  /* session flags (SSH_SESSION_FLAG_*) */
140  int flags;
141 
142  /* Extensions negotiated using RFC 8308 */
143  uint32_t extensions;
144 
145  ssh_string banner; /* that's the issue banner from the server */
146  char *discon_msg; /* disconnect message from the remote host */
147  ssh_buffer in_buffer;
148  PACKET in_packet;
149  ssh_buffer out_buffer;
150  struct ssh_list *out_queue; /* This list is used for delaying packets
151  when rekeying is required */
152 
153  /* the states are used by the nonblocking stuff to remember */
154  /* where it was before being interrupted */
155  enum ssh_pending_call_e pending_call_state;
156  enum ssh_session_state_e session_state;
157  enum ssh_packet_state_e packet_state;
158  enum ssh_dh_state_e dh_handshake_state;
159  enum ssh_channel_request_state_e global_req_state;
160  struct ssh_agent_state_struct *agent_state;
161 
162  struct {
163  struct ssh_auth_auto_state_struct *auto_state;
164  enum ssh_auth_service_state_e service_state;
165  enum ssh_auth_state_e state;
166  uint32_t supported_methods;
167  uint32_t current_method;
168  } auth;
169 
170  /* Sending this flag before key exchange to save one round trip during the
171  * key exchange. This might make sense on high-latency connections.
172  * So far internal only for testing. Usable only on the client side --
173  * there is no key exchange method that would start with server message */
174  bool send_first_kex_follows;
175  /*
176  * RFC 4253, 7.1: if the first_kex_packet_follows flag was set in
177  * the received SSH_MSG_KEXINIT, but the guess was wrong, this
178  * field will be set such that the following guessed packet will
179  * be ignored on the receiving side. Once that packet has been received and
180  * ignored, this field is cleared.
181  * On the sending side, this is set after we got peer KEXINIT message and we
182  * need to resend the initial message of the negotiated KEX algorithm.
183  */
184  bool first_kex_follows_guess_wrong;
185 
186  ssh_buffer in_hashbuf;
187  ssh_buffer out_hashbuf;
188  struct ssh_crypto_struct *current_crypto;
189  /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */
190  struct ssh_crypto_struct *next_crypto;
191 
192  struct ssh_list *channels; /* linked list of channels */
193  int maxchannel;
194  ssh_agent agent; /* ssh agent */
195 
196  /* keyboard interactive data */
197  struct ssh_kbdint_struct *kbdint;
198  struct ssh_gssapi_struct *gssapi;
199 
200  /* server host keys */
201  struct {
202  ssh_key rsa_key;
203  ssh_key dsa_key;
204  ssh_key ecdsa_key;
205  ssh_key ed25519_key;
206  /* The type of host key wanted by client */
207  enum ssh_keytypes_e hostkey;
208  enum ssh_digest_e hostkey_digest;
209  } srv;
210 
211  /* auths accepted by server */
212  struct ssh_list *ssh_message_list; /* list of delayed SSH messages */
213  int (*ssh_message_callback)(struct ssh_session_struct *session,
214  ssh_message msg, void *userdata);
215  void *ssh_message_callback_data;
216  ssh_server_callbacks server_callbacks;
217  void (*ssh_connection_callback)( struct ssh_session_struct *session);
218  struct ssh_packet_callbacks_struct default_packet_callbacks;
219  struct ssh_list *packet_callbacks;
220  struct ssh_socket_callbacks_struct socket_callbacks;
221  ssh_poll_ctx default_poll_ctx;
222  /* options */
223 #ifdef WITH_PCAP
224  ssh_pcap_context pcap_ctx; /* pcap debugging context */
225 #endif
226  struct {
227  struct ssh_list *identity;
228  char *username;
229  char *host;
230  char *bindaddr; /* bind the client to an ip addr */
231  char *sshdir;
232  char *knownhosts;
233  char *global_knownhosts;
234  char *wanted_methods[SSH_KEX_METHODS];
235  char *pubkey_accepted_types;
236  char *ProxyCommand;
237  char *custombanner;
238  unsigned long timeout; /* seconds */
239  unsigned long timeout_usec;
240  uint16_t port;
241  socket_t fd;
242  int StrictHostKeyChecking;
243  char compressionlevel;
244  char *gss_server_identity;
245  char *gss_client_identity;
246  int gss_delegate_creds;
247  int flags;
248  int nodelay;
249  bool config_processed;
250  uint8_t options_seen[SOC_MAX];
251  uint64_t rekey_data;
252  uint32_t rekey_time;
253  } opts;
254  /* counters */
255  ssh_counter socket_counter;
256  ssh_counter raw_counter;
257 };
258 
264 typedef int (*ssh_termination_function)(void *user);
265 int ssh_handle_packets(ssh_session session, int timeout);
266 int ssh_handle_packets_termination(ssh_session session,
267  long timeout,
268  ssh_termination_function fct,
269  void *user);
270 void ssh_socket_exception_callback(int code, int errno_code, void *user);
271 
272 #endif /* SESSION_H_ */
Definition: priv.h:254
Definition: packet.h:29
Definition: auth.c:833
Definition: agent.h:73
Definition: auth.c:971
Definition: buffer.c:47
Definition: callbacks.h:142
Definition: session.h:114
Definition: libssh.h:93
Definition: crypto.h:106
Definition: gssapi.c:48
Definition: auth.h:37
Definition: pki.h:50
Definition: misc.h:39
Definition: messages.h:84
Definition: callbacks.h:530
Definition: poll.c:76
Definition: callbacks.h:304
Definition: session.h:120
Definition: callbacks.h:378
Definition: socket.c:78
Definition: string.h:29
Definition: misc.h:49