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