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;
41 #ifdef SLAP_EXTENDED_SCHEMA
47 AttributeDescription *ad_structuralObjectClass
48 = slap_schema.si_ad_structuralObjectClass;
49 AttributeDescription *ad_objectClass
50 = slap_schema.si_ad_objectClass;
52 int subentry = is_entry_subentry( e );
53 int collectiveSubentry = 0;
56 collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
61 /* misc attribute checks */
62 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
63 const char *type = a->a_desc->ad_cname.bv_val;
65 /* there should be at least one value */
67 assert( a->a_vals[0].bv_val != NULL );
69 if( a->a_desc->ad_type->sat_check ) {
70 int rc = (a->a_desc->ad_type->sat_check)(
71 be, e, a, text, textbuf, textlen );
72 if( rc != LDAP_SUCCESS ) {
77 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
78 snprintf( textbuf, textlen,
79 "'%s' can only appear in collectiveAttributeSubentry",
81 return LDAP_OBJECT_CLASS_VIOLATION;
84 /* if single value type, check for multiple values */
85 if( is_at_single_value( a->a_desc->ad_type ) &&
86 a->a_vals[1].bv_val != NULL )
88 snprintf( textbuf, textlen,
89 "attribute '%s' cannot have multiple values",
93 LDAP_LOG( OPERATION, INFO,
94 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0 );
96 Debug( LDAP_DEBUG_ANY,
98 e->e_dn, textbuf, 0 );
101 return LDAP_CONSTRAINT_VIOLATION;
105 /* it's a REALLY bad idea to disable schema checks */
106 if( !global_schemacheck ) return LDAP_SUCCESS;
108 /* find the structural object class attribute */
109 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
112 LDAP_LOG( OPERATION, INFO,
113 "entry_schema_check: No structuralObjectClass for entry (%s)\n",
116 Debug( LDAP_DEBUG_ANY,
117 "No structuralObjectClass for entry (%s)\n",
121 *text = "no structuralObjectClass operational attribute";
125 assert( asc->a_vals != NULL );
126 assert( asc->a_vals[0].bv_val != NULL );
127 assert( asc->a_vals[1].bv_val == NULL );
129 sc = oc_bvfind( &asc->a_vals[0] );
131 snprintf( textbuf, textlen,
132 "unrecognized structuralObjectClass '%s'",
133 asc->a_vals[0].bv_val );
136 LDAP_LOG( OPERATION, INFO,
137 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
139 Debug( LDAP_DEBUG_ANY,
140 "entry_check_schema(%s): %s\n",
141 e->e_dn, textbuf, 0 );
144 return LDAP_OBJECT_CLASS_VIOLATION;
147 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
148 snprintf( textbuf, textlen,
149 "structuralObjectClass '%s' is not STRUCTURAL",
150 asc->a_vals[0].bv_val );
153 LDAP_LOG( OPERATION, INFO,
154 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
156 Debug( LDAP_DEBUG_ANY,
157 "entry_check_schema(%s): %s\n",
158 e->e_dn, textbuf, 0 );
164 if( sc->soc_obsolete ) {
165 snprintf( textbuf, textlen,
166 "structuralObjectClass '%s' is OBSOLETE",
167 asc->a_vals[0].bv_val );
170 LDAP_LOG( OPERATION, INFO,
171 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
173 Debug( LDAP_DEBUG_ANY,
174 "entry_check_schema(%s): %s\n",
175 e->e_dn, textbuf, 0 );
178 return LDAP_OBJECT_CLASS_VIOLATION;
181 /* find the object class attribute */
182 aoc = attr_find( e->e_attrs, ad_objectClass );
185 LDAP_LOG( OPERATION, INFO,
186 "entry_schema_check: No objectClass for entry (%s).\n",
189 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
193 *text = "no objectClass attribute";
194 return LDAP_OBJECT_CLASS_VIOLATION;
197 assert( aoc->a_vals != NULL );
198 assert( aoc->a_vals[0].bv_val != NULL );
200 rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
201 if( rc != LDAP_SUCCESS ) {
208 snprintf( textbuf, textlen,
209 "unrecognized objectClass '%s'",
210 aoc->a_vals[0].bv_val );
211 return LDAP_OBJECT_CLASS_VIOLATION;
213 } else if ( sc != oc ) {
214 snprintf( textbuf, textlen,
215 "structural object class modification from '%s' to '%s' not allowed",
216 asc->a_vals[0].bv_val, nsc.bv_val );
217 return LDAP_NO_OBJECT_CLASS_MODS;
220 #ifdef SLAP_EXTENDED_SCHEMA
221 /* find the content rule for the structural class */
222 cr = cr_find( sc->soc_oid );
224 /* the cr must be same as the structural class */
225 assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
227 /* check that the entry has required attrs of the content rule */
229 if( cr->scr_obsolete ) {
230 snprintf( textbuf, textlen,
231 "content rule '%s' is obsolete",
232 ldap_contentrule2name( &cr->scr_crule ));
235 LDAP_LOG( OPERATION, INFO,
236 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
238 Debug( LDAP_DEBUG_ANY,
240 e->e_dn, textbuf, 0 );
243 return LDAP_OBJECT_CLASS_VIOLATION;
246 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
247 at = cr->scr_required[i];
249 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
250 if( a->a_desc->ad_type == at ) {
255 /* not there => schema violation */
257 snprintf( textbuf, textlen,
258 "content rule '%s' requires attribute '%s'",
259 ldap_contentrule2name( &cr->scr_crule ),
260 at->sat_cname.bv_val );
263 LDAP_LOG( OPERATION, INFO,
264 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
266 Debug( LDAP_DEBUG_ANY,
268 e->e_dn, textbuf, 0 );
271 return LDAP_OBJECT_CLASS_VIOLATION;
275 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
276 at = cr->scr_precluded[i];
278 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
279 if( a->a_desc->ad_type == at ) {
284 /* there => schema violation */
286 snprintf( textbuf, textlen,
287 "content rule '%s' precluded attribute '%s'",
288 ldap_contentrule2name( &cr->scr_crule ),
289 at->sat_cname.bv_val );
292 LDAP_LOG( OPERATION, INFO,
293 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
295 Debug( LDAP_DEBUG_ANY,
297 e->e_dn, textbuf, 0 );
300 return LDAP_OBJECT_CLASS_VIOLATION;
304 #endif /* SLAP_EXTENDED_SCHEMA */
306 /* check that the entry has required attrs for each oc */
307 for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
308 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
309 snprintf( textbuf, textlen,
310 "unrecognized objectClass '%s'",
311 aoc->a_vals[i].bv_val );
314 LDAP_LOG( OPERATION, INFO,
315 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
317 Debug( LDAP_DEBUG_ANY,
318 "entry_check_schema(%s): %s\n",
319 e->e_dn, textbuf, 0 );
322 return LDAP_OBJECT_CLASS_VIOLATION;
325 if ( oc->soc_check ) {
326 int rc = (oc->soc_check)( be, e, oc,
327 text, textbuf, textlen );
328 if( rc != LDAP_SUCCESS ) {
333 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
334 /* object class is abstract */
335 if ( oc != slap_schema.si_oc_top &&
336 !is_object_subclass( oc, sc ))
339 ObjectClass *xc = NULL;
340 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
342 xc = oc_bvfind( &aoc->a_vals[i] );
344 snprintf( textbuf, textlen,
345 "unrecognized objectClass '%s'",
346 aoc->a_vals[i].bv_val );
349 LDAP_LOG( OPERATION, INFO,
350 "entry_schema_check: dn (%s), %s\n",
351 e->e_dn, textbuf, 0 );
353 Debug( LDAP_DEBUG_ANY,
354 "entry_check_schema(%s): %s\n",
355 e->e_dn, textbuf, 0 );
358 return LDAP_OBJECT_CLASS_VIOLATION;
361 /* since we previous check against the
362 * structural object of this entry, the
363 * abstract class must be a (direct or indirect)
364 * superclass of one of the auxiliary classes of
367 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
368 is_object_subclass( oc, xc ) )
377 snprintf( textbuf, textlen, "instanstantiation of "
378 "abstract objectClass '%s' not allowed",
379 aoc->a_vals[i].bv_val );
382 LDAP_LOG( OPERATION, INFO,
383 "entry_schema_check: dn (%s), %s\n",
384 e->e_dn, textbuf, 0 );
386 Debug( LDAP_DEBUG_ANY,
387 "entry_check_schema(%s): %s\n",
388 e->e_dn, textbuf, 0 );
391 return LDAP_OBJECT_CLASS_VIOLATION;
395 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
398 #ifdef SLAP_EXTENDED_SCHEMA
399 if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
402 if( cr->scr_auxiliaries ) {
403 for( ; cr->scr_auxiliaries[k]; k++ ) {
404 if( cr->scr_auxiliaries[k] == oc ) {
410 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
415 snprintf( textbuf, textlen,
416 "content rule '%s' does not allow class '%s'",
417 ldap_contentrule2name( &cr->scr_crule ),
418 oc->soc_cname.bv_val );
421 LDAP_LOG( OPERATION, INFO,
422 "entry_schema_check: dn=\"%s\" %s",
423 e->e_dn, textbuf, 0 );
425 Debug( LDAP_DEBUG_ANY,
427 e->e_dn, textbuf, 0 );
430 return LDAP_OBJECT_CLASS_VIOLATION;
433 #endif /* SLAP_EXTENDED_SCHEMA */
435 s = oc_check_required( e, oc, &aoc->a_vals[i] );
437 snprintf( textbuf, textlen,
438 "object class '%s' requires attribute '%s'",
439 aoc->a_vals[i].bv_val, s );
442 LDAP_LOG( OPERATION, INFO,
443 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
445 Debug( LDAP_DEBUG_ANY,
447 e->e_dn, textbuf, 0 );
450 return LDAP_OBJECT_CLASS_VIOLATION;
453 if( oc == slap_schema.si_oc_extensibleObject ) {
463 /* check that each attr in the entry is allowed by some oc */
464 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
467 #ifdef SLAP_EXTENDED_SCHEMA
468 ret = LDAP_OBJECT_CLASS_VIOLATION;
470 if( cr && cr->scr_required ) {
471 for( i=0; cr->scr_required[i]; i++ ) {
472 if( cr->scr_required[i] == a->a_desc->ad_type ) {
479 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
480 for( i=0; cr->scr_allowed[i]; i++ ) {
481 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
488 if( ret != LDAP_SUCCESS )
489 #endif /* SLAP_EXTENDED_SCHEMA */
491 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
494 if ( ret != LDAP_SUCCESS ) {
495 char *type = a->a_desc->ad_cname.bv_val;
497 snprintf( textbuf, textlen,
498 "attribute '%s' not allowed",
502 LDAP_LOG( OPERATION, INFO,
503 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0);
505 Debug( LDAP_DEBUG_ANY,
507 e->e_dn, textbuf, 0 );
521 struct berval *ocname )
528 LDAP_LOG( OPERATION, ENTRY,
529 "oc_check_required: dn (%s), objectClass \"%s\"\n",
530 e->e_dn, ocname->bv_val, 0 );
532 Debug( LDAP_DEBUG_TRACE,
533 "oc_check_required entry (%s), objectClass \"%s\"\n",
534 e->e_dn, ocname->bv_val, 0 );
538 /* check for empty oc_required */
539 if(oc->soc_required == NULL) {
543 /* for each required attribute */
544 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
545 at = oc->soc_required[i];
546 /* see if it's in the entry */
547 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
548 if( a->a_desc->ad_type == at ) {
552 /* not there => schema violation */
554 return at->sat_cname.bv_val;
561 int oc_check_allowed(
569 LDAP_LOG( OPERATION, ENTRY,
570 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val, 0, 0 );
572 Debug( LDAP_DEBUG_TRACE,
573 "oc_check_allowed type \"%s\"\n",
574 at->sat_cname.bv_val, 0, 0 );
577 /* always allow objectClass attribute */
578 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
583 * All operational attributions are allowed by schema rules.
585 if( is_at_operational(at) ) {
589 /* check to see if its allowed by the structuralObjectClass */
591 /* does it require the type? */
592 for ( j = 0; sc->soc_required != NULL &&
593 sc->soc_required[j] != NULL; j++ )
595 if( at == sc->soc_required[j] ) {
600 /* does it allow the type? */
601 for ( j = 0; sc->soc_allowed != NULL &&
602 sc->soc_allowed[j] != NULL; j++ )
604 if( at == sc->soc_allowed[j] ) {
610 /* check that the type appears as req or opt in at least one oc */
611 for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
612 /* if we know about the oc */
613 ObjectClass *oc = oc_bvfind( &ocl[i] );
614 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
615 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
617 /* does it require the type? */
618 for ( j = 0; oc->soc_required != NULL &&
619 oc->soc_required[j] != NULL; j++ )
621 if( at == oc->soc_required[j] ) {
625 /* does it allow the type? */
626 for ( j = 0; oc->soc_allowed != NULL &&
627 oc->soc_allowed[j] != NULL; j++ )
629 if( at == oc->soc_allowed[j] ) {
636 /* not allowed by any oc */
637 return LDAP_OBJECT_CLASS_VIOLATION;
641 * Determine the structural object class from a set of OIDs
643 int structural_class(
648 char *textbuf, size_t textlen )
652 ObjectClass *sc = NULL;
655 *text = "structural_class: internal error";
658 for( i=0; ocs[i].bv_val; i++ ) {
659 oc = oc_bvfind( &ocs[i] );
662 snprintf( textbuf, textlen,
663 "unrecognized objectClass '%s'",
666 return LDAP_OBJECT_CLASS_VIOLATION;
669 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
670 if( sc == NULL || is_object_subclass( sc, oc ) ) {
674 } else if ( !is_object_subclass( oc, sc ) ) {
676 ObjectClass *xc = NULL;
678 /* find common superior */
679 for( j=i+1; ocs[j].bv_val; j++ ) {
680 xc = oc_bvfind( &ocs[j] );
683 snprintf( textbuf, textlen,
684 "unrecognized objectClass '%s'",
687 return LDAP_OBJECT_CLASS_VIOLATION;
690 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
695 if( is_object_subclass( sc, xc ) &&
696 is_object_subclass( oc, xc ) )
698 /* found common subclass */
706 /* no common subclass */
707 snprintf( textbuf, textlen,
708 "invalid structural object class chain (%s/%s)",
709 ocs[scn].bv_val, ocs[i].bv_val );
711 return LDAP_OBJECT_CLASS_VIOLATION;
722 *text = "no structural object classes provided";
723 return LDAP_OBJECT_CLASS_VIOLATION;
727 *text = "invalid structural object class";
728 return LDAP_OBJECT_CLASS_VIOLATION;
733 if( scbv->bv_len == 0 ) {
734 *text = "invalid structural object class";
735 return LDAP_OBJECT_CLASS_VIOLATION;
742 * Return structural object class from list of modifications
744 int mods_structural_class(
748 char *textbuf, size_t textlen )
750 Modifications *ocmod = NULL;
752 for( ; mods != NULL; mods = mods->sml_next ) {
753 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
754 if( ocmod != NULL ) {
755 *text = "entry has multiple objectClass attributes";
756 return LDAP_OBJECT_CLASS_VIOLATION;
762 if( ocmod == NULL ) {
763 *text = "entry has no objectClass attribute";
764 return LDAP_OBJECT_CLASS_VIOLATION;
767 if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
768 *text = "objectClass attribute has no values";
769 return LDAP_OBJECT_CLASS_VIOLATION;
772 return structural_class( ocmod->sml_bvalues, sc, NULL,
773 text, textbuf, textlen );