]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/SaslInteractionHandler.cpp
Merge branch 'mdb.master' of ssh://git-master.openldap.org/~git/git/openldap
[openldap] / contrib / ldapc++ / src / SaslInteractionHandler.cpp
1 // $OpenLDAP$
2 /*
3  * Copyright 2007-2011 The 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 #include <cstdio>
15 #endif
16
17 #include <string.h>
18 #include "SaslInteractionHandler.h"
19 #include "SaslInteraction.h"
20 #include "debug.h"
21
22 void DefaultSaslInteractionHandler::handleInteractions( 
23         const std::list<SaslInteraction*> &cb ) 
24 {
25     DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::handleCallbacks()" 
26             << std::endl );
27     std::list<SaslInteraction*>::const_iterator i;
28
29     for (i = cb.begin(); i != cb.end(); i++ ) {
30         bool noecho;
31
32         cleanupList.push_back(*i);
33
34         std::cout << (*i)->getPrompt();
35         if (! (*i)->getDefaultResult().empty() ) {
36             std::cout << "(" << (*i)->getDefaultResult() << ")" ;
37         }
38         std:: cout << ": ";
39
40         switch ( (*i)->getId() ) {
41             case SASL_CB_PASS:
42             case SASL_CB_ECHOPROMPT:
43                 noecho = true;
44                 noecho = true;
45             break;
46             default:
47                 noecho = false;
48             break;
49         }
50 #ifdef HAVE_TERMIOS_H
51         /* turn off terminal echo if needed */
52         struct termios old_attr;
53         if ( noecho ) {
54             struct termios attr;
55             if (tcgetattr(STDIN_FILENO, &attr) < 0) {
56                 perror("tcgetattr");
57             }
58
59             /* save terminal attributes */
60             memcpy(&old_attr, &attr, sizeof(attr));
61
62             /* disable echo */
63             attr.c_lflag &= ~(ECHO);
64
65             /* write attributes to terminal */
66             if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) {
67                 perror("tcsetattr");
68             }
69         }
70 #endif /* HAVE_TERMIOS_H */
71         std::string input;
72         std::cin >> std::noskipws >> input;
73         std::cin >> std::skipws;
74         (*i)->setResult(input);
75         if( std::cin.fail() ) {
76             std::cin.clear();
77         }
78         /* ignore the rest of the input line */
79         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
80
81 #ifdef HAVE_TERMIOS_H
82         /* restore terminal settings */
83         if ( noecho ) {
84             tcsetattr(STDIN_FILENO, TCSANOW, &old_attr);
85             std::cout << std::endl;
86         }
87 #endif /* HAVE_TERMIOS_H */
88     }
89 }
90
91 DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler()
92 {
93     DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler()"
94             << std::endl );
95
96     std::list<SaslInteraction*>::const_iterator i;
97     for (i = cleanupList.begin(); i != cleanupList.end(); i++ ) {
98         delete(*i);
99     }
100 }