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