/** * @internal * * @brief Add a message to the current queue of messages to be parsed. * * @param[in] session The SSH session to add the message. * * @param[in] message The message to add to the queue. */ void ssh_message_queue(ssh_session session, ssh_message message){ if(message) { if(session->ssh_message_list == NULL) { if(session->ssh_message_callback != NULL) { ssh_execute_message_callback(session, message); return; } session->ssh_message_list = ssh_list_new(); } ssh_list_append(session->ssh_message_list, message); } }
/** * @brief Retrieve a SSH message from a SSH session. * * @param[in] session The SSH session to get the message. * * @returns The SSH message received, NULL in case of error, or timeout * elapsed. * * @warning This function blocks until a message has been received. Betterset up * a callback if this behavior is unwanted. */ ssh_message ssh_message_get(ssh_session session) { ssh_message msg = NULL; int rc; msg=ssh_message_pop_head(session); if(msg) { return msg; } if(session->ssh_message_list == NULL) { session->ssh_message_list = ssh_list_new(); } rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER, ssh_message_termination, session); if(rc || session->session_state == SSH_SESSION_STATE_ERROR) return NULL; msg=ssh_list_pop_head(ssh_message, session->ssh_message_list); return msg; }
/** * @brief Retrieve a SSH message from a SSH session. * * @param[in] session The SSH session to get the message. * * @returns The SSH message received, NULL in case of error. * * @warning This function blocks until a message has been received. Betterset up * a callback if this behavior is unwanted. */ ssh_message ssh_message_get(ssh_session session) { ssh_message msg = NULL; enter_function(); msg=ssh_message_pop_head(session); if(msg) { leave_function(); return msg; } if(session->ssh_message_list == NULL) { session->ssh_message_list = ssh_list_new(); } do { if (ssh_handle_packets(session,-1) == SSH_ERROR) { leave_function(); return NULL; } msg=ssh_list_pop_head(ssh_message, session->ssh_message_list); } while(msg==NULL); leave_function(); return msg; }
/** * @brief Create a new ssh session. * * @returns A new ssh_session pointer, NULL on error. */ ssh_session ssh_new(void) { ssh_session session; char *id = NULL; int rc; session = malloc(sizeof (struct ssh_session_struct)); if (session == NULL) { return NULL; } ZERO_STRUCTP(session); session->next_crypto = crypto_new(); if (session->next_crypto == NULL) { goto err; } session->socket = ssh_socket_new(session); if (session->socket == NULL) { goto err; } session->out_buffer = ssh_buffer_new(); if (session->out_buffer == NULL) { goto err; } session->in_buffer=ssh_buffer_new(); if (session->in_buffer == NULL) { goto err; } session->alive = 0; session->auth_methods = 0; ssh_set_blocking(session, 1); session->common.log_indent = 0; session->maxchannel = FIRST_CHANNEL; #ifndef _WIN32 session->agent = agent_new(session); if (session->agent == NULL) { goto err; } #endif /* _WIN32 */ /* OPTIONS */ session->opts.StrictHostKeyChecking = 1; session->opts.port = 22; session->opts.fd = -1; session->opts.ssh2 = 1; session->opts.compressionlevel=7; #ifdef WITH_SSH1 session->opts.ssh1 = 1; #else session->opts.ssh1 = 0; #endif session->opts.identity = ssh_list_new(); if (session->opts.identity == NULL) { goto err; } id = strdup("%d/id_rsa"); if (id == NULL) { goto err; } rc = ssh_list_append(session->opts.identity, id); if (rc == SSH_ERROR) { goto err; } id = strdup("%d/id_dsa"); if (id == NULL) { goto err; } rc = ssh_list_append(session->opts.identity, id); if (rc == SSH_ERROR) { goto err; } id = strdup("%d/identity"); if (id == NULL) { goto err; } rc = ssh_list_append(session->opts.identity, id); if (rc == SSH_ERROR) { goto err; } return session; err: free(id); ssh_free(session); return NULL; }
/** * @brief Create a new ssh session. * * @returns A new ssh_session pointer, NULL on error. */ ssh_session ssh_new(void) { ssh_session session; char *id = NULL; int rc; session = malloc(sizeof (struct ssh_session_struct)); if (session == NULL) { return NULL; } ZERO_STRUCTP(session); session->next_crypto = crypto_new(); if (session->next_crypto == NULL) { goto err; } session->socket = ssh_socket_new(session); if (session->socket == NULL) { goto err; } session->out_buffer = ssh_buffer_new(); if (session->out_buffer == NULL) { goto err; } session->in_buffer=ssh_buffer_new(); if (session->in_buffer == NULL) { goto err; } session->alive = 0; session->auth_methods = 0; ssh_set_blocking(session, 1); session->maxchannel = FIRST_CHANNEL; #ifndef _WIN32 session->agent = ssh_agent_new(session); if (session->agent == NULL) { goto err; } #endif /* _WIN32 */ /* OPTIONS */ session->opts.StrictHostKeyChecking = 1; session->opts.port = 0; session->opts.fd = -1; session->opts.compressionlevel=7; session->opts.nodelay = 0; session->opts.flags = SSH_OPT_FLAG_PASSWORD_AUTH | SSH_OPT_FLAG_PUBKEY_AUTH | SSH_OPT_FLAG_KBDINT_AUTH | SSH_OPT_FLAG_GSSAPI_AUTH; session->opts.identity = ssh_list_new(); if (session->opts.identity == NULL) { goto err; } id = strdup("%d/id_ed25519"); if (id == NULL) { goto err; } rc = ssh_list_append(session->opts.identity, id); if (rc == SSH_ERROR) { goto err; } #ifdef HAVE_ECC id = strdup("%d/id_ecdsa"); if (id == NULL) { goto err; } rc = ssh_list_append(session->opts.identity, id); if (rc == SSH_ERROR) { goto err; } #endif id = strdup("%d/id_rsa"); if (id == NULL) { goto err; } rc = ssh_list_append(session->opts.identity, id); if (rc == SSH_ERROR) { goto err; } #ifdef HAVE_DSA id = strdup("%d/id_dsa"); if (id == NULL) { goto err; } rc = ssh_list_append(session->opts.identity, id); if (rc == SSH_ERROR) { goto err; } #endif return session; err: free(id); ssh_free(session); return NULL; }
/** @internal * @brief sets the callbacks for the packet layer */ void ssh_packet_set_callbacks(ssh_session session, ssh_packet_callbacks callbacks){ if(session->packet_callbacks == NULL){ session->packet_callbacks = ssh_list_new(); } ssh_list_append(session->packet_callbacks, callbacks); }
static void torture_callbacks_execute_list(void **state){ struct ssh_list *list = ssh_list_new(); int v = 0, w = 0; struct ssh_channel_callbacks_struct c1 = { .channel_eof_function = cb1, .userdata = &v }; struct ssh_channel_callbacks_struct c2 = { .channel_exit_status_function = cb2, .userdata = &v }; struct ssh_channel_callbacks_struct c3 = { .channel_eof_function = cb1, .channel_exit_status_function = cb2, .userdata = &w }; (void)state; ssh_callbacks_init(&c1); ssh_callbacks_init(&c2); ssh_callbacks_init(&c3); ssh_list_append(list, &c1); ssh_callbacks_execute_list(list, ssh_channel_callbacks, channel_eof_function, NULL, NULL); assert_int_equal(v, 1); v = 0; ssh_list_append(list, &c2); ssh_callbacks_execute_list(list, ssh_channel_callbacks, channel_eof_function, NULL, NULL); assert_int_equal(v, 1); ssh_callbacks_execute_list(list, ssh_channel_callbacks, channel_exit_status_function, NULL, NULL, 0); assert_int_equal(v, 11); v = 0; w = 0; ssh_list_append(list, &c3); ssh_callbacks_execute_list(list, ssh_channel_callbacks, channel_eof_function, NULL, NULL); assert_int_equal(v, 1); assert_int_equal(w, 1); ssh_callbacks_execute_list(list, ssh_channel_callbacks, channel_exit_status_function, NULL, NULL, 0); assert_int_equal(v, 11); assert_int_equal(w, 11); ssh_list_free(list); }