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