1 /* schema_check.c - routines to enforce schema definitions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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.
51 char *textbuf, size_t textlen )
53 Attribute *a, *asc, *aoc;
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( be )) {
72 collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
77 /* misc attribute checks */
78 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
79 const char *type = a->a_desc->ad_cname.bv_val;
81 /* there should be at least one value */
83 assert( a->a_vals[0].bv_val != NULL );
85 if( a->a_desc->ad_type->sat_check ) {
86 int rc = (a->a_desc->ad_type->sat_check)(
87 be, e, a, text, textbuf, textlen );
88 if( rc != LDAP_SUCCESS ) {
93 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
94 snprintf( textbuf, textlen,
95 "'%s' can only appear in collectiveAttributeSubentry",
97 return LDAP_OBJECT_CLASS_VIOLATION;
100 /* if single value type, check for multiple values */
101 if( is_at_single_value( a->a_desc->ad_type ) &&
102 a->a_vals[1].bv_val != NULL )
104 snprintf( textbuf, textlen,
105 "attribute '%s' cannot have multiple values",
108 Debug( LDAP_DEBUG_ANY,
110 e->e_dn, textbuf, 0 );
112 return LDAP_CONSTRAINT_VIOLATION;
116 /* find the structural object class attribute */
117 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
119 Debug( LDAP_DEBUG_ANY,
120 "No structuralObjectClass for entry (%s)\n",
123 *text = "no structuralObjectClass operational attribute";
127 assert( asc->a_vals != NULL );
128 assert( asc->a_vals[0].bv_val != NULL );
129 assert( asc->a_vals[1].bv_val == NULL );
131 sc = oc_bvfind( &asc->a_vals[0] );
133 snprintf( textbuf, textlen,
134 "unrecognized structuralObjectClass '%s'",
135 asc->a_vals[0].bv_val );
137 Debug( LDAP_DEBUG_ANY,
138 "entry_check_schema(%s): %s\n",
139 e->e_dn, textbuf, 0 );
141 return LDAP_OBJECT_CLASS_VIOLATION;
144 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
145 snprintf( textbuf, textlen,
146 "structuralObjectClass '%s' is not STRUCTURAL",
147 asc->a_vals[0].bv_val );
149 Debug( LDAP_DEBUG_ANY,
150 "entry_check_schema(%s): %s\n",
151 e->e_dn, textbuf, 0 );
156 if( !manage && sc->soc_obsolete ) {
157 snprintf( textbuf, textlen,
158 "structuralObjectClass '%s' is OBSOLETE",
159 asc->a_vals[0].bv_val );
161 Debug( LDAP_DEBUG_ANY,
162 "entry_check_schema(%s): %s\n",
163 e->e_dn, textbuf, 0 );
165 return LDAP_OBJECT_CLASS_VIOLATION;
168 /* find the object class attribute */
169 aoc = attr_find( e->e_attrs, ad_objectClass );
171 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
174 *text = "no objectClass attribute";
175 return LDAP_OBJECT_CLASS_VIOLATION;
178 assert( aoc->a_vals != NULL );
179 assert( aoc->a_vals[0].bv_val != NULL );
181 rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
182 if( rc != LDAP_SUCCESS ) {
189 snprintf( textbuf, textlen,
190 "unrecognized objectClass '%s'",
191 aoc->a_vals[0].bv_val );
192 return LDAP_OBJECT_CLASS_VIOLATION;
194 } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
195 snprintf( textbuf, textlen,
196 "structural object class modification "
197 "from '%s' to '%s' not allowed",
198 asc->a_vals[0].bv_val, nsc.bv_val );
199 return LDAP_NO_OBJECT_CLASS_MODS;
200 } else if ( sc == slap_schema.si_oc_glue ) {
205 if ( !is_entry_objectclass ( e, slap_schema.si_oc_glue, 0 ) ) {
206 rc = entry_naming_check( e, manage, text, textbuf, textlen );
207 if( rc != LDAP_SUCCESS ) {
214 /* find the content rule for the structural class */
215 cr = cr_find( sc->soc_oid );
217 /* the cr must be same as the structural class */
218 assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
220 /* check that the entry has required attrs of the content rule */
222 if( !manage && cr->scr_obsolete ) {
223 snprintf( textbuf, textlen,
224 "content rule '%s' is obsolete",
225 ldap_contentrule2name( &cr->scr_crule ));
227 Debug( LDAP_DEBUG_ANY,
229 e->e_dn, textbuf, 0 );
231 return LDAP_OBJECT_CLASS_VIOLATION;
234 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
235 at = cr->scr_required[i];
237 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
238 if( a->a_desc->ad_type == at ) {
243 /* not there => schema violation */
245 snprintf( textbuf, textlen,
246 "content rule '%s' requires attribute '%s'",
247 ldap_contentrule2name( &cr->scr_crule ),
248 at->sat_cname.bv_val );
250 Debug( LDAP_DEBUG_ANY,
252 e->e_dn, textbuf, 0 );
254 return LDAP_OBJECT_CLASS_VIOLATION;
258 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
259 at = cr->scr_precluded[i];
261 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
262 if( a->a_desc->ad_type == at ) {
267 /* there => schema violation */
269 snprintf( textbuf, textlen,
270 "content rule '%s' precluded attribute '%s'",
271 ldap_contentrule2name( &cr->scr_crule ),
272 at->sat_cname.bv_val );
274 Debug( LDAP_DEBUG_ANY,
276 e->e_dn, textbuf, 0 );
278 return LDAP_OBJECT_CLASS_VIOLATION;
283 /* check that the entry has required attrs for each oc */
284 for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
285 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
286 snprintf( textbuf, textlen,
287 "unrecognized objectClass '%s'",
288 aoc->a_vals[i].bv_val );
290 Debug( LDAP_DEBUG_ANY,
291 "entry_check_schema(%s): %s\n",
292 e->e_dn, textbuf, 0 );
294 return LDAP_OBJECT_CLASS_VIOLATION;
297 if ( !manage && oc->soc_obsolete ) {
298 /* disallow obsolete classes */
299 snprintf( textbuf, textlen,
300 "objectClass '%s' is OBSOLETE",
301 aoc->a_vals[i].bv_val );
303 Debug( LDAP_DEBUG_ANY,
304 "entry_check_schema(%s): %s\n",
305 e->e_dn, textbuf, 0 );
307 return LDAP_OBJECT_CLASS_VIOLATION;
310 if ( oc->soc_check ) {
311 int rc = (oc->soc_check)( be, e, oc,
312 text, textbuf, textlen );
313 if( rc != LDAP_SUCCESS ) {
318 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
319 /* object class is abstract */
320 if ( oc != slap_schema.si_oc_top &&
321 !is_object_subclass( oc, sc ))
324 ObjectClass *xc = NULL;
325 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
327 xc = oc_bvfind( &aoc->a_vals[i] );
329 snprintf( textbuf, textlen,
330 "unrecognized objectClass '%s'",
331 aoc->a_vals[i].bv_val );
333 Debug( LDAP_DEBUG_ANY,
334 "entry_check_schema(%s): %s\n",
335 e->e_dn, textbuf, 0 );
337 return LDAP_OBJECT_CLASS_VIOLATION;
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 return LDAP_OBJECT_CLASS_VIOLATION;
368 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
371 if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
378 if( cr->scr_auxiliaries ) {
379 for( j = 0; cr->scr_auxiliaries[j]; j++ ) {
380 if( cr->scr_auxiliaries[j] == oc ) {
386 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
393 snprintf( textbuf, textlen,
394 "content rule '%s' does not allow class '%s'",
395 ldap_contentrule2name( &cr->scr_crule ),
396 oc->soc_cname.bv_val );
398 Debug( LDAP_DEBUG_ANY,
400 e->e_dn, textbuf, 0 );
402 return LDAP_OBJECT_CLASS_VIOLATION;
406 s = oc_check_required( e, oc, &aoc->a_vals[i] );
408 snprintf( textbuf, textlen,
409 "object class '%s' requires attribute '%s'",
410 aoc->a_vals[i].bv_val, s );
412 Debug( LDAP_DEBUG_ANY,
414 e->e_dn, textbuf, 0 );
416 return LDAP_OBJECT_CLASS_VIOLATION;
419 if( oc == slap_schema.si_oc_extensibleObject ) {
430 /* check that each attr in the entry is allowed by some oc */
431 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
434 ret = LDAP_OBJECT_CLASS_VIOLATION;
436 if( cr && cr->scr_required ) {
437 for( i=0; cr->scr_required[i]; i++ ) {
438 if( cr->scr_required[i] == a->a_desc->ad_type ) {
445 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
446 for( i=0; cr->scr_allowed[i]; i++ ) {
447 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
454 if( ret != LDAP_SUCCESS )
456 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
459 if ( ret != LDAP_SUCCESS ) {
460 char *type = a->a_desc->ad_cname.bv_val;
462 snprintf( textbuf, textlen,
463 "attribute '%s' not allowed",
466 Debug( LDAP_DEBUG_ANY,
468 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;
694 * Return structural object class from list of modifications
696 int mods_structural_class(
700 char *textbuf, size_t textlen )
702 Modifications *ocmod = NULL;
704 for( ; mods != NULL; mods = mods->sml_next ) {
705 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
706 if( ocmod != NULL ) {
707 *text = "entry has multiple objectClass attributes";
708 return LDAP_OBJECT_CLASS_VIOLATION;
714 if( ocmod == NULL ) {
715 *text = "entry has no objectClass attribute";
716 return LDAP_OBJECT_CLASS_VIOLATION;
719 if( ocmod->sml_values == NULL || ocmod->sml_values[0].bv_val == NULL ) {
720 *text = "objectClass attribute has no values";
721 return LDAP_OBJECT_CLASS_VIOLATION;
724 return structural_class( ocmod->sml_values, sc, NULL,
725 text, textbuf, textlen );
734 char *textbuf, size_t textlen )
738 const char *p = NULL;
740 int rc = LDAP_SUCCESS;
742 if ( BER_BVISEMPTY( &e->e_name )) {
747 * Get attribute type(s) and attribute value(s) of our RDN
749 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
750 LDAP_DN_FORMAT_LDAP ) )
752 *text = "unrecongized attribute type(s) in RDN";
753 return LDAP_INVALID_DN_SYNTAX;
756 /* Check that each AVA of the RDN is present in the entry */
757 /* FIXME: Should also check that each AVA lists a distinct type */
758 for ( cnt = 0; rdn[cnt]; cnt++ ) {
759 LDAPAVA *ava = rdn[cnt];
760 AttributeDescription *desc = NULL;
764 if( ava->la_flags & LDAP_AVA_BINARY ) {
765 snprintf( textbuf, textlen,
766 "value of naming attribute '%s' in unsupported BER form",
767 ava->la_attr.bv_val );
768 rc = LDAP_NAMING_VIOLATION;
771 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
772 if ( rc != LDAP_SUCCESS ) {
773 snprintf( textbuf, textlen, "%s (in RDN)", errtext );
777 if( desc->ad_type->sat_usage ) {
778 snprintf( textbuf, textlen,
779 "naming attribute '%s' is operational",
780 ava->la_attr.bv_val );
781 rc = LDAP_NAMING_VIOLATION;
785 if( desc->ad_type->sat_collective ) {
786 snprintf( textbuf, textlen,
787 "naming attribute '%s' is collective",
788 ava->la_attr.bv_val );
789 rc = LDAP_NAMING_VIOLATION;
793 if( !manage && desc->ad_type->sat_obsolete ) {
794 snprintf( textbuf, textlen,
795 "naming attribute '%s' is obsolete",
796 ava->la_attr.bv_val );
797 rc = LDAP_NAMING_VIOLATION;
801 if( !desc->ad_type->sat_equality ) {
802 snprintf( textbuf, textlen,
803 "naming attribute '%s' has no equality matching rule",
804 ava->la_attr.bv_val );
805 rc = LDAP_NAMING_VIOLATION;
809 if( !desc->ad_type->sat_equality->smr_match ) {
810 snprintf( textbuf, textlen,
811 "naming attribute '%s' has unsupported equality matching rule",
812 ava->la_attr.bv_val );
813 rc = LDAP_NAMING_VIOLATION;
817 /* find the naming attribute */
818 attr = attr_find( e->e_attrs, desc );
819 if ( attr == NULL ) {
820 snprintf( textbuf, textlen,
821 "naming attribute '%s' is not present in entry",
822 ava->la_attr.bv_val );
823 rc = LDAP_NAMING_VIOLATION;
827 rc = value_find_ex( desc, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX|
828 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
829 attr->a_nvals, &ava->la_value, NULL );
833 case LDAP_INAPPROPRIATE_MATCHING:
834 snprintf( textbuf, textlen,
835 "inappropriate matching for naming attribute '%s'",
836 ava->la_attr.bv_val );
838 case LDAP_INVALID_SYNTAX:
839 snprintf( textbuf, textlen,
840 "value of naming attribute '%s' is invalid",
841 ava->la_attr.bv_val );
843 case LDAP_NO_SUCH_ATTRIBUTE:
844 snprintf( textbuf, textlen,
845 "value of naming attribute '%s' is not present in entry",
846 ava->la_attr.bv_val );
849 snprintf( textbuf, textlen,
850 "naming attribute '%s' is inappropriate",
851 ava->la_attr.bv_val );
853 rc = LDAP_NAMING_VIOLATION;