1*5ca12230Schristos /*-
2*5ca12230Schristos * Copyright (c) 2015-2017 Dag-Erling Smørgrav
3*5ca12230Schristos * All rights reserved.
4*5ca12230Schristos *
5*5ca12230Schristos * Redistribution and use in source and binary forms, with or without
6*5ca12230Schristos * modification, are permitted provided that the following conditions
7*5ca12230Schristos * are met:
8*5ca12230Schristos * 1. Redistributions of source code must retain the above copyright
9*5ca12230Schristos * notice, this list of conditions and the following disclaimer.
10*5ca12230Schristos * 2. Redistributions in binary form must reproduce the above copyright
11*5ca12230Schristos * notice, this list of conditions and the following disclaimer in the
12*5ca12230Schristos * documentation and/or other materials provided with the distribution.
13*5ca12230Schristos * 3. The name of the author may not be used to endorse or promote
14*5ca12230Schristos * products derived from this software without specific prior written
15*5ca12230Schristos * permission.
16*5ca12230Schristos *
17*5ca12230Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*5ca12230Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*5ca12230Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*5ca12230Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*5ca12230Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*5ca12230Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*5ca12230Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*5ca12230Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*5ca12230Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*5ca12230Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*5ca12230Schristos * SUCH DAMAGE.
28*5ca12230Schristos */
29*5ca12230Schristos
30*5ca12230Schristos #ifdef HAVE_CONFIG_H
31*5ca12230Schristos # include "config.h"
32*5ca12230Schristos #endif
33*5ca12230Schristos
34*5ca12230Schristos #include <err.h>
35*5ca12230Schristos #include <errno.h>
36*5ca12230Schristos #include <stdint.h>
37*5ca12230Schristos #include <stdio.h>
38*5ca12230Schristos #include <stdlib.h>
39*5ca12230Schristos #include <string.h>
40*5ca12230Schristos
41*5ca12230Schristos #include <cryb/test.h>
42*5ca12230Schristos
43*5ca12230Schristos #include <security/pam_appl.h>
44*5ca12230Schristos #include <security/openpam.h>
45*5ca12230Schristos
46*5ca12230Schristos #include "openpam_impl.h"
47*5ca12230Schristos #include "openpam_asprintf.h"
48*5ca12230Schristos
49*5ca12230Schristos #include "t_pam_conv.h"
50*5ca12230Schristos
51*5ca12230Schristos /*
52*5ca12230Schristos * Conversation function
53*5ca12230Schristos *
54*5ca12230Schristos * The appdata argument points to a struct t_pam_conv_script which
55*5ca12230Schristos * contains both the expected messages and the desired responses. If
56*5ca12230Schristos * script.responses is NULL, t_pam_conv() will return PAM_CONV_ERR. If an
57*5ca12230Schristos * error occurs (incorrect number of messages, messages don't match script
58*5ca12230Schristos * etc.), script.comment will be set to point to a malloc()ed string
59*5ca12230Schristos * describing the error. Otherwise, t_pam_conv() will return to its
60*5ca12230Schristos * caller a malloc()ed copy of script.responses.
61*5ca12230Schristos */
62*5ca12230Schristos int
t_pam_conv(int nm,const struct pam_message ** msgs,struct pam_response ** respsp,void * ad)63*5ca12230Schristos t_pam_conv(int nm, const struct pam_message **msgs,
64*5ca12230Schristos struct pam_response **respsp, void *ad)
65*5ca12230Schristos {
66*5ca12230Schristos struct t_pam_conv_script *s = ad;
67*5ca12230Schristos struct pam_response *resps;
68*5ca12230Schristos int i;
69*5ca12230Schristos
70*5ca12230Schristos /* check message count */
71*5ca12230Schristos if (nm != s->nmsg) {
72*5ca12230Schristos asprintf(&s->comment, "expected %d messages, got %d",
73*5ca12230Schristos s->nmsg, nm);
74*5ca12230Schristos return (PAM_CONV_ERR);
75*5ca12230Schristos }
76*5ca12230Schristos if (nm <= 0 || nm > PAM_MAX_NUM_MSG) {
77*5ca12230Schristos /* since the previous test passed, this is intentional! */
78*5ca12230Schristos s->comment = NULL;
79*5ca12230Schristos return (PAM_CONV_ERR);
80*5ca12230Schristos }
81*5ca12230Schristos
82*5ca12230Schristos /* check each message and provide the sed answer */
83*5ca12230Schristos if ((resps = calloc(nm, sizeof *resps)) == NULL)
84*5ca12230Schristos goto enomem;
85*5ca12230Schristos for (i = 0; i < nm; ++i) {
86*5ca12230Schristos if (msgs[i]->msg_style != s->msgs[i].msg_style) {
87*5ca12230Schristos asprintf(&s->comment,
88*5ca12230Schristos "message %d expected style %d got %d", i,
89*5ca12230Schristos s->msgs[i].msg_style, msgs[i]->msg_style);
90*5ca12230Schristos goto fail;
91*5ca12230Schristos }
92*5ca12230Schristos if (strcmp(msgs[i]->msg, s->msgs[i].msg) != 0) {
93*5ca12230Schristos asprintf(&s->comment,
94*5ca12230Schristos "message %d expected \"%s\" got \"%s\"", i,
95*5ca12230Schristos s->msgs[i].msg, msgs[i]->msg);
96*5ca12230Schristos goto fail;
97*5ca12230Schristos }
98*5ca12230Schristos switch (msgs[i]->msg_style) {
99*5ca12230Schristos case PAM_PROMPT_ECHO_OFF:
100*5ca12230Schristos t_printv("[PAM_PROMPT_ECHO_OFF] %s\n", msgs[i]->msg);
101*5ca12230Schristos break;
102*5ca12230Schristos case PAM_PROMPT_ECHO_ON:
103*5ca12230Schristos t_printv("[PAM_PROMPT_ECHO_ON] %s\n", msgs[i]->msg);
104*5ca12230Schristos break;
105*5ca12230Schristos case PAM_ERROR_MSG:
106*5ca12230Schristos t_printv("[PAM_ERROR_MSG] %s\n", msgs[i]->msg);
107*5ca12230Schristos break;
108*5ca12230Schristos case PAM_TEXT_INFO:
109*5ca12230Schristos t_printv("[PAM_TEXT_INFO] %s\n", msgs[i]->msg);
110*5ca12230Schristos break;
111*5ca12230Schristos default:
112*5ca12230Schristos asprintf(&s->comment, "invalid message style %d",
113*5ca12230Schristos msgs[i]->msg_style);
114*5ca12230Schristos goto fail;
115*5ca12230Schristos }
116*5ca12230Schristos /* copy the response, if there is one */
117*5ca12230Schristos if (s->resps[i].resp != NULL &&
118*5ca12230Schristos (resps[i].resp = strdup(s->resps[i].resp)) == NULL)
119*5ca12230Schristos goto enomem;
120*5ca12230Schristos resps[i].resp_retcode = s->resps[i].resp_retcode;
121*5ca12230Schristos }
122*5ca12230Schristos s->comment = NULL;
123*5ca12230Schristos *respsp = resps;
124*5ca12230Schristos return (PAM_SUCCESS);
125*5ca12230Schristos enomem:
126*5ca12230Schristos asprintf(&s->comment, "%s", strerror(ENOMEM));
127*5ca12230Schristos fail:
128*5ca12230Schristos for (i = 0; i < nm; ++i)
129*5ca12230Schristos free(resps[i].resp);
130*5ca12230Schristos free(resps);
131*5ca12230Schristos return (PAM_CONV_ERR);
132*5ca12230Schristos }
133