static void cockpit_ssh_transport_init (CockpitSshTransport *self) { static struct ssh_channel_callbacks_struct channel_cbs = { .channel_data_function = on_channel_data, .channel_eof_function = on_channel_eof, .channel_close_function = on_channel_close, .channel_signal_function = on_channel_signal, .channel_exit_signal_function = on_channel_exit_signal, .channel_exit_status_function = on_channel_exit_status, }; self->data = g_new0 (CockpitSshData, 1); self->data->context = g_main_context_get_thread_default (); if (self->data->context) g_main_context_ref (self->data->context); self->data->session = ssh_new (); g_return_if_fail (self->data->session != NULL); self->buffer = g_byte_array_new (); self->queue = g_queue_new (); memcpy (&self->channel_cbs, &channel_cbs, sizeof (channel_cbs)); self->channel_cbs.userdata = self; ssh_callbacks_init (&self->channel_cbs); self->event = ssh_event_new (); }
int main() { ssh_session my_ssh_session; int rc; int port = 22; my_ssh_session = ssh_new(); if(my_ssh_session == NULL) exit(-1); ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "localhost"); ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port); ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "root"); rc = ssh_connect(my_ssh_session); if(rc != SSH_OK) { fprintf(stderr, "Error connecting to localhost: %s\n", ssh_get_error(my_ssh_session)); exit(-1); } scp_write(my_ssh_session); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); }
SSH_SESSION *connect_host(const char *hostname){ SSH_SESSION *session; SSH_OPTIONS *options; int auth=0; int state; options=ssh_options_new(); ssh_options_set_host(options,hostname); session=ssh_new(); ssh_set_options(session,options); if(ssh_connect(session)){ fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session)); ssh_disconnect(session); return NULL; } state = ssh_session_is_known_server(session); switch(state){ case SSH_SERVER_KNOWN_OK: break; /* ok */ case SSH_SERVER_KNOWN_CHANGED: fprintf(stderr,"Host key for server changed : server's one is now :\n"); fprintf(stderr,"For security reason, connection will be stopped\n"); ssh_disconnect(session); ssh_finalize(); return NULL; case SSH_SERVER_FOUND_OTHER: fprintf(stderr,"The host key for this server was not found but an other type of key exists.\n"); fprintf(stderr,"An attacker might change the default server key to confuse your client" "into thinking the key does not exist\n" "We advise you to rerun the client with -d or -r for more safety.\n"); ssh_disconnect(session); ssh_finalize(); return NULL; case SSH_SERVER_NOT_KNOWN: fprintf(stderr,"The server is unknown. Leaving now"); ssh_disconnect(session); return NULL; case SSH_SERVER_ERROR: fprintf(stderr,"%s",ssh_get_error(session)); ssh_disconnect(session); return NULL; } ssh_userauth_none(session, NULL); auth=ssh_userauth_autopubkey(session, NULL); if(auth==SSH_AUTH_ERROR){ fprintf(stderr,"Authenticating with pubkey: %s\n",ssh_get_error(session)); ssh_disconnect(session); return NULL; } if(auth!=SSH_AUTH_SUCCESS){ fprintf(stderr,"Authentication failed: %s\n",ssh_get_error(session)); ssh_disconnect(session); return NULL; } ssh_log(session, SSH_LOG_FUNCTIONS, "Authentication success"); return session; }
static int session_setup(void **state) { struct torture_state *s = *state; int verbosity = torture_libssh_verbosity(); struct passwd *pwd; bool b = false; int rc; pwd = getpwnam("bob"); assert_non_null(pwd); rc = setuid(pwd->pw_uid); assert_return_code(rc, errno); s->ssh.session = ssh_new(); assert_non_null(s->ssh.session); ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); /* Make sure no other configuration options from system will get used */ rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b); assert_ssh_return_code(s->ssh.session, rc); return 0; }
int main(int argc, char **argv){ ssh_session session; session = ssh_new(); if(ssh_options_getopt(session, &argc, argv)) { fprintf(stderr, "error parsing command line :%s\n", ssh_get_error(session)); usage(); } opts(argc,argv); #ifdef WITH_PCAP set_pcap(session); #endif client(session); ssh_disconnect(session); ssh_free(session); #ifdef WITH_PCAP cleanup_pcap(); #endif ssh_finalize(); return 0; }
void Session::connect() { if (this->c_session != NULL) { this->disconnect(); } this->c_session = ssh_new(); if (this->c_session == NULL) { throw std::runtime_error("Cannot start session"); } ssh_options_set(this->c_session, SSH_OPTIONS_HOST, this->hostname.c_str()); ssh_options_set(this->c_session, SSH_OPTIONS_PORT, &this->port); int rc = ssh_connect(this->c_session); if (rc != SSH_OK) { fprintf(stderr, "Error connecting to localhost: %s\n", ssh_get_error(this->c_session)); throw std::runtime_error("Cannot connect to server"); } if (this->password_auth) { rc = ssh_userauth_password(this->c_session, NULL, this->password.c_str()); if (rc != SSH_AUTH_SUCCESS) { throw std::runtime_error("Cannot authenticate with server"); } } else { rc = ssh_userauth_autopubkey(this->c_session, NULL); if (rc != SSH_AUTH_SUCCESS) { throw std::runtime_error("Cannot authenticate with server"); } } }
int SSH::openChannel() { int rc; _logger->trace("Opening SSH channel"); if (_ssh == NULL) { _ssh = ssh_new(); } _channel = ssh_channel_new(_ssh); if (_channel == NULL) { _logger->error("Failed to open SSH channel"); throw SSHException("Failed to open SSH channel", SSHErrorCode::E_CHAN_OPEN_FAILED); } rc = ssh_channel_open_session(_channel); if (rc != SSH_OK) { ssh_channel_close(_channel); ssh_channel_free(_channel); throw SSHException("Failed to open SSH session", SSHErrorCode::E_SESS_OPEN_FAILED); } _bChanOpen = true; return SSHErrorCode::E_NOERR; }
SSH::SSH(const std::string& host, const std::string& user, const std::string& pass, const std::string port): host_(host),user_(user),pass_(pass),sftp_(0) { std::string timeout = getEnv(envTIMEOUT); timeout_ = atol(timeout.c_str()); if (timeout_ == 0) { throw Error(1, "Timeout Environmental Variable must be a positive integer."); } session_ = ssh_new(); if (session_ == NULL) { throw Error(1, "Unable to create the the ssh session"); } // try to connect ssh_options_set(session_, SSH_OPTIONS_HOST, host_.c_str()); ssh_options_set(session_, SSH_OPTIONS_USER, user_.c_str()); ssh_options_set(session_, SSH_OPTIONS_TIMEOUT, &timeout_); if (port != "") ssh_options_set(session_, SSH_OPTIONS_PORT_STR, port.c_str()); if (ssh_connect(session_) != SSH_OK) { throw Error(1, ssh_get_error(session_)); } // Authentication if (ssh_userauth_password(session_, NULL, pass_.c_str()) != SSH_AUTH_SUCCESS) { throw Error(1, ssh_get_error(session_)); } }
int ssh_login(const char *user, const char *passwd){ ssh_session ssh_id; int ret = 0; printf("[*] testing => %s:%s:%s\n", target, user, passwd); ssh_id = ssh_new(); ssh_options_set(ssh_id, SSH_OPTIONS_HOST, target); ssh_options_set(ssh_id, SSH_OPTIONS_USER, user); if(ssh_connect(ssh_id) != SSH_OK){ goto end; } if(ssh_userauth_password(ssh_id, NULL, passwd) != SSH_AUTH_SUCCESS){ goto end; } printf("\n\n\tCracked --> [%s : %s]\n\n", user, passwd); ret = 1; end: ssh_disconnect(ssh_id); ssh_free(ssh_id); return ret; }
static int session_setup(void **state) { struct torture_state *s = *state; int verbosity = torture_libssh_verbosity(); struct passwd *pwd; int rc; pwd = getpwnam("bob"); assert_non_null(pwd); rc = setuid(pwd->pw_uid); assert_return_code(rc, errno); s->ssh.session = ssh_new(); assert_non_null(s->ssh.session); rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); assert_ssh_return_code(s->ssh.session, rc); rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); assert_ssh_return_code(s->ssh.session, rc); rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE); assert_ssh_return_code(s->ssh.session, rc); return 0; }
int main(int argc, char **argv) { ssh_session_t *session = NULL; session = ssh_new(); ssh_callbacks_init(&cb); ssh_set_callbacks(session,&cb); if(ssh_options_getopt(session, &argc, argv)) { fprintf(stderr, "error parsing command line :%s\n", ssh_get_error(session)); usage(); } opts(argc,argv); signal(SIGTERM, do_exit); client(session); ssh_disconnect(session); ssh_free(session); ssh_finalize(); return 0; }
gboolean remmina_ssh_init_session (RemminaSSH *ssh) { gint verbosity; ssh->callback = g_new0 (struct ssh_callbacks_struct, 1); ssh->callback->userdata = ssh; /* Init & startup the SSH session */ ssh->session = ssh_new (); ssh_options_set (ssh->session, SSH_OPTIONS_HOST, ssh->server); ssh_options_set (ssh->session, SSH_OPTIONS_PORT, &ssh->port); ssh_options_set (ssh->session, SSH_OPTIONS_USER, ssh->user); if (remmina_log_running ()) { verbosity = SSH_LOG_RARE; ssh_options_set (ssh->session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); ssh->callback->log_function = remmina_ssh_log_callback; } ssh_callbacks_init (ssh->callback); ssh_set_callbacks(ssh->session, ssh->callback); if (ssh_connect (ssh->session)) { remmina_ssh_set_error (ssh, _("Failed to startup SSH session: %s")); return FALSE; } /* Try the "none" authentication */ if (ssh_userauth_none (ssh->session, NULL) == SSH_AUTH_SUCCESS) { ssh->authenticated = TRUE; } return TRUE; }
int process_ssh_target(char * pcAddress, char * pcLogin, char * pcPasswd,char * pcDatafile) { ssh_session session; if ( ( NULL == pcAddress) || ( NULL == pcLogin) || ( NULL == pcPasswd) || ( NULL == pcDatafile) ) { printf("ERROR: Incorrect data; somewhere empty string.\n");//TODO return -8;//TODO } session = ssh_new(); ssh_callbacks_init(&cb); ssh_set_callbacks(session,&cb); user_host_file(pcLogin, pcAddress, pcDatafile); signal(SIGTERM, do_exit); /* Create input pipe between two endpoints */ pipe(input_pipe); #if defined(OUT_PIPE) /* Create output pipe between two endpoints */ pipe(output_pipe); #endif /* OUT_PIPE */ /* Launch Successor to push commands into tray */ iInput_Start("none", 25); #if defined(OUT_PIPE) // iOutput_Start("", 25); #endif /* OUT_PIPE */ client_ssh(session, pcPasswd); ssh_disconnect(session); ssh_free(session); ssh_finalize(); /* allocated in <assign_host_file> */ free (user); /* allocated in <assign_host_file> */ free (host); /* Free memory occupied by dynamically stored raw data */ DeleteCmds(&pCmdChain); return 0; }
static void setup(void **state) { int verbosity=torture_libssh_verbosity(); ssh_session session = ssh_new(); ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); *state = session; }
int new_ssh_session() { my_ssh_session = ssh_new(); if (my_ssh_session == NULL) { fprintf(stderr, "Error creating ssh session\n"); return SSH_ERROR; } return SSH_OK; }
static void torture_knownhosts_unknown(void **state) { struct torture_state *s = *state; ssh_session session = s->ssh.session; char known_hosts_file[1024] = {0}; enum ssh_known_hosts_e found; int rc; snprintf(known_hosts_file, sizeof(known_hosts_file), "%s/%s", s->socket_dir, TORTURE_KNOWN_HOSTS_FILE); rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file); assert_ssh_return_code(session, rc); rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "ssh-ed25519"); assert_ssh_return_code(session, rc); rc = ssh_connect(session); assert_ssh_return_code(session, rc); found = ssh_session_is_known_server(session); assert_int_equal(found, SSH_KNOWN_HOSTS_UNKNOWN); rc = ssh_session_update_known_hosts(session); assert_ssh_return_code(session, rc); ssh_disconnect(session); ssh_free(session); /* connect again and check host key */ session = ssh_new(); assert_non_null(session); s->ssh.session = session; rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); assert_ssh_return_code(s->ssh.session, rc); rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE); assert_ssh_return_code(s->ssh.session, rc); rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file); assert_ssh_return_code(session, rc); rc = ssh_connect(session); assert_ssh_return_code(session, rc); /* ssh-rsa is the default but libssh should try ssh-ed25519 instead */ found = ssh_session_is_known_server(session); assert_int_equal(found, SSH_KNOWN_HOSTS_OK); /* session will be freed by session_teardown() */ }
CAMLprim value libssh_ml_ssh_init(void) { ssh_session sess = ssh_new(); if (!sess) { caml_failwith("Couldn't allocate ssh session"); } return (value)sess; }
int spatch() { ssh_session session = NULL; ssh_bind sshbind = NULL; sthreadList* list = NULL; int port = SERVER_PORT; sshbind=ssh_bind_new(); //session=ssh_new(); ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port); ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_DSAKEY, KEYS_FOLDER "ssh_host_dsa_key"); ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY, KEYS_FOLDER "ssh_host_rsa_key"); if (ssh_bind_listen(sshbind)<0) { printf("Error listening to socket: %s\n", ssh_get_error(sshbind)); return 1; } printf("Server start on port %d\n", port); while(1) { session=ssh_new(); if (ssh_bind_accept(sshbind, session) == SSH_ERROR) { printf("Error accepting a connection: %s\n", ssh_get_error(sshbind)); ssh_free(session); } else { list = newNodeList(list, session); if (pthread_create(&(list->thread), NULL, (void*)NewSessionLoop, (void*)&(list->sesData)) != 0) { sthreadList* tmp; ssh_disconnect(session); ssh_free(session); tmp = list; list = list->next; free(tmp); } } } if (session) ssh_free(session); cleanList(list); ssh_bind_free(sshbind); ssh_finalize(); return 0; }
// // dirty workaround here: miscptr is the ptr to the logins, and the first one is used // to test if password authentication is enabled!! // int32_t service_ssh_init(char *ip, int32_t sp, unsigned char options, char *miscptr, FILE * fp, int32_t port, char *hostname) { // called before the childrens are forked off, so this is the function // which should be filled if initial connections and service setup has to be // performed once only. // // fill if needed. // // return codes: // 0 all OK // 1 skip target without generating an error // 2 skip target because of protocol problems // 3 skip target because its unreachable #ifdef LIBSSH int32_t rc, method; ssh_session session = ssh_new(); if (verbose || debug) printf("[INFO] Testing if password authentication is supported by ssh://%s@%s:%d\n", miscptr == NULL ? "hydra" : miscptr, hydra_address2string_beautiful(ip), port); ssh_options_set(session, SSH_OPTIONS_PORT, &port); ssh_options_set(session, SSH_OPTIONS_HOST, hydra_address2string(ip)); if (miscptr == NULL) ssh_options_set(session, SSH_OPTIONS_USER, "hydra"); else ssh_options_set(session, SSH_OPTIONS_USER, miscptr); ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &hydra_options.waittime); ssh_options_set(session, SSH_OPTIONS_COMPRESSION_C_S, "none"); ssh_options_set(session, SSH_OPTIONS_COMPRESSION_S_C, "none"); if (ssh_connect(session) != 0) { fprintf(stderr, "[ERROR] could not connect to ssh://%s:%d - %s\n", hydra_address2string_beautiful(ip), port, ssh_get_error(session)); return 2; } rc = ssh_userauth_none(session, NULL); method = ssh_userauth_list(session, NULL); ssh_disconnect(session); ssh_finalize(); ssh_free(session); if (debug) printf("[DEBUG] SSH method check: %08x\n", method); if ((method & SSH_AUTH_METHOD_INTERACTIVE) || (method & SSH_AUTH_METHOD_PASSWORD)) { if (verbose || debug) printf("[INFO] Successful, password authentication is supported by ssh://%s:%d\n", hydra_address2string_beautiful(ip), port); return 0; } else if (method == 0) { if (verbose || debug) fprintf(stderr, "[WARNING] invalid SSH method reply from ssh://%s:%d, continuing anyway ... (check for empty password!)\n", hydra_address2string_beautiful(ip), port); return 0; } fprintf(stderr, "[ERROR] target ssh://%s:%d/ does not support password authentication (method reply %d).\n", hydra_address2string_beautiful(ip), port, method); return 1; #else return 0; #endif }
std::string SSH::execute(const std::string& command) { _logger->trace("Executing SSH command: %s", command); int rc; char buffer[1024]; int nbytes; memset(buffer, 0, 1024); if (_ssh == NULL) { _logger->debug("Initializing SSH"); _ssh = ssh_new(); } if (_ssh == NULL) { throw SubutaiException("Failed to start SSH session"); } if (!_bChanOpen) { rc = openChannel(); if (rc != E_NOERR) { _logger->error("Couldn't open SSH channel: %d", rc); return ""; } } _logger->trace("Starting SSH command execution"); rc = ssh_channel_request_exec(_channel, command.c_str()); if (rc != SSH_OK) { closeChannel(); throw SSHException("Failed to execute SSH command", SSHErrorCode::E_CMD_EXEC_FAILED); } _logger->trace("Reading from channel"); nbytes = ssh_channel_read(_channel, buffer, sizeof(buffer), 0); std::string pBuffer(""); while (nbytes > 0) { pBuffer.append(buffer); nbytes = ssh_channel_read(_channel, buffer, sizeof(buffer), 0); } if (nbytes < 0) { closeChannel(); throw SSHException("Output channel is empty", SSHErrorCode::E_EMPTY_OUTPUT_CHAN); } closeChannel(); _logger->debug("SSH Execution output: %s", pBuffer); return Poco::trim(pBuffer); }
ssh_session sn_ssh_get_session(int verbosity) { ssh_session s = ssh_new(); if (!s) { std::cerr << "[ssh\tfail] Error getting SSH session object." << std::endl; exit(ERR_NO_SSH_SESSION); } ssh_options_set(s, SSH_OPTIONS_HOST, "segfault.party"); ssh_options_set(s, SSH_OPTIONS_USER, "bots"); ssh_options_set(s, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); return s; }
int process_target(int argc, char **argv) { ssh_session session; printf("process [%d] 0:%s 1:%s 2:%s 3:%s \n", argc, argv[0], argv[1], argv[2], argv[3] ); return 0;//+++ session = ssh_new(); ssh_callbacks_init(&cb); ssh_set_callbacks(session,&cb); if(ssh_options_getopt(session, &argc, argv)) { fprintf(stderr, "error parsing command line :%s\n", ssh_get_error(session) ); usage(); } opts(argc,argv); signal(SIGTERM, do_exit); /* Create input pipe between two endpoints */ pipe(input_pipe); #if defined(OUT_PIPE) /* Create output pipe between two endpoints */ pipe(output_pipe); #endif /* OUT_PIPE */ /* Launch Successor to push commands into tray */ iInput_Start("none", 25); #if defined(OUT_PIPE) // iOutput_Start("", 25); #endif /* OUT_PIPE */ client(session); ssh_disconnect(session); ssh_free(session); ssh_finalize(); /* Free memory occupied by dynamically stored raw data */ DeleteCmds(&pCmdChain); return 0; }
static int session_setup(void **state) { struct torture_state *s = *state; int verbosity = torture_libssh_verbosity(); s->ssh.session = ssh_new(); assert_non_null(s->ssh.session); ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); return 0; }
static void setup_rsa_key(void **state) { ssh_session session; int rc; unlink(LIBSSH_RSA_TESTKEY); unlink(LIBSSH_RSA_TESTKEY ".pub"); rc = system("ssh-keygen -t rsa -q -N \"\" -f " LIBSSH_RSA_TESTKEY); assert_true(rc == 0); session = ssh_new(); *state = session; }
static void setup_both_keys_passphrase(void **state) { ssh_session session; int rc; rc = system("ssh-keygen -t rsa -N " LIBSSH_PASSPHRASE " -f " LIBSSH_RSA_TESTKEY); assert_true(rc == 0); rc = system("ssh-keygen -t dsa -N " LIBSSH_PASSPHRASE " -f " LIBSSH_DSA_TESTKEY); assert_true(rc == 0); session = ssh_new(); *state = session; }
psession_t session_new() { psession_t p_session; p_session = malloc(sizeof(session_t)); p_session->session = ssh_new(); p_session->event = ssh_event_new(); p_session->channel = NULL; p_session->status = SESSION_WAITING; p_session->tried_time = 0; p_session->authed = false; return p_session; }
ssh_session connect_ssh(const char *host, const char *user,int verbosity) { ssh_session session; int auth=0; session=ssh_new(); if(session==NULL) return(NULL); if(user!=NULL) { if(ssh_options_set(session,SSH_OPTIONS_USER,user)<0) { ssh_free(session); return NULL; } } if(ssh_options_set(session,SSH_OPTIONS_HOST,host)<0) { ssh_free(session); return(NULL); } ssh_options_set(session,SSH_OPTIONS_LOG_VERBOSITY,&verbosity); if(ssh_connect(session)) { prterr("Connection failed : %s\n",ssh_get_error(session)); ssh_disconnect(session); ssh_free(session); return(NULL); } if(verify_knownhost(session)<0) { ssh_disconnect(session); ssh_free(session); return(NULL); } auth=authenticate_console(session); if(auth==SSH_AUTH_SUCCESS) { return(session); } else if(auth==SSH_AUTH_DENIED) { prterr("Error: Authentication failed."); } else { prterr("Error while authenticating : %s\n",ssh_get_error(session)); } ssh_disconnect(session); ssh_free(session); return(NULL); }
void tmate_ssh_server_main(struct tmate_session *session, const char *keys_dir, int port) { sigset_t sigchld_set; struct tmate_ssh_client *client = &session->ssh_client; ssh_bind bind; pid_t pid; signal(SIGSEGV, handle_sigsegv); signal(SIGCHLD, handle_sigchld); sigemptyset(&sigchld_set); sigaddset(&sigchld_set, SIGCHLD); sigprocmask(SIG_BLOCK, &sigchld_set, NULL); bind = prepare_ssh(keys_dir, port); for (;;) { client->session = ssh_new(); client->channel = NULL; client->winsize_pty.ws_col = 80; client->winsize_pty.ws_row = 24; if (!client->session) tmate_fatal("Cannot initialize session"); sigprocmask(SIG_UNBLOCK, &sigchld_set, NULL); if (ssh_bind_accept(bind, client->session) < 0) tmate_fatal("Error accepting connection: %s", ssh_get_error(bind)); sigprocmask(SIG_BLOCK, &sigchld_set, NULL); if (get_ip(ssh_get_fd(client->session), client->ip_address, sizeof(client->ip_address)) < 0) tmate_fatal("Error getting IP address from connection"); if ((pid = fork()) < 0) tmate_fatal("Can't fork"); if (pid) { tmate_info("Child spawned pid=%d, ip=%s", pid, client->ip_address); ssh_free(client->session); } else { ssh_bind_free(bind); session->session_token = "init"; client_bootstrap(session); } } }
// Connect to a SSH server. // When the connection is established, read data from stdin and send it to the server. void client_pipe(char *host, int port) { ssh_session s = ssh_new(); ssh_options_set(s, SSH_OPTIONS_HOST, host); ssh_options_set(s, SSH_OPTIONS_PORT, &port); ssh_options_set(s, SSH_OPTIONS_USER, "xya"); //ssh_options_set(s, SSH_OPTIONS_LOG_VERBOSITY_STR, "5"); if(ssh_connect(s) != SSH_OK) return session_error(s, "connect"); char *hash = pubkey_hash(ssh_get_pubkey(s)); if(authenticate(hash, 0)) { session_event(s, "authenticated", hash); free(hash); } else { free(hash); exit(1); } int keytype; ssh_string pub = publickey_from_file(s, "test-client-key.pub", &keytype); if(!pub) session_error(s, "open-public-key"); if(SSH_AUTH_SUCCESS != ssh_userauth_offer_pubkey(s, NULL, keytype, pub)) session_error(s, "offer-public-key"); ssh_private_key priv = privatekey_from_file(s, "test-client-key", keytype, NULL); if(!priv) session_error(s, "open-private-key"); if(SSH_AUTH_SUCCESS != ssh_userauth_pubkey(s, NULL, pub, priv)) session_error(s, "user-auth"); string_free(pub); privatekey_free(priv); ssh_channel chan = channel_new(s); if(!chan) session_error(s, "create-channel"); if(channel_open_session(chan) < 0) session_error(s, "open-channel"); session_event(s, "channel-opened", NULL); channel_from_file(chan, 0); channel_free(chan); ssh_disconnect(s); ssh_finalize(); }
int test() { ssh_session my_ssh_session; int rc; // Open session and set options my_ssh_session = ssh_new(); if (my_ssh_session == NULL) exit(-1); ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "localhost"); // Connect to server rc = ssh_connect(my_ssh_session); if (rc != SSH_OK) { fprintf(stderr, "Error connecting to localhost: %s\n", ssh_get_error(my_ssh_session)); ssh_free(my_ssh_session); exit(-1); } // Verify the server's identity // For the source code of verify_knowhost(), check previous example // if (verify_knownhost(my_ssh_session) < 0) // { // ssh_disconnect(my_ssh_session); // ssh_free(my_ssh_session); // exit(-1); // } // Authenticate ourselves QString password("whcrosedu"); rc = ssh_userauth_password(my_ssh_session, NULL, password.toLatin1()); if (rc != SSH_AUTH_SUCCESS) { fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(my_ssh_session)); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); exit(-1); } qDebug()<<"evrika"; show_remote_processes(my_ssh_session); ssh_disconnect(my_ssh_session); ssh_free(my_ssh_session); }