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