]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
extensibleObject allows all
[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-2007 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                 rc = LDAP_OBJECT_CLASS_VIOLATION;
175                 goto leave;
176         }
177
178         if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
179                 snprintf( textbuf, textlen, 
180                         "structuralObjectClass '%s' is not STRUCTURAL",
181                         asc->a_vals[0].bv_val );
182
183                 Debug( LDAP_DEBUG_ANY,
184                         "entry_check_schema(%s): %s\n",
185                         e->e_dn, textbuf, 0 );
186
187                 rc = LDAP_OTHER;
188                 goto leave;
189         }
190
191 got_soc:
192         if( !manage && sc->soc_obsolete ) {
193                 snprintf( textbuf, textlen, 
194                         "structuralObjectClass '%s' is OBSOLETE",
195                         asc->a_vals[0].bv_val );
196
197                 Debug( LDAP_DEBUG_ANY,
198                         "entry_check_schema(%s): %s\n",
199                         e->e_dn, textbuf, 0 );
200
201                 rc = LDAP_OBJECT_CLASS_VIOLATION;
202                 goto leave;
203         }
204
205         *text = textbuf;
206
207         if ( oc == NULL ) {
208                 snprintf( textbuf, textlen, 
209                         "unrecognized objectClass '%s'",
210                         aoc->a_vals[0].bv_val );
211                 rc = LDAP_OBJECT_CLASS_VIOLATION;
212                 goto leave;
213
214         } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
215                 snprintf( textbuf, textlen, 
216                         "structural object class modification "
217                         "from '%s' to '%s' not allowed",
218                         asc->a_vals[0].bv_val, oc->soc_cname.bv_val );
219                 rc = LDAP_NO_OBJECT_CLASS_MODS;
220                 goto leave;
221         } else if ( sc == slap_schema.si_oc_glue ) {
222                 sc = oc;
223         }
224
225         /* naming check */
226         if ( !is_entry_glue ( e ) ) {
227                 rc = entry_naming_check( e, manage, text, textbuf, textlen );
228                 if( rc != LDAP_SUCCESS ) {
229                         goto leave;
230                 }
231         } else {
232                 /* Glue Entry */
233         }
234
235         /* find the content rule for the structural class */
236         cr = cr_find( sc->soc_oid );
237
238         /* the cr must be same as the structural class */
239         assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
240
241         /* check that the entry has required attrs of the content rule */
242         if( cr ) {
243                 if( !manage && cr->scr_obsolete ) {
244                         snprintf( textbuf, textlen, 
245                                 "content rule '%s' is obsolete",
246                                 ldap_contentrule2name( &cr->scr_crule ));
247
248                         Debug( LDAP_DEBUG_ANY,
249                                 "Entry (%s): %s\n",
250                                 e->e_dn, textbuf, 0 );
251
252                         rc = LDAP_OBJECT_CLASS_VIOLATION;
253                         goto leave;
254                 }
255
256                 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
257                         at = cr->scr_required[i];
258
259                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
260                                 if( a->a_desc->ad_type == at ) {
261                                         break;
262                                 }
263                         }
264
265                         /* not there => schema violation */
266                         if ( a == NULL ) {
267                                 snprintf( textbuf, textlen, 
268                                         "content rule '%s' requires attribute '%s'",
269                                         ldap_contentrule2name( &cr->scr_crule ),
270                                         at->sat_cname.bv_val );
271
272                                 Debug( LDAP_DEBUG_ANY,
273                                         "Entry (%s): %s\n",
274                                         e->e_dn, textbuf, 0 );
275
276                                 rc = LDAP_OBJECT_CLASS_VIOLATION;
277                                 goto leave;
278                         }
279                 }
280
281                 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
282                         at = cr->scr_precluded[i];
283
284                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
285                                 if( a->a_desc->ad_type == at ) {
286                                         break;
287                                 }
288                         }
289
290                         /* there => schema violation */
291                         if ( a != NULL ) {
292                                 snprintf( textbuf, textlen, 
293                                         "content rule '%s' precluded attribute '%s'",
294                                         ldap_contentrule2name( &cr->scr_crule ),
295                                         at->sat_cname.bv_val );
296
297                                 Debug( LDAP_DEBUG_ANY,
298                                         "Entry (%s): %s\n",
299                                         e->e_dn, textbuf, 0 );
300
301                                 rc = LDAP_OBJECT_CLASS_VIOLATION;
302                                 goto leave;
303                         }
304                 }
305         }
306
307         /* check that the entry has required attrs for each oc */
308         for ( i = 0; socs[i]; i++ ) {
309                 oc = socs[i];
310                 if ( !manage && oc->soc_obsolete ) {
311                         /* disallow obsolete classes */
312                         snprintf( textbuf, textlen, 
313                                 "objectClass '%s' is OBSOLETE",
314                                 aoc->a_vals[i].bv_val );
315
316                         Debug( LDAP_DEBUG_ANY,
317                                 "entry_check_schema(%s): %s\n",
318                                 e->e_dn, textbuf, 0 );
319
320                         rc = LDAP_OBJECT_CLASS_VIOLATION;
321                         goto leave;
322                 }
323
324                 if ( oc->soc_check ) {
325                         rc = (oc->soc_check)( op->o_bd, e, oc,
326                                 text, textbuf, textlen );
327                         if( rc != LDAP_SUCCESS ) {
328                                 goto leave;
329                         }
330                 }
331
332                 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
333                         /* object class is abstract */
334                         if ( oc != slap_schema.si_oc_top &&
335                                 !is_object_subclass( oc, sc ))
336                         {
337                                 int j;
338                                 ObjectClass *xc = NULL;
339                                 for( j=0; socs[j]; j++ ) {
340                                         if( i != j ) {
341                                                 xc = socs[j];
342
343                                                 /* since we previous check against the
344                                                  * structural object of this entry, the
345                                                  * abstract class must be a (direct or indirect)
346                                                  * superclass of one of the auxiliary classes of
347                                                  * the entry.
348                                                  */
349                                                 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
350                                                         is_object_subclass( oc, xc ) )
351                                                 {
352                                                         xc = NULL;
353                                                         break;
354                                                 }
355                                         }
356                                 }
357
358                                 if( xc == NULL ) {
359                                         snprintf( textbuf, textlen, "instanstantiation of "
360                                                 "abstract objectClass '%s' not allowed",
361                                                 aoc->a_vals[i].bv_val );
362
363                                         Debug( LDAP_DEBUG_ANY,
364                                                 "entry_check_schema(%s): %s\n",
365                                                 e->e_dn, textbuf, 0 );
366
367                                         rc = LDAP_OBJECT_CLASS_VIOLATION;
368                                         goto leave;
369                                 }
370                         }
371
372                 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
373                         char *s;
374
375                         if( oc->soc_kind == LDAP_SCHEMA_AUXILIARY ) {
376                                 int k;
377
378                                 if( cr ) {
379                                         int j;
380
381                                         k = -1;
382                                         if( cr->scr_auxiliaries ) {
383                                                 for( j = 0; cr->scr_auxiliaries[j]; j++ ) {
384                                                         if( cr->scr_auxiliaries[j] == oc ) {
385                                                                 k = 0;
386                                                                 break;
387                                                         }
388                                                 }
389                                         }
390                                         if ( k ) {
391                                                 snprintf( textbuf, textlen, 
392                                                         "class '%s' not allowed by content rule '%s'",
393                                                         oc->soc_cname.bv_val,
394                                                         ldap_contentrule2name( &cr->scr_crule ) );
395                                         }
396                                 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
397                                         k = -1;
398                                         snprintf( textbuf, textlen, 
399                                                 "class '%s' not allowed by any content rule",
400                                                 oc->soc_cname.bv_val );
401                                 } else {
402                                         k = 0;  
403                                 }
404
405                                 if( k == -1 ) {
406                                         Debug( LDAP_DEBUG_ANY,
407                                                 "Entry (%s): %s\n",
408                                                 e->e_dn, textbuf, 0 );
409
410                                         rc = LDAP_OBJECT_CLASS_VIOLATION;
411                                         goto leave;
412                                 }
413                         }
414
415                         s = oc_check_required( e, oc, &aoc->a_vals[i] );
416                         if (s != NULL) {
417                                 snprintf( textbuf, textlen, 
418                                         "object class '%s' requires attribute '%s'",
419                                         aoc->a_vals[i].bv_val, s );
420
421                                 Debug( LDAP_DEBUG_ANY,
422                                         "Entry (%s): %s\n",
423                                         e->e_dn, textbuf, 0 );
424
425                                 rc = LDAP_OBJECT_CLASS_VIOLATION;
426                                 goto leave;
427                         }
428
429                         if( oc == slap_schema.si_oc_extensibleObject ) {
430                                 extensible=1;
431                         }
432                 }
433         }
434
435         if( extensible ) {
436                 *text = NULL;
437                 rc = LDAP_SUCCESS;
438                 goto leave;
439         }
440
441         /* check that each attr in the entry is allowed by some oc */
442         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
443                 rc = LDAP_OBJECT_CLASS_VIOLATION;
444
445                 if( cr && cr->scr_required ) {
446                         for( i=0; cr->scr_required[i]; i++ ) {
447                                 if( cr->scr_required[i] == a->a_desc->ad_type ) {
448                                         rc = LDAP_SUCCESS;
449                                         break;
450                                 }
451                         }
452                 }
453
454                 if( rc != LDAP_SUCCESS && cr && cr->scr_allowed ) {
455                         for( i=0; cr->scr_allowed[i]; i++ ) {
456                                 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
457                                         rc = LDAP_SUCCESS;
458                                         break;
459                                 }
460                         }
461                 }
462
463                 if( rc != LDAP_SUCCESS ) 
464                 {
465                         rc = oc_check_allowed( a->a_desc->ad_type, socs, sc );
466                 }
467
468                 if ( rc != LDAP_SUCCESS ) {
469                         char *type = a->a_desc->ad_cname.bv_val;
470
471                         snprintf( textbuf, textlen, 
472                                 "attribute '%s' not allowed",
473                                 type );
474
475                         Debug( LDAP_DEBUG_ANY,
476                             "Entry (%s), %s\n",
477                             e->e_dn, textbuf, 0 );
478
479                         goto leave;
480                 }
481         }
482
483         *text = NULL;
484 leave:
485         slap_sl_free( socs, op->o_tmpmemctx );
486         return rc;
487 }
488
489 static char *
490 oc_check_required(
491         Entry *e,
492         ObjectClass *oc,
493         struct berval *ocname )
494 {
495         AttributeType   *at;
496         int             i;
497         Attribute       *a;
498
499         Debug( LDAP_DEBUG_TRACE,
500                 "oc_check_required entry (%s), objectClass \"%s\"\n",
501                 e->e_dn, ocname->bv_val, 0 );
502
503
504         /* check for empty oc_required */
505         if(oc->soc_required == NULL) {
506                 return NULL;
507         }
508
509         /* for each required attribute */
510         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
511                 at = oc->soc_required[i];
512                 /* see if it's in the entry */
513                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
514                         if( a->a_desc->ad_type == at ) {
515                                 break;
516                         }
517                 }
518                 /* not there => schema violation */
519                 if ( a == NULL ) {
520                         return at->sat_cname.bv_val;
521                 }
522         }
523
524         return( NULL );
525 }
526
527 int oc_check_allowed(
528         AttributeType *at,
529         ObjectClass **socs,
530         ObjectClass *sc )
531 {
532         int             i, j;
533
534         Debug( LDAP_DEBUG_TRACE,
535                 "oc_check_allowed type \"%s\"\n",
536                 at->sat_cname.bv_val, 0, 0 );
537
538         /* always allow objectClass attribute */
539         if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
540                 return LDAP_SUCCESS;
541         }
542
543         /*
544          * All operational attributions are allowed by schema rules.
545          */
546         if( is_at_operational(at) ) {
547                 return LDAP_SUCCESS;
548         }
549
550         /* check to see if its allowed by the structuralObjectClass */
551         if( sc ) {
552                 /* does it require the type? */
553                 for ( j = 0; sc->soc_required != NULL && 
554                         sc->soc_required[j] != NULL; j++ )
555                 {
556                         if( at == sc->soc_required[j] ) {
557                                 return LDAP_SUCCESS;
558                         }
559                 }
560
561                 /* does it allow the type? */
562                 for ( j = 0; sc->soc_allowed != NULL && 
563                         sc->soc_allowed[j] != NULL; j++ )
564                 {
565                         if( at == sc->soc_allowed[j] ) {
566                                 return LDAP_SUCCESS;
567                         }
568                 }
569         }
570
571         /* check that the type appears as req or opt in at least one oc */
572         for ( i = 0; socs[i]; i++ ) {
573                 /* if we know about the oc */
574                 ObjectClass     *oc = socs[i];
575                 /* extensibleObject allows all */
576                 if ( oc == slap_schema.si_oc_extensibleObject ) {
577                         return LDAP_SUCCESS;
578                 }
579                 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
580                         ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
581                 {
582                         /* does it require the type? */
583                         for ( j = 0; oc->soc_required != NULL && 
584                                 oc->soc_required[j] != NULL; j++ )
585                         {
586                                 if( at == oc->soc_required[j] ) {
587                                         return LDAP_SUCCESS;
588                                 }
589                         }
590                         /* does it allow the type? */
591                         for ( j = 0; oc->soc_allowed != NULL && 
592                                 oc->soc_allowed[j] != NULL; j++ )
593                         {
594                                 if( at == oc->soc_allowed[j] ) {
595                                         return LDAP_SUCCESS;
596                                 }
597                         }
598                 }
599         }
600
601         /* not allowed by any oc */
602         return LDAP_OBJECT_CLASS_VIOLATION;
603 }
604
605 /*
606  * Determine the structural object class from a set of OIDs
607  */
608 int structural_class(
609         BerVarray ocs,
610         ObjectClass **scp,
611         ObjectClass ***socsp,
612         const char **text,
613         char *textbuf, size_t textlen,
614         void *ctx )
615 {
616         int i, nocs;
617         ObjectClass *oc, **socs;
618         ObjectClass *sc = NULL;
619         int scn = -1;
620
621         *text = "structural_class: internal error";
622
623         /* count them */
624         for( i=0; ocs[i].bv_val; i++ ) ;
625         nocs = i;
626         
627         socs = slap_sl_malloc( (nocs+1) * sizeof(ObjectClass *), ctx );
628
629         for( i=0; ocs[i].bv_val; i++ ) {
630                 socs[i] = oc_bvfind( &ocs[i] );
631
632                 if( socs[i] == NULL ) {
633                         snprintf( textbuf, textlen,
634                                 "unrecognized objectClass '%s'",
635                                 ocs[i].bv_val );
636                         *text = textbuf;
637                         goto fail;
638                 }
639         }
640         socs[i] = NULL;
641
642         for( i=0; ocs[i].bv_val; i++ ) {
643                 oc = socs[i];
644                 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
645                         if( sc == NULL || is_object_subclass( sc, oc ) ) {
646                                 sc = oc;
647                                 scn = i;
648
649                         } else if ( !is_object_subclass( oc, sc ) ) {
650                                 int j;
651                                 ObjectClass *xc = NULL;
652
653                                 /* find common superior */
654                                 for( j=i+1; ocs[j].bv_val; j++ ) {
655                                         xc = socs[j];
656
657                                         if( xc == NULL ) {
658                                                 snprintf( textbuf, textlen,
659                                                         "unrecognized objectClass '%s'",
660                                                         ocs[j].bv_val );
661                                                 *text = textbuf;
662                                                 goto fail;
663                                         }
664
665                                         if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
666                                                 xc = NULL;
667                                                 continue;
668                                         }
669
670                                         if( is_object_subclass( sc, xc ) &&
671                                                 is_object_subclass( oc, xc ) )
672                                         {
673                                                 /* found common subclass */
674                                                 break;
675                                         }
676
677                                         xc = NULL;
678                                 }
679
680                                 if( xc == NULL ) {
681                                         /* no common subclass */
682                                         snprintf( textbuf, textlen,
683                                                 "invalid structural object class chain (%s/%s)",
684                                                 ocs[scn].bv_val, ocs[i].bv_val );
685                                         *text = textbuf;
686                                         goto fail;
687                                 }
688                         }
689                 }
690         }
691
692         if( scp ) {
693                 *scp = sc;
694         }
695
696         if( sc == NULL ) {
697                 *text = "no structural object class provided";
698                 goto fail;
699         }
700
701         if( scn < 0 ) {
702                 *text = "invalid structural object class";
703                 goto fail;
704         }
705
706         if ( socsp ) {
707                 *socsp = socs;
708         } else {
709                 slap_sl_free( socs, ctx );
710         }
711         *text = NULL;
712
713         return LDAP_SUCCESS;
714
715 fail:
716         slap_sl_free( socs, ctx );
717         return LDAP_OBJECT_CLASS_VIOLATION;
718 }
719
720 /*
721  * Return structural object class from list of modifications
722  */
723 int mods_structural_class(
724         Modifications *mods,
725         struct berval *sc,
726         const char **text,
727         char *textbuf, size_t textlen, void *ctx )
728 {
729         Modifications *ocmod = NULL;
730         ObjectClass *ssc;
731         int rc;
732
733         for( ; mods != NULL; mods = mods->sml_next ) {
734                 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
735                         if( ocmod != NULL ) {
736                                 *text = "entry has multiple objectClass attributes";
737                                 return LDAP_OBJECT_CLASS_VIOLATION;
738                         }
739                         ocmod = mods;
740                 }
741         }
742
743         if( ocmod == NULL ) {
744                 *text = "entry has no objectClass attribute";
745                 return LDAP_OBJECT_CLASS_VIOLATION;
746         }
747
748         if( ocmod->sml_values == NULL || ocmod->sml_values[0].bv_val == NULL ) {
749                 *text = "objectClass attribute has no values";
750                 return LDAP_OBJECT_CLASS_VIOLATION;
751         }
752
753         rc = structural_class( ocmod->sml_values, &ssc, NULL,
754                 text, textbuf, textlen, ctx );
755         if ( rc == LDAP_SUCCESS )
756                 *sc = ssc->soc_cname;
757         return rc;
758 }
759
760
761 static int
762 entry_naming_check(
763         Entry *e,
764         int manage,
765         const char** text,
766         char *textbuf, size_t textlen )
767 {
768         /* naming check */
769         LDAPRDN         rdn = NULL;
770         const char      *p = NULL;
771         ber_len_t       cnt;
772         int             rc = LDAP_SUCCESS;
773
774         if ( BER_BVISEMPTY( &e->e_name )) {
775                 return LDAP_SUCCESS;
776         }
777
778         /*
779          * Get attribute type(s) and attribute value(s) of our RDN
780          */
781         if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
782                 LDAP_DN_FORMAT_LDAP ) )
783         {
784                 *text = "unrecongized attribute type(s) in RDN";
785                 return LDAP_INVALID_DN_SYNTAX;
786         }
787
788         /* Check that each AVA of the RDN is present in the entry */
789         /* FIXME: Should also check that each AVA lists a distinct type */
790         for ( cnt = 0; rdn[cnt]; cnt++ ) {
791                 LDAPAVA *ava = rdn[cnt];
792                 AttributeDescription *desc = NULL;
793                 Attribute *attr;
794                 const char *errtext;
795
796                 if( ava->la_flags & LDAP_AVA_BINARY ) {
797                         snprintf( textbuf, textlen, 
798                                 "value of naming attribute '%s' in unsupported BER form",
799                                 ava->la_attr.bv_val );
800                         rc = LDAP_NAMING_VIOLATION;
801                 }
802
803                 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
804                 if ( rc != LDAP_SUCCESS ) {
805                         snprintf( textbuf, textlen, "%s (in RDN)", errtext );
806                         break;
807                 }
808
809                 if( desc->ad_type->sat_usage ) {
810                         snprintf( textbuf, textlen, 
811                                 "naming attribute '%s' is operational",
812                                 ava->la_attr.bv_val );
813                         rc = LDAP_NAMING_VIOLATION;
814                         break;
815                 }
816  
817                 if( desc->ad_type->sat_collective ) {
818                         snprintf( textbuf, textlen, 
819                                 "naming attribute '%s' is collective",
820                                 ava->la_attr.bv_val );
821                         rc = LDAP_NAMING_VIOLATION;
822                         break;
823                 }
824
825                 if( !manage && desc->ad_type->sat_obsolete ) {
826                         snprintf( textbuf, textlen, 
827                                 "naming attribute '%s' is obsolete",
828                                 ava->la_attr.bv_val );
829                         rc = LDAP_NAMING_VIOLATION;
830                         break;
831                 }
832
833                 if( !desc->ad_type->sat_equality ) {
834                         snprintf( textbuf, textlen, 
835                                 "naming attribute '%s' has no equality matching rule",
836                                 ava->la_attr.bv_val );
837                         rc = LDAP_NAMING_VIOLATION;
838                         break;
839                 }
840
841                 if( !desc->ad_type->sat_equality->smr_match ) {
842                         snprintf( textbuf, textlen, 
843                                 "naming attribute '%s' has unsupported equality matching rule",
844                                 ava->la_attr.bv_val );
845                         rc = LDAP_NAMING_VIOLATION;
846                         break;
847                 }
848
849                 /* find the naming attribute */
850                 attr = attr_find( e->e_attrs, desc );
851                 if ( attr == NULL ) {
852                         snprintf( textbuf, textlen, 
853                                 "naming attribute '%s' is not present in entry",
854                                 ava->la_attr.bv_val );
855                         rc = LDAP_NAMING_VIOLATION;
856                         break;
857                 }
858
859                 rc = value_find_ex( desc, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX|
860                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
861                         attr->a_nvals, &ava->la_value, NULL );
862
863                 if( rc != 0 ) {
864                         switch( rc ) {
865                         case LDAP_INAPPROPRIATE_MATCHING:
866                                 snprintf( textbuf, textlen, 
867                                         "inappropriate matching for naming attribute '%s'",
868                                         ava->la_attr.bv_val );
869                                 break;
870                         case LDAP_INVALID_SYNTAX:
871                                 snprintf( textbuf, textlen, 
872                                         "value of naming attribute '%s' is invalid",
873                                         ava->la_attr.bv_val );
874                                 break;
875                         case LDAP_NO_SUCH_ATTRIBUTE:
876                                 snprintf( textbuf, textlen, 
877                                         "value of naming attribute '%s' is not present in entry",
878                                         ava->la_attr.bv_val );
879                                 break;
880                         default:
881                                 snprintf( textbuf, textlen, 
882                                         "naming attribute '%s' is inappropriate",
883                                         ava->la_attr.bv_val );
884                         }
885                         rc = LDAP_NAMING_VIOLATION;
886                         break;
887                 }
888         }
889
890         ldap_rdnfree( rdn );
891         return rc;
892 }
893