1 /* search.c - ldap backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1999-2004 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 );
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 ) ) {
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 #ifdef LDAP_BACK_PROXY_AUTHZ
109 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
110 if ( rc != LDAP_SUCCESS ) {
114 #endif /* LDAP_BACK_PROXY_AUTHZ */
117 rs->sr_err = ldap_search_ext( lc->lc_ld, op->o_req_ndn.bv_val,
118 op->ors_scope, op->ors_filterstr.bv_val,
119 attrs, op->ors_attrsonly, ctrls, NULL,
120 tv.tv_sec ? &tv : NULL,
121 op->ors_slimit, &msgid );
123 if ( rs->sr_err != LDAP_SUCCESS ) {
125 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
127 ldap_back_freeconn( op, lc );
133 /* We pull apart the ber result, stuff it into a slapd entry, and
134 * let send_search_entry stuff it back into ber format. Slow & ugly,
135 * but this is necessary for version matching, and for ACL processing.
138 for ( rc = 0; rc != -1; rc = ldap_result( lc->lc_ld, msgid, 0, &tv, &res ) )
140 /* check for abandon */
141 if ( op->o_abandon ) {
142 ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
150 ldap_pvt_thread_yield();
152 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
159 e = ldap_first_entry( lc->lc_ld, res );
160 rc = ldap_build_entry( op, e, &ent, &bdn,
161 LDAP_BUILD_ENTRY_PRIVATE );
162 if ( rc == LDAP_SUCCESS ) {
164 rs->sr_attrs = op->ors_attrs;
165 rs->sr_operational_attrs = NULL;
167 abort = send_search_entry( op, rs );
168 while ( ent.e_attrs ) {
173 ent.e_attrs = a->a_next;
176 if ( a->a_vals != &slap_dummy_bv ) {
177 ber_bvarray_free( a->a_vals );
179 if ( a->a_nvals != v ) {
180 ber_bvarray_free( a->a_nvals );
185 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) ) {
194 ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
198 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
199 char **references = NULL;
203 rc = ldap_parse_reference( lc->lc_ld, res,
204 &references, &rs->sr_ctrls, 1 );
206 if ( rc != LDAP_SUCCESS ) {
210 if ( references == NULL ) {
214 for ( cnt = 0; references[ cnt ]; cnt++ )
217 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
219 for ( cnt = 0; references[ cnt ]; cnt++ ) {
220 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
223 /* ignore return value by now */
224 ( void )send_search_reference( op, rs );
228 ldap_value_free( references );
229 ch_free( rs->sr_ref );
233 if ( rs->sr_ctrls ) {
234 ldap_controls_free( rs->sr_ctrls );
239 rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
240 &match.bv_val, (char **)&rs->sr_text,
241 NULL, &rs->sr_ctrls, 1 );
242 if (rc != LDAP_SUCCESS ) {
245 rs->sr_err = slap_map_api2result( rs );
254 if ( ldap_back_retry( lc, op, rs ) ) {
258 /* FIXME: invalidate the connection? */
259 rs->sr_err = LDAP_SERVER_DOWN;
265 * Rewrite the matched portion of the search base, if required
267 if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
268 rs->sr_matched = match.bv_val;
270 if ( rs->sr_v2ref ) {
271 rs->sr_err = LDAP_REFERRAL;
275 send_ldap_result( op, rs );
277 #ifdef LDAP_BACK_PROXY_AUTHZ
278 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
279 #endif /* LDAP_BACK_PROXY_AUTHZ */
281 if ( rs->sr_ctrls ) {
282 ldap_controls_free( rs->sr_ctrls );
286 if ( match.bv_val ) {
287 rs->sr_matched = NULL;
288 LDAP_FREE( match.bv_val );
291 if ( !dontfreetext ) {
292 LDAP_FREE( (char *)rs->sr_text );
312 BerElement ber = *e->lm_ber;
313 Attribute *attr, **attrp;
316 int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
318 /* safe assumptions ... */
320 BER_BVZERO( &ent->e_bv );
322 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
323 return LDAP_DECODING_ERROR;
327 * Note: this may fail if the target host(s) schema differs
328 * from the one known to the meta, and a DN with unknown
329 * attributes is returned.
331 * FIXME: should we log anything, or delegate to dnNormalize?
333 /* Note: if the distinguished values or the naming attributes
334 * change, should we massage them as well?
336 if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
337 op->o_tmpmemctx ) != LDAP_SUCCESS )
339 return LDAP_INVALID_DN_SYNTAX;
342 attrp = &ent->e_attrs;
344 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
346 slap_syntax_validate_func *validate;
347 slap_syntax_transform_func *pretty;
349 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
350 if ( attr == NULL ) {
356 if ( slap_bv2ad( &a, &attr->a_desc, &text )
359 if ( slap_bv2undef_ad( &a, &attr->a_desc, &text )
362 Debug( LDAP_DEBUG_ANY,
363 "slap_bv2undef_ad(%s): %s\n",
370 /* no subschemaSubentry */
371 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
374 * We eat target's subschemaSubentry because
375 * a search for this value is likely not
376 * to resolve to the appropriate backend;
377 * later, the local subschemaSubentry is
380 ( void )ber_scanf( &ber, "x" /* [W] */ );
386 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
387 || attr->a_vals == NULL )
390 * Note: attr->a_vals can be null when using
391 * values result filter
394 attr->a_vals = (struct berval *)&slap_dummy_bv;
397 attr->a_vals = ch_malloc( sizeof( struct berval ) );
398 BER_BVZERO( &attr->a_vals[ 0 ] );
403 for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); last++ )
404 /* just count vals */ ;
407 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
408 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
410 if ( !validate && !pretty ) {
411 attr->a_nvals = NULL;
416 for ( i = 0; i < last; i++ ) {
421 rc = pretty( attr->a_desc->ad_type->sat_syntax,
422 &attr->a_vals[i], &pval, NULL );
425 rc = validate( attr->a_desc->ad_type->sat_syntax,
429 if ( rc != LDAP_SUCCESS ) {
430 /* check if, by chance, it's an undefined objectClass */
431 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
432 oc_bvfind_undef( &attr->a_vals[i] ) != NULL )
434 ber_dupbv( &pval, &attr->a_vals[i] );
437 attr->a_nvals = NULL;
444 LBER_FREE( attr->a_vals[i].bv_val );
445 attr->a_vals[i] = pval;
449 if ( last && attr->a_desc->ad_type->sat_equality &&
450 attr->a_desc->ad_type->sat_equality->smr_normalize )
452 attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
453 for ( i = 0; i < last; i++ ) {
457 * check that each value is valid per syntax
458 * and pretty if appropriate
460 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
461 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
462 attr->a_desc->ad_type->sat_syntax,
463 attr->a_desc->ad_type->sat_equality,
464 &attr->a_vals[i], &attr->a_nvals[i],
467 if ( rc != LDAP_SUCCESS ) {
468 BER_BVZERO( &attr->a_nvals[i] );
473 BER_BVZERO( &attr->a_nvals[i] );
476 attr->a_nvals = attr->a_vals;
479 attrp = &attr->a_next;
487 /* return 0 IFF we can retrieve the entry with ndn
494 AttributeDescription *at,
504 LDAPMessage *result = NULL,
512 /* Tell getconn this is a privileged op */
513 do_not_cache = op->o_do_not_cache;
514 op->o_do_not_cache = 1;
515 lc = ldap_back_getconn( op, &rs );
518 if ( !lc || !ldap_back_dobind( lc, op, &rs ) ) {
519 op->o_do_not_cache = do_not_cache;
523 op->o_do_not_cache = do_not_cache;
527 is_oc = ( strcasecmp( "objectclass", at->ad_cname.bv_val ) == 0 );
528 if ( oc && !is_oc ) {
529 gattr[0] = "objectclass";
530 gattr[1] = at->ad_cname.bv_val;
534 gattr[0] = at->ad_cname.bv_val;
542 filter = ch_malloc( STRLENOF( "(objectclass=)" )
543 + oc->soc_cname.bv_len + 1 );
544 ptr = lutil_strcopy( filter, "(objectclass=" );
545 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
551 rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
552 at ? gattr : NULL, 0, NULL, NULL, LDAP_NO_LIMIT,
553 LDAP_NO_LIMIT, &result );
554 if ( rc != LDAP_SUCCESS ) {
555 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
557 if ( ldap_back_retry( lc, op, &rs ) ) {
564 e = ldap_first_entry( lc->lc_ld, result );
569 *ent = ch_calloc( 1, sizeof( Entry ) );
571 rc = ldap_build_entry( op, e, *ent, &bdn, 0 );
573 if ( rc != LDAP_SUCCESS ) {
580 ldap_msgfree( result );