]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
Add register_certificate_map_function() native plugin API for registering
[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 #include "ldap_pvt.h"
27
28 static char * oc_check_required(
29         Entry *e,
30         ObjectClass *oc,
31         struct berval *ocname );
32
33 static int entry_naming_check(
34         Entry *e,
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         const char** text,
50         char *textbuf, size_t textlen )
51 {
52         Attribute       *a, *asc, *aoc;
53         ObjectClass *sc, *oc;
54         AttributeType *at;
55         ContentRule *cr;
56         int     rc, i;
57         struct berval nsc;
58         AttributeDescription *ad_structuralObjectClass
59                 = slap_schema.si_ad_structuralObjectClass;
60         AttributeDescription *ad_objectClass
61                 = slap_schema.si_ad_objectClass;
62         int extensible = 0;
63         int subentry = is_entry_subentry( e );
64         int collectiveSubentry = 0;
65
66         if ( SLAP_NO_SCHEMA_CHECK( be )) {
67                 return LDAP_SUCCESS;
68         }
69
70         if( subentry ) {
71                 collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
72         }
73
74         *text = textbuf;
75
76         /* misc attribute checks */
77         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
78                 const char *type = a->a_desc->ad_cname.bv_val;
79
80                 /* there should be at least one value */
81                 assert( a->a_vals );
82                 assert( a->a_vals[0].bv_val != NULL ); 
83
84                 if( a->a_desc->ad_type->sat_check ) {
85                         int rc = (a->a_desc->ad_type->sat_check)(
86                                 be, e, a, text, textbuf, textlen );
87                         if( rc != LDAP_SUCCESS ) {
88                                 return rc;
89                         }
90                 }
91
92                 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
93                         snprintf( textbuf, textlen,
94                                 "'%s' can only appear in collectiveAttributeSubentry",
95                                 type );
96                         return LDAP_OBJECT_CLASS_VIOLATION;
97                 }
98
99                 /* if single value type, check for multiple values */
100                 if( is_at_single_value( a->a_desc->ad_type ) &&
101                         a->a_vals[1].bv_val != NULL )
102                 {
103                         snprintf( textbuf, textlen, 
104                                 "attribute '%s' cannot have multiple values",
105                                 type );
106
107                         Debug( LDAP_DEBUG_ANY,
108                             "Entry (%s), %s\n",
109                             e->e_dn, textbuf, 0 );
110
111                         return LDAP_CONSTRAINT_VIOLATION;
112                 }
113         }
114
115         /* it's a REALLY bad idea to disable schema checks */
116         if( !global_schemacheck ) return LDAP_SUCCESS;
117
118         /* find the structural object class attribute */
119         asc = attr_find( e->e_attrs, ad_structuralObjectClass );
120         if ( asc == NULL ) {
121                 Debug( LDAP_DEBUG_ANY,
122                         "No structuralObjectClass for entry (%s)\n",
123                     e->e_dn, 0, 0 );
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                 Debug( LDAP_DEBUG_ANY,
140                         "entry_check_schema(%s): %s\n",
141                         e->e_dn, textbuf, 0 );
142
143                 return LDAP_OBJECT_CLASS_VIOLATION;
144         }
145
146         if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
147                 snprintf( textbuf, textlen, 
148                         "structuralObjectClass '%s' is not STRUCTURAL",
149                         asc->a_vals[0].bv_val );
150
151                 Debug( LDAP_DEBUG_ANY,
152                         "entry_check_schema(%s): %s\n",
153                         e->e_dn, textbuf, 0 );
154
155                 return LDAP_OTHER;
156         }
157
158         if( sc->soc_obsolete ) {
159                 snprintf( textbuf, textlen, 
160                         "structuralObjectClass '%s' is OBSOLETE",
161                         asc->a_vals[0].bv_val );
162
163                 Debug( LDAP_DEBUG_ANY,
164                         "entry_check_schema(%s): %s\n",
165                         e->e_dn, textbuf, 0 );
166
167                 return LDAP_OBJECT_CLASS_VIOLATION;
168         }
169
170         /* find the object class attribute */
171         aoc = attr_find( e->e_attrs, ad_objectClass );
172         if ( aoc == NULL ) {
173                 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
174                     e->e_dn, 0, 0 );
175
176                 *text = "no objectClass attribute";
177                 return LDAP_OBJECT_CLASS_VIOLATION;
178         }
179
180         assert( aoc->a_vals != NULL );
181         assert( aoc->a_vals[0].bv_val != NULL );
182
183         rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
184         if( rc != LDAP_SUCCESS ) {
185                 return rc;
186         }
187
188         *text = textbuf;
189
190         if ( oc == NULL ) {
191                 snprintf( textbuf, textlen, 
192                         "unrecognized objectClass '%s'",
193                         aoc->a_vals[0].bv_val );
194                 return LDAP_OBJECT_CLASS_VIOLATION;
195
196         } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
197                 snprintf( textbuf, textlen, 
198                         "structural object class modification "
199                         "from '%s' to '%s' not allowed",
200                         asc->a_vals[0].bv_val, nsc.bv_val );
201                 return LDAP_NO_OBJECT_CLASS_MODS;
202         } else if ( sc == slap_schema.si_oc_glue ) {
203                 sc = oc;
204         }
205
206         /* naming check */
207         if ( !is_entry_objectclass ( e, slap_schema.si_oc_glue, 0 ) ) {
208                 rc = entry_naming_check( e, text, textbuf, textlen );
209                 if( rc != LDAP_SUCCESS ) {
210                         return rc;
211                 }
212         } else {
213                 /* Glue Entry */
214         }
215
216         /* find the content rule for the structural class */
217         cr = cr_find( sc->soc_oid );
218
219         /* the cr must be same as the structural class */
220         assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
221
222         /* check that the entry has required attrs of the content rule */
223         if( cr ) {
224                 if( cr->scr_obsolete ) {
225                         snprintf( textbuf, textlen, 
226                                 "content rule '%s' is obsolete",
227                                 ldap_contentrule2name( &cr->scr_crule ));
228
229                         Debug( LDAP_DEBUG_ANY,
230                                 "Entry (%s): %s\n",
231                                 e->e_dn, textbuf, 0 );
232
233                         return LDAP_OBJECT_CLASS_VIOLATION;
234                 }
235
236                 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
237                         at = cr->scr_required[i];
238
239                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
240                                 if( a->a_desc->ad_type == at ) {
241                                         break;
242                                 }
243                         }
244
245                         /* not there => schema violation */
246                         if ( a == NULL ) {
247                                 snprintf( textbuf, textlen, 
248                                         "content rule '%s' requires attribute '%s'",
249                                         ldap_contentrule2name( &cr->scr_crule ),
250                                         at->sat_cname.bv_val );
251
252                                 Debug( LDAP_DEBUG_ANY,
253                                         "Entry (%s): %s\n",
254                                         e->e_dn, textbuf, 0 );
255
256                                 return LDAP_OBJECT_CLASS_VIOLATION;
257                         }
258                 }
259
260                 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
261                         at = cr->scr_precluded[i];
262
263                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
264                                 if( a->a_desc->ad_type == at ) {
265                                         break;
266                                 }
267                         }
268
269                         /* there => schema violation */
270                         if ( a != NULL ) {
271                                 snprintf( textbuf, textlen, 
272                                         "content rule '%s' precluded attribute '%s'",
273                                         ldap_contentrule2name( &cr->scr_crule ),
274                                         at->sat_cname.bv_val );
275
276                                 Debug( LDAP_DEBUG_ANY,
277                                         "Entry (%s): %s\n",
278                                         e->e_dn, textbuf, 0 );
279
280                                 return LDAP_OBJECT_CLASS_VIOLATION;
281                         }
282                 }
283         }
284
285         /* check that the entry has required attrs for each oc */
286         for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
287                 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
288                         snprintf( textbuf, textlen, 
289                                 "unrecognized objectClass '%s'",
290                                 aoc->a_vals[i].bv_val );
291
292                         Debug( LDAP_DEBUG_ANY,
293                                 "entry_check_schema(%s): %s\n",
294                                 e->e_dn, textbuf, 0 );
295
296                         return LDAP_OBJECT_CLASS_VIOLATION;
297                 }
298
299                 if ( oc->soc_obsolete ) {
300                         /* disallow obsolete classes */
301                         snprintf( textbuf, textlen, 
302                                 "objectClass '%s' is OBSOLETE",
303                                 aoc->a_vals[i].bv_val );
304
305                         Debug( LDAP_DEBUG_ANY,
306                                 "entry_check_schema(%s): %s\n",
307                                 e->e_dn, textbuf, 0 );
308
309                         return LDAP_OBJECT_CLASS_VIOLATION;
310                 }
311
312                 if ( oc->soc_check ) {
313                         int rc = (oc->soc_check)( be, e, oc,
314                                 text, textbuf, textlen );
315                         if( rc != LDAP_SUCCESS ) {
316                                 return rc;
317                         }
318                 }
319
320                 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
321                         /* object class is abstract */
322                         if ( oc != slap_schema.si_oc_top &&
323                                 !is_object_subclass( oc, sc ))
324                         {
325                                 int j;
326                                 ObjectClass *xc = NULL;
327                                 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
328                                         if( i != j ) {
329                                                 xc = oc_bvfind( &aoc->a_vals[i] );
330                                                 if( xc == NULL ) {
331                                                         snprintf( textbuf, textlen, 
332                                                                 "unrecognized objectClass '%s'",
333                                                                 aoc->a_vals[i].bv_val );
334
335                                                         Debug( LDAP_DEBUG_ANY,
336                                                                 "entry_check_schema(%s): %s\n",
337                                                                 e->e_dn, textbuf, 0 );
338
339                                                         return LDAP_OBJECT_CLASS_VIOLATION;
340                                                 }
341
342                                                 /* since we previous check against the
343                                                  * structural object of this entry, the
344                                                  * abstract class must be a (direct or indirect)
345                                                  * superclass of one of the auxiliary classes of
346                                                  * the entry.
347                                                  */
348                                                 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
349                                                         is_object_subclass( oc, xc ) )
350                                                 {
351                                                         xc = NULL;
352                                                         break;
353                                                 }
354                                         }
355                                 }
356
357                                 if( xc == NULL ) {
358                                         snprintf( textbuf, textlen, "instanstantiation of "
359                                                 "abstract objectClass '%s' not allowed",
360                                                 aoc->a_vals[i].bv_val );
361
362                                         Debug( LDAP_DEBUG_ANY,
363                                                 "entry_check_schema(%s): %s\n",
364                                                 e->e_dn, textbuf, 0 );
365
366                                         return LDAP_OBJECT_CLASS_VIOLATION;
367                                 }
368                         }
369
370                 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
371                         char *s;
372
373                         if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
374                                 int k;
375
376                                 if( cr ) {
377                                         int j;
378
379                                         k = -1;
380                                         if( cr->scr_auxiliaries ) {
381                                                 for( j = 0; cr->scr_auxiliaries[j]; j++ ) {
382                                                         if( cr->scr_auxiliaries[j] == oc ) {
383                                                                 k = 0;
384                                                                 break;
385                                                         }
386                                                 }
387                                         }
388                                 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
389                                         k = -1;
390                                 } else {
391                                         k = 0;  
392                                 }
393
394                                 if( k == -1 ) {
395                                         snprintf( textbuf, textlen, 
396                                                 "content rule '%s' does not allow class '%s'",
397                                                 ldap_contentrule2name( &cr->scr_crule ),
398                                                 oc->soc_cname.bv_val );
399
400                                         Debug( LDAP_DEBUG_ANY,
401                                                 "Entry (%s): %s\n",
402                                                 e->e_dn, textbuf, 0 );
403
404                                         return LDAP_OBJECT_CLASS_VIOLATION;
405                                 }
406                         }
407
408                         s = oc_check_required( e, oc, &aoc->a_vals[i] );
409                         if (s != NULL) {
410                                 snprintf( textbuf, textlen, 
411                                         "object class '%s' requires attribute '%s'",
412                                         aoc->a_vals[i].bv_val, s );
413
414                                 Debug( LDAP_DEBUG_ANY,
415                                         "Entry (%s): %s\n",
416                                         e->e_dn, textbuf, 0 );
417
418                                 return LDAP_OBJECT_CLASS_VIOLATION;
419                         }
420
421                         if( oc == slap_schema.si_oc_extensibleObject ) {
422                                 extensible=1;
423                         }
424                 }
425         }
426
427         if( extensible ) {
428                 return LDAP_SUCCESS;
429         }
430
431         /* check that each attr in the entry is allowed by some oc */
432         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
433                 int ret;
434
435                 ret = LDAP_OBJECT_CLASS_VIOLATION;
436
437                 if( cr && cr->scr_required ) {
438                         for( i=0; cr->scr_required[i]; i++ ) {
439                                 if( cr->scr_required[i] == a->a_desc->ad_type ) {
440                                         ret = LDAP_SUCCESS;
441                                         break;
442                                 }
443                         }
444                 }
445
446                 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
447                         for( i=0; cr->scr_allowed[i]; i++ ) {
448                                 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
449                                         ret = LDAP_SUCCESS;
450                                         break;
451                                 }
452                         }
453                 }
454
455                 if( ret != LDAP_SUCCESS ) 
456                 {
457                         ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
458                 }
459
460                 if ( ret != LDAP_SUCCESS ) {
461                         char *type = a->a_desc->ad_cname.bv_val;
462
463                         snprintf( textbuf, textlen, 
464                                 "attribute '%s' not allowed",
465                                 type );
466
467                         Debug( LDAP_DEBUG_ANY,
468                             "Entry (%s), %s\n",
469                             e->e_dn, textbuf, 0 );
470
471                         return ret;
472                 }
473         }
474
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         return LDAP_SUCCESS;
689 }
690
691 /*
692  * Return structural object class from list of modifications
693  */
694 int mods_structural_class(
695         Modifications *mods,
696         struct berval *sc,
697         const char **text,
698         char *textbuf, size_t textlen )
699 {
700         Modifications *ocmod = NULL;
701
702         for( ; mods != NULL; mods = mods->sml_next ) {
703                 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
704                         if( ocmod != NULL ) {
705                                 *text = "entry has multiple objectClass attributes";
706                                 return LDAP_OBJECT_CLASS_VIOLATION;
707                         }
708                         ocmod = mods;
709                 }
710         }
711
712         if( ocmod == NULL ) {
713                 *text = "entry has no objectClass attribute";
714                 return LDAP_OBJECT_CLASS_VIOLATION;
715         }
716
717         if( ocmod->sml_values == NULL || ocmod->sml_values[0].bv_val == NULL ) {
718                 *text = "objectClass attribute has no values";
719                 return LDAP_OBJECT_CLASS_VIOLATION;
720         }
721
722         return structural_class( ocmod->sml_values, sc, NULL,
723                 text, textbuf, textlen );
724 }
725
726
727 static int
728 entry_naming_check(
729         Entry *e,
730         const char** text,
731         char *textbuf, size_t textlen )
732 {
733         /* naming check */
734         LDAPRDN         rdn = NULL;
735         const char      *p = NULL;
736         ber_len_t       cnt;
737         int             rc = LDAP_SUCCESS;
738
739         /*
740          * Get attribute type(s) and attribute value(s) of our RDN
741          */
742         if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
743                 LDAP_DN_FORMAT_LDAP ) )
744         {
745                 *text = "unrecongized attribute type(s) in RDN";
746                 return LDAP_INVALID_DN_SYNTAX;
747         }
748
749         /* Check that each AVA of the RDN is present in the entry */
750         /* FIXME: Should also check that each AVA lists a distinct type */
751         for ( cnt = 0; rdn[cnt]; cnt++ ) {
752                 LDAPAVA *ava = rdn[cnt];
753                 AttributeDescription *desc = NULL;
754                 Attribute *attr;
755                 const char *errtext;
756
757                 if( ava->la_flags & LDAP_AVA_BINARY ) {
758                         snprintf( textbuf, textlen, 
759                                 "value of naming attribute '%s' in unsupported BER form",
760                                 ava->la_attr.bv_val );
761                         rc = LDAP_NAMING_VIOLATION;
762                 }
763
764                 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
765                 if ( rc != LDAP_SUCCESS ) {
766                         snprintf( textbuf, textlen, "%s (in RDN)", errtext );
767                         break;
768                 }
769
770                 if( desc->ad_type->sat_usage ) {
771                         snprintf( textbuf, textlen, 
772                                 "naming attribute '%s' is operational",
773                                 ava->la_attr.bv_val );
774                         rc = LDAP_NAMING_VIOLATION;
775                         break;
776                 }
777  
778                 if( desc->ad_type->sat_collective ) {
779                         snprintf( textbuf, textlen, 
780                                 "naming attribute '%s' is collective",
781                                 ava->la_attr.bv_val );
782                         rc = LDAP_NAMING_VIOLATION;
783                         break;
784                 }
785
786                 if( desc->ad_type->sat_obsolete ) {
787                         snprintf( textbuf, textlen, 
788                                 "naming attribute '%s' is obsolete",
789                                 ava->la_attr.bv_val );
790                         rc = LDAP_NAMING_VIOLATION;
791                         break;
792                 }
793
794                 if( !desc->ad_type->sat_equality ) {
795                         snprintf( textbuf, textlen, 
796                                 "naming attribute '%s' has no equality matching rule",
797                                 ava->la_attr.bv_val );
798                         rc = LDAP_NAMING_VIOLATION;
799                         break;
800                 }
801
802                 if( !desc->ad_type->sat_equality->smr_match ) {
803                         snprintf( textbuf, textlen, 
804                                 "naming attribute '%s' has unsupported equality matching rule",
805                                 ava->la_attr.bv_val );
806                         rc = LDAP_NAMING_VIOLATION;
807                         break;
808                 }
809
810                 /* find the naming attribute */
811                 attr = attr_find( e->e_attrs, desc );
812                 if ( attr == NULL ) {
813                         snprintf( textbuf, textlen, 
814                                 "naming attribute '%s' is not present in entry",
815                                 ava->la_attr.bv_val );
816                         rc = LDAP_NAMING_VIOLATION;
817                         break;
818                 }
819
820                 rc = value_find_ex( desc, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX|
821                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
822                         attr->a_nvals, &ava->la_value, NULL );
823
824                 if( rc != 0 ) {
825                         switch( rc ) {
826                         case LDAP_INAPPROPRIATE_MATCHING:
827                                 snprintf( textbuf, textlen, 
828                                         "inappropriate matching for naming attribute '%s'",
829                                         ava->la_attr.bv_val );
830                                 break;
831                         case LDAP_INVALID_SYNTAX:
832                                 snprintf( textbuf, textlen, 
833                                         "value of naming attribute '%s' is invalid",
834                                         ava->la_attr.bv_val );
835                                 break;
836                         case LDAP_NO_SUCH_ATTRIBUTE:
837                                 snprintf( textbuf, textlen, 
838                                         "value of naming attribute '%s' is not present in entry",
839                                         ava->la_attr.bv_val );
840                                 break;
841                         default:
842                                 snprintf( textbuf, textlen, 
843                                         "naming attribute '%s' is inappropriate",
844                                         ava->la_attr.bv_val );
845                         }
846                         rc = LDAP_NAMING_VIOLATION;
847                         break;
848                 }
849         }
850
851         ldap_rdnfree( rdn );
852         return rc;
853 }
854