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;
62 LDAPControl **ctrls = NULL;
64 lc = ldap_back_getconn(op, rs);
70 * FIXME: in case of values return filter, we might want
71 * to map attrs and maybe rewrite value
73 if ( !ldap_back_dobind( lc, op, rs ) ) {
77 /* should we check return values? */
78 if (op->ors_deref != -1)
79 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->ors_deref);
80 if (op->ors_tlimit != -1) {
81 tv.tv_sec = op->ors_tlimit;
88 * Rewrite the search base, if required
90 dc.rwmap = &li->rwmap;
94 dc.ctx = "searchBase";
99 if ( ldap_back_dn_massage( &dc, &op->o_req_ndn, &mbase ) ) {
100 send_ldap_result( op, rs );
104 rc = ldap_back_filter_map_rewrite( &dc, op->ors_filter,
105 &mfilter, BACKLDAP_MAP );
111 case LDAP_COMPARE_FALSE:
112 rs->sr_err = LDAP_SUCCESS;
118 rs->sr_err = LDAP_OTHER;
119 rs->sr_text = "Rewrite error";
125 rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
127 BACKLDAP_MAP, &mapped_attrs );
134 #ifdef LDAP_BACK_PROXY_AUTHZ
135 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
136 if ( rc != LDAP_SUCCESS ) {
140 #endif /* LDAP_BACK_PROXY_AUTHZ */
142 rs->sr_err = ldap_search_ext(lc->ld, mbase.bv_val,
143 op->ors_scope, mfilter.bv_val,
144 mapped_attrs, op->ors_attrsonly,
146 tv.tv_sec ? &tv : NULL, op->ors_slimit,
149 if ( rs->sr_err != LDAP_SUCCESS ) {
151 rc = ldap_back_op_result(lc, op, rs, msgid, 0);
155 /* We pull apart the ber result, stuff it into a slapd entry, and
156 * let send_search_entry stuff it back into ber format. Slow & ugly,
157 * but this is necessary for version matching, and for ACL processing.
160 for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
162 /* check for abandon */
164 ldap_abandon(lc->ld, msgid);
172 ldap_pvt_thread_yield();
174 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
178 e = ldap_first_entry(lc->ld,res);
179 if ( ( rc = ldap_build_entry(op, e, &ent, &bdn,
180 LDAP_BUILD_ENTRY_PRIVATE)) == LDAP_SUCCESS ) {
182 rs->sr_attrs = op->ors_attrs;
184 abort = send_search_entry( op, rs );
185 while (ent.e_attrs) {
190 ent.e_attrs = a->a_next;
193 if (a->a_vals != &dummy)
194 ber_bvarray_free(a->a_vals);
196 ber_bvarray_free(a->a_nvals);
200 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
207 ldap_abandon(lc->ld, msgid);
211 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
212 char **references = NULL;
215 rc = ldap_parse_reference( lc->ld, res,
216 &references, &rs->sr_ctrls, 1 );
218 if ( rc != LDAP_SUCCESS ) {
222 if ( references == NULL ) {
226 for ( cnt = 0; references[ cnt ]; cnt++ )
229 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
231 for ( cnt = 0; references[ cnt ]; cnt++ ) {
232 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
233 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
236 /* ignore return value by now */
237 ( void )send_search_reference( op, rs );
241 ldap_value_free( references );
242 ch_free( rs->sr_ref );
246 if ( rs->sr_ctrls ) {
247 ldap_controls_free( rs->sr_ctrls );
252 rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
253 &match.bv_val, (char **)&rs->sr_text,
255 if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
256 rs->sr_err = slap_map_api2result( rs );
266 * Rewrite the matched portion of the search base, if required
268 if ( match.bv_val && *match.bv_val ) {
271 #ifdef ENABLE_REWRITE
272 dc.ctx = "matchedDN";
277 match.bv_len = strlen( match.bv_val );
278 ldap_back_dn_massage(&dc, &match, &mdn);
279 rs->sr_matched = mdn.bv_val;
281 if ( rs->sr_v2ref ) {
282 rs->sr_err = LDAP_REFERRAL;
286 send_ldap_result( op, rs );
288 #ifdef LDAP_BACK_PROXY_AUTHZ
289 if ( ctrls && ctrls != op->o_ctrls ) {
293 #endif /* LDAP_BACK_PROXY_AUTHZ */
295 if ( match.bv_val ) {
296 if ( rs->sr_matched != match.bv_val ) {
297 free( (char *)rs->sr_matched );
299 rs->sr_matched = NULL;
300 LDAP_FREE( match.bv_val );
303 if ( !dontfreetext ) {
304 LDAP_FREE( (char *)rs->sr_text );
308 if ( mapped_attrs ) {
309 ch_free( mapped_attrs );
311 if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
312 ch_free( mfilter.bv_val );
314 if ( mbase.bv_val != op->o_req_ndn.bv_val ) {
315 free( mbase.bv_val );
330 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
331 struct berval a, mapped;
332 BerElement ber = *e->lm_ber;
333 Attribute *attr, **attrp;
337 int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
340 /* safe assumptions ... */
342 ent->e_bv.bv_val = NULL;
344 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
345 return LDAP_DECODING_ERROR;
349 * Rewrite the dn of the result, if needed
351 dc.rwmap = &li->rwmap;
352 #ifdef ENABLE_REWRITE
353 dc.conn = op->o_conn;
355 dc.ctx = "searchResult";
360 if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
365 * Note: this may fail if the target host(s) schema differs
366 * from the one known to the meta, and a DN with unknown
367 * attributes is returned.
369 * FIXME: should we log anything, or delegate to dnNormalize?
371 /* Note: if the distinguished values or the naming attributes
372 * change, should we massage them as well?
374 if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
375 op->o_tmpmemctx ) != LDAP_SUCCESS )
377 return LDAP_INVALID_DN_SYNTAX;
380 attrp = &ent->e_attrs;
382 #ifdef ENABLE_REWRITE
383 dc.ctx = "searchAttrDN";
385 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
386 ldap_back_map(&li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP);
387 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
389 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
395 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
396 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
399 LDAP_LOG( BACK_LDAP, DETAIL1,
400 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
401 #else /* !NEW_LOGGING */
402 Debug( LDAP_DEBUG_ANY,
403 "slap_bv2undef_ad(%s): "
404 "%s\n%s", mapped.bv_val, text, "" );
405 #endif /* !NEW_LOGGING */
411 /* no subschemaSubentry */
412 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
415 * We eat target's subschemaSubentry because
416 * a search for this value is likely not
417 * to resolve to the appropriate backend;
418 * later, the local subschemaSubentry is
421 ( void )ber_scanf( &ber, "x" /* [W] */ );
427 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
428 || attr->a_vals == NULL ) {
430 * Note: attr->a_vals can be null when using
431 * values result filter
434 attr->a_vals = &dummy;
436 attr->a_vals = ch_malloc(sizeof(struct berval));
437 attr->a_vals->bv_val = NULL;
438 attr->a_vals->bv_len = 0;
442 for ( last = 0; attr->a_vals[last].bv_val; last++ );
446 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
447 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
448 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
449 ldap_back_map(&li->rwmap.rwm_oc, bv, &mapped,
451 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
452 LBER_FREE(bv->bv_val);
456 *bv = attr->a_vals[last];
457 attr->a_vals[last].bv_val = NULL;
460 } else if ( mapped.bv_val != bv->bv_val ) {
462 * FIXME: after LBER_FREEing
463 * the value is replaced by
466 LBER_FREE(bv->bv_val);
467 ber_dupbv( bv, &mapped );
472 * It is necessary to try to rewrite attributes with
473 * dn syntax because they might be used in ACLs as
474 * members of groups; since ACLs are applied to the
475 * rewritten stuff, no dn-based subject clause could
476 * be used at the ldap backend side (see
477 * http://www.OpenLDAP.org/faq/data/cache/452.html)
478 * The problem can be overcome by moving the dn-based
479 * ACLs to the target directory server, and letting
480 * everything pass thru the ldap backend.
482 } else if ( attr->a_desc->ad_type->sat_syntax ==
483 slap_schema.si_syn_distinguishedName ) {
484 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
487 if ( last && attr->a_desc->ad_type->sat_equality &&
488 attr->a_desc->ad_type->sat_equality->smr_normalize ) {
491 attr->a_nvals = ch_malloc((last+1)*sizeof(struct berval));
492 for (i=0; i<last; i++) {
493 attr->a_desc->ad_type->sat_equality->smr_normalize(
494 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
495 attr->a_desc->ad_type->sat_syntax,
496 attr->a_desc->ad_type->sat_equality,
497 &attr->a_vals[i], &attr->a_nvals[i],
498 NULL /* op->o_tmpmemctx */ );
500 attr->a_nvals[i].bv_val = NULL;
501 attr->a_nvals[i].bv_len = 0;
503 attr->a_nvals = attr->a_vals;
506 attrp = &attr->a_next;
509 /* make sure it's free'able */
510 if (!private && ent->e_name.bv_val == bdn->bv_val)
511 ber_dupbv( &ent->e_name, bdn );
515 /* return 0 IFF we can retrieve the entry with ndn
522 AttributeDescription *at,
527 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
530 struct berval mapped = BER_BVNULL, bdn, mdn;
531 LDAPMessage *result = NULL, *e = NULL;
538 /* Tell getconn this is a privileged op */
539 is_oc = op->o_do_not_cache;
540 op->o_do_not_cache = 1;
541 lc = ldap_back_getconn(op, &rs);
544 if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
545 op->o_do_not_cache = is_oc;
549 op->o_do_not_cache = is_oc;
553 * Rewrite the search base, if required
555 dc.rwmap = &li->rwmap;
556 #ifdef ENABLE_REWRITE
557 dc.conn = op->o_conn;
559 dc.ctx = "searchBase";
564 if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
569 ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
570 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
574 is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
576 gattr[0] = "objectclass";
577 gattr[1] = mapped.bv_val;
580 gattr[0] = mapped.bv_val;
587 ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
589 filter = ch_malloc(sizeof("(objectclass=)") + mapped.bv_len);
590 ptr = lutil_strcopy(filter, "(objectclass=");
591 ptr = lutil_strcopy(ptr, mapped.bv_val);
596 if (ldap_search_ext_s(lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
597 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
598 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
603 if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
607 *ent = ch_calloc(1,sizeof(Entry));
609 rc = ldap_build_entry(op, e, *ent, &bdn, 0);
611 if (rc != LDAP_SUCCESS) {
618 ldap_msgfree(result);
625 if ( mdn.bv_val != ndn->bv_val ) {
626 ch_free( mdn.bv_val );