1 // $OpenLDAP: pkg/ldap/contrib/ldapc++/src/SaslInteractionHandler.cpp,v 1.3.2.2 2008/04/14 23:09:26 quanah Exp $ 2 /* 3 * Copyright 2007, OpenLDAP Foundation, All Rights Reserved. 4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file 5 */ 6 7 #include <iostream> 8 #include <iomanip> 9 #include <limits> 10 #include "config.h" 11 12 #ifdef HAVE_TERMIOS_H 13 #include <termios.h> 14 #endif 15 16 #include <string.h> 17 #include "SaslInteractionHandler.h" 18 #include "SaslInteraction.h" 19 #include "debug.h" 20 21 void DefaultSaslInteractionHandler::handleInteractions( 22 const std::list<SaslInteraction*> &cb ) 23 { 24 DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::handleCallbacks()" 25 << std::endl ); 26 std::list<SaslInteraction*>::const_iterator i; 27 28 for (i = cb.begin(); i != cb.end(); i++ ) { 29 bool noecho; 30 31 cleanupList.push_back(*i); 32 33 std::cout << (*i)->getPrompt(); 34 if (! (*i)->getDefaultResult().empty() ) { 35 std::cout << "(" << (*i)->getDefaultResult() << ")" ; 36 } 37 std:: cout << ": "; 38 39 switch ( (*i)->getId() ) { 40 case SASL_CB_PASS: 41 case SASL_CB_ECHOPROMPT: 42 noecho = true; 43 noecho = true; 44 break; 45 default: 46 noecho = false; 47 break; 48 } 49 #ifdef HAVE_TERMIOS_H 50 /* turn off terminal echo if needed */ 51 struct termios old_attr; 52 if ( noecho ) { 53 struct termios attr; 54 if (tcgetattr(STDIN_FILENO, &attr) < 0) { 55 perror("tcgetattr"); 56 } 57 58 /* save terminal attributes */ 59 memcpy(&old_attr, &attr, sizeof(attr)); 60 61 /* disable echo */ 62 attr.c_lflag &= ~(ECHO); 63 64 /* write attributes to terminal */ 65 if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) { 66 perror("tcsetattr"); 67 } 68 } 69 #endif /* HAVE_TERMIOS_H */ 70 std::string input; 71 std::cin >> std::noskipws >> input; 72 std::cin >> std::skipws; 73 (*i)->setResult(input); 74 if( std::cin.fail() ) { 75 std::cin.clear(); 76 } 77 /* ignore the rest of the input line */ 78 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 79 80 #ifdef HAVE_TERMIOS_H 81 /* restore terminal settings */ 82 if ( noecho ) { 83 tcsetattr(STDIN_FILENO, TCSANOW, &old_attr); 84 std::cout << std::endl; 85 } 86 #endif /* HAVE_TERMIOS_H */ 87 } 88 } 89 90 DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler() 91 { 92 DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler()" 93 << std::endl ); 94 95 std::list<SaslInteraction*>::const_iterator i; 96 for (i = cleanupList.begin(); i != cleanupList.end(); i++ ) { 97 delete(*i); 98 } 99 } 100