]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
60f866f6658937985a84db55d093dc48b1593da1
[openldap] / servers / slapd / schema_check.c
1 /* schema_check.c - routines to enforce schema definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17 #include "ldap_pvt.h"
18
19 static char * oc_check_required(
20         Entry *e,
21         ObjectClass *oc,
22         struct berval *ocname );
23
24 /*
25  * entry_schema_check - check that entry e conforms to the schema required
26  * by its object class(es).
27  *
28  * returns 0 if so, non-zero otherwise.
29  */
30
31 int
32 entry_schema_check( 
33         Backend *be,
34         Entry *e,
35         Attribute *oldattrs,
36         const char** text,
37         char *textbuf, size_t textlen )
38 {
39         Attribute       *a, *asc, *aoc;
40         ObjectClass *sc, *oc;
41 #ifdef SLAP_EXTENDED_SCHEMA
42         AttributeType *at;
43         ContentRule *cr;
44 #endif
45         int     rc, i;
46         struct berval nsc;
47         AttributeDescription *ad_structuralObjectClass
48                 = slap_schema.si_ad_structuralObjectClass;
49         AttributeDescription *ad_objectClass
50                 = slap_schema.si_ad_objectClass;
51         int extensible = 0;
52         int subentry = is_entry_subentry( e );
53         int collectiveSubentry = 0;
54
55         if( subentry ) {
56                 collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
57         }
58
59         *text = textbuf;
60
61         /* misc attribute checks */
62         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
63                 const char *type = a->a_desc->ad_cname.bv_val;
64
65                 /* there should be at least one value */
66                 assert( a->a_vals );
67                 assert( a->a_vals[0].bv_val != NULL ); 
68
69                 if( a->a_desc->ad_type->sat_check ) {
70                         int rc = (a->a_desc->ad_type->sat_check)(
71                                 be, e, a, text, textbuf, textlen );
72                         if( rc != LDAP_SUCCESS ) {
73                                 return rc;
74                         }
75                 }
76
77                 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
78                         snprintf( textbuf, textlen,
79                                 "'%s' can only appear in collectiveAttributeSubentry",
80                                 type );
81                         return LDAP_OBJECT_CLASS_VIOLATION;
82                 }
83
84                 /* if single value type, check for multiple values */
85                 if( is_at_single_value( a->a_desc->ad_type ) &&
86                         a->a_vals[1].bv_val != NULL )
87                 {
88                         snprintf( textbuf, textlen, 
89                                 "attribute '%s' cannot have multiple values",
90                                 type );
91
92 #ifdef NEW_LOGGING
93                         LDAP_LOG( OPERATION, INFO, 
94                                 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0 );
95 #else
96                         Debug( LDAP_DEBUG_ANY,
97                             "Entry (%s), %s\n",
98                             e->e_dn, textbuf, 0 );
99 #endif
100
101                         return LDAP_CONSTRAINT_VIOLATION;
102                 }
103         }
104
105         /* it's a REALLY bad idea to disable schema checks */
106         if( !global_schemacheck ) return LDAP_SUCCESS;
107
108         /* find the structural object class attribute */
109         asc = attr_find( e->e_attrs, ad_structuralObjectClass );
110         if ( asc == NULL ) {
111 #ifdef NEW_LOGGING
112                 LDAP_LOG( OPERATION, INFO, 
113                         "entry_schema_check: No structuralObjectClass for entry (%s)\n", 
114                         e->e_dn, 0, 0 );
115 #else
116                 Debug( LDAP_DEBUG_ANY,
117                         "No structuralObjectClass for entry (%s)\n",
118                     e->e_dn, 0, 0 );
119 #endif
120
121                 *text = "no structuralObjectClass operational attribute";
122                 return LDAP_OTHER;
123         }
124
125         assert( asc->a_vals != NULL );
126         assert( asc->a_vals[0].bv_val != NULL );
127         assert( asc->a_vals[1].bv_val == NULL );
128
129         sc = oc_bvfind( &asc->a_vals[0] );
130         if( sc == NULL ) {
131                 snprintf( textbuf, textlen, 
132                         "unrecognized structuralObjectClass '%s'",
133                         asc->a_vals[0].bv_val );
134
135 #ifdef NEW_LOGGING
136                 LDAP_LOG( OPERATION, INFO, 
137                         "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
138 #else
139                 Debug( LDAP_DEBUG_ANY,
140                         "entry_check_schema(%s): %s\n",
141                         e->e_dn, textbuf, 0 );
142 #endif
143
144                 return LDAP_OBJECT_CLASS_VIOLATION;
145         }
146
147         if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
148                 snprintf( textbuf, textlen, 
149                         "structuralObjectClass '%s' is not STRUCTURAL",
150                         asc->a_vals[0].bv_val );
151
152 #ifdef NEW_LOGGING
153                 LDAP_LOG( OPERATION, INFO, 
154                         "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
155 #else
156                 Debug( LDAP_DEBUG_ANY,
157                         "entry_check_schema(%s): %s\n",
158                         e->e_dn, textbuf, 0 );
159 #endif
160
161                 return LDAP_OTHER;
162         }
163
164         if( sc->soc_obsolete ) {
165                 snprintf( textbuf, textlen, 
166                         "structuralObjectClass '%s' is OBSOLETE",
167                         asc->a_vals[0].bv_val );
168
169 #ifdef NEW_LOGGING
170                 LDAP_LOG( OPERATION, INFO, 
171                         "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
172 #else
173                 Debug( LDAP_DEBUG_ANY,
174                         "entry_check_schema(%s): %s\n",
175                         e->e_dn, textbuf, 0 );
176 #endif
177
178                 return LDAP_OBJECT_CLASS_VIOLATION;
179         }
180
181         /* find the object class attribute */
182         aoc = attr_find( e->e_attrs, ad_objectClass );
183         if ( aoc == NULL ) {
184 #ifdef NEW_LOGGING
185                 LDAP_LOG( OPERATION, INFO, 
186                         "entry_schema_check: No objectClass for entry (%s).\n", 
187                         e->e_dn, 0, 0 );
188 #else
189                 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
190                     e->e_dn, 0, 0 );
191 #endif
192
193                 *text = "no objectClass attribute";
194                 return LDAP_OBJECT_CLASS_VIOLATION;
195         }
196
197         assert( aoc->a_vals != NULL );
198         assert( aoc->a_vals[0].bv_val != NULL );
199
200         rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
201         if( rc != LDAP_SUCCESS ) {
202                 return rc;
203         }
204
205         *text = textbuf;
206
207         if ( oc == NULL ) {
208                 snprintf( textbuf, textlen, 
209                         "unrecognized objectClass '%s'",
210                         aoc->a_vals[0].bv_val );
211                 return LDAP_OBJECT_CLASS_VIOLATION;
212
213         } else if ( sc != oc ) {
214                 snprintf( textbuf, textlen, 
215                         "structural object class modification "
216                         "from '%s' to '%s' not allowed",
217                         asc->a_vals[0].bv_val, nsc.bv_val );
218                 return LDAP_NO_OBJECT_CLASS_MODS;
219         }
220
221         {       /* naming check */
222                 LDAPRDN         *rdn = NULL;
223                 const char      *p = NULL;
224                 ber_len_t       cnt;
225
226                 /*
227                  * Get attribute type(s) and attribute value(s) of our RDN
228                  */
229                 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
230                         LDAP_DN_FORMAT_LDAP ) )
231                 {
232                         *text = "unrecongized attribute type(s) in RDN";
233                         rc = LDAP_INVALID_DN_SYNTAX;
234                         goto rdn_check_done;
235                 }
236
237                 /* Check that each AVA of the RDN is present in the entry */
238                 /* FIXME: Should also check that each AVA lists a distinct type */
239                 for ( cnt = 0; rdn[0][cnt]; cnt++ ) {
240                         LDAPAVA *ava = rdn[0][cnt];
241                         AttributeDescription *desc = NULL;
242                         Attribute *attr;
243                         const char *errtext;
244
245                         rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
246                         if ( rc != LDAP_SUCCESS ) {
247                                 snprintf( textbuf, textlen, "%s (in RDN)", errtext );
248                                 goto rdn_check_done;
249                         }
250
251                         /* find the naming attribute */
252                         attr = attr_find( e->e_attrs, desc );
253                         if ( attr == NULL ) {
254                                 snprintf( textbuf, textlen, 
255                                         "naming attribute '%s' is not present in entry",
256                                         ava->la_attr.bv_val );
257                                 rc = LDAP_NO_SUCH_ATTRIBUTE;
258                                 goto rdn_check_done;
259                         }
260
261                         if ( value_find( desc, attr->a_vals, &ava->la_value ) != 0 ) {
262                                 snprintf( textbuf, textlen, 
263                                         "value of naming attribute '%s' is not present in entry",
264                                         ava->la_attr.bv_val );
265                                 rc = LDAP_NO_SUCH_ATTRIBUTE;
266                                 goto rdn_check_done;
267                         }
268                 }
269
270 rdn_check_done:;
271                 ldap_rdnfree( rdn );
272                 if ( rc != LDAP_SUCCESS ) {
273                         return rc;
274                 }
275         }
276
277 #ifdef SLAP_EXTENDED_SCHEMA
278         /* find the content rule for the structural class */
279         cr = cr_find( sc->soc_oid );
280
281         /* the cr must be same as the structural class */
282         assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
283
284         /* check that the entry has required attrs of the content rule */
285         if( cr ) {
286                 if( cr->scr_obsolete ) {
287                         snprintf( textbuf, textlen, 
288                                 "content rule '%s' is obsolete",
289                                 ldap_contentrule2name( &cr->scr_crule ));
290
291 #ifdef NEW_LOGGING
292                         LDAP_LOG( OPERATION, INFO, 
293                                 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
294 #else
295                         Debug( LDAP_DEBUG_ANY,
296                                 "Entry (%s): %s\n",
297                                 e->e_dn, textbuf, 0 );
298 #endif
299
300                         return LDAP_OBJECT_CLASS_VIOLATION;
301                 }
302
303                 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
304                         at = cr->scr_required[i];
305
306                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
307                                 if( a->a_desc->ad_type == at ) {
308                                         break;
309                                 }
310                         }
311
312                         /* not there => schema violation */
313                         if ( a == NULL ) {
314                                 snprintf( textbuf, textlen, 
315                                         "content rule '%s' requires attribute '%s'",
316                                         ldap_contentrule2name( &cr->scr_crule ),
317                                         at->sat_cname.bv_val );
318
319 #ifdef NEW_LOGGING
320                                 LDAP_LOG( OPERATION, INFO, 
321                                         "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
322 #else
323                                 Debug( LDAP_DEBUG_ANY,
324                                         "Entry (%s): %s\n",
325                                         e->e_dn, textbuf, 0 );
326 #endif
327
328                                 return LDAP_OBJECT_CLASS_VIOLATION;
329                         }
330                 }
331
332                 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
333                         at = cr->scr_precluded[i];
334
335                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
336                                 if( a->a_desc->ad_type == at ) {
337                                         break;
338                                 }
339                         }
340
341                         /* there => schema violation */
342                         if ( a != NULL ) {
343                                 snprintf( textbuf, textlen, 
344                                         "content rule '%s' precluded attribute '%s'",
345                                         ldap_contentrule2name( &cr->scr_crule ),
346                                         at->sat_cname.bv_val );
347
348 #ifdef NEW_LOGGING
349                                 LDAP_LOG( OPERATION, INFO, 
350                                         "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
351 #else
352                                 Debug( LDAP_DEBUG_ANY,
353                                         "Entry (%s): %s\n",
354                                         e->e_dn, textbuf, 0 );
355 #endif
356
357                                 return LDAP_OBJECT_CLASS_VIOLATION;
358                         }
359                 }
360         }
361 #endif /* SLAP_EXTENDED_SCHEMA */
362
363         /* check that the entry has required attrs for each oc */
364         for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
365                 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
366                         snprintf( textbuf, textlen, 
367                                 "unrecognized objectClass '%s'",
368                                 aoc->a_vals[i].bv_val );
369
370 #ifdef NEW_LOGGING
371                         LDAP_LOG( OPERATION, INFO, 
372                                 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
373 #else
374                         Debug( LDAP_DEBUG_ANY,
375                                 "entry_check_schema(%s): %s\n",
376                                 e->e_dn, textbuf, 0 );
377 #endif
378
379                         return LDAP_OBJECT_CLASS_VIOLATION;
380                 }
381
382                 if ( oc->soc_obsolete ) {
383                         /* disallow obsolete classes */
384                         snprintf( textbuf, textlen, 
385                                 "objectClass '%s' is OBSOLETE",
386                                 aoc->a_vals[i].bv_val );
387
388 #ifdef NEW_LOGGING
389                         LDAP_LOG( OPERATION, INFO, 
390                                 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
391 #else
392                         Debug( LDAP_DEBUG_ANY,
393                                 "entry_check_schema(%s): %s\n",
394                                 e->e_dn, textbuf, 0 );
395 #endif
396
397                         return LDAP_OBJECT_CLASS_VIOLATION;
398                 }
399
400                 if ( oc->soc_check ) {
401                         int rc = (oc->soc_check)( be, e, oc,
402                                 text, textbuf, textlen );
403                         if( rc != LDAP_SUCCESS ) {
404                                 return rc;
405                         }
406                 }
407
408                 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
409                         /* object class is abstract */
410                         if ( oc != slap_schema.si_oc_top &&
411                                 !is_object_subclass( oc, sc ))
412                         {
413                                 int j;
414                                 ObjectClass *xc = NULL;
415                                 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
416                                         if( i != j ) {
417                                                 xc = oc_bvfind( &aoc->a_vals[i] );
418                                                 if( xc == NULL ) {
419                                                         snprintf( textbuf, textlen, 
420                                                                 "unrecognized objectClass '%s'",
421                                                                 aoc->a_vals[i].bv_val );
422
423 #ifdef NEW_LOGGING
424                                                         LDAP_LOG( OPERATION, INFO, 
425                                                                 "entry_schema_check: dn (%s), %s\n",
426                                                                 e->e_dn, textbuf, 0 );
427 #else
428                                                         Debug( LDAP_DEBUG_ANY,
429                                                                 "entry_check_schema(%s): %s\n",
430                                                                 e->e_dn, textbuf, 0 );
431 #endif
432
433                                                         return LDAP_OBJECT_CLASS_VIOLATION;
434                                                 }
435
436                                                 /* since we previous check against the
437                                                  * structural object of this entry, the
438                                                  * abstract class must be a (direct or indirect)
439                                                  * superclass of one of the auxiliary classes of
440                                                  * the entry.
441                                                  */
442                                                 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
443                                                         is_object_subclass( oc, xc ) )
444                                                 {
445                                                         xc = NULL;
446                                                         break;
447                                                 }
448                                         }
449                                 }
450
451                                 if( xc == NULL ) {
452                                         snprintf( textbuf, textlen, "instanstantiation of "
453                                                 "abstract objectClass '%s' not allowed",
454                                                 aoc->a_vals[i].bv_val );
455
456 #ifdef NEW_LOGGING
457                                         LDAP_LOG( OPERATION, INFO, 
458                                                 "entry_schema_check: dn (%s), %s\n", 
459                                                 e->e_dn, textbuf, 0 );
460 #else
461                                         Debug( LDAP_DEBUG_ANY,
462                                                 "entry_check_schema(%s): %s\n",
463                                                 e->e_dn, textbuf, 0 );
464 #endif
465
466                                         return LDAP_OBJECT_CLASS_VIOLATION;
467                                 }
468                         }
469
470                 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
471                         char *s;
472
473 #ifdef SLAP_EXTENDED_SCHEMA
474                         if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
475                                 int k=0;
476                                 if( cr ) {
477                                         if( cr->scr_auxiliaries ) {
478                                                 for( ; cr->scr_auxiliaries[k]; k++ ) {
479                                                         if( cr->scr_auxiliaries[k] == oc ) {
480                                                                 k=-1;
481                                                                 break;
482                                                         }
483                                                 }
484                                         }
485                                 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
486                                         k=-1;
487                                 }
488
489                                 if( k == -1 ) {
490                                         snprintf( textbuf, textlen, 
491                                                 "content rule '%s' does not allow class '%s'",
492                                                 ldap_contentrule2name( &cr->scr_crule ),
493                                                 oc->soc_cname.bv_val );
494
495 #ifdef NEW_LOGGING
496                                         LDAP_LOG( OPERATION, INFO, 
497                                                 "entry_schema_check: dn=\"%s\" %s",
498                                                 e->e_dn, textbuf, 0 );
499 #else
500                                         Debug( LDAP_DEBUG_ANY,
501                                                 "Entry (%s): %s\n",
502                                                 e->e_dn, textbuf, 0 );
503 #endif
504
505                                         return LDAP_OBJECT_CLASS_VIOLATION;
506                                 }
507                         }
508 #endif /* SLAP_EXTENDED_SCHEMA */
509
510                         s = oc_check_required( e, oc, &aoc->a_vals[i] );
511                         if (s != NULL) {
512                                 snprintf( textbuf, textlen, 
513                                         "object class '%s' requires attribute '%s'",
514                                         aoc->a_vals[i].bv_val, s );
515
516 #ifdef NEW_LOGGING
517                                 LDAP_LOG( OPERATION, INFO, 
518                                         "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
519 #else
520                                 Debug( LDAP_DEBUG_ANY,
521                                         "Entry (%s): %s\n",
522                                         e->e_dn, textbuf, 0 );
523 #endif
524
525                                 return LDAP_OBJECT_CLASS_VIOLATION;
526                         }
527
528                         if( oc == slap_schema.si_oc_extensibleObject ) {
529                                 extensible=1;
530                         }
531                 }
532         }
533
534         if( extensible ) {
535                 return LDAP_SUCCESS;
536         }
537
538         /* check that each attr in the entry is allowed by some oc */
539         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
540                 int ret;
541
542 #ifdef SLAP_EXTENDED_SCHEMA
543                 ret = LDAP_OBJECT_CLASS_VIOLATION;
544
545                 if( cr && cr->scr_required ) {
546                         for( i=0; cr->scr_required[i]; i++ ) {
547                                 if( cr->scr_required[i] == a->a_desc->ad_type ) {
548                                         ret = LDAP_SUCCESS;
549                                         break;
550                                 }
551                         }
552                 }
553
554                 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
555                         for( i=0; cr->scr_allowed[i]; i++ ) {
556                                 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
557                                         ret = LDAP_SUCCESS;
558                                         break;
559                                 }
560                         }
561                 }
562
563                 if( ret != LDAP_SUCCESS ) 
564 #endif /* SLAP_EXTENDED_SCHEMA */
565                 {
566                         ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
567                 }
568
569                 if ( ret != LDAP_SUCCESS ) {
570                         char *type = a->a_desc->ad_cname.bv_val;
571
572                         snprintf( textbuf, textlen, 
573                                 "attribute '%s' not allowed",
574                                 type );
575
576 #ifdef NEW_LOGGING
577                         LDAP_LOG( OPERATION, INFO, 
578                                 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0);
579 #else
580                         Debug( LDAP_DEBUG_ANY,
581                             "Entry (%s), %s\n",
582                             e->e_dn, textbuf, 0 );
583 #endif
584
585                         return ret;
586                 }
587         }
588
589         return LDAP_SUCCESS;
590 }
591
592 static char *
593 oc_check_required(
594         Entry *e,
595         ObjectClass *oc,
596         struct berval *ocname )
597 {
598         AttributeType   *at;
599         int             i;
600         Attribute       *a;
601
602 #ifdef NEW_LOGGING
603         LDAP_LOG( OPERATION, ENTRY, 
604                 "oc_check_required: dn (%s), objectClass \"%s\"\n", 
605                 e->e_dn, ocname->bv_val, 0 );
606 #else
607         Debug( LDAP_DEBUG_TRACE,
608                 "oc_check_required entry (%s), objectClass \"%s\"\n",
609                 e->e_dn, ocname->bv_val, 0 );
610 #endif
611
612
613         /* check for empty oc_required */
614         if(oc->soc_required == NULL) {
615                 return NULL;
616         }
617
618         /* for each required attribute */
619         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
620                 at = oc->soc_required[i];
621                 /* see if it's in the entry */
622                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
623                         if( a->a_desc->ad_type == at ) {
624                                 break;
625                         }
626                 }
627                 /* not there => schema violation */
628                 if ( a == NULL ) {
629                         return at->sat_cname.bv_val;
630                 }
631         }
632
633         return( NULL );
634 }
635
636 int oc_check_allowed(
637         AttributeType *at,
638         BerVarray ocl,
639         ObjectClass *sc )
640 {
641         int             i, j;
642
643 #ifdef NEW_LOGGING
644         LDAP_LOG( OPERATION, ENTRY, 
645                 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val, 0, 0 );
646 #else
647         Debug( LDAP_DEBUG_TRACE,
648                 "oc_check_allowed type \"%s\"\n",
649                 at->sat_cname.bv_val, 0, 0 );
650 #endif
651
652         /* always allow objectClass attribute */
653         if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
654                 return LDAP_SUCCESS;
655         }
656
657         /*
658          * All operational attributions are allowed by schema rules.
659          */
660         if( is_at_operational(at) ) {
661                 return LDAP_SUCCESS;
662         }
663
664         /* check to see if its allowed by the structuralObjectClass */
665         if( sc ) {
666                 /* does it require the type? */
667                 for ( j = 0; sc->soc_required != NULL && 
668                         sc->soc_required[j] != NULL; j++ )
669                 {
670                         if( at == sc->soc_required[j] ) {
671                                 return LDAP_SUCCESS;
672                         }
673                 }
674
675                 /* does it allow the type? */
676                 for ( j = 0; sc->soc_allowed != NULL && 
677                         sc->soc_allowed[j] != NULL; j++ )
678                 {
679                         if( at == sc->soc_allowed[j] ) {
680                                 return LDAP_SUCCESS;
681                         }
682                 }
683         }
684
685         /* check that the type appears as req or opt in at least one oc */
686         for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
687                 /* if we know about the oc */
688                 ObjectClass     *oc = oc_bvfind( &ocl[i] );
689                 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
690                         ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
691                 {
692                         /* does it require the type? */
693                         for ( j = 0; oc->soc_required != NULL && 
694                                 oc->soc_required[j] != NULL; j++ )
695                         {
696                                 if( at == oc->soc_required[j] ) {
697                                         return LDAP_SUCCESS;
698                                 }
699                         }
700                         /* does it allow the type? */
701                         for ( j = 0; oc->soc_allowed != NULL && 
702                                 oc->soc_allowed[j] != NULL; j++ )
703                         {
704                                 if( at == oc->soc_allowed[j] ) {
705                                         return LDAP_SUCCESS;
706                                 }
707                         }
708                 }
709         }
710
711         /* not allowed by any oc */
712         return LDAP_OBJECT_CLASS_VIOLATION;
713 }
714
715 /*
716  * Determine the structural object class from a set of OIDs
717  */
718 int structural_class(
719         BerVarray ocs,
720         struct berval *scbv,
721         ObjectClass **scp,
722         const char **text,
723         char *textbuf, size_t textlen )
724 {
725         int i;
726         ObjectClass *oc;
727         ObjectClass *sc = NULL;
728         int scn = -1;
729
730         *text = "structural_class: internal error";
731         scbv->bv_len = 0;
732
733         for( i=0; ocs[i].bv_val; i++ ) {
734                 oc = oc_bvfind( &ocs[i] );
735
736                 if( oc == NULL ) {
737                         snprintf( textbuf, textlen,
738                                 "unrecognized objectClass '%s'",
739                                 ocs[i].bv_val );
740                         *text = textbuf;
741                         return LDAP_OBJECT_CLASS_VIOLATION;
742                 }
743
744                 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
745                         if( sc == NULL || is_object_subclass( sc, oc ) ) {
746                                 sc = oc;
747                                 scn = i;
748
749                         } else if ( !is_object_subclass( oc, sc ) ) {
750                                 int j;
751                                 ObjectClass *xc = NULL;
752
753                                 /* find common superior */
754                                 for( j=i+1; ocs[j].bv_val; j++ ) {
755                                         xc = oc_bvfind( &ocs[j] );
756
757                                         if( xc == NULL ) {
758                                                 snprintf( textbuf, textlen,
759                                                         "unrecognized objectClass '%s'",
760                                                         ocs[i].bv_val );
761                                                 *text = textbuf;
762                                                 return LDAP_OBJECT_CLASS_VIOLATION;
763                                         }
764
765                                         if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
766                                                 xc = NULL;
767                                                 continue;
768                                         }
769
770                                         if( is_object_subclass( sc, xc ) &&
771                                                 is_object_subclass( oc, xc ) )
772                                         {
773                                                 /* found common subclass */
774                                                 break;
775                                         }
776
777                                         xc = NULL;
778                                 }
779
780                                 if( xc == NULL ) {
781                                         /* no common subclass */
782                                         snprintf( textbuf, textlen,
783                                                 "invalid structural object class chain (%s/%s)",
784                                                 ocs[scn].bv_val, ocs[i].bv_val );
785                                         *text = textbuf;
786                                         return LDAP_OBJECT_CLASS_VIOLATION;
787                                 }
788                         }
789                 }
790         }
791
792         if( scp ) {
793                 *scp = sc;
794         }
795
796         if( sc == NULL ) {
797                 *text = "no structural object class provided";
798                 return LDAP_OBJECT_CLASS_VIOLATION;
799         }
800
801         if( scn < 0 ) {
802                 *text = "invalid structural object class";
803                 return LDAP_OBJECT_CLASS_VIOLATION;
804         }
805
806         *scbv = ocs[scn];
807
808         if( scbv->bv_len == 0 ) {
809                 *text = "invalid structural object class";
810                 return LDAP_OBJECT_CLASS_VIOLATION;
811         }
812
813         return LDAP_SUCCESS;
814 }
815
816 /*
817  * Return structural object class from list of modifications
818  */
819 int mods_structural_class(
820         Modifications *mods,
821         struct berval *sc,
822         const char **text,
823         char *textbuf, size_t textlen )
824 {
825         Modifications *ocmod = NULL;
826
827         for( ; mods != NULL; mods = mods->sml_next ) {
828                 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
829                         if( ocmod != NULL ) {
830                                 *text = "entry has multiple objectClass attributes";
831                                 return LDAP_OBJECT_CLASS_VIOLATION;
832                         }
833                         ocmod = mods;
834                 }
835         }
836
837         if( ocmod == NULL ) {
838                 *text = "entry has no objectClass attribute";
839                 return LDAP_OBJECT_CLASS_VIOLATION;
840         }
841
842         if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
843                 *text = "objectClass attribute has no values";
844                 return LDAP_OBJECT_CLASS_VIOLATION;
845         }
846
847         return structural_class( ocmod->sml_bvalues, sc, NULL,
848                 text, textbuf, textlen );
849 }