对于我的一个项目,我想为 Java(应用程序端和模块端)实现一个完整的 PAM 实现。
现在,我在应用程序方面。我以 jpam 为基础,但偶然发现了一个问题,经过几个小时的搜索,我仍然找不到解决问题的方法:/
这是当前代码:
JNIEXPORT jint JNICALL Java_org_eel_kitchen_pam_PamHandle_authenticate(
JNIEnv *pEnv, jobject pObj, jstring pServiceName, jstring pUsername,
jstring pPassword, jboolean debug)
{
pam_handle_t *pamh = NULL;
int retval;
/*
* TODO: unclear, see what's what
*
* With my first tests, it appears that GetStringUTFChars() makes the JVM
* crash if memory cannot be allocated... But an array copy was made. See
* what happens if the JVM decides NOT to make a copy. Right now it is
* assumed that allocations succeed. And the JNI spec says
* GetStringUTFChars() does NOT throw an OOM on failure.
*/
service_name = (*pEnv)->GetStringUTFChars(pEnv, pServiceName, NULL);
username = (*pEnv)->GetStringUTFChars(pEnv, pUsername, NULL);
password = (*pEnv)->GetStringUTFChars(pEnv, pPassword, NULL);
/* Get a handle to a PAM instance */
retval = pam_start(service_name, username, &PAM_converse, &pamh);
if (retval != PAM_SUCCESS) {
pr_debug("pam_start failed for service %s: %s\n", service_name,
pam_strerror(NULL, retval));
goto out_nohandle;
}
pam_set_item(pamh, PAM_AUTHTOK, password);
retval = pam_authenticate(pamh, 0);
/* Is user permitted access? */
if (retval != PAM_SUCCESS) {
pr_debug("failed to authenticate user %s: %s\n", username,
pam_strerror(NULL, retval));
goto out_free;
}
retval = pam_acct_mgmt(pamh, 0);
if (retval != PAM_SUCCESS)
pr_debug("failed to setup account for user %s: %s\n", username,
pam_strerror(NULL, retval));
out_free:
/* Clean up our handles and variables */
if (pam_end(pamh, retval) != PAM_SUCCESS) {
pamh = NULL;
pr_debug("Fuchs! Failed to release PAM handle\n");
}
out_nohandle:
(*pEnv)->ReleaseStringUTFChars(pEnv, pServiceName, service_name);
(*pEnv)->ReleaseStringUTFChars(pEnv, pUsername, username);
(*pEnv)->ReleaseStringUTFChars(pEnv, pPassword, password);
return retval;
}
我在这里想要的是pamh
为所有PamHandle
. 这是怎么做到的?
编辑:好的,我有这个答案,现在是清理部分:我是否使用finalize()
调用本机清理方法super.finalize();
,或者是否有一个由我可以/必须实现的 GC 触发的 JNI 函数?