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
{
63 struct tstream_context
*conn
;
66 struct tevent_queue
*outgoing
;
67 struct tevent_req
**pending
;
69 /* For the sync wrappers we need something like get_last_error... */
70 struct tldap_message
*last_msg
;
73 void (*log_fn
)(void *context
, enum tldap_debug_level level
,
74 const char *fmt
, va_list ap
);
77 struct tldap_ctx_attribute
*ctx_attrs
;
80 struct tldap_message
{
81 struct asn1_data
*data
;
88 struct tldap_attribute
*attribs
;
90 /* Error data sent by the server */
93 char *res_diagnosticmessage
;
95 struct tldap_control
*res_sctrls
;
97 /* Controls sent by the server */
98 struct tldap_control
*ctrls
;
101 void tldap_set_debug(struct tldap_context
*ld
,
102 void (*log_fn
)(void *log_private
,
103 enum tldap_debug_level level
,
105 va_list ap
) PRINTF_ATTRIBUTE(3,0),
109 ld
->log_private
= log_private
;
112 static void tldap_debug(struct tldap_context
*ld
,
113 enum tldap_debug_level level
,
114 const char *fmt
, ...)
120 if (ld
->log_fn
== NULL
) {
124 ld
->log_fn(ld
->log_private
, level
, fmt
, ap
);
128 static int tldap_next_msgid(struct tldap_context
*ld
)
132 result
= ld
->msgid
++;
133 if (ld
->msgid
== 2147483647) {
139 struct tldap_context
*tldap_context_create(TALLOC_CTX
*mem_ctx
, int fd
)
141 struct tldap_context
*ctx
;
144 ctx
= talloc_zero(mem_ctx
, struct tldap_context
);
148 ret
= tstream_bsd_existing_socket(ctx
, fd
, &ctx
->conn
);
155 ctx
->outgoing
= tevent_queue_create(ctx
, "tldap_outgoing");
156 if (ctx
->outgoing
== NULL
) {
163 bool tldap_connection_ok(struct tldap_context
*ld
)
168 return !ld
->server_down
;
171 static struct tldap_ctx_attribute
*tldap_context_findattr(
172 struct tldap_context
*ld
, const char *name
)
176 num_attrs
= talloc_array_length(ld
->ctx_attrs
);
178 for (i
=0; i
<num_attrs
; i
++) {
179 if (strcmp(ld
->ctx_attrs
[i
].name
, name
) == 0) {
180 return &ld
->ctx_attrs
[i
];
186 bool tldap_context_setattr(struct tldap_context
*ld
,
187 const char *name
, const void *_pptr
)
189 struct tldap_ctx_attribute
*tmp
, *attr
;
192 void **pptr
= (void **)discard_const_p(void,_pptr
);
194 attr
= tldap_context_findattr(ld
, name
);
197 * We don't actually delete attrs, we don't expect tons of
198 * attributes being shuffled around.
200 TALLOC_FREE(attr
->ptr
);
202 attr
->ptr
= talloc_move(ld
->ctx_attrs
, pptr
);
208 tmpname
= talloc_strdup(ld
, name
);
209 if (tmpname
== NULL
) {
213 num_attrs
= talloc_array_length(ld
->ctx_attrs
);
215 tmp
= talloc_realloc(ld
, ld
->ctx_attrs
, struct tldap_ctx_attribute
,
218 TALLOC_FREE(tmpname
);
221 tmp
[num_attrs
].name
= talloc_move(tmp
, &tmpname
);
223 tmp
[num_attrs
].ptr
= talloc_move(tmp
, pptr
);
225 tmp
[num_attrs
].ptr
= NULL
;
232 void *tldap_context_getattr(struct tldap_context
*ld
, const char *name
)
234 struct tldap_ctx_attribute
*attr
= tldap_context_findattr(ld
, name
);
242 struct read_ldap_state
{
247 static ssize_t
read_ldap_more(uint8_t *buf
, size_t buflen
, void *private_data
);
248 static void read_ldap_done(struct tevent_req
*subreq
);
250 static struct tevent_req
*read_ldap_send(TALLOC_CTX
*mem_ctx
,
251 struct tevent_context
*ev
,
252 struct tstream_context
*conn
)
254 struct tevent_req
*req
, *subreq
;
255 struct read_ldap_state
*state
;
257 req
= tevent_req_create(mem_ctx
, &state
, struct read_ldap_state
);
263 subreq
= tstream_read_packet_send(state
, ev
, conn
, 2, read_ldap_more
,
265 if (tevent_req_nomem(subreq
, req
)) {
266 return tevent_req_post(req
, ev
);
268 tevent_req_set_callback(subreq
, read_ldap_done
, req
);
272 static ssize_t
read_ldap_more(uint8_t *buf
, size_t buflen
, void *private_data
)
274 struct read_ldap_state
*state
= talloc_get_type_abort(
275 private_data
, struct read_ldap_state
);
280 /* We've been here, we're done */
285 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
287 if (buf
[0] != 0x30) {
292 if ((len
& 0x80) == 0) {
297 lensize
= (len
& 0x7f);
301 /* Please get us the full length */
304 if (buflen
> 2 + lensize
) {
308 if (buflen
!= 2 + lensize
) {
312 for (i
=0; i
<lensize
; i
++) {
313 len
= (len
<< 8) | buf
[2+i
];
318 static void read_ldap_done(struct tevent_req
*subreq
)
320 struct tevent_req
*req
= tevent_req_callback_data(
321 subreq
, struct tevent_req
);
322 struct read_ldap_state
*state
= tevent_req_data(
323 req
, struct read_ldap_state
);
327 nread
= tstream_read_packet_recv(subreq
, state
, &state
->buf
, &err
);
330 tevent_req_error(req
, err
);
333 tevent_req_done(req
);
336 static ssize_t
read_ldap_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
337 uint8_t **pbuf
, int *perrno
)
339 struct read_ldap_state
*state
= tevent_req_data(
340 req
, struct read_ldap_state
);
342 if (tevent_req_is_unix_error(req
, perrno
)) {
345 *pbuf
= talloc_move(mem_ctx
, &state
->buf
);
346 return talloc_get_size(*pbuf
);
349 struct tldap_msg_state
{
350 struct tldap_context
*ld
;
351 struct tevent_context
*ev
;
355 struct asn1_data
*data
;
359 static void tldap_push_controls(struct asn1_data
*data
,
360 struct tldap_control
*sctrls
,
365 if ((sctrls
== NULL
) || (num_sctrls
== 0)) {
369 asn1_push_tag(data
, ASN1_CONTEXT(0));
371 for (i
=0; i
<num_sctrls
; i
++) {
372 struct tldap_control
*c
= &sctrls
[i
];
373 asn1_push_tag(data
, ASN1_SEQUENCE(0));
374 asn1_write_OctetString(data
, c
->oid
, strlen(c
->oid
));
376 asn1_write_BOOLEAN(data
, true);
378 if (c
->value
.data
!= NULL
) {
379 asn1_write_OctetString(data
, c
->value
.data
,
382 asn1_pop_tag(data
); /* ASN1_SEQUENCE(0) */
385 asn1_pop_tag(data
); /* ASN1_CONTEXT(0) */
388 static void tldap_msg_sent(struct tevent_req
*subreq
);
389 static void tldap_msg_received(struct tevent_req
*subreq
);
391 static struct tevent_req
*tldap_msg_send(TALLOC_CTX
*mem_ctx
,
392 struct tevent_context
*ev
,
393 struct tldap_context
*ld
,
394 int id
, struct asn1_data
*data
,
395 struct tldap_control
*sctrls
,
398 struct tevent_req
*req
, *subreq
;
399 struct tldap_msg_state
*state
;
402 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "tldap_msg_send: sending msg %d\n",
405 req
= tevent_req_create(mem_ctx
, &state
, struct tldap_msg_state
);
413 if (state
->ld
->server_down
) {
414 tevent_req_error(req
, TLDAP_SERVER_DOWN
);
415 return tevent_req_post(req
, ev
);
418 tldap_push_controls(data
, sctrls
, num_sctrls
);
422 if (!asn1_blob(data
, &blob
)) {
423 tevent_req_error(req
, TLDAP_ENCODING_ERROR
);
424 return tevent_req_post(req
, ev
);
427 state
->iov
.iov_base
= (void *)blob
.data
;
428 state
->iov
.iov_len
= blob
.length
;
430 subreq
= tstream_writev_queue_send(state
, ev
, ld
->conn
, ld
->outgoing
,
432 if (tevent_req_nomem(subreq
, req
)) {
433 return tevent_req_post(req
, ev
);
435 tevent_req_set_callback(subreq
, tldap_msg_sent
, req
);
439 static void tldap_msg_unset_pending(struct tevent_req
*req
)
441 struct tldap_msg_state
*state
= tevent_req_data(
442 req
, struct tldap_msg_state
);
443 struct tldap_context
*ld
= state
->ld
;
444 int num_pending
= talloc_array_length(ld
->pending
);
447 tevent_req_set_cleanup_fn(req
, NULL
);
449 if (num_pending
== 1) {
450 TALLOC_FREE(ld
->pending
);
454 for (i
=0; i
<num_pending
; i
++) {
455 if (req
== ld
->pending
[i
]) {
459 if (i
== num_pending
) {
461 * Something's seriously broken. Just returning here is the
462 * right thing nevertheless, the point of this routine is to
463 * remove ourselves from cli->pending.
469 * Remove ourselves from the cli->pending array
471 if (num_pending
> 1) {
472 ld
->pending
[i
] = ld
->pending
[num_pending
-1];
476 * No NULL check here, we're shrinking by sizeof(void *), and
477 * talloc_realloc just adjusts the size for this.
479 ld
->pending
= talloc_realloc(NULL
, ld
->pending
, struct tevent_req
*,
484 static void tldap_msg_cleanup(struct tevent_req
*req
,
485 enum tevent_req_state req_state
)
488 case TEVENT_REQ_USER_ERROR
:
489 case TEVENT_REQ_RECEIVED
:
490 tldap_msg_unset_pending(req
);
497 static bool tldap_msg_set_pending(struct tevent_req
*req
)
499 struct tldap_msg_state
*state
= tevent_req_data(
500 req
, struct tldap_msg_state
);
501 struct tldap_context
*ld
;
502 struct tevent_req
**pending
;
504 struct tevent_req
*subreq
;
507 num_pending
= talloc_array_length(ld
->pending
);
509 pending
= talloc_realloc(ld
, ld
->pending
, struct tevent_req
*,
511 if (pending
== NULL
) {
514 pending
[num_pending
] = req
;
515 ld
->pending
= pending
;
516 tevent_req_set_cleanup_fn(req
, tldap_msg_cleanup
);
518 if (num_pending
> 0) {
523 * We're the first one, add the read_ldap request that waits for the
524 * answer from the server
526 subreq
= read_ldap_send(ld
->pending
, state
->ev
, ld
->conn
);
527 if (subreq
== NULL
) {
528 tldap_msg_unset_pending(req
);
531 tevent_req_set_callback(subreq
, tldap_msg_received
, ld
);
535 static void tldap_msg_sent(struct tevent_req
*subreq
)
537 struct tevent_req
*req
= tevent_req_callback_data(
538 subreq
, struct tevent_req
);
539 struct tldap_msg_state
*state
= tevent_req_data(
540 req
, struct tldap_msg_state
);
544 nwritten
= tstream_writev_queue_recv(subreq
, &err
);
546 if (nwritten
== -1) {
547 state
->ld
->server_down
= true;
548 tevent_req_error(req
, TLDAP_SERVER_DOWN
);
552 if (!tldap_msg_set_pending(req
)) {
558 static int tldap_msg_msgid(struct tevent_req
*req
)
560 struct tldap_msg_state
*state
= tevent_req_data(
561 req
, struct tldap_msg_state
);
566 static void tldap_msg_received(struct tevent_req
*subreq
)
568 struct tldap_context
*ld
= tevent_req_callback_data(
569 subreq
, struct tldap_context
);
570 struct tevent_req
*req
;
571 struct tldap_msg_state
*state
;
572 struct asn1_data
*data
;
581 received
= read_ldap_recv(subreq
, talloc_tos(), &inbuf
, &err
);
583 if (received
== -1) {
584 status
= TLDAP_SERVER_DOWN
;
588 data
= asn1_init(talloc_tos());
590 status
= TLDAP_NO_MEMORY
;
593 asn1_load_nocopy(data
, inbuf
, received
);
596 ok
&= asn1_start_tag(data
, ASN1_SEQUENCE(0));
597 ok
&= asn1_read_Integer(data
, &id
);
598 ok
&= asn1_peek_uint8(data
, &type
);
601 status
= TLDAP_PROTOCOL_ERROR
;
605 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "tldap_msg_received: got msg %d "
606 "type %d\n", id
, (int)type
);
608 num_pending
= talloc_array_length(ld
->pending
);
610 for (i
=0; i
<num_pending
; i
++) {
611 if (id
== tldap_msg_msgid(ld
->pending
[i
])) {
615 if (i
== num_pending
) {
616 /* Dump unexpected reply */
617 tldap_debug(ld
, TLDAP_DEBUG_WARNING
, "tldap_msg_received: "
618 "No request pending for msg %d\n", id
);
624 req
= ld
->pending
[i
];
625 state
= tevent_req_data(req
, struct tldap_msg_state
);
627 state
->inbuf
= talloc_move(state
, &inbuf
);
628 state
->data
= talloc_move(state
, &data
);
630 tldap_msg_unset_pending(req
);
631 num_pending
= talloc_array_length(ld
->pending
);
633 tevent_req_done(req
);
636 if (num_pending
== 0) {
639 if (talloc_array_length(ld
->pending
) > num_pending
) {
641 * The callback functions called from tevent_req_done() above
642 * have put something on the pending queue. We don't have to
643 * trigger the read_ldap_send(), tldap_msg_set_pending() has
644 * done it for us already.
649 state
= tevent_req_data(ld
->pending
[0], struct tldap_msg_state
);
650 subreq
= read_ldap_send(ld
->pending
, state
->ev
, ld
->conn
);
651 if (subreq
== NULL
) {
652 status
= TLDAP_NO_MEMORY
;
655 tevent_req_set_callback(subreq
, tldap_msg_received
, ld
);
659 while (talloc_array_length(ld
->pending
) > 0) {
660 req
= ld
->pending
[0];
661 state
= tevent_req_data(req
, struct tldap_msg_state
);
662 tevent_req_defer_callback(req
, state
->ev
);
663 tevent_req_error(req
, status
);
667 static int tldap_msg_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
668 struct tldap_message
**pmsg
)
670 struct tldap_msg_state
*state
= tevent_req_data(
671 req
, struct tldap_msg_state
);
672 struct tldap_message
*msg
;
676 if (tevent_req_is_ldap_error(req
, &err
)) {
680 if (!asn1_peek_uint8(state
->data
, &msgtype
)) {
681 return TLDAP_PROTOCOL_ERROR
;
685 return TLDAP_SUCCESS
;
688 msg
= talloc_zero(mem_ctx
, struct tldap_message
);
690 return TLDAP_NO_MEMORY
;
694 msg
->inbuf
= talloc_move(msg
, &state
->inbuf
);
695 msg
->data
= talloc_move(msg
, &state
->data
);
699 return TLDAP_SUCCESS
;
702 struct tldap_req_state
{
704 struct asn1_data
*out
;
705 struct tldap_message
*result
;
708 static struct tevent_req
*tldap_req_create(TALLOC_CTX
*mem_ctx
,
709 struct tldap_context
*ld
,
710 struct tldap_req_state
**pstate
)
712 struct tevent_req
*req
;
713 struct tldap_req_state
*state
;
715 req
= tevent_req_create(mem_ctx
, &state
, struct tldap_req_state
);
720 state
->out
= asn1_init(state
);
721 if (state
->out
== NULL
) {
725 state
->result
= NULL
;
726 state
->id
= tldap_next_msgid(ld
);
728 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
729 asn1_write_Integer(state
->out
, state
->id
);
735 static void tldap_save_msg(struct tldap_context
*ld
, struct tevent_req
*req
)
737 struct tldap_req_state
*state
= tevent_req_data(
738 req
, struct tldap_req_state
);
740 TALLOC_FREE(ld
->last_msg
);
741 ld
->last_msg
= talloc_move(ld
, &state
->result
);
744 static char *blob2string_talloc(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
)
746 char *result
= talloc_array(mem_ctx
, char, blob
.length
+1);
748 if (result
== NULL
) {
752 memcpy(result
, blob
.data
, blob
.length
);
753 result
[blob
.length
] = '\0';
757 static bool asn1_read_OctetString_talloc(TALLOC_CTX
*mem_ctx
,
758 struct asn1_data
*data
,
763 if (!asn1_read_OctetString(data
, mem_ctx
, &string
))
766 result
= blob2string_talloc(mem_ctx
, string
);
768 data_blob_free(&string
);
770 if (result
== NULL
) {
777 static bool tldap_decode_controls(struct tldap_req_state
*state
);
779 static bool tldap_decode_response(struct tldap_req_state
*state
)
781 struct asn1_data
*data
= state
->result
->data
;
782 struct tldap_message
*msg
= state
->result
;
785 ok
&= asn1_read_enumerated(data
, &msg
->lderr
);
786 ok
&= asn1_read_OctetString_talloc(msg
, data
, &msg
->res_matcheddn
);
787 ok
&= asn1_read_OctetString_talloc(msg
, data
,
788 &msg
->res_diagnosticmessage
);
789 if (asn1_peek_tag(data
, ASN1_CONTEXT(3))) {
790 ok
&= asn1_start_tag(data
, ASN1_CONTEXT(3));
791 ok
&= asn1_read_OctetString_talloc(msg
, data
,
793 ok
&= asn1_end_tag(data
);
795 msg
->res_referral
= NULL
;
801 static void tldap_sasl_bind_done(struct tevent_req
*subreq
);
803 struct tevent_req
*tldap_sasl_bind_send(TALLOC_CTX
*mem_ctx
,
804 struct tevent_context
*ev
,
805 struct tldap_context
*ld
,
807 const char *mechanism
,
809 struct tldap_control
*sctrls
,
811 struct tldap_control
*cctrls
,
814 struct tevent_req
*req
, *subreq
;
815 struct tldap_req_state
*state
;
817 req
= tldap_req_create(mem_ctx
, ld
, &state
);
826 asn1_push_tag(state
->out
, TLDAP_REQ_BIND
);
827 asn1_write_Integer(state
->out
, ld
->ld_version
);
828 asn1_write_OctetString(state
->out
, dn
, strlen(dn
));
830 if (mechanism
== NULL
) {
831 asn1_push_tag(state
->out
, ASN1_CONTEXT_SIMPLE(0));
832 asn1_write(state
->out
, creds
->data
, creds
->length
);
833 asn1_pop_tag(state
->out
);
835 asn1_push_tag(state
->out
, ASN1_CONTEXT(3));
836 asn1_write_OctetString(state
->out
, mechanism
,
838 if ((creds
!= NULL
) && (creds
->data
!= NULL
)) {
839 asn1_write_OctetString(state
->out
, creds
->data
,
842 asn1_pop_tag(state
->out
);
845 if (!asn1_pop_tag(state
->out
)) {
846 tevent_req_error(req
, TLDAP_ENCODING_ERROR
);
847 return tevent_req_post(req
, ev
);
850 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
852 if (tevent_req_nomem(subreq
, req
)) {
853 return tevent_req_post(req
, ev
);
855 tevent_req_set_callback(subreq
, tldap_sasl_bind_done
, req
);
859 static void tldap_sasl_bind_done(struct tevent_req
*subreq
)
861 struct tevent_req
*req
= tevent_req_callback_data(
862 subreq
, struct tevent_req
);
863 struct tldap_req_state
*state
= tevent_req_data(
864 req
, struct tldap_req_state
);
867 err
= tldap_msg_recv(subreq
, state
, &state
->result
);
869 if (err
!= TLDAP_SUCCESS
) {
870 tevent_req_error(req
, err
);
873 if (state
->result
->type
!= TLDAP_RES_BIND
) {
874 tevent_req_error(req
, TLDAP_PROTOCOL_ERROR
);
877 if (!asn1_start_tag(state
->result
->data
, state
->result
->type
) ||
878 !tldap_decode_response(state
) ||
879 !asn1_end_tag(state
->result
->data
)) {
880 tevent_req_error(req
, TLDAP_DECODING_ERROR
);
884 * TODO: pull the reply blob
886 if (state
->result
->lderr
!= TLDAP_SUCCESS
) {
887 tevent_req_error(req
, state
->result
->lderr
);
890 tevent_req_done(req
);
893 int tldap_sasl_bind_recv(struct tevent_req
*req
)
895 return tldap_simple_recv(req
);
898 int tldap_sasl_bind(struct tldap_context
*ld
,
900 const char *mechanism
,
902 struct tldap_control
*sctrls
,
904 struct tldap_control
*cctrls
,
907 TALLOC_CTX
*frame
= talloc_stackframe();
908 struct tevent_context
*ev
;
909 struct tevent_req
*req
;
912 ev
= samba_tevent_context_init(frame
);
914 result
= TLDAP_NO_MEMORY
;
918 req
= tldap_sasl_bind_send(frame
, ev
, ld
, dn
, mechanism
, creds
,
919 sctrls
, num_sctrls
, cctrls
, num_cctrls
);
921 result
= TLDAP_NO_MEMORY
;
925 if (!tevent_req_poll(req
, ev
)) {
926 result
= TLDAP_OPERATIONS_ERROR
;
930 result
= tldap_sasl_bind_recv(req
);
931 tldap_save_msg(ld
, req
);
937 struct tevent_req
*tldap_simple_bind_send(TALLOC_CTX
*mem_ctx
,
938 struct tevent_context
*ev
,
939 struct tldap_context
*ld
,
945 if (passwd
!= NULL
) {
946 cred
.data
= discard_const_p(uint8_t, passwd
);
947 cred
.length
= strlen(passwd
);
949 cred
.data
= discard_const_p(uint8_t, "");
952 return tldap_sasl_bind_send(mem_ctx
, ev
, ld
, dn
, NULL
, &cred
, NULL
, 0,
956 int tldap_simple_bind_recv(struct tevent_req
*req
)
958 return tldap_sasl_bind_recv(req
);
961 int tldap_simple_bind(struct tldap_context
*ld
, const char *dn
,
966 if (passwd
!= NULL
) {
967 cred
.data
= discard_const_p(uint8_t, passwd
);
968 cred
.length
= strlen(passwd
);
970 cred
.data
= discard_const_p(uint8_t, "");
973 return tldap_sasl_bind(ld
, dn
, NULL
, &cred
, NULL
, 0, NULL
, 0);
976 /*****************************************************************************/
978 /* can't use isalpha() as only a strict set is valid for LDAP */
980 static bool tldap_is_alpha(char c
)
982 return (((c
>= 'a') && (c
<= 'z')) || \
983 ((c
>= 'A') && (c
<= 'Z')));
986 static bool tldap_is_adh(char c
)
988 return tldap_is_alpha(c
) || isdigit(c
) || (c
== '-');
991 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
992 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
993 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
994 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
995 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
996 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
997 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
998 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
999 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
1000 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
1002 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
1003 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
1004 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
1007 /* oid's should be numerical only in theory,
1008 * but apparently some broken servers may have alphanum aliases instead.
1009 * Do like openldap libraries and allow alphanum aliases for oids, but
1010 * do not allow Tagging options in that case.
1012 static bool tldap_is_attrdesc(const char *s
, int len
, bool no_tagopts
)
1014 bool is_oid
= false;
1018 /* first char has stricter rules */
1021 } else if (!tldap_is_alpha(*s
)) {
1022 /* bad first char */
1026 for (i
= 1; i
< len
; i
++) {
1029 if (isdigit(s
[i
])) {
1042 if (tldap_is_adh(s
[i
])) {
1049 /* no tagging options */
1056 if ((i
+ 1) == len
) {
1074 /* this function copies the value until the closing parenthesis is found. */
1075 static char *tldap_get_val(TALLOC_CTX
*memctx
,
1076 const char *value
, const char **_s
)
1078 const char *s
= value
;
1080 /* find terminator */
1083 if (s
&& (*(s
- 1) == '\\')) {
1088 if (!s
|| !(*s
== ')')) {
1089 /* malformed filter */
1095 return talloc_strndup(memctx
, value
, s
- value
);
1098 static int tldap_hex2char(const char *x
)
1100 if (isxdigit(x
[0]) && isxdigit(x
[1])) {
1101 const char h1
= x
[0], h2
= x
[1];
1104 if (h1
>= 'a') c
= h1
- (int)'a' + 10;
1105 else if (h1
>= 'A') c
= h1
- (int)'A' + 10;
1106 else if (h1
>= '0') c
= h1
- (int)'0';
1108 if (h2
>= 'a') c
+= h2
- (int)'a' + 10;
1109 else if (h2
>= 'A') c
+= h2
- (int)'A' + 10;
1110 else if (h2
>= '0') c
+= h2
- (int)'0';
1118 static bool tldap_find_first_star(const char *val
, const char **star
)
1122 for (s
= val
; *s
; s
++) {
1125 if (isxdigit(s
[1]) && isxdigit(s
[2])) {
1129 /* not hex based escape, check older syntax */
1138 /* invalid escape sequence */
1143 /* end of val, nothing found */
1153 /* string ended without closing parenthesis, filter is malformed */
1157 static bool tldap_unescape_inplace(char *value
, size_t *val_len
)
1161 for (i
= 0,p
= 0; i
< *val_len
; i
++) {
1167 /* these must be escaped */
1171 if (!value
[i
+ 1]) {
1177 c
= tldap_hex2char(&value
[i
]);
1178 if (c
>= 0 && c
< 256) {
1190 value
[p
] = value
[i
];
1199 value
[p
] = value
[i
];
1208 static bool tldap_push_filter_basic(struct tldap_context
*ld
,
1209 struct asn1_data
*data
,
1211 static bool tldap_push_filter_substring(struct tldap_context
*ld
,
1212 struct asn1_data
*data
,
1215 static bool tldap_push_filter_int(struct tldap_context
*ld
,
1216 struct asn1_data
*data
,
1219 const char *s
= *_s
;
1223 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1224 "Incomplete or malformed filter\n");
1229 /* we are right after a parenthesis,
1230 * find out what op we have at hand */
1233 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "Filter op: AND\n");
1234 asn1_push_tag(data
, TLDAP_FILTER_AND
);
1239 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "Filter op: OR\n");
1240 asn1_push_tag(data
, TLDAP_FILTER_OR
);
1245 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "Filter op: NOT\n");
1246 asn1_push_tag(data
, TLDAP_FILTER_NOT
);
1248 ret
= tldap_push_filter_int(ld
, data
, &s
);
1257 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1258 "Invalid parenthesis '%c'\n", *s
);
1262 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1263 "Invalid filter termination\n");
1267 ret
= tldap_push_filter_basic(ld
, data
, &s
);
1274 /* only and/or filters get here.
1275 * go through the list of filters */
1278 /* RFC 4526: empty and/or */
1284 ret
= tldap_push_filter_int(ld
, data
, &s
);
1290 /* end of list, return */
1298 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1299 "Incomplete or malformed filter\n");
1304 if (data
->has_error
) {
1313 static bool tldap_push_filter_basic(struct tldap_context
*ld
,
1314 struct asn1_data
*data
,
1317 TALLOC_CTX
*tmpctx
= talloc_tos();
1318 const char *s
= *_s
;
1326 size_t type_len
= 0;
1329 bool write_octect
= true;
1332 eq
= strchr(s
, '=');
1334 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1335 "Invalid filter, missing equal sign\n");
1344 asn1_push_tag(data
, TLDAP_FILTER_LE
);
1348 asn1_push_tag(data
, TLDAP_FILTER_GE
);
1352 asn1_push_tag(data
, TLDAP_FILTER_APX
);
1356 asn1_push_tag(data
, TLDAP_FILTER_EXT
);
1357 write_octect
= false;
1363 if (*s
== ':') { /* [:dn]:rule:= value */
1365 /* malformed filter */
1369 } else { /* type[:dn][:rule]:= value */
1371 dn
= strchr(s
, ':');
1372 type_len
= dn
- type
;
1373 if (dn
== e
) { /* type:= value */
1380 rule
= strchr(dn
, ':');
1384 if ((rule
== dn
+ 1) || rule
+ 1 == e
) {
1385 /* malformed filter, contains "::" */
1389 if (strncasecmp_m(dn
, "dn:", 3) != 0) {
1394 /* malformed filter. With two
1395 * optionals, the first must be "dn"
1408 if (!type
&& !dn
&& !rule
) {
1409 /* malformed filter, there must be at least one */
1414 MatchingRuleAssertion ::= SEQUENCE {
1415 matchingRule [1] MatchingRuleID OPTIONAL,
1416 type [2] AttributeDescription OPTIONAL,
1417 matchValue [3] AssertionValue,
1418 dnAttributes [4] BOOLEAN DEFAULT FALSE
1422 /* check and add rule */
1424 ret
= tldap_is_attrdesc(rule
, e
- rule
, true);
1428 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(1));
1429 asn1_write(data
, rule
, e
- rule
);
1433 /* check and add type */
1435 ret
= tldap_is_attrdesc(type
, type_len
, false);
1439 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(2));
1440 asn1_write(data
, type
, type_len
);
1444 uval
= tldap_get_val(tmpctx
, val
, _s
);
1448 uval_len
= *_s
- val
;
1449 ret
= tldap_unescape_inplace(uval
, &uval_len
);
1454 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(3));
1455 asn1_write(data
, uval
, uval_len
);
1458 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(4));
1459 asn1_write_uint8(data
, dn
?1:0);
1466 ret
= tldap_is_attrdesc(s
, e
- s
, false);
1471 if (strncmp(val
, "*)", 2) == 0) {
1473 asn1_push_tag(data
, TLDAP_FILTER_PRES
);
1474 asn1_write(data
, s
, e
- s
);
1476 write_octect
= false;
1480 ret
= tldap_find_first_star(val
, &star
);
1486 asn1_push_tag(data
, TLDAP_FILTER_SUB
);
1487 asn1_write_OctetString(data
, s
, e
- s
);
1488 ret
= tldap_push_filter_substring(ld
, data
, val
, &s
);
1493 write_octect
= false;
1497 /* if nothing else, then it is just equality */
1498 asn1_push_tag(data
, TLDAP_FILTER_EQ
);
1499 write_octect
= true;
1504 uval
= tldap_get_val(tmpctx
, val
, _s
);
1508 uval_len
= *_s
- val
;
1509 ret
= tldap_unescape_inplace(uval
, &uval_len
);
1514 asn1_write_OctetString(data
, s
, e
- s
);
1515 asn1_write_OctetString(data
, uval
, uval_len
);
1518 if (data
->has_error
) {
1525 static bool tldap_push_filter_substring(struct tldap_context
*ld
,
1526 struct asn1_data
*data
,
1530 TALLOC_CTX
*tmpctx
= talloc_tos();
1531 bool initial
= true;
1538 SubstringFilter ::= SEQUENCE {
1539 type AttributeDescription,
1540 -- at least one must be present
1541 substrings SEQUENCE OF CHOICE {
1542 initial [0] LDAPString,
1544 final [2] LDAPString } }
1546 asn1_push_tag(data
, ASN1_SEQUENCE(0));
1549 ret
= tldap_find_first_star(val
, &star
);
1553 chunk_len
= star
- val
;
1557 if (!initial
&& chunk_len
== 0) {
1558 /* found '**', which is illegal */
1574 if (initial
&& chunk_len
== 0) {
1580 chunk
= talloc_strndup(tmpctx
, val
, chunk_len
);
1584 ret
= tldap_unescape_inplace(chunk
, &chunk_len
);
1591 asn1_push_tag(data
, TLDAP_SUB_INI
);
1594 asn1_push_tag(data
, TLDAP_SUB_ANY
);
1598 asn1_push_tag(data
, TLDAP_SUB_FIN
);
1604 asn1_write(data
, chunk
, chunk_len
);
1609 } while (*star
== '*');
1613 /* end of sequence */
1618 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1619 * around parenthesis, we do not allow any spaces (except in values of
1620 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1621 * leading or trailing spaces where allowed.
1623 static bool tldap_push_filter(struct tldap_context
*ld
,
1624 struct asn1_data
*data
,
1627 const char *s
= filter
;
1630 ret
= tldap_push_filter_int(ld
, data
, &s
);
1632 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1633 "Incomplete or malformed filter\n");
1639 /*****************************************************************************/
1641 static void tldap_search_done(struct tevent_req
*subreq
);
1643 struct tevent_req
*tldap_search_send(TALLOC_CTX
*mem_ctx
,
1644 struct tevent_context
*ev
,
1645 struct tldap_context
*ld
,
1646 const char *base
, int scope
,
1651 struct tldap_control
*sctrls
,
1653 struct tldap_control
*cctrls
,
1659 struct tevent_req
*req
, *subreq
;
1660 struct tldap_req_state
*state
;
1663 req
= tldap_req_create(mem_ctx
, ld
, &state
);
1668 asn1_push_tag(state
->out
, TLDAP_REQ_SEARCH
);
1669 asn1_write_OctetString(state
->out
, base
, strlen(base
));
1670 asn1_write_enumerated(state
->out
, scope
);
1671 asn1_write_enumerated(state
->out
, deref
);
1672 asn1_write_Integer(state
->out
, sizelimit
);
1673 asn1_write_Integer(state
->out
, timelimit
);
1674 asn1_write_BOOLEAN(state
->out
, attrsonly
);
1676 if (!tldap_push_filter(ld
, state
->out
, filter
)) {
1677 goto encoding_error
;
1680 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
1681 for (i
=0; i
<num_attrs
; i
++) {
1682 asn1_write_OctetString(state
->out
, attrs
[i
], strlen(attrs
[i
]));
1684 asn1_pop_tag(state
->out
);
1685 asn1_pop_tag(state
->out
);
1687 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
1688 sctrls
, num_sctrls
);
1689 if (tevent_req_nomem(subreq
, req
)) {
1690 return tevent_req_post(req
, ev
);
1692 tevent_req_set_callback(subreq
, tldap_search_done
, req
);
1696 tevent_req_error(req
, TLDAP_ENCODING_ERROR
);
1697 return tevent_req_post(req
, ev
);
1700 static void tldap_search_done(struct tevent_req
*subreq
)
1702 struct tevent_req
*req
= tevent_req_callback_data(
1703 subreq
, struct tevent_req
);
1704 struct tldap_req_state
*state
= tevent_req_data(
1705 req
, struct tldap_req_state
);
1708 err
= tldap_msg_recv(subreq
, state
, &state
->result
);
1709 if (err
!= TLDAP_SUCCESS
) {
1710 tevent_req_error(req
, err
);
1713 switch (state
->result
->type
) {
1714 case TLDAP_RES_SEARCH_ENTRY
:
1715 case TLDAP_RES_SEARCH_REFERENCE
:
1716 if (!tldap_msg_set_pending(subreq
)) {
1717 tevent_req_oom(req
);
1720 tevent_req_notify_callback(req
);
1722 case TLDAP_RES_SEARCH_RESULT
:
1723 TALLOC_FREE(subreq
);
1724 if (!asn1_start_tag(state
->result
->data
,
1725 state
->result
->type
) ||
1726 !tldap_decode_response(state
) ||
1727 !asn1_end_tag(state
->result
->data
) ||
1728 !tldap_decode_controls(state
)) {
1729 tevent_req_error(req
, TLDAP_DECODING_ERROR
);
1732 tevent_req_done(req
);
1735 tevent_req_error(req
, TLDAP_PROTOCOL_ERROR
);
1740 int tldap_search_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
1741 struct tldap_message
**pmsg
)
1743 struct tldap_req_state
*state
= tevent_req_data(
1744 req
, struct tldap_req_state
);
1747 if (!tevent_req_is_in_progress(req
)
1748 && tevent_req_is_ldap_error(req
, &err
)) {
1752 if (tevent_req_is_in_progress(req
)) {
1753 switch (state
->result
->type
) {
1754 case TLDAP_RES_SEARCH_ENTRY
:
1755 case TLDAP_RES_SEARCH_REFERENCE
:
1758 return TLDAP_OPERATIONS_ERROR
;
1762 *pmsg
= talloc_move(mem_ctx
, &state
->result
);
1763 return TLDAP_SUCCESS
;
1766 struct tldap_sync_search_state
{
1767 TALLOC_CTX
*mem_ctx
;
1768 struct tldap_message
**entries
;
1769 struct tldap_message
**refs
;
1773 static void tldap_search_cb(struct tevent_req
*req
)
1775 struct tldap_sync_search_state
*state
=
1776 (struct tldap_sync_search_state
*)
1777 tevent_req_callback_data_void(req
);
1778 struct tldap_message
*msg
, **tmp
;
1779 int rc
, num_entries
, num_refs
;
1781 rc
= tldap_search_recv(req
, talloc_tos(), &msg
);
1782 if (rc
!= TLDAP_SUCCESS
) {
1787 switch (tldap_msg_type(msg
)) {
1788 case TLDAP_RES_SEARCH_ENTRY
:
1789 num_entries
= talloc_array_length(state
->entries
);
1790 tmp
= talloc_realloc(state
->mem_ctx
, state
->entries
,
1791 struct tldap_message
*, num_entries
+ 1);
1793 state
->rc
= TLDAP_NO_MEMORY
;
1796 state
->entries
= tmp
;
1797 state
->entries
[num_entries
] = talloc_move(state
->entries
,
1800 case TLDAP_RES_SEARCH_REFERENCE
:
1801 num_refs
= talloc_array_length(state
->refs
);
1802 tmp
= talloc_realloc(state
->mem_ctx
, state
->refs
,
1803 struct tldap_message
*, num_refs
+ 1);
1805 state
->rc
= TLDAP_NO_MEMORY
;
1809 state
->refs
[num_refs
] = talloc_move(state
->refs
, &msg
);
1811 case TLDAP_RES_SEARCH_RESULT
:
1812 state
->rc
= TLDAP_SUCCESS
;
1815 state
->rc
= TLDAP_PROTOCOL_ERROR
;
1820 int tldap_search(struct tldap_context
*ld
,
1821 const char *base
, int scope
, const char *filter
,
1822 const char **attrs
, int num_attrs
, int attrsonly
,
1823 struct tldap_control
*sctrls
, int num_sctrls
,
1824 struct tldap_control
*cctrls
, int num_cctrls
,
1825 int timelimit
, int sizelimit
, int deref
,
1826 TALLOC_CTX
*mem_ctx
, struct tldap_message
***entries
,
1827 struct tldap_message
***refs
)
1829 TALLOC_CTX
*frame
= talloc_stackframe();
1830 struct tevent_context
*ev
;
1831 struct tevent_req
*req
;
1832 struct tldap_sync_search_state state
;
1835 state
.mem_ctx
= mem_ctx
;
1836 state
.rc
= TLDAP_SUCCESS
;
1838 ev
= samba_tevent_context_init(frame
);
1840 state
.rc
= TLDAP_NO_MEMORY
;
1844 req
= tldap_search_send(frame
, ev
, ld
, base
, scope
, filter
,
1845 attrs
, num_attrs
, attrsonly
,
1846 sctrls
, num_sctrls
, cctrls
, num_cctrls
,
1847 timelimit
, sizelimit
, deref
);
1849 state
.rc
= TLDAP_NO_MEMORY
;
1853 tevent_req_set_callback(req
, tldap_search_cb
, &state
);
1855 if (!tevent_req_is_in_progress(req
)) {
1856 /* an error happend before sending */
1857 if (tevent_req_is_ldap_error(req
, &state
.rc
)) {
1862 while (tevent_req_is_in_progress(req
)
1863 && (state
.rc
== TLDAP_SUCCESS
)) {
1864 if (tevent_loop_once(ev
) == -1) {
1865 return TLDAP_OPERATIONS_ERROR
;
1869 if (state
.rc
!= TLDAP_SUCCESS
) {
1873 if (entries
!= NULL
) {
1874 *entries
= state
.entries
;
1876 TALLOC_FREE(state
.entries
);
1881 TALLOC_FREE(state
.refs
);
1883 tldap_save_msg(ld
, req
);
1889 static bool tldap_parse_search_entry(struct tldap_message
*msg
)
1891 int num_attribs
= 0;
1893 asn1_start_tag(msg
->data
, msg
->type
);
1897 asn1_read_OctetString_talloc(msg
, msg
->data
, &msg
->dn
);
1898 if (msg
->dn
== NULL
) {
1903 * Attributes: We overallocate msg->attribs by one, so that while
1904 * looping over the attributes we can directly parse into the last
1905 * array element. Same for the values in the inner loop.
1908 msg
->attribs
= talloc_array(msg
, struct tldap_attribute
, 1);
1909 if (msg
->attribs
== NULL
) {
1913 asn1_start_tag(msg
->data
, ASN1_SEQUENCE(0));
1914 while (asn1_peek_tag(msg
->data
, ASN1_SEQUENCE(0))) {
1915 struct tldap_attribute
*attrib
;
1918 attrib
= &msg
->attribs
[num_attribs
];
1919 attrib
->values
= talloc_array(msg
->attribs
, DATA_BLOB
, 1);
1920 if (attrib
->values
== NULL
) {
1923 asn1_start_tag(msg
->data
, ASN1_SEQUENCE(0));
1924 asn1_read_OctetString_talloc(msg
->attribs
, msg
->data
,
1926 asn1_start_tag(msg
->data
, ASN1_SET
);
1928 while (asn1_peek_tag(msg
->data
, ASN1_OCTET_STRING
)) {
1929 asn1_read_OctetString(msg
->data
, msg
,
1930 &attrib
->values
[num_values
]);
1932 attrib
->values
= talloc_realloc(
1933 msg
->attribs
, attrib
->values
, DATA_BLOB
,
1935 if (attrib
->values
== NULL
) {
1940 attrib
->values
= talloc_realloc(msg
->attribs
, attrib
->values
,
1941 DATA_BLOB
, num_values
);
1942 attrib
->num_values
= num_values
;
1944 asn1_end_tag(msg
->data
); /* ASN1_SET */
1945 asn1_end_tag(msg
->data
); /* ASN1_SEQUENCE(0) */
1946 msg
->attribs
= talloc_realloc(
1947 msg
, msg
->attribs
, struct tldap_attribute
,
1949 if (msg
->attribs
== NULL
) {
1954 msg
->attribs
= talloc_realloc(
1955 msg
, msg
->attribs
, struct tldap_attribute
, num_attribs
);
1956 asn1_end_tag(msg
->data
);
1957 if (msg
->data
->has_error
) {
1963 bool tldap_entry_dn(struct tldap_message
*msg
, char **dn
)
1965 if ((msg
->dn
== NULL
) && (!tldap_parse_search_entry(msg
))) {
1972 bool tldap_entry_attributes(struct tldap_message
*msg
,
1973 struct tldap_attribute
**attributes
,
1974 int *num_attributes
)
1976 if ((msg
->dn
== NULL
) && (!tldap_parse_search_entry(msg
))) {
1979 *attributes
= msg
->attribs
;
1980 *num_attributes
= talloc_array_length(msg
->attribs
);
1984 static bool tldap_decode_controls(struct tldap_req_state
*state
)
1986 struct tldap_message
*msg
= state
->result
;
1987 struct asn1_data
*data
= msg
->data
;
1988 struct tldap_control
*sctrls
= NULL
;
1989 int num_controls
= 0;
1991 msg
->res_sctrls
= NULL
;
1993 if (!asn1_peek_tag(data
, ASN1_CONTEXT(0))) {
1997 asn1_start_tag(data
, ASN1_CONTEXT(0));
1999 while (asn1_peek_tag(data
, ASN1_SEQUENCE(0))) {
2000 struct tldap_control
*c
;
2003 sctrls
= talloc_realloc(msg
, sctrls
, struct tldap_control
,
2005 if (sctrls
== NULL
) {
2008 c
= &sctrls
[num_controls
];
2010 asn1_start_tag(data
, ASN1_SEQUENCE(0));
2011 asn1_read_OctetString_talloc(msg
, data
, &oid
);
2012 if ((data
->has_error
) || (oid
== NULL
)) {
2016 if (asn1_peek_tag(data
, ASN1_BOOLEAN
)) {
2017 asn1_read_BOOLEAN(data
, &c
->critical
);
2019 c
->critical
= false;
2021 c
->value
= data_blob_null
;
2022 if (asn1_peek_tag(data
, ASN1_OCTET_STRING
) &&
2023 !asn1_read_OctetString(data
, msg
, &c
->value
)) {
2026 asn1_end_tag(data
); /* ASN1_SEQUENCE(0) */
2031 asn1_end_tag(data
); /* ASN1_CONTEXT(0) */
2033 if (data
->has_error
) {
2034 TALLOC_FREE(sctrls
);
2037 msg
->res_sctrls
= sctrls
;
2041 static void tldap_simple_done(struct tevent_req
*subreq
, int type
)
2043 struct tevent_req
*req
= tevent_req_callback_data(
2044 subreq
, struct tevent_req
);
2045 struct tldap_req_state
*state
= tevent_req_data(
2046 req
, struct tldap_req_state
);
2049 err
= tldap_msg_recv(subreq
, state
, &state
->result
);
2050 TALLOC_FREE(subreq
);
2051 if (err
!= TLDAP_SUCCESS
) {
2052 tevent_req_error(req
, err
);
2055 if (state
->result
->type
!= type
) {
2056 tevent_req_error(req
, TLDAP_PROTOCOL_ERROR
);
2059 if (!asn1_start_tag(state
->result
->data
, state
->result
->type
) ||
2060 !tldap_decode_response(state
) ||
2061 !asn1_end_tag(state
->result
->data
) ||
2062 !tldap_decode_controls(state
)) {
2063 tevent_req_error(req
, TLDAP_DECODING_ERROR
);
2066 if (state
->result
->lderr
!= TLDAP_SUCCESS
) {
2067 tevent_req_error(req
, state
->result
->lderr
);
2070 tevent_req_done(req
);
2073 static int tldap_simple_recv(struct tevent_req
*req
)
2076 if (tevent_req_is_ldap_error(req
, &err
)) {
2079 return TLDAP_SUCCESS
;
2082 static void tldap_add_done(struct tevent_req
*subreq
);
2084 struct tevent_req
*tldap_add_send(TALLOC_CTX
*mem_ctx
,
2085 struct tevent_context
*ev
,
2086 struct tldap_context
*ld
,
2088 struct tldap_mod
*attributes
,
2090 struct tldap_control
*sctrls
,
2092 struct tldap_control
*cctrls
,
2095 struct tevent_req
*req
, *subreq
;
2096 struct tldap_req_state
*state
;
2099 req
= tldap_req_create(mem_ctx
, ld
, &state
);
2104 asn1_push_tag(state
->out
, TLDAP_REQ_ADD
);
2105 asn1_write_OctetString(state
->out
, dn
, strlen(dn
));
2106 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2108 for (i
=0; i
<num_attributes
; i
++) {
2109 struct tldap_mod
*attrib
= &attributes
[i
];
2110 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2111 asn1_write_OctetString(state
->out
, attrib
->attribute
,
2112 strlen(attrib
->attribute
));
2113 asn1_push_tag(state
->out
, ASN1_SET
);
2114 for (j
=0; j
<attrib
->num_values
; j
++) {
2115 asn1_write_OctetString(state
->out
,
2116 attrib
->values
[j
].data
,
2117 attrib
->values
[j
].length
);
2119 asn1_pop_tag(state
->out
);
2120 asn1_pop_tag(state
->out
);
2123 asn1_pop_tag(state
->out
);
2124 asn1_pop_tag(state
->out
);
2126 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
2127 sctrls
, num_sctrls
);
2128 if (tevent_req_nomem(subreq
, req
)) {
2129 return tevent_req_post(req
, ev
);
2131 tevent_req_set_callback(subreq
, tldap_add_done
, req
);
2135 static void tldap_add_done(struct tevent_req
*subreq
)
2137 tldap_simple_done(subreq
, TLDAP_RES_ADD
);
2140 int tldap_add_recv(struct tevent_req
*req
)
2142 return tldap_simple_recv(req
);
2145 int tldap_add(struct tldap_context
*ld
, const char *dn
,
2146 struct tldap_mod
*attributes
, int num_attributes
,
2147 struct tldap_control
*sctrls
, int num_sctrls
,
2148 struct tldap_control
*cctrls
, int num_cctrls
)
2150 TALLOC_CTX
*frame
= talloc_stackframe();
2151 struct tevent_context
*ev
;
2152 struct tevent_req
*req
;
2155 ev
= samba_tevent_context_init(frame
);
2157 result
= TLDAP_NO_MEMORY
;
2161 req
= tldap_add_send(frame
, ev
, ld
, dn
, attributes
, num_attributes
,
2162 sctrls
, num_sctrls
, cctrls
, num_cctrls
);
2164 result
= TLDAP_NO_MEMORY
;
2168 if (!tevent_req_poll(req
, ev
)) {
2169 result
= TLDAP_OPERATIONS_ERROR
;
2173 result
= tldap_add_recv(req
);
2174 tldap_save_msg(ld
, req
);
2180 static void tldap_modify_done(struct tevent_req
*subreq
);
2182 struct tevent_req
*tldap_modify_send(TALLOC_CTX
*mem_ctx
,
2183 struct tevent_context
*ev
,
2184 struct tldap_context
*ld
,
2186 struct tldap_mod
*mods
, int num_mods
,
2187 struct tldap_control
*sctrls
,
2189 struct tldap_control
*cctrls
,
2192 struct tevent_req
*req
, *subreq
;
2193 struct tldap_req_state
*state
;
2196 req
= tldap_req_create(mem_ctx
, ld
, &state
);
2201 asn1_push_tag(state
->out
, TLDAP_REQ_MODIFY
);
2202 asn1_write_OctetString(state
->out
, dn
, strlen(dn
));
2203 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2205 for (i
=0; i
<num_mods
; i
++) {
2206 struct tldap_mod
*mod
= &mods
[i
];
2207 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2208 asn1_write_enumerated(state
->out
, mod
->mod_op
),
2209 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2210 asn1_write_OctetString(state
->out
, mod
->attribute
,
2211 strlen(mod
->attribute
));
2212 asn1_push_tag(state
->out
, ASN1_SET
);
2213 for (j
=0; j
<mod
->num_values
; j
++) {
2214 asn1_write_OctetString(state
->out
,
2215 mod
->values
[j
].data
,
2216 mod
->values
[j
].length
);
2218 asn1_pop_tag(state
->out
);
2219 asn1_pop_tag(state
->out
);
2220 asn1_pop_tag(state
->out
);
2223 asn1_pop_tag(state
->out
);
2224 asn1_pop_tag(state
->out
);
2226 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
2227 sctrls
, num_sctrls
);
2228 if (tevent_req_nomem(subreq
, req
)) {
2229 return tevent_req_post(req
, ev
);
2231 tevent_req_set_callback(subreq
, tldap_modify_done
, req
);
2235 static void tldap_modify_done(struct tevent_req
*subreq
)
2237 tldap_simple_done(subreq
, TLDAP_RES_MODIFY
);
2240 int tldap_modify_recv(struct tevent_req
*req
)
2242 return tldap_simple_recv(req
);
2245 int tldap_modify(struct tldap_context
*ld
, const char *dn
,
2246 struct tldap_mod
*mods
, int num_mods
,
2247 struct tldap_control
*sctrls
, int num_sctrls
,
2248 struct tldap_control
*cctrls
, int num_cctrls
)
2250 TALLOC_CTX
*frame
= talloc_stackframe();
2251 struct tevent_context
*ev
;
2252 struct tevent_req
*req
;
2255 ev
= samba_tevent_context_init(frame
);
2257 result
= TLDAP_NO_MEMORY
;
2261 req
= tldap_modify_send(frame
, ev
, ld
, dn
, mods
, num_mods
,
2262 sctrls
, num_sctrls
, cctrls
, num_cctrls
);
2264 result
= TLDAP_NO_MEMORY
;
2268 if (!tevent_req_poll(req
, ev
)) {
2269 result
= TLDAP_OPERATIONS_ERROR
;
2273 result
= tldap_modify_recv(req
);
2274 tldap_save_msg(ld
, req
);
2280 static void tldap_delete_done(struct tevent_req
*subreq
);
2282 struct tevent_req
*tldap_delete_send(TALLOC_CTX
*mem_ctx
,
2283 struct tevent_context
*ev
,
2284 struct tldap_context
*ld
,
2286 struct tldap_control
*sctrls
,
2288 struct tldap_control
*cctrls
,
2291 struct tevent_req
*req
, *subreq
;
2292 struct tldap_req_state
*state
;
2294 req
= tldap_req_create(mem_ctx
, ld
, &state
);
2299 asn1_push_tag(state
->out
, TLDAP_REQ_DELETE
);
2300 asn1_write(state
->out
, dn
, strlen(dn
));
2301 asn1_pop_tag(state
->out
);
2303 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
2304 sctrls
, num_sctrls
);
2305 if (tevent_req_nomem(subreq
, req
)) {
2306 return tevent_req_post(req
, ev
);
2308 tevent_req_set_callback(subreq
, tldap_delete_done
, req
);
2312 static void tldap_delete_done(struct tevent_req
*subreq
)
2314 tldap_simple_done(subreq
, TLDAP_RES_DELETE
);
2317 int tldap_delete_recv(struct tevent_req
*req
)
2319 return tldap_simple_recv(req
);
2322 int tldap_delete(struct tldap_context
*ld
, const char *dn
,
2323 struct tldap_control
*sctrls
, int num_sctrls
,
2324 struct tldap_control
*cctrls
, int num_cctrls
)
2326 TALLOC_CTX
*frame
= talloc_stackframe();
2327 struct tevent_context
*ev
;
2328 struct tevent_req
*req
;
2331 ev
= samba_tevent_context_init(frame
);
2333 result
= TLDAP_NO_MEMORY
;
2337 req
= tldap_delete_send(frame
, ev
, ld
, dn
, sctrls
, num_sctrls
,
2338 cctrls
, num_cctrls
);
2340 result
= TLDAP_NO_MEMORY
;
2344 if (!tevent_req_poll(req
, ev
)) {
2345 result
= TLDAP_OPERATIONS_ERROR
;
2349 result
= tldap_delete_recv(req
);
2350 tldap_save_msg(ld
, req
);
2356 int tldap_msg_id(const struct tldap_message
*msg
)
2361 int tldap_msg_type(const struct tldap_message
*msg
)
2366 const char *tldap_msg_matcheddn(struct tldap_message
*msg
)
2371 return msg
->res_matcheddn
;
2374 const char *tldap_msg_diagnosticmessage(struct tldap_message
*msg
)
2379 return msg
->res_diagnosticmessage
;
2382 const char *tldap_msg_referral(struct tldap_message
*msg
)
2387 return msg
->res_referral
;
2390 void tldap_msg_sctrls(struct tldap_message
*msg
, int *num_sctrls
,
2391 struct tldap_control
**sctrls
)
2398 *sctrls
= msg
->res_sctrls
;
2399 *num_sctrls
= talloc_array_length(msg
->res_sctrls
);
2402 struct tldap_message
*tldap_ctx_lastmsg(struct tldap_context
*ld
)
2404 return ld
->last_msg
;
2407 const char *tldap_err2string(int rc
)
2409 const char *res
= NULL
;
2412 * This would normally be a table, but the error codes are not fully
2413 * sequential. Let the compiler figure out the optimum implementation
2419 res
= "TLDAP_SUCCESS";
2421 case TLDAP_OPERATIONS_ERROR
:
2422 res
= "TLDAP_OPERATIONS_ERROR";
2424 case TLDAP_PROTOCOL_ERROR
:
2425 res
= "TLDAP_PROTOCOL_ERROR";
2427 case TLDAP_TIMELIMIT_EXCEEDED
:
2428 res
= "TLDAP_TIMELIMIT_EXCEEDED";
2430 case TLDAP_SIZELIMIT_EXCEEDED
:
2431 res
= "TLDAP_SIZELIMIT_EXCEEDED";
2433 case TLDAP_COMPARE_FALSE
:
2434 res
= "TLDAP_COMPARE_FALSE";
2436 case TLDAP_COMPARE_TRUE
:
2437 res
= "TLDAP_COMPARE_TRUE";
2439 case TLDAP_STRONG_AUTH_NOT_SUPPORTED
:
2440 res
= "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2442 case TLDAP_STRONG_AUTH_REQUIRED
:
2443 res
= "TLDAP_STRONG_AUTH_REQUIRED";
2445 case TLDAP_REFERRAL
:
2446 res
= "TLDAP_REFERRAL";
2448 case TLDAP_ADMINLIMIT_EXCEEDED
:
2449 res
= "TLDAP_ADMINLIMIT_EXCEEDED";
2451 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION
:
2452 res
= "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2454 case TLDAP_CONFIDENTIALITY_REQUIRED
:
2455 res
= "TLDAP_CONFIDENTIALITY_REQUIRED";
2457 case TLDAP_SASL_BIND_IN_PROGRESS
:
2458 res
= "TLDAP_SASL_BIND_IN_PROGRESS";
2460 case TLDAP_NO_SUCH_ATTRIBUTE
:
2461 res
= "TLDAP_NO_SUCH_ATTRIBUTE";
2463 case TLDAP_UNDEFINED_TYPE
:
2464 res
= "TLDAP_UNDEFINED_TYPE";
2466 case TLDAP_INAPPROPRIATE_MATCHING
:
2467 res
= "TLDAP_INAPPROPRIATE_MATCHING";
2469 case TLDAP_CONSTRAINT_VIOLATION
:
2470 res
= "TLDAP_CONSTRAINT_VIOLATION";
2472 case TLDAP_TYPE_OR_VALUE_EXISTS
:
2473 res
= "TLDAP_TYPE_OR_VALUE_EXISTS";
2475 case TLDAP_INVALID_SYNTAX
:
2476 res
= "TLDAP_INVALID_SYNTAX";
2478 case TLDAP_NO_SUCH_OBJECT
:
2479 res
= "TLDAP_NO_SUCH_OBJECT";
2481 case TLDAP_ALIAS_PROBLEM
:
2482 res
= "TLDAP_ALIAS_PROBLEM";
2484 case TLDAP_INVALID_DN_SYNTAX
:
2485 res
= "TLDAP_INVALID_DN_SYNTAX";
2488 res
= "TLDAP_IS_LEAF";
2490 case TLDAP_ALIAS_DEREF_PROBLEM
:
2491 res
= "TLDAP_ALIAS_DEREF_PROBLEM";
2493 case TLDAP_INAPPROPRIATE_AUTH
:
2494 res
= "TLDAP_INAPPROPRIATE_AUTH";
2496 case TLDAP_INVALID_CREDENTIALS
:
2497 res
= "TLDAP_INVALID_CREDENTIALS";
2499 case TLDAP_INSUFFICIENT_ACCESS
:
2500 res
= "TLDAP_INSUFFICIENT_ACCESS";
2505 case TLDAP_UNAVAILABLE
:
2506 res
= "TLDAP_UNAVAILABLE";
2508 case TLDAP_UNWILLING_TO_PERFORM
:
2509 res
= "TLDAP_UNWILLING_TO_PERFORM";
2511 case TLDAP_LOOP_DETECT
:
2512 res
= "TLDAP_LOOP_DETECT";
2514 case TLDAP_NAMING_VIOLATION
:
2515 res
= "TLDAP_NAMING_VIOLATION";
2517 case TLDAP_OBJECT_CLASS_VIOLATION
:
2518 res
= "TLDAP_OBJECT_CLASS_VIOLATION";
2520 case TLDAP_NOT_ALLOWED_ON_NONLEAF
:
2521 res
= "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2523 case TLDAP_NOT_ALLOWED_ON_RDN
:
2524 res
= "TLDAP_NOT_ALLOWED_ON_RDN";
2526 case TLDAP_ALREADY_EXISTS
:
2527 res
= "TLDAP_ALREADY_EXISTS";
2529 case TLDAP_NO_OBJECT_CLASS_MODS
:
2530 res
= "TLDAP_NO_OBJECT_CLASS_MODS";
2532 case TLDAP_RESULTS_TOO_LARGE
:
2533 res
= "TLDAP_RESULTS_TOO_LARGE";
2535 case TLDAP_AFFECTS_MULTIPLE_DSAS
:
2536 res
= "TLDAP_AFFECTS_MULTIPLE_DSAS";
2539 res
= "TLDAP_OTHER";
2541 case TLDAP_SERVER_DOWN
:
2542 res
= "TLDAP_SERVER_DOWN";
2544 case TLDAP_LOCAL_ERROR
:
2545 res
= "TLDAP_LOCAL_ERROR";
2547 case TLDAP_ENCODING_ERROR
:
2548 res
= "TLDAP_ENCODING_ERROR";
2550 case TLDAP_DECODING_ERROR
:
2551 res
= "TLDAP_DECODING_ERROR";
2554 res
= "TLDAP_TIMEOUT";
2556 case TLDAP_AUTH_UNKNOWN
:
2557 res
= "TLDAP_AUTH_UNKNOWN";
2559 case TLDAP_FILTER_ERROR
:
2560 res
= "TLDAP_FILTER_ERROR";
2562 case TLDAP_USER_CANCELLED
:
2563 res
= "TLDAP_USER_CANCELLED";
2565 case TLDAP_PARAM_ERROR
:
2566 res
= "TLDAP_PARAM_ERROR";
2568 case TLDAP_NO_MEMORY
:
2569 res
= "TLDAP_NO_MEMORY";
2571 case TLDAP_CONNECT_ERROR
:
2572 res
= "TLDAP_CONNECT_ERROR";
2574 case TLDAP_NOT_SUPPORTED
:
2575 res
= "TLDAP_NOT_SUPPORTED";
2577 case TLDAP_CONTROL_NOT_FOUND
:
2578 res
= "TLDAP_CONTROL_NOT_FOUND";
2580 case TLDAP_NO_RESULTS_RETURNED
:
2581 res
= "TLDAP_NO_RESULTS_RETURNED";
2583 case TLDAP_MORE_RESULTS_TO_RETURN
:
2584 res
= "TLDAP_MORE_RESULTS_TO_RETURN";
2586 case TLDAP_CLIENT_LOOP
:
2587 res
= "TLDAP_CLIENT_LOOP";
2589 case TLDAP_REFERRAL_LIMIT_EXCEEDED
:
2590 res
= "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2593 res
= talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2598 res
= "Unknown LDAP Error";