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 )
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,
782 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
783 /* "(attr=" */ "%s*)",
786 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
789 if ( f->f_sub_any != NULL ) {
790 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
792 filter_escape_value_x( &f->f_sub_any[i],
793 &tmp, op->o_tmpmemctx );
795 fstr->bv_len += tmp.bv_len + 1;
796 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
799 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
800 /* "(attr=[init]*[any*]" */ "%s*)",
802 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
806 if ( f->f_sub_final.bv_val != NULL ) {
809 filter_escape_value_x( &f->f_sub_final, &tmp, op->o_tmpmemctx );
811 fstr->bv_len += tmp.bv_len;
812 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
815 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
816 /* "(attr=[init*][any*]" */ "%s)",
819 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
824 case LDAP_FILTER_PRESENT:
825 fstr->bv_len = f->f_desc->ad_cname.bv_len +
826 ( sizeof("(=*)") - 1 );
827 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
829 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
830 f->f_desc->ad_cname.bv_val );
833 case LDAP_FILTER_AND:
835 case LDAP_FILTER_NOT:
836 fstr->bv_len = sizeof("(%)") - 1;
837 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
839 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)",
840 f->f_choice == LDAP_FILTER_AND ? '&' :
841 f->f_choice == LDAP_FILTER_OR ? '|' : '!' );
843 for ( p = f->f_list; p != NULL; p = p->f_next ) {
846 filter2bv_x( op, p, &tmp );
848 fstr->bv_len += tmp.bv_len;
849 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
852 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
853 /*"("*/ "%s)", tmp.bv_val );
855 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
860 case LDAP_FILTER_EXT: {
862 filter_escape_value_x( &f->f_mr_value, &tmp, op->o_tmpmemctx );
864 if ( f->f_mr_desc ) {
865 ad = f->f_mr_desc->ad_cname;
871 fstr->bv_len = ad.bv_len +
872 ( f->f_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
873 ( f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_len+1 : 0 ) +
874 tmp.bv_len + ( sizeof("(:=)") - 1 );
875 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
877 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
879 f->f_mr_dnattrs ? ":dn" : "",
880 f->f_mr_rule_text.bv_len ? ":" : "",
881 f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "",
883 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
886 case SLAPD_FILTER_COMPUTED:
887 switch ( f->f_result ) {
888 case LDAP_COMPARE_FALSE:
892 case LDAP_COMPARE_TRUE:
896 case SLAPD_COMPARE_UNDEFINED:
897 tmp = ber_bvundefined;
905 ber_dupbv_x( fstr, &tmp, op->o_tmpmemctx );
909 ber_dupbv_x( fstr, &ber_bvunknown, op->o_tmpmemctx );
915 filter2bv( Filter *f, struct berval *fstr )
918 op.o_tmpmemctx = NULL;
919 op.o_tmpmfuncs = &ch_mfuncs;
921 filter2bv_x( &op, f, fstr );
925 filter_escape_value_x(
934 i = in->bv_len * 3 + 1;
935 out->bv_val = ctx ? sl_malloc( i, ctx ) : ch_malloc( i );
938 for( i=0; i < in->bv_len ; i++ ) {
939 if( FILTER_ESCAPE(in->bv_val[i]) ) {
940 out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR;
941 out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] );
942 out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] );
944 out->bv_val[out->bv_len++] = in->bv_val[i];
948 out->bv_val[out->bv_len] = '\0';
957 return filter_escape_value_x( in, out, NULL );
964 ValuesReturnFilter **filt,
970 ValuesReturnFilter vrf;
973 LDAP_LOG( FILTER, ENTRY,
974 "get_simple_vrFilter: conn %d\n", op->o_connid, 0, 0 );
976 Debug( LDAP_DEBUG_FILTER, "begin get_simple_vrFilter\n", 0, 0, 0 );
979 tag = ber_peek_tag( ber, &len );
981 if( tag == LBER_ERROR ) {
982 *text = "error decoding filter";
983 return SLAPD_DISCONNECT;
989 vrf.vrf_choice = tag;
991 switch ( vrf.vrf_choice ) {
992 case LDAP_FILTER_EQUALITY:
994 LDAP_LOG( FILTER, DETAIL2,
995 "get_simple_vrFilter: conn %d EQUALITY\n", op->o_connid, 0, 0 );
997 Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 );
999 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY, text );
1000 if ( err != LDAP_SUCCESS ) {
1004 assert( vrf.vrf_ava != NULL );
1007 case LDAP_FILTER_SUBSTRINGS:
1009 LDAP_LOG( FILTER, DETAIL1,
1010 "get_simple_vrFilter: conn %d SUBSTRINGS\n", op->o_connid, 0, 0 );
1012 Debug( LDAP_DEBUG_FILTER, "SUBSTRINGS\n", 0, 0, 0 );
1014 err = get_ssa( op, ber, &vrf.vrf_sub, text );
1017 case LDAP_FILTER_GE:
1019 LDAP_LOG( FILTER, DETAIL1,
1020 "get_simple_vrFilter: conn %d GE\n", op->o_connid, 0, 0 );
1022 Debug( LDAP_DEBUG_FILTER, "GE\n", 0, 0, 0 );
1024 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1025 if ( err != LDAP_SUCCESS ) {
1030 case LDAP_FILTER_LE:
1032 LDAP_LOG( FILTER, DETAIL1,
1033 "get_simple_vrFilter: conn %d LE\n", op->o_connid, 0, 0 );
1035 Debug( LDAP_DEBUG_FILTER, "LE\n", 0, 0, 0 );
1037 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_ORDERING, text );
1038 if ( err != LDAP_SUCCESS ) {
1043 case LDAP_FILTER_PRESENT: {
1047 LDAP_LOG( FILTER, DETAIL1,
1048 "get_simple_vrFilter: conn %d PRESENT\n", op->o_connid, 0, 0 );
1050 Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 );
1052 if ( ber_scanf( ber, "m", &type ) == LBER_ERROR ) {
1053 err = SLAPD_DISCONNECT;
1054 *text = "error decoding filter";
1058 vrf.vrf_desc = NULL;
1059 err = slap_bv2ad( &type, &vrf.vrf_desc, text );
1061 if( err != LDAP_SUCCESS ) {
1062 /* unrecognized attribute description or other error */
1064 LDAP_LOG( FILTER, ERR,
1065 "get_simple_vrFilter: conn %d unknown "
1066 "attribute type=%s (%d)\n",
1067 op->o_connid, type.bv_val, err );
1069 Debug( LDAP_DEBUG_ANY,
1070 "get_simple_vrFilter: conn %d unknown "
1071 "attribute type=%s (%d)\n",
1072 op->o_connid, type.bv_val, err );
1075 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1076 vrf.vrf_result = LDAP_COMPARE_FALSE;
1082 case LDAP_FILTER_APPROX:
1084 LDAP_LOG( FILTER, DETAIL1,
1085 "get_simple_vrFilter: conn %d APPROX\n", op->o_connid, 0, 0 );
1087 Debug( LDAP_DEBUG_FILTER, "APPROX\n", 0, 0, 0 );
1089 err = get_ava( op, ber, &vrf.vrf_ava, SLAP_MR_EQUALITY_APPROX, text );
1090 if ( err != LDAP_SUCCESS ) {
1095 case LDAP_FILTER_EXT:
1097 LDAP_LOG( FILTER, DETAIL1,
1098 "get_simple_vrFilter: conn %d EXTENSIBLE\n", op->o_connid, 0, 0 );
1100 Debug( LDAP_DEBUG_FILTER, "EXTENSIBLE\n", 0, 0, 0 );
1103 err = get_mra( op, ber, &vrf.vrf_mra, text );
1104 if ( err != LDAP_SUCCESS ) {
1108 assert( vrf.vrf_mra != NULL );
1112 (void) ber_scanf( ber, "x" ); /* skip the element */
1114 LDAP_LOG( FILTER, ERR,
1115 "get_simple_vrFilter: conn %d unknown filter type=%lu\n",
1116 op->o_connid, vrf.vrf_choice, 0 );
1118 Debug( LDAP_DEBUG_ANY, "get_simple_vrFilter: unknown filter type=%lu\n",
1119 vrf.vrf_choice, 0, 0 );
1121 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1122 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1126 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1128 vrf.vrf_choice = SLAPD_FILTER_COMPUTED;
1129 vrf.vrf_result = SLAPD_COMPARE_UNDEFINED;
1133 if ( err == LDAP_SUCCESS ) {
1134 *filt = ch_malloc( sizeof vrf );
1139 LDAP_LOG( FILTER, DETAIL2,
1140 "get_simple_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1142 Debug( LDAP_DEBUG_FILTER, "end get_simple_vrFilter %d\n", err, 0, 0 );
1149 get_vrFilter( Operation *op, BerElement *ber,
1150 ValuesReturnFilter **vrf,
1154 * A ValuesReturnFilter looks like this:
1156 * ValuesReturnFilter ::= SEQUENCE OF SimpleFilterItem
1157 * SimpleFilterItem ::= CHOICE {
1158 * equalityMatch [3] AttributeValueAssertion,
1159 * substrings [4] SubstringFilter,
1160 * greaterOrEqual [5] AttributeValueAssertion,
1161 * lessOrEqual [6] AttributeValueAssertion,
1162 * present [7] AttributeType,
1163 * approxMatch [8] AttributeValueAssertion,
1164 * extensibleMatch [9] SimpleMatchingAssertion -- LDAPv3
1167 * SubstringFilter ::= SEQUENCE {
1168 * type AttributeType,
1169 * SEQUENCE OF CHOICE {
1170 * initial [0] IA5String,
1171 * any [1] IA5String,
1172 * final [2] IA5String
1176 * SimpleMatchingAssertion ::= SEQUENCE { -- LDAPv3
1177 * matchingRule [1] MatchingRuleId OPTIONAL,
1178 * type [2] AttributeDescription OPTIONAL,
1179 * matchValue [3] AssertionValue }
1182 ValuesReturnFilter **n;
1188 LDAP_LOG( FILTER, ENTRY,
1189 "get_vrFilter: conn %d start\n", op->o_connid, 0, 0 );
1191 Debug( LDAP_DEBUG_FILTER, "begin get_vrFilter\n", 0, 0, 0 );
1194 tag = ber_peek_tag( ber, &len );
1196 if( tag == LBER_ERROR ) {
1197 *text = "error decoding vrFilter";
1198 return SLAPD_DISCONNECT;
1201 if( tag != LBER_SEQUENCE ) {
1202 *text = "error decoding vrFilter, expect SEQUENCE tag";
1203 return SLAPD_DISCONNECT;
1207 for ( tag = ber_first_element( ber, &len, &last );
1208 tag != LBER_DEFAULT;
1209 tag = ber_next_element( ber, &len, last ) )
1211 int err = get_simple_vrFilter( op, ber, n, text );
1213 if ( err != LDAP_SUCCESS ) return( err );
1215 n = &(*n)->vrf_next;
1220 LDAP_LOG( FILTER, ENTRY,
1221 "get_vrFilter: conn %d exit\n", op->o_connid, 0, 0 );
1223 Debug( LDAP_DEBUG_FILTER, "end get_vrFilter\n", 0, 0, 0 );
1225 return( LDAP_SUCCESS );
1229 vrFilter_free( Operation *op, ValuesReturnFilter *vrf )
1231 ValuesReturnFilter *p, *next;
1233 if ( vrf == NULL ) {
1237 for ( p = vrf; p != NULL; p = next ) {
1240 switch ( vrf->vrf_choice ) {
1241 case LDAP_FILTER_PRESENT:
1244 case LDAP_FILTER_EQUALITY:
1245 case LDAP_FILTER_GE:
1246 case LDAP_FILTER_LE:
1247 case LDAP_FILTER_APPROX:
1248 ava_free( op, vrf->vrf_ava, 1 );
1251 case LDAP_FILTER_SUBSTRINGS:
1252 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1253 op->o_tmpfree( vrf->vrf_sub_initial.bv_val, op->o_tmpmemctx );
1255 ber_bvarray_free_x( vrf->vrf_sub_any, op->o_tmpmemctx );
1256 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1257 op->o_tmpfree( vrf->vrf_sub_final.bv_val, op->o_tmpmemctx );
1259 op->o_tmpfree( vrf->vrf_sub, op->o_tmpmemctx );
1262 case LDAP_FILTER_EXT:
1263 mra_free( op, vrf->vrf_mra, 1 );
1266 case SLAPD_FILTER_COMPUTED:
1271 LDAP_LOG( FILTER, ERR, "filter_free: unknown filter type %lu\n",
1272 vrf->vrf_choice, 0, 0 );
1274 Debug( LDAP_DEBUG_ANY, "filter_free: unknown filter type=%lu\n",
1275 vrf->vrf_choice, 0, 0 );
1280 op->o_tmpfree( vrf, op->o_tmpmemctx );
1285 vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1287 ValuesReturnFilter *p;
1291 if ( vrf == NULL ) {
1292 ber_str2bv_x( "No filter!", sizeof("No filter!")-1,
1293 1, fstr, op->o_tmpmemctx );
1297 fstr->bv_len = sizeof("()") - 1;
1298 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1300 snprintf( fstr->bv_val, fstr->bv_len + 1, "()");
1302 for ( p = vrf; p != NULL; p = p->vrf_next ) {
1305 simple_vrFilter2bv( op, p, &tmp );
1307 fstr->bv_len += tmp.bv_len;
1308 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1311 snprintf( &fstr->bv_val[len-1], tmp.bv_len + 2,
1312 /*"("*/ "%s)", tmp.bv_val );
1314 op->o_tmpfree( tmp.bv_val, op->o_tmpmemctx );
1319 simple_vrFilter2bv( Operation *op, ValuesReturnFilter *vrf, struct berval *fstr )
1324 if ( vrf == NULL ) {
1325 ber_str2bv_x( "No filter!", sizeof("No filter!")-1, 1, fstr,
1330 switch ( vrf->vrf_choice ) {
1331 case LDAP_FILTER_EQUALITY:
1332 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1334 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1335 tmp.bv_len + ( sizeof("(=)") - 1 );
1336 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1338 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)",
1339 vrf->vrf_av_desc->ad_cname.bv_val,
1342 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1345 case LDAP_FILTER_GE:
1346 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1348 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1349 tmp.bv_len + ( sizeof("(>=)") - 1 );
1350 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1352 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)",
1353 vrf->vrf_av_desc->ad_cname.bv_val,
1356 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1359 case LDAP_FILTER_LE:
1360 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1362 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1363 tmp.bv_len + ( sizeof("(<=)") - 1 );
1364 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1366 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)",
1367 vrf->vrf_av_desc->ad_cname.bv_val,
1370 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1373 case LDAP_FILTER_APPROX:
1374 filter_escape_value_x( &vrf->vrf_av_value, &tmp, op->o_tmpmemctx );
1376 fstr->bv_len = vrf->vrf_av_desc->ad_cname.bv_len +
1377 tmp.bv_len + ( sizeof("(~=)") - 1 );
1378 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1380 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)",
1381 vrf->vrf_av_desc->ad_cname.bv_val,
1383 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1386 case LDAP_FILTER_SUBSTRINGS:
1387 fstr->bv_len = vrf->vrf_sub_desc->ad_cname.bv_len +
1388 ( sizeof("(=*)") - 1 );
1389 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 128, op->o_tmpmemctx );
1391 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1392 vrf->vrf_sub_desc->ad_cname.bv_val );
1394 if ( vrf->vrf_sub_initial.bv_val != NULL ) {
1397 filter_escape_value_x( &vrf->vrf_sub_initial, &tmp, op->o_tmpmemctx );
1399 fstr->bv_len += tmp.bv_len;
1400 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1403 snprintf( &fstr->bv_val[len-2], tmp.bv_len+3,
1404 /* "(attr=" */ "%s*)",
1407 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1410 if ( vrf->vrf_sub_any != NULL ) {
1412 for ( i = 0; vrf->vrf_sub_any[i].bv_val != NULL; i++ ) {
1414 filter_escape_value_x( &vrf->vrf_sub_any[i], &tmp,
1417 fstr->bv_len += tmp.bv_len + 1;
1418 fstr->bv_val = op->o_tmprealloc( fstr->bv_val,
1419 fstr->bv_len + 1, op->o_tmpmemctx );
1421 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1422 /* "(attr=[init]*[any*]" */ "%s*)",
1424 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1428 if ( vrf->vrf_sub_final.bv_val != NULL ) {
1431 filter_escape_value_x( &vrf->vrf_sub_final, &tmp, op->o_tmpmemctx );
1433 fstr->bv_len += tmp.bv_len;
1434 fstr->bv_val = op->o_tmprealloc( fstr->bv_val, fstr->bv_len + 1,
1437 snprintf( &fstr->bv_val[len-1], tmp.bv_len+3,
1438 /* "(attr=[init*][any*]" */ "%s)",
1441 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1446 case LDAP_FILTER_PRESENT:
1447 fstr->bv_len = vrf->vrf_desc->ad_cname.bv_len +
1448 ( sizeof("(=*)") - 1 );
1449 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1451 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)",
1452 vrf->vrf_desc->ad_cname.bv_val );
1455 case LDAP_FILTER_EXT: {
1457 filter_escape_value_x( &vrf->vrf_mr_value, &tmp, op->o_tmpmemctx );
1459 if ( vrf->vrf_mr_desc ) {
1460 ad = vrf->vrf_mr_desc->ad_cname;
1466 fstr->bv_len = ad.bv_len +
1467 ( vrf->vrf_mr_dnattrs ? sizeof(":dn")-1 : 0 ) +
1468 ( vrf->vrf_mr_rule_text.bv_len
1469 ? vrf->vrf_mr_rule_text.bv_len+1 : 0 ) +
1470 tmp.bv_len + ( sizeof("(:=)") - 1 );
1471 fstr->bv_val = op->o_tmpalloc( fstr->bv_len + 1, op->o_tmpmemctx );
1473 snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)",
1475 vrf->vrf_mr_dnattrs ? ":dn" : "",
1476 vrf->vrf_mr_rule_text.bv_len ? ":" : "",
1477 vrf->vrf_mr_rule_text.bv_len ? vrf->vrf_mr_rule_text.bv_val : "",
1480 ber_memfree_x( tmp.bv_val, op->o_tmpmemctx );
1483 case SLAPD_FILTER_COMPUTED:
1485 vrf->vrf_result == LDAP_COMPARE_FALSE ? "(?=false)" :
1486 vrf->vrf_result == LDAP_COMPARE_TRUE ? "(?=true)" :
1487 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1488 ? "(?=undefined)" : "(?=error)",
1489 vrf->vrf_result == LDAP_COMPARE_FALSE ? sizeof("(?=false)")-1 :
1490 vrf->vrf_result == LDAP_COMPARE_TRUE ? sizeof("(?=true)")-1 :
1491 vrf->vrf_result == SLAPD_COMPARE_UNDEFINED
1492 ? sizeof("(?=undefined)")-1 : sizeof("(?=error)")-1,
1493 1, fstr, op->o_tmpmemctx );
1497 ber_str2bv_x( "(?=unknown)", sizeof("(?=unknown)")-1,
1498 1, fstr, op->o_tmpmemctx );