Пример #1
2
int SSH2Utils::exec(const char *cmd) {
	m_execResultStr.clear();
	int rc = -1;
	LIBSSH2_CHANNEL *channel;
	/* Exec non-blocking on the remove host */
	while ((channel = libssh2_channel_open_session(m_session)) == NULL
			&& libssh2_session_last_error(m_session, NULL, NULL, 0)
					== LIBSSH2_ERROR_EAGAIN) {
		waitsocket();
	}
	if (channel == NULL) {
		fprintf(stderr, "Channel Error\n");
		m_errCode = 9;
		return -1;
	}

    /* Request a terminal with 'vanilla' terminal emulation
     * See /etc/termcap for more options
     */
    if (libssh2_channel_request_pty(channel, "vanilla")) {
        fprintf(stderr, "Failed requesting pty\n");
    }

    /* Open a SHELL on that pty */
    if (libssh2_channel_shell(channel)) {
        fprintf(stderr, "Unable to request shell on allocated pty\n");
    }

    libssh2_channel_write(channel, cmd, strlen(cmd));
    libssh2_channel_send_eof(channel);
	char buffer[BUF_SIZE];
	memset(buffer, 0, BUF_SIZE);
	rc = libssh2_channel_read(channel, buffer, BUF_SIZE);
	if (rc > 0) {
		m_execResultStr.append(buffer);
	}

	m_channelExitCode = 127;
	while ((rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN)
		waitsocket();
	if (rc == 0) {
		m_channelExitCode = libssh2_channel_get_exit_status(channel);
	}

skip_shell:
	if (channel) {
		libssh2_channel_free(channel);
		channel = NULL;
	}
	return rc;
}
Пример #2
0
/**
 * @function ssh.writeFile
 * 
 * ### Synopsis
 * 
 * var status = ssh.writeFile(handle, srcPath, dstPath);
 * var status = ssh.writeFile(handle, srcPath, dstPath, mode);
 * 
 * Write file to remote server via SCP.
 * 
 * @param {object} handle - opaque handle to already open SSH2 connection.
 * @param {string} srcPath - path to file in local file system to send.
 * @param {string} dstPath - path to file in remote file system to create.
 * @param {int} mode - desired resulting file permissions of file on remote end.
 * @return {boolean} success - true if the transfer succeeded, string error message if transfer failed.
 * 
 * ### Note
 * If mode is not provided, the file mode of the file being sent will be used.
 */
static JSVAL ssh2_scp_send(JSARGS args) {
	HandleScope scope;
    SSH2 *ssh2 = HANDLE(args[0]);
	String::Utf8Value srcPath(args[1]);
	String::Utf8Value dstPath(args[2]);
    int mode;
    struct stat fileinfo;
    if (stat(*srcPath, &fileinfo) != 0) {
        return scope.Close(String::New(strerror(errno)));
    }
    if (args.Length() > 3) {
        mode = args[3]->IntegerValue();
    }
    else {
        mode = fileinfo.st_mode;
    }
    mode &= 0777;
    int fd = open(*srcPath, O_RDONLY);
    if (fd < 0) {
        return scope.Close(String::New(strerror(errno)));
    }
#ifdef libssh2_scp_send64
    LIBSSH2_CHANNEL *channel = libssh2_scp_send64(ssh2->mSession, *dstPath, mode, fileinfo.st_size, 0, 0);
#else
    LIBSSH2_CHANNEL *channel = libssh2_scp_send(ssh2->mSession, *dstPath, mode, fileinfo.st_size);
#endif
    if (!channel) {
        char *errmsg;
        int errlen;
        libssh2_session_last_error(ssh2->mSession, &errmsg, &errlen, 0);
        return scope.Close(String::New(errmsg, errlen));
    }
    
    char mem[1024];
    ssize_t toWrite = fileinfo.st_size;
    while (toWrite > 0) {
        ssize_t nRead = read(fd, mem, 1024);
        if (nRead < 0) {
            int eNum = errno;
            libssh2_channel_free(channel);
            close(fd);
            return scope.Close(String::New(strerror(eNum)));
        }
        int rc = libssh2_channel_write(channel, mem, nRead);
        if (rc < 0) {
            char *errmsg;
            int errlen;
            libssh2_session_last_error(ssh2->mSession, &errmsg, &errlen, 0);
            libssh2_channel_free(channel);
            close(fd);
            return scope.Close(String::New(errmsg));
        }
        toWrite -= nRead;
    }
    close(fd);
    libssh2_channel_free(channel);
    return scope.Close(True());
}
Пример #3
0
void CLibssh2::download(const std::string& remote_filepath, std::ostream& out, int* num_bytes) throw (utils::CException, sys::CSyscallException)
{
    LIBSSH2_CHANNEL* channel = static_cast<LIBSSH2_CHANNEL*>(open_scp_read_channel(remote_filepath));

    try
    {
        *num_bytes = read_channel(channel, out);
        libssh2_channel_free(channel);
    }
    catch (...)
    {
        libssh2_channel_free(channel);
        throw;
    }
}
Пример #4
0
/* {{{ libssh2_publickey_shutdown
 * Shutdown the publickey subsystem
 */
LIBSSH2_API void libssh2_publickey_shutdown(LIBSSH2_PUBLICKEY *pkey)
{
	LIBSSH2_SESSION *session = pkey->channel->session;

	libssh2_channel_free(pkey->channel);
	LIBSSH2_FREE(session, pkey);
}
Пример #5
0
static void ssh_stream_free(git_smart_subtransport_stream *stream)
{
	ssh_stream *s = (ssh_stream *)stream;
	ssh_subtransport *t = OWNING_SUBTRANSPORT(s);
	int ret;
	
	GIT_UNUSED(ret);
	
	t->current_stream = NULL;
	
	if (s->channel) {
		libssh2_channel_close(s->channel);
        libssh2_channel_free(s->channel);
        s->channel = NULL;
	}
	
	if (s->session) {
		libssh2_session_free(s->session), s->session = NULL;
	}
	
	if (s->socket.socket) {
		ret = gitno_close(&s->socket);
		assert(!ret);
	}
	
	git__free(s->url);
	git__free(s);
}
Пример #6
0
static void ssh_stream_free(git_smart_subtransport_stream *stream)
{
	ssh_stream *s = (ssh_stream *)stream;
	ssh_subtransport *t;

	if (!stream)
		return;

	t = OWNING_SUBTRANSPORT(s);
	t->current_stream = NULL;

	if (s->channel) {
		libssh2_channel_close(s->channel);
		libssh2_channel_free(s->channel);
		s->channel = NULL;
	}

	if (s->session) {
		libssh2_session_free(s->session);
		s->session = NULL;
	}

	if (s->io) {
		git_stream_close(s->io);
		git_stream_free(s->io);
		s->io = NULL;
	}

	git__free(s->url);
	git__free(s);
}
Пример #7
0
	bool RunCommand(const char *aCommand) {
		if (!mSessionOK) {
			return false;
		}
		LIBSSH2_CHANNEL *channel = libssh2_channel_open_session(mSession);
		if (channel == NULL) {
			strcpy(mErrorMessage, "Could not open SSH2 channel");
			return false;
		}
#ifdef libssh2_session_set_timeout
		libssh2_session_set_timeout(mTimeout);
#endif
		if (libssh2_channel_exec(channel, aCommand) == -1) {
			strcpy(mErrorMessage, "Could not execute SSH2 command");
			return false;
		}
		std::string response;
		char buf[1025];
		ssize_t size = 1024;
		while (size == 1024) {
			size = libssh2_channel_read(channel, buf, 1024);
			buf[size] = '\0';
			response += buf;
		}
		if (mResponse) {
			delete [] mResponse;
		}
		mResponse = new char[response.size()+1];
		strcpy(mResponse, response.c_str());
		libssh2_channel_close(channel);
		mExitCode = libssh2_channel_get_exit_status(channel);
		libssh2_channel_free(channel);
		return true;
	}
Пример #8
0
void redisFree(redisContext *c) {
    if (c == NULL)
        return;

#ifdef FASTO
    if(c->channel != NULL){
        libssh2_channel_free(c->channel);
    }
    if(c->session != NULL){
        libssh2_session_disconnect(c->session, "Client disconnecting normally");
        libssh2_session_free(c->session);
    }
#endif
    if (c->fd > 0)
        close(c->fd);
    if (c->obuf != NULL)
        sdsfree(c->obuf);
    if (c->reader != NULL)
        redisReaderFree(c->reader);
    if (c->tcp.host)
        free(c->tcp.host);
    if (c->tcp.source_addr)
        free(c->tcp.source_addr);
    if (c->unix_sock.path)
        free(c->unix_sock.path);
    if (c->timeout)
        free(c->timeout);
    free(c);
}
Пример #9
0
static void
ssh_relay_event_cb(struct bufferevent *bev, short what, void *ptr)
{
	obfsproxyssh_client_session_t *session = ptr;
	struct evbuffer *buf;

	assert(bev == session->ssh_ev);

	if (what & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
		session->ssh_is_valid = 0;
		libssh2_channel_free(session->ssh_channel);
		session->ssh_channel = NULL;
		libssh2_session_free(session->ssh_session);
		session->ssh_session = NULL;
		buf = bufferevent_get_output(session->socks_ev);
		if (0 == session->socks_is_valid || 0 ==
				evbuffer_get_length(buf))
			session_free(session);
		else {
			bufferevent_disable(session->socks_ev, EV_READ);
			bufferevent_setcb(session->socks_ev, NULL,
					socks_relay_teardown_cb,
					socks_event_cb, session);
		}
	}
}
Пример #10
0
int ssh::quit_channel()
{
	int rc;

	exitcode = 127;
	while( (rc = libssh2_channel_close(channel)) == LIBSSH2_ERROR_EAGAIN )
	waitsocket(_sock_fd, session);

	if( rc == 0 )
	{
		exitcode = libssh2_channel_get_exit_status( channel );
		libssh2_channel_get_exit_signal(channel, &exitsignal,
					NULL, NULL, NULL, NULL, NULL);
	}

	if (exitsignal)
	{
		printf("\nGot signal: %s\n", exitsignal);      }
/*	else 
		printf("\nEXIT: %d bytecount: %d\n", exitcode, bytecount);*/

	libssh2_channel_free(channel);
	channel = NULL;
	return 0;
}
Пример #11
0
static void
virNetSSHSessionDispose(void *obj)
{
    virNetSSHSessionPtr sess = obj;
    VIR_DEBUG("sess=0x%p", sess);

    if (!sess)
        return;

    if (sess->channel) {
        libssh2_channel_send_eof(sess->channel);
        libssh2_channel_close(sess->channel);
        libssh2_channel_free(sess->channel);
    }

    libssh2_knownhost_free(sess->knownHosts);
    libssh2_agent_free(sess->agent);

    if (sess->session) {
        libssh2_session_disconnect(sess->session,
                                   "libvirt: virNetSSHSessionFree()");
        libssh2_session_free(sess->session);
    }

    virNetSSHSessionAuthMethodsFree(sess);

    VIR_FREE(sess->channelCommand);
    VIR_FREE(sess->hostname);
    VIR_FREE(sess->knownHostsFile);
    VIR_FREE(sess->authPath);
}
Пример #12
0
static void ssh_stream_free(git_smart_subtransport_stream *stream)
{
	ssh_stream *s = (ssh_stream *)stream;
	ssh_subtransport *t = OWNING_SUBTRANSPORT(s);
	int ret;

	GIT_UNUSED(ret);

	t->current_stream = NULL;

	if (s->channel) {
		libssh2_channel_close(s->channel);
		libssh2_channel_free(s->channel);
		s->channel = NULL;
	}

	if (s->session) {
		libssh2_session_free(s->session);
		s->session = NULL;
	}

	if (s->socket.socket) {
		(void)gitno_close(&s->socket);
		/* can't do anything here with error return value */
	}

	git__free(s->url);
	git__free(s);
}
Пример #13
0
void PJSSH::ExecuteCmd(const char* aCommand) const
{
  if( !SessionIsOk )
  {
    std::ostringstream o;
    o<<"Can not execute command since the SSH session is not set up ok. "
      "Last error message: ";
    throw std::logic_error(o.str());
  }

  // Try to open a channel to be used for executing the command.
  LIBSSH2_CHANNEL* channel = libssh2_channel_open_session(mSession);
  if( NULL == channel )
  {
    throw std::runtime_error("Could not open communication channel for "
        "executing remote command.");
  }

  //  Execute the command.
  if( -1 == libssh2_channel_exec(channel,aCommand))
  {
    throw std::runtime_error("Failed to execute the remote command.");
  }

  // Close the channel.
  libssh2_channel_close(channel);

  // Free resources.
  libssh2_channel_free(channel);
}
Пример #14
0
cql_ccm_bridge_t::~cql_ccm_bridge_t() {
  libssh2_channel_free(_ssh_internals->_channel);
  close_ssh_session();

  close_socket();
  libssh2_exit();
  finalize_socket_library();
}
Пример #15
0
static int
netsnmp_ssh_close(netsnmp_transport *t)
{
    int rc = -1;
    netsnmp_ssh_addr_pair *addr_pair = NULL;

    if (t != NULL && t->data != NULL) {
	addr_pair = (netsnmp_ssh_addr_pair *) t->data;
    }

    if (t != NULL && addr_pair && t->sock >= 0) {
        DEBUGMSGTL(("ssh", "close fd %d\n", t->sock));

        if (addr_pair->channel) {
            libssh2_channel_close(addr_pair->channel);
            libssh2_channel_free(addr_pair->channel);
            addr_pair->channel = NULL;
        }

        if (addr_pair->session) {
            libssh2_session_disconnect(addr_pair->session, "Normal Shutdown");
            libssh2_session_free(addr_pair->session);
            addr_pair->session = NULL;
        }

#ifndef HAVE_CLOSESOCKET
        rc = close(t->sock);
#else
        rc = closesocket(t->sock);
#endif
        t->sock = -1;

#ifdef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE

        if (!addr_pair->session && !addr_pair->channel) {
            /* XXX: make configurable */
            unlink(addr_pair->socket_path);
        }

#else /* we're called directly by sshd and use stdin/out */

        /* on the server: close stdin/out */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
#endif /* ! SNMPSSHDOMAIN_USE_EXTERNAL_PIPE */

    } else {

#ifndef SNMPSSHDOMAIN_USE_EXTERNAL_PIPE
        /* on the server: close stdin/out */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
#endif /* ! SNMPSSHDOMAIN_USE_EXTERNAL_PIPE */

    }
    return rc;
}
Пример #16
0
/**
 * seashell_tunnel_free (struct seashell_connection* conn)
 * Closes the tunnel connection.
 *
 * Arguments:
 *  conn - Connection to close.
 */
void seashell_tunnel_free (struct seashell_connection* conn) {
  if (conn) {
    libssh2_channel_free(conn->channel);
    libssh2_session_disconnect(conn->session, "Seashell's done!");
    libssh2_session_free(conn->session);
    close(conn->sockfd);
    free(conn);
  }
}
Пример #17
0
void Parser::closeChannel(LIBSSH2_CHANNEL *channel)
{
    if(channel)
    {
        //libssh2_channel_wait_closed(channel);
        libssh2_channel_close(channel);
        libssh2_channel_free(channel); // free will auto close the channel
    }
}
Пример #18
0
void cql_ccm_bridge_t::start_ssh_connection(const cql_ccm_bridge_configuration_t& settings) {
  _ssh_internals->_session = libssh2_session_init();
  if (!_ssh_internals->_session)
    throw cql_ccm_bridge_exception_t("cannot create ssh session");

  try {
    if (libssh2_session_handshake(_ssh_internals->_session, _socket))
      throw cql_ccm_bridge_exception_t("ssh session handshake failed");

    // get authentication modes supported by server
    char* auth_methods = libssh2_userauth_list(_ssh_internals->_session,
                                               settings.ssh_username().c_str(),
                                               settings.ssh_username().size());

    int auth_result;
    if (!settings.ssh_public_key_file().empty() && !settings.ssh_private_key_file().empty()) {
      auth_result = libssh2_userauth_publickey_fromfile(_ssh_internals->_session,
                                                        settings.ssh_username().c_str(),
                                                        settings.ssh_public_key_file().c_str(),
                                                        settings.ssh_private_key_file().c_str(),
                                                        "");
    } else {
      if (strstr(auth_methods, "password") == NULL)
        throw cql_ccm_bridge_exception_t("server doesn't support authentication by password");

      // try to login using username and password
      auth_result = libssh2_userauth_password(_ssh_internals->_session,
                                              settings.ssh_username().c_str(),
                                              settings.ssh_password().c_str());
    }

    if (auth_result != 0)
      throw cql_ccm_bridge_exception_t("invalid password or user");

    if (!(_ssh_internals->_channel = libssh2_channel_open_session(_ssh_internals->_session)))
      throw cql_ccm_bridge_exception_t("cannot open ssh session");

    try {

      if (libssh2_channel_request_pty(_ssh_internals->_channel, "vanilla"))
        throw cql_ccm_bridge_exception_t("pty requests failed");

      if (libssh2_channel_shell(_ssh_internals->_channel))
        throw cql_ccm_bridge_exception_t("cannot open shell");

      //TODO: Copy SSL files to remote connection for CCM to enable SSL with Cassandra instances (or use keytool to simply generate the files remotely)
    } catch (cql_ccm_bridge_exception_t&) {
      // calls channel_close
      libssh2_channel_free(_ssh_internals->_channel);
    }
  } catch (cql_ccm_bridge_exception_t&) {
    close_ssh_session();
    throw;
  }
}
Пример #19
0
/*
 * Cleanup data structures.
 */
static void cleanup(int code)
{
	int ret;
	char buf[1024];

	if (ssh_info) {
		close(ssh_info->forward_sock);
		close(ssh_info->listen_sock);
		if (ssh_info->channel) {
			libssh2_channel_free(ssh_info->channel);
		}

		if (ssh_info->session) {
			libssh2_session_disconnect(ssh_info->session,
					"Client disconnecting normally");
			libssh2_session_free(ssh_info->session);
		}

		free(ssh_info);
	}

	if (opt_dnat) {
		/* Remove DNAT rule */
		snprintf(buf, sizeof(buf), IPTABLE_DEL_DNAT, local_ip, local_port);
		ret = system(buf);
		if (ret != 0) {
			DBG("DNAT iptables command failed\n%s", buf);
		} else {
			DBG("UDP traffic on port 53 restored");
		}
	}

	if (opt_netfilter != -1) {
		snprintf(buf, sizeof(buf), IPTABLE_DEL_QUEUE,
				local_port, opt_netfilter);
		ret = system(buf);
		if (ret != 0) {
			DBG("NFQUEUE iptables command failed\n%s", buf);
		} else {
			DBG("Queuing UDP traffic on port 53 removed on queue num %d",
					opt_netfilter);
		}
	}

	/* Always last */
	if (code < 0) {
		exit(EXIT_FAILURE);
	} else {
		exit(EXIT_SUCCESS);
	}
}
Пример #20
0
int CLibssh2::close_ssh_channel(void* channel, std::string* exitsignal, std::string* errmsg)
{
    int exitcode = 127; // 127: command not exists
    LIBSSH2_CHANNEL* channel_ = static_cast<LIBSSH2_CHANNEL*>(channel);

    while (channel_ != NULL)
    {
        int errcode = libssh2_channel_close(channel_);
        if (0 == errcode)
        {
            char* errmsg_ = NULL;
            char* exitsignal_ = NULL;

            exitcode = libssh2_channel_get_exit_status(channel_);
            if (exitcode != 0) // 0 success
            {
                // 调用端可以strerror(*exitcode)取得出错原因
                libssh2_channel_get_exit_signal(channel_, &exitsignal_, NULL, &errmsg_, NULL, NULL, NULL);
                if (errmsg_ != NULL)
                {
                    *errmsg = errmsg_;
                    free(errmsg_);
                }
                if (exitsignal_ != NULL)
                {
                    *exitsignal = exitsignal_;
                    free(exitsignal_);
                }
            }

            libssh2_channel_free(channel_);
            channel_ = NULL;
            break;
        }
        else if (LIBSSH2_ERROR_EAGAIN == errcode)
        {
            if (!timedwait_socket())
            {
                THROW_SYSCALL_EXCEPTION("channel close timeout", ETIMEDOUT, "poll");
            }
        }
        else
        {
            THROW_EXCEPTION(get_session_errmsg(), errcode);
        }
    }

    return exitcode;
}
Пример #21
0
void
mx_channel_close (mx_channel_t *mcp)
{
    if (mcp == NULL)
	return;

    if (mcp->mc_client) {
	mx_sock_t *msp = mcp->mc_client;
	if (mx_mti(msp)->mti_set_channel)
	    mx_mti(msp)->mti_set_channel(msp, NULL, NULL);
    }

    libssh2_channel_free(mcp->mc_channel);
    mcp->mc_channel = NULL;
    free(mcp);
}
Пример #22
0
static int php_ssh2_channel_stream_close(php_stream *stream, int close_handle TSRMLS_DC)
{
	php_ssh2_channel_data *abstract = (php_ssh2_channel_data*)stream->abstract;

	if (!abstract->refcount || (--(*(abstract->refcount)) == 0)) {
		/* Last one out, turn off the lights */
		if (abstract->refcount) {
			efree(abstract->refcount);
		}
		libssh2_channel_eof(abstract->channel);
		libssh2_channel_free(abstract->channel);
		zend_list_delete(abstract->session_rsrc);
	}
	efree(abstract);

	return 0;
}
Пример #23
0
int ssh_guac_client_free_handler(guac_client* client) {

    ssh_guac_client_data* guac_client_data = (ssh_guac_client_data*) client->data;

    /* Close SSH channel */
    if (guac_client_data->term_channel != NULL) {
        libssh2_channel_send_eof(guac_client_data->term_channel);
        libssh2_channel_close(guac_client_data->term_channel);
    }

    /* Free terminal */
    guac_terminal_free(guac_client_data->term);
    pthread_join(guac_client_data->client_thread, NULL);

    /* Free channels */
    libssh2_channel_free(guac_client_data->term_channel);

    /* Clean up SFTP */
    if (guac_client_data->sftp_session)
        libssh2_sftp_shutdown(guac_client_data->sftp_session);

    if (guac_client_data->sftp_ssh_session) {
        libssh2_session_disconnect(guac_client_data->sftp_ssh_session, "Bye");
        libssh2_session_free(guac_client_data->sftp_ssh_session);
    }

    /* Free session */
    if (guac_client_data->session != NULL)
        libssh2_session_free(guac_client_data->session);

    /* Free auth key */
    if (guac_client_data->key != NULL)
        ssh_key_free(guac_client_data->key);

    /* Free clipboard */
    guac_common_clipboard_free(guac_client_data->clipboard);

    /* Free cursors */
    guac_ssh_cursor_free(client, guac_client_data->ibar_cursor);
    guac_ssh_cursor_free(client, guac_client_data->blank_cursor);

    /* Free generic data struct */
    free(client->data);

    return 0;
}
Пример #24
0
/*
 * Get DNS query using SSH transport layer.
 */
static ssize_t dig_ssh_request(struct ssh_session *ssh_info,
		char *buf, size_t buf_size, ssize_t len)
{
	int ret;
	ssize_t recv_size;

	do {
		ret = libssh2_channel_write(ssh_info->channel, buf, len);
		if (ret < 0) {
			ERR("libssh2_channel_write: %d", ret);
			goto error;
		}
		DBG("DNS request of size %ld sent to ssh channel", len);

		recv_size = libssh2_channel_read(ssh_info->channel, buf, buf_size);
		if (recv_size < 0) {
			char *buf;
			ERR("SSH channel read failed");
			libssh2_session_last_error(ssh_info->session, &buf, NULL, 0);
			ERR("Failure: %s", buf);
		} else if (recv_size == 0) {
			ret = libssh2_channel_eof(ssh_info->channel);
			if (ret) {
				DBG("SSH server disconnected!");
				libssh2_channel_close(ssh_info->channel);
				libssh2_channel_free(ssh_info->channel);
				/* Create new channel */
				ret = ssh_setup_tunnel(ssh_info, dns_ip);
				if (ret < 0) {
					goto error;
				}
			}
		} else {
			DBG("DNS reply red from ssh channel (size: %ld)",
					recv_size);
			goto end;
		}
	} while (1);

end:
	return recv_size;

error:
	return -1;
}
Пример #25
0
int close_ssh_connection(void* ssh_session)
{
    int ec;
    SSH_SESSION* p_session;
    
    p_session = (SSH_SESSION*)ssh_session;
    if(!(login_check(p_session)))
    {
        debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "close_ssh_connection : no session.");
        return SSH_TERMINAL_NO_SESSION_ERROR;
    }
    
    if(p_session->session_param->channel != NULL)
    {
        exit_thread(ssh_session);
    
        while((ec = libssh2_channel_close(p_session->session_param->channel)) == LIBSSH2_ERROR_EAGAIN)
        {
            waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC);
        }
        libssh2_channel_free(p_session->session_param->channel);
        p_session->session_param->channel = NULL;
    }
    
    while((ec = libssh2_session_disconnect(p_session->session_param->session, "normal")) == LIBSSH2_ERROR_EAGAIN)
    {
        waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC);
    }
    if(ec < 0)
    {
        debug_log(SSH_TERMINAL_SESSION_DISCONNECT_ERROR, "close_ssh_connection : disconnect failed.");
        return SSH_TERMINAL_SESSION_DISCONNECT_ERROR;
    }
    libssh2_session_free(p_session->session_param->session);
    close(p_session->session_param->sock);
    free(p_session->session_param);
    ori_thread_mutex_free(p_session->thread_param->command_queue_mutex);
    ori_thread_mutex_free(p_session->thread_param->add_list_mutex);
    ori_thread_condition_free(p_session->thread_param->command_queue_condition);
    ori_thread_condition_free(p_session->thread_param->add_list_condition);
    free(p_session->thread_param);
    free(p_session);
    
    return 0;
}
Пример #26
0
void clear(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel, int sock)
{
    int exitcode = 127, rc;
    char *exitsignal=(char *)"none";
    if( rc == 0 )
    {
        exitcode = libssh2_channel_get_exit_status( channel );
        libssh2_channel_get_exit_signal(channel, &exitsignal, NULL, NULL, NULL, NULL, NULL);
    }

    libssh2_channel_free(channel);
    channel = NULL;

    libssh2_session_disconnect(session, "");
    libssh2_session_free(session);
    close(sock);
    libssh2_exit();
}
Пример #27
0
int guac_ssh_client_free_handler(guac_client* client) {

    guac_ssh_client* ssh_client = (guac_ssh_client*) client->data;

    /* Close SSH channel */
    if (ssh_client->term_channel != NULL) {
        libssh2_channel_send_eof(ssh_client->term_channel);
        libssh2_channel_close(ssh_client->term_channel);
    }

    /* Free terminal (which may still be using term_channel) */
    if (ssh_client->term != NULL) {
        guac_terminal_free(ssh_client->term);
        pthread_join(ssh_client->client_thread, NULL);
    }

    /* Free terminal channel now that the terminal is finished */
    if (ssh_client->term_channel != NULL)
        libssh2_channel_free(ssh_client->term_channel);

    /* Clean up the SFTP filesystem object and session */
    if (ssh_client->sftp_filesystem) {
        guac_common_ssh_destroy_sftp_filesystem(ssh_client->sftp_filesystem);
        guac_common_ssh_destroy_session(ssh_client->sftp_session);
    }

    /* Free interactive SSH session */
    if (ssh_client->session != NULL)
        guac_common_ssh_destroy_session(ssh_client->session);

    /* Free SSH client credentials */
    if (ssh_client->user != NULL)
        guac_common_ssh_destroy_user(ssh_client->user);

    /* Free parsed settings */
    if (ssh_client->settings != NULL)
        guac_ssh_settings_free(ssh_client->settings);

    /* Free client structure */
    free(ssh_client);

    guac_common_ssh_uninit();
    return 0;
}
Пример #28
0
static void
ssh_relay_teardown_cb(obfsproxyssh_client_session_t *session)
{
	struct evbuffer *buf;
	int rval;

	buf = bufferevent_get_input(session->socks_ev);
	if (1 == session->ssh_is_valid && 0 < evbuffer_get_length(buf)) {
		socks_relay_cb(session->socks_ev, session);
		return;
	}

	buf = bufferevent_get_output(session->ssh_ev);
	if (1 == session->ssh_is_valid && 0 < evbuffer_get_length(buf))
		return;

	if (NULL != session->ssh_channel) {
		rval = libssh2_channel_close(session->ssh_channel);
		if (LIBSSH2_ERROR_EAGAIN == rval)
			return;
		libssh2_channel_free(session->ssh_channel);
		session->ssh_channel = NULL;
	}

	if (1 == session->ssh_is_valid && 0 < evbuffer_get_length(buf))
		return;

	if (NULL != session->ssh_session) {
		rval = libssh2_session_disconnect(session->ssh_session,
				ssh_client_profile_get_disconnect_msg());
		if (LIBSSH2_ERROR_EAGAIN == rval)
			return;
		libssh2_session_free(session->ssh_session);
		session->ssh_session = NULL;
	}

	if (1 == session->ssh_is_valid && 0 < evbuffer_get_length(buf))
		return;

	session_free(session);
}
Пример #29
0
int open_channel_and_exec(LIBSSH2_SESSION *session, int sock, const char *cmd,
                          char **out)
{
    int rc;
    LIBSSH2_CHANNEL *chan;
    chan = waitfor_open_channel(session, sock);
    if (!chan)
        return -1;

    while ((rc = libssh2_channel_exec(chan, cmd)) == LIBSSH2_ERROR_EAGAIN)
        waitsocket(session, sock);
    if (rc != 0)
        return -1;

    rc = read_channel(chan, session, sock, out);

    while ((rc = libssh2_channel_close(chan)) == LIBSSH2_ERROR_EAGAIN)
        waitsocket(session, sock);
    libssh2_channel_free(chan);
    return 0;
}
Пример #30
0
void redisFree(redisContext *c) {
    if (c == NULL)
        return;

#ifdef FASTOREDIS
    if(c->channel != NULL){
        libssh2_channel_free(c->channel);
    }
    if(c->session != NULL){
        libssh2_session_disconnect(c->session, "Client disconnecting normally");
        libssh2_session_free(c->session);
    }
#endif
    if (c->fd > 0)
        close(c->fd);
    if (c->obuf != NULL)
        sdsfree(c->obuf);
    if (c->reader != NULL)
        redisReaderFree(c->reader);
    free(c);
}