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