Testing PAM service authentication
#include <stdio.h>
#include <security/pam_appl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
struct pam_response *reply;
// //function used to get user input
int function_conversation(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
{
*resp = reply;
return PAM_SUCCESS;
}
int authenticate_system(const char *username, const char *password, const char *service_name)
{
const struct pam_conv local_conversation = { function_conversation, NULL };
*local_auth_handle = NULL; // this gets set by pam_start
pam_handle_t
int retval;
= pam_start(service_name , username, &local_conversation, &local_auth_handle);
retval
if (retval != PAM_SUCCESS)
{
("pam_start returned: %d\n ", retval);
printfreturn 0;
}
= (struct pam_response *)malloc(sizeof(struct pam_response));
reply
[0].resp = strdup(password);
reply[0].resp_retcode = 0;
reply= pam_authenticate(local_auth_handle, 0);
retval
if (retval != PAM_SUCCESS)
{
if (retval == PAM_AUTH_ERR)
{
("Authentication failure.\n");
printf}
else
{
("pam_authenticate returned %d\n", retval);
printf}
return 0;
}
("Authenticated.\n");
printf= pam_end(local_auth_handle, retval);
retval
if (retval != PAM_SUCCESS)
{
("pam_end returned\n");
printfreturn 0;
}
return 1;
}
int main(int argc, char** argv)
{
char* login;
char* password;
char* service_name;
("Authentication module\n");
printf
if (argc != 4)
{
("Invalid count of arguments %d.\n", argc);
printf("./authModule <username> <password> <service_name>");
printfreturn 1;
}
= argv[1];
login = argv[2];
password = argv[3];
service_name
if (authenticate_system(login, password,service_name) == 1)
{
("Authenticate with %s - %s through system\n", login, password);
printfreturn 0;
}
("Authentication failed!\n");
printfreturn 1;
}
Now it’s time to compile it:
[root@archpepex ~]# gcc -o authModule authModule.c -lpam
[root@archpepex ~]# ./authModule user pass vsftpd