rublon-ssh/PAM/ssh/include/rublon/pam.hpp
2023-07-21 10:55:06 +02:00

62 lines
1.7 KiB
C++

#pragma once
#include <security/pam_ext.h>
#include <security/pam_appl.h>
#include <security/pam_client.h>
#include <security/pam_ext.h>
#include <security/pam_misc.h>
#include <security/pam_modules.h>
#include <optional>
#include <string>
#include <system_error>
#include <type_traits>
#include <utility>
#include <memory>
#include "utils.hpp"
class LinuxPam {
pam_handle_t * pamh;
public:
LinuxPam(pam_handle_t * handler) : pamh{handler} {}
rublon::NonOwningPtr< const char > ip() const {
const void * ip = NULL;
pam_get_item(pamh, PAM_RHOST, &ip);
if(ip == NULL)
ip = "";
rublon::debugLog(__PRETTY_FUNCTION__, (const char*)ip);
return (const char*)ip;
}
rublon::NonOwningPtr< const char > username() const {
const char * user = NULL;
pam_get_user(pamh, &user, nullptr);
if(user == NULL)
user = "";
rublon::debugLog(__PRETTY_FUNCTION__, user);
return user;
}
template < typename... Ti >
void print(const char * fmt, Ti... ti) const noexcept {
pam_prompt(pamh, PAM_TEXT_INFO, nullptr, fmt, std::forward< Ti... >(ti...));
}
template < typename Fun, typename... Ti >
[[nodiscard]] auto scan(Fun && f, const char * fmt, Ti... ti) const noexcept {
char * responseBuffer = nullptr;
pam_prompt(pamh, PAM_PROMPT_ECHO_ON, &responseBuffer, fmt, std::forward< Ti... >(ti...));
if(responseBuffer) {
auto ret = f(responseBuffer);
free(responseBuffer);
return std::optional{ret};
}
return std::optional< std::result_of_t< Fun(char *) > >();
}
};