From 40d73948a9e3705d0a807d0afb35e53feb91385b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 10 Sep 2018 15:19:18 +0300 Subject: [PATCH] MXS-1662: Move mxs_crypt into utils Moved the mxs_crypt function into utils and renamed to mxs::crypt (no C code used it). --- include/maxscale/utils.hh | 10 ++++++++++ server/core/adminusers.cc | 17 ----------------- server/core/utils.cc | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/include/maxscale/utils.hh b/include/maxscale/utils.hh index 88723c805..456c3bf9e 100644 --- a/include/maxscale/utils.hh +++ b/include/maxscale/utils.hh @@ -557,6 +557,16 @@ uint64_t get_byteN(const uint8_t* ptr, int bytes); */ uint8_t* set_byteN(uint8_t* ptr, uint64_t value, int bytes); +/** + * C++ wrapper function for the `crypt` password hashing + * + * @param password Password to hash + * @param salt Salt to use (see man crypt) + * + * @return The hashed password + */ +std::string crypt(const std::string& password, const std::string& salt); + /** * Get kernel version * diff --git a/server/core/adminusers.cc b/server/core/adminusers.cc index 28056b00c..de339d3c0 100644 --- a/server/core/adminusers.cc +++ b/server/core/adminusers.cc @@ -421,23 +421,6 @@ bool admin_linux_account_enabled(const char* uname) return rv; } -#define MXS_CRYPT_SIZE 60 - -void mxs_crypt(const char* password, const char* salt, char* output) -{ -#if HAVE_GLIBC - struct crypt_data cdata; - cdata.initialized = 0; - char* pw = crypt_r(password, salt, &cdata); - snprintf(output, MXS_CRYPT_SIZE, "%s", pw); -#else - static std::mutex mxs_crypt_lock; - std::lock_guard guard(mxs_crypt_lock); - char* pw = crypt(password, salt); - snprintf(output, MXS_CRYPT_SIZE, "%s", pw); -#endif -} - /** * Add insecure remote (network) basic user. * diff --git a/server/core/utils.cc b/server/core/utils.cc index 99d4cb20e..dd22444e8 100644 --- a/server/core/utils.cc +++ b/server/core/utils.cc @@ -42,6 +42,8 @@ #include #include #include +#include +#include #include #include @@ -1168,6 +1170,20 @@ int64_t get_total_memory() namespace maxscale { +std::string crypt(const std::string& password, const std::string& salt) +{ +#if HAVE_GLIBC + struct crypt_data cdata; + cdata.initialized = 0; + return crypt_r(password.c_str(), salt.c_str(), &cdata); +#else + static std::mutex mxs_crypt_lock; + std::lock_guard guard(mxs_crypt_lock); + std::string pw = crypt(password.c_str(), salt.c_str()); + return pw; +#endif +} + std::string to_hex(uint8_t value) { std::string out;