]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
31c16e2f2d58747c6c8fe4f70e183de4df065151
[openldap] / servers / slapd / schema_check.c
1 /* schema_check.c - routines to enforce schema definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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 from '%s' to '%s' not allowed",
216                         asc->a_vals[0].bv_val, nsc.bv_val );
217                 return LDAP_NO_OBJECT_CLASS_MODS;
218         }
219
220 #ifdef SLAP_EXTENDED_SCHEMA
221         /* find the content rule for the structural class */
222         cr = cr_find( sc->soc_oid );
223
224         /* the cr must be same as the structural class */
225         assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
226
227         /* check that the entry has required attrs of the content rule */
228         if( cr ) {
229                 if( cr->scr_obsolete ) {
230                         snprintf( textbuf, textlen, 
231                                 "content rule '%s' is obsolete",
232                                 ldap_contentrule2name( &cr->scr_crule ));
233
234 #ifdef NEW_LOGGING
235                         LDAP_LOG( OPERATION, INFO, 
236                                 "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
237 #else
238                         Debug( LDAP_DEBUG_ANY,
239                                 "Entry (%s): %s\n",
240                                 e->e_dn, textbuf, 0 );
241 #endif
242
243                         return LDAP_OBJECT_CLASS_VIOLATION;
244                 }
245
246                 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
247                         at = cr->scr_required[i];
248
249                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
250                                 if( a->a_desc->ad_type == at ) {
251                                         break;
252                                 }
253                         }
254
255                         /* not there => schema violation */
256                         if ( a == NULL ) {
257                                 snprintf( textbuf, textlen, 
258                                         "content rule '%s' requires attribute '%s'",
259                                         ldap_contentrule2name( &cr->scr_crule ),
260                                         at->sat_cname.bv_val );
261
262 #ifdef NEW_LOGGING
263                                 LDAP_LOG( OPERATION, INFO, 
264                                         "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
265 #else
266                                 Debug( LDAP_DEBUG_ANY,
267                                         "Entry (%s): %s\n",
268                                         e->e_dn, textbuf, 0 );
269 #endif
270
271                                 return LDAP_OBJECT_CLASS_VIOLATION;
272                         }
273                 }
274
275                 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
276                         at = cr->scr_precluded[i];
277
278                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
279                                 if( a->a_desc->ad_type == at ) {
280                                         break;
281                                 }
282                         }
283
284                         /* there => schema violation */
285                         if ( a != NULL ) {
286                                 snprintf( textbuf, textlen, 
287                                         "content rule '%s' precluded attribute '%s'",
288                                         ldap_contentrule2name( &cr->scr_crule ),
289                                         at->sat_cname.bv_val );
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         }
304 #endif /* SLAP_EXTENDED_SCHEMA */
305
306         /* check that the entry has required attrs for each oc */
307         for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
308                 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
309                         snprintf( textbuf, textlen, 
310                                 "unrecognized objectClass '%s'",
311                                 aoc->a_vals[i].bv_val );
312
313 #ifdef NEW_LOGGING
314                         LDAP_LOG( OPERATION, INFO, 
315                                 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
316 #else
317                         Debug( LDAP_DEBUG_ANY,
318                                 "entry_check_schema(%s): %s\n",
319                                 e->e_dn, textbuf, 0 );
320 #endif
321
322                         return LDAP_OBJECT_CLASS_VIOLATION;
323                 }
324
325                 if ( oc->soc_obsolete ) {
326                         /* disallow obsolete classes */
327                         snprintf( textbuf, textlen, 
328                                 "objectClass '%s' is OBSOLETE",
329                                 aoc->a_vals[i].bv_val );
330
331 #ifdef NEW_LOGGING
332                         LDAP_LOG( OPERATION, INFO, 
333                                 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
334 #else
335                         Debug( LDAP_DEBUG_ANY,
336                                 "entry_check_schema(%s): %s\n",
337                                 e->e_dn, textbuf, 0 );
338 #endif
339
340                         return LDAP_OBJECT_CLASS_VIOLATION;
341                 }
342
343                 if ( oc->soc_check ) {
344                         int rc = (oc->soc_check)( be, e, oc,
345                                 text, textbuf, textlen );
346                         if( rc != LDAP_SUCCESS ) {
347                                 return rc;
348                         }
349                 }
350
351                 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
352                         /* object class is abstract */
353                         if ( oc != slap_schema.si_oc_top &&
354                                 !is_object_subclass( oc, sc ))
355                         {
356                                 int j;
357                                 ObjectClass *xc = NULL;
358                                 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
359                                         if( i != j ) {
360                                                 xc = oc_bvfind( &aoc->a_vals[i] );
361                                                 if( xc == NULL ) {
362                                                         snprintf( textbuf, textlen, 
363                                                                 "unrecognized objectClass '%s'",
364                                                                 aoc->a_vals[i].bv_val );
365
366 #ifdef NEW_LOGGING
367                                                         LDAP_LOG( OPERATION, INFO, 
368                                                                 "entry_schema_check: dn (%s), %s\n",
369                                                                 e->e_dn, textbuf, 0 );
370 #else
371                                                         Debug( LDAP_DEBUG_ANY,
372                                                                 "entry_check_schema(%s): %s\n",
373                                                                 e->e_dn, textbuf, 0 );
374 #endif
375
376                                                         return LDAP_OBJECT_CLASS_VIOLATION;
377                                                 }
378
379                                                 /* since we previous check against the
380                                                  * structural object of this entry, the
381                                                  * abstract class must be a (direct or indirect)
382                                                  * superclass of one of the auxiliary classes of
383                                                  * the entry.
384                                                  */
385                                                 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
386                                                         is_object_subclass( oc, xc ) )
387                                                 {
388                                                         xc = NULL;
389                                                         break;
390                                                 }
391                                         }
392                                 }
393
394                                 if( xc == NULL ) {
395                                         snprintf( textbuf, textlen, "instanstantiation of "
396                                                 "abstract objectClass '%s' not allowed",
397                                                 aoc->a_vals[i].bv_val );
398
399 #ifdef NEW_LOGGING
400                                         LDAP_LOG( OPERATION, INFO, 
401                                                 "entry_schema_check: dn (%s), %s\n", 
402                                                 e->e_dn, textbuf, 0 );
403 #else
404                                         Debug( LDAP_DEBUG_ANY,
405                                                 "entry_check_schema(%s): %s\n",
406                                                 e->e_dn, textbuf, 0 );
407 #endif
408
409                                         return LDAP_OBJECT_CLASS_VIOLATION;
410                                 }
411                         }
412
413                 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
414                         char *s;
415
416 #ifdef SLAP_EXTENDED_SCHEMA
417                         if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
418                                 int k=0;
419                                 if( cr ) {
420                                         if( cr->scr_auxiliaries ) {
421                                                 for( ; cr->scr_auxiliaries[k]; k++ ) {
422                                                         if( cr->scr_auxiliaries[k] == oc ) {
423                                                                 k=-1;
424                                                                 break;
425                                                         }
426                                                 }
427                                         }
428                                 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
429                                         k=-1;
430                                 }
431
432                                 if( k == -1 ) {
433                                         snprintf( textbuf, textlen, 
434                                                 "content rule '%s' does not allow class '%s'",
435                                                 ldap_contentrule2name( &cr->scr_crule ),
436                                                 oc->soc_cname.bv_val );
437
438 #ifdef NEW_LOGGING
439                                         LDAP_LOG( OPERATION, INFO, 
440                                                 "entry_schema_check: dn=\"%s\" %s",
441                                                 e->e_dn, textbuf, 0 );
442 #else
443                                         Debug( LDAP_DEBUG_ANY,
444                                                 "Entry (%s): %s\n",
445                                                 e->e_dn, textbuf, 0 );
446 #endif
447
448                                         return LDAP_OBJECT_CLASS_VIOLATION;
449                                 }
450                         }
451 #endif /* SLAP_EXTENDED_SCHEMA */
452
453                         s = oc_check_required( e, oc, &aoc->a_vals[i] );
454                         if (s != NULL) {
455                                 snprintf( textbuf, textlen, 
456                                         "object class '%s' requires attribute '%s'",
457                                         aoc->a_vals[i].bv_val, s );
458
459 #ifdef NEW_LOGGING
460                                 LDAP_LOG( OPERATION, INFO, 
461                                         "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
462 #else
463                                 Debug( LDAP_DEBUG_ANY,
464                                         "Entry (%s): %s\n",
465                                         e->e_dn, textbuf, 0 );
466 #endif
467
468                                 return LDAP_OBJECT_CLASS_VIOLATION;
469                         }
470
471                         if( oc == slap_schema.si_oc_extensibleObject ) {
472                                 extensible=1;
473                         }
474                 }
475         }
476
477         if( extensible ) {
478                 return LDAP_SUCCESS;
479         }
480
481         /* check that each attr in the entry is allowed by some oc */
482         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
483                 int ret;
484
485 #ifdef SLAP_EXTENDED_SCHEMA
486                 ret = LDAP_OBJECT_CLASS_VIOLATION;
487
488                 if( cr && cr->scr_required ) {
489                         for( i=0; cr->scr_required[i]; i++ ) {
490                                 if( cr->scr_required[i] == a->a_desc->ad_type ) {
491                                         ret = LDAP_SUCCESS;
492                                         break;
493                                 }
494                         }
495                 }
496
497                 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
498                         for( i=0; cr->scr_allowed[i]; i++ ) {
499                                 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
500                                         ret = LDAP_SUCCESS;
501                                         break;
502                                 }
503                         }
504                 }
505
506                 if( ret != LDAP_SUCCESS ) 
507 #endif /* SLAP_EXTENDED_SCHEMA */
508                 {
509                         ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
510                 }
511
512                 if ( ret != LDAP_SUCCESS ) {
513                         char *type = a->a_desc->ad_cname.bv_val;
514
515                         snprintf( textbuf, textlen, 
516                                 "attribute '%s' not allowed",
517                                 type );
518
519 #ifdef NEW_LOGGING
520                         LDAP_LOG( OPERATION, INFO, 
521                                 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0);
522 #else
523                         Debug( LDAP_DEBUG_ANY,
524                             "Entry (%s), %s\n",
525                             e->e_dn, textbuf, 0 );
526 #endif
527
528                         return ret;
529                 }
530         }
531
532         return LDAP_SUCCESS;
533 }
534
535 static char *
536 oc_check_required(
537         Entry *e,
538         ObjectClass *oc,
539         struct berval *ocname )
540 {
541         AttributeType   *at;
542         int             i;
543         Attribute       *a;
544
545 #ifdef NEW_LOGGING
546         LDAP_LOG( OPERATION, ENTRY, 
547                 "oc_check_required: dn (%s), objectClass \"%s\"\n", 
548                 e->e_dn, ocname->bv_val, 0 );
549 #else
550         Debug( LDAP_DEBUG_TRACE,
551                 "oc_check_required entry (%s), objectClass \"%s\"\n",
552                 e->e_dn, ocname->bv_val, 0 );
553 #endif
554
555
556         /* check for empty oc_required */
557         if(oc->soc_required == NULL) {
558                 return NULL;
559         }
560
561         /* for each required attribute */
562         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
563                 at = oc->soc_required[i];
564                 /* see if it's in the entry */
565                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
566                         if( a->a_desc->ad_type == at ) {
567                                 break;
568                         }
569                 }
570                 /* not there => schema violation */
571                 if ( a == NULL ) {
572                         return at->sat_cname.bv_val;
573                 }
574         }
575
576         return( NULL );
577 }
578
579 int oc_check_allowed(
580         AttributeType *at,
581         BerVarray ocl,
582         ObjectClass *sc )
583 {
584         int             i, j;
585
586 #ifdef NEW_LOGGING
587         LDAP_LOG( OPERATION, ENTRY, 
588                 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val, 0, 0 );
589 #else
590         Debug( LDAP_DEBUG_TRACE,
591                 "oc_check_allowed type \"%s\"\n",
592                 at->sat_cname.bv_val, 0, 0 );
593 #endif
594
595         /* always allow objectClass attribute */
596         if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
597                 return LDAP_SUCCESS;
598         }
599
600         /*
601          * All operational attributions are allowed by schema rules.
602          */
603         if( is_at_operational(at) ) {
604                 return LDAP_SUCCESS;
605         }
606
607         /* check to see if its allowed by the structuralObjectClass */
608         if( sc ) {
609                 /* does it require the type? */
610                 for ( j = 0; sc->soc_required != NULL && 
611                         sc->soc_required[j] != NULL; j++ )
612                 {
613                         if( at == sc->soc_required[j] ) {
614                                 return LDAP_SUCCESS;
615                         }
616                 }
617
618                 /* does it allow the type? */
619                 for ( j = 0; sc->soc_allowed != NULL && 
620                         sc->soc_allowed[j] != NULL; j++ )
621                 {
622                         if( at == sc->soc_allowed[j] ) {
623                                 return LDAP_SUCCESS;
624                         }
625                 }
626         }
627
628         /* check that the type appears as req or opt in at least one oc */
629         for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
630                 /* if we know about the oc */
631                 ObjectClass     *oc = oc_bvfind( &ocl[i] );
632                 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
633                         ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
634                 {
635                         /* does it require the type? */
636                         for ( j = 0; oc->soc_required != NULL && 
637                                 oc->soc_required[j] != NULL; j++ )
638                         {
639                                 if( at == oc->soc_required[j] ) {
640                                         return LDAP_SUCCESS;
641                                 }
642                         }
643                         /* does it allow the type? */
644                         for ( j = 0; oc->soc_allowed != NULL && 
645                                 oc->soc_allowed[j] != NULL; j++ )
646                         {
647                                 if( at == oc->soc_allowed[j] ) {
648                                         return LDAP_SUCCESS;
649                                 }
650                         }
651                 }
652         }
653
654         /* not allowed by any oc */
655         return LDAP_OBJECT_CLASS_VIOLATION;
656 }
657
658 /*
659  * Determine the structural object class from a set of OIDs
660  */
661 int structural_class(
662         BerVarray ocs,
663         struct berval *scbv,
664         ObjectClass **scp,
665         const char **text,
666         char *textbuf, size_t textlen )
667 {
668         int i;
669         ObjectClass *oc;
670         ObjectClass *sc = NULL;
671         int scn = -1;
672
673         *text = "structural_class: internal error";
674         scbv->bv_len = 0;
675
676         for( i=0; ocs[i].bv_val; i++ ) {
677                 oc = oc_bvfind( &ocs[i] );
678
679                 if( oc == NULL ) {
680                         snprintf( textbuf, textlen,
681                                 "unrecognized objectClass '%s'",
682                                 ocs[i].bv_val );
683                         *text = textbuf;
684                         return LDAP_OBJECT_CLASS_VIOLATION;
685                 }
686
687                 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
688                         if( sc == NULL || is_object_subclass( sc, oc ) ) {
689                                 sc = oc;
690                                 scn = i;
691
692                         } else if ( !is_object_subclass( oc, sc ) ) {
693                                 int j;
694                                 ObjectClass *xc = NULL;
695
696                                 /* find common superior */
697                                 for( j=i+1; ocs[j].bv_val; j++ ) {
698                                         xc = oc_bvfind( &ocs[j] );
699
700                                         if( xc == NULL ) {
701                                                 snprintf( textbuf, textlen,
702                                                         "unrecognized objectClass '%s'",
703                                                         ocs[i].bv_val );
704                                                 *text = textbuf;
705                                                 return LDAP_OBJECT_CLASS_VIOLATION;
706                                         }
707
708                                         if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
709                                                 xc = NULL;
710                                                 continue;
711                                         }
712
713                                         if( is_object_subclass( sc, xc ) &&
714                                                 is_object_subclass( oc, xc ) )
715                                         {
716                                                 /* found common subclass */
717                                                 break;
718                                         }
719
720                                         xc = NULL;
721                                 }
722
723                                 if( xc == NULL ) {
724                                         /* no common subclass */
725                                         snprintf( textbuf, textlen,
726                                                 "invalid structural object class chain (%s/%s)",
727                                                 ocs[scn].bv_val, ocs[i].bv_val );
728                                         *text = textbuf;
729                                         return LDAP_OBJECT_CLASS_VIOLATION;
730                                 }
731                         }
732                 }
733         }
734
735         if( scp ) {
736                 *scp = sc;
737         }
738
739         if( sc == NULL ) {
740                 *text = "no structural object class provided";
741                 return LDAP_OBJECT_CLASS_VIOLATION;
742         }
743
744         if( scn < 0 ) {
745                 *text = "invalid structural object class";
746                 return LDAP_OBJECT_CLASS_VIOLATION;
747         }
748
749         *scbv = ocs[scn];
750
751         if( scbv->bv_len == 0 ) {
752                 *text = "invalid structural object class";
753                 return LDAP_OBJECT_CLASS_VIOLATION;
754         }
755
756         return LDAP_SUCCESS;
757 }
758
759 /*
760  * Return structural object class from list of modifications
761  */
762 int mods_structural_class(
763         Modifications *mods,
764         struct berval *sc,
765         const char **text,
766         char *textbuf, size_t textlen )
767 {
768         Modifications *ocmod = NULL;
769
770         for( ; mods != NULL; mods = mods->sml_next ) {
771                 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
772                         if( ocmod != NULL ) {
773                                 *text = "entry has multiple objectClass attributes";
774                                 return LDAP_OBJECT_CLASS_VIOLATION;
775                         }
776                         ocmod = mods;
777                 }
778         }
779
780         if( ocmod == NULL ) {
781                 *text = "entry has no objectClass attribute";
782                 return LDAP_OBJECT_CLASS_VIOLATION;
783         }
784
785         if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
786                 *text = "objectClass attribute has no values";
787                 return LDAP_OBJECT_CLASS_VIOLATION;
788         }
789
790         return structural_class( ocmod->sml_bvalues, sc, NULL,
791                 text, textbuf, textlen );
792 }