Created
August 8, 2019 11:41
-
-
Save underdoeg/b2ca5d0790ca8fc3ee6c40015de93545 to your computer and use it in GitHub Desktop.
test user auth with pam on linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <security/pam_appl.h> | |
#include <security/pam_misc.h> | |
#include <string> | |
int pam_conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) { | |
auto pass = static_cast<std::string *>(appdata_ptr); | |
auto reply = (struct pam_response *) malloc(sizeof(struct pam_response)); | |
reply->resp = strdup(pass->c_str()); | |
reply->resp_retcode = 0; | |
*resp = reply; | |
return PAM_SUCCESS; | |
} | |
bool checkLogin(std::string user, std::string pass) { | |
pam_conv local_conversation = {pam_conversation, &pass}; | |
pam_handle_t *handle = nullptr; | |
auto checkError = [&](int retval) -> bool{ | |
if (retval != PAM_SUCCESS) { | |
LOG(ERROR) << pam_strerror(handle, retval) << " (" << user << ")"; | |
pam_end(handle, retval); | |
return false; | |
} | |
return true; | |
}; | |
if(!checkError(pam_start("system-auth", user.c_str(), &local_conversation, &handle))) return false; | |
if(!checkError(pam_authenticate(handle, 0))) return false; | |
pam_end(handle, 0); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment