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 "lib/util_tsock.h"
23 #include "../lib/util/asn1.h"
24 #include "../lib/tsocket/tsocket.h"
25 #include "../lib/util/tevent_unix.h"
27 static int tldap_simple_recv(struct tevent_req *req);
29 bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
31 enum tevent_req_state state;
34 if (!tevent_req_is_error(req, &state, &err)) {
38 case TEVENT_REQ_TIMED_OUT:
39 *perr = TLDAP_TIMEOUT;
41 case TEVENT_REQ_NO_MEMORY:
42 *perr = TLDAP_NO_MEMORY;
44 case TEVENT_REQ_USER_ERROR:
48 *perr = TLDAP_OPERATIONS_ERROR;
54 struct tldap_ctx_attribute {
59 struct tldap_context {
61 struct tstream_context *conn;
64 struct tevent_queue *outgoing;
65 struct tevent_req **pending;
67 /* For the sync wrappers we need something like get_last_error... */
68 struct tldap_message *last_msg;
71 void (*log_fn)(void *context, enum tldap_debug_level level,
72 const char *fmt, va_list ap);
75 struct tldap_ctx_attribute *ctx_attrs;
78 struct tldap_message {
79 struct asn1_data *data;
86 struct tldap_attribute *attribs;
88 /* Error data sent by the server */
91 char *res_diagnosticmessage;
93 struct tldap_control *res_sctrls;
95 /* Controls sent by the server */
96 struct tldap_control *ctrls;
99 void tldap_set_debug(struct tldap_context *ld,
100 void (*log_fn)(void *log_private,
101 enum tldap_debug_level level,
103 va_list ap) PRINTF_ATTRIBUTE(3,0),
107 ld->log_private = log_private;
110 static void tldap_debug(struct tldap_context *ld,
111 enum tldap_debug_level level,
112 const char *fmt, ...)
118 if (ld->log_fn == NULL) {
122 ld->log_fn(ld->log_private, level, fmt, ap);
126 static int tldap_next_msgid(struct tldap_context *ld)
130 result = ld->msgid++;
131 if (ld->msgid == 2147483647) {
137 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
139 struct tldap_context *ctx;
142 ctx = talloc_zero(mem_ctx, struct tldap_context);
146 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
153 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
154 if (ctx->outgoing == NULL) {
161 bool tldap_connection_ok(struct tldap_context *ld)
166 return !ld->server_down;
169 static struct tldap_ctx_attribute *tldap_context_findattr(
170 struct tldap_context *ld, const char *name)
174 num_attrs = talloc_array_length(ld->ctx_attrs);
176 for (i=0; i<num_attrs; i++) {
177 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
178 return &ld->ctx_attrs[i];
184 bool tldap_context_setattr(struct tldap_context *ld,
185 const char *name, const void *_pptr)
187 struct tldap_ctx_attribute *tmp, *attr;
190 void **pptr = (void **)discard_const_p(void,_pptr);
192 attr = tldap_context_findattr(ld, name);
195 * We don't actually delete attrs, we don't expect tons of
196 * attributes being shuffled around.
198 TALLOC_FREE(attr->ptr);
200 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
206 tmpname = talloc_strdup(ld, name);
207 if (tmpname == NULL) {
211 num_attrs = talloc_array_length(ld->ctx_attrs);
213 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
216 TALLOC_FREE(tmpname);
219 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
221 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
223 tmp[num_attrs].ptr = NULL;
230 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
232 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
240 struct read_ldap_state {
245 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
246 static void read_ldap_done(struct tevent_req *subreq);
248 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
249 struct tevent_context *ev,
250 struct tstream_context *conn)
252 struct tevent_req *req, *subreq;
253 struct read_ldap_state *state;
255 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
261 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
263 if (tevent_req_nomem(subreq, req)) {
264 return tevent_req_post(req, ev);
266 tevent_req_set_callback(subreq, read_ldap_done, req);
270 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
272 struct read_ldap_state *state = talloc_get_type_abort(
273 private_data, struct read_ldap_state);
278 /* We've been here, we're done */
283 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
285 if (buf[0] != 0x30) {
290 if ((len & 0x80) == 0) {
295 lensize = (len & 0x7f);
299 /* Please get us the full length */
302 if (buflen > 2 + lensize) {
306 if (buflen != 2 + lensize) {
310 for (i=0; i<lensize; i++) {
311 len = (len << 8) | buf[2+i];
316 static void read_ldap_done(struct tevent_req *subreq)
318 struct tevent_req *req = tevent_req_callback_data(
319 subreq, struct tevent_req);
320 struct read_ldap_state *state = tevent_req_data(
321 req, struct read_ldap_state);
325 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
328 tevent_req_error(req, err);
331 tevent_req_done(req);
334 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
335 uint8_t **pbuf, int *perrno)
337 struct read_ldap_state *state = tevent_req_data(
338 req, struct read_ldap_state);
340 if (tevent_req_is_unix_error(req, perrno)) {
343 *pbuf = talloc_move(mem_ctx, &state->buf);
344 return talloc_get_size(*pbuf);
347 struct tldap_msg_state {
348 struct tldap_context *ld;
349 struct tevent_context *ev;
353 struct asn1_data *data;
357 static bool tldap_push_controls(struct asn1_data *data,
358 struct tldap_control *sctrls,
363 if ((sctrls == NULL) || (num_sctrls == 0)) {
367 if (!asn1_push_tag(data, ASN1_CONTEXT(0))) return false;
369 for (i=0; i<num_sctrls; i++) {
370 struct tldap_control *c = &sctrls[i];
371 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
372 if (!asn1_write_OctetString(data, c->oid, strlen(c->oid))) return false;
374 if (!asn1_write_BOOLEAN(data, true)) return false;
376 if (c->value.data != NULL) {
377 if (!asn1_write_OctetString(data, c->value.data,
378 c->value.length)) return false;
380 if (!asn1_pop_tag(data)) return false; /* ASN1_SEQUENCE(0) */
383 return asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
386 static void tldap_msg_sent(struct tevent_req *subreq);
387 static void tldap_msg_received(struct tevent_req *subreq);
389 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
390 struct tevent_context *ev,
391 struct tldap_context *ld,
392 int id, struct asn1_data *data,
393 struct tldap_control *sctrls,
396 struct tevent_req *req, *subreq;
397 struct tldap_msg_state *state;
400 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
403 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
411 if (state->ld->server_down) {
412 tevent_req_error(req, TLDAP_SERVER_DOWN);
413 return tevent_req_post(req, ev);
416 if (!tldap_push_controls(data, sctrls, num_sctrls)) {
417 tevent_req_error(req, TLDAP_ENCODING_ERROR);
418 return tevent_req_post(req, ev);
422 if (!asn1_pop_tag(data)) {
423 tevent_req_error(req, TLDAP_ENCODING_ERROR);
424 return tevent_req_post(req, ev);
427 if (!asn1_blob(data, &blob)) {
428 tevent_req_error(req, TLDAP_ENCODING_ERROR);
429 return tevent_req_post(req, ev);
432 state->iov.iov_base = (void *)blob.data;
433 state->iov.iov_len = blob.length;
435 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
437 if (tevent_req_nomem(subreq, req)) {
438 return tevent_req_post(req, ev);
440 tevent_req_set_callback(subreq, tldap_msg_sent, req);
444 static void tldap_msg_unset_pending(struct tevent_req *req)
446 struct tldap_msg_state *state = tevent_req_data(
447 req, struct tldap_msg_state);
448 struct tldap_context *ld = state->ld;
449 int num_pending = talloc_array_length(ld->pending);
452 tevent_req_set_cleanup_fn(req, NULL);
454 if (num_pending == 1) {
455 TALLOC_FREE(ld->pending);
459 for (i=0; i<num_pending; i++) {
460 if (req == ld->pending[i]) {
464 if (i == num_pending) {
466 * Something's seriously broken. Just returning here is the
467 * right thing nevertheless, the point of this routine is to
468 * remove ourselves from cli->pending.
474 * Remove ourselves from the cli->pending array
476 if (num_pending > 1) {
477 ld->pending[i] = ld->pending[num_pending-1];
481 * No NULL check here, we're shrinking by sizeof(void *), and
482 * talloc_realloc just adjusts the size for this.
484 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
488 static void tldap_msg_cleanup(struct tevent_req *req,
489 enum tevent_req_state req_state)
492 case TEVENT_REQ_USER_ERROR:
493 case TEVENT_REQ_RECEIVED:
494 tldap_msg_unset_pending(req);
501 static bool tldap_msg_set_pending(struct tevent_req *req)
503 struct tldap_msg_state *state = tevent_req_data(
504 req, struct tldap_msg_state);
505 struct tldap_context *ld;
506 struct tevent_req **pending;
508 struct tevent_req *subreq;
511 num_pending = talloc_array_length(ld->pending);
513 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
515 if (pending == NULL) {
518 pending[num_pending] = req;
519 ld->pending = pending;
520 tevent_req_set_cleanup_fn(req, tldap_msg_cleanup);
522 if (num_pending > 0) {
527 * We're the first one, add the read_ldap request that waits for the
528 * answer from the server
530 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
531 if (subreq == NULL) {
532 tldap_msg_unset_pending(req);
535 tevent_req_set_callback(subreq, tldap_msg_received, ld);
539 static void tldap_msg_sent(struct tevent_req *subreq)
541 struct tevent_req *req = tevent_req_callback_data(
542 subreq, struct tevent_req);
543 struct tldap_msg_state *state = tevent_req_data(
544 req, struct tldap_msg_state);
548 nwritten = tstream_writev_queue_recv(subreq, &err);
550 if (nwritten == -1) {
551 state->ld->server_down = true;
552 tevent_req_error(req, TLDAP_SERVER_DOWN);
556 if (!tldap_msg_set_pending(req)) {
562 static int tldap_msg_msgid(struct tevent_req *req)
564 struct tldap_msg_state *state = tevent_req_data(
565 req, struct tldap_msg_state);
570 static void tldap_msg_received(struct tevent_req *subreq)
572 struct tldap_context *ld = tevent_req_callback_data(
573 subreq, struct tldap_context);
574 struct tevent_req *req;
575 struct tldap_msg_state *state;
576 struct asn1_data *data;
585 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
587 if (received == -1) {
588 status = TLDAP_SERVER_DOWN;
592 data = asn1_init(talloc_tos());
594 status = TLDAP_NO_MEMORY;
597 asn1_load_nocopy(data, inbuf, received);
600 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
601 ok &= asn1_read_Integer(data, &id);
602 ok &= asn1_peek_uint8(data, &type);
605 status = TLDAP_PROTOCOL_ERROR;
609 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
610 "type %d\n", id, (int)type);
612 num_pending = talloc_array_length(ld->pending);
614 for (i=0; i<num_pending; i++) {
615 if (id == tldap_msg_msgid(ld->pending[i])) {
619 if (i == num_pending) {
620 /* Dump unexpected reply */
621 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
622 "No request pending for msg %d\n", id);
628 req = ld->pending[i];
629 state = tevent_req_data(req, struct tldap_msg_state);
631 state->inbuf = talloc_move(state, &inbuf);
632 state->data = talloc_move(state, &data);
634 tldap_msg_unset_pending(req);
635 num_pending = talloc_array_length(ld->pending);
637 tevent_req_done(req);
640 if (num_pending == 0) {
643 if (talloc_array_length(ld->pending) > num_pending) {
645 * The callback functions called from tevent_req_done() above
646 * have put something on the pending queue. We don't have to
647 * trigger the read_ldap_send(), tldap_msg_set_pending() has
648 * done it for us already.
653 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
654 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
655 if (subreq == NULL) {
656 status = TLDAP_NO_MEMORY;
659 tevent_req_set_callback(subreq, tldap_msg_received, ld);
663 while (talloc_array_length(ld->pending) > 0) {
664 req = ld->pending[0];
665 state = tevent_req_data(req, struct tldap_msg_state);
666 tevent_req_defer_callback(req, state->ev);
667 tevent_req_error(req, status);
671 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
672 struct tldap_message **pmsg)
674 struct tldap_msg_state *state = tevent_req_data(
675 req, struct tldap_msg_state);
676 struct tldap_message *msg;
680 if (tevent_req_is_ldap_error(req, &err)) {
684 if (!asn1_peek_uint8(state->data, &msgtype)) {
685 return TLDAP_PROTOCOL_ERROR;
689 return TLDAP_SUCCESS;
692 msg = talloc_zero(mem_ctx, struct tldap_message);
694 return TLDAP_NO_MEMORY;
698 msg->inbuf = talloc_move(msg, &state->inbuf);
699 msg->data = talloc_move(msg, &state->data);
703 return TLDAP_SUCCESS;
706 struct tldap_req_state {
708 struct asn1_data *out;
709 struct tldap_message *result;
712 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
713 struct tldap_context *ld,
714 struct tldap_req_state **pstate)
716 struct tevent_req *req;
717 struct tldap_req_state *state;
719 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
723 state->out = asn1_init(state);
724 if (state->out == NULL) {
727 state->id = tldap_next_msgid(ld);
729 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
730 if (!asn1_write_Integer(state->out, state->id)) goto err;
741 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
743 struct tldap_req_state *state = tevent_req_data(
744 req, struct tldap_req_state);
746 TALLOC_FREE(ld->last_msg);
747 ld->last_msg = talloc_move(ld, &state->result);
750 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
752 char *result = talloc_array(mem_ctx, char, blob.length+1);
754 if (result == NULL) {
758 memcpy(result, blob.data, blob.length);
759 result[blob.length] = '\0';
763 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
764 struct asn1_data *data,
769 if (!asn1_read_OctetString(data, mem_ctx, &string))
772 result = blob2string_talloc(mem_ctx, string);
774 data_blob_free(&string);
776 if (result == NULL) {
783 static bool tldap_decode_controls(struct tldap_req_state *state);
785 static bool tldap_decode_response(struct tldap_req_state *state)
787 struct asn1_data *data = state->result->data;
788 struct tldap_message *msg = state->result;
791 ok &= asn1_read_enumerated(data, &msg->lderr);
792 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
793 ok &= asn1_read_OctetString_talloc(msg, data,
794 &msg->res_diagnosticmessage);
796 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
797 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
798 ok &= asn1_read_OctetString_talloc(msg, data,
800 ok &= asn1_end_tag(data);
802 msg->res_referral = NULL;
808 static void tldap_sasl_bind_done(struct tevent_req *subreq);
810 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
811 struct tevent_context *ev,
812 struct tldap_context *ld,
814 const char *mechanism,
816 struct tldap_control *sctrls,
818 struct tldap_control *cctrls,
821 struct tevent_req *req, *subreq;
822 struct tldap_req_state *state;
824 req = tldap_req_create(mem_ctx, ld, &state);
833 if (!asn1_push_tag(state->out, TLDAP_REQ_BIND)) goto err;
834 if (!asn1_write_Integer(state->out, ld->ld_version)) goto err;
835 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
837 if (mechanism == NULL) {
838 if (!asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0))) goto err;
839 if (!asn1_write(state->out, creds->data, creds->length)) goto err;
840 if (!asn1_pop_tag(state->out)) goto err;
842 if (!asn1_push_tag(state->out, ASN1_CONTEXT(3))) goto err;
843 if (!asn1_write_OctetString(state->out, mechanism,
844 strlen(mechanism))) goto err;
845 if ((creds != NULL) && (creds->data != NULL)) {
846 if (!asn1_write_OctetString(state->out, creds->data,
847 creds->length)) goto err;
849 if (!asn1_pop_tag(state->out)) goto err;
852 if (!asn1_pop_tag(state->out)) goto err;
854 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
856 if (tevent_req_nomem(subreq, req)) {
857 return tevent_req_post(req, ev);
859 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
864 tevent_req_error(req, TLDAP_ENCODING_ERROR);
865 return tevent_req_post(req, ev);
868 static void tldap_sasl_bind_done(struct tevent_req *subreq)
870 struct tevent_req *req = tevent_req_callback_data(
871 subreq, struct tevent_req);
872 struct tldap_req_state *state = tevent_req_data(
873 req, struct tldap_req_state);
876 err = tldap_msg_recv(subreq, state, &state->result);
878 if (err != TLDAP_SUCCESS) {
879 tevent_req_error(req, err);
882 if (state->result->type != TLDAP_RES_BIND) {
883 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
886 if (!asn1_start_tag(state->result->data, state->result->type) ||
887 !tldap_decode_response(state) ||
888 !asn1_end_tag(state->result->data)) {
889 tevent_req_error(req, TLDAP_DECODING_ERROR);
893 * TODO: pull the reply blob
895 if (state->result->lderr != TLDAP_SUCCESS) {
896 tevent_req_error(req, state->result->lderr);
899 tevent_req_done(req);
902 int tldap_sasl_bind_recv(struct tevent_req *req)
904 return tldap_simple_recv(req);
907 int tldap_sasl_bind(struct tldap_context *ld,
909 const char *mechanism,
911 struct tldap_control *sctrls,
913 struct tldap_control *cctrls,
916 TALLOC_CTX *frame = talloc_stackframe();
917 struct tevent_context *ev;
918 struct tevent_req *req;
921 ev = samba_tevent_context_init(frame);
923 result = TLDAP_NO_MEMORY;
927 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
928 sctrls, num_sctrls, cctrls, num_cctrls);
930 result = TLDAP_NO_MEMORY;
934 if (!tevent_req_poll(req, ev)) {
935 result = TLDAP_OPERATIONS_ERROR;
939 result = tldap_sasl_bind_recv(req);
940 tldap_save_msg(ld, req);
946 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
947 struct tevent_context *ev,
948 struct tldap_context *ld,
954 if (passwd != NULL) {
955 cred.data = discard_const_p(uint8_t, passwd);
956 cred.length = strlen(passwd);
958 cred.data = discard_const_p(uint8_t, "");
961 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
965 int tldap_simple_bind_recv(struct tevent_req *req)
967 return tldap_sasl_bind_recv(req);
970 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
975 if (passwd != NULL) {
976 cred.data = discard_const_p(uint8_t, passwd);
977 cred.length = strlen(passwd);
979 cred.data = discard_const_p(uint8_t, "");
982 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
985 /*****************************************************************************/
987 /* can't use isalpha() as only a strict set is valid for LDAP */
989 static bool tldap_is_alpha(char c)
991 return (((c >= 'a') && (c <= 'z')) || \
992 ((c >= 'A') && (c <= 'Z')));
995 static bool tldap_is_adh(char c)
997 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
1000 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
1001 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
1002 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
1003 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
1004 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
1005 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
1006 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
1007 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
1008 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
1009 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
1011 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
1012 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
1013 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1016 /* oid's should be numerical only in theory,
1017 * but apparently some broken servers may have alphanum aliases instead.
1018 * Do like openldap libraries and allow alphanum aliases for oids, but
1019 * do not allow Tagging options in that case.
1021 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1023 bool is_oid = false;
1027 /* first char has stricter rules */
1030 } else if (!tldap_is_alpha(*s)) {
1031 /* bad first char */
1035 for (i = 1; i < len; i++) {
1038 if (isdigit(s[i])) {
1051 if (tldap_is_adh(s[i])) {
1058 /* no tagging options */
1065 if ((i + 1) == len) {
1083 /* this function copies the value until the closing parenthesis is found. */
1084 static char *tldap_get_val(TALLOC_CTX *memctx,
1085 const char *value, const char **_s)
1087 const char *s = value;
1089 /* find terminator */
1092 if (s && (*(s - 1) == '\\')) {
1097 if (!s || !(*s == ')')) {
1098 /* malformed filter */
1104 return talloc_strndup(memctx, value, s - value);
1107 static int tldap_hex2char(const char *x)
1109 if (isxdigit(x[0]) && isxdigit(x[1])) {
1110 const char h1 = x[0], h2 = x[1];
1113 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1114 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1115 else if (h1 >= '0') c = h1 - (int)'0';
1117 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1118 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1119 else if (h2 >= '0') c += h2 - (int)'0';
1127 static bool tldap_find_first_star(const char *val, const char **star)
1131 for (s = val; *s; s++) {
1134 if (isxdigit(s[1]) && isxdigit(s[2])) {
1138 /* not hex based escape, check older syntax */
1147 /* invalid escape sequence */
1152 /* end of val, nothing found */
1162 /* string ended without closing parenthesis, filter is malformed */
1166 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1170 for (i = 0,p = 0; i < *val_len; i++) {
1176 /* these must be escaped */
1180 if (!value[i + 1]) {
1186 c = tldap_hex2char(&value[i]);
1187 if (c >= 0 && c < 256) {
1199 value[p] = value[i];
1208 value[p] = value[i];
1217 static bool tldap_push_filter_basic(struct tldap_context *ld,
1218 struct asn1_data *data,
1220 static bool tldap_push_filter_substring(struct tldap_context *ld,
1221 struct asn1_data *data,
1224 static bool tldap_push_filter_int(struct tldap_context *ld,
1225 struct asn1_data *data,
1228 const char *s = *_s;
1232 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1233 "Incomplete or malformed filter\n");
1238 /* we are right after a parenthesis,
1239 * find out what op we have at hand */
1242 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1243 if (!asn1_push_tag(data, TLDAP_FILTER_AND)) return false;
1248 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1249 if (!asn1_push_tag(data, TLDAP_FILTER_OR)) return false;
1254 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1255 if (!asn1_push_tag(data, TLDAP_FILTER_NOT)) return false;
1257 ret = tldap_push_filter_int(ld, data, &s);
1261 if (!asn1_pop_tag(data)) return false;
1266 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1267 "Invalid parenthesis '%c'\n", *s);
1271 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1272 "Invalid filter termination\n");
1276 ret = tldap_push_filter_basic(ld, data, &s);
1283 /* only and/or filters get here.
1284 * go through the list of filters */
1287 /* RFC 4526: empty and/or */
1288 if (!asn1_pop_tag(data)) return false;
1293 ret = tldap_push_filter_int(ld, data, &s);
1299 /* end of list, return */
1300 if (!asn1_pop_tag(data)) return false;
1307 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1308 "Incomplete or malformed filter\n");
1313 if (asn1_has_error(data)) {
1322 static bool tldap_push_filter_basic(struct tldap_context *ld,
1323 struct asn1_data *data,
1326 TALLOC_CTX *tmpctx = talloc_tos();
1327 const char *s = *_s;
1335 size_t type_len = 0;
1338 bool write_octect = true;
1341 eq = strchr(s, '=');
1343 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1344 "Invalid filter, missing equal sign\n");
1353 if (!asn1_push_tag(data, TLDAP_FILTER_LE)) return false;
1357 if (!asn1_push_tag(data, TLDAP_FILTER_GE)) return false;
1361 if (!asn1_push_tag(data, TLDAP_FILTER_APX)) return false;
1365 if (!asn1_push_tag(data, TLDAP_FILTER_EXT)) return false;
1366 write_octect = false;
1372 if (*s == ':') { /* [:dn]:rule:= value */
1374 /* malformed filter */
1378 } else { /* type[:dn][:rule]:= value */
1380 dn = strchr(s, ':');
1381 type_len = dn - type;
1382 if (dn == e) { /* type:= value */
1389 rule = strchr(dn, ':');
1393 if ((rule == dn + 1) || rule + 1 == e) {
1394 /* malformed filter, contains "::" */
1398 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1403 /* malformed filter. With two
1404 * optionals, the first must be "dn"
1417 if (!type && !dn && !rule) {
1418 /* malformed filter, there must be at least one */
1423 MatchingRuleAssertion ::= SEQUENCE {
1424 matchingRule [1] MatchingRuleID OPTIONAL,
1425 type [2] AttributeDescription OPTIONAL,
1426 matchValue [3] AssertionValue,
1427 dnAttributes [4] BOOLEAN DEFAULT FALSE
1431 /* check and add rule */
1433 ret = tldap_is_attrdesc(rule, e - rule, true);
1437 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1))) return false;
1438 if (!asn1_write(data, rule, e - rule)) return false;
1439 if (!asn1_pop_tag(data)) return false;
1442 /* check and add type */
1444 ret = tldap_is_attrdesc(type, type_len, false);
1448 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2))) return false;
1449 if (!asn1_write(data, type, type_len)) return false;
1450 if (!asn1_pop_tag(data)) return false;
1453 uval = tldap_get_val(tmpctx, val, _s);
1457 uval_len = *_s - val;
1458 ret = tldap_unescape_inplace(uval, &uval_len);
1463 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3))) return false;
1464 if (!asn1_write(data, uval, uval_len)) return false;
1465 if (!asn1_pop_tag(data)) return false;
1467 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4))) return false;
1468 if (!asn1_write_uint8(data, dn?1:0)) return false;
1469 if (!asn1_pop_tag(data)) return false;
1475 ret = tldap_is_attrdesc(s, e - s, false);
1480 if (strncmp(val, "*)", 2) == 0) {
1482 if (!asn1_push_tag(data, TLDAP_FILTER_PRES)) return false;
1483 if (!asn1_write(data, s, e - s)) return false;
1485 write_octect = false;
1489 ret = tldap_find_first_star(val, &star);
1495 if (!asn1_push_tag(data, TLDAP_FILTER_SUB)) return false;
1496 if (!asn1_write_OctetString(data, s, e - s)) return false;
1497 ret = tldap_push_filter_substring(ld, data, val, &s);
1502 write_octect = false;
1506 /* if nothing else, then it is just equality */
1507 if (!asn1_push_tag(data, TLDAP_FILTER_EQ)) return false;
1508 write_octect = true;
1513 uval = tldap_get_val(tmpctx, val, _s);
1517 uval_len = *_s - val;
1518 ret = tldap_unescape_inplace(uval, &uval_len);
1523 if (!asn1_write_OctetString(data, s, e - s)) return false;
1524 if (!asn1_write_OctetString(data, uval, uval_len)) return false;
1527 if (asn1_has_error(data)) {
1530 return asn1_pop_tag(data);
1533 static bool tldap_push_filter_substring(struct tldap_context *ld,
1534 struct asn1_data *data,
1538 TALLOC_CTX *tmpctx = talloc_tos();
1539 bool initial = true;
1546 SubstringFilter ::= SEQUENCE {
1547 type AttributeDescription,
1548 -- at least one must be present
1549 substrings SEQUENCE OF CHOICE {
1550 initial [0] LDAPString,
1552 final [2] LDAPString } }
1554 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
1557 ret = tldap_find_first_star(val, &star);
1561 chunk_len = star - val;
1565 if (!initial && chunk_len == 0) {
1566 /* found '**', which is illegal */
1582 if (initial && chunk_len == 0) {
1588 chunk = talloc_strndup(tmpctx, val, chunk_len);
1592 ret = tldap_unescape_inplace(chunk, &chunk_len);
1599 if (!asn1_push_tag(data, TLDAP_SUB_INI)) return false;
1602 if (!asn1_push_tag(data, TLDAP_SUB_ANY)) return false;
1606 if (!asn1_push_tag(data, TLDAP_SUB_FIN)) return false;
1612 if (!asn1_write(data, chunk, chunk_len)) return false;
1613 if (!asn1_pop_tag(data)) return false;
1617 } while (*star == '*');
1621 /* end of sequence */
1622 return asn1_pop_tag(data);
1625 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1626 * around parenthesis, we do not allow any spaces (except in values of
1627 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1628 * leading or trailing spaces where allowed.
1630 static bool tldap_push_filter(struct tldap_context *ld,
1631 struct asn1_data *data,
1634 const char *s = filter;
1637 ret = tldap_push_filter_int(ld, data, &s);
1639 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1640 "Incomplete or malformed filter\n");
1646 /*****************************************************************************/
1648 static void tldap_search_done(struct tevent_req *subreq);
1650 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1651 struct tevent_context *ev,
1652 struct tldap_context *ld,
1653 const char *base, int scope,
1658 struct tldap_control *sctrls,
1660 struct tldap_control *cctrls,
1666 struct tevent_req *req, *subreq;
1667 struct tldap_req_state *state;
1670 req = tldap_req_create(mem_ctx, ld, &state);
1675 if (!asn1_push_tag(state->out, TLDAP_REQ_SEARCH)) goto encoding_error;
1676 if (!asn1_write_OctetString(state->out, base, strlen(base))) goto encoding_error;
1677 if (!asn1_write_enumerated(state->out, scope)) goto encoding_error;
1678 if (!asn1_write_enumerated(state->out, deref)) goto encoding_error;
1679 if (!asn1_write_Integer(state->out, sizelimit)) goto encoding_error;
1680 if (!asn1_write_Integer(state->out, timelimit)) goto encoding_error;
1681 if (!asn1_write_BOOLEAN(state->out, attrsonly)) goto encoding_error;
1683 if (!tldap_push_filter(ld, state->out, filter)) {
1684 goto encoding_error;
1687 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto encoding_error;
1688 for (i=0; i<num_attrs; i++) {
1689 if (!asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]))) goto encoding_error;
1691 if (!asn1_pop_tag(state->out)) goto encoding_error;
1692 if (!asn1_pop_tag(state->out)) goto encoding_error;
1694 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1695 sctrls, num_sctrls);
1696 if (tevent_req_nomem(subreq, req)) {
1697 return tevent_req_post(req, ev);
1699 tevent_req_set_callback(subreq, tldap_search_done, req);
1703 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1704 return tevent_req_post(req, ev);
1707 static void tldap_search_done(struct tevent_req *subreq)
1709 struct tevent_req *req = tevent_req_callback_data(
1710 subreq, struct tevent_req);
1711 struct tldap_req_state *state = tevent_req_data(
1712 req, struct tldap_req_state);
1715 err = tldap_msg_recv(subreq, state, &state->result);
1716 if (err != TLDAP_SUCCESS) {
1717 tevent_req_error(req, err);
1720 switch (state->result->type) {
1721 case TLDAP_RES_SEARCH_ENTRY:
1722 case TLDAP_RES_SEARCH_REFERENCE:
1723 if (!tldap_msg_set_pending(subreq)) {
1724 tevent_req_oom(req);
1727 tevent_req_notify_callback(req);
1729 case TLDAP_RES_SEARCH_RESULT:
1730 TALLOC_FREE(subreq);
1731 if (!asn1_start_tag(state->result->data,
1732 state->result->type) ||
1733 !tldap_decode_response(state) ||
1734 !asn1_end_tag(state->result->data) ||
1735 !tldap_decode_controls(state)) {
1736 tevent_req_error(req, TLDAP_DECODING_ERROR);
1739 tevent_req_done(req);
1742 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1747 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1748 struct tldap_message **pmsg)
1750 struct tldap_req_state *state = tevent_req_data(
1751 req, struct tldap_req_state);
1754 if (!tevent_req_is_in_progress(req)
1755 && tevent_req_is_ldap_error(req, &err)) {
1759 if (tevent_req_is_in_progress(req)) {
1760 switch (state->result->type) {
1761 case TLDAP_RES_SEARCH_ENTRY:
1762 case TLDAP_RES_SEARCH_REFERENCE:
1765 return TLDAP_OPERATIONS_ERROR;
1769 *pmsg = talloc_move(mem_ctx, &state->result);
1770 return TLDAP_SUCCESS;
1773 struct tldap_sync_search_state {
1774 TALLOC_CTX *mem_ctx;
1775 struct tldap_message **entries;
1776 struct tldap_message **refs;
1780 static void tldap_search_cb(struct tevent_req *req)
1782 struct tldap_sync_search_state *state =
1783 (struct tldap_sync_search_state *)
1784 tevent_req_callback_data_void(req);
1785 struct tldap_message *msg, **tmp;
1786 int rc, num_entries, num_refs;
1788 rc = tldap_search_recv(req, talloc_tos(), &msg);
1789 if (rc != TLDAP_SUCCESS) {
1794 switch (tldap_msg_type(msg)) {
1795 case TLDAP_RES_SEARCH_ENTRY:
1796 num_entries = talloc_array_length(state->entries);
1797 tmp = talloc_realloc(state->mem_ctx, state->entries,
1798 struct tldap_message *, num_entries + 1);
1800 state->rc = TLDAP_NO_MEMORY;
1803 state->entries = tmp;
1804 state->entries[num_entries] = talloc_move(state->entries,
1807 case TLDAP_RES_SEARCH_REFERENCE:
1808 num_refs = talloc_array_length(state->refs);
1809 tmp = talloc_realloc(state->mem_ctx, state->refs,
1810 struct tldap_message *, num_refs + 1);
1812 state->rc = TLDAP_NO_MEMORY;
1816 state->refs[num_refs] = talloc_move(state->refs, &msg);
1818 case TLDAP_RES_SEARCH_RESULT:
1819 state->rc = TLDAP_SUCCESS;
1822 state->rc = TLDAP_PROTOCOL_ERROR;
1827 int tldap_search(struct tldap_context *ld,
1828 const char *base, int scope, const char *filter,
1829 const char **attrs, int num_attrs, int attrsonly,
1830 struct tldap_control *sctrls, int num_sctrls,
1831 struct tldap_control *cctrls, int num_cctrls,
1832 int timelimit, int sizelimit, int deref,
1833 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1834 struct tldap_message ***refs)
1836 TALLOC_CTX *frame = talloc_stackframe();
1837 struct tevent_context *ev;
1838 struct tevent_req *req;
1839 struct tldap_sync_search_state state;
1842 state.mem_ctx = mem_ctx;
1843 state.rc = TLDAP_SUCCESS;
1845 ev = samba_tevent_context_init(frame);
1847 state.rc = TLDAP_NO_MEMORY;
1851 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1852 attrs, num_attrs, attrsonly,
1853 sctrls, num_sctrls, cctrls, num_cctrls,
1854 timelimit, sizelimit, deref);
1856 state.rc = TLDAP_NO_MEMORY;
1860 tevent_req_set_callback(req, tldap_search_cb, &state);
1862 if (!tevent_req_is_in_progress(req)) {
1863 /* an error happend before sending */
1864 if (tevent_req_is_ldap_error(req, &state.rc)) {
1869 while (tevent_req_is_in_progress(req)
1870 && (state.rc == TLDAP_SUCCESS)) {
1871 if (tevent_loop_once(ev) == -1) {
1872 return TLDAP_OPERATIONS_ERROR;
1876 if (state.rc != TLDAP_SUCCESS) {
1880 if (entries != NULL) {
1881 *entries = state.entries;
1883 TALLOC_FREE(state.entries);
1888 TALLOC_FREE(state.refs);
1890 tldap_save_msg(ld, req);
1896 static bool tldap_parse_search_entry(struct tldap_message *msg)
1898 int num_attribs = 0;
1900 if (!asn1_start_tag(msg->data, msg->type)) return false;
1904 if (!asn1_read_OctetString_talloc(msg, msg->data, &msg->dn)) return false;
1906 if (msg->dn == NULL) {
1911 * Attributes: We overallocate msg->attribs by one, so that while
1912 * looping over the attributes we can directly parse into the last
1913 * array element. Same for the values in the inner loop.
1916 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1917 if (msg->attribs == NULL) {
1921 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
1922 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1923 struct tldap_attribute *attrib;
1926 attrib = &msg->attribs[num_attribs];
1927 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1928 if (attrib->values == NULL) {
1931 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
1932 if (!asn1_read_OctetString_talloc(msg->attribs, msg->data,
1933 &attrib->name)) return false;
1934 if (!asn1_start_tag(msg->data, ASN1_SET)) return false;
1936 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1937 if (!asn1_read_OctetString(msg->data, msg,
1938 &attrib->values[num_values])) return false;
1940 attrib->values = talloc_realloc(
1941 msg->attribs, attrib->values, DATA_BLOB,
1943 if (attrib->values == NULL) {
1948 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1949 DATA_BLOB, num_values);
1950 attrib->num_values = num_values;
1952 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SET */
1953 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SEQUENCE(0) */
1954 msg->attribs = talloc_realloc(
1955 msg, msg->attribs, struct tldap_attribute,
1957 if (msg->attribs == NULL) {
1962 msg->attribs = talloc_realloc(
1963 msg, msg->attribs, struct tldap_attribute, num_attribs);
1964 return asn1_end_tag(msg->data);
1967 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1969 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1976 bool tldap_entry_attributes(struct tldap_message *msg,
1977 struct tldap_attribute **attributes,
1978 int *num_attributes)
1980 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1983 *attributes = msg->attribs;
1984 *num_attributes = talloc_array_length(msg->attribs);
1988 static bool tldap_decode_controls(struct tldap_req_state *state)
1990 struct tldap_message *msg = state->result;
1991 struct asn1_data *data = msg->data;
1992 struct tldap_control *sctrls = NULL;
1993 int num_controls = 0;
1996 msg->res_sctrls = NULL;
1998 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
2002 if (!asn1_start_tag(data, ASN1_CONTEXT(0))) goto out;
2004 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
2005 struct tldap_control *c;
2008 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
2010 if (sctrls == NULL) {
2013 c = &sctrls[num_controls];
2015 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) goto out;
2016 if (!asn1_read_OctetString_talloc(msg, data, &oid)) goto out;
2017 if (asn1_has_error(data) || (oid == NULL)) {
2021 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2022 if (!asn1_read_BOOLEAN(data, &c->critical)) goto out;
2024 c->critical = false;
2026 c->value = data_blob_null;
2027 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2028 !asn1_read_OctetString(data, msg, &c->value)) {
2031 if (!asn1_end_tag(data)) goto out; /* ASN1_SEQUENCE(0) */
2036 if (!asn1_end_tag(data)) goto out; /* ASN1_CONTEXT(0) */
2043 TALLOC_FREE(sctrls);
2045 msg->res_sctrls = sctrls;
2050 static void tldap_simple_done(struct tevent_req *subreq, int type)
2052 struct tevent_req *req = tevent_req_callback_data(
2053 subreq, struct tevent_req);
2054 struct tldap_req_state *state = tevent_req_data(
2055 req, struct tldap_req_state);
2058 err = tldap_msg_recv(subreq, state, &state->result);
2059 TALLOC_FREE(subreq);
2060 if (err != TLDAP_SUCCESS) {
2061 tevent_req_error(req, err);
2064 if (state->result->type != type) {
2065 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2068 if (!asn1_start_tag(state->result->data, state->result->type) ||
2069 !tldap_decode_response(state) ||
2070 !asn1_end_tag(state->result->data) ||
2071 !tldap_decode_controls(state)) {
2072 tevent_req_error(req, TLDAP_DECODING_ERROR);
2075 if (state->result->lderr != TLDAP_SUCCESS) {
2076 tevent_req_error(req, state->result->lderr);
2079 tevent_req_done(req);
2082 static int tldap_simple_recv(struct tevent_req *req)
2085 if (tevent_req_is_ldap_error(req, &err)) {
2088 return TLDAP_SUCCESS;
2091 static void tldap_add_done(struct tevent_req *subreq);
2093 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2094 struct tevent_context *ev,
2095 struct tldap_context *ld,
2097 struct tldap_mod *attributes,
2099 struct tldap_control *sctrls,
2101 struct tldap_control *cctrls,
2104 struct tevent_req *req, *subreq;
2105 struct tldap_req_state *state;
2108 req = tldap_req_create(mem_ctx, ld, &state);
2113 if (!asn1_push_tag(state->out, TLDAP_REQ_ADD)) goto err;
2114 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2115 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2117 for (i=0; i<num_attributes; i++) {
2118 struct tldap_mod *attrib = &attributes[i];
2119 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2120 if (!asn1_write_OctetString(state->out, attrib->attribute,
2121 strlen(attrib->attribute))) goto err;
2122 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2123 for (j=0; j<attrib->num_values; j++) {
2124 if (!asn1_write_OctetString(state->out,
2125 attrib->values[j].data,
2126 attrib->values[j].length)) goto err;
2128 if (!asn1_pop_tag(state->out)) goto err;
2129 if (!asn1_pop_tag(state->out)) goto err;
2132 if (!asn1_pop_tag(state->out)) goto err;
2133 if (!asn1_pop_tag(state->out)) goto err;
2135 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2136 sctrls, num_sctrls);
2137 if (tevent_req_nomem(subreq, req)) {
2138 return tevent_req_post(req, ev);
2140 tevent_req_set_callback(subreq, tldap_add_done, req);
2145 tevent_req_error(req, TLDAP_ENCODING_ERROR);
2146 return tevent_req_post(req, ev);
2149 static void tldap_add_done(struct tevent_req *subreq)
2151 tldap_simple_done(subreq, TLDAP_RES_ADD);
2154 int tldap_add_recv(struct tevent_req *req)
2156 return tldap_simple_recv(req);
2159 int tldap_add(struct tldap_context *ld, const char *dn,
2160 struct tldap_mod *attributes, int num_attributes,
2161 struct tldap_control *sctrls, int num_sctrls,
2162 struct tldap_control *cctrls, int num_cctrls)
2164 TALLOC_CTX *frame = talloc_stackframe();
2165 struct tevent_context *ev;
2166 struct tevent_req *req;
2169 ev = samba_tevent_context_init(frame);
2171 result = TLDAP_NO_MEMORY;
2175 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2176 sctrls, num_sctrls, cctrls, num_cctrls);
2178 result = TLDAP_NO_MEMORY;
2182 if (!tevent_req_poll(req, ev)) {
2183 result = TLDAP_OPERATIONS_ERROR;
2187 result = tldap_add_recv(req);
2188 tldap_save_msg(ld, req);
2194 static void tldap_modify_done(struct tevent_req *subreq);
2196 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2197 struct tevent_context *ev,
2198 struct tldap_context *ld,
2200 struct tldap_mod *mods, int num_mods,
2201 struct tldap_control *sctrls,
2203 struct tldap_control *cctrls,
2206 struct tevent_req *req, *subreq;
2207 struct tldap_req_state *state;
2210 req = tldap_req_create(mem_ctx, ld, &state);
2215 if (!asn1_push_tag(state->out, TLDAP_REQ_MODIFY)) goto err;
2216 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2217 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2219 for (i=0; i<num_mods; i++) {
2220 struct tldap_mod *mod = &mods[i];
2221 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2222 if (!asn1_write_enumerated(state->out, mod->mod_op)) goto err;
2223 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2224 if (!asn1_write_OctetString(state->out, mod->attribute,
2225 strlen(mod->attribute))) goto err;
2226 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2227 for (j=0; j<mod->num_values; j++) {
2228 if (!asn1_write_OctetString(state->out,
2229 mod->values[j].data,
2230 mod->values[j].length)) goto err;
2232 if (!asn1_pop_tag(state->out)) goto err;
2233 if (!asn1_pop_tag(state->out)) goto err;
2234 if (!asn1_pop_tag(state->out)) goto err;
2237 if (!asn1_pop_tag(state->out)) goto err;
2238 if (!asn1_pop_tag(state->out)) goto err;
2240 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2241 sctrls, num_sctrls);
2242 if (tevent_req_nomem(subreq, req)) {
2243 return tevent_req_post(req, ev);
2245 tevent_req_set_callback(subreq, tldap_modify_done, req);
2250 tevent_req_error(req, TLDAP_ENCODING_ERROR);
2251 return tevent_req_post(req, ev);
2254 static void tldap_modify_done(struct tevent_req *subreq)
2256 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2259 int tldap_modify_recv(struct tevent_req *req)
2261 return tldap_simple_recv(req);
2264 int tldap_modify(struct tldap_context *ld, const char *dn,
2265 struct tldap_mod *mods, int num_mods,
2266 struct tldap_control *sctrls, int num_sctrls,
2267 struct tldap_control *cctrls, int num_cctrls)
2269 TALLOC_CTX *frame = talloc_stackframe();
2270 struct tevent_context *ev;
2271 struct tevent_req *req;
2274 ev = samba_tevent_context_init(frame);
2276 result = TLDAP_NO_MEMORY;
2280 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2281 sctrls, num_sctrls, cctrls, num_cctrls);
2283 result = TLDAP_NO_MEMORY;
2287 if (!tevent_req_poll(req, ev)) {
2288 result = TLDAP_OPERATIONS_ERROR;
2292 result = tldap_modify_recv(req);
2293 tldap_save_msg(ld, req);
2299 static void tldap_delete_done(struct tevent_req *subreq);
2301 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2302 struct tevent_context *ev,
2303 struct tldap_context *ld,
2305 struct tldap_control *sctrls,
2307 struct tldap_control *cctrls,
2310 struct tevent_req *req, *subreq;
2311 struct tldap_req_state *state;
2313 req = tldap_req_create(mem_ctx, ld, &state);
2318 if (!asn1_push_tag(state->out, TLDAP_REQ_DELETE)) goto err;
2319 if (!asn1_write(state->out, dn, strlen(dn))) goto err;
2320 if (!asn1_pop_tag(state->out)) goto err;
2322 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2323 sctrls, num_sctrls);
2324 if (tevent_req_nomem(subreq, req)) {
2325 return tevent_req_post(req, ev);
2327 tevent_req_set_callback(subreq, tldap_delete_done, req);
2332 tevent_req_error(req, TLDAP_ENCODING_ERROR);
2333 return tevent_req_post(req, ev);
2336 static void tldap_delete_done(struct tevent_req *subreq)
2338 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2341 int tldap_delete_recv(struct tevent_req *req)
2343 return tldap_simple_recv(req);
2346 int tldap_delete(struct tldap_context *ld, const char *dn,
2347 struct tldap_control *sctrls, int num_sctrls,
2348 struct tldap_control *cctrls, int num_cctrls)
2350 TALLOC_CTX *frame = talloc_stackframe();
2351 struct tevent_context *ev;
2352 struct tevent_req *req;
2355 ev = samba_tevent_context_init(frame);
2357 result = TLDAP_NO_MEMORY;
2361 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2362 cctrls, num_cctrls);
2364 result = TLDAP_NO_MEMORY;
2368 if (!tevent_req_poll(req, ev)) {
2369 result = TLDAP_OPERATIONS_ERROR;
2373 result = tldap_delete_recv(req);
2374 tldap_save_msg(ld, req);
2380 int tldap_msg_id(const struct tldap_message *msg)
2385 int tldap_msg_type(const struct tldap_message *msg)
2390 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2395 return msg->res_matcheddn;
2398 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2403 return msg->res_diagnosticmessage;
2406 const char *tldap_msg_referral(struct tldap_message *msg)
2411 return msg->res_referral;
2414 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2415 struct tldap_control **sctrls)
2422 *sctrls = msg->res_sctrls;
2423 *num_sctrls = talloc_array_length(msg->res_sctrls);
2426 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2428 return ld->last_msg;
2431 const char *tldap_err2string(int rc)
2433 const char *res = NULL;
2436 * This would normally be a table, but the error codes are not fully
2437 * sequential. Let the compiler figure out the optimum implementation
2443 res = "TLDAP_SUCCESS";
2445 case TLDAP_OPERATIONS_ERROR:
2446 res = "TLDAP_OPERATIONS_ERROR";
2448 case TLDAP_PROTOCOL_ERROR:
2449 res = "TLDAP_PROTOCOL_ERROR";
2451 case TLDAP_TIMELIMIT_EXCEEDED:
2452 res = "TLDAP_TIMELIMIT_EXCEEDED";
2454 case TLDAP_SIZELIMIT_EXCEEDED:
2455 res = "TLDAP_SIZELIMIT_EXCEEDED";
2457 case TLDAP_COMPARE_FALSE:
2458 res = "TLDAP_COMPARE_FALSE";
2460 case TLDAP_COMPARE_TRUE:
2461 res = "TLDAP_COMPARE_TRUE";
2463 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2464 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2466 case TLDAP_STRONG_AUTH_REQUIRED:
2467 res = "TLDAP_STRONG_AUTH_REQUIRED";
2469 case TLDAP_REFERRAL:
2470 res = "TLDAP_REFERRAL";
2472 case TLDAP_ADMINLIMIT_EXCEEDED:
2473 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2475 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2476 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2478 case TLDAP_CONFIDENTIALITY_REQUIRED:
2479 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2481 case TLDAP_SASL_BIND_IN_PROGRESS:
2482 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2484 case TLDAP_NO_SUCH_ATTRIBUTE:
2485 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2487 case TLDAP_UNDEFINED_TYPE:
2488 res = "TLDAP_UNDEFINED_TYPE";
2490 case TLDAP_INAPPROPRIATE_MATCHING:
2491 res = "TLDAP_INAPPROPRIATE_MATCHING";
2493 case TLDAP_CONSTRAINT_VIOLATION:
2494 res = "TLDAP_CONSTRAINT_VIOLATION";
2496 case TLDAP_TYPE_OR_VALUE_EXISTS:
2497 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2499 case TLDAP_INVALID_SYNTAX:
2500 res = "TLDAP_INVALID_SYNTAX";
2502 case TLDAP_NO_SUCH_OBJECT:
2503 res = "TLDAP_NO_SUCH_OBJECT";
2505 case TLDAP_ALIAS_PROBLEM:
2506 res = "TLDAP_ALIAS_PROBLEM";
2508 case TLDAP_INVALID_DN_SYNTAX:
2509 res = "TLDAP_INVALID_DN_SYNTAX";
2512 res = "TLDAP_IS_LEAF";
2514 case TLDAP_ALIAS_DEREF_PROBLEM:
2515 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2517 case TLDAP_INAPPROPRIATE_AUTH:
2518 res = "TLDAP_INAPPROPRIATE_AUTH";
2520 case TLDAP_INVALID_CREDENTIALS:
2521 res = "TLDAP_INVALID_CREDENTIALS";
2523 case TLDAP_INSUFFICIENT_ACCESS:
2524 res = "TLDAP_INSUFFICIENT_ACCESS";
2529 case TLDAP_UNAVAILABLE:
2530 res = "TLDAP_UNAVAILABLE";
2532 case TLDAP_UNWILLING_TO_PERFORM:
2533 res = "TLDAP_UNWILLING_TO_PERFORM";
2535 case TLDAP_LOOP_DETECT:
2536 res = "TLDAP_LOOP_DETECT";
2538 case TLDAP_NAMING_VIOLATION:
2539 res = "TLDAP_NAMING_VIOLATION";
2541 case TLDAP_OBJECT_CLASS_VIOLATION:
2542 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2544 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2545 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2547 case TLDAP_NOT_ALLOWED_ON_RDN:
2548 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2550 case TLDAP_ALREADY_EXISTS:
2551 res = "TLDAP_ALREADY_EXISTS";
2553 case TLDAP_NO_OBJECT_CLASS_MODS:
2554 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2556 case TLDAP_RESULTS_TOO_LARGE:
2557 res = "TLDAP_RESULTS_TOO_LARGE";
2559 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2560 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2563 res = "TLDAP_OTHER";
2565 case TLDAP_SERVER_DOWN:
2566 res = "TLDAP_SERVER_DOWN";
2568 case TLDAP_LOCAL_ERROR:
2569 res = "TLDAP_LOCAL_ERROR";
2571 case TLDAP_ENCODING_ERROR:
2572 res = "TLDAP_ENCODING_ERROR";
2574 case TLDAP_DECODING_ERROR:
2575 res = "TLDAP_DECODING_ERROR";
2578 res = "TLDAP_TIMEOUT";
2580 case TLDAP_AUTH_UNKNOWN:
2581 res = "TLDAP_AUTH_UNKNOWN";
2583 case TLDAP_FILTER_ERROR:
2584 res = "TLDAP_FILTER_ERROR";
2586 case TLDAP_USER_CANCELLED:
2587 res = "TLDAP_USER_CANCELLED";
2589 case TLDAP_PARAM_ERROR:
2590 res = "TLDAP_PARAM_ERROR";
2592 case TLDAP_NO_MEMORY:
2593 res = "TLDAP_NO_MEMORY";
2595 case TLDAP_CONNECT_ERROR:
2596 res = "TLDAP_CONNECT_ERROR";
2598 case TLDAP_NOT_SUPPORTED:
2599 res = "TLDAP_NOT_SUPPORTED";
2601 case TLDAP_CONTROL_NOT_FOUND:
2602 res = "TLDAP_CONTROL_NOT_FOUND";
2604 case TLDAP_NO_RESULTS_RETURNED:
2605 res = "TLDAP_NO_RESULTS_RETURNED";
2607 case TLDAP_MORE_RESULTS_TO_RETURN:
2608 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2610 case TLDAP_CLIENT_LOOP:
2611 res = "TLDAP_CLIENT_LOOP";
2613 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2614 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2617 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2622 res = "Unknown LDAP Error";