libssh  0.9.3
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
146  the server */
147  char *discon_msg; /* disconnect message from
148  the remote host */
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  struct ssh_crypto_struct *next_crypto; /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */
192 
193  struct ssh_list *channels; /* linked list of channels */
194  int maxchannel;
195  ssh_agent agent; /* ssh agent */
196 
197 /* keyb interactive data */
198  struct ssh_kbdint_struct *kbdint;
199  struct ssh_gssapi_struct *gssapi;
200 
201  /* server host keys */
202  struct {
203  ssh_key rsa_key;
204  ssh_key dsa_key;
205  ssh_key ecdsa_key;
206  ssh_key ed25519_key;
207  /* The type of host key wanted by client */
208  enum ssh_keytypes_e hostkey;
209  enum ssh_digest_e hostkey_digest;
210  } srv;
211 
212  /* auths accepted by server */
213  struct ssh_list *ssh_message_list; /* list of delayed SSH messages */
214  int (*ssh_message_callback)( struct ssh_session_struct *session, 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  unsigned int 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_ */
ssh_socket_callbacks_struct
Definition: callbacks.h:378
ssh_key_struct
Definition: pki.h:50
ssh_callbacks_struct
Definition: callbacks.h:142
ssh_poll_ctx_struct
Definition: poll.c:76
ssh_auth_auto_state_struct
Definition: auth.c:971
ssh_counter_struct
Definition: libssh.h:108
ssh_gssapi_struct
Definition: gssapi.c:48
ssh_kbdint_struct
Definition: auth.h:37
ssh_message_struct
Definition: messages.h:84
packet_struct
Definition: packet.h:29
ssh_crypto_struct
Definition: crypto.h:104
ssh_buffer_struct
Definition: buffer.c:47
ssh_socket_struct
Definition: socket.c:76
ssh_common_struct
Definition: session.h:114
ssh_agent_struct
Definition: agent.h:73
ssh_session_struct
Definition: session.h:120
ssh_agent_state_struct
Definition: auth.c:833
ssh_timestamp
Definition: misc.h:49
ssh_string_struct
Definition: string.h:29
ssh_packet_callbacks_struct
Definition: callbacks.h:530
ssh_server_callbacks_struct
Definition: callbacks.h:304
error_struct
Definition: priv.h:253
ssh_list
Definition: misc.h:39