From: Ralf Haferkamp Date: Wed, 19 Dec 2007 16:54:42 +0000 (+0000) Subject: Helper Classes to handle cyrus-sasl interactions X-Git-Tag: OPENLDAP_REL_ENG_2_4_9~20^2~292 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=914872c12a1145c6e6a21595aa332cfc7bd9613d;p=openldap Helper Classes to handle cyrus-sasl interactions --- diff --git a/contrib/ldapc++/src/SaslInteraction.cpp b/contrib/ldapc++/src/SaslInteraction.cpp new file mode 100644 index 0000000000..7ed6666513 --- /dev/null +++ b/contrib/ldapc++/src/SaslInteraction.cpp @@ -0,0 +1,43 @@ +/* + * Copyright 2007, OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ + +#include +#include +#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(); +} diff --git a/contrib/ldapc++/src/SaslInteraction.h b/contrib/ldapc++/src/SaslInteraction.h new file mode 100644 index 0000000000..62a0478b6e --- /dev/null +++ b/contrib/ldapc++/src/SaslInteraction.h @@ -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 +#include + +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 */ diff --git a/contrib/ldapc++/src/SaslInteractionHandler.cpp b/contrib/ldapc++/src/SaslInteractionHandler.cpp new file mode 100644 index 0000000000..c837226ef1 --- /dev/null +++ b/contrib/ldapc++/src/SaslInteractionHandler.cpp @@ -0,0 +1,95 @@ +/* + * Copyright 2007, OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ + +#include +#include + +#ifdef HAVE_TERMIOS_H +#include +#endif + +#include "SaslInteractionHandler.h" +#include "SaslInteraction.h" +#include "debug.h" + +void DefaultSaslInteractionHandler::handleInteractions( + const std::list &cb ) +{ + DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::handleCallbacks()" + << std::endl ); + std::list::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::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::const_iterator i; + for (i = cleanupList.begin(); i != cleanupList.end(); i++ ) { + delete(*i); + } +} diff --git a/contrib/ldapc++/src/SaslInteractionHandler.h b/contrib/ldapc++/src/SaslInteractionHandler.h new file mode 100644 index 0000000000..93621329cd --- /dev/null +++ b/contrib/ldapc++/src/SaslInteractionHandler.h @@ -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 + +class SaslInteraction; + +class SaslInteractionHandler { + public: + virtual void handleInteractions( const std::list &cb )=0; + virtual ~SaslInteractionHandler() {} +}; + +class DefaultSaslInteractionHandler { + public: + virtual void handleInteractions( const std::list &cb ); + virtual ~DefaultSaslInteractionHandler(); + + private: + std::list cleanupList; +}; +#endif /* SASL_INTERACTION_HANDLER_H */