]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
Fix pkiUser
[openldap] / servers / slapd / schema_check.c
1 /* schema_check.c - routines to enforce schema definitions */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/ctype.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17 #include "ldap_pvt.h"
18
19 static char * oc_check_required(
20         Entry *e,
21         ObjectClass *oc,
22         struct berval *ocname );
23
24 /*
25  * entry_schema_check - check that entry e conforms to the schema required
26  * by its object class(es).
27  *
28  * returns 0 if so, non-zero otherwise.
29  */
30
31 int
32 entry_schema_check( 
33         Backend *be,
34         Entry *e,
35         Attribute *oldattrs,
36         const char** text,
37         char *textbuf, size_t textlen )
38 {
39         Attribute       *a, *asc, *aoc;
40         ObjectClass *sc, *oc;
41         int     rc, i;
42         struct berval nsc;
43         AttributeDescription *ad_structuralObjectClass
44                 = slap_schema.si_ad_structuralObjectClass;
45         AttributeDescription *ad_objectClass
46                 = slap_schema.si_ad_objectClass;
47         int extensible = 0;
48         int subentry = is_entry_subentry( e );
49         int collectiveSubentry = 0;
50
51 #if 0
52         if( subentry) collectiveSubentry = is_entry_collectiveAttributeSubentry( e );
53 #endif
54
55         *text = textbuf;
56
57         /* misc attribute checks */
58         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
59                 const char *type = a->a_desc->ad_cname.bv_val;
60
61                 /* there should be at least one value */
62                 assert( a->a_vals );
63                 assert( a->a_vals[0].bv_val != NULL ); 
64
65                 if( a->a_desc->ad_type->sat_check ) {
66                         int rc = (a->a_desc->ad_type->sat_check)(
67                                 be, e, a, text, textbuf, textlen );
68                         if( rc != LDAP_SUCCESS ) {
69                                 return rc;
70                         }
71                 }
72
73                 if( !collectiveSubentry && is_at_collective( a->a_desc->ad_type ) ) {
74                         snprintf( textbuf, textlen,
75                                 "'%s' can only appear in collectiveAttributeSubentry",
76                                 type );
77                         return LDAP_OBJECT_CLASS_VIOLATION;
78                 }
79
80                 /* if single value type, check for multiple values */
81                 if( is_at_single_value( a->a_desc->ad_type ) &&
82                         a->a_vals[1].bv_val != NULL )
83                 {
84                         snprintf( textbuf, textlen, 
85                                 "attribute '%s' cannot have multiple values",
86                                 type );
87
88 #ifdef NEW_LOGGING
89                         LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
90                                 "entry_schema_check: dn=\"%s\" %s\n",
91                                 e->e_dn, textbuf ));
92 #else
93                         Debug( LDAP_DEBUG_ANY,
94                             "Entry (%s), %s\n",
95                             e->e_dn, textbuf, 0 );
96 #endif
97
98                         return LDAP_CONSTRAINT_VIOLATION;
99                 }
100         }
101
102         /* it's a REALLY bad idea to disable schema checks */
103         if( !global_schemacheck ) return LDAP_SUCCESS;
104
105         /* find the object class attribute - could error out here */
106         asc = attr_find( e->e_attrs, ad_structuralObjectClass );
107         if ( asc == NULL ) {
108 #ifdef NEW_LOGGING
109                 LDAP_LOG(( "schema", LDAP_LEVEL_INFO, "entry_schema_check: "
110                         "No structuralObjectClass for entry (%s)\n",
111                         e->e_dn ));
112 #else
113                 Debug( LDAP_DEBUG_ANY,
114                         "No structuralObjectClass for entry (%s)\n",
115                     e->e_dn, 0, 0 );
116 #endif
117
118                 *text = "no structuralObjectClass operational attribute";
119                 return LDAP_OBJECT_CLASS_VIOLATION;
120         }
121
122         assert( asc->a_vals != NULL );
123         assert( asc->a_vals[0].bv_val != NULL );
124         assert( asc->a_vals[1].bv_val == NULL );
125
126         sc = oc_bvfind( &asc->a_vals[0] );
127         if( sc == NULL ) {
128                 snprintf( textbuf, textlen, 
129                         "unrecognized structuralObjectClass '%s'",
130                         asc->a_vals[0].bv_val );
131
132 #ifdef NEW_LOGGING
133                 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
134                         "entry_schema_check: dn (%s), %s\n",
135                         e->e_dn, textbuf ));
136 #else
137                 Debug( LDAP_DEBUG_ANY,
138                         "entry_check_schema(%s): %s\n",
139                         e->e_dn, textbuf, 0 );
140 #endif
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 #ifdef NEW_LOGGING
151                 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
152                         "entry_schema_check: dn (%s), %s\n",
153                         e->e_dn, textbuf ));
154 #else
155                 Debug( LDAP_DEBUG_ANY,
156                         "entry_check_schema(%s): %s\n",
157                         e->e_dn, textbuf, 0 );
158 #endif
159
160                 return LDAP_OBJECT_CLASS_VIOLATION;
161         }
162
163         /* find the object class attribute */
164         aoc = attr_find( e->e_attrs, ad_objectClass );
165         if ( aoc == NULL ) {
166 #ifdef NEW_LOGGING
167                 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
168                         "entry_schema_check: No objectClass for entry (%s).\n",
169                         e->e_dn ));
170 #else
171                 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
172                     e->e_dn, 0, 0 );
173 #endif
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         } else if ( nsc.bv_len == 0 ) {
186                 return LDAP_OBJECT_CLASS_VIOLATION;
187         }
188
189         *text = textbuf;
190
191         if ( oc == NULL ) {
192                 snprintf( textbuf, textlen, 
193                         "unrecognized objectClass '%s'",
194                         aoc->a_vals[0].bv_val );
195                 return LDAP_OBJECT_CLASS_VIOLATION;
196
197         } else if ( sc != oc ) {
198                 snprintf( textbuf, textlen, 
199                         "structuralObjectClass modification from '%s' to '%s' not allowed",
200                         asc->a_vals[0].bv_val, nsc.bv_val );
201                 return LDAP_NO_OBJECT_CLASS_MODS;
202         }
203
204         /* check that the entry has required attrs for each oc */
205         for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
206                 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
207                         snprintf( textbuf, textlen, 
208                                 "unrecognized objectClass '%s'",
209                                 aoc->a_vals[i].bv_val );
210
211 #ifdef NEW_LOGGING
212                         LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
213                                 "entry_schema_check: dn (%s), %s\n",
214                                 e->e_dn, textbuf ));
215 #else
216                         Debug( LDAP_DEBUG_ANY,
217                                 "entry_check_schema(%s): %s\n",
218                                 e->e_dn, textbuf, 0 );
219 #endif
220
221                         return LDAP_OBJECT_CLASS_VIOLATION;
222                 }
223
224                 if ( oc->soc_check ) {
225                         int rc = (oc->soc_check)( be, e, oc,
226                                 text, textbuf, textlen );
227                         if( rc != LDAP_SUCCESS ) {
228                                 return rc;
229                         }
230                 }
231
232                 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
233                         /* object class is abstract */
234                         if ( oc != slap_schema.si_oc_top &&
235                                 !is_object_subclass( oc, sc ))
236                         {
237                                 int j;
238                                 ObjectClass *xc = NULL;
239                                 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
240                                         if( i != j ) {
241                                                 xc = oc_bvfind( &aoc->a_vals[i] );
242                                                 if( xc == NULL ) {
243                                                         snprintf( textbuf, textlen, 
244                                                                 "unrecognized objectClass '%s'",
245                                                                 aoc->a_vals[i].bv_val );
246
247 #ifdef NEW_LOGGING
248                                                         LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
249                                                                 "entry_schema_check: dn (%s), %s\n",
250                                                                 e->e_dn, textbuf ));
251 #else
252                                                         Debug( LDAP_DEBUG_ANY,
253                                                                 "entry_check_schema(%s): %s\n",
254                                                                 e->e_dn, textbuf, 0 );
255 #endif
256
257                                                         return LDAP_OBJECT_CLASS_VIOLATION;
258                                                 }
259
260                                                 /* since we previous check against the
261                                                  * structural object of this entry, the
262                                                  * abstract class must be a (direct or indirect)
263                                                  * superclass of one of the auxiliary classes of
264                                                  * the entry.
265                                                  */
266                                                 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
267                                                         is_object_subclass( oc, xc ) )
268                                                 {
269                                                         break;;
270                                                 }
271
272                                                 xc = NULL;
273                                         }
274                                 }
275
276                                 if( xc == NULL ) {
277                                         snprintf( textbuf, textlen, "instanstantiation of "
278                                                 "abstract objectClass '%s' not allowed",
279                                                 aoc->a_vals[i].bv_val );
280
281 #ifdef NEW_LOGGING
282                                         LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
283                                                 "entry_schema_check: dn (%s), %s\n",
284                                                 e->e_dn, textbuf ));
285 #else
286                                         Debug( LDAP_DEBUG_ANY,
287                                                 "entry_check_schema(%s): %s\n",
288                                                 e->e_dn, textbuf, 0 );
289 #endif
290
291                                         return LDAP_OBJECT_CLASS_VIOLATION;
292                                 }
293                         }
294
295                 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
296                         char *s = oc_check_required( e, oc, &aoc->a_vals[i] );
297
298                         if (s != NULL) {
299                                 snprintf( textbuf, textlen, 
300                                         "object class '%s' requires attribute '%s'",
301                                         aoc->a_vals[i].bv_val, s );
302
303 #ifdef NEW_LOGGING
304                                 LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
305                                         "entry_schema_check: dn=\"%s\" %s",
306                                         e->e_dn, textbuf ));
307 #else
308                                 Debug( LDAP_DEBUG_ANY,
309                                         "Entry (%s): %s\n",
310                                         e->e_dn, textbuf, 0 );
311 #endif
312
313                                 return LDAP_OBJECT_CLASS_VIOLATION;
314                         }
315
316                         if( oc == slap_schema.si_oc_extensibleObject ) {
317                                 extensible=1;
318                         }
319                 }
320         }
321
322         if( extensible ) {
323                 return LDAP_SUCCESS;
324         }
325
326         /* check that each attr in the entry is allowed by some oc */
327         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
328                 int ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
329                 if ( ret != LDAP_SUCCESS ) {
330                         char *type = a->a_desc->ad_cname.bv_val;
331
332                         snprintf( textbuf, textlen, 
333                                 "attribute '%s' not allowed",
334                                 type );
335
336 #ifdef NEW_LOGGING
337                         LDAP_LOG(( "schema", LDAP_LEVEL_INFO,
338                                 "entry_schema_check: dn=\"%s\" %s\n",
339                                 e->e_dn, textbuf ));
340 #else
341                         Debug( LDAP_DEBUG_ANY,
342                             "Entry (%s), %s\n",
343                             e->e_dn, textbuf, 0 );
344 #endif
345
346                         return ret;
347                 }
348         }
349
350         return LDAP_SUCCESS;
351 }
352
353 static char *
354 oc_check_required(
355         Entry *e,
356         ObjectClass *oc,
357         struct berval *ocname )
358 {
359         AttributeType   *at;
360         int             i;
361         Attribute       *a;
362
363 #ifdef NEW_LOGGING
364         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
365                 "oc_check_required: dn (%s), objectClass \"%s\"\n",
366         e->e_dn, ocname->bv_val ));
367 #else
368         Debug( LDAP_DEBUG_TRACE,
369                 "oc_check_required entry (%s), objectClass \"%s\"\n",
370                 e->e_dn, ocname->bv_val, 0 );
371 #endif
372
373
374         /* check for empty oc_required */
375         if(oc->soc_required == NULL) {
376                 return NULL;
377         }
378
379         /* for each required attribute */
380         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
381                 at = oc->soc_required[i];
382                 /* see if it's in the entry */
383                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
384                         if( a->a_desc->ad_type == at ) {
385                                 break;
386                         }
387                 }
388                 /* not there => schema violation */
389                 if ( a == NULL ) {
390                         return at->sat_cname.bv_val;
391                 }
392         }
393
394         return( NULL );
395 }
396
397 int oc_check_allowed(
398         AttributeType *at,
399         BerVarray ocl,
400         ObjectClass *sc )
401 {
402         int             i, j;
403
404 #ifdef NEW_LOGGING
405         LDAP_LOG(( "schema", LDAP_LEVEL_ENTRY,
406                 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val ));
407 #else
408         Debug( LDAP_DEBUG_TRACE,
409                 "oc_check_allowed type \"%s\"\n",
410                 at->sat_cname.bv_val, 0, 0 );
411 #endif
412
413         /* always allow objectClass attribute */
414         if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
415                 return LDAP_SUCCESS;
416         }
417
418         /*
419          * All operational attributions are allowed by schema rules.
420          */
421         if( is_at_operational(at) ) {
422                 return LDAP_SUCCESS;
423         }
424
425         /* check to see if its allowed by the structuralObjectClass */
426         if( sc ) {
427                 /* does it require the type? */
428                 for ( j = 0; sc->soc_required != NULL && 
429                         sc->soc_required[j] != NULL; j++ )
430                 {
431                         if( at == sc->soc_required[j] ) {
432                                 return LDAP_SUCCESS;
433                         }
434                 }
435
436                 /* does it allow the type? */
437                 for ( j = 0; sc->soc_allowed != NULL && 
438                         sc->soc_allowed[j] != NULL; j++ )
439                 {
440                         if( at == sc->soc_allowed[j] ) {
441                                 return LDAP_SUCCESS;
442                         }
443                 }
444         }
445
446         /* check that the type appears as req or opt in at least one oc */
447         for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
448                 /* if we know about the oc */
449                 ObjectClass     *oc = oc_bvfind( &ocl[i] );
450                 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
451                         ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
452                 {
453                         /* does it require the type? */
454                         for ( j = 0; oc->soc_required != NULL && 
455                                 oc->soc_required[j] != NULL; j++ )
456                         {
457                                 if( at == oc->soc_required[j] ) {
458                                         return LDAP_SUCCESS;
459                                 }
460                         }
461                         /* does it allow the type? */
462                         for ( j = 0; oc->soc_allowed != NULL && 
463                                 oc->soc_allowed[j] != NULL; j++ )
464                         {
465                                 if( at == oc->soc_allowed[j] ) {
466                                         return LDAP_SUCCESS;
467                                 }
468                         }
469                 }
470         }
471
472         /* not allowed by any oc */
473         return LDAP_OBJECT_CLASS_VIOLATION;
474 }
475
476 /*
477  * Determine the structural object class from a set of OIDs
478  */
479 int structural_class(
480         BerVarray ocs,
481         struct berval *scbv,
482         ObjectClass **scp,
483         const char **text,
484         char *textbuf, size_t textlen )
485 {
486         int i;
487         ObjectClass *oc;
488         ObjectClass *sc = NULL;
489         int scn = -1;
490
491         *text = "structural_class: internal error";
492         scbv->bv_len = 0;
493
494         for( i=0; ocs[i].bv_val; i++ ) {
495                 oc = oc_bvfind( &ocs[i] );
496
497                 if( oc == NULL ) {
498                         snprintf( textbuf, textlen,
499                                 "unrecognized objectClass '%s'",
500                                 ocs[i].bv_val );
501                         *text = textbuf;
502                         return LDAP_OBJECT_CLASS_VIOLATION;
503                 }
504
505                 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
506                         if( sc == NULL || is_object_subclass( sc, oc ) ) {
507                                 sc = oc;
508                                 scn = i;
509
510                         } else if ( !is_object_subclass( oc, sc ) ) {
511                                 int j;
512                                 ObjectClass *xc = NULL;
513
514                                 /* find common superior */
515                                 for( j=i+1; ocs[j].bv_val; j++ ) {
516                                         xc = oc_bvfind( &ocs[j] );
517
518                                         if( xc == NULL ) {
519                                                 snprintf( textbuf, textlen,
520                                                         "unrecognized objectClass '%s'",
521                                                         ocs[i].bv_val );
522                                                 *text = textbuf;
523                                                 return LDAP_OBJECT_CLASS_VIOLATION;
524                                         }
525
526                                         if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
527                                                 xc = NULL;
528                                                 continue;
529                                         }
530
531                                         if( is_object_subclass( sc, xc ) &&
532                                                 is_object_subclass( oc, xc ) )
533                                         {
534                                                 /* found common subclass */
535                                                 break;
536                                         }
537
538                                         xc = NULL;
539                                 }
540
541                                 if( xc == NULL ) {
542                                         /* no common subclass */
543                                         snprintf( textbuf, textlen,
544                                                 "invalid structural object class chain (%s/%s)",
545                                                 ocs[scn].bv_val, ocs[i].bv_val );
546                                         *text = textbuf;
547                                         return LDAP_OBJECT_CLASS_VIOLATION;
548                                 }
549                         }
550                 }
551         }
552
553         if( scp )
554                 *scp = sc;
555
556         if( sc == NULL ) {
557                 *text = "no structural object classes provided";
558                 return LDAP_OBJECT_CLASS_VIOLATION;
559         }
560
561         *scbv = ocs[scn];
562         return LDAP_SUCCESS;
563 }
564
565 /*
566  * Return structural object class from list of modifications
567  */
568 int mods_structural_class(
569         Modifications *mods,
570         struct berval *sc,
571         const char **text,
572         char *textbuf, size_t textlen )
573 {
574         Modifications *ocmod = NULL;
575
576         for( ; mods != NULL; mods = mods->sml_next ) {
577                 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
578                         if( ocmod != NULL ) {
579                                 *text = "entry has multiple objectClass attributes";
580                                 return LDAP_OBJECT_CLASS_VIOLATION;
581                         }
582                         ocmod = mods;
583                 }
584         }
585
586         if( ocmod == NULL ) {
587                 *text = "entry has no objectClass attribute";
588                 return LDAP_OBJECT_CLASS_VIOLATION;
589         }
590
591         if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
592                 *text = "objectClass attribute has no values";
593                 return LDAP_OBJECT_CLASS_VIOLATION;
594         }
595
596         return structural_class( ocmod->sml_bvalues, sc, NULL,
597                 text, textbuf, textlen );
598 }