1 /* search.c - ldap backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2008 The OpenLDAP Foundation.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include <ac/socket.h>
29 #include <ac/string.h>
33 #include "back-ldap.h"
34 #undef ldap_debug /* silence a warning in ldap-int.h */
35 #include "../../../libraries/libldap/ldap-int.h"
40 ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
44 * Quick'n'dirty rewrite of filter in case of error, to deal with
45 * <draft-zeilenga-ldap-t-f>.
48 ldap_back_munge_filter(
50 struct berval *filter )
52 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
57 Debug( LDAP_DEBUG_ARGS, "=> ldap_back_munge_filter \"%s\"\n",
58 filter->bv_val, 0, 0 );
60 for ( ptr = strstr( filter->bv_val, "(?=" );
62 ptr = strstr( ptr, "(?=" ) )
65 bv_true = BER_BVC( "(?=true)" ),
66 bv_false = BER_BVC( "(?=false)" ),
67 bv_undefined = BER_BVC( "(?=undefined)" ),
68 bv_t = BER_BVC( "(&)" ),
69 bv_f = BER_BVC( "(|)" ),
70 bv_T = BER_BVC( "(objectClass=*)" ),
71 bv_F = BER_BVC( "(!(objectClass=*))" );
72 struct berval *oldbv = NULL,
74 oldfilter = BER_BVNULL;
76 if ( strncmp( ptr, bv_true.bv_val, bv_true.bv_len ) == 0 ) {
78 if ( LDAP_BACK_T_F( li ) ) {
85 } else if ( strncmp( ptr, bv_false.bv_val, bv_false.bv_len ) == 0 )
88 if ( LDAP_BACK_T_F( li ) ) {
95 } else if ( strncmp( ptr, bv_undefined.bv_val, bv_undefined.bv_len ) == 0 )
97 oldbv = &bv_undefined;
105 /* if undef or invalid filter is not allowed,
106 * don't rewrite filter */
107 if ( LDAP_BACK_NOUNDEFFILTER( li ) ) {
112 filter->bv_len += newbv->bv_len - oldbv->bv_len;
113 if ( filter->bv_val == op->ors_filterstr.bv_val ) {
114 filter->bv_val = op->o_tmpalloc( filter->bv_len + 1,
117 AC_MEMCPY( filter->bv_val, op->ors_filterstr.bv_val,
118 op->ors_filterstr.bv_len + 1 );
121 filter->bv_val = op->o_tmprealloc( filter->bv_val,
122 filter->bv_len + 1, op->o_tmpmemctx );
125 ptr = filter->bv_val + ( ptr - oldfilter.bv_val );
127 AC_MEMCPY( &ptr[ newbv->bv_len ],
128 &ptr[ oldbv->bv_len ],
129 oldfilter.bv_len - ( ptr - filter->bv_val ) - oldbv->bv_len + 1 );
130 AC_MEMCPY( ptr, newbv->bv_val, newbv->bv_len );
132 ptr += newbv->bv_len;
137 Debug( LDAP_DEBUG_ARGS, "<= ldap_back_munge_filter \"%s\" (%d)\n",
138 filter->bv_val, gotit, 0 );
148 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
150 ldapconn_t *lc = NULL;
152 time_t stoptime = (time_t)(-1);
157 struct berval match = BER_BVNULL,
163 int do_retry = 1, dont_retry = 0;
164 LDAPControl **ctrls = NULL;
165 char **references = NULL;
167 /* FIXME: shouldn't this be null? */
168 const char *save_matched = rs->sr_matched;
170 if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
175 * FIXME: in case of values return filter, we might want
176 * to map attrs and maybe rewrite value
179 /* should we check return values? */
180 if ( op->ors_deref != -1 ) {
181 ldap_set_option( lc->lc_ld, LDAP_OPT_DEREF,
182 (void *)&op->ors_deref );
185 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
186 tv.tv_sec = op->ors_tlimit;
188 stoptime = op->o_time + op->ors_tlimit;
191 LDAP_BACK_TV_SET( &tv );
194 if ( op->ors_attrs ) {
195 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ )
196 /* just count attrs */ ;
198 attrs = ch_malloc( ( i + 1 )*sizeof( char * ) );
199 if ( attrs == NULL ) {
200 rs->sr_err = LDAP_NO_MEMORY;
205 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ ) {
206 attrs[ i ] = op->ors_attrs[i].an_name.bv_val;
212 rc = ldap_back_controls_add( op, rs, lc, &ctrls );
213 if ( rc != LDAP_SUCCESS ) {
217 /* deal with <draft-zeilenga-ldap-t-f> filters */
218 filter = op->ors_filterstr;
220 rs->sr_err = ldap_search_ext( lc->lc_ld, op->o_req_dn.bv_val,
221 op->ors_scope, filter.bv_val,
222 attrs, op->ors_attrsonly, ctrls, NULL,
223 tv.tv_sec ? &tv : NULL,
224 op->ors_slimit, &msgid );
226 if ( rs->sr_err != LDAP_SUCCESS ) {
227 switch ( rs->sr_err ) {
228 case LDAP_SERVER_DOWN:
231 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_DONTSEND ) ) {
237 /* reset by ldap_back_retry ... */
238 rs->sr_err = slap_map_api2result( rs );
241 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_DONTSEND );
246 case LDAP_FILTER_ERROR:
247 switch (ldap_back_munge_filter( op, &filter ) ) {
250 /* invalid filters return success with no data */
251 rs->sr_err = LDAP_SUCCESS;
262 rs->sr_err = slap_map_api2result( rs );
268 /* if needed, initialize timeout */
269 if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
270 if ( tv.tv_sec == 0 || tv.tv_sec > li->li_timeout[ SLAP_OP_SEARCH ] ) {
271 tv.tv_sec = li->li_timeout[ SLAP_OP_SEARCH ];
276 /* We pull apart the ber result, stuff it into a slapd entry, and
277 * let send_search_entry stuff it back into ber format. Slow & ugly,
278 * but this is necessary for version matching, and for ACL processing.
281 for ( rc = -2; rc != -1; rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ONE, &tv, &res ) )
283 /* check for abandon */
284 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( lc ) ) {
288 (void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
293 if ( rc == 0 || rc == -2 ) {
294 ldap_pvt_thread_yield();
297 if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
299 (void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
300 rs->sr_text = "Operation timed out";
301 rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
302 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
307 LDAP_BACK_TV_SET( &tv );
310 /* check time limit */
311 if ( op->ors_tlimit != SLAP_NO_LIMIT
312 && slap_get_time() > stoptime )
314 (void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
315 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
321 /* only touch when activity actually took place... */
322 if ( li->li_idle_timeout && lc ) {
323 lc->lc_time = op->o_time;
326 /* don't retry any more */
331 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
333 struct berval bdn = BER_BVNULL;
337 e = ldap_first_entry( lc->lc_ld, res );
338 rc = ldap_build_entry( op, e, &ent, &bdn );
339 if ( rc == LDAP_SUCCESS ) {
340 ldap_get_entry_controls( lc->lc_ld, res, &rs->sr_ctrls );
342 rs->sr_attrs = op->ors_attrs;
343 rs->sr_operational_attrs = NULL;
345 rs->sr_err = LDAP_SUCCESS;
346 rc = rs->sr_err = send_search_entry( op, rs );
347 if ( rs->sr_ctrls ) {
348 ldap_controls_free( rs->sr_ctrls );
352 if ( !BER_BVISNULL( &ent.e_name ) ) {
353 assert( ent.e_name.bv_val != bdn.bv_val );
354 op->o_tmpfree( ent.e_name.bv_val, op->o_tmpmemctx );
355 BER_BVZERO( &ent.e_name );
357 if ( !BER_BVISNULL( &ent.e_nname ) ) {
358 op->o_tmpfree( ent.e_nname.bv_val, op->o_tmpmemctx );
359 BER_BVZERO( &ent.e_nname );
364 if ( rc != LDAP_SUCCESS ) {
365 if ( rc == LDAP_UNAVAILABLE ) {
366 rc = rs->sr_err = LDAP_OTHER;
368 (void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
373 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
374 if ( LDAP_BACK_NOREFS( li ) ) {
380 rc = ldap_parse_reference( lc->lc_ld, res,
381 &references, &rs->sr_ctrls, 1 );
383 if ( rc != LDAP_SUCCESS ) {
387 /* FIXME: there MUST be at least one */
388 if ( references && references[ 0 ] && references[ 0 ][ 0 ] ) {
391 for ( cnt = 0; references[ cnt ]; cnt++ )
394 /* FIXME: there MUST be at least one */
395 rs->sr_ref = op->o_tmpalloc( ( cnt + 1 ) * sizeof( struct berval ),
398 for ( cnt = 0; references[ cnt ]; cnt++ ) {
399 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
401 BER_BVZERO( &rs->sr_ref[ cnt ] );
403 /* ignore return value by now */
405 ( void )send_search_reference( op, rs );
408 Debug( LDAP_DEBUG_ANY,
409 "%s ldap_back_search: "
410 "got SEARCH_REFERENCE "
411 "with no referrals\n",
412 op->o_log_prefix, 0, 0 );
417 ber_memvfree( (void **)references );
418 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
423 if ( rs->sr_ctrls ) {
424 ldap_controls_free( rs->sr_ctrls );
431 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
433 &references, &rs->sr_ctrls, 1 );
434 if ( rc != LDAP_SUCCESS ) {
437 rs->sr_err = slap_map_api2result( rs );
443 /* RFC 4511: referrals can only appear
444 * if result code is LDAP_REFERRAL */
447 && references[ 0 ][ 0 ] )
449 if ( rs->sr_err != LDAP_REFERRAL ) {
450 Debug( LDAP_DEBUG_ANY,
451 "%s ldap_back_search: "
452 "got referrals with err=%d\n",
459 for ( cnt = 0; references[ cnt ]; cnt++ )
462 rs->sr_ref = op->o_tmpalloc( ( cnt + 1 ) * sizeof( struct berval ),
465 for ( cnt = 0; references[ cnt ]; cnt++ ) {
467 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
469 BER_BVZERO( &rs->sr_ref[ cnt ] );
472 } else if ( rs->sr_err == LDAP_REFERRAL ) {
473 Debug( LDAP_DEBUG_ANY,
474 "%s ldap_back_search: "
475 "got err=%d with null "
476 "or empty referrals\n",
480 rs->sr_err = LDAP_NO_SUCH_OBJECT;
483 if ( match.bv_val != NULL ) {
484 match.bv_len = strlen( match.bv_val );
491 /* if needed, restore timeout */
492 if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
493 if ( tv.tv_sec == 0 || tv.tv_sec > li->li_timeout[ SLAP_OP_SEARCH ] ) {
494 tv.tv_sec = li->li_timeout[ SLAP_OP_SEARCH ];
500 if ( rc == -1 && dont_retry == 0 ) {
503 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_DONTSEND ) ) {
507 rs->sr_err = LDAP_SERVER_DOWN;
508 rs->sr_err = slap_map_api2result( rs );
513 * Rewrite the matched portion of the search base, if required
515 if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
516 struct berval pmatch;
518 if ( dnPretty( NULL, &match, &pmatch, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
519 rs->sr_matched = pmatch.bv_val;
520 LDAP_FREE( match.bv_val );
523 rs->sr_matched = match.bv_val;
527 if ( rs->sr_v2ref ) {
528 rs->sr_err = LDAP_REFERRAL;
532 if ( LDAP_BACK_QUARANTINE( li ) ) {
533 ldap_back_quarantine( op, rs );
537 /* let send_ldap_result play cleanup handlers (ITS#4645) */
538 if ( rc != SLAPD_ABANDON )
541 send_ldap_result( op, rs );
544 (void)ldap_back_controls_free( op, rs, &ctrls );
546 if ( rs->sr_ctrls ) {
547 ldap_controls_free( rs->sr_ctrls );
551 if ( rs->sr_matched != NULL && rs->sr_matched != save_matched ) {
552 if ( rs->sr_matched != match.bv_val ) {
553 ber_memfree_x( (char *)rs->sr_matched, op->o_tmpmemctx );
556 LDAP_FREE( match.bv_val );
558 rs->sr_matched = save_matched;
562 op->o_tmpfree( filter.bv_val, op->o_tmpmemctx );
567 LDAP_FREE( (char *)rs->sr_text );
573 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
578 ber_memvfree( (void **)references );
586 ldap_back_release_conn( li, lc );
600 BerElement ber = *e->lm_ber;
601 Attribute *attr, **attrp;
607 /* safe assumptions ... */
608 assert( ent != NULL );
609 BER_BVZERO( &ent->e_bv );
611 if ( ber_scanf( &ber, "{m", bdn ) == LBER_ERROR ) {
612 return LDAP_DECODING_ERROR;
616 * Note: this may fail if the target host(s) schema differs
617 * from the one known to the meta, and a DN with unknown
618 * attributes is returned.
620 * FIXME: should we log anything, or delegate to dnNormalize?
622 /* Note: if the distinguished values or the naming attributes
623 * change, should we massage them as well?
625 if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
626 op->o_tmpmemctx ) != LDAP_SUCCESS )
628 return LDAP_INVALID_DN_SYNTAX;
632 if ( ber_first_element( &ber, &len, &lastb ) != LBER_SEQUENCE ) {
636 attrp = &ent->e_attrs;
637 while ( ber_next_element( &ber, &len, lastb ) == LBER_SEQUENCE &&
638 ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
640 slap_syntax_validate_func *validate;
641 slap_syntax_transform_func *pretty;
643 attr = attr_alloc( NULL );
644 if ( attr == NULL ) {
647 if ( slap_bv2ad( &a, &attr->a_desc, &text )
650 if ( slap_bv2undef_ad( &a, &attr->a_desc, &text,
651 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
653 Debug( LDAP_DEBUG_ANY,
654 "%s ldap_build_entry: "
655 "slap_bv2undef_ad(%s): %s\n",
656 op->o_log_prefix, a.bv_val, text );
662 /* no subschemaSubentry */
663 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
664 || attr->a_desc == slap_schema.si_ad_entryDN )
668 * We eat target's subschemaSubentry because
669 * a search for this value is likely not
670 * to resolve to the appropriate backend;
671 * later, the local subschemaSubentry is
674 * We also eat entryDN because the frontend
675 * will reattach it without checking if already
678 ( void )ber_scanf( &ber, "x" /* [W] */ );
684 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
685 || attr->a_vals == NULL )
688 * Note: attr->a_vals can be null when using
689 * values result filter
691 attr->a_vals = (struct berval *)&slap_dummy_bv;
694 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
695 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
697 if ( !validate && !pretty ) {
698 attr->a_nvals = NULL;
703 for ( i = 0; !BER_BVISNULL( &attr->a_vals[i] ); i++ ) {
708 rc = pretty( attr->a_desc->ad_type->sat_syntax,
709 &attr->a_vals[i], &pval, NULL );
712 rc = validate( attr->a_desc->ad_type->sat_syntax,
716 if ( rc != LDAP_SUCCESS ) {
717 /* check if, by chance, it's an undefined objectClass */
718 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
719 oc_bvfind_undef( &attr->a_vals[i] ) != NULL )
721 ber_dupbv( &pval, &attr->a_vals[i] );
724 attr->a_nvals = NULL;
731 LBER_FREE( attr->a_vals[i].bv_val );
732 attr->a_vals[i] = pval;
735 attr->a_numvals = last = i;
737 if ( last && attr->a_desc->ad_type->sat_equality &&
738 attr->a_desc->ad_type->sat_equality->smr_normalize )
740 attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
741 for ( i = 0; i < last; i++ ) {
745 * check that each value is valid per syntax
746 * and pretty if appropriate
748 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
749 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
750 attr->a_desc->ad_type->sat_syntax,
751 attr->a_desc->ad_type->sat_equality,
752 &attr->a_vals[i], &attr->a_nvals[i],
755 if ( rc != LDAP_SUCCESS ) {
756 BER_BVZERO( &attr->a_nvals[i] );
761 BER_BVZERO( &attr->a_nvals[i] );
764 attr->a_nvals = attr->a_vals;
767 attrp = &attr->a_next;
775 /* return 0 IFF we can retrieve the entry with ndn
782 AttributeDescription *at,
786 ldapinfo_t *li = (ldapinfo_t *) op->o_bd->be_private;
788 ldapconn_t *lc = NULL;
793 LDAPMessage *result = NULL,
795 char *attr[3], **attrp = NULL;
799 LDAPControl **ctrls = NULL;
803 /* Tell getconn this is a privileged op */
804 do_not_cache = op->o_do_not_cache;
807 op->o_do_not_cache = 1;
808 /* ldap_back_entry_get() is an entry lookup, so it does not need
809 * to know what the entry is being looked up for */
810 op->o_tag = LDAP_REQ_SEARCH;
811 rc = ldap_back_dobind( &lc, op, &rs, LDAP_BACK_DONTSEND );
812 op->o_do_not_cache = do_not_cache;
820 if ( oc && at != slap_schema.si_ad_objectClass ) {
821 attr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
822 attr[1] = at->ad_cname.bv_val;
826 attr[0] = at->ad_cname.bv_val;
834 filter = op->o_tmpalloc( STRLENOF( "(objectClass=" ")" )
835 + oc->soc_cname.bv_len + 1, op->o_tmpmemctx );
836 ptr = lutil_strcopy( filter, "(objectClass=" );
837 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
844 rc = ldap_back_controls_add( op, &rs, lc, &ctrls );
845 if ( rc != LDAP_SUCCESS ) {
850 rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
851 attrp, 0, ctrls, NULL,
852 NULL, LDAP_NO_LIMIT, &result );
853 if ( rc != LDAP_SUCCESS ) {
854 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
856 if ( ldap_back_retry( &lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
857 /* if the identity changed, there might be need to re-authz */
858 (void)ldap_back_controls_free( op, &rs, &ctrls );
865 e = ldap_first_entry( lc->lc_ld, result );
867 /* the entry exists, but it doesn't match the filter? */
871 *ent = entry_alloc();
872 if ( *ent == NULL ) {
877 rc = ldap_build_entry( op, e, *ent, &bdn );
879 if ( rc != LDAP_SUCCESS ) {
885 (void)ldap_back_controls_free( op, &rs, &ctrls );
888 ldap_msgfree( result );
892 op->o_tmpfree( filter, op->o_tmpmemctx );
896 ldap_back_release_conn( li, lc );