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