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
44 static struct berval dummy = BER_BVNULL;
51 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
56 struct berval match = BER_BVNULL;
57 char **mapped_attrs = NULL;
59 struct berval mfilter = BER_BVNULL;
63 LDAPControl **ctrls = NULL;
65 lc = ldap_back_getconn(op, rs);
71 * FIXME: in case of values return filter, we might want
72 * to map attrs and maybe rewrite value
74 if ( !ldap_back_dobind( lc, op, rs ) ) {
78 /* should we check return values? */
79 if ( op->ors_deref != -1 ) {
80 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->ors_deref );
83 if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
84 tv.tv_sec = op->ors_tlimit;
92 * Rewrite the search base, if required
94 dc.rwmap = &li->rwmap;
98 dc.ctx = "searchBase";
103 if ( ldap_back_dn_massage( &dc, &op->o_req_ndn, &mbase ) ) {
104 send_ldap_result( op, rs );
108 rc = ldap_back_filter_map_rewrite( &dc, op->ors_filter,
109 &mfilter, BACKLDAP_MAP );
115 case LDAP_COMPARE_FALSE:
116 rs->sr_err = LDAP_SUCCESS;
122 rs->sr_err = LDAP_OTHER;
123 rs->sr_text = "Rewrite error";
129 rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
131 BACKLDAP_MAP, &mapped_attrs );
132 if ( rs->sr_err != LDAP_SUCCESS ) {
138 #ifdef LDAP_BACK_PROXY_AUTHZ
139 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
140 if ( rc != LDAP_SUCCESS ) {
144 #endif /* LDAP_BACK_PROXY_AUTHZ */
146 rs->sr_err = ldap_search_ext( lc->ld, mbase.bv_val,
147 op->ors_scope, mfilter.bv_val,
148 mapped_attrs, op->ors_attrsonly,
150 tv.tv_sec ? &tv : NULL, op->ors_slimit,
153 if ( rs->sr_err != LDAP_SUCCESS ) {
155 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
157 ldap_back_freeconn( op, lc );
163 /* We pull apart the ber result, stuff it into a slapd entry, and
164 * let send_search_entry stuff it back into ber format. Slow & ugly,
165 * but this is necessary for version matching, and for ACL processing.
168 for ( rc = 0; rc != -1; rc = ldap_result( lc->ld, msgid, 0, &tv, &res ) )
170 /* check for abandon */
171 if ( op->o_abandon ) {
172 ldap_abandon( lc->ld, msgid );
180 ldap_pvt_thread_yield();
182 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
186 e = ldap_first_entry( lc->ld, res );
187 rc = ldap_build_entry( op, e, &ent, &bdn,
188 LDAP_BUILD_ENTRY_PRIVATE );
189 if ( rc == LDAP_SUCCESS ) {
191 rs->sr_attrs = op->ors_attrs;
193 abort = send_search_entry( op, rs );
194 while (ent.e_attrs) {
199 ent.e_attrs = a->a_next;
202 if ( a->a_vals != &dummy ) {
203 ber_bvarray_free(a->a_vals);
205 if ( a->a_nvals != v ) {
206 ber_bvarray_free(a->a_nvals);
211 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
218 ldap_abandon( lc->ld, msgid );
222 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
223 char **references = NULL;
226 rc = ldap_parse_reference( lc->ld, res,
227 &references, &rs->sr_ctrls, 1 );
229 if ( rc != LDAP_SUCCESS ) {
233 if ( references == NULL ) {
237 for ( cnt = 0; references[ cnt ]; cnt++ )
240 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
242 for ( cnt = 0; references[ cnt ]; cnt++ ) {
243 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
246 /* ignore return value by now */
247 ( void )send_search_reference( op, rs );
251 ldap_value_free( references );
252 ch_free( rs->sr_ref );
256 if ( rs->sr_ctrls ) {
257 ldap_controls_free( rs->sr_ctrls );
262 rc = ldap_parse_result( lc->ld, res, &rs->sr_err,
263 &match.bv_val, (char **)&rs->sr_text,
265 if (rc != LDAP_SUCCESS ) {
268 rs->sr_err = slap_map_api2result( rs );
275 /* FIXME: invalidate the connection? */
276 rs->sr_err = LDAP_SERVER_DOWN;
282 * Rewrite the matched portion of the search base, if required
284 if ( match.bv_val && *match.bv_val ) {
287 #ifdef ENABLE_REWRITE
288 dc.ctx = "matchedDN";
293 match.bv_len = strlen( match.bv_val );
294 ldap_back_dn_massage( &dc, &match, &mdn );
295 rs->sr_matched = mdn.bv_val;
297 if ( rs->sr_v2ref ) {
298 rs->sr_err = LDAP_REFERRAL;
302 send_ldap_result( op, rs );
304 #ifdef LDAP_BACK_PROXY_AUTHZ
305 if ( ctrls && ctrls != op->o_ctrls ) {
309 #endif /* LDAP_BACK_PROXY_AUTHZ */
311 if ( match.bv_val ) {
312 if ( rs->sr_matched != match.bv_val ) {
313 free( (char *)rs->sr_matched );
315 rs->sr_matched = NULL;
316 LDAP_FREE( match.bv_val );
319 if ( !dontfreetext ) {
320 LDAP_FREE( (char *)rs->sr_text );
324 if ( mapped_attrs ) {
325 ch_free( mapped_attrs );
327 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
328 ch_free( mfilter.bv_val );
330 if ( mbase.bv_val != op->o_req_ndn.bv_val ) {
331 free( mbase.bv_val );
346 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
347 struct berval a, mapped;
348 BerElement ber = *e->lm_ber;
349 Attribute *attr, **attrp;
353 int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
356 /* safe assumptions ... */
358 ent->e_bv.bv_val = NULL;
360 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
361 return LDAP_DECODING_ERROR;
365 * Rewrite the dn of the result, if needed
367 dc.rwmap = &li->rwmap;
368 #ifdef ENABLE_REWRITE
369 dc.conn = op->o_conn;
371 dc.ctx = "searchResult";
376 if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
381 * Note: this may fail if the target host(s) schema differs
382 * from the one known to the meta, and a DN with unknown
383 * attributes is returned.
385 * FIXME: should we log anything, or delegate to dnNormalize?
387 /* Note: if the distinguished values or the naming attributes
388 * change, should we massage them as well?
390 if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
391 op->o_tmpmemctx ) != LDAP_SUCCESS )
393 return LDAP_INVALID_DN_SYNTAX;
396 attrp = &ent->e_attrs;
398 #ifdef ENABLE_REWRITE
399 dc.ctx = "searchAttrDN";
401 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
403 slap_syntax_validate_func *validate =
404 attr->a_desc->ad_type->sat_syntax->ssyn_validate;
405 slap_syntax_transform_func *pretty =
406 attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
408 ldap_back_map( &li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP );
409 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) )
411 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
417 if ( slap_bv2ad( &mapped, &attr->a_desc, &text ) != LDAP_SUCCESS ) {
418 if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text )
422 LDAP_LOG( BACK_LDAP, DETAIL1,
423 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
424 #else /* !NEW_LOGGING */
425 Debug( LDAP_DEBUG_ANY,
426 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
427 #endif /* !NEW_LOGGING */
433 /* no subschemaSubentry */
434 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
437 * We eat target's subschemaSubentry because
438 * a search for this value is likely not
439 * to resolve to the appropriate backend;
440 * later, the local subschemaSubentry is
443 ( void )ber_scanf( &ber, "x" /* [W] */ );
449 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
450 || attr->a_vals == NULL )
453 * Note: attr->a_vals can be null when using
454 * values result filter
457 attr->a_vals = &dummy;
459 attr->a_vals = ch_malloc( sizeof( struct berval ) );
460 BER_BVZERO( &attr->a_vals[0] );
465 for ( last = 0; !BER_BVISNULL( &attr->a_vals[last] ); last++ );
470 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
471 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
473 for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
474 ldap_back_map( &li->rwmap.rwm_oc, bv, &mapped,
476 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
477 LBER_FREE( bv->bv_val );
481 *bv = attr->a_vals[last];
482 BER_BVZERO( &attr->a_vals[last] );
485 } else if ( mapped.bv_val != bv->bv_val ) {
487 * FIXME: after LBER_FREEing
488 * the value is replaced by
491 LBER_FREE( bv->bv_val );
492 ber_dupbv( bv, &mapped );
497 * It is necessary to try to rewrite attributes with
498 * dn syntax because they might be used in ACLs as
499 * members of groups; since ACLs are applied to the
500 * rewritten stuff, no dn-based subject clause could
501 * be used at the ldap backend side (see
502 * http://www.OpenLDAP.org/faq/data/cache/452.html)
503 * The problem can be overcome by moving the dn-based
504 * ACLs to the target directory server, and letting
505 * everything pass thru the ldap backend.
507 } else if ( attr->a_desc->ad_type->sat_syntax ==
508 slap_schema.si_syn_distinguishedName )
510 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
513 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
514 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
516 if ( !validate && !pretty ) {
517 attr->a_nvals = NULL;
522 for ( i = 0; i < last; i++ ) {
527 rc = pretty( attr->a_desc->ad_type->sat_syntax,
528 &attr->a_vals[i], &pval, NULL );
530 rc = validate( attr->a_desc->ad_type->sat_syntax,
534 if ( rc != LDAP_SUCCESS ) {
535 attr->a_nvals = NULL;
541 LBER_FREE( attr->a_vals[i].bv_val );
542 attr->a_vals[i] = pval;
546 if ( last && attr->a_desc->ad_type->sat_equality &&
547 attr->a_desc->ad_type->sat_equality->smr_normalize )
549 attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
550 for ( i = 0; i < last; i++ ) {
554 * check that each value is valid per syntax
555 * and pretty if appropriate
557 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
558 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
559 attr->a_desc->ad_type->sat_syntax,
560 attr->a_desc->ad_type->sat_equality,
561 &attr->a_vals[i], &attr->a_nvals[i],
562 NULL /* op->o_tmpmemctx */ );
564 if ( rc != LDAP_SUCCESS ) {
565 BER_BVZERO( &attr->a_nvals[i] );
570 BER_BVZERO( &attr->a_nvals[i] );
573 attr->a_nvals = attr->a_vals;
576 attrp = &attr->a_next;
581 /* make sure it's free'able */
582 if ( !private && ent->e_name.bv_val == bdn->bv_val ) {
583 ber_dupbv( &ent->e_name, bdn );
588 /* return 0 IFF we can retrieve the entry with ndn
595 AttributeDescription *at,
600 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
603 struct berval mapped = BER_BVNULL, bdn, mdn;
604 LDAPMessage *result = NULL, *e = NULL;
611 /* Tell getconn this is a privileged op */
612 is_oc = op->o_do_not_cache;
613 op->o_do_not_cache = 1;
614 lc = ldap_back_getconn(op, &rs);
617 if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
618 op->o_do_not_cache = is_oc;
622 op->o_do_not_cache = is_oc;
626 * Rewrite the search base, if required
628 dc.rwmap = &li->rwmap;
629 #ifdef ENABLE_REWRITE
630 dc.conn = op->o_conn;
632 dc.ctx = "searchBase";
637 if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
642 ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
643 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
647 is_oc = ( strcasecmp( "objectclass", mapped.bv_val ) == 0 );
648 if ( oc && !is_oc ) {
649 gattr[0] = "objectclass";
650 gattr[1] = mapped.bv_val;
654 gattr[0] = mapped.bv_val;
661 ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
663 filter = ch_malloc( STRLENOF( "(objectclass=)" ) + mapped.bv_len + 1 );
664 ptr = lutil_strcopy( filter, "(objectclass=" );
665 ptr = lutil_strcopy( ptr, mapped.bv_val );
670 if ( ldap_search_ext_s( lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
671 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
672 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS )
677 e = ldap_first_entry( lc->ld, result );
682 *ent = ch_calloc( 1, sizeof( Entry ) );
684 rc = ldap_build_entry( op, e, *ent, &bdn, 0 );
686 if ( rc != LDAP_SUCCESS ) {
693 ldap_msgfree( result );
700 if ( mdn.bv_val != ndn->bv_val ) {
701 ch_free( mdn.bv_val );