2000-02-05 13:01:41 +08:00
|
|
|
/* mr.c - routines to manage matching rule definitions */
|
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/*
|
2002-01-05 05:17:25 +08:00
|
|
|
* Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
|
2000-02-05 13:01:41 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
|
|
|
#include "slap.h"
|
|
|
|
#include "ldap_pvt.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct mindexrec {
|
2001-12-28 19:45:25 +08:00
|
|
|
struct berval mir_name;
|
2000-02-05 13:01:41 +08:00
|
|
|
MatchingRule *mir_mr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Avlnode *mr_index = NULL;
|
|
|
|
static MatchingRule *mr_list = NULL;
|
|
|
|
|
|
|
|
static int
|
|
|
|
mr_index_cmp(
|
|
|
|
struct mindexrec *mir1,
|
|
|
|
struct mindexrec *mir2
|
|
|
|
)
|
|
|
|
{
|
2001-12-28 19:45:25 +08:00
|
|
|
int i = mir1->mir_name.bv_len - mir2->mir_name.bv_len;
|
|
|
|
if (i) return i;
|
|
|
|
return (strcmp( mir1->mir_name.bv_val, mir2->mir_name.bv_val ));
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mr_index_name_cmp(
|
2001-12-28 19:45:25 +08:00
|
|
|
struct berval *name,
|
2000-02-05 13:01:41 +08:00
|
|
|
struct mindexrec *mir
|
|
|
|
)
|
|
|
|
{
|
2001-12-28 19:45:25 +08:00
|
|
|
int i = name->bv_len - mir->mir_name.bv_len;
|
|
|
|
if (i) return i;
|
|
|
|
return (strncmp( name->bv_val, mir->mir_name.bv_val, name->bv_len ));
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MatchingRule *
|
|
|
|
mr_find( const char *mrname )
|
2001-12-28 19:45:25 +08:00
|
|
|
{
|
|
|
|
struct berval bv;
|
|
|
|
|
2001-12-29 10:56:27 +08:00
|
|
|
bv.bv_val = (char *)mrname;
|
2001-12-28 19:45:25 +08:00
|
|
|
bv.bv_len = strlen( mrname );
|
|
|
|
return mr_bvfind( &bv );
|
|
|
|
}
|
|
|
|
|
|
|
|
MatchingRule *
|
|
|
|
mr_bvfind( struct berval *mrname )
|
2000-02-05 13:01:41 +08:00
|
|
|
{
|
|
|
|
struct mindexrec *mir = NULL;
|
|
|
|
|
|
|
|
if ( (mir = (struct mindexrec *) avl_find( mr_index, mrname,
|
2001-01-18 00:35:53 +08:00
|
|
|
(AVL_CMP) mr_index_name_cmp )) != NULL ) {
|
2000-02-05 13:01:41 +08:00
|
|
|
return( mir->mir_mr );
|
|
|
|
}
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2001-11-07 09:03:49 +08:00
|
|
|
void
|
|
|
|
mr_destroy( void )
|
|
|
|
{
|
|
|
|
MatchingRule *m, *n;
|
|
|
|
|
|
|
|
avl_free(mr_index, ldap_memfree);
|
|
|
|
for (m=mr_list; m; m=n) {
|
|
|
|
n = m->smr_next;
|
|
|
|
ldap_matchingrule_free((LDAPMatchingRule *)m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-05 13:01:41 +08:00
|
|
|
static int
|
|
|
|
mr_insert(
|
|
|
|
MatchingRule *smr,
|
|
|
|
const char **err
|
|
|
|
)
|
|
|
|
{
|
|
|
|
MatchingRule **mrp;
|
|
|
|
struct mindexrec *mir;
|
|
|
|
char **names;
|
|
|
|
|
|
|
|
mrp = &mr_list;
|
|
|
|
while ( *mrp != NULL ) {
|
|
|
|
mrp = &(*mrp)->smr_next;
|
|
|
|
}
|
|
|
|
*mrp = smr;
|
|
|
|
|
|
|
|
if ( smr->smr_oid ) {
|
|
|
|
mir = (struct mindexrec *)
|
|
|
|
ch_calloc( 1, sizeof(struct mindexrec) );
|
2001-12-28 19:45:25 +08:00
|
|
|
mir->mir_name.bv_val = smr->smr_oid;
|
|
|
|
mir->mir_name.bv_len = strlen( smr->smr_oid );
|
2000-02-05 13:01:41 +08:00
|
|
|
mir->mir_mr = smr;
|
|
|
|
if ( avl_insert( &mr_index, (caddr_t) mir,
|
|
|
|
(AVL_CMP) mr_index_cmp,
|
|
|
|
(AVL_DUP) avl_dup_error ) ) {
|
|
|
|
*err = smr->smr_oid;
|
|
|
|
ldap_memfree(mir);
|
|
|
|
return SLAP_SCHERR_DUP_RULE;
|
|
|
|
}
|
|
|
|
/* FIX: temporal consistency check */
|
2001-12-28 19:45:25 +08:00
|
|
|
mr_bvfind(&mir->mir_name);
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
if ( (names = smr->smr_names) ) {
|
|
|
|
while ( *names ) {
|
|
|
|
mir = (struct mindexrec *)
|
|
|
|
ch_calloc( 1, sizeof(struct mindexrec) );
|
2001-12-28 19:45:25 +08:00
|
|
|
mir->mir_name.bv_val = *names;
|
|
|
|
mir->mir_name.bv_len = strlen( *names );
|
2000-02-05 13:01:41 +08:00
|
|
|
mir->mir_mr = smr;
|
|
|
|
if ( avl_insert( &mr_index, (caddr_t) mir,
|
|
|
|
(AVL_CMP) mr_index_cmp,
|
|
|
|
(AVL_DUP) avl_dup_error ) ) {
|
|
|
|
*err = *names;
|
|
|
|
ldap_memfree(mir);
|
|
|
|
return SLAP_SCHERR_DUP_RULE;
|
|
|
|
}
|
|
|
|
/* FIX: temporal consistency check */
|
2001-12-28 19:45:25 +08:00
|
|
|
mr_bvfind(&mir->mir_name);
|
2000-02-05 13:01:41 +08:00
|
|
|
names++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
mr_add(
|
2000-07-23 01:30:44 +08:00
|
|
|
LDAPMatchingRule *mr,
|
2000-02-15 04:57:34 +08:00
|
|
|
unsigned usage,
|
2000-02-05 13:01:41 +08:00
|
|
|
slap_mr_convert_func *convert,
|
|
|
|
slap_mr_normalize_func *normalize,
|
|
|
|
slap_mr_match_func *match,
|
|
|
|
slap_mr_indexer_func *indexer,
|
|
|
|
slap_mr_filter_func *filter,
|
2000-07-25 05:29:30 +08:00
|
|
|
MatchingRule *amr,
|
2000-02-05 13:01:41 +08:00
|
|
|
const char **err
|
|
|
|
)
|
|
|
|
{
|
|
|
|
MatchingRule *smr;
|
|
|
|
Syntax *syn;
|
|
|
|
int code;
|
|
|
|
|
|
|
|
smr = (MatchingRule *) ch_calloc( 1, sizeof(MatchingRule) );
|
2000-07-28 09:07:07 +08:00
|
|
|
AC_MEMCPY( &smr->smr_mrule, mr, sizeof(LDAPMatchingRule));
|
2000-02-05 13:01:41 +08:00
|
|
|
|
2001-12-26 22:36:02 +08:00
|
|
|
smr->smr_oidlen = strlen( mr->mr_oid );
|
2000-02-15 04:57:34 +08:00
|
|
|
smr->smr_usage = usage;
|
2000-02-05 13:01:41 +08:00
|
|
|
smr->smr_convert = convert;
|
|
|
|
smr->smr_normalize = normalize;
|
|
|
|
smr->smr_match = match;
|
|
|
|
smr->smr_indexer = indexer;
|
|
|
|
smr->smr_filter = filter;
|
2000-07-25 05:29:30 +08:00
|
|
|
smr->smr_associated = amr;
|
2000-02-05 13:01:41 +08:00
|
|
|
|
|
|
|
if ( smr->smr_syntax_oid ) {
|
|
|
|
if ( (syn = syn_find(smr->smr_syntax_oid)) ) {
|
|
|
|
smr->smr_syntax = syn;
|
|
|
|
} else {
|
|
|
|
*err = smr->smr_syntax_oid;
|
|
|
|
return SLAP_SCHERR_SYN_NOT_FOUND;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*err = "";
|
|
|
|
return SLAP_SCHERR_MR_INCOMPLETE;
|
|
|
|
}
|
|
|
|
code = mr_insert(smr,err);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
register_matching_rule(
|
2001-12-20 06:08:24 +08:00
|
|
|
const char * desc,
|
2000-02-15 04:57:34 +08:00
|
|
|
unsigned usage,
|
2000-02-05 13:01:41 +08:00
|
|
|
slap_mr_convert_func *convert,
|
|
|
|
slap_mr_normalize_func *normalize,
|
|
|
|
slap_mr_match_func *match,
|
|
|
|
slap_mr_indexer_func *indexer,
|
2000-07-25 05:29:30 +08:00
|
|
|
slap_mr_filter_func *filter,
|
|
|
|
const char* associated )
|
2000-02-05 13:01:41 +08:00
|
|
|
{
|
2000-07-23 01:30:44 +08:00
|
|
|
LDAPMatchingRule *mr;
|
2000-07-25 05:29:30 +08:00
|
|
|
MatchingRule *amr = NULL;
|
2000-02-05 13:01:41 +08:00
|
|
|
int code;
|
|
|
|
const char *err;
|
|
|
|
|
2000-02-15 04:57:34 +08:00
|
|
|
if( usage == SLAP_MR_NONE ) {
|
2001-01-16 03:17:29 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2001-01-18 00:35:53 +08:00
|
|
|
LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
|
|
|
|
"register_matching_rule: %s not usable\n", desc ));
|
2001-01-16 03:17:29 +08:00
|
|
|
#else
|
2000-02-15 04:57:34 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "register_matching_rule: not usable %s\n",
|
|
|
|
desc, 0, 0 );
|
2001-01-16 03:17:29 +08:00
|
|
|
#endif
|
|
|
|
|
2000-02-15 04:57:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-07-25 05:29:30 +08:00
|
|
|
if( associated != NULL ) {
|
|
|
|
amr = mr_find( associated );
|
|
|
|
|
2000-07-25 05:40:58 +08:00
|
|
|
#if 0
|
|
|
|
/* ignore for now */
|
|
|
|
|
|
|
|
if( amr == NULL ) {
|
2001-01-16 03:17:29 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2001-01-18 00:35:53 +08:00
|
|
|
LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
|
|
|
|
"register_matching_rule: could not locate associated matching rule %s for %s\n",
|
|
|
|
associated, desc ));
|
2001-01-16 03:17:29 +08:00
|
|
|
#else
|
2000-07-25 05:40:58 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "register_matching_rule: could not locate "
|
|
|
|
"associated matching rule %s for %s\n",
|
|
|
|
associated, desc, 0 );
|
2001-01-16 03:17:29 +08:00
|
|
|
#endif
|
|
|
|
|
2000-07-25 05:40:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
2000-07-25 05:29:30 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-05-29 06:45:56 +08:00
|
|
|
mr = ldap_str2matchingrule( desc, &code, &err, LDAP_SCHEMA_ALLOW_ALL);
|
2000-02-05 13:01:41 +08:00
|
|
|
if ( !mr ) {
|
2001-01-16 03:17:29 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2001-01-18 00:35:53 +08:00
|
|
|
LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
|
|
|
|
"register_matching_rule: %s before %s in %s.\n",
|
|
|
|
ldap_scherr2str(code), err, desc ));
|
2001-01-16 03:17:29 +08:00
|
|
|
#else
|
2000-02-05 13:01:41 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s before %s in %s\n",
|
|
|
|
ldap_scherr2str(code), err, desc );
|
2001-01-16 03:17:29 +08:00
|
|
|
#endif
|
|
|
|
|
2000-02-05 13:01:41 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
2000-05-29 07:51:39 +08:00
|
|
|
code = mr_add( mr, usage,
|
2000-07-25 05:29:30 +08:00
|
|
|
convert, normalize, match, indexer, filter, amr,
|
2000-05-29 07:51:39 +08:00
|
|
|
&err );
|
|
|
|
|
2000-07-22 01:25:41 +08:00
|
|
|
ldap_memfree( mr );
|
|
|
|
|
2000-02-05 13:01:41 +08:00
|
|
|
if ( code ) {
|
2001-01-16 03:17:29 +08:00
|
|
|
#ifdef NEW_LOGGING
|
2001-01-18 00:35:53 +08:00
|
|
|
LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
|
|
|
|
"register_matching_rule: %s for %s in %s.\n",
|
|
|
|
scherr2str(code), err, desc ));
|
2001-01-16 03:17:29 +08:00
|
|
|
#else
|
2000-10-10 03:14:44 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "Error in register_matching_rule: %s for %s in %s\n",
|
2000-02-05 13:01:41 +08:00
|
|
|
scherr2str(code), err, desc );
|
2001-01-16 03:17:29 +08:00
|
|
|
#endif
|
|
|
|
|
2000-02-05 13:01:41 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
2000-05-29 07:51:39 +08:00
|
|
|
|
2000-02-05 13:01:41 +08:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if defined( SLAPD_SCHEMA_DN )
|
|
|
|
|
|
|
|
int mr_schema_info( Entry *e )
|
|
|
|
{
|
2002-01-02 19:00:36 +08:00
|
|
|
struct berval vals[2];
|
2000-02-05 13:01:41 +08:00
|
|
|
MatchingRule *mr;
|
|
|
|
|
2000-05-16 07:36:37 +08:00
|
|
|
AttributeDescription *ad_matchingRules = slap_schema.si_ad_matchingRules;
|
|
|
|
|
2002-01-02 19:00:36 +08:00
|
|
|
vals[1].bv_val = NULL;
|
2000-02-05 13:01:41 +08:00
|
|
|
|
|
|
|
for ( mr = mr_list; mr; mr = mr->smr_next ) {
|
2002-01-06 13:29:04 +08:00
|
|
|
if ( mr->smr_usage & SLAP_MR_HIDE ) {
|
|
|
|
/* skip hidden rules */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2000-10-17 07:47:46 +08:00
|
|
|
if ( ! mr->smr_match ) {
|
|
|
|
/* skip rules without matching functions */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-01-02 19:00:36 +08:00
|
|
|
if ( ldap_matchingrule2bv( &mr->smr_mrule, vals ) == NULL ) {
|
2000-02-05 13:01:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2000-05-24 08:59:58 +08:00
|
|
|
#if 0
|
2000-05-16 07:36:37 +08:00
|
|
|
Debug( LDAP_DEBUG_TRACE, "Merging mr [%ld] %s\n",
|
2002-01-02 19:00:36 +08:00
|
|
|
(long) vals[0].bv_len, vals[0].bv_val, 0 );
|
2000-05-24 08:59:58 +08:00
|
|
|
#endif
|
2000-05-16 07:36:37 +08:00
|
|
|
attr_merge( e, ad_matchingRules, vals );
|
2002-01-02 19:00:36 +08:00
|
|
|
ldap_memfree( vals[0].bv_val );
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|