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"
25 static int tldap_simple_recv(struct tevent_req *req);
27 bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
29 enum tevent_req_state state;
32 if (!tevent_req_is_error(req, &state, &err)) {
36 case TEVENT_REQ_TIMED_OUT:
37 *perr = TLDAP_TIMEOUT;
39 case TEVENT_REQ_NO_MEMORY:
40 *perr = TLDAP_NO_MEMORY;
42 case TEVENT_REQ_USER_ERROR:
46 *perr = TLDAP_OPERATIONS_ERROR;
52 struct tldap_ctx_attribute {
57 struct tldap_context {
62 struct tstream_context *conn;
65 struct tevent_queue *outgoing;
66 struct tevent_req **pending;
68 /* For the sync wrappers we need something like get_last_error... */
69 struct tldap_message *last_msg;
72 void (*log_fn)(void *context, enum tldap_debug_level level,
73 const char *fmt, va_list ap);
76 struct tldap_ctx_attribute *ctx_attrs;
79 struct tldap_message {
80 struct asn1_data *data;
87 struct tldap_attribute *attribs;
89 /* Error data sent by the server */
92 char *res_diagnosticmessage;
94 struct tldap_control *res_sctrls;
96 /* Controls sent by the server */
97 struct tldap_control *ctrls;
100 void tldap_set_debug(struct tldap_context *ld,
101 void (*log_fn)(void *log_private,
102 enum tldap_debug_level level,
104 va_list ap) PRINTF_ATTRIBUTE(3,0),
108 ld->log_private = log_private;
111 static void tldap_debug(struct tldap_context *ld,
112 enum tldap_debug_level level,
113 const char *fmt, ...)
119 if (ld->log_fn == NULL) {
123 ld->log_fn(ld->log_private, level, fmt, ap);
127 static int tldap_next_msgid(struct tldap_context *ld)
131 result = ld->msgid++;
132 if (ld->msgid == 2147483647) {
138 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
140 struct tldap_context *ctx;
143 ctx = talloc_zero(mem_ctx, struct tldap_context);
147 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
154 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
155 if (ctx->outgoing == NULL) {
162 bool tldap_connection_ok(struct tldap_context *ld)
167 return !ld->server_down;
170 static struct tldap_ctx_attribute *tldap_context_findattr(
171 struct tldap_context *ld, const char *name)
175 num_attrs = talloc_array_length(ld->ctx_attrs);
177 for (i=0; i<num_attrs; i++) {
178 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
179 return &ld->ctx_attrs[i];
185 bool tldap_context_setattr(struct tldap_context *ld,
186 const char *name, const void *_pptr)
188 struct tldap_ctx_attribute *tmp, *attr;
191 void **pptr = (void **)_pptr;
193 attr = tldap_context_findattr(ld, name);
196 * We don't actually delete attrs, we don't expect tons of
197 * attributes being shuffled around.
199 TALLOC_FREE(attr->ptr);
201 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
207 tmpname = talloc_strdup(ld, name);
208 if (tmpname == NULL) {
212 num_attrs = talloc_array_length(ld->ctx_attrs);
214 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
217 TALLOC_FREE(tmpname);
220 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
222 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
224 tmp[num_attrs].ptr = NULL;
231 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
233 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
241 struct read_ldap_state {
246 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
247 static void read_ldap_done(struct tevent_req *subreq);
249 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
250 struct tevent_context *ev,
251 struct tstream_context *conn)
253 struct tevent_req *req, *subreq;
254 struct read_ldap_state *state;
256 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
262 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
264 if (tevent_req_nomem(subreq, req)) {
265 return tevent_req_post(req, ev);
267 tevent_req_set_callback(subreq, read_ldap_done, req);
271 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
273 struct read_ldap_state *state = talloc_get_type_abort(
274 private_data, struct read_ldap_state);
279 /* We've been here, we're done */
284 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
286 if (buf[0] != 0x30) {
291 if ((len & 0x80) == 0) {
296 lensize = (len & 0x7f);
300 /* Please get us the full length */
303 if (buflen > 2 + lensize) {
307 if (buflen != 2 + lensize) {
311 for (i=0; i<lensize; i++) {
312 len = (len << 8) | buf[2+i];
317 static void read_ldap_done(struct tevent_req *subreq)
319 struct tevent_req *req = tevent_req_callback_data(
320 subreq, struct tevent_req);
321 struct read_ldap_state *state = tevent_req_data(
322 req, struct read_ldap_state);
326 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
329 tevent_req_error(req, err);
332 tevent_req_done(req);
335 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
336 uint8_t **pbuf, int *perrno)
338 struct read_ldap_state *state = tevent_req_data(
339 req, struct read_ldap_state);
341 if (tevent_req_is_unix_error(req, perrno)) {
344 *pbuf = talloc_move(mem_ctx, &state->buf);
345 return talloc_get_size(*pbuf);
348 struct tldap_msg_state {
349 struct tldap_context *ld;
350 struct tevent_context *ev;
354 struct asn1_data *data;
358 static void tldap_push_controls(struct asn1_data *data,
359 struct tldap_control *sctrls,
364 if ((sctrls == NULL) || (num_sctrls == 0)) {
368 asn1_push_tag(data, ASN1_CONTEXT(0));
370 for (i=0; i<num_sctrls; i++) {
371 struct tldap_control *c = &sctrls[i];
372 asn1_push_tag(data, ASN1_SEQUENCE(0));
373 asn1_write_OctetString(data, c->oid, strlen(c->oid));
375 asn1_write_BOOLEAN(data, true);
377 if (c->value.data != NULL) {
378 asn1_write_OctetString(data, c->value.data,
381 asn1_pop_tag(data); /* ASN1_SEQUENCE(0) */
384 asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
387 static void tldap_msg_sent(struct tevent_req *subreq);
388 static void tldap_msg_received(struct tevent_req *subreq);
390 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
391 struct tevent_context *ev,
392 struct tldap_context *ld,
393 int id, struct asn1_data *data,
394 struct tldap_control *sctrls,
397 struct tevent_req *req, *subreq;
398 struct tldap_msg_state *state;
401 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
404 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
412 if (state->ld->server_down) {
413 tevent_req_error(req, TLDAP_SERVER_DOWN);
414 return tevent_req_post(req, ev);
417 tldap_push_controls(data, sctrls, num_sctrls);
421 if (!asn1_blob(data, &blob)) {
422 tevent_req_error(req, TLDAP_ENCODING_ERROR);
423 return tevent_req_post(req, ev);
426 state->iov.iov_base = (void *)blob.data;
427 state->iov.iov_len = blob.length;
429 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
431 if (tevent_req_nomem(subreq, req)) {
432 return tevent_req_post(req, ev);
434 tevent_req_set_callback(subreq, tldap_msg_sent, req);
438 static void tldap_msg_unset_pending(struct tevent_req *req)
440 struct tldap_msg_state *state = tevent_req_data(
441 req, struct tldap_msg_state);
442 struct tldap_context *ld = state->ld;
443 int num_pending = talloc_array_length(ld->pending);
446 if (num_pending == 1) {
447 TALLOC_FREE(ld->pending);
451 for (i=0; i<num_pending; i++) {
452 if (req == ld->pending[i]) {
456 if (i == num_pending) {
458 * Something's seriously broken. Just returning here is the
459 * right thing nevertheless, the point of this routine is to
460 * remove ourselves from cli->pending.
466 * Remove ourselves from the cli->pending array
468 if (num_pending > 1) {
469 ld->pending[i] = ld->pending[num_pending-1];
473 * No NULL check here, we're shrinking by sizeof(void *), and
474 * talloc_realloc just adjusts the size for this.
476 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
481 static int tldap_msg_destructor(struct tevent_req *req)
483 tldap_msg_unset_pending(req);
487 static bool tldap_msg_set_pending(struct tevent_req *req)
489 struct tldap_msg_state *state = tevent_req_data(
490 req, struct tldap_msg_state);
491 struct tldap_context *ld;
492 struct tevent_req **pending;
494 struct tevent_req *subreq;
497 num_pending = talloc_array_length(ld->pending);
499 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
501 if (pending == NULL) {
504 pending[num_pending] = req;
505 ld->pending = pending;
506 talloc_set_destructor(req, tldap_msg_destructor);
508 if (num_pending > 0) {
513 * We're the first one, add the read_ldap request that waits for the
514 * answer from the server
516 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
517 if (subreq == NULL) {
518 tldap_msg_unset_pending(req);
521 tevent_req_set_callback(subreq, tldap_msg_received, ld);
525 static void tldap_msg_sent(struct tevent_req *subreq)
527 struct tevent_req *req = tevent_req_callback_data(
528 subreq, struct tevent_req);
529 struct tldap_msg_state *state = tevent_req_data(
530 req, struct tldap_msg_state);
534 nwritten = tstream_writev_queue_recv(subreq, &err);
536 if (nwritten == -1) {
537 state->ld->server_down = true;
538 tevent_req_error(req, TLDAP_SERVER_DOWN);
542 if (!tldap_msg_set_pending(req)) {
543 tevent_req_nomem(NULL, req);
548 static int tldap_msg_msgid(struct tevent_req *req)
550 struct tldap_msg_state *state = tevent_req_data(
551 req, struct tldap_msg_state);
556 static void tldap_msg_received(struct tevent_req *subreq)
558 struct tldap_context *ld = tevent_req_callback_data(
559 subreq, struct tldap_context);
560 struct tevent_req *req;
561 struct tldap_msg_state *state;
562 struct asn1_data *data;
571 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
573 if (received == -1) {
574 status = TLDAP_SERVER_DOWN;
578 data = asn1_init(talloc_tos());
580 status = TLDAP_NO_MEMORY;
583 asn1_load_nocopy(data, inbuf, received);
586 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
587 ok &= asn1_read_Integer(data, &id);
588 ok &= asn1_peek_uint8(data, &type);
591 status = TLDAP_PROTOCOL_ERROR;
595 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
596 "type %d\n", id, (int)type);
598 num_pending = talloc_array_length(ld->pending);
600 for (i=0; i<num_pending; i++) {
601 if (id == tldap_msg_msgid(ld->pending[i])) {
605 if (i == num_pending) {
606 /* Dump unexpected reply */
607 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
608 "No request pending for msg %d\n", id);
614 req = ld->pending[i];
615 state = tevent_req_data(req, struct tldap_msg_state);
617 state->inbuf = talloc_move(state, &inbuf);
618 state->data = talloc_move(state, &data);
620 talloc_set_destructor(req, NULL);
621 tldap_msg_unset_pending(req);
622 num_pending = talloc_array_length(ld->pending);
624 tevent_req_done(req);
627 if (num_pending == 0) {
630 if (talloc_array_length(ld->pending) > num_pending) {
632 * The callback functions called from tevent_req_done() above
633 * have put something on the pending queue. We don't have to
634 * trigger the read_ldap_send(), tldap_msg_set_pending() has
635 * done it for us already.
640 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
641 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
642 if (subreq == NULL) {
643 status = TLDAP_NO_MEMORY;
646 tevent_req_set_callback(subreq, tldap_msg_received, ld);
650 while (talloc_array_length(ld->pending) > 0) {
651 req = ld->pending[0];
652 talloc_set_destructor(req, NULL);
653 tldap_msg_destructor(req);
654 tevent_req_error(req, status);
658 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
659 struct tldap_message **pmsg)
661 struct tldap_msg_state *state = tevent_req_data(
662 req, struct tldap_msg_state);
663 struct tldap_message *msg;
667 if (tevent_req_is_ldap_error(req, &err)) {
671 if (!asn1_peek_uint8(state->data, &msgtype)) {
672 return TLDAP_PROTOCOL_ERROR;
676 return TLDAP_SUCCESS;
679 msg = talloc_zero(mem_ctx, struct tldap_message);
681 return TLDAP_NO_MEMORY;
685 msg->inbuf = talloc_move(msg, &state->inbuf);
686 msg->data = talloc_move(msg, &state->data);
690 return TLDAP_SUCCESS;
693 struct tldap_req_state {
695 struct asn1_data *out;
696 struct tldap_message *result;
699 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
700 struct tldap_context *ld,
701 struct tldap_req_state **pstate)
703 struct tevent_req *req;
704 struct tldap_req_state *state;
706 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
711 state->out = asn1_init(state);
712 if (state->out == NULL) {
716 state->result = NULL;
717 state->id = tldap_next_msgid(ld);
719 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
720 asn1_write_Integer(state->out, state->id);
726 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
728 struct tldap_req_state *state = tevent_req_data(
729 req, struct tldap_req_state);
731 TALLOC_FREE(ld->last_msg);
732 ld->last_msg = talloc_move(ld, &state->result);
735 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
737 char *result = talloc_array(mem_ctx, char, blob.length+1);
739 if (result == NULL) {
743 memcpy(result, blob.data, blob.length);
744 result[blob.length] = '\0';
748 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
749 struct asn1_data *data,
754 if (!asn1_read_OctetString(data, mem_ctx, &string))
757 result = blob2string_talloc(mem_ctx, string);
759 data_blob_free(&string);
761 if (result == NULL) {
768 static bool tldap_decode_controls(struct tldap_req_state *state);
770 static bool tldap_decode_response(struct tldap_req_state *state)
772 struct asn1_data *data = state->result->data;
773 struct tldap_message *msg = state->result;
776 ok &= asn1_read_enumerated(data, &msg->lderr);
777 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
778 ok &= asn1_read_OctetString_talloc(msg, data,
779 &msg->res_diagnosticmessage);
780 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
781 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
782 ok &= asn1_read_OctetString_talloc(msg, data,
784 ok &= asn1_end_tag(data);
786 msg->res_referral = NULL;
792 static void tldap_sasl_bind_done(struct tevent_req *subreq);
794 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
795 struct tevent_context *ev,
796 struct tldap_context *ld,
798 const char *mechanism,
800 struct tldap_control *sctrls,
802 struct tldap_control *cctrls,
805 struct tevent_req *req, *subreq;
806 struct tldap_req_state *state;
808 req = tldap_req_create(mem_ctx, ld, &state);
817 asn1_push_tag(state->out, TLDAP_REQ_BIND);
818 asn1_write_Integer(state->out, ld->ld_version);
819 asn1_write_OctetString(state->out, dn, (dn != NULL) ? strlen(dn) : 0);
821 if (mechanism == NULL) {
822 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
823 asn1_write(state->out, creds->data, creds->length);
824 asn1_pop_tag(state->out);
826 asn1_push_tag(state->out, ASN1_CONTEXT(3));
827 asn1_write_OctetString(state->out, mechanism,
829 if ((creds != NULL) && (creds->data != NULL)) {
830 asn1_write_OctetString(state->out, creds->data,
833 asn1_pop_tag(state->out);
836 if (!asn1_pop_tag(state->out)) {
837 tevent_req_error(req, TLDAP_ENCODING_ERROR);
838 return tevent_req_post(req, ev);
841 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
843 if (tevent_req_nomem(subreq, req)) {
844 return tevent_req_post(req, ev);
846 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
850 static void tldap_sasl_bind_done(struct tevent_req *subreq)
852 struct tevent_req *req = tevent_req_callback_data(
853 subreq, struct tevent_req);
854 struct tldap_req_state *state = tevent_req_data(
855 req, struct tldap_req_state);
858 err = tldap_msg_recv(subreq, state, &state->result);
860 if (err != TLDAP_SUCCESS) {
861 tevent_req_error(req, err);
864 if (state->result->type != TLDAP_RES_BIND) {
865 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
868 if (!asn1_start_tag(state->result->data, state->result->type) ||
869 !tldap_decode_response(state) ||
870 !asn1_end_tag(state->result->data)) {
871 tevent_req_error(req, TLDAP_DECODING_ERROR);
875 * TODO: pull the reply blob
877 if (state->result->lderr != TLDAP_SUCCESS) {
878 tevent_req_error(req, state->result->lderr);
881 tevent_req_done(req);
884 int tldap_sasl_bind_recv(struct tevent_req *req)
886 return tldap_simple_recv(req);
889 int tldap_sasl_bind(struct tldap_context *ld,
891 const char *mechanism,
893 struct tldap_control *sctrls,
895 struct tldap_control *cctrls,
898 TALLOC_CTX *frame = talloc_stackframe();
899 struct tevent_context *ev;
900 struct tevent_req *req;
903 ev = event_context_init(frame);
905 result = TLDAP_NO_MEMORY;
909 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
910 sctrls, num_sctrls, cctrls, num_cctrls);
912 result = TLDAP_NO_MEMORY;
916 if (!tevent_req_poll(req, ev)) {
917 result = TLDAP_OPERATIONS_ERROR;
921 result = tldap_sasl_bind_recv(req);
922 tldap_save_msg(ld, req);
928 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
929 struct tevent_context *ev,
930 struct tldap_context *ld,
936 if (passwd != NULL) {
937 cred.data = (uint8_t *)passwd;
938 cred.length = strlen(passwd);
940 cred.data = (uint8_t *)"";
943 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
947 int tldap_simple_bind_recv(struct tevent_req *req)
949 return tldap_sasl_bind_recv(req);
952 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
957 if (passwd != NULL) {
958 cred.data = (uint8_t *)passwd;
959 cred.length = strlen(passwd);
961 cred.data = (uint8_t *)"";
964 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
967 /*****************************************************************************/
969 /* can't use isalpha() as only a strict set is valid for LDAP */
971 static bool tldap_is_alpha(char c)
973 return (((c >= 'a') && (c <= 'z')) || \
974 ((c >= 'A') && (c <= 'Z')));
977 static bool tldap_is_adh(char c)
979 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
982 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
983 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
984 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
985 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
986 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
987 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
988 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
989 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
990 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
991 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
993 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
994 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
995 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
998 /* oid's should be numerical only in theory,
999 * but apparently some broken servers may have alphanum aliases instead.
1000 * Do like openldap libraries and allow alphanum aliases for oids, but
1001 * do not allow Tagging options in that case.
1003 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1005 bool is_oid = false;
1009 /* first char has stricter rules */
1012 } else if (!tldap_is_alpha(*s)) {
1013 /* bad first char */
1017 for (i = 1; i < len; i++) {
1020 if (isdigit(s[i])) {
1033 if (tldap_is_adh(s[i])) {
1040 /* no tagging options */
1047 if ((i + 1) == len) {
1065 /* this function copies the value until the closing parenthesis is found. */
1066 static char *tldap_get_val(TALLOC_CTX *memctx,
1067 const char *value, const char **_s)
1069 const char *s = value;
1071 /* find terminator */
1074 if (s && (*(s - 1) == '\\')) {
1079 if (!s || !(*s == ')')) {
1080 /* malformed filter */
1086 return talloc_strndup(memctx, value, s - value);
1089 static int tldap_hex2char(const char *x)
1091 if (isxdigit(x[0]) && isxdigit(x[1])) {
1092 const char h1 = x[0], h2 = x[1];
1095 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1096 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1097 else if (h1 >= '0') c = h1 - (int)'0';
1099 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1100 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1101 else if (h2 >= '0') c += h2 - (int)'0';
1109 static bool tldap_find_first_star(const char *val, const char **star)
1113 for (s = val; *s; s++) {
1116 if (isxdigit(s[1]) && isxdigit(s[2])) {
1120 /* not hex based escape, check older syntax */
1129 /* invalid escape sequence */
1134 /* end of val, nothing found */
1144 /* string ended without closing parenthesis, filter is malformed */
1148 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1152 for (i = 0,p = 0; i < *val_len; i++) {
1158 /* these must be escaped */
1162 if (!value[i + 1]) {
1168 c = tldap_hex2char(&value[i]);
1169 if (c >= 0 && c < 256) {
1181 value[p] = value[i];
1190 value[p] = value[i];
1199 static bool tldap_push_filter_basic(struct tldap_context *ld,
1200 struct asn1_data *data,
1202 static bool tldap_push_filter_substring(struct tldap_context *ld,
1203 struct asn1_data *data,
1206 static bool tldap_push_filter_int(struct tldap_context *ld,
1207 struct asn1_data *data,
1210 const char *s = *_s;
1214 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1215 "Incomplete or malformed filter\n");
1220 /* we are right after a parenthesis,
1221 * find out what op we have at hand */
1224 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1225 asn1_push_tag(data, TLDAP_FILTER_AND);
1230 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1231 asn1_push_tag(data, TLDAP_FILTER_OR);
1236 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1237 asn1_push_tag(data, TLDAP_FILTER_NOT);
1239 ret = tldap_push_filter_int(ld, data, &s);
1248 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1249 "Invalid parenthesis '%c'\n", *s);
1253 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1254 "Invalid filter termination\n");
1258 ret = tldap_push_filter_basic(ld, data, &s);
1265 /* only and/or filters get here.
1266 * go through the list of filters */
1269 /* RFC 4526: empty and/or */
1275 ret = tldap_push_filter_int(ld, data, &s);
1281 /* end of list, return */
1289 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1290 "Incomplete or malformed filter\n");
1295 if (data->has_error) {
1304 static bool tldap_push_filter_basic(struct tldap_context *ld,
1305 struct asn1_data *data,
1308 TALLOC_CTX *tmpctx = talloc_tos();
1309 const char *s = *_s;
1317 size_t type_len = 0;
1320 bool write_octect = true;
1323 eq = strchr(s, '=');
1325 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1326 "Invalid filter, missing equal sign\n");
1335 asn1_push_tag(data, TLDAP_FILTER_LE);
1339 asn1_push_tag(data, TLDAP_FILTER_GE);
1343 asn1_push_tag(data, TLDAP_FILTER_APX);
1347 asn1_push_tag(data, TLDAP_FILTER_EXT);
1348 write_octect = false;
1354 if (*s == ':') { /* [:dn]:rule:= value */
1356 /* malformed filter */
1360 } else { /* type[:dn][:rule]:= value */
1362 dn = strchr(s, ':');
1363 type_len = dn - type;
1364 if (dn == e) { /* type:= value */
1371 rule = strchr(dn, ':');
1372 if ((rule == dn + 1) || rule + 1 == e) {
1373 /* malformed filter, contains "::" */
1377 if (StrnCaseCmp(dn, "dn:", 3) != 0) {
1382 /* malformed filter. With two
1383 * optionals, the first must be "dn"
1396 if (!type && !dn && !rule) {
1397 /* malformed filter, there must be at least one */
1402 MatchingRuleAssertion ::= SEQUENCE {
1403 matchingRule [1] MatchingRuleID OPTIONAL,
1404 type [2] AttributeDescription OPTIONAL,
1405 matchValue [3] AssertionValue,
1406 dnAttributes [4] BOOLEAN DEFAULT FALSE
1410 /* check and add rule */
1412 ret = tldap_is_attrdesc(rule, e - rule, true);
1416 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1417 asn1_write(data, rule, e - rule);
1421 /* check and add type */
1423 ret = tldap_is_attrdesc(type, type_len, false);
1427 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1428 asn1_write(data, type, type_len);
1432 uval = tldap_get_val(tmpctx, val, _s);
1436 uval_len = *_s - val;
1437 ret = tldap_unescape_inplace(uval, &uval_len);
1442 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1443 asn1_write(data, uval, uval_len);
1446 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1447 asn1_write_uint8(data, dn?1:0);
1454 ret = tldap_is_attrdesc(s, e - s, false);
1459 if (strncmp(val, "*)", 2) == 0) {
1461 asn1_push_tag(data, TLDAP_FILTER_PRES);
1462 asn1_write(data, s, e - s);
1464 write_octect = false;
1468 ret = tldap_find_first_star(val, &star);
1474 asn1_push_tag(data, TLDAP_FILTER_SUB);
1475 asn1_write_OctetString(data, s, e - s);
1476 ret = tldap_push_filter_substring(ld, data, val, &s);
1481 write_octect = false;
1485 /* if nothing else, then it is just equality */
1486 asn1_push_tag(data, TLDAP_FILTER_EQ);
1487 write_octect = true;
1492 uval = tldap_get_val(tmpctx, val, _s);
1496 uval_len = *_s - val;
1497 ret = tldap_unescape_inplace(uval, &uval_len);
1502 asn1_write_OctetString(data, s, e - s);
1503 asn1_write_OctetString(data, uval, uval_len);
1506 if (data->has_error) {
1513 static bool tldap_push_filter_substring(struct tldap_context *ld,
1514 struct asn1_data *data,
1518 TALLOC_CTX *tmpctx = talloc_tos();
1519 bool initial = true;
1526 SubstringFilter ::= SEQUENCE {
1527 type AttributeDescription,
1528 -- at least one must be present
1529 substrings SEQUENCE OF CHOICE {
1530 initial [0] LDAPString,
1532 final [2] LDAPString } }
1534 asn1_push_tag(data, ASN1_SEQUENCE(0));
1537 ret = tldap_find_first_star(val, &star);
1541 chunk_len = star - val;
1545 if (!initial && chunk_len == 0) {
1546 /* found '**', which is illegal */
1562 if (initial && chunk_len == 0) {
1568 chunk = talloc_strndup(tmpctx, val, chunk_len);
1572 ret = tldap_unescape_inplace(chunk, &chunk_len);
1579 asn1_push_tag(data, TLDAP_SUB_INI);
1582 asn1_push_tag(data, TLDAP_SUB_ANY);
1586 asn1_push_tag(data, TLDAP_SUB_FIN);
1592 asn1_write(data, chunk, chunk_len);
1597 } while (*star == '*');
1601 /* end of sequence */
1606 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1607 * around parenthesis, we do not allow any spaces (except in values of
1608 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1609 * leading or trailing spaces where allowed.
1611 static bool tldap_push_filter(struct tldap_context *ld,
1612 struct asn1_data *data,
1615 const char *s = filter;
1618 ret = tldap_push_filter_int(ld, data, &s);
1620 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1621 "Incomplete or malformed filter\n");
1627 /*****************************************************************************/
1629 static void tldap_search_done(struct tevent_req *subreq);
1631 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1632 struct tevent_context *ev,
1633 struct tldap_context *ld,
1634 const char *base, int scope,
1639 struct tldap_control *sctrls,
1641 struct tldap_control *cctrls,
1647 struct tevent_req *req, *subreq;
1648 struct tldap_req_state *state;
1651 req = tldap_req_create(mem_ctx, ld, &state);
1656 asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1657 asn1_write_OctetString(state->out, base, strlen(base));
1658 asn1_write_enumerated(state->out, scope);
1659 asn1_write_enumerated(state->out, deref);
1660 asn1_write_Integer(state->out, sizelimit);
1661 asn1_write_Integer(state->out, timelimit);
1662 asn1_write_BOOLEAN(state->out, attrsonly);
1664 if (!tldap_push_filter(ld, state->out, filter)) {
1665 goto encoding_error;
1668 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1669 for (i=0; i<num_attrs; i++) {
1670 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1672 asn1_pop_tag(state->out);
1673 asn1_pop_tag(state->out);
1675 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1676 sctrls, num_sctrls);
1677 if (tevent_req_nomem(subreq, req)) {
1678 return tevent_req_post(req, ev);
1680 tevent_req_set_callback(subreq, tldap_search_done, req);
1684 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1685 return tevent_req_post(req, ev);
1688 static void tldap_search_done(struct tevent_req *subreq)
1690 struct tevent_req *req = tevent_req_callback_data(
1691 subreq, struct tevent_req);
1692 struct tldap_req_state *state = tevent_req_data(
1693 req, struct tldap_req_state);
1696 err = tldap_msg_recv(subreq, state, &state->result);
1697 if (err != TLDAP_SUCCESS) {
1698 tevent_req_error(req, err);
1701 switch (state->result->type) {
1702 case TLDAP_RES_SEARCH_ENTRY:
1703 case TLDAP_RES_SEARCH_REFERENCE:
1704 if (!tldap_msg_set_pending(subreq)) {
1705 tevent_req_nomem(NULL, req);
1708 tevent_req_notify_callback(req);
1710 case TLDAP_RES_SEARCH_RESULT:
1711 TALLOC_FREE(subreq);
1712 if (!asn1_start_tag(state->result->data,
1713 state->result->type) ||
1714 !tldap_decode_response(state) ||
1715 !asn1_end_tag(state->result->data) ||
1716 !tldap_decode_controls(state)) {
1717 tevent_req_error(req, TLDAP_DECODING_ERROR);
1720 tevent_req_done(req);
1723 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1728 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1729 struct tldap_message **pmsg)
1731 struct tldap_req_state *state = tevent_req_data(
1732 req, struct tldap_req_state);
1735 if (!tevent_req_is_in_progress(req)
1736 && tevent_req_is_ldap_error(req, &err)) {
1740 if (tevent_req_is_in_progress(req)) {
1741 switch (state->result->type) {
1742 case TLDAP_RES_SEARCH_ENTRY:
1743 case TLDAP_RES_SEARCH_REFERENCE:
1746 return TLDAP_OPERATIONS_ERROR;
1750 *pmsg = talloc_move(mem_ctx, &state->result);
1751 return TLDAP_SUCCESS;
1754 struct tldap_sync_search_state {
1755 TALLOC_CTX *mem_ctx;
1756 struct tldap_message **entries;
1757 struct tldap_message **refs;
1761 static void tldap_search_cb(struct tevent_req *req)
1763 struct tldap_sync_search_state *state =
1764 (struct tldap_sync_search_state *)
1765 tevent_req_callback_data_void(req);
1766 struct tldap_message *msg, **tmp;
1767 int rc, num_entries, num_refs;
1769 rc = tldap_search_recv(req, talloc_tos(), &msg);
1770 if (rc != TLDAP_SUCCESS) {
1775 switch (tldap_msg_type(msg)) {
1776 case TLDAP_RES_SEARCH_ENTRY:
1777 num_entries = talloc_array_length(state->entries);
1778 tmp = talloc_realloc(state->mem_ctx, state->entries,
1779 struct tldap_message *, num_entries + 1);
1781 state->rc = TLDAP_NO_MEMORY;
1784 state->entries = tmp;
1785 state->entries[num_entries] = talloc_move(state->entries,
1788 case TLDAP_RES_SEARCH_REFERENCE:
1789 num_refs = talloc_array_length(state->refs);
1790 tmp = talloc_realloc(state->mem_ctx, state->refs,
1791 struct tldap_message *, num_refs + 1);
1793 state->rc = TLDAP_NO_MEMORY;
1797 state->refs[num_refs] = talloc_move(state->refs, &msg);
1799 case TLDAP_RES_SEARCH_RESULT:
1800 state->rc = TLDAP_SUCCESS;
1803 state->rc = TLDAP_PROTOCOL_ERROR;
1808 int tldap_search(struct tldap_context *ld,
1809 const char *base, int scope, const char *filter,
1810 const char **attrs, int num_attrs, int attrsonly,
1811 struct tldap_control *sctrls, int num_sctrls,
1812 struct tldap_control *cctrls, int num_cctrls,
1813 int timelimit, int sizelimit, int deref,
1814 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1815 struct tldap_message ***refs)
1817 TALLOC_CTX *frame = talloc_stackframe();
1818 struct tevent_context *ev;
1819 struct tevent_req *req;
1820 struct tldap_sync_search_state state;
1823 state.mem_ctx = mem_ctx;
1824 state.rc = TLDAP_SUCCESS;
1826 ev = event_context_init(frame);
1828 state.rc = TLDAP_NO_MEMORY;
1832 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1833 attrs, num_attrs, attrsonly,
1834 sctrls, num_sctrls, cctrls, num_cctrls,
1835 timelimit, sizelimit, deref);
1837 state.rc = TLDAP_NO_MEMORY;
1841 tevent_req_set_callback(req, tldap_search_cb, &state);
1843 if (!tevent_req_is_in_progress(req)) {
1844 /* an error happend before sending */
1845 if (tevent_req_is_ldap_error(req, &state.rc)) {
1850 while (tevent_req_is_in_progress(req)
1851 && (state.rc == TLDAP_SUCCESS)) {
1852 if (tevent_loop_once(ev) == -1) {
1853 return TLDAP_OPERATIONS_ERROR;
1857 if (state.rc != TLDAP_SUCCESS) {
1861 if (entries != NULL) {
1862 *entries = state.entries;
1864 TALLOC_FREE(state.entries);
1869 TALLOC_FREE(state.refs);
1871 tldap_save_msg(ld, req);
1877 static bool tldap_parse_search_entry(struct tldap_message *msg)
1879 int num_attribs = 0;
1881 asn1_start_tag(msg->data, msg->type);
1885 asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1886 if (msg->dn == NULL) {
1891 * Attributes: We overallocate msg->attribs by one, so that while
1892 * looping over the attributes we can directly parse into the last
1893 * array element. Same for the values in the inner loop.
1896 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1897 if (msg->attribs == NULL) {
1901 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1902 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1903 struct tldap_attribute *attrib;
1906 attrib = &msg->attribs[num_attribs];
1907 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1908 if (attrib->values == NULL) {
1911 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1912 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1914 asn1_start_tag(msg->data, ASN1_SET);
1916 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1917 asn1_read_OctetString(msg->data, msg,
1918 &attrib->values[num_values]);
1920 attrib->values = talloc_realloc(
1921 msg->attribs, attrib->values, DATA_BLOB,
1923 if (attrib->values == NULL) {
1928 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1929 DATA_BLOB, num_values);
1930 attrib->num_values = num_values;
1932 asn1_end_tag(msg->data); /* ASN1_SET */
1933 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1934 msg->attribs = talloc_realloc(
1935 msg, msg->attribs, struct tldap_attribute,
1937 if (msg->attribs == NULL) {
1942 msg->attribs = talloc_realloc(
1943 msg, msg->attribs, struct tldap_attribute, num_attribs);
1944 asn1_end_tag(msg->data);
1945 if (msg->data->has_error) {
1951 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1953 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1960 bool tldap_entry_attributes(struct tldap_message *msg,
1961 struct tldap_attribute **attributes,
1962 int *num_attributes)
1964 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1967 *attributes = msg->attribs;
1968 *num_attributes = talloc_array_length(msg->attribs);
1972 static bool tldap_decode_controls(struct tldap_req_state *state)
1974 struct tldap_message *msg = state->result;
1975 struct asn1_data *data = msg->data;
1976 struct tldap_control *sctrls = NULL;
1977 int num_controls = 0;
1979 msg->res_sctrls = NULL;
1981 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1985 asn1_start_tag(data, ASN1_CONTEXT(0));
1987 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
1988 struct tldap_control *c;
1991 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
1993 if (sctrls == NULL) {
1996 c = &sctrls[num_controls];
1998 asn1_start_tag(data, ASN1_SEQUENCE(0));
1999 asn1_read_OctetString_talloc(msg, data, &oid);
2000 if ((data->has_error) || (oid == NULL)) {
2004 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2005 asn1_read_BOOLEAN(data, &c->critical);
2007 c->critical = false;
2009 c->value = data_blob_null;
2010 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2011 !asn1_read_OctetString(data, msg, &c->value)) {
2014 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
2019 asn1_end_tag(data); /* ASN1_CONTEXT(0) */
2021 if (data->has_error) {
2022 TALLOC_FREE(sctrls);
2025 msg->res_sctrls = sctrls;
2029 static void tldap_simple_done(struct tevent_req *subreq, int type)
2031 struct tevent_req *req = tevent_req_callback_data(
2032 subreq, struct tevent_req);
2033 struct tldap_req_state *state = tevent_req_data(
2034 req, struct tldap_req_state);
2037 err = tldap_msg_recv(subreq, state, &state->result);
2038 TALLOC_FREE(subreq);
2039 if (err != TLDAP_SUCCESS) {
2040 tevent_req_error(req, err);
2043 if (state->result->type != type) {
2044 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2047 if (!asn1_start_tag(state->result->data, state->result->type) ||
2048 !tldap_decode_response(state) ||
2049 !asn1_end_tag(state->result->data) ||
2050 !tldap_decode_controls(state)) {
2051 tevent_req_error(req, TLDAP_DECODING_ERROR);
2054 if (state->result->lderr != TLDAP_SUCCESS) {
2055 tevent_req_error(req, state->result->lderr);
2058 tevent_req_done(req);
2061 static int tldap_simple_recv(struct tevent_req *req)
2064 if (tevent_req_is_ldap_error(req, &err)) {
2067 return TLDAP_SUCCESS;
2070 static void tldap_add_done(struct tevent_req *subreq);
2072 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2073 struct tevent_context *ev,
2074 struct tldap_context *ld,
2076 struct tldap_mod *attributes,
2078 struct tldap_control *sctrls,
2080 struct tldap_control *cctrls,
2083 struct tevent_req *req, *subreq;
2084 struct tldap_req_state *state;
2087 req = tldap_req_create(mem_ctx, ld, &state);
2092 asn1_push_tag(state->out, TLDAP_REQ_ADD);
2093 asn1_write_OctetString(state->out, dn, strlen(dn));
2094 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2096 for (i=0; i<num_attributes; i++) {
2097 struct tldap_mod *attrib = &attributes[i];
2098 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2099 asn1_write_OctetString(state->out, attrib->attribute,
2100 strlen(attrib->attribute));
2101 asn1_push_tag(state->out, ASN1_SET);
2102 for (j=0; j<attrib->num_values; j++) {
2103 asn1_write_OctetString(state->out,
2104 attrib->values[j].data,
2105 attrib->values[j].length);
2107 asn1_pop_tag(state->out);
2108 asn1_pop_tag(state->out);
2111 asn1_pop_tag(state->out);
2112 asn1_pop_tag(state->out);
2114 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2115 sctrls, num_sctrls);
2116 if (tevent_req_nomem(subreq, req)) {
2117 return tevent_req_post(req, ev);
2119 tevent_req_set_callback(subreq, tldap_add_done, req);
2123 static void tldap_add_done(struct tevent_req *subreq)
2125 tldap_simple_done(subreq, TLDAP_RES_ADD);
2128 int tldap_add_recv(struct tevent_req *req)
2130 return tldap_simple_recv(req);
2133 int tldap_add(struct tldap_context *ld, const char *dn,
2134 struct tldap_mod *attributes, int num_attributes,
2135 struct tldap_control *sctrls, int num_sctrls,
2136 struct tldap_control *cctrls, int num_cctrls)
2138 TALLOC_CTX *frame = talloc_stackframe();
2139 struct tevent_context *ev;
2140 struct tevent_req *req;
2143 ev = event_context_init(frame);
2145 result = TLDAP_NO_MEMORY;
2149 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2150 sctrls, num_sctrls, cctrls, num_cctrls);
2152 result = TLDAP_NO_MEMORY;
2156 if (!tevent_req_poll(req, ev)) {
2157 result = TLDAP_OPERATIONS_ERROR;
2161 result = tldap_add_recv(req);
2162 tldap_save_msg(ld, req);
2168 static void tldap_modify_done(struct tevent_req *subreq);
2170 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2171 struct tevent_context *ev,
2172 struct tldap_context *ld,
2174 struct tldap_mod *mods, int num_mods,
2175 struct tldap_control *sctrls,
2177 struct tldap_control *cctrls,
2180 struct tevent_req *req, *subreq;
2181 struct tldap_req_state *state;
2184 req = tldap_req_create(mem_ctx, ld, &state);
2189 asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
2190 asn1_write_OctetString(state->out, dn, strlen(dn));
2191 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2193 for (i=0; i<num_mods; i++) {
2194 struct tldap_mod *mod = &mods[i];
2195 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2196 asn1_write_enumerated(state->out, mod->mod_op),
2197 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2198 asn1_write_OctetString(state->out, mod->attribute,
2199 strlen(mod->attribute));
2200 asn1_push_tag(state->out, ASN1_SET);
2201 for (j=0; j<mod->num_values; j++) {
2202 asn1_write_OctetString(state->out,
2203 mod->values[j].data,
2204 mod->values[j].length);
2206 asn1_pop_tag(state->out);
2207 asn1_pop_tag(state->out);
2208 asn1_pop_tag(state->out);
2211 asn1_pop_tag(state->out);
2212 asn1_pop_tag(state->out);
2214 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2215 sctrls, num_sctrls);
2216 if (tevent_req_nomem(subreq, req)) {
2217 return tevent_req_post(req, ev);
2219 tevent_req_set_callback(subreq, tldap_modify_done, req);
2223 static void tldap_modify_done(struct tevent_req *subreq)
2225 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2228 int tldap_modify_recv(struct tevent_req *req)
2230 return tldap_simple_recv(req);
2233 int tldap_modify(struct tldap_context *ld, const char *dn,
2234 struct tldap_mod *mods, int num_mods,
2235 struct tldap_control *sctrls, int num_sctrls,
2236 struct tldap_control *cctrls, int num_cctrls)
2238 TALLOC_CTX *frame = talloc_stackframe();
2239 struct tevent_context *ev;
2240 struct tevent_req *req;
2243 ev = event_context_init(frame);
2245 result = TLDAP_NO_MEMORY;
2249 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2250 sctrls, num_sctrls, cctrls, num_cctrls);
2252 result = TLDAP_NO_MEMORY;
2256 if (!tevent_req_poll(req, ev)) {
2257 result = TLDAP_OPERATIONS_ERROR;
2261 result = tldap_modify_recv(req);
2262 tldap_save_msg(ld, req);
2268 static void tldap_delete_done(struct tevent_req *subreq);
2270 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2271 struct tevent_context *ev,
2272 struct tldap_context *ld,
2274 struct tldap_control *sctrls,
2276 struct tldap_control *cctrls,
2279 struct tevent_req *req, *subreq;
2280 struct tldap_req_state *state;
2282 req = tldap_req_create(mem_ctx, ld, &state);
2287 asn1_push_tag(state->out, TLDAP_REQ_DELETE);
2288 asn1_write(state->out, dn, strlen(dn));
2289 asn1_pop_tag(state->out);
2291 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2292 sctrls, num_sctrls);
2293 if (tevent_req_nomem(subreq, req)) {
2294 return tevent_req_post(req, ev);
2296 tevent_req_set_callback(subreq, tldap_delete_done, req);
2300 static void tldap_delete_done(struct tevent_req *subreq)
2302 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2305 int tldap_delete_recv(struct tevent_req *req)
2307 return tldap_simple_recv(req);
2310 int tldap_delete(struct tldap_context *ld, const char *dn,
2311 struct tldap_control *sctrls, int num_sctrls,
2312 struct tldap_control *cctrls, int num_cctrls)
2314 TALLOC_CTX *frame = talloc_stackframe();
2315 struct tevent_context *ev;
2316 struct tevent_req *req;
2319 ev = event_context_init(frame);
2321 result = TLDAP_NO_MEMORY;
2325 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2326 cctrls, num_cctrls);
2328 result = TLDAP_NO_MEMORY;
2332 if (!tevent_req_poll(req, ev)) {
2333 result = TLDAP_OPERATIONS_ERROR;
2337 result = tldap_delete_recv(req);
2338 tldap_save_msg(ld, req);
2344 int tldap_msg_id(const struct tldap_message *msg)
2349 int tldap_msg_type(const struct tldap_message *msg)
2354 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2359 return msg->res_matcheddn;
2362 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2367 return msg->res_diagnosticmessage;
2370 const char *tldap_msg_referral(struct tldap_message *msg)
2375 return msg->res_referral;
2378 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2379 struct tldap_control **sctrls)
2386 *sctrls = msg->res_sctrls;
2387 *num_sctrls = talloc_array_length(msg->res_sctrls);
2390 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2392 return ld->last_msg;
2395 const char *tldap_err2string(int rc)
2397 const char *res = NULL;
2400 * This would normally be a table, but the error codes are not fully
2401 * sequential. Let the compiler figure out the optimum implementation
2407 res = "TLDAP_SUCCESS";
2409 case TLDAP_OPERATIONS_ERROR:
2410 res = "TLDAP_OPERATIONS_ERROR";
2412 case TLDAP_PROTOCOL_ERROR:
2413 res = "TLDAP_PROTOCOL_ERROR";
2415 case TLDAP_TIMELIMIT_EXCEEDED:
2416 res = "TLDAP_TIMELIMIT_EXCEEDED";
2418 case TLDAP_SIZELIMIT_EXCEEDED:
2419 res = "TLDAP_SIZELIMIT_EXCEEDED";
2421 case TLDAP_COMPARE_FALSE:
2422 res = "TLDAP_COMPARE_FALSE";
2424 case TLDAP_COMPARE_TRUE:
2425 res = "TLDAP_COMPARE_TRUE";
2427 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2428 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2430 case TLDAP_STRONG_AUTH_REQUIRED:
2431 res = "TLDAP_STRONG_AUTH_REQUIRED";
2433 case TLDAP_REFERRAL:
2434 res = "TLDAP_REFERRAL";
2436 case TLDAP_ADMINLIMIT_EXCEEDED:
2437 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2439 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2440 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2442 case TLDAP_CONFIDENTIALITY_REQUIRED:
2443 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2445 case TLDAP_SASL_BIND_IN_PROGRESS:
2446 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2448 case TLDAP_NO_SUCH_ATTRIBUTE:
2449 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2451 case TLDAP_UNDEFINED_TYPE:
2452 res = "TLDAP_UNDEFINED_TYPE";
2454 case TLDAP_INAPPROPRIATE_MATCHING:
2455 res = "TLDAP_INAPPROPRIATE_MATCHING";
2457 case TLDAP_CONSTRAINT_VIOLATION:
2458 res = "TLDAP_CONSTRAINT_VIOLATION";
2460 case TLDAP_TYPE_OR_VALUE_EXISTS:
2461 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2463 case TLDAP_INVALID_SYNTAX:
2464 res = "TLDAP_INVALID_SYNTAX";
2466 case TLDAP_NO_SUCH_OBJECT:
2467 res = "TLDAP_NO_SUCH_OBJECT";
2469 case TLDAP_ALIAS_PROBLEM:
2470 res = "TLDAP_ALIAS_PROBLEM";
2472 case TLDAP_INVALID_DN_SYNTAX:
2473 res = "TLDAP_INVALID_DN_SYNTAX";
2476 res = "TLDAP_IS_LEAF";
2478 case TLDAP_ALIAS_DEREF_PROBLEM:
2479 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2481 case TLDAP_INAPPROPRIATE_AUTH:
2482 res = "TLDAP_INAPPROPRIATE_AUTH";
2484 case TLDAP_INVALID_CREDENTIALS:
2485 res = "TLDAP_INVALID_CREDENTIALS";
2487 case TLDAP_INSUFFICIENT_ACCESS:
2488 res = "TLDAP_INSUFFICIENT_ACCESS";
2493 case TLDAP_UNAVAILABLE:
2494 res = "TLDAP_UNAVAILABLE";
2496 case TLDAP_UNWILLING_TO_PERFORM:
2497 res = "TLDAP_UNWILLING_TO_PERFORM";
2499 case TLDAP_LOOP_DETECT:
2500 res = "TLDAP_LOOP_DETECT";
2502 case TLDAP_NAMING_VIOLATION:
2503 res = "TLDAP_NAMING_VIOLATION";
2505 case TLDAP_OBJECT_CLASS_VIOLATION:
2506 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2508 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2509 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2511 case TLDAP_NOT_ALLOWED_ON_RDN:
2512 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2514 case TLDAP_ALREADY_EXISTS:
2515 res = "TLDAP_ALREADY_EXISTS";
2517 case TLDAP_NO_OBJECT_CLASS_MODS:
2518 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2520 case TLDAP_RESULTS_TOO_LARGE:
2521 res = "TLDAP_RESULTS_TOO_LARGE";
2523 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2524 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2527 res = "TLDAP_OTHER";
2529 case TLDAP_SERVER_DOWN:
2530 res = "TLDAP_SERVER_DOWN";
2532 case TLDAP_LOCAL_ERROR:
2533 res = "TLDAP_LOCAL_ERROR";
2535 case TLDAP_ENCODING_ERROR:
2536 res = "TLDAP_ENCODING_ERROR";
2538 case TLDAP_DECODING_ERROR:
2539 res = "TLDAP_DECODING_ERROR";
2542 res = "TLDAP_TIMEOUT";
2544 case TLDAP_AUTH_UNKNOWN:
2545 res = "TLDAP_AUTH_UNKNOWN";
2547 case TLDAP_FILTER_ERROR:
2548 res = "TLDAP_FILTER_ERROR";
2550 case TLDAP_USER_CANCELLED:
2551 res = "TLDAP_USER_CANCELLED";
2553 case TLDAP_PARAM_ERROR:
2554 res = "TLDAP_PARAM_ERROR";
2556 case TLDAP_NO_MEMORY:
2557 res = "TLDAP_NO_MEMORY";
2559 case TLDAP_CONNECT_ERROR:
2560 res = "TLDAP_CONNECT_ERROR";
2562 case TLDAP_NOT_SUPPORTED:
2563 res = "TLDAP_NOT_SUPPORTED";
2565 case TLDAP_CONTROL_NOT_FOUND:
2566 res = "TLDAP_CONTROL_NOT_FOUND";
2568 case TLDAP_NO_RESULTS_RETURNED:
2569 res = "TLDAP_NO_RESULTS_RETURNED";
2571 case TLDAP_MORE_RESULTS_TO_RETURN:
2572 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2574 case TLDAP_CLIENT_LOOP:
2575 res = "TLDAP_CLIENT_LOOP";
2577 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2578 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2581 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2586 res = "Unknown LDAP Error";