]> git.sur5r.net Git - openldap/blob - servers/slapd/filter.c
0a76e2bdd617ad181957297858a88747d6e15c50
[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                 *text = NULL;
482                 rc = LDAP_SUCCESS;
483         }
484
485         if( rc == LDAP_SUCCESS ) {
486                 f->f_sub = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
487                 *f->f_sub = ssa;
488         }
489
490         Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
491         return rc /* LDAP_SUCCESS */ ;
492 }
493
494 void
495 filter_free_x( Operation *op, Filter *f )
496 {
497         Filter  *p, *next;
498
499         if ( f == NULL ) {
500                 return;
501         }
502
503         f->f_choice &= SLAPD_FILTER_MASK;
504
505         switch ( f->f_choice ) {
506         case LDAP_FILTER_PRESENT:
507                 break;
508
509         case LDAP_FILTER_EQUALITY:
510         case LDAP_FILTER_GE:
511         case LDAP_FILTER_LE:
512         case LDAP_FILTER_APPROX:
513                 ava_free( op, f->f_ava, 1 );
514                 break;
515
516         case LDAP_FILTER_SUBSTRINGS:
517                 if ( f->f_sub_initial.bv_val != NULL ) {
518                         op->o_tmpfree( f->f_sub_initial.bv_val, op->o_tmpmemctx );
519                 }
520                 ber_bvarray_free_x( f->f_sub_any, op->o_tmpmemctx );
521                 if ( f->f_sub_final.bv_val != NULL ) {
522                         op->o_tmpfree( f->f_sub_final.bv_val, op->o_tmpmemctx );
523                 }
524                 if ( f->f_sub->sa_desc->ad_flags & SLAP_DESC_TEMPORARY )
525                         op->o_tmpfree( f->f_sub->sa_desc, op->o_tmpmemctx );
526                 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
527                 break;
528
529         case LDAP_FILTER_AND:
530         case LDAP_FILTER_OR:
531         case LDAP_FILTER_NOT:
532                 for ( p = f->f_list; p != NULL; p = next ) {
533                         next = p->f_next;
534                         filter_free_x( op, p );
535                 }
536                 break;
537
538         case LDAP_FILTER_EXT:
539                 mra_free( op, f->f_mra, 1 );
540                 break;
541
542         case SLAPD_FILTER_COMPUTED:
543                 break;
544
545         default:
546                 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
547                         f->f_choice, 0, 0 );
548                 break;
549         }
550
551         op->o_tmpfree( f, op->o_tmpmemctx );
552 }
553
554 void
555 filter_free( Filter *f )
556 {
557         Operation op;
558         Opheader ohdr;
559
560         op.o_hdr = &ohdr;
561         op.o_tmpmemctx = slap_sl_context( f );
562         op.o_tmpmfuncs = &slap_sl_mfuncs;
563         filter_free_x( &op, f );
564 }
565
566 void
567 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
568 {
569         int             i;
570         Filter          *p;
571         struct berval   tmp, value;
572         static struct berval
573                         ber_bvfalse = BER_BVC( "(?=false)" ),
574                         ber_bvtrue = BER_BVC( "(?=true)" ),
575                         ber_bvundefined = BER_BVC( "(?=undefined)" ),
576                         ber_bverror = BER_BVC( "(?=error)" ),
577                         ber_bvunknown = BER_BVC( "(?=unknown)" ),
578                         ber_bvnone = BER_BVC( "(?=none)" );
579         ber_len_t       len;
580         ber_tag_t       choice;
581         int undef;
582         char *sign;
583
584         if ( f == NULL ) {
585                 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
586                 return;
587         }
588
589         undef = f->f_choice & SLAPD_FILTER_UNDEFINED;
590         choice = f->f_choice & SLAPD_FILTER_MASK;
591
592         switch ( choice ) {
593         case LDAP_FILTER_EQUALITY:
594                 fstr->bv_len = STRLENOF("(=)");
595                 sign = "=";
596                 goto simple;
597         case LDAP_FILTER_GE:
598                 fstr->bv_len = STRLENOF("(>=)");
599                 sign = ">=";
600                 goto simple;
601         case LDAP_FILTER_LE:
602                 fstr->bv_len = STRLENOF("(<=)");
603                 sign = "<=";
604                 goto simple;
605         case LDAP_FILTER_APPROX:
606                 fstr->bv_len = STRLENOF("(~=)");
607                 sign = "~=";
608
609 simple:
610                 value = f->f_av_value;
611                 if ( f->f_av_desc->ad_type->sat_equality &&
612                         !undef &&
613                         ( f->f_av_desc->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ))
614                 {
615                         f->f_av_desc->ad_type->sat_equality->smr_normalize(
616                                 (SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX),
617                                 NULL, NULL, &f->f_av_value, &value, op->o_tmpmemctx );
618                 }
619
620                 filter_escape_value_x( &value, &tmp, op->o_tmpmemctx );
621                 /* NOTE: tmp can legitimately be NULL (meaning empty) 
622                  * since in a Filter values in AVAs are supposed
623                  * to have been normalized, meaning that an empty value
624                  * is legal for that attribute's syntax */
625
626                 fstr->bv_len += f->f_av_desc->ad_cname.bv_len + tmp.bv_len;
627                 if ( undef )
628                         fstr->bv_len++;
629                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
630
631                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s)",
632                         undef ? "?" : "",
633                         f->f_av_desc->ad_cname.bv_val, sign,
634                         tmp.bv_len ? tmp.bv_val : "" );
635
636                 if ( value.bv_val != f->f_av_value.bv_val ) {
637                         ber_memfree_x( value.bv_val, op->o_tmpmemctx );
638                 }
639
640                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
641                 break;
642
643         case LDAP_FILTER_SUBSTRINGS:
644                 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
645                         STRLENOF("(=*)");
646                 if ( undef )
647                         fstr->bv_len++;
648                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
649
650                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
651                         undef ? "?" : "",
652                         f->f_sub_desc->ad_cname.bv_val );
653
654                 if ( f->f_sub_initial.bv_val != NULL ) {
655                         ber_len_t tmplen;
656
657                         len = fstr->bv_len;
658
659                         filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
660                         tmplen = tmp.bv_len;
661
662                         fstr->bv_len += tmplen;
663                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
664                                 fstr->bv_len + 1, op->o_tmpmemctx );
665
666                         snprintf( &fstr->bv_val[len - 2],
667                                 tmplen + STRLENOF( /*(*/ "*)" ) + 1,
668                                 /* "(attr=" */ "%s*)",
669                                 tmp.bv_len ? tmp.bv_val : "");
670
671                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
672                 }
673
674                 if ( f->f_sub_any != NULL ) {
675                         for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
676                                 ber_len_t tmplen;
677
678                                 len = fstr->bv_len;
679                                 filter_escape_value_x( &f->f_sub_any[i],
680                                         &tmp, op->o_tmpmemctx );
681                                 tmplen = tmp.bv_len;
682
683                                 fstr->bv_len += tmplen + STRLENOF( /*(*/ ")" );
684                                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
685                                         fstr->bv_len + 1, op->o_tmpmemctx );
686
687                                 snprintf( &fstr->bv_val[len - 1],
688                                         tmplen + STRLENOF( /*(*/ "*)" ) + 1,
689                                         /* "(attr=[init]*[any*]" */ "%s*)",
690                                         tmp.bv_len ? tmp.bv_val : "");
691                                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
692                         }
693                 }
694
695                 if ( f->f_sub_final.bv_val != NULL ) {
696                         ber_len_t tmplen;
697
698                         len = fstr->bv_len;
699
700                         filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
701                         tmplen = tmp.bv_len;
702
703                         fstr->bv_len += tmplen;
704                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
705                                 fstr->bv_len + 1, op->o_tmpmemctx );
706
707                         snprintf( &fstr->bv_val[len - 1],
708                                 tmplen + STRLENOF( /*(*/ ")" ) + 1,
709                                 /* "(attr=[init*][any*]" */ "%s)",
710                                 tmp.bv_len ? tmp.bv_val : "");
711
712                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
713                 }
714
715                 break;
716
717         case LDAP_FILTER_PRESENT:
718                 fstr->bv_len = f->f_desc->ad_cname.bv_len +
719                         STRLENOF("(=*)");
720                 if ( undef )
721                         fstr->bv_len++;
722
723                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
724
725                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s=*)",
726                         undef ? "?" : "",
727                         f->f_desc->ad_cname.bv_val );
728                 break;
729
730         case LDAP_FILTER_AND:
731         case LDAP_FILTER_OR:
732         case LDAP_FILTER_NOT:
733                 fstr->bv_len = STRLENOF("(%)");
734                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
735
736                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
737                         f->f_choice == LDAP_FILTER_AND ? '&' :
738                         f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
739
740                 for ( p = f->f_list; p != NULL; p = p->f_next ) {
741                         len = fstr->bv_len;
742
743                         filter2bv_x( op, p, &tmp );
744                         
745                         fstr->bv_len += tmp.bv_len;
746                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
747                                 op->o_tmpmemctx );
748
749                         snprintf( &fstr->bv_val[len-1],
750                                 tmp.bv_len + STRLENOF( /*(*/ ")" ) + 1, 
751                                 /*"("*/ "%s)", tmp.bv_val );
752
753                         op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
754                 }
755
756                 break;
757
758         case LDAP_FILTER_EXT: {
759                 struct berval ad;
760
761                 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
762                 /* NOTE: tmp can legitimately be NULL (meaning empty) 
763                  * since in a Filter values in MRAs are supposed
764                  * to have been normalized, meaning that an empty value
765                  * is legal for that attribute's syntax */
766
767                 if ( f->f_mr_desc ) {
768                         ad = f->f_mr_desc->ad_cname;
769                 } else {
770                         ad.bv_len = 0;
771                         ad.bv_val = "";
772                 }
773                 
774                 fstr->bv_len = ad.bv_len +
775                         ( f->f_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
776                         ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
777                         tmp.bv_len + STRLENOF("(:=)");
778                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
779
780                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s%s:=%s)",
781                         undef ? "?" : "",
782                         ad.bv_val,
783                         f->f_mr_dnattrs ? ":dn" : "",
784                         f->f_mr_rule_text.bv_len ? ":" : "",
785                         f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
786                         tmp.bv_len ? tmp.bv_val : "" );
787                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
788                 } break;
789
790         case SLAPD_FILTER_COMPUTED:
791                 switch ( f->f_result ) {
792                 case LDAP_COMPARE_FALSE:
793                         tmp = ber_bvfalse;
794                         break;
795
796                 case LDAP_COMPARE_TRUE:
797                         tmp = ber_bvtrue;
798                         break;
799                         
800                 case SLAPD_COMPARE_UNDEFINED:
801                         tmp = ber_bvundefined;
802                         break;
803                         
804                 default:
805                         tmp = ber_bverror;
806                         break;
807                 }
808
809                 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
810                 break;
811                 
812         default:
813                 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
814                 break;
815         }
816 }
817
818 void
819 filter2bv( Filter *f, struct berval *fstr )
820 {
821         Operation op;
822         Opheader ohdr;
823
824         op.o_hdr = &ohdr;
825         op.o_tmpmemctx = NULL;
826         op.o_tmpmfuncs = &ch_mfuncs;
827
828         filter2bv_x( &op, f, fstr );
829 }
830
831 Filter *
832 filter_dup( Filter *f, void *memctx )
833 {
834         BerMemoryFunctions *mf = &slap_sl_mfuncs;
835         Filter *n;
836
837         if ( !f )
838                 return NULL;
839
840         n = mf->bmf_malloc( sizeof(Filter), memctx );
841         n->f_choice = f->f_choice;
842         n->f_next = NULL;
843
844         switch( f->f_choice & SLAPD_FILTER_MASK ) {
845         case SLAPD_FILTER_COMPUTED:
846                 n->f_result = f->f_result;
847                 break;
848         case LDAP_FILTER_PRESENT:
849                 if ( f->f_desc->ad_flags & SLAP_DESC_TEMPORARY )
850                         n->f_desc = slap_bv2tmp_ad( &f->f_desc->ad_cname, memctx );
851                 else
852                         n->f_desc = f->f_desc;
853                 break;
854         case LDAP_FILTER_EQUALITY:
855         case LDAP_FILTER_GE:
856         case LDAP_FILTER_LE:
857         case LDAP_FILTER_APPROX:
858                 /* Should this be ava_dup() ? */
859                 n->f_ava = mf->bmf_calloc( 1, sizeof(AttributeAssertion), memctx );
860                 *n->f_ava = *f->f_ava;
861                 if ( f->f_av_desc->ad_flags & SLAP_DESC_TEMPORARY )
862                         n->f_av_desc = slap_bv2tmp_ad( &f->f_av_desc->ad_cname, memctx );
863                 ber_dupbv_x( &n->f_av_value, &f->f_av_value, memctx );
864                 break;
865         case LDAP_FILTER_SUBSTRINGS:
866                 n->f_sub = mf->bmf_calloc( 1, sizeof(SubstringsAssertion), memctx );
867                 if ( f->f_sub_desc->ad_flags & SLAP_DESC_TEMPORARY )
868                         n->f_sub_desc = slap_bv2tmp_ad( &f->f_sub_desc->ad_cname, memctx );
869                 else
870                         n->f_sub_desc = f->f_sub_desc;
871                 if ( !BER_BVISNULL( &f->f_sub_initial ))
872                         ber_dupbv_x( &n->f_sub_initial, &f->f_sub_initial, memctx );
873                 if ( f->f_sub_any ) {
874                         int i;
875                         for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ );
876                         n->f_sub_any = mf->bmf_malloc(( i+1 )*sizeof( struct berval ),
877                                 memctx );
878                         for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) {
879                                 ber_dupbv_x( &n->f_sub_any[i], &f->f_sub_any[i], memctx );
880                         }
881                         BER_BVZERO( &n->f_sub_any[i] );
882                 }
883                 if ( !BER_BVISNULL( &f->f_sub_final ))
884                         ber_dupbv_x( &n->f_sub_final, &f->f_sub_final, memctx );
885                 break;
886         case LDAP_FILTER_EXT: {
887                 /* Should this be mra_dup() ? */
888                 ber_len_t length;
889                 length = sizeof(MatchingRuleAssertion);
890                 if ( !BER_BVISNULL( &f->f_mr_rule_text ))
891                         length += f->f_mr_rule_text.bv_len + 1;
892                 n->f_mra = mf->bmf_calloc( 1, length, memctx );
893                 *n->f_mra = *f->f_mra;
894                 if ( f->f_mr_desc && ( f->f_sub_desc->ad_flags & SLAP_DESC_TEMPORARY ))
895                         n->f_mr_desc = slap_bv2tmp_ad( &f->f_mr_desc->ad_cname, memctx );
896                 ber_dupbv_x( &n->f_mr_value, &f->f_mr_value, memctx );
897                 if ( !BER_BVISNULL( &f->f_mr_rule_text )) {
898                         n->f_mr_rule_text.bv_val = (char *)(n->f_mra+1);
899                         AC_MEMCPY(n->f_mr_rule_text.bv_val,
900                                 f->f_mr_rule_text.bv_val, f->f_mr_rule_text.bv_len );
901                 }
902                 } break;
903         case LDAP_FILTER_AND:
904         case LDAP_FILTER_OR:
905         case LDAP_FILTER_NOT: {
906                 Filter **p;
907                 for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) {
908                         *p = filter_dup( f, memctx );
909                         p = &(*p)->f_next;
910                 }
911                 } break;
912         }
913         return n;
914 }
915
916 static int
917 get_simple_vrFilter(
918         Operation *op,
919         BerElement *ber,
920         ValuesReturnFilter **filt,
921         const char **text )
922 {
923         ber_tag_t       tag;
924         ber_len_t       len;
925         int             err;
926         ValuesReturnFilter vrf;
927
928         Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
929
930         tag = ber_peek_tag( ber, &len );
931
932         if( tag == LBER_ERROR ) {
933                 *text = "error decoding filter";
934                 return SLAPD_DISCONNECT;
935         }
936
937         vrf.vrf_next = NULL;
938
939         err = LDAP_SUCCESS;
940         vrf.vrf_choice = tag; 
941
942         switch ( vrf.vrf_choice ) {
943         case LDAP_FILTER_EQUALITY:
944                 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
945                 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY, text );
946                 if ( err != LDAP_SUCCESS ) {
947                         break;
948                 }
949
950                 assert( vrf.vrf_ava != NULL );
951                 break;
952
953         case LDAP_FILTER_SUBSTRINGS:
954                 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
955                 err = get_ssa( op, ber, (Filter *)&vrf, text );
956                 break;
957
958         case LDAP_FILTER_GE:
959                 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
960                 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
961                 if ( err != LDAP_SUCCESS ) {
962                         break;
963                 }
964                 break;
965
966         case LDAP_FILTER_LE:
967                 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
968                 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_ORDERING, text );
969                 if ( err != LDAP_SUCCESS ) {
970                         break;
971                 }
972                 break;
973
974         case LDAP_FILTER_PRESENT: {
975                 struct berval type;
976
977                 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
978                 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
979                         err = SLAPD_DISCONNECT;
980                         *text = "error decoding filter";
981                         break;
982                 }
983
984                 vrf.vrf_desc = NULL;
985                 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
986
987                 if( err != LDAP_SUCCESS ) {
988                         vrf.vrf_choice |= SLAPD_FILTER_UNDEFINED;
989                         err = slap_bv2undef_ad( &type, &vrf.vrf_desc, text,
990                                 SLAP_AD_PROXIED);
991
992                         if( err != LDAP_SUCCESS ) {
993                                 /* unrecognized attribute description or other error */
994                                 Debug( LDAP_DEBUG_ANY, 
995                                         "get_simple_vrFilter: conn %lu unknown "
996                                         "attribute type=%s (%d)\n",
997                                         op->o_connid, type.bv_val, err );
998         
999                                 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1000                                 vrf.vrf_result = LDAP_COMPARE_FALSE;
1001                                 err = LDAP_SUCCESS;
1002                                 break;
1003                         }
1004                 }
1005                 } break;
1006
1007         case LDAP_FILTER_APPROX:
1008                 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
1009                 err = get_ava( op, ber, (Filter *)&vrf, SLAP_MR_EQUALITY_APPROX, text );
1010                 if ( err != LDAP_SUCCESS ) {
1011                         break;
1012                 }
1013                 break;
1014
1015         case LDAP_FILTER_EXT:
1016                 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1017
1018                 err = get_mra( op, ber, (Filter *)&vrf, text );
1019                 if ( err != LDAP_SUCCESS ) {
1020                         break;
1021                 }
1022
1023                 assert( vrf.vrf_mra != NULL );
1024                 break;
1025
1026         default:
1027                 (void) ber_scanf( ber, "x" ); /* skip the element */
1028                 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1029                         vrf.vrf_choice, 0, 0 );
1030                 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1031                 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1032                 break;
1033         }
1034
1035         if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1036                 /* ignore error */
1037                 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1038                 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1039                 err = LDAP_SUCCESS;
1040         }
1041
1042         if ( err == LDAP_SUCCESS ) {
1043                 *filt = op->o_tmpalloc( sizeof vrf, op->o_tmpmemctx );
1044                 **filt = vrf;
1045         }
1046
1047         Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1048
1049         return err;
1050 }
1051
1052 int
1053 get_vrFilter( Operation *op, BerElement *ber,
1054         ValuesReturnFilter **vrf,
1055         const char **text )
1056 {
1057         /*
1058          * A ValuesReturnFilter looks like this:
1059          *
1060          *      ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1061          *      SimpleFilterItem ::= CHOICE {
1062          *              equalityMatch   [3]     AttributeValueAssertion,
1063          *              substrings      [4]     SubstringFilter,
1064          *              greaterOrEqual  [5]     AttributeValueAssertion,
1065          *              lessOrEqual     [6]     AttributeValueAssertion,
1066          *              present         [7]     AttributeType,
1067          *              approxMatch     [8]     AttributeValueAssertion,
1068          *              extensibleMatch [9]     SimpleMatchingAssertion -- LDAPv3
1069          *      }
1070          *
1071          *      SubstringFilter ::= SEQUENCE {
1072          *              type               AttributeType,
1073          *              SEQUENCE OF CHOICE {
1074          *                      initial          [0] IA5String,
1075          *                      any              [1] IA5String,
1076          *                      final            [2] IA5String
1077          *              }
1078          *      }
1079          *
1080          *      SimpleMatchingAssertion ::= SEQUENCE {  -- LDAPv3
1081          *              matchingRule    [1] MatchingRuleId OPTIONAL,
1082          *              type            [2] AttributeDescription OPTIONAL,
1083          *              matchValue      [3] AssertionValue }
1084          */
1085
1086         ValuesReturnFilter **n;
1087         ber_tag_t       tag;
1088         ber_len_t       len;
1089         char            *last;
1090
1091         Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1092
1093         tag = ber_peek_tag( ber, &len );
1094
1095         if( tag == LBER_ERROR ) {
1096                 *text = "error decoding vrFilter";
1097                 return SLAPD_DISCONNECT;
1098         }
1099
1100         if( tag != LBER_SEQUENCE ) {
1101                 *text = "error decoding vrFilter, expect SEQUENCE tag";
1102                 return SLAPD_DISCONNECT;
1103         }
1104
1105         n = vrf;
1106         for ( tag = ber_first_element( ber, &len, &last );
1107                 tag != LBER_DEFAULT;
1108                 tag = ber_next_element( ber, &len, last ) )
1109         {
1110                 int err = get_simple_vrFilter( op, ber, n, text );
1111
1112                 if ( err != LDAP_SUCCESS ) return( err );
1113
1114                 n = &(*n)->vrf_next;
1115         }
1116         *n = NULL;
1117
1118         Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1119         return( LDAP_SUCCESS );
1120 }
1121
1122 void
1123 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1124 {
1125         ValuesReturnFilter      *p, *next;
1126
1127         if ( vrf == NULL ) {
1128                 return;
1129         }
1130
1131         for ( p = vrf; p != NULL; p = next ) {
1132                 next = p->vrf_next;
1133
1134                 switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1135                 case LDAP_FILTER_PRESENT:
1136                         break;
1137
1138                 case LDAP_FILTER_EQUALITY:
1139                 case LDAP_FILTER_GE:
1140                 case LDAP_FILTER_LE:
1141                 case LDAP_FILTER_APPROX:
1142                         ava_free( op, vrf->vrf_ava, 1 );
1143                         break;
1144
1145                 case LDAP_FILTER_SUBSTRINGS:
1146                         if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1147                                 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1148                         }
1149                         ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1150                         if ( vrf->vrf_sub_final.bv_val != NULL ) {
1151                                 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1152                         }
1153                         op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1154                         break;
1155
1156                 case LDAP_FILTER_EXT:
1157                         mra_free( op, vrf->vrf_mra, 1 );
1158                         break;
1159
1160                 case SLAPD_FILTER_COMPUTED:
1161                         break;
1162
1163                 default:
1164                         Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1165                                 vrf->vrf_choice, 0, 0 );
1166                         break;
1167                 }
1168
1169                 op->o_tmpfree( vrf, op->o_tmpmemctx );
1170         }
1171 }
1172
1173 void
1174 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1175 {
1176         ValuesReturnFilter      *p;
1177         struct berval tmp;
1178         ber_len_t len;
1179
1180         if ( vrf == NULL ) {
1181                 ber_str2bv_x( "No filter!", STRLENOF("No filter!"),
1182                         1, fstr, op->o_tmpmemctx );
1183                 return;
1184         }
1185
1186         fstr->bv_len = STRLENOF("()");
1187         fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1188
1189         snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1190
1191         for ( p = vrf; p != NULL; p = p->vrf_next ) {
1192                 len = fstr->bv_len;
1193
1194                 simple_vrFilter2bv( op, p, &tmp );
1195                         
1196                 fstr->bv_len += tmp.bv_len;
1197                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1198                         op->o_tmpmemctx );
1199
1200                 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2, 
1201                         /*"("*/ "%s)", tmp.bv_val );
1202
1203                 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1204         }
1205 }
1206
1207 static void
1208 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1209 {
1210         struct berval tmp;
1211         ber_len_t len;
1212         int undef;
1213
1214         if ( vrf == NULL ) {
1215                 ber_str2bv_x( "No filter!", STRLENOF("No filter!"), 1, fstr,
1216                         op->o_tmpmemctx );
1217                 return;
1218         }
1219         undef = vrf->vrf_choice & SLAPD_FILTER_UNDEFINED;
1220
1221         switch ( vrf->vrf_choice & SLAPD_FILTER_MASK ) {
1222         case LDAP_FILTER_EQUALITY:
1223                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1224
1225                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1226                         tmp.bv_len + STRLENOF("(=)");
1227                 if ( undef ) fstr->bv_len++;
1228                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1229
1230                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1231                         vrf->vrf_av_desc->ad_cname.bv_val,
1232                         tmp.bv_val );
1233
1234                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1235                 break;
1236
1237         case LDAP_FILTER_GE:
1238                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1239
1240                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1241                         tmp.bv_len + STRLENOF("(>=)");
1242                 if ( undef ) fstr->bv_len++;
1243                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1244
1245                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1246                         vrf->vrf_av_desc->ad_cname.bv_val,
1247                         tmp.bv_val );
1248
1249                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1250                 break;
1251
1252         case LDAP_FILTER_LE:
1253                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1254
1255                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1256                         tmp.bv_len + STRLENOF("(<=)");
1257                 if ( undef ) fstr->bv_len++;
1258                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1259
1260                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1261                         vrf->vrf_av_desc->ad_cname.bv_val,
1262                         tmp.bv_val );
1263
1264                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1265                 break;
1266
1267         case LDAP_FILTER_APPROX:
1268                 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1269
1270                 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1271                         tmp.bv_len + STRLENOF("(~=)");
1272                 if ( undef ) fstr->bv_len++;
1273                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1274
1275                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1276                         vrf->vrf_av_desc->ad_cname.bv_val,
1277                         tmp.bv_val );
1278                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1279                 break;
1280
1281         case LDAP_FILTER_SUBSTRINGS:
1282                 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1283                         STRLENOF("(=*)");
1284                 if ( undef ) fstr->bv_len++;
1285                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1286
1287                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1288                         vrf->vrf_sub_desc->ad_cname.bv_val );
1289
1290                 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1291                         len = fstr->bv_len;
1292
1293                         filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1294
1295                         fstr->bv_len += tmp.bv_len;
1296                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1297                                 op->o_tmpmemctx );
1298
1299                         snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1300                                 /* "(attr=" */ "%s*)",
1301                                 tmp.bv_val );
1302
1303                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1304                 }
1305
1306                 if ( vrf->vrf_sub_any != NULL ) {
1307                         int i;
1308                         for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1309                                 len = fstr->bv_len;
1310                                 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1311                                         op->o_tmpmemctx );
1312
1313                                 fstr->bv_len += tmp.bv_len + 1;
1314                                 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1315                                         fstr->bv_len + 1, op->o_tmpmemctx );
1316
1317                                 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1318                                         /* "(attr=[init]*[any*]" */ "%s*)",
1319                                         tmp.bv_val );
1320                                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1321                         }
1322                 }
1323
1324                 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1325                         len = fstr->bv_len;
1326
1327                         filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1328
1329                         fstr->bv_len += tmp.bv_len;
1330                         fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1331                                 op->o_tmpmemctx );
1332
1333                         snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1334                                 /* "(attr=[init*][any*]" */ "%s)",
1335                                 tmp.bv_val );
1336
1337                         ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1338                 }
1339
1340                 break;
1341
1342         case LDAP_FILTER_PRESENT:
1343                 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1344                         STRLENOF("(=*)");
1345                 if ( undef ) fstr->bv_len++;
1346                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1347
1348                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1349                         vrf->vrf_desc->ad_cname.bv_val );
1350                 break;
1351
1352         case LDAP_FILTER_EXT: {
1353                 struct berval ad;
1354                 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1355
1356                 if ( vrf->vrf_mr_desc ) {
1357                         ad = vrf->vrf_mr_desc->ad_cname;
1358                 } else {
1359                         ad.bv_len = 0;
1360                         ad.bv_val = "";
1361                 }
1362                         
1363                 fstr->bv_len = ad.bv_len +
1364                         ( vrf->vrf_mr_dnattrs ? STRLENOF(":dn") : 0 ) +
1365                         ( vrf->vrf_mr_rule_text.bv_len
1366                                 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1367                         tmp.bv_len + STRLENOF("(:=)");
1368                 if ( undef ) fstr->bv_len++;
1369                 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1370
1371                 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1372                         ad.bv_val,
1373                         vrf->vrf_mr_dnattrs ? ":dn" : "",
1374                         vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1375                         vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1376                         tmp.bv_val );
1377
1378                 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1379                 } break;
1380
1381         case SLAPD_FILTER_COMPUTED:
1382                 ber_str2bv_x(
1383                         vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1384                         vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1385                         vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1386                                 ? "(?=undefined)" : "(?=error)",
1387                         vrf->vrf_result == LDAP_COMPARE_FALSE ? STRLENOF("(?=false)") :
1388                         vrf->vrf_result == LDAP_COMPARE_TRUE ? STRLENOF("(?=true)") :
1389                         vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1390                                 ? STRLENOF("(?=undefined)") : STRLENOF("(?=error)"),
1391                         1, fstr, op->o_tmpmemctx );
1392                 break;
1393
1394         default:
1395                 ber_str2bv_x( "(?=unknown)", STRLENOF("(?=unknown)"),
1396                         1, fstr, op->o_tmpmemctx );
1397                 break;
1398         }
1399 }