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