2 Unix SMB/CIFS implementation.
3 Infrastructure for async ldap client requests
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "system/locale.h"
24 #include "lib/util/talloc_stack.h"
25 #include "lib/util/samba_util.h"
26 #include "lib/util_tsock.h"
27 #include "../lib/util/asn1.h"
28 #include "../lib/tsocket/tsocket.h"
29 #include "../lib/util/tevent_unix.h"
31 static TLDAPRC tldap_simple_recv(struct tevent_req *req);
33 #define TEVENT_TLDAP_RC_MAGIC (0x87bcd26e)
35 bool tevent_req_ldap_error(struct tevent_req *req, TLDAPRC rc)
39 if (TLDAP_RC_IS_SUCCESS(rc)) {
43 err = TEVENT_TLDAP_RC_MAGIC;
45 err |= TLDAP_RC_V(rc);
47 return tevent_req_error(req, err);
50 bool tevent_req_is_ldap_error(struct tevent_req *req, TLDAPRC *perr)
52 enum tevent_req_state state;
55 if (!tevent_req_is_error(req, &state, &err)) {
59 case TEVENT_REQ_TIMED_OUT:
60 *perr = TLDAP_TIMEOUT;
62 case TEVENT_REQ_NO_MEMORY:
63 *perr = TLDAP_NO_MEMORY;
65 case TEVENT_REQ_USER_ERROR:
66 if ((err >> 32) != TEVENT_TLDAP_RC_MAGIC) {
69 *perr = TLDAP_RC(err & 0xffffffff);
72 *perr = TLDAP_OPERATIONS_ERROR;
78 struct tldap_ctx_attribute {
83 struct tldap_context {
85 struct tstream_context *conn;
88 struct tevent_queue *outgoing;
89 struct tevent_req **pending;
91 /* For the sync wrappers we need something like get_last_error... */
92 struct tldap_message *last_msg;
95 void (*log_fn)(void *context, enum tldap_debug_level level,
96 const char *fmt, va_list ap);
99 struct tldap_ctx_attribute *ctx_attrs;
102 struct tldap_message {
103 struct asn1_data *data;
110 struct tldap_attribute *attribs;
112 /* Error data sent by the server */
115 char *res_diagnosticmessage;
117 DATA_BLOB res_serverSaslCreds;
118 struct tldap_control *res_sctrls;
120 /* Controls sent by the server */
121 struct tldap_control *ctrls;
124 void tldap_set_debug(struct tldap_context *ld,
125 void (*log_fn)(void *log_private,
126 enum tldap_debug_level level,
128 va_list ap) PRINTF_ATTRIBUTE(3,0),
132 ld->log_private = log_private;
135 static void tldap_debug(struct tldap_context *ld,
136 enum tldap_debug_level level,
137 const char *fmt, ...)
143 if (ld->log_fn == NULL) {
147 ld->log_fn(ld->log_private, level, fmt, ap);
151 static int tldap_next_msgid(struct tldap_context *ld)
155 result = ld->msgid++;
156 if (ld->msgid == 2147483647) {
162 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
164 struct tldap_context *ctx;
167 ctx = talloc_zero(mem_ctx, struct tldap_context);
171 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
178 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
179 if (ctx->outgoing == NULL) {
186 bool tldap_connection_ok(struct tldap_context *ld)
191 return !ld->server_down;
194 static struct tldap_ctx_attribute *tldap_context_findattr(
195 struct tldap_context *ld, const char *name)
199 num_attrs = talloc_array_length(ld->ctx_attrs);
201 for (i=0; i<num_attrs; i++) {
202 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
203 return &ld->ctx_attrs[i];
209 bool tldap_context_setattr(struct tldap_context *ld,
210 const char *name, const void *_pptr)
212 struct tldap_ctx_attribute *tmp, *attr;
215 void **pptr = (void **)discard_const_p(void,_pptr);
217 attr = tldap_context_findattr(ld, name);
220 * We don't actually delete attrs, we don't expect tons of
221 * attributes being shuffled around.
223 TALLOC_FREE(attr->ptr);
225 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
231 tmpname = talloc_strdup(ld, name);
232 if (tmpname == NULL) {
236 num_attrs = talloc_array_length(ld->ctx_attrs);
238 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
241 TALLOC_FREE(tmpname);
244 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
246 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
248 tmp[num_attrs].ptr = NULL;
255 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
257 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
265 struct read_ldap_state {
270 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
271 static void read_ldap_done(struct tevent_req *subreq);
273 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
274 struct tevent_context *ev,
275 struct tstream_context *conn)
277 struct tevent_req *req, *subreq;
278 struct read_ldap_state *state;
280 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
286 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
288 if (tevent_req_nomem(subreq, req)) {
289 return tevent_req_post(req, ev);
291 tevent_req_set_callback(subreq, read_ldap_done, req);
295 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
297 struct read_ldap_state *state = talloc_get_type_abort(
298 private_data, struct read_ldap_state);
303 /* We've been here, we're done */
308 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
310 if (buf[0] != 0x30) {
315 if ((len & 0x80) == 0) {
320 lensize = (len & 0x7f);
324 /* Please get us the full length */
327 if (buflen > 2 + lensize) {
331 if (buflen != 2 + lensize) {
335 for (i=0; i<lensize; i++) {
336 len = (len << 8) | buf[2+i];
341 static void read_ldap_done(struct tevent_req *subreq)
343 struct tevent_req *req = tevent_req_callback_data(
344 subreq, struct tevent_req);
345 struct read_ldap_state *state = tevent_req_data(
346 req, struct read_ldap_state);
350 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
353 tevent_req_error(req, err);
356 tevent_req_done(req);
359 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
360 uint8_t **pbuf, int *perrno)
362 struct read_ldap_state *state = tevent_req_data(
363 req, struct read_ldap_state);
365 if (tevent_req_is_unix_error(req, perrno)) {
368 *pbuf = talloc_move(mem_ctx, &state->buf);
369 return talloc_get_size(*pbuf);
372 struct tldap_msg_state {
373 struct tldap_context *ld;
374 struct tevent_context *ev;
378 struct asn1_data *data;
382 static bool tldap_push_controls(struct asn1_data *data,
383 struct tldap_control *sctrls,
388 if ((sctrls == NULL) || (num_sctrls == 0)) {
392 if (!asn1_push_tag(data, ASN1_CONTEXT(0))) return false;
394 for (i=0; i<num_sctrls; i++) {
395 struct tldap_control *c = &sctrls[i];
396 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
397 if (!asn1_write_OctetString(data, c->oid, strlen(c->oid))) return false;
399 if (!asn1_write_BOOLEAN(data, true)) return false;
401 if (c->value.data != NULL) {
402 if (!asn1_write_OctetString(data, c->value.data,
403 c->value.length)) return false;
405 if (!asn1_pop_tag(data)) return false; /* ASN1_SEQUENCE(0) */
408 return asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
411 static void tldap_msg_sent(struct tevent_req *subreq);
412 static void tldap_msg_received(struct tevent_req *subreq);
414 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
415 struct tevent_context *ev,
416 struct tldap_context *ld,
417 int id, struct asn1_data *data,
418 struct tldap_control *sctrls,
421 struct tevent_req *req, *subreq;
422 struct tldap_msg_state *state;
425 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
428 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
436 if (state->ld->server_down) {
437 tevent_req_ldap_error(req, TLDAP_SERVER_DOWN);
438 return tevent_req_post(req, ev);
441 if (!tldap_push_controls(data, sctrls, num_sctrls)) {
442 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
443 return tevent_req_post(req, ev);
447 if (!asn1_pop_tag(data)) {
448 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
449 return tevent_req_post(req, ev);
452 if (!asn1_blob(data, &blob)) {
453 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
454 return tevent_req_post(req, ev);
457 state->iov.iov_base = (void *)blob.data;
458 state->iov.iov_len = blob.length;
460 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
462 if (tevent_req_nomem(subreq, req)) {
463 return tevent_req_post(req, ev);
465 tevent_req_set_callback(subreq, tldap_msg_sent, req);
469 static void tldap_msg_unset_pending(struct tevent_req *req)
471 struct tldap_msg_state *state = tevent_req_data(
472 req, struct tldap_msg_state);
473 struct tldap_context *ld = state->ld;
474 int num_pending = talloc_array_length(ld->pending);
477 tevent_req_set_cleanup_fn(req, NULL);
479 if (num_pending == 1) {
480 TALLOC_FREE(ld->pending);
484 for (i=0; i<num_pending; i++) {
485 if (req == ld->pending[i]) {
489 if (i == num_pending) {
491 * Something's seriously broken. Just returning here is the
492 * right thing nevertheless, the point of this routine is to
493 * remove ourselves from cli->pending.
499 * Remove ourselves from the cli->pending array
501 if (num_pending > 1) {
502 ld->pending[i] = ld->pending[num_pending-1];
506 * No NULL check here, we're shrinking by sizeof(void *), and
507 * talloc_realloc just adjusts the size for this.
509 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
513 static void tldap_msg_cleanup(struct tevent_req *req,
514 enum tevent_req_state req_state)
517 case TEVENT_REQ_USER_ERROR:
518 case TEVENT_REQ_RECEIVED:
519 tldap_msg_unset_pending(req);
526 static bool tldap_msg_set_pending(struct tevent_req *req)
528 struct tldap_msg_state *state = tevent_req_data(
529 req, struct tldap_msg_state);
530 struct tldap_context *ld;
531 struct tevent_req **pending;
533 struct tevent_req *subreq;
536 num_pending = talloc_array_length(ld->pending);
538 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
540 if (pending == NULL) {
543 pending[num_pending] = req;
544 ld->pending = pending;
545 tevent_req_set_cleanup_fn(req, tldap_msg_cleanup);
547 if (num_pending > 0) {
552 * We're the first one, add the read_ldap request that waits for the
553 * answer from the server
555 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
556 if (subreq == NULL) {
557 tldap_msg_unset_pending(req);
560 tevent_req_set_callback(subreq, tldap_msg_received, ld);
564 static void tldap_msg_sent(struct tevent_req *subreq)
566 struct tevent_req *req = tevent_req_callback_data(
567 subreq, struct tevent_req);
568 struct tldap_msg_state *state = tevent_req_data(
569 req, struct tldap_msg_state);
573 nwritten = tstream_writev_queue_recv(subreq, &err);
575 if (nwritten == -1) {
576 state->ld->server_down = true;
577 tevent_req_ldap_error(req, TLDAP_SERVER_DOWN);
581 if (!tldap_msg_set_pending(req)) {
587 static int tldap_msg_msgid(struct tevent_req *req)
589 struct tldap_msg_state *state = tevent_req_data(
590 req, struct tldap_msg_state);
595 static void tldap_msg_received(struct tevent_req *subreq)
597 struct tldap_context *ld = tevent_req_callback_data(
598 subreq, struct tldap_context);
599 struct tevent_req *req;
600 struct tldap_msg_state *state;
601 struct asn1_data *data;
611 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
613 if (received == -1) {
614 ld->server_down = true;
615 status = TLDAP_SERVER_DOWN;
619 data = asn1_init(talloc_tos());
621 status = TLDAP_NO_MEMORY;
624 asn1_load_nocopy(data, inbuf, received);
627 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
628 ok &= asn1_read_Integer(data, &id);
629 ok &= asn1_peek_uint8(data, &type);
632 status = TLDAP_PROTOCOL_ERROR;
636 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
637 "type %d\n", id, (int)type);
639 num_pending = talloc_array_length(ld->pending);
641 for (i=0; i<num_pending; i++) {
642 if (id == tldap_msg_msgid(ld->pending[i])) {
646 if (i == num_pending) {
647 /* Dump unexpected reply */
648 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
649 "No request pending for msg %d\n", id);
655 req = ld->pending[i];
656 state = tevent_req_data(req, struct tldap_msg_state);
658 state->inbuf = talloc_move(state, &inbuf);
659 state->data = talloc_move(state, &data);
661 tldap_msg_unset_pending(req);
662 num_pending = talloc_array_length(ld->pending);
664 tevent_req_done(req);
667 if (num_pending == 0) {
670 if (talloc_array_length(ld->pending) > num_pending) {
672 * The callback functions called from tevent_req_done() above
673 * have put something on the pending queue. We don't have to
674 * trigger the read_ldap_send(), tldap_msg_set_pending() has
675 * done it for us already.
680 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
681 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
682 if (subreq == NULL) {
683 status = TLDAP_NO_MEMORY;
686 tevent_req_set_callback(subreq, tldap_msg_received, ld);
690 while (talloc_array_length(ld->pending) > 0) {
691 req = ld->pending[0];
692 state = tevent_req_data(req, struct tldap_msg_state);
693 tevent_req_defer_callback(req, state->ev);
694 tevent_req_ldap_error(req, status);
698 static TLDAPRC tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
699 struct tldap_message **pmsg)
701 struct tldap_msg_state *state = tevent_req_data(
702 req, struct tldap_msg_state);
703 struct tldap_message *msg;
707 if (tevent_req_is_ldap_error(req, &err)) {
711 if (!asn1_peek_uint8(state->data, &msgtype)) {
712 return TLDAP_PROTOCOL_ERROR;
716 return TLDAP_SUCCESS;
719 msg = talloc_zero(mem_ctx, struct tldap_message);
721 return TLDAP_NO_MEMORY;
725 msg->inbuf = talloc_move(msg, &state->inbuf);
726 msg->data = talloc_move(msg, &state->data);
730 return TLDAP_SUCCESS;
733 struct tldap_req_state {
735 struct asn1_data *out;
736 struct tldap_message *result;
739 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
740 struct tldap_context *ld,
741 struct tldap_req_state **pstate)
743 struct tevent_req *req;
744 struct tldap_req_state *state;
746 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
750 state->out = asn1_init(state);
751 if (state->out == NULL) {
754 state->id = tldap_next_msgid(ld);
756 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
757 if (!asn1_write_Integer(state->out, state->id)) goto err;
768 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
770 struct tldap_req_state *state = tevent_req_data(
771 req, struct tldap_req_state);
773 TALLOC_FREE(ld->last_msg);
774 ld->last_msg = talloc_move(ld, &state->result);
777 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
779 char *result = talloc_array(mem_ctx, char, blob.length+1);
781 if (result == NULL) {
785 memcpy(result, blob.data, blob.length);
786 result[blob.length] = '\0';
790 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
791 struct asn1_data *data,
796 if (!asn1_read_OctetString(data, mem_ctx, &string))
799 result = blob2string_talloc(mem_ctx, string);
801 data_blob_free(&string);
803 if (result == NULL) {
810 static bool tldap_decode_controls(struct tldap_req_state *state);
812 static bool tldap_decode_response(struct tldap_req_state *state)
814 struct asn1_data *data = state->result->data;
815 struct tldap_message *msg = state->result;
819 ok &= asn1_read_enumerated(data, &rc);
821 msg->lderr = TLDAP_RC(rc);
824 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
825 ok &= asn1_read_OctetString_talloc(msg, data,
826 &msg->res_diagnosticmessage);
828 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
829 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
830 ok &= asn1_read_OctetString_talloc(msg, data,
832 ok &= asn1_end_tag(data);
834 msg->res_referral = NULL;
840 static void tldap_sasl_bind_done(struct tevent_req *subreq);
842 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
843 struct tevent_context *ev,
844 struct tldap_context *ld,
846 const char *mechanism,
848 struct tldap_control *sctrls,
850 struct tldap_control *cctrls,
853 struct tevent_req *req, *subreq;
854 struct tldap_req_state *state;
856 req = tldap_req_create(mem_ctx, ld, &state);
865 if (!asn1_push_tag(state->out, TLDAP_REQ_BIND)) goto err;
866 if (!asn1_write_Integer(state->out, ld->ld_version)) goto err;
867 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
869 if (mechanism == NULL) {
870 if (!asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0))) goto err;
871 if (!asn1_write(state->out, creds->data, creds->length)) goto err;
872 if (!asn1_pop_tag(state->out)) goto err;
874 if (!asn1_push_tag(state->out, ASN1_CONTEXT(3))) goto err;
875 if (!asn1_write_OctetString(state->out, mechanism,
876 strlen(mechanism))) goto err;
877 if ((creds != NULL) && (creds->data != NULL)) {
878 if (!asn1_write_OctetString(state->out, creds->data,
879 creds->length)) goto err;
881 if (!asn1_pop_tag(state->out)) goto err;
884 if (!asn1_pop_tag(state->out)) goto err;
886 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
888 if (tevent_req_nomem(subreq, req)) {
889 return tevent_req_post(req, ev);
891 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
896 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
897 return tevent_req_post(req, ev);
900 static void tldap_sasl_bind_done(struct tevent_req *subreq)
902 struct tevent_req *req = tevent_req_callback_data(
903 subreq, struct tevent_req);
904 struct tldap_req_state *state = tevent_req_data(
905 req, struct tldap_req_state);
909 rc = tldap_msg_recv(subreq, state, &state->result);
911 if (tevent_req_ldap_error(req, rc)) {
914 if (state->result->type != TLDAP_RES_BIND) {
915 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
919 ok = asn1_start_tag(state->result->data, TLDAP_RES_BIND);
920 ok &= tldap_decode_response(state);
922 if (asn1_peek_tag(state->result->data, ASN1_CONTEXT_SIMPLE(7))) {
925 ok &= asn1_start_tag(state->result->data,
926 ASN1_CONTEXT_SIMPLE(7));
931 len = asn1_tag_remaining(state->result->data);
936 state->result->res_serverSaslCreds =
937 data_blob_talloc(state->result, NULL, len);
938 if (state->result->res_serverSaslCreds.data == NULL) {
942 ok = asn1_read(state->result->data,
943 state->result->res_serverSaslCreds.data,
944 state->result->res_serverSaslCreds.length);
946 ok &= asn1_end_tag(state->result->data);
949 ok &= asn1_end_tag(state->result->data);
955 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr) &&
956 !TLDAP_RC_EQUAL(state->result->lderr,
957 TLDAP_SASL_BIND_IN_PROGRESS)) {
958 tevent_req_ldap_error(req, state->result->lderr);
961 tevent_req_done(req);
965 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
969 TLDAPRC tldap_sasl_bind_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
970 DATA_BLOB *serverSaslCreds)
972 struct tldap_req_state *state = tevent_req_data(
973 req, struct tldap_req_state);
976 if (tevent_req_is_ldap_error(req, &rc)) {
980 if (serverSaslCreds != NULL) {
981 serverSaslCreds->data = talloc_move(
982 mem_ctx, &state->result->res_serverSaslCreds.data);
983 serverSaslCreds->length =
984 state->result->res_serverSaslCreds.length;
987 return state->result->lderr;
990 TLDAPRC tldap_sasl_bind(struct tldap_context *ld,
992 const char *mechanism,
994 struct tldap_control *sctrls,
996 struct tldap_control *cctrls,
999 DATA_BLOB *serverSaslCreds)
1001 TALLOC_CTX *frame = talloc_stackframe();
1002 struct tevent_context *ev;
1003 struct tevent_req *req;
1004 TLDAPRC rc = TLDAP_NO_MEMORY;
1006 ev = samba_tevent_context_init(frame);
1010 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
1011 sctrls, num_sctrls, cctrls, num_cctrls);
1015 if (!tevent_req_poll(req, ev)) {
1016 rc = TLDAP_OPERATIONS_ERROR;
1019 rc = tldap_sasl_bind_recv(req, mem_ctx, serverSaslCreds);
1020 tldap_save_msg(ld, req);
1026 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
1027 struct tevent_context *ev,
1028 struct tldap_context *ld,
1034 if (passwd != NULL) {
1035 cred.data = discard_const_p(uint8_t, passwd);
1036 cred.length = strlen(passwd);
1038 cred.data = discard_const_p(uint8_t, "");
1041 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
1045 TLDAPRC tldap_simple_bind_recv(struct tevent_req *req)
1047 return tldap_sasl_bind_recv(req, NULL, NULL);
1050 TLDAPRC tldap_simple_bind(struct tldap_context *ld, const char *dn,
1055 if (passwd != NULL) {
1056 cred.data = discard_const_p(uint8_t, passwd);
1057 cred.length = strlen(passwd);
1059 cred.data = discard_const_p(uint8_t, "");
1062 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0,
1066 /*****************************************************************************/
1068 /* can't use isalpha() as only a strict set is valid for LDAP */
1070 static bool tldap_is_alpha(char c)
1072 return (((c >= 'a') && (c <= 'z')) || \
1073 ((c >= 'A') && (c <= 'Z')));
1076 static bool tldap_is_adh(char c)
1078 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
1081 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
1082 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
1083 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
1084 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
1085 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
1086 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
1087 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
1088 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
1089 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
1090 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
1092 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
1093 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
1094 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1097 /* oid's should be numerical only in theory,
1098 * but apparently some broken servers may have alphanum aliases instead.
1099 * Do like openldap libraries and allow alphanum aliases for oids, but
1100 * do not allow Tagging options in that case.
1102 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1104 bool is_oid = false;
1108 /* first char has stricter rules */
1111 } else if (!tldap_is_alpha(*s)) {
1112 /* bad first char */
1116 for (i = 1; i < len; i++) {
1119 if (isdigit(s[i])) {
1132 if (tldap_is_adh(s[i])) {
1139 /* no tagging options */
1146 if ((i + 1) == len) {
1164 /* this function copies the value until the closing parenthesis is found. */
1165 static char *tldap_get_val(TALLOC_CTX *memctx,
1166 const char *value, const char **_s)
1168 const char *s = value;
1170 /* find terminator */
1173 if (s && (*(s - 1) == '\\')) {
1178 if (!s || !(*s == ')')) {
1179 /* malformed filter */
1185 return talloc_strndup(memctx, value, s - value);
1188 static int tldap_hex2char(const char *x)
1190 if (isxdigit(x[0]) && isxdigit(x[1])) {
1191 const char h1 = x[0], h2 = x[1];
1194 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1195 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1196 else if (h1 >= '0') c = h1 - (int)'0';
1198 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1199 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1200 else if (h2 >= '0') c += h2 - (int)'0';
1208 static bool tldap_find_first_star(const char *val, const char **star)
1212 for (s = val; *s; s++) {
1215 if (isxdigit(s[1]) && isxdigit(s[2])) {
1219 /* not hex based escape, check older syntax */
1228 /* invalid escape sequence */
1233 /* end of val, nothing found */
1243 /* string ended without closing parenthesis, filter is malformed */
1247 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1251 for (i = 0,p = 0; i < *val_len; i++) {
1257 /* these must be escaped */
1261 if (!value[i + 1]) {
1267 c = tldap_hex2char(&value[i]);
1268 if (c >= 0 && c < 256) {
1280 value[p] = value[i];
1289 value[p] = value[i];
1298 static bool tldap_push_filter_basic(struct tldap_context *ld,
1299 struct asn1_data *data,
1301 static bool tldap_push_filter_substring(struct tldap_context *ld,
1302 struct asn1_data *data,
1305 static bool tldap_push_filter_int(struct tldap_context *ld,
1306 struct asn1_data *data,
1309 const char *s = *_s;
1313 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1314 "Incomplete or malformed filter\n");
1319 /* we are right after a parenthesis,
1320 * find out what op we have at hand */
1323 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1324 if (!asn1_push_tag(data, TLDAP_FILTER_AND)) return false;
1329 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1330 if (!asn1_push_tag(data, TLDAP_FILTER_OR)) return false;
1335 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1336 if (!asn1_push_tag(data, TLDAP_FILTER_NOT)) return false;
1338 ret = tldap_push_filter_int(ld, data, &s);
1342 if (!asn1_pop_tag(data)) return false;
1347 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1348 "Invalid parenthesis '%c'\n", *s);
1352 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1353 "Invalid filter termination\n");
1357 ret = tldap_push_filter_basic(ld, data, &s);
1364 /* only and/or filters get here.
1365 * go through the list of filters */
1368 /* RFC 4526: empty and/or */
1369 if (!asn1_pop_tag(data)) return false;
1374 ret = tldap_push_filter_int(ld, data, &s);
1380 /* end of list, return */
1381 if (!asn1_pop_tag(data)) return false;
1388 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1389 "Incomplete or malformed filter\n");
1394 if (asn1_has_error(data)) {
1403 static bool tldap_push_filter_basic(struct tldap_context *ld,
1404 struct asn1_data *data,
1407 TALLOC_CTX *tmpctx = talloc_tos();
1408 const char *s = *_s;
1416 size_t type_len = 0;
1419 bool write_octect = true;
1422 eq = strchr(s, '=');
1424 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1425 "Invalid filter, missing equal sign\n");
1434 if (!asn1_push_tag(data, TLDAP_FILTER_LE)) return false;
1438 if (!asn1_push_tag(data, TLDAP_FILTER_GE)) return false;
1442 if (!asn1_push_tag(data, TLDAP_FILTER_APX)) return false;
1446 if (!asn1_push_tag(data, TLDAP_FILTER_EXT)) return false;
1447 write_octect = false;
1453 if (*s == ':') { /* [:dn]:rule:= value */
1455 /* malformed filter */
1459 } else { /* type[:dn][:rule]:= value */
1461 dn = strchr(s, ':');
1462 type_len = dn - type;
1463 if (dn == e) { /* type:= value */
1470 rule = strchr(dn, ':');
1474 if ((rule == dn + 1) || rule + 1 == e) {
1475 /* malformed filter, contains "::" */
1479 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1484 /* malformed filter. With two
1485 * optionals, the first must be "dn"
1498 if (!type && !dn && !rule) {
1499 /* malformed filter, there must be at least one */
1504 MatchingRuleAssertion ::= SEQUENCE {
1505 matchingRule [1] MatchingRuleID OPTIONAL,
1506 type [2] AttributeDescription OPTIONAL,
1507 matchValue [3] AssertionValue,
1508 dnAttributes [4] BOOLEAN DEFAULT FALSE
1512 /* check and add rule */
1514 ret = tldap_is_attrdesc(rule, e - rule, true);
1518 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1))) return false;
1519 if (!asn1_write(data, rule, e - rule)) return false;
1520 if (!asn1_pop_tag(data)) return false;
1523 /* check and add type */
1525 ret = tldap_is_attrdesc(type, type_len, false);
1529 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2))) return false;
1530 if (!asn1_write(data, type, type_len)) return false;
1531 if (!asn1_pop_tag(data)) return false;
1534 uval = tldap_get_val(tmpctx, val, _s);
1538 uval_len = *_s - val;
1539 ret = tldap_unescape_inplace(uval, &uval_len);
1544 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3))) return false;
1545 if (!asn1_write(data, uval, uval_len)) return false;
1546 if (!asn1_pop_tag(data)) return false;
1548 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4))) return false;
1549 if (!asn1_write_uint8(data, dn?1:0)) return false;
1550 if (!asn1_pop_tag(data)) return false;
1556 ret = tldap_is_attrdesc(s, e - s, false);
1561 if (strncmp(val, "*)", 2) == 0) {
1563 if (!asn1_push_tag(data, TLDAP_FILTER_PRES)) return false;
1564 if (!asn1_write(data, s, e - s)) return false;
1566 write_octect = false;
1570 ret = tldap_find_first_star(val, &star);
1576 if (!asn1_push_tag(data, TLDAP_FILTER_SUB)) return false;
1577 if (!asn1_write_OctetString(data, s, e - s)) return false;
1578 ret = tldap_push_filter_substring(ld, data, val, &s);
1583 write_octect = false;
1587 /* if nothing else, then it is just equality */
1588 if (!asn1_push_tag(data, TLDAP_FILTER_EQ)) return false;
1589 write_octect = true;
1594 uval = tldap_get_val(tmpctx, val, _s);
1598 uval_len = *_s - val;
1599 ret = tldap_unescape_inplace(uval, &uval_len);
1604 if (!asn1_write_OctetString(data, s, e - s)) return false;
1605 if (!asn1_write_OctetString(data, uval, uval_len)) return false;
1608 if (asn1_has_error(data)) {
1611 return asn1_pop_tag(data);
1614 static bool tldap_push_filter_substring(struct tldap_context *ld,
1615 struct asn1_data *data,
1619 TALLOC_CTX *tmpctx = talloc_tos();
1620 bool initial = true;
1627 SubstringFilter ::= SEQUENCE {
1628 type AttributeDescription,
1629 -- at least one must be present
1630 substrings SEQUENCE OF CHOICE {
1631 initial [0] LDAPString,
1633 final [2] LDAPString } }
1635 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
1638 ret = tldap_find_first_star(val, &star);
1642 chunk_len = star - val;
1646 if (!initial && chunk_len == 0) {
1647 /* found '**', which is illegal */
1663 if (initial && chunk_len == 0) {
1669 chunk = talloc_strndup(tmpctx, val, chunk_len);
1673 ret = tldap_unescape_inplace(chunk, &chunk_len);
1680 if (!asn1_push_tag(data, TLDAP_SUB_INI)) return false;
1683 if (!asn1_push_tag(data, TLDAP_SUB_ANY)) return false;
1687 if (!asn1_push_tag(data, TLDAP_SUB_FIN)) return false;
1693 if (!asn1_write(data, chunk, chunk_len)) return false;
1694 if (!asn1_pop_tag(data)) return false;
1698 } while (*star == '*');
1702 /* end of sequence */
1703 return asn1_pop_tag(data);
1706 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1707 * around parenthesis, we do not allow any spaces (except in values of
1708 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1709 * leading or trailing spaces where allowed.
1711 static bool tldap_push_filter(struct tldap_context *ld,
1712 struct asn1_data *data,
1715 const char *s = filter;
1718 ret = tldap_push_filter_int(ld, data, &s);
1720 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1721 "Incomplete or malformed filter\n");
1727 /*****************************************************************************/
1729 static void tldap_search_done(struct tevent_req *subreq);
1731 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1732 struct tevent_context *ev,
1733 struct tldap_context *ld,
1734 const char *base, int scope,
1739 struct tldap_control *sctrls,
1741 struct tldap_control *cctrls,
1747 struct tevent_req *req, *subreq;
1748 struct tldap_req_state *state;
1751 req = tldap_req_create(mem_ctx, ld, &state);
1756 if (!asn1_push_tag(state->out, TLDAP_REQ_SEARCH)) goto encoding_error;
1757 if (!asn1_write_OctetString(state->out, base, strlen(base))) goto encoding_error;
1758 if (!asn1_write_enumerated(state->out, scope)) goto encoding_error;
1759 if (!asn1_write_enumerated(state->out, deref)) goto encoding_error;
1760 if (!asn1_write_Integer(state->out, sizelimit)) goto encoding_error;
1761 if (!asn1_write_Integer(state->out, timelimit)) goto encoding_error;
1762 if (!asn1_write_BOOLEAN(state->out, attrsonly)) goto encoding_error;
1764 if (!tldap_push_filter(ld, state->out, filter)) {
1765 goto encoding_error;
1768 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto encoding_error;
1769 for (i=0; i<num_attrs; i++) {
1770 if (!asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]))) goto encoding_error;
1772 if (!asn1_pop_tag(state->out)) goto encoding_error;
1773 if (!asn1_pop_tag(state->out)) goto encoding_error;
1775 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1776 sctrls, num_sctrls);
1777 if (tevent_req_nomem(subreq, req)) {
1778 return tevent_req_post(req, ev);
1780 tevent_req_set_callback(subreq, tldap_search_done, req);
1784 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
1785 return tevent_req_post(req, ev);
1788 static void tldap_search_done(struct tevent_req *subreq)
1790 struct tevent_req *req = tevent_req_callback_data(
1791 subreq, struct tevent_req);
1792 struct tldap_req_state *state = tevent_req_data(
1793 req, struct tldap_req_state);
1796 rc = tldap_msg_recv(subreq, state, &state->result);
1797 if (tevent_req_ldap_error(req, rc)) {
1800 switch (state->result->type) {
1801 case TLDAP_RES_SEARCH_ENTRY:
1802 case TLDAP_RES_SEARCH_REFERENCE:
1803 if (!tldap_msg_set_pending(subreq)) {
1804 tevent_req_oom(req);
1807 tevent_req_notify_callback(req);
1809 case TLDAP_RES_SEARCH_RESULT:
1810 TALLOC_FREE(subreq);
1811 if (!asn1_start_tag(state->result->data,
1812 state->result->type) ||
1813 !tldap_decode_response(state) ||
1814 !asn1_end_tag(state->result->data) ||
1815 !tldap_decode_controls(state)) {
1816 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
1819 tevent_req_done(req);
1822 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
1827 TLDAPRC tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1828 struct tldap_message **pmsg)
1830 struct tldap_req_state *state = tevent_req_data(
1831 req, struct tldap_req_state);
1834 if (!tevent_req_is_in_progress(req)
1835 && tevent_req_is_ldap_error(req, &rc)) {
1839 if (tevent_req_is_in_progress(req)) {
1840 switch (state->result->type) {
1841 case TLDAP_RES_SEARCH_ENTRY:
1842 case TLDAP_RES_SEARCH_REFERENCE:
1845 return TLDAP_OPERATIONS_ERROR;
1849 *pmsg = talloc_move(mem_ctx, &state->result);
1850 return TLDAP_SUCCESS;
1853 struct tldap_sync_search_state {
1854 TALLOC_CTX *mem_ctx;
1855 struct tldap_message **entries;
1856 struct tldap_message **refs;
1860 static void tldap_search_cb(struct tevent_req *req)
1862 struct tldap_sync_search_state *state =
1863 (struct tldap_sync_search_state *)
1864 tevent_req_callback_data_void(req);
1865 struct tldap_message *msg, **tmp;
1866 int num_entries, num_refs;
1868 state->rc = tldap_search_recv(req, talloc_tos(), &msg);
1869 if (!TLDAP_RC_IS_SUCCESS(state->rc)) {
1873 switch (tldap_msg_type(msg)) {
1874 case TLDAP_RES_SEARCH_ENTRY:
1875 num_entries = talloc_array_length(state->entries);
1876 tmp = talloc_realloc(state->mem_ctx, state->entries,
1877 struct tldap_message *, num_entries + 1);
1879 state->rc = TLDAP_NO_MEMORY;
1882 state->entries = tmp;
1883 state->entries[num_entries] = talloc_move(state->entries,
1886 case TLDAP_RES_SEARCH_REFERENCE:
1887 num_refs = talloc_array_length(state->refs);
1888 tmp = talloc_realloc(state->mem_ctx, state->refs,
1889 struct tldap_message *, num_refs + 1);
1891 state->rc = TLDAP_NO_MEMORY;
1895 state->refs[num_refs] = talloc_move(state->refs, &msg);
1897 case TLDAP_RES_SEARCH_RESULT:
1898 state->rc = TLDAP_SUCCESS;
1901 state->rc = TLDAP_PROTOCOL_ERROR;
1906 TLDAPRC tldap_search(struct tldap_context *ld,
1907 const char *base, int scope, const char *filter,
1908 const char **attrs, int num_attrs, int attrsonly,
1909 struct tldap_control *sctrls, int num_sctrls,
1910 struct tldap_control *cctrls, int num_cctrls,
1911 int timelimit, int sizelimit, int deref,
1912 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1913 struct tldap_message ***refs)
1915 TALLOC_CTX *frame = talloc_stackframe();
1916 struct tevent_context *ev;
1917 struct tevent_req *req;
1918 struct tldap_sync_search_state state = {
1919 .mem_ctx = mem_ctx, .rc = TLDAP_SUCCESS
1922 ev = samba_tevent_context_init(frame);
1924 state.rc = TLDAP_NO_MEMORY;
1928 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1929 attrs, num_attrs, attrsonly,
1930 sctrls, num_sctrls, cctrls, num_cctrls,
1931 timelimit, sizelimit, deref);
1933 state.rc = TLDAP_NO_MEMORY;
1937 tevent_req_set_callback(req, tldap_search_cb, &state);
1939 if (!tevent_req_is_in_progress(req)) {
1940 /* an error happend before sending */
1941 if (tevent_req_is_ldap_error(req, &state.rc)) {
1946 while (tevent_req_is_in_progress(req) &&
1947 TLDAP_RC_IS_SUCCESS(state.rc)) {
1948 if (tevent_loop_once(ev) == -1) {
1949 return TLDAP_OPERATIONS_ERROR;
1953 if (!TLDAP_RC_IS_SUCCESS(state.rc)) {
1957 if (entries != NULL) {
1958 *entries = state.entries;
1960 TALLOC_FREE(state.entries);
1965 TALLOC_FREE(state.refs);
1967 tldap_save_msg(ld, req);
1973 static bool tldap_parse_search_entry(struct tldap_message *msg)
1975 int num_attribs = 0;
1977 if (!asn1_start_tag(msg->data, msg->type)) return false;
1981 if (!asn1_read_OctetString_talloc(msg, msg->data, &msg->dn)) return false;
1983 if (msg->dn == NULL) {
1988 * Attributes: We overallocate msg->attribs by one, so that while
1989 * looping over the attributes we can directly parse into the last
1990 * array element. Same for the values in the inner loop.
1993 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1994 if (msg->attribs == NULL) {
1998 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
1999 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
2000 struct tldap_attribute *attrib;
2003 attrib = &msg->attribs[num_attribs];
2004 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
2005 if (attrib->values == NULL) {
2008 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
2009 if (!asn1_read_OctetString_talloc(msg->attribs, msg->data,
2010 &attrib->name)) return false;
2011 if (!asn1_start_tag(msg->data, ASN1_SET)) return false;
2013 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
2014 if (!asn1_read_OctetString(msg->data, msg,
2015 &attrib->values[num_values])) return false;
2017 attrib->values = talloc_realloc(
2018 msg->attribs, attrib->values, DATA_BLOB,
2020 if (attrib->values == NULL) {
2025 attrib->values = talloc_realloc(msg->attribs, attrib->values,
2026 DATA_BLOB, num_values);
2027 attrib->num_values = num_values;
2029 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SET */
2030 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SEQUENCE(0) */
2031 msg->attribs = talloc_realloc(
2032 msg, msg->attribs, struct tldap_attribute,
2034 if (msg->attribs == NULL) {
2039 msg->attribs = talloc_realloc(
2040 msg, msg->attribs, struct tldap_attribute, num_attribs);
2041 return asn1_end_tag(msg->data);
2044 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
2046 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2053 bool tldap_entry_attributes(struct tldap_message *msg,
2054 struct tldap_attribute **attributes,
2055 int *num_attributes)
2057 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2060 *attributes = msg->attribs;
2061 *num_attributes = talloc_array_length(msg->attribs);
2065 static bool tldap_decode_controls(struct tldap_req_state *state)
2067 struct tldap_message *msg = state->result;
2068 struct asn1_data *data = msg->data;
2069 struct tldap_control *sctrls = NULL;
2070 int num_controls = 0;
2073 msg->res_sctrls = NULL;
2075 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
2079 if (!asn1_start_tag(data, ASN1_CONTEXT(0))) goto out;
2081 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
2082 struct tldap_control *c;
2085 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
2087 if (sctrls == NULL) {
2090 c = &sctrls[num_controls];
2092 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) goto out;
2093 if (!asn1_read_OctetString_talloc(msg, data, &oid)) goto out;
2094 if (asn1_has_error(data) || (oid == NULL)) {
2098 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2099 if (!asn1_read_BOOLEAN(data, &c->critical)) goto out;
2101 c->critical = false;
2103 c->value = data_blob_null;
2104 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2105 !asn1_read_OctetString(data, msg, &c->value)) {
2108 if (!asn1_end_tag(data)) goto out; /* ASN1_SEQUENCE(0) */
2113 if (!asn1_end_tag(data)) goto out; /* ASN1_CONTEXT(0) */
2120 msg->res_sctrls = sctrls;
2122 TALLOC_FREE(sctrls);
2127 static void tldap_simple_done(struct tevent_req *subreq, int type)
2129 struct tevent_req *req = tevent_req_callback_data(
2130 subreq, struct tevent_req);
2131 struct tldap_req_state *state = tevent_req_data(
2132 req, struct tldap_req_state);
2135 rc = tldap_msg_recv(subreq, state, &state->result);
2136 TALLOC_FREE(subreq);
2137 if (tevent_req_ldap_error(req, rc)) {
2140 if (state->result->type != type) {
2141 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
2144 if (!asn1_start_tag(state->result->data, state->result->type) ||
2145 !tldap_decode_response(state) ||
2146 !asn1_end_tag(state->result->data) ||
2147 !tldap_decode_controls(state)) {
2148 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
2151 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr)) {
2152 tevent_req_ldap_error(req, state->result->lderr);
2155 tevent_req_done(req);
2158 static TLDAPRC tldap_simple_recv(struct tevent_req *req)
2161 if (tevent_req_is_ldap_error(req, &rc)) {
2164 return TLDAP_SUCCESS;
2167 static void tldap_add_done(struct tevent_req *subreq);
2169 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2170 struct tevent_context *ev,
2171 struct tldap_context *ld,
2173 struct tldap_mod *attributes,
2175 struct tldap_control *sctrls,
2177 struct tldap_control *cctrls,
2180 struct tevent_req *req, *subreq;
2181 struct tldap_req_state *state;
2184 req = tldap_req_create(mem_ctx, ld, &state);
2189 if (!asn1_push_tag(state->out, TLDAP_REQ_ADD)) goto err;
2190 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2191 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2193 for (i=0; i<num_attributes; i++) {
2194 struct tldap_mod *attrib = &attributes[i];
2195 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2196 if (!asn1_write_OctetString(state->out, attrib->attribute,
2197 strlen(attrib->attribute))) goto err;
2198 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2199 for (j=0; j<attrib->num_values; j++) {
2200 if (!asn1_write_OctetString(state->out,
2201 attrib->values[j].data,
2202 attrib->values[j].length)) goto err;
2204 if (!asn1_pop_tag(state->out)) goto err;
2205 if (!asn1_pop_tag(state->out)) goto err;
2208 if (!asn1_pop_tag(state->out)) goto err;
2209 if (!asn1_pop_tag(state->out)) goto err;
2211 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2212 sctrls, num_sctrls);
2213 if (tevent_req_nomem(subreq, req)) {
2214 return tevent_req_post(req, ev);
2216 tevent_req_set_callback(subreq, tldap_add_done, req);
2221 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2222 return tevent_req_post(req, ev);
2225 static void tldap_add_done(struct tevent_req *subreq)
2227 tldap_simple_done(subreq, TLDAP_RES_ADD);
2230 TLDAPRC tldap_add_recv(struct tevent_req *req)
2232 return tldap_simple_recv(req);
2235 TLDAPRC tldap_add(struct tldap_context *ld, const char *dn,
2236 struct tldap_mod *attributes, int num_attributes,
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 TLDAPRC rc = TLDAP_NO_MEMORY;
2245 ev = samba_tevent_context_init(frame);
2249 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2250 sctrls, num_sctrls, cctrls, num_cctrls);
2254 if (!tevent_req_poll(req, ev)) {
2255 rc = TLDAP_OPERATIONS_ERROR;
2258 rc = tldap_add_recv(req);
2259 tldap_save_msg(ld, req);
2265 static void tldap_modify_done(struct tevent_req *subreq);
2267 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2268 struct tevent_context *ev,
2269 struct tldap_context *ld,
2271 struct tldap_mod *mods, int num_mods,
2272 struct tldap_control *sctrls,
2274 struct tldap_control *cctrls,
2277 struct tevent_req *req, *subreq;
2278 struct tldap_req_state *state;
2281 req = tldap_req_create(mem_ctx, ld, &state);
2286 if (!asn1_push_tag(state->out, TLDAP_REQ_MODIFY)) goto err;
2287 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2288 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2290 for (i=0; i<num_mods; i++) {
2291 struct tldap_mod *mod = &mods[i];
2292 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2293 if (!asn1_write_enumerated(state->out, mod->mod_op)) goto err;
2294 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2295 if (!asn1_write_OctetString(state->out, mod->attribute,
2296 strlen(mod->attribute))) goto err;
2297 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2298 for (j=0; j<mod->num_values; j++) {
2299 if (!asn1_write_OctetString(state->out,
2300 mod->values[j].data,
2301 mod->values[j].length)) goto err;
2303 if (!asn1_pop_tag(state->out)) goto err;
2304 if (!asn1_pop_tag(state->out)) goto err;
2305 if (!asn1_pop_tag(state->out)) goto err;
2308 if (!asn1_pop_tag(state->out)) goto err;
2309 if (!asn1_pop_tag(state->out)) goto err;
2311 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2312 sctrls, num_sctrls);
2313 if (tevent_req_nomem(subreq, req)) {
2314 return tevent_req_post(req, ev);
2316 tevent_req_set_callback(subreq, tldap_modify_done, req);
2321 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2322 return tevent_req_post(req, ev);
2325 static void tldap_modify_done(struct tevent_req *subreq)
2327 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2330 TLDAPRC tldap_modify_recv(struct tevent_req *req)
2332 return tldap_simple_recv(req);
2335 TLDAPRC tldap_modify(struct tldap_context *ld, const char *dn,
2336 struct tldap_mod *mods, int num_mods,
2337 struct tldap_control *sctrls, int num_sctrls,
2338 struct tldap_control *cctrls, int num_cctrls)
2340 TALLOC_CTX *frame = talloc_stackframe();
2341 struct tevent_context *ev;
2342 struct tevent_req *req;
2343 TLDAPRC rc = TLDAP_NO_MEMORY;
2345 ev = samba_tevent_context_init(frame);
2349 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2350 sctrls, num_sctrls, cctrls, num_cctrls);
2354 if (!tevent_req_poll(req, ev)) {
2355 rc = TLDAP_OPERATIONS_ERROR;
2358 rc = tldap_modify_recv(req);
2359 tldap_save_msg(ld, req);
2365 static void tldap_delete_done(struct tevent_req *subreq);
2367 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2368 struct tevent_context *ev,
2369 struct tldap_context *ld,
2371 struct tldap_control *sctrls,
2373 struct tldap_control *cctrls,
2376 struct tevent_req *req, *subreq;
2377 struct tldap_req_state *state;
2379 req = tldap_req_create(mem_ctx, ld, &state);
2384 if (!asn1_push_tag(state->out, TLDAP_REQ_DELETE)) goto err;
2385 if (!asn1_write(state->out, dn, strlen(dn))) goto err;
2386 if (!asn1_pop_tag(state->out)) goto err;
2388 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2389 sctrls, num_sctrls);
2390 if (tevent_req_nomem(subreq, req)) {
2391 return tevent_req_post(req, ev);
2393 tevent_req_set_callback(subreq, tldap_delete_done, req);
2398 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2399 return tevent_req_post(req, ev);
2402 static void tldap_delete_done(struct tevent_req *subreq)
2404 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2407 TLDAPRC tldap_delete_recv(struct tevent_req *req)
2409 return tldap_simple_recv(req);
2412 TLDAPRC tldap_delete(struct tldap_context *ld, const char *dn,
2413 struct tldap_control *sctrls, int num_sctrls,
2414 struct tldap_control *cctrls, int num_cctrls)
2416 TALLOC_CTX *frame = talloc_stackframe();
2417 struct tevent_context *ev;
2418 struct tevent_req *req;
2419 TLDAPRC rc = TLDAP_NO_MEMORY;
2421 ev = samba_tevent_context_init(frame);
2425 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2426 cctrls, num_cctrls);
2430 if (!tevent_req_poll(req, ev)) {
2431 rc = TLDAP_OPERATIONS_ERROR;
2434 rc = tldap_delete_recv(req);
2435 tldap_save_msg(ld, req);
2441 int tldap_msg_id(const struct tldap_message *msg)
2446 int tldap_msg_type(const struct tldap_message *msg)
2451 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2456 return msg->res_matcheddn;
2459 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2464 return msg->res_diagnosticmessage;
2467 const char *tldap_msg_referral(struct tldap_message *msg)
2472 return msg->res_referral;
2475 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2476 struct tldap_control **sctrls)
2483 *sctrls = msg->res_sctrls;
2484 *num_sctrls = talloc_array_length(msg->res_sctrls);
2487 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2489 return ld->last_msg;
2492 static const struct { TLDAPRC rc; const char *string; } tldaprc_errmap[] =
2496 { TLDAP_OPERATIONS_ERROR,
2497 "TLDAP_OPERATIONS_ERROR" },
2498 { TLDAP_PROTOCOL_ERROR,
2499 "TLDAP_PROTOCOL_ERROR" },
2500 { TLDAP_TIMELIMIT_EXCEEDED,
2501 "TLDAP_TIMELIMIT_EXCEEDED" },
2502 { TLDAP_SIZELIMIT_EXCEEDED,
2503 "TLDAP_SIZELIMIT_EXCEEDED" },
2504 { TLDAP_COMPARE_FALSE,
2505 "TLDAP_COMPARE_FALSE" },
2506 { TLDAP_COMPARE_TRUE,
2507 "TLDAP_COMPARE_TRUE" },
2508 { TLDAP_STRONG_AUTH_NOT_SUPPORTED,
2509 "TLDAP_STRONG_AUTH_NOT_SUPPORTED" },
2510 { TLDAP_STRONG_AUTH_REQUIRED,
2511 "TLDAP_STRONG_AUTH_REQUIRED" },
2514 { TLDAP_ADMINLIMIT_EXCEEDED,
2515 "TLDAP_ADMINLIMIT_EXCEEDED" },
2516 { TLDAP_UNAVAILABLE_CRITICAL_EXTENSION,
2517 "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION" },
2518 { TLDAP_CONFIDENTIALITY_REQUIRED,
2519 "TLDAP_CONFIDENTIALITY_REQUIRED" },
2520 { TLDAP_SASL_BIND_IN_PROGRESS,
2521 "TLDAP_SASL_BIND_IN_PROGRESS" },
2522 { TLDAP_NO_SUCH_ATTRIBUTE,
2523 "TLDAP_NO_SUCH_ATTRIBUTE" },
2524 { TLDAP_UNDEFINED_TYPE,
2525 "TLDAP_UNDEFINED_TYPE" },
2526 { TLDAP_INAPPROPRIATE_MATCHING,
2527 "TLDAP_INAPPROPRIATE_MATCHING" },
2528 { TLDAP_CONSTRAINT_VIOLATION,
2529 "TLDAP_CONSTRAINT_VIOLATION" },
2530 { TLDAP_TYPE_OR_VALUE_EXISTS,
2531 "TLDAP_TYPE_OR_VALUE_EXISTS" },
2532 { TLDAP_INVALID_SYNTAX,
2533 "TLDAP_INVALID_SYNTAX" },
2534 { TLDAP_NO_SUCH_OBJECT,
2535 "TLDAP_NO_SUCH_OBJECT" },
2536 { TLDAP_ALIAS_PROBLEM,
2537 "TLDAP_ALIAS_PROBLEM" },
2538 { TLDAP_INVALID_DN_SYNTAX,
2539 "TLDAP_INVALID_DN_SYNTAX" },
2542 { TLDAP_ALIAS_DEREF_PROBLEM,
2543 "TLDAP_ALIAS_DEREF_PROBLEM" },
2544 { TLDAP_INAPPROPRIATE_AUTH,
2545 "TLDAP_INAPPROPRIATE_AUTH" },
2546 { TLDAP_INVALID_CREDENTIALS,
2547 "TLDAP_INVALID_CREDENTIALS" },
2548 { TLDAP_INSUFFICIENT_ACCESS,
2549 "TLDAP_INSUFFICIENT_ACCESS" },
2552 { TLDAP_UNAVAILABLE,
2553 "TLDAP_UNAVAILABLE" },
2554 { TLDAP_UNWILLING_TO_PERFORM,
2555 "TLDAP_UNWILLING_TO_PERFORM" },
2556 { TLDAP_LOOP_DETECT,
2557 "TLDAP_LOOP_DETECT" },
2558 { TLDAP_NAMING_VIOLATION,
2559 "TLDAP_NAMING_VIOLATION" },
2560 { TLDAP_OBJECT_CLASS_VIOLATION,
2561 "TLDAP_OBJECT_CLASS_VIOLATION" },
2562 { TLDAP_NOT_ALLOWED_ON_NONLEAF,
2563 "TLDAP_NOT_ALLOWED_ON_NONLEAF" },
2564 { TLDAP_NOT_ALLOWED_ON_RDN,
2565 "TLDAP_NOT_ALLOWED_ON_RDN" },
2566 { TLDAP_ALREADY_EXISTS,
2567 "TLDAP_ALREADY_EXISTS" },
2568 { TLDAP_NO_OBJECT_CLASS_MODS,
2569 "TLDAP_NO_OBJECT_CLASS_MODS" },
2570 { TLDAP_RESULTS_TOO_LARGE,
2571 "TLDAP_RESULTS_TOO_LARGE" },
2572 { TLDAP_AFFECTS_MULTIPLE_DSAS,
2573 "TLDAP_AFFECTS_MULTIPLE_DSAS" },
2576 { TLDAP_SERVER_DOWN,
2577 "TLDAP_SERVER_DOWN" },
2578 { TLDAP_LOCAL_ERROR,
2579 "TLDAP_LOCAL_ERROR" },
2580 { TLDAP_ENCODING_ERROR,
2581 "TLDAP_ENCODING_ERROR" },
2582 { TLDAP_DECODING_ERROR,
2583 "TLDAP_DECODING_ERROR" },
2586 { TLDAP_AUTH_UNKNOWN,
2587 "TLDAP_AUTH_UNKNOWN" },
2588 { TLDAP_FILTER_ERROR,
2589 "TLDAP_FILTER_ERROR" },
2590 { TLDAP_USER_CANCELLED,
2591 "TLDAP_USER_CANCELLED" },
2592 { TLDAP_PARAM_ERROR,
2593 "TLDAP_PARAM_ERROR" },
2595 "TLDAP_NO_MEMORY" },
2596 { TLDAP_CONNECT_ERROR,
2597 "TLDAP_CONNECT_ERROR" },
2598 { TLDAP_NOT_SUPPORTED,
2599 "TLDAP_NOT_SUPPORTED" },
2600 { TLDAP_CONTROL_NOT_FOUND,
2601 "TLDAP_CONTROL_NOT_FOUND" },
2602 { TLDAP_NO_RESULTS_RETURNED,
2603 "TLDAP_NO_RESULTS_RETURNED" },
2604 { TLDAP_MORE_RESULTS_TO_RETURN,
2605 "TLDAP_MORE_RESULTS_TO_RETURN" },
2606 { TLDAP_CLIENT_LOOP,
2607 "TLDAP_CLIENT_LOOP" },
2608 { TLDAP_REFERRAL_LIMIT_EXCEEDED,
2609 "TLDAP_REFERRAL_LIMIT_EXCEEDED" },
2612 const char *tldap_rc2string(TLDAPRC rc)
2616 for (i=0; i<ARRAY_SIZE(tldaprc_errmap); i++) {
2617 if (TLDAP_RC_EQUAL(rc, tldaprc_errmap[i].rc)) {
2618 return tldaprc_errmap[i].string;
2622 return "Unknown LDAP Error";