-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-pam.c
80 lines (67 loc) · 2.12 KB
/
test-pam.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
/*
You need to add the following (or equivalent) to the
/etc/pam.d/check_user file:
# check authorization
auth required pam_unix.so
account required pam_unix.so
*/
#ifdef MACOSX
#include <pam/pam_appl.h>
#else
#include <security/pam_appl.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr);
int authenticate(const char *service,const char *user, const char *passwd);
int main(int argc, char *argv[])
{
pam_handle_t *pamh=NULL;
int ret = 0;
const char *user="mog";
const char *password = "tester";
if(argc == 2) {
user = argv[1];
}
if(argc > 2) {
fprintf(stderr, "Usage: check_user [username]\n");
exit(1);
}
ret = authenticate("check_user", user, password);
switch (ret) {
case PAM_SUCCESS:
printf("yay!\n");
break;
default:
printf("fail\n");
break;
}
return 0;
}
static int conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) {
/* return the supplied password back to PAM */
*resp = calloc(num_msg, sizeof(struct pam_response));
(*resp)[0].resp = strdup((char *) appdata_ptr);
(*resp)[0].resp_retcode = 0;
/* do not accept empty passwords */
return ((*resp)[0].resp ? PAM_SUCCESS : PAM_CONV_ERR);
}
int authenticate(const char *service,const char *user, const char *passwd) {
struct pam_conv conv = { conversation, (void *) passwd };
pam_handle_t *pamh = NULL;
int ret;
ret = pam_start(service, user, &conv, &pamh);
if (ret != PAM_SUCCESS) {
return ret;
}
ret = pam_authenticate(pamh, PAM_SILENT);
if (ret != PAM_SUCCESS) {
return ret;
}
ret = pam_end(pamh, 0);
if (ret != PAM_SUCCESS) {
return ret;
}
return PAM_SUCCESS;
}