/
login_googleauth.c
334 lines (300 loc) · 9.24 KB
/
login_googleauth.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
/*
* bsd_auth login program supporting Google's two-factor authentication
*
* Copyright (c) 2012 Chris Kruger <chris@krugerheavyindustries.com>
* Portions Copyright (c) 2010 Daniel Hartmeier <daniel@benzedrine.cx>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/param.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <ctype.h>
#include <login_cap.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "common.h"
#include "googleauth.h"
#define MODE_LOGIN 0
#define MODE_CHALLENGE 1
#define MODE_RESPONSE 2
#define AUTH_OK 0
#define AUTH_FAILED -1
FILE *back = NULL;
#define DBPATH "/var/db/googleauth";
static int seperate_code_and_password(char *code, char *password,
const char *unified);
static int clean_string(const char *);
static int googleauth_login(const char *, const char *);
int main(int argc, char *argv[])
{
int ch, ret, mode = MODE_LOGIN;
char *username;
char code[_PASSWORD_LEN];
char password[1024];
char response[1024];
char *unified = NULL;
char *wheel = NULL;
char *class = NULL;
int lastchance = 0;
memset(password, 0, sizeof(password));
memset(response, 0, sizeof(response));
setpriority(PRIO_PROCESS, 0, 0);
openlog(NULL, LOG_ODELAY, LOG_AUTH);
while ((ch = getopt(argc, argv, "dv:s:")) != -1) {
switch (ch) {
case 'd':
back = stdout;
break;
case 'v':
syslog(LOG_INFO, "-v %s", optarg);
break;
case 's':
if (!strcmp(optarg, "login"))
mode = MODE_LOGIN;
else if (!strcmp(optarg, "response"))
mode = MODE_RESPONSE;
else if (!strcmp(optarg, "challenge"))
mode = MODE_CHALLENGE;
else {
syslog(LOG_ERR, "%s: invalid service", optarg);
exit(EXIT_FAILURE);
}
break;
default:
syslog(LOG_ERR, "usage error1");
exit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
if (argc != 2 && argc != 1) {
syslog(LOG_ERR, "usage error2");
exit(EXIT_FAILURE);
}
username = argv[0];
/* passed by sshd(8) for non-existing users */
if (!strcmp(username, "NOUSER"))
exit(EXIT_FAILURE);
if (!clean_string(username)) {
syslog(LOG_ERR, "clean_string username");
exit(EXIT_FAILURE);
}
if (back == NULL && (back = fdopen(3, "r+")) == NULL) {
syslog(LOG_ERR, "user %s: fdopen: %m", username);
exit(EXIT_FAILURE);
}
switch (mode) {
case MODE_LOGIN:
if ((unified = getpass("Password:")) == NULL) {
syslog(LOG_ERR, "user %s: getpass (Password)",
username);
exit(EXIT_FAILURE);
}
#if 0
if ((password = getpass("Verification Code:")) == NULL) {
syslog(LOG_ERR, "user %s: getpass (Verification Code): %m",
username);
exit(EXIT_FAILURE);
}
strncpy(code, password, _PASSWORD_LEN);
if ((password = getpass("Password:")) == NULL) {
syslog(LOG_ERR, "user %s: getpass (Password): %m",
username);
exit(EXIT_FAILURE);
}
#endif
break;
case MODE_CHALLENGE:
/* see login.conf(5) section CHALLENGES */
fprintf(back, "%s\n", BI_SILENT);
//fprintf(back, BI_VALUE " challenge %s\n", "Verification Code:");
//fprintf(back, BI_CHALLENGE "\n");
//fprintf(back, BI_FDPASS "\n");
//fflush(back);
exit(EXIT_SUCCESS);
break;
case MODE_RESPONSE: {
/* see login.conf(5) section RESPONSES */
/* this happens e.g. when called from sshd(8) */
int count;
mode = 0;
count = -1;
while (++count < sizeof(response) &&
read(fileno(back), &response[count], (size_t)1) ==
(ssize_t)1) {
if (response[count] == '\0' && ++mode == 2)
break;
if (response[count] == '\0' && mode == 1) {
unified = response + count + 1;
}
}
#if 0
syslog(LOG_ERR, "got %s", unified);
#endif
if (mode < 2) {
syslog(LOG_ERR, "user %s: protocol error "
"on back channel", username);
exit(EXIT_FAILURE);
}
break;
}
}
if (seperate_code_and_password(code, password, unified)) {
syslog(LOG_ERR, "user %s: incorrect format", username);
exit(EXIT_FAILURE);
}
ret = googleauth_login(username, code);
if (ret == AUTH_OK)
ret = pwd_login(username, password, wheel, lastchance, class);
memset(password, 0, sizeof(password));
memset(response, 0, sizeof(response));
if (unified != NULL)
memset(unified, 0, strlen(unified));
if (ret != AUTH_OK) {
syslog(LOG_INFO, "user %s: reject", username);
fprintf(back, BI_REJECT "\n");
} else {
syslog(LOG_INFO, "user %s: accepted", username);
}
closelog();
exit(EXIT_SUCCESS);
}
static int
seperate_code_and_password(char *code, char *password, const char *unified)
{
// Verification are six digits starting with '0'..'9',
// scratch codes are eight digits starting with '1'..'9'
//
// OpenBSD provides strlcpy, which is a more robust string copy
// method. This code has been migrated to the strlcpy method
// from the strncpy method because strncpy was producing
// corrupt tokenization for code lengths larger than 7.
size_t len = strlen(unified);
size_t span = strspn(unified, "0123456789");
switch (span) {
case 6:
strlcpy(code, unified, 7);
strlcpy(password, unified + 6, len - 5);
return 0;
case 8:
strlcpy(code, unified, 9);
strlcpy(password, unified + 8, len - 7);
return 0;
}
return 1;
}
static int
clean_string(const char *s)
{
while (*s) {
if (!isalnum(*s) && *s != '-' && *s != '_')
return (0);
++s;
}
return (1);
}
static int
googleauth_login(const char *username, const char *password)
{
uint8_t *secret = NULL;
int secretLen = 0;
int code = 0;
char *endptr;
Config config;
if (read_cfg(username, &config)) {
syslog(LOG_INFO, "user %s: error in user conf file", username);
exit(EXIT_FAILURE);
}
long l = strtol(password, &endptr, 10);
if (l < 0 || *endptr) {
syslog(LOG_ERR, "user %s: failed to extract verification code",
username);
return (AUTH_FAILED);
}
code = (int)l;
if ((secret = get_shared_secret(username, &secretLen))) {
int advance = 0;
if (check_scratch_codes(username, code)) {
if (config.is_totp) {
if (check_timebased_code(&config, secret, secretLen, code)) {
#if 0
syslog(LOG_ERR, "user %s code %d: "
"check_timebased_code != 0",
username,
code);
#else
syslog(LOG_ERR, "user %s: "
"check_timebased_code != 0",
username);
#endif
if (secret) {
memset(secret, 0, secretLen);
free(secret);
}
return (AUTH_FAILED);
}
} else {
int hotp_counter = get_hotp_counter(username);
if (check_counterbased_code(&config, secret, secretLen, code,
hotp_counter, &advance)) {
syslog(LOG_ERR, "user %s: check_counterbased_code != 0",
username);
if (secret) {
memset(secret, 0, secretLen);
free(secret);
}
return (AUTH_FAILED);
}
// If an hotp login attempt has been made,
// the counter must always be advanced by at least one
if (advance) {
if (advance_counter_by(username, hotp_counter, advance)) {
syslog(LOG_ERR, "user %s: failed to advance hotp counter",
username);
if (secret) {
memset(secret, 0, secretLen);
free(secret);
}
return (AUTH_FAILED);
}
}
}
}
if (secret) {
memset(secret, 0, secretLen);
free(secret);
}
return (AUTH_OK);
}
return (AUTH_FAILED);
}