1 /* schema_check.c - routines to enforce schema definitions */
4 * Copyright 1998-2002 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(
22 struct berval *ocname );
25 * entry_schema_check - check that entry e conforms to the schema required
26 * by its object class(es).
28 * returns 0 if so, non-zero otherwise.
37 char *textbuf, size_t textlen )
39 Attribute *a, *asc, *aoc;
43 AttributeDescription *ad_structuralObjectClass
44 = slap_schema.si_ad_structuralObjectClass;
45 AttributeDescription *ad_objectClass
46 = slap_schema.si_ad_objectClass;
48 int subentry = is_entry_subentry( e );
49 int collectiveSubentry = 0;
52 if( subentry) collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
57 /* misc attribute checks */
58 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
59 const char *type = a->a_desc->ad_cname.bv_val;
61 /* there should be at least one value */
63 assert( a->a_vals[0].bv_val != NULL );
65 if( a->a_desc->ad_type->sat_check ) {
66 int rc = (a->a_desc->ad_type->sat_check)(
67 be, e, a, text, textbuf, textlen );
68 if( rc != LDAP_SUCCESS ) {
73 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
74 snprintf( textbuf, textlen,
75 "'%s' can only appear in collectiveAttributeSubentry",
77 return LDAP_OBJECT_CLASS_VIOLATION;
80 /* if single value type, check for multiple values */
81 if( is_at_single_value( a->a_desc->ad_type ) &&
82 a->a_vals[1].bv_val != NULL )
84 snprintf( textbuf, textlen,
85 "attribute '%s' cannot have multiple values",
89 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
90 "entry_schema_check: dn=\"%s\" %s\n",
93 Debug( LDAP_DEBUG_ANY,
95 e->e_dn, textbuf, 0 );
98 return LDAP_CONSTRAINT_VIOLATION;
102 /* it's a REALLY bad idea to disable schema checks */
103 if( !global_schemacheck ) return LDAP_SUCCESS;
105 /* find the object class attribute - could error out here */
106 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
109 LDAP_LOG(( "schema", LDAP_LEVEL_INFO, "entry_schema_check: "
110 "No structuralObjectClass for entry (%s)\n",
113 Debug( LDAP_DEBUG_ANY,
114 "No structuralObjectClass for entry (%s)\n",
118 *text = "no structuralObjectClass operational attribute";
119 return LDAP_OBJECT_CLASS_VIOLATION;
122 assert( asc->a_vals != NULL );
123 assert( asc->a_vals[0].bv_val != NULL );
124 assert( asc->a_vals[1].bv_val == NULL );
126 sc = oc_bvfind( &asc->a_vals[0] );
128 snprintf( textbuf, textlen,
129 "unrecognized structuralObjectClass '%s'",
130 asc->a_vals[0].bv_val );
133 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
134 "entry_schema_check: dn (%s), %s\n",
137 Debug( LDAP_DEBUG_ANY,
138 "entry_check_schema(%s): %s\n",
139 e->e_dn, textbuf, 0 );
142 return LDAP_OBJECT_CLASS_VIOLATION;
145 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
146 snprintf( textbuf, textlen,
147 "structuralObjectClass '%s' is not STRUCTURAL",
148 asc->a_vals[0].bv_val );
151 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
152 "entry_schema_check: dn (%s), %s\n",
155 Debug( LDAP_DEBUG_ANY,
156 "entry_check_schema(%s): %s\n",
157 e->e_dn, textbuf, 0 );
160 return LDAP_OBJECT_CLASS_VIOLATION;
163 /* find the object class attribute */
164 aoc = attr_find( e->e_attrs, ad_objectClass );
167 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
168 "entry_schema_check: No objectClass for entry (%s).\n",
171 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
175 *text = "no objectClass attribute";
176 return LDAP_OBJECT_CLASS_VIOLATION;
179 assert( aoc->a_vals != NULL );
180 assert( aoc->a_vals[0].bv_val != NULL );
182 rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
183 if( rc != LDAP_SUCCESS ) {
185 } else if ( nsc.bv_len == 0 ) {
186 return LDAP_OBJECT_CLASS_VIOLATION;
192 snprintf( textbuf, textlen,
193 "unrecognized objectClass '%s'",
194 aoc->a_vals[0].bv_val );
195 return LDAP_OBJECT_CLASS_VIOLATION;
197 } else if ( sc != oc ) {
198 snprintf( textbuf, textlen,
199 "structuralObjectClass modification from '%s' to '%s' not allowed",
200 asc->a_vals[0].bv_val, nsc.bv_val );
201 return LDAP_NO_OBJECT_CLASS_MODS;
204 /* check that the entry has required attrs for each oc */
205 for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
206 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
207 snprintf( textbuf, textlen,
208 "unrecognized objectClass '%s'",
209 aoc->a_vals[i].bv_val );
212 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
213 "entry_schema_check: dn (%s), %s\n",
216 Debug( LDAP_DEBUG_ANY,
217 "entry_check_schema(%s): %s\n",
218 e->e_dn, textbuf, 0 );
221 return LDAP_OBJECT_CLASS_VIOLATION;
224 if ( oc->soc_check ) {
225 int rc = (oc->soc_check)( be, e, oc,
226 text, textbuf, textlen );
227 if( rc != LDAP_SUCCESS ) {
232 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
233 /* object class is abstract */
234 if ( oc != slap_schema.si_oc_top &&
235 !is_object_subclass( oc, sc ))
238 ObjectClass *xc = NULL;
239 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
241 xc = oc_bvfind( &aoc->a_vals[i] );
243 snprintf( textbuf, textlen,
244 "unrecognized objectClass '%s'",
245 aoc->a_vals[i].bv_val );
248 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
249 "entry_schema_check: dn (%s), %s\n",
252 Debug( LDAP_DEBUG_ANY,
253 "entry_check_schema(%s): %s\n",
254 e->e_dn, textbuf, 0 );
257 return LDAP_OBJECT_CLASS_VIOLATION;
260 /* since we previous check against the
261 * structural object of this entry, the
262 * abstract class must be a (direct or indirect)
263 * superclass of one of the auxiliary classes of
266 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
267 is_object_subclass( oc, xc ) )
277 snprintf( textbuf, textlen, "instanstantiation of "
278 "abstract objectClass '%s' not allowed",
279 aoc->a_vals[i].bv_val );
282 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
283 "entry_schema_check: dn (%s), %s\n",
286 Debug( LDAP_DEBUG_ANY,
287 "entry_check_schema(%s): %s\n",
288 e->e_dn, textbuf, 0 );
291 return LDAP_OBJECT_CLASS_VIOLATION;
295 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
296 char *s = oc_check_required( e, oc, &aoc->a_vals[i] );
299 snprintf( textbuf, textlen,
300 "object class '%s' requires attribute '%s'",
301 aoc->a_vals[i].bv_val, s );
304 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
305 "entry_schema_check: dn=\"%s\" %s",
308 Debug( LDAP_DEBUG_ANY,
310 e->e_dn, textbuf, 0 );
313 return LDAP_OBJECT_CLASS_VIOLATION;
316 if( oc == slap_schema.si_oc_extensibleObject ) {
326 /* check that each attr in the entry is allowed by some oc */
327 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
328 int ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
329 if ( ret != LDAP_SUCCESS ) {
330 char *type = a->a_desc->ad_cname.bv_val;
332 snprintf( textbuf, textlen,
333 "attribute '%s' not allowed",
337 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
338 "entry_schema_check: dn=\"%s\" %s\n",
341 Debug( LDAP_DEBUG_ANY,
343 e->e_dn, textbuf, 0 );
357 struct berval *ocname )
364 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
365 "oc_check_required: dn (%s), objectClass \"%s\"\n",
366 e->e_dn, ocname->bv_val ));
368 Debug( LDAP_DEBUG_TRACE,
369 "oc_check_required entry (%s), objectClass \"%s\"\n",
370 e->e_dn, ocname->bv_val, 0 );
374 /* check for empty oc_required */
375 if(oc->soc_required == NULL) {
379 /* for each required attribute */
380 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
381 at = oc->soc_required[i];
382 /* see if it's in the entry */
383 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
384 if( a->a_desc->ad_type == at ) {
388 /* not there => schema violation */
390 return at->sat_cname.bv_val;
397 int oc_check_allowed(
405 LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
406 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val ));
408 Debug( LDAP_DEBUG_TRACE,
409 "oc_check_allowed type \"%s\"\n",
410 at->sat_cname.bv_val, 0, 0 );
413 /* always allow objectClass attribute */
414 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
419 * All operational attributions are allowed by schema rules.
421 if( is_at_operational(at) ) {
425 /* check to see if its allowed by the structuralObjectClass */
427 /* does it require the type? */
428 for ( j = 0; sc->soc_required != NULL &&
429 sc->soc_required[j] != NULL; j++ )
431 if( at == sc->soc_required[j] ) {
436 /* does it allow the type? */
437 for ( j = 0; sc->soc_allowed != NULL &&
438 sc->soc_allowed[j] != NULL; j++ )
440 if( at == sc->soc_allowed[j] ) {
446 /* check that the type appears as req or opt in at least one oc */
447 for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
448 /* if we know about the oc */
449 ObjectClass *oc = oc_bvfind( &ocl[i] );
450 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
451 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
453 /* does it require the type? */
454 for ( j = 0; oc->soc_required != NULL &&
455 oc->soc_required[j] != NULL; j++ )
457 if( at == oc->soc_required[j] ) {
461 /* does it allow the type? */
462 for ( j = 0; oc->soc_allowed != NULL &&
463 oc->soc_allowed[j] != NULL; j++ )
465 if( at == oc->soc_allowed[j] ) {
472 /* not allowed by any oc */
473 return LDAP_OBJECT_CLASS_VIOLATION;
477 * Determine the structural object class from a set of OIDs
479 int structural_class(
484 char *textbuf, size_t textlen )
488 ObjectClass *sc = NULL;
491 *text = "structural_class: internal error";
494 for( i=0; ocs[i].bv_val; i++ ) {
495 oc = oc_bvfind( &ocs[i] );
498 snprintf( textbuf, textlen,
499 "unrecognized objectClass '%s'",
502 return LDAP_OBJECT_CLASS_VIOLATION;
505 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
506 if( sc == NULL || is_object_subclass( sc, oc ) ) {
510 } else if ( !is_object_subclass( oc, sc ) ) {
512 ObjectClass *xc = NULL;
514 /* find common superior */
515 for( j=i+1; ocs[j].bv_val; j++ ) {
516 xc = oc_bvfind( &ocs[j] );
519 snprintf( textbuf, textlen,
520 "unrecognized objectClass '%s'",
523 return LDAP_OBJECT_CLASS_VIOLATION;
526 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
531 if( is_object_subclass( sc, xc ) &&
532 is_object_subclass( oc, xc ) )
534 /* found common subclass */
542 /* no common subclass */
543 snprintf( textbuf, textlen,
544 "invalid structural object class chain (%s/%s)",
545 ocs[scn].bv_val, ocs[i].bv_val );
547 return LDAP_OBJECT_CLASS_VIOLATION;
557 *text = "no structural object classes provided";
558 return LDAP_OBJECT_CLASS_VIOLATION;
566 * Return structural object class from list of modifications
568 int mods_structural_class(
572 char *textbuf, size_t textlen )
574 Modifications *ocmod = NULL;
576 for( ; mods != NULL; mods = mods->sml_next ) {
577 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
578 if( ocmod != NULL ) {
579 *text = "entry has multiple objectClass attributes";
580 return LDAP_OBJECT_CLASS_VIOLATION;
586 if( ocmod == NULL ) {
587 *text = "entry has no objectClass attribute";
588 return LDAP_OBJECT_CLASS_VIOLATION;
591 if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
592 *text = "objectClass attribute has no values";
593 return LDAP_OBJECT_CLASS_VIOLATION;
596 return structural_class( ocmod->sml_bvalues, sc, NULL,
597 text, textbuf, textlen );