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, 0, &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 send_ldap_result( op, rs );
296 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
298 if ( rs->sr_ctrls ) {
299 ldap_controls_free( rs->sr_ctrls );
303 if ( match.bv_val ) {
304 rs->sr_matched = NULL;
305 LDAP_FREE( match.bv_val );
309 if ( !dontfreetext ) {
310 LDAP_FREE( (char *)rs->sr_text );
316 ber_bvarray_free( rs->sr_ref );
336 BerElement ber = *e->lm_ber;
337 Attribute *attr, **attrp;
340 int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
342 /* safe assumptions ... */
344 BER_BVZERO( &ent->e_bv );
346 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
347 return LDAP_DECODING_ERROR;
351 * Note: this may fail if the target host(s) schema differs
352 * from the one known to the meta, and a DN with unknown
353 * attributes is returned.
355 * FIXME: should we log anything, or delegate to dnNormalize?
357 /* Note: if the distinguished values or the naming attributes
358 * change, should we massage them as well?
360 if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
361 op->o_tmpmemctx ) != LDAP_SUCCESS )
363 return LDAP_INVALID_DN_SYNTAX;
366 attrp = &ent->e_attrs;
368 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
370 slap_syntax_validate_func *validate;
371 slap_syntax_transform_func *pretty;
373 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
374 if ( attr == NULL ) {
380 if ( slap_bv2ad( &a, &attr->a_desc, &text )
383 if ( slap_bv2undef_ad( &a, &attr->a_desc, &text )
386 Debug( LDAP_DEBUG_ANY,
387 "slap_bv2undef_ad(%s): %s\n",
394 /* no subschemaSubentry */
395 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
398 * We eat target's subschemaSubentry because
399 * a search for this value is likely not
400 * to resolve to the appropriate backend;
401 * later, the local subschemaSubentry is
404 ( void )ber_scanf( &ber, "x" /* [W] */ );
410 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
411 || attr->a_vals == NULL )
414 * Note: attr->a_vals can be null when using
415 * values result filter
418 attr->a_vals = (struct berval *)&slap_dummy_bv;
421 attr->a_vals = ch_malloc( sizeof( struct berval ) );
422 BER_BVZERO( &attr->a_vals[ 0 ] );
427 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); last++ )
428 /* just count vals */ ;
431 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
432 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
434 if ( !validate && !pretty ) {
435 attr->a_nvals = NULL;
440 for ( i = 0; i < last; i++ ) {
445 rc = pretty( attr->a_desc->ad_type->sat_syntax,
446 &attr->a_vals[i], &pval, NULL );
449 rc = validate( attr->a_desc->ad_type->sat_syntax,
453 if ( rc != LDAP_SUCCESS ) {
454 /* check if, by chance, it's an undefined objectClass */
455 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
456 oc_bvfind_undef( &attr->a_vals[i] ) != NULL )
458 ber_dupbv( &pval, &attr->a_vals[i] );
461 attr->a_nvals = NULL;
468 LBER_FREE( attr->a_vals[i].bv_val );
469 attr->a_vals[i] = pval;
473 if ( last && attr->a_desc->ad_type->sat_equality &&
474 attr->a_desc->ad_type->sat_equality->smr_normalize )
476 attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
477 for ( i = 0; i < last; i++ ) {
481 * check that each value is valid per syntax
482 * and pretty if appropriate
484 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
485 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
486 attr->a_desc->ad_type->sat_syntax,
487 attr->a_desc->ad_type->sat_equality,
488 &attr->a_vals[i], &attr->a_nvals[i],
491 if ( rc != LDAP_SUCCESS ) {
492 BER_BVZERO( &attr->a_nvals[i] );
497 BER_BVZERO( &attr->a_nvals[i] );
500 attr->a_nvals = attr->a_vals;
503 attrp = &attr->a_next;
511 /* return 0 IFF we can retrieve the entry with ndn
518 AttributeDescription *at,
527 LDAPMessage *result = NULL,
533 LDAPControl **ctrls = NULL;
535 /* Tell getconn this is a privileged op */
536 do_not_cache = op->o_do_not_cache;
537 op->o_do_not_cache = 1;
538 lc = ldap_back_getconn( op, &rs, LDAP_BACK_DONTSEND );
539 if ( !lc || !ldap_back_dobind( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
540 op->o_do_not_cache = do_not_cache;
543 op->o_do_not_cache = do_not_cache;
546 if ( oc && at != slap_schema.si_ad_objectClass ) {
547 gattr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
548 gattr[1] = at->ad_cname.bv_val;
552 gattr[0] = at->ad_cname.bv_val;
560 filter = ch_malloc( STRLENOF( "(objectclass=)" )
561 + oc->soc_cname.bv_len + 1 );
562 ptr = lutil_strcopy( filter, "(objectclass=" );
563 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
569 rc = ldap_back_proxy_authz_ctrl( lc, op, &rs, &ctrls );
570 if ( rc != LDAP_SUCCESS ) {
575 rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
576 at ? gattr : NULL, 0, ctrls, NULL,
577 LDAP_NO_LIMIT, LDAP_NO_LIMIT, &result );
578 if ( rc != LDAP_SUCCESS ) {
579 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
581 if ( ldap_back_retry( lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
588 e = ldap_first_entry( lc->lc_ld, result );
593 *ent = ch_calloc( 1, sizeof( Entry ) );
595 rc = ldap_build_entry( op, e, *ent, &bdn, 0 );
597 if ( rc != LDAP_SUCCESS ) {
603 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
606 ldap_msgfree( result );