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",
108 LDAP_LOG( OPERATION, INFO,
109 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0 );
111 Debug( LDAP_DEBUG_ANY,
113 e->e_dn, textbuf, 0 );
116 return LDAP_CONSTRAINT_VIOLATION;
120 /* it's a REALLY bad idea to disable schema checks */
121 if( !global_schemacheck ) return LDAP_SUCCESS;
123 /* find the structural object class attribute */
124 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
127 LDAP_LOG( OPERATION, INFO,
128 "entry_schema_check: No structuralObjectClass for entry (%s)\n",
131 Debug( LDAP_DEBUG_ANY,
132 "No structuralObjectClass for entry (%s)\n",
136 *text = "no structuralObjectClass operational attribute";
140 assert( asc->a_vals != NULL );
141 assert( asc->a_vals[0].bv_val != NULL );
142 assert( asc->a_vals[1].bv_val == NULL );
144 sc = oc_bvfind( &asc->a_vals[0] );
146 snprintf( textbuf, textlen,
147 "unrecognized structuralObjectClass '%s'",
148 asc->a_vals[0].bv_val );
151 LDAP_LOG( OPERATION, INFO,
152 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
154 Debug( LDAP_DEBUG_ANY,
155 "entry_check_schema(%s): %s\n",
156 e->e_dn, textbuf, 0 );
159 return LDAP_OBJECT_CLASS_VIOLATION;
162 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
163 snprintf( textbuf, textlen,
164 "structuralObjectClass '%s' is not STRUCTURAL",
165 asc->a_vals[0].bv_val );
168 LDAP_LOG( OPERATION, INFO,
169 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
171 Debug( LDAP_DEBUG_ANY,
172 "entry_check_schema(%s): %s\n",
173 e->e_dn, textbuf, 0 );
179 if( sc->soc_obsolete ) {
180 snprintf( textbuf, textlen,
181 "structuralObjectClass '%s' is OBSOLETE",
182 asc->a_vals[0].bv_val );
185 LDAP_LOG( OPERATION, INFO,
186 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
188 Debug( LDAP_DEBUG_ANY,
189 "entry_check_schema(%s): %s\n",
190 e->e_dn, textbuf, 0 );
193 return LDAP_OBJECT_CLASS_VIOLATION;
196 /* find the object class attribute */
197 aoc = attr_find( e->e_attrs, ad_objectClass );
200 LDAP_LOG( OPERATION, INFO,
201 "entry_schema_check: No objectClass for entry (%s).\n",
204 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
208 *text = "no objectClass attribute";
209 return LDAP_OBJECT_CLASS_VIOLATION;
212 assert( aoc->a_vals != NULL );
213 assert( aoc->a_vals[0].bv_val != NULL );
215 rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
216 if( rc != LDAP_SUCCESS ) {
223 snprintf( textbuf, textlen,
224 "unrecognized objectClass '%s'",
225 aoc->a_vals[0].bv_val );
226 return LDAP_OBJECT_CLASS_VIOLATION;
228 } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
229 snprintf( textbuf, textlen,
230 "structural object class modification "
231 "from '%s' to '%s' not allowed",
232 asc->a_vals[0].bv_val, nsc.bv_val );
233 return LDAP_NO_OBJECT_CLASS_MODS;
234 } else if ( sc == slap_schema.si_oc_glue ) {
239 if ( !is_entry_objectclass ( e, slap_schema.si_oc_glue, 0 ) ) {
240 rc = entry_naming_check( e, text, textbuf, textlen );
241 if( rc != LDAP_SUCCESS ) {
248 /* find the content rule for the structural class */
249 cr = cr_find( sc->soc_oid );
251 /* the cr must be same as the structural class */
252 assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
254 /* check that the entry has required attrs of the content rule */
256 if( cr->scr_obsolete ) {
257 snprintf( textbuf, textlen,
258 "content rule '%s' is obsolete",
259 ldap_contentrule2name( &cr->scr_crule ));
262 LDAP_LOG( OPERATION, INFO,
263 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
265 Debug( LDAP_DEBUG_ANY,
267 e->e_dn, textbuf, 0 );
270 return LDAP_OBJECT_CLASS_VIOLATION;
273 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
274 at = cr->scr_required[i];
276 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
277 if( a->a_desc->ad_type == at ) {
282 /* not there => schema violation */
284 snprintf( textbuf, textlen,
285 "content rule '%s' requires attribute '%s'",
286 ldap_contentrule2name( &cr->scr_crule ),
287 at->sat_cname.bv_val );
290 LDAP_LOG( OPERATION, INFO,
291 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
293 Debug( LDAP_DEBUG_ANY,
295 e->e_dn, textbuf, 0 );
298 return LDAP_OBJECT_CLASS_VIOLATION;
302 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
303 at = cr->scr_precluded[i];
305 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
306 if( a->a_desc->ad_type == at ) {
311 /* there => schema violation */
313 snprintf( textbuf, textlen,
314 "content rule '%s' precluded attribute '%s'",
315 ldap_contentrule2name( &cr->scr_crule ),
316 at->sat_cname.bv_val );
319 LDAP_LOG( OPERATION, INFO,
320 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
322 Debug( LDAP_DEBUG_ANY,
324 e->e_dn, textbuf, 0 );
327 return LDAP_OBJECT_CLASS_VIOLATION;
332 /* check that the entry has required attrs for each oc */
333 for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
334 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
335 snprintf( textbuf, textlen,
336 "unrecognized objectClass '%s'",
337 aoc->a_vals[i].bv_val );
340 LDAP_LOG( OPERATION, INFO,
341 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
343 Debug( LDAP_DEBUG_ANY,
344 "entry_check_schema(%s): %s\n",
345 e->e_dn, textbuf, 0 );
348 return LDAP_OBJECT_CLASS_VIOLATION;
351 if ( oc->soc_obsolete ) {
352 /* disallow obsolete classes */
353 snprintf( textbuf, textlen,
354 "objectClass '%s' is OBSOLETE",
355 aoc->a_vals[i].bv_val );
358 LDAP_LOG( OPERATION, INFO,
359 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
361 Debug( LDAP_DEBUG_ANY,
362 "entry_check_schema(%s): %s\n",
363 e->e_dn, textbuf, 0 );
366 return LDAP_OBJECT_CLASS_VIOLATION;
369 if ( oc->soc_check ) {
370 int rc = (oc->soc_check)( be, e, oc,
371 text, textbuf, textlen );
372 if( rc != LDAP_SUCCESS ) {
377 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
378 /* object class is abstract */
379 if ( oc != slap_schema.si_oc_top &&
380 !is_object_subclass( oc, sc ))
383 ObjectClass *xc = NULL;
384 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
386 xc = oc_bvfind( &aoc->a_vals[i] );
388 snprintf( textbuf, textlen,
389 "unrecognized objectClass '%s'",
390 aoc->a_vals[i].bv_val );
393 LDAP_LOG( OPERATION, INFO,
394 "entry_schema_check: dn (%s), %s\n",
395 e->e_dn, textbuf, 0 );
397 Debug( LDAP_DEBUG_ANY,
398 "entry_check_schema(%s): %s\n",
399 e->e_dn, textbuf, 0 );
402 return LDAP_OBJECT_CLASS_VIOLATION;
405 /* since we previous check against the
406 * structural object of this entry, the
407 * abstract class must be a (direct or indirect)
408 * superclass of one of the auxiliary classes of
411 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
412 is_object_subclass( oc, xc ) )
421 snprintf( textbuf, textlen, "instanstantiation of "
422 "abstract objectClass '%s' not allowed",
423 aoc->a_vals[i].bv_val );
426 LDAP_LOG( OPERATION, INFO,
427 "entry_schema_check: dn (%s), %s\n",
428 e->e_dn, textbuf, 0 );
430 Debug( LDAP_DEBUG_ANY,
431 "entry_check_schema(%s): %s\n",
432 e->e_dn, textbuf, 0 );
435 return LDAP_OBJECT_CLASS_VIOLATION;
439 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
442 if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
449 if( cr->scr_auxiliaries ) {
450 for( j = 0; cr->scr_auxiliaries[j]; j++ ) {
451 if( cr->scr_auxiliaries[j] == oc ) {
457 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
464 snprintf( textbuf, textlen,
465 "content rule '%s' does not allow class '%s'",
466 ldap_contentrule2name( &cr->scr_crule ),
467 oc->soc_cname.bv_val );
470 LDAP_LOG( OPERATION, INFO,
471 "entry_schema_check: dn=\"%s\" %s",
472 e->e_dn, textbuf, 0 );
474 Debug( LDAP_DEBUG_ANY,
476 e->e_dn, textbuf, 0 );
479 return LDAP_OBJECT_CLASS_VIOLATION;
483 s = oc_check_required( e, oc, &aoc->a_vals[i] );
485 snprintf( textbuf, textlen,
486 "object class '%s' requires attribute '%s'",
487 aoc->a_vals[i].bv_val, s );
490 LDAP_LOG( OPERATION, INFO,
491 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
493 Debug( LDAP_DEBUG_ANY,
495 e->e_dn, textbuf, 0 );
498 return LDAP_OBJECT_CLASS_VIOLATION;
501 if( oc == slap_schema.si_oc_extensibleObject ) {
511 /* check that each attr in the entry is allowed by some oc */
512 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
515 ret = LDAP_OBJECT_CLASS_VIOLATION;
517 if( cr && cr->scr_required ) {
518 for( i=0; cr->scr_required[i]; i++ ) {
519 if( cr->scr_required[i] == a->a_desc->ad_type ) {
526 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
527 for( i=0; cr->scr_allowed[i]; i++ ) {
528 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
535 if( ret != LDAP_SUCCESS )
537 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
540 if ( ret != LDAP_SUCCESS ) {
541 char *type = a->a_desc->ad_cname.bv_val;
543 snprintf( textbuf, textlen,
544 "attribute '%s' not allowed",
548 LDAP_LOG( OPERATION, INFO,
549 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0);
551 Debug( LDAP_DEBUG_ANY,
553 e->e_dn, textbuf, 0 );
567 struct berval *ocname )
574 LDAP_LOG( OPERATION, ENTRY,
575 "oc_check_required: dn (%s), objectClass \"%s\"\n",
576 e->e_dn, ocname->bv_val, 0 );
578 Debug( LDAP_DEBUG_TRACE,
579 "oc_check_required entry (%s), objectClass \"%s\"\n",
580 e->e_dn, ocname->bv_val, 0 );
584 /* check for empty oc_required */
585 if(oc->soc_required == NULL) {
589 /* for each required attribute */
590 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
591 at = oc->soc_required[i];
592 /* see if it's in the entry */
593 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
594 if( a->a_desc->ad_type == at ) {
598 /* not there => schema violation */
600 return at->sat_cname.bv_val;
607 int oc_check_allowed(
615 LDAP_LOG( OPERATION, ENTRY,
616 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val, 0, 0 );
618 Debug( LDAP_DEBUG_TRACE,
619 "oc_check_allowed type \"%s\"\n",
620 at->sat_cname.bv_val, 0, 0 );
623 /* always allow objectClass attribute */
624 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
629 * All operational attributions are allowed by schema rules.
631 if( is_at_operational(at) ) {
635 /* check to see if its allowed by the structuralObjectClass */
637 /* does it require the type? */
638 for ( j = 0; sc->soc_required != NULL &&
639 sc->soc_required[j] != NULL; j++ )
641 if( at == sc->soc_required[j] ) {
646 /* does it allow the type? */
647 for ( j = 0; sc->soc_allowed != NULL &&
648 sc->soc_allowed[j] != NULL; j++ )
650 if( at == sc->soc_allowed[j] ) {
656 /* check that the type appears as req or opt in at least one oc */
657 for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
658 /* if we know about the oc */
659 ObjectClass *oc = oc_bvfind( &ocl[i] );
660 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
661 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
663 /* does it require the type? */
664 for ( j = 0; oc->soc_required != NULL &&
665 oc->soc_required[j] != NULL; j++ )
667 if( at == oc->soc_required[j] ) {
671 /* does it allow the type? */
672 for ( j = 0; oc->soc_allowed != NULL &&
673 oc->soc_allowed[j] != NULL; j++ )
675 if( at == oc->soc_allowed[j] ) {
682 /* not allowed by any oc */
683 return LDAP_OBJECT_CLASS_VIOLATION;
687 * Determine the structural object class from a set of OIDs
689 int structural_class(
694 char *textbuf, size_t textlen )
698 ObjectClass *sc = NULL;
701 *text = "structural_class: internal error";
704 for( i=0; ocs[i].bv_val; i++ ) {
705 oc = oc_bvfind( &ocs[i] );
708 snprintf( textbuf, textlen,
709 "unrecognized objectClass '%s'",
712 return LDAP_OBJECT_CLASS_VIOLATION;
715 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
716 if( sc == NULL || is_object_subclass( sc, oc ) ) {
720 } else if ( !is_object_subclass( oc, sc ) ) {
722 ObjectClass *xc = NULL;
724 /* find common superior */
725 for( j=i+1; ocs[j].bv_val; j++ ) {
726 xc = oc_bvfind( &ocs[j] );
729 snprintf( textbuf, textlen,
730 "unrecognized objectClass '%s'",
733 return LDAP_OBJECT_CLASS_VIOLATION;
736 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
741 if( is_object_subclass( sc, xc ) &&
742 is_object_subclass( oc, xc ) )
744 /* found common subclass */
752 /* no common subclass */
753 snprintf( textbuf, textlen,
754 "invalid structural object class chain (%s/%s)",
755 ocs[scn].bv_val, ocs[i].bv_val );
757 return LDAP_OBJECT_CLASS_VIOLATION;
768 *text = "no structural object class provided";
769 return LDAP_OBJECT_CLASS_VIOLATION;
773 *text = "invalid structural object class";
774 return LDAP_OBJECT_CLASS_VIOLATION;
779 if( scbv->bv_len == 0 ) {
780 *text = "invalid structural object class";
781 return LDAP_OBJECT_CLASS_VIOLATION;
788 * Return structural object class from list of modifications
790 int mods_structural_class(
794 char *textbuf, size_t textlen )
796 Modifications *ocmod = NULL;
798 for( ; mods != NULL; mods = mods->sml_next ) {
799 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
800 if( ocmod != NULL ) {
801 *text = "entry has multiple objectClass attributes";
802 return LDAP_OBJECT_CLASS_VIOLATION;
808 if( ocmod == NULL ) {
809 *text = "entry has no objectClass attribute";
810 return LDAP_OBJECT_CLASS_VIOLATION;
813 if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
814 *text = "objectClass attribute has no values";
815 return LDAP_OBJECT_CLASS_VIOLATION;
818 return structural_class( ocmod->sml_bvalues, sc, NULL,
819 text, textbuf, textlen );
827 char *textbuf, size_t textlen )
831 const char *p = NULL;
833 int rc = LDAP_SUCCESS;
836 * Get attribute type(s) and attribute value(s) of our RDN
838 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
839 LDAP_DN_FORMAT_LDAP ) )
841 *text = "unrecongized attribute type(s) in RDN";
842 return LDAP_INVALID_DN_SYNTAX;
845 /* Check that each AVA of the RDN is present in the entry */
846 /* FIXME: Should also check that each AVA lists a distinct type */
847 for ( cnt = 0; rdn[cnt]; cnt++ ) {
848 LDAPAVA *ava = rdn[cnt];
849 AttributeDescription *desc = NULL;
853 if( ava->la_flags & LDAP_AVA_BINARY ) {
854 snprintf( textbuf, textlen,
855 "value of naming attribute '%s' in unsupported BER form",
856 ava->la_attr.bv_val );
857 rc = LDAP_NAMING_VIOLATION;
860 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
861 if ( rc != LDAP_SUCCESS ) {
862 snprintf( textbuf, textlen, "%s (in RDN)", errtext );
866 if( desc->ad_type->sat_usage ) {
867 snprintf( textbuf, textlen,
868 "naming attribute '%s' is operational",
869 ava->la_attr.bv_val );
870 rc = LDAP_NAMING_VIOLATION;
874 if( desc->ad_type->sat_collective ) {
875 snprintf( textbuf, textlen,
876 "naming attribute '%s' is collective",
877 ava->la_attr.bv_val );
878 rc = LDAP_NAMING_VIOLATION;
882 if( desc->ad_type->sat_obsolete ) {
883 snprintf( textbuf, textlen,
884 "naming attribute '%s' is collective",
885 ava->la_attr.bv_val );
886 rc = LDAP_NAMING_VIOLATION;
890 if( !desc->ad_type->sat_equality ) {
891 snprintf( textbuf, textlen,
892 "naming attribute '%s' has no equality matching rule",
893 ava->la_attr.bv_val );
894 rc = LDAP_NAMING_VIOLATION;
898 if( !desc->ad_type->sat_equality->smr_match ) {
899 snprintf( textbuf, textlen,
900 "naming attribute '%s' has unsupported equality matching rule",
901 ava->la_attr.bv_val );
902 rc = LDAP_NAMING_VIOLATION;
906 /* find the naming attribute */
907 attr = attr_find( e->e_attrs, desc );
908 if ( attr == NULL ) {
909 snprintf( textbuf, textlen,
910 "naming attribute '%s' is not present in entry",
911 ava->la_attr.bv_val );
912 rc = LDAP_NAMING_VIOLATION;
916 rc = value_find_ex( desc, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX|
917 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
918 attr->a_nvals, &ava->la_value, NULL );
922 case LDAP_INAPPROPRIATE_MATCHING:
923 snprintf( textbuf, textlen,
924 "inappropriate matching for naming attribute '%s'",
925 ava->la_attr.bv_val );
927 case LDAP_INVALID_SYNTAX:
928 snprintf( textbuf, textlen,
929 "value of naming attribute '%s' is invalid",
930 ava->la_attr.bv_val );
932 case LDAP_NO_SUCH_ATTRIBUTE:
933 snprintf( textbuf, textlen,
934 "value of naming attribute '%s' is not present in entry",
935 ava->la_attr.bv_val );
938 snprintf( textbuf, textlen,
939 "naming attribute '%s' is inappropriate",
940 ava->la_attr.bv_val );
942 rc = LDAP_NAMING_VIOLATION;