Path: blob/main/documentation/static/source/articles/pam/converse.c
18096 views
/*-1* Copyright (c) 2002 Networks Associates Technology, Inc.2* All rights reserved.3*4* This software was developed for the FreeBSD Project by ThinkSec AS and5* Network Associates Laboratories, the Security Research Division of6* Network Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-80357* ("CBOSS"), as part of the DARPA CHATS research program.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. The name of the author may not be used to endorse or promote18* products derived from this software without specific prior written19* permission.20*21* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*33* $FreeBSD: head/en_US.ISO8859-1/articles/pam/converse.c 38826 2012-05-17 19:12:14Z hrs $34*/3536#include <stdio.h>37#include <stdlib.h>38#include <string.h>39#include <unistd.h>4041#include <security/pam_appl.h>4243int44converse(int n, const struct pam_message **msg,45struct pam_response **resp, void *data)46{47struct pam_response *aresp;48char buf[PAM_MAX_RESP_SIZE];49int i;5051data = data;52if (n <= 0 || n > PAM_MAX_NUM_MSG)53return (PAM_CONV_ERR);54if ((aresp = calloc(n, sizeof *aresp)) == NULL)55return (PAM_BUF_ERR);56for (i = 0; i < n; ++i) {57aresp[i].resp_retcode = 0;58aresp[i].resp = NULL;59switch (msg[i]->msg_style) {60case PAM_PROMPT_ECHO_OFF:61aresp[i].resp = strdup(getpass(msg[i]->msg));62if (aresp[i].resp == NULL)63goto fail;64break;65case PAM_PROMPT_ECHO_ON:66fputs(msg[i]->msg, stderr);67if (fgets(buf, sizeof buf, stdin) == NULL)68goto fail;69aresp[i].resp = strdup(buf);70if (aresp[i].resp == NULL)71goto fail;72break;73case PAM_ERROR_MSG:74fputs(msg[i]->msg, stderr);75if (strlen(msg[i]->msg) > 0 &&76msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n')77fputc('\n', stderr);78break;79case PAM_TEXT_INFO:80fputs(msg[i]->msg, stdout);81if (strlen(msg[i]->msg) > 0 &&82msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n')83fputc('\n', stdout);84break;85default:86goto fail;87}88}89*resp = aresp;90return (PAM_SUCCESS);91fail:92for (i = 0; i < n; ++i) {93if (aresp[i].resp != NULL) {94memset(aresp[i].resp, 0, strlen(aresp[i].resp));95free(aresp[i].resp);96}97}98memset(aresp, 0, n * sizeof *aresp);99*resp = NULL;100return (PAM_CONV_ERR);101}102103104