]> git.sur5r.net Git - openldap/blob - servers/slapd/filter.c
ITS#3226: Clear attribute flags after schema_check failed
[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                                 slap_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                                 slap_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                                 slap_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                         slap_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                         slap_sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
595                         ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
596                         slap_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 = slap_sl_context( f );
685         op.o_tmpmfuncs = &slap_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         static struct berval
696                         ber_bvfalse = BER_BVC( "(?=false)" ),
697                         ber_bvtrue = BER_BVC( "(?=true)" ),
698                         ber_bvundefined = BER_BVC( "(?=undefined)" ),
699                         ber_bverror = BER_BVC( "(?=error)" ),
700                         ber_bvunknown = BER_BVC( "(?=unknown)" ),
701                         ber_bvnone = BER_BVC( "(?=none)" );
702         ber_len_t       len;
703
704         if ( f == NULL ) {
705                 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
706                 return;
707         }
708
709         switch ( f->f_choice ) {
710         case LDAP_FILTER_EQUALITY:
711                 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
712
713                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
714                         tmp.bv_len + ( sizeof("(=)") - 1 );
715                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
716
717                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
718                         f->f_av_desc->ad_cname.bv_val,
719                         tmp.bv_val );
720
721                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
722                 break;
723
724         case LDAP_FILTER_GE:
725                 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
726
727                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
728                         tmp.bv_len + ( sizeof("(>=)") - 1 );
729                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
730
731                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
732                         f->f_av_desc->ad_cname.bv_val,
733                         tmp.bv_val );
734
735                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
736                 break;
737
738         case LDAP_FILTER_LE:
739                 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
740
741                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
742                         tmp.bv_len + ( sizeof("(<=)") - 1 );
743                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
744
745                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
746                         f->f_av_desc->ad_cname.bv_val,
747                         tmp.bv_val );
748
749                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
750                 break;
751
752         case LDAP_FILTER_APPROX:
753                 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
754
755                 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
756                         tmp.bv_len + ( sizeof("(~=)") - 1 );
757                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
758
759                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
760                         f->f_av_desc->ad_cname.bv_val,
761                         tmp.bv_val );
762                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
763                 break;
764
765         case LDAP_FILTER_SUBSTRINGS:
766                 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
767                         ( sizeof("(=*)") - 1 );
768                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
769
770                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
771                         f->f_sub_desc->ad_cname.bv_val );
772
773                 if ( f->f_sub_initial.bv_val != NULL ) {
774                         len = fstr->bv_len;
775
776                         filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
777
778                         fstr->bv_len += tmp.bv_len;
779                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
780                                 op->o_tmpmemctx );
781
782                         snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
783                                 /* "(attr=" */ "%s*)",
784                                 tmp.bv_val );
785
786                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
787                 }
788
789                 if ( f->f_sub_any != NULL ) {
790                         for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
791                                 len = fstr->bv_len;
792                                 filter_escape_value_x( &f->f_sub_any[i],
793                                         &tmp, op->o_tmpmemctx );
794
795                                 fstr->bv_len += tmp.bv_len + 1;
796                                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
797                                         op->o_tmpmemctx );
798
799                                 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
800                                         /* "(attr=[init]*[any*]" */ "%s*)",
801                                         tmp.bv_val );
802                                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
803                         }
804                 }
805
806                 if ( f->f_sub_final.bv_val != NULL ) {
807                         len = fstr->bv_len;
808
809                         filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
810
811                         fstr->bv_len += tmp.bv_len;
812                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
813                                 op->o_tmpmemctx );
814
815                         snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
816                                 /* "(attr=[init*][any*]" */ "%s)",
817                                 tmp.bv_val );
818
819                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
820                 }
821
822                 break;
823
824         case LDAP_FILTER_PRESENT:
825                 fstr->bv_len = f->f_desc->ad_cname.bv_len +
826                         ( sizeof("(=*)") - 1 );
827                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
828
829                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
830                         f->f_desc->ad_cname.bv_val );
831                 break;
832
833         case LDAP_FILTER_AND:
834         case LDAP_FILTER_OR:
835         case LDAP_FILTER_NOT:
836                 fstr->bv_len = sizeof("(%)") - 1;
837                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
838
839                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
840                         f->f_choice == LDAP_FILTER_AND ? '&' :
841                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
842
843                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
844                         len = fstr->bv_len;
845
846                         filter2bv_x( op, p, &tmp );
847                         
848                         fstr->bv_len += tmp.bv_len;
849                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
850                                 op->o_tmpmemctx );
851
852                         snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, 
853                                 /*"("*/ "%s)", tmp.bv_val );
854
855                         op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
856                 }
857
858                 break;
859
860         case LDAP_FILTER_EXT: {
861                         struct berval ad;
862                         filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
863
864                         if ( f->f_mr_desc ) {
865                                 ad = f->f_mr_desc->ad_cname;
866                         } else {
867                                 ad.bv_len = 0;
868                                 ad.bv_val = "";
869                         }
870                         
871                         fstr->bv_len = ad.bv_len +
872                                 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
873                                 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
874                                 tmp.bv_len + ( sizeof("(:=)") - 1 );
875                         fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
876
877                         snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
878                                 ad.bv_val,
879                                 f->f_mr_dnattrs ? ":dn" : "",
880                                 f->f_mr_rule_text.bv_len ? ":" : "",
881                                 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
882                                 tmp.bv_val );
883                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
884                 } break;
885
886         case SLAPD_FILTER_COMPUTED:
887                 switch ( f->f_result ) {
888                 case LDAP_COMPARE_FALSE:
889                         tmp = ber_bvfalse;
890                         break;
891
892                 case LDAP_COMPARE_TRUE:
893                         tmp = ber_bvtrue;
894                         break;
895                         
896                 case SLAPD_COMPARE_UNDEFINED:
897                         tmp = ber_bvundefined;
898                         break;
899                         
900                 default:
901                         tmp = ber_bverror;
902                         break;
903                 }
904
905                 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
906                 break;
907                 
908         default:
909                 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
910                 break;
911         }
912 }
913
914 void
915 filter2bv( Filter *f, struct berval *fstr )
916 {
917         Operation op;
918         op.o_tmpmemctx = NULL;
919         op.o_tmpmfuncs = &ch_mfuncs;
920
921         filter2bv_x( &op, f, fstr );
922 }
923
924 static int
925 filter_escape_value_x(
926         struct berval *in,
927         struct berval *out,
928         void *ctx )
929 {
930         ber_len_t i;
931         assert( in );
932         assert( out );
933
934         i = in->bv_len * 3 + 1;
935         out->bv_val = ctx ? slap_sl_malloc( i, ctx ) : ch_malloc( i );
936         out->bv_len = 0;
937
938         for( i=0; i < in->bv_len ; i++ ) {
939                 if( FILTER_ESCAPE(in->bv_val[i]) ) {
940                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
941                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
942                         out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
943                 } else {
944                         out->bv_val[out->bv_len++] = in->bv_val[i];
945                 }
946         }
947
948         out->bv_val[out->bv_len] = '\0';
949         return LDAP_SUCCESS;
950 }
951
952 int
953 filter_escape_value(
954         struct berval *in,
955         struct berval *out )
956 {
957         return filter_escape_value_x( in, out, NULL );
958 }
959
960 static int
961 get_simple_vrFilter(
962         Operation *op,
963         BerElement *ber,
964         ValuesReturnFilter **filt,
965         const char **text )
966 {
967         ber_tag_t       tag;
968         ber_len_t       len;
969         int             err;
970         ValuesReturnFilter vrf;
971
972 #ifdef NEW_LOGGING
973         LDAP_LOG( FILTER, ENTRY, 
974                 "get_simple_vrFilter: conn %d\n", op->o_connid, 0, 0 );
975 #else
976         Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
977 #endif
978
979         tag = ber_peek_tag( ber, &len );
980
981         if( tag == LBER_ERROR ) {
982                 *text = "error decoding filter";
983                 return SLAPD_DISCONNECT;
984         }
985
986         vrf.vrf_next = NULL;
987
988         err = LDAP_SUCCESS;
989         vrf.vrf_choice = tag; 
990
991         switch ( vrf.vrf_choice ) {
992         case LDAP_FILTER_EQUALITY:
993 #ifdef NEW_LOGGING
994                 LDAP_LOG( FILTER, DETAIL2, 
995                         "get_simple_vrFilter: conn %d  EQUALITY\n", op->o_connid, 0, 0 );
996 #else
997                 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
998 #endif
999                 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
1000                 if ( err != LDAP_SUCCESS ) {
1001                         break;
1002                 }
1003
1004                 assert( vrf.vrf_ava != NULL );
1005                 break;
1006
1007         case LDAP_FILTER_SUBSTRINGS:
1008 #ifdef NEW_LOGGING
1009                 LDAP_LOG( FILTER, DETAIL1, 
1010                         "get_simple_vrFilter: conn %d  SUBSTRINGS\n", op->o_connid, 0, 0 );
1011 #else
1012                 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
1013 #endif
1014                 err = get_ssa( op, ber, &vrf.vrf_sub, text );
1015                 break;
1016
1017         case LDAP_FILTER_GE:
1018 #ifdef NEW_LOGGING
1019                 LDAP_LOG( FILTER, DETAIL1, 
1020                         "get_simple_vrFilter: conn %d  GE\n", op->o_connid, 0, 0 );
1021 #else
1022                 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
1023 #endif
1024                 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1025                 if ( err != LDAP_SUCCESS ) {
1026                         break;
1027                 }
1028                 break;
1029
1030         case LDAP_FILTER_LE:
1031 #ifdef NEW_LOGGING
1032                 LDAP_LOG( FILTER, DETAIL1, 
1033                         "get_simple_vrFilter: conn %d  LE\n", op->o_connid, 0, 0 );
1034 #else
1035                 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
1036 #endif
1037                 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1038                 if ( err != LDAP_SUCCESS ) {
1039                         break;
1040                 }
1041                 break;
1042
1043         case LDAP_FILTER_PRESENT: {
1044                 struct berval type;
1045
1046 #ifdef NEW_LOGGING
1047                 LDAP_LOG( FILTER, DETAIL1, 
1048                         "get_simple_vrFilter: conn %d PRESENT\n", op->o_connid, 0, 0 );
1049 #else
1050                 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
1051 #endif
1052                 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
1053                         err = SLAPD_DISCONNECT;
1054                         *text = "error decoding filter";
1055                         break;
1056                 }
1057
1058                 vrf.vrf_desc = NULL;
1059                 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
1060
1061                 if( err != LDAP_SUCCESS ) {
1062                         /* unrecognized attribute description or other error */
1063 #ifdef NEW_LOGGING
1064                         LDAP_LOG( FILTER, ERR, 
1065                                 "get_simple_vrFilter: conn %d unknown "
1066                                 "attribute type=%s (%d)\n",
1067                                 op->o_connid, type.bv_val, err );
1068 #else
1069                         Debug( LDAP_DEBUG_ANY, 
1070                                 "get_simple_vrFilter: conn %d unknown "
1071                                 "attribute type=%s (%d)\n",
1072                                 op->o_connid, type.bv_val, err );
1073 #endif
1074
1075                         vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1076                         vrf.vrf_result = LDAP_COMPARE_FALSE;
1077                         err = LDAP_SUCCESS;
1078                         break;
1079                 }
1080                 } break;
1081
1082         case LDAP_FILTER_APPROX:
1083 #ifdef NEW_LOGGING
1084                 LDAP_LOG( FILTER, DETAIL1, 
1085                         "get_simple_vrFilter: conn %d  APPROX\n", op->o_connid, 0, 0 );
1086 #else
1087                 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
1088 #endif
1089                 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
1090                 if ( err != LDAP_SUCCESS ) {
1091                         break;
1092                 }
1093                 break;
1094
1095         case LDAP_FILTER_EXT:
1096 #ifdef NEW_LOGGING
1097                 LDAP_LOG( FILTER, DETAIL1, 
1098                         "get_simple_vrFilter: conn %d  EXTENSIBLE\n", op->o_connid, 0, 0 );
1099 #else
1100                 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1101 #endif
1102
1103                 err = get_mra( op, ber, &vrf.vrf_mra, text );
1104                 if ( err != LDAP_SUCCESS ) {
1105                         break;
1106                 }
1107
1108                 assert( vrf.vrf_mra != NULL );
1109                 break;
1110
1111         default:
1112                 (void) ber_scanf( ber, "x" ); /* skip the element */
1113 #ifdef NEW_LOGGING
1114                 LDAP_LOG( FILTER, ERR, 
1115                         "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
1116                         op->o_connid, vrf.vrf_choice, 0 );
1117 #else
1118                 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1119                         vrf.vrf_choice, 0, 0 );
1120 #endif
1121                 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1122                 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1123                 break;
1124         }
1125
1126         if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1127                 /* ignore error */
1128                 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1129                 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1130                 err = LDAP_SUCCESS;
1131         }
1132
1133         if ( err == LDAP_SUCCESS ) {
1134                 *filt = ch_malloc( sizeof vrf );
1135                 **filt = vrf;
1136         }
1137
1138 #ifdef NEW_LOGGING
1139         LDAP_LOG( FILTER, DETAIL2, 
1140                 "get_simple_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1141 #else
1142         Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1143 #endif
1144
1145         return err;
1146 }
1147
1148 int
1149 get_vrFilter( Operation *op, BerElement *ber,
1150         ValuesReturnFilter **vrf,
1151         const char **text )
1152 {
1153         /*
1154          * A ValuesReturnFilter looks like this:
1155          *
1156          *      ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1157          *      SimpleFilterItem ::= CHOICE {
1158          *              equalityMatch   [3]     AttributeValueAssertion,
1159          *              substrings      [4]     SubstringFilter,
1160          *              greaterOrEqual  [5]     AttributeValueAssertion,
1161          *              lessOrEqual     [6]     AttributeValueAssertion,
1162          *              present         [7]     AttributeType,
1163          *              approxMatch     [8]     AttributeValueAssertion,
1164          *              extensibleMatch [9]     SimpleMatchingAssertion -- LDAPv3
1165          *      }
1166          *
1167          *      SubstringFilter ::= SEQUENCE {
1168          *              type               AttributeType,
1169          *              SEQUENCE OF CHOICE {
1170          *                      initial          [0] IA5String,
1171          *                      any              [1] IA5String,
1172          *                      final            [2] IA5String
1173          *              }
1174          *      }
1175          *
1176          *      SimpleMatchingAssertion ::= SEQUENCE {  -- LDAPv3
1177          *              matchingRule    [1] MatchingRuleId OPTIONAL,
1178          *              type            [2] AttributeDescription OPTIONAL,
1179          *              matchValue      [3] AssertionValue }
1180          */
1181
1182         ValuesReturnFilter **n;
1183         ber_tag_t       tag;
1184         ber_len_t       len;
1185         char            *last;
1186
1187 #ifdef NEW_LOGGING
1188         LDAP_LOG( FILTER, ENTRY, 
1189                 "get_vrFilter: conn %d start\n", op->o_connid, 0, 0 );
1190 #else
1191         Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1192 #endif
1193
1194         tag = ber_peek_tag( ber, &len );
1195
1196         if( tag == LBER_ERROR ) {
1197                 *text = "error decoding vrFilter";
1198                 return SLAPD_DISCONNECT;
1199         }
1200
1201         if( tag != LBER_SEQUENCE ) {
1202                 *text = "error decoding vrFilter, expect SEQUENCE tag";
1203                 return SLAPD_DISCONNECT;
1204         }
1205
1206         n = vrf;
1207         for ( tag = ber_first_element( ber, &len, &last );
1208                 tag != LBER_DEFAULT;
1209                 tag = ber_next_element( ber, &len, last ) )
1210         {
1211                 int err = get_simple_vrFilter( op, ber, n, text );
1212
1213                 if ( err != LDAP_SUCCESS ) return( err );
1214
1215                 n = &(*n)->vrf_next;
1216         }
1217         *n = NULL;
1218
1219 #ifdef NEW_LOGGING
1220         LDAP_LOG( FILTER, ENTRY, 
1221                 "get_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1222 #else
1223         Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1224 #endif
1225         return( LDAP_SUCCESS );
1226 }
1227
1228 void
1229 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1230 {
1231         ValuesReturnFilter      *p, *next;
1232
1233         if ( vrf == NULL ) {
1234                 return;
1235         }
1236
1237         for ( p = vrf; p != NULL; p = next ) {
1238                 next = p->vrf_next;
1239
1240                 switch ( vrf->vrf_choice ) {
1241                 case LDAP_FILTER_PRESENT:
1242                         break;
1243
1244                 case LDAP_FILTER_EQUALITY:
1245                 case LDAP_FILTER_GE:
1246                 case LDAP_FILTER_LE:
1247                 case LDAP_FILTER_APPROX:
1248                         ava_free( op, vrf->vrf_ava, 1 );
1249                         break;
1250
1251                 case LDAP_FILTER_SUBSTRINGS:
1252                         if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1253                                 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1254                         }
1255                         ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1256                         if ( vrf->vrf_sub_final.bv_val != NULL ) {
1257                                 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1258                         }
1259                         op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1260                         break;
1261
1262                 case LDAP_FILTER_EXT:
1263                         mra_free( op, vrf->vrf_mra, 1 );
1264                         break;
1265
1266                 case SLAPD_FILTER_COMPUTED:
1267                         break;
1268
1269                 default:
1270 #ifdef NEW_LOGGING
1271                         LDAP_LOG( FILTER, ERR, "filter_free: unknown filter type %lu\n",
1272                                 vrf->vrf_choice, 0, 0 );
1273 #else
1274                         Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1275                                 vrf->vrf_choice, 0, 0 );
1276 #endif
1277                         break;
1278                 }
1279
1280                 op->o_tmpfree( vrf, op->o_tmpmemctx );
1281         }
1282 }
1283
1284 void
1285 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1286 {
1287         ValuesReturnFilter      *p;
1288         struct berval tmp;
1289         ber_len_t len;
1290
1291         if ( vrf == NULL ) {
1292                 ber_str2bv_x( "No filter!", sizeof("No filter!")-1,
1293                         1, fstr, op->o_tmpmemctx );
1294                 return;
1295         }
1296
1297         fstr->bv_len = sizeof("()") - 1;
1298         fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1299
1300         snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1301
1302         for ( p = vrf; p != NULL; p = p->vrf_next ) {
1303                 len = fstr->bv_len;
1304
1305                 simple_vrFilter2bv( op, p, &tmp );
1306                         
1307                 fstr->bv_len += tmp.bv_len;
1308                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1309                         op->o_tmpmemctx );
1310
1311                 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, 
1312                         /*"("*/ "%s)", tmp.bv_val );
1313
1314                 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1315         }
1316 }
1317
1318 static void
1319 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1320 {
1321         struct berval tmp;
1322         ber_len_t len;
1323
1324         if ( vrf == NULL ) {
1325                 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr,
1326                         op->o_tmpmemctx );
1327                 return;
1328         }
1329
1330         switch ( vrf->vrf_choice ) {
1331         case LDAP_FILTER_EQUALITY:
1332                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1333
1334                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1335                         tmp.bv_len + ( sizeof("(=)") - 1 );
1336                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1337
1338                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1339                         vrf->vrf_av_desc->ad_cname.bv_val,
1340                         tmp.bv_val );
1341
1342                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1343                 break;
1344
1345         case LDAP_FILTER_GE:
1346                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1347
1348                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1349                         tmp.bv_len + ( sizeof("(>=)") - 1 );
1350                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1351
1352                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1353                         vrf->vrf_av_desc->ad_cname.bv_val,
1354                         tmp.bv_val );
1355
1356                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1357                 break;
1358
1359         case LDAP_FILTER_LE:
1360                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1361
1362                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1363                         tmp.bv_len + ( sizeof("(<=)") - 1 );
1364                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1365
1366                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1367                         vrf->vrf_av_desc->ad_cname.bv_val,
1368                         tmp.bv_val );
1369
1370                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1371                 break;
1372
1373         case LDAP_FILTER_APPROX:
1374                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1375
1376                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1377                         tmp.bv_len + ( sizeof("(~=)") - 1 );
1378                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1379
1380                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1381                         vrf->vrf_av_desc->ad_cname.bv_val,
1382                         tmp.bv_val );
1383                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1384                 break;
1385
1386         case LDAP_FILTER_SUBSTRINGS:
1387                 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1388                         ( sizeof("(=*)") - 1 );
1389                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1390
1391                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1392                         vrf->vrf_sub_desc->ad_cname.bv_val );
1393
1394                 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1395                         len = fstr->bv_len;
1396
1397                         filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1398
1399                         fstr->bv_len += tmp.bv_len;
1400                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1401                                 op->o_tmpmemctx );
1402
1403                         snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1404                                 /* "(attr=" */ "%s*)",
1405                                 tmp.bv_val );
1406
1407                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1408                 }
1409
1410                 if ( vrf->vrf_sub_any != NULL ) {
1411                         int i;
1412                         for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1413                                 len = fstr->bv_len;
1414                                 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1415                                         op->o_tmpmemctx );
1416
1417                                 fstr->bv_len += tmp.bv_len + 1;
1418                                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1419                                         fstr->bv_len + 1, op->o_tmpmemctx );
1420
1421                                 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1422                                         /* "(attr=[init]*[any*]" */ "%s*)",
1423                                         tmp.bv_val );
1424                                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1425                         }
1426                 }
1427
1428                 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1429                         len = fstr->bv_len;
1430
1431                         filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1432
1433                         fstr->bv_len += tmp.bv_len;
1434                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1435                                 op->o_tmpmemctx );
1436
1437                         snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1438                                 /* "(attr=[init*][any*]" */ "%s)",
1439                                 tmp.bv_val );
1440
1441                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1442                 }
1443
1444                 break;
1445
1446         case LDAP_FILTER_PRESENT:
1447                 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1448                         ( sizeof("(=*)") - 1 );
1449                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1450
1451                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1452                         vrf->vrf_desc->ad_cname.bv_val );
1453                 break;
1454
1455         case LDAP_FILTER_EXT: {
1456                 struct berval ad;
1457                 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1458
1459                 if ( vrf->vrf_mr_desc ) {
1460                         ad = vrf->vrf_mr_desc->ad_cname;
1461                 } else {
1462                         ad.bv_len = 0;
1463                         ad.bv_val = "";
1464                 }
1465                         
1466                 fstr->bv_len = ad.bv_len +
1467                         ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1468                         ( vrf->vrf_mr_rule_text.bv_len
1469                                 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1470                         tmp.bv_len + ( sizeof("(:=)") - 1 );
1471                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1472
1473                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1474                         ad.bv_val,
1475                         vrf->vrf_mr_dnattrs ? ":dn" : "",
1476                         vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1477                         vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1478                         tmp.bv_val );
1479
1480                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1481                 } break;
1482
1483         case SLAPD_FILTER_COMPUTED:
1484                 ber_str2bv_x(
1485                         vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1486                         vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1487                         vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1488                                 ? "(?=undefined)" : "(?=error)",
1489                         vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1490                         vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1491                         vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1492                                 ? sizeof("(?=undefined)")-1 : sizeof("(?=error)")-1,
1493                         1, fstr, op->o_tmpmemctx );
1494                 break;
1495
1496         default:
1497                 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1,
1498                         1, fstr, op->o_tmpmemctx );
1499                 break;
1500         }
1501 }