1 /* mr.c - routines to manage matching rule definitions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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>
29 struct berval mir_name;
33 static Avlnode *mr_index = NULL;
34 static LDAP_SLIST_HEAD(MRList, slap_matching_rule) mr_list
35 = LDAP_SLIST_HEAD_INITIALIZER(&mr_list);
36 static LDAP_SLIST_HEAD(MRUList, slap_matching_rule_use) mru_list
37 = LDAP_SLIST_HEAD_INITIALIZER(&mru_list);
45 const struct mindexrec *mir1 = v_mir1;
46 const struct mindexrec *mir2 = v_mir2;
47 int i = mir1->mir_name.bv_len - mir2->mir_name.bv_len;
49 return (strcmp( mir1->mir_name.bv_val, mir2->mir_name.bv_val ));
58 const struct berval *name = v_name;
59 const struct mindexrec *mir = v_mir;
60 int i = name->bv_len - mir->mir_name.bv_len;
62 return (strncmp( name->bv_val, mir->mir_name.bv_val, name->bv_len ));
66 mr_find( const char *mrname )
70 bv.bv_val = (char *)mrname;
71 bv.bv_len = strlen( mrname );
72 return mr_bvfind( &bv );
76 mr_bvfind( struct berval *mrname )
78 struct mindexrec *mir = NULL;
80 if ( (mir = avl_find( mr_index, mrname, mr_index_name_cmp )) != NULL ) {
81 return( mir->mir_mr );
91 avl_free(mr_index, ldap_memfree);
92 while( !LDAP_SLIST_EMPTY(&mr_list) ) {
93 m = LDAP_SLIST_FIRST(&mr_list);
94 LDAP_SLIST_REMOVE_HEAD(&mr_list, smr_next);
95 ch_free( m->smr_str.bv_val );
96 ch_free( m->smr_compat_syntaxes );
97 ldap_matchingrule_free((LDAPMatchingRule *)m);
107 struct mindexrec *mir;
110 LDAP_SLIST_NEXT( smr, smr_next ) = NULL;
111 LDAP_SLIST_INSERT_HEAD(&mr_list, smr, smr_next);
113 if ( smr->smr_oid ) {
114 mir = (struct mindexrec *)
115 ch_calloc( 1, sizeof(struct mindexrec) );
116 mir->mir_name.bv_val = smr->smr_oid;
117 mir->mir_name.bv_len = strlen( smr->smr_oid );
119 if ( avl_insert( &mr_index, (caddr_t) mir,
120 mr_index_cmp, avl_dup_error ) ) {
123 return SLAP_SCHERR_MR_DUP;
125 /* FIX: temporal consistency check */
126 mr_bvfind(&mir->mir_name);
128 if ( (names = smr->smr_names) ) {
130 mir = (struct mindexrec *)
131 ch_calloc( 1, sizeof(struct mindexrec) );
132 mir->mir_name.bv_val = *names;
133 mir->mir_name.bv_len = strlen( *names );
135 if ( avl_insert( &mr_index, (caddr_t) mir,
136 mr_index_cmp, avl_dup_error ) ) {
139 return SLAP_SCHERR_MR_DUP;
141 /* FIX: temporal consistency check */
142 mr_bvfind(&mir->mir_name);
151 LDAPMatchingRule *mr,
152 slap_mrule_defs_rec *def,
159 Syntax **compat_syn = NULL;
162 if( def->mrd_compat_syntaxes ) {
164 for( i=0; def->mrd_compat_syntaxes[i]; i++ ) {
168 compat_syn = ch_malloc( sizeof(Syntax *) * (i+1) );
170 for( i=0; def->mrd_compat_syntaxes[i]; i++ ) {
171 compat_syn[i] = syn_find( def->mrd_compat_syntaxes[i] );
172 if( compat_syn[i] == NULL ) {
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;
202 return SLAP_SCHERR_SYN_NOT_FOUND;
206 return SLAP_SCHERR_MR_INCOMPLETE;
208 code = mr_insert(smr,err);
213 register_matching_rule(
214 slap_mrule_defs_rec *def )
216 LDAPMatchingRule *mr;
217 MatchingRule *amr = NULL;
221 if( def->mrd_usage == SLAP_MR_NONE &&
222 def->mrd_compat_syntaxes == NULL )
225 LDAP_LOG( OPERATION, ERR,
226 "register_matching_rule: %s not usable\n", def->mrd_desc, 0, 0 );
228 Debug( LDAP_DEBUG_ANY, "register_matching_rule: not usable %s\n",
229 def->mrd_desc, 0, 0 );
235 if( def->mrd_associated != NULL ) {
236 amr = mr_find( def->mrd_associated );
240 LDAP_LOG( OPERATION, ERR,
241 "register_matching_rule: could not locate associated "
242 "matching rule %s for %s\n",
243 def->mrd_associated, def->mrd_desc, 0 );
245 Debug( LDAP_DEBUG_ANY, "register_matching_rule: could not locate "
246 "associated matching rule %s for %s\n",
247 def->mrd_associated, def->mrd_desc, 0 );
253 if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
254 (( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) == SLAP_MR_NONE ))
256 if (( def->mrd_usage & SLAP_MR_EQUALITY ) &&
257 (( def->mrd_usage & SLAP_MR_SUBTYPE_MASK ) != SLAP_MR_NONE ))
260 LDAP_LOG( OPERATION, ERR,
261 "register_matching_rule: inappropriate (approx) association "
263 def->mrd_associated, def->mrd_desc, 0 );
265 Debug( LDAP_DEBUG_ANY,
266 "register_matching_rule: inappropriate (approx) association "
268 def->mrd_associated, def->mrd_desc, 0 );
273 } else if (!( amr->smr_usage & SLAP_MR_EQUALITY )) {
275 LDAP_LOG( OPERATION, ERR,
276 "register_matching_rule: inappropriate (equalilty) association "
278 def->mrd_associated, def->mrd_desc, 0 );
280 Debug( LDAP_DEBUG_ANY,
281 "register_matching_rule: inappropriate (equalilty) association "
283 def->mrd_associated, def->mrd_desc, 0 );
289 mr = ldap_str2matchingrule( def->mrd_desc, &code, &err,
290 LDAP_SCHEMA_ALLOW_ALL );
293 LDAP_LOG( OPERATION, ERR,
294 "register_matching_rule: %s before %s in %s.\n",
295 ldap_scherr2str(code), err, def->mrd_desc );
297 Debug( LDAP_DEBUG_ANY,
298 "Error in register_matching_rule: %s before %s in %s\n",
299 ldap_scherr2str(code), err, def->mrd_desc );
306 code = mr_add( mr, def, amr, &err );
312 LDAP_LOG( OPERATION, ERR,
313 "register_matching_rule: %s for %s in %s.\n",
314 scherr2str(code), err, def->mrd_desc );
316 Debug( LDAP_DEBUG_ANY,
317 "Error in register_matching_rule: %s for %s in %s\n",
318 scherr2str(code), err, def->mrd_desc );
332 while( !LDAP_SLIST_EMPTY(&mru_list) ) {
333 m = LDAP_SLIST_FIRST(&mru_list);
334 LDAP_SLIST_REMOVE_HEAD(&mru_list, smru_next);
336 if ( m->smru_str.bv_val ) {
337 ch_free( m->smru_str.bv_val );
338 m->smru_str.bv_val = NULL;
340 /* memory borrowed from m->smru_mr */
342 m->smru_names = NULL;
345 /* free what's left (basically smru_mruleuse.mru_applies_oids) */
346 ldap_matchingruleuse_free((LDAPMatchingRuleUse *)m);
351 matching_rule_use_init( void )
354 MatchingRuleUse **mru_ptr = &LDAP_SLIST_FIRST(&mru_list);
357 LDAP_LOG( OPERATION, INFO, "matching_rule_use_init\n", 0, 0, 0 );
359 Debug( LDAP_DEBUG_TRACE, "matching_rule_use_init\n", 0, 0, 0 );
362 LDAP_SLIST_FOREACH( mr, &mr_list, smr_next ) {
364 MatchingRuleUse mru_storage, *mru = &mru_storage;
366 char **applies_oids = NULL;
370 /* hide rules marked as HIDE */
371 if ( mr->smr_usage & SLAP_MR_HIDE ) {
375 /* hide rules not marked as designed for extensibility */
376 /* MR_EXT means can be used any attribute type whose
377 * syntax is same as the assertion syntax.
378 * Another mechanism is needed where rule can be used
379 * with attribute of other syntaxes.
380 * Framework doesn't support this (yet).
383 if (!( ( mr->smr_usage & SLAP_MR_EXT )
384 || mr->smr_compat_syntaxes ) )
389 memset( mru, 0, sizeof( MatchingRuleUse ) );
392 * Note: we're using the same values of the corresponding
393 * MatchingRule structure; maybe we'd copy them ...
396 mru->smru_obsolete = mr->smr_obsolete;
397 mru->smru_applies_oids = NULL;
398 LDAP_SLIST_NEXT(mru, smru_next) = NULL;
399 mru->smru_oid = mr->smr_oid;
400 mru->smru_names = mr->smr_names;
401 mru->smru_desc = mr->smr_desc;
404 LDAP_LOG( OPERATION, INFO, " %s (%s): ",
406 mru->smru_names ? mru->smru_names[ 0 ] : "", 0 );
408 Debug( LDAP_DEBUG_TRACE, " %s (%s): ",
410 mru->smru_names ? mru->smru_names[ 0 ] : "", 0 );
414 for ( at_start( &at ); at; at_next( &at ) ) {
415 if( at->sat_flags & SLAP_AT_HIDE ) continue;
417 if( mr_usable_with_at( mr, at )) {
418 ldap_charray_add( &applies_oids, at->sat_cname.bv_val );
423 * Note: the matchingRules that are not used
424 * by any attributeType are not listed as
427 if ( applies_oids != NULL ) {
428 mru->smru_applies_oids = applies_oids;
431 char *str = ldap_matchingruleuse2str( &mru->smru_mruleuse );
432 LDAP_LOG( OPERATION, INFO, "matchingRuleUse: %s\n", str, 0, 0 );
437 char *str = ldap_matchingruleuse2str( &mru->smru_mruleuse );
438 Debug( LDAP_DEBUG_TRACE, "matchingRuleUse: %s\n", str, 0, 0 );
443 mru = (MatchingRuleUse *)ber_memalloc( sizeof( MatchingRuleUse ) );
444 /* call-forward from MatchingRule to MatchingRuleUse */
446 /* copy static data to newly allocated struct */
448 /* append the struct pointer to the end of the list */
450 /* update the list head pointer */
451 mru_ptr = &LDAP_SLIST_NEXT(mru,smru_next);
458 int mr_usable_with_at(
462 if( mr->smr_usage & SLAP_MR_EXT && (
463 mr->smr_syntax == at->sat_syntax ||
464 mr == at->sat_equality || mr == at->sat_approx ) )
469 if ( mr->smr_compat_syntaxes ) {
471 for( i=0; mr->smr_compat_syntaxes[i]; i++ ) {
472 if( at->sat_syntax == mr->smr_compat_syntaxes[i] ) {
480 int mr_schema_info( Entry *e )
482 AttributeDescription *ad_matchingRules = slap_schema.si_ad_matchingRules;
486 LDAP_SLIST_FOREACH(mr, &mr_list, smr_next ) {
487 if ( mr->smr_usage & SLAP_MR_HIDE ) {
488 /* skip hidden rules */
492 if ( ! mr->smr_match ) {
493 /* skip rules without matching functions */
497 if ( mr->smr_str.bv_val == NULL ) {
498 if ( ldap_matchingrule2bv( &mr->smr_mrule, &mr->smr_str ) == NULL ) {
503 Debug( LDAP_DEBUG_TRACE, "Merging mr [%lu] %s\n",
504 mr->smr_str.bv_len, mr->smr_str.bv_val, 0 );
507 nval.bv_val = mr->smr_oid;
508 nval.bv_len = strlen(mr->smr_oid);
509 if( attr_merge_one( e, ad_matchingRules, &mr->smr_str, &nval ) ) {
516 int mru_schema_info( Entry *e )
518 AttributeDescription *ad_matchingRuleUse
519 = slap_schema.si_ad_matchingRuleUse;
520 MatchingRuleUse *mru;
523 LDAP_SLIST_FOREACH( mru, &mru_list, smru_next ) {
524 assert( !( mru->smru_usage & SLAP_MR_HIDE ) );
526 if ( mru->smru_str.bv_val == NULL ) {
527 if ( ldap_matchingruleuse2bv( &mru->smru_mruleuse, &mru->smru_str )
534 Debug( LDAP_DEBUG_TRACE, "Merging mru [%lu] %s\n",
535 mru->smru_str.bv_len, mru->smru_str.bv_val, 0 );
538 nval.bv_val = mru->smru_oid;
539 nval.bv_len = strlen(mru->smru_oid);
540 if( attr_merge_one( e, ad_matchingRuleUse, &mru->smru_str, &nval ) ) {