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 bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
24 enum tevent_req_state state;
27 if (!tevent_req_is_error(req, &state, &err)) {
31 case TEVENT_REQ_TIMED_OUT:
32 *perr = TLDAP_TIMEOUT;
34 case TEVENT_REQ_NO_MEMORY:
35 *perr = TLDAP_NO_MEMORY;
37 case TEVENT_REQ_USER_ERROR:
41 *perr = TLDAP_OPERATIONS_ERROR;
47 struct tldap_ctx_attribute {
52 struct tldap_context {
57 struct tstream_context *conn;
60 struct tevent_queue *outgoing;
61 struct tevent_req **pending;
63 /* For the sync wrappers we need something like get_last_error... */
64 struct tldap_message *last_msg;
67 void (*log_fn)(void *context, enum tldap_debug_level level,
68 const char *fmt, va_list ap);
71 struct tldap_ctx_attribute *ctx_attrs;
74 struct tldap_message {
75 struct asn1_data *data;
82 struct tldap_attribute *attribs;
84 /* Error data sent by the server */
87 char *res_diagnosticmessage;
89 struct tldap_control *res_sctrls;
91 /* Controls sent by the server */
92 struct tldap_control *ctrls;
95 void tldap_set_debug(struct tldap_context *ld,
96 void (*log_fn)(void *log_private,
97 enum tldap_debug_level level,
99 va_list ap) PRINTF_ATTRIBUTE(3,0),
103 ld->log_private = log_private;
106 static void tldap_debug(struct tldap_context *ld,
107 enum tldap_debug_level level,
108 const char *fmt, ...)
114 if (ld->log_fn == NULL) {
118 ld->log_fn(ld->log_private, level, fmt, ap);
122 static int tldap_next_msgid(struct tldap_context *ld)
126 result = ld->msgid++;
127 if (ld->msgid == 2147483647) {
133 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
135 struct tldap_context *ctx;
138 ctx = talloc_zero(mem_ctx, struct tldap_context);
142 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
149 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
150 if (ctx->outgoing == NULL) {
157 bool tldap_connection_ok(struct tldap_context *ld)
162 return !ld->server_down;
165 static struct tldap_ctx_attribute *tldap_context_findattr(
166 struct tldap_context *ld, const char *name)
170 num_attrs = talloc_array_length(ld->ctx_attrs);
172 for (i=0; i<num_attrs; i++) {
173 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
174 return &ld->ctx_attrs[i];
180 bool tldap_context_setattr(struct tldap_context *ld,
181 const char *name, const void *_pptr)
183 struct tldap_ctx_attribute *tmp, *attr;
186 void **pptr = (void **)_pptr;
188 attr = tldap_context_findattr(ld, name);
191 * We don't actually delete attrs, we don't expect tons of
192 * attributes being shuffled around.
194 TALLOC_FREE(attr->ptr);
196 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
202 tmpname = talloc_strdup(ld, name);
203 if (tmpname == NULL) {
207 num_attrs = talloc_array_length(ld->ctx_attrs);
209 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
212 TALLOC_FREE(tmpname);
215 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
217 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
219 tmp[num_attrs].ptr = NULL;
226 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
228 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
236 struct read_ldap_state {
241 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
242 static void read_ldap_done(struct tevent_req *subreq);
244 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
245 struct tevent_context *ev,
246 struct tstream_context *conn)
248 struct tevent_req *req, *subreq;
249 struct read_ldap_state *state;
251 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
257 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
259 if (tevent_req_nomem(subreq, req)) {
260 return tevent_req_post(req, ev);
262 tevent_req_set_callback(subreq, read_ldap_done, req);
266 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
268 struct read_ldap_state *state = talloc_get_type_abort(
269 private_data, struct read_ldap_state);
274 /* We've been here, we're done */
279 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
281 if (buf[0] != 0x30) {
286 if ((len & 0x80) == 0) {
291 lensize = (len & 0x7f);
295 /* Please get us the full length */
298 if (buflen > 2 + lensize) {
302 if (buflen != 2 + lensize) {
306 for (i=0; i<lensize; i++) {
307 len = (len << 8) | buf[2+i];
312 static void read_ldap_done(struct tevent_req *subreq)
314 struct tevent_req *req = tevent_req_callback_data(
315 subreq, struct tevent_req);
316 struct read_ldap_state *state = tevent_req_data(
317 req, struct read_ldap_state);
321 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
324 tevent_req_error(req, err);
327 tevent_req_done(req);
330 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
331 uint8_t **pbuf, int *perrno)
333 struct read_ldap_state *state = tevent_req_data(
334 req, struct read_ldap_state);
336 if (tevent_req_is_unix_error(req, perrno)) {
339 *pbuf = talloc_move(mem_ctx, &state->buf);
340 return talloc_get_size(*pbuf);
343 struct tldap_msg_state {
344 struct tldap_context *ld;
345 struct tevent_context *ev;
349 struct asn1_data *data;
353 static void tldap_push_controls(struct asn1_data *data,
354 struct tldap_control *sctrls,
359 if ((sctrls == NULL) || (num_sctrls == 0)) {
363 asn1_push_tag(data, ASN1_CONTEXT(0));
365 for (i=0; i<num_sctrls; i++) {
366 struct tldap_control *c = &sctrls[i];
367 asn1_push_tag(data, ASN1_SEQUENCE(0));
368 asn1_write_OctetString(data, c->oid, strlen(c->oid));
370 asn1_write_BOOLEAN(data, true);
372 if (c->value.data != NULL) {
373 asn1_write_OctetString(data, c->value.data,
376 asn1_pop_tag(data); /* ASN1_SEQUENCE(0) */
379 asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
382 static void tldap_msg_sent(struct tevent_req *subreq);
383 static void tldap_msg_received(struct tevent_req *subreq);
385 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
386 struct tevent_context *ev,
387 struct tldap_context *ld,
388 int id, struct asn1_data *data,
389 struct tldap_control *sctrls,
392 struct tevent_req *req, *subreq;
393 struct tldap_msg_state *state;
396 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
399 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
407 if (state->ld->server_down) {
408 tevent_req_error(req, TLDAP_SERVER_DOWN);
409 return tevent_req_post(req, ev);
412 tldap_push_controls(data, sctrls, num_sctrls);
416 if (!asn1_blob(data, &blob)) {
417 tevent_req_error(req, TLDAP_ENCODING_ERROR);
418 return tevent_req_post(req, ev);
421 state->iov.iov_base = blob.data;
422 state->iov.iov_len = blob.length;
424 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
426 if (tevent_req_nomem(subreq, req)) {
427 return tevent_req_post(req, ev);
429 tevent_req_set_callback(subreq, tldap_msg_sent, req);
433 static void tldap_msg_unset_pending(struct tevent_req *req)
435 struct tldap_msg_state *state = tevent_req_data(
436 req, struct tldap_msg_state);
437 struct tldap_context *ld = state->ld;
438 int num_pending = talloc_array_length(ld->pending);
441 if (num_pending == 1) {
442 TALLOC_FREE(ld->pending);
446 for (i=0; i<num_pending; i++) {
447 if (req == ld->pending[i]) {
451 if (i == num_pending) {
453 * Something's seriously broken. Just returning here is the
454 * right thing nevertheless, the point of this routine is to
455 * remove ourselves from cli->pending.
461 * Remove ourselves from the cli->pending array
463 if (num_pending > 1) {
464 ld->pending[i] = ld->pending[num_pending-1];
468 * No NULL check here, we're shrinking by sizeof(void *), and
469 * talloc_realloc just adjusts the size for this.
471 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
476 static int tldap_msg_destructor(struct tevent_req *req)
478 tldap_msg_unset_pending(req);
482 static bool tldap_msg_set_pending(struct tevent_req *req)
484 struct tldap_msg_state *state = tevent_req_data(
485 req, struct tldap_msg_state);
486 struct tldap_context *ld;
487 struct tevent_req **pending;
489 struct tevent_req *subreq;
492 num_pending = talloc_array_length(ld->pending);
494 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
496 if (pending == NULL) {
499 pending[num_pending] = req;
500 ld->pending = pending;
501 talloc_set_destructor(req, tldap_msg_destructor);
503 if (num_pending > 0) {
508 * We're the first ones, add the read_ldap request that waits for the
509 * answer from the server
511 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
512 if (subreq == NULL) {
513 tldap_msg_unset_pending(req);
516 tevent_req_set_callback(subreq, tldap_msg_received, ld);
520 static void tldap_msg_sent(struct tevent_req *subreq)
522 struct tevent_req *req = tevent_req_callback_data(
523 subreq, struct tevent_req);
524 struct tldap_msg_state *state = tevent_req_data(
525 req, struct tldap_msg_state);
529 nwritten = tstream_writev_queue_recv(subreq, &err);
531 if (nwritten == -1) {
532 state->ld->server_down = true;
533 tevent_req_error(req, TLDAP_SERVER_DOWN);
537 if (!tldap_msg_set_pending(req)) {
538 tevent_req_nomem(NULL, req);
543 static int tldap_msg_msgid(struct tevent_req *req)
545 struct tldap_msg_state *state = tevent_req_data(
546 req, struct tldap_msg_state);
551 static void tldap_msg_received(struct tevent_req *subreq)
553 struct tldap_context *ld = tevent_req_callback_data(
554 subreq, struct tldap_context);
555 struct tevent_req *req;
556 struct tldap_msg_state *state;
557 struct tevent_context *ev;
558 struct asn1_data *data;
567 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
569 if (received == -1) {
570 status = TLDAP_SERVER_DOWN;
574 data = asn1_init(talloc_tos());
576 status = TLDAP_NO_MEMORY;
579 asn1_load_nocopy(data, inbuf, received);
582 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
583 ok &= asn1_read_Integer(data, &id);
584 ok &= asn1_peek_uint8(data, &type);
587 status = TLDAP_PROTOCOL_ERROR;
591 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
592 "type %d\n", id, (int)type);
594 num_pending = talloc_array_length(ld->pending);
596 for (i=0; i<num_pending; i++) {
597 if (id == tldap_msg_msgid(ld->pending[i])) {
601 if (i == num_pending) {
602 /* Dump unexpected reply */
603 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
604 "No request pending for msg %d\n", id);
610 req = ld->pending[i];
611 state = tevent_req_data(req, struct tldap_msg_state);
613 state->inbuf = talloc_move(state, &inbuf);
614 state->data = talloc_move(state, &data);
618 talloc_set_destructor(req, NULL);
619 tldap_msg_unset_pending(req);
620 num_pending = talloc_array_length(ld->pending);
622 tevent_req_done(req);
625 if (num_pending == 0) {
628 if (talloc_array_length(ld->pending) > num_pending) {
630 * The callback functions called from tevent_req_done() above
631 * have put something on the pending queue. We don't have to
632 * trigger the read_ldap_send(), tldap_msg_set_pending() has
633 * done it for us already.
638 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
639 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
640 if (subreq == NULL) {
641 status = TLDAP_NO_MEMORY;
644 tevent_req_set_callback(subreq, tldap_msg_received, ld);
648 while (talloc_array_length(ld->pending) > 0) {
649 req = ld->pending[0];
650 talloc_set_destructor(req, NULL);
651 tldap_msg_destructor(req);
652 tevent_req_error(req, status);
656 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
657 struct tldap_message **pmsg)
659 struct tldap_msg_state *state = tevent_req_data(
660 req, struct tldap_msg_state);
661 struct tldap_message *msg;
665 if (tevent_req_is_ldap_error(req, &err)) {
669 if (!asn1_peek_uint8(state->data, &msgtype)) {
670 return TLDAP_PROTOCOL_ERROR;
674 return TLDAP_SUCCESS;
677 msg = talloc_zero(mem_ctx, struct tldap_message);
679 return TLDAP_NO_MEMORY;
683 msg->inbuf = talloc_move(msg, &state->inbuf);
684 msg->data = talloc_move(msg, &state->data);
688 return TLDAP_SUCCESS;
691 struct tldap_req_state {
693 struct asn1_data *out;
694 struct tldap_message *result;
697 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
698 struct tldap_context *ld,
699 struct tldap_req_state **pstate)
701 struct tevent_req *req;
702 struct tldap_req_state *state;
704 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
709 state->out = asn1_init(state);
710 if (state->out == NULL) {
714 state->result = NULL;
715 state->id = tldap_next_msgid(ld);
717 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
718 asn1_write_Integer(state->out, state->id);
724 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
726 struct tldap_req_state *state = tevent_req_data(
727 req, struct tldap_req_state);
729 TALLOC_FREE(ld->last_msg);
730 ld->last_msg = talloc_move(ld, &state->result);
733 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
735 char *result = talloc_array(mem_ctx, char, blob.length+1);
736 memcpy(result, blob.data, blob.length);
737 result[blob.length] = '\0';
741 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
742 struct asn1_data *data,
746 if (!asn1_read_OctetString(data, mem_ctx, &string))
748 *result = blob2string_talloc(mem_ctx, string);
749 data_blob_free(&string);
753 static bool tldap_decode_controls(struct tldap_req_state *state);
755 static bool tldap_decode_response(struct tldap_req_state *state)
757 struct asn1_data *data = state->result->data;
758 struct tldap_message *msg = state->result;
761 ok &= asn1_read_enumerated(data, &msg->lderr);
762 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
763 ok &= asn1_read_OctetString_talloc(msg, data,
764 &msg->res_diagnosticmessage);
765 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
766 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
767 ok &= asn1_read_OctetString_talloc(msg, data,
769 ok &= asn1_end_tag(data);
771 msg->res_referral = NULL;
777 static void tldap_sasl_bind_done(struct tevent_req *subreq);
779 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
780 struct tevent_context *ev,
781 struct tldap_context *ld,
783 const char *mechanism,
785 struct tldap_control *sctrls,
787 struct tldap_control *cctrls,
790 struct tevent_req *req, *subreq;
791 struct tldap_req_state *state;
793 req = tldap_req_create(mem_ctx, ld, &state);
802 asn1_push_tag(state->out, TLDAP_REQ_BIND);
803 asn1_write_Integer(state->out, ld->ld_version);
804 asn1_write_OctetString(state->out, dn, (dn != NULL) ? strlen(dn) : 0);
806 if (mechanism == NULL) {
807 asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0));
808 asn1_write(state->out, creds->data, creds->length);
809 asn1_pop_tag(state->out);
811 asn1_push_tag(state->out, ASN1_CONTEXT(3));
812 asn1_write_OctetString(state->out, mechanism,
814 if ((creds != NULL) && (creds->data != NULL)) {
815 asn1_write_OctetString(state->out, creds->data,
818 asn1_pop_tag(state->out);
821 if (!asn1_pop_tag(state->out)) {
822 tevent_req_error(req, TLDAP_ENCODING_ERROR);
823 return tevent_req_post(req, ev);
826 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
828 if (tevent_req_nomem(subreq, req)) {
829 return tevent_req_post(req, ev);
831 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
835 static void tldap_sasl_bind_done(struct tevent_req *subreq)
837 struct tevent_req *req = tevent_req_callback_data(
838 subreq, struct tevent_req);
839 struct tldap_req_state *state = tevent_req_data(
840 req, struct tldap_req_state);
843 err = tldap_msg_recv(subreq, state, &state->result);
845 if (err != TLDAP_SUCCESS) {
846 tevent_req_error(req, err);
849 if (state->result->type != TLDAP_RES_BIND) {
850 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
853 if (!asn1_start_tag(state->result->data, state->result->type) ||
854 !tldap_decode_response(state) ||
855 !asn1_end_tag(state->result->data)) {
856 tevent_req_error(req, TLDAP_DECODING_ERROR);
860 * TODO: pull the reply blob
862 if (state->result->lderr != TLDAP_SUCCESS) {
863 tevent_req_error(req, state->result->lderr);
866 tevent_req_done(req);
869 int tldap_sasl_bind_recv(struct tevent_req *req)
873 if (tevent_req_is_ldap_error(req, &err)) {
876 return TLDAP_SUCCESS;
879 int tldap_sasl_bind(struct tldap_context *ld,
881 const char *mechanism,
883 struct tldap_control *sctrls,
885 struct tldap_control *cctrls,
888 TALLOC_CTX *frame = talloc_stackframe();
889 struct tevent_context *ev;
890 struct tevent_req *req;
893 ev = event_context_init(frame);
895 result = TLDAP_NO_MEMORY;
899 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
900 sctrls, num_sctrls, cctrls, num_cctrls);
902 result = TLDAP_NO_MEMORY;
906 if (!tevent_req_poll(req, ev)) {
907 result = TLDAP_OPERATIONS_ERROR;
911 result = tldap_sasl_bind_recv(req);
912 tldap_save_msg(ld, req);
918 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
919 struct tevent_context *ev,
920 struct tldap_context *ld,
926 if (passwd != NULL) {
927 cred.data = (uint8_t *)passwd;
928 cred.length = strlen(passwd);
930 cred.data = (uint8_t *)"";
933 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
937 int tldap_simple_bind_recv(struct tevent_req *req)
939 return tldap_sasl_bind_recv(req);
942 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
947 if (passwd != NULL) {
948 cred.data = (uint8_t *)passwd;
949 cred.length = strlen(passwd);
951 cred.data = (uint8_t *)"";
954 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
957 /*****************************************************************************/
959 /* can't use isalpha() as only a strict set is valid for LDAP */
960 #define TLDAP_IS_ALPHA(c) ((((c) >= 'a') && ((c) <= 'z')) || \
961 (((c) >= 'A') && ((c) <= 'Z')))
963 #define TLDAP_IS_ADH(c) (TLDAP_IS_ALPHA(c) || isdigit(c) || (c) == '-')
965 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
966 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
967 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
968 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
969 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
970 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
971 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
972 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
973 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
974 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
976 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
977 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
978 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
981 /* oid's should be numerical only in theory,
982 * but apparently some broken servers may have alphanum aliases instead.
983 * Do like openldap libraries and allow alphanum aliases for oids, but
984 * do not allow Tagging options in that case.
986 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
992 /* first char has stricter rules */
995 } else if (!TLDAP_IS_ALPHA(*s)) {
1000 for (i = 1; i < len; i++) {
1003 if (isdigit(s[i])) {
1016 if (TLDAP_IS_ADH(s[i])) {
1023 /* no tagging options */
1030 if ((i + 1) == len) {
1048 /* this function copies the value until the closing parenthesis is found. */
1049 static char *tldap_get_val(TALLOC_CTX *memctx,
1050 const char *value, const char **_s)
1052 const char *s = value;
1054 /* find terminator */
1057 if (s && (*(s - 1) == '\\')) {
1062 if (!s || !(*s == ')')) {
1063 /* malformed filter */
1069 return talloc_strndup(memctx, value, s - value);
1072 static int tldap_hex2char(const char *x)
1074 if (isxdigit(x[0]) && isxdigit(x[1])) {
1075 const char h1 = x[0], h2 = x[1];
1078 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1079 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1080 else if (h1 >= '0') c = h1 - (int)'0';
1082 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1083 else if (h1 >= 'A') c += h2 - (int)'A' + 10;
1084 else if (h1 >= '0') c += h2 - (int)'0';
1092 static bool tldap_find_first_star(const char *val, const char **star)
1096 for (s = val; *s; s++) {
1099 if (isxdigit(s[1]) && isxdigit(s[2])) {
1103 /* not hex based escape, check older syntax */
1112 /* invalid escape sequence */
1117 /* end of val, nothing found */
1127 /* string ended without closing parenthesis, filter is malformed */
1131 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1135 for (i = 0,p = 0; i < *val_len; i++) {
1141 /* these must be escaped */
1145 if (!value[i + 1]) {
1151 c = tldap_hex2char(&value[i]);
1152 if (c >= 0 && c < 256) {
1164 value[p] = value[i];
1173 value[p] = value[i];
1182 static bool tldap_push_filter_basic(struct tldap_context *ld,
1183 struct asn1_data *data,
1185 static bool tldap_push_filter_substring(struct tldap_context *ld,
1186 struct asn1_data *data,
1189 static bool tldap_push_filter_int(struct tldap_context *ld,
1190 struct asn1_data *data,
1193 const char *s = *_s;
1197 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1198 "Incomplete or malformed filter\n");
1203 /* we are right after a parenthesis,
1204 * find out what op we have at hand */
1207 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1208 asn1_push_tag(data, TLDAP_FILTER_AND);
1213 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1214 asn1_push_tag(data, TLDAP_FILTER_OR);
1219 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1220 asn1_push_tag(data, TLDAP_FILTER_NOT);
1222 ret = tldap_push_filter_int(ld, data, &s);
1231 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1232 "Invalid parenthesis '%c'\n", *s);
1236 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1237 "Invalid filter termination\n");
1241 ret = tldap_push_filter_basic(ld, data, &s);
1248 /* only and/or filters get here.
1249 * go through the list of filters */
1252 /* RFC 4526: empty and/or */
1258 ret = tldap_push_filter_int(ld, data, &s);
1264 /* end of list, return */
1272 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1273 "Incomplete or malformed filter\n");
1278 if (data->has_error) {
1287 static bool tldap_push_filter_basic(struct tldap_context *ld,
1288 struct asn1_data *data,
1291 TALLOC_CTX *tmpctx = talloc_tos();
1292 const char *s = *_s;
1303 bool write_octect = true;
1306 eq = strchr(s, '=');
1308 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1309 "Invalid filter, missing equal sign\n");
1318 asn1_push_tag(data, TLDAP_FILTER_LE);
1322 asn1_push_tag(data, TLDAP_FILTER_GE);
1326 asn1_push_tag(data, TLDAP_FILTER_APX);
1330 asn1_push_tag(data, TLDAP_FILTER_EXT);
1331 write_octect = false;
1337 if (*s == ':') { /* [:dn]:rule:= value */
1339 /* malformed filter */
1343 } else { /* type[:dn][:rule]:= value */
1345 dn = strchr(s, ':');
1346 type_len = dn - type;
1347 if (dn == e) { /* type:= value */
1354 rule = strchr(dn, ':');
1355 if ((rule == dn + 1) || rule + 1 == e) {
1356 /* malformed filter, contains "::" */
1360 if (StrnCaseCmp(dn, "dn:", 3) != 0) {
1365 /* malformed filter. With two
1366 * optionals, the first must be "dn"
1379 if (!type && !dn && !rule) {
1380 /* malformed filter, there must be at least one */
1385 MatchingRuleAssertion ::= SEQUENCE {
1386 matchingRule [1] MatchingRuleID OPTIONAL,
1387 type [2] AttributeDescription OPTIONAL,
1388 matchValue [3] AssertionValue,
1389 dnAttributes [4] BOOLEAN DEFAULT FALSE
1393 /* check and add rule */
1395 ret = tldap_is_attrdesc(rule, e - rule, true);
1399 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1));
1400 asn1_write(data, rule, e - rule);
1404 /* check and add type */
1406 ret = tldap_is_attrdesc(type, type_len, false);
1410 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2));
1411 asn1_write(data, type, type_len);
1415 uval = tldap_get_val(tmpctx, val, _s);
1419 uval_len = *_s - val;
1420 ret = tldap_unescape_inplace(uval, &uval_len);
1425 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3));
1426 asn1_write(data, uval, uval_len);
1429 asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4));
1430 asn1_write_uint8(data, dn?1:0);
1437 ret = tldap_is_attrdesc(s, e - s, false);
1442 if (strncmp(val, "*)", 2) == 0) {
1444 asn1_push_tag(data, TLDAP_FILTER_PRES);
1445 asn1_write(data, s, e - s);
1447 write_octect = false;
1451 ret = tldap_find_first_star(val, &star);
1457 asn1_push_tag(data, TLDAP_FILTER_SUB);
1458 asn1_write_OctetString(data, s, e - s);
1459 ret = tldap_push_filter_substring(ld, data, val, &s);
1464 write_octect = false;
1468 /* if nothing else, then it is just equality */
1469 asn1_push_tag(data, TLDAP_FILTER_EQ);
1470 write_octect = true;
1475 uval = tldap_get_val(tmpctx, val, _s);
1479 uval_len = *_s - val;
1480 ret = tldap_unescape_inplace(uval, &uval_len);
1485 asn1_write_OctetString(data, s, e - s);
1486 asn1_write_OctetString(data, uval, uval_len);
1489 if (data->has_error) {
1496 static bool tldap_push_filter_substring(struct tldap_context *ld,
1497 struct asn1_data *data,
1501 TALLOC_CTX *tmpctx = talloc_tos();
1502 bool initial = true;
1509 SubstringFilter ::= SEQUENCE {
1510 type AttributeDescription,
1511 -- at least one must be present
1512 substrings SEQUENCE OF CHOICE {
1513 initial [0] LDAPString,
1515 final [2] LDAPString } }
1517 asn1_push_tag(data, ASN1_SEQUENCE(0));
1520 ret = tldap_find_first_star(val, &star);
1524 chunk_len = star - val;
1528 if (!initial && chunk_len == 0) {
1529 /* found '**', which is illegal */
1545 if (initial && chunk_len == 0) {
1551 chunk = talloc_strndup(tmpctx, val, chunk_len);
1555 ret = tldap_unescape_inplace(chunk, &chunk_len);
1562 asn1_push_tag(data, TLDAP_SUB_INI);
1565 asn1_push_tag(data, TLDAP_SUB_ANY);
1569 asn1_push_tag(data, TLDAP_SUB_FIN);
1575 asn1_write(data, chunk, chunk_len);
1580 } while (*star == '*');
1584 /* end of sequence */
1589 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1590 * around parenthesis, we do not allow any spaces (except in values of
1591 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1592 * leading or trailing spaces where allowed.
1594 static bool tldap_push_filter(struct tldap_context *ld,
1595 struct asn1_data *data,
1598 const char *s = filter;
1601 ret = tldap_push_filter_int(ld, data, &s);
1603 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1604 "Incomplete or malformed filter\n");
1610 /*****************************************************************************/
1612 static void tldap_search_done(struct tevent_req *subreq);
1614 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1615 struct tevent_context *ev,
1616 struct tldap_context *ld,
1617 const char *base, int scope,
1622 struct tldap_control *sctrls,
1624 struct tldap_control *cctrls,
1630 struct tevent_req *req, *subreq;
1631 struct tldap_req_state *state;
1634 req = tldap_req_create(mem_ctx, ld, &state);
1639 asn1_push_tag(state->out, TLDAP_REQ_SEARCH);
1640 asn1_write_OctetString(state->out, base, strlen(base));
1641 asn1_write_enumerated(state->out, scope);
1642 asn1_write_enumerated(state->out, deref);
1643 asn1_write_Integer(state->out, sizelimit);
1644 asn1_write_Integer(state->out, timelimit);
1645 asn1_write_BOOLEAN(state->out, attrsonly);
1647 if (!tldap_push_filter(ld, state->out, filter)) {
1648 goto encoding_error;
1651 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
1652 for (i=0; i<num_attrs; i++) {
1653 asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]));
1655 asn1_pop_tag(state->out);
1656 asn1_pop_tag(state->out);
1658 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1659 sctrls, num_sctrls);
1660 if (tevent_req_nomem(subreq, req)) {
1661 return tevent_req_post(req, ev);
1663 tevent_req_set_callback(subreq, tldap_search_done, req);
1667 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1668 return tevent_req_post(req, ev);
1671 static void tldap_search_done(struct tevent_req *subreq)
1673 struct tevent_req *req = tevent_req_callback_data(
1674 subreq, struct tevent_req);
1675 struct tldap_req_state *state = tevent_req_data(
1676 req, struct tldap_req_state);
1679 err = tldap_msg_recv(subreq, state, &state->result);
1680 if (err != TLDAP_SUCCESS) {
1681 tevent_req_error(req, err);
1684 switch (state->result->type) {
1685 case TLDAP_RES_SEARCH_ENTRY:
1686 case TLDAP_RES_SEARCH_REFERENCE:
1687 tevent_req_notify_callback(req);
1688 if (!tldap_msg_set_pending(subreq)) {
1689 tevent_req_nomem(NULL, req);
1693 case TLDAP_RES_SEARCH_RESULT:
1694 TALLOC_FREE(subreq);
1695 if (!asn1_start_tag(state->result->data,
1696 state->result->type) ||
1697 !tldap_decode_response(state) ||
1698 !asn1_end_tag(state->result->data) ||
1699 !tldap_decode_controls(state)) {
1700 tevent_req_error(req, TLDAP_DECODING_ERROR);
1703 tevent_req_done(req);
1706 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1711 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1712 struct tldap_message **pmsg)
1714 struct tldap_req_state *state = tevent_req_data(
1715 req, struct tldap_req_state);
1718 if (!tevent_req_is_in_progress(req)
1719 && tevent_req_is_ldap_error(req, &err)) {
1723 if (tevent_req_is_in_progress(req)) {
1724 switch (state->result->type) {
1725 case TLDAP_RES_SEARCH_ENTRY:
1726 case TLDAP_RES_SEARCH_REFERENCE:
1729 return TLDAP_OPERATIONS_ERROR;
1733 *pmsg = talloc_move(mem_ctx, &state->result);
1734 return TLDAP_SUCCESS;
1737 struct tldap_sync_search_state {
1738 TALLOC_CTX *mem_ctx;
1739 struct tldap_message **entries;
1740 struct tldap_message **refs;
1744 static void tldap_search_cb(struct tevent_req *req)
1746 struct tldap_sync_search_state *state =
1747 (struct tldap_sync_search_state *)
1748 tevent_req_callback_data_void(req);
1749 struct tldap_message *msg, **tmp;
1750 int rc, num_entries, num_refs;
1752 rc = tldap_search_recv(req, talloc_tos(), &msg);
1753 if (rc != TLDAP_SUCCESS) {
1758 switch (tldap_msg_type(msg)) {
1759 case TLDAP_RES_SEARCH_ENTRY:
1760 num_entries = talloc_array_length(state->entries);
1761 tmp = talloc_realloc(state->mem_ctx, state->entries,
1762 struct tldap_message *, num_entries + 1);
1764 state->rc = TLDAP_NO_MEMORY;
1767 state->entries = tmp;
1768 state->entries[num_entries] = talloc_move(state->entries,
1771 case TLDAP_RES_SEARCH_REFERENCE:
1772 num_refs = talloc_array_length(state->refs);
1773 tmp = talloc_realloc(state->mem_ctx, state->refs,
1774 struct tldap_message *, num_refs + 1);
1776 state->rc = TLDAP_NO_MEMORY;
1780 state->refs[num_refs] = talloc_move(state->refs, &msg);
1782 case TLDAP_RES_SEARCH_RESULT:
1783 state->rc = TLDAP_SUCCESS;
1786 state->rc = TLDAP_PROTOCOL_ERROR;
1791 int tldap_search(struct tldap_context *ld,
1792 const char *base, int scope, const char *filter,
1793 const char **attrs, int num_attrs, int attrsonly,
1794 struct tldap_control *sctrls, int num_sctrls,
1795 struct tldap_control *cctrls, int num_cctrls,
1796 int timelimit, int sizelimit, int deref,
1797 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1798 struct tldap_message ***refs)
1800 TALLOC_CTX *frame = talloc_stackframe();
1801 struct tevent_context *ev;
1802 struct tevent_req *req;
1803 struct tldap_sync_search_state state;
1806 state.mem_ctx = mem_ctx;
1807 state.rc = TLDAP_SUCCESS;
1809 ev = event_context_init(frame);
1811 state.rc = TLDAP_NO_MEMORY;
1815 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1816 attrs, num_attrs, attrsonly,
1817 sctrls, num_sctrls, cctrls, num_cctrls,
1818 timelimit, sizelimit, deref);
1820 state.rc = TLDAP_NO_MEMORY;
1824 tevent_req_set_callback(req, tldap_search_cb, &state);
1826 if (!tevent_req_is_in_progress(req)) {
1827 /* an error happend before sending */
1828 if (tevent_req_is_ldap_error(req, &state.rc)) {
1833 while (tevent_req_is_in_progress(req)
1834 && (state.rc == TLDAP_SUCCESS)) {
1835 if (tevent_loop_once(ev) == -1) {
1836 return TLDAP_OPERATIONS_ERROR;
1840 if (state.rc != TLDAP_SUCCESS) {
1844 if (entries != NULL) {
1845 *entries = state.entries;
1847 TALLOC_FREE(state.entries);
1852 TALLOC_FREE(state.refs);
1854 tldap_save_msg(ld, req);
1860 static bool tldap_parse_search_entry(struct tldap_message *msg)
1862 int num_attribs = 0;
1864 asn1_start_tag(msg->data, msg->type);
1868 asn1_read_OctetString_talloc(msg, msg->data, &msg->dn);
1869 if (msg->dn == NULL) {
1874 * Attributes: We overallocate msg->attribs by one, so that while
1875 * looping over the attributes we can directly parse into the last
1876 * array element. Same for the values in the inner loop.
1879 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1880 if (msg->attribs == NULL) {
1884 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1885 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1886 struct tldap_attribute *attrib;
1889 attrib = &msg->attribs[num_attribs];
1890 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1891 if (attrib->values == NULL) {
1894 asn1_start_tag(msg->data, ASN1_SEQUENCE(0));
1895 asn1_read_OctetString_talloc(msg->attribs, msg->data,
1897 asn1_start_tag(msg->data, ASN1_SET);
1899 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1900 asn1_read_OctetString(msg->data, msg,
1901 &attrib->values[num_values]);
1903 attrib->values = talloc_realloc(
1904 msg->attribs, attrib->values, DATA_BLOB,
1906 if (attrib->values == NULL) {
1911 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1912 DATA_BLOB, num_values);
1913 attrib->num_values = num_values;
1915 asn1_end_tag(msg->data); /* ASN1_SET */
1916 asn1_end_tag(msg->data); /* ASN1_SEQUENCE(0) */
1917 msg->attribs = talloc_realloc(
1918 msg, msg->attribs, struct tldap_attribute,
1920 if (msg->attribs == NULL) {
1925 msg->attribs = talloc_realloc(
1926 msg, msg->attribs, struct tldap_attribute, num_attribs);
1927 asn1_end_tag(msg->data);
1928 if (msg->data->has_error) {
1934 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1936 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1943 bool tldap_entry_attributes(struct tldap_message *msg, int *num_attributes,
1944 struct tldap_attribute **attributes)
1946 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1949 *attributes = msg->attribs;
1950 *num_attributes = talloc_array_length(msg->attribs);
1954 static bool tldap_decode_controls(struct tldap_req_state *state)
1956 struct tldap_message *msg = state->result;
1957 struct asn1_data *data = msg->data;
1958 struct tldap_control *sctrls = NULL;
1959 int num_controls = 0;
1961 msg->res_sctrls = NULL;
1963 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
1967 asn1_start_tag(data, ASN1_CONTEXT(0));
1969 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
1970 struct tldap_control *c;
1973 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
1975 if (sctrls == NULL) {
1978 c = &sctrls[num_controls];
1980 asn1_start_tag(data, ASN1_SEQUENCE(0));
1981 asn1_read_OctetString_talloc(msg, data, &oid);
1982 if ((data->has_error) || (oid == NULL)) {
1986 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
1987 asn1_read_BOOLEAN(data, &c->critical);
1989 c->critical = false;
1991 c->value = data_blob_null;
1992 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
1993 !asn1_read_OctetString(data, msg, &c->value)) {
1996 asn1_end_tag(data); /* ASN1_SEQUENCE(0) */
2001 asn1_end_tag(data); /* ASN1_CONTEXT(0) */
2003 if (data->has_error) {
2004 TALLOC_FREE(sctrls);
2007 msg->res_sctrls = sctrls;
2011 static void tldap_simple_done(struct tevent_req *subreq, int type)
2013 struct tevent_req *req = tevent_req_callback_data(
2014 subreq, struct tevent_req);
2015 struct tldap_req_state *state = tevent_req_data(
2016 req, struct tldap_req_state);
2019 err = tldap_msg_recv(subreq, state, &state->result);
2020 TALLOC_FREE(subreq);
2021 if (err != TLDAP_SUCCESS) {
2022 tevent_req_error(req, err);
2025 if (state->result->type != type) {
2026 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2029 if (!asn1_start_tag(state->result->data, state->result->type) ||
2030 !tldap_decode_response(state) ||
2031 !asn1_end_tag(state->result->data) ||
2032 !tldap_decode_controls(state)) {
2033 tevent_req_error(req, TLDAP_DECODING_ERROR);
2036 if (state->result->lderr != TLDAP_SUCCESS) {
2037 tevent_req_error(req, state->result->lderr);
2040 tevent_req_done(req);
2043 static int tldap_simple_recv(struct tevent_req *req)
2046 if (tevent_req_is_ldap_error(req, &err)) {
2049 return TLDAP_SUCCESS;
2052 static void tldap_add_done(struct tevent_req *subreq);
2054 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2055 struct tevent_context *ev,
2056 struct tldap_context *ld,
2058 struct tldap_mod *attributes,
2060 struct tldap_control *sctrls,
2062 struct tldap_control *cctrls,
2065 struct tevent_req *req, *subreq;
2066 struct tldap_req_state *state;
2069 req = tldap_req_create(mem_ctx, ld, &state);
2074 asn1_push_tag(state->out, TLDAP_REQ_ADD);
2075 asn1_write_OctetString(state->out, dn, strlen(dn));
2076 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2078 for (i=0; i<num_attributes; i++) {
2079 struct tldap_mod *attrib = &attributes[i];
2080 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2081 asn1_write_OctetString(state->out, attrib->attribute,
2082 strlen(attrib->attribute));
2083 asn1_push_tag(state->out, ASN1_SET);
2084 for (j=0; j<attrib->num_values; j++) {
2085 asn1_write_OctetString(state->out,
2086 attrib->values[j].data,
2087 attrib->values[j].length);
2089 asn1_pop_tag(state->out);
2090 asn1_pop_tag(state->out);
2093 asn1_pop_tag(state->out);
2094 asn1_pop_tag(state->out);
2096 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2097 sctrls, num_sctrls);
2098 if (tevent_req_nomem(subreq, req)) {
2099 return tevent_req_post(req, ev);
2101 tevent_req_set_callback(subreq, tldap_add_done, req);
2105 static void tldap_add_done(struct tevent_req *subreq)
2107 tldap_simple_done(subreq, TLDAP_RES_ADD);
2110 int tldap_add_recv(struct tevent_req *req)
2112 return tldap_simple_recv(req);
2115 int tldap_add(struct tldap_context *ld, const char *dn,
2116 int num_attributes, struct tldap_mod *attributes,
2117 struct tldap_control *sctrls, int num_sctrls,
2118 struct tldap_control *cctrls, int num_cctrls)
2120 TALLOC_CTX *frame = talloc_stackframe();
2121 struct tevent_context *ev;
2122 struct tevent_req *req;
2125 ev = event_context_init(frame);
2127 result = TLDAP_NO_MEMORY;
2131 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2132 sctrls, num_sctrls, cctrls, num_cctrls);
2134 result = TLDAP_NO_MEMORY;
2138 if (!tevent_req_poll(req, ev)) {
2139 result = TLDAP_OPERATIONS_ERROR;
2143 result = tldap_add_recv(req);
2144 tldap_save_msg(ld, req);
2150 static void tldap_modify_done(struct tevent_req *subreq);
2152 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2153 struct tevent_context *ev,
2154 struct tldap_context *ld,
2156 int num_mods, struct tldap_mod *mods,
2157 struct tldap_control *sctrls,
2159 struct tldap_control *cctrls,
2162 struct tevent_req *req, *subreq;
2163 struct tldap_req_state *state;
2166 req = tldap_req_create(mem_ctx, ld, &state);
2171 asn1_push_tag(state->out, TLDAP_REQ_MODIFY);
2172 asn1_write_OctetString(state->out, dn, strlen(dn));
2173 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2175 for (i=0; i<num_mods; i++) {
2176 struct tldap_mod *mod = &mods[i];
2177 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2178 asn1_write_enumerated(state->out, mod->mod_op),
2179 asn1_push_tag(state->out, ASN1_SEQUENCE(0));
2180 asn1_write_OctetString(state->out, mod->attribute,
2181 strlen(mod->attribute));
2182 asn1_push_tag(state->out, ASN1_SET);
2183 for (j=0; j<mod->num_values; j++) {
2184 asn1_write_OctetString(state->out,
2185 mod->values[j].data,
2186 mod->values[j].length);
2188 asn1_pop_tag(state->out);
2189 asn1_pop_tag(state->out);
2190 asn1_pop_tag(state->out);
2193 asn1_pop_tag(state->out);
2194 asn1_pop_tag(state->out);
2196 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2197 sctrls, num_sctrls);
2198 if (tevent_req_nomem(subreq, req)) {
2199 return tevent_req_post(req, ev);
2201 tevent_req_set_callback(subreq, tldap_modify_done, req);
2205 static void tldap_modify_done(struct tevent_req *subreq)
2207 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2210 int tldap_modify_recv(struct tevent_req *req)
2212 return tldap_simple_recv(req);
2215 int tldap_modify(struct tldap_context *ld, const char *dn,
2216 int num_mods, struct tldap_mod *mods,
2217 struct tldap_control *sctrls, int num_sctrls,
2218 struct tldap_control *cctrls, int num_cctrls)
2220 TALLOC_CTX *frame = talloc_stackframe();
2221 struct tevent_context *ev;
2222 struct tevent_req *req;
2225 ev = event_context_init(frame);
2227 result = TLDAP_NO_MEMORY;
2231 req = tldap_modify_send(frame, ev, ld, dn, num_mods, mods,
2232 sctrls, num_sctrls, cctrls, num_cctrls);
2234 result = TLDAP_NO_MEMORY;
2238 if (!tevent_req_poll(req, ev)) {
2239 result = TLDAP_OPERATIONS_ERROR;
2243 result = tldap_modify_recv(req);
2244 tldap_save_msg(ld, req);
2250 static void tldap_delete_done(struct tevent_req *subreq);
2252 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2253 struct tevent_context *ev,
2254 struct tldap_context *ld,
2256 struct tldap_control *sctrls,
2258 struct tldap_control *cctrls,
2261 struct tevent_req *req, *subreq;
2262 struct tldap_req_state *state;
2264 req = tldap_req_create(mem_ctx, ld, &state);
2269 asn1_push_tag(state->out, TLDAP_REQ_DELETE);
2270 asn1_write(state->out, dn, strlen(dn));
2271 asn1_pop_tag(state->out);
2273 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2274 sctrls, num_sctrls);
2275 if (tevent_req_nomem(subreq, req)) {
2276 return tevent_req_post(req, ev);
2278 tevent_req_set_callback(subreq, tldap_delete_done, req);
2282 static void tldap_delete_done(struct tevent_req *subreq)
2284 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2287 int tldap_delete_recv(struct tevent_req *req)
2289 return tldap_simple_recv(req);
2292 int tldap_delete(struct tldap_context *ld, const char *dn,
2293 struct tldap_control *sctrls, int num_sctrls,
2294 struct tldap_control *cctrls, int num_cctrls)
2296 TALLOC_CTX *frame = talloc_stackframe();
2297 struct tevent_context *ev;
2298 struct tevent_req *req;
2301 ev = event_context_init(frame);
2303 result = TLDAP_NO_MEMORY;
2307 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2308 cctrls, num_cctrls);
2310 result = TLDAP_NO_MEMORY;
2314 if (!tevent_req_poll(req, ev)) {
2315 result = TLDAP_OPERATIONS_ERROR;
2319 result = tldap_delete_recv(req);
2320 tldap_save_msg(ld, req);
2326 int tldap_msg_id(const struct tldap_message *msg)
2331 int tldap_msg_type(const struct tldap_message *msg)
2336 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2341 return msg->res_matcheddn;
2344 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2349 return msg->res_diagnosticmessage;
2352 const char *tldap_msg_referral(struct tldap_message *msg)
2357 return msg->res_referral;
2360 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2361 struct tldap_control **sctrls)
2367 *sctrls = msg->res_sctrls;
2368 *num_sctrls = talloc_array_length(msg->res_sctrls);
2371 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2373 return ld->last_msg;
2376 const char *tldap_err2string(int rc)
2378 const char *res = NULL;
2381 * This would normally be a table, but the error codes are not fully
2382 * sequential. Let the compiler figure out the optimum implementation
2388 res = "TLDAP_SUCCESS";
2390 case TLDAP_OPERATIONS_ERROR:
2391 res = "TLDAP_OPERATIONS_ERROR";
2393 case TLDAP_PROTOCOL_ERROR:
2394 res = "TLDAP_PROTOCOL_ERROR";
2396 case TLDAP_TIMELIMIT_EXCEEDED:
2397 res = "TLDAP_TIMELIMIT_EXCEEDED";
2399 case TLDAP_SIZELIMIT_EXCEEDED:
2400 res = "TLDAP_SIZELIMIT_EXCEEDED";
2402 case TLDAP_COMPARE_FALSE:
2403 res = "TLDAP_COMPARE_FALSE";
2405 case TLDAP_COMPARE_TRUE:
2406 res = "TLDAP_COMPARE_TRUE";
2408 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2409 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2411 case TLDAP_STRONG_AUTH_REQUIRED:
2412 res = "TLDAP_STRONG_AUTH_REQUIRED";
2414 case TLDAP_REFERRAL:
2415 res = "TLDAP_REFERRAL";
2417 case TLDAP_ADMINLIMIT_EXCEEDED:
2418 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2420 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2421 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2423 case TLDAP_CONFIDENTIALITY_REQUIRED:
2424 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2426 case TLDAP_SASL_BIND_IN_PROGRESS:
2427 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2429 case TLDAP_NO_SUCH_ATTRIBUTE:
2430 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2432 case TLDAP_UNDEFINED_TYPE:
2433 res = "TLDAP_UNDEFINED_TYPE";
2435 case TLDAP_INAPPROPRIATE_MATCHING:
2436 res = "TLDAP_INAPPROPRIATE_MATCHING";
2438 case TLDAP_CONSTRAINT_VIOLATION:
2439 res = "TLDAP_CONSTRAINT_VIOLATION";
2441 case TLDAP_TYPE_OR_VALUE_EXISTS:
2442 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2444 case TLDAP_INVALID_SYNTAX:
2445 res = "TLDAP_INVALID_SYNTAX";
2447 case TLDAP_NO_SUCH_OBJECT:
2448 res = "TLDAP_NO_SUCH_OBJECT";
2450 case TLDAP_ALIAS_PROBLEM:
2451 res = "TLDAP_ALIAS_PROBLEM";
2453 case TLDAP_INVALID_DN_SYNTAX:
2454 res = "TLDAP_INVALID_DN_SYNTAX";
2457 res = "TLDAP_IS_LEAF";
2459 case TLDAP_ALIAS_DEREF_PROBLEM:
2460 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2462 case TLDAP_INAPPROPRIATE_AUTH:
2463 res = "TLDAP_INAPPROPRIATE_AUTH";
2465 case TLDAP_INVALID_CREDENTIALS:
2466 res = "TLDAP_INVALID_CREDENTIALS";
2468 case TLDAP_INSUFFICIENT_ACCESS:
2469 res = "TLDAP_INSUFFICIENT_ACCESS";
2474 case TLDAP_UNAVAILABLE:
2475 res = "TLDAP_UNAVAILABLE";
2477 case TLDAP_UNWILLING_TO_PERFORM:
2478 res = "TLDAP_UNWILLING_TO_PERFORM";
2480 case TLDAP_LOOP_DETECT:
2481 res = "TLDAP_LOOP_DETECT";
2483 case TLDAP_NAMING_VIOLATION:
2484 res = "TLDAP_NAMING_VIOLATION";
2486 case TLDAP_OBJECT_CLASS_VIOLATION:
2487 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2489 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2490 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2492 case TLDAP_NOT_ALLOWED_ON_RDN:
2493 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2495 case TLDAP_ALREADY_EXISTS:
2496 res = "TLDAP_ALREADY_EXISTS";
2498 case TLDAP_NO_OBJECT_CLASS_MODS:
2499 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2501 case TLDAP_RESULTS_TOO_LARGE:
2502 res = "TLDAP_RESULTS_TOO_LARGE";
2504 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2505 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2508 res = "TLDAP_OTHER";
2510 case TLDAP_SERVER_DOWN:
2511 res = "TLDAP_SERVER_DOWN";
2513 case TLDAP_LOCAL_ERROR:
2514 res = "TLDAP_LOCAL_ERROR";
2516 case TLDAP_ENCODING_ERROR:
2517 res = "TLDAP_ENCODING_ERROR";
2519 case TLDAP_DECODING_ERROR:
2520 res = "TLDAP_DECODING_ERROR";
2523 res = "TLDAP_TIMEOUT";
2525 case TLDAP_AUTH_UNKNOWN:
2526 res = "TLDAP_AUTH_UNKNOWN";
2528 case TLDAP_FILTER_ERROR:
2529 res = "TLDAP_FILTER_ERROR";
2531 case TLDAP_USER_CANCELLED:
2532 res = "TLDAP_USER_CANCELLED";
2534 case TLDAP_PARAM_ERROR:
2535 res = "TLDAP_PARAM_ERROR";
2537 case TLDAP_NO_MEMORY:
2538 res = "TLDAP_NO_MEMORY";
2540 case TLDAP_CONNECT_ERROR:
2541 res = "TLDAP_CONNECT_ERROR";
2543 case TLDAP_NOT_SUPPORTED:
2544 res = "TLDAP_NOT_SUPPORTED";
2546 case TLDAP_CONTROL_NOT_FOUND:
2547 res = "TLDAP_CONTROL_NOT_FOUND";
2549 case TLDAP_NO_RESULTS_RETURNED:
2550 res = "TLDAP_NO_RESULTS_RETURNED";
2552 case TLDAP_MORE_RESULTS_TO_RETURN:
2553 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2555 case TLDAP_CLIENT_LOOP:
2556 res = "TLDAP_CLIENT_LOOP";
2558 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2559 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2562 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2567 res = "Unknown LDAP Error";