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
|
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
* 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 version 3 as
* published by the Free Software Foundation.
*
* 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/>.
*
* Authored by: Andrea Azzarone <andrea.azzarone@canonical.com>
*/
// Kindly inspired by user_authenticator_linux.cc of chromium project
// In the future would be nice to have something less static. For the moment
// let's just fallcback to lightdm.
#include "UserAuthenticatorPam.h"
#include "unity-shared/UnitySettings.h"
#include "UnityCore/GLibWrapper.h"
#include <cstring>
#include <security/pam_appl.h>
#include <vector>
namespace unity
{
namespace lockscreen
{
bool UserAuthenticatorPam::AuthenticateStart(std::string const& username,
AuthenticateEndCallback const& authenticate_cb)
{
if (pam_handle_)
return false;
first_prompt_ = true;
username_ = username;
authenticate_cb_ = authenticate_cb;
glib::Error error;
g_thread_try_new(nullptr, AuthenticationThreadFunc, this, &error);
return !error;
}
gpointer UserAuthenticatorPam::AuthenticationThreadFunc(gpointer data)
{
auto self = static_cast<UserAuthenticatorPam*>(data);
if (!self->InitPam() || !self->pam_handle_)
{
self->pam_handle_ = nullptr;
self->source_manager_.AddTimeout(0, [self] { self->start_failed.emit(); return false; });
return nullptr;
}
self->status_ = pam_authenticate(self->pam_handle_, 0);
if (self->status_ == PAM_SUCCESS)
{
int status2 = pam_acct_mgmt(self->pam_handle_, 0);
if (status2 == PAM_NEW_AUTHTOK_REQD)
status2 = pam_chauthtok(self->pam_handle_, PAM_CHANGE_EXPIRED_AUTHTOK);
if (unity::Settings::Instance().pam_check_account_type())
self->status_ = status2;
pam_setcred(self->pam_handle_, PAM_REINITIALIZE_CRED);
}
pam_end(self->pam_handle_, self->status_);
self->pam_handle_ = nullptr;
self->source_manager_.AddTimeout(0, [self] { self->authenticate_cb_(self->status_ == PAM_SUCCESS); return false; });
return nullptr;
}
bool UserAuthenticatorPam::InitPam()
{
pam_conv conversation;
conversation.conv = ConversationFunction;
conversation.appdata_ptr = static_cast<void*>(this);
return pam_start("unity", username_.c_str(),
&conversation, &pam_handle_) == PAM_SUCCESS;
}
int UserAuthenticatorPam::ConversationFunction(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;
UserAuthenticatorPam* user_auth = static_cast<UserAuthenticatorPam*>(appdata_ptr);
if (!user_auth->first_prompt_)
{
// Adding a timeout ensures that the signal is emitted in the main thread
user_auth->source_manager_.AddTimeout(0, [user_auth] { user_auth->clear_prompts.emit(); return false; });
}
user_auth->first_prompt_ = false;
bool raise_error = false;
int count;
std::vector<PromiseAuthCodePtr> promises;
for (count = 0; count < num_msg && !raise_error; ++count)
{
switch (msg[count]->msg_style)
{
case PAM_PROMPT_ECHO_ON:
{
auto promise = std::make_shared<std::promise<std::string>>();
promises.push_back(promise);
// Adding a timeout ensures that the signal is emitted in the main thread
std::string message(msg[count]->msg);
user_auth->source_manager_.AddTimeout(0, [user_auth, message, promise] { user_auth->echo_on_requested.emit(message, promise); return false; });
break;
}
case PAM_PROMPT_ECHO_OFF:
{
auto promise = std::make_shared<std::promise<std::string>>();
promises.push_back(promise);
// Adding a timeout ensures that the signal is emitted in the main thread
std::string message(msg[count]->msg);
user_auth->source_manager_.AddTimeout(0, [user_auth, message, promise] { user_auth->echo_off_requested.emit(message, promise); return false; });
break;
}
case PAM_TEXT_INFO:
{
// Adding a timeout ensures that the signal is emitted in the main thread
std::string message(msg[count]->msg);
user_auth->source_manager_.AddTimeout(0, [user_auth, message] { user_auth->message_requested.emit(message); return false; });
break;
}
default:
{
// Adding a timeout ensures that the signal is emitted in the main thread
std::string message(msg[count]->msg);
user_auth->source_manager_.AddTimeout(0, [user_auth, message] { user_auth->error_requested.emit(message); return false; });
}
}
}
int i = 0;
for (auto const& promise : promises)
{
auto future = promise->get_future();
pam_response* resp_item = &tmp_response[i++];
resp_item->resp_retcode = 0;
resp_item->resp = strdup(future.get().c_str());
if (!resp_item->resp)
{
raise_error = true;
break;
}
}
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;
}
}
} // lockscreen
} // unity
|