-
Notifications
You must be signed in to change notification settings - Fork 17
/
passwdqc_parse.c
215 lines (200 loc) · 5.33 KB
/
passwdqc_parse.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
/*
* Copyright (c) 2000-2003,2005,2016,2020,2021 by Solar Designer
* Copyright (c) 2008,2009 by Dmitry V. Levin
* See LICENSE
*/
#ifdef _MSC_VER
#define _CRT_NONSTDC_NO_WARNINGS /* we use POSIX function names */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "passwdqc.h"
#include "concat.h"
static const char *skip_prefix(const char *sample, const char *prefix)
{
size_t len = strlen(prefix);
if (strncmp(sample, prefix, len))
return NULL;
return sample + len;
}
static int
parse_option(passwdqc_params_t *params, char **reason, const char *option)
{
const char *err = "Invalid parameter value";
const char * const err_oom = "Out of memory";
const char *p;
char *e;
int i, rc = 0;
unsigned long v;
*reason = NULL;
if ((p = skip_prefix(option, "min="))) {
for (i = 0; i < 5; i++) {
if (!strncmp(p, "disabled", 8)) {
v = INT_MAX;
p += 8;
} else {
v = strtoul(p, &e, 10);
p = e;
}
if (i < 4 && *p++ != ',')
goto parse_error;
if (v > INT_MAX)
goto parse_error;
if (i && (int)v > params->qc.min[i - 1])
goto parse_error;
params->qc.min[i] = v;
}
if (*p)
goto parse_error;
} else if ((p = skip_prefix(option, "max="))) {
v = strtoul(p, &e, 10);
if (*e || v < 8 || v > INT_MAX)
goto parse_error;
if (v > 10000)
v = 10000;
params->qc.max = v;
} else if ((p = skip_prefix(option, "passphrase="))) {
v = strtoul(p, &e, 10);
if (*e || v > INT_MAX)
goto parse_error;
params->qc.passphrase_words = v;
} else if ((p = skip_prefix(option, "match="))) {
v = strtoul(p, &e, 10);
if (*e || v > INT_MAX)
goto parse_error;
params->qc.match_length = v;
} else if ((p = skip_prefix(option, "similar="))) {
if (!strcmp(p, "permit"))
params->qc.similar_deny = 0;
else if (!strcmp(p, "deny"))
params->qc.similar_deny = 1;
else
goto parse_error;
} else if ((p = skip_prefix(option, "random="))) {
v = strtoul(p, &e, 10);
if (!strcmp(e, ",only")) {
e += 5;
params->qc.min[4] = INT_MAX;
}
if (*e || (v && v < 24) || v > 136)
goto parse_error;
params->qc.random_bits = v;
} else if ((p = skip_prefix(option, "wordlist="))) {
free(params->qc.wordlist);
params->qc.wordlist = NULL;
if (*p && !(params->qc.wordlist = strdup(p))) {
err = err_oom;
goto parse_error;
}
} else if ((p = skip_prefix(option, "denylist="))) {
free(params->qc.denylist);
params->qc.denylist = NULL;
if (*p && !(params->qc.denylist = strdup(p))) {
err = err_oom;
goto parse_error;
}
} else if ((p = skip_prefix(option, "filter="))) {
free(params->qc.filter);
params->qc.filter = NULL;
if (*p && !(params->qc.filter = strdup(p))) {
err = err_oom;
goto parse_error;
}
} else if ((p = skip_prefix(option, "enforce="))) {
params->pam.flags &= ~F_ENFORCE_MASK;
if (!strcmp(p, "users"))
params->pam.flags |= F_ENFORCE_USERS;
else if (!strcmp(p, "everyone"))
params->pam.flags |= F_ENFORCE_EVERYONE;
else if (strcmp(p, "none"))
goto parse_error;
} else if (!strcmp(option, "non-unix")) {
if (params->pam.flags & F_CHECK_OLDAUTHTOK)
goto parse_error;
params->pam.flags |= F_NON_UNIX;
} else if ((p = skip_prefix(option, "retry="))) {
v = strtoul(p, &e, 10);
if (*e || v > INT_MAX)
goto parse_error;
params->pam.retry = v;
} else if ((p = skip_prefix(option, "ask_oldauthtok"))) {
params->pam.flags &= ~F_ASK_OLDAUTHTOK_MASK;
if (params->pam.flags & F_USE_FIRST_PASS)
goto parse_error;
if (!p[0])
params->pam.flags |= F_ASK_OLDAUTHTOK_PRELIM;
else if (!strcmp(p, "=update"))
params->pam.flags |= F_ASK_OLDAUTHTOK_UPDATE;
else
goto parse_error;
} else if (!strcmp(option, "check_oldauthtok")) {
if (params->pam.flags & F_NON_UNIX)
goto parse_error;
params->pam.flags |= F_CHECK_OLDAUTHTOK;
} else if (!strcmp(option, "use_first_pass")) {
if (params->pam.flags & F_ASK_OLDAUTHTOK_MASK)
goto parse_error;
params->pam.flags |= F_USE_FIRST_PASS | F_USE_AUTHTOK;
} else if (!strcmp(option, "use_authtok")) {
params->pam.flags |= F_USE_AUTHTOK;
} else if (!strcmp(option, "noaudit")) {
params->pam.flags |= F_NO_AUDIT;
} else if ((p = skip_prefix(option, "config="))) {
if ((rc = passwdqc_params_load(params, reason, p)))
goto parse_error;
} else {
err = "Invalid parameter";
goto parse_error;
}
return 0;
parse_error:
passwdqc_params_free(params);
e = concat("Error parsing parameter \"", option, "\": ",
(rc ? (*reason ? *reason : err_oom) : err), NULL);
free(*reason);
*reason = e;
return rc ? rc : -1;
}
int
passwdqc_params_parse(passwdqc_params_t *params, char **reason,
int argc, const char *const *argv)
{
int i;
*reason = NULL;
for (i = 0; i < argc; ++i) {
int rc;
if ((rc = parse_option(params, reason, argv[i])))
return rc;
}
return 0;
}
static const passwdqc_params_t defaults = {
{
{INT_MAX, 24, 11, 8, 7}, /* min */
72, /* max */
3, /* passphrase_words */
4, /* match_length */
1, /* similar_deny */
47, /* random_bits */
NULL, /* wordlist */
NULL, /* denylist */
NULL /* filter */
},
{
F_ENFORCE_EVERYONE, /* flags */
3 /* retry */
}
};
void passwdqc_params_reset(passwdqc_params_t *params)
{
*params = defaults;
}
void passwdqc_params_free(passwdqc_params_t *params)
{
free(params->qc.wordlist);
free(params->qc.denylist);
free(params->qc.filter);
passwdqc_params_reset(params);
}