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/>.
23 bool tevent_req_is_ldap_error(struct tevent_req
*req
, int *perr
)
25 enum tevent_req_state state
;
28 if (!tevent_req_is_error(req
, &state
, &err
)) {
32 case TEVENT_REQ_TIMED_OUT
:
33 *perr
= TLDAP_TIMEOUT
;
35 case TEVENT_REQ_NO_MEMORY
:
36 *perr
= TLDAP_NO_MEMORY
;
38 case TEVENT_REQ_USER_ERROR
:
42 *perr
= TLDAP_OPERATIONS_ERROR
;
48 struct tldap_ctx_attribute
{
53 struct tldap_context
{
58 struct tstream_context
*conn
;
61 struct tevent_queue
*outgoing
;
62 struct tevent_req
**pending
;
64 /* For the sync wrappers we need something like get_last_error... */
65 struct tldap_message
*last_msg
;
68 void (*log_fn
)(void *context
, enum tldap_debug_level level
,
69 const char *fmt
, va_list ap
);
72 struct tldap_ctx_attribute
*ctx_attrs
;
75 struct tldap_message
{
76 struct asn1_data
*data
;
83 struct tldap_attribute
*attribs
;
85 /* Error data sent by the server */
88 char *res_diagnosticmessage
;
90 struct tldap_control
*res_sctrls
;
92 /* Controls sent by the server */
93 struct tldap_control
*ctrls
;
96 void tldap_set_debug(struct tldap_context
*ld
,
97 void (*log_fn
)(void *log_private
,
98 enum tldap_debug_level level
,
100 va_list ap
) PRINTF_ATTRIBUTE(3,0),
104 ld
->log_private
= log_private
;
107 static void tldap_debug(struct tldap_context
*ld
,
108 enum tldap_debug_level level
,
109 const char *fmt
, ...)
115 if (ld
->log_fn
== NULL
) {
119 ld
->log_fn(ld
->log_private
, level
, fmt
, ap
);
123 static int tldap_next_msgid(struct tldap_context
*ld
)
127 result
= ld
->msgid
++;
128 if (ld
->msgid
== 2147483647) {
134 struct tldap_context
*tldap_context_create(TALLOC_CTX
*mem_ctx
, int fd
)
136 struct tldap_context
*ctx
;
139 ctx
= talloc_zero(mem_ctx
, struct tldap_context
);
143 ret
= tstream_bsd_existing_socket(ctx
, fd
, &ctx
->conn
);
150 ctx
->outgoing
= tevent_queue_create(ctx
, "tldap_outgoing");
151 if (ctx
->outgoing
== NULL
) {
158 bool tldap_connection_ok(struct tldap_context
*ld
)
163 return !ld
->server_down
;
166 static struct tldap_ctx_attribute
*tldap_context_findattr(
167 struct tldap_context
*ld
, const char *name
)
171 num_attrs
= talloc_array_length(ld
->ctx_attrs
);
173 for (i
=0; i
<num_attrs
; i
++) {
174 if (strcmp(ld
->ctx_attrs
[i
].name
, name
) == 0) {
175 return &ld
->ctx_attrs
[i
];
181 bool tldap_context_setattr(struct tldap_context
*ld
,
182 const char *name
, const void *_pptr
)
184 struct tldap_ctx_attribute
*tmp
, *attr
;
187 void **pptr
= (void **)_pptr
;
189 attr
= tldap_context_findattr(ld
, name
);
192 * We don't actually delete attrs, we don't expect tons of
193 * attributes being shuffled around.
195 TALLOC_FREE(attr
->ptr
);
197 attr
->ptr
= talloc_move(ld
->ctx_attrs
, pptr
);
203 tmpname
= talloc_strdup(ld
, name
);
204 if (tmpname
== NULL
) {
208 num_attrs
= talloc_array_length(ld
->ctx_attrs
);
210 tmp
= talloc_realloc(ld
, ld
->ctx_attrs
, struct tldap_ctx_attribute
,
213 TALLOC_FREE(tmpname
);
216 tmp
[num_attrs
].name
= talloc_move(tmp
, &tmpname
);
218 tmp
[num_attrs
].ptr
= talloc_move(tmp
, pptr
);
220 tmp
[num_attrs
].ptr
= NULL
;
227 void *tldap_context_getattr(struct tldap_context
*ld
, const char *name
)
229 struct tldap_ctx_attribute
*attr
= tldap_context_findattr(ld
, name
);
237 struct read_ldap_state
{
242 static ssize_t
read_ldap_more(uint8_t *buf
, size_t buflen
, void *private_data
);
243 static void read_ldap_done(struct tevent_req
*subreq
);
245 static struct tevent_req
*read_ldap_send(TALLOC_CTX
*mem_ctx
,
246 struct tevent_context
*ev
,
247 struct tstream_context
*conn
)
249 struct tevent_req
*req
, *subreq
;
250 struct read_ldap_state
*state
;
252 req
= tevent_req_create(mem_ctx
, &state
, struct read_ldap_state
);
258 subreq
= tstream_read_packet_send(state
, ev
, conn
, 2, read_ldap_more
,
260 if (tevent_req_nomem(subreq
, req
)) {
261 return tevent_req_post(req
, ev
);
263 tevent_req_set_callback(subreq
, read_ldap_done
, req
);
267 static ssize_t
read_ldap_more(uint8_t *buf
, size_t buflen
, void *private_data
)
269 struct read_ldap_state
*state
= talloc_get_type_abort(
270 private_data
, struct read_ldap_state
);
275 /* We've been here, we're done */
280 * From ldap.h: LDAP_TAG_MESSAGE is 0x30
282 if (buf
[0] != 0x30) {
287 if ((len
& 0x80) == 0) {
292 lensize
= (len
& 0x7f);
296 /* Please get us the full length */
299 if (buflen
> 2 + lensize
) {
303 if (buflen
!= 2 + lensize
) {
307 for (i
=0; i
<lensize
; i
++) {
308 len
= (len
<< 8) | buf
[2+i
];
313 static void read_ldap_done(struct tevent_req
*subreq
)
315 struct tevent_req
*req
= tevent_req_callback_data(
316 subreq
, struct tevent_req
);
317 struct read_ldap_state
*state
= tevent_req_data(
318 req
, struct read_ldap_state
);
322 nread
= tstream_read_packet_recv(subreq
, state
, &state
->buf
, &err
);
325 tevent_req_error(req
, err
);
328 tevent_req_done(req
);
331 static ssize_t
read_ldap_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
332 uint8_t **pbuf
, int *perrno
)
334 struct read_ldap_state
*state
= tevent_req_data(
335 req
, struct read_ldap_state
);
337 if (tevent_req_is_unix_error(req
, perrno
)) {
340 *pbuf
= talloc_move(mem_ctx
, &state
->buf
);
341 return talloc_get_size(*pbuf
);
344 struct tldap_msg_state
{
345 struct tldap_context
*ld
;
346 struct tevent_context
*ev
;
350 struct asn1_data
*data
;
354 static void tldap_push_controls(struct asn1_data
*data
,
355 struct tldap_control
*sctrls
,
360 if ((sctrls
== NULL
) || (num_sctrls
== 0)) {
364 asn1_push_tag(data
, ASN1_CONTEXT(0));
366 for (i
=0; i
<num_sctrls
; i
++) {
367 struct tldap_control
*c
= &sctrls
[i
];
368 asn1_push_tag(data
, ASN1_SEQUENCE(0));
369 asn1_write_OctetString(data
, c
->oid
, strlen(c
->oid
));
371 asn1_write_BOOLEAN(data
, true);
373 if (c
->value
.data
!= NULL
) {
374 asn1_write_OctetString(data
, c
->value
.data
,
377 asn1_pop_tag(data
); /* ASN1_SEQUENCE(0) */
380 asn1_pop_tag(data
); /* ASN1_CONTEXT(0) */
383 static void tldap_msg_sent(struct tevent_req
*subreq
);
384 static void tldap_msg_received(struct tevent_req
*subreq
);
386 static struct tevent_req
*tldap_msg_send(TALLOC_CTX
*mem_ctx
,
387 struct tevent_context
*ev
,
388 struct tldap_context
*ld
,
389 int id
, struct asn1_data
*data
,
390 struct tldap_control
*sctrls
,
393 struct tevent_req
*req
, *subreq
;
394 struct tldap_msg_state
*state
;
397 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "tldap_msg_send: sending msg %d\n",
400 req
= tevent_req_create(mem_ctx
, &state
, struct tldap_msg_state
);
408 if (state
->ld
->server_down
) {
409 tevent_req_error(req
, TLDAP_SERVER_DOWN
);
410 return tevent_req_post(req
, ev
);
413 tldap_push_controls(data
, sctrls
, num_sctrls
);
417 if (!asn1_blob(data
, &blob
)) {
418 tevent_req_error(req
, TLDAP_ENCODING_ERROR
);
419 return tevent_req_post(req
, ev
);
422 state
->iov
.iov_base
= blob
.data
;
423 state
->iov
.iov_len
= blob
.length
;
425 subreq
= tstream_writev_queue_send(state
, ev
, ld
->conn
, ld
->outgoing
,
427 if (tevent_req_nomem(subreq
, req
)) {
428 return tevent_req_post(req
, ev
);
430 tevent_req_set_callback(subreq
, tldap_msg_sent
, req
);
434 static void tldap_msg_unset_pending(struct tevent_req
*req
)
436 struct tldap_msg_state
*state
= tevent_req_data(
437 req
, struct tldap_msg_state
);
438 struct tldap_context
*ld
= state
->ld
;
439 int num_pending
= talloc_array_length(ld
->pending
);
442 if (num_pending
== 1) {
443 TALLOC_FREE(ld
->pending
);
447 for (i
=0; i
<num_pending
; i
++) {
448 if (req
== ld
->pending
[i
]) {
452 if (i
== num_pending
) {
454 * Something's seriously broken. Just returning here is the
455 * right thing nevertheless, the point of this routine is to
456 * remove ourselves from cli->pending.
462 * Remove ourselves from the cli->pending array
464 if (num_pending
> 1) {
465 ld
->pending
[i
] = ld
->pending
[num_pending
-1];
469 * No NULL check here, we're shrinking by sizeof(void *), and
470 * talloc_realloc just adjusts the size for this.
472 ld
->pending
= talloc_realloc(NULL
, ld
->pending
, struct tevent_req
*,
477 static int tldap_msg_destructor(struct tevent_req
*req
)
479 tldap_msg_unset_pending(req
);
483 static bool tldap_msg_set_pending(struct tevent_req
*req
)
485 struct tldap_msg_state
*state
= tevent_req_data(
486 req
, struct tldap_msg_state
);
487 struct tldap_context
*ld
;
488 struct tevent_req
**pending
;
490 struct tevent_req
*subreq
;
493 num_pending
= talloc_array_length(ld
->pending
);
495 pending
= talloc_realloc(ld
, ld
->pending
, struct tevent_req
*,
497 if (pending
== NULL
) {
500 pending
[num_pending
] = req
;
501 ld
->pending
= pending
;
502 talloc_set_destructor(req
, tldap_msg_destructor
);
504 if (num_pending
> 0) {
509 * We're the first ones, add the read_ldap request that waits for the
510 * answer from the server
512 subreq
= read_ldap_send(ld
->pending
, state
->ev
, ld
->conn
);
513 if (subreq
== NULL
) {
514 tldap_msg_unset_pending(req
);
517 tevent_req_set_callback(subreq
, tldap_msg_received
, ld
);
521 static void tldap_msg_sent(struct tevent_req
*subreq
)
523 struct tevent_req
*req
= tevent_req_callback_data(
524 subreq
, struct tevent_req
);
525 struct tldap_msg_state
*state
= tevent_req_data(
526 req
, struct tldap_msg_state
);
530 nwritten
= tstream_writev_queue_recv(subreq
, &err
);
532 if (nwritten
== -1) {
533 state
->ld
->server_down
= true;
534 tevent_req_error(req
, TLDAP_SERVER_DOWN
);
538 if (!tldap_msg_set_pending(req
)) {
539 tevent_req_nomem(NULL
, req
);
544 static int tldap_msg_msgid(struct tevent_req
*req
)
546 struct tldap_msg_state
*state
= tevent_req_data(
547 req
, struct tldap_msg_state
);
552 static void tldap_msg_received(struct tevent_req
*subreq
)
554 struct tldap_context
*ld
= tevent_req_callback_data(
555 subreq
, struct tldap_context
);
556 struct tevent_req
*req
;
557 struct tldap_msg_state
*state
;
558 struct tevent_context
*ev
;
559 struct asn1_data
*data
;
568 received
= read_ldap_recv(subreq
, talloc_tos(), &inbuf
, &err
);
570 if (received
== -1) {
571 status
= TLDAP_SERVER_DOWN
;
575 data
= asn1_init(talloc_tos());
577 status
= TLDAP_NO_MEMORY
;
580 asn1_load_nocopy(data
, inbuf
, received
);
583 ok
&= asn1_start_tag(data
, ASN1_SEQUENCE(0));
584 ok
&= asn1_read_Integer(data
, &id
);
585 ok
&= asn1_peek_uint8(data
, &type
);
588 status
= TLDAP_PROTOCOL_ERROR
;
592 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "tldap_msg_received: got msg %d "
593 "type %d\n", id
, (int)type
);
595 num_pending
= talloc_array_length(ld
->pending
);
597 for (i
=0; i
<num_pending
; i
++) {
598 if (id
== tldap_msg_msgid(ld
->pending
[i
])) {
602 if (i
== num_pending
) {
603 /* Dump unexpected reply */
604 tldap_debug(ld
, TLDAP_DEBUG_WARNING
, "tldap_msg_received: "
605 "No request pending for msg %d\n", id
);
611 req
= ld
->pending
[i
];
612 state
= tevent_req_data(req
, struct tldap_msg_state
);
614 state
->inbuf
= talloc_move(state
, &inbuf
);
615 state
->data
= talloc_move(state
, &data
);
619 talloc_set_destructor(req
, NULL
);
620 tldap_msg_unset_pending(req
);
621 num_pending
= talloc_array_length(ld
->pending
);
623 tevent_req_done(req
);
626 if (num_pending
== 0) {
629 if (talloc_array_length(ld
->pending
) > num_pending
) {
631 * The callback functions called from tevent_req_done() above
632 * have put something on the pending queue. We don't have to
633 * trigger the read_ldap_send(), tldap_msg_set_pending() has
634 * done it for us already.
639 state
= tevent_req_data(ld
->pending
[0], struct tldap_msg_state
);
640 subreq
= read_ldap_send(ld
->pending
, state
->ev
, ld
->conn
);
641 if (subreq
== NULL
) {
642 status
= TLDAP_NO_MEMORY
;
645 tevent_req_set_callback(subreq
, tldap_msg_received
, ld
);
649 while (talloc_array_length(ld
->pending
) > 0) {
650 req
= ld
->pending
[0];
651 talloc_set_destructor(req
, NULL
);
652 tldap_msg_destructor(req
);
653 tevent_req_error(req
, status
);
657 static int tldap_msg_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
658 struct tldap_message
**pmsg
)
660 struct tldap_msg_state
*state
= tevent_req_data(
661 req
, struct tldap_msg_state
);
662 struct tldap_message
*msg
;
666 if (tevent_req_is_ldap_error(req
, &err
)) {
670 if (!asn1_peek_uint8(state
->data
, &msgtype
)) {
671 return TLDAP_PROTOCOL_ERROR
;
675 return TLDAP_SUCCESS
;
678 msg
= talloc_zero(mem_ctx
, struct tldap_message
);
680 return TLDAP_NO_MEMORY
;
684 msg
->inbuf
= talloc_move(msg
, &state
->inbuf
);
685 msg
->data
= talloc_move(msg
, &state
->data
);
689 return TLDAP_SUCCESS
;
692 struct tldap_req_state
{
694 struct asn1_data
*out
;
695 struct tldap_message
*result
;
698 static struct tevent_req
*tldap_req_create(TALLOC_CTX
*mem_ctx
,
699 struct tldap_context
*ld
,
700 struct tldap_req_state
**pstate
)
702 struct tevent_req
*req
;
703 struct tldap_req_state
*state
;
705 req
= tevent_req_create(mem_ctx
, &state
, struct tldap_req_state
);
710 state
->out
= asn1_init(state
);
711 if (state
->out
== NULL
) {
715 state
->result
= NULL
;
716 state
->id
= tldap_next_msgid(ld
);
718 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
719 asn1_write_Integer(state
->out
, state
->id
);
725 static void tldap_save_msg(struct tldap_context
*ld
, struct tevent_req
*req
)
727 struct tldap_req_state
*state
= tevent_req_data(
728 req
, struct tldap_req_state
);
730 TALLOC_FREE(ld
->last_msg
);
731 ld
->last_msg
= talloc_move(ld
, &state
->result
);
734 static char *blob2string_talloc(TALLOC_CTX
*mem_ctx
, DATA_BLOB blob
)
736 char *result
= talloc_array(mem_ctx
, char, blob
.length
+1);
737 memcpy(result
, blob
.data
, blob
.length
);
738 result
[blob
.length
] = '\0';
742 static bool asn1_read_OctetString_talloc(TALLOC_CTX
*mem_ctx
,
743 struct asn1_data
*data
,
747 if (!asn1_read_OctetString(data
, mem_ctx
, &string
))
749 *result
= blob2string_talloc(mem_ctx
, string
);
750 data_blob_free(&string
);
754 static bool tldap_decode_controls(struct tldap_req_state
*state
);
756 static bool tldap_decode_response(struct tldap_req_state
*state
)
758 struct asn1_data
*data
= state
->result
->data
;
759 struct tldap_message
*msg
= state
->result
;
762 ok
&= asn1_read_enumerated(data
, &msg
->lderr
);
763 ok
&= asn1_read_OctetString_talloc(msg
, data
, &msg
->res_matcheddn
);
764 ok
&= asn1_read_OctetString_talloc(msg
, data
,
765 &msg
->res_diagnosticmessage
);
766 if (asn1_peek_tag(data
, ASN1_CONTEXT(3))) {
767 ok
&= asn1_start_tag(data
, ASN1_CONTEXT(3));
768 ok
&= asn1_read_OctetString_talloc(msg
, data
,
770 ok
&= asn1_end_tag(data
);
772 msg
->res_referral
= NULL
;
778 static void tldap_sasl_bind_done(struct tevent_req
*subreq
);
780 struct tevent_req
*tldap_sasl_bind_send(TALLOC_CTX
*mem_ctx
,
781 struct tevent_context
*ev
,
782 struct tldap_context
*ld
,
784 const char *mechanism
,
786 struct tldap_control
*sctrls
,
788 struct tldap_control
*cctrls
,
791 struct tevent_req
*req
, *subreq
;
792 struct tldap_req_state
*state
;
794 req
= tldap_req_create(mem_ctx
, ld
, &state
);
803 asn1_push_tag(state
->out
, TLDAP_REQ_BIND
);
804 asn1_write_Integer(state
->out
, ld
->ld_version
);
805 asn1_write_OctetString(state
->out
, dn
, (dn
!= NULL
) ? strlen(dn
) : 0);
807 if (mechanism
== NULL
) {
808 asn1_push_tag(state
->out
, ASN1_CONTEXT_SIMPLE(0));
809 asn1_write(state
->out
, creds
->data
, creds
->length
);
810 asn1_pop_tag(state
->out
);
812 asn1_push_tag(state
->out
, ASN1_CONTEXT(3));
813 asn1_write_OctetString(state
->out
, mechanism
,
815 if ((creds
!= NULL
) && (creds
->data
!= NULL
)) {
816 asn1_write_OctetString(state
->out
, creds
->data
,
819 asn1_pop_tag(state
->out
);
822 if (!asn1_pop_tag(state
->out
)) {
823 tevent_req_error(req
, TLDAP_ENCODING_ERROR
);
824 return tevent_req_post(req
, ev
);
827 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
829 if (tevent_req_nomem(subreq
, req
)) {
830 return tevent_req_post(req
, ev
);
832 tevent_req_set_callback(subreq
, tldap_sasl_bind_done
, req
);
836 static void tldap_sasl_bind_done(struct tevent_req
*subreq
)
838 struct tevent_req
*req
= tevent_req_callback_data(
839 subreq
, struct tevent_req
);
840 struct tldap_req_state
*state
= tevent_req_data(
841 req
, struct tldap_req_state
);
844 err
= tldap_msg_recv(subreq
, state
, &state
->result
);
846 if (err
!= TLDAP_SUCCESS
) {
847 tevent_req_error(req
, err
);
850 if (state
->result
->type
!= TLDAP_RES_BIND
) {
851 tevent_req_error(req
, TLDAP_PROTOCOL_ERROR
);
854 if (!asn1_start_tag(state
->result
->data
, state
->result
->type
) ||
855 !tldap_decode_response(state
) ||
856 !asn1_end_tag(state
->result
->data
)) {
857 tevent_req_error(req
, TLDAP_DECODING_ERROR
);
861 * TODO: pull the reply blob
863 if (state
->result
->lderr
!= TLDAP_SUCCESS
) {
864 tevent_req_error(req
, state
->result
->lderr
);
867 tevent_req_done(req
);
870 int tldap_sasl_bind_recv(struct tevent_req
*req
)
874 if (tevent_req_is_ldap_error(req
, &err
)) {
877 return TLDAP_SUCCESS
;
880 int tldap_sasl_bind(struct tldap_context
*ld
,
882 const char *mechanism
,
884 struct tldap_control
*sctrls
,
886 struct tldap_control
*cctrls
,
889 TALLOC_CTX
*frame
= talloc_stackframe();
890 struct tevent_context
*ev
;
891 struct tevent_req
*req
;
894 ev
= event_context_init(frame
);
896 result
= TLDAP_NO_MEMORY
;
900 req
= tldap_sasl_bind_send(frame
, ev
, ld
, dn
, mechanism
, creds
,
901 sctrls
, num_sctrls
, cctrls
, num_cctrls
);
903 result
= TLDAP_NO_MEMORY
;
907 if (!tevent_req_poll(req
, ev
)) {
908 result
= TLDAP_OPERATIONS_ERROR
;
912 result
= tldap_sasl_bind_recv(req
);
913 tldap_save_msg(ld
, req
);
919 struct tevent_req
*tldap_simple_bind_send(TALLOC_CTX
*mem_ctx
,
920 struct tevent_context
*ev
,
921 struct tldap_context
*ld
,
927 if (passwd
!= NULL
) {
928 cred
.data
= (uint8_t *)passwd
;
929 cred
.length
= strlen(passwd
);
931 cred
.data
= (uint8_t *)"";
934 return tldap_sasl_bind_send(mem_ctx
, ev
, ld
, dn
, NULL
, &cred
, NULL
, 0,
938 int tldap_simple_bind_recv(struct tevent_req
*req
)
940 return tldap_sasl_bind_recv(req
);
943 int tldap_simple_bind(struct tldap_context
*ld
, const char *dn
,
948 if (passwd
!= NULL
) {
949 cred
.data
= (uint8_t *)passwd
;
950 cred
.length
= strlen(passwd
);
952 cred
.data
= (uint8_t *)"";
955 return tldap_sasl_bind(ld
, dn
, NULL
, &cred
, NULL
, 0, NULL
, 0);
958 /*****************************************************************************/
960 /* can't use isalpha() as only a strict set is valid for LDAP */
962 static bool tldap_is_alpha(char c
)
964 return (((c
>= 'a') && (c
<= 'z')) || \
965 ((c
>= 'A') && (c
<= 'Z')));
968 static bool tldap_is_adh(char c
)
970 return tldap_is_alpha(c
) || isdigit(c
) || (c
== '-');
973 #define TLDAP_FILTER_AND ASN1_CONTEXT(0)
974 #define TLDAP_FILTER_OR ASN1_CONTEXT(1)
975 #define TLDAP_FILTER_NOT ASN1_CONTEXT(2)
976 #define TLDAP_FILTER_EQ ASN1_CONTEXT(3)
977 #define TLDAP_FILTER_SUB ASN1_CONTEXT(4)
978 #define TLDAP_FILTER_LE ASN1_CONTEXT(5)
979 #define TLDAP_FILTER_GE ASN1_CONTEXT(6)
980 #define TLDAP_FILTER_PRES ASN1_CONTEXT_SIMPLE(7)
981 #define TLDAP_FILTER_APX ASN1_CONTEXT(8)
982 #define TLDAP_FILTER_EXT ASN1_CONTEXT(9)
984 #define TLDAP_SUB_INI ASN1_CONTEXT_SIMPLE(0)
985 #define TLDAP_SUB_ANY ASN1_CONTEXT_SIMPLE(1)
986 #define TLDAP_SUB_FIN ASN1_CONTEXT_SIMPLE(2)
989 /* oid's should be numerical only in theory,
990 * but apparently some broken servers may have alphanum aliases instead.
991 * Do like openldap libraries and allow alphanum aliases for oids, but
992 * do not allow Tagging options in that case.
994 static bool tldap_is_attrdesc(const char *s
, int len
, bool no_tagopts
)
1000 /* first char has stricter rules */
1003 } else if (!tldap_is_alpha(*s
)) {
1004 /* bad first char */
1008 for (i
= 1; i
< len
; i
++) {
1011 if (isdigit(s
[i
])) {
1024 if (tldap_is_adh(s
[i
])) {
1031 /* no tagging options */
1038 if ((i
+ 1) == len
) {
1056 /* this function copies the value until the closing parenthesis is found. */
1057 static char *tldap_get_val(TALLOC_CTX
*memctx
,
1058 const char *value
, const char **_s
)
1060 const char *s
= value
;
1062 /* find terminator */
1065 if (s
&& (*(s
- 1) == '\\')) {
1070 if (!s
|| !(*s
== ')')) {
1071 /* malformed filter */
1077 return talloc_strndup(memctx
, value
, s
- value
);
1080 static int tldap_hex2char(const char *x
)
1082 if (isxdigit(x
[0]) && isxdigit(x
[1])) {
1083 const char h1
= x
[0], h2
= x
[1];
1086 if (h1
>= 'a') c
= h1
- (int)'a' + 10;
1087 else if (h1
>= 'A') c
= h1
- (int)'A' + 10;
1088 else if (h1
>= '0') c
= h1
- (int)'0';
1090 if (h2
>= 'a') c
+= h2
- (int)'a' + 10;
1091 else if (h2
>= 'A') c
+= h2
- (int)'A' + 10;
1092 else if (h2
>= '0') c
+= h2
- (int)'0';
1100 static bool tldap_find_first_star(const char *val
, const char **star
)
1104 for (s
= val
; *s
; s
++) {
1107 if (isxdigit(s
[1]) && isxdigit(s
[2])) {
1111 /* not hex based escape, check older syntax */
1120 /* invalid escape sequence */
1125 /* end of val, nothing found */
1135 /* string ended without closing parenthesis, filter is malformed */
1139 static bool tldap_unescape_inplace(char *value
, size_t *val_len
)
1143 for (i
= 0,p
= 0; i
< *val_len
; i
++) {
1149 /* these must be escaped */
1153 if (!value
[i
+ 1]) {
1159 c
= tldap_hex2char(&value
[i
]);
1160 if (c
>= 0 && c
< 256) {
1172 value
[p
] = value
[i
];
1181 value
[p
] = value
[i
];
1190 static bool tldap_push_filter_basic(struct tldap_context
*ld
,
1191 struct asn1_data
*data
,
1193 static bool tldap_push_filter_substring(struct tldap_context
*ld
,
1194 struct asn1_data
*data
,
1197 static bool tldap_push_filter_int(struct tldap_context
*ld
,
1198 struct asn1_data
*data
,
1201 const char *s
= *_s
;
1205 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1206 "Incomplete or malformed filter\n");
1211 /* we are right after a parenthesis,
1212 * find out what op we have at hand */
1215 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "Filter op: AND\n");
1216 asn1_push_tag(data
, TLDAP_FILTER_AND
);
1221 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "Filter op: OR\n");
1222 asn1_push_tag(data
, TLDAP_FILTER_OR
);
1227 tldap_debug(ld
, TLDAP_DEBUG_TRACE
, "Filter op: NOT\n");
1228 asn1_push_tag(data
, TLDAP_FILTER_NOT
);
1230 ret
= tldap_push_filter_int(ld
, data
, &s
);
1239 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1240 "Invalid parenthesis '%c'\n", *s
);
1244 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1245 "Invalid filter termination\n");
1249 ret
= tldap_push_filter_basic(ld
, data
, &s
);
1256 /* only and/or filters get here.
1257 * go through the list of filters */
1260 /* RFC 4526: empty and/or */
1266 ret
= tldap_push_filter_int(ld
, data
, &s
);
1272 /* end of list, return */
1280 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1281 "Incomplete or malformed filter\n");
1286 if (data
->has_error
) {
1295 static bool tldap_push_filter_basic(struct tldap_context
*ld
,
1296 struct asn1_data
*data
,
1299 TALLOC_CTX
*tmpctx
= talloc_tos();
1300 const char *s
= *_s
;
1311 bool write_octect
= true;
1314 eq
= strchr(s
, '=');
1316 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1317 "Invalid filter, missing equal sign\n");
1326 asn1_push_tag(data
, TLDAP_FILTER_LE
);
1330 asn1_push_tag(data
, TLDAP_FILTER_GE
);
1334 asn1_push_tag(data
, TLDAP_FILTER_APX
);
1338 asn1_push_tag(data
, TLDAP_FILTER_EXT
);
1339 write_octect
= false;
1345 if (*s
== ':') { /* [:dn]:rule:= value */
1347 /* malformed filter */
1351 } else { /* type[:dn][:rule]:= value */
1353 dn
= strchr(s
, ':');
1354 type_len
= dn
- type
;
1355 if (dn
== e
) { /* type:= value */
1362 rule
= strchr(dn
, ':');
1363 if ((rule
== dn
+ 1) || rule
+ 1 == e
) {
1364 /* malformed filter, contains "::" */
1368 if (StrnCaseCmp(dn
, "dn:", 3) != 0) {
1373 /* malformed filter. With two
1374 * optionals, the first must be "dn"
1387 if (!type
&& !dn
&& !rule
) {
1388 /* malformed filter, there must be at least one */
1393 MatchingRuleAssertion ::= SEQUENCE {
1394 matchingRule [1] MatchingRuleID OPTIONAL,
1395 type [2] AttributeDescription OPTIONAL,
1396 matchValue [3] AssertionValue,
1397 dnAttributes [4] BOOLEAN DEFAULT FALSE
1401 /* check and add rule */
1403 ret
= tldap_is_attrdesc(rule
, e
- rule
, true);
1407 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(1));
1408 asn1_write(data
, rule
, e
- rule
);
1412 /* check and add type */
1414 ret
= tldap_is_attrdesc(type
, type_len
, false);
1418 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(2));
1419 asn1_write(data
, type
, type_len
);
1423 uval
= tldap_get_val(tmpctx
, val
, _s
);
1427 uval_len
= *_s
- val
;
1428 ret
= tldap_unescape_inplace(uval
, &uval_len
);
1433 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(3));
1434 asn1_write(data
, uval
, uval_len
);
1437 asn1_push_tag(data
, ASN1_CONTEXT_SIMPLE(4));
1438 asn1_write_uint8(data
, dn
?1:0);
1445 ret
= tldap_is_attrdesc(s
, e
- s
, false);
1450 if (strncmp(val
, "*)", 2) == 0) {
1452 asn1_push_tag(data
, TLDAP_FILTER_PRES
);
1453 asn1_write(data
, s
, e
- s
);
1455 write_octect
= false;
1459 ret
= tldap_find_first_star(val
, &star
);
1465 asn1_push_tag(data
, TLDAP_FILTER_SUB
);
1466 asn1_write_OctetString(data
, s
, e
- s
);
1467 ret
= tldap_push_filter_substring(ld
, data
, val
, &s
);
1472 write_octect
= false;
1476 /* if nothing else, then it is just equality */
1477 asn1_push_tag(data
, TLDAP_FILTER_EQ
);
1478 write_octect
= true;
1483 uval
= tldap_get_val(tmpctx
, val
, _s
);
1487 uval_len
= *_s
- val
;
1488 ret
= tldap_unescape_inplace(uval
, &uval_len
);
1493 asn1_write_OctetString(data
, s
, e
- s
);
1494 asn1_write_OctetString(data
, uval
, uval_len
);
1497 if (data
->has_error
) {
1504 static bool tldap_push_filter_substring(struct tldap_context
*ld
,
1505 struct asn1_data
*data
,
1509 TALLOC_CTX
*tmpctx
= talloc_tos();
1510 bool initial
= true;
1517 SubstringFilter ::= SEQUENCE {
1518 type AttributeDescription,
1519 -- at least one must be present
1520 substrings SEQUENCE OF CHOICE {
1521 initial [0] LDAPString,
1523 final [2] LDAPString } }
1525 asn1_push_tag(data
, ASN1_SEQUENCE(0));
1528 ret
= tldap_find_first_star(val
, &star
);
1532 chunk_len
= star
- val
;
1536 if (!initial
&& chunk_len
== 0) {
1537 /* found '**', which is illegal */
1553 if (initial
&& chunk_len
== 0) {
1559 chunk
= talloc_strndup(tmpctx
, val
, chunk_len
);
1563 ret
= tldap_unescape_inplace(chunk
, &chunk_len
);
1570 asn1_push_tag(data
, TLDAP_SUB_INI
);
1573 asn1_push_tag(data
, TLDAP_SUB_ANY
);
1577 asn1_push_tag(data
, TLDAP_SUB_FIN
);
1583 asn1_write(data
, chunk
, chunk_len
);
1588 } while (*star
== '*');
1592 /* end of sequence */
1597 /* NOTE: although openldap libraries allow for spaces in some places, mosly
1598 * around parenthesis, we do not allow any spaces (except in values of
1599 * course) as I couldn't fine any place in RFC 4512 or RFC 4515 where
1600 * leading or trailing spaces where allowed.
1602 static bool tldap_push_filter(struct tldap_context
*ld
,
1603 struct asn1_data
*data
,
1606 const char *s
= filter
;
1609 ret
= tldap_push_filter_int(ld
, data
, &s
);
1611 tldap_debug(ld
, TLDAP_DEBUG_ERROR
,
1612 "Incomplete or malformed filter\n");
1618 /*****************************************************************************/
1620 static void tldap_search_done(struct tevent_req
*subreq
);
1622 struct tevent_req
*tldap_search_send(TALLOC_CTX
*mem_ctx
,
1623 struct tevent_context
*ev
,
1624 struct tldap_context
*ld
,
1625 const char *base
, int scope
,
1630 struct tldap_control
*sctrls
,
1632 struct tldap_control
*cctrls
,
1638 struct tevent_req
*req
, *subreq
;
1639 struct tldap_req_state
*state
;
1642 req
= tldap_req_create(mem_ctx
, ld
, &state
);
1647 asn1_push_tag(state
->out
, TLDAP_REQ_SEARCH
);
1648 asn1_write_OctetString(state
->out
, base
, strlen(base
));
1649 asn1_write_enumerated(state
->out
, scope
);
1650 asn1_write_enumerated(state
->out
, deref
);
1651 asn1_write_Integer(state
->out
, sizelimit
);
1652 asn1_write_Integer(state
->out
, timelimit
);
1653 asn1_write_BOOLEAN(state
->out
, attrsonly
);
1655 if (!tldap_push_filter(ld
, state
->out
, filter
)) {
1656 goto encoding_error
;
1659 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
1660 for (i
=0; i
<num_attrs
; i
++) {
1661 asn1_write_OctetString(state
->out
, attrs
[i
], strlen(attrs
[i
]));
1663 asn1_pop_tag(state
->out
);
1664 asn1_pop_tag(state
->out
);
1666 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
1667 sctrls
, num_sctrls
);
1668 if (tevent_req_nomem(subreq
, req
)) {
1669 return tevent_req_post(req
, ev
);
1671 tevent_req_set_callback(subreq
, tldap_search_done
, req
);
1675 tevent_req_error(req
, TLDAP_ENCODING_ERROR
);
1676 return tevent_req_post(req
, ev
);
1679 static void tldap_search_done(struct tevent_req
*subreq
)
1681 struct tevent_req
*req
= tevent_req_callback_data(
1682 subreq
, struct tevent_req
);
1683 struct tldap_req_state
*state
= tevent_req_data(
1684 req
, struct tldap_req_state
);
1687 err
= tldap_msg_recv(subreq
, state
, &state
->result
);
1688 if (err
!= TLDAP_SUCCESS
) {
1689 tevent_req_error(req
, err
);
1692 switch (state
->result
->type
) {
1693 case TLDAP_RES_SEARCH_ENTRY
:
1694 case TLDAP_RES_SEARCH_REFERENCE
:
1695 tevent_req_notify_callback(req
);
1696 if (!tldap_msg_set_pending(subreq
)) {
1697 tevent_req_nomem(NULL
, req
);
1701 case TLDAP_RES_SEARCH_RESULT
:
1702 TALLOC_FREE(subreq
);
1703 if (!asn1_start_tag(state
->result
->data
,
1704 state
->result
->type
) ||
1705 !tldap_decode_response(state
) ||
1706 !asn1_end_tag(state
->result
->data
) ||
1707 !tldap_decode_controls(state
)) {
1708 tevent_req_error(req
, TLDAP_DECODING_ERROR
);
1711 tevent_req_done(req
);
1714 tevent_req_error(req
, TLDAP_PROTOCOL_ERROR
);
1719 int tldap_search_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
1720 struct tldap_message
**pmsg
)
1722 struct tldap_req_state
*state
= tevent_req_data(
1723 req
, struct tldap_req_state
);
1726 if (!tevent_req_is_in_progress(req
)
1727 && tevent_req_is_ldap_error(req
, &err
)) {
1731 if (tevent_req_is_in_progress(req
)) {
1732 switch (state
->result
->type
) {
1733 case TLDAP_RES_SEARCH_ENTRY
:
1734 case TLDAP_RES_SEARCH_REFERENCE
:
1737 return TLDAP_OPERATIONS_ERROR
;
1741 *pmsg
= talloc_move(mem_ctx
, &state
->result
);
1742 return TLDAP_SUCCESS
;
1745 struct tldap_sync_search_state
{
1746 TALLOC_CTX
*mem_ctx
;
1747 struct tldap_message
**entries
;
1748 struct tldap_message
**refs
;
1752 static void tldap_search_cb(struct tevent_req
*req
)
1754 struct tldap_sync_search_state
*state
=
1755 (struct tldap_sync_search_state
*)
1756 tevent_req_callback_data_void(req
);
1757 struct tldap_message
*msg
, **tmp
;
1758 int rc
, num_entries
, num_refs
;
1760 rc
= tldap_search_recv(req
, talloc_tos(), &msg
);
1761 if (rc
!= TLDAP_SUCCESS
) {
1766 switch (tldap_msg_type(msg
)) {
1767 case TLDAP_RES_SEARCH_ENTRY
:
1768 num_entries
= talloc_array_length(state
->entries
);
1769 tmp
= talloc_realloc(state
->mem_ctx
, state
->entries
,
1770 struct tldap_message
*, num_entries
+ 1);
1772 state
->rc
= TLDAP_NO_MEMORY
;
1775 state
->entries
= tmp
;
1776 state
->entries
[num_entries
] = talloc_move(state
->entries
,
1779 case TLDAP_RES_SEARCH_REFERENCE
:
1780 num_refs
= talloc_array_length(state
->refs
);
1781 tmp
= talloc_realloc(state
->mem_ctx
, state
->refs
,
1782 struct tldap_message
*, num_refs
+ 1);
1784 state
->rc
= TLDAP_NO_MEMORY
;
1788 state
->refs
[num_refs
] = talloc_move(state
->refs
, &msg
);
1790 case TLDAP_RES_SEARCH_RESULT
:
1791 state
->rc
= TLDAP_SUCCESS
;
1794 state
->rc
= TLDAP_PROTOCOL_ERROR
;
1799 int tldap_search(struct tldap_context
*ld
,
1800 const char *base
, int scope
, const char *filter
,
1801 const char **attrs
, int num_attrs
, int attrsonly
,
1802 struct tldap_control
*sctrls
, int num_sctrls
,
1803 struct tldap_control
*cctrls
, int num_cctrls
,
1804 int timelimit
, int sizelimit
, int deref
,
1805 TALLOC_CTX
*mem_ctx
, struct tldap_message
***entries
,
1806 struct tldap_message
***refs
)
1808 TALLOC_CTX
*frame
= talloc_stackframe();
1809 struct tevent_context
*ev
;
1810 struct tevent_req
*req
;
1811 struct tldap_sync_search_state state
;
1814 state
.mem_ctx
= mem_ctx
;
1815 state
.rc
= TLDAP_SUCCESS
;
1817 ev
= event_context_init(frame
);
1819 state
.rc
= TLDAP_NO_MEMORY
;
1823 req
= tldap_search_send(frame
, ev
, ld
, base
, scope
, filter
,
1824 attrs
, num_attrs
, attrsonly
,
1825 sctrls
, num_sctrls
, cctrls
, num_cctrls
,
1826 timelimit
, sizelimit
, deref
);
1828 state
.rc
= TLDAP_NO_MEMORY
;
1832 tevent_req_set_callback(req
, tldap_search_cb
, &state
);
1834 if (!tevent_req_is_in_progress(req
)) {
1835 /* an error happend before sending */
1836 if (tevent_req_is_ldap_error(req
, &state
.rc
)) {
1841 while (tevent_req_is_in_progress(req
)
1842 && (state
.rc
== TLDAP_SUCCESS
)) {
1843 if (tevent_loop_once(ev
) == -1) {
1844 return TLDAP_OPERATIONS_ERROR
;
1848 if (state
.rc
!= TLDAP_SUCCESS
) {
1852 if (entries
!= NULL
) {
1853 *entries
= state
.entries
;
1855 TALLOC_FREE(state
.entries
);
1860 TALLOC_FREE(state
.refs
);
1862 tldap_save_msg(ld
, req
);
1868 static bool tldap_parse_search_entry(struct tldap_message
*msg
)
1870 int num_attribs
= 0;
1872 asn1_start_tag(msg
->data
, msg
->type
);
1876 asn1_read_OctetString_talloc(msg
, msg
->data
, &msg
->dn
);
1877 if (msg
->dn
== NULL
) {
1882 * Attributes: We overallocate msg->attribs by one, so that while
1883 * looping over the attributes we can directly parse into the last
1884 * array element. Same for the values in the inner loop.
1887 msg
->attribs
= talloc_array(msg
, struct tldap_attribute
, 1);
1888 if (msg
->attribs
== NULL
) {
1892 asn1_start_tag(msg
->data
, ASN1_SEQUENCE(0));
1893 while (asn1_peek_tag(msg
->data
, ASN1_SEQUENCE(0))) {
1894 struct tldap_attribute
*attrib
;
1897 attrib
= &msg
->attribs
[num_attribs
];
1898 attrib
->values
= talloc_array(msg
->attribs
, DATA_BLOB
, 1);
1899 if (attrib
->values
== NULL
) {
1902 asn1_start_tag(msg
->data
, ASN1_SEQUENCE(0));
1903 asn1_read_OctetString_talloc(msg
->attribs
, msg
->data
,
1905 asn1_start_tag(msg
->data
, ASN1_SET
);
1907 while (asn1_peek_tag(msg
->data
, ASN1_OCTET_STRING
)) {
1908 asn1_read_OctetString(msg
->data
, msg
,
1909 &attrib
->values
[num_values
]);
1911 attrib
->values
= talloc_realloc(
1912 msg
->attribs
, attrib
->values
, DATA_BLOB
,
1914 if (attrib
->values
== NULL
) {
1919 attrib
->values
= talloc_realloc(msg
->attribs
, attrib
->values
,
1920 DATA_BLOB
, num_values
);
1921 attrib
->num_values
= num_values
;
1923 asn1_end_tag(msg
->data
); /* ASN1_SET */
1924 asn1_end_tag(msg
->data
); /* ASN1_SEQUENCE(0) */
1925 msg
->attribs
= talloc_realloc(
1926 msg
, msg
->attribs
, struct tldap_attribute
,
1928 if (msg
->attribs
== NULL
) {
1933 msg
->attribs
= talloc_realloc(
1934 msg
, msg
->attribs
, struct tldap_attribute
, num_attribs
);
1935 asn1_end_tag(msg
->data
);
1936 if (msg
->data
->has_error
) {
1942 bool tldap_entry_dn(struct tldap_message
*msg
, char **dn
)
1944 if ((msg
->dn
== NULL
) && (!tldap_parse_search_entry(msg
))) {
1951 bool tldap_entry_attributes(struct tldap_message
*msg
, int *num_attributes
,
1952 struct tldap_attribute
**attributes
)
1954 if ((msg
->dn
== NULL
) && (!tldap_parse_search_entry(msg
))) {
1957 *attributes
= msg
->attribs
;
1958 *num_attributes
= talloc_array_length(msg
->attribs
);
1962 static bool tldap_decode_controls(struct tldap_req_state
*state
)
1964 struct tldap_message
*msg
= state
->result
;
1965 struct asn1_data
*data
= msg
->data
;
1966 struct tldap_control
*sctrls
= NULL
;
1967 int num_controls
= 0;
1969 msg
->res_sctrls
= NULL
;
1971 if (!asn1_peek_tag(data
, ASN1_CONTEXT(0))) {
1975 asn1_start_tag(data
, ASN1_CONTEXT(0));
1977 while (asn1_peek_tag(data
, ASN1_SEQUENCE(0))) {
1978 struct tldap_control
*c
;
1981 sctrls
= talloc_realloc(msg
, sctrls
, struct tldap_control
,
1983 if (sctrls
== NULL
) {
1986 c
= &sctrls
[num_controls
];
1988 asn1_start_tag(data
, ASN1_SEQUENCE(0));
1989 asn1_read_OctetString_talloc(msg
, data
, &oid
);
1990 if ((data
->has_error
) || (oid
== NULL
)) {
1994 if (asn1_peek_tag(data
, ASN1_BOOLEAN
)) {
1995 asn1_read_BOOLEAN(data
, &c
->critical
);
1997 c
->critical
= false;
1999 c
->value
= data_blob_null
;
2000 if (asn1_peek_tag(data
, ASN1_OCTET_STRING
) &&
2001 !asn1_read_OctetString(data
, msg
, &c
->value
)) {
2004 asn1_end_tag(data
); /* ASN1_SEQUENCE(0) */
2009 asn1_end_tag(data
); /* ASN1_CONTEXT(0) */
2011 if (data
->has_error
) {
2012 TALLOC_FREE(sctrls
);
2015 msg
->res_sctrls
= sctrls
;
2019 static void tldap_simple_done(struct tevent_req
*subreq
, int type
)
2021 struct tevent_req
*req
= tevent_req_callback_data(
2022 subreq
, struct tevent_req
);
2023 struct tldap_req_state
*state
= tevent_req_data(
2024 req
, struct tldap_req_state
);
2027 err
= tldap_msg_recv(subreq
, state
, &state
->result
);
2028 TALLOC_FREE(subreq
);
2029 if (err
!= TLDAP_SUCCESS
) {
2030 tevent_req_error(req
, err
);
2033 if (state
->result
->type
!= type
) {
2034 tevent_req_error(req
, TLDAP_PROTOCOL_ERROR
);
2037 if (!asn1_start_tag(state
->result
->data
, state
->result
->type
) ||
2038 !tldap_decode_response(state
) ||
2039 !asn1_end_tag(state
->result
->data
) ||
2040 !tldap_decode_controls(state
)) {
2041 tevent_req_error(req
, TLDAP_DECODING_ERROR
);
2044 if (state
->result
->lderr
!= TLDAP_SUCCESS
) {
2045 tevent_req_error(req
, state
->result
->lderr
);
2048 tevent_req_done(req
);
2051 static int tldap_simple_recv(struct tevent_req
*req
)
2054 if (tevent_req_is_ldap_error(req
, &err
)) {
2057 return TLDAP_SUCCESS
;
2060 static void tldap_add_done(struct tevent_req
*subreq
);
2062 struct tevent_req
*tldap_add_send(TALLOC_CTX
*mem_ctx
,
2063 struct tevent_context
*ev
,
2064 struct tldap_context
*ld
,
2066 struct tldap_mod
*attributes
,
2068 struct tldap_control
*sctrls
,
2070 struct tldap_control
*cctrls
,
2073 struct tevent_req
*req
, *subreq
;
2074 struct tldap_req_state
*state
;
2077 req
= tldap_req_create(mem_ctx
, ld
, &state
);
2082 asn1_push_tag(state
->out
, TLDAP_REQ_ADD
);
2083 asn1_write_OctetString(state
->out
, dn
, strlen(dn
));
2084 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2086 for (i
=0; i
<num_attributes
; i
++) {
2087 struct tldap_mod
*attrib
= &attributes
[i
];
2088 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2089 asn1_write_OctetString(state
->out
, attrib
->attribute
,
2090 strlen(attrib
->attribute
));
2091 asn1_push_tag(state
->out
, ASN1_SET
);
2092 for (j
=0; j
<attrib
->num_values
; j
++) {
2093 asn1_write_OctetString(state
->out
,
2094 attrib
->values
[j
].data
,
2095 attrib
->values
[j
].length
);
2097 asn1_pop_tag(state
->out
);
2098 asn1_pop_tag(state
->out
);
2101 asn1_pop_tag(state
->out
);
2102 asn1_pop_tag(state
->out
);
2104 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
2105 sctrls
, num_sctrls
);
2106 if (tevent_req_nomem(subreq
, req
)) {
2107 return tevent_req_post(req
, ev
);
2109 tevent_req_set_callback(subreq
, tldap_add_done
, req
);
2113 static void tldap_add_done(struct tevent_req
*subreq
)
2115 tldap_simple_done(subreq
, TLDAP_RES_ADD
);
2118 int tldap_add_recv(struct tevent_req
*req
)
2120 return tldap_simple_recv(req
);
2123 int tldap_add(struct tldap_context
*ld
, const char *dn
,
2124 int num_attributes
, struct tldap_mod
*attributes
,
2125 struct tldap_control
*sctrls
, int num_sctrls
,
2126 struct tldap_control
*cctrls
, int num_cctrls
)
2128 TALLOC_CTX
*frame
= talloc_stackframe();
2129 struct tevent_context
*ev
;
2130 struct tevent_req
*req
;
2133 ev
= event_context_init(frame
);
2135 result
= TLDAP_NO_MEMORY
;
2139 req
= tldap_add_send(frame
, ev
, ld
, dn
, attributes
, num_attributes
,
2140 sctrls
, num_sctrls
, cctrls
, num_cctrls
);
2142 result
= TLDAP_NO_MEMORY
;
2146 if (!tevent_req_poll(req
, ev
)) {
2147 result
= TLDAP_OPERATIONS_ERROR
;
2151 result
= tldap_add_recv(req
);
2152 tldap_save_msg(ld
, req
);
2158 static void tldap_modify_done(struct tevent_req
*subreq
);
2160 struct tevent_req
*tldap_modify_send(TALLOC_CTX
*mem_ctx
,
2161 struct tevent_context
*ev
,
2162 struct tldap_context
*ld
,
2164 int num_mods
, struct tldap_mod
*mods
,
2165 struct tldap_control
*sctrls
,
2167 struct tldap_control
*cctrls
,
2170 struct tevent_req
*req
, *subreq
;
2171 struct tldap_req_state
*state
;
2174 req
= tldap_req_create(mem_ctx
, ld
, &state
);
2179 asn1_push_tag(state
->out
, TLDAP_REQ_MODIFY
);
2180 asn1_write_OctetString(state
->out
, dn
, strlen(dn
));
2181 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2183 for (i
=0; i
<num_mods
; i
++) {
2184 struct tldap_mod
*mod
= &mods
[i
];
2185 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2186 asn1_write_enumerated(state
->out
, mod
->mod_op
),
2187 asn1_push_tag(state
->out
, ASN1_SEQUENCE(0));
2188 asn1_write_OctetString(state
->out
, mod
->attribute
,
2189 strlen(mod
->attribute
));
2190 asn1_push_tag(state
->out
, ASN1_SET
);
2191 for (j
=0; j
<mod
->num_values
; j
++) {
2192 asn1_write_OctetString(state
->out
,
2193 mod
->values
[j
].data
,
2194 mod
->values
[j
].length
);
2196 asn1_pop_tag(state
->out
);
2197 asn1_pop_tag(state
->out
);
2198 asn1_pop_tag(state
->out
);
2201 asn1_pop_tag(state
->out
);
2202 asn1_pop_tag(state
->out
);
2204 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
2205 sctrls
, num_sctrls
);
2206 if (tevent_req_nomem(subreq
, req
)) {
2207 return tevent_req_post(req
, ev
);
2209 tevent_req_set_callback(subreq
, tldap_modify_done
, req
);
2213 static void tldap_modify_done(struct tevent_req
*subreq
)
2215 tldap_simple_done(subreq
, TLDAP_RES_MODIFY
);
2218 int tldap_modify_recv(struct tevent_req
*req
)
2220 return tldap_simple_recv(req
);
2223 int tldap_modify(struct tldap_context
*ld
, const char *dn
,
2224 int num_mods
, struct tldap_mod
*mods
,
2225 struct tldap_control
*sctrls
, int num_sctrls
,
2226 struct tldap_control
*cctrls
, int num_cctrls
)
2228 TALLOC_CTX
*frame
= talloc_stackframe();
2229 struct tevent_context
*ev
;
2230 struct tevent_req
*req
;
2233 ev
= event_context_init(frame
);
2235 result
= TLDAP_NO_MEMORY
;
2239 req
= tldap_modify_send(frame
, ev
, ld
, dn
, num_mods
, mods
,
2240 sctrls
, num_sctrls
, cctrls
, num_cctrls
);
2242 result
= TLDAP_NO_MEMORY
;
2246 if (!tevent_req_poll(req
, ev
)) {
2247 result
= TLDAP_OPERATIONS_ERROR
;
2251 result
= tldap_modify_recv(req
);
2252 tldap_save_msg(ld
, req
);
2258 static void tldap_delete_done(struct tevent_req
*subreq
);
2260 struct tevent_req
*tldap_delete_send(TALLOC_CTX
*mem_ctx
,
2261 struct tevent_context
*ev
,
2262 struct tldap_context
*ld
,
2264 struct tldap_control
*sctrls
,
2266 struct tldap_control
*cctrls
,
2269 struct tevent_req
*req
, *subreq
;
2270 struct tldap_req_state
*state
;
2272 req
= tldap_req_create(mem_ctx
, ld
, &state
);
2277 asn1_push_tag(state
->out
, TLDAP_REQ_DELETE
);
2278 asn1_write(state
->out
, dn
, strlen(dn
));
2279 asn1_pop_tag(state
->out
);
2281 subreq
= tldap_msg_send(state
, ev
, ld
, state
->id
, state
->out
,
2282 sctrls
, num_sctrls
);
2283 if (tevent_req_nomem(subreq
, req
)) {
2284 return tevent_req_post(req
, ev
);
2286 tevent_req_set_callback(subreq
, tldap_delete_done
, req
);
2290 static void tldap_delete_done(struct tevent_req
*subreq
)
2292 tldap_simple_done(subreq
, TLDAP_RES_DELETE
);
2295 int tldap_delete_recv(struct tevent_req
*req
)
2297 return tldap_simple_recv(req
);
2300 int tldap_delete(struct tldap_context
*ld
, const char *dn
,
2301 struct tldap_control
*sctrls
, int num_sctrls
,
2302 struct tldap_control
*cctrls
, int num_cctrls
)
2304 TALLOC_CTX
*frame
= talloc_stackframe();
2305 struct tevent_context
*ev
;
2306 struct tevent_req
*req
;
2309 ev
= event_context_init(frame
);
2311 result
= TLDAP_NO_MEMORY
;
2315 req
= tldap_delete_send(frame
, ev
, ld
, dn
, sctrls
, num_sctrls
,
2316 cctrls
, num_cctrls
);
2318 result
= TLDAP_NO_MEMORY
;
2322 if (!tevent_req_poll(req
, ev
)) {
2323 result
= TLDAP_OPERATIONS_ERROR
;
2327 result
= tldap_delete_recv(req
);
2328 tldap_save_msg(ld
, req
);
2334 int tldap_msg_id(const struct tldap_message
*msg
)
2339 int tldap_msg_type(const struct tldap_message
*msg
)
2344 const char *tldap_msg_matcheddn(struct tldap_message
*msg
)
2349 return msg
->res_matcheddn
;
2352 const char *tldap_msg_diagnosticmessage(struct tldap_message
*msg
)
2357 return msg
->res_diagnosticmessage
;
2360 const char *tldap_msg_referral(struct tldap_message
*msg
)
2365 return msg
->res_referral
;
2368 void tldap_msg_sctrls(struct tldap_message
*msg
, int *num_sctrls
,
2369 struct tldap_control
**sctrls
)
2375 *sctrls
= msg
->res_sctrls
;
2376 *num_sctrls
= talloc_array_length(msg
->res_sctrls
);
2379 struct tldap_message
*tldap_ctx_lastmsg(struct tldap_context
*ld
)
2381 return ld
->last_msg
;
2384 const char *tldap_err2string(int rc
)
2386 const char *res
= NULL
;
2389 * This would normally be a table, but the error codes are not fully
2390 * sequential. Let the compiler figure out the optimum implementation
2396 res
= "TLDAP_SUCCESS";
2398 case TLDAP_OPERATIONS_ERROR
:
2399 res
= "TLDAP_OPERATIONS_ERROR";
2401 case TLDAP_PROTOCOL_ERROR
:
2402 res
= "TLDAP_PROTOCOL_ERROR";
2404 case TLDAP_TIMELIMIT_EXCEEDED
:
2405 res
= "TLDAP_TIMELIMIT_EXCEEDED";
2407 case TLDAP_SIZELIMIT_EXCEEDED
:
2408 res
= "TLDAP_SIZELIMIT_EXCEEDED";
2410 case TLDAP_COMPARE_FALSE
:
2411 res
= "TLDAP_COMPARE_FALSE";
2413 case TLDAP_COMPARE_TRUE
:
2414 res
= "TLDAP_COMPARE_TRUE";
2416 case TLDAP_STRONG_AUTH_NOT_SUPPORTED
:
2417 res
= "TLDAP_STRONG_AUTH_NOT_SUPPORTED";
2419 case TLDAP_STRONG_AUTH_REQUIRED
:
2420 res
= "TLDAP_STRONG_AUTH_REQUIRED";
2422 case TLDAP_REFERRAL
:
2423 res
= "TLDAP_REFERRAL";
2425 case TLDAP_ADMINLIMIT_EXCEEDED
:
2426 res
= "TLDAP_ADMINLIMIT_EXCEEDED";
2428 case TLDAP_UNAVAILABLE_CRITICAL_EXTENSION
:
2429 res
= "TLDAP_UNAVAILABLE_CRITICAL_EXTENSION";
2431 case TLDAP_CONFIDENTIALITY_REQUIRED
:
2432 res
= "TLDAP_CONFIDENTIALITY_REQUIRED";
2434 case TLDAP_SASL_BIND_IN_PROGRESS
:
2435 res
= "TLDAP_SASL_BIND_IN_PROGRESS";
2437 case TLDAP_NO_SUCH_ATTRIBUTE
:
2438 res
= "TLDAP_NO_SUCH_ATTRIBUTE";
2440 case TLDAP_UNDEFINED_TYPE
:
2441 res
= "TLDAP_UNDEFINED_TYPE";
2443 case TLDAP_INAPPROPRIATE_MATCHING
:
2444 res
= "TLDAP_INAPPROPRIATE_MATCHING";
2446 case TLDAP_CONSTRAINT_VIOLATION
:
2447 res
= "TLDAP_CONSTRAINT_VIOLATION";
2449 case TLDAP_TYPE_OR_VALUE_EXISTS
:
2450 res
= "TLDAP_TYPE_OR_VALUE_EXISTS";
2452 case TLDAP_INVALID_SYNTAX
:
2453 res
= "TLDAP_INVALID_SYNTAX";
2455 case TLDAP_NO_SUCH_OBJECT
:
2456 res
= "TLDAP_NO_SUCH_OBJECT";
2458 case TLDAP_ALIAS_PROBLEM
:
2459 res
= "TLDAP_ALIAS_PROBLEM";
2461 case TLDAP_INVALID_DN_SYNTAX
:
2462 res
= "TLDAP_INVALID_DN_SYNTAX";
2465 res
= "TLDAP_IS_LEAF";
2467 case TLDAP_ALIAS_DEREF_PROBLEM
:
2468 res
= "TLDAP_ALIAS_DEREF_PROBLEM";
2470 case TLDAP_INAPPROPRIATE_AUTH
:
2471 res
= "TLDAP_INAPPROPRIATE_AUTH";
2473 case TLDAP_INVALID_CREDENTIALS
:
2474 res
= "TLDAP_INVALID_CREDENTIALS";
2476 case TLDAP_INSUFFICIENT_ACCESS
:
2477 res
= "TLDAP_INSUFFICIENT_ACCESS";
2482 case TLDAP_UNAVAILABLE
:
2483 res
= "TLDAP_UNAVAILABLE";
2485 case TLDAP_UNWILLING_TO_PERFORM
:
2486 res
= "TLDAP_UNWILLING_TO_PERFORM";
2488 case TLDAP_LOOP_DETECT
:
2489 res
= "TLDAP_LOOP_DETECT";
2491 case TLDAP_NAMING_VIOLATION
:
2492 res
= "TLDAP_NAMING_VIOLATION";
2494 case TLDAP_OBJECT_CLASS_VIOLATION
:
2495 res
= "TLDAP_OBJECT_CLASS_VIOLATION";
2497 case TLDAP_NOT_ALLOWED_ON_NONLEAF
:
2498 res
= "TLDAP_NOT_ALLOWED_ON_NONLEAF";
2500 case TLDAP_NOT_ALLOWED_ON_RDN
:
2501 res
= "TLDAP_NOT_ALLOWED_ON_RDN";
2503 case TLDAP_ALREADY_EXISTS
:
2504 res
= "TLDAP_ALREADY_EXISTS";
2506 case TLDAP_NO_OBJECT_CLASS_MODS
:
2507 res
= "TLDAP_NO_OBJECT_CLASS_MODS";
2509 case TLDAP_RESULTS_TOO_LARGE
:
2510 res
= "TLDAP_RESULTS_TOO_LARGE";
2512 case TLDAP_AFFECTS_MULTIPLE_DSAS
:
2513 res
= "TLDAP_AFFECTS_MULTIPLE_DSAS";
2516 res
= "TLDAP_OTHER";
2518 case TLDAP_SERVER_DOWN
:
2519 res
= "TLDAP_SERVER_DOWN";
2521 case TLDAP_LOCAL_ERROR
:
2522 res
= "TLDAP_LOCAL_ERROR";
2524 case TLDAP_ENCODING_ERROR
:
2525 res
= "TLDAP_ENCODING_ERROR";
2527 case TLDAP_DECODING_ERROR
:
2528 res
= "TLDAP_DECODING_ERROR";
2531 res
= "TLDAP_TIMEOUT";
2533 case TLDAP_AUTH_UNKNOWN
:
2534 res
= "TLDAP_AUTH_UNKNOWN";
2536 case TLDAP_FILTER_ERROR
:
2537 res
= "TLDAP_FILTER_ERROR";
2539 case TLDAP_USER_CANCELLED
:
2540 res
= "TLDAP_USER_CANCELLED";
2542 case TLDAP_PARAM_ERROR
:
2543 res
= "TLDAP_PARAM_ERROR";
2545 case TLDAP_NO_MEMORY
:
2546 res
= "TLDAP_NO_MEMORY";
2548 case TLDAP_CONNECT_ERROR
:
2549 res
= "TLDAP_CONNECT_ERROR";
2551 case TLDAP_NOT_SUPPORTED
:
2552 res
= "TLDAP_NOT_SUPPORTED";
2554 case TLDAP_CONTROL_NOT_FOUND
:
2555 res
= "TLDAP_CONTROL_NOT_FOUND";
2557 case TLDAP_NO_RESULTS_RETURNED
:
2558 res
= "TLDAP_NO_RESULTS_RETURNED";
2560 case TLDAP_MORE_RESULTS_TO_RETURN
:
2561 res
= "TLDAP_MORE_RESULTS_TO_RETURN";
2563 case TLDAP_CLIENT_LOOP
:
2564 res
= "TLDAP_CLIENT_LOOP";
2566 case TLDAP_REFERRAL_LIMIT_EXCEEDED
:
2567 res
= "TLDAP_REFERRAL_LIMIT_EXCEEDED";
2570 res
= talloc_asprintf(talloc_tos(), "Unknown LDAP Error (%d)",
2575 res
= "Unknown LDAP Error";