1 /* search.c - ldap backend search function */
4 * Copyright 1998-2003 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 #undef ldap_debug /* silence a warning in ldap-int.h */
49 #include "../../../libraries/libldap/ldap-int.h"
51 static int ldap_send_entry( Backend *be, Operation *op, Connection *conn,
52 LDAPMessage *e, AttributeName *attrs, int attrsonly );
66 struct berval *filterstr,
71 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
75 int count, rc = 0, msgid, sres = LDAP_SUCCESS;
76 char *match = NULL, *err = NULL;
77 char **mapped_attrs = NULL;
80 struct berval mfilter = { 0, NULL };
81 struct slap_limits_set *limit = NULL;
83 BerVarray v2refs = NULL;
85 lc = ldap_back_getconn(li, conn, op);
91 * FIXME: in case of values return filter, we might want
92 * to map attrs and maybe rewrite value
94 if ( !ldap_back_dobind( li, lc, conn, op ) ) {
98 /* if not root, get appropriate limits */
99 if ( be_isroot( be, &op->o_ndn ) ) {
102 ( void ) get_limits( be, &op->o_ndn, &limit );
105 /* if no time limit requested, rely on remote server limits */
106 /* if requested limit higher than hard limit, abort */
107 if ( !isroot && tlimit > limit->lms_t_hard ) {
108 /* no hard limit means use soft instead */
109 if ( limit->lms_t_hard == 0
110 && limit->lms_t_soft > -1
111 && tlimit > limit->lms_t_soft ) {
112 tlimit = limit->lms_t_soft;
114 /* positive hard limit means abort */
115 } else if ( limit->lms_t_hard > 0 ) {
116 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
117 NULL, NULL, NULL, NULL );
122 /* negative hard limit means no limit */
125 /* if no size limit requested, rely on remote server limits */
126 /* if requested limit higher than hard limit, abort */
127 if ( !isroot && slimit > limit->lms_s_hard ) {
128 /* no hard limit means use soft instead */
129 if ( limit->lms_s_hard == 0
130 && limit->lms_s_soft > -1
131 && slimit > limit->lms_s_soft ) {
132 slimit = limit->lms_s_soft;
134 /* positive hard limit means abort */
135 } else if ( limit->lms_s_hard > 0 ) {
136 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
137 NULL, NULL, NULL, NULL );
142 /* negative hard limit means no limit */
145 /* should we check return values? */
147 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&deref);
156 * Rewrite the search base, if required
158 #ifdef ENABLE_REWRITE
159 switch ( rewrite_session( li->rwinfo, "searchBase",
160 base->bv_val, conn, &mbase.bv_val ) ) {
161 case REWRITE_REGEXEC_OK:
162 if ( mbase.bv_val == NULL ) {
166 LDAP_LOG( BACK_LDAP, DETAIL1,
167 "[rw] searchBase: \"%s\" -> \"%s\"\n",
168 base->bv_val, mbase.bv_val, 0 );
169 #else /* !NEW_LOGGING */
170 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
171 base->bv_val, mbase.bv_val, "" );
172 #endif /* !NEW_LOGGING */
175 case REWRITE_REGEXEC_UNWILLING:
176 send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
177 NULL, "Operation not allowed", NULL, NULL );
181 case REWRITE_REGEXEC_ERR:
182 send_ldap_result( conn, op, LDAP_OTHER,
183 NULL, "Rewrite error", NULL, NULL );
188 #else /* !ENABLE_REWRITE */
189 ldap_back_dn_massage( li, base, &mbase, 0, 1 );
190 #endif /* !ENABLE_REWRITE */
192 #ifdef ENABLE_REWRITE
193 rc = ldap_back_filter_map_rewrite_( li->rwinfo, conn,
194 &li->at_map, &li->oc_map, filter, &mfilter,
196 #else /* ! ENABLE_REWRITE */
197 rc = ldap_back_filter_map_rewrite_( &li->at_map, &li->oc_map,
198 filter, &mfilter, BACKLDAP_MAP );
199 #endif /* ! ENABLE_REWRITE */
206 mapped_attrs = ldap_back_map_attrs(&li->at_map, attrs, BACKLDAP_MAP);
207 if ( mapped_attrs == NULL && attrs) {
208 for (count=0; attrs[count].an_name.bv_val; count++);
209 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
210 for (count=0; attrs[count].an_name.bv_val; count++) {
211 mapped_attrs[count] = attrs[count].an_name.bv_val;
213 mapped_attrs[count] = NULL;
216 rc = ldap_search_ext(lc->ld, mbase.bv_val, scope, mfilter.bv_val,
217 mapped_attrs, attrsonly, op->o_ctrls, NULL, tv.tv_sec ? &tv
218 : NULL, slimit, &msgid);
219 if ( rc != LDAP_SUCCESS ) {
221 rc = ldap_back_op_result(li, lc, conn, op, msgid, rc, 0);
225 /* We pull apart the ber result, stuff it into a slapd entry, and
226 * let send_search_entry stuff it back into ber format. Slow & ugly,
227 * but this is necessary for version matching, and for ACL processing.
232 rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
234 /* check for abandon */
236 ldap_abandon(lc->ld, msgid);
244 ldap_pvt_thread_yield();
246 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
247 e = ldap_first_entry(lc->ld,res);
248 if ( ldap_send_entry(be, op, conn, e, attrs, attrsonly)
254 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
255 char **references = NULL;
256 LDAPControl **ctrls = NULL;
260 rc = ldap_parse_reference( lc->ld, res,
261 &references, &ctrls, 1 );
263 if ( rc != LDAP_SUCCESS ) {
267 if ( references == NULL ) {
271 for ( cnt = 0; references[ cnt ]; cnt++ )
274 refs = ch_calloc( cnt + 1, sizeof( struct berval ) );
276 for ( cnt = 0; references[ cnt ]; cnt++ ) {
277 refs[ cnt ].bv_val = references[ cnt ];
278 refs[ cnt ].bv_len = strlen( references[ cnt ] );
281 /* ignore return value by now */
282 ( void )send_search_reference( be, conn, op,
283 NULL, refs, ctrls, &v2refs );
287 ldap_value_free( references );
292 ldap_controls_free( ctrls );
296 rc = ldap_parse_result(lc->ld, res, &sres, &match,
297 &err, NULL, NULL, 1);
298 if (rc != LDAP_SUCCESS ) sres = rc;
299 sres = ldap_back_map_result(sres);
308 #ifdef ENABLE_REWRITE
310 * Rewrite the matched portion of the search base, if required
312 if ( match != NULL ) {
313 switch ( rewrite_session( li->rwinfo, "matchedDn",
314 match, conn, &mmatch ) ) {
315 case REWRITE_REGEXEC_OK:
316 if ( mmatch == NULL ) {
317 mmatch = ( char * )match;
320 LDAP_LOG( BACK_LDAP, DETAIL1,
321 "[rw] matchedDn:" " \"%s\" -> \"%s\"\n", match, mmatch, 0 );
322 #else /* !NEW_LOGGING */
323 Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
324 " \"%s\" -> \"%s\"\n%s",
326 #endif /* !NEW_LOGGING */
329 case REWRITE_REGEXEC_UNWILLING:
331 case REWRITE_REGEXEC_ERR:
332 /* FIXME: no error, but no matched ... */
339 sres = LDAP_REFERRAL;
342 send_search_result( conn, op, sres,
343 mmatch, err, v2refs, NULL, count );
345 #else /* !ENABLE_REWRITE */
346 if ( match != NULL ) {
347 struct berval dn, mdn;
349 ber_str2bv(match, 0, 0, &dn);
350 ldap_back_dn_massage(li, &dn, &mdn, 0, 0);
355 sres = LDAP_REFERRAL;
357 send_search_result( conn, op, sres,
358 mmatch, err, v2refs, NULL, count );
359 #endif /* !ENABLE_REWRITE */
363 if ( mmatch != match ) {
371 if ( mapped_attrs ) {
372 ch_free( mapped_attrs );
374 if ( mfilter.bv_val != filterstr->bv_val ) {
375 ch_free( mfilter.bv_val );
377 if ( mbase.bv_val != base->bv_val ) {
378 free( mbase.bv_val );
390 AttributeName *attrs,
394 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
395 struct berval a, mapped;
397 BerElement ber = *e->lm_ber;
398 Attribute *attr, **attrp;
399 struct berval dummy = { 0, NULL };
400 struct berval *bv, bdn;
403 if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
404 return LDAP_DECODING_ERROR;
406 #ifdef ENABLE_REWRITE
409 * Rewrite the dn of the result, if needed
411 switch ( rewrite_session( li->rwinfo, "searchResult",
412 bdn.bv_val, conn, &ent.e_name.bv_val ) ) {
413 case REWRITE_REGEXEC_OK:
414 if ( ent.e_name.bv_val == NULL ) {
418 LDAP_LOG( BACK_LDAP, DETAIL1,
419 "[rw] searchResult: \"%s\"" " -> \"%s\"\n",
420 bdn.bv_val, ent.e_dn, 0 );
421 #else /* !NEW_LOGGING */
422 Debug( LDAP_DEBUG_ARGS, "rw> searchResult: \"%s\""
423 " -> \"%s\"\n%s", bdn.bv_val, ent.e_dn, "" );
424 #endif /* !NEW_LOGGING */
425 ent.e_name.bv_len = strlen( ent.e_name.bv_val );
429 case REWRITE_REGEXEC_ERR:
430 case REWRITE_REGEXEC_UNWILLING:
433 #else /* !ENABLE_REWRITE */
434 ldap_back_dn_massage( li, &bdn, &ent.e_name, 0, 0 );
435 #endif /* !ENABLE_REWRITE */
438 * Note: this may fail if the target host(s) schema differs
439 * from the one known to the meta, and a DN with unknown
440 * attributes is returned.
442 * FIXME: should we log anything, or delegate to dnNormalize2?
444 if ( dnNormalize2( NULL, &ent.e_name, &ent.e_nname ) != LDAP_SUCCESS ) {
445 return LDAP_INVALID_DN_SYNTAX;
451 attrp = &ent.e_attrs;
453 while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
454 ldap_back_map(&li->at_map, &a, &mapped, BACKLDAP_REMAP);
455 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
457 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
463 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
464 if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text)
467 LDAP_LOG( BACK_LDAP, DETAIL1,
468 "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
469 #else /* !NEW_LOGGING */
470 Debug( LDAP_DEBUG_ANY,
471 "slap_bv2undef_ad(%s): "
472 "%s\n%s", mapped.bv_val, text, "" );
473 #endif /* !NEW_LOGGING */
479 /* no subschemaSubentry */
480 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
485 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
486 || attr->a_vals == NULL ) {
488 * Note: attr->a_vals can be null when using
489 * values result filter
491 attr->a_vals = &dummy;
493 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
494 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
497 for ( last = 0; attr->a_vals[last].bv_val; last++ );
499 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
500 ldap_back_map(&li->oc_map, bv, &mapped,
502 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
503 LBER_FREE(bv->bv_val);
507 *bv = attr->a_vals[last];
508 attr->a_vals[last].bv_val = NULL;
511 } else if ( mapped.bv_val != bv->bv_val ) {
513 * FIXME: after LBER_FREEing
514 * the value is replaced by
517 LBER_FREE(bv->bv_val);
518 ber_dupbv( bv, &mapped );
523 * It is necessary to try to rewrite attributes with
524 * dn syntax because they might be used in ACLs as
525 * members of groups; since ACLs are applied to the
526 * rewritten stuff, no dn-based subject clause could
527 * be used at the ldap backend side (see
528 * http://www.OpenLDAP.org/faq/data/cache/452.html)
529 * The problem can be overcome by moving the dn-based
530 * ACLs to the target directory server, and letting
531 * everything pass thru the ldap backend.
533 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
534 SLAPD_DN_SYNTAX ) == 0 ) {
537 for ( last = 0; attr->a_vals[last].bv_val; last++ );
539 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
540 struct berval newval;
542 #ifdef ENABLE_REWRITE
543 switch ( rewrite_session( li->rwinfo,
548 case REWRITE_REGEXEC_OK:
550 if ( newval.bv_val == NULL ) {
553 newval.bv_len = strlen( newval.bv_val );
555 LDAP_LOG( BACK_LDAP, DETAIL1,
556 "[rw] searchResult on attr=%s: \"%s\" -> \"%s\"\n",
557 attr->a_desc->ad_type->sat_cname.bv_val,
558 bv->bv_val, newval.bv_val );
559 #else /* !NEW_LOGGING */
560 Debug( LDAP_DEBUG_ARGS,
561 "rw> searchResult on attr=%s: \"%s\" -> \"%s\"\n",
562 attr->a_desc->ad_type->sat_cname.bv_val,
563 bv->bv_val, newval.bv_val );
564 #endif /* !NEW_LOGGING */
569 case REWRITE_REGEXEC_UNWILLING:
570 LBER_FREE(bv->bv_val);
574 *bv = attr->a_vals[last];
575 attr->a_vals[last].bv_val = NULL;
579 case REWRITE_REGEXEC_ERR:
581 * FIXME: better give up,
583 * or leave it untouched?
587 #else /* !ENABLE_REWRITE */
588 ldap_back_dn_massage( li, bv, &newval, 0, 0 );
589 if ( bv->bv_val != newval.bv_val ) {
590 LBER_FREE( bv->bv_val );
593 #endif /* !ENABLE_REWRITE */
600 attr->a_nvals = attr->a_vals;
603 attrp = &attr->a_next;
605 send_search_entry( be, conn, op, &ent, attrs, attrsonly, NULL );
606 while (ent.e_attrs) {
608 ent.e_attrs = attr->a_next;
609 if (attr->a_vals != &dummy)
610 ber_bvarray_free(attr->a_vals);
614 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )