mirror of
https://git.openldap.org/openldap/openldap.git
synced 2024-12-21 03:10:25 +08:00
Helper Classes to handle cyrus-sasl interactions
This commit is contained in:
parent
4d06dc7ddd
commit
914872c12a
43
contrib/ldapc++/src/SaslInteraction.cpp
Normal file
43
contrib/ldapc++/src/SaslInteraction.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2007, OpenLDAP Foundation, All Rights Reserved.
|
||||
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
||||
*/
|
||||
|
||||
#include <SaslInteraction.h>
|
||||
#include <iostream>
|
||||
#include "debug.h"
|
||||
|
||||
SaslInteraction::SaslInteraction( sasl_interact_t *interact ) :
|
||||
m_interact(interact) {}
|
||||
|
||||
SaslInteraction::~SaslInteraction()
|
||||
{
|
||||
DEBUG(LDAP_DEBUG_TRACE, "SaslInteraction::~SaslInteraction()" << std::endl);
|
||||
}
|
||||
|
||||
unsigned long SaslInteraction::getId() const
|
||||
{
|
||||
return m_interact->id;
|
||||
}
|
||||
|
||||
const std::string SaslInteraction::getPrompt() const
|
||||
{
|
||||
return std::string(m_interact->prompt);
|
||||
}
|
||||
|
||||
const std::string SaslInteraction::getChallenge() const
|
||||
{
|
||||
return std::string(m_interact->challenge);
|
||||
}
|
||||
|
||||
const std::string SaslInteraction::getDefaultResult() const
|
||||
{
|
||||
return std::string(m_interact->defresult);
|
||||
}
|
||||
|
||||
void SaslInteraction::setResult(const std::string &res)
|
||||
{
|
||||
m_result = res;
|
||||
m_interact->result = m_result.data();
|
||||
m_interact->len = m_result.size();
|
||||
}
|
28
contrib/ldapc++/src/SaslInteraction.h
Normal file
28
contrib/ldapc++/src/SaslInteraction.h
Normal file
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2007, OpenLDAP Foundation, All Rights Reserved.
|
||||
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
||||
*/
|
||||
|
||||
#ifndef SASL_INTERACTION_H
|
||||
#define SASL_INTERACTION_H
|
||||
|
||||
#include <string>
|
||||
#include <sasl/sasl.h>
|
||||
|
||||
class SaslInteraction {
|
||||
public:
|
||||
SaslInteraction( sasl_interact_t *interact );
|
||||
~SaslInteraction();
|
||||
unsigned long getId() const;
|
||||
const std::string getPrompt() const;
|
||||
const std::string getChallenge() const;
|
||||
const std::string getDefaultResult() const;
|
||||
|
||||
void setResult(const std::string &res);
|
||||
|
||||
private:
|
||||
sasl_interact_t *m_interact;
|
||||
std::string m_result;
|
||||
|
||||
};
|
||||
#endif /* SASL_INTERACTION_H */
|
95
contrib/ldapc++/src/SaslInteractionHandler.cpp
Normal file
95
contrib/ldapc++/src/SaslInteractionHandler.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2007, OpenLDAP Foundation, All Rights Reserved.
|
||||
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
#ifdef HAVE_TERMIOS_H
|
||||
#include <termios.h>
|
||||
#endif
|
||||
|
||||
#include "SaslInteractionHandler.h"
|
||||
#include "SaslInteraction.h"
|
||||
#include "debug.h"
|
||||
|
||||
void DefaultSaslInteractionHandler::handleInteractions(
|
||||
const std::list<SaslInteraction*> &cb )
|
||||
{
|
||||
DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::handleCallbacks()"
|
||||
<< std::endl );
|
||||
std::list<SaslInteraction*>::const_iterator i;
|
||||
|
||||
for (i = cb.begin(); i != cb.end(); i++ ) {
|
||||
bool noecho;
|
||||
|
||||
cleanupList.push_back(*i);
|
||||
|
||||
std::cout << (*i)->getPrompt();
|
||||
if (! (*i)->getDefaultResult().empty() ) {
|
||||
std::cout << "(" << (*i)->getDefaultResult() << ")" ;
|
||||
}
|
||||
std:: cout << ": ";
|
||||
|
||||
switch ( (*i)->getId() ) {
|
||||
case SASL_CB_PASS:
|
||||
case SASL_CB_ECHOPROMPT:
|
||||
noecho = true;
|
||||
noecho = true;
|
||||
break;
|
||||
default:
|
||||
noecho = false;
|
||||
break;
|
||||
}
|
||||
#ifdef HAVE_TERMIOS_H
|
||||
/* turn off terminal echo if needed */
|
||||
struct termios old_attr;
|
||||
if ( noecho ) {
|
||||
struct termios attr;
|
||||
if (tcgetattr(STDIN_FILENO, &attr) < 0) {
|
||||
perror("tcgetattr");
|
||||
}
|
||||
|
||||
/* save terminal attributes */
|
||||
memcpy(&old_attr, &attr, sizeof(attr));
|
||||
|
||||
/* disable echo */
|
||||
attr.c_lflag &= ~(ECHO);
|
||||
|
||||
/* write attributes to terminal */
|
||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) {
|
||||
perror("tcsetattr");
|
||||
}
|
||||
}
|
||||
#endif /* HAVE_TERMIOS_H */
|
||||
std::string input;
|
||||
std::cin >> std::noskipws >> input;
|
||||
std::cin >> std::skipws;
|
||||
(*i)->setResult(input);
|
||||
if( std::cin.fail() ) {
|
||||
std::cin.clear();
|
||||
}
|
||||
/* ignore the rest of the input line */
|
||||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||
|
||||
#ifdef HAVE_TERMIOS_H
|
||||
/* restore terminal settings */
|
||||
if ( noecho ) {
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &old_attr);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
#endif /* HAVE_TERMIOS_H */
|
||||
}
|
||||
}
|
||||
|
||||
DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler()
|
||||
{
|
||||
DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler()"
|
||||
<< std::endl );
|
||||
|
||||
std::list<SaslInteraction*>::const_iterator i;
|
||||
for (i = cleanupList.begin(); i != cleanupList.end(); i++ ) {
|
||||
delete(*i);
|
||||
}
|
||||
}
|
26
contrib/ldapc++/src/SaslInteractionHandler.h
Normal file
26
contrib/ldapc++/src/SaslInteractionHandler.h
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2007, OpenLDAP Foundation, All Rights Reserved.
|
||||
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
||||
*/
|
||||
|
||||
#ifndef SASL_INTERACTION_HANDLER_H
|
||||
#define SASL_INTERACTION_HANDLER_H
|
||||
#include <list>
|
||||
|
||||
class SaslInteraction;
|
||||
|
||||
class SaslInteractionHandler {
|
||||
public:
|
||||
virtual void handleInteractions( const std::list<SaslInteraction*> &cb )=0;
|
||||
virtual ~SaslInteractionHandler() {}
|
||||
};
|
||||
|
||||
class DefaultSaslInteractionHandler {
|
||||
public:
|
||||
virtual void handleInteractions( const std::list<SaslInteraction*> &cb );
|
||||
virtual ~DefaultSaslInteractionHandler();
|
||||
|
||||
private:
|
||||
std::list<SaslInteraction*> cleanupList;
|
||||
};
|
||||
#endif /* SASL_INTERACTION_HANDLER_H */
|
Loading…
Reference in New Issue
Block a user