1 /* search.c - ldap backend search function */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 /* This is an altered version */
9 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
11 * Permission is granted to anyone to use this software for any purpose
12 * on any computer system, and to alter it and redistribute it, subject
13 * to the following restrictions:
15 * 1. The author is not responsible for the consequences of use of this
16 * software, no matter how awful, even if they arise from flaws in it.
18 * 2. The origin of this software must not be misrepresented, either by
19 * explicit claim or by omission. Since few users ever read sources,
20 * credits should appear in the documentation.
22 * 3. Altered versions must be plainly marked as such, and must not be
23 * misrepresented as being the original software. Since few users
24 * ever read sources, credits should appear in the documentation.
26 * 4. This notice may not be removed or altered.
30 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
32 * This software is being modified by Pierangelo Masarati.
33 * The previously reported conditions apply to the modified code as well.
34 * Changes in the original code are highlighted where required.
35 * Credits for the original code go to the author, Howard Chu.
42 #include <ac/socket.h>
43 #include <ac/string.h>
47 #include "back-ldap.h"
48 #undef ldap_debug /* silence a warning in ldap-int.h */
49 #include "../../../libraries/libldap/ldap-int.h"
53 static struct berval dummy = { 0, NULL };
60 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
64 int count, rc = 0, msgid;
66 char **mapped_attrs = NULL;
68 struct berval mfilter = { 0, NULL };
69 struct slap_limits_set *limit = NULL;
72 lc = ldap_back_getconn(op, rs);
78 * FIXME: in case of values return filter, we might want
79 * to map attrs and maybe rewrite value
81 if ( !ldap_back_dobind( lc, op, rs ) ) {
85 /* if not root, get appropriate limits */
86 if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
89 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
92 /* if no time limit requested, rely on remote server limits */
93 /* if requested limit higher than hard limit, abort */
94 if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
95 /* no hard limit means use soft instead */
96 if ( limit->lms_t_hard == 0
97 && limit->lms_t_soft > -1
98 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
99 op->oq_search.rs_tlimit = limit->lms_t_soft;
101 /* positive hard limit means abort */
102 } else if ( limit->lms_t_hard > 0 ) {
103 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
104 send_ldap_result( op, rs );
109 /* negative hard limit means no limit */
112 /* if no size limit requested, rely on remote server limits */
113 /* if requested limit higher than hard limit, abort */
114 if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
115 /* no hard limit means use soft instead */
116 if ( limit->lms_s_hard == 0
117 && limit->lms_s_soft > -1
118 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
119 op->oq_search.rs_slimit = limit->lms_s_soft;
121 /* positive hard limit means abort */
122 } else if ( limit->lms_s_hard > 0 ) {
123 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
124 send_ldap_result( op, rs );
129 /* negative hard limit means no limit */
132 /* should we check return values? */
133 if (op->oq_search.rs_deref != -1)
134 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->oq_search.rs_deref);
135 if (op->oq_search.rs_tlimit != -1) {
136 tv.tv_sec = op->oq_search.rs_tlimit;
143 * Rewrite the search base, if required
145 #ifdef ENABLE_REWRITE
146 switch ( rewrite_session( li->rwinfo, "searchBase",
147 op->o_req_dn.bv_val, op->o_conn, &mbase.bv_val ) ) {
148 case REWRITE_REGEXEC_OK:
149 if ( mbase.bv_val == NULL ) {
150 mbase = op->o_req_dn;
152 mbase.bv_len = strlen( mbase.bv_val );
155 LDAP_LOG( BACK_LDAP, DETAIL1,
156 "[rw] searchBase: \"%s\" -> \"%s\"\n",
157 op->o_req_dn.bv_val, mbase.bv_val, 0 );
158 #else /* !NEW_LOGGING */
159 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
160 op->o_req_dn.bv_val, mbase.bv_val, "" );
161 #endif /* !NEW_LOGGING */
164 case REWRITE_REGEXEC_UNWILLING:
165 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
166 "Operation not allowed" );
170 case REWRITE_REGEXEC_ERR:
171 send_ldap_error( op, rs, LDAP_OTHER,
177 #else /* !ENABLE_REWRITE */
178 ldap_back_dn_massage( li, &op->o_req_dn, &mbase, 0, 1 );
179 #endif /* !ENABLE_REWRITE */
181 #ifdef ENABLE_REWRITE
182 rc = ldap_back_filter_map_rewrite_( li->rwinfo, op->o_conn,
183 &li->at_map, &li->oc_map, op->oq_search.rs_filter, &mfilter,
185 #else /* ! ENABLE_REWRITE */
186 rc = ldap_back_filter_map_rewrite_( &li->at_map, &li->oc_map,
187 op->oq_search.rs_filter, &mfilter, BACKLDAP_MAP );
188 #endif /* ! ENABLE_REWRITE */
195 mapped_attrs = ldap_back_map_attrs(&li->at_map, op->oq_search.rs_attrs, BACKLDAP_MAP);
196 if ( mapped_attrs == NULL && op->oq_search.rs_attrs) {
197 for (count=0; op->oq_search.rs_attrs[count].an_name.bv_val; count++);
198 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
199 for (count=0; op->oq_search.rs_attrs[count].an_name.bv_val; count++) {
200 mapped_attrs[count] = op->oq_search.rs_attrs[count].an_name.bv_val;
202 mapped_attrs[count] = NULL;
205 rc = ldap_search_ext(lc->ld, mbase.bv_val, op->oq_search.rs_scope, mfilter.bv_val,
206 mapped_attrs, op->oq_search.rs_attrsonly, op->o_ctrls, NULL, tv.tv_sec ? &tv
207 : NULL, op->oq_search.rs_slimit, &msgid);
208 if ( rc != LDAP_SUCCESS ) {
210 rc = ldap_back_op_result(lc, op, rs, msgid, rc, 0);
214 /* We pull apart the ber result, stuff it into a slapd entry, and
215 * let send_search_entry stuff it back into ber format. Slow & ugly,
216 * but this is necessary for version matching, and for ACL processing.
219 for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
221 /* check for abandon */
223 ldap_abandon(lc->ld, msgid);
231 ldap_pvt_thread_yield();
233 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
236 e = ldap_first_entry(lc->ld,res);
237 if ( ldap_build_entry(op, e, &ent, &bdn, 1) == LDAP_SUCCESS ) {
240 rs->sr_attrs = op->oq_search.rs_attrs;
241 send_search_entry( op, rs );
242 while (ent.e_attrs) {
244 ent.e_attrs = a->a_next;
245 if (a->a_vals != &dummy)
246 ber_bvarray_free(a->a_vals);
250 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
257 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
258 char **references = NULL;
261 rc = ldap_parse_reference( lc->ld, res,
262 &references, &rs->sr_ctrls, 1 );
264 if ( rc != LDAP_SUCCESS ) {
268 if ( references == NULL ) {
272 for ( cnt = 0; references[ cnt ]; cnt++ )
275 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
277 for ( cnt = 0; references[ cnt ]; cnt++ ) {
278 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
279 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
282 /* ignore return value by now */
283 ( void )send_search_reference( op, rs );
287 ldap_value_free( references );
288 ch_free( rs->sr_ref );
292 if ( rs->sr_ctrls ) {
293 ldap_controls_free( rs->sr_ctrls );
298 rc = ldap_parse_result(lc->ld, res, &rs->sr_err, &match,
299 (char **)&rs->sr_text, NULL, NULL, 1);
300 if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
301 rs->sr_err = ldap_back_map_result(rs->sr_err);
310 #ifdef ENABLE_REWRITE
312 * Rewrite the matched portion of the search base, if required
314 if ( match != NULL ) {
315 switch ( rewrite_session( li->rwinfo, "matchedDn",
316 match, op->o_conn, (char **)&rs->sr_matched ) ) {
317 case REWRITE_REGEXEC_OK:
318 if ( rs->sr_matched == NULL ) {
319 rs->sr_matched = ( char * )match;
322 LDAP_LOG( BACK_LDAP, DETAIL1,
323 "[rw] matchedDn:" " \"%s\" -> \"%s\"\n", match, rs->sr_matched, 0 );
324 #else /* !NEW_LOGGING */
325 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
326 " \"%s\" -> \"%s\"\n%s",
327 match, rs->sr_matched, "" );
328 #endif /* !NEW_LOGGING */
331 case REWRITE_REGEXEC_UNWILLING:
333 case REWRITE_REGEXEC_ERR:
334 /* FIXME: no error, but no matched ... */
335 rs->sr_matched = NULL;
339 #else /* !ENABLE_REWRITE */
340 if ( match != NULL ) {
341 struct berval dn, mdn;
343 ber_str2bv(match, 0, 0, &dn);
344 ldap_back_dn_massage(li, &dn, &mdn, 0, 0);
345 rs->sr_matched = mdn.bv_val;
347 #endif /* !ENABLE_REWRITE */
348 if ( rs->sr_v2ref ) {
349 rs->sr_err = LDAP_REFERRAL;
351 send_ldap_result( op, rs );
355 if ( rs->sr_matched != match ) {
356 free( (char *)rs->sr_matched );
358 rs->sr_matched = NULL;
362 LDAP_FREE( (char *)rs->sr_text );
365 if ( mapped_attrs ) {
366 ch_free( mapped_attrs );
368 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
369 ch_free( mfilter.bv_val );
371 if ( mbase.bv_val != op->o_req_dn.bv_val ) {
372 free( mbase.bv_val );
387 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
388 struct berval a, mapped;
389 BerElement ber = *e->lm_ber;
390 Attribute *attr, **attrp;
394 /* safe assumptions ... */
396 ent->e_bv.bv_val = NULL;
398 if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
399 return LDAP_DECODING_ERROR;
401 #ifdef ENABLE_REWRITE
404 * Rewrite the dn of the result, if needed
406 switch ( rewrite_session( li->rwinfo, "searchResult",
407 bdn->bv_val, op->o_conn,
408 &ent->e_name.bv_val ) ) {
409 case REWRITE_REGEXEC_OK:
410 if ( ent->e_name.bv_val == NULL ) {
414 LDAP_LOG( BACK_LDAP, DETAIL1,
415 "[rw] searchResult: \"%s\"" " -> \"%s\"\n",
416 bdn->bv_val, ent->e_dn, 0 );
417 #else /* !NEW_LOGGING */
418 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
419 " -> \"%s\"\n%s", bdn->bv_val, ent->e_dn, "" );
420 #endif /* !NEW_LOGGING */
421 ent->e_name.bv_len = strlen( ent->e_name.bv_val );
425 case REWRITE_REGEXEC_ERR:
426 case REWRITE_REGEXEC_UNWILLING:
429 #else /* !ENABLE_REWRITE */
430 ldap_back_dn_massage( li, bdn, &ent->e_name, 0, 0 );
431 #endif /* !ENABLE_REWRITE */
434 * Note: this may fail if the target host(s) schema differs
435 * from the one known to the meta, and a DN with unknown
436 * attributes is returned.
438 * FIXME: should we log anything, or delegate to dnNormalize2?
440 if ( dnNormalize2( NULL, &ent->e_name, &ent->e_nname ) != LDAP_SUCCESS ) {
441 return LDAP_INVALID_DN_SYNTAX;
447 attrp = &ent->e_attrs;
449 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
450 ldap_back_map(&li->at_map, &a, &mapped, BACKLDAP_REMAP);
451 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
453 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
459 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
460 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
463 LDAP_LOG( BACK_LDAP, DETAIL1,
464 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
465 #else /* !NEW_LOGGING */
466 Debug( LDAP_DEBUG_ANY,
467 "slap_bv2undef_ad(%s): "
468 "%s\n%s", mapped.bv_val, text, "" );
469 #endif /* !NEW_LOGGING */
475 /* no subschemaSubentry */
476 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
480 * We eat target's subschemaSubentry because
481 * a search for this value is likely not
482 * to resolve to the appropriate backend;
483 * later, the local subschemaSubentry is
486 ( void )ber_scanf( &ber, "[W]", &vals );
487 for ( bv = vals; bv->bv_val; bv++ ) {
488 LBER_FREE( bv->bv_val );
496 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
497 || attr->a_vals == NULL ) {
499 * Note: attr->a_vals can be null when using
500 * values result filter
503 attr->a_vals = &dummy;
505 attr->a_vals = ch_malloc(sizeof(struct berval));
506 attr->a_vals->bv_val = NULL;
507 attr->a_vals->bv_len = 0;
509 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
510 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
513 for ( last = 0; attr->a_vals[last].bv_val; last++ );
515 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
516 ldap_back_map(&li->oc_map, bv, &mapped,
518 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
519 LBER_FREE(bv->bv_val);
523 *bv = attr->a_vals[last];
524 attr->a_vals[last].bv_val = NULL;
527 } else if ( mapped.bv_val != bv->bv_val ) {
529 * FIXME: after LBER_FREEing
530 * the value is replaced by
533 LBER_FREE(bv->bv_val);
534 ber_dupbv( bv, &mapped );
539 * It is necessary to try to rewrite attributes with
540 * dn syntax because they might be used in ACLs as
541 * members of groups; since ACLs are applied to the
542 * rewritten stuff, no dn-based subject clause could
543 * be used at the ldap backend side (see
544 * http://www.OpenLDAP.org/faq/data/cache/452.html)
545 * The problem can be overcome by moving the dn-based
546 * ACLs to the target directory server, and letting
547 * everything pass thru the ldap backend.
549 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
550 SLAPD_DN_SYNTAX ) == 0 ) {
553 for ( last = 0; attr->a_vals[last].bv_val; last++ );
555 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
556 struct berval newval;
558 #ifdef ENABLE_REWRITE
559 switch ( rewrite_session( li->rwinfo,
564 case REWRITE_REGEXEC_OK:
566 if ( newval.bv_val == NULL ) {
569 newval.bv_len = strlen( newval.bv_val );
571 LDAP_LOG( BACK_LDAP, DETAIL1,
572 "[rw] searchResult on attr=%s: \"%s\" -> \"%s\"\n",
573 attr->a_desc->ad_type->sat_cname.bv_val,
574 bv->bv_val, newval.bv_val );
575 #else /* !NEW_LOGGING */
576 Debug( LDAP_DEBUG_ARGS,
577 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
578 attr->a_desc->ad_type->sat_cname.bv_val,
579 bv->bv_val, newval.bv_val );
580 #endif /* !NEW_LOGGING */
585 case REWRITE_REGEXEC_UNWILLING:
586 LBER_FREE(bv->bv_val);
590 *bv = attr->a_vals[last];
591 attr->a_vals[last].bv_val = NULL;
595 case REWRITE_REGEXEC_ERR:
597 * FIXME: better give up,
599 * or leave it untouched?
603 #else /* !ENABLE_REWRITE */
604 ldap_back_dn_massage( li, bv, &newval, 0, 0 );
605 if ( bv->bv_val != newval.bv_val ) {
606 LBER_FREE( bv->bv_val );
609 #endif /* !ENABLE_REWRITE */
616 attr->a_nvals = attr->a_vals;
619 attrp = &attr->a_next;
621 /* make sure it's free'able */
622 if (!private && ent->e_name.bv_val == bdn->bv_val)
623 ber_dupbv( &ent->e_name, bdn );
627 /* return 0 IFF we can retrieve the entry with ndn
634 AttributeDescription *at,
639 struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
642 struct berval mapped = { 0, NULL }, bdn;
643 LDAPMessage *result = NULL, *e = NULL;
649 ldap_back_map(&li->at_map, &at->ad_cname, &mapped, BACKLDAP_MAP);
650 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
654 /* Tell getconn this is a privileged op */
655 is_oc = op->o_do_not_cache;
656 op->o_do_not_cache = 1;
657 lc = ldap_back_getconn(op, &rs);
660 if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
661 op->o_do_not_cache = is_oc;
665 op->o_do_not_cache = is_oc;
668 is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
670 gattr[0] = "objectclass";
671 gattr[1] = mapped.bv_val;
674 gattr[0] = mapped.bv_val;
679 filter = ch_malloc(sizeof("(objectclass=)") + oc->soc_cname.bv_len);
680 ptr = lutil_strcopy(filter, "(objectclass=");
681 ptr = lutil_strcopy(ptr, oc->soc_cname.bv_val);
685 filter = "(objectclass=*)";
688 if (ldap_search_ext_s(lc->ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
689 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
690 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
695 if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
699 *ent = ch_malloc(sizeof(Entry));
701 rc = ldap_build_entry(op, e, *ent, &bdn, 0);
703 if (rc != LDAP_SUCCESS) {
710 ldap_msgfree(result);