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