1 /* mr.c - routines to manage matching rule definitions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
28 struct berval mir_name;
32 static Avlnode *mr_index = NULL;
33 static LDAP_SLIST_HEAD(MRList, MatchingRule) mr_list
34 = LDAP_SLIST_HEAD_INITIALIZER(&mr_list);
35 static LDAP_SLIST_HEAD(MRUList, MatchingRuleUse) mru_list
36 = LDAP_SLIST_HEAD_INITIALIZER(&mru_list);
44 const struct mindexrec *mir1 = v_mir1;
45 const struct mindexrec *mir2 = v_mir2;
46 int i = mir1->mir_name.bv_len - mir2->mir_name.bv_len;
48 return (strcasecmp( mir1->mir_name.bv_val, mir2->mir_name.bv_val ));
57 const struct berval *name = v_name;
58 const struct mindexrec *mir = v_mir;
59 int i = name->bv_len - mir->mir_name.bv_len;
61 return (strncasecmp( name->bv_val, mir->mir_name.bv_val, name->bv_len ));
65 mr_find( const char *mrname )
69 bv.bv_val = (char *)mrname;
70 bv.bv_len = strlen( mrname );
71 return mr_bvfind( &bv );
75 mr_bvfind( struct berval *mrname )
77 struct mindexrec *mir = NULL;
79 if ( (mir = avl_find( mr_index, mrname, mr_index_name_cmp )) != NULL ) {
80 return( mir->mir_mr );
90 avl_free(mr_index, ldap_memfree);
91 while( !LDAP_SLIST_EMPTY(&mr_list) ) {
92 m = LDAP_SLIST_FIRST(&mr_list);
93 LDAP_SLIST_REMOVE_HEAD(&mr_list, smr_next);
94 ch_free( m->smr_str.bv_val );
95 ch_free( m->smr_compat_syntaxes );
96 ldap_matchingrule_free((LDAPMatchingRule *)m);
106 struct mindexrec *mir;
109 LDAP_SLIST_NEXT( smr, smr_next ) = NULL;
110 LDAP_SLIST_INSERT_HEAD(&mr_list, smr, smr_next);
112 if ( smr->smr_oid ) {
113 mir = (struct mindexrec *)
114 ch_calloc( 1, sizeof(struct mindexrec) );
115 mir->mir_name.bv_val = smr->smr_oid;
116 mir->mir_name.bv_len = strlen( smr->smr_oid );
118 if ( avl_insert( &mr_index, (caddr_t) mir,
119 mr_index_cmp, avl_dup_error ) ) {
122 return SLAP_SCHERR_MR_DUP;
124 /* FIX: temporal consistency check */
125 mr_bvfind(&mir->mir_name);
127 if ( (names = smr->smr_names) ) {
129 mir = (struct mindexrec *)
130 ch_calloc( 1, sizeof(struct mindexrec) );
131 mir->mir_name.bv_val = *names;
132 mir->mir_name.bv_len = strlen( *names );
134 if ( avl_insert( &mr_index, (caddr_t) mir,
135 mr_index_cmp, avl_dup_error ) ) {
138 return SLAP_SCHERR_MR_DUP;
140 /* FIX: temporal consistency check */
141 mr_bvfind(&mir->mir_name);
150 LDAPMatchingRule *mr,
151 slap_mrule_defs_rec *def,
158 Syntax **compat_syn = NULL;
161 if( def->mrd_compat_syntaxes ) {
163 for( i=0; def->mrd_compat_syntaxes[i]; i++ ) {
167 compat_syn = ch_malloc( sizeof(Syntax *) * (i+1) );
169 for( i=0; def->mrd_compat_syntaxes[i]; i++ ) {
170 compat_syn[i] = syn_find( def->mrd_compat_syntaxes[i] );
171 if( compat_syn[i] == NULL ) {
172 ch_free( compat_syn );
173 return SLAP_SCHERR_SYN_NOT_FOUND;
177 compat_syn[i] = NULL;
180 smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
181 AC_MEMCPY( &smr->smr_mrule, mr, sizeof(LDAPMatchingRule));
184 * note: smr_bvoid uses the same memory of smr_mrule.mr_oid;
185 * smr_oidlen is #defined as smr_bvoid.bv_len
187 smr->smr_bvoid.bv_val = smr->smr_mrule.mr_oid;
188 smr->smr_oidlen = strlen( mr->mr_oid );
189 smr->smr_usage = def->mrd_usage;
190 smr->smr_compat_syntaxes = compat_syn;
191 smr->smr_normalize = def->mrd_normalize;
192 smr->smr_match = def->mrd_match;
193 smr->smr_indexer = def->mrd_indexer;
194 smr->smr_filter = def->mrd_filter;
195 smr->smr_associated = amr;
197 if ( smr->smr_syntax_oid ) {
198 if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
199 smr->smr_syntax = syn;
201 *err = smr->smr_syntax_oid;
203 return SLAP_SCHERR_SYN_NOT_FOUND;
208 return SLAP_SCHERR_MR_INCOMPLETE;
210 code = mr_insert(smr,err);
215 register_matching_rule(
216 slap_mrule_defs_rec *def )
218 LDAPMatchingRule *mr;
219 MatchingRule *amr = NULL;
223 if( def->mrd_usage == SLAP_MR_NONE && def->mrd_compat_syntaxes == NULL ) {
224 Debug( LDAP_DEBUG_ANY, "register_matching_rule: not usable %s\n",
225 def->mrd_desc, 0, 0 );
230 if( def->mrd_associated != NULL ) {
231 amr = mr_find( def->mrd_associated );
233 Debug( LDAP_DEBUG_ANY, "register_matching_rule: "
234 "could not locate associated matching rule %s for %s\n",
235 def->mrd_associated, def->mrd_desc, 0 );
240 if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
241 (( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) == SLAP_MR_NONE ))
243 if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
244 (( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) != SLAP_MR_NONE ))
246 Debug( LDAP_DEBUG_ANY, "register_matching_rule: "
247 "inappropriate (approx) association %s for %s\n",
248 def->mrd_associated, def->mrd_desc, 0 );
252 } else if (!( amr->smr_usage & SLAP_MR_EQUALITY )) {
253 Debug( LDAP_DEBUG_ANY, "register_matching_rule: "
254 "inappropriate (equalilty) association %s for %s\n",
255 def->mrd_associated, def->mrd_desc, 0 );
260 mr = ldap_str2matchingrule( def->mrd_desc, &code, &err,
261 LDAP_SCHEMA_ALLOW_ALL );
263 Debug( LDAP_DEBUG_ANY,
264 "Error in register_matching_rule: %s before %s in %s\n",
265 ldap_scherr2str(code), err, def->mrd_desc );
271 code = mr_add( mr, def, amr, &err );
276 Debug( LDAP_DEBUG_ANY,
277 "Error in register_matching_rule: %s for %s in %s\n",
278 scherr2str(code), err, def->mrd_desc );
291 while( !LDAP_SLIST_EMPTY(&mru_list) ) {
292 m = LDAP_SLIST_FIRST(&mru_list);
293 LDAP_SLIST_REMOVE_HEAD(&mru_list, smru_next);
295 if ( m->smru_str.bv_val ) {
296 ch_free( m->smru_str.bv_val );
297 m->smru_str.bv_val = NULL;
299 /* memory borrowed from m->smru_mr */
301 m->smru_names = NULL;
304 /* free what's left (basically smru_mruleuse.mru_applies_oids) */
305 ldap_matchingruleuse_free((LDAPMatchingRuleUse *)m);
310 matching_rule_use_init( void )
313 MatchingRuleUse **mru_ptr = &LDAP_SLIST_FIRST(&mru_list);
315 Debug( LDAP_DEBUG_TRACE, "matching_rule_use_init\n", 0, 0, 0 );
317 LDAP_SLIST_FOREACH( mr, &mr_list, smr_next ) {
319 MatchingRuleUse mru_storage, *mru = &mru_storage;
321 char **applies_oids = NULL;
325 /* hide rules marked as HIDE */
326 if ( mr->smr_usage & SLAP_MR_HIDE ) {
330 /* hide rules not marked as designed for extensibility */
331 /* MR_EXT means can be used any attribute type whose
332 * syntax is same as the assertion syntax.
333 * Another mechanism is needed where rule can be used
334 * with attribute of other syntaxes.
335 * Framework doesn't support this (yet).
338 if (!( ( mr->smr_usage & SLAP_MR_EXT )
339 || mr->smr_compat_syntaxes ) )
344 memset( mru, 0, sizeof( MatchingRuleUse ) );
347 * Note: we're using the same values of the corresponding
348 * MatchingRule structure; maybe we'd copy them ...
351 mru->smru_obsolete = mr->smr_obsolete;
352 mru->smru_applies_oids = NULL;
353 LDAP_SLIST_NEXT(mru, smru_next) = NULL;
354 mru->smru_oid = mr->smr_oid;
355 mru->smru_names = mr->smr_names;
356 mru->smru_desc = mr->smr_desc;
358 Debug( LDAP_DEBUG_TRACE, " %s (%s): ",
360 mru->smru_names ? mru->smru_names[ 0 ] : "", 0 );
363 for ( at_start( &at ); at; at_next( &at ) ) {
364 if( at->sat_flags & SLAP_AT_HIDE ) continue;
366 if( mr_usable_with_at( mr, at )) {
367 ldap_charray_add( &applies_oids, at->sat_cname.bv_val );
372 * Note: the matchingRules that are not used
373 * by any attributeType are not listed as
376 if ( applies_oids != NULL ) {
377 mru->smru_applies_oids = applies_oids;
379 char *str = ldap_matchingruleuse2str( &mru->smru_mruleuse );
380 Debug( LDAP_DEBUG_TRACE, "matchingRuleUse: %s\n", str, 0, 0 );
384 mru = (MatchingRuleUse *)ber_memalloc( sizeof( MatchingRuleUse ) );
385 /* call-forward from MatchingRule to MatchingRuleUse */
387 /* copy static data to newly allocated struct */
389 /* append the struct pointer to the end of the list */
391 /* update the list head pointer */
392 mru_ptr = &LDAP_SLIST_NEXT(mru,smru_next);
399 int mr_usable_with_at(
403 if( mr->smr_usage & SLAP_MR_EXT && (
404 mr->smr_syntax == at->sat_syntax ||
405 mr == at->sat_equality || mr == at->sat_approx ) )
410 if ( mr->smr_compat_syntaxes ) {
412 for( i=0; mr->smr_compat_syntaxes[i]; i++ ) {
413 if( at->sat_syntax == mr->smr_compat_syntaxes[i] ) {
421 int mr_schema_info( Entry *e )
423 AttributeDescription *ad_matchingRules = slap_schema.si_ad_matchingRules;
427 LDAP_SLIST_FOREACH(mr, &mr_list, smr_next ) {
428 if ( mr->smr_usage & SLAP_MR_HIDE ) {
429 /* skip hidden rules */
433 if ( ! mr->smr_match ) {
434 /* skip rules without matching functions */
438 if ( mr->smr_str.bv_val == NULL ) {
439 if ( ldap_matchingrule2bv( &mr->smr_mrule, &mr->smr_str ) == NULL ) {
444 Debug( LDAP_DEBUG_TRACE, "Merging mr [%lu] %s\n",
445 mr->smr_str.bv_len, mr->smr_str.bv_val, 0 );
448 nval.bv_val = mr->smr_oid;
449 nval.bv_len = strlen(mr->smr_oid);
450 if( attr_merge_one( e, ad_matchingRules, &mr->smr_str, &nval ) ) {
457 int mru_schema_info( Entry *e )
459 AttributeDescription *ad_matchingRuleUse
460 = slap_schema.si_ad_matchingRuleUse;
461 MatchingRuleUse *mru;
464 LDAP_SLIST_FOREACH( mru, &mru_list, smru_next ) {
465 assert( !( mru->smru_usage & SLAP_MR_HIDE ) );
467 if ( mru->smru_str.bv_val == NULL ) {
468 if ( ldap_matchingruleuse2bv( &mru->smru_mruleuse, &mru->smru_str )
475 Debug( LDAP_DEBUG_TRACE, "Merging mru [%lu] %s\n",
476 mru->smru_str.bv_len, mru->smru_str.bv_val, 0 );
479 nval.bv_val = mru->smru_oid;
480 nval.bv_len = strlen(mru->smru_oid);
481 if( attr_merge_one( e, ad_matchingRuleUse, &mru->smru_str, &nval ) ) {