1 /* component.c -- Component Filter Match Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 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;
42 component_encoder_func* component_encoder = NULL;
43 get_component_info_func* get_component_description = NULL;
44 #define OID_ALL_COMP_MATCH "1.2.36.79672281.1.13.6"
45 #define OID_COMP_FILTER_MATCH "1.2.36.79672281.1.13.2"
46 #define MAX_LDAP_STR_LEN 128
49 peek_componentId_type( ComponentAssertionValue* cav );
52 strip_cav_str( ComponentAssertionValue* cav, char* str);
55 peek_cav_str( ComponentAssertionValue* cav, char* str );
58 parse_comp_filter( Operation* op, ComponentAssertionValue* cav,
59 ComponentFilter** filt, const char** text );
62 free_comp_filter( ComponentFilter* f );
65 test_comp_filter( Syntax *syn, ComponentSyntaxInfo *a, ComponentFilter *f );
68 componentCertificateValidate(
76 componentFilterValidate(
84 allComponentsValidate(
92 componentFilterMatch (
100 ComponentSyntaxInfo *csi_attr = (ComponentSyntaxInfo*)value;
101 MatchingRuleAssertion * ma = (MatchingRuleAssertion*)assertedValue;
104 if ( !mr || !ma->ma_cf )
105 return LDAP_INAPPROPRIATE_MATCHING;
106 /* Check if the component module is loaded */
107 if ( !attr_converter || !nibble_mem_allocator )
108 return LDAP_INAPPROPRIATE_MATCHING;
111 rc = test_comp_filter( syntax, csi_attr, ma->ma_cf );
113 if ( rc == LDAP_COMPARE_TRUE ) {
117 else if ( rc == LDAP_COMPARE_FALSE ) {
122 return LDAP_INAPPROPRIATE_MATCHING;
127 directoryComponentsMatch(
132 struct berval *value,
133 void *assertedValue )
135 /* Only for Registeration */
146 struct berval *value,
147 void *assertedValue )
149 /* Only for Registeration */
155 slapd_ber2cav( struct berval* bv, ComponentAssertionValue* cav )
159 cav->cav_ptr = cav->cav_buf = bv->bv_val;
160 cav->cav_end = bv->bv_val + bv->bv_len;
166 dup_comp_ref ( Operation* op, ComponentReference* cr )
169 ComponentReference* dup_cr;
170 ComponentId* ci_curr;
171 ComponentId** ci_temp;
174 dup_cr = op->o_tmpalloc( sizeof( ComponentReference ), op->o_tmpmemctx );
176 dup_cr->cr_len = cr->cr_len;
177 dup_cr->cr_string = cr->cr_string;
179 ci_temp = &dup_cr->cr_list;
180 ci_curr = cr->cr_list;
182 for ( ; ci_curr ; ci_curr = ci_curr->ci_next, ci_temp = &(*ci_temp)->ci_next ) {
183 *ci_temp = op->o_tmpalloc( sizeof( ComponentId ), op->o_tmpmemctx );
184 if ( !ci_temp ) return NULL;
185 **ci_temp = *ci_curr;
188 dup_cr->cr_curr = dup_cr->cr_list;
194 dup_comp_filter_list (
197 ComponentFilter* in_f,
198 ComponentFilter** out_f )
200 ComponentFilter **new, *f;
204 for ( f = in_f; f != NULL; f = f->cf_next ) {
205 rc = dup_comp_filter( op, bv, f, new );
206 if ( rc != LDAP_SUCCESS ) {
209 new = &(*new)->cf_next;
215 get_len_of_next_assert_value ( struct berval* bv, char separator )
219 if ( (bv->bv_val[ i ] == separator) || ( i >= bv->bv_len) )
223 bv->bv_val += (i + 1);
224 bv->bv_len -= (i + 1);
229 dup_comp_filter_item (
231 struct berval* assert_bv,
232 ComponentAssertion* in_ca,
233 ComponentAssertion** out_ca )
235 ComponentAssertion* ca;
238 if ( !in_ca->ca_comp_ref )
239 return SLAPD_DISCONNECT;
241 *out_ca = op->o_tmpalloc( sizeof( ComponentAssertion ), op->o_tmpmemctx );
243 return LDAP_NO_MEMORY;
245 (*out_ca)->ca_comp_data.cd_tree = NULL;
246 (*out_ca)->ca_comp_data.cd_mem_op = NULL;
248 (*out_ca)->ca_comp_ref = dup_comp_ref ( op, in_ca->ca_comp_ref );
249 (*out_ca)->ca_use_def = 0;
250 (*out_ca)->ca_ma_rule = in_ca->ca_ma_rule;
252 (*out_ca)->ca_ma_value.bv_val = assert_bv->bv_val;
253 len = get_len_of_next_assert_value ( assert_bv, '$' );
255 return SLAPD_DISCONNECT;
256 (*out_ca)->ca_ma_value.bv_len = len;
265 ComponentFilter *in_f,
266 ComponentFilter **out_f )
269 ComponentFilter dup_f = {0};
271 if ( !in_f ) return LDAP_PROTOCOL_ERROR;
273 switch ( in_f->cf_choice ) {
274 case LDAP_COMP_FILTER_AND:
275 rc = dup_comp_filter_list( op, bv, in_f->cf_and, &dup_f.cf_and);
276 dup_f.cf_choice = LDAP_COMP_FILTER_AND;
278 case LDAP_COMP_FILTER_OR:
279 rc = dup_comp_filter_list( op, bv, in_f->cf_or, &dup_f.cf_or);
280 dup_f.cf_choice = LDAP_COMP_FILTER_OR;
282 case LDAP_COMP_FILTER_NOT:
283 rc = dup_comp_filter( op, bv, in_f->cf_not, &dup_f.cf_not);
284 dup_f.cf_choice = LDAP_COMP_FILTER_NOT;
286 case LDAP_COMP_FILTER_ITEM:
287 rc = dup_comp_filter_item( op, bv, in_f->cf_ca ,&dup_f.cf_ca );
288 dup_f.cf_choice = LDAP_COMP_FILTER_ITEM;
291 rc = LDAP_PROTOCOL_ERROR;
294 if ( rc == LDAP_SUCCESS ) {
295 *out_f = op->o_tmpalloc( sizeof(dup_f), op->o_tmpmemctx );
304 get_aliased_filter ( Operation* op, MatchingRuleAssertion* ma, AttributeAliasing* aa, const char** text )
307 struct berval assert_bv;
308 ComponentAssertion* ca;
310 Debug( LDAP_DEBUG_FILTER, "get_aliased_filter\n", 0, 0, 0 );
312 if ( !aa->aa_cf ) return LDAP_PROTOCOL_ERROR;
314 assert_bv = ma->ma_value;
315 /* Attribute Description is replaced with aliased one */
316 ma->ma_desc = aa->aa_aliased_ad;
317 ma->ma_rule = aa->aa_mr;
319 * Duplicate aa->aa_cf to ma->ma_cf by replacing the
320 * the component assertion value in assert_bv
321 * Multiple values may be separated with '$'
323 return dup_comp_filter ( op, &assert_bv, aa->aa_cf, &ma->ma_cf );
327 get_comp_filter( Operation* op, struct berval* bv, ComponentFilter** filt,
330 ComponentAssertionValue cav;
333 Debug( LDAP_DEBUG_FILTER, "get_comp_filter\n", 0, 0, 0 );
334 if ( (rc = slapd_ber2cav(bv, &cav) ) != LDAP_SUCCESS ) {
337 rc = parse_comp_filter( op, &cav, filt, text );
338 bv->bv_val = cav.cav_ptr;
344 eat_whsp( ComponentAssertionValue* cav )
346 for ( ; ( *cav->cav_ptr == ' ' ) && ( cav->cav_ptr < cav->cav_end ) ; ) {
352 cav_cur_len( ComponentAssertionValue* cav )
354 return cav->cav_end - cav->cav_ptr;
358 comp_first_element( ComponentAssertionValue* cav )
361 if ( cav_cur_len( cav ) >= 8 && strncmp( cav->cav_ptr, "item", 4 ) == 0 ) {
362 return LDAP_COMP_FILTER_ITEM;
364 else if ( cav_cur_len( cav ) >= 7 && strncmp( cav->cav_ptr, "and", 3 ) == 0 ) {
365 return LDAP_COMP_FILTER_AND;
367 else if ( cav_cur_len( cav ) >= 6 && strncmp( cav->cav_ptr, "or" , 2 ) == 0 ) {
368 return LDAP_COMP_FILTER_OR;
370 else if ( cav_cur_len( cav ) >= 7 && strncmp( cav->cav_ptr, "not", 3 ) == 0 ) {
371 return LDAP_COMP_FILTER_NOT;
374 return LDAP_COMP_FILTER_UNDEFINED;
378 comp_next_element( ComponentAssertionValue* cav )
382 if ( *(cav->cav_ptr) == ',' ) {
383 /* move pointer to the next CA */
385 return comp_first_element( cav );
387 else return LDAP_COMP_FILTER_UNDEFINED;
391 get_comp_filter_list( Operation *op, ComponentAssertionValue *cav,
392 ComponentFilter** f, const char** text )
394 ComponentFilter **new;
398 Debug( LDAP_DEBUG_FILTER, "get_comp_filter_list\n", 0, 0, 0 );
400 for ( tag = comp_first_element( cav ); tag != LDAP_COMP_FILTER_UNDEFINED;
401 tag = comp_next_element( cav ) )
403 err = parse_comp_filter( op, cav, new, text );
404 if ( err != LDAP_SUCCESS )
406 new = &(*new)->cf_next;
410 return( LDAP_SUCCESS );
414 get_componentId( Operation *op, ComponentAssertionValue* cav,
415 ComponentId ** cid, const char** text )
421 type = peek_componentId_type( cav );
423 Debug( LDAP_DEBUG_FILTER, "get_compId [%d]\n", type, 0, 0 );
428 case LDAP_COMPREF_IDENTIFIER :
429 _cid.ci_val.ci_identifier.bv_val = cav->cav_ptr;
430 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
431 cav->cav_ptr[len] != '.' && cav->cav_ptr[len] != '\"' ; len++ );
432 _cid.ci_val.ci_identifier.bv_len = len;
435 case LDAP_COMPREF_FROM_BEGINNING :
436 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
437 cav->cav_ptr[len] != '.' && cav->cav_ptr[len] != '\"' ; len++ );
438 _cid.ci_val.ci_from_beginning = strtol( cav->cav_ptr, NULL, 0 );
441 case LDAP_COMPREF_FROM_END :
442 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
443 cav->cav_ptr[len] != '.' && cav->cav_ptr[len] != '\"' ; len++ );
444 _cid.ci_val.ci_from_end = strtol( cav->cav_ptr, NULL, 0 );
447 case LDAP_COMPREF_COUNT :
448 _cid.ci_val.ci_count = 0;
451 case LDAP_COMPREF_CONTENT :
452 _cid.ci_val.ci_content = 1;
453 cav->cav_ptr += strlen("content");
455 case LDAP_COMPREF_SELECT :
456 if ( cav->cav_ptr[len] != '(' )
457 return LDAP_COMPREF_UNDEFINED;
458 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
459 cav->cav_ptr[len] != '\"' && cav->cav_ptr[len] != ')'
461 _cid.ci_val.ci_select_value.bv_val = cav->cav_ptr + 1;
462 _cid.ci_val.ci_select_value.bv_len = len - 1 ;
463 cav->cav_ptr += len + 1;
465 case LDAP_COMPREF_ALL :
466 _cid.ci_val.ci_all = '*';
470 return LDAP_COMPREF_UNDEFINED;
474 *cid = op->o_tmpalloc( sizeof( ComponentId ), op->o_tmpmemctx );
476 *cid = malloc( sizeof( ComponentId ) );
482 peek_componentId_type( ComponentAssertionValue* cav )
485 if ( cav->cav_ptr[0] == '-' )
486 return LDAP_COMPREF_FROM_END;
487 else if ( cav->cav_ptr[0] == '(' )
488 return LDAP_COMPREF_SELECT;
489 else if ( cav->cav_ptr[0] == '*' )
490 return LDAP_COMPREF_ALL;
491 else if ( cav->cav_ptr[0] == '0' )
492 return LDAP_COMPREF_COUNT;
493 else if ( cav->cav_ptr[0] > '0' && cav->cav_ptr[0] <= '9' )
494 return LDAP_COMPREF_FROM_BEGINNING;
495 else if ( (cav->cav_end - cav->cav_ptr) >= 7 &&
496 strncmp(cav->cav_ptr,"content",7) == 0 )
497 return LDAP_COMPREF_CONTENT;
498 else if ( (cav->cav_ptr[0] >= 'a' && cav->cav_ptr[0] <= 'z') ||
499 (cav->cav_ptr[0] >= 'A' && cav->cav_ptr[0] <= 'Z') )
501 return LDAP_COMPREF_IDENTIFIER;
503 return LDAP_COMPREF_UNDEFINED;
507 comp_next_id( ComponentAssertionValue* cav )
510 if ( *(cav->cav_ptr) == '.' ) {
512 return LDAP_COMPREF_DEFINED;
514 else return LDAP_COMPREF_UNDEFINED;
520 get_component_reference( Operation *op, ComponentAssertionValue* cav,
521 ComponentReference** cr, const char** text )
525 ComponentReference* ca_comp_ref;
526 ComponentId** cr_list;
531 start = cav->cav_ptr;
532 if ( ( rc = strip_cav_str( cav,"\"") ) != LDAP_SUCCESS )
535 ca_comp_ref = op->o_tmpalloc( sizeof( ComponentReference ), op->o_tmpmemctx );
537 ca_comp_ref = malloc( sizeof( ComponentReference ) );
539 if ( !ca_comp_ref ) return LDAP_NO_MEMORY;
541 cr_list = &ca_comp_ref->cr_list;
543 for ( type = peek_componentId_type( cav ) ; type != LDAP_COMPREF_UNDEFINED
544 ; type = comp_next_id( cav ), count++ ) {
545 rc = get_componentId( op, cav, cr_list, text );
546 if ( rc == LDAP_SUCCESS ) {
547 if ( count == 0 ) ca_comp_ref->cr_curr = ca_comp_ref->cr_list;
548 cr_list = &(*cr_list)->ci_next;
550 else if ( rc == LDAP_COMPREF_UNDEFINED )
553 ca_comp_ref->cr_len = count;
555 if ( ( rc = strip_cav_str( cav,"\"") ) != LDAP_SUCCESS ) {
557 op->o_tmpfree( ca_comp_ref , op->o_tmpmemctx );
563 if ( rc == LDAP_SUCCESS ) {
568 op->o_tmpfree( ca_comp_ref , op->o_tmpmemctx );
570 free( ca_comp_ref ) ;
572 (*cr)->cr_string.bv_val = start;
573 (*cr)->cr_string.bv_len = end - start + 1;
579 insert_component_reference( ComponentReference *cr, ComponentReference** cr_list) {
581 return LDAP_PARAM_ERROR;
586 cr->cr_next = *cr_list;
593 * If there is '.' in the name of a given attribute
594 * the first '.'- following characters are considered
595 * as a component reference of the attribute
596 * EX) userCertificate.toBeSigned.serialNumber
597 * attribute : userCertificate
598 * component reference : toBeSigned.serialNumber
601 is_component_reference( char* attr ) {
603 for ( i=0; attr[i] != '\0' ; i++ ) {
604 if ( attr[i] == '.' )
611 extract_component_reference( char* attr, ComponentReference** cr ) {
615 ComponentAssertionValue cav;
618 for ( i=0; attr[i] != '\0' ; i++ ) {
619 if ( attr[i] == '.' ) break;
623 return LDAP_PARAM_ERROR;
626 cr_ptr = attr + i + 1 ;
627 cr_len = strlen ( cr_ptr );
629 return LDAP_PARAM_ERROR;
631 /* enclosed between double quotes*/
632 cav.cav_ptr = cav.cav_buf = ch_malloc (cr_len+2);
633 memcpy( cav.cav_buf+1, cr_ptr, cr_len );
634 cav.cav_buf[0] = '"';
635 cav.cav_buf[cr_len+1] = '"';
636 cav.cav_end = cr_ptr + cr_len + 2;
638 rc = get_component_reference ( NULL, &cav, cr, (const char**)text );
639 if ( rc != LDAP_SUCCESS )
641 (*cr)->cr_string.bv_val = cav.cav_buf;
642 (*cr)->cr_string.bv_len = cr_len + 2;
647 get_ca_use_default( Operation *op, ComponentAssertionValue* cav,
648 int* ca_use_def, const char** text )
650 strip_cav_str( cav, "useDefaultValues" );
651 if ( peek_cav_str( cav, "TRUE" ) == LDAP_SUCCESS ) {
652 strip_cav_str( cav, "TRUE" );
654 } else if ( peek_cav_str( cav, "FALSE" ) == LDAP_SUCCESS ) {
655 strip_cav_str( cav, "FALSE" );
658 return LDAP_INVALID_SYNTAX;
665 get_matching_rule( Operation *op, ComponentAssertionValue* cav,
666 MatchingRule** mr, const char** text )
669 struct berval rule_text = { 0L, NULL };
673 for ( ; ; count++ ) {
674 if ( cav->cav_ptr[count] == ' ' || cav->cav_ptr[count] == ',' ||
675 cav->cav_ptr[count] == '\0' || cav->cav_ptr[count] == '{' ||
676 cav->cav_ptr[count] == '}' || cav->cav_ptr[count] == '\n' )
681 *text = "component matching rule not recognized";
682 return LDAP_INAPPROPRIATE_MATCHING;
685 rule_text.bv_len = count;
686 rule_text.bv_val = cav->cav_ptr;
687 *mr = mr_bvfind( &rule_text );
688 cav->cav_ptr += count;
689 Debug( LDAP_DEBUG_FILTER, "get_matching_rule: %s\n", (*mr)->smr_mrule.mr_oid, 0, 0 );
691 *text = "component matching rule not recognized";
692 return LDAP_INAPPROPRIATE_MATCHING;
698 get_GSER_value( ComponentAssertionValue* cav, struct berval* bv )
700 int count, sequent_dquote, unclosed_brace, succeed;
704 * Four cases of GSER <Values>
706 * StringVal, GeneralizedTimeVal, UTCTimeVal, ObjectDescriptorVal
707 * 2) '...'B or '...'H :
708 * BitStringVal, OctetStringVal
710 * SEQUENCE, SEQUENCEOF, SETOF, SET, CHOICE
711 * 4) Between two white spaces
712 * INTEGER, BOOLEAN, NULL,ENUMERATE, etc
716 if ( cav->cav_ptr[0] == '"' ) {
717 for( count = 1, sequent_dquote = 0 ; ; count++ ) {
718 /* In order to find escaped double quote */
719 if ( cav->cav_ptr[count] == '"' ) sequent_dquote++;
720 else sequent_dquote = 0;
722 if ( cav->cav_ptr[count] == '\0' || (cav->cav_ptr+count) > cav->cav_end ) {
726 if ( ( cav->cav_ptr[count] == '"' && cav->cav_ptr[count-1] != '"') ||
727 ( sequent_dquote > 2 && (sequent_dquote%2) == 1 ) ) {
733 if ( !succeed || cav->cav_ptr[count] != '"' )
734 return LDAP_FILTER_ERROR;
736 bv->bv_val = cav->cav_ptr + 1;
737 bv->bv_len = count - 1; /* exclude '"' */
739 else if ( cav->cav_ptr[0] == '\'' ) {
740 for( count = 1 ; ; count++ ) {
741 if ( cav->cav_ptr[count] == '\0' || (cav->cav_ptr+count) > cav->cav_end ) {
744 if ((cav->cav_ptr[count-1] == '\'' && cav->cav_ptr[count] == 'B')||
745 (cav->cav_ptr[count-1] == '\'' && cav->cav_ptr[count] == 'H') ) {
751 if ( !succeed || !(cav->cav_ptr[count] == 'H' || cav->cav_ptr[count] == 'B') )
752 return LDAP_FILTER_ERROR;
754 bv->bv_val = cav->cav_ptr + 1;/*the next to '"' */
755 bv->bv_len = count - 2;/* exclude "'H" or "'B" */
758 else if ( cav->cav_ptr[0] == '{' ) {
759 for( count = 1, unclosed_brace = 1 ; ; count++ ) {
760 if ( cav->cav_ptr[count] == '{' ) unclosed_brace++;
761 if ( cav->cav_ptr[count] == '}' ) unclosed_brace--;
763 if ( cav->cav_ptr[count] == '\0' || (cav->cav_ptr+count) > cav->cav_end )
765 if ( unclosed_brace == 0 ) {
771 if ( !succeed || cav->cav_ptr[count] != '}' )
772 return LDAP_FILTER_ERROR;
774 bv->bv_val = cav->cav_ptr + 1;/*the next to '"' */
775 bv->bv_len = count - 1;/* exclude "'B" */
779 /*Find following white space where the value is ended*/
780 for( count = 1 ; ; count++ ) {
781 if ( cav->cav_ptr[count] == '\0' || cav->cav_ptr[count] == ' ' || cav->cav_ptr[count] == '}' || cav->cav_ptr[count] == '{' || (cav->cav_ptr+count) > cav->cav_end ) {
785 bv->bv_val = cav->cav_ptr;
789 cav->cav_ptr += bv->bv_len;
795 get_matching_value( Operation *op, ComponentAssertion* ca,
796 ComponentAssertionValue* cav, struct berval* bv,
799 if ( !(ca->ca_ma_rule->smr_usage & (SLAP_MR_COMPONENT)) ) {
800 if ( get_GSER_value( cav, bv ) != LDAP_SUCCESS ) {
801 return LDAP_FILTER_ERROR;
805 /* embeded componentFilterMatch Description */
806 bv->bv_val = cav->cav_ptr;
807 bv->bv_len = cav_cur_len( cav );
813 /* Don't move the position pointer, just peek given string */
815 peek_cav_str( ComponentAssertionValue* cav, char* str )
818 if ( cav_cur_len( cav ) >= strlen( str ) &&
819 strncmp( cav->cav_ptr, str, strlen( str ) ) == 0 )
822 return LDAP_INVALID_SYNTAX;
826 strip_cav_str( ComponentAssertionValue* cav, char* str)
829 if ( cav_cur_len( cav ) >= strlen( str ) &&
830 strncmp( cav->cav_ptr, str, strlen( str ) ) == 0 ) {
831 cav->cav_ptr += strlen( str );
835 return LDAP_INVALID_SYNTAX;
839 * TAG : "item", "and", "or", "not"
842 strip_cav_tag( ComponentAssertionValue* cav )
846 if ( cav_cur_len( cav ) >= 8 && strncmp( cav->cav_ptr, "item", 4 ) == 0 ) {
847 strip_cav_str( cav , "item:" );
848 return LDAP_COMP_FILTER_ITEM;
850 else if ( cav_cur_len( cav ) >= 7 && strncmp( cav->cav_ptr, "and", 3 ) == 0 ) {
851 strip_cav_str( cav , "and:" );
852 return LDAP_COMP_FILTER_AND;
854 else if ( cav_cur_len( cav ) >= 6 && strncmp( cav->cav_ptr, "or" , 2 ) == 0 ) {
855 strip_cav_str( cav , "or:" );
856 return LDAP_COMP_FILTER_OR;
858 else if ( cav_cur_len( cav ) >= 7 && strncmp( cav->cav_ptr, "not", 3 ) == 0 ) {
859 strip_cav_str( cav , "not:" );
860 return LDAP_COMP_FILTER_NOT;
867 * when encoding, "item" is denotation of ComponentAssertion
868 * ComponentAssertion :: SEQUENCE {
869 * component ComponentReference (SIZE(1..MAX)) OPTIONAL,
870 * useDefaultValues BOOLEAN DEFAULT TRUE,
871 * rule MATCHING-RULE.&id,
872 * value MATCHING-RULE.&AssertionType }
875 get_item( Operation *op, ComponentAssertionValue* cav, ComponentAssertion** ca,
879 ComponentAssertion* _ca;
884 Debug( LDAP_DEBUG_FILTER, "get_item \n", 0, 0, 0 );
886 _ca = op->o_tmpalloc( sizeof( ComponentAssertion ), op->o_tmpmemctx );
888 _ca = malloc( sizeof( ComponentAssertion ) );
890 if ( !_ca ) return LDAP_NO_MEMORY;
892 _ca->ca_comp_data.cd_tree = NULL;
893 _ca->ca_comp_data.cd_mem_op = NULL;
895 rc = peek_cav_str( cav, "component" );
896 if ( rc == LDAP_SUCCESS ) {
897 strip_cav_str( cav, "component" );
898 rc = get_component_reference( op, cav, &_ca->ca_comp_ref, text );
899 if ( rc != LDAP_SUCCESS ) {
901 op->o_tmpfree( _ca, op->o_tmpmemctx );
904 return LDAP_INVALID_SYNTAX;
906 if ( ( rc = strip_cav_str( cav,",") ) != LDAP_SUCCESS )
909 _ca->ca_comp_ref = NULL;
912 rc = peek_cav_str( cav, "useDefaultValues");
913 if ( rc == LDAP_SUCCESS ) {
914 rc = get_ca_use_default( op, cav, &_ca->ca_use_def, text );
915 if ( rc != LDAP_SUCCESS ) {
917 op->o_tmpfree( _ca, op->o_tmpmemctx );
920 return LDAP_INVALID_SYNTAX;
922 if ( ( rc = strip_cav_str( cav,",") ) != LDAP_SUCCESS )
925 else _ca->ca_use_def = 1;
927 if ( !( strip_cav_str( cav, "rule" ) == LDAP_SUCCESS &&
928 get_matching_rule( op, cav , &_ca->ca_ma_rule, text ) == LDAP_SUCCESS )) {
930 op->o_tmpfree( _ca, op->o_tmpmemctx );
933 return LDAP_INAPPROPRIATE_MATCHING;
936 if ( ( rc = strip_cav_str( cav,",") ) != LDAP_SUCCESS )
938 if ( !(strip_cav_str( cav, "value" ) == LDAP_SUCCESS &&
939 get_matching_value( op, _ca, cav,&value ,text ) == LDAP_SUCCESS )) {
941 op->o_tmpfree( _ca, op->o_tmpmemctx );
944 return LDAP_INVALID_SYNTAX;
948 * Normalize the value of this component assertion when the matching
949 * rule is one of existing matching rules
951 mr = _ca->ca_ma_rule;
952 if ( op && !(mr->smr_usage & (SLAP_MR_COMPONENT)) && mr->smr_normalize ) {
954 value.bv_val[value.bv_len] = '\0';
955 rc = mr->smr_normalize (
956 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
958 &value, &_ca->ca_ma_value, op->o_tmpmemctx );
959 if ( rc != LDAP_SUCCESS )
963 _ca->ca_ma_value = value;
965 * Validate the value of this component assertion
967 if ( op && mr->smr_syntax->ssyn_validate( mr->smr_syntax, &_ca->ca_ma_value) != LDAP_SUCCESS ) {
968 return LDAP_INVALID_SYNTAX;
972 /* componentFilterMatch contains componentFilterMatch in it */
973 if ( strcmp(_ca->ca_ma_rule->smr_mrule.mr_oid, OID_COMP_FILTER_MATCH ) == 0) {
975 bv.bv_val = cav->cav_ptr;
976 bv.bv_len = cav_cur_len( cav );
977 rc = get_comp_filter( op, &bv,(ComponentFilter**)&_ca->ca_cf, text );
978 if ( rc != LDAP_SUCCESS ) {
980 op->o_tmpfree( _ca, op->o_tmpmemctx );
985 cav->cav_ptr = bv.bv_val;
986 assert( cav->cav_end >= bv.bv_val );
994 parse_comp_filter( Operation* op, ComponentAssertionValue* cav,
995 ComponentFilter** filt, const char** text )
998 * A component filter looks like this coming in:
999 * Filter ::= CHOICE {
1000 * item [0] ComponentAssertion,
1001 * and [1] SEQUENCE OF ComponentFilter,
1002 * or [2] SEQUENCE OF ComponentFilter,
1003 * not [3] ComponentFilter,
1010 /* TAG : item, and, or, not in RFC 2254 */
1011 tag = strip_cav_tag( cav );
1013 if ( tag == LBER_ERROR ) {
1014 *text = "error decoding comp filter";
1015 return LDAP_PROTOCOL_ERROR;
1018 if ( tag != LDAP_COMP_FILTER_NOT )
1019 strip_cav_str( cav, "{");
1026 switch ( f.cf_choice ) {
1027 case LDAP_COMP_FILTER_AND:
1028 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_AND\n", 0, 0, 0 );
1029 err = get_comp_filter_list( op, cav, &f.cf_and, text );
1030 if ( err != LDAP_SUCCESS ) {
1033 if ( f.cf_and == NULL ) {
1034 f.cf_choice = SLAPD_FILTER_COMPUTED;
1035 f.cf_result = LDAP_COMPARE_TRUE;
1039 case LDAP_COMP_FILTER_OR:
1040 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_OR\n", 0, 0, 0 );
1041 err = get_comp_filter_list( op, cav, &f.cf_or, text );
1042 if ( err != LDAP_SUCCESS ) {
1045 if ( f.cf_or == NULL ) {
1046 f.cf_choice = SLAPD_FILTER_COMPUTED;
1047 f.cf_result = LDAP_COMPARE_FALSE;
1049 /* no assert - list could be empty */
1052 case LDAP_COMP_FILTER_NOT:
1053 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_NOT\n", 0, 0, 0 );
1054 err = parse_comp_filter( op, cav, &f.cf_not, text );
1055 if ( err != LDAP_SUCCESS ) {
1059 assert( f.cf_not != NULL );
1060 if ( f.cf_not->cf_choice == SLAPD_FILTER_COMPUTED ) {
1061 int fresult = f.cf_not->cf_result;
1062 f.cf_choice = SLAPD_FILTER_COMPUTED;
1063 op->o_tmpfree( f.cf_not, op->o_tmpmemctx );
1066 switch ( fresult ) {
1067 case LDAP_COMPARE_TRUE:
1068 f.cf_result = LDAP_COMPARE_FALSE;
1070 case LDAP_COMPARE_FALSE:
1071 f.cf_result = LDAP_COMPARE_TRUE;
1074 /* (!Undefined) is Undefined */
1079 case LDAP_COMP_FILTER_ITEM:
1080 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_ITEM\n", 0, 0, 0 );
1081 err = get_item( op, cav, &f.cf_ca, text );
1082 if ( err != LDAP_SUCCESS ) {
1086 assert( f.cf_ca != NULL );
1090 f.cf_choice = SLAPD_FILTER_COMPUTED;
1091 f.cf_result = SLAPD_COMPARE_UNDEFINED;
1095 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1096 *text = "Component Filter Syntax Error";
1100 if ( tag != LDAP_COMP_FILTER_NOT )
1101 strip_cav_str( cav, "}");
1103 if ( err == LDAP_SUCCESS ) {
1105 *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx );
1107 *filt = malloc( sizeof(f) );
1116 test_comp_filter_and(
1118 ComponentSyntaxInfo *a,
1119 ComponentFilter *flist )
1122 int rtn = LDAP_COMPARE_TRUE;
1124 for ( f = flist ; f != NULL; f = f->cf_next ) {
1125 int rc = test_comp_filter( syn, a, f );
1126 if ( rc == LDAP_COMPARE_FALSE ) {
1131 if ( rc != LDAP_COMPARE_TRUE ) {
1140 test_comp_filter_or(
1142 ComponentSyntaxInfo *a,
1143 ComponentFilter *flist )
1146 int rtn = LDAP_COMPARE_TRUE;
1148 for ( f = flist ; f != NULL; f = f->cf_next ) {
1149 int rc = test_comp_filter( syn, a, f );
1150 if ( rc == LDAP_COMPARE_TRUE ) {
1155 if ( rc != LDAP_COMPARE_FALSE ) {
1164 csi_value_match( MatchingRule *mr, struct berval* bv_attr,
1165 struct berval* bv_assert )
1170 assert( mr != NULL );
1171 assert( !(mr->smr_usage & SLAP_MR_COMPONENT) );
1173 if( !mr->smr_match ) {
1174 return LDAP_INAPPROPRIATE_MATCHING;
1177 rc = (mr->smr_match)( &match, 0, NULL /*ad->ad_type->sat_syntax*/,
1178 mr, bv_attr, bv_assert );
1179 if ( rc == LDAP_SUCCESS )
1180 return match? LDAP_COMPARE_FALSE:LDAP_COMPARE_TRUE;
1186 * return codes : LDAP_COMPARE_TRUE, LDAP_COMPARE_FALSE
1189 test_comp_filter_item(
1191 ComponentSyntaxInfo *csi_attr,
1192 ComponentAssertion *ca )
1195 void *attr_nm, *assert_nm;
1197 if ( strcmp(ca->ca_ma_rule->smr_mrule.mr_oid,
1198 OID_COMP_FILTER_MATCH ) == 0 && ca->ca_cf ) {
1199 /* componentFilterMatch inside of componentFilterMatch */
1200 rc = test_comp_filter( syn, csi_attr, ca->ca_cf );
1204 /* Memory for storing will-be-extracted attribute values */
1205 attr_nm = nibble_mem_allocator ( 1024*4 , 1024 );
1206 if ( !attr_nm ) return LDAP_PROTOCOL_ERROR;
1208 /* Memory for storing component assertion values */
1209 if( !ca->ca_comp_data.cd_mem_op ) {
1210 assert_nm = nibble_mem_allocator ( 256, 64 );
1212 nibble_mem_free ( attr_nm );
1213 return LDAP_PROTOCOL_ERROR;
1215 ca->ca_comp_data.cd_mem_op = assert_nm;
1218 assert_nm = ca->ca_comp_data.cd_mem_op;
1221 /* component reference initialization */
1222 if ( ca->ca_comp_ref )
1223 ca->ca_comp_ref->cr_curr = ca->ca_comp_ref->cr_list;
1224 rc = test_components( attr_nm, assert_nm, csi_attr, ca );
1226 /* free memory used for storing extracted attribute value */
1227 nibble_mem_free ( attr_nm );
1234 ComponentSyntaxInfo *a,
1235 ComponentFilter *f )
1239 if ( !f ) return LDAP_PROTOCOL_ERROR;
1241 Debug( LDAP_DEBUG_FILTER, "test_comp_filter\n", 0, 0, 0 );
1242 switch ( f->cf_choice ) {
1243 case SLAPD_FILTER_COMPUTED:
1246 case LDAP_COMP_FILTER_AND:
1247 rc = test_comp_filter_and( syn, a, f->cf_and );
1249 case LDAP_COMP_FILTER_OR:
1250 rc = test_comp_filter_or( syn, a, f->cf_or );
1252 case LDAP_COMP_FILTER_NOT:
1253 rc = test_comp_filter( syn, a, f->cf_not );
1256 case LDAP_COMPARE_TRUE:
1257 rc = LDAP_COMPARE_FALSE;
1259 case LDAP_COMPARE_FALSE:
1260 rc = LDAP_COMPARE_TRUE;
1264 case LDAP_COMP_FILTER_ITEM:
1265 rc = test_comp_filter_item( syn, a, f->cf_ca );
1268 rc = LDAP_PROTOCOL_ERROR;
1275 free_comp_filter_list( ComponentFilter* f )
1277 ComponentFilter* tmp;
1278 for ( tmp = f; tmp; tmp = tmp->cf_next )
1280 free_comp_filter( tmp );
1285 free_comp_filter( ComponentFilter* f )
1288 Debug( LDAP_DEBUG_FILTER, "free_comp_filter:Invalid filter so failed to release memory\n", 0, 0, 0 );
1291 switch ( f->cf_choice ) {
1292 case LDAP_COMP_FILTER_AND:
1293 case LDAP_COMP_FILTER_OR:
1294 free_comp_filter_list( f->cf_any );
1296 case LDAP_COMP_FILTER_NOT:
1297 free_comp_filter( f->cf_any );
1299 case LDAP_COMP_FILTER_ITEM:
1300 if ( nibble_mem_free && f->cf_ca->ca_comp_data.cd_mem_op )
1301 nibble_mem_free( f->cf_ca->ca_comp_data.cd_mem_op );
1310 component_free( ComponentFilter *f ) {
1311 free_comp_filter( f );