Ejemplo n.º 1
0
static int client(ssh_session session){
  int auth=0;
  char *banner;
  int state;

  if (ssh_options_set(session, SSH_OPTIONS_HOST ,host) < 0)
    return -1;
  ssh_options_parse_config(session, NULL);

  if(ssh_connect(session)){
      fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session));
      return -1;
  }
  state=verify_knownhost(session);
  if (state != 0)
  	return -1;
  ssh_userauth_none(session, NULL);
  banner=ssh_get_issue_banner(session);
  if(banner){
      printf("%s\n",banner);
      free(banner);
  }
  auth=authenticate_console(session);
  if(auth != SSH_AUTH_SUCCESS){
  	return -1;
  }
 	forwarding(session);
  return 0;
}
Ejemplo n.º 2
0
static int client(ssh_session_t *session)
{
    int auth=0;
    char *banner;
    int state;
    if (user)
    {
        if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0)
        {
            return -1;
        }
    }
    if (ssh_options_set(session, SSH_OPTIONS_HOST ,host) < 0)
    {
        return -1;
    }
    if (proxycommand != NULL)
    {
        if(ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, proxycommand))
        {
            return -1;
        }
    }
    ssh_options_parse_config(session, NULL);

    if(ssh_connect(session))
    {
        fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session));
        return -1;
    }
    state = verify_knownhost(session);
    if (state != 0)
    {
        return -1;
    }
    ssh_userauth_none(session, NULL);
    banner = ssh_get_issue_banner(session);
    if(banner)
    {
        printf("%s\n", banner);
        free(banner);
    }
    auth = authenticate_console(session);
    if(auth != SSH_AUTH_SUCCESS)
    {
        return -1;
    }
    if(!cmds[0]) {
        shell(session);
    } else {
        batch_shell(session);
    }
    return 0;
}
Ejemplo n.º 3
0
// TODO: remove this obsolete 
static int client(ssh_session session)
{
int auth=0;
int state;

	if (user)

		if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0)

			return -1;

	if (ssh_options_set(session, SSH_OPTIONS_HOST ,host) < 0)

		return -1;

	if (proxycommand != NULL)
	{
		if(ssh_options_set(session, SSH_OPTIONS_PROXYCOMMAND, proxycommand))

		return -1;
	}

	ssh_options_parse_config(session, NULL);

	if(ssh_connect(session))
	{
		fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session));

		return -1;
	}

	state=verify_knownhost(session);

	if (state != 0)

		return -1;

	ssh_userauth_none(session, NULL);

	auth=authenticate_console(session);

	if(auth != SSH_AUTH_SUCCESS)
	{
		return -1;
	}

	shell(session);

	return 0;
}
Ejemplo n.º 4
0
ssh_session connect_ssh(const char *host, const char *user,int verbosity)
{
	ssh_session session;
	int auth=0;

	session=ssh_new();
	if(session==NULL) return(NULL); 

	if(user!=NULL)
	{
		if(ssh_options_set(session,SSH_OPTIONS_USER,user)<0) 
		{
			ssh_free(session);
			return NULL;
		}
	}
	if(ssh_options_set(session,SSH_OPTIONS_HOST,host)<0) 
	{
		ssh_free(session);
		return(NULL);
	}
	ssh_options_set(session,SSH_OPTIONS_LOG_VERBOSITY,&verbosity);
	if(ssh_connect(session))
	{
		prterr("Connection failed : %s\n",ssh_get_error(session));
		ssh_disconnect(session);
		ssh_free(session);
		return(NULL);
	}
	if(verify_knownhost(session)<0)
	{
		ssh_disconnect(session);
		ssh_free(session);
		return(NULL);
	}
	auth=authenticate_console(session);  
	if(auth==SSH_AUTH_SUCCESS)
	{
		return(session);
	} else if(auth==SSH_AUTH_DENIED)
	{
    		prterr("Error:  Authentication failed.");
  	} else {
		prterr("Error while authenticating : %s\n",ssh_get_error(session));
	}
	ssh_disconnect(session);
	ssh_free(session);
	return(NULL);
}
Ejemplo n.º 5
0
ssh_session connect_ssh(const char *host, const char *user,int verbosity)
{
ssh_session session;

int auth=0;

	session=ssh_new();

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

	if(NULL != user)
	{
		if (ssh_options_set(session, SSH_OPTIONS_USER, user) < 0)
		{
			ssh_free(session);

			return NULL;
		}
	}

	if (ssh_options_set(session, SSH_OPTIONS_HOST, host) < 0)
	{
		ssh_free(session);

		return NULL;
	}

	ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);

	if(ssh_connect(session))
	{
		fprintf(stderr,"Connection failed : %s\n",ssh_get_error(session));

		ssh_disconnect(session);

		ssh_free(session);

		return NULL;
	}

	if(verify_knownhost(session)<0)
	{
		ssh_disconnect(session);

		ssh_free(session);

		return NULL;
	}

	auth=authenticate_console(session);

	if(SSH_AUTH_SUCCESS == auth)
	{
		return session;
	}
	else
		if(SSH_AUTH_DENIED == auth)
		{
			fprintf(stderr,"Authentication failed\n");
		}
		else
		{
			fprintf(stderr,"Error while authenticating : %s\n",ssh_get_error(session));
		}

		ssh_disconnect(session);

		ssh_free(session);

		return NULL;
}
Ejemplo n.º 6
0
bool SSH_Socket::onEnter()
{
    // For testing and getting debugging output
    //int verb = SSH_LOG_PROTOCOL;
    int rc;

    // Setup new SSH Shell
    session = ssh_new();
    if(session == nullptr)
    {
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION,
            "Closed Session", "User has closed the program.", nullptr);

        TheSocketHandler::Instance()->setActive(false);
        return false;
    }

    std::cout << "Connecting to: " << host << ":" << port << std::endl;
    std::cout << "User: "******"Starting up SSH Connection, this can take few moments" << std::endl;

    // SSH Connect
    rc = ssh_connect(session);
    if(rc != SSH_OK)
    {
        std::cout << "Error: ssh_connect: " << host
            << " " << ssh_get_error(session) << std::endl;

        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION,
            "Closed Session", "Unable to Connect to Server!", nullptr);

        TheSocketHandler::Instance()->setActive(false);
        return false;
    }

    // Verify Server is a known host.
    rc = verify_knownhost();
    if(rc < 0)
    {
        std::cout << "Error: verify_knownhost: " << host
            << " " << ssh_get_error(session) << " " << rc << std::endl;

        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION,
            "Closed Session", "User has closed the program.", nullptr);

        TheSocketHandler::Instance()->setActive(false);
        return false;
    }

    //if ((ssh_userauth_password(session, NULL, password) == SSH_AUTH_SUCCESS)
    rc = authenticate_console();
    if(rc != SSH_AUTH_SUCCESS)
    {
         std::cout << "Error: authenticate: " << host
            << " " << ssh_get_error(session) << " " << rc << std::endl;

        TheSocketHandler::Instance()->setActive(false);
        return false;
    }

    // Setup Channel for Socket Communications
    sshChannel = ssh_channel_new(session);
    if(sshChannel == nullptr)
    {
        std::cout << "Error: ssh_channel_new: " << host
            << " " << ssh_get_error(session) << " " << rc << std::endl;

        TheSocketHandler::Instance()->setActive(false);
        return false;
    }

    // Open A shell Session
    rc = ssh_channel_open_session(sshChannel);
    if(rc != SSH_OK)
    {
        std::cout << "Error: ssh_channel_open_session: " << host
            << " " << ssh_get_error(session) << " " << rc << std::endl;

        TheSocketHandler::Instance()->setActive(false);
        return false;
    }

    /* Don't need this when we use the PTY size, it does both!
    // Setup sockets or "channels" within the session
    if(ssh_channel_request_pty(sshChannel))
    {
        printf("\r\n SSH Error, Request for PTY Failed. %s: %s - %i \r\n",
        host, ssh_get_error(session), rc);
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION,
             "Closed Session",
             "User has closed the program.",
             NULL);
        onExit();
    }*/

    // Set the term and pty size of the terminal.
    // Only After the Shell has been initialized.
    if(ssh_channel_request_pty_size(sshChannel, "ansi", 80, 25))
    {
        std::cout << "Error: ssh_channel_request_pty_size: " << host
            << " " << ssh_get_error(session) << " " << rc << std::endl;
        // Not an error to exit the connection on.
        //return 0;
    }

    // Now request a shell with the pty to get read/write
    if(ssh_channel_request_shell(sshChannel))
    {
        std::cout << "Error: ssh_channel_request_shell: " << host
            << " " << ssh_get_error(session) << " " << rc << std::endl;

        TheSocketHandler::Instance()->setActive(false);
        return false;
    }
    return true;
}