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"
24 static int tldap_simple_recv(struct tevent_req *req);
26 bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
28 enum tevent_req_state state;
31 if (!tevent_req_is_error(req, &state, &err)) {
35 case TEVENT_REQ_TIMED_OUT:
36 *perr = TLDAP_TIMEOUT;
38 case TEVENT_REQ_NO_MEMORY:
39 *perr = TLDAP_NO_MEMORY;
41 case TEVENT_REQ_USER_ERROR:
45 *perr = TLDAP_OPERATIONS_ERROR;
51 struct tldap_ctx_attribute {
56 struct tldap_context {
61 struct tstream_context *conn;
64 struct tevent_queue *outgoing;
65 struct tevent_req **pending;
67 /* For the sync wrappers we need something like get_last_error... */
68 struct tldap_message *last_msg;
71 void (*log_fn)(void *context, enum tldap_debug_level level,
72 const char *fmt, va_list ap);
75 struct tldap_ctx_attribute *ctx_attrs;
78 struct tldap_message {
79 struct asn1_data *data;
86 struct tldap_attribute *attribs;
88 /* Error data sent by the server */
91 char *res_diagnosticmessage;
93 struct tldap_control *res_sctrls;
95 /* Controls sent by the server */
96 struct tldap_control *ctrls;
99 void tldap_set_debug(struct tldap_context *ld,
100 void (*log_fn)(void *log_private,
101 enum tldap_debug_level level,
103 va_list ap) PRINTF_ATTRIBUTE(3,0),
107 ld->log_private = log_private;
110 static void tldap_debug(struct tldap_context *ld,
111 enum tldap_debug_level level,
112 const char *fmt, ...)
118 if (ld->log_fn == NULL) {
122 ld->log_fn(ld->log_private, level, fmt, ap);
126 static int tldap_next_msgid(struct tldap_context *ld)
130 result = ld->msgid++;
131 if (ld->msgid == 2147483647) {
137 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
139 struct tldap_context *ctx;
142 ctx = talloc_zero(mem_ctx, struct tldap_context);
146 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
153 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
154 if (ctx->outgoing == NULL) {
161 bool tldap_connection_ok(struct tldap_context *ld)
166 return !ld->server_down;
169 static struct tldap_ctx_attribute *tldap_context_findattr(
170 struct tldap_context *ld, const char *name)
174 num_attrs = talloc_array_length(ld->ctx_attrs);
176 for (i=0; i<num_attrs; i++) {
177 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
178 return &ld->ctx_attrs[i];
184 bool tldap_context_setattr(struct tldap_context *ld,
185 const char *name, const void *_pptr)
187 struct tldap_ctx_attribute *tmp, *attr;
190 void **pptr = (void **)_pptr;
192 attr = tldap_context_findattr(ld, name);
195 * We don't actually delete attrs, we don't expect tons of
196 * attributes being shuffled around.
198 TALLOC_FREE(attr->ptr);
200 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
206 tmpname = talloc_strdup(ld, name);
207 if (tmpname == NULL) {
211 num_attrs = talloc_array_length(ld->ctx_attrs);
213 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
216 TALLOC_FREE(tmpname);
219 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
221 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
223 tmp[num_attrs].ptr = NULL;
230 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
232 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
240 struct read_ldap_state {
245 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
246 static void read_ldap_done(struct tevent_req *subreq);
248 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
249 struct tevent_context *ev,
250 struct tstream_context *conn)
252 struct tevent_req *req, *subreq;
253 struct read_ldap_state *state;
255 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
261 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
263 if (tevent_req_nomem(subreq, req)) {
264 return tevent_req_post(req, ev);
266 tevent_req_set_callback(subreq, read_ldap_done, req);
270 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
272 struct read_ldap_state *state = talloc_get_type_abort(
273 private_data, struct read_ldap_state);
278 /* We've been here, we're done */
283 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
285 if (buf[0] != 0x30) {
290 if ((len & 0x80) == 0) {
295 lensize = (len & 0x7f);
299 /* Please get us the full length */
302 if (buflen > 2 + lensize) {
306 if (buflen != 2 + lensize) {
310 for (i=0; i<lensize; i++) {
311 len = (len << 8) | buf[2+i];
316 static void read_ldap_done(struct tevent_req *subreq)
318 struct tevent_req *req = tevent_req_callback_data(
319 subreq, struct tevent_req);
320 struct read_ldap_state *state = tevent_req_data(
321 req, struct read_ldap_state);
325 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
328 tevent_req_error(req, err);
331 tevent_req_done(req);
334 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
335 uint8_t **pbuf, int *perrno)
337 struct read_ldap_state *state = tevent_req_data(
338 req, struct read_ldap_state);
340 if (tevent_req_is_unix_error(req, perrno)) {
343 *pbuf = talloc_move(mem_ctx, &state->buf);
344 return talloc_get_size(*pbuf);
347 struct tldap_msg_state {
348 struct tldap_context *ld;
349 struct tevent_context *ev;
353 struct asn1_data *data;
357 static void tldap_push_controls(struct asn1_data *data,
358 struct tldap_control *sctrls,
363 if ((sctrls == NULL) || (num_sctrls == 0)) {
367 asn1_push_tag(data, ASN1_CONTEXT(0));
369 for (i=0; i<num_sctrls; i++) {
370 struct tldap_control *c = &sctrls[i];
371 asn1_push_tag(data, ASN1_SEQUENCE(0));
372 asn1_write_OctetString(data, c->oid, strlen(c->oid));
374 asn1_write_BOOLEAN(data, true);
376 if (c->value.data != NULL) {
377 asn1_write_OctetString(data, c->value.data,
380 asn1_pop_tag(data); /* ASN1_SEQUENCE(0) */
383 asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
386 static void tldap_msg_sent(struct tevent_req *subreq);
387 static void tldap_msg_received(struct tevent_req *subreq);
389 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
390 struct tevent_context *ev,
391 struct tldap_context *ld,
392 int id, struct asn1_data *data,
393 struct tldap_control *sctrls,
396 struct tevent_req *req, *subreq;
397 struct tldap_msg_state *state;
400 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
403 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
411 if (state->ld->server_down) {
412 tevent_req_error(req, TLDAP_SERVER_DOWN);
413 return tevent_req_post(req, ev);
416 tldap_push_controls(data, sctrls, num_sctrls);
420 if (!asn1_blob(data, &blob)) {
421 tevent_req_error(req, TLDAP_ENCODING_ERROR);
422 return tevent_req_post(req, ev);
425 state->iov.iov_base = (void *)blob.data;
426 state->iov.iov_len = blob.length;
428 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
430 if (tevent_req_nomem(subreq, req)) {
431 return tevent_req_post(req, ev);
433 tevent_req_set_callback(subreq, tldap_msg_sent, req);
437 static void tldap_msg_unset_pending(struct tevent_req *req)
439 struct tldap_msg_state *state = tevent_req_data(
440 req, struct tldap_msg_state);
441 struct tldap_context *ld = state->ld;
442 int num_pending = talloc_array_length(ld->pending);
445 if (num_pending == 1) {
446 TALLOC_FREE(ld->pending);
450 for (i=0; i<num_pending; i++) {
451 if (req == ld->pending[i]) {
455 if (i == num_pending) {
457 * Something's seriously broken. Just returning here is the
458 * right thing nevertheless, the point of this routine is to
459 * remove ourselves from cli->pending.
465 * Remove ourselves from the cli->pending array
467 if (num_pending > 1) {
468 ld->pending[i] = ld->pending[num_pending-1];
472 * No NULL check here, we're shrinking by sizeof(void *), and
473 * talloc_realloc just adjusts the size for this.
475 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
480 static int tldap_msg_destructor(struct tevent_req *req)
482 tldap_msg_unset_pending(req);
486 static bool tldap_msg_set_pending(struct tevent_req *req)
488 struct tldap_msg_state *state = tevent_req_data(
489 req, struct tldap_msg_state);
490 struct tldap_context *ld;
491 struct tevent_req **pending;
493 struct tevent_req *subreq;
496 num_pending = talloc_array_length(ld->pending);
498 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
500 if (pending == NULL) {
503 pending[num_pending] = req;
504 ld->pending = pending;
505 talloc_set_destructor(req, tldap_msg_destructor);
507 if (num_pending > 0) {
512 * We're the first one, add the read_ldap request that waits for the
513 * answer from the server
515 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
516 if (subreq == NULL) {
517 tldap_msg_unset_pending(req);
520 tevent_req_set_callback(subreq, tldap_msg_received, ld);
524 static void tldap_msg_sent(struct tevent_req *subreq)
526 struct tevent_req *req = tevent_req_callback_data(
527 subreq, struct tevent_req);
528 struct tldap_msg_state *state = tevent_req_data(
529 req, struct tldap_msg_state);
533 nwritten = tstream_writev_queue_recv(subreq, &err);
535 if (nwritten == -1) {
536 state->ld->server_down = true;
537 tevent_req_error(req, TLDAP_SERVER_DOWN);
541 if (!tldap_msg_set_pending(req)) {
542 tevent_req_nomem(NULL, req);
547 static int tldap_msg_msgid(struct tevent_req *req)
549 struct tldap_msg_state *state = tevent_req_data(
550 req, struct tldap_msg_state);
555 static void tldap_msg_received(struct tevent_req *subreq)
557 struct tldap_context *ld = tevent_req_callback_data(
558 subreq, struct tldap_context);
559 struct tevent_req *req;
560 struct tldap_msg_state *state;
561 struct asn1_data *data;
570 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
572 if (received == -1) {
573 status = TLDAP_SERVER_DOWN;
577 data = asn1_init(talloc_tos());
579 status = TLDAP_NO_MEMORY;
582 asn1_load_nocopy(data, inbuf, received);
585 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
586 ok &= asn1_read_Integer(data, &id);
587 ok &= asn1_peek_uint8(data, &type);
590 status = TLDAP_PROTOCOL_ERROR;
594 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
595 "type %d\n", id, (int)type);
597 num_pending = talloc_array_length(ld->pending);
599 for (i=0; i<num_pending; i++) {
600 if (id == tldap_msg_msgid(ld->pending[i])) {
604 if (i == num_pending) {
605 /* Dump unexpected reply */
606 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
607 "No request pending for msg %d\n", id);
613 req = ld->pending[i];
614 state = tevent_req_data(req, struct tldap_msg_state);
616 state->inbuf = talloc_move(state, &inbuf);
617 state->data = talloc_move(state, &data);
619 talloc_set_destructor(req, NULL);
620 tldap_msg_unset_pending(req);
621 num_pending = talloc_array_length(ld->pending);
623 tevent_req_done(req);
626 if (num_pending == 0) {
629 if (talloc_array_length(ld->pending) > num_pending) {
631 * The callback functions called from tevent_req_done() above
632 * have put something on the pending queue. We don't have to
633 * trigger the read_ldap_send(), tldap_msg_set_pending() has
634 * done it for us already.
639 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
640 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
641 if (subreq == NULL) {
642 status = TLDAP_NO_MEMORY;
645 tevent_req_set_callback(subreq, tldap_msg_received, ld);
649 while (talloc_array_length(ld->pending) > 0) {
650 req = ld->pending[0];
651 talloc_set_destructor(req, NULL);
652 tldap_msg_destructor(req);
653 tevent_req_error(req, status);
657 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
658 struct tldap_message **pmsg)
660 struct tldap_msg_state *state = tevent_req_data(
661 req, struct tldap_msg_state);
662 struct tldap_message *msg;
666 if (tevent_req_is_ldap_error(req, &err)) {
670 if (!asn1_peek_uint8(state->data, &msgtype)) {
671 return TLDAP_PROTOCOL_ERROR;
675 return TLDAP_SUCCESS;
678 msg = talloc_zero(mem_ctx, struct tldap_message);
680 return TLDAP_NO_MEMORY;
684 msg->inbuf = talloc_move(msg, &state->inbuf);
685 msg->data = talloc_move(msg, &state->data);
689 return TLDAP_SUCCESS;
692 struct tldap_req_state {
694 struct asn1_data *out;
695 struct tldap_message *result;
698 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
699 struct tldap_context *ld,
700 struct tldap_req_state **pstate)
702 struct tevent_req *req;
703 struct tldap_req_state *state;
705 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
710 state->out = asn1_init(state);
711 if (state->out == NULL) {
715 state->result = NULL;
716 state->id = tldap_next_msgid(ld);
718 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
719 asn1_write_Integer(state->out, state->id);
725 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
727 struct tldap_req_state *state = tevent_req_data(
728 req, struct tldap_req_state);
730 TALLOC_FREE(ld->last_msg);
731 ld->last_msg = talloc_move(ld, &state->result);
734 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
736 char *result = talloc_array(mem_ctx, char, blob.length+1);
738 if (result == NULL) {
742 memcpy(result, blob.data, blob.length);
743 result[blob.length] = '\0';
747 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
748 struct asn1_data *data,
753 if (!asn1_read_OctetString(data, mem_ctx, &string))
756 result = blob2string_talloc(mem_ctx, string);
758 data_blob_free(&string);
760 if (result == NULL) {
767 static bool tldap_decode_controls(struct tldap_req_state *state);
769 static bool tldap_decode_response(struct tldap_req_state *state)
771 struct asn1_data *data = state->result->data;
772 struct tldap_message *msg = state->result;
775 ok &= asn1_read_enumerated(data, &msg->lderr);
776 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
777 ok &= asn1_read_OctetString_talloc(msg, data,
778 &msg->res_diagnosticmessage);
779 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
780 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
781 ok &= asn1_read_OctetString_talloc(msg, data,
783 ok &= asn1_end_tag(data);
785 msg->res_referral = NULL;
791 static void tldap_sasl_bind_done(struct tevent_req *subreq);
793 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
794 struct tevent_context *ev,
795 struct tldap_context *ld,
797 const char *mechanism,
799 struct tldap_control *sctrls,
801 struct tldap_control *cctrls,
804 struct tevent_req *req, *subreq;
805 struct tldap_req_state *state;
807 req = tldap_req_create(mem_ctx, ld, &state);
816 asn1_push_tag(state->out, TLDAP_REQ_BIND);
817 asn1_write_Integer(state->out, ld->ld_version);
818 asn1_write_OctetString(state->out, dn, (dn != NULL) ? strlen(dn) : 0);
820 if (mechanism == NULL) {
821 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
822 asn1_write(state->out, creds->data, creds->length);
823 asn1_pop_tag(state->out);
825 asn1_push_tag(state->out, ASN1_CONTEXT(3));
826 asn1_write_OctetString(state->out, mechanism,
828 if ((creds != NULL) && (creds->data != NULL)) {
829 asn1_write_OctetString(state->out, creds->data,
832 asn1_pop_tag(state->out);
835 if (!asn1_pop_tag(state->out)) {
836 tevent_req_error(req, TLDAP_ENCODING_ERROR);
837 return tevent_req_post(req, ev);
840 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
842 if (tevent_req_nomem(subreq, req)) {
843 return tevent_req_post(req, ev);
845 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
849 static void tldap_sasl_bind_done(struct tevent_req *subreq)
851 struct tevent_req *req = tevent_req_callback_data(
852 subreq, struct tevent_req);
853 struct tldap_req_state *state = tevent_req_data(
854 req, struct tldap_req_state);
857 err = tldap_msg_recv(subreq, state, &state->result);
859 if (err != TLDAP_SUCCESS) {
860 tevent_req_error(req, err);
863 if (state->result->type != TLDAP_RES_BIND) {
864 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
867 if (!asn1_start_tag(state->result->data, state->result->type) ||
868 !tldap_decode_response(state) ||
869 !asn1_end_tag(state->result->data)) {
870 tevent_req_error(req, TLDAP_DECODING_ERROR);
874 * TODO: pull the reply blob
876 if (state->result->lderr != TLDAP_SUCCESS) {
877 tevent_req_error(req, state->result->lderr);
880 tevent_req_done(req);
883 int tldap_sasl_bind_recv(struct tevent_req *req)
885 return tldap_simple_recv(req);
888 int tldap_sasl_bind(struct tldap_context *ld,
890 const char *mechanism,
892 struct tldap_control *sctrls,
894 struct tldap_control *cctrls,
897 TALLOC_CTX *frame = talloc_stackframe();
898 struct tevent_context *ev;
899 struct tevent_req *req;
902 ev = event_context_init(frame);
904 result = TLDAP_NO_MEMORY;
908 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
909 sctrls, num_sctrls, cctrls, num_cctrls);
911 result = TLDAP_NO_MEMORY;
915 if (!tevent_req_poll(req, ev)) {
916 result = TLDAP_OPERATIONS_ERROR;
920 result = tldap_sasl_bind_recv(req);
921 tldap_save_msg(ld, req);
927 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
928 struct tevent_context *ev,
929 struct tldap_context *ld,
935 if (passwd != NULL) {
936 cred.data = (uint8_t *)passwd;
937 cred.length = strlen(passwd);
939 cred.data = (uint8_t *)"";
942 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
946 int tldap_simple_bind_recv(struct tevent_req *req)
948 return tldap_sasl_bind_recv(req);
951 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
956 if (passwd != NULL) {
957 cred.data = (uint8_t *)passwd;
958 cred.length = strlen(passwd);
960 cred.data = (uint8_t *)"";
963 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
966 /*****************************************************************************/
968 /* can't use isalpha() as only a strict set is valid for LDAP */
970 static bool tldap_is_alpha(char c)
972 return (((c >= 'a') && (c <= 'z')) || \
973 ((c >= 'A') && (c <= 'Z')));
976 static bool tldap_is_adh(char c)
978 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
981 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
982 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
983 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
984 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
985 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
986 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
987 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
988 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
989 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
990 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
992 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
993 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
994 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
997 /* oid's should be numerical only in theory,
998 * but apparently some broken servers may have alphanum aliases instead.
999 * Do like openldap libraries and allow alphanum aliases for oids, but
1000 * do not allow Tagging options in that case.
1002 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1004 bool is_oid = false;
1008 /* first char has stricter rules */
1011 } else if (!tldap_is_alpha(*s)) {
1012 /* bad first char */
1016 for (i = 1; i < len; i++) {
1019 if (isdigit(s[i])) {
1032 if (tldap_is_adh(s[i])) {
1039 /* no tagging options */
1046 if ((i + 1) == len) {
1064 /* this function copies the value until the closing parenthesis is found. */
1065 static char *tldap_get_val(TALLOC_CTX *memctx,
1066 const char *value, const char **_s)
1068 const char *s = value;
1070 /* find terminator */
1073 if (s && (*(s - 1) == '\\')) {
1078 if (!s || !(*s == ')')) {
1079 /* malformed filter */
1085 return talloc_strndup(memctx, value, s - value);
1088 static int tldap_hex2char(const char *x)
1090 if (isxdigit(x[0]) && isxdigit(x[1])) {
1091 const char h1 = x[0], h2 = x[1];
1094 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1095 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1096 else if (h1 >= '0') c = h1 - (int)'0';
1098 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1099 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1100 else if (h2 >= '0') c += h2 - (int)'0';
1108 static bool tldap_find_first_star(const char *val, const char **star)
1112 for (s = val; *s; s++) {
1115 if (isxdigit(s[1]) && isxdigit(s[2])) {
1119 /* not hex based escape, check older syntax */
1128 /* invalid escape sequence */
1133 /* end of val, nothing found */
1143 /* string ended without closing parenthesis, filter is malformed */
1147 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1151 for (i = 0,p = 0; i < *val_len; i++) {
1157 /* these must be escaped */
1161 if (!value[i + 1]) {
1167 c = tldap_hex2char(&value[i]);
1168 if (c >= 0 && c < 256) {
1180 value[p] = value[i];
1189 value[p] = value[i];
1198 static bool tldap_push_filter_basic(struct tldap_context *ld,
1199 struct asn1_data *data,
1201 static bool tldap_push_filter_substring(struct tldap_context *ld,
1202 struct asn1_data *data,
1205 static bool tldap_push_filter_int(struct tldap_context *ld,
1206 struct asn1_data *data,
1209 const char *s = *_s;
1213 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1214 "Incomplete or malformed filter\n");
1219 /* we are right after a parenthesis,
1220 * find out what op we have at hand */
1223 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1224 asn1_push_tag(data, TLDAP_FILTER_AND);
1229 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1230 asn1_push_tag(data, TLDAP_FILTER_OR);
1235 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1236 asn1_push_tag(data, TLDAP_FILTER_NOT);
1238 ret = tldap_push_filter_int(ld, data, &s);
1247 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1248 "Invalid parenthesis '%c'\n", *s);
1252 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1253 "Invalid filter termination\n");
1257 ret = tldap_push_filter_basic(ld, data, &s);
1264 /* only and/or filters get here.
1265 * go through the list of filters */
1268 /* RFC 4526: empty and/or */
1274 ret = tldap_push_filter_int(ld, data, &s);
1280 /* end of list, return */
1288 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1289 "Incomplete or malformed filter\n");
1294 if (data->has_error) {
1303 static bool tldap_push_filter_basic(struct tldap_context *ld,
1304 struct asn1_data *data,
1307 TALLOC_CTX *tmpctx = talloc_tos();
1308 const char *s = *_s;
1316 size_t type_len = 0;
1319 bool write_octect = true;
1322 eq = strchr(s, '=');
1324 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1325 "Invalid filter, missing equal sign\n");
1334 asn1_push_tag(data, TLDAP_FILTER_LE);
1338 asn1_push_tag(data, TLDAP_FILTER_GE);
1342 asn1_push_tag(data, TLDAP_FILTER_APX);
1346 asn1_push_tag(data, TLDAP_FILTER_EXT);
1347 write_octect = false;
1353 if (*s == ':') { /* [:dn]:rule:= value */
1355 /* malformed filter */
1359 } else { /* type[:dn][:rule]:= value */
1361 dn = strchr(s, ':');
1362 type_len = dn - type;
1363 if (dn == e) { /* type:= value */
1370 rule = strchr(dn, ':');
1371 if ((rule == dn + 1) || rule + 1 == e) {
1372 /* malformed filter, contains "::" */
1376 if (StrnCaseCmp(dn, "dn:", 3) != 0) {
1381 /* malformed filter. With two
1382 * optionals, the first must be "dn"
1395 if (!type && !dn && !rule) {
1396 /* malformed filter, there must be at least one */
1401 MatchingRuleAssertion ::= SEQUENCE {
1402 matchingRule [1] MatchingRuleID OPTIONAL,
1403 type [2] AttributeDescription OPTIONAL,
1404 matchValue [3] AssertionValue,
1405 dnAttributes [4] BOOLEAN DEFAULT FALSE
1409 /* check and add rule */
1411 ret = tldap_is_attrdesc(rule, e - rule, true);
1415 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1416 asn1_write(data, rule, e - rule);
1420 /* check and add type */
1422 ret = tldap_is_attrdesc(type, type_len, false);
1426 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1427 asn1_write(data, type, type_len);
1431 uval = tldap_get_val(tmpctx, val, _s);
1435 uval_len = *_s - val;
1436 ret = tldap_unescape_inplace(uval, &uval_len);
1441 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1442 asn1_write(data, uval, uval_len);
1445 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1446 asn1_write_uint8(data, dn?1:0);
1453 ret = tldap_is_attrdesc(s, e - s, false);
1458 if (strncmp(val, "*)", 2) == 0) {
1460 asn1_push_tag(data, TLDAP_FILTER_PRES);
1461 asn1_write(data, s, e - s);
1463 write_octect = false;
1467 ret = tldap_find_first_star(val, &star);
1473 asn1_push_tag(data, TLDAP_FILTER_SUB);
1474 asn1_write_OctetString(data, s, e - s);
1475 ret = tldap_push_filter_substring(ld, data, val, &s);
1480 write_octect = false;
1484 /* if nothing else, then it is just equality */
1485 asn1_push_tag(data, TLDAP_FILTER_EQ);
1486 write_octect = true;
1491 uval = tldap_get_val(tmpctx, val, _s);
1495 uval_len = *_s - val;
1496 ret = tldap_unescape_inplace(uval, &uval_len);
1501 asn1_write_OctetString(data, s, e - s);
1502 asn1_write_OctetString(data, uval, uval_len);
1505 if (data->has_error) {
1512 static bool tldap_push_filter_substring(struct tldap_context *ld,
1513 struct asn1_data *data,
1517 TALLOC_CTX *tmpctx = talloc_tos();
1518 bool initial = true;
1525 SubstringFilter ::= SEQUENCE {
1526 type AttributeDescription,
1527 -- at least one must be present
1528 substrings SEQUENCE OF CHOICE {
1529 initial [0] LDAPString,
1531 final [2] LDAPString } }
1533 asn1_push_tag(data, ASN1_SEQUENCE(0));
1536 ret = tldap_find_first_star(val, &star);
1540 chunk_len = star - val;
1544 if (!initial && chunk_len == 0) {
1545 /* found '**', which is illegal */
1561 if (initial && chunk_len == 0) {
1567 chunk = talloc_strndup(tmpctx, val, chunk_len);
1571 ret = tldap_unescape_inplace(chunk, &chunk_len);
1578 asn1_push_tag(data, TLDAP_SUB_INI);
1581 asn1_push_tag(data, TLDAP_SUB_ANY);
1585 asn1_push_tag(data, TLDAP_SUB_FIN);
1591 asn1_write(data, chunk, chunk_len);
1596 } while (*star == '*');
1600 /* end of sequence */
1605 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1606 * around parenthesis, we do not allow any spaces (except in values of
1607 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1608 * leading or trailing spaces where allowed.
1610 static bool tldap_push_filter(struct tldap_context *ld,
1611 struct asn1_data *data,
1614 const char *s = filter;
1617 ret = tldap_push_filter_int(ld, data, &s);
1619 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1620 "Incomplete or malformed filter\n");
1626 /*****************************************************************************/
1628 static void tldap_search_done(struct tevent_req *subreq);
1630 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1631 struct tevent_context *ev,
1632 struct tldap_context *ld,
1633 const char *base, int scope,
1638 struct tldap_control *sctrls,
1640 struct tldap_control *cctrls,
1646 struct tevent_req *req, *subreq;
1647 struct tldap_req_state *state;
1650 req = tldap_req_create(mem_ctx, ld, &state);
1655 asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1656 asn1_write_OctetString(state->out, base, strlen(base));
1657 asn1_write_enumerated(state->out, scope);
1658 asn1_write_enumerated(state->out, deref);
1659 asn1_write_Integer(state->out, sizelimit);
1660 asn1_write_Integer(state->out, timelimit);
1661 asn1_write_BOOLEAN(state->out, attrsonly);
1663 if (!tldap_push_filter(ld, state->out, filter)) {
1664 goto encoding_error;
1667 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1668 for (i=0; i<num_attrs; i++) {
1669 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1671 asn1_pop_tag(state->out);
1672 asn1_pop_tag(state->out);
1674 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1675 sctrls, num_sctrls);
1676 if (tevent_req_nomem(subreq, req)) {
1677 return tevent_req_post(req, ev);
1679 tevent_req_set_callback(subreq, tldap_search_done, req);
1683 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1684 return tevent_req_post(req, ev);
1687 static void tldap_search_done(struct tevent_req *subreq)
1689 struct tevent_req *req = tevent_req_callback_data(
1690 subreq, struct tevent_req);
1691 struct tldap_req_state *state = tevent_req_data(
1692 req, struct tldap_req_state);
1695 err = tldap_msg_recv(subreq, state, &state->result);
1696 if (err != TLDAP_SUCCESS) {
1697 tevent_req_error(req, err);
1700 switch (state->result->type) {
1701 case TLDAP_RES_SEARCH_ENTRY:
1702 case TLDAP_RES_SEARCH_REFERENCE:
1703 if (!tldap_msg_set_pending(subreq)) {
1704 tevent_req_nomem(NULL, req);
1707 tevent_req_notify_callback(req);
1709 case TLDAP_RES_SEARCH_RESULT:
1710 TALLOC_FREE(subreq);
1711 if (!asn1_start_tag(state->result->data,
1712 state->result->type) ||
1713 !tldap_decode_response(state) ||
1714 !asn1_end_tag(state->result->data) ||
1715 !tldap_decode_controls(state)) {
1716 tevent_req_error(req, TLDAP_DECODING_ERROR);
1719 tevent_req_done(req);
1722 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1727 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1728 struct tldap_message **pmsg)
1730 struct tldap_req_state *state = tevent_req_data(
1731 req, struct tldap_req_state);
1734 if (!tevent_req_is_in_progress(req)
1735 && tevent_req_is_ldap_error(req, &err)) {
1739 if (tevent_req_is_in_progress(req)) {
1740 switch (state->result->type) {
1741 case TLDAP_RES_SEARCH_ENTRY:
1742 case TLDAP_RES_SEARCH_REFERENCE:
1745 return TLDAP_OPERATIONS_ERROR;
1749 *pmsg = talloc_move(mem_ctx, &state->result);
1750 return TLDAP_SUCCESS;
1753 struct tldap_sync_search_state {
1754 TALLOC_CTX *mem_ctx;
1755 struct tldap_message **entries;
1756 struct tldap_message **refs;
1760 static void tldap_search_cb(struct tevent_req *req)
1762 struct tldap_sync_search_state *state =
1763 (struct tldap_sync_search_state *)
1764 tevent_req_callback_data_void(req);
1765 struct tldap_message *msg, **tmp;
1766 int rc, num_entries, num_refs;
1768 rc = tldap_search_recv(req, talloc_tos(), &msg);
1769 if (rc != TLDAP_SUCCESS) {
1774 switch (tldap_msg_type(msg)) {
1775 case TLDAP_RES_SEARCH_ENTRY:
1776 num_entries = talloc_array_length(state->entries);
1777 tmp = talloc_realloc(state->mem_ctx, state->entries,
1778 struct tldap_message *, num_entries + 1);
1780 state->rc = TLDAP_NO_MEMORY;
1783 state->entries = tmp;
1784 state->entries[num_entries] = talloc_move(state->entries,
1787 case TLDAP_RES_SEARCH_REFERENCE:
1788 num_refs = talloc_array_length(state->refs);
1789 tmp = talloc_realloc(state->mem_ctx, state->refs,
1790 struct tldap_message *, num_refs + 1);
1792 state->rc = TLDAP_NO_MEMORY;
1796 state->refs[num_refs] = talloc_move(state->refs, &msg);
1798 case TLDAP_RES_SEARCH_RESULT:
1799 state->rc = TLDAP_SUCCESS;
1802 state->rc = TLDAP_PROTOCOL_ERROR;
1807 int tldap_search(struct tldap_context *ld,
1808 const char *base, int scope, const char *filter,
1809 const char **attrs, int num_attrs, int attrsonly,
1810 struct tldap_control *sctrls, int num_sctrls,
1811 struct tldap_control *cctrls, int num_cctrls,
1812 int timelimit, int sizelimit, int deref,
1813 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1814 struct tldap_message ***refs)
1816 TALLOC_CTX *frame = talloc_stackframe();
1817 struct tevent_context *ev;
1818 struct tevent_req *req;
1819 struct tldap_sync_search_state state;
1822 state.mem_ctx = mem_ctx;
1823 state.rc = TLDAP_SUCCESS;
1825 ev = event_context_init(frame);
1827 state.rc = TLDAP_NO_MEMORY;
1831 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1832 attrs, num_attrs, attrsonly,
1833 sctrls, num_sctrls, cctrls, num_cctrls,
1834 timelimit, sizelimit, deref);
1836 state.rc = TLDAP_NO_MEMORY;
1840 tevent_req_set_callback(req, tldap_search_cb, &state);
1842 if (!tevent_req_is_in_progress(req)) {
1843 /* an error happend before sending */
1844 if (tevent_req_is_ldap_error(req, &state.rc)) {
1849 while (tevent_req_is_in_progress(req)
1850 && (state.rc == TLDAP_SUCCESS)) {
1851 if (tevent_loop_once(ev) == -1) {
1852 return TLDAP_OPERATIONS_ERROR;
1856 if (state.rc != TLDAP_SUCCESS) {
1860 if (entries != NULL) {
1861 *entries = state.entries;
1863 TALLOC_FREE(state.entries);
1868 TALLOC_FREE(state.refs);
1870 tldap_save_msg(ld, req);
1876 static bool tldap_parse_search_entry(struct tldap_message *msg)
1878 int num_attribs = 0;
1880 asn1_start_tag(msg->data, msg->type);
1884 asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1885 if (msg->dn == NULL) {
1890 * Attributes: We overallocate msg->attribs by one, so that while
1891 * looping over the attributes we can directly parse into the last
1892 * array element. Same for the values in the inner loop.
1895 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1896 if (msg->attribs == NULL) {
1900 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1901 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1902 struct tldap_attribute *attrib;
1905 attrib = &msg->attribs[num_attribs];
1906 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1907 if (attrib->values == NULL) {
1910 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1911 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1913 asn1_start_tag(msg->data, ASN1_SET);
1915 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1916 asn1_read_OctetString(msg->data, msg,
1917 &attrib->values[num_values]);
1919 attrib->values = talloc_realloc(
1920 msg->attribs, attrib->values, DATA_BLOB,
1922 if (attrib->values == NULL) {
1927 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1928 DATA_BLOB, num_values);
1929 attrib->num_values = num_values;
1931 asn1_end_tag(msg->data); /* ASN1_SET */
1932 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1933 msg->attribs = talloc_realloc(
1934 msg, msg->attribs, struct tldap_attribute,
1936 if (msg->attribs == NULL) {
1941 msg->attribs = talloc_realloc(
1942 msg, msg->attribs, struct tldap_attribute, num_attribs);
1943 asn1_end_tag(msg->data);
1944 if (msg->data->has_error) {
1950 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1952 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1959 bool tldap_entry_attributes(struct tldap_message *msg,
1960 struct tldap_attribute **attributes,
1961 int *num_attributes)
1963 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1966 *attributes = msg->attribs;
1967 *num_attributes = talloc_array_length(msg->attribs);
1971 static bool tldap_decode_controls(struct tldap_req_state *state)
1973 struct tldap_message *msg = state->result;
1974 struct asn1_data *data = msg->data;
1975 struct tldap_control *sctrls = NULL;
1976 int num_controls = 0;
1978 msg->res_sctrls = NULL;
1980 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1984 asn1_start_tag(data, ASN1_CONTEXT(0));
1986 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
1987 struct tldap_control *c;
1990 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
1992 if (sctrls == NULL) {
1995 c = &sctrls[num_controls];
1997 asn1_start_tag(data, ASN1_SEQUENCE(0));
1998 asn1_read_OctetString_talloc(msg, data, &oid);
1999 if ((data->has_error) || (oid == NULL)) {
2003 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2004 asn1_read_BOOLEAN(data, &c->critical);
2006 c->critical = false;
2008 c->value = data_blob_null;
2009 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2010 !asn1_read_OctetString(data, msg, &c->value)) {
2013 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
2018 asn1_end_tag(data); /* ASN1_CONTEXT(0) */
2020 if (data->has_error) {
2021 TALLOC_FREE(sctrls);
2024 msg->res_sctrls = sctrls;
2028 static void tldap_simple_done(struct tevent_req *subreq, int type)
2030 struct tevent_req *req = tevent_req_callback_data(
2031 subreq, struct tevent_req);
2032 struct tldap_req_state *state = tevent_req_data(
2033 req, struct tldap_req_state);
2036 err = tldap_msg_recv(subreq, state, &state->result);
2037 TALLOC_FREE(subreq);
2038 if (err != TLDAP_SUCCESS) {
2039 tevent_req_error(req, err);
2042 if (state->result->type != type) {
2043 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2046 if (!asn1_start_tag(state->result->data, state->result->type) ||
2047 !tldap_decode_response(state) ||
2048 !asn1_end_tag(state->result->data) ||
2049 !tldap_decode_controls(state)) {
2050 tevent_req_error(req, TLDAP_DECODING_ERROR);
2053 if (state->result->lderr != TLDAP_SUCCESS) {
2054 tevent_req_error(req, state->result->lderr);
2057 tevent_req_done(req);
2060 static int tldap_simple_recv(struct tevent_req *req)
2063 if (tevent_req_is_ldap_error(req, &err)) {
2066 return TLDAP_SUCCESS;
2069 static void tldap_add_done(struct tevent_req *subreq);
2071 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2072 struct tevent_context *ev,
2073 struct tldap_context *ld,
2075 struct tldap_mod *attributes,
2077 struct tldap_control *sctrls,
2079 struct tldap_control *cctrls,
2082 struct tevent_req *req, *subreq;
2083 struct tldap_req_state *state;
2086 req = tldap_req_create(mem_ctx, ld, &state);
2091 asn1_push_tag(state->out, TLDAP_REQ_ADD);
2092 asn1_write_OctetString(state->out, dn, strlen(dn));
2093 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2095 for (i=0; i<num_attributes; i++) {
2096 struct tldap_mod *attrib = &attributes[i];
2097 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2098 asn1_write_OctetString(state->out, attrib->attribute,
2099 strlen(attrib->attribute));
2100 asn1_push_tag(state->out, ASN1_SET);
2101 for (j=0; j<attrib->num_values; j++) {
2102 asn1_write_OctetString(state->out,
2103 attrib->values[j].data,
2104 attrib->values[j].length);
2106 asn1_pop_tag(state->out);
2107 asn1_pop_tag(state->out);
2110 asn1_pop_tag(state->out);
2111 asn1_pop_tag(state->out);
2113 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2114 sctrls, num_sctrls);
2115 if (tevent_req_nomem(subreq, req)) {
2116 return tevent_req_post(req, ev);
2118 tevent_req_set_callback(subreq, tldap_add_done, req);
2122 static void tldap_add_done(struct tevent_req *subreq)
2124 tldap_simple_done(subreq, TLDAP_RES_ADD);
2127 int tldap_add_recv(struct tevent_req *req)
2129 return tldap_simple_recv(req);
2132 int tldap_add(struct tldap_context *ld, const char *dn,
2133 struct tldap_mod *attributes, int num_attributes,
2134 struct tldap_control *sctrls, int num_sctrls,
2135 struct tldap_control *cctrls, int num_cctrls)
2137 TALLOC_CTX *frame = talloc_stackframe();
2138 struct tevent_context *ev;
2139 struct tevent_req *req;
2142 ev = event_context_init(frame);
2144 result = TLDAP_NO_MEMORY;
2148 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2149 sctrls, num_sctrls, cctrls, num_cctrls);
2151 result = TLDAP_NO_MEMORY;
2155 if (!tevent_req_poll(req, ev)) {
2156 result = TLDAP_OPERATIONS_ERROR;
2160 result = tldap_add_recv(req);
2161 tldap_save_msg(ld, req);
2167 static void tldap_modify_done(struct tevent_req *subreq);
2169 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2170 struct tevent_context *ev,
2171 struct tldap_context *ld,
2173 struct tldap_mod *mods, int num_mods,
2174 struct tldap_control *sctrls,
2176 struct tldap_control *cctrls,
2179 struct tevent_req *req, *subreq;
2180 struct tldap_req_state *state;
2183 req = tldap_req_create(mem_ctx, ld, &state);
2188 asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
2189 asn1_write_OctetString(state->out, dn, strlen(dn));
2190 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2192 for (i=0; i<num_mods; i++) {
2193 struct tldap_mod *mod = &mods[i];
2194 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2195 asn1_write_enumerated(state->out, mod->mod_op),
2196 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2197 asn1_write_OctetString(state->out, mod->attribute,
2198 strlen(mod->attribute));
2199 asn1_push_tag(state->out, ASN1_SET);
2200 for (j=0; j<mod->num_values; j++) {
2201 asn1_write_OctetString(state->out,
2202 mod->values[j].data,
2203 mod->values[j].length);
2205 asn1_pop_tag(state->out);
2206 asn1_pop_tag(state->out);
2207 asn1_pop_tag(state->out);
2210 asn1_pop_tag(state->out);
2211 asn1_pop_tag(state->out);
2213 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2214 sctrls, num_sctrls);
2215 if (tevent_req_nomem(subreq, req)) {
2216 return tevent_req_post(req, ev);
2218 tevent_req_set_callback(subreq, tldap_modify_done, req);
2222 static void tldap_modify_done(struct tevent_req *subreq)
2224 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2227 int tldap_modify_recv(struct tevent_req *req)
2229 return tldap_simple_recv(req);
2232 int tldap_modify(struct tldap_context *ld, const char *dn,
2233 struct tldap_mod *mods, int num_mods,
2234 struct tldap_control *sctrls, int num_sctrls,
2235 struct tldap_control *cctrls, int num_cctrls)
2237 TALLOC_CTX *frame = talloc_stackframe();
2238 struct tevent_context *ev;
2239 struct tevent_req *req;
2242 ev = event_context_init(frame);
2244 result = TLDAP_NO_MEMORY;
2248 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2249 sctrls, num_sctrls, cctrls, num_cctrls);
2251 result = TLDAP_NO_MEMORY;
2255 if (!tevent_req_poll(req, ev)) {
2256 result = TLDAP_OPERATIONS_ERROR;
2260 result = tldap_modify_recv(req);
2261 tldap_save_msg(ld, req);
2267 static void tldap_delete_done(struct tevent_req *subreq);
2269 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2270 struct tevent_context *ev,
2271 struct tldap_context *ld,
2273 struct tldap_control *sctrls,
2275 struct tldap_control *cctrls,
2278 struct tevent_req *req, *subreq;
2279 struct tldap_req_state *state;
2281 req = tldap_req_create(mem_ctx, ld, &state);
2286 asn1_push_tag(state->out, TLDAP_REQ_DELETE);
2287 asn1_write(state->out, dn, strlen(dn));
2288 asn1_pop_tag(state->out);
2290 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2291 sctrls, num_sctrls);
2292 if (tevent_req_nomem(subreq, req)) {
2293 return tevent_req_post(req, ev);
2295 tevent_req_set_callback(subreq, tldap_delete_done, req);
2299 static void tldap_delete_done(struct tevent_req *subreq)
2301 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2304 int tldap_delete_recv(struct tevent_req *req)
2306 return tldap_simple_recv(req);
2309 int tldap_delete(struct tldap_context *ld, const char *dn,
2310 struct tldap_control *sctrls, int num_sctrls,
2311 struct tldap_control *cctrls, int num_cctrls)
2313 TALLOC_CTX *frame = talloc_stackframe();
2314 struct tevent_context *ev;
2315 struct tevent_req *req;
2318 ev = event_context_init(frame);
2320 result = TLDAP_NO_MEMORY;
2324 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2325 cctrls, num_cctrls);
2327 result = TLDAP_NO_MEMORY;
2331 if (!tevent_req_poll(req, ev)) {
2332 result = TLDAP_OPERATIONS_ERROR;
2336 result = tldap_delete_recv(req);
2337 tldap_save_msg(ld, req);
2343 int tldap_msg_id(const struct tldap_message *msg)
2348 int tldap_msg_type(const struct tldap_message *msg)
2353 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2358 return msg->res_matcheddn;
2361 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2366 return msg->res_diagnosticmessage;
2369 const char *tldap_msg_referral(struct tldap_message *msg)
2374 return msg->res_referral;
2377 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2378 struct tldap_control **sctrls)
2385 *sctrls = msg->res_sctrls;
2386 *num_sctrls = talloc_array_length(msg->res_sctrls);
2389 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2391 return ld->last_msg;
2394 const char *tldap_err2string(int rc)
2396 const char *res = NULL;
2399 * This would normally be a table, but the error codes are not fully
2400 * sequential. Let the compiler figure out the optimum implementation
2406 res = "TLDAP_SUCCESS";
2408 case TLDAP_OPERATIONS_ERROR:
2409 res = "TLDAP_OPERATIONS_ERROR";
2411 case TLDAP_PROTOCOL_ERROR:
2412 res = "TLDAP_PROTOCOL_ERROR";
2414 case TLDAP_TIMELIMIT_EXCEEDED:
2415 res = "TLDAP_TIMELIMIT_EXCEEDED";
2417 case TLDAP_SIZELIMIT_EXCEEDED:
2418 res = "TLDAP_SIZELIMIT_EXCEEDED";
2420 case TLDAP_COMPARE_FALSE:
2421 res = "TLDAP_COMPARE_FALSE";
2423 case TLDAP_COMPARE_TRUE:
2424 res = "TLDAP_COMPARE_TRUE";
2426 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2427 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2429 case TLDAP_STRONG_AUTH_REQUIRED:
2430 res = "TLDAP_STRONG_AUTH_REQUIRED";
2432 case TLDAP_REFERRAL:
2433 res = "TLDAP_REFERRAL";
2435 case TLDAP_ADMINLIMIT_EXCEEDED:
2436 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2438 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2439 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2441 case TLDAP_CONFIDENTIALITY_REQUIRED:
2442 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2444 case TLDAP_SASL_BIND_IN_PROGRESS:
2445 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2447 case TLDAP_NO_SUCH_ATTRIBUTE:
2448 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2450 case TLDAP_UNDEFINED_TYPE:
2451 res = "TLDAP_UNDEFINED_TYPE";
2453 case TLDAP_INAPPROPRIATE_MATCHING:
2454 res = "TLDAP_INAPPROPRIATE_MATCHING";
2456 case TLDAP_CONSTRAINT_VIOLATION:
2457 res = "TLDAP_CONSTRAINT_VIOLATION";
2459 case TLDAP_TYPE_OR_VALUE_EXISTS:
2460 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2462 case TLDAP_INVALID_SYNTAX:
2463 res = "TLDAP_INVALID_SYNTAX";
2465 case TLDAP_NO_SUCH_OBJECT:
2466 res = "TLDAP_NO_SUCH_OBJECT";
2468 case TLDAP_ALIAS_PROBLEM:
2469 res = "TLDAP_ALIAS_PROBLEM";
2471 case TLDAP_INVALID_DN_SYNTAX:
2472 res = "TLDAP_INVALID_DN_SYNTAX";
2475 res = "TLDAP_IS_LEAF";
2477 case TLDAP_ALIAS_DEREF_PROBLEM:
2478 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2480 case TLDAP_INAPPROPRIATE_AUTH:
2481 res = "TLDAP_INAPPROPRIATE_AUTH";
2483 case TLDAP_INVALID_CREDENTIALS:
2484 res = "TLDAP_INVALID_CREDENTIALS";
2486 case TLDAP_INSUFFICIENT_ACCESS:
2487 res = "TLDAP_INSUFFICIENT_ACCESS";
2492 case TLDAP_UNAVAILABLE:
2493 res = "TLDAP_UNAVAILABLE";
2495 case TLDAP_UNWILLING_TO_PERFORM:
2496 res = "TLDAP_UNWILLING_TO_PERFORM";
2498 case TLDAP_LOOP_DETECT:
2499 res = "TLDAP_LOOP_DETECT";
2501 case TLDAP_NAMING_VIOLATION:
2502 res = "TLDAP_NAMING_VIOLATION";
2504 case TLDAP_OBJECT_CLASS_VIOLATION:
2505 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2507 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2508 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2510 case TLDAP_NOT_ALLOWED_ON_RDN:
2511 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2513 case TLDAP_ALREADY_EXISTS:
2514 res = "TLDAP_ALREADY_EXISTS";
2516 case TLDAP_NO_OBJECT_CLASS_MODS:
2517 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2519 case TLDAP_RESULTS_TOO_LARGE:
2520 res = "TLDAP_RESULTS_TOO_LARGE";
2522 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2523 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2526 res = "TLDAP_OTHER";
2528 case TLDAP_SERVER_DOWN:
2529 res = "TLDAP_SERVER_DOWN";
2531 case TLDAP_LOCAL_ERROR:
2532 res = "TLDAP_LOCAL_ERROR";
2534 case TLDAP_ENCODING_ERROR:
2535 res = "TLDAP_ENCODING_ERROR";
2537 case TLDAP_DECODING_ERROR:
2538 res = "TLDAP_DECODING_ERROR";
2541 res = "TLDAP_TIMEOUT";
2543 case TLDAP_AUTH_UNKNOWN:
2544 res = "TLDAP_AUTH_UNKNOWN";
2546 case TLDAP_FILTER_ERROR:
2547 res = "TLDAP_FILTER_ERROR";
2549 case TLDAP_USER_CANCELLED:
2550 res = "TLDAP_USER_CANCELLED";
2552 case TLDAP_PARAM_ERROR:
2553 res = "TLDAP_PARAM_ERROR";
2555 case TLDAP_NO_MEMORY:
2556 res = "TLDAP_NO_MEMORY";
2558 case TLDAP_CONNECT_ERROR:
2559 res = "TLDAP_CONNECT_ERROR";
2561 case TLDAP_NOT_SUPPORTED:
2562 res = "TLDAP_NOT_SUPPORTED";
2564 case TLDAP_CONTROL_NOT_FOUND:
2565 res = "TLDAP_CONTROL_NOT_FOUND";
2567 case TLDAP_NO_RESULTS_RETURNED:
2568 res = "TLDAP_NO_RESULTS_RETURNED";
2570 case TLDAP_MORE_RESULTS_TO_RETURN:
2571 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2573 case TLDAP_CLIENT_LOOP:
2574 res = "TLDAP_CLIENT_LOOP";
2576 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2577 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2580 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2585 res = "Unknown LDAP Error";