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" );
402 if ( ad == slap_schema.si_ad_hasSubordinates ) {
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;
413 * clear attributes to fetch, to require ALL
414 * and try extended match on all attributes
416 backsql_attrlist_add( bsi, NULL );
419 #endif /* SLAP_X_FILTER_HASSUBORDINATES || SLAP_X_MRA_MATCH_DNATTRS */
422 at = backsql_ad2at( bsi->oc, ad );
426 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
427 "attribute '%s' is not defined for objectclass '%s'\n",
428 ad->ad_cname.bv_val, BACKSQL_OC_NAME( bsi->oc ), 0 );
429 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
430 (ber_len_t)sizeof( "1=0" ) - 1, "1=0" );
434 backsql_merge_from_clause( &bsi->from, &bsi->from_len,
437 * need to add this attribute to list of attrs to load,
438 * so that we could do test_filter() later
440 backsql_attrlist_add( bsi, ad );
442 if ( at->join_where.bv_val != NULL
443 && strstr( bsi->join_where.bv_val, at->join_where.bv_val ) == NULL ) {
444 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lb",
445 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
451 * FIXME: this is not required any more; however, note that
452 * attribute name syntax might collide with SQL legal aliases
454 if ( at != &oc_attr ) {
455 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cblb",
458 (ber_len_t)sizeof( " AS " ) - 1, " AS ",
463 switch ( f->f_choice ) {
464 case LDAP_FILTER_EQUALITY:
465 filter_value = &f->f_av_value;
468 /* fail over next case */
470 case LDAP_FILTER_EXT:
471 filter_value = &f->f_mra->ma_value;
475 * maybe we should check type of at->sel_expr here somehow,
476 * to know whether upper_func is applicable, but for now
477 * upper_func stuff is made for Oracle, where UPPER is
478 * safely applicable to NUMBER etc.
480 if ( bsi->bi->upper_func.bv_val ) {
483 if ( at->sel_expr_u.bv_val ) {
484 backsql_strfcat( &bsi->flt_where,
485 &bsi->fwhere_len, "cbl",
488 (ber_len_t)sizeof( "='" ) - 1,
491 backsql_strfcat( &bsi->flt_where,
492 &bsi->fwhere_len, "cbcbl",
494 &bsi->bi->upper_func,
497 (ber_len_t)sizeof( /* ( */ ")='" ) - 1,
501 start = bsi->flt_where.bv_len;
503 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
506 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
509 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
512 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
516 (ber_len_t)sizeof( "='" ) - 1, "='",
518 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
525 * FIXME: should we uppercase the operands?
527 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
530 (ber_len_t)sizeof( ">=" ) - 1, ">=",
537 * FIXME: should we uppercase the operands?
539 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
542 (ber_len_t)sizeof( "<=" ) - 1, "<=",
547 case LDAP_FILTER_PRESENT:
548 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "lbl",
549 (ber_len_t)sizeof( "NOT (" ) - 1, "NOT (",
551 (ber_len_t)sizeof( " IS NULL)" ) - 1, " IS NULL)" );
554 case LDAP_FILTER_SUBSTRINGS:
555 backsql_process_sub_filter( bsi, f );
558 case LDAP_FILTER_APPROX:
562 * maybe we should check type of at->sel_expr here somehow,
563 * to know whether upper_func is applicable, but for now
564 * upper_func stuff is made for Oracle, where UPPER is
565 * safely applicable to NUMBER etc.
567 if ( bsi->bi->upper_func.bv_val ) {
570 if ( at->sel_expr_u.bv_val ) {
571 backsql_strfcat( &bsi->flt_where,
572 &bsi->fwhere_len, "cbl",
575 (ber_len_t)sizeof( " LIKE '%" ) - 1,
578 backsql_strfcat( &bsi->flt_where,
579 &bsi->fwhere_len, "cbcbl",
581 &bsi->bi->upper_func,
584 (ber_len_t)sizeof( /* ( */ ") LIKE '%" ) - 1,
585 /* ( */ ") LIKE '%" );
588 start = bsi->flt_where.bv_len;
590 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
593 (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
596 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
599 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
603 (ber_len_t)sizeof( " LIKE '%" ) - 1,
606 (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
612 /* unhandled filter type; should not happen */
614 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
615 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
621 if ( oc_attr.sel_expr.bv_val != NULL ) {
622 free( oc_attr.sel_expr.bv_val );
625 Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter()\n", 0, 0, 0 );
629 if ( oc_attr.sel_expr.bv_val != NULL ) {
630 free( oc_attr.sel_expr.bv_val );
632 Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter() returns -1\n",
638 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
640 backsql_info *bi = (backsql_info *)bsi->be->be_private;
645 query->bv_val = NULL;
648 Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
649 bsi->sel.bv_val = NULL;
652 bsi->from.bv_val = NULL;
653 bsi->from.bv_len = 0;
655 bsi->join_where.bv_val = NULL;
656 bsi->join_where.bv_len = 0;
658 bsi->flt_where.bv_val = NULL;
659 bsi->flt_where.bv_len = 0;
664 * FIXME: this query has been split in case a string cast function
665 * is defined; more sophisticated (pattern based) function should
668 backsql_strcat( &bsi->sel, &bsi->sel_len,
669 "SELECT DISTINCT ldap_entries.id,",
670 bsi->oc->keytbl.bv_val, ".", bsi->oc->keycol.bv_val,
671 ",'", bsi->oc->name.bv_val, "' AS objectClass",
672 ",ldap_entries.dn AS dn", NULL );
675 backsql_strfcat( &bsi->sel, &bsi->sel_len, "lbcbc",
676 (ber_len_t)sizeof( "SELECT DISTINCT ldap_entries.id," ) - 1,
677 "SELECT DISTINCT ldap_entries.id,",
683 if ( bi->strcast_func.bv_val ) {
684 backsql_strfcat( &bsi->sel, &bsi->sel_len, "blbl",
686 (ber_len_t)sizeof( "('" /* ') */ ) - 1,
688 &bsi->oc->oc->soc_cname,
689 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
692 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cbc",
694 &bsi->oc->oc->soc_cname,
697 backsql_strfcat( &bsi->sel, &bsi->sel_len, "l",
698 (ber_len_t)sizeof( " AS objectClass,ldap_entries.dn AS dn" ) - 1,
699 " AS objectClass,ldap_entries.dn AS dn" );
701 backsql_strfcat( &bsi->from, &bsi->from_len, "lb",
702 (ber_len_t)sizeof( " FROM ldap_entries," ) - 1,
703 " FROM ldap_entries,",
706 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lbcbl",
707 (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ",
711 (ber_len_t)sizeof( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ) - 1,
712 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
714 switch ( bsi->scope ) {
715 case LDAP_SCOPE_BASE:
716 if ( bsi->bi->upper_func.bv_val ) {
717 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
719 &bsi->bi->upper_func,
720 (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
721 "(ldap_entries.dn)=",
722 &bsi->bi->upper_func_open,
724 &bsi->bi->upper_func_close );
726 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
728 (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
729 "ldap_entries.dn=?" );
733 case LDAP_SCOPE_ONELEVEL:
734 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "l",
735 (ber_len_t)sizeof( "ldap_entries.parent=?" ) - 1,
736 "ldap_entries.parent=?" );
739 case LDAP_SCOPE_SUBTREE:
740 if ( bsi->bi->upper_func.bv_val ) {
741 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
743 &bsi->bi->upper_func,
744 (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
745 "(ldap_entries.dn) LIKE ",
746 &bsi->bi->upper_func_open,
748 &bsi->bi->upper_func_close );
750 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
752 (ber_len_t)sizeof( "ldap_entries.dn LIKE ?" ) - 1,
753 "ldap_entries.dn LIKE ?" );
757 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "b",
758 &bsi->bi->subtree_cond );
766 rc = backsql_process_filter( bsi, bsi->filter );
768 backsql_strfcat( query, &q_len, "bbblb",
772 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
775 } else if ( rc < 0 ) {
777 * Indicates that there's no possible way the filter matches
778 * anything. No need to issue the query
780 Debug( LDAP_DEBUG_TRACE,
781 "<==backsql_srch_query() returns NULL\n", 0, 0, 0 );
782 free( query->bv_val );
783 query->bv_val = NULL;
786 free( bsi->sel.bv_val );
789 free( bsi->from.bv_val );
790 bsi->from.bv_len = 0;
792 free( bsi->join_where.bv_val );
793 bsi->join_where.bv_len = 0;
795 free( bsi->flt_where.bv_val );
796 bsi->flt_where.bv_len = 0;
799 Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query()\n", 0, 0, 0 );
801 return ( query->bv_val == NULL ? 1 : 0 );
805 backsql_oc_get_candidates( backsql_oc_map_rec *oc, backsql_srch_info *bsi )
810 backsql_entryID base_id, *c_id;
816 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc='%s'\n",
817 BACKSQL_OC_NAME( oc ), 0, 0 );
819 if ( bsi->n_candidates == -1 ) {
820 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
821 "unchecked limit has been overcome\n", 0, 0, 0 );
822 /* should never get here */
828 if ( backsql_srch_query( bsi, &query ) ) {
829 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
830 "could not construct query for objectclass\n",
832 return BACKSQL_CONTINUE;
835 Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n",
836 query.bv_val, 0, 0 );
838 rc = backsql_Prepare( bsi->dbh, &sth, query.bv_val, 0 );
839 free( query.bv_val );
840 if ( rc != SQL_SUCCESS ) {
841 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
842 "error preparing query\n", 0, 0, 0 );
843 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
844 return BACKSQL_CONTINUE;
847 if ( backsql_BindParamID( sth, 1, &bsi->oc->id ) != SQL_SUCCESS ) {
848 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
849 "error binding objectclass id parameter\n", 0, 0, 0 );
850 return BACKSQL_CONTINUE;
853 switch ( bsi->scope ) {
854 case LDAP_SCOPE_BASE:
855 rc = backsql_BindParamStr( sth, 2, bsi->base_dn->bv_val,
856 BACKSQL_MAX_DN_LEN );
857 if ( rc != SQL_SUCCESS ) {
858 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
859 "error binding base_dn parameter\n", 0, 0, 0 );
860 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh,
862 return BACKSQL_CONTINUE;
866 case LDAP_SCOPE_SUBTREE: {
869 * + 1 because we need room for '%'; this makes a subtree
870 * search for a DN BACKSQL_MAX_DN_LEN long legal
871 * if it returns that DN only
873 char temp_base_dn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
876 * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
877 * however this should be handled earlier
879 assert( bsi->base_dn->bv_len <= BACKSQL_MAX_DN_LEN );
882 * Sets the parameters for the SQL built earlier
883 * NOTE that all the databases could actually use
884 * the TimesTen version, which would be cleaner
885 * and would also eliminate the need for the
886 * subtree_cond line in the configuration file.
887 * For now, I'm leaving it the way it is,
888 * so non-TimesTen databases use the original code.
889 * But at some point this should get cleaned up.
891 * If "dn" is being used, do a suffix search.
892 * If "dn_ru" is being used, do a prefix search.
894 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bsi->bi ) ) {
895 temp_base_dn[ 0 ] = '\0';
896 for ( i = 0, j = bsi->base_dn->bv_len - 1;
898 temp_base_dn[ i ] = bsi->base_dn->bv_val[ j ];
900 temp_base_dn[ i ] = '%';
901 temp_base_dn[ i + 1 ] = '\0';
902 ldap_pvt_str2upper( temp_base_dn );
905 temp_base_dn[ 0 ] = '%';
906 AC_MEMCPY( &temp_base_dn[ 1 ], bsi->base_dn->bv_val,
907 bsi->base_dn->bv_len + 1 );
908 ldap_pvt_str2upper( &temp_base_dn[ 1 ] );
911 Debug( LDAP_DEBUG_TRACE, "dn '%s'\n", temp_base_dn, 0, 0 );
913 rc = backsql_BindParamStr( sth, 2, temp_base_dn,
914 BACKSQL_MAX_DN_LEN );
915 if ( rc != SQL_SUCCESS ) {
916 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
917 "error binding base_dn parameter (2)\n",
919 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh,
921 return BACKSQL_CONTINUE;
926 case LDAP_SCOPE_ONELEVEL:
927 res = backsql_dn2id( bsi->bi, &base_id,
928 bsi->dbh, bsi->base_dn );
929 if ( res != LDAP_SUCCESS ) {
930 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
931 "could not retrieve base_dn id%s\n",
932 res == LDAP_NO_SUCH_OBJECT ? ": no such entry"
935 return BACKSQL_CONTINUE;
938 rc = backsql_BindParamID( sth, 2, &base_id.id );
939 backsql_free_entryID( &base_id, 0 );
940 if ( rc != SQL_SUCCESS ) {
941 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
942 "error binding base id parameter\n", 0, 0, 0 );
943 return BACKSQL_CONTINUE;
948 rc = SQLExecute( sth );
949 if ( !BACKSQL_SUCCESS( rc ) ) {
950 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
951 "error executing query\n", 0, 0, 0 );
952 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
953 SQLFreeStmt( sth, SQL_DROP );
954 return BACKSQL_CONTINUE;
957 backsql_BindRowAsStrings( sth, &row );
958 rc = SQLFetch( sth );
959 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
960 c_id = (backsql_entryID *)ch_calloc( 1,
961 sizeof( backsql_entryID ) );
962 c_id->id = strtol( row.cols[ 0 ], NULL, 0 );
963 c_id->keyval = strtol( row.cols[ 1 ], NULL, 0 );
964 c_id->oc_id = bsi->oc->id;
965 ber_str2bv( row.cols[ 3 ], 0, 1, &c_id->dn );
966 c_id->next = bsi->id_list;
970 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
971 "added entry id=%ld, keyval=%ld dn='%s'\n",
972 c_id->id, c_id->keyval, row.cols[ 3 ] );
974 if ( bsi->n_candidates == -1 ) {
978 backsql_FreeRow( &row );
979 SQLFreeStmt( sth, SQL_DROP );
981 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates()\n", 0, 0, 0 );
983 return ( bsi->n_candidates == -1 ? BACKSQL_STOP : BACKSQL_CONTINUE );
992 struct berval *nbase,
998 struct berval *filterstr,
999 AttributeName *attrs,
1002 backsql_info *bi = (backsql_info *)be->be_private;
1007 int manageDSAit = get_manageDSAit( op );
1008 BerVarray v2refs = NULL;
1009 time_t stoptime = 0;
1010 backsql_srch_info srch_info;
1011 backsql_entryID *eid = NULL;
1012 struct slap_limits_set *limit = NULL;
1015 Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
1016 "base='%s', filter='%s', scope=%d,",
1017 nbase->bv_val, filterstr->bv_val, scope );
1018 Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
1019 "attributes to load: %s\n",
1020 deref, attrsonly, attrs == NULL ? "all" : "custom list" );
1022 if ( nbase->bv_len > BACKSQL_MAX_DN_LEN ) {
1023 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1024 "search base length (%ld) exceeds max length (%ld)\n",
1025 nbase->bv_len, BACKSQL_MAX_DN_LEN, 0 );
1027 * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
1028 * since it is impossible that such a long DN exists
1031 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
1032 "", NULL, NULL, NULL );
1036 sres = backsql_get_db_conn( be, conn, &dbh );
1037 if ( sres != LDAP_SUCCESS ) {
1038 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1039 "could not get connection handle - exiting\n",
1041 send_ldap_result( conn, op, sres, "",
1042 sres == LDAP_OTHER ? "SQL-backend error" : "",
1047 /* TimesTen : Pass it along to the lower level routines */
1048 srch_info.use_reverse_dn = BACKSQL_USE_REVERSE_DN( bi );
1050 /* if not root, get appropriate limits */
1051 if ( be_isroot( be, &op->o_ndn ) ) {
1054 ( void ) get_limits( be, &op->o_ndn, &limit );
1057 /* The time/size limits come first because they require very little
1058 * effort, so there's no chance the candidates are selected and then
1059 * the request is not honored only because of time/size constraints */
1061 /* if no time limit requested, use soft limit (unless root!) */
1063 if ( tlimit == 0 ) {
1064 tlimit = -1; /* allow root to set no limit */
1067 if ( slimit == 0 ) {
1072 /* if no limit is required, use soft limit */
1073 if ( tlimit <= 0 ) {
1074 tlimit = limit->lms_t_soft;
1076 /* if requested limit higher than hard limit, abort */
1077 } else if ( tlimit > limit->lms_t_hard ) {
1078 /* no hard limit means use soft instead */
1079 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
1080 tlimit = limit->lms_t_soft;
1082 /* positive hard limit means abort */
1083 } else if ( limit->lms_t_hard > 0 ) {
1084 send_search_result( conn, op,
1085 LDAP_UNWILLING_TO_PERFORM,
1086 NULL, NULL, NULL, NULL, 0 );
1090 /* negative hard limit means no limit */
1093 /* if no limit is required, use soft limit */
1094 if ( slimit <= 0 ) {
1095 slimit = limit->lms_s_soft;
1097 /* if requested limit higher than hard limit, abort */
1098 } else if ( slimit > limit->lms_s_hard ) {
1099 /* no hard limit means use soft instead */
1100 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
1101 slimit = limit->lms_s_soft;
1103 /* positive hard limit means abort */
1104 } else if ( limit->lms_s_hard > 0 ) {
1105 send_search_result( conn, op,
1106 LDAP_UNWILLING_TO_PERFORM,
1107 NULL, NULL, NULL, NULL, 0 );
1111 /* negative hard limit means no limit */
1115 /* compute it anyway; root does not use it */
1116 stoptime = op->o_time + tlimit;
1118 backsql_init_search( &srch_info, bi, nbase, scope,
1119 slimit, tlimit, stoptime, filter, dbh,
1120 be, conn, op, attrs );
1123 * for each objectclass we try to construct query which gets IDs
1124 * of entries matching LDAP query filter and scope (or at least
1125 * candidates), and get the IDs
1127 srch_info.n_candidates = ( isroot ? -2 : limit->lms_s_unchecked == -1
1128 ? -2 : limit->lms_s_unchecked );
1129 avl_apply( bi->oc_by_oc, (AVL_APPLY)backsql_oc_get_candidates,
1130 &srch_info, BACKSQL_STOP, AVL_INORDER );
1131 if ( !isroot && limit->lms_s_unchecked != -1 ) {
1132 if ( srch_info.n_candidates == -1 ) {
1133 send_search_result( conn, op,
1134 LDAP_ADMINLIMIT_EXCEEDED,
1135 NULL, NULL, NULL, NULL, 0 );
1142 * now we load candidate entries (only those attributes
1143 * mentioned in attrs and filter), test it against full filter
1144 * and then send to client
1146 for ( eid = srch_info.id_list; eid != NULL;
1147 eid = backsql_free_entryID( eid, 1 ) ) {
1148 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1149 Attribute *hasSubordinate = NULL,
1151 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1153 /* check for abandon */
1154 if ( op->o_abandon ) {
1158 /* check time limit */
1159 if ( tlimit != -1 && slap_get_time() > stoptime ) {
1160 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
1161 NULL, NULL, v2refs, NULL, nentries );
1165 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1166 "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
1167 eid->id, eid->oc_id, eid->keyval );
1169 entry = (Entry *)ch_calloc( sizeof( Entry ), 1 );
1170 res = backsql_id2entry( &srch_info, entry, eid );
1171 if ( res == NULL ) {
1172 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1173 "error in backsql_id2entry() "
1174 "- skipping entry\n", 0, 0, 0 );
1178 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
1179 is_entry_referral( entry ) ) {
1180 BerVarray refs = get_entry_referrals( be, conn,
1183 send_search_reference( be, conn, op, entry, refs,
1185 ber_bvarray_free( refs );
1189 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1191 * We use this flag since we need to parse the filter
1192 * anyway; we should have used the frontend API function
1193 * filter_has_subordinates()
1195 if ( srch_info.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
1198 rc = backsql_has_children( bi, dbh, &entry->e_nname );
1201 case LDAP_COMPARE_TRUE:
1202 case LDAP_COMPARE_FALSE:
1203 hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
1204 if ( hasSubordinate != NULL ) {
1205 for ( a = entry->e_attrs;
1209 a->a_next = hasSubordinate;
1215 Debug(LDAP_DEBUG_TRACE,
1216 "backsql_search(): "
1217 "has_children failed( %d)\n",
1227 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1229 if ( test_filter( be, conn, op, entry, filter )
1230 == LDAP_COMPARE_TRUE ) {
1231 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1232 if ( hasSubordinate && !( srch_info.bsi_flags & BSQL_SF_ALL_OPER )
1233 && !ad_inlist( slap_schema.si_ad_hasSubordinates, attrs ) ) {
1235 attr_free( hasSubordinate );
1236 hasSubordinate = NULL;
1238 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1240 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
1245 sres = send_search_entry( be, conn, op, entry,
1246 attrs, attrsonly, NULL );
1257 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1258 "connection lost\n", 0, 0, 0 );
1263 * FIXME: send_search_entry failed;
1269 entry_free( entry );
1271 if ( slimit != -1 && nentries >= slimit ) {
1272 send_search_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
1273 NULL, NULL, v2refs, NULL, nentries );
1280 if ( nentries > 0 ) {
1281 send_search_result( conn, op,
1282 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
1283 NULL, NULL, v2refs, NULL, nentries );
1285 send_ldap_result( conn, op, srch_info.status,
1286 NULL, NULL, NULL, 0 );
1290 ch_free( srch_info.attrs );
1292 Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
1296 #endif /* SLAPD_SQL */