2 Unix SMB/CIFS implementation.
3 Infrastructure for async ldap client requests
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "../lib/util/asn1.h"
23 #include "../lib/tsocket/tsocket.h"
24 #include "../lib/util/tevent_unix.h"
26 static int tldap_simple_recv(struct tevent_req *req);
28 bool tevent_req_is_ldap_error(struct tevent_req *req, int *perr)
30 enum tevent_req_state state;
33 if (!tevent_req_is_error(req, &state, &err)) {
37 case TEVENT_REQ_TIMED_OUT:
38 *perr = TLDAP_TIMEOUT;
40 case TEVENT_REQ_NO_MEMORY:
41 *perr = TLDAP_NO_MEMORY;
43 case TEVENT_REQ_USER_ERROR:
47 *perr = TLDAP_OPERATIONS_ERROR;
53 struct tldap_ctx_attribute {
58 struct tldap_context {
60 struct tstream_context *conn;
63 struct tevent_queue *outgoing;
64 struct tevent_req **pending;
66 /* For the sync wrappers we need something like get_last_error... */
67 struct tldap_message *last_msg;
70 void (*log_fn)(void *context, enum tldap_debug_level level,
71 const char *fmt, va_list ap);
74 struct tldap_ctx_attribute *ctx_attrs;
77 struct tldap_message {
78 struct asn1_data *data;
85 struct tldap_attribute *attribs;
87 /* Error data sent by the server */
90 char *res_diagnosticmessage;
92 struct tldap_control *res_sctrls;
94 /* Controls sent by the server */
95 struct tldap_control *ctrls;
98 void tldap_set_debug(struct tldap_context *ld,
99 void (*log_fn)(void *log_private,
100 enum tldap_debug_level level,
102 va_list ap) PRINTF_ATTRIBUTE(3,0),
106 ld->log_private = log_private;
109 static void tldap_debug(struct tldap_context *ld,
110 enum tldap_debug_level level,
111 const char *fmt, ...)
117 if (ld->log_fn == NULL) {
121 ld->log_fn(ld->log_private, level, fmt, ap);
125 static int tldap_next_msgid(struct tldap_context *ld)
129 result = ld->msgid++;
130 if (ld->msgid == 2147483647) {
136 struct tldap_context *tldap_context_create(TALLOC_CTX *mem_ctx, int fd)
138 struct tldap_context *ctx;
141 ctx = talloc_zero(mem_ctx, struct tldap_context);
145 ret = tstream_bsd_existing_socket(ctx, fd, &ctx->conn);
152 ctx->outgoing = tevent_queue_create(ctx, "tldap_outgoing");
153 if (ctx->outgoing == NULL) {
160 bool tldap_connection_ok(struct tldap_context *ld)
165 return !ld->server_down;
168 static struct tldap_ctx_attribute *tldap_context_findattr(
169 struct tldap_context *ld, const char *name)
173 num_attrs = talloc_array_length(ld->ctx_attrs);
175 for (i=0; i<num_attrs; i++) {
176 if (strcmp(ld->ctx_attrs[i].name, name) == 0) {
177 return &ld->ctx_attrs[i];
183 bool tldap_context_setattr(struct tldap_context *ld,
184 const char *name, const void *_pptr)
186 struct tldap_ctx_attribute *tmp, *attr;
189 void **pptr = (void **)discard_const_p(void,_pptr);
191 attr = tldap_context_findattr(ld, name);
194 * We don't actually delete attrs, we don't expect tons of
195 * attributes being shuffled around.
197 TALLOC_FREE(attr->ptr);
199 attr->ptr = talloc_move(ld->ctx_attrs, pptr);
205 tmpname = talloc_strdup(ld, name);
206 if (tmpname == NULL) {
210 num_attrs = talloc_array_length(ld->ctx_attrs);
212 tmp = talloc_realloc(ld, ld->ctx_attrs, struct tldap_ctx_attribute,
215 TALLOC_FREE(tmpname);
218 tmp[num_attrs].name = talloc_move(tmp, &tmpname);
220 tmp[num_attrs].ptr = talloc_move(tmp, pptr);
222 tmp[num_attrs].ptr = NULL;
229 void *tldap_context_getattr(struct tldap_context *ld, const char *name)
231 struct tldap_ctx_attribute *attr = tldap_context_findattr(ld, name);
239 struct read_ldap_state {
244 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data);
245 static void read_ldap_done(struct tevent_req *subreq);
247 static struct tevent_req *read_ldap_send(TALLOC_CTX *mem_ctx,
248 struct tevent_context *ev,
249 struct tstream_context *conn)
251 struct tevent_req *req, *subreq;
252 struct read_ldap_state *state;
254 req = tevent_req_create(mem_ctx, &state, struct read_ldap_state);
260 subreq = tstream_read_packet_send(state, ev, conn, 2, read_ldap_more,
262 if (tevent_req_nomem(subreq, req)) {
263 return tevent_req_post(req, ev);
265 tevent_req_set_callback(subreq, read_ldap_done, req);
269 static ssize_t read_ldap_more(uint8_t *buf, size_t buflen, void *private_data)
271 struct read_ldap_state *state = talloc_get_type_abort(
272 private_data, struct read_ldap_state);
277 /* We've been here, we're done */
282 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
284 if (buf[0] != 0x30) {
289 if ((len & 0x80) == 0) {
294 lensize = (len & 0x7f);
298 /* Please get us the full length */
301 if (buflen > 2 + lensize) {
305 if (buflen != 2 + lensize) {
309 for (i=0; i<lensize; i++) {
310 len = (len << 8) | buf[2+i];
315 static void read_ldap_done(struct tevent_req *subreq)
317 struct tevent_req *req = tevent_req_callback_data(
318 subreq, struct tevent_req);
319 struct read_ldap_state *state = tevent_req_data(
320 req, struct read_ldap_state);
324 nread = tstream_read_packet_recv(subreq, state, &state->buf, &err);
327 tevent_req_error(req, err);
330 tevent_req_done(req);
333 static ssize_t read_ldap_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
334 uint8_t **pbuf, int *perrno)
336 struct read_ldap_state *state = tevent_req_data(
337 req, struct read_ldap_state);
339 if (tevent_req_is_unix_error(req, perrno)) {
342 *pbuf = talloc_move(mem_ctx, &state->buf);
343 return talloc_get_size(*pbuf);
346 struct tldap_msg_state {
347 struct tldap_context *ld;
348 struct tevent_context *ev;
352 struct asn1_data *data;
356 static bool tldap_push_controls(struct asn1_data *data,
357 struct tldap_control *sctrls,
362 if ((sctrls == NULL) || (num_sctrls == 0)) {
366 if (!asn1_push_tag(data, ASN1_CONTEXT(0))) return false;
368 for (i=0; i<num_sctrls; i++) {
369 struct tldap_control *c = &sctrls[i];
370 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
371 if (!asn1_write_OctetString(data, c->oid, strlen(c->oid))) return false;
373 if (!asn1_write_BOOLEAN(data, true)) return false;
375 if (c->value.data != NULL) {
376 if (!asn1_write_OctetString(data, c->value.data,
377 c->value.length)) return false;
379 if (!asn1_pop_tag(data)) return false; /* ASN1_SEQUENCE(0) */
382 return asn1_pop_tag(data); /* ASN1_CONTEXT(0) */
385 static void tldap_msg_sent(struct tevent_req *subreq);
386 static void tldap_msg_received(struct tevent_req *subreq);
388 static struct tevent_req *tldap_msg_send(TALLOC_CTX *mem_ctx,
389 struct tevent_context *ev,
390 struct tldap_context *ld,
391 int id, struct asn1_data *data,
392 struct tldap_control *sctrls,
395 struct tevent_req *req, *subreq;
396 struct tldap_msg_state *state;
399 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_send: sending msg %d\n",
402 req = tevent_req_create(mem_ctx, &state, struct tldap_msg_state);
410 if (state->ld->server_down) {
411 tevent_req_error(req, TLDAP_SERVER_DOWN);
412 return tevent_req_post(req, ev);
415 if (!tldap_push_controls(data, sctrls, num_sctrls)) {
416 tevent_req_error(req, TLDAP_ENCODING_ERROR);
417 return tevent_req_post(req, ev);
421 if (!asn1_pop_tag(data)) {
422 tevent_req_error(req, TLDAP_ENCODING_ERROR);
423 return tevent_req_post(req, ev);
426 if (!asn1_blob(data, &blob)) {
427 tevent_req_error(req, TLDAP_ENCODING_ERROR);
428 return tevent_req_post(req, ev);
431 state->iov.iov_base = (void *)blob.data;
432 state->iov.iov_len = blob.length;
434 subreq = tstream_writev_queue_send(state, ev, ld->conn, ld->outgoing,
436 if (tevent_req_nomem(subreq, req)) {
437 return tevent_req_post(req, ev);
439 tevent_req_set_callback(subreq, tldap_msg_sent, req);
443 static void tldap_msg_unset_pending(struct tevent_req *req)
445 struct tldap_msg_state *state = tevent_req_data(
446 req, struct tldap_msg_state);
447 struct tldap_context *ld = state->ld;
448 int num_pending = talloc_array_length(ld->pending);
451 tevent_req_set_cleanup_fn(req, NULL);
453 if (num_pending == 1) {
454 TALLOC_FREE(ld->pending);
458 for (i=0; i<num_pending; i++) {
459 if (req == ld->pending[i]) {
463 if (i == num_pending) {
465 * Something's seriously broken. Just returning here is the
466 * right thing nevertheless, the point of this routine is to
467 * remove ourselves from cli->pending.
473 * Remove ourselves from the cli->pending array
475 if (num_pending > 1) {
476 ld->pending[i] = ld->pending[num_pending-1];
480 * No NULL check here, we're shrinking by sizeof(void *), and
481 * talloc_realloc just adjusts the size for this.
483 ld->pending = talloc_realloc(NULL, ld->pending, struct tevent_req *,
488 static void tldap_msg_cleanup(struct tevent_req *req,
489 enum tevent_req_state req_state)
492 case TEVENT_REQ_USER_ERROR:
493 case TEVENT_REQ_RECEIVED:
494 tldap_msg_unset_pending(req);
501 static bool tldap_msg_set_pending(struct tevent_req *req)
503 struct tldap_msg_state *state = tevent_req_data(
504 req, struct tldap_msg_state);
505 struct tldap_context *ld;
506 struct tevent_req **pending;
508 struct tevent_req *subreq;
511 num_pending = talloc_array_length(ld->pending);
513 pending = talloc_realloc(ld, ld->pending, struct tevent_req *,
515 if (pending == NULL) {
518 pending[num_pending] = req;
519 ld->pending = pending;
520 tevent_req_set_cleanup_fn(req, tldap_msg_cleanup);
522 if (num_pending > 0) {
527 * We're the first one, add the read_ldap request that waits for the
528 * answer from the server
530 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
531 if (subreq == NULL) {
532 tldap_msg_unset_pending(req);
535 tevent_req_set_callback(subreq, tldap_msg_received, ld);
539 static void tldap_msg_sent(struct tevent_req *subreq)
541 struct tevent_req *req = tevent_req_callback_data(
542 subreq, struct tevent_req);
543 struct tldap_msg_state *state = tevent_req_data(
544 req, struct tldap_msg_state);
548 nwritten = tstream_writev_queue_recv(subreq, &err);
550 if (nwritten == -1) {
551 state->ld->server_down = true;
552 tevent_req_error(req, TLDAP_SERVER_DOWN);
556 if (!tldap_msg_set_pending(req)) {
562 static int tldap_msg_msgid(struct tevent_req *req)
564 struct tldap_msg_state *state = tevent_req_data(
565 req, struct tldap_msg_state);
570 static void tldap_msg_received(struct tevent_req *subreq)
572 struct tldap_context *ld = tevent_req_callback_data(
573 subreq, struct tldap_context);
574 struct tevent_req *req;
575 struct tldap_msg_state *state;
576 struct asn1_data *data;
585 received = read_ldap_recv(subreq, talloc_tos(), &inbuf, &err);
587 if (received == -1) {
588 status = TLDAP_SERVER_DOWN;
592 data = asn1_init(talloc_tos());
594 status = TLDAP_NO_MEMORY;
597 asn1_load_nocopy(data, inbuf, received);
600 ok &= asn1_start_tag(data, ASN1_SEQUENCE(0));
601 ok &= asn1_read_Integer(data, &id);
602 ok &= asn1_peek_uint8(data, &type);
605 status = TLDAP_PROTOCOL_ERROR;
609 tldap_debug(ld, TLDAP_DEBUG_TRACE, "tldap_msg_received: got msg %d "
610 "type %d\n", id, (int)type);
612 num_pending = talloc_array_length(ld->pending);
614 for (i=0; i<num_pending; i++) {
615 if (id == tldap_msg_msgid(ld->pending[i])) {
619 if (i == num_pending) {
620 /* Dump unexpected reply */
621 tldap_debug(ld, TLDAP_DEBUG_WARNING, "tldap_msg_received: "
622 "No request pending for msg %d\n", id);
628 req = ld->pending[i];
629 state = tevent_req_data(req, struct tldap_msg_state);
631 state->inbuf = talloc_move(state, &inbuf);
632 state->data = talloc_move(state, &data);
634 tldap_msg_unset_pending(req);
635 num_pending = talloc_array_length(ld->pending);
637 tevent_req_done(req);
640 if (num_pending == 0) {
643 if (talloc_array_length(ld->pending) > num_pending) {
645 * The callback functions called from tevent_req_done() above
646 * have put something on the pending queue. We don't have to
647 * trigger the read_ldap_send(), tldap_msg_set_pending() has
648 * done it for us already.
653 state = tevent_req_data(ld->pending[0], struct tldap_msg_state);
654 subreq = read_ldap_send(ld->pending, state->ev, ld->conn);
655 if (subreq == NULL) {
656 status = TLDAP_NO_MEMORY;
659 tevent_req_set_callback(subreq, tldap_msg_received, ld);
663 while (talloc_array_length(ld->pending) > 0) {
664 req = ld->pending[0];
665 state = tevent_req_data(req, struct tldap_msg_state);
666 tevent_req_defer_callback(req, state->ev);
667 tevent_req_error(req, status);
671 static int tldap_msg_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
672 struct tldap_message **pmsg)
674 struct tldap_msg_state *state = tevent_req_data(
675 req, struct tldap_msg_state);
676 struct tldap_message *msg;
680 if (tevent_req_is_ldap_error(req, &err)) {
684 if (!asn1_peek_uint8(state->data, &msgtype)) {
685 return TLDAP_PROTOCOL_ERROR;
689 return TLDAP_SUCCESS;
692 msg = talloc_zero(mem_ctx, struct tldap_message);
694 return TLDAP_NO_MEMORY;
698 msg->inbuf = talloc_move(msg, &state->inbuf);
699 msg->data = talloc_move(msg, &state->data);
703 return TLDAP_SUCCESS;
706 struct tldap_req_state {
708 struct asn1_data *out;
709 struct tldap_message *result;
712 static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx,
713 struct tldap_context *ld,
714 struct tldap_req_state **pstate)
716 struct tevent_req *req;
717 struct tldap_req_state *state;
719 req = tevent_req_create(mem_ctx, &state, struct tldap_req_state);
724 state->out = asn1_init(state);
725 if (state->out == NULL) {
728 state->result = NULL;
729 state->id = tldap_next_msgid(ld);
731 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
732 if (!asn1_write_Integer(state->out, state->id)) goto err;
743 static void tldap_save_msg(struct tldap_context *ld, struct tevent_req *req)
745 struct tldap_req_state *state = tevent_req_data(
746 req, struct tldap_req_state);
748 TALLOC_FREE(ld->last_msg);
749 ld->last_msg = talloc_move(ld, &state->result);
752 static char *blob2string_talloc(TALLOC_CTX *mem_ctx, DATA_BLOB blob)
754 char *result = talloc_array(mem_ctx, char, blob.length+1);
756 if (result == NULL) {
760 memcpy(result, blob.data, blob.length);
761 result[blob.length] = '\0';
765 static bool asn1_read_OctetString_talloc(TALLOC_CTX *mem_ctx,
766 struct asn1_data *data,
771 if (!asn1_read_OctetString(data, mem_ctx, &string))
774 result = blob2string_talloc(mem_ctx, string);
776 data_blob_free(&string);
778 if (result == NULL) {
785 static bool tldap_decode_controls(struct tldap_req_state *state);
787 static bool tldap_decode_response(struct tldap_req_state *state)
789 struct asn1_data *data = state->result->data;
790 struct tldap_message *msg = state->result;
793 ok &= asn1_read_enumerated(data, &msg->lderr);
794 ok &= asn1_read_OctetString_talloc(msg, data, &msg->res_matcheddn);
795 ok &= asn1_read_OctetString_talloc(msg, data,
796 &msg->res_diagnosticmessage);
798 if (asn1_peek_tag(data, ASN1_CONTEXT(3))) {
799 ok &= asn1_start_tag(data, ASN1_CONTEXT(3));
800 ok &= asn1_read_OctetString_talloc(msg, data,
802 ok &= asn1_end_tag(data);
804 msg->res_referral = NULL;
810 static void tldap_sasl_bind_done(struct tevent_req *subreq);
812 struct tevent_req *tldap_sasl_bind_send(TALLOC_CTX *mem_ctx,
813 struct tevent_context *ev,
814 struct tldap_context *ld,
816 const char *mechanism,
818 struct tldap_control *sctrls,
820 struct tldap_control *cctrls,
823 struct tevent_req *req, *subreq;
824 struct tldap_req_state *state;
826 req = tldap_req_create(mem_ctx, ld, &state);
835 if (!asn1_push_tag(state->out, TLDAP_REQ_BIND)) goto err;
836 if (!asn1_write_Integer(state->out, ld->ld_version)) goto err;
837 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
839 if (mechanism == NULL) {
840 if (!asn1_push_tag(state->out, ASN1_CONTEXT_SIMPLE(0))) goto err;
841 if (!asn1_write(state->out, creds->data, creds->length)) goto err;
842 if (!asn1_pop_tag(state->out)) goto err;
844 if (!asn1_push_tag(state->out, ASN1_CONTEXT(3))) goto err;
845 if (!asn1_write_OctetString(state->out, mechanism,
846 strlen(mechanism))) goto err;
847 if ((creds != NULL) && (creds->data != NULL)) {
848 if (!asn1_write_OctetString(state->out, creds->data,
849 creds->length)) goto err;
851 if (!asn1_pop_tag(state->out)) goto err;
854 if (!asn1_pop_tag(state->out)) goto err;
856 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
858 if (tevent_req_nomem(subreq, req)) {
859 return tevent_req_post(req, ev);
861 tevent_req_set_callback(subreq, tldap_sasl_bind_done, req);
866 tevent_req_error(req, TLDAP_ENCODING_ERROR);
867 return tevent_req_post(req, ev);
870 static void tldap_sasl_bind_done(struct tevent_req *subreq)
872 struct tevent_req *req = tevent_req_callback_data(
873 subreq, struct tevent_req);
874 struct tldap_req_state *state = tevent_req_data(
875 req, struct tldap_req_state);
878 err = tldap_msg_recv(subreq, state, &state->result);
880 if (err != TLDAP_SUCCESS) {
881 tevent_req_error(req, err);
884 if (state->result->type != TLDAP_RES_BIND) {
885 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
888 if (!asn1_start_tag(state->result->data, state->result->type) ||
889 !tldap_decode_response(state) ||
890 !asn1_end_tag(state->result->data)) {
891 tevent_req_error(req, TLDAP_DECODING_ERROR);
895 * TODO: pull the reply blob
897 if (state->result->lderr != TLDAP_SUCCESS) {
898 tevent_req_error(req, state->result->lderr);
901 tevent_req_done(req);
904 int tldap_sasl_bind_recv(struct tevent_req *req)
906 return tldap_simple_recv(req);
909 int tldap_sasl_bind(struct tldap_context *ld,
911 const char *mechanism,
913 struct tldap_control *sctrls,
915 struct tldap_control *cctrls,
918 TALLOC_CTX *frame = talloc_stackframe();
919 struct tevent_context *ev;
920 struct tevent_req *req;
923 ev = samba_tevent_context_init(frame);
925 result = TLDAP_NO_MEMORY;
929 req = tldap_sasl_bind_send(frame, ev, ld, dn, mechanism, creds,
930 sctrls, num_sctrls, cctrls, num_cctrls);
932 result = TLDAP_NO_MEMORY;
936 if (!tevent_req_poll(req, ev)) {
937 result = TLDAP_OPERATIONS_ERROR;
941 result = tldap_sasl_bind_recv(req);
942 tldap_save_msg(ld, req);
948 struct tevent_req *tldap_simple_bind_send(TALLOC_CTX *mem_ctx,
949 struct tevent_context *ev,
950 struct tldap_context *ld,
956 if (passwd != NULL) {
957 cred.data = discard_const_p(uint8_t, passwd);
958 cred.length = strlen(passwd);
960 cred.data = discard_const_p(uint8_t, "");
963 return tldap_sasl_bind_send(mem_ctx, ev, ld, dn, NULL, &cred, NULL, 0,
967 int tldap_simple_bind_recv(struct tevent_req *req)
969 return tldap_sasl_bind_recv(req);
972 int tldap_simple_bind(struct tldap_context *ld, const char *dn,
977 if (passwd != NULL) {
978 cred.data = discard_const_p(uint8_t, passwd);
979 cred.length = strlen(passwd);
981 cred.data = discard_const_p(uint8_t, "");
984 return tldap_sasl_bind(ld, dn, NULL, &cred, NULL, 0, NULL, 0);
987 /*****************************************************************************/
989 /* can't use isalpha() as only a strict set is valid for LDAP */
991 static bool tldap_is_alpha(char c)
993 return (((c >= 'a') && (c <= 'z')) || \
994 ((c >= 'A') && (c <= 'Z')));
997 static bool tldap_is_adh(char c)
999 return tldap_is_alpha(c) || isdigit(c) || (c == '-');
1002 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
1003 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
1004 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
1005 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
1006 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
1007 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
1008 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
1009 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
1010 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
1011 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
1013 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
1014 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
1015 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1018 /* oid's should be numerical only in theory,
1019 * but apparently some broken servers may have alphanum aliases instead.
1020 * Do like openldap libraries and allow alphanum aliases for oids, but
1021 * do not allow Tagging options in that case.
1023 static bool tldap_is_attrdesc(const char *s, int len, bool no_tagopts)
1025 bool is_oid = false;
1029 /* first char has stricter rules */
1032 } else if (!tldap_is_alpha(*s)) {
1033 /* bad first char */
1037 for (i = 1; i < len; i++) {
1040 if (isdigit(s[i])) {
1053 if (tldap_is_adh(s[i])) {
1060 /* no tagging options */
1067 if ((i + 1) == len) {
1085 /* this function copies the value until the closing parenthesis is found. */
1086 static char *tldap_get_val(TALLOC_CTX *memctx,
1087 const char *value, const char **_s)
1089 const char *s = value;
1091 /* find terminator */
1094 if (s && (*(s - 1) == '\\')) {
1099 if (!s || !(*s == ')')) {
1100 /* malformed filter */
1106 return talloc_strndup(memctx, value, s - value);
1109 static int tldap_hex2char(const char *x)
1111 if (isxdigit(x[0]) && isxdigit(x[1])) {
1112 const char h1 = x[0], h2 = x[1];
1115 if (h1 >= 'a') c = h1 - (int)'a' + 10;
1116 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
1117 else if (h1 >= '0') c = h1 - (int)'0';
1119 if (h2 >= 'a') c += h2 - (int)'a' + 10;
1120 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
1121 else if (h2 >= '0') c += h2 - (int)'0';
1129 static bool tldap_find_first_star(const char *val, const char **star)
1133 for (s = val; *s; s++) {
1136 if (isxdigit(s[1]) && isxdigit(s[2])) {
1140 /* not hex based escape, check older syntax */
1149 /* invalid escape sequence */
1154 /* end of val, nothing found */
1164 /* string ended without closing parenthesis, filter is malformed */
1168 static bool tldap_unescape_inplace(char *value, size_t *val_len)
1172 for (i = 0,p = 0; i < *val_len; i++) {
1178 /* these must be escaped */
1182 if (!value[i + 1]) {
1188 c = tldap_hex2char(&value[i]);
1189 if (c >= 0 && c < 256) {
1201 value[p] = value[i];
1210 value[p] = value[i];
1219 static bool tldap_push_filter_basic(struct tldap_context *ld,
1220 struct asn1_data *data,
1222 static bool tldap_push_filter_substring(struct tldap_context *ld,
1223 struct asn1_data *data,
1226 static bool tldap_push_filter_int(struct tldap_context *ld,
1227 struct asn1_data *data,
1230 const char *s = *_s;
1234 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1235 "Incomplete or malformed filter\n");
1240 /* we are right after a parenthesis,
1241 * find out what op we have at hand */
1244 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: AND\n");
1245 if (!asn1_push_tag(data, TLDAP_FILTER_AND)) return false;
1250 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: OR\n");
1251 if (!asn1_push_tag(data, TLDAP_FILTER_OR)) return false;
1256 tldap_debug(ld, TLDAP_DEBUG_TRACE, "Filter op: NOT\n");
1257 if (!asn1_push_tag(data, TLDAP_FILTER_NOT)) return false;
1259 ret = tldap_push_filter_int(ld, data, &s);
1263 if (!asn1_pop_tag(data)) return false;
1268 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1269 "Invalid parenthesis '%c'\n", *s);
1273 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1274 "Invalid filter termination\n");
1278 ret = tldap_push_filter_basic(ld, data, &s);
1285 /* only and/or filters get here.
1286 * go through the list of filters */
1289 /* RFC 4526: empty and/or */
1290 if (!asn1_pop_tag(data)) return false;
1295 ret = tldap_push_filter_int(ld, data, &s);
1301 /* end of list, return */
1302 if (!asn1_pop_tag(data)) return false;
1309 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1310 "Incomplete or malformed filter\n");
1315 if (asn1_has_error(data)) {
1324 static bool tldap_push_filter_basic(struct tldap_context *ld,
1325 struct asn1_data *data,
1328 TALLOC_CTX *tmpctx = talloc_tos();
1329 const char *s = *_s;
1337 size_t type_len = 0;
1340 bool write_octect = true;
1343 eq = strchr(s, '=');
1345 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1346 "Invalid filter, missing equal sign\n");
1355 if (!asn1_push_tag(data, TLDAP_FILTER_LE)) return false;
1359 if (!asn1_push_tag(data, TLDAP_FILTER_GE)) return false;
1363 if (!asn1_push_tag(data, TLDAP_FILTER_APX)) return false;
1367 if (!asn1_push_tag(data, TLDAP_FILTER_EXT)) return false;
1368 write_octect = false;
1374 if (*s == ':') { /* [:dn]:rule:= value */
1376 /* malformed filter */
1380 } else { /* type[:dn][:rule]:= value */
1382 dn = strchr(s, ':');
1383 type_len = dn - type;
1384 if (dn == e) { /* type:= value */
1391 rule = strchr(dn, ':');
1395 if ((rule == dn + 1) || rule + 1 == e) {
1396 /* malformed filter, contains "::" */
1400 if (strncasecmp_m(dn, "dn:", 3) != 0) {
1405 /* malformed filter. With two
1406 * optionals, the first must be "dn"
1419 if (!type && !dn && !rule) {
1420 /* malformed filter, there must be at least one */
1425 MatchingRuleAssertion ::= SEQUENCE {
1426 matchingRule [1] MatchingRuleID OPTIONAL,
1427 type [2] AttributeDescription OPTIONAL,
1428 matchValue [3] AssertionValue,
1429 dnAttributes [4] BOOLEAN DEFAULT FALSE
1433 /* check and add rule */
1435 ret = tldap_is_attrdesc(rule, e - rule, true);
1439 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(1))) return false;
1440 if (!asn1_write(data, rule, e - rule)) return false;
1441 if (!asn1_pop_tag(data)) return false;
1444 /* check and add type */
1446 ret = tldap_is_attrdesc(type, type_len, false);
1450 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(2))) return false;
1451 if (!asn1_write(data, type, type_len)) return false;
1452 if (!asn1_pop_tag(data)) return false;
1455 uval = tldap_get_val(tmpctx, val, _s);
1459 uval_len = *_s - val;
1460 ret = tldap_unescape_inplace(uval, &uval_len);
1465 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(3))) return false;
1466 if (!asn1_write(data, uval, uval_len)) return false;
1467 if (!asn1_pop_tag(data)) return false;
1469 if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(4))) return false;
1470 if (!asn1_write_uint8(data, dn?1:0)) return false;
1471 if (!asn1_pop_tag(data)) return false;
1477 ret = tldap_is_attrdesc(s, e - s, false);
1482 if (strncmp(val, "*)", 2) == 0) {
1484 if (!asn1_push_tag(data, TLDAP_FILTER_PRES)) return false;
1485 if (!asn1_write(data, s, e - s)) return false;
1487 write_octect = false;
1491 ret = tldap_find_first_star(val, &star);
1497 if (!asn1_push_tag(data, TLDAP_FILTER_SUB)) return false;
1498 if (!asn1_write_OctetString(data, s, e - s)) return false;
1499 ret = tldap_push_filter_substring(ld, data, val, &s);
1504 write_octect = false;
1508 /* if nothing else, then it is just equality */
1509 if (!asn1_push_tag(data, TLDAP_FILTER_EQ)) return false;
1510 write_octect = true;
1515 uval = tldap_get_val(tmpctx, val, _s);
1519 uval_len = *_s - val;
1520 ret = tldap_unescape_inplace(uval, &uval_len);
1525 if (!asn1_write_OctetString(data, s, e - s)) return false;
1526 if (!asn1_write_OctetString(data, uval, uval_len)) return false;
1529 if (asn1_has_error(data)) {
1532 return asn1_pop_tag(data);
1535 static bool tldap_push_filter_substring(struct tldap_context *ld,
1536 struct asn1_data *data,
1540 TALLOC_CTX *tmpctx = talloc_tos();
1541 bool initial = true;
1548 SubstringFilter ::= SEQUENCE {
1549 type AttributeDescription,
1550 -- at least one must be present
1551 substrings SEQUENCE OF CHOICE {
1552 initial [0] LDAPString,
1554 final [2] LDAPString } }
1556 if (!asn1_push_tag(data, ASN1_SEQUENCE(0))) return false;
1559 ret = tldap_find_first_star(val, &star);
1563 chunk_len = star - val;
1567 if (!initial && chunk_len == 0) {
1568 /* found '**', which is illegal */
1584 if (initial && chunk_len == 0) {
1590 chunk = talloc_strndup(tmpctx, val, chunk_len);
1594 ret = tldap_unescape_inplace(chunk, &chunk_len);
1601 if (!asn1_push_tag(data, TLDAP_SUB_INI)) return false;
1604 if (!asn1_push_tag(data, TLDAP_SUB_ANY)) return false;
1608 if (!asn1_push_tag(data, TLDAP_SUB_FIN)) return false;
1614 if (!asn1_write(data, chunk, chunk_len)) return false;
1615 if (!asn1_pop_tag(data)) return false;
1619 } while (*star == '*');
1623 /* end of sequence */
1624 return asn1_pop_tag(data);
1627 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1628 * around parenthesis, we do not allow any spaces (except in values of
1629 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1630 * leading or trailing spaces where allowed.
1632 static bool tldap_push_filter(struct tldap_context *ld,
1633 struct asn1_data *data,
1636 const char *s = filter;
1639 ret = tldap_push_filter_int(ld, data, &s);
1641 tldap_debug(ld, TLDAP_DEBUG_ERROR,
1642 "Incomplete or malformed filter\n");
1648 /*****************************************************************************/
1650 static void tldap_search_done(struct tevent_req *subreq);
1652 struct tevent_req *tldap_search_send(TALLOC_CTX *mem_ctx,
1653 struct tevent_context *ev,
1654 struct tldap_context *ld,
1655 const char *base, int scope,
1660 struct tldap_control *sctrls,
1662 struct tldap_control *cctrls,
1668 struct tevent_req *req, *subreq;
1669 struct tldap_req_state *state;
1672 req = tldap_req_create(mem_ctx, ld, &state);
1677 if (!asn1_push_tag(state->out, TLDAP_REQ_SEARCH)) goto encoding_error;
1678 if (!asn1_write_OctetString(state->out, base, strlen(base))) goto encoding_error;
1679 if (!asn1_write_enumerated(state->out, scope)) goto encoding_error;
1680 if (!asn1_write_enumerated(state->out, deref)) goto encoding_error;
1681 if (!asn1_write_Integer(state->out, sizelimit)) goto encoding_error;
1682 if (!asn1_write_Integer(state->out, timelimit)) goto encoding_error;
1683 if (!asn1_write_BOOLEAN(state->out, attrsonly)) goto encoding_error;
1685 if (!tldap_push_filter(ld, state->out, filter)) {
1686 goto encoding_error;
1689 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto encoding_error;
1690 for (i=0; i<num_attrs; i++) {
1691 if (!asn1_write_OctetString(state->out, attrs[i], strlen(attrs[i]))) goto encoding_error;
1693 if (!asn1_pop_tag(state->out)) goto encoding_error;
1694 if (!asn1_pop_tag(state->out)) goto encoding_error;
1696 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
1697 sctrls, num_sctrls);
1698 if (tevent_req_nomem(subreq, req)) {
1699 return tevent_req_post(req, ev);
1701 tevent_req_set_callback(subreq, tldap_search_done, req);
1705 tevent_req_error(req, TLDAP_ENCODING_ERROR);
1706 return tevent_req_post(req, ev);
1709 static void tldap_search_done(struct tevent_req *subreq)
1711 struct tevent_req *req = tevent_req_callback_data(
1712 subreq, struct tevent_req);
1713 struct tldap_req_state *state = tevent_req_data(
1714 req, struct tldap_req_state);
1717 err = tldap_msg_recv(subreq, state, &state->result);
1718 if (err != TLDAP_SUCCESS) {
1719 tevent_req_error(req, err);
1722 switch (state->result->type) {
1723 case TLDAP_RES_SEARCH_ENTRY:
1724 case TLDAP_RES_SEARCH_REFERENCE:
1725 if (!tldap_msg_set_pending(subreq)) {
1726 tevent_req_oom(req);
1729 tevent_req_notify_callback(req);
1731 case TLDAP_RES_SEARCH_RESULT:
1732 TALLOC_FREE(subreq);
1733 if (!asn1_start_tag(state->result->data,
1734 state->result->type) ||
1735 !tldap_decode_response(state) ||
1736 !asn1_end_tag(state->result->data) ||
1737 !tldap_decode_controls(state)) {
1738 tevent_req_error(req, TLDAP_DECODING_ERROR);
1741 tevent_req_done(req);
1744 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
1749 int tldap_search_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
1750 struct tldap_message **pmsg)
1752 struct tldap_req_state *state = tevent_req_data(
1753 req, struct tldap_req_state);
1756 if (!tevent_req_is_in_progress(req)
1757 && tevent_req_is_ldap_error(req, &err)) {
1761 if (tevent_req_is_in_progress(req)) {
1762 switch (state->result->type) {
1763 case TLDAP_RES_SEARCH_ENTRY:
1764 case TLDAP_RES_SEARCH_REFERENCE:
1767 return TLDAP_OPERATIONS_ERROR;
1771 *pmsg = talloc_move(mem_ctx, &state->result);
1772 return TLDAP_SUCCESS;
1775 struct tldap_sync_search_state {
1776 TALLOC_CTX *mem_ctx;
1777 struct tldap_message **entries;
1778 struct tldap_message **refs;
1782 static void tldap_search_cb(struct tevent_req *req)
1784 struct tldap_sync_search_state *state =
1785 (struct tldap_sync_search_state *)
1786 tevent_req_callback_data_void(req);
1787 struct tldap_message *msg, **tmp;
1788 int rc, num_entries, num_refs;
1790 rc = tldap_search_recv(req, talloc_tos(), &msg);
1791 if (rc != TLDAP_SUCCESS) {
1796 switch (tldap_msg_type(msg)) {
1797 case TLDAP_RES_SEARCH_ENTRY:
1798 num_entries = talloc_array_length(state->entries);
1799 tmp = talloc_realloc(state->mem_ctx, state->entries,
1800 struct tldap_message *, num_entries + 1);
1802 state->rc = TLDAP_NO_MEMORY;
1805 state->entries = tmp;
1806 state->entries[num_entries] = talloc_move(state->entries,
1809 case TLDAP_RES_SEARCH_REFERENCE:
1810 num_refs = talloc_array_length(state->refs);
1811 tmp = talloc_realloc(state->mem_ctx, state->refs,
1812 struct tldap_message *, num_refs + 1);
1814 state->rc = TLDAP_NO_MEMORY;
1818 state->refs[num_refs] = talloc_move(state->refs, &msg);
1820 case TLDAP_RES_SEARCH_RESULT:
1821 state->rc = TLDAP_SUCCESS;
1824 state->rc = TLDAP_PROTOCOL_ERROR;
1829 int tldap_search(struct tldap_context *ld,
1830 const char *base, int scope, const char *filter,
1831 const char **attrs, int num_attrs, int attrsonly,
1832 struct tldap_control *sctrls, int num_sctrls,
1833 struct tldap_control *cctrls, int num_cctrls,
1834 int timelimit, int sizelimit, int deref,
1835 TALLOC_CTX *mem_ctx, struct tldap_message ***entries,
1836 struct tldap_message ***refs)
1838 TALLOC_CTX *frame = talloc_stackframe();
1839 struct tevent_context *ev;
1840 struct tevent_req *req;
1841 struct tldap_sync_search_state state;
1844 state.mem_ctx = mem_ctx;
1845 state.rc = TLDAP_SUCCESS;
1847 ev = samba_tevent_context_init(frame);
1849 state.rc = TLDAP_NO_MEMORY;
1853 req = tldap_search_send(frame, ev, ld, base, scope, filter,
1854 attrs, num_attrs, attrsonly,
1855 sctrls, num_sctrls, cctrls, num_cctrls,
1856 timelimit, sizelimit, deref);
1858 state.rc = TLDAP_NO_MEMORY;
1862 tevent_req_set_callback(req, tldap_search_cb, &state);
1864 if (!tevent_req_is_in_progress(req)) {
1865 /* an error happend before sending */
1866 if (tevent_req_is_ldap_error(req, &state.rc)) {
1871 while (tevent_req_is_in_progress(req)
1872 && (state.rc == TLDAP_SUCCESS)) {
1873 if (tevent_loop_once(ev) == -1) {
1874 return TLDAP_OPERATIONS_ERROR;
1878 if (state.rc != TLDAP_SUCCESS) {
1882 if (entries != NULL) {
1883 *entries = state.entries;
1885 TALLOC_FREE(state.entries);
1890 TALLOC_FREE(state.refs);
1892 tldap_save_msg(ld, req);
1898 static bool tldap_parse_search_entry(struct tldap_message *msg)
1900 int num_attribs = 0;
1902 if (!asn1_start_tag(msg->data, msg->type)) return false;
1906 if (!asn1_read_OctetString_talloc(msg, msg->data, &msg->dn)) return false;
1908 if (msg->dn == NULL) {
1913 * Attributes: We overallocate msg->attribs by one, so that while
1914 * looping over the attributes we can directly parse into the last
1915 * array element. Same for the values in the inner loop.
1918 msg->attribs = talloc_array(msg, struct tldap_attribute, 1);
1919 if (msg->attribs == NULL) {
1923 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
1924 while (asn1_peek_tag(msg->data, ASN1_SEQUENCE(0))) {
1925 struct tldap_attribute *attrib;
1928 attrib = &msg->attribs[num_attribs];
1929 attrib->values = talloc_array(msg->attribs, DATA_BLOB, 1);
1930 if (attrib->values == NULL) {
1933 if (!asn1_start_tag(msg->data, ASN1_SEQUENCE(0))) return false;
1934 if (!asn1_read_OctetString_talloc(msg->attribs, msg->data,
1935 &attrib->name)) return false;
1936 if (!asn1_start_tag(msg->data, ASN1_SET)) return false;
1938 while (asn1_peek_tag(msg->data, ASN1_OCTET_STRING)) {
1939 if (!asn1_read_OctetString(msg->data, msg,
1940 &attrib->values[num_values])) return false;
1942 attrib->values = talloc_realloc(
1943 msg->attribs, attrib->values, DATA_BLOB,
1945 if (attrib->values == NULL) {
1950 attrib->values = talloc_realloc(msg->attribs, attrib->values,
1951 DATA_BLOB, num_values);
1952 attrib->num_values = num_values;
1954 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SET */
1955 if (!asn1_end_tag(msg->data)) return false; /* ASN1_SEQUENCE(0) */
1956 msg->attribs = talloc_realloc(
1957 msg, msg->attribs, struct tldap_attribute,
1959 if (msg->attribs == NULL) {
1964 msg->attribs = talloc_realloc(
1965 msg, msg->attribs, struct tldap_attribute, num_attribs);
1966 return asn1_end_tag(msg->data);
1969 bool tldap_entry_dn(struct tldap_message *msg, char **dn)
1971 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1978 bool tldap_entry_attributes(struct tldap_message *msg,
1979 struct tldap_attribute **attributes,
1980 int *num_attributes)
1982 if ((msg->dn == NULL) && (!tldap_parse_search_entry(msg))) {
1985 *attributes = msg->attribs;
1986 *num_attributes = talloc_array_length(msg->attribs);
1990 static bool tldap_decode_controls(struct tldap_req_state *state)
1992 struct tldap_message *msg = state->result;
1993 struct asn1_data *data = msg->data;
1994 struct tldap_control *sctrls = NULL;
1995 int num_controls = 0;
1998 msg->res_sctrls = NULL;
2000 if (!asn1_peek_tag(data, ASN1_CONTEXT(0))) {
2004 if (!asn1_start_tag(data, ASN1_CONTEXT(0))) goto out;
2006 while (asn1_peek_tag(data, ASN1_SEQUENCE(0))) {
2007 struct tldap_control *c;
2010 sctrls = talloc_realloc(msg, sctrls, struct tldap_control,
2012 if (sctrls == NULL) {
2015 c = &sctrls[num_controls];
2017 if (!asn1_start_tag(data, ASN1_SEQUENCE(0))) goto out;
2018 if (!asn1_read_OctetString_talloc(msg, data, &oid)) goto out;
2019 if (asn1_has_error(data) || (oid == NULL)) {
2023 if (asn1_peek_tag(data, ASN1_BOOLEAN)) {
2024 if (!asn1_read_BOOLEAN(data, &c->critical)) goto out;
2026 c->critical = false;
2028 c->value = data_blob_null;
2029 if (asn1_peek_tag(data, ASN1_OCTET_STRING) &&
2030 !asn1_read_OctetString(data, msg, &c->value)) {
2033 if (!asn1_end_tag(data)) goto out; /* ASN1_SEQUENCE(0) */
2038 if (!asn1_end_tag(data)) goto out; /* ASN1_CONTEXT(0) */
2045 TALLOC_FREE(sctrls);
2047 msg->res_sctrls = sctrls;
2052 static void tldap_simple_done(struct tevent_req *subreq, int type)
2054 struct tevent_req *req = tevent_req_callback_data(
2055 subreq, struct tevent_req);
2056 struct tldap_req_state *state = tevent_req_data(
2057 req, struct tldap_req_state);
2060 err = tldap_msg_recv(subreq, state, &state->result);
2061 TALLOC_FREE(subreq);
2062 if (err != TLDAP_SUCCESS) {
2063 tevent_req_error(req, err);
2066 if (state->result->type != type) {
2067 tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
2070 if (!asn1_start_tag(state->result->data, state->result->type) ||
2071 !tldap_decode_response(state) ||
2072 !asn1_end_tag(state->result->data) ||
2073 !tldap_decode_controls(state)) {
2074 tevent_req_error(req, TLDAP_DECODING_ERROR);
2077 if (state->result->lderr != TLDAP_SUCCESS) {
2078 tevent_req_error(req, state->result->lderr);
2081 tevent_req_done(req);
2084 static int tldap_simple_recv(struct tevent_req *req)
2087 if (tevent_req_is_ldap_error(req, &err)) {
2090 return TLDAP_SUCCESS;
2093 static void tldap_add_done(struct tevent_req *subreq);
2095 struct tevent_req *tldap_add_send(TALLOC_CTX *mem_ctx,
2096 struct tevent_context *ev,
2097 struct tldap_context *ld,
2099 struct tldap_mod *attributes,
2101 struct tldap_control *sctrls,
2103 struct tldap_control *cctrls,
2106 struct tevent_req *req, *subreq;
2107 struct tldap_req_state *state;
2110 req = tldap_req_create(mem_ctx, ld, &state);
2115 if (!asn1_push_tag(state->out, TLDAP_REQ_ADD)) goto err;
2116 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2117 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2119 for (i=0; i<num_attributes; i++) {
2120 struct tldap_mod *attrib = &attributes[i];
2121 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2122 if (!asn1_write_OctetString(state->out, attrib->attribute,
2123 strlen(attrib->attribute))) goto err;
2124 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2125 for (j=0; j<attrib->num_values; j++) {
2126 if (!asn1_write_OctetString(state->out,
2127 attrib->values[j].data,
2128 attrib->values[j].length)) goto err;
2130 if (!asn1_pop_tag(state->out)) goto err;
2131 if (!asn1_pop_tag(state->out)) goto err;
2134 if (!asn1_pop_tag(state->out)) goto err;
2135 if (!asn1_pop_tag(state->out)) goto err;
2137 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2138 sctrls, num_sctrls);
2139 if (tevent_req_nomem(subreq, req)) {
2140 return tevent_req_post(req, ev);
2142 tevent_req_set_callback(subreq, tldap_add_done, req);
2147 tevent_req_error(req, TLDAP_ENCODING_ERROR);
2148 return tevent_req_post(req, ev);
2151 static void tldap_add_done(struct tevent_req *subreq)
2153 tldap_simple_done(subreq, TLDAP_RES_ADD);
2156 int tldap_add_recv(struct tevent_req *req)
2158 return tldap_simple_recv(req);
2161 int tldap_add(struct tldap_context *ld, const char *dn,
2162 struct tldap_mod *attributes, int num_attributes,
2163 struct tldap_control *sctrls, int num_sctrls,
2164 struct tldap_control *cctrls, int num_cctrls)
2166 TALLOC_CTX *frame = talloc_stackframe();
2167 struct tevent_context *ev;
2168 struct tevent_req *req;
2171 ev = samba_tevent_context_init(frame);
2173 result = TLDAP_NO_MEMORY;
2177 req = tldap_add_send(frame, ev, ld, dn, attributes, num_attributes,
2178 sctrls, num_sctrls, cctrls, num_cctrls);
2180 result = TLDAP_NO_MEMORY;
2184 if (!tevent_req_poll(req, ev)) {
2185 result = TLDAP_OPERATIONS_ERROR;
2189 result = tldap_add_recv(req);
2190 tldap_save_msg(ld, req);
2196 static void tldap_modify_done(struct tevent_req *subreq);
2198 struct tevent_req *tldap_modify_send(TALLOC_CTX *mem_ctx,
2199 struct tevent_context *ev,
2200 struct tldap_context *ld,
2202 struct tldap_mod *mods, int num_mods,
2203 struct tldap_control *sctrls,
2205 struct tldap_control *cctrls,
2208 struct tevent_req *req, *subreq;
2209 struct tldap_req_state *state;
2212 req = tldap_req_create(mem_ctx, ld, &state);
2217 if (!asn1_push_tag(state->out, TLDAP_REQ_MODIFY)) goto err;
2218 if (!asn1_write_OctetString(state->out, dn, strlen(dn))) goto err;
2219 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2221 for (i=0; i<num_mods; i++) {
2222 struct tldap_mod *mod = &mods[i];
2223 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2224 if (!asn1_write_enumerated(state->out, mod->mod_op)) goto err;
2225 if (!asn1_push_tag(state->out, ASN1_SEQUENCE(0))) goto err;
2226 if (!asn1_write_OctetString(state->out, mod->attribute,
2227 strlen(mod->attribute))) goto err;
2228 if (!asn1_push_tag(state->out, ASN1_SET)) goto err;
2229 for (j=0; j<mod->num_values; j++) {
2230 if (!asn1_write_OctetString(state->out,
2231 mod->values[j].data,
2232 mod->values[j].length)) goto err;
2234 if (!asn1_pop_tag(state->out)) goto err;
2235 if (!asn1_pop_tag(state->out)) goto err;
2236 if (!asn1_pop_tag(state->out)) goto err;
2239 if (!asn1_pop_tag(state->out)) goto err;
2240 if (!asn1_pop_tag(state->out)) goto err;
2242 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2243 sctrls, num_sctrls);
2244 if (tevent_req_nomem(subreq, req)) {
2245 return tevent_req_post(req, ev);
2247 tevent_req_set_callback(subreq, tldap_modify_done, req);
2252 tevent_req_error(req, TLDAP_ENCODING_ERROR);
2253 return tevent_req_post(req, ev);
2256 static void tldap_modify_done(struct tevent_req *subreq)
2258 tldap_simple_done(subreq, TLDAP_RES_MODIFY);
2261 int tldap_modify_recv(struct tevent_req *req)
2263 return tldap_simple_recv(req);
2266 int tldap_modify(struct tldap_context *ld, const char *dn,
2267 struct tldap_mod *mods, int num_mods,
2268 struct tldap_control *sctrls, int num_sctrls,
2269 struct tldap_control *cctrls, int num_cctrls)
2271 TALLOC_CTX *frame = talloc_stackframe();
2272 struct tevent_context *ev;
2273 struct tevent_req *req;
2276 ev = samba_tevent_context_init(frame);
2278 result = TLDAP_NO_MEMORY;
2282 req = tldap_modify_send(frame, ev, ld, dn, mods, num_mods,
2283 sctrls, num_sctrls, cctrls, num_cctrls);
2285 result = TLDAP_NO_MEMORY;
2289 if (!tevent_req_poll(req, ev)) {
2290 result = TLDAP_OPERATIONS_ERROR;
2294 result = tldap_modify_recv(req);
2295 tldap_save_msg(ld, req);
2301 static void tldap_delete_done(struct tevent_req *subreq);
2303 struct tevent_req *tldap_delete_send(TALLOC_CTX *mem_ctx,
2304 struct tevent_context *ev,
2305 struct tldap_context *ld,
2307 struct tldap_control *sctrls,
2309 struct tldap_control *cctrls,
2312 struct tevent_req *req, *subreq;
2313 struct tldap_req_state *state;
2315 req = tldap_req_create(mem_ctx, ld, &state);
2320 if (!asn1_push_tag(state->out, TLDAP_REQ_DELETE)) goto err;
2321 if (!asn1_write(state->out, dn, strlen(dn))) goto err;
2322 if (!asn1_pop_tag(state->out)) goto err;
2324 subreq = tldap_msg_send(state, ev, ld, state->id, state->out,
2325 sctrls, num_sctrls);
2326 if (tevent_req_nomem(subreq, req)) {
2327 return tevent_req_post(req, ev);
2329 tevent_req_set_callback(subreq, tldap_delete_done, req);
2334 tevent_req_error(req, TLDAP_ENCODING_ERROR);
2335 return tevent_req_post(req, ev);
2338 static void tldap_delete_done(struct tevent_req *subreq)
2340 tldap_simple_done(subreq, TLDAP_RES_DELETE);
2343 int tldap_delete_recv(struct tevent_req *req)
2345 return tldap_simple_recv(req);
2348 int tldap_delete(struct tldap_context *ld, const char *dn,
2349 struct tldap_control *sctrls, int num_sctrls,
2350 struct tldap_control *cctrls, int num_cctrls)
2352 TALLOC_CTX *frame = talloc_stackframe();
2353 struct tevent_context *ev;
2354 struct tevent_req *req;
2357 ev = samba_tevent_context_init(frame);
2359 result = TLDAP_NO_MEMORY;
2363 req = tldap_delete_send(frame, ev, ld, dn, sctrls, num_sctrls,
2364 cctrls, num_cctrls);
2366 result = TLDAP_NO_MEMORY;
2370 if (!tevent_req_poll(req, ev)) {
2371 result = TLDAP_OPERATIONS_ERROR;
2375 result = tldap_delete_recv(req);
2376 tldap_save_msg(ld, req);
2382 int tldap_msg_id(const struct tldap_message *msg)
2387 int tldap_msg_type(const struct tldap_message *msg)
2392 const char *tldap_msg_matcheddn(struct tldap_message *msg)
2397 return msg->res_matcheddn;
2400 const char *tldap_msg_diagnosticmessage(struct tldap_message *msg)
2405 return msg->res_diagnosticmessage;
2408 const char *tldap_msg_referral(struct tldap_message *msg)
2413 return msg->res_referral;
2416 void tldap_msg_sctrls(struct tldap_message *msg, int *num_sctrls,
2417 struct tldap_control **sctrls)
2424 *sctrls = msg->res_sctrls;
2425 *num_sctrls = talloc_array_length(msg->res_sctrls);
2428 struct tldap_message *tldap_ctx_lastmsg(struct tldap_context *ld)
2430 return ld->last_msg;
2433 const char *tldap_err2string(int rc)
2435 const char *res = NULL;
2438 * This would normally be a table, but the error codes are not fully
2439 * sequential. Let the compiler figure out the optimum implementation
2445 res = "TLDAP_SUCCESS";
2447 case TLDAP_OPERATIONS_ERROR:
2448 res = "TLDAP_OPERATIONS_ERROR";
2450 case TLDAP_PROTOCOL_ERROR:
2451 res = "TLDAP_PROTOCOL_ERROR";
2453 case TLDAP_TIMELIMIT_EXCEEDED:
2454 res = "TLDAP_TIMELIMIT_EXCEEDED";
2456 case TLDAP_SIZELIMIT_EXCEEDED:
2457 res = "TLDAP_SIZELIMIT_EXCEEDED";
2459 case TLDAP_COMPARE_FALSE:
2460 res = "TLDAP_COMPARE_FALSE";
2462 case TLDAP_COMPARE_TRUE:
2463 res = "TLDAP_COMPARE_TRUE";
2465 case TLDAP_STRONG_AUTH_NOT_SUPPORTED:
2466 res = "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2468 case TLDAP_STRONG_AUTH_REQUIRED:
2469 res = "TLDAP_STRONG_AUTH_REQUIRED";
2471 case TLDAP_REFERRAL:
2472 res = "TLDAP_REFERRAL";
2474 case TLDAP_ADMINLIMIT_EXCEEDED:
2475 res = "TLDAP_ADMINLIMIT_EXCEEDED";
2477 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION:
2478 res = "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2480 case TLDAP_CONFIDENTIALITY_REQUIRED:
2481 res = "TLDAP_CONFIDENTIALITY_REQUIRED";
2483 case TLDAP_SASL_BIND_IN_PROGRESS:
2484 res = "TLDAP_SASL_BIND_IN_PROGRESS";
2486 case TLDAP_NO_SUCH_ATTRIBUTE:
2487 res = "TLDAP_NO_SUCH_ATTRIBUTE";
2489 case TLDAP_UNDEFINED_TYPE:
2490 res = "TLDAP_UNDEFINED_TYPE";
2492 case TLDAP_INAPPROPRIATE_MATCHING:
2493 res = "TLDAP_INAPPROPRIATE_MATCHING";
2495 case TLDAP_CONSTRAINT_VIOLATION:
2496 res = "TLDAP_CONSTRAINT_VIOLATION";
2498 case TLDAP_TYPE_OR_VALUE_EXISTS:
2499 res = "TLDAP_TYPE_OR_VALUE_EXISTS";
2501 case TLDAP_INVALID_SYNTAX:
2502 res = "TLDAP_INVALID_SYNTAX";
2504 case TLDAP_NO_SUCH_OBJECT:
2505 res = "TLDAP_NO_SUCH_OBJECT";
2507 case TLDAP_ALIAS_PROBLEM:
2508 res = "TLDAP_ALIAS_PROBLEM";
2510 case TLDAP_INVALID_DN_SYNTAX:
2511 res = "TLDAP_INVALID_DN_SYNTAX";
2514 res = "TLDAP_IS_LEAF";
2516 case TLDAP_ALIAS_DEREF_PROBLEM:
2517 res = "TLDAP_ALIAS_DEREF_PROBLEM";
2519 case TLDAP_INAPPROPRIATE_AUTH:
2520 res = "TLDAP_INAPPROPRIATE_AUTH";
2522 case TLDAP_INVALID_CREDENTIALS:
2523 res = "TLDAP_INVALID_CREDENTIALS";
2525 case TLDAP_INSUFFICIENT_ACCESS:
2526 res = "TLDAP_INSUFFICIENT_ACCESS";
2531 case TLDAP_UNAVAILABLE:
2532 res = "TLDAP_UNAVAILABLE";
2534 case TLDAP_UNWILLING_TO_PERFORM:
2535 res = "TLDAP_UNWILLING_TO_PERFORM";
2537 case TLDAP_LOOP_DETECT:
2538 res = "TLDAP_LOOP_DETECT";
2540 case TLDAP_NAMING_VIOLATION:
2541 res = "TLDAP_NAMING_VIOLATION";
2543 case TLDAP_OBJECT_CLASS_VIOLATION:
2544 res = "TLDAP_OBJECT_CLASS_VIOLATION";
2546 case TLDAP_NOT_ALLOWED_ON_NONLEAF:
2547 res = "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2549 case TLDAP_NOT_ALLOWED_ON_RDN:
2550 res = "TLDAP_NOT_ALLOWED_ON_RDN";
2552 case TLDAP_ALREADY_EXISTS:
2553 res = "TLDAP_ALREADY_EXISTS";
2555 case TLDAP_NO_OBJECT_CLASS_MODS:
2556 res = "TLDAP_NO_OBJECT_CLASS_MODS";
2558 case TLDAP_RESULTS_TOO_LARGE:
2559 res = "TLDAP_RESULTS_TOO_LARGE";
2561 case TLDAP_AFFECTS_MULTIPLE_DSAS:
2562 res = "TLDAP_AFFECTS_MULTIPLE_DSAS";
2565 res = "TLDAP_OTHER";
2567 case TLDAP_SERVER_DOWN:
2568 res = "TLDAP_SERVER_DOWN";
2570 case TLDAP_LOCAL_ERROR:
2571 res = "TLDAP_LOCAL_ERROR";
2573 case TLDAP_ENCODING_ERROR:
2574 res = "TLDAP_ENCODING_ERROR";
2576 case TLDAP_DECODING_ERROR:
2577 res = "TLDAP_DECODING_ERROR";
2580 res = "TLDAP_TIMEOUT";
2582 case TLDAP_AUTH_UNKNOWN:
2583 res = "TLDAP_AUTH_UNKNOWN";
2585 case TLDAP_FILTER_ERROR:
2586 res = "TLDAP_FILTER_ERROR";
2588 case TLDAP_USER_CANCELLED:
2589 res = "TLDAP_USER_CANCELLED";
2591 case TLDAP_PARAM_ERROR:
2592 res = "TLDAP_PARAM_ERROR";
2594 case TLDAP_NO_MEMORY:
2595 res = "TLDAP_NO_MEMORY";
2597 case TLDAP_CONNECT_ERROR:
2598 res = "TLDAP_CONNECT_ERROR";
2600 case TLDAP_NOT_SUPPORTED:
2601 res = "TLDAP_NOT_SUPPORTED";
2603 case TLDAP_CONTROL_NOT_FOUND:
2604 res = "TLDAP_CONTROL_NOT_FOUND";
2606 case TLDAP_NO_RESULTS_RETURNED:
2607 res = "TLDAP_NO_RESULTS_RETURNED";
2609 case TLDAP_MORE_RESULTS_TO_RETURN:
2610 res = "TLDAP_MORE_RESULTS_TO_RETURN";
2612 case TLDAP_CLIENT_LOOP:
2613 res = "TLDAP_CLIENT_LOOP";
2615 case TLDAP_REFERRAL_LIMIT_EXCEEDED:
2616 res = "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2619 res = talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2624 res = "Unknown LDAP Error";