1 /* search.c - ldap backend search function */
4 * Copyright 1998-1999 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 ) {
152 mbase.bv_val = ( char * )base->bv_val;
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;
190 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
191 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
192 filterstr->bv_val, mfilter.bv_val ));
193 #else /* !NEW_LOGGING */
194 Debug( LDAP_DEBUG_ARGS,
195 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
196 filterstr->bv_val, mfilter.bv_val, "" );
197 #endif /* !NEW_LOGGING */
200 case REWRITE_REGEXEC_UNWILLING:
201 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
202 NULL, "Unwilling to perform", NULL, NULL );
203 case REWRITE_REGEXEC_ERR:
207 #else /* !ENABLE_REWRITE */
208 ldap_back_dn_massage( li, base, &mbase, 0, 1 );
209 #endif /* !ENABLE_REWRITE */
211 mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
212 #ifdef ENABLE_REWRITE
214 #else /* !ENABLE_REWRITE */
216 #endif /* !ENABLE_REWRITE */
218 if ( mapped_filter == NULL ) {
219 #ifdef ENABLE_REWRITE
220 mapped_filter = mfilter.bv_val;
221 #else /* !ENABLE_REWRITE */
222 mapped_filter = filterstr->bv_val;
223 #endif /* !ENABLE_REWRITE */
226 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
227 if ( mapped_attrs == NULL && attrs) {
229 for (count=0, an=attrs; an; an=an->an_next,count++);
230 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
231 for (count=0, an=attrs; an; an=an->an_next,count++) {
232 mapped_attrs[count] = an->an_name.bv_val;
234 mapped_attrs[count] = NULL;
237 if ((msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter, mapped_attrs,
241 rc = ldap_back_op_result(lc, op);
245 /* We pull apart the ber result, stuff it into a slapd entry, and
246 * let send_search_entry stuff it back into ber format. Slow & ugly,
247 * but this is necessary for version matching, and for ACL processing.
252 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
256 /* check for abandon */
257 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
259 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
262 ldap_abandon(lc->ld, msgid);
269 ldap_pvt_thread_yield();
270 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
271 e = ldap_first_entry(lc->ld,res);
272 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
276 sres = ldap_result2error(lc->ld, res, 1);
277 sres = ldap_back_map_result(sres);
278 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
279 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
288 #ifdef ENABLE_REWRITE
290 * Rewrite the matched portion of the search base, if required
292 if ( match != NULL ) {
293 switch ( rewrite_session( li->rwinfo, "matchedDn",
294 match, conn, &mmatch ) ) {
295 case REWRITE_REGEXEC_OK:
296 if ( mmatch == NULL ) {
297 mmatch = ( char * )match;
300 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
302 " \"%s\" -> \"%s\"\n",
304 #else /* !NEW_LOGGING */
305 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
306 " \"%s\" -> \"%s\"\n%s",
308 #endif /* !NEW_LOGGING */
311 case REWRITE_REGEXEC_UNWILLING:
312 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
313 NULL, "Unwilling to perform",
316 case REWRITE_REGEXEC_ERR:
322 send_search_result( conn, op, sres,
323 mmatch, err, NULL, NULL, count );
325 #else /* !ENABLE_REWRITE */
326 send_search_result( conn, op, sres,
327 match, err, NULL, NULL, count );
328 #endif /* !ENABLE_REWRITE */
332 #ifdef ENABLE_REWRITE
333 if ( mmatch != match ) {
336 #endif /* ENABLE_REWRITE */
342 if ( mapped_attrs ) {
343 free( mapped_attrs );
345 #ifdef ENABLE_REWRITE
346 if ( mapped_filter != mfilter.bv_val ) {
347 free( mapped_filter );
349 if ( mfilter.bv_val != filterstr->bv_val ) {
350 free( mfilter.bv_val );
352 #else /* !ENABLE_REWRITE */
353 if ( mapped_filter != filterstr->bv_val ) {
354 free( mapped_filter );
356 #endif /* !ENABLE_REWRITE */
358 if ( mbase.bv_val != base->bv_val ) {
359 free( mbase.bv_val );
371 AttributeName *attrs,
375 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
376 struct berval a, mapped;
378 BerElement ber = *e->lm_ber;
379 Attribute *attr, **attrp;
380 struct berval dummy = { 0, NULL };
381 struct berval *bv, bdn;
384 if ( ber_scanf( &ber, "{o", &bdn ) == LBER_ERROR ) {
387 #ifdef ENABLE_REWRITE
390 * Rewrite the dn of the result, if needed
392 switch ( rewrite_session( li->rwinfo, "searchResult",
393 bdn.bv_val, lc->conn, &ent.e_name.bv_val ) ) {
394 case REWRITE_REGEXEC_OK:
395 if ( ent.e_name.bv_val == NULL ) {
400 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
401 "[rw] searchResult: \"%s\""
402 " -> \"%s\"\n", bdn.bv_val, ent.e_dn ));
403 #else /* !NEW_LOGGING */
404 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
405 " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
406 #endif /* !NEW_LOGGING */
409 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
413 case REWRITE_REGEXEC_ERR:
414 case REWRITE_REGEXEC_UNWILLING:
417 #else /* !ENABLE_REWRITE */
418 ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
419 #endif /* !ENABLE_REWRITE */
421 dnNormalize2( NULL, &ent.e_name, &ent.e_nname );
425 attrp = &ent.e_attrs;
427 while ( ber_scanf( &ber, "{{o", &a ) != LBER_ERROR ) {
428 ldap_back_map(&li->at_map, &a, &mapped, 1);
429 if (mapped.bv_val == NULL)
431 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
436 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
437 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
440 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
441 "slap_str2undef_ad(%s): "
442 "%s\n", mapped.bv_val, text ));
443 #else /* !NEW_LOGGING */
444 Debug( LDAP_DEBUG_ANY,
445 "slap_str2undef_ad(%s): "
446 "%s\n%s", mapped.bv_val, text, "" );
447 #endif /* !NEW_LOGGING */
453 if (ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR ) {
454 attr->a_vals = &dummy;
455 } else if ( strcasecmp( mapped.bv_val, "objectclass" ) == 0 ) {
457 for ( last = 0; attr->a_vals[last].bv_val; last++ ) ;
458 for ( i = 0; ( bv = &attr->a_vals[i] ); i++ ) {
459 ldap_back_map(&li->oc_map, bv, &mapped, 1);
460 if (mapped.bv_val == NULL) {
461 free(attr->a_vals[i].bv_val);
462 attr->a_vals[i].bv_val = NULL;
465 attr->a_vals[i] = attr->a_vals[last];
466 attr->a_vals[last].bv_val = NULL;
468 } else if ( mapped.bv_val != bv->bv_val ) {
470 ber_dupbv( bv, &mapped );
474 #ifdef ENABLE_REWRITE
476 * It is necessary to try to rewrite attributes with
477 * dn syntax because they might be used in ACLs as
478 * members of groups; since ACLs are applied to the
479 * rewritten stuff, no dn-based subecj clause could
480 * be used at the ldap backend side (see
481 * http://www.OpenLDAP.org/faq/data/cache/452.html)
482 * The problem can be overcome by moving the dn-based
483 * ACLs to the target directory server, and letting
484 * everything pass thru the ldap backend.
486 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
487 SLAPD_DN_SYNTAX ) == 0 ) {
489 for ( i = 0; ( bv = &attr->a_vals[ i ] ); i++ ) {
492 switch ( rewrite_session( li->rwinfo,
495 lc->conn, &newval )) {
496 case REWRITE_REGEXEC_OK:
498 if ( newval == NULL ) {
502 LDAP_LOG(( "backend",
504 "[rw] searchResult on"
506 " \"%s\" -> \"%s\"\n",
507 attr->a_desc->ad_type->sat_cname.bv_val,
508 bv->bv_val, newval ));
509 #else /* !NEW_LOGGING */
510 Debug( LDAP_DEBUG_ARGS,
511 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
512 attr->a_desc->ad_type->sat_cname.bv_val,
513 bv->bv_val, newval );
514 #endif /* !NEW_LOGGING */
518 bv->bv_len = strlen( newval );
522 case REWRITE_REGEXEC_UNWILLING:
524 case REWRITE_REGEXEC_ERR:
526 * FIXME: better give up,
528 * or leave it untouched?
533 #endif /* ENABLE_REWRITE */
537 attrp = &attr->a_next;
539 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
540 while (ent.e_attrs) {
542 ent.e_attrs = attr->a_next;
543 if (attr->a_vals != &dummy)
544 bvarray_free(attr->a_vals);