static void recv_result_success(void) { set_ssh_connect_res(SSH_OK); set_ssh_channel_open_session_ret(SSH_OK); set_ssh_channel_request_exec_ret(SSH_OK); set_ssh_channel_read_ret(sizeof(encoded_res)); set_ssh_channel_read_set(encoded_res); set_ssh_channel_change_pty_size_ret(SSH_OK); set_ssh_channel_write_ret(11); reset_ssh_channel_write_first(FALSE); wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t); wsh_cmd_res_t* res = NULL; session->hostname = remote; session->username = username; GError* err = NULL; wsh_ssh_host(session, &err); wsh_ssh_exec_wshd(session, &err); gint ret = wsh_ssh_recv_cmd_res(session, &res, &err); g_assert(ret == 0); g_assert(res != NULL); g_assert(session->session != NULL); g_assert(session->channel != NULL); g_assert_no_error(err); g_free(session->session); g_free(session->channel); g_slice_free(wsh_ssh_session_t, session); wsh_free_unpacked_response(&res); }
static void exec_wshd_success(void) { set_ssh_connect_res(SSH_OK); set_ssh_channel_open_session_ret(SSH_OK); set_ssh_channel_request_exec_ret(SSH_OK); set_ssh_channel_poll_timeout_ret(0); reset_ssh_channel_write_first(FALSE); set_ssh_channel_write_ret(11); wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t); session->hostname = remote; session->username = username; GError* err = NULL; wsh_ssh_host(session, &err); gint ret = wsh_ssh_exec_wshd(session, &err); g_assert(ret == 0); g_assert(session->session != NULL); g_assert(session->channel != NULL); g_assert_no_error(err); g_free(session->session); g_free(session->channel); g_slice_free(wsh_ssh_session_t, session); }
static void exec_wshd_channel_failure(void) { set_ssh_connect_res(SSH_OK); set_ssh_channel_open_session_ret(SSH_ERROR); wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t); session->hostname = remote; session->username = username; GError* err = NULL; wsh_ssh_host(session, &err); gint ret = wsh_ssh_exec_wshd(session, &err); g_assert(ret == WSH_SSH_CHANNEL_CREATION_ERR); g_assert(session->session == NULL); g_assert(session->channel == NULL); g_assert_error(err, WSH_SSH_ERROR, WSH_SSH_CHANNEL_CREATION_ERR); g_error_free(err); g_slice_free(wsh_ssh_session_t, session); }
static void send_cmd_write_success(void) { set_ssh_connect_res(SSH_OK); set_ssh_channel_open_session_ret(SSH_OK); set_ssh_channel_request_exec_ret(SSH_OK); set_ssh_channel_change_pty_size_ret(SSH_OK); set_ssh_channel_write_ret(11); reset_ssh_channel_write_first(FALSE); wsh_cmd_req_t* req = g_slice_new0(wsh_cmd_req_t); wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t); session->hostname = remote; session->username = username; req->cmd_string = req_cmd; req->std_input = req_stdin; req->std_input_len = req_stdin_len; req->env = req_env; req->cwd = req_cwd; req->timeout = req_timeout; req->username = req_username; req->password = req_password; GError* err = NULL; wsh_ssh_host(session, &err); wsh_ssh_exec_wshd(session, &err); reset_ssh_channel_write_first(TRUE); set_ssh_channel_write_ret(89); gint ret = wsh_ssh_send_cmd(session, req, &err); g_assert(ret == 0); g_assert(session->session != NULL); g_assert(session->channel != NULL); g_assert_no_error(err); g_free(session->session); g_free(session->channel); g_slice_free(wsh_ssh_session_t, session); g_slice_free(wsh_cmd_req_t, req); }
void wshc_try_ssh(wshc_host_info_t* host_info, const wshc_cmd_info_t* cmd_info) { g_assert(cmd_info != NULL); g_assert(host_info != NULL); GError* err = NULL; wsh_ssh_session_t session = { .hostname = host_info->hostname, .username = cmd_info->username, .password = cmd_info->password, .port = cmd_info->port, .session = NULL, .ssh_opts = cmd_info->ssh_opts, }; if (session.password == NULL) { session.auth_type = WSH_SSH_AUTH_PUBKEY; wshc_verbose_print(cmd_info->out, "Using public key authentication\n"); } else { session.auth_type = WSH_SSH_AUTH_PASSWORD; wshc_verbose_print(cmd_info->out, "Using password authentication\n"); } wshc_verbose_print(cmd_info->out, "Initiating connection to %s\n", host_info->hostname); if (wsh_ssh_host(&session, &err)) { wshc_add_failed_host(cmd_info->out, host_info->hostname, err->message); wshc_verbose_print(cmd_info->out, "Connection failed on %s: %s\n", host_info->hostname, err->message); g_error_free(err); err = NULL; return; } wshc_verbose_print(cmd_info->out, "Connection to %s successful\n", host_info->hostname); wshc_verbose_print(cmd_info->out, "Verifying host key for %s\n", host_info->hostname); if (wsh_verify_host_key(&session, FALSE, FALSE, &err)) { wshc_add_failed_host(cmd_info->out, host_info->hostname, err->message); wshc_verbose_print(cmd_info->out, "Host key verification for %s failed: %s\n", host_info->hostname, err->message); g_error_free(err); err = NULL; return; } wshc_verbose_print(cmd_info->out, "Host verification for %s successful\n", host_info->hostname); wshc_verbose_print(cmd_info->out, "Authenticating to %s\n", host_info->hostname); if (wsh_ssh_authenticate(&session, &err)) { wshc_add_failed_host(cmd_info->out, host_info->hostname, err->message); wshc_verbose_print(cmd_info->out, "Failed to authenticate to %s: %s\n", host_info->hostname, err->message); g_error_free(err); err = NULL; return; } wshc_verbose_print(cmd_info->out, "Authenticated to %s successfully\n", host_info->hostname); if (cmd_info->script) { wshc_verbose_print(cmd_info->out, "Initializing scp subsystem for %s\n", host_info->hostname); if (wsh_ssh_scp_init(&session, "~")) { wshc_add_failed_host(cmd_info->out, host_info->hostname, "Could not init scp"); wshc_verbose_print(cmd_info->out, "Failed to init scp on %s\n", host_info->hostname); return; } wshc_verbose_print(cmd_info->out, "Initialized scp subsystem on %s\n", host_info->hostname); wshc_verbose_print(cmd_info->out, "Transferring script %s to %s\n", cmd_info->script, host_info->hostname); if (wsh_ssh_scp_file(&session, cmd_info->script, TRUE, &err)) { wshc_add_failed_host(cmd_info->out, host_info->hostname, err->message); wshc_verbose_print(cmd_info->out, "Failed to transfer script %s to %s\n", cmd_info->script, host_info->hostname); g_error_free(err); err = NULL; } wshc_verbose_print(cmd_info->out, "Transferred script %s to %s successfully\n", cmd_info->script, host_info->hostname); wsh_ssh_scp_cleanup(&session); } wshc_verbose_print(cmd_info->out, "Execing wshd on %s\n", host_info->hostname); if (wsh_ssh_exec_wshd(&session, &err)) { wshc_add_failed_host(cmd_info->out, host_info->hostname, err->message); wshc_verbose_print(cmd_info->out, "Failed to exec wshd on %s: %s\n", host_info->hostname, err->message); g_error_free(err); err = NULL; return; } wshc_verbose_print(cmd_info->out, "Successfully launched wshd %s\n", host_info->hostname); wshc_verbose_print(cmd_info->out, "Sending command info to wshd on %s\n", host_info->hostname); if (wsh_ssh_send_cmd(&session, cmd_info->req, &err)) { wshc_verbose_print(cmd_info->out, "Failed to send command to %s: %s\n", host_info->hostname, err->message); wshc_add_failed_host(cmd_info->out, host_info->hostname, err->message); g_error_free(err); err = NULL; return; } wshc_verbose_print(cmd_info->out, "Successfully sent command info to wshd on %s\n", host_info->hostname); wshc_verbose_print(cmd_info->out, "Waiting for response from %s\n", host_info->hostname); if (wsh_ssh_recv_cmd_res(&session, host_info->res, &err)) { wshc_verbose_print(cmd_info->out, "Failed to receive a command from %s: %s\n", host_info->hostname, err->message); wshc_add_failed_host(cmd_info->out, host_info->hostname, err->message); g_error_free(err); err = NULL; return; } wshc_verbose_print(cmd_info->out, "Got response from %s\n", host_info->hostname); wsh_log_client_cmd_status(cmd_info->req->cmd_string, cmd_info->req->username, host_info->hostname, cmd_info->req->cwd, (*host_info->res)->exit_status); wshc_write_output(cmd_info->out, host_info->hostname, *host_info->res); wsh_free_unpacked_response(host_info->res); wsh_ssh_disconnect(&session); }