forked from anermakov/arenadata_password_check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharenadata_password_check.c
296 lines (260 loc) · 8.04 KB
/
arenadata_password_check.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
/*-------------------------------------------------------------------------
*
* arenadata_password_check.c
*
* Copyright (c) 2024, Arenadata Software LLC
*
* Author: Alexander Ermakov <[email protected]>
*
* IDENTIFICATION
* arenadata_password_check/arenadata_password_check.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <ctype.h>
#ifdef USE_CRACKLIB
#include <crack.h>
#endif
#include "commands/user.h"
#include "fmgr.h"
#include "libpq/md5.h"
#include "lib/stringinfo.h"
#include "utils/guc.h"
PG_MODULE_MAGIC;
/* GUC variables */
static int password_min_len = 8;
static int password_max_len = 15;
static bool password_lower_case = true;
static bool password_upper_case = true;
static bool password_special = true;
static bool password_numbers = true;
static char *password_special_chars = "!@#$%^&*()_+{}|<>?=";
/*
* Flags to check contents of plain text passwords, which allow tracking
* of restrictions more easily.
*/
#define PASSWORD_HAS_LOWER 0x0001 /* Lower-case character */
#define PASSWORD_HAS_UPPER 0x0002 /* Upper-case character */
#define PASSWORD_HAS_SPECIAL 0x0004 /* Special character */
#define PASSWORD_HAS_NUMBER 0x0008 /* Number */
extern void _PG_init(void);
/*
* 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_password(const char *username,
const char *password,
PasswordType password_type,
Datum validuntil_time,
bool validuntil_null)
{
int pwdlen = strlen(password);
char encrypted[MD5_PASSWD_LEN + 1];
int i;
int password_flag = 0;
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 (strcmp(password, encrypted) == 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password must not contain user name")));
break;
case PASSWORD_TYPE_PLAINTEXT:
{
StringInfoData buf;
bool set_comma = false;
/*
* For unencrypted passwords we can perform better checks.
*/
/* enforce minimum length */
if (pwdlen < password_min_len)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password is too short")));
/* enforce maximum length */
if (pwdlen > password_max_len)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password is too long")));
/* 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")));
/* Scan password characters and check contents */
for (i = 0; i < pwdlen; i++)
{
/* Check character validity */
if (isupper((unsigned char) password[i]))
password_flag |= PASSWORD_HAS_UPPER;
else if (islower((unsigned char) password[i]))
password_flag |= PASSWORD_HAS_LOWER;
else if (isdigit((unsigned char) password[i]))
password_flag |= PASSWORD_HAS_NUMBER;
else if (strchr(password_special_chars,
(unsigned char) password[i]) != NULL)
password_flag |= PASSWORD_HAS_SPECIAL;
else
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("password contains invalid characters")));
}
/* Initialize error message */
initStringInfo(&buf);
/* Lower-case character missing? */
if ((password_flag & PASSWORD_HAS_LOWER) == 0 &&
password_lower_case)
{
appendStringInfo(&buf, "lower-case character missing");
set_comma = true;
}
/* Upper-case character missing? */
if ((password_flag & PASSWORD_HAS_UPPER) == 0 &&
password_upper_case)
{
if (set_comma)
appendStringInfo(&buf, ", ");
appendStringInfo(&buf, "upper-case character missing");
set_comma = true;
}
/* Number missing? */
if ((password_flag & PASSWORD_HAS_NUMBER) == 0 &&
password_numbers)
{
if (set_comma)
appendStringInfo(&buf, ", ");
appendStringInfo(&buf, "number missing");
set_comma = true;
}
/* Special character missing */
if ((password_flag & PASSWORD_HAS_SPECIAL) == 0 &&
password_special)
{
if (set_comma)
appendStringInfo(&buf, ", ");
appendStringInfo(&buf, "special character missing "
"(needs to be one listed in \"%s\")",
password_special_chars);
}
/*
* Complain with everything lacking if anything has been
* found.
*/
if (buf.len != 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Incorrect password format: %s",
buf.data)));
#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 */
}
/*
* Entry point for parameter loading
*/
static void
arenadata_password_check_load_params(void)
{
/* Special character set */
DefineCustomStringVariable("arenadata_password_check.special_chars",
"Special characters defined.",
"Default value is \"!@#$%^&*()_+{}|<>?=\".",
&password_special_chars,
"!@#$%^&*()_+{}|<>?=",
PGC_SUSET,
0, NULL, NULL, NULL);
/* Restrict use of lower-case characters */
DefineCustomBoolVariable("arenadata_password_check.restrict_lower",
"Enforce use of lower-case characters.",
NULL,
&password_lower_case,
true,
PGC_SUSET,
0, NULL, NULL, NULL);
/* Restrict use of upper-case characters */
DefineCustomBoolVariable("arenadata_password_check.restrict_upper",
"Enforce use of upper-case characters.",
NULL,
&password_upper_case,
true,
PGC_SUSET,
0, NULL, NULL, NULL);
/* Restrict use of numbers */
DefineCustomBoolVariable("arenadata_password_check.restrict_numbers",
"Enforce use of numbers.",
NULL,
&password_numbers,
true,
PGC_SUSET,
0, NULL, NULL, NULL);
/* Restrict use of special characters */
DefineCustomBoolVariable("arenadata_password_check.restrict_special",
"Enforce use of special characters.",
NULL,
&password_special,
true,
PGC_SUSET,
0, NULL, NULL, NULL);
/* Minimum password length */
DefineCustomIntVariable("arenadata_password_check.minimum_length",
"Minimum length of password allowed",
"Default value set to 8.",
&password_min_len,
8, 1, 10000,
PGC_SUSET,
0, NULL, NULL, NULL);
/* Maximum password length */
DefineCustomIntVariable("arenadata_password_check.maximum_length",
"Maximum length of password allowed",
"Default value set to 15, which actually sucks.",
&password_max_len,
15, 1, 10000,
PGC_SUSET,
0, NULL, NULL, NULL);
}
/*
* Module initialization function
*/
void
_PG_init(void)
{
/* Load library parameters */
arenadata_password_check_load_params();
/* activate password checks when the module is loaded */
check_password_hook = check_password;
}