2 Unix SMB/CIFS implementation.
3 Infrastructure for async ldap client requests
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "system/network.h"
23 #include "system/locale.h"
24 #include "lib/util/talloc_stack.h"
25 #include "lib/util/samba_util.h"
26 #include "lib/util_tsock.h"
27 #include "../lib/util/asn1.h"
28 #include "../lib/tsocket/tsocket.h"
29 #include "../lib/util/tevent_unix.h"
31 static TLDAPRC tldap_simple_recv(struct tevent_req *req);
33 #define TEVENT_TLDAP_RC_MAGIC (0x87bcd26e)
35 bool tevent_req_ldap_error(struct tevent_req *req, TLDAPRC rc)
39 if (TLDAP_RC_IS_SUCCESS(rc)) {
43 err = TEVENT_TLDAP_RC_MAGIC;
45 err |= TLDAP_RC_V(rc);
47 return tevent_req_error(req, err);
50 bool tevent_req_is_ldap_error(struct tevent_req *req, TLDAPRC *perr)
52 enum tevent_req_state state;
55 if (!tevent_req_is_error(req, &state, &err)) {
59 case TEVENT_REQ_TIMED_OUT:
60 *perr = TLDAP_TIMEOUT;
62 case TEVENT_REQ_NO_MEMORY:
63 *perr = TLDAP_NO_MEMORY;
65 case TEVENT_REQ_USER_ERROR:
66 if ((err >> 32) != TEVENT_TLDAP_RC_MAGIC) {
69 *perr = TLDAP_RC(err & 0xffffffff);
72 *perr = TLDAP_OPERATIONS_ERROR;
78 struct tldap_ctx_attribute {
83 struct tldap_context {
85 struct tstream_context *conn;
88 struct tevent_queue *outgoing;
89 struct tevent_req **pending;
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(struct tldap_context *ld,
137 enum tldap_debug_level level,
138 const char *fmt, ...)
144 if (ld->log_fn == NULL) {
148 ld->log_fn(ld->log_private, level, fmt, ap);
152 static int tldap_next_msgid(struct tldap_context *ld)
156 result = ld->msgid++;
157 if (ld->msgid == 2147483647) {
163 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
165 struct tldap_context *ctx;
168 ctx = talloc_zero(mem_ctx, struct tldap_context);
172 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
179 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
180 if (ctx->outgoing == NULL) {
187 bool tldap_connection_ok(struct tldap_context *ld)
192 return !ld->server_down;
195 static size_t tldap_pending_reqs(struct tldap_context *ld)
197 return talloc_array_length(ld->pending);
200 struct tstream_context *tldap_get_tstream(struct tldap_context *ld)
205 void tldap_set_tstream(struct tldap_context *ld,
206 struct tstream_context *stream)
211 static struct tldap_ctx_attribute *tldap_context_findattr(
212 struct tldap_context *ld, const char *name)
216 num_attrs = talloc_array_length(ld->ctx_attrs);
218 for (i=0; i<num_attrs; i++) {
219 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
220 return &ld->ctx_attrs[i];
226 bool tldap_context_setattr(struct tldap_context *ld,
227 const char *name, const void *_pptr)
229 struct tldap_ctx_attribute *tmp, *attr;
232 void **pptr = (void **)discard_const_p(void,_pptr);
234 attr = tldap_context_findattr(ld, name);
237 * We don't actually delete attrs, we don't expect tons of
238 * attributes being shuffled around.
240 TALLOC_FREE(attr->ptr);
242 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
248 tmpname = talloc_strdup(ld, name);
249 if (tmpname == NULL) {
253 num_attrs = talloc_array_length(ld->ctx_attrs);
255 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
258 TALLOC_FREE(tmpname);
261 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
263 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
265 tmp[num_attrs].ptr = NULL;
272 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
274 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
282 struct read_ldap_state {
287 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
288 static void read_ldap_done(struct tevent_req *subreq);
290 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
291 struct tevent_context *ev,
292 struct tstream_context *conn)
294 struct tevent_req *req, *subreq;
295 struct read_ldap_state *state;
297 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
303 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
305 if (tevent_req_nomem(subreq, req)) {
306 return tevent_req_post(req, ev);
308 tevent_req_set_callback(subreq, read_ldap_done, req);
312 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
314 struct read_ldap_state *state = talloc_get_type_abort(
315 private_data, struct read_ldap_state);
320 /* We've been here, we're done */
325 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
327 if (buf[0] != 0x30) {
332 if ((len & 0x80) == 0) {
337 lensize = (len & 0x7f);
341 /* Please get us the full length */
344 if (buflen > 2 + lensize) {
348 if (buflen != 2 + lensize) {
352 for (i=0; i<lensize; i++) {
353 len = (len << 8) | buf[2+i];
358 static void read_ldap_done(struct tevent_req *subreq)
360 struct tevent_req *req = tevent_req_callback_data(
361 subreq, struct tevent_req);
362 struct read_ldap_state *state = tevent_req_data(
363 req, struct read_ldap_state);
367 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
370 tevent_req_error(req, err);
373 tevent_req_done(req);
376 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
377 uint8_t **pbuf, int *perrno)
379 struct read_ldap_state *state = tevent_req_data(
380 req, struct read_ldap_state);
382 if (tevent_req_is_unix_error(req, perrno)) {
385 *pbuf = talloc_move(mem_ctx, &state->buf);
386 return talloc_get_size(*pbuf);
389 struct tldap_msg_state {
390 struct tldap_context *ld;
391 struct tevent_context *ev;
395 struct asn1_data *data;
399 static bool tldap_push_controls(struct asn1_data *data,
400 struct tldap_control *sctrls,
405 if ((sctrls == NULL) || (num_sctrls == 0)) {
409 if (!asn1_push_tag(data, ASN1_CONTEXT(0))) return false;
411 for (i=0; i<num_sctrls; i++) {
412 struct tldap_control *c = &sctrls[i];
413 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
414 if (!asn1_write_OctetString(data, c->oid, strlen(c->oid))) return false;
416 if (!asn1_write_BOOLEAN(data, true)) return false;
418 if (c->value.data != NULL) {
419 if (!asn1_write_OctetString(data, c->value.data,
420 c->value.length)) return false;
422 if (!asn1_pop_tag(data)) return false; /* ASN1_SEQUENCE(0) */
425 return asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
428 static void tldap_msg_sent(struct tevent_req *subreq);
429 static void tldap_msg_received(struct tevent_req *subreq);
431 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
432 struct tevent_context *ev,
433 struct tldap_context *ld,
434 int id, struct asn1_data *data,
435 struct tldap_control *sctrls,
438 struct tevent_req *req, *subreq;
439 struct tldap_msg_state *state;
442 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
445 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
453 if (state->ld->server_down) {
454 tevent_req_ldap_error(req, TLDAP_SERVER_DOWN);
455 return tevent_req_post(req, ev);
458 if (!tldap_push_controls(data, sctrls, num_sctrls)) {
459 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
460 return tevent_req_post(req, ev);
464 if (!asn1_pop_tag(data)) {
465 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
466 return tevent_req_post(req, ev);
469 if (!asn1_blob(data, &blob)) {
470 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
471 return tevent_req_post(req, ev);
474 state->iov.iov_base = (void *)blob.data;
475 state->iov.iov_len = blob.length;
477 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
479 if (tevent_req_nomem(subreq, req)) {
480 return tevent_req_post(req, ev);
482 tevent_req_set_callback(subreq, tldap_msg_sent, req);
486 static void tldap_msg_unset_pending(struct tevent_req *req)
488 struct tldap_msg_state *state = tevent_req_data(
489 req, struct tldap_msg_state);
490 struct tldap_context *ld = state->ld;
491 int num_pending = tldap_pending_reqs(ld);
494 tevent_req_set_cleanup_fn(req, NULL);
496 for (i=0; i<num_pending; i++) {
497 if (req == ld->pending[i]) {
501 if (i == num_pending) {
503 * Something's seriously broken. Just returning here is the
504 * right thing nevertheless, the point of this routine is to
505 * remove ourselves from cli->pending.
510 if (num_pending == 1) {
511 TALLOC_FREE(ld->pending);
516 * Remove ourselves from the cli->pending array
518 if (num_pending > 1) {
519 ld->pending[i] = ld->pending[num_pending-1];
523 * No NULL check here, we're shrinking by sizeof(void *), and
524 * talloc_realloc just adjusts the size for this.
526 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
530 static void tldap_msg_cleanup(struct tevent_req *req,
531 enum tevent_req_state req_state)
533 tldap_msg_unset_pending(req);
536 static bool tldap_msg_set_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;
541 struct tevent_req **pending;
545 num_pending = tldap_pending_reqs(ld);
547 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
549 if (pending == NULL) {
552 pending[num_pending] = req;
553 ld->pending = pending;
554 tevent_req_set_cleanup_fn(req, tldap_msg_cleanup);
556 if (ld->read_req != NULL) {
561 * We're the first one, add the read_ldap request that waits for the
562 * answer from the server
564 ld->read_req = read_ldap_send(ld->pending, state->ev, ld->conn);
565 if (ld->read_req == NULL) {
566 tldap_msg_unset_pending(req);
569 tevent_req_set_callback(ld->read_req, tldap_msg_received, ld);
573 static void tldap_msg_sent(struct tevent_req *subreq)
575 struct tevent_req *req = tevent_req_callback_data(
576 subreq, struct tevent_req);
577 struct tldap_msg_state *state = tevent_req_data(
578 req, struct tldap_msg_state);
582 nwritten = tstream_writev_queue_recv(subreq, &err);
584 if (nwritten == -1) {
585 state->ld->server_down = true;
586 tevent_req_ldap_error(req, TLDAP_SERVER_DOWN);
590 if (!tldap_msg_set_pending(req)) {
596 static int tldap_msg_msgid(struct tevent_req *req)
598 struct tldap_msg_state *state = tevent_req_data(
599 req, struct tldap_msg_state);
604 static void tldap_msg_received(struct tevent_req *subreq)
606 struct tldap_context *ld = tevent_req_callback_data(
607 subreq, struct tldap_context);
608 struct tevent_req *req;
609 struct tldap_msg_state *state;
610 struct asn1_data *data;
620 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
623 if (received == -1) {
624 ld->server_down = true;
625 status = TLDAP_SERVER_DOWN;
629 data = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH);
631 status = TLDAP_NO_MEMORY;
634 asn1_load_nocopy(data, inbuf, received);
637 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
638 ok &= asn1_read_Integer(data, &id);
639 ok &= asn1_peek_uint8(data, &type);
642 status = TLDAP_PROTOCOL_ERROR;
646 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
647 "type %d\n", id, (int)type);
649 num_pending = talloc_array_length(ld->pending);
651 for (i=0; i<num_pending; i++) {
652 if (id == tldap_msg_msgid(ld->pending[i])) {
656 if (i == num_pending) {
657 /* Dump unexpected reply */
658 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
659 "No request pending for msg %d\n", id);
665 req = ld->pending[i];
666 state = tevent_req_data(req, struct tldap_msg_state);
668 state->inbuf = talloc_move(state, &inbuf);
669 state->data = talloc_move(state, &data);
671 tldap_msg_unset_pending(req);
672 num_pending = talloc_array_length(ld->pending);
674 tevent_req_defer_callback(req, state->ev);
675 tevent_req_done(req);
678 if (num_pending == 0) {
682 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
683 ld->read_req = read_ldap_send(ld->pending, state->ev, ld->conn);
684 if (ld->read_req == NULL) {
685 status = TLDAP_NO_MEMORY;
688 tevent_req_set_callback(ld->read_req, tldap_msg_received, ld);
692 while (talloc_array_length(ld->pending) > 0) {
693 req = ld->pending[0];
694 state = tevent_req_data(req, struct tldap_msg_state);
695 tevent_req_defer_callback(req, state->ev);
696 tevent_req_ldap_error(req, status);
700 static TLDAPRC tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
701 struct tldap_message **pmsg)
703 struct tldap_msg_state *state = tevent_req_data(
704 req, struct tldap_msg_state);
705 struct tldap_message *msg;
709 if (tevent_req_is_ldap_error(req, &err)) {
713 if (!asn1_peek_uint8(state->data, &msgtype)) {
714 return TLDAP_PROTOCOL_ERROR;
718 return TLDAP_SUCCESS;
721 msg = talloc_zero(mem_ctx, struct tldap_message);
723 return TLDAP_NO_MEMORY;
727 msg->inbuf = talloc_move(msg, &state->inbuf);
728 msg->data = talloc_move(msg, &state->data);
732 return TLDAP_SUCCESS;
735 struct tldap_req_state {
737 struct asn1_data *out;
738 struct tldap_message *result;
741 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
742 struct tldap_context *ld,
743 struct tldap_req_state **pstate)
745 struct tevent_req *req;
746 struct tldap_req_state *state;
748 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
752 state->out = asn1_init(state, ASN1_MAX_TREE_DEPTH);
753 if (state->out == NULL) {
756 state->id = tldap_next_msgid(ld);
758 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
759 if (!asn1_write_Integer(state->out, state->id)) goto err;
770 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
772 struct tldap_req_state *state = tevent_req_data(
773 req, struct tldap_req_state);
775 TALLOC_FREE(ld->last_msg);
776 ld->last_msg = talloc_move(ld, &state->result);
779 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
781 char *result = talloc_array(mem_ctx, char, blob.length+1);
783 if (result == NULL) {
787 memcpy(result, blob.data, blob.length);
788 result[blob.length] = '\0';
792 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
793 struct asn1_data *data,
798 if (!asn1_read_OctetString(data, mem_ctx, &string))
801 result = blob2string_talloc(mem_ctx, string);
803 data_blob_free(&string);
805 if (result == NULL) {
812 static bool tldap_decode_controls(struct tldap_req_state *state);
814 static bool tldap_decode_response(struct tldap_req_state *state)
816 struct asn1_data *data = state->result->data;
817 struct tldap_message *msg = state->result;
821 ok &= asn1_read_enumerated(data, &rc);
823 msg->lderr = TLDAP_RC(rc);
826 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
827 ok &= asn1_read_OctetString_talloc(msg, data,
828 &msg->res_diagnosticmessage);
830 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
831 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
832 ok &= asn1_read_OctetString_talloc(msg, data,
834 ok &= asn1_end_tag(data);
836 msg->res_referral = NULL;
842 static void tldap_sasl_bind_done(struct tevent_req *subreq);
844 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
845 struct tevent_context *ev,
846 struct tldap_context *ld,
848 const char *mechanism,
850 struct tldap_control *sctrls,
852 struct tldap_control *cctrls,
855 struct tevent_req *req, *subreq;
856 struct tldap_req_state *state;
858 req = tldap_req_create(mem_ctx, ld, &state);
867 if (!asn1_push_tag(state->out, TLDAP_REQ_BIND)) goto err;
868 if (!asn1_write_Integer(state->out, ld->ld_version)) goto err;
869 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
871 if (mechanism == NULL) {
872 if (!asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0))) goto err;
873 if (!asn1_write(state->out, creds->data, creds->length)) goto err;
874 if (!asn1_pop_tag(state->out)) goto err;
876 if (!asn1_push_tag(state->out, ASN1_CONTEXT(3))) goto err;
877 if (!asn1_write_OctetString(state->out, mechanism,
878 strlen(mechanism))) goto err;
879 if ((creds != NULL) && (creds->data != NULL)) {
880 if (!asn1_write_OctetString(state->out, creds->data,
881 creds->length)) goto err;
883 if (!asn1_pop_tag(state->out)) goto err;
886 if (!asn1_pop_tag(state->out)) goto err;
888 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
890 if (tevent_req_nomem(subreq, req)) {
891 return tevent_req_post(req, ev);
893 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
898 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
899 return tevent_req_post(req, ev);
902 static void tldap_sasl_bind_done(struct tevent_req *subreq)
904 struct tevent_req *req = tevent_req_callback_data(
905 subreq, struct tevent_req);
906 struct tldap_req_state *state = tevent_req_data(
907 req, struct tldap_req_state);
911 rc = tldap_msg_recv(subreq, state, &state->result);
913 if (tevent_req_ldap_error(req, rc)) {
916 if (state->result->type != TLDAP_RES_BIND) {
917 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
921 ok = asn1_start_tag(state->result->data, TLDAP_RES_BIND);
922 ok &= tldap_decode_response(state);
924 if (asn1_peek_tag(state->result->data, ASN1_CONTEXT_SIMPLE(7))) {
927 ok &= asn1_start_tag(state->result->data,
928 ASN1_CONTEXT_SIMPLE(7));
933 len = asn1_tag_remaining(state->result->data);
938 state->result->res_serverSaslCreds =
939 data_blob_talloc(state->result, NULL, len);
940 if (state->result->res_serverSaslCreds.data == NULL) {
944 ok = asn1_read(state->result->data,
945 state->result->res_serverSaslCreds.data,
946 state->result->res_serverSaslCreds.length);
948 ok &= asn1_end_tag(state->result->data);
951 ok &= asn1_end_tag(state->result->data);
957 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr) &&
958 !TLDAP_RC_EQUAL(state->result->lderr,
959 TLDAP_SASL_BIND_IN_PROGRESS)) {
960 tevent_req_ldap_error(req, state->result->lderr);
963 tevent_req_done(req);
967 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
971 TLDAPRC tldap_sasl_bind_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
972 DATA_BLOB *serverSaslCreds)
974 struct tldap_req_state *state = tevent_req_data(
975 req, struct tldap_req_state);
978 if (tevent_req_is_ldap_error(req, &rc)) {
982 if (serverSaslCreds != NULL) {
983 serverSaslCreds->data = talloc_move(
984 mem_ctx, &state->result->res_serverSaslCreds.data);
985 serverSaslCreds->length =
986 state->result->res_serverSaslCreds.length;
989 return state->result->lderr;
992 TLDAPRC tldap_sasl_bind(struct tldap_context *ld,
994 const char *mechanism,
996 struct tldap_control *sctrls,
998 struct tldap_control *cctrls,
1000 TALLOC_CTX *mem_ctx,
1001 DATA_BLOB *serverSaslCreds)
1003 TALLOC_CTX *frame = talloc_stackframe();
1004 struct tevent_context *ev;
1005 struct tevent_req *req;
1006 TLDAPRC rc = TLDAP_NO_MEMORY;
1008 ev = samba_tevent_context_init(frame);
1012 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
1013 sctrls, num_sctrls, cctrls, num_cctrls);
1017 if (!tevent_req_poll(req, ev)) {
1018 rc = TLDAP_OPERATIONS_ERROR;
1021 rc = tldap_sasl_bind_recv(req, mem_ctx, serverSaslCreds);
1022 tldap_save_msg(ld, req);
1028 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
1029 struct tevent_context *ev,
1030 struct tldap_context *ld,
1036 if (passwd != NULL) {
1037 cred.data = discard_const_p(uint8_t, passwd);
1038 cred.length = strlen(passwd);
1040 cred.data = discard_const_p(uint8_t, "");
1043 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
1047 TLDAPRC tldap_simple_bind_recv(struct tevent_req *req)
1049 return tldap_sasl_bind_recv(req, NULL, NULL);
1052 TLDAPRC tldap_simple_bind(struct tldap_context *ld, const char *dn,
1057 if (passwd != NULL) {
1058 cred.data = discard_const_p(uint8_t, passwd);
1059 cred.length = strlen(passwd);
1061 cred.data = discard_const_p(uint8_t, "");
1064 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0,
1068 /*****************************************************************************/
1070 /* can't use isalpha() as only a strict set is valid for LDAP */
1072 static bool tldap_is_alpha(char c)
1074 return (((c >= 'a') && (c <= 'z')) || \
1075 ((c >= 'A') && (c <= 'Z')));
1078 static bool tldap_is_adh(char c)
1080 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
1083 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
1084 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
1085 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
1086 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
1087 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
1088 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
1089 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
1090 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
1091 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
1092 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
1094 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
1095 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
1096 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1099 /* oid's should be numerical only in theory,
1100 * but apparently some broken servers may have alphanum aliases instead.
1101 * Do like openldap libraries and allow alphanum aliases for oids, but
1102 * do not allow Tagging options in that case.
1104 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1106 bool is_oid = false;
1110 /* first char has stricter rules */
1113 } else if (!tldap_is_alpha(*s)) {
1114 /* bad first char */
1118 for (i = 1; i < len; i++) {
1121 if (isdigit(s[i])) {
1134 if (tldap_is_adh(s[i])) {
1141 /* no tagging options */
1148 if ((i + 1) == len) {
1166 /* this function copies the value until the closing parenthesis is found. */
1167 static char *tldap_get_val(TALLOC_CTX *memctx,
1168 const char *value, const char **_s)
1170 const char *s = value;
1172 /* find terminator */
1175 if (s && (*(s - 1) == '\\')) {
1180 if (!s || !(*s == ')')) {
1181 /* malformed filter */
1187 return talloc_strndup(memctx, value, s - value);
1190 static int tldap_hex2char(const char *x)
1192 if (isxdigit(x[0]) && isxdigit(x[1])) {
1193 const char h1 = x[0], h2 = x[1];
1196 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1197 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1198 else if (h1 >= '0') c = h1 - (int)'0';
1200 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1201 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1202 else if (h2 >= '0') c += h2 - (int)'0';
1210 static bool tldap_find_first_star(const char *val, const char **star)
1214 for (s = val; *s; s++) {
1217 if (isxdigit(s[1]) && isxdigit(s[2])) {
1221 /* not hex based escape, check older syntax */
1230 /* invalid escape sequence */
1235 /* end of val, nothing found */
1245 /* string ended without closing parenthesis, filter is malformed */
1249 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1254 for (i = 0,p = 0; i < *val_len; i++) {
1260 /* these must be escaped */
1264 if (!value[i + 1]) {
1270 /* LDAPv3 escaped */
1271 c = tldap_hex2char(&value[i]);
1272 if (c >= 0 && c < 256) {
1279 /* LDAPv2 escaped */
1285 value[p] = value[i];
1296 value[p] = value[i];
1305 static bool tldap_push_filter_basic(struct tldap_context *ld,
1306 struct asn1_data *data,
1308 static bool tldap_push_filter_substring(struct tldap_context *ld,
1309 struct asn1_data *data,
1312 static bool tldap_push_filter_int(struct tldap_context *ld,
1313 struct asn1_data *data,
1316 const char *s = *_s;
1320 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1321 "Incomplete or malformed filter\n");
1326 /* we are right after a parenthesis,
1327 * find out what op we have at hand */
1330 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1331 if (!asn1_push_tag(data, TLDAP_FILTER_AND)) return false;
1336 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1337 if (!asn1_push_tag(data, TLDAP_FILTER_OR)) return false;
1342 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1343 if (!asn1_push_tag(data, TLDAP_FILTER_NOT)) return false;
1345 ret = tldap_push_filter_int(ld, data, &s);
1349 if (!asn1_pop_tag(data)) return false;
1354 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1355 "Invalid parenthesis '%c'\n", *s);
1359 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1360 "Invalid filter termination\n");
1364 ret = tldap_push_filter_basic(ld, data, &s);
1371 /* only and/or filters get here.
1372 * go through the list of filters */
1375 /* RFC 4526: empty and/or */
1376 if (!asn1_pop_tag(data)) return false;
1381 ret = tldap_push_filter_int(ld, data, &s);
1387 /* end of list, return */
1388 if (!asn1_pop_tag(data)) return false;
1395 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1396 "Incomplete or malformed filter\n");
1401 if (asn1_has_error(data)) {
1410 static bool tldap_push_filter_basic(struct tldap_context *ld,
1411 struct asn1_data *data,
1414 TALLOC_CTX *tmpctx = talloc_tos();
1415 const char *s = *_s;
1423 size_t type_len = 0;
1426 bool write_octect = true;
1429 eq = strchr(s, '=');
1431 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1432 "Invalid filter, missing equal sign\n");
1441 if (!asn1_push_tag(data, TLDAP_FILTER_LE)) return false;
1445 if (!asn1_push_tag(data, TLDAP_FILTER_GE)) return false;
1449 if (!asn1_push_tag(data, TLDAP_FILTER_APX)) return false;
1453 if (!asn1_push_tag(data, TLDAP_FILTER_EXT)) return false;
1454 write_octect = false;
1460 if (*s == ':') { /* [:dn]:rule:= value */
1462 /* malformed filter */
1466 } else { /* type[:dn][:rule]:= value */
1468 dn = strchr(s, ':');
1469 type_len = dn - type;
1470 if (dn == e) { /* type:= value */
1477 rule = strchr(dn, ':');
1481 if ((rule == dn + 1) || rule + 1 == e) {
1482 /* malformed filter, contains "::" */
1486 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1491 /* malformed filter. With two
1492 * optionals, the first must be "dn"
1505 if (!type && !dn && !rule) {
1506 /* malformed filter, there must be at least one */
1511 MatchingRuleAssertion ::= SEQUENCE {
1512 matchingRule [1] MatchingRuleID OPTIONAL,
1513 type [2] AttributeDescription OPTIONAL,
1514 matchValue [3] AssertionValue,
1515 dnAttributes [4] BOOLEAN DEFAULT FALSE
1519 /* check and add rule */
1521 ret = tldap_is_attrdesc(rule, e - rule, true);
1525 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1))) return false;
1526 if (!asn1_write(data, rule, e - rule)) return false;
1527 if (!asn1_pop_tag(data)) return false;
1530 /* check and add type */
1532 ret = tldap_is_attrdesc(type, type_len, false);
1536 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2))) return false;
1537 if (!asn1_write(data, type, type_len)) return false;
1538 if (!asn1_pop_tag(data)) return false;
1541 uval = tldap_get_val(tmpctx, val, _s);
1545 uval_len = *_s - val;
1546 ret = tldap_unescape_inplace(uval, &uval_len);
1551 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3))) return false;
1552 if (!asn1_write(data, uval, uval_len)) return false;
1553 if (!asn1_pop_tag(data)) return false;
1555 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4))) return false;
1556 if (!asn1_write_uint8(data, dn?1:0)) return false;
1557 if (!asn1_pop_tag(data)) return false;
1563 ret = tldap_is_attrdesc(s, e - s, false);
1568 if (strncmp(val, "*)", 2) == 0) {
1570 if (!asn1_push_tag(data, TLDAP_FILTER_PRES)) return false;
1571 if (!asn1_write(data, s, e - s)) return false;
1573 write_octect = false;
1577 ret = tldap_find_first_star(val, &star);
1583 if (!asn1_push_tag(data, TLDAP_FILTER_SUB)) return false;
1584 if (!asn1_write_OctetString(data, s, e - s)) return false;
1585 ret = tldap_push_filter_substring(ld, data, val, &s);
1590 write_octect = false;
1594 /* if nothing else, then it is just equality */
1595 if (!asn1_push_tag(data, TLDAP_FILTER_EQ)) return false;
1596 write_octect = true;
1601 uval = tldap_get_val(tmpctx, val, _s);
1605 uval_len = *_s - val;
1606 ret = tldap_unescape_inplace(uval, &uval_len);
1611 if (!asn1_write_OctetString(data, s, e - s)) return false;
1612 if (!asn1_write_OctetString(data, uval, uval_len)) return false;
1615 if (asn1_has_error(data)) {
1618 return asn1_pop_tag(data);
1621 static bool tldap_push_filter_substring(struct tldap_context *ld,
1622 struct asn1_data *data,
1626 TALLOC_CTX *tmpctx = talloc_tos();
1627 bool initial = true;
1634 SubstringFilter ::= SEQUENCE {
1635 type AttributeDescription,
1636 -- at least one must be present
1637 substrings SEQUENCE OF CHOICE {
1638 initial [0] LDAPString,
1640 final [2] LDAPString } }
1642 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
1645 ret = tldap_find_first_star(val, &star);
1649 chunk_len = star - val;
1653 if (!initial && chunk_len == 0) {
1654 /* found '**', which is illegal */
1670 if (initial && chunk_len == 0) {
1676 chunk = talloc_strndup(tmpctx, val, chunk_len);
1680 ret = tldap_unescape_inplace(chunk, &chunk_len);
1687 if (!asn1_push_tag(data, TLDAP_SUB_INI)) return false;
1690 if (!asn1_push_tag(data, TLDAP_SUB_ANY)) return false;
1694 if (!asn1_push_tag(data, TLDAP_SUB_FIN)) return false;
1700 if (!asn1_write(data, chunk, chunk_len)) return false;
1701 if (!asn1_pop_tag(data)) return false;
1705 } while (*star == '*');
1709 /* end of sequence */
1710 return asn1_pop_tag(data);
1713 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1714 * around parenthesis, we do not allow any spaces (except in values of
1715 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1716 * leading or trailing spaces where allowed.
1718 static bool tldap_push_filter(struct tldap_context *ld,
1719 struct asn1_data *data,
1722 const char *s = filter;
1725 ret = tldap_push_filter_int(ld, data, &s);
1727 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1728 "Incomplete or malformed filter\n");
1734 /*****************************************************************************/
1736 static void tldap_search_done(struct tevent_req *subreq);
1738 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1739 struct tevent_context *ev,
1740 struct tldap_context *ld,
1741 const char *base, int scope,
1746 struct tldap_control *sctrls,
1748 struct tldap_control *cctrls,
1754 struct tevent_req *req, *subreq;
1755 struct tldap_req_state *state;
1758 req = tldap_req_create(mem_ctx, ld, &state);
1763 if (!asn1_push_tag(state->out, TLDAP_REQ_SEARCH)) goto encoding_error;
1764 if (!asn1_write_OctetString(state->out, base, strlen(base))) goto encoding_error;
1765 if (!asn1_write_enumerated(state->out, scope)) goto encoding_error;
1766 if (!asn1_write_enumerated(state->out, deref)) goto encoding_error;
1767 if (!asn1_write_Integer(state->out, sizelimit)) goto encoding_error;
1768 if (!asn1_write_Integer(state->out, timelimit)) goto encoding_error;
1769 if (!asn1_write_BOOLEAN(state->out, attrsonly)) goto encoding_error;
1771 if (!tldap_push_filter(ld, state->out, filter)) {
1772 goto encoding_error;
1775 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto encoding_error;
1776 for (i=0; i<num_attrs; i++) {
1777 if (!asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]))) goto encoding_error;
1779 if (!asn1_pop_tag(state->out)) goto encoding_error;
1780 if (!asn1_pop_tag(state->out)) goto encoding_error;
1782 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1783 sctrls, num_sctrls);
1784 if (tevent_req_nomem(subreq, req)) {
1785 return tevent_req_post(req, ev);
1787 tevent_req_set_callback(subreq, tldap_search_done, req);
1791 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
1792 return tevent_req_post(req, ev);
1795 static void tldap_search_done(struct tevent_req *subreq)
1797 struct tevent_req *req = tevent_req_callback_data(
1798 subreq, struct tevent_req);
1799 struct tldap_req_state *state = tevent_req_data(
1800 req, struct tldap_req_state);
1803 rc = tldap_msg_recv(subreq, state, &state->result);
1804 if (tevent_req_ldap_error(req, rc)) {
1807 switch (state->result->type) {
1808 case TLDAP_RES_SEARCH_ENTRY:
1809 case TLDAP_RES_SEARCH_REFERENCE:
1810 if (!tldap_msg_set_pending(subreq)) {
1811 tevent_req_oom(req);
1814 tevent_req_notify_callback(req);
1816 case TLDAP_RES_SEARCH_RESULT:
1817 TALLOC_FREE(subreq);
1818 if (!asn1_start_tag(state->result->data,
1819 state->result->type) ||
1820 !tldap_decode_response(state) ||
1821 !asn1_end_tag(state->result->data) ||
1822 !tldap_decode_controls(state)) {
1823 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
1826 tevent_req_done(req);
1829 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
1834 TLDAPRC tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1835 struct tldap_message **pmsg)
1837 struct tldap_req_state *state = tevent_req_data(
1838 req, struct tldap_req_state);
1841 if (!tevent_req_is_in_progress(req)
1842 && tevent_req_is_ldap_error(req, &rc)) {
1846 if (tevent_req_is_in_progress(req)) {
1847 switch (state->result->type) {
1848 case TLDAP_RES_SEARCH_ENTRY:
1849 case TLDAP_RES_SEARCH_REFERENCE:
1852 return TLDAP_OPERATIONS_ERROR;
1856 *pmsg = talloc_move(mem_ctx, &state->result);
1857 return TLDAP_SUCCESS;
1860 struct tldap_search_all_state {
1861 struct tldap_message **msgs;
1862 struct tldap_message *result;
1865 static void tldap_search_all_done(struct tevent_req *subreq);
1867 struct tevent_req *tldap_search_all_send(
1868 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1869 struct tldap_context *ld, const char *base, int scope,
1870 const char *filter, const char **attrs, int num_attrs, int attrsonly,
1871 struct tldap_control *sctrls, int num_sctrls,
1872 struct tldap_control *cctrls, int num_cctrls,
1873 int timelimit, int sizelimit, int deref)
1875 struct tevent_req *req, *subreq;
1876 struct tldap_search_all_state *state;
1878 req = tevent_req_create(mem_ctx, &state,
1879 struct tldap_search_all_state);
1884 subreq = tldap_search_send(state, ev, ld, base, scope, filter,
1885 attrs, num_attrs, attrsonly,
1886 sctrls, num_sctrls, cctrls, num_cctrls,
1887 timelimit, sizelimit, deref);
1888 if (tevent_req_nomem(subreq, req)) {
1889 return tevent_req_post(req, ev);
1891 tevent_req_set_callback(subreq, tldap_search_all_done, req);
1895 static void tldap_search_all_done(struct tevent_req *subreq)
1897 struct tevent_req *req = tevent_req_callback_data(
1898 subreq, struct tevent_req);
1899 struct tldap_search_all_state *state = tevent_req_data(
1900 req, struct tldap_search_all_state);
1901 struct tldap_message *msg, **tmp;
1906 rc = tldap_search_recv(subreq, state, &msg);
1907 /* No TALLOC_FREE(subreq), this is multi-step */
1908 if (tevent_req_ldap_error(req, rc)) {
1912 msgtype = tldap_msg_type(msg);
1913 if (msgtype == TLDAP_RES_SEARCH_RESULT) {
1914 state->result = msg;
1915 tevent_req_done(req);
1919 num_msgs = talloc_array_length(state->msgs);
1921 tmp = talloc_realloc(state, state->msgs, struct tldap_message *,
1923 if (tevent_req_nomem(tmp, req)) {
1927 state->msgs[num_msgs] = talloc_move(state->msgs, &msg);
1930 TLDAPRC tldap_search_all_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1931 struct tldap_message ***msgs,
1932 struct tldap_message **result)
1934 struct tldap_search_all_state *state = tevent_req_data(
1935 req, struct tldap_search_all_state);
1938 if (tevent_req_is_ldap_error(req, &rc)) {
1943 *msgs = talloc_move(mem_ctx, &state->msgs);
1945 if (result != NULL) {
1946 *result = talloc_move(mem_ctx, &state->result);
1949 return TLDAP_SUCCESS;
1952 TLDAPRC tldap_search(struct tldap_context *ld,
1953 const char *base, int scope, const char *filter,
1954 const char **attrs, int num_attrs, int attrsonly,
1955 struct tldap_control *sctrls, int num_sctrls,
1956 struct tldap_control *cctrls, int num_cctrls,
1957 int timelimit, int sizelimit, int deref,
1958 TALLOC_CTX *mem_ctx, struct tldap_message ***pmsgs)
1961 struct tevent_context *ev;
1962 struct tevent_req *req;
1963 TLDAPRC rc = TLDAP_NO_MEMORY;
1964 struct tldap_message **msgs;
1965 struct tldap_message *result;
1967 if (tldap_pending_reqs(ld)) {
1971 frame = talloc_stackframe();
1973 ev = samba_tevent_context_init(frame);
1977 req = tldap_search_all_send(frame, ev, ld, base, scope, filter,
1978 attrs, num_attrs, attrsonly,
1979 sctrls, num_sctrls, cctrls, num_cctrls,
1980 timelimit, sizelimit, deref);
1984 if (!tevent_req_poll(req, ev)) {
1985 rc = TLDAP_OPERATIONS_ERROR;
1988 rc = tldap_search_all_recv(req, frame, &msgs, &result);
1990 if (!TLDAP_RC_IS_SUCCESS(rc)) {
1994 TALLOC_FREE(ld->last_msg);
1995 ld->last_msg = talloc_move(ld, &result);
1997 if (pmsgs != NULL) {
1998 *pmsgs = talloc_move(mem_ctx, &msgs);
2005 static bool tldap_parse_search_entry(struct tldap_message *msg)
2007 int num_attribs = 0;
2009 if (msg->type != TLDAP_RES_SEARCH_ENTRY) {
2012 if (!asn1_start_tag(msg->data, TLDAP_RES_SEARCH_ENTRY)) {
2018 if (!asn1_read_OctetString_talloc(msg, msg->data, &msg->dn)) return false;
2020 if (msg->dn == NULL) {
2025 * Attributes: We overallocate msg->attribs by one, so that while
2026 * looping over the attributes we can directly parse into the last
2027 * array element. Same for the values in the inner loop.
2030 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
2031 if (msg->attribs == NULL) {
2035 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
2036 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
2037 struct tldap_attribute *attrib;
2040 attrib = &msg->attribs[num_attribs];
2041 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
2042 if (attrib->values == NULL) {
2045 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
2046 if (!asn1_read_OctetString_talloc(msg->attribs, msg->data,
2047 &attrib->name)) return false;
2048 if (!asn1_start_tag(msg->data, ASN1_SET)) return false;
2050 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
2051 if (!asn1_read_OctetString(msg->data, msg,
2052 &attrib->values[num_values])) return false;
2054 attrib->values = talloc_realloc(
2055 msg->attribs, attrib->values, DATA_BLOB,
2057 if (attrib->values == NULL) {
2062 attrib->values = talloc_realloc(msg->attribs, attrib->values,
2063 DATA_BLOB, num_values);
2064 attrib->num_values = num_values;
2066 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SET */
2067 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SEQUENCE(0) */
2068 msg->attribs = talloc_realloc(
2069 msg, msg->attribs, struct tldap_attribute,
2071 if (msg->attribs == NULL) {
2076 msg->attribs = talloc_realloc(
2077 msg, msg->attribs, struct tldap_attribute, num_attribs);
2078 return asn1_end_tag(msg->data);
2081 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
2083 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2090 bool tldap_entry_attributes(struct tldap_message *msg,
2091 struct tldap_attribute **attributes,
2092 int *num_attributes)
2094 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
2097 *attributes = msg->attribs;
2098 *num_attributes = talloc_array_length(msg->attribs);
2102 static bool tldap_decode_controls(struct tldap_req_state *state)
2104 struct tldap_message *msg = state->result;
2105 struct asn1_data *data = msg->data;
2106 struct tldap_control *sctrls = NULL;
2107 int num_controls = 0;
2110 msg->res_sctrls = NULL;
2112 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
2116 if (!asn1_start_tag(data, ASN1_CONTEXT(0))) goto out;
2118 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
2119 struct tldap_control *c;
2122 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
2124 if (sctrls == NULL) {
2127 c = &sctrls[num_controls];
2129 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) goto out;
2130 if (!asn1_read_OctetString_talloc(msg, data, &oid)) goto out;
2131 if (asn1_has_error(data) || (oid == NULL)) {
2135 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2136 if (!asn1_read_BOOLEAN(data, &c->critical)) goto out;
2138 c->critical = false;
2140 c->value = data_blob_null;
2141 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2142 !asn1_read_OctetString(data, msg, &c->value)) {
2145 if (!asn1_end_tag(data)) goto out; /* ASN1_SEQUENCE(0) */
2150 if (!asn1_end_tag(data)) goto out; /* ASN1_CONTEXT(0) */
2157 msg->res_sctrls = sctrls;
2159 TALLOC_FREE(sctrls);
2164 static void tldap_simple_done(struct tevent_req *subreq, int type)
2166 struct tevent_req *req = tevent_req_callback_data(
2167 subreq, struct tevent_req);
2168 struct tldap_req_state *state = tevent_req_data(
2169 req, struct tldap_req_state);
2172 rc = tldap_msg_recv(subreq, state, &state->result);
2173 TALLOC_FREE(subreq);
2174 if (tevent_req_ldap_error(req, rc)) {
2177 if (state->result->type != type) {
2178 tevent_req_ldap_error(req, TLDAP_PROTOCOL_ERROR);
2181 if (!asn1_start_tag(state->result->data, state->result->type) ||
2182 !tldap_decode_response(state) ||
2183 !asn1_end_tag(state->result->data) ||
2184 !tldap_decode_controls(state)) {
2185 tevent_req_ldap_error(req, TLDAP_DECODING_ERROR);
2188 if (!TLDAP_RC_IS_SUCCESS(state->result->lderr)) {
2189 tevent_req_ldap_error(req, state->result->lderr);
2192 tevent_req_done(req);
2195 static TLDAPRC tldap_simple_recv(struct tevent_req *req)
2198 if (tevent_req_is_ldap_error(req, &rc)) {
2201 return TLDAP_SUCCESS;
2204 static void tldap_add_done(struct tevent_req *subreq);
2206 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2207 struct tevent_context *ev,
2208 struct tldap_context *ld,
2210 struct tldap_mod *attributes,
2212 struct tldap_control *sctrls,
2214 struct tldap_control *cctrls,
2217 struct tevent_req *req, *subreq;
2218 struct tldap_req_state *state;
2221 req = tldap_req_create(mem_ctx, ld, &state);
2226 if (!asn1_push_tag(state->out, TLDAP_REQ_ADD)) goto err;
2227 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2228 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2230 for (i=0; i<num_attributes; i++) {
2231 struct tldap_mod *attrib = &attributes[i];
2232 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2233 if (!asn1_write_OctetString(state->out, attrib->attribute,
2234 strlen(attrib->attribute))) goto err;
2235 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2236 for (j=0; j<attrib->num_values; j++) {
2237 if (!asn1_write_OctetString(state->out,
2238 attrib->values[j].data,
2239 attrib->values[j].length)) goto err;
2241 if (!asn1_pop_tag(state->out)) goto err;
2242 if (!asn1_pop_tag(state->out)) goto err;
2245 if (!asn1_pop_tag(state->out)) goto err;
2246 if (!asn1_pop_tag(state->out)) goto err;
2248 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2249 sctrls, num_sctrls);
2250 if (tevent_req_nomem(subreq, req)) {
2251 return tevent_req_post(req, ev);
2253 tevent_req_set_callback(subreq, tldap_add_done, req);
2258 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2259 return tevent_req_post(req, ev);
2262 static void tldap_add_done(struct tevent_req *subreq)
2264 tldap_simple_done(subreq, TLDAP_RES_ADD);
2267 TLDAPRC tldap_add_recv(struct tevent_req *req)
2269 return tldap_simple_recv(req);
2272 TLDAPRC tldap_add(struct tldap_context *ld, const char *dn,
2273 struct tldap_mod *attributes, int num_attributes,
2274 struct tldap_control *sctrls, int num_sctrls,
2275 struct tldap_control *cctrls, int num_cctrls)
2277 TALLOC_CTX *frame = talloc_stackframe();
2278 struct tevent_context *ev;
2279 struct tevent_req *req;
2280 TLDAPRC rc = TLDAP_NO_MEMORY;
2282 ev = samba_tevent_context_init(frame);
2286 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2287 sctrls, num_sctrls, cctrls, num_cctrls);
2291 if (!tevent_req_poll(req, ev)) {
2292 rc = TLDAP_OPERATIONS_ERROR;
2295 rc = tldap_add_recv(req);
2296 tldap_save_msg(ld, req);
2302 static void tldap_modify_done(struct tevent_req *subreq);
2304 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2305 struct tevent_context *ev,
2306 struct tldap_context *ld,
2308 struct tldap_mod *mods, int num_mods,
2309 struct tldap_control *sctrls,
2311 struct tldap_control *cctrls,
2314 struct tevent_req *req, *subreq;
2315 struct tldap_req_state *state;
2318 req = tldap_req_create(mem_ctx, ld, &state);
2323 if (!asn1_push_tag(state->out, TLDAP_REQ_MODIFY)) goto err;
2324 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2325 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2327 for (i=0; i<num_mods; i++) {
2328 struct tldap_mod *mod = &mods[i];
2329 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2330 if (!asn1_write_enumerated(state->out, mod->mod_op)) goto err;
2331 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2332 if (!asn1_write_OctetString(state->out, mod->attribute,
2333 strlen(mod->attribute))) goto err;
2334 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2335 for (j=0; j<mod->num_values; j++) {
2336 if (!asn1_write_OctetString(state->out,
2337 mod->values[j].data,
2338 mod->values[j].length)) goto err;
2340 if (!asn1_pop_tag(state->out)) goto err;
2341 if (!asn1_pop_tag(state->out)) goto err;
2342 if (!asn1_pop_tag(state->out)) goto err;
2345 if (!asn1_pop_tag(state->out)) goto err;
2346 if (!asn1_pop_tag(state->out)) goto err;
2348 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2349 sctrls, num_sctrls);
2350 if (tevent_req_nomem(subreq, req)) {
2351 return tevent_req_post(req, ev);
2353 tevent_req_set_callback(subreq, tldap_modify_done, req);
2358 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2359 return tevent_req_post(req, ev);
2362 static void tldap_modify_done(struct tevent_req *subreq)
2364 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2367 TLDAPRC tldap_modify_recv(struct tevent_req *req)
2369 return tldap_simple_recv(req);
2372 TLDAPRC tldap_modify(struct tldap_context *ld, const char *dn,
2373 struct tldap_mod *mods, int num_mods,
2374 struct tldap_control *sctrls, int num_sctrls,
2375 struct tldap_control *cctrls, int num_cctrls)
2377 TALLOC_CTX *frame = talloc_stackframe();
2378 struct tevent_context *ev;
2379 struct tevent_req *req;
2380 TLDAPRC rc = TLDAP_NO_MEMORY;
2382 ev = samba_tevent_context_init(frame);
2386 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2387 sctrls, num_sctrls, cctrls, num_cctrls);
2391 if (!tevent_req_poll(req, ev)) {
2392 rc = TLDAP_OPERATIONS_ERROR;
2395 rc = tldap_modify_recv(req);
2396 tldap_save_msg(ld, req);
2402 static void tldap_delete_done(struct tevent_req *subreq);
2404 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2405 struct tevent_context *ev,
2406 struct tldap_context *ld,
2408 struct tldap_control *sctrls,
2410 struct tldap_control *cctrls,
2413 struct tevent_req *req, *subreq;
2414 struct tldap_req_state *state;
2416 req = tldap_req_create(mem_ctx, ld, &state);
2421 if (!asn1_push_tag(state->out, TLDAP_REQ_DELETE)) goto err;
2422 if (!asn1_write(state->out, dn, strlen(dn))) goto err;
2423 if (!asn1_pop_tag(state->out)) goto err;
2425 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2426 sctrls, num_sctrls);
2427 if (tevent_req_nomem(subreq, req)) {
2428 return tevent_req_post(req, ev);
2430 tevent_req_set_callback(subreq, tldap_delete_done, req);
2435 tevent_req_ldap_error(req, TLDAP_ENCODING_ERROR);
2436 return tevent_req_post(req, ev);
2439 static void tldap_delete_done(struct tevent_req *subreq)
2441 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2444 TLDAPRC tldap_delete_recv(struct tevent_req *req)
2446 return tldap_simple_recv(req);
2449 TLDAPRC tldap_delete(struct tldap_context *ld, const char *dn,
2450 struct tldap_control *sctrls, int num_sctrls,
2451 struct tldap_control *cctrls, int num_cctrls)
2453 TALLOC_CTX *frame = talloc_stackframe();
2454 struct tevent_context *ev;
2455 struct tevent_req *req;
2456 TLDAPRC rc = TLDAP_NO_MEMORY;
2458 ev = samba_tevent_context_init(frame);
2462 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2463 cctrls, num_cctrls);
2467 if (!tevent_req_poll(req, ev)) {
2468 rc = TLDAP_OPERATIONS_ERROR;
2471 rc = tldap_delete_recv(req);
2472 tldap_save_msg(ld, req);
2478 int tldap_msg_id(const struct tldap_message *msg)
2483 int tldap_msg_type(const struct tldap_message *msg)
2488 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2493 return msg->res_matcheddn;
2496 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2501 return msg->res_diagnosticmessage;
2504 const char *tldap_msg_referral(struct tldap_message *msg)
2509 return msg->res_referral;
2512 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2513 struct tldap_control **sctrls)
2520 *sctrls = msg->res_sctrls;
2521 *num_sctrls = talloc_array_length(msg->res_sctrls);
2524 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2526 return ld->last_msg;
2529 static const struct { TLDAPRC rc; const char *string; } tldaprc_errmap[] =
2533 { TLDAP_OPERATIONS_ERROR,
2534 "TLDAP_OPERATIONS_ERROR" },
2535 { TLDAP_PROTOCOL_ERROR,
2536 "TLDAP_PROTOCOL_ERROR" },
2537 { TLDAP_TIMELIMIT_EXCEEDED,
2538 "TLDAP_TIMELIMIT_EXCEEDED" },
2539 { TLDAP_SIZELIMIT_EXCEEDED,
2540 "TLDAP_SIZELIMIT_EXCEEDED" },
2541 { TLDAP_COMPARE_FALSE,
2542 "TLDAP_COMPARE_FALSE" },
2543 { TLDAP_COMPARE_TRUE,
2544 "TLDAP_COMPARE_TRUE" },
2545 { TLDAP_STRONG_AUTH_NOT_SUPPORTED,
2546 "TLDAP_STRONG_AUTH_NOT_SUPPORTED" },
2547 { TLDAP_STRONG_AUTH_REQUIRED,
2548 "TLDAP_STRONG_AUTH_REQUIRED" },
2551 { TLDAP_ADMINLIMIT_EXCEEDED,
2552 "TLDAP_ADMINLIMIT_EXCEEDED" },
2553 { TLDAP_UNAVAILABLE_CRITICAL_EXTENSION,
2554 "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION" },
2555 { TLDAP_CONFIDENTIALITY_REQUIRED,
2556 "TLDAP_CONFIDENTIALITY_REQUIRED" },
2557 { TLDAP_SASL_BIND_IN_PROGRESS,
2558 "TLDAP_SASL_BIND_IN_PROGRESS" },
2559 { TLDAP_NO_SUCH_ATTRIBUTE,
2560 "TLDAP_NO_SUCH_ATTRIBUTE" },
2561 { TLDAP_UNDEFINED_TYPE,
2562 "TLDAP_UNDEFINED_TYPE" },
2563 { TLDAP_INAPPROPRIATE_MATCHING,
2564 "TLDAP_INAPPROPRIATE_MATCHING" },
2565 { TLDAP_CONSTRAINT_VIOLATION,
2566 "TLDAP_CONSTRAINT_VIOLATION" },
2567 { TLDAP_TYPE_OR_VALUE_EXISTS,
2568 "TLDAP_TYPE_OR_VALUE_EXISTS" },
2569 { TLDAP_INVALID_SYNTAX,
2570 "TLDAP_INVALID_SYNTAX" },
2571 { TLDAP_NO_SUCH_OBJECT,
2572 "TLDAP_NO_SUCH_OBJECT" },
2573 { TLDAP_ALIAS_PROBLEM,
2574 "TLDAP_ALIAS_PROBLEM" },
2575 { TLDAP_INVALID_DN_SYNTAX,
2576 "TLDAP_INVALID_DN_SYNTAX" },
2579 { TLDAP_ALIAS_DEREF_PROBLEM,
2580 "TLDAP_ALIAS_DEREF_PROBLEM" },
2581 { TLDAP_INAPPROPRIATE_AUTH,
2582 "TLDAP_INAPPROPRIATE_AUTH" },
2583 { TLDAP_INVALID_CREDENTIALS,
2584 "TLDAP_INVALID_CREDENTIALS" },
2585 { TLDAP_INSUFFICIENT_ACCESS,
2586 "TLDAP_INSUFFICIENT_ACCESS" },
2589 { TLDAP_UNAVAILABLE,
2590 "TLDAP_UNAVAILABLE" },
2591 { TLDAP_UNWILLING_TO_PERFORM,
2592 "TLDAP_UNWILLING_TO_PERFORM" },
2593 { TLDAP_LOOP_DETECT,
2594 "TLDAP_LOOP_DETECT" },
2595 { TLDAP_NAMING_VIOLATION,
2596 "TLDAP_NAMING_VIOLATION" },
2597 { TLDAP_OBJECT_CLASS_VIOLATION,
2598 "TLDAP_OBJECT_CLASS_VIOLATION" },
2599 { TLDAP_NOT_ALLOWED_ON_NONLEAF,
2600 "TLDAP_NOT_ALLOWED_ON_NONLEAF" },
2601 { TLDAP_NOT_ALLOWED_ON_RDN,
2602 "TLDAP_NOT_ALLOWED_ON_RDN" },
2603 { TLDAP_ALREADY_EXISTS,
2604 "TLDAP_ALREADY_EXISTS" },
2605 { TLDAP_NO_OBJECT_CLASS_MODS,
2606 "TLDAP_NO_OBJECT_CLASS_MODS" },
2607 { TLDAP_RESULTS_TOO_LARGE,
2608 "TLDAP_RESULTS_TOO_LARGE" },
2609 { TLDAP_AFFECTS_MULTIPLE_DSAS,
2610 "TLDAP_AFFECTS_MULTIPLE_DSAS" },
2613 { TLDAP_SERVER_DOWN,
2614 "TLDAP_SERVER_DOWN" },
2615 { TLDAP_LOCAL_ERROR,
2616 "TLDAP_LOCAL_ERROR" },
2617 { TLDAP_ENCODING_ERROR,
2618 "TLDAP_ENCODING_ERROR" },
2619 { TLDAP_DECODING_ERROR,
2620 "TLDAP_DECODING_ERROR" },
2623 { TLDAP_AUTH_UNKNOWN,
2624 "TLDAP_AUTH_UNKNOWN" },
2625 { TLDAP_FILTER_ERROR,
2626 "TLDAP_FILTER_ERROR" },
2627 { TLDAP_USER_CANCELLED,
2628 "TLDAP_USER_CANCELLED" },
2629 { TLDAP_PARAM_ERROR,
2630 "TLDAP_PARAM_ERROR" },
2632 "TLDAP_NO_MEMORY" },
2633 { TLDAP_CONNECT_ERROR,
2634 "TLDAP_CONNECT_ERROR" },
2635 { TLDAP_NOT_SUPPORTED,
2636 "TLDAP_NOT_SUPPORTED" },
2637 { TLDAP_CONTROL_NOT_FOUND,
2638 "TLDAP_CONTROL_NOT_FOUND" },
2639 { TLDAP_NO_RESULTS_RETURNED,
2640 "TLDAP_NO_RESULTS_RETURNED" },
2641 { TLDAP_MORE_RESULTS_TO_RETURN,
2642 "TLDAP_MORE_RESULTS_TO_RETURN" },
2643 { TLDAP_CLIENT_LOOP,
2644 "TLDAP_CLIENT_LOOP" },
2645 { TLDAP_REFERRAL_LIMIT_EXCEEDED,
2646 "TLDAP_REFERRAL_LIMIT_EXCEEDED" },
2649 const char *tldap_rc2string(TLDAPRC rc)
2653 for (i=0; i<ARRAY_SIZE(tldaprc_errmap); i++) {
2654 if (TLDAP_RC_EQUAL(rc, tldaprc_errmap[i].rc)) {
2655 return tldaprc_errmap[i].string;
2659 return "Unknown LDAP Error";