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 #include "../../../libraries/libldap/ldap-int.h"
50 static void ldap_send_entry( Backend *be, Operation *op, struct ldapconn *lc,
51 LDAPMessage *e, AttributeName *attrs, int attrsonly );
65 struct berval *filterstr,
70 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
74 int count, rc = 0, msgid, sres = LDAP_SUCCESS;
75 char *match = NULL, *err = NULL;
76 char *mapped_filter = NULL, **mapped_attrs = NULL;
80 struct berval mfilter = { 0, NULL };
81 #endif /* ENABLE_REWRITE */
82 struct slap_limits_set *limit = NULL;
85 lc = ldap_back_getconn(li, conn, op);
90 /* if not root, get appropriate limits */
91 if ( be_isroot( be, &op->o_ndn ) ) {
94 ( void ) get_limits( be, &op->o_ndn, &limit );
97 /* if no time limit requested, rely on remote server limits */
98 /* if requested limit higher than hard limit, abort */
99 if ( !isroot && tlimit > limit->lms_t_hard ) {
100 /* no hard limit means use soft instead */
101 if ( limit->lms_t_hard == 0 ) {
102 tlimit = limit->lms_t_soft;
104 /* positive hard limit means abort */
105 } else if ( limit->lms_t_hard > 0 ) {
106 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
107 NULL, NULL, NULL, NULL, 0 );
112 /* negative hard limit means no limit */
115 /* if no size limit requested, rely on remote server limits */
116 /* if requested limit higher than hard limit, abort */
117 if ( !isroot && slimit > limit->lms_s_hard ) {
118 /* no hard limit means use soft instead */
119 if ( limit->lms_s_hard == 0 ) {
120 slimit = limit->lms_s_soft;
122 /* positive hard limit means abort */
123 } else if ( limit->lms_s_hard > 0 ) {
124 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
125 NULL, NULL, NULL, NULL, 0 );
130 /* negative hard limit means no limit */
134 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
136 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&tlimit);
138 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&slimit);
140 if ( !ldap_back_dobind( lc, op ) ) {
145 * Rewrite the search base, if required
147 #ifdef ENABLE_REWRITE
148 switch ( rewrite_session( li->rwinfo, "searchBase",
149 base->bv_val, conn, &mbase.bv_val ) ) {
150 case REWRITE_REGEXEC_OK:
151 if ( mbase.bv_val == NULL ) {
155 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
156 "[rw] searchBase: \"%s\" -> \"%s\"\n%",
157 base->bv_val, mbase.bv_val ));
158 #else /* !NEW_LOGGING */
159 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
160 base->bv_val, mbase.bv_val, "" );
161 #endif /* !NEW_LOGGING */
164 case REWRITE_REGEXEC_UNWILLING:
165 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
166 NULL, "Unwilling to perform", NULL, NULL );
170 case REWRITE_REGEXEC_ERR:
171 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
172 NULL, "Operations error", NULL, NULL );
178 * Rewrite the search filter, if required
180 switch ( rewrite_session( li->rwinfo, "searchFilter",
181 filterstr->bv_val, conn, &mfilter.bv_val ) ) {
182 case REWRITE_REGEXEC_OK:
183 if ( mfilter.bv_val == NULL || mfilter.bv_val[0] == '\0') {
184 if ( mfilter.bv_val != NULL ) {
185 free( mfilter.bv_val );
187 mfilter = *filterstr;
189 mfilter.bv_len = strlen( mfilter.bv_val );
193 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
194 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
195 filterstr->bv_val, mfilter.bv_val ));
196 #else /* !NEW_LOGGING */
197 Debug( LDAP_DEBUG_ARGS,
198 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
199 filterstr->bv_val, mfilter.bv_val, "" );
200 #endif /* !NEW_LOGGING */
203 case REWRITE_REGEXEC_UNWILLING:
204 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
205 NULL, "Unwilling to perform", NULL, NULL );
206 case REWRITE_REGEXEC_ERR:
210 #else /* !ENABLE_REWRITE */
211 ldap_back_dn_massage( li, base, &mbase, 0, 1 );
212 #endif /* !ENABLE_REWRITE */
214 mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
215 #ifdef ENABLE_REWRITE
217 #else /* !ENABLE_REWRITE */
219 #endif /* !ENABLE_REWRITE */
221 if ( mapped_filter == NULL ) {
222 #ifdef ENABLE_REWRITE
223 mapped_filter = mfilter.bv_val;
224 #else /* !ENABLE_REWRITE */
225 mapped_filter = filterstr->bv_val;
226 #endif /* !ENABLE_REWRITE */
229 #ifdef ENABLE_REWRITE
230 if ( mfilter.bv_val != filterstr->bv_val ) {
231 ldap_memfree( mfilter.bv_val );
233 #endif /* ENABLE_REWRITE */
235 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
236 if ( mapped_attrs == NULL && attrs) {
237 for (count=0; attrs[count].an_name.bv_val; count++);
238 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
239 for (count=0; attrs[count].an_name.bv_val; count++) {
240 mapped_attrs[count] = attrs[count].an_name.bv_val;
242 mapped_attrs[count] = NULL;
245 if ((msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter, mapped_attrs,
249 rc = ldap_back_op_result(lc, op);
253 /* We pull apart the ber result, stuff it into a slapd entry, and
254 * let send_search_entry stuff it back into ber format. Slow & ugly,
255 * but this is necessary for version matching, and for ACL processing.
260 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
264 /* check for abandon */
265 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
267 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
270 ldap_abandon(lc->ld, msgid);
277 ldap_pvt_thread_yield();
278 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
279 e = ldap_first_entry(lc->ld,res);
280 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
284 sres = ldap_result2error(lc->ld, res, 1);
285 sres = ldap_back_map_result(sres);
286 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
287 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
296 #ifdef ENABLE_REWRITE
298 * Rewrite the matched portion of the search base, if required
300 if ( match != NULL ) {
301 switch ( rewrite_session( li->rwinfo, "matchedDn",
302 match, conn, &mmatch ) ) {
303 case REWRITE_REGEXEC_OK:
304 if ( mmatch == NULL ) {
305 mmatch = ( char * )match;
308 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
310 " \"%s\" -> \"%s\"\n",
312 #else /* !NEW_LOGGING */
313 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
314 " \"%s\" -> \"%s\"\n%s",
316 #endif /* !NEW_LOGGING */
319 case REWRITE_REGEXEC_UNWILLING:
320 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
321 NULL, "Unwilling to perform",
324 case REWRITE_REGEXEC_ERR:
330 send_search_result( conn, op, sres,
331 mmatch, err, NULL, NULL, count );
333 #else /* !ENABLE_REWRITE */
334 send_search_result( conn, op, sres,
335 match, err, NULL, NULL, count );
336 #endif /* !ENABLE_REWRITE */
340 #ifdef ENABLE_REWRITE
341 if ( mmatch != match ) {
344 #endif /* ENABLE_REWRITE */
350 if ( mapped_attrs ) {
351 free( mapped_attrs );
353 if ( mapped_filter != filterstr->bv_val ) {
354 free( mapped_filter );
356 if ( mbase.bv_val != base->bv_val ) {
357 free( mbase.bv_val );
369 AttributeName *attrs,
373 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
374 struct berval a, mapped;
376 BerElement ber = *e->lm_ber;
377 Attribute *attr, **attrp;
378 struct berval dummy = { 0, NULL };
379 struct berval *bv, bdn;
382 if ( ber_scanf( &ber, "{o{", &bdn ) == LBER_ERROR ) {
385 #ifdef ENABLE_REWRITE
388 * Rewrite the dn of the result, if needed
390 switch ( rewrite_session( li->rwinfo, "searchResult",
391 bdn.bv_val, lc->conn, &ent.e_name.bv_val ) ) {
392 case REWRITE_REGEXEC_OK:
393 if ( ent.e_name.bv_val == NULL ) {
398 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
399 "[rw] searchResult: \"%s\""
400 " -> \"%s\"\n", bdn.bv_val, ent.e_dn ));
401 #else /* !NEW_LOGGING */
402 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
403 " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
404 #endif /* !NEW_LOGGING */
407 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
411 case REWRITE_REGEXEC_ERR:
412 case REWRITE_REGEXEC_UNWILLING:
415 #else /* !ENABLE_REWRITE */
416 ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
417 #endif /* !ENABLE_REWRITE */
419 dnNormalize2( NULL, &ent.e_name, &ent.e_nname );
423 attrp = &ent.e_attrs;
425 while ( ber_scanf( &ber, "{o", &a ) != LBER_ERROR ) {
426 ldap_back_map(&li->at_map, &a, &mapped, 1);
427 if ( mapped.bv_val != a.bv_val) {
430 if (mapped.bv_val == NULL)
432 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
437 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
438 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
441 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
442 "slap_bv2undef_ad(%s): "
443 "%s\n", mapped.bv_val, text ));
444 #else /* !NEW_LOGGING */
445 Debug( LDAP_DEBUG_ANY,
446 "slap_bv2undef_ad(%s): "
447 "%s\n%s", mapped.bv_val, text, "" );
448 #endif /* !NEW_LOGGING */
454 /* no subschemaSubentry */
455 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
460 if (ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR ) {
461 attr->a_vals = &dummy;
462 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
463 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
465 for ( last = 0; attr->a_vals[last].bv_val; last++ ) ;
466 for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
467 ldap_back_map(&li->oc_map, bv, &mapped, 1);
468 if (mapped.bv_val == NULL) {
473 *bv = attr->a_vals[last];
474 attr->a_vals[last].bv_val = NULL;
476 } else if ( mapped.bv_val != bv->bv_val ) {
478 ber_dupbv( bv, &mapped );
482 #ifdef ENABLE_REWRITE
484 * It is necessary to try to rewrite attributes with
485 * dn syntax because they might be used in ACLs as
486 * members of groups; since ACLs are applied to the
487 * rewritten stuff, no dn-based subecj clause could
488 * be used at the ldap backend side (see
489 * http://www.OpenLDAP.org/faq/data/cache/452.html)
490 * The problem can be overcome by moving the dn-based
491 * ACLs to the target directory server, and letting
492 * everything pass thru the ldap backend.
494 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
495 SLAPD_DN_SYNTAX ) == 0 ) {
497 for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
500 switch ( rewrite_session( li->rwinfo,
503 lc->conn, &newval )) {
504 case REWRITE_REGEXEC_OK:
506 if ( newval == NULL ) {
510 LDAP_LOG(( "backend",
512 "[rw] searchResult on"
514 " \"%s\" -> \"%s\"\n",
515 attr->a_desc->ad_type->sat_cname.bv_val,
516 bv->bv_val, newval ));
517 #else /* !NEW_LOGGING */
518 Debug( LDAP_DEBUG_ARGS,
519 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
520 attr->a_desc->ad_type->sat_cname.bv_val,
521 bv->bv_val, newval );
522 #endif /* !NEW_LOGGING */
526 bv->bv_len = strlen( newval );
530 case REWRITE_REGEXEC_UNWILLING:
532 case REWRITE_REGEXEC_ERR:
534 * FIXME: better give up,
536 * or leave it untouched?
541 #endif /* ENABLE_REWRITE */
545 attrp = &attr->a_next;
547 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
548 while (ent.e_attrs) {
550 ent.e_attrs = attr->a_next;
551 if (attr->a_vals != &dummy)
552 bvarray_free(attr->a_vals);