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