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,
41 struct berval *bdn, int flags );
42 #define LDAP_BUILD_ENTRY_PRIVATE 0x01
55 struct berval match = BER_BVNULL;
61 LDAPControl **ctrls = NULL;
63 lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
69 * FIXME: in case of values return filter, we might want
70 * to map attrs and maybe rewrite value
72 if ( !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR ) ) {
76 /* should we check return values? */
77 if ( op->ors_deref != -1 ) {
78 ldap_set_option( lc->lc_ld, LDAP_OPT_DEREF,
79 (void *)&op->ors_deref );
82 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
83 tv.tv_sec = op->ors_tlimit;
90 if ( op->ors_attrs ) {
91 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ )
92 /* just count attrs */ ;
94 attrs = ch_malloc( ( i + 1 )*sizeof( char * ) );
95 if ( attrs == NULL ) {
96 rs->sr_err = LDAP_NO_MEMORY;
101 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ ) {
102 attrs[ i ] = op->ors_attrs[i].an_name.bv_val;
108 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
109 if ( rc != LDAP_SUCCESS ) {
115 rs->sr_err = ldap_search_ext( lc->lc_ld, op->o_req_ndn.bv_val,
116 op->ors_scope, op->ors_filterstr.bv_val,
117 attrs, op->ors_attrsonly, ctrls, NULL,
118 tv.tv_sec ? &tv : NULL,
119 op->ors_slimit, &msgid );
121 if ( rs->sr_err != LDAP_SUCCESS ) {
123 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
125 ldap_back_freeconn( op, lc );
131 /* We pull apart the ber result, stuff it into a slapd entry, and
132 * let send_search_entry stuff it back into ber format. Slow & ugly,
133 * but this is necessary for version matching, and for ACL processing.
136 for ( rc = 0; rc != -1; rc = ldap_result( lc->lc_ld, msgid, 0, &tv, &res ) )
138 /* check for abandon */
139 if ( op->o_abandon ) {
140 ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
148 ldap_pvt_thread_yield();
150 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
157 e = ldap_first_entry( lc->lc_ld, res );
158 rc = ldap_build_entry( op, e, &ent, &bdn,
159 LDAP_BUILD_ENTRY_PRIVATE );
160 if ( rc == LDAP_SUCCESS ) {
162 rs->sr_attrs = op->ors_attrs;
163 rs->sr_operational_attrs = NULL;
165 abort = send_search_entry( op, rs );
166 while ( ent.e_attrs ) {
171 ent.e_attrs = a->a_next;
174 if ( a->a_vals != &slap_dummy_bv ) {
175 ber_bvarray_free( a->a_vals );
177 if ( a->a_nvals != v ) {
178 ber_bvarray_free( a->a_nvals );
183 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) ) {
192 ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
196 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
197 char **references = NULL;
201 rc = ldap_parse_reference( lc->lc_ld, res,
202 &references, &rs->sr_ctrls, 1 );
204 if ( rc != LDAP_SUCCESS ) {
208 if ( references == NULL ) {
212 for ( cnt = 0; references[ cnt ]; cnt++ )
215 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
217 for ( cnt = 0; references[ cnt ]; cnt++ ) {
218 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
221 /* ignore return value by now */
222 ( void )send_search_reference( op, rs );
226 ldap_value_free( references );
227 ch_free( rs->sr_ref );
231 if ( rs->sr_ctrls ) {
232 ldap_controls_free( rs->sr_ctrls );
237 char **references = NULL;
239 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
240 &match.bv_val, (char **)&rs->sr_text,
241 &references, &rs->sr_ctrls, 1 );
242 if ( rc != LDAP_SUCCESS ) {
245 rs->sr_err = slap_map_api2result( rs );
250 for ( cnt = 0; references[ cnt ]; cnt++ )
253 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
255 for ( cnt = 0; references[ cnt ]; cnt++ ) {
256 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
261 ldap_value_free( references );
273 if ( ldap_back_retry( lc, op, rs, LDAP_BACK_SENDERR ) ) {
277 /* FIXME: invalidate the connection? */
278 rs->sr_err = LDAP_SERVER_DOWN;
284 * Rewrite the matched portion of the search base, if required
286 if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
287 rs->sr_matched = match.bv_val;
289 if ( rs->sr_v2ref ) {
290 rs->sr_err = LDAP_REFERRAL;
294 if ( rc != SLAPD_ABANDON ) {
295 send_ldap_result( op, rs );
298 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
300 if ( rs->sr_ctrls ) {
301 ldap_controls_free( rs->sr_ctrls );
305 if ( match.bv_val ) {
306 rs->sr_matched = NULL;
307 LDAP_FREE( match.bv_val );
311 if ( !dontfreetext ) {
312 LDAP_FREE( (char *)rs->sr_text );
318 ber_bvarray_free( rs->sr_ref );
338 BerElement ber = *e->lm_ber;
339 Attribute *attr, **attrp;
342 int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
344 /* safe assumptions ... */
346 BER_BVZERO( &ent->e_bv );
348 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
349 return LDAP_DECODING_ERROR;
353 * Note: this may fail if the target host(s) schema differs
354 * from the one known to the meta, and a DN with unknown
355 * attributes is returned.
357 * FIXME: should we log anything, or delegate to dnNormalize?
359 /* Note: if the distinguished values or the naming attributes
360 * change, should we massage them as well?
362 if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
363 op->o_tmpmemctx ) != LDAP_SUCCESS )
365 return LDAP_INVALID_DN_SYNTAX;
368 attrp = &ent->e_attrs;
370 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
372 slap_syntax_validate_func *validate;
373 slap_syntax_transform_func *pretty;
375 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
376 if ( attr == NULL ) {
382 if ( slap_bv2ad( &a, &attr->a_desc, &text )
385 if ( slap_bv2undef_ad( &a, &attr->a_desc, &text )
388 Debug( LDAP_DEBUG_ANY,
389 "slap_bv2undef_ad(%s): %s\n",
396 /* no subschemaSubentry */
397 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
400 * We eat target's subschemaSubentry because
401 * a search for this value is likely not
402 * to resolve to the appropriate backend;
403 * later, the local subschemaSubentry is
406 ( void )ber_scanf( &ber, "x" /* [W] */ );
412 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
413 || attr->a_vals == NULL )
416 * Note: attr->a_vals can be null when using
417 * values result filter
420 attr->a_vals = (struct berval *)&slap_dummy_bv;
423 attr->a_vals = ch_malloc( sizeof( struct berval ) );
424 BER_BVZERO( &attr->a_vals[ 0 ] );
429 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); last++ )
430 /* just count vals */ ;
433 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
434 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
436 if ( !validate && !pretty ) {
437 attr->a_nvals = NULL;
442 for ( i = 0; i < last; i++ ) {
447 rc = pretty( attr->a_desc->ad_type->sat_syntax,
448 &attr->a_vals[i], &pval, NULL );
451 rc = validate( attr->a_desc->ad_type->sat_syntax,
455 if ( rc != LDAP_SUCCESS ) {
456 /* check if, by chance, it's an undefined objectClass */
457 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
458 oc_bvfind_undef( &attr->a_vals[i] ) != NULL )
460 ber_dupbv( &pval, &attr->a_vals[i] );
463 attr->a_nvals = NULL;
470 LBER_FREE( attr->a_vals[i].bv_val );
471 attr->a_vals[i] = pval;
475 if ( last && attr->a_desc->ad_type->sat_equality &&
476 attr->a_desc->ad_type->sat_equality->smr_normalize )
478 attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
479 for ( i = 0; i < last; i++ ) {
483 * check that each value is valid per syntax
484 * and pretty if appropriate
486 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
487 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
488 attr->a_desc->ad_type->sat_syntax,
489 attr->a_desc->ad_type->sat_equality,
490 &attr->a_vals[i], &attr->a_nvals[i],
493 if ( rc != LDAP_SUCCESS ) {
494 BER_BVZERO( &attr->a_nvals[i] );
499 BER_BVZERO( &attr->a_nvals[i] );
502 attr->a_nvals = attr->a_vals;
505 attrp = &attr->a_next;
513 /* return 0 IFF we can retrieve the entry with ndn
520 AttributeDescription *at,
529 LDAPMessage *result = NULL,
535 LDAPControl **ctrls = NULL;
537 /* Tell getconn this is a privileged op */
538 do_not_cache = op->o_do_not_cache;
539 op->o_do_not_cache = 1;
540 lc = ldap_back_getconn( op, &rs, LDAP_BACK_DONTSEND );
541 if ( !lc || !ldap_back_dobind( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
542 op->o_do_not_cache = do_not_cache;
545 op->o_do_not_cache = do_not_cache;
548 if ( oc && at != slap_schema.si_ad_objectClass ) {
549 gattr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
550 gattr[1] = at->ad_cname.bv_val;
554 gattr[0] = at->ad_cname.bv_val;
562 filter = ch_malloc( STRLENOF( "(objectclass=)" )
563 + oc->soc_cname.bv_len + 1 );
564 ptr = lutil_strcopy( filter, "(objectclass=" );
565 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
571 rc = ldap_back_proxy_authz_ctrl( lc, op, &rs, &ctrls );
572 if ( rc != LDAP_SUCCESS ) {
577 rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
578 at ? gattr : NULL, 0, ctrls, NULL,
579 LDAP_NO_LIMIT, LDAP_NO_LIMIT, &result );
580 if ( rc != LDAP_SUCCESS ) {
581 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
583 if ( ldap_back_retry( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
590 e = ldap_first_entry( lc->lc_ld, result );
595 *ent = ch_calloc( 1, sizeof( Entry ) );
597 rc = ldap_build_entry( op, e, *ent, &bdn, 0 );
599 if ( rc != LDAP_SUCCESS ) {
605 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
608 ldap_msgfree( result );