]> git.sur5r.net Git - openldap/blob - servers/slapd/filter.c
fix the problem right now
[openldap] / servers / slapd / filter.c
1 /* filter.c - routines for parsing and dealing with filters */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/socket.h>
32 #include <ac/string.h>
33
34 #include "slap.h"
35
36 static int      get_filter_list(
37         Operation *op,
38         BerElement *ber,
39         Filter **f,
40         const char **text );
41
42 static int      get_ssa(
43         Operation *op,
44         BerElement *ber,
45         SubstringsAssertion **s,
46         const char **text );
47
48 static int filter_escape_value_x(
49         struct berval *in,
50         struct berval *out,
51         void *ctx );
52
53 static void simple_vrFilter2bv(
54         Operation *op,
55         ValuesReturnFilter *f,
56         struct berval *fstr );
57
58 static int      get_simple_vrFilter(
59         Operation *op,
60         BerElement *ber,
61         ValuesReturnFilter **f,
62         const char **text );
63
64 int
65 get_filter(
66         Operation *op,
67         BerElement *ber,
68         Filter **filt,
69         const char **text )
70 {
71         ber_tag_t       tag;
72         ber_len_t       len;
73         int             err;
74         Filter          f;
75
76 #ifdef NEW_LOGGING
77         LDAP_LOG( FILTER, ENTRY, "get_filter: conn %d\n", op->o_connid, 0, 0 );
78 #else
79         Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
80 #endif
81         /*
82          * A filter looks like this coming in:
83          *      Filter ::= CHOICE {
84          *              and             [0]     SET OF Filter,
85          *              or              [1]     SET OF Filter,
86          *              not             [2]     Filter,
87          *              equalityMatch   [3]     AttributeValueAssertion,
88          *              substrings      [4]     SubstringFilter,
89          *              greaterOrEqual  [5]     AttributeValueAssertion,
90          *              lessOrEqual     [6]     AttributeValueAssertion,
91          *              present         [7]     AttributeType,,
92          *              approxMatch     [8]     AttributeValueAssertion
93          *              extensibleMatch [9]     MatchingRuleAssertion
94          *      }
95          *
96          *      SubstringFilter ::= SEQUENCE {
97          *              type               AttributeType,
98          *              SEQUENCE OF CHOICE {
99          *                      initial          [0] IA5String,
100          *                      any              [1] IA5String,
101          *                      final            [2] IA5String
102          *              }
103          *      }
104          *
105          *      MatchingRuleAssertion ::= SEQUENCE {
106          *              matchingRule    [1] MatchingRuleId OPTIONAL,
107          *              type            [2] AttributeDescription OPTIONAL,
108          *              matchValue      [3] AssertionValue,
109          *              dnAttributes    [4] BOOLEAN DEFAULT FALSE
110          *      }
111          *
112          */
113
114         tag = ber_peek_tag( ber, &len );
115
116         if( tag == LBER_ERROR ) {
117                 *text = "error decoding filter";
118                 return SLAPD_DISCONNECT;
119         }
120
121         err = LDAP_SUCCESS;
122
123         f.f_next = NULL;
124         f.f_choice = tag; 
125
126         switch ( f.f_choice ) {
127         case LDAP_FILTER_EQUALITY:
128 #ifdef NEW_LOGGING
129                 LDAP_LOG( FILTER, DETAIL2, 
130                         "get_filter: conn %d  EQUALITY\n", op->o_connid, 0, 0 );
131 #else
132                 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
133 #endif
134                 err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY, text );
135                 if ( err != LDAP_SUCCESS ) {
136                         break;
137                 }
138
139                 assert( f.f_ava != NULL );
140                 break;
141
142         case LDAP_FILTER_SUBSTRINGS:
143 #ifdef NEW_LOGGING
144                 LDAP_LOG( FILTER, DETAIL1, 
145                         "get_filter: conn %d  SUBSTRINGS\n", op->o_connid, 0, 0 );
146 #else
147                 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
148 #endif
149                 err = get_ssa( op, ber, &f.f_sub, text );
150                 if( err != LDAP_SUCCESS ) {
151                         break;
152                 }
153                 assert( f.f_sub != NULL );
154                 break;
155
156         case LDAP_FILTER_GE:
157 #ifdef NEW_LOGGING
158                 LDAP_LOG( FILTER, DETAIL1, 
159                         "get_filter: conn %d  GE\n", op->o_connid, 0, 0 );
160 #else
161                 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
162 #endif
163                 err = get_ava( op, ber, &f.f_ava, SLAP_MR_ORDERING, text );
164                 if ( err != LDAP_SUCCESS ) {
165                         break;
166                 }
167                 assert( f.f_ava != NULL );
168                 break;
169
170         case LDAP_FILTER_LE:
171 #ifdef NEW_LOGGING
172                 LDAP_LOG( FILTER, DETAIL1, 
173                         "get_filter: conn %d  LE\n", op->o_connid, 0, 0 );
174 #else
175                 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
176 #endif
177                 err = get_ava( op, ber, &f.f_ava, SLAP_MR_ORDERING, text );
178                 if ( err != LDAP_SUCCESS ) {
179                         break;
180                 }
181                 assert( f.f_ava != NULL );
182                 break;
183
184         case LDAP_FILTER_PRESENT: {
185                 struct berval type;
186
187 #ifdef NEW_LOGGING
188                 LDAP_LOG( FILTER, DETAIL1, 
189                         "get_filter: conn %d PRESENT\n", op->o_connid, 0, 0 );
190 #else
191                 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
192 #endif
193                 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
194                         err = SLAPD_DISCONNECT;
195                         *text = "error decoding filter";
196                         break;
197                 }
198
199                 f.f_desc = NULL;
200                 err = slap_bv2ad( &type, &f.f_desc, text );
201
202                 if( err != LDAP_SUCCESS ) {
203                         /* unrecognized attribute description or other error */
204 #ifdef NEW_LOGGING
205                         LDAP_LOG( FILTER, ERR, 
206                                 "get_filter: conn %d unknown attribute "
207                                 "type=%s (%d)\n",
208                                 op->o_connid, type.bv_val, err );
209 #else
210                         Debug( LDAP_DEBUG_ANY, 
211                                 "get_filter: conn %d unknown attribute "
212                                 "type=%s (%d)\n",
213                                 op->o_connid, type.bv_val, err );
214 #endif
215
216                         f.f_choice = SLAPD_FILTER_COMPUTED;
217                         f.f_result = LDAP_COMPARE_FALSE;
218                         err = LDAP_SUCCESS;
219                         *text = NULL;
220                         break;
221                 }
222
223                 assert( f.f_desc != NULL );
224                 } break;
225
226         case LDAP_FILTER_APPROX:
227 #ifdef NEW_LOGGING
228                 LDAP_LOG( FILTER, DETAIL1, 
229                         "get_filter: conn %d  APPROX\n", op->o_connid, 0, 0 );
230 #else
231                 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
232 #endif
233                 err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY_APPROX, text );
234                 if ( err != LDAP_SUCCESS ) {
235                         break;
236                 }
237                 assert( f.f_ava != NULL );
238                 break;
239
240         case LDAP_FILTER_AND:
241 #ifdef NEW_LOGGING
242                 LDAP_LOG( FILTER, DETAIL1, 
243                         "get_filter: conn %d  AND\n", op->o_connid, 0, 0 );
244 #else
245                 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
246 #endif
247                 err = get_filter_list( op, ber, &f.f_and, text );
248                 if ( err != LDAP_SUCCESS ) {
249                         break;
250                 }
251                 if ( f.f_and == NULL ) {
252                         f.f_choice = SLAPD_FILTER_COMPUTED;
253                         f.f_result = LDAP_COMPARE_TRUE;
254                 }
255                 /* no assert - list could be empty */
256                 break;
257
258         case LDAP_FILTER_OR:
259 #ifdef NEW_LOGGING
260                 LDAP_LOG( FILTER, DETAIL1, 
261                         "get_filter: conn %d  OR\n", op->o_connid, 0, 0  );
262 #else
263                 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
264 #endif
265                 err = get_filter_list( op, ber, &f.f_or, text );
266                 if ( err != LDAP_SUCCESS ) {
267                         break;
268                 }
269                 if ( f.f_or == NULL ) {
270                         f.f_choice = SLAPD_FILTER_COMPUTED;
271                         f.f_result = LDAP_COMPARE_FALSE;
272                 }
273                 /* no assert - list could be empty */
274                 break;
275
276         case LDAP_FILTER_NOT:
277 #ifdef NEW_LOGGING
278                 LDAP_LOG( FILTER, DETAIL1, 
279                         "get_filter: conn %d  NOT\n", op->o_connid, 0, 0 );
280 #else
281                 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
282 #endif
283                 (void) ber_skip_tag( ber, &len );
284                 err = get_filter( op, ber, &f.f_not, text );
285                 if ( err != LDAP_SUCCESS ) {
286                         break;
287                 }
288
289                 assert( f.f_not != NULL );
290                 if ( f.f_not->f_choice == SLAPD_FILTER_COMPUTED ) {
291                         int fresult = f.f_not->f_result;
292                         f.f_choice = SLAPD_FILTER_COMPUTED;
293                         op->o_tmpfree( f.f_not, op->o_tmpmemctx );
294                         f.f_not = NULL;
295
296                         switch( fresult ) {
297                         case LDAP_COMPARE_TRUE:
298                                 f.f_result = LDAP_COMPARE_FALSE;
299                                 break;
300                         case LDAP_COMPARE_FALSE:
301                                 f.f_result = LDAP_COMPARE_TRUE;
302                                 break;
303                         default: ;
304                                 /* (!Undefined) is Undefined */
305                         }
306                 }
307                 break;
308
309         case LDAP_FILTER_EXT:
310 #ifdef NEW_LOGGING
311                 LDAP_LOG( FILTER, DETAIL1, 
312                         "get_filter: conn %d  EXTENSIBLE\n", op->o_connid, 0, 0 );
313 #else
314                 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
315 #endif
316
317                 err = get_mra( op, ber, &f.f_mra, text );
318                 if ( err != LDAP_SUCCESS ) {
319                         break;
320                 }
321
322                 assert( f.f_mra != NULL );
323                 break;
324
325         default:
326                 (void) ber_scanf( ber, "x" ); /* skip the element */
327 #ifdef NEW_LOGGING
328                 LDAP_LOG( FILTER, ERR, 
329                         "get_filter: conn %d unknown filter type=%lu\n",
330                         op->o_connid, f.f_choice, 0 );
331 #else
332                 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
333                         f.f_choice, 0, 0 );
334 #endif
335                 f.f_choice = SLAPD_FILTER_COMPUTED;
336                 f.f_result = SLAPD_COMPARE_UNDEFINED;
337                 break;
338         }
339
340         if( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
341                 /* ignore error */
342                 *text = NULL;
343                 f.f_choice = SLAPD_FILTER_COMPUTED;
344                 f.f_result = SLAPD_COMPARE_UNDEFINED;
345                 err = LDAP_SUCCESS;
346         }
347
348         if ( err == LDAP_SUCCESS ) {
349                 *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx );
350                 **filt = f;
351         }
352
353 #ifdef NEW_LOGGING
354         LDAP_LOG( FILTER, DETAIL2, 
355                 "get_filter: conn %d exit\n", op->o_connid, 0, 0 );
356 #else
357         Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
358 #endif
359
360         return( err );
361 }
362
363 static int
364 get_filter_list( Operation *op, BerElement *ber,
365         Filter **f,
366         const char **text )
367 {
368         Filter          **new;
369         int             err;
370         ber_tag_t       tag;
371         ber_len_t       len;
372         char            *last;
373
374 #ifdef NEW_LOGGING
375         LDAP_LOG( FILTER, ENTRY, 
376                 "get_filter_list: conn %d start\n", op->o_connid, 0, 0 );
377 #else
378         Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
379 #endif
380         new = f;
381         for ( tag = ber_first_element( ber, &len, &last );
382                 tag != LBER_DEFAULT;
383                 tag = ber_next_element( ber, &len, last ) )
384         {
385                 err = get_filter( op, ber, new, text );
386                 if ( err != LDAP_SUCCESS )
387                         return( err );
388                 new = &(*new)->f_next;
389         }
390         *new = NULL;
391
392 #ifdef NEW_LOGGING
393         LDAP_LOG( FILTER, ENTRY, 
394                 "get_filter_list: conn %d exit\n", op->o_connid, 0, 0 );
395 #else
396         Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
397 #endif
398         return( LDAP_SUCCESS );
399 }
400
401 static int
402 get_ssa(
403         Operation *op,
404         BerElement      *ber,
405         SubstringsAssertion     **out,
406         const char      **text )
407 {
408         ber_tag_t       tag;
409         ber_len_t       len;
410         ber_tag_t       rc;
411         struct berval desc, value, nvalue;
412         char            *last;
413         SubstringsAssertion ssa;
414
415         *text = "error decoding filter";
416         *out = NULL;
417
418 #ifdef NEW_LOGGING
419         LDAP_LOG( FILTER, ENTRY, 
420                 "get_ssa: conn %d  begin\n", op->o_connid, 0, 0 );
421 #else
422         Debug( LDAP_DEBUG_FILTER, "begin get_ssa\n", 0, 0, 0 );
423 #endif
424         if ( ber_scanf( ber, "{m" /*}*/, &desc ) == LBER_ERROR ) {
425                 return SLAPD_DISCONNECT;
426         }
427
428         *text = NULL;
429
430         ssa.sa_desc = NULL;
431         ssa.sa_initial.bv_val = NULL;
432         ssa.sa_any = NULL;
433         ssa.sa_final.bv_val = NULL;
434
435         rc = slap_bv2ad( &desc, &ssa.sa_desc, text );
436
437         if( rc != LDAP_SUCCESS ) {
438 #ifdef NEW_LOGGING
439                 LDAP_LOG( FILTER, ERR, 
440                         "get_ssa: conn %d d unknown attribute type=%s (%d)\n",
441                         op->o_connid, desc.bv_val, rc );
442 #else
443                 Debug( LDAP_DEBUG_ANY, 
444                         "get_ssa: conn %d unknown attribute type=%s (%d)\n",
445                         op->o_connid, desc.bv_val, rc );
446 #endif
447
448                 /* skip over the rest of this filter */
449                 for ( tag = ber_first_element( ber, &len, &last );
450                         tag != LBER_DEFAULT;
451                         tag = ber_next_element( ber, &len, last ) ) {
452                         ber_scanf( ber, "x" );
453                 }
454                 return rc;
455         }
456
457         rc = LDAP_PROTOCOL_ERROR;
458
459         for ( tag = ber_first_element( ber, &len, &last );
460                 tag != LBER_DEFAULT;
461                 tag = ber_next_element( ber, &len, last ) )
462         {
463                 unsigned usage;
464
465                 rc = ber_scanf( ber, "m", &value );
466                 if ( rc == LBER_ERROR ) {
467                         rc = SLAPD_DISCONNECT;
468                         goto return_error;
469                 }
470
471                 if ( value.bv_val == NULL || value.bv_len == 0 ) {
472                         rc = LDAP_INVALID_SYNTAX;
473                         goto return_error;
474                 } 
475
476                 switch ( tag ) {
477                 case LDAP_SUBSTRING_INITIAL:
478                         usage = SLAP_MR_SUBSTR_INITIAL;
479                         break;
480
481                 case LDAP_SUBSTRING_ANY:
482                         usage = SLAP_MR_SUBSTR_ANY;
483                         break;
484
485                 case LDAP_SUBSTRING_FINAL:
486                         usage = SLAP_MR_SUBSTR_FINAL;
487                         break;
488
489                 default:
490                         rc = LDAP_PROTOCOL_ERROR;
491
492 #ifdef NEW_LOGGING
493                         LDAP_LOG( FILTER, ERR,
494                                 "get_filter_substring: conn %d  unknown substring choice=%ld\n",
495                                 op->o_connid, (long)tag, 0 );
496 #else
497                         Debug( LDAP_DEBUG_FILTER,
498                                 "  unknown substring choice=%ld\n",
499                                 (long) tag, 0, 0 );
500 #endif
501
502                         goto return_error;
503                 }
504
505                 /* validate/normalize using equality matching rule validator! */
506                 rc = asserted_value_validate_normalize(
507                         ssa.sa_desc, ssa.sa_desc->ad_type->sat_equality,
508                         usage, &value, &nvalue, text, op->o_tmpmemctx );
509
510                 if( rc != LDAP_SUCCESS ) {
511                         goto return_error;
512                 }
513
514                 rc = LDAP_PROTOCOL_ERROR;
515
516                 switch ( tag ) {
517                 case LDAP_SUBSTRING_INITIAL:
518 #ifdef NEW_LOGGING
519                         LDAP_LOG( FILTER, DETAIL1,
520                                 "get_ssa: conn %d  INITIAL\n",
521                                 op->o_connid, 0, 0 );
522 #else
523                         Debug( LDAP_DEBUG_FILTER, "  INITIAL\n", 0, 0, 0 );
524 #endif
525
526                         if ( ssa.sa_initial.bv_val != NULL
527                                 || ssa.sa_any != NULL 
528                                 || ssa.sa_final.bv_val != NULL )
529                         {
530                                 sl_free( nvalue.bv_val, op->o_tmpmemctx );
531                                 goto return_error;
532                         }
533
534                         ssa.sa_initial = nvalue;
535                         break;
536
537                 case LDAP_SUBSTRING_ANY:
538 #ifdef NEW_LOGGING
539                         LDAP_LOG( FILTER, DETAIL1,
540                                 "get_ssa: conn %d  ANY\n",
541                                 op->o_connid, 0, 0 );
542 #else
543                         Debug( LDAP_DEBUG_FILTER, "  ANY\n", 0, 0, 0 );
544 #endif
545
546                         if ( ssa.sa_final.bv_val != NULL ) {
547                                 sl_free( nvalue.bv_val, op->o_tmpmemctx );
548                                 goto return_error;
549                         }
550
551                         ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx );
552                         break;
553
554                 case LDAP_SUBSTRING_FINAL:
555 #ifdef NEW_LOGGING
556                         LDAP_LOG( FILTER, DETAIL1, 
557                                 "get_ssa: conn %d  FINAL\n",
558                                 op->o_connid, 0, 0 );
559 #else
560                         Debug( LDAP_DEBUG_FILTER, "  FINAL\n", 0, 0, 0 );
561 #endif
562
563                         if ( ssa.sa_final.bv_val != NULL ) {
564                                 sl_free( nvalue.bv_val, op->o_tmpmemctx );
565                                 goto return_error;
566                         }
567
568                         ssa.sa_final = nvalue;
569                         break;
570
571                 default:
572 #ifdef NEW_LOGGING
573                         LDAP_LOG( FILTER, INFO, 
574                                 "get_ssa: conn %d  unknown substring type %ld\n",
575                                 op->o_connid, (long)tag, 0 );
576 #else
577                         Debug( LDAP_DEBUG_FILTER,
578                                 "  unknown substring type=%ld\n",
579                                 (long) tag, 0, 0 );
580 #endif
581
582                         assert( 0 );
583                         sl_free( nvalue.bv_val, op->o_tmpmemctx );
584
585 return_error:
586 #ifdef NEW_LOGGING
587                         LDAP_LOG( FILTER, INFO, 
588                                 "get_ssa: conn %d  error %ld\n",
589                                 op->o_connid, (long)rc, 0 );
590 #else
591                         Debug( LDAP_DEBUG_FILTER, "  error=%ld\n",
592                                 (long) rc, 0, 0 );
593 #endif
594                         sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
595                         ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
596                         sl_free( ssa.sa_final.bv_val, op->o_tmpmemctx );
597                         return rc;
598                 }
599
600                 rc = LDAP_SUCCESS;
601         }
602
603         if( rc == LDAP_SUCCESS ) {
604                 *out = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
605                 **out = ssa;
606         }
607
608 #ifdef NEW_LOGGING
609         LDAP_LOG( FILTER, ENTRY, 
610                 "get_ssa: conn %d exit\n", op->o_connid, 0, 0 );
611 #else
612         Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
613 #endif
614
615         return rc /* LDAP_SUCCESS */ ;
616 }
617
618 void
619 filter_free_x( Operation *op, Filter *f )
620 {
621         Filter  *p, *next;
622
623         if ( f == NULL ) {
624                 return;
625         }
626
627         switch ( f->f_choice ) {
628         case LDAP_FILTER_PRESENT:
629                 break;
630
631         case LDAP_FILTER_EQUALITY:
632         case LDAP_FILTER_GE:
633         case LDAP_FILTER_LE:
634         case LDAP_FILTER_APPROX:
635                 ava_free( op, f->f_ava, 1 );
636                 break;
637
638         case LDAP_FILTER_SUBSTRINGS:
639                 if ( f->f_sub_initial.bv_val != NULL ) {
640                         op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
641                 }
642                 ber_bvarray_free_x( f->f_sub_any, op->o_tmpmemctx );
643                 if ( f->f_sub_final.bv_val != NULL ) {
644                         op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
645                 }
646                 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
647                 break;
648
649         case LDAP_FILTER_AND:
650         case LDAP_FILTER_OR:
651         case LDAP_FILTER_NOT:
652                 for ( p = f->f_list; p != NULL; p = next ) {
653                         next = p->f_next;
654                         filter_free_x( op, p );
655                 }
656                 break;
657
658         case LDAP_FILTER_EXT:
659                 mra_free( op, f->f_mra, 1 );
660                 break;
661
662         case SLAPD_FILTER_COMPUTED:
663                 break;
664
665         default:
666 #ifdef NEW_LOGGING
667                 LDAP_LOG( FILTER, ERR, 
668                         "filter_free: unknown filter type %lu\n", f->f_choice, 0, 0 );
669 #else
670                 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
671                         f->f_choice, 0, 0 );
672 #endif
673                 break;
674         }
675
676         op->o_tmpfree( f, op->o_tmpmemctx );
677 }
678
679 void
680 filter_free( Filter *f )
681 {
682         Operation op;
683
684         op.o_tmpmemctx = sl_context( f );
685         op.o_tmpmfuncs = &sl_mfuncs;
686         filter_free_x( &op, f );
687 }
688
689 void
690 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
691 {
692         int             i;
693         Filter          *p;
694         struct berval   tmp,
695                         ber_bvfalse = BER_BVC( "(?=false)" ),
696                         ber_bvtrue = BER_BVC( "(?=true)" ),
697                         ber_bvundefined = BER_BVC( "(?=undefined)" ),
698                         ber_bverror = BER_BVC( "(?=error)" ),
699                         ber_bvunknown = BER_BVC( "(?=unknown)" );
700         ber_len_t       len;
701
702         if ( f == NULL ) {
703                 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx );
704                 return;
705         }
706
707         switch ( f->f_choice ) {
708         case LDAP_FILTER_EQUALITY:
709                 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
710
711                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
712                         tmp.bv_len + ( sizeof("(=)") - 1 );
713                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
714
715                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
716                         f->f_av_desc->ad_cname.bv_val,
717                         tmp.bv_val );
718
719                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
720                 break;
721
722         case LDAP_FILTER_GE:
723                 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
724
725                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
726                         tmp.bv_len + ( sizeof("(>=)") - 1 );
727                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
728
729                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
730                         f->f_av_desc->ad_cname.bv_val,
731                         tmp.bv_val );
732
733                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
734                 break;
735
736         case LDAP_FILTER_LE:
737                 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
738
739                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
740                         tmp.bv_len + ( sizeof("(<=)") - 1 );
741                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
742
743                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
744                         f->f_av_desc->ad_cname.bv_val,
745                         tmp.bv_val );
746
747                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
748                 break;
749
750         case LDAP_FILTER_APPROX:
751                 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
752
753                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
754                         tmp.bv_len + ( sizeof("(~=)") - 1 );
755                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
756
757                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
758                         f->f_av_desc->ad_cname.bv_val,
759                         tmp.bv_val );
760                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
761                 break;
762
763         case LDAP_FILTER_SUBSTRINGS:
764                 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
765                         ( sizeof("(=*)") - 1 );
766                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
767
768                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
769                         f->f_sub_desc->ad_cname.bv_val );
770
771                 if ( f->f_sub_initial.bv_val != NULL ) {
772                         len = fstr->bv_len;
773
774                         filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
775
776                         fstr->bv_len += tmp.bv_len;
777                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
778
779                         snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
780                                 /* "(attr=" */ "%s*)",
781                                 tmp.bv_val );
782
783                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
784                 }
785
786                 if ( f->f_sub_any != NULL ) {
787                         for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
788                                 len = fstr->bv_len;
789                                 filter_escape_value_x( &f->f_sub_any[i], &tmp, op->o_tmpmemctx );
790
791                                 fstr->bv_len += tmp.bv_len + 1;
792                                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
793
794                                 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
795                                         /* "(attr=[init]*[any*]" */ "%s*)",
796                                         tmp.bv_val );
797                                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
798                         }
799                 }
800
801                 if ( f->f_sub_final.bv_val != NULL ) {
802                         len = fstr->bv_len;
803
804                         filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
805
806                         fstr->bv_len += tmp.bv_len;
807                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
808
809                         snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
810                                 /* "(attr=[init*][any*]" */ "%s)",
811                                 tmp.bv_val );
812
813                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
814                 }
815
816                 break;
817
818         case LDAP_FILTER_PRESENT:
819                 fstr->bv_len = f->f_desc->ad_cname.bv_len +
820                         ( sizeof("(=*)") - 1 );
821                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
822
823                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
824                         f->f_desc->ad_cname.bv_val );
825                 break;
826
827         case LDAP_FILTER_AND:
828         case LDAP_FILTER_OR:
829         case LDAP_FILTER_NOT:
830                 fstr->bv_len = sizeof("(%)") - 1;
831                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
832
833                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
834                         f->f_choice == LDAP_FILTER_AND ? '&' :
835                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
836
837                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
838                         len = fstr->bv_len;
839
840                         filter2bv_x( op, p, &tmp );
841                         
842                         fstr->bv_len += tmp.bv_len;
843                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
844
845                         snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, 
846                                 /*"("*/ "%s)", tmp.bv_val );
847
848                         op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
849                 }
850
851                 break;
852
853         case LDAP_FILTER_EXT: {
854                 struct berval ad;
855                 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
856
857                 if ( f->f_mr_desc ) {
858                         ad = f->f_mr_desc->ad_cname;
859                 } else {
860                         ad.bv_len = 0;
861                         ad.bv_val = "";
862                 }
863                         
864                 fstr->bv_len = ad.bv_len +
865                         ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
866                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
867                         tmp.bv_len + ( sizeof("(:=)") - 1 );
868                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
869
870                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
871                         ad.bv_val,
872                         f->f_mr_dnattrs ? ":dn" : "",
873                         f->f_mr_rule_text.bv_len ? ":" : "",
874                         f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
875                         tmp.bv_val );
876                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
877                 } break;
878
879         case SLAPD_FILTER_COMPUTED:
880                 switch ( f->f_result ) {
881                 case LDAP_COMPARE_FALSE:
882                         tmp = ber_bvfalse;
883                         break;
884
885                 case LDAP_COMPARE_TRUE:
886                         tmp = ber_bvtrue;
887                         break;
888                         
889                 case SLAPD_COMPARE_UNDEFINED:
890                         tmp = ber_bvundefined;
891                         break;
892                         
893                 default:
894                         tmp = ber_bverror;
895                         break;
896                 }
897
898                 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
899                 break;
900                 
901         default:
902                 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
903                 break;
904         }
905 }
906
907 void
908 filter2bv( Filter *f, struct berval *fstr )
909 {
910         Operation op;
911         op.o_tmpmemctx = NULL;
912         op.o_tmpmfuncs = &ch_mfuncs;
913
914         filter2bv_x( &op, f, fstr );
915 }
916
917 static int
918 filter_escape_value_x(
919         struct berval *in,
920         struct berval *out,
921         void *ctx )
922 {
923         ber_len_t i;
924         assert( in );
925         assert( out );
926
927         i = in->bv_len * 3 + 1;
928         out->bv_val = ctx ? sl_malloc( i, ctx ) : ch_malloc( i );
929         out->bv_len = 0;
930
931         for( i=0; i < in->bv_len ; i++ ) {
932                 if( FILTER_ESCAPE(in->bv_val[i]) ) {
933                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
934                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
935                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
936                 } else {
937                         out->bv_val[out->bv_len++] = in->bv_val[i];
938                 }
939         }
940
941         out->bv_val[out->bv_len] = '\0';
942         return LDAP_SUCCESS;
943 }
944
945 int
946 filter_escape_value(
947         struct berval *in,
948         struct berval *out )
949 {
950         return filter_escape_value_x( in, out, NULL );
951 }
952
953 static int
954 get_simple_vrFilter(
955         Operation *op,
956         BerElement *ber,
957         ValuesReturnFilter **filt,
958         const char **text )
959 {
960         ber_tag_t       tag;
961         ber_len_t       len;
962         int             err;
963         ValuesReturnFilter vrf;
964
965 #ifdef NEW_LOGGING
966         LDAP_LOG( FILTER, ENTRY, 
967                 "get_simple_vrFilter: conn %d\n", op->o_connid, 0, 0 );
968 #else
969         Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
970 #endif
971
972         tag = ber_peek_tag( ber, &len );
973
974         if( tag == LBER_ERROR ) {
975                 *text = "error decoding filter";
976                 return SLAPD_DISCONNECT;
977         }
978
979         vrf.vrf_next = NULL;
980
981         err = LDAP_SUCCESS;
982         vrf.vrf_choice = tag; 
983
984         switch ( vrf.vrf_choice ) {
985         case LDAP_FILTER_EQUALITY:
986 #ifdef NEW_LOGGING
987                 LDAP_LOG( FILTER, DETAIL2, 
988                         "get_simple_vrFilter: conn %d  EQUALITY\n", op->o_connid, 0, 0 );
989 #else
990                 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
991 #endif
992                 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
993                 if ( err != LDAP_SUCCESS ) {
994                         break;
995                 }
996
997                 assert( vrf.vrf_ava != NULL );
998                 break;
999
1000         case LDAP_FILTER_SUBSTRINGS:
1001 #ifdef NEW_LOGGING
1002                 LDAP_LOG( FILTER, DETAIL1, 
1003                         "get_simple_vrFilter: conn %d  SUBSTRINGS\n", op->o_connid, 0, 0 );
1004 #else
1005                 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
1006 #endif
1007                 err = get_ssa( op, ber, &vrf.vrf_sub, text );
1008                 break;
1009
1010         case LDAP_FILTER_GE:
1011 #ifdef NEW_LOGGING
1012                 LDAP_LOG( FILTER, DETAIL1, 
1013                         "get_simple_vrFilter: conn %d  GE\n", op->o_connid, 0, 0 );
1014 #else
1015                 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
1016 #endif
1017                 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1018                 if ( err != LDAP_SUCCESS ) {
1019                         break;
1020                 }
1021                 break;
1022
1023         case LDAP_FILTER_LE:
1024 #ifdef NEW_LOGGING
1025                 LDAP_LOG( FILTER, DETAIL1, 
1026                         "get_simple_vrFilter: conn %d  LE\n", op->o_connid, 0, 0 );
1027 #else
1028                 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
1029 #endif
1030                 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1031                 if ( err != LDAP_SUCCESS ) {
1032                         break;
1033                 }
1034                 break;
1035
1036         case LDAP_FILTER_PRESENT: {
1037                 struct berval type;
1038
1039 #ifdef NEW_LOGGING
1040                 LDAP_LOG( FILTER, DETAIL1, 
1041                         "get_simple_vrFilter: conn %d PRESENT\n", op->o_connid, 0, 0 );
1042 #else
1043                 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
1044 #endif
1045                 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
1046                         err = SLAPD_DISCONNECT;
1047                         *text = "error decoding filter";
1048                         break;
1049                 }
1050
1051                 vrf.vrf_desc = NULL;
1052                 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
1053
1054                 if( err != LDAP_SUCCESS ) {
1055                         /* unrecognized attribute description or other error */
1056 #ifdef NEW_LOGGING
1057                         LDAP_LOG( FILTER, ERR, 
1058                                 "get_simple_vrFilter: conn %d unknown "
1059                                 "attribute type=%s (%d)\n",
1060                                 op->o_connid, type.bv_val, err );
1061 #else
1062                         Debug( LDAP_DEBUG_ANY, 
1063                                 "get_simple_vrFilter: conn %d unknown "
1064                                 "attribute type=%s (%d)\n",
1065                                 op->o_connid, type.bv_val, err );
1066 #endif
1067
1068                         vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1069                         vrf.vrf_result = LDAP_COMPARE_FALSE;
1070                         err = LDAP_SUCCESS;
1071                         break;
1072                 }
1073                 } break;
1074
1075         case LDAP_FILTER_APPROX:
1076 #ifdef NEW_LOGGING
1077                 LDAP_LOG( FILTER, DETAIL1, 
1078                         "get_simple_vrFilter: conn %d  APPROX\n", op->o_connid, 0, 0 );
1079 #else
1080                 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
1081 #endif
1082                 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
1083                 if ( err != LDAP_SUCCESS ) {
1084                         break;
1085                 }
1086                 break;
1087
1088         case LDAP_FILTER_EXT:
1089 #ifdef NEW_LOGGING
1090                 LDAP_LOG( FILTER, DETAIL1, 
1091                         "get_simple_vrFilter: conn %d  EXTENSIBLE\n", op->o_connid, 0, 0 );
1092 #else
1093                 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1094 #endif
1095
1096                 err = get_mra( op, ber, &vrf.vrf_mra, text );
1097                 if ( err != LDAP_SUCCESS ) {
1098                         break;
1099                 }
1100
1101                 assert( vrf.vrf_mra != NULL );
1102                 break;
1103
1104         default:
1105                 (void) ber_scanf( ber, "x" ); /* skip the element */
1106 #ifdef NEW_LOGGING
1107                 LDAP_LOG( FILTER, ERR, 
1108                         "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
1109                         op->o_connid, vrf.vrf_choice, 0 );
1110 #else
1111                 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1112                         vrf.vrf_choice, 0, 0 );
1113 #endif
1114                 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1115                 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1116                 break;
1117         }
1118
1119         if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1120                 /* ignore error */
1121                 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1122                 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1123                 err = LDAP_SUCCESS;
1124         }
1125
1126         if ( err == LDAP_SUCCESS ) {
1127                 *filt = ch_malloc( sizeof vrf );
1128                 **filt = vrf;
1129         }
1130
1131 #ifdef NEW_LOGGING
1132         LDAP_LOG( FILTER, DETAIL2, 
1133                 "get_simple_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1134 #else
1135         Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1136 #endif
1137
1138         return err;
1139 }
1140
1141 int
1142 get_vrFilter( Operation *op, BerElement *ber,
1143         ValuesReturnFilter **vrf,
1144         const char **text )
1145 {
1146         /*
1147          * A ValuesReturnFilter looks like this:
1148          *
1149          *      ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1150          *      SimpleFilterItem ::= CHOICE {
1151          *              equalityMatch   [3]     AttributeValueAssertion,
1152          *              substrings      [4]     SubstringFilter,
1153          *              greaterOrEqual  [5]     AttributeValueAssertion,
1154          *              lessOrEqual     [6]     AttributeValueAssertion,
1155          *              present         [7]     AttributeType,
1156          *              approxMatch     [8]     AttributeValueAssertion,
1157          *              extensibleMatch [9]     SimpleMatchingAssertion -- LDAPv3
1158          *      }
1159          *
1160          *      SubstringFilter ::= SEQUENCE {
1161          *              type               AttributeType,
1162          *              SEQUENCE OF CHOICE {
1163          *                      initial          [0] IA5String,
1164          *                      any              [1] IA5String,
1165          *                      final            [2] IA5String
1166          *              }
1167          *      }
1168          *
1169          *      SimpleMatchingAssertion ::= SEQUENCE {  -- LDAPv3
1170          *              matchingRule    [1] MatchingRuleId OPTIONAL,
1171          *              type            [2] AttributeDescription OPTIONAL,
1172          *              matchValue      [3] AssertionValue }
1173          */
1174
1175         ValuesReturnFilter **n;
1176         ber_tag_t       tag;
1177         ber_len_t       len;
1178         char            *last;
1179
1180 #ifdef NEW_LOGGING
1181         LDAP_LOG( FILTER, ENTRY, 
1182                 "get_vrFilter: conn %d start\n", op->o_connid, 0, 0 );
1183 #else
1184         Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1185 #endif
1186
1187         tag = ber_peek_tag( ber, &len );
1188
1189         if( tag == LBER_ERROR ) {
1190                 *text = "error decoding vrFilter";
1191                 return SLAPD_DISCONNECT;
1192         }
1193
1194         if( tag != LBER_SEQUENCE ) {
1195                 *text = "error decoding vrFilter, expect SEQUENCE tag";
1196                 return SLAPD_DISCONNECT;
1197         }
1198
1199         n = vrf;
1200         for ( tag = ber_first_element( ber, &len, &last );
1201                 tag != LBER_DEFAULT;
1202                 tag = ber_next_element( ber, &len, last ) )
1203         {
1204                 int err = get_simple_vrFilter( op, ber, n, text );
1205
1206                 if ( err != LDAP_SUCCESS ) return( err );
1207
1208                 n = &(*n)->vrf_next;
1209         }
1210         *n = NULL;
1211
1212 #ifdef NEW_LOGGING
1213         LDAP_LOG( FILTER, ENTRY, 
1214                 "get_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1215 #else
1216         Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1217 #endif
1218         return( LDAP_SUCCESS );
1219 }
1220
1221 void
1222 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1223 {
1224         ValuesReturnFilter      *p, *next;
1225
1226         if ( vrf == NULL ) {
1227                 return;
1228         }
1229
1230         for ( p = vrf; p != NULL; p = next ) {
1231                 next = p->vrf_next;
1232
1233                 switch ( vrf->vrf_choice ) {
1234                 case LDAP_FILTER_PRESENT:
1235                         break;
1236
1237                 case LDAP_FILTER_EQUALITY:
1238                 case LDAP_FILTER_GE:
1239                 case LDAP_FILTER_LE:
1240                 case LDAP_FILTER_APPROX:
1241                         ava_free( op, vrf->vrf_ava, 1 );
1242                         break;
1243
1244                 case LDAP_FILTER_SUBSTRINGS:
1245                         if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1246                                 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1247                         }
1248                         ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1249                         if ( vrf->vrf_sub_final.bv_val != NULL ) {
1250                                 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1251                         }
1252                         op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1253                         break;
1254
1255                 case LDAP_FILTER_EXT:
1256                         mra_free( op, vrf->vrf_mra, 1 );
1257                         break;
1258
1259                 case SLAPD_FILTER_COMPUTED:
1260                         break;
1261
1262                 default:
1263 #ifdef NEW_LOGGING
1264                         LDAP_LOG( FILTER, ERR, 
1265                                 "filter_free: unknown filter type %lu\n", vrf->vrf_choice, 0, 0 );
1266 #else
1267                         Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1268                                 vrf->vrf_choice, 0, 0 );
1269 #endif
1270                         break;
1271                 }
1272
1273                 op->o_tmpfree( vrf, op->o_tmpmemctx );
1274         }
1275 }
1276
1277 void
1278 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1279 {
1280         ValuesReturnFilter      *p;
1281         struct berval tmp;
1282         ber_len_t len;
1283
1284         if ( vrf == NULL ) {
1285                 ber_str2bv_x( "No filter!", sizeof("No filter!")-1,
1286                         1, fstr, op->o_tmpmemctx );
1287                 return;
1288         }
1289
1290         fstr->bv_len = sizeof("()") - 1;
1291         fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1292
1293         snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1294
1295         for ( p = vrf; p != NULL; p = p->vrf_next ) {
1296                 len = fstr->bv_len;
1297
1298                 simple_vrFilter2bv( op, p, &tmp );
1299                         
1300                 fstr->bv_len += tmp.bv_len;
1301                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1302
1303                 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, 
1304                         /*"("*/ "%s)", tmp.bv_val );
1305
1306                 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1307         }
1308 }
1309
1310 static void
1311 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1312 {
1313         struct berval tmp;
1314         ber_len_t len;
1315
1316         if ( vrf == NULL ) {
1317                 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx );
1318                 return;
1319         }
1320
1321         switch ( vrf->vrf_choice ) {
1322         case LDAP_FILTER_EQUALITY:
1323                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1324
1325                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1326                         tmp.bv_len + ( sizeof("(=)") - 1 );
1327                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1328
1329                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1330                         vrf->vrf_av_desc->ad_cname.bv_val,
1331                         tmp.bv_val );
1332
1333                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1334                 break;
1335
1336         case LDAP_FILTER_GE:
1337                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1338
1339                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1340                         tmp.bv_len + ( sizeof("(>=)") - 1 );
1341                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1342
1343                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1344                         vrf->vrf_av_desc->ad_cname.bv_val,
1345                         tmp.bv_val );
1346
1347                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1348                 break;
1349
1350         case LDAP_FILTER_LE:
1351                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1352
1353                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1354                         tmp.bv_len + ( sizeof("(<=)") - 1 );
1355                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1356
1357                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1358                         vrf->vrf_av_desc->ad_cname.bv_val,
1359                         tmp.bv_val );
1360
1361                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1362                 break;
1363
1364         case LDAP_FILTER_APPROX:
1365                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1366
1367                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1368                         tmp.bv_len + ( sizeof("(~=)") - 1 );
1369                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1370
1371                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1372                         vrf->vrf_av_desc->ad_cname.bv_val,
1373                         tmp.bv_val );
1374                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1375                 break;
1376
1377         case LDAP_FILTER_SUBSTRINGS:
1378                 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1379                         ( sizeof("(=*)") - 1 );
1380                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1381
1382                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1383                         vrf->vrf_sub_desc->ad_cname.bv_val );
1384
1385                 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1386                         len = fstr->bv_len;
1387
1388                         filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1389
1390                         fstr->bv_len += tmp.bv_len;
1391                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1392
1393                         snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1394                                 /* "(attr=" */ "%s*)",
1395                                 tmp.bv_val );
1396
1397                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1398                 }
1399
1400                 if ( vrf->vrf_sub_any != NULL ) {
1401                         int i;
1402                         for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1403                                 len = fstr->bv_len;
1404                                 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp, op->o_tmpmemctx );
1405
1406                                 fstr->bv_len += tmp.bv_len + 1;
1407                                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1408
1409                                 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1410                                         /* "(attr=[init]*[any*]" */ "%s*)",
1411                                         tmp.bv_val );
1412                                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1413                         }
1414                 }
1415
1416                 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1417                         len = fstr->bv_len;
1418
1419                         filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1420
1421                         fstr->bv_len += tmp.bv_len;
1422                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1423
1424                         snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1425                                 /* "(attr=[init*][any*]" */ "%s)",
1426                                 tmp.bv_val );
1427
1428                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1429                 }
1430
1431                 break;
1432
1433         case LDAP_FILTER_PRESENT:
1434                 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1435                         ( sizeof("(=*)") - 1 );
1436                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1437
1438                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1439                         vrf->vrf_desc->ad_cname.bv_val );
1440                 break;
1441
1442         case LDAP_FILTER_EXT: {
1443                 struct berval ad;
1444                 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1445
1446                 if ( vrf->vrf_mr_desc ) {
1447                         ad = vrf->vrf_mr_desc->ad_cname;
1448                 } else {
1449                         ad.bv_len = 0;
1450                         ad.bv_val = "";
1451                 }
1452                         
1453                 fstr->bv_len = ad.bv_len +
1454                         ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1455                         ( vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1456                         tmp.bv_len + ( sizeof("(:=)") - 1 );
1457                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1458
1459                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1460                         ad.bv_val,
1461                         vrf->vrf_mr_dnattrs ? ":dn" : "",
1462                         vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1463                         vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1464                         tmp.bv_val );
1465
1466                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1467                 } break;
1468
1469         case SLAPD_FILTER_COMPUTED:
1470                 ber_str2bv_x(
1471                         vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1472                         vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1473                         vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
1474                         "(?=error)",
1475                         vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1476                         vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1477                         vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
1478                         sizeof("(?=error)")-1,
1479                         1, fstr, op->o_tmpmemctx );
1480                 break;
1481
1482         default:
1483                 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1,
1484                         1, fstr, op->o_tmpmemctx );
1485                 break;
1486         }
1487 }