21
21
* MA 02111-1307, USA.
24
25
#include <string.h>
25
26
#include <stdlib.h>
26
27
#include "libssh/libssh.h"
27
28
#include "libssh/priv.h"
28
29
#include "libssh/server.h"
30
#include "libssh/socket.h"
31
#include "libssh/agent.h"
32
#include "libssh/packet.h"
33
#include "libssh/session.h"
34
#include "libssh/misc.h"
29
36
#define FIRST_CHANNEL 42 // why not ? it helps to find bugs.
31
38
/** \defgroup ssh_session SSH Session
37
44
/** \brief creates a new ssh session
38
45
* \returns new ssh_session pointer
40
SSH_SESSION *ssh_new(void) {
47
ssh_session ssh_new(void) {
43
session = malloc(sizeof (SSH_SESSION));
50
session = malloc(sizeof (struct ssh_session_struct));
44
51
if (session == NULL) {
48
memset(session, 0, sizeof(SSH_SESSION));
54
ZERO_STRUCTP(session);
50
56
session->next_crypto = crypto_new();
51
57
if (session->next_crypto == NULL) {
55
session->maxchannel = FIRST_CHANNEL;
56
61
session->socket = ssh_socket_new(session);
57
62
if (session->socket == NULL) {
62
session->auth_methods = 0;
63
session->blocking = 1;
64
session->log_indent = 0;
66
66
session->out_buffer = buffer_new();
67
67
if (session->out_buffer == NULL) {
97
118
SAFE_FREE(session->serverbanner);
98
119
SAFE_FREE(session->clientbanner);
99
120
SAFE_FREE(session->banner);
122
if(session->pcap_ctx){
123
ssh_pcap_context_free(session->pcap_ctx);
124
session->pcap_ctx=NULL;
100
127
buffer_free(session->in_buffer);
101
128
buffer_free(session->out_buffer);
102
129
session->in_buffer=session->out_buffer=NULL;
127
154
privatekey_free(session->dsa_key);
128
155
privatekey_free(session->rsa_key);
129
ssh_message_free(session->ssh_message);
130
ssh_options_free(session->options);
156
if(session->ssh_message_list){
158
while((msg=ssh_list_get_head(ssh_message ,session->ssh_message_list))
160
ssh_message_free(msg);
162
ssh_list_free(session->ssh_message_list);
166
SAFE_FREE(session->username);
167
SAFE_FREE(session->host);
168
SAFE_FREE(session->identity);
169
SAFE_FREE(session->sshdir);
170
SAFE_FREE(session->knownhosts);
172
for (i = 0; i < 10; i++) {
173
if (session->wanted_methods[i]) {
174
SAFE_FREE(session->wanted_methods[i]);
132
178
/* burn connection, it could hang sensitive datas */
133
memset(session,'X',sizeof(SSH_SESSION));
179
ZERO_STRUCTP(session);
135
180
SAFE_FREE(session);
136
/* FIXME: leave_function(); ??? */
139
/** \brief disconnect impolitely from remote host
183
/** \brief disconnect impolitely from remote host by closing the socket.
184
* Suitable if you forked and want to destroy this session.
140
185
* \param session current ssh session
142
void ssh_silent_disconnect(SSH_SESSION *session) {
187
void ssh_silent_disconnect(ssh_session session) {
143
188
enter_function();
145
190
if (session == NULL) {
149
194
ssh_socket_close(session->socket);
150
195
session->alive = 0;
151
196
ssh_disconnect(session);
152
/* FIXME: leave_function(); ??? */
155
/** \brief set the options for the current session
156
* \param session ssh session
157
* \param options options structure
159
* \see ssh_options_new()
161
void ssh_set_options(SSH_SESSION *session, SSH_OPTIONS *options) {
162
if (session == NULL || options == NULL) {
166
session->options = options;
167
session->log_verbosity = options->log_verbosity;
170
200
/** \brief set the session in blocking/nonblocking mode
172
202
* \param blocking zero for nonblocking mode
173
203
* \bug nonblocking code is in development and won't work as expected
175
void ssh_set_blocking(SSH_SESSION *session, int blocking) {
205
void ssh_set_blocking(ssh_session session, int blocking) {
176
206
if (session == NULL) {
198
228
/** \brief say to the session it has data to read on the file descriptor without blocking
199
229
* \param session ssh session
201
void ssh_set_fd_toread(SSH_SESSION *session) {
231
void ssh_set_fd_toread(ssh_session session) {
202
232
if (session == NULL) {
209
239
/** \brief say the session it may write to the file descriptor without blocking
210
240
* \param session ssh session
212
void ssh_set_fd_towrite(SSH_SESSION *session) {
242
void ssh_set_fd_towrite(ssh_session session) {
213
243
if (session == NULL) {
231
261
/** \warning I don't remember if this should be internal or not
233
263
/* looks if there is data to read on the socket and parse it. */
234
int ssh_handle_packets(SSH_SESSION *session) {
264
int ssh_handle_packets(ssh_session session) {
269
299
* which respectively means the session is closed, has data to read on
270
300
* the connection socket and session was closed due to an error.
272
int ssh_get_status(SSH_SESSION *session) {
302
int ssh_get_status(ssh_session session) {