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 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 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 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 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 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 );
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 = sl_context( f );
685 op.o_tmpmfuncs = &sl_mfuncs;
686 filter_free_x( &op, f );
690 filter2bv_x( Operation *op, Filter *f, struct berval *fstr )
695 ber_bvfalse = BER_BVC( "(?=false)" ),
696 ber_bvtrue = BER_BVC( "(?=true)" ),
697 ber_bvundefined = BER_BVC( "(?=undefined)" ),
698 ber_bverror = BER_BVC( "(?=error)" ),
699 ber_bvunknown = BER_BVC( "(?=unknown)" );
703 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx );
707 switch ( f->f_choice ) {
708 case LDAP_FILTER_EQUALITY:
709 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
711 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
712 tmp.bv_len + ( sizeof("(=)") - 1 );
713 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
715 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
716 f->f_av_desc->ad_cname.bv_val,
719 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
723 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
725 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
726 tmp.bv_len + ( sizeof("(>=)") - 1 );
727 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
729 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
730 f->f_av_desc->ad_cname.bv_val,
733 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
737 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
739 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
740 tmp.bv_len + ( sizeof("(<=)") - 1 );
741 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
743 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
744 f->f_av_desc->ad_cname.bv_val,
747 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
750 case LDAP_FILTER_APPROX:
751 filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx );
753 fstr->bv_len = f->f_av_desc->ad_cname.bv_len +
754 tmp.bv_len + ( sizeof("(~=)") - 1 );
755 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
757 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
758 f->f_av_desc->ad_cname.bv_val,
760 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
763 case LDAP_FILTER_SUBSTRINGS:
764 fstr->bv_len = f->f_sub_desc->ad_cname.bv_len +
765 ( sizeof("(=*)") - 1 );
766 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
768 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
769 f->f_sub_desc->ad_cname.bv_val );
771 if ( f->f_sub_initial.bv_val != NULL ) {
774 filter_escape_value_x( &f->f_sub_initial, &tmp, op->o_tmpmemctx );
776 fstr->bv_len += tmp.bv_len;
777 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
779 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
780 /* "(attr=" */ "%s*)",
783 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
786 if ( f->f_sub_any != NULL ) {
787 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
789 filter_escape_value_x( &f->f_sub_any[i], &tmp, op->o_tmpmemctx );
791 fstr->bv_len += tmp.bv_len + 1;
792 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
794 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
795 /* "(attr=[init]*[any*]" */ "%s*)",
797 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
801 if ( f->f_sub_final.bv_val != NULL ) {
804 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
806 fstr->bv_len += tmp.bv_len;
807 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
809 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
810 /* "(attr=[init*][any*]" */ "%s)",
813 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
818 case LDAP_FILTER_PRESENT:
819 fstr->bv_len = f->f_desc->ad_cname.bv_len +
820 ( sizeof("(=*)") - 1 );
821 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
823 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
824 f->f_desc->ad_cname.bv_val );
827 case LDAP_FILTER_AND:
829 case LDAP_FILTER_NOT:
830 fstr->bv_len = sizeof("(%)") - 1;
831 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
833 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
834 f->f_choice == LDAP_FILTER_AND ? '&' :
835 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
837 for ( p = f->f_list; p != NULL; p = p->f_next ) {
840 filter2bv_x( op, p, &tmp );
842 fstr->bv_len += tmp.bv_len;
843 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
845 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
846 /*"("*/ "%s)", tmp.bv_val );
848 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
853 case LDAP_FILTER_EXT: {
855 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
857 if ( f->f_mr_desc ) {
858 ad = f->f_mr_desc->ad_cname;
864 fstr->bv_len = ad.bv_len +
865 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
866 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
867 tmp.bv_len + ( sizeof("(:=)") - 1 );
868 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
870 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
872 f->f_mr_dnattrs ? ":dn" : "",
873 f->f_mr_rule_text.bv_len ? ":" : "",
874 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
876 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
879 case SLAPD_FILTER_COMPUTED:
880 switch ( f->f_result ) {
881 case LDAP_COMPARE_FALSE:
885 case LDAP_COMPARE_TRUE:
889 case SLAPD_COMPARE_UNDEFINED:
890 tmp = ber_bvundefined;
898 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
902 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
908 filter2bv( Filter *f, struct berval *fstr )
911 op.o_tmpmemctx = NULL;
912 op.o_tmpmfuncs = &ch_mfuncs;
914 filter2bv_x( &op, f, fstr );
918 filter_escape_value_x(
927 i = in->bv_len * 3 + 1;
928 out->bv_val = ctx ? sl_malloc( i, ctx ) : ch_malloc( i );
931 for( i=0; i < in->bv_len ; i++ ) {
932 if( FILTER_ESCAPE(in->bv_val[i]) ) {
933 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
934 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
935 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
937 out->bv_val[out->bv_len++] = in->bv_val[i];
941 out->bv_val[out->bv_len] = '\0';
950 return filter_escape_value_x( in, out, NULL );
957 ValuesReturnFilter **filt,
963 ValuesReturnFilter vrf;
966 LDAP_LOG( FILTER, ENTRY,
967 "get_simple_vrFilter: conn %d\n", op->o_connid, 0, 0 );
969 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
972 tag = ber_peek_tag( ber, &len );
974 if( tag == LBER_ERROR ) {
975 *text = "error decoding filter";
976 return SLAPD_DISCONNECT;
982 vrf.vrf_choice = tag;
984 switch ( vrf.vrf_choice ) {
985 case LDAP_FILTER_EQUALITY:
987 LDAP_LOG( FILTER, DETAIL2,
988 "get_simple_vrFilter: conn %d EQUALITY\n", op->o_connid, 0, 0 );
990 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
992 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
993 if ( err != LDAP_SUCCESS ) {
997 assert( vrf.vrf_ava != NULL );
1000 case LDAP_FILTER_SUBSTRINGS:
1002 LDAP_LOG( FILTER, DETAIL1,
1003 "get_simple_vrFilter: conn %d SUBSTRINGS\n", op->o_connid, 0, 0 );
1005 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
1007 err = get_ssa( op, ber, &vrf.vrf_sub, text );
1010 case LDAP_FILTER_GE:
1012 LDAP_LOG( FILTER, DETAIL1,
1013 "get_simple_vrFilter: conn %d GE\n", op->o_connid, 0, 0 );
1015 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
1017 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1018 if ( err != LDAP_SUCCESS ) {
1023 case LDAP_FILTER_LE:
1025 LDAP_LOG( FILTER, DETAIL1,
1026 "get_simple_vrFilter: conn %d LE\n", op->o_connid, 0, 0 );
1028 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
1030 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1031 if ( err != LDAP_SUCCESS ) {
1036 case LDAP_FILTER_PRESENT: {
1040 LDAP_LOG( FILTER, DETAIL1,
1041 "get_simple_vrFilter: conn %d PRESENT\n", op->o_connid, 0, 0 );
1043 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
1045 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
1046 err = SLAPD_DISCONNECT;
1047 *text = "error decoding filter";
1051 vrf.vrf_desc = NULL;
1052 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
1054 if( err != LDAP_SUCCESS ) {
1055 /* unrecognized attribute description or other error */
1057 LDAP_LOG( FILTER, ERR,
1058 "get_simple_vrFilter: conn %d unknown "
1059 "attribute type=%s (%d)\n",
1060 op->o_connid, type.bv_val, err );
1062 Debug( LDAP_DEBUG_ANY,
1063 "get_simple_vrFilter: conn %d unknown "
1064 "attribute type=%s (%d)\n",
1065 op->o_connid, type.bv_val, err );
1068 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1069 vrf.vrf_result = LDAP_COMPARE_FALSE;
1075 case LDAP_FILTER_APPROX:
1077 LDAP_LOG( FILTER, DETAIL1,
1078 "get_simple_vrFilter: conn %d APPROX\n", op->o_connid, 0, 0 );
1080 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
1082 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
1083 if ( err != LDAP_SUCCESS ) {
1088 case LDAP_FILTER_EXT:
1090 LDAP_LOG( FILTER, DETAIL1,
1091 "get_simple_vrFilter: conn %d EXTENSIBLE\n", op->o_connid, 0, 0 );
1093 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1096 err = get_mra( op, ber, &vrf.vrf_mra, text );
1097 if ( err != LDAP_SUCCESS ) {
1101 assert( vrf.vrf_mra != NULL );
1105 (void) ber_scanf( ber, "x" ); /* skip the element */
1107 LDAP_LOG( FILTER, ERR,
1108 "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
1109 op->o_connid, vrf.vrf_choice, 0 );
1111 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1112 vrf.vrf_choice, 0, 0 );
1114 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1115 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1119 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1121 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1122 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1126 if ( err == LDAP_SUCCESS ) {
1127 *filt = ch_malloc( sizeof vrf );
1132 LDAP_LOG( FILTER, DETAIL2,
1133 "get_simple_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1135 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1142 get_vrFilter( Operation *op, BerElement *ber,
1143 ValuesReturnFilter **vrf,
1147 * A ValuesReturnFilter looks like this:
1149 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1150 * SimpleFilterItem ::= CHOICE {
1151 * equalityMatch [3] AttributeValueAssertion,
1152 * substrings [4] SubstringFilter,
1153 * greaterOrEqual [5] AttributeValueAssertion,
1154 * lessOrEqual [6] AttributeValueAssertion,
1155 * present [7] AttributeType,
1156 * approxMatch [8] AttributeValueAssertion,
1157 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1160 * SubstringFilter ::= SEQUENCE {
1161 * type AttributeType,
1162 * SEQUENCE OF CHOICE {
1163 * initial [0] IA5String,
1164 * any [1] IA5String,
1165 * final [2] IA5String
1169 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1170 * matchingRule [1] MatchingRuleId OPTIONAL,
1171 * type [2] AttributeDescription OPTIONAL,
1172 * matchValue [3] AssertionValue }
1175 ValuesReturnFilter **n;
1181 LDAP_LOG( FILTER, ENTRY,
1182 "get_vrFilter: conn %d start\n", op->o_connid, 0, 0 );
1184 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1187 tag = ber_peek_tag( ber, &len );
1189 if( tag == LBER_ERROR ) {
1190 *text = "error decoding vrFilter";
1191 return SLAPD_DISCONNECT;
1194 if( tag != LBER_SEQUENCE ) {
1195 *text = "error decoding vrFilter, expect SEQUENCE tag";
1196 return SLAPD_DISCONNECT;
1200 for ( tag = ber_first_element( ber, &len, &last );
1201 tag != LBER_DEFAULT;
1202 tag = ber_next_element( ber, &len, last ) )
1204 int err = get_simple_vrFilter( op, ber, n, text );
1206 if ( err != LDAP_SUCCESS ) return( err );
1208 n = &(*n)->vrf_next;
1213 LDAP_LOG( FILTER, ENTRY,
1214 "get_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1216 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1218 return( LDAP_SUCCESS );
1222 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1224 ValuesReturnFilter *p, *next;
1226 if ( vrf == NULL ) {
1230 for ( p = vrf; p != NULL; p = next ) {
1233 switch ( vrf->vrf_choice ) {
1234 case LDAP_FILTER_PRESENT:
1237 case LDAP_FILTER_EQUALITY:
1238 case LDAP_FILTER_GE:
1239 case LDAP_FILTER_LE:
1240 case LDAP_FILTER_APPROX:
1241 ava_free( op, vrf->vrf_ava, 1 );
1244 case LDAP_FILTER_SUBSTRINGS:
1245 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1246 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1248 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1249 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1250 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1252 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1255 case LDAP_FILTER_EXT:
1256 mra_free( op, vrf->vrf_mra, 1 );
1259 case SLAPD_FILTER_COMPUTED:
1264 LDAP_LOG( FILTER, ERR,
1265 "filter_free: unknown filter type %lu\n", vrf->vrf_choice, 0, 0 );
1267 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1268 vrf->vrf_choice, 0, 0 );
1273 op->o_tmpfree( vrf, op->o_tmpmemctx );
1278 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1280 ValuesReturnFilter *p;
1284 if ( vrf == NULL ) {
1285 ber_str2bv_x( "No filter!", sizeof("No filter!")-1,
1286 1, fstr, op->o_tmpmemctx );
1290 fstr->bv_len = sizeof("()") - 1;
1291 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1293 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1295 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1298 simple_vrFilter2bv( op, p, &tmp );
1300 fstr->bv_len += tmp.bv_len;
1301 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1303 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1304 /*"("*/ "%s)", tmp.bv_val );
1306 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1311 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1316 if ( vrf == NULL ) {
1317 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr, op->o_tmpmemctx );
1321 switch ( vrf->vrf_choice ) {
1322 case LDAP_FILTER_EQUALITY:
1323 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1325 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1326 tmp.bv_len + ( sizeof("(=)") - 1 );
1327 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1329 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1330 vrf->vrf_av_desc->ad_cname.bv_val,
1333 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1336 case LDAP_FILTER_GE:
1337 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1339 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1340 tmp.bv_len + ( sizeof("(>=)") - 1 );
1341 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1343 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1344 vrf->vrf_av_desc->ad_cname.bv_val,
1347 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1350 case LDAP_FILTER_LE:
1351 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1353 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1354 tmp.bv_len + ( sizeof("(<=)") - 1 );
1355 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1357 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1358 vrf->vrf_av_desc->ad_cname.bv_val,
1361 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1364 case LDAP_FILTER_APPROX:
1365 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1367 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1368 tmp.bv_len + ( sizeof("(~=)") - 1 );
1369 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1371 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1372 vrf->vrf_av_desc->ad_cname.bv_val,
1374 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1377 case LDAP_FILTER_SUBSTRINGS:
1378 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1379 ( sizeof("(=*)") - 1 );
1380 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1382 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1383 vrf->vrf_sub_desc->ad_cname.bv_val );
1385 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1388 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1390 fstr->bv_len += tmp.bv_len;
1391 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1393 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1394 /* "(attr=" */ "%s*)",
1397 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1400 if ( vrf->vrf_sub_any != NULL ) {
1402 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1404 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp, op->o_tmpmemctx );
1406 fstr->bv_len += tmp.bv_len + 1;
1407 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1409 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1410 /* "(attr=[init]*[any*]" */ "%s*)",
1412 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1416 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1419 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1421 fstr->bv_len += tmp.bv_len;
1422 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1, op->o_tmpmemctx );
1424 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1425 /* "(attr=[init*][any*]" */ "%s)",
1428 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1433 case LDAP_FILTER_PRESENT:
1434 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1435 ( sizeof("(=*)") - 1 );
1436 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1438 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1439 vrf->vrf_desc->ad_cname.bv_val );
1442 case LDAP_FILTER_EXT: {
1444 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1446 if ( vrf->vrf_mr_desc ) {
1447 ad = vrf->vrf_mr_desc->ad_cname;
1453 fstr->bv_len = ad.bv_len +
1454 ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1455 ( vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1456 tmp.bv_len + ( sizeof("(:=)") - 1 );
1457 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1459 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1461 vrf->vrf_mr_dnattrs ? ":dn" : "",
1462 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1463 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1466 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1469 case SLAPD_FILTER_COMPUTED:
1471 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1472 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1473 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? "(?=undefined)" :
1475 vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1476 vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1477 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED ? sizeof("(?=undefined)")-1 :
1478 sizeof("(?=error)")-1,
1479 1, fstr, op->o_tmpmemctx );
1483 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1,
1484 1, fstr, op->o_tmpmemctx );