hyprlock/src/core/Password.cpp

67 lines
2.1 KiB
C++
Raw Normal View History

2024-02-18 23:08:03 +00:00
#include "Password.hpp"
#include "hyprlock.hpp"
2024-02-18 23:08:03 +00:00
#include <unistd.h>
#include <security/pam_appl.h>
core: Unbreak build on FreeBSD (#71) * Add OpenPAM support (used by BSDs except OpenBSD) -- Package 'pam', required by 'virtual:world', not found CMake Error at /usr/local/share/cmake/Modules/FindPkgConfig.cmake:619 (message): The following required packages were not found: - pam src/core/Password.cpp:6:10: fatal error: 'security/pam_misc.h' file not found 6 | #include <security/pam_misc.h> | ^~~~~~~~~~~~~~~~~~~~~ * Add missing headers src/renderer/widgets/IWidget.cpp: In member function 'virtual IWidget::SFormatResult IWidget::formatString(std::string)': src/renderer/widgets/IWidget.cpp:51:41: error: 'getlogin' was not declared in this scope 51 | replaceAll(in, "$USER", std::string{getlogin()}); | ^~~~~~~~ src/renderer/widgets/IWidget.cpp:51:51: error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(<brace-enclosed initializer list>)' 51 | replaceAll(in, "$USER", std::string{getlogin()}); | ^ src/renderer/DMAFrame.cpp: In member function 'bool CDMAFrame::onBufferDone()': src/renderer/DMAFrame.cpp:181:17: error: 'close' was not declared in this scope; did you mean 'pclose'? 181 | close(fd[plane_tmp]); | ^~~~~ | pclose src/renderer/DMAFrame.cpp:196:13: error: 'close' was not declared in this scope; did you mean 'pclose'? 196 | close(fd[plane]); | ^~~~~ | pclose src/core/Password.cpp: In lambda function: src/core/Password.cpp:44:31: error: 'strdup' was not declared in this scope 44 | reply->resp = strdup(pass.c_str()); | ^~~~~~ src/core/hyprlock.cpp: In member function 'void CHyprlock::spawnAsync(const std::string&)': src/core/hyprlock.cpp:768:9: error: 'sigemptyset' was not declared in this scope 768 | sigemptyset(&set); | ^~~~~~~~~~~ src/core/hyprlock.cpp:769:21: error: 'SIG_SETMASK' was not declared in this scope 769 | sigprocmask(SIG_SETMASK, &set, NULL); | ^~~~~~~~~~~ src/core/hyprlock.cpp:769:9: error: 'sigprocmask' was not declared in this scope 769 | sigprocmask(SIG_SETMASK, &set, NULL); | ^~~~~~~~~~~
2024-02-23 12:54:14 +00:00
#if __has_include(<security/pam_misc.h>)
2024-02-18 23:08:03 +00:00
#include <security/pam_misc.h>
core: Unbreak build on FreeBSD (#71) * Add OpenPAM support (used by BSDs except OpenBSD) -- Package 'pam', required by 'virtual:world', not found CMake Error at /usr/local/share/cmake/Modules/FindPkgConfig.cmake:619 (message): The following required packages were not found: - pam src/core/Password.cpp:6:10: fatal error: 'security/pam_misc.h' file not found 6 | #include <security/pam_misc.h> | ^~~~~~~~~~~~~~~~~~~~~ * Add missing headers src/renderer/widgets/IWidget.cpp: In member function 'virtual IWidget::SFormatResult IWidget::formatString(std::string)': src/renderer/widgets/IWidget.cpp:51:41: error: 'getlogin' was not declared in this scope 51 | replaceAll(in, "$USER", std::string{getlogin()}); | ^~~~~~~~ src/renderer/widgets/IWidget.cpp:51:51: error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(<brace-enclosed initializer list>)' 51 | replaceAll(in, "$USER", std::string{getlogin()}); | ^ src/renderer/DMAFrame.cpp: In member function 'bool CDMAFrame::onBufferDone()': src/renderer/DMAFrame.cpp:181:17: error: 'close' was not declared in this scope; did you mean 'pclose'? 181 | close(fd[plane_tmp]); | ^~~~~ | pclose src/renderer/DMAFrame.cpp:196:13: error: 'close' was not declared in this scope; did you mean 'pclose'? 196 | close(fd[plane]); | ^~~~~ | pclose src/core/Password.cpp: In lambda function: src/core/Password.cpp:44:31: error: 'strdup' was not declared in this scope 44 | reply->resp = strdup(pass.c_str()); | ^~~~~~ src/core/hyprlock.cpp: In member function 'void CHyprlock::spawnAsync(const std::string&)': src/core/hyprlock.cpp:768:9: error: 'sigemptyset' was not declared in this scope 768 | sigemptyset(&set); | ^~~~~~~~~~~ src/core/hyprlock.cpp:769:21: error: 'SIG_SETMASK' was not declared in this scope 769 | sigprocmask(SIG_SETMASK, &set, NULL); | ^~~~~~~~~~~ src/core/hyprlock.cpp:769:9: error: 'sigprocmask' was not declared in this scope 769 | sigprocmask(SIG_SETMASK, &set, NULL); | ^~~~~~~~~~~
2024-02-23 12:54:14 +00:00
#endif
2024-02-18 23:08:03 +00:00
core: Unbreak build on FreeBSD (#71) * Add OpenPAM support (used by BSDs except OpenBSD) -- Package 'pam', required by 'virtual:world', not found CMake Error at /usr/local/share/cmake/Modules/FindPkgConfig.cmake:619 (message): The following required packages were not found: - pam src/core/Password.cpp:6:10: fatal error: 'security/pam_misc.h' file not found 6 | #include <security/pam_misc.h> | ^~~~~~~~~~~~~~~~~~~~~ * Add missing headers src/renderer/widgets/IWidget.cpp: In member function 'virtual IWidget::SFormatResult IWidget::formatString(std::string)': src/renderer/widgets/IWidget.cpp:51:41: error: 'getlogin' was not declared in this scope 51 | replaceAll(in, "$USER", std::string{getlogin()}); | ^~~~~~~~ src/renderer/widgets/IWidget.cpp:51:51: error: no matching function for call to 'std::__cxx11::basic_string<char>::basic_string(<brace-enclosed initializer list>)' 51 | replaceAll(in, "$USER", std::string{getlogin()}); | ^ src/renderer/DMAFrame.cpp: In member function 'bool CDMAFrame::onBufferDone()': src/renderer/DMAFrame.cpp:181:17: error: 'close' was not declared in this scope; did you mean 'pclose'? 181 | close(fd[plane_tmp]); | ^~~~~ | pclose src/renderer/DMAFrame.cpp:196:13: error: 'close' was not declared in this scope; did you mean 'pclose'? 196 | close(fd[plane]); | ^~~~~ | pclose src/core/Password.cpp: In lambda function: src/core/Password.cpp:44:31: error: 'strdup' was not declared in this scope 44 | reply->resp = strdup(pass.c_str()); | ^~~~~~ src/core/hyprlock.cpp: In member function 'void CHyprlock::spawnAsync(const std::string&)': src/core/hyprlock.cpp:768:9: error: 'sigemptyset' was not declared in this scope 768 | sigemptyset(&set); | ^~~~~~~~~~~ src/core/hyprlock.cpp:769:21: error: 'SIG_SETMASK' was not declared in this scope 769 | sigprocmask(SIG_SETMASK, &set, NULL); | ^~~~~~~~~~~ src/core/hyprlock.cpp:769:9: error: 'sigprocmask' was not declared in this scope 769 | sigprocmask(SIG_SETMASK, &set, NULL); | ^~~~~~~~~~~
2024-02-23 12:54:14 +00:00
#include <cstring>
#include <thread>
2024-02-18 23:08:03 +00: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;
2024-03-02 00:49:44 +01:00
int ret = pam_start("hyprlock", getlogin(), &localConv, &handle);
2024-02-18 23:08:03 +00: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-18 23:08:03 +00:00
reply = (struct pam_response*)malloc(sizeof(struct pam_response));
2024-02-18 23:08:03 +00:00
reply->resp = strdup(pass.c_str());
reply->resp_retcode = 0;
ret = pam_authenticate(handle, 0);
2024-02-18 23:08:03 +00: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-18 23:08:03 +00:00
ret = pam_end(handle, ret);
2024-02-18 23:08:03 +00:00
result->success = true;
result->failReason = "Successfully authenticated";
result->realized = true;
g_pHyprlock->addTimer(std::chrono::milliseconds(1), passwordCheckTimerCallback, nullptr);
}).detach();
2024-02-18 23:08:03 +00:00
return result;
2024-03-02 00:49:44 +01:00
}