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