libssh 0.11.0
The SSH library
Loading...
Searching...
No Matches
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/callbacks.h"
27#include "libssh/kex.h"
28#include "libssh/packet.h"
29#include "libssh/pcap.h"
30#include "libssh/auth.h"
31#include "libssh/channels.h"
32#include "libssh/poll.h"
33#include "libssh/config.h"
34#include "libssh/misc.h"
35
36/* These are the different states a SSH session can be into its life */
37enum ssh_session_state_e {
38 SSH_SESSION_STATE_NONE=0,
39 SSH_SESSION_STATE_CONNECTING,
40 SSH_SESSION_STATE_SOCKET_CONNECTED,
41 SSH_SESSION_STATE_BANNER_RECEIVED,
42 SSH_SESSION_STATE_INITIAL_KEX,
43 SSH_SESSION_STATE_KEXINIT_RECEIVED,
44 SSH_SESSION_STATE_DH,
45 SSH_SESSION_STATE_AUTHENTICATING,
46 SSH_SESSION_STATE_AUTHENTICATED,
47 SSH_SESSION_STATE_ERROR,
48 SSH_SESSION_STATE_DISCONNECTED
49};
50
51enum ssh_dh_state_e {
52 DH_STATE_INIT=0,
53 DH_STATE_GROUP_SENT,
54 DH_STATE_REQUEST_SENT,
55 DH_STATE_INIT_SENT,
56 DH_STATE_NEWKEYS_SENT,
57 DH_STATE_FINISHED
58};
59
60enum ssh_pending_call_e {
61 SSH_PENDING_CALL_NONE = 0,
62 SSH_PENDING_CALL_CONNECT,
63 SSH_PENDING_CALL_AUTH_NONE,
64 SSH_PENDING_CALL_AUTH_PASSWORD,
65 SSH_PENDING_CALL_AUTH_OFFER_PUBKEY,
66 SSH_PENDING_CALL_AUTH_PUBKEY,
67 SSH_PENDING_CALL_AUTH_AGENT,
68 SSH_PENDING_CALL_AUTH_KBDINT_INIT,
69 SSH_PENDING_CALL_AUTH_KBDINT_SEND,
70 SSH_PENDING_CALL_AUTH_GSSAPI_MIC
71};
72
73/* libssh calls may block an undefined amount of time */
74#define SSH_SESSION_FLAG_BLOCKING 0x0001
75
76/* Client successfully authenticated */
77#define SSH_SESSION_FLAG_AUTHENTICATED 0x0002
78
79/* Do not accept new session channels (no-more-sessions@openssh.com) */
80#define SSH_SESSION_FLAG_NO_MORE_SESSIONS 0x0004
81
82/* The KEXINIT message can be sent first by either of the parties so this flag
83 * indicates that the message was already sent to make sure it is sent and avoid
84 * sending it twice during key exchange to simplify the state machine. */
85#define SSH_SESSION_FLAG_KEXINIT_SENT 0x0008
86
87/* The current SSH2 session implements the "strict KEX" feature and should behave
88 * differently on SSH2_MSG_NEWKEYS. */
89#define SSH_SESSION_FLAG_KEX_STRICT 0x0010
90/* Unexpected packets have been sent while the session was still unencrypted */
91#define SSH_SESSION_FLAG_KEX_TAINTED 0x0020
92/* The scp on server can not handle quoted paths. Skip the mitigation for
93 * CVE-2019-14889 when using scp */
94#define SSH_SESSION_FLAG_SCP_QUOTING_BROKEN 0x0040
95
96/* codes to use with ssh_handle_packets*() */
97/* Infinite timeout */
98#define SSH_TIMEOUT_INFINITE -1
99/* Use the timeout defined by user if any. Mostly used with new connections */
100#define SSH_TIMEOUT_USER -2
101/* Use the default timeout, depending on ssh_is_blocking() */
102#define SSH_TIMEOUT_DEFAULT -3
103/* Don't block at all */
104#define SSH_TIMEOUT_NONBLOCKING 0
105
106/* options flags */
107/* Authentication with *** allowed */
108#define SSH_OPT_FLAG_PASSWORD_AUTH 0x1
109#define SSH_OPT_FLAG_PUBKEY_AUTH 0x2
110#define SSH_OPT_FLAG_KBDINT_AUTH 0x4
111#define SSH_OPT_FLAG_GSSAPI_AUTH 0x8
112
113/* Escape expansion of different variables */
114#define SSH_OPT_EXP_FLAG_KNOWNHOSTS 0x1
115#define SSH_OPT_EXP_FLAG_GLOBAL_KNOWNHOSTS 0x2
116#define SSH_OPT_EXP_FLAG_PROXYCOMMAND 0x4
117#define SSH_OPT_EXP_FLAG_IDENTITY 0x8
118#define SSH_OPT_EXP_FLAG_CONTROL_PATH 0x10
119
120/* extensions flags */
121/* negotiation enabled */
122#define SSH_EXT_NEGOTIATION 0x01
123/* server-sig-algs extension */
124#define SSH_EXT_SIG_RSA_SHA256 0x02
125#define SSH_EXT_SIG_RSA_SHA512 0x04
126/* Host-bound public key authentication extension */
127#define SSH_EXT_PUBLICKEY_HOSTBOUND 0x08
128
129/* members that are common to ssh_session and ssh_bind */
130struct ssh_common_struct {
131 struct error_struct error;
132 ssh_callbacks callbacks; /* Callbacks to user functions */
133 int log_verbosity; /* verbosity of the log functions */
134};
135
136struct ssh_session_struct {
137 struct ssh_common_struct common;
138 struct ssh_socket_struct *socket;
139 char *serverbanner;
140 char *clientbanner;
141 int protoversion;
142 int server;
143 int client;
144 int openssh;
145 uint32_t send_seq;
146 uint32_t recv_seq;
147 struct ssh_timestamp last_rekey_time;
148 bool proxy_root;
149
150 int connected;
151 /* !=0 when the user got a session handle */
152 int alive;
153 /* two previous are deprecated */
154 /* int auth_service_asked; */
155
156 /* session flags (SSH_SESSION_FLAG_*) */
157 int flags;
158
159 /* Extensions negotiated using RFC 8308 */
160 uint32_t extensions;
161
162 ssh_string banner; /* that's the issue banner from the server */
163 char *peer_discon_msg; /* disconnect message from the remote host */
164 char *disconnect_message; /* disconnect message to be set */
165 ssh_buffer in_buffer;
166 PACKET in_packet;
167 ssh_buffer out_buffer;
168 struct ssh_list *out_queue; /* This list is used for delaying packets
169 when rekeying is required */
170
171 /* the states are used by the nonblocking stuff to remember */
172 /* where it was before being interrupted */
173 enum ssh_pending_call_e pending_call_state;
174 enum ssh_session_state_e session_state;
175 enum ssh_packet_state_e packet_state;
176 enum ssh_dh_state_e dh_handshake_state;
177 enum ssh_channel_request_state_e global_req_state;
178 struct ssh_agent_state_struct *agent_state;
179
180 struct {
181 struct ssh_auth_auto_state_struct *auto_state;
182 enum ssh_auth_service_state_e service_state;
183 enum ssh_auth_state_e state;
184 uint32_t supported_methods;
185 uint32_t current_method;
186 } auth;
187
188 /* Sending this flag before key exchange to save one round trip during the
189 * key exchange. This might make sense on high-latency connections.
190 * So far internal only for testing. Usable only on the client side --
191 * there is no key exchange method that would start with server message */
192 bool send_first_kex_follows;
193 /*
194 * RFC 4253, 7.1: if the first_kex_packet_follows flag was set in
195 * the received SSH_MSG_KEXINIT, but the guess was wrong, this
196 * field will be set such that the following guessed packet will
197 * be ignored on the receiving side. Once that packet has been received and
198 * ignored, this field is cleared.
199 * On the sending side, this is set after we got peer KEXINIT message and we
200 * need to resend the initial message of the negotiated KEX algorithm.
201 */
202 bool first_kex_follows_guess_wrong;
203
204 ssh_buffer in_hashbuf;
205 ssh_buffer out_hashbuf;
206 struct ssh_crypto_struct *current_crypto;
207 /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */
208 struct ssh_crypto_struct *next_crypto;
209
210 struct ssh_list *channels; /* linked list of channels */
211 uint32_t maxchannel;
212 ssh_agent agent; /* ssh agent */
213
214 /* keyboard interactive data */
215 struct ssh_kbdint_struct *kbdint;
216 struct ssh_gssapi_struct *gssapi;
217
218 /* server host keys */
219 struct {
220 ssh_key rsa_key;
221 ssh_key ecdsa_key;
222 ssh_key ed25519_key;
223 /* The type of host key wanted by client */
224 enum ssh_keytypes_e hostkey;
225 enum ssh_digest_e hostkey_digest;
226 } srv;
227
228 /* auths accepted by server */
229 struct ssh_list *ssh_message_list; /* list of delayed SSH messages */
230 int (*ssh_message_callback)(struct ssh_session_struct *session,
231 ssh_message msg, void *userdata);
232 void *ssh_message_callback_data;
233 ssh_server_callbacks server_callbacks;
234 void (*ssh_connection_callback)( struct ssh_session_struct *session);
235 struct ssh_packet_callbacks_struct default_packet_callbacks;
236 struct ssh_list *packet_callbacks;
237 struct ssh_socket_callbacks_struct socket_callbacks;
238 ssh_poll_ctx default_poll_ctx;
239 /* options */
240#ifdef WITH_PCAP
241 ssh_pcap_context pcap_ctx; /* pcap debugging context */
242#endif
243 struct {
244 struct ssh_list *identity;
245 struct ssh_list *identity_non_exp;
246 struct ssh_list *certificate;
247 struct ssh_list *certificate_non_exp;
248 struct ssh_list *proxy_jumps;
249 struct ssh_list *proxy_jumps_user_cb;
250 char *username;
251 char *host;
252 char *bindaddr; /* bind the client to an ip addr */
253 char *sshdir;
254 char *knownhosts;
255 char *global_knownhosts;
256 char *wanted_methods[SSH_KEX_METHODS];
257 char *pubkey_accepted_types;
258 char *ProxyCommand;
259 char *agent_socket;
260 unsigned long timeout; /* seconds */
261 unsigned long timeout_usec;
262 uint16_t port;
263 socket_t fd;
264 int StrictHostKeyChecking;
265 char compressionlevel;
266 char *gss_server_identity;
267 char *gss_client_identity;
268 int gss_delegate_creds;
269 int flags;
270 int exp_flags;
271 int nodelay;
272 bool config_processed;
273 uint8_t options_seen[SOC_MAX];
274 uint64_t rekey_data;
275 uint32_t rekey_time;
276 int rsa_min_size;
277 bool identities_only;
278 int control_master;
279 char *control_path;
280 } opts;
281
282 /* server options */
283 struct {
284 char *custombanner;
285 char *moduli_file;
286 } server_opts;
287
288 /* counters */
289 ssh_counter socket_counter;
290 ssh_counter raw_counter;
291
292 /* PKI context structure containing various parameters to configure PKI
293 * operations */
294 struct ssh_pki_ctx_struct *pki_context;
295};
296
302typedef int (*ssh_termination_function)(void *user);
303int ssh_handle_packets(ssh_session session, int timeout);
304int ssh_handle_packets_termination(ssh_session session,
305 int timeout,
306 ssh_termination_function fct,
307 void *user);
308void ssh_socket_exception_callback(int code, int errno_code, void *user);
309
310#endif /* SESSION_H_ */