forked from lastpass/lastpass-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints-login.c
363 lines (319 loc) · 10.9 KB
/
endpoints-login.c
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
/*
* https endpoints for logging into LastPass
*
* Copyright (C) 2014-2018 LastPass.
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*
* See LICENSE.OpenSSL for more details regarding this exception.
*/
#include "endpoints.h"
#include "http.h"
#include "xml.h"
#include "password.h"
#include "config.h"
#include "util.h"
#include "upload-queue.h"
#include "version.h"
#include "terminal.h"
#include <sys/utsname.h>
#include <string.h>
struct multifactor_type {
const char *name;
const char *error_str;
const char *error_failure_str;
const char *post_var;
};
static struct multifactor_type multifactor_types[] = {
{
.name = "Google Authenticator Code",
.error_str = "googleauthrequired",
.error_failure_str = "googleauthfailed",
.post_var = "otp"
},
{
.name = "YubiKey OTP",
.error_str = "otprequired",
.error_failure_str = "otpfailed",
.post_var = "otp"
},
{
.name = "Sesame OTP",
.error_str = "sesameotprequired",
.error_failure_str = "sesameotpfailed",
.post_var = "sesameotp"
},
{
.name = "Out-of-Band OTP",
.error_str = "outofbandrequired",
.error_failure_str = "multifactorresponsefailed",
.post_var = "otp"
},
{
.name = "Microsoft Authenticator Code",
.error_str = "microsoftauthrequired",
.error_failure_str = "microsoftauthfailed",
.post_var = "otp"
}
};
static void filter_error_message(char *message)
{
char *nullit;
nullit = strstr(message, " Upgrade your browser extension so you can enter it.");
if (nullit)
*nullit = '\0';
}
static inline void append_post(char **args, const char *name, const char *val)
{
char **last = args;
while (*last && strcmp(*last, name))
++last;
*last = (char *)name;
*(last + 1) = (char *)val;
}
static char *calculate_trust_id(bool force)
{
char *trusted_id;
trusted_id = config_read_string("trusted_id");
if (force && !trusted_id) {
trusted_id = xcalloc(33, 1);
for (size_t i = 0; i < 32; ++i)
trusted_id[i] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$"[range_rand(0, 66)];
config_write_string("trusted_id", trusted_id);
}
return trusted_id;
}
static char *calculate_trust_label(void)
{
char *trusted_label;
struct utsname utsname;
if (uname(&utsname) < 0)
die_errno("Failed to determine uname.");
xasprintf(&trusted_label, "%s - %s %s", utsname.nodename, utsname.sysname, utsname.release);
return trusted_label;
}
static bool error_post(char **message, struct session **session)
{
*session = NULL;
if (message)
*message = xstrdup("Unable to post login request.");
return true;
}
static bool error_other(char **message, struct session **session, const char *txt)
{
*session = NULL;
if (message)
*message = xstrdup(txt);
return true;
}
static bool error_message(char **message, struct session **session, const char *reply)
{
*session = NULL;
if (message) {
*message = xml_error_cause(reply, "message");
if (*message)
filter_error_message(*message);
else
*message = xstrdup("Could not parse error message to login request.");
}
return true;
}
static bool ordinary_login(const char *login_server, const unsigned char key[KDF_HASH_LEN], char **args, char **cause, char **message, char **reply, struct session **session,
char **ret_login_server)
{
char *server;
free(*reply);
*reply = http_post_lastpass_v(login_server, "login.php", NULL, NULL, args);
if (!*reply)
return error_post(message, session);
*session = xml_ok_session(*reply, key);
if (*session) {
(*session)->server = xstrdup(login_server);
return true;
}
/* handle server redirection if requested for lastpass.eu */
server = xml_error_cause(*reply, "server");
if (server && strcmp(server, "lastpass.eu") == 0)
return ordinary_login(server, key, args, cause, message, reply, session, ret_login_server);
*cause = xml_error_cause(*reply, "cause");
if (!*cause)
return error_other(message, session, "Unable to determine login failure cause.");
*ret_login_server = xstrdup(login_server);
return false;
}
static inline bool has_capabilities(const char *capabilities, const char *capability)
{
_cleanup_free_ char *caps = xstrdup(capabilities);
char *token;
for (token = strtok(caps, ","); token; token = strtok(NULL, ",")) {
if (!strcmp(capability, token))
return true;
}
return false;
}
static bool oob_login(const char *login_server, const unsigned char key[KDF_HASH_LEN], char **args, char **message, char **reply, char **oob_name, struct session **session)
{
_cleanup_free_ char *oob_capabilities = NULL;
_cleanup_free_ char *cause = NULL;
_cleanup_free_ char *retryid = NULL;
bool can_do_passcode;
bool ret;
*oob_name = xml_error_cause(*reply, "outofbandname");
oob_capabilities = xml_error_cause(*reply, "capabilities");
if (!*oob_name || !oob_capabilities)
return error_other(message, session, "Could not determine out-of-band type.");
can_do_passcode = has_capabilities(oob_capabilities, "passcode");
if (can_do_passcode && !has_capabilities(oob_capabilities, "outofband")) {
xstrappend(oob_name, " OTP");
goto failure;
}
terminal_fprintf(stderr, TERMINAL_FG_YELLOW TERMINAL_BOLD "Waiting for approval of out-of-band %s login%s" TERMINAL_NO_BOLD "...", *oob_name, can_do_passcode ? ", or press Ctrl+C to enter a passcode" : "");
append_post(args, "outofbandrequest", "1");
for (;;) {
free(*reply);
*reply = http_post_lastpass_v(login_server, "login.php", NULL, NULL, args);
if (!*reply) {
if (can_do_passcode) {
append_post(args, "outofbandrequest", "0");
append_post(args, "outofbandretry", "0");
append_post(args, "outofbandretryid", "");
xstrappend(oob_name, " OTP");
goto failure;
} else {
error_post(message, session);
goto success;
}
}
*session = xml_ok_session(*reply, key);
if (*session) {
(*session)->server = xstrdup(login_server);
goto success;
}
free(cause);
cause = xml_error_cause(*reply, "cause");
if (cause && !strcmp(cause, "outofbandrequired")) {
free(retryid);
retryid = xml_error_cause(*reply, "retryid");
append_post(args, "outofbandretry", "1");
append_post(args, "outofbandretryid", retryid);
fprintf(stderr, ".");
continue;
}
error_message(message, session, *reply);
goto success;
}
success:
ret = true;
goto out;
failure:
ret = false;
goto out;
out:
terminal_fprintf(stderr, TERMINAL_RESET "\n" TERMINAL_UP_CURSOR(1) TERMINAL_CLEAR_DOWN);
return ret;
}
static bool otp_login(const char *login_server, const unsigned char key[KDF_HASH_LEN], char **args, char **message, char **reply, const char *otp_name, const char *cause, const char *username, struct session **session)
{
struct multifactor_type *replied_multifactor = NULL;
_cleanup_free_ char *multifactor = NULL;
_cleanup_free_ char *next_cause = NULL;
char *multifactor_error = NULL;
for (size_t i = 0; i < ARRAY_SIZE(multifactor_types); ++i) {
if (!strcmp(multifactor_types[i].error_str, cause)) {
replied_multifactor = &multifactor_types[i];
break;
}
}
if (!replied_multifactor)
return error_message(message, session, *reply);
for (;;) {
free(multifactor);
multifactor = password_prompt("Code", multifactor_error, "Please enter your %s for <%s>.", otp_name ? otp_name : replied_multifactor->name, username);
if (!multifactor)
return error_other(message, session, "Aborted multifactor authentication.");
append_post(args, replied_multifactor->post_var, multifactor);
free(*reply);
*reply = http_post_lastpass_v(login_server, "login.php", NULL, NULL, args);
if (!*reply)
return error_post(message, session);
*session = xml_ok_session(*reply, key);
if (*session) {
(*session)->server = xstrdup(login_server);
return true;
}
free(next_cause);
next_cause = xml_error_cause(*reply, "cause");
if (next_cause && !strcmp(next_cause, replied_multifactor->error_failure_str))
multifactor_error = "Invalid multifactor code; please try again.";
else
return error_message(message, session, *reply);
}
}
struct session *lastpass_login(const char *username, const char hash[KDF_HEX_LEN], const unsigned char key[KDF_HASH_LEN], int iterations, char **error_message, bool trust)
{
char *args[33];
_cleanup_free_ char *user_lower = NULL;
_cleanup_free_ char *iters = NULL;
_cleanup_free_ char *trusted_id = NULL;
_cleanup_free_ char *trusted_label = NULL;
_cleanup_free_ char *cause = NULL;
_cleanup_free_ char *reply = NULL;
_cleanup_free_ char *otp_name = NULL;
_cleanup_free_ char *login_server = NULL;
struct session *session = NULL;
iters = xultostr(iterations);
user_lower = xstrlower(username);
trusted_id = calculate_trust_id(trust);
memset(args, 0, sizeof(args));
append_post(args, "xml", "2");
append_post(args, "username", user_lower);
append_post(args, "hash", hash);
append_post(args, "iterations", iters);
append_post(args, "includeprivatekeyenc", "1");
append_post(args, "method", "cli");
append_post(args, "outofbandsupported", "1");
if (trusted_id)
append_post(args, "uuid", trusted_id);
if (ordinary_login(LASTPASS_SERVER, key, args, &cause, error_message, &reply, &session, &login_server))
return session;
if (trust) {
trusted_label = calculate_trust_label();
append_post(args, "trustlabel", trusted_label);
}
if (cause && !strcmp(cause, "outofbandrequired") &&
oob_login(login_server, key, args, error_message, &reply, &otp_name, &session)) {
if (trust)
http_post_lastpass("trust.php", session, NULL, "token", session->token, "uuid", trusted_id, "trustlabel", trusted_label, NULL);
return session;
}
if (otp_login(login_server, key, args, error_message, &reply, otp_name, cause, user_lower, &session))
return session;
error_other(error_message, &session, "An unspecified error occurred.");
return NULL;
}