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