Exemple #1
0
int logon(char* username, char* pass, LIBSSH2_SESSION **session, int sock)
{
    int rc;
    /* Create a session instance */
    if((*session = libssh2_session_init()) == NULL) return -1;
    //libssh2_session_set_blocking(*session, 0);
    libssh2_session_set_blocking(*session, 1);
    //while ((rc = libssh2_session_handshake(*session, sock)) == LIBSSH2_ERROR_EAGAIN);
    rc = libssh2_session_handshake(*session, sock);
    if(rc)
    {
        fprintf(stderr, "Failure establishing SSH session: %d %s\n", rc, strerror(errno));
        return -1;
    }
    
//    while ((rc = libssh2_userauth_password(*session, username, pass)) == LIBSSH2_ERROR_EAGAIN);
    rc = libssh2_userauth_password(*session, username, pass);
    if (rc) 
    {
        fprintf(stderr, "Authentication by password failed.\n");
        if(*session != NULL)
        {
            libssh2_session_disconnect(*session, "");
            libssh2_session_free(*session);
        }
        close(sock);
        libssh2_exit();
        return 0;
    }
}
Exemple #2
0
/* {{{ session_close
 */
int
Session_close(PYLIBSSH2_SESSION *self)
{
    PRINTFUNCNAME
    int rc;
    char *reason = "end";
    PyObject *item;

    if(self->opened) {
        while(PySet_Size(self->sftps)) {
            item = PySet_Pop(self->sftps);
            Sftp_shutdown((PYLIBSSH2_SFTP*)item);
            Py_XDECREF(item);
        }

        while(PySet_Size(self->channels)) {
            item = PySet_Pop(self->channels);
            Channel_close((PYLIBSSH2_CHANNEL*)item);
            Py_XDECREF(item);
        }
        while(PySet_Size(self->listeners)) {
            item = PySet_Pop(self->listeners);
            Py_XDECREF(item);
        }

        Py_BEGIN_ALLOW_THREADS
        rc = libssh2_session_disconnect(self->session, reason);
        Py_END_ALLOW_THREADS

        self->opened = 0;

        return rc;
    }
    return 0;
}
Exemple #3
0
void getClient(char * serverAddress) {
    int sockFd = makeSocketOrDie();
    struct addrinfo * serverInfo = getServerInfo(serverAddress);
	printf("Connecting to server\n");
    connectOrDie(sockFd, serverInfo);
	printf("Connected to server.  Making LIBSSH2 session\n");
    LIBSSH2_SESSION * session = makeSession();
    libssh2_session_set_blocking(session, 1);
	libssh2_session_set_timeout(session, 5000);
	printf("Made session, handshaking\n");
    int result = libssh2_session_handshake(session, sockFd);
    //const char * fingerprint = libssh2_hostkey_hash(session, LIBSSH_HOSTKEY_HASH_SHA1);
    //TODO: Match the fingerprint against something.
    if (result) {
		char * errorMessage;
		libssh2_session_last_error(session, &errorMessage, NULL, 0);
		fprintf(stderr, "Error %s handshaking\n", errorMessage);
		exit(EXIT_FAILURE);
	}
    printf("Handshake completed, making SFTP Session\n");
	libssh2_userauth_password(session, NETID, PWD);
    LIBSSH2_SFTP * sftpSession = makeSFTPSession(session);
	printf("Started SFTP - Downloading file\n");
    LIBSSH2_SFTP_HANDLE * fileHandle = libssh2_sftp_open(sftpSession, serverFilePath, LIBSSH2_FXF_READ, 0);
	readFile(session, sftpSession, fileHandle);
    libssh2_sftp_shutdown(sftpSession);
    libssh2_session_disconnect(session, "Done.\n");
    libssh2_session_free(session);
    freeaddrinfo(serverInfo);
    close(sockFd);
}
Exemple #4
0
void CEasyssh::DisconnectAP()
{
	libssh2_session_disconnect(m_ssh_session, "Normal Shutdown, Thank you for playing");
	libssh2_session_free(m_ssh_session);
	closesocket(m_sock);
	libssh2_exit();
}
Exemple #5
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);
}
Exemple #6
0
static void php_ssh2_session_dtor(zend_resource *rsrc)
{
	LIBSSH2_SESSION *session = (LIBSSH2_SESSION*)rsrc->ptr;
	php_ssh2_session_data **data = (php_ssh2_session_data**)libssh2_session_abstract(session);

	libssh2_session_disconnect(session, "PECL/ssh2 (http://pecl.php.net/packages/ssh2)");

	if (*data) {
		if ((*data)->ignore_cb) {
			zval_ptr_dtor((*data)->ignore_cb);
		}
		if ((*data)->debug_cb) {
			zval_ptr_dtor((*data)->debug_cb);
		}
		if ((*data)->macerror_cb) {
			zval_ptr_dtor((*data)->macerror_cb);
		}
		if ((*data)->disconnect_cb) {
			zval_ptr_dtor((*data)->disconnect_cb);
		}

		closesocket((*data)->socket);

		efree(*data);
		*data = NULL;
	}

	libssh2_session_free(session);
}
Exemple #7
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_disconnect(s->session, "closing transport");
		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);
}
CURLcode Curl_scp_done(struct connectdata *conn, CURLcode status,
                       bool premature)
{
  struct SSHPROTO *scp = conn->data->reqdata.proto.ssh;
  (void)premature; /* not used */

  Curl_safefree(scp->path);
  scp->path = NULL;

  if (scp->ssh_channel) {
    if (libssh2_channel_close(scp->ssh_channel) < 0) {
      infof(conn->data, "Failed to stop libssh2 channel subsystem\n");
    }
  }

  if (scp->ssh_session) {
    libssh2_session_disconnect(scp->ssh_session, "Shutdown");
    libssh2_session_free(scp->ssh_session);
    scp->ssh_session = NULL;
  }

  free(conn->data->reqdata.proto.ssh);
  conn->data->reqdata.proto.ssh = NULL;
  Curl_pgrsDone(conn);

  (void)status; /* unused */

  return CURLE_OK;
}
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);
}
Exemple #10
0
/*
 * Deallocate the memory used by the Session object
 *
 * Arguments: self - The Session object
 * Returns:   None
 */
static void
session_dealloc(SSH2_SessionObj *self)
{
	if (self->opened) {
		Py_BEGIN_ALLOW_THREADS
		while (libssh2_session_disconnect(self->session, "") == LIBSSH2_ERROR_EAGAIN) {}
		Py_END_ALLOW_THREADS
	}
Exemple #11
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;
}
Exemple #12
0
static void freeConnection(const SFtpConnectionCache::SFtpConnection *conn)
{
    libssh2_sftp_shutdown(conn->sftp_session);
    libssh2_session_disconnect(conn->session, "Normal Shutdown");
    libssh2_session_free(conn->session);
    closesocket(conn->sock);

    delete(conn);
}
Exemple #13
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);
  }
}
void guac_common_ssh_destroy_session(guac_common_ssh_session* session) {

    /* Disconnect and clean up libssh2 */
    libssh2_session_disconnect(session->session, "Bye");
    libssh2_session_free(session->session);

    /* Free all other data */
    free(session);

}
Exemple #15
0
int Parser::closeSession()
{
    if(socket_->isOpen())
        socket_->disconnectFromHost();
    socket_->deleteLater();
    socket_=0;
    libssh2_session_disconnect(session_, "Client disconnecting normally");
    libssh2_session_free(session_);
    libssh2_exit();
    return 1;
}
Exemple #16
0
	~SSH2() {
		if (mSessionOK) {
			libssh2_session_disconnect(mSession, "Goodbye from SilkJS");
			libssh2_session_free(mSession);
		}
		if (mSock > 0) {
			close(mSock);
			mSock = -1;
		}
#ifdef libssh2_exit
		libssh2_exit();
#endif
	}
/* sftp协议断开连接 */
static void sftp_disconnect(protocol_data_t *protocol)
{
    if (protocol == NULL || protocol->protocol_data == NULL) {
        return;
    }

    sftp_data_t *data = (sftp_data_t *)protocol->protocol_data;

    libssh2_sftp_shutdown(data->sftp_session);
    libssh2_session_disconnect(data->session, "Normal Shutdown");
    libssh2_session_free(data->session);
    close(data->sock);
}
Exemple #18
0
void SSH2Utils::deconnect() {
	libssh2_session_disconnect(m_session,
			"Normal Shutdown, Thank you for playing");
	libssh2_session_free(m_session);
#ifdef WIN32
	closesocket(m_sock);
#else
	close(m_sock);
#endif
	libssh2_exit();
	m_session = 0;
	m_sock = 0;
}
Exemple #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);
	}
}
Exemple #20
0
void guac_common_ssh_destroy_session(guac_common_ssh_session* session) {

    /* Disconnect and clean up libssh2 */
    libssh2_session_disconnect(session->session, "Bye");
    libssh2_session_free(session->session);

    /* Destroy associated user */
    if (session->user)
        guac_common_ssh_destroy_user(session->user);

    /* Free all other data */
    free(session);

}
Exemple #21
0
void stop_session_fixture()
{
    if(connected_session) {
        libssh2_session_disconnect(connected_session, "test ended");
        libssh2_session_free(connected_session);
        shutdown(connected_socket, 2);
        connected_session = NULL;
    }
    else {
        fprintf(stderr, "Cannot stop session - none started");
    }

    stop_openssh_fixture();
}
Exemple #22
0
bool SSHSession::end_session(){

 libssh2_session_disconnect(this->session, "Normal Shutdown, Thank you for playing");

 libssh2_session_free(session);

#ifdef WIN32
    closesocket(this->sock);
#else
    ::close(this->sock);
#endif

return true;

}
Exemple #23
0
void CLibssh2::cleanup()
{
    if (_session != NULL)
    {
        LIBSSH2_SESSION* session = static_cast<LIBSSH2_SESSION*>(_session);
        libssh2_session_disconnect(session, "normal Shutdown");
        libssh2_session_free(session);
        _session = NULL;
    }
    if (_socket_fd != -1)
    {
        close(_socket_fd);
        _socket_fd = -1;
    }
}
Exemple #24
0
PJSSH::~PJSSH()
{
  if( SessionIsOk )
  {
    libssh2_session_disconnect(mSession,"Goodbye from PJSSH.");
    libssh2_session_free(mSession);
  }
#ifdef PJSSH_WINDOWS
  closesocket(hSock);
  WSACleanup();
#else
#ifdef PJSSH_POSIX
  close(hSock);
#endif
#endif
}
Exemple #25
0
int ssh::quit()
{

	libssh2_session_disconnect(session,
                               "Normal Shutdown, Thank you for playing");
	libssh2_session_free(session);

	close(_sock_fd);

	//cout << "all done" << std::endl;

	libssh2_exit();
	
	return 0;

}
Exemple #26
0
/**
 * @function SFTP.close
 * 
 * ### Synopsis
 * 
 * SFTP.close(handle);
 * 
 * Close an SFTP connection and free up any resources used.
 * 
 * @param {object} handle - handle to open connect returned by SFTP.connect().
 */
JSVAL sftp_close (JSARGS args) {
    HandleScope scope;
    SFTP *handle = HANDLE(args[0]);
    if (handle->sftp_session) {
        libssh2_sftp_shutdown(handle->sftp_session);
    }
    if (handle->session) {
        libssh2_session_disconnect(handle->session, "SilkJS SFTP Disconnecting");
        libssh2_session_free(handle->session);
    }
    if (handle->sock > 0) {
        close(handle->sock);
    }
    delete handle;
    return Undefined();
}
Exemple #27
0
void CSSHTunnelThread::Cleanup()
{
	// Close all the sockets
	g_SSHThreadMutex.Lock();
	subThreadSDSet::iterator it;
	for( it = g_setSocketDescriptor.begin(); it != g_setSocketDescriptor.end(); ++it )
	{
		int socketDescriptor = *it;
#ifdef WIN32
		closesocket(socketDescriptor);
#else
		close(socketDescriptor);
#endif
	}
	g_SSHThreadMutex.Unlock();

	Sleep(1000);

	if(m_session)
	{
		libssh2_session_disconnect(m_session, "Client disconnecting normally");
		libssh2_session_free(m_session);
		m_session = NULL;
	}

	if (m_listensock)
	{
#ifdef WIN32
		closesocket(m_listensock);
#else
		close(m_listensock);
#endif
		m_listensock = 0;
	}

	if (m_sock)
	{
#ifdef WIN32
		closesocket(m_sock);
#else
		close(m_sock);
#endif
		m_sock = 0;
	}

	libssh2_exit();
}
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;
}
Exemple #29
0
Fichier : ssh.c Projet : 3mao/stool
int ssh_free(SSH * pSsh)
{
	if (NULL==pSsh)
	{
		return 0;
	}
	pSsh->channel=NULL;
	libssh2_session_disconnect(pSsh->session,"Normal Shutdown, Thank you for playing");
	libssh2_session_free(pSsh->session);

	close(pSsh->sock_fd);
	fprintf(stderr, "all done\n");

	libssh2_exit();

	return 1;
}
Exemple #30
0
/*
 * Decrements the reference counter on the ruby session container.
 * When this goes to 0 then the session will be released.
 * */
void
libssh2_ruby_session_release(LibSSH2_Ruby_Session *session_data) {
    // Decrease the reference count
    session_data->refcount--;

    // If the reference count is 0, free all the things!
    if (session_data->refcount == 0) {
        if (session_data->session != NULL) {
            BLOCK(libssh2_session_disconnect(
                        session_data->session,
                        "Normal shutdown by libssh2-ruby."));
            BLOCK(libssh2_session_free(session_data->session));
        }

        free(session_data);
    }
}