1 /* component.c -- Component Filter Match Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2004 by IBM Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/socket.h>
27 #ifdef LDAP_COMP_MATCH
29 #include "component.h"
32 * Following function pointers are initialized
33 * when a component module is loaded
35 alloc_nibble_func* nibble_mem_allocator = NULL;
36 free_nibble_func* nibble_mem_free = NULL;
37 convert_attr_to_comp_func* attr_converter = NULL ;
38 convert_assert_to_comp_func* assert_converter = NULL ;
39 free_component_func* component_destructor = NULL ;
40 test_component_func* test_components = NULL;
41 test_membership_func* is_aliased_attribute = NULL;
43 #define OID_ALL_COMP_MATCH "1.2.36.79672281.1.13.6"
44 #define OID_COMP_FILTER_MATCH "1.2.36.79672281.1.13.2"
45 #define MAX_LDAP_STR_LEN 128
48 peek_componentId_type( ComponentAssertionValue* cav );
51 strip_cav_str( ComponentAssertionValue* cav, char* str);
54 peek_cav_str( ComponentAssertionValue* cav, char* str );
57 parse_comp_filter( Operation* op, ComponentAssertionValue* cav,
58 ComponentFilter** filt, const char** text );
61 free_comp_filter( ComponentFilter* f );
64 test_comp_filter( Syntax *syn, Attribute *a, struct berval *bv,
68 componentCertificateValidate(
76 componentFilterValidate(
84 allComponentsValidate(
92 componentFilterMatch (
100 Attribute *a = (Attribute*)value;
101 MatchingRuleAssertion * ma = (MatchingRuleAssertion*)assertedValue;
105 if ( !(mr && mr->smr_usage & SLAP_MR_COMPONENT) || !ma->ma_cf )
106 return LDAP_INAPPROPRIATE_MATCHING;
108 rc = test_comp_filter( syntax, a, a->a_vals, ma->ma_cf );
110 if ( rc == LDAP_COMPARE_TRUE ) {
114 else if ( rc == LDAP_COMPARE_FALSE ) {
119 return LDAP_INAPPROPRIATE_MATCHING;
124 directoryComponentsMatch(
129 struct berval *value,
130 void *assertedValue )
132 /* Only for Registeration */
143 struct berval *value,
144 void *assertedValue )
146 /* Only for Registeration */
152 slapd_ber2cav( struct berval* bv, ComponentAssertionValue* cav )
156 cav->cav_ptr = cav->cav_buf = bv->bv_val;
157 cav->cav_end = bv->bv_val + bv->bv_len;
162 static ComponentReference*
163 dup_comp_ref ( Operation* op, ComponentReference* cr )
166 ComponentReference* dup_cr;
167 ComponentId* ci_curr;
168 ComponentId** ci_temp;
171 dup_cr = op->o_tmpalloc( sizeof( ComponentReference ), op->o_tmpmemctx );
173 dup_cr->cr_len = cr->cr_len;
174 dup_cr->cr_string = cr->cr_string;
176 ci_temp = &dup_cr->cr_list;
177 ci_curr = cr->cr_list;
179 for ( ; ci_curr ; ci_curr = ci_curr->ci_next, ci_temp = &(*ci_temp)->ci_next ) {
180 *ci_temp = op->o_tmpalloc( sizeof( ComponentId ), op->o_tmpmemctx );
181 if ( !ci_temp ) return NULL;
182 **ci_temp = *ci_curr;
185 dup_cr->cr_curr = dup_cr->cr_list;
191 dup_comp_filter_list (
194 ComponentFilter* in_f,
195 ComponentFilter** out_f )
197 ComponentFilter **new, *f;
201 for ( f = in_f; f != NULL; f = f->cf_next ) {
202 rc = dup_comp_filter( op, bv, f, new );
203 if ( rc != LDAP_SUCCESS ) {
206 new = &(*new)->cf_next;
212 get_len_of_next_assert_value ( struct berval* bv, char separator )
216 if ( (bv->bv_val[ i ] == separator) || ( i >= bv->bv_len) )
220 bv->bv_val += (i + 1);
221 bv->bv_len -= (i + 1);
226 dup_comp_filter_item (
228 struct berval* assert_bv,
229 ComponentAssertion* in_ca,
230 ComponentAssertion** out_ca )
232 ComponentAssertion* ca;
235 if ( !in_ca->ca_comp_ref )
236 return SLAPD_DISCONNECT;
238 *out_ca = op->o_tmpalloc( sizeof( ComponentAssertion ), op->o_tmpmemctx );
240 return LDAP_NO_MEMORY;
242 (*out_ca)->ca_comp_data.cd_tree = NULL;
243 (*out_ca)->ca_comp_data.cd_mem_op = NULL;
245 (*out_ca)->ca_comp_ref = dup_comp_ref ( op, in_ca->ca_comp_ref );
246 (*out_ca)->ca_use_def = 0;
247 (*out_ca)->ca_ma_rule = in_ca->ca_ma_rule;
249 (*out_ca)->ca_ma_value.bv_val = assert_bv->bv_val;
250 len = get_len_of_next_assert_value ( assert_bv, '$' );
252 return SLAPD_DISCONNECT;
253 (*out_ca)->ca_ma_value.bv_len = len;
262 ComponentFilter *in_f,
263 ComponentFilter **out_f )
266 ComponentFilter dup_f = {0};
268 if ( !in_f ) return LDAP_PROTOCOL_ERROR;
270 switch ( in_f->cf_choice ) {
271 case LDAP_COMP_FILTER_AND:
272 rc = dup_comp_filter_list( op, bv, in_f->cf_and, &dup_f.cf_and);
273 dup_f.cf_choice = LDAP_COMP_FILTER_AND;
275 case LDAP_COMP_FILTER_OR:
276 rc = dup_comp_filter_list( op, bv, in_f->cf_or, &dup_f.cf_or);
277 dup_f.cf_choice = LDAP_COMP_FILTER_OR;
279 case LDAP_COMP_FILTER_NOT:
280 rc = dup_comp_filter( op, bv, in_f->cf_not, &dup_f.cf_not);
281 dup_f.cf_choice = LDAP_COMP_FILTER_NOT;
283 case LDAP_COMP_FILTER_ITEM:
284 rc = dup_comp_filter_item( op, bv, in_f->cf_ca ,&dup_f.cf_ca );
285 dup_f.cf_choice = LDAP_COMP_FILTER_ITEM;
288 rc = LDAP_PROTOCOL_ERROR;
291 if ( rc == LDAP_SUCCESS ) {
292 *out_f = op->o_tmpalloc( sizeof(dup_f), op->o_tmpmemctx );
301 get_aliased_filter ( Operation* op, MatchingRuleAssertion* ma, AttributeAliasing* aa, const char** text )
304 struct berval assert_bv;
305 ComponentAssertion* ca;
307 Debug( LDAP_DEBUG_FILTER, "get_aliased_filter\n", 0, 0, 0 );
309 if ( !aa->aa_cf ) return LDAP_PROTOCOL_ERROR;
311 assert_bv = ma->ma_value;
312 /* Attribute Description is replaced with aliased one */
313 ma->ma_desc = aa->aa_aliased_ad;
314 ma->ma_rule = aa->aa_mr;
316 * Duplicate aa->aa_cf to ma->ma_cf by replacing the
317 * the component assertion value in assert_bv
318 * Multiple values may be separated with '$'
320 rc = dup_comp_filter ( op, &assert_bv, aa->aa_cf, &ma->ma_cf );
324 get_comp_filter( Operation* op, struct berval* bv, ComponentFilter** filt,
327 ComponentAssertionValue cav;
330 Debug( LDAP_DEBUG_FILTER, "get_comp_filter\n", 0, 0, 0 );
331 if ( (rc = slapd_ber2cav(bv, &cav) ) != LDAP_SUCCESS ) {
334 rc = parse_comp_filter( op, &cav, filt, text );
335 bv->bv_val = cav.cav_ptr;
341 eat_whsp( ComponentAssertionValue* cav )
343 for ( ; ( *cav->cav_ptr == ' ' ) && ( cav->cav_ptr < cav->cav_end ) ; ) {
349 cav_cur_len( ComponentAssertionValue* cav )
351 return cav->cav_end - cav->cav_ptr;
355 comp_first_element( ComponentAssertionValue* cav )
358 if ( cav_cur_len( cav ) >= 8 && strncmp( cav->cav_ptr, "item", 4 ) == 0 ) {
359 return LDAP_COMP_FILTER_ITEM;
361 else if ( cav_cur_len( cav ) >= 7 && strncmp( cav->cav_ptr, "and", 3 ) == 0 ) {
362 return LDAP_COMP_FILTER_AND;
364 else if ( cav_cur_len( cav ) >= 6 && strncmp( cav->cav_ptr, "or" , 2 ) == 0 ) {
365 return LDAP_COMP_FILTER_OR;
367 else if ( cav_cur_len( cav ) >= 7 && strncmp( cav->cav_ptr, "not", 3 ) == 0 ) {
368 return LDAP_COMP_FILTER_NOT;
371 return LDAP_COMP_FILTER_UNDEFINED;
375 comp_next_element( ComponentAssertionValue* cav )
379 if ( *(cav->cav_ptr) == ',' ) {
380 /* move pointer to the next CA */
382 return comp_first_element( cav );
384 else return LDAP_COMP_FILTER_UNDEFINED;
388 get_comp_filter_list( Operation *op, ComponentAssertionValue *cav,
389 ComponentFilter** f, const char** text )
391 ComponentFilter **new;
395 Debug( LDAP_DEBUG_FILTER, "get_comp_filter_list\n", 0, 0, 0 );
397 for ( tag = comp_first_element( cav ); tag != LDAP_COMP_FILTER_UNDEFINED;
398 tag = comp_next_element( cav ) )
400 err = parse_comp_filter( op, cav, new, text );
401 if ( err != LDAP_SUCCESS )
403 new = &(*new)->cf_next;
407 return( LDAP_SUCCESS );
411 get_componentId( Operation *op, ComponentAssertionValue* cav,
412 ComponentId ** cid, const char** text )
418 type = peek_componentId_type( cav );
420 Debug( LDAP_DEBUG_FILTER, "get_compId [%d]\n", type, 0, 0 );
425 case LDAP_COMPREF_IDENTIFIER :
426 _cid.ci_val.ci_identifier.bv_val = cav->cav_ptr;
427 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
428 cav->cav_ptr[len] != '.' && cav->cav_ptr[len] != '\"' ; len++ );
429 _cid.ci_val.ci_identifier.bv_len = len;
432 case LDAP_COMPREF_FROM_BEGINNING :
433 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
434 cav->cav_ptr[len] != '.' && cav->cav_ptr[len] != '\"' ; len++ );
435 _cid.ci_val.ci_from_beginning = strtol( cav->cav_ptr, NULL, 0 );
438 case LDAP_COMPREF_FROM_END :
439 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
440 cav->cav_ptr[len] != '.' && cav->cav_ptr[len] != '\"' ; len++ );
441 _cid.ci_val.ci_from_end = strtol( cav->cav_ptr, NULL, 0 );
444 case LDAP_COMPREF_COUNT :
445 _cid.ci_val.ci_count = 0;
448 case LDAP_COMPREF_CONTENT :
449 _cid.ci_val.ci_content = 1;
450 cav->cav_ptr += strlen("content");
452 case LDAP_COMPREF_SELECT :
453 if ( cav->cav_ptr[len] != '(' )
454 return LDAP_COMPREF_UNDEFINED;
455 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
456 cav->cav_ptr[len] != '\"' && cav->cav_ptr[len] != ')'
458 _cid.ci_val.ci_select_value.bv_val = cav->cav_ptr + 1;
459 _cid.ci_val.ci_select_value.bv_len = len - 1 ;
460 cav->cav_ptr += len + 1;
462 case LDAP_COMPREF_ALL :
463 _cid.ci_val.ci_all = '*';
467 return LDAP_COMPREF_UNDEFINED;
471 *cid = op->o_tmpalloc( sizeof( ComponentId ), op->o_tmpmemctx );
473 *cid = malloc( sizeof( ComponentId ) );
479 peek_componentId_type( ComponentAssertionValue* cav )
482 if ( cav->cav_ptr[0] == '-' )
483 return LDAP_COMPREF_FROM_END;
484 else if ( cav->cav_ptr[0] == '(' )
485 return LDAP_COMPREF_SELECT;
486 else if ( cav->cav_ptr[0] == '*' )
487 return LDAP_COMPREF_ALL;
488 else if ( cav->cav_ptr[0] == '0' )
489 return LDAP_COMPREF_COUNT;
490 else if ( cav->cav_ptr[0] > '0' && cav->cav_ptr[0] <= '9' )
491 return LDAP_COMPREF_FROM_BEGINNING;
492 else if ( (cav->cav_end - cav->cav_ptr) >= 7 &&
493 strncmp(cav->cav_ptr,"content",7) == 0 )
494 return LDAP_COMPREF_CONTENT;
495 else if ( (cav->cav_ptr[0] >= 'a' && cav->cav_ptr[0] <= 'z') ||
496 (cav->cav_ptr[0] >= 'A' && cav->cav_ptr[0] <= 'Z') )
498 return LDAP_COMPREF_IDENTIFIER;
500 return LDAP_COMPREF_UNDEFINED;
504 comp_next_id( ComponentAssertionValue* cav )
507 if ( *(cav->cav_ptr) == '.' ) {
509 return LDAP_COMPREF_DEFINED;
511 else return LDAP_COMPREF_UNDEFINED;
515 get_component_reference( Operation *op, ComponentAssertionValue* cav,
516 ComponentReference** cr, const char** text )
520 ComponentReference* ca_comp_ref;
521 ComponentId** cr_list;
525 if ( ( rc = strip_cav_str( cav,"\"") ) != LDAP_SUCCESS )
529 ca_comp_ref = op->o_tmpalloc( sizeof( ComponentReference ), op->o_tmpmemctx );
531 ca_comp_ref = malloc( sizeof( ComponentReference ) );
533 if ( !ca_comp_ref ) return LDAP_NO_MEMORY;
535 cr_list = &ca_comp_ref->cr_list;
537 for ( type = peek_componentId_type( cav ) ; type != LDAP_COMPREF_UNDEFINED
538 ; type = comp_next_id( cav ), count++ ) {
539 rc = get_componentId( op, cav, cr_list, text );
540 if ( rc == LDAP_SUCCESS ) {
541 if ( count == 0 ) ca_comp_ref->cr_curr = ca_comp_ref->cr_list;
542 cr_list = &(*cr_list)->ci_next;
544 else if ( rc == LDAP_COMPREF_UNDEFINED )
547 ca_comp_ref->cr_len = count;
549 if ( ( rc = strip_cav_str( cav,"\"") ) != LDAP_SUCCESS ) {
551 op->o_tmpfree( ca_comp_ref , op->o_tmpmemctx );
557 if ( rc == LDAP_SUCCESS ) {
562 op->o_tmpfree( ca_comp_ref , op->o_tmpmemctx );
564 free( ca_comp_ref ) ;
570 get_ca_use_default( Operation *op, ComponentAssertionValue* cav,
571 int* ca_use_def, const char** text )
573 strip_cav_str( cav, "useDefaultValues" );
574 if ( peek_cav_str( cav, "TRUE" ) == LDAP_SUCCESS ) {
575 strip_cav_str( cav, "TRUE" );
577 } else if ( peek_cav_str( cav, "FALSE" ) == LDAP_SUCCESS ) {
578 strip_cav_str( cav, "FALSE" );
581 return LDAP_INVALID_SYNTAX;
588 get_matching_rule( Operation *op, ComponentAssertionValue* cav,
589 MatchingRule** mr, const char** text )
592 struct berval rule_text = { 0L, NULL };
596 for ( ; ; count++ ) {
597 if ( cav->cav_ptr[count] == ' ' || cav->cav_ptr[count] == ',' ||
598 cav->cav_ptr[count] == '\0' || cav->cav_ptr[count] == '{' ||
599 cav->cav_ptr[count] == '}' || cav->cav_ptr[count] == '\n' )
604 *text = "component matching rule not recognized";
605 return LDAP_INAPPROPRIATE_MATCHING;
608 rule_text.bv_len = count;
609 rule_text.bv_val = cav->cav_ptr;
610 *mr = mr_bvfind( &rule_text );
611 cav->cav_ptr += count;
612 Debug( LDAP_DEBUG_FILTER, "get_matching_rule: %s\n", (*mr)->smr_mrule.mr_oid, 0, 0 );
614 *text = "component matching rule not recognized";
615 return LDAP_INAPPROPRIATE_MATCHING;
621 get_GSER_value( ComponentAssertionValue* cav, struct berval* bv )
623 int count, sequent_dquote, unclosed_brace, succeed;
627 * Four cases of GSER <Values>
629 * StringVal, GeneralizedTimeVal, UTCTimeVal, ObjectDescriptorVal
630 * 2) '...'B or '...'H :
631 * BitStringVal, OctetStringVal
633 * SEQUENCE, SEQUENCEOF, SETOF, SET, CHOICE
634 * 4) Between two white spaces
635 * INTEGER, BOOLEAN, NULL,ENUMERATE, etc
639 if ( cav->cav_ptr[0] == '"' ) {
640 for( count = 1, sequent_dquote = 0 ; ; count++ ) {
641 /* In order to find escaped double quote */
642 if ( cav->cav_ptr[count] == '"' ) sequent_dquote++;
643 else sequent_dquote = 0;
645 if ( cav->cav_ptr[count] == '\0' || (cav->cav_ptr+count) > cav->cav_end ) {
649 if ( ( cav->cav_ptr[count] == '"' && cav->cav_ptr[count-1] != '"') ||
650 ( sequent_dquote > 2 && (sequent_dquote%2) == 1 ) ) {
656 if ( !succeed || cav->cav_ptr[count] != '"' )
657 return LDAP_FILTER_ERROR;
659 bv->bv_val = cav->cav_ptr + 1;
660 bv->bv_len = count - 1; /* exclude '"' */
662 else if ( cav->cav_ptr[0] == '\'' ) {
663 for( count = 1 ; ; count++ ) {
664 if ( cav->cav_ptr[count] == '\0' || (cav->cav_ptr+count) > cav->cav_end ) {
667 if ((cav->cav_ptr[count-1] == '\'' && cav->cav_ptr[count] == 'B')||
668 (cav->cav_ptr[count-1] == '\'' && cav->cav_ptr[count] == 'H') ) {
674 if ( !succeed || !(cav->cav_ptr[count] == 'H' || cav->cav_ptr[count] == 'B') )
675 return LDAP_FILTER_ERROR;
677 bv->bv_val = cav->cav_ptr + 1;/*the next to '"' */
678 bv->bv_len = count - 2;/* exclude "'H" or "'B" */
681 else if ( cav->cav_ptr[0] == '{' ) {
682 for( count = 1, unclosed_brace = 1 ; ; count++ ) {
683 if ( cav->cav_ptr[count] == '{' ) unclosed_brace++;
684 if ( cav->cav_ptr[count] == '}' ) unclosed_brace--;
686 if ( cav->cav_ptr[count] == '\0' || (cav->cav_ptr+count) > cav->cav_end )
688 if ( unclosed_brace == 0 ) {
694 if ( !succeed || cav->cav_ptr[count] != '}' )
695 return LDAP_FILTER_ERROR;
697 bv->bv_val = cav->cav_ptr + 1;/*the next to '"' */
698 bv->bv_len = count - 1;/* exclude "'B" */
702 /*Find following white space where the value is ended*/
703 for( count = 1 ; ; count++ ) {
704 if ( cav->cav_ptr[count] == '\0' || cav->cav_ptr[count] == ' ' || (cav->cav_ptr+count) > cav->cav_end ) {
708 bv->bv_val = cav->cav_ptr;
712 cav->cav_ptr += bv->bv_len;
718 get_matching_value( Operation *op, ComponentAssertion* ca,
719 ComponentAssertionValue* cav, struct berval* bv,
722 if ( !(ca->ca_ma_rule->smr_usage & (SLAP_MR_COMPONENT)) ) {
723 if ( get_GSER_value( cav, bv ) != LDAP_SUCCESS ) {
724 return LDAP_FILTER_ERROR;
728 /* embeded componentFilterMatch Description */
729 bv->bv_val = cav->cav_ptr;
730 bv->bv_len = cav_cur_len( cav );
736 /* Don't move the position pointer, just peek given string */
738 peek_cav_str( ComponentAssertionValue* cav, char* str )
741 if ( cav_cur_len( cav ) >= strlen( str ) &&
742 strncmp( cav->cav_ptr, str, strlen( str ) ) == 0 )
745 return LDAP_INVALID_SYNTAX;
749 strip_cav_str( ComponentAssertionValue* cav, char* str)
752 if ( cav_cur_len( cav ) >= strlen( str ) &&
753 strncmp( cav->cav_ptr, str, strlen( str ) ) == 0 ) {
754 cav->cav_ptr += strlen( str );
758 return LDAP_INVALID_SYNTAX;
762 * TAG : "item", "and", "or", "not"
765 strip_cav_tag( ComponentAssertionValue* cav )
769 if ( cav_cur_len( cav ) >= 8 && strncmp( cav->cav_ptr, "item", 4 ) == 0 ) {
770 strip_cav_str( cav , "item:" );
771 return LDAP_COMP_FILTER_ITEM;
773 else if ( cav_cur_len( cav ) >= 7 && strncmp( cav->cav_ptr, "and", 3 ) == 0 ) {
774 strip_cav_str( cav , "and:" );
775 return LDAP_COMP_FILTER_AND;
777 else if ( cav_cur_len( cav ) >= 6 && strncmp( cav->cav_ptr, "or" , 2 ) == 0 ) {
778 strip_cav_str( cav , "or:" );
779 return LDAP_COMP_FILTER_OR;
781 else if ( cav_cur_len( cav ) >= 7 && strncmp( cav->cav_ptr, "not", 3 ) == 0 ) {
782 strip_cav_str( cav , "not:" );
783 return LDAP_COMP_FILTER_NOT;
790 * when encoding, "item" is denotation of ComponentAssertion
791 * ComponentAssertion :: SEQUENCE {
792 * component ComponentReference (SIZE(1..MAX)) OPTIONAL,
793 * useDefaultValues BOOLEAN DEFAULT TRUE,
794 * rule MATCHING-RULE.&id,
795 * value MATCHING-RULE.&AssertionType }
798 get_item( Operation *op, ComponentAssertionValue* cav, ComponentAssertion** ca,
802 ComponentAssertion* _ca;
807 Debug( LDAP_DEBUG_FILTER, "get_item \n", 0, 0, 0 );
809 _ca = op->o_tmpalloc( sizeof( ComponentAssertion ), op->o_tmpmemctx );
811 _ca = malloc( sizeof( ComponentAssertion ) );
813 if ( !_ca ) return LDAP_NO_MEMORY;
815 _ca->ca_comp_data.cd_tree = NULL;
816 _ca->ca_comp_data.cd_mem_op = NULL;
818 rc = peek_cav_str( cav, "component" );
819 if ( rc == LDAP_SUCCESS ) {
820 strip_cav_str( cav, "component" );
821 rc = get_component_reference( op, cav, &_ca->ca_comp_ref, text );
822 if ( rc != LDAP_SUCCESS ) {
824 op->o_tmpfree( _ca, op->o_tmpmemctx );
827 return LDAP_INVALID_SYNTAX;
831 if ( ( rc = strip_cav_str( cav,",") ) != LDAP_SUCCESS )
834 rc = peek_cav_str( cav, "useDefaultValues");
835 if ( rc == LDAP_SUCCESS ) {
836 rc = get_ca_use_default( op, cav, &_ca->ca_use_def, text );
837 if ( rc != LDAP_SUCCESS ) {
839 op->o_tmpfree( _ca, op->o_tmpmemctx );
842 return LDAP_INVALID_SYNTAX;
844 if ( ( rc = strip_cav_str( cav,",") ) != LDAP_SUCCESS )
847 else _ca->ca_use_def = 1;
849 if ( !( strip_cav_str( cav, "rule" ) == LDAP_SUCCESS &&
850 get_matching_rule( op, cav , &_ca->ca_ma_rule, text ) == LDAP_SUCCESS )) {
852 op->o_tmpfree( _ca, op->o_tmpmemctx );
855 return LDAP_INAPPROPRIATE_MATCHING;
858 if ( ( rc = strip_cav_str( cav,",") ) != LDAP_SUCCESS )
860 if ( !(strip_cav_str( cav, "value" ) == LDAP_SUCCESS &&
861 get_matching_value( op, _ca, cav,&value ,text ) == LDAP_SUCCESS )) {
863 op->o_tmpfree( _ca, op->o_tmpmemctx );
866 return LDAP_INVALID_SYNTAX;
870 * Normalize the value of this component assertion when the matching
871 * rule is one of existing matching rules
873 mr = _ca->ca_ma_rule;
874 if ( op && !(mr->smr_usage & (SLAP_MR_COMPONENT)) && mr->smr_normalize ) {
876 value.bv_val[value.bv_len] = '\0';
877 rc = mr->smr_normalize (
878 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
880 &value, &_ca->ca_ma_value, op->o_tmpmemctx );
881 if ( rc != LDAP_SUCCESS )
885 _ca->ca_ma_value = value;
887 * Validate the value of this component assertion
889 if ( op && mr->smr_syntax->ssyn_validate( mr->smr_syntax, &_ca->ca_ma_value) != LDAP_SUCCESS ) {
890 return LDAP_INVALID_SYNTAX;
894 /* componentFilterMatch contains componentFilterMatch in it */
895 if ( strcmp(_ca->ca_ma_rule->smr_mrule.mr_oid, OID_COMP_FILTER_MATCH ) == 0) {
897 bv.bv_val = cav->cav_ptr;
898 bv.bv_len = cav_cur_len( cav );
899 rc = get_comp_filter( op, &bv,(ComponentFilter**)&_ca->ca_cf, text );
900 if ( rc != LDAP_SUCCESS ) {
902 op->o_tmpfree( _ca, op->o_tmpmemctx );
907 cav->cav_ptr = bv.bv_val;
908 assert( cav->cav_end >= bv.bv_val );
916 parse_comp_filter( Operation* op, ComponentAssertionValue* cav,
917 ComponentFilter** filt, const char** text )
920 * A component filter looks like this coming in:
921 * Filter ::= CHOICE {
922 * item [0] ComponentAssertion,
923 * and [1] SEQUENCE OF ComponentFilter,
924 * or [2] SEQUENCE OF ComponentFilter,
925 * not [3] ComponentFilter,
932 /* TAG : item, and, or, not in RFC 2254 */
933 tag = strip_cav_tag( cav );
935 if ( tag == LBER_ERROR ) {
936 *text = "error decoding comp filter";
937 return LDAP_PROTOCOL_ERROR;
940 if ( tag != LDAP_COMP_FILTER_NOT )
941 strip_cav_str( cav, "{");
948 switch ( f.cf_choice ) {
949 case LDAP_COMP_FILTER_AND:
950 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_AND\n", 0, 0, 0 );
951 err = get_comp_filter_list( op, cav, &f.cf_and, text );
952 if ( err != LDAP_SUCCESS ) {
955 if ( f.cf_and == NULL ) {
956 f.cf_choice = SLAPD_FILTER_COMPUTED;
957 f.cf_result = LDAP_COMPARE_TRUE;
961 case LDAP_COMP_FILTER_OR:
962 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_OR\n", 0, 0, 0 );
963 err = get_comp_filter_list( op, cav, &f.cf_or, text );
964 if ( err != LDAP_SUCCESS ) {
967 if ( f.cf_or == NULL ) {
968 f.cf_choice = SLAPD_FILTER_COMPUTED;
969 f.cf_result = LDAP_COMPARE_FALSE;
971 /* no assert - list could be empty */
974 case LDAP_COMP_FILTER_NOT:
975 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_NOT\n", 0, 0, 0 );
976 err = parse_comp_filter( op, cav, &f.cf_not, text );
977 if ( err != LDAP_SUCCESS ) {
981 assert( f.cf_not != NULL );
982 if ( f.cf_not->cf_choice == SLAPD_FILTER_COMPUTED ) {
983 int fresult = f.cf_not->cf_result;
984 f.cf_choice = SLAPD_FILTER_COMPUTED;
985 op->o_tmpfree( f.cf_not, op->o_tmpmemctx );
989 case LDAP_COMPARE_TRUE:
990 f.cf_result = LDAP_COMPARE_FALSE;
992 case LDAP_COMPARE_FALSE:
993 f.cf_result = LDAP_COMPARE_TRUE;
996 /* (!Undefined) is Undefined */
1001 case LDAP_COMP_FILTER_ITEM:
1002 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_ITEM\n", 0, 0, 0 );
1003 err = get_item( op, cav, &f.cf_ca, text );
1004 if ( err != LDAP_SUCCESS ) {
1008 assert( f.cf_ca != NULL );
1012 f.cf_choice = SLAPD_FILTER_COMPUTED;
1013 f.cf_result = SLAPD_COMPARE_UNDEFINED;
1017 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1018 *text = "Component Filter Syntax Error";
1022 if ( tag != LDAP_COMP_FILTER_NOT )
1023 strip_cav_str( cav, "}");
1025 if ( err == LDAP_SUCCESS ) {
1027 *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx );
1029 *filt = malloc( sizeof(f) );
1038 test_comp_filter_and(
1042 ComponentFilter *flist )
1045 int rtn = LDAP_COMPARE_TRUE;
1047 for ( f = flist ; f != NULL; f = f->cf_next ) {
1048 int rc = test_comp_filter( syn, a, bv, f );
1049 if ( rc == LDAP_COMPARE_FALSE ) {
1054 if ( rc != LDAP_COMPARE_TRUE ) {
1063 test_comp_filter_or(
1067 ComponentFilter *flist )
1070 int rtn = LDAP_COMPARE_TRUE;
1072 for ( f = flist ; f != NULL; f = f->cf_next ) {
1073 int rc = test_comp_filter( syn, a, bv, f );
1074 if ( rc == LDAP_COMPARE_TRUE ) {
1079 if ( rc != LDAP_COMPARE_FALSE ) {
1088 csi_value_match( MatchingRule *mr, struct berval* bv_attr,
1089 struct berval* bv_assert )
1094 assert( mr != NULL );
1095 assert( !(mr->smr_usage & SLAP_MR_COMPONENT) );
1097 if( !mr->smr_match ) {
1098 return LDAP_INAPPROPRIATE_MATCHING;
1101 rc = (mr->smr_match)( &match, 0, NULL /*ad->ad_type->sat_syntax*/,
1102 mr, bv_attr, bv_assert );
1103 if ( rc == LDAP_SUCCESS )
1104 return match? LDAP_COMPARE_FALSE:LDAP_COMPARE_TRUE;
1110 * return codes : LDAP_COMPARE_TRUE, LDAP_COMPARE_FALSE
1113 test_comp_filter_item(
1117 ComponentAssertion *ca )
1120 ComponentSyntaxInfo* csi_attr, *csi_assert=NULL;
1121 void *attr_nm, *assert_nm;
1123 if ( strcmp(ca->ca_ma_rule->smr_mrule.mr_oid,
1124 OID_COMP_FILTER_MATCH ) == 0 && ca->ca_cf ) {
1125 /* componentFilterMatch inside of componentFilterMatch */
1126 rc = test_comp_filter( syn, a, bv, ca->ca_cf );
1130 /* load attribute containg components */
1131 if ( !a->a_comp_data && attr_converter && nibble_mem_allocator ) {
1132 a->a_comp_data = malloc( sizeof( ComponentData ) );
1133 /* Memory chunk pre-allocation for decoders */
1134 a->a_comp_data->cd_mem_op = (void*) nibble_mem_allocator ( 1024, 128 );
1135 a->a_comp_data->cd_tree = attr_converter (a, syn, bv);
1138 if ( a->a_comp_data->cd_tree == NULL ) {
1139 nibble_mem_free( a->a_comp_data->cd_mem_op );
1140 free ( a->a_comp_data );
1141 a->a_comp_data = NULL;
1142 return LDAP_PROTOCOL_ERROR;
1145 /* Memory for storing will-be-extracted attribute values */
1146 attr_nm = nibble_mem_allocator ( 1024*4 , 1024 );
1147 if ( !attr_nm )return LDAP_PROTOCOL_ERROR;
1149 /* Memory for storing component assertion values */
1150 if( !ca->ca_comp_data.cd_mem_op ) {
1151 assert_nm = nibble_mem_allocator ( 256, 64 );
1153 nibble_mem_free ( attr_nm );
1154 return LDAP_PROTOCOL_ERROR;
1156 ca->ca_comp_data.cd_mem_op = assert_nm;
1159 assert_nm = ca->ca_comp_data.cd_mem_op;
1162 /* component reference initialization */
1163 ca->ca_comp_ref->cr_curr = ca->ca_comp_ref->cr_list;
1164 rc = test_components( attr_nm, assert_nm, (ComponentSyntaxInfo*)a->a_comp_data->cd_tree, ca );
1166 /* free memory used for storing extracted attribute value */
1167 nibble_mem_free ( attr_nm );
1176 ComponentFilter *f )
1180 if ( !f ) return LDAP_PROTOCOL_ERROR;
1182 Debug( LDAP_DEBUG_FILTER, "test_comp_filter\n", 0, 0, 0 );
1183 switch ( f->cf_choice ) {
1184 case SLAPD_FILTER_COMPUTED:
1187 case LDAP_COMP_FILTER_AND:
1188 rc = test_comp_filter_and( syn, a, bv, f->cf_and );
1190 case LDAP_COMP_FILTER_OR:
1191 rc = test_comp_filter_or( syn, a, bv, f->cf_or );
1193 case LDAP_COMP_FILTER_NOT:
1194 rc = test_comp_filter( syn, a, bv, f->cf_not );
1197 case LDAP_COMPARE_TRUE:
1198 rc = LDAP_COMPARE_FALSE;
1200 case LDAP_COMPARE_FALSE:
1201 rc = LDAP_COMPARE_TRUE;
1205 case LDAP_COMP_FILTER_ITEM:
1206 rc = test_comp_filter_item( syn, a, bv, f->cf_ca );
1209 rc = LDAP_PROTOCOL_ERROR;
1216 free_comp_filter_list( ComponentFilter* f )
1218 ComponentFilter* tmp;
1219 for ( tmp = f ; tmp; tmp = tmp->cf_next );
1221 free_comp_filter( tmp );
1226 free_comp_filter( ComponentFilter* f )
1228 switch ( f->cf_choice ) {
1229 case LDAP_COMP_FILTER_AND:
1230 case LDAP_COMP_FILTER_OR:
1231 free_comp_filter_list( f->cf_any );
1233 case LDAP_COMP_FILTER_NOT:
1234 free_comp_filter( f->cf_any );
1236 case LDAP_COMP_FILTER_ITEM:
1237 if ( nibble_mem_free && f->cf_ca->ca_comp_data.cd_mem_op )
1238 nibble_mem_free( f->cf_ca->ca_comp_data.cd_mem_op );
1247 component_free( ComponentFilter *f ) {
1248 free_comp_filter( f );