libssh  0.5.4
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 /* members that are common to ssh_session and ssh_bind */
65 struct ssh_common_struct {
66  struct error_struct error;
67  ssh_callbacks callbacks; /* Callbacks to user functions */
68  int log_verbosity; /* verbosity of the log functions */
69  int log_indent; /* indentation level in enter_function logs */
70 };
71 
72 struct ssh_session_struct {
73  struct ssh_common_struct common;
74  struct ssh_socket_struct *socket;
75  char *serverbanner;
76  char *clientbanner;
77  int protoversion;
78  int server;
79  int client;
80  int openssh;
81  uint32_t send_seq;
82  uint32_t recv_seq;
83 /* status flags */
84  int closed;
85  int closed_by_except;
86 
87  int connected;
88  /* !=0 when the user got a session handle */
89  int alive;
90  /* two previous are deprecated */
91  /* int auth_service_asked; */
92 
93  /* session flags (SSH_SESSION_FLAG_*) */
94  int flags;
95 
96  ssh_string banner; /* that's the issue banner from
97  the server */
98  char *discon_msg; /* disconnect message from
99  the remote host */
100  ssh_buffer in_buffer;
101  PACKET in_packet;
102  ssh_buffer out_buffer;
103 
104  /* the states are used by the nonblocking stuff to remember */
105  /* where it was before being interrupted */
106  enum ssh_pending_call_e pending_call_state;
107  enum ssh_session_state_e session_state;
108  int packet_state;
109  enum ssh_dh_state_e dh_handshake_state;
110  enum ssh_auth_service_state_e auth_service_state;
111  enum ssh_auth_state_e auth_state;
112  enum ssh_channel_request_state_e global_req_state;
113  ssh_string dh_server_signature; /* information used by dh_handshake. */
114  KEX server_kex;
115  KEX client_kex;
116  ssh_buffer in_hashbuf;
117  ssh_buffer out_hashbuf;
118  struct ssh_crypto_struct *current_crypto;
119  struct ssh_crypto_struct *next_crypto; /* next_crypto is going to be used after a SSH2_MSG_NEWKEYS */
120 
121  struct ssh_list *channels; /* linked list of channels */
122  int maxchannel;
123  int exec_channel_opened; /* version 1 only. more
124  info in channels1.c */
125  ssh_agent agent; /* ssh agent */
126 
127 /* keyb interactive data */
128  struct ssh_kbdint_struct *kbdint;
129  int version; /* 1 or 2 */
130  /* server host keys */
131  ssh_private_key rsa_key;
132  ssh_private_key dsa_key;
133  /* auths accepted by server */
134  int auth_methods;
135  int hostkeys; /* contains type of host key wanted by client, in server impl */
136  struct ssh_list *ssh_message_list; /* list of delayed SSH messages */
137  int (*ssh_message_callback)( struct ssh_session_struct *session, ssh_message msg, void *userdata);
138  void *ssh_message_callback_data;
139 
140  void (*ssh_connection_callback)( struct ssh_session_struct *session);
141  struct ssh_packet_callbacks_struct default_packet_callbacks;
142  struct ssh_list *packet_callbacks;
143  struct ssh_socket_callbacks_struct socket_callbacks;
144  ssh_poll_ctx default_poll_ctx;
145  /* options */
146 #ifdef WITH_PCAP
147  ssh_pcap_context pcap_ctx; /* pcap debugging context */
148 #endif
149  char *username;
150  char *host;
151  char *bindaddr; /* bind the client to an ip addr */
152  char *xbanner; /* TODO: looks like it is not needed */
153  struct ssh_list *identity;
154  char *sshdir;
155  char *knownhosts;
156  char *wanted_methods[10];
157  char compressionlevel;
158  unsigned long timeout; /* seconds */
159  unsigned long timeout_usec;
160  unsigned int port;
161  socket_t fd;
162  int ssh2;
163  int ssh1;
164  int StrictHostKeyChecking;
165  char *ProxyCommand;
166 };
167 
173 typedef int (*ssh_termination_function)(void *user);
174 int ssh_handle_packets(ssh_session session, int timeout);
175 int ssh_handle_packets_termination(ssh_session session, int timeout,
176  ssh_termination_function fct, void *user);
177 void ssh_socket_exception_callback(int code, int errno_code, void *user);
178 
179 #endif /* SESSION_H_ */