1 /* schema_check.c - routines to enforce schema definitions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
27 static char * oc_check_required(
30 struct berval *ocname );
32 static int entry_naming_check(
36 char *textbuf, size_t textlen );
38 * entry_schema_check - check that entry e conforms to the schema required
39 * by its object class(es).
41 * returns 0 if so, non-zero otherwise.
52 char *textbuf, size_t textlen )
54 Attribute *a, *asc = NULL, *aoc = NULL;
55 ObjectClass *sc, *oc, **socs = NULL;
59 AttributeDescription *ad_structuralObjectClass
60 = slap_schema.si_ad_structuralObjectClass;
61 AttributeDescription *ad_objectClass
62 = slap_schema.si_ad_objectClass;
64 int subentry = is_entry_subentry( e );
65 int collectiveSubentry = 0;
67 if ( SLAP_NO_SCHEMA_CHECK( op->o_bd )) {
71 if ( get_no_schema_check( op ) ) {
76 collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
81 /* misc attribute checks */
82 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
83 const char *type = a->a_desc->ad_cname.bv_val;
85 /* there should be at least one value */
86 assert( a->a_vals != NULL );
87 assert( a->a_vals[0].bv_val != NULL );
89 if( a->a_desc->ad_type->sat_check ) {
90 rc = (a->a_desc->ad_type->sat_check)(
91 op->o_bd, e, a, text, textbuf, textlen );
92 if( rc != LDAP_SUCCESS ) {
97 if( a->a_desc == ad_structuralObjectClass )
99 else if ( a->a_desc == ad_objectClass )
102 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
103 snprintf( textbuf, textlen,
104 "'%s' can only appear in collectiveAttributeSubentry",
106 return LDAP_OBJECT_CLASS_VIOLATION;
109 /* if single value type, check for multiple values */
110 if( is_at_single_value( a->a_desc->ad_type ) &&
111 a->a_vals[1].bv_val != NULL )
113 snprintf( textbuf, textlen,
114 "attribute '%s' cannot have multiple values",
117 Debug( LDAP_DEBUG_ANY,
119 e->e_dn, textbuf, 0 );
121 return LDAP_CONSTRAINT_VIOLATION;
125 /* check the object class attribute */
127 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
130 *text = "no objectClass attribute";
131 return LDAP_OBJECT_CLASS_VIOLATION;
134 assert( aoc->a_vals != NULL );
135 assert( aoc->a_vals[0].bv_val != NULL );
137 /* check the structural object class attribute */
138 if ( asc == NULL && !add_soc ) {
139 Debug( LDAP_DEBUG_ANY,
140 "No structuralObjectClass for entry (%s)\n",
143 *text = "no structuralObjectClass operational attribute";
147 rc = structural_class( aoc->a_vals, &oc, &socs, text, textbuf, textlen,
149 if( rc != LDAP_SUCCESS ) {
153 if ( asc == NULL && add_soc ) {
154 attr_merge_one( e, ad_structuralObjectClass, &oc->soc_cname, NULL );
155 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
160 assert( asc->a_vals != NULL );
161 assert( asc->a_vals[0].bv_val != NULL );
162 assert( asc->a_vals[1].bv_val == NULL );
164 sc = oc_bvfind( &asc->a_vals[0] );
166 snprintf( textbuf, textlen,
167 "unrecognized structuralObjectClass '%s'",
168 asc->a_vals[0].bv_val );
170 Debug( LDAP_DEBUG_ANY,
171 "entry_check_schema(%s): %s\n",
172 e->e_dn, textbuf, 0 );
174 return LDAP_OBJECT_CLASS_VIOLATION;
177 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
178 snprintf( textbuf, textlen,
179 "structuralObjectClass '%s' is not STRUCTURAL",
180 asc->a_vals[0].bv_val );
182 Debug( LDAP_DEBUG_ANY,
183 "entry_check_schema(%s): %s\n",
184 e->e_dn, textbuf, 0 );
190 if( !manage && sc->soc_obsolete ) {
191 snprintf( textbuf, textlen,
192 "structuralObjectClass '%s' is OBSOLETE",
193 asc->a_vals[0].bv_val );
195 Debug( LDAP_DEBUG_ANY,
196 "entry_check_schema(%s): %s\n",
197 e->e_dn, textbuf, 0 );
199 return LDAP_OBJECT_CLASS_VIOLATION;
205 snprintf( textbuf, textlen,
206 "unrecognized objectClass '%s'",
207 aoc->a_vals[0].bv_val );
208 rc = LDAP_OBJECT_CLASS_VIOLATION;
211 } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
212 snprintf( textbuf, textlen,
213 "structural object class modification "
214 "from '%s' to '%s' not allowed",
215 asc->a_vals[0].bv_val, oc->soc_cname.bv_val );
216 rc = LDAP_NO_OBJECT_CLASS_MODS;
218 } else if ( sc == slap_schema.si_oc_glue ) {
223 if ( !is_entry_glue ( e ) ) {
224 rc = entry_naming_check( e, manage, text, textbuf, textlen );
225 if( rc != LDAP_SUCCESS ) {
232 /* find the content rule for the structural class */
233 cr = cr_find( sc->soc_oid );
235 /* the cr must be same as the structural class */
236 assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
238 /* check that the entry has required attrs of the content rule */
240 if( !manage && cr->scr_obsolete ) {
241 snprintf( textbuf, textlen,
242 "content rule '%s' is obsolete",
243 ldap_contentrule2name( &cr->scr_crule ));
245 Debug( LDAP_DEBUG_ANY,
247 e->e_dn, textbuf, 0 );
249 rc = LDAP_OBJECT_CLASS_VIOLATION;
253 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
254 at = cr->scr_required[i];
256 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
257 if( a->a_desc->ad_type == at ) {
262 /* not there => schema violation */
264 snprintf( textbuf, textlen,
265 "content rule '%s' requires attribute '%s'",
266 ldap_contentrule2name( &cr->scr_crule ),
267 at->sat_cname.bv_val );
269 Debug( LDAP_DEBUG_ANY,
271 e->e_dn, textbuf, 0 );
273 rc = LDAP_OBJECT_CLASS_VIOLATION;
278 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
279 at = cr->scr_precluded[i];
281 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
282 if( a->a_desc->ad_type == at ) {
287 /* there => schema violation */
289 snprintf( textbuf, textlen,
290 "content rule '%s' precluded attribute '%s'",
291 ldap_contentrule2name( &cr->scr_crule ),
292 at->sat_cname.bv_val );
294 Debug( LDAP_DEBUG_ANY,
296 e->e_dn, textbuf, 0 );
298 rc = LDAP_OBJECT_CLASS_VIOLATION;
304 /* check that the entry has required attrs for each oc */
305 for ( i = 0; socs[i]; i++ ) {
307 if ( !manage && oc->soc_obsolete ) {
308 /* disallow obsolete classes */
309 snprintf( textbuf, textlen,
310 "objectClass '%s' is OBSOLETE",
311 aoc->a_vals[i].bv_val );
313 Debug( LDAP_DEBUG_ANY,
314 "entry_check_schema(%s): %s\n",
315 e->e_dn, textbuf, 0 );
317 rc = LDAP_OBJECT_CLASS_VIOLATION;
321 if ( oc->soc_check ) {
322 rc = (oc->soc_check)( op->o_bd, e, oc,
323 text, textbuf, textlen );
324 if( rc != LDAP_SUCCESS ) {
329 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
330 /* object class is abstract */
331 if ( oc != slap_schema.si_oc_top &&
332 !is_object_subclass( oc, sc ))
335 ObjectClass *xc = NULL;
336 for( j=0; socs[j]; j++ ) {
340 /* since we previous check against the
341 * structural object of this entry, the
342 * abstract class must be a (direct or indirect)
343 * superclass of one of the auxiliary classes of
346 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
347 is_object_subclass( oc, xc ) )
356 snprintf( textbuf, textlen, "instanstantiation of "
357 "abstract objectClass '%s' not allowed",
358 aoc->a_vals[i].bv_val );
360 Debug( LDAP_DEBUG_ANY,
361 "entry_check_schema(%s): %s\n",
362 e->e_dn, textbuf, 0 );
364 rc = LDAP_OBJECT_CLASS_VIOLATION;
369 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
372 if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
379 if( cr->scr_auxiliaries ) {
380 for( j = 0; cr->scr_auxiliaries[j]; j++ ) {
381 if( cr->scr_auxiliaries[j] == oc ) {
387 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
394 snprintf( textbuf, textlen,
395 "content rule '%s' does not allow class '%s'",
396 ldap_contentrule2name( &cr->scr_crule ),
397 oc->soc_cname.bv_val );
399 Debug( LDAP_DEBUG_ANY,
401 e->e_dn, textbuf, 0 );
403 rc = LDAP_OBJECT_CLASS_VIOLATION;
408 s = oc_check_required( e, oc, &aoc->a_vals[i] );
410 snprintf( textbuf, textlen,
411 "object class '%s' requires attribute '%s'",
412 aoc->a_vals[i].bv_val, s );
414 Debug( LDAP_DEBUG_ANY,
416 e->e_dn, textbuf, 0 );
418 rc = LDAP_OBJECT_CLASS_VIOLATION;
422 if( oc == slap_schema.si_oc_extensibleObject ) {
434 /* check that each attr in the entry is allowed by some oc */
435 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
436 rc = LDAP_OBJECT_CLASS_VIOLATION;
438 if( cr && cr->scr_required ) {
439 for( i=0; cr->scr_required[i]; i++ ) {
440 if( cr->scr_required[i] == a->a_desc->ad_type ) {
447 if( rc != LDAP_SUCCESS && cr && cr->scr_allowed ) {
448 for( i=0; cr->scr_allowed[i]; i++ ) {
449 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
456 if( rc != LDAP_SUCCESS )
458 rc = oc_check_allowed( a->a_desc->ad_type, socs, sc );
461 if ( rc != LDAP_SUCCESS ) {
462 char *type = a->a_desc->ad_cname.bv_val;
464 snprintf( textbuf, textlen,
465 "attribute '%s' not allowed",
468 Debug( LDAP_DEBUG_ANY,
470 e->e_dn, textbuf, 0 );
478 slap_sl_free( socs, op->o_tmpmemctx );
486 struct berval *ocname )
492 Debug( LDAP_DEBUG_TRACE,
493 "oc_check_required entry (%s), objectClass \"%s\"\n",
494 e->e_dn, ocname->bv_val, 0 );
497 /* check for empty oc_required */
498 if(oc->soc_required == NULL) {
502 /* for each required attribute */
503 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
504 at = oc->soc_required[i];
505 /* see if it's in the entry */
506 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
507 if( a->a_desc->ad_type == at ) {
511 /* not there => schema violation */
513 return at->sat_cname.bv_val;
520 int oc_check_allowed(
527 Debug( LDAP_DEBUG_TRACE,
528 "oc_check_allowed type \"%s\"\n",
529 at->sat_cname.bv_val, 0, 0 );
531 /* always allow objectClass attribute */
532 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
537 * All operational attributions are allowed by schema rules.
539 if( is_at_operational(at) ) {
543 /* check to see if its allowed by the structuralObjectClass */
545 /* does it require the type? */
546 for ( j = 0; sc->soc_required != NULL &&
547 sc->soc_required[j] != NULL; j++ )
549 if( at == sc->soc_required[j] ) {
554 /* does it allow the type? */
555 for ( j = 0; sc->soc_allowed != NULL &&
556 sc->soc_allowed[j] != NULL; j++ )
558 if( at == sc->soc_allowed[j] ) {
564 /* check that the type appears as req or opt in at least one oc */
565 for ( i = 0; socs[i]; i++ ) {
566 /* if we know about the oc */
567 ObjectClass *oc = socs[i];
568 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
569 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
571 /* does it require the type? */
572 for ( j = 0; oc->soc_required != NULL &&
573 oc->soc_required[j] != NULL; j++ )
575 if( at == oc->soc_required[j] ) {
579 /* does it allow the type? */
580 for ( j = 0; oc->soc_allowed != NULL &&
581 oc->soc_allowed[j] != NULL; j++ )
583 if( at == oc->soc_allowed[j] ) {
590 /* not allowed by any oc */
591 return LDAP_OBJECT_CLASS_VIOLATION;
595 * Determine the structural object class from a set of OIDs
597 int structural_class(
600 ObjectClass ***socsp,
602 char *textbuf, size_t textlen,
606 ObjectClass *oc, **socs;
607 ObjectClass *sc = NULL;
610 *text = "structural_class: internal error";
613 for( i=0; ocs[i].bv_val; i++ ) ;
616 socs = slap_sl_malloc( (nocs+1) * sizeof(ObjectClass *), ctx );
618 for( i=0; ocs[i].bv_val; i++ ) {
619 socs[i] = oc_bvfind( &ocs[i] );
621 if( socs[i] == NULL ) {
622 snprintf( textbuf, textlen,
623 "unrecognized objectClass '%s'",
631 for( i=0; ocs[i].bv_val; i++ ) {
633 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
634 if( sc == NULL || is_object_subclass( sc, oc ) ) {
638 } else if ( !is_object_subclass( oc, sc ) ) {
640 ObjectClass *xc = NULL;
642 /* find common superior */
643 for( j=i+1; ocs[j].bv_val; j++ ) {
647 snprintf( textbuf, textlen,
648 "unrecognized objectClass '%s'",
654 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
659 if( is_object_subclass( sc, xc ) &&
660 is_object_subclass( oc, xc ) )
662 /* found common subclass */
670 /* no common subclass */
671 snprintf( textbuf, textlen,
672 "invalid structural object class chain (%s/%s)",
673 ocs[scn].bv_val, ocs[i].bv_val );
686 *text = "no structural object class provided";
691 *text = "invalid structural object class";
698 slap_sl_free( socs, ctx );
705 slap_sl_free( socs, ctx );
706 return LDAP_OBJECT_CLASS_VIOLATION;
710 * Return structural object class from list of modifications
712 int mods_structural_class(
716 char *textbuf, size_t textlen, void *ctx )
718 Modifications *ocmod = NULL;
722 for( ; mods != NULL; mods = mods->sml_next ) {
723 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
724 if( ocmod != NULL ) {
725 *text = "entry has multiple objectClass attributes";
726 return LDAP_OBJECT_CLASS_VIOLATION;
732 if( ocmod == NULL ) {
733 *text = "entry has no objectClass attribute";
734 return LDAP_OBJECT_CLASS_VIOLATION;
737 if( ocmod->sml_values == NULL || ocmod->sml_values[0].bv_val == NULL ) {
738 *text = "objectClass attribute has no values";
739 return LDAP_OBJECT_CLASS_VIOLATION;
742 rc = structural_class( ocmod->sml_values, &ssc, NULL,
743 text, textbuf, textlen, ctx );
744 if ( rc == LDAP_SUCCESS )
745 *sc = ssc->soc_cname;
755 char *textbuf, size_t textlen )
759 const char *p = NULL;
761 int rc = LDAP_SUCCESS;
763 if ( BER_BVISEMPTY( &e->e_name )) {
768 * Get attribute type(s) and attribute value(s) of our RDN
770 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
771 LDAP_DN_FORMAT_LDAP ) )
773 *text = "unrecongized attribute type(s) in RDN";
774 return LDAP_INVALID_DN_SYNTAX;
777 /* Check that each AVA of the RDN is present in the entry */
778 /* FIXME: Should also check that each AVA lists a distinct type */
779 for ( cnt = 0; rdn[cnt]; cnt++ ) {
780 LDAPAVA *ava = rdn[cnt];
781 AttributeDescription *desc = NULL;
785 if( ava->la_flags & LDAP_AVA_BINARY ) {
786 snprintf( textbuf, textlen,
787 "value of naming attribute '%s' in unsupported BER form",
788 ava->la_attr.bv_val );
789 rc = LDAP_NAMING_VIOLATION;
792 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
793 if ( rc != LDAP_SUCCESS ) {
794 snprintf( textbuf, textlen, "%s (in RDN)", errtext );
798 if( desc->ad_type->sat_usage ) {
799 snprintf( textbuf, textlen,
800 "naming attribute '%s' is operational",
801 ava->la_attr.bv_val );
802 rc = LDAP_NAMING_VIOLATION;
806 if( desc->ad_type->sat_collective ) {
807 snprintf( textbuf, textlen,
808 "naming attribute '%s' is collective",
809 ava->la_attr.bv_val );
810 rc = LDAP_NAMING_VIOLATION;
814 if( !manage && desc->ad_type->sat_obsolete ) {
815 snprintf( textbuf, textlen,
816 "naming attribute '%s' is obsolete",
817 ava->la_attr.bv_val );
818 rc = LDAP_NAMING_VIOLATION;
822 if( !desc->ad_type->sat_equality ) {
823 snprintf( textbuf, textlen,
824 "naming attribute '%s' has no equality matching rule",
825 ava->la_attr.bv_val );
826 rc = LDAP_NAMING_VIOLATION;
830 if( !desc->ad_type->sat_equality->smr_match ) {
831 snprintf( textbuf, textlen,
832 "naming attribute '%s' has unsupported equality matching rule",
833 ava->la_attr.bv_val );
834 rc = LDAP_NAMING_VIOLATION;
838 /* find the naming attribute */
839 attr = attr_find( e->e_attrs, desc );
840 if ( attr == NULL ) {
841 snprintf( textbuf, textlen,
842 "naming attribute '%s' is not present in entry",
843 ava->la_attr.bv_val );
844 rc = LDAP_NAMING_VIOLATION;
848 rc = value_find_ex( desc, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX|
849 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
850 attr->a_nvals, &ava->la_value, NULL );
854 case LDAP_INAPPROPRIATE_MATCHING:
855 snprintf( textbuf, textlen,
856 "inappropriate matching for naming attribute '%s'",
857 ava->la_attr.bv_val );
859 case LDAP_INVALID_SYNTAX:
860 snprintf( textbuf, textlen,
861 "value of naming attribute '%s' is invalid",
862 ava->la_attr.bv_val );
864 case LDAP_NO_SUCH_ATTRIBUTE:
865 snprintf( textbuf, textlen,
866 "value of naming attribute '%s' is not present in entry",
867 ava->la_attr.bv_val );
870 snprintf( textbuf, textlen,
871 "naming attribute '%s' is inappropriate",
872 ava->la_attr.bv_val );
874 rc = LDAP_NAMING_VIOLATION;