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