예제 #1
0
파일: SSH.cpp 프로젝트: subutai-io/launcher
    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;
    }
예제 #2
0
void clSSH::DoOpenChannel() throw(clException)
{
    if(m_channel) return;

    m_channel = ssh_channel_new(m_session);
    if(!m_channel) {
        throw clException(ssh_get_error(m_session));
    }

    int rc = ssh_channel_open_session(m_channel);
    if(rc != SSH_OK) {
        throw clException(ssh_get_error(m_session));
    }

    rc = ssh_channel_request_pty(m_channel);
    if(rc != SSH_OK) {
        throw clException(ssh_get_error(m_session));
    }

    rc = ssh_channel_change_pty_size(m_channel, 80, 24);
    if(rc != SSH_OK) {
        throw clException(ssh_get_error(m_session));
    }

    rc = ssh_channel_request_shell(m_channel);
    if(rc != SSH_OK) {
        throw clException(ssh_get_error(m_session));
    }
}
예제 #3
0
파일: ssh.c 프로젝트: isdrupter/busybotnet
static void shell(ssh_session session){
    ssh_channel channel;
    struct termios terminal_local;
    int interactive=isatty(0);
    channel = ssh_channel_new(session);
    if(interactive){
        tcgetattr(0,&terminal_local);
        memcpy(&terminal,&terminal_local,sizeof(struct termios));
    }
    if(ssh_channel_open_session(channel)){
        printf("error opening channel : %s\n",ssh_get_error(session));
        return;
    }
    chan=channel;
    if(interactive){
        ssh_channel_request_pty(channel);
        sizechanged();
    }
    if(ssh_channel_request_shell(channel)){
        printf("Requesting shell : %s\n",ssh_get_error(session));
        return;
    }
    if(interactive){
        cfmakeraw(&terminal_local);
        tcsetattr(0,TCSANOW,&terminal_local);
        setsignal();
    }
    signal(SIGTERM,do_cleanup);
    select_loop(session,channel);
    if(interactive)
    	do_cleanup(0);
}
예제 #4
0
static ssh_channel channel_open_request_cb(ssh_session session, void *userdata)
{
	struct tmate_ssh_client *client = userdata;

	if (!client->username) {
		/* The authentication did not go through yet */
		return NULL;
	}

	if (client->channel) {
		/*
		 * We already have a channel, and we don't support multi
		 * channels yet. Returning NULL means the channel request will
		 * be denied.
		 */
		return NULL;
	}

	client->channel = ssh_channel_new(session);
	if (!client->channel)
		tmate_fatal("Error getting channel");

	memset(&client->channel_cb, 0, sizeof(client->channel_cb));
	ssh_callbacks_init(&client->channel_cb);
	client->channel_cb.userdata = client;
	client->channel_cb.channel_pty_request_function = pty_request;
	client->channel_cb.channel_shell_request_function = shell_request;
	client->channel_cb.channel_subsystem_request_function = subsystem_request;
	client->channel_cb.channel_exec_request_function = exec_request;
	ssh_set_channel_callbacks(client->channel, &client->channel_cb);

	return client->channel;
}
예제 #5
0
static const gchar *
cockpit_ssh_connect (CockpitSshData *data)
{
  const gchar *problem;
  int rc;

  /*
   * If connect_done is set prematurely by another thread then the
   * connection attempt was cancelled.
   */

  rc = ssh_connect (data->session);
  if (rc != SSH_OK)
    {
      if (g_atomic_int_get (data->connecting))
        g_message ("%s: couldn't connect: %s", data->logname,
                   ssh_get_error (data->session));
      return "no-host";
    }

  g_debug ("%s: connected", data->logname);

  if (!data->ignore_key)
    {
      problem = verify_knownhost (data);
      if (problem != NULL)
        return problem;
    }

  /* The problem returned when auth failure */
  problem = cockpit_ssh_authenticate (data);
  if (problem != NULL)
    return problem;

  data->channel = ssh_channel_new (data->session);
  g_return_val_if_fail (data->channel != NULL, NULL);

  rc = ssh_channel_open_session (data->channel);
  if (rc != SSH_OK)
    {
      if (g_atomic_int_get (data->connecting))
        g_message ("%s: couldn't open session: %s", data->logname,
                   ssh_get_error (data->session));
      return "internal-error";
    }

  rc = ssh_channel_request_exec (data->channel, data->command);
  if (rc != SSH_OK)
    {
      if (g_atomic_int_get (data->connecting))
        g_message ("%s: couldn't execute command: %s: %s", data->logname,
                   data->command, ssh_get_error (data->session));
      return "internal-error";
    }

  g_debug ("%s: opened channel", data->logname);

  /* Success */
  return NULL;
}
예제 #6
0
파일: ssh.cpp 프로젝트: obklar/exiv2
 int SSH::runCommand(const std::string& cmd, std::string* output) {
     int rc;
     ssh_channel channel;
     channel = ssh_channel_new(session_);
     if (channel == NULL) {
        rc = SSH_ERROR;
     } else {
         rc = ssh_channel_open_session(channel);
         if (rc != SSH_OK) {
             ssh_channel_free(channel);
         } else {
             char buffer[256];
             rc = ssh_channel_request_exec(channel, cmd.c_str());
             if (rc == SSH_OK) {
                 while ((rc = ssh_channel_read(channel, buffer, sizeof(buffer), 0)) > 0) {
                     output->append(buffer, rc);
                 }
             }
             ssh_channel_send_eof(channel);
             ssh_channel_close(channel);
             ssh_channel_free(channel);
         }
     }
     return rc;
 }
예제 #7
0
/** @internal
 * @brief Calculates the RTT of the host with SSH channel operations, and
 * returns the average of the calculated RTT.
 * @param[in] session active SSH session to test.
 * @param[out] average average RTT in milliseconds.
 * @returns 0 on success, -1 if there is an error.
 */
int benchmarks_ssh_latency(ssh_session session, float *average){
  float times[3];
  struct timestamp_struct ts;
  int i;
  ssh_channel channel;
  channel=ssh_channel_new(session);
  if(channel==NULL)
    goto error;
  if(ssh_channel_open_session(channel)==SSH_ERROR)
    goto error;

  for(i=0;i<3;++i){
    timestamp_init(&ts);
    if(ssh_channel_request_env(channel,"TEST","test")==SSH_ERROR &&
        ssh_get_error_code(session)==SSH_FATAL)
      goto error;
    times[i]=elapsed_time(&ts);
  }
  ssh_channel_close(channel);
  ssh_channel_free(channel);
  channel=NULL;
  printf("Times : %f ms ; %f ms ; %f ms\n", times[0], times[1], times[2]);
  *average=(times[0]+times[1]+times[2])/3;
  return 0;
error:
  fprintf(stderr,"Error calculating SSH latency : %s\n",ssh_get_error(session));
  if(channel)
    ssh_channel_free(channel);
  return -1;
}
예제 #8
0
static ssh_channel pkd_channel_openreq_cb(ssh_session s,
                                          void *userdata) {
    ssh_channel c = NULL;
    ssh_channel *out = (ssh_channel *) userdata;

    /* assumes pubkey authentication has already succeeded */
    pkdout("pkd_channel_openreq_cb\n");

    c = ssh_channel_new(s);
    if (c == NULL) {
        pkderr("ssh_channel_new: %s\n", ssh_get_error(s));
        return NULL;
    }

    ssh_callbacks_init(&pkd_channel_cb);
    pkd_channel_cb.userdata = userdata;
    if (ssh_set_channel_callbacks(c, &pkd_channel_cb) != SSH_OK) {
        pkderr("ssh_set_channel_callbacks: %s\n", ssh_get_error(s));
        ssh_channel_free(c);
        c = NULL;
    }

    *out = c;

    return c;
}
예제 #9
0
static void *client_thread(void *arg) {
    unsigned int test_port = TEST_SERVER_PORT;
    int rc;
    ssh_session session;
    ssh_channel channel;

    /* unused */
    (void)arg;

    usleep(200);
    session = torture_ssh_session("localhost",
                                  &test_port,
                                  "foo", "bar");
    assert_non_null(session);

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

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

    rc = ssh_channel_request_x11(channel, 0, NULL, NULL,
                                 (uint32_t)x11_screen_number);
    assert_int_equal(rc, SSH_OK);

    ssh_free(session);
    return NULL;
}
예제 #10
0
static ssh_channel run_ssh_command(ssh_session sshs, const char* capture_bin, const char* iface, const char* cfilter,
		unsigned long int count)
{
	gchar* cmdline;
	ssh_channel channel;
	char* quoted_bin;
	char* quoted_iface;
	char* default_filter;
	char* quoted_filter;
	char* count_str = NULL;
	unsigned int remote_port = 22;

	if (!capture_bin)
		capture_bin = DEFAULT_CAPTURE_BIN;

	if (!iface)
		iface = "eth0";

	channel = ssh_channel_new(sshs);
	if (!channel)
		return NULL;

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

	ssh_options_get_port(sshs, &remote_port);

	/* escape parameters to go save with the shell */
	quoted_bin = g_shell_quote(capture_bin);
	quoted_iface = g_shell_quote(iface);
	default_filter = local_interfaces_to_filter(remote_port);
	if (!cfilter)
		cfilter = default_filter;
	quoted_filter = g_shell_quote(cfilter);
	if (count > 0)
		count_str = g_strdup_printf("-c %lu", count);

	cmdline = g_strdup_printf("%s -i %s -P -w - -f %s %s", quoted_bin, quoted_iface, quoted_filter,
		count_str ? count_str : "");

	verbose_print("Running: %s\n", cmdline);
	if (ssh_channel_request_exec(channel, cmdline) != SSH_OK) {
		ssh_channel_close(channel);
		ssh_channel_free(channel);
		channel = NULL;
	}

	g_free(quoted_bin);
	g_free(quoted_iface);
	g_free(default_filter);
	g_free(quoted_filter);
	g_free(cmdline);
	if (count_str)
		g_free(count_str);

	return channel;
}
예제 #11
0
int new_ssh_channel() {
  channel = ssh_channel_new(my_ssh_session);
  if (channel == NULL) {
    fprintf(stderr, "Error creating ssh channel\n");
    return SSH_ERROR;
  }
  return SSH_OK;
}
예제 #12
0
/* TODO: make this function accept a ssh_channel */
ssh_channel ssh_message_channel_request_open_reply_accept(ssh_message msg) {
  ssh_session session;
  ssh_channel chan = NULL;

  enter_function();

  if (msg == NULL) {
    leave_function();
    return NULL;
  }

  session = msg->session;

  chan = ssh_channel_new(session);
  if (chan == NULL) {
    leave_function();
    return NULL;
  }

  chan->local_channel = ssh_channel_new_id(session);
  chan->local_maxpacket = 35000;
  chan->local_window = 32000;
  chan->remote_channel = msg->channel_request_open.sender;
  chan->remote_maxpacket = msg->channel_request_open.packet_size;
  chan->remote_window = msg->channel_request_open.window;
  chan->state = SSH_CHANNEL_STATE_OPEN;

  if (buffer_add_u8(session->out_buffer, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION) < 0) {
    goto error;
  }
  if (buffer_add_u32(session->out_buffer, htonl(chan->remote_channel)) < 0) {
    goto error;
  }
  if (buffer_add_u32(session->out_buffer, htonl(chan->local_channel)) < 0) {
    goto error;
  }
  if (buffer_add_u32(session->out_buffer, htonl(chan->local_window)) < 0) {
    goto error;
  }
  if (buffer_add_u32(session->out_buffer, htonl(chan->local_maxpacket)) < 0) {
    goto error;
  }

  ssh_log(session, SSH_LOG_PACKET,
      "Accepting a channel request_open for chan %d", chan->remote_channel);

  if (packet_send(session) == SSH_ERROR) {
    goto error;
  }

  leave_function();
  return chan;
error:
  ssh_channel_free(chan);

  leave_function();
  return NULL;
}
예제 #13
0
파일: scp.c 프로젝트: Paxxi/libssh
/**
 * @brief Initialize the scp channel.
 *
 * @param[in]  scp      The scp context to initialize.
 *
 * @return SSH_OK on success or an SSH error code.
 *
 * @see ssh_scp_new()
 */
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(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;
}
예제 #14
0
static void forwarding(ssh_session session){
    ssh_channel channel;
    int r;
    channel = ssh_channel_new(session);
    r = ssh_channel_open_forward(channel, desthost, atoi(port), "localhost", 22);
    if(r<0) {
        printf("error forwarding port : %s\n",ssh_get_error(session));
        return;
    }
    select_loop(session,channel);
}
예제 #15
0
static ssh_channel new_session_channel(ssh_session session, void *userdata){
    (void) session;
    (void) userdata;
    if(chan != NULL)
        return NULL;
    printf("Allocated session channel\n");
    chan = ssh_channel_new(session);
    ssh_callbacks_init(&channel_cb);
    ssh_set_channel_callbacks(chan, &channel_cb);
    return chan;
}
예제 #16
0
파일: channel.c 프로젝트: sorah/libssh-ruby
static VALUE m_initialize(VALUE self, VALUE session) {
  ChannelHolder *holder;
  SessionHolder *session_holder;

  TypedData_Get_Struct(self, ChannelHolder, &channel_type, holder);
  session_holder = libssh_ruby_session_holder(session);
  holder->channel = ssh_channel_new(session_holder->session);
  holder->session = session;

  return self;
}
예제 #17
0
파일: full.c 프로젝트: birdatdotty/webutils
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;
}
예제 #18
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);
}
예제 #19
0
void clSSHChannel::Open()
{
    if(IsOpen()) { return; }
    if(!m_ssh) { throw clException("ssh session is not opened"); }
    m_channel = ssh_channel_new(m_ssh->GetSession());
    if(!m_channel) { throw clException(BuildError("Failed to allocte ssh channel")); }

    int rc = ssh_channel_open_session(m_channel);
    if(rc != SSH_OK) {
        ssh_channel_free(m_channel);
        m_channel = NULL;
        throw clException(BuildError("Failed to open ssh channel"));
    }
}
예제 #20
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};
  }
예제 #21
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);
}
예제 #23
0
ssh_channel open_client_channel(ssh_session session)
{
    ssh_channel channel;
    int rc;

    channel = ssh_channel_new(session);
    if (channel == NULL)
        return NULL;
    rc = ssh_channel_open_session(channel);
    if (rc != SSH_OK)
    {
        ssh_channel_free(channel);
        return NULL;
    }
    return (channel);
}
예제 #24
0
static ssh_channel channel_open(ssh_session session, void *userdata) {
    ssh_channel channel = NULL;
    ssh_channel_callbacks channel_cb = userdata;

    /* unused */
    (void)userdata;

    channel = ssh_channel_new(session);
    if (channel == NULL) {
        goto out;
    }
    ssh_set_channel_callbacks(channel, channel_cb);

 out:
    return channel;
}
예제 #25
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;
}
예제 #26
0
파일: ssh_server.c 프로젝트: seem8/friendup
static ssh_channel new_session_channel( ssh_session session, void *userdata )
{
	SSHSession *s = (SSHSession *)userdata;
	(void) session;
	
	if( s->sshs_Chan != NULL)
	{
		ERROR("New session channel\n");
		return NULL;
	}
	printf("Allocated session channel\n");
	s->sshs_Chan = ssh_channel_new( session );
	ssh_callbacks_init( &channel_cb );
	ssh_set_channel_callbacks( s->sshs_Chan, &channel_cb );
	
	return s->sshs_Chan;
}
예제 #27
0
파일: messages.c 프로젝트: magnum/tmate
ssh_channel ssh_message_channel_request_open_reply_accept(ssh_message msg) {
	ssh_channel chan;
	int rc;

	if (msg == NULL) {
	    return NULL;
	}

	chan = ssh_channel_new(msg->session);
	if (chan == NULL) {
		return NULL;
	}
	rc = ssh_message_channel_request_open_reply_accept_channel(msg, chan);
	if (rc < 0) {
		ssh_channel_free(chan);
		chan = NULL;
	}
	return chan;

}
예제 #28
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);
}
예제 #29
0
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);
}
예제 #30
0
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);
}