Blame libpam/pam_vprompt.c

Packit Service b29381
/*
Packit Service b29381
 * Redistribution and use in source and binary forms, with or without
Packit Service b29381
 * modification, are permitted provided that the following conditions
Packit Service b29381
 * are met:
Packit Service b29381
 * 1. Redistributions of source code must retain the above copyright
Packit Service b29381
 *    notice, and the entire permission notice in its entirety,
Packit Service b29381
 *    including the disclaimer of warranties.
Packit Service b29381
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service b29381
 *    notice, this list of conditions and the following disclaimer in the
Packit Service b29381
 *    documentation and/or other materials provided with the distribution.
Packit Service b29381
 * 3. The name of the author may not be used to endorse or promote
Packit Service b29381
 *    products derived from this software without specific prior
Packit Service b29381
 *    written permission.
Packit Service b29381
 *
Packit Service b29381
 * ALTERNATIVELY, this product may be distributed under the terms of
Packit Service b29381
 * the GNU Public License, in which case the provisions of the GPL are
Packit Service b29381
 * required INSTEAD OF the above restrictions.  (This clause is
Packit Service b29381
 * necessary due to a potential bad interaction between the GPL and
Packit Service b29381
 * the restrictions contained in a BSD-style copyright.)
Packit Service b29381
 *
Packit Service b29381
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
Packit Service b29381
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service b29381
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit Service b29381
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
Packit Service b29381
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Packit Service b29381
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit Service b29381
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit Service b29381
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
Packit Service b29381
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit Service b29381
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit Service b29381
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service b29381
 */
Packit Service b29381
Packit Service b29381
#include "config.h"
Packit Service b29381
Packit Service b29381
#include <stdio.h>
Packit Service b29381
#include <stdlib.h>
Packit Service b29381
#include <unistd.h>
Packit Service b29381
#include <stdarg.h>
Packit Service b29381
#include <errno.h>
Packit Service b29381
Packit Service b29381
#include <security/pam_modules.h>
Packit Service b29381
#include <security/_pam_macros.h>
Packit Service b29381
#include <security/pam_ext.h>
Packit Service b29381
Packit Service b29381
#include "pam_private.h"
Packit Service b29381
Packit Service b29381
int
Packit Service b29381
pam_vprompt (pam_handle_t *pamh, int style, char **response,
Packit Service b29381
	     const char *fmt, va_list args)
Packit Service b29381
{
Packit Service b29381
  struct pam_message msg;
Packit Service b29381
  struct pam_response *pam_resp = NULL;
Packit Service b29381
  const struct pam_message *pmsg;
Packit Service b29381
  const struct pam_conv *conv;
Packit Service b29381
  const void *convp;
Packit Service b29381
  char *msgbuf;
Packit Service b29381
  int retval;
Packit Service b29381
Packit Service b29381
  if (response)
Packit Service b29381
    *response = NULL;
Packit Service b29381
Packit Service b29381
  retval = pam_get_item (pamh, PAM_CONV, &convp);
Packit Service b29381
  if (retval != PAM_SUCCESS)
Packit Service b29381
    return retval;
Packit Service b29381
  conv = convp;
Packit Service b29381
  if (conv == NULL || conv->conv == NULL)
Packit Service b29381
    {
Packit Service b29381
      pam_syslog (pamh, LOG_ERR, "no conversation function");
Packit Service b29381
      return PAM_SYSTEM_ERR;
Packit Service b29381
    }
Packit Service b29381
Packit Service b29381
  if (vasprintf (&msgbuf, fmt, args) < 0)
Packit Service b29381
    {
Packit Service b29381
      pam_syslog (pamh, LOG_ERR, "vasprintf: %m");
Packit Service b29381
      return PAM_BUF_ERR;
Packit Service b29381
    }
Packit Service b29381
Packit Service b29381
  msg.msg_style = style;
Packit Service b29381
  msg.msg = msgbuf;
Packit Service b29381
  pmsg = &msg;
Packit Service b29381
Packit Service b29381
  retval = conv->conv (1, &pmsg, &pam_resp, conv->appdata_ptr);
Packit Service b29381
  if (retval != PAM_SUCCESS && pam_resp != NULL)
Packit Service b29381
    pam_syslog(pamh, LOG_WARNING,
Packit Service b29381
      "unexpected response from failed conversation function");
Packit Service b29381
  if (response)
Packit Service b29381
    *response = pam_resp == NULL ? NULL : pam_resp->resp;
Packit Service b29381
  else if (pam_resp && pam_resp->resp)
Packit Service b29381
    {
Packit Service b29381
      _pam_overwrite (pam_resp->resp);
Packit Service b29381
      _pam_drop (pam_resp->resp);
Packit Service b29381
    }
Packit Service b29381
  _pam_overwrite (msgbuf);
Packit Service b29381
  _pam_drop (pam_resp);
Packit Service b29381
  free (msgbuf);
Packit Service b29381
  if (retval != PAM_SUCCESS)
Packit Service b29381
    pam_syslog (pamh, LOG_ERR, "conversation failed");
Packit Service b29381
Packit Service b29381
  return retval;
Packit Service b29381
}
Packit Service b29381
Packit Service b29381
int
Packit Service b29381
pam_prompt (pam_handle_t *pamh, int style, char **response,
Packit Service b29381
	    const char *fmt, ...)
Packit Service b29381
{
Packit Service b29381
  va_list args;
Packit Service b29381
  int retval;
Packit Service b29381
Packit Service b29381
  va_start (args, fmt);
Packit Service b29381
  retval = pam_vprompt (pamh, style, response, fmt, args);
Packit Service b29381
  va_end (args);
Packit Service b29381
Packit Service b29381
  return retval;
Packit Service b29381
}