summaryrefslogtreecommitdiffstats
path: root/contrib/ldapc++/src/SaslInteractionHandler.cpp
blob: cb5d37dd9547ee536c2474d6640c118b81710ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// $OpenLDAP$
/*
 * Copyright 2007-2022 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

#include <iostream>
#include <iomanip>
#include <limits>
#include "config.h"

#ifdef HAVE_TERMIOS_H
#include <termios.h>
#include <unistd.h>
#include <cstdio>
#endif

#include <string.h>
#include "SaslInteractionHandler.h"
#include "SaslInteraction.h"
#include "debug.h"

void DefaultSaslInteractionHandler::handleInteractions( 
        const std::list<SaslInteraction*> &cb ) 
{
    DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::handleCallbacks()" 
            << std::endl );
    std::list<SaslInteraction*>::const_iterator i;

    for (i = cb.begin(); i != cb.end(); i++ ) {
        bool noecho;

        cleanupList.push_back(*i);

        std::cout << (*i)->getPrompt();
        if (! (*i)->getDefaultResult().empty() ) {
            std::cout << "(" << (*i)->getDefaultResult() << ")" ;
        }
        std:: cout << ": ";

        switch ( (*i)->getId() ) {
            case SASL_CB_PASS:
            case SASL_CB_ECHOPROMPT:
                noecho = true;
                noecho = true;
            break;
            default:
                noecho = false;
            break;
        }
#ifdef HAVE_TERMIOS_H
        /* turn off terminal echo if needed */
        struct termios old_attr;
        if ( noecho ) {
            struct termios attr;
            if (tcgetattr(STDIN_FILENO, &attr) < 0) {
                perror("tcgetattr");
            }

            /* save terminal attributes */
            memcpy(&old_attr, &attr, sizeof(attr));

            /* disable echo */
            attr.c_lflag &= ~(ECHO);

            /* write attributes to terminal */
            if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) < 0) {
                perror("tcsetattr");
            }
        }
#endif /* HAVE_TERMIOS_H */
        std::string input;
        std::cin >> std::noskipws >> input;
        std::cin >> std::skipws;
        (*i)->setResult(input);
        if( std::cin.fail() ) {
            std::cin.clear();
        }
        /* ignore the rest of the input line */
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

#ifdef HAVE_TERMIOS_H
        /* restore terminal settings */
        if ( noecho ) {
            tcsetattr(STDIN_FILENO, TCSANOW, &old_attr);
            std::cout << std::endl;
        }
#endif /* HAVE_TERMIOS_H */
    }
}

DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler()
{
    DEBUG(LDAP_DEBUG_TRACE, "DefaultSaslInteractionHandler::~DefaultSaslInteractionHandler()"
            << std::endl );

    std::list<SaslInteraction*>::const_iterator i;
    for (i = cleanupList.begin(); i != cleanupList.end(); i++ ) {
        delete(*i);
    }
}