2 * Copyright (c) 2015-2017 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $OpenPAM: t_pam_conv.c 938 2017-04-30 21:34:42Z des $
43 #include <cryb/test.h>
45 #include <security/pam_appl.h>
46 #include <security/openpam.h>
48 #include "openpam_impl.h"
49 #include "openpam_asprintf.h"
51 #include "t_pam_conv.h"
54 * Conversation function
56 * The appdata argument points to a struct t_pam_conv_script which
57 * contains both the expected messages and the desired responses. If
58 * script.responses is NULL, t_pam_conv() will return PAM_CONV_ERR. If an
59 * error occurs (incorrect number of messages, messages don't match script
60 * etc.), script.comment will be set to point to a malloc()ed string
61 * describing the error. Otherwise, t_pam_conv() will return to its
62 * caller a malloc()ed copy of script.responses.
65 t_pam_conv(int nm, const struct pam_message **msgs,
66 struct pam_response **respsp, void *ad)
68 struct t_pam_conv_script *s = ad;
69 struct pam_response *resps;
72 /* check message count */
74 asprintf(&s->comment, "expected %d messages, got %d",
76 return (PAM_CONV_ERR);
78 if (nm <= 0 || nm > PAM_MAX_NUM_MSG) {
79 /* since the previous test passed, this is intentional! */
81 return (PAM_CONV_ERR);
84 /* check each message and provide the sed answer */
85 if ((resps = calloc(nm, sizeof *resps)) == NULL)
87 for (i = 0; i < nm; ++i) {
88 if (msgs[i]->msg_style != s->msgs[i].msg_style) {
90 "message %d expected style %d got %d", i,
91 s->msgs[i].msg_style, msgs[i]->msg_style);
94 if (strcmp(msgs[i]->msg, s->msgs[i].msg) != 0) {
96 "message %d expected \"%s\" got \"%s\"", i,
97 s->msgs[i].msg, msgs[i]->msg);
100 switch (msgs[i]->msg_style) {
101 case PAM_PROMPT_ECHO_OFF:
102 t_printv("[PAM_PROMPT_ECHO_OFF] %s\n", msgs[i]->msg);
104 case PAM_PROMPT_ECHO_ON:
105 t_printv("[PAM_PROMPT_ECHO_ON] %s\n", msgs[i]->msg);
108 t_printv("[PAM_ERROR_MSG] %s\n", msgs[i]->msg);
111 t_printv("[PAM_TEXT_INFO] %s\n", msgs[i]->msg);
114 asprintf(&s->comment, "invalid message style %d",
118 /* copy the response, if there is one */
119 if (s->resps[i].resp != NULL &&
120 (resps[i].resp = strdup(s->resps[i].resp)) == NULL)
122 resps[i].resp_retcode = s->resps[i].resp_retcode;
126 return (PAM_SUCCESS);
128 asprintf(&s->comment, "%s", strerror(ENOMEM));
130 for (i = 0; i < nm; ++i)
133 return (PAM_CONV_ERR);