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