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