1N/A/*
1N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
1N/A */
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A/*
1N/A * Copyright (c) 1990 Regents of the University of Michigan.
1N/A * All rights reserved.
1N/A */
1N/A/*
1N/A * search.c
1N/A */
1N/A
1N/A#if 0
1N/A#ifndef lint
1N/Astatic char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
1N/A#endif
1N/A#endif
1N/A
1N/A#include "ldap-int.h"
1N/A
1N/Astatic int nsldapi_timeval2ldaplimit( struct timeval *timeoutp,
1N/A int defaultvalue );
1N/Astatic int nsldapi_search( LDAP *ld, const char *base, int scope,
1N/A const char *filter, char **attrs, int attrsonly,
1N/A LDAPControl **serverctrls, LDAPControl **clientctrls,
1N/A int timelimit, int sizelimit, int *msgidp );
1N/Astatic char *find_right_paren( char *s );
1N/Astatic char *put_complex_filter( BerElement *ber, char *str,
1N/A ber_tag_t tag, int not );
1N/Astatic int unescape_filterval( char *str );
1N/Astatic int hexchar2int( char c );
1N/Astatic int is_valid_attr( char *a );
1N/Astatic int put_simple_filter( BerElement *ber, char *str );
1N/Astatic int put_substring_filter( BerElement *ber, char *type,
1N/A char *str );
1N/Astatic int put_filter_list( BerElement *ber, char *str );
1N/Astatic int nsldapi_search_s( LDAP *ld, const char *base, int scope,
1N/A const char *filter, char **attrs, int attrsonly,
1N/A LDAPControl **serverctrls, LDAPControl **clientctrls,
1N/A struct timeval *localtimeoutp, int timelimit, int sizelimit,
1N/A LDAPMessage **res );
1N/A
1N/A/*
1N/A * ldap_search - initiate an ldap search operation. Parameters:
1N/A *
1N/A * ld LDAP descriptor
1N/A * base DN of the base object
1N/A * scope the search scope - one of LDAP_SCOPE_BASE,
1N/A * LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE
1N/A * filter a string containing the search filter
1N/A * (e.g., "(|(cn=bob)(sn=bob))")
1N/A * attrs list of attribute types to return for matches
1N/A * attrsonly 1 => attributes only 0 => attributes and values
1N/A *
1N/A * Example:
1N/A * char *attrs[] = { "mail", "title", 0 };
1N/A * msgid = ldap_search( ld, "c=us@o=UM", LDAP_SCOPE_SUBTREE, "cn~=bob",
1N/A * attrs, attrsonly );
1N/A */
1N/Aint
1N/ALDAP_CALL
1N/Aldap_search(
1N/A LDAP *ld,
1N/A const char *base,
1N/A int scope,
1N/A const char *filter,
1N/A char **attrs,
1N/A int attrsonly
1N/A)
1N/A{
1N/A int msgid;
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_search\n", 0, 0, 0 );
1N/A
1N/A if ( ldap_search_ext( ld, base, scope, filter, attrs, attrsonly, NULL,
1N/A NULL, NULL, -1, &msgid ) == LDAP_SUCCESS ) {
1N/A return( msgid );
1N/A } else {
1N/A return( -1 ); /* error is in ld handle */
1N/A }
1N/A}
1N/A
1N/A
1N/A/*
1N/A * LDAPv3 extended search.
1N/A * Returns an LDAP error code.
1N/A */
1N/Aint
1N/ALDAP_CALL
1N/Aldap_search_ext(
1N/A LDAP *ld,
1N/A const char *base,
1N/A int scope,
1N/A const char *filter,
1N/A char **attrs,
1N/A int attrsonly,
1N/A LDAPControl **serverctrls,
1N/A LDAPControl **clientctrls,
1N/A struct timeval *timeoutp, /* NULL means use ld->ld_timelimit */
1N/A int sizelimit,
1N/A int *msgidp
1N/A)
1N/A{
1N/A /*
1N/A * It is an error to pass in a zero'd timeval.
1N/A */
1N/A if ( timeoutp != NULL && timeoutp->tv_sec == 0 &&
1N/A timeoutp->tv_usec == 0 ) {
1N/A if ( ld != NULL ) {
1N/A LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A }
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A return( nsldapi_search( ld, base, scope, filter, attrs, attrsonly,
1N/A serverctrls, clientctrls,
1N/A nsldapi_timeval2ldaplimit( timeoutp, -1 ), sizelimit, msgidp ));
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Like ldap_search_ext() except an integer timelimit is passed instead of
1N/A * using the overloaded struct timeval *timeoutp.
1N/A */
1N/Astatic int
1N/Ansldapi_search(
1N/A LDAP *ld,
1N/A const char *base,
1N/A int scope,
1N/A const char *filter,
1N/A char **attrs,
1N/A int attrsonly,
1N/A LDAPControl **serverctrls,
1N/A LDAPControl **clientctrls,
1N/A int timelimit, /* -1 means use ld->ld_timelimit */
1N/A int sizelimit, /* -1 means use ld->ld_sizelimit */
1N/A int *msgidp
1N/A)
1N/A{
1N/A BerElement *ber;
1N/A int rc, rc_key;
1N/A unsigned long key; /* XXXmcs: memcache */
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "ldap_search_ext\n", 0, 0, 0 );
1N/A
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( base == NULL ) {
1N/A base = "";
1N/A }
1N/A
1N/A if ( filter == NULL ) {
1N/A filter = "(objectclass=*)";
1N/A }
1N/A
1N/A if ( msgidp == NULL || ( scope != LDAP_SCOPE_BASE
1N/A && scope != LDAP_SCOPE_ONELEVEL && scope != LDAP_SCOPE_SUBTREE )
1N/A || ( sizelimit < -1 )) {
1N/A LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A LDAP_MUTEX_LOCK( ld, LDAP_MSGID_LOCK );
1N/A *msgidp = ++ld->ld_msgid;
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_MSGID_LOCK );
1N/A
1N/A /*
1N/A * XXXmcs: should use cache function pointers to hook in memcache
1N/A */
1N/A if ( ld->ld_memcache == NULL ) {
1N/A rc_key = LDAP_NOT_SUPPORTED;
1N/A } else if (( rc_key = ldap_memcache_createkey( ld, base, scope, filter,
1N/A attrs, attrsonly, serverctrls, clientctrls, &key)) == LDAP_SUCCESS
1N/A && ldap_memcache_result( ld, *msgidp, key ) == LDAP_SUCCESS ) {
1N/A return LDAP_SUCCESS;
1N/A }
1N/A
1N/A /* check the cache */
1N/A if ( ld->ld_cache_on && ld->ld_cache_search != NULL ) {
1N/A LDAP_MUTEX_LOCK( ld, LDAP_CACHE_LOCK );
1N/A if ( (rc = (ld->ld_cache_search)( ld, *msgidp, LDAP_REQ_SEARCH,
1N/A base, scope, filter, attrs, attrsonly )) != 0 ) {
1N/A *msgidp = rc;
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1N/A return( LDAP_SUCCESS );
1N/A }
1N/A LDAP_MUTEX_UNLOCK( ld, LDAP_CACHE_LOCK );
1N/A }
1N/A
1N/A /* caching off or did not find it in the cache - check the net */
1N/A if (( rc = nsldapi_build_search_req( ld, base, scope, filter, attrs,
1N/A attrsonly, serverctrls, clientctrls, timelimit, sizelimit,
1N/A *msgidp, &ber )) != LDAP_SUCCESS ) {
1N/A return( rc );
1N/A }
1N/A
1N/A /* send the message */
1N/A rc = nsldapi_send_initial_request( ld, *msgidp, LDAP_REQ_SEARCH,
1N/A (char *) base, ber );
1N/A
1N/A /*
1N/A * XXXmcs: should use cache function pointers to hook in memcache
1N/A */
1N/A if ( (rc_key == LDAP_SUCCESS) && (rc >= 0) ) {
1N/A ldap_memcache_new( ld, rc, key, base );
1N/A }
1N/A
1N/A *msgidp = rc;
1N/A return( rc < 0 ? LDAP_GET_LDERRNO( ld, NULL, NULL ) : LDAP_SUCCESS );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Convert a non-NULL timeoutp to a value in seconds that is appropriate to
1N/A * send in an LDAP search request. If timeoutp is NULL, return defaultvalue.
1N/A */
1N/Astatic int
1N/Ansldapi_timeval2ldaplimit( struct timeval *timeoutp, int defaultvalue )
1N/A{
1N/A int timelimit;
1N/A
1N/A if ( NULL == timeoutp ) {
1N/A timelimit = defaultvalue;
1N/A } else if ( timeoutp->tv_sec > 0 ) {
1N/A timelimit = timeoutp->tv_sec;
1N/A } else if ( timeoutp->tv_usec > 0 ) {
1N/A timelimit = 1; /* minimum we can express in LDAP */
1N/A } else {
1N/A /*
1N/A * both tv_sec and tv_usec are less than one (zero?) so
1N/A * to maintain compatiblity with our "zero means no limit"
1N/A * convention we pass no limit to the server.
1N/A */
1N/A timelimit = 0; /* no limit */
1N/A }
1N/A
1N/A return( timelimit );
1N/A}
1N/A
1N/A
1N/A/* returns an LDAP error code and also sets it in ld */
1N/Aint
1N/Ansldapi_build_search_req(
1N/A LDAP *ld,
1N/A const char *base,
1N/A int scope,
1N/A const char *filter,
1N/A char **attrs,
1N/A int attrsonly,
1N/A LDAPControl **serverctrls,
1N/A LDAPControl **clientctrls, /* not used for anything yet */
1N/A int timelimit, /* if -1, ld->ld_timelimit is used */
1N/A int sizelimit, /* if -1, ld->ld_sizelimit is used */
1N/A int msgid,
1N/A BerElement **berp
1N/A)
1N/A{
1N/A BerElement *ber;
1N/A int err;
1N/A char *fdup;
1N/A
1N/A /*
1N/A * Create the search request. It looks like this:
1N/A * SearchRequest := [APPLICATION 3] SEQUENCE {
1N/A * baseObject DistinguishedName,
1N/A * scope ENUMERATED {
1N/A * baseObject (0),
1N/A * singleLevel (1),
1N/A * wholeSubtree (2)
1N/A * },
1N/A * derefAliases ENUMERATED {
1N/A * neverDerefaliases (0),
1N/A * derefInSearching (1),
1N/A * derefFindingBaseObj (2),
1N/A * alwaysDerefAliases (3)
1N/A * },
1N/A * sizelimit INTEGER (0 .. 65535),
1N/A * timelimit INTEGER (0 .. 65535),
1N/A * attrsOnly BOOLEAN,
1N/A * filter Filter,
1N/A * attributes SEQUENCE OF AttributeType
1N/A * }
1N/A * wrapped in an ldap message.
1N/A */
1N/A
1N/A /* create a message to send */
1N/A if (( err = nsldapi_alloc_ber_with_options( ld, &ber ))
1N/A != LDAP_SUCCESS ) {
1N/A return( err );
1N/A }
1N/A
1N/A if ( base == NULL ) {
1N/A base = "";
1N/A }
1N/A
1N/A if ( sizelimit == -1 ) {
1N/A sizelimit = ld->ld_sizelimit;
1N/A }
1N/A
1N/A if ( timelimit == -1 ) {
1N/A timelimit = ld->ld_timelimit;
1N/A }
1N/A
1N/A#ifdef CLDAP
1N/A if ( ld->ld_sbp->sb_naddr > 0 ) {
1N/A err = ber_printf( ber, "{ist{seeiib", msgid,
1N/A ld->ld_cldapdn, LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
1N/A sizelimit, timelimit, attrsonly );
1N/A } else {
1N/A#endif /* CLDAP */
1N/A err = ber_printf( ber, "{it{seeiib", msgid,
1N/A LDAP_REQ_SEARCH, base, scope, ld->ld_deref,
1N/A sizelimit, timelimit, attrsonly );
1N/A#ifdef CLDAP
1N/A }
1N/A#endif /* CLDAP */
1N/A
1N/A if ( err == -1 ) {
1N/A LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
1N/A ber_free( ber, 1 );
1N/A return( LDAP_ENCODING_ERROR );
1N/A }
1N/A
1N/A fdup = nsldapi_strdup( filter );
1N/A if (fdup == NULL) {
1N/A LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
1N/A ber_free( ber, 1 );
1N/A return( LDAP_NO_MEMORY );
1N/A }
1N/A err = ldap_put_filter( ber, fdup );
1N/A NSLDAPI_FREE( fdup );
1N/A
1N/A if ( err == -1 ) {
1N/A LDAP_SET_LDERRNO( ld, LDAP_FILTER_ERROR, NULL, NULL );
1N/A ber_free( ber, 1 );
1N/A return( LDAP_FILTER_ERROR );
1N/A }
1N/A
1N/A if ( ber_printf( ber, "{v}}", attrs ) == -1 ) {
1N/A LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
1N/A ber_free( ber, 1 );
1N/A return( LDAP_ENCODING_ERROR );
1N/A }
1N/A
1N/A if ( (err = nsldapi_put_controls( ld, serverctrls, 1, ber ))
1N/A != LDAP_SUCCESS ) {
1N/A ber_free( ber, 1 );
1N/A return( err );
1N/A }
1N/A
1N/A *berp = ber;
1N/A return( LDAP_SUCCESS );
1N/A}
1N/A
1N/Astatic char *
1N/Afind_right_paren( char *s )
1N/A{
1N/A int balance, escape;
1N/A
1N/A balance = 1;
1N/A escape = 0;
1N/A while ( *s && balance ) {
1N/A if ( escape == 0 ) {
1N/A if ( *s == '(' )
1N/A balance++;
1N/A else if ( *s == ')' )
1N/A balance--;
1N/A }
1N/A if ( *s == '\\' && ! escape )
1N/A escape = 1;
1N/A else
1N/A escape = 0;
1N/A if ( balance )
1N/A s++;
1N/A }
1N/A
1N/A return( *s ? s : NULL );
1N/A}
1N/A
1N/Astatic char *
1N/Aput_complex_filter(
1N/A BerElement *ber,
1N/A char *str,
1N/A ber_tag_t tag,
1N/A int not
1N/A)
1N/A{
1N/A char *next;
1N/A
1N/A /*
1N/A * We have (x(filter)...) with str sitting on
1N/A * the x. We have to find the paren matching
1N/A * the one before the x and put the intervening
1N/A * filters by calling put_filter_list().
1N/A */
1N/A
1N/A /* put explicit tag */
1N/A if ( ber_printf( ber, "t{", tag ) == -1 )
1N/A return( NULL );
1N/A
1N/A str++;
1N/A if ( (next = find_right_paren( str )) == NULL )
1N/A return( NULL );
1N/A
1N/A *next = '\0';
1N/A if ( put_filter_list( ber, str ) == -1 )
1N/A return( NULL );
1N/A *next++ = ')';
1N/A
1N/A /* flush explicit tagged thang */
1N/A if ( ber_printf( ber, "}" ) == -1 )
1N/A return( NULL );
1N/A
1N/A return( next );
1N/A}
1N/A
1N/Aint
1N/Aldap_put_filter( BerElement *ber, char *str )
1N/A{
1N/A char *next;
1N/A int parens, balance, escape;
1N/A
1N/A /*
1N/A * A Filter looks like this:
1N/A * Filter ::= CHOICE {
1N/A * and [0] SET OF Filter,
1N/A * or [1] SET OF Filter,
1N/A * not [2] Filter,
1N/A * equalityMatch [3] AttributeValueAssertion,
1N/A * substrings [4] SubstringFilter,
1N/A * greaterOrEqual [5] AttributeValueAssertion,
1N/A * lessOrEqual [6] AttributeValueAssertion,
1N/A * present [7] AttributeType,,
1N/A * approxMatch [8] AttributeValueAssertion
1N/A * }
1N/A *
1N/A * SubstringFilter ::= SEQUENCE {
1N/A * type AttributeType,
1N/A * SEQUENCE OF CHOICE {
1N/A * initial [0] IA5String,
1N/A * any [1] IA5String,
1N/A * final [2] IA5String
1N/A * }
1N/A * }
1N/A * Note: tags in a choice are always explicit
1N/A */
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "put_filter \"%s\"\n", str, 0, 0 );
1N/A
1N/A parens = 0;
1N/A while ( *str ) {
1N/A switch ( *str ) {
1N/A case '(':
1N/A str++;
1N/A parens++;
1N/A switch ( *str ) {
1N/A case '&':
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
1N/A 0, 0, 0 );
1N/A
1N/A if ( (str = put_complex_filter( ber, str,
1N/A LDAP_FILTER_AND, 0 )) == NULL )
1N/A return( -1 );
1N/A
1N/A parens--;
1N/A break;
1N/A
1N/A case '|':
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
1N/A 0, 0, 0 );
1N/A
1N/A if ( (str = put_complex_filter( ber, str,
1N/A LDAP_FILTER_OR, 0 )) == NULL )
1N/A return( -1 );
1N/A
1N/A parens--;
1N/A break;
1N/A
1N/A case '!':
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
1N/A 0, 0, 0 );
1N/A
1N/A if ( (str = put_complex_filter( ber, str,
1N/A LDAP_FILTER_NOT, 1 )) == NULL )
1N/A return( -1 );
1N/A
1N/A parens--;
1N/A break;
1N/A
1N/A default:
1N/A LDAPDebug( LDAP_DEBUG_TRACE,
1N/A "put_filter: simple\n", 0, 0, 0 );
1N/A
1N/A balance = 1;
1N/A escape = 0;
1N/A next = str;
1N/A while ( *next && balance ) {
1N/A if ( escape == 0 ) {
1N/A if ( *next == '(' )
1N/A balance++;
1N/A else if ( *next == ')' )
1N/A balance--;
1N/A }
1N/A if ( *next == '\\' && ! escape )
1N/A escape = 1;
1N/A else
1N/A escape = 0;
1N/A if ( balance )
1N/A next++;
1N/A }
1N/A if ( balance != 0 )
1N/A return( -1 );
1N/A
1N/A *next = '\0';
1N/A if ( put_simple_filter( ber, str ) == -1 ) {
1N/A return( -1 );
1N/A }
1N/A *next++ = ')';
1N/A str = next;
1N/A parens--;
1N/A break;
1N/A }
1N/A break;
1N/A
1N/A case ')':
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: end\n", 0, 0,
1N/A 0 );
1N/A if ( ber_printf( ber, "]" ) == -1 )
1N/A return( -1 );
1N/A str++;
1N/A parens--;
1N/A break;
1N/A
1N/A case ' ':
1N/A str++;
1N/A break;
1N/A
1N/A default: /* assume it's a simple type=value filter */
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "put_filter: default\n", 0, 0,
1N/A 0 );
1N/A next = strchr( str, '\0' );
1N/A if ( put_simple_filter( ber, str ) == -1 ) {
1N/A return( -1 );
1N/A }
1N/A str = next;
1N/A break;
1N/A }
1N/A }
1N/A
1N/A return( parens ? -1 : 0 );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Put a list of filters like this "(filter1)(filter2)..."
1N/A */
1N/A
1N/Astatic int
1N/Aput_filter_list( BerElement *ber, char *str )
1N/A{
1N/A char *next;
1N/A char save;
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n", str, 0, 0 );
1N/A
1N/A while ( *str ) {
1N/A while ( *str && isspace( *str ) )
1N/A str++;
1N/A if ( *str == '\0' )
1N/A break;
1N/A
1N/A if ( (next = find_right_paren( str + 1 )) == NULL )
1N/A return( -1 );
1N/A save = *++next;
1N/A
1N/A /* now we have "(filter)" with str pointing to it */
1N/A *next = '\0';
1N/A if ( ldap_put_filter( ber, str ) == -1 )
1N/A return( -1 );
1N/A *next = save;
1N/A
1N/A str = next;
1N/A }
1N/A
1N/A return( 0 );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * is_valid_attr - returns 1 if a is a syntactically valid left-hand side
1N/A * of a filter expression, 0 otherwise. A valid string may contain only
1N/A * letters, numbers, hyphens, semicolons, colons and periods. examples:
1N/A * cn
1N/A * cn;lang-fr
1N/A * 1.2.3.4;binary;dynamic
1N/A * mail;dynamic
1N/A * cn:dn:1.2.3.4
1N/A *
1N/A * For compatibility with older servers, we also allow underscores in
1N/A * attribute types, even through they are not allowed by the LDAPv3 RFCs.
1N/A */
1N/Astatic int
1N/Ais_valid_attr( char *a )
1N/A{
1N/A for ( ; *a; a++ ) {
1N/A if ( !isascii( *a ) ) {
1N/A return( 0 );
1N/A } else if ( !isalnum( *a ) ) {
1N/A switch ( *a ) {
1N/A case '-':
1N/A case '.':
1N/A case ';':
1N/A case ':':
1N/A case '_':
1N/A break; /* valid */
1N/A default:
1N/A return( 0 );
1N/A }
1N/A }
1N/A }
1N/A
1N/A return( 1 );
1N/A}
1N/A
1N/Astatic char *
1N/Afind_star( char *s )
1N/A{
1N/A for ( ; *s; ++s ) {
1N/A switch ( *s ) {
1N/A case '*': return s;
1N/A case '\\':
1N/A ++s;
1N/A if ( hexchar2int(s[0]) >= 0 && hexchar2int(s[1]) >= 0 ) ++s;
1N/A default: break;
1N/A }
1N/A }
1N/A return NULL;
1N/A}
1N/A
1N/Astatic int
1N/Aput_simple_filter( BerElement *ber, char *str )
1N/A{
1N/A char *s, *s2, *s3, filterop;
1N/A char *value;
1N/A ber_uint_t ftype;
1N/A int rc, len;
1N/A char *oid; /* for v3 extended filter */
1N/A int dnattr; /* for v3 extended filter */
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "put_simple_filter \"%s\"\n", str, 0, 0 );
1N/A
1N/A rc = -1; /* pessimistic */
1N/A
1N/A if (( str = nsldapi_strdup( str )) == NULL ) {
1N/A return( rc );
1N/A }
1N/A
1N/A if ( (s = strchr( str, '=' )) == NULL ) {
1N/A goto free_and_return;
1N/A }
1N/A value = s + 1;
1N/A *s-- = '\0';
1N/A filterop = *s;
1N/A if ( filterop == '<' || filterop == '>' || filterop == '~' ||
1N/A filterop == ':' ) {
1N/A *s = '\0';
1N/A }
1N/A
1N/A if ( ! is_valid_attr( str ) ) {
1N/A goto free_and_return;
1N/A }
1N/A
1N/A switch ( filterop ) {
1N/A case '<':
1N/A ftype = LDAP_FILTER_LE;
1N/A break;
1N/A case '>':
1N/A ftype = LDAP_FILTER_GE;
1N/A break;
1N/A case '~':
1N/A ftype = LDAP_FILTER_APPROX;
1N/A break;
1N/A case ':': /* extended filter - v3 only */
1N/A /*
1N/A * extended filter looks like this:
1N/A *
1N/A * [type][':dn'][':'oid]':='value
1N/A *
1N/A * where one of type or :oid is required.
1N/A *
1N/A */
1N/A ftype = LDAP_FILTER_EXTENDED;
1N/A s2 = s3 = NULL;
1N/A if ( (s2 = strrchr( str, ':' )) == NULL ) {
1N/A goto free_and_return;
1N/A }
1N/A if ( strcasecmp( s2, ":dn" ) == 0 ) {
1N/A oid = NULL;
1N/A dnattr = 1;
1N/A *s2 = '\0';
1N/A } else {
1N/A oid = s2 + 1;
1N/A dnattr = 0;
1N/A *s2 = '\0';
1N/A if ( (s3 = strrchr( str, ':' )) != NULL ) {
1N/A if ( strcasecmp( s3, ":dn" ) == 0 ) {
1N/A dnattr = 1;
1N/A } else {
1N/A goto free_and_return;
1N/A }
1N/A *s3 = '\0';
1N/A }
1N/A }
1N/A if ( (rc = ber_printf( ber, "t{", ftype )) == -1 ) {
1N/A goto free_and_return;
1N/A }
1N/A if ( oid != NULL ) {
1N/A if ( (rc = ber_printf( ber, "ts", LDAP_TAG_MRA_OID,
1N/A oid )) == -1 ) {
1N/A goto free_and_return;
1N/A }
1N/A }
1N/A if ( *str != '\0' ) {
1N/A if ( (rc = ber_printf( ber, "ts",
1N/A LDAP_TAG_MRA_TYPE, str )) == -1 ) {
1N/A goto free_and_return;
1N/A }
1N/A }
1N/A if (( len = unescape_filterval( value )) < 0 ||
1N/A ( rc = ber_printf( ber, "totb}", LDAP_TAG_MRA_VALUE,
1N/A value, len, LDAP_TAG_MRA_DNATTRS, dnattr )) == -1 ) {
1N/A goto free_and_return;
1N/A }
1N/A rc = 0;
1N/A goto free_and_return;
1N/A /* break; */
1N/A default:
1N/A if ( find_star( value ) == NULL ) {
1N/A ftype = LDAP_FILTER_EQUALITY;
1N/A } else if ( strcmp( value, "*" ) == 0 ) {
1N/A ftype = LDAP_FILTER_PRESENT;
1N/A } else {
1N/A rc = put_substring_filter( ber, str, value );
1N/A goto free_and_return;
1N/A }
1N/A break;
1N/A }
1N/A
1N/A if ( ftype == LDAP_FILTER_PRESENT ) {
1N/A rc = ber_printf( ber, "ts", ftype, str );
1N/A } else if (( len = unescape_filterval( value )) >= 0 ) {
1N/A rc = ber_printf( ber, "t{so}", ftype, str, value, len );
1N/A }
1N/A if ( rc != -1 ) {
1N/A rc = 0;
1N/A }
1N/A
1N/Afree_and_return:
1N/A NSLDAPI_FREE( str );
1N/A return( rc );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * Undo in place both LDAPv2 (RFC-1960) and LDAPv3 (hexadecimal) escape
1N/A * sequences within the null-terminated string 'val'. The resulting value
1N/A * may contain null characters.
1N/A *
1N/A * If 'val' contains invalid escape sequences we return -1.
1N/A * Otherwise the length of the unescaped value is returned.
1N/A */
1N/Astatic int
1N/Aunescape_filterval( char *val )
1N/A{
1N/A int escape, firstdigit, ival;
1N/A char *s, *d;
1N/A
1N/A escape = 0;
1N/A for ( s = d = val; *s; s++ ) {
1N/A if ( escape ) {
1N/A /*
1N/A * first try LDAPv3 escape (hexadecimal) sequence
1N/A */
1N/A if (( ival = hexchar2int( *s )) < 0 ) {
1N/A if ( firstdigit ) {
1N/A /*
1N/A * LDAPv2 (RFC1960) escape sequence
1N/A */
1N/A *d++ = *s;
1N/A escape = 0;
1N/A } else {
1N/A return(-1);
1N/A }
1N/A }
1N/A if ( firstdigit ) {
1N/A *d = ( ival<<4 );
1N/A firstdigit = 0;
1N/A } else {
1N/A *d++ |= ival;
1N/A escape = 0;
1N/A }
1N/A
1N/A } else if ( *s != '\\' ) {
1N/A *d++ = *s;
1N/A escape = 0;
1N/A
1N/A } else {
1N/A escape = 1;
1N/A firstdigit = 1;
1N/A }
1N/A }
1N/A
1N/A return( d - val );
1N/A}
1N/A
1N/A
1N/A/*
1N/A * convert character 'c' that represents a hexadecimal digit to an integer.
1N/A * if 'c' is not a hexidecimal digit [0-9A-Fa-f], -1 is returned.
1N/A * otherwise the converted value is returned.
1N/A */
1N/Astatic int
1N/Ahexchar2int( char c )
1N/A{
1N/A if ( c >= '0' && c <= '9' ) {
1N/A return( c - '0' );
1N/A }
1N/A if ( c >= 'A' && c <= 'F' ) {
1N/A return( c - 'A' + 10 );
1N/A }
1N/A if ( c >= 'a' && c <= 'f' ) {
1N/A return( c - 'a' + 10 );
1N/A }
1N/A return( -1 );
1N/A}
1N/A
1N/Astatic int
1N/Aput_substring_filter( BerElement *ber, char *type, char *val )
1N/A{
1N/A char *nextstar, gotstar = 0;
1N/A ber_uint_t ftype;
1N/A int len;
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n", type,
1N/A val, 0 );
1N/A
1N/A if ( ber_printf( ber, "t{s{", LDAP_FILTER_SUBSTRINGS, type ) == -1 ) {
1N/A return( -1 );
1N/A }
1N/A
1N/A for ( ; val != NULL; val = nextstar ) {
1N/A if ( (nextstar = find_star( val )) != NULL ) {
1N/A *nextstar++ = '\0';
1N/A }
1N/A
1N/A if ( gotstar == 0 ) {
1N/A ftype = LDAP_SUBSTRING_INITIAL;
1N/A } else if ( nextstar == NULL ) {
1N/A ftype = LDAP_SUBSTRING_FINAL;
1N/A } else {
1N/A ftype = LDAP_SUBSTRING_ANY;
1N/A }
1N/A if ( *val != '\0' ) {
1N/A if (( len = unescape_filterval( val )) < 0 ||
1N/A ber_printf( ber, "to", ftype, val, len ) == -1 ) {
1N/A return( -1 );
1N/A }
1N/A }
1N/A
1N/A gotstar = 1;
1N/A }
1N/A
1N/A if ( ber_printf( ber, "}}" ) == -1 ) {
1N/A return( -1 );
1N/A }
1N/A
1N/A return( 0 );
1N/A}
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_search_st(
1N/A LDAP *ld,
1N/A const char *base,
1N/A int scope,
1N/A const char *filter,
1N/A char **attrs,
1N/A int attrsonly,
1N/A struct timeval *timeout,
1N/A LDAPMessage **res
1N/A)
1N/A{
1N/A return( nsldapi_search_s( ld, base, scope, filter, attrs, attrsonly,
1N/A NULL, NULL, timeout, -1, -1, res ));
1N/A}
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_search_s(
1N/A LDAP *ld,
1N/A const char *base,
1N/A int scope,
1N/A const char *filter,
1N/A char **attrs,
1N/A int attrsonly,
1N/A LDAPMessage **res
1N/A)
1N/A{
1N/A return( nsldapi_search_s( ld, base, scope, filter, attrs, attrsonly,
1N/A NULL, NULL, NULL, -1, -1, res ));
1N/A}
1N/A
1N/Aint LDAP_CALL
1N/Aldap_search_ext_s(
1N/A LDAP *ld,
1N/A const char *base,
1N/A int scope,
1N/A const char *filter,
1N/A char **attrs,
1N/A int attrsonly,
1N/A LDAPControl **serverctrls,
1N/A LDAPControl **clientctrls,
1N/A struct timeval *timeoutp,
1N/A int sizelimit,
1N/A LDAPMessage **res
1N/A)
1N/A{
1N/A return( nsldapi_search_s( ld, base, scope, filter, attrs, attrsonly,
1N/A serverctrls, clientctrls, timeoutp,
1N/A nsldapi_timeval2ldaplimit( timeoutp, -1 ), sizelimit, res ));
1N/A}
1N/A
1N/A
1N/Astatic int
1N/Ansldapi_search_s(
1N/A LDAP *ld,
1N/A const char *base,
1N/A int scope,
1N/A const char *filter,
1N/A char **attrs,
1N/A int attrsonly,
1N/A LDAPControl **serverctrls,
1N/A LDAPControl **clientctrls,
1N/A struct timeval *localtimeoutp,
1N/A int timelimit, /* -1 means use ld->ld_timelimit */
1N/A int sizelimit, /* -1 means use ld->ld_sizelimit */
1N/A LDAPMessage **res
1N/A)
1N/A{
1N/A int err, msgid;
1N/A
1N/A /*
1N/A * It is an error to pass in a zero'd timeval.
1N/A */
1N/A if ( localtimeoutp != NULL && localtimeoutp->tv_sec == 0 &&
1N/A localtimeoutp->tv_usec == 0 ) {
1N/A if ( ld != NULL ) {
1N/A LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
1N/A }
1N/A if ( res != NULL ) {
1N/A *res = NULL;
1N/A }
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if (( err = nsldapi_search( ld, base, scope, filter, attrs, attrsonly,
1N/A serverctrls, clientctrls, timelimit, sizelimit, &msgid ))
1N/A != LDAP_SUCCESS ) {
1N/A if ( res != NULL ) {
1N/A *res = NULL;
1N/A }
1N/A return( err );
1N/A }
1N/A
1N/A if ( ldap_result( ld, msgid, 1, localtimeoutp, res ) == -1 ) {
1N/A /*
1N/A * Error. ldap_result() sets *res to NULL for us.
1N/A */
1N/A return( LDAP_GET_LDERRNO( ld, NULL, NULL ) );
1N/A }
1N/A
1N/A if ( LDAP_GET_LDERRNO( ld, NULL, NULL ) == LDAP_TIMEOUT ) {
1N/A (void) ldap_abandon( ld, msgid );
1N/A err = LDAP_TIMEOUT;
1N/A LDAP_SET_LDERRNO( ld, err, NULL, NULL );
1N/A if ( res != NULL ) {
1N/A *res = NULL;
1N/A }
1N/A return( err );
1N/A }
1N/A
1N/A return( ldap_result2error( ld, *res, 0 ) );
1N/A}