Blame compat/pam_prompt.c

Packit 6bd9ab
/*
Packit 6bd9ab
   pam_prompt.c - replacement function for pam_prompt()
Packit 6bd9ab
Packit 6bd9ab
   Copyright (C) 2010, 2012 Arthur de Jong
Packit 6bd9ab
Packit 6bd9ab
   This library is free software; you can redistribute it and/or
Packit 6bd9ab
   modify it under the terms of the GNU Lesser General Public
Packit 6bd9ab
   License as published by the Free Software Foundation; either
Packit 6bd9ab
   version 2.1 of the License, or (at your option) any later version.
Packit 6bd9ab
Packit 6bd9ab
   This library is distributed in the hope that it will be useful,
Packit 6bd9ab
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6bd9ab
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6bd9ab
   Lesser General Public License for more details.
Packit 6bd9ab
Packit 6bd9ab
   You should have received a copy of the GNU Lesser General Public
Packit 6bd9ab
   License along with this library; if not, write to the Free Software
Packit 6bd9ab
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6bd9ab
   02110-1301 USA
Packit 6bd9ab
*/
Packit 6bd9ab
Packit 6bd9ab
#include "config.h"
Packit 6bd9ab
Packit 6bd9ab
#include <stdlib.h>
Packit 6bd9ab
#include <stdio.h>
Packit 6bd9ab
#include <stdarg.h>
Packit 6bd9ab
Packit 6bd9ab
#include "compat/attrs.h"
Packit 6bd9ab
#include "compat/pam_compat.h"
Packit 6bd9ab
Packit 6bd9ab
int pam_prompt(pam_handle_t *pamh, int style, char **response,
Packit 6bd9ab
               const char *format, ...)
Packit 6bd9ab
{
Packit 6bd9ab
  int rc;
Packit 6bd9ab
  struct pam_conv *aconv;
Packit 6bd9ab
  char buffer[200];
Packit 6bd9ab
  va_list ap;
Packit 6bd9ab
  struct pam_message msg, *pmsg;
Packit 6bd9ab
  struct pam_response *resp;
Packit 6bd9ab
  /* the the conversion function */
Packit 6bd9ab
  rc = pam_get_item(pamh, PAM_CONV, (PAM_ITEM_CONST void **)&aconv);
Packit 6bd9ab
  if (rc != PAM_SUCCESS)
Packit 6bd9ab
    return rc;
Packit 6bd9ab
  /* make the message string */
Packit 6bd9ab
  va_start(ap, format);
Packit 6bd9ab
  vsnprintf(buffer, sizeof(buffer), format, ap);
Packit 6bd9ab
  buffer[sizeof(buffer) - 1] = '\0';
Packit 6bd9ab
  va_end(ap);
Packit 6bd9ab
  /* build the message */
Packit 6bd9ab
  msg.msg_style = style;
Packit 6bd9ab
  msg.msg = buffer;
Packit 6bd9ab
  pmsg = &msg;
Packit 6bd9ab
  resp = NULL;
Packit 6bd9ab
  rc = aconv->conv(1, (const struct pam_message **)&pmsg, &resp, aconv->appdata_ptr);
Packit 6bd9ab
  if (rc != PAM_SUCCESS)
Packit 6bd9ab
    return rc;
Packit 6bd9ab
  /* assign response if it is set */
Packit 6bd9ab
  if (response != NULL)
Packit 6bd9ab
  {
Packit 6bd9ab
    if (resp == NULL)
Packit 6bd9ab
      return PAM_CONV_ERR;
Packit 6bd9ab
    if (resp[0].resp == NULL)
Packit 6bd9ab
    {
Packit 6bd9ab
      free(resp);
Packit 6bd9ab
      return PAM_CONV_ERR;
Packit 6bd9ab
    }
Packit 6bd9ab
    *response = resp[0].resp;
Packit 6bd9ab
  }
Packit 6bd9ab
  else
Packit 6bd9ab
    free(resp[0].resp);
Packit 6bd9ab
  free(resp);
Packit 6bd9ab
  return PAM_SUCCESS;
Packit 6bd9ab
}