1 /* search.c - ldap backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2005 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 struct ldapinfo *li = (struct ldapinfo *) 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_t = BER_BVC( "(&)" ),
68 bv_f = BER_BVC( "(|)" ),
69 bv_T = BER_BVC( "(objectClass=*)" ),
70 bv_F = BER_BVC( "(!(objectClass=*))" );
71 struct berval *oldbv = NULL,
73 oldfilter = BER_BVNULL;
75 if ( strncmp( ptr, bv_true.bv_val, bv_true.bv_len ) == 0 ) {
77 if ( li->flags & LDAP_BACK_F_SUPPORT_T_F ) {
84 } else if ( strncmp( ptr, bv_false.bv_val, bv_false.bv_len ) == 0 )
87 if ( li->flags & LDAP_BACK_F_SUPPORT_T_F ) {
100 if ( !( li->flags & LDAP_BACK_F_SUPPORT_T_F ) ) {
101 filter->bv_len += newbv->bv_len - oldbv->bv_len;
102 if ( filter->bv_val == op->ors_filterstr.bv_val ) {
103 filter->bv_val = op->o_tmpalloc( filter->bv_len + 1,
106 AC_MEMCPY( filter->bv_val, op->ors_filterstr.bv_val,
107 op->ors_filterstr.bv_len + 1 );
110 filter->bv_val = op->o_tmprealloc( filter->bv_val,
111 filter->bv_len + 1, op->o_tmpmemctx );
114 ptr = filter->bv_val + ( ptr - oldfilter.bv_val );
117 AC_MEMCPY( &ptr[ newbv->bv_len ],
118 &ptr[ oldbv->bv_len ],
119 oldfilter.bv_len - ( ptr - filter->bv_val ) - oldbv->bv_len + 1 );
120 AC_MEMCPY( ptr, newbv->bv_val, newbv->bv_len );
122 ptr += newbv->bv_len;
127 Debug( LDAP_DEBUG_ARGS, "<= ldap_back_munge_filter \"%s\" (%d)\n",
128 filter->bv_val, gotit, 0 );
144 struct berval match = BER_BVNULL,
148 int dontfreetext = 0;
150 LDAPControl **ctrls = NULL;
152 lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
158 * FIXME: in case of values return filter, we might want
159 * to map attrs and maybe rewrite value
161 if ( !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR ) ) {
165 /* should we check return values? */
166 if ( op->ors_deref != -1 ) {
167 ldap_set_option( lc->lc_ld, LDAP_OPT_DEREF,
168 (void *)&op->ors_deref );
171 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
172 tv.tv_sec = op->ors_tlimit;
179 if ( op->ors_attrs ) {
180 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ )
181 /* just count attrs */ ;
183 attrs = ch_malloc( ( i + 1 )*sizeof( char * ) );
184 if ( attrs == NULL ) {
185 rs->sr_err = LDAP_NO_MEMORY;
190 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ ) {
191 attrs[ i ] = op->ors_attrs[i].an_name.bv_val;
197 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
198 if ( rc != LDAP_SUCCESS ) {
203 /* deal with <draft-zeilenga-ldap-t-f> filters */
204 filter = op->ors_filterstr;
206 rs->sr_err = ldap_search_ext( lc->lc_ld, op->o_req_ndn.bv_val,
207 op->ors_scope, filter.bv_val,
208 attrs, op->ors_attrsonly, ctrls, NULL,
209 tv.tv_sec ? &tv : NULL,
210 op->ors_slimit, &msgid );
212 if ( rs->sr_err != LDAP_SUCCESS ) {
214 switch ( rs->sr_err ) {
215 case LDAP_SERVER_DOWN:
218 if ( ldap_back_retry( lc, op, rs, LDAP_BACK_DONTSEND ) ) {
222 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
223 ldap_back_freeconn( op, lc );
227 case LDAP_FILTER_ERROR:
228 if ( ldap_back_munge_filter( op, &filter ) ) {
232 /* invalid filters return success with no data */
233 rs->sr_err = LDAP_SUCCESS;
238 rs->sr_err = slap_map_api2result( rs );
244 /* We pull apart the ber result, stuff it into a slapd entry, and
245 * let send_search_entry stuff it back into ber format. Slow & ugly,
246 * but this is necessary for version matching, and for ACL processing.
249 for ( rc = 0; rc != -1; rc = ldap_result( lc->lc_ld, msgid, 0, &tv, &res ) )
251 /* check for abandon */
252 if ( op->o_abandon ) {
253 ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
261 ldap_pvt_thread_yield();
263 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
270 e = ldap_first_entry( lc->lc_ld, res );
271 rc = ldap_build_entry( op, e, &ent, &bdn );
272 if ( rc == LDAP_SUCCESS ) {
274 rs->sr_attrs = op->ors_attrs;
275 rs->sr_operational_attrs = NULL;
277 abort = send_search_entry( op, rs );
278 if ( !BER_BVISNULL( &ent.e_name ) && ( ent.e_name.bv_val != bdn.bv_val ) )
280 free( ent.e_name.bv_val );
281 BER_BVZERO( &ent.e_name );
283 if ( !BER_BVISNULL( &ent.e_nname ) ) {
284 free( ent.e_nname.bv_val );
285 BER_BVZERO( &ent.e_nname );
291 ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
295 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
296 char **references = NULL;
300 rc = ldap_parse_reference( lc->lc_ld, res,
301 &references, &rs->sr_ctrls, 1 );
303 if ( rc != LDAP_SUCCESS ) {
307 if ( references == NULL ) {
311 for ( cnt = 0; references[ cnt ]; cnt++ )
314 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
316 for ( cnt = 0; references[ cnt ]; cnt++ ) {
317 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
320 /* ignore return value by now */
321 ( void )send_search_reference( op, rs );
325 ldap_value_free( references );
326 ch_free( rs->sr_ref );
330 if ( rs->sr_ctrls ) {
331 ldap_controls_free( rs->sr_ctrls );
336 char **references = NULL;
338 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
339 &match.bv_val, (char **)&rs->sr_text,
340 &references, &rs->sr_ctrls, 1 );
341 if ( rc != LDAP_SUCCESS ) {
344 rs->sr_err = slap_map_api2result( rs );
349 for ( cnt = 0; references[ cnt ]; cnt++ )
352 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
354 for ( cnt = 0; references[ cnt ]; cnt++ ) {
355 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
360 ldap_value_free( references );
372 if ( ldap_back_retry( lc, op, rs, LDAP_BACK_SENDERR ) ) {
376 rs->sr_err = LDAP_SERVER_DOWN;
381 * Rewrite the matched portion of the search base, if required
383 if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
384 rs->sr_matched = match.bv_val;
386 if ( rs->sr_v2ref ) {
387 rs->sr_err = LDAP_REFERRAL;
391 if ( rc != SLAPD_ABANDON ) {
392 send_ldap_result( op, rs );
395 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
397 if ( rs->sr_ctrls ) {
398 ldap_controls_free( rs->sr_ctrls );
402 if ( match.bv_val ) {
403 rs->sr_matched = NULL;
404 LDAP_FREE( match.bv_val );
407 if ( !BER_BVISNULL( &filter ) && filter.bv_val != op->ors_filterstr.bv_val ) {
408 op->o_tmpfree( filter.bv_val, op->o_tmpmemctx );
412 if ( !dontfreetext ) {
413 LDAP_FREE( (char *)rs->sr_text );
419 ber_bvarray_free( rs->sr_ref );
438 BerElement ber = *e->lm_ber;
439 Attribute *attr, **attrp;
443 /* safe assumptions ... */
445 BER_BVZERO( &ent->e_bv );
447 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
448 return LDAP_DECODING_ERROR;
452 * Note: this may fail if the target host(s) schema differs
453 * from the one known to the meta, and a DN with unknown
454 * attributes is returned.
456 * FIXME: should we log anything, or delegate to dnNormalize?
458 /* Note: if the distinguished values or the naming attributes
459 * change, should we massage them as well?
461 if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
462 op->o_tmpmemctx ) != LDAP_SUCCESS )
464 return LDAP_INVALID_DN_SYNTAX;
467 attrp = &ent->e_attrs;
469 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
471 slap_syntax_validate_func *validate;
472 slap_syntax_transform_func *pretty;
474 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
475 if ( attr == NULL ) {
481 if ( slap_bv2ad( &a, &attr->a_desc, &text )
484 if ( slap_bv2undef_ad( &a, &attr->a_desc, &text )
487 Debug( LDAP_DEBUG_ANY,
488 "slap_bv2undef_ad(%s): %s\n",
495 /* no subschemaSubentry */
496 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
499 * We eat target's subschemaSubentry because
500 * a search for this value is likely not
501 * to resolve to the appropriate backend;
502 * later, the local subschemaSubentry is
505 ( void )ber_scanf( &ber, "x" /* [W] */ );
511 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
512 || attr->a_vals == NULL )
515 * Note: attr->a_vals can be null when using
516 * values result filter
518 attr->a_vals = (struct berval *)&slap_dummy_bv;
522 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); last++ )
523 /* just count vals */ ;
526 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
527 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
529 if ( !validate && !pretty ) {
530 attr->a_nvals = NULL;
535 for ( i = 0; i < last; i++ ) {
540 rc = pretty( attr->a_desc->ad_type->sat_syntax,
541 &attr->a_vals[i], &pval, NULL );
544 rc = validate( attr->a_desc->ad_type->sat_syntax,
548 if ( rc != LDAP_SUCCESS ) {
549 /* check if, by chance, it's an undefined objectClass */
550 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
551 oc_bvfind_undef( &attr->a_vals[i] ) != NULL )
553 ber_dupbv( &pval, &attr->a_vals[i] );
556 attr->a_nvals = NULL;
563 LBER_FREE( attr->a_vals[i].bv_val );
564 attr->a_vals[i] = pval;
568 if ( last && attr->a_desc->ad_type->sat_equality &&
569 attr->a_desc->ad_type->sat_equality->smr_normalize )
571 attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
572 for ( i = 0; i < last; i++ ) {
576 * check that each value is valid per syntax
577 * and pretty if appropriate
579 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
580 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
581 attr->a_desc->ad_type->sat_syntax,
582 attr->a_desc->ad_type->sat_equality,
583 &attr->a_vals[i], &attr->a_nvals[i],
586 if ( rc != LDAP_SUCCESS ) {
587 BER_BVZERO( &attr->a_nvals[i] );
592 BER_BVZERO( &attr->a_nvals[i] );
595 attr->a_nvals = attr->a_vals;
598 attrp = &attr->a_next;
606 /* return 0 IFF we can retrieve the entry with ndn
613 AttributeDescription *at,
622 LDAPMessage *result = NULL,
628 LDAPControl **ctrls = NULL;
630 /* Tell getconn this is a privileged op */
631 do_not_cache = op->o_do_not_cache;
632 op->o_do_not_cache = 1;
633 lc = ldap_back_getconn( op, &rs, LDAP_BACK_DONTSEND );
634 if ( !lc || !ldap_back_dobind( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
635 op->o_do_not_cache = do_not_cache;
638 op->o_do_not_cache = do_not_cache;
641 if ( oc && at != slap_schema.si_ad_objectClass ) {
642 gattr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
643 gattr[1] = at->ad_cname.bv_val;
647 gattr[0] = at->ad_cname.bv_val;
655 filter = ch_malloc( STRLENOF( "(objectclass=)" )
656 + oc->soc_cname.bv_len + 1 );
657 ptr = lutil_strcopy( filter, "(objectclass=" );
658 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
664 rc = ldap_back_proxy_authz_ctrl( lc, op, &rs, &ctrls );
665 if ( rc != LDAP_SUCCESS ) {
670 rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
671 at ? gattr : NULL, 0, ctrls, NULL,
672 LDAP_NO_LIMIT, LDAP_NO_LIMIT, &result );
673 if ( rc != LDAP_SUCCESS ) {
674 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
676 if ( ldap_back_retry( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
683 e = ldap_first_entry( lc->lc_ld, result );
688 *ent = ch_calloc( 1, sizeof( Entry ) );
690 rc = ldap_build_entry( op, e, *ent, &bdn );
692 if ( rc != LDAP_SUCCESS ) {
698 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
701 ldap_msgfree( result );