service/pam: move pam execution to subprocess to allow killing it

Many pam modules can't be aborted well without this.
This commit is contained in:
outfoxxed 2024-06-18 03:29:25 -07:00
parent b5c8774a79
commit e89035b18c
Signed by untrusted user: outfoxxed
GPG key ID: 4C88A185FB89301E
10 changed files with 480 additions and 173 deletions

69
src/services/pam/ipc.cpp Normal file
View file

@ -0,0 +1,69 @@
#include "ipc.hpp"
#include <cstddef>
#include <cstdint>
#include <string>
#include <unistd.h>
PamIpcPipes::~PamIpcPipes() {
if (this->fdIn != 0) close(this->fdIn);
if (this->fdOut != 0) close(this->fdOut);
}
bool PamIpcPipes::readBytes(char* buffer, size_t length) const {
size_t i = 0;
while (i < length) {
auto count = read(this->fdIn, buffer + i, length - i); // NOLINT
if (count == -1 || count == 0) {
return false;
}
i += count;
}
return true;
}
bool PamIpcPipes::writeBytes(const char* buffer, size_t length) const {
size_t i = 0;
while (i < length) {
auto count = write(this->fdOut, buffer + i, length - i); // NOLINT
if (count == -1 || count == 0) {
return false;
}
i += count;
}
return true;
}
std::string PamIpcPipes::readString(bool* ok) const {
if (ok != nullptr) *ok = false;
uint32_t length = 0;
if (!this->readBytes(reinterpret_cast<char*>(&length), sizeof(length))) { // NOLINT
return "";
}
char data[length]; // NOLINT
if (!this->readBytes(data, length)) {
return "";
}
if (ok != nullptr) *ok = true;
return std::string(data, length);
}
bool PamIpcPipes::writeString(const std::string& string) const {
uint32_t length = string.length();
if (!this->writeBytes(reinterpret_cast<char*>(&length), sizeof(length))) { // NOLINT
return false;
}
return this->writeBytes(string.data(), string.length());
}