libssh  0.8.7
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 "libssh/priv.h"
24 #include "libssh/kex.h"
25 #include "libssh/packet.h"
26 #include "libssh/pcap.h"
27 #include "libssh/auth.h"
28 #include "libssh/channels.h"
29 #include "libssh/poll.h"
30 #include <stdbool.h>
31 
32 /* These are the different states a SSH session can be into its life */
33 enum ssh_session_state_e {
34  SSH_SESSION_STATE_NONE=0,
35  SSH_SESSION_STATE_CONNECTING,
36  SSH_SESSION_STATE_SOCKET_CONNECTED,
37  SSH_SESSION_STATE_BANNER_RECEIVED,
38  SSH_SESSION_STATE_INITIAL_KEX,
39  SSH_SESSION_STATE_KEXINIT_RECEIVED,
40  SSH_SESSION_STATE_DH,
41  SSH_SESSION_STATE_AUTHENTICATING,
42  SSH_SESSION_STATE_AUTHENTICATED,
43  SSH_SESSION_STATE_ERROR,
44  SSH_SESSION_STATE_DISCONNECTED
45 };
46 
47 enum ssh_dh_state_e {
48  DH_STATE_INIT=0,
49  DH_STATE_INIT_SENT,
50  DH_STATE_NEWKEYS_SENT,
51  DH_STATE_FINISHED
52 };
53 
54 enum ssh_pending_call_e {
55  SSH_PENDING_CALL_NONE = 0,
56  SSH_PENDING_CALL_CONNECT,
57  SSH_PENDING_CALL_AUTH_NONE,
58  SSH_PENDING_CALL_AUTH_PASSWORD,
59  SSH_PENDING_CALL_AUTH_OFFER_PUBKEY,
60  SSH_PENDING_CALL_AUTH_PUBKEY,
61  SSH_PENDING_CALL_AUTH_AGENT,
62  SSH_PENDING_CALL_AUTH_KBDINT_INIT,
63  SSH_PENDING_CALL_AUTH_KBDINT_SEND,
64  SSH_PENDING_CALL_AUTH_GSSAPI_MIC
65 };
66 
67 /* libssh calls may block an undefined amount of time */
68 #define SSH_SESSION_FLAG_BLOCKING 1
69 
70 /* Client successfully authenticated */
71 #define SSH_SESSION_FLAG_AUTHENTICATED 2
72 
73 /* The KEXINIT message can be sent first by either of the parties so this flag
74  * indicates that the message was already sent to make sure it is sent and avoid
75  * sending it twice during key exchange to simplify the state machine. */
76 #define SSH_SESSION_FLAG_KEXINIT_SENT 4
77 
78 /* codes to use with ssh_handle_packets*() */
79 /* Infinite timeout */
80 #define SSH_TIMEOUT_INFINITE -1
81 /* Use the timeout defined by user if any. Mostly used with new connections */
82 #define SSH_TIMEOUT_USER -2
83 /* Use the default timeout, depending on ssh_is_blocking() */
84 #define SSH_TIMEOUT_DEFAULT -3
85 /* Don't block at all */
86 #define SSH_TIMEOUT_NONBLOCKING 0
87 
88 /* options flags */
89 /* Authentication with *** allowed */
90 #define SSH_OPT_FLAG_PASSWORD_AUTH 0x1
91 #define SSH_OPT_FLAG_PUBKEY_AUTH 0x2
92 #define SSH_OPT_FLAG_KBDINT_AUTH 0x4
93 #define SSH_OPT_FLAG_GSSAPI_AUTH 0x8
94 
95 /* extensions flags */
96 /* negotiation enabled */
97 #define SSH_EXT_NEGOTIATION 0x01
98 /* server-sig-algs extension */
99 #define SSH_EXT_SIG_RSA_SHA256 0x02
100 #define SSH_EXT_SIG_RSA_SHA512 0x04
101 
102 /* members that are common to ssh_session and ssh_bind */
104  struct error_struct error;
105  ssh_callbacks callbacks; /* Callbacks to user functions */
106  int log_verbosity; /* verbosity of the log functions */
107 };
108 
110  struct ssh_common_struct common;
111  struct ssh_socket_struct *socket;
112  char *serverbanner;
113  char *clientbanner;
114  int protoversion;
115  int server;
116  int client;
117  int openssh;
118  uint32_t send_seq;
119  uint32_t recv_seq;
120 
121  int connected;
122  /* !=0 when the user got a session handle */
123  int alive;
124  /* two previous are deprecated */
125  /* int auth_service_asked; */
126 
127  /* session flags (SSH_SESSION_FLAG_*) */
128  int flags;
129 
130  /* Extensions negotiated using RFC 8308 */
131  uint32_t extensions;
132 
133  ssh_string banner; /* that's the issue banner from
134  the server */
135  char *discon_msg; /* disconnect message from
136  the remote host */
137  ssh_buffer in_buffer;
138  PACKET in_packet;
139  ssh_buffer out_buffer;
140 
141  /* the states are used by the nonblocking stuff to remember */
142  /* where it was before being interrupted */
143  enum ssh_pending_call_e pending_call_state;
144  enum ssh_session_state_e session_state;
145  int packet_state;
146  enum ssh_dh_state_e dh_handshake_state;
147  enum ssh_channel_request_state_e global_req_state;
148  struct ssh_agent_state_struct *agent_state;
149 
150  struct {
151  struct ssh_auth_auto_state_struct *auto_state;
152  enum ssh_auth_service_state_e service_state;
153  enum ssh_auth_state_e state;
154  uint32_t supported_methods;
155  uint32_t current_method;
156  } auth;
157 
158  /* Sending this flag before key exchange to save one round trip during the
159  * key exchange. This might make sense on high-latency connections.
160  * So far internal only for testing. Usable only on the client side --
161  * there is no key exchange method that would start with server message */
162  bool send_first_kex_follows;
163  /*
164  * RFC 4253, 7.1: if the first_kex_packet_follows flag was set in
165  * the received SSH_MSG_KEXINIT, but the guess was wrong, this
166  * field will be set such that the following guessed packet will
167  * be ignored on the receiving side. Once that packet has been received and
168  * ignored, this field is cleared.
169  * On the sending side, this is set after we got peer KEXINIT message and we
170  * need to resend the initial message of the negotiated KEX algorithm.
171  */
172  bool first_kex_follows_guess_wrong;
173 
174  ssh_buffer in_hashbuf;
175  ssh_buffer out_hashbuf;
176  struct ssh_crypto_struct *current_crypto;
177  struct ssh_crypto_struct *next_crypto; /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */
178 
179  struct ssh_list *channels; /* linked list of channels */
180  int maxchannel;
181  ssh_agent agent; /* ssh agent */
182 
183 /* keyb interactive data */
184  struct ssh_kbdint_struct *kbdint;
185  struct ssh_gssapi_struct *gssapi;
186 
187  /* server host keys */
188  struct {
189  ssh_key rsa_key;
190  ssh_key dsa_key;
191  ssh_key ecdsa_key;
192  ssh_key ed25519_key;
193  /* The type of host key wanted by client */
194  enum ssh_keytypes_e hostkey;
195  } srv;
196 
197  /* auths accepted by server */
198  struct ssh_list *ssh_message_list; /* list of delayed SSH messages */
199  int (*ssh_message_callback)( struct ssh_session_struct *session, ssh_message msg, void *userdata);
200  void *ssh_message_callback_data;
201  ssh_server_callbacks server_callbacks;
202  void (*ssh_connection_callback)( struct ssh_session_struct *session);
203  struct ssh_packet_callbacks_struct default_packet_callbacks;
204  struct ssh_list *packet_callbacks;
205  struct ssh_socket_callbacks_struct socket_callbacks;
206  ssh_poll_ctx default_poll_ctx;
207  /* options */
208 #ifdef WITH_PCAP
209  ssh_pcap_context pcap_ctx; /* pcap debugging context */
210 #endif
211  struct {
212  struct ssh_list *identity;
213  char *username;
214  char *host;
215  char *bindaddr; /* bind the client to an ip addr */
216  char *sshdir;
217  char *knownhosts;
218  char *global_knownhosts;
219  char *wanted_methods[10];
220  char *pubkey_accepted_types;
221  char *ProxyCommand;
222  char *custombanner;
223  unsigned long timeout; /* seconds */
224  unsigned long timeout_usec;
225  unsigned int port;
226  socket_t fd;
227  int StrictHostKeyChecking;
228  char compressionlevel;
229  char *gss_server_identity;
230  char *gss_client_identity;
231  int gss_delegate_creds;
232  int flags;
233  int nodelay;
234  } opts;
235  /* counters */
236  ssh_counter socket_counter;
237  ssh_counter raw_counter;
238 };
239 
245 typedef int (*ssh_termination_function)(void *user);
246 int ssh_handle_packets(ssh_session session, int timeout);
247 int ssh_handle_packets_termination(ssh_session session, int timeout,
248  ssh_termination_function fct, void *user);
249 void ssh_socket_exception_callback(int code, int errno_code, void *user);
250 
251 #endif /* SESSION_H_ */
Definition: pki.h:42
Definition: callbacks.h:378
Definition: auth.h:37
Definition: crypto.h:84
Definition: agent.h:73
Definition: callbacks.h:142
Definition: messages.h:84
Definition: poll.c:76
Definition: callbacks.h:530
Definition: session.h:109
Definition: gssapi.c:48
Definition: packet.h:29
Definition: auth.c:980
Definition: misc.h:38
Definition: callbacks.h:304
Definition: string.h:29
Definition: auth.c:842
Definition: priv.h:227
Definition: socket.c:76
Definition: session.h:103
Definition: buffer.c:47
Definition: libssh.h:108