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/>.
22 bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
24 enum tevent_req_state state;
27 if (!tevent_req_is_error(req, &state, &err)) {
31 case TEVENT_REQ_TIMED_OUT:
32 *perr = TLDAP_TIMEOUT;
34 case TEVENT_REQ_NO_MEMORY:
35 *perr = TLDAP_NO_MEMORY;
37 case TEVENT_REQ_USER_ERROR:
41 *perr = TLDAP_OPERATIONS_ERROR;
47 struct tldap_ctx_attribute {
52 struct tldap_context {
59 struct tevent_queue *outgoing;
60 struct tevent_req **pending;
62 /* For the sync wrappers we need something like get_last_error... */
65 char *res_diagnosticmessage;
67 struct tldap_control *res_sctrls;
70 void (*log_fn)(void *context, enum tldap_debug_level level,
71 const char *fmt, va_list ap);
74 struct tldap_ctx_attribute *ctx_attrs;
77 struct tldap_message {
78 struct asn1_data *data;
85 struct tldap_attribute *attribs;
88 void tldap_set_debug(struct tldap_context *ld,
89 void (*log_fn)(void *log_private,
90 enum tldap_debug_level level,
92 va_list ap) PRINTF_ATTRIBUTE(3,0),
96 ld->log_private = log_private;
99 static void tldap_debug(struct tldap_context *ld,
100 enum tldap_debug_level level,
101 const char *fmt, ...)
107 if (ld->log_fn == NULL) {
111 ld->log_fn(ld->log_private, level, fmt, ap);
115 static int tldap_next_msgid(struct tldap_context *ld)
119 result = ld->msgid++;
120 if (ld->msgid == 2147483647) {
126 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
128 struct tldap_context *ctx;
130 ctx = talloc_zero(mem_ctx, struct tldap_context);
137 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
138 if (ctx->outgoing == NULL) {
145 static struct tldap_ctx_attribute *tldap_context_findattr(
146 struct tldap_context *ld, const char *name)
150 num_attrs = talloc_array_length(ld->ctx_attrs);
152 for (i=0; i<num_attrs; i++) {
153 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
154 return &ld->ctx_attrs[i];
160 bool tldap_context_setattr(struct tldap_context *ld,
161 const char *name, const void *_pptr)
163 struct tldap_ctx_attribute *tmp, *attr;
166 void **pptr = (void **)_pptr;
168 attr = tldap_context_findattr(ld, name);
171 * We don't actually delete attrs, we don't expect tons of
172 * attributes being shuffled around.
174 TALLOC_FREE(attr->ptr);
176 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
182 tmpname = talloc_strdup(ld, name);
183 if (tmpname == NULL) {
187 num_attrs = talloc_array_length(ld->ctx_attrs);
189 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
192 TALLOC_FREE(tmpname);
195 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
197 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
199 tmp[num_attrs].ptr = NULL;
206 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
208 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
216 struct read_ldap_state {
221 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
223 struct read_ldap_state *state = talloc_get_type_abort(
224 private_data, struct read_ldap_state);
229 /* We've been here, we're done */
234 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
236 if (buf[0] != 0x30) {
241 if ((len & 0x80) == 0) {
246 lensize = (len & 0x7f);
250 /* Please get us the full length */
253 if (buflen > 2 + lensize) {
257 if (buflen != 2 + lensize) {
261 for (i=0; i<lensize; i++) {
262 len = (len << 8) | buf[2+i];
267 static void read_ldap_done(struct tevent_req *subreq);
269 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
270 struct tevent_context *ev,
273 struct tevent_req *req, *subreq;
274 struct read_ldap_state *state;
276 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
282 subreq = read_packet_send(state, ev, fd, 2, read_ldap_more, state);
283 if (tevent_req_nomem(subreq, req)) {
284 return tevent_req_post(req, ev);
286 tevent_req_set_callback(subreq, read_ldap_done, req);
290 static void read_ldap_done(struct tevent_req *subreq)
292 struct tevent_req *req = tevent_req_callback_data(
293 subreq, struct tevent_req);
294 struct read_ldap_state *state = tevent_req_data(
295 req, struct read_ldap_state);
299 nread = read_packet_recv(subreq, state, &state->buf, &err);
302 tevent_req_error(req, err);
305 tevent_req_done(req);
308 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
309 uint8_t **pbuf, int *perrno)
311 struct read_ldap_state *state = tevent_req_data(
312 req, struct read_ldap_state);
314 if (tevent_req_is_unix_error(req, perrno)) {
317 *pbuf = talloc_move(mem_ctx, &state->buf);
318 return talloc_get_size(*pbuf);
321 static bool asn1_blob(const struct asn1_data *asn1, DATA_BLOB *blob)
323 if (asn1->has_error) {
326 if (asn1->nesting != NULL) {
329 blob->data = asn1->data;
330 blob->length = asn1->length;
334 static void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len)
341 struct tldap_msg_state {
342 struct tldap_context *ld;
343 struct tevent_context *ev;
347 struct asn1_data *data;
351 static void tldap_push_controls(struct asn1_data *data,
352 struct tldap_control *sctrls,
357 if ((sctrls == NULL) || (num_sctrls == 0)) {
361 asn1_push_tag(data, ASN1_CONTEXT(0));
363 for (i=0; i<num_sctrls; i++) {
364 struct tldap_control *c = &sctrls[i];
365 asn1_push_tag(data, ASN1_SEQUENCE(0));
366 asn1_write_OctetString(data, c->oid, strlen(c->oid));
368 asn1_write_BOOLEAN(data, true);
370 if (c->value.data != NULL) {
371 asn1_write_OctetString(data, c->value.data,
374 asn1_pop_tag(data); /* ASN1_SEQUENCE(0) */
377 asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
380 static void tldap_msg_sent(struct tevent_req *subreq);
381 static void tldap_msg_received(struct tevent_req *subreq);
383 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
384 struct tevent_context *ev,
385 struct tldap_context *ld,
386 int id, struct asn1_data *data,
387 struct tldap_control *sctrls,
390 struct tevent_req *req, *subreq;
391 struct tldap_msg_state *state;
394 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
397 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
405 tldap_push_controls(data, sctrls, num_sctrls);
409 if (!asn1_blob(data, &blob)) {
410 tevent_req_error(req, TLDAP_ENCODING_ERROR);
411 return tevent_req_post(req, ev);
414 state->iov.iov_base = blob.data;
415 state->iov.iov_len = blob.length;
417 subreq = writev_send(state, ev, ld->outgoing, ld->fd, false,
419 if (tevent_req_nomem(subreq, req)) {
420 return tevent_req_post(req, ev);
422 tevent_req_set_callback(subreq, tldap_msg_sent, req);
426 static void tldap_msg_unset_pending(struct tevent_req *req)
428 struct tldap_msg_state *state = tevent_req_data(
429 req, struct tldap_msg_state);
430 struct tldap_context *ld = state->ld;
431 int num_pending = talloc_array_length(ld->pending);
434 if (num_pending == 1) {
435 TALLOC_FREE(ld->pending);
439 for (i=0; i<num_pending; i++) {
440 if (req == ld->pending[i]) {
444 if (i == num_pending) {
446 * Something's seriously broken. Just returning here is the
447 * right thing nevertheless, the point of this routine is to
448 * remove ourselves from cli->pending.
454 * Remove ourselves from the cli->pending array
456 if (num_pending > 1) {
457 ld->pending[i] = ld->pending[num_pending-1];
461 * No NULL check here, we're shrinking by sizeof(void *), and
462 * talloc_realloc just adjusts the size for this.
464 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
469 static int tldap_msg_destructor(struct tevent_req *req)
471 tldap_msg_unset_pending(req);
475 static bool tldap_msg_set_pending(struct tevent_req *req)
477 struct tldap_msg_state *state = tevent_req_data(
478 req, struct tldap_msg_state);
479 struct tldap_context *ld;
480 struct tevent_req **pending;
482 struct tevent_req *subreq;
485 num_pending = talloc_array_length(ld->pending);
487 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
489 if (pending == NULL) {
492 pending[num_pending] = req;
493 ld->pending = pending;
494 talloc_set_destructor(req, tldap_msg_destructor);
496 if (num_pending > 0) {
501 * We're the first ones, add the read_ldap request that waits for the
502 * answer from the server
504 subreq = read_ldap_send(ld->pending, state->ev, ld->fd);
505 if (subreq == NULL) {
506 tldap_msg_unset_pending(req);
509 tevent_req_set_callback(subreq, tldap_msg_received, ld);
513 static void tldap_msg_sent(struct tevent_req *subreq)
515 struct tevent_req *req = tevent_req_callback_data(
516 subreq, struct tevent_req);
520 nwritten = writev_recv(subreq, &err);
522 if (nwritten == -1) {
523 tevent_req_error(req, TLDAP_SERVER_DOWN);
527 if (!tldap_msg_set_pending(req)) {
528 tevent_req_nomem(NULL, req);
533 static int tldap_msg_msgid(struct tevent_req *req)
535 struct tldap_msg_state *state = tevent_req_data(
536 req, struct tldap_msg_state);
541 static void tldap_msg_received(struct tevent_req *subreq)
543 struct tldap_context *ld = tevent_req_callback_data(
544 subreq, struct tldap_context);
545 struct tevent_req *req;
546 struct tldap_msg_state *state;
547 struct tevent_context *ev;
548 struct asn1_data *data;
557 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
559 if (received == -1) {
560 status = TLDAP_SERVER_DOWN;
564 data = asn1_init(talloc_tos());
566 status = TLDAP_NO_MEMORY;
569 asn1_load_nocopy(data, inbuf, received);
572 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
573 ok &= asn1_read_Integer(data, &id);
574 ok &= asn1_peek_uint8(data, &type);
577 status = TLDAP_PROTOCOL_ERROR;
581 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
582 "type %d\n", id, (int)type);
584 num_pending = talloc_array_length(ld->pending);
586 for (i=0; i<num_pending; i++) {
587 if (id == tldap_msg_msgid(ld->pending[i])) {
591 if (i == num_pending) {
592 /* Dump unexpected reply */
593 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
594 "No request pending for msg %d\n", id);
599 req = ld->pending[i];
600 state = tevent_req_data(req, struct tldap_msg_state);
602 state->inbuf = talloc_move(state, &inbuf);
603 state->data = talloc_move(state, &data);
607 talloc_set_destructor(req, NULL);
608 tldap_msg_destructor(req);
609 tevent_req_done(req);
612 if (talloc_array_length(ld->pending) > 0) {
613 state = tevent_req_data(ld->pending[0],
614 struct tldap_msg_state);
615 subreq = read_ldap_send(ld->pending, state->ev, ld->fd);
616 if (subreq == NULL) {
617 status = TLDAP_NO_MEMORY;
620 tevent_req_set_callback(subreq, tldap_msg_received, ld);
625 while (talloc_array_length(ld->pending) > 0) {
626 req = ld->pending[0];
627 talloc_set_destructor(req, NULL);
628 tldap_msg_destructor(req);
629 tevent_req_error(req, status);
633 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
634 struct tldap_message **pmsg)
636 struct tldap_msg_state *state = tevent_req_data(
637 req, struct tldap_msg_state);
638 struct tldap_message *msg;
642 if (tevent_req_is_ldap_error(req, &err)) {
646 if (!asn1_peek_uint8(state->data, &msgtype)) {
647 return TLDAP_PROTOCOL_ERROR;
651 return TLDAP_SUCCESS;
654 msg = talloc_zero(mem_ctx, struct tldap_message);
656 return TLDAP_NO_MEMORY;
660 msg->inbuf = talloc_move(msg, &state->inbuf);
661 msg->data = talloc_move(msg, &state->data);
665 return TLDAP_SUCCESS;
668 struct tldap_req_state {
670 struct asn1_data *out;
671 struct tldap_message *result;
675 char *res_diagnosticmessage;
677 struct tldap_control *res_sctrls;
680 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
681 struct tldap_context *ld,
682 struct tldap_req_state **pstate)
684 struct tevent_req *req;
685 struct tldap_req_state *state;
687 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
692 state->out = asn1_init(state);
693 if (state->out == NULL) {
697 state->result = NULL;
698 state->id = tldap_next_msgid(ld);
700 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
701 asn1_write_Integer(state->out, state->id);
707 static void tldap_save_errors(struct tldap_context *ctx,
708 struct tevent_req *req)
710 struct tldap_req_state *state = tevent_req_data(
711 req, struct tldap_req_state);
713 TALLOC_FREE(ctx->res_matcheddn);
714 TALLOC_FREE(ctx->res_diagnosticmessage);
715 TALLOC_FREE(ctx->res_referral);
716 TALLOC_FREE(ctx->res_sctrls);
718 ctx->lderr = state->lderr;
719 ctx->res_matcheddn = talloc_move(ctx, &state->res_matcheddn);
720 ctx->res_diagnosticmessage = talloc_move(
721 ctx, &state->res_diagnosticmessage);
722 ctx->res_referral = talloc_move(ctx, &state->res_referral);
723 ctx->res_sctrls = talloc_move(ctx, &state->res_sctrls);
726 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
728 char *result = talloc_array(mem_ctx, char, blob.length+1);
729 memcpy(result, blob.data, blob.length);
730 result[blob.length] = '\0';
734 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
735 struct asn1_data *data,
739 if (!asn1_read_OctetString(data, mem_ctx, &string))
741 *result = blob2string_talloc(mem_ctx, string);
742 data_blob_free(&string);
746 static bool tldap_decode_response(struct tldap_req_state *state)
748 struct asn1_data *data = state->result->data;
751 ok &= asn1_read_enumerated(data, &state->lderr);
752 ok &= asn1_read_OctetString_talloc(state, data, &state->res_matcheddn);
753 ok &= asn1_read_OctetString_talloc(state, data,
754 &state->res_diagnosticmessage);
755 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
756 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
757 ok &= asn1_read_OctetString_talloc(
758 state, data, &state->res_referral);
759 ok &= asn1_end_tag(data);
761 state->res_referral = NULL;
767 static void tldap_sasl_bind_done(struct tevent_req *subreq);
769 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
770 struct tevent_context *ev,
771 struct tldap_context *ld,
773 const char *mechanism,
775 struct tldap_control *sctrls,
777 struct tldap_control *cctrls,
780 struct tevent_req *req, *subreq;
781 struct tldap_req_state *state;
783 req = tldap_req_create(mem_ctx, ld, &state);
792 asn1_push_tag(state->out, TLDAP_REQ_BIND);
793 asn1_write_Integer(state->out, ld->ld_version);
794 asn1_write_OctetString(state->out, dn, (dn != NULL) ? strlen(dn) : 0);
796 if (mechanism == NULL) {
797 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
798 asn1_write(state->out, creds->data, creds->length);
799 asn1_pop_tag(state->out);
801 asn1_push_tag(state->out, ASN1_CONTEXT(3));
802 asn1_write_OctetString(state->out, mechanism,
804 if ((creds != NULL) && (creds->data != NULL)) {
805 asn1_write_OctetString(state->out, creds->data,
808 asn1_pop_tag(state->out);
811 if (!asn1_pop_tag(state->out)) {
812 tevent_req_error(req, TLDAP_ENCODING_ERROR);
813 return tevent_req_post(req, ev);
816 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
818 if (tevent_req_nomem(subreq, req)) {
819 return tevent_req_post(req, ev);
821 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
825 static void tldap_sasl_bind_done(struct tevent_req *subreq)
827 struct tevent_req *req = tevent_req_callback_data(
828 subreq, struct tevent_req);
829 struct tldap_req_state *state = tevent_req_data(
830 req, struct tldap_req_state);
833 err = tldap_msg_recv(subreq, state, &state->result);
835 if (err != TLDAP_SUCCESS) {
836 tevent_req_error(req, err);
839 if (state->result->type != TLDAP_RES_BIND) {
840 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
843 if (!asn1_start_tag(state->result->data, state->result->type) ||
844 !tldap_decode_response(state) ||
845 !asn1_end_tag(state->result->data)) {
846 tevent_req_error(req, TLDAP_DECODING_ERROR);
850 * TODO: pull the reply blob
852 if (state->lderr != TLDAP_SUCCESS) {
853 tevent_req_error(req, state->lderr);
856 tevent_req_done(req);
859 int tldap_sasl_bind_recv(struct tevent_req *req)
863 if (tevent_req_is_ldap_error(req, &err)) {
866 return TLDAP_SUCCESS;
869 int tldap_sasl_bind(struct tldap_context *ld,
871 const char *mechanism,
873 struct tldap_control *sctrls,
875 struct tldap_control *cctrls,
878 TALLOC_CTX *frame = talloc_stackframe();
879 struct tevent_context *ev;
880 struct tevent_req *req;
883 ev = event_context_init(frame);
885 result = TLDAP_NO_MEMORY;
889 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
890 sctrls, num_sctrls, cctrls, num_cctrls);
892 result = TLDAP_NO_MEMORY;
896 if (!tevent_req_poll(req, ev)) {
897 result = TLDAP_OPERATIONS_ERROR;
901 result = tldap_sasl_bind_recv(req);
902 tldap_save_errors(ld, req);
908 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
909 struct tevent_context *ev,
910 struct tldap_context *ld,
916 if (passwd != NULL) {
917 cred.data = (uint8_t *)passwd;
918 cred.length = strlen(passwd);
920 cred.data = (uint8_t *)"";
923 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
927 int tldap_simple_bind_recv(struct tevent_req *req)
929 return tldap_sasl_bind_recv(req);
932 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
937 if (passwd != NULL) {
938 cred.data = (uint8_t *)passwd;
939 cred.length = strlen(passwd);
941 cred.data = (uint8_t *)"";
944 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
947 /*****************************************************************************/
950 * This piece has a dependency on ldb, the ldb_parse_tree() function is used.
951 * In case we want to separate out tldap, we need to copy or rewrite it.
954 #include "lib/ldb/include/ldb.h"
955 #include "lib/ldb/include/ldb_errors.h"
957 static bool ldap_push_filter(struct asn1_data *data,
958 struct ldb_parse_tree *tree)
962 switch (tree->operation) {
966 ASN1_CONTEXT(tree->operation==LDB_OP_AND?0:1));
967 for (i=0; i<tree->u.list.num_elements; i++) {
968 if (!ldap_push_filter(data,
969 tree->u.list.elements[i])) {
977 asn1_push_tag(data, ASN1_CONTEXT(2));
978 if (!ldap_push_filter(data, tree->u.isnot.child)) {
984 case LDB_OP_EQUALITY:
986 asn1_push_tag(data, ASN1_CONTEXT(3));
987 asn1_write_OctetString(data, tree->u.equality.attr,
988 strlen(tree->u.equality.attr));
989 asn1_write_OctetString(data, tree->u.equality.value.data,
990 tree->u.equality.value.length);
994 case LDB_OP_SUBSTRING:
996 SubstringFilter ::= SEQUENCE {
997 type AttributeDescription,
998 -- at least one must be present
999 substrings SEQUENCE OF CHOICE {
1000 initial [0] LDAPString,
1002 final [2] LDAPString } }
1004 asn1_push_tag(data, ASN1_CONTEXT(4));
1005 asn1_write_OctetString(data, tree->u.substring.attr,
1006 strlen(tree->u.substring.attr));
1007 asn1_push_tag(data, ASN1_SEQUENCE(0));
1009 if (!tree->u.substring.start_with_wildcard) {
1010 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(0));
1011 asn1_write_DATA_BLOB_LDAPString(
1012 data, tree->u.substring.chunks[i]);
1016 while (tree->u.substring.chunks[i]) {
1019 if ((!tree->u.substring.chunks[i + 1]) &&
1020 (tree->u.substring.end_with_wildcard == 0)) {
1025 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(ctx));
1026 asn1_write_DATA_BLOB_LDAPString(
1027 data, tree->u.substring.chunks[i]);
1035 case LDB_OP_GREATER:
1036 /* greaterOrEqual test */
1037 asn1_push_tag(data, ASN1_CONTEXT(5));
1038 asn1_write_OctetString(data, tree->u.comparison.attr,
1039 strlen(tree->u.comparison.attr));
1040 asn1_write_OctetString(data, tree->u.comparison.value.data,
1041 tree->u.comparison.value.length);
1046 /* lessOrEqual test */
1047 asn1_push_tag(data, ASN1_CONTEXT(6));
1048 asn1_write_OctetString(data, tree->u.comparison.attr,
1049 strlen(tree->u.comparison.attr));
1050 asn1_write_OctetString(data, tree->u.comparison.value.data,
1051 tree->u.comparison.value.length);
1055 case LDB_OP_PRESENT:
1057 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(7));
1058 asn1_write_LDAPString(data, tree->u.present.attr);
1060 return !data->has_error;
1064 asn1_push_tag(data, ASN1_CONTEXT(8));
1065 asn1_write_OctetString(data, tree->u.comparison.attr,
1066 strlen(tree->u.comparison.attr));
1067 asn1_write_OctetString(data, tree->u.comparison.value.data,
1068 tree->u.comparison.value.length);
1072 case LDB_OP_EXTENDED:
1074 MatchingRuleAssertion ::= SEQUENCE {
1075 matchingRule [1] MatchingRuleID OPTIONAL,
1076 type [2] AttributeDescription OPTIONAL,
1077 matchValue [3] AssertionValue,
1078 dnAttributes [4] BOOLEAN DEFAULT FALSE
1081 asn1_push_tag(data, ASN1_CONTEXT(9));
1082 if (tree->u.extended.rule_id) {
1083 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1084 asn1_write_LDAPString(data, tree->u.extended.rule_id);
1087 if (tree->u.extended.attr) {
1088 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1089 asn1_write_LDAPString(data, tree->u.extended.attr);
1092 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1093 asn1_write_DATA_BLOB_LDAPString(data, &tree->u.extended.value);
1095 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1096 asn1_write_uint8(data, tree->u.extended.dnAttributes);
1104 return !data->has_error;
1107 static bool tldap_push_filter(struct asn1_data *data, const char *filter)
1109 struct ldb_parse_tree *tree;
1112 tree = ldb_parse_tree(talloc_tos(), filter);
1116 ret = ldap_push_filter(data, tree);
1121 /*****************************************************************************/
1123 static void tldap_search_done(struct tevent_req *subreq);
1125 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1126 struct tevent_context *ev,
1127 struct tldap_context *ld,
1128 const char *base, int scope,
1133 struct tldap_control *sctrls,
1135 struct tldap_control *cctrls,
1141 struct tevent_req *req, *subreq;
1142 struct tldap_req_state *state;
1145 req = tldap_req_create(mem_ctx, ld, &state);
1150 asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1151 asn1_write_OctetString(state->out, base, strlen(base));
1152 asn1_write_enumerated(state->out, scope);
1153 asn1_write_enumerated(state->out, deref);
1154 asn1_write_Integer(state->out, sizelimit);
1155 asn1_write_Integer(state->out, timelimit);
1156 asn1_write_BOOLEAN(state->out, attrsonly);
1158 if (!tldap_push_filter(state->out, filter)) {
1159 goto encoding_error;
1162 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1163 for (i=0; i<num_attrs; i++) {
1164 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1166 asn1_pop_tag(state->out);
1167 asn1_pop_tag(state->out);
1169 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1170 sctrls, num_sctrls);
1171 if (tevent_req_nomem(subreq, req)) {
1172 return tevent_req_post(req, ev);
1174 tevent_req_set_callback(subreq, tldap_search_done, req);
1178 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1179 return tevent_req_post(req, ev);
1182 static void tldap_search_done(struct tevent_req *subreq)
1184 struct tevent_req *req = tevent_req_callback_data(
1185 subreq, struct tevent_req);
1186 struct tldap_req_state *state = tevent_req_data(
1187 req, struct tldap_req_state);
1190 err = tldap_msg_recv(subreq, state, &state->result);
1191 if (err != TLDAP_SUCCESS) {
1192 tevent_req_error(req, err);
1195 switch (state->result->type) {
1196 case TLDAP_RES_SEARCH_ENTRY:
1197 case TLDAP_RES_SEARCH_REFERENCE:
1198 tevent_req_notify_callback(req);
1199 if (!tldap_msg_set_pending(subreq)) {
1200 tevent_req_nomem(NULL, req);
1204 case TLDAP_RES_SEARCH_RESULT:
1205 TALLOC_FREE(subreq);
1206 if (!asn1_start_tag(state->result->data,
1207 state->result->type) ||
1208 !tldap_decode_response(state) ||
1209 !asn1_end_tag(state->result->data)) {
1210 tevent_req_error(req, TLDAP_DECODING_ERROR);
1213 tevent_req_done(req);
1216 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1221 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1222 struct tldap_message **pmsg)
1224 struct tldap_req_state *state = tevent_req_data(
1225 req, struct tldap_req_state);
1228 if (!tevent_req_is_in_progress(req)
1229 && tevent_req_is_ldap_error(req, &err)) {
1233 if (tevent_req_is_in_progress(req)) {
1234 switch (state->result->type) {
1235 case TLDAP_RES_SEARCH_ENTRY:
1236 case TLDAP_RES_SEARCH_REFERENCE:
1239 return TLDAP_OPERATIONS_ERROR;
1243 *pmsg = talloc_move(mem_ctx, &state->result);
1244 return TLDAP_SUCCESS;
1247 struct tldap_sync_search_state {
1248 TALLOC_CTX *mem_ctx;
1249 struct tldap_message **entries;
1250 struct tldap_message **refs;
1254 static void tldap_search_cb(struct tevent_req *req)
1256 struct tldap_sync_search_state *state =
1257 (struct tldap_sync_search_state *)
1258 tevent_req_callback_data_void(req);
1259 struct tldap_message *msg, **tmp;
1260 int rc, num_entries, num_refs;
1262 rc = tldap_search_recv(req, talloc_tos(), &msg);
1263 if (rc != TLDAP_SUCCESS) {
1268 switch (tldap_msg_type(msg)) {
1269 case TLDAP_RES_SEARCH_ENTRY:
1270 num_entries = talloc_array_length(state->entries);
1271 tmp = talloc_realloc(state->mem_ctx, state->entries,
1272 struct tldap_message *, num_entries + 1);
1274 state->rc = TLDAP_NO_MEMORY;
1277 state->entries = tmp;
1278 state->entries[num_entries] = talloc_move(state->entries,
1281 case TLDAP_RES_SEARCH_REFERENCE:
1282 num_refs = talloc_array_length(state->refs);
1283 tmp = talloc_realloc(state->mem_ctx, state->refs,
1284 struct tldap_message *, num_refs + 1);
1286 state->rc = TLDAP_NO_MEMORY;
1290 state->refs[num_refs] = talloc_move(state->refs, &msg);
1292 case TLDAP_RES_SEARCH_RESULT:
1293 state->rc = TLDAP_SUCCESS;
1296 state->rc = TLDAP_PROTOCOL_ERROR;
1301 int tldap_search(struct tldap_context *ld,
1302 const char *base, int scope, const char *filter,
1303 const char **attrs, int num_attrs, int attrsonly,
1304 struct tldap_control *sctrls, int num_sctrls,
1305 struct tldap_control *cctrls, int num_cctrls,
1306 int timelimit, int sizelimit, int deref,
1307 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1308 struct tldap_message ***refs)
1310 TALLOC_CTX *frame = talloc_stackframe();
1311 struct tevent_context *ev;
1312 struct tevent_req *req;
1313 struct tldap_sync_search_state state;
1316 state.mem_ctx = mem_ctx;
1317 state.rc = TLDAP_SUCCESS;
1319 ev = event_context_init(frame);
1321 state.rc = TLDAP_NO_MEMORY;
1325 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1326 attrs, num_attrs, attrsonly,
1327 sctrls, num_sctrls, cctrls, num_cctrls,
1328 timelimit, sizelimit, deref);
1330 state.rc = TLDAP_NO_MEMORY;
1334 tevent_req_set_callback(req, tldap_search_cb, &state);
1336 while (tevent_req_is_in_progress(req)
1337 && (state.rc == TLDAP_SUCCESS)) {
1338 if (tevent_loop_once(ev) == -1) {
1339 return TLDAP_OPERATIONS_ERROR;
1343 if (state.rc != TLDAP_SUCCESS) {
1347 if (entries != NULL) {
1348 *entries = state.entries;
1350 TALLOC_FREE(state.entries);
1355 TALLOC_FREE(state.refs);
1357 tldap_save_errors(ld, req);
1363 static bool tldap_parse_search_entry(struct tldap_message *msg)
1365 int num_attribs = 0;
1367 asn1_start_tag(msg->data, msg->type);
1371 asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1372 if (msg->dn == NULL) {
1377 * Attributes: We overallocate msg->attribs by one, so that while
1378 * looping over the attributes we can directly parse into the last
1379 * array element. Same for the values in the inner loop.
1382 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1383 if (msg->attribs == NULL) {
1387 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1388 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1389 struct tldap_attribute *attrib;
1392 attrib = &msg->attribs[num_attribs];
1393 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1394 if (attrib->values == NULL) {
1397 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1398 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1400 asn1_start_tag(msg->data, ASN1_SET);
1402 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1403 asn1_read_OctetString(msg->data, msg,
1404 &attrib->values[num_values]);
1406 attrib->values = talloc_realloc(
1407 msg->attribs, attrib->values, DATA_BLOB,
1409 if (attrib->values == NULL) {
1414 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1415 DATA_BLOB, num_values);
1416 attrib->num_values = num_values;
1418 asn1_end_tag(msg->data); /* ASN1_SET */
1419 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1420 msg->attribs = talloc_realloc(
1421 msg, msg->attribs, struct tldap_attribute,
1423 if (msg->attribs == NULL) {
1428 msg->attribs = talloc_realloc(
1429 msg, msg->attribs, struct tldap_attribute, num_attribs);
1430 asn1_end_tag(msg->data);
1431 if (msg->data->has_error) {
1437 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1439 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1446 bool tldap_entry_attributes(struct tldap_message *msg, int *num_attributes,
1447 struct tldap_attribute **attributes)
1449 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1452 *attributes = msg->attribs;
1453 *num_attributes = talloc_array_length(msg->attribs);
1457 static bool tldap_decode_controls(struct tldap_req_state *state)
1459 struct asn1_data *data = state->result->data;
1460 struct tldap_control *sctrls = NULL;
1461 int num_controls = 0;
1463 state->res_sctrls = NULL;
1465 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1469 asn1_start_tag(data, ASN1_CONTEXT(0));
1471 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
1472 struct tldap_control *c;
1475 sctrls = talloc_realloc(state, sctrls, struct tldap_control,
1477 if (sctrls == NULL) {
1480 c = &sctrls[num_controls];
1482 asn1_start_tag(data, ASN1_SEQUENCE(0));
1483 asn1_read_OctetString_talloc(state, data, &oid);
1484 if ((data->has_error) || (oid == NULL)) {
1488 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
1489 asn1_read_BOOLEAN(data, &c->critical);
1491 c->critical = false;
1493 c->value = data_blob_null;
1494 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
1495 !asn1_read_OctetString(data, state, &c->value)) {
1498 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
1503 asn1_end_tag(data); /* ASN1_CONTEXT(0) */
1505 if (data->has_error) {
1506 TALLOC_FREE(sctrls);
1509 state->res_sctrls = sctrls;
1513 static void tldap_simple_done(struct tevent_req *subreq, int type)
1515 struct tevent_req *req = tevent_req_callback_data(
1516 subreq, struct tevent_req);
1517 struct tldap_req_state *state = tevent_req_data(
1518 req, struct tldap_req_state);
1521 err = tldap_msg_recv(subreq, state, &state->result);
1522 TALLOC_FREE(subreq);
1523 if (err != TLDAP_SUCCESS) {
1524 tevent_req_error(req, err);
1527 if (state->result->type != type) {
1528 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1531 if (!asn1_start_tag(state->result->data, state->result->type) ||
1532 !tldap_decode_response(state) ||
1533 !asn1_end_tag(state->result->data) ||
1534 !tldap_decode_controls(state)) {
1535 tevent_req_error(req, TLDAP_DECODING_ERROR);
1538 if (state->lderr != TLDAP_SUCCESS) {
1539 tevent_req_error(req, state->lderr);
1542 tevent_req_done(req);
1545 static int tldap_simple_recv(struct tevent_req *req)
1548 if (tevent_req_is_ldap_error(req, &err)) {
1551 return TLDAP_SUCCESS;
1554 static void tldap_add_done(struct tevent_req *subreq);
1556 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
1557 struct tevent_context *ev,
1558 struct tldap_context *ld,
1560 struct tldap_mod *attributes,
1562 struct tldap_control *sctrls,
1564 struct tldap_control *cctrls,
1567 struct tevent_req *req, *subreq;
1568 struct tldap_req_state *state;
1571 req = tldap_req_create(mem_ctx, ld, &state);
1576 asn1_push_tag(state->out, TLDAP_REQ_ADD);
1577 asn1_write_OctetString(state->out, dn, strlen(dn));
1578 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1580 for (i=0; i<num_attributes; i++) {
1581 struct tldap_mod *attrib = &attributes[i];
1582 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1583 asn1_write_OctetString(state->out, attrib->attribute,
1584 strlen(attrib->attribute));
1585 asn1_push_tag(state->out, ASN1_SET);
1586 for (j=0; j<attrib->num_values; j++) {
1587 asn1_write_OctetString(state->out,
1588 attrib->values[j].data,
1589 attrib->values[j].length);
1591 asn1_pop_tag(state->out);
1592 asn1_pop_tag(state->out);
1595 asn1_pop_tag(state->out);
1596 asn1_pop_tag(state->out);
1598 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1599 sctrls, num_sctrls);
1600 if (tevent_req_nomem(subreq, req)) {
1601 return tevent_req_post(req, ev);
1603 tevent_req_set_callback(subreq, tldap_add_done, req);
1607 static void tldap_add_done(struct tevent_req *subreq)
1609 return tldap_simple_done(subreq, TLDAP_RES_ADD);
1612 int tldap_add_recv(struct tevent_req *req)
1614 return tldap_simple_recv(req);
1617 int tldap_add(struct tldap_context *ld, const char *dn,
1618 int num_attributes, struct tldap_mod *attributes,
1619 struct tldap_control *sctrls, int num_sctrls,
1620 struct tldap_control *cctrls, int num_cctrls)
1622 TALLOC_CTX *frame = talloc_stackframe();
1623 struct tevent_context *ev;
1624 struct tevent_req *req;
1627 ev = event_context_init(frame);
1629 result = TLDAP_NO_MEMORY;
1633 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
1634 sctrls, num_sctrls, cctrls, num_cctrls);
1636 result = TLDAP_NO_MEMORY;
1640 if (!tevent_req_poll(req, ev)) {
1641 result = TLDAP_OPERATIONS_ERROR;
1645 result = tldap_add_recv(req);
1646 tldap_save_errors(ld, req);
1652 static void tldap_modify_done(struct tevent_req *subreq);
1654 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
1655 struct tevent_context *ev,
1656 struct tldap_context *ld,
1658 int num_mods, struct tldap_mod *mods,
1659 struct tldap_control *sctrls,
1661 struct tldap_control *cctrls,
1664 struct tevent_req *req, *subreq;
1665 struct tldap_req_state *state;
1668 req = tldap_req_create(mem_ctx, ld, &state);
1673 asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
1674 asn1_write_OctetString(state->out, dn, strlen(dn));
1675 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1677 for (i=0; i<num_mods; i++) {
1678 struct tldap_mod *mod = &mods[i];
1679 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1680 asn1_write_enumerated(state->out, mod->mod_op),
1681 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1682 asn1_write_OctetString(state->out, mod->attribute,
1683 strlen(mod->attribute));
1684 asn1_push_tag(state->out, ASN1_SET);
1685 for (j=0; j<mod->num_values; j++) {
1686 asn1_write_OctetString(state->out,
1687 mod->values[j].data,
1688 mod->values[j].length);
1690 asn1_pop_tag(state->out);
1691 asn1_pop_tag(state->out);
1692 asn1_pop_tag(state->out);
1695 asn1_pop_tag(state->out);
1696 asn1_pop_tag(state->out);
1698 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1699 sctrls, num_sctrls);
1700 if (tevent_req_nomem(subreq, req)) {
1701 return tevent_req_post(req, ev);
1703 tevent_req_set_callback(subreq, tldap_modify_done, req);
1707 static void tldap_modify_done(struct tevent_req *subreq)
1709 return tldap_simple_done(subreq, TLDAP_RES_MODIFY);
1712 int tldap_modify_recv(struct tevent_req *req)
1714 return tldap_simple_recv(req);
1717 int tldap_modify(struct tldap_context *ld, const char *dn,
1718 int num_mods, struct tldap_mod *mods,
1719 struct tldap_control *sctrls, int num_sctrls,
1720 struct tldap_control *cctrls, int num_cctrls)
1722 TALLOC_CTX *frame = talloc_stackframe();
1723 struct tevent_context *ev;
1724 struct tevent_req *req;
1727 ev = event_context_init(frame);
1729 result = TLDAP_NO_MEMORY;
1733 req = tldap_modify_send(frame, ev, ld, dn, num_mods, mods,
1734 sctrls, num_sctrls, cctrls, num_cctrls);
1736 result = TLDAP_NO_MEMORY;
1740 if (!tevent_req_poll(req, ev)) {
1741 result = TLDAP_OPERATIONS_ERROR;
1745 result = tldap_modify_recv(req);
1746 tldap_save_errors(ld, req);
1752 static void tldap_delete_done(struct tevent_req *subreq);
1754 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
1755 struct tevent_context *ev,
1756 struct tldap_context *ld,
1758 struct tldap_control *sctrls,
1760 struct tldap_control *cctrls,
1763 struct tevent_req *req, *subreq;
1764 struct tldap_req_state *state;
1766 req = tldap_req_create(mem_ctx, ld, &state);
1771 asn1_push_tag(state->out, TLDAP_REQ_DELETE);
1772 asn1_write(state->out, dn, strlen(dn));
1773 asn1_pop_tag(state->out);
1775 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1776 sctrls, num_sctrls);
1777 if (tevent_req_nomem(subreq, req)) {
1778 return tevent_req_post(req, ev);
1780 tevent_req_set_callback(subreq, tldap_delete_done, req);
1784 static void tldap_delete_done(struct tevent_req *subreq)
1786 return tldap_simple_done(subreq, TLDAP_RES_DELETE);
1789 int tldap_delete_recv(struct tevent_req *req)
1791 return tldap_simple_recv(req);
1794 int tldap_delete(struct tldap_context *ld, const char *dn,
1795 struct tldap_control *sctrls, int num_sctrls,
1796 struct tldap_control *cctrls, int num_cctrls)
1798 TALLOC_CTX *frame = talloc_stackframe();
1799 struct tevent_context *ev;
1800 struct tevent_req *req;
1803 ev = event_context_init(frame);
1805 result = TLDAP_NO_MEMORY;
1809 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
1810 cctrls, num_cctrls);
1812 result = TLDAP_NO_MEMORY;
1816 if (!tevent_req_poll(req, ev)) {
1817 result = TLDAP_OPERATIONS_ERROR;
1821 result = tldap_delete_recv(req);
1822 tldap_save_errors(ld, req);
1828 int tldap_msg_id(const struct tldap_message *msg)
1833 int tldap_msg_type(const struct tldap_message *msg)
1838 const char *tldap_req_matcheddn(struct tevent_req *req)
1840 struct tldap_req_state *state = tevent_req_data(
1841 req, struct tldap_req_state);
1842 return state->res_matcheddn;
1845 const char *tldap_req_diagnosticmessage(struct tevent_req *req)
1847 struct tldap_req_state *state = tevent_req_data(
1848 req, struct tldap_req_state);
1849 return state->res_diagnosticmessage;
1852 const char *tldap_req_referral(struct tevent_req *req)
1854 struct tldap_req_state *state = tevent_req_data(
1855 req, struct tldap_req_state);
1856 return state->res_referral;
1859 void tldap_req_sctrls(struct tevent_req *req, int *num_sctrls,
1860 struct tldap_control **sctrls)
1862 struct tldap_req_state *state = tevent_req_data(
1863 req, struct tldap_req_state);
1864 *sctrls = state->res_sctrls;
1865 *num_sctrls = talloc_array_length(state->res_sctrls);
1868 const char *tldap_ctx_matcheddn(struct tldap_context *ctx)
1870 return ctx->res_matcheddn;
1873 const char *tldap_ctx_diagnosticmessage(struct tldap_context *ctx)
1875 return ctx->res_diagnosticmessage;
1878 const char *tldap_ctx_referral(struct tldap_context *ctx)
1880 return ctx->res_referral;
1883 void tldap_ctx_sctrls(struct tldap_context *ctx, int *num_sctrls,
1884 struct tldap_control **sctrls)
1886 *sctrls = ctx->res_sctrls;
1887 *num_sctrls = talloc_array_length(ctx->res_sctrls);
1890 const char *tldap_err2string(int rc)
1892 const char *res = NULL;
1895 * This would normally be a table, but the error codes are not fully
1896 * sequential. Let the compiler figure out the optimum implementation
1902 res = "TLDAP_SUCCESS";
1904 case TLDAP_OPERATIONS_ERROR:
1905 res = "TLDAP_OPERATIONS_ERROR";
1907 case TLDAP_PROTOCOL_ERROR:
1908 res = "TLDAP_PROTOCOL_ERROR";
1910 case TLDAP_TIMELIMIT_EXCEEDED:
1911 res = "TLDAP_TIMELIMIT_EXCEEDED";
1913 case TLDAP_SIZELIMIT_EXCEEDED:
1914 res = "TLDAP_SIZELIMIT_EXCEEDED";
1916 case TLDAP_COMPARE_FALSE:
1917 res = "TLDAP_COMPARE_FALSE";
1919 case TLDAP_COMPARE_TRUE:
1920 res = "TLDAP_COMPARE_TRUE";
1922 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
1923 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
1925 case TLDAP_STRONG_AUTH_REQUIRED:
1926 res = "TLDAP_STRONG_AUTH_REQUIRED";
1928 case TLDAP_REFERRAL:
1929 res = "TLDAP_REFERRAL";
1931 case TLDAP_ADMINLIMIT_EXCEEDED:
1932 res = "TLDAP_ADMINLIMIT_EXCEEDED";
1934 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
1935 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
1937 case TLDAP_CONFIDENTIALITY_REQUIRED:
1938 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
1940 case TLDAP_SASL_BIND_IN_PROGRESS:
1941 res = "TLDAP_SASL_BIND_IN_PROGRESS";
1943 case TLDAP_NO_SUCH_ATTRIBUTE:
1944 res = "TLDAP_NO_SUCH_ATTRIBUTE";
1946 case TLDAP_UNDEFINED_TYPE:
1947 res = "TLDAP_UNDEFINED_TYPE";
1949 case TLDAP_INAPPROPRIATE_MATCHING:
1950 res = "TLDAP_INAPPROPRIATE_MATCHING";
1952 case TLDAP_CONSTRAINT_VIOLATION:
1953 res = "TLDAP_CONSTRAINT_VIOLATION";
1955 case TLDAP_TYPE_OR_VALUE_EXISTS:
1956 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
1958 case TLDAP_INVALID_SYNTAX:
1959 res = "TLDAP_INVALID_SYNTAX";
1961 case TLDAP_NO_SUCH_OBJECT:
1962 res = "TLDAP_NO_SUCH_OBJECT";
1964 case TLDAP_ALIAS_PROBLEM:
1965 res = "TLDAP_ALIAS_PROBLEM";
1967 case TLDAP_INVALID_DN_SYNTAX:
1968 res = "TLDAP_INVALID_DN_SYNTAX";
1971 res = "TLDAP_IS_LEAF";
1973 case TLDAP_ALIAS_DEREF_PROBLEM:
1974 res = "TLDAP_ALIAS_DEREF_PROBLEM";
1976 case TLDAP_INAPPROPRIATE_AUTH:
1977 res = "TLDAP_INAPPROPRIATE_AUTH";
1979 case TLDAP_INVALID_CREDENTIALS:
1980 res = "TLDAP_INVALID_CREDENTIALS";
1982 case TLDAP_INSUFFICIENT_ACCESS:
1983 res = "TLDAP_INSUFFICIENT_ACCESS";
1988 case TLDAP_UNAVAILABLE:
1989 res = "TLDAP_UNAVAILABLE";
1991 case TLDAP_UNWILLING_TO_PERFORM:
1992 res = "TLDAP_UNWILLING_TO_PERFORM";
1994 case TLDAP_LOOP_DETECT:
1995 res = "TLDAP_LOOP_DETECT";
1997 case TLDAP_NAMING_VIOLATION:
1998 res = "TLDAP_NAMING_VIOLATION";
2000 case TLDAP_OBJECT_CLASS_VIOLATION:
2001 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2003 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2004 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2006 case TLDAP_NOT_ALLOWED_ON_RDN:
2007 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2009 case TLDAP_ALREADY_EXISTS:
2010 res = "TLDAP_ALREADY_EXISTS";
2012 case TLDAP_NO_OBJECT_CLASS_MODS:
2013 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2015 case TLDAP_RESULTS_TOO_LARGE:
2016 res = "TLDAP_RESULTS_TOO_LARGE";
2018 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2019 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2022 res = "TLDAP_OTHER";
2024 case TLDAP_SERVER_DOWN:
2025 res = "TLDAP_SERVER_DOWN";
2027 case TLDAP_LOCAL_ERROR:
2028 res = "TLDAP_LOCAL_ERROR";
2030 case TLDAP_ENCODING_ERROR:
2031 res = "TLDAP_ENCODING_ERROR";
2033 case TLDAP_DECODING_ERROR:
2034 res = "TLDAP_DECODING_ERROR";
2037 res = "TLDAP_TIMEOUT";
2039 case TLDAP_AUTH_UNKNOWN:
2040 res = "TLDAP_AUTH_UNKNOWN";
2042 case TLDAP_FILTER_ERROR:
2043 res = "TLDAP_FILTER_ERROR";
2045 case TLDAP_USER_CANCELLED:
2046 res = "TLDAP_USER_CANCELLED";
2048 case TLDAP_PARAM_ERROR:
2049 res = "TLDAP_PARAM_ERROR";
2051 case TLDAP_NO_MEMORY:
2052 res = "TLDAP_NO_MEMORY";
2054 case TLDAP_CONNECT_ERROR:
2055 res = "TLDAP_CONNECT_ERROR";
2057 case TLDAP_NOT_SUPPORTED:
2058 res = "TLDAP_NOT_SUPPORTED";
2060 case TLDAP_CONTROL_NOT_FOUND:
2061 res = "TLDAP_CONTROL_NOT_FOUND";
2063 case TLDAP_NO_RESULTS_RETURNED:
2064 res = "TLDAP_NO_RESULTS_RETURNED";
2066 case TLDAP_MORE_RESULTS_TO_RETURN:
2067 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2069 case TLDAP_CLIENT_LOOP:
2070 res = "TLDAP_CLIENT_LOOP";
2072 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2073 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2076 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2081 res = "Unknown LDAP Error";