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