diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-09-08 15:21:21 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2020-09-15 11:52:30 +0200 |
commit | 2ae297fe0d8b0736ee16b3be47ef62e17b4afe4e (patch) | |
tree | a274d294137dd41b31c24aff1ab16a4029628069 | |
parent | shared/libcrypt-util: use libcrypt_ra() (diff) | |
download | systemd-2ae297fe0d8b0736ee16b3be47ef62e17b4afe4e.tar.xz systemd-2ae297fe0d8b0736ee16b3be47ef62e17b4afe4e.zip |
Move test_password_{one,many} to libcrypt-util.c
They are only used under src/home/, but I want to add tests in test-libcrypt-util.c.
And the functions are almost trivial, so I think it is OK to move them to shared.
-rw-r--r-- | src/home/home-util.c | 33 | ||||
-rw-r--r-- | src/home/home-util.h | 3 | ||||
-rw-r--r-- | src/home/user-record-pwquality.c | 1 | ||||
-rw-r--r-- | src/shared/libcrypt-util.c | 32 | ||||
-rw-r--r-- | src/shared/libcrypt-util.h | 2 |
5 files changed, 35 insertions, 36 deletions
diff --git a/src/home/home-util.c b/src/home/home-util.c index 3fd57639f8..8e28e3ab76 100644 --- a/src/home/home-util.c +++ b/src/home/home-util.c @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include "dns-domain.h" -#include "errno-util.h" #include "home-util.h" #include "libcrypt-util.h" #include "memory-util.h" @@ -134,35 +133,3 @@ int bus_message_append_secret(sd_bus_message *m, UserRecord *secret) { return sd_bus_message_append(m, "s", formatted); } - -int test_password_one(const char *hashed_password, const char *password) { - struct crypt_data cc = {}; - const char *k; - bool b; - - errno = 0; - k = crypt_r(password, hashed_password, &cc); - if (!k) { - explicit_bzero_safe(&cc, sizeof(cc)); - return errno_or_else(EINVAL); - } - - b = streq(k, hashed_password); - explicit_bzero_safe(&cc, sizeof(cc)); - return b; -} - -int test_password_many(char **hashed_password, const char *password) { - char **hpw; - int r; - - STRV_FOREACH(hpw, hashed_password) { - r = test_password_one(*hpw, password); - if (r < 0) - return r; - if (r > 0) - return true; - } - - return false; -} diff --git a/src/home/home-util.h b/src/home/home-util.h index 6161d4c3d0..73602e4f8e 100644 --- a/src/home/home-util.h +++ b/src/home/home-util.h @@ -21,6 +21,3 @@ int bus_message_append_secret(sd_bus_message *m, UserRecord *secret); /* Many of our operations might be slow due to crypto, fsck, recursive chown() and so on. For these * operations permit a *very* long timeout */ #define HOME_SLOW_BUS_CALL_TIMEOUT_USEC (2*USEC_PER_MINUTE) - -int test_password_one(const char *hashed_password, const char *password); -int test_password_many(char **hashed_password, const char *password); diff --git a/src/home/user-record-pwquality.c b/src/home/user-record-pwquality.c index a5d632c772..08d7dc0169 100644 --- a/src/home/user-record-pwquality.c +++ b/src/home/user-record-pwquality.c @@ -3,6 +3,7 @@ #include "bus-common-errors.h" #include "errno-util.h" #include "home-util.h" +#include "libcrypt-util.h" #include "pwquality-util.h" #include "strv.h" #include "user-record-pwquality.h" diff --git a/src/shared/libcrypt-util.c b/src/shared/libcrypt-util.c index cb12cd65f3..1faf683d71 100644 --- a/src/shared/libcrypt-util.c +++ b/src/shared/libcrypt-util.c @@ -117,3 +117,35 @@ bool looks_like_hashed_password(const char *s) { return !STR_IN_SET(s, "x", "*"); } + +int test_password_one(const char *hashed_password, const char *password) { + struct crypt_data cc = {}; + const char *k; + bool b; + + errno = 0; + k = crypt_r(password, hashed_password, &cc); + if (!k) { + explicit_bzero_safe(&cc, sizeof(cc)); + return errno_or_else(EINVAL); + } + + b = streq(k, hashed_password); + explicit_bzero_safe(&cc, sizeof(cc)); + return b; +} + +int test_password_many(char **hashed_password, const char *password) { + char **hpw; + int r; + + STRV_FOREACH(hpw, hashed_password) { + r = test_password_one(*hpw, password); + if (r < 0) + return r; + if (r > 0) + return true; + } + + return false; +} diff --git a/src/shared/libcrypt-util.h b/src/shared/libcrypt-util.h index 2f8c352ab3..5be4e64a52 100644 --- a/src/shared/libcrypt-util.h +++ b/src/shared/libcrypt-util.h @@ -23,3 +23,5 @@ static inline int hash_password(const char *password, char **ret) { return hash_password_full(password, NULL, NULL, ret); } bool looks_like_hashed_password(const char *s); +int test_password_one(const char *hashed_password, const char *password); +int test_password_many(char **hashed_password, const char *password); |