File: main.cpp

package info (click to toggle)
pinentry 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,912 kB
  • ctags: 1,081
  • sloc: ansic: 7,025; sh: 6,517; cpp: 922; makefile: 203
file content (356 lines) | stat: -rw-r--r-- 10,697 bytes parent folder | download
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* main.cpp - A Qt dialog for PIN entry.

   Copyright (C) 2002, 2008 Klarälvdalens Datakonsult AB (KDAB)
   Copyright (C) 2003 g10 Code GmbH
   Copyright 2007 Ingo Klöcker

   Written by Steffen Hansen <steffen@klaralvdalens-datakonsult.se>.
   Modified by Marcus Brinkmann <marcus@g10code.de>.
   Modified by Marc Mutz <marc@kdab.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "pinentryconfirm.h"
#include "pinentrydialog.h"
#include "pinentry.h"

#include <qapplication.h>
#include <QIcon>
#include <QString>
#include <qwidget.h>
#include <qmessagebox.h>
#include <QPushButton>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <memory>
#include <stdexcept>
#include <gpg-error.h>

#ifdef FALLBACK_CURSES
#include <pinentry-curses.h>
#endif

#if QT_VERSION >= 0x050000 && defined(QT_STATIC)
  #include <QtPlugin>
  #ifdef Q_OS_WIN
    #include <windows.h>
    #include <shlobj.h>
    Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
  #elif defined(Q_OS_MAC)
    Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin)
  #else
    Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
  #endif
#endif

static QString escape_accel(const QString &s)
{

    QString result;
    result.reserve(s.size());

    bool afterUnderscore = false;

    for (unsigned int i = 0, end = s.size() ; i != end ; ++i) {
        const QChar ch = s[i];
        if (ch == QLatin1Char('_')) {
            if (afterUnderscore) { // escaped _
                result += QLatin1Char('_');
                afterUnderscore = false;
            } else { // accel
                afterUnderscore = true;
            }
        } else {
            if (afterUnderscore ||  // accel
                    ch == QLatin1Char('&')) {  // escape & from being interpreted by Qt
                result += QLatin1Char('&');
            }
            result += ch;
            afterUnderscore = false;
        }
    }

    if (afterUnderscore)
        // trailing single underscore: shouldn't happen, but deal with it robustly:
    {
        result += QLatin1Char('_');
    }

    return result;
}

/* Hack for creating a QWidget with a "foreign" window ID */
class ForeignWidget : public QWidget
{
public:
    explicit ForeignWidget(WId wid) : QWidget(0)
    {
        QWidget::destroy();
        create(wid, false, false);
    }

    ~ForeignWidget()
    {
        destroy(false, false);
    }
};

namespace
{
class InvalidUtf8 : public std::invalid_argument
{
public:
    InvalidUtf8() : std::invalid_argument("invalid utf8") {}
    ~InvalidUtf8() throw() {}
};
}

static const bool GPG_AGENT_IS_PORTED_TO_ONLY_SEND_UTF8 = false;

static QString from_utf8(const char *s)
{
    const QString result = QString::fromUtf8(s);
    if (result.contains(QChar::ReplacementCharacter)) {
        if (GPG_AGENT_IS_PORTED_TO_ONLY_SEND_UTF8) {
            throw InvalidUtf8();
        } else {
            return QString::fromLocal8Bit(s);
        }
    }

    return result;
}

static int
qt_cmd_handler(pinentry_t pe)
{
    QWidget *parent = 0;

    /* FIXME: Add parent window ID to pinentry and GTK.  */
    if (pe->parent_wid) {
        parent = new ForeignWidget((WId) pe->parent_wid);
    }

    int want_pass = !!pe->pin;

    const QString ok =
        pe->ok             ? escape_accel(from_utf8(pe->ok)) :
        pe->default_ok     ? escape_accel(from_utf8(pe->default_ok)) :
        /* else */           QLatin1String("&OK") ;
    const QString cancel =
        pe->cancel         ? escape_accel(from_utf8(pe->cancel)) :
        pe->default_cancel ? escape_accel(from_utf8(pe->default_cancel)) :
        /* else */           QLatin1String("&Cancel") ;
    const QString title =
        pe->title ? from_utf8(pe->title) :
        /* else */  QLatin1String("pinentry-qt") ;
    const QString repeatError =
        pe->repeat_error_string ? from_utf8(pe->repeat_error_string) :
                                  QLatin1String("Passphrases do not match");
    const QString repeatString =
        pe->repeat_passphrase ? from_utf8(pe->repeat_passphrase) :
                                QString();
    const QString visibilityTT =
        pe->default_tt_visi ? from_utf8(pe->default_tt_visi) :
                              QLatin1String("Show passphrase");
    const QString hideTT =
        pe->default_tt_hide ? from_utf8(pe->default_tt_hide) :
                              QLatin1String("Hide passphrase");


    if (want_pass) {
        PinEntryDialog pinentry(parent, 0, pe->timeout, true, !!pe->quality_bar,
                                repeatString, visibilityTT, hideTT);

        pinentry.setPinentryInfo(pe);
        pinentry.setPrompt(escape_accel(from_utf8(pe->prompt)));
        pinentry.setDescription(from_utf8(pe->description));
        pinentry.setRepeatErrorText(repeatError);
        if (pe->title) {
            pinentry.setWindowTitle(from_utf8(pe->title));
        }

        /* If we reuse the same dialog window.  */
        pinentry.setPin(QString());

        pinentry.setOkText(ok);
        pinentry.setCancelText(cancel);
        if (pe->error) {
            pinentry.setError(from_utf8(pe->error));
        }
        if (pe->quality_bar) {
            pinentry.setQualityBar(from_utf8(pe->quality_bar));
        }
        if (pe->quality_bar_tt) {
            pinentry.setQualityBarTT(from_utf8(pe->quality_bar_tt));
        }
        bool ret = pinentry.exec();
        if (!ret) {
            if (pinentry.timedOut())
                pe->specific_err = gpg_error (GPG_ERR_TIMEOUT);
            return -1;
        }

        const QString pinStr = pinentry.pin();
        QByteArray pin = pinStr.toUtf8();

        if (!!pe->repeat_passphrase) {
            /* Should not have been possible to accept
               the dialog in that case but we do a safety
               check here */
            pe->repeat_okay = (pinStr == pinentry.repeatedPin());
        }

        int len = strlen(pin.constData());
        if (len >= 0) {
            pinentry_setbufferlen(pe, len + 1);
            if (pe->pin) {
                strcpy(pe->pin, pin.constData());
                return len;
            }
        }
        return -1;
    } else {
        const QString desc  = pe->description ? from_utf8(pe->description) : QString();
        const QString notok = pe->notok       ? escape_accel(from_utf8(pe->notok)) : QString();

        const QMessageBox::StandardButtons buttons =
            pe->one_button ? QMessageBox::Ok :
            pe->notok      ? QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel :
            /* else */       QMessageBox::Ok | QMessageBox::Cancel ;

        PinentryConfirm box(QMessageBox::Information, pe->timeout, title, desc, buttons, parent);

        const struct {
            QMessageBox::StandardButton button;
            QString label;
        } buttonLabels[] = {
            { QMessageBox::Ok,     ok     },
            { QMessageBox::Yes,    ok     },
            { QMessageBox::No,     notok  },
            { QMessageBox::Cancel, cancel },
        };

        for (size_t i = 0 ; i < sizeof buttonLabels / sizeof * buttonLabels ; ++i)
            if ((buttons & buttonLabels[i].button) && !buttonLabels[i].label.isEmpty()) {
                box.button(buttonLabels[i].button)->setText(buttonLabels[i].label);
#ifndef QT_NO_ACCESSIBILITY
                box.button(buttonLabels[i].button)->setAccessibleDescription(buttonLabels[i].label);
#endif
            }

        box.setIconPixmap(icon());

        if (!pe->one_button) {
            box.setDefaultButton(QMessageBox::Cancel);
        }

        box.show();
        raiseWindow(&box);

        const int rc = box.exec();

        if (rc == QMessageBox::Cancel) {
            pe->canceled = true;
        }
        if (box.timedOut()) {
          pe->specific_err = gpg_error (GPG_ERR_TIMEOUT);
        }

        return rc == QMessageBox::Ok || rc == QMessageBox::Yes ;

    }
}

static int
qt_cmd_handler_ex(pinentry_t pe)
{
    try {
        return qt_cmd_handler(pe);
    } catch (const InvalidUtf8 &) {
        pe->locale_err = true;
        return pe->pin ? -1 : false ;
    } catch (...) {
        pe->canceled = true;
        return pe->pin ? -1 : false ;
    }
}

pinentry_cmd_handler_t pinentry_cmd_handler = qt_cmd_handler_ex;

int
main(int argc, char *argv[])
{
    pinentry_init("pinentry-qt");

    std::auto_ptr<QApplication> app;

#ifdef FALLBACK_CURSES
    if (!pinentry_have_display(argc, argv)) {
        pinentry_cmd_handler = curses_cmd_handler;
        pinentry_set_flavor_flag ("curses");
    } else
#endif
    {
        /* Qt does only understand -display but not --display; thus we
           are fixing that here.  The code is pretty simply and may get
           confused if an argument is called "--display". */
        char **new_argv, *p;
        size_t n;
        int i, done;

        for (n = 0, i = 0; i < argc; i++) {
            n += strlen(argv[i]) + 1;
        }
        n++;
        new_argv = (char **)calloc(argc + 1, sizeof * new_argv);
        if (new_argv) {
            *new_argv = (char *)malloc(n);
        }
        if (!new_argv || !*new_argv) {
            fprintf(stderr, "pinentry-qt: can't fixup argument list: %s\n",
                    strerror(errno));
            exit(EXIT_FAILURE);

        }
        for (done = 0, p = *new_argv, i = 0; i < argc; i++)
            if (!done && !strcmp(argv[i], "--display")) {
                new_argv[i] = strcpy(p, argv[i] + 1);
                p += strlen(argv[i] + 1) + 1;
                done = 1;
            } else {
                new_argv[i] = strcpy(p, argv[i]);
                p += strlen(argv[i]) + 1;
            }

        /* We use a modal dialog window, so we don't need the application
           window anymore.  */
        i = argc;
        app.reset(new QApplication(i, new_argv));
        app->setWindowIcon(QIcon(QLatin1String(":/document-encrypt.png")));
    }

    pinentry_parse_opts(argc, argv);

    return pinentry_loop() ? EXIT_FAILURE : EXIT_SUCCESS ;
}