1 /* schema_check.c - routines to enforce schema definitions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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 rc = LDAP_OBJECT_CLASS_VIOLATION;
178 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
179 snprintf( textbuf, textlen,
180 "structuralObjectClass '%s' is not STRUCTURAL",
181 asc->a_vals[0].bv_val );
183 Debug( LDAP_DEBUG_ANY,
184 "entry_check_schema(%s): %s\n",
185 e->e_dn, textbuf, 0 );
192 if( !manage && sc->soc_obsolete ) {
193 snprintf( textbuf, textlen,
194 "structuralObjectClass '%s' is OBSOLETE",
195 asc->a_vals[0].bv_val );
197 Debug( LDAP_DEBUG_ANY,
198 "entry_check_schema(%s): %s\n",
199 e->e_dn, textbuf, 0 );
201 rc = LDAP_OBJECT_CLASS_VIOLATION;
208 snprintf( textbuf, textlen,
209 "unrecognized objectClass '%s'",
210 aoc->a_vals[0].bv_val );
211 rc = LDAP_OBJECT_CLASS_VIOLATION;
214 } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
215 snprintf( textbuf, textlen,
216 "structural object class modification "
217 "from '%s' to '%s' not allowed",
218 asc->a_vals[0].bv_val, oc->soc_cname.bv_val );
219 rc = LDAP_NO_OBJECT_CLASS_MODS;
221 } else if ( sc == slap_schema.si_oc_glue ) {
226 if ( !is_entry_glue ( e ) ) {
227 rc = entry_naming_check( e, manage, text, textbuf, textlen );
228 if( rc != LDAP_SUCCESS ) {
235 /* find the content rule for the structural class */
236 cr = cr_find( sc->soc_oid );
238 /* the cr must be same as the structural class */
239 assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
241 /* check that the entry has required attrs of the content rule */
243 if( !manage && cr->scr_obsolete ) {
244 snprintf( textbuf, textlen,
245 "content rule '%s' is obsolete",
246 ldap_contentrule2name( &cr->scr_crule ));
248 Debug( LDAP_DEBUG_ANY,
250 e->e_dn, textbuf, 0 );
252 rc = LDAP_OBJECT_CLASS_VIOLATION;
256 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
257 at = cr->scr_required[i];
259 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
260 if( a->a_desc->ad_type == at ) {
265 /* not there => schema violation */
267 snprintf( textbuf, textlen,
268 "content rule '%s' requires attribute '%s'",
269 ldap_contentrule2name( &cr->scr_crule ),
270 at->sat_cname.bv_val );
272 Debug( LDAP_DEBUG_ANY,
274 e->e_dn, textbuf, 0 );
276 rc = LDAP_OBJECT_CLASS_VIOLATION;
281 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
282 at = cr->scr_precluded[i];
284 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
285 if( a->a_desc->ad_type == at ) {
290 /* there => schema violation */
292 snprintf( textbuf, textlen,
293 "content rule '%s' precluded attribute '%s'",
294 ldap_contentrule2name( &cr->scr_crule ),
295 at->sat_cname.bv_val );
297 Debug( LDAP_DEBUG_ANY,
299 e->e_dn, textbuf, 0 );
301 rc = LDAP_OBJECT_CLASS_VIOLATION;
307 /* check that the entry has required attrs for each oc */
308 for ( i = 0; socs[i]; i++ ) {
310 if ( !manage && oc->soc_obsolete ) {
311 /* disallow obsolete classes */
312 snprintf( textbuf, textlen,
313 "objectClass '%s' is OBSOLETE",
314 aoc->a_vals[i].bv_val );
316 Debug( LDAP_DEBUG_ANY,
317 "entry_check_schema(%s): %s\n",
318 e->e_dn, textbuf, 0 );
320 rc = LDAP_OBJECT_CLASS_VIOLATION;
324 if ( oc->soc_check ) {
325 rc = (oc->soc_check)( op->o_bd, e, oc,
326 text, textbuf, textlen );
327 if( rc != LDAP_SUCCESS ) {
332 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
333 /* object class is abstract */
334 if ( oc != slap_schema.si_oc_top &&
335 !is_object_subclass( oc, sc ))
338 ObjectClass *xc = NULL;
339 for( j=0; socs[j]; j++ ) {
343 /* since we previous check against the
344 * structural object of this entry, the
345 * abstract class must be a (direct or indirect)
346 * superclass of one of the auxiliary classes of
349 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
350 is_object_subclass( oc, xc ) )
359 snprintf( textbuf, textlen, "instanstantiation of "
360 "abstract objectClass '%s' not allowed",
361 aoc->a_vals[i].bv_val );
363 Debug( LDAP_DEBUG_ANY,
364 "entry_check_schema(%s): %s\n",
365 e->e_dn, textbuf, 0 );
367 rc = LDAP_OBJECT_CLASS_VIOLATION;
372 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
375 if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
382 if( cr->scr_auxiliaries ) {
383 for( j = 0; cr->scr_auxiliaries[j]; j++ ) {
384 if( cr->scr_auxiliaries[j] == oc ) {
391 snprintf( textbuf, textlen,
392 "class '%s' not allowed by content rule '%s'",
393 oc->soc_cname.bv_val,
394 ldap_contentrule2name( &cr->scr_crule ) );
396 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
398 snprintf( textbuf, textlen,
399 "class '%s' not allowed by any content rule",
400 oc->soc_cname.bv_val );
406 Debug( LDAP_DEBUG_ANY,
408 e->e_dn, textbuf, 0 );
410 rc = LDAP_OBJECT_CLASS_VIOLATION;
415 s = oc_check_required( e, oc, &aoc->a_vals[i] );
417 snprintf( textbuf, textlen,
418 "object class '%s' requires attribute '%s'",
419 aoc->a_vals[i].bv_val, s );
421 Debug( LDAP_DEBUG_ANY,
423 e->e_dn, textbuf, 0 );
425 rc = LDAP_OBJECT_CLASS_VIOLATION;
429 if( oc == slap_schema.si_oc_extensibleObject ) {
441 /* check that each attr in the entry is allowed by some oc */
442 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
443 rc = LDAP_OBJECT_CLASS_VIOLATION;
445 if( cr && cr->scr_required ) {
446 for( i=0; cr->scr_required[i]; i++ ) {
447 if( cr->scr_required[i] == a->a_desc->ad_type ) {
454 if( rc != LDAP_SUCCESS && cr && cr->scr_allowed ) {
455 for( i=0; cr->scr_allowed[i]; i++ ) {
456 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
463 if( rc != LDAP_SUCCESS )
465 rc = oc_check_allowed( a->a_desc->ad_type, socs, sc );
468 if ( rc != LDAP_SUCCESS ) {
469 char *type = a->a_desc->ad_cname.bv_val;
471 snprintf( textbuf, textlen,
472 "attribute '%s' not allowed",
475 Debug( LDAP_DEBUG_ANY,
477 e->e_dn, textbuf, 0 );
485 slap_sl_free( socs, op->o_tmpmemctx );
493 struct berval *ocname )
499 Debug( LDAP_DEBUG_TRACE,
500 "oc_check_required entry (%s), objectClass \"%s\"\n",
501 e->e_dn, ocname->bv_val, 0 );
504 /* check for empty oc_required */
505 if(oc->soc_required == NULL) {
509 /* for each required attribute */
510 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
511 at = oc->soc_required[i];
512 /* see if it's in the entry */
513 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
514 if( a->a_desc->ad_type == at ) {
518 /* not there => schema violation */
520 return at->sat_cname.bv_val;
527 int oc_check_allowed(
534 Debug( LDAP_DEBUG_TRACE,
535 "oc_check_allowed type \"%s\"\n",
536 at->sat_cname.bv_val, 0, 0 );
538 /* always allow objectClass attribute */
539 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
544 * All operational attributions are allowed by schema rules.
546 if( is_at_operational(at) ) {
550 /* check to see if its allowed by the structuralObjectClass */
552 /* does it require the type? */
553 for ( j = 0; sc->soc_required != NULL &&
554 sc->soc_required[j] != NULL; j++ )
556 if( at == sc->soc_required[j] ) {
561 /* does it allow the type? */
562 for ( j = 0; sc->soc_allowed != NULL &&
563 sc->soc_allowed[j] != NULL; j++ )
565 if( at == sc->soc_allowed[j] ) {
571 /* check that the type appears as req or opt in at least one oc */
572 for ( i = 0; socs[i]; i++ ) {
573 /* if we know about the oc */
574 ObjectClass *oc = socs[i];
575 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
576 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
578 /* does it require the type? */
579 for ( j = 0; oc->soc_required != NULL &&
580 oc->soc_required[j] != NULL; j++ )
582 if( at == oc->soc_required[j] ) {
586 /* does it allow the type? */
587 for ( j = 0; oc->soc_allowed != NULL &&
588 oc->soc_allowed[j] != NULL; j++ )
590 if( at == oc->soc_allowed[j] ) {
597 /* not allowed by any oc */
598 return LDAP_OBJECT_CLASS_VIOLATION;
602 * Determine the structural object class from a set of OIDs
604 int structural_class(
607 ObjectClass ***socsp,
609 char *textbuf, size_t textlen,
613 ObjectClass *oc, **socs;
614 ObjectClass *sc = NULL;
617 *text = "structural_class: internal error";
620 for( i=0; ocs[i].bv_val; i++ ) ;
623 socs = slap_sl_malloc( (nocs+1) * sizeof(ObjectClass *), ctx );
625 for( i=0; ocs[i].bv_val; i++ ) {
626 socs[i] = oc_bvfind( &ocs[i] );
628 if( socs[i] == NULL ) {
629 snprintf( textbuf, textlen,
630 "unrecognized objectClass '%s'",
638 for( i=0; ocs[i].bv_val; i++ ) {
640 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
641 if( sc == NULL || is_object_subclass( sc, oc ) ) {
645 } else if ( !is_object_subclass( oc, sc ) ) {
647 ObjectClass *xc = NULL;
649 /* find common superior */
650 for( j=i+1; ocs[j].bv_val; j++ ) {
654 snprintf( textbuf, textlen,
655 "unrecognized objectClass '%s'",
661 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
666 if( is_object_subclass( sc, xc ) &&
667 is_object_subclass( oc, xc ) )
669 /* found common subclass */
677 /* no common subclass */
678 snprintf( textbuf, textlen,
679 "invalid structural object class chain (%s/%s)",
680 ocs[scn].bv_val, ocs[i].bv_val );
693 *text = "no structural object class provided";
698 *text = "invalid structural object class";
705 slap_sl_free( socs, ctx );
712 slap_sl_free( socs, ctx );
713 return LDAP_OBJECT_CLASS_VIOLATION;
717 * Return structural object class from list of modifications
719 int mods_structural_class(
723 char *textbuf, size_t textlen, void *ctx )
725 Modifications *ocmod = NULL;
729 for( ; mods != NULL; mods = mods->sml_next ) {
730 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
731 if( ocmod != NULL ) {
732 *text = "entry has multiple objectClass attributes";
733 return LDAP_OBJECT_CLASS_VIOLATION;
739 if( ocmod == NULL ) {
740 *text = "entry has no objectClass attribute";
741 return LDAP_OBJECT_CLASS_VIOLATION;
744 if( ocmod->sml_values == NULL || ocmod->sml_values[0].bv_val == NULL ) {
745 *text = "objectClass attribute has no values";
746 return LDAP_OBJECT_CLASS_VIOLATION;
749 rc = structural_class( ocmod->sml_values, &ssc, NULL,
750 text, textbuf, textlen, ctx );
751 if ( rc == LDAP_SUCCESS )
752 *sc = ssc->soc_cname;
762 char *textbuf, size_t textlen )
766 const char *p = NULL;
768 int rc = LDAP_SUCCESS;
770 if ( BER_BVISEMPTY( &e->e_name )) {
775 * Get attribute type(s) and attribute value(s) of our RDN
777 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
778 LDAP_DN_FORMAT_LDAP ) )
780 *text = "unrecongized attribute type(s) in RDN";
781 return LDAP_INVALID_DN_SYNTAX;
784 /* Check that each AVA of the RDN is present in the entry */
785 /* FIXME: Should also check that each AVA lists a distinct type */
786 for ( cnt = 0; rdn[cnt]; cnt++ ) {
787 LDAPAVA *ava = rdn[cnt];
788 AttributeDescription *desc = NULL;
792 if( ava->la_flags & LDAP_AVA_BINARY ) {
793 snprintf( textbuf, textlen,
794 "value of naming attribute '%s' in unsupported BER form",
795 ava->la_attr.bv_val );
796 rc = LDAP_NAMING_VIOLATION;
799 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
800 if ( rc != LDAP_SUCCESS ) {
801 snprintf( textbuf, textlen, "%s (in RDN)", errtext );
805 if( desc->ad_type->sat_usage ) {
806 snprintf( textbuf, textlen,
807 "naming attribute '%s' is operational",
808 ava->la_attr.bv_val );
809 rc = LDAP_NAMING_VIOLATION;
813 if( desc->ad_type->sat_collective ) {
814 snprintf( textbuf, textlen,
815 "naming attribute '%s' is collective",
816 ava->la_attr.bv_val );
817 rc = LDAP_NAMING_VIOLATION;
821 if( !manage && desc->ad_type->sat_obsolete ) {
822 snprintf( textbuf, textlen,
823 "naming attribute '%s' is obsolete",
824 ava->la_attr.bv_val );
825 rc = LDAP_NAMING_VIOLATION;
829 if( !desc->ad_type->sat_equality ) {
830 snprintf( textbuf, textlen,
831 "naming attribute '%s' has no equality matching rule",
832 ava->la_attr.bv_val );
833 rc = LDAP_NAMING_VIOLATION;
837 if( !desc->ad_type->sat_equality->smr_match ) {
838 snprintf( textbuf, textlen,
839 "naming attribute '%s' has unsupported equality matching rule",
840 ava->la_attr.bv_val );
841 rc = LDAP_NAMING_VIOLATION;
845 /* find the naming attribute */
846 attr = attr_find( e->e_attrs, desc );
847 if ( attr == NULL ) {
848 snprintf( textbuf, textlen,
849 "naming attribute '%s' is not present in entry",
850 ava->la_attr.bv_val );
851 rc = LDAP_NAMING_VIOLATION;
855 rc = value_find_ex( desc, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX|
856 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
857 attr->a_nvals, &ava->la_value, NULL );
861 case LDAP_INAPPROPRIATE_MATCHING:
862 snprintf( textbuf, textlen,
863 "inappropriate matching for naming attribute '%s'",
864 ava->la_attr.bv_val );
866 case LDAP_INVALID_SYNTAX:
867 snprintf( textbuf, textlen,
868 "value of naming attribute '%s' is invalid",
869 ava->la_attr.bv_val );
871 case LDAP_NO_SUCH_ATTRIBUTE:
872 snprintf( textbuf, textlen,
873 "value of naming attribute '%s' is not present in entry",
874 ava->la_attr.bv_val );
877 snprintf( textbuf, textlen,
878 "naming attribute '%s' is inappropriate",
879 ava->la_attr.bv_val );
881 rc = LDAP_NAMING_VIOLATION;