5ff796137f798783efbab00b249f7e2c8bd99916
[bbaumbach/samba-autobuild/.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
24 bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
25                         int *num_values, DATA_BLOB **values)
26 {
27         struct tldap_attribute *attributes;
28         int i, num_attributes;
29
30         if (!tldap_entry_attributes(msg, &num_attributes, &attributes)) {
31                 return false;
32         }
33
34         for (i=0; i<num_attributes; i++) {
35                 if (strequal(attribute, attributes[i].name)) {
36                         break;
37                 }
38         }
39         if (i == num_attributes) {
40                 return false;
41         }
42         *num_values = attributes[i].num_values;
43         *values = attributes[i].values;
44         return true;
45 }
46
47 bool tldap_get_single_valueblob(struct tldap_message *msg,
48                                 const char *attribute, DATA_BLOB *blob)
49 {
50         int num_values;
51         DATA_BLOB *values;
52
53         if (attribute == NULL) {
54                 return NULL;
55         }
56         if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
57                 return NULL;
58         }
59         if (num_values != 1) {
60                 return NULL;
61         }
62         *blob = values[0];
63         return true;
64 }
65
66 char *tldap_talloc_single_attribute(struct tldap_message *msg,
67                                     const char *attribute,
68                                     TALLOC_CTX *mem_ctx)
69 {
70         DATA_BLOB val;
71         char *result;
72         size_t len;
73
74         if (!tldap_get_single_valueblob(msg, attribute, &val)) {
75                 return false;
76         }
77         if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
78                                    val.data, val.length,
79                                    &result, &len, false)) {
80                 return NULL;
81         }
82         return result;
83 }
84
85 bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
86                        struct dom_sid *sid)
87 {
88         DATA_BLOB val;
89
90         if (!tldap_get_single_valueblob(msg, attribute, &val)) {
91                 return false;
92         }
93         return sid_parse((char *)val.data, val.length, sid);
94 }
95
96 bool tldap_pull_guid(struct tldap_message *msg, const char *attribute,
97                      struct GUID *guid)
98 {
99         DATA_BLOB val;
100
101         if (!tldap_get_single_valueblob(msg, attribute, &val)) {
102                 return false;
103         }
104         return NT_STATUS_IS_OK(GUID_from_data_blob(&val, guid));
105 }
106
107 static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
108                                 int num_newvals, DATA_BLOB *newvals)
109 {
110         int num_values = talloc_array_length(mod->values);
111         int i;
112         DATA_BLOB *tmp;
113
114         tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
115                              num_values + num_newvals);
116         if (tmp == NULL) {
117                 return false;
118         }
119         mod->values = tmp;
120
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;
126                 }
127                 mod->values[i+num_values].length = newvals[i].length;
128         }
129         mod->num_values = num_values + num_newvals;
130         return true;
131 }
132
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)
136 {
137         struct tldap_mod new_mod;
138         struct tldap_mod *mods = *pmods;
139         struct tldap_mod *mod = NULL;
140         int i, num_mods;
141
142         if (mods == NULL) {
143                 mods = talloc_array(mem_ctx, struct tldap_mod, 0);
144         }
145         if (mods == NULL) {
146                 return false;
147         }
148
149         num_mods = talloc_array_length(mods);
150
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;
156                 }
157         }
158
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;
164                 }
165                 new_mod.num_values = 0;
166                 new_mod.values = NULL;
167                 mod = &new_mod;
168         }
169
170         if ((num_newvals != 0)
171             && !tldap_add_blob_vals(mods, mod, num_newvals, newvals)) {
172                 return false;
173         }
174
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;
180                 }
181                 mods[num_mods] = *mod;
182         }
183
184         *pmods = mods;
185         return true;
186 }
187
188 bool tldap_add_mod_str(TALLOC_CTX *mem_ctx, struct tldap_mod **pmods,
189                        int mod_op, const char *attrib, const char *str)
190 {
191         DATA_BLOB utf8;
192         bool ret;
193
194         if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
195                                    strlen(str), &utf8.data, &utf8.length,
196                                    false)) {
197                 return false;
198         }
199
200         ret = tldap_add_mod_blobs(mem_ctx, pmods, mod_op, attrib, 1, &utf8);
201         TALLOC_FREE(utf8.data);
202         return ret;
203 }
204
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))
211 {
212         int num_values = 0;
213         DATA_BLOB *values = NULL;
214         DATA_BLOB oldval = data_blob_null;
215
216         if ((existing != NULL)
217             && tldap_entry_values(existing, attrib, &num_values, &values)) {
218
219                 if (num_values > 1) {
220                         /* can't change multivalue attributes atm */
221                         return false;
222                 }
223                 if (num_values == 1) {
224                         oldval = values[0];
225                 }
226         }
227
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;
236         }
237
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 */
246
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;
252                 }
253         }
254
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. */
258
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;
265                 }
266         }
267         *pnum_mods = talloc_array_length(*pmods);
268         return true;
269 }
270
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)
274 {
275         return tldap_make_mod_blob_int(existing, mem_ctx, pnum_mods, pmods,
276                                        attrib, newval, data_blob_cmp);
277 }
278
279 static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
280 {
281         char *s1, *s2;
282         size_t s1len, s2len;
283         int ret;
284
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;
289         }
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;
295         }
296         ret = StrCaseCmp(s1, s2);
297         TALLOC_FREE(s2);
298         TALLOC_FREE(s1);
299         return ret;
300 }
301
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, ...)
305 {
306         va_list ap;
307         char *newval;
308         bool ret;
309         DATA_BLOB blob = data_blob_null;
310
311         va_start(ap, fmt);
312         newval = talloc_vasprintf(talloc_tos(), fmt, ap);
313         va_end(ap);
314
315         if (newval == NULL) {
316                 return false;
317         }
318
319         blob.length = strlen(newval);
320         if (blob.length != 0) {
321                 blob.data = CONST_DISCARD(uint8_t *, newval);
322         }
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;
327 }
328
329 const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld, int rc)
330 {
331         const char *ld_error = NULL;
332         char *res;
333
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;
339 }
340
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)
345 {
346         char *filter;
347         int ret;
348
349         filter = talloc_vasprintf(talloc_tos(), fmt, ap);
350         if (filter == NULL) {
351                 return TLDAP_NO_MEMORY;
352         }
353
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;
361 }
362
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, ...)
367 {
368         va_list ap;
369         int ret;
370
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;
376 }
377
378 bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
379                        uint64_t *presult)
380 {
381         char *str;
382         uint64_t result;
383
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;
388         }
389         result = strtoull(str, NULL, 10);
390         TALLOC_FREE(str);
391         *presult = result;
392         return true;
393 }
394
395 bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
396                        uint32_t *presult)
397 {
398         uint64_t result;
399
400         if (!tldap_pull_uint64(msg, attr, &result)) {
401                 return false;
402         }
403         *presult = (uint32_t)result;
404         return true;
405 }
406
407 struct tldap_fetch_rootdse_state {
408         struct tldap_context *ld;
409         struct tldap_message *rootdse;
410 };
411
412 static void tldap_fetch_rootdse_done(struct tevent_req *subreq);
413
414 struct tevent_req *tldap_fetch_rootdse_send(TALLOC_CTX *mem_ctx,
415                                             struct tevent_context *ev,
416                                             struct tldap_context *ld)
417 {
418         struct tevent_req *req, *subreq;
419         struct tldap_fetch_rootdse_state *state;
420         static const char *attrs[2] = { "*", "+" };
421
422         req = tevent_req_create(mem_ctx, &state,
423                                 struct tldap_fetch_rootdse_state);
424         if (req == NULL) {
425                 return NULL;
426         }
427         state->ld = ld;
428         state->rootdse = NULL;
429
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);
435         }
436         tevent_req_set_callback(subreq, tldap_fetch_rootdse_done, req);
437         return req;
438 }
439
440 static void tldap_fetch_rootdse_done(struct tevent_req *subreq)
441 {
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;
448
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;
454         }
455
456         switch (tldap_msg_type(msg)) {
457         case TLDAP_RES_SEARCH_ENTRY:
458                 if (state->rootdse != NULL) {
459                         goto protocol_error;
460                 }
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;
467                 }
468                 tevent_req_done(req);
469                 break;
470         default:
471                 goto protocol_error;
472         }
473         return;
474
475 protocol_error:
476         tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
477         return;
478 }
479
480 int tldap_fetch_rootdse_recv(struct tevent_req *req)
481 {
482         struct tldap_fetch_rootdse_state *state = tevent_req_data(
483                 req, struct tldap_fetch_rootdse_state);
484         int err;
485         char *dn;
486
487         if (tevent_req_is_ldap_error(req, &err)) {
488                 return err;
489         }
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;
493         }
494         if (!tldap_context_setattr(state->ld, "tldap:rootdse",
495                                    &state->rootdse)) {
496                 return TLDAP_NO_MEMORY;
497         }
498         return 0;
499 }
500
501 int tldap_fetch_rootdse(struct tldap_context *ld)
502 {
503         TALLOC_CTX *frame = talloc_stackframe();
504         struct tevent_context *ev;
505         struct tevent_req *req;
506         int result;
507
508         ev = event_context_init(frame);
509         if (ev == NULL) {
510                 result = TLDAP_NO_MEMORY;
511                 goto fail;
512         }
513
514         req = tldap_fetch_rootdse_send(frame, ev, ld);
515         if (req == NULL) {
516                 result = TLDAP_NO_MEMORY;
517                 goto fail;
518         }
519
520         if (!tevent_req_poll(req, ev)) {
521                 result = TLDAP_OPERATIONS_ERROR;
522                 goto fail;
523         }
524
525         result = tldap_fetch_rootdse_recv(req);
526  fail:
527         TALLOC_FREE(frame);
528         return result;
529 }
530
531 struct tldap_message *tldap_rootdse(struct tldap_context *ld)
532 {
533         return talloc_get_type(tldap_context_getattr(ld, "tldap:rootdse"),
534                                struct tldap_message);
535 }
536
537 bool tldap_entry_has_attrvalue(struct tldap_message *msg,
538                                const char *attribute,
539                                const DATA_BLOB blob)
540 {
541         int i, num_values;
542         DATA_BLOB *values;
543
544         if (!tldap_entry_values(msg, attribute, &num_values, &values)) {
545                 return false;
546         }
547         for (i=0; i<num_values; i++) {
548                 if (data_blob_cmp(&values[i], &blob) == 0) {
549                         return true;
550                 }
551         }
552         return false;
553 }
554
555 bool tldap_supports_control(struct tldap_context *ld, const char *oid)
556 {
557         struct tldap_message *rootdse = tldap_rootdse(ld);
558
559         if (rootdse == NULL) {
560                 return false;
561         }
562         return tldap_entry_has_attrvalue(rootdse, "supportedControl",
563                                          data_blob_const(oid, strlen(oid)));
564 }
565
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)
570 {
571         struct tldap_control *result;
572
573         result = talloc_array(mem_ctx, struct tldap_control, num_ctrls+1);
574         if (result == NULL) {
575                 return NULL;
576         }
577         memcpy(result, ctrls, sizeof(struct tldap_control) * num_ctrls);
578         result[num_ctrls] = *ctrl;
579         return result;
580 }
581
582 /*
583  * Find a control returned by the server
584  */
585 struct tldap_control *tldap_msg_findcontrol(struct tldap_message *msg,
586                                             const char *oid)
587 {
588         struct tldap_control *controls;
589         int i, num_controls;
590
591         tldap_msg_sctrls(msg, &num_controls, &controls);
592
593         for (i=0; i<num_controls; i++) {
594                 if (strcmp(controls[i].oid, oid) == 0) {
595                         return &controls[i];
596                 }
597         }
598         return NULL;
599 }
600
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;
617
618         int page_size;
619         struct asn1_data *asn1;
620         DATA_BLOB cookie;
621         struct tldap_message *result;
622 };
623
624 static struct tevent_req *tldap_ship_paged_search(
625         TALLOC_CTX *mem_ctx,
626         struct tldap_search_paged_state *state)
627 {
628         struct tldap_control *pgctrl;
629         struct asn1_data *asn1;
630
631         asn1 = asn1_init(state);
632         if (asn1 == NULL) {
633                 return NULL;
634         }
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;
642         }
643         state->asn1 = asn1;
644
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;
651         }
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);
659 }
660
661 static void tldap_search_paged_done(struct tevent_req *subreq);
662
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)
679 {
680         struct tevent_req *req, *subreq;
681         struct tldap_search_paged_state *state;
682         struct tldap_control empty_control;
683
684         req = tevent_req_create(mem_ctx, &state,
685                                 struct tldap_search_paged_state);
686         if (req == NULL) {
687                 return NULL;
688         }
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;
702
703         state->page_size = page_size;
704         state->asn1 = NULL;
705         state->cookie = data_blob_null;
706
707         ZERO_STRUCT(empty_control);
708
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);
713         }
714         state->num_sctrls = num_sctrls+1;
715
716         subreq = tldap_ship_paged_search(state, state);
717         if (tevent_req_nomem(subreq, req)) {
718                 return tevent_req_post(req, ev);
719         }
720         tevent_req_set_callback(subreq, tldap_search_paged_done, req);
721
722         return req;
723 }
724
725 static void tldap_search_paged_done(struct tevent_req *subreq)
726 {
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;
734
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;
740         }
741
742         TALLOC_FREE(state->asn1);
743
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;
755         }
756
757         TALLOC_FREE(subreq);
758
759         /* We've finished one paged search, fire the next */
760
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;
767         }
768
769         TALLOC_FREE(state->cookie.data);
770
771         asn1 = asn1_init(talloc_tos());
772         if (asn1 == NULL) {
773                 tevent_req_error(req, TLDAP_NO_MEMORY);
774                 return;
775         }
776
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;
785         }
786         TALLOC_FREE(asn1);
787
788         if (state->cookie.length == 0) {
789                 /* We're done, no cookie anymore */
790                 tevent_req_done(req);
791                 return;
792         }
793
794         TALLOC_FREE(state->result);
795
796         subreq = tldap_ship_paged_search(state, state);
797         if (tevent_req_nomem(subreq, req)) {
798                 return;
799         }
800         tevent_req_set_callback(subreq, tldap_search_paged_done, req);
801 }
802
803 int tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
804                             struct tldap_message **pmsg)
805 {
806         struct tldap_search_paged_state *state = tevent_req_data(
807                 req, struct tldap_search_paged_state);
808         int err;
809
810         if (!tevent_req_is_in_progress(req)
811             && tevent_req_is_ldap_error(req, &err)) {
812                 return err;
813         }
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;
821                 }
822         }
823         *pmsg = talloc_move(mem_ctx, &state->result);
824         return TLDAP_SUCCESS;
825 }