1 /* schema_check.c - routines to enforce schema definitions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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>
28 static char * oc_check_required(
31 struct berval *ocname );
33 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.
50 char *textbuf, size_t textlen )
52 Attribute *a, *asc, *aoc;
58 AttributeDescription *ad_structuralObjectClass
59 = slap_schema.si_ad_structuralObjectClass;
60 AttributeDescription *ad_objectClass
61 = slap_schema.si_ad_objectClass;
63 int subentry = is_entry_subentry( e );
64 int collectiveSubentry = 0;
66 if ( SLAP_NO_SCHEMA_CHECK( be )) {
71 collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
76 /* misc attribute checks */
77 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
78 const char *type = a->a_desc->ad_cname.bv_val;
80 /* there should be at least one value */
82 assert( a->a_vals[0].bv_val != NULL );
84 if( a->a_desc->ad_type->sat_check ) {
85 int rc = (a->a_desc->ad_type->sat_check)(
86 be, e, a, text, textbuf, textlen );
87 if( rc != LDAP_SUCCESS ) {
92 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
93 snprintf( textbuf, textlen,
94 "'%s' can only appear in collectiveAttributeSubentry",
96 return LDAP_OBJECT_CLASS_VIOLATION;
99 /* if single value type, check for multiple values */
100 if( is_at_single_value( a->a_desc->ad_type ) &&
101 a->a_vals[1].bv_val != NULL )
103 snprintf( textbuf, textlen,
104 "attribute '%s' cannot have multiple values",
107 Debug( LDAP_DEBUG_ANY,
109 e->e_dn, textbuf, 0 );
111 return LDAP_CONSTRAINT_VIOLATION;
115 /* it's a REALLY bad idea to disable schema checks */
116 if( !global_schemacheck ) return LDAP_SUCCESS;
118 /* find the structural object class attribute */
119 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
121 Debug( LDAP_DEBUG_ANY,
122 "No structuralObjectClass for entry (%s)\n",
125 *text = "no structuralObjectClass operational attribute";
129 assert( asc->a_vals != NULL );
130 assert( asc->a_vals[0].bv_val != NULL );
131 assert( asc->a_vals[1].bv_val == NULL );
133 sc = oc_bvfind( &asc->a_vals[0] );
135 snprintf( textbuf, textlen,
136 "unrecognized structuralObjectClass '%s'",
137 asc->a_vals[0].bv_val );
139 Debug( LDAP_DEBUG_ANY,
140 "entry_check_schema(%s): %s\n",
141 e->e_dn, textbuf, 0 );
143 return LDAP_OBJECT_CLASS_VIOLATION;
146 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
147 snprintf( textbuf, textlen,
148 "structuralObjectClass '%s' is not STRUCTURAL",
149 asc->a_vals[0].bv_val );
151 Debug( LDAP_DEBUG_ANY,
152 "entry_check_schema(%s): %s\n",
153 e->e_dn, textbuf, 0 );
158 if( sc->soc_obsolete ) {
159 snprintf( textbuf, textlen,
160 "structuralObjectClass '%s' is OBSOLETE",
161 asc->a_vals[0].bv_val );
163 Debug( LDAP_DEBUG_ANY,
164 "entry_check_schema(%s): %s\n",
165 e->e_dn, textbuf, 0 );
167 return LDAP_OBJECT_CLASS_VIOLATION;
170 /* find the object class attribute */
171 aoc = attr_find( e->e_attrs, ad_objectClass );
173 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
176 *text = "no objectClass attribute";
177 return LDAP_OBJECT_CLASS_VIOLATION;
180 assert( aoc->a_vals != NULL );
181 assert( aoc->a_vals[0].bv_val != NULL );
183 rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
184 if( rc != LDAP_SUCCESS ) {
191 snprintf( textbuf, textlen,
192 "unrecognized objectClass '%s'",
193 aoc->a_vals[0].bv_val );
194 return LDAP_OBJECT_CLASS_VIOLATION;
196 } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
197 snprintf( textbuf, textlen,
198 "structural object class modification "
199 "from '%s' to '%s' not allowed",
200 asc->a_vals[0].bv_val, nsc.bv_val );
201 return LDAP_NO_OBJECT_CLASS_MODS;
202 } else if ( sc == slap_schema.si_oc_glue ) {
207 if ( !is_entry_objectclass ( e, slap_schema.si_oc_glue, 0 ) ) {
208 rc = entry_naming_check( e, text, textbuf, textlen );
209 if( rc != LDAP_SUCCESS ) {
216 /* find the content rule for the structural class */
217 cr = cr_find( sc->soc_oid );
219 /* the cr must be same as the structural class */
220 assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
222 /* check that the entry has required attrs of the content rule */
224 if( cr->scr_obsolete ) {
225 snprintf( textbuf, textlen,
226 "content rule '%s' is obsolete",
227 ldap_contentrule2name( &cr->scr_crule ));
229 Debug( LDAP_DEBUG_ANY,
231 e->e_dn, textbuf, 0 );
233 return LDAP_OBJECT_CLASS_VIOLATION;
236 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
237 at = cr->scr_required[i];
239 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
240 if( a->a_desc->ad_type == at ) {
245 /* not there => schema violation */
247 snprintf( textbuf, textlen,
248 "content rule '%s' requires attribute '%s'",
249 ldap_contentrule2name( &cr->scr_crule ),
250 at->sat_cname.bv_val );
252 Debug( LDAP_DEBUG_ANY,
254 e->e_dn, textbuf, 0 );
256 return LDAP_OBJECT_CLASS_VIOLATION;
260 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
261 at = cr->scr_precluded[i];
263 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
264 if( a->a_desc->ad_type == at ) {
269 /* there => schema violation */
271 snprintf( textbuf, textlen,
272 "content rule '%s' precluded attribute '%s'",
273 ldap_contentrule2name( &cr->scr_crule ),
274 at->sat_cname.bv_val );
276 Debug( LDAP_DEBUG_ANY,
278 e->e_dn, textbuf, 0 );
280 return LDAP_OBJECT_CLASS_VIOLATION;
285 /* check that the entry has required attrs for each oc */
286 for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
287 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
288 snprintf( textbuf, textlen,
289 "unrecognized objectClass '%s'",
290 aoc->a_vals[i].bv_val );
292 Debug( LDAP_DEBUG_ANY,
293 "entry_check_schema(%s): %s\n",
294 e->e_dn, textbuf, 0 );
296 return LDAP_OBJECT_CLASS_VIOLATION;
299 if ( oc->soc_obsolete ) {
300 /* disallow obsolete classes */
301 snprintf( textbuf, textlen,
302 "objectClass '%s' is OBSOLETE",
303 aoc->a_vals[i].bv_val );
305 Debug( LDAP_DEBUG_ANY,
306 "entry_check_schema(%s): %s\n",
307 e->e_dn, textbuf, 0 );
309 return LDAP_OBJECT_CLASS_VIOLATION;
312 if ( oc->soc_check ) {
313 int rc = (oc->soc_check)( be, e, oc,
314 text, textbuf, textlen );
315 if( rc != LDAP_SUCCESS ) {
320 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
321 /* object class is abstract */
322 if ( oc != slap_schema.si_oc_top &&
323 !is_object_subclass( oc, sc ))
326 ObjectClass *xc = NULL;
327 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
329 xc = oc_bvfind( &aoc->a_vals[i] );
331 snprintf( textbuf, textlen,
332 "unrecognized objectClass '%s'",
333 aoc->a_vals[i].bv_val );
335 Debug( LDAP_DEBUG_ANY,
336 "entry_check_schema(%s): %s\n",
337 e->e_dn, textbuf, 0 );
339 return LDAP_OBJECT_CLASS_VIOLATION;
342 /* since we previous check against the
343 * structural object of this entry, the
344 * abstract class must be a (direct or indirect)
345 * superclass of one of the auxiliary classes of
348 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
349 is_object_subclass( oc, xc ) )
358 snprintf( textbuf, textlen, "instanstantiation of "
359 "abstract objectClass '%s' not allowed",
360 aoc->a_vals[i].bv_val );
362 Debug( LDAP_DEBUG_ANY,
363 "entry_check_schema(%s): %s\n",
364 e->e_dn, textbuf, 0 );
366 return LDAP_OBJECT_CLASS_VIOLATION;
370 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
373 if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
380 if( cr->scr_auxiliaries ) {
381 for( j = 0; cr->scr_auxiliaries[j]; j++ ) {
382 if( cr->scr_auxiliaries[j] == oc ) {
388 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
395 snprintf( textbuf, textlen,
396 "content rule '%s' does not allow class '%s'",
397 ldap_contentrule2name( &cr->scr_crule ),
398 oc->soc_cname.bv_val );
400 Debug( LDAP_DEBUG_ANY,
402 e->e_dn, textbuf, 0 );
404 return 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 return LDAP_OBJECT_CLASS_VIOLATION;
421 if( oc == slap_schema.si_oc_extensibleObject ) {
431 /* check that each attr in the entry is allowed by some oc */
432 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
435 ret = LDAP_OBJECT_CLASS_VIOLATION;
437 if( cr && cr->scr_required ) {
438 for( i=0; cr->scr_required[i]; i++ ) {
439 if( cr->scr_required[i] == a->a_desc->ad_type ) {
446 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
447 for( i=0; cr->scr_allowed[i]; i++ ) {
448 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
455 if( ret != LDAP_SUCCESS )
457 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
460 if ( ret != LDAP_SUCCESS ) {
461 char *type = a->a_desc->ad_cname.bv_val;
463 snprintf( textbuf, textlen,
464 "attribute '%s' not allowed",
467 Debug( LDAP_DEBUG_ANY,
469 e->e_dn, textbuf, 0 );
482 struct berval *ocname )
488 Debug( LDAP_DEBUG_TRACE,
489 "oc_check_required entry (%s), objectClass \"%s\"\n",
490 e->e_dn, ocname->bv_val, 0 );
493 /* check for empty oc_required */
494 if(oc->soc_required == NULL) {
498 /* for each required attribute */
499 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
500 at = oc->soc_required[i];
501 /* see if it's in the entry */
502 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
503 if( a->a_desc->ad_type == at ) {
507 /* not there => schema violation */
509 return at->sat_cname.bv_val;
516 int oc_check_allowed(
523 Debug( LDAP_DEBUG_TRACE,
524 "oc_check_allowed type \"%s\"\n",
525 at->sat_cname.bv_val, 0, 0 );
527 /* always allow objectClass attribute */
528 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
533 * All operational attributions are allowed by schema rules.
535 if( is_at_operational(at) ) {
539 /* check to see if its allowed by the structuralObjectClass */
541 /* does it require the type? */
542 for ( j = 0; sc->soc_required != NULL &&
543 sc->soc_required[j] != NULL; j++ )
545 if( at == sc->soc_required[j] ) {
550 /* does it allow the type? */
551 for ( j = 0; sc->soc_allowed != NULL &&
552 sc->soc_allowed[j] != NULL; j++ )
554 if( at == sc->soc_allowed[j] ) {
560 /* check that the type appears as req or opt in at least one oc */
561 for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
562 /* if we know about the oc */
563 ObjectClass *oc = oc_bvfind( &ocl[i] );
564 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
565 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
567 /* does it require the type? */
568 for ( j = 0; oc->soc_required != NULL &&
569 oc->soc_required[j] != NULL; j++ )
571 if( at == oc->soc_required[j] ) {
575 /* does it allow the type? */
576 for ( j = 0; oc->soc_allowed != NULL &&
577 oc->soc_allowed[j] != NULL; j++ )
579 if( at == oc->soc_allowed[j] ) {
586 /* not allowed by any oc */
587 return LDAP_OBJECT_CLASS_VIOLATION;
591 * Determine the structural object class from a set of OIDs
593 int structural_class(
598 char *textbuf, size_t textlen )
602 ObjectClass *sc = NULL;
605 *text = "structural_class: internal error";
608 for( i=0; ocs[i].bv_val; i++ ) {
609 oc = oc_bvfind( &ocs[i] );
612 snprintf( textbuf, textlen,
613 "unrecognized objectClass '%s'",
616 return LDAP_OBJECT_CLASS_VIOLATION;
619 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
620 if( sc == NULL || is_object_subclass( sc, oc ) ) {
624 } else if ( !is_object_subclass( oc, sc ) ) {
626 ObjectClass *xc = NULL;
628 /* find common superior */
629 for( j=i+1; ocs[j].bv_val; j++ ) {
630 xc = oc_bvfind( &ocs[j] );
633 snprintf( textbuf, textlen,
634 "unrecognized objectClass '%s'",
637 return LDAP_OBJECT_CLASS_VIOLATION;
640 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
645 if( is_object_subclass( sc, xc ) &&
646 is_object_subclass( oc, xc ) )
648 /* found common subclass */
656 /* no common subclass */
657 snprintf( textbuf, textlen,
658 "invalid structural object class chain (%s/%s)",
659 ocs[scn].bv_val, ocs[i].bv_val );
661 return LDAP_OBJECT_CLASS_VIOLATION;
672 *text = "no structural object class provided";
673 return LDAP_OBJECT_CLASS_VIOLATION;
677 *text = "invalid structural object class";
678 return LDAP_OBJECT_CLASS_VIOLATION;
683 if( scbv->bv_len == 0 ) {
684 *text = "invalid structural object class";
685 return LDAP_OBJECT_CLASS_VIOLATION;
692 * Return structural object class from list of modifications
694 int mods_structural_class(
698 char *textbuf, size_t textlen )
700 Modifications *ocmod = NULL;
702 for( ; mods != NULL; mods = mods->sml_next ) {
703 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
704 if( ocmod != NULL ) {
705 *text = "entry has multiple objectClass attributes";
706 return LDAP_OBJECT_CLASS_VIOLATION;
712 if( ocmod == NULL ) {
713 *text = "entry has no objectClass attribute";
714 return LDAP_OBJECT_CLASS_VIOLATION;
717 if( ocmod->sml_values == NULL || ocmod->sml_values[0].bv_val == NULL ) {
718 *text = "objectClass attribute has no values";
719 return LDAP_OBJECT_CLASS_VIOLATION;
722 return structural_class( ocmod->sml_values, sc, NULL,
723 text, textbuf, textlen );
731 char *textbuf, size_t textlen )
735 const char *p = NULL;
737 int rc = LDAP_SUCCESS;
740 * Get attribute type(s) and attribute value(s) of our RDN
742 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
743 LDAP_DN_FORMAT_LDAP ) )
745 *text = "unrecongized attribute type(s) in RDN";
746 return LDAP_INVALID_DN_SYNTAX;
749 /* Check that each AVA of the RDN is present in the entry */
750 /* FIXME: Should also check that each AVA lists a distinct type */
751 for ( cnt = 0; rdn[cnt]; cnt++ ) {
752 LDAPAVA *ava = rdn[cnt];
753 AttributeDescription *desc = NULL;
757 if( ava->la_flags & LDAP_AVA_BINARY ) {
758 snprintf( textbuf, textlen,
759 "value of naming attribute '%s' in unsupported BER form",
760 ava->la_attr.bv_val );
761 rc = LDAP_NAMING_VIOLATION;
764 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
765 if ( rc != LDAP_SUCCESS ) {
766 snprintf( textbuf, textlen, "%s (in RDN)", errtext );
770 if( desc->ad_type->sat_usage ) {
771 snprintf( textbuf, textlen,
772 "naming attribute '%s' is operational",
773 ava->la_attr.bv_val );
774 rc = LDAP_NAMING_VIOLATION;
778 if( desc->ad_type->sat_collective ) {
779 snprintf( textbuf, textlen,
780 "naming attribute '%s' is collective",
781 ava->la_attr.bv_val );
782 rc = LDAP_NAMING_VIOLATION;
786 if( desc->ad_type->sat_obsolete ) {
787 snprintf( textbuf, textlen,
788 "naming attribute '%s' is obsolete",
789 ava->la_attr.bv_val );
790 rc = LDAP_NAMING_VIOLATION;
794 if( !desc->ad_type->sat_equality ) {
795 snprintf( textbuf, textlen,
796 "naming attribute '%s' has no equality matching rule",
797 ava->la_attr.bv_val );
798 rc = LDAP_NAMING_VIOLATION;
802 if( !desc->ad_type->sat_equality->smr_match ) {
803 snprintf( textbuf, textlen,
804 "naming attribute '%s' has unsupported equality matching rule",
805 ava->la_attr.bv_val );
806 rc = LDAP_NAMING_VIOLATION;
810 /* find the naming attribute */
811 attr = attr_find( e->e_attrs, desc );
812 if ( attr == NULL ) {
813 snprintf( textbuf, textlen,
814 "naming attribute '%s' is not present in entry",
815 ava->la_attr.bv_val );
816 rc = LDAP_NAMING_VIOLATION;
820 rc = value_find_ex( desc, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX|
821 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
822 attr->a_nvals, &ava->la_value, NULL );
826 case LDAP_INAPPROPRIATE_MATCHING:
827 snprintf( textbuf, textlen,
828 "inappropriate matching for naming attribute '%s'",
829 ava->la_attr.bv_val );
831 case LDAP_INVALID_SYNTAX:
832 snprintf( textbuf, textlen,
833 "value of naming attribute '%s' is invalid",
834 ava->la_attr.bv_val );
836 case LDAP_NO_SUCH_ATTRIBUTE:
837 snprintf( textbuf, textlen,
838 "value of naming attribute '%s' is not present in entry",
839 ava->la_attr.bv_val );
842 snprintf( textbuf, textlen,
843 "naming attribute '%s' is inappropriate",
844 ava->la_attr.bv_val );
846 rc = LDAP_NAMING_VIOLATION;