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;
327 Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n", 0, 0, 0 );
328 if ( f == NULL || f->f_choice == SLAPD_FILTER_COMPUTED ) {
332 switch( f->f_choice ) {
334 rc = backsql_process_filter_list( bsi, f->f_or,
339 case LDAP_FILTER_AND:
340 rc = backsql_process_filter_list( bsi, f->f_and,
345 case LDAP_FILTER_NOT:
346 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
347 (ber_len_t)sizeof( "NOT (" /* ) */ ) - 1,
349 rc = backsql_process_filter( bsi, f->f_not );
350 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c",
355 case LDAP_FILTER_PRESENT:
359 case LDAP_FILTER_EXT:
360 ad = f->f_mra->ma_desc;
369 /* TimesTen : Don't run the query */
378 * Turn structuralObjectClass into objectClass
380 if ( ad == slap_schema.si_ad_objectClass
381 || ad == slap_schema.si_ad_structuralObjectClass ) {
383 backsql_strfcat( &at->sel_expr, &len, "cbc",
385 &bsi->oc->oc->soc_cname,
388 #if defined(SLAP_X_FILTER_HASSUBORDINATES) || defined(SLAP_X_MRA_MATCH_DNATTRS)
389 } else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) {
391 * FIXME: this is not robust; e.g. a filter
392 * '(!(hasSubordinates=TRUE))' fails because
393 * in SQL it would read 'NOT (1=1)' instead
395 * Note however that hasSubordinates is boolean,
396 * so a more appropriate filter would be
397 * '(hasSubordinates=FALSE)'
399 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
400 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
403 * We use this flag since we need to parse
404 * the filter anyway; we should have used
405 * the frontend API function
406 * filter_has_subordinates()
408 bsi->bsi_flags |= BSQL_SF_FILTER_HASSUBORDINATE;
411 * clear attributes to fetch, to require ALL
412 * and try extended match on all attributes
414 backsql_attrlist_add( bsi, NULL );
417 #endif /* SLAP_X_FILTER_HASSUBORDINATES || SLAP_X_MRA_MATCH_DNATTRS */
420 at = backsql_ad2at( bsi->oc, ad );
424 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
425 "attribute '%s' is not defined for objectclass '%s'\n",
426 ad->ad_cname.bv_val, BACKSQL_OC_NAME( bsi->oc ), 0 );
427 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
428 (ber_len_t)sizeof( "1=0" ) - 1, "1=0" );
432 backsql_merge_from_clause( &bsi->from, &bsi->from_len,
435 * need to add this attribute to list of attrs to load,
436 * so that we could do test_filter() later
438 backsql_attrlist_add( bsi, ad );
440 if ( at->join_where.bv_val != NULL
441 && strstr( bsi->join_where.bv_val, at->join_where.bv_val ) == NULL ) {
442 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lb",
443 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
449 * FIXME: this is not required any more; however, note that
450 * attribute name syntax might collide with SQL legal aliases
452 if ( at != &oc_attr ) {
453 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cblb",
456 (ber_len_t)sizeof( " AS " ) - 1, " AS ",
461 switch ( f->f_choice ) {
462 case LDAP_FILTER_EQUALITY:
464 * maybe we should check type of at->sel_expr here somehow,
465 * to know whether upper_func is applicable, but for now
466 * upper_func stuff is made for Oracle, where UPPER is
467 * safely applicable to NUMBER etc.
469 if ( bsi->bi->upper_func.bv_val ) {
472 if ( at->sel_expr_u.bv_val ) {
473 backsql_strfcat( &bsi->flt_where,
474 &bsi->fwhere_len, "cbl",
477 (ber_len_t)sizeof( "='" ) - 1,
480 backsql_strfcat( &bsi->flt_where,
481 &bsi->fwhere_len, "cbcbl",
483 &bsi->bi->upper_func,
486 (ber_len_t)sizeof( /* ( */ ")='" ) - 1,
490 start = bsi->flt_where.bv_len;
492 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
495 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
498 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
501 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
505 (ber_len_t)sizeof( "='" ) - 1, "='",
507 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
514 * FIXME: should we uppercase the operands?
516 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
519 (ber_len_t)sizeof( ">=" ) - 1, ">=",
526 * FIXME: should we uppercase the operands?
528 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
531 (ber_len_t)sizeof( "<=" ) - 1, "<=",
536 case LDAP_FILTER_PRESENT:
537 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "lbl",
538 (ber_len_t)sizeof( "NOT (" ) - 1, "NOT (",
540 (ber_len_t)sizeof( " IS NULL)" ) - 1, " IS NULL)" );
543 case LDAP_FILTER_SUBSTRINGS:
544 backsql_process_sub_filter( bsi, f );
549 if ( oc_attr.sel_expr.bv_val != NULL ) {
550 free( oc_attr.sel_expr.bv_val );
553 Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter()\n", 0, 0, 0 );
557 if ( oc_attr.sel_expr.bv_val != NULL ) {
558 free( oc_attr.sel_expr.bv_val );
560 Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter() returns -1\n",
566 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
568 backsql_info *bi = (backsql_info *)bsi->be->be_private;
573 query->bv_val = NULL;
576 Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
577 bsi->sel.bv_val = NULL;
580 bsi->from.bv_val = NULL;
581 bsi->from.bv_len = 0;
583 bsi->join_where.bv_val = NULL;
584 bsi->join_where.bv_len = 0;
586 bsi->flt_where.bv_val = NULL;
587 bsi->flt_where.bv_len = 0;
592 * FIXME: this query has been split in case a string cast function
593 * is defined; more sophisticated (pattern based) function should
596 backsql_strcat( &bsi->sel, &bsi->sel_len,
597 "SELECT DISTINCT ldap_entries.id,",
598 bsi->oc->keytbl.bv_val, ".", bsi->oc->keycol.bv_val,
599 ",'", bsi->oc->name.bv_val, "' AS objectClass",
600 ",ldap_entries.dn AS dn", NULL );
603 backsql_strfcat( &bsi->sel, &bsi->sel_len, "lbcbc",
604 (ber_len_t)sizeof( "SELECT DISTINCT ldap_entries.id," ) - 1,
605 "SELECT DISTINCT ldap_entries.id,",
611 if ( bi->strcast_func.bv_val ) {
612 backsql_strfcat( &bsi->sel, &bsi->sel_len, "blbl",
614 (ber_len_t)sizeof( "('" /* ') */ ) - 1,
616 &bsi->oc->oc->soc_cname,
617 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
620 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cbc",
622 &bsi->oc->oc->soc_cname,
625 backsql_strfcat( &bsi->sel, &bsi->sel_len, "l",
626 (ber_len_t)sizeof( " AS objectClass,ldap_entries.dn AS dn" ) - 1,
627 " AS objectClass,ldap_entries.dn AS dn" );
629 backsql_strfcat( &bsi->from, &bsi->from_len, "lb",
630 (ber_len_t)sizeof( " FROM ldap_entries," ) - 1,
631 " FROM ldap_entries,",
634 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lbcbl",
635 (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ",
639 (ber_len_t)sizeof( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ) - 1,
640 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
642 switch ( bsi->scope ) {
643 case LDAP_SCOPE_BASE:
644 if ( bsi->bi->upper_func.bv_val ) {
645 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
647 &bsi->bi->upper_func,
648 (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
649 "(ldap_entries.dn)=",
650 &bsi->bi->upper_func_open,
652 &bsi->bi->upper_func_close );
654 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
656 (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
657 "ldap_entries.dn=?" );
661 case LDAP_SCOPE_ONELEVEL:
662 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "l",
663 (ber_len_t)sizeof( "ldap_entries.parent=?" ) - 1,
664 "ldap_entries.parent=?" );
667 case LDAP_SCOPE_SUBTREE:
668 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "b",
669 &bsi->bi->subtree_cond );
676 rc = backsql_process_filter( bsi, bsi->filter );
678 backsql_strfcat( query, &q_len, "bbblb",
682 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
685 } else if ( rc < 0 ) {
687 * Indicates that there's no possible way the filter matches
688 * anything. No need to issue the query
690 Debug( LDAP_DEBUG_TRACE,
691 "<==backsql_srch_query() returns NULL\n", 0, 0, 0 );
692 free( query->bv_val );
693 query->bv_val = NULL;
696 free( bsi->sel.bv_val );
699 free( bsi->from.bv_val );
700 bsi->from.bv_len = 0;
702 free( bsi->join_where.bv_val );
703 bsi->join_where.bv_len = 0;
705 free( bsi->flt_where.bv_val );
706 bsi->flt_where.bv_len = 0;
709 Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query()\n", 0, 0, 0 );
711 return ( query->bv_val == NULL ? 1 : 0 );
715 backsql_oc_get_candidates( backsql_oc_map_rec *oc, backsql_srch_info *bsi )
720 backsql_entryID base_id, *c_id;
726 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc='%s'\n",
727 BACKSQL_OC_NAME( oc ), 0, 0 );
729 if ( bsi->n_candidates == -1 ) {
730 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
731 "unchecked limit has been overcome\n", 0, 0, 0 );
732 /* should never get here */
738 if ( backsql_srch_query( bsi, &query ) ) {
739 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
740 "could not construct query for objectclass\n",
742 return BACKSQL_CONTINUE;
745 Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n",
746 query.bv_val, 0, 0 );
748 rc = backsql_Prepare( bsi->dbh, &sth, query.bv_val, 0 );
749 free( query.bv_val );
750 if ( rc != SQL_SUCCESS ) {
751 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
752 "error preparing query\n", 0, 0, 0 );
753 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
754 return BACKSQL_CONTINUE;
757 if ( backsql_BindParamID( sth, 1, &bsi->oc->id ) != SQL_SUCCESS ) {
758 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
759 "error binding objectclass id parameter\n", 0, 0, 0 );
760 return BACKSQL_CONTINUE;
763 switch ( bsi->scope ) {
764 case LDAP_SCOPE_BASE:
765 rc = backsql_BindParamStr( sth, 2, bsi->base_dn->bv_val,
766 BACKSQL_MAX_DN_LEN );
767 if ( rc != SQL_SUCCESS ) {
768 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
769 "error binding base_dn parameter\n", 0, 0, 0 );
770 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh,
772 return BACKSQL_CONTINUE;
776 case LDAP_SCOPE_SUBTREE: {
779 * + 1 because we need room for '%'; this makes a subtree
780 * search for a DN BACKSQL_MAX_DN_LEN long legal
781 * if it returns that DN only
783 char temp_base_dn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
786 * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
787 * however this should be handled earlier
789 assert( bsi->base_dn->bv_len <= BACKSQL_MAX_DN_LEN );
792 * Sets the parameters for the SQL built earlier
793 * NOTE that all the databases could actually use
794 * the TimesTen version, which would be cleaner
795 * and would also eliminate the need for the
796 * subtree_cond line in the configuration file.
797 * For now, I'm leaving it the way it is,
798 * so non-TimesTen databases use the original code.
799 * But at some point this should get cleaned up.
801 * If "dn" is being used, do a suffix search.
802 * If "dn_ru" is being used, do a prefix search.
804 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bsi->bi ) ) {
805 temp_base_dn[ 0 ] = '\0';
806 for ( i = 0, j = bsi->base_dn->bv_len - 1;
808 temp_base_dn[ i ] = bsi->base_dn->bv_val[ j ];
810 temp_base_dn[ i ] = '%';
811 temp_base_dn[ i + 1 ] = '\0';
812 ldap_pvt_str2upper( temp_base_dn );
815 temp_base_dn[ 0 ] = '%';
816 AC_MEMCPY( &temp_base_dn[ 1 ], bsi->base_dn->bv_val,
817 bsi->base_dn->bv_len + 1 );
818 ldap_pvt_str2upper( &temp_base_dn[ 1 ] );
821 Debug( LDAP_DEBUG_TRACE, "dn '%s'\n", temp_base_dn, 0, 0 );
823 rc = backsql_BindParamStr( sth, 2, temp_base_dn,
824 BACKSQL_MAX_DN_LEN );
825 if ( rc != SQL_SUCCESS ) {
826 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
827 "error binding base_dn parameter (2)\n",
829 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh,
831 return BACKSQL_CONTINUE;
836 case LDAP_SCOPE_ONELEVEL:
837 res = backsql_dn2id( bsi->bi, &base_id,
838 bsi->dbh, bsi->base_dn );
839 if ( res != LDAP_SUCCESS ) {
840 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
841 "could not retrieve base_dn id%s\n",
842 res == LDAP_NO_SUCH_OBJECT ? ": no such entry"
845 return BACKSQL_CONTINUE;
848 rc = backsql_BindParamID( sth, 2, &base_id.id );
849 backsql_free_entryID( &base_id, 0 );
850 if ( rc != SQL_SUCCESS ) {
851 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
852 "error binding base id parameter\n", 0, 0, 0 );
853 return BACKSQL_CONTINUE;
858 rc = SQLExecute( sth );
859 if ( !BACKSQL_SUCCESS( rc ) ) {
860 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
861 "error executing query\n", 0, 0, 0 );
862 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
863 SQLFreeStmt( sth, SQL_DROP );
864 return BACKSQL_CONTINUE;
867 backsql_BindRowAsStrings( sth, &row );
868 rc = SQLFetch( sth );
869 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
870 c_id = (backsql_entryID *)ch_calloc( 1,
871 sizeof( backsql_entryID ) );
872 c_id->id = strtol( row.cols[ 0 ], NULL, 0 );
873 c_id->keyval = strtol( row.cols[ 1 ], NULL, 0 );
874 c_id->oc_id = bsi->oc->id;
875 ber_str2bv( row.cols[ 3 ], 0, 1, &c_id->dn );
876 c_id->next = bsi->id_list;
880 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
881 "added entry id=%ld, keyval=%ld dn='%s'\n",
882 c_id->id, c_id->keyval, row.cols[ 3 ] );
884 if ( bsi->n_candidates == -1 ) {
888 backsql_FreeRow( &row );
889 SQLFreeStmt( sth, SQL_DROP );
891 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates()\n", 0, 0, 0 );
893 return ( bsi->n_candidates == -1 ? BACKSQL_STOP : BACKSQL_CONTINUE );
902 struct berval *nbase,
908 struct berval *filterstr,
909 AttributeName *attrs,
912 backsql_info *bi = (backsql_info *)be->be_private;
917 int manageDSAit = get_manageDSAit( op );
918 BerVarray v2refs = NULL;
920 backsql_srch_info srch_info;
921 backsql_entryID *eid = NULL;
922 struct slap_limits_set *limit = NULL;
925 Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
926 "base='%s', filter='%s', scope=%d,",
927 nbase->bv_val, filterstr->bv_val, scope );
928 Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
929 "attributes to load: %s\n",
930 deref, attrsonly, attrs == NULL ? "all" : "custom list" );
932 if ( nbase->bv_len > BACKSQL_MAX_DN_LEN ) {
933 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
934 "search base length (%ld) exceeds max length (%ld)\n",
935 nbase->bv_len, BACKSQL_MAX_DN_LEN, 0 );
937 * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
938 * since it is impossible that such a long DN exists
941 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
942 "", NULL, NULL, NULL );
946 sres = backsql_get_db_conn( be, conn, &dbh );
947 if ( sres != LDAP_SUCCESS ) {
948 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
949 "could not get connection handle - exiting\n",
951 send_ldap_result( conn, op, sres, "",
952 sres == LDAP_OTHER ? "SQL-backend error" : "",
957 /* TimesTen : Pass it along to the lower level routines */
958 srch_info.use_reverse_dn = BACKSQL_USE_REVERSE_DN( bi );
960 /* if not root, get appropriate limits */
961 if ( be_isroot( be, &op->o_ndn ) ) {
964 ( void ) get_limits( be, &op->o_ndn, &limit );
967 /* The time/size limits come first because they require very little
968 * effort, so there's no chance the candidates are selected and then
969 * the request is not honored only because of time/size constraints */
971 /* if no time limit requested, use soft limit (unless root!) */
974 tlimit = -1; /* allow root to set no limit */
982 /* if no limit is required, use soft limit */
984 tlimit = limit->lms_t_soft;
986 /* if requested limit higher than hard limit, abort */
987 } else if ( tlimit > limit->lms_t_hard ) {
988 /* no hard limit means use soft instead */
989 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
990 tlimit = limit->lms_t_soft;
992 /* positive hard limit means abort */
993 } else if ( limit->lms_t_hard > 0 ) {
994 send_search_result( conn, op,
995 LDAP_UNWILLING_TO_PERFORM,
996 NULL, NULL, NULL, NULL, 0 );
1000 /* negative hard limit means no limit */
1003 /* if no limit is required, use soft limit */
1004 if ( slimit <= 0 ) {
1005 slimit = limit->lms_s_soft;
1007 /* if requested limit higher than hard limit, abort */
1008 } else if ( slimit > limit->lms_s_hard ) {
1009 /* no hard limit means use soft instead */
1010 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
1011 slimit = limit->lms_s_soft;
1013 /* positive hard limit means abort */
1014 } else if ( limit->lms_s_hard > 0 ) {
1015 send_search_result( conn, op,
1016 LDAP_UNWILLING_TO_PERFORM,
1017 NULL, NULL, NULL, NULL, 0 );
1021 /* negative hard limit means no limit */
1025 /* compute it anyway; root does not use it */
1026 stoptime = op->o_time + tlimit;
1028 backsql_init_search( &srch_info, bi, nbase, scope,
1029 slimit, tlimit, stoptime, filter, dbh,
1030 be, conn, op, attrs );
1033 * for each objectclass we try to construct query which gets IDs
1034 * of entries matching LDAP query filter and scope (or at least
1035 * candidates), and get the IDs
1037 srch_info.n_candidates = ( isroot ? -2 : limit->lms_s_unchecked == -1
1038 ? -2 : limit->lms_s_unchecked );
1039 avl_apply( bi->oc_by_oc, (AVL_APPLY)backsql_oc_get_candidates,
1040 &srch_info, BACKSQL_STOP, AVL_INORDER );
1041 if ( !isroot && limit->lms_s_unchecked != -1 ) {
1042 if ( srch_info.n_candidates == -1 ) {
1043 send_search_result( conn, op,
1044 LDAP_ADMINLIMIT_EXCEEDED,
1045 NULL, NULL, NULL, NULL, 0 );
1052 * now we load candidate entries (only those attributes
1053 * mentioned in attrs and filter), test it against full filter
1054 * and then send to client
1056 for ( eid = srch_info.id_list; eid != NULL;
1057 eid = backsql_free_entryID( eid, 1 ) ) {
1058 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1059 Attribute *hasSubordinate = NULL,
1061 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1063 /* check for abandon */
1064 if ( op->o_abandon ) {
1068 /* check time limit */
1069 if ( tlimit != -1 && slap_get_time() > stoptime ) {
1070 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
1071 NULL, NULL, v2refs, NULL, nentries );
1075 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1076 "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
1077 eid->id, eid->oc_id, eid->keyval );
1079 entry = (Entry *)ch_calloc( sizeof( Entry ), 1 );
1080 res = backsql_id2entry( &srch_info, entry, eid );
1081 if ( res == NULL ) {
1082 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1083 "error in backsql_id2entry() "
1084 "- skipping entry\n", 0, 0, 0 );
1088 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
1089 is_entry_referral( entry ) ) {
1090 BerVarray refs = get_entry_referrals( be, conn,
1093 send_search_reference( be, conn, op, entry, refs,
1095 ber_bvarray_free( refs );
1099 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1101 * We use this flag since we need to parse the filter
1102 * anyway; we should have used the frontend API function
1103 * filter_has_subordinates()
1105 if ( srch_info.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
1108 rc = backsql_has_children( bi, dbh, &entry->e_nname );
1111 case LDAP_COMPARE_TRUE:
1112 case LDAP_COMPARE_FALSE:
1113 hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
1114 if ( hasSubordinate != NULL ) {
1115 for ( a = entry->e_attrs;
1119 a->a_next = hasSubordinate;
1125 Debug(LDAP_DEBUG_TRACE,
1126 "backsql_search(): "
1127 "has_children failed( %d)\n",
1137 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1139 if ( test_filter( be, conn, op, entry, filter )
1140 == LDAP_COMPARE_TRUE ) {
1141 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1142 if ( hasSubordinate && !( srch_info.bsi_flags & BSQL_SF_ALL_OPER )
1143 && !ad_inlist( slap_schema.si_ad_hasSubordinates, attrs ) ) {
1145 attr_free( hasSubordinate );
1146 hasSubordinate = NULL;
1148 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1150 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
1155 sres = send_search_entry( be, conn, op, entry,
1156 attrs, attrsonly, NULL );
1167 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1168 "connection lost\n", 0, 0, 0 );
1173 * FIXME: send_search_entry failed;
1179 entry_free( entry );
1181 if ( slimit != -1 && nentries >= slimit ) {
1182 send_search_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
1183 NULL, NULL, v2refs, NULL, nentries );
1190 if ( nentries > 0 ) {
1191 send_search_result( conn, op,
1192 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
1193 NULL, NULL, v2refs, NULL, nentries );
1195 send_ldap_result( conn, op, srch_info.status,
1196 NULL, NULL, NULL, 0 );
1200 ch_free( srch_info.attrs );
1202 Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
1206 #endif /* SLAPD_SQL */