librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/gebeck_regimport.git] / source3 / lib / tldap.c
blobb094c2d3c2904d4fe808cf2fdab01011a6576663
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 if (num_pending == 1) {
448 TALLOC_FREE(ld->pending);
449 return;
452 for (i=0; i<num_pending; i++) {
453 if (req == ld->pending[i]) {
454 break;
457 if (i == num_pending) {
459 * Something's seriously broken. Just returning here is the
460 * right thing nevertheless, the point of this routine is to
461 * remove ourselves from cli->pending.
463 return;
467 * Remove ourselves from the cli->pending array
469 if (num_pending > 1) {
470 ld->pending[i] = ld->pending[num_pending-1];
474 * No NULL check here, we're shrinking by sizeof(void *), and
475 * talloc_realloc just adjusts the size for this.
477 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
478 num_pending - 1);
479 return;
482 static int tldap_msg_destructor(struct tevent_req *req)
484 tldap_msg_unset_pending(req);
485 return 0;
488 static bool tldap_msg_set_pending(struct tevent_req *req)
490 struct tldap_msg_state *state = tevent_req_data(
491 req, struct tldap_msg_state);
492 struct tldap_context *ld;
493 struct tevent_req **pending;
494 int num_pending;
495 struct tevent_req *subreq;
497 ld = state->ld;
498 num_pending = talloc_array_length(ld->pending);
500 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
501 num_pending+1);
502 if (pending == NULL) {
503 return false;
505 pending[num_pending] = req;
506 ld->pending = pending;
507 talloc_set_destructor(req, tldap_msg_destructor);
509 if (num_pending > 0) {
510 return true;
514 * We're the first one, add the read_ldap request that waits for the
515 * answer from the server
517 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
518 if (subreq == NULL) {
519 tldap_msg_unset_pending(req);
520 return false;
522 tevent_req_set_callback(subreq, tldap_msg_received, ld);
523 return true;
526 static void tldap_msg_sent(struct tevent_req *subreq)
528 struct tevent_req *req = tevent_req_callback_data(
529 subreq, struct tevent_req);
530 struct tldap_msg_state *state = tevent_req_data(
531 req, struct tldap_msg_state);
532 ssize_t nwritten;
533 int err;
535 nwritten = tstream_writev_queue_recv(subreq, &err);
536 TALLOC_FREE(subreq);
537 if (nwritten == -1) {
538 state->ld->server_down = true;
539 tevent_req_error(req, TLDAP_SERVER_DOWN);
540 return;
543 if (!tldap_msg_set_pending(req)) {
544 tevent_req_oom(req);
545 return;
549 static int tldap_msg_msgid(struct tevent_req *req)
551 struct tldap_msg_state *state = tevent_req_data(
552 req, struct tldap_msg_state);
554 return state->id;
557 static void tldap_msg_received(struct tevent_req *subreq)
559 struct tldap_context *ld = tevent_req_callback_data(
560 subreq, struct tldap_context);
561 struct tevent_req *req;
562 struct tldap_msg_state *state;
563 struct asn1_data *data;
564 uint8_t *inbuf;
565 ssize_t received;
566 size_t num_pending;
567 int i, err, status;
568 int id;
569 uint8_t type;
570 bool ok;
572 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
573 TALLOC_FREE(subreq);
574 if (received == -1) {
575 status = TLDAP_SERVER_DOWN;
576 goto fail;
579 data = asn1_init(talloc_tos());
580 if (data == NULL) {
581 status = TLDAP_NO_MEMORY;
582 goto fail;
584 asn1_load_nocopy(data, inbuf, received);
586 ok = true;
587 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
588 ok &= asn1_read_Integer(data, &id);
589 ok &= asn1_peek_uint8(data, &type);
591 if (!ok) {
592 status = TLDAP_PROTOCOL_ERROR;
593 goto fail;
596 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
597 "type %d\n", id, (int)type);
599 num_pending = talloc_array_length(ld->pending);
601 for (i=0; i<num_pending; i++) {
602 if (id == tldap_msg_msgid(ld->pending[i])) {
603 break;
606 if (i == num_pending) {
607 /* Dump unexpected reply */
608 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
609 "No request pending for msg %d\n", id);
610 TALLOC_FREE(data);
611 TALLOC_FREE(inbuf);
612 goto done;
615 req = ld->pending[i];
616 state = tevent_req_data(req, struct tldap_msg_state);
618 state->inbuf = talloc_move(state, &inbuf);
619 state->data = talloc_move(state, &data);
621 talloc_set_destructor(req, NULL);
622 tldap_msg_unset_pending(req);
623 num_pending = talloc_array_length(ld->pending);
625 tevent_req_done(req);
627 done:
628 if (num_pending == 0) {
629 return;
631 if (talloc_array_length(ld->pending) > num_pending) {
633 * The callback functions called from tevent_req_done() above
634 * have put something on the pending queue. We don't have to
635 * trigger the read_ldap_send(), tldap_msg_set_pending() has
636 * done it for us already.
638 return;
641 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
642 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
643 if (subreq == NULL) {
644 status = TLDAP_NO_MEMORY;
645 goto fail;
647 tevent_req_set_callback(subreq, tldap_msg_received, ld);
648 return;
650 fail:
651 while (talloc_array_length(ld->pending) > 0) {
652 req = ld->pending[0];
653 talloc_set_destructor(req, NULL);
654 tldap_msg_destructor(req);
655 tevent_req_error(req, status);
659 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
660 struct tldap_message **pmsg)
662 struct tldap_msg_state *state = tevent_req_data(
663 req, struct tldap_msg_state);
664 struct tldap_message *msg;
665 int err;
666 uint8_t msgtype;
668 if (tevent_req_is_ldap_error(req, &err)) {
669 return err;
672 if (!asn1_peek_uint8(state->data, &msgtype)) {
673 return TLDAP_PROTOCOL_ERROR;
676 if (pmsg == NULL) {
677 return TLDAP_SUCCESS;
680 msg = talloc_zero(mem_ctx, struct tldap_message);
681 if (msg == NULL) {
682 return TLDAP_NO_MEMORY;
684 msg->id = state->id;
686 msg->inbuf = talloc_move(msg, &state->inbuf);
687 msg->data = talloc_move(msg, &state->data);
688 msg->type = msgtype;
690 *pmsg = msg;
691 return TLDAP_SUCCESS;
694 struct tldap_req_state {
695 int id;
696 struct asn1_data *out;
697 struct tldap_message *result;
700 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
701 struct tldap_context *ld,
702 struct tldap_req_state **pstate)
704 struct tevent_req *req;
705 struct tldap_req_state *state;
707 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
708 if (req == NULL) {
709 return NULL;
711 ZERO_STRUCTP(state);
712 state->out = asn1_init(state);
713 if (state->out == NULL) {
714 TALLOC_FREE(req);
715 return NULL;
717 state->result = NULL;
718 state->id = tldap_next_msgid(ld);
720 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
721 asn1_write_Integer(state->out, state->id);
723 *pstate = state;
724 return req;
727 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
729 struct tldap_req_state *state = tevent_req_data(
730 req, struct tldap_req_state);
732 TALLOC_FREE(ld->last_msg);
733 ld->last_msg = talloc_move(ld, &state->result);
736 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
738 char *result = talloc_array(mem_ctx, char, blob.length+1);
740 if (result == NULL) {
741 return NULL;
744 memcpy(result, blob.data, blob.length);
745 result[blob.length] = '\0';
746 return result;
749 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
750 struct asn1_data *data,
751 char **presult)
753 DATA_BLOB string;
754 char *result;
755 if (!asn1_read_OctetString(data, mem_ctx, &string))
756 return false;
758 result = blob2string_talloc(mem_ctx, string);
760 data_blob_free(&string);
762 if (result == NULL) {
763 return false;
765 *presult = result;
766 return true;
769 static bool tldap_decode_controls(struct tldap_req_state *state);
771 static bool tldap_decode_response(struct tldap_req_state *state)
773 struct asn1_data *data = state->result->data;
774 struct tldap_message *msg = state->result;
775 bool ok = true;
777 ok &= asn1_read_enumerated(data, &msg->lderr);
778 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
779 ok &= asn1_read_OctetString_talloc(msg, data,
780 &msg->res_diagnosticmessage);
781 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
782 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
783 ok &= asn1_read_OctetString_talloc(msg, data,
784 &msg->res_referral);
785 ok &= asn1_end_tag(data);
786 } else {
787 msg->res_referral = NULL;
790 return ok;
793 static void tldap_sasl_bind_done(struct tevent_req *subreq);
795 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
796 struct tevent_context *ev,
797 struct tldap_context *ld,
798 const char *dn,
799 const char *mechanism,
800 DATA_BLOB *creds,
801 struct tldap_control *sctrls,
802 int num_sctrls,
803 struct tldap_control *cctrls,
804 int num_cctrls)
806 struct tevent_req *req, *subreq;
807 struct tldap_req_state *state;
809 req = tldap_req_create(mem_ctx, ld, &state);
810 if (req == NULL) {
811 return NULL;
814 if (dn == NULL) {
815 dn = "";
818 asn1_push_tag(state->out, TLDAP_REQ_BIND);
819 asn1_write_Integer(state->out, ld->ld_version);
820 asn1_write_OctetString(state->out, dn, strlen(dn));
822 if (mechanism == NULL) {
823 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
824 asn1_write(state->out, creds->data, creds->length);
825 asn1_pop_tag(state->out);
826 } else {
827 asn1_push_tag(state->out, ASN1_CONTEXT(3));
828 asn1_write_OctetString(state->out, mechanism,
829 strlen(mechanism));
830 if ((creds != NULL) && (creds->data != NULL)) {
831 asn1_write_OctetString(state->out, creds->data,
832 creds->length);
834 asn1_pop_tag(state->out);
837 if (!asn1_pop_tag(state->out)) {
838 tevent_req_error(req, TLDAP_ENCODING_ERROR);
839 return tevent_req_post(req, ev);
842 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
843 sctrls, num_sctrls);
844 if (tevent_req_nomem(subreq, req)) {
845 return tevent_req_post(req, ev);
847 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
848 return req;
851 static void tldap_sasl_bind_done(struct tevent_req *subreq)
853 struct tevent_req *req = tevent_req_callback_data(
854 subreq, struct tevent_req);
855 struct tldap_req_state *state = tevent_req_data(
856 req, struct tldap_req_state);
857 int err;
859 err = tldap_msg_recv(subreq, state, &state->result);
860 TALLOC_FREE(subreq);
861 if (err != TLDAP_SUCCESS) {
862 tevent_req_error(req, err);
863 return;
865 if (state->result->type != TLDAP_RES_BIND) {
866 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
867 return;
869 if (!asn1_start_tag(state->result->data, state->result->type) ||
870 !tldap_decode_response(state) ||
871 !asn1_end_tag(state->result->data)) {
872 tevent_req_error(req, TLDAP_DECODING_ERROR);
873 return;
876 * TODO: pull the reply blob
878 if (state->result->lderr != TLDAP_SUCCESS) {
879 tevent_req_error(req, state->result->lderr);
880 return;
882 tevent_req_done(req);
885 int tldap_sasl_bind_recv(struct tevent_req *req)
887 return tldap_simple_recv(req);
890 int tldap_sasl_bind(struct tldap_context *ld,
891 const char *dn,
892 const char *mechanism,
893 DATA_BLOB *creds,
894 struct tldap_control *sctrls,
895 int num_sctrls,
896 struct tldap_control *cctrls,
897 int num_cctrls)
899 TALLOC_CTX *frame = talloc_stackframe();
900 struct tevent_context *ev;
901 struct tevent_req *req;
902 int result;
904 ev = samba_tevent_context_init(frame);
905 if (ev == NULL) {
906 result = TLDAP_NO_MEMORY;
907 goto fail;
910 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
911 sctrls, num_sctrls, cctrls, num_cctrls);
912 if (req == NULL) {
913 result = TLDAP_NO_MEMORY;
914 goto fail;
917 if (!tevent_req_poll(req, ev)) {
918 result = TLDAP_OPERATIONS_ERROR;
919 goto fail;
922 result = tldap_sasl_bind_recv(req);
923 tldap_save_msg(ld, req);
924 fail:
925 TALLOC_FREE(frame);
926 return result;
929 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
930 struct tevent_context *ev,
931 struct tldap_context *ld,
932 const char *dn,
933 const char *passwd)
935 DATA_BLOB cred;
937 if (passwd != NULL) {
938 cred.data = discard_const_p(uint8_t, passwd);
939 cred.length = strlen(passwd);
940 } else {
941 cred.data = discard_const_p(uint8_t, "");
942 cred.length = 0;
944 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
945 NULL, 0);
948 int tldap_simple_bind_recv(struct tevent_req *req)
950 return tldap_sasl_bind_recv(req);
953 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
954 const char *passwd)
956 DATA_BLOB cred;
958 if (passwd != NULL) {
959 cred.data = discard_const_p(uint8_t, passwd);
960 cred.length = strlen(passwd);
961 } else {
962 cred.data = discard_const_p(uint8_t, "");
963 cred.length = 0;
965 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
968 /*****************************************************************************/
970 /* can't use isalpha() as only a strict set is valid for LDAP */
972 static bool tldap_is_alpha(char c)
974 return (((c >= 'a') && (c <= 'z')) || \
975 ((c >= 'A') && (c <= 'Z')));
978 static bool tldap_is_adh(char c)
980 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
983 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
984 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
985 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
986 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
987 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
988 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
989 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
990 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
991 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
992 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
994 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
995 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
996 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
999 /* oid's should be numerical only in theory,
1000 * but apparently some broken servers may have alphanum aliases instead.
1001 * Do like openldap libraries and allow alphanum aliases for oids, but
1002 * do not allow Tagging options in that case.
1004 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1006 bool is_oid = false;
1007 bool dot = false;
1008 int i;
1010 /* first char has stricter rules */
1011 if (isdigit(*s)) {
1012 is_oid = true;
1013 } else if (!tldap_is_alpha(*s)) {
1014 /* bad first char */
1015 return false;
1018 for (i = 1; i < len; i++) {
1020 if (is_oid) {
1021 if (isdigit(s[i])) {
1022 dot = false;
1023 continue;
1025 if (s[i] == '.') {
1026 if (dot) {
1027 /* malformed */
1028 return false;
1030 dot = true;
1031 continue;
1033 } else {
1034 if (tldap_is_adh(s[i])) {
1035 continue;
1039 if (s[i] == ';') {
1040 if (no_tagopts) {
1041 /* no tagging options */
1042 return false;
1044 if (dot) {
1045 /* malformed */
1046 return false;
1048 if ((i + 1) == len) {
1049 /* malformed */
1050 return false;
1053 is_oid = false;
1054 continue;
1058 if (dot) {
1059 /* malformed */
1060 return false;
1063 return true;
1066 /* this function copies the value until the closing parenthesis is found. */
1067 static char *tldap_get_val(TALLOC_CTX *memctx,
1068 const char *value, const char **_s)
1070 const char *s = value;
1072 /* find terminator */
1073 while (*s) {
1074 s = strchr(s, ')');
1075 if (s && (*(s - 1) == '\\')) {
1076 continue;
1078 break;
1080 if (!s || !(*s == ')')) {
1081 /* malformed filter */
1082 return NULL;
1085 *_s = s;
1087 return talloc_strndup(memctx, value, s - value);
1090 static int tldap_hex2char(const char *x)
1092 if (isxdigit(x[0]) && isxdigit(x[1])) {
1093 const char h1 = x[0], h2 = x[1];
1094 int c = 0;
1096 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1097 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1098 else if (h1 >= '0') c = h1 - (int)'0';
1099 c = c << 4;
1100 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1101 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1102 else if (h2 >= '0') c += h2 - (int)'0';
1104 return c;
1107 return -1;
1110 static bool tldap_find_first_star(const char *val, const char **star)
1112 const char *s;
1114 for (s = val; *s; s++) {
1115 switch (*s) {
1116 case '\\':
1117 if (isxdigit(s[1]) && isxdigit(s[2])) {
1118 s += 2;
1119 break;
1121 /* not hex based escape, check older syntax */
1122 switch (s[1]) {
1123 case '(':
1124 case ')':
1125 case '*':
1126 case '\\':
1127 s++;
1128 break;
1129 default:
1130 /* invalid escape sequence */
1131 return false;
1133 break;
1134 case ')':
1135 /* end of val, nothing found */
1136 *star = s;
1137 return true;
1139 case '*':
1140 *star = s;
1141 return true;
1145 /* string ended without closing parenthesis, filter is malformed */
1146 return false;
1149 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1151 int c, i, p;
1153 for (i = 0,p = 0; i < *val_len; i++) {
1155 switch (value[i]) {
1156 case '(':
1157 case ')':
1158 case '*':
1159 /* these must be escaped */
1160 return false;
1162 case '\\':
1163 if (!value[i + 1]) {
1164 /* invalid EOL */
1165 return false;
1167 i++;
1169 c = tldap_hex2char(&value[i]);
1170 if (c >= 0 && c < 256) {
1171 value[p] = c;
1172 i++;
1173 p++;
1174 break;
1177 switch (value[i]) {
1178 case '(':
1179 case ')':
1180 case '*':
1181 case '\\':
1182 value[p] = value[i];
1183 p++;
1184 default:
1185 /* invalid */
1186 return false;
1188 break;
1190 default:
1191 value[p] = value[i];
1192 p++;
1195 value[p] = '\0';
1196 *val_len = p;
1197 return true;
1200 static bool tldap_push_filter_basic(struct tldap_context *ld,
1201 struct asn1_data *data,
1202 const char **_s);
1203 static bool tldap_push_filter_substring(struct tldap_context *ld,
1204 struct asn1_data *data,
1205 const char *val,
1206 const char **_s);
1207 static bool tldap_push_filter_int(struct tldap_context *ld,
1208 struct asn1_data *data,
1209 const char **_s)
1211 const char *s = *_s;
1212 bool ret;
1214 if (*s != '(') {
1215 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1216 "Incomplete or malformed filter\n");
1217 return false;
1219 s++;
1221 /* we are right after a parenthesis,
1222 * find out what op we have at hand */
1223 switch (*s) {
1224 case '&':
1225 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1226 asn1_push_tag(data, TLDAP_FILTER_AND);
1227 s++;
1228 break;
1230 case '|':
1231 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1232 asn1_push_tag(data, TLDAP_FILTER_OR);
1233 s++;
1234 break;
1236 case '!':
1237 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1238 asn1_push_tag(data, TLDAP_FILTER_NOT);
1239 s++;
1240 ret = tldap_push_filter_int(ld, data, &s);
1241 if (!ret) {
1242 return false;
1244 asn1_pop_tag(data);
1245 goto done;
1247 case '(':
1248 case ')':
1249 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1250 "Invalid parenthesis '%c'\n", *s);
1251 return false;
1253 case '\0':
1254 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1255 "Invalid filter termination\n");
1256 return false;
1258 default:
1259 ret = tldap_push_filter_basic(ld, data, &s);
1260 if (!ret) {
1261 return false;
1263 goto done;
1266 /* only and/or filters get here.
1267 * go through the list of filters */
1269 if (*s == ')') {
1270 /* RFC 4526: empty and/or */
1271 asn1_pop_tag(data);
1272 goto done;
1275 while (*s) {
1276 ret = tldap_push_filter_int(ld, data, &s);
1277 if (!ret) {
1278 return false;
1281 if (*s == ')') {
1282 /* end of list, return */
1283 asn1_pop_tag(data);
1284 break;
1288 done:
1289 if (*s != ')') {
1290 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1291 "Incomplete or malformed filter\n");
1292 return false;
1294 s++;
1296 if (data->has_error) {
1297 return false;
1300 *_s = s;
1301 return true;
1305 static bool tldap_push_filter_basic(struct tldap_context *ld,
1306 struct asn1_data *data,
1307 const char **_s)
1309 TALLOC_CTX *tmpctx = talloc_tos();
1310 const char *s = *_s;
1311 const char *e;
1312 const char *eq;
1313 const char *val;
1314 const char *type;
1315 const char *dn;
1316 const char *rule;
1317 const char *star;
1318 size_t type_len = 0;
1319 char *uval;
1320 size_t uval_len;
1321 bool write_octect = true;
1322 bool ret;
1324 eq = strchr(s, '=');
1325 if (!eq) {
1326 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1327 "Invalid filter, missing equal sign\n");
1328 return false;
1331 val = eq + 1;
1332 e = eq - 1;
1334 switch (*e) {
1335 case '<':
1336 asn1_push_tag(data, TLDAP_FILTER_LE);
1337 break;
1339 case '>':
1340 asn1_push_tag(data, TLDAP_FILTER_GE);
1341 break;
1343 case '~':
1344 asn1_push_tag(data, TLDAP_FILTER_APX);
1345 break;
1347 case ':':
1348 asn1_push_tag(data, TLDAP_FILTER_EXT);
1349 write_octect = false;
1351 type = NULL;
1352 dn = NULL;
1353 rule = NULL;
1355 if (*s == ':') { /* [:dn]:rule:= value */
1356 if (s == e) {
1357 /* malformed filter */
1358 return false;
1360 dn = s;
1361 } else { /* type[:dn][:rule]:= value */
1362 type = s;
1363 dn = strchr(s, ':');
1364 type_len = dn - type;
1365 if (dn == e) { /* type:= value */
1366 dn = NULL;
1369 if (dn) {
1370 dn++;
1372 rule = strchr(dn, ':');
1373 if (rule == NULL) {
1374 return false;
1376 if ((rule == dn + 1) || rule + 1 == e) {
1377 /* malformed filter, contains "::" */
1378 return false;
1381 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1382 if (rule == e) {
1383 rule = dn;
1384 dn = NULL;
1385 } else {
1386 /* malformed filter. With two
1387 * optionals, the first must be "dn"
1389 return false;
1391 } else {
1392 if (rule == e) {
1393 rule = NULL;
1394 } else {
1395 rule++;
1400 if (!type && !dn && !rule) {
1401 /* malformed filter, there must be at least one */
1402 return false;
1406 MatchingRuleAssertion ::= SEQUENCE {
1407 matchingRule [1] MatchingRuleID OPTIONAL,
1408 type [2] AttributeDescription OPTIONAL,
1409 matchValue [3] AssertionValue,
1410 dnAttributes [4] BOOLEAN DEFAULT FALSE
1414 /* check and add rule */
1415 if (rule) {
1416 ret = tldap_is_attrdesc(rule, e - rule, true);
1417 if (!ret) {
1418 return false;
1420 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1421 asn1_write(data, rule, e - rule);
1422 asn1_pop_tag(data);
1425 /* check and add type */
1426 if (type) {
1427 ret = tldap_is_attrdesc(type, type_len, false);
1428 if (!ret) {
1429 return false;
1431 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1432 asn1_write(data, type, type_len);
1433 asn1_pop_tag(data);
1436 uval = tldap_get_val(tmpctx, val, _s);
1437 if (!uval) {
1438 return false;
1440 uval_len = *_s - val;
1441 ret = tldap_unescape_inplace(uval, &uval_len);
1442 if (!ret) {
1443 return false;
1446 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1447 asn1_write(data, uval, uval_len);
1448 asn1_pop_tag(data);
1450 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1451 asn1_write_uint8(data, dn?1:0);
1452 asn1_pop_tag(data);
1453 break;
1455 default:
1456 e = eq;
1458 ret = tldap_is_attrdesc(s, e - s, false);
1459 if (!ret) {
1460 return false;
1463 if (strncmp(val, "*)", 2) == 0) {
1464 /* presence */
1465 asn1_push_tag(data, TLDAP_FILTER_PRES);
1466 asn1_write(data, s, e - s);
1467 *_s = val + 1;
1468 write_octect = false;
1469 break;
1472 ret = tldap_find_first_star(val, &star);
1473 if (!ret) {
1474 return false;
1476 if (*star == '*') {
1477 /* substring */
1478 asn1_push_tag(data, TLDAP_FILTER_SUB);
1479 asn1_write_OctetString(data, s, e - s);
1480 ret = tldap_push_filter_substring(ld, data, val, &s);
1481 if (!ret) {
1482 return false;
1484 *_s = s;
1485 write_octect = false;
1486 break;
1489 /* if nothing else, then it is just equality */
1490 asn1_push_tag(data, TLDAP_FILTER_EQ);
1491 write_octect = true;
1492 break;
1495 if (write_octect) {
1496 uval = tldap_get_val(tmpctx, val, _s);
1497 if (!uval) {
1498 return false;
1500 uval_len = *_s - val;
1501 ret = tldap_unescape_inplace(uval, &uval_len);
1502 if (!ret) {
1503 return false;
1506 asn1_write_OctetString(data, s, e - s);
1507 asn1_write_OctetString(data, uval, uval_len);
1510 if (data->has_error) {
1511 return false;
1513 asn1_pop_tag(data);
1514 return true;
1517 static bool tldap_push_filter_substring(struct tldap_context *ld,
1518 struct asn1_data *data,
1519 const char *val,
1520 const char **_s)
1522 TALLOC_CTX *tmpctx = talloc_tos();
1523 bool initial = true;
1524 const char *star;
1525 char *chunk;
1526 size_t chunk_len;
1527 bool ret;
1530 SubstringFilter ::= SEQUENCE {
1531 type AttributeDescription,
1532 -- at least one must be present
1533 substrings SEQUENCE OF CHOICE {
1534 initial [0] LDAPString,
1535 any [1] LDAPString,
1536 final [2] LDAPString } }
1538 asn1_push_tag(data, ASN1_SEQUENCE(0));
1540 do {
1541 ret = tldap_find_first_star(val, &star);
1542 if (!ret) {
1543 return false;
1545 chunk_len = star - val;
1547 switch (*star) {
1548 case '*':
1549 if (!initial && chunk_len == 0) {
1550 /* found '**', which is illegal */
1551 return false;
1553 break;
1554 case ')':
1555 if (initial) {
1556 /* no stars ?? */
1557 return false;
1559 /* we are done */
1560 break;
1561 default:
1562 /* ?? */
1563 return false;
1566 if (initial && chunk_len == 0) {
1567 val = star + 1;
1568 initial = false;
1569 continue;
1572 chunk = talloc_strndup(tmpctx, val, chunk_len);
1573 if (!chunk) {
1574 return false;
1576 ret = tldap_unescape_inplace(chunk, &chunk_len);
1577 if (!ret) {
1578 return false;
1580 switch (*star) {
1581 case '*':
1582 if (initial) {
1583 asn1_push_tag(data, TLDAP_SUB_INI);
1584 initial = false;
1585 } else {
1586 asn1_push_tag(data, TLDAP_SUB_ANY);
1588 break;
1589 case ')':
1590 asn1_push_tag(data, TLDAP_SUB_FIN);
1591 break;
1592 default:
1593 /* ?? */
1594 return false;
1596 asn1_write(data, chunk, chunk_len);
1597 asn1_pop_tag(data);
1599 val = star + 1;
1601 } while (*star == '*');
1603 *_s = star;
1605 /* end of sequence */
1606 asn1_pop_tag(data);
1607 return true;
1610 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1611 * around parenthesis, we do not allow any spaces (except in values of
1612 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1613 * leading or trailing spaces where allowed.
1615 static bool tldap_push_filter(struct tldap_context *ld,
1616 struct asn1_data *data,
1617 const char *filter)
1619 const char *s = filter;
1620 bool ret;
1622 ret = tldap_push_filter_int(ld, data, &s);
1623 if (ret && *s) {
1624 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1625 "Incomplete or malformed filter\n");
1626 return false;
1628 return ret;
1631 /*****************************************************************************/
1633 static void tldap_search_done(struct tevent_req *subreq);
1635 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1636 struct tevent_context *ev,
1637 struct tldap_context *ld,
1638 const char *base, int scope,
1639 const char *filter,
1640 const char **attrs,
1641 int num_attrs,
1642 int attrsonly,
1643 struct tldap_control *sctrls,
1644 int num_sctrls,
1645 struct tldap_control *cctrls,
1646 int num_cctrls,
1647 int timelimit,
1648 int sizelimit,
1649 int deref)
1651 struct tevent_req *req, *subreq;
1652 struct tldap_req_state *state;
1653 int i;
1655 req = tldap_req_create(mem_ctx, ld, &state);
1656 if (req == NULL) {
1657 return NULL;
1660 asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1661 asn1_write_OctetString(state->out, base, strlen(base));
1662 asn1_write_enumerated(state->out, scope);
1663 asn1_write_enumerated(state->out, deref);
1664 asn1_write_Integer(state->out, sizelimit);
1665 asn1_write_Integer(state->out, timelimit);
1666 asn1_write_BOOLEAN(state->out, attrsonly);
1668 if (!tldap_push_filter(ld, state->out, filter)) {
1669 goto encoding_error;
1672 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1673 for (i=0; i<num_attrs; i++) {
1674 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1676 asn1_pop_tag(state->out);
1677 asn1_pop_tag(state->out);
1679 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1680 sctrls, num_sctrls);
1681 if (tevent_req_nomem(subreq, req)) {
1682 return tevent_req_post(req, ev);
1684 tevent_req_set_callback(subreq, tldap_search_done, req);
1685 return req;
1687 encoding_error:
1688 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1689 return tevent_req_post(req, ev);
1692 static void tldap_search_done(struct tevent_req *subreq)
1694 struct tevent_req *req = tevent_req_callback_data(
1695 subreq, struct tevent_req);
1696 struct tldap_req_state *state = tevent_req_data(
1697 req, struct tldap_req_state);
1698 int err;
1700 err = tldap_msg_recv(subreq, state, &state->result);
1701 if (err != TLDAP_SUCCESS) {
1702 tevent_req_error(req, err);
1703 return;
1705 switch (state->result->type) {
1706 case TLDAP_RES_SEARCH_ENTRY:
1707 case TLDAP_RES_SEARCH_REFERENCE:
1708 if (!tldap_msg_set_pending(subreq)) {
1709 tevent_req_oom(req);
1710 return;
1712 tevent_req_notify_callback(req);
1713 break;
1714 case TLDAP_RES_SEARCH_RESULT:
1715 TALLOC_FREE(subreq);
1716 if (!asn1_start_tag(state->result->data,
1717 state->result->type) ||
1718 !tldap_decode_response(state) ||
1719 !asn1_end_tag(state->result->data) ||
1720 !tldap_decode_controls(state)) {
1721 tevent_req_error(req, TLDAP_DECODING_ERROR);
1722 return;
1724 tevent_req_done(req);
1725 break;
1726 default:
1727 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1728 return;
1732 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1733 struct tldap_message **pmsg)
1735 struct tldap_req_state *state = tevent_req_data(
1736 req, struct tldap_req_state);
1737 int err;
1739 if (!tevent_req_is_in_progress(req)
1740 && tevent_req_is_ldap_error(req, &err)) {
1741 return err;
1744 if (tevent_req_is_in_progress(req)) {
1745 switch (state->result->type) {
1746 case TLDAP_RES_SEARCH_ENTRY:
1747 case TLDAP_RES_SEARCH_REFERENCE:
1748 break;
1749 default:
1750 return TLDAP_OPERATIONS_ERROR;
1754 *pmsg = talloc_move(mem_ctx, &state->result);
1755 return TLDAP_SUCCESS;
1758 struct tldap_sync_search_state {
1759 TALLOC_CTX *mem_ctx;
1760 struct tldap_message **entries;
1761 struct tldap_message **refs;
1762 int rc;
1765 static void tldap_search_cb(struct tevent_req *req)
1767 struct tldap_sync_search_state *state =
1768 (struct tldap_sync_search_state *)
1769 tevent_req_callback_data_void(req);
1770 struct tldap_message *msg, **tmp;
1771 int rc, num_entries, num_refs;
1773 rc = tldap_search_recv(req, talloc_tos(), &msg);
1774 if (rc != TLDAP_SUCCESS) {
1775 state->rc = rc;
1776 return;
1779 switch (tldap_msg_type(msg)) {
1780 case TLDAP_RES_SEARCH_ENTRY:
1781 num_entries = talloc_array_length(state->entries);
1782 tmp = talloc_realloc(state->mem_ctx, state->entries,
1783 struct tldap_message *, num_entries + 1);
1784 if (tmp == NULL) {
1785 state->rc = TLDAP_NO_MEMORY;
1786 return;
1788 state->entries = tmp;
1789 state->entries[num_entries] = talloc_move(state->entries,
1790 &msg);
1791 break;
1792 case TLDAP_RES_SEARCH_REFERENCE:
1793 num_refs = talloc_array_length(state->refs);
1794 tmp = talloc_realloc(state->mem_ctx, state->refs,
1795 struct tldap_message *, num_refs + 1);
1796 if (tmp == NULL) {
1797 state->rc = TLDAP_NO_MEMORY;
1798 return;
1800 state->refs = tmp;
1801 state->refs[num_refs] = talloc_move(state->refs, &msg);
1802 break;
1803 case TLDAP_RES_SEARCH_RESULT:
1804 state->rc = TLDAP_SUCCESS;
1805 break;
1806 default:
1807 state->rc = TLDAP_PROTOCOL_ERROR;
1808 break;
1812 int tldap_search(struct tldap_context *ld,
1813 const char *base, int scope, const char *filter,
1814 const char **attrs, int num_attrs, int attrsonly,
1815 struct tldap_control *sctrls, int num_sctrls,
1816 struct tldap_control *cctrls, int num_cctrls,
1817 int timelimit, int sizelimit, int deref,
1818 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1819 struct tldap_message ***refs)
1821 TALLOC_CTX *frame = talloc_stackframe();
1822 struct tevent_context *ev;
1823 struct tevent_req *req;
1824 struct tldap_sync_search_state state;
1826 ZERO_STRUCT(state);
1827 state.mem_ctx = mem_ctx;
1828 state.rc = TLDAP_SUCCESS;
1830 ev = samba_tevent_context_init(frame);
1831 if (ev == NULL) {
1832 state.rc = TLDAP_NO_MEMORY;
1833 goto fail;
1836 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1837 attrs, num_attrs, attrsonly,
1838 sctrls, num_sctrls, cctrls, num_cctrls,
1839 timelimit, sizelimit, deref);
1840 if (req == NULL) {
1841 state.rc = TLDAP_NO_MEMORY;
1842 goto fail;
1845 tevent_req_set_callback(req, tldap_search_cb, &state);
1847 if (!tevent_req_is_in_progress(req)) {
1848 /* an error happend before sending */
1849 if (tevent_req_is_ldap_error(req, &state.rc)) {
1850 goto fail;
1854 while (tevent_req_is_in_progress(req)
1855 && (state.rc == TLDAP_SUCCESS)) {
1856 if (tevent_loop_once(ev) == -1) {
1857 return TLDAP_OPERATIONS_ERROR;
1861 if (state.rc != TLDAP_SUCCESS) {
1862 return state.rc;
1865 if (entries != NULL) {
1866 *entries = state.entries;
1867 } else {
1868 TALLOC_FREE(state.entries);
1870 if (refs != NULL) {
1871 *refs = state.refs;
1872 } else {
1873 TALLOC_FREE(state.refs);
1875 tldap_save_msg(ld, req);
1876 fail:
1877 TALLOC_FREE(frame);
1878 return state.rc;
1881 static bool tldap_parse_search_entry(struct tldap_message *msg)
1883 int num_attribs = 0;
1885 asn1_start_tag(msg->data, msg->type);
1887 /* dn */
1889 asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1890 if (msg->dn == NULL) {
1891 return false;
1895 * Attributes: We overallocate msg->attribs by one, so that while
1896 * looping over the attributes we can directly parse into the last
1897 * array element. Same for the values in the inner loop.
1900 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1901 if (msg->attribs == NULL) {
1902 return false;
1905 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1906 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1907 struct tldap_attribute *attrib;
1908 int num_values = 0;
1910 attrib = &msg->attribs[num_attribs];
1911 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1912 if (attrib->values == NULL) {
1913 return false;
1915 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1916 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1917 &attrib->name);
1918 asn1_start_tag(msg->data, ASN1_SET);
1920 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1921 asn1_read_OctetString(msg->data, msg,
1922 &attrib->values[num_values]);
1924 attrib->values = talloc_realloc(
1925 msg->attribs, attrib->values, DATA_BLOB,
1926 num_values + 2);
1927 if (attrib->values == NULL) {
1928 return false;
1930 num_values += 1;
1932 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1933 DATA_BLOB, num_values);
1934 attrib->num_values = num_values;
1936 asn1_end_tag(msg->data); /* ASN1_SET */
1937 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1938 msg->attribs = talloc_realloc(
1939 msg, msg->attribs, struct tldap_attribute,
1940 num_attribs + 2);
1941 if (msg->attribs == NULL) {
1942 return false;
1944 num_attribs += 1;
1946 msg->attribs = talloc_realloc(
1947 msg, msg->attribs, struct tldap_attribute, num_attribs);
1948 asn1_end_tag(msg->data);
1949 if (msg->data->has_error) {
1950 return false;
1952 return true;
1955 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1957 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1958 return false;
1960 *dn = msg->dn;
1961 return true;
1964 bool tldap_entry_attributes(struct tldap_message *msg,
1965 struct tldap_attribute **attributes,
1966 int *num_attributes)
1968 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1969 return false;
1971 *attributes = msg->attribs;
1972 *num_attributes = talloc_array_length(msg->attribs);
1973 return true;
1976 static bool tldap_decode_controls(struct tldap_req_state *state)
1978 struct tldap_message *msg = state->result;
1979 struct asn1_data *data = msg->data;
1980 struct tldap_control *sctrls = NULL;
1981 int num_controls = 0;
1983 msg->res_sctrls = NULL;
1985 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1986 return true;
1989 asn1_start_tag(data, ASN1_CONTEXT(0));
1991 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
1992 struct tldap_control *c;
1993 char *oid = NULL;
1995 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
1996 num_controls + 1);
1997 if (sctrls == NULL) {
1998 return false;
2000 c = &sctrls[num_controls];
2002 asn1_start_tag(data, ASN1_SEQUENCE(0));
2003 asn1_read_OctetString_talloc(msg, data, &oid);
2004 if ((data->has_error) || (oid == NULL)) {
2005 return false;
2007 c->oid = oid;
2008 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2009 asn1_read_BOOLEAN(data, &c->critical);
2010 } else {
2011 c->critical = false;
2013 c->value = data_blob_null;
2014 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2015 !asn1_read_OctetString(data, msg, &c->value)) {
2016 return false;
2018 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
2020 num_controls += 1;
2023 asn1_end_tag(data); /* ASN1_CONTEXT(0) */
2025 if (data->has_error) {
2026 TALLOC_FREE(sctrls);
2027 return false;
2029 msg->res_sctrls = sctrls;
2030 return true;
2033 static void tldap_simple_done(struct tevent_req *subreq, int type)
2035 struct tevent_req *req = tevent_req_callback_data(
2036 subreq, struct tevent_req);
2037 struct tldap_req_state *state = tevent_req_data(
2038 req, struct tldap_req_state);
2039 int err;
2041 err = tldap_msg_recv(subreq, state, &state->result);
2042 TALLOC_FREE(subreq);
2043 if (err != TLDAP_SUCCESS) {
2044 tevent_req_error(req, err);
2045 return;
2047 if (state->result->type != type) {
2048 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2049 return;
2051 if (!asn1_start_tag(state->result->data, state->result->type) ||
2052 !tldap_decode_response(state) ||
2053 !asn1_end_tag(state->result->data) ||
2054 !tldap_decode_controls(state)) {
2055 tevent_req_error(req, TLDAP_DECODING_ERROR);
2056 return;
2058 if (state->result->lderr != TLDAP_SUCCESS) {
2059 tevent_req_error(req, state->result->lderr);
2060 return;
2062 tevent_req_done(req);
2065 static int tldap_simple_recv(struct tevent_req *req)
2067 int err;
2068 if (tevent_req_is_ldap_error(req, &err)) {
2069 return err;
2071 return TLDAP_SUCCESS;
2074 static void tldap_add_done(struct tevent_req *subreq);
2076 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2077 struct tevent_context *ev,
2078 struct tldap_context *ld,
2079 const char *dn,
2080 struct tldap_mod *attributes,
2081 int num_attributes,
2082 struct tldap_control *sctrls,
2083 int num_sctrls,
2084 struct tldap_control *cctrls,
2085 int num_cctrls)
2087 struct tevent_req *req, *subreq;
2088 struct tldap_req_state *state;
2089 int i, j;
2091 req = tldap_req_create(mem_ctx, ld, &state);
2092 if (req == NULL) {
2093 return NULL;
2096 asn1_push_tag(state->out, TLDAP_REQ_ADD);
2097 asn1_write_OctetString(state->out, dn, strlen(dn));
2098 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2100 for (i=0; i<num_attributes; i++) {
2101 struct tldap_mod *attrib = &attributes[i];
2102 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2103 asn1_write_OctetString(state->out, attrib->attribute,
2104 strlen(attrib->attribute));
2105 asn1_push_tag(state->out, ASN1_SET);
2106 for (j=0; j<attrib->num_values; j++) {
2107 asn1_write_OctetString(state->out,
2108 attrib->values[j].data,
2109 attrib->values[j].length);
2111 asn1_pop_tag(state->out);
2112 asn1_pop_tag(state->out);
2115 asn1_pop_tag(state->out);
2116 asn1_pop_tag(state->out);
2118 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2119 sctrls, num_sctrls);
2120 if (tevent_req_nomem(subreq, req)) {
2121 return tevent_req_post(req, ev);
2123 tevent_req_set_callback(subreq, tldap_add_done, req);
2124 return req;
2127 static void tldap_add_done(struct tevent_req *subreq)
2129 tldap_simple_done(subreq, TLDAP_RES_ADD);
2132 int tldap_add_recv(struct tevent_req *req)
2134 return tldap_simple_recv(req);
2137 int tldap_add(struct tldap_context *ld, const char *dn,
2138 struct tldap_mod *attributes, int num_attributes,
2139 struct tldap_control *sctrls, int num_sctrls,
2140 struct tldap_control *cctrls, int num_cctrls)
2142 TALLOC_CTX *frame = talloc_stackframe();
2143 struct tevent_context *ev;
2144 struct tevent_req *req;
2145 int result;
2147 ev = samba_tevent_context_init(frame);
2148 if (ev == NULL) {
2149 result = TLDAP_NO_MEMORY;
2150 goto fail;
2153 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2154 sctrls, num_sctrls, cctrls, num_cctrls);
2155 if (req == NULL) {
2156 result = TLDAP_NO_MEMORY;
2157 goto fail;
2160 if (!tevent_req_poll(req, ev)) {
2161 result = TLDAP_OPERATIONS_ERROR;
2162 goto fail;
2165 result = tldap_add_recv(req);
2166 tldap_save_msg(ld, req);
2167 fail:
2168 TALLOC_FREE(frame);
2169 return result;
2172 static void tldap_modify_done(struct tevent_req *subreq);
2174 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2175 struct tevent_context *ev,
2176 struct tldap_context *ld,
2177 const char *dn,
2178 struct tldap_mod *mods, int num_mods,
2179 struct tldap_control *sctrls,
2180 int num_sctrls,
2181 struct tldap_control *cctrls,
2182 int num_cctrls)
2184 struct tevent_req *req, *subreq;
2185 struct tldap_req_state *state;
2186 int i, j;
2188 req = tldap_req_create(mem_ctx, ld, &state);
2189 if (req == NULL) {
2190 return NULL;
2193 asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
2194 asn1_write_OctetString(state->out, dn, strlen(dn));
2195 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2197 for (i=0; i<num_mods; i++) {
2198 struct tldap_mod *mod = &mods[i];
2199 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2200 asn1_write_enumerated(state->out, mod->mod_op),
2201 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2202 asn1_write_OctetString(state->out, mod->attribute,
2203 strlen(mod->attribute));
2204 asn1_push_tag(state->out, ASN1_SET);
2205 for (j=0; j<mod->num_values; j++) {
2206 asn1_write_OctetString(state->out,
2207 mod->values[j].data,
2208 mod->values[j].length);
2210 asn1_pop_tag(state->out);
2211 asn1_pop_tag(state->out);
2212 asn1_pop_tag(state->out);
2215 asn1_pop_tag(state->out);
2216 asn1_pop_tag(state->out);
2218 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2219 sctrls, num_sctrls);
2220 if (tevent_req_nomem(subreq, req)) {
2221 return tevent_req_post(req, ev);
2223 tevent_req_set_callback(subreq, tldap_modify_done, req);
2224 return req;
2227 static void tldap_modify_done(struct tevent_req *subreq)
2229 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2232 int tldap_modify_recv(struct tevent_req *req)
2234 return tldap_simple_recv(req);
2237 int tldap_modify(struct tldap_context *ld, const char *dn,
2238 struct tldap_mod *mods, int num_mods,
2239 struct tldap_control *sctrls, int num_sctrls,
2240 struct tldap_control *cctrls, int num_cctrls)
2242 TALLOC_CTX *frame = talloc_stackframe();
2243 struct tevent_context *ev;
2244 struct tevent_req *req;
2245 int result;
2247 ev = samba_tevent_context_init(frame);
2248 if (ev == NULL) {
2249 result = TLDAP_NO_MEMORY;
2250 goto fail;
2253 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2254 sctrls, num_sctrls, cctrls, num_cctrls);
2255 if (req == NULL) {
2256 result = TLDAP_NO_MEMORY;
2257 goto fail;
2260 if (!tevent_req_poll(req, ev)) {
2261 result = TLDAP_OPERATIONS_ERROR;
2262 goto fail;
2265 result = tldap_modify_recv(req);
2266 tldap_save_msg(ld, req);
2267 fail:
2268 TALLOC_FREE(frame);
2269 return result;
2272 static void tldap_delete_done(struct tevent_req *subreq);
2274 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2275 struct tevent_context *ev,
2276 struct tldap_context *ld,
2277 const char *dn,
2278 struct tldap_control *sctrls,
2279 int num_sctrls,
2280 struct tldap_control *cctrls,
2281 int num_cctrls)
2283 struct tevent_req *req, *subreq;
2284 struct tldap_req_state *state;
2286 req = tldap_req_create(mem_ctx, ld, &state);
2287 if (req == NULL) {
2288 return NULL;
2291 asn1_push_tag(state->out, TLDAP_REQ_DELETE);
2292 asn1_write(state->out, dn, strlen(dn));
2293 asn1_pop_tag(state->out);
2295 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2296 sctrls, num_sctrls);
2297 if (tevent_req_nomem(subreq, req)) {
2298 return tevent_req_post(req, ev);
2300 tevent_req_set_callback(subreq, tldap_delete_done, req);
2301 return req;
2304 static void tldap_delete_done(struct tevent_req *subreq)
2306 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2309 int tldap_delete_recv(struct tevent_req *req)
2311 return tldap_simple_recv(req);
2314 int tldap_delete(struct tldap_context *ld, const char *dn,
2315 struct tldap_control *sctrls, int num_sctrls,
2316 struct tldap_control *cctrls, int num_cctrls)
2318 TALLOC_CTX *frame = talloc_stackframe();
2319 struct tevent_context *ev;
2320 struct tevent_req *req;
2321 int result;
2323 ev = samba_tevent_context_init(frame);
2324 if (ev == NULL) {
2325 result = TLDAP_NO_MEMORY;
2326 goto fail;
2329 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2330 cctrls, num_cctrls);
2331 if (req == NULL) {
2332 result = TLDAP_NO_MEMORY;
2333 goto fail;
2336 if (!tevent_req_poll(req, ev)) {
2337 result = TLDAP_OPERATIONS_ERROR;
2338 goto fail;
2341 result = tldap_delete_recv(req);
2342 tldap_save_msg(ld, req);
2343 fail:
2344 TALLOC_FREE(frame);
2345 return result;
2348 int tldap_msg_id(const struct tldap_message *msg)
2350 return msg->id;
2353 int tldap_msg_type(const struct tldap_message *msg)
2355 return msg->type;
2358 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2360 if (msg == NULL) {
2361 return NULL;
2363 return msg->res_matcheddn;
2366 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2368 if (msg == NULL) {
2369 return NULL;
2371 return msg->res_diagnosticmessage;
2374 const char *tldap_msg_referral(struct tldap_message *msg)
2376 if (msg == NULL) {
2377 return NULL;
2379 return msg->res_referral;
2382 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2383 struct tldap_control **sctrls)
2385 if (msg == NULL) {
2386 *sctrls = NULL;
2387 *num_sctrls = 0;
2388 return;
2390 *sctrls = msg->res_sctrls;
2391 *num_sctrls = talloc_array_length(msg->res_sctrls);
2394 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2396 return ld->last_msg;
2399 const char *tldap_err2string(int rc)
2401 const char *res = NULL;
2404 * This would normally be a table, but the error codes are not fully
2405 * sequential. Let the compiler figure out the optimum implementation
2406 * :-)
2409 switch (rc) {
2410 case TLDAP_SUCCESS:
2411 res = "TLDAP_SUCCESS";
2412 break;
2413 case TLDAP_OPERATIONS_ERROR:
2414 res = "TLDAP_OPERATIONS_ERROR";
2415 break;
2416 case TLDAP_PROTOCOL_ERROR:
2417 res = "TLDAP_PROTOCOL_ERROR";
2418 break;
2419 case TLDAP_TIMELIMIT_EXCEEDED:
2420 res = "TLDAP_TIMELIMIT_EXCEEDED";
2421 break;
2422 case TLDAP_SIZELIMIT_EXCEEDED:
2423 res = "TLDAP_SIZELIMIT_EXCEEDED";
2424 break;
2425 case TLDAP_COMPARE_FALSE:
2426 res = "TLDAP_COMPARE_FALSE";
2427 break;
2428 case TLDAP_COMPARE_TRUE:
2429 res = "TLDAP_COMPARE_TRUE";
2430 break;
2431 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2432 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2433 break;
2434 case TLDAP_STRONG_AUTH_REQUIRED:
2435 res = "TLDAP_STRONG_AUTH_REQUIRED";
2436 break;
2437 case TLDAP_REFERRAL:
2438 res = "TLDAP_REFERRAL";
2439 break;
2440 case TLDAP_ADMINLIMIT_EXCEEDED:
2441 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2442 break;
2443 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2444 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2445 break;
2446 case TLDAP_CONFIDENTIALITY_REQUIRED:
2447 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2448 break;
2449 case TLDAP_SASL_BIND_IN_PROGRESS:
2450 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2451 break;
2452 case TLDAP_NO_SUCH_ATTRIBUTE:
2453 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2454 break;
2455 case TLDAP_UNDEFINED_TYPE:
2456 res = "TLDAP_UNDEFINED_TYPE";
2457 break;
2458 case TLDAP_INAPPROPRIATE_MATCHING:
2459 res = "TLDAP_INAPPROPRIATE_MATCHING";
2460 break;
2461 case TLDAP_CONSTRAINT_VIOLATION:
2462 res = "TLDAP_CONSTRAINT_VIOLATION";
2463 break;
2464 case TLDAP_TYPE_OR_VALUE_EXISTS:
2465 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2466 break;
2467 case TLDAP_INVALID_SYNTAX:
2468 res = "TLDAP_INVALID_SYNTAX";
2469 break;
2470 case TLDAP_NO_SUCH_OBJECT:
2471 res = "TLDAP_NO_SUCH_OBJECT";
2472 break;
2473 case TLDAP_ALIAS_PROBLEM:
2474 res = "TLDAP_ALIAS_PROBLEM";
2475 break;
2476 case TLDAP_INVALID_DN_SYNTAX:
2477 res = "TLDAP_INVALID_DN_SYNTAX";
2478 break;
2479 case TLDAP_IS_LEAF:
2480 res = "TLDAP_IS_LEAF";
2481 break;
2482 case TLDAP_ALIAS_DEREF_PROBLEM:
2483 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2484 break;
2485 case TLDAP_INAPPROPRIATE_AUTH:
2486 res = "TLDAP_INAPPROPRIATE_AUTH";
2487 break;
2488 case TLDAP_INVALID_CREDENTIALS:
2489 res = "TLDAP_INVALID_CREDENTIALS";
2490 break;
2491 case TLDAP_INSUFFICIENT_ACCESS:
2492 res = "TLDAP_INSUFFICIENT_ACCESS";
2493 break;
2494 case TLDAP_BUSY:
2495 res = "TLDAP_BUSY";
2496 break;
2497 case TLDAP_UNAVAILABLE:
2498 res = "TLDAP_UNAVAILABLE";
2499 break;
2500 case TLDAP_UNWILLING_TO_PERFORM:
2501 res = "TLDAP_UNWILLING_TO_PERFORM";
2502 break;
2503 case TLDAP_LOOP_DETECT:
2504 res = "TLDAP_LOOP_DETECT";
2505 break;
2506 case TLDAP_NAMING_VIOLATION:
2507 res = "TLDAP_NAMING_VIOLATION";
2508 break;
2509 case TLDAP_OBJECT_CLASS_VIOLATION:
2510 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2511 break;
2512 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2513 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2514 break;
2515 case TLDAP_NOT_ALLOWED_ON_RDN:
2516 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2517 break;
2518 case TLDAP_ALREADY_EXISTS:
2519 res = "TLDAP_ALREADY_EXISTS";
2520 break;
2521 case TLDAP_NO_OBJECT_CLASS_MODS:
2522 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2523 break;
2524 case TLDAP_RESULTS_TOO_LARGE:
2525 res = "TLDAP_RESULTS_TOO_LARGE";
2526 break;
2527 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2528 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2529 break;
2530 case TLDAP_OTHER:
2531 res = "TLDAP_OTHER";
2532 break;
2533 case TLDAP_SERVER_DOWN:
2534 res = "TLDAP_SERVER_DOWN";
2535 break;
2536 case TLDAP_LOCAL_ERROR:
2537 res = "TLDAP_LOCAL_ERROR";
2538 break;
2539 case TLDAP_ENCODING_ERROR:
2540 res = "TLDAP_ENCODING_ERROR";
2541 break;
2542 case TLDAP_DECODING_ERROR:
2543 res = "TLDAP_DECODING_ERROR";
2544 break;
2545 case TLDAP_TIMEOUT:
2546 res = "TLDAP_TIMEOUT";
2547 break;
2548 case TLDAP_AUTH_UNKNOWN:
2549 res = "TLDAP_AUTH_UNKNOWN";
2550 break;
2551 case TLDAP_FILTER_ERROR:
2552 res = "TLDAP_FILTER_ERROR";
2553 break;
2554 case TLDAP_USER_CANCELLED:
2555 res = "TLDAP_USER_CANCELLED";
2556 break;
2557 case TLDAP_PARAM_ERROR:
2558 res = "TLDAP_PARAM_ERROR";
2559 break;
2560 case TLDAP_NO_MEMORY:
2561 res = "TLDAP_NO_MEMORY";
2562 break;
2563 case TLDAP_CONNECT_ERROR:
2564 res = "TLDAP_CONNECT_ERROR";
2565 break;
2566 case TLDAP_NOT_SUPPORTED:
2567 res = "TLDAP_NOT_SUPPORTED";
2568 break;
2569 case TLDAP_CONTROL_NOT_FOUND:
2570 res = "TLDAP_CONTROL_NOT_FOUND";
2571 break;
2572 case TLDAP_NO_RESULTS_RETURNED:
2573 res = "TLDAP_NO_RESULTS_RETURNED";
2574 break;
2575 case TLDAP_MORE_RESULTS_TO_RETURN:
2576 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2577 break;
2578 case TLDAP_CLIENT_LOOP:
2579 res = "TLDAP_CLIENT_LOOP";
2580 break;
2581 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2582 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2583 break;
2584 default:
2585 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2586 rc);
2587 break;
2589 if (res == NULL) {
2590 res = "Unknown LDAP Error";
2592 return res;