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