aboutsummaryrefslogtreecommitdiff
path: root/tests/mock_pam.c
blob: 9111092627c4aec56e1515fe03ac09a6c6fa373c (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
/*
 * Copyright © 2012-2013 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>.
 * Copyright © 2012 Canonical Ltd. All rights reserved.
 *
 * Author(s): Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 *            David Barth <david.barth@canonical.com>
 *
 */

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

#include "mock_pam.h"
#include "pam-x2go.h"

struct pam_handle {
	void *item[PAM_NUM_ITEMS];

	struct pam_conv *conv;

	/* note: the other fields have been omitted */
};

int fake_conv (int num_msg, const struct pam_message **msg,
				struct pam_response **resp, void *appdata_ptr)
{
	struct pam_response *response = NULL;
	response = malloc (sizeof (struct pam_response));

	if (response == NULL)
		return PAM_BUF_ERR;

	response->resp_retcode = 0;

	if (strcmp((*msg)->msg, PAM_X2GO_PROMPT_GUESTLOGIN) == 0)
		response->resp = strdup ("guest"); /* IMPORTANT: this needs to be in /etc/passwd */
	else if (strcmp((*msg)->msg, PAM_X2GO_PROMPT_USER) == 0)
		response->resp = strdup ("ruser");
	else if (strcmp((*msg)->msg, PAM_X2GO_PROMPT_HOST) == 0)
		response->resp = strdup ("protocol://rhost/dummy");
	else if (strcmp((*msg)->msg, PAM_X2GO_PROMPT_PASSWORD) == 0)
		response->resp = strdup ("password");
	else if (strcmp((*msg)->msg, PAM_X2GO_PROMPT_COMMAND) == 0)
		response->resp = strdup ("rcommand");
	else
		return PAM_SYMBOL_ERR; /* leaks... */

	*resp = response;

	return PAM_SUCCESS;
}

struct pam_conv static_conv = { &fake_conv, (void *)NULL };

pam_handle_t *pam_handle_new (void)
{
	pam_handle_t *newh = malloc (sizeof (pam_handle_t));

	if (newh != NULL) {
		newh->conv = &static_conv;
		memset(newh->item, 0, sizeof(void *) * PAM_NUM_ITEMS);
	}

	return newh;
}

int PAM_NONNULL((1)) pam_get_item (const pam_handle_t *pamh, int type, const void **value)
{
	if (pamh == NULL)
		return PAM_SYSTEM_ERR;

	if (type == PAM_CONV)
		*value = pamh->conv;
	else  if (pamh->item[type] != NULL)
		*value = pamh->item[type];
	else
		*value = NULL; /* will result in a prompt conversation */

	return PAM_SUCCESS;
}

int PAM_NONNULL((1)) pam_set_item (pam_handle_t *pamh, int type, const void *value)
{
	if (pamh == NULL)
		return PAM_SYSTEM_ERR;

	void **slot, *tmp;
	size_t nsize, osize;

	slot = &pamh->item[type];
	osize = nsize = 0;

	if (*slot != NULL)
		osize = strlen((const char *)*slot) + 1;
	if (value != NULL)
		nsize = strlen((const char *)value) + 1;

	if (*slot != NULL) {
		memset(*slot, 0xd0, osize);
		free(*slot);
	}

	if (value != NULL) {
		if ((tmp = malloc(nsize)) == NULL)
			return PAM_BUF_ERR;
		memcpy(tmp, value, nsize);
	} else {
		tmp = NULL;
	}
	*slot = tmp;

	return PAM_SUCCESS;
}