File: saslclient.cpp

package info (click to toggle)
qca2 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,884 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (561 lines) | stat: -rw-r--r-- 18,291 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
 Copyright (C) 2003-2008  Justin Karneges <justin@affinix.com>
 Copyright (C) 2006  Michail Pishchagin

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <QCoreApplication>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
#include <cstdio>

// QtCrypto has the declarations for all of QCA
#include <QtCrypto>

#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif

static QString prompt(const QString &s)
{
    printf("* %s ", qPrintable(s));
    fflush(stdout);
    char line[256];
    fgets(line, 255, stdin);
    QString result = QString::fromLatin1(line);
    if (result[result.length() - 1] == QLatin1Char('\n'))
        result.truncate(result.length() - 1);
    return result;
}

static QString socketErrorToString(QAbstractSocket::SocketError x)
{
    QString s;
    switch (x) {
    case QAbstractSocket::ConnectionRefusedError:
        s = QStringLiteral("connection refused or timed out");
        break;
    case QAbstractSocket::RemoteHostClosedError:
        s = QStringLiteral("remote host closed the connection");
        break;
    case QAbstractSocket::HostNotFoundError:
        s = QStringLiteral("host not found");
        break;
    case QAbstractSocket::SocketAccessError:
        s = QStringLiteral("access error");
        break;
    case QAbstractSocket::SocketResourceError:
        s = QStringLiteral("too many sockets");
        break;
    case QAbstractSocket::SocketTimeoutError:
        s = QStringLiteral("operation timed out");
        break;
    case QAbstractSocket::DatagramTooLargeError:
        s = QStringLiteral("datagram was larger than system limit");
        break;
    case QAbstractSocket::NetworkError:
        s = QStringLiteral("network error");
        break;
    case QAbstractSocket::AddressInUseError:
        s = QStringLiteral("address is already in use");
        break;
    case QAbstractSocket::SocketAddressNotAvailableError:
        s = QStringLiteral("address does not belong to the host");
        break;
    case QAbstractSocket::UnsupportedSocketOperationError:
        s = QStringLiteral("operation is not supported by the local operating system");
        break;
    default:
        s = QStringLiteral("unknown socket error");
        break;
    }
    return s;
}

static QString saslAuthConditionToString(QCA::SASL::AuthCondition x)
{
    QString s;
    switch (x) {
    case QCA::SASL::NoMechanism:
        s = QStringLiteral("no appropriate mechanism could be negotiated");
        break;
    case QCA::SASL::BadProtocol:
        s = QStringLiteral("bad SASL protocol");
        break;
    case QCA::SASL::BadServer:
        s = QStringLiteral("server failed mutual authentication");
        break;
    // AuthFail or unknown (including those defined for server only)
    default:
        s = QStringLiteral("generic authentication failure");
        break;
    };
    return s;
}

class ClientTest : public QObject
{
    Q_OBJECT

private:
    QString     host, proto, authzid, realm, user, pass;
    int         port;
    bool        no_authzid, no_realm;
    int         mode; // 0 = receive mechanism list, 1 = sasl negotiation, 2 = app
    QTcpSocket *sock;
    QCA::SASL  *sasl;
    QByteArray  inbuf;
    bool        sock_done;
    int         waitCycles;

public:
    ClientTest(const QString &_host,
               int            _port,
               const QString &_proto,
               const QString &_authzid,
               const QString &_realm,
               const QString &_user,
               const QString &_pass,
               bool           _no_authzid,
               bool           _no_realm)
        : host(_host)
        , proto(_proto)
        , authzid(_authzid)
        , realm(_realm)
        , user(_user)
        , pass(_pass)
        , port(_port)
        , no_authzid(_no_authzid)
        , no_realm(_no_realm)
        , sock_done(false)
        , waitCycles(0)
    {
        sock = new QTcpSocket(this);
        connect(sock, &QTcpSocket::connected, this, &ClientTest::sock_connected);
        connect(sock, &QTcpSocket::readyRead, this, &ClientTest::sock_readyRead);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
        connect(sock, &QTcpSocket::errorOccurred, this, &ClientTest::sock_error);
#else
        connect(sock, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error), this, &ClientTest::sock_error);
#endif

        sasl = new QCA::SASL(this);
        connect(sasl, &QCA::SASL::clientStarted, this, &ClientTest::sasl_clientFirstStep);
        connect(sasl, &QCA::SASL::nextStep, this, &ClientTest::sasl_nextStep);
        connect(sasl, &QCA::SASL::needParams, this, &ClientTest::sasl_needParams);
        connect(sasl, &QCA::SASL::authenticated, this, &ClientTest::sasl_authenticated);
        connect(sasl, &QCA::SASL::readyRead, this, &ClientTest::sasl_readyRead);
        connect(sasl, &QCA::SASL::readyReadOutgoing, this, &ClientTest::sasl_readyReadOutgoing);
        connect(sasl, &QCA::SASL::error, this, &ClientTest::sasl_error);
    }

public Q_SLOTS:
    void start()
    {
        mode = 0; // mech list mode

        int flags = 0;
        flags |= QCA::SASL::AllowPlain;
        flags |= QCA::SASL::AllowAnonymous;
        sasl->setConstraints((QCA::SASL::AuthFlags)flags, 0, 256);

        if (!user.isEmpty())
            sasl->setUsername(user);
        if (!authzid.isEmpty())
            sasl->setAuthzid(authzid);
        if (!pass.isEmpty())
            sasl->setPassword(pass.toUtf8());
        if (!realm.isEmpty())
            sasl->setRealm(realm);

        printf("Connecting to %s:%d, for protocol %s\n", qPrintable(host), port, qPrintable(proto));
        sock->connectToHost(host, port);
    }

Q_SIGNALS:
    void quit();

private Q_SLOTS:
    void sock_connected()
    {
        printf("Connected to server.  Awaiting mechanism list...\n");
    }

    void sock_error(QAbstractSocket::SocketError x)
    {
        if (x == QAbstractSocket::RemoteHostClosedError) {
            if (mode == 2) // app mode, where disconnect means completion
            {
                sock_done = true;
                tryFinished();
                return;
            } else // any other mode, where disconnect is an error
            {
                printf("Error: server closed connection unexpectedly.\n");
                emit quit();
                return;
            }
        }

        printf("Error: socket: %s\n", qPrintable(socketErrorToString(x)));
        emit quit();
    }

    void sock_readyRead()
    {
        if (mode == 2) // app mode
        {
            QByteArray a = sock->readAll();
            printf("Read %d bytes\n", int(a.size()));

            // there is a possible flaw in the qca 2.0 api, in
            //   that if sasl data is received from the peer
            //   followed by a disconnect from the peer, there is
            //   no clear approach to salvaging the bytes.  tls is
            //   not affected because tls has the concept of
            //   closing a session.  with sasl, there is no
            //   closing, and since the qca api is asynchronous,
            //   we could potentially wait forever for decoded
            //   data, if the last write was a partial packet.
            //
            // for now, we can perform a simple workaround of
            //   waiting at least three event loop cycles for
            //   decoded data before giving up and assuming the
            //   last write was partial.  the fact is, all current
            //   qca sasl providers respond within this time
            //   frame, so this fix should work fine for now.  in
            //   qca 2.1, we should revise the api to handle this
            //   situation better.
            //
            // further note: i guess this only affects application
            //   protocols that have no close message of their
            //   own, and rely on the tcp-level close.  examples
            //   are http, and of course this qcatest protocol.
            if (waitCycles == 0) {
                waitCycles = 3;
                QMetaObject::invokeMethod(this, "waitWriteIncoming", Qt::QueuedConnection);
            }

            sasl->writeIncoming(a);
        } else // mech list or sasl negotiation mode
        {
            if (sock->canReadLine()) {
                QString line = QString::fromLatin1(sock->readLine());
                line.truncate(line.length() - 1); // chop the newline
                handleLine(line);
            }
        }
    }

    void sasl_clientFirstStep(bool clientInit, const QByteArray &clientInitData)
    {
        printf("Choosing mech: %s\n", qPrintable(sasl->mechanism()));
        QString line = sasl->mechanism();
        if (clientInit) {
            line += QLatin1Char(' ');
            line += arrayToString(clientInitData);
        }
        sendLine(line);
    }

    void sasl_nextStep(const QByteArray &stepData)
    {
        QString line = QStringLiteral("C");
        if (!stepData.isEmpty()) {
            line += QLatin1Char(',');
            line += arrayToString(stepData);
        }
        sendLine(line);
    }

    void sasl_needParams(const QCA::SASL::Params &params)
    {
        if (params.needUsername()) {
            user = prompt(QStringLiteral("Username:"));
            sasl->setUsername(user);
        }

        if (params.canSendAuthzid() && !no_authzid) {
            authzid = prompt(QStringLiteral("Authorize As (enter to skip):"));
            if (!authzid.isEmpty())
                sasl->setAuthzid(authzid);
        }

        if (params.needPassword()) {
            QCA::ConsolePrompt prompt;
            prompt.getHidden(QStringLiteral("* Password"));
            prompt.waitForFinished();
            QCA::SecureArray pass = prompt.result();
            sasl->setPassword(pass);
        }

        if (params.canSendRealm() && !no_realm) {
            QStringList realms = sasl->realmList();
            printf("Available realms:\n");
            if (realms.isEmpty())
                printf("  (none specified)\n");
            foreach (const QString &s, realms)
                printf("  %s\n", qPrintable(s));
            realm = prompt(QStringLiteral("Realm (enter to skip):"));
            if (!realm.isEmpty())
                sasl->setRealm(realm);
        }

        sasl->continueAfterParams();
    }

    void sasl_authenticated()
    {
        printf("SASL success!\n");
        printf("SSF: %d\n", sasl->ssf());
    }

    void sasl_readyRead()
    {
        QByteArray a = sasl->read();
        inbuf += a;
        processInbuf();
    }

    void sasl_readyReadOutgoing()
    {
        QByteArray a = sasl->readOutgoing();
        sock->write(a);
    }

    void sasl_error()
    {
        int e = sasl->errorCode();
        if (e == QCA::SASL::ErrorInit)
            printf("Error: sasl: initialization failed.\n");
        else if (e == QCA::SASL::ErrorHandshake)
            printf("Error: sasl: %s.\n", qPrintable(saslAuthConditionToString(sasl->authCondition())));
        else if (e == QCA::SASL::ErrorCrypt)
            printf("Error: sasl: broken security layer.\n");
        else
            printf("Error: sasl: unknown error.\n");

        emit quit();
    }

    void waitWriteIncoming()
    {
        --waitCycles;
        if (waitCycles > 0) {
            QMetaObject::invokeMethod(this, "waitWriteIncoming", Qt::QueuedConnection);
            return;
        }

        tryFinished();
    }

private:
    void tryFinished()
    {
        if (sock_done && waitCycles == 0) {
            printf("Finished, server closed connection.\n");

            // if we give up on waiting for a response to
            //   writeIncoming, then it might come late.  in
            //   theory this shouldn't happen if we wait enough
            //   cycles, but if one were to arrive then it could
            //   occur between the request to quit the app and
            //   the actual quit of the app.  to assist with
            //   debugging, then, we'll explicitly stop listening
            //   for signals here.  otherwise the response may
            //   still be received and displayed, giving a false
            //   sense of correctness.
            sasl->disconnect(this);

            emit quit();
        }
    }

    QString arrayToString(const QByteArray &ba)
    {
        return QCA::Base64().arrayToString(ba);
    }

    QByteArray stringToArray(const QString &s)
    {
        return QCA::Base64().stringToArray(s).toByteArray();
    }

    void sendLine(const QString &line)
    {
        printf("Writing: {%s}\n", qPrintable(line));
        QString    s = line + QLatin1Char('\n');
        QByteArray a = s.toUtf8();
        if (mode == 2)      // app mode
            sasl->write(a); // write to sasl
        else                // mech list or sasl negotiation
            sock->write(a); // write to socket
    }

    void processInbuf()
    {
        // collect completed lines from inbuf
        QStringList list;
        int         at;
        while ((at = inbuf.indexOf('\n')) != -1) {
            list += QString::fromUtf8(inbuf.mid(0, at));
            inbuf = inbuf.mid(at + 1);
        }

        // process the lines
        foreach (const QString &line, list)
            handleLine(line);
    }

    void handleLine(const QString &line)
    {
        printf("Reading: [%s]\n", qPrintable(line));
        if (mode == 0) {
            // first line is the method list
            const QStringList mechlist = line.split(QLatin1Char(' '));
            mode                       = 1; // switch to sasl negotiation mode
            sasl->startClient(proto, host, mechlist);
        } else if (mode == 1) {
            QString type, rest;
            int     n = line.indexOf(QLatin1Char(','));
            if (n != -1) {
                type = line.mid(0, n);
                rest = line.mid(n + 1);
            } else
                type = line;

            if (type == QLatin1String("C")) {
                sasl->putStep(stringToArray(rest));
            } else if (type == QLatin1String("E")) {
                if (!rest.isEmpty())
                    printf("Error: server says: %s.\n", qPrintable(rest));
                else
                    printf("Error: server error, unspecified.\n");
                emit quit();
                return;
            } else if (type == QLatin1String("A")) {
                printf("Authentication success.\n");
                mode = 2; // switch to app mode

                // at this point, the server may send us text
                //   lines for us to display and then close.

                sock_readyRead(); // any extra data?
                return;
            } else {
                printf("Error: Bad format from peer, closing.\n");
                emit quit();
                return;
            }
        }
    }
};

void usage()
{
    printf("usage: saslclient (options) host(:port) (user) (pass)\n");
    printf("options: --proto=x, --authzid=x, --realm=x\n");
}

int main(int argc, char **argv)
{
    QCA::Initializer init;
    QCoreApplication qapp(argc, argv);

    QStringList args = qapp.arguments();
    args.removeFirst();

    // options
    QString proto = QStringLiteral("qcatest"); // default protocol
    QString authzid, realm;
    bool    no_authzid = false;
    bool    no_realm   = false;
    for (int n = 0; n < args.count(); ++n) {
        if (!args[n].startsWith(QLatin1String("--")))
            continue;

        QString opt = args[n].mid(2);
        QString var, val;
        int     at = opt.indexOf(QLatin1Char('='));
        if (at != -1) {
            var = opt.mid(0, at);
            val = opt.mid(at + 1);
        } else
            var = opt;

        if (var == QLatin1String("proto")) {
            proto = val;
        } else if (var == QLatin1String("authzid")) {
            // specifying empty authzid means force unspecified
            if (val.isEmpty())
                no_authzid = true;
            else
                authzid = val;
        } else if (var == QLatin1String("realm")) {
            // specifying empty realm means force unspecified
            if (val.isEmpty())
                no_realm = true;
            else
                realm = val;
        }

        args.removeAt(n);
        --n; // adjust position
    }

    if (args.count() < 1) {
        usage();
        return 0;
    }

    QString host, user, pass;
    int     port = 8001; // default port

    QString hostinput = args[0];
    if (args.count() >= 2)
        user = args[1];
    if (args.count() >= 3)
        pass = args[2];

    int at = hostinput.indexOf(QLatin1Char(':'));
    if (at != -1) {
        host = hostinput.mid(0, at);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 2)
        port = QStringView(hostinput).mid(at + 1).toInt();
#else
        port = hostinput.midRef(at + 1).toInt();
#endif
    } else
        host = hostinput;

    if (!QCA::isSupported("sasl")) {
        printf("Error: SASL support not found.\n");
        return 1;
    }

    ClientTest client(host, port, proto, authzid, realm, user, pass, no_authzid, no_realm);
    QObject::connect(&client, &ClientTest::quit, &qapp, &QCoreApplication::quit);
    QTimer::singleShot(0, &client, &ClientTest::start);
    qapp.exec();

    return 0;
}

#include "saslclient.moc"