forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwstr.c
422 lines (325 loc) · 8.17 KB
/
pwstr.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
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <errno.h>
#include "config.h"
#include "pwstr.h"
/*
Rule configuration structure
*/
#define RULE_T_NONE 0
#define RULE_T_LESS 1 /* < */
#define RULE_T_LESS_EQUAL 2 /* <= */
#define RULE_T_EQUAL 3 /* = */
#define RULE_T_GREATER 4 /* > */
#define RULE_T_GREATER_EQUAL 5 /* >= */
typedef struct __rule_ {
int test; /* Test to perform */
double value; /* Value that triggers the rule */
} rule_t;
static char rule_desc[512] = { 0 };
static int rule_ret = 0, rules_loaded = 0;
static rule_t rule_alpha, rule_alphadelta, rule_numeric, rule_other, rule_length, *rule_last = NULL;
/*
Load password strength rules from a file
*/
static int load_rules(const char *filename)
{
int test = RULE_T_NONE;
rule_t *r = NULL;
FILE *stream = NULL;
char b[1024] = { 0 }, *t = NULL, *v = NULL, *p = NULL;
/*
Reset rules
*/
rule_ret = 0;
rule_last = NULL;
memset(rule_desc, 0, sizeof(rule_desc));
memset(&rule_alpha, 0, sizeof(rule_alpha));
memset(&rule_alphadelta, 0, sizeof(rule_alphadelta));
memset(&rule_numeric, 0, sizeof(rule_numeric));
memset(&rule_other, 0, sizeof(rule_other));
memset(&rule_length, 0, sizeof(rule_length));
/*
Open rules configuration
*/
stream = fopen(filename, "r");
if (stream == NULL) {
if (errno != ENOENT) {
fprintf(stderr,"load_rules: %s: %s\n", filename, strerror(errno));
return 0;
}
return 1;
}
/*
Loop through configurations
*/
while(!(feof(stream))) {
memset(b, 0, sizeof(b));
fgets(b, sizeof(b), stream);
/*
Cut off comments and newlines
*/
for (p = b; *p; p++) {
if ((*p == '#') || (*p == '\r') || (*p == '\n')) {
*p = '\0';
break;
}
}
/*
Skip comment lines
*/
if (!(*b))
continue;
/*
Find name/test separator
*/
for (t = b; *t; t++) {
if ((*t == ' ') || (*t == '\t')) {
*t++ = '\0';
while ((*t == ' ') || (*t == '\t')) t++;
break;
}
}
if (!(*t)) {
fprintf(stderr,"load_rules: %s: syntax error: %s\n", filename, b);
continue;
}
/*
Find test/value seperator
*/
for (v = t; *v; v++) {
if ((*v == ' ') || (*v == '\t')) {
*v++ = '\0';
while ((*v == ' ') || (*v == '\t')) v++;
break;
}
}
if (!(*v)) {
fprintf(stderr,"load_rules: %s: syntax error: %s\n", filename, b);
continue;
}
/*
If setting policy description, save it and move on
*/
if (!(strcasecmp(b, "policy"))) {
snprintf(rule_desc, sizeof(rule_desc), "%s", v);
continue;
}
/*
Cut off any extra whitespace
*/
for (p = v; *p; p++) {
if ((*p == ' ') || (*p == '\t')) {
*p = '\0';
break;
}
}
/*
Point to appropriate rule structure
*/
r = NULL;
if (!strcasecmp(b, "alphabet")) r = &rule_alpha;
else if (!strcasecmp(b, "upperlower")) r = &rule_alphadelta;
else if (!strcasecmp(b, "numeric")) r = &rule_numeric;
else if (!strcasecmp(b, "other")) r = &rule_other;
else if (!strcasecmp(b, "length")) r = &rule_length;
else {
fprintf(stderr,"load_rules: %s: unknown rule: %s\n", filename, b);
fclose(stream);
return 0;
}
/*
Determine test type
*/
if (!strcasecmp(t, "<")) test = RULE_T_LESS;
else if (!strcasecmp(t, "<=")) test = RULE_T_LESS_EQUAL;
else if (!strcasecmp(t, "=")) test = RULE_T_EQUAL;
else if (!strcasecmp(t, ">")) test = RULE_T_GREATER;
else if (!strcasecmp(t, ">=")) test = RULE_T_GREATER_EQUAL;
else {
fprintf(stderr,"load_rules: %s: invalid test: %s\n", filename, t);
fclose(stream);
return 0;
}
/*
Set rule
*/
r->test = test;
r->value = strtod(v, NULL);
}
fclose(stream);
return 1;
}
/*
Returns if a rule evalutes as true
*/
static int rule_is_true(rule_t *r, double value)
{
/*
Default to true
*/
rule_ret = 1;
rule_last = NULL;
/*
No rule is always true
*/
if (r == NULL) return 1;
/*
Save last rule checked
*/
rule_last = r;
/*
Test
*/
switch(r->test) {
case RULE_T_LESS:
rule_ret = (value < r->value);
break;
case RULE_T_LESS_EQUAL:
rule_ret = (value <= r->value);
break;
case RULE_T_EQUAL:
rule_ret = (value == r->value);
break;
case RULE_T_GREATER:
rule_ret = (value > r->value);
break;
case RULE_T_GREATER_EQUAL:
rule_ret = (value >= r->value);
break;
/*
No rule, or an unknown test, always evaluate true
*/
default:
rule_ret = 1;
break;
};
return rule_ret;
}
/*
Check password strength matches rules
*/
int pw_strength(const char *password)
{
char b[512] = { 0 };
const char *p = NULL;
double len = 0, score = 0, n_lower = 0, n_upper = 0, n_numeric = 0, n_other = 0, n_alpha = 0;
double r_lower = 0, r_upper = 0, r_numeric = 0, r_other = 0, r_alpha = 0;
double alphadelta = 0;
char configfile[50];
/*
Load rules
*/
if (rules_loaded == 0) {
rules_loaded = 1;
snprintf(configfile, sizeof(configfile), "%s/etc/password_strength.conf", VPOPMAILDIR);
if (!load_rules(configfile)) fprintf(stderr,"load_rules: failed\n");
}
if (password == NULL) return -69;
/*
Count different character sets within password
*/
n_lower = n_upper = n_numeric = n_other = n_alpha = 0;
for (p = password; *p; p++) {
if ((*p >= 65) && (*p <= 90)) {
n_upper++;
n_alpha++;
}
else if ((*p >= 90) && (*p <= 122)) {
n_lower++;
n_alpha++;
}
else if ((*p >= '0') && (*p <= '9')) n_numeric++;
else n_other++;
len++;
}
/*
Test length
*/
len = strlen(password);
if (!(rule_is_true(&rule_length, len)))
return -70;
/*
Calculate character set to length ratios
*/
r_alpha = (double)(n_alpha / len);
r_lower = (double)(n_lower / len);
r_upper = (double)(n_upper / len);
r_numeric = (double)(n_numeric / len);
r_other = (double)(n_other / len);
#if 0
printf("AR:%f LR:%f UR:%f NR:%f PR:%f\n",
r_alpha, r_lower, r_upper, r_numeric, r_other);
#endif
alphadelta = (n_upper - n_lower);
if (alphadelta < 0)
alphadelta = -(alphadelta);
alphadelta /= n_alpha;
#if 0
printf("LOWER:%f UPPER:%f DELTA: %f\n", n_lower, n_upper, alphadelta);
#endif
/*
Test rules
*/
if (!rule_is_true(&rule_alpha, r_alpha)) return -71;
if (!rule_is_true(&rule_numeric, r_numeric)) return -72;
if (!rule_is_true(&rule_other, r_other)) return -73;
if (!rule_is_true(&rule_alphadelta, alphadelta)) return -74;
return 1;
}
/*
Based on the last test made, and the last error returned,
generate an error string
*/
const char *pw_strength_error(void)
{
static char b[512] = { 0 }, val[127] = { 0 };
const char *condition = NULL, *name = NULL, *adjust = NULL;
adjust = NULL;
if (rule_last == NULL)
return "password suitable";
switch(rule_last->test) {
case RULE_T_LESS:
case RULE_T_LESS_EQUAL:
condition = "should have less";
break;
case RULE_T_EQUAL:
condition = "can only be";
break;
case RULE_T_GREATER:
case RULE_T_GREATER_EQUAL:
condition = "requires more";
break;
default:
return "unknown password failure";
};
if (rule_last == &rule_alpha) name = "alphabetical characters";
else if (rule_last == &rule_numeric) name = "numbers";
else if (rule_last == &rule_alphadelta) {
name = "uppercase and lowercase character diversity";
if (condition == "should have less") condition = "requires more";
else condition = "should have less";
}
else if (rule_last == &rule_other) name = "punctuation, symbols, or other characters";
else if (rule_last == &rule_length) {
if (condition == "can only be") {
condition = "must always be";
snprintf(val, sizeof(val), "%d characters long", (int)rule_last->value);
name = val;
}
else name = "characters";
}
else return "unknown password failure";
snprintf(b, sizeof(b), "password %s %s %s", condition, name, adjust ? adjust : "");
return (const char *)b;
}
/*
Return configured policy string
*/
const char *pw_strength_policy(void)
{
if (*rule_desc) return (const char *)rule_desc;
return NULL;
}