]> git.sur5r.net Git - openldap/blob - servers/slapd/schema_check.c
sync with HEAD
[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( OPERATION, INFO, 
90                                 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0 );
91 #else
92                         Debug( LDAP_DEBUG_ANY,
93                             "Entry (%s), %s\n",
94                             e->e_dn, textbuf, 0 );
95 #endif
96
97                         return LDAP_CONSTRAINT_VIOLATION;
98                 }
99         }
100
101         /* it's a REALLY bad idea to disable schema checks */
102         if( !global_schemacheck ) return LDAP_SUCCESS;
103
104         /* find the object class attribute - could error out here */
105         asc = attr_find( e->e_attrs, ad_structuralObjectClass );
106         if ( asc == NULL ) {
107 #ifdef NEW_LOGGING
108                 LDAP_LOG( OPERATION, INFO, 
109                         "entry_schema_check: No structuralObjectClass for entry (%s)\n", 
110                         e->e_dn, 0, 0 );
111 #else
112                 Debug( LDAP_DEBUG_ANY,
113                         "No structuralObjectClass for entry (%s)\n",
114                     e->e_dn, 0, 0 );
115 #endif
116
117                 *text = "no structuralObjectClass operational attribute";
118                 return LDAP_OBJECT_CLASS_VIOLATION;
119         }
120
121         assert( asc->a_vals != NULL );
122         assert( asc->a_vals[0].bv_val != NULL );
123         assert( asc->a_vals[1].bv_val == NULL );
124
125         sc = oc_bvfind( &asc->a_vals[0] );
126         if( sc == NULL ) {
127                 snprintf( textbuf, textlen, 
128                         "unrecognized structuralObjectClass '%s'",
129                         asc->a_vals[0].bv_val );
130
131 #ifdef NEW_LOGGING
132                 LDAP_LOG( OPERATION, INFO, 
133                         "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
134 #else
135                 Debug( LDAP_DEBUG_ANY,
136                         "entry_check_schema(%s): %s\n",
137                         e->e_dn, textbuf, 0 );
138 #endif
139
140                 return LDAP_OBJECT_CLASS_VIOLATION;
141         }
142
143         if( sc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
144                 snprintf( textbuf, textlen, 
145                         "structuralObjectClass '%s' is not STRUCTURAL",
146                         asc->a_vals[0].bv_val );
147
148 #ifdef NEW_LOGGING
149                 LDAP_LOG( OPERATION, INFO, 
150                         "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
151 #else
152                 Debug( LDAP_DEBUG_ANY,
153                         "entry_check_schema(%s): %s\n",
154                         e->e_dn, textbuf, 0 );
155 #endif
156
157                 return LDAP_OBJECT_CLASS_VIOLATION;
158         }
159
160         /* find the object class attribute */
161         aoc = attr_find( e->e_attrs, ad_objectClass );
162         if ( aoc == NULL ) {
163 #ifdef NEW_LOGGING
164                 LDAP_LOG( OPERATION, INFO, 
165                         "entry_schema_check: No objectClass for entry (%s).\n", 
166                         e->e_dn, 0, 0 );
167 #else
168                 Debug( LDAP_DEBUG_ANY, "No objectClass for entry (%s)\n",
169                     e->e_dn, 0, 0 );
170 #endif
171
172                 *text = "no objectClass attribute";
173                 return LDAP_OBJECT_CLASS_VIOLATION;
174         }
175
176         assert( aoc->a_vals != NULL );
177         assert( aoc->a_vals[0].bv_val != NULL );
178
179         rc = structural_class( aoc->a_vals, &nsc, &oc, text, textbuf, textlen );
180         if( rc != LDAP_SUCCESS ) {
181                 return rc;
182         } else if ( nsc.bv_len == 0 ) {
183                 return LDAP_OBJECT_CLASS_VIOLATION;
184         }
185
186         *text = textbuf;
187
188         if ( oc == NULL ) {
189                 snprintf( textbuf, textlen, 
190                         "unrecognized objectClass '%s'",
191                         aoc->a_vals[0].bv_val );
192                 return LDAP_OBJECT_CLASS_VIOLATION;
193
194         } else if ( sc != oc ) {
195                 snprintf( textbuf, textlen, 
196                         "structuralObjectClass modification from '%s' to '%s' not allowed",
197                         asc->a_vals[0].bv_val, nsc.bv_val );
198                 return LDAP_NO_OBJECT_CLASS_MODS;
199         }
200
201         /* check that the entry has required attrs for each oc */
202         for ( i = 0; aoc->a_vals[i].bv_val != NULL; i++ ) {
203                 if ( (oc = oc_bvfind( &aoc->a_vals[i] )) == NULL ) {
204                         snprintf( textbuf, textlen, 
205                                 "unrecognized objectClass '%s'",
206                                 aoc->a_vals[i].bv_val );
207
208 #ifdef NEW_LOGGING
209                         LDAP_LOG( OPERATION, INFO, 
210                                 "entry_schema_check: dn (%s), %s\n", e->e_dn, textbuf, 0 );
211 #else
212                         Debug( LDAP_DEBUG_ANY,
213                                 "entry_check_schema(%s): %s\n",
214                                 e->e_dn, textbuf, 0 );
215 #endif
216
217                         return LDAP_OBJECT_CLASS_VIOLATION;
218                 }
219
220                 if ( oc->soc_check ) {
221                         int rc = (oc->soc_check)( be, e, oc,
222                                 text, textbuf, textlen );
223                         if( rc != LDAP_SUCCESS ) {
224                                 return rc;
225                         }
226                 }
227
228                 if ( oc->soc_kind == LDAP_SCHEMA_ABSTRACT ) {
229                         /* object class is abstract */
230                         if ( oc != slap_schema.si_oc_top &&
231                                 !is_object_subclass( oc, sc ))
232                         {
233                                 int j;
234                                 ObjectClass *xc = NULL;
235                                 for( j=0; aoc->a_vals[j].bv_val; j++ ) {
236                                         if( i != j ) {
237                                                 xc = oc_bvfind( &aoc->a_vals[i] );
238                                                 if( xc == NULL ) {
239                                                         snprintf( textbuf, textlen, 
240                                                                 "unrecognized objectClass '%s'",
241                                                                 aoc->a_vals[i].bv_val );
242
243 #ifdef NEW_LOGGING
244                                                         LDAP_LOG( OPERATION, INFO, 
245                                                                 "entry_schema_check: dn (%s), %s\n",
246                                                                 e->e_dn, textbuf, 0 );
247 #else
248                                                         Debug( LDAP_DEBUG_ANY,
249                                                                 "entry_check_schema(%s): %s\n",
250                                                                 e->e_dn, textbuf, 0 );
251 #endif
252
253                                                         return LDAP_OBJECT_CLASS_VIOLATION;
254                                                 }
255
256                                                 /* since we previous check against the
257                                                  * structural object of this entry, the
258                                                  * abstract class must be a (direct or indirect)
259                                                  * superclass of one of the auxiliary classes of
260                                                  * the entry.
261                                                  */
262                                                 if ( xc->soc_kind == LDAP_SCHEMA_AUXILIARY &&
263                                                         is_object_subclass( oc, xc ) )
264                                                 {
265                                                         break;;
266                                                 }
267
268                                                 xc = NULL;
269                                         }
270                                 }
271
272                                 if( xc == NULL ) {
273                                         snprintf( textbuf, textlen, "instanstantiation of "
274                                                 "abstract objectClass '%s' not allowed",
275                                                 aoc->a_vals[i].bv_val );
276
277 #ifdef NEW_LOGGING
278                                         LDAP_LOG( OPERATION, INFO, 
279                                                 "entry_schema_check: dn (%s), %s\n", 
280                                                 e->e_dn, textbuf, 0 );
281 #else
282                                         Debug( LDAP_DEBUG_ANY,
283                                                 "entry_check_schema(%s): %s\n",
284                                                 e->e_dn, textbuf, 0 );
285 #endif
286
287                                         return LDAP_OBJECT_CLASS_VIOLATION;
288                                 }
289                         }
290
291                 } else if ( oc->soc_kind != LDAP_SCHEMA_STRUCTURAL || oc == sc ) {
292                         char *s = oc_check_required( e, oc, &aoc->a_vals[i] );
293
294                         if (s != NULL) {
295                                 snprintf( textbuf, textlen, 
296                                         "object class '%s' requires attribute '%s'",
297                                         aoc->a_vals[i].bv_val, s );
298
299 #ifdef NEW_LOGGING
300                                 LDAP_LOG( OPERATION, INFO, 
301                                         "entry_schema_check: dn=\"%s\" %s", e->e_dn, textbuf, 0 );
302 #else
303                                 Debug( LDAP_DEBUG_ANY,
304                                         "Entry (%s): %s\n",
305                                         e->e_dn, textbuf, 0 );
306 #endif
307
308                                 return LDAP_OBJECT_CLASS_VIOLATION;
309                         }
310
311                         if( oc == slap_schema.si_oc_extensibleObject ) {
312                                 extensible=1;
313                         }
314                 }
315         }
316
317         if( extensible ) {
318                 return LDAP_SUCCESS;
319         }
320
321         /* check that each attr in the entry is allowed by some oc */
322         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
323                 int ret = oc_check_allowed( a->a_desc->ad_type, aoc->a_vals, sc );
324                 if ( ret != LDAP_SUCCESS ) {
325                         char *type = a->a_desc->ad_cname.bv_val;
326
327                         snprintf( textbuf, textlen, 
328                                 "attribute '%s' not allowed",
329                                 type );
330
331 #ifdef NEW_LOGGING
332                         LDAP_LOG( OPERATION, INFO, 
333                                 "entry_schema_check: dn=\"%s\" %s\n", e->e_dn, textbuf, 0);
334 #else
335                         Debug( LDAP_DEBUG_ANY,
336                             "Entry (%s), %s\n",
337                             e->e_dn, textbuf, 0 );
338 #endif
339
340                         return ret;
341                 }
342         }
343
344         return LDAP_SUCCESS;
345 }
346
347 static char *
348 oc_check_required(
349         Entry *e,
350         ObjectClass *oc,
351         struct berval *ocname )
352 {
353         AttributeType   *at;
354         int             i;
355         Attribute       *a;
356
357 #ifdef NEW_LOGGING
358         LDAP_LOG( OPERATION, ENTRY, 
359                 "oc_check_required: dn (%s), objectClass \"%s\"\n", 
360                 e->e_dn, ocname->bv_val, 0 );
361 #else
362         Debug( LDAP_DEBUG_TRACE,
363                 "oc_check_required entry (%s), objectClass \"%s\"\n",
364                 e->e_dn, ocname->bv_val, 0 );
365 #endif
366
367
368         /* check for empty oc_required */
369         if(oc->soc_required == NULL) {
370                 return NULL;
371         }
372
373         /* for each required attribute */
374         for ( i = 0; oc->soc_required[i] != NULL; i++ ) {
375                 at = oc->soc_required[i];
376                 /* see if it's in the entry */
377                 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
378                         if( a->a_desc->ad_type == at ) {
379                                 break;
380                         }
381                 }
382                 /* not there => schema violation */
383                 if ( a == NULL ) {
384                         return at->sat_cname.bv_val;
385                 }
386         }
387
388         return( NULL );
389 }
390
391 int oc_check_allowed(
392         AttributeType *at,
393         BerVarray ocl,
394         ObjectClass *sc )
395 {
396         int             i, j;
397
398 #ifdef NEW_LOGGING
399         LDAP_LOG( OPERATION, ENTRY, 
400                 "oc_check_allowed: type \"%s\"\n", at->sat_cname.bv_val, 0, 0 );
401 #else
402         Debug( LDAP_DEBUG_TRACE,
403                 "oc_check_allowed type \"%s\"\n",
404                 at->sat_cname.bv_val, 0, 0 );
405 #endif
406
407         /* always allow objectClass attribute */
408         if ( strcasecmp( at->sat_cname.bv_val, "objectClass" ) == 0 ) {
409                 return LDAP_SUCCESS;
410         }
411
412         /*
413          * All operational attributions are allowed by schema rules.
414          */
415         if( is_at_operational(at) ) {
416                 return LDAP_SUCCESS;
417         }
418
419         /* check to see if its allowed by the structuralObjectClass */
420         if( sc ) {
421                 /* does it require the type? */
422                 for ( j = 0; sc->soc_required != NULL && 
423                         sc->soc_required[j] != NULL; j++ )
424                 {
425                         if( at == sc->soc_required[j] ) {
426                                 return LDAP_SUCCESS;
427                         }
428                 }
429
430                 /* does it allow the type? */
431                 for ( j = 0; sc->soc_allowed != NULL && 
432                         sc->soc_allowed[j] != NULL; j++ )
433                 {
434                         if( at == sc->soc_allowed[j] ) {
435                                 return LDAP_SUCCESS;
436                         }
437                 }
438         }
439
440         /* check that the type appears as req or opt in at least one oc */
441         for ( i = 0; ocl[i].bv_val != NULL; i++ ) {
442                 /* if we know about the oc */
443                 ObjectClass     *oc = oc_bvfind( &ocl[i] );
444                 if ( oc != NULL && oc->soc_kind != LDAP_SCHEMA_ABSTRACT &&
445                         ( sc == NULL || oc->soc_kind == LDAP_SCHEMA_AUXILIARY ))
446                 {
447                         /* does it require the type? */
448                         for ( j = 0; oc->soc_required != NULL && 
449                                 oc->soc_required[j] != NULL; j++ )
450                         {
451                                 if( at == oc->soc_required[j] ) {
452                                         return LDAP_SUCCESS;
453                                 }
454                         }
455                         /* does it allow the type? */
456                         for ( j = 0; oc->soc_allowed != NULL && 
457                                 oc->soc_allowed[j] != NULL; j++ )
458                         {
459                                 if( at == oc->soc_allowed[j] ) {
460                                         return LDAP_SUCCESS;
461                                 }
462                         }
463                 }
464         }
465
466         /* not allowed by any oc */
467         return LDAP_OBJECT_CLASS_VIOLATION;
468 }
469
470 /*
471  * Determine the structural object class from a set of OIDs
472  */
473 int structural_class(
474         BerVarray ocs,
475         struct berval *scbv,
476         ObjectClass **scp,
477         const char **text,
478         char *textbuf, size_t textlen )
479 {
480         int i;
481         ObjectClass *oc;
482         ObjectClass *sc = NULL;
483         int scn = -1;
484
485         *text = "structural_class: internal error";
486         scbv->bv_len = 0;
487
488         for( i=0; ocs[i].bv_val; i++ ) {
489                 oc = oc_bvfind( &ocs[i] );
490
491                 if( oc == NULL ) {
492                         snprintf( textbuf, textlen,
493                                 "unrecognized objectClass '%s'",
494                                 ocs[i].bv_val );
495                         *text = textbuf;
496                         return LDAP_OBJECT_CLASS_VIOLATION;
497                 }
498
499                 if( oc->soc_kind == LDAP_SCHEMA_STRUCTURAL ) {
500                         if( sc == NULL || is_object_subclass( sc, oc ) ) {
501                                 sc = oc;
502                                 scn = i;
503
504                         } else if ( !is_object_subclass( oc, sc ) ) {
505                                 int j;
506                                 ObjectClass *xc = NULL;
507
508                                 /* find common superior */
509                                 for( j=i+1; ocs[j].bv_val; j++ ) {
510                                         xc = oc_bvfind( &ocs[j] );
511
512                                         if( xc == NULL ) {
513                                                 snprintf( textbuf, textlen,
514                                                         "unrecognized objectClass '%s'",
515                                                         ocs[i].bv_val );
516                                                 *text = textbuf;
517                                                 return LDAP_OBJECT_CLASS_VIOLATION;
518                                         }
519
520                                         if( xc->soc_kind != LDAP_SCHEMA_STRUCTURAL ) {
521                                                 xc = NULL;
522                                                 continue;
523                                         }
524
525                                         if( is_object_subclass( sc, xc ) &&
526                                                 is_object_subclass( oc, xc ) )
527                                         {
528                                                 /* found common subclass */
529                                                 break;
530                                         }
531
532                                         xc = NULL;
533                                 }
534
535                                 if( xc == NULL ) {
536                                         /* no common subclass */
537                                         snprintf( textbuf, textlen,
538                                                 "invalid structural object class chain (%s/%s)",
539                                                 ocs[scn].bv_val, ocs[i].bv_val );
540                                         *text = textbuf;
541                                         return LDAP_OBJECT_CLASS_VIOLATION;
542                                 }
543                         }
544                 }
545         }
546
547         if( scp )
548                 *scp = sc;
549
550         if( sc == NULL ) {
551                 *text = "no structural object classes provided";
552                 return LDAP_OBJECT_CLASS_VIOLATION;
553         }
554
555         *scbv = ocs[scn];
556         return LDAP_SUCCESS;
557 }
558
559 /*
560  * Return structural object class from list of modifications
561  */
562 int mods_structural_class(
563         Modifications *mods,
564         struct berval *sc,
565         const char **text,
566         char *textbuf, size_t textlen )
567 {
568         Modifications *ocmod = NULL;
569
570         for( ; mods != NULL; mods = mods->sml_next ) {
571                 if( mods->sml_desc == slap_schema.si_ad_objectClass ) {
572                         if( ocmod != NULL ) {
573                                 *text = "entry has multiple objectClass attributes";
574                                 return LDAP_OBJECT_CLASS_VIOLATION;
575                         }
576                         ocmod = mods;
577                 }
578         }
579
580         if( ocmod == NULL ) {
581                 *text = "entry has no objectClass attribute";
582                 return LDAP_OBJECT_CLASS_VIOLATION;
583         }
584
585         if( ocmod->sml_bvalues == NULL || ocmod->sml_bvalues[0].bv_val == NULL ) {
586                 *text = "objectClass attribute has no values";
587                 return LDAP_OBJECT_CLASS_VIOLATION;
588         }
589
590         return structural_class( ocmod->sml_bvalues, sc, NULL,
591                 text, textbuf, textlen );
592 }