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"
49 static void ldap_send_entry( Backend *be, Operation *op, struct ldapconn *lc,
50 LDAPMessage *e, char **attrs, int attrsonly );
64 const char *filterstr,
69 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
73 int count, rc = 0, msgid, sres = LDAP_SUCCESS;
74 char *match = NULL, *err = NULL;
75 char *mbase = NULL, *mapped_filter = NULL, **mapped_attrs = NULL;
77 char *mfilter = NULL, *mmatch = NULL;
78 #endif /* ENABLE_REWRITE */
79 struct slap_limits_set *limit = NULL;
82 lc = ldap_back_getconn(li, conn, op);
87 /* if not root, get appropriate limits */
88 if ( be_isroot( be, op->o_ndn ) ) {
91 ( void ) get_limits( be, op->o_ndn, &limit );
94 /* if no time limit requested, rely on remote server limits */
95 /* if requested limit higher than hard limit, abort */
96 if ( !isroot && tlimit > limit->lms_t_hard ) {
97 /* no hard limit means use soft instead */
98 if ( limit->lms_t_hard == 0 ) {
99 tlimit = limit->lms_t_soft;
101 /* positive hard limit means abort */
102 } else if ( limit->lms_t_hard > 0 ) {
103 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
104 NULL, NULL, NULL, NULL, 0 );
109 /* negative hard limit means no limit */
112 /* if no size limit requested, rely on remote server limits */
113 /* if requested limit higher than hard limit, abort */
114 if ( !isroot && slimit > limit->lms_s_hard ) {
115 /* no hard limit means use soft instead */
116 if ( limit->lms_s_hard == 0 ) {
117 slimit = limit->lms_s_soft;
119 /* positive hard limit means abort */
120 } else if ( limit->lms_s_hard > 0 ) {
121 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
122 NULL, NULL, NULL, NULL, 0 );
127 /* negative hard limit means no limit */
131 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
133 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&tlimit);
135 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&slimit);
137 if ( !ldap_back_dobind( lc, op ) ) {
142 * Rewrite the search base, if required
144 #ifdef ENABLE_REWRITE
145 switch ( rewrite_session( li->rwinfo, "searchBase",
146 base, conn, &mbase ) ) {
147 case REWRITE_REGEXEC_OK:
148 if ( mbase == NULL ) {
149 mbase = ( char * )base;
152 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
153 "[rw] searchBase: \"%s\" -> \"%s\"\n%",
155 #else /* !NEW_LOGGING */
156 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
158 #endif /* !NEW_LOGGING */
161 case REWRITE_REGEXEC_UNWILLING:
162 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
163 NULL, "Unwilling to perform", NULL, NULL );
167 case REWRITE_REGEXEC_ERR:
168 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
169 NULL, "Operations error", NULL, NULL );
175 * Rewrite the search filter, if required
177 switch ( rewrite_session( li->rwinfo, "searchFilter",
178 filterstr, conn, &mfilter ) ) {
179 case REWRITE_REGEXEC_OK:
180 if ( mfilter == NULL || mfilter[0] == '\0') {
181 if ( mfilter != NULL ) {
184 mfilter = ( char * )filterstr;
187 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
188 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
189 filterstr, mfilter ));
190 #else /* !NEW_LOGGING */
191 Debug( LDAP_DEBUG_ARGS,
192 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
193 filterstr, mfilter, "" );
194 #endif /* !NEW_LOGGING */
197 case REWRITE_REGEXEC_UNWILLING:
198 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
199 NULL, "Unwilling to perform", NULL, NULL );
200 case REWRITE_REGEXEC_ERR:
204 #else /* !ENABLE_REWRITE */
205 mbase = ldap_back_dn_massage( li, ch_strdup( base ), 0 );
206 #endif /* !ENABLE_REWRITE */
208 mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
209 #ifdef ENABLE_REWRITE
211 #else /* !ENABLE_REWRITE */
213 #endif /* !ENABLE_REWRITE */
215 if ( mapped_filter == NULL ) {
216 #ifdef ENABLE_REWRITE
217 mapped_filter = (char *)mfilter;
218 #else /* !ENABLE_REWRITE */
219 mapped_filter = (char *)filterstr;
220 #endif /* !ENABLE_REWRITE */
223 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
224 if ( mapped_attrs == NULL ) {
225 mapped_attrs = attrs;
228 if ((msgid = ldap_search(lc->ld, mbase, scope, mapped_filter, mapped_attrs,
232 rc = ldap_back_op_result(lc, op);
236 /* We pull apart the ber result, stuff it into a slapd entry, and
237 * let send_search_entry stuff it back into ber format. Slow & ugly,
238 * but this is necessary for version matching, and for ACL processing.
243 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
247 /* check for abandon */
248 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
250 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
253 ldap_abandon(lc->ld, msgid);
260 ldap_pvt_thread_yield();
261 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
262 e = ldap_first_entry(lc->ld,res);
263 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
267 sres = ldap_result2error(lc->ld, res, 1);
268 sres = ldap_back_map_result(sres);
269 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
270 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
279 #ifdef ENABLE_REWRITE
281 * Rewrite the matched portion of the search base, if required
283 if ( match != NULL ) {
284 switch ( rewrite_session( li->rwinfo, "matchedDn",
285 match, conn, &mmatch ) ) {
286 case REWRITE_REGEXEC_OK:
287 if ( mmatch == NULL ) {
288 mmatch = ( char * )match;
291 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
293 " \"%s\" -> \"%s\"\n",
295 #else /* !NEW_LOGGING */
296 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
297 " \"%s\" -> \"%s\"\n%s",
299 #endif /* !NEW_LOGGING */
302 case REWRITE_REGEXEC_UNWILLING:
303 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
304 NULL, "Unwilling to perform",
307 case REWRITE_REGEXEC_ERR:
313 send_search_result( conn, op, sres,
314 mmatch, err, NULL, NULL, count );
316 #else /* !ENABLE_REWRITE */
317 send_search_result( conn, op, sres,
318 match, err, NULL, NULL, count );
319 #endif /* !ENABLE_REWRITE */
323 #ifdef ENABLE_REWRITE
324 if ( mmatch != match ) {
327 #endif /* ENABLE_REWRITE */
333 if ( mapped_attrs != attrs ) {
334 charray_free( mapped_attrs );
336 #ifdef ENABLE_REWRITE
337 if ( mapped_filter != mfilter ) {
338 free( mapped_filter );
340 if ( mfilter != filterstr ) {
343 #else /* !ENABLE_REWRITE */
344 if ( mapped_filter != filterstr ) {
345 free( mapped_filter );
347 #endif /* !ENABLE_REWRITE */
349 #ifdef ENABLE_REWRITE
350 if ( mbase != base ) {
351 #endif /* ENABLE_REWRITE */
353 #ifdef ENABLE_REWRITE
355 #endif /* ENABLE_REWRITE */
370 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
373 BerElement *ber = NULL;
374 Attribute *attr, **attrp;
375 struct berval *dummy = NULL;
379 #ifdef ENABLE_REWRITE
382 dn = ldap_get_dn(lc->ld, e);
388 * Rewrite the dn of the result, if needed
390 switch ( rewrite_session( li->rwinfo, "searchResult",
391 dn, lc->conn, &ent.e_dn ) ) {
392 case REWRITE_REGEXEC_OK:
393 if ( ent.e_dn == NULL ) {
397 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
398 "[rw] searchResult: \"%s\""
399 " -> \"%s\"\n", dn, ent.e_dn ));
400 #else /* !NEW_LOGGING */
401 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
402 " -> \"%s\"\n%s", dn, ent.e_dn, "" );
403 #endif /* !NEW_LOGGING */
409 case REWRITE_REGEXEC_ERR:
410 case REWRITE_REGEXEC_UNWILLING:
414 #else /* !ENABLE_REWRITE */
415 ent.e_dn = ldap_back_dn_restore( li, ldap_get_dn(lc->ld, e), 0 );
416 #endif /* !ENABLE_REWRITE */
418 ent.e_ndn = ch_strdup( ent.e_dn );
419 (void) dn_normalize( ent.e_ndn );
423 attrp = &ent.e_attrs;
425 for ( a = ldap_first_attribute(lc->ld, e, &ber);
427 a = ldap_next_attribute(lc->ld, e, ber))
429 mapped = ldap_back_map(&li->at_map, a, 1);
432 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
437 if (slap_str2ad(mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
438 if (slap_str2undef_ad(mapped, &attr->a_desc, &text)
441 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
442 "slap_str2undef_ad(%s): "
443 "%s\n", mapped, text ));
444 #else /* !NEW_LOGGING */
445 Debug( LDAP_DEBUG_ANY,
446 "slap_str2undef_ad(%s): "
447 "%s\n%s", mapped, text, "" );
448 #endif /* !NEW_LOGGING */
454 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
456 attr->a_vals = &dummy;
457 } else if ( strcasecmp( mapped, "objectclass" ) == 0 ) {
459 for ( last = 0; attr->a_vals[last]; last++ ) ;
460 for ( i = 0; ( bv = attr->a_vals[i] ); i++ ) {
461 mapped = ldap_back_map(&li->oc_map, bv->bv_val, 1);
462 if (mapped == NULL) {
463 ber_bvfree(attr->a_vals[i]);
464 attr->a_vals[i] = NULL;
467 attr->a_vals[i] = attr->a_vals[last];
468 attr->a_vals[last] = NULL;
470 } else if ( mapped != bv->bv_val ) {
472 bv->bv_val = ch_strdup( mapped );
473 bv->bv_len = strlen( 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[ i ] ); 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,
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,
516 bv->bv_val, newval );
517 #endif /* !NEW_LOGGING */
521 bv->bv_len = strlen( newval );
525 case REWRITE_REGEXEC_UNWILLING:
527 case REWRITE_REGEXEC_ERR:
529 * FIXME: better give up,
531 * or leave it untouched?
536 #endif /* ENABLE_REWRITE */
540 attrp = &attr->a_next;
542 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
543 while (ent.e_attrs) {
545 ent.e_attrs = attr->a_next;
546 if (attr->a_vals != &dummy)
547 ber_bvecfree(attr->a_vals);