aboutsummaryrefslogtreecommitdiff
path: root/src/libpamtest.c
blob: 74da180d258a896c52efdfd200ce0efd6eeee2d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (c) 2015 Andreas Schneider <asn@samba.org>
 * Copyright (c) 2015 Jakub Hrozek <jakub.hrozek@posteo.se>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "libpamtest.h"

#define MIN(a,b) ((a) < (b) ? (a) : (b))

static enum pamtest_err run_test_case(pam_handle_t *ph,
				      struct pam_testcase *tc)
{
	switch (tc->pam_operation) {
	case PAMTEST_AUTHENTICATE:
		tc->op_rv = pam_authenticate(ph, tc->flags);
		return PAMTEST_ERR_OK;
	case PAMTEST_SETCRED:
		tc->op_rv = pam_setcred(ph, tc->flags);
		return PAMTEST_ERR_OK;
	case PAMTEST_ACCOUNT:
		tc->op_rv = pam_acct_mgmt(ph, tc->flags);
		return PAMTEST_ERR_OK;
	case PAMTEST_OPEN_SESSION:
		tc->op_rv = pam_open_session(ph, tc->flags);
		return PAMTEST_ERR_OK;
	case PAMTEST_CLOSE_SESSION:
		tc->op_rv = pam_close_session(ph, tc->flags);
		return PAMTEST_ERR_OK;
	case PAMTEST_CHAUTHTOK:
		tc->op_rv = pam_chauthtok(ph, tc->flags);
		return PAMTEST_ERR_OK;
	case PAMTEST_GETENVLIST:
		tc->case_out.envlist = pam_getenvlist(ph);
		return PAMTEST_ERR_OK;
	case PAMTEST_KEEPHANDLE:
		tc->case_out.ph = ph;
		return PAMTEST_ERR_KEEPHANDLE;
	default:
		return PAMTEST_ERR_OP;
	}

	return PAMTEST_ERR_OP;
}

enum pamtest_err _pamtest_conv(const char *service,
			       const char *user,
			       pam_conv_fn conv_fn,
			       void *conv_userdata,
			       struct pam_testcase test_cases[],
			       size_t num_test_cases)
{
	int rv;
	pam_handle_t *ph;
	struct pam_conv conv;
	size_t tcindex;
	struct pam_testcase *tc = NULL;
	bool call_pam_end = true;

	conv.conv = conv_fn;
	conv.appdata_ptr = conv_userdata;

	if (test_cases == NULL) {
		return PAMTEST_ERR_INTERNAL;
	}

	rv = pam_start(service, user, &conv, &ph);
	if (rv != PAM_SUCCESS) {
		return PAMTEST_ERR_START;
	}

	for (tcindex = 0; tcindex < num_test_cases; tcindex++) {
		tc = &test_cases[tcindex];

		rv = run_test_case(ph, tc);
		if (rv == PAMTEST_ERR_KEEPHANDLE) {
			call_pam_end = false;
			continue;
		} else if (rv != PAMTEST_ERR_OK) {
			return PAMTEST_ERR_INTERNAL;
		}

		if (tc->op_rv != tc->expected_rv) {
			break;
		}
	}

	if (call_pam_end == true && tc != NULL) {
		rv = pam_end(ph, tc->op_rv);
		if (rv != PAM_SUCCESS) {
			return PAMTEST_ERR_END;
		}
	}

	if (tcindex < num_test_cases) {
		return PAMTEST_ERR_CASE;
	}

	return PAMTEST_ERR_OK;
}

void pamtest_free_env(char **envlist)
{
	size_t i;

	if (envlist == NULL) {
		return;
	}

	for (i = 0; envlist[i] != NULL; i++) {
		free(envlist[i]);
	}
	free(envlist);
}

const struct pam_testcase *
_pamtest_failed_case(struct pam_testcase *test_cases,
		     size_t num_test_cases)
{
	size_t tcindex;

	for (tcindex = 0; tcindex < num_test_cases; tcindex++) {
		const struct pam_testcase *tc = &test_cases[tcindex];

		if (tc->expected_rv != tc->op_rv) {
			return tc;
		}
	}

	/* Nothing failed */
	return NULL;
}

const char *pamtest_strerror(enum pamtest_err perr)
{
	switch (perr) {
	case PAMTEST_ERR_OK:
		return "Success";
	case PAMTEST_ERR_START:
		return "pam_start failed()";
	case PAMTEST_ERR_CASE:
		return "Unexpected testcase result";
	case PAMTEST_ERR_OP:
		return "Could not run a test case";
	case PAMTEST_ERR_END:
		return "pam_end failed()";
	case PAMTEST_ERR_KEEPHANDLE:
		/* Fallthrough */
	case PAMTEST_ERR_INTERNAL:
		return "Internal libpamtest error";
	}

	return "Unknown";
}

struct pamtest_conv_ctx {
	struct pamtest_conv_data *data;

	size_t echo_off_idx;
	size_t echo_on_idx;
	size_t err_idx;
	size_t info_idx;
};

static int add_to_reply(struct pam_response *reply, const char *str)
{
	size_t len;

	len = strlen(str) + 1;

	reply->resp = calloc(len, sizeof(char));
	if (reply->resp == NULL) {
		return PAM_BUF_ERR;
	}

	memcpy(reply->resp, str, len);
	return PAM_SUCCESS;
}

static void free_reply(struct pam_response *reply, int num_msg)
{
	int i;

	if (reply == NULL) {
		return;
	}

	for (i = 0; i < num_msg; i++) {
		free(reply[i].resp);
	}
	free(reply);
}

static int pamtest_simple_conv(int num_msg,
			       const struct pam_message **msgm,
			       struct pam_response **response,
			       void *appdata_ptr)
{
	int i = 0;
	int ret;
	struct pam_response *reply = NULL;
	const char *prompt;
	struct pamtest_conv_ctx *cctx = (struct pamtest_conv_ctx *)appdata_ptr;

	if (cctx == NULL) {
		return PAM_CONV_ERR;
	}

	if (response) {
		reply = (struct pam_response *) calloc(num_msg,
						sizeof(struct pam_response));
		if (reply == NULL) {
			return PAM_CONV_ERR;
		}
	}

	for (i=0; i < num_msg; i++) {
		switch (msgm[i]->msg_style) {
		case PAM_PROMPT_ECHO_OFF:
			prompt = (const char *) \
				   cctx->data->in_echo_off[cctx->echo_off_idx];

			if (reply != NULL) {
				if (prompt != NULL) {
					ret = add_to_reply(&reply[i], prompt);
					if (ret != PAM_SUCCESS) {
						free_reply(reply, num_msg);
						return ret;
					}
				}
			}

			cctx->echo_off_idx++;
			break;
		case PAM_PROMPT_ECHO_ON:
			prompt = (const char *) \
				   cctx->data->in_echo_on[cctx->echo_on_idx];
			if (prompt == NULL) {
				free_reply(reply, num_msg);
				return PAM_CONV_ERR;
			}

			if (reply != NULL) {
				if (prompt != NULL) {
					ret = add_to_reply(&reply[i], prompt);
					if (ret != PAM_SUCCESS) {
						free_reply(reply, num_msg);
						return ret;
					}
				}
			}

			cctx->echo_on_idx++;
			break;
		case PAM_ERROR_MSG:
			if (cctx->data->out_err != NULL) {
				memcpy(cctx->data->out_err[cctx->err_idx],
				       msgm[i]->msg,
				       MIN(strlen(msgm[i]->msg),
					   PAM_MAX_MSG_SIZE));
				cctx->err_idx++;
			}
			break;
		case PAM_TEXT_INFO:
			if (cctx->data->out_info != NULL) {
				memcpy(cctx->data->out_info[cctx->info_idx],
				       msgm[i]->msg,
				       MIN(strlen(msgm[i]->msg),
					   PAM_MAX_MSG_SIZE));
				cctx->info_idx++;
			}
			break;
		default:
			continue;
		}
	}

	if (response != NULL) {
		*response = reply;
	} else {
		free(reply);
	}

	return PAM_SUCCESS;
}

enum pamtest_err _pamtest(const char *service,
			  const char *user,
			  struct pamtest_conv_data *conv_data,
			  struct pam_testcase test_cases[],
			  size_t num_test_cases)
{
	struct pamtest_conv_ctx cctx = {
		.data = conv_data,
	};

	return _pamtest_conv(service, user,
			     pamtest_simple_conv,
			     &cctx,
			     test_cases,
			     num_test_cases);
}