1/*-
2 * Copyright (c) 2015-2017 Dag-Erling Sm��rgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
15 *    permission.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30#ifdef HAVE_CONFIG_H
31# include "config.h"
32#endif
33
34#include <err.h>
35#include <errno.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40
41#include <cryb/test.h>
42
43#include <security/pam_appl.h>
44#include <security/openpam.h>
45
46#include "openpam_impl.h"
47#include "openpam_asprintf.h"
48
49#include "t_pam_conv.h"
50
51/*
52 * Conversation function
53 *
54 * The appdata argument points to a struct t_pam_conv_script which
55 * contains both the expected messages and the desired responses.  If
56 * script.responses is NULL, t_pam_conv() will return PAM_CONV_ERR.  If an
57 * error occurs (incorrect number of messages, messages don't match script
58 * etc.), script.comment will be set to point to a malloc()ed string
59 * describing the error.  Otherwise, t_pam_conv() will return to its
60 * caller a malloc()ed copy of script.responses.
61 */
62int
63t_pam_conv(int nm, const struct pam_message **msgs,
64    struct pam_response **respsp, void *ad)
65{
66	struct t_pam_conv_script *s = ad;
67	struct pam_response *resps;
68	int i;
69
70	/* check message count */
71	if (nm != s->nmsg) {
72		asprintf(&s->comment, "expected %d messages, got %d",
73		    s->nmsg, nm);
74		return (PAM_CONV_ERR);
75	}
76	if (nm <= 0 || nm > PAM_MAX_NUM_MSG) {
77		/* since the previous test passed, this is intentional! */
78		s->comment = NULL;
79		return (PAM_CONV_ERR);
80	}
81
82	/* check each message and provide the sed answer */
83	if ((resps = calloc(nm, sizeof *resps)) == NULL)
84		goto enomem;
85	for (i = 0; i < nm; ++i) {
86		if (msgs[i]->msg_style != s->msgs[i].msg_style) {
87			asprintf(&s->comment,
88			    "message %d expected style %d got %d", i,
89			    s->msgs[i].msg_style, msgs[i]->msg_style);
90			goto fail;
91		}
92		if (strcmp(msgs[i]->msg, s->msgs[i].msg) != 0) {
93			asprintf(&s->comment,
94			    "message %d expected \"%s\" got \"%s\"", i,
95			    s->msgs[i].msg, msgs[i]->msg);
96			goto fail;
97		}
98		switch (msgs[i]->msg_style) {
99		case PAM_PROMPT_ECHO_OFF:
100			t_printv("[PAM_PROMPT_ECHO_OFF] %s\n", msgs[i]->msg);
101			break;
102		case PAM_PROMPT_ECHO_ON:
103			t_printv("[PAM_PROMPT_ECHO_ON] %s\n", msgs[i]->msg);
104			break;
105		case PAM_ERROR_MSG:
106			t_printv("[PAM_ERROR_MSG] %s\n", msgs[i]->msg);
107			break;
108		case PAM_TEXT_INFO:
109			t_printv("[PAM_TEXT_INFO] %s\n", msgs[i]->msg);
110			break;
111		default:
112			asprintf(&s->comment, "invalid message style %d",
113			    msgs[i]->msg_style);
114			goto fail;
115		}
116		/* copy the response, if there is one */
117		if (s->resps[i].resp != NULL &&
118		    (resps[i].resp = strdup(s->resps[i].resp)) == NULL)
119			goto enomem;
120		resps[i].resp_retcode = s->resps[i].resp_retcode;
121	}
122	s->comment = NULL;
123	*respsp = resps;
124	return (PAM_SUCCESS);
125enomem:
126	asprintf(&s->comment, "%s", strerror(ENOMEM));
127fail:
128	for (i = 0; i < nm; ++i)
129		free(resps[i].resp);
130	free(resps);
131	return (PAM_CONV_ERR);
132}
133