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