xref: /freebsd/contrib/openpam/t/t_pam_conv.c (revision b00ab754)
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  * $OpenPAM: t_pam_conv.c 938 2017-04-30 21:34:42Z des $
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #include <err.h>
37 #include <errno.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <cryb/test.h>
44 
45 #include <security/pam_appl.h>
46 #include <security/openpam.h>
47 
48 #include "openpam_impl.h"
49 #include "openpam_asprintf.h"
50 
51 #include "t_pam_conv.h"
52 
53 /*
54  * Conversation function
55  *
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.
63  */
64 int
65 t_pam_conv(int nm, const struct pam_message **msgs,
66     struct pam_response **respsp, void *ad)
67 {
68 	struct t_pam_conv_script *s = ad;
69 	struct pam_response *resps;
70 	int i;
71 
72 	/* check message count */
73 	if (nm != s->nmsg) {
74 		asprintf(&s->comment, "expected %d messages, got %d",
75 		    s->nmsg, nm);
76 		return (PAM_CONV_ERR);
77 	}
78 	if (nm <= 0 || nm > PAM_MAX_NUM_MSG) {
79 		/* since the previous test passed, this is intentional! */
80 		s->comment = NULL;
81 		return (PAM_CONV_ERR);
82 	}
83 
84 	/* check each message and provide the sed answer */
85 	if ((resps = calloc(nm, sizeof *resps)) == NULL)
86 		goto enomem;
87 	for (i = 0; i < nm; ++i) {
88 		if (msgs[i]->msg_style != s->msgs[i].msg_style) {
89 			asprintf(&s->comment,
90 			    "message %d expected style %d got %d", i,
91 			    s->msgs[i].msg_style, msgs[i]->msg_style);
92 			goto fail;
93 		}
94 		if (strcmp(msgs[i]->msg, s->msgs[i].msg) != 0) {
95 			asprintf(&s->comment,
96 			    "message %d expected \"%s\" got \"%s\"", i,
97 			    s->msgs[i].msg, msgs[i]->msg);
98 			goto fail;
99 		}
100 		switch (msgs[i]->msg_style) {
101 		case PAM_PROMPT_ECHO_OFF:
102 			t_printv("[PAM_PROMPT_ECHO_OFF] %s\n", msgs[i]->msg);
103 			break;
104 		case PAM_PROMPT_ECHO_ON:
105 			t_printv("[PAM_PROMPT_ECHO_ON] %s\n", msgs[i]->msg);
106 			break;
107 		case PAM_ERROR_MSG:
108 			t_printv("[PAM_ERROR_MSG] %s\n", msgs[i]->msg);
109 			break;
110 		case PAM_TEXT_INFO:
111 			t_printv("[PAM_TEXT_INFO] %s\n", msgs[i]->msg);
112 			break;
113 		default:
114 			asprintf(&s->comment, "invalid message style %d",
115 			    msgs[i]->msg_style);
116 			goto fail;
117 		}
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)
121 			goto enomem;
122 		resps[i].resp_retcode = s->resps[i].resp_retcode;
123 	}
124 	s->comment = NULL;
125 	*respsp = resps;
126 	return (PAM_SUCCESS);
127 enomem:
128 	asprintf(&s->comment, "%s", strerror(ENOMEM));
129 fail:
130 	for (i = 0; i < nm; ++i)
131 		free(resps[i].resp);
132 	free(resps);
133 	return (PAM_CONV_ERR);
134 }
135