]> git.sur5r.net Git - openldap/blob - servers/slapd/filter.c
Suck in HEAD changes since 2.1alpha
[openldap] / servers / slapd / filter.c
1 /* filter.c - routines for parsing and dealing with filters */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16
17 static int      get_filter_list(
18         Connection *conn,
19         BerElement *ber,
20         Filter **f,
21         const char **text );
22
23 static int      get_substring_filter(
24         Connection *conn,
25         BerElement *ber,
26         Filter *f,
27         const char **text );
28
29 static int filter_escape_value(
30         struct berval *in,
31         struct berval *out );
32
33 int
34 get_filter(
35         Connection *conn,
36         BerElement *ber,
37         Filter **filt,
38         const char **text )
39 {
40         ber_tag_t       tag;
41         ber_len_t       len;
42         int             err;
43         Filter          *f;
44
45 #ifdef NEW_LOGGING
46         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY, "get_filter: conn %d\n",
47                 conn->c_connid ));
48 #else
49         Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
50 #endif
51         /*
52          * A filter looks like this coming in:
53          *      Filter ::= CHOICE {
54          *              and             [0]     SET OF Filter,
55          *              or              [1]     SET OF Filter,
56          *              not             [2]     Filter,
57          *              equalityMatch   [3]     AttributeValueAssertion,
58          *              substrings      [4]     SubstringFilter,
59          *              greaterOrEqual  [5]     AttributeValueAssertion,
60          *              lessOrEqual     [6]     AttributeValueAssertion,
61          *              present         [7]     AttributeType,,
62          *              approxMatch     [8]     AttributeValueAssertion
63          *              extensibleMatch [9] MatchingRuleAssertion
64          *      }
65          *
66          *      SubstringFilter ::= SEQUENCE {
67          *              type               AttributeType,
68          *              SEQUENCE OF CHOICE {
69          *                      initial          [0] IA5String,
70          *                      any              [1] IA5String,
71          *                      final            [2] IA5String
72          *              }
73          *      }
74          *
75          *      MatchingRuleAssertion ::= SEQUENCE {
76          *              matchingRule    [1] MatchingRuleId OPTIONAL,
77          *              type            [2] AttributeDescription OPTIONAL,
78          *              matchValue      [3] AssertionValue,
79          *              dnAttributes    [4] BOOLEAN DEFAULT FALSE
80          *      }
81          *
82          */
83
84         tag = ber_peek_tag( ber, &len );
85
86         if( tag == LBER_ERROR ) {
87                 *text = "error decoding filter";
88                 return SLAPD_DISCONNECT;
89         }
90
91         f = (Filter *) ch_malloc( sizeof(Filter) );
92         f->f_next = NULL;
93
94         err = LDAP_SUCCESS;
95         f->f_choice = tag; 
96
97         switch ( f->f_choice ) {
98         case LDAP_FILTER_EQUALITY:
99 #ifdef NEW_LOGGING
100                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL2,
101                         "get_filter: conn %d  EQUALITY\n", conn->c_connid ));
102 #else
103                 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
104 #endif
105                 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY, text );
106                 if ( err != LDAP_SUCCESS ) {
107                         break;
108                 }
109
110                 assert( f->f_ava != NULL );
111                 break;
112
113         case LDAP_FILTER_SUBSTRINGS:
114 #ifdef NEW_LOGGING
115                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
116                         "get_filter: conn %d  SUBSTRINGS\n", conn->c_connid ));
117 #else
118                 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
119 #endif
120                 err = get_substring_filter( conn, ber, f, text );
121                 break;
122
123         case LDAP_FILTER_GE:
124 #ifdef NEW_LOGGING
125                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
126                         "get_filter: conn %d  GE\n", conn->c_connid ));
127 #else
128                 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
129 #endif
130                 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
131                 if ( err != LDAP_SUCCESS ) {
132                         break;
133                 }
134                 break;
135
136         case LDAP_FILTER_LE:
137 #ifdef NEW_LOGGING
138                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
139                         "get_filter: conn %d  LE\n", conn->c_connid ));
140 #else
141                 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
142 #endif
143                 err = get_ava( ber, &f->f_ava, SLAP_MR_ORDERING, text );
144                 if ( err != LDAP_SUCCESS ) {
145                         break;
146                 }
147                 break;
148
149         case LDAP_FILTER_PRESENT: {
150                 struct berval type;
151
152 #ifdef NEW_LOGGING
153                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
154                         "get_filter: conn %d PRESENT\n", conn->c_connid ));
155 #else
156                 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
157 #endif
158                 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
159                         err = SLAPD_DISCONNECT;
160                         *text = "error decoding filter";
161                         break;
162                 }
163
164                 f->f_desc = NULL;
165                 err = slap_bv2ad( &type, &f->f_desc, text );
166
167                 if( err != LDAP_SUCCESS ) {
168                         /* unrecognized attribute description or other error */
169                         f->f_choice = SLAPD_FILTER_COMPUTED;
170                         f->f_result = LDAP_COMPARE_FALSE;
171                         err = LDAP_SUCCESS;
172                         break;
173                 }
174                 } break;
175
176         case LDAP_FILTER_APPROX:
177 #ifdef NEW_LOGGING
178                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
179                         "get_filter: conn %d  APPROX\n", conn->c_connid ));
180 #else
181                 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
182 #endif
183                 err = get_ava( ber, &f->f_ava, SLAP_MR_EQUALITY_APPROX, text );
184                 if ( err != LDAP_SUCCESS ) {
185                         break;
186                 }
187                 break;
188
189         case LDAP_FILTER_AND:
190 #ifdef NEW_LOGGING
191                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
192                         "get_filter: conn %d  AND\n", conn->c_connid ));
193 #else
194                 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
195 #endif
196                 err = get_filter_list( conn, ber, &f->f_and, text );
197                 if ( err != LDAP_SUCCESS ) {
198                         break;
199                 }
200                 break;
201
202         case LDAP_FILTER_OR:
203 #ifdef NEW_LOGGING
204                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
205                         "get_filter: conn %d  OR\n", conn->c_connid  ));
206 #else
207                 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
208 #endif
209                 err = get_filter_list( conn, ber, &f->f_or, text );
210                 if ( err != LDAP_SUCCESS ) {
211                         break;
212                 }
213                 break;
214
215         case LDAP_FILTER_NOT:
216 #ifdef NEW_LOGGING
217                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
218                         "get_filter: conn %d  NOT\n", conn->c_connid ));
219 #else
220                 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
221 #endif
222                 (void) ber_skip_tag( ber, &len );
223                 err = get_filter( conn, ber, &f->f_not, text );
224                 if ( err != LDAP_SUCCESS ) {
225                         break;
226                 }
227                 break;
228
229         case LDAP_FILTER_EXT:
230 #ifdef NEW_LOGGING
231                 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
232                         "get_filter: conn %d  EXTENSIBLE\n", conn->c_connid ));
233 #else
234                 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
235 #endif
236
237                 err = get_mra( ber, &f->f_mra, text );
238                 if ( err != LDAP_SUCCESS ) {
239                         break;
240                 }
241
242                 assert( f->f_mra != NULL );
243                 break;
244
245         default:
246                 (void) ber_scanf( ber, "x" ); /* skip the element */
247 #ifdef NEW_LOGGING
248                 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
249                         "get_filter: conn %d unknown filter type=%lu\n",
250                         conn->c_connid, f->f_choice ));
251 #else
252                 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
253                         f->f_choice, 0, 0 );
254 #endif
255                 f->f_choice = SLAPD_FILTER_COMPUTED;
256                 f->f_result = SLAPD_COMPARE_UNDEFINED;
257                 break;
258         }
259
260         if ( err != LDAP_SUCCESS ) {
261                 if( err != SLAPD_DISCONNECT ) {
262                         /* ignore error */
263                         f->f_choice = SLAPD_FILTER_COMPUTED;
264                         f->f_result = SLAPD_COMPARE_UNDEFINED;
265                         err = LDAP_SUCCESS;
266                         *filt = f;
267
268                 } else {
269                         free(f);
270                 }
271
272         } else {
273                 *filt = f;
274         }
275
276 #ifdef NEW_LOGGING
277         LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL2,
278                 "get_filter: conn %d exit\n", conn->c_connid ));
279 #else
280         Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
281 #endif
282         return( err );
283 }
284
285 static int
286 get_filter_list( Connection *conn, BerElement *ber,
287         Filter **f,
288         const char **text )
289 {
290         Filter          **new;
291         int             err;
292         ber_tag_t       tag;
293         ber_len_t       len;
294         char            *last;
295
296 #ifdef NEW_LOGGING
297         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
298                 "get_filter_list: conn %d start\n", conn->c_connid ));
299 #else
300         Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
301 #endif
302         new = f;
303         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
304                 tag = ber_next_element( ber, &len, last ) )
305         {
306                 err = get_filter( conn, ber, new, text );
307                 if ( err != LDAP_SUCCESS )
308                         return( err );
309                 new = &(*new)->f_next;
310         }
311         *new = NULL;
312
313 #ifdef NEW_LOGGING
314         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
315                 "get_filter_list: conn %d exit\n", conn->c_connid ));
316 #else
317         Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
318 #endif
319         return( LDAP_SUCCESS );
320 }
321
322 static int
323 get_substring_filter(
324         Connection      *conn,
325         BerElement      *ber,
326         Filter  *f,
327         const char      **text )
328 {
329         ber_tag_t       tag;
330         ber_len_t       len;
331         ber_tag_t       rc;
332         struct berval value;
333         char            *last;
334         struct berval bv;
335         *text = "error decoding filter";
336
337 #ifdef NEW_LOGGING
338         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
339                 "get_substring_filter: conn %d  begin\n", conn->c_connid ));
340 #else
341         Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
342 #endif
343         if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
344                 return SLAPD_DISCONNECT;
345         }
346
347         f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
348         f->f_sub_desc = NULL;
349         rc = slap_bv2ad( &bv, &f->f_sub_desc, text );
350
351         if( rc != LDAP_SUCCESS ) {
352                 text = NULL;
353                 ch_free( f->f_sub );
354                 f->f_choice = SLAPD_FILTER_COMPUTED;
355                 f->f_result = SLAPD_COMPARE_UNDEFINED;
356                 return LDAP_SUCCESS;
357         }
358
359         f->f_sub_initial.bv_val = NULL;
360         f->f_sub_any = NULL;
361         f->f_sub_final.bv_val = NULL;
362
363         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
364                 tag = ber_next_element( ber, &len, last ) )
365         {
366                 unsigned usage;
367
368                 rc = ber_scanf( ber, "m", &value );
369                 if ( rc == LBER_ERROR ) {
370                         rc = SLAPD_DISCONNECT;
371                         goto return_error;
372                 }
373
374                 if ( value.bv_val == NULL || value.bv_len == 0 ) {
375                         rc = LDAP_INVALID_SYNTAX;
376                         goto return_error;
377                 } 
378
379                 switch ( tag ) {
380                 case LDAP_SUBSTRING_INITIAL:
381                         usage = SLAP_MR_SUBSTR_INITIAL;
382                         break;
383
384                 case LDAP_SUBSTRING_ANY:
385                         usage = SLAP_MR_SUBSTR_ANY;
386                         break;
387
388                 case LDAP_SUBSTRING_FINAL:
389                         usage = SLAP_MR_SUBSTR_FINAL;
390                         break;
391
392                 default:
393                         rc = LDAP_PROTOCOL_ERROR;
394
395 #ifdef NEW_LOGGING
396                         LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
397                                 "get_filter_substring: conn %d  unknown substring choice=%ld\n",
398                                 conn->c_connid, (long)tag ));
399 #else
400                         Debug( LDAP_DEBUG_FILTER,
401                                 "  unknown substring choice=%ld\n",
402                                 (long) tag, 0, 0 );
403 #endif
404                         goto return_error;
405                 }
406
407                 /* valiate using equality matching rule validator! */
408                 rc = value_validate( f->f_sub_desc->ad_type->sat_equality,
409                         &value, text );
410                 if( rc != LDAP_SUCCESS ) {
411                         goto return_error;
412                 }
413
414                 rc = value_normalize( f->f_sub_desc, usage,
415                         &value, &bv, text );
416                 if( rc != LDAP_SUCCESS ) {
417                         goto return_error;
418                 }
419
420                 value = bv;
421
422                 rc = LDAP_PROTOCOL_ERROR;
423
424                 switch ( tag ) {
425                 case LDAP_SUBSTRING_INITIAL:
426 #ifdef NEW_LOGGING
427                         LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
428                                 "get_substring_filter: conn %d  INITIAL\n",
429                                 conn->c_connid ));
430 #else
431                         Debug( LDAP_DEBUG_FILTER, "  INITIAL\n", 0, 0, 0 );
432 #endif
433
434                         if ( f->f_sub_initial.bv_val != NULL
435                                 || f->f_sub_any != NULL 
436                                 || f->f_sub_final.bv_val != NULL )
437                         {
438                                 free( value.bv_val );
439                                 goto return_error;
440                         }
441
442                         f->f_sub_initial = value;
443                         break;
444
445                 case LDAP_SUBSTRING_ANY:
446 #ifdef NEW_LOGGING
447                         LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
448                                 "get_substring_filter: conn %d  ANY\n",
449                                 conn->c_connid ));
450 #else
451                         Debug( LDAP_DEBUG_FILTER, "  ANY\n", 0, 0, 0 );
452 #endif
453
454                         if ( f->f_sub_final.bv_val != NULL ) {
455                                 free( value.bv_val );
456                                 goto return_error;
457                         }
458
459                         ber_bvarray_add( &f->f_sub_any, &value );
460                         break;
461
462                 case LDAP_SUBSTRING_FINAL:
463 #ifdef NEW_LOGGING
464                         LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
465                                 "get_substring_filter: conn %d  FINAL\n",
466                                 conn->c_connid ));
467 #else
468                         Debug( LDAP_DEBUG_FILTER, "  FINAL\n", 0, 0, 0 );
469 #endif
470
471                         if ( f->f_sub_final.bv_val != NULL ) {
472                                 free( value.bv_val );
473                                 goto return_error;
474                         }
475
476                         f->f_sub_final = value;
477                         break;
478
479                 default:
480 #ifdef NEW_LOGGING
481                         LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
482                                 "get_substring_filter: conn %d  unknown substring type %ld\n",
483                                 conn->c_connid, (long)tag ));
484 #else
485                         Debug( LDAP_DEBUG_FILTER,
486                                 "  unknown substring type=%ld\n",
487                                 (long) tag, 0, 0 );
488 #endif
489
490                         free( value.bv_val );
491
492 return_error:
493 #ifdef NEW_LOGGING
494                         LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
495                                 "get_substring_filter: conn %d  error %ld\n",
496                                 conn->c_connid, (long)rc ));
497 #else
498                         Debug( LDAP_DEBUG_FILTER, "  error=%ld\n",
499                                 (long) rc, 0, 0 );
500 #endif
501                         free( f->f_sub_initial.bv_val );
502                         ber_bvarray_free( f->f_sub_any );
503                         free( f->f_sub_final.bv_val );
504                         ch_free( f->f_sub );
505                         return rc;
506                 }
507         }
508
509 #ifdef NEW_LOGGING
510         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
511                 "get_substring_filter: conn %d exit\n", conn->c_connid ));
512 #else
513         Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
514 #endif
515         return( LDAP_SUCCESS );
516 }
517
518 void
519 filter_free( Filter *f )
520 {
521         Filter  *p, *next;
522
523         if ( f == NULL ) {
524                 return;
525         }
526
527         switch ( f->f_choice ) {
528         case LDAP_FILTER_PRESENT:
529                 break;
530
531         case LDAP_FILTER_EQUALITY:
532         case LDAP_FILTER_GE:
533         case LDAP_FILTER_LE:
534         case LDAP_FILTER_APPROX:
535                 ava_free( f->f_ava, 1 );
536                 break;
537
538         case LDAP_FILTER_SUBSTRINGS:
539                 if ( f->f_sub_initial.bv_val != NULL ) {
540                         free( f->f_sub_initial.bv_val );
541                 }
542                 ber_bvarray_free( f->f_sub_any );
543                 if ( f->f_sub_final.bv_val != NULL ) {
544                         free( f->f_sub_final.bv_val );
545                 }
546                 ch_free( f->f_sub );
547                 break;
548
549         case LDAP_FILTER_AND:
550         case LDAP_FILTER_OR:
551         case LDAP_FILTER_NOT:
552                 for ( p = f->f_list; p != NULL; p = next ) {
553                         next = p->f_next;
554                         filter_free( p );
555                 }
556                 break;
557
558         case LDAP_FILTER_EXT:
559                 mra_free( f->f_mra, 1 );
560                 break;
561
562         case SLAPD_FILTER_COMPUTED:
563                 break;
564
565         default:
566 #ifdef NEW_LOGGING
567                 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
568                         "filter_free: unknown filter type %lu\n", f->f_choice ));
569 #else
570                 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
571                         f->f_choice, 0, 0 );
572 #endif
573                 break;
574         }
575
576         free( f );
577 }
578
579 void
580 filter2bv( Filter *f, struct berval *fstr )
581 {
582         int     i;
583         Filter  *p;
584         struct berval tmp;
585         ber_len_t len;
586
587         if ( f == NULL ) {
588                 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
589                 return;
590         }
591
592         switch ( f->f_choice ) {
593         case LDAP_FILTER_EQUALITY:
594                 filter_escape_value( &f->f_av_value, &tmp );
595
596                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
597                         tmp.bv_len + ( sizeof("(=)") - 1 );
598                 fstr->bv_val = malloc( fstr->bv_len + 1 );
599
600                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
601                         f->f_av_desc->ad_cname.bv_val,
602                         tmp.bv_val );
603
604                 ber_memfree( tmp.bv_val );
605                 break;
606
607         case LDAP_FILTER_GE:
608                 filter_escape_value( &f->f_av_value, &tmp );
609
610                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
611                         tmp.bv_len + ( sizeof("(>=)") - 1 );
612                 fstr->bv_val = malloc( fstr->bv_len + 1 );
613
614                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
615                         f->f_av_desc->ad_cname.bv_val,
616                         tmp.bv_val );
617
618                 ber_memfree( tmp.bv_val );
619                 break;
620
621         case LDAP_FILTER_LE:
622                 filter_escape_value( &f->f_av_value, &tmp );
623
624                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
625                         tmp.bv_len + ( sizeof("(<=)") - 1 );
626                 fstr->bv_val = malloc( fstr->bv_len + 1 );
627
628                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
629                         f->f_av_desc->ad_cname.bv_val,
630                         tmp.bv_val );
631
632                 ber_memfree( tmp.bv_val );
633                 break;
634
635         case LDAP_FILTER_APPROX:
636                 filter_escape_value( &f->f_av_value, &tmp );
637
638                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
639                         tmp.bv_len + ( sizeof("(~=)") - 1 );
640                 fstr->bv_val = malloc( fstr->bv_len + 1 );
641
642                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
643                         f->f_av_desc->ad_cname.bv_val,
644                         tmp.bv_val );
645                 ber_memfree( tmp.bv_val );
646                 break;
647
648         case LDAP_FILTER_SUBSTRINGS:
649                 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
650                         ( sizeof("(=*)") - 1 );
651                 fstr->bv_val = malloc( fstr->bv_len + 128 );
652
653                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
654                         f->f_sub_desc->ad_cname.bv_val );
655
656                 if ( f->f_sub_initial.bv_val != NULL ) {
657                         len = fstr->bv_len;
658
659                         filter_escape_value( &f->f_sub_initial, &tmp );
660
661                         fstr->bv_len += tmp.bv_len;
662                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
663
664                         snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
665                                 /* "(attr=" */ "%s*)",
666                                 tmp.bv_val );
667
668                         ber_memfree( tmp.bv_val );
669                 }
670
671                 if ( f->f_sub_any != NULL ) {
672                         for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
673                                 len = fstr->bv_len;
674                                 filter_escape_value( &f->f_sub_any[i], &tmp );
675
676                                 fstr->bv_len += tmp.bv_len + 1;
677                                 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
678
679                                 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
680                                         /* "(attr=[init]*[any*]" */ "%s*)",
681                                         tmp.bv_val );
682                                 ber_memfree( tmp.bv_val );
683                         }
684                 }
685
686                 if ( f->f_sub_final.bv_val != NULL ) {
687                         len = fstr->bv_len;
688
689                         filter_escape_value( &f->f_sub_final, &tmp );
690
691                         fstr->bv_len += tmp.bv_len;
692                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
693
694                         snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
695                                 /* "(attr=[init*][any*]" */ "%s)",
696                                 tmp.bv_val );
697
698                         ber_memfree( tmp.bv_val );
699                 }
700
701                 break;
702
703         case LDAP_FILTER_PRESENT:
704                 fstr->bv_len = f->f_desc->ad_cname.bv_len +
705                         ( sizeof("(=*)") - 1 );
706                 fstr->bv_val = malloc( fstr->bv_len + 1 );
707
708                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
709                         f->f_desc->ad_cname.bv_val );
710                 break;
711
712         case LDAP_FILTER_AND:
713         case LDAP_FILTER_OR:
714         case LDAP_FILTER_NOT:
715                 fstr->bv_len = sizeof("(%)") - 1;
716                 fstr->bv_val = malloc( fstr->bv_len + 128 );
717
718                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
719                         f->f_choice == LDAP_FILTER_AND ? '&' :
720                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
721
722                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
723                         len = fstr->bv_len;
724
725                         filter2bv( p, &tmp );
726                         
727                         fstr->bv_len += tmp.bv_len;
728                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
729
730                         snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, 
731                                 /*"("*/ "%s)", tmp.bv_val );
732
733                         ch_free( tmp.bv_val );
734                 }
735
736                 break;
737
738         case LDAP_FILTER_EXT:
739                 filter_escape_value( &f->f_mr_value, &tmp );
740
741                 fstr->bv_len = f->f_mr_desc->ad_cname.bv_len +
742                         ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
743                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
744                         tmp.bv_len + ( sizeof("(:=)") - 1 );
745                 fstr->bv_val = malloc( fstr->bv_len + 1 );
746
747                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
748                         f->f_mr_desc->ad_cname.bv_val,
749                         f->f_mr_dnattrs ? ":dn" : "",
750                         f->f_mr_rule_text.bv_len ? ":" : "",
751                         f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
752                         tmp.bv_val );
753                 ber_memfree( tmp.bv_val );
754                 break;
755
756         case SLAPD_FILTER_COMPUTED:
757                 ber_str2bv(
758                         f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
759                         f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
760                         f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
761                         "(?=error)",
762                         f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
763                         f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
764                         f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
765                         sizeof("(?=error)")-1,
766                         1, fstr );
767                 break;
768
769         default:
770                 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
771                 break;
772         }
773 }
774
775 static int filter_escape_value(
776         struct berval *in,
777         struct berval *out )
778 {
779         ber_len_t i;
780         assert( in );
781         assert( out );
782
783         out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
784         out->bv_len = 0;
785
786         for( i=0; i < in->bv_len ; i++ ) {
787                 if( FILTER_ESCAPE(in->bv_val[i]) ) {
788                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
789                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
790                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
791                 } else {
792                         out->bv_val[out->bv_len++] = in->bv_val[i];
793                 }
794         }
795
796         out->bv_val[out->bv_len] = '\0';
797         return LDAP_SUCCESS;
798 }