1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
2003-01-04 04:20:47 +08:00
|
|
|
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
|
1998-12-29 04:53:15 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* Portions
|
1998-08-09 08:43:13 +08:00
|
|
|
* Copyright (c) 1990 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* search.c
|
|
|
|
*/
|
|
|
|
|
1998-10-25 11:13:43 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 11:13:43 +08:00
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
1998-08-21 03:42:38 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include "ldap-int.h"
|
2002-07-12 04:33:24 +08:00
|
|
|
#include "ldap_log.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-22 03:20:25 +08:00
|
|
|
/*
|
|
|
|
* ldap_search_ext - initiate an ldap search operation.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* ld LDAP descriptor
|
|
|
|
* base DN of the base object
|
|
|
|
* scope the search scope - one of LDAP_SCOPE_BASE,
|
|
|
|
* LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
|
|
|
|
* filter a string containing the search filter
|
|
|
|
* (e.g., "(|(cn=bob)(sn=bob))")
|
|
|
|
* attrs list of attribute types to return for matches
|
|
|
|
* attrsonly 1 => attributes only 0 => attributes and values
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* char *attrs[] = { "mail", "title", 0 };
|
2001-12-09 10:34:45 +08:00
|
|
|
* ldap_search_ext( ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "cn~=bob",
|
1999-05-22 03:20:25 +08:00
|
|
|
* attrs, attrsonly, sctrls, ctrls, timeout, sizelimit,
|
|
|
|
* &msgid );
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ldap_search_ext(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAP_CONST char *base,
|
|
|
|
int scope,
|
|
|
|
LDAP_CONST char *filter,
|
|
|
|
char **attrs,
|
|
|
|
int attrsonly,
|
|
|
|
LDAPControl **sctrls,
|
|
|
|
LDAPControl **cctrls,
|
|
|
|
struct timeval *timeout,
|
|
|
|
int sizelimit,
|
|
|
|
int *msgidp )
|
|
|
|
{
|
2001-07-06 11:13:42 +08:00
|
|
|
int rc;
|
1999-05-22 03:20:25 +08:00
|
|
|
BerElement *ber;
|
|
|
|
int timelimit;
|
|
|
|
|
2002-04-02 06:31:44 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( OPERATION, ENTRY, "ldap_search_ext\n", 0, 0, 0 );
|
2002-04-02 06:31:44 +08:00
|
|
|
#else
|
1999-05-22 03:20:25 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
|
2002-04-02 06:31:44 +08:00
|
|
|
#endif
|
1999-05-22 03:20:25 +08:00
|
|
|
|
2000-10-19 00:32:57 +08:00
|
|
|
assert( ld != NULL );
|
|
|
|
assert( LDAP_VALID( ld ) );
|
|
|
|
|
2001-07-06 11:13:42 +08:00
|
|
|
/* check client controls */
|
|
|
|
rc = ldap_int_client_controls( ld, cctrls );
|
|
|
|
if( rc != LDAP_SUCCESS ) return rc;
|
|
|
|
|
1999-05-22 03:20:25 +08:00
|
|
|
/*
|
1999-12-16 08:41:18 +08:00
|
|
|
* if timeout is provided, both tv_sec and tv_usec must
|
|
|
|
* be non-zero
|
1999-05-22 03:20:25 +08:00
|
|
|
*/
|
1999-12-16 08:41:18 +08:00
|
|
|
if( timeout != NULL ) {
|
|
|
|
if( timeout->tv_sec == 0 && timeout->tv_usec == 0 ) {
|
|
|
|
return LDAP_PARAM_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* timelimit must be non-zero if timeout is provided */
|
|
|
|
timelimit = timeout->tv_sec != 0 ? timeout->tv_sec : 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* no timeout, no timelimit */
|
|
|
|
timelimit = -1;
|
|
|
|
}
|
1999-05-22 03:20:25 +08:00
|
|
|
|
|
|
|
ber = ldap_build_search_req( ld, base, scope, filter, attrs,
|
|
|
|
attrsonly, sctrls, cctrls, timelimit, sizelimit );
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( ber == NULL ) {
|
1999-05-22 03:20:25 +08:00
|
|
|
return ld->ld_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* send the message */
|
|
|
|
*msgidp = ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber );
|
|
|
|
|
|
|
|
if( *msgidp < 0 )
|
|
|
|
return ld->ld_errno;
|
|
|
|
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ldap_search_ext_s(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAP_CONST char *base,
|
|
|
|
int scope,
|
|
|
|
LDAP_CONST char *filter,
|
|
|
|
char **attrs,
|
|
|
|
int attrsonly,
|
|
|
|
LDAPControl **sctrls,
|
|
|
|
LDAPControl **cctrls,
|
|
|
|
struct timeval *timeout,
|
|
|
|
int sizelimit,
|
|
|
|
LDAPMessage **res )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int msgid;
|
|
|
|
|
|
|
|
rc = ldap_search_ext( ld, base, scope, filter, attrs, attrsonly,
|
|
|
|
sctrls, cctrls, timeout, sizelimit, &msgid );
|
|
|
|
|
|
|
|
if ( rc != LDAP_SUCCESS ) {
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
1999-12-16 08:41:18 +08:00
|
|
|
rc = ldap_result( ld, msgid, 1, timeout, res );
|
|
|
|
|
|
|
|
if( rc <= 0 ) {
|
|
|
|
/* error(-1) or timeout(0) */
|
1999-05-22 03:20:25 +08:00
|
|
|
return( ld->ld_errno );
|
1999-12-16 08:41:18 +08:00
|
|
|
}
|
1999-05-22 03:20:25 +08:00
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
if( rc == LDAP_RES_SEARCH_REFERENCE || rc == LDAP_RES_EXTENDED_PARTIAL ) {
|
2000-06-01 01:34:43 +08:00
|
|
|
return( ld->ld_errno );
|
|
|
|
}
|
|
|
|
|
1999-05-22 03:20:25 +08:00
|
|
|
return( ldap_result2error( ld, *res, 0 ) );
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
1999-05-19 09:12:33 +08:00
|
|
|
* ldap_search - initiate an ldap search operation.
|
|
|
|
*
|
|
|
|
* Parameters:
|
1998-08-09 08:43:13 +08:00
|
|
|
*
|
|
|
|
* ld LDAP descriptor
|
|
|
|
* base DN of the base object
|
|
|
|
* scope the search scope - one of LDAP_SCOPE_BASE,
|
|
|
|
* LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
|
|
|
|
* filter a string containing the search filter
|
|
|
|
* (e.g., "(|(cn=bob)(sn=bob))")
|
|
|
|
* attrs list of attribute types to return for matches
|
|
|
|
* attrsonly 1 => attributes only 0 => attributes and values
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* char *attrs[] = { "mail", "title", 0 };
|
2001-12-09 10:34:45 +08:00
|
|
|
* msgid = ldap_search( ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, "cn~=bob",
|
1998-08-09 08:43:13 +08:00
|
|
|
* attrs, attrsonly );
|
|
|
|
*/
|
|
|
|
int
|
1999-05-22 03:20:25 +08:00
|
|
|
ldap_search(
|
|
|
|
LDAP *ld, LDAP_CONST char *base, int scope, LDAP_CONST char *filter,
|
1998-08-09 08:43:13 +08:00
|
|
|
char **attrs, int attrsonly )
|
|
|
|
{
|
|
|
|
BerElement *ber;
|
|
|
|
|
2002-04-02 06:31:44 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2002-07-12 04:33:24 +08:00
|
|
|
LDAP_LOG ( OPERATION, ENTRY, "ldap_search\n", 0, 0, 0 );
|
2002-04-02 06:31:44 +08:00
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
|
2002-04-02 06:31:44 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-19 00:32:57 +08:00
|
|
|
assert( ld != NULL );
|
|
|
|
assert( LDAP_VALID( ld ) );
|
|
|
|
|
1999-05-22 03:20:25 +08:00
|
|
|
ber = ldap_build_search_req( ld, base, scope, filter, attrs,
|
|
|
|
attrsonly, NULL, NULL, -1, -1 );
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( ber == NULL ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* send the message */
|
1998-08-09 11:04:12 +08:00
|
|
|
return ( ldap_send_initial_request( ld, LDAP_REQ_SEARCH, base, ber ));
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BerElement *
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_build_search_req(
|
|
|
|
LDAP *ld,
|
1999-08-29 06:14:21 +08:00
|
|
|
LDAP_CONST char *base,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_int_t scope,
|
2002-01-04 11:50:54 +08:00
|
|
|
LDAP_CONST char *filter,
|
1999-05-19 09:12:33 +08:00
|
|
|
char **attrs,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_int_t attrsonly,
|
1999-05-19 09:12:33 +08:00
|
|
|
LDAPControl **sctrls,
|
1999-05-22 03:20:25 +08:00
|
|
|
LDAPControl **cctrls,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_int_t timelimit,
|
|
|
|
ber_int_t sizelimit )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
BerElement *ber;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the search request. It looks like this:
|
|
|
|
* SearchRequest := [APPLICATION 3] SEQUENCE {
|
|
|
|
* baseObject DistinguishedName,
|
|
|
|
* scope ENUMERATED {
|
|
|
|
* baseObject (0),
|
|
|
|
* singleLevel (1),
|
|
|
|
* wholeSubtree (2)
|
|
|
|
* },
|
|
|
|
* derefAliases ENUMERATED {
|
|
|
|
* neverDerefaliases (0),
|
|
|
|
* derefInSearching (1),
|
|
|
|
* derefFindingBaseObj (2),
|
|
|
|
* alwaysDerefAliases (3)
|
|
|
|
* },
|
|
|
|
* sizelimit INTEGER (0 .. 65535),
|
|
|
|
* timelimit INTEGER (0 .. 65535),
|
|
|
|
* attrsOnly BOOLEAN,
|
|
|
|
* filter Filter,
|
|
|
|
* attributes SEQUENCE OF AttributeType
|
|
|
|
* }
|
|
|
|
* wrapped in an ldap message.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* create a message to send */
|
1999-06-19 07:53:05 +08:00
|
|
|
if ( (ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
|
|
|
|
return( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-08-29 06:14:21 +08:00
|
|
|
if ( base == NULL ) {
|
1998-12-02 04:08:39 +08:00
|
|
|
/* no base provided, use session default base */
|
|
|
|
base = ld->ld_options.ldo_defbase;
|
|
|
|
|
1999-08-29 06:14:21 +08:00
|
|
|
if ( base == NULL ) {
|
|
|
|
/* no session default base, use top */
|
|
|
|
base = "";
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2001-09-28 08:18:40 +08:00
|
|
|
#ifdef LDAP_CONNECTIONLESS
|
|
|
|
if ( LDAP_IS_UDP(ld) ) {
|
|
|
|
err = ber_write( ber, ld->ld_options.ldo_peer,
|
|
|
|
sizeof(struct sockaddr), 0);
|
2001-10-02 09:02:23 +08:00
|
|
|
}
|
|
|
|
if ( LDAP_IS_UDP(ld) && ld->ld_options.ldo_version == LDAP_VERSION2) {
|
|
|
|
char *dn = ld->ld_options.ldo_cldapdn;
|
|
|
|
if (!dn) dn = "";
|
|
|
|
err = ber_printf( ber, "{ist{seeiib", ++ld->ld_msgid, dn,
|
|
|
|
LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
|
|
|
|
(sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
|
|
|
|
(timelimit < 0) ? ld->ld_timelimit : timelimit,
|
|
|
|
attrsonly );
|
2001-09-28 08:18:40 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = ber_printf( ber, "{it{seeiib", ++ld->ld_msgid,
|
2000-08-18 12:25:00 +08:00
|
|
|
LDAP_REQ_SEARCH, base, (ber_int_t) scope, ld->ld_deref,
|
|
|
|
(sizelimit < 0) ? ld->ld_sizelimit : sizelimit,
|
|
|
|
(timelimit < 0) ? ld->ld_timelimit : timelimit,
|
|
|
|
attrsonly );
|
2001-09-28 08:18:40 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( err == -1 ) {
|
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
|
|
|
ber_free( ber, 1 );
|
1999-06-19 07:53:05 +08:00
|
|
|
return( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2002-01-04 11:50:54 +08:00
|
|
|
if( filter == NULL ) {
|
|
|
|
filter = "(objectclass=*)";
|
2000-04-25 21:52:44 +08:00
|
|
|
}
|
2002-01-04 11:50:54 +08:00
|
|
|
|
2002-03-30 08:42:40 +08:00
|
|
|
err = ldap_pvt_put_filter( ber, filter );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( err == -1 ) {
|
|
|
|
ld->ld_errno = LDAP_FILTER_ERROR;
|
|
|
|
ber_free( ber, 1 );
|
1999-06-19 07:53:05 +08:00
|
|
|
return( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
if ( ber_printf( ber, /*{*/ "{v}N}", attrs ) == -1 ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
|
|
|
ber_free( ber, 1 );
|
1999-06-19 07:53:05 +08:00
|
|
|
return( NULL );
|
1999-05-19 09:12:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put Server Controls */
|
|
|
|
if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
|
|
|
|
ber_free( ber, 1 );
|
1999-06-19 07:53:05 +08:00
|
|
|
return( NULL );
|
1999-05-19 09:12:33 +08:00
|
|
|
}
|
|
|
|
|
2000-06-24 09:40:39 +08:00
|
|
|
if ( ber_printf( ber, /*{*/ "N}" ) == -1 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
ld->ld_errno = LDAP_ENCODING_ERROR;
|
|
|
|
ber_free( ber, 1 );
|
1999-06-19 07:53:05 +08:00
|
|
|
return( NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ber );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_search_st(
|
|
|
|
LDAP *ld, LDAP_CONST char *base, int scope,
|
|
|
|
LDAP_CONST char *filter, char **attrs,
|
1998-08-09 08:43:13 +08:00
|
|
|
int attrsonly, struct timeval *timeout, LDAPMessage **res )
|
|
|
|
{
|
|
|
|
int msgid;
|
|
|
|
|
|
|
|
if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
|
|
|
|
== -1 )
|
|
|
|
return( ld->ld_errno );
|
|
|
|
|
|
|
|
if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 )
|
|
|
|
return( ld->ld_errno );
|
|
|
|
|
|
|
|
if ( ld->ld_errno == LDAP_TIMEOUT ) {
|
|
|
|
(void) ldap_abandon( ld, msgid );
|
|
|
|
ld->ld_errno = LDAP_TIMEOUT;
|
|
|
|
return( ld->ld_errno );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( ldap_result2error( ld, *res, 0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1999-05-19 09:12:33 +08:00
|
|
|
ldap_search_s(
|
|
|
|
LDAP *ld,
|
|
|
|
LDAP_CONST char *base,
|
|
|
|
int scope,
|
|
|
|
LDAP_CONST char *filter,
|
|
|
|
char **attrs,
|
|
|
|
int attrsonly,
|
|
|
|
LDAPMessage **res )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
int msgid;
|
|
|
|
|
|
|
|
if ( (msgid = ldap_search( ld, base, scope, filter, attrs, attrsonly ))
|
|
|
|
== -1 )
|
|
|
|
return( ld->ld_errno );
|
|
|
|
|
|
|
|
if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, res ) == -1 )
|
|
|
|
return( ld->ld_errno );
|
|
|
|
|
|
|
|
return( ldap_result2error( ld, *res, 0 ) );
|
|
|
|
}
|
|
|
|
|