version 1.5, 2003/01/20 19:21:17
|
version 1.27, 2010/04/13 20:18:06
|
Line 1
|
Line 1
|
/* cr.c - content rule routines */ |
/* cr.c - content rule routines */ |
/* $OpenLDAP: pkg/ldap/servers/slapd/cr.c,v 1.4 2003/01/03 19:20:54 kurt Exp $ */ |
/* $OpenLDAP: pkg/ldap/servers/slapd/cr.c,v 1.26 2009/01/21 23:40:26 kurt Exp $ */ |
/* |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
* Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved. |
* |
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
* Copyright 1998-2010 The OpenLDAP Foundation. |
|
* All rights reserved. |
|
* |
|
* Redistribution and use in source and binary forms, with or without |
|
* modification, are permitted only as authorized by the OpenLDAP |
|
* Public License. |
|
* |
|
* A copy of this license is available in the file LICENSE in the |
|
* top-level directory of the distribution or, alternatively, at |
|
* <http://www.OpenLDAP.org/license.html>. |
*/ |
*/ |
|
|
#include "portable.h" |
#include "portable.h" |
Line 14
|
Line 23
|
#include <ac/socket.h> |
#include <ac/socket.h> |
|
|
#include "slap.h" |
#include "slap.h" |
#include "ldap_pvt.h" |
|
|
|
#ifdef SLAP_EXTENDED_SCHEMA |
|
|
|
struct cindexrec { |
struct cindexrec { |
struct berval cir_name; |
struct berval cir_name; |
Line 24 struct cindexrec {
|
Line 30 struct cindexrec {
|
}; |
}; |
|
|
static Avlnode *cr_index = NULL; |
static Avlnode *cr_index = NULL; |
static LDAP_SLIST_HEAD(CRList, slap_content_rule) cr_list |
static LDAP_STAILQ_HEAD(CRList, ContentRule) cr_list |
= LDAP_SLIST_HEAD_INITIALIZER(&cr_list); |
= LDAP_STAILQ_HEAD_INITIALIZER(cr_list); |
|
|
static int |
static int |
cr_index_cmp( |
cr_index_cmp( |
Line 35 cr_index_cmp(
|
Line 41 cr_index_cmp(
|
const struct cindexrec *cir1 = v_cir1; |
const struct cindexrec *cir1 = v_cir1; |
const struct cindexrec *cir2 = v_cir2; |
const struct cindexrec *cir2 = v_cir2; |
int i = cir1->cir_name.bv_len - cir2->cir_name.bv_len; |
int i = cir1->cir_name.bv_len - cir2->cir_name.bv_len; |
if (i) |
if (i) return i; |
return i; |
|
return strcasecmp( cir1->cir_name.bv_val, cir2->cir_name.bv_val ); |
return strcasecmp( cir1->cir_name.bv_val, cir2->cir_name.bv_val ); |
} |
} |
|
|
Line 48 cr_index_name_cmp(
|
Line 53 cr_index_name_cmp(
|
const struct berval *name = v_name; |
const struct berval *name = v_name; |
const struct cindexrec *cir = v_cir; |
const struct cindexrec *cir = v_cir; |
int i = name->bv_len - cir->cir_name.bv_len; |
int i = name->bv_len - cir->cir_name.bv_len; |
if (i) |
if (i) return i; |
return i; |
|
return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len ); |
return strncasecmp( name->bv_val, cir->cir_name.bv_val, name->bv_len ); |
} |
} |
|
|
Line 78 cr_bvfind( struct berval *crname )
|
Line 82 cr_bvfind( struct berval *crname )
|
return( NULL ); |
return( NULL ); |
} |
} |
|
|
|
static int |
|
cr_destroy_one( ContentRule *c ) |
|
{ |
|
assert( c != NULL ); |
|
|
|
if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries); |
|
if (c->scr_required) ldap_memfree(c->scr_required); |
|
if (c->scr_allowed) ldap_memfree(c->scr_allowed); |
|
if (c->scr_precluded) ldap_memfree(c->scr_precluded); |
|
ldap_contentrule_free((LDAPContentRule *)c); |
|
|
|
return 0; |
|
} |
|
|
void |
void |
cr_destroy( void ) |
cr_destroy( void ) |
{ |
{ |
Line 85 cr_destroy( void )
|
Line 103 cr_destroy( void )
|
|
|
avl_free(cr_index, ldap_memfree); |
avl_free(cr_index, ldap_memfree); |
|
|
while( !LDAP_SLIST_EMPTY(&cr_list) ) { |
while( !LDAP_STAILQ_EMPTY(&cr_list) ) { |
c = LDAP_SLIST_FIRST(&cr_list); |
c = LDAP_STAILQ_FIRST(&cr_list); |
LDAP_SLIST_REMOVE_HEAD(&cr_list, scr_next); |
LDAP_STAILQ_REMOVE_HEAD(&cr_list, scr_next); |
|
|
if (c->scr_auxiliaries) ldap_memfree(c->scr_auxiliaries); |
cr_destroy_one( c ); |
if (c->scr_required) ldap_memfree(c->scr_required); |
|
if (c->scr_allowed) ldap_memfree(c->scr_allowed); |
|
if (c->scr_precluded) ldap_memfree(c->scr_precluded); |
|
ldap_contentrule_free((LDAPContentRule *)c); |
|
} |
} |
} |
} |
|
|
Line 106 cr_insert(
|
Line 120 cr_insert(
|
struct cindexrec *cir; |
struct cindexrec *cir; |
char **names; |
char **names; |
|
|
LDAP_SLIST_INSERT_HEAD(&cr_list, scr, scr_next); |
|
|
|
if ( scr->scr_oid ) { |
if ( scr->scr_oid ) { |
cir = (struct cindexrec *) |
cir = (struct cindexrec *) |
ch_calloc( 1, sizeof(struct cindexrec) ); |
ch_calloc( 1, sizeof(struct cindexrec) ); |
Line 115 cr_insert(
|
Line 127 cr_insert(
|
cir->cir_name.bv_len = strlen( scr->scr_oid ); |
cir->cir_name.bv_len = strlen( scr->scr_oid ); |
cir->cir_cr = scr; |
cir->cir_cr = scr; |
|
|
assert( cir->cir_name.bv_val ); |
assert( cir->cir_name.bv_val != NULL ); |
assert( cir->cir_cr ); |
assert( cir->cir_cr != NULL ); |
|
|
if ( avl_insert( &cr_index, (caddr_t) cir, |
if ( avl_insert( &cr_index, (caddr_t) cir, |
cr_index_cmp, avl_dup_error ) ) |
cr_index_cmp, avl_dup_error ) ) |
Line 138 cr_insert(
|
Line 150 cr_insert(
|
cir->cir_name.bv_len = strlen( *names ); |
cir->cir_name.bv_len = strlen( *names ); |
cir->cir_cr = scr; |
cir->cir_cr = scr; |
|
|
assert( cir->cir_name.bv_val ); |
assert( cir->cir_name.bv_val != NULL ); |
assert( cir->cir_cr ); |
assert( cir->cir_cr != NULL ); |
|
|
if ( avl_insert( &cr_index, (caddr_t) cir, |
if ( avl_insert( &cr_index, (caddr_t) cir, |
cr_index_cmp, avl_dup_error ) ) |
cr_index_cmp, avl_dup_error ) ) |
Line 156 cr_insert(
|
Line 168 cr_insert(
|
} |
} |
} |
} |
|
|
|
LDAP_STAILQ_INSERT_TAIL(&cr_list, scr, scr_next); |
|
|
return 0; |
return 0; |
} |
} |
|
|
Line 183 cr_add_auxiliaries(
|
Line 197 cr_add_auxiliaries(
|
return SLAP_SCHERR_CLASS_NOT_FOUND; |
return SLAP_SCHERR_CLASS_NOT_FOUND; |
} |
} |
|
|
if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++; |
if( soc->soc_flags & SLAP_OC_OPERATIONAL && |
|
soc != slap_schema.si_oc_extensibleObject ) |
|
{ |
|
(*op)++; |
|
} |
|
|
if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) { |
if( soc->soc_kind != LDAP_SCHEMA_AUXILIARY ) { |
*err = scr->scr_oc_oids_aux[naux]; |
*err = scr->scr_oc_oids_aux[naux]; |
Line 192 cr_add_auxiliaries(
|
Line 210 cr_add_auxiliaries(
|
} |
} |
|
|
scr->scr_auxiliaries[naux] = NULL; |
scr->scr_auxiliaries[naux] = NULL; |
|
|
return 0; |
return 0; |
} |
} |
|
|
Line 316 int
|
Line 333 int
|
cr_add( |
cr_add( |
LDAPContentRule *cr, |
LDAPContentRule *cr, |
int user, |
int user, |
|
ContentRule **rscr, |
const char **err |
const char **err |
) |
) |
{ |
{ |
ContentRule *scr; |
ContentRule *scr; |
int code; |
int code; |
int op = 0; |
int op = 0; |
|
char *oidm = NULL; |
|
|
if ( cr->cr_names != NULL ) { |
if ( cr->cr_names != NULL ) { |
int i; |
int i; |
Line 341 cr_add(
|
Line 360 cr_add(
|
return SLAP_SCHERR_OIDM; |
return SLAP_SCHERR_OIDM; |
} |
} |
if ( oid != cr->cr_oid ) { |
if ( oid != cr->cr_oid ) { |
ldap_memfree( cr->cr_oid ); |
oidm = cr->cr_oid; |
cr->cr_oid = oid; |
cr->cr_oid = oid; |
} |
} |
} |
} |
Line 349 cr_add(
|
Line 368 cr_add(
|
scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) ); |
scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) ); |
AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) ); |
AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) ); |
|
|
|
scr->scr_oidmacro = oidm; |
scr->scr_sclass = oc_find(cr->cr_oid); |
scr->scr_sclass = oc_find(cr->cr_oid); |
if ( !scr->scr_sclass ) { |
if ( !scr->scr_sclass ) { |
*err = cr->cr_oid; |
*err = cr->cr_oid; |
return SLAP_SCHERR_CLASS_NOT_FOUND; |
code = SLAP_SCHERR_CLASS_NOT_FOUND; |
|
goto fail; |
} |
} |
|
|
/* check object class usage */ |
/* check object class usage */ |
if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL ) |
if( scr->scr_sclass->soc_kind != LDAP_SCHEMA_STRUCTURAL ) |
{ |
{ |
*err = cr->cr_oid; |
*err = cr->cr_oid; |
return SLAP_SCHERR_CR_BAD_STRUCT; |
code = SLAP_SCHERR_CR_BAD_STRUCT; |
|
goto fail; |
} |
} |
|
|
if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++; |
if( scr->scr_sclass->soc_flags & SLAP_OC_OPERATIONAL ) op++; |
|
|
code = cr_add_auxiliaries( scr, &op, err ); |
code = cr_add_auxiliaries( scr, &op, err ); |
if ( code != 0 ) return code; |
if ( code != 0 ) goto fail; |
|
|
code = cr_create_required( scr, &op, err ); |
code = cr_create_required( scr, &op, err ); |
if ( code != 0 ) return code; |
if ( code != 0 ) goto fail; |
|
|
code = cr_create_allowed( scr, &op, err ); |
code = cr_create_allowed( scr, &op, err ); |
if ( code != 0 ) return code; |
if ( code != 0 ) goto fail; |
|
|
code = cr_create_precluded( scr, &op, err ); |
code = cr_create_precluded( scr, &op, err ); |
if ( code != 0 ) return code; |
if ( code != 0 ) goto fail; |
|
|
if( user && op ) return SLAP_SCHERR_CR_BAD_AUX; |
if( user && op ) { |
|
code = SLAP_SCHERR_CR_BAD_AUX; |
|
goto fail; |
|
} |
|
|
code = cr_insert(scr,err); |
code = cr_insert(scr,err); |
|
if ( code == 0 && rscr ) |
|
*rscr = scr; |
|
return code; |
|
fail: |
|
ch_free( scr ); |
return code; |
return code; |
} |
} |
|
|
#endif |
void |
|
cr_unparse( BerVarray *res, ContentRule *start, ContentRule *end, int sys ) |
|
{ |
|
ContentRule *cr; |
|
int i, num; |
|
struct berval bv, *bva = NULL, idx; |
|
char ibuf[32]; |
|
|
|
if ( !start ) |
|
start = LDAP_STAILQ_FIRST( &cr_list ); |
|
|
|
/* count the result size */ |
|
i = 0; |
|
for ( cr=start; cr; cr=LDAP_STAILQ_NEXT(cr, scr_next)) { |
|
if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue; |
|
i++; |
|
if ( cr == end ) break; |
|
} |
|
if (!i) return; |
|
|
|
num = i; |
|
bva = ch_malloc( (num+1) * sizeof(struct berval) ); |
|
BER_BVZERO( bva ); |
|
idx.bv_val = ibuf; |
|
if ( sys ) { |
|
idx.bv_len = 0; |
|
ibuf[0] = '\0'; |
|
} |
|
i = 0; |
|
for ( cr=start; cr; cr=LDAP_STAILQ_NEXT(cr, scr_next)) { |
|
LDAPContentRule lcr, *lcrp; |
|
if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue; |
|
if ( cr->scr_oidmacro ) { |
|
lcr = cr->scr_crule; |
|
lcr.cr_oid = cr->scr_oidmacro; |
|
lcrp = &lcr; |
|
} else { |
|
lcrp = &cr->scr_crule; |
|
} |
|
if ( ldap_contentrule2bv( lcrp, &bv ) == NULL ) { |
|
ber_bvarray_free( bva ); |
|
} |
|
if ( !sys ) { |
|
idx.bv_len = sprintf(idx.bv_val, "{%d}", i); |
|
} |
|
bva[i].bv_len = idx.bv_len + bv.bv_len; |
|
bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 ); |
|
strcpy( bva[i].bv_val, ibuf ); |
|
strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val ); |
|
i++; |
|
bva[i].bv_val = NULL; |
|
ldap_memfree( bv.bv_val ); |
|
if ( cr == end ) break; |
|
} |
|
*res = bva; |
|
} |
|
|
int |
int |
cr_schema_info( Entry *e ) |
cr_schema_info( Entry *e ) |
{ |
{ |
#ifdef SLAP_EXTENDED_SCHEMA |
AttributeDescription *ad_ditContentRules |
struct berval vals[2]; |
= slap_schema.si_ad_ditContentRules; |
ContentRule *cr; |
ContentRule *cr; |
|
|
AttributeDescription *ad_ditContentRules = slap_schema.si_ad_ditContentRules; |
struct berval val; |
|
struct berval nval; |
|
|
vals[1].bv_val = NULL; |
LDAP_STAILQ_FOREACH(cr, &cr_list, scr_next) { |
|
if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) { |
LDAP_SLIST_FOREACH(cr, &cr_list, scr_next) { |
|
if ( ldap_contentrule2bv( &cr->scr_crule, vals ) == NULL ) { |
|
return -1; |
return -1; |
} |
} |
|
|
#if 0 |
#if 0 |
if( cr->scr_flags & SLAP_CR_HIDE ) continue; |
if( cr->scr_flags & SLAP_CR_HIDE ) continue; |
#endif |
#endif |
|
|
#if 0 |
#if 0 |
Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n", |
Debug( LDAP_DEBUG_TRACE, "Merging cr [%ld] %s\n", |
(long) vals[0].bv_len, vals[0].bv_val, 0 ); |
(long) val.bv_len, val.bv_val, 0 ); |
#endif |
#endif |
if( attr_merge( e, ad_ditContentRules, vals ) ) |
|
|
nval.bv_val = cr->scr_oid; |
|
nval.bv_len = strlen(cr->scr_oid); |
|
|
|
if( attr_merge_one( e, ad_ditContentRules, &val, &nval ) ) |
|
{ |
return -1; |
return -1; |
ldap_memfree( vals[0].bv_val ); |
} |
|
ldap_memfree( val.bv_val ); |
} |
} |
#endif |
|
return 0; |
return 0; |
} |
} |