s3:trusts_util: pass dcname to trust_pw_change()
[Samba.git] / source3 / lib / tldap_util.c
blob89f812b97e07f04b62fd9416582bfde4b96b8746
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"
23 #include "../libcli/security/security.h"
24 #include "../lib/util/asn1.h"
25 #include "../librpc/ndr/libndr.h"
26 #include "lib/util/base64.h"
28 bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
29 DATA_BLOB **values, int *num_values)
31 struct tldap_attribute *attributes;
32 int i, num_attributes;
34 if (!tldap_entry_attributes(msg, &attributes, &num_attributes)) {
35 return false;
38 for (i=0; i<num_attributes; i++) {
39 if (strequal(attribute, attributes[i].name)) {
40 break;
43 if (i == num_attributes) {
44 return false;
46 *num_values = attributes[i].num_values;
47 *values = attributes[i].values;
48 return true;
51 bool tldap_get_single_valueblob(struct tldap_message *msg,
52 const char *attribute, DATA_BLOB *blob)
54 int num_values;
55 DATA_BLOB *values;
57 if (attribute == NULL) {
58 return NULL;
60 if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
61 return NULL;
63 if (num_values != 1) {
64 return NULL;
66 *blob = values[0];
67 return true;
70 char *tldap_talloc_single_attribute(struct tldap_message *msg,
71 const char *attribute,
72 TALLOC_CTX *mem_ctx)
74 DATA_BLOB val;
75 char *result;
76 size_t len;
78 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
79 return NULL;
81 if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
82 val.data, val.length,
83 &result, &len)) {
84 return NULL;
86 return result;
89 bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
90 struct dom_sid *sid)
92 DATA_BLOB val;
94 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
95 return false;
97 return sid_parse(val.data, val.length, sid);
100 bool tldap_pull_guid(struct tldap_message *msg, const char *attribute,
101 struct GUID *guid)
103 DATA_BLOB val;
105 if (!tldap_get_single_valueblob(msg, attribute, &val)) {
106 return false;
108 return NT_STATUS_IS_OK(GUID_from_data_blob(&val, guid));
111 static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
112 DATA_BLOB *newvals, int num_newvals)
114 int num_values = talloc_array_length(mod->values);
115 int i;
116 DATA_BLOB *tmp;
118 tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
119 num_values + num_newvals);
120 if (tmp == NULL) {
121 return false;
123 mod->values = tmp;
125 for (i=0; i<num_newvals; i++) {
126 mod->values[i+num_values].data = (uint8_t *)talloc_memdup(
127 mod->values, newvals[i].data, newvals[i].length);
128 if (mod->values[i+num_values].data == NULL) {
129 return false;
131 mod->values[i+num_values].length = newvals[i].length;
133 mod->num_values = num_values + num_newvals;
134 return true;
137 bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx,
138 struct tldap_mod **pmods, int *pnum_mods,
139 int mod_op, const char *attrib,
140 DATA_BLOB *newvals, int num_newvals)
142 struct tldap_mod new_mod;
143 struct tldap_mod *mods = *pmods;
144 struct tldap_mod *mod = NULL;
145 int i, num_mods;
147 if (mods == NULL) {
148 mods = talloc_array(mem_ctx, struct tldap_mod, 0);
150 if (mods == NULL) {
151 return false;
154 num_mods = *pnum_mods;
156 for (i=0; i<num_mods; i++) {
157 if ((mods[i].mod_op == mod_op)
158 && strequal(mods[i].attribute, attrib)) {
159 mod = &mods[i];
160 break;
164 if (mod == NULL) {
165 new_mod.mod_op = mod_op;
166 new_mod.attribute = talloc_strdup(mods, attrib);
167 if (new_mod.attribute == NULL) {
168 return false;
170 new_mod.num_values = 0;
171 new_mod.values = NULL;
172 mod = &new_mod;
175 if ((num_newvals != 0)
176 && !tldap_add_blob_vals(mods, mod, newvals, num_newvals)) {
177 return false;
180 if ((i == num_mods) && (talloc_array_length(mods) < num_mods + 1)) {
181 mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
182 num_mods+1);
183 if (mods == NULL) {
184 return false;
186 mods[num_mods] = *mod;
189 *pmods = mods;
190 *pnum_mods += 1;
191 return true;
194 bool tldap_add_mod_str(TALLOC_CTX *mem_ctx,
195 struct tldap_mod **pmods, int *pnum_mods,
196 int mod_op, const char *attrib, const char *str)
198 DATA_BLOB utf8;
199 bool ret;
201 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
202 strlen(str), &utf8.data, &utf8.length)) {
203 return false;
206 ret = tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods, mod_op, attrib,
207 &utf8, 1);
208 TALLOC_FREE(utf8.data);
209 return ret;
212 static bool tldap_make_mod_blob_int(struct tldap_message *existing,
213 TALLOC_CTX *mem_ctx,
214 struct tldap_mod **pmods, int *pnum_mods,
215 const char *attrib, DATA_BLOB newval,
216 int (*comparison)(const DATA_BLOB *d1,
217 const DATA_BLOB *d2))
219 int num_values = 0;
220 DATA_BLOB *values = NULL;
221 DATA_BLOB oldval = data_blob_null;
223 if ((existing != NULL)
224 && tldap_entry_values(existing, attrib, &values, &num_values)) {
226 if (num_values > 1) {
227 /* can't change multivalue attributes atm */
228 return false;
230 if (num_values == 1) {
231 oldval = values[0];
235 if ((oldval.data != NULL) && (newval.data != NULL)
236 && (comparison(&oldval, &newval) == 0)) {
237 /* Believe it or not, but LDAP will deny a delete and
238 an add at the same time if the values are the
239 same... */
240 DEBUG(10,("tldap_make_mod_blob_int: attribute |%s| not "
241 "changed.\n", attrib));
242 return true;
245 if (oldval.data != NULL) {
246 /* By deleting exactly the value we found in the entry this
247 * should be race-free in the sense that the LDAP-Server will
248 * deny the complete operation if somebody changed the
249 * attribute behind our back. */
250 /* This will also allow modifying single valued attributes in
251 * Novell NDS. In NDS you have to first remove attribute and
252 * then you could add new value */
254 DEBUG(10, ("tldap_make_mod_blob_int: deleting attribute |%s|\n",
255 attrib));
256 if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
257 TLDAP_MOD_DELETE,
258 attrib, &oldval, 1)) {
259 return false;
263 /* Regardless of the real operation (add or modify)
264 we add the new value here. We rely on deleting
265 the old value, should it exist. */
267 if (newval.data != NULL) {
268 DEBUG(10, ("tldap_make_mod_blob_int: adding attribute |%s| value len "
269 "%d\n", attrib, (int)newval.length));
270 if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
271 TLDAP_MOD_ADD,
272 attrib, &newval, 1)) {
273 return false;
276 return true;
279 bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
280 struct tldap_mod **pmods, int *pnum_mods,
281 const char *attrib, DATA_BLOB newval)
283 return tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
284 attrib, newval, data_blob_cmp);
287 static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
289 char *s1, *s2;
290 size_t s1len, s2len;
291 int ret;
293 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d1->data,
294 d1->length, &s1, &s1len)) {
295 /* can't do much here */
296 return 0;
298 if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d2->data,
299 d2->length, &s2, &s2len)) {
300 /* can't do much here */
301 TALLOC_FREE(s1);
302 return 0;
304 ret = strcasecmp_m(s1, s2);
305 TALLOC_FREE(s2);
306 TALLOC_FREE(s1);
307 return ret;
310 bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
311 struct tldap_mod **pmods, int *pnum_mods,
312 const char *attrib, const char *fmt, ...)
314 va_list ap;
315 char *newval;
316 bool ret;
317 DATA_BLOB blob = data_blob_null;
319 va_start(ap, fmt);
320 newval = talloc_vasprintf(talloc_tos(), fmt, ap);
321 va_end(ap);
323 if (newval == NULL) {
324 return false;
327 blob.length = strlen(newval);
328 if (blob.length != 0) {
329 blob.data = discard_const_p(uint8_t, newval);
331 ret = tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
332 attrib, blob, compare_utf8_blobs);
333 TALLOC_FREE(newval);
334 return ret;
337 const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld,
338 TLDAPRC rc)
340 const char *ld_error = NULL;
341 char *res;
343 if (ld != NULL) {
344 ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
346 res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s",
347 (int)TLDAP_RC_V(rc), tldap_rc2string(rc),
348 ld_error ? ld_error : "unknown");
349 return res;
352 TLDAPRC tldap_search_va(struct tldap_context *ld, const char *base, int scope,
353 const char *attrs[], int num_attrs, int attrsonly,
354 TALLOC_CTX *mem_ctx, struct tldap_message ***res,
355 const char *fmt, va_list ap)
357 char *filter;
358 TLDAPRC rc;
360 filter = talloc_vasprintf(talloc_tos(), fmt, ap);
361 if (filter == NULL) {
362 return TLDAP_NO_MEMORY;
365 rc = tldap_search(ld, base, scope, filter,
366 attrs, num_attrs, attrsonly,
367 NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
368 0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
369 mem_ctx, res);
370 TALLOC_FREE(filter);
371 return rc;
374 TLDAPRC tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
375 const char *attrs[], int num_attrs, int attrsonly,
376 TALLOC_CTX *mem_ctx, struct tldap_message ***res,
377 const char *fmt, ...)
379 va_list ap;
380 TLDAPRC rc;
382 va_start(ap, fmt);
383 rc = tldap_search_va(ld, base, scope, attrs, num_attrs, attrsonly,
384 mem_ctx, res, fmt, ap);
385 va_end(ap);
386 return rc;
389 bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
390 uint64_t *presult)
392 char *str;
393 uint64_t result;
395 str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
396 if (str == NULL) {
397 DEBUG(10, ("Could not find attribute %s\n", attr));
398 return false;
400 result = strtoull(str, NULL, 10);
401 TALLOC_FREE(str);
402 *presult = result;
403 return true;
406 bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
407 uint32_t *presult)
409 uint64_t result;
411 if (!tldap_pull_uint64(msg, attr, &result)) {
412 return false;
414 *presult = (uint32_t)result;
415 return true;
418 struct tldap_fetch_rootdse_state {
419 struct tldap_context *ld;
420 struct tldap_message *rootdse;
423 static void tldap_fetch_rootdse_done(struct tevent_req *subreq);
425 struct tevent_req *tldap_fetch_rootdse_send(TALLOC_CTX *mem_ctx,
426 struct tevent_context *ev,
427 struct tldap_context *ld)
429 struct tevent_req *req, *subreq;
430 struct tldap_fetch_rootdse_state *state;
431 static const char *attrs[2] = { "*", "+" };
433 req = tevent_req_create(mem_ctx, &state,
434 struct tldap_fetch_rootdse_state);
435 if (req == NULL) {
436 return NULL;
438 state->ld = ld;
439 state->rootdse = NULL;
441 subreq = tldap_search_send(
442 mem_ctx, ev, ld, "", TLDAP_SCOPE_BASE, "(objectclass=*)",
443 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0, 0, 0, 0);
444 if (tevent_req_nomem(subreq, req)) {
445 return tevent_req_post(req, ev);
447 tevent_req_set_callback(subreq, tldap_fetch_rootdse_done, req);
448 return req;
451 static void tldap_fetch_rootdse_done(struct tevent_req *subreq)
453 struct tevent_req *req = tevent_req_callback_data(
454 subreq, struct tevent_req);
455 struct tldap_fetch_rootdse_state *state = tevent_req_data(
456 req, struct tldap_fetch_rootdse_state);
457 struct tldap_message *msg;
458 TLDAPRC rc;
460 rc = tldap_search_recv(subreq, state, &msg);
461 if (tevent_req_ldap_error(req, rc)) {
462 TALLOC_FREE(subreq);
463 return;
466 switch (tldap_msg_type(msg)) {
467 case TLDAP_RES_SEARCH_ENTRY:
468 if (state->rootdse != NULL) {
469 goto protocol_error;
471 state->rootdse = msg;
472 break;
473 case TLDAP_RES_SEARCH_RESULT:
474 TALLOC_FREE(subreq);
475 if (state->rootdse == NULL) {
476 goto protocol_error;
478 tevent_req_done(req);
479 break;
480 default:
481 goto protocol_error;
483 return;
485 protocol_error:
486 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
487 return;
490 TLDAPRC tldap_fetch_rootdse_recv(struct tevent_req *req)
492 struct tldap_fetch_rootdse_state *state = tevent_req_data(
493 req, struct tldap_fetch_rootdse_state);
494 TLDAPRC rc;
495 char *dn;
497 if (tevent_req_is_ldap_error(req, &rc)) {
498 return rc;
500 /* Trigger parsing the dn, just to make sure it's ok */
501 if (!tldap_entry_dn(state->rootdse, &dn)) {
502 return TLDAP_DECODING_ERROR;
504 if (!tldap_context_setattr(state->ld, "tldap:rootdse",
505 &state->rootdse)) {
506 return TLDAP_NO_MEMORY;
508 return TLDAP_SUCCESS;
511 TLDAPRC tldap_fetch_rootdse(struct tldap_context *ld)
513 TALLOC_CTX *frame = talloc_stackframe();
514 struct tevent_context *ev;
515 struct tevent_req *req;
516 TLDAPRC rc = TLDAP_NO_MEMORY;
518 ev = samba_tevent_context_init(frame);
519 if (ev == NULL) {
520 goto fail;
522 req = tldap_fetch_rootdse_send(frame, ev, ld);
523 if (req == NULL) {
524 goto fail;
526 if (!tevent_req_poll(req, ev)) {
527 rc = TLDAP_OPERATIONS_ERROR;
528 goto fail;
531 rc = tldap_fetch_rootdse_recv(req);
532 fail:
533 TALLOC_FREE(frame);
534 return rc;
537 struct tldap_message *tldap_rootdse(struct tldap_context *ld)
539 return talloc_get_type(tldap_context_getattr(ld, "tldap:rootdse"),
540 struct tldap_message);
543 bool tldap_entry_has_attrvalue(struct tldap_message *msg,
544 const char *attribute,
545 const DATA_BLOB blob)
547 int i, num_values;
548 DATA_BLOB *values;
550 if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
551 return false;
553 for (i=0; i<num_values; i++) {
554 if (data_blob_cmp(&values[i], &blob) == 0) {
555 return true;
558 return false;
561 bool tldap_supports_control(struct tldap_context *ld, const char *oid)
563 struct tldap_message *rootdse = tldap_rootdse(ld);
565 if (rootdse == NULL) {
566 return false;
568 return tldap_entry_has_attrvalue(rootdse, "supportedControl",
569 data_blob_const(oid, strlen(oid)));
572 struct tldap_control *tldap_add_control(TALLOC_CTX *mem_ctx,
573 struct tldap_control *ctrls,
574 int num_ctrls,
575 struct tldap_control *ctrl)
577 struct tldap_control *result;
579 result = talloc_array(mem_ctx, struct tldap_control, num_ctrls+1);
580 if (result == NULL) {
581 return NULL;
583 memcpy(result, ctrls, sizeof(struct tldap_control) * num_ctrls);
584 result[num_ctrls] = *ctrl;
585 return result;
589 * Find a control returned by the server
591 struct tldap_control *tldap_msg_findcontrol(struct tldap_message *msg,
592 const char *oid)
594 struct tldap_control *controls;
595 int i, num_controls;
597 tldap_msg_sctrls(msg, &num_controls, &controls);
599 for (i=0; i<num_controls; i++) {
600 if (strcmp(controls[i].oid, oid) == 0) {
601 return &controls[i];
604 return NULL;
607 struct tldap_search_paged_state {
608 struct tevent_context *ev;
609 struct tldap_context *ld;
610 const char *base;
611 const char *filter;
612 int scope;
613 const char **attrs;
614 int num_attrs;
615 int attrsonly;
616 struct tldap_control *sctrls;
617 int num_sctrls;
618 struct tldap_control *cctrls;
619 int num_cctrls;
620 int timelimit;
621 int sizelimit;
622 int deref;
624 int page_size;
625 struct asn1_data *asn1;
626 DATA_BLOB cookie;
627 struct tldap_message *result;
630 static struct tevent_req *tldap_ship_paged_search(
631 TALLOC_CTX *mem_ctx,
632 struct tldap_search_paged_state *state)
634 struct tldap_control *pgctrl;
635 struct asn1_data *asn1 = NULL;
637 asn1 = asn1_init(state);
638 if (asn1 == NULL) {
639 return NULL;
641 if (!asn1_push_tag(asn1, ASN1_SEQUENCE(0))) goto err;
642 if (!asn1_write_Integer(asn1, state->page_size)) goto err;
643 if (!asn1_write_OctetString(asn1, state->cookie.data, state->cookie.length)) goto err;
644 if (!asn1_pop_tag(asn1)) goto err;
645 state->asn1 = asn1;
647 pgctrl = &state->sctrls[state->num_sctrls-1];
648 pgctrl->oid = TLDAP_CONTROL_PAGEDRESULTS;
649 pgctrl->critical = true;
650 if (!asn1_blob(state->asn1, &pgctrl->value)) {
651 goto err;
653 return tldap_search_send(mem_ctx, state->ev, state->ld, state->base,
654 state->scope, state->filter, state->attrs,
655 state->num_attrs, state->attrsonly,
656 state->sctrls, state->num_sctrls,
657 state->cctrls, state->num_cctrls,
658 state->timelimit, state->sizelimit,
659 state->deref);
661 err:
663 TALLOC_FREE(asn1);
664 return NULL;
667 static void tldap_search_paged_done(struct tevent_req *subreq);
669 struct tevent_req *tldap_search_paged_send(TALLOC_CTX *mem_ctx,
670 struct tevent_context *ev,
671 struct tldap_context *ld,
672 const char *base, int scope,
673 const char *filter,
674 const char **attrs,
675 int num_attrs,
676 int attrsonly,
677 struct tldap_control *sctrls,
678 int num_sctrls,
679 struct tldap_control *cctrls,
680 int num_cctrls,
681 int timelimit,
682 int sizelimit,
683 int deref,
684 int page_size)
686 struct tevent_req *req, *subreq;
687 struct tldap_search_paged_state *state;
688 struct tldap_control empty_control;
690 req = tevent_req_create(mem_ctx, &state,
691 struct tldap_search_paged_state);
692 if (req == NULL) {
693 return NULL;
695 state->ev = ev;
696 state->ld = ld;
697 state->base = base;
698 state->filter = filter;
699 state->scope = scope;
700 state->attrs = attrs;
701 state->num_attrs = num_attrs;
702 state->attrsonly = attrsonly;
703 state->cctrls = cctrls;
704 state->num_cctrls = num_cctrls;
705 state->timelimit = timelimit;
706 state->sizelimit = sizelimit;
707 state->deref = deref;
709 state->page_size = page_size;
710 state->asn1 = NULL;
711 state->cookie = data_blob_null;
713 ZERO_STRUCT(empty_control);
715 state->sctrls = tldap_add_control(state, sctrls, num_sctrls,
716 &empty_control);
717 if (tevent_req_nomem(state->sctrls, req)) {
718 return tevent_req_post(req, ev);
720 state->num_sctrls = num_sctrls+1;
722 subreq = tldap_ship_paged_search(state, state);
723 if (tevent_req_nomem(subreq, req)) {
724 return tevent_req_post(req, ev);
726 tevent_req_set_callback(subreq, tldap_search_paged_done, req);
728 return req;
731 static void tldap_search_paged_done(struct tevent_req *subreq)
733 struct tevent_req *req = tevent_req_callback_data(
734 subreq, struct tevent_req);
735 struct tldap_search_paged_state *state = tevent_req_data(
736 req, struct tldap_search_paged_state);
737 struct asn1_data *asn1 = NULL;
738 struct tldap_control *pgctrl;
739 TLDAPRC rc;
740 int size;
742 rc = tldap_search_recv(subreq, state, &state->result);
743 if (tevent_req_ldap_error(req, rc)) {
744 TALLOC_FREE(subreq);
745 return;
748 TALLOC_FREE(state->asn1);
750 switch (tldap_msg_type(state->result)) {
751 case TLDAP_RES_SEARCH_ENTRY:
752 case TLDAP_RES_SEARCH_REFERENCE:
753 tevent_req_notify_callback(req);
754 return;
755 case TLDAP_RES_SEARCH_RESULT:
756 break;
757 default:
758 TALLOC_FREE(subreq);
759 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
760 return;
763 TALLOC_FREE(subreq);
765 /* We've finished one paged search, fire the next */
767 pgctrl = tldap_msg_findcontrol(state->result,
768 TLDAP_CONTROL_PAGEDRESULTS);
769 if (pgctrl == NULL) {
770 /* RFC2696 requires the server to return the control */
771 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
772 return;
775 TALLOC_FREE(state->cookie.data);
777 asn1 = asn1_init(talloc_tos());
778 if (tevent_req_nomem(asn1, req)) {
779 return;
782 asn1_load_nocopy(asn1, pgctrl->value.data, pgctrl->value.length);
783 if (!asn1_start_tag(asn1, ASN1_SEQUENCE(0))) goto err;
784 if (!asn1_read_Integer(asn1, &size)) goto err;
785 if (!asn1_read_OctetString(asn1, state, &state->cookie)) goto err;
786 if (!asn1_end_tag(asn1)) goto err;
788 TALLOC_FREE(asn1);
790 if (state->cookie.length == 0) {
791 /* We're done, no cookie anymore */
792 tevent_req_done(req);
793 return;
796 TALLOC_FREE(state->result);
798 subreq = tldap_ship_paged_search(state, state);
799 if (tevent_req_nomem(subreq, req)) {
800 return;
802 tevent_req_set_callback(subreq, tldap_search_paged_done, req);
804 err:
806 TALLOC_FREE(asn1);
807 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
810 TLDAPRC tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
811 struct tldap_message **pmsg)
813 struct tldap_search_paged_state *state = tevent_req_data(
814 req, struct tldap_search_paged_state);
815 TLDAPRC rc;
817 if (!tevent_req_is_in_progress(req)
818 && tevent_req_is_ldap_error(req, &rc)) {
819 return rc;
821 if (tevent_req_is_in_progress(req)) {
822 switch (tldap_msg_type(state->result)) {
823 case TLDAP_RES_SEARCH_ENTRY:
824 case TLDAP_RES_SEARCH_REFERENCE:
825 break;
826 default:
827 return TLDAP_PROTOCOL_ERROR;
830 *pmsg = talloc_move(mem_ctx, &state->result);
831 return TLDAP_SUCCESS;