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);
32 static bool tldap_msg_set_pending(struct tevent_req *req);
34 #define TEVENT_TLDAP_RC_MAGIC (0x87bcd26e)
36 bool tevent_req_ldap_error(struct tevent_req *req, TLDAPRC rc)
40 if (TLDAP_RC_IS_SUCCESS(rc)) {
44 err = TEVENT_TLDAP_RC_MAGIC;
46 err |= TLDAP_RC_V(rc);
48 return tevent_req_error(req, err);
51 bool tevent_req_is_ldap_error(struct tevent_req *req, TLDAPRC *perr)
53 enum tevent_req_state state;
56 if (!tevent_req_is_error(req, &state, &err)) {
60 case TEVENT_REQ_TIMED_OUT:
61 *perr = TLDAP_TIMEOUT;
63 case TEVENT_REQ_NO_MEMORY:
64 *perr = TLDAP_NO_MEMORY;
66 case TEVENT_REQ_USER_ERROR:
67 if ((err >> 32) != TEVENT_TLDAP_RC_MAGIC) {
70 *perr = TLDAP_RC(err & 0xffffffff);
73 *perr = TLDAP_OPERATIONS_ERROR;
79 struct tldap_ctx_attribute {
84 struct tldap_context {
86 struct tstream_context *conn;
88 struct tevent_queue *outgoing;
89 struct tevent_req **pending;
90 struct tevent_req *read_req;
92 /* For the sync wrappers we need something like get_last_error... */
93 struct tldap_message *last_msg;
96 void (*log_fn)(void *context, enum tldap_debug_level level,
97 const char *fmt, va_list ap);
100 struct tldap_ctx_attribute *ctx_attrs;
103 struct tldap_message {
104 struct asn1_data *data;
111 struct tldap_attribute *attribs;
113 /* Error data sent by the server */
116 char *res_diagnosticmessage;
118 DATA_BLOB res_serverSaslCreds;
119 struct tldap_control *res_sctrls;
121 /* Controls sent by the server */
122 struct tldap_control *ctrls;
125 void tldap_set_debug(struct tldap_context *ld,
126 void (*log_fn)(void *log_private,
127 enum tldap_debug_level level,
129 va_list ap) PRINTF_ATTRIBUTE(3,0),
133 ld->log_private = log_private;
136 static void tldap_debug(
137 struct tldap_context *ld,
138 enum tldap_debug_level level,
139 const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
141 static void tldap_debug(struct tldap_context *ld,
142 enum tldap_debug_level level,
143 const char *fmt, ...)
149 if (ld->log_fn == NULL) {
153 ld->log_fn(ld->log_private, level, fmt, ap);
157 static int tldap_next_msgid(struct tldap_context *ld)
161 result = ld->msgid++;
162 if (ld->msgid == INT_MAX) {
168 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
170 struct tldap_context *ctx;
173 ctx = talloc_zero(mem_ctx, struct tldap_context);
177 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
184 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
185 if (ctx->outgoing == NULL) {
192 bool tldap_connection_ok(struct tldap_context *ld)
200 if (ld->conn == NULL) {
204 ret = tstream_pending_bytes(ld->conn);
212 static size_t tldap_pending_reqs(struct tldap_context *ld)
214 return talloc_array_length(ld->pending);
217 struct tstream_context *tldap_get_tstream(struct tldap_context *ld)
222 void tldap_set_tstream(struct tldap_context *ld,
223 struct tstream_context *stream)
228 static struct tldap_ctx_attribute *tldap_context_findattr(
229 struct tldap_context *ld, const char *name)
233 num_attrs = talloc_array_length(ld->ctx_attrs);
235 for (i=0; i<num_attrs; i++) {
236 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
237 return &ld->ctx_attrs[i];
243 bool tldap_context_setattr(struct tldap_context *ld,
244 const char *name, const void *_pptr)
246 struct tldap_ctx_attribute *tmp, *attr;
249 void **pptr = (void **)discard_const_p(void,_pptr);
251 attr = tldap_context_findattr(ld, name);
254 * We don't actually delete attrs, we don't expect tons of
255 * attributes being shuffled around.
257 TALLOC_FREE(attr->ptr);
259 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
265 tmpname = talloc_strdup(ld, name);
266 if (tmpname == NULL) {
270 num_attrs = talloc_array_length(ld->ctx_attrs);
272 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
275 TALLOC_FREE(tmpname);
278 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
280 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
282 tmp[num_attrs].ptr = NULL;
289 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
291 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
299 struct read_ldap_state {
304 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
305 static void read_ldap_done(struct tevent_req *subreq);
307 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
308 struct tevent_context *ev,
309 struct tstream_context *conn)
311 struct tevent_req *req, *subreq;
312 struct read_ldap_state *state;
314 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
320 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
322 if (tevent_req_nomem(subreq, req)) {
323 return tevent_req_post(req, ev);
325 tevent_req_set_callback(subreq, read_ldap_done, req);
329 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
331 struct read_ldap_state *state = talloc_get_type_abort(
332 private_data, struct read_ldap_state);
337 /* We've been here, we're done */
342 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
344 if (buf[0] != 0x30) {
349 if ((len & 0x80) == 0) {
354 lensize = (len & 0x7f);
358 /* Please get us the full length */
361 if (buflen > 2 + lensize) {
365 if (buflen != 2 + lensize) {
369 for (i=0; i<lensize; i++) {
370 len = (len << 8) | buf[2+i];
375 static void read_ldap_done(struct tevent_req *subreq)
377 struct tevent_req *req = tevent_req_callback_data(
378 subreq, struct tevent_req);
379 struct read_ldap_state *state = tevent_req_data(
380 req, struct read_ldap_state);
384 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
387 tevent_req_error(req, err);
390 tevent_req_done(req);
393 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
394 uint8_t **pbuf, int *perrno)
396 struct read_ldap_state *state = tevent_req_data(
397 req, struct read_ldap_state);
399 if (tevent_req_is_unix_error(req, perrno)) {
402 *pbuf = talloc_move(mem_ctx, &state->buf);
403 return talloc_get_size(*pbuf);
406 struct tldap_msg_state {
407 struct tldap_context *ld;
408 struct tevent_context *ev;
412 struct asn1_data *data;
416 static bool tldap_push_controls(struct asn1_data *data,
417 struct tldap_control *sctrls,
422 if ((sctrls == NULL) || (num_sctrls == 0)) {
426 if (!asn1_push_tag(data, ASN1_CONTEXT(0))) return false;
428 for (i=0; i<num_sctrls; i++) {
429 struct tldap_control *c = &sctrls[i];
430 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
431 if (!asn1_write_OctetString(data, c->oid, strlen(c->oid))) return false;
433 if (!asn1_write_BOOLEAN(data, true)) return false;
435 if (c->value.data != NULL) {
436 if (!asn1_write_OctetString(data, c->value.data,
437 c->value.length)) return false;
439 if (!asn1_pop_tag(data)) return false; /* ASN1_SEQUENCE(0) */
442 return asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
445 #define tldap_context_disconnect(ld, status) \
446 _tldap_context_disconnect(ld, status, __location__)
448 static void _tldap_context_disconnect(struct tldap_context *ld,
450 const char *location)
452 if (ld->conn == NULL) {
454 * We don't need to tldap_debug() on
455 * a potential 2nd run.
457 * The rest of the function would just
458 * be a noop for the 2nd run anyway.
463 tldap_debug(ld, TLDAP_DEBUG_WARNING,
464 "tldap_context_disconnect: %s at %s\n",
465 tldap_rc2string(status),
467 tevent_queue_stop(ld->outgoing);
468 TALLOC_FREE(ld->read_req);
469 TALLOC_FREE(ld->conn);
471 while (talloc_array_length(ld->pending) > 0) {
472 struct tevent_req *req = NULL;
473 struct tldap_msg_state *state = NULL;
475 req = ld->pending[0];
476 state = tevent_req_data(req, struct tldap_msg_state);
477 tevent_req_defer_callback(req, state->ev);
478 tevent_req_ldap_error(req, status);
482 static void tldap_msg_sent(struct tevent_req *subreq);
483 static void tldap_msg_received(struct tevent_req *subreq);
485 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
486 struct tevent_context *ev,
487 struct tldap_context *ld,
488 int id, struct asn1_data *data,
489 struct tldap_control *sctrls,
492 struct tevent_req *req, *subreq;
493 struct tldap_msg_state *state;
497 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
500 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
508 ok = tldap_connection_ok(ld);
510 tevent_req_ldap_error(req, TLDAP_SERVER_DOWN);
511 return tevent_req_post(req, ev);
514 if (!tldap_push_controls(data, sctrls, num_sctrls)) {
515 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
516 return tevent_req_post(req, ev);
520 if (!asn1_pop_tag(data)) {
521 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
522 return tevent_req_post(req, ev);
525 if (!asn1_blob(data, &blob)) {
526 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
527 return tevent_req_post(req, ev);
530 if (!tldap_msg_set_pending(req)) {
532 return tevent_req_post(req, ev);;
535 state->iov.iov_base = (void *)blob.data;
536 state->iov.iov_len = blob.length;
538 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
540 if (tevent_req_nomem(subreq, req)) {
541 return tevent_req_post(req, ev);
543 tevent_req_set_callback(subreq, tldap_msg_sent, req);
547 static void tldap_msg_unset_pending(struct tevent_req *req)
549 struct tldap_msg_state *state = tevent_req_data(
550 req, struct tldap_msg_state);
551 struct tldap_context *ld = state->ld;
552 int num_pending = tldap_pending_reqs(ld);
555 tevent_req_set_cleanup_fn(req, NULL);
557 for (i=0; i<num_pending; i++) {
558 if (req == ld->pending[i]) {
562 if (i == num_pending) {
564 * Something's seriously broken. Just returning here is the
565 * right thing nevertheless, the point of this routine is to
566 * remove ourselves from cli->pending.
571 if (num_pending == 1) {
572 TALLOC_FREE(ld->pending);
577 * Remove ourselves from the cli->pending array
579 if (num_pending > 1) {
580 ld->pending[i] = ld->pending[num_pending-1];
584 * No NULL check here, we're shrinking by sizeof(void *), and
585 * talloc_realloc just adjusts the size for this.
587 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
591 static void tldap_msg_cleanup(struct tevent_req *req,
592 enum tevent_req_state req_state)
594 tldap_msg_unset_pending(req);
597 static bool tldap_msg_set_pending(struct tevent_req *req)
599 struct tldap_msg_state *state = tevent_req_data(
600 req, struct tldap_msg_state);
601 struct tldap_context *ld;
602 struct tevent_req **pending;
606 num_pending = tldap_pending_reqs(ld);
608 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
610 if (pending == NULL) {
613 pending[num_pending] = req;
614 ld->pending = pending;
615 tevent_req_set_cleanup_fn(req, tldap_msg_cleanup);
617 if (ld->read_req != NULL) {
622 * We're the first one, add the read_ldap request that waits for the
623 * answer from the server
625 ld->read_req = read_ldap_send(ld->pending, state->ev, ld->conn);
626 if (ld->read_req == NULL) {
627 tldap_msg_unset_pending(req);
630 tevent_req_set_callback(ld->read_req, tldap_msg_received, ld);
634 static void tldap_msg_sent(struct tevent_req *subreq)
636 struct tevent_req *req = tevent_req_callback_data(
637 subreq, struct tevent_req);
638 struct tldap_msg_state *state = tevent_req_data(
639 req, struct tldap_msg_state);
643 nwritten = tstream_writev_queue_recv(subreq, &err);
645 if (nwritten == -1) {
646 tldap_context_disconnect(state->ld, TLDAP_SERVER_DOWN);
651 static int tldap_msg_msgid(struct tevent_req *req)
653 struct tldap_msg_state *state = tevent_req_data(
654 req, struct tldap_msg_state);
659 static void tldap_msg_received(struct tevent_req *subreq)
661 struct tldap_context *ld = tevent_req_callback_data(
662 subreq, struct tldap_context);
663 struct tevent_req *req;
664 struct tldap_msg_state *state;
665 struct asn1_data *data;
671 TLDAPRC status = TLDAP_PROTOCOL_ERROR;
676 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
679 if (received == -1) {
680 status = TLDAP_SERVER_DOWN;
684 data = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH);
687 * We have to disconnect all, we can't tell which of
688 * the requests this reply is for.
690 status = TLDAP_NO_MEMORY;
693 asn1_load_nocopy(data, inbuf, received);
696 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
697 ok &= asn1_read_Integer(data, &id);
698 ok &= asn1_peek_uint8(data, &type);
701 status = TLDAP_PROTOCOL_ERROR;
705 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
706 "type %d\n", id, (int)type);
712 "tldap_msg_received: got msgid 0 of "
713 "type %"PRIu8", disconnecting\n",
715 tldap_context_disconnect(ld, TLDAP_SERVER_DOWN);
719 num_pending = talloc_array_length(ld->pending);
721 for (i=0; i<num_pending; i++) {
722 if (id == tldap_msg_msgid(ld->pending[i])) {
726 if (i == num_pending) {
727 /* Dump unexpected reply */
728 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
729 "No request pending for msg %d\n", id);
735 req = ld->pending[i];
736 state = tevent_req_data(req, struct tldap_msg_state);
738 state->inbuf = talloc_move(state, &inbuf);
739 state->data = talloc_move(state, &data);
741 tldap_msg_unset_pending(req);
742 num_pending = talloc_array_length(ld->pending);
744 tevent_req_defer_callback(req, state->ev);
745 tevent_req_done(req);
748 if (num_pending == 0) {
752 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
753 ld->read_req = read_ldap_send(ld->pending, state->ev, ld->conn);
754 if (ld->read_req == NULL) {
755 status = TLDAP_NO_MEMORY;
758 tevent_req_set_callback(ld->read_req, tldap_msg_received, ld);
762 tldap_context_disconnect(ld, status);
765 static TLDAPRC tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
766 struct tldap_message **pmsg)
768 struct tldap_msg_state *state = tevent_req_data(
769 req, struct tldap_msg_state);
770 struct tldap_message *msg;
774 if (tevent_req_is_ldap_error(req, &err)) {
778 if (!asn1_peek_uint8(state->data, &msgtype)) {
779 return TLDAP_PROTOCOL_ERROR;
783 return TLDAP_SUCCESS;
786 msg = talloc_zero(mem_ctx, struct tldap_message);
788 return TLDAP_NO_MEMORY;
792 msg->inbuf = talloc_move(msg, &state->inbuf);
793 msg->data = talloc_move(msg, &state->data);
797 return TLDAP_SUCCESS;
800 struct tldap_req_state {
802 struct asn1_data *out;
803 struct tldap_message *result;
806 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
807 struct tldap_context *ld,
808 struct tldap_req_state **pstate)
810 struct tevent_req *req;
811 struct tldap_req_state *state;
813 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
817 state->out = asn1_init(state, ASN1_MAX_TREE_DEPTH);
818 if (state->out == NULL) {
821 state->id = tldap_next_msgid(ld);
823 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
824 if (!asn1_write_Integer(state->out, state->id)) goto err;
835 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
837 struct tldap_req_state *state = tevent_req_data(
838 req, struct tldap_req_state);
840 TALLOC_FREE(ld->last_msg);
841 ld->last_msg = talloc_move(ld, &state->result);
844 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
846 char *result = talloc_array(mem_ctx, char, blob.length+1);
848 if (result == NULL) {
852 memcpy(result, blob.data, blob.length);
853 result[blob.length] = '\0';
857 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
858 struct asn1_data *data,
863 if (!asn1_read_OctetString(data, mem_ctx, &string))
866 result = blob2string_talloc(mem_ctx, string);
868 data_blob_free(&string);
870 if (result == NULL) {
877 static bool tldap_decode_controls(struct tldap_req_state *state);
879 static bool tldap_decode_response(struct tldap_req_state *state)
881 struct asn1_data *data = state->result->data;
882 struct tldap_message *msg = state->result;
886 ok &= asn1_read_enumerated(data, &rc);
888 msg->lderr = TLDAP_RC(rc);
891 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
892 ok &= asn1_read_OctetString_talloc(msg, data,
893 &msg->res_diagnosticmessage);
895 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
896 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
897 ok &= asn1_read_OctetString_talloc(msg, data,
899 ok &= asn1_end_tag(data);
901 msg->res_referral = NULL;
907 static void tldap_sasl_bind_done(struct tevent_req *subreq);
909 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
910 struct tevent_context *ev,
911 struct tldap_context *ld,
913 const char *mechanism,
915 struct tldap_control *sctrls,
917 struct tldap_control *cctrls,
920 struct tevent_req *req, *subreq;
921 struct tldap_req_state *state;
923 req = tldap_req_create(mem_ctx, ld, &state);
932 if (!asn1_push_tag(state->out, TLDAP_REQ_BIND)) goto err;
933 if (!asn1_write_Integer(state->out, ld->ld_version)) goto err;
934 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
936 if (mechanism == NULL) {
937 if (!asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0))) goto err;
938 if (!asn1_write(state->out, creds->data, creds->length)) goto err;
939 if (!asn1_pop_tag(state->out)) goto err;
941 if (!asn1_push_tag(state->out, ASN1_CONTEXT(3))) goto err;
942 if (!asn1_write_OctetString(state->out, mechanism,
943 strlen(mechanism))) goto err;
944 if ((creds != NULL) && (creds->data != NULL)) {
945 if (!asn1_write_OctetString(state->out, creds->data,
946 creds->length)) goto err;
948 if (!asn1_pop_tag(state->out)) goto err;
951 if (!asn1_pop_tag(state->out)) goto err;
953 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
955 if (tevent_req_nomem(subreq, req)) {
956 return tevent_req_post(req, ev);
958 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
963 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
964 return tevent_req_post(req, ev);
967 static void tldap_sasl_bind_done(struct tevent_req *subreq)
969 struct tevent_req *req = tevent_req_callback_data(
970 subreq, struct tevent_req);
971 struct tldap_req_state *state = tevent_req_data(
972 req, struct tldap_req_state);
976 rc = tldap_msg_recv(subreq, state, &state->result);
978 if (tevent_req_ldap_error(req, rc)) {
981 if (state->result->type != TLDAP_RES_BIND) {
982 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
986 ok = asn1_start_tag(state->result->data, TLDAP_RES_BIND);
987 ok &= tldap_decode_response(state);
989 if (asn1_peek_tag(state->result->data, ASN1_CONTEXT_SIMPLE(7))) {
992 ok &= asn1_start_tag(state->result->data,
993 ASN1_CONTEXT_SIMPLE(7));
998 len = asn1_tag_remaining(state->result->data);
1003 state->result->res_serverSaslCreds =
1004 data_blob_talloc(state->result, NULL, len);
1005 if (state->result->res_serverSaslCreds.data == NULL) {
1009 ok = asn1_read(state->result->data,
1010 state->result->res_serverSaslCreds.data,
1011 state->result->res_serverSaslCreds.length);
1013 ok &= asn1_end_tag(state->result->data);
1016 ok &= asn1_end_tag(state->result->data);
1022 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr) &&
1023 !TLDAP_RC_EQUAL(state->result->lderr,
1024 TLDAP_SASL_BIND_IN_PROGRESS)) {
1025 tevent_req_ldap_error(req, state->result->lderr);
1028 tevent_req_done(req);
1032 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
1036 TLDAPRC tldap_sasl_bind_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1037 DATA_BLOB *serverSaslCreds)
1039 struct tldap_req_state *state = tevent_req_data(
1040 req, struct tldap_req_state);
1043 if (tevent_req_is_ldap_error(req, &rc)) {
1047 if (serverSaslCreds != NULL) {
1048 serverSaslCreds->data = talloc_move(
1049 mem_ctx, &state->result->res_serverSaslCreds.data);
1050 serverSaslCreds->length =
1051 state->result->res_serverSaslCreds.length;
1054 return state->result->lderr;
1057 TLDAPRC tldap_sasl_bind(struct tldap_context *ld,
1059 const char *mechanism,
1061 struct tldap_control *sctrls,
1063 struct tldap_control *cctrls,
1065 TALLOC_CTX *mem_ctx,
1066 DATA_BLOB *serverSaslCreds)
1068 TALLOC_CTX *frame = talloc_stackframe();
1069 struct tevent_context *ev;
1070 struct tevent_req *req;
1071 TLDAPRC rc = TLDAP_NO_MEMORY;
1073 ev = samba_tevent_context_init(frame);
1077 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
1078 sctrls, num_sctrls, cctrls, num_cctrls);
1082 if (!tevent_req_poll(req, ev)) {
1083 rc = TLDAP_OPERATIONS_ERROR;
1086 rc = tldap_sasl_bind_recv(req, mem_ctx, serverSaslCreds);
1087 tldap_save_msg(ld, req);
1093 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
1094 struct tevent_context *ev,
1095 struct tldap_context *ld,
1101 if (passwd != NULL) {
1102 cred.data = discard_const_p(uint8_t, passwd);
1103 cred.length = strlen(passwd);
1105 cred.data = discard_const_p(uint8_t, "");
1108 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
1112 TLDAPRC tldap_simple_bind_recv(struct tevent_req *req)
1114 return tldap_sasl_bind_recv(req, NULL, NULL);
1117 TLDAPRC tldap_simple_bind(struct tldap_context *ld, const char *dn,
1122 if (passwd != NULL) {
1123 cred.data = discard_const_p(uint8_t, passwd);
1124 cred.length = strlen(passwd);
1126 cred.data = discard_const_p(uint8_t, "");
1129 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0,
1133 /*****************************************************************************/
1135 /* can't use isalpha() as only a strict set is valid for LDAP */
1137 static bool tldap_is_alpha(char c)
1139 return (((c >= 'a') && (c <= 'z')) || \
1140 ((c >= 'A') && (c <= 'Z')));
1143 static bool tldap_is_adh(char c)
1145 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
1148 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
1149 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
1150 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
1151 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
1152 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
1153 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
1154 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
1155 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
1156 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
1157 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
1159 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
1160 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
1161 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1164 /* oid's should be numerical only in theory,
1165 * but apparently some broken servers may have alphanum aliases instead.
1166 * Do like openldap libraries and allow alphanum aliases for oids, but
1167 * do not allow Tagging options in that case.
1169 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1171 bool is_oid = false;
1175 /* first char has stricter rules */
1178 } else if (!tldap_is_alpha(*s)) {
1179 /* bad first char */
1183 for (i = 1; i < len; i++) {
1186 if (isdigit(s[i])) {
1199 if (tldap_is_adh(s[i])) {
1206 /* no tagging options */
1213 if ((i + 1) == len) {
1231 /* this function copies the value until the closing parenthesis is found. */
1232 static char *tldap_get_val(TALLOC_CTX *memctx,
1233 const char *value, const char **_s)
1235 const char *s = value;
1237 /* find terminator */
1240 if (s && (*(s - 1) == '\\')) {
1246 if (!s || !(*s == ')')) {
1247 /* malformed filter */
1253 return talloc_strndup(memctx, value, s - value);
1256 static int tldap_hex2char(const char *x)
1258 if (isxdigit(x[0]) && isxdigit(x[1])) {
1259 const char h1 = x[0], h2 = x[1];
1262 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1263 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1264 else if (h1 >= '0') c = h1 - (int)'0';
1266 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1267 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1268 else if (h2 >= '0') c += h2 - (int)'0';
1276 static bool tldap_find_first_star(const char *val, const char **star)
1280 for (s = val; *s; s++) {
1283 if (isxdigit(s[1]) && isxdigit(s[2])) {
1287 /* not hex based escape, check older syntax */
1296 /* invalid escape sequence */
1301 /* end of val, nothing found */
1311 /* string ended without closing parenthesis, filter is malformed */
1315 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1320 for (i = 0,p = 0; i < *val_len; i++) {
1326 /* these must be escaped */
1330 if (!value[i + 1]) {
1336 /* LDAPv3 escaped */
1337 c = tldap_hex2char(&value[i]);
1338 if (c >= 0 && c < 256) {
1345 /* LDAPv2 escaped */
1351 value[p] = value[i];
1362 value[p] = value[i];
1371 static bool tldap_push_filter_basic(struct tldap_context *ld,
1372 struct asn1_data *data,
1374 static bool tldap_push_filter_substring(struct tldap_context *ld,
1375 struct asn1_data *data,
1378 static bool tldap_push_filter_int(struct tldap_context *ld,
1379 struct asn1_data *data,
1382 const char *s = *_s;
1386 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1387 "Incomplete or malformed filter\n");
1392 /* we are right after a parenthesis,
1393 * find out what op we have at hand */
1396 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1397 if (!asn1_push_tag(data, TLDAP_FILTER_AND)) return false;
1402 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1403 if (!asn1_push_tag(data, TLDAP_FILTER_OR)) return false;
1408 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1409 if (!asn1_push_tag(data, TLDAP_FILTER_NOT)) return false;
1411 ret = tldap_push_filter_int(ld, data, &s);
1415 if (!asn1_pop_tag(data)) return false;
1420 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1421 "Invalid parenthesis '%c'\n", *s);
1425 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1426 "Invalid filter termination\n");
1430 ret = tldap_push_filter_basic(ld, data, &s);
1437 /* only and/or filters get here.
1438 * go through the list of filters */
1441 /* RFC 4526: empty and/or */
1442 if (!asn1_pop_tag(data)) return false;
1447 ret = tldap_push_filter_int(ld, data, &s);
1453 /* end of list, return */
1454 if (!asn1_pop_tag(data)) return false;
1461 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1462 "Incomplete or malformed filter\n");
1467 if (asn1_has_error(data)) {
1476 static bool tldap_push_filter_basic(struct tldap_context *ld,
1477 struct asn1_data *data,
1480 TALLOC_CTX *tmpctx = talloc_tos();
1481 const char *s = *_s;
1489 size_t type_len = 0;
1492 bool write_octect = true;
1495 eq = strchr(s, '=');
1497 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1498 "Invalid filter, missing equal sign\n");
1507 if (!asn1_push_tag(data, TLDAP_FILTER_LE)) return false;
1511 if (!asn1_push_tag(data, TLDAP_FILTER_GE)) return false;
1515 if (!asn1_push_tag(data, TLDAP_FILTER_APX)) return false;
1519 if (!asn1_push_tag(data, TLDAP_FILTER_EXT)) return false;
1520 write_octect = false;
1526 if (*s == ':') { /* [:dn]:rule:= value */
1528 /* malformed filter */
1532 } else { /* type[:dn][:rule]:= value */
1534 dn = strchr(s, ':');
1535 type_len = dn - type;
1536 if (dn == e) { /* type:= value */
1543 rule = strchr(dn, ':');
1547 if ((rule == dn + 1) || rule + 1 == e) {
1548 /* malformed filter, contains "::" */
1552 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1557 /* malformed filter. With two
1558 * optionals, the first must be "dn"
1571 if (!type && !dn && !rule) {
1572 /* malformed filter, there must be at least one */
1577 MatchingRuleAssertion ::= SEQUENCE {
1578 matchingRule [1] MatchingRuleID OPTIONAL,
1579 type [2] AttributeDescription OPTIONAL,
1580 matchValue [3] AssertionValue,
1581 dnAttributes [4] BOOLEAN DEFAULT FALSE
1585 /* check and add rule */
1587 ret = tldap_is_attrdesc(rule, e - rule, true);
1591 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1))) return false;
1592 if (!asn1_write(data, rule, e - rule)) return false;
1593 if (!asn1_pop_tag(data)) return false;
1596 /* check and add type */
1598 ret = tldap_is_attrdesc(type, type_len, false);
1602 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2))) return false;
1603 if (!asn1_write(data, type, type_len)) return false;
1604 if (!asn1_pop_tag(data)) return false;
1607 uval = tldap_get_val(tmpctx, val, _s);
1611 uval_len = *_s - val;
1612 ret = tldap_unescape_inplace(uval, &uval_len);
1617 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3))) return false;
1618 if (!asn1_write(data, uval, uval_len)) return false;
1619 if (!asn1_pop_tag(data)) return false;
1621 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4))) return false;
1622 if (!asn1_write_uint8(data, dn?1:0)) return false;
1623 if (!asn1_pop_tag(data)) return false;
1629 ret = tldap_is_attrdesc(s, e - s, false);
1634 if (strncmp(val, "*)", 2) == 0) {
1636 if (!asn1_push_tag(data, TLDAP_FILTER_PRES)) return false;
1637 if (!asn1_write(data, s, e - s)) return false;
1639 write_octect = false;
1643 ret = tldap_find_first_star(val, &star);
1649 if (!asn1_push_tag(data, TLDAP_FILTER_SUB)) return false;
1650 if (!asn1_write_OctetString(data, s, e - s)) return false;
1651 ret = tldap_push_filter_substring(ld, data, val, &s);
1656 write_octect = false;
1660 /* if nothing else, then it is just equality */
1661 if (!asn1_push_tag(data, TLDAP_FILTER_EQ)) return false;
1662 write_octect = true;
1667 uval = tldap_get_val(tmpctx, val, _s);
1671 uval_len = *_s - val;
1672 ret = tldap_unescape_inplace(uval, &uval_len);
1677 if (!asn1_write_OctetString(data, s, e - s)) return false;
1678 if (!asn1_write_OctetString(data, uval, uval_len)) return false;
1681 if (asn1_has_error(data)) {
1684 return asn1_pop_tag(data);
1687 static bool tldap_push_filter_substring(struct tldap_context *ld,
1688 struct asn1_data *data,
1692 TALLOC_CTX *tmpctx = talloc_tos();
1693 bool initial = true;
1700 SubstringFilter ::= SEQUENCE {
1701 type AttributeDescription,
1702 -- at least one must be present
1703 substrings SEQUENCE OF CHOICE {
1704 initial [0] LDAPString,
1706 final [2] LDAPString } }
1708 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
1711 ret = tldap_find_first_star(val, &star);
1715 chunk_len = star - val;
1719 if (!initial && chunk_len == 0) {
1720 /* found '**', which is illegal */
1736 if (initial && chunk_len == 0) {
1742 chunk = talloc_strndup(tmpctx, val, chunk_len);
1746 ret = tldap_unescape_inplace(chunk, &chunk_len);
1753 if (!asn1_push_tag(data, TLDAP_SUB_INI)) return false;
1756 if (!asn1_push_tag(data, TLDAP_SUB_ANY)) return false;
1760 if (!asn1_push_tag(data, TLDAP_SUB_FIN)) return false;
1766 if (!asn1_write(data, chunk, chunk_len)) return false;
1767 if (!asn1_pop_tag(data)) return false;
1771 } while (*star == '*');
1775 /* end of sequence */
1776 return asn1_pop_tag(data);
1779 /* NOTE: although openldap libraries allow for spaces in some places, mostly
1780 * around parentheses, we do not allow any spaces (except in values of
1781 * course) as I couldn't find any place in RFC 4512 or RFC 4515 where
1782 * leading or trailing spaces were allowed.
1784 static bool tldap_push_filter(struct tldap_context *ld,
1785 struct asn1_data *data,
1788 const char *s = filter;
1791 ret = tldap_push_filter_int(ld, data, &s);
1793 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1794 "Incomplete or malformed filter\n");
1800 /*****************************************************************************/
1802 static void tldap_search_done(struct tevent_req *subreq);
1804 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1805 struct tevent_context *ev,
1806 struct tldap_context *ld,
1807 const char *base, int scope,
1812 struct tldap_control *sctrls,
1814 struct tldap_control *cctrls,
1820 struct tevent_req *req, *subreq;
1821 struct tldap_req_state *state;
1824 req = tldap_req_create(mem_ctx, ld, &state);
1829 if (!asn1_push_tag(state->out, TLDAP_REQ_SEARCH)) goto encoding_error;
1830 if (!asn1_write_OctetString(state->out, base, strlen(base))) goto encoding_error;
1831 if (!asn1_write_enumerated(state->out, scope)) goto encoding_error;
1832 if (!asn1_write_enumerated(state->out, deref)) goto encoding_error;
1833 if (!asn1_write_Integer(state->out, sizelimit)) goto encoding_error;
1834 if (!asn1_write_Integer(state->out, timelimit)) goto encoding_error;
1835 if (!asn1_write_BOOLEAN(state->out, attrsonly)) goto encoding_error;
1837 if (!tldap_push_filter(ld, state->out, filter)) {
1838 goto encoding_error;
1841 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto encoding_error;
1842 for (i=0; i<num_attrs; i++) {
1843 if (!asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]))) goto encoding_error;
1845 if (!asn1_pop_tag(state->out)) goto encoding_error;
1846 if (!asn1_pop_tag(state->out)) goto encoding_error;
1848 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1849 sctrls, num_sctrls);
1850 if (tevent_req_nomem(subreq, req)) {
1851 return tevent_req_post(req, ev);
1853 tevent_req_set_callback(subreq, tldap_search_done, req);
1857 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
1858 return tevent_req_post(req, ev);
1861 static void tldap_search_done(struct tevent_req *subreq)
1863 struct tevent_req *req = tevent_req_callback_data(
1864 subreq, struct tevent_req);
1865 struct tldap_req_state *state = tevent_req_data(
1866 req, struct tldap_req_state);
1869 rc = tldap_msg_recv(subreq, state, &state->result);
1870 if (tevent_req_ldap_error(req, rc)) {
1873 switch (state->result->type) {
1874 case TLDAP_RES_SEARCH_ENTRY:
1875 case TLDAP_RES_SEARCH_REFERENCE:
1876 if (!tldap_msg_set_pending(subreq)) {
1877 tevent_req_oom(req);
1880 tevent_req_notify_callback(req);
1882 case TLDAP_RES_SEARCH_RESULT:
1883 TALLOC_FREE(subreq);
1884 if (!asn1_start_tag(state->result->data,
1885 state->result->type) ||
1886 !tldap_decode_response(state) ||
1887 !asn1_end_tag(state->result->data) ||
1888 !tldap_decode_controls(state)) {
1889 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
1892 tevent_req_done(req);
1895 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
1900 TLDAPRC tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1901 struct tldap_message **pmsg)
1903 struct tldap_req_state *state = tevent_req_data(
1904 req, struct tldap_req_state);
1907 if (!tevent_req_is_in_progress(req)
1908 && tevent_req_is_ldap_error(req, &rc)) {
1912 if (tevent_req_is_in_progress(req)) {
1913 switch (state->result->type) {
1914 case TLDAP_RES_SEARCH_ENTRY:
1915 case TLDAP_RES_SEARCH_REFERENCE:
1918 return TLDAP_OPERATIONS_ERROR;
1922 *pmsg = talloc_move(mem_ctx, &state->result);
1923 return TLDAP_SUCCESS;
1926 struct tldap_search_all_state {
1927 struct tldap_message **msgs;
1928 struct tldap_message *result;
1931 static void tldap_search_all_done(struct tevent_req *subreq);
1933 struct tevent_req *tldap_search_all_send(
1934 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1935 struct tldap_context *ld, const char *base, int scope,
1936 const char *filter, const char **attrs, int num_attrs, int attrsonly,
1937 struct tldap_control *sctrls, int num_sctrls,
1938 struct tldap_control *cctrls, int num_cctrls,
1939 int timelimit, int sizelimit, int deref)
1941 struct tevent_req *req, *subreq;
1942 struct tldap_search_all_state *state;
1944 req = tevent_req_create(mem_ctx, &state,
1945 struct tldap_search_all_state);
1950 subreq = tldap_search_send(state, ev, ld, base, scope, filter,
1951 attrs, num_attrs, attrsonly,
1952 sctrls, num_sctrls, cctrls, num_cctrls,
1953 timelimit, sizelimit, deref);
1954 if (tevent_req_nomem(subreq, req)) {
1955 return tevent_req_post(req, ev);
1957 tevent_req_set_callback(subreq, tldap_search_all_done, req);
1961 static void tldap_search_all_done(struct tevent_req *subreq)
1963 struct tevent_req *req = tevent_req_callback_data(
1964 subreq, struct tevent_req);
1965 struct tldap_search_all_state *state = tevent_req_data(
1966 req, struct tldap_search_all_state);
1967 struct tldap_message *msg, **tmp;
1972 rc = tldap_search_recv(subreq, state, &msg);
1973 /* No TALLOC_FREE(subreq), this is multi-step */
1974 if (tevent_req_ldap_error(req, rc)) {
1978 msgtype = tldap_msg_type(msg);
1979 if (msgtype == TLDAP_RES_SEARCH_RESULT) {
1980 state->result = msg;
1981 tevent_req_done(req);
1985 num_msgs = talloc_array_length(state->msgs);
1987 tmp = talloc_realloc(state, state->msgs, struct tldap_message *,
1989 if (tevent_req_nomem(tmp, req)) {
1993 state->msgs[num_msgs] = talloc_move(state->msgs, &msg);
1996 TLDAPRC tldap_search_all_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1997 struct tldap_message ***msgs,
1998 struct tldap_message **result)
2000 struct tldap_search_all_state *state = tevent_req_data(
2001 req, struct tldap_search_all_state);
2004 if (tevent_req_is_ldap_error(req, &rc)) {
2009 *msgs = talloc_move(mem_ctx, &state->msgs);
2011 if (result != NULL) {
2012 *result = talloc_move(mem_ctx, &state->result);
2015 return TLDAP_SUCCESS;
2018 TLDAPRC tldap_search(struct tldap_context *ld,
2019 const char *base, int scope, const char *filter,
2020 const char **attrs, int num_attrs, int attrsonly,
2021 struct tldap_control *sctrls, int num_sctrls,
2022 struct tldap_control *cctrls, int num_cctrls,
2023 int timelimit, int sizelimit, int deref,
2024 TALLOC_CTX *mem_ctx, struct tldap_message ***pmsgs)
2027 struct tevent_context *ev;
2028 struct tevent_req *req;
2029 TLDAPRC rc = TLDAP_NO_MEMORY;
2030 struct tldap_message **msgs;
2031 struct tldap_message *result;
2033 if (tldap_pending_reqs(ld)) {
2037 frame = talloc_stackframe();
2039 ev = samba_tevent_context_init(frame);
2043 req = tldap_search_all_send(frame, ev, ld, base, scope, filter,
2044 attrs, num_attrs, attrsonly,
2045 sctrls, num_sctrls, cctrls, num_cctrls,
2046 timelimit, sizelimit, deref);
2050 if (!tevent_req_poll(req, ev)) {
2051 rc = TLDAP_OPERATIONS_ERROR;
2054 rc = tldap_search_all_recv(req, frame, &msgs, &result);
2056 if (!TLDAP_RC_IS_SUCCESS(rc)) {
2060 TALLOC_FREE(ld->last_msg);
2061 ld->last_msg = talloc_move(ld, &result);
2063 if (pmsgs != NULL) {
2064 *pmsgs = talloc_move(mem_ctx, &msgs);
2071 static bool tldap_parse_search_entry(struct tldap_message *msg)
2073 int num_attribs = 0;
2075 if (msg->type != TLDAP_RES_SEARCH_ENTRY) {
2078 if (!asn1_start_tag(msg->data, TLDAP_RES_SEARCH_ENTRY)) {
2084 if (!asn1_read_OctetString_talloc(msg, msg->data, &msg->dn)) return false;
2086 if (msg->dn == NULL) {
2091 * Attributes: We overallocate msg->attribs by one, so that while
2092 * looping over the attributes we can directly parse into the last
2093 * array element. Same for the values in the inner loop.
2096 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
2097 if (msg->attribs == NULL) {
2101 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
2102 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
2103 struct tldap_attribute *attrib;
2106 attrib = &msg->attribs[num_attribs];
2107 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
2108 if (attrib->values == NULL) {
2111 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
2112 if (!asn1_read_OctetString_talloc(msg->attribs, msg->data,
2113 &attrib->name)) return false;
2114 if (!asn1_start_tag(msg->data, ASN1_SET)) return false;
2116 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
2117 if (!asn1_read_OctetString(msg->data, msg,
2118 &attrib->values[num_values])) return false;
2120 attrib->values = talloc_realloc(
2121 msg->attribs, attrib->values, DATA_BLOB,
2123 if (attrib->values == NULL) {
2128 attrib->values = talloc_realloc(msg->attribs, attrib->values,
2129 DATA_BLOB, num_values);
2130 attrib->num_values = num_values;
2132 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SET */
2133 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SEQUENCE(0) */
2134 msg->attribs = talloc_realloc(
2135 msg, msg->attribs, struct tldap_attribute,
2137 if (msg->attribs == NULL) {
2142 msg->attribs = talloc_realloc(
2143 msg, msg->attribs, struct tldap_attribute, num_attribs);
2144 return asn1_end_tag(msg->data);
2147 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
2149 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2156 bool tldap_entry_attributes(struct tldap_message *msg,
2157 struct tldap_attribute **attributes,
2158 int *num_attributes)
2160 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2163 *attributes = msg->attribs;
2164 *num_attributes = talloc_array_length(msg->attribs);
2168 static bool tldap_decode_controls(struct tldap_req_state *state)
2170 struct tldap_message *msg = state->result;
2171 struct asn1_data *data = msg->data;
2172 struct tldap_control *sctrls = NULL;
2173 int num_controls = 0;
2176 msg->res_sctrls = NULL;
2178 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
2182 if (!asn1_start_tag(data, ASN1_CONTEXT(0))) goto out;
2184 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
2185 struct tldap_control *c;
2188 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
2190 if (sctrls == NULL) {
2193 c = &sctrls[num_controls];
2195 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) goto out;
2196 if (!asn1_read_OctetString_talloc(msg, data, &oid)) goto out;
2197 if (asn1_has_error(data) || (oid == NULL)) {
2201 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2202 if (!asn1_read_BOOLEAN(data, &c->critical)) goto out;
2204 c->critical = false;
2206 c->value = data_blob_null;
2207 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2208 !asn1_read_OctetString(data, msg, &c->value)) {
2211 if (!asn1_end_tag(data)) goto out; /* ASN1_SEQUENCE(0) */
2216 if (!asn1_end_tag(data)) goto out; /* ASN1_CONTEXT(0) */
2223 msg->res_sctrls = sctrls;
2225 TALLOC_FREE(sctrls);
2230 static void tldap_simple_done(struct tevent_req *subreq, int type)
2232 struct tevent_req *req = tevent_req_callback_data(
2233 subreq, struct tevent_req);
2234 struct tldap_req_state *state = tevent_req_data(
2235 req, struct tldap_req_state);
2238 rc = tldap_msg_recv(subreq, state, &state->result);
2239 TALLOC_FREE(subreq);
2240 if (tevent_req_ldap_error(req, rc)) {
2243 if (state->result->type != type) {
2244 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
2247 if (!asn1_start_tag(state->result->data, state->result->type) ||
2248 !tldap_decode_response(state) ||
2249 !asn1_end_tag(state->result->data) ||
2250 !tldap_decode_controls(state)) {
2251 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
2254 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr)) {
2255 tevent_req_ldap_error(req, state->result->lderr);
2258 tevent_req_done(req);
2261 static TLDAPRC tldap_simple_recv(struct tevent_req *req)
2264 if (tevent_req_is_ldap_error(req, &rc)) {
2267 return TLDAP_SUCCESS;
2270 static void tldap_add_done(struct tevent_req *subreq);
2272 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2273 struct tevent_context *ev,
2274 struct tldap_context *ld,
2276 struct tldap_mod *attributes,
2278 struct tldap_control *sctrls,
2280 struct tldap_control *cctrls,
2283 struct tevent_req *req, *subreq;
2284 struct tldap_req_state *state;
2287 req = tldap_req_create(mem_ctx, ld, &state);
2292 if (!asn1_push_tag(state->out, TLDAP_REQ_ADD)) goto err;
2293 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2294 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2296 for (i=0; i<num_attributes; i++) {
2297 struct tldap_mod *attrib = &attributes[i];
2298 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2299 if (!asn1_write_OctetString(state->out, attrib->attribute,
2300 strlen(attrib->attribute))) goto err;
2301 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2302 for (j=0; j<attrib->num_values; j++) {
2303 if (!asn1_write_OctetString(state->out,
2304 attrib->values[j].data,
2305 attrib->values[j].length)) goto err;
2307 if (!asn1_pop_tag(state->out)) goto err;
2308 if (!asn1_pop_tag(state->out)) goto err;
2311 if (!asn1_pop_tag(state->out)) goto err;
2312 if (!asn1_pop_tag(state->out)) goto err;
2314 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2315 sctrls, num_sctrls);
2316 if (tevent_req_nomem(subreq, req)) {
2317 return tevent_req_post(req, ev);
2319 tevent_req_set_callback(subreq, tldap_add_done, req);
2324 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2325 return tevent_req_post(req, ev);
2328 static void tldap_add_done(struct tevent_req *subreq)
2330 tldap_simple_done(subreq, TLDAP_RES_ADD);
2333 TLDAPRC tldap_add_recv(struct tevent_req *req)
2335 return tldap_simple_recv(req);
2338 TLDAPRC tldap_add(struct tldap_context *ld, const char *dn,
2339 struct tldap_mod *attributes, int num_attributes,
2340 struct tldap_control *sctrls, int num_sctrls,
2341 struct tldap_control *cctrls, int num_cctrls)
2343 TALLOC_CTX *frame = talloc_stackframe();
2344 struct tevent_context *ev;
2345 struct tevent_req *req;
2346 TLDAPRC rc = TLDAP_NO_MEMORY;
2348 ev = samba_tevent_context_init(frame);
2352 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2353 sctrls, num_sctrls, cctrls, num_cctrls);
2357 if (!tevent_req_poll(req, ev)) {
2358 rc = TLDAP_OPERATIONS_ERROR;
2361 rc = tldap_add_recv(req);
2362 tldap_save_msg(ld, req);
2368 static void tldap_modify_done(struct tevent_req *subreq);
2370 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2371 struct tevent_context *ev,
2372 struct tldap_context *ld,
2374 struct tldap_mod *mods, int num_mods,
2375 struct tldap_control *sctrls,
2377 struct tldap_control *cctrls,
2380 struct tevent_req *req, *subreq;
2381 struct tldap_req_state *state;
2384 req = tldap_req_create(mem_ctx, ld, &state);
2389 if (!asn1_push_tag(state->out, TLDAP_REQ_MODIFY)) goto err;
2390 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2391 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2393 for (i=0; i<num_mods; i++) {
2394 struct tldap_mod *mod = &mods[i];
2395 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2396 if (!asn1_write_enumerated(state->out, mod->mod_op)) goto err;
2397 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2398 if (!asn1_write_OctetString(state->out, mod->attribute,
2399 strlen(mod->attribute))) goto err;
2400 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2401 for (j=0; j<mod->num_values; j++) {
2402 if (!asn1_write_OctetString(state->out,
2403 mod->values[j].data,
2404 mod->values[j].length)) goto err;
2406 if (!asn1_pop_tag(state->out)) goto err;
2407 if (!asn1_pop_tag(state->out)) goto err;
2408 if (!asn1_pop_tag(state->out)) goto err;
2411 if (!asn1_pop_tag(state->out)) goto err;
2412 if (!asn1_pop_tag(state->out)) goto err;
2414 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2415 sctrls, num_sctrls);
2416 if (tevent_req_nomem(subreq, req)) {
2417 return tevent_req_post(req, ev);
2419 tevent_req_set_callback(subreq, tldap_modify_done, req);
2424 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2425 return tevent_req_post(req, ev);
2428 static void tldap_modify_done(struct tevent_req *subreq)
2430 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2433 TLDAPRC tldap_modify_recv(struct tevent_req *req)
2435 return tldap_simple_recv(req);
2438 TLDAPRC tldap_modify(struct tldap_context *ld, const char *dn,
2439 struct tldap_mod *mods, int num_mods,
2440 struct tldap_control *sctrls, int num_sctrls,
2441 struct tldap_control *cctrls, int num_cctrls)
2443 TALLOC_CTX *frame = talloc_stackframe();
2444 struct tevent_context *ev;
2445 struct tevent_req *req;
2446 TLDAPRC rc = TLDAP_NO_MEMORY;
2448 ev = samba_tevent_context_init(frame);
2452 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2453 sctrls, num_sctrls, cctrls, num_cctrls);
2457 if (!tevent_req_poll(req, ev)) {
2458 rc = TLDAP_OPERATIONS_ERROR;
2461 rc = tldap_modify_recv(req);
2462 tldap_save_msg(ld, req);
2468 static void tldap_delete_done(struct tevent_req *subreq);
2470 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2471 struct tevent_context *ev,
2472 struct tldap_context *ld,
2474 struct tldap_control *sctrls,
2476 struct tldap_control *cctrls,
2479 struct tevent_req *req, *subreq;
2480 struct tldap_req_state *state;
2482 req = tldap_req_create(mem_ctx, ld, &state);
2487 if (!asn1_push_tag(state->out, TLDAP_REQ_DELETE)) goto err;
2488 if (!asn1_write(state->out, dn, strlen(dn))) goto err;
2489 if (!asn1_pop_tag(state->out)) goto err;
2491 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2492 sctrls, num_sctrls);
2493 if (tevent_req_nomem(subreq, req)) {
2494 return tevent_req_post(req, ev);
2496 tevent_req_set_callback(subreq, tldap_delete_done, req);
2501 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2502 return tevent_req_post(req, ev);
2505 static void tldap_delete_done(struct tevent_req *subreq)
2507 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2510 TLDAPRC tldap_delete_recv(struct tevent_req *req)
2512 return tldap_simple_recv(req);
2515 TLDAPRC tldap_delete(struct tldap_context *ld, const char *dn,
2516 struct tldap_control *sctrls, int num_sctrls,
2517 struct tldap_control *cctrls, int num_cctrls)
2519 TALLOC_CTX *frame = talloc_stackframe();
2520 struct tevent_context *ev;
2521 struct tevent_req *req;
2522 TLDAPRC rc = TLDAP_NO_MEMORY;
2524 ev = samba_tevent_context_init(frame);
2528 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2529 cctrls, num_cctrls);
2533 if (!tevent_req_poll(req, ev)) {
2534 rc = TLDAP_OPERATIONS_ERROR;
2537 rc = tldap_delete_recv(req);
2538 tldap_save_msg(ld, req);
2544 int tldap_msg_id(const struct tldap_message *msg)
2549 int tldap_msg_type(const struct tldap_message *msg)
2554 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2559 return msg->res_matcheddn;
2562 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2567 return msg->res_diagnosticmessage;
2570 const char *tldap_msg_referral(struct tldap_message *msg)
2575 return msg->res_referral;
2578 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2579 struct tldap_control **sctrls)
2586 *sctrls = msg->res_sctrls;
2587 *num_sctrls = talloc_array_length(msg->res_sctrls);
2590 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2592 return ld->last_msg;
2595 static const struct { TLDAPRC rc; const char *string; } tldaprc_errmap[] =
2599 { TLDAP_OPERATIONS_ERROR,
2600 "TLDAP_OPERATIONS_ERROR" },
2601 { TLDAP_PROTOCOL_ERROR,
2602 "TLDAP_PROTOCOL_ERROR" },
2603 { TLDAP_TIMELIMIT_EXCEEDED,
2604 "TLDAP_TIMELIMIT_EXCEEDED" },
2605 { TLDAP_SIZELIMIT_EXCEEDED,
2606 "TLDAP_SIZELIMIT_EXCEEDED" },
2607 { TLDAP_COMPARE_FALSE,
2608 "TLDAP_COMPARE_FALSE" },
2609 { TLDAP_COMPARE_TRUE,
2610 "TLDAP_COMPARE_TRUE" },
2611 { TLDAP_STRONG_AUTH_NOT_SUPPORTED,
2612 "TLDAP_STRONG_AUTH_NOT_SUPPORTED" },
2613 { TLDAP_STRONG_AUTH_REQUIRED,
2614 "TLDAP_STRONG_AUTH_REQUIRED" },
2617 { TLDAP_ADMINLIMIT_EXCEEDED,
2618 "TLDAP_ADMINLIMIT_EXCEEDED" },
2619 { TLDAP_UNAVAILABLE_CRITICAL_EXTENSION,
2620 "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION" },
2621 { TLDAP_CONFIDENTIALITY_REQUIRED,
2622 "TLDAP_CONFIDENTIALITY_REQUIRED" },
2623 { TLDAP_SASL_BIND_IN_PROGRESS,
2624 "TLDAP_SASL_BIND_IN_PROGRESS" },
2625 { TLDAP_NO_SUCH_ATTRIBUTE,
2626 "TLDAP_NO_SUCH_ATTRIBUTE" },
2627 { TLDAP_UNDEFINED_TYPE,
2628 "TLDAP_UNDEFINED_TYPE" },
2629 { TLDAP_INAPPROPRIATE_MATCHING,
2630 "TLDAP_INAPPROPRIATE_MATCHING" },
2631 { TLDAP_CONSTRAINT_VIOLATION,
2632 "TLDAP_CONSTRAINT_VIOLATION" },
2633 { TLDAP_TYPE_OR_VALUE_EXISTS,
2634 "TLDAP_TYPE_OR_VALUE_EXISTS" },
2635 { TLDAP_INVALID_SYNTAX,
2636 "TLDAP_INVALID_SYNTAX" },
2637 { TLDAP_NO_SUCH_OBJECT,
2638 "TLDAP_NO_SUCH_OBJECT" },
2639 { TLDAP_ALIAS_PROBLEM,
2640 "TLDAP_ALIAS_PROBLEM" },
2641 { TLDAP_INVALID_DN_SYNTAX,
2642 "TLDAP_INVALID_DN_SYNTAX" },
2645 { TLDAP_ALIAS_DEREF_PROBLEM,
2646 "TLDAP_ALIAS_DEREF_PROBLEM" },
2647 { TLDAP_INAPPROPRIATE_AUTH,
2648 "TLDAP_INAPPROPRIATE_AUTH" },
2649 { TLDAP_INVALID_CREDENTIALS,
2650 "TLDAP_INVALID_CREDENTIALS" },
2651 { TLDAP_INSUFFICIENT_ACCESS,
2652 "TLDAP_INSUFFICIENT_ACCESS" },
2655 { TLDAP_UNAVAILABLE,
2656 "TLDAP_UNAVAILABLE" },
2657 { TLDAP_UNWILLING_TO_PERFORM,
2658 "TLDAP_UNWILLING_TO_PERFORM" },
2659 { TLDAP_LOOP_DETECT,
2660 "TLDAP_LOOP_DETECT" },
2661 { TLDAP_NAMING_VIOLATION,
2662 "TLDAP_NAMING_VIOLATION" },
2663 { TLDAP_OBJECT_CLASS_VIOLATION,
2664 "TLDAP_OBJECT_CLASS_VIOLATION" },
2665 { TLDAP_NOT_ALLOWED_ON_NONLEAF,
2666 "TLDAP_NOT_ALLOWED_ON_NONLEAF" },
2667 { TLDAP_NOT_ALLOWED_ON_RDN,
2668 "TLDAP_NOT_ALLOWED_ON_RDN" },
2669 { TLDAP_ALREADY_EXISTS,
2670 "TLDAP_ALREADY_EXISTS" },
2671 { TLDAP_NO_OBJECT_CLASS_MODS,
2672 "TLDAP_NO_OBJECT_CLASS_MODS" },
2673 { TLDAP_RESULTS_TOO_LARGE,
2674 "TLDAP_RESULTS_TOO_LARGE" },
2675 { TLDAP_AFFECTS_MULTIPLE_DSAS,
2676 "TLDAP_AFFECTS_MULTIPLE_DSAS" },
2679 { TLDAP_SERVER_DOWN,
2680 "TLDAP_SERVER_DOWN" },
2681 { TLDAP_LOCAL_ERROR,
2682 "TLDAP_LOCAL_ERROR" },
2683 { TLDAP_ENCODING_ERROR,
2684 "TLDAP_ENCODING_ERROR" },
2685 { TLDAP_DECODING_ERROR,
2686 "TLDAP_DECODING_ERROR" },
2689 { TLDAP_AUTH_UNKNOWN,
2690 "TLDAP_AUTH_UNKNOWN" },
2691 { TLDAP_FILTER_ERROR,
2692 "TLDAP_FILTER_ERROR" },
2693 { TLDAP_USER_CANCELLED,
2694 "TLDAP_USER_CANCELLED" },
2695 { TLDAP_PARAM_ERROR,
2696 "TLDAP_PARAM_ERROR" },
2698 "TLDAP_NO_MEMORY" },
2699 { TLDAP_CONNECT_ERROR,
2700 "TLDAP_CONNECT_ERROR" },
2701 { TLDAP_NOT_SUPPORTED,
2702 "TLDAP_NOT_SUPPORTED" },
2703 { TLDAP_CONTROL_NOT_FOUND,
2704 "TLDAP_CONTROL_NOT_FOUND" },
2705 { TLDAP_NO_RESULTS_RETURNED,
2706 "TLDAP_NO_RESULTS_RETURNED" },
2707 { TLDAP_MORE_RESULTS_TO_RETURN,
2708 "TLDAP_MORE_RESULTS_TO_RETURN" },
2709 { TLDAP_CLIENT_LOOP,
2710 "TLDAP_CLIENT_LOOP" },
2711 { TLDAP_REFERRAL_LIMIT_EXCEEDED,
2712 "TLDAP_REFERRAL_LIMIT_EXCEEDED" },
2715 const char *tldap_rc2string(TLDAPRC rc)
2719 for (i=0; i<ARRAY_SIZE(tldaprc_errmap); i++) {
2720 if (TLDAP_RC_EQUAL(rc, tldaprc_errmap[i].rc)) {
2721 return tldaprc_errmap[i].string;
2725 return "Unknown LDAP Error";