-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.c
96 lines (85 loc) · 2.52 KB
/
provider.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <string.h>
#include <security/pam_ext.h>
#include <time.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/param.h>
#define LIST_PATH "/etc/daily-word/list"
#define LIST_LENGTH 119
#define LINE_LENGTH 5
#define TRIES_MIN 1
#define TRIES_STD 6
#define TRIES_MAX 30
int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return PAM_PERM_DENIED;
}
int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
int tries = TRIES_STD;
for (int i = 0; i < argc; i++)
{
if (strncmp(argv[i], "tries=", 6) == 0)
{
const char *s = &argv[i][6];
long val = atoi(s);
if (val >= TRIES_MIN && val <= TRIES_MAX)
{
tries = (int)val;
}
}
}
printf("tries: %d\n", tries);
// get the line of the day
time_t t = time(NULL);
struct tm *lt = localtime(&t);
int chosen_line = (lt->tm_yday % LIST_LENGTH);
pam_syslog(pamh, LOG_NOTICE, "Chosen line number: %d", chosen_line);
// read line from list
FILE *list_file = fopen(LIST_PATH, "r");
char line[LINE_LENGTH + 2];
for (int i = 0; i < chosen_line + 1; i++)
{
fgets(line, LINE_LENGTH + 2, list_file);
}
fclose(list_file);
char wordle[LINE_LENGTH + 1] = {"\0"};
strncpy(wordle, line, LINE_LENGTH);
pam_syslog(pamh, LOG_NOTICE, "Chosen line text: %s", line);
for (int i = 0; i < tries; i++)
{
char *user_response;
int return_value = pam_prompt(pamh, PAM_PROMPT_ECHO_ON, &user_response, "Guess wordle: ");
if (return_value != PAM_SUCCESS)
{
pam_syslog(pamh, LOG_ERR, "pam_prompt failed with return code %i", return_value);
}
printf("Response: ");
for (size_t i = 0; i < MIN(strlen(user_response), strlen(wordle)); i++)
{
if (wordle[i] == user_response[i])
{
printf("\033[0;32m%c\033[0m", user_response[i]);
}
else if (strchr(wordle, user_response[i]) != NULL)
{
printf("\033[0;33m%c\033[0m", user_response[i]);
}
else
{
printf("%c", user_response[i]);
}
}
printf("\n");
if (strcmp(user_response, wordle) == 0)
{
return PAM_SUCCESS;
}
}
return PAM_AUTH_ERR;
}
int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
{
return PAM_PERM_DENIED;
}