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, struct berval **attrs, int attrsonly );
64 const char *filterstr,
65 struct berval **attrs,
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 *mapped_filter = NULL, **mapped_attrs = NULL;
78 char *mfilter = NULL, *mmatch = NULL;
79 #endif /* ENABLE_REWRITE */
80 struct slap_limits_set *limit = NULL;
83 lc = ldap_back_getconn(li, conn, op);
88 /* if not root, get appropriate limits */
89 if ( be_isroot( be, &op->o_ndn ) ) {
92 ( void ) get_limits( be, &op->o_ndn, &limit );
95 /* if no time limit requested, rely on remote server limits */
96 /* if requested limit higher than hard limit, abort */
97 if ( !isroot && tlimit > limit->lms_t_hard ) {
98 /* no hard limit means use soft instead */
99 if ( limit->lms_t_hard == 0 ) {
100 tlimit = limit->lms_t_soft;
102 /* positive hard limit means abort */
103 } else if ( limit->lms_t_hard > 0 ) {
104 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
105 NULL, NULL, NULL, NULL, 0 );
110 /* negative hard limit means no limit */
113 /* if no size limit requested, rely on remote server limits */
114 /* if requested limit higher than hard limit, abort */
115 if ( !isroot && slimit > limit->lms_s_hard ) {
116 /* no hard limit means use soft instead */
117 if ( limit->lms_s_hard == 0 ) {
118 slimit = limit->lms_s_soft;
120 /* positive hard limit means abort */
121 } else if ( limit->lms_s_hard > 0 ) {
122 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
123 NULL, NULL, NULL, NULL, 0 );
128 /* negative hard limit means no limit */
132 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
134 ldap_set_option( lc->ld, LDAP_OPT_TIMELIMIT, (void *)&tlimit);
136 ldap_set_option( lc->ld, LDAP_OPT_SIZELIMIT, (void *)&slimit);
138 if ( !ldap_back_dobind( lc, op ) ) {
143 * Rewrite the search base, if required
145 #ifdef ENABLE_REWRITE
146 switch ( rewrite_session( li->rwinfo, "searchBase",
147 base->bv_val, conn, &mbase.bv_val ) ) {
148 case REWRITE_REGEXEC_OK:
149 if ( mbase.bv_val == NULL ) {
150 mbase.bv_val = ( char * )base->bv_val;
153 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
154 "[rw] searchBase: \"%s\" -> \"%s\"\n%",
155 base->bv_val, mbase.bv_val ));
156 #else /* !NEW_LOGGING */
157 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
158 base->bv_val, mbase.bv_val, "" );
159 #endif /* !NEW_LOGGING */
162 case REWRITE_REGEXEC_UNWILLING:
163 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
164 NULL, "Unwilling to perform", NULL, NULL );
168 case REWRITE_REGEXEC_ERR:
169 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
170 NULL, "Operations error", NULL, NULL );
176 * Rewrite the search filter, if required
178 switch ( rewrite_session( li->rwinfo, "searchFilter",
179 filterstr, conn, &mfilter ) ) {
180 case REWRITE_REGEXEC_OK:
181 if ( mfilter == NULL || mfilter[0] == '\0') {
182 if ( mfilter != NULL ) {
185 mfilter = ( char * )filterstr;
188 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
189 "[rw] searchFilter: \"%s\" -> \"%s\"\n",
190 filterstr, mfilter ));
191 #else /* !NEW_LOGGING */
192 Debug( LDAP_DEBUG_ARGS,
193 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
194 filterstr, mfilter, "" );
195 #endif /* !NEW_LOGGING */
198 case REWRITE_REGEXEC_UNWILLING:
199 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
200 NULL, "Unwilling to perform", NULL, NULL );
201 case REWRITE_REGEXEC_ERR:
205 #else /* !ENABLE_REWRITE */
206 ldap_back_dn_massage( li, base, &mbase, 0, 1 );
207 #endif /* !ENABLE_REWRITE */
209 mapped_filter = ldap_back_map_filter(&li->at_map, &li->oc_map,
210 #ifdef ENABLE_REWRITE
212 #else /* !ENABLE_REWRITE */
214 #endif /* !ENABLE_REWRITE */
216 if ( mapped_filter == NULL ) {
217 #ifdef ENABLE_REWRITE
218 mapped_filter = (char *)mfilter;
219 #else /* !ENABLE_REWRITE */
220 mapped_filter = (char *)filterstr;
221 #endif /* !ENABLE_REWRITE */
224 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, 0);
225 if ( mapped_attrs == NULL && attrs) {
226 for (count=0; attrs[count]; count++);
227 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
228 for (count=0; attrs[count]; count++) {
229 mapped_attrs[count] = attrs[count]->bv_val;
231 mapped_attrs[count] = NULL;
234 if ((msgid = ldap_search(lc->ld, mbase.bv_val, scope, mapped_filter, mapped_attrs,
238 rc = ldap_back_op_result(lc, op);
242 /* We pull apart the ber result, stuff it into a slapd entry, and
243 * let send_search_entry stuff it back into ber format. Slow & ugly,
244 * but this is necessary for version matching, and for ACL processing.
249 rc = ldap_result(lc->ld, LDAP_RES_ANY, 0, &tv, &res))
253 /* check for abandon */
254 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
256 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
259 ldap_abandon(lc->ld, msgid);
266 ldap_pvt_thread_yield();
267 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
268 e = ldap_first_entry(lc->ld,res);
269 ldap_send_entry(be, op, lc, e, attrs, attrsonly);
273 sres = ldap_result2error(lc->ld, res, 1);
274 sres = ldap_back_map_result(sres);
275 ldap_get_option(lc->ld, LDAP_OPT_ERROR_STRING, &err);
276 ldap_get_option(lc->ld, LDAP_OPT_MATCHED_DN, &match);
285 #ifdef ENABLE_REWRITE
287 * Rewrite the matched portion of the search base, if required
289 if ( match != NULL ) {
290 switch ( rewrite_session( li->rwinfo, "matchedDn",
291 match, conn, &mmatch ) ) {
292 case REWRITE_REGEXEC_OK:
293 if ( mmatch == NULL ) {
294 mmatch = ( char * )match;
297 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
299 " \"%s\" -> \"%s\"\n",
301 #else /* !NEW_LOGGING */
302 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
303 " \"%s\" -> \"%s\"\n%s",
305 #endif /* !NEW_LOGGING */
308 case REWRITE_REGEXEC_UNWILLING:
309 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
310 NULL, "Unwilling to perform",
313 case REWRITE_REGEXEC_ERR:
319 send_search_result( conn, op, sres,
320 mmatch, err, NULL, NULL, count );
322 #else /* !ENABLE_REWRITE */
323 send_search_result( conn, op, sres,
324 match, err, NULL, NULL, count );
325 #endif /* !ENABLE_REWRITE */
329 #ifdef ENABLE_REWRITE
330 if ( mmatch != match ) {
333 #endif /* ENABLE_REWRITE */
339 if ( mapped_attrs ) {
340 free( mapped_attrs );
342 #ifdef ENABLE_REWRITE
343 if ( mapped_filter != mfilter ) {
344 free( mapped_filter );
346 if ( mfilter != filterstr ) {
349 #else /* !ENABLE_REWRITE */
350 if ( mapped_filter != filterstr ) {
351 free( mapped_filter );
353 #endif /* !ENABLE_REWRITE */
355 if ( mbase.bv_val != base->bv_val ) {
356 free( mbase.bv_val );
368 struct berval **attrs,
372 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
375 BerElement *ber = NULL;
376 Attribute *attr, **attrp;
377 struct berval *dummy = NULL;
378 struct berval *bv, bdn;
381 #ifdef ENABLE_REWRITE
384 dn = ldap_get_dn(lc->ld, e);
390 * Rewrite the dn of the result, if needed
392 switch ( rewrite_session( li->rwinfo, "searchResult",
393 dn, lc->conn, &ent.e_dn ) ) {
394 case REWRITE_REGEXEC_OK:
395 if ( ent.e_dn == NULL ) {
399 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
400 "[rw] searchResult: \"%s\""
401 " -> \"%s\"\n", dn, ent.e_dn ));
402 #else /* !NEW_LOGGING */
403 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
404 " -> \"%s\"\n%s", dn, ent.e_dn, "" );
405 #endif /* !NEW_LOGGING */
411 case REWRITE_REGEXEC_ERR:
412 case REWRITE_REGEXEC_UNWILLING:
416 #else /* !ENABLE_REWRITE */
417 ber_str2bv( ldap_get_dn(lc->ld, e), 0, 0, &bdn );
418 ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
419 #endif /* !ENABLE_REWRITE */
422 dnNormalize( NULL, &ent.e_name, &bv );
427 attrp = &ent.e_attrs;
429 for ( a = ldap_first_attribute(lc->ld, e, &ber);
431 a = ldap_next_attribute(lc->ld, e, ber))
433 mapped = ldap_back_map(&li->at_map, a, 1);
436 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
441 if (slap_str2ad(mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
442 if (slap_str2undef_ad(mapped, &attr->a_desc, &text)
445 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
446 "slap_str2undef_ad(%s): "
447 "%s\n", mapped, text ));
448 #else /* !NEW_LOGGING */
449 Debug( LDAP_DEBUG_ANY,
450 "slap_str2undef_ad(%s): "
451 "%s\n%s", mapped, text, "" );
452 #endif /* !NEW_LOGGING */
458 attr->a_vals = ldap_get_values_len(lc->ld, e, a);
460 attr->a_vals = &dummy;
461 } else if ( strcasecmp( mapped, "objectclass" ) == 0 ) {
463 for ( last = 0; attr->a_vals[last]; last++ ) ;
464 for ( i = 0; ( bv = attr->a_vals[i] ); i++ ) {
465 mapped = ldap_back_map(&li->oc_map, bv->bv_val, 1);
466 if (mapped == NULL) {
467 ber_bvfree(attr->a_vals[i]);
468 attr->a_vals[i] = NULL;
471 attr->a_vals[i] = attr->a_vals[last];
472 attr->a_vals[last] = NULL;
474 } else if ( mapped != bv->bv_val ) {
476 ber_str2bv( mapped, 0, 1, bv );
480 #ifdef ENABLE_REWRITE
482 * It is necessary to try to rewrite attributes with
483 * dn syntax because they might be used in ACLs as
484 * members of groups; since ACLs are applied to the
485 * rewritten stuff, no dn-based subecj clause could
486 * be used at the ldap backend side (see
487 * http://www.OpenLDAP.org/faq/data/cache/452.html)
488 * The problem can be overcome by moving the dn-based
489 * ACLs to the target directory server, and letting
490 * everything pass thru the ldap backend.
492 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
493 SLAPD_DN_SYNTAX ) == 0 ) {
495 for ( i = 0; ( bv = attr->a_vals[ i ] ); i++ ) {
498 switch ( rewrite_session( li->rwinfo,
501 lc->conn, &newval )) {
502 case REWRITE_REGEXEC_OK:
504 if ( newval == NULL ) {
508 LDAP_LOG(( "backend",
510 "[rw] searchResult on"
512 " \"%s\" -> \"%s\"\n",
513 attr->a_desc->ad_type->sat_cname.bv_val,
514 bv->bv_val, newval ));
515 #else /* !NEW_LOGGING */
516 Debug( LDAP_DEBUG_ARGS,
517 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
518 attr->a_desc->ad_type->sat_cname.bv_val,
519 bv->bv_val, newval );
520 #endif /* !NEW_LOGGING */
524 bv->bv_len = strlen( newval );
528 case REWRITE_REGEXEC_UNWILLING:
530 case REWRITE_REGEXEC_ERR:
532 * FIXME: better give up,
534 * or leave it untouched?
539 #endif /* ENABLE_REWRITE */
543 attrp = &attr->a_next;
545 send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
546 while (ent.e_attrs) {
548 ent.e_attrs = attr->a_next;
549 if (attr->a_vals != &dummy)
550 ber_bvecfree(attr->a_vals);