Search code examples
clinuxubuntu-14.04pam

Why PAM module code isn't working in my ubuntu?


I implemented basic PAM module and test application from this github link.

In the src folder it has a simple PAM module and test code for it.

PAM module code mypam.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>

/* expected hook */
PAM_EXTERN int pam_sm_setcred( pam_handle_t *pamh, int flags, int argc, const char **argv ) {
    return PAM_SUCCESS;
}

PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc, const char **argv) {
    printf("Acct mgmt\n");
    return PAM_SUCCESS;
}

/* expected hook, this is where custom stuff happens */
PAM_EXTERN int pam_sm_authenticate( pam_handle_t *pamh, int flags,int argc, const char **argv ) {
    int retval;

    const char* pUsername;
    retval = pam_get_user(pamh, &pUsername, "Username: ");

    printf("Welcome %s\n", pUsername);

    if (retval != PAM_SUCCESS) {
        return retval;
    }

    if (strcmp(pUsername, "backdoor") != 0) {
        return PAM_AUTH_ERR;
    }

    return PAM_SUCCESS;
}

Test code test.c:

#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <stdio.h>

const struct pam_conv conv = {
    misc_conv,
    NULL
};

int main(int argc, char *argv[]) {
    pam_handle_t* pamh = NULL;
    int retval;
    const char* user = "nobody";

    if(argc != 2) {
        printf("Usage: app [username]\n");
        exit(1);
    }

    user = argv[1];

    retval = pam_start("check_user", user, &conv, &pamh);

    // Are the credentials correct?
    if (retval == PAM_SUCCESS) {
        printf("Credentials accepted.\n");
        retval = pam_authenticate(pamh, 0);
    }

    // Can the accound be used at this time?
    if (retval == PAM_SUCCESS) {
        printf("Account is valid.\n");
        retval = pam_acct_mgmt(pamh, 0);
    }

    // Did everything work?
    if (retval == PAM_SUCCESS) {
        printf("Authenticated\n");
    } else {
        printf("Not Authenticated\n");
    }

    // close PAM (end session)
    if (pam_end(pamh, retval) != PAM_SUCCESS) {
        pamh = NULL;
        printf("check_user: failed to release authenticator\n");
        exit(1);
    }

    return retval == PAM_SUCCESS ? 0 : 1;
}

I built the module according to the github link instructions:

gcc -fPIC -fno-stack-protector -c src/mypam.c

sudo ld -x --shared -o /lib/security/mypam.so mypam.o

sudo ld -x --shared -o /lib/x86_64-linux-gnu/security/mypam.so mypam.o

gcc -o pam_test src/test.c -lpam -lpam_misc

I put below two command into /etc/pam.d/common-auth at the top.

auth sufficient mypam.so
account sufficient mypam.s

According to the site:

To run the test program, just do: pam_test backdoor and you should get some messages saying that you're authenticated!

But I got following error:

abnormal@abnormal:~/Desktop$ pam_test backdoor
No command 'pam_test' found, did you mean:
 Command 'pim_test' from package 'styx' (universe)
pam_test: command not found
abnormal@abnormal:~/Desktop$ 

what sholud I do now? I am using ubuntu 14,04 LTS. Please help.


Solution

  • There's nothing wrong with the code, but the invocation. You shoud use this:

    abnormal@abnormal:~/Desktop$ ./pam_test backdoor
    

    Unlike Windows, the current directory usually is not part of the search PATH on Linux.