libssh 0.11.1 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 */
37 enum 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
51 enum 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
60 enum 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
93/* codes to use with ssh_handle_packets*() */
94/* Infinite timeout */
95#define SSH_TIMEOUT_INFINITE -1
96/* Use the timeout defined by user if any. Mostly used with new connections */
97#define SSH_TIMEOUT_USER -2
98/* Use the default timeout, depending on ssh_is_blocking() */
99#define SSH_TIMEOUT_DEFAULT -3
100/* Don't block at all */
101#define SSH_TIMEOUT_NONBLOCKING 0
102
103/* options flags */
104/* Authentication with *** allowed */
105#define SSH_OPT_FLAG_PASSWORD_AUTH 0x1
106#define SSH_OPT_FLAG_PUBKEY_AUTH 0x2
107#define SSH_OPT_FLAG_KBDINT_AUTH 0x4
108#define SSH_OPT_FLAG_GSSAPI_AUTH 0x8
109
110/* Escape expansion of different variables */
111#define SSH_OPT_EXP_FLAG_KNOWNHOSTS 0x1
112#define SSH_OPT_EXP_FLAG_GLOBAL_KNOWNHOSTS 0x2
113#define SSH_OPT_EXP_FLAG_PROXYCOMMAND 0x4
114#define SSH_OPT_EXP_FLAG_IDENTITY 0x8
115#define SSH_OPT_EXP_FLAG_CONTROL_PATH 0x10
116
117/* extensions flags */
118/* negotiation enabled */
119#define SSH_EXT_NEGOTIATION 0x01
120/* server-sig-algs extension */
121#define SSH_EXT_SIG_RSA_SHA256 0x02
122#define SSH_EXT_SIG_RSA_SHA512 0x04
123
124/* members that are common to ssh_session and ssh_bind */
125 struct ssh_common_struct {
126 struct error_struct error;
127 ssh_callbacks callbacks; /* Callbacks to user functions */
128 int log_verbosity; /* verbosity of the log functions */
129};
130
131 struct ssh_session_struct {
132 struct ssh_common_struct common;
133 struct ssh_socket_struct *socket;
134 char *serverbanner;
135 char *clientbanner;
136 int protoversion;
137 int server;
138 int client;
139 int openssh;
140 uint32_t send_seq;
141 uint32_t recv_seq;
142 struct ssh_timestamp last_rekey_time;
143 bool proxy_root;
144
145 int connected;
146 /* !=0 when the user got a session handle */
147 int alive;
148 /* two previous are deprecated */
149 /* int auth_service_asked; */
150
151 /* session flags (SSH_SESSION_FLAG_*) */
152 int flags;
153
154 /* Extensions negotiated using RFC 8308 */
155 uint32_t extensions;
156
157 ssh_string banner; /* that's the issue banner from the server */
158 char *peer_discon_msg; /* disconnect message from the remote host */
159 char *disconnect_message; /* disconnect message to be set */
160 ssh_buffer in_buffer;
161 PACKET in_packet;
162 ssh_buffer out_buffer;
163 struct ssh_list *out_queue; /* This list is used for delaying packets
164 when rekeying is required */
165
166 /* the states are used by the nonblocking stuff to remember */
167 /* where it was before being interrupted */
168 enum ssh_pending_call_e pending_call_state;
169 enum ssh_session_state_e session_state;
170 enum ssh_packet_state_e packet_state;
171 enum ssh_dh_state_e dh_handshake_state;
172 enum ssh_channel_request_state_e global_req_state;
173 struct ssh_agent_state_struct *agent_state;
174
175 struct {
176 struct ssh_auth_auto_state_struct *auto_state;
177 enum ssh_auth_service_state_e service_state;
178 enum ssh_auth_state_e state;
179 uint32_t supported_methods;
180 uint32_t current_method;
181 } auth;
182
183 /* Sending this flag before key exchange to save one round trip during the
184 * key exchange. This might make sense on high-latency connections.
185 * So far internal only for testing. Usable only on the client side --
186 * there is no key exchange method that would start with server message */
187 bool send_first_kex_follows;
188 /*
189 * RFC 4253, 7.1: if the first_kex_packet_follows flag was set in
190 * the received SSH_MSG_KEXINIT, but the guess was wrong, this
191 * field will be set such that the following guessed packet will
192 * be ignored on the receiving side. Once that packet has been received and
193 * ignored, this field is cleared.
194 * On the sending side, this is set after we got peer KEXINIT message and we
195 * need to resend the initial message of the negotiated KEX algorithm.
196 */
197 bool first_kex_follows_guess_wrong;
198
199 ssh_buffer in_hashbuf;
200 ssh_buffer out_hashbuf;
201 struct ssh_crypto_struct *current_crypto;
202 /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */
203 struct ssh_crypto_struct *next_crypto;
204
205 struct ssh_list *channels; /* linked list of channels */
206 uint32_t maxchannel;
207 ssh_agent agent; /* ssh agent */
208
209 /* keyboard interactive data */
210 struct ssh_kbdint_struct *kbdint;
211 struct ssh_gssapi_struct *gssapi;
212
213 /* server host keys */
214 struct {
215 ssh_key rsa_key;
216 ssh_key ecdsa_key;
217 ssh_key ed25519_key;
218 /* The type of host key wanted by client */
219 enum ssh_keytypes_e hostkey;
220 enum ssh_digest_e hostkey_digest;
221 } srv;
222
223 /* auths accepted by server */
224 struct ssh_list *ssh_message_list; /* list of delayed SSH messages */
225 int (*ssh_message_callback)(struct ssh_session_struct *session,
226 ssh_message msg, void *userdata);
227 void *ssh_message_callback_data;
228 ssh_server_callbacks server_callbacks;
229 void (*ssh_connection_callback)( struct ssh_session_struct *session);
230 struct ssh_packet_callbacks_struct default_packet_callbacks;
231 struct ssh_list *packet_callbacks;
232 struct ssh_socket_callbacks_struct socket_callbacks;
233 ssh_poll_ctx default_poll_ctx;
234 /* options */
235#ifdef WITH_PCAP
236 ssh_pcap_context pcap_ctx; /* pcap debugging context */
237#endif
238 struct {
239 struct ssh_list *identity;
240 struct ssh_list *identity_non_exp;
241 struct ssh_list *certificate;
242 struct ssh_list *certificate_non_exp;
243 struct ssh_list *proxy_jumps;
244 struct ssh_list *proxy_jumps_user_cb;
245 char *username;
246 char *host;
247 char *bindaddr; /* bind the client to an ip addr */
248 char *sshdir;
249 char *knownhosts;
250 char *global_knownhosts;
251 char *wanted_methods[SSH_KEX_METHODS];
252 char *pubkey_accepted_types;
253 char *ProxyCommand;
254 char *agent_socket;
255 unsigned long timeout; /* seconds */
256 unsigned long timeout_usec;
257 uint16_t port;
258 socket_t fd;
259 int StrictHostKeyChecking;
260 char compressionlevel;
261 char *gss_server_identity;
262 char *gss_client_identity;
263 int gss_delegate_creds;
264 int flags;
265 int exp_flags;
266 int nodelay;
267 bool config_processed;
268 uint8_t options_seen[SOC_MAX];
269 uint64_t rekey_data;
270 uint32_t rekey_time;
271 int rsa_min_size;
272 bool identities_only;
273 int control_master;
274 char *control_path;
275 } opts;
276
277 /* server options */
278 struct {
279 char *custombanner;
280 char *moduli_file;
281 } server_opts;
282
283 /* counters */
284 ssh_counter socket_counter;
285 ssh_counter raw_counter;
286};
287
293 typedef int (*ssh_termination_function)(void *user);
294 int ssh_handle_packets(ssh_session session, int timeout);
295 int ssh_handle_packets_termination(ssh_session session,
296 int timeout,
297 ssh_termination_function fct,
298 void *user);
299 void ssh_socket_exception_callback(int code, int errno_code, void *user);
300
301#endif /* SESSION_H_ */
ssh_callbacks_struct
Definition callbacks.h:165
ssh_server_callbacks_struct
Definition callbacks.h:333
ssh_socket_callbacks_struct
Definition callbacks.h:411