1 /* filter.c - routines for parsing and dealing with filters */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
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.
31 #include <ac/socket.h>
32 #include <ac/string.h>
36 static int get_filter_list(
45 SubstringsAssertion **s,
48 static int filter_escape_value_x(
53 static void simple_vrFilter2bv(
55 ValuesReturnFilter *f,
56 struct berval *fstr );
58 static int get_simple_vrFilter(
61 ValuesReturnFilter **f,
77 LDAP_LOG( FILTER, ENTRY, "get_filter: conn %d\n", op->o_connid, 0, 0 );
79 Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 );
82 * A filter looks like this coming in:
84 * and [0] SET OF Filter,
85 * or [1] SET OF 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
96 * SubstringFilter ::= SEQUENCE {
98 * SEQUENCE OF CHOICE {
99 * initial [0] IA5String,
101 * final [2] IA5String
105 * MatchingRuleAssertion ::= SEQUENCE {
106 * matchingRule [1] MatchingRuleId OPTIONAL,
107 * type [2] AttributeDescription OPTIONAL,
108 * matchValue [3] AssertionValue,
109 * dnAttributes [4] BOOLEAN DEFAULT FALSE
114 tag = ber_peek_tag( ber, &len );
116 if( tag == LBER_ERROR ) {
117 *text = "error decoding filter";
118 return SLAPD_DISCONNECT;
126 switch ( f.f_choice ) {
127 case LDAP_FILTER_EQUALITY:
129 LDAP_LOG( FILTER, DETAIL2,
130 "get_filter: conn %d EQUALITY\n", op->o_connid, 0, 0 );
132 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
134 err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY, text );
135 if ( err != LDAP_SUCCESS ) {
139 assert( f.f_ava != NULL );
142 case LDAP_FILTER_SUBSTRINGS:
144 LDAP_LOG( FILTER, DETAIL1,
145 "get_filter: conn %d SUBSTRINGS\n", op->o_connid, 0, 0 );
147 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
149 err = get_ssa( op, ber, &f.f_sub, text );
150 if( err != LDAP_SUCCESS ) {
153 assert( f.f_sub != NULL );
158 LDAP_LOG( FILTER, DETAIL1,
159 "get_filter: conn %d GE\n", op->o_connid, 0, 0 );
161 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
163 err = get_ava( op, ber, &f.f_ava, SLAP_MR_ORDERING, text );
164 if ( err != LDAP_SUCCESS ) {
167 assert( f.f_ava != NULL );
172 LDAP_LOG( FILTER, DETAIL1,
173 "get_filter: conn %d LE\n", op->o_connid, 0, 0 );
175 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
177 err = get_ava( op, ber, &f.f_ava, SLAP_MR_ORDERING, text );
178 if ( err != LDAP_SUCCESS ) {
181 assert( f.f_ava != NULL );
184 case LDAP_FILTER_PRESENT: {
188 LDAP_LOG( FILTER, DETAIL1,
189 "get_filter: conn %d PRESENT\n", op->o_connid, 0, 0 );
191 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
193 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
194 err = SLAPD_DISCONNECT;
195 *text = "error decoding filter";
200 err = slap_bv2ad( &type, &f.f_desc, text );
202 if( err != LDAP_SUCCESS ) {
203 /* unrecognized attribute description or other error */
205 LDAP_LOG( FILTER, ERR,
206 "get_filter: conn %d unknown attribute "
208 op->o_connid, type.bv_val, err );
210 Debug( LDAP_DEBUG_ANY,
211 "get_filter: conn %d unknown attribute "
213 op->o_connid, type.bv_val, err );
216 f.f_choice = SLAPD_FILTER_COMPUTED;
217 f.f_result = LDAP_COMPARE_FALSE;
223 assert( f.f_desc != NULL );
226 case LDAP_FILTER_APPROX:
228 LDAP_LOG( FILTER, DETAIL1,
229 "get_filter: conn %d APPROX\n", op->o_connid, 0, 0 );
231 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
233 err = get_ava( op, ber, &f.f_ava, SLAP_MR_EQUALITY_APPROX, text );
234 if ( err != LDAP_SUCCESS ) {
237 assert( f.f_ava != NULL );
240 case LDAP_FILTER_AND:
242 LDAP_LOG( FILTER, DETAIL1,
243 "get_filter: conn %d AND\n", op->o_connid, 0, 0 );
245 Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 );
247 err = get_filter_list( op, ber, &f.f_and, text );
248 if ( err != LDAP_SUCCESS ) {
251 if ( f.f_and == NULL ) {
252 f.f_choice = SLAPD_FILTER_COMPUTED;
253 f.f_result = LDAP_COMPARE_TRUE;
255 /* no assert - list could be empty */
260 LDAP_LOG( FILTER, DETAIL1,
261 "get_filter: conn %d OR\n", op->o_connid, 0, 0 );
263 Debug( LDAP_DEBUG_FILTER, "OR\n", 0, 0, 0 );
265 err = get_filter_list( op, ber, &f.f_or, text );
266 if ( err != LDAP_SUCCESS ) {
269 if ( f.f_or == NULL ) {
270 f.f_choice = SLAPD_FILTER_COMPUTED;
271 f.f_result = LDAP_COMPARE_FALSE;
273 /* no assert - list could be empty */
276 case LDAP_FILTER_NOT:
278 LDAP_LOG( FILTER, DETAIL1,
279 "get_filter: conn %d NOT\n", op->o_connid, 0, 0 );
281 Debug( LDAP_DEBUG_FILTER, "NOT\n", 0, 0, 0 );
283 (void) ber_skip_tag( ber, &len );
284 err = get_filter( op, ber, &f.f_not, text );
285 if ( err != LDAP_SUCCESS ) {
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 );
297 case LDAP_COMPARE_TRUE:
298 f.f_result = LDAP_COMPARE_FALSE;
300 case LDAP_COMPARE_FALSE:
301 f.f_result = LDAP_COMPARE_TRUE;
304 /* (!Undefined) is Undefined */
309 case LDAP_FILTER_EXT:
311 LDAP_LOG( FILTER, DETAIL1,
312 "get_filter: conn %d EXTENSIBLE\n", op->o_connid, 0, 0 );
314 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
317 err = get_mra( op, ber, &f.f_mra, text );
318 if ( err != LDAP_SUCCESS ) {
322 assert( f.f_mra != NULL );
326 (void) ber_scanf( ber, "x" ); /* skip the element */
328 LDAP_LOG( FILTER, ERR,
329 "get_filter: conn %d unknown filter type=%lu\n",
330 op->o_connid, f.f_choice, 0 );
332 Debug( LDAP_DEBUG_ANY, "get_filter: unknown filter type=%lu\n",
335 f.f_choice = SLAPD_FILTER_COMPUTED;
336 f.f_result = SLAPD_COMPARE_UNDEFINED;
340 if( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
343 f.f_choice = SLAPD_FILTER_COMPUTED;
344 f.f_result = SLAPD_COMPARE_UNDEFINED;
348 if ( err == LDAP_SUCCESS ) {
349 *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx );
354 LDAP_LOG( FILTER, DETAIL2,
355 "get_filter: conn %d exit\n", op->o_connid, 0, 0 );
357 Debug( LDAP_DEBUG_FILTER, "end get_filter %d\n", err, 0, 0 );
364 get_filter_list( Operation *op, BerElement *ber,
375 LDAP_LOG( FILTER, ENTRY,
376 "get_filter_list: conn %d start\n", op->o_connid, 0, 0 );
378 Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 );
381 for ( tag = ber_first_element( ber, &len, &last );
383 tag = ber_next_element( ber, &len, last ) )
385 err = get_filter( op, ber, new, text );
386 if ( err != LDAP_SUCCESS )
388 new = &(*new)->f_next;
393 LDAP_LOG( FILTER, ENTRY,
394 "get_filter_list: conn %d exit\n", op->o_connid, 0, 0 );
396 Debug( LDAP_DEBUG_FILTER, "end get_filter_list\n", 0, 0, 0 );
398 return( LDAP_SUCCESS );
405 SubstringsAssertion **out,
411 struct berval desc, value, nvalue;
413 SubstringsAssertion ssa;
415 *text = "error decoding filter";
419 LDAP_LOG( FILTER, ENTRY,
420 "get_ssa: conn %d begin\n", op->o_connid, 0, 0 );
422 Debug( LDAP_DEBUG_FILTER, "begin get_ssa\n", 0, 0, 0 );
424 if ( ber_scanf( ber, "{m" /*}*/, &desc ) == LBER_ERROR ) {
425 return SLAPD_DISCONNECT;
431 ssa.sa_initial.bv_val = NULL;
433 ssa.sa_final.bv_val = NULL;
435 rc = slap_bv2ad( &desc, &ssa.sa_desc, text );
437 if( rc != LDAP_SUCCESS ) {
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 );
443 Debug( LDAP_DEBUG_ANY,
444 "get_ssa: conn %d unknown attribute type=%s (%d)\n",
445 op->o_connid, desc.bv_val, rc );
448 /* skip over the rest of this filter */
449 for ( tag = ber_first_element( ber, &len, &last );
451 tag = ber_next_element( ber, &len, last ) ) {
452 ber_scanf( ber, "x" );
457 rc = LDAP_PROTOCOL_ERROR;
459 for ( tag = ber_first_element( ber, &len, &last );
461 tag = ber_next_element( ber, &len, last ) )
465 rc = ber_scanf( ber, "m", &value );
466 if ( rc == LBER_ERROR ) {
467 rc = SLAPD_DISCONNECT;
471 if ( value.bv_val == NULL || value.bv_len == 0 ) {
472 rc = LDAP_INVALID_SYNTAX;
477 case LDAP_SUBSTRING_INITIAL:
478 usage = SLAP_MR_SUBSTR_INITIAL;
481 case LDAP_SUBSTRING_ANY:
482 usage = SLAP_MR_SUBSTR_ANY;
485 case LDAP_SUBSTRING_FINAL:
486 usage = SLAP_MR_SUBSTR_FINAL;
490 rc = LDAP_PROTOCOL_ERROR;
493 LDAP_LOG( FILTER, ERR,
494 "get_filter_substring: conn %d unknown substring choice=%ld\n",
495 op->o_connid, (long)tag, 0 );
497 Debug( LDAP_DEBUG_FILTER,
498 " unknown substring choice=%ld\n",
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 );
510 if( rc != LDAP_SUCCESS ) {
514 rc = LDAP_PROTOCOL_ERROR;
517 case LDAP_SUBSTRING_INITIAL:
519 LDAP_LOG( FILTER, DETAIL1,
520 "get_ssa: conn %d INITIAL\n",
521 op->o_connid, 0, 0 );
523 Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 );
526 if ( ssa.sa_initial.bv_val != NULL
527 || ssa.sa_any != NULL
528 || ssa.sa_final.bv_val != NULL )
530 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
534 ssa.sa_initial = nvalue;
537 case LDAP_SUBSTRING_ANY:
539 LDAP_LOG( FILTER, DETAIL1,
540 "get_ssa: conn %d ANY\n",
541 op->o_connid, 0, 0 );
543 Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 );
546 if ( ssa.sa_final.bv_val != NULL ) {
547 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
551 ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx );
554 case LDAP_SUBSTRING_FINAL:
556 LDAP_LOG( FILTER, DETAIL1,
557 "get_ssa: conn %d FINAL\n",
558 op->o_connid, 0, 0 );
560 Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 );
563 if ( ssa.sa_final.bv_val != NULL ) {
564 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
568 ssa.sa_final = nvalue;
573 LDAP_LOG( FILTER, INFO,
574 "get_ssa: conn %d unknown substring type %ld\n",
575 op->o_connid, (long)tag, 0 );
577 Debug( LDAP_DEBUG_FILTER,
578 " unknown substring type=%ld\n",
583 slap_sl_free( nvalue.bv_val, op->o_tmpmemctx );
587 LDAP_LOG( FILTER, INFO,
588 "get_ssa: conn %d error %ld\n",
589 op->o_connid, (long)rc, 0 );
591 Debug( LDAP_DEBUG_FILTER, " error=%ld\n",
594 slap_sl_free( ssa.sa_initial.bv_val, op->o_tmpmemctx );
595 ber_bvarray_free_x( ssa.sa_any, op->o_tmpmemctx );
596 slap_sl_free( ssa.sa_final.bv_val, op->o_tmpmemctx );
603 if( rc == LDAP_SUCCESS ) {
604 *out = op->o_tmpalloc( sizeof( ssa ), op->o_tmpmemctx );
609 LDAP_LOG( FILTER, ENTRY,
610 "get_ssa: conn %d exit\n", op->o_connid, 0, 0 );
612 Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 );
615 return rc /* LDAP_SUCCESS */ ;
619 filter_free_x( Operation *op, Filter *f )
627 switch ( f->f_choice ) {
628 case LDAP_FILTER_PRESENT:
631 case LDAP_FILTER_EQUALITY:
634 case LDAP_FILTER_APPROX:
635 ava_free( op, f->f_ava, 1 );
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 );
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 );
646 op->o_tmpfree( f->f_sub, op->o_tmpmemctx );
649 case LDAP_FILTER_AND:
651 case LDAP_FILTER_NOT:
652 for ( p = f->f_list; p != NULL; p = next ) {
654 filter_free_x( op, p );
658 case LDAP_FILTER_EXT:
659 mra_free( op, f->f_mra, 1 );
662 case SLAPD_FILTER_COMPUTED:
667 LDAP_LOG( FILTER, ERR,
668 "filter_free: unknown filter type %lu\n", f->f_choice, 0, 0 );
670 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
676 op->o_tmpfree( f, op->o_tmpmemctx );
680 filter_free( Filter *f )
684 op.o_tmpmemctx = slap_sl_context( f );
685 op.o_tmpmfuncs = &slap_sl_mfuncs;
686 filter_free_x( &op, f );
690 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
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)" );
705 ber_dupbv_x( fstr, &ber_bvnone, op->o_tmpmemctx );
709 switch ( f->f_choice ) {
710 case LDAP_FILTER_EQUALITY:
711 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
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 );
717 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
718 f->f_av_desc->ad_cname.bv_val,
721 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
725 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
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 );
731 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
732 f->f_av_desc->ad_cname.bv_val,
735 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
739 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
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 );
745 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
746 f->f_av_desc->ad_cname.bv_val,
749 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
752 case LDAP_FILTER_APPROX:
753 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
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 );
759 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
760 f->f_av_desc->ad_cname.bv_val,
762 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
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 );
770 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
771 f->f_sub_desc->ad_cname.bv_val );
773 if ( f->f_sub_initial.bv_val != NULL ) {
776 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
778 fstr->bv_len += tmp.bv_len;
779 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
781 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
782 /* "(attr=" */ "%s*)",
785 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
788 if ( f->f_sub_any != NULL ) {
789 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
791 filter_escape_value_x( &f->f_sub_any[i], &tmp, op->o_tmpmemctx );
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 );
796 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
797 /* "(attr=[init]*[any*]" */ "%s*)",
799 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
803 if ( f->f_sub_final.bv_val != NULL ) {
806 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
808 fstr->bv_len += tmp.bv_len;
809 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
811 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
812 /* "(attr=[init*][any*]" */ "%s)",
815 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
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 );
825 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
826 f->f_desc->ad_cname.bv_val );
829 case LDAP_FILTER_AND:
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 );
835 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
836 f->f_choice == LDAP_FILTER_AND ? '&' :
837 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
839 for ( p = f->f_list; p != NULL; p = p->f_next ) {
842 filter2bv_x( op, p, &tmp );
844 fstr->bv_len += tmp.bv_len;
845 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
847 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
848 /*"("*/ "%s)", tmp.bv_val );
850 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
855 case LDAP_FILTER_EXT: {
857 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
859 if ( f->f_mr_desc ) {
860 ad = f->f_mr_desc->ad_cname;
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 );
872 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
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 : "",
878 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
881 case SLAPD_FILTER_COMPUTED:
882 switch ( f->f_result ) {
883 case LDAP_COMPARE_FALSE:
887 case LDAP_COMPARE_TRUE:
891 case SLAPD_COMPARE_UNDEFINED:
892 tmp = ber_bvundefined;
900 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
904 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
910 filter2bv( Filter *f, struct berval *fstr )
913 op.o_tmpmemctx = NULL;
914 op.o_tmpmfuncs = &ch_mfuncs;
916 filter2bv_x( &op, f, fstr );
920 filter_escape_value_x(
929 i = in->bv_len * 3 + 1;
930 out->bv_val = ctx ? slap_sl_malloc( i, ctx ) : ch_malloc( i );
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] );
939 out->bv_val[out->bv_len++] = in->bv_val[i];
943 out->bv_val[out->bv_len] = '\0';
952 return filter_escape_value_x( in, out, NULL );
959 ValuesReturnFilter **filt,
965 ValuesReturnFilter vrf;
968 LDAP_LOG( FILTER, ENTRY,
969 "get_simple_vrFilter: conn %d\n", op->o_connid, 0, 0 );
971 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
974 tag = ber_peek_tag( ber, &len );
976 if( tag == LBER_ERROR ) {
977 *text = "error decoding filter";
978 return SLAPD_DISCONNECT;
984 vrf.vrf_choice = tag;
986 switch ( vrf.vrf_choice ) {
987 case LDAP_FILTER_EQUALITY:
989 LDAP_LOG( FILTER, DETAIL2,
990 "get_simple_vrFilter: conn %d EQUALITY\n", op->o_connid, 0, 0 );
992 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
994 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
995 if ( err != LDAP_SUCCESS ) {
999 assert( vrf.vrf_ava != NULL );
1002 case LDAP_FILTER_SUBSTRINGS:
1004 LDAP_LOG( FILTER, DETAIL1,
1005 "get_simple_vrFilter: conn %d SUBSTRINGS\n", op->o_connid, 0, 0 );
1007 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
1009 err = get_ssa( op, ber, &vrf.vrf_sub, text );
1012 case LDAP_FILTER_GE:
1014 LDAP_LOG( FILTER, DETAIL1,
1015 "get_simple_vrFilter: conn %d GE\n", op->o_connid, 0, 0 );
1017 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
1019 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1020 if ( err != LDAP_SUCCESS ) {
1025 case LDAP_FILTER_LE:
1027 LDAP_LOG( FILTER, DETAIL1,
1028 "get_simple_vrFilter: conn %d LE\n", op->o_connid, 0, 0 );
1030 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
1032 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1033 if ( err != LDAP_SUCCESS ) {
1038 case LDAP_FILTER_PRESENT: {
1042 LDAP_LOG( FILTER, DETAIL1,
1043 "get_simple_vrFilter: conn %d PRESENT\n", op->o_connid, 0, 0 );
1045 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
1047 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
1048 err = SLAPD_DISCONNECT;
1049 *text = "error decoding filter";
1053 vrf.vrf_desc = NULL;
1054 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
1056 if( err != LDAP_SUCCESS ) {
1057 /* unrecognized attribute description or other error */
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 );
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 );
1070 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1071 vrf.vrf_result = LDAP_COMPARE_FALSE;
1077 case LDAP_FILTER_APPROX:
1079 LDAP_LOG( FILTER, DETAIL1,
1080 "get_simple_vrFilter: conn %d APPROX\n", op->o_connid, 0, 0 );
1082 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
1084 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
1085 if ( err != LDAP_SUCCESS ) {
1090 case LDAP_FILTER_EXT:
1092 LDAP_LOG( FILTER, DETAIL1,
1093 "get_simple_vrFilter: conn %d EXTENSIBLE\n", op->o_connid, 0, 0 );
1095 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1098 err = get_mra( op, ber, &vrf.vrf_mra, text );
1099 if ( err != LDAP_SUCCESS ) {
1103 assert( vrf.vrf_mra != NULL );
1107 (void) ber_scanf( ber, "x" ); /* skip the element */
1109 LDAP_LOG( FILTER, ERR,
1110 "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
1111 op->o_connid, vrf.vrf_choice, 0 );
1113 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1114 vrf.vrf_choice, 0, 0 );
1116 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1117 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1121 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1123 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1124 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1128 if ( err == LDAP_SUCCESS ) {
1129 *filt = ch_malloc( sizeof vrf );
1134 LDAP_LOG( FILTER, DETAIL2,
1135 "get_simple_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1137 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1144 get_vrFilter( Operation *op, BerElement *ber,
1145 ValuesReturnFilter **vrf,
1149 * A ValuesReturnFilter looks like this:
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
1162 * SubstringFilter ::= SEQUENCE {
1163 * type AttributeType,
1164 * SEQUENCE OF CHOICE {
1165 * initial [0] IA5String,
1166 * any [1] IA5String,
1167 * final [2] IA5String
1171 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1172 * matchingRule [1] MatchingRuleId OPTIONAL,
1173 * type [2] AttributeDescription OPTIONAL,
1174 * matchValue [3] AssertionValue }
1177 ValuesReturnFilter **n;
1183 LDAP_LOG( FILTER, ENTRY,
1184 "get_vrFilter: conn %d start\n", op->o_connid, 0, 0 );
1186 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1189 tag = ber_peek_tag( ber, &len );
1191 if( tag == LBER_ERROR ) {
1192 *text = "error decoding vrFilter";
1193 return SLAPD_DISCONNECT;
1196 if( tag != LBER_SEQUENCE ) {
1197 *text = "error decoding vrFilter, expect SEQUENCE tag";
1198 return SLAPD_DISCONNECT;
1202 for ( tag = ber_first_element( ber, &len, &last );
1203 tag != LBER_DEFAULT;
1204 tag = ber_next_element( ber, &len, last ) )
1206 int err = get_simple_vrFilter( op, ber, n, text );
1208 if ( err != LDAP_SUCCESS ) return( err );
1210 n = &(*n)->vrf_next;
1215 LDAP_LOG( FILTER, ENTRY,
1216 "get_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1218 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1220 return( LDAP_SUCCESS );
1224 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1226 ValuesReturnFilter *p, *next;
1228 if ( vrf == NULL ) {
1232 for ( p = vrf; p != NULL; p = next ) {
1235 switch ( vrf->vrf_choice ) {
1236 case LDAP_FILTER_PRESENT:
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 );
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 );
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 );
1254 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1257 case LDAP_FILTER_EXT:
1258 mra_free( op, vrf->vrf_mra, 1 );
1261 case SLAPD_FILTER_COMPUTED:
1266 LDAP_LOG( FILTER, ERR,
1267 "filter_free: unknown filter type %lu\n", vrf->vrf_choice, 0, 0 );
1269 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1270 vrf->vrf_choice, 0, 0 );
1275 op->o_tmpfree( vrf, op->o_tmpmemctx );
1280 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1282 ValuesReturnFilter *p;
1286 if ( vrf == NULL ) {
1287 ber_str2bv_x( "No filter!", sizeof("No filter!")-1,
1288 1, fstr, op->o_tmpmemctx );
1292 fstr->bv_len = sizeof("()") - 1;
1293 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1295 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1297 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1300 simple_vrFilter2bv( op, p, &tmp );
1302 fstr->bv_len += tmp.bv_len;
1303 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1305 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1306 /*"("*/ "%s)", tmp.bv_val );
1308 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1313 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1318 if ( vrf == NULL ) {
1319 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx );
1323 switch ( vrf->vrf_choice ) {
1324 case LDAP_FILTER_EQUALITY:
1325 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
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 );
1331 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1332 vrf->vrf_av_desc->ad_cname.bv_val,
1335 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1338 case LDAP_FILTER_GE:
1339 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
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 );
1345 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1346 vrf->vrf_av_desc->ad_cname.bv_val,
1349 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1352 case LDAP_FILTER_LE:
1353 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
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 );
1359 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1360 vrf->vrf_av_desc->ad_cname.bv_val,
1363 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1366 case LDAP_FILTER_APPROX:
1367 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
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 );
1373 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1374 vrf->vrf_av_desc->ad_cname.bv_val,
1376 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
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 );
1384 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1385 vrf->vrf_sub_desc->ad_cname.bv_val );
1387 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1390 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1392 fstr->bv_len += tmp.bv_len;
1393 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1395 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1396 /* "(attr=" */ "%s*)",
1399 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1402 if ( vrf->vrf_sub_any != NULL ) {
1404 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1406 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp, op->o_tmpmemctx );
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 );
1411 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1412 /* "(attr=[init]*[any*]" */ "%s*)",
1414 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1418 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1421 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1423 fstr->bv_len += tmp.bv_len;
1424 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1426 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1427 /* "(attr=[init*][any*]" */ "%s)",
1430 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
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 );
1440 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1441 vrf->vrf_desc->ad_cname.bv_val );
1444 case LDAP_FILTER_EXT: {
1446 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1448 if ( vrf->vrf_mr_desc ) {
1449 ad = vrf->vrf_mr_desc->ad_cname;
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 );
1461 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
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 : "",
1468 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1471 case SLAPD_FILTER_COMPUTED:
1473 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1474 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1475 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
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 );
1485 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1,
1486 1, fstr, op->o_tmpmemctx );