summaryrefslogtreecommitdiff
path: root/liblightdm-qt/greeter.cpp
blob: 9f8add3ffdb746966fda4ffea97498b8c6987fc1 (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
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
/*
 * Copyright (C) 2010-2011 David Edmundson
 * Copyright (C) 2010-2011 Robert Ancell
 * Author: David Edmundson <kde@davidedmundson.co.uk>
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
 * license.
 */


#include "QLightDM/greeter.h"

#include "config.h"

#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QVariant>
#include <QtCore/QSettings>
#include <QtCore/QUrl>
#include <QtCore/QFile>
#include <QtCore/QHash>
#include <QtCore/QSocketNotifier>
#include <QtDBus/QDBusPendingReply>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusReply>

#include <security/pam_appl.h>


/* Messages from the greeter to the server */
typedef enum
{
    GREETER_MESSAGE_CONNECT = 0,
    GREETER_MESSAGE_AUTHENTICATE,
    GREETER_MESSAGE_AUTHENTICATE_AS_GUEST,
    GREETER_MESSAGE_CONTINUE_AUTHENTICATION,
    GREETER_MESSAGE_START_SESSION,
    GREETER_MESSAGE_CANCEL_AUTHENTICATION,
    GREETER_MESSAGE_SET_LANGUAGE
} GreeterMessage;

/* Messages from the server to the greeter */
typedef enum
{
    SERVER_MESSAGE_CONNECTED = 0,
    SERVER_MESSAGE_PROMPT_AUTHENTICATION,
    SERVER_MESSAGE_END_AUTHENTICATION,
    SERVER_MESSAGE_SESSION_RESULT
} ServerMessage;

#define HEADER_SIZE 8

using namespace QLightDM;

class GreeterPrivate
{
public:
    QHash<QString, QString> hints;
    int toServerFd;
    int fromServerFd;
    QSocketNotifier *n;
    char *readBuffer;
    int nRead;
    bool inAuthentication;
    bool isAuthenticated;
    QString authenticationUser;
    int authenticateSequenceNumber;
    bool cancellingAuthentication;

    void writeInt(int value);
    void writeString(QString value);
    void writeHeader(int id, int length);
    void flush();
    char *readMessage(int *length, bool block);
};

Greeter::Greeter(QObject *parent) :
    QObject(parent),
    d(new GreeterPrivate)
{
    d->readBuffer = (char *)malloc(HEADER_SIZE);
    d->nRead = 0;
    d->authenticateSequenceNumber = 0;
}

Greeter::~Greeter()
{
    delete d->readBuffer;
    delete d;
}

static int intLength()
{
    return 4;
}

static int stringLength(QString value)
{
    QByteArray a = value.toUtf8();
    return intLength() + a.size();
}

void GreeterPrivate::writeInt(int value)
{
    char buffer[4];
    buffer[0] = value >> 24;
    buffer[1] = (value >> 16) & 0xFF;
    buffer[2] = (value >> 8) & 0xFF;
    buffer[3] = value & 0xFF;
    if (write(toServerFd, buffer, intLength()) != intLength()) {
        qDebug() << "Error writing to server";
    }
}

void GreeterPrivate::writeString(QString value)
{
    QByteArray a = value.toUtf8();
    writeInt(a.size());
    if (write(toServerFd, a.data(), a.size()) != a.size()) {
        qDebug() << "Error writing to server";
    }
}

void GreeterPrivate::writeHeader(int id, int length)
{
    writeInt(id);
    writeInt(length);
}

void GreeterPrivate::flush()
{
    fsync(toServerFd);
}

static int readInt(char *message, int messageLength, int *offset)
{
    if(messageLength - *offset < intLength()) {
        qDebug() << "Not enough space for int, need " << intLength() << ", got " << (messageLength - *offset);
        return 0;
    }

    char *buffer = message + *offset;
    int value = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
    *offset += intLength();
    return value;
}

static int getMessageLength(char *message, int messageLength)
{
    int offset = intLength();
    return readInt(message, messageLength, &offset);
}

static QString readString(char *message, int messageLength, int *offset)
{
    int length = readInt(message, messageLength, offset);
    if(messageLength - *offset < length) {
        qDebug() << "Not enough space for string, need " << length << ", got " << (messageLength - *offset);
        return "";
    }
    char *start = message + *offset;
    *offset += length;
    return QString::fromUtf8(start, length);
}

bool Greeter::connectSync()
{
    QDBusConnection busType = QDBusConnection::systemBus();
    QString ldmBus(qgetenv("LIGHTDM_BUS"));
    if(ldmBus == QLatin1String("SESSION")) {
        busType = QDBusConnection::sessionBus();
    }

    char* fd = getenv("LIGHTDM_TO_SERVER_FD");
    if(!fd) {
       qDebug() << "No LIGHTDM_TO_SERVER_FD environment variable";
       return false;
    }
    d->toServerFd = atoi(fd);

    qDebug() << "***connecting to server";
    QFile toServer;
    qDebug() << toServer.open(d->toServerFd, QIODevice::WriteOnly);

    fd = getenv("LIGHTDM_FROM_SERVER_FD");
    if(!fd) {
       qDebug() << "No LIGHTDM_FROM_SERVER_FD environment variable";
       return false;
    }
    d->fromServerFd = atoi(fd);

    d->n = new QSocketNotifier(d->fromServerFd, QSocketNotifier::Read);
    connect(d->n, SIGNAL(activated(int)), this, SLOT(onRead(int)));

    qDebug() << "Connecting to display manager...";
    d->writeHeader(GREETER_MESSAGE_CONNECT, stringLength(VERSION));
    d->writeString(VERSION);
    d->flush();

    int responseLength;
    char *response = d->readMessage(&responseLength, false);
    if (!response)
        return false;

    int offset = 0;
    int id = readInt(response, responseLength, &offset);
    int length = readInt(response, responseLength, &offset);
    bool connected = false;
    if (id == SERVER_MESSAGE_CONNECTED)
    {
        QString version = readString(response, responseLength, &offset);
        QString hintString = "";
        while (offset < length)
        {
            QString name = readString(response, responseLength, &offset);
            QString value = readString(response, responseLength, &offset);
            hintString.append (" ");
            hintString.append (name);
            hintString.append ("=");
            hintString.append (value);
        }

        qDebug() << "Connected version=" << version << hintString;
        connected = true;
    }
    else
        qDebug() << "Expected CONNECTED message, got " << id;
    free(response);

    return connected;
}

void Greeter::authenticate(const QString &username)
{
    d->inAuthentication = true;
    d->isAuthenticated = false;
    d->cancellingAuthentication = false;
    d->authenticationUser = username;
    qDebug() << "Starting authentication for user " << username << "...";
    d->writeHeader(GREETER_MESSAGE_AUTHENTICATE, intLength() + stringLength(username));
    d->authenticateSequenceNumber++;
    d->writeInt(d->authenticateSequenceNumber);
    d->writeString(username);
    d->flush();
}

void Greeter::authenticateAsGuest()
{
    d->authenticateSequenceNumber++;
    d->inAuthentication = true;
    d->isAuthenticated = false;
    d->cancellingAuthentication = false;
    d->authenticationUser = "";
    qDebug() << "Starting authentication for guest account";
    d->writeHeader(GREETER_MESSAGE_AUTHENTICATE_AS_GUEST, intLength());
    d->writeInt(d->authenticateSequenceNumber);
    d->flush();
}

void Greeter::respond(const QString &response)
{
    qDebug() << "Providing response to display manager";
    d->writeHeader(GREETER_MESSAGE_CONTINUE_AUTHENTICATION, intLength() + stringLength(response));
    // FIXME: Could be multiple response required
    d->writeInt(1);
    d->writeString(response);
    d->flush();
}

void Greeter::cancelAuthentication()
{
    qDebug() << "Cancelling authentication";
    d->cancellingAuthentication = true;
    d->writeHeader(GREETER_MESSAGE_CANCEL_AUTHENTICATION, 0);
    d->flush();
}

bool Greeter::inAuthentication() const
{
    return d->inAuthentication;
}

bool Greeter::isAuthenticated() const
{
    return d->isAuthenticated;
}

QString Greeter::authenticationUser() const
{
    return d->authenticationUser;
}

void Greeter::setLanguage (QString language)
{
    d->writeHeader(GREETER_MESSAGE_SET_LANGUAGE, stringLength(language));
    d->writeString (language);
    d->flush();
}

bool Greeter::startSessionSync(const QString &session)
{
    if (session == "")
        qDebug() << "Starting default session";
    else
        qDebug() << "Starting session " << session;

    d->writeHeader(GREETER_MESSAGE_START_SESSION, stringLength(session));
    d->writeString(session);
    d->flush();

    int responseLength;
    char *response = d->readMessage(&responseLength, false);
    if (!response)
        return false;

    int offset = 0;
    int id = readInt(response, responseLength, &offset);
    readInt(response, responseLength, &offset);
    int returnCode = -1;
    if (id == SERVER_MESSAGE_SESSION_RESULT)
        returnCode = readInt(response, responseLength, &offset);
    else
        qDebug() << "Expected SESSION_RESULT message, got " << id;
    free(response);

    return returnCode == 0;
}

char* GreeterPrivate::readMessage(int *length, bool block)
{
    /* Read the header, or the whole message if we already have that */
    int nToRead = HEADER_SIZE;
    if(nRead >= HEADER_SIZE)
        nToRead += getMessageLength(readBuffer, nRead);

    do
    {      
        ssize_t nRead = read(fromServerFd, readBuffer + nRead, nToRead - nRead);
        if(nRead < 0)
        {
            qDebug() << "Error reading from server";
            return NULL;
        }
        if (nRead == 0)
        {
            qDebug() << "EOF reading from server";
            return NULL;
        }

        qDebug() << "Read " << nRead << " octets from daemon";
        nRead += nRead;
    } while(nRead < nToRead && block);

    /* Stop if haven't got all the data we want */  
    if(nRead != nToRead)
        return NULL;

    /* If have header, rerun for content */
    if(nRead == HEADER_SIZE)
    {
        nToRead = getMessageLength(readBuffer, nRead);
        if(nToRead > 0)
        {
            readBuffer = (char *)realloc(readBuffer, HEADER_SIZE + nToRead);
            return readMessage(length, block);
        }
    }

    char *buffer = readBuffer;
    *length = nRead;

    readBuffer = (char *)malloc(nRead);
    nRead = 0;

    return buffer;
}

void Greeter::onRead(int fd)
{
    qDebug() << "Reading from server";

    int messageLength;
    char *message = d->readMessage(&messageLength, false);
    if (!message)
        return;

    int offset = 0;
    int id = readInt(message, messageLength, &offset);
    int length = readInt(message, messageLength, &offset);
    int nMessages, sequenceNumber, returnCode;
    QString version, username;
    switch(id)
    {
    case SERVER_MESSAGE_PROMPT_AUTHENTICATION:
        sequenceNumber = readInt(message, messageLength, &offset);
        username = readString(message, messageLength, &offset);

        d->authenticationUser = username;

        if (sequenceNumber == d->authenticateSequenceNumber &&
            !d->cancellingAuthentication)
        {
            nMessages = readInt(message, messageLength, &offset);
            qDebug() << "Prompt user with " << nMessages << " message(s)";
            for(int i = 0; i < nMessages; i++)
            {
                int style = readInt(message, messageLength, &offset);
                QString text = readString(message, messageLength, &offset);

                // FIXME: Should stop on prompts?
                switch (style)
                {
                case PAM_PROMPT_ECHO_OFF:
                    emit showPrompt(text, PROMPT_TYPE_SECRET);
                    break;
                case PAM_PROMPT_ECHO_ON:
                    emit showPrompt(text, PROMPT_TYPE_QUESTION);
                    break;
                case PAM_ERROR_MSG:
                    emit showMessage(text, MESSAGE_TYPE_ERROR);
                    break;
                case PAM_TEXT_INFO:
                    emit showMessage(text, MESSAGE_TYPE_INFO);
                    break;
                }
            }
        }
        break;
    case SERVER_MESSAGE_END_AUTHENTICATION:
        sequenceNumber = readInt(message, messageLength, &offset);
        username = readString(message, messageLength, &offset);
        returnCode = readInt(message, messageLength, &offset);

        if (sequenceNumber == d->authenticateSequenceNumber)
        {
            qDebug() << "Authentication complete with return code " << returnCode;

            d->cancellingAuthentication = false;
            d->isAuthenticated = (returnCode == 0);
            d->authenticationUser = username;
            d->inAuthentication = false;
            emit authenticationComplete();
        }
        else
            qDebug () << "Ignoring end authentication with invalid sequence number " << sequenceNumber;
        break;
    default:
        qDebug() << "Unknown message from server: " << id;
    }
    free(message);
}

QString Greeter::getHint(QString name) const
{
    return d->hints.value (name);
}

QString Greeter::defaultSessionHint() const
{
    return getHint ("default-session");
}

bool Greeter::hideUsersHint() const
{
    return d->hints.value ("hide-users", "true") == "true";
}

bool Greeter::hasGuestAccountHint() const
{
    return d->hints.value ("has-guest-account", "false") == "true";
}

QString Greeter::selectUserHint() const
{
    return getHint ("select-user");
}

bool Greeter::selectGuestHint() const
{
    return d->hints.value ("select-guest", "false") == "true";
}

QString Greeter::autologinUserHint() const
{
    return getHint ("autologin-user");
}

bool Greeter::autologinGuestHint() const
{
    return d->hints.value ("autologin-guest", "false") == "true";
}

int Greeter::autologinTimeoutHint() const
{
    return d->hints.value ("autologin-timeout", "0").toInt ();
}

#include "greeter_moc.cpp"