libssh  0.5.2
session.h
1 /*
2  * This file is part of the SSH Library
3  *
4  * Copyright (c) 2009 by Aris Adamantiadis
5  *
6  * The SSH Library is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or (at your
9  * option) any later version.
10  *
11  * The SSH Library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with the SSH Library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19  * MA 02111-1307, USA.
20  */
21 
22 #ifndef SESSION_H_
23 #define SESSION_H_
24 #include "libssh/priv.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 typedef struct ssh_kbdint_struct* ssh_kbdint;
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 };
60 
61 /* libssh calls may block an undefined amount of time */
62 #define SSH_SESSION_FLAG_BLOCKING 1
63 
64 struct ssh_session_struct {
65  struct error_struct error;
66  struct ssh_socket_struct *socket;
67  char *serverbanner;
68  char *clientbanner;
69  int protoversion;
70  int server;
71  int client;
72  int openssh;
73  uint32_t send_seq;
74  uint32_t recv_seq;
75 /* status flags */
76  int closed;
77  int closed_by_except;
78 
79  int connected;
80  /* !=0 when the user got a session handle */
81  int alive;
82  /* two previous are deprecated */
83  /* int auth_service_asked; */
84 
85  /* session flags (SSH_SESSION_FLAG_*) */
86  int flags;
87 
88  ssh_string banner; /* that's the issue banner from
89  the server */
90  char *discon_msg; /* disconnect message from
91  the remote host */
92  ssh_buffer in_buffer;
93  PACKET in_packet;
94  ssh_buffer out_buffer;
95 
96  /* the states are used by the nonblocking stuff to remember */
97  /* where it was before being interrupted */
98  enum ssh_pending_call_e pending_call_state;
99  enum ssh_session_state_e session_state;
100  int packet_state;
101  enum ssh_dh_state_e dh_handshake_state;
102  enum ssh_auth_service_state_e auth_service_state;
103  enum ssh_auth_state_e auth_state;
104  enum ssh_channel_request_state_e global_req_state;
105  ssh_string dh_server_signature; /* information used by dh_handshake. */
106  KEX server_kex;
107  KEX client_kex;
108  ssh_buffer in_hashbuf;
109  ssh_buffer out_hashbuf;
110  struct ssh_crypto_struct *current_crypto;
111  struct ssh_crypto_struct *next_crypto; /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */
112 
113  struct ssh_list *channels; /* linked list of channels */
114  int maxchannel;
115  int exec_channel_opened; /* version 1 only. more
116  info in channels1.c */
117  ssh_agent agent; /* ssh agent */
118 
119 /* keyb interactive data */
120  struct ssh_kbdint_struct *kbdint;
121  int version; /* 1 or 2 */
122  /* server host keys */
123  ssh_private_key rsa_key;
124  ssh_private_key dsa_key;
125  /* auths accepted by server */
126  int auth_methods;
127  int hostkeys; /* contains type of host key wanted by client, in server impl */
128  struct ssh_list *ssh_message_list; /* list of delayed SSH messages */
129  int (*ssh_message_callback)( struct ssh_session_struct *session, ssh_message msg, void *userdata);
130  void *ssh_message_callback_data;
131  int log_verbosity; /*cached copy of the option structure */
132  int log_indent; /* indentation level in enter_function logs */
133 
134  void (*ssh_connection_callback)( struct ssh_session_struct *session);
135  ssh_callbacks callbacks; /* Callbacks to user functions */
136  struct ssh_packet_callbacks_struct default_packet_callbacks;
137  struct ssh_list *packet_callbacks;
138  struct ssh_socket_callbacks_struct socket_callbacks;
139  ssh_poll_ctx default_poll_ctx;
140  /* options */
141 #ifdef WITH_PCAP
142  ssh_pcap_context pcap_ctx; /* pcap debugging context */
143 #endif
144  char *username;
145  char *host;
146  char *bindaddr; /* bind the client to an ip addr */
147  char *xbanner; /* TODO: looks like it is not needed */
148  struct ssh_list *identity;
149  char *sshdir;
150  char *knownhosts;
151  char *wanted_methods[10];
152  char compressionlevel;
153  unsigned long timeout; /* seconds */
154  unsigned long timeout_usec;
155  unsigned int port;
156  socket_t fd;
157  int ssh2;
158  int ssh1;
159  int StrictHostKeyChecking;
160  char *ProxyCommand;
161 };
162 
168 typedef int (*ssh_termination_function)(void *user);
169 int ssh_handle_packets(ssh_session session, int timeout);
170 int ssh_handle_packets_termination(ssh_session session, int timeout,
171  ssh_termination_function fct, void *user);
172 void ssh_socket_exception_callback(int code, int errno_code, void *user);
173 
174 #endif /* SESSION_H_ */