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 tevent_context *ev;
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);
622 talloc_set_destructor(req, NULL);
623 tldap_msg_unset_pending(req);
624 num_pending = talloc_array_length(ld->pending);
626 tevent_req_done(req);
629 if (num_pending == 0) {
632 if (talloc_array_length(ld->pending) > num_pending) {
634 * The callback functions called from tevent_req_done() above
635 * have put something on the pending queue. We don't have to
636 * trigger the read_ldap_send(), tldap_msg_set_pending() has
637 * done it for us already.
642 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
643 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
644 if (subreq == NULL) {
645 status = TLDAP_NO_MEMORY;
648 tevent_req_set_callback(subreq, tldap_msg_received, ld);
652 while (talloc_array_length(ld->pending) > 0) {
653 req = ld->pending[0];
654 talloc_set_destructor(req, NULL);
655 tldap_msg_destructor(req);
656 tevent_req_error(req, status);
660 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
661 struct tldap_message **pmsg)
663 struct tldap_msg_state *state = tevent_req_data(
664 req, struct tldap_msg_state);
665 struct tldap_message *msg;
669 if (tevent_req_is_ldap_error(req, &err)) {
673 if (!asn1_peek_uint8(state->data, &msgtype)) {
674 return TLDAP_PROTOCOL_ERROR;
678 return TLDAP_SUCCESS;
681 msg = talloc_zero(mem_ctx, struct tldap_message);
683 return TLDAP_NO_MEMORY;
687 msg->inbuf = talloc_move(msg, &state->inbuf);
688 msg->data = talloc_move(msg, &state->data);
692 return TLDAP_SUCCESS;
695 struct tldap_req_state {
697 struct asn1_data *out;
698 struct tldap_message *result;
701 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
702 struct tldap_context *ld,
703 struct tldap_req_state **pstate)
705 struct tevent_req *req;
706 struct tldap_req_state *state;
708 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
713 state->out = asn1_init(state);
714 if (state->out == NULL) {
718 state->result = NULL;
719 state->id = tldap_next_msgid(ld);
721 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
722 asn1_write_Integer(state->out, state->id);
728 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
730 struct tldap_req_state *state = tevent_req_data(
731 req, struct tldap_req_state);
733 TALLOC_FREE(ld->last_msg);
734 ld->last_msg = talloc_move(ld, &state->result);
737 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
739 char *result = talloc_array(mem_ctx, char, blob.length+1);
741 if (result == NULL) {
745 memcpy(result, blob.data, blob.length);
746 result[blob.length] = '\0';
750 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
751 struct asn1_data *data,
756 if (!asn1_read_OctetString(data, mem_ctx, &string))
759 result = blob2string_talloc(mem_ctx, string);
761 data_blob_free(&string);
763 if (result == NULL) {
770 static bool tldap_decode_controls(struct tldap_req_state *state);
772 static bool tldap_decode_response(struct tldap_req_state *state)
774 struct asn1_data *data = state->result->data;
775 struct tldap_message *msg = state->result;
778 ok &= asn1_read_enumerated(data, &msg->lderr);
779 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
780 ok &= asn1_read_OctetString_talloc(msg, data,
781 &msg->res_diagnosticmessage);
782 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
783 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
784 ok &= asn1_read_OctetString_talloc(msg, data,
786 ok &= asn1_end_tag(data);
788 msg->res_referral = NULL;
794 static void tldap_sasl_bind_done(struct tevent_req *subreq);
796 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
797 struct tevent_context *ev,
798 struct tldap_context *ld,
800 const char *mechanism,
802 struct tldap_control *sctrls,
804 struct tldap_control *cctrls,
807 struct tevent_req *req, *subreq;
808 struct tldap_req_state *state;
810 req = tldap_req_create(mem_ctx, ld, &state);
819 asn1_push_tag(state->out, TLDAP_REQ_BIND);
820 asn1_write_Integer(state->out, ld->ld_version);
821 asn1_write_OctetString(state->out, dn, (dn != NULL) ? strlen(dn) : 0);
823 if (mechanism == NULL) {
824 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
825 asn1_write(state->out, creds->data, creds->length);
826 asn1_pop_tag(state->out);
828 asn1_push_tag(state->out, ASN1_CONTEXT(3));
829 asn1_write_OctetString(state->out, mechanism,
831 if ((creds != NULL) && (creds->data != NULL)) {
832 asn1_write_OctetString(state->out, creds->data,
835 asn1_pop_tag(state->out);
838 if (!asn1_pop_tag(state->out)) {
839 tevent_req_error(req, TLDAP_ENCODING_ERROR);
840 return tevent_req_post(req, ev);
843 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
845 if (tevent_req_nomem(subreq, req)) {
846 return tevent_req_post(req, ev);
848 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
852 static void tldap_sasl_bind_done(struct tevent_req *subreq)
854 struct tevent_req *req = tevent_req_callback_data(
855 subreq, struct tevent_req);
856 struct tldap_req_state *state = tevent_req_data(
857 req, struct tldap_req_state);
860 err = tldap_msg_recv(subreq, state, &state->result);
862 if (err != TLDAP_SUCCESS) {
863 tevent_req_error(req, err);
866 if (state->result->type != TLDAP_RES_BIND) {
867 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
870 if (!asn1_start_tag(state->result->data, state->result->type) ||
871 !tldap_decode_response(state) ||
872 !asn1_end_tag(state->result->data)) {
873 tevent_req_error(req, TLDAP_DECODING_ERROR);
877 * TODO: pull the reply blob
879 if (state->result->lderr != TLDAP_SUCCESS) {
880 tevent_req_error(req, state->result->lderr);
883 tevent_req_done(req);
886 int tldap_sasl_bind_recv(struct tevent_req *req)
888 return tldap_simple_recv(req);
891 int tldap_sasl_bind(struct tldap_context *ld,
893 const char *mechanism,
895 struct tldap_control *sctrls,
897 struct tldap_control *cctrls,
900 TALLOC_CTX *frame = talloc_stackframe();
901 struct tevent_context *ev;
902 struct tevent_req *req;
905 ev = event_context_init(frame);
907 result = TLDAP_NO_MEMORY;
911 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
912 sctrls, num_sctrls, cctrls, num_cctrls);
914 result = TLDAP_NO_MEMORY;
918 if (!tevent_req_poll(req, ev)) {
919 result = TLDAP_OPERATIONS_ERROR;
923 result = tldap_sasl_bind_recv(req);
924 tldap_save_msg(ld, req);
930 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
931 struct tevent_context *ev,
932 struct tldap_context *ld,
938 if (passwd != NULL) {
939 cred.data = (uint8_t *)passwd;
940 cred.length = strlen(passwd);
942 cred.data = (uint8_t *)"";
945 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
949 int tldap_simple_bind_recv(struct tevent_req *req)
951 return tldap_sasl_bind_recv(req);
954 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
959 if (passwd != NULL) {
960 cred.data = (uint8_t *)passwd;
961 cred.length = strlen(passwd);
963 cred.data = (uint8_t *)"";
966 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
969 /*****************************************************************************/
971 /* can't use isalpha() as only a strict set is valid for LDAP */
973 static bool tldap_is_alpha(char c)
975 return (((c >= 'a') && (c <= 'z')) || \
976 ((c >= 'A') && (c <= 'Z')));
979 static bool tldap_is_adh(char c)
981 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
984 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
985 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
986 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
987 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
988 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
989 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
990 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
991 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
992 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
993 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
995 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
996 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
997 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1000 /* oid's should be numerical only in theory,
1001 * but apparently some broken servers may have alphanum aliases instead.
1002 * Do like openldap libraries and allow alphanum aliases for oids, but
1003 * do not allow Tagging options in that case.
1005 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1007 bool is_oid = false;
1011 /* first char has stricter rules */
1014 } else if (!tldap_is_alpha(*s)) {
1015 /* bad first char */
1019 for (i = 1; i < len; i++) {
1022 if (isdigit(s[i])) {
1035 if (tldap_is_adh(s[i])) {
1042 /* no tagging options */
1049 if ((i + 1) == len) {
1067 /* this function copies the value until the closing parenthesis is found. */
1068 static char *tldap_get_val(TALLOC_CTX *memctx,
1069 const char *value, const char **_s)
1071 const char *s = value;
1073 /* find terminator */
1076 if (s && (*(s - 1) == '\\')) {
1081 if (!s || !(*s == ')')) {
1082 /* malformed filter */
1088 return talloc_strndup(memctx, value, s - value);
1091 static int tldap_hex2char(const char *x)
1093 if (isxdigit(x[0]) && isxdigit(x[1])) {
1094 const char h1 = x[0], h2 = x[1];
1097 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1098 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1099 else if (h1 >= '0') c = h1 - (int)'0';
1101 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1102 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1103 else if (h2 >= '0') c += h2 - (int)'0';
1111 static bool tldap_find_first_star(const char *val, const char **star)
1115 for (s = val; *s; s++) {
1118 if (isxdigit(s[1]) && isxdigit(s[2])) {
1122 /* not hex based escape, check older syntax */
1131 /* invalid escape sequence */
1136 /* end of val, nothing found */
1146 /* string ended without closing parenthesis, filter is malformed */
1150 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1154 for (i = 0,p = 0; i < *val_len; i++) {
1160 /* these must be escaped */
1164 if (!value[i + 1]) {
1170 c = tldap_hex2char(&value[i]);
1171 if (c >= 0 && c < 256) {
1183 value[p] = value[i];
1192 value[p] = value[i];
1201 static bool tldap_push_filter_basic(struct tldap_context *ld,
1202 struct asn1_data *data,
1204 static bool tldap_push_filter_substring(struct tldap_context *ld,
1205 struct asn1_data *data,
1208 static bool tldap_push_filter_int(struct tldap_context *ld,
1209 struct asn1_data *data,
1212 const char *s = *_s;
1216 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1217 "Incomplete or malformed filter\n");
1222 /* we are right after a parenthesis,
1223 * find out what op we have at hand */
1226 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1227 asn1_push_tag(data, TLDAP_FILTER_AND);
1232 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1233 asn1_push_tag(data, TLDAP_FILTER_OR);
1238 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1239 asn1_push_tag(data, TLDAP_FILTER_NOT);
1241 ret = tldap_push_filter_int(ld, data, &s);
1250 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1251 "Invalid parenthesis '%c'\n", *s);
1255 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1256 "Invalid filter termination\n");
1260 ret = tldap_push_filter_basic(ld, data, &s);
1267 /* only and/or filters get here.
1268 * go through the list of filters */
1271 /* RFC 4526: empty and/or */
1277 ret = tldap_push_filter_int(ld, data, &s);
1283 /* end of list, return */
1291 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1292 "Incomplete or malformed filter\n");
1297 if (data->has_error) {
1306 static bool tldap_push_filter_basic(struct tldap_context *ld,
1307 struct asn1_data *data,
1310 TALLOC_CTX *tmpctx = talloc_tos();
1311 const char *s = *_s;
1319 size_t type_len = 0;
1322 bool write_octect = true;
1325 eq = strchr(s, '=');
1327 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1328 "Invalid filter, missing equal sign\n");
1337 asn1_push_tag(data, TLDAP_FILTER_LE);
1341 asn1_push_tag(data, TLDAP_FILTER_GE);
1345 asn1_push_tag(data, TLDAP_FILTER_APX);
1349 asn1_push_tag(data, TLDAP_FILTER_EXT);
1350 write_octect = false;
1356 if (*s == ':') { /* [:dn]:rule:= value */
1358 /* malformed filter */
1362 } else { /* type[:dn][:rule]:= value */
1364 dn = strchr(s, ':');
1365 type_len = dn - type;
1366 if (dn == e) { /* type:= value */
1373 rule = strchr(dn, ':');
1374 if ((rule == dn + 1) || rule + 1 == e) {
1375 /* malformed filter, contains "::" */
1379 if (StrnCaseCmp(dn, "dn:", 3) != 0) {
1384 /* malformed filter. With two
1385 * optionals, the first must be "dn"
1398 if (!type && !dn && !rule) {
1399 /* malformed filter, there must be at least one */
1404 MatchingRuleAssertion ::= SEQUENCE {
1405 matchingRule [1] MatchingRuleID OPTIONAL,
1406 type [2] AttributeDescription OPTIONAL,
1407 matchValue [3] AssertionValue,
1408 dnAttributes [4] BOOLEAN DEFAULT FALSE
1412 /* check and add rule */
1414 ret = tldap_is_attrdesc(rule, e - rule, true);
1418 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1419 asn1_write(data, rule, e - rule);
1423 /* check and add type */
1425 ret = tldap_is_attrdesc(type, type_len, false);
1429 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1430 asn1_write(data, type, type_len);
1434 uval = tldap_get_val(tmpctx, val, _s);
1438 uval_len = *_s - val;
1439 ret = tldap_unescape_inplace(uval, &uval_len);
1444 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1445 asn1_write(data, uval, uval_len);
1448 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1449 asn1_write_uint8(data, dn?1:0);
1456 ret = tldap_is_attrdesc(s, e - s, false);
1461 if (strncmp(val, "*)", 2) == 0) {
1463 asn1_push_tag(data, TLDAP_FILTER_PRES);
1464 asn1_write(data, s, e - s);
1466 write_octect = false;
1470 ret = tldap_find_first_star(val, &star);
1476 asn1_push_tag(data, TLDAP_FILTER_SUB);
1477 asn1_write_OctetString(data, s, e - s);
1478 ret = tldap_push_filter_substring(ld, data, val, &s);
1483 write_octect = false;
1487 /* if nothing else, then it is just equality */
1488 asn1_push_tag(data, TLDAP_FILTER_EQ);
1489 write_octect = true;
1494 uval = tldap_get_val(tmpctx, val, _s);
1498 uval_len = *_s - val;
1499 ret = tldap_unescape_inplace(uval, &uval_len);
1504 asn1_write_OctetString(data, s, e - s);
1505 asn1_write_OctetString(data, uval, uval_len);
1508 if (data->has_error) {
1515 static bool tldap_push_filter_substring(struct tldap_context *ld,
1516 struct asn1_data *data,
1520 TALLOC_CTX *tmpctx = talloc_tos();
1521 bool initial = true;
1528 SubstringFilter ::= SEQUENCE {
1529 type AttributeDescription,
1530 -- at least one must be present
1531 substrings SEQUENCE OF CHOICE {
1532 initial [0] LDAPString,
1534 final [2] LDAPString } }
1536 asn1_push_tag(data, ASN1_SEQUENCE(0));
1539 ret = tldap_find_first_star(val, &star);
1543 chunk_len = star - val;
1547 if (!initial && chunk_len == 0) {
1548 /* found '**', which is illegal */
1564 if (initial && chunk_len == 0) {
1570 chunk = talloc_strndup(tmpctx, val, chunk_len);
1574 ret = tldap_unescape_inplace(chunk, &chunk_len);
1581 asn1_push_tag(data, TLDAP_SUB_INI);
1584 asn1_push_tag(data, TLDAP_SUB_ANY);
1588 asn1_push_tag(data, TLDAP_SUB_FIN);
1594 asn1_write(data, chunk, chunk_len);
1599 } while (*star == '*');
1603 /* end of sequence */
1608 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1609 * around parenthesis, we do not allow any spaces (except in values of
1610 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1611 * leading or trailing spaces where allowed.
1613 static bool tldap_push_filter(struct tldap_context *ld,
1614 struct asn1_data *data,
1617 const char *s = filter;
1620 ret = tldap_push_filter_int(ld, data, &s);
1622 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1623 "Incomplete or malformed filter\n");
1629 /*****************************************************************************/
1631 static void tldap_search_done(struct tevent_req *subreq);
1633 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1634 struct tevent_context *ev,
1635 struct tldap_context *ld,
1636 const char *base, int scope,
1641 struct tldap_control *sctrls,
1643 struct tldap_control *cctrls,
1649 struct tevent_req *req, *subreq;
1650 struct tldap_req_state *state;
1653 req = tldap_req_create(mem_ctx, ld, &state);
1658 asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1659 asn1_write_OctetString(state->out, base, strlen(base));
1660 asn1_write_enumerated(state->out, scope);
1661 asn1_write_enumerated(state->out, deref);
1662 asn1_write_Integer(state->out, sizelimit);
1663 asn1_write_Integer(state->out, timelimit);
1664 asn1_write_BOOLEAN(state->out, attrsonly);
1666 if (!tldap_push_filter(ld, state->out, filter)) {
1667 goto encoding_error;
1670 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1671 for (i=0; i<num_attrs; i++) {
1672 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1674 asn1_pop_tag(state->out);
1675 asn1_pop_tag(state->out);
1677 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1678 sctrls, num_sctrls);
1679 if (tevent_req_nomem(subreq, req)) {
1680 return tevent_req_post(req, ev);
1682 tevent_req_set_callback(subreq, tldap_search_done, req);
1686 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1687 return tevent_req_post(req, ev);
1690 static void tldap_search_done(struct tevent_req *subreq)
1692 struct tevent_req *req = tevent_req_callback_data(
1693 subreq, struct tevent_req);
1694 struct tldap_req_state *state = tevent_req_data(
1695 req, struct tldap_req_state);
1698 err = tldap_msg_recv(subreq, state, &state->result);
1699 if (err != TLDAP_SUCCESS) {
1700 tevent_req_error(req, err);
1703 switch (state->result->type) {
1704 case TLDAP_RES_SEARCH_ENTRY:
1705 case TLDAP_RES_SEARCH_REFERENCE:
1706 tevent_req_notify_callback(req);
1707 if (!tldap_msg_set_pending(subreq)) {
1708 tevent_req_nomem(NULL, req);
1712 case TLDAP_RES_SEARCH_RESULT:
1713 TALLOC_FREE(subreq);
1714 if (!asn1_start_tag(state->result->data,
1715 state->result->type) ||
1716 !tldap_decode_response(state) ||
1717 !asn1_end_tag(state->result->data) ||
1718 !tldap_decode_controls(state)) {
1719 tevent_req_error(req, TLDAP_DECODING_ERROR);
1722 tevent_req_done(req);
1725 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1730 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1731 struct tldap_message **pmsg)
1733 struct tldap_req_state *state = tevent_req_data(
1734 req, struct tldap_req_state);
1737 if (!tevent_req_is_in_progress(req)
1738 && tevent_req_is_ldap_error(req, &err)) {
1742 if (tevent_req_is_in_progress(req)) {
1743 switch (state->result->type) {
1744 case TLDAP_RES_SEARCH_ENTRY:
1745 case TLDAP_RES_SEARCH_REFERENCE:
1748 return TLDAP_OPERATIONS_ERROR;
1752 *pmsg = talloc_move(mem_ctx, &state->result);
1753 return TLDAP_SUCCESS;
1756 struct tldap_sync_search_state {
1757 TALLOC_CTX *mem_ctx;
1758 struct tldap_message **entries;
1759 struct tldap_message **refs;
1763 static void tldap_search_cb(struct tevent_req *req)
1765 struct tldap_sync_search_state *state =
1766 (struct tldap_sync_search_state *)
1767 tevent_req_callback_data_void(req);
1768 struct tldap_message *msg, **tmp;
1769 int rc, num_entries, num_refs;
1771 rc = tldap_search_recv(req, talloc_tos(), &msg);
1772 if (rc != TLDAP_SUCCESS) {
1777 switch (tldap_msg_type(msg)) {
1778 case TLDAP_RES_SEARCH_ENTRY:
1779 num_entries = talloc_array_length(state->entries);
1780 tmp = talloc_realloc(state->mem_ctx, state->entries,
1781 struct tldap_message *, num_entries + 1);
1783 state->rc = TLDAP_NO_MEMORY;
1786 state->entries = tmp;
1787 state->entries[num_entries] = talloc_move(state->entries,
1790 case TLDAP_RES_SEARCH_REFERENCE:
1791 num_refs = talloc_array_length(state->refs);
1792 tmp = talloc_realloc(state->mem_ctx, state->refs,
1793 struct tldap_message *, num_refs + 1);
1795 state->rc = TLDAP_NO_MEMORY;
1799 state->refs[num_refs] = talloc_move(state->refs, &msg);
1801 case TLDAP_RES_SEARCH_RESULT:
1802 state->rc = TLDAP_SUCCESS;
1805 state->rc = TLDAP_PROTOCOL_ERROR;
1810 int tldap_search(struct tldap_context *ld,
1811 const char *base, int scope, const char *filter,
1812 const char **attrs, int num_attrs, int attrsonly,
1813 struct tldap_control *sctrls, int num_sctrls,
1814 struct tldap_control *cctrls, int num_cctrls,
1815 int timelimit, int sizelimit, int deref,
1816 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1817 struct tldap_message ***refs)
1819 TALLOC_CTX *frame = talloc_stackframe();
1820 struct tevent_context *ev;
1821 struct tevent_req *req;
1822 struct tldap_sync_search_state state;
1825 state.mem_ctx = mem_ctx;
1826 state.rc = TLDAP_SUCCESS;
1828 ev = event_context_init(frame);
1830 state.rc = TLDAP_NO_MEMORY;
1834 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1835 attrs, num_attrs, attrsonly,
1836 sctrls, num_sctrls, cctrls, num_cctrls,
1837 timelimit, sizelimit, deref);
1839 state.rc = TLDAP_NO_MEMORY;
1843 tevent_req_set_callback(req, tldap_search_cb, &state);
1845 if (!tevent_req_is_in_progress(req)) {
1846 /* an error happend before sending */
1847 if (tevent_req_is_ldap_error(req, &state.rc)) {
1852 while (tevent_req_is_in_progress(req)
1853 && (state.rc == TLDAP_SUCCESS)) {
1854 if (tevent_loop_once(ev) == -1) {
1855 return TLDAP_OPERATIONS_ERROR;
1859 if (state.rc != TLDAP_SUCCESS) {
1863 if (entries != NULL) {
1864 *entries = state.entries;
1866 TALLOC_FREE(state.entries);
1871 TALLOC_FREE(state.refs);
1873 tldap_save_msg(ld, req);
1879 static bool tldap_parse_search_entry(struct tldap_message *msg)
1881 int num_attribs = 0;
1883 asn1_start_tag(msg->data, msg->type);
1887 asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1888 if (msg->dn == NULL) {
1893 * Attributes: We overallocate msg->attribs by one, so that while
1894 * looping over the attributes we can directly parse into the last
1895 * array element. Same for the values in the inner loop.
1898 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1899 if (msg->attribs == NULL) {
1903 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1904 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1905 struct tldap_attribute *attrib;
1908 attrib = &msg->attribs[num_attribs];
1909 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1910 if (attrib->values == NULL) {
1913 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1914 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1916 asn1_start_tag(msg->data, ASN1_SET);
1918 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1919 asn1_read_OctetString(msg->data, msg,
1920 &attrib->values[num_values]);
1922 attrib->values = talloc_realloc(
1923 msg->attribs, attrib->values, DATA_BLOB,
1925 if (attrib->values == NULL) {
1930 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1931 DATA_BLOB, num_values);
1932 attrib->num_values = num_values;
1934 asn1_end_tag(msg->data); /* ASN1_SET */
1935 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1936 msg->attribs = talloc_realloc(
1937 msg, msg->attribs, struct tldap_attribute,
1939 if (msg->attribs == NULL) {
1944 msg->attribs = talloc_realloc(
1945 msg, msg->attribs, struct tldap_attribute, num_attribs);
1946 asn1_end_tag(msg->data);
1947 if (msg->data->has_error) {
1953 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1955 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1962 bool tldap_entry_attributes(struct tldap_message *msg,
1963 struct tldap_attribute **attributes,
1964 int *num_attributes)
1966 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1969 *attributes = msg->attribs;
1970 *num_attributes = talloc_array_length(msg->attribs);
1974 static bool tldap_decode_controls(struct tldap_req_state *state)
1976 struct tldap_message *msg = state->result;
1977 struct asn1_data *data = msg->data;
1978 struct tldap_control *sctrls = NULL;
1979 int num_controls = 0;
1981 msg->res_sctrls = NULL;
1983 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1987 asn1_start_tag(data, ASN1_CONTEXT(0));
1989 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
1990 struct tldap_control *c;
1993 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
1995 if (sctrls == NULL) {
1998 c = &sctrls[num_controls];
2000 asn1_start_tag(data, ASN1_SEQUENCE(0));
2001 asn1_read_OctetString_talloc(msg, data, &oid);
2002 if ((data->has_error) || (oid == NULL)) {
2006 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2007 asn1_read_BOOLEAN(data, &c->critical);
2009 c->critical = false;
2011 c->value = data_blob_null;
2012 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2013 !asn1_read_OctetString(data, msg, &c->value)) {
2016 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
2021 asn1_end_tag(data); /* ASN1_CONTEXT(0) */
2023 if (data->has_error) {
2024 TALLOC_FREE(sctrls);
2027 msg->res_sctrls = sctrls;
2031 static void tldap_simple_done(struct tevent_req *subreq, int type)
2033 struct tevent_req *req = tevent_req_callback_data(
2034 subreq, struct tevent_req);
2035 struct tldap_req_state *state = tevent_req_data(
2036 req, struct tldap_req_state);
2039 err = tldap_msg_recv(subreq, state, &state->result);
2040 TALLOC_FREE(subreq);
2041 if (err != TLDAP_SUCCESS) {
2042 tevent_req_error(req, err);
2045 if (state->result->type != type) {
2046 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2049 if (!asn1_start_tag(state->result->data, state->result->type) ||
2050 !tldap_decode_response(state) ||
2051 !asn1_end_tag(state->result->data) ||
2052 !tldap_decode_controls(state)) {
2053 tevent_req_error(req, TLDAP_DECODING_ERROR);
2056 if (state->result->lderr != TLDAP_SUCCESS) {
2057 tevent_req_error(req, state->result->lderr);
2060 tevent_req_done(req);
2063 static int tldap_simple_recv(struct tevent_req *req)
2066 if (tevent_req_is_ldap_error(req, &err)) {
2069 return TLDAP_SUCCESS;
2072 static void tldap_add_done(struct tevent_req *subreq);
2074 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2075 struct tevent_context *ev,
2076 struct tldap_context *ld,
2078 struct tldap_mod *attributes,
2080 struct tldap_control *sctrls,
2082 struct tldap_control *cctrls,
2085 struct tevent_req *req, *subreq;
2086 struct tldap_req_state *state;
2089 req = tldap_req_create(mem_ctx, ld, &state);
2094 asn1_push_tag(state->out, TLDAP_REQ_ADD);
2095 asn1_write_OctetString(state->out, dn, strlen(dn));
2096 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2098 for (i=0; i<num_attributes; i++) {
2099 struct tldap_mod *attrib = &attributes[i];
2100 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2101 asn1_write_OctetString(state->out, attrib->attribute,
2102 strlen(attrib->attribute));
2103 asn1_push_tag(state->out, ASN1_SET);
2104 for (j=0; j<attrib->num_values; j++) {
2105 asn1_write_OctetString(state->out,
2106 attrib->values[j].data,
2107 attrib->values[j].length);
2109 asn1_pop_tag(state->out);
2110 asn1_pop_tag(state->out);
2113 asn1_pop_tag(state->out);
2114 asn1_pop_tag(state->out);
2116 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2117 sctrls, num_sctrls);
2118 if (tevent_req_nomem(subreq, req)) {
2119 return tevent_req_post(req, ev);
2121 tevent_req_set_callback(subreq, tldap_add_done, req);
2125 static void tldap_add_done(struct tevent_req *subreq)
2127 tldap_simple_done(subreq, TLDAP_RES_ADD);
2130 int tldap_add_recv(struct tevent_req *req)
2132 return tldap_simple_recv(req);
2135 int tldap_add(struct tldap_context *ld, const char *dn,
2136 struct tldap_mod *attributes, int num_attributes,
2137 struct tldap_control *sctrls, int num_sctrls,
2138 struct tldap_control *cctrls, int num_cctrls)
2140 TALLOC_CTX *frame = talloc_stackframe();
2141 struct tevent_context *ev;
2142 struct tevent_req *req;
2145 ev = event_context_init(frame);
2147 result = TLDAP_NO_MEMORY;
2151 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2152 sctrls, num_sctrls, cctrls, num_cctrls);
2154 result = TLDAP_NO_MEMORY;
2158 if (!tevent_req_poll(req, ev)) {
2159 result = TLDAP_OPERATIONS_ERROR;
2163 result = tldap_add_recv(req);
2164 tldap_save_msg(ld, req);
2170 static void tldap_modify_done(struct tevent_req *subreq);
2172 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2173 struct tevent_context *ev,
2174 struct tldap_context *ld,
2176 struct tldap_mod *mods, int num_mods,
2177 struct tldap_control *sctrls,
2179 struct tldap_control *cctrls,
2182 struct tevent_req *req, *subreq;
2183 struct tldap_req_state *state;
2186 req = tldap_req_create(mem_ctx, ld, &state);
2191 asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
2192 asn1_write_OctetString(state->out, dn, strlen(dn));
2193 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2195 for (i=0; i<num_mods; i++) {
2196 struct tldap_mod *mod = &mods[i];
2197 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2198 asn1_write_enumerated(state->out, mod->mod_op),
2199 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2200 asn1_write_OctetString(state->out, mod->attribute,
2201 strlen(mod->attribute));
2202 asn1_push_tag(state->out, ASN1_SET);
2203 for (j=0; j<mod->num_values; j++) {
2204 asn1_write_OctetString(state->out,
2205 mod->values[j].data,
2206 mod->values[j].length);
2208 asn1_pop_tag(state->out);
2209 asn1_pop_tag(state->out);
2210 asn1_pop_tag(state->out);
2213 asn1_pop_tag(state->out);
2214 asn1_pop_tag(state->out);
2216 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2217 sctrls, num_sctrls);
2218 if (tevent_req_nomem(subreq, req)) {
2219 return tevent_req_post(req, ev);
2221 tevent_req_set_callback(subreq, tldap_modify_done, req);
2225 static void tldap_modify_done(struct tevent_req *subreq)
2227 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2230 int tldap_modify_recv(struct tevent_req *req)
2232 return tldap_simple_recv(req);
2235 int tldap_modify(struct tldap_context *ld, const char *dn,
2236 struct tldap_mod *mods, int num_mods,
2237 struct tldap_control *sctrls, int num_sctrls,
2238 struct tldap_control *cctrls, int num_cctrls)
2240 TALLOC_CTX *frame = talloc_stackframe();
2241 struct tevent_context *ev;
2242 struct tevent_req *req;
2245 ev = event_context_init(frame);
2247 result = TLDAP_NO_MEMORY;
2251 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2252 sctrls, num_sctrls, cctrls, num_cctrls);
2254 result = TLDAP_NO_MEMORY;
2258 if (!tevent_req_poll(req, ev)) {
2259 result = TLDAP_OPERATIONS_ERROR;
2263 result = tldap_modify_recv(req);
2264 tldap_save_msg(ld, req);
2270 static void tldap_delete_done(struct tevent_req *subreq);
2272 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2273 struct tevent_context *ev,
2274 struct tldap_context *ld,
2276 struct tldap_control *sctrls,
2278 struct tldap_control *cctrls,
2281 struct tevent_req *req, *subreq;
2282 struct tldap_req_state *state;
2284 req = tldap_req_create(mem_ctx, ld, &state);
2289 asn1_push_tag(state->out, TLDAP_REQ_DELETE);
2290 asn1_write(state->out, dn, strlen(dn));
2291 asn1_pop_tag(state->out);
2293 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2294 sctrls, num_sctrls);
2295 if (tevent_req_nomem(subreq, req)) {
2296 return tevent_req_post(req, ev);
2298 tevent_req_set_callback(subreq, tldap_delete_done, req);
2302 static void tldap_delete_done(struct tevent_req *subreq)
2304 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2307 int tldap_delete_recv(struct tevent_req *req)
2309 return tldap_simple_recv(req);
2312 int tldap_delete(struct tldap_context *ld, const char *dn,
2313 struct tldap_control *sctrls, int num_sctrls,
2314 struct tldap_control *cctrls, int num_cctrls)
2316 TALLOC_CTX *frame = talloc_stackframe();
2317 struct tevent_context *ev;
2318 struct tevent_req *req;
2321 ev = event_context_init(frame);
2323 result = TLDAP_NO_MEMORY;
2327 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2328 cctrls, num_cctrls);
2330 result = TLDAP_NO_MEMORY;
2334 if (!tevent_req_poll(req, ev)) {
2335 result = TLDAP_OPERATIONS_ERROR;
2339 result = tldap_delete_recv(req);
2340 tldap_save_msg(ld, req);
2346 int tldap_msg_id(const struct tldap_message *msg)
2351 int tldap_msg_type(const struct tldap_message *msg)
2356 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2361 return msg->res_matcheddn;
2364 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2369 return msg->res_diagnosticmessage;
2372 const char *tldap_msg_referral(struct tldap_message *msg)
2377 return msg->res_referral;
2380 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2381 struct tldap_control **sctrls)
2388 *sctrls = msg->res_sctrls;
2389 *num_sctrls = talloc_array_length(msg->res_sctrls);
2392 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2394 return ld->last_msg;
2397 const char *tldap_err2string(int rc)
2399 const char *res = NULL;
2402 * This would normally be a table, but the error codes are not fully
2403 * sequential. Let the compiler figure out the optimum implementation
2409 res = "TLDAP_SUCCESS";
2411 case TLDAP_OPERATIONS_ERROR:
2412 res = "TLDAP_OPERATIONS_ERROR";
2414 case TLDAP_PROTOCOL_ERROR:
2415 res = "TLDAP_PROTOCOL_ERROR";
2417 case TLDAP_TIMELIMIT_EXCEEDED:
2418 res = "TLDAP_TIMELIMIT_EXCEEDED";
2420 case TLDAP_SIZELIMIT_EXCEEDED:
2421 res = "TLDAP_SIZELIMIT_EXCEEDED";
2423 case TLDAP_COMPARE_FALSE:
2424 res = "TLDAP_COMPARE_FALSE";
2426 case TLDAP_COMPARE_TRUE:
2427 res = "TLDAP_COMPARE_TRUE";
2429 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2430 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2432 case TLDAP_STRONG_AUTH_REQUIRED:
2433 res = "TLDAP_STRONG_AUTH_REQUIRED";
2435 case TLDAP_REFERRAL:
2436 res = "TLDAP_REFERRAL";
2438 case TLDAP_ADMINLIMIT_EXCEEDED:
2439 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2441 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2442 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2444 case TLDAP_CONFIDENTIALITY_REQUIRED:
2445 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2447 case TLDAP_SASL_BIND_IN_PROGRESS:
2448 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2450 case TLDAP_NO_SUCH_ATTRIBUTE:
2451 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2453 case TLDAP_UNDEFINED_TYPE:
2454 res = "TLDAP_UNDEFINED_TYPE";
2456 case TLDAP_INAPPROPRIATE_MATCHING:
2457 res = "TLDAP_INAPPROPRIATE_MATCHING";
2459 case TLDAP_CONSTRAINT_VIOLATION:
2460 res = "TLDAP_CONSTRAINT_VIOLATION";
2462 case TLDAP_TYPE_OR_VALUE_EXISTS:
2463 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2465 case TLDAP_INVALID_SYNTAX:
2466 res = "TLDAP_INVALID_SYNTAX";
2468 case TLDAP_NO_SUCH_OBJECT:
2469 res = "TLDAP_NO_SUCH_OBJECT";
2471 case TLDAP_ALIAS_PROBLEM:
2472 res = "TLDAP_ALIAS_PROBLEM";
2474 case TLDAP_INVALID_DN_SYNTAX:
2475 res = "TLDAP_INVALID_DN_SYNTAX";
2478 res = "TLDAP_IS_LEAF";
2480 case TLDAP_ALIAS_DEREF_PROBLEM:
2481 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2483 case TLDAP_INAPPROPRIATE_AUTH:
2484 res = "TLDAP_INAPPROPRIATE_AUTH";
2486 case TLDAP_INVALID_CREDENTIALS:
2487 res = "TLDAP_INVALID_CREDENTIALS";
2489 case TLDAP_INSUFFICIENT_ACCESS:
2490 res = "TLDAP_INSUFFICIENT_ACCESS";
2495 case TLDAP_UNAVAILABLE:
2496 res = "TLDAP_UNAVAILABLE";
2498 case TLDAP_UNWILLING_TO_PERFORM:
2499 res = "TLDAP_UNWILLING_TO_PERFORM";
2501 case TLDAP_LOOP_DETECT:
2502 res = "TLDAP_LOOP_DETECT";
2504 case TLDAP_NAMING_VIOLATION:
2505 res = "TLDAP_NAMING_VIOLATION";
2507 case TLDAP_OBJECT_CLASS_VIOLATION:
2508 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2510 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2511 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2513 case TLDAP_NOT_ALLOWED_ON_RDN:
2514 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2516 case TLDAP_ALREADY_EXISTS:
2517 res = "TLDAP_ALREADY_EXISTS";
2519 case TLDAP_NO_OBJECT_CLASS_MODS:
2520 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2522 case TLDAP_RESULTS_TOO_LARGE:
2523 res = "TLDAP_RESULTS_TOO_LARGE";
2525 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2526 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2529 res = "TLDAP_OTHER";
2531 case TLDAP_SERVER_DOWN:
2532 res = "TLDAP_SERVER_DOWN";
2534 case TLDAP_LOCAL_ERROR:
2535 res = "TLDAP_LOCAL_ERROR";
2537 case TLDAP_ENCODING_ERROR:
2538 res = "TLDAP_ENCODING_ERROR";
2540 case TLDAP_DECODING_ERROR:
2541 res = "TLDAP_DECODING_ERROR";
2544 res = "TLDAP_TIMEOUT";
2546 case TLDAP_AUTH_UNKNOWN:
2547 res = "TLDAP_AUTH_UNKNOWN";
2549 case TLDAP_FILTER_ERROR:
2550 res = "TLDAP_FILTER_ERROR";
2552 case TLDAP_USER_CANCELLED:
2553 res = "TLDAP_USER_CANCELLED";
2555 case TLDAP_PARAM_ERROR:
2556 res = "TLDAP_PARAM_ERROR";
2558 case TLDAP_NO_MEMORY:
2559 res = "TLDAP_NO_MEMORY";
2561 case TLDAP_CONNECT_ERROR:
2562 res = "TLDAP_CONNECT_ERROR";
2564 case TLDAP_NOT_SUPPORTED:
2565 res = "TLDAP_NOT_SUPPORTED";
2567 case TLDAP_CONTROL_NOT_FOUND:
2568 res = "TLDAP_CONTROL_NOT_FOUND";
2570 case TLDAP_NO_RESULTS_RETURNED:
2571 res = "TLDAP_NO_RESULTS_RETURNED";
2573 case TLDAP_MORE_RESULTS_TO_RETURN:
2574 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2576 case TLDAP_CLIENT_LOOP:
2577 res = "TLDAP_CLIENT_LOOP";
2579 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2580 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2583 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2588 res = "Unknown LDAP Error";