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