]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/search.c
benign buffer overflow fix (ITS#1964)
[openldap] / servers / slapd / back-sql / search.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
3  *
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.
8  */
9
10 #include "portable.h"
11
12 #ifdef SLAPD_SQL
13
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include "ac/string.h"
17 #include "slap.h"
18 #include "lber_pvt.h"
19 #include "ldap_pvt.h"
20 #include "back-sql.h"
21 #include "sql-wrap.h"
22 #include "schema-map.h"
23 #include "entry-id.h"
24 #include "util.h"
25
26 #define BACKSQL_STOP            0
27 #define BACKSQL_CONTINUE        1
28
29 static int backsql_process_filter( backsql_srch_info *bsi, Filter *f );
30
31 static int
32 backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad )
33 {
34         int             n_attrs = 0;
35         AttributeName   *an = NULL;
36
37         if ( bsi->attrs == NULL ) {
38                 return 1;
39         }
40
41         /*
42          * clear the list (retrieve all attrs)
43          */
44         if ( ad == NULL ) {
45                 ch_free( bsi->attrs );
46                 bsi->attrs = NULL;
47                 return 1;
48         }
49
50         for ( ; bsi->attrs[ n_attrs ].an_name.bv_val; n_attrs++ ) {
51                 an = &bsi->attrs[ n_attrs ];
52                 
53                 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
54                         "attribute '%s' is in list\n", 
55                         an->an_name.bv_val, 0, 0 );
56                 /*
57                  * We can live with strcmp because the attribute 
58                  * list has been normalized before calling be_search
59                  */
60                 if ( !BACKSQL_NCMP( &an->an_name, &ad->ad_cname ) ) {
61                         return 1;
62                 }
63         }
64         
65         Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
66                 "adding '%s' to list\n", ad->ad_cname.bv_val, 0, 0 );
67
68         an = (AttributeName *)ch_realloc( bsi->attrs,
69                         sizeof( AttributeName ) * ( n_attrs + 2 ) );
70         if ( an == NULL ) {
71                 return -1;
72         }
73
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;
78
79         bsi->attrs = an;
80         
81         return 1;
82 }
83
84 void
85 backsql_init_search(
86         backsql_srch_info       *bsi, 
87         backsql_info            *bi,
88         struct berval           *nbase, 
89         int                     scope, 
90         int                     slimit,
91         int                     tlimit,
92         time_t                  stoptime, 
93         Filter                  *filter, 
94         SQLHDBC                 dbh,
95         BackendDB               *be, 
96         Connection              *conn, 
97         Operation               *op,
98         AttributeName           *attrs )
99 {
100         AttributeName           *p;
101         
102         bsi->base_dn = nbase;
103         bsi->scope = scope;
104         bsi->slimit = slimit;
105         bsi->tlimit = tlimit;
106         bsi->filter = filter;
107         bsi->dbh = dbh;
108         bsi->be = be;
109         bsi->conn = conn;
110         bsi->op = op;
111         bsi->bsi_flags = 0;
112
113         /*
114          * handle "*"
115          */
116         if ( attrs == NULL || an_find( attrs, &AllUser ) ) {
117                 bsi->attrs = NULL;
118
119         } else {
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;
124                 
125                 for ( p = attrs; p->an_name.bv_val; p++ ) {
126                         /*
127                          * ignore "1.1"; handle "+"
128                          */
129                         if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
130                                 bsi->bsi_flags |= BSQL_SF_ALL_OPER;
131                                 continue;
132
133                         } else if ( BACKSQL_NCMP( &p->an_name, &NoAttrs ) == 0 ) {
134                                 continue;
135                         }
136
137                         backsql_attrlist_add( bsi, p->an_desc );
138                 }
139         }
140
141         bsi->abandon = 0;
142         bsi->id_list = NULL;
143         bsi->n_candidates = 0;
144         bsi->stoptime = stoptime;
145         bsi->bi = bi;
146         bsi->sel.bv_val = NULL;
147         bsi->sel.bv_len = 0;
148         bsi->sel_len = 0;
149         bsi->from.bv_val = NULL;
150         bsi->from.bv_len = 0;
151         bsi->from_len = 0;
152         bsi->join_where.bv_val = NULL;
153         bsi->join_where.bv_len = 0;
154         bsi->jwhere_len = 0;
155         bsi->flt_where.bv_val = NULL;
156         bsi->flt_where.bv_len = 0;
157         bsi->fwhere_len = 0;
158
159         bsi->status = LDAP_SUCCESS;
160 }
161
162 static int
163 backsql_process_filter_list( backsql_srch_info *bsi, Filter *f, int op )
164 {
165         int             res;
166
167         if ( !f ) {
168                 return 0;
169         }
170
171         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c", '(' /* ) */  );
172
173         while ( 1 ) {
174                 res = backsql_process_filter( bsi, f );
175                 if ( res < 0 ) {
176                         /*
177                          * TimesTen : If the query has no answers,
178                          * don't bother to run the query.
179                          */
180                         return -1;
181                 }
182  
183                 f = f->f_next;
184                 if ( f == NULL ) {
185                         break;
186                 }
187
188                 switch ( op ) {
189                 case LDAP_FILTER_AND:
190                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
191                                         (ber_len_t)sizeof( " AND " ) - 1, 
192                                                 " AND " );
193                         break;
194
195                 case LDAP_FILTER_OR:
196                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
197                                         (ber_len_t)sizeof( " OR " ) - 1,
198                                                 " OR " );
199                         break;
200                 }
201         }
202
203         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c", /* ( */ ')' );
204
205         return 1;
206 }
207
208 static int
209 backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f )
210 {
211         int                     i;
212         backsql_at_map_rec      *at;
213
214         if ( !f ) {
215                 return 0;
216         }
217
218         at = backsql_ad2at( bsi->oc, f->f_sub_desc );
219
220         assert( at );
221
222         /*
223          * When dealing with case-sensitive strings 
224          * we may omit normalization; however, normalized
225          * SQL filters are more liberal.
226          */
227
228         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c", '(' /* ) */  );
229
230         /* TimesTen */
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 ) {
234                 /*
235                  * If a pre-upper-cased version of the column exists, use it
236                  */
237                 if ( at->sel_expr_u.bv_val ) {
238                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, 
239                                         "bl",
240                                         &at->sel_expr_u,
241                                         (ber_len_t)sizeof( " LIKE '" ) - 1,
242                                                 " LIKE '" );
243                 } else {
244                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
245                                         "bcbcl",
246                                         &bsi->bi->upper_func,
247                                         '(',
248                                         &at->sel_expr,
249                                         ')', 
250                                         (ber_len_t)sizeof( " LIKE '" ) - 1,
251                                                 " LIKE '" );
252                 }
253         } else {
254                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "bl",
255                                 &at->sel_expr,
256                                 (ber_len_t)sizeof( " LIKE '" ) - 1, " LIKE '" );
257         }
258  
259         if ( f->f_sub_initial.bv_val != NULL ) {
260                 size_t  start;
261
262                 start = bsi->flt_where.bv_len;
263                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "b",
264                                 &f->f_sub_initial );
265                 if ( bsi->bi->upper_func.bv_val ) {
266                         ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
267                 }
268         }
269
270         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c", '%' );
271
272         if ( f->f_sub_any != NULL ) {
273                 for ( i = 0; f->f_sub_any[ i ].bv_val != NULL; i++ ) {
274                         size_t  start;
275
276 #ifdef BACKSQL_TRACE
277                         Debug( LDAP_DEBUG_TRACE, 
278                                 "==>backsql_process_sub_filter(): "
279                                 "sub_any='%s'\n", f->f_sub_any[ i ].bv_val,
280                                 0, 0 );
281 #endif /* BACKSQL_TRACE */
282
283                         start = bsi->flt_where.bv_len;
284                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
285                                         "bc",
286                                         &f->f_sub_any[ i ],
287                                         '%' );
288                         if ( bsi->bi->upper_func.bv_val ) {
289                                 /*
290                                  * Note: toupper('%') = '%'
291                                  */
292                                 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
293                         }
294                 }
295
296                 if ( f->f_sub_final.bv_val != NULL ) {
297                         size_t  start;
298
299                         start = bsi->flt_where.bv_len;
300                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "b",
301                                         &f->f_sub_final );
302                         if ( bsi->bi->upper_func.bv_val ) {
303                                 ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
304                         }
305                 }
306         }
307
308         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l", 
309                         (ber_len_t)sizeof( /* (' */ "')" ) - 1, /* ( */ "')" );
310  
311         return 1;
312 }
313
314 static int
315 backsql_process_filter( backsql_srch_info *bsi, Filter *f )
316 {
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;
322         int                     done = 0;
323         ber_len_t               len = 0;
324         /* TimesTen */
325         int                     rc = 0;
326         struct berval           *filter_value = NULL;
327
328         Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n", 0, 0, 0 );
329         if ( f == NULL || f->f_choice == SLAPD_FILTER_COMPUTED ) {
330                 return 0;
331         }
332
333         switch( f->f_choice ) {
334         case LDAP_FILTER_OR:
335                 rc = backsql_process_filter_list( bsi, f->f_or, 
336                                 LDAP_FILTER_OR );
337                 done = 1;
338                 break;
339                 
340         case LDAP_FILTER_AND:
341                 rc = backsql_process_filter_list( bsi, f->f_and,
342                                 LDAP_FILTER_AND );
343                 done = 1;
344                 break;
345
346         case LDAP_FILTER_NOT:
347                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
348                                 (ber_len_t)sizeof( "NOT (" /* ) */ ) - 1,
349                                         "NOT (" /* ) */ );
350                 rc = backsql_process_filter( bsi, f->f_not );
351                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "c",
352                                 /* ( */ ')' );
353                 done = 1;
354                 break;
355
356         case LDAP_FILTER_PRESENT:
357                 ad = f->f_desc;
358                 break;
359                 
360         case LDAP_FILTER_EXT:
361                 ad = f->f_mra->ma_desc;
362                 break;
363                 
364         default:
365                 ad = f->f_av_desc;
366                 break;
367         }
368
369         if ( rc == -1 ) {
370                 /* TimesTen : Don't run the query */
371                 goto impossible;
372         }
373  
374         if ( done ) {
375                 goto done;
376         }
377
378         /*
379          * Turn structuralObjectClass into objectClass
380          */
381         if ( ad == slap_schema.si_ad_objectClass 
382                         || ad == slap_schema.si_ad_structuralObjectClass ) {
383                 at = &oc_attr;
384                 backsql_strfcat( &at->sel_expr, &len, "cbc",
385                                 '\'', 
386                                 &bsi->oc->oc->soc_cname, 
387                                 '\'' );
388
389 #if defined(SLAP_X_FILTER_HASSUBORDINATES) || defined(SLAP_X_MRA_MATCH_DNATTRS)
390         } else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) {
391                 /*
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 
395                  * of no condition.  
396                  * Note however that hasSubordinates is boolean, 
397                  * so a more appropriate filter would be 
398                  * '(hasSubordinates=FALSE)'
399                  */
400                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
401                                 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
402                 if ( ad != NULL ) {
403                         /*
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()
408                          */
409                         bsi->bsi_flags |= BSQL_SF_FILTER_HASSUBORDINATE;
410                 } else {
411                         /*
412                          * clear attributes to fetch, to require ALL
413                          * and try extended match on all attributes
414                          */
415                         backsql_attrlist_add( bsi, NULL );
416                 }
417                 goto done;
418 #endif /* SLAP_X_FILTER_HASSUBORDINATES || SLAP_X_MRA_MATCH_DNATTRS */
419                 
420         } else {
421                 at = backsql_ad2at( bsi->oc, ad );
422         }
423
424         if ( at == NULL ) {
425                 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
426                         "attribute '%s' is not defined for objectclass '%s'\n",
427                         ad->ad_cname.bv_val, BACKSQL_OC_NAME( bsi->oc ), 0 );
428                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
429                                 (ber_len_t)sizeof( "1=0" ) - 1, "1=0" );
430                 goto impossible;
431         }
432
433         backsql_merge_from_clause( &bsi->from, &bsi->from_len, 
434                         &at->from_tbls );
435         /*
436          * need to add this attribute to list of attrs to load,
437          * so that we could do test_filter() later
438          */
439         backsql_attrlist_add( bsi, ad );
440
441         if ( at->join_where.bv_val != NULL 
442                         && strstr( bsi->join_where.bv_val, at->join_where.bv_val ) == NULL ) {
443                 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lb",
444                                 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
445                                 &at->join_where );
446         }
447
448 #if 0
449         /*
450          * FIXME: this is not required any more; however, note that
451          * attribute name syntax might collide with SQL legal aliases
452          */
453         if ( at != &oc_attr ) {
454                 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cblb",
455                                 ',',
456                                 &at->sel_expr,
457                                 (ber_len_t)sizeof( " AS " ) - 1, " AS ", 
458                                 &at->name );
459         }
460 #endif
461
462         switch ( f->f_choice ) {
463         case LDAP_FILTER_EQUALITY:
464                 filter_value = &f->f_av_value;
465                 goto equality_match;
466
467                 /* fail over next case */
468                 
469         case LDAP_FILTER_EXT:
470                 filter_value = &f->f_mra->ma_value;
471
472 equality_match:;
473                 /*
474                  * maybe we should check type of at->sel_expr here somehow,
475                  * to know whether upper_func is applicable, but for now
476                  * upper_func stuff is made for Oracle, where UPPER is
477                  * safely applicable to NUMBER etc.
478                  */
479                 if ( bsi->bi->upper_func.bv_val ) {
480                         size_t  start;
481
482                         if ( at->sel_expr_u.bv_val ) {
483                                 backsql_strfcat( &bsi->flt_where,
484                                                 &bsi->fwhere_len, "cbl",
485                                                 '(',
486                                                 &at->sel_expr_u, 
487                                                 (ber_len_t)sizeof( "='" ) - 1,
488                                                         "='" );
489                         } else {
490                                 backsql_strfcat( &bsi->flt_where,
491                                                 &bsi->fwhere_len, "cbcbl",
492                                                 '(' /* ) */ ,
493                                                 &bsi->bi->upper_func,
494                                                 '(' /* ) */ ,
495                                                 &at->sel_expr,
496                                                 (ber_len_t)sizeof( /* ( */ ")='" ) - 1,
497                                                         /* ( */ ")='" );
498                         }
499
500                         start = bsi->flt_where.bv_len;
501
502                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
503                                         "bl",
504                                         filter_value, 
505                                         (ber_len_t)sizeof( /* (' */ "')" ) - 1,
506                                                 /* (' */ "')" );
507
508                         ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
509
510                 } else {
511                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
512                                         "cblbl",
513                                         '(',
514                                         &at->sel_expr,
515                                         (ber_len_t)sizeof( "='" ) - 1, "='",
516                                         filter_value,
517                                         (ber_len_t)sizeof( /* (' */ "')" ) - 1,
518                                                 /* (' */ "')" );
519                 }
520                 break;
521
522         case LDAP_FILTER_GE:
523                 /*
524                  * FIXME: should we uppercase the operands?
525                  */
526                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
527                                 '(' /* ) */ ,
528                                 &at->sel_expr,
529                                 (ber_len_t)sizeof( ">=" ) - 1, ">=", 
530                                 &f->f_av_value,
531                                 /* ( */ ')' );
532                 break;
533                 
534         case LDAP_FILTER_LE:
535                 /*
536                  * FIXME: should we uppercase the operands?
537                  */
538                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
539                                 '(' /* ) */ ,
540                                 &at->sel_expr,
541                                 (ber_len_t)sizeof( "<=" ) - 1, "<=", 
542                                 &f->f_av_value,
543                                 /* ( */ ')' );
544                 break;
545
546         case LDAP_FILTER_PRESENT:
547                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "lbl",
548                                 (ber_len_t)sizeof( "NOT (" ) - 1, "NOT (", 
549                                 &at->sel_expr, 
550                                 (ber_len_t)sizeof( " IS NULL)" ) - 1, " IS NULL)" );
551                 break;
552
553         case LDAP_FILTER_SUBSTRINGS:
554                 backsql_process_sub_filter( bsi, f );
555                 break;
556
557         case LDAP_FILTER_APPROX:
558                 /* we do our best */
559
560                 /*
561                  * maybe we should check type of at->sel_expr here somehow,
562                  * to know whether upper_func is applicable, but for now
563                  * upper_func stuff is made for Oracle, where UPPER is
564                  * safely applicable to NUMBER etc.
565                  */
566                 if ( bsi->bi->upper_func.bv_val ) {
567                         size_t  start;
568
569                         if ( at->sel_expr_u.bv_val ) {
570                                 backsql_strfcat( &bsi->flt_where,
571                                                 &bsi->fwhere_len, "cbl",
572                                                 '(',
573                                                 &at->sel_expr_u, 
574                                                 (ber_len_t)sizeof( " LIKE '%" ) - 1,
575                                                         " LIKE '%" );
576                         } else {
577                                 backsql_strfcat( &bsi->flt_where,
578                                                 &bsi->fwhere_len, "cbcbl",
579                                                 '(' /* ) */ ,
580                                                 &bsi->bi->upper_func,
581                                                 '(' /* ) */ ,
582                                                 &at->sel_expr,
583                                                 (ber_len_t)sizeof( /* ( */ ") LIKE '%" ) - 1,
584                                                         /* ( */ ") LIKE '%" );
585                         }
586
587                         start = bsi->flt_where.bv_len;
588
589                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
590                                         "bl",
591                                         &f->f_av_value, 
592                                         (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
593                                                 /* (' */ "%')" );
594
595                         ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
596
597                 } else {
598                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
599                                         "cblbl",
600                                         '(',
601                                         &at->sel_expr,
602                                         (ber_len_t)sizeof( " LIKE '%" ) - 1,
603                                                 " LIKE '%",
604                                         &f->f_av_value,
605                                         (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
606                                                 /* (' */ "%')" );
607                 }
608                 break;
609
610         default:
611                 /* unhandled filter type; should not happen */
612                 assert( 0 );
613                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
614                                 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
615                 break;
616
617         }
618
619 done:
620         if ( oc_attr.sel_expr.bv_val != NULL ) {
621                 free( oc_attr.sel_expr.bv_val );
622         }
623         
624         Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter()\n", 0, 0, 0 );
625         return 1;
626
627 impossible:
628         if ( oc_attr.sel_expr.bv_val != NULL ) {
629                 free( oc_attr.sel_expr.bv_val );
630         }
631         Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter() returns -1\n",
632                         0, 0, 0 );
633         return -1;
634 }
635
636 static int
637 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
638 {
639         backsql_info    *bi = (backsql_info *)bsi->be->be_private;
640         ber_len_t       q_len = 0;
641         int             rc;
642
643         assert( query );
644         query->bv_val = NULL;
645         query->bv_len = 0;
646
647         Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
648         bsi->sel.bv_val = NULL;
649         bsi->sel.bv_len = 0;
650         bsi->sel_len = 0;
651         bsi->from.bv_val = NULL;
652         bsi->from.bv_len = 0;
653         bsi->from_len = 0;
654         bsi->join_where.bv_val = NULL;
655         bsi->join_where.bv_len = 0;
656         bsi->jwhere_len = 0;
657         bsi->flt_where.bv_val = NULL;
658         bsi->flt_where.bv_len = 0;
659         bsi->fwhere_len = 0;
660
661 #if 0
662         /*
663          * FIXME: this query has been split in case a string cast function
664          * is defined; more sophisticated (pattern based) function should
665          * be used
666          */
667         backsql_strcat( &bsi->sel, &bsi->sel_len,
668                         "SELECT DISTINCT ldap_entries.id,", 
669                         bsi->oc->keytbl.bv_val, ".", bsi->oc->keycol.bv_val,
670                         ",'", bsi->oc->name.bv_val, "' AS objectClass",
671                         ",ldap_entries.dn AS dn", NULL );
672 #endif
673
674         backsql_strfcat( &bsi->sel, &bsi->sel_len, "lbcbc",
675                         (ber_len_t)sizeof( "SELECT DISTINCT ldap_entries.id," ) - 1,
676                                 "SELECT DISTINCT ldap_entries.id,", 
677                         &bsi->oc->keytbl, 
678                         '.', 
679                         &bsi->oc->keycol, 
680                         ',' );
681
682         if ( bi->strcast_func.bv_val ) {
683                 backsql_strfcat( &bsi->sel, &bsi->sel_len, "blbl",
684                                 &bi->strcast_func, 
685                                 (ber_len_t)sizeof( "('" /* ') */ ) - 1,
686                                         "('" /* ') */ ,
687                                 &bsi->oc->oc->soc_cname,
688                                 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
689                                         /* (' */ "')" );
690         } else {
691                 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cbc",
692                                 '\'',
693                                 &bsi->oc->oc->soc_cname,
694                                 '\'' );
695         }
696         backsql_strfcat( &bsi->sel, &bsi->sel_len, "l",
697                         (ber_len_t)sizeof( " AS objectClass,ldap_entries.dn AS dn" ) - 1,
698                         " AS objectClass,ldap_entries.dn AS dn" );
699
700         backsql_strfcat( &bsi->from, &bsi->from_len, "lb",
701                         (ber_len_t)sizeof( " FROM ldap_entries," ) - 1,
702                                 " FROM ldap_entries,",
703                         &bsi->oc->keytbl );
704
705         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lbcbl",
706                         (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ",
707                         &bsi->oc->keytbl,
708                         '.',
709                         &bsi->oc->keycol,
710                         (ber_len_t)sizeof( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ) - 1,
711                                 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
712
713         switch ( bsi->scope ) {
714         case LDAP_SCOPE_BASE:
715                 if ( bsi->bi->upper_func.bv_val ) {
716                         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, 
717                                         "blbcb",
718                                         &bsi->bi->upper_func,
719                                         (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
720                                                 "(ldap_entries.dn)=",
721                                         &bsi->bi->upper_func_open,
722                                         '?', 
723                                         &bsi->bi->upper_func_close );
724                 } else {
725                         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
726                                         "l",
727                                         (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
728                                                 "ldap_entries.dn=?" );
729                 }
730                 break;
731                 
732         case LDAP_SCOPE_ONELEVEL:
733                 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "l",
734                                 (ber_len_t)sizeof( "ldap_entries.parent=?" ) - 1,
735                                         "ldap_entries.parent=?" );
736                 break;
737
738         case LDAP_SCOPE_SUBTREE:
739                 if ( bsi->bi->upper_func.bv_val ) {
740                         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, 
741                                         "blbcb",
742                                         &bsi->bi->upper_func,
743                                         (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
744                                                 "(ldap_entries.dn) LIKE ",
745                                         &bsi->bi->upper_func_open,
746                                         '?', 
747                                         &bsi->bi->upper_func_close );
748                 } else {
749                         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
750                                         "l",
751                                         (ber_len_t)sizeof( "ldap_entries.dn LIKE ?" ) - 1,
752                                                 "ldap_entries.dn LIKE ?" );
753                 }
754
755 #if 0
756                 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "b",
757                                 &bsi->bi->subtree_cond );
758 #endif
759                 break;
760
761         default:
762                 assert( 0 );
763         }
764
765         rc = backsql_process_filter( bsi, bsi->filter );
766         if ( rc > 0 ) {
767                 backsql_strfcat( query, &q_len, "bbblb",
768                                 &bsi->sel,
769                                 &bsi->from, 
770                                 &bsi->join_where,
771                                 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
772                                 &bsi->flt_where );
773
774         } else if ( rc < 0 ) {
775                 /* 
776                  * Indicates that there's no possible way the filter matches
777                  * anything.  No need to issue the query
778                  */
779                 Debug( LDAP_DEBUG_TRACE,
780                         "<==backsql_srch_query() returns NULL\n", 0, 0, 0 );
781                 free( query->bv_val );
782                 query->bv_val = NULL;
783         }
784  
785         free( bsi->sel.bv_val );
786         bsi->sel.bv_len = 0;
787         bsi->sel_len = 0;
788         free( bsi->from.bv_val );
789         bsi->from.bv_len = 0;
790         bsi->from_len = 0;
791         free( bsi->join_where.bv_val );
792         bsi->join_where.bv_len = 0;
793         bsi->jwhere_len = 0;
794         free( bsi->flt_where.bv_val );
795         bsi->flt_where.bv_len = 0;
796         bsi->fwhere_len = 0;
797         
798         Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query()\n", 0, 0, 0 );
799         
800         return ( query->bv_val == NULL ? 1 : 0 );
801 }
802
803 int
804 backsql_oc_get_candidates( backsql_oc_map_rec *oc, backsql_srch_info *bsi )
805 {
806         struct berval           query;
807         SQLHSTMT                sth;
808         RETCODE                 rc;
809         backsql_entryID         base_id, *c_id;
810         int                     res;
811         BACKSQL_ROW_NTS         row;
812         int                     i;
813         int                     j;
814  
815         Debug(  LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc='%s'\n",
816                         BACKSQL_OC_NAME( oc ), 0, 0 );
817
818         if ( bsi->n_candidates == -1 ) {
819                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
820                         "unchecked limit has been overcome\n", 0, 0, 0 );
821                 /* should never get here */
822                 assert( 0 );
823                 return BACKSQL_STOP;
824         }
825         
826         bsi->oc = oc;
827         if ( backsql_srch_query( bsi, &query ) ) {
828                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
829                         "could not construct query for objectclass\n",
830                         0, 0, 0 );
831                 return BACKSQL_CONTINUE;
832         }
833
834         Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n", 
835                         query.bv_val, 0, 0 );
836
837         rc = backsql_Prepare( bsi->dbh, &sth, query.bv_val, 0 );
838         free( query.bv_val );
839         if ( rc != SQL_SUCCESS ) {
840                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
841                         "error preparing query\n", 0, 0, 0 );
842                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
843                 return BACKSQL_CONTINUE;
844         }
845
846         if ( backsql_BindParamID( sth, 1, &bsi->oc->id ) != SQL_SUCCESS ) {
847                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
848                         "error binding objectclass id parameter\n", 0, 0, 0 );
849                 return BACKSQL_CONTINUE;
850         }
851
852         switch ( bsi->scope ) {
853         case LDAP_SCOPE_BASE:
854                 rc = backsql_BindParamStr( sth, 2, bsi->base_dn->bv_val,
855                                 BACKSQL_MAX_DN_LEN );
856                 if ( rc != SQL_SUCCESS ) {
857                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
858                                 "error binding base_dn parameter\n", 0, 0, 0 );
859                         backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, 
860                                         sth, rc );
861                         return BACKSQL_CONTINUE;
862                 }
863                 break;
864
865         case LDAP_SCOPE_SUBTREE: {
866
867                 /* 
868                  * + 1 because we need room for '%'; this makes a subtree
869                  * search for a DN BACKSQL_MAX_DN_LEN long legal 
870                  * if it returns that DN only
871                  */
872                 char            temp_base_dn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
873
874                 /*
875                  * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
876                  * however this should be handled earlier
877                  */
878                 assert( bsi->base_dn->bv_len <= BACKSQL_MAX_DN_LEN );
879                         
880                 /* 
881                  * Sets the parameters for the SQL built earlier
882                  * NOTE that all the databases could actually use 
883                  * the TimesTen version, which would be cleaner 
884                  * and would also eliminate the need for the
885                  * subtree_cond line in the configuration file.  
886                  * For now, I'm leaving it the way it is, 
887                  * so non-TimesTen databases use the original code.
888                  * But at some point this should get cleaned up.
889                  *
890                  * If "dn" is being used, do a suffix search.
891                  * If "dn_ru" is being used, do a prefix search.
892                  */
893                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bsi->bi ) ) {
894                         temp_base_dn[ 0 ] = '\0';
895                         for ( i = 0, j = bsi->base_dn->bv_len - 1;
896                                         j >= 0; i++, j--) {
897                                 temp_base_dn[ i ] = bsi->base_dn->bv_val[ j ];
898                         }
899                         temp_base_dn[ i ] = '%';
900                         temp_base_dn[ i + 1 ] = '\0';
901                         ldap_pvt_str2upper( temp_base_dn );
902
903                 } else {
904                         temp_base_dn[ 0 ] = '%';
905                         AC_MEMCPY( &temp_base_dn[ 1 ], bsi->base_dn->bv_val,
906                                 bsi->base_dn->bv_len + 1 );
907                         ldap_pvt_str2upper( &temp_base_dn[ 1 ] );
908                 }
909
910                 Debug( LDAP_DEBUG_TRACE, "dn '%s'\n", temp_base_dn, 0, 0 );
911
912                 rc = backsql_BindParamStr( sth, 2, temp_base_dn, 
913                                 BACKSQL_MAX_DN_LEN );
914                 if ( rc != SQL_SUCCESS ) {
915                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
916                                 "error binding base_dn parameter (2)\n",
917                                 0, 0, 0 );
918                         backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, 
919                                         sth, rc );
920                         return BACKSQL_CONTINUE;
921                 }
922                 break;
923         }
924
925         case LDAP_SCOPE_ONELEVEL:
926                 res = backsql_dn2id( bsi->bi, &base_id, 
927                                 bsi->dbh, bsi->base_dn );
928                 if ( res != LDAP_SUCCESS ) {
929                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
930                                 "could not retrieve base_dn id%s\n",
931                                 res == LDAP_NO_SUCH_OBJECT ? ": no such entry"
932                                 : "", 0, 0 );
933                         bsi->status = res;
934                         return BACKSQL_CONTINUE;
935                 }
936                 
937                 rc = backsql_BindParamID( sth, 2, &base_id.id );
938                 backsql_free_entryID( &base_id, 0 );
939                 if ( rc != SQL_SUCCESS ) {
940                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
941                                 "error binding base id parameter\n", 0, 0, 0 );
942                         return BACKSQL_CONTINUE;
943                 }
944                 break;
945         }
946         
947         rc = SQLExecute( sth );
948         if ( !BACKSQL_SUCCESS( rc ) ) {
949                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
950                         "error executing query\n", 0, 0, 0 );
951                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
952                 SQLFreeStmt( sth, SQL_DROP );
953                 return BACKSQL_CONTINUE;
954         }
955
956         backsql_BindRowAsStrings( sth, &row );
957         rc = SQLFetch( sth );
958         for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
959                 c_id = (backsql_entryID *)ch_calloc( 1, 
960                                 sizeof( backsql_entryID ) );
961                 c_id->id = strtol( row.cols[ 0 ], NULL, 0 );
962                 c_id->keyval = strtol( row.cols[ 1 ], NULL, 0 );
963                 c_id->oc_id = bsi->oc->id;
964                 ber_str2bv( row.cols[ 3 ], 0, 1, &c_id->dn );
965                 c_id->next = bsi->id_list;
966                 bsi->id_list = c_id;
967                 bsi->n_candidates--;
968
969                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
970                         "added entry id=%ld, keyval=%ld dn='%s'\n",
971                         c_id->id, c_id->keyval, row.cols[ 3 ] );
972
973                 if ( bsi->n_candidates == -1 ) {
974                         break;
975                 }
976         }
977         backsql_FreeRow( &row );
978         SQLFreeStmt( sth, SQL_DROP );
979
980         Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates()\n", 0, 0, 0 );
981
982         return ( bsi->n_candidates == -1 ? BACKSQL_STOP : BACKSQL_CONTINUE );
983 }
984
985 int
986 backsql_search(
987         BackendDB       *be,
988         Connection      *conn,
989         Operation       *op,
990         struct berval   *base,
991         struct berval   *nbase,
992         int             scope,
993         int             deref,
994         int             slimit,
995         int             tlimit,
996         Filter          *filter,
997         struct berval   *filterstr,
998         AttributeName   *attrs,
999         int             attrsonly )
1000 {
1001         backsql_info            *bi = (backsql_info *)be->be_private;
1002         SQLHDBC                 dbh;
1003         int                     sres;
1004         int                     nentries;
1005         Entry                   *entry, *res;
1006         int                     manageDSAit = get_manageDSAit( op );
1007         BerVarray               v2refs = NULL;
1008         time_t                  stoptime = 0;
1009         backsql_srch_info       srch_info;
1010         backsql_entryID         *eid = NULL;
1011         struct slap_limits_set  *limit = NULL;
1012         int                     isroot = 0;
1013
1014         Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
1015                 "base='%s', filter='%s', scope=%d,", 
1016                 nbase->bv_val, filterstr->bv_val, scope );
1017         Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
1018                 "attributes to load: %s\n",
1019                 deref, attrsonly, attrs == NULL ? "all" : "custom list" );
1020
1021         if ( nbase->bv_len > BACKSQL_MAX_DN_LEN ) {
1022                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1023                         "search base length (%ld) exceeds max length (%ld)\n", 
1024                         nbase->bv_len, BACKSQL_MAX_DN_LEN, 0 );
1025                 /*
1026                  * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
1027                  * since it is impossible that such a long DN exists
1028                  * in the backend
1029                  */
1030                 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED, 
1031                                 "", NULL, NULL, NULL );
1032                 return 1;
1033         }
1034
1035         sres = backsql_get_db_conn( be, conn, &dbh );
1036         if ( sres != LDAP_SUCCESS ) {
1037                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1038                         "could not get connection handle - exiting\n", 
1039                         0, 0, 0 );
1040                 send_ldap_result( conn, op, sres, "",
1041                                 sres == LDAP_OTHER ?  "SQL-backend error" : "",
1042                                 NULL, NULL );
1043                 return 1;
1044         }
1045
1046         /* TimesTen : Pass it along to the lower level routines */ 
1047         srch_info.use_reverse_dn = BACKSQL_USE_REVERSE_DN( bi ); 
1048  
1049         /* if not root, get appropriate limits */
1050         if ( be_isroot( be, &op->o_ndn ) ) {
1051                 isroot = 1;
1052         } else {
1053                 ( void ) get_limits( be, &op->o_ndn, &limit );
1054         }
1055
1056         /* The time/size limits come first because they require very little
1057          * effort, so there's no chance the candidates are selected and then 
1058          * the request is not honored only because of time/size constraints */
1059
1060         /* if no time limit requested, use soft limit (unless root!) */
1061         if ( isroot ) {
1062                 if ( tlimit == 0 ) {
1063                         tlimit = -1;    /* allow root to set no limit */
1064                 }
1065
1066                 if ( slimit == 0 ) {
1067                         slimit = -1;
1068                 }
1069
1070         } else {
1071                 /* if no limit is required, use soft limit */
1072                 if ( tlimit <= 0 ) {
1073                         tlimit = limit->lms_t_soft;
1074
1075                 /* if requested limit higher than hard limit, abort */
1076                 } else if ( tlimit > limit->lms_t_hard ) {
1077                         /* no hard limit means use soft instead */
1078                         if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
1079                                 tlimit = limit->lms_t_soft;
1080
1081                         /* positive hard limit means abort */
1082                         } else if ( limit->lms_t_hard > 0 ) {
1083                                 send_search_result( conn, op, 
1084                                                 LDAP_UNWILLING_TO_PERFORM,
1085                                                 NULL, NULL, NULL, NULL, 0 );
1086                                 return 0;
1087                         }
1088                 
1089                         /* negative hard limit means no limit */
1090                 }
1091                 
1092                 /* if no limit is required, use soft limit */
1093                 if ( slimit <= 0 ) {
1094                         slimit = limit->lms_s_soft;
1095
1096                 /* if requested limit higher than hard limit, abort */
1097                 } else if ( slimit > limit->lms_s_hard ) {
1098                         /* no hard limit means use soft instead */
1099                         if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
1100                                 slimit = limit->lms_s_soft;
1101
1102                         /* positive hard limit means abort */
1103                         } else if ( limit->lms_s_hard > 0 ) {
1104                                 send_search_result( conn, op, 
1105                                                 LDAP_UNWILLING_TO_PERFORM,
1106                                                 NULL, NULL, NULL, NULL, 0 );
1107                                 return 0;
1108                         }
1109                         
1110                         /* negative hard limit means no limit */
1111                 }
1112         }
1113
1114         /* compute it anyway; root does not use it */
1115         stoptime = op->o_time + tlimit;
1116
1117         backsql_init_search( &srch_info, bi, nbase, scope,
1118                         slimit, tlimit, stoptime, filter, dbh,
1119                         be, conn, op, attrs );
1120
1121         /*
1122          * for each objectclass we try to construct query which gets IDs
1123          * of entries matching LDAP query filter and scope (or at least 
1124          * candidates), and get the IDs
1125          */
1126         srch_info.n_candidates = ( isroot ? -2 : limit->lms_s_unchecked == -1 
1127                         ? -2 : limit->lms_s_unchecked );
1128         avl_apply( bi->oc_by_oc, (AVL_APPLY)backsql_oc_get_candidates,
1129                         &srch_info, BACKSQL_STOP, AVL_INORDER );
1130         if ( !isroot && limit->lms_s_unchecked != -1 ) {
1131                 if ( srch_info.n_candidates == -1 ) {
1132                         send_search_result( conn, op,
1133                                         LDAP_ADMINLIMIT_EXCEEDED,
1134                                         NULL, NULL, NULL, NULL, 0 );
1135                         goto done;
1136                 }
1137         }
1138         
1139         nentries = 0;
1140         /*
1141          * now we load candidate entries (only those attributes 
1142          * mentioned in attrs and filter), test it against full filter 
1143          * and then send to client
1144          */
1145         for ( eid = srch_info.id_list; eid != NULL; 
1146                         eid = backsql_free_entryID( eid, 1 ) ) {
1147 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1148                 Attribute       *hasSubordinate = NULL,
1149                                 *a = NULL;
1150 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1151
1152                 /* check for abandon */
1153                 if ( op->o_abandon ) {
1154                         break;
1155                 }
1156
1157                 /* check time limit */
1158                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
1159                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
1160                                 NULL, NULL, v2refs, NULL, nentries );
1161                         goto end_of_search;
1162                 }
1163
1164                 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1165                         "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
1166                         eid->id, eid->oc_id, eid->keyval );
1167
1168                 entry = (Entry *)ch_calloc( sizeof( Entry ), 1 );
1169                 res = backsql_id2entry( &srch_info, entry, eid );
1170                 if ( res == NULL ) {
1171                         Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1172                                 "error in backsql_id2entry() "
1173                                 "- skipping entry\n", 0, 0, 0 );
1174                         continue;
1175                 }
1176
1177                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
1178                         is_entry_referral( entry ) ) {
1179                         BerVarray refs = get_entry_referrals( be, conn,
1180                                         op, entry );
1181
1182                         send_search_reference( be, conn, op, entry, refs, 
1183                                         NULL, &v2refs );
1184                         ber_bvarray_free( refs );
1185                         continue;
1186                 }
1187
1188 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1189                 /*
1190                  * We use this flag since we need to parse the filter
1191                  * anyway; we should have used the frontend API function
1192                  * filter_has_subordinates()
1193                  */
1194                 if ( srch_info.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
1195                         int             rc;
1196
1197                         rc = backsql_has_children( bi, dbh, &entry->e_nname );
1198
1199                         switch( rc ) {
1200                         case LDAP_COMPARE_TRUE:
1201                         case LDAP_COMPARE_FALSE:
1202                                 hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
1203                                 if ( hasSubordinate != NULL ) {
1204                                         for ( a = entry->e_attrs; 
1205                                                         a && a->a_next; 
1206                                                         a = a->a_next );
1207
1208                                         a->a_next = hasSubordinate;
1209                                 }
1210                                 rc = 0;
1211                                 break;
1212
1213                         default:
1214                                 Debug(LDAP_DEBUG_TRACE, 
1215                                         "backsql_search(): "
1216                                         "has_children failed( %d)\n", 
1217                                         rc, 0, 0 );
1218                                 rc = 1;
1219                                 break;
1220                         }
1221
1222                         if ( rc ) {
1223                                 continue;
1224                         }
1225                 }
1226 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1227
1228                 if ( test_filter( be, conn, op, entry, filter ) 
1229                                 == LDAP_COMPARE_TRUE ) {
1230 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1231                         if ( hasSubordinate && !( srch_info.bsi_flags & BSQL_SF_ALL_OPER ) 
1232                                         && !ad_inlist( slap_schema.si_ad_hasSubordinates, attrs ) ) {
1233                                 a->a_next = NULL;
1234                                 attr_free( hasSubordinate );
1235                                 hasSubordinate = NULL;
1236                         }
1237 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1238
1239 #if 0   /* noop is masked SLAP_CTRL_UPDATE */
1240                         if ( op->o_noop ) {
1241                                 sres = 0;
1242                         } else {
1243 #endif
1244                                 sres = send_search_entry( be, conn, op, entry,
1245                                                 attrs, attrsonly, NULL );
1246 #if 0
1247                         }
1248 #endif
1249
1250                         switch ( sres ) {
1251                         case 0:
1252                                 nentries++;
1253                                 break;
1254
1255                         case -1:
1256                                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1257                                         "connection lost\n", 0, 0, 0 );
1258                                 goto end_of_search;
1259
1260                         default:
1261                                 /*
1262                                  * FIXME: send_search_entry failed;
1263                                  * better stop
1264                                  */
1265                                 break;
1266                         }
1267                 }
1268                 entry_free( entry );
1269
1270                 if ( slimit != -1 && nentries >= slimit ) {
1271                         send_search_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
1272                                 NULL, NULL, v2refs, NULL, nentries );
1273                         goto end_of_search;
1274                 }
1275         }
1276
1277 end_of_search:;
1278
1279         if ( nentries > 0 ) {
1280                 send_search_result( conn, op,
1281                         v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
1282                         NULL, NULL, v2refs, NULL, nentries );
1283         } else {
1284                 send_ldap_result( conn, op, srch_info.status,
1285                                 NULL, NULL, NULL, 0 );
1286         }
1287         
1288 done:;
1289         ch_free( srch_info.attrs );
1290
1291         Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
1292         return 0;
1293 }
1294
1295 #endif /* SLAPD_SQL */
1296