s4:auth/system_session.c - check for OOM
[Samba/id10ts.git] / source3 / lib / tldap.c
blob2f45a056df8c0a05f407c673168c5e2984b51a2f
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"
24 static int tldap_simple_recv(struct tevent_req *req);
26 bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
28 enum tevent_req_state state;
29 uint64_t err;
31 if (!tevent_req_is_error(req, &state, &err)) {
32 return false;
34 switch (state) {
35 case TEVENT_REQ_TIMED_OUT:
36 *perr = TLDAP_TIMEOUT;
37 break;
38 case TEVENT_REQ_NO_MEMORY:
39 *perr = TLDAP_NO_MEMORY;
40 break;
41 case TEVENT_REQ_USER_ERROR:
42 *perr = err;
43 break;
44 default:
45 *perr = TLDAP_OPERATIONS_ERROR;
46 break;
48 return true;
51 struct tldap_ctx_attribute {
52 char *name;
53 void *ptr;
56 struct tldap_context {
57 int ld_version;
58 int ld_deref;
59 int ld_sizelimit;
60 int ld_timelimit;
61 struct tstream_context *conn;
62 bool server_down;
63 int msgid;
64 struct tevent_queue *outgoing;
65 struct tevent_req **pending;
67 /* For the sync wrappers we need something like get_last_error... */
68 struct tldap_message *last_msg;
70 /* debug */
71 void (*log_fn)(void *context, enum tldap_debug_level level,
72 const char *fmt, va_list ap);
73 void *log_private;
75 struct tldap_ctx_attribute *ctx_attrs;
78 struct tldap_message {
79 struct asn1_data *data;
80 uint8_t *inbuf;
81 int type;
82 int id;
84 /* RESULT_ENTRY */
85 char *dn;
86 struct tldap_attribute *attribs;
88 /* Error data sent by the server */
89 int lderr;
90 char *res_matcheddn;
91 char *res_diagnosticmessage;
92 char *res_referral;
93 struct tldap_control *res_sctrls;
95 /* Controls sent by the server */
96 struct tldap_control *ctrls;
99 void tldap_set_debug(struct tldap_context *ld,
100 void (*log_fn)(void *log_private,
101 enum tldap_debug_level level,
102 const char *fmt,
103 va_list ap) PRINTF_ATTRIBUTE(3,0),
104 void *log_private)
106 ld->log_fn = log_fn;
107 ld->log_private = log_private;
110 static void tldap_debug(struct tldap_context *ld,
111 enum tldap_debug_level level,
112 const char *fmt, ...)
114 va_list ap;
115 if (!ld) {
116 return;
118 if (ld->log_fn == NULL) {
119 return;
121 va_start(ap, fmt);
122 ld->log_fn(ld->log_private, level, fmt, ap);
123 va_end(ap);
126 static int tldap_next_msgid(struct tldap_context *ld)
128 int result;
130 result = ld->msgid++;
131 if (ld->msgid == 2147483647) {
132 ld->msgid = 1;
134 return result;
137 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
139 struct tldap_context *ctx;
140 int ret;
142 ctx = talloc_zero(mem_ctx, struct tldap_context);
143 if (ctx == NULL) {
144 return NULL;
146 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
147 if (ret == -1) {
148 TALLOC_FREE(ctx);
149 return NULL;
151 ctx->msgid = 1;
152 ctx->ld_version = 3;
153 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
154 if (ctx->outgoing == NULL) {
155 TALLOC_FREE(ctx);
156 return NULL;
158 return ctx;
161 bool tldap_connection_ok(struct tldap_context *ld)
163 if (ld == NULL) {
164 return false;
166 return !ld->server_down;
169 static struct tldap_ctx_attribute *tldap_context_findattr(
170 struct tldap_context *ld, const char *name)
172 int i, num_attrs;
174 num_attrs = talloc_array_length(ld->ctx_attrs);
176 for (i=0; i<num_attrs; i++) {
177 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
178 return &ld->ctx_attrs[i];
181 return NULL;
184 bool tldap_context_setattr(struct tldap_context *ld,
185 const char *name, const void *_pptr)
187 struct tldap_ctx_attribute *tmp, *attr;
188 char *tmpname;
189 int num_attrs;
190 void **pptr = (void **)_pptr;
192 attr = tldap_context_findattr(ld, name);
193 if (attr != NULL) {
195 * We don't actually delete attrs, we don't expect tons of
196 * attributes being shuffled around.
198 TALLOC_FREE(attr->ptr);
199 if (*pptr != NULL) {
200 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
201 *pptr = NULL;
203 return true;
206 tmpname = talloc_strdup(ld, name);
207 if (tmpname == NULL) {
208 return false;
211 num_attrs = talloc_array_length(ld->ctx_attrs);
213 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
214 num_attrs+1);
215 if (tmp == NULL) {
216 TALLOC_FREE(tmpname);
217 return false;
219 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
220 if (*pptr != NULL) {
221 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
222 } else {
223 tmp[num_attrs].ptr = NULL;
225 *pptr = NULL;
226 ld->ctx_attrs = tmp;
227 return true;
230 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
232 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
234 if (attr == NULL) {
235 return NULL;
237 return attr->ptr;
240 struct read_ldap_state {
241 uint8_t *buf;
242 bool done;
245 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
246 static void read_ldap_done(struct tevent_req *subreq);
248 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
249 struct tevent_context *ev,
250 struct tstream_context *conn)
252 struct tevent_req *req, *subreq;
253 struct read_ldap_state *state;
255 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
256 if (req == NULL) {
257 return NULL;
259 state->done = false;
261 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
262 state);
263 if (tevent_req_nomem(subreq, req)) {
264 return tevent_req_post(req, ev);
266 tevent_req_set_callback(subreq, read_ldap_done, req);
267 return req;
270 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
272 struct read_ldap_state *state = talloc_get_type_abort(
273 private_data, struct read_ldap_state);
274 size_t len;
275 int i, lensize;
277 if (state->done) {
278 /* We've been here, we're done */
279 return 0;
283 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
285 if (buf[0] != 0x30) {
286 return -1;
289 len = buf[1];
290 if ((len & 0x80) == 0) {
291 state->done = true;
292 return len;
295 lensize = (len & 0x7f);
296 len = 0;
298 if (buflen == 2) {
299 /* Please get us the full length */
300 return lensize;
302 if (buflen > 2 + lensize) {
303 state->done = true;
304 return 0;
306 if (buflen != 2 + lensize) {
307 return -1;
310 for (i=0; i<lensize; i++) {
311 len = (len << 8) | buf[2+i];
313 return len;
316 static void read_ldap_done(struct tevent_req *subreq)
318 struct tevent_req *req = tevent_req_callback_data(
319 subreq, struct tevent_req);
320 struct read_ldap_state *state = tevent_req_data(
321 req, struct read_ldap_state);
322 ssize_t nread;
323 int err;
325 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
326 TALLOC_FREE(subreq);
327 if (nread == -1) {
328 tevent_req_error(req, err);
329 return;
331 tevent_req_done(req);
334 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
335 uint8_t **pbuf, int *perrno)
337 struct read_ldap_state *state = tevent_req_data(
338 req, struct read_ldap_state);
340 if (tevent_req_is_unix_error(req, perrno)) {
341 return -1;
343 *pbuf = talloc_move(mem_ctx, &state->buf);
344 return talloc_get_size(*pbuf);
347 struct tldap_msg_state {
348 struct tldap_context *ld;
349 struct tevent_context *ev;
350 int id;
351 struct iovec iov;
353 struct asn1_data *data;
354 uint8_t *inbuf;
357 static void tldap_push_controls(struct asn1_data *data,
358 struct tldap_control *sctrls,
359 int num_sctrls)
361 int i;
363 if ((sctrls == NULL) || (num_sctrls == 0)) {
364 return;
367 asn1_push_tag(data, ASN1_CONTEXT(0));
369 for (i=0; i<num_sctrls; i++) {
370 struct tldap_control *c = &sctrls[i];
371 asn1_push_tag(data, ASN1_SEQUENCE(0));
372 asn1_write_OctetString(data, c->oid, strlen(c->oid));
373 if (c->critical) {
374 asn1_write_BOOLEAN(data, true);
376 if (c->value.data != NULL) {
377 asn1_write_OctetString(data, c->value.data,
378 c->value.length);
380 asn1_pop_tag(data); /* ASN1_SEQUENCE(0) */
383 asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
386 static void tldap_msg_sent(struct tevent_req *subreq);
387 static void tldap_msg_received(struct tevent_req *subreq);
389 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
390 struct tevent_context *ev,
391 struct tldap_context *ld,
392 int id, struct asn1_data *data,
393 struct tldap_control *sctrls,
394 int num_sctrls)
396 struct tevent_req *req, *subreq;
397 struct tldap_msg_state *state;
398 DATA_BLOB blob;
400 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
401 id);
403 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
404 if (req == NULL) {
405 return NULL;
407 state->ld = ld;
408 state->ev = ev;
409 state->id = id;
411 if (state->ld->server_down) {
412 tevent_req_error(req, TLDAP_SERVER_DOWN);
413 return tevent_req_post(req, ev);
416 tldap_push_controls(data, sctrls, num_sctrls);
418 asn1_pop_tag(data);
420 if (!asn1_blob(data, &blob)) {
421 tevent_req_error(req, TLDAP_ENCODING_ERROR);
422 return tevent_req_post(req, ev);
425 state->iov.iov_base = (void *)blob.data;
426 state->iov.iov_len = blob.length;
428 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
429 &state->iov, 1);
430 if (tevent_req_nomem(subreq, req)) {
431 return tevent_req_post(req, ev);
433 tevent_req_set_callback(subreq, tldap_msg_sent, req);
434 return req;
437 static void tldap_msg_unset_pending(struct tevent_req *req)
439 struct tldap_msg_state *state = tevent_req_data(
440 req, struct tldap_msg_state);
441 struct tldap_context *ld = state->ld;
442 int num_pending = talloc_array_length(ld->pending);
443 int i;
445 if (num_pending == 1) {
446 TALLOC_FREE(ld->pending);
447 return;
450 for (i=0; i<num_pending; i++) {
451 if (req == ld->pending[i]) {
452 break;
455 if (i == num_pending) {
457 * Something's seriously broken. Just returning here is the
458 * right thing nevertheless, the point of this routine is to
459 * remove ourselves from cli->pending.
461 return;
465 * Remove ourselves from the cli->pending array
467 if (num_pending > 1) {
468 ld->pending[i] = ld->pending[num_pending-1];
472 * No NULL check here, we're shrinking by sizeof(void *), and
473 * talloc_realloc just adjusts the size for this.
475 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
476 num_pending - 1);
477 return;
480 static int tldap_msg_destructor(struct tevent_req *req)
482 tldap_msg_unset_pending(req);
483 return 0;
486 static bool tldap_msg_set_pending(struct tevent_req *req)
488 struct tldap_msg_state *state = tevent_req_data(
489 req, struct tldap_msg_state);
490 struct tldap_context *ld;
491 struct tevent_req **pending;
492 int num_pending;
493 struct tevent_req *subreq;
495 ld = state->ld;
496 num_pending = talloc_array_length(ld->pending);
498 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
499 num_pending+1);
500 if (pending == NULL) {
501 return false;
503 pending[num_pending] = req;
504 ld->pending = pending;
505 talloc_set_destructor(req, tldap_msg_destructor);
507 if (num_pending > 0) {
508 return true;
512 * We're the first one, add the read_ldap request that waits for the
513 * answer from the server
515 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
516 if (subreq == NULL) {
517 tldap_msg_unset_pending(req);
518 return false;
520 tevent_req_set_callback(subreq, tldap_msg_received, ld);
521 return true;
524 static void tldap_msg_sent(struct tevent_req *subreq)
526 struct tevent_req *req = tevent_req_callback_data(
527 subreq, struct tevent_req);
528 struct tldap_msg_state *state = tevent_req_data(
529 req, struct tldap_msg_state);
530 ssize_t nwritten;
531 int err;
533 nwritten = tstream_writev_queue_recv(subreq, &err);
534 TALLOC_FREE(subreq);
535 if (nwritten == -1) {
536 state->ld->server_down = true;
537 tevent_req_error(req, TLDAP_SERVER_DOWN);
538 return;
541 if (!tldap_msg_set_pending(req)) {
542 tevent_req_nomem(NULL, req);
543 return;
547 static int tldap_msg_msgid(struct tevent_req *req)
549 struct tldap_msg_state *state = tevent_req_data(
550 req, struct tldap_msg_state);
552 return state->id;
555 static void tldap_msg_received(struct tevent_req *subreq)
557 struct tldap_context *ld = tevent_req_callback_data(
558 subreq, struct tldap_context);
559 struct tevent_req *req;
560 struct tldap_msg_state *state;
561 struct tevent_context *ev;
562 struct asn1_data *data;
563 uint8_t *inbuf;
564 ssize_t received;
565 size_t num_pending;
566 int i, err, status;
567 int id;
568 uint8_t type;
569 bool ok;
571 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
572 TALLOC_FREE(subreq);
573 if (received == -1) {
574 status = TLDAP_SERVER_DOWN;
575 goto fail;
578 data = asn1_init(talloc_tos());
579 if (data == NULL) {
580 status = TLDAP_NO_MEMORY;
581 goto fail;
583 asn1_load_nocopy(data, inbuf, received);
585 ok = true;
586 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
587 ok &= asn1_read_Integer(data, &id);
588 ok &= asn1_peek_uint8(data, &type);
590 if (!ok) {
591 status = TLDAP_PROTOCOL_ERROR;
592 goto fail;
595 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
596 "type %d\n", id, (int)type);
598 num_pending = talloc_array_length(ld->pending);
600 for (i=0; i<num_pending; i++) {
601 if (id == tldap_msg_msgid(ld->pending[i])) {
602 break;
605 if (i == num_pending) {
606 /* Dump unexpected reply */
607 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
608 "No request pending for msg %d\n", id);
609 TALLOC_FREE(data);
610 TALLOC_FREE(inbuf);
611 goto done;
614 req = ld->pending[i];
615 state = tevent_req_data(req, struct tldap_msg_state);
617 state->inbuf = talloc_move(state, &inbuf);
618 state->data = talloc_move(state, &data);
620 ev = state->ev;
622 talloc_set_destructor(req, NULL);
623 tldap_msg_unset_pending(req);
624 num_pending = talloc_array_length(ld->pending);
626 tevent_req_done(req);
628 done:
629 if (num_pending == 0) {
630 return;
632 if (talloc_array_length(ld->pending) > num_pending) {
634 * The callback functions called from tevent_req_done() above
635 * have put something on the pending queue. We don't have to
636 * trigger the read_ldap_send(), tldap_msg_set_pending() has
637 * done it for us already.
639 return;
642 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
643 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
644 if (subreq == NULL) {
645 status = TLDAP_NO_MEMORY;
646 goto fail;
648 tevent_req_set_callback(subreq, tldap_msg_received, ld);
649 return;
651 fail:
652 while (talloc_array_length(ld->pending) > 0) {
653 req = ld->pending[0];
654 talloc_set_destructor(req, NULL);
655 tldap_msg_destructor(req);
656 tevent_req_error(req, status);
660 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
661 struct tldap_message **pmsg)
663 struct tldap_msg_state *state = tevent_req_data(
664 req, struct tldap_msg_state);
665 struct tldap_message *msg;
666 int err;
667 uint8_t msgtype;
669 if (tevent_req_is_ldap_error(req, &err)) {
670 return err;
673 if (!asn1_peek_uint8(state->data, &msgtype)) {
674 return TLDAP_PROTOCOL_ERROR;
677 if (pmsg == NULL) {
678 return TLDAP_SUCCESS;
681 msg = talloc_zero(mem_ctx, struct tldap_message);
682 if (msg == NULL) {
683 return TLDAP_NO_MEMORY;
685 msg->id = state->id;
687 msg->inbuf = talloc_move(msg, &state->inbuf);
688 msg->data = talloc_move(msg, &state->data);
689 msg->type = msgtype;
691 *pmsg = msg;
692 return TLDAP_SUCCESS;
695 struct tldap_req_state {
696 int id;
697 struct asn1_data *out;
698 struct tldap_message *result;
701 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
702 struct tldap_context *ld,
703 struct tldap_req_state **pstate)
705 struct tevent_req *req;
706 struct tldap_req_state *state;
708 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
709 if (req == NULL) {
710 return NULL;
712 ZERO_STRUCTP(state);
713 state->out = asn1_init(state);
714 if (state->out == NULL) {
715 TALLOC_FREE(req);
716 return NULL;
718 state->result = NULL;
719 state->id = tldap_next_msgid(ld);
721 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
722 asn1_write_Integer(state->out, state->id);
724 *pstate = state;
725 return req;
728 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
730 struct tldap_req_state *state = tevent_req_data(
731 req, struct tldap_req_state);
733 TALLOC_FREE(ld->last_msg);
734 ld->last_msg = talloc_move(ld, &state->result);
737 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
739 char *result = talloc_array(mem_ctx, char, blob.length+1);
741 if (result == NULL) {
742 return NULL;
745 memcpy(result, blob.data, blob.length);
746 result[blob.length] = '\0';
747 return result;
750 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
751 struct asn1_data *data,
752 char **presult)
754 DATA_BLOB string;
755 char *result;
756 if (!asn1_read_OctetString(data, mem_ctx, &string))
757 return false;
759 result = blob2string_talloc(mem_ctx, string);
761 data_blob_free(&string);
763 if (result == NULL) {
764 return false;
766 *presult = result;
767 return true;
770 static bool tldap_decode_controls(struct tldap_req_state *state);
772 static bool tldap_decode_response(struct tldap_req_state *state)
774 struct asn1_data *data = state->result->data;
775 struct tldap_message *msg = state->result;
776 bool ok = true;
778 ok &= asn1_read_enumerated(data, &msg->lderr);
779 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
780 ok &= asn1_read_OctetString_talloc(msg, data,
781 &msg->res_diagnosticmessage);
782 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
783 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
784 ok &= asn1_read_OctetString_talloc(msg, data,
785 &msg->res_referral);
786 ok &= asn1_end_tag(data);
787 } else {
788 msg->res_referral = NULL;
791 return ok;
794 static void tldap_sasl_bind_done(struct tevent_req *subreq);
796 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
797 struct tevent_context *ev,
798 struct tldap_context *ld,
799 const char *dn,
800 const char *mechanism,
801 DATA_BLOB *creds,
802 struct tldap_control *sctrls,
803 int num_sctrls,
804 struct tldap_control *cctrls,
805 int num_cctrls)
807 struct tevent_req *req, *subreq;
808 struct tldap_req_state *state;
810 req = tldap_req_create(mem_ctx, ld, &state);
811 if (req == NULL) {
812 return NULL;
815 if (dn == NULL) {
816 dn = "";
819 asn1_push_tag(state->out, TLDAP_REQ_BIND);
820 asn1_write_Integer(state->out, ld->ld_version);
821 asn1_write_OctetString(state->out, dn, (dn != NULL) ? strlen(dn) : 0);
823 if (mechanism == NULL) {
824 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
825 asn1_write(state->out, creds->data, creds->length);
826 asn1_pop_tag(state->out);
827 } else {
828 asn1_push_tag(state->out, ASN1_CONTEXT(3));
829 asn1_write_OctetString(state->out, mechanism,
830 strlen(mechanism));
831 if ((creds != NULL) && (creds->data != NULL)) {
832 asn1_write_OctetString(state->out, creds->data,
833 creds->length);
835 asn1_pop_tag(state->out);
838 if (!asn1_pop_tag(state->out)) {
839 tevent_req_error(req, TLDAP_ENCODING_ERROR);
840 return tevent_req_post(req, ev);
843 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
844 sctrls, num_sctrls);
845 if (tevent_req_nomem(subreq, req)) {
846 return tevent_req_post(req, ev);
848 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
849 return req;
852 static void tldap_sasl_bind_done(struct tevent_req *subreq)
854 struct tevent_req *req = tevent_req_callback_data(
855 subreq, struct tevent_req);
856 struct tldap_req_state *state = tevent_req_data(
857 req, struct tldap_req_state);
858 int err;
860 err = tldap_msg_recv(subreq, state, &state->result);
861 TALLOC_FREE(subreq);
862 if (err != TLDAP_SUCCESS) {
863 tevent_req_error(req, err);
864 return;
866 if (state->result->type != TLDAP_RES_BIND) {
867 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
868 return;
870 if (!asn1_start_tag(state->result->data, state->result->type) ||
871 !tldap_decode_response(state) ||
872 !asn1_end_tag(state->result->data)) {
873 tevent_req_error(req, TLDAP_DECODING_ERROR);
874 return;
877 * TODO: pull the reply blob
879 if (state->result->lderr != TLDAP_SUCCESS) {
880 tevent_req_error(req, state->result->lderr);
881 return;
883 tevent_req_done(req);
886 int tldap_sasl_bind_recv(struct tevent_req *req)
888 return tldap_simple_recv(req);
891 int tldap_sasl_bind(struct tldap_context *ld,
892 const char *dn,
893 const char *mechanism,
894 DATA_BLOB *creds,
895 struct tldap_control *sctrls,
896 int num_sctrls,
897 struct tldap_control *cctrls,
898 int num_cctrls)
900 TALLOC_CTX *frame = talloc_stackframe();
901 struct tevent_context *ev;
902 struct tevent_req *req;
903 int result;
905 ev = event_context_init(frame);
906 if (ev == NULL) {
907 result = TLDAP_NO_MEMORY;
908 goto fail;
911 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
912 sctrls, num_sctrls, cctrls, num_cctrls);
913 if (req == NULL) {
914 result = TLDAP_NO_MEMORY;
915 goto fail;
918 if (!tevent_req_poll(req, ev)) {
919 result = TLDAP_OPERATIONS_ERROR;
920 goto fail;
923 result = tldap_sasl_bind_recv(req);
924 tldap_save_msg(ld, req);
925 fail:
926 TALLOC_FREE(frame);
927 return result;
930 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
931 struct tevent_context *ev,
932 struct tldap_context *ld,
933 const char *dn,
934 const char *passwd)
936 DATA_BLOB cred;
938 if (passwd != NULL) {
939 cred.data = (uint8_t *)passwd;
940 cred.length = strlen(passwd);
941 } else {
942 cred.data = (uint8_t *)"";
943 cred.length = 0;
945 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
946 NULL, 0);
949 int tldap_simple_bind_recv(struct tevent_req *req)
951 return tldap_sasl_bind_recv(req);
954 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
955 const char *passwd)
957 DATA_BLOB cred;
959 if (passwd != NULL) {
960 cred.data = (uint8_t *)passwd;
961 cred.length = strlen(passwd);
962 } else {
963 cred.data = (uint8_t *)"";
964 cred.length = 0;
966 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
969 /*****************************************************************************/
971 /* can't use isalpha() as only a strict set is valid for LDAP */
973 static bool tldap_is_alpha(char c)
975 return (((c >= 'a') && (c <= 'z')) || \
976 ((c >= 'A') && (c <= 'Z')));
979 static bool tldap_is_adh(char c)
981 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
984 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
985 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
986 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
987 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
988 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
989 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
990 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
991 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
992 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
993 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
995 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
996 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
997 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1000 /* oid's should be numerical only in theory,
1001 * but apparently some broken servers may have alphanum aliases instead.
1002 * Do like openldap libraries and allow alphanum aliases for oids, but
1003 * do not allow Tagging options in that case.
1005 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1007 bool is_oid = false;
1008 bool dot = false;
1009 int i;
1011 /* first char has stricter rules */
1012 if (isdigit(*s)) {
1013 is_oid = true;
1014 } else if (!tldap_is_alpha(*s)) {
1015 /* bad first char */
1016 return false;
1019 for (i = 1; i < len; i++) {
1021 if (is_oid) {
1022 if (isdigit(s[i])) {
1023 dot = false;
1024 continue;
1026 if (s[i] == '.') {
1027 if (dot) {
1028 /* malformed */
1029 return false;
1031 dot = true;
1032 continue;
1034 } else {
1035 if (tldap_is_adh(s[i])) {
1036 continue;
1040 if (s[i] == ';') {
1041 if (no_tagopts) {
1042 /* no tagging options */
1043 return false;
1045 if (dot) {
1046 /* malformed */
1047 return false;
1049 if ((i + 1) == len) {
1050 /* malformed */
1051 return false;
1054 is_oid = false;
1055 continue;
1059 if (dot) {
1060 /* malformed */
1061 return false;
1064 return true;
1067 /* this function copies the value until the closing parenthesis is found. */
1068 static char *tldap_get_val(TALLOC_CTX *memctx,
1069 const char *value, const char **_s)
1071 const char *s = value;
1073 /* find terminator */
1074 while (*s) {
1075 s = strchr(s, ')');
1076 if (s && (*(s - 1) == '\\')) {
1077 continue;
1079 break;
1081 if (!s || !(*s == ')')) {
1082 /* malformed filter */
1083 return NULL;
1086 *_s = s;
1088 return talloc_strndup(memctx, value, s - value);
1091 static int tldap_hex2char(const char *x)
1093 if (isxdigit(x[0]) && isxdigit(x[1])) {
1094 const char h1 = x[0], h2 = x[1];
1095 int c = 0;
1097 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1098 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1099 else if (h1 >= '0') c = h1 - (int)'0';
1100 c = c << 4;
1101 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1102 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1103 else if (h2 >= '0') c += h2 - (int)'0';
1105 return c;
1108 return -1;
1111 static bool tldap_find_first_star(const char *val, const char **star)
1113 const char *s;
1115 for (s = val; *s; s++) {
1116 switch (*s) {
1117 case '\\':
1118 if (isxdigit(s[1]) && isxdigit(s[2])) {
1119 s += 2;
1120 break;
1122 /* not hex based escape, check older syntax */
1123 switch (s[1]) {
1124 case '(':
1125 case ')':
1126 case '*':
1127 case '\\':
1128 s++;
1129 break;
1130 default:
1131 /* invalid escape sequence */
1132 return false;
1134 break;
1135 case ')':
1136 /* end of val, nothing found */
1137 *star = s;
1138 return true;
1140 case '*':
1141 *star = s;
1142 return true;
1146 /* string ended without closing parenthesis, filter is malformed */
1147 return false;
1150 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1152 int c, i, p;
1154 for (i = 0,p = 0; i < *val_len; i++) {
1156 switch (value[i]) {
1157 case '(':
1158 case ')':
1159 case '*':
1160 /* these must be escaped */
1161 return false;
1163 case '\\':
1164 if (!value[i + 1]) {
1165 /* invalid EOL */
1166 return false;
1168 i++;
1170 c = tldap_hex2char(&value[i]);
1171 if (c >= 0 && c < 256) {
1172 value[p] = c;
1173 i++;
1174 p++;
1175 break;
1178 switch (value[i]) {
1179 case '(':
1180 case ')':
1181 case '*':
1182 case '\\':
1183 value[p] = value[i];
1184 p++;
1185 default:
1186 /* invalid */
1187 return false;
1189 break;
1191 default:
1192 value[p] = value[i];
1193 p++;
1196 value[p] = '\0';
1197 *val_len = p;
1198 return true;
1201 static bool tldap_push_filter_basic(struct tldap_context *ld,
1202 struct asn1_data *data,
1203 const char **_s);
1204 static bool tldap_push_filter_substring(struct tldap_context *ld,
1205 struct asn1_data *data,
1206 const char *val,
1207 const char **_s);
1208 static bool tldap_push_filter_int(struct tldap_context *ld,
1209 struct asn1_data *data,
1210 const char **_s)
1212 const char *s = *_s;
1213 bool ret;
1215 if (*s != '(') {
1216 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1217 "Incomplete or malformed filter\n");
1218 return false;
1220 s++;
1222 /* we are right after a parenthesis,
1223 * find out what op we have at hand */
1224 switch (*s) {
1225 case '&':
1226 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1227 asn1_push_tag(data, TLDAP_FILTER_AND);
1228 s++;
1229 break;
1231 case '|':
1232 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1233 asn1_push_tag(data, TLDAP_FILTER_OR);
1234 s++;
1235 break;
1237 case '!':
1238 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1239 asn1_push_tag(data, TLDAP_FILTER_NOT);
1240 s++;
1241 ret = tldap_push_filter_int(ld, data, &s);
1242 if (!ret) {
1243 return false;
1245 asn1_pop_tag(data);
1246 goto done;
1248 case '(':
1249 case ')':
1250 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1251 "Invalid parenthesis '%c'\n", *s);
1252 return false;
1254 case '\0':
1255 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1256 "Invalid filter termination\n");
1257 return false;
1259 default:
1260 ret = tldap_push_filter_basic(ld, data, &s);
1261 if (!ret) {
1262 return false;
1264 goto done;
1267 /* only and/or filters get here.
1268 * go through the list of filters */
1270 if (*s == ')') {
1271 /* RFC 4526: empty and/or */
1272 asn1_pop_tag(data);
1273 goto done;
1276 while (*s) {
1277 ret = tldap_push_filter_int(ld, data, &s);
1278 if (!ret) {
1279 return false;
1282 if (*s == ')') {
1283 /* end of list, return */
1284 asn1_pop_tag(data);
1285 break;
1289 done:
1290 if (*s != ')') {
1291 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1292 "Incomplete or malformed filter\n");
1293 return false;
1295 s++;
1297 if (data->has_error) {
1298 return false;
1301 *_s = s;
1302 return true;
1306 static bool tldap_push_filter_basic(struct tldap_context *ld,
1307 struct asn1_data *data,
1308 const char **_s)
1310 TALLOC_CTX *tmpctx = talloc_tos();
1311 const char *s = *_s;
1312 const char *e;
1313 const char *eq;
1314 const char *val;
1315 const char *type;
1316 const char *dn;
1317 const char *rule;
1318 const char *star;
1319 size_t type_len = 0;
1320 char *uval;
1321 size_t uval_len;
1322 bool write_octect = true;
1323 bool ret;
1325 eq = strchr(s, '=');
1326 if (!eq) {
1327 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1328 "Invalid filter, missing equal sign\n");
1329 return false;
1332 val = eq + 1;
1333 e = eq - 1;
1335 switch (*e) {
1336 case '<':
1337 asn1_push_tag(data, TLDAP_FILTER_LE);
1338 break;
1340 case '>':
1341 asn1_push_tag(data, TLDAP_FILTER_GE);
1342 break;
1344 case '~':
1345 asn1_push_tag(data, TLDAP_FILTER_APX);
1346 break;
1348 case ':':
1349 asn1_push_tag(data, TLDAP_FILTER_EXT);
1350 write_octect = false;
1352 type = NULL;
1353 dn = NULL;
1354 rule = NULL;
1356 if (*s == ':') { /* [:dn]:rule:= value */
1357 if (s == e) {
1358 /* malformed filter */
1359 return false;
1361 dn = s;
1362 } else { /* type[:dn][:rule]:= value */
1363 type = s;
1364 dn = strchr(s, ':');
1365 type_len = dn - type;
1366 if (dn == e) { /* type:= value */
1367 dn = NULL;
1370 if (dn) {
1371 dn++;
1373 rule = strchr(dn, ':');
1374 if ((rule == dn + 1) || rule + 1 == e) {
1375 /* malformed filter, contains "::" */
1376 return false;
1379 if (StrnCaseCmp(dn, "dn:", 3) != 0) {
1380 if (rule == e) {
1381 rule = dn;
1382 dn = NULL;
1383 } else {
1384 /* malformed filter. With two
1385 * optionals, the first must be "dn"
1387 return false;
1389 } else {
1390 if (rule == e) {
1391 rule = NULL;
1392 } else {
1393 rule++;
1398 if (!type && !dn && !rule) {
1399 /* malformed filter, there must be at least one */
1400 return false;
1404 MatchingRuleAssertion ::= SEQUENCE {
1405 matchingRule [1] MatchingRuleID OPTIONAL,
1406 type [2] AttributeDescription OPTIONAL,
1407 matchValue [3] AssertionValue,
1408 dnAttributes [4] BOOLEAN DEFAULT FALSE
1412 /* check and add rule */
1413 if (rule) {
1414 ret = tldap_is_attrdesc(rule, e - rule, true);
1415 if (!ret) {
1416 return false;
1418 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1419 asn1_write(data, rule, e - rule);
1420 asn1_pop_tag(data);
1423 /* check and add type */
1424 if (type) {
1425 ret = tldap_is_attrdesc(type, type_len, false);
1426 if (!ret) {
1427 return false;
1429 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1430 asn1_write(data, type, type_len);
1431 asn1_pop_tag(data);
1434 uval = tldap_get_val(tmpctx, val, _s);
1435 if (!uval) {
1436 return false;
1438 uval_len = *_s - val;
1439 ret = tldap_unescape_inplace(uval, &uval_len);
1440 if (!ret) {
1441 return false;
1444 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1445 asn1_write(data, uval, uval_len);
1446 asn1_pop_tag(data);
1448 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1449 asn1_write_uint8(data, dn?1:0);
1450 asn1_pop_tag(data);
1451 break;
1453 default:
1454 e = eq;
1456 ret = tldap_is_attrdesc(s, e - s, false);
1457 if (!ret) {
1458 return false;
1461 if (strncmp(val, "*)", 2) == 0) {
1462 /* presence */
1463 asn1_push_tag(data, TLDAP_FILTER_PRES);
1464 asn1_write(data, s, e - s);
1465 *_s = val + 1;
1466 write_octect = false;
1467 break;
1470 ret = tldap_find_first_star(val, &star);
1471 if (!ret) {
1472 return false;
1474 if (*star == '*') {
1475 /* substring */
1476 asn1_push_tag(data, TLDAP_FILTER_SUB);
1477 asn1_write_OctetString(data, s, e - s);
1478 ret = tldap_push_filter_substring(ld, data, val, &s);
1479 if (!ret) {
1480 return false;
1482 *_s = s;
1483 write_octect = false;
1484 break;
1487 /* if nothing else, then it is just equality */
1488 asn1_push_tag(data, TLDAP_FILTER_EQ);
1489 write_octect = true;
1490 break;
1493 if (write_octect) {
1494 uval = tldap_get_val(tmpctx, val, _s);
1495 if (!uval) {
1496 return false;
1498 uval_len = *_s - val;
1499 ret = tldap_unescape_inplace(uval, &uval_len);
1500 if (!ret) {
1501 return false;
1504 asn1_write_OctetString(data, s, e - s);
1505 asn1_write_OctetString(data, uval, uval_len);
1508 if (data->has_error) {
1509 return false;
1511 asn1_pop_tag(data);
1512 return true;
1515 static bool tldap_push_filter_substring(struct tldap_context *ld,
1516 struct asn1_data *data,
1517 const char *val,
1518 const char **_s)
1520 TALLOC_CTX *tmpctx = talloc_tos();
1521 bool initial = true;
1522 const char *star;
1523 char *chunk;
1524 size_t chunk_len;
1525 bool ret;
1528 SubstringFilter ::= SEQUENCE {
1529 type AttributeDescription,
1530 -- at least one must be present
1531 substrings SEQUENCE OF CHOICE {
1532 initial [0] LDAPString,
1533 any [1] LDAPString,
1534 final [2] LDAPString } }
1536 asn1_push_tag(data, ASN1_SEQUENCE(0));
1538 do {
1539 ret = tldap_find_first_star(val, &star);
1540 if (!ret) {
1541 return false;
1543 chunk_len = star - val;
1545 switch (*star) {
1546 case '*':
1547 if (!initial && chunk_len == 0) {
1548 /* found '**', which is illegal */
1549 return false;
1551 break;
1552 case ')':
1553 if (initial) {
1554 /* no stars ?? */
1555 return false;
1557 /* we are done */
1558 break;
1559 default:
1560 /* ?? */
1561 return false;
1564 if (initial && chunk_len == 0) {
1565 val = star + 1;
1566 initial = false;
1567 continue;
1570 chunk = talloc_strndup(tmpctx, val, chunk_len);
1571 if (!chunk) {
1572 return false;
1574 ret = tldap_unescape_inplace(chunk, &chunk_len);
1575 if (!ret) {
1576 return false;
1578 switch (*star) {
1579 case '*':
1580 if (initial) {
1581 asn1_push_tag(data, TLDAP_SUB_INI);
1582 initial = false;
1583 } else {
1584 asn1_push_tag(data, TLDAP_SUB_ANY);
1586 break;
1587 case ')':
1588 asn1_push_tag(data, TLDAP_SUB_FIN);
1589 break;
1590 default:
1591 /* ?? */
1592 return false;
1594 asn1_write(data, chunk, chunk_len);
1595 asn1_pop_tag(data);
1597 val = star + 1;
1599 } while (*star == '*');
1601 *_s = star;
1603 /* end of sequence */
1604 asn1_pop_tag(data);
1605 return true;
1608 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1609 * around parenthesis, we do not allow any spaces (except in values of
1610 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1611 * leading or trailing spaces where allowed.
1613 static bool tldap_push_filter(struct tldap_context *ld,
1614 struct asn1_data *data,
1615 const char *filter)
1617 const char *s = filter;
1618 bool ret;
1620 ret = tldap_push_filter_int(ld, data, &s);
1621 if (ret && *s) {
1622 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1623 "Incomplete or malformed filter\n");
1624 return false;
1626 return ret;
1629 /*****************************************************************************/
1631 static void tldap_search_done(struct tevent_req *subreq);
1633 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1634 struct tevent_context *ev,
1635 struct tldap_context *ld,
1636 const char *base, int scope,
1637 const char *filter,
1638 const char **attrs,
1639 int num_attrs,
1640 int attrsonly,
1641 struct tldap_control *sctrls,
1642 int num_sctrls,
1643 struct tldap_control *cctrls,
1644 int num_cctrls,
1645 int timelimit,
1646 int sizelimit,
1647 int deref)
1649 struct tevent_req *req, *subreq;
1650 struct tldap_req_state *state;
1651 int i;
1653 req = tldap_req_create(mem_ctx, ld, &state);
1654 if (req == NULL) {
1655 return NULL;
1658 asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1659 asn1_write_OctetString(state->out, base, strlen(base));
1660 asn1_write_enumerated(state->out, scope);
1661 asn1_write_enumerated(state->out, deref);
1662 asn1_write_Integer(state->out, sizelimit);
1663 asn1_write_Integer(state->out, timelimit);
1664 asn1_write_BOOLEAN(state->out, attrsonly);
1666 if (!tldap_push_filter(ld, state->out, filter)) {
1667 goto encoding_error;
1670 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1671 for (i=0; i<num_attrs; i++) {
1672 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1674 asn1_pop_tag(state->out);
1675 asn1_pop_tag(state->out);
1677 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1678 sctrls, num_sctrls);
1679 if (tevent_req_nomem(subreq, req)) {
1680 return tevent_req_post(req, ev);
1682 tevent_req_set_callback(subreq, tldap_search_done, req);
1683 return req;
1685 encoding_error:
1686 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1687 return tevent_req_post(req, ev);
1690 static void tldap_search_done(struct tevent_req *subreq)
1692 struct tevent_req *req = tevent_req_callback_data(
1693 subreq, struct tevent_req);
1694 struct tldap_req_state *state = tevent_req_data(
1695 req, struct tldap_req_state);
1696 int err;
1698 err = tldap_msg_recv(subreq, state, &state->result);
1699 if (err != TLDAP_SUCCESS) {
1700 tevent_req_error(req, err);
1701 return;
1703 switch (state->result->type) {
1704 case TLDAP_RES_SEARCH_ENTRY:
1705 case TLDAP_RES_SEARCH_REFERENCE:
1706 if (!tldap_msg_set_pending(subreq)) {
1707 tevent_req_nomem(NULL, req);
1708 return;
1710 tevent_req_notify_callback(req);
1711 break;
1712 case TLDAP_RES_SEARCH_RESULT:
1713 TALLOC_FREE(subreq);
1714 if (!asn1_start_tag(state->result->data,
1715 state->result->type) ||
1716 !tldap_decode_response(state) ||
1717 !asn1_end_tag(state->result->data) ||
1718 !tldap_decode_controls(state)) {
1719 tevent_req_error(req, TLDAP_DECODING_ERROR);
1720 return;
1722 tevent_req_done(req);
1723 break;
1724 default:
1725 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1726 return;
1730 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1731 struct tldap_message **pmsg)
1733 struct tldap_req_state *state = tevent_req_data(
1734 req, struct tldap_req_state);
1735 int err;
1737 if (!tevent_req_is_in_progress(req)
1738 && tevent_req_is_ldap_error(req, &err)) {
1739 return err;
1742 if (tevent_req_is_in_progress(req)) {
1743 switch (state->result->type) {
1744 case TLDAP_RES_SEARCH_ENTRY:
1745 case TLDAP_RES_SEARCH_REFERENCE:
1746 break;
1747 default:
1748 return TLDAP_OPERATIONS_ERROR;
1752 *pmsg = talloc_move(mem_ctx, &state->result);
1753 return TLDAP_SUCCESS;
1756 struct tldap_sync_search_state {
1757 TALLOC_CTX *mem_ctx;
1758 struct tldap_message **entries;
1759 struct tldap_message **refs;
1760 int rc;
1763 static void tldap_search_cb(struct tevent_req *req)
1765 struct tldap_sync_search_state *state =
1766 (struct tldap_sync_search_state *)
1767 tevent_req_callback_data_void(req);
1768 struct tldap_message *msg, **tmp;
1769 int rc, num_entries, num_refs;
1771 rc = tldap_search_recv(req, talloc_tos(), &msg);
1772 if (rc != TLDAP_SUCCESS) {
1773 state->rc = rc;
1774 return;
1777 switch (tldap_msg_type(msg)) {
1778 case TLDAP_RES_SEARCH_ENTRY:
1779 num_entries = talloc_array_length(state->entries);
1780 tmp = talloc_realloc(state->mem_ctx, state->entries,
1781 struct tldap_message *, num_entries + 1);
1782 if (tmp == NULL) {
1783 state->rc = TLDAP_NO_MEMORY;
1784 return;
1786 state->entries = tmp;
1787 state->entries[num_entries] = talloc_move(state->entries,
1788 &msg);
1789 break;
1790 case TLDAP_RES_SEARCH_REFERENCE:
1791 num_refs = talloc_array_length(state->refs);
1792 tmp = talloc_realloc(state->mem_ctx, state->refs,
1793 struct tldap_message *, num_refs + 1);
1794 if (tmp == NULL) {
1795 state->rc = TLDAP_NO_MEMORY;
1796 return;
1798 state->refs = tmp;
1799 state->refs[num_refs] = talloc_move(state->refs, &msg);
1800 break;
1801 case TLDAP_RES_SEARCH_RESULT:
1802 state->rc = TLDAP_SUCCESS;
1803 break;
1804 default:
1805 state->rc = TLDAP_PROTOCOL_ERROR;
1806 break;
1810 int tldap_search(struct tldap_context *ld,
1811 const char *base, int scope, const char *filter,
1812 const char **attrs, int num_attrs, int attrsonly,
1813 struct tldap_control *sctrls, int num_sctrls,
1814 struct tldap_control *cctrls, int num_cctrls,
1815 int timelimit, int sizelimit, int deref,
1816 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1817 struct tldap_message ***refs)
1819 TALLOC_CTX *frame = talloc_stackframe();
1820 struct tevent_context *ev;
1821 struct tevent_req *req;
1822 struct tldap_sync_search_state state;
1824 ZERO_STRUCT(state);
1825 state.mem_ctx = mem_ctx;
1826 state.rc = TLDAP_SUCCESS;
1828 ev = event_context_init(frame);
1829 if (ev == NULL) {
1830 state.rc = TLDAP_NO_MEMORY;
1831 goto fail;
1834 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1835 attrs, num_attrs, attrsonly,
1836 sctrls, num_sctrls, cctrls, num_cctrls,
1837 timelimit, sizelimit, deref);
1838 if (req == NULL) {
1839 state.rc = TLDAP_NO_MEMORY;
1840 goto fail;
1843 tevent_req_set_callback(req, tldap_search_cb, &state);
1845 if (!tevent_req_is_in_progress(req)) {
1846 /* an error happend before sending */
1847 if (tevent_req_is_ldap_error(req, &state.rc)) {
1848 goto fail;
1852 while (tevent_req_is_in_progress(req)
1853 && (state.rc == TLDAP_SUCCESS)) {
1854 if (tevent_loop_once(ev) == -1) {
1855 return TLDAP_OPERATIONS_ERROR;
1859 if (state.rc != TLDAP_SUCCESS) {
1860 return state.rc;
1863 if (entries != NULL) {
1864 *entries = state.entries;
1865 } else {
1866 TALLOC_FREE(state.entries);
1868 if (refs != NULL) {
1869 *refs = state.refs;
1870 } else {
1871 TALLOC_FREE(state.refs);
1873 tldap_save_msg(ld, req);
1874 fail:
1875 TALLOC_FREE(frame);
1876 return state.rc;
1879 static bool tldap_parse_search_entry(struct tldap_message *msg)
1881 int num_attribs = 0;
1883 asn1_start_tag(msg->data, msg->type);
1885 /* dn */
1887 asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1888 if (msg->dn == NULL) {
1889 return false;
1893 * Attributes: We overallocate msg->attribs by one, so that while
1894 * looping over the attributes we can directly parse into the last
1895 * array element. Same for the values in the inner loop.
1898 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1899 if (msg->attribs == NULL) {
1900 return false;
1903 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1904 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1905 struct tldap_attribute *attrib;
1906 int num_values = 0;
1908 attrib = &msg->attribs[num_attribs];
1909 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1910 if (attrib->values == NULL) {
1911 return false;
1913 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1914 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1915 &attrib->name);
1916 asn1_start_tag(msg->data, ASN1_SET);
1918 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1919 asn1_read_OctetString(msg->data, msg,
1920 &attrib->values[num_values]);
1922 attrib->values = talloc_realloc(
1923 msg->attribs, attrib->values, DATA_BLOB,
1924 num_values + 2);
1925 if (attrib->values == NULL) {
1926 return false;
1928 num_values += 1;
1930 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1931 DATA_BLOB, num_values);
1932 attrib->num_values = num_values;
1934 asn1_end_tag(msg->data); /* ASN1_SET */
1935 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1936 msg->attribs = talloc_realloc(
1937 msg, msg->attribs, struct tldap_attribute,
1938 num_attribs + 2);
1939 if (msg->attribs == NULL) {
1940 return false;
1942 num_attribs += 1;
1944 msg->attribs = talloc_realloc(
1945 msg, msg->attribs, struct tldap_attribute, num_attribs);
1946 asn1_end_tag(msg->data);
1947 if (msg->data->has_error) {
1948 return false;
1950 return true;
1953 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1955 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1956 return false;
1958 *dn = msg->dn;
1959 return true;
1962 bool tldap_entry_attributes(struct tldap_message *msg,
1963 struct tldap_attribute **attributes,
1964 int *num_attributes)
1966 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1967 return false;
1969 *attributes = msg->attribs;
1970 *num_attributes = talloc_array_length(msg->attribs);
1971 return true;
1974 static bool tldap_decode_controls(struct tldap_req_state *state)
1976 struct tldap_message *msg = state->result;
1977 struct asn1_data *data = msg->data;
1978 struct tldap_control *sctrls = NULL;
1979 int num_controls = 0;
1981 msg->res_sctrls = NULL;
1983 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1984 return true;
1987 asn1_start_tag(data, ASN1_CONTEXT(0));
1989 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
1990 struct tldap_control *c;
1991 char *oid = NULL;
1993 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
1994 num_controls + 1);
1995 if (sctrls == NULL) {
1996 return false;
1998 c = &sctrls[num_controls];
2000 asn1_start_tag(data, ASN1_SEQUENCE(0));
2001 asn1_read_OctetString_talloc(msg, data, &oid);
2002 if ((data->has_error) || (oid == NULL)) {
2003 return false;
2005 c->oid = oid;
2006 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2007 asn1_read_BOOLEAN(data, &c->critical);
2008 } else {
2009 c->critical = false;
2011 c->value = data_blob_null;
2012 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2013 !asn1_read_OctetString(data, msg, &c->value)) {
2014 return false;
2016 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
2018 num_controls += 1;
2021 asn1_end_tag(data); /* ASN1_CONTEXT(0) */
2023 if (data->has_error) {
2024 TALLOC_FREE(sctrls);
2025 return false;
2027 msg->res_sctrls = sctrls;
2028 return true;
2031 static void tldap_simple_done(struct tevent_req *subreq, int type)
2033 struct tevent_req *req = tevent_req_callback_data(
2034 subreq, struct tevent_req);
2035 struct tldap_req_state *state = tevent_req_data(
2036 req, struct tldap_req_state);
2037 int err;
2039 err = tldap_msg_recv(subreq, state, &state->result);
2040 TALLOC_FREE(subreq);
2041 if (err != TLDAP_SUCCESS) {
2042 tevent_req_error(req, err);
2043 return;
2045 if (state->result->type != type) {
2046 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2047 return;
2049 if (!asn1_start_tag(state->result->data, state->result->type) ||
2050 !tldap_decode_response(state) ||
2051 !asn1_end_tag(state->result->data) ||
2052 !tldap_decode_controls(state)) {
2053 tevent_req_error(req, TLDAP_DECODING_ERROR);
2054 return;
2056 if (state->result->lderr != TLDAP_SUCCESS) {
2057 tevent_req_error(req, state->result->lderr);
2058 return;
2060 tevent_req_done(req);
2063 static int tldap_simple_recv(struct tevent_req *req)
2065 int err;
2066 if (tevent_req_is_ldap_error(req, &err)) {
2067 return err;
2069 return TLDAP_SUCCESS;
2072 static void tldap_add_done(struct tevent_req *subreq);
2074 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2075 struct tevent_context *ev,
2076 struct tldap_context *ld,
2077 const char *dn,
2078 struct tldap_mod *attributes,
2079 int num_attributes,
2080 struct tldap_control *sctrls,
2081 int num_sctrls,
2082 struct tldap_control *cctrls,
2083 int num_cctrls)
2085 struct tevent_req *req, *subreq;
2086 struct tldap_req_state *state;
2087 int i, j;
2089 req = tldap_req_create(mem_ctx, ld, &state);
2090 if (req == NULL) {
2091 return NULL;
2094 asn1_push_tag(state->out, TLDAP_REQ_ADD);
2095 asn1_write_OctetString(state->out, dn, strlen(dn));
2096 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2098 for (i=0; i<num_attributes; i++) {
2099 struct tldap_mod *attrib = &attributes[i];
2100 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2101 asn1_write_OctetString(state->out, attrib->attribute,
2102 strlen(attrib->attribute));
2103 asn1_push_tag(state->out, ASN1_SET);
2104 for (j=0; j<attrib->num_values; j++) {
2105 asn1_write_OctetString(state->out,
2106 attrib->values[j].data,
2107 attrib->values[j].length);
2109 asn1_pop_tag(state->out);
2110 asn1_pop_tag(state->out);
2113 asn1_pop_tag(state->out);
2114 asn1_pop_tag(state->out);
2116 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2117 sctrls, num_sctrls);
2118 if (tevent_req_nomem(subreq, req)) {
2119 return tevent_req_post(req, ev);
2121 tevent_req_set_callback(subreq, tldap_add_done, req);
2122 return req;
2125 static void tldap_add_done(struct tevent_req *subreq)
2127 tldap_simple_done(subreq, TLDAP_RES_ADD);
2130 int tldap_add_recv(struct tevent_req *req)
2132 return tldap_simple_recv(req);
2135 int tldap_add(struct tldap_context *ld, const char *dn,
2136 struct tldap_mod *attributes, int num_attributes,
2137 struct tldap_control *sctrls, int num_sctrls,
2138 struct tldap_control *cctrls, int num_cctrls)
2140 TALLOC_CTX *frame = talloc_stackframe();
2141 struct tevent_context *ev;
2142 struct tevent_req *req;
2143 int result;
2145 ev = event_context_init(frame);
2146 if (ev == NULL) {
2147 result = TLDAP_NO_MEMORY;
2148 goto fail;
2151 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2152 sctrls, num_sctrls, cctrls, num_cctrls);
2153 if (req == NULL) {
2154 result = TLDAP_NO_MEMORY;
2155 goto fail;
2158 if (!tevent_req_poll(req, ev)) {
2159 result = TLDAP_OPERATIONS_ERROR;
2160 goto fail;
2163 result = tldap_add_recv(req);
2164 tldap_save_msg(ld, req);
2165 fail:
2166 TALLOC_FREE(frame);
2167 return result;
2170 static void tldap_modify_done(struct tevent_req *subreq);
2172 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2173 struct tevent_context *ev,
2174 struct tldap_context *ld,
2175 const char *dn,
2176 struct tldap_mod *mods, int num_mods,
2177 struct tldap_control *sctrls,
2178 int num_sctrls,
2179 struct tldap_control *cctrls,
2180 int num_cctrls)
2182 struct tevent_req *req, *subreq;
2183 struct tldap_req_state *state;
2184 int i, j;
2186 req = tldap_req_create(mem_ctx, ld, &state);
2187 if (req == NULL) {
2188 return NULL;
2191 asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
2192 asn1_write_OctetString(state->out, dn, strlen(dn));
2193 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2195 for (i=0; i<num_mods; i++) {
2196 struct tldap_mod *mod = &mods[i];
2197 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2198 asn1_write_enumerated(state->out, mod->mod_op),
2199 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2200 asn1_write_OctetString(state->out, mod->attribute,
2201 strlen(mod->attribute));
2202 asn1_push_tag(state->out, ASN1_SET);
2203 for (j=0; j<mod->num_values; j++) {
2204 asn1_write_OctetString(state->out,
2205 mod->values[j].data,
2206 mod->values[j].length);
2208 asn1_pop_tag(state->out);
2209 asn1_pop_tag(state->out);
2210 asn1_pop_tag(state->out);
2213 asn1_pop_tag(state->out);
2214 asn1_pop_tag(state->out);
2216 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2217 sctrls, num_sctrls);
2218 if (tevent_req_nomem(subreq, req)) {
2219 return tevent_req_post(req, ev);
2221 tevent_req_set_callback(subreq, tldap_modify_done, req);
2222 return req;
2225 static void tldap_modify_done(struct tevent_req *subreq)
2227 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2230 int tldap_modify_recv(struct tevent_req *req)
2232 return tldap_simple_recv(req);
2235 int tldap_modify(struct tldap_context *ld, const char *dn,
2236 struct tldap_mod *mods, int num_mods,
2237 struct tldap_control *sctrls, int num_sctrls,
2238 struct tldap_control *cctrls, int num_cctrls)
2240 TALLOC_CTX *frame = talloc_stackframe();
2241 struct tevent_context *ev;
2242 struct tevent_req *req;
2243 int result;
2245 ev = event_context_init(frame);
2246 if (ev == NULL) {
2247 result = TLDAP_NO_MEMORY;
2248 goto fail;
2251 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2252 sctrls, num_sctrls, cctrls, num_cctrls);
2253 if (req == NULL) {
2254 result = TLDAP_NO_MEMORY;
2255 goto fail;
2258 if (!tevent_req_poll(req, ev)) {
2259 result = TLDAP_OPERATIONS_ERROR;
2260 goto fail;
2263 result = tldap_modify_recv(req);
2264 tldap_save_msg(ld, req);
2265 fail:
2266 TALLOC_FREE(frame);
2267 return result;
2270 static void tldap_delete_done(struct tevent_req *subreq);
2272 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2273 struct tevent_context *ev,
2274 struct tldap_context *ld,
2275 const char *dn,
2276 struct tldap_control *sctrls,
2277 int num_sctrls,
2278 struct tldap_control *cctrls,
2279 int num_cctrls)
2281 struct tevent_req *req, *subreq;
2282 struct tldap_req_state *state;
2284 req = tldap_req_create(mem_ctx, ld, &state);
2285 if (req == NULL) {
2286 return NULL;
2289 asn1_push_tag(state->out, TLDAP_REQ_DELETE);
2290 asn1_write(state->out, dn, strlen(dn));
2291 asn1_pop_tag(state->out);
2293 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2294 sctrls, num_sctrls);
2295 if (tevent_req_nomem(subreq, req)) {
2296 return tevent_req_post(req, ev);
2298 tevent_req_set_callback(subreq, tldap_delete_done, req);
2299 return req;
2302 static void tldap_delete_done(struct tevent_req *subreq)
2304 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2307 int tldap_delete_recv(struct tevent_req *req)
2309 return tldap_simple_recv(req);
2312 int tldap_delete(struct tldap_context *ld, const char *dn,
2313 struct tldap_control *sctrls, int num_sctrls,
2314 struct tldap_control *cctrls, int num_cctrls)
2316 TALLOC_CTX *frame = talloc_stackframe();
2317 struct tevent_context *ev;
2318 struct tevent_req *req;
2319 int result;
2321 ev = event_context_init(frame);
2322 if (ev == NULL) {
2323 result = TLDAP_NO_MEMORY;
2324 goto fail;
2327 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2328 cctrls, num_cctrls);
2329 if (req == NULL) {
2330 result = TLDAP_NO_MEMORY;
2331 goto fail;
2334 if (!tevent_req_poll(req, ev)) {
2335 result = TLDAP_OPERATIONS_ERROR;
2336 goto fail;
2339 result = tldap_delete_recv(req);
2340 tldap_save_msg(ld, req);
2341 fail:
2342 TALLOC_FREE(frame);
2343 return result;
2346 int tldap_msg_id(const struct tldap_message *msg)
2348 return msg->id;
2351 int tldap_msg_type(const struct tldap_message *msg)
2353 return msg->type;
2356 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2358 if (msg == NULL) {
2359 return NULL;
2361 return msg->res_matcheddn;
2364 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2366 if (msg == NULL) {
2367 return NULL;
2369 return msg->res_diagnosticmessage;
2372 const char *tldap_msg_referral(struct tldap_message *msg)
2374 if (msg == NULL) {
2375 return NULL;
2377 return msg->res_referral;
2380 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2381 struct tldap_control **sctrls)
2383 if (msg == NULL) {
2384 *sctrls = NULL;
2385 *num_sctrls = 0;
2386 return;
2388 *sctrls = msg->res_sctrls;
2389 *num_sctrls = talloc_array_length(msg->res_sctrls);
2392 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2394 return ld->last_msg;
2397 const char *tldap_err2string(int rc)
2399 const char *res = NULL;
2402 * This would normally be a table, but the error codes are not fully
2403 * sequential. Let the compiler figure out the optimum implementation
2404 * :-)
2407 switch (rc) {
2408 case TLDAP_SUCCESS:
2409 res = "TLDAP_SUCCESS";
2410 break;
2411 case TLDAP_OPERATIONS_ERROR:
2412 res = "TLDAP_OPERATIONS_ERROR";
2413 break;
2414 case TLDAP_PROTOCOL_ERROR:
2415 res = "TLDAP_PROTOCOL_ERROR";
2416 break;
2417 case TLDAP_TIMELIMIT_EXCEEDED:
2418 res = "TLDAP_TIMELIMIT_EXCEEDED";
2419 break;
2420 case TLDAP_SIZELIMIT_EXCEEDED:
2421 res = "TLDAP_SIZELIMIT_EXCEEDED";
2422 break;
2423 case TLDAP_COMPARE_FALSE:
2424 res = "TLDAP_COMPARE_FALSE";
2425 break;
2426 case TLDAP_COMPARE_TRUE:
2427 res = "TLDAP_COMPARE_TRUE";
2428 break;
2429 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2430 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2431 break;
2432 case TLDAP_STRONG_AUTH_REQUIRED:
2433 res = "TLDAP_STRONG_AUTH_REQUIRED";
2434 break;
2435 case TLDAP_REFERRAL:
2436 res = "TLDAP_REFERRAL";
2437 break;
2438 case TLDAP_ADMINLIMIT_EXCEEDED:
2439 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2440 break;
2441 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2442 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2443 break;
2444 case TLDAP_CONFIDENTIALITY_REQUIRED:
2445 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2446 break;
2447 case TLDAP_SASL_BIND_IN_PROGRESS:
2448 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2449 break;
2450 case TLDAP_NO_SUCH_ATTRIBUTE:
2451 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2452 break;
2453 case TLDAP_UNDEFINED_TYPE:
2454 res = "TLDAP_UNDEFINED_TYPE";
2455 break;
2456 case TLDAP_INAPPROPRIATE_MATCHING:
2457 res = "TLDAP_INAPPROPRIATE_MATCHING";
2458 break;
2459 case TLDAP_CONSTRAINT_VIOLATION:
2460 res = "TLDAP_CONSTRAINT_VIOLATION";
2461 break;
2462 case TLDAP_TYPE_OR_VALUE_EXISTS:
2463 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2464 break;
2465 case TLDAP_INVALID_SYNTAX:
2466 res = "TLDAP_INVALID_SYNTAX";
2467 break;
2468 case TLDAP_NO_SUCH_OBJECT:
2469 res = "TLDAP_NO_SUCH_OBJECT";
2470 break;
2471 case TLDAP_ALIAS_PROBLEM:
2472 res = "TLDAP_ALIAS_PROBLEM";
2473 break;
2474 case TLDAP_INVALID_DN_SYNTAX:
2475 res = "TLDAP_INVALID_DN_SYNTAX";
2476 break;
2477 case TLDAP_IS_LEAF:
2478 res = "TLDAP_IS_LEAF";
2479 break;
2480 case TLDAP_ALIAS_DEREF_PROBLEM:
2481 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2482 break;
2483 case TLDAP_INAPPROPRIATE_AUTH:
2484 res = "TLDAP_INAPPROPRIATE_AUTH";
2485 break;
2486 case TLDAP_INVALID_CREDENTIALS:
2487 res = "TLDAP_INVALID_CREDENTIALS";
2488 break;
2489 case TLDAP_INSUFFICIENT_ACCESS:
2490 res = "TLDAP_INSUFFICIENT_ACCESS";
2491 break;
2492 case TLDAP_BUSY:
2493 res = "TLDAP_BUSY";
2494 break;
2495 case TLDAP_UNAVAILABLE:
2496 res = "TLDAP_UNAVAILABLE";
2497 break;
2498 case TLDAP_UNWILLING_TO_PERFORM:
2499 res = "TLDAP_UNWILLING_TO_PERFORM";
2500 break;
2501 case TLDAP_LOOP_DETECT:
2502 res = "TLDAP_LOOP_DETECT";
2503 break;
2504 case TLDAP_NAMING_VIOLATION:
2505 res = "TLDAP_NAMING_VIOLATION";
2506 break;
2507 case TLDAP_OBJECT_CLASS_VIOLATION:
2508 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2509 break;
2510 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2511 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2512 break;
2513 case TLDAP_NOT_ALLOWED_ON_RDN:
2514 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2515 break;
2516 case TLDAP_ALREADY_EXISTS:
2517 res = "TLDAP_ALREADY_EXISTS";
2518 break;
2519 case TLDAP_NO_OBJECT_CLASS_MODS:
2520 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2521 break;
2522 case TLDAP_RESULTS_TOO_LARGE:
2523 res = "TLDAP_RESULTS_TOO_LARGE";
2524 break;
2525 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2526 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2527 break;
2528 case TLDAP_OTHER:
2529 res = "TLDAP_OTHER";
2530 break;
2531 case TLDAP_SERVER_DOWN:
2532 res = "TLDAP_SERVER_DOWN";
2533 break;
2534 case TLDAP_LOCAL_ERROR:
2535 res = "TLDAP_LOCAL_ERROR";
2536 break;
2537 case TLDAP_ENCODING_ERROR:
2538 res = "TLDAP_ENCODING_ERROR";
2539 break;
2540 case TLDAP_DECODING_ERROR:
2541 res = "TLDAP_DECODING_ERROR";
2542 break;
2543 case TLDAP_TIMEOUT:
2544 res = "TLDAP_TIMEOUT";
2545 break;
2546 case TLDAP_AUTH_UNKNOWN:
2547 res = "TLDAP_AUTH_UNKNOWN";
2548 break;
2549 case TLDAP_FILTER_ERROR:
2550 res = "TLDAP_FILTER_ERROR";
2551 break;
2552 case TLDAP_USER_CANCELLED:
2553 res = "TLDAP_USER_CANCELLED";
2554 break;
2555 case TLDAP_PARAM_ERROR:
2556 res = "TLDAP_PARAM_ERROR";
2557 break;
2558 case TLDAP_NO_MEMORY:
2559 res = "TLDAP_NO_MEMORY";
2560 break;
2561 case TLDAP_CONNECT_ERROR:
2562 res = "TLDAP_CONNECT_ERROR";
2563 break;
2564 case TLDAP_NOT_SUPPORTED:
2565 res = "TLDAP_NOT_SUPPORTED";
2566 break;
2567 case TLDAP_CONTROL_NOT_FOUND:
2568 res = "TLDAP_CONTROL_NOT_FOUND";
2569 break;
2570 case TLDAP_NO_RESULTS_RETURNED:
2571 res = "TLDAP_NO_RESULTS_RETURNED";
2572 break;
2573 case TLDAP_MORE_RESULTS_TO_RETURN:
2574 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2575 break;
2576 case TLDAP_CLIENT_LOOP:
2577 res = "TLDAP_CLIENT_LOOP";
2578 break;
2579 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2580 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2581 break;
2582 default:
2583 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2584 rc);
2585 break;
2587 if (res == NULL) {
2588 res = "Unknown LDAP Error";
2590 return res;