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(
35 char *textbuf, size_t textlen );
37 * entry_schema_check - check that entry e conforms to the schema required
38 * by its object class(es).
40 * returns 0 if so, non-zero otherwise.
49 char *textbuf, size_t textlen )
51 Attribute *a, *asc, *aoc;
57 AttributeDescription *ad_structuralObjectClass
58 = slap_schema.si_ad_structuralObjectClass;
59 AttributeDescription *ad_objectClass
60 = slap_schema.si_ad_objectClass;
62 int subentry = is_entry_subentry( e );
63 int collectiveSubentry = 0;
65 if ( SLAP_NO_SCHEMA_CHECK( be )) {
70 collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
75 /* misc attribute checks */
76 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
77 const char *type = a->a_desc->ad_cname.bv_val;
79 /* there should be at least one value */
81 assert( a->a_vals[0].bv_val != NULL );
83 if( a->a_desc->ad_type->sat_check ) {
84 int rc = (a->a_desc->ad_type->sat_check)(
85 be, e, a, text, textbuf, textlen );
86 if( rc != LDAP_SUCCESS ) {
91 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
92 snprintf( textbuf, textlen,
93 "'%s' can only appear in collectiveAttributeSubentry",
95 return LDAP_OBJECT_CLASS_VIOLATION;
98 /* if single value type, check for multiple values */
99 if( is_at_single_value( a->a_desc->ad_type ) &&
100 a->a_vals[1].bv_val != NULL )
102 snprintf( textbuf, textlen,
103 "attribute '%s' cannot have multiple values",
106 Debug( LDAP_DEBUG_ANY,
108 e->e_dn, textbuf, 0 );
110 return LDAP_CONSTRAINT_VIOLATION;
114 /* it's a REALLY bad idea to disable schema checks */
115 if( !global_schemacheck ) return LDAP_SUCCESS;
117 /* find the structural object class attribute */
118 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
120 Debug( LDAP_DEBUG_ANY,
121 "No structuralObjectClass for entry (%s)\n",
124 *text = "no structuralObjectClass operational attribute";
128 assert( asc->a_vals != NULL );
129 assert( asc->a_vals[0].bv_val != NULL );
130 assert( asc->a_vals[1].bv_val == NULL );
132 sc = oc_bvfind( &asc->a_vals[0] );
134 snprintf( textbuf, textlen,
135 "unrecognized structuralObjectClass '%s'",
136 asc->a_vals[0].bv_val );
138 Debug( LDAP_DEBUG_ANY,
139 "entry_check_schema(%s): %s\n",
140 e->e_dn, textbuf, 0 );
142 return LDAP_OBJECT_CLASS_VIOLATION;
145 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
146 snprintf( textbuf, textlen,
147 "structuralObjectClass '%s' is not STRUCTURAL",
148 asc->a_vals[0].bv_val );
150 Debug( LDAP_DEBUG_ANY,
151 "entry_check_schema(%s): %s\n",
152 e->e_dn, textbuf, 0 );
157 if( sc->soc_obsolete ) {
158 snprintf( textbuf, textlen,
159 "structuralObjectClass '%s' is OBSOLETE",
160 asc->a_vals[0].bv_val );
162 Debug( LDAP_DEBUG_ANY,
163 "entry_check_schema(%s): %s\n",
164 e->e_dn, textbuf, 0 );
166 return LDAP_OBJECT_CLASS_VIOLATION;
169 /* find the object class attribute */
170 aoc = attr_find( e->e_attrs, ad_objectClass );
172 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
175 *text = "no objectClass attribute";
176 return LDAP_OBJECT_CLASS_VIOLATION;
179 assert( aoc->a_vals != NULL );
180 assert( aoc->a_vals[0].bv_val != NULL );
182 rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
183 if( rc != LDAP_SUCCESS ) {
190 snprintf( textbuf, textlen,
191 "unrecognized objectClass '%s'",
192 aoc->a_vals[0].bv_val );
193 return LDAP_OBJECT_CLASS_VIOLATION;
195 } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
196 snprintf( textbuf, textlen,
197 "structural object class modification "
198 "from '%s' to '%s' not allowed",
199 asc->a_vals[0].bv_val, nsc.bv_val );
200 return LDAP_NO_OBJECT_CLASS_MODS;
201 } else if ( sc == slap_schema.si_oc_glue ) {
206 if ( !is_entry_objectclass ( e, slap_schema.si_oc_glue, 0 ) ) {
207 rc = entry_naming_check( e, text, textbuf, textlen );
208 if( rc != LDAP_SUCCESS ) {
215 /* find the content rule for the structural class */
216 cr = cr_find( sc->soc_oid );
218 /* the cr must be same as the structural class */
219 assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
221 /* check that the entry has required attrs of the content rule */
223 if( cr->scr_obsolete ) {
224 snprintf( textbuf, textlen,
225 "content rule '%s' is obsolete",
226 ldap_contentrule2name( &cr->scr_crule ));
228 Debug( LDAP_DEBUG_ANY,
230 e->e_dn, textbuf, 0 );
232 return LDAP_OBJECT_CLASS_VIOLATION;
235 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
236 at = cr->scr_required[i];
238 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
239 if( a->a_desc->ad_type == at ) {
244 /* not there => schema violation */
246 snprintf( textbuf, textlen,
247 "content rule '%s' requires attribute '%s'",
248 ldap_contentrule2name( &cr->scr_crule ),
249 at->sat_cname.bv_val );
251 Debug( LDAP_DEBUG_ANY,
253 e->e_dn, textbuf, 0 );
255 return LDAP_OBJECT_CLASS_VIOLATION;
259 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
260 at = cr->scr_precluded[i];
262 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
263 if( a->a_desc->ad_type == at ) {
268 /* there => schema violation */
270 snprintf( textbuf, textlen,
271 "content rule '%s' precluded attribute '%s'",
272 ldap_contentrule2name( &cr->scr_crule ),
273 at->sat_cname.bv_val );
275 Debug( LDAP_DEBUG_ANY,
277 e->e_dn, textbuf, 0 );
279 return LDAP_OBJECT_CLASS_VIOLATION;
284 /* check that the entry has required attrs for each oc */
285 for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
286 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
287 snprintf( textbuf, textlen,
288 "unrecognized objectClass '%s'",
289 aoc->a_vals[i].bv_val );
291 Debug( LDAP_DEBUG_ANY,
292 "entry_check_schema(%s): %s\n",
293 e->e_dn, textbuf, 0 );
295 return LDAP_OBJECT_CLASS_VIOLATION;
298 if ( oc->soc_obsolete ) {
299 /* disallow obsolete classes */
300 snprintf( textbuf, textlen,
301 "objectClass '%s' is OBSOLETE",
302 aoc->a_vals[i].bv_val );
304 Debug( LDAP_DEBUG_ANY,
305 "entry_check_schema(%s): %s\n",
306 e->e_dn, textbuf, 0 );
308 return LDAP_OBJECT_CLASS_VIOLATION;
311 if ( oc->soc_check ) {
312 int rc = (oc->soc_check)( be, e, oc,
313 text, textbuf, textlen );
314 if( rc != LDAP_SUCCESS ) {
319 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
320 /* object class is abstract */
321 if ( oc != slap_schema.si_oc_top &&
322 !is_object_subclass( oc, sc ))
325 ObjectClass *xc = NULL;
326 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
328 xc = oc_bvfind( &aoc->a_vals[i] );
330 snprintf( textbuf, textlen,
331 "unrecognized objectClass '%s'",
332 aoc->a_vals[i].bv_val );
334 Debug( LDAP_DEBUG_ANY,
335 "entry_check_schema(%s): %s\n",
336 e->e_dn, textbuf, 0 );
338 return LDAP_OBJECT_CLASS_VIOLATION;
341 /* since we previous check against the
342 * structural object of this entry, the
343 * abstract class must be a (direct or indirect)
344 * superclass of one of the auxiliary classes of
347 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
348 is_object_subclass( oc, xc ) )
357 snprintf( textbuf, textlen, "instanstantiation of "
358 "abstract objectClass '%s' not allowed",
359 aoc->a_vals[i].bv_val );
361 Debug( LDAP_DEBUG_ANY,
362 "entry_check_schema(%s): %s\n",
363 e->e_dn, textbuf, 0 );
365 return 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 return LDAP_OBJECT_CLASS_VIOLATION;
407 s = oc_check_required( e, oc, &aoc->a_vals[i] );
409 snprintf( textbuf, textlen,
410 "object class '%s' requires attribute '%s'",
411 aoc->a_vals[i].bv_val, s );
413 Debug( LDAP_DEBUG_ANY,
415 e->e_dn, textbuf, 0 );
417 return LDAP_OBJECT_CLASS_VIOLATION;
420 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 );
483 struct berval *ocname )
489 Debug( LDAP_DEBUG_TRACE,
490 "oc_check_required entry (%s), objectClass \"%s\"\n",
491 e->e_dn, ocname->bv_val, 0 );
494 /* check for empty oc_required */
495 if(oc->soc_required == NULL) {
499 /* for each required attribute */
500 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
501 at = oc->soc_required[i];
502 /* see if it's in the entry */
503 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
504 if( a->a_desc->ad_type == at ) {
508 /* not there => schema violation */
510 return at->sat_cname.bv_val;
517 int oc_check_allowed(
524 Debug( LDAP_DEBUG_TRACE,
525 "oc_check_allowed type \"%s\"\n",
526 at->sat_cname.bv_val, 0, 0 );
528 /* always allow objectClass attribute */
529 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
534 * All operational attributions are allowed by schema rules.
536 if( is_at_operational(at) ) {
540 /* check to see if its allowed by the structuralObjectClass */
542 /* does it require the type? */
543 for ( j = 0; sc->soc_required != NULL &&
544 sc->soc_required[j] != NULL; j++ )
546 if( at == sc->soc_required[j] ) {
551 /* does it allow the type? */
552 for ( j = 0; sc->soc_allowed != NULL &&
553 sc->soc_allowed[j] != NULL; j++ )
555 if( at == sc->soc_allowed[j] ) {
561 /* check that the type appears as req or opt in at least one oc */
562 for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
563 /* if we know about the oc */
564 ObjectClass *oc = oc_bvfind( &ocl[i] );
565 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
566 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
568 /* does it require the type? */
569 for ( j = 0; oc->soc_required != NULL &&
570 oc->soc_required[j] != NULL; j++ )
572 if( at == oc->soc_required[j] ) {
576 /* does it allow the type? */
577 for ( j = 0; oc->soc_allowed != NULL &&
578 oc->soc_allowed[j] != NULL; j++ )
580 if( at == oc->soc_allowed[j] ) {
587 /* not allowed by any oc */
588 return LDAP_OBJECT_CLASS_VIOLATION;
592 * Determine the structural object class from a set of OIDs
594 int structural_class(
599 char *textbuf, size_t textlen )
603 ObjectClass *sc = NULL;
606 *text = "structural_class: internal error";
609 for( i=0; ocs[i].bv_val; i++ ) {
610 oc = oc_bvfind( &ocs[i] );
613 snprintf( textbuf, textlen,
614 "unrecognized objectClass '%s'",
617 return LDAP_OBJECT_CLASS_VIOLATION;
620 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
621 if( sc == NULL || is_object_subclass( sc, oc ) ) {
625 } else if ( !is_object_subclass( oc, sc ) ) {
627 ObjectClass *xc = NULL;
629 /* find common superior */
630 for( j=i+1; ocs[j].bv_val; j++ ) {
631 xc = oc_bvfind( &ocs[j] );
634 snprintf( textbuf, textlen,
635 "unrecognized objectClass '%s'",
638 return LDAP_OBJECT_CLASS_VIOLATION;
641 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
646 if( is_object_subclass( sc, xc ) &&
647 is_object_subclass( oc, xc ) )
649 /* found common subclass */
657 /* no common subclass */
658 snprintf( textbuf, textlen,
659 "invalid structural object class chain (%s/%s)",
660 ocs[scn].bv_val, ocs[i].bv_val );
662 return LDAP_OBJECT_CLASS_VIOLATION;
673 *text = "no structural object class provided";
674 return LDAP_OBJECT_CLASS_VIOLATION;
678 *text = "invalid structural object class";
679 return LDAP_OBJECT_CLASS_VIOLATION;
684 if( scbv->bv_len == 0 ) {
685 *text = "invalid structural object class";
686 return LDAP_OBJECT_CLASS_VIOLATION;
695 * Return structural object class from list of modifications
697 int mods_structural_class(
701 char *textbuf, size_t textlen )
703 Modifications *ocmod = NULL;
705 for( ; mods != NULL; mods = mods->sml_next ) {
706 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
707 if( ocmod != NULL ) {
708 *text = "entry has multiple objectClass attributes";
709 return LDAP_OBJECT_CLASS_VIOLATION;
715 if( ocmod == NULL ) {
716 *text = "entry has no objectClass attribute";
717 return LDAP_OBJECT_CLASS_VIOLATION;
720 if( ocmod->sml_values == NULL || ocmod->sml_values[0].bv_val == NULL ) {
721 *text = "objectClass attribute has no values";
722 return LDAP_OBJECT_CLASS_VIOLATION;
725 return structural_class( ocmod->sml_values, sc, NULL,
726 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( 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;