summaryrefslogtreecommitdiff
path: root/libopie2/opiesecurity/multiauthcommon.cpp
blob: 9de62d20ad1686a4e691ee330995448edc1c8196 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "multiauthplugininterface.h"
#include "multiauthcommon.h"

/* Opie */
#include <opie2/odebug.h>
#include <opie2/oapplication.h>

/* Qt */
#include <qpe/qpeapplication.h>
#include <qpe/qlibrary.h>
#include <qpe/qcom.h>
#include <qtextview.h>
#include <qdir.h>

/* UNIX */
#include <unistd.h>
#include <qpe/config.h>

namespace Opie {
namespace Security {

SecOwnerDlg::SecOwnerDlg( QWidget *parent, const char * name, Contact c,
                          bool modal, bool fullscreen = FALSE )
: QDialog( parent, name, modal,
           fullscreen ?
           WStyle_NoBorder | WStyle_Customize | WStyle_StaysOnTop : 0 )
{
    if ( fullscreen ) {
        QRect desk = qApp->desktop()->geometry();
        setGeometry( 0, 0, desk.width(), desk.height() );
    }
    // set up contents.
    QString text("<H3>" + tr("Please contact the owner (directions follow), or try again clicking of this screen (and waiting for the penalty time) if you are the legitimate owner") + "</H3>");
    text += c.toRichText();
    tv = new QTextView(this);
    tv->setText(text);

    tv->viewport()->installEventFilter(this);
}

void SecOwnerDlg::resizeEvent( QResizeEvent * )
{
    tv->resize( size() );
}

bool SecOwnerDlg::eventFilter(QObject *o, QEvent *e)
{
    if (e->type() == QEvent::KeyPress || e->type() == QEvent::MouseButtonPress ) {
        accept();
        return TRUE;
    }
    return QWidget::eventFilter(o, e);
}

void SecOwnerDlg::mousePressEvent( QMouseEvent * ) { accept(); }


namespace Internal {
/// run plugins until we reach nbSuccessMin successes
int runPlugins() {

    SecOwnerDlg *oi = 0;
    // see if there is contact information.
    QString vfilename = Global::applicationFileName("addressbook",
                                                    "businesscard.vcf");
    if (QFile::exists(vfilename)) {
        Contact c;
        c = Contact::readVCard( vfilename )[0];

        oi = new SecOwnerDlg(0, 0, c, TRUE, TRUE);
    }

    Config config("Security");
    config.setGroup("Plugins");
    QStringList plugins = config.readListEntry("IncludePlugins", ',');
    /* if there are no configured plugins, we simply return 0 to
     * let the user in:
     */
    if (plugins.isEmpty() == true) {
        owarn << "No authentication plugin has been configured yet!" << oendl;
        odebug << "Letting the user in..." << oendl;
        if(oi) delete oi;
        return 0;
    }
    config.setGroup("Misc");
    int nbSuccessMin = config.readNumEntry("nbSuccessMin", 1);
    int nbSuccess = 0;

    /* tries to launch successively each plugin in $OPIEDIR/plugins/security
     * directory which file name is in Security.conf / [Misc] / IncludePlugins
     */
    QString path = QPEApplication::qpeDir() + "/plugins/security";
    QStringList::Iterator libIt;

    for ( libIt = plugins.begin(); libIt != plugins.end(); ++libIt ) {
        QInterfacePtr<MultiauthPluginInterface> iface;
        QLibrary *lib = new QLibrary( path + "/" + *libIt );

        if ( lib->queryInterface(
                                 IID_MultiauthPluginInterface,
                                 (QUnknownInterface**)&iface ) == QS_OK )
        {
            // the plugin is a true Multiauth plugin
            odebug << "Accepted plugin: " << QString( path + "/" + *libIt ) << oendl;
            odebug << "Plugin name: " << iface->plugin()->pluginName() << oendl;

            int resultCode;
            int tries = 0;

            // perform authentication
            resultCode = iface->plugin()->authenticate();

            // display the result in command line
            QString resultMessage;
            switch (resultCode)
            {
                case MultiauthPluginObject::Success:
                    resultMessage = "Success!";
                    nbSuccess++;
                    break;
                case MultiauthPluginObject::Failure:
                    resultMessage = "Failure...";
                    break;
                case MultiauthPluginObject::Skip:
                    resultMessage = "Skip";
                    break;
            }
            odebug << "Plugin result: " << resultMessage << oendl;

            // if failure, wait, reperform, wait, reperform... until right
            while (resultCode == MultiauthPluginObject::Failure)
            {
                tries++;
                owarn << "This plugin has failed " << tries << " times already" << oendl;

                // displays owner information, if any
                if (oi)
                {
                    oi->exec();
                    odebug << "Contact information displayed" << oendl;
                }

                /// \todo parametrize the time penalty according to \em mode (exponential,
                /// linear or fixed) and \em basetime (time penalty for the first failure)
                sleep(2 * tries);

                if (oi)
                {
                    oi->hide();
                    /** \todo fix the focus here: should go back to the current plugin widget
                     * but it doesn't, so we have to tap once on the widget before e.g. buttons
                     * are active again
                     */
                    odebug << "Contact information hidden" << oendl;
                }

                // perform authentication
                resultCode = iface->plugin()->authenticate();

                // display the result in command line
                switch (resultCode)
                {
                    case MultiauthPluginObject::Success:
                        resultMessage = "Success!";
                        nbSuccess++;
                        break;
                    case MultiauthPluginObject::Failure:
                        resultMessage = "Failure...";
                        break;
                    case MultiauthPluginObject::Skip:
                        resultMessage = "Skip";
                        break;
                }
                odebug << "Plugin result: " << resultMessage << oendl;
            }
            delete lib;

            if (resultCode == MultiauthPluginObject::Success && nbSuccess == nbSuccessMin)
            {
                if(oi) delete oi;
                // we have reached the required number of successes, we can exit the plugin loop
                return 0;
            }
        } else {
            owarn << "Could not recognize plugin " << QString( path + "/" + *libIt ) << oendl;
            delete lib;
        } // end if plugin recognized
    } //end for
    if(oi) delete oi;
    return 1;
}

}
}
}