Back to index

openldap  2.4.31
SaslInteractionHandler.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2007-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 #include <iostream>
00008 #include <iomanip>
00009 #include <limits>
00010 #include "config.h"
00011 
00012 #ifdef HAVE_TERMIOS_H
00013 #include <termios.h>
00014 #include <cstdio>
00015 #endif
00016 
00017 #include <string.h>
00018 #include "SaslInteractionHandler.h"
00019 #include "SaslInteraction.h"
00020 #include "debug.h"
00021 
00022 void DefaultSaslInteractionHandler::handleInteractions( 
00023         const std::list<SaslInteraction*> &cb ) 
00024 {
00025     DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::handleCallbacks()" 
00026             << std::endl );
00027     std::list<SaslInteraction*>::const_iterator i;
00028 
00029     for (i = cb.begin(); i != cb.end(); i++ ) {
00030         bool noecho;
00031 
00032         cleanupList.push_back(*i);
00033 
00034         std::cout << (*i)->getPrompt();
00035         if (! (*i)->getDefaultResult().empty() ) {
00036             std::cout << "(" << (*i)->getDefaultResult() << ")" ;
00037         }
00038         std:: cout << ": ";
00039 
00040         switch ( (*i)->getId() ) {
00041             case SASL_CB_PASS:
00042             case SASL_CB_ECHOPROMPT:
00043                 noecho = true;
00044                 noecho = true;
00045             break;
00046             default:
00047                 noecho = false;
00048             break;
00049         }
00050 #ifdef HAVE_TERMIOS_H
00051         /* turn off terminal echo if needed */
00052         struct termios old_attr;
00053         if ( noecho ) {
00054             struct termios attr;
00055             if (tcgetattr(STDIN_FILENO, &attr) < 0) {
00056                 perror("tcgetattr");
00057             }
00058 
00059             /* save terminal attributes */
00060             memcpy(&old_attr, &attr, sizeof(attr));
00061 
00062             /* disable echo */
00063             attr.c_lflag &= ~(ECHO);
00064 
00065             /* write attributes to terminal */
00066             if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) {
00067                 perror("tcsetattr");
00068             }
00069         }
00070 #endif /* HAVE_TERMIOS_H */
00071         std::string input;
00072         std::cin >> std::noskipws >> input;
00073         std::cin >> std::skipws;
00074         (*i)->setResult(input);
00075         if( std::cin.fail() ) {
00076             std::cin.clear();
00077         }
00078         /* ignore the rest of the input line */
00079         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
00080 
00081 #ifdef HAVE_TERMIOS_H
00082         /* restore terminal settings */
00083         if ( noecho ) {
00084             tcsetattr(STDIN_FILENO, TCSANOW, &old_attr);
00085             std::cout << std::endl;
00086         }
00087 #endif /* HAVE_TERMIOS_H */
00088     }
00089 }
00090 
00091 DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler()
00092 {
00093     DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler()"
00094             << std::endl );
00095 
00096     std::list<SaslInteraction*>::const_iterator i;
00097     for (i = cleanupList.begin(); i != cleanupList.end(); i++ ) {
00098         delete(*i);
00099     }
00100 }