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) {
228 for (count=0; attrs[count].an_name.bv_val; count++);
229 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
230 for (count=0; attrs[count].an_name.bv_val; count++) {
231 mapped_attrs[count] = attrs[count].an_name.bv_val;
233 mapped_attrs[count] = NULL;
236 if ((msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter, mapped_attrs,
240 rc = ldap_back_op_result(lc, op);
244 /* We pull apart the ber result, stuff it into a slapd entry, and
245 * let send_search_entry stuff it back into ber format. Slow & ugly,
246 * but this is necessary for version matching, and for ACL processing.
251 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
255 /* check for abandon */
256 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
258 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
261 ldap_abandon(lc->ld, msgid);
268 ldap_pvt_thread_yield();
269 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
270 e = ldap_first_entry(lc->ld,res);
271 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
275 sres = ldap_result2error(lc->ld, res, 1);
276 sres = ldap_back_map_result(sres);
277 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
278 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
287 #ifdef ENABLE_REWRITE
289 * Rewrite the matched portion of the search base, if required
291 if ( match != NULL ) {
292 switch ( rewrite_session( li->rwinfo, "matchedDn",
293 match, conn, &mmatch ) ) {
294 case REWRITE_REGEXEC_OK:
295 if ( mmatch == NULL ) {
296 mmatch = ( char * )match;
299 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
301 " \"%s\" -> \"%s\"\n",
303 #else /* !NEW_LOGGING */
304 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
305 " \"%s\" -> \"%s\"\n%s",
307 #endif /* !NEW_LOGGING */
310 case REWRITE_REGEXEC_UNWILLING:
311 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
312 NULL, "Unwilling to perform",
315 case REWRITE_REGEXEC_ERR:
321 send_search_result( conn, op, sres,
322 mmatch, err, NULL, NULL, count );
324 #else /* !ENABLE_REWRITE */
325 send_search_result( conn, op, sres,
326 match, err, NULL, NULL, count );
327 #endif /* !ENABLE_REWRITE */
331 #ifdef ENABLE_REWRITE
332 if ( mmatch != match ) {
335 #endif /* ENABLE_REWRITE */
341 if ( mapped_attrs ) {
342 free( mapped_attrs );
344 #ifdef ENABLE_REWRITE
345 if ( mapped_filter != mfilter.bv_val ) {
346 free( mapped_filter );
348 if ( mfilter.bv_val != filterstr->bv_val ) {
349 free( mfilter.bv_val );
351 #else /* !ENABLE_REWRITE */
352 if ( mapped_filter != filterstr->bv_val ) {
353 free( mapped_filter );
355 #endif /* !ENABLE_REWRITE */
357 if ( mbase.bv_val != base->bv_val ) {
358 free( mbase.bv_val );
370 AttributeName *attrs,
374 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
375 struct berval a, mapped;
377 BerElement ber = *e->lm_ber;
378 Attribute *attr, **attrp;
379 struct berval dummy = { 0, NULL };
380 struct berval *bv, bdn;
383 if ( ber_scanf( &ber, "{o", &bdn ) == LBER_ERROR ) {
386 #ifdef ENABLE_REWRITE
389 * Rewrite the dn of the result, if needed
391 switch ( rewrite_session( li->rwinfo, "searchResult",
392 bdn.bv_val, lc->conn, &ent.e_name.bv_val ) ) {
393 case REWRITE_REGEXEC_OK:
394 if ( ent.e_name.bv_val == NULL ) {
399 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
400 "[rw] searchResult: \"%s\""
401 " -> \"%s\"\n", bdn.bv_val, ent.e_dn ));
402 #else /* !NEW_LOGGING */
403 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
404 " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
405 #endif /* !NEW_LOGGING */
408 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
412 case REWRITE_REGEXEC_ERR:
413 case REWRITE_REGEXEC_UNWILLING:
416 #else /* !ENABLE_REWRITE */
417 ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
418 #endif /* !ENABLE_REWRITE */
420 dnNormalize2( NULL, &ent.e_name, &ent.e_nname );
424 attrp = &ent.e_attrs;
426 while ( ber_scanf( &ber, "{{o", &a ) != LBER_ERROR ) {
427 ldap_back_map(&li->at_map, &a, &mapped, 1);
428 if (mapped.bv_val == NULL)
430 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
435 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
436 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
439 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
440 "slap_str2undef_ad(%s): "
441 "%s\n", mapped.bv_val, text ));
442 #else /* !NEW_LOGGING */
443 Debug( LDAP_DEBUG_ANY,
444 "slap_str2undef_ad(%s): "
445 "%s\n%s", mapped.bv_val, text, "" );
446 #endif /* !NEW_LOGGING */
452 if (ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR ) {
453 attr->a_vals = &dummy;
454 } else if ( strcasecmp( mapped.bv_val, "objectclass" ) == 0 ) {
456 for ( last = 0; attr->a_vals[last].bv_val; last++ ) ;
457 for ( i = 0; ( bv = &attr->a_vals[i] ); i++ ) {
458 ldap_back_map(&li->oc_map, bv, &mapped, 1);
459 if (mapped.bv_val == NULL) {
460 free(attr->a_vals[i].bv_val);
461 attr->a_vals[i].bv_val = NULL;
464 attr->a_vals[i] = attr->a_vals[last];
465 attr->a_vals[last].bv_val = NULL;
467 } else if ( mapped.bv_val != bv->bv_val ) {
469 ber_dupbv( bv, &mapped );
473 #ifdef ENABLE_REWRITE
475 * It is necessary to try to rewrite attributes with
476 * dn syntax because they might be used in ACLs as
477 * members of groups; since ACLs are applied to the
478 * rewritten stuff, no dn-based subecj clause could
479 * be used at the ldap backend side (see
480 * http://www.OpenLDAP.org/faq/data/cache/452.html)
481 * The problem can be overcome by moving the dn-based
482 * ACLs to the target directory server, and letting
483 * everything pass thru the ldap backend.
485 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
486 SLAPD_DN_SYNTAX ) == 0 ) {
488 for ( i = 0; ( bv = &attr->a_vals[ i ] ); i++ ) {
491 switch ( rewrite_session( li->rwinfo,
494 lc->conn, &newval )) {
495 case REWRITE_REGEXEC_OK:
497 if ( newval == NULL ) {
501 LDAP_LOG(( "backend",
503 "[rw] searchResult on"
505 " \"%s\" -> \"%s\"\n",
506 attr->a_desc->ad_type->sat_cname.bv_val,
507 bv->bv_val, newval ));
508 #else /* !NEW_LOGGING */
509 Debug( LDAP_DEBUG_ARGS,
510 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
511 attr->a_desc->ad_type->sat_cname.bv_val,
512 bv->bv_val, newval );
513 #endif /* !NEW_LOGGING */
517 bv->bv_len = strlen( newval );
521 case REWRITE_REGEXEC_UNWILLING:
523 case REWRITE_REGEXEC_ERR:
525 * FIXME: better give up,
527 * or leave it untouched?
532 #endif /* ENABLE_REWRITE */
536 attrp = &attr->a_next;
538 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
539 while (ent.e_attrs) {
541 ent.e_attrs = attr->a_next;
542 if (attr->a_vals != &dummy)
543 bvarray_free(attr->a_vals);