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 size_t tldap_pending_reqs(struct tldap_context *ld)
196 return talloc_array_length(ld->pending);
199 static struct tldap_ctx_attribute *tldap_context_findattr(
200 struct tldap_context *ld, const char *name)
204 num_attrs = talloc_array_length(ld->ctx_attrs);
206 for (i=0; i<num_attrs; i++) {
207 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
208 return &ld->ctx_attrs[i];
214 bool tldap_context_setattr(struct tldap_context *ld,
215 const char *name, const void *_pptr)
217 struct tldap_ctx_attribute *tmp, *attr;
220 void **pptr = (void **)discard_const_p(void,_pptr);
222 attr = tldap_context_findattr(ld, name);
225 * We don't actually delete attrs, we don't expect tons of
226 * attributes being shuffled around.
228 TALLOC_FREE(attr->ptr);
230 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
236 tmpname = talloc_strdup(ld, name);
237 if (tmpname == NULL) {
241 num_attrs = talloc_array_length(ld->ctx_attrs);
243 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
246 TALLOC_FREE(tmpname);
249 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
251 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
253 tmp[num_attrs].ptr = NULL;
260 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
262 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
270 struct read_ldap_state {
275 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
276 static void read_ldap_done(struct tevent_req *subreq);
278 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
279 struct tevent_context *ev,
280 struct tstream_context *conn)
282 struct tevent_req *req, *subreq;
283 struct read_ldap_state *state;
285 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
291 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
293 if (tevent_req_nomem(subreq, req)) {
294 return tevent_req_post(req, ev);
296 tevent_req_set_callback(subreq, read_ldap_done, req);
300 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
302 struct read_ldap_state *state = talloc_get_type_abort(
303 private_data, struct read_ldap_state);
308 /* We've been here, we're done */
313 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
315 if (buf[0] != 0x30) {
320 if ((len & 0x80) == 0) {
325 lensize = (len & 0x7f);
329 /* Please get us the full length */
332 if (buflen > 2 + lensize) {
336 if (buflen != 2 + lensize) {
340 for (i=0; i<lensize; i++) {
341 len = (len << 8) | buf[2+i];
346 static void read_ldap_done(struct tevent_req *subreq)
348 struct tevent_req *req = tevent_req_callback_data(
349 subreq, struct tevent_req);
350 struct read_ldap_state *state = tevent_req_data(
351 req, struct read_ldap_state);
355 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
358 tevent_req_error(req, err);
361 tevent_req_done(req);
364 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
365 uint8_t **pbuf, int *perrno)
367 struct read_ldap_state *state = tevent_req_data(
368 req, struct read_ldap_state);
370 if (tevent_req_is_unix_error(req, perrno)) {
373 *pbuf = talloc_move(mem_ctx, &state->buf);
374 return talloc_get_size(*pbuf);
377 struct tldap_msg_state {
378 struct tldap_context *ld;
379 struct tevent_context *ev;
383 struct asn1_data *data;
387 static bool tldap_push_controls(struct asn1_data *data,
388 struct tldap_control *sctrls,
393 if ((sctrls == NULL) || (num_sctrls == 0)) {
397 if (!asn1_push_tag(data, ASN1_CONTEXT(0))) return false;
399 for (i=0; i<num_sctrls; i++) {
400 struct tldap_control *c = &sctrls[i];
401 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
402 if (!asn1_write_OctetString(data, c->oid, strlen(c->oid))) return false;
404 if (!asn1_write_BOOLEAN(data, true)) return false;
406 if (c->value.data != NULL) {
407 if (!asn1_write_OctetString(data, c->value.data,
408 c->value.length)) return false;
410 if (!asn1_pop_tag(data)) return false; /* ASN1_SEQUENCE(0) */
413 return asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
416 static void tldap_msg_sent(struct tevent_req *subreq);
417 static void tldap_msg_received(struct tevent_req *subreq);
419 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
420 struct tevent_context *ev,
421 struct tldap_context *ld,
422 int id, struct asn1_data *data,
423 struct tldap_control *sctrls,
426 struct tevent_req *req, *subreq;
427 struct tldap_msg_state *state;
430 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
433 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
441 if (state->ld->server_down) {
442 tevent_req_ldap_error(req, TLDAP_SERVER_DOWN);
443 return tevent_req_post(req, ev);
446 if (!tldap_push_controls(data, sctrls, num_sctrls)) {
447 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
448 return tevent_req_post(req, ev);
452 if (!asn1_pop_tag(data)) {
453 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
454 return tevent_req_post(req, ev);
457 if (!asn1_blob(data, &blob)) {
458 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
459 return tevent_req_post(req, ev);
462 state->iov.iov_base = (void *)blob.data;
463 state->iov.iov_len = blob.length;
465 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
467 if (tevent_req_nomem(subreq, req)) {
468 return tevent_req_post(req, ev);
470 tevent_req_set_callback(subreq, tldap_msg_sent, req);
474 static void tldap_msg_unset_pending(struct tevent_req *req)
476 struct tldap_msg_state *state = tevent_req_data(
477 req, struct tldap_msg_state);
478 struct tldap_context *ld = state->ld;
479 int num_pending = tldap_pending_reqs(ld);
482 tevent_req_set_cleanup_fn(req, NULL);
484 if (num_pending == 1) {
485 TALLOC_FREE(ld->pending);
489 for (i=0; i<num_pending; i++) {
490 if (req == ld->pending[i]) {
494 if (i == num_pending) {
496 * Something's seriously broken. Just returning here is the
497 * right thing nevertheless, the point of this routine is to
498 * remove ourselves from cli->pending.
504 * Remove ourselves from the cli->pending array
506 if (num_pending > 1) {
507 ld->pending[i] = ld->pending[num_pending-1];
511 * No NULL check here, we're shrinking by sizeof(void *), and
512 * talloc_realloc just adjusts the size for this.
514 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
518 static void tldap_msg_cleanup(struct tevent_req *req,
519 enum tevent_req_state req_state)
522 case TEVENT_REQ_USER_ERROR:
523 case TEVENT_REQ_RECEIVED:
524 tldap_msg_unset_pending(req);
531 static bool tldap_msg_set_pending(struct tevent_req *req)
533 struct tldap_msg_state *state = tevent_req_data(
534 req, struct tldap_msg_state);
535 struct tldap_context *ld;
536 struct tevent_req **pending;
538 struct tevent_req *subreq;
541 num_pending = tldap_pending_reqs(ld);
543 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
545 if (pending == NULL) {
548 pending[num_pending] = req;
549 ld->pending = pending;
550 tevent_req_set_cleanup_fn(req, tldap_msg_cleanup);
552 if (num_pending > 0) {
557 * We're the first one, add the read_ldap request that waits for the
558 * answer from the server
560 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
561 if (subreq == NULL) {
562 tldap_msg_unset_pending(req);
565 tevent_req_set_callback(subreq, tldap_msg_received, ld);
569 static void tldap_msg_sent(struct tevent_req *subreq)
571 struct tevent_req *req = tevent_req_callback_data(
572 subreq, struct tevent_req);
573 struct tldap_msg_state *state = tevent_req_data(
574 req, struct tldap_msg_state);
578 nwritten = tstream_writev_queue_recv(subreq, &err);
580 if (nwritten == -1) {
581 state->ld->server_down = true;
582 tevent_req_ldap_error(req, TLDAP_SERVER_DOWN);
586 if (!tldap_msg_set_pending(req)) {
592 static int tldap_msg_msgid(struct tevent_req *req)
594 struct tldap_msg_state *state = tevent_req_data(
595 req, struct tldap_msg_state);
600 static void tldap_msg_received(struct tevent_req *subreq)
602 struct tldap_context *ld = tevent_req_callback_data(
603 subreq, struct tldap_context);
604 struct tevent_req *req;
605 struct tldap_msg_state *state;
606 struct asn1_data *data;
616 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
618 if (received == -1) {
619 ld->server_down = true;
620 status = TLDAP_SERVER_DOWN;
624 data = asn1_init(talloc_tos());
626 status = TLDAP_NO_MEMORY;
629 asn1_load_nocopy(data, inbuf, received);
632 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
633 ok &= asn1_read_Integer(data, &id);
634 ok &= asn1_peek_uint8(data, &type);
637 status = TLDAP_PROTOCOL_ERROR;
641 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
642 "type %d\n", id, (int)type);
644 num_pending = talloc_array_length(ld->pending);
646 for (i=0; i<num_pending; i++) {
647 if (id == tldap_msg_msgid(ld->pending[i])) {
651 if (i == num_pending) {
652 /* Dump unexpected reply */
653 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
654 "No request pending for msg %d\n", id);
660 req = ld->pending[i];
661 state = tevent_req_data(req, struct tldap_msg_state);
663 state->inbuf = talloc_move(state, &inbuf);
664 state->data = talloc_move(state, &data);
666 tldap_msg_unset_pending(req);
667 num_pending = talloc_array_length(ld->pending);
669 tevent_req_done(req);
672 if (num_pending == 0) {
675 if (talloc_array_length(ld->pending) > num_pending) {
677 * The callback functions called from tevent_req_done() above
678 * have put something on the pending queue. We don't have to
679 * trigger the read_ldap_send(), tldap_msg_set_pending() has
680 * done it for us already.
685 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
686 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
687 if (subreq == NULL) {
688 status = TLDAP_NO_MEMORY;
691 tevent_req_set_callback(subreq, tldap_msg_received, ld);
695 while (talloc_array_length(ld->pending) > 0) {
696 req = ld->pending[0];
697 state = tevent_req_data(req, struct tldap_msg_state);
698 tevent_req_defer_callback(req, state->ev);
699 tevent_req_ldap_error(req, status);
703 static TLDAPRC tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
704 struct tldap_message **pmsg)
706 struct tldap_msg_state *state = tevent_req_data(
707 req, struct tldap_msg_state);
708 struct tldap_message *msg;
712 if (tevent_req_is_ldap_error(req, &err)) {
716 if (!asn1_peek_uint8(state->data, &msgtype)) {
717 return TLDAP_PROTOCOL_ERROR;
721 return TLDAP_SUCCESS;
724 msg = talloc_zero(mem_ctx, struct tldap_message);
726 return TLDAP_NO_MEMORY;
730 msg->inbuf = talloc_move(msg, &state->inbuf);
731 msg->data = talloc_move(msg, &state->data);
735 return TLDAP_SUCCESS;
738 struct tldap_req_state {
740 struct asn1_data *out;
741 struct tldap_message *result;
744 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
745 struct tldap_context *ld,
746 struct tldap_req_state **pstate)
748 struct tevent_req *req;
749 struct tldap_req_state *state;
751 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
755 state->out = asn1_init(state);
756 if (state->out == NULL) {
759 state->id = tldap_next_msgid(ld);
761 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
762 if (!asn1_write_Integer(state->out, state->id)) goto err;
773 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
775 struct tldap_req_state *state = tevent_req_data(
776 req, struct tldap_req_state);
778 TALLOC_FREE(ld->last_msg);
779 ld->last_msg = talloc_move(ld, &state->result);
782 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
784 char *result = talloc_array(mem_ctx, char, blob.length+1);
786 if (result == NULL) {
790 memcpy(result, blob.data, blob.length);
791 result[blob.length] = '\0';
795 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
796 struct asn1_data *data,
801 if (!asn1_read_OctetString(data, mem_ctx, &string))
804 result = blob2string_talloc(mem_ctx, string);
806 data_blob_free(&string);
808 if (result == NULL) {
815 static bool tldap_decode_controls(struct tldap_req_state *state);
817 static bool tldap_decode_response(struct tldap_req_state *state)
819 struct asn1_data *data = state->result->data;
820 struct tldap_message *msg = state->result;
824 ok &= asn1_read_enumerated(data, &rc);
826 msg->lderr = TLDAP_RC(rc);
829 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
830 ok &= asn1_read_OctetString_talloc(msg, data,
831 &msg->res_diagnosticmessage);
833 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
834 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
835 ok &= asn1_read_OctetString_talloc(msg, data,
837 ok &= asn1_end_tag(data);
839 msg->res_referral = NULL;
845 static void tldap_sasl_bind_done(struct tevent_req *subreq);
847 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
848 struct tevent_context *ev,
849 struct tldap_context *ld,
851 const char *mechanism,
853 struct tldap_control *sctrls,
855 struct tldap_control *cctrls,
858 struct tevent_req *req, *subreq;
859 struct tldap_req_state *state;
861 req = tldap_req_create(mem_ctx, ld, &state);
870 if (!asn1_push_tag(state->out, TLDAP_REQ_BIND)) goto err;
871 if (!asn1_write_Integer(state->out, ld->ld_version)) goto err;
872 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
874 if (mechanism == NULL) {
875 if (!asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0))) goto err;
876 if (!asn1_write(state->out, creds->data, creds->length)) goto err;
877 if (!asn1_pop_tag(state->out)) goto err;
879 if (!asn1_push_tag(state->out, ASN1_CONTEXT(3))) goto err;
880 if (!asn1_write_OctetString(state->out, mechanism,
881 strlen(mechanism))) goto err;
882 if ((creds != NULL) && (creds->data != NULL)) {
883 if (!asn1_write_OctetString(state->out, creds->data,
884 creds->length)) goto err;
886 if (!asn1_pop_tag(state->out)) goto err;
889 if (!asn1_pop_tag(state->out)) goto err;
891 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
893 if (tevent_req_nomem(subreq, req)) {
894 return tevent_req_post(req, ev);
896 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
901 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
902 return tevent_req_post(req, ev);
905 static void tldap_sasl_bind_done(struct tevent_req *subreq)
907 struct tevent_req *req = tevent_req_callback_data(
908 subreq, struct tevent_req);
909 struct tldap_req_state *state = tevent_req_data(
910 req, struct tldap_req_state);
914 rc = tldap_msg_recv(subreq, state, &state->result);
916 if (tevent_req_ldap_error(req, rc)) {
919 if (state->result->type != TLDAP_RES_BIND) {
920 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
924 ok = asn1_start_tag(state->result->data, TLDAP_RES_BIND);
925 ok &= tldap_decode_response(state);
927 if (asn1_peek_tag(state->result->data, ASN1_CONTEXT_SIMPLE(7))) {
930 ok &= asn1_start_tag(state->result->data,
931 ASN1_CONTEXT_SIMPLE(7));
936 len = asn1_tag_remaining(state->result->data);
941 state->result->res_serverSaslCreds =
942 data_blob_talloc(state->result, NULL, len);
943 if (state->result->res_serverSaslCreds.data == NULL) {
947 ok = asn1_read(state->result->data,
948 state->result->res_serverSaslCreds.data,
949 state->result->res_serverSaslCreds.length);
951 ok &= asn1_end_tag(state->result->data);
954 ok &= asn1_end_tag(state->result->data);
960 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr) &&
961 !TLDAP_RC_EQUAL(state->result->lderr,
962 TLDAP_SASL_BIND_IN_PROGRESS)) {
963 tevent_req_ldap_error(req, state->result->lderr);
966 tevent_req_done(req);
970 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
974 TLDAPRC tldap_sasl_bind_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
975 DATA_BLOB *serverSaslCreds)
977 struct tldap_req_state *state = tevent_req_data(
978 req, struct tldap_req_state);
981 if (tevent_req_is_ldap_error(req, &rc)) {
985 if (serverSaslCreds != NULL) {
986 serverSaslCreds->data = talloc_move(
987 mem_ctx, &state->result->res_serverSaslCreds.data);
988 serverSaslCreds->length =
989 state->result->res_serverSaslCreds.length;
992 return state->result->lderr;
995 TLDAPRC tldap_sasl_bind(struct tldap_context *ld,
997 const char *mechanism,
999 struct tldap_control *sctrls,
1001 struct tldap_control *cctrls,
1003 TALLOC_CTX *mem_ctx,
1004 DATA_BLOB *serverSaslCreds)
1006 TALLOC_CTX *frame = talloc_stackframe();
1007 struct tevent_context *ev;
1008 struct tevent_req *req;
1009 TLDAPRC rc = TLDAP_NO_MEMORY;
1011 ev = samba_tevent_context_init(frame);
1015 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
1016 sctrls, num_sctrls, cctrls, num_cctrls);
1020 if (!tevent_req_poll(req, ev)) {
1021 rc = TLDAP_OPERATIONS_ERROR;
1024 rc = tldap_sasl_bind_recv(req, mem_ctx, serverSaslCreds);
1025 tldap_save_msg(ld, req);
1031 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
1032 struct tevent_context *ev,
1033 struct tldap_context *ld,
1039 if (passwd != NULL) {
1040 cred.data = discard_const_p(uint8_t, passwd);
1041 cred.length = strlen(passwd);
1043 cred.data = discard_const_p(uint8_t, "");
1046 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
1050 TLDAPRC tldap_simple_bind_recv(struct tevent_req *req)
1052 return tldap_sasl_bind_recv(req, NULL, NULL);
1055 TLDAPRC tldap_simple_bind(struct tldap_context *ld, const char *dn,
1060 if (passwd != NULL) {
1061 cred.data = discard_const_p(uint8_t, passwd);
1062 cred.length = strlen(passwd);
1064 cred.data = discard_const_p(uint8_t, "");
1067 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0,
1071 /*****************************************************************************/
1073 /* can't use isalpha() as only a strict set is valid for LDAP */
1075 static bool tldap_is_alpha(char c)
1077 return (((c >= 'a') && (c <= 'z')) || \
1078 ((c >= 'A') && (c <= 'Z')));
1081 static bool tldap_is_adh(char c)
1083 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
1086 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
1087 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
1088 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
1089 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
1090 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
1091 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
1092 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
1093 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
1094 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
1095 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
1097 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
1098 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
1099 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1102 /* oid's should be numerical only in theory,
1103 * but apparently some broken servers may have alphanum aliases instead.
1104 * Do like openldap libraries and allow alphanum aliases for oids, but
1105 * do not allow Tagging options in that case.
1107 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1109 bool is_oid = false;
1113 /* first char has stricter rules */
1116 } else if (!tldap_is_alpha(*s)) {
1117 /* bad first char */
1121 for (i = 1; i < len; i++) {
1124 if (isdigit(s[i])) {
1137 if (tldap_is_adh(s[i])) {
1144 /* no tagging options */
1151 if ((i + 1) == len) {
1169 /* this function copies the value until the closing parenthesis is found. */
1170 static char *tldap_get_val(TALLOC_CTX *memctx,
1171 const char *value, const char **_s)
1173 const char *s = value;
1175 /* find terminator */
1178 if (s && (*(s - 1) == '\\')) {
1183 if (!s || !(*s == ')')) {
1184 /* malformed filter */
1190 return talloc_strndup(memctx, value, s - value);
1193 static int tldap_hex2char(const char *x)
1195 if (isxdigit(x[0]) && isxdigit(x[1])) {
1196 const char h1 = x[0], h2 = x[1];
1199 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1200 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1201 else if (h1 >= '0') c = h1 - (int)'0';
1203 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1204 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1205 else if (h2 >= '0') c += h2 - (int)'0';
1213 static bool tldap_find_first_star(const char *val, const char **star)
1217 for (s = val; *s; s++) {
1220 if (isxdigit(s[1]) && isxdigit(s[2])) {
1224 /* not hex based escape, check older syntax */
1233 /* invalid escape sequence */
1238 /* end of val, nothing found */
1248 /* string ended without closing parenthesis, filter is malformed */
1252 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1256 for (i = 0,p = 0; i < *val_len; i++) {
1262 /* these must be escaped */
1266 if (!value[i + 1]) {
1272 c = tldap_hex2char(&value[i]);
1273 if (c >= 0 && c < 256) {
1285 value[p] = value[i];
1294 value[p] = value[i];
1303 static bool tldap_push_filter_basic(struct tldap_context *ld,
1304 struct asn1_data *data,
1306 static bool tldap_push_filter_substring(struct tldap_context *ld,
1307 struct asn1_data *data,
1310 static bool tldap_push_filter_int(struct tldap_context *ld,
1311 struct asn1_data *data,
1314 const char *s = *_s;
1318 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1319 "Incomplete or malformed filter\n");
1324 /* we are right after a parenthesis,
1325 * find out what op we have at hand */
1328 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1329 if (!asn1_push_tag(data, TLDAP_FILTER_AND)) return false;
1334 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1335 if (!asn1_push_tag(data, TLDAP_FILTER_OR)) return false;
1340 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1341 if (!asn1_push_tag(data, TLDAP_FILTER_NOT)) return false;
1343 ret = tldap_push_filter_int(ld, data, &s);
1347 if (!asn1_pop_tag(data)) return false;
1352 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1353 "Invalid parenthesis '%c'\n", *s);
1357 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1358 "Invalid filter termination\n");
1362 ret = tldap_push_filter_basic(ld, data, &s);
1369 /* only and/or filters get here.
1370 * go through the list of filters */
1373 /* RFC 4526: empty and/or */
1374 if (!asn1_pop_tag(data)) return false;
1379 ret = tldap_push_filter_int(ld, data, &s);
1385 /* end of list, return */
1386 if (!asn1_pop_tag(data)) return false;
1393 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1394 "Incomplete or malformed filter\n");
1399 if (asn1_has_error(data)) {
1408 static bool tldap_push_filter_basic(struct tldap_context *ld,
1409 struct asn1_data *data,
1412 TALLOC_CTX *tmpctx = talloc_tos();
1413 const char *s = *_s;
1421 size_t type_len = 0;
1424 bool write_octect = true;
1427 eq = strchr(s, '=');
1429 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1430 "Invalid filter, missing equal sign\n");
1439 if (!asn1_push_tag(data, TLDAP_FILTER_LE)) return false;
1443 if (!asn1_push_tag(data, TLDAP_FILTER_GE)) return false;
1447 if (!asn1_push_tag(data, TLDAP_FILTER_APX)) return false;
1451 if (!asn1_push_tag(data, TLDAP_FILTER_EXT)) return false;
1452 write_octect = false;
1458 if (*s == ':') { /* [:dn]:rule:= value */
1460 /* malformed filter */
1464 } else { /* type[:dn][:rule]:= value */
1466 dn = strchr(s, ':');
1467 type_len = dn - type;
1468 if (dn == e) { /* type:= value */
1475 rule = strchr(dn, ':');
1479 if ((rule == dn + 1) || rule + 1 == e) {
1480 /* malformed filter, contains "::" */
1484 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1489 /* malformed filter. With two
1490 * optionals, the first must be "dn"
1503 if (!type && !dn && !rule) {
1504 /* malformed filter, there must be at least one */
1509 MatchingRuleAssertion ::= SEQUENCE {
1510 matchingRule [1] MatchingRuleID OPTIONAL,
1511 type [2] AttributeDescription OPTIONAL,
1512 matchValue [3] AssertionValue,
1513 dnAttributes [4] BOOLEAN DEFAULT FALSE
1517 /* check and add rule */
1519 ret = tldap_is_attrdesc(rule, e - rule, true);
1523 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1))) return false;
1524 if (!asn1_write(data, rule, e - rule)) return false;
1525 if (!asn1_pop_tag(data)) return false;
1528 /* check and add type */
1530 ret = tldap_is_attrdesc(type, type_len, false);
1534 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2))) return false;
1535 if (!asn1_write(data, type, type_len)) return false;
1536 if (!asn1_pop_tag(data)) return false;
1539 uval = tldap_get_val(tmpctx, val, _s);
1543 uval_len = *_s - val;
1544 ret = tldap_unescape_inplace(uval, &uval_len);
1549 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3))) return false;
1550 if (!asn1_write(data, uval, uval_len)) return false;
1551 if (!asn1_pop_tag(data)) return false;
1553 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4))) return false;
1554 if (!asn1_write_uint8(data, dn?1:0)) return false;
1555 if (!asn1_pop_tag(data)) return false;
1561 ret = tldap_is_attrdesc(s, e - s, false);
1566 if (strncmp(val, "*)", 2) == 0) {
1568 if (!asn1_push_tag(data, TLDAP_FILTER_PRES)) return false;
1569 if (!asn1_write(data, s, e - s)) return false;
1571 write_octect = false;
1575 ret = tldap_find_first_star(val, &star);
1581 if (!asn1_push_tag(data, TLDAP_FILTER_SUB)) return false;
1582 if (!asn1_write_OctetString(data, s, e - s)) return false;
1583 ret = tldap_push_filter_substring(ld, data, val, &s);
1588 write_octect = false;
1592 /* if nothing else, then it is just equality */
1593 if (!asn1_push_tag(data, TLDAP_FILTER_EQ)) return false;
1594 write_octect = true;
1599 uval = tldap_get_val(tmpctx, val, _s);
1603 uval_len = *_s - val;
1604 ret = tldap_unescape_inplace(uval, &uval_len);
1609 if (!asn1_write_OctetString(data, s, e - s)) return false;
1610 if (!asn1_write_OctetString(data, uval, uval_len)) return false;
1613 if (asn1_has_error(data)) {
1616 return asn1_pop_tag(data);
1619 static bool tldap_push_filter_substring(struct tldap_context *ld,
1620 struct asn1_data *data,
1624 TALLOC_CTX *tmpctx = talloc_tos();
1625 bool initial = true;
1632 SubstringFilter ::= SEQUENCE {
1633 type AttributeDescription,
1634 -- at least one must be present
1635 substrings SEQUENCE OF CHOICE {
1636 initial [0] LDAPString,
1638 final [2] LDAPString } }
1640 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
1643 ret = tldap_find_first_star(val, &star);
1647 chunk_len = star - val;
1651 if (!initial && chunk_len == 0) {
1652 /* found '**', which is illegal */
1668 if (initial && chunk_len == 0) {
1674 chunk = talloc_strndup(tmpctx, val, chunk_len);
1678 ret = tldap_unescape_inplace(chunk, &chunk_len);
1685 if (!asn1_push_tag(data, TLDAP_SUB_INI)) return false;
1688 if (!asn1_push_tag(data, TLDAP_SUB_ANY)) return false;
1692 if (!asn1_push_tag(data, TLDAP_SUB_FIN)) return false;
1698 if (!asn1_write(data, chunk, chunk_len)) return false;
1699 if (!asn1_pop_tag(data)) return false;
1703 } while (*star == '*');
1707 /* end of sequence */
1708 return asn1_pop_tag(data);
1711 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1712 * around parenthesis, we do not allow any spaces (except in values of
1713 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1714 * leading or trailing spaces where allowed.
1716 static bool tldap_push_filter(struct tldap_context *ld,
1717 struct asn1_data *data,
1720 const char *s = filter;
1723 ret = tldap_push_filter_int(ld, data, &s);
1725 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1726 "Incomplete or malformed filter\n");
1732 /*****************************************************************************/
1734 static void tldap_search_done(struct tevent_req *subreq);
1736 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1737 struct tevent_context *ev,
1738 struct tldap_context *ld,
1739 const char *base, int scope,
1744 struct tldap_control *sctrls,
1746 struct tldap_control *cctrls,
1752 struct tevent_req *req, *subreq;
1753 struct tldap_req_state *state;
1756 req = tldap_req_create(mem_ctx, ld, &state);
1761 if (!asn1_push_tag(state->out, TLDAP_REQ_SEARCH)) goto encoding_error;
1762 if (!asn1_write_OctetString(state->out, base, strlen(base))) goto encoding_error;
1763 if (!asn1_write_enumerated(state->out, scope)) goto encoding_error;
1764 if (!asn1_write_enumerated(state->out, deref)) goto encoding_error;
1765 if (!asn1_write_Integer(state->out, sizelimit)) goto encoding_error;
1766 if (!asn1_write_Integer(state->out, timelimit)) goto encoding_error;
1767 if (!asn1_write_BOOLEAN(state->out, attrsonly)) goto encoding_error;
1769 if (!tldap_push_filter(ld, state->out, filter)) {
1770 goto encoding_error;
1773 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto encoding_error;
1774 for (i=0; i<num_attrs; i++) {
1775 if (!asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]))) goto encoding_error;
1777 if (!asn1_pop_tag(state->out)) goto encoding_error;
1778 if (!asn1_pop_tag(state->out)) goto encoding_error;
1780 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1781 sctrls, num_sctrls);
1782 if (tevent_req_nomem(subreq, req)) {
1783 return tevent_req_post(req, ev);
1785 tevent_req_set_callback(subreq, tldap_search_done, req);
1789 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
1790 return tevent_req_post(req, ev);
1793 static void tldap_search_done(struct tevent_req *subreq)
1795 struct tevent_req *req = tevent_req_callback_data(
1796 subreq, struct tevent_req);
1797 struct tldap_req_state *state = tevent_req_data(
1798 req, struct tldap_req_state);
1801 rc = tldap_msg_recv(subreq, state, &state->result);
1802 if (tevent_req_ldap_error(req, rc)) {
1805 switch (state->result->type) {
1806 case TLDAP_RES_SEARCH_ENTRY:
1807 case TLDAP_RES_SEARCH_REFERENCE:
1808 if (!tldap_msg_set_pending(subreq)) {
1809 tevent_req_oom(req);
1812 tevent_req_notify_callback(req);
1814 case TLDAP_RES_SEARCH_RESULT:
1815 TALLOC_FREE(subreq);
1816 if (!asn1_start_tag(state->result->data,
1817 state->result->type) ||
1818 !tldap_decode_response(state) ||
1819 !asn1_end_tag(state->result->data) ||
1820 !tldap_decode_controls(state)) {
1821 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
1824 tevent_req_done(req);
1827 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
1832 TLDAPRC tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1833 struct tldap_message **pmsg)
1835 struct tldap_req_state *state = tevent_req_data(
1836 req, struct tldap_req_state);
1839 if (!tevent_req_is_in_progress(req)
1840 && tevent_req_is_ldap_error(req, &rc)) {
1844 if (tevent_req_is_in_progress(req)) {
1845 switch (state->result->type) {
1846 case TLDAP_RES_SEARCH_ENTRY:
1847 case TLDAP_RES_SEARCH_REFERENCE:
1850 return TLDAP_OPERATIONS_ERROR;
1854 *pmsg = talloc_move(mem_ctx, &state->result);
1855 return TLDAP_SUCCESS;
1858 struct tldap_search_all_state {
1859 struct tldap_message **msgs;
1860 struct tldap_message *result;
1863 static void tldap_search_all_done(struct tevent_req *subreq);
1865 struct tevent_req *tldap_search_all_send(
1866 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1867 struct tldap_context *ld, const char *base, int scope,
1868 const char *filter, const char **attrs, int num_attrs, int attrsonly,
1869 struct tldap_control *sctrls, int num_sctrls,
1870 struct tldap_control *cctrls, int num_cctrls,
1871 int timelimit, int sizelimit, int deref)
1873 struct tevent_req *req, *subreq;
1874 struct tldap_search_all_state *state;
1876 req = tevent_req_create(mem_ctx, &state,
1877 struct tldap_search_all_state);
1882 subreq = tldap_search_send(state, ev, ld, base, scope, filter,
1883 attrs, num_attrs, attrsonly,
1884 sctrls, num_sctrls, cctrls, num_cctrls,
1885 timelimit, sizelimit, deref);
1886 if (tevent_req_nomem(subreq, req)) {
1887 return tevent_req_post(req, ev);
1889 tevent_req_set_callback(subreq, tldap_search_all_done, req);
1893 static void tldap_search_all_done(struct tevent_req *subreq)
1895 struct tevent_req *req = tevent_req_callback_data(
1896 subreq, struct tevent_req);
1897 struct tldap_search_all_state *state = tevent_req_data(
1898 req, struct tldap_search_all_state);
1899 struct tldap_message *msg, **tmp;
1904 rc = tldap_search_recv(subreq, state, &msg);
1905 /* No TALLOC_FREE(subreq), this is multi-step */
1906 if (tevent_req_ldap_error(req, rc)) {
1907 TALLOC_FREE(subreq);
1911 msgtype = tldap_msg_type(msg);
1912 if (msgtype == TLDAP_RES_SEARCH_RESULT) {
1913 state->result = msg;
1914 tevent_req_done(req);
1918 num_msgs = talloc_array_length(state->msgs);
1920 tmp = talloc_realloc(state, state->msgs, struct tldap_message *,
1922 if (tevent_req_nomem(tmp, req)) {
1926 state->msgs[num_msgs] = talloc_move(state->msgs, &msg);
1929 TLDAPRC tldap_search_all_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1930 struct tldap_message ***msgs,
1931 struct tldap_message **result)
1933 struct tldap_search_all_state *state = tevent_req_data(
1934 req, struct tldap_search_all_state);
1937 if (tevent_req_is_ldap_error(req, &rc)) {
1942 *msgs = talloc_move(mem_ctx, &state->msgs);
1944 if (result != NULL) {
1945 *result = talloc_move(mem_ctx, &state->result);
1948 return TLDAP_SUCCESS;
1951 TLDAPRC tldap_search(struct tldap_context *ld,
1952 const char *base, int scope, const char *filter,
1953 const char **attrs, int num_attrs, int attrsonly,
1954 struct tldap_control *sctrls, int num_sctrls,
1955 struct tldap_control *cctrls, int num_cctrls,
1956 int timelimit, int sizelimit, int deref,
1957 TALLOC_CTX *mem_ctx, struct tldap_message ***pmsgs)
1960 struct tevent_context *ev;
1961 struct tevent_req *req;
1962 TLDAPRC rc = TLDAP_NO_MEMORY;
1963 struct tldap_message **msgs;
1964 struct tldap_message *result;
1966 if (tldap_pending_reqs(ld)) {
1970 frame = talloc_stackframe();
1972 ev = samba_tevent_context_init(frame);
1976 req = tldap_search_all_send(frame, ev, ld, base, scope, filter,
1977 attrs, num_attrs, attrsonly,
1978 sctrls, num_sctrls, cctrls, num_cctrls,
1979 timelimit, sizelimit, deref);
1983 if (!tevent_req_poll(req, ev)) {
1984 rc = TLDAP_OPERATIONS_ERROR;
1987 rc = tldap_search_all_recv(req, mem_ctx, &msgs, &result);
1989 if (!TLDAP_RC_IS_SUCCESS(rc)) {
1993 TALLOC_FREE(ld->last_msg);
1994 ld->last_msg = talloc_move(ld, &result);
2002 static bool tldap_parse_search_entry(struct tldap_message *msg)
2004 int num_attribs = 0;
2006 if (msg->type != TLDAP_RES_SEARCH_ENTRY) {
2009 if (!asn1_start_tag(msg->data, TLDAP_RES_SEARCH_ENTRY)) {
2015 if (!asn1_read_OctetString_talloc(msg, msg->data, &msg->dn)) return false;
2017 if (msg->dn == NULL) {
2022 * Attributes: We overallocate msg->attribs by one, so that while
2023 * looping over the attributes we can directly parse into the last
2024 * array element. Same for the values in the inner loop.
2027 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
2028 if (msg->attribs == NULL) {
2032 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
2033 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
2034 struct tldap_attribute *attrib;
2037 attrib = &msg->attribs[num_attribs];
2038 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
2039 if (attrib->values == NULL) {
2042 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
2043 if (!asn1_read_OctetString_talloc(msg->attribs, msg->data,
2044 &attrib->name)) return false;
2045 if (!asn1_start_tag(msg->data, ASN1_SET)) return false;
2047 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
2048 if (!asn1_read_OctetString(msg->data, msg,
2049 &attrib->values[num_values])) return false;
2051 attrib->values = talloc_realloc(
2052 msg->attribs, attrib->values, DATA_BLOB,
2054 if (attrib->values == NULL) {
2059 attrib->values = talloc_realloc(msg->attribs, attrib->values,
2060 DATA_BLOB, num_values);
2061 attrib->num_values = num_values;
2063 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SET */
2064 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SEQUENCE(0) */
2065 msg->attribs = talloc_realloc(
2066 msg, msg->attribs, struct tldap_attribute,
2068 if (msg->attribs == NULL) {
2073 msg->attribs = talloc_realloc(
2074 msg, msg->attribs, struct tldap_attribute, num_attribs);
2075 return asn1_end_tag(msg->data);
2078 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
2080 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2087 bool tldap_entry_attributes(struct tldap_message *msg,
2088 struct tldap_attribute **attributes,
2089 int *num_attributes)
2091 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2094 *attributes = msg->attribs;
2095 *num_attributes = talloc_array_length(msg->attribs);
2099 static bool tldap_decode_controls(struct tldap_req_state *state)
2101 struct tldap_message *msg = state->result;
2102 struct asn1_data *data = msg->data;
2103 struct tldap_control *sctrls = NULL;
2104 int num_controls = 0;
2107 msg->res_sctrls = NULL;
2109 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
2113 if (!asn1_start_tag(data, ASN1_CONTEXT(0))) goto out;
2115 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
2116 struct tldap_control *c;
2119 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
2121 if (sctrls == NULL) {
2124 c = &sctrls[num_controls];
2126 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) goto out;
2127 if (!asn1_read_OctetString_talloc(msg, data, &oid)) goto out;
2128 if (asn1_has_error(data) || (oid == NULL)) {
2132 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2133 if (!asn1_read_BOOLEAN(data, &c->critical)) goto out;
2135 c->critical = false;
2137 c->value = data_blob_null;
2138 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2139 !asn1_read_OctetString(data, msg, &c->value)) {
2142 if (!asn1_end_tag(data)) goto out; /* ASN1_SEQUENCE(0) */
2147 if (!asn1_end_tag(data)) goto out; /* ASN1_CONTEXT(0) */
2154 msg->res_sctrls = sctrls;
2156 TALLOC_FREE(sctrls);
2161 static void tldap_simple_done(struct tevent_req *subreq, int type)
2163 struct tevent_req *req = tevent_req_callback_data(
2164 subreq, struct tevent_req);
2165 struct tldap_req_state *state = tevent_req_data(
2166 req, struct tldap_req_state);
2169 rc = tldap_msg_recv(subreq, state, &state->result);
2170 TALLOC_FREE(subreq);
2171 if (tevent_req_ldap_error(req, rc)) {
2174 if (state->result->type != type) {
2175 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
2178 if (!asn1_start_tag(state->result->data, state->result->type) ||
2179 !tldap_decode_response(state) ||
2180 !asn1_end_tag(state->result->data) ||
2181 !tldap_decode_controls(state)) {
2182 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
2185 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr)) {
2186 tevent_req_ldap_error(req, state->result->lderr);
2189 tevent_req_done(req);
2192 static TLDAPRC tldap_simple_recv(struct tevent_req *req)
2195 if (tevent_req_is_ldap_error(req, &rc)) {
2198 return TLDAP_SUCCESS;
2201 static void tldap_add_done(struct tevent_req *subreq);
2203 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2204 struct tevent_context *ev,
2205 struct tldap_context *ld,
2207 struct tldap_mod *attributes,
2209 struct tldap_control *sctrls,
2211 struct tldap_control *cctrls,
2214 struct tevent_req *req, *subreq;
2215 struct tldap_req_state *state;
2218 req = tldap_req_create(mem_ctx, ld, &state);
2223 if (!asn1_push_tag(state->out, TLDAP_REQ_ADD)) goto err;
2224 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2225 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2227 for (i=0; i<num_attributes; i++) {
2228 struct tldap_mod *attrib = &attributes[i];
2229 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2230 if (!asn1_write_OctetString(state->out, attrib->attribute,
2231 strlen(attrib->attribute))) goto err;
2232 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2233 for (j=0; j<attrib->num_values; j++) {
2234 if (!asn1_write_OctetString(state->out,
2235 attrib->values[j].data,
2236 attrib->values[j].length)) goto err;
2238 if (!asn1_pop_tag(state->out)) goto err;
2239 if (!asn1_pop_tag(state->out)) goto err;
2242 if (!asn1_pop_tag(state->out)) goto err;
2243 if (!asn1_pop_tag(state->out)) goto err;
2245 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2246 sctrls, num_sctrls);
2247 if (tevent_req_nomem(subreq, req)) {
2248 return tevent_req_post(req, ev);
2250 tevent_req_set_callback(subreq, tldap_add_done, req);
2255 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2256 return tevent_req_post(req, ev);
2259 static void tldap_add_done(struct tevent_req *subreq)
2261 tldap_simple_done(subreq, TLDAP_RES_ADD);
2264 TLDAPRC tldap_add_recv(struct tevent_req *req)
2266 return tldap_simple_recv(req);
2269 TLDAPRC tldap_add(struct tldap_context *ld, const char *dn,
2270 struct tldap_mod *attributes, int num_attributes,
2271 struct tldap_control *sctrls, int num_sctrls,
2272 struct tldap_control *cctrls, int num_cctrls)
2274 TALLOC_CTX *frame = talloc_stackframe();
2275 struct tevent_context *ev;
2276 struct tevent_req *req;
2277 TLDAPRC rc = TLDAP_NO_MEMORY;
2279 ev = samba_tevent_context_init(frame);
2283 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2284 sctrls, num_sctrls, cctrls, num_cctrls);
2288 if (!tevent_req_poll(req, ev)) {
2289 rc = TLDAP_OPERATIONS_ERROR;
2292 rc = tldap_add_recv(req);
2293 tldap_save_msg(ld, req);
2299 static void tldap_modify_done(struct tevent_req *subreq);
2301 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2302 struct tevent_context *ev,
2303 struct tldap_context *ld,
2305 struct tldap_mod *mods, int num_mods,
2306 struct tldap_control *sctrls,
2308 struct tldap_control *cctrls,
2311 struct tevent_req *req, *subreq;
2312 struct tldap_req_state *state;
2315 req = tldap_req_create(mem_ctx, ld, &state);
2320 if (!asn1_push_tag(state->out, TLDAP_REQ_MODIFY)) goto err;
2321 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2322 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2324 for (i=0; i<num_mods; i++) {
2325 struct tldap_mod *mod = &mods[i];
2326 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2327 if (!asn1_write_enumerated(state->out, mod->mod_op)) goto err;
2328 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2329 if (!asn1_write_OctetString(state->out, mod->attribute,
2330 strlen(mod->attribute))) goto err;
2331 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2332 for (j=0; j<mod->num_values; j++) {
2333 if (!asn1_write_OctetString(state->out,
2334 mod->values[j].data,
2335 mod->values[j].length)) goto err;
2337 if (!asn1_pop_tag(state->out)) goto err;
2338 if (!asn1_pop_tag(state->out)) goto err;
2339 if (!asn1_pop_tag(state->out)) goto err;
2342 if (!asn1_pop_tag(state->out)) goto err;
2343 if (!asn1_pop_tag(state->out)) goto err;
2345 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2346 sctrls, num_sctrls);
2347 if (tevent_req_nomem(subreq, req)) {
2348 return tevent_req_post(req, ev);
2350 tevent_req_set_callback(subreq, tldap_modify_done, req);
2355 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2356 return tevent_req_post(req, ev);
2359 static void tldap_modify_done(struct tevent_req *subreq)
2361 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2364 TLDAPRC tldap_modify_recv(struct tevent_req *req)
2366 return tldap_simple_recv(req);
2369 TLDAPRC tldap_modify(struct tldap_context *ld, const char *dn,
2370 struct tldap_mod *mods, int num_mods,
2371 struct tldap_control *sctrls, int num_sctrls,
2372 struct tldap_control *cctrls, int num_cctrls)
2374 TALLOC_CTX *frame = talloc_stackframe();
2375 struct tevent_context *ev;
2376 struct tevent_req *req;
2377 TLDAPRC rc = TLDAP_NO_MEMORY;
2379 ev = samba_tevent_context_init(frame);
2383 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2384 sctrls, num_sctrls, cctrls, num_cctrls);
2388 if (!tevent_req_poll(req, ev)) {
2389 rc = TLDAP_OPERATIONS_ERROR;
2392 rc = tldap_modify_recv(req);
2393 tldap_save_msg(ld, req);
2399 static void tldap_delete_done(struct tevent_req *subreq);
2401 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2402 struct tevent_context *ev,
2403 struct tldap_context *ld,
2405 struct tldap_control *sctrls,
2407 struct tldap_control *cctrls,
2410 struct tevent_req *req, *subreq;
2411 struct tldap_req_state *state;
2413 req = tldap_req_create(mem_ctx, ld, &state);
2418 if (!asn1_push_tag(state->out, TLDAP_REQ_DELETE)) goto err;
2419 if (!asn1_write(state->out, dn, strlen(dn))) goto err;
2420 if (!asn1_pop_tag(state->out)) goto err;
2422 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2423 sctrls, num_sctrls);
2424 if (tevent_req_nomem(subreq, req)) {
2425 return tevent_req_post(req, ev);
2427 tevent_req_set_callback(subreq, tldap_delete_done, req);
2432 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2433 return tevent_req_post(req, ev);
2436 static void tldap_delete_done(struct tevent_req *subreq)
2438 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2441 TLDAPRC tldap_delete_recv(struct tevent_req *req)
2443 return tldap_simple_recv(req);
2446 TLDAPRC tldap_delete(struct tldap_context *ld, const char *dn,
2447 struct tldap_control *sctrls, int num_sctrls,
2448 struct tldap_control *cctrls, int num_cctrls)
2450 TALLOC_CTX *frame = talloc_stackframe();
2451 struct tevent_context *ev;
2452 struct tevent_req *req;
2453 TLDAPRC rc = TLDAP_NO_MEMORY;
2455 ev = samba_tevent_context_init(frame);
2459 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2460 cctrls, num_cctrls);
2464 if (!tevent_req_poll(req, ev)) {
2465 rc = TLDAP_OPERATIONS_ERROR;
2468 rc = tldap_delete_recv(req);
2469 tldap_save_msg(ld, req);
2475 int tldap_msg_id(const struct tldap_message *msg)
2480 int tldap_msg_type(const struct tldap_message *msg)
2485 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2490 return msg->res_matcheddn;
2493 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2498 return msg->res_diagnosticmessage;
2501 const char *tldap_msg_referral(struct tldap_message *msg)
2506 return msg->res_referral;
2509 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2510 struct tldap_control **sctrls)
2517 *sctrls = msg->res_sctrls;
2518 *num_sctrls = talloc_array_length(msg->res_sctrls);
2521 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2523 return ld->last_msg;
2526 static const struct { TLDAPRC rc; const char *string; } tldaprc_errmap[] =
2530 { TLDAP_OPERATIONS_ERROR,
2531 "TLDAP_OPERATIONS_ERROR" },
2532 { TLDAP_PROTOCOL_ERROR,
2533 "TLDAP_PROTOCOL_ERROR" },
2534 { TLDAP_TIMELIMIT_EXCEEDED,
2535 "TLDAP_TIMELIMIT_EXCEEDED" },
2536 { TLDAP_SIZELIMIT_EXCEEDED,
2537 "TLDAP_SIZELIMIT_EXCEEDED" },
2538 { TLDAP_COMPARE_FALSE,
2539 "TLDAP_COMPARE_FALSE" },
2540 { TLDAP_COMPARE_TRUE,
2541 "TLDAP_COMPARE_TRUE" },
2542 { TLDAP_STRONG_AUTH_NOT_SUPPORTED,
2543 "TLDAP_STRONG_AUTH_NOT_SUPPORTED" },
2544 { TLDAP_STRONG_AUTH_REQUIRED,
2545 "TLDAP_STRONG_AUTH_REQUIRED" },
2548 { TLDAP_ADMINLIMIT_EXCEEDED,
2549 "TLDAP_ADMINLIMIT_EXCEEDED" },
2550 { TLDAP_UNAVAILABLE_CRITICAL_EXTENSION,
2551 "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION" },
2552 { TLDAP_CONFIDENTIALITY_REQUIRED,
2553 "TLDAP_CONFIDENTIALITY_REQUIRED" },
2554 { TLDAP_SASL_BIND_IN_PROGRESS,
2555 "TLDAP_SASL_BIND_IN_PROGRESS" },
2556 { TLDAP_NO_SUCH_ATTRIBUTE,
2557 "TLDAP_NO_SUCH_ATTRIBUTE" },
2558 { TLDAP_UNDEFINED_TYPE,
2559 "TLDAP_UNDEFINED_TYPE" },
2560 { TLDAP_INAPPROPRIATE_MATCHING,
2561 "TLDAP_INAPPROPRIATE_MATCHING" },
2562 { TLDAP_CONSTRAINT_VIOLATION,
2563 "TLDAP_CONSTRAINT_VIOLATION" },
2564 { TLDAP_TYPE_OR_VALUE_EXISTS,
2565 "TLDAP_TYPE_OR_VALUE_EXISTS" },
2566 { TLDAP_INVALID_SYNTAX,
2567 "TLDAP_INVALID_SYNTAX" },
2568 { TLDAP_NO_SUCH_OBJECT,
2569 "TLDAP_NO_SUCH_OBJECT" },
2570 { TLDAP_ALIAS_PROBLEM,
2571 "TLDAP_ALIAS_PROBLEM" },
2572 { TLDAP_INVALID_DN_SYNTAX,
2573 "TLDAP_INVALID_DN_SYNTAX" },
2576 { TLDAP_ALIAS_DEREF_PROBLEM,
2577 "TLDAP_ALIAS_DEREF_PROBLEM" },
2578 { TLDAP_INAPPROPRIATE_AUTH,
2579 "TLDAP_INAPPROPRIATE_AUTH" },
2580 { TLDAP_INVALID_CREDENTIALS,
2581 "TLDAP_INVALID_CREDENTIALS" },
2582 { TLDAP_INSUFFICIENT_ACCESS,
2583 "TLDAP_INSUFFICIENT_ACCESS" },
2586 { TLDAP_UNAVAILABLE,
2587 "TLDAP_UNAVAILABLE" },
2588 { TLDAP_UNWILLING_TO_PERFORM,
2589 "TLDAP_UNWILLING_TO_PERFORM" },
2590 { TLDAP_LOOP_DETECT,
2591 "TLDAP_LOOP_DETECT" },
2592 { TLDAP_NAMING_VIOLATION,
2593 "TLDAP_NAMING_VIOLATION" },
2594 { TLDAP_OBJECT_CLASS_VIOLATION,
2595 "TLDAP_OBJECT_CLASS_VIOLATION" },
2596 { TLDAP_NOT_ALLOWED_ON_NONLEAF,
2597 "TLDAP_NOT_ALLOWED_ON_NONLEAF" },
2598 { TLDAP_NOT_ALLOWED_ON_RDN,
2599 "TLDAP_NOT_ALLOWED_ON_RDN" },
2600 { TLDAP_ALREADY_EXISTS,
2601 "TLDAP_ALREADY_EXISTS" },
2602 { TLDAP_NO_OBJECT_CLASS_MODS,
2603 "TLDAP_NO_OBJECT_CLASS_MODS" },
2604 { TLDAP_RESULTS_TOO_LARGE,
2605 "TLDAP_RESULTS_TOO_LARGE" },
2606 { TLDAP_AFFECTS_MULTIPLE_DSAS,
2607 "TLDAP_AFFECTS_MULTIPLE_DSAS" },
2610 { TLDAP_SERVER_DOWN,
2611 "TLDAP_SERVER_DOWN" },
2612 { TLDAP_LOCAL_ERROR,
2613 "TLDAP_LOCAL_ERROR" },
2614 { TLDAP_ENCODING_ERROR,
2615 "TLDAP_ENCODING_ERROR" },
2616 { TLDAP_DECODING_ERROR,
2617 "TLDAP_DECODING_ERROR" },
2620 { TLDAP_AUTH_UNKNOWN,
2621 "TLDAP_AUTH_UNKNOWN" },
2622 { TLDAP_FILTER_ERROR,
2623 "TLDAP_FILTER_ERROR" },
2624 { TLDAP_USER_CANCELLED,
2625 "TLDAP_USER_CANCELLED" },
2626 { TLDAP_PARAM_ERROR,
2627 "TLDAP_PARAM_ERROR" },
2629 "TLDAP_NO_MEMORY" },
2630 { TLDAP_CONNECT_ERROR,
2631 "TLDAP_CONNECT_ERROR" },
2632 { TLDAP_NOT_SUPPORTED,
2633 "TLDAP_NOT_SUPPORTED" },
2634 { TLDAP_CONTROL_NOT_FOUND,
2635 "TLDAP_CONTROL_NOT_FOUND" },
2636 { TLDAP_NO_RESULTS_RETURNED,
2637 "TLDAP_NO_RESULTS_RETURNED" },
2638 { TLDAP_MORE_RESULTS_TO_RETURN,
2639 "TLDAP_MORE_RESULTS_TO_RETURN" },
2640 { TLDAP_CLIENT_LOOP,
2641 "TLDAP_CLIENT_LOOP" },
2642 { TLDAP_REFERRAL_LIMIT_EXCEEDED,
2643 "TLDAP_REFERRAL_LIMIT_EXCEEDED" },
2646 const char *tldap_rc2string(TLDAPRC rc)
2650 for (i=0; i<ARRAY_SIZE(tldaprc_errmap); i++) {
2651 if (TLDAP_RC_EQUAL(rc, tldaprc_errmap[i].rc)) {
2652 return tldaprc_errmap[i].string;
2656 return "Unknown LDAP Error";