1 /* schema_check.c - routines to enforce schema definitions */
4 * Copyright 1998-2003 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_obsolete ) {
326 /* disallow obsolete classes */
327 snprintf( textbuf, textlen,
328 "objectClass '%s' is OBSOLETE",
329 aoc->a_vals[i].bv_val );
332 LDAP_LOG( OPERATION, INFO,
333 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
335 Debug( LDAP_DEBUG_ANY,
336 "entry_check_schema(%s): %s\n",
337 e->e_dn, textbuf, 0 );
340 return LDAP_OBJECT_CLASS_VIOLATION;
343 if ( oc->soc_check ) {
344 int rc = (oc->soc_check)( be, e, oc,
345 text, textbuf, textlen );
346 if( rc != LDAP_SUCCESS ) {
351 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
352 /* object class is abstract */
353 if ( oc != slap_schema.si_oc_top &&
354 !is_object_subclass( oc, sc ))
357 ObjectClass *xc = NULL;
358 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
360 xc = oc_bvfind( &aoc->a_vals[i] );
362 snprintf( textbuf, textlen,
363 "unrecognized objectClass '%s'",
364 aoc->a_vals[i].bv_val );
367 LDAP_LOG( OPERATION, INFO,
368 "entry_schema_check: dn (%s), %s\n",
369 e->e_dn, textbuf, 0 );
371 Debug( LDAP_DEBUG_ANY,
372 "entry_check_schema(%s): %s\n",
373 e->e_dn, textbuf, 0 );
376 return LDAP_OBJECT_CLASS_VIOLATION;
379 /* since we previous check against the
380 * structural object of this entry, the
381 * abstract class must be a (direct or indirect)
382 * superclass of one of the auxiliary classes of
385 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
386 is_object_subclass( oc, xc ) )
395 snprintf( textbuf, textlen, "instanstantiation of "
396 "abstract objectClass '%s' not allowed",
397 aoc->a_vals[i].bv_val );
400 LDAP_LOG( OPERATION, INFO,
401 "entry_schema_check: dn (%s), %s\n",
402 e->e_dn, textbuf, 0 );
404 Debug( LDAP_DEBUG_ANY,
405 "entry_check_schema(%s): %s\n",
406 e->e_dn, textbuf, 0 );
409 return LDAP_OBJECT_CLASS_VIOLATION;
413 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
416 #ifdef SLAP_EXTENDED_SCHEMA
417 if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
420 if( cr->scr_auxiliaries ) {
421 for( ; cr->scr_auxiliaries[k]; k++ ) {
422 if( cr->scr_auxiliaries[k] == oc ) {
428 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
433 snprintf( textbuf, textlen,
434 "content rule '%s' does not allow class '%s'",
435 ldap_contentrule2name( &cr->scr_crule ),
436 oc->soc_cname.bv_val );
439 LDAP_LOG( OPERATION, INFO,
440 "entry_schema_check: dn=\"%s\" %s",
441 e->e_dn, textbuf, 0 );
443 Debug( LDAP_DEBUG_ANY,
445 e->e_dn, textbuf, 0 );
448 return LDAP_OBJECT_CLASS_VIOLATION;
451 #endif /* SLAP_EXTENDED_SCHEMA */
453 s = oc_check_required( e, oc, &aoc->a_vals[i] );
455 snprintf( textbuf, textlen,
456 "object class '%s' requires attribute '%s'",
457 aoc->a_vals[i].bv_val, s );
460 LDAP_LOG( OPERATION, INFO,
461 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
463 Debug( LDAP_DEBUG_ANY,
465 e->e_dn, textbuf, 0 );
468 return LDAP_OBJECT_CLASS_VIOLATION;
471 if( oc == slap_schema.si_oc_extensibleObject ) {
481 /* check that each attr in the entry is allowed by some oc */
482 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
485 #ifdef SLAP_EXTENDED_SCHEMA
486 ret = LDAP_OBJECT_CLASS_VIOLATION;
488 if( cr && cr->scr_required ) {
489 for( i=0; cr->scr_required[i]; i++ ) {
490 if( cr->scr_required[i] == a->a_desc->ad_type ) {
497 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
498 for( i=0; cr->scr_allowed[i]; i++ ) {
499 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
506 if( ret != LDAP_SUCCESS )
507 #endif /* SLAP_EXTENDED_SCHEMA */
509 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
512 if ( ret != LDAP_SUCCESS ) {
513 char *type = a->a_desc->ad_cname.bv_val;
515 snprintf( textbuf, textlen,
516 "attribute '%s' not allowed",
520 LDAP_LOG( OPERATION, INFO,
521 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0);
523 Debug( LDAP_DEBUG_ANY,
525 e->e_dn, textbuf, 0 );
539 struct berval *ocname )
546 LDAP_LOG( OPERATION, ENTRY,
547 "oc_check_required: dn (%s), objectClass \"%s\"\n",
548 e->e_dn, ocname->bv_val, 0 );
550 Debug( LDAP_DEBUG_TRACE,
551 "oc_check_required entry (%s), objectClass \"%s\"\n",
552 e->e_dn, ocname->bv_val, 0 );
556 /* check for empty oc_required */
557 if(oc->soc_required == NULL) {
561 /* for each required attribute */
562 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
563 at = oc->soc_required[i];
564 /* see if it's in the entry */
565 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
566 if( a->a_desc->ad_type == at ) {
570 /* not there => schema violation */
572 return at->sat_cname.bv_val;
579 int oc_check_allowed(
587 LDAP_LOG( OPERATION, ENTRY,
588 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val, 0, 0 );
590 Debug( LDAP_DEBUG_TRACE,
591 "oc_check_allowed type \"%s\"\n",
592 at->sat_cname.bv_val, 0, 0 );
595 /* always allow objectClass attribute */
596 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
601 * All operational attributions are allowed by schema rules.
603 if( is_at_operational(at) ) {
607 /* check to see if its allowed by the structuralObjectClass */
609 /* does it require the type? */
610 for ( j = 0; sc->soc_required != NULL &&
611 sc->soc_required[j] != NULL; j++ )
613 if( at == sc->soc_required[j] ) {
618 /* does it allow the type? */
619 for ( j = 0; sc->soc_allowed != NULL &&
620 sc->soc_allowed[j] != NULL; j++ )
622 if( at == sc->soc_allowed[j] ) {
628 /* check that the type appears as req or opt in at least one oc */
629 for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
630 /* if we know about the oc */
631 ObjectClass *oc = oc_bvfind( &ocl[i] );
632 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
633 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
635 /* does it require the type? */
636 for ( j = 0; oc->soc_required != NULL &&
637 oc->soc_required[j] != NULL; j++ )
639 if( at == oc->soc_required[j] ) {
643 /* does it allow the type? */
644 for ( j = 0; oc->soc_allowed != NULL &&
645 oc->soc_allowed[j] != NULL; j++ )
647 if( at == oc->soc_allowed[j] ) {
654 /* not allowed by any oc */
655 return LDAP_OBJECT_CLASS_VIOLATION;
659 * Determine the structural object class from a set of OIDs
661 int structural_class(
666 char *textbuf, size_t textlen )
670 ObjectClass *sc = NULL;
673 *text = "structural_class: internal error";
676 for( i=0; ocs[i].bv_val; i++ ) {
677 oc = oc_bvfind( &ocs[i] );
680 snprintf( textbuf, textlen,
681 "unrecognized objectClass '%s'",
684 return LDAP_OBJECT_CLASS_VIOLATION;
687 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
688 if( sc == NULL || is_object_subclass( sc, oc ) ) {
692 } else if ( !is_object_subclass( oc, sc ) ) {
694 ObjectClass *xc = NULL;
696 /* find common superior */
697 for( j=i+1; ocs[j].bv_val; j++ ) {
698 xc = oc_bvfind( &ocs[j] );
701 snprintf( textbuf, textlen,
702 "unrecognized objectClass '%s'",
705 return LDAP_OBJECT_CLASS_VIOLATION;
708 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
713 if( is_object_subclass( sc, xc ) &&
714 is_object_subclass( oc, xc ) )
716 /* found common subclass */
724 /* no common subclass */
725 snprintf( textbuf, textlen,
726 "invalid structural object class chain (%s/%s)",
727 ocs[scn].bv_val, ocs[i].bv_val );
729 return LDAP_OBJECT_CLASS_VIOLATION;
740 *text = "no structural object class provided";
741 return LDAP_OBJECT_CLASS_VIOLATION;
745 *text = "invalid structural object class";
746 return LDAP_OBJECT_CLASS_VIOLATION;
751 if( scbv->bv_len == 0 ) {
752 *text = "invalid structural object class";
753 return LDAP_OBJECT_CLASS_VIOLATION;
760 * Return structural object class from list of modifications
762 int mods_structural_class(
766 char *textbuf, size_t textlen )
768 Modifications *ocmod = NULL;
770 for( ; mods != NULL; mods = mods->sml_next ) {
771 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
772 if( ocmod != NULL ) {
773 *text = "entry has multiple objectClass attributes";
774 return LDAP_OBJECT_CLASS_VIOLATION;
780 if( ocmod == NULL ) {
781 *text = "entry has no objectClass attribute";
782 return LDAP_OBJECT_CLASS_VIOLATION;
785 if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
786 *text = "objectClass attribute has no values";
787 return LDAP_OBJECT_CLASS_VIOLATION;
790 return structural_class( ocmod->sml_bvalues, sc, NULL,
791 text, textbuf, textlen );