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 void 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;
86 lc = ldap_back_getconn(li, conn, op);
91 /* if not root, get appropriate limits */
92 if ( be_isroot( be, &op->o_ndn ) ) {
95 ( void ) get_limits( be, &op->o_ndn, &limit );
98 /* if no time limit requested, rely on remote server limits */
99 /* if requested limit higher than hard limit, abort */
100 if ( !isroot && tlimit > limit->lms_t_hard ) {
101 /* no hard limit means use soft instead */
102 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
103 tlimit = limit->lms_t_soft;
105 /* positive hard limit means abort */
106 } else if ( limit->lms_t_hard > 0 ) {
107 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
108 NULL, NULL, NULL, NULL, 0 );
113 /* negative hard limit means no limit */
116 /* if no size limit requested, rely on remote server limits */
117 /* if requested limit higher than hard limit, abort */
118 if ( !isroot && slimit > limit->lms_s_hard ) {
119 /* no hard limit means use soft instead */
120 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
121 slimit = limit->lms_s_soft;
123 /* positive hard limit means abort */
124 } else if ( limit->lms_s_hard > 0 ) {
125 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
126 NULL, NULL, NULL, NULL, 0 );
131 /* negative hard limit means no limit */
135 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
137 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&tlimit);
139 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&slimit);
141 if ( !ldap_back_dobind( lc, op ) ) {
146 * Rewrite the search base, if required
148 #ifdef ENABLE_REWRITE
149 switch ( rewrite_session( li->rwinfo, "searchBase",
150 base->bv_val, conn, &mbase.bv_val ) ) {
151 case REWRITE_REGEXEC_OK:
152 if ( mbase.bv_val == NULL ) {
156 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
157 "[rw] searchBase: \"%s\" -> \"%s\"\n%",
158 base->bv_val, mbase.bv_val ));
159 #else /* !NEW_LOGGING */
160 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
161 base->bv_val, mbase.bv_val, "" );
162 #endif /* !NEW_LOGGING */
165 case REWRITE_REGEXEC_UNWILLING:
166 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
167 NULL, "Unwilling to perform", NULL, NULL );
171 case REWRITE_REGEXEC_ERR:
172 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
173 NULL, "Operations error", NULL, NULL );
179 * Rewrite the search filter, if required
181 switch ( rewrite_session( li->rwinfo, "searchFilter",
182 filterstr->bv_val, conn, &mfilter.bv_val ) ) {
183 case REWRITE_REGEXEC_OK:
184 if ( mfilter.bv_val == NULL || mfilter.bv_val[0] == '\0') {
185 if ( mfilter.bv_val != NULL ) {
186 free( mfilter.bv_val );
188 mfilter = *filterstr;
190 mfilter.bv_len = strlen( mfilter.bv_val );
194 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
195 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
196 filterstr->bv_val, mfilter.bv_val ));
197 #else /* !NEW_LOGGING */
198 Debug( LDAP_DEBUG_ARGS,
199 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
200 filterstr->bv_val, mfilter.bv_val, "" );
201 #endif /* !NEW_LOGGING */
204 case REWRITE_REGEXEC_UNWILLING:
205 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
206 NULL, "Unwilling to perform", NULL, NULL );
207 case REWRITE_REGEXEC_ERR:
211 #else /* !ENABLE_REWRITE */
212 ldap_back_dn_massage( li, base, &mbase, 0, 1 );
213 #endif /* !ENABLE_REWRITE */
215 mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
216 #ifdef ENABLE_REWRITE
218 #else /* !ENABLE_REWRITE */
220 #endif /* !ENABLE_REWRITE */
222 if ( mapped_filter == NULL ) {
223 #ifdef ENABLE_REWRITE
224 mapped_filter = mfilter.bv_val;
225 #else /* !ENABLE_REWRITE */
226 mapped_filter = filterstr->bv_val;
227 #endif /* !ENABLE_REWRITE */
230 #ifdef ENABLE_REWRITE
231 if ( mfilter.bv_val != filterstr->bv_val ) {
232 free( mfilter.bv_val );
234 #endif /* ENABLE_REWRITE */
236 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
237 if ( mapped_attrs == NULL && attrs) {
238 for (count=0; attrs[count].an_name.bv_val; count++);
239 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
240 for (count=0; attrs[count].an_name.bv_val; count++) {
241 mapped_attrs[count] = attrs[count].an_name.bv_val;
243 mapped_attrs[count] = NULL;
246 if ((msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter, mapped_attrs,
250 rc = ldap_back_op_result(lc, op);
254 /* We pull apart the ber result, stuff it into a slapd entry, and
255 * let send_search_entry stuff it back into ber format. Slow & ugly,
256 * but this is necessary for version matching, and for ACL processing.
261 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
263 /* check for abandon */
265 ldap_abandon(lc->ld, msgid);
272 ldap_pvt_thread_yield();
273 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
274 e = ldap_first_entry(lc->ld,res);
275 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
279 sres = ldap_result2error(lc->ld, res, 1);
280 sres = ldap_back_map_result(sres);
281 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
282 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
291 #ifdef ENABLE_REWRITE
293 * Rewrite the matched portion of the search base, if required
295 if ( match != NULL ) {
296 switch ( rewrite_session( li->rwinfo, "matchedDn",
297 match, conn, &mmatch ) ) {
298 case REWRITE_REGEXEC_OK:
299 if ( mmatch == NULL ) {
300 mmatch = ( char * )match;
303 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
305 " \"%s\" -> \"%s\"\n",
307 #else /* !NEW_LOGGING */
308 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
309 " \"%s\" -> \"%s\"\n%s",
311 #endif /* !NEW_LOGGING */
314 case REWRITE_REGEXEC_UNWILLING:
315 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
316 NULL, "Unwilling to perform",
319 case REWRITE_REGEXEC_ERR:
325 send_search_result( conn, op, sres,
326 mmatch, err, NULL, NULL, count );
328 #else /* !ENABLE_REWRITE */
329 send_search_result( conn, op, sres,
330 match, err, NULL, NULL, count );
331 #endif /* !ENABLE_REWRITE */
335 #ifdef ENABLE_REWRITE
336 if ( mmatch != match ) {
339 #endif /* ENABLE_REWRITE */
345 if ( mapped_attrs ) {
346 ch_free( mapped_attrs );
348 if ( mapped_filter != filterstr->bv_val ) {
349 ch_free( mapped_filter );
351 if ( mbase.bv_val != base->bv_val ) {
352 free( mbase.bv_val );
364 AttributeName *attrs,
368 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
369 struct berval a, mapped;
371 BerElement ber = *e->lm_ber;
372 Attribute *attr, **attrp;
373 struct berval dummy = { 0, NULL };
374 struct berval *bv, bdn;
377 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
380 #ifdef ENABLE_REWRITE
383 * Rewrite the dn of the result, if needed
385 switch ( rewrite_session( li->rwinfo, "searchResult",
386 bdn.bv_val, lc->conn, &ent.e_name.bv_val ) ) {
387 case REWRITE_REGEXEC_OK:
388 if ( ent.e_name.bv_val == NULL ) {
392 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
393 "[rw] searchResult: \"%s\""
394 " -> \"%s\"\n", bdn.bv_val, ent.e_dn ));
395 #else /* !NEW_LOGGING */
396 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
397 " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
398 #endif /* !NEW_LOGGING */
399 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
403 case REWRITE_REGEXEC_ERR:
404 case REWRITE_REGEXEC_UNWILLING:
407 #else /* !ENABLE_REWRITE */
408 ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
409 #endif /* !ENABLE_REWRITE */
411 dnNormalize2( NULL, &ent.e_name, &ent.e_nname );
415 attrp = &ent.e_attrs;
417 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
418 ldap_back_map(&li->at_map, &a, &mapped, 1);
419 if (mapped.bv_val == NULL)
421 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
427 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
428 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
431 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
432 "slap_bv2undef_ad(%s): "
433 "%s\n", mapped.bv_val, text ));
434 #else /* !NEW_LOGGING */
435 Debug( LDAP_DEBUG_ANY,
436 "slap_bv2undef_ad(%s): "
437 "%s\n%s", mapped.bv_val, text, "" );
438 #endif /* !NEW_LOGGING */
444 /* no subschemaSubentry */
445 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
450 if (ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR ) {
451 attr->a_vals = &dummy;
452 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
453 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
455 for ( last = 0; attr->a_vals[last].bv_val; last++ ) ;
456 for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
457 ldap_back_map(&li->oc_map, bv, &mapped, 1);
458 if (mapped.bv_val == NULL) {
459 LBER_FREE(bv->bv_val);
463 *bv = attr->a_vals[last];
464 attr->a_vals[last].bv_val = NULL;
466 } else if ( mapped.bv_val != bv->bv_val ) {
468 * FIXME: after LBER_FREEing
469 * the value is replaced by
472 LBER_FREE(bv->bv_val);
473 ber_dupbv( bv, &mapped );
477 #ifdef ENABLE_REWRITE
479 * It is necessary to try to rewrite attributes with
480 * dn syntax because they might be used in ACLs as
481 * members of groups; since ACLs are applied to the
482 * rewritten stuff, no dn-based subecj clause could
483 * be used at the ldap backend side (see
484 * http://www.OpenLDAP.org/faq/data/cache/452.html)
485 * The problem can be overcome by moving the dn-based
486 * ACLs to the target directory server, and letting
487 * everything pass thru the ldap backend.
489 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
490 SLAPD_DN_SYNTAX ) == 0 ) {
492 for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
495 switch ( rewrite_session( li->rwinfo,
498 lc->conn, &newval )) {
499 case REWRITE_REGEXEC_OK:
501 if ( newval == NULL ) {
505 LDAP_LOG(( "backend",
507 "[rw] searchResult on"
509 " \"%s\" -> \"%s\"\n",
510 attr->a_desc->ad_type->sat_cname.bv_val,
511 bv->bv_val, newval ));
512 #else /* !NEW_LOGGING */
513 Debug( LDAP_DEBUG_ARGS,
514 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
515 attr->a_desc->ad_type->sat_cname.bv_val,
516 bv->bv_val, newval );
517 #endif /* !NEW_LOGGING */
520 bv->bv_len = strlen( newval );
524 case REWRITE_REGEXEC_UNWILLING:
526 case REWRITE_REGEXEC_ERR:
528 * FIXME: better give up,
530 * or leave it untouched?
535 #endif /* ENABLE_REWRITE */
539 attrp = &attr->a_next;
541 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
542 while (ent.e_attrs) {
544 ent.e_attrs = attr->a_next;
545 if (attr->a_vals != &dummy)
546 ber_bvarray_free(attr->a_vals);
550 if ( ent.e_dn && ent.e_dn != bdn.bv_val )