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.
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( 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 int rc = (a->a_desc->ad_type->sat_check)(
91 op->o_bd, e, a, text, textbuf, textlen );
92 if( rc != LDAP_SUCCESS ) {
97 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
98 snprintf( textbuf, textlen,
99 "'%s' can only appear in collectiveAttributeSubentry",
101 return LDAP_OBJECT_CLASS_VIOLATION;
104 /* if single value type, check for multiple values */
105 if( is_at_single_value( a->a_desc->ad_type ) &&
106 a->a_vals[1].bv_val != NULL )
108 snprintf( textbuf, textlen,
109 "attribute '%s' cannot have multiple values",
112 Debug( LDAP_DEBUG_ANY,
114 e->e_dn, textbuf, 0 );
116 return LDAP_CONSTRAINT_VIOLATION;
120 /* find the structural object class attribute */
121 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
123 Debug( LDAP_DEBUG_ANY,
124 "No structuralObjectClass for entry (%s)\n",
127 *text = "no structuralObjectClass operational attribute";
131 assert( asc->a_vals != NULL );
132 assert( asc->a_vals[0].bv_val != NULL );
133 assert( asc->a_vals[1].bv_val == NULL );
135 sc = oc_bvfind( &asc->a_vals[0] );
137 snprintf( textbuf, textlen,
138 "unrecognized structuralObjectClass '%s'",
139 asc->a_vals[0].bv_val );
141 Debug( LDAP_DEBUG_ANY,
142 "entry_check_schema(%s): %s\n",
143 e->e_dn, textbuf, 0 );
145 return LDAP_OBJECT_CLASS_VIOLATION;
148 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
149 snprintf( textbuf, textlen,
150 "structuralObjectClass '%s' is not STRUCTURAL",
151 asc->a_vals[0].bv_val );
153 Debug( LDAP_DEBUG_ANY,
154 "entry_check_schema(%s): %s\n",
155 e->e_dn, textbuf, 0 );
160 if( !manage && sc->soc_obsolete ) {
161 snprintf( textbuf, textlen,
162 "structuralObjectClass '%s' is OBSOLETE",
163 asc->a_vals[0].bv_val );
165 Debug( LDAP_DEBUG_ANY,
166 "entry_check_schema(%s): %s\n",
167 e->e_dn, textbuf, 0 );
169 return LDAP_OBJECT_CLASS_VIOLATION;
172 /* find the object class attribute */
173 aoc = attr_find( e->e_attrs, ad_objectClass );
175 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
178 *text = "no objectClass attribute";
179 return LDAP_OBJECT_CLASS_VIOLATION;
182 assert( aoc->a_vals != NULL );
183 assert( aoc->a_vals[0].bv_val != NULL );
185 rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
186 if( rc != LDAP_SUCCESS ) {
193 snprintf( textbuf, textlen,
194 "unrecognized objectClass '%s'",
195 aoc->a_vals[0].bv_val );
196 return LDAP_OBJECT_CLASS_VIOLATION;
198 } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
199 snprintf( textbuf, textlen,
200 "structural object class modification "
201 "from '%s' to '%s' not allowed",
202 asc->a_vals[0].bv_val, nsc.bv_val );
203 return LDAP_NO_OBJECT_CLASS_MODS;
204 } else if ( sc == slap_schema.si_oc_glue ) {
209 if ( !is_entry_objectclass ( e, slap_schema.si_oc_glue, 0 ) ) {
210 rc = entry_naming_check( e, manage, text, textbuf, textlen );
211 if( rc != LDAP_SUCCESS ) {
218 /* find the content rule for the structural class */
219 cr = cr_find( sc->soc_oid );
221 /* the cr must be same as the structural class */
222 assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
224 /* check that the entry has required attrs of the content rule */
226 if( !manage && cr->scr_obsolete ) {
227 snprintf( textbuf, textlen,
228 "content rule '%s' is obsolete",
229 ldap_contentrule2name( &cr->scr_crule ));
231 Debug( LDAP_DEBUG_ANY,
233 e->e_dn, textbuf, 0 );
235 return LDAP_OBJECT_CLASS_VIOLATION;
238 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
239 at = cr->scr_required[i];
241 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
242 if( a->a_desc->ad_type == at ) {
247 /* not there => schema violation */
249 snprintf( textbuf, textlen,
250 "content rule '%s' requires attribute '%s'",
251 ldap_contentrule2name( &cr->scr_crule ),
252 at->sat_cname.bv_val );
254 Debug( LDAP_DEBUG_ANY,
256 e->e_dn, textbuf, 0 );
258 return LDAP_OBJECT_CLASS_VIOLATION;
262 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
263 at = cr->scr_precluded[i];
265 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
266 if( a->a_desc->ad_type == at ) {
271 /* there => schema violation */
273 snprintf( textbuf, textlen,
274 "content rule '%s' precluded attribute '%s'",
275 ldap_contentrule2name( &cr->scr_crule ),
276 at->sat_cname.bv_val );
278 Debug( LDAP_DEBUG_ANY,
280 e->e_dn, textbuf, 0 );
282 return LDAP_OBJECT_CLASS_VIOLATION;
287 /* check that the entry has required attrs for each oc */
288 for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
289 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
290 snprintf( textbuf, textlen,
291 "unrecognized objectClass '%s'",
292 aoc->a_vals[i].bv_val );
294 Debug( LDAP_DEBUG_ANY,
295 "entry_check_schema(%s): %s\n",
296 e->e_dn, textbuf, 0 );
298 return LDAP_OBJECT_CLASS_VIOLATION;
301 if ( !manage && oc->soc_obsolete ) {
302 /* disallow obsolete classes */
303 snprintf( textbuf, textlen,
304 "objectClass '%s' is OBSOLETE",
305 aoc->a_vals[i].bv_val );
307 Debug( LDAP_DEBUG_ANY,
308 "entry_check_schema(%s): %s\n",
309 e->e_dn, textbuf, 0 );
311 return LDAP_OBJECT_CLASS_VIOLATION;
314 if ( oc->soc_check ) {
315 int rc = (oc->soc_check)( op->o_bd, e, oc,
316 text, textbuf, textlen );
317 if( rc != LDAP_SUCCESS ) {
322 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
323 /* object class is abstract */
324 if ( oc != slap_schema.si_oc_top &&
325 !is_object_subclass( oc, sc ))
328 ObjectClass *xc = NULL;
329 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
331 xc = oc_bvfind( &aoc->a_vals[i] );
333 snprintf( textbuf, textlen,
334 "unrecognized objectClass '%s'",
335 aoc->a_vals[i].bv_val );
337 Debug( LDAP_DEBUG_ANY,
338 "entry_check_schema(%s): %s\n",
339 e->e_dn, textbuf, 0 );
341 return LDAP_OBJECT_CLASS_VIOLATION;
344 /* since we previous check against the
345 * structural object of this entry, the
346 * abstract class must be a (direct or indirect)
347 * superclass of one of the auxiliary classes of
350 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
351 is_object_subclass( oc, xc ) )
360 snprintf( textbuf, textlen, "instanstantiation of "
361 "abstract objectClass '%s' not allowed",
362 aoc->a_vals[i].bv_val );
364 Debug( LDAP_DEBUG_ANY,
365 "entry_check_schema(%s): %s\n",
366 e->e_dn, textbuf, 0 );
368 return 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 ) {
390 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
397 snprintf( textbuf, textlen,
398 "content rule '%s' does not allow class '%s'",
399 ldap_contentrule2name( &cr->scr_crule ),
400 oc->soc_cname.bv_val );
402 Debug( LDAP_DEBUG_ANY,
404 e->e_dn, textbuf, 0 );
406 return LDAP_OBJECT_CLASS_VIOLATION;
410 s = oc_check_required( e, oc, &aoc->a_vals[i] );
412 snprintf( textbuf, textlen,
413 "object class '%s' requires attribute '%s'",
414 aoc->a_vals[i].bv_val, s );
416 Debug( LDAP_DEBUG_ANY,
418 e->e_dn, textbuf, 0 );
420 return LDAP_OBJECT_CLASS_VIOLATION;
423 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 ) {
438 ret = LDAP_OBJECT_CLASS_VIOLATION;
440 if( cr && cr->scr_required ) {
441 for( i=0; cr->scr_required[i]; i++ ) {
442 if( cr->scr_required[i] == a->a_desc->ad_type ) {
449 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
450 for( i=0; cr->scr_allowed[i]; i++ ) {
451 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
458 if( ret != LDAP_SUCCESS )
460 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
463 if ( ret != LDAP_SUCCESS ) {
464 char *type = a->a_desc->ad_cname.bv_val;
466 snprintf( textbuf, textlen,
467 "attribute '%s' not allowed",
470 Debug( LDAP_DEBUG_ANY,
472 e->e_dn, textbuf, 0 );
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; ocl[i].bv_val != NULL; i++ ) {
566 /* if we know about the oc */
567 ObjectClass *oc = oc_bvfind( &ocl[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(
602 char *textbuf, size_t textlen )
606 ObjectClass *sc = NULL;
609 *text = "structural_class: internal error";
612 for( i=0; ocs[i].bv_val; i++ ) {
613 oc = oc_bvfind( &ocs[i] );
616 snprintf( textbuf, textlen,
617 "unrecognized objectClass '%s'",
620 return LDAP_OBJECT_CLASS_VIOLATION;
623 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
624 if( sc == NULL || is_object_subclass( sc, oc ) ) {
628 } else if ( !is_object_subclass( oc, sc ) ) {
630 ObjectClass *xc = NULL;
632 /* find common superior */
633 for( j=i+1; ocs[j].bv_val; j++ ) {
634 xc = oc_bvfind( &ocs[j] );
637 snprintf( textbuf, textlen,
638 "unrecognized objectClass '%s'",
641 return LDAP_OBJECT_CLASS_VIOLATION;
644 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
649 if( is_object_subclass( sc, xc ) &&
650 is_object_subclass( oc, xc ) )
652 /* found common subclass */
660 /* no common subclass */
661 snprintf( textbuf, textlen,
662 "invalid structural object class chain (%s/%s)",
663 ocs[scn].bv_val, ocs[i].bv_val );
665 return LDAP_OBJECT_CLASS_VIOLATION;
676 *text = "no structural object class provided";
677 return LDAP_OBJECT_CLASS_VIOLATION;
681 *text = "invalid structural object class";
682 return LDAP_OBJECT_CLASS_VIOLATION;
687 if( scbv->bv_len == 0 ) {
688 *text = "invalid structural object class";
689 return LDAP_OBJECT_CLASS_VIOLATION;
698 * Return structural object class from list of modifications
700 int mods_structural_class(
704 char *textbuf, size_t textlen )
706 Modifications *ocmod = NULL;
708 for( ; mods != NULL; mods = mods->sml_next ) {
709 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
710 if( ocmod != NULL ) {
711 *text = "entry has multiple objectClass attributes";
712 return LDAP_OBJECT_CLASS_VIOLATION;
718 if( ocmod == NULL ) {
719 *text = "entry has no objectClass attribute";
720 return LDAP_OBJECT_CLASS_VIOLATION;
723 if( ocmod->sml_values == NULL || ocmod->sml_values[0].bv_val == NULL ) {
724 *text = "objectClass attribute has no values";
725 return LDAP_OBJECT_CLASS_VIOLATION;
728 return structural_class( ocmod->sml_values, sc, NULL,
729 text, textbuf, textlen );
738 char *textbuf, size_t textlen )
742 const char *p = NULL;
744 int rc = LDAP_SUCCESS;
746 if ( BER_BVISEMPTY( &e->e_name )) {
751 * Get attribute type(s) and attribute value(s) of our RDN
753 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
754 LDAP_DN_FORMAT_LDAP ) )
756 *text = "unrecongized attribute type(s) in RDN";
757 return LDAP_INVALID_DN_SYNTAX;
760 /* Check that each AVA of the RDN is present in the entry */
761 /* FIXME: Should also check that each AVA lists a distinct type */
762 for ( cnt = 0; rdn[cnt]; cnt++ ) {
763 LDAPAVA *ava = rdn[cnt];
764 AttributeDescription *desc = NULL;
768 if( ava->la_flags & LDAP_AVA_BINARY ) {
769 snprintf( textbuf, textlen,
770 "value of naming attribute '%s' in unsupported BER form",
771 ava->la_attr.bv_val );
772 rc = LDAP_NAMING_VIOLATION;
775 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
776 if ( rc != LDAP_SUCCESS ) {
777 snprintf( textbuf, textlen, "%s (in RDN)", errtext );
781 if( desc->ad_type->sat_usage ) {
782 snprintf( textbuf, textlen,
783 "naming attribute '%s' is operational",
784 ava->la_attr.bv_val );
785 rc = LDAP_NAMING_VIOLATION;
789 if( desc->ad_type->sat_collective ) {
790 snprintf( textbuf, textlen,
791 "naming attribute '%s' is collective",
792 ava->la_attr.bv_val );
793 rc = LDAP_NAMING_VIOLATION;
797 if( !manage && desc->ad_type->sat_obsolete ) {
798 snprintf( textbuf, textlen,
799 "naming attribute '%s' is obsolete",
800 ava->la_attr.bv_val );
801 rc = LDAP_NAMING_VIOLATION;
805 if( !desc->ad_type->sat_equality ) {
806 snprintf( textbuf, textlen,
807 "naming attribute '%s' has no equality matching rule",
808 ava->la_attr.bv_val );
809 rc = LDAP_NAMING_VIOLATION;
813 if( !desc->ad_type->sat_equality->smr_match ) {
814 snprintf( textbuf, textlen,
815 "naming attribute '%s' has unsupported equality matching rule",
816 ava->la_attr.bv_val );
817 rc = LDAP_NAMING_VIOLATION;
821 /* find the naming attribute */
822 attr = attr_find( e->e_attrs, desc );
823 if ( attr == NULL ) {
824 snprintf( textbuf, textlen,
825 "naming attribute '%s' is not present in entry",
826 ava->la_attr.bv_val );
827 rc = LDAP_NAMING_VIOLATION;
831 rc = value_find_ex( desc, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX|
832 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
833 attr->a_nvals, &ava->la_value, NULL );
837 case LDAP_INAPPROPRIATE_MATCHING:
838 snprintf( textbuf, textlen,
839 "inappropriate matching for naming attribute '%s'",
840 ava->la_attr.bv_val );
842 case LDAP_INVALID_SYNTAX:
843 snprintf( textbuf, textlen,
844 "value of naming attribute '%s' is invalid",
845 ava->la_attr.bv_val );
847 case LDAP_NO_SUCH_ATTRIBUTE:
848 snprintf( textbuf, textlen,
849 "value of naming attribute '%s' is not present in entry",
850 ava->la_attr.bv_val );
853 snprintf( textbuf, textlen,
854 "naming attribute '%s' is inappropriate",
855 ava->la_attr.bv_val );
857 rc = LDAP_NAMING_VIOLATION;