]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
import fix to ITS#5474
[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-2008 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         const char** text,
51         char *textbuf, size_t textlen )
52 {
53         Attribute       *a, *asc, *aoc;
54         ObjectClass *sc, *oc;
55         AttributeType *at;
56         ContentRule *cr;
57         int     rc, i;
58         struct berval nsc;
59         AttributeDescription *ad_structuralObjectClass
60                 = slap_schema.si_ad_structuralObjectClass;
61         AttributeDescription *ad_objectClass
62                 = slap_schema.si_ad_objectClass;
63         int extensible = 0;
64         int subentry = is_entry_subentry( e );
65         int collectiveSubentry = 0;
66
67         if ( SLAP_NO_SCHEMA_CHECK( 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                         int 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( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
98                         snprintf( textbuf, textlen,
99                                 "'%s' can only appear in collectiveAttributeSubentry",
100                                 type );
101                         return LDAP_OBJECT_CLASS_VIOLATION;
102                 }
103
104                 /* if single value type, check for multiple values */
105                 if( is_at_single_value( a->a_desc->ad_type ) &&
106                         a->a_vals[1].bv_val != NULL )
107                 {
108                         snprintf( textbuf, textlen, 
109                                 "attribute '%s' cannot have multiple values",
110                                 type );
111
112                         Debug( LDAP_DEBUG_ANY,
113                             "Entry (%s), %s\n",
114                             e->e_dn, textbuf, 0 );
115
116                         return LDAP_CONSTRAINT_VIOLATION;
117                 }
118         }
119
120         /* find the structural object class attribute */
121         asc = attr_find( e->e_attrs, ad_structuralObjectClass );
122         if ( asc == NULL ) {
123                 Debug( LDAP_DEBUG_ANY,
124                         "No structuralObjectClass for entry (%s)\n",
125                     e->e_dn, 0, 0 );
126
127                 *text = "no structuralObjectClass operational attribute";
128                 return LDAP_OTHER;
129         }
130
131         assert( asc->a_vals != NULL );
132         assert( asc->a_vals[0].bv_val != NULL );
133         assert( asc->a_vals[1].bv_val == NULL );
134
135         sc = oc_bvfind( &asc->a_vals[0] );
136         if( sc == NULL ) {
137                 snprintf( textbuf, textlen, 
138                         "unrecognized structuralObjectClass '%s'",
139                         asc->a_vals[0].bv_val );
140
141                 Debug( LDAP_DEBUG_ANY,
142                         "entry_check_schema(%s): %s\n",
143                         e->e_dn, textbuf, 0 );
144
145                 return LDAP_OBJECT_CLASS_VIOLATION;
146         }
147
148         if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
149                 snprintf( textbuf, textlen, 
150                         "structuralObjectClass '%s' is not STRUCTURAL",
151                         asc->a_vals[0].bv_val );
152
153                 Debug( LDAP_DEBUG_ANY,
154                         "entry_check_schema(%s): %s\n",
155                         e->e_dn, textbuf, 0 );
156
157                 return LDAP_OTHER;
158         }
159
160         if( !manage && sc->soc_obsolete ) {
161                 snprintf( textbuf, textlen, 
162                         "structuralObjectClass '%s' is OBSOLETE",
163                         asc->a_vals[0].bv_val );
164
165                 Debug( LDAP_DEBUG_ANY,
166                         "entry_check_schema(%s): %s\n",
167                         e->e_dn, textbuf, 0 );
168
169                 return LDAP_OBJECT_CLASS_VIOLATION;
170         }
171
172         /* find the object class attribute */
173         aoc = attr_find( e->e_attrs, ad_objectClass );
174         if ( aoc == NULL ) {
175                 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
176                     e->e_dn, 0, 0 );
177
178                 *text = "no objectClass attribute";
179                 return LDAP_OBJECT_CLASS_VIOLATION;
180         }
181
182         assert( aoc->a_vals != NULL );
183         assert( aoc->a_vals[0].bv_val != NULL );
184
185         rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
186         if( rc != LDAP_SUCCESS ) {
187                 return rc;
188         }
189
190         *text = textbuf;
191
192         if ( oc == NULL ) {
193                 snprintf( textbuf, textlen, 
194                         "unrecognized objectClass '%s'",
195                         aoc->a_vals[0].bv_val );
196                 return LDAP_OBJECT_CLASS_VIOLATION;
197
198         } else if ( sc != slap_schema.si_oc_glue && sc != oc ) {
199                 snprintf( textbuf, textlen, 
200                         "structural object class modification "
201                         "from '%s' to '%s' not allowed",
202                         asc->a_vals[0].bv_val, nsc.bv_val );
203                 return LDAP_NO_OBJECT_CLASS_MODS;
204         } else if ( sc == slap_schema.si_oc_glue ) {
205                 sc = oc;
206         }
207
208         /* naming check */
209         if ( !is_entry_objectclass ( e, slap_schema.si_oc_glue, 0 ) ) {
210                 rc = entry_naming_check( e, manage, text, textbuf, textlen );
211                 if( rc != LDAP_SUCCESS ) {
212                         return rc;
213                 }
214         } else {
215                 /* Glue Entry */
216         }
217
218         /* find the content rule for the structural class */
219         cr = cr_find( sc->soc_oid );
220
221         /* the cr must be same as the structural class */
222         assert( !cr || !strcmp( cr->scr_oid, sc->soc_oid ) );
223
224         /* check that the entry has required attrs of the content rule */
225         if( cr ) {
226                 if( !manage && cr->scr_obsolete ) {
227                         snprintf( textbuf, textlen, 
228                                 "content rule '%s' is obsolete",
229                                 ldap_contentrule2name( &cr->scr_crule ));
230
231                         Debug( LDAP_DEBUG_ANY,
232                                 "Entry (%s): %s\n",
233                                 e->e_dn, textbuf, 0 );
234
235                         return LDAP_OBJECT_CLASS_VIOLATION;
236                 }
237
238                 if( cr->scr_required ) for( i=0; cr->scr_required[i]; i++ ) {
239                         at = cr->scr_required[i];
240
241                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
242                                 if( a->a_desc->ad_type == at ) {
243                                         break;
244                                 }
245                         }
246
247                         /* not there => schema violation */
248                         if ( a == NULL ) {
249                                 snprintf( textbuf, textlen, 
250                                         "content rule '%s' requires attribute '%s'",
251                                         ldap_contentrule2name( &cr->scr_crule ),
252                                         at->sat_cname.bv_val );
253
254                                 Debug( LDAP_DEBUG_ANY,
255                                         "Entry (%s): %s\n",
256                                         e->e_dn, textbuf, 0 );
257
258                                 return LDAP_OBJECT_CLASS_VIOLATION;
259                         }
260                 }
261
262                 if( cr->scr_precluded ) for( i=0; cr->scr_precluded[i]; i++ ) {
263                         at = cr->scr_precluded[i];
264
265                         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
266                                 if( a->a_desc->ad_type == at ) {
267                                         break;
268                                 }
269                         }
270
271                         /* there => schema violation */
272                         if ( a != NULL ) {
273                                 snprintf( textbuf, textlen, 
274                                         "content rule '%s' precluded attribute '%s'",
275                                         ldap_contentrule2name( &cr->scr_crule ),
276                                         at->sat_cname.bv_val );
277
278                                 Debug( LDAP_DEBUG_ANY,
279                                         "Entry (%s): %s\n",
280                                         e->e_dn, textbuf, 0 );
281
282                                 return LDAP_OBJECT_CLASS_VIOLATION;
283                         }
284                 }
285         }
286
287         /* check that the entry has required attrs for each oc */
288         for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
289                 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
290                         snprintf( textbuf, textlen, 
291                                 "unrecognized objectClass '%s'",
292                                 aoc->a_vals[i].bv_val );
293
294                         Debug( LDAP_DEBUG_ANY,
295                                 "entry_check_schema(%s): %s\n",
296                                 e->e_dn, textbuf, 0 );
297
298                         return LDAP_OBJECT_CLASS_VIOLATION;
299                 }
300
301                 if ( !manage && oc->soc_obsolete ) {
302                         /* disallow obsolete classes */
303                         snprintf( textbuf, textlen, 
304                                 "objectClass '%s' is OBSOLETE",
305                                 aoc->a_vals[i].bv_val );
306
307                         Debug( LDAP_DEBUG_ANY,
308                                 "entry_check_schema(%s): %s\n",
309                                 e->e_dn, textbuf, 0 );
310
311                         return LDAP_OBJECT_CLASS_VIOLATION;
312                 }
313
314                 if ( oc->soc_check ) {
315                         int rc = (oc->soc_check)( op->o_bd, e, oc,
316                                 text, textbuf, textlen );
317                         if( rc != LDAP_SUCCESS ) {
318                                 return rc;
319                         }
320                 }
321
322                 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
323                         /* object class is abstract */
324                         if ( oc != slap_schema.si_oc_top &&
325                                 !is_object_subclass( oc, sc ))
326                         {
327                                 int j;
328                                 ObjectClass *xc = NULL;
329                                 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
330                                         if( i != j ) {
331                                                 xc = oc_bvfind( &aoc->a_vals[j] );
332                                                 if( xc == NULL ) {
333                                                         snprintf( textbuf, textlen, 
334                                                                 "unrecognized objectClass '%s'",
335                                                                 aoc->a_vals[j].bv_val );
336
337                                                         Debug( LDAP_DEBUG_ANY,
338                                                                 "entry_check_schema(%s): %s\n",
339                                                                 e->e_dn, textbuf, 0 );
340
341                                                         return LDAP_OBJECT_CLASS_VIOLATION;
342                                                 }
343
344                                                 /* since we previous check against the
345                                                  * structural object of this entry, the
346                                                  * abstract class must be a (direct or indirect)
347                                                  * superclass of one of the auxiliary classes of
348                                                  * the entry.
349                                                  */
350                                                 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
351                                                         is_object_subclass( oc, xc ) )
352                                                 {
353                                                         xc = NULL;
354                                                         break;
355                                                 }
356                                         }
357                                 }
358
359                                 if( xc != NULL ) {
360                                         snprintf( textbuf, textlen, "instantiation of "
361                                                 "abstract objectClass '%s' not allowed",
362                                                 aoc->a_vals[i].bv_val );
363
364                                         Debug( LDAP_DEBUG_ANY,
365                                                 "entry_check_schema(%s): %s\n",
366                                                 e->e_dn, textbuf, 0 );
367
368                                         return LDAP_OBJECT_CLASS_VIOLATION;
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                                 } else if ( global_disallows & SLAP_DISALLOW_AUX_WO_CR ) {
391                                         k = -1;
392                                 } else {
393                                         k = 0;  
394                                 }
395
396                                 if( k == -1 ) {
397                                         snprintf( textbuf, textlen, 
398                                                 "content rule '%s' does not allow class '%s'",
399                                                 ldap_contentrule2name( &cr->scr_crule ),
400                                                 oc->soc_cname.bv_val );
401
402                                         Debug( LDAP_DEBUG_ANY,
403                                                 "Entry (%s): %s\n",
404                                                 e->e_dn, textbuf, 0 );
405
406                                         return LDAP_OBJECT_CLASS_VIOLATION;
407                                 }
408                         }
409
410                         s = oc_check_required( e, oc, &aoc->a_vals[i] );
411                         if (s != NULL) {
412                                 snprintf( textbuf, textlen, 
413                                         "object class '%s' requires attribute '%s'",
414                                         aoc->a_vals[i].bv_val, s );
415
416                                 Debug( LDAP_DEBUG_ANY,
417                                         "Entry (%s): %s\n",
418                                         e->e_dn, textbuf, 0 );
419
420                                 return LDAP_OBJECT_CLASS_VIOLATION;
421                         }
422
423                         if( oc == slap_schema.si_oc_extensibleObject ) {
424                                 extensible=1;
425                         }
426                 }
427         }
428
429         if( extensible ) {
430                 *text = NULL;
431                 return LDAP_SUCCESS;
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                 int ret;
437
438                 ret = LDAP_OBJECT_CLASS_VIOLATION;
439
440                 if( cr && cr->scr_required ) {
441                         for( i=0; cr->scr_required[i]; i++ ) {
442                                 if( cr->scr_required[i] == a->a_desc->ad_type ) {
443                                         ret = LDAP_SUCCESS;
444                                         break;
445                                 }
446                         }
447                 }
448
449                 if( ret != LDAP_SUCCESS && cr && cr->scr_allowed ) {
450                         for( i=0; cr->scr_allowed[i]; i++ ) {
451                                 if( cr->scr_allowed[i] == a->a_desc->ad_type ) {
452                                         ret = LDAP_SUCCESS;
453                                         break;
454                                 }
455                         }
456                 }
457
458                 if( ret != LDAP_SUCCESS ) 
459                 {
460                         ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
461                 }
462
463                 if ( ret != LDAP_SUCCESS ) {
464                         char *type = a->a_desc->ad_cname.bv_val;
465
466                         snprintf( textbuf, textlen, 
467                                 "attribute '%s' not allowed",
468                                 type );
469
470                         Debug( LDAP_DEBUG_ANY,
471                             "Entry (%s), %s\n",
472                             e->e_dn, textbuf, 0 );
473
474                         return ret;
475                 }
476         }
477
478         *text = NULL;
479         return LDAP_SUCCESS;
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         BerVarray ocl,
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; ocl[i].bv_val != NULL; i++ ) {
566                 /* if we know about the oc */
567                 ObjectClass     *oc = oc_bvfind( &ocl[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         struct berval *scbv,
600         ObjectClass **scp,
601         const char **text,
602         char *textbuf, size_t textlen )
603 {
604         int i;
605         ObjectClass *oc;
606         ObjectClass *sc = NULL;
607         int scn = -1;
608
609         *text = "structural_class: internal error";
610         scbv->bv_len = 0;
611
612         for( i=0; ocs[i].bv_val; i++ ) {
613                 oc = oc_bvfind( &ocs[i] );
614
615                 if( oc == NULL ) {
616                         snprintf( textbuf, textlen,
617                                 "unrecognized objectClass '%s'",
618                                 ocs[i].bv_val );
619                         *text = textbuf;
620                         return LDAP_OBJECT_CLASS_VIOLATION;
621                 }
622
623                 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
624                         if( sc == NULL || is_object_subclass( sc, oc ) ) {
625                                 sc = oc;
626                                 scn = i;
627
628                         } else if ( !is_object_subclass( oc, sc ) ) {
629                                 int j;
630                                 ObjectClass *xc = NULL;
631
632                                 /* find common superior */
633                                 for( j=i+1; ocs[j].bv_val; j++ ) {
634                                         xc = oc_bvfind( &ocs[j] );
635
636                                         if( xc == NULL ) {
637                                                 snprintf( textbuf, textlen,
638                                                         "unrecognized objectClass '%s'",
639                                                         ocs[j].bv_val );
640                                                 *text = textbuf;
641                                                 return LDAP_OBJECT_CLASS_VIOLATION;
642                                         }
643
644                                         if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
645                                                 xc = NULL;
646                                                 continue;
647                                         }
648
649                                         if( is_object_subclass( sc, xc ) &&
650                                                 is_object_subclass( oc, xc ) )
651                                         {
652                                                 /* found common subclass */
653                                                 break;
654                                         }
655
656                                         xc = NULL;
657                                 }
658
659                                 if( xc == NULL ) {
660                                         /* no common subclass */
661                                         snprintf( textbuf, textlen,
662                                                 "invalid structural object class chain (%s/%s)",
663                                                 ocs[scn].bv_val, ocs[i].bv_val );
664                                         *text = textbuf;
665                                         return LDAP_OBJECT_CLASS_VIOLATION;
666                                 }
667                         }
668                 }
669         }
670
671         if( scp ) {
672                 *scp = sc;
673         }
674
675         if( sc == NULL ) {
676                 *text = "no structural object class provided";
677                 return LDAP_OBJECT_CLASS_VIOLATION;
678         }
679
680         if( scn < 0 ) {
681                 *text = "invalid structural object class";
682                 return LDAP_OBJECT_CLASS_VIOLATION;
683         }
684
685         *scbv = ocs[scn];
686
687         if( scbv->bv_len == 0 ) {
688                 *text = "invalid structural object class";
689                 return LDAP_OBJECT_CLASS_VIOLATION;
690         }
691
692         *text = NULL;
693
694         return LDAP_SUCCESS;
695 }
696
697 /*
698  * Return structural object class from list of modifications
699  */
700 int mods_structural_class(
701         Modifications *mods,
702         struct berval *sc,
703         const char **text,
704         char *textbuf, size_t textlen )
705 {
706         Modifications *ocmod = NULL;
707
708         for( ; mods != NULL; mods = mods->sml_next ) {
709                 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
710                         if( ocmod != NULL ) {
711                                 *text = "entry has multiple objectClass attributes";
712                                 return LDAP_OBJECT_CLASS_VIOLATION;
713                         }
714                         ocmod = mods;
715                 }
716         }
717
718         if( ocmod == NULL ) {
719                 *text = "entry has no objectClass attribute";
720                 return LDAP_OBJECT_CLASS_VIOLATION;
721         }
722
723         if( ocmod->sml_values == NULL || ocmod->sml_values[0].bv_val == NULL ) {
724                 *text = "objectClass attribute has no values";
725                 return LDAP_OBJECT_CLASS_VIOLATION;
726         }
727
728         return structural_class( ocmod->sml_values, sc, NULL,
729                 text, textbuf, textlen );
730 }
731
732
733 static int
734 entry_naming_check(
735         Entry *e,
736         int manage,
737         const char** text,
738         char *textbuf, size_t textlen )
739 {
740         /* naming check */
741         LDAPRDN         rdn = NULL;
742         const char      *p = NULL;
743         ber_len_t       cnt;
744         int             rc = LDAP_SUCCESS;
745
746         if ( BER_BVISEMPTY( &e->e_name )) {
747                 return LDAP_SUCCESS;
748         }
749
750         /*
751          * Get attribute type(s) and attribute value(s) of our RDN
752          */
753         if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&p,
754                 LDAP_DN_FORMAT_LDAP ) )
755         {
756                 *text = "unrecongized attribute type(s) in RDN";
757                 return LDAP_INVALID_DN_SYNTAX;
758         }
759
760         /* Check that each AVA of the RDN is present in the entry */
761         /* FIXME: Should also check that each AVA lists a distinct type */
762         for ( cnt = 0; rdn[cnt]; cnt++ ) {
763                 LDAPAVA *ava = rdn[cnt];
764                 AttributeDescription *desc = NULL;
765                 Attribute *attr;
766                 const char *errtext;
767
768                 if( ava->la_flags & LDAP_AVA_BINARY ) {
769                         snprintf( textbuf, textlen, 
770                                 "value of naming attribute '%s' in unsupported BER form",
771                                 ava->la_attr.bv_val );
772                         rc = LDAP_NAMING_VIOLATION;
773                 }
774
775                 rc = slap_bv2ad( &ava->la_attr, &desc, &errtext );
776                 if ( rc != LDAP_SUCCESS ) {
777                         snprintf( textbuf, textlen, "%s (in RDN)", errtext );
778                         break;
779                 }
780
781                 if( desc->ad_type->sat_usage ) {
782                         snprintf( textbuf, textlen, 
783                                 "naming attribute '%s' is operational",
784                                 ava->la_attr.bv_val );
785                         rc = LDAP_NAMING_VIOLATION;
786                         break;
787                 }
788  
789                 if( desc->ad_type->sat_collective ) {
790                         snprintf( textbuf, textlen, 
791                                 "naming attribute '%s' is collective",
792                                 ava->la_attr.bv_val );
793                         rc = LDAP_NAMING_VIOLATION;
794                         break;
795                 }
796
797                 if( !manage && desc->ad_type->sat_obsolete ) {
798                         snprintf( textbuf, textlen, 
799                                 "naming attribute '%s' is obsolete",
800                                 ava->la_attr.bv_val );
801                         rc = LDAP_NAMING_VIOLATION;
802                         break;
803                 }
804
805                 if( !desc->ad_type->sat_equality ) {
806                         snprintf( textbuf, textlen, 
807                                 "naming attribute '%s' has no equality matching rule",
808                                 ava->la_attr.bv_val );
809                         rc = LDAP_NAMING_VIOLATION;
810                         break;
811                 }
812
813                 if( !desc->ad_type->sat_equality->smr_match ) {
814                         snprintf( textbuf, textlen, 
815                                 "naming attribute '%s' has unsupported equality matching rule",
816                                 ava->la_attr.bv_val );
817                         rc = LDAP_NAMING_VIOLATION;
818                         break;
819                 }
820
821                 /* find the naming attribute */
822                 attr = attr_find( e->e_attrs, desc );
823                 if ( attr == NULL ) {
824                         snprintf( textbuf, textlen, 
825                                 "naming attribute '%s' is not present in entry",
826                                 ava->la_attr.bv_val );
827                         rc = LDAP_NAMING_VIOLATION;
828                         break;
829                 }
830
831                 rc = value_find_ex( desc, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX|
832                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
833                         attr->a_nvals, &ava->la_value, NULL );
834
835                 if( rc != 0 ) {
836                         switch( rc ) {
837                         case LDAP_INAPPROPRIATE_MATCHING:
838                                 snprintf( textbuf, textlen, 
839                                         "inappropriate matching for naming attribute '%s'",
840                                         ava->la_attr.bv_val );
841                                 break;
842                         case LDAP_INVALID_SYNTAX:
843                                 snprintf( textbuf, textlen, 
844                                         "value of naming attribute '%s' is invalid",
845                                         ava->la_attr.bv_val );
846                                 break;
847                         case LDAP_NO_SUCH_ATTRIBUTE:
848                                 snprintf( textbuf, textlen, 
849                                         "value of naming attribute '%s' is not present in entry",
850                                         ava->la_attr.bv_val );
851                                 break;
852                         default:
853                                 snprintf( textbuf, textlen, 
854                                         "naming attribute '%s' is inappropriate",
855                                         ava->la_attr.bv_val );
856                         }
857                         rc = LDAP_NAMING_VIOLATION;
858                         break;
859                 }
860         }
861
862         ldap_rdnfree( rdn );
863         return rc;
864 }
865