2005-06-17 02:45:40 +00:00
/*
Unix SMB / CIFS mplementation .
ildap api - an api similar to the traditional ldap api
Copyright ( C ) Andrew Tridgell 2005
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 02:07:03 +00:00
the Free Software Foundation ; either version 3 of the License , or
2005-06-17 02:45:40 +00: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 02:07:03 +00:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2005-06-17 02:45:40 +00:00
*/
# include "includes.h"
2010-05-21 17:39:15 +10:00
# include "libcli/ldap/libcli_ldap.h"
2005-06-17 02:45:40 +00:00
# include "libcli/ldap/ldap_client.h"
/*
count the returned search entries
*/
2008-04-02 04:53:27 +02:00
_PUBLIC_ int ildap_count_entries ( struct ldap_connection * conn , struct ldap_message * * res )
2005-06-17 02:45:40 +00:00
{
int i ;
for ( i = 0 ; res & & res [ i ] ; i + + ) /* noop */ ;
return i ;
}
/*
2006-02-22 01:31:35 +00:00
perform a synchronous ldap search
2005-06-17 02:45:40 +00:00
*/
2008-04-02 04:53:27 +02:00
_PUBLIC_ NTSTATUS ildap_search_bytree ( struct ldap_connection * conn , const char * basedn ,
2005-09-30 23:56:54 +00:00
int scope , struct ldb_parse_tree * tree ,
2007-10-06 22:28:14 +00:00
const char * const * attrs , bool attributesonly ,
2006-02-22 01:31:35 +00:00
struct ldb_control * * control_req ,
struct ldb_control * * * control_res ,
2005-09-30 23:56:54 +00:00
struct ldap_message * * * results )
2005-06-17 02:45:40 +00:00
{
struct ldap_message * msg ;
int n , i ;
NTSTATUS status ;
struct ldap_request * req ;
2006-01-06 04:01:23 +00:00
if ( control_res )
* control_res = NULL ;
2005-06-17 02:45:40 +00:00
* results = NULL ;
msg = new_ldap_message ( conn ) ;
NT_STATUS_HAVE_NO_MEMORY ( msg ) ;
for ( n = 0 ; attrs & & attrs [ n ] ; n + + ) /* noop */ ;
msg - > type = LDAP_TAG_SearchRequest ;
msg - > r . SearchRequest . basedn = basedn ;
msg - > r . SearchRequest . scope = scope ;
msg - > r . SearchRequest . deref = LDAP_DEREFERENCE_NEVER ;
msg - > r . SearchRequest . timelimit = 0 ;
msg - > r . SearchRequest . sizelimit = 0 ;
msg - > r . SearchRequest . attributesonly = attributesonly ;
2005-09-30 23:56:54 +00:00
msg - > r . SearchRequest . tree = tree ;
2005-06-17 02:45:40 +00:00
msg - > r . SearchRequest . num_attributes = n ;
2009-02-13 11:37:15 +01:00
msg - > r . SearchRequest . attributes = attrs ;
2006-01-06 04:01:23 +00:00
msg - > controls = control_req ;
2005-06-17 02:45:40 +00:00
req = ldap_request_send ( conn , msg ) ;
2010-09-27 12:54:26 -07:00
talloc_reparent ( conn , msg , req ) ;
2005-06-17 02:45:40 +00:00
2007-10-06 22:28:14 +00:00
for ( i = n = 0 ; true ; i + + ) {
2005-06-17 02:45:40 +00:00
struct ldap_message * res ;
status = ldap_result_n ( req , i , & res ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) break ;
2005-06-18 07:54:14 +00:00
if ( res - > type = = LDAP_TAG_SearchResultDone ) {
status = ldap_check_response ( conn , & res - > r . GeneralResult ) ;
2006-01-06 04:01:23 +00:00
if ( control_res ) {
* control_res = talloc_steal ( conn , res - > controls ) ;
}
2005-06-18 07:54:14 +00:00
break ;
}
2006-02-22 01:31:35 +00:00
if ( res - > type ! = LDAP_TAG_SearchResultEntry & &
res - > type ! = LDAP_TAG_SearchResultReference )
continue ;
2005-06-17 02:45:40 +00:00
( * results ) = talloc_realloc ( conn , * results , struct ldap_message * , n + 2 ) ;
if ( * results = = NULL ) {
talloc_free ( msg ) ;
return NT_STATUS_NO_MEMORY ;
}
( * results ) [ n ] = talloc_steal ( * results , res ) ;
( * results ) [ n + 1 ] = NULL ;
n + + ;
}
if ( NT_STATUS_EQUAL ( status , NT_STATUS_NO_MORE_ENTRIES ) ) {
status = NT_STATUS_OK ;
}
return status ;
}
2005-09-30 23:56:54 +00:00
/*
perform a ldap search
*/
2008-04-02 04:53:27 +02:00
_PUBLIC_ NTSTATUS ildap_search ( struct ldap_connection * conn , const char * basedn ,
2005-09-30 23:56:54 +00:00
int scope , const char * expression ,
2007-10-06 22:28:14 +00:00
const char * const * attrs , bool attributesonly ,
2006-02-22 01:31:35 +00:00
struct ldb_control * * control_req ,
struct ldb_control * * * control_res ,
2005-09-30 23:56:54 +00:00
struct ldap_message * * * results )
{
NTSTATUS status ;
2012-05-04 11:42:14 +02:00
struct ldb_parse_tree * tree = ldb_parse_tree ( conn , expression ) ;
if ( tree = = NULL ) {
return NT_STATUS_INVALID_PARAMETER ;
}
2005-10-24 04:19:27 +00:00
status = ildap_search_bytree ( conn , basedn , scope , tree , attrs ,
2006-01-06 04:01:23 +00:00
attributesonly , control_req ,
control_res , results ) ;
2005-09-30 23:56:54 +00:00
talloc_free ( tree ) ;
return status ;
}