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