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