2 * Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
4 * Redistribution and use in source and binary forms are permitted only
5 * as authorized by the OpenLDAP Public License. A copy of this
6 * license is available at http://www.OpenLDAP.org/license.html or
7 * in file LICENSE in the top-level directory of the distribution.
15 #include <sys/types.h>
16 #include "ac/string.h"
22 #include "schema-map.h"
26 #define BACKSQL_STOP 0
27 #define BACKSQL_CONTINUE 1
29 static int backsql_process_filter( backsql_srch_info *bsi, Filter *f );
32 backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad )
35 AttributeName *an = NULL;
37 if ( bsi->attrs == NULL ) {
42 * clear the list (retrieve all attrs)
45 ch_free( bsi->attrs );
50 for ( ; bsi->attrs[ n_attrs ].an_name.bv_val; n_attrs++ ) {
51 an = &bsi->attrs[ n_attrs ];
53 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
54 "attribute '%s' is in list\n",
55 an->an_name.bv_val, 0, 0 );
57 * We can live with strcmp because the attribute
58 * list has been normalized before calling be_search
60 if ( !BACKSQL_NCMP( &an->an_name, &ad->ad_cname ) ) {
65 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
66 "adding '%s' to list\n", ad->ad_cname.bv_val, 0, 0 );
68 an = (AttributeName *)ch_realloc( bsi->attrs,
69 sizeof( AttributeName ) * ( n_attrs + 2 ) );
74 an[ n_attrs ].an_name = ad->ad_cname;
75 an[ n_attrs ].an_desc = ad;
76 an[ n_attrs + 1 ].an_name.bv_val = NULL;
77 an[ n_attrs + 1 ].an_name.bv_len = 0;
86 backsql_srch_info *bsi,
98 AttributeName *attrs )
102 bsi->base_dn = nbase;
104 bsi->slimit = slimit;
105 bsi->tlimit = tlimit;
106 bsi->filter = filter;
116 if ( attrs == NULL || an_find( attrs, &AllUser ) ) {
120 bsi->attrs = (AttributeName *)ch_calloc( 1,
121 sizeof( AttributeName ) );
122 bsi->attrs[ 0 ].an_name.bv_val = NULL;
123 bsi->attrs[ 0 ].an_name.bv_len = 0;
125 for ( p = attrs; p->an_name.bv_val; p++ ) {
127 * ignore "1.1"; handle "+"
129 if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
130 bsi->bsi_flags |= BSQL_SF_ALL_OPER;
133 } else if ( BACKSQL_NCMP( &p->an_name, &NoAttrs ) == 0 ) {
137 backsql_attrlist_add( bsi, p->an_desc );
143 bsi->n_candidates = 0;
144 bsi->stoptime = stoptime;
146 bsi->sel.bv_val = NULL;
149 bsi->from.bv_val = NULL;
150 bsi->from.bv_len = 0;
152 bsi->join_where.bv_val = NULL;
153 bsi->join_where.bv_len = 0;
155 bsi->flt_where.bv_val = NULL;
156 bsi->flt_where.bv_len = 0;
159 bsi->status = LDAP_SUCCESS;
163 backsql_process_filter_list( backsql_srch_info *bsi, Filter *f, int op )
171 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c", '(' /* ) */ );
174 res = backsql_process_filter( bsi, f );
177 * TimesTen : If the query has no answers,
178 * don't bother to run the query.
189 case LDAP_FILTER_AND:
190 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
191 (ber_len_t)sizeof( " AND " ) - 1,
196 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
197 (ber_len_t)sizeof( " OR " ) - 1,
203 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c", /* ( */ ')' );
209 backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f )
212 backsql_at_map_rec *at;
218 at = backsql_ad2at( bsi->oc, f->f_sub_desc );
223 * When dealing with case-sensitive strings
224 * we may omit normalization; however, normalized
225 * SQL filters are more liberal.
228 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c", '(' /* ) */ );
231 Debug( LDAP_DEBUG_TRACE, "expr: '%s' '%s'\n", at->sel_expr.bv_val,
232 at->sel_expr_u.bv_val ? at->sel_expr_u.bv_val : "<NULL>", 0 );
233 if ( bsi->bi->upper_func.bv_val ) {
235 * If a pre-upper-cased version of the column exists, use it
237 if ( at->sel_expr_u.bv_val ) {
238 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
241 (ber_len_t)sizeof( " LIKE '" ) - 1,
244 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
246 &bsi->bi->upper_func,
250 (ber_len_t)sizeof( " LIKE '" ) - 1,
254 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "bl",
256 (ber_len_t)sizeof( " LIKE '" ) - 1, " LIKE '" );
259 if ( f->f_sub_initial.bv_val != NULL ) {
262 start = bsi->flt_where.bv_len;
263 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "b",
265 if ( bsi->bi->upper_func.bv_val ) {
266 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
270 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c", '%' );
272 if ( f->f_sub_any != NULL ) {
273 for ( i = 0; f->f_sub_any[ i ].bv_val != NULL; i++ ) {
277 Debug( LDAP_DEBUG_TRACE,
278 "==>backsql_process_sub_filter(): "
279 "sub_any='%s'\n", f->f_sub_any[ i ].bv_val,
281 #endif /* BACKSQL_TRACE */
283 start = bsi->flt_where.bv_len;
284 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
288 if ( bsi->bi->upper_func.bv_val ) {
290 * Note: toupper('%') = '%'
292 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
296 if ( f->f_sub_final.bv_val != NULL ) {
299 start = bsi->flt_where.bv_len;
300 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "b",
302 if ( bsi->bi->upper_func.bv_val ) {
303 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
308 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
309 (ber_len_t)sizeof( /* (' */ "')" ) - 1, /* ( */ "')" );
315 backsql_process_filter( backsql_srch_info *bsi, Filter *f )
317 backsql_at_map_rec *at;
318 backsql_at_map_rec oc_attr = {
319 slap_schema.si_ad_objectClass, BER_BVC(""), BER_BVC(""),
320 BER_BVNULL, NULL, NULL, NULL };
321 AttributeDescription *ad = NULL;
326 struct berval *filter_value = NULL;
328 Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n", 0, 0, 0 );
329 if ( f == NULL || f->f_choice == SLAPD_FILTER_COMPUTED ) {
333 switch( f->f_choice ) {
335 rc = backsql_process_filter_list( bsi, f->f_or,
340 case LDAP_FILTER_AND:
341 rc = backsql_process_filter_list( bsi, f->f_and,
346 case LDAP_FILTER_NOT:
347 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
348 (ber_len_t)sizeof( "NOT (" /* ) */ ) - 1,
350 rc = backsql_process_filter( bsi, f->f_not );
351 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c",
356 case LDAP_FILTER_PRESENT:
360 case LDAP_FILTER_EXT:
361 ad = f->f_mra->ma_desc;
370 /* TimesTen : Don't run the query */
379 * Turn structuralObjectClass into objectClass
381 if ( ad == slap_schema.si_ad_objectClass
382 || ad == slap_schema.si_ad_structuralObjectClass ) {
384 backsql_strfcat( &at->sel_expr, &len, "cbc",
386 &bsi->oc->oc->soc_cname,
389 #if defined(SLAP_X_FILTER_HASSUBORDINATES) || defined(SLAP_X_MRA_MATCH_DNATTRS)
390 } else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) {
392 * FIXME: this is not robust; e.g. a filter
393 * '(!(hasSubordinates=TRUE))' fails because
394 * in SQL it would read 'NOT (1=1)' instead
396 * Note however that hasSubordinates is boolean,
397 * so a more appropriate filter would be
398 * '(hasSubordinates=FALSE)'
400 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
401 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
404 * We use this flag since we need to parse
405 * the filter anyway; we should have used
406 * the frontend API function
407 * filter_has_subordinates()
409 bsi->bsi_flags |= BSQL_SF_FILTER_HASSUBORDINATE;
412 * clear attributes to fetch, to require ALL
413 * and try extended match on all attributes
415 backsql_attrlist_add( bsi, NULL );
418 #endif /* SLAP_X_FILTER_HASSUBORDINATES || SLAP_X_MRA_MATCH_DNATTRS */
421 at = backsql_ad2at( bsi->oc, ad );
425 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
426 "attribute '%s' is not defined for objectclass '%s'\n",
427 ad->ad_cname.bv_val, BACKSQL_OC_NAME( bsi->oc ), 0 );
428 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
429 (ber_len_t)sizeof( "1=0" ) - 1, "1=0" );
433 backsql_merge_from_clause( &bsi->from, &bsi->from_len,
436 * need to add this attribute to list of attrs to load,
437 * so that we could do test_filter() later
439 backsql_attrlist_add( bsi, ad );
441 if ( at->join_where.bv_val != NULL
442 && strstr( bsi->join_where.bv_val, at->join_where.bv_val ) == NULL ) {
443 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lb",
444 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
450 * FIXME: this is not required any more; however, note that
451 * attribute name syntax might collide with SQL legal aliases
453 if ( at != &oc_attr ) {
454 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cblb",
457 (ber_len_t)sizeof( " AS " ) - 1, " AS ",
462 switch ( f->f_choice ) {
463 case LDAP_FILTER_EQUALITY:
464 filter_value = &f->f_av_value;
467 /* fail over next case */
469 case LDAP_FILTER_EXT:
470 filter_value = &f->f_mra->ma_value;
474 * maybe we should check type of at->sel_expr here somehow,
475 * to know whether upper_func is applicable, but for now
476 * upper_func stuff is made for Oracle, where UPPER is
477 * safely applicable to NUMBER etc.
479 if ( bsi->bi->upper_func.bv_val ) {
482 if ( at->sel_expr_u.bv_val ) {
483 backsql_strfcat( &bsi->flt_where,
484 &bsi->fwhere_len, "cbl",
487 (ber_len_t)sizeof( "='" ) - 1,
490 backsql_strfcat( &bsi->flt_where,
491 &bsi->fwhere_len, "cbcbl",
493 &bsi->bi->upper_func,
496 (ber_len_t)sizeof( /* ( */ ")='" ) - 1,
500 start = bsi->flt_where.bv_len;
502 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
505 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
508 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
511 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
515 (ber_len_t)sizeof( "='" ) - 1, "='",
517 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
524 * FIXME: should we uppercase the operands?
526 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
529 (ber_len_t)sizeof( ">=" ) - 1, ">=",
536 * FIXME: should we uppercase the operands?
538 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
541 (ber_len_t)sizeof( "<=" ) - 1, "<=",
546 case LDAP_FILTER_PRESENT:
547 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "lbl",
548 (ber_len_t)sizeof( "NOT (" ) - 1, "NOT (",
550 (ber_len_t)sizeof( " IS NULL)" ) - 1, " IS NULL)" );
553 case LDAP_FILTER_SUBSTRINGS:
554 backsql_process_sub_filter( bsi, f );
557 case LDAP_FILTER_APPROX:
561 * maybe we should check type of at->sel_expr here somehow,
562 * to know whether upper_func is applicable, but for now
563 * upper_func stuff is made for Oracle, where UPPER is
564 * safely applicable to NUMBER etc.
566 if ( bsi->bi->upper_func.bv_val ) {
569 if ( at->sel_expr_u.bv_val ) {
570 backsql_strfcat( &bsi->flt_where,
571 &bsi->fwhere_len, "cbl",
574 (ber_len_t)sizeof( " LIKE '%" ) - 1,
577 backsql_strfcat( &bsi->flt_where,
578 &bsi->fwhere_len, "cbcbl",
580 &bsi->bi->upper_func,
583 (ber_len_t)sizeof( /* ( */ ") LIKE '%" ) - 1,
584 /* ( */ ") LIKE '%" );
587 start = bsi->flt_where.bv_len;
589 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
592 (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
595 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
598 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
602 (ber_len_t)sizeof( " LIKE '%" ) - 1,
605 (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
611 /* unhandled filter type; should not happen */
613 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
614 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
620 if ( oc_attr.sel_expr.bv_val != NULL ) {
621 free( oc_attr.sel_expr.bv_val );
624 Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter()\n", 0, 0, 0 );
628 if ( oc_attr.sel_expr.bv_val != NULL ) {
629 free( oc_attr.sel_expr.bv_val );
631 Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter() returns -1\n",
637 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
639 backsql_info *bi = (backsql_info *)bsi->be->be_private;
644 query->bv_val = NULL;
647 Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
648 bsi->sel.bv_val = NULL;
651 bsi->from.bv_val = NULL;
652 bsi->from.bv_len = 0;
654 bsi->join_where.bv_val = NULL;
655 bsi->join_where.bv_len = 0;
657 bsi->flt_where.bv_val = NULL;
658 bsi->flt_where.bv_len = 0;
663 * FIXME: this query has been split in case a string cast function
664 * is defined; more sophisticated (pattern based) function should
667 backsql_strcat( &bsi->sel, &bsi->sel_len,
668 "SELECT DISTINCT ldap_entries.id,",
669 bsi->oc->keytbl.bv_val, ".", bsi->oc->keycol.bv_val,
670 ",'", bsi->oc->name.bv_val, "' AS objectClass",
671 ",ldap_entries.dn AS dn", NULL );
674 backsql_strfcat( &bsi->sel, &bsi->sel_len, "lbcbc",
675 (ber_len_t)sizeof( "SELECT DISTINCT ldap_entries.id," ) - 1,
676 "SELECT DISTINCT ldap_entries.id,",
682 if ( bi->strcast_func.bv_val ) {
683 backsql_strfcat( &bsi->sel, &bsi->sel_len, "blbl",
685 (ber_len_t)sizeof( "('" /* ') */ ) - 1,
687 &bsi->oc->oc->soc_cname,
688 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
691 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cbc",
693 &bsi->oc->oc->soc_cname,
696 backsql_strfcat( &bsi->sel, &bsi->sel_len, "l",
697 (ber_len_t)sizeof( " AS objectClass,ldap_entries.dn AS dn" ) - 1,
698 " AS objectClass,ldap_entries.dn AS dn" );
700 backsql_strfcat( &bsi->from, &bsi->from_len, "lb",
701 (ber_len_t)sizeof( " FROM ldap_entries," ) - 1,
702 " FROM ldap_entries,",
705 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lbcbl",
706 (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ",
710 (ber_len_t)sizeof( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ) - 1,
711 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
713 switch ( bsi->scope ) {
714 case LDAP_SCOPE_BASE:
715 if ( bsi->bi->upper_func.bv_val ) {
716 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
718 &bsi->bi->upper_func,
719 (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
720 "(ldap_entries.dn)=",
721 &bsi->bi->upper_func_open,
723 &bsi->bi->upper_func_close );
725 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
727 (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
728 "ldap_entries.dn=?" );
732 case LDAP_SCOPE_ONELEVEL:
733 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "l",
734 (ber_len_t)sizeof( "ldap_entries.parent=?" ) - 1,
735 "ldap_entries.parent=?" );
738 case LDAP_SCOPE_SUBTREE:
739 if ( bsi->bi->upper_func.bv_val ) {
740 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
742 &bsi->bi->upper_func,
743 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
744 "(ldap_entries.dn) LIKE ",
745 &bsi->bi->upper_func_open,
747 &bsi->bi->upper_func_close );
749 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
751 (ber_len_t)sizeof( "ldap_entries.dn LIKE ?" ) - 1,
752 "ldap_entries.dn LIKE ?" );
756 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "b",
757 &bsi->bi->subtree_cond );
765 rc = backsql_process_filter( bsi, bsi->filter );
767 backsql_strfcat( query, &q_len, "bbblb",
771 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
774 } else if ( rc < 0 ) {
776 * Indicates that there's no possible way the filter matches
777 * anything. No need to issue the query
779 Debug( LDAP_DEBUG_TRACE,
780 "<==backsql_srch_query() returns NULL\n", 0, 0, 0 );
781 free( query->bv_val );
782 query->bv_val = NULL;
785 free( bsi->sel.bv_val );
788 free( bsi->from.bv_val );
789 bsi->from.bv_len = 0;
791 free( bsi->join_where.bv_val );
792 bsi->join_where.bv_len = 0;
794 free( bsi->flt_where.bv_val );
795 bsi->flt_where.bv_len = 0;
798 Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query()\n", 0, 0, 0 );
800 return ( query->bv_val == NULL ? 1 : 0 );
804 backsql_oc_get_candidates( backsql_oc_map_rec *oc, backsql_srch_info *bsi )
809 backsql_entryID base_id, *c_id;
815 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc='%s'\n",
816 BACKSQL_OC_NAME( oc ), 0, 0 );
818 if ( bsi->n_candidates == -1 ) {
819 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
820 "unchecked limit has been overcome\n", 0, 0, 0 );
821 /* should never get here */
827 if ( backsql_srch_query( bsi, &query ) ) {
828 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
829 "could not construct query for objectclass\n",
831 return BACKSQL_CONTINUE;
834 Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n",
835 query.bv_val, 0, 0 );
837 rc = backsql_Prepare( bsi->dbh, &sth, query.bv_val, 0 );
838 free( query.bv_val );
839 if ( rc != SQL_SUCCESS ) {
840 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
841 "error preparing query\n", 0, 0, 0 );
842 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
843 return BACKSQL_CONTINUE;
846 if ( backsql_BindParamID( sth, 1, &bsi->oc->id ) != SQL_SUCCESS ) {
847 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
848 "error binding objectclass id parameter\n", 0, 0, 0 );
849 return BACKSQL_CONTINUE;
852 switch ( bsi->scope ) {
853 case LDAP_SCOPE_BASE:
854 rc = backsql_BindParamStr( sth, 2, bsi->base_dn->bv_val,
855 BACKSQL_MAX_DN_LEN );
856 if ( rc != SQL_SUCCESS ) {
857 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
858 "error binding base_dn parameter\n", 0, 0, 0 );
859 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh,
861 return BACKSQL_CONTINUE;
865 case LDAP_SCOPE_SUBTREE: {
868 * + 1 because we need room for '%'; this makes a subtree
869 * search for a DN BACKSQL_MAX_DN_LEN long legal
870 * if it returns that DN only
872 char temp_base_dn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
875 * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
876 * however this should be handled earlier
878 assert( bsi->base_dn->bv_len <= BACKSQL_MAX_DN_LEN );
881 * Sets the parameters for the SQL built earlier
882 * NOTE that all the databases could actually use
883 * the TimesTen version, which would be cleaner
884 * and would also eliminate the need for the
885 * subtree_cond line in the configuration file.
886 * For now, I'm leaving it the way it is,
887 * so non-TimesTen databases use the original code.
888 * But at some point this should get cleaned up.
890 * If "dn" is being used, do a suffix search.
891 * If "dn_ru" is being used, do a prefix search.
893 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bsi->bi ) ) {
894 temp_base_dn[ 0 ] = '\0';
895 for ( i = 0, j = bsi->base_dn->bv_len - 1;
897 temp_base_dn[ i ] = bsi->base_dn->bv_val[ j ];
899 temp_base_dn[ i ] = '%';
900 temp_base_dn[ i + 1 ] = '\0';
901 ldap_pvt_str2upper( temp_base_dn );
904 temp_base_dn[ 0 ] = '%';
905 AC_MEMCPY( &temp_base_dn[ 1 ], bsi->base_dn->bv_val,
906 bsi->base_dn->bv_len + 1 );
907 ldap_pvt_str2upper( &temp_base_dn[ 1 ] );
910 Debug( LDAP_DEBUG_TRACE, "dn '%s'\n", temp_base_dn, 0, 0 );
912 rc = backsql_BindParamStr( sth, 2, temp_base_dn,
913 BACKSQL_MAX_DN_LEN );
914 if ( rc != SQL_SUCCESS ) {
915 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
916 "error binding base_dn parameter (2)\n",
918 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh,
920 return BACKSQL_CONTINUE;
925 case LDAP_SCOPE_ONELEVEL:
926 res = backsql_dn2id( bsi->bi, &base_id,
927 bsi->dbh, bsi->base_dn );
928 if ( res != LDAP_SUCCESS ) {
929 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
930 "could not retrieve base_dn id%s\n",
931 res == LDAP_NO_SUCH_OBJECT ? ": no such entry"
934 return BACKSQL_CONTINUE;
937 rc = backsql_BindParamID( sth, 2, &base_id.id );
938 backsql_free_entryID( &base_id, 0 );
939 if ( rc != SQL_SUCCESS ) {
940 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
941 "error binding base id parameter\n", 0, 0, 0 );
942 return BACKSQL_CONTINUE;
947 rc = SQLExecute( sth );
948 if ( !BACKSQL_SUCCESS( rc ) ) {
949 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
950 "error executing query\n", 0, 0, 0 );
951 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
952 SQLFreeStmt( sth, SQL_DROP );
953 return BACKSQL_CONTINUE;
956 backsql_BindRowAsStrings( sth, &row );
957 rc = SQLFetch( sth );
958 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
959 c_id = (backsql_entryID *)ch_calloc( 1,
960 sizeof( backsql_entryID ) );
961 c_id->id = strtol( row.cols[ 0 ], NULL, 0 );
962 c_id->keyval = strtol( row.cols[ 1 ], NULL, 0 );
963 c_id->oc_id = bsi->oc->id;
964 ber_str2bv( row.cols[ 3 ], 0, 1, &c_id->dn );
965 c_id->next = bsi->id_list;
969 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
970 "added entry id=%ld, keyval=%ld dn='%s'\n",
971 c_id->id, c_id->keyval, row.cols[ 3 ] );
973 if ( bsi->n_candidates == -1 ) {
977 backsql_FreeRow( &row );
978 SQLFreeStmt( sth, SQL_DROP );
980 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates()\n", 0, 0, 0 );
982 return ( bsi->n_candidates == -1 ? BACKSQL_STOP : BACKSQL_CONTINUE );
991 struct berval *nbase,
997 struct berval *filterstr,
998 AttributeName *attrs,
1001 backsql_info *bi = (backsql_info *)be->be_private;
1006 int manageDSAit = get_manageDSAit( op );
1007 BerVarray v2refs = NULL;
1008 time_t stoptime = 0;
1009 backsql_srch_info srch_info;
1010 backsql_entryID *eid = NULL;
1011 struct slap_limits_set *limit = NULL;
1014 Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
1015 "base='%s', filter='%s', scope=%d,",
1016 nbase->bv_val, filterstr->bv_val, scope );
1017 Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
1018 "attributes to load: %s\n",
1019 deref, attrsonly, attrs == NULL ? "all" : "custom list" );
1021 if ( nbase->bv_len > BACKSQL_MAX_DN_LEN ) {
1022 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1023 "search base length (%ld) exceeds max length (%ld)\n",
1024 nbase->bv_len, BACKSQL_MAX_DN_LEN, 0 );
1026 * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
1027 * since it is impossible that such a long DN exists
1030 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
1031 "", NULL, NULL, NULL );
1035 sres = backsql_get_db_conn( be, conn, &dbh );
1036 if ( sres != LDAP_SUCCESS ) {
1037 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1038 "could not get connection handle - exiting\n",
1040 send_ldap_result( conn, op, sres, "",
1041 sres == LDAP_OTHER ? "SQL-backend error" : "",
1046 /* TimesTen : Pass it along to the lower level routines */
1047 srch_info.use_reverse_dn = BACKSQL_USE_REVERSE_DN( bi );
1049 /* if not root, get appropriate limits */
1050 if ( be_isroot( be, &op->o_ndn ) ) {
1053 ( void ) get_limits( be, &op->o_ndn, &limit );
1056 /* The time/size limits come first because they require very little
1057 * effort, so there's no chance the candidates are selected and then
1058 * the request is not honored only because of time/size constraints */
1060 /* if no time limit requested, use soft limit (unless root!) */
1062 if ( tlimit == 0 ) {
1063 tlimit = -1; /* allow root to set no limit */
1066 if ( slimit == 0 ) {
1071 /* if no limit is required, use soft limit */
1072 if ( tlimit <= 0 ) {
1073 tlimit = limit->lms_t_soft;
1075 /* if requested limit higher than hard limit, abort */
1076 } else if ( tlimit > limit->lms_t_hard ) {
1077 /* no hard limit means use soft instead */
1078 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
1079 tlimit = limit->lms_t_soft;
1081 /* positive hard limit means abort */
1082 } else if ( limit->lms_t_hard > 0 ) {
1083 send_search_result( conn, op,
1084 LDAP_UNWILLING_TO_PERFORM,
1085 NULL, NULL, NULL, NULL, 0 );
1089 /* negative hard limit means no limit */
1092 /* if no limit is required, use soft limit */
1093 if ( slimit <= 0 ) {
1094 slimit = limit->lms_s_soft;
1096 /* if requested limit higher than hard limit, abort */
1097 } else if ( slimit > limit->lms_s_hard ) {
1098 /* no hard limit means use soft instead */
1099 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
1100 slimit = limit->lms_s_soft;
1102 /* positive hard limit means abort */
1103 } else if ( limit->lms_s_hard > 0 ) {
1104 send_search_result( conn, op,
1105 LDAP_UNWILLING_TO_PERFORM,
1106 NULL, NULL, NULL, NULL, 0 );
1110 /* negative hard limit means no limit */
1114 /* compute it anyway; root does not use it */
1115 stoptime = op->o_time + tlimit;
1117 backsql_init_search( &srch_info, bi, nbase, scope,
1118 slimit, tlimit, stoptime, filter, dbh,
1119 be, conn, op, attrs );
1122 * for each objectclass we try to construct query which gets IDs
1123 * of entries matching LDAP query filter and scope (or at least
1124 * candidates), and get the IDs
1126 srch_info.n_candidates = ( isroot ? -2 : limit->lms_s_unchecked == -1
1127 ? -2 : limit->lms_s_unchecked );
1128 avl_apply( bi->oc_by_oc, (AVL_APPLY)backsql_oc_get_candidates,
1129 &srch_info, BACKSQL_STOP, AVL_INORDER );
1130 if ( !isroot && limit->lms_s_unchecked != -1 ) {
1131 if ( srch_info.n_candidates == -1 ) {
1132 send_search_result( conn, op,
1133 LDAP_ADMINLIMIT_EXCEEDED,
1134 NULL, NULL, NULL, NULL, 0 );
1141 * now we load candidate entries (only those attributes
1142 * mentioned in attrs and filter), test it against full filter
1143 * and then send to client
1145 for ( eid = srch_info.id_list; eid != NULL;
1146 eid = backsql_free_entryID( eid, 1 ) ) {
1147 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1148 Attribute *hasSubordinate = NULL,
1150 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1152 /* check for abandon */
1153 if ( op->o_abandon ) {
1157 /* check time limit */
1158 if ( tlimit != -1 && slap_get_time() > stoptime ) {
1159 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
1160 NULL, NULL, v2refs, NULL, nentries );
1164 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1165 "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
1166 eid->id, eid->oc_id, eid->keyval );
1168 entry = (Entry *)ch_calloc( sizeof( Entry ), 1 );
1169 res = backsql_id2entry( &srch_info, entry, eid );
1170 if ( res == NULL ) {
1171 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1172 "error in backsql_id2entry() "
1173 "- skipping entry\n", 0, 0, 0 );
1177 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
1178 is_entry_referral( entry ) ) {
1179 BerVarray refs = get_entry_referrals( be, conn,
1182 send_search_reference( be, conn, op, entry, refs,
1184 ber_bvarray_free( refs );
1188 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1190 * We use this flag since we need to parse the filter
1191 * anyway; we should have used the frontend API function
1192 * filter_has_subordinates()
1194 if ( srch_info.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
1197 rc = backsql_has_children( bi, dbh, &entry->e_nname );
1200 case LDAP_COMPARE_TRUE:
1201 case LDAP_COMPARE_FALSE:
1202 hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
1203 if ( hasSubordinate != NULL ) {
1204 for ( a = entry->e_attrs;
1208 a->a_next = hasSubordinate;
1214 Debug(LDAP_DEBUG_TRACE,
1215 "backsql_search(): "
1216 "has_children failed( %d)\n",
1226 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1228 if ( test_filter( be, conn, op, entry, filter )
1229 == LDAP_COMPARE_TRUE ) {
1230 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1231 if ( hasSubordinate && !( srch_info.bsi_flags & BSQL_SF_ALL_OPER )
1232 && !ad_inlist( slap_schema.si_ad_hasSubordinates, attrs ) ) {
1234 attr_free( hasSubordinate );
1235 hasSubordinate = NULL;
1237 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1239 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
1244 sres = send_search_entry( be, conn, op, entry,
1245 attrs, attrsonly, NULL );
1256 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1257 "connection lost\n", 0, 0, 0 );
1262 * FIXME: send_search_entry failed;
1268 entry_free( entry );
1270 if ( slimit != -1 && nentries >= slimit ) {
1271 send_search_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
1272 NULL, NULL, v2refs, NULL, nentries );
1279 if ( nentries > 0 ) {
1280 send_search_result( conn, op,
1281 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
1282 NULL, NULL, v2refs, NULL, nentries );
1284 send_ldap_result( conn, op, srch_info.status,
1285 NULL, NULL, NULL, 0 );
1289 ch_free( srch_info.attrs );
1291 Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
1295 #endif /* SLAPD_SQL */