~ubuntu-branches/ubuntu/trusty/libssh/trusty

« back to all changes in this revision

Viewing changes to libssh/session.c

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Bigonville
  • Date: 2009-12-12 14:29:12 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20091212142912-ha5g2iibt6nfnjq8
Tags: 0.4.0-1
* New upstream release.
  - Bump soname
  - Adjust .symbols file
* Readd static library in -dev package
* Let dh_lintian install override file
* debian/README.Debian: Update file
* debian/rules: Add list-missing rule

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * MA 02111-1307, USA.
22
22
 */
23
23
 
 
24
#include "config.h"
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"
 
35
 
29
36
#define FIRST_CHANNEL 42 // why not ? it helps to find bugs.
30
37
 
31
38
/** \defgroup ssh_session SSH Session
37
44
/** \brief creates a new ssh session
38
45
 * \returns new ssh_session pointer
39
46
 */
40
 
SSH_SESSION *ssh_new(void) {
41
 
  SSH_SESSION *session;
 
47
ssh_session ssh_new(void) {
 
48
  ssh_session session;
42
49
 
43
 
  session = malloc(sizeof (SSH_SESSION));
 
50
  session = malloc(sizeof (struct ssh_session_struct));
44
51
  if (session == NULL) {
45
52
    return NULL;
46
53
  }
47
 
 
48
 
  memset(session, 0, sizeof(SSH_SESSION));
 
54
  ZERO_STRUCTP(session);
49
55
 
50
56
  session->next_crypto = crypto_new();
51
57
  if (session->next_crypto == NULL) {
52
58
    goto err;
53
59
  }
54
60
 
55
 
  session->maxchannel = FIRST_CHANNEL;
56
61
  session->socket = ssh_socket_new(session);
57
62
  if (session->socket == NULL) {
58
63
    goto err;
59
64
  }
60
65
 
61
 
  session->alive = 0;
62
 
  session->auth_methods = 0;
63
 
  session->blocking = 1;
64
 
  session->log_indent = 0;
65
 
 
66
66
  session->out_buffer = buffer_new();
67
67
  if (session->out_buffer == NULL) {
68
68
    goto err;
73
73
    goto err;
74
74
  }
75
75
 
 
76
  session->alive = 0;
 
77
  session->auth_methods = 0;
 
78
  session->blocking = 1;
 
79
  session->log_indent = 0;
 
80
  session->maxchannel = FIRST_CHANNEL;
 
81
 
 
82
  /* options */
 
83
  session->port = 22;
 
84
  session->fd = -1;
 
85
  session->ssh2 = 1;
 
86
#ifdef WITH_SSH1
 
87
  session->ssh1 = 1;
 
88
#else
 
89
  session->ssh1 = 0;
 
90
#endif
 
91
 
76
92
#ifndef _WIN32
77
93
    session->agent = agent_new(session);
78
94
    if (session->agent == NULL) {
82
98
    return session;
83
99
 
84
100
err:
85
 
    ssh_cleanup(session);
 
101
    ssh_free(session);
86
102
    return NULL;
87
103
}
88
104
 
89
 
void ssh_cleanup(SSH_SESSION *session) {
 
105
/**
 
106
 * @brief deallocate a session handle
 
107
 * @see ssh_disconnect()
 
108
 * @see ssh_new()
 
109
 */
 
110
void ssh_free(ssh_session session) {
90
111
  int i;
91
112
  enter_function();
92
113
 
97
118
  SAFE_FREE(session->serverbanner);
98
119
  SAFE_FREE(session->clientbanner);
99
120
  SAFE_FREE(session->banner);
 
121
#ifdef WITH_PCAP
 
122
  if(session->pcap_ctx){
 
123
        ssh_pcap_context_free(session->pcap_ctx);
 
124
        session->pcap_ctx=NULL;
 
125
  }
 
126
#endif
100
127
  buffer_free(session->in_buffer);
101
128
  buffer_free(session->out_buffer);
102
129
  session->in_buffer=session->out_buffer=NULL;
126
153
 
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){
 
157
    ssh_message msg;
 
158
    while((msg=ssh_list_get_head(ssh_message ,session->ssh_message_list))
 
159
        != NULL){
 
160
      ssh_message_free(msg);
 
161
    }
 
162
    ssh_list_free(session->ssh_message_list);
 
163
  }
 
164
 
 
165
  /* options */
 
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);
 
171
 
 
172
  for (i = 0; i < 10; i++) {
 
173
    if (session->wanted_methods[i]) {
 
174
      SAFE_FREE(session->wanted_methods[i]);
 
175
    }
 
176
  }
131
177
 
132
178
  /* burn connection, it could hang sensitive datas */
133
 
  memset(session,'X',sizeof(SSH_SESSION));
134
 
 
 
179
  ZERO_STRUCTP(session);
135
180
  SAFE_FREE(session);
136
 
  /* FIXME: leave_function(); ??? */
137
181
}
138
182
 
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
141
186
 */
142
 
void ssh_silent_disconnect(SSH_SESSION *session) {
 
187
void ssh_silent_disconnect(ssh_session session) {
143
188
  enter_function();
144
189
 
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(); ??? */
153
 
}
154
 
 
155
 
/** \brief set the options for the current session
156
 
 * \param session ssh session
157
 
 * \param options options structure
158
 
 * \see ssh_new()
159
 
 * \see ssh_options_new()
160
 
 */
161
 
void ssh_set_options(SSH_SESSION *session, SSH_OPTIONS *options) {
162
 
  if (session == NULL || options == NULL) {
163
 
    return;
164
 
  }
165
 
 
166
 
  session->options = options;
167
 
  session->log_verbosity = options->log_verbosity;
 
197
  leave_function();
168
198
}
169
199
 
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
174
204
 */
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) {
177
207
    return;
178
208
  }
187
217
 * \return file descriptor of the connection, or -1 if it is
188
218
 * not connected
189
219
 */
190
 
socket_t ssh_get_fd(SSH_SESSION *session) {
 
220
socket_t ssh_get_fd(ssh_session session) {
191
221
  if (session == NULL) {
192
222
    return -1;
193
223
  }
198
228
/** \brief say to the session it has data to read on the file descriptor without blocking
199
229
 * \param session ssh session
200
230
 */
201
 
void ssh_set_fd_toread(SSH_SESSION *session) {
 
231
void ssh_set_fd_toread(ssh_session session) {
202
232
  if (session == NULL) {
203
233
    return;
204
234
  }
209
239
/** \brief say the session it may write to the file descriptor without blocking
210
240
 * \param session ssh session
211
241
 */
212
 
void ssh_set_fd_towrite(SSH_SESSION *session) {
 
242
void ssh_set_fd_towrite(ssh_session session) {
213
243
  if (session == NULL) {
214
244
    return;
215
245
  }
220
250
/** \brief say the session it has an exception to catch on the file descriptor
221
251
 * \param session ssh session
222
252
 */
223
 
void ssh_set_fd_except(SSH_SESSION *session) {
 
253
void ssh_set_fd_except(ssh_session session) {
224
254
  if (session == NULL) {
225
255
    return;
226
256
  }
231
261
/** \warning I don't remember if this should be internal or not
232
262
 */
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) {
235
265
  int w = 0;
236
266
  int e = 0;
237
267
  int rc = -1;
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.
271
301
 */
272
 
int ssh_get_status(SSH_SESSION *session) {
 
302
int ssh_get_status(ssh_session session) {
273
303
  int socketstate;
274
304
  int r = 0;
275
305
 
297
327
 * \return message sent by the server along with the disconnect, or NULL in which case the reason of the disconnect may be found with ssh_get_error.
298
328
 * \see ssh_get_error()
299
329
 */
300
 
const char *ssh_get_disconnect_message(SSH_SESSION *session) {
 
330
const char *ssh_get_disconnect_message(ssh_session session) {
301
331
  if (session == NULL) {
302
332
    return NULL;
303
333
  }
325
355
 *
326
356
 * @return 1 or 2, for ssh1 or ssh2, < 0 on error.
327
357
 */
328
 
int ssh_get_version(SSH_SESSION *session) {
 
358
int ssh_get_version(ssh_session session) {
329
359
  if (session == NULL) {
330
360
    return -1;
331
361
  }