]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/search.c
Converted ch_malloc, ch_calloc and ch_realloc calls to SLAP_MALLOC,
[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 == slap_schema.si_ad_hasSubordinates ) {
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
411                 } else {
412                         /*
413                          * clear attributes to fetch, to require ALL
414                          * and try extended match on all attributes
415                          */
416                         backsql_attrlist_add( bsi, NULL );
417                 }
418                 goto done;
419 #endif /* SLAP_X_FILTER_HASSUBORDINATES || SLAP_X_MRA_MATCH_DNATTRS */
420                 
421         } else {
422                 at = backsql_ad2at( bsi->oc, ad );
423         }
424
425         if ( at == NULL ) {
426                 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
427                         "attribute '%s' is not defined for objectclass '%s'\n",
428                         ad->ad_cname.bv_val, BACKSQL_OC_NAME( bsi->oc ), 0 );
429                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
430                                 (ber_len_t)sizeof( "1=0" ) - 1, "1=0" );
431                 goto impossible;
432         }
433
434         backsql_merge_from_clause( &bsi->from, &bsi->from_len, 
435                         &at->from_tbls );
436         /*
437          * need to add this attribute to list of attrs to load,
438          * so that we could do test_filter() later
439          */
440         backsql_attrlist_add( bsi, ad );
441
442         if ( at->join_where.bv_val != NULL 
443                         && strstr( bsi->join_where.bv_val, at->join_where.bv_val ) == NULL ) {
444                 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lb",
445                                 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
446                                 &at->join_where );
447         }
448
449 #if 0
450         /*
451          * FIXME: this is not required any more; however, note that
452          * attribute name syntax might collide with SQL legal aliases
453          */
454         if ( at != &oc_attr ) {
455                 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cblb",
456                                 ',',
457                                 &at->sel_expr,
458                                 (ber_len_t)sizeof( " AS " ) - 1, " AS ", 
459                                 &at->name );
460         }
461 #endif
462
463         switch ( f->f_choice ) {
464         case LDAP_FILTER_EQUALITY:
465                 filter_value = &f->f_av_value;
466                 goto equality_match;
467
468                 /* fail over next case */
469                 
470         case LDAP_FILTER_EXT:
471                 filter_value = &f->f_mra->ma_value;
472
473 equality_match:;
474                 /*
475                  * maybe we should check type of at->sel_expr here somehow,
476                  * to know whether upper_func is applicable, but for now
477                  * upper_func stuff is made for Oracle, where UPPER is
478                  * safely applicable to NUMBER etc.
479                  */
480                 if ( bsi->bi->upper_func.bv_val ) {
481                         size_t  start;
482
483                         if ( at->sel_expr_u.bv_val ) {
484                                 backsql_strfcat( &bsi->flt_where,
485                                                 &bsi->fwhere_len, "cbl",
486                                                 '(',
487                                                 &at->sel_expr_u, 
488                                                 (ber_len_t)sizeof( "='" ) - 1,
489                                                         "='" );
490                         } else {
491                                 backsql_strfcat( &bsi->flt_where,
492                                                 &bsi->fwhere_len, "cbcbl",
493                                                 '(' /* ) */ ,
494                                                 &bsi->bi->upper_func,
495                                                 '(' /* ) */ ,
496                                                 &at->sel_expr,
497                                                 (ber_len_t)sizeof( /* ( */ ")='" ) - 1,
498                                                         /* ( */ ")='" );
499                         }
500
501                         start = bsi->flt_where.bv_len;
502
503                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
504                                         "bl",
505                                         filter_value, 
506                                         (ber_len_t)sizeof( /* (' */ "')" ) - 1,
507                                                 /* (' */ "')" );
508
509                         ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
510
511                 } else {
512                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
513                                         "cblbl",
514                                         '(',
515                                         &at->sel_expr,
516                                         (ber_len_t)sizeof( "='" ) - 1, "='",
517                                         filter_value,
518                                         (ber_len_t)sizeof( /* (' */ "')" ) - 1,
519                                                 /* (' */ "')" );
520                 }
521                 break;
522
523         case LDAP_FILTER_GE:
524                 /*
525                  * FIXME: should we uppercase the operands?
526                  */
527                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
528                                 '(' /* ) */ ,
529                                 &at->sel_expr,
530                                 (ber_len_t)sizeof( ">=" ) - 1, ">=", 
531                                 &f->f_av_value,
532                                 /* ( */ ')' );
533                 break;
534                 
535         case LDAP_FILTER_LE:
536                 /*
537                  * FIXME: should we uppercase the operands?
538                  */
539                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "cblbc",
540                                 '(' /* ) */ ,
541                                 &at->sel_expr,
542                                 (ber_len_t)sizeof( "<=" ) - 1, "<=", 
543                                 &f->f_av_value,
544                                 /* ( */ ')' );
545                 break;
546
547         case LDAP_FILTER_PRESENT:
548                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "lbl",
549                                 (ber_len_t)sizeof( "NOT (" ) - 1, "NOT (", 
550                                 &at->sel_expr, 
551                                 (ber_len_t)sizeof( " IS NULL)" ) - 1, " IS NULL)" );
552                 break;
553
554         case LDAP_FILTER_SUBSTRINGS:
555                 backsql_process_sub_filter( bsi, f );
556                 break;
557
558         case LDAP_FILTER_APPROX:
559                 /* we do our best */
560
561                 /*
562                  * maybe we should check type of at->sel_expr here somehow,
563                  * to know whether upper_func is applicable, but for now
564                  * upper_func stuff is made for Oracle, where UPPER is
565                  * safely applicable to NUMBER etc.
566                  */
567                 if ( bsi->bi->upper_func.bv_val ) {
568                         size_t  start;
569
570                         if ( at->sel_expr_u.bv_val ) {
571                                 backsql_strfcat( &bsi->flt_where,
572                                                 &bsi->fwhere_len, "cbl",
573                                                 '(',
574                                                 &at->sel_expr_u, 
575                                                 (ber_len_t)sizeof( " LIKE '%" ) - 1,
576                                                         " LIKE '%" );
577                         } else {
578                                 backsql_strfcat( &bsi->flt_where,
579                                                 &bsi->fwhere_len, "cbcbl",
580                                                 '(' /* ) */ ,
581                                                 &bsi->bi->upper_func,
582                                                 '(' /* ) */ ,
583                                                 &at->sel_expr,
584                                                 (ber_len_t)sizeof( /* ( */ ") LIKE '%" ) - 1,
585                                                         /* ( */ ") LIKE '%" );
586                         }
587
588                         start = bsi->flt_where.bv_len;
589
590                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
591                                         "bl",
592                                         &f->f_av_value, 
593                                         (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
594                                                 /* (' */ "%')" );
595
596                         ldap_pvt_str2upper( &bsi->flt_where.bv_val[ start ] );
597
598                 } else {
599                         backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len,
600                                         "cblbl",
601                                         '(',
602                                         &at->sel_expr,
603                                         (ber_len_t)sizeof( " LIKE '%" ) - 1,
604                                                 " LIKE '%",
605                                         &f->f_av_value,
606                                         (ber_len_t)sizeof( /* (' */ "%')" ) - 1,
607                                                 /* (' */ "%')" );
608                 }
609                 break;
610
611         default:
612                 /* unhandled filter type; should not happen */
613                 assert( 0 );
614                 backsql_strfcat( &bsi->flt_where, &bsi->fwhere_len, "l",
615                                 (ber_len_t)sizeof( "1=1" ) - 1, "1=1" );
616                 break;
617
618         }
619
620 done:
621         if ( oc_attr.sel_expr.bv_val != NULL ) {
622                 free( oc_attr.sel_expr.bv_val );
623         }
624         
625         Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter()\n", 0, 0, 0 );
626         return 1;
627
628 impossible:
629         if ( oc_attr.sel_expr.bv_val != NULL ) {
630                 free( oc_attr.sel_expr.bv_val );
631         }
632         Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter() returns -1\n",
633                         0, 0, 0 );
634         return -1;
635 }
636
637 static int
638 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
639 {
640         backsql_info    *bi = (backsql_info *)bsi->be->be_private;
641         ber_len_t       q_len = 0;
642         int             rc;
643
644         assert( query );
645         query->bv_val = NULL;
646         query->bv_len = 0;
647
648         Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
649         bsi->sel.bv_val = NULL;
650         bsi->sel.bv_len = 0;
651         bsi->sel_len = 0;
652         bsi->from.bv_val = NULL;
653         bsi->from.bv_len = 0;
654         bsi->from_len = 0;
655         bsi->join_where.bv_val = NULL;
656         bsi->join_where.bv_len = 0;
657         bsi->jwhere_len = 0;
658         bsi->flt_where.bv_val = NULL;
659         bsi->flt_where.bv_len = 0;
660         bsi->fwhere_len = 0;
661
662 #if 0
663         /*
664          * FIXME: this query has been split in case a string cast function
665          * is defined; more sophisticated (pattern based) function should
666          * be used
667          */
668         backsql_strcat( &bsi->sel, &bsi->sel_len,
669                         "SELECT DISTINCT ldap_entries.id,", 
670                         bsi->oc->keytbl.bv_val, ".", bsi->oc->keycol.bv_val,
671                         ",'", bsi->oc->name.bv_val, "' AS objectClass",
672                         ",ldap_entries.dn AS dn", NULL );
673 #endif
674
675         backsql_strfcat( &bsi->sel, &bsi->sel_len, "lbcbc",
676                         (ber_len_t)sizeof( "SELECT DISTINCT ldap_entries.id," ) - 1,
677                                 "SELECT DISTINCT ldap_entries.id,", 
678                         &bsi->oc->keytbl, 
679                         '.', 
680                         &bsi->oc->keycol, 
681                         ',' );
682
683         if ( bi->strcast_func.bv_val ) {
684                 backsql_strfcat( &bsi->sel, &bsi->sel_len, "blbl",
685                                 &bi->strcast_func, 
686                                 (ber_len_t)sizeof( "('" /* ') */ ) - 1,
687                                         "('" /* ') */ ,
688                                 &bsi->oc->oc->soc_cname,
689                                 (ber_len_t)sizeof( /* (' */ "')" ) - 1,
690                                         /* (' */ "')" );
691         } else {
692                 backsql_strfcat( &bsi->sel, &bsi->sel_len, "cbc",
693                                 '\'',
694                                 &bsi->oc->oc->soc_cname,
695                                 '\'' );
696         }
697         backsql_strfcat( &bsi->sel, &bsi->sel_len, "l",
698                         (ber_len_t)sizeof( " AS objectClass,ldap_entries.dn AS dn" ) - 1,
699                         " AS objectClass,ldap_entries.dn AS dn" );
700
701         backsql_strfcat( &bsi->from, &bsi->from_len, "lb",
702                         (ber_len_t)sizeof( " FROM ldap_entries," ) - 1,
703                                 " FROM ldap_entries,",
704                         &bsi->oc->keytbl );
705
706         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "lbcbl",
707                         (ber_len_t)sizeof( " WHERE " ) - 1, " WHERE ",
708                         &bsi->oc->keytbl,
709                         '.',
710                         &bsi->oc->keycol,
711                         (ber_len_t)sizeof( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ) - 1,
712                                 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
713
714         switch ( bsi->scope ) {
715         case LDAP_SCOPE_BASE:
716                 if ( bsi->bi->upper_func.bv_val ) {
717                         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, 
718                                         "blbcb",
719                                         &bsi->bi->upper_func,
720                                         (ber_len_t)sizeof( "(ldap_entries.dn)=" ) - 1,
721                                                 "(ldap_entries.dn)=",
722                                         &bsi->bi->upper_func_open,
723                                         '?', 
724                                         &bsi->bi->upper_func_close );
725                 } else {
726                         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
727                                         "l",
728                                         (ber_len_t)sizeof( "ldap_entries.dn=?" ) - 1,
729                                                 "ldap_entries.dn=?" );
730                 }
731                 break;
732                 
733         case LDAP_SCOPE_ONELEVEL:
734                 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "l",
735                                 (ber_len_t)sizeof( "ldap_entries.parent=?" ) - 1,
736                                         "ldap_entries.parent=?" );
737                 break;
738
739         case LDAP_SCOPE_SUBTREE:
740                 if ( bsi->bi->upper_func.bv_val ) {
741                         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, 
742                                         "blbcb",
743                                         &bsi->bi->upper_func,
744                                         (ber_len_t)sizeof( "(ldap_entries.dn) LIKE " ) - 1,
745                                                 "(ldap_entries.dn) LIKE ",
746                                         &bsi->bi->upper_func_open,
747                                         '?', 
748                                         &bsi->bi->upper_func_close );
749                 } else {
750                         backsql_strfcat( &bsi->join_where, &bsi->jwhere_len,
751                                         "l",
752                                         (ber_len_t)sizeof( "ldap_entries.dn LIKE ?" ) - 1,
753                                                 "ldap_entries.dn LIKE ?" );
754                 }
755
756 #if 0
757                 backsql_strfcat( &bsi->join_where, &bsi->jwhere_len, "b",
758                                 &bsi->bi->subtree_cond );
759 #endif
760                 break;
761
762         default:
763                 assert( 0 );
764         }
765
766         rc = backsql_process_filter( bsi, bsi->filter );
767         if ( rc > 0 ) {
768                 backsql_strfcat( query, &q_len, "bbblb",
769                                 &bsi->sel,
770                                 &bsi->from, 
771                                 &bsi->join_where,
772                                 (ber_len_t)sizeof( " AND " ) - 1, " AND ",
773                                 &bsi->flt_where );
774
775         } else if ( rc < 0 ) {
776                 /* 
777                  * Indicates that there's no possible way the filter matches
778                  * anything.  No need to issue the query
779                  */
780                 Debug( LDAP_DEBUG_TRACE,
781                         "<==backsql_srch_query() returns NULL\n", 0, 0, 0 );
782                 free( query->bv_val );
783                 query->bv_val = NULL;
784         }
785  
786         free( bsi->sel.bv_val );
787         bsi->sel.bv_len = 0;
788         bsi->sel_len = 0;
789         free( bsi->from.bv_val );
790         bsi->from.bv_len = 0;
791         bsi->from_len = 0;
792         free( bsi->join_where.bv_val );
793         bsi->join_where.bv_len = 0;
794         bsi->jwhere_len = 0;
795         free( bsi->flt_where.bv_val );
796         bsi->flt_where.bv_len = 0;
797         bsi->fwhere_len = 0;
798         
799         Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query()\n", 0, 0, 0 );
800         
801         return ( query->bv_val == NULL ? 1 : 0 );
802 }
803
804 int
805 backsql_oc_get_candidates( backsql_oc_map_rec *oc, backsql_srch_info *bsi )
806 {
807         struct berval           query;
808         SQLHSTMT                sth;
809         RETCODE                 rc;
810         backsql_entryID         base_id, *c_id;
811         int                     res;
812         BACKSQL_ROW_NTS         row;
813         int                     i;
814         int                     j;
815  
816         Debug(  LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc='%s'\n",
817                         BACKSQL_OC_NAME( oc ), 0, 0 );
818
819         if ( bsi->n_candidates == -1 ) {
820                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
821                         "unchecked limit has been overcome\n", 0, 0, 0 );
822                 /* should never get here */
823                 assert( 0 );
824                 return BACKSQL_STOP;
825         }
826         
827         bsi->oc = oc;
828         if ( backsql_srch_query( bsi, &query ) ) {
829                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
830                         "could not construct query for objectclass\n",
831                         0, 0, 0 );
832                 return BACKSQL_CONTINUE;
833         }
834
835         Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n", 
836                         query.bv_val, 0, 0 );
837
838         rc = backsql_Prepare( bsi->dbh, &sth, query.bv_val, 0 );
839         free( query.bv_val );
840         if ( rc != SQL_SUCCESS ) {
841                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
842                         "error preparing query\n", 0, 0, 0 );
843                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
844                 return BACKSQL_CONTINUE;
845         }
846
847         if ( backsql_BindParamID( sth, 1, &bsi->oc->id ) != SQL_SUCCESS ) {
848                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
849                         "error binding objectclass id parameter\n", 0, 0, 0 );
850                 return BACKSQL_CONTINUE;
851         }
852
853         switch ( bsi->scope ) {
854         case LDAP_SCOPE_BASE:
855                 rc = backsql_BindParamStr( sth, 2, bsi->base_dn->bv_val,
856                                 BACKSQL_MAX_DN_LEN );
857                 if ( rc != SQL_SUCCESS ) {
858                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
859                                 "error binding base_dn parameter\n", 0, 0, 0 );
860                         backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, 
861                                         sth, rc );
862                         return BACKSQL_CONTINUE;
863                 }
864                 break;
865
866         case LDAP_SCOPE_SUBTREE: {
867
868                 /* 
869                  * + 1 because we need room for '%'; this makes a subtree
870                  * search for a DN BACKSQL_MAX_DN_LEN long legal 
871                  * if it returns that DN only
872                  */
873                 char            temp_base_dn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
874
875                 /*
876                  * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
877                  * however this should be handled earlier
878                  */
879                 assert( bsi->base_dn->bv_len <= BACKSQL_MAX_DN_LEN );
880                         
881                 /* 
882                  * Sets the parameters for the SQL built earlier
883                  * NOTE that all the databases could actually use 
884                  * the TimesTen version, which would be cleaner 
885                  * and would also eliminate the need for the
886                  * subtree_cond line in the configuration file.  
887                  * For now, I'm leaving it the way it is, 
888                  * so non-TimesTen databases use the original code.
889                  * But at some point this should get cleaned up.
890                  *
891                  * If "dn" is being used, do a suffix search.
892                  * If "dn_ru" is being used, do a prefix search.
893                  */
894                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bsi->bi ) ) {
895                         temp_base_dn[ 0 ] = '\0';
896                         for ( i = 0, j = bsi->base_dn->bv_len - 1;
897                                         j >= 0; i++, j--) {
898                                 temp_base_dn[ i ] = bsi->base_dn->bv_val[ j ];
899                         }
900                         temp_base_dn[ i ] = '%';
901                         temp_base_dn[ i + 1 ] = '\0';
902                         ldap_pvt_str2upper( temp_base_dn );
903
904                 } else {
905                         temp_base_dn[ 0 ] = '%';
906                         AC_MEMCPY( &temp_base_dn[ 1 ], bsi->base_dn->bv_val,
907                                 bsi->base_dn->bv_len + 1 );
908                         ldap_pvt_str2upper( &temp_base_dn[ 1 ] );
909                 }
910
911                 Debug( LDAP_DEBUG_TRACE, "dn '%s'\n", temp_base_dn, 0, 0 );
912
913                 rc = backsql_BindParamStr( sth, 2, temp_base_dn, 
914                                 BACKSQL_MAX_DN_LEN );
915                 if ( rc != SQL_SUCCESS ) {
916                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
917                                 "error binding base_dn parameter (2)\n",
918                                 0, 0, 0 );
919                         backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, 
920                                         sth, rc );
921                         return BACKSQL_CONTINUE;
922                 }
923                 break;
924         }
925
926         case LDAP_SCOPE_ONELEVEL:
927                 res = backsql_dn2id( bsi->bi, &base_id, 
928                                 bsi->dbh, bsi->base_dn );
929                 if ( res != LDAP_SUCCESS ) {
930                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
931                                 "could not retrieve base_dn id%s\n",
932                                 res == LDAP_NO_SUCH_OBJECT ? ": no such entry"
933                                 : "", 0, 0 );
934                         bsi->status = res;
935                         return BACKSQL_CONTINUE;
936                 }
937                 
938                 rc = backsql_BindParamID( sth, 2, &base_id.id );
939                 backsql_free_entryID( &base_id, 0 );
940                 if ( rc != SQL_SUCCESS ) {
941                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
942                                 "error binding base id parameter\n", 0, 0, 0 );
943                         return BACKSQL_CONTINUE;
944                 }
945                 break;
946         }
947         
948         rc = SQLExecute( sth );
949         if ( !BACKSQL_SUCCESS( rc ) ) {
950                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
951                         "error executing query\n", 0, 0, 0 );
952                 backsql_PrintErrors( bsi->bi->db_env, bsi->dbh, sth, rc );
953                 SQLFreeStmt( sth, SQL_DROP );
954                 return BACKSQL_CONTINUE;
955         }
956
957         backsql_BindRowAsStrings( sth, &row );
958         rc = SQLFetch( sth );
959         for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
960                 c_id = (backsql_entryID *)ch_calloc( 1, 
961                                 sizeof( backsql_entryID ) );
962                 c_id->id = strtol( row.cols[ 0 ], NULL, 0 );
963                 c_id->keyval = strtol( row.cols[ 1 ], NULL, 0 );
964                 c_id->oc_id = bsi->oc->id;
965                 ber_str2bv( row.cols[ 3 ], 0, 1, &c_id->dn );
966                 c_id->next = bsi->id_list;
967                 bsi->id_list = c_id;
968                 bsi->n_candidates--;
969
970                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
971                         "added entry id=%ld, keyval=%ld dn='%s'\n",
972                         c_id->id, c_id->keyval, row.cols[ 3 ] );
973
974                 if ( bsi->n_candidates == -1 ) {
975                         break;
976                 }
977         }
978         backsql_FreeRow( &row );
979         SQLFreeStmt( sth, SQL_DROP );
980
981         Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates()\n", 0, 0, 0 );
982
983         return ( bsi->n_candidates == -1 ? BACKSQL_STOP : BACKSQL_CONTINUE );
984 }
985
986 int
987 backsql_search(
988         BackendDB       *be,
989         Connection      *conn,
990         Operation       *op,
991         struct berval   *base,
992         struct berval   *nbase,
993         int             scope,
994         int             deref,
995         int             slimit,
996         int             tlimit,
997         Filter          *filter,
998         struct berval   *filterstr,
999         AttributeName   *attrs,
1000         int             attrsonly )
1001 {
1002         backsql_info            *bi = (backsql_info *)be->be_private;
1003         SQLHDBC                 dbh;
1004         int                     sres;
1005         int                     nentries;
1006         Entry                   *entry, *res;
1007         int                     manageDSAit = get_manageDSAit( op );
1008         BerVarray               v2refs = NULL;
1009         time_t                  stoptime = 0;
1010         backsql_srch_info       srch_info;
1011         backsql_entryID         *eid = NULL;
1012         struct slap_limits_set  *limit = NULL;
1013         int                     isroot = 0;
1014
1015         Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
1016                 "base='%s', filter='%s', scope=%d,", 
1017                 nbase->bv_val, filterstr->bv_val, scope );
1018         Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
1019                 "attributes to load: %s\n",
1020                 deref, attrsonly, attrs == NULL ? "all" : "custom list" );
1021
1022         if ( nbase->bv_len > BACKSQL_MAX_DN_LEN ) {
1023                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1024                         "search base length (%ld) exceeds max length (%ld)\n", 
1025                         nbase->bv_len, BACKSQL_MAX_DN_LEN, 0 );
1026                 /*
1027                  * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
1028                  * since it is impossible that such a long DN exists
1029                  * in the backend
1030                  */
1031                 send_ldap_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED, 
1032                                 "", NULL, NULL, NULL );
1033                 return 1;
1034         }
1035
1036         sres = backsql_get_db_conn( be, conn, &dbh );
1037         if ( sres != LDAP_SUCCESS ) {
1038                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1039                         "could not get connection handle - exiting\n", 
1040                         0, 0, 0 );
1041                 send_ldap_result( conn, op, sres, "",
1042                                 sres == LDAP_OTHER ?  "SQL-backend error" : "",
1043                                 NULL, NULL );
1044                 return 1;
1045         }
1046
1047         /* TimesTen : Pass it along to the lower level routines */ 
1048         srch_info.use_reverse_dn = BACKSQL_USE_REVERSE_DN( bi ); 
1049  
1050         /* if not root, get appropriate limits */
1051         if ( be_isroot( be, &op->o_ndn ) ) {
1052                 isroot = 1;
1053         } else {
1054                 ( void ) get_limits( be, &op->o_ndn, &limit );
1055         }
1056
1057         /* The time/size limits come first because they require very little
1058          * effort, so there's no chance the candidates are selected and then 
1059          * the request is not honored only because of time/size constraints */
1060
1061         /* if no time limit requested, use soft limit (unless root!) */
1062         if ( isroot ) {
1063                 if ( tlimit == 0 ) {
1064                         tlimit = -1;    /* allow root to set no limit */
1065                 }
1066
1067                 if ( slimit == 0 ) {
1068                         slimit = -1;
1069                 }
1070
1071         } else {
1072                 /* if no limit is required, use soft limit */
1073                 if ( tlimit <= 0 ) {
1074                         tlimit = limit->lms_t_soft;
1075
1076                 /* if requested limit higher than hard limit, abort */
1077                 } else if ( tlimit > limit->lms_t_hard ) {
1078                         /* no hard limit means use soft instead */
1079                         if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
1080                                 tlimit = limit->lms_t_soft;
1081
1082                         /* positive hard limit means abort */
1083                         } else if ( limit->lms_t_hard > 0 ) {
1084                                 send_search_result( conn, op, 
1085                                                 LDAP_UNWILLING_TO_PERFORM,
1086                                                 NULL, NULL, NULL, NULL, 0 );
1087                                 return 0;
1088                         }
1089                 
1090                         /* negative hard limit means no limit */
1091                 }
1092                 
1093                 /* if no limit is required, use soft limit */
1094                 if ( slimit <= 0 ) {
1095                         slimit = limit->lms_s_soft;
1096
1097                 /* if requested limit higher than hard limit, abort */
1098                 } else if ( slimit > limit->lms_s_hard ) {
1099                         /* no hard limit means use soft instead */
1100                         if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
1101                                 slimit = limit->lms_s_soft;
1102
1103                         /* positive hard limit means abort */
1104                         } else if ( limit->lms_s_hard > 0 ) {
1105                                 send_search_result( conn, op, 
1106                                                 LDAP_UNWILLING_TO_PERFORM,
1107                                                 NULL, NULL, NULL, NULL, 0 );
1108                                 return 0;
1109                         }
1110                         
1111                         /* negative hard limit means no limit */
1112                 }
1113         }
1114
1115         /* compute it anyway; root does not use it */
1116         stoptime = op->o_time + tlimit;
1117
1118         backsql_init_search( &srch_info, bi, nbase, scope,
1119                         slimit, tlimit, stoptime, filter, dbh,
1120                         be, conn, op, attrs );
1121
1122         /*
1123          * for each objectclass we try to construct query which gets IDs
1124          * of entries matching LDAP query filter and scope (or at least 
1125          * candidates), and get the IDs
1126          */
1127         srch_info.n_candidates = ( isroot ? -2 : limit->lms_s_unchecked == -1 
1128                         ? -2 : limit->lms_s_unchecked );
1129         avl_apply( bi->oc_by_oc, (AVL_APPLY)backsql_oc_get_candidates,
1130                         &srch_info, BACKSQL_STOP, AVL_INORDER );
1131         if ( !isroot && limit->lms_s_unchecked != -1 ) {
1132                 if ( srch_info.n_candidates == -1 ) {
1133                         send_search_result( conn, op,
1134                                         LDAP_ADMINLIMIT_EXCEEDED,
1135                                         NULL, NULL, NULL, NULL, 0 );
1136                         goto done;
1137                 }
1138         }
1139         
1140         nentries = 0;
1141         /*
1142          * now we load candidate entries (only those attributes 
1143          * mentioned in attrs and filter), test it against full filter 
1144          * and then send to client
1145          */
1146         for ( eid = srch_info.id_list; eid != NULL; 
1147                         eid = backsql_free_entryID( eid, 1 ) ) {
1148 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1149                 Attribute       *hasSubordinate = NULL,
1150                                 *a = NULL;
1151 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1152
1153                 /* check for abandon */
1154                 if ( op->o_abandon ) {
1155                         break;
1156                 }
1157
1158                 /* check time limit */
1159                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
1160                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
1161                                 NULL, NULL, v2refs, NULL, nentries );
1162                         goto end_of_search;
1163                 }
1164
1165                 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1166                         "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
1167                         eid->id, eid->oc_id, eid->keyval );
1168
1169                 entry = (Entry *)ch_calloc( sizeof( Entry ), 1 );
1170                 res = backsql_id2entry( &srch_info, entry, eid );
1171                 if ( res == NULL ) {
1172                         Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1173                                 "error in backsql_id2entry() "
1174                                 "- skipping entry\n", 0, 0, 0 );
1175                         continue;
1176                 }
1177
1178                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
1179                         is_entry_referral( entry ) ) {
1180                         BerVarray refs = get_entry_referrals( be, conn,
1181                                         op, entry );
1182
1183                         send_search_reference( be, conn, op, entry, refs, 
1184                                         NULL, &v2refs );
1185                         ber_bvarray_free( refs );
1186                         continue;
1187                 }
1188
1189 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1190                 /*
1191                  * We use this flag since we need to parse the filter
1192                  * anyway; we should have used the frontend API function
1193                  * filter_has_subordinates()
1194                  */
1195                 if ( srch_info.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
1196                         int             rc;
1197
1198                         rc = backsql_has_children( bi, dbh, &entry->e_nname );
1199
1200                         switch( rc ) {
1201                         case LDAP_COMPARE_TRUE:
1202                         case LDAP_COMPARE_FALSE:
1203                                 hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
1204                                 if ( hasSubordinate != NULL ) {
1205                                         for ( a = entry->e_attrs; 
1206                                                         a && a->a_next; 
1207                                                         a = a->a_next );
1208
1209                                         a->a_next = hasSubordinate;
1210                                 }
1211                                 rc = 0;
1212                                 break;
1213
1214                         default:
1215                                 Debug(LDAP_DEBUG_TRACE, 
1216                                         "backsql_search(): "
1217                                         "has_children failed( %d)\n", 
1218                                         rc, 0, 0 );
1219                                 rc = 1;
1220                                 break;
1221                         }
1222
1223                         if ( rc ) {
1224                                 continue;
1225                         }
1226                 }
1227 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1228
1229                 if ( test_filter( be, conn, op, entry, filter ) 
1230                                 == LDAP_COMPARE_TRUE ) {
1231 #ifdef SLAP_X_FILTER_HASSUBORDINATES
1232                         if ( hasSubordinate && !( srch_info.bsi_flags & BSQL_SF_ALL_OPER ) 
1233                                         && !ad_inlist( slap_schema.si_ad_hasSubordinates, attrs ) ) {
1234                                 a->a_next = NULL;
1235                                 attr_free( hasSubordinate );
1236                                 hasSubordinate = NULL;
1237                         }
1238 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
1239
1240 #if 0   /* noop is masked SLAP_CTRL_UPDATE */
1241                         if ( op->o_noop ) {
1242                                 sres = 0;
1243                         } else {
1244 #endif
1245                                 sres = send_search_entry( be, conn, op, entry,
1246                                                 attrs, attrsonly, NULL );
1247 #if 0
1248                         }
1249 #endif
1250
1251                         switch ( sres ) {
1252                         case 0:
1253                                 nentries++;
1254                                 break;
1255
1256                         case -1:
1257                                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1258                                         "connection lost\n", 0, 0, 0 );
1259                                 goto end_of_search;
1260
1261                         default:
1262                                 /*
1263                                  * FIXME: send_search_entry failed;
1264                                  * better stop
1265                                  */
1266                                 break;
1267                         }
1268                 }
1269                 entry_free( entry );
1270
1271                 if ( slimit != -1 && nentries >= slimit ) {
1272                         send_search_result( conn, op, LDAP_SIZELIMIT_EXCEEDED,
1273                                 NULL, NULL, v2refs, NULL, nentries );
1274                         goto end_of_search;
1275                 }
1276         }
1277
1278 end_of_search:;
1279
1280         if ( nentries > 0 ) {
1281                 send_search_result( conn, op,
1282                         v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
1283                         NULL, NULL, v2refs, NULL, nentries );
1284         } else {
1285                 send_ldap_result( conn, op, srch_info.status,
1286                                 NULL, NULL, NULL, 0 );
1287         }
1288         
1289 done:;
1290         ch_free( srch_info.attrs );
1291
1292         Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
1293         return 0;
1294 }
1295
1296 #endif /* SLAPD_SQL */
1297