Exemplo n.º 1
0
/**
 * Authenticate in the SSH server with password with interactive mode
 * @param   QString password      the password to use for connect in the SSH server (password or passphrase when you use private key)
 * @return  bool                  true if it's ok, false else
 */
bool Kssh::authenticatePasswordInteractive(QString password)
{
    int auth, nb_prompt;
    QString name;

    auth = ssh_userauth_kbdint(this->m_session, NULL, NULL);

    while (auth == SSH_AUTH_INFO) {
        // enter in the interactive mode
        nb_prompt = ssh_userauth_kbdint_getnprompts(this->m_session);
        for (int i = 0; i < nb_prompt; i++) {
            name = QString(ssh_userauth_kbdint_getprompt(this->m_session, i, NULL));
            if (name == "Password: "******"Authenticated denied with this method : ") + QString(ssh_get_error(this->m_session)));
        return false;
    } else if (auth == SSH_AUTH_ERROR) {
        Klog::error(QString("Fatal error in authenticated with password interactive : ") + QString(ssh_get_error(this->m_session)));
        return false;
    }

    return false;
}
Exemplo n.º 2
0
int auth_kbdint(SSH_SESSION *session){
    int err=ssh_userauth_kbdint(session,NULL,NULL);
    char *name,*instruction,*prompt,*ptr;
    char buffer[128];
    int i,n;
    char echo;
    while (err==SSH_AUTH_INFO){
        name=ssh_userauth_kbdint_getname(session);
        instruction=ssh_userauth_kbdint_getinstruction(session);
        n=ssh_userauth_kbdint_getnprompts(session);
        if(strlen(name)>0)
            printf("%s\n",name);
        if(strlen(instruction)>0)
            printf("%s\n",instruction);
        for(i=0;i<n;++i){
            prompt=ssh_userauth_kbdint_getprompt(session,i,&echo);
            if(echo){
                printf("%s",prompt);
                fgets(buffer,sizeof(buffer),stdin);
                buffer[sizeof(buffer)-1]=0;
                if((ptr=strchr(buffer,'\n')))
                    *ptr=0;
                ssh_userauth_kbdint_setanswer(session,i,buffer);
                memset(buffer,0,strlen(buffer));
            } else {
                ptr=getpass(prompt);
                ssh_userauth_kbdint_setanswer(session,i,ptr);
            }
        }
        err=ssh_userauth_kbdint(session,NULL,NULL);
    }
    return err;
}
Exemplo n.º 3
0
int authenticate_kbdint(ssh_session session, const char *password) {
    int err;
    err = ssh_userauth_kbdint(session, NULL, NULL);
    while (err == SSH_AUTH_INFO) {
        const char *instruction;
        const char *name;
        char buffer[128];
        int i, n;
        name = ssh_userauth_kbdint_getname(session);
        instruction = ssh_userauth_kbdint_getinstruction(session);
        n = ssh_userauth_kbdint_getnprompts(session);
        if (name && strlen(name) > 0) {
            printf("%s\n", name);
        }
        if (instruction && strlen(instruction) > 0) {
            printf("%s\n", instruction);
        }
        for (i = 0; i < n; i++) {
            const char *answer;
            const char *prompt;
            char echo;
            prompt = ssh_userauth_kbdint_getprompt(session, i, &echo);
            if (prompt == NULL) {
                break;
            }
            if (echo) {
                char *p;
                printf("%s", prompt);
                if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
                    return SSH_AUTH_ERROR;
                }
                buffer[sizeof(buffer) - 1] = '\0';
                if ((p = strchr(buffer, '\n'))) {
                    *p = '\0';
                }
                if (ssh_userauth_kbdint_setanswer(session, i, buffer) < 0) {
                    return SSH_AUTH_ERROR;
                }
                memset(buffer, 0, strlen(buffer));
            } else {
                if (password && strstr(prompt, "Password:")) {
                    answer = password;
                } else {
                    buffer[0] = '\0';
                    if (ssh_getpass(prompt, buffer, sizeof(buffer), 0, 0) < 0) {
                        return SSH_AUTH_ERROR;
                    }
                    answer = buffer;
                }
                if (ssh_userauth_kbdint_setanswer(session, i, answer) < 0) {
                    return SSH_AUTH_ERROR;
                }
            }
        }
        err=ssh_userauth_kbdint(session,NULL,NULL);
    }
    return err;
}
Exemplo n.º 4
0
static int auth_kbdint(ssh_session session, const char *user,
    const char *passwd) {
  const char *name = NULL;
  const char *instruction = NULL;
  const char *prompt = NULL;
  char buffer[256] = {0};
  int err = SSH_AUTH_ERROR;
  int rc;

  err = ssh_userauth_kbdint(session, user, NULL);
  while (err == SSH_AUTH_INFO) {
    int n = 0;
    int i = 0;

    name = ssh_userauth_kbdint_getname(session);
    instruction = ssh_userauth_kbdint_getinstruction(session);
    n = ssh_userauth_kbdint_getnprompts(session);

    if (strlen(name) > 0) {
      printf("%s\n", name);
    }

    if (strlen(instruction) > 0) {
      printf("%s\n", instruction);
    }

    for (i = 0; i < n; ++i) {
      char echo;

      prompt = ssh_userauth_kbdint_getprompt(session, i, &echo);
      if (echo) {
        (*_authcb) (prompt, buffer, sizeof(buffer), 1, 0, NULL);
        rc = ssh_userauth_kbdint_setanswer(session, i, buffer);
        if (rc < 0) {
          return SSH_AUTH_ERROR;
        }
        ZERO_STRUCT(buffer);
      } else {
        if (passwd != NULL) {
          rc = ssh_userauth_kbdint_setanswer(session, i, passwd);
          if (rc < 0) {
            return SSH_AUTH_ERROR;
          }
        } else {
          (*_authcb) ("Password:", buffer, sizeof(buffer), 0, 0, NULL);
          rc = ssh_userauth_kbdint_setanswer(session, i, buffer);
          if (rc < 0) {
            return SSH_AUTH_ERROR;
          }
          ZERO_STRUCT(buffer);
        }
      }
    }
    err = ssh_userauth_kbdint(session, user, NULL);
  }

  return err;
}
Exemplo n.º 5
0
static int authenticate_kbdint(ssh_session session)
{
  int rc = ssh_userauth_kbdint(session, NULL, NULL);
  while (rc == SSH_AUTH_INFO)
  {
    const char* name = ssh_userauth_kbdint_getname(session);
    const char* instruction = ssh_userauth_kbdint_getinstruction(session);
    int nprompts = ssh_userauth_kbdint_getnprompts(session);

    if (strlen(name) > 0)
      printf("%s\n", name);
    if (strlen(instruction) > 0)
      printf("%s\n", instruction);
    for (int iprompt = 0; iprompt < nprompts; iprompt++)
    {
      const char* prompt = NULL;
      char echo;

      prompt = ssh_userauth_kbdint_getprompt(session, iprompt, &echo);
      if (echo)
      {
        char buffer[128], *ptr;

        printf("%s", prompt);
        if (fgets(buffer, sizeof(buffer), stdin) == NULL)
          return SSH_AUTH_ERROR;
        buffer[sizeof(buffer) - 1] = '\0';
        if ((ptr = strchr(buffer, '\n')) != NULL)
          *ptr = '\0';
        if (ssh_userauth_kbdint_setanswer(session, iprompt, buffer) < 0)
          return SSH_AUTH_ERROR;
        memset(buffer, 0, strlen(buffer));
      }
      else
      {
        if (!ftp->getpass_hook)
          return SSH_AUTH_ERROR;

        char* pw = ftp->getpass_hook(prompt);
        if (!pw)
          return SSH_AUTH_ERROR;

        rc = ssh_userauth_kbdint_setanswer(session, iprompt, pw);
        memset(pw, 0, strlen(pw));
        free(pw);
        if (rc < 0)
          return SSH_AUTH_ERROR;
      }
    }
    rc = ssh_userauth_kbdint(session, NULL, NULL);
  }
  return rc;
}
Exemplo n.º 6
0
bool clSSH::LoginInteractiveKBD(bool throwExc) throw(clException)
{
    if(!m_session) {
        THROW_OR_FALSE("NULL SSH session");
    }

    int rc;
    rc = ssh_userauth_kbdint(m_session, NULL, NULL);
    if(rc == SSH_AUTH_INFO) {
        while(rc == SSH_AUTH_INFO) {
            const char* name, *instruction;
            int nprompts, iprompt;
            name = ssh_userauth_kbdint_getname(m_session);
            instruction = ssh_userauth_kbdint_getinstruction(m_session);
            nprompts = ssh_userauth_kbdint_getnprompts(m_session);
            wxUnusedVar(name);
            wxUnusedVar(instruction);
            for(iprompt = 0; iprompt < nprompts; iprompt++) {
                const char* prompt;
                char echo;
                prompt = ssh_userauth_kbdint_getprompt(m_session, iprompt, &echo);
                if(echo) {
                    wxString answer = ::wxGetTextFromUser(prompt, "SSH");
                    if(answer.IsEmpty()) {
                        THROW_OR_FALSE(wxString() << "Login error: " << ssh_get_error(m_session));
                    }
                    if(ssh_userauth_kbdint_setanswer(m_session, iprompt, answer.mb_str(wxConvUTF8).data()) < 0) {
                        THROW_OR_FALSE(wxString() << "Login error: " << ssh_get_error(m_session));
                    }
                } else {
                    if(ssh_userauth_kbdint_setanswer(m_session, iprompt, GetPassword().mb_str(wxConvUTF8).data()) < 0) {
                        THROW_OR_FALSE(wxString() << "Login error: " << ssh_get_error(m_session));
                    }
                }
            }
            rc = ssh_userauth_kbdint(m_session, NULL, NULL);
        }
        return true; // success
    }
    THROW_OR_FALSE("Interactive Keyboard is not enabled for this server");
    return false;
}
Exemplo n.º 7
0
int
SSHClient::authKbdint(ssh_session session)
{
//    GNASH_REPORT_FUNCTION;
    int err = ssh_userauth_kbdint(session, NULL, NULL);
    char *name,*instruction,*prompt,*ptr;
    char buffer[128];
    int i,n;
    char echo;
    while (err == SSH_AUTH_INFO){
        name = const_cast<char *>(ssh_userauth_kbdint_getname(session));
        instruction = const_cast<char *>(ssh_userauth_kbdint_getinstruction(session));
        n=ssh_userauth_kbdint_getnprompts(session);
        if(strlen(name)>0)
            log_debug(name);
        if(strlen(instruction)>0)
            log_debug(instruction);
        for(i=0; i<n; ++i){
            prompt = const_cast<char *>(ssh_userauth_kbdint_getprompt(session, i, &echo));
            if(echo){
                log_debug(prompt);
                fgets(buffer,sizeof(buffer),stdin);
                buffer[sizeof(buffer)-1]=0;
                if((ptr=strchr(buffer,'\n')))
                    *ptr=0;
                if (ssh_userauth_kbdint_setanswer(session, i, buffer) < 0) {
                  return SSH_AUTH_ERROR;
                }
                memset(buffer,0,strlen(buffer));
            } else {
                ptr=getpass(prompt);
                if (ssh_userauth_kbdint_setanswer(session, i, ptr) < 0) {
                  return SSH_AUTH_ERROR;
                }
            }
        }
        err=ssh_userauth_kbdint(session, NULL, NULL);
    }

    return err;
}
Exemplo n.º 8
0
static int _torture_auth_kbdint(ssh_session session,
                               const char *password) {
    const char *prompt;
    char echo;
    int err;

    if (session == NULL || password == NULL) {
        return SSH_AUTH_ERROR;
    }

    err = ssh_userauth_kbdint(session, NULL, NULL);
    if (err == SSH_AUTH_ERROR) {
        return err;
    }

    if (ssh_userauth_kbdint_getnprompts(session) != 1) {
        return SSH_AUTH_ERROR;
    }

    prompt = ssh_userauth_kbdint_getprompt(session, 0, &echo);
    if (prompt == NULL) {
        return SSH_AUTH_ERROR;
    }

    if (ssh_userauth_kbdint_setanswer(session, 0, password) < 0) {
        return SSH_AUTH_ERROR;
    }
    err = ssh_userauth_kbdint(session, NULL, NULL);
    if (err == SSH_AUTH_INFO) {
        if (ssh_userauth_kbdint_getnprompts(session) != 0) {
            return SSH_AUTH_ERROR;
        }
        err = ssh_userauth_kbdint(session, NULL, NULL);
    }

    return err;
}
Exemplo n.º 9
0
int authenticate_kbdint(ssh_session session, const char *password) 
{
	int err;
	const char *instruction;
        const char *name; 
        int i, n;
	const char *answer;
	const char *prompt;
	char echo;

	err = ssh_userauth_kbdint(session, NULL, NULL);
	while (err == SSH_AUTH_INFO) 
	{

		name = ssh_userauth_kbdint_getname(session);
		instruction = ssh_userauth_kbdint_getinstruction(session);
		n = ssh_userauth_kbdint_getnprompts(session);

		for (i = 0; i < n; i++) 
		{
			prompt=ssh_userauth_kbdint_getprompt(session,i,&echo);
			if(prompt==NULL) break;
			if (password && strstr(prompt, "Password:")) 
			{
				answer = password;
				err = ssh_userauth_kbdint_setanswer(session, i, answer);
			}
			if (err < 0) 
			{
				return SSH_AUTH_ERROR;
			}
		}
		err=ssh_userauth_kbdint(session,NULL,NULL);
	}
	return err;
}
Exemplo n.º 10
0
INT_PTR KBIntDialog::OnInitDialog() {
	//TODO: fix the tab order
	const char * name = ssh_userauth_kbdint_getname(m_session);
	if (!name)
		name = "";
	::SetDlgItemTextA(m_hwnd, IDC_STATIC_NAMEFIELD, name);

	if (m_nrPrompt > 1) {	//for each prompt, clone prompt and input fields

		HWND hPrompt1 = ::GetDlgItem(m_hwnd, IDC_EDIT_PROMPT1);
		HWND hAnswer1 = ::GetDlgItem(m_hwnd, IDC_EDIT_ANSWER1);
		HWND hMarker = ::GetDlgItem(m_hwnd, IDC_STATIC_MARKER);
		RECT promptRect;
		RECT answerRect;
		RECT markerRect;
		::GetWindowRect(hPrompt1, &promptRect);
		ScreenRectToClient(m_hwnd, &promptRect);
		::GetWindowRect(hAnswer1, &answerRect);
		ScreenRectToClient(m_hwnd, &answerRect);
		::GetWindowRect(hMarker, &markerRect);
		ScreenRectToClient(m_hwnd, &markerRect);

		int deltaY = markerRect.top - promptRect.top;
		//int deltaX = 0;	//unused


		int curPromptID = IDC_EDIT_PROMPT1;
		int curAnswerID = IDC_EDIT_ANSWER1;

		for(int i = 1; i < m_nrPrompt; i++) {
			curPromptID = curPromptID+2;
			curAnswerID = curAnswerID+2;

			promptRect.top += deltaY;
			promptRect.bottom += deltaY;
			answerRect.top += deltaY;
			answerRect.bottom += deltaY;

			::CreateWindowEx(0, WC_EDIT, TEXT(""),
							WS_CHILD | WS_VSCROLL | ES_AUTOHSCROLL | ES_MULTILINE | ES_READONLY | WS_VISIBLE,
							promptRect.left, promptRect.top, promptRect.right-promptRect.left, promptRect.bottom-promptRect.top,
							m_hwnd, (HMENU)curPromptID, m_hInstance, NULL);

			::CreateWindowEx(WS_EX_CLIENTEDGE, WC_EDIT, TEXT(""),
							WS_CHILD | WS_BORDER | ES_AUTOHSCROLL | WS_VISIBLE,
							answerRect.left, answerRect.top, answerRect.right-answerRect.left, answerRect.bottom-answerRect.top,
							m_hwnd, (HMENU)curPromptID, m_hInstance, NULL);
		}

		int totalDeltaY = deltaY * (m_nrPrompt-1);

		HWND hBtnOK = ::GetDlgItem(m_hwnd, IDOK);
		HWND hBtnCancel = ::GetDlgItem(m_hwnd, IDCANCEL);
		RECT btnRect;
		::GetWindowRect(hBtnOK, &btnRect);
		ScreenRectToClient(m_hwnd, &btnRect);
		::MoveWindow(hBtnOK, btnRect.left, btnRect.top+totalDeltaY, btnRect.right-btnRect.left, btnRect.bottom-btnRect.top, TRUE);

		::GetWindowRect(hBtnCancel, &btnRect);
		ScreenRectToClient(m_hwnd, &btnRect);
		::MoveWindow(hBtnCancel, btnRect.left, btnRect.top+totalDeltaY, btnRect.right-btnRect.left, btnRect.bottom-btnRect.top, TRUE);

		RECT winRect;
		::GetWindowRect(m_hwnd, &winRect);
		Resize(winRect.right-winRect.left, winRect.bottom-winRect.top+totalDeltaY);
	}

	for(int i = 0; i < m_nrPrompt; i++) {
		char echo = 0;
		const char * prompt = ssh_userauth_kbdint_getprompt(m_session, i, &echo);
		::SetDlgItemTextA(m_hwnd, IDC_EDIT_PROMPT1+(i*2), prompt);

		if (!echo) {
			HWND hCurAnswer = ::GetDlgItem(m_hwnd, IDC_EDIT_ANSWER1+(i*2));
			//LONG_PTR style = ::GetWindowLongPtr(hCurAnswer, GWL_STYLE);
			//::SetWindowLongPtr(hCurAnswer, GWL_STYLE, style|ES_PASSWORD);
			::SendMessage(hCurAnswer, EM_SETPASSWORDCHAR, (WPARAM)'*', 0);
		}

	}

	Dialog::OnInitDialog();
	::SetFocus(::GetDlgItem(m_hwnd, IDC_EDIT_ANSWER1));

	return FALSE;
}
Exemplo n.º 11
0
int FSSftp::CheckSession( int* err, FSCInfo* info )
{
	if ( sshSession ) { return 0; }

	try
	{
		sshSession = ssh_new();

		if ( !sshSession ) { throw int( SSH_INTERROR_X3 ); }

		if ( ssh_options_set( sshSession, SSH_OPTIONS_HOST, unicode_to_utf8( _operParam.server.Data() ).ptr() ) )
		{
			throw int( SSH_INTERROR_X3 );
		}

		int port = _operParam.port;

		if ( ssh_options_set( sshSession, SSH_OPTIONS_PORT, &port ) )
		{
			throw int( SSH_INTERROR_X3 );
		}


		FSString userName = "";

		if ( _operParam.user.Data()[0] )
		{
			userName = _operParam.user.Data();
		}
		else
		{
			char* ret = getenv( "LOGNAME" );

			if ( ret )
			{
				userName = FSString( sys_charset_id, ret );
				_operParam.user = userName.GetUnicode();

				MutexLock infoLock( &infoMutex );
				_infoParam.user = userName.GetUnicode();
			}
		};

		if ( ssh_options_set( sshSession, SSH_OPTIONS_USER, ( char* )userName.Get( _operParam.charset ) ) ) //есть сомнения, что надо все таки в utf8
		{
			throw int( SSH_INTERROR_X3 );
		}

		if ( ssh_connect( sshSession ) != SSH_OK )
		{
			throw int( SSH_INTERROR_CONNECT );
		}


		int method = ssh_userauth_list( sshSession, 0 );

		int ret;


		static unicode_t userSymbol = '@';

		if ( method & SSH_AUTH_METHOD_PASSWORD )
		{
			FSPromptData data;
			data.visible = false;
			data.prompt = utf8_to_unicode( "Password:"******"SFTP_" ).ptr(),
			        carray_cat<unicode_t>( userName.GetUnicode(), &userSymbol, _operParam.server.Data() ).ptr(),
			        &data, 1 ) ) { throw int( SSH_INTERROR_STOPPED ); }

			ret = ssh_userauth_password( sshSession,
			                             ( char* )FSString( _operParam.user.Data() ).Get( _operParam.charset ),
			                             ( char* )FSString( data.prompt.Data() ).Get( _operParam.charset ) );
		}

		if ( ret != SSH_AUTH_SUCCESS &&
		     ( method & SSH_AUTH_METHOD_INTERACTIVE ) != 0 )
		{
			while ( true )
			{
				ret = ssh_userauth_kbdint( sshSession, 0, 0 );

				if ( ret != SSH_AUTH_INFO ) { break; }

				const char* instruction = ssh_userauth_kbdint_getinstruction( sshSession );

				if ( !instruction ) { instruction = ""; }

				int n = ssh_userauth_kbdint_getnprompts( sshSession );

				if ( n <= 0 ) { continue; }

				std::vector<FSPromptData> pData( n );
				int i;

				for ( i = 0; i < n; i++ )
				{
					char echo;
					const char* prompt = ssh_userauth_kbdint_getprompt( sshSession, i, &echo );

					if ( !prompt ) { break; }

					pData[i].visible = echo != 0;
					pData[i].prompt = utf8_to_unicode( prompt ).ptr();
				}

				if ( !info ) { throw int( SSH_INTERROR_AUTH ); }

				if ( !info->Prompt(
				        utf8_to_unicode( "SFTP" ).ptr(),
				        carray_cat<unicode_t>( userName.GetUnicode(), &userSymbol, _operParam.server.Data() ).ptr(),
				        pData.ptr(), n ) ) { throw int( SSH_INTERROR_STOPPED ); }

				for ( i = 0; i < n; i++ )
				{
					if ( ssh_userauth_kbdint_setanswer( sshSession, i, ( char* )FSString( pData[i].prompt.Data() ).Get( _operParam.charset ) ) < 0 )
					{
						throw int( SSH_INTERROR_AUTH );
					}
				}
			}
		}

		if ( ret != SSH_AUTH_SUCCESS )
		{
			if ( ret == SSH_AUTH_PARTIAL )
			{
				throw int( SSH_INTERROR_UNSUPPORTED_AUTH );
			}
			else
			{
				throw int( SSH_INTERROR_AUTH );
			}
		}

		sftpSession = sftp_new( sshSession );

		if ( !sftpSession ) { throw int( SSH_INTERROR_FATAL ); }

		if ( sftp_init( sftpSession ) != SSH_OK ) { throw int( SSH_INTERROR_FATAL ); }

	}
	catch ( int e )
	{
		if ( err ) { *err = e; }

		if ( sftpSession ) { sftp_free( sftpSession ); }

		if ( sshSession ) { ssh_free( sshSession ); }

		sshSession = 0;
		sftpSession = 0;
		return ( e == SSH_INTERROR_STOPPED ) ? -2 : -1;
	}

	return 0;
}