hyprlock/src/core/Password.cpp

63 lines
2 KiB
C++
Raw Normal View History

2024-02-19 00:08:03 +01:00
#include "Password.hpp"
#include "hyprlock.hpp"
2024-02-19 00:08:03 +01:00
#include <unistd.h>
#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <thread>
2024-02-19 00:08:03 +01:00
struct pam_response* reply;
//
int conv(int num_msg, const struct pam_message** msg, struct pam_response** resp, void* appdata_ptr) {
*resp = reply;
return PAM_SUCCESS;
}
static void passwordCheckTimerCallback(std::shared_ptr<CTimer> self, void* data) {
g_pHyprlock->onPasswordCheckTimer();
}
std::shared_ptr<CPassword::SVerificationResult> CPassword::verify(const std::string& pass) {
std::shared_ptr<CPassword::SVerificationResult> result = std::make_shared<CPassword::SVerificationResult>(false);
std::thread([this, result, pass]() {
const pam_conv localConv = {conv, NULL};
pam_handle_t* handle = NULL;
int ret = pam_start("su", getlogin(), &localConv, &handle);
2024-02-19 00:08:03 +01:00
if (ret != PAM_SUCCESS) {
result->success = false;
result->failReason = "pam_start failed";
result->realized = true;
g_pHyprlock->addTimer(std::chrono::milliseconds(1), passwordCheckTimerCallback, nullptr);
return;
}
2024-02-19 00:08:03 +01:00
reply = (struct pam_response*)malloc(sizeof(struct pam_response));
2024-02-19 00:08:03 +01:00
reply->resp = strdup(pass.c_str());
reply->resp_retcode = 0;
ret = pam_authenticate(handle, 0);
2024-02-19 00:08:03 +01:00
if (ret != PAM_SUCCESS) {
result->success = false;
result->failReason = ret == PAM_AUTH_ERR ? "Authentication failed" : "pam_authenticate failed";
result->realized = true;
g_pHyprlock->addTimer(std::chrono::milliseconds(1), passwordCheckTimerCallback, nullptr);
return;
}
2024-02-19 00:08:03 +01:00
ret = pam_end(handle, ret);
2024-02-19 00:08:03 +01:00
result->success = true;
result->failReason = "Successfully authenticated";
result->realized = true;
g_pHyprlock->addTimer(std::chrono::milliseconds(1), passwordCheckTimerCallback, nullptr);
}).detach();
2024-02-19 00:08:03 +01:00
return result;
2024-02-19 00:08:03 +01:00
}