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