static int ssh_open_remote_connection(const char* hostname, const unsigned int port, const char* username, const char* password, const char* sshkey, const char* sshkey_passphrase, const char* iface, const char* cfilter, const char* capture_bin, const unsigned long int count, const char* fifo) { ssh_session sshs = NULL; ssh_channel channel = NULL; int fd = STDOUT_FILENO; int ret = EXIT_FAILURE; char* err_info = NULL; if (g_strcmp0(fifo, "-")) { /* Open or create the output file */ fd = ws_open(fifo, O_WRONLY, 0640); if (fd == -1) { fd = ws_open(fifo, O_WRONLY | O_CREAT, 0640); if (fd == -1) { errmsg_print("Error creating output file: %s", g_strerror(errno)); return EXIT_FAILURE; } } } sshs = create_ssh_connection(hostname, port, username, password, sshkey, sshkey_passphrase, &err_info); if (!sshs) { errmsg_print("Error creating connection: %s", err_info); goto cleanup; } channel = run_ssh_command(sshs, capture_bin, iface, cfilter, count); if (!channel) goto cleanup; /* read from channel and write into fd */ ssh_loop_read(channel, fd); ret = EXIT_SUCCESS; cleanup: if (err_info) errmsg_print("%s", err_info); g_free(err_info); /* clean up and exit */ ssh_cleanup(&sshs, &channel); if (g_strcmp0(fifo, "-")) ws_close(fd); return ret; }
static void ciscodump_cleanup(ssh_session sshs, ssh_channel channel, const char* iface, const char* cfilter) { if (channel) { if (read_output_bytes(channel, -1, NULL) == EXIT_SUCCESS) { ssh_channel_printf(channel, "monitor capture point stop %s\n", WIRESHARK_CAPTURE_POINT); ssh_channel_printf(channel, "no monitor capture point ip cef %s %s\n", WIRESHARK_CAPTURE_POINT, iface); ssh_channel_printf(channel, "no monitor capture buffer %s\n", WIRESHARK_CAPTURE_BUFFER); if (cfilter) { ssh_channel_printf(channel, "configure terminal\n"); ssh_channel_printf(channel, "no ip access-list ex %s\n", WIRESHARK_CAPTURE_ACCESSLIST); } read_output_bytes(channel, -1, NULL); } } ssh_cleanup(&sshs, &channel); }
/** \brief disconnect from a session (client or server) * \param session ssh session */ void ssh_disconnect(SSH_SESSION *session){ STRING *str; if(session->fd!= -1) { packet_clear_out(session); buffer_add_u8(session->out_buffer,SSH2_MSG_DISCONNECT); buffer_add_u32(session->out_buffer,htonl(SSH2_DISCONNECT_BY_APPLICATION)); str=string_from_char("Bye Bye"); buffer_add_ssh_string(session->out_buffer,str); free(str); packet_send(session); close(session->fd); session->fd=-1; } session->alive=0; ssh_cleanup(session); }
static int ssh_open_remote_connection(const char* hostname, const unsigned int port, const char* username, const char* password, const char* sshkey, const char* sshkey_passphrase, const char* iface, const char* cfilter, const char* capture_bin, const unsigned long int count, const char* fifo) { ssh_session sshs; ssh_channel channel; int fd; if (!g_strcmp0(fifo, "-")) { /* use stdout */ fd = STDOUT_FILENO; } else { /* Open or create the output file */ fd = open(fifo, O_WRONLY); if (fd == -1) { fd = open(fifo, O_WRONLY | O_CREAT, 0640); if (fd == -1) { errmsg_print("Error creating output file: %s\n", g_strerror(errno)); return EXIT_FAILURE; } } } sshs = create_ssh_connection(hostname, port, username, password, sshkey, sshkey_passphrase); if (!sshs) return EXIT_FAILURE; channel = run_ssh_command(sshs, capture_bin, iface, cfilter, count); if (!channel) return EXIT_FAILURE; /* read from channel and write into fd */ ssh_loop_read(channel, fd); /* clean up and exit */ ssh_cleanup(sshs, channel); return EXIT_SUCCESS; }
SSH_SESSION *ssh_bind_accept(SSH_BIND *ssh_bind) { SSH_SESSION *session; PRIVATE_KEY *dsa = NULL; PRIVATE_KEY *rsa = NULL; int fd = -1; if (ssh_bind->bindfd < 0) { ssh_set_error(ssh_bind, SSH_FATAL, "Can't accept new clients on a not bound socket."); return NULL; } if (ssh_bind->options->dsakey == NULL || ssh_bind->options->rsakey == NULL) { ssh_set_error(ssh_bind, SSH_FATAL, "DSA or RSA host key file must be set before accept()"); return NULL; } if (ssh_bind->options->dsakey) { dsa = _privatekey_from_file(ssh_bind, ssh_bind->options->dsakey, TYPE_DSS); if (dsa == NULL) { return NULL; } } if (ssh_bind->options->rsakey) { rsa = _privatekey_from_file(ssh_bind, ssh_bind->options->rsakey, TYPE_RSA); if (rsa == NULL) { privatekey_free(dsa); return NULL; } } fd = accept(ssh_bind->bindfd, NULL, NULL); if (fd < 0) { ssh_set_error(ssh_bind, SSH_FATAL, "Accepting a new connection: %s", strerror(errno)); privatekey_free(dsa); privatekey_free(rsa); return NULL; } session = ssh_new(); if (session == NULL) { ssh_set_error(ssh_bind, SSH_FATAL, "Not enough space"); privatekey_free(dsa); privatekey_free(rsa); return NULL; } session->server = 1; session->version = 2; session->options = ssh_options_copy(ssh_bind->options); if (session->options == NULL) { ssh_set_error(ssh_bind, SSH_FATAL, "No space left"); privatekey_free(dsa); privatekey_free(rsa); ssh_cleanup(session); return NULL; } ssh_socket_free(session->socket); session->socket = ssh_socket_new(session); if (session->socket == NULL) { privatekey_free(dsa); privatekey_free(rsa); ssh_cleanup(session); return NULL; } ssh_socket_set_fd(session->socket,fd); session->dsa_key = dsa; session->rsa_key = rsa; return session; }