-
Notifications
You must be signed in to change notification settings - Fork 2
/
passwordpolicy.c
304 lines (260 loc) · 9.07 KB
/
passwordpolicy.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
/*-------------------------------------------------------------------------
*
* passwordpolicy.c
*
* Copyright (c) 2018, indrajit
*
* Created from postgres passwordcheck.
*
*
* Original license:
*
*
* /*-------------------------------------------------------------------------
* *
* * passwordcheck.c
* *
* *
* * Copyright (c) 2009-2017, PostgreSQL Global Development Group
* *
* * Author: Laurenz Albe <[email protected]>
* *
* * IDENTIFICATION
* * contrib/passwordcheck/passwordcheck.c
* *
* *-------------------------------------------------------------------------
* *\/
*
*-------------------------------------------------------------------------
*/
#include <ctype.h>
#include "postgres.h"
#include "catalog/namespace.h"
#include "utils/guc.h"
#include "commands/user.h"
#include "libpq/crypt.h"
#include "fmgr.h"
#if PG_VERSION_NUM < 100000
#include "libpq/md5.h"
#endif
#ifdef USE_CRACKLIB
#include <crack.h>
#endif
PG_MODULE_MAGIC;
extern void _PG_init(void);
// p_policy.min_password_len
int passMinLength = 8;
// p_policy.min_special_chars
int passMinSpcChar = 2;
// p_policy.min_numbers
int passMinNumChar = 2;
// p_policy.min_uppercase_letter
int passMinUpperChar = 2;
// p_policy.min_lowercase_letter
int passMinLowerChar = 2;
/*
* check_password
*
* performs checks on an encrypted or unencrypted password
* ereport's if not acceptable
*
* username: name of role being created or changed
* password: new password (possibly already encrypted)
* password_type: PASSWORD_TYPE_PLAINTEXT or PASSWORD_TYPE_MD5 (there
* could be other encryption schemes in future)
* validuntil_time: password expiration time, as a timestamptz Datum
* validuntil_null: true if password expiration time is NULL
*
* This sample implementation doesn't pay any attention to the password
* expiration time, but you might wish to insist that it be non-null and
* not too far in the future.
*/
static void check_policy(const char *password) {
int i, pwdlen, letter_count, number_count, spc_char_count, upper_count, lower_count;
pwdlen = strlen(password);
letter_count = 0;
number_count = 0;
spc_char_count = 0;
upper_count = 0;
lower_count = 0;
for (i = 0; i < pwdlen; i++) {
/*
* isalpha() does not work for multibyte encodings but let's
* consider non-ASCII characters non-letters
*/
if (isalpha((unsigned char)password[i])) {
letter_count++;
if (isupper((unsigned char)password[i])) {
upper_count++;
} else if (islower((unsigned char)password[i])) {
lower_count++;
}
} else if (isdigit((unsigned char)password[i])) {
number_count++;
} else {
spc_char_count++;
}
}
if (number_count < passMinNumChar) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password must contain atleast %d numeric characters.",
passMinNumChar)));
} else if (spc_char_count < passMinSpcChar) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password must contain atleast %d special characters.",
passMinSpcChar)));
} else if (upper_count < passMinUpperChar) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password must contain atleast %d upper case letters.",
passMinUpperChar)));
} else if (lower_count < passMinLowerChar) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password must contain atleast %d lower case letters.",
passMinLowerChar)));
}
}
#if PG_VERSION_NUM >= 100000
static void check_password(const char *username, const char *shadow_pass,
PasswordType password_type, Datum validuntil_time,
bool validuntil_null) {
if (password_type != PASSWORD_TYPE_PLAINTEXT) {
/*
* Unfortunately we cannot perform exhaustive checks on encrypted
* passwords - we are restricted to guessing. (Alternatively, we could
* insist on the password being presented non-encrypted, but that has
* its own security disadvantages.)
*
* We only check for username = password.
*/
char *logdetail;
if (plain_crypt_verify(username, shadow_pass, username, &logdetail) == STATUS_OK) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password must not contain user name")));
}
} else {
/*
* For unencrypted passwords we can perform better checks
*/
const char *password = shadow_pass;
int pwdlen = strlen(password);
/* enforce minimum length */
if (pwdlen < passMinLength) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password is too short.")));
}
/* check if the password contains the username */
if (strstr(password, username)) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password must not contain user name.")));
}
check_policy(password);
#ifdef USE_CRACKLIB
/* call cracklib to check password */
if (FascistCheck(password, CRACKLIB_DICTPATH)) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password is easily cracked.")));
}
#endif
}
/* all checks passed, password is ok */
}
#else
static void check_password(const char *username, const char *password,
int password_type, Datum validuntil_time,
bool validuntil_null) {
int namelen = strlen(username);
int pwdlen = strlen(password);
char encrypted[MD5_PASSWD_LEN + 1];
switch (password_type) {
case PASSWORD_TYPE_MD5:
/*
* Unfortunately we cannot perform exhaustive checks on encrypted
* passwords - we are restricted to guessing. (Alternatively, we
* could insist on the password being presented non-encrypted, but
* that has its own security disadvantages.)
*
* We only check for username = password.
*/
if (!pg_md5_encrypt(username, username, namelen, encrypted)) {
elog(ERROR, "password encryption failed.");
}
if (strcmp(password, encrypted) == 0) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password must not contain user name.")));
}
break;
case PASSWORD_TYPE_PLAINTEXT:
/*
* For unencrypted passwords we can perform better checks
*/
/* enforce minimum length */
if (pwdlen < passMinLength) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password is too short.")));
}
/* check if the password contains the username */
if (strstr(password, username)) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password must not contain user name.")));
}
check_policy(password);
#ifdef USE_CRACKLIB
/* call cracklib to check password */
if (FascistCheck(password, CRACKLIB_DICTPATH)) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password is easily cracked.")));
}
#endif
break;
default:
elog(ERROR, "unrecognized password type: %d.", password_type);
break;
}
/* all checks passed, password is ok */
}
#endif
static void define_variables() {
/* Define p_policy.min_pass_len */
DefineCustomIntVariable("p_policy.min_password_len",
"Minimum password length.", NULL, &passMinLength, 8,
1, INT_MAX, PGC_SIGHUP, 0, NULL, NULL, NULL);
/* Define p_policy.min_special_chars */
DefineCustomIntVariable(
"p_policy.min_special_chars", "Minimum number of special characters.",
NULL, &passMinSpcChar, 2, 1, INT_MAX, PGC_SIGHUP, 0, NULL, NULL, NULL);
/* Define p_policy.min_numbers */
DefineCustomIntVariable(
"p_policy.min_numbers", "Minimum number of numeric characters.", NULL,
&passMinNumChar, 2, 1, INT_MAX, PGC_SIGHUP, 0, NULL, NULL, NULL);
/* Define p_policy.min_uppercase_letter */
DefineCustomIntVariable(
"p_policy.min_uppercase_letter", "Minimum number of upper case letters.",
NULL, &passMinUpperChar, 2, 1, INT_MAX, PGC_SIGHUP, 0, NULL, NULL, NULL);
/* Define p_policy.min_lowercase_letter */
DefineCustomIntVariable(
"p_policy.min_lowercase_letter", "Minimum number of lower case letters.",
NULL, &passMinLowerChar, 2, 1, INT_MAX, PGC_SIGHUP, 0, NULL, NULL, NULL);
if (passMinLength < (passMinSpcChar + passMinNumChar + passMinUpperChar + passMinLowerChar)) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("configuration error.\nsum of minimum character "
"requirement exceeds minimum password length.")));
}
}
/*
* Module initialization function
*/
void _PG_init(void) {
/* Be sure we do initialization only once */
static bool inited = false;
if (inited) {
return;
}
define_variables();
/* activate password checks when the module is loaded */
check_password_hook = check_password;
inited = true;
}