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
43 #define LDAP_BUILD_ENTRY_NORMALIZE 0x02
45 static struct berval dummy = { 0, NULL };
52 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
57 struct berval match = { 0, NULL };
58 char **mapped_attrs = NULL;
60 struct berval mfilter = { 0, NULL };
61 struct slap_limits_set *limit = NULL;
65 #ifdef LDAP_BACK_PROXY_AUTHZ
66 LDAPControl **ctrls = NULL;
67 #endif /* LDAP_BACK_PROXY_AUTHZ */
69 lc = ldap_back_getconn(op, rs);
75 * FIXME: in case of values return filter, we might want
76 * to map attrs and maybe rewrite value
78 if ( !ldap_back_dobind( lc, op, rs ) ) {
82 /* if not root, get appropriate limits */
83 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
86 ( void ) get_limits( op, &op->o_ndn, &limit );
89 /* if no time limit requested, rely on remote server limits */
90 /* if requested limit higher than hard limit, abort */
91 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
92 /* no hard limit means use soft instead */
93 if ( limit->lms_t_hard == 0
94 && limit->lms_t_soft > -1
95 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
96 op->oq_search.rs_tlimit = limit->lms_t_soft;
98 /* positive hard limit means abort */
99 } else if ( limit->lms_t_hard > 0 ) {
100 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
105 /* negative hard limit means no limit */
108 /* if no size limit requested, rely on remote server limits */
109 /* if requested limit higher than hard limit, abort */
110 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
111 /* no hard limit means use soft instead */
112 if ( limit->lms_s_hard == 0
113 && limit->lms_s_soft > -1
114 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
115 op->oq_search.rs_slimit = limit->lms_s_soft;
117 /* positive hard limit means abort */
118 } else if ( limit->lms_s_hard > 0 ) {
119 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
124 /* negative hard limit means no limit */
127 /* should we check return values? */
128 if (op->oq_search.rs_deref != -1)
129 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->oq_search.rs_deref);
130 if (op->oq_search.rs_tlimit != -1) {
131 tv.tv_sec = op->oq_search.rs_tlimit;
138 * Rewrite the search base, if required
140 dc.rwmap = &li->rwmap;
141 #ifdef ENABLE_REWRITE
142 dc.conn = op->o_conn;
144 dc.ctx = "searchBase";
149 if ( ldap_back_dn_massage( &dc, &op->o_req_ndn, &mbase ) ) {
150 send_ldap_result( op, rs );
154 rc = ldap_back_filter_map_rewrite( &dc, op->oq_search.rs_filter,
155 &mfilter, BACKLDAP_MAP );
161 case LDAP_COMPARE_FALSE:
162 rs->sr_err = LDAP_SUCCESS;
168 rs->sr_err = LDAP_OTHER;
169 rs->sr_text = "Rewrite error";
175 rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
176 op->oq_search.rs_attrs,
177 BACKLDAP_MAP, &mapped_attrs );
183 #ifdef LDAP_BACK_PROXY_AUTHZ
184 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
185 if ( rc != LDAP_SUCCESS ) {
189 #endif /* LDAP_BACK_PROXY_AUTHZ */
191 rs->sr_err = ldap_search_ext(lc->ld, mbase.bv_val,
192 op->oq_search.rs_scope, mfilter.bv_val,
193 mapped_attrs, op->oq_search.rs_attrsonly,
194 #ifdef LDAP_BACK_PROXY_AUTHZ
196 #else /* ! LDAP_BACK_PROXY_AUTHZ */
198 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
200 tv.tv_sec ? &tv : NULL, op->oq_search.rs_slimit,
203 if ( rs->sr_err != LDAP_SUCCESS ) {
205 rc = ldap_back_op_result(lc, op, rs, msgid, 0);
209 /* We pull apart the ber result, stuff it into a slapd entry, and
210 * let send_search_entry stuff it back into ber format. Slow & ugly,
211 * but this is necessary for version matching, and for ACL processing.
214 for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
216 /* check for abandon */
218 ldap_abandon(lc->ld, msgid);
226 ldap_pvt_thread_yield();
228 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
232 e = ldap_first_entry(lc->ld,res);
233 if ( ( rc = ldap_build_entry(op, e, &ent, &bdn,
234 LDAP_BUILD_ENTRY_PRIVATE)) == LDAP_SUCCESS ) {
236 rs->sr_attrs = op->oq_search.rs_attrs;
237 abort = send_search_entry( op, rs );
238 while (ent.e_attrs) {
243 ent.e_attrs = a->a_next;
246 if (a->a_vals != &dummy)
247 ber_bvarray_free(a->a_vals);
249 ber_bvarray_free(a->a_nvals);
253 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
260 ldap_abandon(lc->ld, msgid);
264 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
265 char **references = NULL;
268 rc = ldap_parse_reference( lc->ld, res,
269 &references, &rs->sr_ctrls, 1 );
271 if ( rc != LDAP_SUCCESS ) {
275 if ( references == NULL ) {
279 for ( cnt = 0; references[ cnt ]; cnt++ )
282 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
284 for ( cnt = 0; references[ cnt ]; cnt++ ) {
285 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
286 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
289 /* ignore return value by now */
290 ( void )send_search_reference( op, rs );
294 ldap_value_free( references );
295 ch_free( rs->sr_ref );
299 if ( rs->sr_ctrls ) {
300 ldap_controls_free( rs->sr_ctrls );
305 rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
306 &match.bv_val, (char **)&rs->sr_text,
308 if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
309 rs->sr_err = ldap_back_map_result(rs);
319 * Rewrite the matched portion of the search base, if required
321 if ( match.bv_val && *match.bv_val ) {
324 #ifdef ENABLE_REWRITE
325 dc.ctx = "matchedDn";
330 match.bv_len = strlen( match.bv_val );
331 ldap_back_dn_massage(&dc, &match, &mdn);
332 rs->sr_matched = mdn.bv_val;
334 if ( rs->sr_v2ref ) {
335 rs->sr_err = LDAP_REFERRAL;
339 send_ldap_result( op, rs );
341 #ifdef LDAP_BACK_PROXY_AUTHZ
342 if ( ctrls && ctrls != op->o_ctrls ) {
346 #endif /* LDAP_BACK_PROXY_AUTHZ */
348 if ( match.bv_val ) {
349 if ( rs->sr_matched != match.bv_val ) {
350 free( (char *)rs->sr_matched );
352 rs->sr_matched = NULL;
353 LDAP_FREE( match.bv_val );
356 if ( !dontfreetext ) {
357 LDAP_FREE( (char *)rs->sr_text );
361 if ( mapped_attrs ) {
362 ch_free( mapped_attrs );
364 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
365 ch_free( mfilter.bv_val );
367 if ( mbase.bv_val != op->o_req_dn.bv_val ) {
368 free( mbase.bv_val );
383 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
384 struct berval a, mapped;
385 BerElement ber = *e->lm_ber;
386 Attribute *attr, **attrp;
390 int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
391 int normalize = flags & LDAP_BUILD_ENTRY_NORMALIZE;
394 /* safe assumptions ... */
396 ent->e_bv.bv_val = NULL;
398 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
399 return LDAP_DECODING_ERROR;
403 * Rewrite the dn of the result, if needed
405 dc.rwmap = &li->rwmap;
406 #ifdef ENABLE_REWRITE
407 dc.conn = op->o_conn;
409 dc.ctx = "searchResult";
414 if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
419 * Note: this may fail if the target host(s) schema differs
420 * from the one known to the meta, and a DN with unknown
421 * attributes is returned.
423 * FIXME: should we log anything, or delegate to dnNormalize?
425 if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
426 op->o_tmpmemctx ) != LDAP_SUCCESS )
428 return LDAP_INVALID_DN_SYNTAX;
431 attrp = &ent->e_attrs;
433 #ifdef ENABLE_REWRITE
434 dc.ctx = "searchAttrDN";
436 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
437 ldap_back_map(&li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP);
438 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
440 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
446 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
447 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
450 LDAP_LOG( BACK_LDAP, DETAIL1,
451 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
452 #else /* !NEW_LOGGING */
453 Debug( LDAP_DEBUG_ANY,
454 "slap_bv2undef_ad(%s): "
455 "%s\n%s", mapped.bv_val, text, "" );
456 #endif /* !NEW_LOGGING */
462 /* no subschemaSubentry */
463 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
466 * We eat target's subschemaSubentry because
467 * a search for this value is likely not
468 * to resolve to the appropriate backend;
469 * later, the local subschemaSubentry is
472 ( void )ber_scanf( &ber, "x" /* [W] */ );
478 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
479 || attr->a_vals == NULL ) {
481 * Note: attr->a_vals can be null when using
482 * values result filter
485 attr->a_vals = &dummy;
487 attr->a_vals = ch_malloc(sizeof(struct berval));
488 attr->a_vals->bv_val = NULL;
489 attr->a_vals->bv_len = 0;
493 for ( last = 0; attr->a_vals[last].bv_val; last++ );
497 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
498 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
499 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
500 ldap_back_map(&li->rwmap.rwm_oc, bv, &mapped,
502 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
503 LBER_FREE(bv->bv_val);
507 *bv = attr->a_vals[last];
508 attr->a_vals[last].bv_val = NULL;
511 } else if ( mapped.bv_val != bv->bv_val ) {
513 * FIXME: after LBER_FREEing
514 * the value is replaced by
517 LBER_FREE(bv->bv_val);
518 ber_dupbv( bv, &mapped );
523 * It is necessary to try to rewrite attributes with
524 * dn syntax because they might be used in ACLs as
525 * members of groups; since ACLs are applied to the
526 * rewritten stuff, no dn-based subject clause could
527 * be used at the ldap backend side (see
528 * http://www.OpenLDAP.org/faq/data/cache/452.html)
529 * The problem can be overcome by moving the dn-based
530 * ACLs to the target directory server, and letting
531 * everything pass thru the ldap backend.
533 } else if ( attr->a_desc->ad_type->sat_syntax ==
534 slap_schema.si_syn_distinguishedName ) {
535 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
538 if ( normalize && last && attr->a_desc->ad_type->sat_equality &&
539 attr->a_desc->ad_type->sat_equality->smr_normalize ) {
542 attr->a_nvals = ch_malloc((last+1)*sizeof(struct berval));
543 for (i=0; i<last; i++) {
544 attr->a_desc->ad_type->sat_equality->smr_normalize(
545 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
546 attr->a_desc->ad_type->sat_syntax,
547 attr->a_desc->ad_type->sat_equality,
548 &attr->a_vals[i], &attr->a_nvals[i],
549 NULL /* op->o_tmpmemctx */ );
551 attr->a_nvals[i].bv_val = NULL;
552 attr->a_nvals[i].bv_len = 0;
554 attr->a_nvals = attr->a_vals;
557 attrp = &attr->a_next;
560 /* make sure it's free'able */
561 if (!private && ent->e_name.bv_val == bdn->bv_val)
562 ber_dupbv( &ent->e_name, bdn );
566 /* return 0 IFF we can retrieve the entry with ndn
573 AttributeDescription *at,
578 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
581 struct berval mapped = { 0, NULL }, bdn, mdn;
582 LDAPMessage *result = NULL, *e = NULL;
589 /* Tell getconn this is a privileged op */
590 is_oc = op->o_do_not_cache;
591 op->o_do_not_cache = 1;
592 lc = ldap_back_getconn(op, &rs);
595 if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
596 op->o_do_not_cache = is_oc;
600 op->o_do_not_cache = is_oc;
604 * Rewrite the search base, if required
606 dc.rwmap = &li->rwmap;
607 #ifdef ENABLE_REWRITE
608 dc.conn = op->o_conn;
610 dc.ctx = "searchBase";
615 if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
620 ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
621 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
627 is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
629 gattr[0] = "objectclass";
630 gattr[1] = mapped.bv_val;
633 gattr[0] = mapped.bv_val;
638 ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
640 filter = ch_malloc(sizeof("(objectclass=)") + mapped.bv_len);
641 ptr = lutil_strcopy(filter, "(objectclass=");
642 ptr = lutil_strcopy(ptr, mapped.bv_val);
647 if (ldap_search_ext_s(lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
648 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
649 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
654 if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
658 *ent = ch_calloc(1,sizeof(Entry));
660 rc = ldap_build_entry(op, e, *ent, &bdn, LDAP_BUILD_ENTRY_NORMALIZE);
662 if (rc != LDAP_SUCCESS) {
669 ldap_msgfree(result);
676 if ( mdn.bv_val != ndn->bv_val ) {
677 ch_free( mdn.bv_val );