Debian Codegraph @

src/settings.c

raw
1 /*
2 * libpwquality main API code for reading and manipulation of settings
3 *
4 * See the end of the file for Copyright and License Information
5 */
6
7 #include "config.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <limits.h>
13 #include <ctype.h>
14 #include <errno.h>
15 #include <dirent.h>
16 #ifdef HAVE_CRACK_H
17 #include <crack.h>
18 #endif
19
20 #include "pwquality.h"
21 #include "pwqprivate.h"
22
23 /* returns default pwquality settings to be used in other library calls */
24 pwquality_settings_t *
25 pwquality_default_settings(void)
26 {
27 pwquality_settings_t *pwq;
28
29 pwq = calloc(1, sizeof(*pwq));
30 if (!pwq)
31 return NULL;
32
33 pwq->diff_ok = PWQ_DEFAULT_DIFF_OK;
34 pwq->min_length = PWQ_DEFAULT_MIN_LENGTH;
35 pwq->dig_credit = PWQ_DEFAULT_DIG_CREDIT;
36 pwq->up_credit = PWQ_DEFAULT_UP_CREDIT;
37 pwq->low_credit = PWQ_DEFAULT_LOW_CREDIT;
38 pwq->oth_credit = PWQ_DEFAULT_OTH_CREDIT;
39 pwq->dict_check = PWQ_DEFAULT_DICT_CHECK;
40 pwq->user_check = PWQ_DEFAULT_USER_CHECK;
41 pwq->user_substr = PWQ_DEFAULT_USER_SUBSTR;
42 pwq->enforcing = PWQ_DEFAULT_ENFORCING;
43 pwq->retry_times = PWQ_DEFAULT_RETRY_TIMES;
44 pwq->enforce_for_root = PWQ_DEFAULT_ENFORCE_ROOT;
45 pwq->local_users_only = PWQ_DEFAULT_LOCAL_USERS;
46
47 return pwq;
48 }
49
50 /* frees pwquality settings data */
51 void
52 pwquality_free_settings(pwquality_settings_t *pwq)
53 {
54 if (pwq) {
55 free(pwq->dict_path);
56 free(pwq->bad_words);
57 free(pwq);
58 }
59 }
60
61
62 static const struct setting_mapping s_map[] = {
63 { "difok", PWQ_SETTING_DIFF_OK, PWQ_TYPE_INT},
64 { "minlen", PWQ_SETTING_MIN_LENGTH, PWQ_TYPE_INT},
65 { "dcredit", PWQ_SETTING_DIG_CREDIT, PWQ_TYPE_INT},
66 { "ucredit", PWQ_SETTING_UP_CREDIT, PWQ_TYPE_INT},
67 { "lcredit", PWQ_SETTING_LOW_CREDIT, PWQ_TYPE_INT},
68 { "ocredit", PWQ_SETTING_OTH_CREDIT, PWQ_TYPE_INT},
69 { "minclass", PWQ_SETTING_MIN_CLASS, PWQ_TYPE_INT},
70 { "maxrepeat", PWQ_SETTING_MAX_REPEAT, PWQ_TYPE_INT},
71 { "maxclassrepeat", PWQ_SETTING_MAX_CLASS_REPEAT, PWQ_TYPE_INT},
72 { "maxsequence", PWQ_SETTING_MAX_SEQUENCE, PWQ_TYPE_INT},
73 { "gecoscheck", PWQ_SETTING_GECOS_CHECK, PWQ_TYPE_INT},
74 { "dictcheck", PWQ_SETTING_DICT_CHECK, PWQ_TYPE_INT},
75 { "usercheck", PWQ_SETTING_USER_CHECK, PWQ_TYPE_INT},
76 { "usersubstr", PWQ_SETTING_USER_SUBSTR, PWQ_TYPE_INT},
77 { "enforcing", PWQ_SETTING_ENFORCING, PWQ_TYPE_INT},
78 { "badwords", PWQ_SETTING_BAD_WORDS, PWQ_TYPE_STR},
79 { "dictpath", PWQ_SETTING_DICT_PATH, PWQ_TYPE_STR},
80 { "retry", PWQ_SETTING_RETRY_TIMES, PWQ_TYPE_INT},
81 { "enforce_for_root", PWQ_SETTING_ENFORCE_ROOT, PWQ_TYPE_SET},
82 { "local_users_only", PWQ_SETTING_LOCAL_USERS, PWQ_TYPE_SET}
83 };
84
85 /* set setting name with value */
86 static int
87 set_name_value(pwquality_settings_t *pwq, const char *name, const char *value)
88 {
89 int i;
90 long val;
91 char *endptr;
92
93 for (i = 0; i < (int)(sizeof(s_map)/sizeof(s_map[0])); i++) {
94 if (strcasecmp(s_map[i].name, name) == 0) {
95 switch(s_map[i].type) {
96 case PWQ_TYPE_INT:
97 errno = 0;
98 val = strtol(value, &endptr, 10);
99 if (errno != 0 || *value == '\0' ||
100 *endptr != '\0' || val >= INT_MAX || val <= INT_MIN) {
101 return PWQ_ERROR_INTEGER;
102 }
103 return pwquality_set_int_value(pwq, s_map[i].id,
104 (int)val);
105 case PWQ_TYPE_STR:
106 return pwquality_set_str_value(pwq, s_map[i].id,
107 value);
108 case PWQ_TYPE_SET:
109 return pwquality_set_int_value(pwq, s_map[i].id,
110 1);
111 }
112 }
113 }
114 return PWQ_ERROR_UNKNOWN_SETTING;
115 }
116
117 #define PWQSETTINGS_MAX_LINELEN 1023
118
119 /* parse a single configuration file*/
120 int
121 read_config_file(pwquality_settings_t *pwq, const char *cfgfile, void **auxerror)
122 {
123 FILE *f;
124 char linebuf[PWQSETTINGS_MAX_LINELEN+1];
125 int rv = 0;
126
127 f = fopen(cfgfile, "r");
128 if (f == NULL) {
129 /* ignore non-existent default config file */
130 if (errno == ENOENT && strcmp(cfgfile, PWQUALITY_DEFAULT_CFGFILE) == 0)
131 return 0;
132 return PWQ_ERROR_CFGFILE_OPEN;
133 }
134
135 while (fgets(linebuf, sizeof(linebuf), f) != NULL) {
136 size_t len;
137 char *ptr;
138 char *name;
139 int eq;
140
141 len = strlen(linebuf);
142 /* len cannot be 0 unless there is a bug in fgets */
143 if (len && linebuf[len - 1] != '\n' && !feof(f)) {
144 (void) fclose(f);
145 return PWQ_ERROR_CFGFILE_MALFORMED;
146 }
147
148 if ((ptr=strchr(linebuf, '#')) != NULL) {
149 *ptr = '\0';
150 } else {
151 ptr = linebuf + len;
152 }
153
154 /* drop terminating whitespace including the \n */
155 while (ptr > linebuf) {
156 if (!isspace(*(ptr-1))) {
157 *ptr = '\0';
158 break;
159 }
160 --ptr;
161 }
162
163 /* skip initial whitespace */
164 for (ptr = linebuf; isspace(*ptr); ptr++);
165 if (*ptr == '\0')
166 continue;
167
168 eq = 0;
169 name = ptr;
170 while (*ptr != '\0') {
171 if (isspace(*ptr) || *ptr == '=') {
172 eq = *ptr == '=';
173 *ptr = '\0';
174 ++ptr;
175 break;
176 }
177 ++ptr;
178 }
179
180 while (*ptr != '\0') {
181 if (*ptr != '=' || eq) {
182 if (!isspace(*ptr)) {
183 break;
184 }
185 } else {
186 eq = 1;
187 }
188 ++ptr;
189 }
190
191 if ((rv=set_name_value(pwq, name, ptr)) != 0) {
192 if (auxerror)
193 *auxerror = strdup(name);
194 break;
195 }
196 }
197
198 (void)fclose(f);
199 return rv;
200 }
201
202 static int
203 filter_conf(const struct dirent *d)
204 {
205 const char *p;
206
207 if ((p = strstr(d->d_name, ".conf")) == NULL)
208 return 0;
209
210 if (p[5] != '\0')
211 return 0;
212
213 return 1;
214 }
215
216 static int
217 comp_func(const struct dirent **a, const struct dirent **b)
218 {
219 return strcmp ((*a)->d_name, (*b)->d_name);
220 }
221
222 /* parse the configuration file (if NULL then the default one) */
223 int
224 pwquality_read_config(pwquality_settings_t *pwq, const char *cfgfile, void **auxerror)
225 {
226 char *dirname;
227 struct dirent **namelist;
228 int n;
229 int i;
230 int rv = 0;
231
232 if (auxerror)
233 *auxerror = NULL;
234 if (cfgfile == NULL)
235 cfgfile = PWQUALITY_DEFAULT_CFGFILE;
236
237 /* read "*.conf" files from "<cfgfile>.d" directory first */
238
239 if (asprintf(&dirname, "%s.d", cfgfile) < 0)
240 return PWQ_ERROR_MEM_ALLOC;
241
242 /* we do not care about scandir races here so we use scandir */
243 n = scandir(dirname, &namelist, filter_conf, comp_func);
244
245 if (n < 0) {
246 namelist = NULL;
247
248 if (errno == ENOMEM) {
249 free(dirname);
250 return PWQ_ERROR_MEM_ALLOC;
251 } /* other errors are ignored */
252 }
253
254 for (i = 0; i < n; i++) {
255 char *subcfg;
256
257 if (rv) {
258 free(namelist[i]);
259 continue;
260 }
261
262 if (asprintf(&subcfg, "%s/%s", dirname, namelist[i]->d_name) < 0)
263 rv = PWQ_ERROR_MEM_ALLOC;
264 else {
265 rv = read_config_file(pwq, subcfg, auxerror);
266 if (rv == PWQ_ERROR_CFGFILE_OPEN)
267 rv = 0; /* ignore, this one does not modify auxerror */
268 free(subcfg);
269 }
270
271 free(namelist[i]);
272 }
273 free(dirname);
274 free(namelist);
275
276 if (rv)
277 return rv;
278
279 return read_config_file(pwq, cfgfile, auxerror);
280 }
281
282 /* useful for setting the options as configured on a pam module
283 * command line in form of <opt>=<val> */
284 int
285 pwquality_set_option(pwquality_settings_t *pwq, const char *option)
286 {
287 char name[80]; /* no options with name longer than that */
288 const char *value;
289 size_t len;
290
291 value = strchr(option, '=');
292 if (value == NULL) {
293 len = strlen(option);
294 value = option + len; /* just empty value */
295 } else {
296 len = value - option;
297 ++value;
298 }
299 if (len > sizeof(name) - 1)
300 return PWQ_ERROR_UNKNOWN_SETTING;
301
302 strncpy(name, option, len);
303 name[len] = '\0';
304
305 return set_name_value(pwq, name, value);
306 }
307
308 /* set value of an integer setting */
309 int
310 pwquality_set_int_value(pwquality_settings_t *pwq, int setting, int value)
311 {
312 switch(setting) {
313 case PWQ_SETTING_DIFF_OK:
314 pwq->diff_ok = value;
315 break;
316 case PWQ_SETTING_MIN_LENGTH:
317 if (value < PWQ_BASE_MIN_LENGTH)
318 value = PWQ_BASE_MIN_LENGTH;
319 pwq->min_length = value;
320 break;
321 case PWQ_SETTING_DIG_CREDIT:
322 pwq->dig_credit = value;
323 break;
324 case PWQ_SETTING_UP_CREDIT:
325 pwq->up_credit = value;
326 break;
327 case PWQ_SETTING_LOW_CREDIT:
328 pwq->low_credit = value;
329 break;
330 case PWQ_SETTING_OTH_CREDIT:
331 pwq->oth_credit = value;
332 break;
333 case PWQ_SETTING_MIN_CLASS:
334 if (value > PWQ_NUM_CLASSES)
335 value = PWQ_NUM_CLASSES;
336 pwq->min_class = value;
337 break;
338 case PWQ_SETTING_MAX_REPEAT:
339 pwq->max_repeat = value;
340 break;
341 case PWQ_SETTING_MAX_CLASS_REPEAT:
342 pwq->max_class_repeat = value;
343 break;
344 case PWQ_SETTING_MAX_SEQUENCE:
345 pwq->max_sequence = value;
346 break;
347 case PWQ_SETTING_GECOS_CHECK:
348 pwq->gecos_check = value;
349 break;
350 case PWQ_SETTING_DICT_CHECK:
351 pwq->dict_check = value;
352 break;
353 case PWQ_SETTING_USER_CHECK:
354 pwq->user_check = value;
355 break;
356 case PWQ_SETTING_USER_SUBSTR:
357 pwq->user_substr = value;
358 break;
359 case PWQ_SETTING_ENFORCING:
360 pwq->enforcing = value;
361 break;
362 case PWQ_SETTING_RETRY_TIMES:
363 pwq->retry_times = value;
364 break;
365 case PWQ_SETTING_ENFORCE_ROOT:
366 pwq->enforce_for_root = value;
367 break;
368 case PWQ_SETTING_LOCAL_USERS:
369 pwq->local_users_only = value;
370 break;
371 default:
372 return PWQ_ERROR_NON_INT_SETTING;
373 }
374
375 return 0;
376 }
377
378 /* set value of a string setting */
379 int
380 pwquality_set_str_value(pwquality_settings_t *pwq, int setting,
381 const char *value)
382 {
383 char *dup;
384
385 if (value == NULL || *value == '\0') {
386 dup = NULL;
387 } else {
388 dup = strdup(value);
389 if (dup == NULL)
390 return PWQ_ERROR_MEM_ALLOC;
391 }
392
393 switch(setting) {
394 case PWQ_SETTING_BAD_WORDS:
395 free(pwq->bad_words);
396 pwq->bad_words = dup;
397 break;
398 case PWQ_SETTING_DICT_PATH:
399 free(pwq->dict_path);
400 pwq->dict_path = dup;
401 break;
402 default:
403 free(dup);
404 return PWQ_ERROR_NON_STR_SETTING;
405 }
406
407 return 0;
408 }
409
410 /* get value of an integer setting */
411 int
412 pwquality_get_int_value(pwquality_settings_t *pwq, int setting, int *value)
413 {
414 switch(setting) {
415 case PWQ_SETTING_DIFF_OK:
416 *value = pwq->diff_ok;
417 break;
418 case PWQ_SETTING_MIN_LENGTH:
419 *value = pwq->min_length;
420 break;
421 case PWQ_SETTING_DIG_CREDIT:
422 *value = pwq->dig_credit;
423 break;
424 case PWQ_SETTING_UP_CREDIT:
425 *value = pwq->up_credit;
426 break;
427 case PWQ_SETTING_LOW_CREDIT:
428 *value = pwq->low_credit;
429 break;
430 case PWQ_SETTING_OTH_CREDIT:
431 *value = pwq->oth_credit;
432 break;
433 case PWQ_SETTING_MIN_CLASS:
434 *value = pwq->min_class;
435 break;
436 case PWQ_SETTING_MAX_REPEAT:
437 *value = pwq->max_repeat;
438 break;
439 case PWQ_SETTING_MAX_CLASS_REPEAT:
440 *value = pwq->max_class_repeat;
441 break;
442 case PWQ_SETTING_MAX_SEQUENCE:
443 *value = pwq->max_sequence;
444 break;
445 case PWQ_SETTING_GECOS_CHECK:
446 *value = pwq->gecos_check;
447 break;
448 case PWQ_SETTING_DICT_CHECK:
449 *value = pwq->dict_check;
450 break;
451 case PWQ_SETTING_USER_CHECK:
452 *value = pwq->user_check;
453 break;
454 case PWQ_SETTING_USER_SUBSTR:
455 *value = pwq->user_substr;
456 break;
457 case PWQ_SETTING_ENFORCING:
458 *value = pwq->enforcing;
459 break;
460 case PWQ_SETTING_RETRY_TIMES:
461 *value = pwq->retry_times;
462 break;
463 case PWQ_SETTING_ENFORCE_ROOT:
464 *value = pwq->enforce_for_root;
465 break;
466 case PWQ_SETTING_LOCAL_USERS:
467 *value = pwq->local_users_only;
468 break;
469 default:
470 return PWQ_ERROR_NON_INT_SETTING;
471 }
472 return 0;
473 }
474
475 /* get value of a string setting, or NULL if setting unknown */
476 int
477 pwquality_get_str_value(pwquality_settings_t *pwq, int setting, const char **value)
478 {
479 switch(setting) {
480 case PWQ_SETTING_BAD_WORDS:
481 *value = pwq->bad_words;
482 break;
483 case PWQ_SETTING_DICT_PATH:
484 #ifdef HAVE_CRACK_H
485 if (pwq->dict_path)
486 *value = pwq->dict_path;
487 else
488 *value = GetDefaultCracklibDict();
489 #else
490 *value = NULL;
491 #endif
492 break;
493 default:
494 return PWQ_ERROR_NON_STR_SETTING;
495 }
496 return 0;
497 }
498
499 /*
500 * Copyright (c) Red Hat, Inc, 2011, 2015
501 * Copyright (c) Tomas Mraz <[email protected]>, 2011, 2015
502 *
503 * Redistribution and use in source and binary forms, with or without
504 * modification, are permitted provided that the following conditions
505 * are met:
506 * 1. Redistributions of source code must retain the above copyright
507 * notice, and the entire permission notice in its entirety,
508 * including the disclaimer of warranties.
509 * 2. Redistributions in binary form must reproduce the above copyright
510 * notice, this list of conditions and the following disclaimer in the
511 * documentation and/or other materials provided with the distribution.
512 * 3. The name of the author may not be used to endorse or promote
513 * products derived from this software without specific prior
514 * written permission.
515 *
516 * ALTERNATIVELY, this product may be distributed under the terms of
517 * the GNU General Public License version 2 or later, in which case the
518 * provisions of the GPL are required INSTEAD OF the above restrictions.
519 *
520 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
521 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
522 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
523 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
524 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
525 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
526 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
527 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
528 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
529 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
530 * OF THE POSSIBILITY OF SUCH DAMAGE.
531 */