summaryrefslogtreecommitdiffhomepage
path: root/auth.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-04-26 20:23:37 +0200
committerRoland Reichwein <mail@reichwein.it>2020-04-26 20:23:37 +0200
commit5b7e374d60985852a3b7388c0b622865cfb2dfc4 (patch)
tree208ccfabca389f9e413f43398d101727b11fac06 /auth.cpp
parenta595932283a3f3bf002eff5bf044762b78cab5f0 (diff)
Fix auth for Debian 10
Diffstat (limited to 'auth.cpp')
-rw-r--r--auth.cpp24
1 files changed, 10 insertions, 14 deletions
diff --git a/auth.cpp b/auth.cpp
index c9c9765..451c1ce 100644
--- a/auth.cpp
+++ b/auth.cpp
@@ -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;
}