openldap/servers/slapd/schema_check.c

239 lines
5.4 KiB
C
Raw Normal View History

/* schema_check.c - routines to enforce schema definitions */
/* $OpenLDAP$ */
/*
2000-05-13 10:47:56 +08:00
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
* 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"
static char * oc_check_required(Entry *e, struct berval *ocname);
/*
* entry_schema_check - check that entry e conforms to the schema required
* by its object class(es).
*
* returns 0 if so, non-zero otherwise.
*/
int
entry_schema_check(
Entry *e, Attribute *oldattrs, const char** text )
{
Attribute *a, *aoc;
ObjectClass *oc;
int i;
int ret;
AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
2000-06-01 02:46:13 +08:00
int extensible = 0;
if( !global_schemacheck ) return LDAP_SUCCESS;
/* find the object class attribute - could error out here */
if ( (aoc = attr_find( e->e_attrs, ad_objectClass )) == NULL ) {
2001-01-16 03:17:29 +08:00
#ifdef NEW_LOGGING
2001-01-18 00:35:53 +08:00
LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
"entry_schema_check: No objectClass for entry (%s).\n", e->e_dn ));
2001-01-16 03:17:29 +08:00
#else
2001-01-17 10:51:05 +08:00
Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
e->e_dn, 0, 0 );
2001-01-16 03:17:29 +08:00
#endif
*text = "no objectClass attribute";
return LDAP_OBJECT_CLASS_VIOLATION;
}
/* check that the entry has required attrs for each oc */
for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
2001-01-16 03:17:29 +08:00
#ifdef NEW_LOGGING
2001-01-18 00:35:53 +08:00
LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
"entry_schema_check: dn (%s), objectClass \"%s\" not recognized\n",
e->e_dn, aoc->a_vals[i]->bv_val ));
2001-01-16 03:17:29 +08:00
#else
Debug( LDAP_DEBUG_ANY,
2001-01-17 10:51:05 +08:00
"entry_check_schema(%s): objectClass \"%s\" not recognized\n",
e->e_dn, aoc->a_vals[i]->bv_val, 0 );
2001-01-16 03:17:29 +08:00
#endif
*text = "unrecognized object class";
return LDAP_OBJECT_CLASS_VIOLATION;
} else {
char *s = oc_check_required( e, aoc->a_vals[i] );
if (s != NULL) {
2001-01-16 03:17:29 +08:00
#ifdef NEW_LOGGING
2001-01-18 00:35:53 +08:00
LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
"entry_schema_check: dn (%s) oc \"%s\" requires att \"%s\"\n",
e->e_dn, aoc->a_vals[i]->bv_val, s ));
2001-01-16 03:17:29 +08:00
#else
Debug( LDAP_DEBUG_ANY,
"Entry (%s), oc \"%s\" requires attr \"%s\"\n",
e->e_dn, aoc->a_vals[i]->bv_val, s );
2001-01-16 03:17:29 +08:00
#endif
*text = "missing required attribute";
return LDAP_OBJECT_CLASS_VIOLATION;
}
2000-06-01 02:46:13 +08:00
if( oc == slap_schema.si_oc_extensibleObject ) {
2000-06-01 02:46:13 +08:00
extensible=1;
}
}
}
2000-06-01 02:46:13 +08:00
if( extensible ) {
return LDAP_SUCCESS;
}
2001-01-17 10:48:55 +08:00
/* optimistic */
ret = LDAP_SUCCESS;
/* check that each attr in the entry is allowed by some oc */
for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
if ( ret != 0 ) {
char *type = a->a_desc->ad_cname->bv_val;
2001-01-16 03:17:29 +08:00
#ifdef NEW_LOGGING
2001-01-18 00:35:53 +08:00
LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
"entry_schema_check: Entry (%s) attr \"%s\" not allowed.\n",
e->e_dn, type ));
2001-01-16 03:17:29 +08:00
#else
Debug( LDAP_DEBUG_ANY,
"Entry (%s), attr \"%s\" not allowed\n",
e->e_dn, type, 0 );
2001-01-16 03:17:29 +08:00
#endif
*text = "attribute not allowed";
break;
}
}
return( ret );
}
static char *
oc_check_required( Entry *e, struct berval *ocname )
{
ObjectClass *oc;
AttributeType *at;
int i;
Attribute *a;
2001-01-16 03:17:29 +08:00
#ifdef NEW_LOGGING
2001-01-18 00:35:53 +08:00
LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
"oc_check_required: dn (%s), objectClass \"%s\"\n",
e->e_dn, ocname->bv_val ));
2001-01-16 03:17:29 +08:00
#else
Debug( LDAP_DEBUG_TRACE,
2001-01-17 10:51:05 +08:00
"oc_check_required entry (%s), objectClass \"%s\"\n",
e->e_dn, ocname->bv_val, 0 );
2001-01-16 03:17:29 +08:00
#endif
/* find global oc defn. it we don't know about it assume it's ok */
if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
return NULL;
}
/* check for empty oc_required */
if(oc->soc_required == NULL) {
return NULL;
}
/* for each required attribute */
for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
at = oc->soc_required[i];
/* see if it's in the entry */
for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
if( a->a_desc->ad_type == at ) {
break;
}
}
/* not there => schema violation */
if ( a == NULL ) {
return at->sat_cname;
}
}
return( NULL );
}
int oc_check_allowed(
AttributeType *at,
struct berval **ocl )
{
ObjectClass *oc;
int i, j;
2001-01-16 03:17:29 +08:00
#ifdef NEW_LOGGING
2001-01-18 00:35:53 +08:00
LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
"oc_check_allowed: type \"%s\"\n", at->sat_cname ));
2001-01-16 03:17:29 +08:00
#else
Debug( LDAP_DEBUG_TRACE,
"oc_check_allowed type \"%s\"\n",
at->sat_cname, 0, 0 );
2001-01-16 03:17:29 +08:00
#endif
2001-01-17 10:51:05 +08:00
/* always allow objectClass attribute */
if ( strcasecmp( at->sat_cname, "objectClass" ) == 0 ) {
return LDAP_SUCCESS;
}
/*
* All operational attributions are allowed by schema rules.
*/
if( is_at_operational(at) ) {
return LDAP_SUCCESS;
}
/* check that the type appears as req or opt in at least one oc */
for ( i = 0; ocl[i] != NULL; i++ ) {
/* if we know about the oc */
if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
/* does it require the type? */
for ( j = 0; oc->soc_required != NULL &&
oc->soc_required[j] != NULL; j++ )
{
if( at == oc->soc_required[j] ) {
return LDAP_SUCCESS;
}
}
/* does it allow the type? */
for ( j = 0; oc->soc_allowed != NULL &&
oc->soc_allowed[j] != NULL; j++ )
{
if( at == oc->soc_allowed[j] ) {
return LDAP_SUCCESS;
}
}
/* maybe the next oc allows it */
#ifdef OC_UNDEFINED_IMPLES_EXTENSIBLE
/* we don't know about the oc. assume it allows it */
} else {
if ( t != type )
ldap_memfree( t );
return LDAP_SUCCESS;
#endif
}
}
/* not allowed by any oc */
return LDAP_OBJECT_CLASS_VIOLATION;
}