]> git.sur5r.net Git - openldap/blob - servers/slapd/filter.c
Replace get_filter generation of fstr with filter2bv().
[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
244                 break;
245
246         default:
247                 (void) ber_scanf( ber, "x" ); /* skip the element */
248 #ifdef NEW_LOGGING
249                 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
250                         "get_filter: conn %d unknown filter type=%lu\n",
251                         conn->c_connid, f->f_choice ));
252 #else
253                 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
254                         f->f_choice, 0, 0 );
255 #endif
256                 f->f_choice = SLAPD_FILTER_COMPUTED;
257                 f->f_result = SLAPD_COMPARE_UNDEFINED;
258                 break;
259         }
260
261         if ( err != LDAP_SUCCESS ) {
262                 if( err != SLAPD_DISCONNECT ) {
263                         /* ignore error */
264                         f->f_choice = SLAPD_FILTER_COMPUTED;
265                         f->f_result = SLAPD_COMPARE_UNDEFINED;
266                         err = LDAP_SUCCESS;
267                         *filt = f;
268
269                 } else {
270                         free(f);
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         struct berval escaped;
334         char            *last;
335         struct berval bv;
336         *text = "error decoding filter";
337
338 #ifdef NEW_LOGGING
339         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
340                 "get_substring_filter: conn %d  begin\n", conn->c_connid ));
341 #else
342         Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 );
343 #endif
344         if ( ber_scanf( ber, "{m" /*}*/, &bv ) == LBER_ERROR ) {
345                 return SLAPD_DISCONNECT;
346         }
347
348         f->f_sub = ch_calloc( 1, sizeof(SubstringsAssertion) );
349         f->f_sub_desc = NULL;
350         rc = slap_bv2ad( &bv, &f->f_sub_desc, text );
351
352         if( rc != LDAP_SUCCESS ) {
353                 text = NULL;
354                 ch_free( f->f_sub );
355                 f->f_choice = SLAPD_FILTER_COMPUTED;
356                 f->f_result = SLAPD_COMPARE_UNDEFINED;
357                 return LDAP_SUCCESS;
358         }
359
360         f->f_sub_initial.bv_val = NULL;
361         f->f_sub_any = NULL;
362         f->f_sub_final.bv_val = NULL;
363
364         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
365                 tag = ber_next_element( ber, &len, last ) )
366         {
367                 unsigned usage;
368
369                 rc = ber_scanf( ber, "m", &value );
370                 if ( rc == LBER_ERROR ) {
371                         rc = SLAPD_DISCONNECT;
372                         goto return_error;
373                 }
374
375                 if ( value.bv_val == NULL || value.bv_len == 0 ) {
376                         rc = LDAP_INVALID_SYNTAX;
377                         goto return_error;
378                 } 
379
380                 switch ( tag ) {
381                 case LDAP_SUBSTRING_INITIAL:
382                         usage = SLAP_MR_SUBSTR_INITIAL;
383                         break;
384
385                 case LDAP_SUBSTRING_ANY:
386                         usage = SLAP_MR_SUBSTR_ANY;
387                         break;
388
389                 case LDAP_SUBSTRING_FINAL:
390                         usage = SLAP_MR_SUBSTR_FINAL;
391                         break;
392
393                 default:
394                         rc = LDAP_PROTOCOL_ERROR;
395
396 #ifdef NEW_LOGGING
397                         LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
398                                 "get_filter_substring: conn %d  unknown substring choice=%ld\n",
399                                 conn->c_connid, (long)tag ));
400 #else
401                         Debug( LDAP_DEBUG_FILTER,
402                                 "  unknown substring choice=%ld\n",
403                                 (long) tag, 0, 0 );
404 #endif
405                         goto return_error;
406                 }
407
408                 /* valiate using equality matching rule validator! */
409                 rc = value_validate( f->f_sub_desc->ad_type->sat_equality,
410                         &value, text );
411                 if( rc != LDAP_SUCCESS ) {
412                         goto return_error;
413                 }
414
415                 rc = value_normalize( f->f_sub_desc, usage,
416                         &value, &bv, text );
417                 if( rc != LDAP_SUCCESS ) {
418                         goto return_error;
419                 }
420
421                 value = bv;
422
423                 rc = LDAP_PROTOCOL_ERROR;
424
425                 switch ( tag ) {
426                 case LDAP_SUBSTRING_INITIAL:
427 #ifdef NEW_LOGGING
428                         LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
429                                 "get_substring_filter: conn %d  INITIAL\n",
430                                 conn->c_connid ));
431 #else
432                         Debug( LDAP_DEBUG_FILTER, "  INITIAL\n", 0, 0, 0 );
433 #endif
434
435                         if ( f->f_sub_initial.bv_val != NULL
436                                 || f->f_sub_any != NULL 
437                                 || f->f_sub_final.bv_val != NULL )
438                         {
439                                 free( value.bv_val );
440                                 goto return_error;
441                         }
442
443                         f->f_sub_initial = value;
444                         break;
445
446                 case LDAP_SUBSTRING_ANY:
447 #ifdef NEW_LOGGING
448                         LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
449                                 "get_substring_filter: conn %d  ANY\n",
450                                 conn->c_connid ));
451 #else
452                         Debug( LDAP_DEBUG_FILTER, "  ANY\n", 0, 0, 0 );
453 #endif
454
455                         if ( f->f_sub_final.bv_val != NULL ) {
456                                 free( value.bv_val );
457                                 goto return_error;
458                         }
459
460                         ber_bvarray_add( &f->f_sub_any, &value );
461                         break;
462
463                 case LDAP_SUBSTRING_FINAL:
464 #ifdef NEW_LOGGING
465                         LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
466                                 "get_substring_filter: conn %d  FINAL\n",
467                                 conn->c_connid ));
468 #else
469                         Debug( LDAP_DEBUG_FILTER, "  FINAL\n", 0, 0, 0 );
470 #endif
471
472                         if ( f->f_sub_final.bv_val != NULL ) {
473                                 free( value.bv_val );
474                                 goto return_error;
475                         }
476
477                         f->f_sub_final = value;
478                         break;
479
480                 default:
481 #ifdef NEW_LOGGING
482                         LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
483                                 "get_substring_filter: conn %d  unknown substring type %ld\n",
484                                 conn->c_connid, (long)tag ));
485 #else
486                         Debug( LDAP_DEBUG_FILTER,
487                                 "  unknown substring type=%ld\n",
488                                 (long) tag, 0, 0 );
489 #endif
490
491                         free( value.bv_val );
492
493 return_error:
494 #ifdef NEW_LOGGING
495                         LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
496                                 "get_substring_filter: conn %d  error %ld\n",
497                                 conn->c_connid, (long)rc ));
498 #else
499                         Debug( LDAP_DEBUG_FILTER, "  error=%ld\n",
500                                 (long) rc, 0, 0 );
501 #endif
502                         free( f->f_sub_initial.bv_val );
503                         ber_bvarray_free( f->f_sub_any );
504                         free( f->f_sub_final.bv_val );
505                         ch_free( f->f_sub );
506                         return rc;
507                 }
508         }
509
510 #ifdef NEW_LOGGING
511         LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
512                 "get_substring_filter: conn %d exit\n", conn->c_connid ));
513 #else
514         Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 );
515 #endif
516         return( LDAP_SUCCESS );
517 }
518
519 void
520 filter_free( Filter *f )
521 {
522         Filter  *p, *next;
523
524         if ( f == NULL ) {
525                 return;
526         }
527
528         switch ( f->f_choice ) {
529         case LDAP_FILTER_PRESENT:
530                 break;
531
532         case LDAP_FILTER_EQUALITY:
533         case LDAP_FILTER_GE:
534         case LDAP_FILTER_LE:
535         case LDAP_FILTER_APPROX:
536                 ava_free( f->f_ava, 1 );
537                 break;
538
539         case LDAP_FILTER_SUBSTRINGS:
540                 if ( f->f_sub_initial.bv_val != NULL ) {
541                         free( f->f_sub_initial.bv_val );
542                 }
543                 ber_bvarray_free( f->f_sub_any );
544                 if ( f->f_sub_final.bv_val != NULL ) {
545                         free( f->f_sub_final.bv_val );
546                 }
547                 ch_free( f->f_sub );
548                 break;
549
550         case LDAP_FILTER_AND:
551         case LDAP_FILTER_OR:
552         case LDAP_FILTER_NOT:
553                 for ( p = f->f_list; p != NULL; p = next ) {
554                         next = p->f_next;
555                         filter_free( p );
556                 }
557                 break;
558
559         case LDAP_FILTER_EXT:
560                 mra_free( f->f_mra, 1 );
561                 break;
562
563         case SLAPD_FILTER_COMPUTED:
564                 break;
565
566         default:
567 #ifdef NEW_LOGGING
568                 LDAP_LOG(( "filter", LDAP_LEVEL_ERR,
569                         "filter_free: unknown filter type %lu\n", f->f_choice ));
570 #else
571                 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
572                         f->f_choice, 0, 0 );
573 #endif
574                 break;
575         }
576
577         free( f );
578 }
579
580 #ifdef LDAP_DEBUG
581 void
582 filter_print( Filter *f )
583 {
584         int     i;
585         Filter  *p;
586         struct berval escaped;
587
588         if ( f == NULL ) {
589                 fprintf( stderr, "No filter!" );
590         }
591
592         switch ( f->f_choice ) {
593         case LDAP_FILTER_EQUALITY:
594                 filter_escape_value( &f->f_av_value, &escaped );
595                 fprintf( stderr, "(%s=%s)",
596                         f->f_av_desc->ad_cname.bv_val,
597                         escaped.bv_val );
598                 ber_memfree( escaped.bv_val );
599                 break;
600
601         case LDAP_FILTER_GE:
602                 filter_escape_value( &f->f_av_value, &escaped );
603                 fprintf( stderr, "(%s>=%s)",
604                         f->f_av_desc->ad_cname.bv_val,
605                         escaped.bv_val );
606                 ber_memfree( escaped.bv_val );
607                 break;
608
609         case LDAP_FILTER_LE:
610                 filter_escape_value( &f->f_av_value, &escaped );
611                 fprintf( stderr, "(%s<=%s)",
612                         f->f_ava->aa_desc->ad_cname.bv_val,
613                         escaped.bv_val );
614                 ber_memfree( escaped.bv_val );
615                 break;
616
617         case LDAP_FILTER_APPROX:
618                 filter_escape_value( &f->f_av_value, &escaped );
619                 fprintf( stderr, "(%s~=%s)",
620                         f->f_ava->aa_desc->ad_cname.bv_val,
621                         escaped.bv_val );
622                 ber_memfree( escaped.bv_val );
623                 break;
624
625         case LDAP_FILTER_SUBSTRINGS:
626                 fprintf( stderr, "(%s=" /*)*/,
627                         f->f_sub_desc->ad_cname.bv_val );
628                 if ( f->f_sub_initial.bv_val != NULL ) {
629                         filter_escape_value( &f->f_sub_initial, &escaped );
630                         fprintf( stderr, "%s",
631                                 escaped.bv_val );
632                         ber_memfree( escaped.bv_val );
633                 }
634                 if ( f->f_sub_any != NULL ) {
635                         for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
636                                 filter_escape_value( &f->f_sub_any[i], &escaped );
637                                 fprintf( stderr, "*%s",
638                                         escaped.bv_val );
639                                 ber_memfree( escaped.bv_val );
640                         }
641                 }
642                 if ( f->f_sub_final.bv_val != NULL ) {
643                         filter_escape_value( &f->f_sub_final, &escaped );
644                         fprintf( stderr,
645                                 "*%s", escaped.bv_val );
646                         ber_memfree( escaped.bv_val );
647                 }
648                 fprintf( stderr, /*(*/ ")" );
649                 break;
650
651         case LDAP_FILTER_PRESENT:
652                 fprintf( stderr, "(%s=*)",
653                         f->f_desc->ad_cname.bv_val );
654                 break;
655
656         case LDAP_FILTER_AND:
657         case LDAP_FILTER_OR:
658         case LDAP_FILTER_NOT:
659                 fprintf( stderr, "(%c" /*)*/,
660                         f->f_choice == LDAP_FILTER_AND ? '&' :
661                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
662                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
663                         filter_print( p );
664                 }
665                 fprintf( stderr, /*(*/ ")" );
666                 break;
667
668         case SLAPD_FILTER_COMPUTED:
669                 fprintf( stderr, "(?=%s)",
670                         f->f_result == LDAP_COMPARE_FALSE ? "false" :
671                         f->f_result == LDAP_COMPARE_TRUE ? "true" :
672                         f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
673                         "error" );
674                 break;
675
676         default:
677                 fprintf( stderr, "(unknown-filter=%lu)", f->f_choice );
678                 break;
679         }
680 }
681 #endif /* ldap_debug */
682
683 void
684 filter2bv( Filter *f, struct berval *fstr )
685 {
686         int     i;
687         Filter  *p;
688         struct berval tmp;
689         ber_len_t len;
690
691         if ( f == NULL ) {
692                 ber_str2bv( "No filter!", sizeof("No filter!")-1, 1, fstr );
693                 return;
694         }
695
696         switch ( f->f_choice ) {
697         case LDAP_FILTER_EQUALITY:
698                 filter_escape_value( &f->f_av_value, &tmp );
699
700                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
701                         tmp.bv_len + ( sizeof("(=)") - 1 );
702                 fstr->bv_val = malloc( fstr->bv_len + 1 );
703
704                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
705                         f->f_av_desc->ad_cname.bv_val,
706                         tmp.bv_val );
707
708                 ber_memfree( tmp.bv_val );
709                 break;
710
711         case LDAP_FILTER_GE:
712                 filter_escape_value( &f->f_av_value, &tmp );
713
714                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
715                         tmp.bv_len + ( sizeof("(>=)") - 1 );
716                 fstr->bv_val = malloc( fstr->bv_len + 1 );
717
718                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
719                         f->f_av_desc->ad_cname.bv_val,
720                         tmp.bv_val );
721
722                 ber_memfree( tmp.bv_val );
723                 break;
724
725         case LDAP_FILTER_LE:
726                 filter_escape_value( &f->f_av_value, &tmp );
727
728                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
729                         tmp.bv_len + ( sizeof("(<=)") - 1 );
730                 fstr->bv_val = malloc( fstr->bv_len + 1 );
731
732                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
733                         f->f_av_desc->ad_cname.bv_val,
734                         tmp.bv_val );
735
736                 ber_memfree( tmp.bv_val );
737                 break;
738
739         case LDAP_FILTER_APPROX:
740                 filter_escape_value( &f->f_av_value, &tmp );
741
742                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
743                         tmp.bv_len + ( sizeof("(~=)") - 1 );
744                 fstr->bv_val = malloc( fstr->bv_len + 1 );
745
746                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
747                         f->f_av_desc->ad_cname.bv_val,
748                         tmp.bv_val );
749                 ber_memfree( tmp.bv_val );
750                 break;
751
752         case LDAP_FILTER_SUBSTRINGS:
753                 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
754                         ( sizeof("(=*)") - 1 );
755                 fstr->bv_val = malloc( fstr->bv_len + 128 );
756
757                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
758                         f->f_sub_desc->ad_cname.bv_val );
759
760                 if ( f->f_sub_initial.bv_val != NULL ) {
761                         len = fstr->bv_len;
762
763                         filter_escape_value( &f->f_sub_initial, &tmp );
764
765                         fstr->bv_len += tmp.bv_len;
766                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
767
768                         snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
769                                 /* "(attr=" */ "%s*)",
770                                 tmp.bv_val );
771
772                         ber_memfree( tmp.bv_val );
773                 }
774
775                 if ( f->f_sub_any != NULL ) {
776                         for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
777                                 len = fstr->bv_len;
778                                 filter_escape_value( &f->f_sub_any[i], &tmp );
779
780                                 fstr->bv_len += tmp.bv_len + 1;
781                                 fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
782
783                                 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
784                                         /* "(attr=[init]*[any*]" */ "%s*)",
785                                         tmp.bv_val );
786                                 ber_memfree( tmp.bv_val );
787                         }
788                 }
789
790                 if ( f->f_sub_final.bv_val != NULL ) {
791                         len = fstr->bv_len;
792
793                         filter_escape_value( &f->f_sub_final, &tmp );
794
795                         fstr->bv_len += tmp.bv_len;
796                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
797
798                         snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
799                                 /* "(attr=[init*][any*]" */ "%s)",
800                                 tmp.bv_val );
801
802                         ber_memfree( tmp.bv_val );
803                 }
804
805                 break;
806
807         case LDAP_FILTER_PRESENT:
808                 fstr->bv_len = f->f_desc->ad_cname.bv_len +
809                         ( sizeof("(=*)") - 1 );
810                 fstr->bv_val = malloc( fstr->bv_len + 1 );
811
812                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
813                         f->f_desc->ad_cname.bv_val );
814                 break;
815
816         case LDAP_FILTER_AND:
817         case LDAP_FILTER_OR:
818         case LDAP_FILTER_NOT:
819                 fstr->bv_len = sizeof("(%)") - 1;
820                 fstr->bv_val = malloc( fstr->bv_len + 128 );
821
822                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
823                         f->f_choice == LDAP_FILTER_AND ? '&' :
824                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
825
826                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
827                         len = fstr->bv_len;
828
829                         filter2bv( p, &tmp );
830                         
831                         fstr->bv_len += tmp.bv_len;
832                         fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 1 );
833
834                         snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, 
835                                 /*"("*/ "%s)", tmp.bv_val );
836
837                         ch_free( tmp.bv_val );
838                 }
839
840                 break;
841
842         case SLAPD_FILTER_COMPUTED:
843                 ber_str2bv(
844                         f->f_result == LDAP_COMPARE_FALSE ? "(?=false)" :
845                         f->f_result == LDAP_COMPARE_TRUE ? "(?=true)" :
846                         f->f_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
847                         "(?=error)",
848                         f->f_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
849                         f->f_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
850                         f->f_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
851                         sizeof("(?=error)")-1,
852                         1, fstr );
853                 break;
854
855         default:
856                 ber_str2bv( "(?=unknown)", sizeof("(?=unknown)")-1, 1, fstr );
857                 break;
858         }
859 }
860
861 static int filter_escape_value(
862         struct berval *in,
863         struct berval *out )
864 {
865         ber_len_t i;
866         assert( in );
867         assert( out );
868
869         out->bv_val = (char *) ch_malloc( ( in->bv_len * 3 ) + 1 );
870         out->bv_len = 0;
871
872         for( i=0; i < in->bv_len ; i++ ) {
873                 if( FILTER_ESCAPE(in->bv_val[i]) ) {
874                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
875                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
876                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
877                 } else {
878                         out->bv_val[out->bv_len++] = in->bv_val[i];
879                 }
880         }
881
882         out->bv_val[out->bv_len] = '\0';
883         return LDAP_SUCCESS;
884 }