param: Rename variable used for lp_default_devmode bDefaultDevmode
[Samba.git] / source3 / lib / tldap.c
blobb15ee73b0377f8638a627660a2a4d77c942392c1
1 /*
2 Unix SMB/CIFS implementation.
3 Infrastructure for async ldap client requests
4 Copyright (C) Volker Lendecke 2009
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.
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.
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/>.
20 #include "includes.h"
21 #include "tldap.h"
22 #include "../lib/util/asn1.h"
23 #include "../lib/tsocket/tsocket.h"
24 #include "../lib/util/tevent_unix.h"
26 static int tldap_simple_recv(struct tevent_req *req);
28 bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
30 enum tevent_req_state state;
31 uint64_t err;
33 if (!tevent_req_is_error(req, &state, &err)) {
34 return false;
36 switch (state) {
37 case TEVENT_REQ_TIMED_OUT:
38 *perr = TLDAP_TIMEOUT;
39 break;
40 case TEVENT_REQ_NO_MEMORY:
41 *perr = TLDAP_NO_MEMORY;
42 break;
43 case TEVENT_REQ_USER_ERROR:
44 *perr = err;
45 break;
46 default:
47 *perr = TLDAP_OPERATIONS_ERROR;
48 break;
50 return true;
53 struct tldap_ctx_attribute {
54 char *name;
55 void *ptr;
58 struct tldap_context {
59 int ld_version;
60 int ld_deref;
61 int ld_sizelimit;
62 int ld_timelimit;
63 struct tstream_context *conn;
64 bool server_down;
65 int msgid;
66 struct tevent_queue *outgoing;
67 struct tevent_req **pending;
69 /* For the sync wrappers we need something like get_last_error... */
70 struct tldap_message *last_msg;
72 /* debug */
73 void (*log_fn)(void *context, enum tldap_debug_level level,
74 const char *fmt, va_list ap);
75 void *log_private;
77 struct tldap_ctx_attribute *ctx_attrs;
80 struct tldap_message {
81 struct asn1_data *data;
82 uint8_t *inbuf;
83 int type;
84 int id;
86 /* RESULT_ENTRY */
87 char *dn;
88 struct tldap_attribute *attribs;
90 /* Error data sent by the server */
91 int lderr;
92 char *res_matcheddn;
93 char *res_diagnosticmessage;
94 char *res_referral;
95 struct tldap_control *res_sctrls;
97 /* Controls sent by the server */
98 struct tldap_control *ctrls;
101 void tldap_set_debug(struct tldap_context *ld,
102 void (*log_fn)(void *log_private,
103 enum tldap_debug_level level,
104 const char *fmt,
105 va_list ap) PRINTF_ATTRIBUTE(3,0),
106 void *log_private)
108 ld->log_fn = log_fn;
109 ld->log_private = log_private;
112 static void tldap_debug(struct tldap_context *ld,
113 enum tldap_debug_level level,
114 const char *fmt, ...)
116 va_list ap;
117 if (!ld) {
118 return;
120 if (ld->log_fn == NULL) {
121 return;
123 va_start(ap, fmt);
124 ld->log_fn(ld->log_private, level, fmt, ap);
125 va_end(ap);
128 static int tldap_next_msgid(struct tldap_context *ld)
130 int result;
132 result = ld->msgid++;
133 if (ld->msgid == 2147483647) {
134 ld->msgid = 1;
136 return result;
139 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
141 struct tldap_context *ctx;
142 int ret;
144 ctx = talloc_zero(mem_ctx, struct tldap_context);
145 if (ctx == NULL) {
146 return NULL;
148 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
149 if (ret == -1) {
150 TALLOC_FREE(ctx);
151 return NULL;
153 ctx->msgid = 1;
154 ctx->ld_version = 3;
155 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
156 if (ctx->outgoing == NULL) {
157 TALLOC_FREE(ctx);
158 return NULL;
160 return ctx;
163 bool tldap_connection_ok(struct tldap_context *ld)
165 if (ld == NULL) {
166 return false;
168 return !ld->server_down;
171 static struct tldap_ctx_attribute *tldap_context_findattr(
172 struct tldap_context *ld, const char *name)
174 int i, num_attrs;
176 num_attrs = talloc_array_length(ld->ctx_attrs);
178 for (i=0; i<num_attrs; i++) {
179 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
180 return &ld->ctx_attrs[i];
183 return NULL;
186 bool tldap_context_setattr(struct tldap_context *ld,
187 const char *name, const void *_pptr)
189 struct tldap_ctx_attribute *tmp, *attr;
190 char *tmpname;
191 int num_attrs;
192 void **pptr = (void **)discard_const_p(void,_pptr);
194 attr = tldap_context_findattr(ld, name);
195 if (attr != NULL) {
197 * We don't actually delete attrs, we don't expect tons of
198 * attributes being shuffled around.
200 TALLOC_FREE(attr->ptr);
201 if (*pptr != NULL) {
202 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
203 *pptr = NULL;
205 return true;
208 tmpname = talloc_strdup(ld, name);
209 if (tmpname == NULL) {
210 return false;
213 num_attrs = talloc_array_length(ld->ctx_attrs);
215 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
216 num_attrs+1);
217 if (tmp == NULL) {
218 TALLOC_FREE(tmpname);
219 return false;
221 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
222 if (*pptr != NULL) {
223 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
224 } else {
225 tmp[num_attrs].ptr = NULL;
227 *pptr = NULL;
228 ld->ctx_attrs = tmp;
229 return true;
232 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
234 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
236 if (attr == NULL) {
237 return NULL;
239 return attr->ptr;
242 struct read_ldap_state {
243 uint8_t *buf;
244 bool done;
247 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
248 static void read_ldap_done(struct tevent_req *subreq);
250 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
251 struct tevent_context *ev,
252 struct tstream_context *conn)
254 struct tevent_req *req, *subreq;
255 struct read_ldap_state *state;
257 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
258 if (req == NULL) {
259 return NULL;
261 state->done = false;
263 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
264 state);
265 if (tevent_req_nomem(subreq, req)) {
266 return tevent_req_post(req, ev);
268 tevent_req_set_callback(subreq, read_ldap_done, req);
269 return req;
272 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
274 struct read_ldap_state *state = talloc_get_type_abort(
275 private_data, struct read_ldap_state);
276 size_t len;
277 int i, lensize;
279 if (state->done) {
280 /* We've been here, we're done */
281 return 0;
285 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
287 if (buf[0] != 0x30) {
288 return -1;
291 len = buf[1];
292 if ((len & 0x80) == 0) {
293 state->done = true;
294 return len;
297 lensize = (len & 0x7f);
298 len = 0;
300 if (buflen == 2) {
301 /* Please get us the full length */
302 return lensize;
304 if (buflen > 2 + lensize) {
305 state->done = true;
306 return 0;
308 if (buflen != 2 + lensize) {
309 return -1;
312 for (i=0; i<lensize; i++) {
313 len = (len << 8) | buf[2+i];
315 return len;
318 static void read_ldap_done(struct tevent_req *subreq)
320 struct tevent_req *req = tevent_req_callback_data(
321 subreq, struct tevent_req);
322 struct read_ldap_state *state = tevent_req_data(
323 req, struct read_ldap_state);
324 ssize_t nread;
325 int err;
327 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
328 TALLOC_FREE(subreq);
329 if (nread == -1) {
330 tevent_req_error(req, err);
331 return;
333 tevent_req_done(req);
336 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
337 uint8_t **pbuf, int *perrno)
339 struct read_ldap_state *state = tevent_req_data(
340 req, struct read_ldap_state);
342 if (tevent_req_is_unix_error(req, perrno)) {
343 return -1;
345 *pbuf = talloc_move(mem_ctx, &state->buf);
346 return talloc_get_size(*pbuf);
349 struct tldap_msg_state {
350 struct tldap_context *ld;
351 struct tevent_context *ev;
352 int id;
353 struct iovec iov;
355 struct asn1_data *data;
356 uint8_t *inbuf;
359 static void tldap_push_controls(struct asn1_data *data,
360 struct tldap_control *sctrls,
361 int num_sctrls)
363 int i;
365 if ((sctrls == NULL) || (num_sctrls == 0)) {
366 return;
369 asn1_push_tag(data, ASN1_CONTEXT(0));
371 for (i=0; i<num_sctrls; i++) {
372 struct tldap_control *c = &sctrls[i];
373 asn1_push_tag(data, ASN1_SEQUENCE(0));
374 asn1_write_OctetString(data, c->oid, strlen(c->oid));
375 if (c->critical) {
376 asn1_write_BOOLEAN(data, true);
378 if (c->value.data != NULL) {
379 asn1_write_OctetString(data, c->value.data,
380 c->value.length);
382 asn1_pop_tag(data); /* ASN1_SEQUENCE(0) */
385 asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
388 static void tldap_msg_sent(struct tevent_req *subreq);
389 static void tldap_msg_received(struct tevent_req *subreq);
391 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
392 struct tevent_context *ev,
393 struct tldap_context *ld,
394 int id, struct asn1_data *data,
395 struct tldap_control *sctrls,
396 int num_sctrls)
398 struct tevent_req *req, *subreq;
399 struct tldap_msg_state *state;
400 DATA_BLOB blob;
402 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
403 id);
405 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
406 if (req == NULL) {
407 return NULL;
409 state->ld = ld;
410 state->ev = ev;
411 state->id = id;
413 if (state->ld->server_down) {
414 tevent_req_error(req, TLDAP_SERVER_DOWN);
415 return tevent_req_post(req, ev);
418 tldap_push_controls(data, sctrls, num_sctrls);
420 asn1_pop_tag(data);
422 if (!asn1_blob(data, &blob)) {
423 tevent_req_error(req, TLDAP_ENCODING_ERROR);
424 return tevent_req_post(req, ev);
427 state->iov.iov_base = (void *)blob.data;
428 state->iov.iov_len = blob.length;
430 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
431 &state->iov, 1);
432 if (tevent_req_nomem(subreq, req)) {
433 return tevent_req_post(req, ev);
435 tevent_req_set_callback(subreq, tldap_msg_sent, req);
436 return req;
439 static void tldap_msg_unset_pending(struct tevent_req *req)
441 struct tldap_msg_state *state = tevent_req_data(
442 req, struct tldap_msg_state);
443 struct tldap_context *ld = state->ld;
444 int num_pending = talloc_array_length(ld->pending);
445 int i;
447 tevent_req_set_cleanup_fn(req, NULL);
449 if (num_pending == 1) {
450 TALLOC_FREE(ld->pending);
451 return;
454 for (i=0; i<num_pending; i++) {
455 if (req == ld->pending[i]) {
456 break;
459 if (i == num_pending) {
461 * Something's seriously broken. Just returning here is the
462 * right thing nevertheless, the point of this routine is to
463 * remove ourselves from cli->pending.
465 return;
469 * Remove ourselves from the cli->pending array
471 if (num_pending > 1) {
472 ld->pending[i] = ld->pending[num_pending-1];
476 * No NULL check here, we're shrinking by sizeof(void *), and
477 * talloc_realloc just adjusts the size for this.
479 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
480 num_pending - 1);
481 return;
484 static void tldap_msg_cleanup(struct tevent_req *req,
485 enum tevent_req_state req_state)
487 switch (req_state) {
488 case TEVENT_REQ_USER_ERROR:
489 case TEVENT_REQ_RECEIVED:
490 tldap_msg_unset_pending(req);
491 return;
492 default:
493 return;
497 static bool tldap_msg_set_pending(struct tevent_req *req)
499 struct tldap_msg_state *state = tevent_req_data(
500 req, struct tldap_msg_state);
501 struct tldap_context *ld;
502 struct tevent_req **pending;
503 int num_pending;
504 struct tevent_req *subreq;
506 ld = state->ld;
507 num_pending = talloc_array_length(ld->pending);
509 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
510 num_pending+1);
511 if (pending == NULL) {
512 return false;
514 pending[num_pending] = req;
515 ld->pending = pending;
516 tevent_req_set_cleanup_fn(req, tldap_msg_cleanup);
518 if (num_pending > 0) {
519 return true;
523 * We're the first one, add the read_ldap request that waits for the
524 * answer from the server
526 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
527 if (subreq == NULL) {
528 tldap_msg_unset_pending(req);
529 return false;
531 tevent_req_set_callback(subreq, tldap_msg_received, ld);
532 return true;
535 static void tldap_msg_sent(struct tevent_req *subreq)
537 struct tevent_req *req = tevent_req_callback_data(
538 subreq, struct tevent_req);
539 struct tldap_msg_state *state = tevent_req_data(
540 req, struct tldap_msg_state);
541 ssize_t nwritten;
542 int err;
544 nwritten = tstream_writev_queue_recv(subreq, &err);
545 TALLOC_FREE(subreq);
546 if (nwritten == -1) {
547 state->ld->server_down = true;
548 tevent_req_error(req, TLDAP_SERVER_DOWN);
549 return;
552 if (!tldap_msg_set_pending(req)) {
553 tevent_req_oom(req);
554 return;
558 static int tldap_msg_msgid(struct tevent_req *req)
560 struct tldap_msg_state *state = tevent_req_data(
561 req, struct tldap_msg_state);
563 return state->id;
566 static void tldap_msg_received(struct tevent_req *subreq)
568 struct tldap_context *ld = tevent_req_callback_data(
569 subreq, struct tldap_context);
570 struct tevent_req *req;
571 struct tldap_msg_state *state;
572 struct asn1_data *data;
573 uint8_t *inbuf;
574 ssize_t received;
575 size_t num_pending;
576 int i, err, status;
577 int id;
578 uint8_t type;
579 bool ok;
581 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
582 TALLOC_FREE(subreq);
583 if (received == -1) {
584 status = TLDAP_SERVER_DOWN;
585 goto fail;
588 data = asn1_init(talloc_tos());
589 if (data == NULL) {
590 status = TLDAP_NO_MEMORY;
591 goto fail;
593 asn1_load_nocopy(data, inbuf, received);
595 ok = true;
596 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
597 ok &= asn1_read_Integer(data, &id);
598 ok &= asn1_peek_uint8(data, &type);
600 if (!ok) {
601 status = TLDAP_PROTOCOL_ERROR;
602 goto fail;
605 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
606 "type %d\n", id, (int)type);
608 num_pending = talloc_array_length(ld->pending);
610 for (i=0; i<num_pending; i++) {
611 if (id == tldap_msg_msgid(ld->pending[i])) {
612 break;
615 if (i == num_pending) {
616 /* Dump unexpected reply */
617 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
618 "No request pending for msg %d\n", id);
619 TALLOC_FREE(data);
620 TALLOC_FREE(inbuf);
621 goto done;
624 req = ld->pending[i];
625 state = tevent_req_data(req, struct tldap_msg_state);
627 state->inbuf = talloc_move(state, &inbuf);
628 state->data = talloc_move(state, &data);
630 tldap_msg_unset_pending(req);
631 num_pending = talloc_array_length(ld->pending);
633 tevent_req_done(req);
635 done:
636 if (num_pending == 0) {
637 return;
639 if (talloc_array_length(ld->pending) > num_pending) {
641 * The callback functions called from tevent_req_done() above
642 * have put something on the pending queue. We don't have to
643 * trigger the read_ldap_send(), tldap_msg_set_pending() has
644 * done it for us already.
646 return;
649 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
650 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
651 if (subreq == NULL) {
652 status = TLDAP_NO_MEMORY;
653 goto fail;
655 tevent_req_set_callback(subreq, tldap_msg_received, ld);
656 return;
658 fail:
659 while (talloc_array_length(ld->pending) > 0) {
660 req = ld->pending[0];
661 state = tevent_req_data(req, struct tldap_msg_state);
662 tevent_req_defer_callback(req, state->ev);
663 tevent_req_error(req, status);
667 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
668 struct tldap_message **pmsg)
670 struct tldap_msg_state *state = tevent_req_data(
671 req, struct tldap_msg_state);
672 struct tldap_message *msg;
673 int err;
674 uint8_t msgtype;
676 if (tevent_req_is_ldap_error(req, &err)) {
677 return err;
680 if (!asn1_peek_uint8(state->data, &msgtype)) {
681 return TLDAP_PROTOCOL_ERROR;
684 if (pmsg == NULL) {
685 return TLDAP_SUCCESS;
688 msg = talloc_zero(mem_ctx, struct tldap_message);
689 if (msg == NULL) {
690 return TLDAP_NO_MEMORY;
692 msg->id = state->id;
694 msg->inbuf = talloc_move(msg, &state->inbuf);
695 msg->data = talloc_move(msg, &state->data);
696 msg->type = msgtype;
698 *pmsg = msg;
699 return TLDAP_SUCCESS;
702 struct tldap_req_state {
703 int id;
704 struct asn1_data *out;
705 struct tldap_message *result;
708 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
709 struct tldap_context *ld,
710 struct tldap_req_state **pstate)
712 struct tevent_req *req;
713 struct tldap_req_state *state;
715 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
716 if (req == NULL) {
717 return NULL;
719 ZERO_STRUCTP(state);
720 state->out = asn1_init(state);
721 if (state->out == NULL) {
722 TALLOC_FREE(req);
723 return NULL;
725 state->result = NULL;
726 state->id = tldap_next_msgid(ld);
728 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
729 asn1_write_Integer(state->out, state->id);
731 *pstate = state;
732 return req;
735 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
737 struct tldap_req_state *state = tevent_req_data(
738 req, struct tldap_req_state);
740 TALLOC_FREE(ld->last_msg);
741 ld->last_msg = talloc_move(ld, &state->result);
744 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
746 char *result = talloc_array(mem_ctx, char, blob.length+1);
748 if (result == NULL) {
749 return NULL;
752 memcpy(result, blob.data, blob.length);
753 result[blob.length] = '\0';
754 return result;
757 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
758 struct asn1_data *data,
759 char **presult)
761 DATA_BLOB string;
762 char *result;
763 if (!asn1_read_OctetString(data, mem_ctx, &string))
764 return false;
766 result = blob2string_talloc(mem_ctx, string);
768 data_blob_free(&string);
770 if (result == NULL) {
771 return false;
773 *presult = result;
774 return true;
777 static bool tldap_decode_controls(struct tldap_req_state *state);
779 static bool tldap_decode_response(struct tldap_req_state *state)
781 struct asn1_data *data = state->result->data;
782 struct tldap_message *msg = state->result;
783 bool ok = true;
785 ok &= asn1_read_enumerated(data, &msg->lderr);
786 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
787 ok &= asn1_read_OctetString_talloc(msg, data,
788 &msg->res_diagnosticmessage);
789 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
790 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
791 ok &= asn1_read_OctetString_talloc(msg, data,
792 &msg->res_referral);
793 ok &= asn1_end_tag(data);
794 } else {
795 msg->res_referral = NULL;
798 return ok;
801 static void tldap_sasl_bind_done(struct tevent_req *subreq);
803 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
804 struct tevent_context *ev,
805 struct tldap_context *ld,
806 const char *dn,
807 const char *mechanism,
808 DATA_BLOB *creds,
809 struct tldap_control *sctrls,
810 int num_sctrls,
811 struct tldap_control *cctrls,
812 int num_cctrls)
814 struct tevent_req *req, *subreq;
815 struct tldap_req_state *state;
817 req = tldap_req_create(mem_ctx, ld, &state);
818 if (req == NULL) {
819 return NULL;
822 if (dn == NULL) {
823 dn = "";
826 asn1_push_tag(state->out, TLDAP_REQ_BIND);
827 asn1_write_Integer(state->out, ld->ld_version);
828 asn1_write_OctetString(state->out, dn, strlen(dn));
830 if (mechanism == NULL) {
831 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
832 asn1_write(state->out, creds->data, creds->length);
833 asn1_pop_tag(state->out);
834 } else {
835 asn1_push_tag(state->out, ASN1_CONTEXT(3));
836 asn1_write_OctetString(state->out, mechanism,
837 strlen(mechanism));
838 if ((creds != NULL) && (creds->data != NULL)) {
839 asn1_write_OctetString(state->out, creds->data,
840 creds->length);
842 asn1_pop_tag(state->out);
845 if (!asn1_pop_tag(state->out)) {
846 tevent_req_error(req, TLDAP_ENCODING_ERROR);
847 return tevent_req_post(req, ev);
850 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
851 sctrls, num_sctrls);
852 if (tevent_req_nomem(subreq, req)) {
853 return tevent_req_post(req, ev);
855 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
856 return req;
859 static void tldap_sasl_bind_done(struct tevent_req *subreq)
861 struct tevent_req *req = tevent_req_callback_data(
862 subreq, struct tevent_req);
863 struct tldap_req_state *state = tevent_req_data(
864 req, struct tldap_req_state);
865 int err;
867 err = tldap_msg_recv(subreq, state, &state->result);
868 TALLOC_FREE(subreq);
869 if (err != TLDAP_SUCCESS) {
870 tevent_req_error(req, err);
871 return;
873 if (state->result->type != TLDAP_RES_BIND) {
874 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
875 return;
877 if (!asn1_start_tag(state->result->data, state->result->type) ||
878 !tldap_decode_response(state) ||
879 !asn1_end_tag(state->result->data)) {
880 tevent_req_error(req, TLDAP_DECODING_ERROR);
881 return;
884 * TODO: pull the reply blob
886 if (state->result->lderr != TLDAP_SUCCESS) {
887 tevent_req_error(req, state->result->lderr);
888 return;
890 tevent_req_done(req);
893 int tldap_sasl_bind_recv(struct tevent_req *req)
895 return tldap_simple_recv(req);
898 int tldap_sasl_bind(struct tldap_context *ld,
899 const char *dn,
900 const char *mechanism,
901 DATA_BLOB *creds,
902 struct tldap_control *sctrls,
903 int num_sctrls,
904 struct tldap_control *cctrls,
905 int num_cctrls)
907 TALLOC_CTX *frame = talloc_stackframe();
908 struct tevent_context *ev;
909 struct tevent_req *req;
910 int result;
912 ev = samba_tevent_context_init(frame);
913 if (ev == NULL) {
914 result = TLDAP_NO_MEMORY;
915 goto fail;
918 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
919 sctrls, num_sctrls, cctrls, num_cctrls);
920 if (req == NULL) {
921 result = TLDAP_NO_MEMORY;
922 goto fail;
925 if (!tevent_req_poll(req, ev)) {
926 result = TLDAP_OPERATIONS_ERROR;
927 goto fail;
930 result = tldap_sasl_bind_recv(req);
931 tldap_save_msg(ld, req);
932 fail:
933 TALLOC_FREE(frame);
934 return result;
937 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
938 struct tevent_context *ev,
939 struct tldap_context *ld,
940 const char *dn,
941 const char *passwd)
943 DATA_BLOB cred;
945 if (passwd != NULL) {
946 cred.data = discard_const_p(uint8_t, passwd);
947 cred.length = strlen(passwd);
948 } else {
949 cred.data = discard_const_p(uint8_t, "");
950 cred.length = 0;
952 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
953 NULL, 0);
956 int tldap_simple_bind_recv(struct tevent_req *req)
958 return tldap_sasl_bind_recv(req);
961 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
962 const char *passwd)
964 DATA_BLOB cred;
966 if (passwd != NULL) {
967 cred.data = discard_const_p(uint8_t, passwd);
968 cred.length = strlen(passwd);
969 } else {
970 cred.data = discard_const_p(uint8_t, "");
971 cred.length = 0;
973 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
976 /*****************************************************************************/
978 /* can't use isalpha() as only a strict set is valid for LDAP */
980 static bool tldap_is_alpha(char c)
982 return (((c >= 'a') && (c <= 'z')) || \
983 ((c >= 'A') && (c <= 'Z')));
986 static bool tldap_is_adh(char c)
988 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
991 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
992 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
993 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
994 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
995 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
996 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
997 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
998 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
999 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
1000 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
1002 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
1003 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
1004 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1007 /* oid's should be numerical only in theory,
1008 * but apparently some broken servers may have alphanum aliases instead.
1009 * Do like openldap libraries and allow alphanum aliases for oids, but
1010 * do not allow Tagging options in that case.
1012 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1014 bool is_oid = false;
1015 bool dot = false;
1016 int i;
1018 /* first char has stricter rules */
1019 if (isdigit(*s)) {
1020 is_oid = true;
1021 } else if (!tldap_is_alpha(*s)) {
1022 /* bad first char */
1023 return false;
1026 for (i = 1; i < len; i++) {
1028 if (is_oid) {
1029 if (isdigit(s[i])) {
1030 dot = false;
1031 continue;
1033 if (s[i] == '.') {
1034 if (dot) {
1035 /* malformed */
1036 return false;
1038 dot = true;
1039 continue;
1041 } else {
1042 if (tldap_is_adh(s[i])) {
1043 continue;
1047 if (s[i] == ';') {
1048 if (no_tagopts) {
1049 /* no tagging options */
1050 return false;
1052 if (dot) {
1053 /* malformed */
1054 return false;
1056 if ((i + 1) == len) {
1057 /* malformed */
1058 return false;
1061 is_oid = false;
1062 continue;
1066 if (dot) {
1067 /* malformed */
1068 return false;
1071 return true;
1074 /* this function copies the value until the closing parenthesis is found. */
1075 static char *tldap_get_val(TALLOC_CTX *memctx,
1076 const char *value, const char **_s)
1078 const char *s = value;
1080 /* find terminator */
1081 while (*s) {
1082 s = strchr(s, ')');
1083 if (s && (*(s - 1) == '\\')) {
1084 continue;
1086 break;
1088 if (!s || !(*s == ')')) {
1089 /* malformed filter */
1090 return NULL;
1093 *_s = s;
1095 return talloc_strndup(memctx, value, s - value);
1098 static int tldap_hex2char(const char *x)
1100 if (isxdigit(x[0]) && isxdigit(x[1])) {
1101 const char h1 = x[0], h2 = x[1];
1102 int c = 0;
1104 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1105 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1106 else if (h1 >= '0') c = h1 - (int)'0';
1107 c = c << 4;
1108 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1109 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1110 else if (h2 >= '0') c += h2 - (int)'0';
1112 return c;
1115 return -1;
1118 static bool tldap_find_first_star(const char *val, const char **star)
1120 const char *s;
1122 for (s = val; *s; s++) {
1123 switch (*s) {
1124 case '\\':
1125 if (isxdigit(s[1]) && isxdigit(s[2])) {
1126 s += 2;
1127 break;
1129 /* not hex based escape, check older syntax */
1130 switch (s[1]) {
1131 case '(':
1132 case ')':
1133 case '*':
1134 case '\\':
1135 s++;
1136 break;
1137 default:
1138 /* invalid escape sequence */
1139 return false;
1141 break;
1142 case ')':
1143 /* end of val, nothing found */
1144 *star = s;
1145 return true;
1147 case '*':
1148 *star = s;
1149 return true;
1153 /* string ended without closing parenthesis, filter is malformed */
1154 return false;
1157 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1159 int c, i, p;
1161 for (i = 0,p = 0; i < *val_len; i++) {
1163 switch (value[i]) {
1164 case '(':
1165 case ')':
1166 case '*':
1167 /* these must be escaped */
1168 return false;
1170 case '\\':
1171 if (!value[i + 1]) {
1172 /* invalid EOL */
1173 return false;
1175 i++;
1177 c = tldap_hex2char(&value[i]);
1178 if (c >= 0 && c < 256) {
1179 value[p] = c;
1180 i++;
1181 p++;
1182 break;
1185 switch (value[i]) {
1186 case '(':
1187 case ')':
1188 case '*':
1189 case '\\':
1190 value[p] = value[i];
1191 p++;
1192 default:
1193 /* invalid */
1194 return false;
1196 break;
1198 default:
1199 value[p] = value[i];
1200 p++;
1203 value[p] = '\0';
1204 *val_len = p;
1205 return true;
1208 static bool tldap_push_filter_basic(struct tldap_context *ld,
1209 struct asn1_data *data,
1210 const char **_s);
1211 static bool tldap_push_filter_substring(struct tldap_context *ld,
1212 struct asn1_data *data,
1213 const char *val,
1214 const char **_s);
1215 static bool tldap_push_filter_int(struct tldap_context *ld,
1216 struct asn1_data *data,
1217 const char **_s)
1219 const char *s = *_s;
1220 bool ret;
1222 if (*s != '(') {
1223 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1224 "Incomplete or malformed filter\n");
1225 return false;
1227 s++;
1229 /* we are right after a parenthesis,
1230 * find out what op we have at hand */
1231 switch (*s) {
1232 case '&':
1233 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1234 asn1_push_tag(data, TLDAP_FILTER_AND);
1235 s++;
1236 break;
1238 case '|':
1239 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1240 asn1_push_tag(data, TLDAP_FILTER_OR);
1241 s++;
1242 break;
1244 case '!':
1245 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1246 asn1_push_tag(data, TLDAP_FILTER_NOT);
1247 s++;
1248 ret = tldap_push_filter_int(ld, data, &s);
1249 if (!ret) {
1250 return false;
1252 asn1_pop_tag(data);
1253 goto done;
1255 case '(':
1256 case ')':
1257 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1258 "Invalid parenthesis '%c'\n", *s);
1259 return false;
1261 case '\0':
1262 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1263 "Invalid filter termination\n");
1264 return false;
1266 default:
1267 ret = tldap_push_filter_basic(ld, data, &s);
1268 if (!ret) {
1269 return false;
1271 goto done;
1274 /* only and/or filters get here.
1275 * go through the list of filters */
1277 if (*s == ')') {
1278 /* RFC 4526: empty and/or */
1279 asn1_pop_tag(data);
1280 goto done;
1283 while (*s) {
1284 ret = tldap_push_filter_int(ld, data, &s);
1285 if (!ret) {
1286 return false;
1289 if (*s == ')') {
1290 /* end of list, return */
1291 asn1_pop_tag(data);
1292 break;
1296 done:
1297 if (*s != ')') {
1298 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1299 "Incomplete or malformed filter\n");
1300 return false;
1302 s++;
1304 if (data->has_error) {
1305 return false;
1308 *_s = s;
1309 return true;
1313 static bool tldap_push_filter_basic(struct tldap_context *ld,
1314 struct asn1_data *data,
1315 const char **_s)
1317 TALLOC_CTX *tmpctx = talloc_tos();
1318 const char *s = *_s;
1319 const char *e;
1320 const char *eq;
1321 const char *val;
1322 const char *type;
1323 const char *dn;
1324 const char *rule;
1325 const char *star;
1326 size_t type_len = 0;
1327 char *uval;
1328 size_t uval_len;
1329 bool write_octect = true;
1330 bool ret;
1332 eq = strchr(s, '=');
1333 if (!eq) {
1334 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1335 "Invalid filter, missing equal sign\n");
1336 return false;
1339 val = eq + 1;
1340 e = eq - 1;
1342 switch (*e) {
1343 case '<':
1344 asn1_push_tag(data, TLDAP_FILTER_LE);
1345 break;
1347 case '>':
1348 asn1_push_tag(data, TLDAP_FILTER_GE);
1349 break;
1351 case '~':
1352 asn1_push_tag(data, TLDAP_FILTER_APX);
1353 break;
1355 case ':':
1356 asn1_push_tag(data, TLDAP_FILTER_EXT);
1357 write_octect = false;
1359 type = NULL;
1360 dn = NULL;
1361 rule = NULL;
1363 if (*s == ':') { /* [:dn]:rule:= value */
1364 if (s == e) {
1365 /* malformed filter */
1366 return false;
1368 dn = s;
1369 } else { /* type[:dn][:rule]:= value */
1370 type = s;
1371 dn = strchr(s, ':');
1372 type_len = dn - type;
1373 if (dn == e) { /* type:= value */
1374 dn = NULL;
1377 if (dn) {
1378 dn++;
1380 rule = strchr(dn, ':');
1381 if (rule == NULL) {
1382 return false;
1384 if ((rule == dn + 1) || rule + 1 == e) {
1385 /* malformed filter, contains "::" */
1386 return false;
1389 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1390 if (rule == e) {
1391 rule = dn;
1392 dn = NULL;
1393 } else {
1394 /* malformed filter. With two
1395 * optionals, the first must be "dn"
1397 return false;
1399 } else {
1400 if (rule == e) {
1401 rule = NULL;
1402 } else {
1403 rule++;
1408 if (!type && !dn && !rule) {
1409 /* malformed filter, there must be at least one */
1410 return false;
1414 MatchingRuleAssertion ::= SEQUENCE {
1415 matchingRule [1] MatchingRuleID OPTIONAL,
1416 type [2] AttributeDescription OPTIONAL,
1417 matchValue [3] AssertionValue,
1418 dnAttributes [4] BOOLEAN DEFAULT FALSE
1422 /* check and add rule */
1423 if (rule) {
1424 ret = tldap_is_attrdesc(rule, e - rule, true);
1425 if (!ret) {
1426 return false;
1428 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1429 asn1_write(data, rule, e - rule);
1430 asn1_pop_tag(data);
1433 /* check and add type */
1434 if (type) {
1435 ret = tldap_is_attrdesc(type, type_len, false);
1436 if (!ret) {
1437 return false;
1439 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1440 asn1_write(data, type, type_len);
1441 asn1_pop_tag(data);
1444 uval = tldap_get_val(tmpctx, val, _s);
1445 if (!uval) {
1446 return false;
1448 uval_len = *_s - val;
1449 ret = tldap_unescape_inplace(uval, &uval_len);
1450 if (!ret) {
1451 return false;
1454 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1455 asn1_write(data, uval, uval_len);
1456 asn1_pop_tag(data);
1458 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1459 asn1_write_uint8(data, dn?1:0);
1460 asn1_pop_tag(data);
1461 break;
1463 default:
1464 e = eq;
1466 ret = tldap_is_attrdesc(s, e - s, false);
1467 if (!ret) {
1468 return false;
1471 if (strncmp(val, "*)", 2) == 0) {
1472 /* presence */
1473 asn1_push_tag(data, TLDAP_FILTER_PRES);
1474 asn1_write(data, s, e - s);
1475 *_s = val + 1;
1476 write_octect = false;
1477 break;
1480 ret = tldap_find_first_star(val, &star);
1481 if (!ret) {
1482 return false;
1484 if (*star == '*') {
1485 /* substring */
1486 asn1_push_tag(data, TLDAP_FILTER_SUB);
1487 asn1_write_OctetString(data, s, e - s);
1488 ret = tldap_push_filter_substring(ld, data, val, &s);
1489 if (!ret) {
1490 return false;
1492 *_s = s;
1493 write_octect = false;
1494 break;
1497 /* if nothing else, then it is just equality */
1498 asn1_push_tag(data, TLDAP_FILTER_EQ);
1499 write_octect = true;
1500 break;
1503 if (write_octect) {
1504 uval = tldap_get_val(tmpctx, val, _s);
1505 if (!uval) {
1506 return false;
1508 uval_len = *_s - val;
1509 ret = tldap_unescape_inplace(uval, &uval_len);
1510 if (!ret) {
1511 return false;
1514 asn1_write_OctetString(data, s, e - s);
1515 asn1_write_OctetString(data, uval, uval_len);
1518 if (data->has_error) {
1519 return false;
1521 asn1_pop_tag(data);
1522 return true;
1525 static bool tldap_push_filter_substring(struct tldap_context *ld,
1526 struct asn1_data *data,
1527 const char *val,
1528 const char **_s)
1530 TALLOC_CTX *tmpctx = talloc_tos();
1531 bool initial = true;
1532 const char *star;
1533 char *chunk;
1534 size_t chunk_len;
1535 bool ret;
1538 SubstringFilter ::= SEQUENCE {
1539 type AttributeDescription,
1540 -- at least one must be present
1541 substrings SEQUENCE OF CHOICE {
1542 initial [0] LDAPString,
1543 any [1] LDAPString,
1544 final [2] LDAPString } }
1546 asn1_push_tag(data, ASN1_SEQUENCE(0));
1548 do {
1549 ret = tldap_find_first_star(val, &star);
1550 if (!ret) {
1551 return false;
1553 chunk_len = star - val;
1555 switch (*star) {
1556 case '*':
1557 if (!initial && chunk_len == 0) {
1558 /* found '**', which is illegal */
1559 return false;
1561 break;
1562 case ')':
1563 if (initial) {
1564 /* no stars ?? */
1565 return false;
1567 /* we are done */
1568 break;
1569 default:
1570 /* ?? */
1571 return false;
1574 if (initial && chunk_len == 0) {
1575 val = star + 1;
1576 initial = false;
1577 continue;
1580 chunk = talloc_strndup(tmpctx, val, chunk_len);
1581 if (!chunk) {
1582 return false;
1584 ret = tldap_unescape_inplace(chunk, &chunk_len);
1585 if (!ret) {
1586 return false;
1588 switch (*star) {
1589 case '*':
1590 if (initial) {
1591 asn1_push_tag(data, TLDAP_SUB_INI);
1592 initial = false;
1593 } else {
1594 asn1_push_tag(data, TLDAP_SUB_ANY);
1596 break;
1597 case ')':
1598 asn1_push_tag(data, TLDAP_SUB_FIN);
1599 break;
1600 default:
1601 /* ?? */
1602 return false;
1604 asn1_write(data, chunk, chunk_len);
1605 asn1_pop_tag(data);
1607 val = star + 1;
1609 } while (*star == '*');
1611 *_s = star;
1613 /* end of sequence */
1614 asn1_pop_tag(data);
1615 return true;
1618 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1619 * around parenthesis, we do not allow any spaces (except in values of
1620 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1621 * leading or trailing spaces where allowed.
1623 static bool tldap_push_filter(struct tldap_context *ld,
1624 struct asn1_data *data,
1625 const char *filter)
1627 const char *s = filter;
1628 bool ret;
1630 ret = tldap_push_filter_int(ld, data, &s);
1631 if (ret && *s) {
1632 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1633 "Incomplete or malformed filter\n");
1634 return false;
1636 return ret;
1639 /*****************************************************************************/
1641 static void tldap_search_done(struct tevent_req *subreq);
1643 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1644 struct tevent_context *ev,
1645 struct tldap_context *ld,
1646 const char *base, int scope,
1647 const char *filter,
1648 const char **attrs,
1649 int num_attrs,
1650 int attrsonly,
1651 struct tldap_control *sctrls,
1652 int num_sctrls,
1653 struct tldap_control *cctrls,
1654 int num_cctrls,
1655 int timelimit,
1656 int sizelimit,
1657 int deref)
1659 struct tevent_req *req, *subreq;
1660 struct tldap_req_state *state;
1661 int i;
1663 req = tldap_req_create(mem_ctx, ld, &state);
1664 if (req == NULL) {
1665 return NULL;
1668 asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1669 asn1_write_OctetString(state->out, base, strlen(base));
1670 asn1_write_enumerated(state->out, scope);
1671 asn1_write_enumerated(state->out, deref);
1672 asn1_write_Integer(state->out, sizelimit);
1673 asn1_write_Integer(state->out, timelimit);
1674 asn1_write_BOOLEAN(state->out, attrsonly);
1676 if (!tldap_push_filter(ld, state->out, filter)) {
1677 goto encoding_error;
1680 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1681 for (i=0; i<num_attrs; i++) {
1682 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1684 asn1_pop_tag(state->out);
1685 asn1_pop_tag(state->out);
1687 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1688 sctrls, num_sctrls);
1689 if (tevent_req_nomem(subreq, req)) {
1690 return tevent_req_post(req, ev);
1692 tevent_req_set_callback(subreq, tldap_search_done, req);
1693 return req;
1695 encoding_error:
1696 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1697 return tevent_req_post(req, ev);
1700 static void tldap_search_done(struct tevent_req *subreq)
1702 struct tevent_req *req = tevent_req_callback_data(
1703 subreq, struct tevent_req);
1704 struct tldap_req_state *state = tevent_req_data(
1705 req, struct tldap_req_state);
1706 int err;
1708 err = tldap_msg_recv(subreq, state, &state->result);
1709 if (err != TLDAP_SUCCESS) {
1710 tevent_req_error(req, err);
1711 return;
1713 switch (state->result->type) {
1714 case TLDAP_RES_SEARCH_ENTRY:
1715 case TLDAP_RES_SEARCH_REFERENCE:
1716 if (!tldap_msg_set_pending(subreq)) {
1717 tevent_req_oom(req);
1718 return;
1720 tevent_req_notify_callback(req);
1721 break;
1722 case TLDAP_RES_SEARCH_RESULT:
1723 TALLOC_FREE(subreq);
1724 if (!asn1_start_tag(state->result->data,
1725 state->result->type) ||
1726 !tldap_decode_response(state) ||
1727 !asn1_end_tag(state->result->data) ||
1728 !tldap_decode_controls(state)) {
1729 tevent_req_error(req, TLDAP_DECODING_ERROR);
1730 return;
1732 tevent_req_done(req);
1733 break;
1734 default:
1735 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1736 return;
1740 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1741 struct tldap_message **pmsg)
1743 struct tldap_req_state *state = tevent_req_data(
1744 req, struct tldap_req_state);
1745 int err;
1747 if (!tevent_req_is_in_progress(req)
1748 && tevent_req_is_ldap_error(req, &err)) {
1749 return err;
1752 if (tevent_req_is_in_progress(req)) {
1753 switch (state->result->type) {
1754 case TLDAP_RES_SEARCH_ENTRY:
1755 case TLDAP_RES_SEARCH_REFERENCE:
1756 break;
1757 default:
1758 return TLDAP_OPERATIONS_ERROR;
1762 *pmsg = talloc_move(mem_ctx, &state->result);
1763 return TLDAP_SUCCESS;
1766 struct tldap_sync_search_state {
1767 TALLOC_CTX *mem_ctx;
1768 struct tldap_message **entries;
1769 struct tldap_message **refs;
1770 int rc;
1773 static void tldap_search_cb(struct tevent_req *req)
1775 struct tldap_sync_search_state *state =
1776 (struct tldap_sync_search_state *)
1777 tevent_req_callback_data_void(req);
1778 struct tldap_message *msg, **tmp;
1779 int rc, num_entries, num_refs;
1781 rc = tldap_search_recv(req, talloc_tos(), &msg);
1782 if (rc != TLDAP_SUCCESS) {
1783 state->rc = rc;
1784 return;
1787 switch (tldap_msg_type(msg)) {
1788 case TLDAP_RES_SEARCH_ENTRY:
1789 num_entries = talloc_array_length(state->entries);
1790 tmp = talloc_realloc(state->mem_ctx, state->entries,
1791 struct tldap_message *, num_entries + 1);
1792 if (tmp == NULL) {
1793 state->rc = TLDAP_NO_MEMORY;
1794 return;
1796 state->entries = tmp;
1797 state->entries[num_entries] = talloc_move(state->entries,
1798 &msg);
1799 break;
1800 case TLDAP_RES_SEARCH_REFERENCE:
1801 num_refs = talloc_array_length(state->refs);
1802 tmp = talloc_realloc(state->mem_ctx, state->refs,
1803 struct tldap_message *, num_refs + 1);
1804 if (tmp == NULL) {
1805 state->rc = TLDAP_NO_MEMORY;
1806 return;
1808 state->refs = tmp;
1809 state->refs[num_refs] = talloc_move(state->refs, &msg);
1810 break;
1811 case TLDAP_RES_SEARCH_RESULT:
1812 state->rc = TLDAP_SUCCESS;
1813 break;
1814 default:
1815 state->rc = TLDAP_PROTOCOL_ERROR;
1816 break;
1820 int tldap_search(struct tldap_context *ld,
1821 const char *base, int scope, const char *filter,
1822 const char **attrs, int num_attrs, int attrsonly,
1823 struct tldap_control *sctrls, int num_sctrls,
1824 struct tldap_control *cctrls, int num_cctrls,
1825 int timelimit, int sizelimit, int deref,
1826 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1827 struct tldap_message ***refs)
1829 TALLOC_CTX *frame = talloc_stackframe();
1830 struct tevent_context *ev;
1831 struct tevent_req *req;
1832 struct tldap_sync_search_state state;
1834 ZERO_STRUCT(state);
1835 state.mem_ctx = mem_ctx;
1836 state.rc = TLDAP_SUCCESS;
1838 ev = samba_tevent_context_init(frame);
1839 if (ev == NULL) {
1840 state.rc = TLDAP_NO_MEMORY;
1841 goto fail;
1844 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1845 attrs, num_attrs, attrsonly,
1846 sctrls, num_sctrls, cctrls, num_cctrls,
1847 timelimit, sizelimit, deref);
1848 if (req == NULL) {
1849 state.rc = TLDAP_NO_MEMORY;
1850 goto fail;
1853 tevent_req_set_callback(req, tldap_search_cb, &state);
1855 if (!tevent_req_is_in_progress(req)) {
1856 /* an error happend before sending */
1857 if (tevent_req_is_ldap_error(req, &state.rc)) {
1858 goto fail;
1862 while (tevent_req_is_in_progress(req)
1863 && (state.rc == TLDAP_SUCCESS)) {
1864 if (tevent_loop_once(ev) == -1) {
1865 return TLDAP_OPERATIONS_ERROR;
1869 if (state.rc != TLDAP_SUCCESS) {
1870 return state.rc;
1873 if (entries != NULL) {
1874 *entries = state.entries;
1875 } else {
1876 TALLOC_FREE(state.entries);
1878 if (refs != NULL) {
1879 *refs = state.refs;
1880 } else {
1881 TALLOC_FREE(state.refs);
1883 tldap_save_msg(ld, req);
1884 fail:
1885 TALLOC_FREE(frame);
1886 return state.rc;
1889 static bool tldap_parse_search_entry(struct tldap_message *msg)
1891 int num_attribs = 0;
1893 asn1_start_tag(msg->data, msg->type);
1895 /* dn */
1897 asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1898 if (msg->dn == NULL) {
1899 return false;
1903 * Attributes: We overallocate msg->attribs by one, so that while
1904 * looping over the attributes we can directly parse into the last
1905 * array element. Same for the values in the inner loop.
1908 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1909 if (msg->attribs == NULL) {
1910 return false;
1913 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1914 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1915 struct tldap_attribute *attrib;
1916 int num_values = 0;
1918 attrib = &msg->attribs[num_attribs];
1919 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1920 if (attrib->values == NULL) {
1921 return false;
1923 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1924 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1925 &attrib->name);
1926 asn1_start_tag(msg->data, ASN1_SET);
1928 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1929 asn1_read_OctetString(msg->data, msg,
1930 &attrib->values[num_values]);
1932 attrib->values = talloc_realloc(
1933 msg->attribs, attrib->values, DATA_BLOB,
1934 num_values + 2);
1935 if (attrib->values == NULL) {
1936 return false;
1938 num_values += 1;
1940 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1941 DATA_BLOB, num_values);
1942 attrib->num_values = num_values;
1944 asn1_end_tag(msg->data); /* ASN1_SET */
1945 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1946 msg->attribs = talloc_realloc(
1947 msg, msg->attribs, struct tldap_attribute,
1948 num_attribs + 2);
1949 if (msg->attribs == NULL) {
1950 return false;
1952 num_attribs += 1;
1954 msg->attribs = talloc_realloc(
1955 msg, msg->attribs, struct tldap_attribute, num_attribs);
1956 asn1_end_tag(msg->data);
1957 if (msg->data->has_error) {
1958 return false;
1960 return true;
1963 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1965 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1966 return false;
1968 *dn = msg->dn;
1969 return true;
1972 bool tldap_entry_attributes(struct tldap_message *msg,
1973 struct tldap_attribute **attributes,
1974 int *num_attributes)
1976 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1977 return false;
1979 *attributes = msg->attribs;
1980 *num_attributes = talloc_array_length(msg->attribs);
1981 return true;
1984 static bool tldap_decode_controls(struct tldap_req_state *state)
1986 struct tldap_message *msg = state->result;
1987 struct asn1_data *data = msg->data;
1988 struct tldap_control *sctrls = NULL;
1989 int num_controls = 0;
1991 msg->res_sctrls = NULL;
1993 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1994 return true;
1997 asn1_start_tag(data, ASN1_CONTEXT(0));
1999 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
2000 struct tldap_control *c;
2001 char *oid = NULL;
2003 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
2004 num_controls + 1);
2005 if (sctrls == NULL) {
2006 return false;
2008 c = &sctrls[num_controls];
2010 asn1_start_tag(data, ASN1_SEQUENCE(0));
2011 asn1_read_OctetString_talloc(msg, data, &oid);
2012 if ((data->has_error) || (oid == NULL)) {
2013 return false;
2015 c->oid = oid;
2016 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2017 asn1_read_BOOLEAN(data, &c->critical);
2018 } else {
2019 c->critical = false;
2021 c->value = data_blob_null;
2022 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2023 !asn1_read_OctetString(data, msg, &c->value)) {
2024 return false;
2026 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
2028 num_controls += 1;
2031 asn1_end_tag(data); /* ASN1_CONTEXT(0) */
2033 if (data->has_error) {
2034 TALLOC_FREE(sctrls);
2035 return false;
2037 msg->res_sctrls = sctrls;
2038 return true;
2041 static void tldap_simple_done(struct tevent_req *subreq, int type)
2043 struct tevent_req *req = tevent_req_callback_data(
2044 subreq, struct tevent_req);
2045 struct tldap_req_state *state = tevent_req_data(
2046 req, struct tldap_req_state);
2047 int err;
2049 err = tldap_msg_recv(subreq, state, &state->result);
2050 TALLOC_FREE(subreq);
2051 if (err != TLDAP_SUCCESS) {
2052 tevent_req_error(req, err);
2053 return;
2055 if (state->result->type != type) {
2056 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2057 return;
2059 if (!asn1_start_tag(state->result->data, state->result->type) ||
2060 !tldap_decode_response(state) ||
2061 !asn1_end_tag(state->result->data) ||
2062 !tldap_decode_controls(state)) {
2063 tevent_req_error(req, TLDAP_DECODING_ERROR);
2064 return;
2066 if (state->result->lderr != TLDAP_SUCCESS) {
2067 tevent_req_error(req, state->result->lderr);
2068 return;
2070 tevent_req_done(req);
2073 static int tldap_simple_recv(struct tevent_req *req)
2075 int err;
2076 if (tevent_req_is_ldap_error(req, &err)) {
2077 return err;
2079 return TLDAP_SUCCESS;
2082 static void tldap_add_done(struct tevent_req *subreq);
2084 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2085 struct tevent_context *ev,
2086 struct tldap_context *ld,
2087 const char *dn,
2088 struct tldap_mod *attributes,
2089 int num_attributes,
2090 struct tldap_control *sctrls,
2091 int num_sctrls,
2092 struct tldap_control *cctrls,
2093 int num_cctrls)
2095 struct tevent_req *req, *subreq;
2096 struct tldap_req_state *state;
2097 int i, j;
2099 req = tldap_req_create(mem_ctx, ld, &state);
2100 if (req == NULL) {
2101 return NULL;
2104 asn1_push_tag(state->out, TLDAP_REQ_ADD);
2105 asn1_write_OctetString(state->out, dn, strlen(dn));
2106 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2108 for (i=0; i<num_attributes; i++) {
2109 struct tldap_mod *attrib = &attributes[i];
2110 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2111 asn1_write_OctetString(state->out, attrib->attribute,
2112 strlen(attrib->attribute));
2113 asn1_push_tag(state->out, ASN1_SET);
2114 for (j=0; j<attrib->num_values; j++) {
2115 asn1_write_OctetString(state->out,
2116 attrib->values[j].data,
2117 attrib->values[j].length);
2119 asn1_pop_tag(state->out);
2120 asn1_pop_tag(state->out);
2123 asn1_pop_tag(state->out);
2124 asn1_pop_tag(state->out);
2126 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2127 sctrls, num_sctrls);
2128 if (tevent_req_nomem(subreq, req)) {
2129 return tevent_req_post(req, ev);
2131 tevent_req_set_callback(subreq, tldap_add_done, req);
2132 return req;
2135 static void tldap_add_done(struct tevent_req *subreq)
2137 tldap_simple_done(subreq, TLDAP_RES_ADD);
2140 int tldap_add_recv(struct tevent_req *req)
2142 return tldap_simple_recv(req);
2145 int tldap_add(struct tldap_context *ld, const char *dn,
2146 struct tldap_mod *attributes, int num_attributes,
2147 struct tldap_control *sctrls, int num_sctrls,
2148 struct tldap_control *cctrls, int num_cctrls)
2150 TALLOC_CTX *frame = talloc_stackframe();
2151 struct tevent_context *ev;
2152 struct tevent_req *req;
2153 int result;
2155 ev = samba_tevent_context_init(frame);
2156 if (ev == NULL) {
2157 result = TLDAP_NO_MEMORY;
2158 goto fail;
2161 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2162 sctrls, num_sctrls, cctrls, num_cctrls);
2163 if (req == NULL) {
2164 result = TLDAP_NO_MEMORY;
2165 goto fail;
2168 if (!tevent_req_poll(req, ev)) {
2169 result = TLDAP_OPERATIONS_ERROR;
2170 goto fail;
2173 result = tldap_add_recv(req);
2174 tldap_save_msg(ld, req);
2175 fail:
2176 TALLOC_FREE(frame);
2177 return result;
2180 static void tldap_modify_done(struct tevent_req *subreq);
2182 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2183 struct tevent_context *ev,
2184 struct tldap_context *ld,
2185 const char *dn,
2186 struct tldap_mod *mods, int num_mods,
2187 struct tldap_control *sctrls,
2188 int num_sctrls,
2189 struct tldap_control *cctrls,
2190 int num_cctrls)
2192 struct tevent_req *req, *subreq;
2193 struct tldap_req_state *state;
2194 int i, j;
2196 req = tldap_req_create(mem_ctx, ld, &state);
2197 if (req == NULL) {
2198 return NULL;
2201 asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
2202 asn1_write_OctetString(state->out, dn, strlen(dn));
2203 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2205 for (i=0; i<num_mods; i++) {
2206 struct tldap_mod *mod = &mods[i];
2207 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2208 asn1_write_enumerated(state->out, mod->mod_op),
2209 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2210 asn1_write_OctetString(state->out, mod->attribute,
2211 strlen(mod->attribute));
2212 asn1_push_tag(state->out, ASN1_SET);
2213 for (j=0; j<mod->num_values; j++) {
2214 asn1_write_OctetString(state->out,
2215 mod->values[j].data,
2216 mod->values[j].length);
2218 asn1_pop_tag(state->out);
2219 asn1_pop_tag(state->out);
2220 asn1_pop_tag(state->out);
2223 asn1_pop_tag(state->out);
2224 asn1_pop_tag(state->out);
2226 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2227 sctrls, num_sctrls);
2228 if (tevent_req_nomem(subreq, req)) {
2229 return tevent_req_post(req, ev);
2231 tevent_req_set_callback(subreq, tldap_modify_done, req);
2232 return req;
2235 static void tldap_modify_done(struct tevent_req *subreq)
2237 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2240 int tldap_modify_recv(struct tevent_req *req)
2242 return tldap_simple_recv(req);
2245 int tldap_modify(struct tldap_context *ld, const char *dn,
2246 struct tldap_mod *mods, int num_mods,
2247 struct tldap_control *sctrls, int num_sctrls,
2248 struct tldap_control *cctrls, int num_cctrls)
2250 TALLOC_CTX *frame = talloc_stackframe();
2251 struct tevent_context *ev;
2252 struct tevent_req *req;
2253 int result;
2255 ev = samba_tevent_context_init(frame);
2256 if (ev == NULL) {
2257 result = TLDAP_NO_MEMORY;
2258 goto fail;
2261 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2262 sctrls, num_sctrls, cctrls, num_cctrls);
2263 if (req == NULL) {
2264 result = TLDAP_NO_MEMORY;
2265 goto fail;
2268 if (!tevent_req_poll(req, ev)) {
2269 result = TLDAP_OPERATIONS_ERROR;
2270 goto fail;
2273 result = tldap_modify_recv(req);
2274 tldap_save_msg(ld, req);
2275 fail:
2276 TALLOC_FREE(frame);
2277 return result;
2280 static void tldap_delete_done(struct tevent_req *subreq);
2282 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2283 struct tevent_context *ev,
2284 struct tldap_context *ld,
2285 const char *dn,
2286 struct tldap_control *sctrls,
2287 int num_sctrls,
2288 struct tldap_control *cctrls,
2289 int num_cctrls)
2291 struct tevent_req *req, *subreq;
2292 struct tldap_req_state *state;
2294 req = tldap_req_create(mem_ctx, ld, &state);
2295 if (req == NULL) {
2296 return NULL;
2299 asn1_push_tag(state->out, TLDAP_REQ_DELETE);
2300 asn1_write(state->out, dn, strlen(dn));
2301 asn1_pop_tag(state->out);
2303 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2304 sctrls, num_sctrls);
2305 if (tevent_req_nomem(subreq, req)) {
2306 return tevent_req_post(req, ev);
2308 tevent_req_set_callback(subreq, tldap_delete_done, req);
2309 return req;
2312 static void tldap_delete_done(struct tevent_req *subreq)
2314 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2317 int tldap_delete_recv(struct tevent_req *req)
2319 return tldap_simple_recv(req);
2322 int tldap_delete(struct tldap_context *ld, const char *dn,
2323 struct tldap_control *sctrls, int num_sctrls,
2324 struct tldap_control *cctrls, int num_cctrls)
2326 TALLOC_CTX *frame = talloc_stackframe();
2327 struct tevent_context *ev;
2328 struct tevent_req *req;
2329 int result;
2331 ev = samba_tevent_context_init(frame);
2332 if (ev == NULL) {
2333 result = TLDAP_NO_MEMORY;
2334 goto fail;
2337 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2338 cctrls, num_cctrls);
2339 if (req == NULL) {
2340 result = TLDAP_NO_MEMORY;
2341 goto fail;
2344 if (!tevent_req_poll(req, ev)) {
2345 result = TLDAP_OPERATIONS_ERROR;
2346 goto fail;
2349 result = tldap_delete_recv(req);
2350 tldap_save_msg(ld, req);
2351 fail:
2352 TALLOC_FREE(frame);
2353 return result;
2356 int tldap_msg_id(const struct tldap_message *msg)
2358 return msg->id;
2361 int tldap_msg_type(const struct tldap_message *msg)
2363 return msg->type;
2366 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2368 if (msg == NULL) {
2369 return NULL;
2371 return msg->res_matcheddn;
2374 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2376 if (msg == NULL) {
2377 return NULL;
2379 return msg->res_diagnosticmessage;
2382 const char *tldap_msg_referral(struct tldap_message *msg)
2384 if (msg == NULL) {
2385 return NULL;
2387 return msg->res_referral;
2390 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2391 struct tldap_control **sctrls)
2393 if (msg == NULL) {
2394 *sctrls = NULL;
2395 *num_sctrls = 0;
2396 return;
2398 *sctrls = msg->res_sctrls;
2399 *num_sctrls = talloc_array_length(msg->res_sctrls);
2402 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2404 return ld->last_msg;
2407 const char *tldap_err2string(int rc)
2409 const char *res = NULL;
2412 * This would normally be a table, but the error codes are not fully
2413 * sequential. Let the compiler figure out the optimum implementation
2414 * :-)
2417 switch (rc) {
2418 case TLDAP_SUCCESS:
2419 res = "TLDAP_SUCCESS";
2420 break;
2421 case TLDAP_OPERATIONS_ERROR:
2422 res = "TLDAP_OPERATIONS_ERROR";
2423 break;
2424 case TLDAP_PROTOCOL_ERROR:
2425 res = "TLDAP_PROTOCOL_ERROR";
2426 break;
2427 case TLDAP_TIMELIMIT_EXCEEDED:
2428 res = "TLDAP_TIMELIMIT_EXCEEDED";
2429 break;
2430 case TLDAP_SIZELIMIT_EXCEEDED:
2431 res = "TLDAP_SIZELIMIT_EXCEEDED";
2432 break;
2433 case TLDAP_COMPARE_FALSE:
2434 res = "TLDAP_COMPARE_FALSE";
2435 break;
2436 case TLDAP_COMPARE_TRUE:
2437 res = "TLDAP_COMPARE_TRUE";
2438 break;
2439 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2440 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2441 break;
2442 case TLDAP_STRONG_AUTH_REQUIRED:
2443 res = "TLDAP_STRONG_AUTH_REQUIRED";
2444 break;
2445 case TLDAP_REFERRAL:
2446 res = "TLDAP_REFERRAL";
2447 break;
2448 case TLDAP_ADMINLIMIT_EXCEEDED:
2449 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2450 break;
2451 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2452 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2453 break;
2454 case TLDAP_CONFIDENTIALITY_REQUIRED:
2455 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2456 break;
2457 case TLDAP_SASL_BIND_IN_PROGRESS:
2458 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2459 break;
2460 case TLDAP_NO_SUCH_ATTRIBUTE:
2461 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2462 break;
2463 case TLDAP_UNDEFINED_TYPE:
2464 res = "TLDAP_UNDEFINED_TYPE";
2465 break;
2466 case TLDAP_INAPPROPRIATE_MATCHING:
2467 res = "TLDAP_INAPPROPRIATE_MATCHING";
2468 break;
2469 case TLDAP_CONSTRAINT_VIOLATION:
2470 res = "TLDAP_CONSTRAINT_VIOLATION";
2471 break;
2472 case TLDAP_TYPE_OR_VALUE_EXISTS:
2473 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2474 break;
2475 case TLDAP_INVALID_SYNTAX:
2476 res = "TLDAP_INVALID_SYNTAX";
2477 break;
2478 case TLDAP_NO_SUCH_OBJECT:
2479 res = "TLDAP_NO_SUCH_OBJECT";
2480 break;
2481 case TLDAP_ALIAS_PROBLEM:
2482 res = "TLDAP_ALIAS_PROBLEM";
2483 break;
2484 case TLDAP_INVALID_DN_SYNTAX:
2485 res = "TLDAP_INVALID_DN_SYNTAX";
2486 break;
2487 case TLDAP_IS_LEAF:
2488 res = "TLDAP_IS_LEAF";
2489 break;
2490 case TLDAP_ALIAS_DEREF_PROBLEM:
2491 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2492 break;
2493 case TLDAP_INAPPROPRIATE_AUTH:
2494 res = "TLDAP_INAPPROPRIATE_AUTH";
2495 break;
2496 case TLDAP_INVALID_CREDENTIALS:
2497 res = "TLDAP_INVALID_CREDENTIALS";
2498 break;
2499 case TLDAP_INSUFFICIENT_ACCESS:
2500 res = "TLDAP_INSUFFICIENT_ACCESS";
2501 break;
2502 case TLDAP_BUSY:
2503 res = "TLDAP_BUSY";
2504 break;
2505 case TLDAP_UNAVAILABLE:
2506 res = "TLDAP_UNAVAILABLE";
2507 break;
2508 case TLDAP_UNWILLING_TO_PERFORM:
2509 res = "TLDAP_UNWILLING_TO_PERFORM";
2510 break;
2511 case TLDAP_LOOP_DETECT:
2512 res = "TLDAP_LOOP_DETECT";
2513 break;
2514 case TLDAP_NAMING_VIOLATION:
2515 res = "TLDAP_NAMING_VIOLATION";
2516 break;
2517 case TLDAP_OBJECT_CLASS_VIOLATION:
2518 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2519 break;
2520 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2521 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2522 break;
2523 case TLDAP_NOT_ALLOWED_ON_RDN:
2524 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2525 break;
2526 case TLDAP_ALREADY_EXISTS:
2527 res = "TLDAP_ALREADY_EXISTS";
2528 break;
2529 case TLDAP_NO_OBJECT_CLASS_MODS:
2530 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2531 break;
2532 case TLDAP_RESULTS_TOO_LARGE:
2533 res = "TLDAP_RESULTS_TOO_LARGE";
2534 break;
2535 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2536 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2537 break;
2538 case TLDAP_OTHER:
2539 res = "TLDAP_OTHER";
2540 break;
2541 case TLDAP_SERVER_DOWN:
2542 res = "TLDAP_SERVER_DOWN";
2543 break;
2544 case TLDAP_LOCAL_ERROR:
2545 res = "TLDAP_LOCAL_ERROR";
2546 break;
2547 case TLDAP_ENCODING_ERROR:
2548 res = "TLDAP_ENCODING_ERROR";
2549 break;
2550 case TLDAP_DECODING_ERROR:
2551 res = "TLDAP_DECODING_ERROR";
2552 break;
2553 case TLDAP_TIMEOUT:
2554 res = "TLDAP_TIMEOUT";
2555 break;
2556 case TLDAP_AUTH_UNKNOWN:
2557 res = "TLDAP_AUTH_UNKNOWN";
2558 break;
2559 case TLDAP_FILTER_ERROR:
2560 res = "TLDAP_FILTER_ERROR";
2561 break;
2562 case TLDAP_USER_CANCELLED:
2563 res = "TLDAP_USER_CANCELLED";
2564 break;
2565 case TLDAP_PARAM_ERROR:
2566 res = "TLDAP_PARAM_ERROR";
2567 break;
2568 case TLDAP_NO_MEMORY:
2569 res = "TLDAP_NO_MEMORY";
2570 break;
2571 case TLDAP_CONNECT_ERROR:
2572 res = "TLDAP_CONNECT_ERROR";
2573 break;
2574 case TLDAP_NOT_SUPPORTED:
2575 res = "TLDAP_NOT_SUPPORTED";
2576 break;
2577 case TLDAP_CONTROL_NOT_FOUND:
2578 res = "TLDAP_CONTROL_NOT_FOUND";
2579 break;
2580 case TLDAP_NO_RESULTS_RETURNED:
2581 res = "TLDAP_NO_RESULTS_RETURNED";
2582 break;
2583 case TLDAP_MORE_RESULTS_TO_RETURN:
2584 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2585 break;
2586 case TLDAP_CLIENT_LOOP:
2587 res = "TLDAP_CLIENT_LOOP";
2588 break;
2589 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2590 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2591 break;
2592 default:
2593 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2594 rc);
2595 break;
2597 if (res == NULL) {
2598 res = "Unknown LDAP Error";
2600 return res;