summaryrefslogtreecommitdiffstats
path: root/src/auth/auth-fields.c
blob: 07713901c457d6ca60faf2281e088862d72e859c (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
/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */

#include "auth-common.h"
#include "array.h"
#include "str.h"
#include "strescape.h"
#include "ostream.h"
#include "auth-request.h"
#include "auth-fields.h"

struct auth_fields {
	pool_t pool;
	ARRAY_TYPE(auth_field) fields, snapshot_fields;
	unsigned int snapshot_idx;
	bool snapshotted;
};

struct auth_fields *auth_fields_init(pool_t pool)
{
	struct auth_fields *fields;

	fields = p_new(pool, struct auth_fields, 1);
	fields->pool = pool;
	return fields;
}

static void auth_fields_snapshot_preserve(struct auth_fields *fields)
{
	if (!fields->snapshotted || array_is_created(&fields->snapshot_fields))
		return;

	p_array_init(&fields->snapshot_fields, fields->pool,
		     array_count(&fields->fields));
	array_append_array(&fields->snapshot_fields, &fields->fields);
}

static bool
auth_fields_find_idx(struct auth_fields *fields, const char *key,
		     unsigned int *idx_r)
{
	const struct auth_field *f;
	unsigned int i, count;

	if (!array_is_created(&fields->fields))
		return FALSE;

	f = array_get(&fields->fields, &count);
	for (i = 0; i < count; i++) {
		if (strcmp(f[i].key, key) == 0) {
			*idx_r = i;
			return TRUE;
		}
	}
	return FALSE;
}

void auth_fields_add(struct auth_fields *fields,
		     const char *key, const char *value,
		     enum auth_field_flags flags)
{
	struct auth_field *field;
	unsigned int idx;

	i_assert(*key != '\0');
	i_assert(strchr(key, '\t') == NULL &&
		 strchr(key, '\n') == NULL);

	if (!auth_fields_find_idx(fields, key, &idx)) {
		if (!array_is_created(&fields->fields))
			p_array_init(&fields->fields, fields->pool, 16);

		field = array_append_space(&fields->fields);
		field->key = p_strdup(fields->pool, key);
	} else {
		auth_fields_snapshot_preserve(fields);
		field = array_idx_modifiable(&fields->fields, idx);
	}
	field->value = p_strdup_empty(fields->pool, value);
	field->flags = flags | AUTH_FIELD_FLAG_CHANGED;
}

void auth_fields_remove(struct auth_fields *fields, const char *key)
{
	unsigned int idx;

	if (auth_fields_find_idx(fields, key, &idx)) {
		auth_fields_snapshot_preserve(fields);
		array_delete(&fields->fields, idx, 1);
	}
}

const char *auth_fields_find(struct auth_fields *fields, const char *key)
{
	const struct auth_field *field;
	unsigned int idx;

	if (!auth_fields_find_idx(fields, key, &idx))
		return NULL;

	field = array_idx(&fields->fields, idx);
	return field->value == NULL ? "" : field->value;
}

bool auth_fields_exists(struct auth_fields *fields, const char *key)
{
	return auth_fields_find(fields, key) != NULL;
}

void auth_fields_reset(struct auth_fields *fields)
{
	if (array_is_created(&fields->fields)) {
		auth_fields_snapshot_preserve(fields);
		array_clear(&fields->fields);
	}
}

void auth_fields_import_prefixed(struct auth_fields *fields, const char *prefix,
				 const char *str, enum auth_field_flags flags)
{
	T_BEGIN {
		const char *const *arg = t_strsplit_tabescaped(str);
		const char *key, *value;

		for (; *arg != NULL; arg++) {
			value = strchr(*arg, '=');
			if (value == NULL) {
				key = *arg;
				value = NULL;
			} else {
				key = t_strdup_until(*arg, value++);
				if (*prefix != '\0')
					key = t_strconcat(prefix, key, NULL);
			}
			auth_fields_add(fields, key, value, flags);
		}
	} T_END;
}

void auth_fields_import(struct auth_fields *fields, const char *str,
			enum auth_field_flags flags)
{
	auth_fields_import_prefixed(fields, "", str, flags);
}

const ARRAY_TYPE(auth_field) *auth_fields_export(struct auth_fields *fields)
{
	if (!array_is_created(&fields->fields))
		p_array_init(&fields->fields, fields->pool, 1);
	return &fields->fields;
}

void auth_fields_append(struct auth_fields *fields, string_t *dest,
			enum auth_field_flags flags_mask,
			enum auth_field_flags flags_result)
{
	const struct auth_field *f;
	unsigned int i, count;
	bool first = TRUE;

	if (!array_is_created(&fields->fields))
		return;

	f = array_get(&fields->fields, &count);
	for (i = 0; i < count; i++) {
		if ((f[i].flags & flags_mask) != flags_result)
			continue;

		if (first)
			first = FALSE;
		else
			str_append_c(dest, '\t');
		str_append(dest, f[i].key);
		if (f[i].value != NULL) {
			str_append_c(dest, '=');
			str_append_tabescaped(dest, f[i].value);
		}
	}
}

bool auth_fields_is_empty(struct auth_fields *fields)
{
	return fields == NULL || !array_is_created(&fields->fields) ||
		array_count(&fields->fields) == 0;
}

void auth_fields_booleanize(struct auth_fields *fields, const char *key)
{
	struct auth_field *field;
	unsigned int idx;

	if (auth_fields_find_idx(fields, key, &idx)) {
		field = array_idx_modifiable(&fields->fields, idx);
		field->value = NULL;
	}
}

void auth_fields_snapshot(struct auth_fields *fields)
{
	struct auth_field *field;

	fields->snapshotted = TRUE;
	if (!array_is_created(&fields->fields))
		return;

	if (!array_is_created(&fields->snapshot_fields)) {
		/* try to avoid creating this array */
		fields->snapshot_idx = array_count(&fields->fields);
	} else {
		array_clear(&fields->snapshot_fields);
		array_append_array(&fields->snapshot_fields, &fields->fields);
	}
	array_foreach_modifiable(&fields->fields, field)
		field->flags &= ENUM_NEGATE(AUTH_FIELD_FLAG_CHANGED);
}

void auth_fields_rollback(struct auth_fields *fields)
{
	if (array_is_created(&fields->snapshot_fields)) {
		array_clear(&fields->fields);
		array_append_array(&fields->fields, &fields->snapshot_fields);
	} else if (array_is_created(&fields->fields)) {
		array_delete(&fields->fields, fields->snapshot_idx,
			     array_count(&fields->fields) -
			     fields->snapshot_idx);
	}
}