2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2006 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
7 * Portions Copyright 2004 Mark Adamson.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by Dmitry Kovalev for inclusion
20 * by OpenLDAP Software. Additional significant contributors include
21 * Pierangelo Masarati and Mark Adamson.
27 #include <sys/types.h>
28 #include "ac/string.h"
33 #include "proto-sql.h"
35 static int backsql_process_filter( backsql_srch_info *bsi, Filter *f );
36 static int backsql_process_filter_eq( backsql_srch_info *bsi,
37 backsql_at_map_rec *at,
38 int casefold, struct berval *filter_value );
39 static int backsql_process_filter_like( backsql_srch_info *bsi,
40 backsql_at_map_rec *at,
41 int casefold, struct berval *filter_value );
42 static int backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f,
43 backsql_at_map_rec *at );
46 backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad )
49 AttributeName *an = NULL;
51 if ( bsi->bsi_attrs == NULL ) {
56 * clear the list (retrieve all attrs)
59 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs, bsi->bsi_op->o_tmpmemctx );
60 bsi->bsi_attrs = NULL;
61 bsi->bsi_flags |= BSQL_SF_ALL_ATTRS;
65 for ( ; !BER_BVISNULL( &bsi->bsi_attrs[ n_attrs ].an_name ); n_attrs++ ) {
66 an = &bsi->bsi_attrs[ n_attrs ];
68 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
69 "attribute \"%s\" is in list\n",
70 an->an_name.bv_val, 0, 0 );
72 * We can live with strcmp because the attribute
73 * list has been normalized before calling be_search
75 if ( !BACKSQL_NCMP( &an->an_name, &ad->ad_cname ) ) {
80 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
81 "adding \"%s\" to list\n", ad->ad_cname.bv_val, 0, 0 );
83 an = (AttributeName *)bsi->bsi_op->o_tmprealloc( bsi->bsi_attrs,
84 sizeof( AttributeName ) * ( n_attrs + 2 ),
85 bsi->bsi_op->o_tmpmemctx );
90 an[ n_attrs ].an_name = ad->ad_cname;
91 an[ n_attrs ].an_desc = ad;
92 BER_BVZERO( &an[ n_attrs + 1 ].an_name );
100 * Initializes the search structure.
102 * If get_base_id != 0, the field bsi_base_id is filled
103 * with the entryID of bsi_base_ndn; it must be freed
104 * by backsql_free_entryID() when no longer required.
106 * NOTE: base must be normalized
110 backsql_srch_info *bsi,
111 struct berval *nbase,
118 AttributeName *attrs,
121 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
122 int rc = LDAP_SUCCESS;
124 bsi->bsi_base_ndn = nbase;
125 bsi->bsi_use_subtree_shortcut = 0;
126 BER_BVZERO( &bsi->bsi_base_id.eid_dn );
127 BER_BVZERO( &bsi->bsi_base_id.eid_ndn );
128 bsi->bsi_scope = scope;
129 bsi->bsi_filter = filter;
133 bsi->bsi_flags = BSQL_SF_NONE;
135 bsi->bsi_attrs = NULL;
137 if ( BACKSQL_FETCH_ALL_ATTRS( bi ) ) {
139 * if requested, simply try to fetch all attributes
141 bsi->bsi_flags |= BSQL_SF_ALL_ATTRS;
144 if ( BACKSQL_FETCH_ALL_USERATTRS( bi ) ) {
145 bsi->bsi_flags |= BSQL_SF_ALL_USER;
147 } else if ( BACKSQL_FETCH_ALL_OPATTRS( bi ) ) {
148 bsi->bsi_flags |= BSQL_SF_ALL_OPER;
151 if ( attrs == NULL ) {
152 /* NULL means all user attributes */
153 bsi->bsi_flags |= BSQL_SF_ALL_USER;
159 bsi->bsi_attrs = (AttributeName *)bsi->bsi_op->o_tmpalloc(
160 sizeof( AttributeName ),
161 bsi->bsi_op->o_tmpmemctx );
162 BER_BVZERO( &bsi->bsi_attrs[ 0 ].an_name );
164 for ( p = attrs; !BER_BVISNULL( &p->an_name ); p++ ) {
165 if ( BACKSQL_NCMP( &p->an_name, &AllUser ) == 0 ) {
167 bsi->bsi_flags |= BSQL_SF_ALL_USER;
169 /* if all attrs are requested, there's
170 * no need to continue */
171 if ( BSQL_ISF_ALL_ATTRS( bsi ) ) {
172 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
173 bsi->bsi_op->o_tmpmemctx );
174 bsi->bsi_attrs = NULL;
179 } else if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
181 bsi->bsi_flags |= BSQL_SF_ALL_OPER;
183 /* if all attrs are requested, there's
184 * no need to continue */
185 if ( BSQL_ISF_ALL_ATTRS( bsi ) ) {
186 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
187 bsi->bsi_op->o_tmpmemctx );
188 bsi->bsi_attrs = NULL;
193 } else if ( BACKSQL_NCMP( &p->an_name, &NoAttrs ) == 0 ) {
197 } else if ( p->an_desc == slap_schema.si_ad_objectClass ) {
201 backsql_attrlist_add( bsi, p->an_desc );
204 if ( got_oc == 0 && !( bsi->bsi_flags & BSQL_SF_ALL_USER ) ) {
205 /* add objectClass if not present,
206 * because it is required to understand
207 * if an entry is a referral, an alias
209 backsql_attrlist_add( bsi, slap_schema.si_ad_objectClass );
213 if ( !BSQL_ISF_ALL_ATTRS( bsi ) && bi->sql_anlist ) {
216 /* use hints if available */
217 for ( p = bi->sql_anlist; !BER_BVISNULL( &p->an_name ); p++ ) {
218 if ( BACKSQL_NCMP( &p->an_name, &AllUser ) == 0 ) {
220 bsi->bsi_flags |= BSQL_SF_ALL_USER;
222 /* if all attrs are requested, there's
223 * no need to continue */
224 if ( BSQL_ISF_ALL_ATTRS( bsi ) ) {
225 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
226 bsi->bsi_op->o_tmpmemctx );
227 bsi->bsi_attrs = NULL;
232 } else if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
234 bsi->bsi_flags |= BSQL_SF_ALL_OPER;
236 /* if all attrs are requested, there's
237 * no need to continue */
238 if ( BSQL_ISF_ALL_ATTRS( bsi ) ) {
239 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
240 bsi->bsi_op->o_tmpmemctx );
241 bsi->bsi_attrs = NULL;
247 backsql_attrlist_add( bsi, p->an_desc );
253 bsi->bsi_id_list = NULL;
254 bsi->bsi_id_listtail = &bsi->bsi_id_list;
255 bsi->bsi_n_candidates = 0;
256 bsi->bsi_stoptime = stoptime;
257 BER_BVZERO( &bsi->bsi_sel.bb_val );
258 bsi->bsi_sel.bb_len = 0;
259 BER_BVZERO( &bsi->bsi_from.bb_val );
260 bsi->bsi_from.bb_len = 0;
261 BER_BVZERO( &bsi->bsi_join_where.bb_val );
262 bsi->bsi_join_where.bb_len = 0;
263 BER_BVZERO( &bsi->bsi_flt_where.bb_val );
264 bsi->bsi_flt_where.bb_len = 0;
265 bsi->bsi_filter_oc = NULL;
267 if ( BACKSQL_IS_GET_ID( flags ) ) {
268 int matched = BACKSQL_IS_MATCHED( flags );
269 int getentry = BACKSQL_IS_GET_ENTRY( flags );
272 assert( op->o_bd->be_private != NULL );
274 rc = backsql_dn2id( op, rs, dbh, nbase, &bsi->bsi_base_id,
277 /* the entry is collected either if requested for by getentry
278 * or if get noSuchObject and requested to climb the tree,
279 * so that a matchedDN or a referral can be returned */
280 if ( ( rc == LDAP_NO_SUCH_OBJECT && matched ) || getentry ) {
281 if ( !BER_BVISNULL( &bsi->bsi_base_id.eid_ndn ) ) {
282 assert( bsi->bsi_e != NULL );
284 if ( dn_match( nbase, &bsi->bsi_base_id.eid_ndn ) )
290 * let's see if it is a referral and, in case, get it
292 backsql_attrlist_add( bsi, slap_schema.si_ad_ref );
293 rc = backsql_id2entry( bsi, &bsi->bsi_base_id );
294 if ( rc == LDAP_SUCCESS ) {
295 if ( is_entry_referral( bsi->bsi_e ) )
297 BerVarray erefs = get_entry_referrals( op, bsi->bsi_e );
299 rc = rs->sr_err = LDAP_REFERRAL;
300 rs->sr_ref = referral_rewrite( erefs,
301 &bsi->bsi_e->e_nname,
304 ber_bvarray_free( erefs );
307 rc = rs->sr_err = LDAP_OTHER;
308 rs->sr_text = "bad referral object";
311 } else if ( !gotit ) {
312 rc = rs->sr_err = LDAP_NO_SUCH_OBJECT;
317 rs->sr_ref = referral_rewrite( default_referral,
318 NULL, &op->o_req_dn, scope );
319 rc = rs->sr_err = LDAP_REFERRAL;
324 bsi->bsi_status = rc;
332 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
333 bsi->bsi_op->o_tmpmemctx );
341 backsql_process_filter_list( backsql_srch_info *bsi, Filter *f, int op )
349 backsql_strfcat_x( &bsi->bsi_flt_where,
350 bsi->bsi_op->o_tmpmemctx, "c", '(' /* ) */ );
353 res = backsql_process_filter( bsi, f );
356 * TimesTen : If the query has no answers,
357 * don't bother to run the query.
368 case LDAP_FILTER_AND:
369 backsql_strfcat_x( &bsi->bsi_flt_where,
370 bsi->bsi_op->o_tmpmemctx, "l",
371 (ber_len_t)STRLENOF( " AND " ),
376 backsql_strfcat_x( &bsi->bsi_flt_where,
377 bsi->bsi_op->o_tmpmemctx, "l",
378 (ber_len_t)STRLENOF( " OR " ),
384 backsql_strfcat_x( &bsi->bsi_flt_where,
385 bsi->bsi_op->o_tmpmemctx, "c", /* ( */ ')' );
391 backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f,
392 backsql_at_map_rec *at )
394 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
402 /* always uppercase strings by now */
403 #ifdef BACKSQL_UPPERCASE_FILTER
404 if ( f->f_sub_desc->ad_type->sat_substr &&
405 SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
406 bi->sql_caseIgnoreMatch ) )
407 #endif /* BACKSQL_UPPERCASE_FILTER */
412 if ( f->f_sub_desc->ad_type->sat_substr &&
413 SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
414 bi->sql_telephoneNumberMatch ) )
421 * to check for matching telephone numbers
422 * with intermixed chars, e.g. val='1234'
425 * val LIKE '%1%2%3%4%'
429 if ( f->f_sub_initial.bv_val ) {
430 bv.bv_len += f->f_sub_initial.bv_len;
432 if ( f->f_sub_any != NULL ) {
433 for ( a = 0; f->f_sub_any[ a ].bv_val != NULL; a++ ) {
434 bv.bv_len += f->f_sub_any[ a ].bv_len;
437 if ( f->f_sub_final.bv_val ) {
438 bv.bv_len += f->f_sub_final.bv_len;
440 bv.bv_len = 2 * bv.bv_len - 1;
441 bv.bv_val = ch_malloc( bv.bv_len + 1 );
444 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
445 bv.bv_val[ s ] = f->f_sub_initial.bv_val[ 0 ];
446 for ( i = 1; i < f->f_sub_initial.bv_len; i++ ) {
447 bv.bv_val[ s + 2 * i - 1 ] = '%';
448 bv.bv_val[ s + 2 * i ] = f->f_sub_initial.bv_val[ i ];
450 bv.bv_val[ s + 2 * i - 1 ] = '%';
454 if ( f->f_sub_any != NULL ) {
455 for ( a = 0; !BER_BVISNULL( &f->f_sub_any[ a ] ); a++ ) {
456 bv.bv_val[ s ] = f->f_sub_any[ a ].bv_val[ 0 ];
457 for ( i = 1; i < f->f_sub_any[ a ].bv_len; i++ ) {
458 bv.bv_val[ s + 2 * i - 1 ] = '%';
459 bv.bv_val[ s + 2 * i ] = f->f_sub_any[ a ].bv_val[ i ];
461 bv.bv_val[ s + 2 * i - 1 ] = '%';
466 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
467 bv.bv_val[ s ] = f->f_sub_final.bv_val[ 0 ];
468 for ( i = 1; i < f->f_sub_final.bv_len; i++ ) {
469 bv.bv_val[ s + 2 * i - 1 ] = '%';
470 bv.bv_val[ s + 2 * i ] = f->f_sub_final.bv_val[ i ];
472 bv.bv_val[ s + 2 * i - 1 ] = '%';
476 bv.bv_val[ s - 1 ] = '\0';
478 (void)backsql_process_filter_like( bsi, at, casefold, &bv );
479 ch_free( bv.bv_val );
485 * When dealing with case-sensitive strings
486 * we may omit normalization; however, normalized
487 * SQL filters are more liberal.
490 backsql_strfcat_x( &bsi->bsi_flt_where,
491 bsi->bsi_op->o_tmpmemctx, "c", '(' /* ) */ );
494 Debug( LDAP_DEBUG_TRACE, "backsql_process_sub_filter(%s):\n",
495 at->bam_ad->ad_cname.bv_val, 0, 0 );
496 Debug(LDAP_DEBUG_TRACE, " expr: '%s%s%s'\n", at->bam_sel_expr.bv_val,
497 at->bam_sel_expr_u.bv_val ? "' '" : "",
498 at->bam_sel_expr_u.bv_val ? at->bam_sel_expr_u.bv_val : "" );
499 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
501 * If a pre-upper-cased version of the column
502 * or a precompiled upper function exists, use it
504 backsql_strfcat_x( &bsi->bsi_flt_where,
505 bsi->bsi_op->o_tmpmemctx,
508 (ber_len_t)STRLENOF( " LIKE '" ),
512 backsql_strfcat_x( &bsi->bsi_flt_where,
513 bsi->bsi_op->o_tmpmemctx,
516 (ber_len_t)STRLENOF( " LIKE '" ), " LIKE '" );
519 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
523 Debug( LDAP_DEBUG_TRACE,
524 "==>backsql_process_sub_filter(%s): "
525 "sub_initial=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
526 f->f_sub_initial.bv_val, 0 );
527 #endif /* BACKSQL_TRACE */
529 start = bsi->bsi_flt_where.bb_val.bv_len;
530 backsql_strfcat_x( &bsi->bsi_flt_where,
531 bsi->bsi_op->o_tmpmemctx,
534 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
535 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
539 backsql_strfcat_x( &bsi->bsi_flt_where,
540 bsi->bsi_op->o_tmpmemctx,
543 if ( f->f_sub_any != NULL ) {
544 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[ i ] ); i++ ) {
548 Debug( LDAP_DEBUG_TRACE,
549 "==>backsql_process_sub_filter(%s): "
550 "sub_any[%d]=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
551 i, f->f_sub_any[ i ].bv_val );
552 #endif /* BACKSQL_TRACE */
554 start = bsi->bsi_flt_where.bb_val.bv_len;
555 backsql_strfcat_x( &bsi->bsi_flt_where,
556 bsi->bsi_op->o_tmpmemctx,
560 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
562 * Note: toupper('%') = '%'
564 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
569 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
573 Debug( LDAP_DEBUG_TRACE,
574 "==>backsql_process_sub_filter(%s): "
575 "sub_final=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
576 f->f_sub_final.bv_val, 0 );
577 #endif /* BACKSQL_TRACE */
579 start = bsi->bsi_flt_where.bb_val.bv_len;
580 backsql_strfcat_x( &bsi->bsi_flt_where,
581 bsi->bsi_op->o_tmpmemctx,
584 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
585 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
589 backsql_strfcat_x( &bsi->bsi_flt_where,
590 bsi->bsi_op->o_tmpmemctx,
592 (ber_len_t)STRLENOF( /* (' */ "')" ), /* (' */ "')" );
598 backsql_merge_from_tbls( backsql_srch_info *bsi, struct berval *from_tbls )
600 if ( BER_BVISNULL( from_tbls ) ) {
604 if ( !BER_BVISNULL( &bsi->bsi_from.bb_val ) ) {
608 ber_dupbv_x( &tmp, from_tbls, bsi->bsi_op->o_tmpmemctx );
610 for ( start = tmp.bv_val, end = strchr( start, ',' ); start; ) {
615 if ( strstr( bsi->bsi_from.bb_val.bv_val, start) == NULL )
617 backsql_strfcat_x( &bsi->bsi_from,
618 bsi->bsi_op->o_tmpmemctx,
623 /* in case there are spaces after the comma... */
624 for ( start = &end[1]; isspace( start[0] ); start++ );
626 end = strchr( start, ',' );
635 bsi->bsi_op->o_tmpfree( tmp.bv_val, bsi->bsi_op->o_tmpmemctx );
638 backsql_strfcat_x( &bsi->bsi_from,
639 bsi->bsi_op->o_tmpmemctx,
647 backsql_process_filter( backsql_srch_info *bsi, Filter *f )
649 backsql_at_map_rec **vat = NULL;
650 AttributeDescription *ad = NULL;
655 Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n", 0, 0, 0 );
656 if ( f->f_choice == SLAPD_FILTER_COMPUTED ) {
657 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
658 "invalid filter\n", 0, 0, 0 );
663 switch( f->f_choice ) {
665 rc = backsql_process_filter_list( bsi, f->f_or,
670 case LDAP_FILTER_AND:
671 rc = backsql_process_filter_list( bsi, f->f_and,
676 case LDAP_FILTER_NOT:
677 backsql_strfcat_x( &bsi->bsi_flt_where,
678 bsi->bsi_op->o_tmpmemctx,
680 (ber_len_t)STRLENOF( "NOT (" /* ) */ ),
682 rc = backsql_process_filter( bsi, f->f_not );
683 backsql_strfcat_x( &bsi->bsi_flt_where,
684 bsi->bsi_op->o_tmpmemctx,
689 case LDAP_FILTER_PRESENT:
693 case LDAP_FILTER_EXT:
694 ad = f->f_mra->ma_desc;
695 if ( f->f_mr_dnattrs ) {
697 * if dn attrs filtering is requested, better return
698 * success and let test_filter() deal with candidate
699 * selection; otherwise we'd need to set conditions
700 * on the contents of the DN, e.g. "SELECT ... FROM
701 * ldap_entries AS attributeName WHERE attributeName.dn
702 * like '%attributeName=value%'"
704 backsql_strfcat_x( &bsi->bsi_flt_where,
705 bsi->bsi_op->o_tmpmemctx,
707 (ber_len_t)STRLENOF( "1=1" ), "1=1" );
708 bsi->bsi_status = LDAP_SUCCESS;
729 * Turn structuralObjectClass into objectClass
731 if ( ad == slap_schema.si_ad_objectClass
732 || ad == slap_schema.si_ad_structuralObjectClass )
735 * If the filter is LDAP_FILTER_PRESENT, then it's done;
736 * otherwise, let's see if we are lucky: filtering
737 * for "structural" objectclass or ancestor...
739 switch ( f->f_choice ) {
740 case LDAP_FILTER_EQUALITY:
742 ObjectClass *oc = oc_bvfind( &f->f_av_value );
745 Debug( LDAP_DEBUG_TRACE,
746 "backsql_process_filter(): "
747 "unknown objectClass \"%s\" "
749 f->f_av_value.bv_val, 0, 0 );
750 bsi->bsi_status = LDAP_OTHER;
756 * "structural" objectClass inheritance:
757 * - a search for "person" will also return
759 * - a search for "top" will return everything
761 if ( is_object_subclass( oc, bsi->bsi_oc->bom_oc ) ) {
762 static struct berval ldap_entry_objclasses = BER_BVC( "ldap_entry_objclasses" );
764 backsql_merge_from_tbls( bsi, &ldap_entry_objclasses );
766 backsql_strfcat_x( &bsi->bsi_flt_where,
767 bsi->bsi_op->o_tmpmemctx,
769 (ber_len_t)STRLENOF( "(2=2 OR (ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ')) */ ),
770 "(2=2 OR (ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ')) */,
771 &bsi->bsi_oc->bom_oc->soc_cname,
772 (ber_len_t)STRLENOF( /* ((' */ "'))" ),
774 bsi->bsi_status = LDAP_SUCCESS;
782 case LDAP_FILTER_PRESENT:
783 backsql_strfcat_x( &bsi->bsi_flt_where,
784 bsi->bsi_op->o_tmpmemctx,
786 (ber_len_t)STRLENOF( "3=3" ), "3=3" );
787 bsi->bsi_status = LDAP_SUCCESS;
791 /* FIXME: LDAP_FILTER_EXT? */
794 Debug( LDAP_DEBUG_TRACE,
795 "backsql_process_filter(): "
796 "illegal/unhandled filter "
797 "on objectClass attribute",
799 bsi->bsi_status = LDAP_OTHER;
804 } else if ( ad == slap_schema.si_ad_entryUUID ) {
806 #ifdef BACKSQL_ARBITRARY_KEY
807 struct berval keyval;
808 #else /* ! BACKSQL_ARBITRARY_KEY */
809 unsigned long keyval;
810 char keyvalbuf[] = "18446744073709551615";
811 #endif /* ! BACKSQL_ARBITRARY_KEY */
813 switch ( f->f_choice ) {
814 case LDAP_FILTER_EQUALITY:
815 backsql_entryUUID_decode( &f->f_av_value, &oc_id, &keyval );
817 if ( oc_id != bsi->bsi_oc->bom_id ) {
818 bsi->bsi_status = LDAP_SUCCESS;
823 #ifdef BACKSQL_ARBITRARY_KEY
824 backsql_strfcat_x( &bsi->bsi_flt_where,
825 bsi->bsi_op->o_tmpmemctx,
827 &bsi->bsi_oc->bom_keytbl, '.',
828 &bsi->bsi_oc->bom_keycol,
829 STRLENOF( " LIKE '" ), " LIKE '",
831 #else /* ! BACKSQL_ARBITRARY_KEY */
832 snprintf( keyvalbuf, sizeof( keyvalbuf ), "%lu", keyval );
833 backsql_strfcat_x( &bsi->bsi_flt_where,
834 bsi->bsi_op->o_tmpmemctx,
836 &bsi->bsi_oc->bom_keytbl, '.',
837 &bsi->bsi_oc->bom_keycol, '=', keyvalbuf );
838 #endif /* ! BACKSQL_ARBITRARY_KEY */
841 case LDAP_FILTER_PRESENT:
842 backsql_strfcat_x( &bsi->bsi_flt_where,
843 bsi->bsi_op->o_tmpmemctx,
845 (ber_len_t)STRLENOF( "4=4" ), "4=4" );
853 bsi->bsi_flags |= BSQL_SF_FILTER_ENTRYUUID;
857 #ifdef BACKSQL_SYNCPROV
858 } else if ( ad == slap_schema.si_ad_entryCSN ) {
860 * support for syncrepl as producer...
863 if ( !bsi->bsi_op->o_sync ) {
864 /* unsupported at present... */
865 bsi->bsi_status = LDAP_OTHER;
871 bsi->bsi_flags |= ( BSQL_SF_FILTER_ENTRYCSN | BSQL_SF_RETURN_ENTRYUUID);
873 /* if doing a syncrepl, try to return as much as possible,
874 * and always match the filter */
875 backsql_strfcat_x( &bsi->bsi_flt_where,
876 bsi->bsi_op->o_tmpmemctx,
878 (ber_len_t)STRLENOF( "5=5" ), "5=5" );
880 /* save for later use in operational attributes */
881 /* FIXME: saves only the first occurrence, because
882 * the filter during updates is written as
883 * "(&(entryCSN<={contextCSN})(entryCSN>={oldContextCSN})({filter}))"
884 * so we want our fake entryCSN to match the greatest
887 if ( bsi->bsi_op->o_private == NULL ) {
888 bsi->bsi_op->o_private = &f->f_av_value;
890 bsi->bsi_status = LDAP_SUCCESS;
894 #endif /* BACKSQL_SYNCPROV */
896 } else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) {
898 * FIXME: this is not robust; e.g. a filter
899 * '(!(hasSubordinates=TRUE))' fails because
900 * in SQL it would read 'NOT (1=1)' instead
902 * Note however that hasSubordinates is boolean,
903 * so a more appropriate filter would be
904 * '(hasSubordinates=FALSE)'
906 * A more robust search for hasSubordinates
907 * would * require joining the ldap_entries table
908 * selecting if there are descendants of the
911 backsql_strfcat_x( &bsi->bsi_flt_where,
912 bsi->bsi_op->o_tmpmemctx,
914 (ber_len_t)STRLENOF( "6=6" ), "6=6" );
915 if ( ad == slap_schema.si_ad_hasSubordinates ) {
917 * instruct candidate selection algorithm
918 * and attribute list to try to detect
919 * if an entry has subordinates
921 bsi->bsi_flags |= BSQL_SF_FILTER_HASSUBORDINATE;
925 * clear attributes to fetch, to require ALL
926 * and try extended match on all attributes
928 backsql_attrlist_add( bsi, NULL );
935 * attribute inheritance:
937 if ( backsql_supad2at( bsi->bsi_oc, ad, &vat ) ) {
938 bsi->bsi_status = LDAP_OTHER;
944 /* search anyway; other parts of the filter
946 backsql_strfcat_x( &bsi->bsi_flt_where,
947 bsi->bsi_op->o_tmpmemctx,
949 (ber_len_t)STRLENOF( "7=7" ), "7=7" );
950 bsi->bsi_status = LDAP_SUCCESS;
955 /* if required, open extra level of parens */
957 if ( vat[0]->bam_next || vat[1] ) {
958 backsql_strfcat_x( &bsi->bsi_flt_where,
959 bsi->bsi_op->o_tmpmemctx,
967 if ( backsql_process_filter_attr( bsi, f, vat[i] ) == -1 ) {
971 /* if more definitions of the same attr, apply */
972 if ( vat[i]->bam_next ) {
973 backsql_strfcat_x( &bsi->bsi_flt_where,
974 bsi->bsi_op->o_tmpmemctx,
976 STRLENOF( " OR " ), " OR " );
977 vat[i] = vat[i]->bam_next;
981 /* if more descendants of the same attr, apply */
984 backsql_strfcat_x( &bsi->bsi_flt_where,
985 bsi->bsi_op->o_tmpmemctx,
987 STRLENOF( " OR " ), " OR " );
991 /* if needed, close extra level of parens */
993 backsql_strfcat_x( &bsi->bsi_flt_where,
994 bsi->bsi_op->o_tmpmemctx,
1005 Debug( LDAP_DEBUG_TRACE,
1006 "<==backsql_process_filter() %s\n",
1007 rc == 1 ? "succeeded" : "failed", 0, 0);
1013 backsql_process_filter_eq( backsql_srch_info *bsi, backsql_at_map_rec *at,
1014 int casefold, struct berval *filter_value )
1017 * maybe we should check type of at->sel_expr here somehow,
1018 * to know whether upper_func is applicable, but for now
1019 * upper_func stuff is made for Oracle, where UPPER is
1020 * safely applicable to NUMBER etc.
1022 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
1025 backsql_strfcat_x( &bsi->bsi_flt_where,
1026 bsi->bsi_op->o_tmpmemctx,
1029 &at->bam_sel_expr_u,
1030 (ber_len_t)STRLENOF( "='" ),
1033 start = bsi->bsi_flt_where.bb_val.bv_len;
1035 backsql_strfcat_x( &bsi->bsi_flt_where,
1036 bsi->bsi_op->o_tmpmemctx,
1039 (ber_len_t)STRLENOF( /* (' */ "')" ),
1042 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
1045 backsql_strfcat_x( &bsi->bsi_flt_where,
1046 bsi->bsi_op->o_tmpmemctx,
1050 (ber_len_t)STRLENOF( "='" ), "='",
1052 (ber_len_t)STRLENOF( /* (' */ "')" ),
1060 backsql_process_filter_like( backsql_srch_info *bsi, backsql_at_map_rec *at,
1061 int casefold, struct berval *filter_value )
1064 * maybe we should check type of at->sel_expr here somehow,
1065 * to know whether upper_func is applicable, but for now
1066 * upper_func stuff is made for Oracle, where UPPER is
1067 * safely applicable to NUMBER etc.
1069 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
1072 backsql_strfcat_x( &bsi->bsi_flt_where,
1073 bsi->bsi_op->o_tmpmemctx,
1076 &at->bam_sel_expr_u,
1077 (ber_len_t)STRLENOF( " LIKE '%" ),
1080 start = bsi->bsi_flt_where.bb_val.bv_len;
1082 backsql_strfcat_x( &bsi->bsi_flt_where,
1083 bsi->bsi_op->o_tmpmemctx,
1086 (ber_len_t)STRLENOF( /* (' */ "%')" ),
1089 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
1092 backsql_strfcat_x( &bsi->bsi_flt_where,
1093 bsi->bsi_op->o_tmpmemctx,
1097 (ber_len_t)STRLENOF( " LIKE '%" ),
1100 (ber_len_t)STRLENOF( /* (' */ "%')" ),
1108 backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, backsql_at_map_rec *at )
1110 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1112 struct berval *filter_value = NULL;
1113 MatchingRule *matching_rule = NULL;
1114 struct berval ordering = BER_BVC("<=");
1116 Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter_attr(%s)\n",
1117 at->bam_ad->ad_cname.bv_val, 0, 0 );
1120 * need to add this attribute to list of attrs to load,
1121 * so that we can do test_filter() later
1123 backsql_attrlist_add( bsi, at->bam_ad );
1125 backsql_merge_from_tbls( bsi, &at->bam_from_tbls );
1127 if ( !BER_BVISNULL( &at->bam_join_where )
1128 && strstr( bsi->bsi_join_where.bb_val.bv_val,
1129 at->bam_join_where.bv_val ) == NULL )
1131 backsql_strfcat_x( &bsi->bsi_join_where,
1132 bsi->bsi_op->o_tmpmemctx,
1134 (ber_len_t)STRLENOF( " AND " ), " AND ",
1135 &at->bam_join_where );
1138 switch ( f->f_choice ) {
1139 case LDAP_FILTER_EQUALITY:
1140 filter_value = &f->f_av_value;
1141 matching_rule = at->bam_ad->ad_type->sat_equality;
1143 goto equality_match;
1145 /* fail over into next case */
1147 case LDAP_FILTER_EXT:
1148 filter_value = &f->f_mra->ma_value;
1149 matching_rule = f->f_mr_rule;
1152 /* always uppercase strings by now */
1153 #ifdef BACKSQL_UPPERCASE_FILTER
1154 if ( SLAP_MR_ASSOCIATED( matching_rule,
1155 bi->sql_caseIgnoreMatch ) )
1156 #endif /* BACKSQL_UPPERCASE_FILTER */
1161 /* FIXME: directoryString filtering should use a similar
1162 * approach to deal with non-prettified values like
1163 * " A non prettified value ", by using a LIKE
1164 * filter with all whitespaces collapsed to a single '%' */
1165 if ( SLAP_MR_ASSOCIATED( matching_rule,
1166 bi->sql_telephoneNumberMatch ) )
1172 * to check for matching telephone numbers
1173 * with intermized chars, e.g. val='1234'
1176 * val LIKE '%1%2%3%4%'
1179 bv.bv_len = 2 * filter_value->bv_len - 1;
1180 bv.bv_val = ch_malloc( bv.bv_len + 1 );
1182 bv.bv_val[ 0 ] = filter_value->bv_val[ 0 ];
1183 for ( i = 1; i < filter_value->bv_len; i++ ) {
1184 bv.bv_val[ 2 * i - 1 ] = '%';
1185 bv.bv_val[ 2 * i ] = filter_value->bv_val[ i ];
1187 bv.bv_val[ 2 * i - 1 ] = '\0';
1189 (void)backsql_process_filter_like( bsi, at, casefold, &bv );
1190 ch_free( bv.bv_val );
1195 /* NOTE: this is required by objectClass inheritance
1196 * and auxiliary objectClass use in filters for slightly
1197 * more efficient candidate selection. */
1198 /* FIXME: a bit too many specializations to deal with
1199 * very specific cases... */
1200 if ( at->bam_ad == slap_schema.si_ad_objectClass
1201 || at->bam_ad == slap_schema.si_ad_structuralObjectClass )
1203 backsql_strfcat_x( &bsi->bsi_flt_where,
1204 bsi->bsi_op->o_tmpmemctx,
1206 (ber_len_t)STRLENOF( "(ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */ ),
1207 "(ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */,
1209 (ber_len_t)STRLENOF( /* (' */ "')" ),
1215 * maybe we should check type of at->sel_expr here somehow,
1216 * to know whether upper_func is applicable, but for now
1217 * upper_func stuff is made for Oracle, where UPPER is
1218 * safely applicable to NUMBER etc.
1220 (void)backsql_process_filter_eq( bsi, at, casefold, filter_value );
1223 case LDAP_FILTER_GE:
1224 ordering.bv_val = ">=";
1226 /* fall thru to next case */
1228 case LDAP_FILTER_LE:
1229 filter_value = &f->f_av_value;
1231 /* always uppercase strings by now */
1232 #ifdef BACKSQL_UPPERCASE_FILTER
1233 if ( at->bam_ad->ad_type->sat_ordering &&
1234 SLAP_MR_ASSOCIATED( at->bam_ad->ad_type->sat_ordering,
1235 bi->sql_caseIgnoreMatch ) )
1236 #endif /* BACKSQL_UPPERCASE_FILTER */
1242 * FIXME: should we uppercase the operands?
1244 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
1247 backsql_strfcat_x( &bsi->bsi_flt_where,
1248 bsi->bsi_op->o_tmpmemctx,
1251 &at->bam_sel_expr_u,
1255 start = bsi->bsi_flt_where.bb_val.bv_len;
1257 backsql_strfcat_x( &bsi->bsi_flt_where,
1258 bsi->bsi_op->o_tmpmemctx,
1261 (ber_len_t)STRLENOF( /* (' */ "')" ),
1264 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
1267 backsql_strfcat_x( &bsi->bsi_flt_where,
1268 bsi->bsi_op->o_tmpmemctx,
1275 (ber_len_t)STRLENOF( /* (' */ "')" ),
1280 case LDAP_FILTER_PRESENT:
1281 backsql_strfcat_x( &bsi->bsi_flt_where,
1282 bsi->bsi_op->o_tmpmemctx,
1284 (ber_len_t)STRLENOF( "NOT (" /* ) */),
1287 (ber_len_t)STRLENOF( /* ( */ " IS NULL)" ),
1288 /* ( */ " IS NULL)" );
1291 case LDAP_FILTER_SUBSTRINGS:
1292 backsql_process_sub_filter( bsi, f, at );
1295 case LDAP_FILTER_APPROX:
1296 /* we do our best */
1299 * maybe we should check type of at->sel_expr here somehow,
1300 * to know whether upper_func is applicable, but for now
1301 * upper_func stuff is made for Oracle, where UPPER is
1302 * safely applicable to NUMBER etc.
1304 (void)backsql_process_filter_like( bsi, at, 1, &f->f_av_value );
1308 /* unhandled filter type; should not happen */
1310 backsql_strfcat_x( &bsi->bsi_flt_where,
1311 bsi->bsi_op->o_tmpmemctx,
1313 (ber_len_t)STRLENOF( "8=8" ), "8=8" );
1318 Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter_attr(%s)\n",
1319 at->bam_ad->ad_cname.bv_val, 0, 0 );
1325 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
1327 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1330 assert( query != NULL );
1331 BER_BVZERO( query );
1333 bsi->bsi_use_subtree_shortcut = 0;
1335 Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
1336 BER_BVZERO( &bsi->bsi_sel.bb_val );
1337 BER_BVZERO( &bsi->bsi_sel.bb_val );
1338 bsi->bsi_sel.bb_len = 0;
1339 BER_BVZERO( &bsi->bsi_from.bb_val );
1340 bsi->bsi_from.bb_len = 0;
1341 BER_BVZERO( &bsi->bsi_join_where.bb_val );
1342 bsi->bsi_join_where.bb_len = 0;
1343 BER_BVZERO( &bsi->bsi_flt_where.bb_val );
1344 bsi->bsi_flt_where.bb_len = 0;
1346 backsql_strfcat_x( &bsi->bsi_sel,
1347 bsi->bsi_op->o_tmpmemctx,
1349 (ber_len_t)STRLENOF( "SELECT DISTINCT ldap_entries.id," ),
1350 "SELECT DISTINCT ldap_entries.id,",
1351 &bsi->bsi_oc->bom_keytbl,
1353 &bsi->bsi_oc->bom_keycol,
1356 if ( !BER_BVISNULL( &bi->sql_strcast_func ) ) {
1357 backsql_strfcat_x( &bsi->bsi_sel,
1358 bsi->bsi_op->o_tmpmemctx,
1360 &bi->sql_strcast_func,
1361 (ber_len_t)STRLENOF( "('" /* ') */ ),
1363 &bsi->bsi_oc->bom_oc->soc_cname,
1364 (ber_len_t)STRLENOF( /* (' */ "')" ),
1367 backsql_strfcat_x( &bsi->bsi_sel,
1368 bsi->bsi_op->o_tmpmemctx,
1371 &bsi->bsi_oc->bom_oc->soc_cname,
1375 backsql_strfcat_x( &bsi->bsi_sel,
1376 bsi->bsi_op->o_tmpmemctx,
1378 &bi->sql_dn_oc_aliasing );
1379 backsql_strfcat_x( &bsi->bsi_from,
1380 bsi->bsi_op->o_tmpmemctx,
1382 (ber_len_t)STRLENOF( " FROM ldap_entries," ),
1383 " FROM ldap_entries,",
1384 &bsi->bsi_oc->bom_keytbl );
1386 backsql_strfcat_x( &bsi->bsi_join_where,
1387 bsi->bsi_op->o_tmpmemctx,
1389 (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
1390 &bsi->bsi_oc->bom_keytbl,
1392 &bsi->bsi_oc->bom_keycol,
1393 (ber_len_t)STRLENOF( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ),
1394 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
1396 switch ( bsi->bsi_scope ) {
1397 case LDAP_SCOPE_BASE:
1398 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1399 backsql_strfcat_x( &bsi->bsi_join_where,
1400 bsi->bsi_op->o_tmpmemctx,
1402 &bi->sql_upper_func,
1403 (ber_len_t)STRLENOF( "(ldap_entries.dn)=?" ),
1404 "(ldap_entries.dn)=?" );
1406 backsql_strfcat_x( &bsi->bsi_join_where,
1407 bsi->bsi_op->o_tmpmemctx,
1409 (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
1410 "ldap_entries.dn=?" );
1414 case BACKSQL_SCOPE_BASE_LIKE:
1415 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1416 backsql_strfcat_x( &bsi->bsi_join_where,
1417 bsi->bsi_op->o_tmpmemctx,
1419 &bi->sql_upper_func,
1420 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ),
1421 "(ldap_entries.dn) LIKE ?" );
1423 backsql_strfcat_x( &bsi->bsi_join_where,
1424 bsi->bsi_op->o_tmpmemctx,
1426 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ),
1427 "ldap_entries.dn LIKE ?" );
1431 case LDAP_SCOPE_ONELEVEL:
1432 backsql_strfcat_x( &bsi->bsi_join_where,
1433 bsi->bsi_op->o_tmpmemctx,
1435 (ber_len_t)STRLENOF( "ldap_entries.parent=?" ),
1436 "ldap_entries.parent=?" );
1439 case LDAP_SCOPE_SUBORDINATE:
1440 case LDAP_SCOPE_SUBTREE:
1441 if ( BACKSQL_USE_SUBTREE_SHORTCUT( bi ) ) {
1443 BackendDB *bd = bsi->bsi_op->o_bd;
1445 assert( bd->be_nsuffix != NULL );
1447 for ( i = 0; !BER_BVISNULL( &bd->be_nsuffix[ i ] ); i++ )
1449 if ( dn_match( &bd->be_nsuffix[ i ],
1450 bsi->bsi_base_ndn ) )
1452 /* pass this to the candidate selection
1453 * routine so that the DN is not bound
1454 * to the select statement */
1455 bsi->bsi_use_subtree_shortcut = 1;
1461 if ( bsi->bsi_use_subtree_shortcut ) {
1462 /* Skip the base DN filter, as every entry will match it */
1463 backsql_strfcat_x( &bsi->bsi_join_where,
1464 bsi->bsi_op->o_tmpmemctx,
1466 (ber_len_t)STRLENOF( "9=9"), "9=9");
1468 } else if ( !BER_BVISNULL( &bi->sql_subtree_cond ) ) {
1469 backsql_strfcat_x( &bsi->bsi_join_where,
1470 bsi->bsi_op->o_tmpmemctx,
1472 &bi->sql_subtree_cond );
1474 } else if ( BACKSQL_CANUPPERCASE( bi ) ) {
1475 backsql_strfcat_x( &bsi->bsi_join_where,
1476 bsi->bsi_op->o_tmpmemctx,
1478 &bi->sql_upper_func,
1479 (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ),
1480 "(ldap_entries.dn) LIKE ?" );
1483 backsql_strfcat_x( &bsi->bsi_join_where,
1484 bsi->bsi_op->o_tmpmemctx,
1486 (ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ),
1487 "ldap_entries.dn LIKE ?" );
1496 rc = backsql_process_filter( bsi, bsi->bsi_filter );
1498 struct berbuf bb = BB_NULL;
1500 backsql_strfcat_x( &bb,
1501 bsi->bsi_op->o_tmpmemctx,
1503 &bsi->bsi_sel.bb_val,
1504 &bsi->bsi_from.bb_val,
1505 &bsi->bsi_join_where.bb_val,
1506 (ber_len_t)STRLENOF( " AND " ), " AND ",
1507 &bsi->bsi_flt_where.bb_val );
1511 } else if ( rc < 0 ) {
1513 * Indicates that there's no possible way the filter matches
1514 * anything. No need to issue the query
1516 free( query->bv_val );
1517 BER_BVZERO( query );
1520 bsi->bsi_op->o_tmpfree( bsi->bsi_sel.bb_val.bv_val, bsi->bsi_op->o_tmpmemctx );
1521 BER_BVZERO( &bsi->bsi_sel.bb_val );
1522 bsi->bsi_sel.bb_len = 0;
1523 bsi->bsi_op->o_tmpfree( bsi->bsi_from.bb_val.bv_val, bsi->bsi_op->o_tmpmemctx );
1524 BER_BVZERO( &bsi->bsi_from.bb_val );
1525 bsi->bsi_from.bb_len = 0;
1526 bsi->bsi_op->o_tmpfree( bsi->bsi_join_where.bb_val.bv_val, bsi->bsi_op->o_tmpmemctx );
1527 BER_BVZERO( &bsi->bsi_join_where.bb_val );
1528 bsi->bsi_join_where.bb_len = 0;
1529 bsi->bsi_op->o_tmpfree( bsi->bsi_flt_where.bb_val.bv_val, bsi->bsi_op->o_tmpmemctx );
1530 BER_BVZERO( &bsi->bsi_flt_where.bb_val );
1531 bsi->bsi_flt_where.bb_len = 0;
1533 Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query() returns %s\n",
1534 query->bv_val ? query->bv_val : "NULL", 0, 0 );
1536 return ( rc <= 0 ? 1 : 0 );
1540 backsql_oc_get_candidates( void *v_oc, void *v_bsi )
1542 backsql_oc_map_rec *oc = v_oc;
1543 backsql_srch_info *bsi = v_bsi;
1544 Operation *op = bsi->bsi_op;
1545 backsql_info *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1546 struct berval query;
1547 SQLHSTMT sth = SQL_NULL_HSTMT;
1550 BACKSQL_ROW_NTS row;
1553 int n_candidates = bsi->bsi_n_candidates;
1556 * + 1 because we need room for '%';
1557 * + 1 because we need room for ',' for LDAP_SCOPE_SUBORDINATE;
1558 * this makes a subtree
1559 * search for a DN BACKSQL_MAX_DN_LEN long legal
1560 * if it returns that DN only
1562 char tmp_base_ndn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
1564 bsi->bsi_status = LDAP_SUCCESS;
1566 Debug( LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc=\"%s\"\n",
1567 BACKSQL_OC_NAME( oc ), 0, 0 );
1569 /* check for abandon */
1570 if ( op->o_abandon ) {
1571 bsi->bsi_status = SLAPD_ABANDON;
1572 return BACKSQL_AVL_STOP;
1575 if ( bsi->bsi_n_candidates == -1 ) {
1576 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1577 "unchecked limit has been overcome\n", 0, 0, 0 );
1578 /* should never get here */
1580 bsi->bsi_status = LDAP_ADMINLIMIT_EXCEEDED;
1581 return BACKSQL_AVL_STOP;
1585 res = backsql_srch_query( bsi, &query );
1587 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1588 "error while constructing query for objectclass \"%s\"\n",
1589 oc->bom_oc->soc_cname.bv_val, 0, 0 );
1591 * FIXME: need to separate errors from legally
1592 * impossible filters
1594 switch ( bsi->bsi_status ) {
1596 case LDAP_UNDEFINED_TYPE:
1597 case LDAP_NO_SUCH_OBJECT:
1598 /* we are conservative... */
1600 bsi->bsi_status = LDAP_SUCCESS;
1602 return BACKSQL_AVL_CONTINUE;
1604 case LDAP_ADMINLIMIT_EXCEEDED:
1606 /* don't try any more */
1607 return BACKSQL_AVL_STOP;
1611 if ( BER_BVISNULL( &query ) ) {
1612 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1613 "could not construct query for objectclass \"%s\"\n",
1614 oc->bom_oc->soc_cname.bv_val, 0, 0 );
1615 bsi->bsi_status = LDAP_SUCCESS;
1616 return BACKSQL_AVL_CONTINUE;
1619 Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n",
1620 query.bv_val, 0, 0 );
1622 rc = backsql_Prepare( bsi->bsi_dbh, &sth, query.bv_val, 0 );
1623 bsi->bsi_op->o_tmpfree( query.bv_val, bsi->bsi_op->o_tmpmemctx );
1624 BER_BVZERO( &query );
1625 if ( rc != SQL_SUCCESS ) {
1626 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1627 "error preparing query\n", 0, 0, 0 );
1628 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
1629 bsi->bsi_status = LDAP_OTHER;
1630 return BACKSQL_AVL_CONTINUE;
1633 Debug( LDAP_DEBUG_TRACE, "id: '%ld'\n", bsi->bsi_oc->bom_id, 0, 0 );
1635 rc = backsql_BindParamInt( sth, 1, SQL_PARAM_INPUT,
1636 &bsi->bsi_oc->bom_id );
1637 if ( rc != SQL_SUCCESS ) {
1638 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1639 "error binding objectclass id parameter\n", 0, 0, 0 );
1640 bsi->bsi_status = LDAP_OTHER;
1641 return BACKSQL_AVL_CONTINUE;
1644 switch ( bsi->bsi_scope ) {
1645 case LDAP_SCOPE_BASE:
1646 case BACKSQL_SCOPE_BASE_LIKE:
1648 * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
1649 * however this should be handled earlier
1651 if ( bsi->bsi_base_ndn->bv_len > BACKSQL_MAX_DN_LEN ) {
1652 bsi->bsi_status = LDAP_OTHER;
1653 return BACKSQL_AVL_CONTINUE;
1656 AC_MEMCPY( tmp_base_ndn, bsi->bsi_base_ndn->bv_val,
1657 bsi->bsi_base_ndn->bv_len + 1 );
1659 /* uppercase DN only if the stored DN can be uppercased
1661 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1662 ldap_pvt_str2upper( tmp_base_ndn );
1665 Debug( LDAP_DEBUG_TRACE, "(base)dn: \"%s\"\n",
1666 tmp_base_ndn, 0, 0 );
1668 rc = backsql_BindParamStr( sth, 2, SQL_PARAM_INPUT,
1669 tmp_base_ndn, BACKSQL_MAX_DN_LEN );
1670 if ( rc != SQL_SUCCESS ) {
1671 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1672 "error binding base_ndn parameter\n", 0, 0, 0 );
1673 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh,
1675 bsi->bsi_status = LDAP_OTHER;
1676 return BACKSQL_AVL_CONTINUE;
1680 case LDAP_SCOPE_SUBORDINATE:
1681 case LDAP_SCOPE_SUBTREE:
1683 /* if short-cutting the search base,
1684 * don't bind any parameter */
1685 if ( bsi->bsi_use_subtree_shortcut ) {
1690 * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
1691 * however this should be handled earlier
1693 if ( bsi->bsi_base_ndn->bv_len > BACKSQL_MAX_DN_LEN ) {
1694 bsi->bsi_status = LDAP_OTHER;
1695 return BACKSQL_AVL_CONTINUE;
1699 * Sets the parameters for the SQL built earlier
1700 * NOTE that all the databases could actually use
1701 * the TimesTen version, which would be cleaner
1702 * and would also eliminate the need for the
1703 * subtree_cond line in the configuration file.
1704 * For now, I'm leaving it the way it is,
1705 * so non-TimesTen databases use the original code.
1706 * But at some point this should get cleaned up.
1708 * If "dn" is being used, do a suffix search.
1709 * If "dn_ru" is being used, do a prefix search.
1711 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
1712 tmp_base_ndn[ 0 ] = '\0';
1714 for ( i = 0, j = bsi->bsi_base_ndn->bv_len - 1;
1716 tmp_base_ndn[ i ] = bsi->bsi_base_ndn->bv_val[ j ];
1719 if ( bsi->bsi_scope == LDAP_SCOPE_SUBORDINATE ) {
1720 tmp_base_ndn[ i++ ] = ',';
1723 tmp_base_ndn[ i ] = '%';
1724 tmp_base_ndn[ i + 1 ] = '\0';
1729 tmp_base_ndn[ i++ ] = '%';
1731 if ( bsi->bsi_scope == LDAP_SCOPE_SUBORDINATE ) {
1732 tmp_base_ndn[ i++ ] = ',';
1735 AC_MEMCPY( &tmp_base_ndn[ i ], bsi->bsi_base_ndn->bv_val,
1736 bsi->bsi_base_ndn->bv_len + 1 );
1739 /* uppercase DN only if the stored DN can be uppercased
1741 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1742 ldap_pvt_str2upper( tmp_base_ndn );
1745 if ( bsi->bsi_scope == LDAP_SCOPE_SUBORDINATE ) {
1746 Debug( LDAP_DEBUG_TRACE, "(children)dn: \"%s\"\n",
1747 tmp_base_ndn, 0, 0 );
1749 Debug( LDAP_DEBUG_TRACE, "(sub)dn: \"%s\"\n",
1750 tmp_base_ndn, 0, 0 );
1753 rc = backsql_BindParamStr( sth, 2, SQL_PARAM_INPUT,
1754 tmp_base_ndn, BACKSQL_MAX_DN_LEN );
1755 if ( rc != SQL_SUCCESS ) {
1756 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1757 "error binding base_ndn parameter (2)\n",
1759 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh,
1761 bsi->bsi_status = LDAP_OTHER;
1762 return BACKSQL_AVL_CONTINUE;
1767 case LDAP_SCOPE_ONELEVEL:
1768 assert( !BER_BVISNULL( &bsi->bsi_base_id.eid_ndn ) );
1770 #ifdef BACKSQL_ARBITRARY_KEY
1771 Debug( LDAP_DEBUG_TRACE, "(one)id: \"%s\"\n",
1772 bsi->bsi_base_id.eid_id.bv_val, 0, 0 );
1773 #else /* ! BACKSQL_ARBITRARY_KEY */
1774 Debug( LDAP_DEBUG_TRACE, "(one)id: '%lu'\n",
1775 bsi->bsi_base_id.eid_id, 0, 0 );
1776 #endif /* ! BACKSQL_ARBITRARY_KEY */
1777 rc = backsql_BindParamID( sth, 2, SQL_PARAM_INPUT,
1778 &bsi->bsi_base_id.eid_id );
1779 if ( rc != SQL_SUCCESS ) {
1780 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1781 "error binding base id parameter\n", 0, 0, 0 );
1782 bsi->bsi_status = LDAP_OTHER;
1783 return BACKSQL_AVL_CONTINUE;
1788 rc = SQLExecute( sth );
1789 if ( !BACKSQL_SUCCESS( rc ) ) {
1790 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1791 "error executing query\n", 0, 0, 0 );
1792 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
1793 SQLFreeStmt( sth, SQL_DROP );
1794 bsi->bsi_status = LDAP_OTHER;
1795 return BACKSQL_AVL_CONTINUE;
1798 backsql_BindRowAsStrings_x( sth, &row, bsi->bsi_op->o_tmpmemctx );
1799 rc = SQLFetch( sth );
1800 for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
1801 struct berval dn, pdn, ndn;
1802 backsql_entryID *c_id = NULL;
1805 ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
1807 if ( backsql_api_odbc2dn( bsi->bsi_op, bsi->bsi_rs, &dn ) ) {
1811 ret = dnPrettyNormal( NULL, &dn, &pdn, &ndn, op->o_tmpmemctx );
1812 if ( dn.bv_val != row.cols[ 3 ] ) {
1816 if ( ret != LDAP_SUCCESS ) {
1820 if ( bi->sql_baseObject && dn_match( &ndn, &bi->sql_baseObject->e_nname ) ) {
1824 c_id = (backsql_entryID *)op->o_tmpcalloc( 1,
1825 sizeof( backsql_entryID ), op->o_tmpmemctx );
1826 #ifdef BACKSQL_ARBITRARY_KEY
1827 ber_str2bv_x( row.cols[ 0 ], 0, 1, &c_id->eid_id,
1829 ber_str2bv_x( row.cols[ 1 ], 0, 1, &c_id->eid_keyval,
1831 #else /* ! BACKSQL_ARBITRARY_KEY */
1832 if ( lutil_atoulx( &c_id->eid_id, row.cols[ 0 ], 0 ) != 0 ) {
1835 if ( lutil_atoulx( &c_id->eid_keyval, row.cols[ 1 ], 0 ) != 0 ) {
1838 #endif /* ! BACKSQL_ARBITRARY_KEY */
1839 c_id->eid_oc_id = bsi->bsi_oc->bom_id;
1842 c_id->eid_ndn = ndn;
1844 /* append at end of list ... */
1845 c_id->eid_next = NULL;
1846 *bsi->bsi_id_listtail = c_id;
1847 bsi->bsi_id_listtail = &c_id->eid_next;
1849 #ifdef BACKSQL_ARBITRARY_KEY
1850 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1851 "added entry id=%s, keyval=%s dn=\"%s\"\n",
1852 c_id->eid_id.bv_val, c_id->eid_keyval.bv_val,
1854 #else /* ! BACKSQL_ARBITRARY_KEY */
1855 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1856 "added entry id=%ld, keyval=%ld dn=\"%s\"\n",
1857 c_id->eid_id, c_id->eid_keyval, row.cols[ 3 ] );
1858 #endif /* ! BACKSQL_ARBITRARY_KEY */
1860 /* count candidates, for unchecked limit */
1861 bsi->bsi_n_candidates--;
1862 if ( bsi->bsi_n_candidates == -1 ) {
1868 if ( !BER_BVISNULL( &pdn ) ) {
1869 op->o_tmpfree( pdn.bv_val, op->o_tmpmemctx );
1871 if ( !BER_BVISNULL( &ndn ) ) {
1872 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
1874 if ( c_id != NULL ) {
1878 backsql_FreeRow_x( &row, bsi->bsi_op->o_tmpmemctx );
1879 SQLFreeStmt( sth, SQL_DROP );
1881 Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates(): %d\n",
1882 n_candidates - bsi->bsi_n_candidates, 0, 0 );
1884 return ( bsi->bsi_n_candidates == -1 ? BACKSQL_AVL_STOP : BACKSQL_AVL_CONTINUE );
1888 backsql_search( Operation *op, SlapReply *rs )
1890 backsql_info *bi = (backsql_info *)op->o_bd->be_private;
1891 SQLHDBC dbh = SQL_NULL_HDBC;
1893 Entry user_entry = { 0 },
1895 int manageDSAit = get_manageDSAit( op );
1896 time_t stoptime = 0;
1897 backsql_srch_info bsi = { 0 };
1898 backsql_entryID *eid = NULL;
1899 struct berval nbase = BER_BVNULL;
1901 Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
1902 "base=\"%s\", filter=\"%s\", scope=%d,",
1903 op->o_req_ndn.bv_val,
1904 op->ors_filterstr.bv_val,
1906 Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
1907 "attributes to load: %s\n",
1910 op->ors_attrs == NULL ? "all" : "custom list" );
1912 if ( op->o_req_ndn.bv_len > BACKSQL_MAX_DN_LEN ) {
1913 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1914 "search base length (%ld) exceeds max length (%d)\n",
1915 op->o_req_ndn.bv_len, BACKSQL_MAX_DN_LEN, 0 );
1917 * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
1918 * since it is impossible that such a long DN exists
1921 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1922 send_ldap_result( op, rs );
1926 sres = backsql_get_db_conn( op, &dbh );
1927 if ( sres != LDAP_SUCCESS ) {
1928 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1929 "could not get connection handle - exiting\n",
1932 rs->sr_text = sres == LDAP_OTHER ? "SQL-backend error" : NULL;
1933 send_ldap_result( op, rs );
1937 /* compute it anyway; root does not use it */
1938 stoptime = op->o_time + op->ors_tlimit;
1941 bsi.bsi_e = &base_entry;
1942 rs->sr_err = backsql_init_search( &bsi, &op->o_req_ndn,
1944 stoptime, op->ors_filter,
1945 dbh, op, rs, op->ors_attrs,
1946 ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
1947 switch ( rs->sr_err ) {
1952 if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
1953 dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) )
1955 rs->sr_err = LDAP_SUCCESS;
1957 rs->sr_matched = NULL;
1959 ber_bvarray_free( rs->sr_ref );
1965 /* an entry was created; free it */
1966 entry_clean( bsi.bsi_e );
1971 if ( !BER_BVISNULL( &base_entry.e_nname )
1972 && !access_allowed( op, &base_entry,
1973 slap_schema.si_ad_entry, NULL,
1974 ACL_DISCLOSE, NULL ) )
1976 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1978 ber_bvarray_free( rs->sr_ref );
1981 rs->sr_matched = NULL;
1985 send_ldap_result( op, rs );
1988 ber_bvarray_free( rs->sr_ref );
1992 if ( !BER_BVISNULL( &base_entry.e_nname ) ) {
1993 entry_clean( &base_entry );
1998 /* NOTE: __NEW__ "search" access is required
1999 * on searchBase object */
2003 if ( get_assert( op ) &&
2004 ( test_filter( op, &base_entry, get_assertion( op ) )
2005 != LDAP_COMPARE_TRUE ) )
2007 rs->sr_err = LDAP_ASSERTION_FAILED;
2010 if ( ! access_allowed_mask( op, &base_entry,
2011 slap_schema.si_ad_entry,
2012 NULL, ACL_SEARCH, NULL, &mask ) )
2014 if ( rs->sr_err == LDAP_SUCCESS ) {
2015 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
2019 if ( rs->sr_err != LDAP_SUCCESS ) {
2020 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
2021 rs->sr_err = LDAP_NO_SUCH_OBJECT;
2024 send_ldap_result( op, rs );
2031 bsi.bsi_n_candidates =
2032 ( op->ors_limit == NULL /* isroot == TRUE */ ? -2 :
2033 ( op->ors_limit->lms_s_unchecked == -1 ? -2 :
2034 ( op->ors_limit->lms_s_unchecked ) ) );
2036 switch ( bsi.bsi_scope ) {
2037 case LDAP_SCOPE_BASE:
2038 case BACKSQL_SCOPE_BASE_LIKE:
2040 * probably already found...
2042 bsi.bsi_id_list = &bsi.bsi_base_id;
2043 bsi.bsi_id_listtail = &bsi.bsi_base_id.eid_next;
2046 case LDAP_SCOPE_SUBTREE:
2048 * if baseObject is defined, and if it is the root
2049 * of the search, add it to the candidate list
2051 if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &bsi.bsi_base_id.eid_id ) )
2053 bsi.bsi_id_list = &bsi.bsi_base_id;
2054 bsi.bsi_id_listtail = &bsi.bsi_base_id.eid_next;
2061 * for each objectclass we try to construct query which gets IDs
2062 * of entries matching LDAP query filter and scope (or at least
2063 * candidates), and get the IDs
2065 avl_apply( bi->sql_oc_by_oc, backsql_oc_get_candidates,
2066 &bsi, BACKSQL_AVL_STOP, AVL_INORDER );
2068 /* check for abandon */
2069 if ( op->o_abandon ) {
2070 eid = bsi.bsi_id_list;
2071 rs->sr_err = SLAPD_ABANDON;
2076 if ( op->ors_limit != NULL /* isroot == FALSE */
2077 && op->ors_limit->lms_s_unchecked != -1
2078 && bsi.bsi_n_candidates == -1 )
2080 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
2081 send_ldap_result( op, rs );
2086 * now we load candidate entries (only those attributes
2087 * mentioned in attrs and filter), test it against full filter
2088 * and then send to client; don't free entry_id if baseObject...
2090 for ( eid = bsi.bsi_id_list;
2092 eid = backsql_free_entryID( op,
2093 eid, eid == &bsi.bsi_base_id ? 0 : 1 ) )
2096 Attribute *a_hasSubordinate = NULL,
2097 *a_entryUUID = NULL,
2102 /* check for abandon */
2103 if ( op->o_abandon ) {
2104 rs->sr_err = SLAPD_ABANDON;
2108 /* check time limit */
2109 if ( op->ors_tlimit != SLAP_NO_LIMIT
2110 && slap_get_time() > stoptime )
2112 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
2113 rs->sr_ctrls = NULL;
2114 rs->sr_ref = rs->sr_v2ref;
2118 #ifdef BACKSQL_ARBITRARY_KEY
2119 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
2120 "for entry id=%s, oc_id=%ld, keyval=%s\n",
2121 eid->eid_id.bv_val, eid->eid_oc_id,
2122 eid->eid_keyval.bv_val );
2123 #else /* ! BACKSQL_ARBITRARY_KEY */
2124 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
2125 "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
2126 eid->eid_id, eid->eid_oc_id, eid->eid_keyval );
2127 #endif /* ! BACKSQL_ARBITRARY_KEY */
2130 switch ( op->ors_scope ) {
2131 case LDAP_SCOPE_BASE:
2132 case BACKSQL_SCOPE_BASE_LIKE:
2133 if ( !dn_match( &eid->eid_ndn, &op->o_req_ndn ) ) {
2138 case LDAP_SCOPE_ONE:
2140 struct berval rdn = eid->eid_ndn;
2142 rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
2143 if ( !dnIsOneLevelRDN( &rdn ) ) {
2149 case LDAP_SCOPE_SUBORDINATE:
2150 /* discard the baseObject entry */
2151 if ( dn_match( &eid->eid_ndn, &op->o_req_ndn ) ) {
2155 case LDAP_SCOPE_SUBTREE:
2156 /* FIXME: this should never fail... */
2157 if ( !dnIsSuffix( &eid->eid_ndn, &op->o_req_ndn ) ) {
2164 if ( BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) {
2165 /* don't recollect baseObject... */
2166 e = bi->sql_baseObject;
2168 } else if ( eid == &bsi.bsi_base_id ) {
2169 /* don't recollect searchBase object... */
2173 bsi.bsi_e = &user_entry;
2174 rc = backsql_id2entry( &bsi, eid );
2175 if ( rc != LDAP_SUCCESS ) {
2176 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
2177 "error %d in backsql_id2entry() "
2178 "- skipping\n", rc, 0, 0 );
2184 if ( !manageDSAit &&
2185 op->ors_scope != LDAP_SCOPE_BASE &&
2186 op->ors_scope != BACKSQL_SCOPE_BASE_LIKE &&
2187 is_entry_referral( e ) )
2191 refs = get_entry_referrals( op, e );
2193 backsql_srch_info bsi2 = { 0 };
2194 Entry user_entry2 = { 0 };
2196 /* retry with the full entry... */
2197 bsi2.bsi_e = &user_entry2;
2198 rc = backsql_init_search( &bsi2,
2203 BACKSQL_ISF_GET_ENTRY );
2204 if ( rc == LDAP_SUCCESS ) {
2205 if ( is_entry_referral( &user_entry2 ) )
2207 refs = get_entry_referrals( op,
2210 rs->sr_err = LDAP_OTHER;
2212 backsql_entry_clean( op, &user_entry2 );
2214 if ( bsi2.bsi_attrs != NULL ) {
2215 op->o_tmpfree( bsi2.bsi_attrs,
2221 rs->sr_ref = referral_rewrite( refs,
2225 ber_bvarray_free( refs );
2229 rs->sr_err = LDAP_REFERRAL;
2232 rs->sr_text = "bad referral object";
2236 rs->sr_matched = user_entry.e_name.bv_val;
2237 send_search_reference( op, rs );
2239 ber_bvarray_free( rs->sr_ref );
2241 rs->sr_matched = NULL;
2242 rs->sr_entry = NULL;
2248 * We use this flag since we need to parse the filter
2249 * anyway; we should have used the frontend API function
2250 * filter_has_subordinates()
2252 if ( bsi.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
2253 rc = backsql_has_children( op, dbh, &e->e_nname );
2256 case LDAP_COMPARE_TRUE:
2257 case LDAP_COMPARE_FALSE:
2258 a_hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
2259 if ( a_hasSubordinate != NULL ) {
2260 for ( ap = &user_entry.e_attrs;
2262 ap = &(*ap)->a_next );
2264 *ap = a_hasSubordinate;
2270 Debug(LDAP_DEBUG_TRACE,
2271 "backsql_search(): "
2272 "has_children failed( %d)\n",
2279 if ( bsi.bsi_flags & BSQL_SF_FILTER_ENTRYUUID ) {
2280 a_entryUUID = backsql_operational_entryUUID( bi, eid );
2281 if ( a_entryUUID != NULL ) {
2283 ap = &user_entry.e_attrs;
2286 for ( ; *ap; ap = &(*ap)->a_next );
2292 #ifdef BACKSQL_SYNCPROV
2293 if ( bsi.bsi_flags & BSQL_SF_FILTER_ENTRYCSN ) {
2294 a_entryCSN = backsql_operational_entryCSN( op );
2295 if ( a_entryCSN != NULL ) {
2297 ap = &user_entry.e_attrs;
2300 for ( ; *ap; ap = &(*ap)->a_next );
2305 #endif /* BACKSQL_SYNCPROV */
2307 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE )
2309 rs->sr_attrs = op->ors_attrs;
2310 rs->sr_operational_attrs = NULL;
2312 rs->sr_flags = ( e == &user_entry ) ? REP_ENTRY_MODIFIABLE : 0;
2313 /* FIXME: need the whole entry (ITS#3480) */
2314 rs->sr_err = send_search_entry( op, rs );
2315 rs->sr_entry = NULL;
2316 rs->sr_attrs = NULL;
2317 rs->sr_operational_attrs = NULL;
2319 switch ( rs->sr_err ) {
2320 case LDAP_UNAVAILABLE:
2322 * FIXME: send_search_entry failed;
2325 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
2326 "connection lost\n", 0, 0, 0 );
2329 case LDAP_SIZELIMIT_EXCEEDED:
2335 if ( e == &user_entry ) {
2336 backsql_entry_clean( op, &user_entry );
2343 if ( rs->sr_nentries > 0 ) {
2344 rs->sr_ref = rs->sr_v2ref;
2345 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS
2349 rs->sr_err = bsi.bsi_status;
2353 if ( rs->sr_err != SLAPD_ABANDON ) {
2354 send_ldap_result( op, rs );
2357 /* cleanup in case of abandon */
2358 for ( ; eid != NULL;
2359 eid = backsql_free_entryID( op,
2360 eid, eid == &bsi.bsi_base_id ? 0 : 1 ) )
2363 backsql_entry_clean( op, &base_entry );
2365 /* in case we got here accidentally */
2366 backsql_entry_clean( op, &user_entry );
2368 if ( rs->sr_v2ref ) {
2369 ber_bvarray_free( rs->sr_v2ref );
2370 rs->sr_v2ref = NULL;
2373 #ifdef BACKSQL_SYNCPROV
2375 Operation op2 = *op;
2376 SlapReply rs2 = { 0 };
2378 slap_callback cb = { 0 };
2380 op2.o_tag = LDAP_REQ_ADD;
2381 op2.o_bd = select_backend( &op->o_bd->be_nsuffix[0], 0, 0 );
2383 op2.o_callback = &cb;
2385 e.e_name = op->o_bd->be_suffix[0];
2386 e.e_nname = op->o_bd->be_nsuffix[0];
2388 cb.sc_response = slap_null_cb;
2390 op2.o_bd->be_add( &op2, &rs2 );
2392 #endif /* BACKSQL_SYNCPROV */
2395 (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
2397 if ( bsi.bsi_attrs != NULL ) {
2398 op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
2401 if ( !BER_BVISNULL( &nbase )
2402 && nbase.bv_val != op->o_req_ndn.bv_val )
2404 ch_free( nbase.bv_val );
2407 /* restore scope ... FIXME: this should be done before ANY
2408 * frontend call that uses op */
2409 if ( op->ors_scope == BACKSQL_SCOPE_BASE_LIKE ) {
2410 op->ors_scope = LDAP_SCOPE_BASE;
2413 Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
2418 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
2425 AttributeDescription *at,
2429 backsql_srch_info bsi = { 0 };
2430 SQLHDBC dbh = SQL_NULL_HDBC;
2432 SlapReply rs = { 0 };
2433 AttributeName anlist[ 2 ];
2437 rc = backsql_get_db_conn( op, &dbh );
2443 anlist[ 0 ].an_name = at->ad_cname;
2444 anlist[ 0 ].an_desc = at;
2445 BER_BVZERO( &anlist[ 1 ].an_name );
2448 bsi.bsi_e = ch_malloc( sizeof( Entry ) );
2449 rc = backsql_init_search( &bsi,
2453 dbh, op, &rs, at ? anlist : NULL,
2454 BACKSQL_ISF_GET_ENTRY );
2456 if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
2457 (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
2460 if ( rc == LDAP_SUCCESS ) {
2462 #if 0 /* not supported at present */
2463 /* find attribute values */
2464 if ( is_entry_alias( bsi.bsi_e ) ) {
2465 Debug( LDAP_DEBUG_ACL,
2466 "<= backsql_entry_get: entry is an alias\n",
2468 rc = LDAP_ALIAS_PROBLEM;
2469 goto return_results;
2473 if ( is_entry_referral( bsi.bsi_e ) ) {
2474 Debug( LDAP_DEBUG_ACL,
2475 "<= backsql_entry_get: entry is a referral\n",
2478 goto return_results;
2481 if ( oc && !is_entry_objectclass( bsi.bsi_e, oc, 0 ) ) {
2482 Debug( LDAP_DEBUG_ACL,
2483 "<= backsql_entry_get: "
2484 "failed to find objectClass\n",
2486 rc = LDAP_NO_SUCH_ATTRIBUTE;
2487 goto return_results;
2494 if ( bsi.bsi_attrs != NULL ) {
2495 op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
2498 if ( rc != LDAP_SUCCESS ) {
2500 entry_free( bsi.bsi_e );
2508 backsql_entry_clean(
2514 ctx = ldap_pvt_thread_pool_context();
2516 if ( ctx == NULL || ctx != op->o_tmpmemctx ) {
2517 if ( !BER_BVISNULL( &e->e_name ) ) {
2518 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
2519 BER_BVZERO( &e->e_name );
2522 if ( !BER_BVISNULL( &e->e_nname ) ) {
2523 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
2524 BER_BVZERO( &e->e_nname );
2532 backsql_entry_release(
2537 backsql_entry_clean( op, e );