aboutsummaryrefslogtreecommitdiff
path: root/tests/mock_pam.c
blob: f32d95fc753c0e63e93d3853181d65a2b269a328 (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
/*
 * Copyright © 2012 Canonical Ltd. All rights reserved.
 *
 * Author(s): David Barth <david.barth@canonical.com>
 *
 */

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

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

struct pam_handle {
	void *item[PAM_NUM_ITEMS];

	struct pam_conv *conv;

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

int fake_conv (int __attribute__((unused)) num_msg,
               const struct pam_message **msg,
               struct pam_response **resp,
               void __attribute__((unused)) *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_FREERDP2_PROMPT_GUESTLOGIN) == 0) {
		response->resp = strdup ("guest"); /* IMPORTANT: this needs to be in /etc/passwd */
	} else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_USER) == 0) {
		response->resp = strdup ("ruser");
	} else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_HOST) == 0) {
		response->resp = strdup ("protocol://rhost/dummy");
	} else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_PASSWORD) == 0) {
		response->resp = strdup ("password");
	} else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_DOMAIN) == 0) {
		response->resp = strdup ("domain");
	} else {
		free(response);
		return PAM_SYMBOL_ERR; /* leaks... */
	}

	*resp = response;

	return PAM_SUCCESS;
}

int fake_conv_empty_password (int __attribute__((unused)) num_msg,
                              const struct pam_message **msg,
                              struct pam_response **resp,
                              void __attribute__((unused)) *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_FREERDP2_PROMPT_GUESTLOGIN) == 0) {
		response->resp = strdup ("guest"); /* IMPORTANT: this needs to be in /etc/passwd */
	} else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_USER) == 0) {
		response->resp = strdup ("ruser");
	} else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_HOST) == 0) {
		response->resp = strdup ("protocol://rhost/dummy");
	} else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_PASSWORD) == 0) {
		response->resp = strdup ("");
	} else if (strcmp((*msg)->msg, PAM_FREERDP2_PROMPT_DOMAIN) == 0) {
		response->resp = strdup ("domain");
	} else {
		free(response);
		return PAM_SYMBOL_ERR; /* leaks... */
	}

	*resp = response;

	return PAM_SUCCESS;
}

struct pam_conv static_conv = { &fake_conv, (void *)NULL };
struct pam_conv static_conv_empty_pswd = { &fake_conv_empty_password, (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;
}

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

	if (newh != NULL) {
		newh->conv = &static_conv_empty_pswd;
		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;
}