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
|
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "auth-common.h"
#include "userdb.h"
#include "ioloop.h"
#include "array.h"
#include "str.h"
#include "auth-cache.h"
#include "db-dict.h"
#include <dict.h>
struct dict_userdb_module {
struct userdb_module module;
struct dict_connection *conn;
};
struct dict_userdb_iterate_context {
struct userdb_iterate_context ctx;
userdb_callback_t *userdb_callback;
const char *key_prefix;
size_t key_prefix_len;
struct dict_iterate_context *iter;
};
static int
dict_query_save_results(struct auth_request *auth_request,
struct db_dict_value_iter *iter)
{
const char *key, *value, *error;
while (db_dict_value_iter_next(iter, &key, &value)) {
if (value != NULL)
auth_request_set_userdb_field(auth_request, key, value);
}
if (db_dict_value_iter_deinit(&iter, &error) < 0) {
e_error(authdb_event(auth_request), "%s", error);
return -1;
}
return 0;
}
static void userdb_dict_lookup(struct auth_request *auth_request,
userdb_callback_t *callback)
{
struct userdb_module *_module = auth_request->userdb->userdb;
struct dict_userdb_module *module =
(struct dict_userdb_module *)_module;
struct db_dict_value_iter *iter;
enum userdb_result userdb_result;
int ret;
if (array_count(&module->conn->set.userdb_fields) == 0 &&
array_count(&module->conn->set.parsed_userdb_objects) == 0) {
e_error(authdb_event(auth_request),
"No userdb_objects or userdb_fields specified");
callback(USERDB_RESULT_INTERNAL_FAILURE, auth_request);
return;
}
ret = db_dict_value_iter_init(module->conn, auth_request,
&module->conn->set.userdb_fields,
&module->conn->set.parsed_userdb_objects,
&iter);
if (ret < 0)
userdb_result = USERDB_RESULT_INTERNAL_FAILURE;
else if (ret == 0) {
auth_request_log_unknown_user(auth_request, AUTH_SUBSYS_DB);
userdb_result = USERDB_RESULT_USER_UNKNOWN;
} else {
if (dict_query_save_results(auth_request, iter) < 0)
userdb_result = USERDB_RESULT_INTERNAL_FAILURE;
else
userdb_result = USERDB_RESULT_OK;
}
callback(userdb_result, auth_request);
}
static struct userdb_iterate_context *
userdb_dict_iterate_init(struct auth_request *auth_request,
userdb_iter_callback_t *callback, void *context)
{
struct userdb_module *_module = auth_request->userdb->userdb;
struct dict_userdb_module *module =
(struct dict_userdb_module *)_module;
struct dict_userdb_iterate_context *ctx;
string_t *path;
const char *error;
ctx = i_new(struct dict_userdb_iterate_context, 1);
ctx->ctx.auth_request = auth_request;
ctx->ctx.callback = callback;
ctx->ctx.context = context;
auth_request_ref(auth_request);
if (*module->conn->set.iterate_prefix == '\0') {
if (!module->conn->set.iterate_disable) {
e_error(authdb_event(auth_request),
"iterate: iterate_prefix not set");
ctx->ctx.failed = TRUE;
}
return &ctx->ctx;
}
path = t_str_new(128);
str_append(path, DICT_PATH_SHARED);
if (auth_request_var_expand(path, module->conn->set.iterate_prefix,
auth_request, NULL, &error) <= 0) {
e_error(authdb_event(auth_request),
"Failed to expand iterate_prefix=%s: %s",
module->conn->set.iterate_prefix, error);
ctx->ctx.failed = TRUE;
return &ctx->ctx;
}
ctx->key_prefix = p_strdup(auth_request->pool, str_c(path));
ctx->key_prefix_len = strlen(ctx->key_prefix);
struct dict_op_settings set = {
.username = auth_request->fields.user,
};
ctx->iter = dict_iterate_init(module->conn->dict, &set, ctx->key_prefix, 0);
e_debug(authdb_event(auth_request),
"iterate: prefix=%s", ctx->key_prefix);
return &ctx->ctx;
}
static const char *
userdb_dict_get_user(struct dict_userdb_iterate_context *ctx, const char *key)
{
i_assert(strncmp(key, ctx->key_prefix, ctx->key_prefix_len) == 0);
return key + ctx->key_prefix_len;
}
static void userdb_dict_iterate_next(struct userdb_iterate_context *_ctx)
{
struct dict_userdb_iterate_context *ctx =
(struct dict_userdb_iterate_context *)_ctx;
const char *key, *value;
if (ctx->iter != NULL && dict_iterate(ctx->iter, &key, &value))
_ctx->callback(userdb_dict_get_user(ctx, key), _ctx->context);
else
_ctx->callback(NULL, _ctx->context);
}
static int userdb_dict_iterate_deinit(struct userdb_iterate_context *_ctx)
{
struct dict_userdb_iterate_context *ctx =
(struct dict_userdb_iterate_context *)_ctx;
const char *error;
int ret = _ctx->failed ? -1 : 0;
if (dict_iterate_deinit(&ctx->iter, &error) < 0) {
e_error(authdb_event(_ctx->auth_request),
"dict_iterate(%s) failed: %s",
ctx->key_prefix, error);
ret = -1;
}
auth_request_unref(&ctx->ctx.auth_request);
i_free(ctx);
return ret;
}
static struct userdb_module *
userdb_dict_preinit(pool_t pool, const char *args)
{
struct dict_userdb_module *module;
struct dict_connection *conn;
module = p_new(pool, struct dict_userdb_module, 1);
module->conn = conn = db_dict_init(args);
module->module.blocking = TRUE;
module->module.default_cache_key = auth_cache_parse_key(pool,
db_dict_parse_cache_key(&conn->set.keys, &conn->set.userdb_fields,
&conn->set.parsed_userdb_objects));
return &module->module;
}
static void userdb_dict_deinit(struct userdb_module *_module)
{
struct dict_userdb_module *module =
(struct dict_userdb_module *)_module;
db_dict_unref(&module->conn);
}
struct userdb_module_interface userdb_dict =
{
"dict",
userdb_dict_preinit,
NULL,
userdb_dict_deinit,
userdb_dict_lookup,
userdb_dict_iterate_init,
userdb_dict_iterate_next,
userdb_dict_iterate_deinit
};
|