1 /* schema_check.c - routines to enforce schema definitions */
4 * Copyright 1998-2002 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 );
25 * entry_schema_check - check that entry e conforms to the schema required
26 * by its object class(es).
28 * returns 0 if so, non-zero otherwise.
37 char *textbuf, size_t textlen )
39 Attribute *a, *asc, *aoc;
43 AttributeDescription *ad_structuralObjectClass
44 = slap_schema.si_ad_structuralObjectClass;
45 AttributeDescription *ad_objectClass
46 = slap_schema.si_ad_objectClass;
48 int subentry = is_entry_subentry( e );
49 int collectiveSubentry = 0;
51 if( subentry) collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
55 /* misc attribute checks */
56 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
57 const char *type = a->a_desc->ad_cname.bv_val;
59 /* there should be at least one value */
61 assert( a->a_vals[0].bv_val != NULL );
63 if( a->a_desc->ad_type->sat_check ) {
64 int rc = (a->a_desc->ad_type->sat_check)(
65 be, e, a, text, textbuf, textlen );
66 if( rc != LDAP_SUCCESS ) {
71 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
72 snprintf( textbuf, textlen,
73 "'%s' can only appear in collectiveAttributeSubentry",
75 return LDAP_OBJECT_CLASS_VIOLATION;
78 /* if single value type, check for multiple values */
79 if( is_at_single_value( a->a_desc->ad_type ) &&
80 a->a_vals[1].bv_val != NULL )
82 snprintf( textbuf, textlen,
83 "attribute '%s' cannot have multiple values",
87 LDAP_LOG( OPERATION, INFO,
88 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0 );
90 Debug( LDAP_DEBUG_ANY,
92 e->e_dn, textbuf, 0 );
95 return LDAP_CONSTRAINT_VIOLATION;
99 /* it's a REALLY bad idea to disable schema checks */
100 if( !global_schemacheck ) return LDAP_SUCCESS;
102 /* find the object class attribute - could error out here */
103 asc = attr_find( e->e_attrs, ad_structuralObjectClass );
106 LDAP_LOG( OPERATION, INFO,
107 "entry_schema_check: No structuralObjectClass for entry (%s)\n",
110 Debug( LDAP_DEBUG_ANY,
111 "No structuralObjectClass for entry (%s)\n",
115 *text = "no structuralObjectClass operational attribute";
116 return LDAP_OBJECT_CLASS_VIOLATION;
119 assert( asc->a_vals != NULL );
120 assert( asc->a_vals[0].bv_val != NULL );
121 assert( asc->a_vals[1].bv_val == NULL );
123 sc = oc_bvfind( &asc->a_vals[0] );
125 snprintf( textbuf, textlen,
126 "unrecognized structuralObjectClass '%s'",
127 asc->a_vals[0].bv_val );
130 LDAP_LOG( OPERATION, INFO,
131 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
133 Debug( LDAP_DEBUG_ANY,
134 "entry_check_schema(%s): %s\n",
135 e->e_dn, textbuf, 0 );
138 return LDAP_OBJECT_CLASS_VIOLATION;
141 if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
142 snprintf( textbuf, textlen,
143 "structuralObjectClass '%s' is not STRUCTURAL",
144 asc->a_vals[0].bv_val );
147 LDAP_LOG( OPERATION, INFO,
148 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
150 Debug( LDAP_DEBUG_ANY,
151 "entry_check_schema(%s): %s\n",
152 e->e_dn, textbuf, 0 );
155 return LDAP_OBJECT_CLASS_VIOLATION;
158 /* find the object class attribute */
159 aoc = attr_find( e->e_attrs, ad_objectClass );
162 LDAP_LOG( OPERATION, INFO,
163 "entry_schema_check: No objectClass for entry (%s).\n",
166 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
170 *text = "no objectClass attribute";
171 return LDAP_OBJECT_CLASS_VIOLATION;
174 assert( aoc->a_vals != NULL );
175 assert( aoc->a_vals[0].bv_val != NULL );
177 rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
178 if( rc != LDAP_SUCCESS ) {
180 } else if ( nsc.bv_len == 0 ) {
181 return LDAP_OBJECT_CLASS_VIOLATION;
187 snprintf( textbuf, textlen,
188 "unrecognized objectClass '%s'",
189 aoc->a_vals[0].bv_val );
190 return LDAP_OBJECT_CLASS_VIOLATION;
192 } else if ( sc != oc ) {
193 snprintf( textbuf, textlen,
194 "structuralObjectClass modification from '%s' to '%s' not allowed",
195 asc->a_vals[0].bv_val, nsc.bv_val );
196 return LDAP_NO_OBJECT_CLASS_MODS;
199 /* check that the entry has required attrs for each oc */
200 for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
201 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
202 snprintf( textbuf, textlen,
203 "unrecognized objectClass '%s'",
204 aoc->a_vals[i].bv_val );
207 LDAP_LOG( OPERATION, INFO,
208 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
210 Debug( LDAP_DEBUG_ANY,
211 "entry_check_schema(%s): %s\n",
212 e->e_dn, textbuf, 0 );
215 return LDAP_OBJECT_CLASS_VIOLATION;
218 if ( oc->soc_check ) {
219 int rc = (oc->soc_check)( be, e, oc,
220 text, textbuf, textlen );
221 if( rc != LDAP_SUCCESS ) {
226 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
227 /* object class is abstract */
228 if ( oc != slap_schema.si_oc_top &&
229 !is_object_subclass( oc, sc ))
232 ObjectClass *xc = NULL;
233 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
235 xc = oc_bvfind( &aoc->a_vals[i] );
237 snprintf( textbuf, textlen,
238 "unrecognized objectClass '%s'",
239 aoc->a_vals[i].bv_val );
242 LDAP_LOG( OPERATION, INFO,
243 "entry_schema_check: dn (%s), %s\n",
244 e->e_dn, textbuf, 0 );
246 Debug( LDAP_DEBUG_ANY,
247 "entry_check_schema(%s): %s\n",
248 e->e_dn, textbuf, 0 );
251 return LDAP_OBJECT_CLASS_VIOLATION;
254 /* since we previous check against the
255 * structural object of this entry, the
256 * abstract class must be a (direct or indirect)
257 * superclass of one of the auxiliary classes of
260 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
261 is_object_subclass( oc, xc ) )
271 snprintf( textbuf, textlen, "instanstantiation of "
272 "abstract objectClass '%s' not allowed",
273 aoc->a_vals[i].bv_val );
276 LDAP_LOG( OPERATION, INFO,
277 "entry_schema_check: dn (%s), %s\n",
278 e->e_dn, textbuf, 0 );
280 Debug( LDAP_DEBUG_ANY,
281 "entry_check_schema(%s): %s\n",
282 e->e_dn, textbuf, 0 );
285 return LDAP_OBJECT_CLASS_VIOLATION;
289 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
290 char *s = oc_check_required( e, oc, &aoc->a_vals[i] );
293 snprintf( textbuf, textlen,
294 "object class '%s' requires attribute '%s'",
295 aoc->a_vals[i].bv_val, s );
298 LDAP_LOG( OPERATION, INFO,
299 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
301 Debug( LDAP_DEBUG_ANY,
303 e->e_dn, textbuf, 0 );
306 return LDAP_OBJECT_CLASS_VIOLATION;
309 if( oc == slap_schema.si_oc_extensibleObject ) {
319 /* check that each attr in the entry is allowed by some oc */
320 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
321 int ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
322 if ( ret != LDAP_SUCCESS ) {
323 char *type = a->a_desc->ad_cname.bv_val;
325 snprintf( textbuf, textlen,
326 "attribute '%s' not allowed",
330 LDAP_LOG( OPERATION, INFO,
331 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0);
333 Debug( LDAP_DEBUG_ANY,
335 e->e_dn, textbuf, 0 );
349 struct berval *ocname )
356 LDAP_LOG( OPERATION, ENTRY,
357 "oc_check_required: dn (%s), objectClass \"%s\"\n",
358 e->e_dn, ocname->bv_val, 0 );
360 Debug( LDAP_DEBUG_TRACE,
361 "oc_check_required entry (%s), objectClass \"%s\"\n",
362 e->e_dn, ocname->bv_val, 0 );
366 /* check for empty oc_required */
367 if(oc->soc_required == NULL) {
371 /* for each required attribute */
372 for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
373 at = oc->soc_required[i];
374 /* see if it's in the entry */
375 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
376 if( a->a_desc->ad_type == at ) {
380 /* not there => schema violation */
382 return at->sat_cname.bv_val;
389 int oc_check_allowed(
397 LDAP_LOG( OPERATION, ENTRY,
398 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val, 0, 0 );
400 Debug( LDAP_DEBUG_TRACE,
401 "oc_check_allowed type \"%s\"\n",
402 at->sat_cname.bv_val, 0, 0 );
405 /* always allow objectClass attribute */
406 if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
411 * All operational attributions are allowed by schema rules.
413 if( is_at_operational(at) ) {
417 /* check to see if its allowed by the structuralObjectClass */
419 /* does it require the type? */
420 for ( j = 0; sc->soc_required != NULL &&
421 sc->soc_required[j] != NULL; j++ )
423 if( at == sc->soc_required[j] ) {
428 /* does it allow the type? */
429 for ( j = 0; sc->soc_allowed != NULL &&
430 sc->soc_allowed[j] != NULL; j++ )
432 if( at == sc->soc_allowed[j] ) {
438 /* check that the type appears as req or opt in at least one oc */
439 for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
440 /* if we know about the oc */
441 ObjectClass *oc = oc_bvfind( &ocl[i] );
442 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
443 ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
445 /* does it require the type? */
446 for ( j = 0; oc->soc_required != NULL &&
447 oc->soc_required[j] != NULL; j++ )
449 if( at == oc->soc_required[j] ) {
453 /* does it allow the type? */
454 for ( j = 0; oc->soc_allowed != NULL &&
455 oc->soc_allowed[j] != NULL; j++ )
457 if( at == oc->soc_allowed[j] ) {
464 /* not allowed by any oc */
465 return LDAP_OBJECT_CLASS_VIOLATION;
469 * Determine the structural object class from a set of OIDs
471 int structural_class(
476 char *textbuf, size_t textlen )
480 ObjectClass *sc = NULL;
483 *text = "structural_class: internal error";
486 for( i=0; ocs[i].bv_val; i++ ) {
487 oc = oc_bvfind( &ocs[i] );
490 snprintf( textbuf, textlen,
491 "unrecognized objectClass '%s'",
494 return LDAP_OBJECT_CLASS_VIOLATION;
497 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
498 if( sc == NULL || is_object_subclass( sc, oc ) ) {
502 } else if ( !is_object_subclass( oc, sc ) ) {
504 ObjectClass *xc = NULL;
506 /* find common superior */
507 for( j=i+1; ocs[j].bv_val; j++ ) {
508 xc = oc_bvfind( &ocs[j] );
511 snprintf( textbuf, textlen,
512 "unrecognized objectClass '%s'",
515 return LDAP_OBJECT_CLASS_VIOLATION;
518 if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
523 if( is_object_subclass( sc, xc ) &&
524 is_object_subclass( oc, xc ) )
526 /* found common subclass */
534 /* no common subclass */
535 snprintf( textbuf, textlen,
536 "invalid structural object class chain (%s/%s)",
537 ocs[scn].bv_val, ocs[i].bv_val );
539 return LDAP_OBJECT_CLASS_VIOLATION;
549 *text = "no structural object classes provided";
550 return LDAP_OBJECT_CLASS_VIOLATION;
558 * Return structural object class from list of modifications
560 int mods_structural_class(
564 char *textbuf, size_t textlen )
566 Modifications *ocmod = NULL;
568 for( ; mods != NULL; mods = mods->sml_next ) {
569 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
570 if( ocmod != NULL ) {
571 *text = "entry has multiple objectClass attributes";
572 return LDAP_OBJECT_CLASS_VIOLATION;
578 if( ocmod == NULL ) {
579 *text = "entry has no objectClass attribute";
580 return LDAP_OBJECT_CLASS_VIOLATION;
583 if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
584 *text = "objectClass attribute has no values";
585 return LDAP_OBJECT_CLASS_VIOLATION;
588 return structural_class( ocmod->sml_bvalues, sc, NULL,
589 text, textbuf, textlen );