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