s3-rpc_client: make dcerpc_lsa_lookup_names_generic() public.
[kai/samba.git] / source3 / lib / tldap_util.c
1 /*
2    Unix SMB/CIFS implementation.
3    Infrastructure for async ldap client requests
4    Copyright (C) Volker Lendecke 2009
5
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.
10
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.
15
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/>.
18 */
19
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
27 bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
28                         DATA_BLOB **values, int *num_values)
29 {
30         struct tldap_attribute *attributes;
31         int i, num_attributes;
32
33         if (!tldap_entry_attributes(msg, &attributes, &num_attributes)) {
34                 return false;
35         }
36
37         for (i=0; i<num_attributes; i++) {
38                 if (strequal(attribute, attributes[i].name)) {
39                         break;
40                 }
41         }
42         if (i == num_attributes) {
43                 return false;
44         }
45         *num_values = attributes[i].num_values;
46         *values = attributes[i].values;
47         return true;
48 }
49
50 bool tldap_get_single_valueblob(struct tldap_message *msg,
51                                 const char *attribute, DATA_BLOB *blob)
52 {
53         int num_values;
54         DATA_BLOB *values;
55
56         if (attribute == NULL) {
57                 return NULL;
58         }
59         if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
60                 return NULL;
61         }
62         if (num_values != 1) {
63                 return NULL;
64         }
65         *blob = values[0];
66         return true;
67 }
68
69 char *tldap_talloc_single_attribute(struct tldap_message *msg,
70                                     const char *attribute,
71                                     TALLOC_CTX *mem_ctx)
72 {
73         DATA_BLOB val;
74         char *result;
75         size_t len;
76
77         if (!tldap_get_single_valueblob(msg, attribute, &val)) {
78                 return NULL;
79         }
80         if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
81                                    val.data, val.length,
82                                    &result, &len)) {
83                 return NULL;
84         }
85         return result;
86 }
87
88 bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
89                        struct dom_sid *sid)
90 {
91         DATA_BLOB val;
92
93         if (!tldap_get_single_valueblob(msg, attribute, &val)) {
94                 return false;
95         }
96         return sid_parse((char *)val.data, val.length, sid);
97 }
98
99 bool tldap_pull_guid(struct tldap_message *msg, const char *attribute,
100                      struct GUID *guid)
101 {
102         DATA_BLOB val;
103
104         if (!tldap_get_single_valueblob(msg, attribute, &val)) {
105                 return false;
106         }
107         return NT_STATUS_IS_OK(GUID_from_data_blob(&val, guid));
108 }
109
110 static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
111                                 DATA_BLOB *newvals, int num_newvals)
112 {
113         int num_values = talloc_array_length(mod->values);
114         int i;
115         DATA_BLOB *tmp;
116
117         tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
118                              num_values + num_newvals);
119         if (tmp == NULL) {
120                 return false;
121         }
122         mod->values = tmp;
123
124         for (i=0; i<num_newvals; i++) {
125                 mod->values[i+num_values].data = (uint8_t *)talloc_memdup(
126                         mod->values, newvals[i].data, newvals[i].length);
127                 if (mod->values[i+num_values].data == NULL) {
128                         return false;
129                 }
130                 mod->values[i+num_values].length = newvals[i].length;
131         }
132         mod->num_values = num_values + num_newvals;
133         return true;
134 }
135
136 bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx,
137                          struct tldap_mod **pmods, int *pnum_mods,
138                          int mod_op, const char *attrib,
139                          DATA_BLOB *newvals, int num_newvals)
140 {
141         struct tldap_mod new_mod;
142         struct tldap_mod *mods = *pmods;
143         struct tldap_mod *mod = NULL;
144         int i, num_mods;
145
146         if (mods == NULL) {
147                 mods = talloc_array(mem_ctx, struct tldap_mod, 0);
148         }
149         if (mods == NULL) {
150                 return false;
151         }
152
153         num_mods = *pnum_mods;
154
155         for (i=0; i<num_mods; i++) {
156                 if ((mods[i].mod_op == mod_op)
157                     && strequal(mods[i].attribute, attrib)) {
158                         mod = &mods[i];
159                         break;
160                 }
161         }
162
163         if (mod == NULL) {
164                 new_mod.mod_op = mod_op;
165                 new_mod.attribute = talloc_strdup(mods, attrib);
166                 if (new_mod.attribute == NULL) {
167                         return false;
168                 }
169                 new_mod.num_values = 0;
170                 new_mod.values = NULL;
171                 mod = &new_mod;
172         }
173
174         if ((num_newvals != 0)
175             && !tldap_add_blob_vals(mods, mod, newvals, num_newvals)) {
176                 return false;
177         }
178
179         if ((i == num_mods) && (talloc_array_length(mods) < num_mods + 1)) {
180                 mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
181                                       num_mods+1);
182                 if (mods == NULL) {
183                         return false;
184                 }
185                 mods[num_mods] = *mod;
186         }
187
188         *pmods = mods;
189         *pnum_mods += 1;
190         return true;
191 }
192
193 bool tldap_add_mod_str(TALLOC_CTX *mem_ctx,
194                        struct tldap_mod **pmods, int *pnum_mods,
195                        int mod_op, const char *attrib, const char *str)
196 {
197         DATA_BLOB utf8;
198         bool ret;
199
200         if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
201                                    strlen(str), &utf8.data, &utf8.length)) {
202                 return false;
203         }
204
205         ret = tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods, mod_op, attrib,
206                                   &utf8, 1);
207         TALLOC_FREE(utf8.data);
208         return ret;
209 }
210
211 static bool tldap_make_mod_blob_int(struct tldap_message *existing,
212                                     TALLOC_CTX *mem_ctx,
213                                     struct tldap_mod **pmods, int *pnum_mods,
214                                     const char *attrib, DATA_BLOB newval,
215                                     int (*comparison)(const DATA_BLOB *d1,
216                                                       const DATA_BLOB *d2))
217 {
218         int num_values = 0;
219         DATA_BLOB *values = NULL;
220         DATA_BLOB oldval = data_blob_null;
221
222         if ((existing != NULL)
223             && tldap_entry_values(existing, attrib, &values, &num_values)) {
224
225                 if (num_values > 1) {
226                         /* can't change multivalue attributes atm */
227                         return false;
228                 }
229                 if (num_values == 1) {
230                         oldval = values[0];
231                 }
232         }
233
234         if ((oldval.data != NULL) && (newval.data != NULL)
235             && (comparison(&oldval, &newval) == 0)) {
236                 /* Believe it or not, but LDAP will deny a delete and
237                    an add at the same time if the values are the
238                    same... */
239                 DEBUG(10,("tldap_make_mod_blob_int: attribute |%s| not "
240                           "changed.\n", attrib));
241                 return true;
242         }
243
244         if (oldval.data != NULL) {
245                 /* By deleting exactly the value we found in the entry this
246                  * should be race-free in the sense that the LDAP-Server will
247                  * deny the complete operation if somebody changed the
248                  * attribute behind our back. */
249                 /* This will also allow modifying single valued attributes in
250                  * Novell NDS. In NDS you have to first remove attribute and
251                  * then you could add new value */
252
253                 DEBUG(10, ("tldap_make_mod_blob_int: deleting attribute |%s|\n",
254                            attrib));
255                 if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
256                                          TLDAP_MOD_DELETE,
257                                          attrib, &oldval, 1)) {
258                         return false;
259                 }
260         }
261
262         /* Regardless of the real operation (add or modify)
263            we add the new value here. We rely on deleting
264            the old value, should it exist. */
265
266         if (newval.data != NULL) {
267                 DEBUG(10, ("tldap_make_mod_blob_int: adding attribute |%s| value len "
268                            "%d\n", attrib, (int)newval.length));
269                 if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
270                                          TLDAP_MOD_ADD,
271                                          attrib, &newval, 1)) {
272                         return false;
273                 }
274         }
275         return true;
276 }
277
278 bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
279                          struct tldap_mod **pmods, int *pnum_mods,
280                          const char *attrib, DATA_BLOB newval)
281 {
282         return tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
283                                        attrib, newval, data_blob_cmp);
284 }
285
286 static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
287 {
288         char *s1, *s2;
289         size_t s1len, s2len;
290         int ret;
291
292         if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d1->data,
293                                    d1->length, &s1, &s1len)) {
294                 /* can't do much here */
295                 return 0;
296         }
297         if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d2->data,
298                                    d2->length, &s2, &s2len)) {
299                 /* can't do much here */
300                 TALLOC_FREE(s1);
301                 return 0;
302         }
303         ret = strcasecmp_m(s1, s2);
304         TALLOC_FREE(s2);
305         TALLOC_FREE(s1);
306         return ret;
307 }
308
309 bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
310                         struct tldap_mod **pmods, int *pnum_mods,
311                         const char *attrib, const char *fmt, ...)
312 {
313         va_list ap;
314         char *newval;
315         bool ret;
316         DATA_BLOB blob = data_blob_null;
317
318         va_start(ap, fmt);
319         newval = talloc_vasprintf(talloc_tos(), fmt, ap);
320         va_end(ap);
321
322         if (newval == NULL) {
323                 return false;
324         }
325
326         blob.length = strlen(newval);
327         if (blob.length != 0) {
328                 blob.data = discard_const_p(uint8_t, newval);
329         }
330         ret = tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
331                                       attrib, blob, compare_utf8_blobs);
332         TALLOC_FREE(newval);
333         return ret;
334 }
335
336 const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld, int rc)
337 {
338         const char *ld_error = NULL;
339         char *res;
340
341         if (ld != NULL) {
342                 ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
343         }
344         res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s", rc,
345                               tldap_err2string(rc),
346                               ld_error ? ld_error : "unknown");
347         return res;
348 }
349
350 int tldap_search_va(struct tldap_context *ld, const char *base, int scope,
351                     const char *attrs[], int num_attrs, int attrsonly,
352                     TALLOC_CTX *mem_ctx, struct tldap_message ***res,
353                     const char *fmt, va_list ap)
354 {
355         char *filter;
356         int ret;
357
358         filter = talloc_vasprintf(talloc_tos(), fmt, ap);
359         if (filter == NULL) {
360                 return TLDAP_NO_MEMORY;
361         }
362
363         ret = tldap_search(ld, base, scope, filter,
364                            attrs, num_attrs, attrsonly,
365                            NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
366                            0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
367                            mem_ctx, res, NULL);
368         TALLOC_FREE(filter);
369         return ret;
370 }
371
372 int tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
373                      const char *attrs[], int num_attrs, int attrsonly,
374                      TALLOC_CTX *mem_ctx, struct tldap_message ***res,
375                      const char *fmt, ...)
376 {
377         va_list ap;
378         int ret;
379
380         va_start(ap, fmt);
381         ret = tldap_search_va(ld, base, scope, attrs, num_attrs, attrsonly,
382                               mem_ctx, res, fmt, ap);
383         va_end(ap);
384         return ret;
385 }
386
387 bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
388                        uint64_t *presult)
389 {
390         char *str;
391         uint64_t result;
392
393         str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
394         if (str == NULL) {
395                 DEBUG(10, ("Could not find attribute %s\n", attr));
396                 return false;
397         }
398         result = strtoull(str, NULL, 10);
399         TALLOC_FREE(str);
400         *presult = result;
401         return true;
402 }
403
404 bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
405                        uint32_t *presult)
406 {
407         uint64_t result;
408
409         if (!tldap_pull_uint64(msg, attr, &result)) {
410                 return false;
411         }
412         *presult = (uint32_t)result;
413         return true;
414 }
415
416 struct tldap_fetch_rootdse_state {
417         struct tldap_context *ld;
418         struct tldap_message *rootdse;
419 };
420
421 static void tldap_fetch_rootdse_done(struct tevent_req *subreq);
422
423 struct tevent_req *tldap_fetch_rootdse_send(TALLOC_CTX *mem_ctx,
424                                             struct tevent_context *ev,
425                                             struct tldap_context *ld)
426 {
427         struct tevent_req *req, *subreq;
428         struct tldap_fetch_rootdse_state *state;
429         static const char *attrs[2] = { "*", "+" };
430
431         req = tevent_req_create(mem_ctx, &state,
432                                 struct tldap_fetch_rootdse_state);
433         if (req == NULL) {
434                 return NULL;
435         }
436         state->ld = ld;
437         state->rootdse = NULL;
438
439         subreq = tldap_search_send(
440                 mem_ctx, ev, ld, "", TLDAP_SCOPE_BASE, "(objectclass=*)",
441                 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0, 0, 0, 0);
442         if (tevent_req_nomem(subreq, req)) {
443                 return tevent_req_post(req, ev);
444         }
445         tevent_req_set_callback(subreq, tldap_fetch_rootdse_done, req);
446         return req;
447 }
448
449 static void tldap_fetch_rootdse_done(struct tevent_req *subreq)
450 {
451         struct tevent_req *req = tevent_req_callback_data(
452                 subreq, struct tevent_req);
453         struct tldap_fetch_rootdse_state *state = tevent_req_data(
454                 req, struct tldap_fetch_rootdse_state);
455         struct tldap_message *msg;
456         int rc;
457
458         rc = tldap_search_recv(subreq, state, &msg);
459         if (rc != TLDAP_SUCCESS) {
460                 TALLOC_FREE(subreq);
461                 tevent_req_error(req, rc);
462                 return;
463         }
464
465         switch (tldap_msg_type(msg)) {
466         case TLDAP_RES_SEARCH_ENTRY:
467                 if (state->rootdse != NULL) {
468                         goto protocol_error;
469                 }
470                 state->rootdse = msg;
471                 break;
472         case TLDAP_RES_SEARCH_RESULT:
473                 TALLOC_FREE(subreq);
474                 if (state->rootdse == NULL) {
475                         goto protocol_error;
476                 }
477                 tevent_req_done(req);
478                 break;
479         default:
480                 goto protocol_error;
481         }
482         return;
483
484 protocol_error:
485         tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
486         return;
487 }
488
489 int tldap_fetch_rootdse_recv(struct tevent_req *req)
490 {
491         struct tldap_fetch_rootdse_state *state = tevent_req_data(
492                 req, struct tldap_fetch_rootdse_state);
493         int err;
494         char *dn;
495
496         if (tevent_req_is_ldap_error(req, &err)) {
497                 return err;
498         }
499         /* Trigger parsing the dn, just to make sure it's ok */
500         if (!tldap_entry_dn(state->rootdse, &dn)) {
501                 return TLDAP_DECODING_ERROR;
502         }
503         if (!tldap_context_setattr(state->ld, "tldap:rootdse",
504                                    &state->rootdse)) {
505                 return TLDAP_NO_MEMORY;
506         }
507         return 0;
508 }
509
510 int tldap_fetch_rootdse(struct tldap_context *ld)
511 {
512         TALLOC_CTX *frame = talloc_stackframe();
513         struct tevent_context *ev;
514         struct tevent_req *req;
515         int result;
516
517         ev = event_context_init(frame);
518         if (ev == NULL) {
519                 result = TLDAP_NO_MEMORY;
520                 goto fail;
521         }
522
523         req = tldap_fetch_rootdse_send(frame, ev, ld);
524         if (req == NULL) {
525                 result = TLDAP_NO_MEMORY;
526                 goto fail;
527         }
528
529         if (!tevent_req_poll(req, ev)) {
530                 result = TLDAP_OPERATIONS_ERROR;
531                 goto fail;
532         }
533
534         result = tldap_fetch_rootdse_recv(req);
535  fail:
536         TALLOC_FREE(frame);
537         return result;
538 }
539
540 struct tldap_message *tldap_rootdse(struct tldap_context *ld)
541 {
542         return talloc_get_type(tldap_context_getattr(ld, "tldap:rootdse"),
543                                struct tldap_message);
544 }
545
546 bool tldap_entry_has_attrvalue(struct tldap_message *msg,
547                                const char *attribute,
548                                const DATA_BLOB blob)
549 {
550         int i, num_values;
551         DATA_BLOB *values;
552
553         if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
554                 return false;
555         }
556         for (i=0; i<num_values; i++) {
557                 if (data_blob_cmp(&values[i], &blob) == 0) {
558                         return true;
559                 }
560         }
561         return false;
562 }
563
564 bool tldap_supports_control(struct tldap_context *ld, const char *oid)
565 {
566         struct tldap_message *rootdse = tldap_rootdse(ld);
567
568         if (rootdse == NULL) {
569                 return false;
570         }
571         return tldap_entry_has_attrvalue(rootdse, "supportedControl",
572                                          data_blob_const(oid, strlen(oid)));
573 }
574
575 struct tldap_control *tldap_add_control(TALLOC_CTX *mem_ctx,
576                                         struct tldap_control *ctrls,
577                                         int num_ctrls,
578                                         struct tldap_control *ctrl)
579 {
580         struct tldap_control *result;
581
582         result = talloc_array(mem_ctx, struct tldap_control, num_ctrls+1);
583         if (result == NULL) {
584                 return NULL;
585         }
586         memcpy(result, ctrls, sizeof(struct tldap_control) * num_ctrls);
587         result[num_ctrls] = *ctrl;
588         return result;
589 }
590
591 /*
592  * Find a control returned by the server
593  */
594 struct tldap_control *tldap_msg_findcontrol(struct tldap_message *msg,
595                                             const char *oid)
596 {
597         struct tldap_control *controls;
598         int i, num_controls;
599
600         tldap_msg_sctrls(msg, &num_controls, &controls);
601
602         for (i=0; i<num_controls; i++) {
603                 if (strcmp(controls[i].oid, oid) == 0) {
604                         return &controls[i];
605                 }
606         }
607         return NULL;
608 }
609
610 struct tldap_search_paged_state {
611         struct tevent_context *ev;
612         struct tldap_context *ld;
613         const char *base;
614         const char *filter;
615         int scope;
616         const char **attrs;
617         int num_attrs;
618         int attrsonly;
619         struct tldap_control *sctrls;
620         int num_sctrls;
621         struct tldap_control *cctrls;
622         int num_cctrls;
623         int timelimit;
624         int sizelimit;
625         int deref;
626
627         int page_size;
628         struct asn1_data *asn1;
629         DATA_BLOB cookie;
630         struct tldap_message *result;
631 };
632
633 static struct tevent_req *tldap_ship_paged_search(
634         TALLOC_CTX *mem_ctx,
635         struct tldap_search_paged_state *state)
636 {
637         struct tldap_control *pgctrl;
638         struct asn1_data *asn1;
639
640         asn1 = asn1_init(state);
641         if (asn1 == NULL) {
642                 return NULL;
643         }
644         asn1_push_tag(asn1, ASN1_SEQUENCE(0));
645         asn1_write_Integer(asn1, state->page_size);
646         asn1_write_OctetString(asn1, state->cookie.data, state->cookie.length);
647         asn1_pop_tag(asn1);
648         if (asn1->has_error) {
649                 TALLOC_FREE(asn1);
650                 return NULL;
651         }
652         state->asn1 = asn1;
653
654         pgctrl = &state->sctrls[state->num_sctrls-1];
655         pgctrl->oid = TLDAP_CONTROL_PAGEDRESULTS;
656         pgctrl->critical = true;
657         if (!asn1_blob(state->asn1, &pgctrl->value)) {
658                 TALLOC_FREE(asn1);
659                 return NULL;
660         }
661         return tldap_search_send(mem_ctx, state->ev, state->ld, state->base,
662                                  state->scope, state->filter, state->attrs,
663                                  state->num_attrs, state->attrsonly,
664                                  state->sctrls, state->num_sctrls,
665                                  state->cctrls, state->num_cctrls,
666                                  state->timelimit, state->sizelimit,
667                                  state->deref);
668 }
669
670 static void tldap_search_paged_done(struct tevent_req *subreq);
671
672 struct tevent_req *tldap_search_paged_send(TALLOC_CTX *mem_ctx,
673                                            struct tevent_context *ev,
674                                            struct tldap_context *ld,
675                                            const char *base, int scope,
676                                            const char *filter,
677                                            const char **attrs,
678                                            int num_attrs,
679                                            int attrsonly,
680                                            struct tldap_control *sctrls,
681                                            int num_sctrls,
682                                            struct tldap_control *cctrls,
683                                            int num_cctrls,
684                                            int timelimit,
685                                            int sizelimit,
686                                            int deref,
687                                            int page_size)
688 {
689         struct tevent_req *req, *subreq;
690         struct tldap_search_paged_state *state;
691         struct tldap_control empty_control;
692
693         req = tevent_req_create(mem_ctx, &state,
694                                 struct tldap_search_paged_state);
695         if (req == NULL) {
696                 return NULL;
697         }
698         state->ev = ev;
699         state->ld = ld;
700         state->base = base;
701         state->filter = filter;
702         state->scope = scope;
703         state->attrs = attrs;
704         state->num_attrs = num_attrs;
705         state->attrsonly = attrsonly;
706         state->cctrls = cctrls;
707         state->num_cctrls = num_cctrls;
708         state->timelimit = timelimit;
709         state->sizelimit = sizelimit;
710         state->deref = deref;
711
712         state->page_size = page_size;
713         state->asn1 = NULL;
714         state->cookie = data_blob_null;
715
716         ZERO_STRUCT(empty_control);
717
718         state->sctrls = tldap_add_control(state, sctrls, num_sctrls,
719                                           &empty_control);
720         if (tevent_req_nomem(state->sctrls, req)) {
721                 return tevent_req_post(req, ev);
722         }
723         state->num_sctrls = num_sctrls+1;
724
725         subreq = tldap_ship_paged_search(state, state);
726         if (tevent_req_nomem(subreq, req)) {
727                 return tevent_req_post(req, ev);
728         }
729         tevent_req_set_callback(subreq, tldap_search_paged_done, req);
730
731         return req;
732 }
733
734 static void tldap_search_paged_done(struct tevent_req *subreq)
735 {
736         struct tevent_req *req = tevent_req_callback_data(
737                 subreq, struct tevent_req);
738         struct tldap_search_paged_state *state = tevent_req_data(
739                 req, struct tldap_search_paged_state);
740         struct asn1_data *asn1;
741         struct tldap_control *pgctrl;
742         int rc, size;
743
744         rc = tldap_search_recv(subreq, state, &state->result);
745         if (rc != TLDAP_SUCCESS) {
746                 TALLOC_FREE(subreq);
747                 tevent_req_error(req, rc);
748                 return;
749         }
750
751         TALLOC_FREE(state->asn1);
752
753         switch (tldap_msg_type(state->result)) {
754         case TLDAP_RES_SEARCH_ENTRY:
755         case TLDAP_RES_SEARCH_REFERENCE:
756                 tevent_req_notify_callback(req);
757                 return;
758         case TLDAP_RES_SEARCH_RESULT:
759                 break;
760         default:
761                 TALLOC_FREE(subreq);
762                 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
763                 return;
764         }
765
766         TALLOC_FREE(subreq);
767
768         /* We've finished one paged search, fire the next */
769
770         pgctrl = tldap_msg_findcontrol(state->result,
771                                        TLDAP_CONTROL_PAGEDRESULTS);
772         if (pgctrl == NULL) {
773                 /* RFC2696 requires the server to return the control */
774                 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
775                 return;
776         }
777
778         TALLOC_FREE(state->cookie.data);
779
780         asn1 = asn1_init(talloc_tos());
781         if (asn1 == NULL) {
782                 tevent_req_error(req, TLDAP_NO_MEMORY);
783                 return;
784         }
785
786         asn1_load_nocopy(asn1, pgctrl->value.data, pgctrl->value.length);
787         asn1_start_tag(asn1, ASN1_SEQUENCE(0));
788         asn1_read_Integer(asn1, &size);
789         asn1_read_OctetString(asn1, state, &state->cookie);
790         asn1_end_tag(asn1);
791         if (asn1->has_error) {
792                 tevent_req_error(req, TLDAP_DECODING_ERROR);
793                 return;
794         }
795         TALLOC_FREE(asn1);
796
797         if (state->cookie.length == 0) {
798                 /* We're done, no cookie anymore */
799                 tevent_req_done(req);
800                 return;
801         }
802
803         TALLOC_FREE(state->result);
804
805         subreq = tldap_ship_paged_search(state, state);
806         if (tevent_req_nomem(subreq, req)) {
807                 return;
808         }
809         tevent_req_set_callback(subreq, tldap_search_paged_done, req);
810 }
811
812 int tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
813                             struct tldap_message **pmsg)
814 {
815         struct tldap_search_paged_state *state = tevent_req_data(
816                 req, struct tldap_search_paged_state);
817         int err;
818
819         if (!tevent_req_is_in_progress(req)
820             && tevent_req_is_ldap_error(req, &err)) {
821                 return err;
822         }
823         if (tevent_req_is_in_progress(req)) {
824                 switch (tldap_msg_type(state->result)) {
825                 case TLDAP_RES_SEARCH_ENTRY:
826                 case TLDAP_RES_SEARCH_REFERENCE:
827                         break;
828                 default:
829                         return TLDAP_PROTOCOL_ERROR;
830                 }
831         }
832         *pmsg = talloc_move(mem_ctx, &state->result);
833         return TLDAP_SUCCESS;
834 }