|
| 1 | +/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | + |
| 3 | +#include "dlfcn-util.h" |
| 4 | +#include "errno-util.h" |
| 5 | +#include "log.h" |
| 6 | +#include "macro.h" |
| 7 | +#include "memory-util.h" |
| 8 | +#include "password-quality-util.h" |
| 9 | +#include "strv.h" |
| 10 | + |
| 11 | +#if HAVE_PASSWDQC |
| 12 | + |
| 13 | +static void *passwdqc_dl = NULL; |
| 14 | + |
| 15 | +void (*sym_passwdqc_params_reset)(passwdqc_params_t *params); |
| 16 | +int (*sym_passwdqc_params_load)(passwdqc_params_t *params, char **reason, const char *pathname); |
| 17 | +int (*sym_passwdqc_params_parse)(passwdqc_params_t *params, char **reason, int argc, const char *const *argv); |
| 18 | +void (*sym_passwdqc_params_free)(passwdqc_params_t *params); |
| 19 | +const char *(*sym_passwdqc_check)(const passwdqc_params_qc_t *params, const char *newpass, const char *oldpass, const struct passwd *pw); |
| 20 | +char *(*sym_passwdqc_random)(const passwdqc_params_qc_t *params); |
| 21 | + |
| 22 | +int dlopen_passwdqc(void) { |
| 23 | + return dlopen_many_sym_or_warn( |
| 24 | + &passwdqc_dl, "libpasswdqc.so.1", LOG_DEBUG, |
| 25 | + DLSYM_ARG(passwdqc_params_reset), |
| 26 | + DLSYM_ARG(passwdqc_params_load), |
| 27 | + DLSYM_ARG(passwdqc_params_parse), |
| 28 | + DLSYM_ARG(passwdqc_params_free), |
| 29 | + DLSYM_ARG(passwdqc_check), |
| 30 | + DLSYM_ARG(passwdqc_random)); |
| 31 | +} |
| 32 | + |
| 33 | +static int pwqc_allocate_context(passwdqc_params_t **ret) { |
| 34 | + |
| 35 | + _cleanup_(sym_passwdqc_params_freep) passwdqc_params_t *params = NULL; |
| 36 | + _cleanup_free_ char *load_reason = NULL; |
| 37 | + int r; |
| 38 | + |
| 39 | + assert(ret); |
| 40 | + |
| 41 | + r = dlopen_passwdqc(); |
| 42 | + if (r < 0) |
| 43 | + return r; |
| 44 | + |
| 45 | + params = new0(passwdqc_params_t, 1); |
| 46 | + if (!params) |
| 47 | + return log_oom(); |
| 48 | + |
| 49 | + sym_passwdqc_params_reset(params); |
| 50 | + |
| 51 | + r = sym_passwdqc_params_load(params, &load_reason, "/etc/passwdqc.conf"); |
| 52 | + if (r < 0) { |
| 53 | + if (!load_reason) |
| 54 | + return log_oom(); |
| 55 | + log_debug("Failed to load passwdqc configuration file, ignoring: %s", load_reason); |
| 56 | + } |
| 57 | + |
| 58 | + *ret = TAKE_PTR(params); |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +int suggest_passwords(void) { |
| 63 | + |
| 64 | + _cleanup_(sym_passwdqc_params_freep) passwdqc_params_t *params = NULL; |
| 65 | + _cleanup_strv_free_erase_ char **suggestions = NULL; |
| 66 | + _cleanup_(erase_and_freep) char *joined = NULL; |
| 67 | + int r; |
| 68 | + |
| 69 | + r = pwqc_allocate_context(¶ms); |
| 70 | + if (r < 0) { |
| 71 | + if (ERRNO_IS_NOT_SUPPORTED(r)) |
| 72 | + return 0; |
| 73 | + return log_error_errno(r, "Failed to allocate libpasswdqc context: %m"); |
| 74 | + } |
| 75 | + |
| 76 | + suggestions = new0(char*, N_SUGGESTIONS+1); |
| 77 | + if (!suggestions) |
| 78 | + return log_oom(); |
| 79 | + |
| 80 | + for (size_t i = 0; i < N_SUGGESTIONS; i++) { |
| 81 | + suggestions[i] = sym_passwdqc_random(¶ms->qc); |
| 82 | + if (!suggestions[i]) |
| 83 | + return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to generate password, ignoring"); |
| 84 | + } |
| 85 | + |
| 86 | + joined = strv_join(suggestions, " "); |
| 87 | + if (!joined) |
| 88 | + return log_oom(); |
| 89 | + |
| 90 | + printf("Password suggestions: %s\n", joined); |
| 91 | + return 1; |
| 92 | +} |
| 93 | + |
| 94 | +int check_password_quality( |
| 95 | + const char *password, |
| 96 | + const char *old, |
| 97 | + const char *username, |
| 98 | + char **ret_error) { |
| 99 | + |
| 100 | + _cleanup_(sym_passwdqc_params_freep) passwdqc_params_t *params = NULL; |
| 101 | + const char *check_reason; |
| 102 | + int r; |
| 103 | + |
| 104 | + assert(password); |
| 105 | + |
| 106 | + r = pwqc_allocate_context(¶ms); |
| 107 | + if (r < 0) |
| 108 | + return log_debug_errno(r, "Failed to allocate libpasswdqc context: %m"); |
| 109 | + |
| 110 | + if (username) { |
| 111 | + const struct passwd pw = { |
| 112 | + .pw_name = (char *) username, |
| 113 | + /* |
| 114 | + * passwdqc_check() could use this information to check |
| 115 | + * whether the password is based on the personal login information, |
| 116 | + * but we cannot provide it. |
| 117 | + */ |
| 118 | + .pw_passwd = (char *) "", |
| 119 | + .pw_gecos = (char *) "", |
| 120 | + .pw_dir = (char *) "", |
| 121 | + .pw_shell = (char *) "" |
| 122 | + }; |
| 123 | + |
| 124 | + check_reason = sym_passwdqc_check(¶ms->qc, password, old, &pw); |
| 125 | + } else |
| 126 | + check_reason = sym_passwdqc_check(¶ms->qc, password, old, /* pw */ NULL); |
| 127 | + |
| 128 | + if (check_reason) { |
| 129 | + if (ret_error) { |
| 130 | + char *e = strdup(check_reason); |
| 131 | + if (!e) |
| 132 | + return log_oom(); |
| 133 | + *ret_error = e; |
| 134 | + } |
| 135 | + |
| 136 | + return 0; /* all bad */ |
| 137 | + } |
| 138 | + |
| 139 | + return 1; /* all good */ |
| 140 | +} |
| 141 | + |
| 142 | +#endif |
0 commit comments