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
|
/* Copyright (c) 2016-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "ldap-private.h"
#include <stdio.h>
#include <sys/time.h>
struct ldap_search_ctx {
const struct ldap_search_input *input;
struct ldap_result res;
};
static void
ldap_search_result_failure(struct ldap_op_queue_entry *req,
int ret, const char *error)
{
struct ldap_search_ctx *sctx = req->ctx;
sctx->res.openldap_ret = ret;
sctx->res.error_string = error;
req->result_callback(&sctx->res, req->result_callback_ctx);
}
static void ldap_search_result_success(struct ldap_op_queue_entry *req)
{
struct ldap_search_ctx *sctx = req->ctx;
sctx->res.openldap_ret = LDAP_SUCCESS;
req->result_callback(&sctx->res, req->result_callback_ctx);
}
static int
ldap_search_callback(struct ldap_connection *conn,
struct ldap_op_queue_entry *req,
LDAPMessage *message, bool *finished_r)
{
struct ldap_search_ctx *sctx = req->ctx;
int msgtype = ldap_msgtype(message);
char *result_errmsg = NULL;
int ret, result_err;
if (msgtype != LDAP_RES_SEARCH_ENTRY &&
msgtype != LDAP_RES_SEARCH_RESULT) {
*finished_r = FALSE;
return LDAP_SUCCESS;
}
*finished_r = TRUE;
ret = ldap_parse_result(conn->conn, message, &result_err, NULL,
&result_errmsg, NULL, NULL, 0);
if (ret == LDAP_NO_RESULTS_RETURNED) {
/*ret = LDAP_SUCCESS;*/
} else if (ret != LDAP_SUCCESS) {
ldap_search_result_failure(req, ret, t_strdup_printf(
"ldap_parse_result() failed for search: %s", ldap_err2string(ret)));
return ret;
} else if (result_err != LDAP_SUCCESS) {
const struct ldap_search_input *input = &req->input.search;
const char *error = result_errmsg != NULL ?
result_errmsg : ldap_err2string(result_err);
ldap_search_result_failure(req, result_err, t_strdup_printf(
"ldap_search_ext(base=%s, scope=%d, filter=%s) failed: %s",
input->base_dn, input->scope, input->filter, error));
ldap_memfree(result_errmsg);
return result_err;
}
LDAPMessage *res = ldap_first_entry(conn->conn, message);
while(res != NULL) {
struct ldap_entry *obj = p_new(req->pool, struct ldap_entry, 1);
ldap_entry_init(obj, &sctx->res, message);
array_push_back(&sctx->res.entries, obj);
res = ldap_next_entry(conn->conn, res);
}
if (msgtype == LDAP_RES_SEARCH_RESULT) {
ldap_search_result_success(req);
return LDAP_SUCCESS;
}
*finished_r = FALSE;
return LDAP_SUCCESS;
}
static int
ldap_search_send(struct ldap_connection *conn, struct ldap_op_queue_entry *req,
const char **error_r)
{
const struct ldap_search_input *input = &req->input.search;
LDAPControl manageDSAIT = {
LDAP_CONTROL_MANAGEDSAIT, {0, 0}, 0
};
/* try to use ManageDSAIT if available */
LDAPControl *sctrls[] = {
&manageDSAIT,
NULL
};
struct timeval tv = {
.tv_sec = req->timeout_secs,
.tv_usec = 0
};
int ret = ldap_search_ext(conn->conn,
input->base_dn,
input->scope,
input->filter,
(char**)input->attributes,
0,
sctrls,
NULL,
&tv,
input->size_limit,
&req->msgid);
if (ret != LDAP_SUCCESS) {
*error_r = t_strdup_printf(
"ldap_search_ext(base=%s, scope=%d, filter=%s) failed: %s",
input->base_dn, input->scope, input->filter,
ldap_err2string(ret));
}
return ret;
}
void ldap_connection_search_start(struct ldap_connection *conn,
const struct ldap_search_input *input,
ldap_result_callback_t *callback,
void *context)
{
struct ldap_op_queue_entry *req;
pool_t pool = pool_alloconly_create(MEMPOOL_GROWING "ldap search", 128);
req = p_new(pool, struct ldap_op_queue_entry, 1);
req->pool = pool;
struct ldap_search_ctx *sctx = p_new(pool, struct ldap_search_ctx, 1);
sctx->res.conn = conn;
sctx->res.pool = pool;
p_array_init(&sctx->res.entries, req->pool, 8);
req->internal_response_cb = ldap_search_callback;
req->result_callback = callback;
req->result_callback_ctx = context;
req->input.search = *input;
/* copy strings */
req->input.search.base_dn = p_strdup(req->pool, input->base_dn);
req->input.search.filter = p_strdup(req->pool, input->filter);
if (input->attributes != NULL) {
ARRAY_TYPE(const_string) arr;
p_array_init(&arr, req->pool, 8);
for (const char *const *ptr = input->attributes; *ptr != NULL; ptr++) {
const char *tmp = p_strdup(req->pool, *ptr);
array_push_back(&arr, &tmp);
}
array_append_zero(&arr);
req->input.search.attributes = array_front_modifiable(&arr);
}
req->send_request_cb = ldap_search_send;
sctx->input = &req->input.search;
req->ctx = sctx;
req->timeout_secs = input->timeout_secs;
ldap_connection_queue_request(conn, req);
}
|