2000-02-05 13:01:41 +08:00
|
|
|
/* 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.
|
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"
|
|
|
|
|
2000-05-29 00:36:34 +08:00
|
|
|
#ifndef SLAPD_SCHEMA_NOT_COMPAT
|
2000-02-07 05:09:44 +08:00
|
|
|
static int oc_check_allowed(char *type, struct berval **oclist);
|
|
|
|
#endif
|
|
|
|
static char * oc_check_required(Entry *e, struct berval *ocname);
|
2000-02-05 13:01:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2000-04-25 21:15:55 +08:00
|
|
|
entry_schema_check(
|
2000-05-22 11:46:57 +08:00
|
|
|
Entry *e, Attribute *oldattrs, const char** text )
|
2000-02-05 13:01:41 +08:00
|
|
|
{
|
|
|
|
Attribute *a, *aoc;
|
|
|
|
ObjectClass *oc;
|
|
|
|
int i;
|
2000-04-25 21:15:55 +08:00
|
|
|
int ret;
|
2000-02-15 04:57:34 +08:00
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
2000-05-16 07:36:37 +08:00
|
|
|
AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
|
2000-02-15 04:57:34 +08:00
|
|
|
#else
|
2000-05-16 10:17:59 +08:00
|
|
|
static const char *ad_objectClass = "objectclass";
|
2000-02-15 04:57:34 +08:00
|
|
|
#endif
|
2000-06-01 02:46:13 +08:00
|
|
|
int extensible = 0;
|
2000-02-15 04:57:34 +08:00
|
|
|
|
2000-04-25 21:15:55 +08:00
|
|
|
if( !global_schemacheck ) return LDAP_SUCCESS;
|
2000-02-05 13:01:41 +08:00
|
|
|
|
|
|
|
/* find the object class attribute - could error out here */
|
2000-05-16 07:36:37 +08:00
|
|
|
if ( (aoc = attr_find( e->e_attrs, ad_objectClass )) == NULL ) {
|
2000-02-05 13:01:41 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY, "No object class for entry (%s)\n",
|
|
|
|
e->e_dn, 0, 0 );
|
2000-04-25 21:15:55 +08:00
|
|
|
*text = "no objectclass attribute";
|
|
|
|
return oldattrs != NULL
|
|
|
|
? LDAP_OBJECT_CLASS_VIOLATION
|
|
|
|
: LDAP_NO_OBJECT_CLASS_MODS;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
|
2000-04-25 21:15:55 +08:00
|
|
|
ret = LDAP_SUCCESS;
|
|
|
|
|
2000-02-05 13:01:41 +08:00
|
|
|
/* 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 ) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
2000-05-24 04:47:38 +08:00
|
|
|
"entry_check_schema(%s): objectclass \"%s\" not defined\n",
|
|
|
|
e->e_dn, aoc->a_vals[i]->bv_val, 0 );
|
2000-04-25 21:15:55 +08:00
|
|
|
|
|
|
|
} else {
|
2000-02-07 05:09:44 +08:00
|
|
|
char *s = oc_check_required( e, aoc->a_vals[i] );
|
2000-02-05 13:01:41 +08:00
|
|
|
|
|
|
|
if (s != NULL) {
|
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Entry (%s), oc \"%s\" requires attr \"%s\"\n",
|
|
|
|
e->e_dn, aoc->a_vals[i]->bv_val, s );
|
2000-04-25 21:15:55 +08:00
|
|
|
*text = "missing required attribute";
|
|
|
|
ret = LDAP_OBJECT_CLASS_VIOLATION;
|
|
|
|
break;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
2000-06-01 02:46:13 +08:00
|
|
|
|
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
|
|
|
if( oc == slap_schema.si_oc_extensibleObject )
|
|
|
|
#else
|
2000-06-04 12:34:21 +08:00
|
|
|
if( !strcmp( aoc->a_vals[i]->bv_val, "extensibleObject" ) == 0 )
|
2000-06-01 02:46:13 +08:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
extensible=1;
|
|
|
|
}
|
|
|
|
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-25 21:15:55 +08:00
|
|
|
if ( ret != LDAP_SUCCESS ) {
|
|
|
|
return ret;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
|
2000-06-01 02:46:13 +08:00
|
|
|
if( extensible ) {
|
|
|
|
return LDAP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2000-02-05 13:01:41 +08:00
|
|
|
/* check that each attr in the entry is allowed by some oc */
|
|
|
|
for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
|
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
2000-04-25 21:15:55 +08:00
|
|
|
ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
|
2000-02-05 13:01:41 +08:00
|
|
|
#else
|
2000-04-25 21:15:55 +08:00
|
|
|
ret = oc_check_allowed( a->a_type, aoc->a_vals );
|
|
|
|
#endif
|
|
|
|
if ( ret != 0 ) {
|
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
|
|
|
char *type = a->a_desc->ad_cname->bv_val;
|
|
|
|
#else
|
|
|
|
char *type = a->a_type;
|
|
|
|
#endif
|
2000-02-05 13:01:41 +08:00
|
|
|
Debug( LDAP_DEBUG_ANY,
|
|
|
|
"Entry (%s), attr \"%s\" not allowed\n",
|
2000-04-25 21:15:55 +08:00
|
|
|
e->e_dn, type, 0 );
|
|
|
|
*text = "attribute not allowed";
|
|
|
|
break;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2000-02-07 05:09:44 +08:00
|
|
|
oc_check_required( Entry *e, struct berval *ocname )
|
2000-02-05 13:01:41 +08:00
|
|
|
{
|
|
|
|
ObjectClass *oc;
|
|
|
|
AttributeType *at;
|
|
|
|
int i;
|
|
|
|
Attribute *a;
|
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"oc_check_required entry (%s), objectclass \"%s\"\n",
|
2000-05-24 04:33:40 +08:00
|
|
|
e->e_dn, ocname->bv_val, 0 );
|
2000-02-05 13:01:41 +08:00
|
|
|
|
|
|
|
/* find global oc defn. it we don't know about it assume it's ok */
|
2000-02-07 05:09:44 +08:00
|
|
|
if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
|
2000-04-25 21:15:55 +08:00
|
|
|
return NULL;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check for empty oc_required */
|
|
|
|
if(oc->soc_required == NULL) {
|
2000-04-25 21:15:55 +08:00
|
|
|
return NULL;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 ) {
|
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
2000-02-29 05:16:05 +08:00
|
|
|
if( a->a_desc->ad_type == at ) {
|
2000-02-05 13:01:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
char **pp;
|
|
|
|
|
|
|
|
if ( at->sat_oid &&
|
|
|
|
strcmp( a->a_type, at->sat_oid ) == 0 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pp = at->sat_names;
|
|
|
|
if ( pp == NULL ) {
|
|
|
|
/* Empty name list => not found */
|
|
|
|
a = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while ( *pp ) {
|
|
|
|
if ( strcasecmp( a->a_type, *pp ) == 0 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pp++;
|
|
|
|
}
|
|
|
|
if ( *pp ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* not there => schema violation */
|
|
|
|
if ( a == NULL ) {
|
2000-02-07 05:09:44 +08:00
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
|
|
|
return at->sat_cname;
|
|
|
|
#else
|
2000-02-05 13:01:41 +08:00
|
|
|
if ( at->sat_names && at->sat_names[0] ) {
|
|
|
|
return at->sat_names[0];
|
|
|
|
} else {
|
|
|
|
return at->sat_oid;
|
|
|
|
}
|
2000-02-07 05:09:44 +08:00
|
|
|
#endif
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2000-05-29 00:36:34 +08:00
|
|
|
#ifndef SLAPD_SCHEMA_NOT_COMPAT
|
|
|
|
static
|
|
|
|
#endif
|
|
|
|
int oc_check_allowed(
|
2000-02-07 05:09:44 +08:00
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
|
|
|
AttributeType *at,
|
|
|
|
#else
|
|
|
|
char *type,
|
|
|
|
#endif
|
|
|
|
struct berval **ocl )
|
2000-02-05 13:01:41 +08:00
|
|
|
{
|
|
|
|
ObjectClass *oc;
|
|
|
|
int i, j;
|
2000-02-07 05:09:44 +08:00
|
|
|
|
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"oc_check_allowed type \"%s\"\n",
|
|
|
|
at->sat_cname, 0, 0 );
|
|
|
|
|
|
|
|
/* always allow objectclass attribute */
|
|
|
|
if ( strcasecmp( at->sat_cname, "objectclass" ) == 0 ) {
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
AttributeType *at;
|
2000-02-05 13:01:41 +08:00
|
|
|
char **pp;
|
2000-02-07 05:09:44 +08:00
|
|
|
char *p;
|
|
|
|
char *t;
|
2000-02-05 13:01:41 +08:00
|
|
|
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"oc_check_allowed type \"%s\"\n", type, 0, 0 );
|
|
|
|
|
|
|
|
/* always allow objectclass attribute */
|
|
|
|
if ( strcasecmp( type, "objectclass" ) == 0 ) {
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
2000-02-07 05:09:44 +08:00
|
|
|
#endif
|
2000-02-05 13:01:41 +08:00
|
|
|
|
2000-02-07 05:09:44 +08:00
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
2000-05-22 11:46:57 +08:00
|
|
|
/*
|
|
|
|
* All operational attributions are allowed by schema rules.
|
|
|
|
*/
|
2000-02-07 05:09:44 +08:00
|
|
|
if( is_at_operational(at) ) {
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
|
|
|
#else
|
2000-02-05 13:01:41 +08:00
|
|
|
/*
|
|
|
|
* The "type" we have received is actually an AttributeDescription.
|
|
|
|
* Let's find out the corresponding type.
|
|
|
|
*/
|
|
|
|
p = strchr( type, ';' );
|
|
|
|
if ( p ) {
|
|
|
|
t = ch_malloc( p-type+1 );
|
|
|
|
strncpy( t, type, p-type );
|
|
|
|
t[p-type] = '\0';
|
|
|
|
Debug( LDAP_DEBUG_TRACE,
|
|
|
|
"oc_check_allowed type \"%s\" from \"%s\"\n",
|
|
|
|
t, type, 0 );
|
|
|
|
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
t = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All operational attributions are allowed by schema rules.
|
|
|
|
*/
|
|
|
|
if ( oc_check_op_attr( t ) ) {
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
2000-02-07 05:09:44 +08:00
|
|
|
#endif
|
2000-02-05 13:01:41 +08:00
|
|
|
|
|
|
|
/* 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 &&
|
2000-02-07 05:09:44 +08:00
|
|
|
oc->soc_required[j] != NULL; j++ )
|
|
|
|
{
|
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
|
|
|
if( at == oc->soc_required[j] ) {
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
|
|
|
#else
|
2000-02-05 13:01:41 +08:00
|
|
|
at = oc->soc_required[j];
|
|
|
|
if ( at->sat_oid &&
|
|
|
|
strcmp(at->sat_oid, t ) == 0 ) {
|
|
|
|
if ( t != type )
|
|
|
|
ldap_memfree( t );
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
pp = at->sat_names;
|
|
|
|
if ( pp == NULL )
|
|
|
|
continue;
|
|
|
|
while ( *pp ) {
|
|
|
|
if ( strcasecmp( *pp, t ) == 0 ) {
|
|
|
|
if ( t != type )
|
|
|
|
ldap_memfree( t );
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
pp++;
|
|
|
|
}
|
2000-02-07 05:09:44 +08:00
|
|
|
#endif
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
/* does it allow the type? */
|
|
|
|
for ( j = 0; oc->soc_allowed != NULL &&
|
2000-02-07 05:09:44 +08:00
|
|
|
oc->soc_allowed[j] != NULL; j++ )
|
|
|
|
{
|
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT
|
|
|
|
if( at == oc->soc_allowed[j] ) {
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-07 05:09:44 +08:00
|
|
|
}
|
|
|
|
#else
|
2000-02-05 13:01:41 +08:00
|
|
|
at = oc->soc_allowed[j];
|
|
|
|
if ( at->sat_oid &&
|
|
|
|
strcmp( at->sat_oid, t ) == 0 ) {
|
|
|
|
if ( t != type )
|
|
|
|
ldap_memfree( t );
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
pp = at->sat_names;
|
|
|
|
if ( pp == NULL )
|
|
|
|
continue;
|
|
|
|
while ( *pp ) {
|
|
|
|
if ( strcasecmp( *pp, t ) == 0 ||
|
|
|
|
strcmp( *pp, "*" ) == 0 ) {
|
|
|
|
if ( t != type )
|
|
|
|
ldap_memfree( t );
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
pp++;
|
|
|
|
}
|
2000-02-07 05:09:44 +08:00
|
|
|
#endif
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|
|
|
|
/* 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 );
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_SUCCESS;
|
2000-02-05 13:01:41 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-07 05:09:44 +08:00
|
|
|
#ifndef SLAPD_SCHEMA_NOT_COMPAT
|
2000-02-05 13:01:41 +08:00
|
|
|
if ( t != type )
|
|
|
|
ldap_memfree( t );
|
2000-02-07 05:09:44 +08:00
|
|
|
#endif
|
|
|
|
|
2000-02-05 13:01:41 +08:00
|
|
|
/* not allowed by any oc */
|
2000-04-25 21:15:55 +08:00
|
|
|
return LDAP_OBJECT_CLASS_VIOLATION;
|
2000-02-05 13:01:41 +08:00
|
|
|
}
|