1 /* schema_check.c - routines to enforce schema definitions */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
19 static char * oc_check_required(Entry *e, struct berval *ocname);
22 * entry_schema_check - check that entry e conforms to the schema required
23 * by its object class(es).
25 * returns 0 if so, non-zero otherwise.
30 Entry *e, Attribute *oldattrs, const char** text,
31 char *textbuf, size_t textlen )
37 AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
40 if( !global_schemacheck ) return LDAP_SUCCESS;
44 /* find the object class attribute - could error out here */
45 if ( (aoc = attr_find( e->e_attrs, ad_objectClass )) == NULL ) {
47 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
48 "entry_schema_check: No objectClass for entry (%s).\n", e->e_dn ));
50 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
54 *text = "no objectClass attribute";
55 return LDAP_OBJECT_CLASS_VIOLATION;
58 /* check that the entry has required attrs for each oc */
59 for ( i = 0; aoc->a_vals[i] != NULL; i++ ) {
60 if ( (oc = oc_find( aoc->a_vals[i]->bv_val )) == NULL ) {
61 snprintf( textbuf, textlen,
62 "unrecognized objectClass '%s'",
63 aoc->a_vals[i]->bv_val );
66 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
67 "entry_schema_check: dn (%s), %s\n",
70 Debug( LDAP_DEBUG_ANY,
71 "entry_check_schema(%s): \"%s\" not recognized\n",
72 e->e_dn, textbuf, 0 );
75 return LDAP_OBJECT_CLASS_VIOLATION;
78 char *s = oc_check_required( e, aoc->a_vals[i] );
81 snprintf( textbuf, textlen,
82 "object class '%s' requires attribute '%s'",
83 aoc->a_vals[i]->bv_val, s );
86 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
87 "entry_schema_check: dn=\"%s\" %s",
90 Debug( LDAP_DEBUG_ANY,
92 e->e_dn, textbuf, 0 );
95 return LDAP_OBJECT_CLASS_VIOLATION;
98 if( oc == slap_schema.si_oc_extensibleObject ) {
112 /* check that each attr in the entry is allowed by some oc */
113 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
114 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals );
116 char *type = a->a_desc->ad_cname->bv_val;
118 snprintf( textbuf, textlen,
119 "attribute '%s' not allowed",
123 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
124 "entry_schema_check: dn=\"%s\" %s\n",
127 Debug( LDAP_DEBUG_ANY,
129 e->e_dn, textbuf, 0 );
136 /* there should be at least one value */
138 assert( a->a_vals[0] != NULL );
140 /* if single value type, check for multiple values */
141 if( is_at_single_value( a->a_desc->ad_type ) &&
142 a->a_vals[1] != NULL )
144 char *type = a->a_desc->ad_cname->bv_val;
146 snprintf( textbuf, textlen,
147 "attribute '%s' cannot have multiple values",
151 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
152 "entry_schema_check: dn=\"%s\" %s\n",
155 Debug( LDAP_DEBUG_ANY,
157 e->e_dn, textbuf, 0 );
160 ret = LDAP_CONSTRAINT_VIOLATION;
169 oc_check_required( Entry *e, struct berval *ocname )
177 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
178 "oc_check_required: dn (%s), objectClass \"%s\"\n",
179 e->e_dn, ocname->bv_val ));
181 Debug( LDAP_DEBUG_TRACE,
182 "oc_check_required entry (%s), objectClass \"%s\"\n",
183 e->e_dn, ocname->bv_val, 0 );
187 /* find global oc defn. it we don't know about it assume it's ok */
188 if ( (oc = oc_find( ocname->bv_val )) == NULL ) {
192 /* check for empty oc_required */
193 if(oc->soc_required == NULL) {
197 /* for each required attribute */
198 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
199 at = oc->soc_required[i];
200 /* see if it's in the entry */
201 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
202 if( a->a_desc->ad_type == at ) {
206 /* not there => schema violation */
208 return at->sat_cname;
215 int oc_check_allowed(
217 struct berval **ocl )
223 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
224 "oc_check_allowed: type \"%s\"\n", at->sat_cname ));
226 Debug( LDAP_DEBUG_TRACE,
227 "oc_check_allowed type \"%s\"\n",
228 at->sat_cname, 0, 0 );
231 /* always allow objectClass attribute */
232 if ( strcasecmp( at->sat_cname, "objectClass" ) == 0 ) {
238 * All operational attributions are allowed by schema rules.
240 if( is_at_operational(at) ) {
244 /* check that the type appears as req or opt in at least one oc */
245 for ( i = 0; ocl[i] != NULL; i++ ) {
246 /* if we know about the oc */
247 if ( (oc = oc_find( ocl[i]->bv_val )) != NULL ) {
248 /* does it require the type? */
249 for ( j = 0; oc->soc_required != NULL &&
250 oc->soc_required[j] != NULL; j++ )
252 if( at == oc->soc_required[j] ) {
256 /* does it allow the type? */
257 for ( j = 0; oc->soc_allowed != NULL &&
258 oc->soc_allowed[j] != NULL; j++ )
260 if( at == oc->soc_allowed[j] ) {
264 /* maybe the next oc allows it */
268 /* not allowed by any oc */
269 return LDAP_OBJECT_CLASS_VIOLATION;