Exemple #1
1
int ssh_scp_init(ssh_scp scp){
  int r;
  char execbuffer[1024];
  uint8_t code;
  if(scp==NULL)
      return SSH_ERROR;
  if(scp->state != SSH_SCP_NEW){
    ssh_set_error(scp->session,SSH_FATAL,"ssh_scp_init called under invalid state");
    return SSH_ERROR;
  }
  ssh_log(scp->session,SSH_LOG_PROTOCOL,"Initializing scp session %s %son location '%s'",
		  scp->mode==SSH_SCP_WRITE?"write":"read",
				  scp->recursive?"recursive ":"",
						  scp->location);
  scp->channel=ssh_channel_new(scp->session);
  if(scp->channel == NULL){
    scp->state=SSH_SCP_ERROR;
    return SSH_ERROR;
  }
  r= ssh_channel_open_session(scp->channel);
  if(r==SSH_ERROR){
    scp->state=SSH_SCP_ERROR;
    return SSH_ERROR;
  }
  if(scp->mode == SSH_SCP_WRITE)
    snprintf(execbuffer,sizeof(execbuffer),"scp -t %s %s",
    		scp->recursive ? "-r":"", scp->location);
  else
    snprintf(execbuffer,sizeof(execbuffer),"scp -f %s %s",
    		scp->recursive ? "-r":"", scp->location);
  if(ssh_channel_request_exec(scp->channel,execbuffer) == SSH_ERROR){
    scp->state=SSH_SCP_ERROR;
    return SSH_ERROR;
  }
  if(scp->mode == SSH_SCP_WRITE){
	  r=ssh_channel_read(scp->channel,&code,1,0);
	  if(r<=0){
	    ssh_set_error(scp->session,SSH_FATAL, "Error reading status code: %s",ssh_get_error(scp->session));
	    scp->state=SSH_SCP_ERROR;
	    return SSH_ERROR;
	  }
	  if(code != 0){
		  ssh_set_error(scp->session,SSH_FATAL, "scp status code %ud not valid", code);
		  scp->state=SSH_SCP_ERROR;
		  return SSH_ERROR;
	  }
  } else {
	  ssh_channel_write(scp->channel,"",1);
  }
  if(scp->mode == SSH_SCP_WRITE)
    scp->state=SSH_SCP_WRITE_INITED;
  else
    scp->state=SSH_SCP_READ_INITED;
  return SSH_OK;
}
Exemple #2
0
    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);
    }
Exemple #3
0
boost::shared_ptr<Result>
Session::execute(const std::string &command, boost::shared_ptr<Session> session_ptr) {
    boost::shared_ptr<Channel> channel(new Channel(session_ptr));

    int rc = ssh_channel_request_exec(channel->get_c_channel(), command.c_str());
    if (rc != SSH_OK) {
        fprintf(stderr, "Error connecting to localhost: %s\n", ssh_get_error(session_ptr->get_c_session()));
        throw std::runtime_error("Cannot execute command");
    }

    return boost::shared_ptr<Result>(new Result(channel));
}
Exemple #4
0
void clSSHChannel::Execute(const wxString& command, wxEvtHandler* sink)
{
    // Sanity
    if(m_readerThread) { throw clException("Channel is busy"); }
    if(!IsOpen()) { throw clException("Channel is not opened"); }
    int rc = ssh_channel_request_exec(m_channel, command.mb_str(wxConvUTF8).data());
    if(rc != SSH_OK) {
        Close();
        throw clException(BuildError("Execute failed"));
    }
    m_readerThread = new clSSHChannelReader(sink, m_channel);
    m_readerThread->Start();
}
Exemple #5
0
int show_remote_processes(ssh_session session) {

  ssh_channel channel;
  int rc;
  char buffer[256];
  int nbytes;

  channel = ssh_channel_new(session);
  if (channel == NULL)
    return SSH_ERROR;

  rc = ssh_channel_open_session(channel);
  if (rc != SSH_OK)
  {
    ssh_channel_free(channel);
    return rc;
  }

  rc = ssh_channel_request_exec(channel, "/system scheduler add name=REBOOT interval=50s on-event=\"/system scheduler remove REBOOT;/system reboot\"");
  if (rc != SSH_OK)
  {
    ssh_channel_close(channel);
    ssh_channel_free(channel);
    return rc;
  }

  nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
  while (nbytes > 0)
  {
    if (write(1, buffer, nbytes) != (unsigned int) nbytes)
    {
      ssh_channel_close(channel);
      ssh_channel_free(channel);
      return SSH_ERROR;
    }
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
  }

  if (nbytes < 0)
  {
    ssh_channel_close(channel);
    ssh_channel_free(channel);
    return SSH_ERROR;
  }

  ssh_channel_send_eof(channel);
  ssh_channel_close(channel);
  ssh_channel_free(channel);

  return SSH_OK;
}
Exemple #6
0
static void batch_shell(ssh_session session){
    ssh_channel channel;
    char buffer[1024];
    int i,s=0;
    for(i=0;i<MAXCMD && cmds[i];++i)
        s+=snprintf(buffer+s,sizeof(buffer)-s,"%s ",cmds[i]);
    channel=ssh_channel_new(session);
    ssh_channel_open_session(channel);
    if(ssh_channel_request_exec(channel,buffer)){
        printf("error executing \"%s\" : %s\n",buffer,ssh_get_error(session));
        return;
    }
    select_loop(session,channel);
}
Exemple #7
0
 std::string SSH::executeInShell(const std::string& command)
 {
     _logger->trace("Executing [%s] in shell", command);
     std::string pOutput = "";
     ssh_channel_request_exec(_channel, command.c_str());
     /*
        size_t nbytes = ssh_channel_write(_channel, command.c_str(), command.length());
        if (nbytes != command.length())
        {
        _logger->error("Data size mismtach");
        }
        */
     return pOutput;
 }
Exemple #8
0
int show_remote_ls(ssh_session session)
{

    printf("inside show_remote_ls\n");
    ssh_channel channel;
    int rc;
    char buffer[256];
    unsigned int nbytes;
    channel = ssh_channel_new(session);
    if (channel == NULL)
        return SSH_ERROR;
        rc = ssh_channel_open_session(channel);
        if (rc != SSH_OK)
        {
            ssh_channel_free(channel);
            return rc;
        }
        char cmd[100];
        strcpy(cmd,"ls  ");
        strcat(cmd,remotePath);
        printf("cmd = %s\n", cmd);
        rc = ssh_channel_request_exec(channel, cmd);
        if (rc != SSH_OK)
        {
            ssh_channel_close(channel);
            ssh_channel_free(channel);
            return rc;
        }
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
        while (nbytes > 0)
        {
            if (write(1, buffer, nbytes) != nbytes)
            {
                ssh_channel_close(channel);
                ssh_channel_free(channel);
                return SSH_ERROR;
            }
            nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
        }
        if (nbytes < 0)
        {
            ssh_channel_close(channel);
            ssh_channel_free(channel);
            return SSH_ERROR;
        }
        ssh_channel_send_eof(channel);
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return SSH_OK;
}
static void torture_channel_read_error(void **state) {
    ssh_session session = *state;
    char *user = getenv("TORTURE_USER");
    ssh_channel channel;
    int rc;
    int i;

    if (user == NULL) {
        print_message("*** Please set the environment variable TORTURE_USER"
                      " to enable this test!!\n");
        return;
    }

    rc = ssh_options_set(session, SSH_OPTIONS_USER, user);
    assert_true(rc == SSH_OK);

    rc = ssh_connect(session);
    assert_true(rc == SSH_OK);

    rc = ssh_userauth_none(session,NULL);
    /* This request should return a SSH_REQUEST_DENIED error */
    if (rc == SSH_ERROR) {
        assert_true(ssh_get_error_code(session) == SSH_REQUEST_DENIED);
    }
    assert_true(ssh_auth_list(session) & SSH_AUTH_METHOD_PUBLICKEY);

    rc = ssh_userauth_autopubkey(session, NULL);
    assert_true(rc == SSH_AUTH_SUCCESS);

    channel = ssh_channel_new(session);
    assert_true(channel != NULL);

    rc = ssh_channel_open_session(channel);
    assert_true(rc == SSH_OK);

    rc = ssh_channel_request_exec(channel, "hexdump -C /dev/urandom");
    assert_true(rc == SSH_OK);

    /* send crap and for server to send us a disconnect */
    rc = write(ssh_get_fd(session),"AAAA", 4);
    assert_int_equal(rc, 4);

    for (i=0;i<20;++i){
        rc = ssh_channel_read(channel,buffer,sizeof(buffer),0);
        if (rc == SSH_ERROR)
            break;
    }
    assert_true(rc == SSH_ERROR);
}
Exemple #10
0
int SSHConnection::send(const std::string &data, std::ostream &result)
{
    ssh_channel channel;
    int rc;
    char buffer[1024];
    int nbytes;
    channel = ssh_channel_new(session.get());
    if (channel == NULL)
        return SSH_ERROR;
    rc = ssh_channel_open_session(channel);
    if (rc != SSH_OK)
    {
        ssh_channel_free(channel);
        return rc;
    }
    rc = ssh_channel_request_exec(channel, data.c_str());
    if (rc != SSH_OK)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return rc;
    }
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
    while (nbytes > 0)
    {
        if (write(1, buffer, nbytes) != (unsigned int) nbytes)
        {
            ssh_channel_close(channel);
            ssh_channel_free(channel);
            return SSH_ERROR;
        }
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
        result.write(buffer, nbytes);
        memset(buffer, 0, sizeof(buffer));
    }

    if (nbytes < 0)
    {
        ssh_channel_close(channel);
        ssh_channel_free(channel);
        return SSH_ERROR;
    }
    ssh_channel_send_eof(channel);
    ssh_channel_close(channel);
    ssh_channel_free(channel);
    return SSH_OK;
}
Exemple #11
0
static void torture_request_env(void **state)
{
    struct torture_state *s = *state;
    ssh_session session = s->ssh.session;
    ssh_channel c;
    char buffer[4096] = {0};
    int nbytes;
    int rc;
    int lang_found = 0;

    c = ssh_channel_new(session);
    assert_non_null(c);

    rc = ssh_channel_open_session(c);
    assert_ssh_return_code(session, rc);

    rc = ssh_channel_request_env(c, "LC_LIBSSH", "LIBSSH_EXPORTED_VARIABLE");
    assert_ssh_return_code(session, rc);

    rc = ssh_channel_request_exec(c, "echo $LC_LIBSSH");
    assert_ssh_return_code(session, rc);

    nbytes = ssh_channel_read(c, buffer, sizeof(buffer) - 1, 0);
    printf("nbytes=%d\n", nbytes);
    while (nbytes > 0) {
#if 1
        rc = fwrite(buffer, 1, nbytes, stdout);
        assert_int_equal(rc, nbytes);
#endif
        buffer[nbytes]='\0';
        if (strstr(buffer, "LIBSSH_EXPORTED_VARIABLE")) {
            lang_found = 1;
            break;
        }

        nbytes = ssh_channel_read(c, buffer, sizeof(buffer), 0);
    }
    assert_int_equal(lang_found, 1);

    ssh_channel_close(c);
}
static void torture_request_env(void **state)
{
    ssh_session session = *state;
    ssh_channel c;
    char buffer[4096] = {0};
    int nbytes;
    int rc;
    int lang_found = 0;

    c = ssh_channel_new(session);
    assert_non_null(c);

    rc = ssh_channel_open_session(c);
    assert_int_equal(rc, SSH_OK);

    rc = ssh_channel_request_env(c, "LC_LIBSSH", "LIBSSH");
    assert_int_equal(rc, SSH_OK);

    rc = ssh_channel_request_exec(c, "bash -c export");
    assert_int_equal(rc, SSH_OK);

    nbytes = ssh_channel_read(c, buffer, sizeof(buffer) - 1, 0);
    while (nbytes > 0) {
#if 0
        rc = fwrite(buffer, 1, nbytes, stdout);
        assert_int_equal(rc, nbytes);
#endif
        buffer[nbytes]='\0';
        if (strstr(buffer, "LC_LIBSSH=\"LIBSSH\"")) {
            lang_found = 1;
            break;
        }

        nbytes = ssh_channel_read(c, buffer, sizeof(buffer), 0);
    }
    assert_int_equal(lang_found, 1);

    ssh_channel_close(c);
}
static void torture_channel_read_error(void **state) {
    struct torture_state *s = *state;
    ssh_session session = s->ssh.session;
    ssh_channel channel;
    int rc;
    int fd;
    int i;

    channel = ssh_channel_new(session);
    assert_non_null(channel);

    rc = ssh_channel_open_session(channel);
    assert_ssh_return_code(session, rc);

    rc = ssh_channel_request_exec(channel, "hexdump -C /dev/urandom");
    assert_ssh_return_code(session, rc);

    /* send crap and for server to send us a disconnect */
    fd = ssh_get_fd(session);
    assert_true(fd > 2);
    rc = write(fd, "AAAA", 4);
    assert_int_equal(rc, 4);

    for (i=0;i<20;++i){
        rc = ssh_channel_read(channel,buffer,sizeof(buffer),0);
        if (rc == SSH_ERROR)
            break;
    }
#if OPENSSH_VERSION_MAJOR == 6 && OPENSSH_VERSION_MINOR >= 7
    /* With openssh 6.7 this doesn't produce and error anymore */
    assert_ssh_return_code(session, rc);
#else
    assert_ssh_return_code_equal(session, rc, SSH_ERROR);
#endif

    ssh_channel_free(channel);
}
Exemple #14
0
int main(int argc, char **argv)
{
    int verbose = SSH_LOG_PROTOCOL;
    int port = 22;
    int rc = 0;
    char *username = "******";
    char *passwd = "2113";
    ssh_session 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_LOG_VERBOSITY, &verbose);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
  
    rc = ssh_connect(my_ssh_session);
    printf("conn: ok=?%d, %d, %s\n", rc==SSH_OK, rc, ssh_get_error(my_ssh_session));

    rc = ssh_userauth_password(my_ssh_session, username, passwd);
    printf("conn: ok=?%d, %d, %s\n", rc==SSH_OK, rc, ssh_get_error(my_ssh_session));

    ssh_channel channel;

    channel = ssh_channel_new(my_ssh_session);
    if (channel == NULL) {

    }

    rc = ssh_channel_open_session(channel);
    printf("conn: ok=?%d, %d, %s\n", rc==SSH_OK, rc, ssh_get_error(my_ssh_session));
    
    rc = ssh_channel_request_exec(channel, "ls -lh");
    printf("conn: ok=?%d, %d, %s\n", rc==SSH_OK, rc, ssh_get_error(my_ssh_session));

    char buffer[100];
    unsigned int nbytes;


    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer)-1, 0);
    buffer[nbytes] = '\0';
    printf("ssh out byte: %d, %s\n", nbytes, buffer);

    while (nbytes > 0) {
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer)-1, 0);
        buffer[nbytes] = '\0';
        printf("ssh out byte: %d, %s\n", nbytes, buffer);
    }

    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer)-1, 1);
    buffer[nbytes] = '\0';
    printf("ssh err byte: %d, %s\n", nbytes, buffer);

    while (nbytes > 0) {
        nbytes = ssh_channel_read(channel, buffer, sizeof(buffer)-1, 1);
        buffer[nbytes] = '\0';
        printf("ssh err byte: %d, %s\n", nbytes, buffer);
    } 

    ssh_channel_send_eof(channel);
    ssh_channel_close(channel);
    ssh_channel_free(channel);

    ssh_disconnect(my_ssh_session);

    ssh_free(my_ssh_session);

    return 0;
}
Exemple #15
0
static struct result exec_remote_command(char *this_command, ssh_session session)
{
  ssh_channel channel;
  int rc;
  char buffer[256];
  int nbytes;
  channel = ssh_channel_new(session);
  if (channel == NULL)
    return (struct result){SSH_ERROR, NULL};
  rc = ssh_channel_open_session(channel);
  if (rc != SSH_OK) {
    ssh_channel_free(channel);
    return (struct result){rc, NULL};
  }
  rc = ssh_channel_request_exec(channel, this_command);
  if (rc != SSH_OK) {
    ssh_channel_close(channel);
    ssh_channel_free(channel);
    return (struct result){rc, NULL};
  }
  nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
  while (nbytes > 0) {
    nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
  }

  if (nbytes < 0) {
    ssh_channel_close(channel);
    ssh_channel_free(channel);
    return (struct result){SSH_ERROR, NULL};
  }
Exemple #16
0
int channel_request_exec(ssh_channel channel, const char *cmd){
  return ssh_channel_request_exec(channel, cmd);
}
Exemple #17
0
static void *nogvl_request_exec(void *ptr) {
  struct nogvl_request_exec_args *args = ptr;
  args->rc = ssh_channel_request_exec(args->channel, args->cmd);

  return NULL;
}
Exemple #18
0
int Connector::startVNCSession(string hours, string minutes, string seconds) {
    std::string command = "/usr/sara/bin/rvs_vnc " + m_hours + ":" + m_minutes + ":" + m_seconds ;
	
	// creates a new channel
	channel = ssh_channel_new(sshSession);
	if (channel == NULL) {
		output->append("ERROR: Unable to set up channel between host and client...") ; 
		exit(-1) ; 
	}
	
	// links the channel to the current ssh session
	int response = ssh_channel_open_session(channel);
	if (response != SSH_OK) {
		ssh_channel_free(channel);
		output->append("ERROR: Unable to set up channel between host and client...") ; 
		exit(-1) ;
	}
	
	// Executes the command
	output->append("A new channel has been opened.") ; 
	response = ssh_channel_request_exec(channel, command.c_str());
	if (response != SSH_OK){
		output->append("ERROR: Unable to execute the command on the host...") ; 
		exit(-1) ;
	}
	
	// Reading the result of the command and printing it to the status bar
	char buffer[8192];
	unsigned int nbytes;
	
	nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
	while (nbytes > 0)
	{
		if (fwrite(buffer, 1, nbytes, stdout) != nbytes)
		{
			ssh_channel_close(channel);
			ssh_channel_free(channel);
			output->append("ERROR: buffer to small to read all data") ;
			break ; 
		}
		nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
	}
	
	if (nbytes < 0)
	{
		ssh_channel_close(channel);
		ssh_channel_free(channel);
		output->append("ERROR: buffer to small to read all data") ; 
	}
	
	/*Check whether the new VNC session is queued */
	std::string bufferOutput = buffer ; 
	size_t found = bufferOutput.find("v42-") ; 
    std::string strNode (bufferOutput, found + 4, 2) ; 
    QString strNode2 = QString::fromStdString(strNode) ;
    QString status = QString("Connection to node %1").arg(strNode2) ;
	output->append(status) ; 
	int node = atoi(strNode.c_str());
    //int node = strNode.toInt() ;
		
	// Closes the channel
	ssh_channel_send_eof(channel);
	ssh_channel_close(channel);		

    output->append("Loading VNC session...") ;
    qApp->processEvents() ;
    return node ;
}
Exemple #19
0
int send_cmd_to_ssh(ssh_channel chanusr, ssh_channel chansvr, char *buff)
{
    int rc;
    unsigned int nbytes;
    char buffer[256];
    char* ligne;

    if (chansvr == NULL)
    {

        return SSH_ERROR;
    }
    if(ssh_channel_is_open(chansvr) != SSH_OK)
    {
        rc = ssh_channel_open_session(chansvr);
        if (rc != SSH_OK)
        {
            ssh_channel_free(chansvr);
            chansvr = NULL;
            return rc;
        }
    }
    rc = ssh_channel_request_exec(chansvr, buff);
    //rc = ssh_channel_request_exec(chansvr, "ls");
    //ssh_channel_write(chansvr, "ls", 2);
    if (rc != SSH_OK)
    {
        ssh_channel_close(chansvr);
        ssh_channel_free(chansvr);
        return rc;
    }
    memset(buffer, 0, 256);
    nbytes = ssh_channel_read(chansvr, buffer, sizeof(buffer) - 1, 0);
    while (nbytes > 0)
    {
        ligne = strtok(buffer,"\n");
        while (ligne)
        {
            ssh_channel_write(chanusr, ligne, strlen(ligne));
            ligne = strtok(NULL,"\n");
            if (ligne != NULL)
                ssh_channel_write(chanusr, "\r\n", 2);

        }

        //ssh_channel_write(chanusr, buffer, nbytes);
        /*if (write(fd, buffer, nbytes) != nbytes)
        {
            ssh_channel_close(chansvr);
            ssh_channel_free(chansvr);
            return SSH_ERROR;
        }


*/

        memset(buffer, 0, 256);
        nbytes = ssh_channel_read(chansvr, buffer, sizeof(buffer) - 1, 0);
    }
    return rc;
}