2 * Copyright (C) 2013 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Author: Michael Terry <michael.terry@canonical.com>
20#include "GreeterPrivate.h"
22#include <QFutureInterface>
23#include <QFutureWatcher>
25#include <QtConcurrent>
27#include <security/pam_appl.h>
32class GreeterImpl : public QObject
42 typedef QFutureInterface<QString> ResponseFuture;
45 explicit GreeterImpl(Greeter *parent, GreeterPrivate *greeterPrivate)
48 greeterPrivate(greeterPrivate),
51 qRegisterMetaType<QLightDM::GreeterImpl::ResponseFuture>("QLightDM::GreeterImpl::ResponseFuture");
53 connect(&futureWatcher, &QFutureWatcher<int>::finished, this, &GreeterImpl::finishPam);
54 connect(this, SIGNAL(showMessage(pam_handle *, QString, QLightDM::Greeter::MessageType)),
55 this, SLOT(handleMessage(pam_handle *, QString, QLightDM::Greeter::MessageType)));
56 // This next connect is how we pass ResponseFutures between threads
57 connect(this, SIGNAL(showPrompt(pam_handle *, QString, QLightDM::Greeter::PromptType, QLightDM::GreeterImpl::ResponseFuture)),
58 this, SLOT(handlePrompt(pam_handle *, QString, QLightDM::Greeter::PromptType, QLightDM::GreeterImpl::ResponseFuture)),
59 Qt::BlockingQueuedConnection);
67 void start(QString username)
69 // Clear out any existing PAM interactions first
71 if (pamHandle != nullptr) {
72 // While we were cancelling pam above, we processed Qt events.
73 // Which may have allowed someone to call start() on us again.
74 // In which case, we'll bail on our current start() call.
75 // This isn't racy because it's all in the same thread.
79 AppData *appData = new AppData();
82 // Now actually start a new conversation with PAM
83 pam_conv conversation;
84 conversation.conv = converseWithPam;
85 conversation.appdata_ptr = static_cast<void*>(appData);
87 if (pam_start("lightdm", username.toUtf8(), &conversation, &pamHandle) == PAM_SUCCESS) {
88 appData->handle = pamHandle;
89 futureWatcher.setFuture(QtConcurrent::mapped(QList<pam_handle*>() << pamHandle, authenticateWithPam));
92 greeterPrivate->authenticated = false;
93 Q_EMIT greeter->showMessage(QStringLiteral("Internal error: could not start PAM authentication"), QLightDM::Greeter::MessageTypeError);
94 Q_EMIT greeter->authenticationComplete();
98 static int authenticateWithPam(pam_handle* const& pamHandle)
100 int pamStatus = pam_authenticate(pamHandle, 0);
101 if (pamStatus == PAM_SUCCESS) {
102 pamStatus = pam_acct_mgmt(pamHandle, 0);
104 if (pamStatus == PAM_NEW_AUTHTOK_REQD) {
105 pamStatus = pam_chauthtok(pamHandle, PAM_CHANGE_EXPIRED_AUTHTOK);
107 if (pamStatus == PAM_SUCCESS) {
108 pam_setcred(pamHandle, PAM_REINITIALIZE_CRED);
113 static int converseWithPam(int num_msg, const pam_message** msg,
114 pam_response** resp, void* appdata_ptr)
119 auto* tmp_response = static_cast<pam_response*>(calloc(num_msg, sizeof(pam_response)));
123 AppData *appData = static_cast<AppData*>(appdata_ptr);
124 GreeterImpl *impl = appData->impl;
125 pam_handle *handle = appData->handle;
128 QVector<ResponseFuture> responses;
130 for (count = 0; count < num_msg; ++count)
132 switch (msg[count]->msg_style)
134 case PAM_PROMPT_ECHO_ON:
136 QString message(msg[count]->msg);
137 responses.append(ResponseFuture());
138 responses.last().reportStarted();
139 Q_EMIT impl->showPrompt(handle, message, Greeter::PromptTypeQuestion, responses.last());
142 case PAM_PROMPT_ECHO_OFF:
144 QString message(msg[count]->msg);
145 responses.append(ResponseFuture());
146 responses.last().reportStarted();
147 Q_EMIT impl->showPrompt(handle, message, Greeter::PromptTypeSecret, responses.last());
152 QString message(msg[count]->msg);
153 Q_EMIT impl->showMessage(handle, message, Greeter::MessageTypeInfo);
158 QString message(msg[count]->msg);
159 Q_EMIT impl->showMessage(handle, message, Greeter::MessageTypeError);
166 bool raise_error = false;
168 for (auto &response : responses)
170 pam_response* resp_item = &tmp_response[i++];
171 resp_item->resp_retcode = 0;
172 resp_item->resp = strdup(response.future().result().toUtf8());
174 if (!resp_item->resp)
185 for (int i = 0; i < count; ++i)
186 free(tmp_response[i].resp);
193 *resp = tmp_response;
199 bool respond(QString response)
201 if (!futures.isEmpty()) {
202 futures.dequeue().reportFinished(&response);
210 void showMessage(pam_handle *handle, QString text, QLightDM::Greeter::MessageType type);
211 void showPrompt(pam_handle *handle, QString text, QLightDM::Greeter::PromptType type, QLightDM::GreeterImpl::ResponseFuture response);
216 if (pamHandle == nullptr) {
220 int pamStatus = futureWatcher.result();
222 pam_end(pamHandle, pamStatus);
225 greeterPrivate->authenticated = (pamStatus == PAM_SUCCESS);
226 Q_EMIT greeter->authenticationComplete();
229 void handleMessage(pam_handle *handle, QString text, QLightDM::Greeter::MessageType type)
231 if (handle != pamHandle)
234 Q_EMIT greeter->showMessage(text, type);
237 void handlePrompt(pam_handle *handle, QString text, QLightDM::Greeter::PromptType type, QLightDM::GreeterImpl::ResponseFuture future)
239 if (handle != pamHandle) {
240 future.reportResult(QString());
241 future.reportFinished();
245 futures.enqueue(future);
246 Q_EMIT greeter->showPrompt(text, type);
252 if (pamHandle != nullptr) {
253 QFuture<int> pamFuture = futureWatcher.future();
254 pam_handle *handle = pamHandle;
255 pamHandle = nullptr; // to disable normal finishPam() handling
258 // Note the empty loop, we just want to clear the futures queue.
259 // Any further prompts from the pam thread will be immediately
260 // responded to/dismissed in handlePrompt().
261 while (respond(QString()));
263 // Now let signal/slot handling happen so the thread can finish
264 while (!pamFuture.isFinished()) {
265 QCoreApplication::processEvents();
268 pam_end(handle, PAM_CONV_ERR);
273 GreeterPrivate *greeterPrivate;
274 pam_handle* pamHandle;
275 QFutureWatcher<int> futureWatcher;
276 QQueue<ResponseFuture> futures;
279GreeterPrivate::GreeterPrivate(Greeter* parent)
280 : authenticated(false),
281 authenticationUser(),
282 m_impl(new GreeterImpl(parent, this)),
287GreeterPrivate::~GreeterPrivate()
292void GreeterPrivate::handleAuthenticate()
294 m_impl->start(authenticationUser);
297void GreeterPrivate::handleRespond(const QString &response)
299 m_impl->respond(response);
304#include "GreeterPrivate.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list