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