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/asn1.h"
23 #include "../lib/tsocket/tsocket.h"
24 #include "../lib/util/tevent_unix.h"
26 static int tldap_simple_recv(struct tevent_req *req);
28 bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
30 enum tevent_req_state state;
33 if (!tevent_req_is_error(req, &state, &err)) {
37 case TEVENT_REQ_TIMED_OUT:
38 *perr = TLDAP_TIMEOUT;
40 case TEVENT_REQ_NO_MEMORY:
41 *perr = TLDAP_NO_MEMORY;
43 case TEVENT_REQ_USER_ERROR:
47 *perr = TLDAP_OPERATIONS_ERROR;
53 struct tldap_ctx_attribute {
58 struct tldap_context {
63 struct tstream_context *conn;
66 struct tevent_queue *outgoing;
67 struct tevent_req **pending;
69 /* For the sync wrappers we need something like get_last_error... */
70 struct tldap_message *last_msg;
73 void (*log_fn)(void *context, enum tldap_debug_level level,
74 const char *fmt, va_list ap);
77 struct tldap_ctx_attribute *ctx_attrs;
80 struct tldap_message {
81 struct asn1_data *data;
88 struct tldap_attribute *attribs;
90 /* Error data sent by the server */
93 char *res_diagnosticmessage;
95 struct tldap_control *res_sctrls;
97 /* Controls sent by the server */
98 struct tldap_control *ctrls;
101 void tldap_set_debug(struct tldap_context *ld,
102 void (*log_fn)(void *log_private,
103 enum tldap_debug_level level,
105 va_list ap) PRINTF_ATTRIBUTE(3,0),
109 ld->log_private = log_private;
112 static void tldap_debug(struct tldap_context *ld,
113 enum tldap_debug_level level,
114 const char *fmt, ...)
120 if (ld->log_fn == NULL) {
124 ld->log_fn(ld->log_private, level, fmt, ap);
128 static int tldap_next_msgid(struct tldap_context *ld)
132 result = ld->msgid++;
133 if (ld->msgid == 2147483647) {
139 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
141 struct tldap_context *ctx;
144 ctx = talloc_zero(mem_ctx, struct tldap_context);
148 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
155 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
156 if (ctx->outgoing == NULL) {
163 bool tldap_connection_ok(struct tldap_context *ld)
168 return !ld->server_down;
171 static struct tldap_ctx_attribute *tldap_context_findattr(
172 struct tldap_context *ld, const char *name)
176 num_attrs = talloc_array_length(ld->ctx_attrs);
178 for (i=0; i<num_attrs; i++) {
179 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
180 return &ld->ctx_attrs[i];
186 bool tldap_context_setattr(struct tldap_context *ld,
187 const char *name, const void *_pptr)
189 struct tldap_ctx_attribute *tmp, *attr;
192 void **pptr = (void **)discard_const_p(void,_pptr);
194 attr = tldap_context_findattr(ld, name);
197 * We don't actually delete attrs, we don't expect tons of
198 * attributes being shuffled around.
200 TALLOC_FREE(attr->ptr);
202 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
208 tmpname = talloc_strdup(ld, name);
209 if (tmpname == NULL) {
213 num_attrs = talloc_array_length(ld->ctx_attrs);
215 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
218 TALLOC_FREE(tmpname);
221 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
223 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
225 tmp[num_attrs].ptr = NULL;
232 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
234 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
242 struct read_ldap_state {
247 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
248 static void read_ldap_done(struct tevent_req *subreq);
250 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
251 struct tevent_context *ev,
252 struct tstream_context *conn)
254 struct tevent_req *req, *subreq;
255 struct read_ldap_state *state;
257 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
263 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
265 if (tevent_req_nomem(subreq, req)) {
266 return tevent_req_post(req, ev);
268 tevent_req_set_callback(subreq, read_ldap_done, req);
272 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
274 struct read_ldap_state *state = talloc_get_type_abort(
275 private_data, struct read_ldap_state);
280 /* We've been here, we're done */
285 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
287 if (buf[0] != 0x30) {
292 if ((len & 0x80) == 0) {
297 lensize = (len & 0x7f);
301 /* Please get us the full length */
304 if (buflen > 2 + lensize) {
308 if (buflen != 2 + lensize) {
312 for (i=0; i<lensize; i++) {
313 len = (len << 8) | buf[2+i];
318 static void read_ldap_done(struct tevent_req *subreq)
320 struct tevent_req *req = tevent_req_callback_data(
321 subreq, struct tevent_req);
322 struct read_ldap_state *state = tevent_req_data(
323 req, struct read_ldap_state);
327 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
330 tevent_req_error(req, err);
333 tevent_req_done(req);
336 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
337 uint8_t **pbuf, int *perrno)
339 struct read_ldap_state *state = tevent_req_data(
340 req, struct read_ldap_state);
342 if (tevent_req_is_unix_error(req, perrno)) {
345 *pbuf = talloc_move(mem_ctx, &state->buf);
346 return talloc_get_size(*pbuf);
349 struct tldap_msg_state {
350 struct tldap_context *ld;
351 struct tevent_context *ev;
355 struct asn1_data *data;
359 static void tldap_push_controls(struct asn1_data *data,
360 struct tldap_control *sctrls,
365 if ((sctrls == NULL) || (num_sctrls == 0)) {
369 asn1_push_tag(data, ASN1_CONTEXT(0));
371 for (i=0; i<num_sctrls; i++) {
372 struct tldap_control *c = &sctrls[i];
373 asn1_push_tag(data, ASN1_SEQUENCE(0));
374 asn1_write_OctetString(data, c->oid, strlen(c->oid));
376 asn1_write_BOOLEAN(data, true);
378 if (c->value.data != NULL) {
379 asn1_write_OctetString(data, c->value.data,
382 asn1_pop_tag(data); /* ASN1_SEQUENCE(0) */
385 asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
388 static void tldap_msg_sent(struct tevent_req *subreq);
389 static void tldap_msg_received(struct tevent_req *subreq);
391 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
392 struct tevent_context *ev,
393 struct tldap_context *ld,
394 int id, struct asn1_data *data,
395 struct tldap_control *sctrls,
398 struct tevent_req *req, *subreq;
399 struct tldap_msg_state *state;
402 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
405 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
413 if (state->ld->server_down) {
414 tevent_req_error(req, TLDAP_SERVER_DOWN);
415 return tevent_req_post(req, ev);
418 tldap_push_controls(data, sctrls, num_sctrls);
422 if (!asn1_blob(data, &blob)) {
423 tevent_req_error(req, TLDAP_ENCODING_ERROR);
424 return tevent_req_post(req, ev);
427 state->iov.iov_base = (void *)blob.data;
428 state->iov.iov_len = blob.length;
430 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
432 if (tevent_req_nomem(subreq, req)) {
433 return tevent_req_post(req, ev);
435 tevent_req_set_callback(subreq, tldap_msg_sent, req);
439 static void tldap_msg_unset_pending(struct tevent_req *req)
441 struct tldap_msg_state *state = tevent_req_data(
442 req, struct tldap_msg_state);
443 struct tldap_context *ld = state->ld;
444 int num_pending = talloc_array_length(ld->pending);
447 if (num_pending == 1) {
448 TALLOC_FREE(ld->pending);
452 for (i=0; i<num_pending; i++) {
453 if (req == ld->pending[i]) {
457 if (i == num_pending) {
459 * Something's seriously broken. Just returning here is the
460 * right thing nevertheless, the point of this routine is to
461 * remove ourselves from cli->pending.
467 * Remove ourselves from the cli->pending array
469 if (num_pending > 1) {
470 ld->pending[i] = ld->pending[num_pending-1];
474 * No NULL check here, we're shrinking by sizeof(void *), and
475 * talloc_realloc just adjusts the size for this.
477 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
482 static int tldap_msg_destructor(struct tevent_req *req)
484 tldap_msg_unset_pending(req);
488 static bool tldap_msg_set_pending(struct tevent_req *req)
490 struct tldap_msg_state *state = tevent_req_data(
491 req, struct tldap_msg_state);
492 struct tldap_context *ld;
493 struct tevent_req **pending;
495 struct tevent_req *subreq;
498 num_pending = talloc_array_length(ld->pending);
500 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
502 if (pending == NULL) {
505 pending[num_pending] = req;
506 ld->pending = pending;
507 talloc_set_destructor(req, tldap_msg_destructor);
509 if (num_pending > 0) {
514 * We're the first one, add the read_ldap request that waits for the
515 * answer from the server
517 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
518 if (subreq == NULL) {
519 tldap_msg_unset_pending(req);
522 tevent_req_set_callback(subreq, tldap_msg_received, ld);
526 static void tldap_msg_sent(struct tevent_req *subreq)
528 struct tevent_req *req = tevent_req_callback_data(
529 subreq, struct tevent_req);
530 struct tldap_msg_state *state = tevent_req_data(
531 req, struct tldap_msg_state);
535 nwritten = tstream_writev_queue_recv(subreq, &err);
537 if (nwritten == -1) {
538 state->ld->server_down = true;
539 tevent_req_error(req, TLDAP_SERVER_DOWN);
543 if (!tldap_msg_set_pending(req)) {
549 static int tldap_msg_msgid(struct tevent_req *req)
551 struct tldap_msg_state *state = tevent_req_data(
552 req, struct tldap_msg_state);
557 static void tldap_msg_received(struct tevent_req *subreq)
559 struct tldap_context *ld = tevent_req_callback_data(
560 subreq, struct tldap_context);
561 struct tevent_req *req;
562 struct tldap_msg_state *state;
563 struct asn1_data *data;
572 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
574 if (received == -1) {
575 status = TLDAP_SERVER_DOWN;
579 data = asn1_init(talloc_tos());
581 status = TLDAP_NO_MEMORY;
584 asn1_load_nocopy(data, inbuf, received);
587 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
588 ok &= asn1_read_Integer(data, &id);
589 ok &= asn1_peek_uint8(data, &type);
592 status = TLDAP_PROTOCOL_ERROR;
596 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
597 "type %d\n", id, (int)type);
599 num_pending = talloc_array_length(ld->pending);
601 for (i=0; i<num_pending; i++) {
602 if (id == tldap_msg_msgid(ld->pending[i])) {
606 if (i == num_pending) {
607 /* Dump unexpected reply */
608 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
609 "No request pending for msg %d\n", id);
615 req = ld->pending[i];
616 state = tevent_req_data(req, struct tldap_msg_state);
618 state->inbuf = talloc_move(state, &inbuf);
619 state->data = talloc_move(state, &data);
621 talloc_set_destructor(req, NULL);
622 tldap_msg_unset_pending(req);
623 num_pending = talloc_array_length(ld->pending);
625 tevent_req_done(req);
628 if (num_pending == 0) {
631 if (talloc_array_length(ld->pending) > num_pending) {
633 * The callback functions called from tevent_req_done() above
634 * have put something on the pending queue. We don't have to
635 * trigger the read_ldap_send(), tldap_msg_set_pending() has
636 * done it for us already.
641 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
642 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
643 if (subreq == NULL) {
644 status = TLDAP_NO_MEMORY;
647 tevent_req_set_callback(subreq, tldap_msg_received, ld);
651 while (talloc_array_length(ld->pending) > 0) {
652 req = ld->pending[0];
653 state = tevent_req_data(req, struct tldap_msg_state);
654 tevent_req_defer_callback(req, state->ev);
655 talloc_set_destructor(req, NULL);
656 tldap_msg_destructor(req);
657 tevent_req_error(req, status);
661 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
662 struct tldap_message **pmsg)
664 struct tldap_msg_state *state = tevent_req_data(
665 req, struct tldap_msg_state);
666 struct tldap_message *msg;
670 if (tevent_req_is_ldap_error(req, &err)) {
674 if (!asn1_peek_uint8(state->data, &msgtype)) {
675 return TLDAP_PROTOCOL_ERROR;
679 return TLDAP_SUCCESS;
682 msg = talloc_zero(mem_ctx, struct tldap_message);
684 return TLDAP_NO_MEMORY;
688 msg->inbuf = talloc_move(msg, &state->inbuf);
689 msg->data = talloc_move(msg, &state->data);
693 return TLDAP_SUCCESS;
696 struct tldap_req_state {
698 struct asn1_data *out;
699 struct tldap_message *result;
702 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
703 struct tldap_context *ld,
704 struct tldap_req_state **pstate)
706 struct tevent_req *req;
707 struct tldap_req_state *state;
709 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
714 state->out = asn1_init(state);
715 if (state->out == NULL) {
719 state->result = NULL;
720 state->id = tldap_next_msgid(ld);
722 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
723 asn1_write_Integer(state->out, state->id);
729 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
731 struct tldap_req_state *state = tevent_req_data(
732 req, struct tldap_req_state);
734 TALLOC_FREE(ld->last_msg);
735 ld->last_msg = talloc_move(ld, &state->result);
738 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
740 char *result = talloc_array(mem_ctx, char, blob.length+1);
742 if (result == NULL) {
746 memcpy(result, blob.data, blob.length);
747 result[blob.length] = '\0';
751 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
752 struct asn1_data *data,
757 if (!asn1_read_OctetString(data, mem_ctx, &string))
760 result = blob2string_talloc(mem_ctx, string);
762 data_blob_free(&string);
764 if (result == NULL) {
771 static bool tldap_decode_controls(struct tldap_req_state *state);
773 static bool tldap_decode_response(struct tldap_req_state *state)
775 struct asn1_data *data = state->result->data;
776 struct tldap_message *msg = state->result;
779 ok &= asn1_read_enumerated(data, &msg->lderr);
780 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
781 ok &= asn1_read_OctetString_talloc(msg, data,
782 &msg->res_diagnosticmessage);
783 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
784 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
785 ok &= asn1_read_OctetString_talloc(msg, data,
787 ok &= asn1_end_tag(data);
789 msg->res_referral = NULL;
795 static void tldap_sasl_bind_done(struct tevent_req *subreq);
797 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
798 struct tevent_context *ev,
799 struct tldap_context *ld,
801 const char *mechanism,
803 struct tldap_control *sctrls,
805 struct tldap_control *cctrls,
808 struct tevent_req *req, *subreq;
809 struct tldap_req_state *state;
811 req = tldap_req_create(mem_ctx, ld, &state);
820 asn1_push_tag(state->out, TLDAP_REQ_BIND);
821 asn1_write_Integer(state->out, ld->ld_version);
822 asn1_write_OctetString(state->out, dn, strlen(dn));
824 if (mechanism == NULL) {
825 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
826 asn1_write(state->out, creds->data, creds->length);
827 asn1_pop_tag(state->out);
829 asn1_push_tag(state->out, ASN1_CONTEXT(3));
830 asn1_write_OctetString(state->out, mechanism,
832 if ((creds != NULL) && (creds->data != NULL)) {
833 asn1_write_OctetString(state->out, creds->data,
836 asn1_pop_tag(state->out);
839 if (!asn1_pop_tag(state->out)) {
840 tevent_req_error(req, TLDAP_ENCODING_ERROR);
841 return tevent_req_post(req, ev);
844 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
846 if (tevent_req_nomem(subreq, req)) {
847 return tevent_req_post(req, ev);
849 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
853 static void tldap_sasl_bind_done(struct tevent_req *subreq)
855 struct tevent_req *req = tevent_req_callback_data(
856 subreq, struct tevent_req);
857 struct tldap_req_state *state = tevent_req_data(
858 req, struct tldap_req_state);
861 err = tldap_msg_recv(subreq, state, &state->result);
863 if (err != TLDAP_SUCCESS) {
864 tevent_req_error(req, err);
867 if (state->result->type != TLDAP_RES_BIND) {
868 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
871 if (!asn1_start_tag(state->result->data, state->result->type) ||
872 !tldap_decode_response(state) ||
873 !asn1_end_tag(state->result->data)) {
874 tevent_req_error(req, TLDAP_DECODING_ERROR);
878 * TODO: pull the reply blob
880 if (state->result->lderr != TLDAP_SUCCESS) {
881 tevent_req_error(req, state->result->lderr);
884 tevent_req_done(req);
887 int tldap_sasl_bind_recv(struct tevent_req *req)
889 return tldap_simple_recv(req);
892 int tldap_sasl_bind(struct tldap_context *ld,
894 const char *mechanism,
896 struct tldap_control *sctrls,
898 struct tldap_control *cctrls,
901 TALLOC_CTX *frame = talloc_stackframe();
902 struct tevent_context *ev;
903 struct tevent_req *req;
906 ev = samba_tevent_context_init(frame);
908 result = TLDAP_NO_MEMORY;
912 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
913 sctrls, num_sctrls, cctrls, num_cctrls);
915 result = TLDAP_NO_MEMORY;
919 if (!tevent_req_poll(req, ev)) {
920 result = TLDAP_OPERATIONS_ERROR;
924 result = tldap_sasl_bind_recv(req);
925 tldap_save_msg(ld, req);
931 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
932 struct tevent_context *ev,
933 struct tldap_context *ld,
939 if (passwd != NULL) {
940 cred.data = discard_const_p(uint8_t, passwd);
941 cred.length = strlen(passwd);
943 cred.data = discard_const_p(uint8_t, "");
946 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
950 int tldap_simple_bind_recv(struct tevent_req *req)
952 return tldap_sasl_bind_recv(req);
955 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
960 if (passwd != NULL) {
961 cred.data = discard_const_p(uint8_t, passwd);
962 cred.length = strlen(passwd);
964 cred.data = discard_const_p(uint8_t, "");
967 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
970 /*****************************************************************************/
972 /* can't use isalpha() as only a strict set is valid for LDAP */
974 static bool tldap_is_alpha(char c)
976 return (((c >= 'a') && (c <= 'z')) || \
977 ((c >= 'A') && (c <= 'Z')));
980 static bool tldap_is_adh(char c)
982 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
985 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
986 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
987 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
988 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
989 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
990 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
991 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
992 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
993 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
994 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
996 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
997 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
998 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1001 /* oid's should be numerical only in theory,
1002 * but apparently some broken servers may have alphanum aliases instead.
1003 * Do like openldap libraries and allow alphanum aliases for oids, but
1004 * do not allow Tagging options in that case.
1006 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1008 bool is_oid = false;
1012 /* first char has stricter rules */
1015 } else if (!tldap_is_alpha(*s)) {
1016 /* bad first char */
1020 for (i = 1; i < len; i++) {
1023 if (isdigit(s[i])) {
1036 if (tldap_is_adh(s[i])) {
1043 /* no tagging options */
1050 if ((i + 1) == len) {
1068 /* this function copies the value until the closing parenthesis is found. */
1069 static char *tldap_get_val(TALLOC_CTX *memctx,
1070 const char *value, const char **_s)
1072 const char *s = value;
1074 /* find terminator */
1077 if (s && (*(s - 1) == '\\')) {
1082 if (!s || !(*s == ')')) {
1083 /* malformed filter */
1089 return talloc_strndup(memctx, value, s - value);
1092 static int tldap_hex2char(const char *x)
1094 if (isxdigit(x[0]) && isxdigit(x[1])) {
1095 const char h1 = x[0], h2 = x[1];
1098 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1099 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1100 else if (h1 >= '0') c = h1 - (int)'0';
1102 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1103 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1104 else if (h2 >= '0') c += h2 - (int)'0';
1112 static bool tldap_find_first_star(const char *val, const char **star)
1116 for (s = val; *s; s++) {
1119 if (isxdigit(s[1]) && isxdigit(s[2])) {
1123 /* not hex based escape, check older syntax */
1132 /* invalid escape sequence */
1137 /* end of val, nothing found */
1147 /* string ended without closing parenthesis, filter is malformed */
1151 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1155 for (i = 0,p = 0; i < *val_len; i++) {
1161 /* these must be escaped */
1165 if (!value[i + 1]) {
1171 c = tldap_hex2char(&value[i]);
1172 if (c >= 0 && c < 256) {
1184 value[p] = value[i];
1193 value[p] = value[i];
1202 static bool tldap_push_filter_basic(struct tldap_context *ld,
1203 struct asn1_data *data,
1205 static bool tldap_push_filter_substring(struct tldap_context *ld,
1206 struct asn1_data *data,
1209 static bool tldap_push_filter_int(struct tldap_context *ld,
1210 struct asn1_data *data,
1213 const char *s = *_s;
1217 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1218 "Incomplete or malformed filter\n");
1223 /* we are right after a parenthesis,
1224 * find out what op we have at hand */
1227 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1228 asn1_push_tag(data, TLDAP_FILTER_AND);
1233 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1234 asn1_push_tag(data, TLDAP_FILTER_OR);
1239 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1240 asn1_push_tag(data, TLDAP_FILTER_NOT);
1242 ret = tldap_push_filter_int(ld, data, &s);
1251 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1252 "Invalid parenthesis '%c'\n", *s);
1256 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1257 "Invalid filter termination\n");
1261 ret = tldap_push_filter_basic(ld, data, &s);
1268 /* only and/or filters get here.
1269 * go through the list of filters */
1272 /* RFC 4526: empty and/or */
1278 ret = tldap_push_filter_int(ld, data, &s);
1284 /* end of list, return */
1292 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1293 "Incomplete or malformed filter\n");
1298 if (data->has_error) {
1307 static bool tldap_push_filter_basic(struct tldap_context *ld,
1308 struct asn1_data *data,
1311 TALLOC_CTX *tmpctx = talloc_tos();
1312 const char *s = *_s;
1320 size_t type_len = 0;
1323 bool write_octect = true;
1326 eq = strchr(s, '=');
1328 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1329 "Invalid filter, missing equal sign\n");
1338 asn1_push_tag(data, TLDAP_FILTER_LE);
1342 asn1_push_tag(data, TLDAP_FILTER_GE);
1346 asn1_push_tag(data, TLDAP_FILTER_APX);
1350 asn1_push_tag(data, TLDAP_FILTER_EXT);
1351 write_octect = false;
1357 if (*s == ':') { /* [:dn]:rule:= value */
1359 /* malformed filter */
1363 } else { /* type[:dn][:rule]:= value */
1365 dn = strchr(s, ':');
1366 type_len = dn - type;
1367 if (dn == e) { /* type:= value */
1374 rule = strchr(dn, ':');
1378 if ((rule == dn + 1) || rule + 1 == e) {
1379 /* malformed filter, contains "::" */
1383 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1388 /* malformed filter. With two
1389 * optionals, the first must be "dn"
1402 if (!type && !dn && !rule) {
1403 /* malformed filter, there must be at least one */
1408 MatchingRuleAssertion ::= SEQUENCE {
1409 matchingRule [1] MatchingRuleID OPTIONAL,
1410 type [2] AttributeDescription OPTIONAL,
1411 matchValue [3] AssertionValue,
1412 dnAttributes [4] BOOLEAN DEFAULT FALSE
1416 /* check and add rule */
1418 ret = tldap_is_attrdesc(rule, e - rule, true);
1422 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1423 asn1_write(data, rule, e - rule);
1427 /* check and add type */
1429 ret = tldap_is_attrdesc(type, type_len, false);
1433 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1434 asn1_write(data, type, type_len);
1438 uval = tldap_get_val(tmpctx, val, _s);
1442 uval_len = *_s - val;
1443 ret = tldap_unescape_inplace(uval, &uval_len);
1448 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1449 asn1_write(data, uval, uval_len);
1452 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1453 asn1_write_uint8(data, dn?1:0);
1460 ret = tldap_is_attrdesc(s, e - s, false);
1465 if (strncmp(val, "*)", 2) == 0) {
1467 asn1_push_tag(data, TLDAP_FILTER_PRES);
1468 asn1_write(data, s, e - s);
1470 write_octect = false;
1474 ret = tldap_find_first_star(val, &star);
1480 asn1_push_tag(data, TLDAP_FILTER_SUB);
1481 asn1_write_OctetString(data, s, e - s);
1482 ret = tldap_push_filter_substring(ld, data, val, &s);
1487 write_octect = false;
1491 /* if nothing else, then it is just equality */
1492 asn1_push_tag(data, TLDAP_FILTER_EQ);
1493 write_octect = true;
1498 uval = tldap_get_val(tmpctx, val, _s);
1502 uval_len = *_s - val;
1503 ret = tldap_unescape_inplace(uval, &uval_len);
1508 asn1_write_OctetString(data, s, e - s);
1509 asn1_write_OctetString(data, uval, uval_len);
1512 if (data->has_error) {
1519 static bool tldap_push_filter_substring(struct tldap_context *ld,
1520 struct asn1_data *data,
1524 TALLOC_CTX *tmpctx = talloc_tos();
1525 bool initial = true;
1532 SubstringFilter ::= SEQUENCE {
1533 type AttributeDescription,
1534 -- at least one must be present
1535 substrings SEQUENCE OF CHOICE {
1536 initial [0] LDAPString,
1538 final [2] LDAPString } }
1540 asn1_push_tag(data, ASN1_SEQUENCE(0));
1543 ret = tldap_find_first_star(val, &star);
1547 chunk_len = star - val;
1551 if (!initial && chunk_len == 0) {
1552 /* found '**', which is illegal */
1568 if (initial && chunk_len == 0) {
1574 chunk = talloc_strndup(tmpctx, val, chunk_len);
1578 ret = tldap_unescape_inplace(chunk, &chunk_len);
1585 asn1_push_tag(data, TLDAP_SUB_INI);
1588 asn1_push_tag(data, TLDAP_SUB_ANY);
1592 asn1_push_tag(data, TLDAP_SUB_FIN);
1598 asn1_write(data, chunk, chunk_len);
1603 } while (*star == '*');
1607 /* end of sequence */
1612 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1613 * around parenthesis, we do not allow any spaces (except in values of
1614 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1615 * leading or trailing spaces where allowed.
1617 static bool tldap_push_filter(struct tldap_context *ld,
1618 struct asn1_data *data,
1621 const char *s = filter;
1624 ret = tldap_push_filter_int(ld, data, &s);
1626 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1627 "Incomplete or malformed filter\n");
1633 /*****************************************************************************/
1635 static void tldap_search_done(struct tevent_req *subreq);
1637 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1638 struct tevent_context *ev,
1639 struct tldap_context *ld,
1640 const char *base, int scope,
1645 struct tldap_control *sctrls,
1647 struct tldap_control *cctrls,
1653 struct tevent_req *req, *subreq;
1654 struct tldap_req_state *state;
1657 req = tldap_req_create(mem_ctx, ld, &state);
1662 asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1663 asn1_write_OctetString(state->out, base, strlen(base));
1664 asn1_write_enumerated(state->out, scope);
1665 asn1_write_enumerated(state->out, deref);
1666 asn1_write_Integer(state->out, sizelimit);
1667 asn1_write_Integer(state->out, timelimit);
1668 asn1_write_BOOLEAN(state->out, attrsonly);
1670 if (!tldap_push_filter(ld, state->out, filter)) {
1671 goto encoding_error;
1674 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1675 for (i=0; i<num_attrs; i++) {
1676 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1678 asn1_pop_tag(state->out);
1679 asn1_pop_tag(state->out);
1681 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1682 sctrls, num_sctrls);
1683 if (tevent_req_nomem(subreq, req)) {
1684 return tevent_req_post(req, ev);
1686 tevent_req_set_callback(subreq, tldap_search_done, req);
1690 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1691 return tevent_req_post(req, ev);
1694 static void tldap_search_done(struct tevent_req *subreq)
1696 struct tevent_req *req = tevent_req_callback_data(
1697 subreq, struct tevent_req);
1698 struct tldap_req_state *state = tevent_req_data(
1699 req, struct tldap_req_state);
1702 err = tldap_msg_recv(subreq, state, &state->result);
1703 if (err != TLDAP_SUCCESS) {
1704 tevent_req_error(req, err);
1707 switch (state->result->type) {
1708 case TLDAP_RES_SEARCH_ENTRY:
1709 case TLDAP_RES_SEARCH_REFERENCE:
1710 if (!tldap_msg_set_pending(subreq)) {
1711 tevent_req_oom(req);
1714 tevent_req_notify_callback(req);
1716 case TLDAP_RES_SEARCH_RESULT:
1717 TALLOC_FREE(subreq);
1718 if (!asn1_start_tag(state->result->data,
1719 state->result->type) ||
1720 !tldap_decode_response(state) ||
1721 !asn1_end_tag(state->result->data) ||
1722 !tldap_decode_controls(state)) {
1723 tevent_req_error(req, TLDAP_DECODING_ERROR);
1726 tevent_req_done(req);
1729 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1734 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1735 struct tldap_message **pmsg)
1737 struct tldap_req_state *state = tevent_req_data(
1738 req, struct tldap_req_state);
1741 if (!tevent_req_is_in_progress(req)
1742 && tevent_req_is_ldap_error(req, &err)) {
1746 if (tevent_req_is_in_progress(req)) {
1747 switch (state->result->type) {
1748 case TLDAP_RES_SEARCH_ENTRY:
1749 case TLDAP_RES_SEARCH_REFERENCE:
1752 return TLDAP_OPERATIONS_ERROR;
1756 *pmsg = talloc_move(mem_ctx, &state->result);
1757 return TLDAP_SUCCESS;
1760 struct tldap_sync_search_state {
1761 TALLOC_CTX *mem_ctx;
1762 struct tldap_message **entries;
1763 struct tldap_message **refs;
1767 static void tldap_search_cb(struct tevent_req *req)
1769 struct tldap_sync_search_state *state =
1770 (struct tldap_sync_search_state *)
1771 tevent_req_callback_data_void(req);
1772 struct tldap_message *msg, **tmp;
1773 int rc, num_entries, num_refs;
1775 rc = tldap_search_recv(req, talloc_tos(), &msg);
1776 if (rc != TLDAP_SUCCESS) {
1781 switch (tldap_msg_type(msg)) {
1782 case TLDAP_RES_SEARCH_ENTRY:
1783 num_entries = talloc_array_length(state->entries);
1784 tmp = talloc_realloc(state->mem_ctx, state->entries,
1785 struct tldap_message *, num_entries + 1);
1787 state->rc = TLDAP_NO_MEMORY;
1790 state->entries = tmp;
1791 state->entries[num_entries] = talloc_move(state->entries,
1794 case TLDAP_RES_SEARCH_REFERENCE:
1795 num_refs = talloc_array_length(state->refs);
1796 tmp = talloc_realloc(state->mem_ctx, state->refs,
1797 struct tldap_message *, num_refs + 1);
1799 state->rc = TLDAP_NO_MEMORY;
1803 state->refs[num_refs] = talloc_move(state->refs, &msg);
1805 case TLDAP_RES_SEARCH_RESULT:
1806 state->rc = TLDAP_SUCCESS;
1809 state->rc = TLDAP_PROTOCOL_ERROR;
1814 int tldap_search(struct tldap_context *ld,
1815 const char *base, int scope, const char *filter,
1816 const char **attrs, int num_attrs, int attrsonly,
1817 struct tldap_control *sctrls, int num_sctrls,
1818 struct tldap_control *cctrls, int num_cctrls,
1819 int timelimit, int sizelimit, int deref,
1820 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1821 struct tldap_message ***refs)
1823 TALLOC_CTX *frame = talloc_stackframe();
1824 struct tevent_context *ev;
1825 struct tevent_req *req;
1826 struct tldap_sync_search_state state;
1829 state.mem_ctx = mem_ctx;
1830 state.rc = TLDAP_SUCCESS;
1832 ev = samba_tevent_context_init(frame);
1834 state.rc = TLDAP_NO_MEMORY;
1838 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1839 attrs, num_attrs, attrsonly,
1840 sctrls, num_sctrls, cctrls, num_cctrls,
1841 timelimit, sizelimit, deref);
1843 state.rc = TLDAP_NO_MEMORY;
1847 tevent_req_set_callback(req, tldap_search_cb, &state);
1849 if (!tevent_req_is_in_progress(req)) {
1850 /* an error happend before sending */
1851 if (tevent_req_is_ldap_error(req, &state.rc)) {
1856 while (tevent_req_is_in_progress(req)
1857 && (state.rc == TLDAP_SUCCESS)) {
1858 if (tevent_loop_once(ev) == -1) {
1859 return TLDAP_OPERATIONS_ERROR;
1863 if (state.rc != TLDAP_SUCCESS) {
1867 if (entries != NULL) {
1868 *entries = state.entries;
1870 TALLOC_FREE(state.entries);
1875 TALLOC_FREE(state.refs);
1877 tldap_save_msg(ld, req);
1883 static bool tldap_parse_search_entry(struct tldap_message *msg)
1885 int num_attribs = 0;
1887 asn1_start_tag(msg->data, msg->type);
1891 asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1892 if (msg->dn == NULL) {
1897 * Attributes: We overallocate msg->attribs by one, so that while
1898 * looping over the attributes we can directly parse into the last
1899 * array element. Same for the values in the inner loop.
1902 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1903 if (msg->attribs == NULL) {
1907 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1908 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1909 struct tldap_attribute *attrib;
1912 attrib = &msg->attribs[num_attribs];
1913 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1914 if (attrib->values == NULL) {
1917 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1918 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1920 asn1_start_tag(msg->data, ASN1_SET);
1922 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1923 asn1_read_OctetString(msg->data, msg,
1924 &attrib->values[num_values]);
1926 attrib->values = talloc_realloc(
1927 msg->attribs, attrib->values, DATA_BLOB,
1929 if (attrib->values == NULL) {
1934 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1935 DATA_BLOB, num_values);
1936 attrib->num_values = num_values;
1938 asn1_end_tag(msg->data); /* ASN1_SET */
1939 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1940 msg->attribs = talloc_realloc(
1941 msg, msg->attribs, struct tldap_attribute,
1943 if (msg->attribs == NULL) {
1948 msg->attribs = talloc_realloc(
1949 msg, msg->attribs, struct tldap_attribute, num_attribs);
1950 asn1_end_tag(msg->data);
1951 if (msg->data->has_error) {
1957 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1959 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1966 bool tldap_entry_attributes(struct tldap_message *msg,
1967 struct tldap_attribute **attributes,
1968 int *num_attributes)
1970 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1973 *attributes = msg->attribs;
1974 *num_attributes = talloc_array_length(msg->attribs);
1978 static bool tldap_decode_controls(struct tldap_req_state *state)
1980 struct tldap_message *msg = state->result;
1981 struct asn1_data *data = msg->data;
1982 struct tldap_control *sctrls = NULL;
1983 int num_controls = 0;
1985 msg->res_sctrls = NULL;
1987 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1991 asn1_start_tag(data, ASN1_CONTEXT(0));
1993 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
1994 struct tldap_control *c;
1997 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
1999 if (sctrls == NULL) {
2002 c = &sctrls[num_controls];
2004 asn1_start_tag(data, ASN1_SEQUENCE(0));
2005 asn1_read_OctetString_talloc(msg, data, &oid);
2006 if ((data->has_error) || (oid == NULL)) {
2010 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2011 asn1_read_BOOLEAN(data, &c->critical);
2013 c->critical = false;
2015 c->value = data_blob_null;
2016 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2017 !asn1_read_OctetString(data, msg, &c->value)) {
2020 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
2025 asn1_end_tag(data); /* ASN1_CONTEXT(0) */
2027 if (data->has_error) {
2028 TALLOC_FREE(sctrls);
2031 msg->res_sctrls = sctrls;
2035 static void tldap_simple_done(struct tevent_req *subreq, int type)
2037 struct tevent_req *req = tevent_req_callback_data(
2038 subreq, struct tevent_req);
2039 struct tldap_req_state *state = tevent_req_data(
2040 req, struct tldap_req_state);
2043 err = tldap_msg_recv(subreq, state, &state->result);
2044 TALLOC_FREE(subreq);
2045 if (err != TLDAP_SUCCESS) {
2046 tevent_req_error(req, err);
2049 if (state->result->type != type) {
2050 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2053 if (!asn1_start_tag(state->result->data, state->result->type) ||
2054 !tldap_decode_response(state) ||
2055 !asn1_end_tag(state->result->data) ||
2056 !tldap_decode_controls(state)) {
2057 tevent_req_error(req, TLDAP_DECODING_ERROR);
2060 if (state->result->lderr != TLDAP_SUCCESS) {
2061 tevent_req_error(req, state->result->lderr);
2064 tevent_req_done(req);
2067 static int tldap_simple_recv(struct tevent_req *req)
2070 if (tevent_req_is_ldap_error(req, &err)) {
2073 return TLDAP_SUCCESS;
2076 static void tldap_add_done(struct tevent_req *subreq);
2078 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2079 struct tevent_context *ev,
2080 struct tldap_context *ld,
2082 struct tldap_mod *attributes,
2084 struct tldap_control *sctrls,
2086 struct tldap_control *cctrls,
2089 struct tevent_req *req, *subreq;
2090 struct tldap_req_state *state;
2093 req = tldap_req_create(mem_ctx, ld, &state);
2098 asn1_push_tag(state->out, TLDAP_REQ_ADD);
2099 asn1_write_OctetString(state->out, dn, strlen(dn));
2100 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2102 for (i=0; i<num_attributes; i++) {
2103 struct tldap_mod *attrib = &attributes[i];
2104 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2105 asn1_write_OctetString(state->out, attrib->attribute,
2106 strlen(attrib->attribute));
2107 asn1_push_tag(state->out, ASN1_SET);
2108 for (j=0; j<attrib->num_values; j++) {
2109 asn1_write_OctetString(state->out,
2110 attrib->values[j].data,
2111 attrib->values[j].length);
2113 asn1_pop_tag(state->out);
2114 asn1_pop_tag(state->out);
2117 asn1_pop_tag(state->out);
2118 asn1_pop_tag(state->out);
2120 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2121 sctrls, num_sctrls);
2122 if (tevent_req_nomem(subreq, req)) {
2123 return tevent_req_post(req, ev);
2125 tevent_req_set_callback(subreq, tldap_add_done, req);
2129 static void tldap_add_done(struct tevent_req *subreq)
2131 tldap_simple_done(subreq, TLDAP_RES_ADD);
2134 int tldap_add_recv(struct tevent_req *req)
2136 return tldap_simple_recv(req);
2139 int tldap_add(struct tldap_context *ld, const char *dn,
2140 struct tldap_mod *attributes, int num_attributes,
2141 struct tldap_control *sctrls, int num_sctrls,
2142 struct tldap_control *cctrls, int num_cctrls)
2144 TALLOC_CTX *frame = talloc_stackframe();
2145 struct tevent_context *ev;
2146 struct tevent_req *req;
2149 ev = samba_tevent_context_init(frame);
2151 result = TLDAP_NO_MEMORY;
2155 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2156 sctrls, num_sctrls, cctrls, num_cctrls);
2158 result = TLDAP_NO_MEMORY;
2162 if (!tevent_req_poll(req, ev)) {
2163 result = TLDAP_OPERATIONS_ERROR;
2167 result = tldap_add_recv(req);
2168 tldap_save_msg(ld, req);
2174 static void tldap_modify_done(struct tevent_req *subreq);
2176 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2177 struct tevent_context *ev,
2178 struct tldap_context *ld,
2180 struct tldap_mod *mods, int num_mods,
2181 struct tldap_control *sctrls,
2183 struct tldap_control *cctrls,
2186 struct tevent_req *req, *subreq;
2187 struct tldap_req_state *state;
2190 req = tldap_req_create(mem_ctx, ld, &state);
2195 asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
2196 asn1_write_OctetString(state->out, dn, strlen(dn));
2197 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2199 for (i=0; i<num_mods; i++) {
2200 struct tldap_mod *mod = &mods[i];
2201 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2202 asn1_write_enumerated(state->out, mod->mod_op),
2203 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2204 asn1_write_OctetString(state->out, mod->attribute,
2205 strlen(mod->attribute));
2206 asn1_push_tag(state->out, ASN1_SET);
2207 for (j=0; j<mod->num_values; j++) {
2208 asn1_write_OctetString(state->out,
2209 mod->values[j].data,
2210 mod->values[j].length);
2212 asn1_pop_tag(state->out);
2213 asn1_pop_tag(state->out);
2214 asn1_pop_tag(state->out);
2217 asn1_pop_tag(state->out);
2218 asn1_pop_tag(state->out);
2220 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2221 sctrls, num_sctrls);
2222 if (tevent_req_nomem(subreq, req)) {
2223 return tevent_req_post(req, ev);
2225 tevent_req_set_callback(subreq, tldap_modify_done, req);
2229 static void tldap_modify_done(struct tevent_req *subreq)
2231 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2234 int tldap_modify_recv(struct tevent_req *req)
2236 return tldap_simple_recv(req);
2239 int tldap_modify(struct tldap_context *ld, const char *dn,
2240 struct tldap_mod *mods, int num_mods,
2241 struct tldap_control *sctrls, int num_sctrls,
2242 struct tldap_control *cctrls, int num_cctrls)
2244 TALLOC_CTX *frame = talloc_stackframe();
2245 struct tevent_context *ev;
2246 struct tevent_req *req;
2249 ev = samba_tevent_context_init(frame);
2251 result = TLDAP_NO_MEMORY;
2255 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2256 sctrls, num_sctrls, cctrls, num_cctrls);
2258 result = TLDAP_NO_MEMORY;
2262 if (!tevent_req_poll(req, ev)) {
2263 result = TLDAP_OPERATIONS_ERROR;
2267 result = tldap_modify_recv(req);
2268 tldap_save_msg(ld, req);
2274 static void tldap_delete_done(struct tevent_req *subreq);
2276 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2277 struct tevent_context *ev,
2278 struct tldap_context *ld,
2280 struct tldap_control *sctrls,
2282 struct tldap_control *cctrls,
2285 struct tevent_req *req, *subreq;
2286 struct tldap_req_state *state;
2288 req = tldap_req_create(mem_ctx, ld, &state);
2293 asn1_push_tag(state->out, TLDAP_REQ_DELETE);
2294 asn1_write(state->out, dn, strlen(dn));
2295 asn1_pop_tag(state->out);
2297 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2298 sctrls, num_sctrls);
2299 if (tevent_req_nomem(subreq, req)) {
2300 return tevent_req_post(req, ev);
2302 tevent_req_set_callback(subreq, tldap_delete_done, req);
2306 static void tldap_delete_done(struct tevent_req *subreq)
2308 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2311 int tldap_delete_recv(struct tevent_req *req)
2313 return tldap_simple_recv(req);
2316 int tldap_delete(struct tldap_context *ld, const char *dn,
2317 struct tldap_control *sctrls, int num_sctrls,
2318 struct tldap_control *cctrls, int num_cctrls)
2320 TALLOC_CTX *frame = talloc_stackframe();
2321 struct tevent_context *ev;
2322 struct tevent_req *req;
2325 ev = samba_tevent_context_init(frame);
2327 result = TLDAP_NO_MEMORY;
2331 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2332 cctrls, num_cctrls);
2334 result = TLDAP_NO_MEMORY;
2338 if (!tevent_req_poll(req, ev)) {
2339 result = TLDAP_OPERATIONS_ERROR;
2343 result = tldap_delete_recv(req);
2344 tldap_save_msg(ld, req);
2350 int tldap_msg_id(const struct tldap_message *msg)
2355 int tldap_msg_type(const struct tldap_message *msg)
2360 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2365 return msg->res_matcheddn;
2368 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2373 return msg->res_diagnosticmessage;
2376 const char *tldap_msg_referral(struct tldap_message *msg)
2381 return msg->res_referral;
2384 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2385 struct tldap_control **sctrls)
2392 *sctrls = msg->res_sctrls;
2393 *num_sctrls = talloc_array_length(msg->res_sctrls);
2396 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2398 return ld->last_msg;
2401 const char *tldap_err2string(int rc)
2403 const char *res = NULL;
2406 * This would normally be a table, but the error codes are not fully
2407 * sequential. Let the compiler figure out the optimum implementation
2413 res = "TLDAP_SUCCESS";
2415 case TLDAP_OPERATIONS_ERROR:
2416 res = "TLDAP_OPERATIONS_ERROR";
2418 case TLDAP_PROTOCOL_ERROR:
2419 res = "TLDAP_PROTOCOL_ERROR";
2421 case TLDAP_TIMELIMIT_EXCEEDED:
2422 res = "TLDAP_TIMELIMIT_EXCEEDED";
2424 case TLDAP_SIZELIMIT_EXCEEDED:
2425 res = "TLDAP_SIZELIMIT_EXCEEDED";
2427 case TLDAP_COMPARE_FALSE:
2428 res = "TLDAP_COMPARE_FALSE";
2430 case TLDAP_COMPARE_TRUE:
2431 res = "TLDAP_COMPARE_TRUE";
2433 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2434 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2436 case TLDAP_STRONG_AUTH_REQUIRED:
2437 res = "TLDAP_STRONG_AUTH_REQUIRED";
2439 case TLDAP_REFERRAL:
2440 res = "TLDAP_REFERRAL";
2442 case TLDAP_ADMINLIMIT_EXCEEDED:
2443 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2445 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2446 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2448 case TLDAP_CONFIDENTIALITY_REQUIRED:
2449 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2451 case TLDAP_SASL_BIND_IN_PROGRESS:
2452 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2454 case TLDAP_NO_SUCH_ATTRIBUTE:
2455 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2457 case TLDAP_UNDEFINED_TYPE:
2458 res = "TLDAP_UNDEFINED_TYPE";
2460 case TLDAP_INAPPROPRIATE_MATCHING:
2461 res = "TLDAP_INAPPROPRIATE_MATCHING";
2463 case TLDAP_CONSTRAINT_VIOLATION:
2464 res = "TLDAP_CONSTRAINT_VIOLATION";
2466 case TLDAP_TYPE_OR_VALUE_EXISTS:
2467 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2469 case TLDAP_INVALID_SYNTAX:
2470 res = "TLDAP_INVALID_SYNTAX";
2472 case TLDAP_NO_SUCH_OBJECT:
2473 res = "TLDAP_NO_SUCH_OBJECT";
2475 case TLDAP_ALIAS_PROBLEM:
2476 res = "TLDAP_ALIAS_PROBLEM";
2478 case TLDAP_INVALID_DN_SYNTAX:
2479 res = "TLDAP_INVALID_DN_SYNTAX";
2482 res = "TLDAP_IS_LEAF";
2484 case TLDAP_ALIAS_DEREF_PROBLEM:
2485 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2487 case TLDAP_INAPPROPRIATE_AUTH:
2488 res = "TLDAP_INAPPROPRIATE_AUTH";
2490 case TLDAP_INVALID_CREDENTIALS:
2491 res = "TLDAP_INVALID_CREDENTIALS";
2493 case TLDAP_INSUFFICIENT_ACCESS:
2494 res = "TLDAP_INSUFFICIENT_ACCESS";
2499 case TLDAP_UNAVAILABLE:
2500 res = "TLDAP_UNAVAILABLE";
2502 case TLDAP_UNWILLING_TO_PERFORM:
2503 res = "TLDAP_UNWILLING_TO_PERFORM";
2505 case TLDAP_LOOP_DETECT:
2506 res = "TLDAP_LOOP_DETECT";
2508 case TLDAP_NAMING_VIOLATION:
2509 res = "TLDAP_NAMING_VIOLATION";
2511 case TLDAP_OBJECT_CLASS_VIOLATION:
2512 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2514 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2515 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2517 case TLDAP_NOT_ALLOWED_ON_RDN:
2518 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2520 case TLDAP_ALREADY_EXISTS:
2521 res = "TLDAP_ALREADY_EXISTS";
2523 case TLDAP_NO_OBJECT_CLASS_MODS:
2524 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2526 case TLDAP_RESULTS_TOO_LARGE:
2527 res = "TLDAP_RESULTS_TOO_LARGE";
2529 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2530 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2533 res = "TLDAP_OTHER";
2535 case TLDAP_SERVER_DOWN:
2536 res = "TLDAP_SERVER_DOWN";
2538 case TLDAP_LOCAL_ERROR:
2539 res = "TLDAP_LOCAL_ERROR";
2541 case TLDAP_ENCODING_ERROR:
2542 res = "TLDAP_ENCODING_ERROR";
2544 case TLDAP_DECODING_ERROR:
2545 res = "TLDAP_DECODING_ERROR";
2548 res = "TLDAP_TIMEOUT";
2550 case TLDAP_AUTH_UNKNOWN:
2551 res = "TLDAP_AUTH_UNKNOWN";
2553 case TLDAP_FILTER_ERROR:
2554 res = "TLDAP_FILTER_ERROR";
2556 case TLDAP_USER_CANCELLED:
2557 res = "TLDAP_USER_CANCELLED";
2559 case TLDAP_PARAM_ERROR:
2560 res = "TLDAP_PARAM_ERROR";
2562 case TLDAP_NO_MEMORY:
2563 res = "TLDAP_NO_MEMORY";
2565 case TLDAP_CONNECT_ERROR:
2566 res = "TLDAP_CONNECT_ERROR";
2568 case TLDAP_NOT_SUPPORTED:
2569 res = "TLDAP_NOT_SUPPORTED";
2571 case TLDAP_CONTROL_NOT_FOUND:
2572 res = "TLDAP_CONTROL_NOT_FOUND";
2574 case TLDAP_NO_RESULTS_RETURNED:
2575 res = "TLDAP_NO_RESULTS_RETURNED";
2577 case TLDAP_MORE_RESULTS_TO_RETURN:
2578 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2580 case TLDAP_CLIENT_LOOP:
2581 res = "TLDAP_CLIENT_LOOP";
2583 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2584 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2587 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2592 res = "Unknown LDAP Error";