-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpam.c
70 lines (57 loc) · 1.98 KB
/
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
#include <security/pam_appl.h>
#include <string.h>
#include <stdlib.h>
static int my_conv(int num_msg, const struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr) {
int i = 0;
struct pam_response *reply;
reply = (struct pam_response *) calloc(num_msg, sizeof(struct pam_response));
if (!reply) return PAM_CONV_ERR;
while (i < num_msg) {
/* printf("msg[%d]: %d '%s'\n", i, msg[i]->msg_style, msg[i]->msg); */
reply[i].resp_retcode = 0;
reply[i].resp = appdata_ptr ? strdup((char*)appdata_ptr) : strdup("");
i++;
}
*resp = reply;
return PAM_SUCCESS;
}
/* returns 1 if the user has been authenticated, 0 otherwise */
static int check_user_pam(const char *app, const char *user, const char *pwd) {
pam_handle_t *pamh = NULL;
int retval, ok = 0;
struct pam_conv conv = {
my_conv,
(void*)pwd
};
retval = pam_start(app, user, &conv, &pamh);
if (retval == PAM_SUCCESS)
retval = pam_authenticate(pamh, 0); /* is user really user? */
if (retval == PAM_SUCCESS)
retval = pam_acct_mgmt(pamh, 0); /* permitted access? */
ok = (retval == PAM_SUCCESS) ? 1 : 0;
if (pamh)
pam_end(pamh, retval);
return ok;
}
#include <jni.h>
JNIEXPORT jboolean JNICALL Java_com_att_research_RCloud_PAM_PAMchkUser(JNIEnv *, jclass, jstring, jstring, jstring);
jboolean JNICALL Java_com_att_research_RCloud_PAM_PAMchkUser(JNIEnv *env, jclass cls, jstring sApp, jstring sUser, jstring sPwd) {
const char *app, *usr, *pwd;
int res = 0;
if (!env || !sApp || !sUser || !sPwd) return 0;
app = (*env)->GetStringUTFChars(env, sApp, 0);
if (app) {
usr = (*env)->GetStringUTFChars(env, sUser, 0);
if (usr) {
pwd = (*env)->GetStringUTFChars(env, sPwd, 0);
if (pwd) {
res = check_user_pam(app, usr, pwd);
(*env)->ReleaseStringUTFChars(env, sPwd, pwd);
}
(*env)->ReleaseStringUTFChars(env, sUser, usr);
}
(*env)->ReleaseStringUTFChars(env, sApp, app);
}
return (jboolean) res;
}