1 /* search.c - ldap backend search function */
4 * Copyright 1998-2002 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"
51 static int ldap_send_entry( Backend *be, Operation *op, struct ldapconn *lc,
52 LDAPMessage *e, AttributeName *attrs, int attrsonly );
66 struct berval *filterstr,
71 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
75 int count, rc = 0, msgid, sres = LDAP_SUCCESS;
76 char *match = NULL, *err = NULL;
77 char *mapped_filter = NULL, **mapped_attrs = NULL;
81 struct berval mfilter = { 0, NULL };
82 #endif /* ENABLE_REWRITE */
83 struct slap_limits_set *limit = NULL;
85 BerVarray v2refs = NULL;
87 lc = ldap_back_getconn(li, conn, op);
93 * controls are set in ldap_back_dobind()
95 * FIXME: in case of values return filter, we might want
96 * to map attrs and maybe rewrite value
98 if ( !ldap_back_dobind( lc, op ) ) {
102 /* if not root, get appropriate limits */
103 if ( be_isroot( be, &op->o_ndn ) ) {
106 ( void ) get_limits( be, &op->o_ndn, &limit );
109 /* if no time limit requested, rely on remote server limits */
110 /* if requested limit higher than hard limit, abort */
111 if ( !isroot && tlimit > limit->lms_t_hard ) {
112 /* no hard limit means use soft instead */
113 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
114 tlimit = limit->lms_t_soft;
116 /* positive hard limit means abort */
117 } else if ( limit->lms_t_hard > 0 ) {
118 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
119 NULL, NULL, NULL, NULL );
124 /* negative hard limit means no limit */
127 /* if no size limit requested, rely on remote server limits */
128 /* if requested limit higher than hard limit, abort */
129 if ( !isroot && slimit > limit->lms_s_hard ) {
130 /* no hard limit means use soft instead */
131 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
132 slimit = limit->lms_s_soft;
134 /* positive hard limit means abort */
135 } else if ( limit->lms_s_hard > 0 ) {
136 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
137 NULL, NULL, NULL, NULL );
142 /* negative hard limit means no limit */
145 /* should we check return values? */
147 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
149 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&tlimit);
151 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&slimit);
154 * Rewrite the search base, if required
156 #ifdef ENABLE_REWRITE
157 switch ( rewrite_session( li->rwinfo, "searchBase",
158 base->bv_val, conn, &mbase.bv_val ) ) {
159 case REWRITE_REGEXEC_OK:
160 if ( mbase.bv_val == NULL ) {
164 LDAP_LOG( BACK_LDAP, DETAIL1,
165 "[rw] searchBase: \"%s\" -> \"%s\"\n",
166 base->bv_val, mbase.bv_val, 0 );
167 #else /* !NEW_LOGGING */
168 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
169 base->bv_val, mbase.bv_val, "" );
170 #endif /* !NEW_LOGGING */
173 case REWRITE_REGEXEC_UNWILLING:
174 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
175 NULL, "Unwilling to perform", NULL, NULL );
179 case REWRITE_REGEXEC_ERR:
180 send_ldap_result( conn, op, LDAP_OTHER,
181 NULL, "Operations error", NULL, NULL );
187 * Rewrite the search filter, if required
189 switch ( rewrite_session( li->rwinfo, "searchFilter",
190 filterstr->bv_val, conn, &mfilter.bv_val ) ) {
191 case REWRITE_REGEXEC_OK:
192 if ( mfilter.bv_val == NULL || mfilter.bv_val[0] == '\0') {
193 if ( mfilter.bv_val != NULL ) {
194 free( mfilter.bv_val );
196 mfilter = *filterstr;
198 mfilter.bv_len = strlen( mfilter.bv_val );
202 LDAP_LOG( BACK_LDAP, DETAIL1,
203 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
204 filterstr->bv_val, mfilter.bv_val, 0 );
205 #else /* !NEW_LOGGING */
206 Debug( LDAP_DEBUG_ARGS,
207 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
208 filterstr->bv_val, mfilter.bv_val, "" );
209 #endif /* !NEW_LOGGING */
212 case REWRITE_REGEXEC_UNWILLING:
213 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
214 NULL, "Unwilling to perform", NULL, NULL );
215 case REWRITE_REGEXEC_ERR:
219 #else /* !ENABLE_REWRITE */
220 ldap_back_dn_massage( li, base, &mbase, 0, 1 );
221 #endif /* !ENABLE_REWRITE */
223 mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
224 #ifdef ENABLE_REWRITE
226 #else /* !ENABLE_REWRITE */
228 #endif /* !ENABLE_REWRITE */
230 if ( mapped_filter == NULL ) {
231 #ifdef ENABLE_REWRITE
232 mapped_filter = mfilter.bv_val;
233 #else /* !ENABLE_REWRITE */
234 mapped_filter = filterstr->bv_val;
235 #endif /* !ENABLE_REWRITE */
238 #ifdef ENABLE_REWRITE
239 if ( mfilter.bv_val != filterstr->bv_val ) {
240 free( mfilter.bv_val );
242 #endif /* ENABLE_REWRITE */
244 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
245 if ( mapped_attrs == NULL && attrs) {
246 for (count=0; attrs[count].an_name.bv_val; count++);
247 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
248 for (count=0; attrs[count].an_name.bv_val; count++) {
249 mapped_attrs[count] = attrs[count].an_name.bv_val;
251 mapped_attrs[count] = NULL;
254 if ((msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter, mapped_attrs,
258 rc = ldap_back_op_result(lc, op);
262 /* We pull apart the ber result, stuff it into a slapd entry, and
263 * let send_search_entry stuff it back into ber format. Slow & ugly,
264 * but this is necessary for version matching, and for ACL processing.
269 rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
271 /* check for abandon */
273 ldap_abandon(lc->ld, msgid);
281 ldap_pvt_thread_yield();
283 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
284 e = ldap_first_entry(lc->ld,res);
285 if ( ldap_send_entry(be, op, lc, e, attrs, attrsonly)
291 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
292 char **references = NULL;
293 LDAPControl **ctrls = NULL;
297 rc = ldap_parse_reference( lc->ld, res,
298 &references, &ctrls, 1 );
300 if ( rc != LDAP_SUCCESS ) {
304 if ( references == NULL ) {
308 for ( cnt = 0; references[ cnt ]; cnt++ )
311 refs = ch_calloc( cnt + 1, sizeof( struct berval ) );
313 for ( cnt = 0; references[ cnt ]; cnt++ ) {
314 refs[ cnt ].bv_val = references[ cnt ];
315 refs[ cnt ].bv_len = strlen( references[ cnt ] );
318 /* ignore return value by now */
319 ( void )send_search_reference( be, conn, op,
320 NULL, refs, ctrls, &v2refs );
324 ldap_value_free( references );
329 ldap_controls_free( ctrls );
333 sres = ldap_result2error(lc->ld, res, 1);
334 sres = ldap_back_map_result(sres);
335 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
336 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
345 #ifdef ENABLE_REWRITE
347 * Rewrite the matched portion of the search base, if required
349 if ( match != NULL ) {
350 switch ( rewrite_session( li->rwinfo, "matchedDn",
351 match, conn, &mmatch ) ) {
352 case REWRITE_REGEXEC_OK:
353 if ( mmatch == NULL ) {
354 mmatch = ( char * )match;
357 LDAP_LOG( BACK_LDAP, DETAIL1,
358 "[rw] matchedDn:" " \"%s\" -> \"%s\"\n", match, mmatch, 0 );
359 #else /* !NEW_LOGGING */
360 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
361 " \"%s\" -> \"%s\"\n%s",
363 #endif /* !NEW_LOGGING */
366 case REWRITE_REGEXEC_UNWILLING:
367 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
368 NULL, "Unwilling to perform",
371 case REWRITE_REGEXEC_ERR:
378 sres = LDAP_REFERRAL;
380 send_search_result( conn, op, sres,
381 mmatch, err, v2refs, NULL, count );
383 #else /* !ENABLE_REWRITE */
385 sres = LDAP_REFERRAL;
387 send_search_result( conn, op, sres,
388 match, err, v2refs, NULL, count );
389 #endif /* !ENABLE_REWRITE */
393 #ifdef ENABLE_REWRITE
394 if ( mmatch != match ) {
397 #endif /* ENABLE_REWRITE */
403 if ( mapped_attrs ) {
404 ch_free( mapped_attrs );
406 if ( mapped_filter != filterstr->bv_val ) {
407 ch_free( mapped_filter );
409 if ( mbase.bv_val != base->bv_val ) {
410 free( mbase.bv_val );
422 AttributeName *attrs,
426 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
427 struct berval a, mapped;
429 BerElement ber = *e->lm_ber;
430 Attribute *attr, **attrp;
431 struct berval dummy = { 0, NULL };
432 struct berval *bv, bdn;
435 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
436 return LDAP_DECODING_ERROR;
438 #ifdef ENABLE_REWRITE
441 * Rewrite the dn of the result, if needed
443 switch ( rewrite_session( li->rwinfo, "searchResult",
444 bdn.bv_val, lc->conn, &ent.e_name.bv_val ) ) {
445 case REWRITE_REGEXEC_OK:
446 if ( ent.e_name.bv_val == NULL ) {
450 LDAP_LOG( BACK_LDAP, DETAIL1,
451 "[rw] searchResult: \"%s\"" " -> \"%s\"\n",
452 bdn.bv_val, ent.e_dn, 0 );
453 #else /* !NEW_LOGGING */
454 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
455 " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
456 #endif /* !NEW_LOGGING */
457 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
461 case REWRITE_REGEXEC_ERR:
462 case REWRITE_REGEXEC_UNWILLING:
465 #else /* !ENABLE_REWRITE */
466 ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
467 #endif /* !ENABLE_REWRITE */
470 * Note: this may fail if the target host(s) schema differs
471 * from the one known to the meta, and a DN with unknown
472 * attributes is returned.
474 * FIXME: should we log anything, or delegate to dnNormalize2?
476 if ( dnNormalize2( NULL, &ent.e_name, &ent.e_nname ) != LDAP_SUCCESS ) {
477 return LDAP_INVALID_DN_SYNTAX;
483 attrp = &ent.e_attrs;
485 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
486 ldap_back_map(&li->at_map, &a, &mapped, 1);
487 if (mapped.bv_val == NULL)
489 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
495 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
496 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
499 LDAP_LOG( BACK_LDAP, DETAIL1,
500 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
501 #else /* !NEW_LOGGING */
502 Debug( LDAP_DEBUG_ANY,
503 "slap_bv2undef_ad(%s): "
504 "%s\n%s", mapped.bv_val, text, "" );
505 #endif /* !NEW_LOGGING */
511 /* no subschemaSubentry */
512 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
517 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
518 || attr->a_vals == NULL ) {
520 * Note: attr->a_vals can be null when using
521 * values result filter
523 attr->a_vals = &dummy;
525 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
526 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
529 for ( last = 0; attr->a_vals[last].bv_val; last++ ) ;
530 for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
531 ldap_back_map(&li->oc_map, bv, &mapped, 1);
532 if (mapped.bv_val == NULL) {
533 LBER_FREE(bv->bv_val);
537 *bv = attr->a_vals[last];
538 attr->a_vals[last].bv_val = NULL;
540 } else if ( mapped.bv_val != bv->bv_val ) {
542 * FIXME: after LBER_FREEing
543 * the value is replaced by
546 LBER_FREE(bv->bv_val);
547 ber_dupbv( bv, &mapped );
552 * It is necessary to try to rewrite attributes with
553 * dn syntax because they might be used in ACLs as
554 * members of groups; since ACLs are applied to the
555 * rewritten stuff, no dn-based subject clause could
556 * be used at the ldap backend side (see
557 * http://www.OpenLDAP.org/faq/data/cache/452.html)
558 * The problem can be overcome by moving the dn-based
559 * ACLs to the target directory server, and letting
560 * everything pass thru the ldap backend.
562 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
563 SLAPD_DN_SYNTAX ) == 0 ) {
565 for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
566 struct berval newval;
568 #ifdef ENABLE_REWRITE
569 switch ( rewrite_session( li->rwinfo,
574 case REWRITE_REGEXEC_OK:
576 if ( newval.bv_val == NULL ) {
579 newval.bv_len = strlen( newval.bv_val );
581 LDAP_LOG( BACK_LDAP, DETAIL1,
582 "[rw] searchResult on attr=%s: \"%s\" -> \"%s\"\n",
583 attr->a_desc->ad_type->sat_cname.bv_val,
584 bv->bv_val, newval.bv_val );
585 #else /* !NEW_LOGGING */
586 Debug( LDAP_DEBUG_ARGS,
587 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
588 attr->a_desc->ad_type->sat_cname.bv_val,
589 bv->bv_val, newval.bv_val );
590 #endif /* !NEW_LOGGING */
595 case REWRITE_REGEXEC_UNWILLING:
597 case REWRITE_REGEXEC_ERR:
599 * FIXME: better give up,
601 * or leave it untouched?
605 #else /* !ENABLE_REWRITE */
606 ldap_back_dn_massage( li, bv, &newval, 0, 0 );
608 #endif /* !ENABLE_REWRITE */
613 attrp = &attr->a_next;
615 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
616 while (ent.e_attrs) {
618 ent.e_attrs = attr->a_next;
619 if (attr->a_vals != &dummy)
620 ber_bvarray_free(attr->a_vals);
624 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )