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 static int backsql_process_filter( backsql_srch_info *bsi, Filter *f );
35 static int backsql_process_filter_eq( backsql_srch_info *bsi,
36 backsql_at_map_rec *at,
37 int casefold, struct berval *filter_value );
38 static int backsql_process_filter_like( backsql_srch_info *bsi,
39 backsql_at_map_rec *at,
40 int casefold, struct berval *filter_value );
41 static int backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f,
42 backsql_at_map_rec *at );
45 backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad )
48 AttributeName *an = NULL;
50 if ( bsi->bsi_attrs == NULL ) {
55 * clear the list (retrieve all attrs)
58 ch_free( bsi->bsi_attrs );
59 bsi->bsi_attrs = NULL;
63 for ( ; bsi->bsi_attrs[ n_attrs ].an_name.bv_val; n_attrs++ ) {
64 an = &bsi->bsi_attrs[ n_attrs ];
66 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
67 "attribute \"%s\" is in list\n",
68 an->an_name.bv_val, 0, 0 );
70 * We can live with strcmp because the attribute
71 * list has been normalized before calling be_search
73 if ( !BACKSQL_NCMP( &an->an_name, &ad->ad_cname ) ) {
78 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
79 "adding \"%s\" to list\n", ad->ad_cname.bv_val, 0, 0 );
81 an = (AttributeName *)ch_realloc( bsi->bsi_attrs,
82 sizeof( AttributeName ) * ( n_attrs + 2 ) );
87 an[ n_attrs ].an_name = ad->ad_cname;
88 an[ n_attrs ].an_desc = ad;
89 an[ n_attrs + 1 ].an_name.bv_val = NULL;
90 an[ n_attrs + 1 ].an_name.bv_len = 0;
99 backsql_srch_info *bsi,
109 AttributeName *attrs )
113 bsi->bsi_base_dn = base;
114 bsi->bsi_scope = scope;
115 bsi->bsi_slimit = slimit;
116 bsi->bsi_tlimit = tlimit;
117 bsi->bsi_filter = filter;
126 if ( attrs == NULL || an_find( attrs, &AllUser ) ) {
127 bsi->bsi_attrs = NULL;
130 bsi->bsi_attrs = (AttributeName *)ch_calloc( 1,
131 sizeof( AttributeName ) );
132 bsi->bsi_attrs[ 0 ].an_name.bv_val = NULL;
133 bsi->bsi_attrs[ 0 ].an_name.bv_len = 0;
135 for ( p = attrs; p->an_name.bv_val; p++ ) {
137 * ignore "1.1"; handle "+"
139 if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
140 bsi->bsi_flags |= BSQL_SF_ALL_OPER;
143 } else if ( BACKSQL_NCMP( &p->an_name, &NoAttrs ) == 0 ) {
147 backsql_attrlist_add( bsi, p->an_desc );
151 bsi->bsi_abandon = 0;
152 bsi->bsi_id_list = NULL;
153 bsi->bsi_n_candidates = 0;
154 bsi->bsi_stoptime = stoptime;
155 bsi->bsi_sel.bb_val.bv_val = NULL;
156 bsi->bsi_sel.bb_val.bv_len = 0;
157 bsi->bsi_sel.bb_len = 0;
158 bsi->bsi_from.bb_val.bv_val = NULL;
159 bsi->bsi_from.bb_val.bv_len = 0;
160 bsi->bsi_from.bb_len = 0;
161 bsi->bsi_join_where.bb_val.bv_val = NULL;
162 bsi->bsi_join_where.bb_val.bv_len = 0;
163 bsi->bsi_join_where.bb_len = 0;
164 bsi->bsi_flt_where.bb_val.bv_val = NULL;
165 bsi->bsi_flt_where.bb_val.bv_len = 0;
166 bsi->bsi_flt_where.bb_len = 0;
167 bsi->bsi_filter_oc = NULL;
169 bsi->bsi_status = LDAP_SUCCESS;
173 backsql_process_filter_list( backsql_srch_info *bsi, Filter *f, int op )
181 backsql_strfcat( &bsi->bsi_flt_where, "c", '(' /* ) */ );
184 res = backsql_process_filter( bsi, f );
187 * TimesTen : If the query has no answers,
188 * don't bother to run the query.
199 case LDAP_FILTER_AND:
200 backsql_strfcat( &bsi->bsi_flt_where, "l",
201 (ber_len_t)STRLENOF( " AND " ),
206 backsql_strfcat( &bsi->bsi_flt_where, "l",
207 (ber_len_t)STRLENOF( " OR " ),
213 backsql_strfcat( &bsi->bsi_flt_where, "c", /* ( */ ')' );
219 backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f,
220 backsql_at_map_rec *at )
222 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
230 /* always uppercase strings by now */
231 #ifdef BACKSQL_UPPERCASE_FILTER
232 if ( SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
233 bi->bi_caseIgnoreMatch ) )
234 #endif /* BACKSQL_UPPERCASE_FILTER */
239 if ( SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
240 bi->bi_telephoneNumberMatch ) )
247 * to check for matching telephone numbers
248 * with intermixed chars, e.g. val='1234'
251 * val LIKE '%1%2%3%4%'
255 if ( f->f_sub_initial.bv_val ) {
256 bv.bv_len += f->f_sub_initial.bv_len;
258 if ( f->f_sub_any != NULL ) {
259 for ( a = 0; f->f_sub_any[ a ].bv_val != NULL; a++ ) {
260 bv.bv_len += f->f_sub_any[ a ].bv_len;
263 if ( f->f_sub_final.bv_val ) {
264 bv.bv_len += f->f_sub_final.bv_len;
266 bv.bv_len = 2 * bv.bv_len - 1;
267 bv.bv_val = ch_malloc( bv.bv_len + 1 );
270 if ( f->f_sub_initial.bv_val ) {
271 bv.bv_val[ s ] = f->f_sub_initial.bv_val[ 0 ];
272 for ( i = 1; i < f->f_sub_initial.bv_len; i++ ) {
273 bv.bv_val[ s + 2 * i - 1 ] = '%';
274 bv.bv_val[ s + 2 * i ] = f->f_sub_initial.bv_val[ i ];
276 bv.bv_val[ s + 2 * i - 1 ] = '%';
280 if ( f->f_sub_any != NULL ) {
281 for ( a = 0; f->f_sub_any[ a ].bv_val != NULL; a++ ) {
282 bv.bv_val[ s ] = f->f_sub_any[ a ].bv_val[ 0 ];
283 for ( i = 1; i < f->f_sub_any[ a ].bv_len; i++ ) {
284 bv.bv_val[ s + 2 * i - 1 ] = '%';
285 bv.bv_val[ s + 2 * i ] = f->f_sub_any[ a ].bv_val[ i ];
287 bv.bv_val[ s + 2 * i - 1 ] = '%';
292 if ( f->f_sub_final.bv_val ) {
293 bv.bv_val[ s ] = f->f_sub_final.bv_val[ 0 ];
294 for ( i = 1; i < f->f_sub_final.bv_len; i++ ) {
295 bv.bv_val[ s + 2 * i - 1 ] = '%';
296 bv.bv_val[ s + 2 * i ] = f->f_sub_final.bv_val[ i ];
298 bv.bv_val[ s + 2 * i - 1 ] = '%';
302 bv.bv_val[ s - 1 ] = '\0';
304 (void)backsql_process_filter_like( bsi, at, casefold, &bv );
305 ch_free( bv.bv_val );
311 * When dealing with case-sensitive strings
312 * we may omit normalization; however, normalized
313 * SQL filters are more liberal.
316 backsql_strfcat( &bsi->bsi_flt_where, "c", '(' /* ) */ );
319 Debug( LDAP_DEBUG_TRACE, "backsql_process_sub_filter(%s):\n",
320 at->bam_ad->ad_cname.bv_val, 0, 0 );
321 Debug(LDAP_DEBUG_TRACE, " expr: '%s%s%s'\n", at->bam_sel_expr.bv_val,
322 at->bam_sel_expr_u.bv_val ? "' '" : "",
323 at->bam_sel_expr_u.bv_val ? at->bam_sel_expr_u.bv_val : "" );
324 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
326 * If a pre-upper-cased version of the column
327 * or a precompiled upper function exists, use it
329 backsql_strfcat( &bsi->bsi_flt_where,
332 (ber_len_t)STRLENOF( " LIKE '" ),
336 backsql_strfcat( &bsi->bsi_flt_where, "bl",
338 (ber_len_t)STRLENOF( " LIKE '" ), " LIKE '" );
341 if ( f->f_sub_initial.bv_val != NULL ) {
345 Debug( LDAP_DEBUG_TRACE,
346 "==>backsql_process_sub_filter(%s): "
347 "sub_initial=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
348 f->f_sub_initial.bv_val, 0 );
349 #endif /* BACKSQL_TRACE */
351 start = bsi->bsi_flt_where.bb_val.bv_len;
352 backsql_strfcat( &bsi->bsi_flt_where, "b",
354 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
355 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
359 backsql_strfcat( &bsi->bsi_flt_where, "c", '%' );
361 if ( f->f_sub_any != NULL ) {
362 for ( i = 0; f->f_sub_any[ i ].bv_val != NULL; i++ ) {
366 Debug( LDAP_DEBUG_TRACE,
367 "==>backsql_process_sub_filter(%s): "
368 "sub_any[%d]=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
369 i, f->f_sub_any[ i ].bv_val );
370 #endif /* BACKSQL_TRACE */
372 start = bsi->bsi_flt_where.bb_val.bv_len;
373 backsql_strfcat( &bsi->bsi_flt_where,
377 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
379 * Note: toupper('%') = '%'
381 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
386 if ( f->f_sub_final.bv_val != NULL ) {
390 Debug( LDAP_DEBUG_TRACE,
391 "==>backsql_process_sub_filter(%s): "
392 "sub_final=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
393 f->f_sub_final.bv_val, 0 );
394 #endif /* BACKSQL_TRACE */
396 start = bsi->bsi_flt_where.bb_val.bv_len;
397 backsql_strfcat( &bsi->bsi_flt_where, "b",
399 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
400 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
404 backsql_strfcat( &bsi->bsi_flt_where, "l",
405 (ber_len_t)sizeof( /* (' */ "')" ) - 1, /* ( */ "')" );
411 backsql_process_filter( backsql_srch_info *bsi, Filter *f )
413 backsql_at_map_rec **vat = NULL;
414 AttributeDescription *ad = NULL;
419 Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n", 0, 0, 0 );
420 if ( f->f_choice == SLAPD_FILTER_COMPUTED ) {
421 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
422 "invalid filter\n", 0, 0, 0 );
427 switch( f->f_choice ) {
429 rc = backsql_process_filter_list( bsi, f->f_or,
434 case LDAP_FILTER_AND:
435 rc = backsql_process_filter_list( bsi, f->f_and,
440 case LDAP_FILTER_NOT:
441 backsql_strfcat( &bsi->bsi_flt_where, "l",
442 (ber_len_t)sizeof( "NOT (" /* ) */ ) - 1,
444 rc = backsql_process_filter( bsi, f->f_not );
445 backsql_strfcat( &bsi->bsi_flt_where, "c", /* ( */ ')' );
449 case LDAP_FILTER_PRESENT:
453 case LDAP_FILTER_EXT:
454 ad = f->f_mra->ma_desc;
455 if ( f->f_mr_dnattrs ) {
457 * if dn attrs filtering is requested, better return
458 * success and let test_filter() deal with candidate
459 * selection; otherwise we'd need to set conditions
460 * on the contents of the DN, e.g. "SELECT ... FROM
461 * ldap_entries AS attributeName WHERE attributeName.dn
462 * like '%attributeName=value%'"
464 backsql_strfcat( &bsi->bsi_flt_where, "l",
465 (ber_len_t)STRLENOF( "1=1" ), "1=1" );
466 bsi->bsi_status = LDAP_SUCCESS;
487 * Turn structuralObjectClass into objectClass
489 if ( ad == slap_schema.si_ad_objectClass
490 || ad == slap_schema.si_ad_structuralObjectClass ) {
492 * If the filter is LDAP_FILTER_PRESENT, then it's done;
493 * otherwise, let's see if we are lucky: filtering
494 * for "structural" objectclass or ancestor...
496 switch ( f->f_choice ) {
497 case LDAP_FILTER_EQUALITY:
499 ObjectClass *oc = oc_bvfind( &f->f_av_value );
502 Debug( LDAP_DEBUG_TRACE,
503 "backsql_process_filter(): "
504 "unknown objectClass \"%s\" "
506 f->f_av_value.bv_val, 0, 0 );
507 bsi->bsi_status = LDAP_OTHER;
513 * "structural" objectClass inheritance:
514 * - a search for "person" will also return
516 * - a search for "top" will return everything
518 if ( is_object_subclass( oc, bsi->bsi_oc->bom_oc ) ) {
519 goto filter_oc_success;
525 case LDAP_FILTER_PRESENT:
527 backsql_strfcat( &bsi->bsi_flt_where, "l",
528 (ber_len_t)STRLENOF( "1=1" ), "1=1" );
529 bsi->bsi_status = LDAP_SUCCESS;
534 Debug( LDAP_DEBUG_TRACE,
535 "backsql_process_filter(): "
536 "illegal/unhandled filter "
537 "on objectClass attribute",
539 bsi->bsi_status = LDAP_OTHER;
544 } else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) {
546 * FIXME: this is not robust; e.g. a filter
547 * '(!(hasSubordinates=TRUE))' fails because
548 * in SQL it would read 'NOT (1=1)' instead
550 * Note however that hasSubordinates is boolean,
551 * so a more appropriate filter would be
552 * '(hasSubordinates=FALSE)'
554 * A more robust search for hasSubordinates
555 * would * require joining the ldap_entries table
556 * selecting if there are descendants of the
559 backsql_strfcat( &bsi->bsi_flt_where, "l",
560 (ber_len_t)STRLENOF( "1=1" ), "1=1" );
561 if ( ad == slap_schema.si_ad_hasSubordinates ) {
563 * instruct candidate selection algorithm
564 * and attribute list to try to detect
565 * if an entry has subordinates
567 bsi->bsi_flags |= BSQL_SF_FILTER_HASSUBORDINATE;
571 * clear attributes to fetch, to require ALL
572 * and try extended match on all attributes
574 backsql_attrlist_add( bsi, NULL );
581 * attribute inheritance:
583 if ( backsql_supad2at( bsi->bsi_oc, ad, &vat ) ) {
584 bsi->bsi_status = LDAP_OTHER;
590 /* search anyway; other parts of the filter
592 backsql_strfcat( &bsi->bsi_flt_where, "l",
593 (ber_len_t)STRLENOF( "1=1" ), "1=1" );
594 bsi->bsi_status = LDAP_SUCCESS;
599 /* if required, open extra level of parens */
601 if ( vat[0]->bam_next || vat[1] ) {
602 backsql_strfcat( &bsi->bsi_flt_where, "c", '(' );
609 if ( backsql_process_filter_attr( bsi, f, vat[i] ) == -1 ) {
613 /* if more definitions of the same attr, apply */
614 if ( vat[i]->bam_next ) {
615 backsql_strfcat( &bsi->bsi_flt_where, "l",
616 STRLENOF( " OR " ), " OR " );
617 vat[i] = vat[i]->bam_next;
621 /* if more descendants of the same attr, apply */
624 backsql_strfcat( &bsi->bsi_flt_where, "l",
625 STRLENOF( " OR " ), " OR " );
629 /* if needed, close extra level of parens */
631 backsql_strfcat( &bsi->bsi_flt_where, "c", ')' );
641 Debug( LDAP_DEBUG_TRACE,
642 "<==backsql_process_filter() %s\n",
643 rc == 1 ? "succeeded" : "failed", 0, 0);
649 backsql_process_filter_eq( backsql_srch_info *bsi, backsql_at_map_rec *at,
650 int casefold, struct berval *filter_value )
653 * maybe we should check type of at->sel_expr here somehow,
654 * to know whether upper_func is applicable, but for now
655 * upper_func stuff is made for Oracle, where UPPER is
656 * safely applicable to NUMBER etc.
658 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
661 backsql_strfcat( &bsi->bsi_flt_where, "cbl",
664 (ber_len_t)STRLENOF( "='" ),
667 start = bsi->bsi_flt_where.bb_val.bv_len;
669 backsql_strfcat( &bsi->bsi_flt_where, "bl",
671 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
674 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
677 backsql_strfcat( &bsi->bsi_flt_where, "cblbl",
680 (ber_len_t)STRLENOF( "='" ), "='",
682 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
690 backsql_process_filter_like( backsql_srch_info *bsi, backsql_at_map_rec *at,
691 int casefold, struct berval *filter_value )
694 * maybe we should check type of at->sel_expr here somehow,
695 * to know whether upper_func is applicable, but for now
696 * upper_func stuff is made for Oracle, where UPPER is
697 * safely applicable to NUMBER etc.
699 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
702 backsql_strfcat( &bsi->bsi_flt_where, "cbl",
705 (ber_len_t)STRLENOF( " LIKE '%" ),
708 start = bsi->bsi_flt_where.bb_val.bv_len;
710 backsql_strfcat( &bsi->bsi_flt_where, "bl",
712 (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
715 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
718 backsql_strfcat( &bsi->bsi_flt_where, "cblbl",
721 (ber_len_t)STRLENOF( " LIKE '%" ),
724 (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
732 backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, backsql_at_map_rec *at )
734 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
736 struct berval *filter_value = NULL;
737 MatchingRule *matching_rule = NULL;
738 struct berval ordering = BER_BVC("<=");
740 Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter_attr(%s)\n",
741 at->bam_ad->ad_cname.bv_val, 0, 0 );
743 backsql_merge_from_clause( &bsi->bsi_from, &at->bam_from_tbls );
746 * need to add this attribute to list of attrs to load,
747 * so that we can do test_filter() later
749 backsql_attrlist_add( bsi, at->bam_ad );
751 if ( at->bam_join_where.bv_val != NULL
752 && strstr( bsi->bsi_join_where.bb_val.bv_val, at->bam_join_where.bv_val ) == NULL ) {
753 backsql_strfcat( &bsi->bsi_join_where, "lb",
754 (ber_len_t)STRLENOF( " AND " ), " AND ",
755 &at->bam_join_where );
758 switch ( f->f_choice ) {
759 case LDAP_FILTER_EQUALITY:
760 filter_value = &f->f_av_value;
761 matching_rule = at->bam_ad->ad_type->sat_equality;
765 /* fail over into next case */
767 case LDAP_FILTER_EXT:
768 filter_value = &f->f_mra->ma_value;
769 matching_rule = f->f_mr_rule;
772 /* always uppercase strings by now */
773 #ifdef BACKSQL_UPPERCASE_FILTER
774 if ( SLAP_MR_ASSOCIATED( matching_rule,
775 bi->bi_caseIgnoreMatch ) )
776 #endif /* BACKSQL_UPPERCASE_FILTER */
781 if ( SLAP_MR_ASSOCIATED( matching_rule,
782 bi->bi_telephoneNumberMatch ) )
788 * to check for matching telephone numbers
789 * with intermized chars, e.g. val='1234'
792 * val LIKE '%1%2%3%4%'
795 bv.bv_len = 2 * filter_value->bv_len - 1;
796 bv.bv_val = ch_malloc( bv.bv_len + 1 );
798 bv.bv_val[ 0 ] = filter_value->bv_val[ 0 ];
799 for ( i = 1; i < filter_value->bv_len; i++ ) {
800 bv.bv_val[ 2 * i - 1 ] = '%';
801 bv.bv_val[ 2 * i ] = filter_value->bv_val[ i ];
803 bv.bv_val[ 2 * i - 1 ] = '\0';
805 (void)backsql_process_filter_like( bsi, at, casefold, &bv );
806 ch_free( bv.bv_val );
812 * maybe we should check type of at->sel_expr here somehow,
813 * to know whether upper_func is applicable, but for now
814 * upper_func stuff is made for Oracle, where UPPER is
815 * safely applicable to NUMBER etc.
817 (void)backsql_process_filter_eq( bsi, at, casefold, filter_value );
821 ordering.bv_val = ">=";
823 /* fall thru to next case */
826 /* always uppercase strings by now */
827 #ifdef BACKSQL_UPPERCASE_FILTER
828 if ( SLAP_MR_ASSOCIATED( at->bam_ad->ad_type->sat_ordering,
829 bi->bi_caseIgnoreMatch ) )
830 #endif /* BACKSQL_UPPERCASE_FILTER */
836 * FIXME: should we uppercase the operands?
838 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
841 backsql_strfcat( &bsi->bsi_flt_where, "cbbc",
847 start = bsi->bsi_flt_where.bb_val.bv_len;
849 backsql_strfcat( &bsi->bsi_flt_where, "bl",
851 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
854 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
857 backsql_strfcat( &bsi->bsi_flt_where, "cbbcbl",
863 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
868 case LDAP_FILTER_PRESENT:
869 backsql_strfcat( &bsi->bsi_flt_where, "lbl",
870 (ber_len_t)sizeof( "NOT (" /* ) */) - 1,
873 (ber_len_t)sizeof( /* ( */ " IS NULL)" ) - 1,
874 /* ( */ " IS NULL)" );
877 case LDAP_FILTER_SUBSTRINGS:
878 backsql_process_sub_filter( bsi, f, at );
881 case LDAP_FILTER_APPROX:
885 * maybe we should check type of at->sel_expr here somehow,
886 * to know whether upper_func is applicable, but for now
887 * upper_func stuff is made for Oracle, where UPPER is
888 * safely applicable to NUMBER etc.
890 (void)backsql_process_filter_like( bsi, at, 1, &f->f_av_value );
894 /* unhandled filter type; should not happen */
896 backsql_strfcat( &bsi->bsi_flt_where, "l",
897 (ber_len_t)STRLENOF( "1=1" ), "1=1" );
902 Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter_attr(%s)\n",
903 at->bam_ad->ad_cname.bv_val, 0, 0 );
909 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
911 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
915 query->bv_val = NULL;
918 Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
919 bsi->bsi_sel.bb_val.bv_val = NULL;
920 bsi->bsi_sel.bb_val.bv_len = 0;
921 bsi->bsi_sel.bb_len = 0;
922 bsi->bsi_from.bb_val.bv_val = NULL;
923 bsi->bsi_from.bb_val.bv_len = 0;
924 bsi->bsi_from.bb_len = 0;
925 bsi->bsi_join_where.bb_val.bv_val = NULL;
926 bsi->bsi_join_where.bb_val.bv_len = 0;
927 bsi->bsi_join_where.bb_len = 0;
928 bsi->bsi_flt_where.bb_val.bv_val = NULL;
929 bsi->bsi_flt_where.bb_val.bv_len = 0;
930 bsi->bsi_flt_where.bb_len = 0;
932 backsql_strfcat( &bsi->bsi_sel, "lbcbc",
933 (ber_len_t)STRLENOF( "SELECT DISTINCT ldap_entries.id," ),
934 "SELECT DISTINCT ldap_entries.id,",
935 &bsi->bsi_oc->bom_keytbl,
937 &bsi->bsi_oc->bom_keycol,
940 if ( bi->strcast_func.bv_val ) {
941 backsql_strfcat( &bsi->bsi_sel, "blbl",
943 (ber_len_t)sizeof( "('" /* ') */ ) - 1,
945 &bsi->bsi_oc->bom_oc->soc_cname,
946 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
949 backsql_strfcat( &bsi->bsi_sel, "cbc",
951 &bsi->bsi_oc->bom_oc->soc_cname,
954 backsql_strfcat( &bsi->bsi_sel, "l",
955 (ber_len_t)STRLENOF( " AS objectClass,ldap_entries.dn AS dn" ),
956 " AS objectClass,ldap_entries.dn AS dn" );
958 backsql_strfcat( &bsi->bsi_from, "lb",
959 (ber_len_t)STRLENOF( " FROM ldap_entries," ),
960 " FROM ldap_entries,",
961 &bsi->bsi_oc->bom_keytbl );
963 backsql_strfcat( &bsi->bsi_join_where, "lbcbl",
964 (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
965 &bsi->bsi_oc->bom_keytbl,
967 &bsi->bsi_oc->bom_keycol,
968 (ber_len_t)STRLENOF( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ),
969 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
971 switch ( bsi->bsi_scope ) {
972 case LDAP_SCOPE_BASE:
973 if ( BACKSQL_CANUPPERCASE( bi ) ) {
974 backsql_strfcat( &bsi->bsi_join_where, "bl",
976 (ber_len_t)STRLENOF( "(ldap_entries.dn)=?" ),
977 "(ldap_entries.dn)=?" );
979 backsql_strfcat( &bsi->bsi_join_where, "l",
980 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
981 "ldap_entries.dn=?" );
985 case BACKSQL_SCOPE_BASE_LIKE:
986 if ( BACKSQL_CANUPPERCASE( bi ) ) {
987 backsql_strfcat( &bsi->bsi_join_where, "bl",
989 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ),
990 "(ldap_entries.dn) LIKE ?" );
992 backsql_strfcat( &bsi->bsi_join_where, "l",
993 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ),
994 "ldap_entries.dn LIKE ?" );
998 case LDAP_SCOPE_ONELEVEL:
999 backsql_strfcat( &bsi->bsi_join_where, "l",
1000 (ber_len_t)STRLENOF( "ldap_entries.parent=?" ),
1001 "ldap_entries.parent=?" );
1004 case LDAP_SCOPE_SUBTREE:
1005 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1006 backsql_strfcat( &bsi->bsi_join_where, "bl",
1008 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ),
1009 "(ldap_entries.dn) LIKE ?" );
1011 backsql_strfcat( &bsi->bsi_join_where, "l",
1012 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ),
1013 "ldap_entries.dn LIKE ?" );
1022 rc = backsql_process_filter( bsi, bsi->bsi_filter );
1024 struct berbuf bb = BB_NULL;
1026 backsql_strfcat( &bb, "bbblb",
1027 &bsi->bsi_sel.bb_val,
1028 &bsi->bsi_from.bb_val,
1029 &bsi->bsi_join_where.bb_val,
1030 (ber_len_t)STRLENOF( " AND " ), " AND ",
1031 &bsi->bsi_flt_where.bb_val );
1035 } else if ( rc < 0 ) {
1037 * Indicates that there's no possible way the filter matches
1038 * anything. No need to issue the query
1040 free( query->bv_val );
1041 query->bv_val = NULL;
1044 free( bsi->bsi_sel.bb_val.bv_val );
1045 bsi->bsi_sel.bb_val.bv_len = 0;
1046 bsi->bsi_sel.bb_len = 0;
1047 free( bsi->bsi_from.bb_val.bv_val );
1048 bsi->bsi_from.bb_val.bv_len = 0;
1049 bsi->bsi_from.bb_len = 0;
1050 free( bsi->bsi_join_where.bb_val.bv_val );
1051 bsi->bsi_join_where.bb_val.bv_len = 0;
1052 bsi->bsi_join_where.bb_len = 0;
1053 free( bsi->bsi_flt_where.bb_val.bv_val );
1054 bsi->bsi_flt_where.bb_val.bv_len = 0;
1055 bsi->bsi_flt_where.bb_len = 0;
1057 Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query() returns %s\n",
1058 query->bv_val ? query->bv_val : "NULL", 0, 0 );
1060 return ( rc <= 0 ? 1 : 0 );
1064 backsql_oc_get_candidates( void *v_oc, void *v_bsi )
1066 backsql_oc_map_rec *oc = v_oc;
1067 backsql_srch_info *bsi = v_bsi;
1068 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1069 struct berval query;
1072 backsql_entryID base_id = BACKSQL_ENTRYID_INIT;
1074 BACKSQL_ROW_NTS row;
1077 int n_candidates = bsi->bsi_n_candidates;
1080 * + 1 because we need room for '%'; this makes a subtree
1081 * search for a DN BACKSQL_MAX_DN_LEN long legal
1082 * if it returns that DN only
1084 char temp_base_dn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
1086 bsi->bsi_status = LDAP_SUCCESS;
1088 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc=\"%s\"\n",
1089 BACKSQL_OC_NAME( oc ), 0, 0 );
1091 if ( bsi->bsi_n_candidates == -1 ) {
1092 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1093 "unchecked limit has been overcome\n", 0, 0, 0 );
1094 /* should never get here */
1096 bsi->bsi_status = LDAP_ADMINLIMIT_EXCEEDED;
1097 return BACKSQL_AVL_STOP;
1101 res = backsql_srch_query( bsi, &query );
1103 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1104 "error while constructing query for objectclass \"%s\"\n",
1105 oc->bom_oc->soc_cname.bv_val, 0, 0 );
1107 * FIXME: need to separate errors from legally
1108 * impossible filters
1110 switch ( bsi->bsi_status ) {
1112 case LDAP_UNDEFINED_TYPE:
1113 case LDAP_NO_SUCH_OBJECT:
1114 /* we are conservative... */
1116 bsi->bsi_status = LDAP_SUCCESS;
1118 return BACKSQL_AVL_CONTINUE;
1120 case LDAP_ADMINLIMIT_EXCEEDED:
1122 /* don't try any more */
1123 return BACKSQL_AVL_STOP;
1127 if ( query.bv_val == NULL ) {
1128 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1129 "could not construct query for objectclass \"%s\"\n",
1130 oc->bom_oc->soc_cname.bv_val, 0, 0 );
1131 bsi->bsi_status = LDAP_SUCCESS;
1132 return BACKSQL_AVL_CONTINUE;
1135 Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n",
1136 query.bv_val, 0, 0 );
1138 rc = backsql_Prepare( bsi->bsi_dbh, &sth, query.bv_val, 0 );
1139 free( query.bv_val );
1140 if ( rc != SQL_SUCCESS ) {
1141 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1142 "error preparing query\n", 0, 0, 0 );
1143 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
1144 bsi->bsi_status = LDAP_OTHER;
1145 return BACKSQL_AVL_CONTINUE;
1148 Debug( LDAP_DEBUG_TRACE, "id: '%ld'\n", bsi->bsi_oc->bom_id, 0, 0 );
1150 if ( backsql_BindParamID( sth, 1, &bsi->bsi_oc->bom_id ) != SQL_SUCCESS ) {
1151 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1152 "error binding objectclass id parameter\n", 0, 0, 0 );
1153 bsi->bsi_status = LDAP_OTHER;
1154 return BACKSQL_AVL_CONTINUE;
1157 switch ( bsi->bsi_scope ) {
1158 case LDAP_SCOPE_BASE:
1159 case BACKSQL_SCOPE_BASE_LIKE:
1161 * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
1162 * however this should be handled earlier
1164 if ( bsi->bsi_base_dn->bv_len > BACKSQL_MAX_DN_LEN ) {
1165 bsi->bsi_status = LDAP_OTHER;
1166 return BACKSQL_AVL_CONTINUE;
1169 AC_MEMCPY( temp_base_dn, bsi->bsi_base_dn->bv_val,
1170 bsi->bsi_base_dn->bv_len + 1 );
1172 /* uppercase DN only if the stored DN can be uppercased
1174 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1175 ldap_pvt_str2upper( temp_base_dn );
1178 Debug( LDAP_DEBUG_TRACE, "(base)dn: \"%s\"\n",
1179 temp_base_dn, 0, 0 );
1181 rc = backsql_BindParamStr( sth, 2, temp_base_dn,
1182 BACKSQL_MAX_DN_LEN );
1183 if ( rc != SQL_SUCCESS ) {
1184 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1185 "error binding base_dn parameter\n", 0, 0, 0 );
1186 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh,
1188 bsi->bsi_status = LDAP_OTHER;
1189 return BACKSQL_AVL_CONTINUE;
1193 case LDAP_SCOPE_SUBTREE: {
1195 * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
1196 * however this should be handled earlier
1198 if ( bsi->bsi_base_dn->bv_len > BACKSQL_MAX_DN_LEN ) {
1199 bsi->bsi_status = LDAP_OTHER;
1200 return BACKSQL_AVL_CONTINUE;
1204 * Sets the parameters for the SQL built earlier
1205 * NOTE that all the databases could actually use
1206 * the TimesTen version, which would be cleaner
1207 * and would also eliminate the need for the
1208 * subtree_cond line in the configuration file.
1209 * For now, I'm leaving it the way it is,
1210 * so non-TimesTen databases use the original code.
1211 * But at some point this should get cleaned up.
1213 * If "dn" is being used, do a suffix search.
1214 * If "dn_ru" is being used, do a prefix search.
1216 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
1217 temp_base_dn[ 0 ] = '\0';
1218 for ( i = 0, j = bsi->bsi_base_dn->bv_len - 1;
1220 temp_base_dn[ i ] = bsi->bsi_base_dn->bv_val[ j ];
1222 temp_base_dn[ i ] = '%';
1223 temp_base_dn[ i + 1 ] = '\0';
1226 temp_base_dn[ 0 ] = '%';
1227 AC_MEMCPY( &temp_base_dn[ 1 ], bsi->bsi_base_dn->bv_val,
1228 bsi->bsi_base_dn->bv_len + 1 );
1231 /* uppercase DN only if the stored DN can be uppercased
1233 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1234 ldap_pvt_str2upper( temp_base_dn );
1237 Debug( LDAP_DEBUG_TRACE, "(sub)dn: \"%s\"\n", temp_base_dn,
1240 rc = backsql_BindParamStr( sth, 2, temp_base_dn,
1241 BACKSQL_MAX_DN_LEN );
1242 if ( rc != SQL_SUCCESS ) {
1243 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1244 "error binding base_dn parameter (2)\n",
1246 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh,
1248 bsi->bsi_status = LDAP_OTHER;
1249 return BACKSQL_AVL_CONTINUE;
1254 case LDAP_SCOPE_ONELEVEL:
1255 res = backsql_dn2id( bi, &base_id,
1256 bsi->bsi_dbh, bsi->bsi_base_dn );
1257 if ( res != LDAP_SUCCESS ) {
1258 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1259 "could not retrieve base_dn id%s\n",
1260 res == LDAP_NO_SUCH_OBJECT ? ": no such entry"
1262 bsi->bsi_status = res;
1263 return BACKSQL_AVL_CONTINUE;
1266 #ifdef BACKSQL_ARBITRARY_KEY
1267 Debug( LDAP_DEBUG_TRACE, "(one)id: \"%s\"\n",
1268 base_id.eid_id.bv_val, 0, 0 );
1270 rc = backsql_BindParamStr( sth, 2, base_id.eid_id.bv_val,
1271 BACKSQL_MAX_KEY_LEN );
1272 #else /* ! BACKSQL_ARBITRARY_KEY */
1273 Debug( LDAP_DEBUG_TRACE, "(one)id: '%lu'\n", base_id.eid_id,
1276 rc = backsql_BindParamID( sth, 2, &base_id.eid_id );
1277 #endif /* ! BACKSQL_ARBITRARY_KEY */
1278 backsql_free_entryID( &base_id, 0 );
1279 if ( rc != SQL_SUCCESS ) {
1280 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1281 "error binding base id parameter\n", 0, 0, 0 );
1282 bsi->bsi_status = LDAP_OTHER;
1283 return BACKSQL_AVL_CONTINUE;
1288 rc = SQLExecute( sth );
1289 if ( !BACKSQL_SUCCESS( rc ) ) {
1290 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1291 "error executing query\n", 0, 0, 0 );
1292 backsql_PrintErrors( bi->db_env, bsi->bsi_dbh, sth, rc );
1293 SQLFreeStmt( sth, SQL_DROP );
1294 bsi->bsi_status = LDAP_OTHER;
1295 return BACKSQL_AVL_CONTINUE;
1298 backsql_BindRowAsStrings( sth, &row );
1299 rc = SQLFetch( sth );
1300 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
1302 backsql_entryID *c_id = NULL;
1304 ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
1306 if ( backsql_api_odbc2dn( bsi->bsi_op, bsi->bsi_rs, &dn ) ) {
1310 c_id = (backsql_entryID *)ch_calloc( 1,
1311 sizeof( backsql_entryID ) );
1312 #ifdef BACKSQL_ARBITRARY_KEY
1313 ber_str2bv( row.cols[ 0 ], 0, 1, &c_id->eid_id );
1314 ber_str2bv( row.cols[ 1 ], 0, 1, &c_id->eid_keyval );
1315 #else /* ! BACKSQL_ARBITRARY_KEY */
1316 c_id->eid_id = strtol( row.cols[ 0 ], NULL, 0 );
1317 c_id->eid_keyval = strtol( row.cols[ 1 ], NULL, 0 );
1318 #endif /* ! BACKSQL_ARBITRARY_KEY */
1319 c_id->eid_oc_id = bsi->bsi_oc->bom_id;
1321 if ( dn.bv_val == row.cols[ 3 ] ) {
1322 ber_dupbv( &c_id->eid_dn, &dn );
1327 c_id->eid_next = bsi->bsi_id_list;
1328 bsi->bsi_id_list = c_id;
1329 bsi->bsi_n_candidates--;
1331 #ifdef BACKSQL_ARBITRARY_KEY
1332 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1333 "added entry id=%s, keyval=%s dn=\"%s\"\n",
1334 c_id->eid_id.bv_val, c_id->eid_keyval.bv_val,
1336 #else /* ! BACKSQL_ARBITRARY_KEY */
1337 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1338 "added entry id=%ld, keyval=%ld dn=\"%s\"\n",
1339 c_id->eid_id, c_id->eid_keyval, row.cols[ 3 ] );
1340 #endif /* ! BACKSQL_ARBITRARY_KEY */
1342 if ( bsi->bsi_n_candidates == -1 ) {
1346 backsql_FreeRow( &row );
1347 SQLFreeStmt( sth, SQL_DROP );
1349 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates(): %d\n",
1350 n_candidates - bsi->bsi_n_candidates, 0, 0 );
1352 return ( bsi->bsi_n_candidates == -1 ? BACKSQL_AVL_STOP : BACKSQL_AVL_CONTINUE );
1356 backsql_search( Operation *op, SlapReply *rs )
1358 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
1363 time_t stoptime = 0;
1364 backsql_srch_info srch_info;
1365 backsql_entryID *eid = NULL;
1368 manageDSAit = get_manageDSAit( op );
1370 Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
1371 "base=\"%s\", filter=\"%s\", scope=%d,",
1372 op->o_req_ndn.bv_val,
1373 op->ors_filterstr.bv_val,
1375 Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
1376 "attributes to load: %s\n",
1379 op->ors_attrs == NULL ? "all" : "custom list" );
1381 if ( op->o_req_ndn.bv_len > BACKSQL_MAX_DN_LEN ) {
1382 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1383 "search base length (%ld) exceeds max length (%ld)\n",
1384 op->o_req_ndn.bv_len, BACKSQL_MAX_DN_LEN, 0 );
1386 * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
1387 * since it is impossible that such a long DN exists
1390 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1391 send_ldap_result( op, rs );
1395 sres = backsql_get_db_conn( op, &dbh );
1396 if ( sres != LDAP_SUCCESS ) {
1397 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1398 "could not get connection handle - exiting\n",
1401 rs->sr_text = sres == LDAP_OTHER ? "SQL-backend error" : NULL;
1402 send_ldap_result( op, rs );
1406 /* compute it anyway; root does not use it */
1407 stoptime = op->o_time + op->ors_tlimit;
1409 base = op->o_req_dn;
1410 if ( backsql_api_dn2odbc( op, rs, &base ) ) {
1411 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1412 "backsql_api_dn2odbc failed\n",
1414 rs->sr_err = LDAP_OTHER;
1415 rs->sr_text = "SQL-backend error";
1416 send_ldap_result( op, rs );
1420 backsql_init_search( &srch_info, &base,
1422 op->ors_slimit, op->ors_tlimit,
1423 stoptime, op->ors_filter,
1424 dbh, op, rs, op->ors_attrs );
1427 * for each objectclass we try to construct query which gets IDs
1428 * of entries matching LDAP query filter and scope (or at least
1429 * candidates), and get the IDs
1431 srch_info.bsi_n_candidates =
1432 ( op->ors_limit == NULL /* isroot == FALSE */ ? -2 :
1433 ( op->ors_limit->lms_s_unchecked == -1 ? -2 :
1434 ( op->ors_limit->lms_s_unchecked ) ) );
1435 avl_apply( bi->oc_by_oc, backsql_oc_get_candidates,
1436 &srch_info, BACKSQL_AVL_STOP, AVL_INORDER );
1437 if ( op->ors_limit != NULL /* isroot == TRUE */
1438 && op->ors_limit->lms_s_unchecked != -1
1439 && srch_info.bsi_n_candidates == -1 )
1441 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1442 send_ldap_result( op, rs );
1447 * now we load candidate entries (only those attributes
1448 * mentioned in attrs and filter), test it against full filter
1449 * and then send to client
1451 for ( eid = srch_info.bsi_id_list; eid != NULL;
1452 eid = backsql_free_entryID( eid, 1 ) ) {
1453 Attribute *hasSubordinate = NULL,
1456 /* check for abandon */
1457 if ( op->o_abandon ) {
1461 /* check time limit */
1462 if ( op->ors_tlimit != SLAP_NO_LIMIT
1463 && slap_get_time() > stoptime )
1465 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1466 rs->sr_ctrls = NULL;
1467 rs->sr_ref = rs->sr_v2ref;
1468 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS
1470 send_ldap_result( op, rs );
1474 #ifdef BACKSQL_ARBITRARY_KEY
1475 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1476 "for entry id=%s, oc_id=%ld, keyval=%s\n",
1477 eid->eid_id.bv_val, eid->eid_oc_id,
1478 eid->eid_keyval.bv_val );
1479 #else /* ! BACKSQL_ARBITRARY_KEY */
1480 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1481 "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
1482 eid->eid_id, eid->eid_oc_id, eid->eid_keyval );
1483 #endif /* ! BACKSQL_ARBITRARY_KEY */
1485 entry = (Entry *)ch_calloc( sizeof( Entry ), 1 );
1486 res = backsql_id2entry( &srch_info, entry, eid );
1487 if ( res == NULL ) {
1488 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1489 "error in backsql_id2entry() "
1490 "- skipping entry\n", 0, 0, 0 );
1494 if ( !manageDSAit &&
1495 op->ors_scope != LDAP_SCOPE_BASE &&
1496 op->ors_scope != BACKSQL_SCOPE_BASE_LIKE &&
1497 is_entry_referral( entry ) ) {
1499 struct berval matched_dn;
1501 ber_dupbv( &matched_dn, &entry->e_name );
1502 refs = get_entry_referrals( op, entry );
1504 rs->sr_ref = referral_rewrite( refs,
1505 &matched_dn, &op->o_req_dn,
1507 ber_bvarray_free( refs );
1510 if ( !rs->sr_ref ) {
1511 rs->sr_text = "bad_referral object";
1514 rs->sr_err = LDAP_REFERRAL;
1515 rs->sr_matched = matched_dn.bv_val;
1516 send_search_reference( op, rs );
1518 ber_bvarray_free( rs->sr_ref );
1520 ber_memfree( matched_dn.bv_val );
1521 rs->sr_matched = NULL;
1527 * We use this flag since we need to parse the filter
1528 * anyway; we should have used the frontend API function
1529 * filter_has_subordinates()
1531 if ( srch_info.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
1534 rc = backsql_has_children( bi, dbh, &entry->e_nname );
1537 case LDAP_COMPARE_TRUE:
1538 case LDAP_COMPARE_FALSE:
1539 hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
1540 if ( hasSubordinate != NULL ) {
1541 for ( a = entry->e_attrs;
1545 a->a_next = hasSubordinate;
1551 Debug(LDAP_DEBUG_TRACE,
1552 "backsql_search(): "
1553 "has_children failed( %d)\n",
1564 if ( test_filter( op, entry, op->ors_filter )
1565 == LDAP_COMPARE_TRUE ) {
1566 if ( hasSubordinate && !( srch_info.bsi_flags & BSQL_SF_ALL_OPER )
1567 && !ad_inlist( slap_schema.si_ad_hasSubordinates, op->ors_attrs ) ) {
1569 attr_free( hasSubordinate );
1570 hasSubordinate = NULL;
1573 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
1579 rs->sr_attrs = op->ors_attrs;
1580 rs->sr_entry = entry;
1581 rs->sr_flags = REP_ENTRY_MODIFIABLE;
1582 sres = send_search_entry( op, rs );
1583 rs->sr_entry = NULL;
1584 rs->sr_attrs = NULL;
1592 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1593 "connection lost\n", 0, 0, 0 );
1598 * FIXME: send_search_entry failed;
1604 entry_free( entry );
1606 if ( op->ors_slimit != SLAP_NO_LIMIT
1607 && rs->sr_nentries >= op->ors_slimit )
1609 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1610 send_ldap_result( op, rs );
1617 if ( rs->sr_nentries > 0 ) {
1618 rs->sr_ref = rs->sr_v2ref;
1619 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS
1622 rs->sr_err = srch_info.bsi_status;
1624 send_ldap_result( op, rs );
1626 if ( rs->sr_v2ref ) {
1627 ber_bvarray_free( rs->sr_v2ref );
1628 rs->sr_v2ref = NULL;
1632 ch_free( srch_info.bsi_attrs );
1633 if ( base.bv_val != op->o_req_ndn.bv_val ) {
1634 ch_free( base.bv_val );
1637 Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
1641 #endif /* SLAPD_SQL */