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 ) return LDAP_INAPPROPRIATE_MATCHING;
106 /* Check if the component module is loaded */
107 if ( !attr_converter || !nibble_mem_allocator ) {
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 registration */
146 struct berval *value,
147 void *assertedValue )
149 /* Only for registration */
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 != NULL ;
183 ci_curr = ci_curr->ci_next, ci_temp = &(*ci_temp)->ci_next )
185 *ci_temp = op->o_tmpalloc( sizeof( ComponentId ), op->o_tmpmemctx );
186 if ( !ci_temp ) return NULL;
187 **ci_temp = *ci_curr;
190 dup_cr->cr_curr = dup_cr->cr_list;
196 dup_comp_filter_list (
199 ComponentFilter* in_f,
200 ComponentFilter** out_f )
202 ComponentFilter **new, *f;
206 for ( f = in_f; f != NULL; f = f->cf_next ) {
207 rc = dup_comp_filter( op, bv, f, new );
208 if ( rc != LDAP_SUCCESS ) {
211 new = &(*new)->cf_next;
217 get_len_of_next_assert_value ( struct berval* bv, char separator )
221 if ( (bv->bv_val[ i ] == separator) || ( i >= bv->bv_len) )
225 bv->bv_val += (i + 1);
226 bv->bv_len -= (i + 1);
231 dup_comp_filter_item (
233 struct berval* assert_bv,
234 ComponentAssertion* in_ca,
235 ComponentAssertion** out_ca )
237 ComponentAssertion* ca;
240 if ( !in_ca->ca_comp_ref ) return SLAPD_DISCONNECT;
242 *out_ca = op->o_tmpalloc( sizeof( ComponentAssertion ), op->o_tmpmemctx );
243 if ( !(*out_ca) ) 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, '$' );
254 if ( len <= 0 ) return SLAPD_DISCONNECT;
255 (*out_ca)->ca_ma_value.bv_len = len;
264 ComponentFilter *in_f,
265 ComponentFilter **out_f )
268 ComponentFilter dup_f = {0};
270 if ( !in_f ) return LDAP_PROTOCOL_ERROR;
272 switch ( in_f->cf_choice ) {
273 case LDAP_COMP_FILTER_AND:
274 rc = dup_comp_filter_list( op, bv, in_f->cf_and, &dup_f.cf_and);
275 dup_f.cf_choice = LDAP_COMP_FILTER_AND;
277 case LDAP_COMP_FILTER_OR:
278 rc = dup_comp_filter_list( op, bv, in_f->cf_or, &dup_f.cf_or);
279 dup_f.cf_choice = LDAP_COMP_FILTER_OR;
281 case LDAP_COMP_FILTER_NOT:
282 rc = dup_comp_filter( op, bv, in_f->cf_not, &dup_f.cf_not);
283 dup_f.cf_choice = LDAP_COMP_FILTER_NOT;
285 case LDAP_COMP_FILTER_ITEM:
286 rc = dup_comp_filter_item( op, bv, in_f->cf_ca ,&dup_f.cf_ca );
287 dup_f.cf_choice = LDAP_COMP_FILTER_ITEM;
290 rc = LDAP_PROTOCOL_ERROR;
293 if ( rc == LDAP_SUCCESS ) {
294 *out_f = op->o_tmpalloc( sizeof(dup_f), op->o_tmpmemctx );
303 get_aliased_filter( Operation* op,
304 MatchingRuleAssertion* ma, AttributeAliasing* aa,
308 struct berval assert_bv;
309 ComponentAssertion* ca;
311 Debug( LDAP_DEBUG_FILTER, "get_aliased_filter\n", 0, 0, 0 );
313 if ( !aa->aa_cf ) return LDAP_PROTOCOL_ERROR;
315 assert_bv = ma->ma_value;
316 /* Attribute Description is replaced with aliased one */
317 ma->ma_desc = aa->aa_aliased_ad;
318 ma->ma_rule = aa->aa_mr;
320 * Duplicate aa->aa_cf to ma->ma_cf by replacing the
321 * the component assertion value in assert_bv
322 * Multiple values may be separated with '$'
324 return dup_comp_filter ( op, &assert_bv, aa->aa_cf, &ma->ma_cf );
328 get_comp_filter( Operation* op, struct berval* bv,
329 ComponentFilter** filt, const char **text )
331 ComponentAssertionValue cav;
334 Debug( LDAP_DEBUG_FILTER, "get_comp_filter\n", 0, 0, 0 );
335 if ( (rc = slapd_ber2cav(bv, &cav) ) != LDAP_SUCCESS ) {
338 rc = parse_comp_filter( op, &cav, filt, text );
339 bv->bv_val = cav.cav_ptr;
345 eat_whsp( ComponentAssertionValue* cav )
347 for ( ; ( *cav->cav_ptr == ' ' ) && ( cav->cav_ptr < cav->cav_end ) ; ) {
353 cav_cur_len( ComponentAssertionValue* cav )
355 return cav->cav_end - cav->cav_ptr;
359 comp_first_element( ComponentAssertionValue* cav )
362 if ( cav_cur_len( cav ) >= 8 && strncmp( cav->cav_ptr, "item", 4 ) == 0 ) {
363 return LDAP_COMP_FILTER_ITEM;
365 } else if ( cav_cur_len( cav ) >= 7 &&
366 strncmp( cav->cav_ptr, "and", 3 ) == 0 )
368 return LDAP_COMP_FILTER_AND;
370 } else if ( cav_cur_len( cav ) >= 6 &&
371 strncmp( cav->cav_ptr, "or" , 2 ) == 0 )
373 return LDAP_COMP_FILTER_OR;
375 } else if ( cav_cur_len( cav ) >= 7 &&
376 strncmp( cav->cav_ptr, "not", 3 ) == 0 )
378 return LDAP_COMP_FILTER_NOT;
381 return LDAP_COMP_FILTER_UNDEFINED;
386 comp_next_element( ComponentAssertionValue* cav )
389 if ( *(cav->cav_ptr) == ',' ) {
390 /* move pointer to the next CA */
392 return comp_first_element( cav );
394 else return LDAP_COMP_FILTER_UNDEFINED;
398 get_comp_filter_list( Operation *op, ComponentAssertionValue *cav,
399 ComponentFilter** f, const char** text )
401 ComponentFilter **new;
405 Debug( LDAP_DEBUG_FILTER, "get_comp_filter_list\n", 0, 0, 0 );
407 for ( tag = comp_first_element( cav );
408 tag != LDAP_COMP_FILTER_UNDEFINED;
409 tag = comp_next_element( cav ) )
411 err = parse_comp_filter( op, cav, new, text );
412 if ( err != LDAP_SUCCESS ) return ( err );
413 new = &(*new)->cf_next;
417 return( LDAP_SUCCESS );
421 get_componentId( Operation *op, ComponentAssertionValue* cav,
422 ComponentId ** cid, const char** text )
428 type = peek_componentId_type( cav );
430 Debug( LDAP_DEBUG_FILTER, "get_compId [%d]\n", type, 0, 0 );
435 case LDAP_COMPREF_IDENTIFIER :
436 _cid.ci_val.ci_identifier.bv_val = cav->cav_ptr;
437 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
438 cav->cav_ptr[len] != '.' && cav->cav_ptr[len] != '\"' ; len++ );
439 _cid.ci_val.ci_identifier.bv_len = len;
442 case LDAP_COMPREF_FROM_BEGINNING :
443 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
444 cav->cav_ptr[len] != '.' && cav->cav_ptr[len] != '\"' ; len++ );
445 _cid.ci_val.ci_from_beginning = strtol( cav->cav_ptr, NULL, 0 );
448 case LDAP_COMPREF_FROM_END :
449 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
450 cav->cav_ptr[len] != '.' && cav->cav_ptr[len] != '\"' ; len++ );
451 _cid.ci_val.ci_from_end = strtol( cav->cav_ptr, NULL, 0 );
454 case LDAP_COMPREF_COUNT :
455 _cid.ci_val.ci_count = 0;
458 case LDAP_COMPREF_CONTENT :
459 _cid.ci_val.ci_content = 1;
460 cav->cav_ptr += strlen("content");
462 case LDAP_COMPREF_SELECT :
463 if ( cav->cav_ptr[len] != '(' ) return LDAP_COMPREF_UNDEFINED;
464 for( ;cav->cav_ptr[len] != ' ' && cav->cav_ptr[len] != '\0' &&
465 cav->cav_ptr[len] != '\"' && cav->cav_ptr[len] != ')'
467 _cid.ci_val.ci_select_value.bv_val = cav->cav_ptr + 1;
468 _cid.ci_val.ci_select_value.bv_len = len - 1 ;
469 cav->cav_ptr += len + 1;
471 case LDAP_COMPREF_ALL :
472 _cid.ci_val.ci_all = '*';
476 return LDAP_COMPREF_UNDEFINED;
480 *cid = op->o_tmpalloc( sizeof( ComponentId ), op->o_tmpmemctx );
482 *cid = malloc( sizeof( ComponentId ) );
489 peek_componentId_type( ComponentAssertionValue* cav )
493 if ( cav->cav_ptr[0] == '-' ) {
494 return LDAP_COMPREF_FROM_END;
496 } else if ( cav->cav_ptr[0] == '(' ) {
497 return LDAP_COMPREF_SELECT;
499 } else if ( cav->cav_ptr[0] == '*' ) {
500 return LDAP_COMPREF_ALL;
502 } else if ( cav->cav_ptr[0] == '0' ) {
503 return LDAP_COMPREF_COUNT;
505 } else if ( cav->cav_ptr[0] > '0' && cav->cav_ptr[0] <= '9' ) {
506 return LDAP_COMPREF_FROM_BEGINNING;
508 } else if ( (cav->cav_end - cav->cav_ptr) >= 7 &&
509 strncmp(cav->cav_ptr,"content",7) == 0 )
511 return LDAP_COMPREF_CONTENT;
512 } else if ( (cav->cav_ptr[0] >= 'a' && cav->cav_ptr[0] <= 'z') ||
513 (cav->cav_ptr[0] >= 'A' && cav->cav_ptr[0] <= 'Z') )
515 return LDAP_COMPREF_IDENTIFIER;
518 return LDAP_COMPREF_UNDEFINED;
522 comp_next_id( ComponentAssertionValue* cav )
524 if ( *(cav->cav_ptr) == '.' ) {
526 return LDAP_COMPREF_DEFINED;
529 return LDAP_COMPREF_UNDEFINED;
535 get_component_reference(
537 ComponentAssertionValue* cav,
538 ComponentReference** cr,
543 ComponentReference* ca_comp_ref;
544 ComponentId** cr_list;
549 start = cav->cav_ptr;
550 if ( ( rc = strip_cav_str( cav,"\"") ) != LDAP_SUCCESS ) return rc;
552 ca_comp_ref = op->o_tmpalloc( sizeof( ComponentReference ),
555 ca_comp_ref = malloc( sizeof( ComponentReference ) );
558 if ( !ca_comp_ref ) return LDAP_NO_MEMORY;
560 cr_list = &ca_comp_ref->cr_list;
562 for ( type = peek_componentId_type( cav ) ; type != LDAP_COMPREF_UNDEFINED
563 ; type = comp_next_id( cav ), count++ )
565 rc = get_componentId( op, cav, cr_list, text );
566 if ( rc == LDAP_SUCCESS ) {
567 if ( count == 0 ) ca_comp_ref->cr_curr = ca_comp_ref->cr_list;
568 cr_list = &(*cr_list)->ci_next;
570 } else if ( rc == LDAP_COMPREF_UNDEFINED ) {
574 ca_comp_ref->cr_len = count;
576 if ( ( rc = strip_cav_str( cav,"\"") ) != LDAP_SUCCESS ) {
578 op->o_tmpfree( ca_comp_ref , op->o_tmpmemctx );
585 if ( rc == LDAP_SUCCESS ) {
590 op->o_tmpfree( ca_comp_ref , op->o_tmpmemctx );
593 free( ca_comp_ref ) ;
596 (*cr)->cr_string.bv_val = start;
597 (*cr)->cr_string.bv_len = end - start + 1;
603 insert_component_reference(
604 ComponentReference *cr,
605 ComponentReference** cr_list)
607 if ( !cr ) return LDAP_PARAM_ERROR;
613 cr->cr_next = *cr_list;
620 * If there is '.' in the name of a given attribute
621 * the first '.'- following characters are considered
622 * as a component reference of the attribute
623 * EX) userCertificate.toBeSigned.serialNumber
624 * attribute : userCertificate
625 * component reference : toBeSigned.serialNumber
628 is_component_reference( char* attr ) {
630 for ( i=0; attr[i] != '\0' ; i++ ) {
631 if ( attr[i] == '.' ) return (1);
637 extract_component_reference(
639 ComponentReference** cr )
644 ComponentAssertionValue cav;
647 for ( i=0; attr[i] != '\0' ; i++ ) {
648 if ( attr[i] == '.' ) break;
651 if (attr[i] != '.' ) return LDAP_PARAM_ERROR;
654 cr_ptr = attr + i + 1 ;
655 cr_len = strlen ( cr_ptr );
656 if ( cr_len <= 0 ) return LDAP_PARAM_ERROR;
658 /* enclosed between double quotes*/
659 cav.cav_ptr = cav.cav_buf = ch_malloc (cr_len+2);
660 memcpy( cav.cav_buf+1, cr_ptr, cr_len );
661 cav.cav_buf[0] = '"';
662 cav.cav_buf[cr_len+1] = '"';
663 cav.cav_end = cr_ptr + cr_len + 2;
665 rc = get_component_reference ( NULL, &cav, cr, (const char**)text );
666 if ( rc != LDAP_SUCCESS ) return rc;
667 (*cr)->cr_string.bv_val = cav.cav_buf;
668 (*cr)->cr_string.bv_len = cr_len + 2;
674 get_ca_use_default( Operation *op,
675 ComponentAssertionValue* cav,
676 int* ca_use_def, const char** text )
678 strip_cav_str( cav, "useDefaultValues" );
680 if ( peek_cav_str( cav, "TRUE" ) == LDAP_SUCCESS ) {
681 strip_cav_str( cav, "TRUE" );
684 } else if ( peek_cav_str( cav, "FALSE" ) == LDAP_SUCCESS ) {
685 strip_cav_str( cav, "FALSE" );
689 return LDAP_INVALID_SYNTAX;
696 get_matching_rule( Operation *op, ComponentAssertionValue* cav,
697 MatchingRule** mr, const char** text )
700 struct berval rule_text = { 0L, NULL };
704 for ( ; ; count++ ) {
705 if ( cav->cav_ptr[count] == ' ' || cav->cav_ptr[count] == ',' ||
706 cav->cav_ptr[count] == '\0' || cav->cav_ptr[count] == '{' ||
707 cav->cav_ptr[count] == '}' || cav->cav_ptr[count] == '\n' )
714 *text = "component matching rule not recognized";
715 return LDAP_INAPPROPRIATE_MATCHING;
718 rule_text.bv_len = count;
719 rule_text.bv_val = cav->cav_ptr;
720 *mr = mr_bvfind( &rule_text );
721 cav->cav_ptr += count;
722 Debug( LDAP_DEBUG_FILTER, "get_matching_rule: %s\n",
723 (*mr)->smr_mrule.mr_oid, 0, 0 );
725 *text = "component matching rule not recognized";
726 return LDAP_INAPPROPRIATE_MATCHING;
732 get_GSER_value( ComponentAssertionValue* cav, struct berval* bv )
734 int count, sequent_dquote, unclosed_brace, succeed;
738 * Four cases of GSER <Values>
740 * StringVal, GeneralizedTimeVal, UTCTimeVal, ObjectDescriptorVal
741 * 2) '...'B or '...'H :
742 * BitStringVal, OctetStringVal
744 * SEQUENCE, SEQUENCEOF, SETOF, SET, CHOICE
745 * 4) Between two white spaces
746 * INTEGER, BOOLEAN, NULL,ENUMERATE, etc
750 if ( cav->cav_ptr[0] == '"' ) {
751 for( count = 1, sequent_dquote = 0 ; ; count++ ) {
752 /* In order to find escaped double quote */
753 if ( cav->cav_ptr[count] == '"' ) sequent_dquote++;
754 else sequent_dquote = 0;
756 if ( cav->cav_ptr[count] == '\0' ||
757 (cav->cav_ptr+count) > cav->cav_end )
762 if ( ( cav->cav_ptr[count] == '"' &&
763 cav->cav_ptr[count-1] != '"') ||
764 ( sequent_dquote > 2 && (sequent_dquote%2) == 1 ) )
771 if ( !succeed || cav->cav_ptr[count] != '"' ) {
772 return LDAP_FILTER_ERROR;
775 bv->bv_val = cav->cav_ptr + 1;
776 bv->bv_len = count - 1; /* exclude '"' */
778 } else if ( cav->cav_ptr[0] == '\'' ) {
779 for( count = 1 ; ; count++ ) {
780 if ( cav->cav_ptr[count] == '\0' ||
781 (cav->cav_ptr+count) > cav->cav_end )
785 if ((cav->cav_ptr[count-1] == '\'' && cav->cav_ptr[count] == 'B') ||
786 (cav->cav_ptr[count-1] == '\'' && cav->cav_ptr[count] == 'H') )
794 !(cav->cav_ptr[count] == 'H' || cav->cav_ptr[count] == 'B') )
796 return LDAP_FILTER_ERROR;
799 bv->bv_val = cav->cav_ptr + 1;/*the next to '"' */
800 bv->bv_len = count - 2;/* exclude "'H" or "'B" */
802 } else if ( cav->cav_ptr[0] == '{' ) {
803 for( count = 1, unclosed_brace = 1 ; ; count++ ) {
804 if ( cav->cav_ptr[count] == '{' ) unclosed_brace++;
805 if ( cav->cav_ptr[count] == '}' ) unclosed_brace--;
807 if ( cav->cav_ptr[count] == '\0' ||
808 (cav->cav_ptr+count) > cav->cav_end )
812 if ( unclosed_brace == 0 ) {
818 if ( !succeed || cav->cav_ptr[count] != '}' ) return LDAP_FILTER_ERROR;
820 bv->bv_val = cav->cav_ptr + 1;/*the next to '"' */
821 bv->bv_len = count - 1;/* exclude "'B" */
825 /*Find following white space where the value is ended*/
826 for( count = 1 ; ; count++ ) {
827 if ( cav->cav_ptr[count] == '\0' ||
828 cav->cav_ptr[count] == ' ' || cav->cav_ptr[count] == '}' ||
829 cav->cav_ptr[count] == '{' ||
830 (cav->cav_ptr+count) > cav->cav_end )
835 bv->bv_val = cav->cav_ptr;
839 cav->cav_ptr += bv->bv_len;
844 get_matching_value( Operation *op, ComponentAssertion* ca,
845 ComponentAssertionValue* cav, struct berval* bv,
848 if ( !(ca->ca_ma_rule->smr_usage & (SLAP_MR_COMPONENT)) ) {
849 if ( get_GSER_value( cav, bv ) != LDAP_SUCCESS ) {
850 return LDAP_FILTER_ERROR;
854 /* embeded componentFilterMatch Description */
855 bv->bv_val = cav->cav_ptr;
856 bv->bv_len = cav_cur_len( cav );
862 /* Don't move the position pointer, just peek given string */
864 peek_cav_str( ComponentAssertionValue* cav, char* str )
867 if ( cav_cur_len( cav ) >= strlen( str ) &&
868 strncmp( cav->cav_ptr, str, strlen( str ) ) == 0 )
873 return LDAP_INVALID_SYNTAX;
877 strip_cav_str( ComponentAssertionValue* cav, char* str)
880 if ( cav_cur_len( cav ) >= strlen( str ) &&
881 strncmp( cav->cav_ptr, str, strlen( str ) ) == 0 )
883 cav->cav_ptr += strlen( str );
887 return LDAP_INVALID_SYNTAX;
891 * TAG : "item", "and", "or", "not"
894 strip_cav_tag( ComponentAssertionValue* cav )
898 if ( cav_cur_len( cav ) >= 8 && strncmp( cav->cav_ptr, "item", 4 ) == 0 ) {
899 strip_cav_str( cav , "item:" );
900 return LDAP_COMP_FILTER_ITEM;
902 } else if ( cav_cur_len( cav ) >= 7 &&
903 strncmp( cav->cav_ptr, "and", 3 ) == 0 )
905 strip_cav_str( cav , "and:" );
906 return LDAP_COMP_FILTER_AND;
908 } else if ( cav_cur_len( cav ) >= 6 &&
909 strncmp( cav->cav_ptr, "or" , 2 ) == 0 )
911 strip_cav_str( cav , "or:" );
912 return LDAP_COMP_FILTER_OR;
914 } else if ( cav_cur_len( cav ) >= 7 &&
915 strncmp( cav->cav_ptr, "not", 3 ) == 0 )
917 strip_cav_str( cav , "not:" );
918 return LDAP_COMP_FILTER_NOT;
925 * when encoding, "item" is denotation of ComponentAssertion
926 * ComponentAssertion :: SEQUENCE {
927 * component ComponentReference (SIZE(1..MAX)) OPTIONAL,
928 * useDefaultValues BOOLEAN DEFAULT TRUE,
929 * rule MATCHING-RULE.&id,
930 * value MATCHING-RULE.&AssertionType }
933 get_item( Operation *op, ComponentAssertionValue* cav, ComponentAssertion** ca,
937 ComponentAssertion* _ca;
942 Debug( LDAP_DEBUG_FILTER, "get_item \n", 0, 0, 0 );
944 _ca = op->o_tmpalloc( sizeof( ComponentAssertion ), op->o_tmpmemctx );
946 _ca = malloc( sizeof( ComponentAssertion ) );
948 if ( !_ca ) return LDAP_NO_MEMORY;
950 _ca->ca_comp_data.cd_tree = NULL;
951 _ca->ca_comp_data.cd_mem_op = NULL;
953 rc = peek_cav_str( cav, "component" );
954 if ( rc == LDAP_SUCCESS ) {
955 strip_cav_str( cav, "component" );
956 rc = get_component_reference( op, cav, &_ca->ca_comp_ref, text );
957 if ( rc != LDAP_SUCCESS ) {
959 op->o_tmpfree( _ca, op->o_tmpmemctx );
962 return LDAP_INVALID_SYNTAX;
964 if ( ( rc = strip_cav_str( cav,",") ) != LDAP_SUCCESS )
967 _ca->ca_comp_ref = NULL;
970 rc = peek_cav_str( cav, "useDefaultValues");
971 if ( rc == LDAP_SUCCESS ) {
972 rc = get_ca_use_default( op, cav, &_ca->ca_use_def, text );
973 if ( rc != LDAP_SUCCESS ) {
975 op->o_tmpfree( _ca, op->o_tmpmemctx );
978 return LDAP_INVALID_SYNTAX;
980 if ( ( rc = strip_cav_str( cav,",") ) != LDAP_SUCCESS )
983 else _ca->ca_use_def = 1;
985 if ( !( strip_cav_str( cav, "rule" ) == LDAP_SUCCESS &&
986 get_matching_rule( op, cav , &_ca->ca_ma_rule, text ) == LDAP_SUCCESS )) {
988 op->o_tmpfree( _ca, op->o_tmpmemctx );
991 return LDAP_INAPPROPRIATE_MATCHING;
994 if ( ( rc = strip_cav_str( cav,",") ) != LDAP_SUCCESS )
996 if ( !(strip_cav_str( cav, "value" ) == LDAP_SUCCESS &&
997 get_matching_value( op, _ca, cav,&value ,text ) == LDAP_SUCCESS )) {
999 op->o_tmpfree( _ca, op->o_tmpmemctx );
1002 return LDAP_INVALID_SYNTAX;
1006 * Normalize the value of this component assertion when the matching
1007 * rule is one of existing matching rules
1009 mr = _ca->ca_ma_rule;
1010 if ( op && !(mr->smr_usage & (SLAP_MR_COMPONENT)) && mr->smr_normalize ) {
1012 value.bv_val[value.bv_len] = '\0';
1013 rc = mr->smr_normalize (
1014 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
1016 &value, &_ca->ca_ma_value, op->o_tmpmemctx );
1017 if ( rc != LDAP_SUCCESS )
1021 _ca->ca_ma_value = value;
1023 * Validate the value of this component assertion
1025 if ( op && mr->smr_syntax->ssyn_validate( mr->smr_syntax, &_ca->ca_ma_value) != LDAP_SUCCESS ) {
1026 return LDAP_INVALID_SYNTAX;
1030 /* componentFilterMatch contains componentFilterMatch in it */
1031 if ( strcmp(_ca->ca_ma_rule->smr_mrule.mr_oid, OID_COMP_FILTER_MATCH ) == 0) {
1033 bv.bv_val = cav->cav_ptr;
1034 bv.bv_len = cav_cur_len( cav );
1035 rc = get_comp_filter( op, &bv,(ComponentFilter**)&_ca->ca_cf, text );
1036 if ( rc != LDAP_SUCCESS ) {
1038 op->o_tmpfree( _ca, op->o_tmpmemctx );
1043 cav->cav_ptr = bv.bv_val;
1044 assert( cav->cav_end >= bv.bv_val );
1048 return LDAP_SUCCESS;
1052 parse_comp_filter( Operation* op, ComponentAssertionValue* cav,
1053 ComponentFilter** filt, const char** text )
1056 * A component filter looks like this coming in:
1057 * Filter ::= CHOICE {
1058 * item [0] ComponentAssertion,
1059 * and [1] SEQUENCE OF ComponentFilter,
1060 * or [2] SEQUENCE OF ComponentFilter,
1061 * not [3] ComponentFilter,
1068 /* TAG : item, and, or, not in RFC 2254 */
1069 tag = strip_cav_tag( cav );
1071 if ( tag == LBER_ERROR ) {
1072 *text = "error decoding comp filter";
1073 return LDAP_PROTOCOL_ERROR;
1076 if ( tag != LDAP_COMP_FILTER_NOT )
1077 strip_cav_str( cav, "{");
1084 switch ( f.cf_choice ) {
1085 case LDAP_COMP_FILTER_AND:
1086 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_AND\n", 0, 0, 0 );
1087 err = get_comp_filter_list( op, cav, &f.cf_and, text );
1088 if ( err != LDAP_SUCCESS ) {
1091 if ( f.cf_and == NULL ) {
1092 f.cf_choice = SLAPD_FILTER_COMPUTED;
1093 f.cf_result = LDAP_COMPARE_TRUE;
1097 case LDAP_COMP_FILTER_OR:
1098 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_OR\n", 0, 0, 0 );
1099 err = get_comp_filter_list( op, cav, &f.cf_or, text );
1100 if ( err != LDAP_SUCCESS ) {
1103 if ( f.cf_or == NULL ) {
1104 f.cf_choice = SLAPD_FILTER_COMPUTED;
1105 f.cf_result = LDAP_COMPARE_FALSE;
1107 /* no assert - list could be empty */
1110 case LDAP_COMP_FILTER_NOT:
1111 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_NOT\n", 0, 0, 0 );
1112 err = parse_comp_filter( op, cav, &f.cf_not, text );
1113 if ( err != LDAP_SUCCESS ) {
1117 assert( f.cf_not != NULL );
1118 if ( f.cf_not->cf_choice == SLAPD_FILTER_COMPUTED ) {
1119 int fresult = f.cf_not->cf_result;
1120 f.cf_choice = SLAPD_FILTER_COMPUTED;
1121 op->o_tmpfree( f.cf_not, op->o_tmpmemctx );
1124 switch ( fresult ) {
1125 case LDAP_COMPARE_TRUE:
1126 f.cf_result = LDAP_COMPARE_FALSE;
1128 case LDAP_COMPARE_FALSE:
1129 f.cf_result = LDAP_COMPARE_TRUE;
1132 /* (!Undefined) is Undefined */
1137 case LDAP_COMP_FILTER_ITEM:
1138 Debug( LDAP_DEBUG_FILTER, "LDAP_COMP_FILTER_ITEM\n", 0, 0, 0 );
1139 err = get_item( op, cav, &f.cf_ca, text );
1140 if ( err != LDAP_SUCCESS ) {
1144 assert( f.cf_ca != NULL );
1148 f.cf_choice = SLAPD_FILTER_COMPUTED;
1149 f.cf_result = SLAPD_COMPARE_UNDEFINED;
1153 if ( err != LDAP_SUCCESS && err != SLAPD_DISCONNECT ) {
1154 *text = "Component Filter Syntax Error";
1158 if ( tag != LDAP_COMP_FILTER_NOT )
1159 strip_cav_str( cav, "}");
1161 if ( err == LDAP_SUCCESS ) {
1163 *filt = op->o_tmpalloc( sizeof(f), op->o_tmpmemctx );
1165 *filt = malloc( sizeof(f) );
1174 test_comp_filter_and(
1176 ComponentSyntaxInfo *a,
1177 ComponentFilter *flist )
1180 int rtn = LDAP_COMPARE_TRUE;
1182 for ( f = flist ; f != NULL; f = f->cf_next ) {
1183 int rc = test_comp_filter( syn, a, f );
1184 if ( rc == LDAP_COMPARE_FALSE ) {
1189 if ( rc != LDAP_COMPARE_TRUE ) {
1198 test_comp_filter_or(
1200 ComponentSyntaxInfo *a,
1201 ComponentFilter *flist )
1204 int rtn = LDAP_COMPARE_TRUE;
1206 for ( f = flist ; f != NULL; f = f->cf_next ) {
1207 int rc = test_comp_filter( syn, a, f );
1208 if ( rc == LDAP_COMPARE_TRUE ) {
1213 if ( rc != LDAP_COMPARE_FALSE ) {
1222 csi_value_match( MatchingRule *mr, struct berval* bv_attr,
1223 struct berval* bv_assert )
1228 assert( mr != NULL );
1229 assert( !(mr->smr_usage & SLAP_MR_COMPONENT) );
1231 if( !mr->smr_match ) return LDAP_INAPPROPRIATE_MATCHING;
1233 rc = (mr->smr_match)( &match, 0, NULL /*ad->ad_type->sat_syntax*/,
1234 mr, bv_attr, bv_assert );
1236 if ( rc != LDAP_SUCCESS ) return rc;
1238 return match ? LDAP_COMPARE_FALSE : LDAP_COMPARE_TRUE;
1242 * return codes : LDAP_COMPARE_TRUE, LDAP_COMPARE_FALSE
1245 test_comp_filter_item(
1247 ComponentSyntaxInfo *csi_attr,
1248 ComponentAssertion *ca )
1251 void *attr_nm, *assert_nm;
1253 if ( strcmp(ca->ca_ma_rule->smr_mrule.mr_oid,
1254 OID_COMP_FILTER_MATCH ) == 0 && ca->ca_cf ) {
1255 /* componentFilterMatch inside of componentFilterMatch */
1256 rc = test_comp_filter( syn, csi_attr, ca->ca_cf );
1260 /* Memory for storing will-be-extracted attribute values */
1261 attr_nm = nibble_mem_allocator ( 1024*4 , 1024 );
1262 if ( !attr_nm ) return LDAP_PROTOCOL_ERROR;
1264 /* Memory for storing component assertion values */
1265 if( !ca->ca_comp_data.cd_mem_op ) {
1266 assert_nm = nibble_mem_allocator ( 256, 64 );
1268 nibble_mem_free ( attr_nm );
1269 return LDAP_PROTOCOL_ERROR;
1271 ca->ca_comp_data.cd_mem_op = assert_nm;
1274 assert_nm = ca->ca_comp_data.cd_mem_op;
1277 /* component reference initialization */
1278 if ( ca->ca_comp_ref ) {
1279 ca->ca_comp_ref->cr_curr = ca->ca_comp_ref->cr_list;
1281 rc = test_components( attr_nm, assert_nm, csi_attr, ca );
1283 /* free memory used for storing extracted attribute value */
1284 nibble_mem_free ( attr_nm );
1291 ComponentSyntaxInfo *a,
1292 ComponentFilter *f )
1296 if ( !f ) return LDAP_PROTOCOL_ERROR;
1298 Debug( LDAP_DEBUG_FILTER, "test_comp_filter\n", 0, 0, 0 );
1299 switch ( f->cf_choice ) {
1300 case SLAPD_FILTER_COMPUTED:
1303 case LDAP_COMP_FILTER_AND:
1304 rc = test_comp_filter_and( syn, a, f->cf_and );
1306 case LDAP_COMP_FILTER_OR:
1307 rc = test_comp_filter_or( syn, a, f->cf_or );
1309 case LDAP_COMP_FILTER_NOT:
1310 rc = test_comp_filter( syn, a, f->cf_not );
1313 case LDAP_COMPARE_TRUE:
1314 rc = LDAP_COMPARE_FALSE;
1316 case LDAP_COMPARE_FALSE:
1317 rc = LDAP_COMPARE_TRUE;
1321 case LDAP_COMP_FILTER_ITEM:
1322 rc = test_comp_filter_item( syn, a, f->cf_ca );
1325 rc = LDAP_PROTOCOL_ERROR;
1332 free_comp_filter_list( ComponentFilter* f )
1334 ComponentFilter* tmp;
1335 for ( tmp = f; tmp; tmp = tmp->cf_next ) {
1336 free_comp_filter( tmp );
1341 free_comp_filter( ComponentFilter* f )
1344 Debug( LDAP_DEBUG_FILTER,
1345 "free_comp_filter: Invalid filter so failed to release memory\n",
1349 switch ( f->cf_choice ) {
1350 case LDAP_COMP_FILTER_AND:
1351 case LDAP_COMP_FILTER_OR:
1352 free_comp_filter_list( f->cf_any );
1354 case LDAP_COMP_FILTER_NOT:
1355 free_comp_filter( f->cf_any );
1357 case LDAP_COMP_FILTER_ITEM:
1358 if ( nibble_mem_free && f->cf_ca->ca_comp_data.cd_mem_op ) {
1359 nibble_mem_free( f->cf_ca->ca_comp_data.cd_mem_op );
1368 component_free( ComponentFilter *f ) {
1369 free_comp_filter( f );