Having pain with PAM programming
Hello,
I'm trying to authenticate a user, such as I have the username and password. I go through the calls but I'm always receiving error 7 PAM_AUTH_ERROR. If I use a fake username and password it returns 10 (PAM_USER_UNKOWN) what is correct. So, I know it's working but I guess I'm missing something else.
Long story short: I need to be able to authenticate against PAM (it's very flexable).
To compile you need pam_devel*.rpm an you need to link with libpam (-lpam).
Your main should have
CAuthenticate a;
a.Authenticate("test","test");
the C++ header and cpp follows
Thanks for the help,
Eddy
Header:
#ifndef CAUTHENTICATE_H
#define CAUTHENTICATE_H
#include <cstdlib>
#include <security/pam_appl.h>
#include <string.h>
/**
@author Eddy Hahn
*/
class CAuthenticate{
private:
pam_handle_t* m_pPAM;
public:
CAuthenticate();
~CAuthenticate();
bool Authenticate(char* UserName, char* Password);
};
#endif
CPP:
#include "cauthenticate.h"
CAuthenticate::CAuthenticate()
{
m_pPAM = NULL;
}
CAuthenticate::~CAuthenticate()
{
}
int Conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
{
if((*resp = (pam_response*) calloc( num_msg, sizeof **resp)) == NULL)
return PAM_SUCCESS;
for(int i=0; i< num_msg; i++)
switch(msg[i]->msg_style)
{
case PAM_PROMPT_ECHO_OFF:
resp[i]->resp_retcode=0;
resp[i]->resp = (char*) malloc( strlen( (char*) appdata_ptr));
strcpy(resp[i]->resp, (char*) appdata_ptr);
break;
case PAM_PROMPT_ECHO_ON:
break;
case PAM_ERROR_MSG:
break;
case PAM_TEXT_INFO:
break;
default:
break;
}
return PAM_SUCCESS;
}
bool CAuthenticate::Authenticate(char* UserName, char* Password)
{
bool RetVal = false;
int ret;
pam_handle_t* ph;
pam_conv conv;
conv.conv = &Conversation;
conv.appdata_ptr = Password;
if( pam_start( "auth", UserName, &conv, &m_pPAM) != PAM_SUCCESS)
return false;
ret = pam_authenticate( m_pPAM, PAM_SILENT);
if( ret == PAM_SUCCESS)
RetVal = true;
pam_end( m_pPAM, PAM_SUCCESS);
return RetVal;
}
|