/*
 * $Id$
 *
 * A conversation function for checkpam
 *
 * Hacked about from misc_conv by Tim Baverstock <tim@sable.demon.co.uk>
 * misc_conv written by Andrew Morgan <morgan@physics.ucla.edu>
 *
 * Licensed under the GPL. <www.gnu.org>
 *
 * $Log$
 *
 */

#include <stdio.h>
#include <stdlib.h>

#include <security/pam_appl.h>
#include <security/pam_misc.h>

#include "checkpam.h"

#define INPUTSIZE PAM_MAX_MSG_SIZE

#define CONV_ECHO_ON  1
#define CONV_ECHO_OFF 0

#define REPLY_CHUNK 5

static void drop_reply(struct pam_response *reply, int replies)
{
     int i;

     for (i=0; i<replies; ++i) {
	  _pam_overwrite(reply[i].resp);      /* might be a password */
	  free(reply[i].resp);
     }
     if (reply)
	  free(reply);
}

/* Just gets huffy (PAM_CONV_ERR) if it's asked to do anything at all */

extern int checkpam_conv (int num_msg, const struct pam_message **msgm,
		     struct pam_response **response, void *appdata_ptr)
{
  int count=0,replies=0;
  struct pam_response *reply=NULL;
  
  D(("entering conversation function."));
  
  for (count=0; count < num_msg; ++count) {
	switch (msgm[count]->msg_style) {
	case PAM_PROMPT_ECHO_OFF:
	case PAM_PROMPT_ECHO_ON:
	case PAM_ERROR_MSG:
	case PAM_TEXT_INFO:
	default:
	  fprintf(stderr, "erroneous conversation (%d)\n"
			  ,msgm[count]->msg_style);
	  drop_reply(reply,replies);
	  return (PAM_CONV_ERR);
	}
  }
  return PAM_SUCCESS;  /* They didn't talk to us after all! :) */
}
