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
|
/*
* Copyright (C) 2013 Canonical Ltd.
*
* 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; version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* Author: Michael Terry <michael.terry@canonical.com>
*/
#include "Greeter.h"
#include "GreeterPrivate.h"
#include <QFuture>
#include <QFutureInterface>
#include <QFutureWatcher>
#include <QQueue>
#include <QtConcurrent>
#include <QVector>
#include <security/pam_appl.h>
namespace QLightDM
{
class GreeterImpl : public QObject
{
Q_OBJECT
struct AppData
{
GreeterImpl *impl;
pam_handle *handle;
};
typedef QFutureInterface<QString> ResponseFuture;
public:
explicit GreeterImpl(Greeter *parent, GreeterPrivate *greeterPrivate)
: QObject(parent),
greeter(parent),
greeterPrivate(greeterPrivate),
pamHandle(nullptr)
{
qRegisterMetaType<QLightDM::GreeterImpl::ResponseFuture>("QLightDM::GreeterImpl::ResponseFuture");
connect(&futureWatcher, &QFutureWatcher<int>::finished, this, &GreeterImpl::finishPam);
connect(this, SIGNAL(showMessage(pam_handle *, QString, QLightDM::Greeter::MessageType)),
this, SLOT(handleMessage(pam_handle *, QString, QLightDM::Greeter::MessageType)));
// This next connect is how we pass ResponseFutures between threads
connect(this, SIGNAL(showPrompt(pam_handle *, QString, QLightDM::Greeter::PromptType, QLightDM::GreeterImpl::ResponseFuture)),
this, SLOT(handlePrompt(pam_handle *, QString, QLightDM::Greeter::PromptType, QLightDM::GreeterImpl::ResponseFuture)),
Qt::BlockingQueuedConnection);
}
~GreeterImpl()
{
cancelPam();
}
void start(QString username)
{
// Clear out any existing PAM interactions first
cancelPam();
if (pamHandle != nullptr) {
// While we were cancelling pam above, we processed Qt events.
// Which may have allowed someone to call start() on us again.
// In which case, we'll bail on our current start() call.
// This isn't racy because it's all in the same thread.
return;
}
AppData *appData = new AppData();
appData->impl = this;
// Now actually start a new conversation with PAM
pam_conv conversation;
conversation.conv = converseWithPam;
conversation.appdata_ptr = static_cast<void*>(appData);
if (pam_start("lightdm", username.toUtf8(), &conversation, &pamHandle) == PAM_SUCCESS) {
appData->handle = pamHandle;
futureWatcher.setFuture(QtConcurrent::mapped(QList<pam_handle*>() << pamHandle, authenticateWithPam));
} else {
delete appData;
greeterPrivate->authenticated = false;
Q_EMIT greeter->showMessage(QStringLiteral("Internal error: could not start PAM authentication"), QLightDM::Greeter::MessageTypeError);
Q_EMIT greeter->authenticationComplete();
}
}
static int authenticateWithPam(pam_handle* const& pamHandle)
{
int pamStatus = pam_authenticate(pamHandle, 0);
if (pamStatus == PAM_SUCCESS) {
pamStatus = pam_acct_mgmt(pamHandle, 0);
}
if (pamStatus == PAM_NEW_AUTHTOK_REQD) {
pamStatus = pam_chauthtok(pamHandle, PAM_CHANGE_EXPIRED_AUTHTOK);
}
if (pamStatus == PAM_SUCCESS) {
pam_setcred(pamHandle, PAM_REINITIALIZE_CRED);
}
return pamStatus;
}
static int converseWithPam(int num_msg, const pam_message** msg,
pam_response** resp, void* appdata_ptr)
{
if (num_msg <= 0)
return PAM_CONV_ERR;
auto* tmp_response = static_cast<pam_response*>(calloc(num_msg, sizeof(pam_response)));
if (!tmp_response)
return PAM_CONV_ERR;
AppData *appData = static_cast<AppData*>(appdata_ptr);
GreeterImpl *impl = appData->impl;
pam_handle *handle = appData->handle;
int count;
QVector<ResponseFuture> responses;
for (count = 0; count < num_msg; ++count)
{
switch (msg[count]->msg_style)
{
case PAM_PROMPT_ECHO_ON:
{
QString message(msg[count]->msg);
responses.append(ResponseFuture());
responses.last().reportStarted();
Q_EMIT impl->showPrompt(handle, message, Greeter::PromptTypeQuestion, responses.last());
break;
}
case PAM_PROMPT_ECHO_OFF:
{
QString message(msg[count]->msg);
responses.append(ResponseFuture());
responses.last().reportStarted();
Q_EMIT impl->showPrompt(handle, message, Greeter::PromptTypeSecret, responses.last());
break;
}
case PAM_TEXT_INFO:
{
QString message(msg[count]->msg);
Q_EMIT impl->showMessage(handle, message, Greeter::MessageTypeInfo);
break;
}
default:
{
QString message(msg[count]->msg);
Q_EMIT impl->showMessage(handle, message, Greeter::MessageTypeError);
break;
}
}
}
int i = 0;
bool raise_error = false;
for (auto &response : responses)
{
pam_response* resp_item = &tmp_response[i++];
resp_item->resp_retcode = 0;
resp_item->resp = strdup(response.future().result().toUtf8());
if (!resp_item->resp)
{
raise_error = true;
break;
}
}
delete appData;
if (raise_error)
{
for (int i = 0; i < count; ++i)
free(tmp_response[i].resp);
free(tmp_response);
return PAM_CONV_ERR;
}
else
{
*resp = tmp_response;
return PAM_SUCCESS;
}
}
public Q_SLOTS:
bool respond(QString response)
{
if (!futures.isEmpty()) {
futures.dequeue().reportFinished(&response);
return true;
} else {
return false;
}
}
void cancelPam()
{
if (pamHandle != nullptr) {
QFuture<int> pamFuture = futureWatcher.future();
pam_handle *handle = pamHandle;
pamHandle = nullptr; // to disable normal finishPam() handling
pamFuture.cancel();
// Note the empty loop, we just want to clear the futures queue.
// Any further prompts from the pam thread will be immediately
// responded to/dismissed in handlePrompt().
while (respond(QString()));
pam_end(handle, PAM_CONV_ERR);
}
}
Q_SIGNALS:
void showMessage(pam_handle *handle, QString text, QLightDM::Greeter::MessageType type);
void showPrompt(pam_handle *handle, QString text, QLightDM::Greeter::PromptType type, QLightDM::GreeterImpl::ResponseFuture response);
private Q_SLOTS:
void finishPam()
{
if (pamHandle == nullptr) {
return;
}
int pamStatus = futureWatcher.result();
pam_end(pamHandle, pamStatus);
pamHandle = nullptr;
greeterPrivate->authenticated = (pamStatus == PAM_SUCCESS);
Q_EMIT greeter->authenticationComplete();
}
void handleMessage(pam_handle *handle, QString text, QLightDM::Greeter::MessageType type)
{
if (handle != pamHandle)
return;
Q_EMIT greeter->showMessage(text, type);
}
void handlePrompt(pam_handle *handle, QString text, QLightDM::Greeter::PromptType type, QLightDM::GreeterImpl::ResponseFuture future)
{
if (handle != pamHandle) {
future.reportResult(QString());
future.reportFinished();
return;
}
futures.enqueue(future);
Q_EMIT greeter->showPrompt(text, type);
}
private:
Greeter *greeter;
GreeterPrivate *greeterPrivate;
pam_handle* pamHandle;
QFutureWatcher<int> futureWatcher;
QQueue<ResponseFuture> futures;
};
GreeterPrivate::GreeterPrivate(Greeter* parent)
: authenticated(false),
authenticationUser(),
m_impl(new GreeterImpl(parent, this)),
q_ptr(parent)
{
}
GreeterPrivate::~GreeterPrivate()
{
delete m_impl;
}
void GreeterPrivate::handleAuthenticate()
{
m_impl->start(authenticationUser);
}
void GreeterPrivate::handleRespond(const QString &response)
{
m_impl->respond(response);
}
void GreeterPrivate::cancelAuthentication()
{
m_impl->cancelPam();
}
}
#include "GreeterPrivate.moc"
|