s3-security: move ALL_SECURITY_INFORMATION to the only user.
[Samba/ekacnet.git] / source3 / lib / tldap_util.c
blob5ff796137f798783efbab00b249f7e2c8bd99916
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async ldap client requests
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "tldap.h"
22 #include "tldap_util.h"
24 bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
25 int *num_values, DATA_BLOB **values)
27 struct tldap_attribute *attributes;
28 int i, num_attributes;
30 if (!tldap_entry_attributes(msg, &num_attributes, &attributes)) {
31 return false;
34 for (i=0; i<num_attributes; i++) {
35 if (strequal(attribute, attributes[i].name)) {
36 break;
39 if (i == num_attributes) {
40 return false;
42 *num_values = attributes[i].num_values;
43 *values = attributes[i].values;
44 return true;
47 bool tldap_get_single_valueblob(struct tldap_message *msg,
48 const char *attribute, DATA_BLOB *blob)
50 int num_values;
51 DATA_BLOB *values;
53 if (attribute == NULL) {
54 return NULL;
56 if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
57 return NULL;
59 if (num_values != 1) {
60 return NULL;
62 *blob = values[0];
63 return true;
66 char *tldap_talloc_single_attribute(struct tldap_message *msg,
67 const char *attribute,
68 TALLOC_CTX *mem_ctx)
70 DATA_BLOB val;
71 char *result;
72 size_t len;
74 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
75 return false;
77 if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
78 val.data, val.length,
79 &result, &len, false)) {
80 return NULL;
82 return result;
85 bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
86 struct dom_sid *sid)
88 DATA_BLOB val;
90 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
91 return false;
93 return sid_parse((char *)val.data, val.length, sid);
96 bool tldap_pull_guid(struct tldap_message *msg, const char *attribute,
97 struct GUID *guid)
99 DATA_BLOB val;
101 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
102 return false;
104 return NT_STATUS_IS_OK(GUID_from_data_blob(&val, guid));
107 static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
108 int num_newvals, DATA_BLOB *newvals)
110 int num_values = talloc_array_length(mod->values);
111 int i;
112 DATA_BLOB *tmp;
114 tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
115 num_values + num_newvals);
116 if (tmp == NULL) {
117 return false;
119 mod->values = tmp;
121 for (i=0; i<num_newvals; i++) {
122 mod->values[i+num_values].data = (uint8_t *)talloc_memdup(
123 mod->values, newvals[i].data, newvals[i].length);
124 if (mod->values[i+num_values].data == NULL) {
125 return false;
127 mod->values[i+num_values].length = newvals[i].length;
129 mod->num_values = num_values + num_newvals;
130 return true;
133 bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
134 int mod_op, const char *attrib,
135 int num_newvals, DATA_BLOB *newvals)
137 struct tldap_mod new_mod;
138 struct tldap_mod *mods = *pmods;
139 struct tldap_mod *mod = NULL;
140 int i, num_mods;
142 if (mods == NULL) {
143 mods = talloc_array(mem_ctx, struct tldap_mod, 0);
145 if (mods == NULL) {
146 return false;
149 num_mods = talloc_array_length(mods);
151 for (i=0; i<num_mods; i++) {
152 if ((mods[i].mod_op == mod_op)
153 && strequal(mods[i].attribute, attrib)) {
154 mod = &mods[i];
155 break;
159 if (mod == NULL) {
160 new_mod.mod_op = mod_op;
161 new_mod.attribute = talloc_strdup(mods, attrib);
162 if (new_mod.attribute == NULL) {
163 return false;
165 new_mod.num_values = 0;
166 new_mod.values = NULL;
167 mod = &new_mod;
170 if ((num_newvals != 0)
171 && !tldap_add_blob_vals(mods, mod, num_newvals, newvals)) {
172 return false;
175 if (i == num_mods) {
176 mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
177 num_mods+1);
178 if (mods == NULL) {
179 return false;
181 mods[num_mods] = *mod;
184 *pmods = mods;
185 return true;
188 bool tldap_add_mod_str(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
189 int mod_op, const char *attrib, const char *str)
191 DATA_BLOB utf8;
192 bool ret;
194 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
195 strlen(str), &utf8.data, &utf8.length,
196 false)) {
197 return false;
200 ret = tldap_add_mod_blobs(mem_ctx, pmods, mod_op, attrib, 1, &utf8);
201 TALLOC_FREE(utf8.data);
202 return ret;
205 static bool tldap_make_mod_blob_int(struct tldap_message *existing,
206 TALLOC_CTX *mem_ctx,
207 int *pnum_mods, struct tldap_mod **pmods,
208 const char *attrib, DATA_BLOB newval,
209 int (*comparison)(const DATA_BLOB *d1,
210 const DATA_BLOB *d2))
212 int num_values = 0;
213 DATA_BLOB *values = NULL;
214 DATA_BLOB oldval = data_blob_null;
216 if ((existing != NULL)
217 && tldap_entry_values(existing, attrib, &num_values, &values)) {
219 if (num_values > 1) {
220 /* can't change multivalue attributes atm */
221 return false;
223 if (num_values == 1) {
224 oldval = values[0];
228 if ((oldval.data != NULL) && (newval.data != NULL)
229 && (comparison(&oldval, &newval) == 0)) {
230 /* Believe it or not, but LDAP will deny a delete and
231 an add at the same time if the values are the
232 same... */
233 DEBUG(10,("smbldap_make_mod_blob: attribute |%s| not "
234 "changed.\n", attrib));
235 return true;
238 if (oldval.data != NULL) {
239 /* By deleting exactly the value we found in the entry this
240 * should be race-free in the sense that the LDAP-Server will
241 * deny the complete operation if somebody changed the
242 * attribute behind our back. */
243 /* This will also allow modifying single valued attributes in
244 * Novell NDS. In NDS you have to first remove attribute and
245 * then you could add new value */
247 DEBUG(10, ("smbldap_make_mod_blob: deleting attribute |%s|\n",
248 attrib));
249 if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_DELETE,
250 attrib, 1, &oldval)) {
251 return false;
255 /* Regardless of the real operation (add or modify)
256 we add the new value here. We rely on deleting
257 the old value, should it exist. */
259 if (newval.data != NULL) {
260 DEBUG(10, ("smbldap_make_mod: adding attribute |%s| value len "
261 "%d\n", attrib, (int)newval.length));
262 if (!tldap_add_mod_blobs(mem_ctx, pmods, TLDAP_MOD_ADD,
263 attrib, 1, &newval)) {
264 return false;
267 *pnum_mods = talloc_array_length(*pmods);
268 return true;
271 bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
272 int *pnum_mods, struct tldap_mod **pmods,
273 const char *attrib, DATA_BLOB newval)
275 return tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
276 attrib, newval, data_blob_cmp);
279 static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
281 char *s1, *s2;
282 size_t s1len, s2len;
283 int ret;
285 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d1->data,
286 d1->length, &s1, &s1len, false)) {
287 /* can't do much here */
288 return 0;
290 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d2->data,
291 d2->length, &s2, &s2len, false)) {
292 /* can't do much here */
293 TALLOC_FREE(s1);
294 return 0;
296 ret = StrCaseCmp(s1, s2);
297 TALLOC_FREE(s2);
298 TALLOC_FREE(s1);
299 return ret;
302 bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
303 int *pnum_mods, struct tldap_mod **pmods,
304 const char *attrib, const char *fmt, ...)
306 va_list ap;
307 char *newval;
308 bool ret;
309 DATA_BLOB blob = data_blob_null;
311 va_start(ap, fmt);
312 newval = talloc_vasprintf(talloc_tos(), fmt, ap);
313 va_end(ap);
315 if (newval == NULL) {
316 return false;
319 blob.length = strlen(newval);
320 if (blob.length != 0) {
321 blob.data = CONST_DISCARD(uint8_t *, newval);
323 ret = tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
324 attrib, blob, compare_utf8_blobs);
325 TALLOC_FREE(newval);
326 return ret;
329 const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld, int rc)
331 const char *ld_error = NULL;
332 char *res;
334 ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
335 res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s", rc,
336 tldap_err2string(rc),
337 ld_error ? ld_error : "unknown");
338 return res;
341 int tldap_search_va(struct tldap_context *ld, const char *base, int scope,
342 const char *attrs[], int num_attrs, int attrsonly,
343 TALLOC_CTX *mem_ctx, struct tldap_message ***res,
344 const char *fmt, va_list ap)
346 char *filter;
347 int ret;
349 filter = talloc_vasprintf(talloc_tos(), fmt, ap);
350 if (filter == NULL) {
351 return TLDAP_NO_MEMORY;
354 ret = tldap_search(ld, base, scope, filter,
355 attrs, num_attrs, attrsonly,
356 NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
357 0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
358 mem_ctx, res, NULL);
359 TALLOC_FREE(filter);
360 return ret;
363 int tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
364 const char *attrs[], int num_attrs, int attrsonly,
365 TALLOC_CTX *mem_ctx, struct tldap_message ***res,
366 const char *fmt, ...)
368 va_list ap;
369 int ret;
371 va_start(ap, fmt);
372 ret = tldap_search_va(ld, base, scope, attrs, num_attrs, attrsonly,
373 mem_ctx, res, fmt, ap);
374 va_end(ap);
375 return ret;
378 bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
379 uint64_t *presult)
381 char *str;
382 uint64_t result;
384 str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
385 if (str == NULL) {
386 DEBUG(10, ("Could not find attribute %s\n", attr));
387 return false;
389 result = strtoull(str, NULL, 10);
390 TALLOC_FREE(str);
391 *presult = result;
392 return true;
395 bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
396 uint32_t *presult)
398 uint64_t result;
400 if (!tldap_pull_uint64(msg, attr, &result)) {
401 return false;
403 *presult = (uint32_t)result;
404 return true;
407 struct tldap_fetch_rootdse_state {
408 struct tldap_context *ld;
409 struct tldap_message *rootdse;
412 static void tldap_fetch_rootdse_done(struct tevent_req *subreq);
414 struct tevent_req *tldap_fetch_rootdse_send(TALLOC_CTX *mem_ctx,
415 struct tevent_context *ev,
416 struct tldap_context *ld)
418 struct tevent_req *req, *subreq;
419 struct tldap_fetch_rootdse_state *state;
420 static const char *attrs[2] = { "*", "+" };
422 req = tevent_req_create(mem_ctx, &state,
423 struct tldap_fetch_rootdse_state);
424 if (req == NULL) {
425 return NULL;
427 state->ld = ld;
428 state->rootdse = NULL;
430 subreq = tldap_search_send(
431 mem_ctx, ev, ld, "", TLDAP_SCOPE_BASE, "(objectclass=*)",
432 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0, 0, 0, 0);
433 if (tevent_req_nomem(subreq, req)) {
434 return tevent_req_post(req, ev);
436 tevent_req_set_callback(subreq, tldap_fetch_rootdse_done, req);
437 return req;
440 static void tldap_fetch_rootdse_done(struct tevent_req *subreq)
442 struct tevent_req *req = tevent_req_callback_data(
443 subreq, struct tevent_req);
444 struct tldap_fetch_rootdse_state *state = tevent_req_data(
445 req, struct tldap_fetch_rootdse_state);
446 struct tldap_message *msg;
447 int rc;
449 rc = tldap_search_recv(subreq, state, &msg);
450 if (rc != TLDAP_SUCCESS) {
451 TALLOC_FREE(subreq);
452 tevent_req_error(req, rc);
453 return;
456 switch (tldap_msg_type(msg)) {
457 case TLDAP_RES_SEARCH_ENTRY:
458 if (state->rootdse != NULL) {
459 goto protocol_error;
461 state->rootdse = msg;
462 break;
463 case TLDAP_RES_SEARCH_RESULT:
464 TALLOC_FREE(subreq);
465 if (state->rootdse == NULL) {
466 goto protocol_error;
468 tevent_req_done(req);
469 break;
470 default:
471 goto protocol_error;
473 return;
475 protocol_error:
476 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
477 return;
480 int tldap_fetch_rootdse_recv(struct tevent_req *req)
482 struct tldap_fetch_rootdse_state *state = tevent_req_data(
483 req, struct tldap_fetch_rootdse_state);
484 int err;
485 char *dn;
487 if (tevent_req_is_ldap_error(req, &err)) {
488 return err;
490 /* Trigger parsing the dn, just to make sure it's ok */
491 if (!tldap_entry_dn(state->rootdse, &dn)) {
492 return TLDAP_DECODING_ERROR;
494 if (!tldap_context_setattr(state->ld, "tldap:rootdse",
495 &state->rootdse)) {
496 return TLDAP_NO_MEMORY;
498 return 0;
501 int tldap_fetch_rootdse(struct tldap_context *ld)
503 TALLOC_CTX *frame = talloc_stackframe();
504 struct tevent_context *ev;
505 struct tevent_req *req;
506 int result;
508 ev = event_context_init(frame);
509 if (ev == NULL) {
510 result = TLDAP_NO_MEMORY;
511 goto fail;
514 req = tldap_fetch_rootdse_send(frame, ev, ld);
515 if (req == NULL) {
516 result = TLDAP_NO_MEMORY;
517 goto fail;
520 if (!tevent_req_poll(req, ev)) {
521 result = TLDAP_OPERATIONS_ERROR;
522 goto fail;
525 result = tldap_fetch_rootdse_recv(req);
526 fail:
527 TALLOC_FREE(frame);
528 return result;
531 struct tldap_message *tldap_rootdse(struct tldap_context *ld)
533 return talloc_get_type(tldap_context_getattr(ld, "tldap:rootdse"),
534 struct tldap_message);
537 bool tldap_entry_has_attrvalue(struct tldap_message *msg,
538 const char *attribute,
539 const DATA_BLOB blob)
541 int i, num_values;
542 DATA_BLOB *values;
544 if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
545 return false;
547 for (i=0; i<num_values; i++) {
548 if (data_blob_cmp(&values[i], &blob) == 0) {
549 return true;
552 return false;
555 bool tldap_supports_control(struct tldap_context *ld, const char *oid)
557 struct tldap_message *rootdse = tldap_rootdse(ld);
559 if (rootdse == NULL) {
560 return false;
562 return tldap_entry_has_attrvalue(rootdse, "supportedControl",
563 data_blob_const(oid, strlen(oid)));
566 struct tldap_control *tldap_add_control(TALLOC_CTX *mem_ctx,
567 struct tldap_control *ctrls,
568 int num_ctrls,
569 struct tldap_control *ctrl)
571 struct tldap_control *result;
573 result = talloc_array(mem_ctx, struct tldap_control, num_ctrls+1);
574 if (result == NULL) {
575 return NULL;
577 memcpy(result, ctrls, sizeof(struct tldap_control) * num_ctrls);
578 result[num_ctrls] = *ctrl;
579 return result;
583 * Find a control returned by the server
585 struct tldap_control *tldap_msg_findcontrol(struct tldap_message *msg,
586 const char *oid)
588 struct tldap_control *controls;
589 int i, num_controls;
591 tldap_msg_sctrls(msg, &num_controls, &controls);
593 for (i=0; i<num_controls; i++) {
594 if (strcmp(controls[i].oid, oid) == 0) {
595 return &controls[i];
598 return NULL;
601 struct tldap_search_paged_state {
602 struct tevent_context *ev;
603 struct tldap_context *ld;
604 const char *base;
605 const char *filter;
606 int scope;
607 const char **attrs;
608 int num_attrs;
609 int attrsonly;
610 struct tldap_control *sctrls;
611 int num_sctrls;
612 struct tldap_control *cctrls;
613 int num_cctrls;
614 int timelimit;
615 int sizelimit;
616 int deref;
618 int page_size;
619 struct asn1_data *asn1;
620 DATA_BLOB cookie;
621 struct tldap_message *result;
624 static struct tevent_req *tldap_ship_paged_search(
625 TALLOC_CTX *mem_ctx,
626 struct tldap_search_paged_state *state)
628 struct tldap_control *pgctrl;
629 struct asn1_data *asn1;
631 asn1 = asn1_init(state);
632 if (asn1 == NULL) {
633 return NULL;
635 asn1_push_tag(asn1, ASN1_SEQUENCE(0));
636 asn1_write_Integer(asn1, state->page_size);
637 asn1_write_OctetString(asn1, state->cookie.data, state->cookie.length);
638 asn1_pop_tag(asn1);
639 if (asn1->has_error) {
640 TALLOC_FREE(asn1);
641 return NULL;
643 state->asn1 = asn1;
645 pgctrl = &state->sctrls[state->num_sctrls-1];
646 pgctrl->oid = TLDAP_CONTROL_PAGEDRESULTS;
647 pgctrl->critical = true;
648 if (!asn1_blob(state->asn1, &pgctrl->value)) {
649 TALLOC_FREE(asn1);
650 return NULL;
652 return tldap_search_send(mem_ctx, state->ev, state->ld, state->base,
653 state->scope, state->filter, state->attrs,
654 state->num_attrs, state->attrsonly,
655 state->sctrls, state->num_sctrls,
656 state->cctrls, state->num_cctrls,
657 state->timelimit, state->sizelimit,
658 state->deref);
661 static void tldap_search_paged_done(struct tevent_req *subreq);
663 struct tevent_req *tldap_search_paged_send(TALLOC_CTX *mem_ctx,
664 struct tevent_context *ev,
665 struct tldap_context *ld,
666 const char *base, int scope,
667 const char *filter,
668 const char **attrs,
669 int num_attrs,
670 int attrsonly,
671 struct tldap_control *sctrls,
672 int num_sctrls,
673 struct tldap_control *cctrls,
674 int num_cctrls,
675 int timelimit,
676 int sizelimit,
677 int deref,
678 int page_size)
680 struct tevent_req *req, *subreq;
681 struct tldap_search_paged_state *state;
682 struct tldap_control empty_control;
684 req = tevent_req_create(mem_ctx, &state,
685 struct tldap_search_paged_state);
686 if (req == NULL) {
687 return NULL;
689 state->ev = ev;
690 state->ld = ld;
691 state->base = base;
692 state->filter = filter;
693 state->scope = scope;
694 state->attrs = attrs;
695 state->num_attrs = num_attrs;
696 state->attrsonly = attrsonly;
697 state->cctrls = cctrls;
698 state->num_cctrls = num_cctrls;
699 state->timelimit = timelimit;
700 state->sizelimit = sizelimit;
701 state->deref = deref;
703 state->page_size = page_size;
704 state->asn1 = NULL;
705 state->cookie = data_blob_null;
707 ZERO_STRUCT(empty_control);
709 state->sctrls = tldap_add_control(state, sctrls, num_sctrls,
710 &empty_control);
711 if (tevent_req_nomem(state->sctrls, req)) {
712 return tevent_req_post(req, ev);
714 state->num_sctrls = num_sctrls+1;
716 subreq = tldap_ship_paged_search(state, state);
717 if (tevent_req_nomem(subreq, req)) {
718 return tevent_req_post(req, ev);
720 tevent_req_set_callback(subreq, tldap_search_paged_done, req);
722 return req;
725 static void tldap_search_paged_done(struct tevent_req *subreq)
727 struct tevent_req *req = tevent_req_callback_data(
728 subreq, struct tevent_req);
729 struct tldap_search_paged_state *state = tevent_req_data(
730 req, struct tldap_search_paged_state);
731 struct asn1_data *asn1;
732 struct tldap_control *pgctrl;
733 int rc, size;
735 rc = tldap_search_recv(subreq, state, &state->result);
736 if (rc != TLDAP_SUCCESS) {
737 TALLOC_FREE(subreq);
738 tevent_req_error(req, rc);
739 return;
742 TALLOC_FREE(state->asn1);
744 switch (tldap_msg_type(state->result)) {
745 case TLDAP_RES_SEARCH_ENTRY:
746 case TLDAP_RES_SEARCH_REFERENCE:
747 tevent_req_notify_callback(req);
748 return;
749 case TLDAP_RES_SEARCH_RESULT:
750 break;
751 default:
752 TALLOC_FREE(subreq);
753 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
754 return;
757 TALLOC_FREE(subreq);
759 /* We've finished one paged search, fire the next */
761 pgctrl = tldap_msg_findcontrol(state->result,
762 TLDAP_CONTROL_PAGEDRESULTS);
763 if (pgctrl == NULL) {
764 /* RFC2696 requires the server to return the control */
765 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
766 return;
769 TALLOC_FREE(state->cookie.data);
771 asn1 = asn1_init(talloc_tos());
772 if (asn1 == NULL) {
773 tevent_req_error(req, TLDAP_NO_MEMORY);
774 return;
777 asn1_load_nocopy(asn1, pgctrl->value.data, pgctrl->value.length);
778 asn1_start_tag(asn1, ASN1_SEQUENCE(0));
779 asn1_read_Integer(asn1, &size);
780 asn1_read_OctetString(asn1, state, &state->cookie);
781 asn1_end_tag(asn1);
782 if (asn1->has_error) {
783 tevent_req_error(req, TLDAP_DECODING_ERROR);
784 return;
786 TALLOC_FREE(asn1);
788 if (state->cookie.length == 0) {
789 /* We're done, no cookie anymore */
790 tevent_req_done(req);
791 return;
794 TALLOC_FREE(state->result);
796 subreq = tldap_ship_paged_search(state, state);
797 if (tevent_req_nomem(subreq, req)) {
798 return;
800 tevent_req_set_callback(subreq, tldap_search_paged_done, req);
803 int tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
804 struct tldap_message **pmsg)
806 struct tldap_search_paged_state *state = tevent_req_data(
807 req, struct tldap_search_paged_state);
808 int err;
810 if (!tevent_req_is_in_progress(req)
811 && tevent_req_is_ldap_error(req, &err)) {
812 return err;
814 if (tevent_req_is_in_progress(req)) {
815 switch (tldap_msg_type(state->result)) {
816 case TLDAP_RES_SEARCH_ENTRY:
817 case TLDAP_RES_SEARCH_REFERENCE:
818 break;
819 default:
820 return TLDAP_PROTOCOL_ERROR;
823 *pmsg = talloc_move(mem_ctx, &state->result);
824 return TLDAP_SUCCESS;