cbd9648007026e3e6fc15cfedd0817e07cbea0e2
[metze/samba/wip.git] / source3 / lib / tldap.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 tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
23 {
24         enum tevent_req_state state;
25         uint64_t err;
26
27         if (!tevent_req_is_error(req, &state, &err)) {
28                 return false;
29         }
30         switch (state) {
31         case TEVENT_REQ_TIMED_OUT:
32                 *perr = TLDAP_TIMEOUT;
33                 break;
34         case TEVENT_REQ_NO_MEMORY:
35                 *perr = TLDAP_NO_MEMORY;
36                 break;
37         case TEVENT_REQ_USER_ERROR:
38                 *perr = err;
39                 break;
40         default:
41                 *perr = TLDAP_OPERATIONS_ERROR;
42                 break;
43         }
44         return true;
45 }
46
47 struct tldap_ctx_attribute {
48         char *name;
49         void *ptr;
50 };
51
52 struct tldap_context {
53         int ld_version;
54         int ld_deref;
55         int ld_sizelimit;
56         int ld_timelimit;
57         struct tstream_context *conn;
58         int msgid;
59         struct tevent_queue *outgoing;
60         struct tevent_req **pending;
61
62         /* For the sync wrappers we need something like get_last_error... */
63         struct tldap_message *last_msg;
64
65         /* debug */
66         void (*log_fn)(void *context, enum tldap_debug_level level,
67                        const char *fmt, va_list ap);
68         void *log_private;
69
70         struct tldap_ctx_attribute *ctx_attrs;
71 };
72
73 struct tldap_message {
74         struct asn1_data *data;
75         uint8_t *inbuf;
76         int type;
77         int id;
78
79         /* RESULT_ENTRY */
80         char *dn;
81         struct tldap_attribute *attribs;
82
83         /* Error data sent by the server */
84         int lderr;
85         char *res_matcheddn;
86         char *res_diagnosticmessage;
87         char *res_referral;
88         struct tldap_control *res_sctrls;
89
90         /* Controls sent by the server */
91         struct tldap_control *ctrls;
92 };
93
94 void tldap_set_debug(struct tldap_context *ld,
95                      void (*log_fn)(void *log_private,
96                                     enum tldap_debug_level level,
97                                     const char *fmt,
98                                     va_list ap) PRINTF_ATTRIBUTE(3,0),
99                      void *log_private)
100 {
101         ld->log_fn = log_fn;
102         ld->log_private = log_private;
103 }
104
105 static void tldap_debug(struct tldap_context *ld,
106                          enum tldap_debug_level level,
107                          const char *fmt, ...)
108 {
109         va_list ap;
110         if (!ld) {
111                 return;
112         }
113         if (ld->log_fn == NULL) {
114                 return;
115         }
116         va_start(ap, fmt);
117         ld->log_fn(ld->log_private, level, fmt, ap);
118         va_end(ap);
119 }
120
121 static int tldap_next_msgid(struct tldap_context *ld)
122 {
123         int result;
124
125         result = ld->msgid++;
126         if (ld->msgid == 2147483647) {
127                 ld->msgid = 1;
128         }
129         return result;
130 }
131
132 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
133 {
134         struct tldap_context *ctx;
135         int ret;
136
137         ctx = talloc_zero(mem_ctx, struct tldap_context);
138         if (ctx == NULL) {
139                 return NULL;
140         }
141         ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
142         if (ret == -1) {
143                 TALLOC_FREE(ctx);
144                 return NULL;
145         }
146         ctx->msgid = 1;
147         ctx->ld_version = 3;
148         ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
149         if (ctx->outgoing == NULL) {
150                 TALLOC_FREE(ctx);
151                 return NULL;
152         }
153         return ctx;
154 }
155
156 static struct tldap_ctx_attribute *tldap_context_findattr(
157         struct tldap_context *ld, const char *name)
158 {
159         int i, num_attrs;
160
161         num_attrs = talloc_array_length(ld->ctx_attrs);
162
163         for (i=0; i<num_attrs; i++) {
164                 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
165                         return &ld->ctx_attrs[i];
166                 }
167         }
168         return NULL;
169 }
170
171 bool tldap_context_setattr(struct tldap_context *ld,
172                            const char *name, const void *_pptr)
173 {
174         struct tldap_ctx_attribute *tmp, *attr;
175         char *tmpname;
176         int num_attrs;
177         void **pptr = (void **)_pptr;
178
179         attr = tldap_context_findattr(ld, name);
180         if (attr != NULL) {
181                 /*
182                  * We don't actually delete attrs, we don't expect tons of
183                  * attributes being shuffled around.
184                  */
185                 TALLOC_FREE(attr->ptr);
186                 if (*pptr != NULL) {
187                         attr->ptr = talloc_move(ld->ctx_attrs, pptr);
188                         *pptr = NULL;
189                 }
190                 return true;
191         }
192
193         tmpname = talloc_strdup(ld, name);
194         if (tmpname == NULL) {
195                 return false;
196         }
197
198         num_attrs = talloc_array_length(ld->ctx_attrs);
199
200         tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
201                              num_attrs+1);
202         if (tmp == NULL) {
203                 TALLOC_FREE(tmpname);
204                 return false;
205         }
206         tmp[num_attrs].name = talloc_move(tmp, &tmpname);
207         if (*pptr != NULL) {
208                 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
209         } else {
210                 tmp[num_attrs].ptr = NULL;
211         }
212         *pptr = NULL;
213         ld->ctx_attrs = tmp;
214         return true;
215 }
216
217 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
218 {
219         struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
220
221         if (attr == NULL) {
222                 return NULL;
223         }
224         return attr->ptr;
225 }
226
227 struct read_ldap_state {
228         uint8_t *buf;
229         bool done;
230 };
231
232 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
233 static void read_ldap_done(struct tevent_req *subreq);
234
235 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
236                                          struct tevent_context *ev,
237                                          struct tstream_context *conn)
238 {
239         struct tevent_req *req, *subreq;
240         struct read_ldap_state *state;
241
242         req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
243         if (req == NULL) {
244                 return NULL;
245         }
246         state->done = false;
247
248         subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
249                                           state);
250         if (tevent_req_nomem(subreq, req)) {
251                 return tevent_req_post(req, ev);
252         }
253         tevent_req_set_callback(subreq, read_ldap_done, req);
254         return req;
255 }
256
257 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
258 {
259         struct read_ldap_state *state = talloc_get_type_abort(
260                 private_data, struct read_ldap_state);
261         size_t len;
262         int i, lensize;
263
264         if (state->done) {
265                 /* We've been here, we're done */
266                 return 0;
267         }
268
269         /*
270          * From ldap.h: LDAP_TAG_MESSAGE is 0x30
271          */
272         if (buf[0] != 0x30) {
273                 return -1;
274         }
275
276         len = buf[1];
277         if ((len & 0x80) == 0) {
278                 state->done = true;
279                 return len;
280         }
281
282         lensize = (len & 0x7f);
283         len = 0;
284
285         if (buflen == 2) {
286                 /* Please get us the full length */
287                 return lensize;
288         }
289         if (buflen > 2 + lensize) {
290                 state->done = true;
291                 return 0;
292         }
293         if (buflen != 2 + lensize) {
294                 return -1;
295         }
296
297         for (i=0; i<lensize; i++) {
298                 len = (len << 8) | buf[2+i];
299         }
300         return len;
301 }
302
303 static void read_ldap_done(struct tevent_req *subreq)
304 {
305         struct tevent_req *req = tevent_req_callback_data(
306                 subreq, struct tevent_req);
307         struct read_ldap_state *state = tevent_req_data(
308                 req, struct read_ldap_state);
309         ssize_t nread;
310         int err;
311
312         nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
313         TALLOC_FREE(subreq);
314         if (nread == -1) {
315                 tevent_req_error(req, err);
316                 return;
317         }
318         tevent_req_done(req);
319 }
320
321 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
322                               uint8_t **pbuf, int *perrno)
323 {
324         struct read_ldap_state *state = tevent_req_data(
325                 req, struct read_ldap_state);
326
327         if (tevent_req_is_unix_error(req, perrno)) {
328                 return -1;
329         }
330         *pbuf = talloc_move(mem_ctx, &state->buf);
331         return talloc_get_size(*pbuf);
332 }
333
334 struct tldap_msg_state {
335         struct tldap_context *ld;
336         struct tevent_context *ev;
337         int id;
338         struct iovec iov;
339
340         struct asn1_data *data;
341         uint8_t *inbuf;
342 };
343
344 static void tldap_push_controls(struct asn1_data *data,
345                                 struct tldap_control *sctrls,
346                                 int num_sctrls)
347 {
348         int i;
349
350         if ((sctrls == NULL) || (num_sctrls == 0)) {
351                 return;
352         }
353
354         asn1_push_tag(data, ASN1_CONTEXT(0));
355
356         for (i=0; i<num_sctrls; i++) {
357                 struct tldap_control *c = &sctrls[i];
358                 asn1_push_tag(data, ASN1_SEQUENCE(0));
359                 asn1_write_OctetString(data, c->oid, strlen(c->oid));
360                 if (c->critical) {
361                         asn1_write_BOOLEAN(data, true);
362                 }
363                 if (c->value.data != NULL) {
364                         asn1_write_OctetString(data, c->value.data,
365                                                c->value.length);
366                 }
367                 asn1_pop_tag(data); /* ASN1_SEQUENCE(0) */
368         }
369
370         asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
371 }
372
373 static void tldap_msg_sent(struct tevent_req *subreq);
374 static void tldap_msg_received(struct tevent_req *subreq);
375
376 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
377                                          struct tevent_context *ev,
378                                          struct tldap_context *ld,
379                                          int id, struct asn1_data *data,
380                                          struct tldap_control *sctrls,
381                                          int num_sctrls)
382 {
383         struct tevent_req *req, *subreq;
384         struct tldap_msg_state *state;
385         DATA_BLOB blob;
386
387         tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
388                     id);
389
390         req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
391         if (req == NULL) {
392                 return NULL;
393         }
394         state->ld = ld;
395         state->ev = ev;
396         state->id = id;
397
398         tldap_push_controls(data, sctrls, num_sctrls);
399
400         asn1_pop_tag(data);
401
402         if (!asn1_blob(data, &blob)) {
403                 tevent_req_error(req, TLDAP_ENCODING_ERROR);
404                 return tevent_req_post(req, ev);
405         }
406
407         state->iov.iov_base = blob.data;
408         state->iov.iov_len = blob.length;
409
410         subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
411                                            &state->iov, 1);
412         if (tevent_req_nomem(subreq, req)) {
413                 return tevent_req_post(req, ev);
414         }
415         tevent_req_set_callback(subreq, tldap_msg_sent, req);
416         return req;
417 }
418
419 static void tldap_msg_unset_pending(struct tevent_req *req)
420 {
421         struct tldap_msg_state *state = tevent_req_data(
422                 req, struct tldap_msg_state);
423         struct tldap_context *ld = state->ld;
424         int num_pending = talloc_array_length(ld->pending);
425         int i;
426
427         if (num_pending == 1) {
428                 TALLOC_FREE(ld->pending);
429                 return;
430         }
431
432         for (i=0; i<num_pending; i++) {
433                 if (req == ld->pending[i]) {
434                         break;
435                 }
436         }
437         if (i == num_pending) {
438                 /*
439                  * Something's seriously broken. Just returning here is the
440                  * right thing nevertheless, the point of this routine is to
441                  * remove ourselves from cli->pending.
442                  */
443                 return;
444         }
445
446         /*
447          * Remove ourselves from the cli->pending array
448          */
449         if (num_pending > 1) {
450                 ld->pending[i] = ld->pending[num_pending-1];
451         }
452
453         /*
454          * No NULL check here, we're shrinking by sizeof(void *), and
455          * talloc_realloc just adjusts the size for this.
456          */
457         ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
458                                      num_pending - 1);
459         return;
460 }
461
462 static int tldap_msg_destructor(struct tevent_req *req)
463 {
464         tldap_msg_unset_pending(req);
465         return 0;
466 }
467
468 static bool tldap_msg_set_pending(struct tevent_req *req)
469 {
470         struct tldap_msg_state *state = tevent_req_data(
471                 req, struct tldap_msg_state);
472         struct tldap_context *ld;
473         struct tevent_req **pending;
474         int num_pending;
475         struct tevent_req *subreq;
476
477         ld = state->ld;
478         num_pending = talloc_array_length(ld->pending);
479
480         pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
481                                  num_pending+1);
482         if (pending == NULL) {
483                 return false;
484         }
485         pending[num_pending] = req;
486         ld->pending = pending;
487         talloc_set_destructor(req, tldap_msg_destructor);
488
489         if (num_pending > 0) {
490                 return true;
491         }
492
493         /*
494          * We're the first ones, add the read_ldap request that waits for the
495          * answer from the server
496          */
497         subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
498         if (subreq == NULL) {
499                 tldap_msg_unset_pending(req);
500                 return false;
501         }
502         tevent_req_set_callback(subreq, tldap_msg_received, ld);
503         return true;
504 }
505
506 static void tldap_msg_sent(struct tevent_req *subreq)
507 {
508         struct tevent_req *req = tevent_req_callback_data(
509                 subreq, struct tevent_req);
510         ssize_t nwritten;
511         int err;
512
513         nwritten = tstream_writev_queue_recv(subreq, &err);
514         TALLOC_FREE(subreq);
515         if (nwritten == -1) {
516                 tevent_req_error(req, TLDAP_SERVER_DOWN);
517                 return;
518         }
519
520         if (!tldap_msg_set_pending(req)) {
521                 tevent_req_nomem(NULL, req);
522                 return;
523         }
524 }
525
526 static int tldap_msg_msgid(struct tevent_req *req)
527 {
528         struct tldap_msg_state *state = tevent_req_data(
529                 req, struct tldap_msg_state);
530
531         return state->id;
532 }
533
534 static void tldap_msg_received(struct tevent_req *subreq)
535 {
536         struct tldap_context *ld = tevent_req_callback_data(
537                 subreq, struct tldap_context);
538         struct tevent_req *req;
539         struct tldap_msg_state *state;
540         struct tevent_context *ev;
541         struct asn1_data *data;
542         uint8_t *inbuf;
543         ssize_t received;
544         size_t num_pending;
545         int i, err, status;
546         int id;
547         uint8_t type;
548         bool ok;
549
550         received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
551         TALLOC_FREE(subreq);
552         if (received == -1) {
553                 status = TLDAP_SERVER_DOWN;
554                 goto fail;
555         }
556
557         data = asn1_init(talloc_tos());
558         if (data == NULL) {
559                 status = TLDAP_NO_MEMORY;
560                 goto fail;
561         }
562         asn1_load_nocopy(data, inbuf, received);
563
564         ok = true;
565         ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
566         ok &= asn1_read_Integer(data, &id);
567         ok &= asn1_peek_uint8(data, &type);
568
569         if (!ok) {
570                 status = TLDAP_PROTOCOL_ERROR;
571                 goto fail;
572         }
573
574         tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
575                     "type %d\n", id, (int)type);
576
577         num_pending = talloc_array_length(ld->pending);
578
579         for (i=0; i<num_pending; i++) {
580                 if (id == tldap_msg_msgid(ld->pending[i])) {
581                         break;
582                 }
583         }
584         if (i == num_pending) {
585                 /* Dump unexpected reply */
586                 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
587                             "No request pending for msg %d\n", id);
588                 TALLOC_FREE(data);
589                 TALLOC_FREE(inbuf);
590                 goto done;
591         }
592
593         req = ld->pending[i];
594         state = tevent_req_data(req, struct tldap_msg_state);
595
596         state->inbuf = talloc_move(state, &inbuf);
597         state->data = talloc_move(state, &data);
598
599         ev = state->ev;
600
601         talloc_set_destructor(req, NULL);
602         tldap_msg_unset_pending(req);
603         num_pending = talloc_array_length(ld->pending);
604
605         tevent_req_done(req);
606
607  done:
608         if (num_pending == 0) {
609                 return;
610         }
611         if (talloc_array_length(ld->pending) > num_pending) {
612                 /*
613                  * The callback functions called from tevent_req_done() above
614                  * have put something on the pending queue. We don't have to
615                  * trigger the read_ldap_send(), tldap_msg_set_pending() has
616                  * done it for us already.
617                  */
618                 return;
619         }
620
621         state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
622         subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
623         if (subreq == NULL) {
624                 status = TLDAP_NO_MEMORY;
625                 goto fail;
626         }
627         tevent_req_set_callback(subreq, tldap_msg_received, ld);
628         return;
629
630  fail:
631         while (talloc_array_length(ld->pending) > 0) {
632                 req = ld->pending[0];
633                 talloc_set_destructor(req, NULL);
634                 tldap_msg_destructor(req);
635                 tevent_req_error(req, status);
636         }
637 }
638
639 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
640                           struct tldap_message **pmsg)
641 {
642         struct tldap_msg_state *state = tevent_req_data(
643                 req, struct tldap_msg_state);
644         struct tldap_message *msg;
645         int err;
646         uint8_t msgtype;
647
648         if (tevent_req_is_ldap_error(req, &err)) {
649                 return err;
650         }
651
652         if (!asn1_peek_uint8(state->data, &msgtype)) {
653                 return TLDAP_PROTOCOL_ERROR;
654         }
655
656         if (pmsg == NULL) {
657                 return TLDAP_SUCCESS;
658         }
659
660         msg = talloc_zero(mem_ctx, struct tldap_message);
661         if (msg == NULL) {
662                 return TLDAP_NO_MEMORY;
663         }
664         msg->id = state->id;
665
666         msg->inbuf = talloc_move(msg, &state->inbuf);
667         msg->data = talloc_move(msg, &state->data);
668         msg->type = msgtype;
669
670         *pmsg = msg;
671         return TLDAP_SUCCESS;
672 }
673
674 struct tldap_req_state {
675         int id;
676         struct asn1_data *out;
677         struct tldap_message *result;
678 };
679
680 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
681                                            struct tldap_context *ld,
682                                            struct tldap_req_state **pstate)
683 {
684         struct tevent_req *req;
685         struct tldap_req_state *state;
686
687         req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
688         if (req == NULL) {
689                 return NULL;
690         }
691         ZERO_STRUCTP(state);
692         state->out = asn1_init(state);
693         if (state->out == NULL) {
694                 TALLOC_FREE(req);
695                 return NULL;
696         }
697         state->result = NULL;
698         state->id = tldap_next_msgid(ld);
699
700         asn1_push_tag(state->out, ASN1_SEQUENCE(0));
701         asn1_write_Integer(state->out, state->id);
702
703         *pstate = state;
704         return req;
705 }
706
707 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
708 {
709         struct tldap_req_state *state = tevent_req_data(
710                 req, struct tldap_req_state);
711
712         TALLOC_FREE(ld->last_msg);
713         ld->last_msg = talloc_move(ld, &state->result);
714 }
715
716 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
717 {
718         char *result = talloc_array(mem_ctx, char, blob.length+1);
719         memcpy(result, blob.data, blob.length);
720         result[blob.length] = '\0';
721         return result;
722 }
723
724 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
725                                          struct asn1_data *data,
726                                          char **result)
727 {
728         DATA_BLOB string;
729         if (!asn1_read_OctetString(data, mem_ctx, &string))
730                 return false;
731         *result = blob2string_talloc(mem_ctx, string);
732         data_blob_free(&string);
733         return true;
734 }
735
736 static bool tldap_decode_controls(struct tldap_req_state *state);
737
738 static bool tldap_decode_response(struct tldap_req_state *state)
739 {
740         struct asn1_data *data = state->result->data;
741         struct tldap_message *msg = state->result;
742         bool ok = true;
743
744         ok &= asn1_read_enumerated(data, &msg->lderr);
745         ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
746         ok &= asn1_read_OctetString_talloc(msg, data,
747                                            &msg->res_diagnosticmessage);
748         if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
749                 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
750                 ok &= asn1_read_OctetString_talloc(msg, data,
751                                                    &msg->res_referral);
752                 ok &= asn1_end_tag(data);
753         } else {
754                 msg->res_referral = NULL;
755         }
756
757         return ok;
758 }
759
760 static void tldap_sasl_bind_done(struct tevent_req *subreq);
761
762 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
763                                         struct tevent_context *ev,
764                                         struct tldap_context *ld,
765                                         const char *dn,
766                                         const char *mechanism,
767                                         DATA_BLOB *creds,
768                                         struct tldap_control *sctrls,
769                                         int num_sctrls,
770                                         struct tldap_control *cctrls,
771                                         int num_cctrls)
772 {
773         struct tevent_req *req, *subreq;
774         struct tldap_req_state *state;
775
776         req = tldap_req_create(mem_ctx, ld, &state);
777         if (req == NULL) {
778                 return NULL;
779         }
780
781         if (dn == NULL) {
782                 dn = "";
783         }
784
785         asn1_push_tag(state->out, TLDAP_REQ_BIND);
786         asn1_write_Integer(state->out, ld->ld_version);
787         asn1_write_OctetString(state->out, dn, (dn != NULL) ? strlen(dn) : 0);
788
789         if (mechanism == NULL) {
790                 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
791                 asn1_write(state->out, creds->data, creds->length);
792                 asn1_pop_tag(state->out);
793         } else {
794                 asn1_push_tag(state->out, ASN1_CONTEXT(3));
795                 asn1_write_OctetString(state->out, mechanism,
796                                        strlen(mechanism));
797                 if ((creds != NULL) && (creds->data != NULL)) {
798                         asn1_write_OctetString(state->out, creds->data,
799                                                creds->length);
800                 }
801                 asn1_pop_tag(state->out);
802         }
803
804         if (!asn1_pop_tag(state->out)) {
805                 tevent_req_error(req, TLDAP_ENCODING_ERROR);
806                 return tevent_req_post(req, ev);
807         }
808
809         subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
810                                 sctrls, num_sctrls);
811         if (tevent_req_nomem(subreq, req)) {
812                 return tevent_req_post(req, ev);
813         }
814         tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
815         return req;
816 }
817
818 static void tldap_sasl_bind_done(struct tevent_req *subreq)
819 {
820         struct tevent_req *req = tevent_req_callback_data(
821                 subreq, struct tevent_req);
822         struct tldap_req_state *state = tevent_req_data(
823                 req, struct tldap_req_state);
824         int err;
825
826         err = tldap_msg_recv(subreq, state, &state->result);
827         TALLOC_FREE(subreq);
828         if (err != TLDAP_SUCCESS) {
829                 tevent_req_error(req, err);
830                 return;
831         }
832         if (state->result->type != TLDAP_RES_BIND) {
833                 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
834                 return;
835         }
836         if (!asn1_start_tag(state->result->data, state->result->type) ||
837             !tldap_decode_response(state) ||
838             !asn1_end_tag(state->result->data)) {
839                 tevent_req_error(req, TLDAP_DECODING_ERROR);
840                 return;
841         }
842         /*
843          * TODO: pull the reply blob
844          */
845         if (state->result->lderr != TLDAP_SUCCESS) {
846                 tevent_req_error(req, state->result->lderr);
847                 return;
848         }
849         tevent_req_done(req);
850 }
851
852 int tldap_sasl_bind_recv(struct tevent_req *req)
853 {
854         int err;
855
856         if (tevent_req_is_ldap_error(req, &err)) {
857                 return err;
858         }
859         return TLDAP_SUCCESS;
860 }
861
862 int tldap_sasl_bind(struct tldap_context *ld,
863                     const char *dn,
864                     const char *mechanism,
865                     DATA_BLOB *creds,
866                     struct tldap_control *sctrls,
867                     int num_sctrls,
868                     struct tldap_control *cctrls,
869                     int num_cctrls)
870 {
871         TALLOC_CTX *frame = talloc_stackframe();
872         struct tevent_context *ev;
873         struct tevent_req *req;
874         int result;
875
876         ev = event_context_init(frame);
877         if (ev == NULL) {
878                 result = TLDAP_NO_MEMORY;
879                 goto fail;
880         }
881
882         req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
883                                    sctrls, num_sctrls, cctrls, num_cctrls);
884         if (req == NULL) {
885                 result = TLDAP_NO_MEMORY;
886                 goto fail;
887         }
888
889         if (!tevent_req_poll(req, ev)) {
890                 result = TLDAP_OPERATIONS_ERROR;
891                 goto fail;
892         }
893
894         result = tldap_sasl_bind_recv(req);
895         tldap_save_msg(ld, req);
896  fail:
897         TALLOC_FREE(frame);
898         return result;
899 }
900
901 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
902                                           struct tevent_context *ev,
903                                           struct tldap_context *ld,
904                                           const char *dn,
905                                           const char *passwd)
906 {
907         DATA_BLOB cred;
908
909         if (passwd != NULL) {
910                 cred.data = (uint8_t *)passwd;
911                 cred.length = strlen(passwd);
912         } else {
913                 cred.data = (uint8_t *)"";
914                 cred.length = 0;
915         }
916         return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
917                                     NULL, 0);
918 }
919
920 int tldap_simple_bind_recv(struct tevent_req *req)
921 {
922         return tldap_sasl_bind_recv(req);
923 }
924
925 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
926                       const char *passwd)
927 {
928         DATA_BLOB cred;
929
930         if (passwd != NULL) {
931                 cred.data = (uint8_t *)passwd;
932                 cred.length = strlen(passwd);
933         } else {
934                 cred.data = (uint8_t *)"";
935                 cred.length = 0;
936         }
937         return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
938 }
939
940 /*****************************************************************************/
941
942 /*
943  * This piece has a dependency on ldb, the ldb_parse_tree() function is used.
944  * In case we want to separate out tldap, we need to copy or rewrite it.
945  */
946
947 #include "lib/ldb/include/ldb.h"
948 #include "lib/ldb/include/ldb_errors.h"
949
950 static bool ldap_push_filter(struct asn1_data *data,
951                              struct ldb_parse_tree *tree)
952 {
953         int i;
954
955         switch (tree->operation) {
956         case LDB_OP_AND:
957         case LDB_OP_OR:
958                 asn1_push_tag(data,
959                               ASN1_CONTEXT(tree->operation==LDB_OP_AND?0:1));
960                 for (i=0; i<tree->u.list.num_elements; i++) {
961                         if (!ldap_push_filter(data,
962                                               tree->u.list.elements[i])) {
963                                 return false;
964                         }
965                 }
966                 asn1_pop_tag(data);
967                 break;
968
969         case LDB_OP_NOT:
970                 asn1_push_tag(data, ASN1_CONTEXT(2));
971                 if (!ldap_push_filter(data, tree->u.isnot.child)) {
972                         return false;
973                 }
974                 asn1_pop_tag(data);
975                 break;
976
977         case LDB_OP_EQUALITY:
978                 /* equality test */
979                 asn1_push_tag(data, ASN1_CONTEXT(3));
980                 asn1_write_OctetString(data, tree->u.equality.attr,
981                                       strlen(tree->u.equality.attr));
982                 asn1_write_OctetString(data, tree->u.equality.value.data,
983                                       tree->u.equality.value.length);
984                 asn1_pop_tag(data);
985                 break;
986
987         case LDB_OP_SUBSTRING:
988                 /*
989                   SubstringFilter ::= SEQUENCE {
990                           type            AttributeDescription,
991                           -- at least one must be present
992                           substrings      SEQUENCE OF CHOICE {
993                                   initial [0] LDAPString,
994                                   any     [1] LDAPString,
995                                   final   [2] LDAPString } }
996                 */
997                 asn1_push_tag(data, ASN1_CONTEXT(4));
998                 asn1_write_OctetString(data, tree->u.substring.attr,
999                                        strlen(tree->u.substring.attr));
1000                 asn1_push_tag(data, ASN1_SEQUENCE(0));
1001                 i = 0;
1002                 if (!tree->u.substring.start_with_wildcard) {
1003                         asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(0));
1004                         asn1_write_DATA_BLOB_LDAPString(
1005                                 data, tree->u.substring.chunks[i]);
1006                         asn1_pop_tag(data);
1007                         i++;
1008                 }
1009                 while (tree->u.substring.chunks[i]) {
1010                         int ctx;
1011
1012                         if ((!tree->u.substring.chunks[i + 1]) &&
1013                             (tree->u.substring.end_with_wildcard == 0)) {
1014                                 ctx = 2;
1015                         } else {
1016                                 ctx = 1;
1017                         }
1018                         asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(ctx));
1019                         asn1_write_DATA_BLOB_LDAPString(
1020                                 data, tree->u.substring.chunks[i]);
1021                         asn1_pop_tag(data);
1022                         i++;
1023                 }
1024                 asn1_pop_tag(data);
1025                 asn1_pop_tag(data);
1026                 break;
1027
1028         case LDB_OP_GREATER:
1029                 /* greaterOrEqual test */
1030                 asn1_push_tag(data, ASN1_CONTEXT(5));
1031                 asn1_write_OctetString(data, tree->u.comparison.attr,
1032                                       strlen(tree->u.comparison.attr));
1033                 asn1_write_OctetString(data, tree->u.comparison.value.data,
1034                                       tree->u.comparison.value.length);
1035                 asn1_pop_tag(data);
1036                 break;
1037
1038         case LDB_OP_LESS:
1039                 /* lessOrEqual test */
1040                 asn1_push_tag(data, ASN1_CONTEXT(6));
1041                 asn1_write_OctetString(data, tree->u.comparison.attr,
1042                                       strlen(tree->u.comparison.attr));
1043                 asn1_write_OctetString(data, tree->u.comparison.value.data,
1044                                       tree->u.comparison.value.length);
1045                 asn1_pop_tag(data);
1046                 break;
1047
1048         case LDB_OP_PRESENT:
1049                 /* present test */
1050                 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(7));
1051                 asn1_write_LDAPString(data, tree->u.present.attr);
1052                 asn1_pop_tag(data);
1053                 return !data->has_error;
1054
1055         case LDB_OP_APPROX:
1056                 /* approx test */
1057                 asn1_push_tag(data, ASN1_CONTEXT(8));
1058                 asn1_write_OctetString(data, tree->u.comparison.attr,
1059                                       strlen(tree->u.comparison.attr));
1060                 asn1_write_OctetString(data, tree->u.comparison.value.data,
1061                                       tree->u.comparison.value.length);
1062                 asn1_pop_tag(data);
1063                 break;
1064
1065         case LDB_OP_EXTENDED:
1066                 /*
1067                   MatchingRuleAssertion ::= SEQUENCE {
1068                   matchingRule    [1] MatchingRuleID OPTIONAL,
1069                   type            [2] AttributeDescription OPTIONAL,
1070                   matchValue      [3] AssertionValue,
1071                   dnAttributes    [4] BOOLEAN DEFAULT FALSE
1072                   }
1073                 */
1074                 asn1_push_tag(data, ASN1_CONTEXT(9));
1075                 if (tree->u.extended.rule_id) {
1076                         asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1077                         asn1_write_LDAPString(data, tree->u.extended.rule_id);
1078                         asn1_pop_tag(data);
1079                 }
1080                 if (tree->u.extended.attr) {
1081                         asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1082                         asn1_write_LDAPString(data, tree->u.extended.attr);
1083                         asn1_pop_tag(data);
1084                 }
1085                 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1086                 asn1_write_DATA_BLOB_LDAPString(data, &tree->u.extended.value);
1087                 asn1_pop_tag(data);
1088                 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1089                 asn1_write_uint8(data, tree->u.extended.dnAttributes);
1090                 asn1_pop_tag(data);
1091                 asn1_pop_tag(data);
1092                 break;
1093
1094         default:
1095                 return false;
1096         }
1097         return !data->has_error;
1098 }
1099
1100 static bool tldap_push_filter(struct asn1_data *data, const char *filter)
1101 {
1102         struct ldb_parse_tree *tree;
1103         bool ret;
1104
1105         tree = ldb_parse_tree(talloc_tos(), filter);
1106         if (tree == NULL) {
1107                 return false;
1108         }
1109         ret = ldap_push_filter(data, tree);
1110         TALLOC_FREE(tree);
1111         return ret;
1112 }
1113
1114 /*****************************************************************************/
1115
1116 static void tldap_search_done(struct tevent_req *subreq);
1117
1118 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1119                                      struct tevent_context *ev,
1120                                      struct tldap_context *ld,
1121                                      const char *base, int scope,
1122                                      const char *filter,
1123                                      const char **attrs,
1124                                      int num_attrs,
1125                                      int attrsonly,
1126                                      struct tldap_control *sctrls,
1127                                      int num_sctrls,
1128                                      struct tldap_control *cctrls,
1129                                      int num_cctrls,
1130                                      int timelimit,
1131                                      int sizelimit,
1132                                      int deref)
1133 {
1134         struct tevent_req *req, *subreq;
1135         struct tldap_req_state *state;
1136         int i;
1137
1138         req = tldap_req_create(mem_ctx, ld, &state);
1139         if (req == NULL) {
1140                 return NULL;
1141         }
1142
1143         asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1144         asn1_write_OctetString(state->out, base, strlen(base));
1145         asn1_write_enumerated(state->out, scope);
1146         asn1_write_enumerated(state->out, deref);
1147         asn1_write_Integer(state->out, sizelimit);
1148         asn1_write_Integer(state->out, timelimit);
1149         asn1_write_BOOLEAN(state->out, attrsonly);
1150
1151         if (!tldap_push_filter(state->out, filter)) {
1152                 goto encoding_error;
1153         }
1154
1155         asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1156         for (i=0; i<num_attrs; i++) {
1157                 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1158         }
1159         asn1_pop_tag(state->out);
1160         asn1_pop_tag(state->out);
1161
1162         subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1163                                 sctrls, num_sctrls);
1164         if (tevent_req_nomem(subreq, req)) {
1165                 return tevent_req_post(req, ev);
1166         }
1167         tevent_req_set_callback(subreq, tldap_search_done, req);
1168         return req;
1169
1170  encoding_error:
1171         tevent_req_error(req, TLDAP_ENCODING_ERROR);
1172         return tevent_req_post(req, ev);
1173 }
1174
1175 static void tldap_search_done(struct tevent_req *subreq)
1176 {
1177         struct tevent_req *req = tevent_req_callback_data(
1178                 subreq, struct tevent_req);
1179         struct tldap_req_state *state = tevent_req_data(
1180                 req, struct tldap_req_state);
1181         int err;
1182
1183         err = tldap_msg_recv(subreq, state, &state->result);
1184         if (err != TLDAP_SUCCESS) {
1185                 tevent_req_error(req, err);
1186                 return;
1187         }
1188         switch (state->result->type) {
1189         case TLDAP_RES_SEARCH_ENTRY:
1190         case TLDAP_RES_SEARCH_REFERENCE:
1191                 tevent_req_notify_callback(req);
1192                 if (!tldap_msg_set_pending(subreq)) {
1193                         tevent_req_nomem(NULL, req);
1194                         return;
1195                 }
1196                 break;
1197         case TLDAP_RES_SEARCH_RESULT:
1198                 TALLOC_FREE(subreq);
1199                 if (!asn1_start_tag(state->result->data,
1200                                     state->result->type) ||
1201                     !tldap_decode_response(state) ||
1202                     !asn1_end_tag(state->result->data) ||
1203                     !tldap_decode_controls(state)) {
1204                         tevent_req_error(req, TLDAP_DECODING_ERROR);
1205                         return;
1206                 }
1207                 tevent_req_done(req);
1208                 break;
1209         default:
1210                 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1211                 return;
1212         }
1213 }
1214
1215 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1216                       struct tldap_message **pmsg)
1217 {
1218         struct tldap_req_state *state = tevent_req_data(
1219                 req, struct tldap_req_state);
1220         int err;
1221
1222         if (!tevent_req_is_in_progress(req)
1223             && tevent_req_is_ldap_error(req, &err)) {
1224                 return err;
1225         }
1226
1227         if (tevent_req_is_in_progress(req)) {
1228                 switch (state->result->type) {
1229                 case TLDAP_RES_SEARCH_ENTRY:
1230                 case TLDAP_RES_SEARCH_REFERENCE:
1231                         break;
1232                 default:
1233                         return TLDAP_OPERATIONS_ERROR;
1234                 }
1235         }
1236
1237         *pmsg = talloc_move(mem_ctx, &state->result);
1238         return TLDAP_SUCCESS;
1239 }
1240
1241 struct tldap_sync_search_state {
1242         TALLOC_CTX *mem_ctx;
1243         struct tldap_message **entries;
1244         struct tldap_message **refs;
1245         int rc;
1246 };
1247
1248 static void tldap_search_cb(struct tevent_req *req)
1249 {
1250         struct tldap_sync_search_state *state =
1251                 (struct tldap_sync_search_state *)
1252                 tevent_req_callback_data_void(req);
1253         struct tldap_message *msg, **tmp;
1254         int rc, num_entries, num_refs;
1255
1256         rc = tldap_search_recv(req, talloc_tos(), &msg);
1257         if (rc != TLDAP_SUCCESS) {
1258                 state->rc = rc;
1259                 return;
1260         }
1261
1262         switch (tldap_msg_type(msg)) {
1263         case TLDAP_RES_SEARCH_ENTRY:
1264                 num_entries = talloc_array_length(state->entries);
1265                 tmp = talloc_realloc(state->mem_ctx, state->entries,
1266                                      struct tldap_message *, num_entries + 1);
1267                 if (tmp == NULL) {
1268                         state->rc = TLDAP_NO_MEMORY;
1269                         return;
1270                 }
1271                 state->entries = tmp;
1272                 state->entries[num_entries] = talloc_move(state->entries,
1273                                                           &msg);
1274                 break;
1275         case TLDAP_RES_SEARCH_REFERENCE:
1276                 num_refs = talloc_array_length(state->refs);
1277                 tmp = talloc_realloc(state->mem_ctx, state->refs,
1278                                      struct tldap_message *, num_refs + 1);
1279                 if (tmp == NULL) {
1280                         state->rc = TLDAP_NO_MEMORY;
1281                         return;
1282                 }
1283                 state->refs = tmp;
1284                 state->refs[num_refs] = talloc_move(state->refs, &msg);
1285                 break;
1286         case TLDAP_RES_SEARCH_RESULT:
1287                 state->rc = TLDAP_SUCCESS;
1288                 break;
1289         default:
1290                 state->rc = TLDAP_PROTOCOL_ERROR;
1291                 break;
1292         }
1293 }
1294
1295 int tldap_search(struct tldap_context *ld,
1296                  const char *base, int scope, const char *filter,
1297                  const char **attrs, int num_attrs, int attrsonly,
1298                  struct tldap_control *sctrls, int num_sctrls,
1299                  struct tldap_control *cctrls, int num_cctrls,
1300                  int timelimit, int sizelimit, int deref,
1301                  TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1302                  struct tldap_message ***refs)
1303 {
1304         TALLOC_CTX *frame = talloc_stackframe();
1305         struct tevent_context *ev;
1306         struct tevent_req *req;
1307         struct tldap_sync_search_state state;
1308
1309         ZERO_STRUCT(state);
1310         state.mem_ctx = mem_ctx;
1311         state.rc = TLDAP_SUCCESS;
1312
1313         ev = event_context_init(frame);
1314         if (ev == NULL) {
1315                 state.rc = TLDAP_NO_MEMORY;
1316                 goto fail;
1317         }
1318
1319         req = tldap_search_send(frame, ev, ld, base, scope, filter,
1320                                 attrs, num_attrs, attrsonly,
1321                                 sctrls, num_sctrls, cctrls, num_cctrls,
1322                                 timelimit, sizelimit, deref);
1323         if (req == NULL) {
1324                 state.rc = TLDAP_NO_MEMORY;
1325                 goto fail;
1326         }
1327
1328         tevent_req_set_callback(req, tldap_search_cb, &state);
1329
1330         while (tevent_req_is_in_progress(req)
1331                && (state.rc == TLDAP_SUCCESS)) {
1332                 if (tevent_loop_once(ev) == -1) {
1333                         return TLDAP_OPERATIONS_ERROR;
1334                 }
1335         }
1336
1337         if (state.rc != TLDAP_SUCCESS) {
1338                 return state.rc;
1339         }
1340
1341         if (entries != NULL) {
1342                 *entries = state.entries;
1343         } else {
1344                 TALLOC_FREE(state.entries);
1345         }
1346         if (refs != NULL) {
1347                 *refs = state.refs;
1348         } else {
1349                 TALLOC_FREE(state.refs);
1350         }
1351         tldap_save_msg(ld, req);
1352 fail:
1353         TALLOC_FREE(frame);
1354         return state.rc;
1355 }
1356
1357 static bool tldap_parse_search_entry(struct tldap_message *msg)
1358 {
1359         int num_attribs = 0;
1360
1361         asn1_start_tag(msg->data, msg->type);
1362
1363         /* dn */
1364
1365         asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1366         if (msg->dn == NULL) {
1367                 return false;
1368         }
1369
1370         /*
1371          * Attributes: We overallocate msg->attribs by one, so that while
1372          * looping over the attributes we can directly parse into the last
1373          * array element. Same for the values in the inner loop.
1374          */
1375
1376         msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1377         if (msg->attribs == NULL) {
1378                 return false;
1379         }
1380
1381         asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1382         while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1383                 struct tldap_attribute *attrib;
1384                 int num_values = 0;
1385
1386                 attrib = &msg->attribs[num_attribs];
1387                 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1388                 if (attrib->values == NULL) {
1389                         return false;
1390                 }
1391                 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1392                 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1393                                              &attrib->name);
1394                 asn1_start_tag(msg->data, ASN1_SET);
1395
1396                 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1397                         asn1_read_OctetString(msg->data, msg,
1398                                               &attrib->values[num_values]);
1399
1400                         attrib->values = talloc_realloc(
1401                                 msg->attribs, attrib->values, DATA_BLOB,
1402                                 num_values + 2);
1403                         if (attrib->values == NULL) {
1404                                 return false;
1405                         }
1406                         num_values += 1;
1407                 }
1408                 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1409                                                 DATA_BLOB, num_values);
1410                 attrib->num_values = num_values;
1411
1412                 asn1_end_tag(msg->data); /* ASN1_SET */
1413                 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1414                 msg->attribs = talloc_realloc(
1415                         msg, msg->attribs, struct tldap_attribute,
1416                         num_attribs + 2);
1417                 if (msg->attribs == NULL) {
1418                         return false;
1419                 }
1420                 num_attribs += 1;
1421         }
1422         msg->attribs = talloc_realloc(
1423                 msg, msg->attribs, struct tldap_attribute, num_attribs);
1424         asn1_end_tag(msg->data);
1425         if (msg->data->has_error) {
1426                 return false;
1427         }
1428         return true;
1429 }
1430
1431 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1432 {
1433         if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1434                 return false;
1435         }
1436         *dn = msg->dn;
1437         return true;
1438 }
1439
1440 bool tldap_entry_attributes(struct tldap_message *msg, int *num_attributes,
1441                             struct tldap_attribute **attributes)
1442 {
1443         if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1444                 return false;
1445         }
1446         *attributes = msg->attribs;
1447         *num_attributes = talloc_array_length(msg->attribs);
1448         return true;
1449 }
1450
1451 static bool tldap_decode_controls(struct tldap_req_state *state)
1452 {
1453         struct tldap_message *msg = state->result;
1454         struct asn1_data *data = msg->data;
1455         struct tldap_control *sctrls = NULL;
1456         int num_controls = 0;
1457
1458         msg->res_sctrls = NULL;
1459
1460         if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1461                 return true;
1462         }
1463
1464         asn1_start_tag(data, ASN1_CONTEXT(0));
1465
1466         while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
1467                 struct tldap_control *c;
1468                 char *oid = NULL;
1469
1470                 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
1471                                         num_controls + 1);
1472                 if (sctrls == NULL) {
1473                         return false;
1474                 }
1475                 c = &sctrls[num_controls];
1476
1477                 asn1_start_tag(data, ASN1_SEQUENCE(0));
1478                 asn1_read_OctetString_talloc(msg, data, &oid);
1479                 if ((data->has_error) || (oid == NULL)) {
1480                         return false;
1481                 }
1482                 c->oid = oid;
1483                 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
1484                         asn1_read_BOOLEAN(data, &c->critical);
1485                 } else {
1486                         c->critical = false;
1487                 }
1488                 c->value = data_blob_null;
1489                 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
1490                     !asn1_read_OctetString(data, msg, &c->value)) {
1491                         return false;
1492                 }
1493                 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
1494
1495                 num_controls += 1;
1496         }
1497
1498         asn1_end_tag(data);     /* ASN1_CONTEXT(0) */
1499
1500         if (data->has_error) {
1501                 TALLOC_FREE(sctrls);
1502                 return false;
1503         }
1504         msg->res_sctrls = sctrls;
1505         return true;
1506 }
1507
1508 static void tldap_simple_done(struct tevent_req *subreq, int type)
1509 {
1510         struct tevent_req *req = tevent_req_callback_data(
1511                 subreq, struct tevent_req);
1512         struct tldap_req_state *state = tevent_req_data(
1513                 req, struct tldap_req_state);
1514         int err;
1515
1516         err = tldap_msg_recv(subreq, state, &state->result);
1517         TALLOC_FREE(subreq);
1518         if (err != TLDAP_SUCCESS) {
1519                 tevent_req_error(req, err);
1520                 return;
1521         }
1522         if (state->result->type != type) {
1523                 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1524                 return;
1525         }
1526         if (!asn1_start_tag(state->result->data, state->result->type) ||
1527             !tldap_decode_response(state) ||
1528             !asn1_end_tag(state->result->data) ||
1529             !tldap_decode_controls(state)) {
1530                 tevent_req_error(req, TLDAP_DECODING_ERROR);
1531                 return;
1532         }
1533         if (state->result->lderr != TLDAP_SUCCESS) {
1534                 tevent_req_error(req, state->result->lderr);
1535                 return;
1536         }
1537         tevent_req_done(req);
1538 }
1539
1540 static int tldap_simple_recv(struct tevent_req *req)
1541 {
1542         int err;
1543         if (tevent_req_is_ldap_error(req, &err)) {
1544                 return err;
1545         }
1546         return TLDAP_SUCCESS;
1547 }
1548
1549 static void tldap_add_done(struct tevent_req *subreq);
1550
1551 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
1552                                   struct tevent_context *ev,
1553                                   struct tldap_context *ld,
1554                                   const char *dn,
1555                                   struct tldap_mod *attributes,
1556                                   int num_attributes,
1557                                   struct tldap_control *sctrls,
1558                                   int num_sctrls,
1559                                   struct tldap_control *cctrls,
1560                                   int num_cctrls)
1561 {
1562         struct tevent_req *req, *subreq;
1563         struct tldap_req_state *state;
1564         int i, j;
1565
1566         req = tldap_req_create(mem_ctx, ld, &state);
1567         if (req == NULL) {
1568                 return NULL;
1569         }
1570
1571         asn1_push_tag(state->out, TLDAP_REQ_ADD);
1572         asn1_write_OctetString(state->out, dn, strlen(dn));
1573         asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1574
1575         for (i=0; i<num_attributes; i++) {
1576                 struct tldap_mod *attrib = &attributes[i];
1577                 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1578                 asn1_write_OctetString(state->out, attrib->attribute,
1579                                        strlen(attrib->attribute));
1580                 asn1_push_tag(state->out, ASN1_SET);
1581                 for (j=0; j<attrib->num_values; j++) {
1582                         asn1_write_OctetString(state->out,
1583                                                attrib->values[j].data,
1584                                                attrib->values[j].length);
1585                 }
1586                 asn1_pop_tag(state->out);
1587                 asn1_pop_tag(state->out);
1588         }
1589
1590         asn1_pop_tag(state->out);
1591         asn1_pop_tag(state->out);
1592
1593         subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1594                                 sctrls, num_sctrls);
1595         if (tevent_req_nomem(subreq, req)) {
1596                 return tevent_req_post(req, ev);
1597         }
1598         tevent_req_set_callback(subreq, tldap_add_done, req);
1599         return req;
1600 }
1601
1602 static void tldap_add_done(struct tevent_req *subreq)
1603 {
1604         return tldap_simple_done(subreq, TLDAP_RES_ADD);
1605 }
1606
1607 int tldap_add_recv(struct tevent_req *req)
1608 {
1609         return tldap_simple_recv(req);
1610 }
1611
1612 int tldap_add(struct tldap_context *ld, const char *dn,
1613               int num_attributes, struct tldap_mod *attributes,
1614               struct tldap_control *sctrls, int num_sctrls,
1615               struct tldap_control *cctrls, int num_cctrls)
1616 {
1617         TALLOC_CTX *frame = talloc_stackframe();
1618         struct tevent_context *ev;
1619         struct tevent_req *req;
1620         int result;
1621
1622         ev = event_context_init(frame);
1623         if (ev == NULL) {
1624                 result = TLDAP_NO_MEMORY;
1625                 goto fail;
1626         }
1627
1628         req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
1629                              sctrls, num_sctrls, cctrls, num_cctrls);
1630         if (req == NULL) {
1631                 result = TLDAP_NO_MEMORY;
1632                 goto fail;
1633         }
1634
1635         if (!tevent_req_poll(req, ev)) {
1636                 result = TLDAP_OPERATIONS_ERROR;
1637                 goto fail;
1638         }
1639
1640         result = tldap_add_recv(req);
1641         tldap_save_msg(ld, req);
1642  fail:
1643         TALLOC_FREE(frame);
1644         return result;
1645 }
1646
1647 static void tldap_modify_done(struct tevent_req *subreq);
1648
1649 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
1650                                      struct tevent_context *ev,
1651                                      struct tldap_context *ld,
1652                                      const char *dn,
1653                                      int num_mods, struct tldap_mod *mods,
1654                                      struct tldap_control *sctrls,
1655                                      int num_sctrls,
1656                                      struct tldap_control *cctrls,
1657                                      int num_cctrls)
1658 {
1659         struct tevent_req *req, *subreq;
1660         struct tldap_req_state *state;
1661         int i, j;
1662
1663         req = tldap_req_create(mem_ctx, ld, &state);
1664         if (req == NULL) {
1665                 return NULL;
1666         }
1667
1668         asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
1669         asn1_write_OctetString(state->out, dn, strlen(dn));
1670         asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1671
1672         for (i=0; i<num_mods; i++) {
1673                 struct tldap_mod *mod = &mods[i];
1674                 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1675                 asn1_write_enumerated(state->out, mod->mod_op),
1676                 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1677                 asn1_write_OctetString(state->out, mod->attribute,
1678                                        strlen(mod->attribute));
1679                 asn1_push_tag(state->out, ASN1_SET);
1680                 for (j=0; j<mod->num_values; j++) {
1681                         asn1_write_OctetString(state->out,
1682                                                mod->values[j].data,
1683                                                mod->values[j].length);
1684                 }
1685                 asn1_pop_tag(state->out);
1686                 asn1_pop_tag(state->out);
1687                 asn1_pop_tag(state->out);
1688         }
1689
1690         asn1_pop_tag(state->out);
1691         asn1_pop_tag(state->out);
1692
1693         subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1694                                 sctrls, num_sctrls);
1695         if (tevent_req_nomem(subreq, req)) {
1696                 return tevent_req_post(req, ev);
1697         }
1698         tevent_req_set_callback(subreq, tldap_modify_done, req);
1699         return req;
1700 }
1701
1702 static void tldap_modify_done(struct tevent_req *subreq)
1703 {
1704         return tldap_simple_done(subreq, TLDAP_RES_MODIFY);
1705 }
1706
1707 int tldap_modify_recv(struct tevent_req *req)
1708 {
1709         return tldap_simple_recv(req);
1710 }
1711
1712 int tldap_modify(struct tldap_context *ld, const char *dn,
1713                  int num_mods, struct tldap_mod *mods,
1714                  struct tldap_control *sctrls, int num_sctrls,
1715                  struct tldap_control *cctrls, int num_cctrls)
1716  {
1717         TALLOC_CTX *frame = talloc_stackframe();
1718         struct tevent_context *ev;
1719         struct tevent_req *req;
1720         int result;
1721
1722         ev = event_context_init(frame);
1723         if (ev == NULL) {
1724                 result = TLDAP_NO_MEMORY;
1725                 goto fail;
1726         }
1727
1728         req = tldap_modify_send(frame, ev, ld, dn, num_mods, mods,
1729                                 sctrls, num_sctrls, cctrls, num_cctrls);
1730         if (req == NULL) {
1731                 result = TLDAP_NO_MEMORY;
1732                 goto fail;
1733         }
1734
1735         if (!tevent_req_poll(req, ev)) {
1736                 result = TLDAP_OPERATIONS_ERROR;
1737                 goto fail;
1738         }
1739
1740         result = tldap_modify_recv(req);
1741         tldap_save_msg(ld, req);
1742  fail:
1743         TALLOC_FREE(frame);
1744         return result;
1745 }
1746
1747 static void tldap_delete_done(struct tevent_req *subreq);
1748
1749 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
1750                                      struct tevent_context *ev,
1751                                      struct tldap_context *ld,
1752                                      const char *dn,
1753                                      struct tldap_control *sctrls,
1754                                      int num_sctrls,
1755                                      struct tldap_control *cctrls,
1756                                      int num_cctrls)
1757 {
1758         struct tevent_req *req, *subreq;
1759         struct tldap_req_state *state;
1760
1761         req = tldap_req_create(mem_ctx, ld, &state);
1762         if (req == NULL) {
1763                 return NULL;
1764         }
1765
1766         asn1_push_tag(state->out, TLDAP_REQ_DELETE);
1767         asn1_write(state->out, dn, strlen(dn));
1768         asn1_pop_tag(state->out);
1769
1770         subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1771                                 sctrls, num_sctrls);
1772         if (tevent_req_nomem(subreq, req)) {
1773                 return tevent_req_post(req, ev);
1774         }
1775         tevent_req_set_callback(subreq, tldap_delete_done, req);
1776         return req;
1777 }
1778
1779 static void tldap_delete_done(struct tevent_req *subreq)
1780 {
1781         return tldap_simple_done(subreq, TLDAP_RES_DELETE);
1782 }
1783
1784 int tldap_delete_recv(struct tevent_req *req)
1785 {
1786         return tldap_simple_recv(req);
1787 }
1788
1789 int tldap_delete(struct tldap_context *ld, const char *dn,
1790                  struct tldap_control *sctrls, int num_sctrls,
1791                  struct tldap_control *cctrls, int num_cctrls)
1792 {
1793         TALLOC_CTX *frame = talloc_stackframe();
1794         struct tevent_context *ev;
1795         struct tevent_req *req;
1796         int result;
1797
1798         ev = event_context_init(frame);
1799         if (ev == NULL) {
1800                 result = TLDAP_NO_MEMORY;
1801                 goto fail;
1802         }
1803
1804         req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
1805                                 cctrls, num_cctrls);
1806         if (req == NULL) {
1807                 result = TLDAP_NO_MEMORY;
1808                 goto fail;
1809         }
1810
1811         if (!tevent_req_poll(req, ev)) {
1812                 result = TLDAP_OPERATIONS_ERROR;
1813                 goto fail;
1814         }
1815
1816         result = tldap_delete_recv(req);
1817         tldap_save_msg(ld, req);
1818  fail:
1819         TALLOC_FREE(frame);
1820         return result;
1821 }
1822
1823 int tldap_msg_id(const struct tldap_message *msg)
1824 {
1825         return msg->id;
1826 }
1827
1828 int tldap_msg_type(const struct tldap_message *msg)
1829 {
1830         return msg->type;
1831 }
1832
1833 const char *tldap_msg_matcheddn(struct tldap_message *msg)
1834 {
1835         if (msg == NULL) {
1836                 return NULL;
1837         }
1838         return msg->res_matcheddn;
1839 }
1840
1841 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
1842 {
1843         if (msg == NULL) {
1844                 return NULL;
1845         }
1846         return msg->res_diagnosticmessage;
1847 }
1848
1849 const char *tldap_msg_referral(struct tldap_message *msg)
1850 {
1851         if (msg == NULL) {
1852                 return NULL;
1853         }
1854         return msg->res_referral;
1855 }
1856
1857 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
1858                       struct tldap_control **sctrls)
1859 {
1860         if (msg == NULL) {
1861                 *sctrls = NULL;
1862                 *num_sctrls = 0;
1863         }
1864         *sctrls = msg->res_sctrls;
1865         *num_sctrls = talloc_array_length(msg->res_sctrls);
1866 }
1867
1868 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
1869 {
1870         return ld->last_msg;
1871 }
1872
1873 const char *tldap_err2string(int rc)
1874 {
1875         const char *res = NULL;
1876
1877         /*
1878          * This would normally be a table, but the error codes are not fully
1879          * sequential. Let the compiler figure out the optimum implementation
1880          * :-)
1881          */
1882
1883         switch (rc) {
1884         case TLDAP_SUCCESS:
1885                 res = "TLDAP_SUCCESS";
1886                 break;
1887         case TLDAP_OPERATIONS_ERROR:
1888                 res = "TLDAP_OPERATIONS_ERROR";
1889                 break;
1890         case TLDAP_PROTOCOL_ERROR:
1891                 res = "TLDAP_PROTOCOL_ERROR";
1892                 break;
1893         case TLDAP_TIMELIMIT_EXCEEDED:
1894                 res = "TLDAP_TIMELIMIT_EXCEEDED";
1895                 break;
1896         case TLDAP_SIZELIMIT_EXCEEDED:
1897                 res = "TLDAP_SIZELIMIT_EXCEEDED";
1898                 break;
1899         case TLDAP_COMPARE_FALSE:
1900                 res = "TLDAP_COMPARE_FALSE";
1901                 break;
1902         case TLDAP_COMPARE_TRUE:
1903                 res = "TLDAP_COMPARE_TRUE";
1904                 break;
1905         case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
1906                 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
1907                 break;
1908         case TLDAP_STRONG_AUTH_REQUIRED:
1909                 res = "TLDAP_STRONG_AUTH_REQUIRED";
1910                 break;
1911         case TLDAP_REFERRAL:
1912                 res = "TLDAP_REFERRAL";
1913                 break;
1914         case TLDAP_ADMINLIMIT_EXCEEDED:
1915                 res = "TLDAP_ADMINLIMIT_EXCEEDED";
1916                 break;
1917         case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
1918                 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
1919                 break;
1920         case TLDAP_CONFIDENTIALITY_REQUIRED:
1921                 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
1922                 break;
1923         case TLDAP_SASL_BIND_IN_PROGRESS:
1924                 res = "TLDAP_SASL_BIND_IN_PROGRESS";
1925                 break;
1926         case TLDAP_NO_SUCH_ATTRIBUTE:
1927                 res = "TLDAP_NO_SUCH_ATTRIBUTE";
1928                 break;
1929         case TLDAP_UNDEFINED_TYPE:
1930                 res = "TLDAP_UNDEFINED_TYPE";
1931                 break;
1932         case TLDAP_INAPPROPRIATE_MATCHING:
1933                 res = "TLDAP_INAPPROPRIATE_MATCHING";
1934                 break;
1935         case TLDAP_CONSTRAINT_VIOLATION:
1936                 res = "TLDAP_CONSTRAINT_VIOLATION";
1937                 break;
1938         case TLDAP_TYPE_OR_VALUE_EXISTS:
1939                 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
1940                 break;
1941         case TLDAP_INVALID_SYNTAX:
1942                 res = "TLDAP_INVALID_SYNTAX";
1943                 break;
1944         case TLDAP_NO_SUCH_OBJECT:
1945                 res = "TLDAP_NO_SUCH_OBJECT";
1946                 break;
1947         case TLDAP_ALIAS_PROBLEM:
1948                 res = "TLDAP_ALIAS_PROBLEM";
1949                 break;
1950         case TLDAP_INVALID_DN_SYNTAX:
1951                 res = "TLDAP_INVALID_DN_SYNTAX";
1952                 break;
1953         case TLDAP_IS_LEAF:
1954                 res = "TLDAP_IS_LEAF";
1955                 break;
1956         case TLDAP_ALIAS_DEREF_PROBLEM:
1957                 res = "TLDAP_ALIAS_DEREF_PROBLEM";
1958                 break;
1959         case TLDAP_INAPPROPRIATE_AUTH:
1960                 res = "TLDAP_INAPPROPRIATE_AUTH";
1961                 break;
1962         case TLDAP_INVALID_CREDENTIALS:
1963                 res = "TLDAP_INVALID_CREDENTIALS";
1964                 break;
1965         case TLDAP_INSUFFICIENT_ACCESS:
1966                 res = "TLDAP_INSUFFICIENT_ACCESS";
1967                 break;
1968         case TLDAP_BUSY:
1969                 res = "TLDAP_BUSY";
1970                 break;
1971         case TLDAP_UNAVAILABLE:
1972                 res = "TLDAP_UNAVAILABLE";
1973                 break;
1974         case TLDAP_UNWILLING_TO_PERFORM:
1975                 res = "TLDAP_UNWILLING_TO_PERFORM";
1976                 break;
1977         case TLDAP_LOOP_DETECT:
1978                 res = "TLDAP_LOOP_DETECT";
1979                 break;
1980         case TLDAP_NAMING_VIOLATION:
1981                 res = "TLDAP_NAMING_VIOLATION";
1982                 break;
1983         case TLDAP_OBJECT_CLASS_VIOLATION:
1984                 res = "TLDAP_OBJECT_CLASS_VIOLATION";
1985                 break;
1986         case TLDAP_NOT_ALLOWED_ON_NONLEAF:
1987                 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
1988                 break;
1989         case TLDAP_NOT_ALLOWED_ON_RDN:
1990                 res = "TLDAP_NOT_ALLOWED_ON_RDN";
1991                 break;
1992         case TLDAP_ALREADY_EXISTS:
1993                 res = "TLDAP_ALREADY_EXISTS";
1994                 break;
1995         case TLDAP_NO_OBJECT_CLASS_MODS:
1996                 res = "TLDAP_NO_OBJECT_CLASS_MODS";
1997                 break;
1998         case TLDAP_RESULTS_TOO_LARGE:
1999                 res = "TLDAP_RESULTS_TOO_LARGE";
2000                 break;
2001         case TLDAP_AFFECTS_MULTIPLE_DSAS:
2002                 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2003                 break;
2004         case TLDAP_OTHER:
2005                 res = "TLDAP_OTHER";
2006                 break;
2007         case TLDAP_SERVER_DOWN:
2008                 res = "TLDAP_SERVER_DOWN";
2009                 break;
2010         case TLDAP_LOCAL_ERROR:
2011                 res = "TLDAP_LOCAL_ERROR";
2012                 break;
2013         case TLDAP_ENCODING_ERROR:
2014                 res = "TLDAP_ENCODING_ERROR";
2015                 break;
2016         case TLDAP_DECODING_ERROR:
2017                 res = "TLDAP_DECODING_ERROR";
2018                 break;
2019         case TLDAP_TIMEOUT:
2020                 res = "TLDAP_TIMEOUT";
2021                 break;
2022         case TLDAP_AUTH_UNKNOWN:
2023                 res = "TLDAP_AUTH_UNKNOWN";
2024                 break;
2025         case TLDAP_FILTER_ERROR:
2026                 res = "TLDAP_FILTER_ERROR";
2027                 break;
2028         case TLDAP_USER_CANCELLED:
2029                 res = "TLDAP_USER_CANCELLED";
2030                 break;
2031         case TLDAP_PARAM_ERROR:
2032                 res = "TLDAP_PARAM_ERROR";
2033                 break;
2034         case TLDAP_NO_MEMORY:
2035                 res = "TLDAP_NO_MEMORY";
2036                 break;
2037         case TLDAP_CONNECT_ERROR:
2038                 res = "TLDAP_CONNECT_ERROR";
2039                 break;
2040         case TLDAP_NOT_SUPPORTED:
2041                 res = "TLDAP_NOT_SUPPORTED";
2042                 break;
2043         case TLDAP_CONTROL_NOT_FOUND:
2044                 res = "TLDAP_CONTROL_NOT_FOUND";
2045                 break;
2046         case TLDAP_NO_RESULTS_RETURNED:
2047                 res = "TLDAP_NO_RESULTS_RETURNED";
2048                 break;
2049         case TLDAP_MORE_RESULTS_TO_RETURN:
2050                 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2051                 break;
2052         case TLDAP_CLIENT_LOOP:
2053                 res = "TLDAP_CLIENT_LOOP";
2054                 break;
2055         case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2056                 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2057                 break;
2058         default:
2059                 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2060                                       rc);
2061                 break;
2062         }
2063         if (res == NULL) {
2064                 res = "Unknown LDAP Error";
2065         }
2066         return res;
2067 }