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;
87 struct tevent_queue *outgoing;
88 struct tevent_req **pending;
89 struct tevent_req *read_req;
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)
194 if (ld->conn == NULL) {
198 ret = tstream_pending_bytes(ld->conn);
206 static size_t tldap_pending_reqs(struct tldap_context *ld)
208 return talloc_array_length(ld->pending);
211 struct tstream_context *tldap_get_tstream(struct tldap_context *ld)
216 void tldap_set_tstream(struct tldap_context *ld,
217 struct tstream_context *stream)
222 static struct tldap_ctx_attribute *tldap_context_findattr(
223 struct tldap_context *ld, const char *name)
227 num_attrs = talloc_array_length(ld->ctx_attrs);
229 for (i=0; i<num_attrs; i++) {
230 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
231 return &ld->ctx_attrs[i];
237 bool tldap_context_setattr(struct tldap_context *ld,
238 const char *name, const void *_pptr)
240 struct tldap_ctx_attribute *tmp, *attr;
243 void **pptr = (void **)discard_const_p(void,_pptr);
245 attr = tldap_context_findattr(ld, name);
248 * We don't actually delete attrs, we don't expect tons of
249 * attributes being shuffled around.
251 TALLOC_FREE(attr->ptr);
253 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
259 tmpname = talloc_strdup(ld, name);
260 if (tmpname == NULL) {
264 num_attrs = talloc_array_length(ld->ctx_attrs);
266 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
269 TALLOC_FREE(tmpname);
272 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
274 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
276 tmp[num_attrs].ptr = NULL;
283 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
285 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
293 struct read_ldap_state {
298 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
299 static void read_ldap_done(struct tevent_req *subreq);
301 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
302 struct tevent_context *ev,
303 struct tstream_context *conn)
305 struct tevent_req *req, *subreq;
306 struct read_ldap_state *state;
308 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
314 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
316 if (tevent_req_nomem(subreq, req)) {
317 return tevent_req_post(req, ev);
319 tevent_req_set_callback(subreq, read_ldap_done, req);
323 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
325 struct read_ldap_state *state = talloc_get_type_abort(
326 private_data, struct read_ldap_state);
331 /* We've been here, we're done */
336 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
338 if (buf[0] != 0x30) {
343 if ((len & 0x80) == 0) {
348 lensize = (len & 0x7f);
352 /* Please get us the full length */
355 if (buflen > 2 + lensize) {
359 if (buflen != 2 + lensize) {
363 for (i=0; i<lensize; i++) {
364 len = (len << 8) | buf[2+i];
369 static void read_ldap_done(struct tevent_req *subreq)
371 struct tevent_req *req = tevent_req_callback_data(
372 subreq, struct tevent_req);
373 struct read_ldap_state *state = tevent_req_data(
374 req, struct read_ldap_state);
378 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
381 tevent_req_error(req, err);
384 tevent_req_done(req);
387 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
388 uint8_t **pbuf, int *perrno)
390 struct read_ldap_state *state = tevent_req_data(
391 req, struct read_ldap_state);
393 if (tevent_req_is_unix_error(req, perrno)) {
396 *pbuf = talloc_move(mem_ctx, &state->buf);
397 return talloc_get_size(*pbuf);
400 struct tldap_msg_state {
401 struct tldap_context *ld;
402 struct tevent_context *ev;
406 struct asn1_data *data;
410 static bool tldap_push_controls(struct asn1_data *data,
411 struct tldap_control *sctrls,
416 if ((sctrls == NULL) || (num_sctrls == 0)) {
420 if (!asn1_push_tag(data, ASN1_CONTEXT(0))) return false;
422 for (i=0; i<num_sctrls; i++) {
423 struct tldap_control *c = &sctrls[i];
424 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
425 if (!asn1_write_OctetString(data, c->oid, strlen(c->oid))) return false;
427 if (!asn1_write_BOOLEAN(data, true)) return false;
429 if (c->value.data != NULL) {
430 if (!asn1_write_OctetString(data, c->value.data,
431 c->value.length)) return false;
433 if (!asn1_pop_tag(data)) return false; /* ASN1_SEQUENCE(0) */
436 return asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
439 #define tldap_context_disconnect(ld, status) \
440 _tldap_context_disconnect(ld, status, __location__)
442 static void _tldap_context_disconnect(struct tldap_context *ld,
444 const char *location)
446 if (ld->conn == NULL) {
448 * We don't need to tldap_debug() on
449 * a potential 2nd run.
451 * The rest of the function would just
452 * be a noop for the 2nd run anyway.
457 tldap_debug(ld, TLDAP_DEBUG_WARNING,
458 "tldap_context_disconnect: %s at %s\n",
459 tldap_rc2string(status),
461 tevent_queue_stop(ld->outgoing);
462 TALLOC_FREE(ld->read_req);
463 TALLOC_FREE(ld->conn);
465 while (talloc_array_length(ld->pending) > 0) {
466 struct tevent_req *req = NULL;
467 struct tldap_msg_state *state = NULL;
469 req = ld->pending[0];
470 state = tevent_req_data(req, struct tldap_msg_state);
471 tevent_req_defer_callback(req, state->ev);
472 tevent_req_ldap_error(req, status);
476 static void tldap_msg_sent(struct tevent_req *subreq);
477 static void tldap_msg_received(struct tevent_req *subreq);
479 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
480 struct tevent_context *ev,
481 struct tldap_context *ld,
482 int id, struct asn1_data *data,
483 struct tldap_control *sctrls,
486 struct tevent_req *req, *subreq;
487 struct tldap_msg_state *state;
491 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
494 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
502 ok = tldap_connection_ok(ld);
504 tevent_req_ldap_error(req, TLDAP_SERVER_DOWN);
505 return tevent_req_post(req, ev);
508 if (!tldap_push_controls(data, sctrls, num_sctrls)) {
509 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
510 return tevent_req_post(req, ev);
514 if (!asn1_pop_tag(data)) {
515 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
516 return tevent_req_post(req, ev);
519 if (!asn1_blob(data, &blob)) {
520 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
521 return tevent_req_post(req, ev);
524 state->iov.iov_base = (void *)blob.data;
525 state->iov.iov_len = blob.length;
527 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
529 if (tevent_req_nomem(subreq, req)) {
530 return tevent_req_post(req, ev);
532 tevent_req_set_callback(subreq, tldap_msg_sent, req);
536 static void tldap_msg_unset_pending(struct tevent_req *req)
538 struct tldap_msg_state *state = tevent_req_data(
539 req, struct tldap_msg_state);
540 struct tldap_context *ld = state->ld;
541 int num_pending = tldap_pending_reqs(ld);
544 tevent_req_set_cleanup_fn(req, NULL);
546 for (i=0; i<num_pending; i++) {
547 if (req == ld->pending[i]) {
551 if (i == num_pending) {
553 * Something's seriously broken. Just returning here is the
554 * right thing nevertheless, the point of this routine is to
555 * remove ourselves from cli->pending.
560 if (num_pending == 1) {
561 TALLOC_FREE(ld->pending);
566 * Remove ourselves from the cli->pending array
568 if (num_pending > 1) {
569 ld->pending[i] = ld->pending[num_pending-1];
573 * No NULL check here, we're shrinking by sizeof(void *), and
574 * talloc_realloc just adjusts the size for this.
576 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
580 static void tldap_msg_cleanup(struct tevent_req *req,
581 enum tevent_req_state req_state)
583 tldap_msg_unset_pending(req);
586 static bool tldap_msg_set_pending(struct tevent_req *req)
588 struct tldap_msg_state *state = tevent_req_data(
589 req, struct tldap_msg_state);
590 struct tldap_context *ld;
591 struct tevent_req **pending;
595 num_pending = tldap_pending_reqs(ld);
597 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
599 if (pending == NULL) {
602 pending[num_pending] = req;
603 ld->pending = pending;
604 tevent_req_set_cleanup_fn(req, tldap_msg_cleanup);
606 if (ld->read_req != NULL) {
611 * We're the first one, add the read_ldap request that waits for the
612 * answer from the server
614 ld->read_req = read_ldap_send(ld->pending, state->ev, ld->conn);
615 if (ld->read_req == NULL) {
616 tldap_msg_unset_pending(req);
619 tevent_req_set_callback(ld->read_req, tldap_msg_received, ld);
623 static void tldap_msg_sent(struct tevent_req *subreq)
625 struct tevent_req *req = tevent_req_callback_data(
626 subreq, struct tevent_req);
627 struct tldap_msg_state *state = tevent_req_data(
628 req, struct tldap_msg_state);
632 nwritten = tstream_writev_queue_recv(subreq, &err);
634 if (nwritten == -1) {
635 tldap_context_disconnect(state->ld, TLDAP_SERVER_DOWN);
636 tevent_req_ldap_error(req, TLDAP_SERVER_DOWN);
640 if (!tldap_msg_set_pending(req)) {
646 static int tldap_msg_msgid(struct tevent_req *req)
648 struct tldap_msg_state *state = tevent_req_data(
649 req, struct tldap_msg_state);
654 static void tldap_msg_received(struct tevent_req *subreq)
656 struct tldap_context *ld = tevent_req_callback_data(
657 subreq, struct tldap_context);
658 struct tevent_req *req;
659 struct tldap_msg_state *state;
660 struct asn1_data *data;
665 TLDAPRC status = TLDAP_PROTOCOL_ERROR;
670 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
673 if (received == -1) {
674 status = TLDAP_SERVER_DOWN;
678 data = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH);
681 * We have to disconnect all, we can't tell which of
682 * the requests this reply is for.
684 status = TLDAP_NO_MEMORY;
687 asn1_load_nocopy(data, inbuf, received);
690 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
691 ok &= asn1_read_Integer(data, &id);
692 ok &= asn1_peek_uint8(data, &type);
695 status = TLDAP_PROTOCOL_ERROR;
699 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
700 "type %d\n", id, (int)type);
702 num_pending = talloc_array_length(ld->pending);
704 for (i=0; i<num_pending; i++) {
705 if (id == tldap_msg_msgid(ld->pending[i])) {
709 if (i == num_pending) {
710 /* Dump unexpected reply */
711 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
712 "No request pending for msg %d\n", id);
718 req = ld->pending[i];
719 state = tevent_req_data(req, struct tldap_msg_state);
721 state->inbuf = talloc_move(state, &inbuf);
722 state->data = talloc_move(state, &data);
724 tldap_msg_unset_pending(req);
725 num_pending = talloc_array_length(ld->pending);
727 tevent_req_defer_callback(req, state->ev);
728 tevent_req_done(req);
731 if (num_pending == 0) {
735 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
736 ld->read_req = read_ldap_send(ld->pending, state->ev, ld->conn);
737 if (ld->read_req == NULL) {
738 status = TLDAP_NO_MEMORY;
741 tevent_req_set_callback(ld->read_req, tldap_msg_received, ld);
745 tldap_context_disconnect(ld, status);
748 static TLDAPRC tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
749 struct tldap_message **pmsg)
751 struct tldap_msg_state *state = tevent_req_data(
752 req, struct tldap_msg_state);
753 struct tldap_message *msg;
757 if (tevent_req_is_ldap_error(req, &err)) {
761 if (!asn1_peek_uint8(state->data, &msgtype)) {
762 return TLDAP_PROTOCOL_ERROR;
766 return TLDAP_SUCCESS;
769 msg = talloc_zero(mem_ctx, struct tldap_message);
771 return TLDAP_NO_MEMORY;
775 msg->inbuf = talloc_move(msg, &state->inbuf);
776 msg->data = talloc_move(msg, &state->data);
780 return TLDAP_SUCCESS;
783 struct tldap_req_state {
785 struct asn1_data *out;
786 struct tldap_message *result;
789 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
790 struct tldap_context *ld,
791 struct tldap_req_state **pstate)
793 struct tevent_req *req;
794 struct tldap_req_state *state;
796 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
800 state->out = asn1_init(state, ASN1_MAX_TREE_DEPTH);
801 if (state->out == NULL) {
804 state->id = tldap_next_msgid(ld);
806 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
807 if (!asn1_write_Integer(state->out, state->id)) goto err;
818 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
820 struct tldap_req_state *state = tevent_req_data(
821 req, struct tldap_req_state);
823 TALLOC_FREE(ld->last_msg);
824 ld->last_msg = talloc_move(ld, &state->result);
827 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
829 char *result = talloc_array(mem_ctx, char, blob.length+1);
831 if (result == NULL) {
835 memcpy(result, blob.data, blob.length);
836 result[blob.length] = '\0';
840 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
841 struct asn1_data *data,
846 if (!asn1_read_OctetString(data, mem_ctx, &string))
849 result = blob2string_talloc(mem_ctx, string);
851 data_blob_free(&string);
853 if (result == NULL) {
860 static bool tldap_decode_controls(struct tldap_req_state *state);
862 static bool tldap_decode_response(struct tldap_req_state *state)
864 struct asn1_data *data = state->result->data;
865 struct tldap_message *msg = state->result;
869 ok &= asn1_read_enumerated(data, &rc);
871 msg->lderr = TLDAP_RC(rc);
874 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
875 ok &= asn1_read_OctetString_talloc(msg, data,
876 &msg->res_diagnosticmessage);
878 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
879 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
880 ok &= asn1_read_OctetString_talloc(msg, data,
882 ok &= asn1_end_tag(data);
884 msg->res_referral = NULL;
890 static void tldap_sasl_bind_done(struct tevent_req *subreq);
892 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
893 struct tevent_context *ev,
894 struct tldap_context *ld,
896 const char *mechanism,
898 struct tldap_control *sctrls,
900 struct tldap_control *cctrls,
903 struct tevent_req *req, *subreq;
904 struct tldap_req_state *state;
906 req = tldap_req_create(mem_ctx, ld, &state);
915 if (!asn1_push_tag(state->out, TLDAP_REQ_BIND)) goto err;
916 if (!asn1_write_Integer(state->out, ld->ld_version)) goto err;
917 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
919 if (mechanism == NULL) {
920 if (!asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0))) goto err;
921 if (!asn1_write(state->out, creds->data, creds->length)) goto err;
922 if (!asn1_pop_tag(state->out)) goto err;
924 if (!asn1_push_tag(state->out, ASN1_CONTEXT(3))) goto err;
925 if (!asn1_write_OctetString(state->out, mechanism,
926 strlen(mechanism))) goto err;
927 if ((creds != NULL) && (creds->data != NULL)) {
928 if (!asn1_write_OctetString(state->out, creds->data,
929 creds->length)) goto err;
931 if (!asn1_pop_tag(state->out)) goto err;
934 if (!asn1_pop_tag(state->out)) goto err;
936 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
938 if (tevent_req_nomem(subreq, req)) {
939 return tevent_req_post(req, ev);
941 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
946 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
947 return tevent_req_post(req, ev);
950 static void tldap_sasl_bind_done(struct tevent_req *subreq)
952 struct tevent_req *req = tevent_req_callback_data(
953 subreq, struct tevent_req);
954 struct tldap_req_state *state = tevent_req_data(
955 req, struct tldap_req_state);
959 rc = tldap_msg_recv(subreq, state, &state->result);
961 if (tevent_req_ldap_error(req, rc)) {
964 if (state->result->type != TLDAP_RES_BIND) {
965 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
969 ok = asn1_start_tag(state->result->data, TLDAP_RES_BIND);
970 ok &= tldap_decode_response(state);
972 if (asn1_peek_tag(state->result->data, ASN1_CONTEXT_SIMPLE(7))) {
975 ok &= asn1_start_tag(state->result->data,
976 ASN1_CONTEXT_SIMPLE(7));
981 len = asn1_tag_remaining(state->result->data);
986 state->result->res_serverSaslCreds =
987 data_blob_talloc(state->result, NULL, len);
988 if (state->result->res_serverSaslCreds.data == NULL) {
992 ok = asn1_read(state->result->data,
993 state->result->res_serverSaslCreds.data,
994 state->result->res_serverSaslCreds.length);
996 ok &= asn1_end_tag(state->result->data);
999 ok &= asn1_end_tag(state->result->data);
1005 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr) &&
1006 !TLDAP_RC_EQUAL(state->result->lderr,
1007 TLDAP_SASL_BIND_IN_PROGRESS)) {
1008 tevent_req_ldap_error(req, state->result->lderr);
1011 tevent_req_done(req);
1015 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
1019 TLDAPRC tldap_sasl_bind_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1020 DATA_BLOB *serverSaslCreds)
1022 struct tldap_req_state *state = tevent_req_data(
1023 req, struct tldap_req_state);
1026 if (tevent_req_is_ldap_error(req, &rc)) {
1030 if (serverSaslCreds != NULL) {
1031 serverSaslCreds->data = talloc_move(
1032 mem_ctx, &state->result->res_serverSaslCreds.data);
1033 serverSaslCreds->length =
1034 state->result->res_serverSaslCreds.length;
1037 return state->result->lderr;
1040 TLDAPRC tldap_sasl_bind(struct tldap_context *ld,
1042 const char *mechanism,
1044 struct tldap_control *sctrls,
1046 struct tldap_control *cctrls,
1048 TALLOC_CTX *mem_ctx,
1049 DATA_BLOB *serverSaslCreds)
1051 TALLOC_CTX *frame = talloc_stackframe();
1052 struct tevent_context *ev;
1053 struct tevent_req *req;
1054 TLDAPRC rc = TLDAP_NO_MEMORY;
1056 ev = samba_tevent_context_init(frame);
1060 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
1061 sctrls, num_sctrls, cctrls, num_cctrls);
1065 if (!tevent_req_poll(req, ev)) {
1066 rc = TLDAP_OPERATIONS_ERROR;
1069 rc = tldap_sasl_bind_recv(req, mem_ctx, serverSaslCreds);
1070 tldap_save_msg(ld, req);
1076 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
1077 struct tevent_context *ev,
1078 struct tldap_context *ld,
1084 if (passwd != NULL) {
1085 cred.data = discard_const_p(uint8_t, passwd);
1086 cred.length = strlen(passwd);
1088 cred.data = discard_const_p(uint8_t, "");
1091 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
1095 TLDAPRC tldap_simple_bind_recv(struct tevent_req *req)
1097 return tldap_sasl_bind_recv(req, NULL, NULL);
1100 TLDAPRC tldap_simple_bind(struct tldap_context *ld, const char *dn,
1105 if (passwd != NULL) {
1106 cred.data = discard_const_p(uint8_t, passwd);
1107 cred.length = strlen(passwd);
1109 cred.data = discard_const_p(uint8_t, "");
1112 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0,
1116 /*****************************************************************************/
1118 /* can't use isalpha() as only a strict set is valid for LDAP */
1120 static bool tldap_is_alpha(char c)
1122 return (((c >= 'a') && (c <= 'z')) || \
1123 ((c >= 'A') && (c <= 'Z')));
1126 static bool tldap_is_adh(char c)
1128 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
1131 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
1132 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
1133 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
1134 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
1135 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
1136 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
1137 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
1138 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
1139 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
1140 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
1142 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
1143 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
1144 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1147 /* oid's should be numerical only in theory,
1148 * but apparently some broken servers may have alphanum aliases instead.
1149 * Do like openldap libraries and allow alphanum aliases for oids, but
1150 * do not allow Tagging options in that case.
1152 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1154 bool is_oid = false;
1158 /* first char has stricter rules */
1161 } else if (!tldap_is_alpha(*s)) {
1162 /* bad first char */
1166 for (i = 1; i < len; i++) {
1169 if (isdigit(s[i])) {
1182 if (tldap_is_adh(s[i])) {
1189 /* no tagging options */
1196 if ((i + 1) == len) {
1214 /* this function copies the value until the closing parenthesis is found. */
1215 static char *tldap_get_val(TALLOC_CTX *memctx,
1216 const char *value, const char **_s)
1218 const char *s = value;
1220 /* find terminator */
1223 if (s && (*(s - 1) == '\\')) {
1228 if (!s || !(*s == ')')) {
1229 /* malformed filter */
1235 return talloc_strndup(memctx, value, s - value);
1238 static int tldap_hex2char(const char *x)
1240 if (isxdigit(x[0]) && isxdigit(x[1])) {
1241 const char h1 = x[0], h2 = x[1];
1244 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1245 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1246 else if (h1 >= '0') c = h1 - (int)'0';
1248 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1249 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1250 else if (h2 >= '0') c += h2 - (int)'0';
1258 static bool tldap_find_first_star(const char *val, const char **star)
1262 for (s = val; *s; s++) {
1265 if (isxdigit(s[1]) && isxdigit(s[2])) {
1269 /* not hex based escape, check older syntax */
1278 /* invalid escape sequence */
1283 /* end of val, nothing found */
1293 /* string ended without closing parenthesis, filter is malformed */
1297 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1302 for (i = 0,p = 0; i < *val_len; i++) {
1308 /* these must be escaped */
1312 if (!value[i + 1]) {
1318 /* LDAPv3 escaped */
1319 c = tldap_hex2char(&value[i]);
1320 if (c >= 0 && c < 256) {
1327 /* LDAPv2 escaped */
1333 value[p] = value[i];
1344 value[p] = value[i];
1353 static bool tldap_push_filter_basic(struct tldap_context *ld,
1354 struct asn1_data *data,
1356 static bool tldap_push_filter_substring(struct tldap_context *ld,
1357 struct asn1_data *data,
1360 static bool tldap_push_filter_int(struct tldap_context *ld,
1361 struct asn1_data *data,
1364 const char *s = *_s;
1368 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1369 "Incomplete or malformed filter\n");
1374 /* we are right after a parenthesis,
1375 * find out what op we have at hand */
1378 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1379 if (!asn1_push_tag(data, TLDAP_FILTER_AND)) return false;
1384 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1385 if (!asn1_push_tag(data, TLDAP_FILTER_OR)) return false;
1390 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1391 if (!asn1_push_tag(data, TLDAP_FILTER_NOT)) return false;
1393 ret = tldap_push_filter_int(ld, data, &s);
1397 if (!asn1_pop_tag(data)) return false;
1402 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1403 "Invalid parenthesis '%c'\n", *s);
1407 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1408 "Invalid filter termination\n");
1412 ret = tldap_push_filter_basic(ld, data, &s);
1419 /* only and/or filters get here.
1420 * go through the list of filters */
1423 /* RFC 4526: empty and/or */
1424 if (!asn1_pop_tag(data)) return false;
1429 ret = tldap_push_filter_int(ld, data, &s);
1435 /* end of list, return */
1436 if (!asn1_pop_tag(data)) return false;
1443 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1444 "Incomplete or malformed filter\n");
1449 if (asn1_has_error(data)) {
1458 static bool tldap_push_filter_basic(struct tldap_context *ld,
1459 struct asn1_data *data,
1462 TALLOC_CTX *tmpctx = talloc_tos();
1463 const char *s = *_s;
1471 size_t type_len = 0;
1474 bool write_octect = true;
1477 eq = strchr(s, '=');
1479 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1480 "Invalid filter, missing equal sign\n");
1489 if (!asn1_push_tag(data, TLDAP_FILTER_LE)) return false;
1493 if (!asn1_push_tag(data, TLDAP_FILTER_GE)) return false;
1497 if (!asn1_push_tag(data, TLDAP_FILTER_APX)) return false;
1501 if (!asn1_push_tag(data, TLDAP_FILTER_EXT)) return false;
1502 write_octect = false;
1508 if (*s == ':') { /* [:dn]:rule:= value */
1510 /* malformed filter */
1514 } else { /* type[:dn][:rule]:= value */
1516 dn = strchr(s, ':');
1517 type_len = dn - type;
1518 if (dn == e) { /* type:= value */
1525 rule = strchr(dn, ':');
1529 if ((rule == dn + 1) || rule + 1 == e) {
1530 /* malformed filter, contains "::" */
1534 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1539 /* malformed filter. With two
1540 * optionals, the first must be "dn"
1553 if (!type && !dn && !rule) {
1554 /* malformed filter, there must be at least one */
1559 MatchingRuleAssertion ::= SEQUENCE {
1560 matchingRule [1] MatchingRuleID OPTIONAL,
1561 type [2] AttributeDescription OPTIONAL,
1562 matchValue [3] AssertionValue,
1563 dnAttributes [4] BOOLEAN DEFAULT FALSE
1567 /* check and add rule */
1569 ret = tldap_is_attrdesc(rule, e - rule, true);
1573 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1))) return false;
1574 if (!asn1_write(data, rule, e - rule)) return false;
1575 if (!asn1_pop_tag(data)) return false;
1578 /* check and add type */
1580 ret = tldap_is_attrdesc(type, type_len, false);
1584 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2))) return false;
1585 if (!asn1_write(data, type, type_len)) return false;
1586 if (!asn1_pop_tag(data)) return false;
1589 uval = tldap_get_val(tmpctx, val, _s);
1593 uval_len = *_s - val;
1594 ret = tldap_unescape_inplace(uval, &uval_len);
1599 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3))) return false;
1600 if (!asn1_write(data, uval, uval_len)) return false;
1601 if (!asn1_pop_tag(data)) return false;
1603 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4))) return false;
1604 if (!asn1_write_uint8(data, dn?1:0)) return false;
1605 if (!asn1_pop_tag(data)) return false;
1611 ret = tldap_is_attrdesc(s, e - s, false);
1616 if (strncmp(val, "*)", 2) == 0) {
1618 if (!asn1_push_tag(data, TLDAP_FILTER_PRES)) return false;
1619 if (!asn1_write(data, s, e - s)) return false;
1621 write_octect = false;
1625 ret = tldap_find_first_star(val, &star);
1631 if (!asn1_push_tag(data, TLDAP_FILTER_SUB)) return false;
1632 if (!asn1_write_OctetString(data, s, e - s)) return false;
1633 ret = tldap_push_filter_substring(ld, data, val, &s);
1638 write_octect = false;
1642 /* if nothing else, then it is just equality */
1643 if (!asn1_push_tag(data, TLDAP_FILTER_EQ)) return false;
1644 write_octect = true;
1649 uval = tldap_get_val(tmpctx, val, _s);
1653 uval_len = *_s - val;
1654 ret = tldap_unescape_inplace(uval, &uval_len);
1659 if (!asn1_write_OctetString(data, s, e - s)) return false;
1660 if (!asn1_write_OctetString(data, uval, uval_len)) return false;
1663 if (asn1_has_error(data)) {
1666 return asn1_pop_tag(data);
1669 static bool tldap_push_filter_substring(struct tldap_context *ld,
1670 struct asn1_data *data,
1674 TALLOC_CTX *tmpctx = talloc_tos();
1675 bool initial = true;
1682 SubstringFilter ::= SEQUENCE {
1683 type AttributeDescription,
1684 -- at least one must be present
1685 substrings SEQUENCE OF CHOICE {
1686 initial [0] LDAPString,
1688 final [2] LDAPString } }
1690 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
1693 ret = tldap_find_first_star(val, &star);
1697 chunk_len = star - val;
1701 if (!initial && chunk_len == 0) {
1702 /* found '**', which is illegal */
1718 if (initial && chunk_len == 0) {
1724 chunk = talloc_strndup(tmpctx, val, chunk_len);
1728 ret = tldap_unescape_inplace(chunk, &chunk_len);
1735 if (!asn1_push_tag(data, TLDAP_SUB_INI)) return false;
1738 if (!asn1_push_tag(data, TLDAP_SUB_ANY)) return false;
1742 if (!asn1_push_tag(data, TLDAP_SUB_FIN)) return false;
1748 if (!asn1_write(data, chunk, chunk_len)) return false;
1749 if (!asn1_pop_tag(data)) return false;
1753 } while (*star == '*');
1757 /* end of sequence */
1758 return asn1_pop_tag(data);
1761 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1762 * around parenthesis, we do not allow any spaces (except in values of
1763 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1764 * leading or trailing spaces where allowed.
1766 static bool tldap_push_filter(struct tldap_context *ld,
1767 struct asn1_data *data,
1770 const char *s = filter;
1773 ret = tldap_push_filter_int(ld, data, &s);
1775 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1776 "Incomplete or malformed filter\n");
1782 /*****************************************************************************/
1784 static void tldap_search_done(struct tevent_req *subreq);
1786 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1787 struct tevent_context *ev,
1788 struct tldap_context *ld,
1789 const char *base, int scope,
1794 struct tldap_control *sctrls,
1796 struct tldap_control *cctrls,
1802 struct tevent_req *req, *subreq;
1803 struct tldap_req_state *state;
1806 req = tldap_req_create(mem_ctx, ld, &state);
1811 if (!asn1_push_tag(state->out, TLDAP_REQ_SEARCH)) goto encoding_error;
1812 if (!asn1_write_OctetString(state->out, base, strlen(base))) goto encoding_error;
1813 if (!asn1_write_enumerated(state->out, scope)) goto encoding_error;
1814 if (!asn1_write_enumerated(state->out, deref)) goto encoding_error;
1815 if (!asn1_write_Integer(state->out, sizelimit)) goto encoding_error;
1816 if (!asn1_write_Integer(state->out, timelimit)) goto encoding_error;
1817 if (!asn1_write_BOOLEAN(state->out, attrsonly)) goto encoding_error;
1819 if (!tldap_push_filter(ld, state->out, filter)) {
1820 goto encoding_error;
1823 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto encoding_error;
1824 for (i=0; i<num_attrs; i++) {
1825 if (!asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]))) goto encoding_error;
1827 if (!asn1_pop_tag(state->out)) goto encoding_error;
1828 if (!asn1_pop_tag(state->out)) goto encoding_error;
1830 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1831 sctrls, num_sctrls);
1832 if (tevent_req_nomem(subreq, req)) {
1833 return tevent_req_post(req, ev);
1835 tevent_req_set_callback(subreq, tldap_search_done, req);
1839 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
1840 return tevent_req_post(req, ev);
1843 static void tldap_search_done(struct tevent_req *subreq)
1845 struct tevent_req *req = tevent_req_callback_data(
1846 subreq, struct tevent_req);
1847 struct tldap_req_state *state = tevent_req_data(
1848 req, struct tldap_req_state);
1851 rc = tldap_msg_recv(subreq, state, &state->result);
1852 if (tevent_req_ldap_error(req, rc)) {
1855 switch (state->result->type) {
1856 case TLDAP_RES_SEARCH_ENTRY:
1857 case TLDAP_RES_SEARCH_REFERENCE:
1858 if (!tldap_msg_set_pending(subreq)) {
1859 tevent_req_oom(req);
1862 tevent_req_notify_callback(req);
1864 case TLDAP_RES_SEARCH_RESULT:
1865 TALLOC_FREE(subreq);
1866 if (!asn1_start_tag(state->result->data,
1867 state->result->type) ||
1868 !tldap_decode_response(state) ||
1869 !asn1_end_tag(state->result->data) ||
1870 !tldap_decode_controls(state)) {
1871 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
1874 tevent_req_done(req);
1877 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
1882 TLDAPRC tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1883 struct tldap_message **pmsg)
1885 struct tldap_req_state *state = tevent_req_data(
1886 req, struct tldap_req_state);
1889 if (!tevent_req_is_in_progress(req)
1890 && tevent_req_is_ldap_error(req, &rc)) {
1894 if (tevent_req_is_in_progress(req)) {
1895 switch (state->result->type) {
1896 case TLDAP_RES_SEARCH_ENTRY:
1897 case TLDAP_RES_SEARCH_REFERENCE:
1900 return TLDAP_OPERATIONS_ERROR;
1904 *pmsg = talloc_move(mem_ctx, &state->result);
1905 return TLDAP_SUCCESS;
1908 struct tldap_search_all_state {
1909 struct tldap_message **msgs;
1910 struct tldap_message *result;
1913 static void tldap_search_all_done(struct tevent_req *subreq);
1915 struct tevent_req *tldap_search_all_send(
1916 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1917 struct tldap_context *ld, const char *base, int scope,
1918 const char *filter, const char **attrs, int num_attrs, int attrsonly,
1919 struct tldap_control *sctrls, int num_sctrls,
1920 struct tldap_control *cctrls, int num_cctrls,
1921 int timelimit, int sizelimit, int deref)
1923 struct tevent_req *req, *subreq;
1924 struct tldap_search_all_state *state;
1926 req = tevent_req_create(mem_ctx, &state,
1927 struct tldap_search_all_state);
1932 subreq = tldap_search_send(state, ev, ld, base, scope, filter,
1933 attrs, num_attrs, attrsonly,
1934 sctrls, num_sctrls, cctrls, num_cctrls,
1935 timelimit, sizelimit, deref);
1936 if (tevent_req_nomem(subreq, req)) {
1937 return tevent_req_post(req, ev);
1939 tevent_req_set_callback(subreq, tldap_search_all_done, req);
1943 static void tldap_search_all_done(struct tevent_req *subreq)
1945 struct tevent_req *req = tevent_req_callback_data(
1946 subreq, struct tevent_req);
1947 struct tldap_search_all_state *state = tevent_req_data(
1948 req, struct tldap_search_all_state);
1949 struct tldap_message *msg, **tmp;
1954 rc = tldap_search_recv(subreq, state, &msg);
1955 /* No TALLOC_FREE(subreq), this is multi-step */
1956 if (tevent_req_ldap_error(req, rc)) {
1960 msgtype = tldap_msg_type(msg);
1961 if (msgtype == TLDAP_RES_SEARCH_RESULT) {
1962 state->result = msg;
1963 tevent_req_done(req);
1967 num_msgs = talloc_array_length(state->msgs);
1969 tmp = talloc_realloc(state, state->msgs, struct tldap_message *,
1971 if (tevent_req_nomem(tmp, req)) {
1975 state->msgs[num_msgs] = talloc_move(state->msgs, &msg);
1978 TLDAPRC tldap_search_all_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1979 struct tldap_message ***msgs,
1980 struct tldap_message **result)
1982 struct tldap_search_all_state *state = tevent_req_data(
1983 req, struct tldap_search_all_state);
1986 if (tevent_req_is_ldap_error(req, &rc)) {
1991 *msgs = talloc_move(mem_ctx, &state->msgs);
1993 if (result != NULL) {
1994 *result = talloc_move(mem_ctx, &state->result);
1997 return TLDAP_SUCCESS;
2000 TLDAPRC tldap_search(struct tldap_context *ld,
2001 const char *base, int scope, const char *filter,
2002 const char **attrs, int num_attrs, int attrsonly,
2003 struct tldap_control *sctrls, int num_sctrls,
2004 struct tldap_control *cctrls, int num_cctrls,
2005 int timelimit, int sizelimit, int deref,
2006 TALLOC_CTX *mem_ctx, struct tldap_message ***pmsgs)
2009 struct tevent_context *ev;
2010 struct tevent_req *req;
2011 TLDAPRC rc = TLDAP_NO_MEMORY;
2012 struct tldap_message **msgs;
2013 struct tldap_message *result;
2015 if (tldap_pending_reqs(ld)) {
2019 frame = talloc_stackframe();
2021 ev = samba_tevent_context_init(frame);
2025 req = tldap_search_all_send(frame, ev, ld, base, scope, filter,
2026 attrs, num_attrs, attrsonly,
2027 sctrls, num_sctrls, cctrls, num_cctrls,
2028 timelimit, sizelimit, deref);
2032 if (!tevent_req_poll(req, ev)) {
2033 rc = TLDAP_OPERATIONS_ERROR;
2036 rc = tldap_search_all_recv(req, frame, &msgs, &result);
2038 if (!TLDAP_RC_IS_SUCCESS(rc)) {
2042 TALLOC_FREE(ld->last_msg);
2043 ld->last_msg = talloc_move(ld, &result);
2045 if (pmsgs != NULL) {
2046 *pmsgs = talloc_move(mem_ctx, &msgs);
2053 static bool tldap_parse_search_entry(struct tldap_message *msg)
2055 int num_attribs = 0;
2057 if (msg->type != TLDAP_RES_SEARCH_ENTRY) {
2060 if (!asn1_start_tag(msg->data, TLDAP_RES_SEARCH_ENTRY)) {
2066 if (!asn1_read_OctetString_talloc(msg, msg->data, &msg->dn)) return false;
2068 if (msg->dn == NULL) {
2073 * Attributes: We overallocate msg->attribs by one, so that while
2074 * looping over the attributes we can directly parse into the last
2075 * array element. Same for the values in the inner loop.
2078 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
2079 if (msg->attribs == NULL) {
2083 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
2084 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
2085 struct tldap_attribute *attrib;
2088 attrib = &msg->attribs[num_attribs];
2089 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
2090 if (attrib->values == NULL) {
2093 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
2094 if (!asn1_read_OctetString_talloc(msg->attribs, msg->data,
2095 &attrib->name)) return false;
2096 if (!asn1_start_tag(msg->data, ASN1_SET)) return false;
2098 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
2099 if (!asn1_read_OctetString(msg->data, msg,
2100 &attrib->values[num_values])) return false;
2102 attrib->values = talloc_realloc(
2103 msg->attribs, attrib->values, DATA_BLOB,
2105 if (attrib->values == NULL) {
2110 attrib->values = talloc_realloc(msg->attribs, attrib->values,
2111 DATA_BLOB, num_values);
2112 attrib->num_values = num_values;
2114 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SET */
2115 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SEQUENCE(0) */
2116 msg->attribs = talloc_realloc(
2117 msg, msg->attribs, struct tldap_attribute,
2119 if (msg->attribs == NULL) {
2124 msg->attribs = talloc_realloc(
2125 msg, msg->attribs, struct tldap_attribute, num_attribs);
2126 return asn1_end_tag(msg->data);
2129 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
2131 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2138 bool tldap_entry_attributes(struct tldap_message *msg,
2139 struct tldap_attribute **attributes,
2140 int *num_attributes)
2142 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2145 *attributes = msg->attribs;
2146 *num_attributes = talloc_array_length(msg->attribs);
2150 static bool tldap_decode_controls(struct tldap_req_state *state)
2152 struct tldap_message *msg = state->result;
2153 struct asn1_data *data = msg->data;
2154 struct tldap_control *sctrls = NULL;
2155 int num_controls = 0;
2158 msg->res_sctrls = NULL;
2160 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
2164 if (!asn1_start_tag(data, ASN1_CONTEXT(0))) goto out;
2166 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
2167 struct tldap_control *c;
2170 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
2172 if (sctrls == NULL) {
2175 c = &sctrls[num_controls];
2177 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) goto out;
2178 if (!asn1_read_OctetString_talloc(msg, data, &oid)) goto out;
2179 if (asn1_has_error(data) || (oid == NULL)) {
2183 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2184 if (!asn1_read_BOOLEAN(data, &c->critical)) goto out;
2186 c->critical = false;
2188 c->value = data_blob_null;
2189 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2190 !asn1_read_OctetString(data, msg, &c->value)) {
2193 if (!asn1_end_tag(data)) goto out; /* ASN1_SEQUENCE(0) */
2198 if (!asn1_end_tag(data)) goto out; /* ASN1_CONTEXT(0) */
2205 msg->res_sctrls = sctrls;
2207 TALLOC_FREE(sctrls);
2212 static void tldap_simple_done(struct tevent_req *subreq, int type)
2214 struct tevent_req *req = tevent_req_callback_data(
2215 subreq, struct tevent_req);
2216 struct tldap_req_state *state = tevent_req_data(
2217 req, struct tldap_req_state);
2220 rc = tldap_msg_recv(subreq, state, &state->result);
2221 TALLOC_FREE(subreq);
2222 if (tevent_req_ldap_error(req, rc)) {
2225 if (state->result->type != type) {
2226 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
2229 if (!asn1_start_tag(state->result->data, state->result->type) ||
2230 !tldap_decode_response(state) ||
2231 !asn1_end_tag(state->result->data) ||
2232 !tldap_decode_controls(state)) {
2233 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
2236 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr)) {
2237 tevent_req_ldap_error(req, state->result->lderr);
2240 tevent_req_done(req);
2243 static TLDAPRC tldap_simple_recv(struct tevent_req *req)
2246 if (tevent_req_is_ldap_error(req, &rc)) {
2249 return TLDAP_SUCCESS;
2252 static void tldap_add_done(struct tevent_req *subreq);
2254 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2255 struct tevent_context *ev,
2256 struct tldap_context *ld,
2258 struct tldap_mod *attributes,
2260 struct tldap_control *sctrls,
2262 struct tldap_control *cctrls,
2265 struct tevent_req *req, *subreq;
2266 struct tldap_req_state *state;
2269 req = tldap_req_create(mem_ctx, ld, &state);
2274 if (!asn1_push_tag(state->out, TLDAP_REQ_ADD)) goto err;
2275 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2276 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2278 for (i=0; i<num_attributes; i++) {
2279 struct tldap_mod *attrib = &attributes[i];
2280 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2281 if (!asn1_write_OctetString(state->out, attrib->attribute,
2282 strlen(attrib->attribute))) goto err;
2283 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2284 for (j=0; j<attrib->num_values; j++) {
2285 if (!asn1_write_OctetString(state->out,
2286 attrib->values[j].data,
2287 attrib->values[j].length)) goto err;
2289 if (!asn1_pop_tag(state->out)) goto err;
2290 if (!asn1_pop_tag(state->out)) goto err;
2293 if (!asn1_pop_tag(state->out)) goto err;
2294 if (!asn1_pop_tag(state->out)) goto err;
2296 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2297 sctrls, num_sctrls);
2298 if (tevent_req_nomem(subreq, req)) {
2299 return tevent_req_post(req, ev);
2301 tevent_req_set_callback(subreq, tldap_add_done, req);
2306 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2307 return tevent_req_post(req, ev);
2310 static void tldap_add_done(struct tevent_req *subreq)
2312 tldap_simple_done(subreq, TLDAP_RES_ADD);
2315 TLDAPRC tldap_add_recv(struct tevent_req *req)
2317 return tldap_simple_recv(req);
2320 TLDAPRC tldap_add(struct tldap_context *ld, const char *dn,
2321 struct tldap_mod *attributes, int num_attributes,
2322 struct tldap_control *sctrls, int num_sctrls,
2323 struct tldap_control *cctrls, int num_cctrls)
2325 TALLOC_CTX *frame = talloc_stackframe();
2326 struct tevent_context *ev;
2327 struct tevent_req *req;
2328 TLDAPRC rc = TLDAP_NO_MEMORY;
2330 ev = samba_tevent_context_init(frame);
2334 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2335 sctrls, num_sctrls, cctrls, num_cctrls);
2339 if (!tevent_req_poll(req, ev)) {
2340 rc = TLDAP_OPERATIONS_ERROR;
2343 rc = tldap_add_recv(req);
2344 tldap_save_msg(ld, req);
2350 static void tldap_modify_done(struct tevent_req *subreq);
2352 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2353 struct tevent_context *ev,
2354 struct tldap_context *ld,
2356 struct tldap_mod *mods, int num_mods,
2357 struct tldap_control *sctrls,
2359 struct tldap_control *cctrls,
2362 struct tevent_req *req, *subreq;
2363 struct tldap_req_state *state;
2366 req = tldap_req_create(mem_ctx, ld, &state);
2371 if (!asn1_push_tag(state->out, TLDAP_REQ_MODIFY)) goto err;
2372 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2373 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2375 for (i=0; i<num_mods; i++) {
2376 struct tldap_mod *mod = &mods[i];
2377 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2378 if (!asn1_write_enumerated(state->out, mod->mod_op)) goto err;
2379 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2380 if (!asn1_write_OctetString(state->out, mod->attribute,
2381 strlen(mod->attribute))) goto err;
2382 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2383 for (j=0; j<mod->num_values; j++) {
2384 if (!asn1_write_OctetString(state->out,
2385 mod->values[j].data,
2386 mod->values[j].length)) goto err;
2388 if (!asn1_pop_tag(state->out)) goto err;
2389 if (!asn1_pop_tag(state->out)) goto err;
2390 if (!asn1_pop_tag(state->out)) goto err;
2393 if (!asn1_pop_tag(state->out)) goto err;
2394 if (!asn1_pop_tag(state->out)) goto err;
2396 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2397 sctrls, num_sctrls);
2398 if (tevent_req_nomem(subreq, req)) {
2399 return tevent_req_post(req, ev);
2401 tevent_req_set_callback(subreq, tldap_modify_done, req);
2406 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2407 return tevent_req_post(req, ev);
2410 static void tldap_modify_done(struct tevent_req *subreq)
2412 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2415 TLDAPRC tldap_modify_recv(struct tevent_req *req)
2417 return tldap_simple_recv(req);
2420 TLDAPRC tldap_modify(struct tldap_context *ld, const char *dn,
2421 struct tldap_mod *mods, int num_mods,
2422 struct tldap_control *sctrls, int num_sctrls,
2423 struct tldap_control *cctrls, int num_cctrls)
2425 TALLOC_CTX *frame = talloc_stackframe();
2426 struct tevent_context *ev;
2427 struct tevent_req *req;
2428 TLDAPRC rc = TLDAP_NO_MEMORY;
2430 ev = samba_tevent_context_init(frame);
2434 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2435 sctrls, num_sctrls, cctrls, num_cctrls);
2439 if (!tevent_req_poll(req, ev)) {
2440 rc = TLDAP_OPERATIONS_ERROR;
2443 rc = tldap_modify_recv(req);
2444 tldap_save_msg(ld, req);
2450 static void tldap_delete_done(struct tevent_req *subreq);
2452 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2453 struct tevent_context *ev,
2454 struct tldap_context *ld,
2456 struct tldap_control *sctrls,
2458 struct tldap_control *cctrls,
2461 struct tevent_req *req, *subreq;
2462 struct tldap_req_state *state;
2464 req = tldap_req_create(mem_ctx, ld, &state);
2469 if (!asn1_push_tag(state->out, TLDAP_REQ_DELETE)) goto err;
2470 if (!asn1_write(state->out, dn, strlen(dn))) goto err;
2471 if (!asn1_pop_tag(state->out)) goto err;
2473 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2474 sctrls, num_sctrls);
2475 if (tevent_req_nomem(subreq, req)) {
2476 return tevent_req_post(req, ev);
2478 tevent_req_set_callback(subreq, tldap_delete_done, req);
2483 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2484 return tevent_req_post(req, ev);
2487 static void tldap_delete_done(struct tevent_req *subreq)
2489 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2492 TLDAPRC tldap_delete_recv(struct tevent_req *req)
2494 return tldap_simple_recv(req);
2497 TLDAPRC tldap_delete(struct tldap_context *ld, const char *dn,
2498 struct tldap_control *sctrls, int num_sctrls,
2499 struct tldap_control *cctrls, int num_cctrls)
2501 TALLOC_CTX *frame = talloc_stackframe();
2502 struct tevent_context *ev;
2503 struct tevent_req *req;
2504 TLDAPRC rc = TLDAP_NO_MEMORY;
2506 ev = samba_tevent_context_init(frame);
2510 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2511 cctrls, num_cctrls);
2515 if (!tevent_req_poll(req, ev)) {
2516 rc = TLDAP_OPERATIONS_ERROR;
2519 rc = tldap_delete_recv(req);
2520 tldap_save_msg(ld, req);
2526 int tldap_msg_id(const struct tldap_message *msg)
2531 int tldap_msg_type(const struct tldap_message *msg)
2536 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2541 return msg->res_matcheddn;
2544 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2549 return msg->res_diagnosticmessage;
2552 const char *tldap_msg_referral(struct tldap_message *msg)
2557 return msg->res_referral;
2560 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2561 struct tldap_control **sctrls)
2568 *sctrls = msg->res_sctrls;
2569 *num_sctrls = talloc_array_length(msg->res_sctrls);
2572 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2574 return ld->last_msg;
2577 static const struct { TLDAPRC rc; const char *string; } tldaprc_errmap[] =
2581 { TLDAP_OPERATIONS_ERROR,
2582 "TLDAP_OPERATIONS_ERROR" },
2583 { TLDAP_PROTOCOL_ERROR,
2584 "TLDAP_PROTOCOL_ERROR" },
2585 { TLDAP_TIMELIMIT_EXCEEDED,
2586 "TLDAP_TIMELIMIT_EXCEEDED" },
2587 { TLDAP_SIZELIMIT_EXCEEDED,
2588 "TLDAP_SIZELIMIT_EXCEEDED" },
2589 { TLDAP_COMPARE_FALSE,
2590 "TLDAP_COMPARE_FALSE" },
2591 { TLDAP_COMPARE_TRUE,
2592 "TLDAP_COMPARE_TRUE" },
2593 { TLDAP_STRONG_AUTH_NOT_SUPPORTED,
2594 "TLDAP_STRONG_AUTH_NOT_SUPPORTED" },
2595 { TLDAP_STRONG_AUTH_REQUIRED,
2596 "TLDAP_STRONG_AUTH_REQUIRED" },
2599 { TLDAP_ADMINLIMIT_EXCEEDED,
2600 "TLDAP_ADMINLIMIT_EXCEEDED" },
2601 { TLDAP_UNAVAILABLE_CRITICAL_EXTENSION,
2602 "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION" },
2603 { TLDAP_CONFIDENTIALITY_REQUIRED,
2604 "TLDAP_CONFIDENTIALITY_REQUIRED" },
2605 { TLDAP_SASL_BIND_IN_PROGRESS,
2606 "TLDAP_SASL_BIND_IN_PROGRESS" },
2607 { TLDAP_NO_SUCH_ATTRIBUTE,
2608 "TLDAP_NO_SUCH_ATTRIBUTE" },
2609 { TLDAP_UNDEFINED_TYPE,
2610 "TLDAP_UNDEFINED_TYPE" },
2611 { TLDAP_INAPPROPRIATE_MATCHING,
2612 "TLDAP_INAPPROPRIATE_MATCHING" },
2613 { TLDAP_CONSTRAINT_VIOLATION,
2614 "TLDAP_CONSTRAINT_VIOLATION" },
2615 { TLDAP_TYPE_OR_VALUE_EXISTS,
2616 "TLDAP_TYPE_OR_VALUE_EXISTS" },
2617 { TLDAP_INVALID_SYNTAX,
2618 "TLDAP_INVALID_SYNTAX" },
2619 { TLDAP_NO_SUCH_OBJECT,
2620 "TLDAP_NO_SUCH_OBJECT" },
2621 { TLDAP_ALIAS_PROBLEM,
2622 "TLDAP_ALIAS_PROBLEM" },
2623 { TLDAP_INVALID_DN_SYNTAX,
2624 "TLDAP_INVALID_DN_SYNTAX" },
2627 { TLDAP_ALIAS_DEREF_PROBLEM,
2628 "TLDAP_ALIAS_DEREF_PROBLEM" },
2629 { TLDAP_INAPPROPRIATE_AUTH,
2630 "TLDAP_INAPPROPRIATE_AUTH" },
2631 { TLDAP_INVALID_CREDENTIALS,
2632 "TLDAP_INVALID_CREDENTIALS" },
2633 { TLDAP_INSUFFICIENT_ACCESS,
2634 "TLDAP_INSUFFICIENT_ACCESS" },
2637 { TLDAP_UNAVAILABLE,
2638 "TLDAP_UNAVAILABLE" },
2639 { TLDAP_UNWILLING_TO_PERFORM,
2640 "TLDAP_UNWILLING_TO_PERFORM" },
2641 { TLDAP_LOOP_DETECT,
2642 "TLDAP_LOOP_DETECT" },
2643 { TLDAP_NAMING_VIOLATION,
2644 "TLDAP_NAMING_VIOLATION" },
2645 { TLDAP_OBJECT_CLASS_VIOLATION,
2646 "TLDAP_OBJECT_CLASS_VIOLATION" },
2647 { TLDAP_NOT_ALLOWED_ON_NONLEAF,
2648 "TLDAP_NOT_ALLOWED_ON_NONLEAF" },
2649 { TLDAP_NOT_ALLOWED_ON_RDN,
2650 "TLDAP_NOT_ALLOWED_ON_RDN" },
2651 { TLDAP_ALREADY_EXISTS,
2652 "TLDAP_ALREADY_EXISTS" },
2653 { TLDAP_NO_OBJECT_CLASS_MODS,
2654 "TLDAP_NO_OBJECT_CLASS_MODS" },
2655 { TLDAP_RESULTS_TOO_LARGE,
2656 "TLDAP_RESULTS_TOO_LARGE" },
2657 { TLDAP_AFFECTS_MULTIPLE_DSAS,
2658 "TLDAP_AFFECTS_MULTIPLE_DSAS" },
2661 { TLDAP_SERVER_DOWN,
2662 "TLDAP_SERVER_DOWN" },
2663 { TLDAP_LOCAL_ERROR,
2664 "TLDAP_LOCAL_ERROR" },
2665 { TLDAP_ENCODING_ERROR,
2666 "TLDAP_ENCODING_ERROR" },
2667 { TLDAP_DECODING_ERROR,
2668 "TLDAP_DECODING_ERROR" },
2671 { TLDAP_AUTH_UNKNOWN,
2672 "TLDAP_AUTH_UNKNOWN" },
2673 { TLDAP_FILTER_ERROR,
2674 "TLDAP_FILTER_ERROR" },
2675 { TLDAP_USER_CANCELLED,
2676 "TLDAP_USER_CANCELLED" },
2677 { TLDAP_PARAM_ERROR,
2678 "TLDAP_PARAM_ERROR" },
2680 "TLDAP_NO_MEMORY" },
2681 { TLDAP_CONNECT_ERROR,
2682 "TLDAP_CONNECT_ERROR" },
2683 { TLDAP_NOT_SUPPORTED,
2684 "TLDAP_NOT_SUPPORTED" },
2685 { TLDAP_CONTROL_NOT_FOUND,
2686 "TLDAP_CONTROL_NOT_FOUND" },
2687 { TLDAP_NO_RESULTS_RETURNED,
2688 "TLDAP_NO_RESULTS_RETURNED" },
2689 { TLDAP_MORE_RESULTS_TO_RETURN,
2690 "TLDAP_MORE_RESULTS_TO_RETURN" },
2691 { TLDAP_CLIENT_LOOP,
2692 "TLDAP_CLIENT_LOOP" },
2693 { TLDAP_REFERRAL_LIMIT_EXCEEDED,
2694 "TLDAP_REFERRAL_LIMIT_EXCEEDED" },
2697 const char *tldap_rc2string(TLDAPRC rc)
2701 for (i=0; i<ARRAY_SIZE(tldaprc_errmap); i++) {
2702 if (TLDAP_RC_EQUAL(rc, tldaprc_errmap[i].rc)) {
2703 return tldaprc_errmap[i].string;
2707 return "Unknown LDAP Error";