hikari/hikari_unlocker.c

91 lines
1.8 KiB
C
Raw Permalink Normal View History

2020-02-05 04:02:33 -06:00
#include <pwd.h>
#include <security/pam_appl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/mman.h>
#include <unistd.h>
static char *input_buffer = NULL;
#define INPUT_BUFFER_SIZE 1024
2020-02-05 04:02:33 -06:00
static int
conversation_handler(int num_msg,
2024-02-24 01:00:11 -06:00
const struct pam_message **msg,
struct pam_response **resp,
void *data)
2020-02-05 04:02:33 -06:00
{
2024-02-24 01:00:11 -06:00
struct pam_response *pam_reply = calloc(num_msg, sizeof(struct pam_response));
2020-02-05 04:02:33 -06:00
2024-02-24 01:00:11 -06:00
if (pam_reply == NULL) {
return PAM_ABORT;
}
*resp = pam_reply;
for (int i = 0; i < num_msg; ++i) {
switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_OFF:
case PAM_PROMPT_ECHO_ON:
pam_reply[i].resp = strdup(input_buffer);
if (pam_reply[i].resp == NULL) {
return PAM_ABORT;
}
break;
2020-02-05 04:02:33 -06:00
2024-02-24 01:00:11 -06:00
case PAM_ERROR_MSG:
case PAM_TEXT_INFO:
break;
}
}
return PAM_SUCCESS;
2020-02-05 04:02:33 -06:00
}
bool
check_password(const char *username)
2020-02-05 04:02:33 -06:00
{
2024-02-24 01:00:11 -06:00
const struct pam_conv conv = {
.conv = conversation_handler,
.appdata_ptr = NULL,
};
2020-02-05 04:02:33 -06:00
2024-02-24 01:00:11 -06:00
bool success = false;
pam_handle_t *auth_handle = NULL;
if (pam_start("hikari-unlocker", username, &conv, &auth_handle) !=
PAM_SUCCESS) {
return false;
}
2020-02-05 04:02:33 -06:00
2024-02-24 01:00:11 -06:00
read(0, input_buffer, INPUT_BUFFER_SIZE - 1);
int pam_status = pam_authenticate(auth_handle, 0);
memset(input_buffer, 0, INPUT_BUFFER_SIZE);
success = pam_status == PAM_SUCCESS;
write(1, &success, sizeof(bool));
2020-02-05 04:02:33 -06:00
2024-02-24 01:00:11 -06:00
pam_end(auth_handle, pam_status);
2020-02-05 04:02:33 -06:00
2024-02-24 01:00:11 -06:00
return success;
2020-02-05 04:02:33 -06:00
}
int
main(int argc, char **argv)
{
2024-02-24 01:00:11 -06:00
char input;
bool success = false;
struct passwd *passwd = getpwuid(getuid());
2024-02-24 01:00:11 -06:00
input_buffer = malloc(INPUT_BUFFER_SIZE);
memset(input_buffer, 0, INPUT_BUFFER_SIZE);
mlock(input_buffer, INPUT_BUFFER_SIZE);
2020-02-05 04:02:33 -06:00
2024-02-24 01:00:11 -06:00
while (!success) {
success = check_password(passwd->pw_name);
}
2020-02-05 04:02:33 -06:00
2024-02-24 01:00:11 -06:00
munlock(input_buffer, INPUT_BUFFER_SIZE);
free(input_buffer);
2020-02-05 04:02:33 -06:00
2024-02-24 01:00:11 -06:00
return 0;
2020-02-05 04:02:33 -06:00
}