Listing 1. A Basic Conversation Function
int su_conv(int num_msg,const struct pam_message **msgm,
struct pam_response **resp,void *appdata)
{
int count;
struct pam_response *r;
char *recvpass=(char *)malloc(20*sizeof(char));
*(recvpass+19) = '\0';
r = (struct pam_response*)calloc(num_msg,sizeof(struct pam_response));
for(count=0;count < num_msg;++count)
{
switch(msgm[count]->msg_style)
{
case PAM_PROMPT_ECHO_OFF:
printf("%s",msgm[count]->msg);
getPassword(recvpass);
break;
case PAM_PROMPT_ECHO_ON:
printf("%s",msgm[count]->msg);
scanf("%s",recvpass);
break;
case PAM_ERROR_MSG:
printf(" %s\n",msgm[count]->msg);
break;
case PAM_TEXT_INFO:
printf(" %s\n",msgm[count]->msg);
break;
default:
printf("Erroneous Conversation (%d)\n"
,msgm[count]->msg_style);
}
r[count].resp_retcode = 0;
r[count].resp = recvpass;
}
*resp = r;
return PAM_SUCCESS;
}
void getPassword(char *revcbuf)
{
int i=0;
char buf[20];
while((i = getch()) != '\n')
buf[i++] = i;
buf[i] = '\0';
strcpy(recvbuf,buf);
}
Copyright © 1994 - 2018 Linux Journal. All rights reserved.