2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Dmitry Kovalev for inclusion
18 * by OpenLDAP Software.
26 #include <sys/types.h>
27 #include "ac/string.h"
32 #include "proto-sql.h"
34 #define BACKSQL_STOP 0
35 #define BACKSQL_CONTINUE 1
37 static int backsql_process_filter( backsql_srch_info *bsi, Filter *f );
38 static int backsql_process_filter_eq( backsql_srch_info *bsi,
39 backsql_at_map_rec *at,
40 int casefold, struct berval *filter_value );
41 static int backsql_process_filter_like( backsql_srch_info *bsi,
42 backsql_at_map_rec *at,
43 int casefold, struct berval *filter_value );
44 static int backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f,
45 backsql_at_map_rec *at );
48 backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad )
51 AttributeName *an = NULL;
53 if ( bsi->bsi_attrs == NULL ) {
58 * clear the list (retrieve all attrs)
61 ch_free( bsi->bsi_attrs );
62 bsi->bsi_attrs = NULL;
66 for ( ; bsi->bsi_attrs[ n_attrs ].an_name.bv_val; n_attrs++ ) {
67 an = &bsi->bsi_attrs[ n_attrs ];
69 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
70 "attribute '%s' is in list\n",
71 an->an_name.bv_val, 0, 0 );
73 * We can live with strcmp because the attribute
74 * list has been normalized before calling be_search
76 if ( !BACKSQL_NCMP( &an->an_name, &ad->ad_cname ) ) {
81 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
82 "adding '%s' to list\n", ad->ad_cname.bv_val, 0, 0 );
84 an = (AttributeName *)ch_realloc( bsi->bsi_attrs,
85 sizeof( AttributeName ) * ( n_attrs + 2 ) );
90 an[ n_attrs ].an_name = ad->ad_cname;
91 an[ n_attrs ].an_desc = ad;
92 an[ n_attrs + 1 ].an_name.bv_val = NULL;
93 an[ n_attrs + 1 ].an_name.bv_len = 0;
102 backsql_srch_info *bsi,
111 AttributeName *attrs )
115 bsi->bsi_base_dn = base;
116 bsi->bsi_scope = scope;
117 bsi->bsi_slimit = slimit;
118 bsi->bsi_tlimit = tlimit;
119 bsi->bsi_filter = filter;
127 if ( attrs == NULL || an_find( attrs, &AllUser ) ) {
128 bsi->bsi_attrs = NULL;
131 bsi->bsi_attrs = (AttributeName *)ch_calloc( 1,
132 sizeof( AttributeName ) );
133 bsi->bsi_attrs[ 0 ].an_name.bv_val = NULL;
134 bsi->bsi_attrs[ 0 ].an_name.bv_len = 0;
136 for ( p = attrs; p->an_name.bv_val; p++ ) {
138 * ignore "1.1"; handle "+"
140 if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
141 bsi->bsi_flags |= BSQL_SF_ALL_OPER;
144 } else if ( BACKSQL_NCMP( &p->an_name, &NoAttrs ) == 0 ) {
148 backsql_attrlist_add( bsi, p->an_desc );
152 bsi->bsi_abandon = 0;
153 bsi->bsi_id_list = NULL;
154 bsi->bsi_n_candidates = 0;
155 bsi->bsi_stoptime = stoptime;
156 bsi->bsi_sel.bb_val.bv_val = NULL;
157 bsi->bsi_sel.bb_val.bv_len = 0;
158 bsi->bsi_sel.bb_len = 0;
159 bsi->bsi_from.bb_val.bv_val = NULL;
160 bsi->bsi_from.bb_val.bv_len = 0;
161 bsi->bsi_from.bb_len = 0;
162 bsi->bsi_join_where.bb_val.bv_val = NULL;
163 bsi->bsi_join_where.bb_val.bv_len = 0;
164 bsi->bsi_join_where.bb_len = 0;
165 bsi->bsi_flt_where.bb_val.bv_val = NULL;
166 bsi->bsi_flt_where.bb_val.bv_len = 0;
167 bsi->bsi_flt_where.bb_len = 0;
168 bsi->bsi_filter_oc = NULL;
170 bsi->bsi_status = LDAP_SUCCESS;
174 backsql_process_filter_list( backsql_srch_info *bsi, Filter *f, int op )
182 backsql_strfcat( &bsi->bsi_flt_where, "c", '(' /* ) */ );
185 res = backsql_process_filter( bsi, f );
188 * TimesTen : If the query has no answers,
189 * don't bother to run the query.
200 case LDAP_FILTER_AND:
201 backsql_strfcat( &bsi->bsi_flt_where, "l",
202 (ber_len_t)sizeof( " AND " ) - 1,
207 backsql_strfcat( &bsi->bsi_flt_where, "l",
208 (ber_len_t)sizeof( " OR " ) - 1,
214 backsql_strfcat( &bsi->bsi_flt_where, "c", /* ( */ ')' );
220 backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f,
221 backsql_at_map_rec *at )
223 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
231 /* always uppercase strings by now */
232 #ifdef BACKSQL_UPPERCASE_FILTER
233 if ( SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
234 bi->bi_caseIgnoreMatch ) )
235 #endif /* BACKSQL_UPPERCASE_FILTER */
240 if ( SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
241 bi->bi_telephoneNumberMatch ) )
248 * to check for matching telephone numbers
249 * with intermized chars, e.g. val='1234'
252 * val LIKE '%1%2%3%4%'
256 if ( f->f_sub_initial.bv_val ) {
257 bv.bv_len += f->f_sub_initial.bv_len;
259 if ( f->f_sub_any != NULL ) {
260 for ( a = 0; f->f_sub_any[ a ].bv_val != NULL; a++ ) {
261 bv.bv_len += f->f_sub_any[ a ].bv_len;
264 if ( f->f_sub_final.bv_val ) {
265 bv.bv_len += f->f_sub_final.bv_len;
267 bv.bv_len = 2 * bv.bv_len - 1;
268 bv.bv_val = ch_malloc( bv.bv_len + 1 );
271 if ( f->f_sub_initial.bv_val ) {
272 bv.bv_val[ s ] = f->f_sub_initial.bv_val[ 0 ];
273 for ( i = 1; i < f->f_sub_initial.bv_len; i++ ) {
274 bv.bv_val[ s + 2 * i - 1 ] = '%';
275 bv.bv_val[ s + 2 * i ] = f->f_sub_initial.bv_val[ i ];
277 bv.bv_val[ s + 2 * i - 1 ] = '%';
281 if ( f->f_sub_any != NULL ) {
282 for ( a = 0; f->f_sub_any[ a ].bv_val != NULL; a++ ) {
283 bv.bv_val[ s ] = f->f_sub_any[ a ].bv_val[ 0 ];
284 for ( i = 1; i < f->f_sub_any[ a ].bv_len; i++ ) {
285 bv.bv_val[ s + 2 * i - 1 ] = '%';
286 bv.bv_val[ s + 2 * i ] = f->f_sub_any[ a ].bv_val[ i ];
288 bv.bv_val[ s + 2 * i - 1 ] = '%';
293 if ( f->f_sub_final.bv_val ) {
294 bv.bv_val[ s ] = f->f_sub_final.bv_val[ 0 ];
295 for ( i = 1; i < f->f_sub_final.bv_len; i++ ) {
296 bv.bv_val[ s + 2 * i - 1 ] = '%';
297 bv.bv_val[ s + 2 * i ] = f->f_sub_final.bv_val[ i ];
299 bv.bv_val[ s + 2 * i - 1 ] = '%';
303 bv.bv_val[ s - 1 ] = '\0';
305 (void)backsql_process_filter_like( bsi, at, casefold, &bv );
306 ch_free( bv.bv_val );
312 * When dealing with case-sensitive strings
313 * we may omit normalization; however, normalized
314 * SQL filters are more liberal.
317 backsql_strfcat( &bsi->bsi_flt_where, "c", '(' /* ) */ );
320 Debug( LDAP_DEBUG_TRACE, "expr: '%s%s%s'\n", at->bam_sel_expr.bv_val,
321 at->bam_sel_expr_u.bv_val ? "' '" : "",
322 at->bam_sel_expr_u.bv_val ? at->bam_sel_expr_u.bv_val : "" );
323 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
325 * If a pre-upper-cased version of the column
326 * or a precompiled upper function exists, use it
328 backsql_strfcat( &bsi->bsi_flt_where,
331 (ber_len_t)sizeof( " LIKE '" ) - 1,
335 backsql_strfcat( &bsi->bsi_flt_where, "bl",
337 (ber_len_t)sizeof( " LIKE '" ) - 1, " LIKE '" );
340 if ( f->f_sub_initial.bv_val != NULL ) {
343 start = bsi->bsi_flt_where.bb_val.bv_len;
344 backsql_strfcat( &bsi->bsi_flt_where, "b",
346 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
347 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
351 backsql_strfcat( &bsi->bsi_flt_where, "c", '%' );
353 if ( f->f_sub_any != NULL ) {
354 for ( i = 0; f->f_sub_any[ i ].bv_val != NULL; i++ ) {
358 Debug( LDAP_DEBUG_TRACE,
359 "==>backsql_process_sub_filter(%s): "
360 "sub_any='%s'\n", at->bam_ad->ad_cname.bv_val,
361 f->f_sub_any[ i ].bv_val, 0 );
362 #endif /* BACKSQL_TRACE */
364 start = bsi->bsi_flt_where.bb_val.bv_len;
365 backsql_strfcat( &bsi->bsi_flt_where,
369 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
371 * Note: toupper('%') = '%'
373 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
377 if ( f->f_sub_final.bv_val != NULL ) {
380 start = bsi->bsi_flt_where.bb_val.bv_len;
381 backsql_strfcat( &bsi->bsi_flt_where, "b",
383 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
384 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
389 backsql_strfcat( &bsi->bsi_flt_where, "l",
390 (ber_len_t)sizeof( /* (' */ "')" ) - 1, /* ( */ "')" );
396 backsql_process_filter( backsql_srch_info *bsi, Filter *f )
398 backsql_at_map_rec **vat = NULL;
399 AttributeDescription *ad = NULL;
404 Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n", 0, 0, 0 );
405 if ( f->f_choice == SLAPD_FILTER_COMPUTED ) {
406 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
407 "invalid filter\n", 0, 0, 0 );
412 switch( f->f_choice ) {
414 rc = backsql_process_filter_list( bsi, f->f_or,
419 case LDAP_FILTER_AND:
420 rc = backsql_process_filter_list( bsi, f->f_and,
425 case LDAP_FILTER_NOT:
426 backsql_strfcat( &bsi->bsi_flt_where, "l",
427 (ber_len_t)sizeof( "NOT (" /* ) */ ) - 1,
429 rc = backsql_process_filter( bsi, f->f_not );
430 backsql_strfcat( &bsi->bsi_flt_where, "c", /* ( */ ')' );
434 case LDAP_FILTER_PRESENT:
438 case LDAP_FILTER_EXT:
439 ad = f->f_mra->ma_desc;
440 if ( f->f_mr_dnattrs ) {
442 * if dn attrs filtering is requested, better return
443 * success and let test_filter() deal with candidate
444 * selection; otherwise we'd need to set conditions
445 * on the contents of the DN, e.g. "SELECT ... FROM
446 * ldap_entries AS attributeName WHERE attributeName.dn
447 * like '%attributeName=value%'"
449 backsql_strfcat( &bsi->bsi_flt_where, "l",
450 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
451 bsi->bsi_status = LDAP_SUCCESS;
472 * Turn structuralObjectClass into objectClass
474 if ( ad == slap_schema.si_ad_objectClass
475 || ad == slap_schema.si_ad_structuralObjectClass ) {
477 * If the filter is LDAP_FILTER_PRESENT, then it's done;
478 * otherwise, let's see if we are lucky: filtering
479 * for "structural" objectclass or ancestor...
481 switch ( f->f_choice ) {
482 case LDAP_FILTER_EQUALITY:
484 ObjectClass *oc = oc_bvfind( &f->f_av_value );
487 Debug( LDAP_DEBUG_TRACE,
488 "backsql_process_filter(): "
489 "unknown objectClass \"%s\" "
491 f->f_av_value.bv_val, 0, 0 );
492 bsi->bsi_status = LDAP_OTHER;
498 * "structural" objectClass inheritance:
499 * - a search for "person" will also return
501 * - a search for "top" will return everything
503 if ( is_object_subclass( oc, bsi->bsi_oc->bom_oc ) ) {
504 goto filter_oc_success;
510 case LDAP_FILTER_PRESENT:
512 backsql_strfcat( &bsi->bsi_flt_where, "l",
513 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
514 bsi->bsi_status = LDAP_SUCCESS;
519 Debug( LDAP_DEBUG_TRACE,
520 "backsql_process_filter(): "
521 "illegal/unhandled filter "
522 "on objectClass attribute",
524 bsi->bsi_status = LDAP_OTHER;
529 } else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) {
531 * FIXME: this is not robust; e.g. a filter
532 * '(!(hasSubordinates=TRUE))' fails because
533 * in SQL it would read 'NOT (1=1)' instead
535 * Note however that hasSubordinates is boolean,
536 * so a more appropriate filter would be
537 * '(hasSubordinates=FALSE)'
539 * A more robust search for hasSubordinates
540 * would * require joining the ldap_entries table
541 * selecting if there are descendants of the
544 backsql_strfcat( &bsi->bsi_flt_where, "l",
545 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
546 if ( ad == slap_schema.si_ad_hasSubordinates ) {
548 * instruct candidate selection algorithm
549 * and attribute list to try to detect
550 * if an entry has subordinates
552 bsi->bsi_flags |= BSQL_SF_FILTER_HASSUBORDINATE;
556 * clear attributes to fetch, to require ALL
557 * and try extended match on all attributes
559 backsql_attrlist_add( bsi, NULL );
566 * attribute inheritance:
568 if ( backsql_supad2at( bsi->bsi_oc, ad, &vat ) ) {
569 bsi->bsi_status = LDAP_OTHER;
575 /* search anyway; other parts of the filter
577 backsql_strfcat( &bsi->bsi_flt_where, "l",
578 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
579 bsi->bsi_status = LDAP_SUCCESS;
584 /* if required, open extra level of parens */
586 if ( vat[0]->bam_next || vat[1] ) {
587 backsql_strfcat( &bsi->bsi_flt_where, "c", '(' );
594 if ( backsql_process_filter_attr( bsi, f, vat[i] ) == -1 ) {
598 /* if more definitions of the same attr, apply */
599 if ( vat[i]->bam_next ) {
600 backsql_strfcat( &bsi->bsi_flt_where, "l",
601 sizeof( " OR " ) - 1, " OR " );
602 vat[i] = vat[i]->bam_next;
606 /* if more descendants of the same attr, apply */
609 backsql_strfcat( &bsi->bsi_flt_where, "l",
610 sizeof( " OR " ) - 1, " OR " );
614 /* if needed, close extra level of parens */
616 backsql_strfcat( &bsi->bsi_flt_where, "c", ')' );
626 Debug( LDAP_DEBUG_TRACE,
627 "<==backsql_process_filter() %s\n",
628 rc == 1 ? "succeeded" : "failed", 0, 0);
634 backsql_process_filter_eq( backsql_srch_info *bsi, backsql_at_map_rec *at,
635 int casefold, struct berval *filter_value )
638 * maybe we should check type of at->sel_expr here somehow,
639 * to know whether upper_func is applicable, but for now
640 * upper_func stuff is made for Oracle, where UPPER is
641 * safely applicable to NUMBER etc.
643 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
646 backsql_strfcat( &bsi->bsi_flt_where, "cbl",
649 (ber_len_t)sizeof( "='" ) - 1,
652 start = bsi->bsi_flt_where.bb_val.bv_len;
654 backsql_strfcat( &bsi->bsi_flt_where, "bl",
656 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
659 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
662 backsql_strfcat( &bsi->bsi_flt_where, "cblbl",
665 (ber_len_t)sizeof( "='" ) - 1, "='",
667 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
675 backsql_process_filter_like( backsql_srch_info *bsi, backsql_at_map_rec *at,
676 int casefold, struct berval *filter_value )
679 * maybe we should check type of at->sel_expr here somehow,
680 * to know whether upper_func is applicable, but for now
681 * upper_func stuff is made for Oracle, where UPPER is
682 * safely applicable to NUMBER etc.
684 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
687 backsql_strfcat( &bsi->bsi_flt_where, "cbl",
690 (ber_len_t)sizeof( " LIKE '%" ) - 1,
693 start = bsi->bsi_flt_where.bb_val.bv_len;
695 backsql_strfcat( &bsi->bsi_flt_where, "bl",
697 (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
700 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
703 backsql_strfcat( &bsi->bsi_flt_where, "cblbl",
706 (ber_len_t)sizeof( " LIKE '%" ) - 1,
709 (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
717 backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, backsql_at_map_rec *at )
719 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
721 struct berval *filter_value = NULL;
722 MatchingRule *matching_rule = NULL;
723 struct berval ordering = BER_BVC("<=");
725 Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter_attr(%s)\n",
726 at->bam_ad->ad_cname.bv_val, 0, 0 );
728 backsql_merge_from_clause( &bsi->bsi_from, &at->bam_from_tbls );
731 * need to add this attribute to list of attrs to load,
732 * so that we can do test_filter() later
734 backsql_attrlist_add( bsi, at->bam_ad );
736 if ( at->bam_join_where.bv_val != NULL
737 && strstr( bsi->bsi_join_where.bb_val.bv_val, at->bam_join_where.bv_val ) == NULL ) {
738 backsql_strfcat( &bsi->bsi_join_where, "lb",
739 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
740 &at->bam_join_where );
743 switch ( f->f_choice ) {
744 case LDAP_FILTER_EQUALITY:
745 filter_value = &f->f_av_value;
746 matching_rule = at->bam_ad->ad_type->sat_equality;
750 /* fail over into next case */
752 case LDAP_FILTER_EXT:
753 filter_value = &f->f_mra->ma_value;
754 matching_rule = f->f_mr_rule;
757 /* always uppercase strings by now */
758 #ifdef BACKSQL_UPPERCASE_FILTER
759 if ( SLAP_MR_ASSOCIATED( matching_rule,
760 bi->bi_caseIgnoreMatch ) )
761 #endif /* BACKSQL_UPPERCASE_FILTER */
766 if ( SLAP_MR_ASSOCIATED( matching_rule,
767 bi->bi_telephoneNumberMatch ) )
773 * to check for matching telephone numbers
774 * with intermized chars, e.g. val='1234'
777 * val LIKE '%1%2%3%4%'
780 bv.bv_len = 2 * filter_value->bv_len - 1;
781 bv.bv_val = ch_malloc( bv.bv_len + 1 );
783 bv.bv_val[ 0 ] = filter_value->bv_val[ 0 ];
784 for ( i = 1; i < filter_value->bv_len; i++ ) {
785 bv.bv_val[ 2 * i - 1 ] = '%';
786 bv.bv_val[ 2 * i ] = filter_value->bv_val[ i ];
788 bv.bv_val[ 2 * i - 1 ] = '\0';
790 (void)backsql_process_filter_like( bsi, at, casefold, &bv );
791 ch_free( bv.bv_val );
797 * maybe we should check type of at->sel_expr here somehow,
798 * to know whether upper_func is applicable, but for now
799 * upper_func stuff is made for Oracle, where UPPER is
800 * safely applicable to NUMBER etc.
802 (void)backsql_process_filter_eq( bsi, at, casefold, filter_value );
806 ordering.bv_val = ">=";
808 /* fall thru to next case */
811 /* always uppercase strings by now */
812 #ifdef BACKSQL_UPPERCASE_FILTER
813 if ( SLAP_MR_ASSOCIATED( at->bam_ad->ad_type->sat_ordering,
814 bi->bi_caseIgnoreMatch ) )
815 #endif /* BACKSQL_UPPERCASE_FILTER */
821 * FIXME: should we uppercase the operands?
823 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
826 backsql_strfcat( &bsi->bsi_flt_where, "cbbc",
832 start = bsi->bsi_flt_where.bb_val.bv_len;
834 backsql_strfcat( &bsi->bsi_flt_where, "bl",
836 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
839 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
842 backsql_strfcat( &bsi->bsi_flt_where, "cbbcbl",
848 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
853 case LDAP_FILTER_PRESENT:
854 backsql_strfcat( &bsi->bsi_flt_where, "lbl",
855 (ber_len_t)sizeof( "NOT (" /* ) */) - 1,
858 (ber_len_t)sizeof( /* ( */ " IS NULL)" ) - 1,
859 /* ( */ " IS NULL)" );
862 case LDAP_FILTER_SUBSTRINGS:
863 backsql_process_sub_filter( bsi, f, at );
866 case LDAP_FILTER_APPROX:
870 * maybe we should check type of at->sel_expr here somehow,
871 * to know whether upper_func is applicable, but for now
872 * upper_func stuff is made for Oracle, where UPPER is
873 * safely applicable to NUMBER etc.
875 (void)backsql_process_filter_like( bsi, at, 1, &f->f_av_value );
879 /* unhandled filter type; should not happen */
881 backsql_strfcat( &bsi->bsi_flt_where, "l",
882 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
887 Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter_attr(%s)\n",
888 at->bam_ad->ad_cname.bv_val, 0, 0 );
894 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
896 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
900 query->bv_val = NULL;
903 Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
904 bsi->bsi_sel.bb_val.bv_val = NULL;
905 bsi->bsi_sel.bb_val.bv_len = 0;
906 bsi->bsi_sel.bb_len = 0;
907 bsi->bsi_from.bb_val.bv_val = NULL;
908 bsi->bsi_from.bb_val.bv_len = 0;
909 bsi->bsi_from.bb_len = 0;
910 bsi->bsi_join_where.bb_val.bv_val = NULL;
911 bsi->bsi_join_where.bb_val.bv_len = 0;
912 bsi->bsi_join_where.bb_len = 0;
913 bsi->bsi_flt_where.bb_val.bv_val = NULL;
914 bsi->bsi_flt_where.bb_val.bv_len = 0;
915 bsi->bsi_flt_where.bb_len = 0;
917 backsql_strfcat( &bsi->bsi_sel, "lbcbc",
918 (ber_len_t)sizeof( "SELECT DISTINCT ldap_entries.id," ) - 1,
919 "SELECT DISTINCT ldap_entries.id,",
920 &bsi->bsi_oc->bom_keytbl,
922 &bsi->bsi_oc->bom_keycol,
925 if ( bi->strcast_func.bv_val ) {
926 backsql_strfcat( &bsi->bsi_sel, "blbl",
928 (ber_len_t)sizeof( "('" /* ') */ ) - 1,
930 &bsi->bsi_oc->bom_oc->soc_cname,
931 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
934 backsql_strfcat( &bsi->bsi_sel, "cbc",
936 &bsi->bsi_oc->bom_oc->soc_cname,
939 backsql_strfcat( &bsi->bsi_sel, "l",
940 (ber_len_t)sizeof( " AS objectClass,ldap_entries.dn AS dn" ) - 1,
941 " AS objectClass,ldap_entries.dn AS dn" );
943 backsql_strfcat( &bsi->bsi_from, "lb",
944 (ber_len_t)sizeof( " FROM ldap_entries," ) - 1,
945 " FROM ldap_entries,",
946 &bsi->bsi_oc->bom_keytbl );
948 backsql_strfcat( &bsi->bsi_join_where, "lbcbl",
949 (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ",
950 &bsi->bsi_oc->bom_keytbl,
952 &bsi->bsi_oc->bom_keycol,
953 (ber_len_t)sizeof( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ) - 1,
954 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
956 switch ( bsi->bsi_scope ) {
957 case LDAP_SCOPE_BASE:
958 if ( BACKSQL_CANUPPERCASE( bi ) ) {
959 backsql_strfcat( &bsi->bsi_join_where, "bl",
961 (ber_len_t)sizeof( "(ldap_entries.dn)=?" ) - 1,
962 "(ldap_entries.dn)=?" );
964 backsql_strfcat( &bsi->bsi_join_where, "l",
965 (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
966 "ldap_entries.dn=?" );
970 case LDAP_SCOPE_ONELEVEL:
971 backsql_strfcat( &bsi->bsi_join_where, "l",
972 (ber_len_t)sizeof( "ldap_entries.parent=?" ) - 1,
973 "ldap_entries.parent=?" );
976 case LDAP_SCOPE_SUBTREE:
977 if ( BACKSQL_CANUPPERCASE( bi ) ) {
978 backsql_strfcat( &bsi->bsi_join_where, "bl",
980 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE ?" ) - 1,
981 "(ldap_entries.dn) LIKE ?" );
983 backsql_strfcat( &bsi->bsi_join_where, "l",
984 (ber_len_t)sizeof( "ldap_entries.dn LIKE ?" ) - 1,
985 "ldap_entries.dn LIKE ?" );
994 rc = backsql_process_filter( bsi, bsi->bsi_filter );
996 struct berbuf bb = BB_NULL;
998 backsql_strfcat( &bb, "bbblb",
999 &bsi->bsi_sel.bb_val,
1000 &bsi->bsi_from.bb_val,
1001 &bsi->bsi_join_where.bb_val,
1002 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
1003 &bsi->bsi_flt_where.bb_val );
1007 } else if ( rc < 0 ) {
1009 * Indicates that there's no possible way the filter matches
1010 * anything. No need to issue the query
1012 free( query->bv_val );
1013 query->bv_val = NULL;
1016 free( bsi->bsi_sel.bb_val.bv_val );
1017 bsi->bsi_sel.bb_val.bv_len = 0;
1018 bsi->bsi_sel.bb_len = 0;
1019 free( bsi->bsi_from.bb_val.bv_val );
1020 bsi->bsi_from.bb_val.bv_len = 0;
1021 bsi->bsi_from.bb_len = 0;
1022 free( bsi->bsi_join_where.bb_val.bv_val );
1023 bsi->bsi_join_where.bb_val.bv_len = 0;
1024 bsi->bsi_join_where.bb_len = 0;
1025 free( bsi->bsi_flt_where.bb_val.bv_val );
1026 bsi->bsi_flt_where.bb_val.bv_len = 0;
1027 bsi->bsi_flt_where.bb_len = 0;
1029 Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query() returns %s\n",
1030 query->bv_val ? query->bv_val : "NULL", 0, 0 );
1032 return ( rc <= 0 ? 1 : 0 );
1036 backsql_oc_get_candidates( void *v_oc, void *v_bsi )
1038 backsql_oc_map_rec *oc = v_oc;
1039 backsql_srch_info *bsi = v_bsi;
1040 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1041 struct berval query;
1044 backsql_entryID base_id, *c_id;
1046 BACKSQL_ROW_NTS row;
1050 int n_candidates = bsi->bsi_n_candidates;
1052 bsi->bsi_status = LDAP_SUCCESS;
1054 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc='%s'\n",
1055 BACKSQL_OC_NAME( oc ), 0, 0 );
1057 if ( bsi->bsi_n_candidates == -1 ) {
1058 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1059 "unchecked limit has been overcome\n", 0, 0, 0 );
1060 /* should never get here */
1062 bsi->bsi_status = LDAP_ADMINLIMIT_EXCEEDED;
1063 return BACKSQL_STOP;
1067 res = backsql_srch_query( bsi, &query );
1069 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1070 "error while constructing query for objectclass '%s'\n",
1071 oc->bom_oc->soc_cname.bv_val, 0, 0 );
1073 * FIXME: need to separate errors from legally
1074 * impossible filters
1076 switch ( bsi->bsi_status ) {
1078 case LDAP_UNDEFINED_TYPE:
1079 case LDAP_NO_SUCH_OBJECT:
1080 /* we are conservative... */
1082 bsi->bsi_status = LDAP_SUCCESS;
1084 return BACKSQL_CONTINUE;
1086 case LDAP_ADMINLIMIT_EXCEEDED:
1088 /* don't try any more */
1089 return BACKSQL_STOP;
1093 if ( query.bv_val == NULL ) {
1094 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1095 "could not construct query for objectclass '%s'\n",
1096 oc->bom_oc->soc_cname.bv_val, 0, 0 );
1097 bsi->bsi_status = LDAP_SUCCESS;
1098 return BACKSQL_CONTINUE;
1101 Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n",
1102 query.bv_val, 0, 0 );
1104 rc = backsql_Prepare( bsi->bsi_dbh, &sth, query.bv_val, 0 );
1105 free( query.bv_val );
1106 if ( rc != SQL_SUCCESS ) {
1107 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1108 "error preparing query\n", 0, 0, 0 );
1109 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
1110 bsi->bsi_status = LDAP_OTHER;
1111 return BACKSQL_CONTINUE;
1114 Debug( LDAP_DEBUG_TRACE, "id: '%ld'\n", bsi->bsi_oc->bom_id, 0, 0 );
1116 if ( backsql_BindParamID( sth, 1, &bsi->bsi_oc->bom_id ) != SQL_SUCCESS ) {
1117 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1118 "error binding objectclass id parameter\n", 0, 0, 0 );
1119 bsi->bsi_status = LDAP_OTHER;
1120 return BACKSQL_CONTINUE;
1123 switch ( bsi->bsi_scope ) {
1124 case LDAP_SCOPE_BASE:
1125 Debug( LDAP_DEBUG_TRACE, "(base)dn: '%s'\n",
1126 bsi->bsi_base_dn->bv_val, 0, 0 );
1128 rc = backsql_BindParamStr( sth, 2, bsi->bsi_base_dn->bv_val,
1129 BACKSQL_MAX_DN_LEN );
1130 if ( rc != SQL_SUCCESS ) {
1131 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1132 "error binding base_dn parameter\n", 0, 0, 0 );
1133 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh,
1135 bsi->bsi_status = LDAP_OTHER;
1136 return BACKSQL_CONTINUE;
1140 case LDAP_SCOPE_SUBTREE: {
1143 * + 1 because we need room for '%'; this makes a subtree
1144 * search for a DN BACKSQL_MAX_DN_LEN long legal
1145 * if it returns that DN only
1147 char temp_base_dn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
1150 * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
1151 * however this should be handled earlier
1153 assert( bsi->bsi_base_dn->bv_len <= BACKSQL_MAX_DN_LEN );
1156 * Sets the parameters for the SQL built earlier
1157 * NOTE that all the databases could actually use
1158 * the TimesTen version, which would be cleaner
1159 * and would also eliminate the need for the
1160 * subtree_cond line in the configuration file.
1161 * For now, I'm leaving it the way it is,
1162 * so non-TimesTen databases use the original code.
1163 * But at some point this should get cleaned up.
1165 * If "dn" is being used, do a suffix search.
1166 * If "dn_ru" is being used, do a prefix search.
1168 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
1169 temp_base_dn[ 0 ] = '\0';
1170 for ( i = 0, j = bsi->bsi_base_dn->bv_len - 1;
1172 temp_base_dn[ i ] = bsi->bsi_base_dn->bv_val[ j ];
1174 temp_base_dn[ i ] = '%';
1175 temp_base_dn[ i + 1 ] = '\0';
1178 temp_base_dn[ 0 ] = '%';
1179 AC_MEMCPY( &temp_base_dn[ 1 ], bsi->bsi_base_dn->bv_val,
1180 bsi->bsi_base_dn->bv_len + 1 );
1182 /* uppercase DN only if the stored DN can be uppercased
1184 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1185 ldap_pvt_str2upper( temp_base_dn );
1188 Debug( LDAP_DEBUG_TRACE, "(sub)dn: '%s'\n", temp_base_dn,
1191 rc = backsql_BindParamStr( sth, 2, temp_base_dn,
1192 BACKSQL_MAX_DN_LEN );
1193 if ( rc != SQL_SUCCESS ) {
1194 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1195 "error binding base_dn parameter (2)\n",
1197 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh,
1199 bsi->bsi_status = LDAP_OTHER;
1200 return BACKSQL_CONTINUE;
1205 case LDAP_SCOPE_ONELEVEL:
1206 res = backsql_dn2id( bi, &base_id,
1207 bsi->bsi_dbh, bsi->bsi_base_dn );
1208 if ( res != LDAP_SUCCESS ) {
1209 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1210 "could not retrieve base_dn id%s\n",
1211 res == LDAP_NO_SUCH_OBJECT ? ": no such entry"
1213 bsi->bsi_status = res;
1214 return BACKSQL_CONTINUE;
1217 #ifdef BACKSQL_ARBITRARY_KEY
1218 Debug( LDAP_DEBUG_TRACE, "(one)id: '%s'\n",
1219 base_id.eid_id.bv_val, 0, 0 );
1221 rc = backsql_BindParamStr( sth, 2, base_id.eid_id.bv_val,
1222 BACKSQL_MAX_KEY_LEN );
1223 #else /* ! BACKSQL_ARBITRARY_KEY */
1224 Debug( LDAP_DEBUG_TRACE, "(one)id: '%lu'\n", base_id.eid_id,
1227 rc = backsql_BindParamID( sth, 2, &base_id.eid_id );
1228 #endif /* ! BACKSQL_ARBITRARY_KEY */
1229 backsql_free_entryID( &base_id, 0 );
1230 if ( rc != SQL_SUCCESS ) {
1231 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1232 "error binding base id parameter\n", 0, 0, 0 );
1233 bsi->bsi_status = LDAP_OTHER;
1234 return BACKSQL_CONTINUE;
1239 rc = SQLExecute( sth );
1240 if ( !BACKSQL_SUCCESS( rc ) ) {
1241 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1242 "error executing query\n", 0, 0, 0 );
1243 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
1244 SQLFreeStmt( sth, SQL_DROP );
1245 bsi->bsi_status = LDAP_OTHER;
1246 return BACKSQL_CONTINUE;
1249 backsql_BindRowAsStrings( sth, &row );
1250 rc = SQLFetch( sth );
1251 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
1252 c_id = (backsql_entryID *)ch_calloc( 1,
1253 sizeof( backsql_entryID ) );
1254 #ifdef BACKSQL_ARBITRARY_KEY
1255 ber_str2bv( row.cols[ 0 ], 0, 1, &c_id->eid_id );
1256 ber_str2bv( row.cols[ 1 ], 0, 1, &c_id->eid_keyval );
1257 #else /* ! BACKSQL_ARBITRARY_KEY */
1258 c_id->eid_id = strtol( row.cols[ 0 ], NULL, 0 );
1259 c_id->eid_keyval = strtol( row.cols[ 1 ], NULL, 0 );
1260 #endif /* ! BACKSQL_ARBITRARY_KEY */
1261 c_id->eid_oc_id = bsi->bsi_oc->bom_id;
1262 ber_str2bv( row.cols[ 3 ], 0, 1, &c_id->eid_dn );
1263 c_id->eid_next = bsi->bsi_id_list;
1264 bsi->bsi_id_list = c_id;
1265 bsi->bsi_n_candidates--;
1267 #ifdef BACKSQL_ARBITRARY_KEY
1268 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1269 "added entry id=%s, keyval=%s dn='%s'\n",
1270 c_id->eid_id.bv_val, c_id->eid_keyval.bv_val,
1272 #else /* ! BACKSQL_ARBITRARY_KEY */
1273 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1274 "added entry id=%ld, keyval=%ld dn='%s'\n",
1275 c_id->eid_id, c_id->eid_keyval, row.cols[ 3 ] );
1276 #endif /* ! BACKSQL_ARBITRARY_KEY */
1278 if ( bsi->bsi_n_candidates == -1 ) {
1282 backsql_FreeRow( &row );
1283 SQLFreeStmt( sth, SQL_DROP );
1285 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates(): %d\n",
1286 n_candidates - bsi->bsi_n_candidates, 0, 0 );
1288 return ( bsi->bsi_n_candidates == -1 ? BACKSQL_STOP : BACKSQL_CONTINUE );
1292 backsql_search( Operation *op, SlapReply *rs )
1294 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
1299 time_t stoptime = 0;
1300 backsql_srch_info srch_info;
1301 backsql_entryID *eid = NULL;
1303 manageDSAit = get_manageDSAit( op );
1305 Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
1306 "base='%s', filter='%s', scope=%d,",
1307 op->o_req_ndn.bv_val,
1308 op->ors_filterstr.bv_val,
1310 Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
1311 "attributes to load: %s\n",
1314 op->ors_attrs == NULL ? "all" : "custom list" );
1316 if ( op->o_req_ndn.bv_len > BACKSQL_MAX_DN_LEN ) {
1317 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1318 "search base length (%ld) exceeds max length (%ld)\n",
1319 op->o_req_ndn.bv_len, BACKSQL_MAX_DN_LEN, 0 );
1321 * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
1322 * since it is impossible that such a long DN exists
1325 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1326 send_ldap_result( op, rs );
1330 sres = backsql_get_db_conn( op, &dbh );
1331 if ( sres != LDAP_SUCCESS ) {
1332 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1333 "could not get connection handle - exiting\n",
1336 rs->sr_text = sres == LDAP_OTHER ? "SQL-backend error" : NULL;
1337 send_ldap_result( op, rs );
1341 /* compute it anyway; root does not use it */
1342 stoptime = op->o_time + op->ors_tlimit;
1344 backsql_init_search( &srch_info, &op->o_req_dn,
1346 op->ors_slimit, op->ors_tlimit,
1347 stoptime, op->ors_filter,
1348 dbh, op, op->ors_attrs );
1351 * for each objectclass we try to construct query which gets IDs
1352 * of entries matching LDAP query filter and scope (or at least
1353 * candidates), and get the IDs
1355 srch_info.bsi_n_candidates =
1356 ( op->ors_limit == NULL /* isroot == FALSE */ ? -2 :
1357 ( op->ors_limit->lms_s_unchecked == -1 ? -2 :
1358 ( op->ors_limit->lms_s_unchecked ) ) );
1359 avl_apply( bi->oc_by_oc, backsql_oc_get_candidates,
1360 &srch_info, BACKSQL_STOP, AVL_INORDER );
1361 if ( op->ors_limit != NULL /* isroot == TRUE */
1362 && op->ors_limit->lms_s_unchecked != -1
1363 && srch_info.bsi_n_candidates == -1 )
1365 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1366 send_ldap_result( op, rs );
1371 * now we load candidate entries (only those attributes
1372 * mentioned in attrs and filter), test it against full filter
1373 * and then send to client
1375 for ( eid = srch_info.bsi_id_list; eid != NULL;
1376 eid = backsql_free_entryID( eid, 1 ) ) {
1377 Attribute *hasSubordinate = NULL,
1380 /* check for abandon */
1381 if ( op->o_abandon ) {
1385 /* check time limit */
1386 if ( op->ors_tlimit != -1 && slap_get_time() > stoptime ) {
1387 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1388 rs->sr_ctrls = NULL;
1389 rs->sr_ref = rs->sr_v2ref;
1390 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS
1392 send_ldap_result( op, rs );
1396 #ifdef BACKSQL_ARBITRARY_KEY
1397 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1398 "for entry id=%s, oc_id=%ld, keyval=%s\n",
1399 eid->eid_id.bv_val, eid->eid_oc_id,
1400 eid->eid_keyval.bv_val );
1401 #else /* ! BACKSQL_ARBITRARY_KEY */
1402 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1403 "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
1404 eid->eid_id, eid->eid_oc_id, eid->eid_keyval );
1405 #endif /* ! BACKSQL_ARBITRARY_KEY */
1407 entry = (Entry *)ch_calloc( sizeof( Entry ), 1 );
1408 res = backsql_id2entry( &srch_info, entry, eid );
1409 if ( res == NULL ) {
1410 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1411 "error in backsql_id2entry() "
1412 "- skipping entry\n", 0, 0, 0 );
1416 if ( !manageDSAit &&
1417 op->ors_scope != LDAP_SCOPE_BASE &&
1418 is_entry_referral( entry ) ) {
1420 struct berval matched_dn;
1422 ber_dupbv( &matched_dn, &entry->e_name );
1423 refs = get_entry_referrals( op, entry );
1425 rs->sr_ref = referral_rewrite( refs,
1426 &matched_dn, &op->o_req_dn,
1428 ber_bvarray_free( refs );
1431 if ( !rs->sr_ref ) {
1432 rs->sr_text = "bad_referral object";
1435 rs->sr_err = LDAP_REFERRAL;
1436 rs->sr_matched = matched_dn.bv_val;
1437 send_search_reference( op, rs );
1439 ber_bvarray_free( rs->sr_ref );
1441 ber_memfree( matched_dn.bv_val );
1442 rs->sr_matched = NULL;
1448 * We use this flag since we need to parse the filter
1449 * anyway; we should have used the frontend API function
1450 * filter_has_subordinates()
1452 if ( srch_info.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
1455 rc = backsql_has_children( bi, dbh, &entry->e_nname );
1458 case LDAP_COMPARE_TRUE:
1459 case LDAP_COMPARE_FALSE:
1460 hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
1461 if ( hasSubordinate != NULL ) {
1462 for ( a = entry->e_attrs;
1466 a->a_next = hasSubordinate;
1472 Debug(LDAP_DEBUG_TRACE,
1473 "backsql_search(): "
1474 "has_children failed( %d)\n",
1485 if ( test_filter( op, entry, op->ors_filter )
1486 == LDAP_COMPARE_TRUE ) {
1487 if ( hasSubordinate && !( srch_info.bsi_flags & BSQL_SF_ALL_OPER )
1488 && !ad_inlist( slap_schema.si_ad_hasSubordinates, op->ors_attrs ) ) {
1490 attr_free( hasSubordinate );
1491 hasSubordinate = NULL;
1494 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
1500 rs->sr_attrs = op->ors_attrs;
1501 rs->sr_entry = entry;
1502 rs->sr_flags = REP_ENTRY_MODIFIABLE;
1503 sres = send_search_entry( op, rs );
1504 rs->sr_entry = NULL;
1505 rs->sr_attrs = NULL;
1513 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1514 "connection lost\n", 0, 0, 0 );
1519 * FIXME: send_search_entry failed;
1525 entry_free( entry );
1527 if ( op->ors_slimit != -1
1528 && rs->sr_nentries >= op->ors_slimit ) {
1529 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1530 send_ldap_result( op, rs );
1537 if ( rs->sr_nentries > 0 ) {
1538 rs->sr_ref = rs->sr_v2ref;
1539 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS
1542 rs->sr_err = srch_info.bsi_status;
1544 send_ldap_result( op, rs );
1546 if ( rs->sr_v2ref ) {
1547 ber_bvarray_free( rs->sr_v2ref );
1548 rs->sr_v2ref = NULL;
1552 ch_free( srch_info.bsi_attrs );
1554 Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
1558 #endif /* SLAPD_SQL */