-
Notifications
You must be signed in to change notification settings - Fork 17
/
pam_passwdqc.c
558 lines (490 loc) · 16 KB
/
pam_passwdqc.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
* Copyright (c) 2000-2003,2005,2012,2016,2019,2021 by Solar Designer
* Copyright (c) 2017,2018 by Dmitry V. Levin
* Copyright (c) 2017,2018 by Oleg Solovyov
* See LICENSE
*/
#define _XOPEN_SOURCE 600
#define _XOPEN_SOURCE_EXTENDED
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <pwd.h>
#ifdef HAVE_SHADOW
#include <shadow.h>
#endif
#ifdef HAVE_LIBAUDIT
#include <security/pam_modutil.h>
#include <libaudit.h>
#endif
#define PAM_SM_PASSWORD
#ifndef LINUX_PAM
#include <security/pam_appl.h>
#endif
#include <security/pam_modules.h>
#include "pam_macros.h"
#if !defined(PAM_EXTERN) && !defined(PAM_STATIC)
#define PAM_EXTERN extern
#endif
#if !defined(PAM_AUTHTOK_RECOVERY_ERR) && defined(PAM_AUTHTOK_RECOVER_ERR)
#define PAM_AUTHTOK_RECOVERY_ERR PAM_AUTHTOK_RECOVER_ERR
#endif
#if (defined(__sun) || defined(__hpux) || defined(_AIX)) && \
!defined(LINUX_PAM) && !defined(_OPENPAM)
/* Sun's PAM doesn't use const here, while Linux-PAM and OpenPAM do */
#define lo_const
#else
#define lo_const const
#endif
#ifdef _OPENPAM
/* OpenPAM doesn't use const here, while Linux-PAM does */
#define l_const
#else
#define l_const lo_const
#endif
typedef lo_const void *pam_item_t;
#include "passwdqc.h"
#include "passwdqc_i18n.h"
#define PROMPT_OLDPASS \
_("Enter current password: ")
#define PROMPT_NEWPASS1 \
_("Enter new password: ")
#define PROMPT_NEWPASS2 \
_("Re-type new password: ")
#define MESSAGE_MISCONFIGURED \
_("System configuration error. Please contact your administrator.")
#define MESSAGE_INVALID_OPTION \
"pam_passwdqc: %s."
#define MESSAGE_INTRO_PASSWORD \
_("\nYou can now choose the new password.\n")
#define MESSAGE_INTRO_BOTH \
_("\nYou can now choose the new password or passphrase.\n")
#define MESSAGE_EXPLAIN_PASSWORD_1_CLASS(count) \
P3_( \
"A good password should be a mix of upper and lower case letters, digits, and\n" \
"other characters. You can use a password containing at least %d character.\n", \
\
"A good password should be a mix of upper and lower case letters, digits, and\n" \
"other characters. You can use a password containing at least %d characters.\n", \
count), (count)
#define MESSAGE_EXPLAIN_PASSWORD_N_CLASSES(count) \
P3_( \
"A valid password should be a mix of upper and lower case letters, digits, and\n" \
"other characters. You can use a password containing at least %d character\n" \
"from at least %d of these 4 classes.\n" \
"An upper case letter that begins the password and a digit that ends it do not\n" \
"count towards the number of character classes used.\n", \
\
"A valid password should be a mix of upper and lower case letters, digits, and\n" \
"other characters. You can use a password containing at least %d characters\n" \
"from at least %d of these 4 classes.\n" \
"An upper case letter that begins the password and a digit that ends it do not\n" \
"count towards the number of character classes used.\n", \
count), (count)
#define MESSAGE_EXPLAIN_PASSWORD_ALL_CLASSES(count) \
P3_( \
"A valid password should be a mix of upper and lower case letters, digits, and\n" \
"other characters. You can use a password containing at least %d character\n" \
"from all of these classes.\n" \
"An upper case letter that begins the password and a digit that ends it do not\n" \
"count towards the number of character classes used.\n", \
\
"A valid password should be a mix of upper and lower case letters, digits, and\n" \
"other characters. You can use a password containing at least %d characters\n" \
"from all of these classes.\n" \
"An upper case letter that begins the password and a digit that ends it do not\n" \
"count towards the number of character classes used.\n", \
count), (count)
#define MESSAGE_EXPLAIN_PASSWORD_ALL_OR_3_CLASSES(count) \
P3_( \
"A valid password should be a mix of upper and lower case letters, digits, and\n" \
"other characters. You can use a password containing at least %d character\n" \
"from all of these classes, or a password containing at least %d characters\n" \
"from just 3 of these 4 classes.\n" \
"An upper case letter that begins the password and a digit that ends it do not\n" \
"count towards the number of character classes used.\n", \
\
"A valid password should be a mix of upper and lower case letters, digits, and\n" \
"other characters. You can use a password containing at least %d characters\n" \
"from all of these classes, or a password containing at least %d characters\n" \
"from just 3 of these 4 classes.\n" \
"An upper case letter that begins the password and a digit that ends it do not\n" \
"count towards the number of character classes used.\n", \
count), (count)
#define MESSAGE_EXPLAIN_PASSPHRASE(count) \
P3_(\
"A passphrase should be of at least %d word, %d to %d characters long, and\n" \
"contain enough different characters.\n", \
\
"A passphrase should be of at least %d words, %d to %d characters long, and\n" \
"contain enough different characters.\n", \
count), (count)
#define MESSAGE_RANDOM \
_("Alternatively, if no one else can see your terminal now, you can pick this as\n" \
"your password: \"%s\".\n")
#define MESSAGE_RANDOMONLY \
_("This system is configured to permit randomly generated passwords only.\n" \
"If no one else can see your terminal now, you can pick this as your\n" \
"password: \"%s\". Otherwise come back later.\n")
#define MESSAGE_RANDOMFAILED \
_("This system is configured to use randomly generated passwords only,\n" \
"but the attempt to generate a password has failed. This could happen\n" \
"for a number of reasons: you could have requested an impossible password\n" \
"length, or the access to kernel random number pool could have failed.")
#define MESSAGE_TOOLONG \
_("This password may be too long for some services. Choose another.")
#define MESSAGE_TRUNCATED \
_("Warning: your longer password will be truncated to 8 characters.")
#define MESSAGE_WEAKPASS \
_("Weak password: %s.")
#define MESSAGE_NOTRANDOM \
_("Sorry, you've mistyped the password that was generated for you.")
#define MESSAGE_MISTYPED \
_("Sorry, passwords do not match.")
#define MESSAGE_RETRY \
_("Try again.")
static int logaudit(pam_handle_t *pamh, int status, passwdqc_params_t *params)
{
#ifdef HAVE_LIBAUDIT
if (!(params->pam.flags & F_NO_AUDIT)) {
int rc = pam_modutil_audit_write(pamh, AUDIT_USER_CHAUTHTOK, "pam_passwdqc", status);
if (status == PAM_SUCCESS)
status = rc;
}
#else /* !HAVE_LIBAUDIT */
(void) pamh;
#endif
passwdqc_params_free(params);
return status;
}
static int converse(pam_handle_t *pamh, int style, l_const char *text,
struct pam_response **resp)
{
pam_item_t item;
const struct pam_conv *conv;
struct pam_message msg, *pmsg;
int status;
*resp = NULL;
status = pam_get_item(pamh, PAM_CONV, &item);
if (status != PAM_SUCCESS)
return status;
conv = item;
pmsg = &msg;
msg.msg_style = style;
msg.msg = text;
return conv->conv(1, (lo_const struct pam_message **)&pmsg, resp,
conv->appdata_ptr);
}
#ifdef __GNUC__
__attribute__ ((format (printf, 3, 4)))
#endif
static int say(pam_handle_t *pamh, int style, const char *format, ...)
{
va_list args;
char buffer[0x800];
int needed;
struct pam_response *resp;
int status;
va_start(args, format);
needed = vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
if ((unsigned int)needed < sizeof(buffer)) {
status = converse(pamh, style, buffer, &resp);
pwqc_drop_pam_reply(resp, 1);
} else {
status = PAM_ABORT;
}
_passwdqc_memzero(buffer, sizeof(buffer));
return status;
}
static int check_max(passwdqc_params_qc_t *qc, pam_handle_t *pamh,
const char *newpass)
{
if (strlen(newpass) > (size_t)qc->max) {
if (qc->max != 8) {
say(pamh, PAM_ERROR_MSG, MESSAGE_TOOLONG);
return -1;
}
say(pamh, PAM_TEXT_INFO, MESSAGE_TRUNCATED);
}
return 0;
}
static int check_pass(struct passwd *pw, const char *pass)
{
const char *hash;
int retval;
#ifdef HAVE_SHADOW
#ifdef __hpux
if (iscomsec()) {
#else
if (!strcmp(pw->pw_passwd, "x")) {
#endif
struct spwd *spw = getspnam(pw->pw_name);
endspent();
if (!spw)
return -1;
hash = NULL;
if (strlen(spw->sp_pwdp) >= 13) {
#ifdef __hpux
hash = bigcrypt(pass, spw->sp_pwdp);
#else
hash = crypt(pass, spw->sp_pwdp);
#endif
}
retval = (hash && !strcmp(hash, spw->sp_pwdp)) ? 0 : -1;
_passwdqc_memzero(spw->sp_pwdp, strlen(spw->sp_pwdp));
return retval;
}
#endif
hash = NULL;
if (strlen(pw->pw_passwd) >= 13)
hash = crypt(pass, pw->pw_passwd);
retval = (hash && !strcmp(hash, pw->pw_passwd)) ? 0 : -1;
_passwdqc_memzero(pw->pw_passwd, strlen(pw->pw_passwd));
return retval;
}
static int am_root(pam_handle_t *pamh)
{
pam_item_t item;
const char *service;
if (getuid() != 0)
return 0;
if (pam_get_item(pamh, PAM_SERVICE, &item) != PAM_SUCCESS)
return 0;
service = item;
return !strcmp(service, "passwd") || !strcmp(service, "chpasswd");
}
PAM_EXTERN int pam_sm_chauthtok(pam_handle_t *pamh, int flags,
int argc, const char **argv)
{
passwdqc_params_t params;
struct pam_response *resp;
struct passwd *pw, fake_pw;
pam_item_t item;
const char *user, *oldpass, *newpass;
char *trypass, *randompass;
char *parse_reason;
const char *check_reason;
int ask_oldauthtok;
int randomonly, enforce, retries_left, retry_wanted;
int status;
passwdqc_params_reset(¶ms);
if (passwdqc_params_parse(¶ms, &parse_reason, argc, argv)) {
say(pamh, PAM_ERROR_MSG, am_root(pamh) ?
MESSAGE_INVALID_OPTION : MESSAGE_MISCONFIGURED,
parse_reason);
free(parse_reason);
return PAM_ABORT;
}
status = PAM_SUCCESS;
ask_oldauthtok = 0;
if (flags & PAM_PRELIM_CHECK) {
if (params.pam.flags & F_ASK_OLDAUTHTOK_PRELIM)
ask_oldauthtok = 1;
} else if (flags & PAM_UPDATE_AUTHTOK) {
if (params.pam.flags & F_ASK_OLDAUTHTOK_UPDATE)
ask_oldauthtok = 1;
} else {
passwdqc_params_free(¶ms);
return PAM_SERVICE_ERR;
}
if (ask_oldauthtok && !am_root(pamh)) {
status = converse(pamh, PAM_PROMPT_ECHO_OFF,
PROMPT_OLDPASS, &resp);
if (status == PAM_SUCCESS) {
if (resp && resp->resp) {
status = pam_set_item(pamh,
PAM_OLDAUTHTOK, resp->resp);
pwqc_drop_pam_reply(resp, 1);
} else
status = PAM_AUTHTOK_RECOVERY_ERR;
}
if (status != PAM_SUCCESS)
return logaudit(pamh, status, ¶ms);
}
if (flags & PAM_PRELIM_CHECK) {
passwdqc_params_free(¶ms);
return status;
}
status = pam_get_item(pamh, PAM_USER, &item);
if (status != PAM_SUCCESS)
return logaudit(pamh, status, ¶ms);
user = item;
status = pam_get_item(pamh, PAM_OLDAUTHTOK, &item);
if (status != PAM_SUCCESS)
return logaudit(pamh, status, ¶ms);
oldpass = item;
if (params.pam.flags & F_NON_UNIX) {
pw = &fake_pw;
memset(pw, 0, sizeof(*pw));
pw->pw_name = (char *)user;
pw->pw_gecos = "";
pw->pw_dir = "";
} else {
/* As currently implemented, we don't avoid timing leaks for valid vs. not
* usernames and hashes. Normally, the username would have already been
* checked and determined valid, and the check_oldauthtok option is only needed
* on systems that happen to have similar timing leaks all over the place. */
pw = getpwnam(user);
endpwent();
if (!pw)
return logaudit(pamh, PAM_USER_UNKNOWN, ¶ms);
if ((params.pam.flags & F_CHECK_OLDAUTHTOK) && !am_root(pamh)
&& (!oldpass || check_pass(pw, oldpass)))
status = PAM_AUTH_ERR;
_passwdqc_memzero(pw->pw_passwd, strlen(pw->pw_passwd));
if (status != PAM_SUCCESS)
return logaudit(pamh, status, ¶ms);
}
randomonly = params.qc.min[4] > params.qc.max;
if (am_root(pamh))
enforce = params.pam.flags & F_ENFORCE_ROOT;
else
enforce = params.pam.flags & F_ENFORCE_USERS;
if (params.pam.flags & F_USE_AUTHTOK) {
status = pam_get_item(pamh, PAM_AUTHTOK, &item);
if (status != PAM_SUCCESS)
return logaudit(pamh, status, ¶ms);
newpass = item;
if (!newpass ||
(check_max(¶ms.qc, pamh, newpass) && enforce))
return logaudit(pamh, PAM_AUTHTOK_ERR, ¶ms);
check_reason =
passwdqc_check(¶ms.qc, newpass, oldpass, pw);
if (check_reason) {
say(pamh, PAM_ERROR_MSG, MESSAGE_WEAKPASS,
check_reason);
if (enforce)
status = PAM_AUTHTOK_ERR;
}
return logaudit(pamh, status, ¶ms);
}
retries_left = params.pam.retry;
retry:
retry_wanted = 0;
if (!randomonly &&
params.qc.passphrase_words && params.qc.min[2] <= params.qc.max)
status = say(pamh, PAM_TEXT_INFO, MESSAGE_INTRO_BOTH);
else
status = say(pamh, PAM_TEXT_INFO, MESSAGE_INTRO_PASSWORD);
if (status != PAM_SUCCESS)
return logaudit(pamh, status, ¶ms);
if (!randomonly && params.qc.min[0] == params.qc.min[4])
status = say(pamh, PAM_TEXT_INFO,
MESSAGE_EXPLAIN_PASSWORD_1_CLASS(params.qc.min[4]));
else if (!randomonly && params.qc.min[3] == params.qc.min[4])
status = say(pamh, PAM_TEXT_INFO,
MESSAGE_EXPLAIN_PASSWORD_N_CLASSES(params.qc.min[4]),
params.qc.min[1] != params.qc.min[3] ? 3 : 2);
else if (!randomonly && params.qc.min[3] == INT_MAX)
status = say(pamh, PAM_TEXT_INFO,
MESSAGE_EXPLAIN_PASSWORD_ALL_CLASSES(params.qc.min[4]));
else if (!randomonly) {
status = say(pamh, PAM_TEXT_INFO,
MESSAGE_EXPLAIN_PASSWORD_ALL_OR_3_CLASSES(params.qc.min[4]),
params.qc.min[3]);
}
if (status != PAM_SUCCESS)
return logaudit(pamh, status, ¶ms);
if (!randomonly &&
params.qc.passphrase_words && params.qc.min[2] <= params.qc.max) {
status = say(pamh, PAM_TEXT_INFO,
MESSAGE_EXPLAIN_PASSPHRASE(params.qc.passphrase_words),
params.qc.min[2], params.qc.max);
if (status != PAM_SUCCESS)
return logaudit(pamh, status, ¶ms);
}
randompass = passwdqc_random(¶ms.qc);
if (randompass) {
status = say(pamh, PAM_TEXT_INFO, randomonly ?
MESSAGE_RANDOMONLY : MESSAGE_RANDOM, randompass);
if (status != PAM_SUCCESS) {
pwqc_overwrite_string(randompass);
pwqc_drop_mem(randompass);
}
} else if (randomonly) {
say(pamh, PAM_ERROR_MSG, am_root(pamh) ?
MESSAGE_RANDOMFAILED : MESSAGE_MISCONFIGURED);
return logaudit(pamh, PAM_AUTHTOK_ERR, ¶ms);
}
status = converse(pamh, PAM_PROMPT_ECHO_OFF, PROMPT_NEWPASS1, &resp);
if (status == PAM_SUCCESS && (!resp || !resp->resp))
status = PAM_AUTHTOK_ERR;
if (status != PAM_SUCCESS) {
pwqc_overwrite_string(randompass);
pwqc_drop_mem(randompass);
return logaudit(pamh, status, ¶ms);
}
trypass = strdup(resp->resp);
pwqc_drop_pam_reply(resp, 1);
if (!trypass) {
pwqc_overwrite_string(randompass);
pwqc_drop_mem(randompass);
return logaudit(pamh, PAM_AUTHTOK_ERR, ¶ms);
}
if (check_max(¶ms.qc, pamh, trypass) && enforce) {
status = PAM_AUTHTOK_ERR;
retry_wanted = 1;
}
check_reason = NULL; /* unused */
if (status == PAM_SUCCESS &&
(!randompass || !strstr(trypass, randompass)) &&
(randomonly ||
(check_reason = passwdqc_check(¶ms.qc, trypass, oldpass, pw)))) {
if (randomonly)
say(pamh, PAM_ERROR_MSG, MESSAGE_NOTRANDOM);
else
say(pamh, PAM_ERROR_MSG, MESSAGE_WEAKPASS,
check_reason);
if (enforce) {
status = PAM_AUTHTOK_ERR;
retry_wanted = 1;
}
}
if (status == PAM_SUCCESS)
status = converse(pamh, PAM_PROMPT_ECHO_OFF,
PROMPT_NEWPASS2, &resp);
if (status == PAM_SUCCESS) {
if (resp && resp->resp) {
if (strcmp(trypass, resp->resp)) {
status = say(pamh,
PAM_ERROR_MSG, MESSAGE_MISTYPED);
if (status == PAM_SUCCESS) {
status = PAM_AUTHTOK_ERR;
retry_wanted = 1;
}
}
pwqc_drop_pam_reply(resp, 1);
} else
status = PAM_AUTHTOK_ERR;
}
if (status == PAM_SUCCESS)
status = pam_set_item(pamh, PAM_AUTHTOK, trypass);
pwqc_overwrite_string(randompass);
pwqc_drop_mem(randompass);
pwqc_overwrite_string(trypass);
pwqc_drop_mem(trypass);
if (retry_wanted && --retries_left > 0) {
status = say(pamh, PAM_TEXT_INFO, MESSAGE_RETRY);
if (status == PAM_SUCCESS)
goto retry;
}
return logaudit(pamh, status, ¶ms);
}
#ifdef PAM_MODULE_ENTRY
PAM_MODULE_ENTRY("pam_passwdqc");
#elif defined(PAM_STATIC)
const struct pam_module _pam_passwdqc_modstruct = {
"pam_passwdqc",
NULL,
NULL,
NULL,
NULL,
NULL,
pam_sm_chauthtok
};
#endif