2004-11-06 23:15:39 +03:00
/*
Unix SMB / CIFS mplementation .
LDAP protocol helper functions for SAMBA
Copyright ( C ) Andrew Tridgell 2004
Copyright ( C ) Volker Lendecke 2004
Copyright ( C ) Stefan Metzmacher 2004
Copyright ( C ) Simo Sorce 2004
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2004-11-06 23:15:39 +03:00
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2004-11-06 23:15:39 +03:00
*/
# include "includes.h"
2009-01-03 14:49:50 +03:00
# include <tevent.h>
# include "lib/socket/socket.h"
2015-06-19 13:26:06 +03:00
# include "lib/tsocket/tsocket.h"
# include "libcli/util/tstream.h"
2008-10-11 23:31:42 +04:00
# include "../lib/util/asn1.h"
# include "../lib/util/dlinklist.h"
2010-05-21 11:39:15 +04:00
# include "libcli/ldap/libcli_ldap.h"
2008-04-02 06:53:27 +04:00
# include "libcli/ldap/ldap_proto.h"
2005-06-16 09:39:40 +04:00
# include "libcli/ldap/ldap_client.h"
2005-10-24 13:34:12 +04:00
# include "libcli/composite/composite.h"
2006-05-03 00:15:47 +04:00
# include "lib/tls/tls.h"
2005-12-28 18:38:36 +03:00
# include "auth/gensec/gensec.h"
2006-04-25 15:50:32 +04:00
# include "system/time.h"
2007-12-03 02:28:22 +03:00
# include "param/param.h"
2007-12-10 20:41:19 +03:00
# include "libcli/resolve/resolve.h"
2004-11-06 23:15:39 +03:00
2015-06-19 13:26:06 +03:00
static void ldap_connection_dead ( struct ldap_connection * conn , NTSTATUS status ) ;
static int ldap_connection_destructor ( struct ldap_connection * conn )
{
/*
* NT_STATUS_OK means that callbacks of pending requests are not
* triggered
*/
ldap_connection_dead ( conn , NT_STATUS_OK ) ;
return 0 ;
}
2007-12-10 06:33:16 +03:00
/**
2005-06-16 09:39:40 +04:00
create a new ldap_connection stucture . The event context is optional
*/
2015-06-19 13:26:06 +03:00
2008-04-02 06:53:27 +04:00
_PUBLIC_ struct ldap_connection * ldap4_new_connection ( TALLOC_CTX * mem_ctx ,
2007-12-10 06:33:16 +03:00
struct loadparm_context * lp_ctx ,
2008-12-29 22:24:57 +03:00
struct tevent_context * ev )
2005-01-30 10:22:16 +03:00
{
2005-06-16 09:39:40 +04:00
struct ldap_connection * conn ;
2005-01-30 10:22:16 +03:00
2008-04-22 01:58:23 +04:00
if ( ev = = NULL ) {
2005-06-16 09:39:40 +04:00
return NULL ;
2005-01-30 10:22:16 +03:00
}
2008-04-22 01:58:23 +04:00
conn = talloc_zero ( mem_ctx , struct ldap_connection ) ;
if ( conn = = NULL ) {
return NULL ;
2005-01-30 10:22:16 +03:00
}
2005-06-16 09:39:40 +04:00
conn - > next_messageid = 1 ;
conn - > event . event_ctx = ev ;
2005-01-30 10:22:16 +03:00
2015-06-19 13:26:06 +03:00
conn - > sockets . send_queue = tevent_queue_create ( conn ,
" ldap_connection send_queue " ) ;
if ( conn - > sockets . send_queue = = NULL ) {
TALLOC_FREE ( conn ) ;
return NULL ;
}
2007-12-10 06:33:16 +03:00
conn - > lp_ctx = lp_ctx ;
2005-06-16 09:39:40 +04:00
/* set a reasonable request timeout */
conn - > timeout = 60 ;
2005-01-30 10:22:16 +03:00
2006-04-25 15:50:32 +04:00
/* explicitly avoid reconnections by default */
conn - > reconnect . max_retries = 0 ;
2015-06-19 13:26:06 +03:00
talloc_set_destructor ( conn , ldap_connection_destructor ) ;
2005-06-16 09:39:40 +04:00
return conn ;
2005-01-30 10:22:16 +03:00
}
2005-06-16 09:39:40 +04:00
/*
the connection is dead
*/
2015-06-19 13:26:06 +03:00
static void ldap_connection_dead ( struct ldap_connection * conn , NTSTATUS status )
2005-06-16 09:39:40 +04:00
{
struct ldap_request * req ;
2005-01-30 10:22:16 +03:00
2015-06-19 13:26:06 +03:00
tevent_queue_stop ( conn - > sockets . send_queue ) ;
TALLOC_FREE ( conn - > sockets . recv_subreq ) ;
conn - > sockets . active = NULL ;
TALLOC_FREE ( conn - > sockets . sasl ) ;
TALLOC_FREE ( conn - > sockets . tls ) ;
TALLOC_FREE ( conn - > sockets . raw ) ;
2008-09-24 09:37:16 +04:00
2006-04-25 15:50:32 +04:00
/* return an error for any pending request ... */
2005-06-16 09:39:40 +04:00
while ( conn - > pending ) {
req = conn - > pending ;
DLIST_REMOVE ( req - > conn - > pending , req ) ;
2015-06-19 13:26:06 +03:00
req - > conn = NULL ;
2005-06-16 09:39:40 +04:00
req - > state = LDAP_REQUEST_DONE ;
2015-06-19 13:26:06 +03:00
if ( NT_STATUS_IS_OK ( status ) ) {
continue ;
}
req - > status = status ;
2005-06-16 09:39:40 +04:00
if ( req - > async . fn ) {
req - > async . fn ( req ) ;
}
2006-05-03 00:15:47 +04:00
}
2005-01-30 10:22:16 +03:00
}
2006-04-25 15:50:32 +04:00
static void ldap_reconnect ( struct ldap_connection * conn ) ;
2005-11-10 03:28:02 +03:00
/*
handle packet errors
*/
2015-06-19 13:26:06 +03:00
static void ldap_error_handler ( struct ldap_connection * conn , NTSTATUS status )
2005-11-10 03:28:02 +03:00
{
2015-06-19 13:26:06 +03:00
ldap_connection_dead ( conn , status ) ;
2006-04-25 15:50:32 +04:00
/* but try to reconnect so that the ldb client can go on */
ldap_reconnect ( conn ) ;
2005-11-10 03:28:02 +03:00
}
2005-01-30 10:22:16 +03:00
2005-06-16 09:39:40 +04:00
/*
match up with a pending message , adding to the replies list
*/
static void ldap_match_message ( struct ldap_connection * conn , struct ldap_message * msg )
2004-11-06 23:15:39 +03:00
{
2005-06-16 09:39:40 +04:00
struct ldap_request * req ;
2007-11-29 10:00:04 +03:00
int i ;
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
for ( req = conn - > pending ; req ; req = req - > next ) {
if ( req - > messageid = = msg - > messageid ) break ;
2004-11-06 23:15:39 +03:00
}
2005-07-17 14:52:31 +04:00
/* match a zero message id to the last request sent.
It seems that servers send 0 if unable to parse */
if ( req = = NULL & & msg - > messageid = = 0 ) {
req = conn - > pending ;
}
2005-06-16 09:39:40 +04:00
if ( req = = NULL ) {
DEBUG ( 0 , ( " ldap: no matching message id for %u \n " ,
msg - > messageid ) ) ;
2015-06-19 13:26:06 +03:00
TALLOC_FREE ( msg ) ;
2005-06-16 09:39:40 +04:00
return ;
2004-11-06 23:15:39 +03:00
}
2007-11-29 10:00:04 +03:00
/* Check for undecoded critical extensions */
for ( i = 0 ; msg - > controls & & msg - > controls [ i ] ; i + + ) {
if ( ! msg - > controls_decoded [ i ] & &
msg - > controls [ i ] - > critical ) {
2015-06-19 13:26:06 +03:00
TALLOC_FREE ( msg ) ;
2007-11-29 10:00:04 +03:00
req - > status = NT_STATUS_LDAP ( LDAP_UNAVAILABLE_CRITICAL_EXTENSION ) ;
req - > state = LDAP_REQUEST_DONE ;
DLIST_REMOVE ( conn - > pending , req ) ;
if ( req - > async . fn ) {
req - > async . fn ( req ) ;
}
return ;
}
}
2005-06-16 09:39:40 +04:00
/* add to the list of replies received */
req - > replies = talloc_realloc ( req , req - > replies ,
struct ldap_message * , req - > num_replies + 1 ) ;
if ( req - > replies = = NULL ) {
2015-06-19 13:26:06 +03:00
TALLOC_FREE ( msg ) ;
2005-06-16 09:39:40 +04:00
req - > status = NT_STATUS_NO_MEMORY ;
req - > state = LDAP_REQUEST_DONE ;
DLIST_REMOVE ( conn - > pending , req ) ;
if ( req - > async . fn ) {
req - > async . fn ( req ) ;
}
return ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
req - > replies [ req - > num_replies ] = talloc_steal ( req - > replies , msg ) ;
req - > num_replies + + ;
2004-11-06 23:15:39 +03:00
2005-06-21 17:42:47 +04:00
if ( msg - > type ! = LDAP_TAG_SearchResultEntry & &
msg - > type ! = LDAP_TAG_SearchResultReference ) {
2005-06-16 09:39:40 +04:00
/* currently only search results expect multiple
replies */
req - > state = LDAP_REQUEST_DONE ;
DLIST_REMOVE ( conn - > pending , req ) ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
if ( req - > async . fn ) {
req - > async . fn ( req ) ;
}
2004-11-06 23:15:39 +03:00
}
2015-06-19 13:26:06 +03:00
static void ldap_connection_recv_done ( struct tevent_req * subreq ) ;
static void ldap_connection_recv_next ( struct ldap_connection * conn )
{
struct tevent_req * subreq = NULL ;
if ( conn - > sockets . recv_subreq ! = NULL ) {
return ;
}
if ( conn - > sockets . active = = NULL ) {
return ;
}
if ( conn - > pending = = NULL ) {
return ;
}
/*
2015-12-17 17:42:33 +03:00
* The minimum size of a LDAP pdu is 7 bytes
2015-06-19 13:26:06 +03:00
*
* dumpasn1 - hh ldap - unbind - min . dat
*
* < 30 05 02 01 09 42 00 >
* 0 5 : SEQUENCE {
* < 02 01 09 >
* 2 1 : INTEGER 9
* < 42 00 >
* 5 0 : [ APPLICATION 2 ]
* : Error : Object has zero length .
* : }
*
* dumpasn1 - hh ldap - unbind - windows . dat
*
* < 30 84 00 00 00 05 02 01 09 42 00 >
* 0 5 : SEQUENCE {
* < 02 01 09 >
* 6 1 : INTEGER 9
* < 42 00 >
* 9 0 : [ APPLICATION 2 ]
* : Error : Object has zero length .
* : }
*
* This means using an initial read size
* of 7 is ok .
*/
subreq = tstream_read_pdu_blob_send ( conn ,
conn - > event . event_ctx ,
conn - > sockets . active ,
7 , /* initial_read_size */
ldap_full_packet ,
conn ) ;
if ( subreq = = NULL ) {
ldap_error_handler ( conn , NT_STATUS_NO_MEMORY ) ;
return ;
}
tevent_req_set_callback ( subreq , ldap_connection_recv_done , conn ) ;
conn - > sockets . recv_subreq = subreq ;
return ;
}
2005-11-10 03:28:02 +03:00
2005-06-16 09:39:40 +04:00
/*
r17197: This patch moves the encryption of bulk data on SASL negotiated security
contexts from the application layer into the socket layer.
This improves a number of correctness aspects, as we now allow LDAP
packets to cross multiple SASL packets. It should also make it much
easier to write async LDAP tests from windows clients, as they use SASL
by default. It is also vital to allowing OpenLDAP clients to use GSSAPI
against Samba4, as it negotiates a rather small SASL buffer size.
This patch mirrors the earlier work done to move TLS into the socket
layer.
Unusual in this pstch is the extra read callback argument I take. As
SASL is a layer on top of a socket, it is entirely possible for the
SASL layer to drain a socket dry, but for the caller not to have read
all the decrypted data. This would leave the system without an event
to restart the read (as the socket is dry).
As such, I re-invoke the read handler from a timed callback, which
should trigger on the next running of the event loop. I believe that
the TLS code does require a similar callback.
In trying to understand why this is required, imagine a SASL-encrypted
LDAP packet in the following formation:
+-----------------+---------------------+
| SASL Packet #1 | SASL Packet #2 |
----------------------------------------+
| LDAP Packet #1 | LDAP Packet #2 |
----------------------------------------+
In the old code, this was illegal, but it is perfectly standard
SASL-encrypted LDAP. Without the callback, we would read and process
the first LDAP packet, and the SASL code would have read the second SASL
packet (to decrypt enough data for the LDAP packet), and no data would
remain on the socket.
Without data on the socket, read events stop. That is why I add timed
events, until the SASL buffer is drained.
Another approach would be to add a hack to the event system, to have it
pretend there remained data to read off the network (but that is ugly).
In improving the code, to handle more real-world cases, I've been able
to remove almost all the special-cases in the testnonblock code. The
only special case is that we must use a deterministic partial packet
when calling send, rather than a random length. (1 + n/2). This is
needed because of the way the SASL and TLS code works, and the 'resend
on failure' requirements.
Andrew Bartlett
(This used to be commit 5d7c9c12cb2b39673172a357092b80cd814850b0)
2006-07-23 06:50:08 +04:00
decode / process LDAP data
2005-06-16 09:39:40 +04:00
*/
2015-06-19 13:26:06 +03:00
static void ldap_connection_recv_done ( struct tevent_req * subreq )
2004-11-06 23:15:39 +03:00
{
2007-03-13 03:59:06 +03:00
NTSTATUS status ;
2015-06-19 13:26:06 +03:00
struct ldap_connection * conn =
tevent_req_callback_data ( subreq ,
struct ldap_connection ) ;
struct ldap_message * msg ;
struct asn1_data * asn1 ;
DATA_BLOB blob ;
msg = talloc_zero ( conn , struct ldap_message ) ;
if ( msg = = NULL ) {
ldap_error_handler ( conn , NT_STATUS_NO_MEMORY ) ;
return ;
2005-11-10 03:28:02 +03:00
}
2004-11-06 23:15:39 +03:00
2015-06-19 13:26:06 +03:00
asn1 = asn1_init ( conn ) ;
if ( asn1 = = NULL ) {
TALLOC_FREE ( msg ) ;
ldap_error_handler ( conn , NT_STATUS_NO_MEMORY ) ;
return ;
2005-11-10 03:28:02 +03:00
}
2015-06-19 13:26:06 +03:00
conn - > sockets . recv_subreq = NULL ;
status = tstream_read_pdu_blob_recv ( subreq ,
asn1 ,
& blob ) ;
TALLOC_FREE ( subreq ) ;
2007-03-13 03:59:06 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2015-06-19 13:26:06 +03:00
TALLOC_FREE ( msg ) ;
2007-05-21 10:12:06 +04:00
asn1_free ( asn1 ) ;
2015-06-19 13:26:06 +03:00
ldap_error_handler ( conn , status ) ;
return ;
2005-11-10 03:28:02 +03:00
}
2004-11-06 23:15:39 +03:00
2015-06-19 13:26:06 +03:00
asn1_load_nocopy ( asn1 , blob . data , blob . length ) ;
2004-11-06 23:15:39 +03:00
2015-06-19 13:26:06 +03:00
status = ldap_decode ( asn1 , samba_ldap_control_handlers ( ) , msg ) ;
2007-05-21 10:12:06 +04:00
asn1_free ( asn1 ) ;
2015-06-19 13:26:06 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
TALLOC_FREE ( msg ) ;
ldap_error_handler ( conn , status ) ;
return ;
}
2004-11-06 23:15:39 +03:00
2015-06-19 13:26:06 +03:00
ldap_match_message ( conn , msg ) ;
ldap_connection_recv_next ( conn ) ;
2005-05-11 18:38:13 +04:00
2015-06-19 13:26:06 +03:00
return ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
/*
parse a ldap URL
*/
static NTSTATUS ldap_parse_basic_url ( TALLOC_CTX * mem_ctx , const char * url ,
2007-10-07 02:28:14 +04:00
char * * host , uint16_t * port , bool * ldaps )
2004-11-06 23:15:39 +03:00
{
2005-06-16 09:39:40 +04:00
int tmp_port = 0 ;
char protocol [ 11 ] ;
2006-09-04 04:26:10 +04:00
char tmp_host [ 1025 ] ;
2005-06-16 09:39:40 +04:00
int ret ;
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
/* Paranoia check */
SMB_ASSERT ( sizeof ( protocol ) > 10 & & sizeof ( tmp_host ) > 254 ) ;
2006-09-04 04:26:10 +04:00
ret = sscanf ( url , " %10[^:]://%254[^:/]:%d " , protocol , tmp_host , & tmp_port ) ;
2005-06-16 09:39:40 +04:00
if ( ret < 2 ) {
return NT_STATUS_INVALID_PARAMETER ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
if ( strequal ( protocol , " ldap " ) ) {
* port = 389 ;
2007-10-07 02:28:14 +04:00
* ldaps = false ;
2005-06-16 09:39:40 +04:00
} else if ( strequal ( protocol , " ldaps " ) ) {
* port = 636 ;
2007-10-07 02:28:14 +04:00
* ldaps = true ;
2004-11-06 23:15:39 +03:00
} else {
2005-06-16 09:39:40 +04:00
DEBUG ( 0 , ( " unrecognised ldap protocol (%s)! \n " , protocol ) ) ;
return NT_STATUS_PROTOCOL_UNREACHABLE ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
if ( tmp_port ! = 0 )
* port = tmp_port ;
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
* host = talloc_strdup ( mem_ctx , tmp_host ) ;
NT_STATUS_HAVE_NO_MEMORY ( * host ) ;
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
return NT_STATUS_OK ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
/*
connect to a ldap server
*/
2004-11-06 23:15:39 +03:00
2005-10-24 13:34:12 +04:00
struct ldap_connect_state {
struct composite_context * ctx ;
struct ldap_connection * conn ;
2015-06-19 13:26:06 +03:00
struct socket_context * sock ;
struct tstream_context * raw ;
struct tstream_tls_params * tls_params ;
struct tstream_context * tls ;
2005-10-24 13:34:12 +04:00
} ;
2004-11-06 23:15:39 +03:00
2006-09-04 04:26:10 +04:00
static void ldap_connect_recv_unix_conn ( struct composite_context * ctx ) ;
static void ldap_connect_recv_tcp_conn ( struct composite_context * ctx ) ;
2004-11-06 23:15:39 +03:00
2008-04-02 06:53:27 +04:00
_PUBLIC_ struct composite_context * ldap_connect_send ( struct ldap_connection * conn ,
2005-10-24 13:34:12 +04:00
const char * url )
{
struct composite_context * result , * ctx ;
struct ldap_connect_state * state ;
2006-09-04 04:26:10 +04:00
char protocol [ 11 ] ;
int ret ;
2005-10-24 13:34:12 +04:00
2008-07-15 09:10:29 +04:00
result = talloc_zero ( conn , struct composite_context ) ;
2005-10-24 13:34:12 +04:00
if ( result = = NULL ) goto failed ;
result - > state = COMPOSITE_STATE_IN_PROGRESS ;
result - > async . fn = NULL ;
result - > event_ctx = conn - > event . event_ctx ;
state = talloc ( result , struct ldap_connect_state ) ;
if ( state = = NULL ) goto failed ;
state - > ctx = result ;
result - > private_data = state ;
state - > conn = conn ;
2006-05-03 00:15:47 +04:00
if ( conn - > reconnect . url = = NULL ) {
conn - > reconnect . url = talloc_strdup ( conn , url ) ;
if ( conn - > reconnect . url = = NULL ) goto failed ;
}
2006-09-04 04:26:10 +04:00
/* Paranoia check */
SMB_ASSERT ( sizeof ( protocol ) > 10 ) ;
ret = sscanf ( url , " %10[^:]:// " , protocol ) ;
if ( ret < 1 ) {
return NULL ;
2005-10-24 13:34:12 +04:00
}
2004-11-06 23:15:39 +03:00
2006-09-04 04:26:10 +04:00
if ( strequal ( protocol , " ldapi " ) ) {
struct socket_address * unix_addr ;
char path [ 1025 ] ;
2015-06-19 13:26:06 +03:00
NTSTATUS status = socket_create ( " unix " , SOCKET_TYPE_STREAM , & state - > sock , 0 ) ;
2006-09-04 04:26:10 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return NULL ;
}
2015-06-19 13:26:06 +03:00
talloc_steal ( state , state - > sock ) ;
2006-09-04 04:26:10 +04:00
SMB_ASSERT ( sizeof ( protocol ) > 10 ) ;
SMB_ASSERT ( sizeof ( path ) > 1024 ) ;
2009-08-14 11:50:18 +04:00
/* LDAPI connections are to localhost, so give the
* local host name as the target for gensec ' s
* DIGEST - MD5 mechanism */
2009-10-12 21:09:18 +04:00
conn - > host = talloc_asprintf ( conn , " %s.%s " ,
2010-07-16 08:32:42 +04:00
lpcfg_netbios_name ( conn - > lp_ctx ) ,
lpcfg_dnsdomain ( conn - > lp_ctx ) ) ;
2008-07-15 09:10:29 +04:00
if ( composite_nomem ( conn - > host , state - > ctx ) ) {
return result ;
}
2006-09-04 05:59:23 +04:00
/* The %c specifier doesn't null terminate :-( */
ZERO_STRUCT ( path ) ;
2006-09-04 04:26:10 +04:00
ret = sscanf ( url , " %10[^:]://%1025c " , protocol , path ) ;
if ( ret < 2 ) {
composite_error ( state - > ctx , NT_STATUS_INVALID_PARAMETER ) ;
return result ;
}
2005-10-24 13:34:12 +04:00
2006-09-04 04:26:10 +04:00
rfc1738_unescape ( path ) ;
2015-06-19 13:26:06 +03:00
unix_addr = socket_address_from_strings ( state , state - > sock - > backend_name ,
2006-09-04 04:26:10 +04:00
path , 0 ) ;
2015-06-19 13:26:06 +03:00
if ( composite_nomem ( unix_addr , result ) ) {
return result ;
2006-09-04 04:26:10 +04:00
}
2015-06-19 13:26:06 +03:00
ctx = socket_connect_send ( state - > sock , NULL , unix_addr ,
0 , result - > event_ctx ) ;
2006-09-04 04:26:10 +04:00
ctx - > async . fn = ldap_connect_recv_unix_conn ;
ctx - > async . private_data = state ;
return result ;
} else {
NTSTATUS status = ldap_parse_basic_url ( conn , url , & conn - > host ,
& conn - > port , & conn - > ldaps ) ;
2015-06-19 13:26:06 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
composite_error ( result , status ) ;
2006-09-04 04:26:10 +04:00
return result ;
}
2015-06-19 13:26:06 +03:00
if ( conn - > ldaps ) {
char * ca_file = lpcfg_tls_cafile ( state , conn - > lp_ctx ) ;
char * crl_file = lpcfg_tls_crlfile ( state , conn - > lp_ctx ) ;
2015-07-20 02:22:46 +03:00
const char * tls_priority = lpcfg_tls_priority ( conn - > lp_ctx ) ;
2015-06-19 13:26:06 +03:00
if ( ! ca_file | | ! * ca_file ) {
composite_error ( result ,
NT_STATUS_INVALID_PARAMETER_MIX ) ;
return result ;
}
status = tstream_tls_params_client ( state ,
ca_file ,
crl_file ,
2015-07-20 02:22:46 +03:00
tls_priority ,
2015-06-19 13:26:06 +03:00
& state - > tls_params ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
composite_error ( result , status ) ;
return result ;
}
}
2006-09-04 04:26:10 +04:00
ctx = socket_connect_multi_send ( state , conn - > host , 1 , & conn - > port ,
2015-06-19 13:26:06 +03:00
lpcfg_resolve_context ( conn - > lp_ctx ) ,
result - > event_ctx ) ;
if ( composite_nomem ( ctx , result ) ) {
return result ;
}
2005-10-24 13:34:12 +04:00
2006-09-04 04:26:10 +04:00
ctx - > async . fn = ldap_connect_recv_tcp_conn ;
ctx - > async . private_data = state ;
return result ;
}
2005-10-24 13:34:12 +04:00
failed :
talloc_free ( result ) ;
return NULL ;
}
2015-06-19 13:26:06 +03:00
static void ldap_connect_got_tls ( struct tevent_req * subreq ) ;
2007-12-03 23:25:06 +03:00
static void ldap_connect_got_sock ( struct composite_context * ctx ,
2015-06-19 13:26:06 +03:00
struct ldap_connection * conn )
2005-10-24 13:34:12 +04:00
{
2015-06-19 13:26:06 +03:00
struct ldap_connect_state * state =
talloc_get_type_abort ( ctx - > private_data ,
struct ldap_connect_state ) ;
struct tevent_req * subreq = NULL ;
int fd ;
int ret ;
2007-10-18 05:32:07 +04:00
2015-06-19 13:26:06 +03:00
socket_set_flags ( state - > sock , SOCKET_FLAG_NOCLOSE ) ;
fd = socket_get_fd ( state - > sock ) ;
TALLOC_FREE ( state - > sock ) ;
2007-12-03 02:28:22 +03:00
2015-06-19 13:26:06 +03:00
smb_set_close_on_exec ( fd ) ;
2015-06-30 15:10:50 +03:00
ret = set_blocking ( fd , false ) ;
if ( ret = = - 1 ) {
NTSTATUS status = map_nt_error_from_unix_common ( errno ) ;
composite_error ( state - > ctx , status ) ;
return ;
}
2007-12-03 02:28:22 +03:00
2015-06-19 13:26:06 +03:00
ret = tstream_bsd_existing_socket ( state , fd , & state - > raw ) ;
if ( ret = = - 1 ) {
NTSTATUS status = map_nt_error_from_unix_common ( errno ) ;
composite_error ( state - > ctx , status ) ;
return ;
}
2008-09-24 09:37:16 +04:00
2015-06-19 13:26:06 +03:00
if ( ! conn - > ldaps ) {
conn - > sockets . raw = talloc_move ( conn , & state - > raw ) ;
conn - > sockets . active = conn - > sockets . raw ;
composite_done ( state - > ctx ) ;
return ;
2005-06-20 05:17:29 +04:00
}
2015-06-19 13:26:06 +03:00
subreq = tstream_tls_connect_send ( state , state - > ctx - > event_ctx ,
state - > raw , state - > tls_params ) ;
if ( composite_nomem ( subreq , state - > ctx ) ) {
2005-11-10 03:28:02 +03:00
return ;
}
2015-06-19 13:26:06 +03:00
tevent_req_set_callback ( subreq , ldap_connect_got_tls , state ) ;
}
2006-05-03 00:15:47 +04:00
2015-06-19 13:26:06 +03:00
static void ldap_connect_got_tls ( struct tevent_req * subreq )
{
struct ldap_connect_state * state =
tevent_req_callback_data ( subreq ,
struct ldap_connect_state ) ;
int err ;
int ret ;
2005-11-10 03:28:02 +03:00
2015-06-19 13:26:06 +03:00
ret = tstream_tls_connect_recv ( subreq , & err , state , & state - > tls ) ;
TALLOC_FREE ( subreq ) ;
if ( ret = = - 1 ) {
NTSTATUS status = map_nt_error_from_unix_common ( err ) ;
composite_error ( state - > ctx , status ) ;
return ;
2009-02-18 09:37:45 +03:00
}
2015-06-19 13:26:06 +03:00
talloc_steal ( state - > tls , state - > tls_params ) ;
state - > conn - > sockets . raw = talloc_move ( state - > conn , & state - > raw ) ;
state - > conn - > sockets . tls = talloc_move ( state - > conn - > sockets . raw ,
& state - > tls ) ;
state - > conn - > sockets . active = state - > conn - > sockets . tls ;
composite_done ( state - > ctx ) ;
2006-09-04 04:26:10 +04:00
}
static void ldap_connect_recv_tcp_conn ( struct composite_context * ctx )
{
struct ldap_connect_state * state =
2015-06-19 13:26:06 +03:00
talloc_get_type_abort ( ctx - > async . private_data ,
struct ldap_connect_state ) ;
2006-09-04 04:26:10 +04:00
struct ldap_connection * conn = state - > conn ;
uint16_t port ;
2015-06-19 13:26:06 +03:00
NTSTATUS status = socket_connect_multi_recv ( ctx , state , & state - > sock ,
2006-09-04 04:26:10 +04:00
& port ) ;
2006-09-08 10:04:10 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2006-09-04 04:26:10 +04:00
composite_error ( state - > ctx , status ) ;
return ;
}
ldap_connect_got_sock ( state - > ctx , conn ) ;
}
static void ldap_connect_recv_unix_conn ( struct composite_context * ctx )
{
struct ldap_connect_state * state =
2015-06-19 13:26:06 +03:00
talloc_get_type_abort ( ctx - > async . private_data ,
struct ldap_connect_state ) ;
2006-09-04 04:26:10 +04:00
struct ldap_connection * conn = state - > conn ;
NTSTATUS status = socket_connect_recv ( ctx ) ;
if ( ! NT_STATUS_IS_OK ( state - > ctx - > status ) ) {
composite_error ( state - > ctx , status ) ;
return ;
}
2005-10-24 13:34:12 +04:00
2006-09-04 04:26:10 +04:00
ldap_connect_got_sock ( state - > ctx , conn ) ;
2005-10-24 13:34:12 +04:00
}
2006-10-29 20:40:19 +03:00
_PUBLIC_ NTSTATUS ldap_connect_recv ( struct composite_context * ctx )
2005-10-24 13:34:12 +04:00
{
NTSTATUS status = composite_wait ( ctx ) ;
talloc_free ( ctx ) ;
return status ;
}
2008-04-02 06:53:27 +04:00
_PUBLIC_ NTSTATUS ldap_connect ( struct ldap_connection * conn , const char * url )
2005-10-24 13:34:12 +04:00
{
struct composite_context * ctx = ldap_connect_send ( conn , url ) ;
return ldap_connect_recv ( ctx ) ;
2004-11-06 23:15:39 +03:00
}
2006-04-25 16:34:13 +04:00
/* set reconnect parameters */
2008-04-02 06:53:27 +04:00
_PUBLIC_ void ldap_set_reconn_params ( struct ldap_connection * conn , int max_retries )
2006-04-25 16:34:13 +04:00
{
if ( conn ) {
conn - > reconnect . max_retries = max_retries ;
conn - > reconnect . retries = 0 ;
2010-09-10 22:39:20 +04:00
conn - > reconnect . previous = time_mono ( NULL ) ;
2006-04-25 16:34:13 +04:00
}
}
2006-04-25 15:50:32 +04:00
/* Actually this function is NOT ASYNC safe, FIXME? */
static void ldap_reconnect ( struct ldap_connection * conn )
{
NTSTATUS status ;
2010-09-10 22:39:20 +04:00
time_t now = time_mono ( NULL ) ;
2006-04-25 15:50:32 +04:00
/* do we have set up reconnect ? */
if ( conn - > reconnect . max_retries = = 0 ) return ;
/* is the retry time expired ? */
if ( now > conn - > reconnect . previous + 30 ) {
conn - > reconnect . retries = 0 ;
conn - > reconnect . previous = now ;
}
/* are we reconnectind too often and too fast? */
if ( conn - > reconnect . retries > conn - > reconnect . max_retries ) return ;
/* keep track of the number of reconnections */
conn - > reconnect . retries + + ;
/* reconnect */
status = ldap_connect ( conn , conn - > reconnect . url ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return ;
}
/* rebind */
status = ldap_rebind ( conn ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2015-06-19 13:26:06 +03:00
ldap_connection_dead ( conn , status ) ;
2006-04-25 15:50:32 +04:00
}
}
2016-02-01 13:00:14 +03:00
static void ldap_request_destructor_abandon ( struct ldap_request * abandon )
{
TALLOC_FREE ( abandon ) ;
}
2005-06-16 09:39:40 +04:00
/* destroy an open ldap request */
2006-05-24 11:34:11 +04:00
static int ldap_request_destructor ( struct ldap_request * req )
2004-11-06 23:15:39 +03:00
{
2005-06-16 09:39:40 +04:00
if ( req - > state = = LDAP_REQUEST_PENDING ) {
2016-02-01 13:00:14 +03:00
struct ldap_message msg = {
. type = LDAP_TAG_AbandonRequest ,
. r . AbandonRequest . messageid = req - > messageid ,
} ;
struct ldap_request * abandon = NULL ;
2005-06-16 09:39:40 +04:00
DLIST_REMOVE ( req - > conn - > pending , req ) ;
2016-02-01 13:00:14 +03:00
abandon = ldap_request_send ( req - > conn , & msg ) ;
if ( abandon = = NULL ) {
ldap_error_handler ( req - > conn , NT_STATUS_NO_MEMORY ) ;
return 0 ;
}
abandon - > async . fn = ldap_request_destructor_abandon ;
abandon - > async . private_data = NULL ;
2004-11-06 23:15:39 +03:00
}
2016-02-01 13:00:14 +03:00
2005-06-16 09:39:40 +04:00
return 0 ;
2004-11-06 23:15:39 +03:00
}
2016-02-01 13:00:14 +03:00
static void ldap_request_timeout_abandon ( struct ldap_request * abandon )
{
struct ldap_request * req =
talloc_get_type_abort ( abandon - > async . private_data ,
struct ldap_request ) ;
if ( req - > state = = LDAP_REQUEST_PENDING ) {
DLIST_REMOVE ( req - > conn - > pending , req ) ;
}
req - > state = LDAP_REQUEST_DONE ;
if ( req - > async . fn ) {
req - > async . fn ( req ) ;
}
}
2005-06-16 09:39:40 +04:00
/*
called on timeout of a ldap request
*/
2008-12-29 22:24:57 +03:00
static void ldap_request_timeout ( struct tevent_context * ev , struct tevent_timer * te ,
2006-02-22 04:31:35 +03:00
struct timeval t , void * private_data )
2004-11-06 23:15:39 +03:00
{
2015-06-19 13:26:06 +03:00
struct ldap_request * req =
talloc_get_type_abort ( private_data ,
struct ldap_request ) ;
2005-06-16 09:39:40 +04:00
req - > status = NT_STATUS_IO_TIMEOUT ;
if ( req - > state = = LDAP_REQUEST_PENDING ) {
2016-02-01 13:00:14 +03:00
struct ldap_message msg = {
. type = LDAP_TAG_AbandonRequest ,
. r . AbandonRequest . messageid = req - > messageid ,
} ;
struct ldap_request * abandon = NULL ;
abandon = ldap_request_send ( req - > conn , & msg ) ;
if ( abandon = = NULL ) {
ldap_error_handler ( req - > conn , NT_STATUS_NO_MEMORY ) ;
return ;
}
talloc_reparent ( req - > conn , req , abandon ) ;
abandon - > async . fn = ldap_request_timeout_abandon ;
abandon - > async . private_data = req ;
2005-06-16 09:39:40 +04:00
DLIST_REMOVE ( req - > conn - > pending , req ) ;
2016-02-01 13:00:14 +03:00
return ;
2005-06-16 09:39:40 +04:00
}
req - > state = LDAP_REQUEST_DONE ;
if ( req - > async . fn ) {
req - > async . fn ( req ) ;
2004-11-06 23:15:39 +03:00
}
}
2005-11-10 03:28:02 +03:00
/*
2010-09-27 00:34:37 +04:00
called on completion of a failed ldap request
2005-11-10 03:28:02 +03:00
*/
2010-09-27 00:34:37 +04:00
static void ldap_request_failed_complete ( struct tevent_context * ev , struct tevent_timer * te ,
struct timeval t , void * private_data )
2005-11-10 03:28:02 +03:00
{
2015-06-19 13:26:06 +03:00
struct ldap_request * req =
talloc_get_type_abort ( private_data ,
struct ldap_request ) ;
2005-11-10 03:28:02 +03:00
2010-09-27 00:34:37 +04:00
if ( req - > async . fn ) {
req - > async . fn ( req ) ;
}
}
2015-06-19 13:26:06 +03:00
static void ldap_request_written ( struct tevent_req * subreq ) ;
2005-06-16 09:39:40 +04:00
/*
send a ldap message - async interface
*/
2008-04-02 06:53:27 +04:00
_PUBLIC_ struct ldap_request * ldap_request_send ( struct ldap_connection * conn ,
2005-06-16 09:39:40 +04:00
struct ldap_message * msg )
2004-11-06 23:15:39 +03:00
{
2005-06-16 09:39:40 +04:00
struct ldap_request * req ;
2006-04-25 15:50:32 +04:00
NTSTATUS status = NT_STATUS_UNSUCCESSFUL ;
2015-06-19 13:26:06 +03:00
struct tevent_req * subreq = NULL ;
2006-04-25 15:50:32 +04:00
req = talloc_zero ( conn , struct ldap_request ) ;
if ( req = = NULL ) return NULL ;
2004-11-06 23:15:39 +03:00
2015-06-19 13:26:06 +03:00
if ( conn - > sockets . active = = NULL ) {
2006-04-25 15:50:32 +04:00
status = NT_STATUS_INVALID_CONNECTION ;
goto failed ;
2005-06-16 09:39:40 +04:00
}
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
req - > state = LDAP_REQUEST_SEND ;
req - > conn = conn ;
req - > messageid = conn - > next_messageid + + ;
2005-07-17 14:52:31 +04:00
if ( conn - > next_messageid = = 0 ) {
conn - > next_messageid = 1 ;
}
2005-06-16 09:39:40 +04:00
req - > type = msg - > type ;
if ( req - > messageid = = - 1 ) {
goto failed ;
}
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
talloc_set_destructor ( req , ldap_request_destructor ) ;
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
msg - > messageid = req - > messageid ;
2004-11-06 23:15:39 +03:00
2009-02-24 18:49:26 +03:00
if ( ! ldap_encode ( msg , samba_ldap_control_handlers ( ) , & req - > data , req ) ) {
2007-03-08 09:23:39 +03:00
status = NT_STATUS_INTERNAL_ERROR ;
2005-06-16 09:39:40 +04:00
goto failed ;
}
2004-11-06 23:15:39 +03:00
2015-06-19 13:26:06 +03:00
/* put a timeout on the request */
req - > time_event = tevent_add_timer ( conn - > event . event_ctx , req ,
timeval_current_ofs ( conn - > timeout , 0 ) ,
ldap_request_timeout , req ) ;
if ( req - > time_event = = NULL ) {
status = NT_STATUS_NO_MEMORY ;
goto failed ;
2010-09-27 00:34:37 +04:00
}
2015-06-19 13:26:06 +03:00
req - > write_iov . iov_base = req - > data . data ;
req - > write_iov . iov_len = req - > data . length ;
subreq = tstream_writev_queue_send ( req , conn - > event . event_ctx ,
conn - > sockets . active ,
conn - > sockets . send_queue ,
& req - > write_iov , 1 ) ;
if ( subreq = = NULL ) {
status = NT_STATUS_NO_MEMORY ;
2010-09-27 00:34:37 +04:00
goto failed ;
2004-11-06 23:15:39 +03:00
}
2015-06-19 13:26:06 +03:00
tevent_req_set_callback ( subreq , ldap_request_written , req ) ;
2005-11-10 03:28:02 +03:00
req - > state = LDAP_REQUEST_PENDING ;
DLIST_ADD ( conn - > pending , req ) ;
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
return req ;
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
failed :
2006-04-25 15:50:32 +04:00
req - > status = status ;
req - > state = LDAP_REQUEST_ERROR ;
2009-01-03 14:49:50 +03:00
tevent_add_timer ( conn - > event . event_ctx , req , timeval_zero ( ) ,
2010-09-27 00:34:37 +04:00
ldap_request_failed_complete , req ) ;
2006-04-25 15:50:32 +04:00
return req ;
2004-11-06 23:15:39 +03:00
}
2015-06-19 13:26:06 +03:00
static void ldap_request_written ( struct tevent_req * subreq )
{
struct ldap_request * req =
tevent_req_callback_data ( subreq ,
struct ldap_request ) ;
int err ;
ssize_t ret ;
ret = tstream_writev_queue_recv ( subreq , & err ) ;
TALLOC_FREE ( subreq ) ;
if ( ret = = - 1 ) {
NTSTATUS error = map_nt_error_from_unix_common ( err ) ;
ldap_error_handler ( req - > conn , error ) ;
return ;
}
if ( req - > type = = LDAP_TAG_AbandonRequest | |
req - > type = = LDAP_TAG_UnbindRequest )
{
if ( req - > state = = LDAP_REQUEST_PENDING ) {
DLIST_REMOVE ( req - > conn - > pending , req ) ;
}
req - > state = LDAP_REQUEST_DONE ;
if ( req - > async . fn ) {
req - > async . fn ( req ) ;
}
return ;
}
ldap_connection_recv_next ( req - > conn ) ;
}
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
/*
wait for a request to complete
note that this does not destroy the request
*/
2008-04-02 06:53:27 +04:00
_PUBLIC_ NTSTATUS ldap_request_wait ( struct ldap_request * req )
2005-06-16 09:39:40 +04:00
{
2006-04-26 20:52:45 +04:00
while ( req - > state < LDAP_REQUEST_DONE ) {
2009-01-03 14:49:50 +03:00
if ( tevent_loop_once ( req - > conn - > event . event_ctx ) ! = 0 ) {
2015-06-19 13:26:06 +03:00
req - > state = LDAP_REQUEST_ERROR ;
2005-06-16 09:39:40 +04:00
req - > status = NT_STATUS_UNEXPECTED_NETWORK_ERROR ;
break ;
}
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
return req - > status ;
2004-11-06 23:15:39 +03:00
}
2005-10-17 15:50:34 +04:00
/*
a mapping of ldap response code to strings
*/
static const struct {
enum ldap_result_code code ;
const char * str ;
} ldap_code_map [ ] = {
# define _LDAP_MAP_CODE(c) { c, #c }
_LDAP_MAP_CODE ( LDAP_SUCCESS ) ,
_LDAP_MAP_CODE ( LDAP_OPERATIONS_ERROR ) ,
_LDAP_MAP_CODE ( LDAP_PROTOCOL_ERROR ) ,
_LDAP_MAP_CODE ( LDAP_TIME_LIMIT_EXCEEDED ) ,
_LDAP_MAP_CODE ( LDAP_SIZE_LIMIT_EXCEEDED ) ,
_LDAP_MAP_CODE ( LDAP_COMPARE_FALSE ) ,
_LDAP_MAP_CODE ( LDAP_COMPARE_TRUE ) ,
_LDAP_MAP_CODE ( LDAP_AUTH_METHOD_NOT_SUPPORTED ) ,
_LDAP_MAP_CODE ( LDAP_STRONG_AUTH_REQUIRED ) ,
_LDAP_MAP_CODE ( LDAP_REFERRAL ) ,
_LDAP_MAP_CODE ( LDAP_ADMIN_LIMIT_EXCEEDED ) ,
_LDAP_MAP_CODE ( LDAP_UNAVAILABLE_CRITICAL_EXTENSION ) ,
_LDAP_MAP_CODE ( LDAP_CONFIDENTIALITY_REQUIRED ) ,
_LDAP_MAP_CODE ( LDAP_SASL_BIND_IN_PROGRESS ) ,
_LDAP_MAP_CODE ( LDAP_NO_SUCH_ATTRIBUTE ) ,
_LDAP_MAP_CODE ( LDAP_UNDEFINED_ATTRIBUTE_TYPE ) ,
_LDAP_MAP_CODE ( LDAP_INAPPROPRIATE_MATCHING ) ,
_LDAP_MAP_CODE ( LDAP_CONSTRAINT_VIOLATION ) ,
_LDAP_MAP_CODE ( LDAP_ATTRIBUTE_OR_VALUE_EXISTS ) ,
_LDAP_MAP_CODE ( LDAP_INVALID_ATTRIBUTE_SYNTAX ) ,
_LDAP_MAP_CODE ( LDAP_NO_SUCH_OBJECT ) ,
_LDAP_MAP_CODE ( LDAP_ALIAS_PROBLEM ) ,
_LDAP_MAP_CODE ( LDAP_INVALID_DN_SYNTAX ) ,
_LDAP_MAP_CODE ( LDAP_ALIAS_DEREFERENCING_PROBLEM ) ,
_LDAP_MAP_CODE ( LDAP_INAPPROPRIATE_AUTHENTICATION ) ,
_LDAP_MAP_CODE ( LDAP_INVALID_CREDENTIALS ) ,
2007-05-15 11:16:04 +04:00
_LDAP_MAP_CODE ( LDAP_INSUFFICIENT_ACCESS_RIGHTS ) ,
2005-10-17 15:50:34 +04:00
_LDAP_MAP_CODE ( LDAP_BUSY ) ,
_LDAP_MAP_CODE ( LDAP_UNAVAILABLE ) ,
_LDAP_MAP_CODE ( LDAP_UNWILLING_TO_PERFORM ) ,
_LDAP_MAP_CODE ( LDAP_LOOP_DETECT ) ,
_LDAP_MAP_CODE ( LDAP_NAMING_VIOLATION ) ,
_LDAP_MAP_CODE ( LDAP_OBJECT_CLASS_VIOLATION ) ,
_LDAP_MAP_CODE ( LDAP_NOT_ALLOWED_ON_NON_LEAF ) ,
_LDAP_MAP_CODE ( LDAP_NOT_ALLOWED_ON_RDN ) ,
_LDAP_MAP_CODE ( LDAP_ENTRY_ALREADY_EXISTS ) ,
_LDAP_MAP_CODE ( LDAP_OBJECT_CLASS_MODS_PROHIBITED ) ,
_LDAP_MAP_CODE ( LDAP_AFFECTS_MULTIPLE_DSAS ) ,
_LDAP_MAP_CODE ( LDAP_OTHER )
} ;
2005-06-16 09:39:40 +04:00
/*
used to setup the status code from a ldap response
*/
2008-04-02 06:53:27 +04:00
_PUBLIC_ NTSTATUS ldap_check_response ( struct ldap_connection * conn , struct ldap_Result * r )
2005-06-16 09:39:40 +04:00
{
2005-10-17 15:50:34 +04:00
int i ;
const char * codename = " unknown " ;
2005-06-16 09:39:40 +04:00
if ( r - > resultcode = = LDAP_SUCCESS ) {
return NT_STATUS_OK ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
if ( conn - > last_error ) {
talloc_free ( conn - > last_error ) ;
}
2005-10-17 15:50:34 +04:00
for ( i = 0 ; i < ARRAY_SIZE ( ldap_code_map ) ; i + + ) {
if ( r - > resultcode = = ldap_code_map [ i ] . code ) {
codename = ldap_code_map [ i ] . str ;
break ;
}
}
conn - > last_error = talloc_asprintf ( conn , " LDAP error %u %s - %s <%s> <%s> " ,
2005-06-16 09:39:40 +04:00
r - > resultcode ,
2005-10-17 15:50:34 +04:00
codename ,
2005-06-18 13:01:51 +04:00
r - > dn ? r - > dn : " (NULL) " ,
r - > errormessage ? r - > errormessage : " " ,
r - > referral ? r - > referral : " " ) ;
2005-06-16 09:39:40 +04:00
return NT_STATUS_LDAP ( r - > resultcode ) ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
/*
return error string representing the last error
*/
2008-04-02 06:53:27 +04:00
_PUBLIC_ const char * ldap_errstr ( struct ldap_connection * conn ,
2007-03-08 09:23:39 +03:00
TALLOC_CTX * mem_ctx ,
NTSTATUS status )
2004-11-06 23:15:39 +03:00
{
2005-06-16 09:39:40 +04:00
if ( NT_STATUS_IS_LDAP ( status ) & & conn - > last_error ! = NULL ) {
2007-03-08 09:23:39 +03:00
return talloc_strdup ( mem_ctx , conn - > last_error ) ;
2004-11-06 23:15:39 +03:00
}
2007-03-08 09:23:39 +03:00
return talloc_asprintf ( mem_ctx , " LDAP client internal error: %s " , nt_errstr ( status ) ) ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
/*
return the Nth result message , waiting if necessary
*/
2008-04-02 06:53:27 +04:00
_PUBLIC_ NTSTATUS ldap_result_n ( struct ldap_request * req , int n , struct ldap_message * * msg )
2004-11-06 23:15:39 +03:00
{
2005-06-16 09:39:40 +04:00
* msg = NULL ;
2004-11-06 23:15:39 +03:00
2005-06-17 06:45:40 +04:00
NT_STATUS_HAVE_NO_MEMORY ( req ) ;
2006-04-26 20:52:45 +04:00
while ( req - > state < LDAP_REQUEST_DONE & & n > = req - > num_replies ) {
2009-01-03 14:49:50 +03:00
if ( tevent_loop_once ( req - > conn - > event . event_ctx ) ! = 0 ) {
2005-06-16 09:39:40 +04:00
return NT_STATUS_UNEXPECTED_NETWORK_ERROR ;
}
}
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
if ( n < req - > num_replies ) {
* msg = req - > replies [ n ] ;
return NT_STATUS_OK ;
}
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
if ( ! NT_STATUS_IS_OK ( req - > status ) ) {
return req - > status ;
}
2004-11-06 23:15:39 +03:00
2005-06-16 09:39:40 +04:00
return NT_STATUS_NO_MORE_ENTRIES ;
2004-11-06 23:15:39 +03:00
}
2005-06-16 09:39:40 +04:00
/*
return a single result message , checking if it is of the expected LDAP type
*/
2008-04-02 06:53:27 +04:00
_PUBLIC_ NTSTATUS ldap_result_one ( struct ldap_request * req , struct ldap_message * * msg , int type )
2004-11-06 23:15:39 +03:00
{
2005-06-16 09:39:40 +04:00
NTSTATUS status ;
status = ldap_result_n ( req , 0 , msg ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return status ;
}
if ( ( * msg ) - > type ! = type ) {
* msg = NULL ;
return NT_STATUS_UNEXPECTED_NETWORK_ERROR ;
}
return status ;
2004-11-06 23:15:39 +03:00
}
2005-06-17 06:45:40 +04:00
/*
a simple ldap transaction , for single result requests that only need a status code
this relies on single valued requests having the response type = = request type + 1
*/
2008-04-02 06:53:27 +04:00
_PUBLIC_ NTSTATUS ldap_transaction ( struct ldap_connection * conn , struct ldap_message * msg )
2005-06-17 06:45:40 +04:00
{
struct ldap_request * req = ldap_request_send ( conn , msg ) ;
struct ldap_message * res ;
NTSTATUS status ;
status = ldap_result_n ( req , 0 , & res ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
talloc_free ( req ) ;
return status ;
}
if ( res - > type ! = msg - > type + 1 ) {
talloc_free ( req ) ;
return NT_STATUS_LDAP ( LDAP_PROTOCOL_ERROR ) ;
}
status = ldap_check_response ( conn , & res - > r . GeneralResult ) ;
talloc_free ( req ) ;
return status ;
}