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