Search code examples
cpam

Why can't my program link pam_* calls


I'm trying to compile and build the following very simple and straight-forward C program:

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

// Define custom PAM conversation function
int custom_conversation(int num_msg, const struct pam_message** msg, struct pam_response** resp, void* appdata_ptr)
{
    // Provide password for the PAM conversation response that was passed into appdata_ptr
    struct pam_response* reply = (struct pam_response* )malloc(sizeof(struct pam_response));
    reply[0].resp = (char*)appdata_ptr;
    reply[0].resp_retcode = 0;

    *resp = reply;

    return PAM_SUCCESS;
}

int main (int argc, char* argv[]) 
{
    if (argc > 2)
    {
        // Set up a custom PAM conversation passing in authentication password
        char* password = (char*)malloc(strlen(argv[2]) + 1);
        strcpy(password, argv[2]);        
        struct pam_conv pamc = { custom_conversation, password };
        pam_handle_t* pamh; 
        int retval;

        // Start PAM - just associate with something simple like the "whoami" command
        if ((retval = pam_start("whoami", argv[1], &pamc, &pamh)) == PAM_SUCCESS)
        {
            // Authenticate the user
            if ((retval = pam_authenticate(pamh, 0)) == PAM_SUCCESS) 
                fprintf(stdout, "OK\n"); 
            else 
                fprintf(stderr, "FAIL: pam_authentication failed.\n"); 

            // All done
            pam_end(pamh, 0); 
            return retval; 
        }
        else
        {
            fprintf(stderr, "FAIL: pam_start failed.\n"); 
            return retval;
        }
    }

    fprintf(stderr, "FAIL: expected two arguments for user name and password.\n"); 
    return 1; 
}

I found it somewhere on google. The problem is that it doesn't find the main pam_* symbols:

X@Y:~$ gcc -o checkpwd -lpam -lpam_misc checkpwd.c
/tmp/ccjIBywU.o: In Funktion `main': 
checkpwd.c:(.text+0xcf): Nicht definierter Verweis auf `pam_start'  
checkpwd.c:(.text+0xe9): Nicht definierter Verweis auf `pam_authenticate'
checkpwd.c:(.text+0x141): Nicht definierter Verweis auf `pam_end' 
collect2: error: ld returned 1 exit status

Sorry for the German, it means sth like "missing symbol". And that is my question: Why does it not find the symbols? Which libraries do I need to include? When I do an nm on libpam.a I can find all the symbols:

X@Y:~$ nm /usr/lib/x86_64-linux-gnu/libpam.a | grep pam_start
             U _pam_start_timer
    0000000000000010 T _pam_start_timer
    0000000000001930 T _pam_start_handlers
             U _pam_start_timer
pam_start.o:
    0000000000000000 T pam_start
             U _pam_start_handlers
kai@KTEC32:~$ nm /usr/lib/x86_64-linux-gnu/libpam.a | grep pam_authenticate
    0000000000000000 T pam_authenticate

Can anyone see what's going wrong here?

Thanks for any hint.


Solution

  • You must put source and object files before library files:

    gcc -o checkpwd checkpwd.c -lpam -lpam_misc 
    

    see: Why does the order of '-l' option in gcc matter?