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