]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/search.c
e501d1b3bc22d8605e5755ffde4b8e75ecc0f8bc
[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-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * Portions Copyright 2002 Pierangelo Masarati.
7  * Portions Copyright 2004 Mark Adamson.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by Dmitry Kovalev for inclusion
20  * by OpenLDAP Software.  Additional significant contributors include
21  * Pierangelo Masarati and Mark Adamson.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include "ac/string.h"
29 #include "ac/ctype.h"
30
31 #include "slap.h"
32 #include "proto-sql.h"
33
34 static int backsql_process_filter( backsql_srch_info *bsi, Filter *f );
35 static int backsql_process_filter_eq( backsql_srch_info *bsi, 
36                 backsql_at_map_rec *at,
37                 int casefold, struct berval *filter_value );
38 static int backsql_process_filter_like( backsql_srch_info *bsi, 
39                 backsql_at_map_rec *at,
40                 int casefold, struct berval *filter_value );
41 static int backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, 
42                 backsql_at_map_rec *at );
43
44 static int
45 backsql_attrlist_add( backsql_srch_info *bsi, AttributeDescription *ad )
46 {
47         int             n_attrs = 0;
48         AttributeName   *an = NULL;
49
50         if ( bsi->bsi_attrs == NULL ) {
51                 return 1;
52         }
53
54         /*
55          * clear the list (retrieve all attrs)
56          */
57         if ( ad == NULL ) {
58                 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs, bsi->bsi_op->o_tmpmemctx );
59                 bsi->bsi_attrs = NULL;
60                 bsi->bsi_flags |= BSQL_SF_ALL_ATTRS;
61                 return 1;
62         }
63
64         for ( ; !BER_BVISNULL( &bsi->bsi_attrs[ n_attrs ].an_name ); n_attrs++ ) {
65                 an = &bsi->bsi_attrs[ n_attrs ];
66                 
67                 Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
68                         "attribute \"%s\" is in list\n", 
69                         an->an_name.bv_val, 0, 0 );
70                 /*
71                  * We can live with strcmp because the attribute 
72                  * list has been normalized before calling be_search
73                  */
74                 if ( !BACKSQL_NCMP( &an->an_name, &ad->ad_cname ) ) {
75                         return 1;
76                 }
77         }
78         
79         Debug( LDAP_DEBUG_TRACE, "==>backsql_attrlist_add(): "
80                 "adding \"%s\" to list\n", ad->ad_cname.bv_val, 0, 0 );
81
82         an = (AttributeName *)bsi->bsi_op->o_tmprealloc( bsi->bsi_attrs,
83                         sizeof( AttributeName ) * ( n_attrs + 2 ),
84                         bsi->bsi_op->o_tmpmemctx );
85         if ( an == NULL ) {
86                 return -1;
87         }
88
89         an[ n_attrs ].an_name = ad->ad_cname;
90         an[ n_attrs ].an_desc = ad;
91         BER_BVZERO( &an[ n_attrs + 1 ].an_name );
92
93         bsi->bsi_attrs = an;
94         
95         return 1;
96 }
97
98 /*
99  * Initializes the search structure.
100  * 
101  * If get_base_id != 0, the field bsi_base_id is filled 
102  * with the entryID of bsi_base_ndn; it must be freed
103  * by backsql_free_entryID() when no longer required.
104  *
105  * NOTE: base must be normalized
106  */
107 int
108 backsql_init_search(
109         backsql_srch_info       *bsi, 
110         struct berval           *nbase, 
111         int                     scope, 
112         int                     slimit,
113         int                     tlimit,
114         time_t                  stoptime, 
115         Filter                  *filter, 
116         SQLHDBC                 dbh,
117         Operation               *op,
118         SlapReply               *rs,
119         AttributeName           *attrs,
120         unsigned                flags )
121 {
122         backsql_info            *bi = (backsql_info *)op->o_bd->be_private;
123         int                     rc = LDAP_SUCCESS;
124
125         bsi->bsi_base_ndn = nbase;
126         bsi->bsi_use_subtree_shortcut = 0;
127         BER_BVZERO( &bsi->bsi_base_id.eid_dn );
128         BER_BVZERO( &bsi->bsi_base_id.eid_ndn );
129         bsi->bsi_scope = scope;
130         bsi->bsi_slimit = slimit;
131         bsi->bsi_tlimit = tlimit;
132         bsi->bsi_filter = filter;
133         bsi->bsi_dbh = dbh;
134         bsi->bsi_op = op;
135         bsi->bsi_rs = rs;
136         bsi->bsi_flags = BSQL_SF_NONE;
137
138         bsi->bsi_attrs = NULL;
139
140         if ( BACKSQL_FETCH_ALL_ATTRS( bi ) ) {
141                 /*
142                  * if requested, simply try to fetch all attributes
143                  */
144                 bsi->bsi_flags |= BSQL_SF_ALL_ATTRS;
145
146         } else {
147                 if ( BACKSQL_FETCH_ALL_USERATTRS( bi ) ) {
148                         bsi->bsi_flags |= BSQL_SF_ALL_USER;
149
150                 } else if ( BACKSQL_FETCH_ALL_OPATTRS( bi ) ) {
151                         bsi->bsi_flags |= BSQL_SF_ALL_OPER;
152                 }
153
154                 if ( attrs == NULL ) {
155                         /* NULL means all user attributes */
156                         bsi->bsi_flags |= BSQL_SF_ALL_USER;
157
158                 } else {
159                         AttributeName   *p;
160                         int             got_oc = 0;
161
162                         bsi->bsi_attrs = (AttributeName *)bsi->bsi_op->o_tmpalloc(
163                                         sizeof( AttributeName ),
164                                         bsi->bsi_op->o_tmpmemctx );
165                         BER_BVZERO( &bsi->bsi_attrs[ 0 ].an_name );
166         
167                         for ( p = attrs; !BER_BVISNULL( &p->an_name ); p++ ) {
168                                 if ( BACKSQL_NCMP( &p->an_name, &AllUser ) == 0 ) {
169                                         /* handle "*" */
170                                         bsi->bsi_flags |= BSQL_SF_ALL_USER;
171
172                                         /* if all attrs are requested, there's
173                                          * no need to continue */
174                                         if ( BSQL_ISF_ALL_ATTRS( bsi ) ) {
175                                                 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
176                                                                 bsi->bsi_op->o_tmpmemctx );
177                                                 bsi->bsi_attrs = NULL;
178                                                 break;
179                                         }
180                                         continue;
181
182                                 } else if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
183                                         /* handle "+" */
184                                         bsi->bsi_flags |= BSQL_SF_ALL_OPER;
185
186                                         /* if all attrs are requested, there's
187                                          * no need to continue */
188                                         if ( BSQL_ISF_ALL_ATTRS( bsi ) ) {
189                                                 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
190                                                                 bsi->bsi_op->o_tmpmemctx );
191                                                 bsi->bsi_attrs = NULL;
192                                                 break;
193                                         }
194                                         continue;
195
196                                 } else if ( BACKSQL_NCMP( &p->an_name, &NoAttrs ) == 0 ) {
197                                         /* ignore "1.1" */
198                                         continue;
199
200                                 } else if ( p->an_desc == slap_schema.si_ad_objectClass ) {
201                                         got_oc = 1;
202                                 }
203
204                                 backsql_attrlist_add( bsi, p->an_desc );
205                         }
206
207                         if ( got_oc == 0 && !( bsi->bsi_flags & BSQL_SF_ALL_USER ) ) {
208                                 /* add objectClass if not present,
209                                  * because it is required to understand
210                                  * if an entry is a referral, an alias 
211                                  * or so... */
212                                 backsql_attrlist_add( bsi, slap_schema.si_ad_objectClass );
213                         }
214                 }
215
216                 if ( !BSQL_ISF_ALL_ATTRS( bsi ) && bi->sql_anlist ) {
217                         AttributeName   *p;
218                         
219                         /* use hints if available */
220                         for ( p = bi->sql_anlist; !BER_BVISNULL( &p->an_name ); p++ ) {
221                                 if ( BACKSQL_NCMP( &p->an_name, &AllUser ) == 0 ) {
222                                         /* handle "*" */
223                                         bsi->bsi_flags |= BSQL_SF_ALL_USER;
224
225                                         /* if all attrs are requested, there's
226                                          * no need to continue */
227                                         if ( BSQL_ISF_ALL_ATTRS( bsi ) ) {
228                                                 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
229                                                                 bsi->bsi_op->o_tmpmemctx );
230                                                 bsi->bsi_attrs = NULL;
231                                                 break;
232                                         }
233                                         continue;
234
235                                 } else if ( BACKSQL_NCMP( &p->an_name, &AllOper ) == 0 ) {
236                                         /* handle "+" */
237                                         bsi->bsi_flags |= BSQL_SF_ALL_OPER;
238
239                                         /* if all attrs are requested, there's
240                                          * no need to continue */
241                                         if ( BSQL_ISF_ALL_ATTRS( bsi ) ) {
242                                                 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
243                                                                 bsi->bsi_op->o_tmpmemctx );
244                                                 bsi->bsi_attrs = NULL;
245                                                 break;
246                                         }
247                                         continue;
248                                 }
249
250                                 backsql_attrlist_add( bsi, p->an_desc );
251                         }
252
253                 }
254         }
255
256         bsi->bsi_abandon = 0;
257         bsi->bsi_id_list = NULL;
258         bsi->bsi_id_listtail = &bsi->bsi_id_list;
259         bsi->bsi_n_candidates = 0;
260         bsi->bsi_stoptime = stoptime;
261         BER_BVZERO( &bsi->bsi_sel.bb_val );
262         bsi->bsi_sel.bb_len = 0;
263         BER_BVZERO( &bsi->bsi_from.bb_val );
264         bsi->bsi_from.bb_len = 0;
265         BER_BVZERO( &bsi->bsi_join_where.bb_val );
266         bsi->bsi_join_where.bb_len = 0;
267         BER_BVZERO( &bsi->bsi_flt_where.bb_val );
268         bsi->bsi_flt_where.bb_len = 0;
269         bsi->bsi_filter_oc = NULL;
270
271         if ( BACKSQL_IS_GET_ID( flags ) ) {
272                 int     matched = BACKSQL_IS_MATCHED( flags );
273                 int     getentry = BACKSQL_IS_GET_ENTRY( flags );
274
275                 assert( op->o_bd->be_private );
276
277                 rc = backsql_dn2id( op, rs, dbh, nbase, &bsi->bsi_base_id,
278                                 matched, 1 );
279                 
280                 if ( ( rc == LDAP_NO_SUCH_OBJECT && matched ) || getentry ) {
281                         if ( !BER_BVISNULL( &bsi->bsi_base_id.eid_ndn ) ) {
282                                 assert( bsi->bsi_e != NULL );
283
284                                 /*
285                                  * let's see if it is a referral and, in case, get it
286                                  */
287                                 backsql_attrlist_add( bsi, slap_schema.si_ad_ref );
288                                 rc = backsql_id2entry( bsi, &bsi->bsi_base_id );
289                                 if ( rc == LDAP_SUCCESS && is_entry_referral( bsi->bsi_e ) )
290                                 {
291                                         BerVarray erefs = get_entry_referrals( op, bsi->bsi_e );
292                                         if ( erefs ) {
293                                                 rc = rs->sr_err = LDAP_REFERRAL;
294                                                 rs->sr_ref = referral_rewrite( erefs,
295                                                                 &bsi->bsi_e->e_nname,
296                                                                 &op->o_req_dn,
297                                                                 scope );
298                                                 ber_bvarray_free( erefs );
299
300                                         } else {
301                                                 rc = rs->sr_err = LDAP_OTHER;
302                                                 rs->sr_text = "bad referral object";
303                                         }
304
305                                 } else {
306                                         rc = rs->sr_err = getentry ?
307                                                 LDAP_SUCCESS : LDAP_NO_SUCH_OBJECT;
308                                 }
309
310                         } else {
311                                 rs->sr_ref = referral_rewrite( default_referral,
312                                                 NULL, &op->o_req_dn, scope );
313                                 rc = rs->sr_err = LDAP_REFERRAL;
314                         }
315                 }
316         }
317
318         bsi->bsi_status = rc;
319
320         switch ( rc ) {
321         case LDAP_SUCCESS:
322         case LDAP_REFERRAL:
323                 break;
324
325         default:
326                 bsi->bsi_op->o_tmpfree( bsi->bsi_attrs,
327                                 bsi->bsi_op->o_tmpmemctx );
328                 break;
329         }
330
331         return rc;
332 }
333
334 static int
335 backsql_process_filter_list( backsql_srch_info *bsi, Filter *f, int op )
336 {
337         int             res;
338
339         if ( !f ) {
340                 return 0;
341         }
342
343         backsql_strfcat( &bsi->bsi_flt_where, "c", '(' /* ) */  );
344
345         while ( 1 ) {
346                 res = backsql_process_filter( bsi, f );
347                 if ( res < 0 ) {
348                         /*
349                          * TimesTen : If the query has no answers,
350                          * don't bother to run the query.
351                          */
352                         return -1;
353                 }
354  
355                 f = f->f_next;
356                 if ( f == NULL ) {
357                         break;
358                 }
359
360                 switch ( op ) {
361                 case LDAP_FILTER_AND:
362                         backsql_strfcat( &bsi->bsi_flt_where, "l",
363                                         (ber_len_t)STRLENOF( " AND " ), 
364                                                 " AND " );
365                         break;
366
367                 case LDAP_FILTER_OR:
368                         backsql_strfcat( &bsi->bsi_flt_where, "l",
369                                         (ber_len_t)STRLENOF( " OR " ),
370                                                 " OR " );
371                         break;
372                 }
373         }
374
375         backsql_strfcat( &bsi->bsi_flt_where, "c", /* ( */ ')' );
376
377         return 1;
378 }
379
380 static int
381 backsql_process_sub_filter( backsql_srch_info *bsi, Filter *f,
382         backsql_at_map_rec *at )
383 {
384         backsql_info            *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
385         int                     i;
386         int                     casefold = 0;
387
388         if ( !f ) {
389                 return 0;
390         }
391
392         /* always uppercase strings by now */
393 #ifdef BACKSQL_UPPERCASE_FILTER
394         if ( f->f_sub_desc->ad_type->sat_substr &&
395                         SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
396                                 bi->sql_caseIgnoreMatch ) )
397 #endif /* BACKSQL_UPPERCASE_FILTER */
398         {
399                 casefold = 1;
400         }
401
402         if ( f->f_sub_desc->ad_type->sat_substr &&
403                         SLAP_MR_ASSOCIATED( f->f_sub_desc->ad_type->sat_substr,
404                                 bi->sql_telephoneNumberMatch ) )
405         {
406
407                 struct berval   bv;
408                 ber_len_t       i, s, a;
409
410                 /*
411                  * to check for matching telephone numbers
412                  * with intermixed chars, e.g. val='1234'
413                  * use
414                  * 
415                  * val LIKE '%1%2%3%4%'
416                  */
417
418                 BER_BVZERO( &bv );
419                 if ( f->f_sub_initial.bv_val ) {
420                         bv.bv_len += f->f_sub_initial.bv_len;
421                 }
422                 if ( f->f_sub_any != NULL ) {
423                         for ( a = 0; f->f_sub_any[ a ].bv_val != NULL; a++ ) {
424                                 bv.bv_len += f->f_sub_any[ a ].bv_len;
425                         }
426                 }
427                 if ( f->f_sub_final.bv_val ) {
428                         bv.bv_len += f->f_sub_final.bv_len;
429                 }
430                 bv.bv_len = 2 * bv.bv_len - 1;
431                 bv.bv_val = ch_malloc( bv.bv_len + 1 );
432
433                 s = 0;
434                 if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
435                         bv.bv_val[ s ] = f->f_sub_initial.bv_val[ 0 ];
436                         for ( i = 1; i < f->f_sub_initial.bv_len; i++ ) {
437                                 bv.bv_val[ s + 2 * i - 1 ] = '%';
438                                 bv.bv_val[ s + 2 * i ] = f->f_sub_initial.bv_val[ i ];
439                         }
440                         bv.bv_val[ s + 2 * i - 1 ] = '%';
441                         s += 2 * i;
442                 }
443
444                 if ( f->f_sub_any != NULL ) {
445                         for ( a = 0; !BER_BVISNULL( &f->f_sub_any[ a ] ); a++ ) {
446                                 bv.bv_val[ s ] = f->f_sub_any[ a ].bv_val[ 0 ];
447                                 for ( i = 1; i < f->f_sub_any[ a ].bv_len; i++ ) {
448                                         bv.bv_val[ s + 2 * i - 1 ] = '%';
449                                         bv.bv_val[ s + 2 * i ] = f->f_sub_any[ a ].bv_val[ i ];
450                                 }
451                                 bv.bv_val[ s + 2 * i - 1 ] = '%';
452                                 s += 2 * i;
453                         }
454                 }
455
456                 if ( !BER_BVISNULL( &f->f_sub_final ) ) {
457                         bv.bv_val[ s ] = f->f_sub_final.bv_val[ 0 ];
458                         for ( i = 1; i < f->f_sub_final.bv_len; i++ ) {
459                                 bv.bv_val[ s + 2 * i - 1 ] = '%';
460                                 bv.bv_val[ s + 2 * i ] = f->f_sub_final.bv_val[ i ];
461                         }
462                                 bv.bv_val[ s + 2 * i - 1 ] = '%';
463                         s += 2 * i;
464                 }
465
466                 bv.bv_val[ s - 1 ] = '\0';
467
468                 (void)backsql_process_filter_like( bsi, at, casefold, &bv );
469                 ch_free( bv.bv_val );
470
471                 return 1;
472         }
473
474         /*
475          * When dealing with case-sensitive strings 
476          * we may omit normalization; however, normalized
477          * SQL filters are more liberal.
478          */
479
480         backsql_strfcat( &bsi->bsi_flt_where, "c", '(' /* ) */  );
481
482         /* TimesTen */
483         Debug( LDAP_DEBUG_TRACE, "backsql_process_sub_filter(%s):\n",
484                 at->bam_ad->ad_cname.bv_val, 0, 0 );
485         Debug(LDAP_DEBUG_TRACE, "   expr: '%s%s%s'\n", at->bam_sel_expr.bv_val,
486                 at->bam_sel_expr_u.bv_val ? "' '" : "",
487                 at->bam_sel_expr_u.bv_val ? at->bam_sel_expr_u.bv_val : "" );
488         if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
489                 /*
490                  * If a pre-upper-cased version of the column 
491                  * or a precompiled upper function exists, use it
492                  */
493                 backsql_strfcat( &bsi->bsi_flt_where, 
494                                 "bl",
495                                 &at->bam_sel_expr_u,
496                                 (ber_len_t)STRLENOF( " LIKE '" ),
497                                         " LIKE '" );
498
499         } else {
500                 backsql_strfcat( &bsi->bsi_flt_where, "bl",
501                                 &at->bam_sel_expr,
502                                 (ber_len_t)STRLENOF( " LIKE '" ), " LIKE '" );
503         }
504  
505         if ( !BER_BVISNULL( &f->f_sub_initial ) ) {
506                 ber_len_t       start;
507
508 #ifdef BACKSQL_TRACE
509                 Debug( LDAP_DEBUG_TRACE, 
510                         "==>backsql_process_sub_filter(%s): "
511                         "sub_initial=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
512                         f->f_sub_initial.bv_val, 0 );
513 #endif /* BACKSQL_TRACE */
514
515                 start = bsi->bsi_flt_where.bb_val.bv_len;
516                 backsql_strfcat( &bsi->bsi_flt_where, "b",
517                                 &f->f_sub_initial );
518                 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
519                         ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
520                 }
521         }
522
523         backsql_strfcat( &bsi->bsi_flt_where, "c", '%' );
524
525         if ( f->f_sub_any != NULL ) {
526                 for ( i = 0; !BER_BVISNULL( &f->f_sub_any[ i ] ); i++ ) {
527                         ber_len_t       start;
528
529 #ifdef BACKSQL_TRACE
530                         Debug( LDAP_DEBUG_TRACE, 
531                                 "==>backsql_process_sub_filter(%s): "
532                                 "sub_any[%d]=\"%s\"\n", at->bam_ad->ad_cname.bv_val, 
533                                 i, f->f_sub_any[ i ].bv_val );
534 #endif /* BACKSQL_TRACE */
535
536                         start = bsi->bsi_flt_where.bb_val.bv_len;
537                         backsql_strfcat( &bsi->bsi_flt_where,
538                                         "bc",
539                                         &f->f_sub_any[ i ],
540                                         '%' );
541                         if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
542                                 /*
543                                  * Note: toupper('%') = '%'
544                                  */
545                                 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
546                         }
547                 }
548         }
549
550         if ( !BER_BVISNULL( &f->f_sub_final ) ) {
551                 ber_len_t       start;
552
553 #ifdef BACKSQL_TRACE
554                 Debug( LDAP_DEBUG_TRACE, 
555                         "==>backsql_process_sub_filter(%s): "
556                         "sub_final=\"%s\"\n", at->bam_ad->ad_cname.bv_val,
557                         f->f_sub_final.bv_val, 0 );
558 #endif /* BACKSQL_TRACE */
559
560                 start = bsi->bsi_flt_where.bb_val.bv_len;
561                 backsql_strfcat( &bsi->bsi_flt_where, "b",
562                                 &f->f_sub_final );
563                 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
564                         ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
565                 }
566         }
567
568         backsql_strfcat( &bsi->bsi_flt_where, "l", 
569                         (ber_len_t)STRLENOF( /* (' */ "')" ), /* (' */ "')" );
570  
571         return 1;
572 }
573
574 static int
575 backsql_merge_from_tbls( backsql_srch_info *bsi, struct berval *from_tbls )
576 {
577         if ( BER_BVISNULL( from_tbls ) ) {
578                 return LDAP_SUCCESS;
579         }
580
581         if ( !BER_BVISNULL( &bsi->bsi_from.bb_val ) ) {
582                 char    *start, *end, *tmp;
583
584                 tmp = ch_strdup( from_tbls->bv_val );
585
586                 for ( start = tmp, end = strchr( start, ',' ); start; ) {
587                         if ( end ) {
588                                 end[0] = '\0';
589                         }
590
591                         if ( strstr( bsi->bsi_from.bb_val.bv_val, start) == NULL )
592                         {
593                                 backsql_strfcat( &bsi->bsi_from, "cs", ',', start );
594                         }
595
596                         if ( end ) {
597                                 /* in case there are spaces after the comma... */
598                                 for ( start = &end[1]; isspace( start[0] ); start++ );
599                                 if ( start[0] ) {
600                                         end = strchr( start, ',' );
601                                 } else {
602                                         start = NULL;
603                                 }
604                         } else {
605                                 start = NULL;
606                         }
607                 }
608
609                 ch_free( tmp );
610
611         } else {
612                 backsql_strfcat( &bsi->bsi_from, "b", from_tbls );
613         }
614
615         return LDAP_SUCCESS;
616 }
617
618 static int
619 backsql_process_filter( backsql_srch_info *bsi, Filter *f )
620 {
621         backsql_at_map_rec      **vat = NULL;
622         AttributeDescription    *ad = NULL;
623         unsigned                i;
624         int                     done = 0;
625         int                     rc = 0;
626
627         Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter()\n", 0, 0, 0 );
628         if ( f->f_choice == SLAPD_FILTER_COMPUTED ) {
629                 Debug( LDAP_DEBUG_TRACE, "backsql_process_filter(): "
630                         "invalid filter\n", 0, 0, 0 );
631                 rc = -1;
632                 goto done;
633         }
634
635         switch( f->f_choice ) {
636         case LDAP_FILTER_OR:
637                 rc = backsql_process_filter_list( bsi, f->f_or, 
638                                 LDAP_FILTER_OR );
639                 done = 1;
640                 break;
641                 
642         case LDAP_FILTER_AND:
643                 rc = backsql_process_filter_list( bsi, f->f_and,
644                                 LDAP_FILTER_AND );
645                 done = 1;
646                 break;
647
648         case LDAP_FILTER_NOT:
649                 backsql_strfcat( &bsi->bsi_flt_where, "l",
650                                 (ber_len_t)STRLENOF( "NOT (" /* ) */ ),
651                                         "NOT (" /* ) */ );
652                 rc = backsql_process_filter( bsi, f->f_not );
653                 backsql_strfcat( &bsi->bsi_flt_where, "c", /* ( */ ')' );
654                 done = 1;
655                 break;
656
657         case LDAP_FILTER_PRESENT:
658                 ad = f->f_desc;
659                 break;
660                 
661         case LDAP_FILTER_EXT:
662                 ad = f->f_mra->ma_desc;
663                 if ( f->f_mr_dnattrs ) {
664                         /*
665                          * if dn attrs filtering is requested, better return 
666                          * success and let test_filter() deal with candidate
667                          * selection; otherwise we'd need to set conditions
668                          * on the contents of the DN, e.g. "SELECT ... FROM
669                          * ldap_entries AS attributeName WHERE attributeName.dn
670                          * like '%attributeName=value%'"
671                          */
672                         backsql_strfcat( &bsi->bsi_flt_where, "l",
673                                         (ber_len_t)STRLENOF( "1=1" ), "1=1" );
674                         bsi->bsi_status = LDAP_SUCCESS;
675                         rc = 1;
676                         goto done;
677                 }
678                 break;
679                 
680         default:
681                 ad = f->f_av_desc;
682                 break;
683         }
684
685         if ( rc == -1 ) {
686                 goto done;
687         }
688  
689         if ( done ) {
690                 rc = 1;
691                 goto done;
692         }
693
694         /*
695          * Turn structuralObjectClass into objectClass
696          */
697         if ( ad == slap_schema.si_ad_objectClass 
698                         || ad == slap_schema.si_ad_structuralObjectClass )
699         {
700                 /*
701                  * If the filter is LDAP_FILTER_PRESENT, then it's done;
702                  * otherwise, let's see if we are lucky: filtering
703                  * for "structural" objectclass or ancestor...
704                  */
705                 switch ( f->f_choice ) {
706                 case LDAP_FILTER_EQUALITY:
707                 {
708                         ObjectClass     *oc = oc_bvfind( &f->f_av_value );
709
710                         if ( oc == NULL ) {
711                                 Debug( LDAP_DEBUG_TRACE,
712                                                 "backsql_process_filter(): "
713                                                 "unknown objectClass \"%s\" "
714                                                 "in filter\n",
715                                                 f->f_av_value.bv_val, 0, 0 );
716                                 bsi->bsi_status = LDAP_OTHER;
717                                 rc = -1;
718                                 goto done;
719                         }
720
721                         /*
722                          * "structural" objectClass inheritance:
723                          * - a search for "person" will also return 
724                          *   "inetOrgPerson"
725                          * - a search for "top" will return everything
726                          */
727                         if ( is_object_subclass( oc, bsi->bsi_oc->bom_oc ) ) {
728                                 static struct berval ldap_entry_objclasses = BER_BVC( "ldap_entry_objclasses" );
729
730                                 backsql_merge_from_tbls( bsi, &ldap_entry_objclasses );
731
732                                 backsql_strfcat( &bsi->bsi_flt_where, "lbl",
733                                                 (ber_len_t)STRLENOF( "2=2 OR (ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */ ),
734                                                         "2=2 OR (ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */,
735                                                 &bsi->bsi_oc->bom_oc->soc_cname,
736                                                 (ber_len_t)STRLENOF( /* (' */ "')" ),
737                                                         /* (' */ "')" );
738                                 bsi->bsi_status = LDAP_SUCCESS;
739                                 rc = 1;
740                                 goto done;
741                         }
742
743                         break;
744                 }
745
746                 case LDAP_FILTER_PRESENT:
747                         backsql_strfcat( &bsi->bsi_flt_where, "l",
748                                         (ber_len_t)STRLENOF( "3=3" ), "3=3" );
749                         bsi->bsi_status = LDAP_SUCCESS;
750                         rc = 1;
751                         goto done;
752
753                         /* FIXME: LDAP_FILTER_EXT? */
754                         
755                 default:
756                         Debug( LDAP_DEBUG_TRACE,
757                                         "backsql_process_filter(): "
758                                         "illegal/unhandled filter "
759                                         "on objectClass attribute",
760                                         0, 0, 0 );
761                         bsi->bsi_status = LDAP_OTHER;
762                         rc = -1;
763                         goto done;
764                 }
765
766         } else if ( ad == slap_schema.si_ad_entryUUID ) {
767                 unsigned long   oc_id;
768 #ifdef BACKSQL_ARBITRARY_KEY
769                 struct berval   keyval;
770 #else /* ! BACKSQL_ARBITRARY_KEY */
771                 unsigned long   keyval;
772                 char            keyvalbuf[] = "18446744073709551615";
773 #endif /* ! BACKSQL_ARBITRARY_KEY */
774
775                 switch ( f->f_choice ) {
776                 case LDAP_FILTER_EQUALITY:
777                         backsql_entryUUID_decode( &f->f_av_value, &oc_id, &keyval );
778
779                         if ( oc_id != bsi->bsi_oc->bom_id ) {
780                                 bsi->bsi_status = LDAP_SUCCESS;
781                                 rc = -1;
782                                 goto done;
783                         }
784
785 #ifdef BACKSQL_ARBITRARY_KEY
786                         backsql_strfcat( &bsi->bsi_flt_where, "bcblbc",
787                                         &bsi->bsi_oc->bom_keytbl, '.',
788                                         &bsi->bsi_oc->bom_keycol,
789                                         STRLENOF( " LIKE '" ), " LIKE '",
790                                         &keyval, '\'' );
791 #else /* ! BACKSQL_ARBITRARY_KEY */
792                         snprintf( keyvalbuf, sizeof( keyvalbuf ), "%lu", keyval );
793                         backsql_strfcat( &bsi->bsi_flt_where, "bcbcs",
794                                         &bsi->bsi_oc->bom_keytbl, '.',
795                                         &bsi->bsi_oc->bom_keycol, '=', keyvalbuf );
796 #endif /* ! BACKSQL_ARBITRARY_KEY */
797                         break;
798
799                 case LDAP_FILTER_PRESENT:
800                         backsql_strfcat( &bsi->bsi_flt_where, "l",
801                                         (ber_len_t)STRLENOF( "4=4" ), "4=4" );
802                         break;
803
804                 default:
805                         rc = -1;
806                         goto done;
807                 }
808
809                 bsi->bsi_flags |= BSQL_SF_FILTER_ENTRYUUID;
810                 rc = 1;
811                 goto done;
812
813 #ifdef BACKSQL_SYNCPROV
814         } else if ( ad == slap_schema.si_ad_entryCSN ) {
815                 /*
816                  * support for syncrepl as producer...
817                  */
818                 if ( !bsi->bsi_op->o_sync ) {
819                         /* unsupported at present... */
820                         bsi->bsi_status = LDAP_OTHER;
821                         rc = -1;
822                         goto done;
823                 }
824
825                 bsi->bsi_flags |= ( BSQL_SF_FILTER_ENTRYCSN | BSQL_SF_RETURN_ENTRYUUID);
826
827                 /* if doing a syncrepl, try to return as much as possible,
828                  * and always match the filter */
829                 backsql_strfcat( &bsi->bsi_flt_where, "l",
830                                 (ber_len_t)STRLENOF( "5=5" ), "5=5" );
831
832                 /* save for later use in operational attributes */
833                 /* FIXME: saves only the first occurrence, because 
834                  * the filter during updates is written as
835                  * "(&(entryCSN<={contextCSN})(entryCSN>={oldContextCSN})({filter}))"
836                  * so we want our fake entryCSN to match the greatest
837                  * value
838                  */
839                 if ( bsi->bsi_op->o_private == NULL ) {
840                         bsi->bsi_op->o_private = &f->f_av_value;
841                 }
842                 bsi->bsi_status = LDAP_SUCCESS;
843
844                 rc = 1;
845                 goto done;
846 #endif /* BACKSQL_SYNCPROV */
847
848         } else if ( ad == slap_schema.si_ad_hasSubordinates || ad == NULL ) {
849                 /*
850                  * FIXME: this is not robust; e.g. a filter
851                  * '(!(hasSubordinates=TRUE))' fails because
852                  * in SQL it would read 'NOT (1=1)' instead 
853                  * of no condition.  
854                  * Note however that hasSubordinates is boolean, 
855                  * so a more appropriate filter would be 
856                  * '(hasSubordinates=FALSE)'
857                  *
858                  * A more robust search for hasSubordinates
859                  * would * require joining the ldap_entries table
860                  * selecting if there are descendants of the
861                  * candidate.
862                  */
863                 backsql_strfcat( &bsi->bsi_flt_where, "l",
864                                 (ber_len_t)STRLENOF( "6=6" ), "6=6" );
865                 if ( ad == slap_schema.si_ad_hasSubordinates ) {
866                         /*
867                          * instruct candidate selection algorithm
868                          * and attribute list to try to detect
869                          * if an entry has subordinates
870                          */
871                         bsi->bsi_flags |= BSQL_SF_FILTER_HASSUBORDINATE;
872
873                 } else {
874                         /*
875                          * clear attributes to fetch, to require ALL
876                          * and try extended match on all attributes
877                          */
878                         backsql_attrlist_add( bsi, NULL );
879                 }
880                 rc = 1;
881                 goto done;
882         }
883
884         /*
885          * attribute inheritance:
886          */
887         if ( backsql_supad2at( bsi->bsi_oc, ad, &vat ) ) {
888                 bsi->bsi_status = LDAP_OTHER;
889                 rc = -1;
890                 goto done;
891         }
892
893         if ( vat == NULL ) {
894                 /* search anyway; other parts of the filter
895                  * may succeeed */
896                 backsql_strfcat( &bsi->bsi_flt_where, "l",
897                                 (ber_len_t)STRLENOF( "7=7" ), "7=7" );
898                 bsi->bsi_status = LDAP_SUCCESS;
899                 rc = 1;
900                 goto done;
901         }
902
903         /* if required, open extra level of parens */
904         done = 0;
905         if ( vat[0]->bam_next || vat[1] ) {
906                 backsql_strfcat( &bsi->bsi_flt_where, "c", '(' );
907                 done = 1;
908         }
909
910         i = 0;
911 next:;
912         /* apply attr */
913         if ( backsql_process_filter_attr( bsi, f, vat[i] ) == -1 ) {
914                 return -1;
915         }
916
917         /* if more definitions of the same attr, apply */
918         if ( vat[i]->bam_next ) {
919                 backsql_strfcat( &bsi->bsi_flt_where, "l",
920                         STRLENOF( " OR " ), " OR " );
921                 vat[i] = vat[i]->bam_next;
922                 goto next;
923         }
924
925         /* if more descendants of the same attr, apply */
926         i++;
927         if ( vat[i] ) {
928                 backsql_strfcat( &bsi->bsi_flt_where, "l",
929                         STRLENOF( " OR " ), " OR " );
930                 goto next;
931         }
932
933         /* if needed, close extra level of parens */
934         if ( done ) {
935                 backsql_strfcat( &bsi->bsi_flt_where, "c", ')' );
936         }
937
938         rc = 1;
939
940 done:;
941         if ( vat ) {
942                 ch_free( vat );
943         }
944
945         Debug( LDAP_DEBUG_TRACE,
946                         "<==backsql_process_filter() %s\n",
947                         rc == 1 ? "succeeded" : "failed", 0, 0);
948
949         return rc;
950 }
951
952 static int
953 backsql_process_filter_eq( backsql_srch_info *bsi, backsql_at_map_rec *at,
954                 int casefold, struct berval *filter_value )
955 {
956         /*
957          * maybe we should check type of at->sel_expr here somehow,
958          * to know whether upper_func is applicable, but for now
959          * upper_func stuff is made for Oracle, where UPPER is
960          * safely applicable to NUMBER etc.
961          */
962         if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
963                 ber_len_t       start;
964
965                 backsql_strfcat( &bsi->bsi_flt_where, "cbl",
966                                 '(', /* ) */
967                                 &at->bam_sel_expr_u, 
968                                 (ber_len_t)STRLENOF( "='" ),
969                                         "='" );
970
971                 start = bsi->bsi_flt_where.bb_val.bv_len;
972
973                 backsql_strfcat( &bsi->bsi_flt_where, "bl",
974                                 filter_value, 
975                                 (ber_len_t)STRLENOF( /* (' */ "')" ),
976                                         /* (' */ "')" );
977
978                 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
979
980         } else {
981                 backsql_strfcat( &bsi->bsi_flt_where, "cblbl",
982                                 '(', /* ) */
983                                 &at->bam_sel_expr,
984                                 (ber_len_t)STRLENOF( "='" ), "='",
985                                 filter_value,
986                                 (ber_len_t)STRLENOF( /* (' */ "')" ),
987                                         /* (' */ "')" );
988         }
989
990         return 1;
991 }
992         
993 static int
994 backsql_process_filter_like( backsql_srch_info *bsi, backsql_at_map_rec *at,
995                 int casefold, struct berval *filter_value )
996 {
997         /*
998          * maybe we should check type of at->sel_expr here somehow,
999          * to know whether upper_func is applicable, but for now
1000          * upper_func stuff is made for Oracle, where UPPER is
1001          * safely applicable to NUMBER etc.
1002          */
1003         if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
1004                 ber_len_t       start;
1005
1006                 backsql_strfcat( &bsi->bsi_flt_where, "cbl",
1007                                 '(', /* ) */
1008                                 &at->bam_sel_expr_u, 
1009                                 (ber_len_t)STRLENOF( " LIKE '%" ),
1010                                         " LIKE '%" );
1011
1012                 start = bsi->bsi_flt_where.bb_val.bv_len;
1013
1014                 backsql_strfcat( &bsi->bsi_flt_where, "bl",
1015                                 filter_value, 
1016                                 (ber_len_t)STRLENOF( /* (' */ "%')" ),
1017                                         /* (' */ "%')" );
1018
1019                 ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
1020
1021         } else {
1022                 backsql_strfcat( &bsi->bsi_flt_where, "cblbl",
1023                                 '(', /* ) */
1024                                 &at->bam_sel_expr,
1025                                 (ber_len_t)STRLENOF( " LIKE '%" ),
1026                                         " LIKE '%",
1027                                 filter_value,
1028                                 (ber_len_t)STRLENOF( /* (' */ "%')" ),
1029                                         /* (' */ "%')" );
1030         }
1031
1032         return 1;
1033 }
1034
1035 static int
1036 backsql_process_filter_attr( backsql_srch_info *bsi, Filter *f, backsql_at_map_rec *at )
1037 {
1038         backsql_info            *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1039         int                     casefold = 0;
1040         struct berval           *filter_value = NULL;
1041         MatchingRule            *matching_rule = NULL;
1042         struct berval           ordering = BER_BVC("<=");
1043
1044         Debug( LDAP_DEBUG_TRACE, "==>backsql_process_filter_attr(%s)\n",
1045                 at->bam_ad->ad_cname.bv_val, 0, 0 );
1046
1047         /*
1048          * need to add this attribute to list of attrs to load,
1049          * so that we can do test_filter() later
1050          */
1051         backsql_attrlist_add( bsi, at->bam_ad );
1052
1053         backsql_merge_from_tbls( bsi, &at->bam_from_tbls );
1054
1055         if ( !BER_BVISNULL( &at->bam_join_where )
1056                         && strstr( bsi->bsi_join_where.bb_val.bv_val,
1057                                 at->bam_join_where.bv_val ) == NULL )
1058         {
1059                 backsql_strfcat( &bsi->bsi_join_where, "lb",
1060                                 (ber_len_t)STRLENOF( " AND " ), " AND ",
1061                                 &at->bam_join_where );
1062         }
1063
1064         switch ( f->f_choice ) {
1065         case LDAP_FILTER_EQUALITY:
1066                 filter_value = &f->f_av_value;
1067                 matching_rule = at->bam_ad->ad_type->sat_equality;
1068
1069                 goto equality_match;
1070
1071                 /* fail over into next case */
1072                 
1073         case LDAP_FILTER_EXT:
1074                 filter_value = &f->f_mra->ma_value;
1075                 matching_rule = f->f_mr_rule;
1076
1077 equality_match:;
1078                 /* always uppercase strings by now */
1079 #ifdef BACKSQL_UPPERCASE_FILTER
1080                 if ( SLAP_MR_ASSOCIATED( matching_rule,
1081                                         bi->sql_caseIgnoreMatch ) )
1082 #endif /* BACKSQL_UPPERCASE_FILTER */
1083                 {
1084                         casefold = 1;
1085                 }
1086
1087                 /* FIXME: directoryString filtering should use a similar
1088                  * approach to deal with non-prettified values like
1089                  * " A  non    prettified   value  ", by using a LIKE
1090                  * filter with all whitespaces collapsed to a single '%' */
1091                 if ( SLAP_MR_ASSOCIATED( matching_rule,
1092                                         bi->sql_telephoneNumberMatch ) )
1093                 {
1094                         struct berval   bv;
1095                         ber_len_t       i;
1096
1097                         /*
1098                          * to check for matching telephone numbers
1099                          * with intermized chars, e.g. val='1234'
1100                          * use
1101                          * 
1102                          * val LIKE '%1%2%3%4%'
1103                          */
1104
1105                         bv.bv_len = 2 * filter_value->bv_len - 1;
1106                         bv.bv_val = ch_malloc( bv.bv_len + 1 );
1107
1108                         bv.bv_val[ 0 ] = filter_value->bv_val[ 0 ];
1109                         for ( i = 1; i < filter_value->bv_len; i++ ) {
1110                                 bv.bv_val[ 2 * i - 1 ] = '%';
1111                                 bv.bv_val[ 2 * i ] = filter_value->bv_val[ i ];
1112                         }
1113                         bv.bv_val[ 2 * i - 1 ] = '\0';
1114
1115                         (void)backsql_process_filter_like( bsi, at, casefold, &bv );
1116                         ch_free( bv.bv_val );
1117
1118                         break;
1119                 }
1120
1121                 /* NOTE: this is required by objectClass inheritance 
1122                  * and auxiliary objectClass use in filters for slightly
1123                  * more efficient candidate selection. */
1124                 /* FIXME: a bit too many specializations to deal with
1125                  * very specific cases... */
1126                 if ( at->bam_ad == slap_schema.si_ad_objectClass
1127                                 || at->bam_ad == slap_schema.si_ad_structuralObjectClass )
1128                 {
1129                         backsql_strfcat( &bsi->bsi_flt_where, "lbl",
1130                                         (ber_len_t)STRLENOF( "(ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */ ),
1131                                                 "(ldap_entries.id=ldap_entry_objclasses.entry_id AND ldap_entry_objclasses.oc_name='" /* ') */,
1132                                         filter_value,
1133                                         (ber_len_t)STRLENOF( /* (' */ "')" ),
1134                                                 /* (' */ "')" );
1135                         break;
1136                 }
1137
1138                 /*
1139                  * maybe we should check type of at->sel_expr here somehow,
1140                  * to know whether upper_func is applicable, but for now
1141                  * upper_func stuff is made for Oracle, where UPPER is
1142                  * safely applicable to NUMBER etc.
1143                  */
1144                 (void)backsql_process_filter_eq( bsi, at, casefold, filter_value );
1145                 break;
1146
1147         case LDAP_FILTER_GE:
1148                 ordering.bv_val = ">=";
1149
1150                 /* fall thru to next case */
1151                 
1152         case LDAP_FILTER_LE:
1153                 filter_value = &f->f_av_value;
1154                 
1155                 /* always uppercase strings by now */
1156 #ifdef BACKSQL_UPPERCASE_FILTER
1157                 if ( at->bam_ad->ad_type->sat_ordering &&
1158                                 SLAP_MR_ASSOCIATED( at->bam_ad->ad_type->sat_ordering,
1159                                         bi->sql_caseIgnoreMatch ) )
1160 #endif /* BACKSQL_UPPERCASE_FILTER */
1161                 {
1162                         casefold = 1;
1163                 }
1164
1165                 /*
1166                  * FIXME: should we uppercase the operands?
1167                  */
1168                 if ( casefold && BACKSQL_AT_CANUPPERCASE( at ) ) {
1169                         ber_len_t       start;
1170
1171                         backsql_strfcat( &bsi->bsi_flt_where, "cbbc",
1172                                         '(', /* ) */
1173                                         &at->bam_sel_expr_u, 
1174                                         &ordering,
1175                                         '\'' );
1176
1177                         start = bsi->bsi_flt_where.bb_val.bv_len;
1178
1179                         backsql_strfcat( &bsi->bsi_flt_where, "bl",
1180                                         filter_value, 
1181                                         (ber_len_t)STRLENOF( /* (' */ "')" ),
1182                                                 /* (' */ "')" );
1183
1184                         ldap_pvt_str2upper( &bsi->bsi_flt_where.bb_val.bv_val[ start ] );
1185                 
1186                 } else {
1187                         backsql_strfcat( &bsi->bsi_flt_where, "cbbcbl",
1188                                         '(' /* ) */ ,
1189                                         &at->bam_sel_expr,
1190                                         &ordering,
1191                                         '\'',
1192                                         &f->f_av_value,
1193                                         (ber_len_t)STRLENOF( /* (' */ "')" ),
1194                                                 /* ( */ "')" );
1195                 }
1196                 break;
1197
1198         case LDAP_FILTER_PRESENT:
1199                 backsql_strfcat( &bsi->bsi_flt_where, "lbl",
1200                                 (ber_len_t)STRLENOF( "NOT (" /* ) */),
1201                                         "NOT (", /* ) */
1202                                 &at->bam_sel_expr, 
1203                                 (ber_len_t)STRLENOF( /* ( */ " IS NULL)" ),
1204                                         /* ( */ " IS NULL)" );
1205                 break;
1206
1207         case LDAP_FILTER_SUBSTRINGS:
1208                 backsql_process_sub_filter( bsi, f, at );
1209                 break;
1210
1211         case LDAP_FILTER_APPROX:
1212                 /* we do our best */
1213
1214                 /*
1215                  * maybe we should check type of at->sel_expr here somehow,
1216                  * to know whether upper_func is applicable, but for now
1217                  * upper_func stuff is made for Oracle, where UPPER is
1218                  * safely applicable to NUMBER etc.
1219                  */
1220                 (void)backsql_process_filter_like( bsi, at, 1, &f->f_av_value );
1221                 break;
1222
1223         default:
1224                 /* unhandled filter type; should not happen */
1225                 assert( 0 );
1226                 backsql_strfcat( &bsi->bsi_flt_where, "l",
1227                                 (ber_len_t)STRLENOF( "8=8" ), "8=8" );
1228                 break;
1229
1230         }
1231
1232         Debug( LDAP_DEBUG_TRACE, "<==backsql_process_filter_attr(%s)\n",
1233                 at->bam_ad->ad_cname.bv_val, 0, 0 );
1234
1235         return 1;
1236 }
1237
1238 static int
1239 backsql_srch_query( backsql_srch_info *bsi, struct berval *query )
1240 {
1241         backsql_info    *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1242         int             rc;
1243
1244         assert( query );
1245         BER_BVZERO( query );
1246
1247         bsi->bsi_use_subtree_shortcut = 0;
1248
1249         Debug( LDAP_DEBUG_TRACE, "==>backsql_srch_query()\n", 0, 0, 0 );
1250         BER_BVZERO( &bsi->bsi_sel.bb_val );
1251         BER_BVZERO( &bsi->bsi_sel.bb_val );
1252         bsi->bsi_sel.bb_len = 0;
1253         BER_BVZERO( &bsi->bsi_from.bb_val );
1254         bsi->bsi_from.bb_len = 0;
1255         BER_BVZERO( &bsi->bsi_join_where.bb_val );
1256         bsi->bsi_join_where.bb_len = 0;
1257         BER_BVZERO( &bsi->bsi_flt_where.bb_val );
1258         bsi->bsi_flt_where.bb_len = 0;
1259
1260         backsql_strfcat( &bsi->bsi_sel, "lbcbc",
1261                         (ber_len_t)STRLENOF( "SELECT DISTINCT ldap_entries.id," ),
1262                                 "SELECT DISTINCT ldap_entries.id,", 
1263                         &bsi->bsi_oc->bom_keytbl, 
1264                         '.', 
1265                         &bsi->bsi_oc->bom_keycol, 
1266                         ',' );
1267
1268         if ( !BER_BVISNULL( &bi->sql_strcast_func ) ) {
1269                 backsql_strfcat( &bsi->bsi_sel, "blbl",
1270                                 &bi->sql_strcast_func, 
1271                                 (ber_len_t)STRLENOF( "('" /* ') */ ),
1272                                         "('" /* ') */ ,
1273                                 &bsi->bsi_oc->bom_oc->soc_cname,
1274                                 (ber_len_t)STRLENOF( /* (' */ "')" ),
1275                                         /* (' */ "')" );
1276         } else {
1277                 backsql_strfcat( &bsi->bsi_sel, "cbc",
1278                                 '\'',
1279                                 &bsi->bsi_oc->bom_oc->soc_cname,
1280                                 '\'' );
1281         }
1282 #ifdef BACKSQL_ALIASING_QUOTE
1283         backsql_strfcat( &bsi->bsi_sel, "lclcl",
1284                         (ber_len_t)STRLENOF( " " BACKSQL_ALIASING ),
1285                                 " " BACKSQL_ALIASING,
1286                         BACKSQL_ALIASING_QUOTE,
1287                         (ber_len_t)STRLENOF( "objectClass" ),
1288                                 "objectClass",
1289                         BACKSQL_ALIASING_QUOTE,
1290                         (ber_len_t)STRLENOF( ",ldap_entries.dn " BACKSQL_ALIASING "dn" ),
1291                                 ",ldap_entries.dn " BACKSQL_ALIASING "dn" );
1292 #else /* ! BACKSQL_ALIASING_QUOTE */
1293         backsql_strfcat( &bsi->bsi_sel, "l",
1294                         (ber_len_t)STRLENOF( " " BACKSQL_ALIASING "objectClass,ldap_entries.dn " BACKSQL_ALIASING "dn" ),
1295                                 " " BACKSQL_ALIASING "objectClass,ldap_entries.dn " BACKSQL_ALIASING "dn" );
1296 #endif /* ! BACKSQL_ALIASING_QUOTE */
1297
1298         backsql_strfcat( &bsi->bsi_from, "lb",
1299                         (ber_len_t)STRLENOF( " FROM ldap_entries," ),
1300                                 " FROM ldap_entries,",
1301                         &bsi->bsi_oc->bom_keytbl );
1302
1303         backsql_strfcat( &bsi->bsi_join_where, "lbcbl",
1304                         (ber_len_t)STRLENOF( " WHERE " ), " WHERE ",
1305                         &bsi->bsi_oc->bom_keytbl,
1306                         '.',
1307                         &bsi->bsi_oc->bom_keycol,
1308                         (ber_len_t)STRLENOF( "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " ),
1309                                 "=ldap_entries.keyval AND ldap_entries.oc_map_id=? AND " );
1310
1311         switch ( bsi->bsi_scope ) {
1312         case LDAP_SCOPE_BASE:
1313                 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1314                         backsql_strfcat( &bsi->bsi_join_where, "bl",
1315                                         &bi->sql_upper_func,
1316                                         (ber_len_t)STRLENOF( "(ldap_entries.dn)=?" ),
1317                                                 "(ldap_entries.dn)=?" );
1318                 } else {
1319                         backsql_strfcat( &bsi->bsi_join_where, "l",
1320                                         (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
1321                                                 "ldap_entries.dn=?" );
1322                 }
1323                 break;
1324                 
1325         case BACKSQL_SCOPE_BASE_LIKE:
1326                 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1327                         backsql_strfcat( &bsi->bsi_join_where, "bl",
1328                                         &bi->sql_upper_func,
1329                                         (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ),
1330                                                 "(ldap_entries.dn) LIKE ?" );
1331                 } else {
1332                         backsql_strfcat( &bsi->bsi_join_where, "l",
1333                                         (ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ),
1334                                                 "ldap_entries.dn LIKE ?" );
1335                 }
1336                 break;
1337                 
1338         case LDAP_SCOPE_ONELEVEL:
1339                 backsql_strfcat( &bsi->bsi_join_where, "l",
1340                                 (ber_len_t)STRLENOF( "ldap_entries.parent=?" ),
1341                                         "ldap_entries.parent=?" );
1342                 break;
1343
1344 #ifdef LDAP_SCOPE_SUBORDINATE
1345         case LDAP_SCOPE_SUBORDINATE:
1346 #endif /* LDAP_SCOPE_SUBORDINATE */
1347         case LDAP_SCOPE_SUBTREE:
1348                 if ( BACKSQL_USE_SUBTREE_SHORTCUT( bi ) ) {
1349                         int             i;
1350                         BackendDB       *bd = bsi->bsi_op->o_bd;
1351
1352                         assert( bd->be_nsuffix );
1353
1354                         for ( i = 0; !BER_BVISNULL( &bd->be_nsuffix[ i ] ); i++ )
1355                         {
1356                                 if ( dn_match( &bd->be_nsuffix[ i ],
1357                                                         bsi->bsi_base_ndn ) )
1358                                 {
1359                                         /* pass this to the candidate selection
1360                                          * routine so that the DN is not bound
1361                                          * to the select statement */
1362                                         bsi->bsi_use_subtree_shortcut = 1;
1363                                         break;
1364                                 }
1365                         }
1366                 }
1367
1368                 if ( bsi->bsi_use_subtree_shortcut ) {
1369                         /* Skip the base DN filter, as every entry will match it */
1370                         backsql_strfcat( &bsi->bsi_join_where, "l",
1371                                         (ber_len_t)STRLENOF( "9=9"), "9=9");
1372
1373                 } else if ( !BER_BVISNULL( &bi->sql_subtree_cond ) ) {
1374                         backsql_strfcat( &bsi->bsi_join_where, "b", &bi->sql_subtree_cond );
1375
1376                 } else if ( BACKSQL_CANUPPERCASE( bi ) ) {
1377                         backsql_strfcat( &bsi->bsi_join_where, "bl",
1378                                         &bi->sql_upper_func,
1379                                         (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE ?" ),
1380                                                 "(ldap_entries.dn) LIKE ?"  );
1381
1382                 } else {
1383                         backsql_strfcat( &bsi->bsi_join_where, "l",
1384                                         (ber_len_t)STRLENOF( "ldap_entries.dn LIKE ?" ),
1385                                                 "ldap_entries.dn LIKE ?" );
1386                 }
1387
1388                 break;
1389
1390         default:
1391                 assert( 0 );
1392         }
1393
1394         rc = backsql_process_filter( bsi, bsi->bsi_filter );
1395         if ( rc > 0 ) {
1396                 struct berbuf   bb = BB_NULL;
1397
1398                 backsql_strfcat( &bb, "bbblb",
1399                                 &bsi->bsi_sel.bb_val,
1400                                 &bsi->bsi_from.bb_val, 
1401                                 &bsi->bsi_join_where.bb_val,
1402                                 (ber_len_t)STRLENOF( " AND " ), " AND ",
1403                                 &bsi->bsi_flt_where.bb_val );
1404
1405                 *query = bb.bb_val;
1406
1407         } else if ( rc < 0 ) {
1408                 /* 
1409                  * Indicates that there's no possible way the filter matches
1410                  * anything.  No need to issue the query
1411                  */
1412                 free( query->bv_val );
1413                 BER_BVZERO( query );
1414         }
1415  
1416         free( bsi->bsi_sel.bb_val.bv_val );
1417         BER_BVZERO( &bsi->bsi_sel.bb_val );
1418         bsi->bsi_sel.bb_len = 0;
1419         free( bsi->bsi_from.bb_val.bv_val );
1420         BER_BVZERO( &bsi->bsi_from.bb_val );
1421         bsi->bsi_from.bb_len = 0;
1422         free( bsi->bsi_join_where.bb_val.bv_val );
1423         BER_BVZERO( &bsi->bsi_join_where.bb_val );
1424         bsi->bsi_join_where.bb_len = 0;
1425         free( bsi->bsi_flt_where.bb_val.bv_val );
1426         BER_BVZERO( &bsi->bsi_flt_where.bb_val );
1427         bsi->bsi_flt_where.bb_len = 0;
1428         
1429         Debug( LDAP_DEBUG_TRACE, "<==backsql_srch_query() returns %s\n",
1430                 query->bv_val ? query->bv_val : "NULL", 0, 0 );
1431         
1432         return ( rc <= 0 ? 1 : 0 );
1433 }
1434
1435 static int
1436 backsql_oc_get_candidates( void *v_oc, void *v_bsi )
1437 {
1438         backsql_oc_map_rec      *oc = v_oc;
1439         backsql_srch_info       *bsi = v_bsi;
1440         Operation               *op = bsi->bsi_op;
1441         backsql_info            *bi = (backsql_info *)bsi->bsi_op->o_bd->be_private;
1442         struct berval           query;
1443         SQLHSTMT                sth = SQL_NULL_HSTMT;
1444         RETCODE                 rc;
1445         int                     res;
1446         BACKSQL_ROW_NTS         row;
1447         int                     i;
1448         int                     j;
1449         int                     n_candidates = bsi->bsi_n_candidates;
1450
1451         /* 
1452          * + 1 because we need room for '%';
1453          * + 1 because we need room for ',' for LDAP_SCOPE_SUBORDINATE;
1454          * this makes a subtree
1455          * search for a DN BACKSQL_MAX_DN_LEN long legal 
1456          * if it returns that DN only
1457          */
1458         char                    tmp_base_ndn[ BACKSQL_MAX_DN_LEN + 1 + 1 ];
1459
1460         bsi->bsi_status = LDAP_SUCCESS;
1461  
1462         Debug( LDAP_DEBUG_TRACE, "==>backsql_oc_get_candidates(): oc=\"%s\"\n",
1463                         BACKSQL_OC_NAME( oc ), 0, 0 );
1464
1465         if ( bsi->bsi_n_candidates == -1 ) {
1466                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1467                         "unchecked limit has been overcome\n", 0, 0, 0 );
1468                 /* should never get here */
1469                 assert( 0 );
1470                 bsi->bsi_status = LDAP_ADMINLIMIT_EXCEEDED;
1471                 return BACKSQL_AVL_STOP;
1472         }
1473         
1474         bsi->bsi_oc = oc;
1475         res = backsql_srch_query( bsi, &query );
1476         if ( res ) {
1477                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1478                         "error while constructing query for objectclass \"%s\"\n",
1479                         oc->bom_oc->soc_cname.bv_val, 0, 0 );
1480                 /*
1481                  * FIXME: need to separate errors from legally
1482                  * impossible filters
1483                  */
1484                 switch ( bsi->bsi_status ) {
1485                 case LDAP_SUCCESS:
1486                 case LDAP_UNDEFINED_TYPE:
1487                 case LDAP_NO_SUCH_OBJECT:
1488                         /* we are conservative... */
1489                 default:
1490                         bsi->bsi_status = LDAP_SUCCESS;
1491                         /* try next */
1492                         return BACKSQL_AVL_CONTINUE;
1493
1494                 case LDAP_ADMINLIMIT_EXCEEDED:
1495                 case LDAP_OTHER:
1496                         /* don't try any more */
1497                         return BACKSQL_AVL_STOP;
1498                 }
1499         }
1500
1501         if ( BER_BVISNULL( &query ) ) {
1502                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1503                         "could not construct query for objectclass \"%s\"\n",
1504                         oc->bom_oc->soc_cname.bv_val, 0, 0 );
1505                 bsi->bsi_status = LDAP_SUCCESS;
1506                 return BACKSQL_AVL_CONTINUE;
1507         }
1508
1509         Debug( LDAP_DEBUG_TRACE, "Constructed query: %s\n", 
1510                         query.bv_val, 0, 0 );
1511
1512         rc = backsql_Prepare( bsi->bsi_dbh, &sth, query.bv_val, 0 );
1513         free( query.bv_val );
1514         BER_BVZERO( &query );
1515         if ( rc != SQL_SUCCESS ) {
1516                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1517                         "error preparing query\n", 0, 0, 0 );
1518                 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
1519                 bsi->bsi_status = LDAP_OTHER;
1520                 return BACKSQL_AVL_CONTINUE;
1521         }
1522         
1523         Debug( LDAP_DEBUG_TRACE, "id: '%ld'\n", bsi->bsi_oc->bom_id, 0, 0 );
1524
1525         rc = backsql_BindParamInt( sth, 1, SQL_PARAM_INPUT,
1526                         &bsi->bsi_oc->bom_id );
1527         if ( rc != SQL_SUCCESS ) {
1528                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1529                         "error binding objectclass id parameter\n", 0, 0, 0 );
1530                 bsi->bsi_status = LDAP_OTHER;
1531                 return BACKSQL_AVL_CONTINUE;
1532         }
1533
1534         switch ( bsi->bsi_scope ) {
1535         case LDAP_SCOPE_BASE:
1536         case BACKSQL_SCOPE_BASE_LIKE:
1537                 /*
1538                  * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
1539                  * however this should be handled earlier
1540                  */
1541                 if ( bsi->bsi_base_ndn->bv_len > BACKSQL_MAX_DN_LEN ) {
1542                         bsi->bsi_status = LDAP_OTHER;
1543                         return BACKSQL_AVL_CONTINUE;
1544                 }
1545
1546                 AC_MEMCPY( tmp_base_ndn, bsi->bsi_base_ndn->bv_val,
1547                                 bsi->bsi_base_ndn->bv_len + 1 );
1548
1549                 /* uppercase DN only if the stored DN can be uppercased
1550                  * for comparison */
1551                 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1552                         ldap_pvt_str2upper( tmp_base_ndn );
1553                 }
1554
1555                 Debug( LDAP_DEBUG_TRACE, "(base)dn: \"%s\"\n",
1556                                 tmp_base_ndn, 0, 0 );
1557
1558                 rc = backsql_BindParamStr( sth, 2, SQL_PARAM_INPUT,
1559                                 tmp_base_ndn, BACKSQL_MAX_DN_LEN );
1560                 if ( rc != SQL_SUCCESS ) {
1561                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1562                                 "error binding base_ndn parameter\n", 0, 0, 0 );
1563                         backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, 
1564                                         sth, rc );
1565                         bsi->bsi_status = LDAP_OTHER;
1566                         return BACKSQL_AVL_CONTINUE;
1567                 }
1568                 break;
1569
1570 #ifdef LDAP_SCOPE_SUBORDINATE
1571         case LDAP_SCOPE_SUBORDINATE:
1572 #endif /* LDAP_SCOPE_SUBORDINATE */
1573         case LDAP_SCOPE_SUBTREE:
1574         {
1575                 /* if short-cutting the search base,
1576                  * don't bind any parameter */
1577                 if ( bsi->bsi_use_subtree_shortcut ) {
1578                         break;
1579                 }
1580                 
1581                 /*
1582                  * We do not accept DNs longer than BACKSQL_MAX_DN_LEN;
1583                  * however this should be handled earlier
1584                  */
1585                 if ( bsi->bsi_base_ndn->bv_len > BACKSQL_MAX_DN_LEN ) {
1586                         bsi->bsi_status = LDAP_OTHER;
1587                         return BACKSQL_AVL_CONTINUE;
1588                 }
1589
1590                 /* 
1591                  * Sets the parameters for the SQL built earlier
1592                  * NOTE that all the databases could actually use 
1593                  * the TimesTen version, which would be cleaner 
1594                  * and would also eliminate the need for the
1595                  * subtree_cond line in the configuration file.  
1596                  * For now, I'm leaving it the way it is, 
1597                  * so non-TimesTen databases use the original code.
1598                  * But at some point this should get cleaned up.
1599                  *
1600                  * If "dn" is being used, do a suffix search.
1601                  * If "dn_ru" is being used, do a prefix search.
1602                  */
1603                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
1604                         tmp_base_ndn[ 0 ] = '\0';
1605
1606                         for ( i = 0, j = bsi->bsi_base_ndn->bv_len - 1;
1607                                         j >= 0; i++, j--) {
1608                                 tmp_base_ndn[ i ] = bsi->bsi_base_ndn->bv_val[ j ];
1609                         }
1610
1611 #ifdef LDAP_SCOPE_SUBORDINATE
1612                         if ( bsi->bsi_scope == LDAP_SCOPE_SUBORDINATE ) {
1613                                 tmp_base_ndn[ i++ ] = ',';
1614                         }
1615 #endif /* LDAP_SCOPE_SUBORDINATE */
1616
1617                         tmp_base_ndn[ i ] = '%';
1618                         tmp_base_ndn[ i + 1 ] = '\0';
1619
1620                 } else {
1621                         i = 0;
1622
1623                         tmp_base_ndn[ i++ ] = '%';
1624
1625 #ifdef LDAP_SCOPE_SUBORDINATE
1626                         if ( bsi->bsi_scope == LDAP_SCOPE_SUBORDINATE ) {
1627                                 tmp_base_ndn[ i++ ] = ',';
1628                         }
1629 #endif /* LDAP_SCOPE_SUBORDINATE */
1630
1631                         AC_MEMCPY( &tmp_base_ndn[ i ], bsi->bsi_base_ndn->bv_val,
1632                                 bsi->bsi_base_ndn->bv_len + 1 );
1633                 }
1634
1635                 /* uppercase DN only if the stored DN can be uppercased
1636                  * for comparison */
1637                 if ( BACKSQL_CANUPPERCASE( bi ) ) {
1638                         ldap_pvt_str2upper( tmp_base_ndn );
1639                 }
1640
1641 #ifdef LDAP_SCOPE_SUBORDINATE
1642                 if ( bsi->bsi_scope == LDAP_SCOPE_SUBORDINATE ) {
1643                         Debug( LDAP_DEBUG_TRACE, "(children)dn: \"%s\"\n",
1644                                 tmp_base_ndn, 0, 0 );
1645                 } else 
1646 #endif /* LDAP_SCOPE_SUBORDINATE */
1647                 {
1648                         Debug( LDAP_DEBUG_TRACE, "(sub)dn: \"%s\"\n",
1649                                 tmp_base_ndn, 0, 0 );
1650                 }
1651
1652                 rc = backsql_BindParamStr( sth, 2, SQL_PARAM_INPUT,
1653                                 tmp_base_ndn, BACKSQL_MAX_DN_LEN );
1654                 if ( rc != SQL_SUCCESS ) {
1655                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1656                                 "error binding base_ndn parameter (2)\n",
1657                                 0, 0, 0 );
1658                         backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, 
1659                                         sth, rc );
1660                         bsi->bsi_status = LDAP_OTHER;
1661                         return BACKSQL_AVL_CONTINUE;
1662                 }
1663                 break;
1664         }
1665
1666         case LDAP_SCOPE_ONELEVEL:
1667                 assert( !BER_BVISNULL( &bsi->bsi_base_id.eid_ndn ) );
1668
1669 #ifdef BACKSQL_ARBITRARY_KEY
1670                 Debug( LDAP_DEBUG_TRACE, "(one)id: \"%s\"\n",
1671                                 bsi->bsi_base_id.eid_id.bv_val, 0, 0 );
1672 #else /* ! BACKSQL_ARBITRARY_KEY */
1673                 Debug( LDAP_DEBUG_TRACE, "(one)id: '%lu'\n",
1674                                 bsi->bsi_base_id.eid_id, 0, 0 );
1675 #endif /* ! BACKSQL_ARBITRARY_KEY */
1676                 rc = backsql_BindParamID( sth, 2, SQL_PARAM_INPUT,
1677                                 &bsi->bsi_base_id.eid_id );
1678                 if ( rc != SQL_SUCCESS ) {
1679                         Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1680                                 "error binding base id parameter\n", 0, 0, 0 );
1681                         bsi->bsi_status = LDAP_OTHER;
1682                         return BACKSQL_AVL_CONTINUE;
1683                 }
1684                 break;
1685         }
1686         
1687         rc = SQLExecute( sth );
1688         if ( !BACKSQL_SUCCESS( rc ) ) {
1689                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1690                         "error executing query\n", 0, 0, 0 );
1691                 backsql_PrintErrors( bi->sql_db_env, bsi->bsi_dbh, sth, rc );
1692                 SQLFreeStmt( sth, SQL_DROP );
1693                 bsi->bsi_status = LDAP_OTHER;
1694                 return BACKSQL_AVL_CONTINUE;
1695         }
1696
1697         backsql_BindRowAsStrings( sth, &row );
1698         rc = SQLFetch( sth );
1699         for ( ; BACKSQL_SUCCESS( rc ); rc = SQLFetch( sth ) ) {
1700                 struct berval           dn, pdn, ndn;
1701                 backsql_entryID         *c_id = NULL;
1702                 int                     ret;
1703
1704                 ber_str2bv( row.cols[ 3 ], 0, 0, &dn );
1705
1706                 if ( backsql_api_odbc2dn( bsi->bsi_op, bsi->bsi_rs, &dn ) ) {
1707                         continue;
1708                 }
1709
1710                 ret = dnPrettyNormal( NULL, &dn, &pdn, &ndn, op->o_tmpmemctx );
1711                 if ( dn.bv_val != row.cols[ 3 ] ) {
1712                         free( dn.bv_val );
1713                 }
1714
1715                 if ( ret != LDAP_SUCCESS ) {
1716                         continue;
1717                 }
1718
1719                 if ( bi->sql_baseObject && dn_match( &ndn, &bi->sql_baseObject->e_nname ) ) {
1720                         op->o_tmpfree( pdn.bv_val, op->o_tmpmemctx );
1721                         op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
1722                         continue;
1723                 }
1724
1725                 c_id = (backsql_entryID *)ch_calloc( 1, 
1726                                 sizeof( backsql_entryID ) );
1727 #ifdef BACKSQL_ARBITRARY_KEY
1728                 ber_str2bv_x( row.cols[ 0 ], 0, 1, &c_id->eid_id,
1729                                 op->o_tmpmemctx );
1730                 ber_str2bv_x( row.cols[ 1 ], 0, 1, &c_id->eid_keyval,
1731                                 op->o_tmpmemctx );
1732 #else /* ! BACKSQL_ARBITRARY_KEY */
1733                 c_id->eid_id = strtol( row.cols[ 0 ], NULL, 0 );
1734                 c_id->eid_keyval = strtol( row.cols[ 1 ], NULL, 0 );
1735 #endif /* ! BACKSQL_ARBITRARY_KEY */
1736                 c_id->eid_oc_id = bsi->bsi_oc->bom_id;
1737
1738                 c_id->eid_dn = pdn;
1739                 c_id->eid_ndn = ndn;
1740
1741                 /* append at end of list ... */
1742                 c_id->eid_next = NULL;
1743                 *bsi->bsi_id_listtail = c_id;
1744                 bsi->bsi_id_listtail = &c_id->eid_next;
1745
1746 #ifdef BACKSQL_ARBITRARY_KEY
1747                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1748                         "added entry id=%s, keyval=%s dn=\"%s\"\n",
1749                         c_id->eid_id.bv_val, c_id->eid_keyval.bv_val,
1750                         row.cols[ 3 ] );
1751 #else /* ! BACKSQL_ARBITRARY_KEY */
1752                 Debug( LDAP_DEBUG_TRACE, "backsql_oc_get_candidates(): "
1753                         "added entry id=%ld, keyval=%ld dn=\"%s\"\n",
1754                         c_id->eid_id, c_id->eid_keyval, row.cols[ 3 ] );
1755 #endif /* ! BACKSQL_ARBITRARY_KEY */
1756
1757                 /* count candidates, for unchecked limit */
1758                 bsi->bsi_n_candidates--;
1759                 if ( bsi->bsi_n_candidates == -1 ) {
1760                         break;
1761                 }
1762         }
1763         backsql_FreeRow( &row );
1764         SQLFreeStmt( sth, SQL_DROP );
1765
1766         Debug( LDAP_DEBUG_TRACE, "<==backsql_oc_get_candidates(): %d\n",
1767                         n_candidates - bsi->bsi_n_candidates, 0, 0 );
1768
1769         return ( bsi->bsi_n_candidates == -1 ? BACKSQL_AVL_STOP : BACKSQL_AVL_CONTINUE );
1770 }
1771
1772 int
1773 backsql_search( Operation *op, SlapReply *rs )
1774 {
1775         backsql_info            *bi = (backsql_info *)op->o_bd->be_private;
1776         SQLHDBC                 dbh = SQL_NULL_HDBC;
1777         int                     sres;
1778         Entry                   user_entry = { 0 },
1779                                 base_entry = { 0 };
1780         int                     manageDSAit;
1781         time_t                  stoptime = 0;
1782         backsql_srch_info       bsi;
1783         backsql_entryID         *eid = NULL;
1784         struct berval           nbase = BER_BVNULL,
1785                                 realndn = BER_BVNULL;
1786
1787         manageDSAit = get_manageDSAit( op );
1788
1789         Debug( LDAP_DEBUG_TRACE, "==>backsql_search(): "
1790                 "base=\"%s\", filter=\"%s\", scope=%d,", 
1791                 op->o_req_ndn.bv_val,
1792                 op->ors_filterstr.bv_val ? op->ors_filterstr.bv_val : "(no filter)",
1793                 op->ors_scope );
1794         Debug( LDAP_DEBUG_TRACE, " deref=%d, attrsonly=%d, "
1795                 "attributes to load: %s\n",
1796                 op->ors_deref,
1797                 op->ors_attrsonly,
1798                 op->ors_attrs == NULL ? "all" : "custom list" );
1799
1800         if ( op->o_req_ndn.bv_len > BACKSQL_MAX_DN_LEN ) {
1801                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1802                         "search base length (%ld) exceeds max length (%d)\n", 
1803                         op->o_req_ndn.bv_len, BACKSQL_MAX_DN_LEN, 0 );
1804                 /*
1805                  * FIXME: a LDAP_NO_SUCH_OBJECT could be appropriate
1806                  * since it is impossible that such a long DN exists
1807                  * in the backend
1808                  */
1809                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1810                 send_ldap_result( op, rs );
1811                 return 1;
1812         }
1813
1814         sres = backsql_get_db_conn( op, &dbh );
1815         if ( sres != LDAP_SUCCESS ) {
1816                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
1817                         "could not get connection handle - exiting\n", 
1818                         0, 0, 0 );
1819                 rs->sr_err = sres;
1820                 rs->sr_text = sres == LDAP_OTHER ?  "SQL-backend error" : NULL;
1821                 send_ldap_result( op, rs );
1822                 return 1;
1823         }
1824
1825         /* compute it anyway; root does not use it */
1826         stoptime = op->o_time + op->ors_tlimit;
1827
1828         realndn = op->o_req_ndn;
1829         if ( backsql_api_dn2odbc( op, rs, &realndn ) ) {
1830                 Debug( LDAP_DEBUG_TRACE, "   backsql_search(\"%s\"): "
1831                         "backsql_api_dn2odbc(\"%s\") failed\n", 
1832                         op->o_req_ndn.bv_val, realndn.bv_val, 0 );
1833                 rs->sr_err = LDAP_OTHER;
1834                 rs->sr_text = "SQL-backend error";
1835                 send_ldap_result( op, rs );
1836                 goto done;
1837         }
1838
1839         /* init search */
1840         bsi.bsi_e = &base_entry;
1841         rs->sr_err = backsql_init_search( &bsi, &realndn,
1842                         op->ors_scope,
1843                         op->ors_slimit, op->ors_tlimit,
1844                         stoptime, op->ors_filter,
1845                         dbh, op, rs, op->ors_attrs,
1846                         ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
1847         if ( rs->sr_err != LDAP_SUCCESS ) {
1848 #ifdef SLAP_ACL_HONOR_DISCLOSE
1849                 if ( !BER_BVISNULL( &base_entry.e_nname )
1850                                 && ! access_allowed( op, &base_entry,
1851                                         slap_schema.si_ad_entry, NULL,
1852                                         ACL_DISCLOSE, NULL ) )
1853                 {
1854                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
1855                         if ( rs->sr_ref ) {
1856                                 ber_bvarray_free( rs->sr_ref );
1857                         }
1858                         rs->sr_matched = NULL;
1859                         rs->sr_text = NULL;
1860                 }
1861 #endif /* SLAP_ACL_HONOR_DISCLOSE */
1862                 send_ldap_result( op, rs );
1863                 goto done;
1864
1865         }
1866 #ifdef SLAP_ACL_HONOR_DISCLOSE
1867         /* NOTE: __NEW__ "search" access is required
1868          * on searchBase object */
1869         else {
1870                 slap_mask_t     mask;
1871
1872                 /* FIXME: need the whole entry (ITS#3480) */
1873                 if ( ! access_allowed_mask( op, &base_entry,
1874                                         slap_schema.si_ad_entry,
1875                                         NULL, ACL_SEARCH, NULL, &mask ) )
1876                 {
1877                         if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
1878                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1879                                 rs->sr_text = NULL;
1880
1881                         } else {
1882                                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1883                         }
1884                         send_ldap_result( op, rs );
1885                         goto done;
1886                 }
1887         }
1888 #endif /* SLAP_ACL_HONOR_DISCLOSE */
1889
1890         bsi.bsi_e = NULL;
1891
1892         bsi.bsi_n_candidates =
1893                 ( op->ors_limit == NULL /* isroot == TRUE */ ? -2 : 
1894                 ( op->ors_limit->lms_s_unchecked == -1 ? -2 :
1895                 ( op->ors_limit->lms_s_unchecked ) ) );
1896
1897         switch ( bsi.bsi_scope ) {
1898         case LDAP_SCOPE_BASE:
1899         case BACKSQL_SCOPE_BASE_LIKE:
1900                 /*
1901                  * probably already found...
1902                  */
1903                 bsi.bsi_id_list = &bsi.bsi_base_id;
1904                 bsi.bsi_id_listtail = &bsi.bsi_base_id.eid_next;
1905                 break;
1906
1907         case LDAP_SCOPE_SUBTREE:
1908                 /*
1909                  * if baseObject is defined, and if it is the root 
1910                  * of the search, add it to the candidate list
1911                  */
1912                 if ( bi->sql_baseObject && BACKSQL_IS_BASEOBJECT_ID( &bsi.bsi_base_id.eid_id ) )
1913                 {
1914                         bsi.bsi_id_list = &bsi.bsi_base_id;
1915                         bsi.bsi_id_listtail = &bsi.bsi_base_id.eid_next;
1916                 }
1917
1918                 /* FALLTHRU */
1919         default:
1920
1921                 /*
1922                  * for each objectclass we try to construct query which gets IDs
1923                  * of entries matching LDAP query filter and scope (or at least 
1924                  * candidates), and get the IDs
1925                  */
1926                 avl_apply( bi->sql_oc_by_oc, backsql_oc_get_candidates,
1927                                 &bsi, BACKSQL_AVL_STOP, AVL_INORDER );
1928         }
1929
1930         if ( op->ors_limit != NULL      /* isroot == FALSE */
1931                         && op->ors_limit->lms_s_unchecked != -1
1932                         && bsi.bsi_n_candidates == -1 )
1933         {
1934                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
1935                 send_ldap_result( op, rs );
1936                 goto done;
1937         }
1938
1939         /*
1940          * now we load candidate entries (only those attributes 
1941          * mentioned in attrs and filter), test it against full filter 
1942          * and then send to client; don't free entry_id if baseObject...
1943          */
1944         for ( eid = bsi.bsi_id_list;
1945                         eid != NULL; 
1946                         eid = backsql_free_entryID( op,
1947                                 eid, eid == &bsi.bsi_base_id ? 0 : 1 ) )
1948         {
1949                 int             rc;
1950                 Attribute       *a_hasSubordinate = NULL,
1951                                 *a_entryUUID = NULL,
1952                                 *a_entryCSN = NULL,
1953                                 **ap = NULL;
1954                 Entry           *e = NULL;
1955
1956                 /* check for abandon */
1957                 if ( op->o_abandon ) {
1958                         break;
1959                 }
1960
1961                 /* check time limit */
1962                 if ( op->ors_tlimit != SLAP_NO_LIMIT
1963                                 && slap_get_time() > stoptime )
1964                 {
1965                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
1966                         rs->sr_ctrls = NULL;
1967                         rs->sr_ref = rs->sr_v2ref;
1968                         rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS
1969                                 : LDAP_REFERRAL;
1970                         send_ldap_result( op, rs );
1971                         goto end_of_search;
1972                 }
1973
1974 #ifdef BACKSQL_ARBITRARY_KEY
1975                 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1976                         "for entry id=%s, oc_id=%ld, keyval=%s\n",
1977                         eid->eid_id.bv_val, eid->eid_oc_id,
1978                         eid->eid_keyval.bv_val );
1979 #else /* ! BACKSQL_ARBITRARY_KEY */
1980                 Debug(LDAP_DEBUG_TRACE, "backsql_search(): loading data "
1981                         "for entry id=%ld, oc_id=%ld, keyval=%ld\n",
1982                         eid->eid_id, eid->eid_oc_id, eid->eid_keyval );
1983 #endif /* ! BACKSQL_ARBITRARY_KEY */
1984
1985                 /* check scope */
1986                 switch ( op->ors_scope ) {
1987                 case LDAP_SCOPE_BASE:
1988                 case BACKSQL_SCOPE_BASE_LIKE:
1989                         if ( !dn_match( &eid->eid_ndn, &op->o_req_ndn ) ) {
1990                                 goto next_entry2;
1991                         }
1992                         break;
1993
1994                 case LDAP_SCOPE_ONE:
1995                 {
1996                         struct berval   rdn = eid->eid_ndn;
1997
1998                         rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
1999                         if ( !dnIsOneLevelRDN( &rdn ) ) {
2000                                 goto next_entry2;
2001                         }
2002                         /* fall thru */
2003                 }
2004
2005 #ifdef LDAP_SCOPE_SUBORDINATE
2006                 case LDAP_SCOPE_SUBORDINATE:
2007                         /* discard the baseObject entry */
2008                         if ( dn_match( &eid->eid_ndn, &op->o_req_ndn ) ) {
2009                                 goto next_entry2;
2010                         }
2011                 /* FALLTHRU */
2012 #endif /* LDAP_SCOPE_SUBORDINATE */
2013
2014                 case LDAP_SCOPE_SUBTREE:
2015                         /* FIXME: this should never fail... */
2016                         if ( !dnIsSuffix( &eid->eid_ndn, &op->o_req_ndn ) ) {
2017                                 assert( 0 );
2018                                 goto next_entry2;
2019                         }
2020                         break;
2021                 }
2022
2023                 if ( BACKSQL_IS_BASEOBJECT_ID( &eid->eid_id ) ) {
2024                         /* don't recollect baseObject... */
2025                         e = bi->sql_baseObject;
2026
2027                 } else if ( eid == &bsi.bsi_base_id ) {
2028                         /* don't recollect searchBase object... */
2029                         e = &base_entry;
2030
2031                 } else {
2032                         bsi.bsi_e = &user_entry;
2033                         rc = backsql_id2entry( &bsi, eid );
2034                         if ( rc != LDAP_SUCCESS ) {
2035                                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
2036                                         "error %d in backsql_id2entry() "
2037                                         "- skipping\n", rc, 0, 0 );
2038                                 continue;
2039                         }
2040                         e = &user_entry;
2041                 }
2042
2043                 if ( !manageDSAit &&
2044                                 op->ors_scope != LDAP_SCOPE_BASE &&
2045                                 op->ors_scope != BACKSQL_SCOPE_BASE_LIKE &&
2046                                 is_entry_referral( e ) )
2047                 {
2048                         BerVarray refs;
2049
2050                         refs = get_entry_referrals( op, e );
2051                         if ( !refs ) {
2052                                 backsql_srch_info       bsi2 = { 0 };
2053                                 Entry                   user_entry2 = { 0 };
2054
2055                                 /* retry with the full entry... */
2056                                 bsi2.bsi_e = &user_entry2;
2057                                 rc = backsql_init_search( &bsi2,
2058                                                 &e->e_nname,
2059                                                 LDAP_SCOPE_BASE, 
2060                                                 SLAP_NO_LIMIT, SLAP_NO_LIMIT,
2061                                                 (time_t)(-1), NULL,
2062                                                 dbh, op, rs, NULL,
2063                                                 BACKSQL_ISF_GET_ENTRY );
2064                                 if ( rc == LDAP_SUCCESS ) {
2065                                         if ( is_entry_referral( &user_entry2 ) )
2066                                         {
2067                                                 refs = get_entry_referrals( op,
2068                                                                 &user_entry2 );
2069                                         } else {
2070                                                 rs->sr_err = LDAP_OTHER;
2071                                         }
2072                                         entry_clean( &user_entry2 );
2073                                 }
2074                                 if ( bsi2.bsi_attrs != NULL ) {
2075                                         op->o_tmpfree( bsi2.bsi_attrs,
2076                                                         op->o_tmpmemctx );
2077                                 }
2078                         }
2079
2080                         if ( refs ) {
2081                                 rs->sr_ref = referral_rewrite( refs,
2082                                                 &e->e_name,
2083                                                 &op->o_req_dn,
2084                                                 op->ors_scope );
2085                                 ber_bvarray_free( refs );
2086                         }
2087
2088                         if ( rs->sr_ref ) {
2089                                 rs->sr_err = LDAP_REFERRAL;
2090
2091                         } else {
2092                                 rs->sr_text = "bad referral object";
2093                         }
2094
2095                         rs->sr_entry = e;
2096                         rs->sr_matched = user_entry.e_name.bv_val;
2097                         send_search_reference( op, rs );
2098
2099                         ber_bvarray_free( rs->sr_ref );
2100                         rs->sr_ref = NULL;
2101                         rs->sr_matched = NULL;
2102                         rs->sr_entry = NULL;
2103
2104                         goto next_entry;
2105                 }
2106
2107                 /*
2108                  * We use this flag since we need to parse the filter
2109                  * anyway; we should have used the frontend API function
2110                  * filter_has_subordinates()
2111                  */
2112                 if ( bsi.bsi_flags & BSQL_SF_FILTER_HASSUBORDINATE ) {
2113                         rc = backsql_has_children( bi, dbh, &e->e_nname );
2114
2115                         switch ( rc ) {
2116                         case LDAP_COMPARE_TRUE:
2117                         case LDAP_COMPARE_FALSE:
2118                                 a_hasSubordinate = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
2119                                 if ( a_hasSubordinate != NULL ) {
2120                                         for ( ap = &user_entry.e_attrs; 
2121                                                         *ap; 
2122                                                         ap = &(*ap)->a_next );
2123
2124                                         *ap = a_hasSubordinate;
2125                                 }
2126                                 rc = 0;
2127                                 break;
2128
2129                         default:
2130                                 Debug(LDAP_DEBUG_TRACE, 
2131                                         "backsql_search(): "
2132                                         "has_children failed( %d)\n", 
2133                                         rc, 0, 0 );
2134                                 rc = 1;
2135                                 goto next_entry;
2136                         }
2137                 }
2138
2139                 if ( bsi.bsi_flags & BSQL_SF_FILTER_ENTRYUUID ) {
2140                         a_entryUUID = backsql_operational_entryUUID( bi, eid );
2141                         if ( a_entryUUID != NULL ) {
2142                                 if ( ap == NULL ) {
2143                                         ap = &user_entry.e_attrs;
2144                                 }
2145
2146                                 for ( ; *ap; ap = &(*ap)->a_next );
2147
2148                                 *ap = a_entryUUID;
2149                         }
2150                 }
2151
2152 #ifdef BACKSQL_SYNCPROV
2153                 if ( bsi.bsi_flags & BSQL_SF_FILTER_ENTRYCSN ) {
2154                         a_entryCSN = backsql_operational_entryCSN( op );
2155                         if ( a_entryCSN != NULL ) {
2156                                 if ( ap == NULL ) {
2157                                         ap = &user_entry.e_attrs;
2158                                 }
2159
2160                                 for ( ; *ap; ap = &(*ap)->a_next );
2161
2162                                 *ap = a_entryCSN;
2163                         }
2164                 }
2165 #endif /* BACKSQL_SYNCPROV */
2166
2167                 if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE )
2168                 {
2169                         rs->sr_attrs = op->ors_attrs;
2170                         rs->sr_operational_attrs = NULL;
2171                         rs->sr_entry = e;
2172                         if ( e == &user_entry ) {
2173                                 rs->sr_flags = REP_ENTRY_MODIFIABLE;
2174                         }
2175                         /* FIXME: need the whole entry (ITS#3480) */
2176                         sres = send_search_entry( op, rs );
2177                         rs->sr_entry = NULL;
2178                         rs->sr_attrs = NULL;
2179                         rs->sr_operational_attrs = NULL;
2180
2181                         if ( sres == -1 ) {
2182                                 /*
2183                                  * FIXME: send_search_entry failed;
2184                                  * better stop
2185                                  */
2186                                 Debug( LDAP_DEBUG_TRACE, "backsql_search(): "
2187                                         "connection lost\n", 0, 0, 0 );
2188                                 goto end_of_search;
2189                         }
2190                 }
2191
2192 next_entry:;
2193                 if ( e == &user_entry ) {
2194                         entry_clean( &user_entry );
2195                 }
2196
2197 next_entry2:;
2198                 if ( op->ors_slimit != SLAP_NO_LIMIT
2199                                 && rs->sr_nentries >= op->ors_slimit )
2200                 {
2201                         rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
2202                         send_ldap_result( op, rs );
2203                         goto end_of_search;
2204                 }
2205         }
2206
2207 end_of_search:;
2208         entry_clean( &base_entry );
2209
2210         /* in case we got here accidentally */
2211         entry_clean( &user_entry );
2212
2213         if ( rs->sr_nentries > 0 ) {
2214                 rs->sr_ref = rs->sr_v2ref;
2215                 rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS
2216                         : LDAP_REFERRAL;
2217
2218         } else {
2219                 rs->sr_err = bsi.bsi_status;
2220         }
2221         send_ldap_result( op, rs );
2222
2223         if ( rs->sr_v2ref ) {
2224                 ber_bvarray_free( rs->sr_v2ref );
2225                 rs->sr_v2ref = NULL;
2226         }
2227
2228 #ifdef BACKSQL_SYNCPROV
2229         if ( op->o_sync ) {
2230                 Operation       op2 = *op;
2231                 SlapReply       rs2 = { 0 };
2232                 Entry           e = { 0 };
2233                 slap_callback   cb = { 0 };
2234
2235                 op2.o_tag = LDAP_REQ_ADD;
2236                 op2.o_bd = select_backend( &op->o_bd->be_nsuffix[0], 0, 0 );
2237                 op2.ora_e = &e;
2238                 op2.o_callback = &cb;
2239
2240                 e.e_name = op->o_bd->be_suffix[0];
2241                 e.e_nname = op->o_bd->be_nsuffix[0];
2242
2243                 cb.sc_response = slap_null_cb;
2244
2245                 op2.o_bd->be_add( &op2, &rs2 );
2246         }
2247 #endif /* BACKSQL_SYNCPROV */
2248
2249 done:;
2250         if ( !BER_BVISNULL( &realndn ) && realndn.bv_val != op->o_req_ndn.bv_val ) {
2251                 ch_free( realndn.bv_val );
2252         }
2253
2254         (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
2255
2256         if ( bsi.bsi_attrs != NULL ) {
2257                 op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
2258         }
2259
2260         if ( !BER_BVISNULL( &nbase )
2261                         && nbase.bv_val != op->o_req_ndn.bv_val )
2262         {
2263                 ch_free( nbase.bv_val );
2264         }
2265
2266         /* restore scope ... FIXME: this should be done before ANY
2267          * frontend call that uses op */
2268         if ( op->ors_scope == BACKSQL_SCOPE_BASE_LIKE ) {
2269                 op->ors_scope = LDAP_SCOPE_BASE;
2270         }
2271
2272         Debug( LDAP_DEBUG_TRACE, "<==backsql_search()\n", 0, 0, 0 );
2273         return 0;
2274 }
2275
2276 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
2277  */
2278 int
2279 backsql_entry_get(
2280                 Operation               *op,
2281                 struct berval           *ndn,
2282                 ObjectClass             *oc,
2283                 AttributeDescription    *at,
2284                 int                     rw,
2285                 Entry                   **ent )
2286 {
2287         backsql_srch_info       bsi;
2288         SQLHDBC                 dbh = SQL_NULL_HDBC;
2289         int                     rc;
2290         SlapReply               rs = { 0 };
2291         AttributeName           anlist[ 2 ];
2292
2293         *ent = NULL;
2294
2295         rc = backsql_get_db_conn( op, &dbh );
2296         if ( !dbh ) {
2297                 return LDAP_OTHER;
2298         }
2299
2300         if ( at ) {
2301                 anlist[ 0 ].an_name = at->ad_cname;
2302                 anlist[ 0 ].an_desc = at;
2303                 BER_BVZERO( &anlist[ 1 ].an_name );
2304         }
2305
2306         bsi.bsi_e = ch_malloc( sizeof( Entry ) );
2307         rc = backsql_init_search( &bsi,
2308                         ndn,
2309                         LDAP_SCOPE_BASE, 
2310                         SLAP_NO_LIMIT, SLAP_NO_LIMIT,
2311                         (time_t)(-1), NULL,
2312                         dbh, op, &rs, at ? anlist : NULL,
2313                         BACKSQL_ISF_GET_ENTRY );
2314
2315         (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
2316
2317         if ( rc == LDAP_SUCCESS ) {
2318
2319 #if 0 /* not supported at present */
2320                 /* find attribute values */
2321                 if ( is_entry_alias( bsi.bsi_e ) ) {
2322                         Debug( LDAP_DEBUG_ACL,
2323                                 "<= backsql_entry_get: entry is an alias\n",
2324                                 0, 0, 0 );
2325                         rc = LDAP_ALIAS_PROBLEM;
2326                         goto return_results;
2327                 }
2328 #endif
2329
2330                 if ( is_entry_referral( bsi.bsi_e ) ) {
2331                         Debug( LDAP_DEBUG_ACL,
2332                                 "<= backsql_entry_get: entry is a referral\n",
2333                                 0, 0, 0 );
2334                         rc = LDAP_REFERRAL;
2335                         goto return_results;
2336                 }
2337
2338                 if ( oc && !is_entry_objectclass( bsi.bsi_e, oc, 0 ) ) {
2339                         Debug( LDAP_DEBUG_ACL,
2340                                         "<= backsql_entry_get: "
2341                                         "failed to find objectClass\n",
2342                                         0, 0, 0 ); 
2343                         rc = LDAP_NO_SUCH_ATTRIBUTE;
2344                         goto return_results;
2345                 }
2346
2347                 *ent = bsi.bsi_e;
2348         }
2349
2350 return_results:;
2351         if ( bsi.bsi_attrs != NULL ) {
2352                 op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
2353         }
2354
2355         if ( rc != LDAP_SUCCESS ) {
2356                 if ( bsi.bsi_e ) {
2357                         entry_free( bsi.bsi_e );
2358                 }
2359         }
2360
2361         return rc;
2362 }
2363