1 /* schema_check.c - routines to enforce schema definitions */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
19 static char * oc_check_required(
22 struct berval *ocname );
24 static int entry_naming_check(
27 char *textbuf, size_t textlen );
29 * entry_schema_check - check that entry e conforms to the schema required
30 * by its object class(es).
32 * returns 0 if so, non-zero otherwise.
41 char *textbuf, size_t textlen )
43 Attribute *a, *asc, *aoc;
45 #ifdef SLAP_EXTENDED_SCHEMA
51 AttributeDescription *ad_structuralObjectClass
52 = slap_schema.si_ad_structuralObjectClass;
53 AttributeDescription *ad_objectClass
54 = slap_schema.si_ad_objectClass;
56 int subentry = is_entry_subentry( e );
57 int collectiveSubentry = 0;
60 collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
65 /* misc attribute checks */
66 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
67 const char *type = a->a_desc->ad_cname.bv_val;
69 /* there should be at least one value */
71 assert( a->a_vals[0].bv_val != NULL );
73 if( a->a_desc->ad_type->sat_check ) {
74 int rc = (a->a_desc->ad_type->sat_check)(
75 be, e, a, text, textbuf, textlen );
76 if( rc != LDAP_SUCCESS ) {
81 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
82 snprintf( textbuf, textlen,
83 "'%s' can only appear in collectiveAttributeSubentry",
85 return LDAP_OBJECT_CLASS_VIOLATION;
88 /* if single value type, check for multiple values */
89 if( is_at_single_value( a->a_desc->ad_type ) &&
90 a->a_vals[1].bv_val != NULL )
92 snprintf( textbuf, textlen,
93 "attribute '%s' cannot have multiple values",
97 LDAP_LOG( OPERATION, INFO,
98 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0 );
100 Debug( LDAP_DEBUG_ANY,
102 e->e_dn, textbuf, 0 );
105 return LDAP_CONSTRAINT_VIOLATION;
109 /* it's a REALLY bad idea to disable schema checks */
110 if( !global_schemacheck ) return LDAP_SUCCESS;
112 /* find the structural object class attribute */
113 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
116 LDAP_LOG( OPERATION, INFO,
117 "entry_schema_check: No structuralObjectClass for entry (%s)\n",
120 Debug( LDAP_DEBUG_ANY,
121 "No structuralObjectClass for entry (%s)\n",
125 *text = "no structuralObjectClass operational attribute";
129 assert( asc->a_vals != NULL );
130 assert( asc->a_vals[0].bv_val != NULL );
131 assert( asc->a_vals[1].bv_val == NULL );
133 sc = oc_bvfind( &asc->a_vals[0] );
135 snprintf( textbuf, textlen,
136 "unrecognized structuralObjectClass '%s'",
137 asc->a_vals[0].bv_val );
140 LDAP_LOG( OPERATION, INFO,
141 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
143 Debug( LDAP_DEBUG_ANY,
144 "entry_check_schema(%s): %s\n",
145 e->e_dn, textbuf, 0 );
148 return LDAP_OBJECT_CLASS_VIOLATION;
151 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
152 snprintf( textbuf, textlen,
153 "structuralObjectClass '%s' is not STRUCTURAL",
154 asc->a_vals[0].bv_val );
157 LDAP_LOG( OPERATION, INFO,
158 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
160 Debug( LDAP_DEBUG_ANY,
161 "entry_check_schema(%s): %s\n",
162 e->e_dn, textbuf, 0 );
168 if( sc->soc_obsolete ) {
169 snprintf( textbuf, textlen,
170 "structuralObjectClass '%s' is OBSOLETE",
171 asc->a_vals[0].bv_val );
174 LDAP_LOG( OPERATION, INFO,
175 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
177 Debug( LDAP_DEBUG_ANY,
178 "entry_check_schema(%s): %s\n",
179 e->e_dn, textbuf, 0 );
182 return LDAP_OBJECT_CLASS_VIOLATION;
185 /* find the object class attribute */
186 aoc = attr_find( e->e_attrs, ad_objectClass );
189 LDAP_LOG( OPERATION, INFO,
190 "entry_schema_check: No objectClass for entry (%s).\n",
193 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
197 *text = "no objectClass attribute";
198 return LDAP_OBJECT_CLASS_VIOLATION;
201 assert( aoc->a_vals != NULL );
202 assert( aoc->a_vals[0].bv_val != NULL );
204 rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
205 if( rc != LDAP_SUCCESS ) {
212 snprintf( textbuf, textlen,
213 "unrecognized objectClass '%s'",
214 aoc->a_vals[0].bv_val );
215 return LDAP_OBJECT_CLASS_VIOLATION;
217 } else if ( sc != oc ) {
218 snprintf( textbuf, textlen,
219 "structural object class modification "
220 "from '%s' to '%s' not allowed",
221 asc->a_vals[0].bv_val, nsc.bv_val );
222 return LDAP_NO_OBJECT_CLASS_MODS;
227 if ( !is_entry_objectclass ( e, slap_schema.si_oc_glue, 0 ) ) {
228 rc = entry_naming_check( e, text, textbuf, textlen );
229 if( rc != LDAP_SUCCESS ) {
233 printf("glue !!!\n");
236 rc = entry_naming_check( e, text, textbuf, textlen );
237 if( rc != LDAP_SUCCESS ) {
242 #ifdef SLAP_EXTENDED_SCHEMA
243 /* find the content rule for the structural class */
244 cr = cr_find( sc->soc_oid );
246 /* the cr must be same as the structural class */
247 assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
249 /* check that the entry has required attrs of the content rule */
251 if( cr->scr_obsolete ) {
252 snprintf( textbuf, textlen,
253 "content rule '%s' is obsolete",
254 ldap_contentrule2name( &cr->scr_crule ));
257 LDAP_LOG( OPERATION, INFO,
258 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
260 Debug( LDAP_DEBUG_ANY,
262 e->e_dn, textbuf, 0 );
265 return LDAP_OBJECT_CLASS_VIOLATION;
268 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
269 at = cr->scr_required[i];
271 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
272 if( a->a_desc->ad_type == at ) {
277 /* not there => schema violation */
279 snprintf( textbuf, textlen,
280 "content rule '%s' requires attribute '%s'",
281 ldap_contentrule2name( &cr->scr_crule ),
282 at->sat_cname.bv_val );
285 LDAP_LOG( OPERATION, INFO,
286 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
288 Debug( LDAP_DEBUG_ANY,
290 e->e_dn, textbuf, 0 );
293 return LDAP_OBJECT_CLASS_VIOLATION;
297 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
298 at = cr->scr_precluded[i];
300 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
301 if( a->a_desc->ad_type == at ) {
306 /* there => schema violation */
308 snprintf( textbuf, textlen,
309 "content rule '%s' precluded attribute '%s'",
310 ldap_contentrule2name( &cr->scr_crule ),
311 at->sat_cname.bv_val );
314 LDAP_LOG( OPERATION, INFO,
315 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
317 Debug( LDAP_DEBUG_ANY,
319 e->e_dn, textbuf, 0 );
322 return LDAP_OBJECT_CLASS_VIOLATION;
326 #endif /* SLAP_EXTENDED_SCHEMA */
328 /* check that the entry has required attrs for each oc */
329 for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
330 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
331 snprintf( textbuf, textlen,
332 "unrecognized objectClass '%s'",
333 aoc->a_vals[i].bv_val );
336 LDAP_LOG( OPERATION, INFO,
337 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
339 Debug( LDAP_DEBUG_ANY,
340 "entry_check_schema(%s): %s\n",
341 e->e_dn, textbuf, 0 );
344 return LDAP_OBJECT_CLASS_VIOLATION;
347 if ( oc->soc_obsolete ) {
348 /* disallow obsolete classes */
349 snprintf( textbuf, textlen,
350 "objectClass '%s' is OBSOLETE",
351 aoc->a_vals[i].bv_val );
354 LDAP_LOG( OPERATION, INFO,
355 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
357 Debug( LDAP_DEBUG_ANY,
358 "entry_check_schema(%s): %s\n",
359 e->e_dn, textbuf, 0 );
362 return LDAP_OBJECT_CLASS_VIOLATION;
365 if ( oc->soc_check ) {
366 int rc = (oc->soc_check)( be, e, oc,
367 text, textbuf, textlen );
368 if( rc != LDAP_SUCCESS ) {
373 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
374 /* object class is abstract */
375 if ( oc != slap_schema.si_oc_top &&
376 !is_object_subclass( oc, sc ))
379 ObjectClass *xc = NULL;
380 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
382 xc = oc_bvfind( &aoc->a_vals[i] );
384 snprintf( textbuf, textlen,
385 "unrecognized objectClass '%s'",
386 aoc->a_vals[i].bv_val );
389 LDAP_LOG( OPERATION, INFO,
390 "entry_schema_check: dn (%s), %s\n",
391 e->e_dn, textbuf, 0 );
393 Debug( LDAP_DEBUG_ANY,
394 "entry_check_schema(%s): %s\n",
395 e->e_dn, textbuf, 0 );
398 return LDAP_OBJECT_CLASS_VIOLATION;
401 /* since we previous check against the
402 * structural object of this entry, the
403 * abstract class must be a (direct or indirect)
404 * superclass of one of the auxiliary classes of
407 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
408 is_object_subclass( oc, xc ) )
417 snprintf( textbuf, textlen, "instanstantiation of "
418 "abstract objectClass '%s' not allowed",
419 aoc->a_vals[i].bv_val );
422 LDAP_LOG( OPERATION, INFO,
423 "entry_schema_check: dn (%s), %s\n",
424 e->e_dn, textbuf, 0 );
426 Debug( LDAP_DEBUG_ANY,
427 "entry_check_schema(%s): %s\n",
428 e->e_dn, textbuf, 0 );
431 return LDAP_OBJECT_CLASS_VIOLATION;
435 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
438 #ifdef SLAP_EXTENDED_SCHEMA
439 if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
442 if( cr->scr_auxiliaries ) {
443 for( ; cr->scr_auxiliaries[k]; k++ ) {
444 if( cr->scr_auxiliaries[k] == oc ) {
450 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
455 snprintf( textbuf, textlen,
456 "content rule '%s' does not allow class '%s'",
457 ldap_contentrule2name( &cr->scr_crule ),
458 oc->soc_cname.bv_val );
461 LDAP_LOG( OPERATION, INFO,
462 "entry_schema_check: dn=\"%s\" %s",
463 e->e_dn, textbuf, 0 );
465 Debug( LDAP_DEBUG_ANY,
467 e->e_dn, textbuf, 0 );
470 return LDAP_OBJECT_CLASS_VIOLATION;
473 #endif /* SLAP_EXTENDED_SCHEMA */
475 s = oc_check_required( e, oc, &aoc->a_vals[i] );
477 snprintf( textbuf, textlen,
478 "object class '%s' requires attribute '%s'",
479 aoc->a_vals[i].bv_val, s );
482 LDAP_LOG( OPERATION, INFO,
483 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
485 Debug( LDAP_DEBUG_ANY,
487 e->e_dn, textbuf, 0 );
490 return LDAP_OBJECT_CLASS_VIOLATION;
493 if( oc == slap_schema.si_oc_extensibleObject ) {
503 /* check that each attr in the entry is allowed by some oc */
504 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
507 #ifdef SLAP_EXTENDED_SCHEMA
508 ret = LDAP_OBJECT_CLASS_VIOLATION;
510 if( cr && cr->scr_required ) {
511 for( i=0; cr->scr_required[i]; i++ ) {
512 if( cr->scr_required[i] == a->a_desc->ad_type ) {
519 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
520 for( i=0; cr->scr_allowed[i]; i++ ) {
521 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
528 if( ret != LDAP_SUCCESS )
529 #endif /* SLAP_EXTENDED_SCHEMA */
531 ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
534 if ( ret != LDAP_SUCCESS ) {
535 char *type = a->a_desc->ad_cname.bv_val;
537 snprintf( textbuf, textlen,
538 "attribute '%s' not allowed",
542 LDAP_LOG( OPERATION, INFO,
543 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0);
545 Debug( LDAP_DEBUG_ANY,
547 e->e_dn, textbuf, 0 );
561 struct berval *ocname )
568 LDAP_LOG( OPERATION, ENTRY,
569 "oc_check_required: dn (%s), objectClass \"%s\"\n",
570 e->e_dn, ocname->bv_val, 0 );
572 Debug( LDAP_DEBUG_TRACE,
573 "oc_check_required entry (%s), objectClass \"%s\"\n",
574 e->e_dn, ocname->bv_val, 0 );
578 /* check for empty oc_required */
579 if(oc->soc_required == NULL) {
583 /* for each required attribute */
584 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
585 at = oc->soc_required[i];
586 /* see if it's in the entry */
587 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
588 if( a->a_desc->ad_type == at ) {
592 /* not there => schema violation */
594 return at->sat_cname.bv_val;
601 int oc_check_allowed(
609 LDAP_LOG( OPERATION, ENTRY,
610 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val, 0, 0 );
612 Debug( LDAP_DEBUG_TRACE,
613 "oc_check_allowed type \"%s\"\n",
614 at->sat_cname.bv_val, 0, 0 );
617 /* always allow objectClass attribute */
618 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
623 * All operational attributions are allowed by schema rules.
625 if( is_at_operational(at) ) {
629 /* check to see if its allowed by the structuralObjectClass */
631 /* does it require the type? */
632 for ( j = 0; sc->soc_required != NULL &&
633 sc->soc_required[j] != NULL; j++ )
635 if( at == sc->soc_required[j] ) {
640 /* does it allow the type? */
641 for ( j = 0; sc->soc_allowed != NULL &&
642 sc->soc_allowed[j] != NULL; j++ )
644 if( at == sc->soc_allowed[j] ) {
650 /* check that the type appears as req or opt in at least one oc */
651 for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
652 /* if we know about the oc */
653 ObjectClass *oc = oc_bvfind( &ocl[i] );
654 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
655 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
657 /* does it require the type? */
658 for ( j = 0; oc->soc_required != NULL &&
659 oc->soc_required[j] != NULL; j++ )
661 if( at == oc->soc_required[j] ) {
665 /* does it allow the type? */
666 for ( j = 0; oc->soc_allowed != NULL &&
667 oc->soc_allowed[j] != NULL; j++ )
669 if( at == oc->soc_allowed[j] ) {
676 /* not allowed by any oc */
677 return LDAP_OBJECT_CLASS_VIOLATION;
681 * Determine the structural object class from a set of OIDs
683 int structural_class(
688 char *textbuf, size_t textlen )
692 ObjectClass *sc = NULL;
695 *text = "structural_class: internal error";
698 for( i=0; ocs[i].bv_val; i++ ) {
699 oc = oc_bvfind( &ocs[i] );
702 snprintf( textbuf, textlen,
703 "unrecognized objectClass '%s'",
706 return LDAP_OBJECT_CLASS_VIOLATION;
709 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
710 if( sc == NULL || is_object_subclass( sc, oc ) ) {
714 } else if ( !is_object_subclass( oc, sc ) ) {
716 ObjectClass *xc = NULL;
718 /* find common superior */
719 for( j=i+1; ocs[j].bv_val; j++ ) {
720 xc = oc_bvfind( &ocs[j] );
723 snprintf( textbuf, textlen,
724 "unrecognized objectClass '%s'",
727 return LDAP_OBJECT_CLASS_VIOLATION;
730 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
735 if( is_object_subclass( sc, xc ) &&
736 is_object_subclass( oc, xc ) )
738 /* found common subclass */
746 /* no common subclass */
747 snprintf( textbuf, textlen,
748 "invalid structural object class chain (%s/%s)",
749 ocs[scn].bv_val, ocs[i].bv_val );
751 return LDAP_OBJECT_CLASS_VIOLATION;
762 *text = "no structural object class provided";
763 return LDAP_OBJECT_CLASS_VIOLATION;
767 *text = "invalid structural object class";
768 return LDAP_OBJECT_CLASS_VIOLATION;
773 if( scbv->bv_len == 0 ) {
774 *text = "invalid structural object class";
775 return LDAP_OBJECT_CLASS_VIOLATION;
782 * Return structural object class from list of modifications
784 int mods_structural_class(
788 char *textbuf, size_t textlen )
790 Modifications *ocmod = NULL;
792 for( ; mods != NULL; mods = mods->sml_next ) {
793 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
794 if( ocmod != NULL ) {
795 *text = "entry has multiple objectClass attributes";
796 return LDAP_OBJECT_CLASS_VIOLATION;
802 if( ocmod == NULL ) {
803 *text = "entry has no objectClass attribute";
804 return LDAP_OBJECT_CLASS_VIOLATION;
807 if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
808 *text = "objectClass attribute has no values";
809 return LDAP_OBJECT_CLASS_VIOLATION;
812 return structural_class( ocmod->sml_bvalues, sc, NULL,
813 text, textbuf, textlen );
821 char *textbuf, size_t textlen )
825 const char *p = NULL;
827 int rc = LDAP_SUCCESS;
830 * Get attribute type(s) and attribute value(s) of our RDN
832 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
833 LDAP_DN_FORMAT_LDAP ) )
835 *text = "unrecongized attribute type(s) in RDN";
836 return LDAP_INVALID_DN_SYNTAX;
839 /* Check that each AVA of the RDN is present in the entry */
840 /* FIXME: Should also check that each AVA lists a distinct type */
841 for ( cnt = 0; rdn[cnt]; cnt++ ) {
842 LDAPAVA *ava = rdn[cnt];
843 AttributeDescription *desc = NULL;
847 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
848 if ( rc != LDAP_SUCCESS ) {
849 snprintf( textbuf, textlen, "%s (in RDN)", errtext );
853 /* find the naming attribute */
854 attr = attr_find( e->e_attrs, desc );
855 if ( attr == NULL ) {
856 snprintf( textbuf, textlen,
857 "naming attribute '%s' is not present in entry",
858 ava->la_attr.bv_val );
859 rc = LDAP_NAMING_VIOLATION;
863 if( ava->la_flags & LDAP_AVA_BINARY ) {
864 snprintf( textbuf, textlen,
865 "value of naming attribute '%s' in unsupported BER form",
866 ava->la_attr.bv_val );
867 rc = LDAP_NAMING_VIOLATION;
870 if ( value_find_ex( desc,
871 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
873 &ava->la_value, NULL ) != 0 )
875 snprintf( textbuf, textlen,
876 "value of naming attribute '%s' is not present in entry",
877 ava->la_attr.bv_val );
878 rc = LDAP_NAMING_VIOLATION;