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