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;
60 LDAPControl **ctrls = NULL;
62 lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
68 * FIXME: in case of values return filter, we might want
69 * to map attrs and maybe rewrite value
71 if ( !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR ) ) {
75 /* should we check return values? */
76 if ( op->ors_deref != -1 ) {
77 ldap_set_option( lc->lc_ld, LDAP_OPT_DEREF,
78 (void *)&op->ors_deref );
81 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
82 tv.tv_sec = op->ors_tlimit;
89 if ( op->ors_attrs ) {
90 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ )
91 /* just count attrs */ ;
93 attrs = ch_malloc( ( i + 1 )*sizeof( char * ) );
94 if ( attrs == NULL ) {
95 rs->sr_err = LDAP_NO_MEMORY;
100 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ ) {
101 attrs[ i ] = op->ors_attrs[i].an_name.bv_val;
107 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
108 if ( rc != LDAP_SUCCESS ) {
114 rs->sr_err = ldap_search_ext( lc->lc_ld, op->o_req_ndn.bv_val,
115 op->ors_scope, op->ors_filterstr.bv_val,
116 attrs, op->ors_attrsonly, ctrls, NULL,
117 tv.tv_sec ? &tv : NULL,
118 op->ors_slimit, &msgid );
120 if ( rs->sr_err != LDAP_SUCCESS ) {
122 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
125 if ( ldap_back_retry( lc, op, rs, LDAP_BACK_DONTSEND ) ) {
129 rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
130 ldap_back_freeconn( op, lc );
136 /* We pull apart the ber result, stuff it into a slapd entry, and
137 * let send_search_entry stuff it back into ber format. Slow & ugly,
138 * but this is necessary for version matching, and for ACL processing.
141 for ( rc = 0; rc != -1; rc = ldap_result( lc->lc_ld, msgid, 0, &tv, &res ) )
143 /* check for abandon */
144 if ( op->o_abandon ) {
145 ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
153 ldap_pvt_thread_yield();
155 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
162 e = ldap_first_entry( lc->lc_ld, res );
163 rc = ldap_build_entry( op, e, &ent, &bdn,
164 LDAP_BUILD_ENTRY_PRIVATE );
165 if ( rc == LDAP_SUCCESS ) {
167 rs->sr_attrs = op->ors_attrs;
168 rs->sr_operational_attrs = NULL;
170 abort = send_search_entry( op, rs );
171 while ( ent.e_attrs ) {
176 ent.e_attrs = a->a_next;
179 if ( a->a_vals != &slap_dummy_bv ) {
180 ber_bvarray_free( a->a_vals );
182 if ( a->a_nvals != v ) {
183 ber_bvarray_free( a->a_nvals );
188 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) ) {
197 ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
201 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
202 char **references = NULL;
206 rc = ldap_parse_reference( lc->lc_ld, res,
207 &references, &rs->sr_ctrls, 1 );
209 if ( rc != LDAP_SUCCESS ) {
213 if ( references == NULL ) {
217 for ( cnt = 0; references[ cnt ]; cnt++ )
220 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
222 for ( cnt = 0; references[ cnt ]; cnt++ ) {
223 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
226 /* ignore return value by now */
227 ( void )send_search_reference( op, rs );
231 ldap_value_free( references );
232 ch_free( rs->sr_ref );
236 if ( rs->sr_ctrls ) {
237 ldap_controls_free( rs->sr_ctrls );
242 char **references = NULL;
244 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
245 &match.bv_val, (char **)&rs->sr_text,
246 &references, &rs->sr_ctrls, 1 );
247 if ( rc != LDAP_SUCCESS ) {
250 rs->sr_err = slap_map_api2result( rs );
255 for ( cnt = 0; references[ cnt ]; cnt++ )
258 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
260 for ( cnt = 0; references[ cnt ]; cnt++ ) {
261 ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
266 ldap_value_free( references );
278 if ( ldap_back_retry( lc, op, rs, LDAP_BACK_SENDERR ) ) {
282 rs->sr_err = LDAP_SERVER_DOWN;
287 * Rewrite the matched portion of the search base, if required
289 if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
290 rs->sr_matched = match.bv_val;
292 if ( rs->sr_v2ref ) {
293 rs->sr_err = LDAP_REFERRAL;
297 if ( rc != SLAPD_ABANDON ) {
298 send_ldap_result( op, rs );
301 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
303 if ( rs->sr_ctrls ) {
304 ldap_controls_free( rs->sr_ctrls );
308 if ( match.bv_val ) {
309 rs->sr_matched = NULL;
310 LDAP_FREE( match.bv_val );
314 if ( !dontfreetext ) {
315 LDAP_FREE( (char *)rs->sr_text );
321 ber_bvarray_free( rs->sr_ref );
341 BerElement ber = *e->lm_ber;
342 Attribute *attr, **attrp;
345 int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
347 /* safe assumptions ... */
349 BER_BVZERO( &ent->e_bv );
351 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
352 return LDAP_DECODING_ERROR;
356 * Note: this may fail if the target host(s) schema differs
357 * from the one known to the meta, and a DN with unknown
358 * attributes is returned.
360 * FIXME: should we log anything, or delegate to dnNormalize?
362 /* Note: if the distinguished values or the naming attributes
363 * change, should we massage them as well?
365 if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
366 op->o_tmpmemctx ) != LDAP_SUCCESS )
368 return LDAP_INVALID_DN_SYNTAX;
371 attrp = &ent->e_attrs;
373 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
375 slap_syntax_validate_func *validate;
376 slap_syntax_transform_func *pretty;
378 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
379 if ( attr == NULL ) {
385 if ( slap_bv2ad( &a, &attr->a_desc, &text )
388 if ( slap_bv2undef_ad( &a, &attr->a_desc, &text )
391 Debug( LDAP_DEBUG_ANY,
392 "slap_bv2undef_ad(%s): %s\n",
399 /* no subschemaSubentry */
400 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
403 * We eat target's subschemaSubentry because
404 * a search for this value is likely not
405 * to resolve to the appropriate backend;
406 * later, the local subschemaSubentry is
409 ( void )ber_scanf( &ber, "x" /* [W] */ );
415 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
416 || attr->a_vals == NULL )
419 * Note: attr->a_vals can be null when using
420 * values result filter
423 attr->a_vals = (struct berval *)&slap_dummy_bv;
426 attr->a_vals = ch_malloc( sizeof( struct berval ) );
427 BER_BVZERO( &attr->a_vals[ 0 ] );
432 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); last++ )
433 /* just count vals */ ;
436 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
437 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
439 if ( !validate && !pretty ) {
440 attr->a_nvals = NULL;
445 for ( i = 0; i < last; i++ ) {
450 rc = pretty( attr->a_desc->ad_type->sat_syntax,
451 &attr->a_vals[i], &pval, NULL );
454 rc = validate( attr->a_desc->ad_type->sat_syntax,
458 if ( rc != LDAP_SUCCESS ) {
459 /* check if, by chance, it's an undefined objectClass */
460 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
461 oc_bvfind_undef( &attr->a_vals[i] ) != NULL )
463 ber_dupbv( &pval, &attr->a_vals[i] );
466 attr->a_nvals = NULL;
473 LBER_FREE( attr->a_vals[i].bv_val );
474 attr->a_vals[i] = pval;
478 if ( last && attr->a_desc->ad_type->sat_equality &&
479 attr->a_desc->ad_type->sat_equality->smr_normalize )
481 attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
482 for ( i = 0; i < last; i++ ) {
486 * check that each value is valid per syntax
487 * and pretty if appropriate
489 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
490 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
491 attr->a_desc->ad_type->sat_syntax,
492 attr->a_desc->ad_type->sat_equality,
493 &attr->a_vals[i], &attr->a_nvals[i],
496 if ( rc != LDAP_SUCCESS ) {
497 BER_BVZERO( &attr->a_nvals[i] );
502 BER_BVZERO( &attr->a_nvals[i] );
505 attr->a_nvals = attr->a_vals;
508 attrp = &attr->a_next;
516 /* return 0 IFF we can retrieve the entry with ndn
523 AttributeDescription *at,
532 LDAPMessage *result = NULL,
538 LDAPControl **ctrls = NULL;
540 /* Tell getconn this is a privileged op */
541 do_not_cache = op->o_do_not_cache;
542 op->o_do_not_cache = 1;
543 lc = ldap_back_getconn( op, &rs, LDAP_BACK_DONTSEND );
544 if ( !lc || !ldap_back_dobind( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
545 op->o_do_not_cache = do_not_cache;
548 op->o_do_not_cache = do_not_cache;
551 if ( oc && at != slap_schema.si_ad_objectClass ) {
552 gattr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
553 gattr[1] = at->ad_cname.bv_val;
557 gattr[0] = at->ad_cname.bv_val;
565 filter = ch_malloc( STRLENOF( "(objectclass=)" )
566 + oc->soc_cname.bv_len + 1 );
567 ptr = lutil_strcopy( filter, "(objectclass=" );
568 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
574 rc = ldap_back_proxy_authz_ctrl( lc, op, &rs, &ctrls );
575 if ( rc != LDAP_SUCCESS ) {
580 rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
581 at ? gattr : NULL, 0, ctrls, NULL,
582 LDAP_NO_LIMIT, LDAP_NO_LIMIT, &result );
583 if ( rc != LDAP_SUCCESS ) {
584 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
586 if ( ldap_back_retry( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
593 e = ldap_first_entry( lc->lc_ld, result );
598 *ent = ch_calloc( 1, sizeof( Entry ) );
600 rc = ldap_build_entry( op, e, *ent, &bdn, 0 );
602 if ( rc != LDAP_SUCCESS ) {
608 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
611 ldap_msgfree( result );