Home | History | Annotate | Line # | Download | only in slapd
mr.c revision 1.1
      1  1.1  lukem /* mr.c - routines to manage matching rule definitions */
      2  1.1  lukem /* $OpenLDAP: pkg/ldap/servers/slapd/mr.c,v 1.64.2.3 2008/02/11 23:26:44 kurt Exp $ */
      3  1.1  lukem /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      4  1.1  lukem  *
      5  1.1  lukem  * Copyright 1998-2008 The OpenLDAP Foundation.
      6  1.1  lukem  * All rights reserved.
      7  1.1  lukem  *
      8  1.1  lukem  * Redistribution and use in source and binary forms, with or without
      9  1.1  lukem  * modification, are permitted only as authorized by the OpenLDAP
     10  1.1  lukem  * Public License.
     11  1.1  lukem  *
     12  1.1  lukem  * A copy of this license is available in the file LICENSE in the
     13  1.1  lukem  * top-level directory of the distribution or, alternatively, at
     14  1.1  lukem  * <http://www.OpenLDAP.org/license.html>.
     15  1.1  lukem  */
     16  1.1  lukem 
     17  1.1  lukem #include "portable.h"
     18  1.1  lukem 
     19  1.1  lukem #include <stdio.h>
     20  1.1  lukem 
     21  1.1  lukem #include <ac/ctype.h>
     22  1.1  lukem #include <ac/string.h>
     23  1.1  lukem #include <ac/socket.h>
     24  1.1  lukem 
     25  1.1  lukem #include "slap.h"
     26  1.1  lukem 
     27  1.1  lukem struct mindexrec {
     28  1.1  lukem 	struct berval	mir_name;
     29  1.1  lukem 	MatchingRule	*mir_mr;
     30  1.1  lukem };
     31  1.1  lukem 
     32  1.1  lukem static Avlnode	*mr_index = NULL;
     33  1.1  lukem static LDAP_SLIST_HEAD(MRList, MatchingRule) mr_list
     34  1.1  lukem 	= LDAP_SLIST_HEAD_INITIALIZER(&mr_list);
     35  1.1  lukem static LDAP_SLIST_HEAD(MRUList, MatchingRuleUse) mru_list
     36  1.1  lukem 	= LDAP_SLIST_HEAD_INITIALIZER(&mru_list);
     37  1.1  lukem 
     38  1.1  lukem static int
     39  1.1  lukem mr_index_cmp(
     40  1.1  lukem     const void	*v_mir1,
     41  1.1  lukem     const void	*v_mir2
     42  1.1  lukem )
     43  1.1  lukem {
     44  1.1  lukem 	const struct mindexrec	*mir1 = v_mir1;
     45  1.1  lukem 	const struct mindexrec	*mir2 = v_mir2;
     46  1.1  lukem 	int i = mir1->mir_name.bv_len - mir2->mir_name.bv_len;
     47  1.1  lukem 	if (i) return i;
     48  1.1  lukem 	return (strcasecmp( mir1->mir_name.bv_val, mir2->mir_name.bv_val ));
     49  1.1  lukem }
     50  1.1  lukem 
     51  1.1  lukem static int
     52  1.1  lukem mr_index_name_cmp(
     53  1.1  lukem     const void	*v_name,
     54  1.1  lukem     const void	*v_mir
     55  1.1  lukem )
     56  1.1  lukem {
     57  1.1  lukem 	const struct berval    *name = v_name;
     58  1.1  lukem 	const struct mindexrec *mir  = v_mir;
     59  1.1  lukem 	int i = name->bv_len - mir->mir_name.bv_len;
     60  1.1  lukem 	if (i) return i;
     61  1.1  lukem 	return (strncasecmp( name->bv_val, mir->mir_name.bv_val, name->bv_len ));
     62  1.1  lukem }
     63  1.1  lukem 
     64  1.1  lukem MatchingRule *
     65  1.1  lukem mr_find( const char *mrname )
     66  1.1  lukem {
     67  1.1  lukem 	struct berval bv;
     68  1.1  lukem 
     69  1.1  lukem 	bv.bv_val = (char *)mrname;
     70  1.1  lukem 	bv.bv_len = strlen( mrname );
     71  1.1  lukem 	return mr_bvfind( &bv );
     72  1.1  lukem }
     73  1.1  lukem 
     74  1.1  lukem MatchingRule *
     75  1.1  lukem mr_bvfind( struct berval *mrname )
     76  1.1  lukem {
     77  1.1  lukem 	struct mindexrec	*mir = NULL;
     78  1.1  lukem 
     79  1.1  lukem 	if ( (mir = avl_find( mr_index, mrname, mr_index_name_cmp )) != NULL ) {
     80  1.1  lukem 		return( mir->mir_mr );
     81  1.1  lukem 	}
     82  1.1  lukem 	return( NULL );
     83  1.1  lukem }
     84  1.1  lukem 
     85  1.1  lukem void
     86  1.1  lukem mr_destroy( void )
     87  1.1  lukem {
     88  1.1  lukem 	MatchingRule *m;
     89  1.1  lukem 
     90  1.1  lukem 	avl_free(mr_index, ldap_memfree);
     91  1.1  lukem 	while( !LDAP_SLIST_EMPTY(&mr_list) ) {
     92  1.1  lukem 		m = LDAP_SLIST_FIRST(&mr_list);
     93  1.1  lukem 		LDAP_SLIST_REMOVE_HEAD(&mr_list, smr_next);
     94  1.1  lukem 		ch_free( m->smr_str.bv_val );
     95  1.1  lukem 		ch_free( m->smr_compat_syntaxes );
     96  1.1  lukem 		ldap_matchingrule_free((LDAPMatchingRule *)m);
     97  1.1  lukem 	}
     98  1.1  lukem }
     99  1.1  lukem 
    100  1.1  lukem static int
    101  1.1  lukem mr_insert(
    102  1.1  lukem     MatchingRule	*smr,
    103  1.1  lukem     const char		**err
    104  1.1  lukem )
    105  1.1  lukem {
    106  1.1  lukem 	struct mindexrec	*mir;
    107  1.1  lukem 	char			**names;
    108  1.1  lukem 
    109  1.1  lukem 	LDAP_SLIST_NEXT( smr, smr_next ) = NULL;
    110  1.1  lukem 	LDAP_SLIST_INSERT_HEAD(&mr_list, smr, smr_next);
    111  1.1  lukem 
    112  1.1  lukem 	if ( smr->smr_oid ) {
    113  1.1  lukem 		mir = (struct mindexrec *)
    114  1.1  lukem 			ch_calloc( 1, sizeof(struct mindexrec) );
    115  1.1  lukem 		mir->mir_name.bv_val = smr->smr_oid;
    116  1.1  lukem 		mir->mir_name.bv_len = strlen( smr->smr_oid );
    117  1.1  lukem 		mir->mir_mr = smr;
    118  1.1  lukem 		if ( avl_insert( &mr_index, (caddr_t) mir,
    119  1.1  lukem 		                 mr_index_cmp, avl_dup_error ) ) {
    120  1.1  lukem 			*err = smr->smr_oid;
    121  1.1  lukem 			ldap_memfree(mir);
    122  1.1  lukem 			return SLAP_SCHERR_MR_DUP;
    123  1.1  lukem 		}
    124  1.1  lukem 		/* FIX: temporal consistency check */
    125  1.1  lukem 		mr_bvfind(&mir->mir_name);
    126  1.1  lukem 	}
    127  1.1  lukem 	if ( (names = smr->smr_names) ) {
    128  1.1  lukem 		while ( *names ) {
    129  1.1  lukem 			mir = (struct mindexrec *)
    130  1.1  lukem 				ch_calloc( 1, sizeof(struct mindexrec) );
    131  1.1  lukem 			mir->mir_name.bv_val = *names;
    132  1.1  lukem 			mir->mir_name.bv_len = strlen( *names );
    133  1.1  lukem 			mir->mir_mr = smr;
    134  1.1  lukem 			if ( avl_insert( &mr_index, (caddr_t) mir,
    135  1.1  lukem 			                 mr_index_cmp, avl_dup_error ) ) {
    136  1.1  lukem 				*err = *names;
    137  1.1  lukem 				ldap_memfree(mir);
    138  1.1  lukem 				return SLAP_SCHERR_MR_DUP;
    139  1.1  lukem 			}
    140  1.1  lukem 			/* FIX: temporal consistency check */
    141  1.1  lukem 			mr_bvfind(&mir->mir_name);
    142  1.1  lukem 			names++;
    143  1.1  lukem 		}
    144  1.1  lukem 	}
    145  1.1  lukem 	return 0;
    146  1.1  lukem }
    147  1.1  lukem 
    148  1.1  lukem int
    149  1.1  lukem mr_make_syntax_compat_with_mr(
    150  1.1  lukem 	Syntax		*syn,
    151  1.1  lukem 	MatchingRule	*mr )
    152  1.1  lukem {
    153  1.1  lukem 	int		n = 0;
    154  1.1  lukem 
    155  1.1  lukem 	assert( syn != NULL );
    156  1.1  lukem 	assert( mr != NULL );
    157  1.1  lukem 
    158  1.1  lukem 	if ( mr->smr_compat_syntaxes ) {
    159  1.1  lukem 		/* count esisting */
    160  1.1  lukem 		for ( n = 0;
    161  1.1  lukem 			mr->smr_compat_syntaxes[ n ];
    162  1.1  lukem 			n++ )
    163  1.1  lukem 		{
    164  1.1  lukem 			if ( mr->smr_compat_syntaxes[ n ] == syn ) {
    165  1.1  lukem 				/* already compatible; mmmmh... */
    166  1.1  lukem 				return 1;
    167  1.1  lukem 			}
    168  1.1  lukem 		}
    169  1.1  lukem 	}
    170  1.1  lukem 
    171  1.1  lukem 	mr->smr_compat_syntaxes = ch_realloc(
    172  1.1  lukem 		mr->smr_compat_syntaxes,
    173  1.1  lukem 		sizeof( Syntax * )*(n + 2) );
    174  1.1  lukem 	mr->smr_compat_syntaxes[ n ] = syn;
    175  1.1  lukem 	mr->smr_compat_syntaxes[ n + 1 ] = NULL;
    176  1.1  lukem 
    177  1.1  lukem 	return 0;
    178  1.1  lukem }
    179  1.1  lukem 
    180  1.1  lukem int
    181  1.1  lukem mr_make_syntax_compat_with_mrs(
    182  1.1  lukem 	const char *syntax,
    183  1.1  lukem 	char *const *mrs )
    184  1.1  lukem {
    185  1.1  lukem 	int	r, rc = 0;
    186  1.1  lukem 	Syntax	*syn;
    187  1.1  lukem 
    188  1.1  lukem 	assert( syntax != NULL );
    189  1.1  lukem 	assert( mrs != NULL );
    190  1.1  lukem 
    191  1.1  lukem 	syn = syn_find( syntax );
    192  1.1  lukem 	if ( syn == NULL ) {
    193  1.1  lukem 		return -1;
    194  1.1  lukem 	}
    195  1.1  lukem 
    196  1.1  lukem 	for ( r = 0; mrs[ r ] != NULL; r++ ) {
    197  1.1  lukem 		MatchingRule	*mr = mr_find( mrs[ r ] );
    198  1.1  lukem 		if ( mr == NULL ) {
    199  1.1  lukem 			/* matchingRule not found -- ignore by now */
    200  1.1  lukem 			continue;
    201  1.1  lukem 		}
    202  1.1  lukem 
    203  1.1  lukem 		rc += mr_make_syntax_compat_with_mr( syn, mr );
    204  1.1  lukem 	}
    205  1.1  lukem 
    206  1.1  lukem 	return rc;
    207  1.1  lukem }
    208  1.1  lukem 
    209  1.1  lukem int
    210  1.1  lukem mr_add(
    211  1.1  lukem     LDAPMatchingRule		*mr,
    212  1.1  lukem     slap_mrule_defs_rec	*def,
    213  1.1  lukem 	MatchingRule	*amr,
    214  1.1  lukem     const char		**err
    215  1.1  lukem )
    216  1.1  lukem {
    217  1.1  lukem 	MatchingRule	*smr;
    218  1.1  lukem 	Syntax		*syn;
    219  1.1  lukem 	Syntax		**compat_syn = NULL;
    220  1.1  lukem 	int		code;
    221  1.1  lukem 
    222  1.1  lukem 	if( def->mrd_compat_syntaxes ) {
    223  1.1  lukem 		int i;
    224  1.1  lukem 		for( i=0; def->mrd_compat_syntaxes[i]; i++ ) {
    225  1.1  lukem 			/* just count em */
    226  1.1  lukem 		}
    227  1.1  lukem 
    228  1.1  lukem 		compat_syn = ch_malloc( sizeof(Syntax *) * (i+1) );
    229  1.1  lukem 
    230  1.1  lukem 		for( i=0; def->mrd_compat_syntaxes[i]; i++ ) {
    231  1.1  lukem 			compat_syn[i] = syn_find( def->mrd_compat_syntaxes[i] );
    232  1.1  lukem 			if( compat_syn[i] == NULL ) {
    233  1.1  lukem 				ch_free( compat_syn );
    234  1.1  lukem 				return SLAP_SCHERR_SYN_NOT_FOUND;
    235  1.1  lukem 			}
    236  1.1  lukem 		}
    237  1.1  lukem 
    238  1.1  lukem 		compat_syn[i] = NULL;
    239  1.1  lukem 	}
    240  1.1  lukem 
    241  1.1  lukem 	smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
    242  1.1  lukem 	AC_MEMCPY( &smr->smr_mrule, mr, sizeof(LDAPMatchingRule));
    243  1.1  lukem 
    244  1.1  lukem 	/*
    245  1.1  lukem 	 * note: smr_bvoid uses the same memory of smr_mrule.mr_oid;
    246  1.1  lukem 	 * smr_oidlen is #defined as smr_bvoid.bv_len
    247  1.1  lukem 	 */
    248  1.1  lukem 	smr->smr_bvoid.bv_val = smr->smr_mrule.mr_oid;
    249  1.1  lukem 	smr->smr_oidlen = strlen( mr->mr_oid );
    250  1.1  lukem 	smr->smr_usage = def->mrd_usage;
    251  1.1  lukem 	smr->smr_compat_syntaxes = compat_syn;
    252  1.1  lukem 	smr->smr_normalize = def->mrd_normalize;
    253  1.1  lukem 	smr->smr_match = def->mrd_match;
    254  1.1  lukem 	smr->smr_indexer = def->mrd_indexer;
    255  1.1  lukem 	smr->smr_filter = def->mrd_filter;
    256  1.1  lukem 	smr->smr_associated = amr;
    257  1.1  lukem 
    258  1.1  lukem 	if ( smr->smr_syntax_oid ) {
    259  1.1  lukem 		if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
    260  1.1  lukem 			smr->smr_syntax = syn;
    261  1.1  lukem 		} else {
    262  1.1  lukem 			*err = smr->smr_syntax_oid;
    263  1.1  lukem 			ch_free( smr );
    264  1.1  lukem 			return SLAP_SCHERR_SYN_NOT_FOUND;
    265  1.1  lukem 		}
    266  1.1  lukem 	} else {
    267  1.1  lukem 		*err = "";
    268  1.1  lukem 		ch_free( smr );
    269  1.1  lukem 		return SLAP_SCHERR_MR_INCOMPLETE;
    270  1.1  lukem 	}
    271  1.1  lukem 	code = mr_insert(smr,err);
    272  1.1  lukem 	return code;
    273  1.1  lukem }
    274  1.1  lukem 
    275  1.1  lukem int
    276  1.1  lukem register_matching_rule(
    277  1.1  lukem 	slap_mrule_defs_rec *def )
    278  1.1  lukem {
    279  1.1  lukem 	LDAPMatchingRule *mr;
    280  1.1  lukem 	MatchingRule *amr = NULL;
    281  1.1  lukem 	int		code;
    282  1.1  lukem 	const char	*err;
    283  1.1  lukem 
    284  1.1  lukem 	if( def->mrd_usage == SLAP_MR_NONE && def->mrd_compat_syntaxes == NULL ) {
    285  1.1  lukem 		Debug( LDAP_DEBUG_ANY, "register_matching_rule: not usable %s\n",
    286  1.1  lukem 		    def->mrd_desc, 0, 0 );
    287  1.1  lukem 
    288  1.1  lukem 		return -1;
    289  1.1  lukem 	}
    290  1.1  lukem 
    291  1.1  lukem 	if( def->mrd_associated != NULL ) {
    292  1.1  lukem 		amr = mr_find( def->mrd_associated );
    293  1.1  lukem 		if( amr == NULL ) {
    294  1.1  lukem 			Debug( LDAP_DEBUG_ANY, "register_matching_rule: "
    295  1.1  lukem 				"could not locate associated matching rule %s for %s\n",
    296  1.1  lukem 				def->mrd_associated, def->mrd_desc, 0 );
    297  1.1  lukem 
    298  1.1  lukem 			return -1;
    299  1.1  lukem 		}
    300  1.1  lukem 
    301  1.1  lukem 		if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
    302  1.1  lukem 			(( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) == SLAP_MR_NONE ))
    303  1.1  lukem 		{
    304  1.1  lukem 			if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
    305  1.1  lukem 				(( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) != SLAP_MR_NONE ))
    306  1.1  lukem 			{
    307  1.1  lukem 				Debug( LDAP_DEBUG_ANY, "register_matching_rule: "
    308  1.1  lukem 						"inappropriate (approx) association %s for %s\n",
    309  1.1  lukem 					def->mrd_associated, def->mrd_desc, 0 );
    310  1.1  lukem 				return -1;
    311  1.1  lukem 			}
    312  1.1  lukem 
    313  1.1  lukem 		} else if (!( amr->smr_usage & SLAP_MR_EQUALITY )) {
    314  1.1  lukem 				Debug( LDAP_DEBUG_ANY, "register_matching_rule: "
    315  1.1  lukem 					"inappropriate (equalilty) association %s for %s\n",
    316  1.1  lukem 					def->mrd_associated, def->mrd_desc, 0 );
    317  1.1  lukem 				return -1;
    318  1.1  lukem 		}
    319  1.1  lukem 	}
    320  1.1  lukem 
    321  1.1  lukem 	mr = ldap_str2matchingrule( def->mrd_desc, &code, &err,
    322  1.1  lukem 		LDAP_SCHEMA_ALLOW_ALL );
    323  1.1  lukem 	if ( !mr ) {
    324  1.1  lukem 		Debug( LDAP_DEBUG_ANY,
    325  1.1  lukem 			"Error in register_matching_rule: %s before %s in %s\n",
    326  1.1  lukem 		    ldap_scherr2str(code), err, def->mrd_desc );
    327  1.1  lukem 
    328  1.1  lukem 		return -1;
    329  1.1  lukem 	}
    330  1.1  lukem 
    331  1.1  lukem 
    332  1.1  lukem 	code = mr_add( mr, def, amr, &err );
    333  1.1  lukem 
    334  1.1  lukem 	ldap_memfree( mr );
    335  1.1  lukem 
    336  1.1  lukem 	if ( code ) {
    337  1.1  lukem 		Debug( LDAP_DEBUG_ANY,
    338  1.1  lukem 			"Error in register_matching_rule: %s for %s in %s\n",
    339  1.1  lukem 		    scherr2str(code), err, def->mrd_desc );
    340  1.1  lukem 
    341  1.1  lukem 		return -1;
    342  1.1  lukem 	}
    343  1.1  lukem 
    344  1.1  lukem 	return 0;
    345  1.1  lukem }
    346  1.1  lukem 
    347  1.1  lukem void
    348  1.1  lukem mru_destroy( void )
    349  1.1  lukem {
    350  1.1  lukem 	MatchingRuleUse *m;
    351  1.1  lukem 
    352  1.1  lukem 	while( !LDAP_SLIST_EMPTY(&mru_list) ) {
    353  1.1  lukem 		m = LDAP_SLIST_FIRST(&mru_list);
    354  1.1  lukem 		LDAP_SLIST_REMOVE_HEAD(&mru_list, smru_next);
    355  1.1  lukem 
    356  1.1  lukem 		if ( m->smru_str.bv_val ) {
    357  1.1  lukem 			ch_free( m->smru_str.bv_val );
    358  1.1  lukem 			m->smru_str.bv_val = NULL;
    359  1.1  lukem 		}
    360  1.1  lukem 		/* memory borrowed from m->smru_mr */
    361  1.1  lukem 		m->smru_oid = NULL;
    362  1.1  lukem 		m->smru_names = NULL;
    363  1.1  lukem 		m->smru_desc = NULL;
    364  1.1  lukem 
    365  1.1  lukem 		/* free what's left (basically smru_mruleuse.mru_applies_oids) */
    366  1.1  lukem 		ldap_matchingruleuse_free((LDAPMatchingRuleUse *)m);
    367  1.1  lukem 	}
    368  1.1  lukem }
    369  1.1  lukem 
    370  1.1  lukem int
    371  1.1  lukem matching_rule_use_init( void )
    372  1.1  lukem {
    373  1.1  lukem 	MatchingRule	*mr;
    374  1.1  lukem 	MatchingRuleUse	**mru_ptr = &LDAP_SLIST_FIRST(&mru_list);
    375  1.1  lukem 
    376  1.1  lukem 	Debug( LDAP_DEBUG_TRACE, "matching_rule_use_init\n", 0, 0, 0 );
    377  1.1  lukem 
    378  1.1  lukem 	LDAP_SLIST_FOREACH( mr, &mr_list, smr_next ) {
    379  1.1  lukem 		AttributeType	*at;
    380  1.1  lukem 		MatchingRuleUse	mru_storage = { 0 },
    381  1.1  lukem 				*mru = &mru_storage;
    382  1.1  lukem 
    383  1.1  lukem 		char		**applies_oids = NULL;
    384  1.1  lukem 
    385  1.1  lukem 		mr->smr_mru = NULL;
    386  1.1  lukem 
    387  1.1  lukem 		/* hide rules marked as HIDE */
    388  1.1  lukem 		if ( mr->smr_usage & SLAP_MR_HIDE ) {
    389  1.1  lukem 			continue;
    390  1.1  lukem 		}
    391  1.1  lukem 
    392  1.1  lukem 		/* hide rules not marked as designed for extensibility */
    393  1.1  lukem 		/* MR_EXT means can be used any attribute type whose
    394  1.1  lukem 		 * syntax is same as the assertion syntax.
    395  1.1  lukem 		 * Another mechanism is needed where rule can be used
    396  1.1  lukem 		 * with attribute of other syntaxes.
    397  1.1  lukem 		 * Framework doesn't support this (yet).
    398  1.1  lukem 		 */
    399  1.1  lukem 
    400  1.1  lukem 		if (!( ( mr->smr_usage & SLAP_MR_EXT )
    401  1.1  lukem 			|| mr->smr_compat_syntaxes ) )
    402  1.1  lukem 		{
    403  1.1  lukem 			continue;
    404  1.1  lukem 		}
    405  1.1  lukem 
    406  1.1  lukem 		/*
    407  1.1  lukem 		 * Note: we're using the same values of the corresponding
    408  1.1  lukem 		 * MatchingRule structure; maybe we'd copy them ...
    409  1.1  lukem 		 */
    410  1.1  lukem 		mru->smru_mr = mr;
    411  1.1  lukem 		mru->smru_obsolete = mr->smr_obsolete;
    412  1.1  lukem 		mru->smru_applies_oids = NULL;
    413  1.1  lukem 		LDAP_SLIST_NEXT(mru, smru_next) = NULL;
    414  1.1  lukem 		mru->smru_oid = mr->smr_oid;
    415  1.1  lukem 		mru->smru_names = mr->smr_names;
    416  1.1  lukem 		mru->smru_desc = mr->smr_desc;
    417  1.1  lukem 
    418  1.1  lukem 		Debug( LDAP_DEBUG_TRACE, "    %s (%s): ",
    419  1.1  lukem 				mru->smru_oid,
    420  1.1  lukem 				mru->smru_names ? mru->smru_names[ 0 ] : "", 0 );
    421  1.1  lukem 
    422  1.1  lukem 		at = NULL;
    423  1.1  lukem 		for ( at_start( &at ); at; at_next( &at ) ) {
    424  1.1  lukem 			if( at->sat_flags & SLAP_AT_HIDE ) continue;
    425  1.1  lukem 
    426  1.1  lukem 			if( mr_usable_with_at( mr, at )) {
    427  1.1  lukem 				ldap_charray_add( &applies_oids, at->sat_cname.bv_val );
    428  1.1  lukem 			}
    429  1.1  lukem 		}
    430  1.1  lukem 
    431  1.1  lukem 		/*
    432  1.1  lukem 		 * Note: the matchingRules that are not used
    433  1.1  lukem 		 * by any attributeType are not listed as
    434  1.1  lukem 		 * matchingRuleUse
    435  1.1  lukem 		 */
    436  1.1  lukem 		if ( applies_oids != NULL ) {
    437  1.1  lukem 			mru->smru_applies_oids = applies_oids;
    438  1.1  lukem 			{
    439  1.1  lukem 				char *str = ldap_matchingruleuse2str( &mru->smru_mruleuse );
    440  1.1  lukem 				Debug( LDAP_DEBUG_TRACE, "matchingRuleUse: %s\n", str, 0, 0 );
    441  1.1  lukem 				ldap_memfree( str );
    442  1.1  lukem 			}
    443  1.1  lukem 
    444  1.1  lukem 			mru = (MatchingRuleUse *)ber_memalloc( sizeof( MatchingRuleUse ) );
    445  1.1  lukem 			/* call-forward from MatchingRule to MatchingRuleUse */
    446  1.1  lukem 			mr->smr_mru = mru;
    447  1.1  lukem 			/* copy static data to newly allocated struct */
    448  1.1  lukem 			*mru = mru_storage;
    449  1.1  lukem 			/* append the struct pointer to the end of the list */
    450  1.1  lukem 			*mru_ptr = mru;
    451  1.1  lukem 			/* update the list head pointer */
    452  1.1  lukem 			mru_ptr = &LDAP_SLIST_NEXT(mru,smru_next);
    453  1.1  lukem 		}
    454  1.1  lukem 	}
    455  1.1  lukem 
    456  1.1  lukem 	return( 0 );
    457  1.1  lukem }
    458  1.1  lukem 
    459  1.1  lukem int
    460  1.1  lukem mr_usable_with_at(
    461  1.1  lukem 	MatchingRule	*mr,
    462  1.1  lukem 	AttributeType	*at )
    463  1.1  lukem {
    464  1.1  lukem 	if ( ( mr->smr_usage & SLAP_MR_EXT ) && (
    465  1.1  lukem 		mr->smr_syntax == at->sat_syntax ||
    466  1.1  lukem 		mr == at->sat_equality ||
    467  1.1  lukem 		mr == at->sat_approx ||
    468  1.1  lukem 		syn_is_sup( at->sat_syntax, mr->smr_syntax ) ) )
    469  1.1  lukem 	{
    470  1.1  lukem 		return 1;
    471  1.1  lukem 	}
    472  1.1  lukem 
    473  1.1  lukem 	if ( mr->smr_compat_syntaxes ) {
    474  1.1  lukem 		int i;
    475  1.1  lukem 		for( i=0; mr->smr_compat_syntaxes[i]; i++ ) {
    476  1.1  lukem 			if( at->sat_syntax == mr->smr_compat_syntaxes[i] ) {
    477  1.1  lukem 				return 1;
    478  1.1  lukem 			}
    479  1.1  lukem 		}
    480  1.1  lukem 	}
    481  1.1  lukem 	return 0;
    482  1.1  lukem }
    483  1.1  lukem 
    484  1.1  lukem int mr_schema_info( Entry *e )
    485  1.1  lukem {
    486  1.1  lukem 	AttributeDescription *ad_matchingRules = slap_schema.si_ad_matchingRules;
    487  1.1  lukem 	MatchingRule *mr;
    488  1.1  lukem 	struct berval nval;
    489  1.1  lukem 
    490  1.1  lukem 	LDAP_SLIST_FOREACH(mr, &mr_list, smr_next ) {
    491  1.1  lukem 		if ( mr->smr_usage & SLAP_MR_HIDE ) {
    492  1.1  lukem 			/* skip hidden rules */
    493  1.1  lukem 			continue;
    494  1.1  lukem 		}
    495  1.1  lukem 
    496  1.1  lukem 		if ( ! mr->smr_match ) {
    497  1.1  lukem 			/* skip rules without matching functions */
    498  1.1  lukem 			continue;
    499  1.1  lukem 		}
    500  1.1  lukem 
    501  1.1  lukem 		if ( mr->smr_str.bv_val == NULL ) {
    502  1.1  lukem 			if ( ldap_matchingrule2bv( &mr->smr_mrule, &mr->smr_str ) == NULL ) {
    503  1.1  lukem 				return -1;
    504  1.1  lukem 			}
    505  1.1  lukem 		}
    506  1.1  lukem #if 0
    507  1.1  lukem 		Debug( LDAP_DEBUG_TRACE, "Merging mr [%lu] %s\n",
    508  1.1  lukem 			mr->smr_str.bv_len, mr->smr_str.bv_val, 0 );
    509  1.1  lukem #endif
    510  1.1  lukem 
    511  1.1  lukem 		nval.bv_val = mr->smr_oid;
    512  1.1  lukem 		nval.bv_len = strlen(mr->smr_oid);
    513  1.1  lukem 		if( attr_merge_one( e, ad_matchingRules, &mr->smr_str, &nval ) ) {
    514  1.1  lukem 			return -1;
    515  1.1  lukem 		}
    516  1.1  lukem 	}
    517  1.1  lukem 	return 0;
    518  1.1  lukem }
    519  1.1  lukem 
    520  1.1  lukem int mru_schema_info( Entry *e )
    521  1.1  lukem {
    522  1.1  lukem 	AttributeDescription *ad_matchingRuleUse
    523  1.1  lukem 		= slap_schema.si_ad_matchingRuleUse;
    524  1.1  lukem 	MatchingRuleUse	*mru;
    525  1.1  lukem 	struct berval nval;
    526  1.1  lukem 
    527  1.1  lukem 	LDAP_SLIST_FOREACH( mru, &mru_list, smru_next ) {
    528  1.1  lukem 		assert( !( mru->smru_usage & SLAP_MR_HIDE ) );
    529  1.1  lukem 
    530  1.1  lukem 		if ( mru->smru_str.bv_val == NULL ) {
    531  1.1  lukem 			if ( ldap_matchingruleuse2bv( &mru->smru_mruleuse, &mru->smru_str )
    532  1.1  lukem 					== NULL ) {
    533  1.1  lukem 				return -1;
    534  1.1  lukem 			}
    535  1.1  lukem 		}
    536  1.1  lukem 
    537  1.1  lukem #if 0
    538  1.1  lukem 		Debug( LDAP_DEBUG_TRACE, "Merging mru [%lu] %s\n",
    539  1.1  lukem 			mru->smru_str.bv_len, mru->smru_str.bv_val, 0 );
    540  1.1  lukem #endif
    541  1.1  lukem 
    542  1.1  lukem 		nval.bv_val = mru->smru_oid;
    543  1.1  lukem 		nval.bv_len = strlen(mru->smru_oid);
    544  1.1  lukem 		if( attr_merge_one( e, ad_matchingRuleUse, &mru->smru_str, &nval ) ) {
    545  1.1  lukem 			return -1;
    546  1.1  lukem 		}
    547  1.1  lukem 	}
    548  1.1  lukem 	return 0;
    549  1.1  lukem }
    550