]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/search.c
f87da1db5cc9c45da698ecef9a2f76dbb69c74d5
[openldap] / servers / slapd / back-sql / search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #ifdef SLAPD_SQL
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28 #include "ac/ctype.h"
29
30 #include "slap.h"
31 #include "proto-sql.h"
32
33 static int backsql_process_filter( backsql_srch_info *bsi, Filter *f );
34 static int backsql_process_filter_eq( backsql_srch_info *bsi, 
35                 backsql_at_map_rec *at,
36                 int casefold, struct berval *filter_value );
37 static int backsql_process_filter_like( backsql_srch_info *bsi, 
38                 backsql_at_map_rec *at,
39                 int casefold, struct berval *filter_value );
40 static int backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, 
41                 backsql_at_map_rec *at );
42
43 static int
44 backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad )
45 {
46         int             n_attrs = 0;
47         AttributeName   *an = NULL;
48
49         if ( bsi->bsi_attrs == NULL ) {
50                 return 1;
51         }
52
53         /*
54          * clear the list (retrieve all attrs)
55          */
56         if ( ad == NULL ) {
57                 ch_free( bsi->bsi_attrs );
58                 bsi->bsi_attrs = NULL;
59                 return 1;
60         }
61
62         for ( ; !BER_BVISNULL( &bsi->bsi_attrs[ n_attrs ].an_name ); n_attrs++ ) {
63                 an = &bsi->bsi_attrs[ n_attrs ];
64                 
65                 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
66                         "attribute \"%s\" is in list\n", 
67                         an->an_name.bv_val, 0, 0 );
68                 /*
69                  * We can live with strcmp because the attribute 
70                  * list has been normalized before calling be_search
71                  */
72                 if ( !BACKSQL_NCMP( &an->an_name, &ad->ad_cname ) ) {
73                         return 1;
74                 }
75         }
76         
77         Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
78                 "adding \"%s\" to list\n", ad->ad_cname.bv_val, 0, 0 );
79
80         an = (AttributeName *)ch_realloc( bsi->bsi_attrs,
81                         sizeof( AttributeName ) * ( n_attrs + 2 ) );
82         if ( an == NULL ) {
83                 return -1;
84         }
85
86         an[ n_attrs ].an_name = ad->ad_cname;
87         an[ n_attrs ].an_desc = ad;
88         BER_BVZERO( &an[ n_attrs + 1 ].an_name );
89
90         bsi->bsi_attrs = an;
91         
92         return 1;
93 }
94
95 /*
96  * Initializes the search structure.
97  * 
98  * If get_base_id != 0, the field bsi_base_id is filled 
99  * with the entryID of bsi_base_dn; it must be freed
100  * by backsql_free_entryID() when no longer required.
101  */
102 int
103 backsql_init_search(
104         backsql_srch_info       *bsi, 
105         struct berval           *base, 
106         int                     scope, 
107         int                     slimit,
108         int                     tlimit,
109         time_t                  stoptime, 
110         Filter                  *filter, 
111         SQLHDBC                 dbh,
112         Operation               *op,
113         SlapReply               *rs,
114         AttributeName           *attrs,
115         int                     get_base_id )
116 {
117         AttributeName           *p;
118         int                     rc = LDAP_SUCCESS;
119
120         bsi->bsi_base_dn = base;
121         BER_BVZERO( &bsi->bsi_base_id.eid_dn );
122         bsi->bsi_scope = scope;
123         bsi->bsi_slimit = slimit;
124         bsi->bsi_tlimit = tlimit;
125         bsi->bsi_filter = filter;
126         bsi->bsi_dbh = dbh;
127         bsi->bsi_op = op;
128         bsi->bsi_rs = rs;
129         bsi->bsi_flags = 0;
130
131         /*
132          * handle "*"
133          */
134         if ( attrs == NULL || an_find( attrs, &AllUser ) ) {
135                 bsi->bsi_attrs = NULL;
136
137         } else {
138                 int     got_oc = 0;
139
140                 bsi->bsi_attrs = (AttributeName *)ch_calloc( 1, 
141                                 sizeof( AttributeName ) );
142                 BER_BVZERO( &bsi->bsi_attrs[ 0 ].an_name );
143                 
144                 for ( p = attrs; !BER_BVISNULL( &p->an_name ); p++ ) {
145                         /*
146                          * ignore "1.1"; handle "+"
147                          */
148                         if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
149                                 bsi->bsi_flags |= BSQL_SF_ALL_OPER;
150                                 continue;
151
152                         } else if ( BACKSQL_NCMP( &p->an_name, &NoAttrs ) == 0 ) {
153                                 continue;
154
155                         } else if ( p->an_desc == slap_schema.si_ad_objectClass ) {
156                                 got_oc = 1;
157                         }
158
159                         backsql_attrlist_add( bsi, p->an_desc );
160                 }
161
162                 if ( got_oc == 0 ) {
163                         /* add objectClass if not present,
164                          * because it is required to understand
165                          * if an entry is a referral, an alias 
166                          * or so... */
167                         backsql_attrlist_add( bsi, slap_schema.si_ad_objectClass );
168                 }
169         }
170
171         bsi->bsi_abandon = 0;
172         bsi->bsi_id_list = NULL;
173         bsi->bsi_id_listtail = &bsi->bsi_id_list;
174         bsi->bsi_n_candidates = 0;
175         bsi->bsi_stoptime = stoptime;
176         BER_BVZERO( &bsi->bsi_sel.bb_val );
177         bsi->bsi_sel.bb_len = 0;
178         BER_BVZERO( &bsi->bsi_from.bb_val );
179         bsi->bsi_from.bb_len = 0;
180         BER_BVZERO( &bsi->bsi_join_where.bb_val );
181         bsi->bsi_join_where.bb_len = 0;
182         BER_BVZERO( &bsi->bsi_flt_where.bb_val );
183         bsi->bsi_flt_where.bb_len = 0;
184         bsi->bsi_filter_oc = NULL;
185
186         if ( get_base_id ) {
187                 rc = backsql_dn2id( (backsql_info *)op->o_bd->be_private,
188                                 &bsi->bsi_base_id, dbh, base );
189         }
190
191         return ( bsi->bsi_status = rc );
192 }
193
194 static int
195 backsql_process_filter_list( backsql_srch_info *bsi, Filter *f, int op )
196 {
197         int             res;
198
199         if ( !f ) {
200                 return 0;
201         }
202
203         backsql_strfcat( &bsi->bsi_flt_where, "c", '(' /* ) */  );
204
205         while ( 1 ) {
206                 res = backsql_process_filter( bsi, f );
207                 if ( res < 0 ) {
208                         /*
209                          * TimesTen : If the query has no answers,
210                          * don't bother to run the query.
211                          */
212                         return -1;
213                 }
214  
215                 f = f->f_next;
216                 if ( f == NULL ) {
217                         break;
218                 }
219
220                 switch ( op ) {
221                 case LDAP_FILTER_AND:
222                         backsql_strfcat( &bsi->bsi_flt_where, "l",
223                                         (ber_len_t)STRLENOF( " AND " ), 
224                                                 " AND " );
225                         break;
226
227                 case LDAP_FILTER_OR:
228                         backsql_strfcat( &bsi->bsi_flt_where, "l",
229                                         (ber_len_t)STRLENOF( " OR " ),
230                                                 " OR " );
231                         break;
232                 }
233         }
234
235         backsql_strfcat( &bsi->bsi_flt_where, "c", /* ( */ ')' );
236
237         return 1;
238 }
239
240 static int
241 backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f,
242         backsql_at_map_rec *at )
243 {
244         backsql_info            *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
245         int                     i;
246         int                     casefold = 0;
247
248         if ( !f ) {
249                 return 0;
250         }
251
252         /* always uppercase strings by now */
253 #ifdef BACKSQL_UPPERCASE_FILTER
254         if ( SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
255                         bi->sql_caseIgnoreMatch ) )
256 #endif /* BACKSQL_UPPERCASE_FILTER */
257         {
258                 casefold = 1;
259         }
260
261         if ( SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
262                         bi->sql_telephoneNumberMatch ) )
263         {
264
265                 struct berval   bv;
266                 ber_len_t       i, s, a;
267
268                 /*
269                  * to check for matching telephone numbers
270                  * with intermixed chars, e.g. val='1234'
271                  * use
272                  * 
273                  * val LIKE '%1%2%3%4%'
274                  */
275
276                 BER_BVZERO( &bv );
277                 if ( f->f_sub_initial.bv_val ) {
278                         bv.bv_len += f->f_sub_initial.bv_len;
279                 }
280                 if ( f->f_sub_any != NULL ) {
281                         for ( a = 0; f->f_sub_any[ a ].bv_val != NULL; a++ ) {
282                                 bv.bv_len += f->f_sub_any[ a ].bv_len;
283                         }
284                 }
285                 if ( f->f_sub_final.bv_val ) {
286                         bv.bv_len += f->f_sub_final.bv_len;
287                 }
288                 bv.bv_len = 2 * bv.bv_len - 1;
289                 bv.bv_val = ch_malloc( bv.bv_len + 1 );
290
291                 s = 0;
292                 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
293                         bv.bv_val[ s ] = f->f_sub_initial.bv_val[ 0 ];
294                         for ( i = 1; i < f->f_sub_initial.bv_len; i++ ) {
295                                 bv.bv_val[ s + 2 * i - 1 ] = '%';
296                                 bv.bv_val[ s + 2 * i ] = f->f_sub_initial.bv_val[ i ];
297                         }
298                         bv.bv_val[ s + 2 * i - 1 ] = '%';
299                         s += 2 * i;
300                 }
301
302                 if ( f->f_sub_any != NULL ) {
303                         for ( a = 0; !BER_BVISNULL( &f->f_sub_any[ a ] ); a++ ) {
304                                 bv.bv_val[ s ] = f->f_sub_any[ a ].bv_val[ 0 ];
305                                 for ( i = 1; i < f->f_sub_any[ a ].bv_len; i++ ) {
306                                         bv.bv_val[ s + 2 * i - 1 ] = '%';
307                                         bv.bv_val[ s + 2 * i ] = f->f_sub_any[ a ].bv_val[ i ];
308                                 }
309                                 bv.bv_val[ s + 2 * i - 1 ] = '%';
310                                 s += 2 * i;
311                         }
312                 }
313
314                 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
315                         bv.bv_val[ s ] = f->f_sub_final.bv_val[ 0 ];
316                         for ( i = 1; i < f->f_sub_final.bv_len; i++ ) {
317                                 bv.bv_val[ s + 2 * i - 1 ] = '%';
318                                 bv.bv_val[ s + 2 * i ] = f->f_sub_final.bv_val[ i ];
319                         }
320                                 bv.bv_val[ s + 2 * i - 1 ] = '%';
321                         s += 2 * i;
322                 }
323
324                 bv.bv_val[ s - 1 ] = '\0';
325
326                 (void)backsql_process_filter_like( bsi, at, casefold, &bv );
327                 ch_free( bv.bv_val );
328
329                 return 1;
330         }
331
332         /*
333          * When dealing with case-sensitive strings 
334          * we may omit normalization; however, normalized
335          * SQL filters are more liberal.
336          */
337
338         backsql_strfcat( &bsi->bsi_flt_where, "c", '(' /* ) */  );
339
340         /* TimesTen */
341         Debug( LDAP_DEBUG_TRACE, "backsql_process_sub_filter(%s):\n",
342                 at->bam_ad->ad_cname.bv_val, 0, 0 );
343         Debug(LDAP_DEBUG_TRACE, "   expr: '%s%s%s'\n", at->bam_sel_expr.bv_val,
344                 at->bam_sel_expr_u.bv_val ? "' '" : "",
345                 at->bam_sel_expr_u.bv_val ? at->bam_sel_expr_u.bv_val : "" );
346         if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
347                 /*
348                  * If a pre-upper-cased version of the column 
349                  * or a precompiled upper function exists, use it
350                  */
351                 backsql_strfcat( &bsi->bsi_flt_where, 
352                                 "bl",
353                                 &at->bam_sel_expr_u,
354                                 (ber_len_t)STRLENOF( " LIKE '" ),
355                                         " LIKE '" );
356
357         } else {
358                 backsql_strfcat( &bsi->bsi_flt_where, "bl",
359                                 &at->bam_sel_expr,
360                                 (ber_len_t)STRLENOF( " LIKE '" ), " LIKE '" );
361         }
362  
363         if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
364                 ber_len_t       start;
365
366 #ifdef BACKSQL_TRACE
367                 Debug( LDAP_DEBUG_TRACE, 
368                         "==>backsql_process_sub_filter(%s): "
369                         "sub_initial=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
370                         f->f_sub_initial.bv_val, 0 );
371 #endif /* BACKSQL_TRACE */
372
373                 start = bsi->bsi_flt_where.bb_val.bv_len;
374                 backsql_strfcat( &bsi->bsi_flt_where, "b",
375                                 &f->f_sub_initial );
376                 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
377                         ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
378                 }
379         }
380
381         backsql_strfcat( &bsi->bsi_flt_where, "c", '%' );
382
383         if ( f->f_sub_any != NULL ) {
384                 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[ i ] ); i++ ) {
385                         ber_len_t       start;
386
387 #ifdef BACKSQL_TRACE
388                         Debug( LDAP_DEBUG_TRACE, 
389                                 "==>backsql_process_sub_filter(%s): "
390                                 "sub_any[%d]=\"%s\"\n", at->bam_ad->ad_cname.bv_val, 
391                                 i, f->f_sub_any[ i ].bv_val );
392 #endif /* BACKSQL_TRACE */
393
394                         start = bsi->bsi_flt_where.bb_val.bv_len;
395                         backsql_strfcat( &bsi->bsi_flt_where,
396                                         "bc",
397                                         &f->f_sub_any[ i ],
398                                         '%' );
399                         if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
400                                 /*
401                                  * Note: toupper('%') = '%'
402                                  */
403                                 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
404                         }
405                 }
406         }
407
408         if ( !BER_BVISNULL( &f->f_sub_final ) ) {
409                 ber_len_t       start;
410
411 #ifdef BACKSQL_TRACE
412                 Debug( LDAP_DEBUG_TRACE, 
413                         "==>backsql_process_sub_filter(%s): "
414                         "sub_final=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
415                         f->f_sub_final.bv_val, 0 );
416 #endif /* BACKSQL_TRACE */
417
418                 start = bsi->bsi_flt_where.bb_val.bv_len;
419                 backsql_strfcat( &bsi->bsi_flt_where, "b",
420                                 &f->f_sub_final );
421                 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
422                         ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
423                 }
424         }
425
426         backsql_strfcat( &bsi->bsi_flt_where, "l", 
427                         (ber_len_t)STRLENOF( /* (' */ "')" ), /* (' */ "')" );
428  
429         return 1;
430 }
431
432 static int
433 backsql_merge_from_tbls( backsql_srch_info *bsi, struct berval *from_tbls )
434 {
435         if ( BER_BVISNULL( from_tbls ) ) {
436                 return LDAP_SUCCESS;
437         }
438
439         if ( !BER_BVISNULL( &bsi->bsi_from.bb_val ) ) {
440                 char    *start, *end, *tmp;
441
442                 tmp = ch_strdup( from_tbls->bv_val );
443
444                 for ( start = tmp, end = strchr( start, ',' ); start; ) {
445                         if ( end ) {
446                                 end[0] = '\0';
447                         }
448
449                         if ( strstr( bsi->bsi_from.bb_val.bv_val, start) == NULL )
450                         {
451                                 backsql_strfcat( &bsi->bsi_from, "cs", ',', start );
452                         }
453
454                         if ( end ) {
455                                 /* in case there are spaces after the comma... */
456                                 for ( start = &end[1]; isspace( start[0] ); start++ );
457                                 if ( start[0] ) {
458                                         end = strchr( start, ',' );
459                                 } else {
460                                         start = NULL;
461                                 }
462                         } else {
463                                 start = NULL;
464                         }
465                 }
466
467                 ch_free( tmp );
468
469         } else {
470                 backsql_strfcat( &bsi->bsi_from, "b", from_tbls );
471         }
472
473         return LDAP_SUCCESS;
474 }
475
476 static int
477 backsql_process_filter( backsql_srch_info *bsi, Filter *f )
478 {
479         backsql_at_map_rec      **vat = NULL;
480         AttributeDescription    *ad = NULL;
481         unsigned                i;
482         int                     done = 0;
483         int                     rc = 0;
484
485         Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n", 0, 0, 0 );
486         if ( f->f_choice == SLAPD_FILTER_COMPUTED ) {
487                 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
488                         "invalid filter\n", 0, 0, 0 );
489                 rc = -1;
490                 goto done;
491         }
492
493         switch( f->f_choice ) {
494         case LDAP_FILTER_OR:
495                 rc = backsql_process_filter_list( bsi, f->f_or, 
496                                 LDAP_FILTER_OR );
497                 done = 1;
498                 break;
499                 
500         case LDAP_FILTER_AND:
501                 rc = backsql_process_filter_list( bsi, f->f_and,
502                                 LDAP_FILTER_AND );
503                 done = 1;
504                 break;
505
506         case LDAP_FILTER_NOT:
507                 backsql_strfcat( &bsi->bsi_flt_where, "l",
508                                 (ber_len_t)STRLENOF( "NOT (" /* ) */ ),
509                                         "NOT (" /* ) */ );
510                 rc = backsql_process_filter( bsi, f->f_not );
511                 backsql_strfcat( &bsi->bsi_flt_where, "c", /* ( */ ')' );
512                 done = 1;
513                 break;
514
515         case LDAP_FILTER_PRESENT:
516                 ad = f->f_desc;
517                 break;
518                 
519         case LDAP_FILTER_EXT:
520                 ad = f->f_mra->ma_desc;
521                 if ( f->f_mr_dnattrs ) {
522                         /*
523                          * if dn attrs filtering is requested, better return 
524                          * success and let test_filter() deal with candidate
525                          * selection; otherwise we'd need to set conditions
526                          * on the contents of the DN, e.g. "SELECT ... FROM
527                          * ldap_entries AS attributeName WHERE attributeName.dn
528                          * like '%attributeName=value%'"
529                          */
530                         backsql_strfcat( &bsi->bsi_flt_where, "l",
531                                         (ber_len_t)STRLENOF( "1=1" ), "1=1" );
532                         bsi->bsi_status = LDAP_SUCCESS;
533                         rc = 1;
534                         goto done;
535                 }
536                 break;
537                 
538         default:
539                 ad = f->f_av_desc;
540                 break;
541         }
542
543         if ( rc == -1 ) {
544                 goto done;
545         }
546  
547         if ( done ) {
548                 rc = 1;
549                 goto done;
550         }
551
552         /*
553          * Turn structuralObjectClass into objectClass
554          */
555         if ( ad == slap_schema.si_ad_objectClass 
556                         || ad == slap_schema.si_ad_structuralObjectClass ) {
557                 /*
558                  * If the filter is LDAP_FILTER_PRESENT, then it's done;
559                  * otherwise, let's see if we are lucky: filtering
560                  * for "structural" objectclass or ancestor...
561                  */
562                 switch ( f->f_choice ) {
563                 case LDAP_FILTER_EQUALITY:
564                 {
565                         ObjectClass     *oc = oc_bvfind( &f->f_av_value );
566
567                         if ( oc == NULL ) {
568                                 Debug( LDAP_DEBUG_TRACE,
569                                                 "backsql_process_filter(): "
570                                                 "unknown objectClass \"%s\" "
571                                                 "in filter\n",
572                                                 f->f_av_value.bv_val, 0, 0 );
573                                 bsi->bsi_status = LDAP_OTHER;
574                                 rc = -1;
575                                 goto done;
576                         }
577
578                         /*
579                          * "structural" objectClass inheritance:
580                          * - a search for "person" will also return 
581                          *   "inetOrgPerson"
582                          * - a search for "top" will return everything
583                          */
584                         if ( is_object_subclass( oc, bsi->bsi_oc->bom_oc ) ) {
585                                 static struct berval ldap_entry_objclasses = BER_BVC( "ldap_entry_objclasses" );
586
587                                 backsql_merge_from_tbls( bsi, &ldap_entry_objclasses );
588
589                                 backsql_strfcat( &bsi->bsi_flt_where, "lbl",
590                                                 (ber_len_t)STRLENOF( "1=1 OR (ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */ ),
591                                                         "1=1 OR (ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */,
592                                                 &bsi->bsi_oc->bom_oc->soc_cname,
593                                                 (ber_len_t)STRLENOF( /* (' */ "')" ),
594                                                         /* (' */ "')" );
595                                 bsi->bsi_status = LDAP_SUCCESS;
596                                 rc = 1;
597                                 goto done;
598                         }
599
600                         break;
601                 }
602
603                 case LDAP_FILTER_PRESENT:
604                         backsql_strfcat( &bsi->bsi_flt_where, "l",
605                                         (ber_len_t)STRLENOF( "1=1" ), "1=1" );
606                         bsi->bsi_status = LDAP_SUCCESS;
607                         rc = 1;
608                         goto done;
609
610                         /* FIXME: LDAP_FILTER_EXT? */
611                         
612                 default:
613                         Debug( LDAP_DEBUG_TRACE,
614                                         "backsql_process_filter(): "
615                                         "illegal/unhandled filter "
616                                         "on objectClass attribute",
617                                         0, 0, 0 );
618                         bsi->bsi_status = LDAP_OTHER;
619                         rc = -1;
620                         goto done;
621                 }
622
623         } else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) {
624                 /*
625                  * FIXME: this is not robust; e.g. a filter
626                  * '(!(hasSubordinates=TRUE))' fails because
627                  * in SQL it would read 'NOT (1=1)' instead 
628                  * of no condition.  
629                  * Note however that hasSubordinates is boolean, 
630                  * so a more appropriate filter would be 
631                  * '(hasSubordinates=FALSE)'
632                  *
633                  * A more robust search for hasSubordinates
634                  * would * require joining the ldap_entries table
635                  * selecting if there are descendants of the
636                  * candidate.
637                  */
638                 backsql_strfcat( &bsi->bsi_flt_where, "l",
639                                 (ber_len_t)STRLENOF( "1=1" ), "1=1" );
640                 if ( ad == slap_schema.si_ad_hasSubordinates ) {
641                         /*
642                          * instruct candidate selection algorithm
643                          * and attribute list to try to detect
644                          * if an entry has subordinates
645                          */
646                         bsi->bsi_flags |= BSQL_SF_FILTER_HASSUBORDINATE;
647
648                 } else {
649                         /*
650                          * clear attributes to fetch, to require ALL
651                          * and try extended match on all attributes
652                          */
653                         backsql_attrlist_add( bsi, NULL );
654                 }
655                 rc = 1;
656                 goto done;
657         }
658
659         /*
660          * attribute inheritance:
661          */
662         if ( backsql_supad2at( bsi->bsi_oc, ad, &vat ) ) {
663                 bsi->bsi_status = LDAP_OTHER;
664                 rc = -1;
665                 goto done;
666         }
667
668         if ( vat == NULL ) {
669                 /* search anyway; other parts of the filter
670                  * may succeeed */
671                 backsql_strfcat( &bsi->bsi_flt_where, "l",
672                                 (ber_len_t)STRLENOF( "1=1" ), "1=1" );
673                 bsi->bsi_status = LDAP_SUCCESS;
674                 rc = 1;
675                 goto done;
676         }
677
678         /* if required, open extra level of parens */
679         done = 0;
680         if ( vat[0]->bam_next || vat[1] ) {
681                 backsql_strfcat( &bsi->bsi_flt_where, "c", '(' );
682                 done = 1;
683         }
684
685         i = 0;
686 next:;
687         /* apply attr */
688         if ( backsql_process_filter_attr( bsi, f, vat[i] ) == -1 ) {
689                 return -1;
690         }
691
692         /* if more definitions of the same attr, apply */
693         if ( vat[i]->bam_next ) {
694                 backsql_strfcat( &bsi->bsi_flt_where, "l",
695                         STRLENOF( " OR " ), " OR " );
696                 vat[i] = vat[i]->bam_next;
697                 goto next;
698         }
699
700         /* if more descendants of the same attr, apply */
701         i++;
702         if ( vat[i] ) {
703                 backsql_strfcat( &bsi->bsi_flt_where, "l",
704                         STRLENOF( " OR " ), " OR " );
705                 goto next;
706         }
707
708         /* if needed, close extra level of parens */
709         if ( done ) {
710                 backsql_strfcat( &bsi->bsi_flt_where, "c", ')' );
711         }
712
713         rc = 1;
714
715 done:;
716         if ( vat ) {
717                 ch_free( vat );
718         }
719
720         Debug( LDAP_DEBUG_TRACE,
721                         "<==backsql_process_filter() %s\n",
722                         rc == 1 ? "succeeded" : "failed", 0, 0);
723
724         return rc;
725 }
726
727 static int
728 backsql_process_filter_eq( backsql_srch_info *bsi, backsql_at_map_rec *at,
729                 int casefold, struct berval *filter_value )
730 {
731         /*
732          * maybe we should check type of at->sel_expr here somehow,
733          * to know whether upper_func is applicable, but for now
734          * upper_func stuff is made for Oracle, where UPPER is
735          * safely applicable to NUMBER etc.
736          */
737         if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
738                 ber_len_t       start;
739
740                 backsql_strfcat( &bsi->bsi_flt_where, "cbl",
741                                 '(', /* ) */
742                                 &at->bam_sel_expr_u, 
743                                 (ber_len_t)STRLENOF( "='" ),
744                                         "='" );
745
746                 start = bsi->bsi_flt_where.bb_val.bv_len;
747
748                 backsql_strfcat( &bsi->bsi_flt_where, "bl",
749                                 filter_value, 
750                                 (ber_len_t)STRLENOF( /* (' */ "')" ),
751                                         /* (' */ "')" );
752
753                 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
754
755         } else {
756                 backsql_strfcat( &bsi->bsi_flt_where, "cblbl",
757                                 '(', /* ) */
758                                 &at->bam_sel_expr,
759                                 (ber_len_t)STRLENOF( "='" ), "='",
760                                 filter_value,
761                                 (ber_len_t)STRLENOF( /* (' */ "')" ),
762                                         /* (' */ "')" );
763         }
764
765         return 1;
766 }
767         
768 static int
769 backsql_process_filter_like( backsql_srch_info *bsi, backsql_at_map_rec *at,
770                 int casefold, struct berval *filter_value )
771 {
772         /*
773          * maybe we should check type of at->sel_expr here somehow,
774          * to know whether upper_func is applicable, but for now
775          * upper_func stuff is made for Oracle, where UPPER is
776          * safely applicable to NUMBER etc.
777          */
778         if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
779                 ber_len_t       start;
780
781                 backsql_strfcat( &bsi->bsi_flt_where, "cbl",
782                                 '(', /* ) */
783                                 &at->bam_sel_expr_u, 
784                                 (ber_len_t)STRLENOF( " LIKE '%" ),
785                                         " LIKE '%" );
786
787                 start = bsi->bsi_flt_where.bb_val.bv_len;
788
789                 backsql_strfcat( &bsi->bsi_flt_where, "bl",
790                                 filter_value, 
791                                 (ber_len_t)STRLENOF( /* (' */ "%')" ),
792                                         /* (' */ "%')" );
793
794                 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
795
796         } else {
797                 backsql_strfcat( &bsi->bsi_flt_where, "cblbl",
798                                 '(', /* ) */
799                                 &at->bam_sel_expr,
800                                 (ber_len_t)STRLENOF( " LIKE '%" ),
801                                         " LIKE '%",
802                                 filter_value,
803                                 (ber_len_t)STRLENOF( /* (' */ "%')" ),
804                                         /* (' */ "%')" );
805         }
806
807         return 1;
808 }
809
810 static int
811 backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, backsql_at_map_rec *at )
812 {
813         backsql_info            *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
814         int                     casefold = 0;
815         struct berval           *filter_value = NULL;
816         MatchingRule            *matching_rule = NULL;
817         struct berval           ordering = BER_BVC("<=");
818
819         Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter_attr(%s)\n",
820                 at->bam_ad->ad_cname.bv_val, 0, 0 );
821
822         /*
823          * need to add this attribute to list of attrs to load,
824          * so that we can do test_filter() later
825          */
826         backsql_attrlist_add( bsi, at->bam_ad );
827
828         backsql_merge_from_tbls( bsi, &at->bam_from_tbls );
829
830         if ( !BER_BVISNULL( &at->bam_join_where )
831                         && strstr( bsi->bsi_join_where.bb_val.bv_val,
832                                 at->bam_join_where.bv_val ) == NULL )
833         {
834                 backsql_strfcat( &bsi->bsi_join_where, "lb",
835                                 (ber_len_t)STRLENOF( " AND " ), " AND ",
836                                 &at->bam_join_where );
837         }
838
839         switch ( f->f_choice ) {
840         case LDAP_FILTER_EQUALITY:
841                 filter_value = &f->f_av_value;
842                 matching_rule = at->bam_ad->ad_type->sat_equality;
843
844                 goto equality_match;
845
846                 /* fail over into next case */
847                 
848         case LDAP_FILTER_EXT:
849                 filter_value = &f->f_mra->ma_value;
850                 matching_rule = f->f_mr_rule;
851
852 equality_match:;
853                 /* always uppercase strings by now */
854 #ifdef BACKSQL_UPPERCASE_FILTER
855                 if ( SLAP_MR_ASSOCIATED( matching_rule,
856                                         bi->sql_caseIgnoreMatch ) )
857 #endif /* BACKSQL_UPPERCASE_FILTER */
858                 {
859                         casefold = 1;
860                 }
861
862                 if ( SLAP_MR_ASSOCIATED( matching_rule,
863                                         bi->sql_telephoneNumberMatch ) )
864                 {
865                         struct berval   bv;
866                         ber_len_t       i;
867
868                         /*
869                          * to check for matching telephone numbers
870                          * with intermized chars, e.g. val='1234'
871                          * use
872                          * 
873                          * val LIKE '%1%2%3%4%'
874                          */
875
876                         bv.bv_len = 2 * filter_value->bv_len - 1;
877                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
878
879                         bv.bv_val[ 0 ] = filter_value->bv_val[ 0 ];
880                         for ( i = 1; i < filter_value->bv_len; i++ ) {
881                                 bv.bv_val[ 2 * i - 1 ] = '%';
882                                 bv.bv_val[ 2 * i ] = filter_value->bv_val[ i ];
883                         }
884                         bv.bv_val[ 2 * i - 1 ] = '\0';
885
886                         (void)backsql_process_filter_like( bsi, at, casefold, &bv );
887                         ch_free( bv.bv_val );
888
889                         break;
890                 }
891
892                 /* NOTE: this is required by objectClass inheritance 
893                  * and auxiliary objectClass use in filters for slightly
894                  * more efficient candidate selection. */
895                 /* FIXME: a bit too many specializations to deal with
896                  * very specific cases... */
897                 if ( at->bam_ad == slap_schema.si_ad_objectClass
898                                 || at->bam_ad == slap_schema.si_ad_structuralObjectClass )
899                 {
900                         backsql_strfcat( &bsi->bsi_flt_where, "lbl",
901                                         (ber_len_t)STRLENOF( "(ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */ ),
902                                                 "(ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */,
903                                         filter_value,
904                                         (ber_len_t)STRLENOF( /* (' */ "')" ),
905                                                 /* (' */ "')" );
906                         break;
907                 }
908
909                 /*
910                  * maybe we should check type of at->sel_expr here somehow,
911                  * to know whether upper_func is applicable, but for now
912                  * upper_func stuff is made for Oracle, where UPPER is
913                  * safely applicable to NUMBER etc.
914                  */
915                 (void)backsql_process_filter_eq( bsi, at, casefold, filter_value );
916                 break;
917
918         case LDAP_FILTER_GE:
919                 ordering.bv_val = ">=";
920
921                 /* fall thru to next case */
922                 
923         case LDAP_FILTER_LE:
924                 /* always uppercase strings by now */
925 #ifdef BACKSQL_UPPERCASE_FILTER
926                 if ( SLAP_MR_ASSOCIATED( at->bam_ad->ad_type->sat_ordering,
927                                 bi->sql_caseIgnoreMatch ) )
928 #endif /* BACKSQL_UPPERCASE_FILTER */
929                 {
930                         casefold = 1;
931                 }
932
933                 /*
934                  * FIXME: should we uppercase the operands?
935                  */
936                 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
937                         ber_len_t       start;
938
939                         backsql_strfcat( &bsi->bsi_flt_where, "cbbc",
940                                         '(', /* ) */
941                                         &at->bam_sel_expr_u, 
942                                         &ordering,
943                                         '\'' );
944
945                         start = bsi->bsi_flt_where.bb_val.bv_len;
946
947                         backsql_strfcat( &bsi->bsi_flt_where, "bl",
948                                         filter_value, 
949                                         (ber_len_t)STRLENOF( /* (' */ "')" ),
950                                                 /* (' */ "')" );
951
952                         ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
953                 
954                 } else {
955                         backsql_strfcat( &bsi->bsi_flt_where, "cbbcbl",
956                                         '(' /* ) */ ,
957                                         &at->bam_sel_expr,
958                                         &ordering,
959                                         '\'',
960                                         &f->f_av_value,
961                                         (ber_len_t)STRLENOF( /* (' */ "')" ),
962                                                 /* ( */ "')" );
963                 }
964                 break;
965
966         case LDAP_FILTER_PRESENT:
967                 backsql_strfcat( &bsi->bsi_flt_where, "lbl",
968                                 (ber_len_t)STRLENOF( "NOT (" /* ) */),
969                                         "NOT (", /* ) */
970                                 &at->bam_sel_expr, 
971                                 (ber_len_t)STRLENOF( /* ( */ " IS NULL)" ),
972                                         /* ( */ " IS NULL)" );
973                 break;
974
975         case LDAP_FILTER_SUBSTRINGS:
976                 backsql_process_sub_filter( bsi, f, at );
977                 break;
978
979         case LDAP_FILTER_APPROX:
980                 /* we do our best */
981
982                 /*
983                  * maybe we should check type of at->sel_expr here somehow,
984                  * to know whether upper_func is applicable, but for now
985                  * upper_func stuff is made for Oracle, where UPPER is
986                  * safely applicable to NUMBER etc.
987                  */
988                 (void)backsql_process_filter_like( bsi, at, 1, &f->f_av_value );
989                 break;
990
991         default:
992                 /* unhandled filter type; should not happen */
993                 assert( 0 );
994                 backsql_strfcat( &bsi->bsi_flt_where, "l",
995                                 (ber_len_t)STRLENOF( "1=1" ), "1=1" );
996                 break;
997
998         }
999
1000         Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter_attr(%s)\n",
1001                 at->bam_ad->ad_cname.bv_val, 0, 0 );
1002
1003         return 1;
1004 }
1005
1006 static int
1007 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
1008 {
1009         backsql_info    *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1010         int             rc;
1011
1012         assert( query );
1013         BER_BVZERO( query );
1014
1015         Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
1016         BER_BVZERO( &bsi->bsi_sel.bb_val );
1017         BER_BVZERO( &bsi->bsi_sel.bb_val );
1018         bsi->bsi_sel.bb_len = 0;
1019         BER_BVZERO( &bsi->bsi_from.bb_val );
1020         bsi->bsi_from.bb_len = 0;
1021         BER_BVZERO( &bsi->bsi_join_where.bb_val );
1022         bsi->bsi_join_where.bb_len = 0;
1023         BER_BVZERO( &bsi->bsi_flt_where.bb_val );
1024         bsi->bsi_flt_where.bb_len = 0;
1025
1026         backsql_strfcat( &bsi->bsi_sel, "lbcbc",
1027                         (ber_len_t)STRLENOF( "SELECT DISTINCT ldap_entries.id," ),
1028                                 "SELECT DISTINCT ldap_entries.id,", 
1029                         &bsi->bsi_oc->bom_keytbl, 
1030                         '.', 
1031                         &bsi->bsi_oc->bom_keycol, 
1032                         ',' );
1033
1034         if ( !BER_BVISNULL( &bi->sql_strcast_func ) ) {
1035                 backsql_strfcat( &bsi->bsi_sel, "blbl",
1036                                 &bi->sql_strcast_func, 
1037                                 (ber_len_t)STRLENOF( "('" /* ') */ ),
1038                                         "('" /* ') */ ,
1039                                 &bsi->bsi_oc->bom_oc->soc_cname,
1040                                 (ber_len_t)STRLENOF( /* (' */ "')" ),
1041                                         /* (' */ "')" );
1042         } else {
1043                 backsql_strfcat( &bsi->bsi_sel, "cbc",
1044                                 '\'',
1045                                 &bsi->bsi_oc->bom_oc->soc_cname,
1046                                 '\'' );
1047         }
1048 #ifdef BACKSQL_ALIASING_QUOTE
1049         backsql_strfcat( &bsi->bsi_sel, "lclcl",
1050                         (ber_len_t)STRLENOF( " " BACKSQL_ALIASING ),
1051                                 " " BACKSQL_ALIASING,
1052                         BACKSQL_ALIASING_QUOTE,
1053                         (ber_len_t)STRLENOF( "objectClass" ),
1054                                 "objectClass",
1055                         BACKSQL_ALIASING_QUOTE,
1056                         (ber_len_t)STRLENOF( ",ldap_entries.dn " BACKSQL_ALIASING "dn" ),
1057                                 ",ldap_entries.dn " BACKSQL_ALIASING "dn" );
1058 #else /* ! BACKSQL_ALIASING_QUOTE */
1059         backsql_strfcat( &bsi->bsi_sel, "l",
1060                         (ber_len_t)STRLENOF( " " BACKSQL_ALIASING "objectClass,ldap_entries.dn " BACKSQL_ALIASING "dn" ),
1061                                 " " BACKSQL_ALIASING "objectClass,ldap_entries.dn " BACKSQL_ALIASING "dn" );
1062 #endif /* ! BACKSQL_ALIASING_QUOTE */
1063
1064         backsql_strfcat( &bsi->bsi_from, "lb",
1065                         (ber_len_t)STRLENOF( " FROM ldap_entries," ),
1066                                 " FROM ldap_entries,",
1067                         &bsi->bsi_oc->bom_keytbl );
1068
1069         backsql_strfcat( &bsi->bsi_join_where, "lbcbl",
1070                         (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
1071                         &bsi->bsi_oc->bom_keytbl,
1072                         '.',
1073                         &bsi->bsi_oc->bom_keycol,
1074                         (ber_len_t)STRLENOF( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ),
1075                                 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
1076
1077         switch ( bsi->bsi_scope ) {
1078         case LDAP_SCOPE_BASE:
1079                 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1080                         backsql_strfcat( &bsi->bsi_join_where, "bl",
1081                                         &bi->sql_upper_func,
1082                                         (ber_len_t)STRLENOF( "(ldap_entries.dn)=?" ),
1083                                                 "(ldap_entries.dn)=?" );
1084                 } else {
1085                         backsql_strfcat( &bsi->bsi_join_where, "l",
1086                                         (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
1087                                                 "ldap_entries.dn=?" );
1088                 }
1089                 break;
1090                 
1091         case BACKSQL_SCOPE_BASE_LIKE:
1092                 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1093                         backsql_strfcat( &bsi->bsi_join_where, "bl",
1094                                         &bi->sql_upper_func,
1095                                         (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ),
1096                                                 "(ldap_entries.dn) LIKE ?" );
1097                 } else {
1098                         backsql_strfcat( &bsi->bsi_join_where, "l",
1099                                         (ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ),
1100                                                 "ldap_entries.dn LIKE ?" );
1101                 }
1102                 break;
1103                 
1104         case LDAP_SCOPE_ONELEVEL:
1105                 backsql_strfcat( &bsi->bsi_join_where, "l",
1106                                 (ber_len_t)STRLENOF( "ldap_entries.parent=?" ),
1107                                         "ldap_entries.parent=?" );
1108                 break;
1109
1110         case LDAP_SCOPE_SUBTREE:
1111                 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1112                         backsql_strfcat( &bsi->bsi_join_where, "bl",
1113                                         &bi->sql_upper_func,
1114                                         (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ),
1115                                                 "(ldap_entries.dn) LIKE ?"  );
1116                 } else {
1117                         backsql_strfcat( &bsi->bsi_join_where, "l",
1118                                         (ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ),
1119                                                 "ldap_entries.dn LIKE ?" );
1120                 }
1121
1122                 break;
1123
1124         default:
1125                 assert( 0 );
1126         }
1127
1128         rc = backsql_process_filter( bsi, bsi->bsi_filter );
1129         if ( rc > 0 ) {
1130                 struct berbuf   bb = BB_NULL;
1131
1132                 backsql_strfcat( &bb, "bbblb",
1133                                 &bsi->bsi_sel.bb_val,
1134                                 &bsi->bsi_from.bb_val, 
1135                                 &bsi->bsi_join_where.bb_val,
1136                                 (ber_len_t)STRLENOF( " AND " ), " AND ",
1137                                 &bsi->bsi_flt_where.bb_val );
1138
1139                 *query = bb.bb_val;
1140
1141         } else if ( rc < 0 ) {
1142                 /* 
1143                  * Indicates that there's no possible way the filter matches
1144                  * anything.  No need to issue the query
1145                  */
1146                 free( query->bv_val );
1147                 BER_BVZERO( query );
1148         }
1149  
1150         free( bsi->bsi_sel.bb_val.bv_val );
1151         BER_BVZERO( &bsi->bsi_sel.bb_val );
1152         bsi->bsi_sel.bb_len = 0;
1153         free( bsi->bsi_from.bb_val.bv_val );
1154         BER_BVZERO( &bsi->bsi_from.bb_val );
1155         bsi->bsi_from.bb_len = 0;
1156         free( bsi->bsi_join_where.bb_val.bv_val );
1157         BER_BVZERO( &bsi->bsi_join_where.bb_val );
1158         bsi->bsi_join_where.bb_len = 0;
1159         free( bsi->bsi_flt_where.bb_val.bv_val );
1160         BER_BVZERO( &bsi->bsi_flt_where.bb_val );
1161         bsi->bsi_flt_where.bb_len = 0;
1162         
1163         Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query() returns %s\n",
1164                 query->bv_val ? query->bv_val : "NULL", 0, 0 );
1165         
1166         return ( rc <= 0 ? 1 : 0 );
1167 }
1168
1169 static int
1170 backsql_oc_get_candidates( void *v_oc, void *v_bsi )
1171 {
1172         backsql_oc_map_rec      *oc = v_oc;
1173         backsql_srch_info       *bsi = v_bsi;
1174         backsql_info            *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1175         struct berval           query;
1176         SQLHSTMT                sth;
1177         RETCODE                 rc;
1178         int                     res;
1179         BACKSQL_ROW_NTS         row;
1180         int                     i;
1181         int                     j;
1182         int                     n_candidates = bsi->bsi_n_candidates;
1183
1184         /* 
1185          * + 1 because we need room for '%'; this makes a subtree
1186          * search for a DN BACKSQL_MAX_DN_LEN long legal 
1187          * if it returns that DN only
1188          */
1189         char                    temp_base_dn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
1190
1191         bsi->bsi_status = LDAP_SUCCESS;
1192  
1193         Debug( LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc=\"%s\"\n",
1194                         BACKSQL_OC_NAME( oc ), 0, 0 );
1195
1196         if ( bsi->bsi_n_candidates == -1 ) {
1197                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1198                         "unchecked limit has been overcome\n", 0, 0, 0 );
1199                 /* should never get here */
1200                 assert( 0 );
1201                 bsi->bsi_status = LDAP_ADMINLIMIT_EXCEEDED;
1202                 return BACKSQL_AVL_STOP;
1203         }
1204         
1205         bsi->bsi_oc = oc;
1206         res = backsql_srch_query( bsi, &query );
1207         if ( res ) {
1208                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1209                         "error while constructing query for objectclass \"%s\"\n",
1210                         oc->bom_oc->soc_cname.bv_val, 0, 0 );
1211                 /*
1212                  * FIXME: need to separate errors from legally
1213                  * impossible filters
1214                  */
1215                 switch ( bsi->bsi_status ) {
1216                 case LDAP_SUCCESS:
1217                 case LDAP_UNDEFINED_TYPE:
1218                 case LDAP_NO_SUCH_OBJECT:
1219                         /* we are conservative... */
1220                 default:
1221                         bsi->bsi_status = LDAP_SUCCESS;
1222                         /* try next */
1223                         return BACKSQL_AVL_CONTINUE;
1224
1225                 case LDAP_ADMINLIMIT_EXCEEDED:
1226                 case LDAP_OTHER:
1227                         /* don't try any more */
1228                         return BACKSQL_AVL_STOP;
1229                 }
1230         }
1231
1232         if ( BER_BVISNULL( &query ) ) {
1233                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1234                         "could not construct query for objectclass \"%s\"\n",
1235                         oc->bom_oc->soc_cname.bv_val, 0, 0 );
1236                 bsi->bsi_status = LDAP_SUCCESS;
1237                 return BACKSQL_AVL_CONTINUE;
1238         }
1239
1240         Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n", 
1241                         query.bv_val, 0, 0 );
1242
1243         rc = backsql_Prepare( bsi->bsi_dbh, &sth, query.bv_val, 0 );
1244         free( query.bv_val );
1245         BER_BVZERO( &query );
1246         if ( rc != SQL_SUCCESS ) {
1247                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1248                         "error preparing query\n", 0, 0, 0 );
1249                 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
1250                 bsi->bsi_status = LDAP_OTHER;
1251                 return BACKSQL_AVL_CONTINUE;
1252         }
1253         
1254         Debug( LDAP_DEBUG_TRACE, "id: '%ld'\n", bsi->bsi_oc->bom_id, 0, 0 );
1255
1256         rc = backsql_BindParamInt( sth, 1, SQL_PARAM_INPUT,
1257                         &bsi->bsi_oc->bom_id );
1258         if ( rc != SQL_SUCCESS ) {
1259                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1260                         "error binding objectclass id parameter\n", 0, 0, 0 );
1261                 bsi->bsi_status = LDAP_OTHER;
1262                 return BACKSQL_AVL_CONTINUE;
1263         }
1264
1265         switch ( bsi->bsi_scope ) {
1266         case LDAP_SCOPE_BASE:
1267         case BACKSQL_SCOPE_BASE_LIKE:
1268                 /*
1269                  * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
1270                  * however this should be handled earlier
1271                  */
1272                 if ( bsi->bsi_base_dn->bv_len > BACKSQL_MAX_DN_LEN ) {
1273                         bsi->bsi_status = LDAP_OTHER;
1274                         return BACKSQL_AVL_CONTINUE;
1275                 }
1276
1277                 AC_MEMCPY( temp_base_dn, bsi->bsi_base_dn->bv_val,
1278                                 bsi->bsi_base_dn->bv_len + 1 );
1279
1280                 /* uppercase DN only if the stored DN can be uppercased
1281                  * for comparison */
1282                 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1283                         ldap_pvt_str2upper( temp_base_dn );
1284                 }
1285
1286                 Debug( LDAP_DEBUG_TRACE, "(base)dn: \"%s\"\n",
1287                                 temp_base_dn, 0, 0 );
1288
1289                 rc = backsql_BindParamStr( sth, 2, SQL_PARAM_INPUT,
1290                                 temp_base_dn, BACKSQL_MAX_DN_LEN );
1291                 if ( rc != SQL_SUCCESS ) {
1292                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1293                                 "error binding base_dn parameter\n", 0, 0, 0 );
1294                         backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, 
1295                                         sth, rc );
1296                         bsi->bsi_status = LDAP_OTHER;
1297                         return BACKSQL_AVL_CONTINUE;
1298                 }
1299                 break;
1300
1301         case LDAP_SCOPE_SUBTREE: {
1302                 /*
1303                  * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
1304                  * however this should be handled earlier
1305                  */
1306                 if ( bsi->bsi_base_dn->bv_len > BACKSQL_MAX_DN_LEN ) {
1307                         bsi->bsi_status = LDAP_OTHER;
1308                         return BACKSQL_AVL_CONTINUE;
1309                 }
1310
1311                 /* 
1312                  * Sets the parameters for the SQL built earlier
1313                  * NOTE that all the databases could actually use 
1314                  * the TimesTen version, which would be cleaner 
1315                  * and would also eliminate the need for the
1316                  * subtree_cond line in the configuration file.  
1317                  * For now, I'm leaving it the way it is, 
1318                  * so non-TimesTen databases use the original code.
1319                  * But at some point this should get cleaned up.
1320                  *
1321                  * If "dn" is being used, do a suffix search.
1322                  * If "dn_ru" is being used, do a prefix search.
1323                  */
1324                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
1325                         temp_base_dn[ 0 ] = '\0';
1326                         for ( i = 0, j = bsi->bsi_base_dn->bv_len - 1;
1327                                         j >= 0; i++, j--) {
1328                                 temp_base_dn[ i ] = bsi->bsi_base_dn->bv_val[ j ];
1329                         }
1330                         temp_base_dn[ i ] = '%';
1331                         temp_base_dn[ i + 1 ] = '\0';
1332
1333                 } else {
1334                         temp_base_dn[ 0 ] = '%';
1335                         AC_MEMCPY( &temp_base_dn[ 1 ], bsi->bsi_base_dn->bv_val,
1336                                 bsi->bsi_base_dn->bv_len + 1 );
1337                 }
1338
1339                 /* uppercase DN only if the stored DN can be uppercased
1340                  * for comparison */
1341                 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1342                         ldap_pvt_str2upper( temp_base_dn );
1343                 }
1344
1345                 Debug( LDAP_DEBUG_TRACE, "(sub)dn: \"%s\"\n", temp_base_dn,
1346                                 0, 0 );
1347
1348                 rc = backsql_BindParamStr( sth, 2, SQL_PARAM_INPUT,
1349                                 temp_base_dn, BACKSQL_MAX_DN_LEN );
1350                 if ( rc != SQL_SUCCESS ) {
1351                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1352                                 "error binding base_dn parameter (2)\n",
1353                                 0, 0, 0 );
1354                         backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, 
1355                                         sth, rc );
1356                         bsi->bsi_status = LDAP_OTHER;
1357                         return BACKSQL_AVL_CONTINUE;
1358                 }
1359                 break;
1360         }
1361
1362         case LDAP_SCOPE_ONELEVEL:
1363                 assert( !BER_BVISNULL( &bsi->bsi_base_id.eid_dn ) );
1364
1365 #ifdef BACKSQL_ARBITRARY_KEY
1366                 Debug( LDAP_DEBUG_TRACE, "(one)id: \"%s\"\n",
1367                                 bsi->bsi_base_id.eid_id.bv_val, 0, 0 );
1368 #else /* ! BACKSQL_ARBITRARY_KEY */
1369                 Debug( LDAP_DEBUG_TRACE, "(one)id: '%lu'\n",
1370                                 bsi->bsi_base_id.eid_id, 0, 0 );
1371 #endif /* ! BACKSQL_ARBITRARY_KEY */
1372                 rc = backsql_BindParamID( sth, 2, SQL_PARAM_INPUT,
1373                                 &bsi->bsi_base_id.eid_id );
1374                 if ( rc != SQL_SUCCESS ) {
1375                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1376                                 "error binding base id parameter\n", 0, 0, 0 );
1377                         bsi->bsi_status = LDAP_OTHER;
1378                         return BACKSQL_AVL_CONTINUE;
1379                 }
1380                 break;
1381         }
1382         
1383         rc = SQLExecute( sth );
1384         if ( !BACKSQL_SUCCESS( rc ) ) {
1385                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1386                         "error executing query\n", 0, 0, 0 );
1387                 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
1388                 SQLFreeStmt( sth, SQL_DROP );
1389                 bsi->bsi_status = LDAP_OTHER;
1390                 return BACKSQL_AVL_CONTINUE;
1391         }
1392
1393         backsql_BindRowAsStrings( sth, &row );
1394         rc = SQLFetch( sth );
1395         for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
1396                 struct berval           dn;
1397                 backsql_entryID         *c_id = NULL;
1398
1399                 ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
1400
1401                 if ( backsql_api_odbc2dn( bsi->bsi_op, bsi->bsi_rs, &dn ) ) {
1402                         continue;
1403                 }
1404
1405                 c_id = (backsql_entryID *)ch_calloc( 1, 
1406                                 sizeof( backsql_entryID ) );
1407 #ifdef BACKSQL_ARBITRARY_KEY
1408                 ber_str2bv( row.cols[ 0 ], 0, 1, &c_id->eid_id );
1409                 ber_str2bv( row.cols[ 1 ], 0, 1, &c_id->eid_keyval );
1410 #else /* ! BACKSQL_ARBITRARY_KEY */
1411                 c_id->eid_id = strtol( row.cols[ 0 ], NULL, 0 );
1412                 c_id->eid_keyval = strtol( row.cols[ 1 ], NULL, 0 );
1413 #endif /* ! BACKSQL_ARBITRARY_KEY */
1414                 c_id->eid_oc_id = bsi->bsi_oc->bom_id;
1415
1416                 if ( dn.bv_val == row.cols[ 3 ] ) {
1417                         ber_dupbv( &c_id->eid_dn, &dn );
1418                 } else {
1419                         c_id->eid_dn = dn;
1420                 }
1421
1422                 /* append at end of list ... */
1423                 c_id->eid_next = NULL;
1424                 *bsi->bsi_id_listtail = c_id;
1425                 bsi->bsi_id_listtail = &c_id->eid_next;
1426
1427 #ifdef BACKSQL_ARBITRARY_KEY
1428                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1429                         "added entry id=%s, keyval=%s dn=\"%s\"\n",
1430                         c_id->eid_id.bv_val, c_id->eid_keyval.bv_val,
1431                         row.cols[ 3 ] );
1432 #else /* ! BACKSQL_ARBITRARY_KEY */
1433                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1434                         "added entry id=%ld, keyval=%ld dn=\"%s\"\n",
1435                         c_id->eid_id, c_id->eid_keyval, row.cols[ 3 ] );
1436 #endif /* ! BACKSQL_ARBITRARY_KEY */
1437
1438                 /* count candidates, for unchecked limit */
1439                 bsi->bsi_n_candidates--;
1440                 if ( bsi->bsi_n_candidates == -1 ) {
1441                         break;
1442                 }
1443         }
1444         backsql_FreeRow( &row );
1445         SQLFreeStmt( sth, SQL_DROP );
1446
1447         Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates(): %d\n",
1448                         n_candidates - bsi->bsi_n_candidates, 0, 0 );
1449
1450         return ( bsi->bsi_n_candidates == -1 ? BACKSQL_AVL_STOP : BACKSQL_AVL_CONTINUE );
1451 }
1452
1453 int
1454 backsql_search( Operation *op, SlapReply *rs )
1455 {
1456         backsql_info            *bi = (backsql_info *)op->o_bd->be_private;
1457         SQLHDBC                 dbh;
1458         int                     sres;
1459         Entry                   user_entry = { 0 };
1460         int                     manageDSAit;
1461         time_t                  stoptime = 0;
1462         backsql_srch_info       srch_info;
1463         backsql_entryID         *eid = NULL;
1464         struct berval           base;
1465
1466         manageDSAit = get_manageDSAit( op );
1467
1468         Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
1469                 "base=\"%s\", filter=\"%s\", scope=%d,", 
1470                 op->o_req_ndn.bv_val,
1471                 op->ors_filterstr.bv_val,
1472                 op->ors_scope );
1473         Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
1474                 "attributes to load: %s\n",
1475                 op->ors_deref,
1476                 op->ors_attrsonly,
1477                 op->ors_attrs == NULL ? "all" : "custom list" );
1478
1479         if ( op->o_req_ndn.bv_len > BACKSQL_MAX_DN_LEN ) {
1480                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1481                         "search base length (%ld) exceeds max length (%d)\n", 
1482                         op->o_req_ndn.bv_len, BACKSQL_MAX_DN_LEN, 0 );
1483                 /*
1484                  * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
1485                  * since it is impossible that such a long DN exists
1486                  * in the backend
1487                  */
1488                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1489                 send_ldap_result( op, rs );
1490                 return 1;
1491         }
1492
1493         sres = backsql_get_db_conn( op, &dbh );
1494         if ( sres != LDAP_SUCCESS ) {
1495                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1496                         "could not get connection handle - exiting\n", 
1497                         0, 0, 0 );
1498                 rs->sr_err = sres;
1499                 rs->sr_text = sres == LDAP_OTHER ?  "SQL-backend error" : NULL;
1500                 send_ldap_result( op, rs );
1501                 return 1;
1502         }
1503
1504         /* compute it anyway; root does not use it */
1505         stoptime = op->o_time + op->ors_tlimit;
1506
1507         base = op->o_req_dn;
1508         if ( backsql_api_dn2odbc( op, rs, &base ) ) {
1509                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1510                         "backsql_api_dn2odbc failed\n", 
1511                         0, 0, 0 );
1512                 rs->sr_err = LDAP_OTHER;
1513                 rs->sr_text = "SQL-backend error";
1514                 send_ldap_result( op, rs );
1515                 return 1;
1516         }
1517
1518         /* init search */
1519         rs->sr_err = backsql_init_search( &srch_info, &base,
1520                         op->ors_scope,
1521                         op->ors_slimit, op->ors_tlimit,
1522                         stoptime, op->ors_filter,
1523                         dbh, op, rs, op->ors_attrs, 1 );
1524         if ( rs->sr_err != LDAP_SUCCESS ) {
1525                 send_ldap_result( op, rs );
1526                 goto done;
1527         }
1528
1529         /*
1530          * for each objectclass we try to construct query which gets IDs
1531          * of entries matching LDAP query filter and scope (or at least 
1532          * candidates), and get the IDs
1533          */
1534         srch_info.bsi_n_candidates =
1535                 ( op->ors_limit == NULL /* isroot == FALSE */ ? -2 : 
1536                 ( op->ors_limit->lms_s_unchecked == -1 ? -2 :
1537                 ( op->ors_limit->lms_s_unchecked ) ) );
1538         avl_apply( bi->sql_oc_by_oc, backsql_oc_get_candidates,
1539                         &srch_info, BACKSQL_AVL_STOP, AVL_INORDER );
1540         if ( op->ors_limit != NULL      /* isroot == TRUE */
1541                         && op->ors_limit->lms_s_unchecked != -1
1542                         && srch_info.bsi_n_candidates == -1 )
1543         {
1544                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1545                 send_ldap_result( op, rs );
1546                 goto done;
1547         }
1548
1549         /*
1550          * now we load candidate entries (only those attributes 
1551          * mentioned in attrs and filter), test it against full filter 
1552          * and then send to client
1553          */
1554         for ( eid = srch_info.bsi_id_list;
1555                         eid != NULL; 
1556                         eid = backsql_free_entryID( eid, 1 ) )
1557         {
1558                 int             rc;
1559                 Attribute       *hasSubordinate = NULL,
1560                                 *a = NULL;
1561
1562                 /* check for abandon */
1563                 if ( op->o_abandon ) {
1564                         break;
1565                 }
1566
1567                 /* check time limit */
1568                 if ( op->ors_tlimit != SLAP_NO_LIMIT
1569                                 && slap_get_time() > stoptime )
1570                 {
1571                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1572                         rs->sr_ctrls = NULL;
1573                         rs->sr_ref = rs->sr_v2ref;
1574                         rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS
1575                                 : LDAP_REFERRAL;
1576                         send_ldap_result( op, rs );
1577                         goto end_of_search;
1578                 }
1579
1580 #ifdef BACKSQL_ARBITRARY_KEY
1581                 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1582                         "for entry id=%s, oc_id=%ld, keyval=%s\n",
1583                         eid->eid_id.bv_val, eid->eid_oc_id,
1584                         eid->eid_keyval.bv_val );
1585 #else /* ! BACKSQL_ARBITRARY_KEY */
1586                 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1587                         "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
1588                         eid->eid_id, eid->eid_oc_id, eid->eid_keyval );
1589 #endif /* ! BACKSQL_ARBITRARY_KEY */
1590
1591                 srch_info.bsi_e = &user_entry;
1592                 rc = backsql_id2entry( &srch_info, eid );
1593                 if ( rc != LDAP_SUCCESS ) {
1594                         Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1595                                 "error %d in backsql_id2entry() "
1596                                 "- skipping\n", rc, 0, 0 );
1597                         continue;
1598                 }
1599
1600                 /* check scope */
1601                 switch ( op->ors_scope ) {
1602                 case LDAP_SCOPE_BASE:
1603                 case BACKSQL_SCOPE_BASE_LIKE:
1604                         if ( !bvmatch( &user_entry.e_nname, &op->o_req_ndn ) ) {
1605                                 goto next_entry;
1606                         }
1607                         break;
1608
1609                 case LDAP_SCOPE_ONE:
1610                 {
1611                         struct berval   rdn = user_entry.e_nname;
1612                         rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
1613                         if ( !dnIsOneLevelRDN( &rdn ) ) {
1614                                 goto next_entry;
1615                         }
1616                         /* fall thru */
1617                 }
1618
1619                 case LDAP_SCOPE_SUBTREE:
1620                         if ( !dnIsSuffix( &user_entry.e_nname, &op->o_req_ndn ) ) {
1621                                 goto next_entry;
1622                         }
1623                         break;
1624                 }
1625
1626                 if ( !manageDSAit &&
1627                                 op->ors_scope != LDAP_SCOPE_BASE &&
1628                                 op->ors_scope != BACKSQL_SCOPE_BASE_LIKE &&
1629                                 is_entry_referral( &user_entry ) )
1630                 {
1631                         BerVarray refs;
1632
1633                         refs = get_entry_referrals( op, &user_entry );
1634                         if ( !refs ) {
1635                                 backsql_srch_info       srch_info2 = { 0 };
1636                                 Entry                   user_entry2 = { 0 };
1637
1638                                 /* retry with the full entry... */
1639                                 (void)backsql_init_search( &srch_info2,
1640                                                 &user_entry.e_name,
1641                                                 LDAP_SCOPE_BASE, 
1642                                                 -1, -1, -1, NULL,
1643                                                 dbh, op, rs, NULL, 0 );
1644                                 srch_info2.bsi_e = &user_entry2;
1645                                 rc = backsql_id2entry( &srch_info2, eid );
1646                                 if ( rc == LDAP_SUCCESS ) {
1647                                         if ( is_entry_referral( &user_entry2 ) )
1648                                         {
1649                                                 refs = get_entry_referrals( op,
1650                                                                 &user_entry2 );
1651                                         } /* else: FIXME: inconsistency! */
1652                                         entry_clean( &user_entry2 );
1653                                 }
1654                         }
1655
1656                         if ( refs ) {
1657                                 rs->sr_ref = referral_rewrite( refs,
1658                                                 &user_entry.e_name,
1659                                                 &op->o_req_dn,
1660                                                 op->ors_scope );
1661                                 ber_bvarray_free( refs );
1662                         }
1663
1664                         if ( !rs->sr_ref ) {
1665                                 rs->sr_text = "bad_referral object";
1666                         }
1667
1668                         rs->sr_err = LDAP_REFERRAL;
1669                         rs->sr_matched = user_entry.e_name.bv_val;
1670                         send_search_reference( op, rs );
1671
1672                         ber_bvarray_free( rs->sr_ref );
1673                         rs->sr_ref = NULL;
1674                         rs->sr_matched = NULL;
1675
1676                         goto next_entry;
1677                 }
1678
1679                 /*
1680                  * We use this flag since we need to parse the filter
1681                  * anyway; we should have used the frontend API function
1682                  * filter_has_subordinates()
1683                  */
1684                 if ( srch_info.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
1685                         rc = backsql_has_children( bi, dbh, &user_entry.e_nname );
1686
1687                         switch ( rc ) {
1688                         case LDAP_COMPARE_TRUE:
1689                         case LDAP_COMPARE_FALSE:
1690                                 hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
1691                                 if ( hasSubordinate != NULL ) {
1692                                         for ( a = user_entry.e_attrs; 
1693                                                         a && a->a_next; 
1694                                                         a = a->a_next );
1695
1696                                         a->a_next = hasSubordinate;
1697                                 }
1698                                 rc = 0;
1699                                 break;
1700
1701                         default:
1702                                 Debug(LDAP_DEBUG_TRACE, 
1703                                         "backsql_search(): "
1704                                         "has_children failed( %d)\n", 
1705                                         rc, 0, 0 );
1706                                 rc = 1;
1707                                 goto next_entry;
1708                         }
1709                 }
1710
1711                 if ( test_filter( op, &user_entry, op->ors_filter )
1712                                 == LDAP_COMPARE_TRUE ) {
1713                         if ( hasSubordinate && !( srch_info.bsi_flags & BSQL_SF_ALL_OPER ) 
1714                                         && !ad_inlist( slap_schema.si_ad_hasSubordinates, op->ors_attrs ) ) {
1715                                 a->a_next = NULL;
1716                                 attr_free( hasSubordinate );
1717                                 hasSubordinate = NULL;
1718                         }
1719
1720                         rs->sr_attrs = op->ors_attrs;
1721                         rs->sr_operational_attrs = NULL;
1722                         rs->sr_entry = &user_entry;
1723                         rs->sr_flags = REP_ENTRY_MODIFIABLE;
1724                         sres = send_search_entry( op, rs );
1725                         rs->sr_entry = NULL;
1726                         rs->sr_attrs = NULL;
1727                         rs->sr_operational_attrs = NULL;
1728
1729                         switch ( sres ) {
1730                         case 0:
1731                                 break;
1732
1733                         default:
1734                                 /*
1735                                  * FIXME: send_search_entry failed;
1736                                  * better stop
1737                                  */
1738                         case -1:
1739                                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1740                                         "connection lost\n", 0, 0, 0 );
1741                                 goto end_of_search;
1742                         }
1743                 }
1744
1745 next_entry:;
1746                 entry_clean( &user_entry );
1747
1748                 if ( op->ors_slimit != SLAP_NO_LIMIT
1749                                 && rs->sr_nentries >= op->ors_slimit )
1750                 {
1751                         rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1752                         send_ldap_result( op, rs );
1753                         goto end_of_search;
1754                 }
1755         }
1756
1757 end_of_search:;
1758         /* in case we got here accidentally */
1759         entry_clean( &user_entry );
1760
1761         if ( rs->sr_nentries > 0 ) {
1762                 rs->sr_ref = rs->sr_v2ref;
1763                 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS
1764                         : LDAP_REFERRAL;
1765
1766         } else {
1767                 rs->sr_err = srch_info.bsi_status;
1768         }
1769         send_ldap_result( op, rs );
1770
1771         if ( rs->sr_v2ref ) {
1772                 ber_bvarray_free( rs->sr_v2ref );
1773                 rs->sr_v2ref = NULL;
1774         }
1775
1776 done:;
1777         if ( !BER_BVISNULL( &srch_info.bsi_base_id.eid_dn ) ) {
1778                 (void)backsql_free_entryID( &srch_info.bsi_base_id, 0 );
1779         }
1780
1781         if ( srch_info.bsi_attrs ) {
1782                 ch_free( srch_info.bsi_attrs );
1783         }
1784
1785         if ( base.bv_val != op->o_req_ndn.bv_val ) {
1786                 ch_free( base.bv_val );
1787         }
1788
1789         Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
1790         return 0;
1791 }
1792
1793 #endif /* SLAPD_SQL */
1794