3938fca901f917ca79d7793cd3b61bf77331c4ea
[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 #include "../lib/util/asn1.h"
25
26 bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
27                         DATA_BLOB **values, int *num_values)
28 {
29         struct tldap_attribute *attributes;
30         int i, num_attributes;
31
32         if (!tldap_entry_attributes(msg, &attributes, &num_attributes)) {
33                 return false;
34         }
35
36         for (i=0; i<num_attributes; i++) {
37                 if (strequal(attribute, attributes[i].name)) {
38                         break;
39                 }
40         }
41         if (i == num_attributes) {
42                 return false;
43         }
44         *num_values = attributes[i].num_values;
45         *values = attributes[i].values;
46         return true;
47 }
48
49 bool tldap_get_single_valueblob(struct tldap_message *msg,
50                                 const char *attribute, DATA_BLOB *blob)
51 {
52         int num_values;
53         DATA_BLOB *values;
54
55         if (attribute == NULL) {
56                 return NULL;
57         }
58         if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
59                 return NULL;
60         }
61         if (num_values != 1) {
62                 return NULL;
63         }
64         *blob = values[0];
65         return true;
66 }
67
68 char *tldap_talloc_single_attribute(struct tldap_message *msg,
69                                     const char *attribute,
70                                     TALLOC_CTX *mem_ctx)
71 {
72         DATA_BLOB val;
73         char *result;
74         size_t len;
75
76         if (!tldap_get_single_valueblob(msg, attribute, &val)) {
77                 return NULL;
78         }
79         if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
80                                    val.data, val.length,
81                                    &result, &len)) {
82                 return NULL;
83         }
84         return result;
85 }
86
87 bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
88                        struct dom_sid *sid)
89 {
90         DATA_BLOB val;
91
92         if (!tldap_get_single_valueblob(msg, attribute, &val)) {
93                 return false;
94         }
95         return sid_parse(val.data, val.length, sid);
96 }
97
98 bool tldap_pull_guid(struct tldap_message *msg, const char *attribute,
99                      struct GUID *guid)
100 {
101         DATA_BLOB val;
102
103         if (!tldap_get_single_valueblob(msg, attribute, &val)) {
104                 return false;
105         }
106         return NT_STATUS_IS_OK(GUID_from_data_blob(&val, guid));
107 }
108
109 static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
110                                 DATA_BLOB *newvals, int num_newvals)
111 {
112         int num_values = talloc_array_length(mod->values);
113         int i;
114         DATA_BLOB *tmp;
115
116         tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
117                              num_values + num_newvals);
118         if (tmp == NULL) {
119                 return false;
120         }
121         mod->values = tmp;
122
123         for (i=0; i<num_newvals; i++) {
124                 mod->values[i+num_values].data = (uint8_t *)talloc_memdup(
125                         mod->values, newvals[i].data, newvals[i].length);
126                 if (mod->values[i+num_values].data == NULL) {
127                         return false;
128                 }
129                 mod->values[i+num_values].length = newvals[i].length;
130         }
131         mod->num_values = num_values + num_newvals;
132         return true;
133 }
134
135 bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx,
136                          struct tldap_mod **pmods, int *pnum_mods,
137                          int mod_op, const char *attrib,
138                          DATA_BLOB *newvals, int num_newvals)
139 {
140         struct tldap_mod new_mod;
141         struct tldap_mod *mods = *pmods;
142         struct tldap_mod *mod = NULL;
143         int i, num_mods;
144
145         if (mods == NULL) {
146                 mods = talloc_array(mem_ctx, struct tldap_mod, 0);
147         }
148         if (mods == NULL) {
149                 return false;
150         }
151
152         num_mods = *pnum_mods;
153
154         for (i=0; i<num_mods; i++) {
155                 if ((mods[i].mod_op == mod_op)
156                     && strequal(mods[i].attribute, attrib)) {
157                         mod = &mods[i];
158                         break;
159                 }
160         }
161
162         if (mod == NULL) {
163                 new_mod.mod_op = mod_op;
164                 new_mod.attribute = talloc_strdup(mods, attrib);
165                 if (new_mod.attribute == NULL) {
166                         return false;
167                 }
168                 new_mod.num_values = 0;
169                 new_mod.values = NULL;
170                 mod = &new_mod;
171         }
172
173         if ((num_newvals != 0)
174             && !tldap_add_blob_vals(mods, mod, newvals, num_newvals)) {
175                 return false;
176         }
177
178         if ((i == num_mods) && (talloc_array_length(mods) < num_mods + 1)) {
179                 mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
180                                       num_mods+1);
181                 if (mods == NULL) {
182                         return false;
183                 }
184                 mods[num_mods] = *mod;
185         }
186
187         *pmods = mods;
188         *pnum_mods += 1;
189         return true;
190 }
191
192 bool tldap_add_mod_str(TALLOC_CTX *mem_ctx,
193                        struct tldap_mod **pmods, int *pnum_mods,
194                        int mod_op, const char *attrib, const char *str)
195 {
196         DATA_BLOB utf8;
197         bool ret;
198
199         if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
200                                    strlen(str), &utf8.data, &utf8.length)) {
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,("tldap_make_mod_blob_int: 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, ("tldap_make_mod_blob_int: 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, ("tldap_make_mod_blob_int: 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)) {
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)) {
298                 /* can't do much here */
299                 TALLOC_FREE(s1);
300                 return 0;
301         }
302         ret = strcasecmp_m(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 = discard_const_p(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,
336                          TLDAPRC rc)
337 {
338         const char *ld_error = NULL;
339         char *res;
340
341         if (ld != NULL) {
342                 ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
343         }
344         res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s",
345                               (int)TLDAP_RC_V(rc), tldap_rc2string(rc),
346                               ld_error ? ld_error : "unknown");
347         return res;
348 }
349
350 TLDAPRC tldap_search_va(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, va_list ap)
354 {
355         char *filter;
356         TLDAPRC rc;
357
358         filter = talloc_vasprintf(talloc_tos(), fmt, ap);
359         if (filter == NULL) {
360                 return TLDAP_NO_MEMORY;
361         }
362
363         rc = tldap_search(ld, base, scope, filter,
364                           attrs, num_attrs, attrsonly,
365                           NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
366                           0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
367                           mem_ctx, res);
368         TALLOC_FREE(filter);
369         return rc;
370 }
371
372 TLDAPRC tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
373                          const char *attrs[], int num_attrs, int attrsonly,
374                          TALLOC_CTX *mem_ctx, struct tldap_message ***res,
375                          const char *fmt, ...)
376 {
377         va_list ap;
378         TLDAPRC rc;
379
380         va_start(ap, fmt);
381         rc = tldap_search_va(ld, base, scope, attrs, num_attrs, attrsonly,
382                              mem_ctx, res, fmt, ap);
383         va_end(ap);
384         return rc;
385 }
386
387 bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
388                        uint64_t *presult)
389 {
390         char *str;
391         uint64_t result;
392         int error = 0;
393
394         str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
395         if (str == NULL) {
396                 DEBUG(10, ("Could not find attribute %s\n", attr));
397                 return false;
398         }
399
400         result = strtoull_err(str, NULL, 10, &error);
401         if (error != 0) {
402                 DBG_DEBUG("Attribute conversion failed (%s)\n",
403                           strerror(error));
404                 TALLOC_FREE(str);
405                 return false;
406         }
407
408         TALLOC_FREE(str);
409         *presult = result;
410         return true;
411 }
412
413 bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
414                        uint32_t *presult)
415 {
416         uint64_t result;
417
418         if (!tldap_pull_uint64(msg, attr, &result)) {
419                 return false;
420         }
421         *presult = (uint32_t)result;
422         return true;
423 }
424
425 struct tldap_fetch_rootdse_state {
426         struct tldap_context *ld;
427         struct tldap_message *rootdse;
428 };
429
430 static void tldap_fetch_rootdse_done(struct tevent_req *subreq);
431
432 struct tevent_req *tldap_fetch_rootdse_send(TALLOC_CTX *mem_ctx,
433                                             struct tevent_context *ev,
434                                             struct tldap_context *ld)
435 {
436         struct tevent_req *req, *subreq;
437         struct tldap_fetch_rootdse_state *state;
438         static const char *attrs[2] = { "*", "+" };
439
440         req = tevent_req_create(mem_ctx, &state,
441                                 struct tldap_fetch_rootdse_state);
442         if (req == NULL) {
443                 return NULL;
444         }
445         state->ld = ld;
446         state->rootdse = NULL;
447
448         subreq = tldap_search_send(
449                 mem_ctx, ev, ld, "", TLDAP_SCOPE_BASE, "(objectclass=*)",
450                 attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0, 0, 0, 0);
451         if (tevent_req_nomem(subreq, req)) {
452                 return tevent_req_post(req, ev);
453         }
454         tevent_req_set_callback(subreq, tldap_fetch_rootdse_done, req);
455         return req;
456 }
457
458 static void tldap_fetch_rootdse_done(struct tevent_req *subreq)
459 {
460         struct tevent_req *req = tevent_req_callback_data(
461                 subreq, struct tevent_req);
462         struct tldap_fetch_rootdse_state *state = tevent_req_data(
463                 req, struct tldap_fetch_rootdse_state);
464         struct tldap_message *msg;
465         TLDAPRC rc;
466
467         rc = tldap_search_recv(subreq, state, &msg);
468         if (tevent_req_ldap_error(req, rc)) {
469                 return;
470         }
471
472         switch (tldap_msg_type(msg)) {
473         case TLDAP_RES_SEARCH_ENTRY:
474                 if (state->rootdse != NULL) {
475                         goto protocol_error;
476                 }
477                 state->rootdse = msg;
478                 break;
479         case TLDAP_RES_SEARCH_RESULT:
480                 TALLOC_FREE(subreq);
481                 if (state->rootdse == NULL) {
482                         goto protocol_error;
483                 }
484                 tevent_req_done(req);
485                 break;
486         default:
487                 goto protocol_error;
488         }
489         return;
490
491 protocol_error:
492         tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
493         return;
494 }
495
496 TLDAPRC tldap_fetch_rootdse_recv(struct tevent_req *req)
497 {
498         struct tldap_fetch_rootdse_state *state = tevent_req_data(
499                 req, struct tldap_fetch_rootdse_state);
500         TLDAPRC rc;
501         char *dn;
502
503         if (tevent_req_is_ldap_error(req, &rc)) {
504                 return rc;
505         }
506         /* Trigger parsing the dn, just to make sure it's ok */
507         if (!tldap_entry_dn(state->rootdse, &dn)) {
508                 return TLDAP_DECODING_ERROR;
509         }
510         if (!tldap_context_setattr(state->ld, "tldap:rootdse",
511                                    &state->rootdse)) {
512                 return TLDAP_NO_MEMORY;
513         }
514         return TLDAP_SUCCESS;
515 }
516
517 TLDAPRC tldap_fetch_rootdse(struct tldap_context *ld)
518 {
519         TALLOC_CTX *frame = talloc_stackframe();
520         struct tevent_context *ev;
521         struct tevent_req *req;
522         TLDAPRC rc = TLDAP_NO_MEMORY;
523
524         ev = samba_tevent_context_init(frame);
525         if (ev == NULL) {
526                 goto fail;
527         }
528         req = tldap_fetch_rootdse_send(frame, ev, ld);
529         if (req == NULL) {
530                 goto fail;
531         }
532         if (!tevent_req_poll(req, ev)) {
533                 rc = TLDAP_OPERATIONS_ERROR;
534                 goto fail;
535         }
536
537         rc = tldap_fetch_rootdse_recv(req);
538  fail:
539         TALLOC_FREE(frame);
540         return rc;
541 }
542
543 struct tldap_message *tldap_rootdse(struct tldap_context *ld)
544 {
545         return talloc_get_type(tldap_context_getattr(ld, "tldap:rootdse"),
546                                struct tldap_message);
547 }
548
549 bool tldap_entry_has_attrvalue(struct tldap_message *msg,
550                                const char *attribute,
551                                const DATA_BLOB blob)
552 {
553         int i, num_values;
554         DATA_BLOB *values;
555
556         if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
557                 return false;
558         }
559         for (i=0; i<num_values; i++) {
560                 if (data_blob_cmp(&values[i], &blob) == 0) {
561                         return true;
562                 }
563         }
564         return false;
565 }
566
567 bool tldap_supports_control(struct tldap_context *ld, const char *oid)
568 {
569         struct tldap_message *rootdse = tldap_rootdse(ld);
570
571         if (rootdse == NULL) {
572                 return false;
573         }
574         return tldap_entry_has_attrvalue(rootdse, "supportedControl",
575                                          data_blob_const(oid, strlen(oid)));
576 }
577
578 struct tldap_control *tldap_add_control(TALLOC_CTX *mem_ctx,
579                                         struct tldap_control *ctrls,
580                                         int num_ctrls,
581                                         struct tldap_control *ctrl)
582 {
583         struct tldap_control *result;
584
585         result = talloc_array(mem_ctx, struct tldap_control, num_ctrls+1);
586         if (result == NULL) {
587                 return NULL;
588         }
589         memcpy(result, ctrls, sizeof(struct tldap_control) * num_ctrls);
590         result[num_ctrls] = *ctrl;
591         return result;
592 }
593
594 /*
595  * Find a control returned by the server
596  */
597 struct tldap_control *tldap_msg_findcontrol(struct tldap_message *msg,
598                                             const char *oid)
599 {
600         struct tldap_control *controls;
601         int i, num_controls;
602
603         tldap_msg_sctrls(msg, &num_controls, &controls);
604
605         for (i=0; i<num_controls; i++) {
606                 if (strcmp(controls[i].oid, oid) == 0) {
607                         return &controls[i];
608                 }
609         }
610         return NULL;
611 }
612
613 struct tldap_search_paged_state {
614         struct tevent_context *ev;
615         struct tldap_context *ld;
616         const char *base;
617         const char *filter;
618         int scope;
619         const char **attrs;
620         int num_attrs;
621         int attrsonly;
622         struct tldap_control *sctrls;
623         int num_sctrls;
624         struct tldap_control *cctrls;
625         int num_cctrls;
626         int timelimit;
627         int sizelimit;
628         int deref;
629
630         int page_size;
631         struct asn1_data *asn1;
632         DATA_BLOB cookie;
633         struct tldap_message *result;
634 };
635
636 static struct tevent_req *tldap_ship_paged_search(
637         TALLOC_CTX *mem_ctx,
638         struct tldap_search_paged_state *state)
639 {
640         struct tldap_control *pgctrl;
641         struct asn1_data *asn1 = NULL;
642
643         asn1 = asn1_init(state);
644         if (asn1 == NULL) {
645                 return NULL;
646         }
647         if (!asn1_push_tag(asn1, ASN1_SEQUENCE(0))) goto err;
648         if (!asn1_write_Integer(asn1, state->page_size)) goto err;
649         if (!asn1_write_OctetString(asn1, state->cookie.data, state->cookie.length)) goto err;
650         if (!asn1_pop_tag(asn1)) goto err;
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                 goto err;
658         }
659         return tldap_search_send(mem_ctx, state->ev, state->ld, state->base,
660                                  state->scope, state->filter, state->attrs,
661                                  state->num_attrs, state->attrsonly,
662                                  state->sctrls, state->num_sctrls,
663                                  state->cctrls, state->num_cctrls,
664                                  state->timelimit, state->sizelimit,
665                                  state->deref);
666
667   err:
668
669         TALLOC_FREE(asn1);
670         return NULL;
671 }
672
673 static void tldap_search_paged_done(struct tevent_req *subreq);
674
675 struct tevent_req *tldap_search_paged_send(TALLOC_CTX *mem_ctx,
676                                            struct tevent_context *ev,
677                                            struct tldap_context *ld,
678                                            const char *base, int scope,
679                                            const char *filter,
680                                            const char **attrs,
681                                            int num_attrs,
682                                            int attrsonly,
683                                            struct tldap_control *sctrls,
684                                            int num_sctrls,
685                                            struct tldap_control *cctrls,
686                                            int num_cctrls,
687                                            int timelimit,
688                                            int sizelimit,
689                                            int deref,
690                                            int page_size)
691 {
692         struct tevent_req *req, *subreq;
693         struct tldap_search_paged_state *state;
694         struct tldap_control empty_control;
695
696         req = tevent_req_create(mem_ctx, &state,
697                                 struct tldap_search_paged_state);
698         if (req == NULL) {
699                 return NULL;
700         }
701         state->ev = ev;
702         state->ld = ld;
703         state->base = base;
704         state->filter = filter;
705         state->scope = scope;
706         state->attrs = attrs;
707         state->num_attrs = num_attrs;
708         state->attrsonly = attrsonly;
709         state->cctrls = cctrls;
710         state->num_cctrls = num_cctrls;
711         state->timelimit = timelimit;
712         state->sizelimit = sizelimit;
713         state->deref = deref;
714
715         state->page_size = page_size;
716         state->asn1 = NULL;
717         state->cookie = data_blob_null;
718
719         ZERO_STRUCT(empty_control);
720
721         state->sctrls = tldap_add_control(state, sctrls, num_sctrls,
722                                           &empty_control);
723         if (tevent_req_nomem(state->sctrls, req)) {
724                 return tevent_req_post(req, ev);
725         }
726         state->num_sctrls = num_sctrls+1;
727
728         subreq = tldap_ship_paged_search(state, state);
729         if (tevent_req_nomem(subreq, req)) {
730                 return tevent_req_post(req, ev);
731         }
732         tevent_req_set_callback(subreq, tldap_search_paged_done, req);
733
734         return req;
735 }
736
737 static void tldap_search_paged_done(struct tevent_req *subreq)
738 {
739         struct tevent_req *req = tevent_req_callback_data(
740                 subreq, struct tevent_req);
741         struct tldap_search_paged_state *state = tevent_req_data(
742                 req, struct tldap_search_paged_state);
743         struct asn1_data *asn1 = NULL;
744         struct tldap_control *pgctrl;
745         TLDAPRC rc;
746         int size;
747
748         rc = tldap_search_recv(subreq, state, &state->result);
749         if (tevent_req_ldap_error(req, rc)) {
750                 return;
751         }
752
753         TALLOC_FREE(state->asn1);
754
755         switch (tldap_msg_type(state->result)) {
756         case TLDAP_RES_SEARCH_ENTRY:
757         case TLDAP_RES_SEARCH_REFERENCE:
758                 tevent_req_notify_callback(req);
759                 return;
760         case TLDAP_RES_SEARCH_RESULT:
761                 break;
762         default:
763                 TALLOC_FREE(subreq);
764                 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
765                 return;
766         }
767
768         TALLOC_FREE(subreq);
769
770         /* We've finished one paged search, fire the next */
771
772         pgctrl = tldap_msg_findcontrol(state->result,
773                                        TLDAP_CONTROL_PAGEDRESULTS);
774         if (pgctrl == NULL) {
775                 /* RFC2696 requires the server to return the control */
776                 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
777                 return;
778         }
779
780         TALLOC_FREE(state->cookie.data);
781
782         asn1 = asn1_init(talloc_tos());
783         if (tevent_req_nomem(asn1, req)) {
784                 return;
785         }
786
787         asn1_load_nocopy(asn1, pgctrl->value.data, pgctrl->value.length);
788         if (!asn1_start_tag(asn1, ASN1_SEQUENCE(0))) goto err;
789         if (!asn1_read_Integer(asn1, &size)) goto err;
790         if (!asn1_read_OctetString(asn1, state, &state->cookie)) goto err;
791         if (!asn1_end_tag(asn1)) goto err;
792
793         TALLOC_FREE(asn1);
794
795         if (state->cookie.length == 0) {
796                 /* We're done, no cookie anymore */
797                 tevent_req_done(req);
798                 return;
799         }
800
801         TALLOC_FREE(state->result);
802
803         subreq = tldap_ship_paged_search(state, state);
804         if (tevent_req_nomem(subreq, req)) {
805                 return;
806         }
807         tevent_req_set_callback(subreq, tldap_search_paged_done, req);
808
809   err:
810
811         TALLOC_FREE(asn1);
812         tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
813 }
814
815 TLDAPRC tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
816                                 struct tldap_message **pmsg)
817 {
818         struct tldap_search_paged_state *state = tevent_req_data(
819                 req, struct tldap_search_paged_state);
820         TLDAPRC rc;
821
822         if (!tevent_req_is_in_progress(req)
823             && tevent_req_is_ldap_error(req, &rc)) {
824                 return rc;
825         }
826         if (tevent_req_is_in_progress(req)) {
827                 switch (tldap_msg_type(state->result)) {
828                 case TLDAP_RES_SEARCH_ENTRY:
829                 case TLDAP_RES_SEARCH_REFERENCE:
830                         break;
831                 default:
832                         return TLDAP_PROTOCOL_ERROR;
833                 }
834         }
835         *pmsg = talloc_move(mem_ctx, &state->result);
836         return TLDAP_SUCCESS;
837 }