Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6945387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:25:47+00:00 2026-05-27T13:25:47+00:00

For one of my projects, I want to implement a complete PAM implementation for

  • 0

For one of my projects, I want to implement a complete PAM implementation for Java (application side and module side as well).

Right now, I’m on the application side. I took jpam as a base but I stumble upon a problem, and after some hours of searching around I still cannot find the solution to my problem :/

This is the current code:

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;
}

What I want here is keep a reference to pamh for all instances of PamHandle. How is this done?

edit: OK, I have the answer to this, and now there’s the cleanup part: do I use finalize() to call a native cleanup method then super.finalize();, or is there a JNI function which is triggered by the GC which I can/must implement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:25:47+00:00Added an answer on May 27, 2026 at 1:25 pm

    Use a long to store a pointer to pam_handle_t.

    Java-side it would look like

    long handle = Pam.create();
    Pam.DoSomething(handle,arg1,arg2);
    

    Of course you could encapsulate this inside a class, so you could have the interface.

    PamHandle p = new PamHandle();
    p.DoSomething(arg1,arg2);
    

    C side it would look like this:

    JNIEXPORT jlong JNICALL Java_org_Create(
        JNIEnv *pEnv)
    {
        pam_handle_t *pamh = createNew pam_handle somehow
        jlong result = (jlong) pamh;
        return result;
    }
    
    JNIEXPORT jint JNICALL Java_org_Blah_Blah_blah(
        JNIEnv *pEnv, jlong handle, jstring arg1,jstring arg2)
    {
        pam_handle_t *pamh = (pam_handle_t*)handle;
     // ... Do rest of stuff
    }
    

    This would allow you to have one pam_handle_t per instance.
    It is also alot more performant to manually pass the integer each time, rather than passing an object, and then having to access the object’s field.

    EDIT

    Also, if you are worried about the jlong not being able to hold a pointer correctly, a jlong is guaranteed to be 64-bit. So, a jlong will work for this case all the way until we start getting 128-bit integers( a long long way away).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a solution that has two projects: One ASP.NET web application, and
I am stucked with an idea I want to implement into one of my
Hi i am using kannel in one of my projects and i want to
I am trying to implement Quartz.net in one of my projects. I am using
In one of my current side projects, I am scanning through some text looking
I want to implement MVC arch. in silverlight application. I have the following questions:
I'm currently considering using java in one of my projects(for reasons unrelated to networking).
i need to have one project on asp.net mvc 1 but i want to
I created one GWT webapplication project.Inthat i want to create servlet program,but in that
I want to create unit tests for one of my project's classes but I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.