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