| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#if HAVE_LIBCRYPT |
| 4 |
# include <crypt.h> |
| 5 |
#endif |
| 6 |
|
| 7 |
#include "sd-dlopen.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "dlfcn-util.h" |
| 11 |
#include "errno-util.h" |
| 12 |
#include "libcrypt-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "string-util.h" |
| 15 |
#include "strv.h" |
| 16 |
|
| 17 |
#if HAVE_LIBCRYPT |
| 18 |
#ifdef __GLIBC__ |
| 19 |
static DLSYM_PROTOTYPE(crypt_gensalt_ra) = NULL; |
| 20 |
static DLSYM_PROTOTYPE(crypt_preferred_method) = NULL; |
| 21 |
static DLSYM_PROTOTYPE(crypt_ra) = NULL; |
| 22 |
#else |
| 23 |
static DLSYM_PROTOTYPE(crypt_gensalt_ra) = missing_crypt_gensalt_ra; |
| 24 |
static DLSYM_PROTOTYPE(crypt_preferred_method) = missing_crypt_preferred_method; |
| 25 |
static DLSYM_PROTOTYPE(crypt_ra) = missing_crypt_ra; |
| 26 |
#endif |
| 27 |
|
| 28 |
int make_salt(char **ret) { |
| 29 |
const char *e; |
| 30 |
char *salt; |
| 31 |
int r; |
| 32 |
|
| 33 |
assert(ret); |
| 34 |
|
| 35 |
r = dlopen_libcrypt(LOG_DEBUG); |
| 36 |
if (r < 0) |
| 37 |
return r; |
| 38 |
|
| 39 |
e = secure_getenv("SYSTEMD_CRYPT_PREFIX"); |
| 40 |
if (!e) |
| 41 |
e = sym_crypt_preferred_method(); |
| 42 |
|
| 43 |
log_debug("Generating salt for hash prefix: %s", e); |
| 44 |
|
| 45 |
salt = sym_crypt_gensalt_ra(e, 0, NULL, 0); |
| 46 |
if (!salt) |
| 47 |
return -errno; |
| 48 |
|
| 49 |
*ret = salt; |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
int hash_password(const char *password, char **ret) { |
| 54 |
_cleanup_free_ char *salt = NULL; |
| 55 |
_cleanup_(erase_and_freep) void *cd_data = NULL; |
| 56 |
const char *p; |
| 57 |
int r, cd_size = 0; |
| 58 |
|
| 59 |
assert(password); |
| 60 |
assert(ret); |
| 61 |
|
| 62 |
r = make_salt(&salt); |
| 63 |
if (r < 0) |
| 64 |
return log_debug_errno(r, "Failed to generate salt: %m"); |
| 65 |
|
| 66 |
errno = 0; |
| 67 |
p = sym_crypt_ra(password, salt, &cd_data, &cd_size); |
| 68 |
if (!p) |
| 69 |
return log_debug_errno(errno_or_else(SYNTHETIC_ERRNO(EINVAL)), "crypt_ra() failed: %m"); |
| 70 |
|
| 71 |
return strdup_to(ret, p); |
| 72 |
} |
| 73 |
|
| 74 |
int test_password_one(const char *hashed_password, const char *password) { |
| 75 |
_cleanup_(erase_and_freep) void *cd_data = NULL; |
| 76 |
int r, cd_size = 0; |
| 77 |
const char *k; |
| 78 |
|
| 79 |
assert(hashed_password); |
| 80 |
assert(password); |
| 81 |
|
| 82 |
r = dlopen_libcrypt(LOG_DEBUG); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
|
| 86 |
errno = 0; |
| 87 |
k = sym_crypt_ra(password, hashed_password, &cd_data, &cd_size); |
| 88 |
if (!k) { |
| 89 |
if (errno == ENOMEM) |
| 90 |
return -ENOMEM; |
| 91 |
/* Unknown or unavailable hashing method or string too short */ |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
return streq(k, hashed_password); |
| 96 |
} |
| 97 |
|
| 98 |
int test_password_many(char **hashed_password, const char *password) { |
| 99 |
int r; |
| 100 |
|
| 101 |
assert(password); |
| 102 |
|
| 103 |
STRV_FOREACH(hpw, hashed_password) { |
| 104 |
r = test_password_one(*hpw, password); |
| 105 |
if (r < 0) |
| 106 |
return r; |
| 107 |
if (r > 0) |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
return false; |
| 112 |
} |
| 113 |
#endif |
| 114 |
|
| 115 |
bool looks_like_hashed_password(const char *s) { |
| 116 |
/* Returns false if the specified string is certainly not a hashed UNIX password. crypt(5) lists |
| 117 |
* various hashing methods. We only reject (return false) strings which are documented to have |
| 118 |
* different meanings. |
| 119 |
* |
| 120 |
* In particular, we allow locked passwords, i.e. strings starting with "!", including just "!", |
| 121 |
* i.e. the locked empty password. See also fc58c0c7bf7e4f525b916e3e5be0de2307fef04e. |
| 122 |
*/ |
| 123 |
if (!s) |
| 124 |
return false; |
| 125 |
|
| 126 |
s += strspn(s, "!"); /* Skip (possibly duplicated) locking prefix */ |
| 127 |
|
| 128 |
return !STR_IN_SET(s, "x", "*"); |
| 129 |
} |
| 130 |
|
| 131 |
int dlopen_libcrypt(int log_level) { |
| 132 |
#if HAVE_LIBCRYPT |
| 133 |
#ifdef __GLIBC__ |
| 134 |
static void *libcrypt_dl = NULL; |
| 135 |
static int cached = 0; |
| 136 |
int r = -ENOENT; |
| 137 |
|
| 138 |
if (libcrypt_dl) |
| 139 |
return 1; /* Already loaded */ |
| 140 |
|
| 141 |
if (cached < 0) |
| 142 |
return cached; /* Already tried, and failed. */ |
| 143 |
|
| 144 |
/* Several distributions like Debian/Ubuntu and OpenSUSE provide libxcrypt as libcrypt.so.1 |
| 145 |
* (libcrypt.so.1.1 on some architectures), while others like Fedora/CentOS and Arch provide it as |
| 146 |
* libcrypt.so.2. */ |
| 147 |
SD_ELF_NOTE_DLOPEN( |
| 148 |
"crypt", |
| 149 |
"Support for hashing passwords", |
| 150 |
SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED, |
| 151 |
"libcrypt.so.2", "libcrypt.so.1", "libcrypt.so.1.1"); |
| 152 |
|
| 153 |
FOREACH_STRING(soname, "libcrypt.so.2", "libcrypt.so.1", "libcrypt.so.1.1") { |
| 154 |
r = dlopen_many_sym_or_warn( |
| 155 |
&libcrypt_dl, soname, LOG_DEBUG, |
| 156 |
DLSYM_ARG(crypt_gensalt_ra), |
| 157 |
DLSYM_ARG(crypt_preferred_method), |
| 158 |
DLSYM_ARG(crypt_ra)); |
| 159 |
if (r >= 0) |
| 160 |
break; |
| 161 |
} |
| 162 |
if (r < 0) |
| 163 |
return cached = log_full_errno(log_level, r, "Failed to load libcrypt: %m"); |
| 164 |
#endif |
| 165 |
return 1; |
| 166 |
#else |
| 167 |
return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 168 |
"libcrypt support is not compiled in."); |
| 169 |
#endif |
| 170 |
} |