diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-04-26 20:23:37 +0200 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-04-26 20:23:37 +0200 |
commit | 5b7e374d60985852a3b7388c0b622865cfb2dfc4 (patch) | |
tree | 208ccfabca389f9e413f43398d101727b11fac06 | |
parent | a595932283a3f3bf002eff5bf044762b78cab5f0 (diff) |
Fix auth for Debian 10
-rw-r--r-- | auth.cpp | 24 |
1 files changed, 10 insertions, 14 deletions
@@ -11,16 +11,18 @@ std::string Auth::generate(const std::string& pw) { struct crypt_data data; memset((void *)&data, '\0', sizeof(data)); + + char setting[1000]; - if (crypt_gensalt_rn("$6$", 2000, nullptr, 0, data.setting, sizeof(data.setting)) == nullptr) + if (crypt_gensalt_rn("$6$", 2000, nullptr, 0, setting, sizeof(setting)) == nullptr) throw std::runtime_error("Error on crypt_gensalt_r()"); - strncpy(data.input, pw.data(), sizeof(data.input)); + char* result; - if (crypt_r(data.input, data.setting, &data) == nullptr) + if ((result = crypt_r(pw.data(), setting, &data)) == nullptr) throw std::runtime_error("Error on crypt_r()"); - return data.output; + return result; } // validate specified password against crypted hash @@ -35,20 +37,14 @@ bool Auth::validate(const std::string& crypted, const std::string& pw) return false; } - if (sizeof(data.setting) <= pos) { - std::cerr << "Warning: Bad password hash configured (salt size)" << std::endl; - return false; - } - - memcpy(&data.setting, crypted.data(), pos); - - strncpy(data.input, pw.data(), sizeof(data.input)); + std::string setting{crypted.substr(0, pos)}; - if (crypt_r(data.input, data.setting, &data) == nullptr) { + char* output; + if ((output = crypt_r(pw.data(), setting.data(), &data)) == nullptr) { std::cerr << "Warning: Error on crypt_r()" << std::endl; return false; } - return crypted == data.output; + return crypted == output; } |