1 /* search.c - ldap backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by the Howard Chu for inclusion
18 * in OpenLDAP Software and subsequently enhanced by Pierangelo
21 /* This is an altered version */
23 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
25 * Permission is granted to anyone to use this software for any purpose
26 * on any computer system, and to alter it and redistribute it, subject
27 * to the following restrictions:
29 * 1. The author is not responsible for the consequences of use of this
30 * software, no matter how awful, even if they arise from flaws in it.
32 * 2. The origin of this software must not be misrepresented, either by
33 * explicit claim or by omission. Since few users ever read sources,
34 * credits should appear in the documentation.
36 * 3. Altered versions must be plainly marked as such, and must not be
37 * misrepresented as being the original software. Since few users
38 * ever read sources, credits should appear in the documentation.
40 * 4. This notice may not be removed or altered.
44 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
46 * This software is being modified by Pierangelo Masarati.
47 * The previously reported conditions apply to the modified code as well.
48 * Changes in the original code are highlighted where required.
49 * Credits for the original code go to the author, Howard Chu.
56 #include <ac/socket.h>
57 #include <ac/string.h>
61 #include "back-ldap.h"
62 #undef ldap_debug /* silence a warning in ldap-int.h */
63 #include "../../../libraries/libldap/ldap-int.h"
68 ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
69 struct berval *bdn, int flags );
70 #define LDAP_BUILD_ENTRY_PRIVATE 0x01
71 #define LDAP_BUILD_ENTRY_NORMALIZE 0x02
73 static struct berval dummy = { 0, NULL };
80 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
85 struct berval match = { 0, NULL };
86 char **mapped_attrs = NULL;
88 struct berval mfilter = { 0, NULL };
89 struct slap_limits_set *limit = NULL;
93 #ifdef LDAP_BACK_PROXY_AUTHZ
94 LDAPControl **ctrls = NULL;
95 #endif /* LDAP_BACK_PROXY_AUTHZ */
97 lc = ldap_back_getconn(op, rs);
103 * FIXME: in case of values return filter, we might want
104 * to map attrs and maybe rewrite value
106 if ( !ldap_back_dobind( lc, op, rs ) ) {
110 /* if not root, get appropriate limits */
111 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
114 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
117 /* if no time limit requested, rely on remote server limits */
118 /* if requested limit higher than hard limit, abort */
119 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
120 /* no hard limit means use soft instead */
121 if ( limit->lms_t_hard == 0
122 && limit->lms_t_soft > -1
123 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
124 op->oq_search.rs_tlimit = limit->lms_t_soft;
126 /* positive hard limit means abort */
127 } else if ( limit->lms_t_hard > 0 ) {
128 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
133 /* negative hard limit means no limit */
136 /* if no size limit requested, rely on remote server limits */
137 /* if requested limit higher than hard limit, abort */
138 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
139 /* no hard limit means use soft instead */
140 if ( limit->lms_s_hard == 0
141 && limit->lms_s_soft > -1
142 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
143 op->oq_search.rs_slimit = limit->lms_s_soft;
145 /* positive hard limit means abort */
146 } else if ( limit->lms_s_hard > 0 ) {
147 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
152 /* negative hard limit means no limit */
155 /* should we check return values? */
156 if (op->oq_search.rs_deref != -1)
157 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->oq_search.rs_deref);
158 if (op->oq_search.rs_tlimit != -1) {
159 tv.tv_sec = op->oq_search.rs_tlimit;
166 * Rewrite the search base, if required
168 dc.rwmap = &li->rwmap;
169 #ifdef ENABLE_REWRITE
170 dc.conn = op->o_conn;
172 dc.ctx = "searchBase";
177 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mbase ) ) {
178 send_ldap_result( op, rs );
182 rc = ldap_back_filter_map_rewrite( &dc, op->oq_search.rs_filter,
183 &mfilter, BACKLDAP_MAP );
186 rs->sr_err = LDAP_OTHER;
187 rs->sr_text = "Rewrite error";
193 rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
194 op->oq_search.rs_attrs,
195 BACKLDAP_MAP, &mapped_attrs );
201 #ifdef LDAP_BACK_PROXY_AUTHZ
202 rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
203 if ( rc != LDAP_SUCCESS ) {
207 #endif /* LDAP_BACK_PROXY_AUTHZ */
209 rs->sr_err = ldap_search_ext(lc->ld, mbase.bv_val,
210 op->oq_search.rs_scope, mfilter.bv_val,
211 mapped_attrs, op->oq_search.rs_attrsonly,
212 #ifdef LDAP_BACK_PROXY_AUTHZ
214 #else /* ! LDAP_BACK_PROXY_AUTHZ */
216 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
218 tv.tv_sec ? &tv : NULL, op->oq_search.rs_slimit,
221 if ( rs->sr_err != LDAP_SUCCESS ) {
223 rc = ldap_back_op_result(lc, op, rs, msgid, 0);
227 /* We pull apart the ber result, stuff it into a slapd entry, and
228 * let send_search_entry stuff it back into ber format. Slow & ugly,
229 * but this is necessary for version matching, and for ACL processing.
232 for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
234 /* check for abandon */
236 ldap_abandon(lc->ld, msgid);
244 ldap_pvt_thread_yield();
246 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
249 e = ldap_first_entry(lc->ld,res);
250 if ( ldap_build_entry(op, e, &ent, &bdn,
251 LDAP_BUILD_ENTRY_PRIVATE) == LDAP_SUCCESS ) {
253 rs->sr_attrs = op->oq_search.rs_attrs;
254 send_search_entry( op, rs );
255 while (ent.e_attrs) {
260 ent.e_attrs = a->a_next;
263 if (a->a_vals != &dummy)
264 ber_bvarray_free(a->a_vals);
266 ber_bvarray_free(a->a_nvals);
270 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
277 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
278 char **references = NULL;
281 rc = ldap_parse_reference( lc->ld, res,
282 &references, &rs->sr_ctrls, 1 );
284 if ( rc != LDAP_SUCCESS ) {
288 if ( references == NULL ) {
292 for ( cnt = 0; references[ cnt ]; cnt++ )
295 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
297 for ( cnt = 0; references[ cnt ]; cnt++ ) {
298 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
299 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
302 /* ignore return value by now */
303 ( void )send_search_reference( op, rs );
307 ldap_value_free( references );
308 ch_free( rs->sr_ref );
312 if ( rs->sr_ctrls ) {
313 ldap_controls_free( rs->sr_ctrls );
318 rc = ldap_parse_result(lc->ld, res, &rs->sr_err,
319 &match.bv_val, (char **)&rs->sr_text,
321 if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
322 rs->sr_err = ldap_back_map_result(rs);
332 * Rewrite the matched portion of the search base, if required
334 if ( match.bv_val && *match.bv_val ) {
337 #ifdef ENABLE_REWRITE
338 dc.ctx = "matchedDn";
343 match.bv_len = strlen( match.bv_val );
344 ldap_back_dn_massage(&dc, &match, &mdn);
345 rs->sr_matched = mdn.bv_val;
347 if ( rs->sr_v2ref ) {
348 rs->sr_err = LDAP_REFERRAL;
352 send_ldap_result( op, rs );
354 #ifdef LDAP_BACK_PROXY_AUTHZ
355 if ( ctrls && ctrls != op->o_ctrls ) {
359 #endif /* LDAP_BACK_PROXY_AUTHZ */
361 if ( match.bv_val ) {
362 if ( rs->sr_matched != match.bv_val ) {
363 free( (char *)rs->sr_matched );
365 rs->sr_matched = NULL;
366 LDAP_FREE( match.bv_val );
369 if ( !dontfreetext ) {
370 LDAP_FREE( (char *)rs->sr_text );
374 if ( mapped_attrs ) {
375 ch_free( mapped_attrs );
377 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
378 ch_free( mfilter.bv_val );
380 if ( mbase.bv_val != op->o_req_dn.bv_val ) {
381 free( mbase.bv_val );
396 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
397 struct berval a, mapped;
398 BerElement ber = *e->lm_ber;
399 Attribute *attr, **attrp;
403 int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
404 int normalize = flags & LDAP_BUILD_ENTRY_NORMALIZE;
407 /* safe assumptions ... */
409 ent->e_bv.bv_val = NULL;
411 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
412 return LDAP_DECODING_ERROR;
416 * Rewrite the dn of the result, if needed
418 dc.rwmap = &li->rwmap;
419 #ifdef ENABLE_REWRITE
420 dc.conn = op->o_conn;
422 dc.ctx = "searchResult";
427 if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
432 * Note: this may fail if the target host(s) schema differs
433 * from the one known to the meta, and a DN with unknown
434 * attributes is returned.
436 * FIXME: should we log anything, or delegate to dnNormalize?
438 if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
439 op->o_tmpmemctx ) != LDAP_SUCCESS )
441 return LDAP_INVALID_DN_SYNTAX;
448 attrp = &ent->e_attrs;
450 #ifdef ENABLE_REWRITE
451 dc.ctx = "searchAttrDN";
453 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
454 ldap_back_map(&li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP);
455 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
457 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
463 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
464 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
467 LDAP_LOG( BACK_LDAP, DETAIL1,
468 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
469 #else /* !NEW_LOGGING */
470 Debug( LDAP_DEBUG_ANY,
471 "slap_bv2undef_ad(%s): "
472 "%s\n%s", mapped.bv_val, text, "" );
473 #endif /* !NEW_LOGGING */
479 /* no subschemaSubentry */
480 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
483 * We eat target's subschemaSubentry because
484 * a search for this value is likely not
485 * to resolve to the appropriate backend;
486 * later, the local subschemaSubentry is
489 ( void )ber_scanf( &ber, "x" /* [W] */ );
495 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
496 || attr->a_vals == NULL ) {
498 * Note: attr->a_vals can be null when using
499 * values result filter
502 attr->a_vals = &dummy;
504 attr->a_vals = ch_malloc(sizeof(struct berval));
505 attr->a_vals->bv_val = NULL;
506 attr->a_vals->bv_len = 0;
510 for ( last = 0; attr->a_vals[last].bv_val; last++ );
514 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
515 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
516 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
517 ldap_back_map(&li->rwmap.rwm_oc, bv, &mapped,
519 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
520 LBER_FREE(bv->bv_val);
524 *bv = attr->a_vals[last];
525 attr->a_vals[last].bv_val = NULL;
528 } else if ( mapped.bv_val != bv->bv_val ) {
530 * FIXME: after LBER_FREEing
531 * the value is replaced by
534 LBER_FREE(bv->bv_val);
535 ber_dupbv( bv, &mapped );
540 * It is necessary to try to rewrite attributes with
541 * dn syntax because they might be used in ACLs as
542 * members of groups; since ACLs are applied to the
543 * rewritten stuff, no dn-based subject clause could
544 * be used at the ldap backend side (see
545 * http://www.OpenLDAP.org/faq/data/cache/452.html)
546 * The problem can be overcome by moving the dn-based
547 * ACLs to the target directory server, and letting
548 * everything pass thru the ldap backend.
550 } else if ( attr->a_desc->ad_type->sat_syntax ==
551 slap_schema.si_syn_distinguishedName ) {
552 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
555 if ( normalize && last && attr->a_desc->ad_type->sat_equality &&
556 attr->a_desc->ad_type->sat_equality->smr_normalize ) {
559 attr->a_nvals = ch_malloc((last+1)*sizeof(struct berval));
560 for (i=0; i<last; i++) {
561 attr->a_desc->ad_type->sat_equality->smr_normalize(
562 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
563 attr->a_desc->ad_type->sat_syntax,
564 attr->a_desc->ad_type->sat_equality,
565 &attr->a_vals[i], &attr->a_nvals[i],
566 NULL /* op->o_tmpmemctx */ );
568 attr->a_nvals[i].bv_val = NULL;
569 attr->a_nvals[i].bv_len = 0;
571 attr->a_nvals = attr->a_vals;
574 attrp = &attr->a_next;
577 /* make sure it's free'able */
578 if (!private && ent->e_name.bv_val == bdn->bv_val)
579 ber_dupbv( &ent->e_name, bdn );
583 /* return 0 IFF we can retrieve the entry with ndn
590 AttributeDescription *at,
595 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
598 struct berval mapped = { 0, NULL }, bdn, mdn;
599 LDAPMessage *result = NULL, *e = NULL;
606 /* Tell getconn this is a privileged op */
607 is_oc = op->o_do_not_cache;
608 op->o_do_not_cache = 1;
609 lc = ldap_back_getconn(op, &rs);
612 if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
613 op->o_do_not_cache = is_oc;
617 op->o_do_not_cache = is_oc;
621 * Rewrite the search base, if required
623 dc.rwmap = &li->rwmap;
624 #ifdef ENABLE_REWRITE
625 dc.conn = op->o_conn;
627 dc.ctx = "searchBase";
632 if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
637 ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
638 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
644 is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
646 gattr[0] = "objectclass";
647 gattr[1] = mapped.bv_val;
650 gattr[0] = mapped.bv_val;
655 ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
657 filter = ch_malloc(sizeof("(objectclass=)") + mapped.bv_len);
658 ptr = lutil_strcopy(filter, "(objectclass=");
659 ptr = lutil_strcopy(ptr, mapped.bv_val);
664 if (ldap_search_ext_s(lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
665 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
666 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
671 if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
675 *ent = ch_calloc(1,sizeof(Entry));
677 rc = ldap_build_entry(op, e, *ent, &bdn, LDAP_BUILD_ENTRY_NORMALIZE);
679 if (rc != LDAP_SUCCESS) {
686 ldap_msgfree(result);
693 if ( mdn.bv_val != ndn->bv_val ) {
694 ch_free( mdn.bv_val );