]> git.sur5r.net Git - openldap/blob - servers/slapd/oc.c
Sync with HEAD
[openldap] / servers / slapd / oc.c
1 /* oc.c - object class routines */
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 int is_object_subclass(
28         ObjectClass *sup,
29         ObjectClass *sub )
30 {
31         int i;
32
33         if( sub == NULL || sup == NULL ) return 0;
34
35 #if 0
36         Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
37                 sup->soc_oid, sub->soc_oid, sup == sub );
38 #endif
39
40         if( sup == sub ) {
41                 return 1;
42         }
43
44         if( sub->soc_sups == NULL ) {
45                 return 0;
46         }
47
48         for( i=0; sub->soc_sups[i] != NULL; i++ ) {
49                 if( is_object_subclass( sup, sub->soc_sups[i] ) ) {
50                         return 1;
51                 }
52         }
53
54         return 0;
55 }
56
57 int is_entry_objectclass(
58         Entry*  e,
59         ObjectClass *oc,
60         unsigned flags )
61 {
62         /*
63          * set_flags should only be true if oc is one of operational
64          * object classes which we support objectClass flags for
65          * (e.g., referral, alias, ...).  See <slap.h>.
66          */
67
68         Attribute *attr;
69         struct berval *bv;
70
71         assert( !( e == NULL || oc == NULL ) );
72         assert( ( flags & SLAP_OCF_MASK ) != SLAP_OCF_MASK );
73
74         if( e == NULL || oc == NULL ) {
75                 return 0;
76         }
77
78         if( flags == SLAP_OCF_SET_FLAGS && ( e->e_ocflags & SLAP_OC__END ) )
79         {
80                 /* flags are set, use them */
81                 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
82         }
83
84         /*
85          * find objectClass attribute
86          */
87         attr = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
88         if( attr == NULL ) {
89                 /* no objectClass attribute */
90                 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
91                         "no objectClass attribute\n",
92                         e->e_dn == NULL ? "" : e->e_dn,
93                         oc->soc_oclass.oc_oid, 0 );
94
95                 return 0;
96         }
97
98         for( bv=attr->a_vals; bv->bv_val; bv++ ) {
99                 ObjectClass *objectClass = oc_bvfind( bv );
100
101                 if ( objectClass == NULL ) {
102                         /* FIXME: is this acceptable? */
103                         continue;
104                 }
105
106                 if ( !( flags & SLAP_OCF_SET_FLAGS ) ) {
107                         if ( objectClass == oc ) {
108                                 return 1;
109                         }
110
111                         if ( ( flags & SLAP_OCF_CHECK_SUP )
112                                 && is_object_subclass( oc, objectClass ) )
113                         {
114                                 return 1;
115                         }
116                 }
117                 
118                 e->e_ocflags |= objectClass->soc_flags;
119         }
120
121         /* mark flags as set */
122         e->e_ocflags |= SLAP_OC__END;
123
124         return ( e->e_ocflags & oc->soc_flags & SLAP_OC__MASK ) != 0;
125 }
126
127
128 struct oindexrec {
129         struct berval oir_name;
130         ObjectClass     *oir_oc;
131 };
132
133 static Avlnode  *oc_index = NULL;
134 static Avlnode  *oc_cache = NULL;
135 static LDAP_STAILQ_HEAD(OCList, slap_object_class) oc_list
136         = LDAP_STAILQ_HEAD_INITIALIZER(oc_list);
137
138 static int
139 oc_index_cmp(
140         const void *v_oir1,
141         const void *v_oir2 )
142 {
143         const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
144         int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
145         if (i) return i;
146         return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
147 }
148
149 static int
150 oc_index_name_cmp(
151         const void *v_name,
152         const void *v_oir )
153 {
154         const struct berval    *name = v_name;
155         const struct oindexrec *oir  = v_oir;
156         int i = name->bv_len - oir->oir_name.bv_len;
157         if (i) return i;
158         return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
159 }
160
161 ObjectClass *
162 oc_find( const char *ocname )
163 {
164         struct berval bv;
165
166         bv.bv_val = (char *)ocname;
167         bv.bv_len = strlen( ocname );
168
169         return( oc_bvfind( &bv ) );
170 }
171
172 ObjectClass *
173 oc_bvfind( struct berval *ocname )
174 {
175         struct oindexrec        *oir;
176
177         if ( oc_cache ) {
178                 oir = avl_find( oc_cache, ocname, oc_index_name_cmp );
179                 if ( oir ) return oir->oir_oc;
180         }
181         oir = avl_find( oc_index, ocname, oc_index_name_cmp );
182
183         if ( oir != NULL ) {
184                 if ( at_oc_cache ) {
185                         avl_insert( &oc_cache, (caddr_t) oir,
186                                 oc_index_cmp, avl_dup_error );
187                 }
188                 return( oir->oir_oc );
189         }
190
191         return( NULL );
192 }
193
194 static LDAP_STAILQ_HEAD(OCUList, slap_object_class) oc_undef_list
195         = LDAP_STAILQ_HEAD_INITIALIZER(oc_undef_list);
196
197 ObjectClass *
198 oc_bvfind_undef( struct berval *ocname )
199 {
200         ObjectClass     *oc = oc_bvfind( ocname );
201
202         if ( oc ) {
203                 return oc;
204         }
205
206         LDAP_STAILQ_FOREACH( oc, &oc_undef_list, soc_next ) {
207                 int     d = oc->soc_cname.bv_len - ocname->bv_len;
208
209                 if ( d ) {
210                         continue;
211                 }
212
213                 if ( strcasecmp( oc->soc_cname.bv_val, ocname->bv_val ) == 0 ) {
214                         break;
215                 }
216         }
217         
218         if ( oc ) {
219                 return oc;
220         }
221         
222         oc = ch_malloc( sizeof( ObjectClass ) + ocname->bv_len + 1 );
223         memset( oc, 0, sizeof( ObjectClass ) );
224
225         oc->soc_cname.bv_len = ocname->bv_len;
226         oc->soc_cname.bv_val = (char *)&oc[ 1 ];
227         AC_MEMCPY( oc->soc_cname.bv_val, ocname->bv_val, ocname->bv_len );
228
229         LDAP_STAILQ_NEXT( oc, soc_next ) = NULL;
230         ldap_pvt_thread_mutex_lock( &oc_undef_mutex );
231         LDAP_STAILQ_INSERT_HEAD( &oc_undef_list, oc, soc_next );
232         ldap_pvt_thread_mutex_unlock( &oc_undef_mutex );
233
234         return oc;
235 }
236
237 static int
238 oc_create_required(
239         ObjectClass             *soc,
240         char                    **attrs,
241         int                     *op,
242         const char              **err )
243 {
244         char            **attrs1;
245         AttributeType   *sat;
246         AttributeType   **satp;
247         int             i;
248
249         if ( attrs ) {
250                 attrs1 = attrs;
251                 while ( *attrs1 ) {
252                         sat = at_find(*attrs1);
253                         if ( !sat ) {
254                                 *err = *attrs1;
255                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
256                         }
257
258                         if( is_at_operational( sat )) (*op)++;
259
260                         if ( at_find_in_list(sat, soc->soc_required) < 0) {
261                                 if ( at_append_to_list(sat, &soc->soc_required) ) {
262                                         *err = *attrs1;
263                                         return SLAP_SCHERR_OUTOFMEM;
264                                 }
265                         }
266                         attrs1++;
267                 }
268                 /* Now delete duplicates from the allowed list */
269                 for ( satp = soc->soc_required; *satp; satp++ ) {
270                         i = at_find_in_list(*satp, soc->soc_allowed);
271                         if ( i >= 0 ) {
272                                 at_delete_from_list(i, &soc->soc_allowed);
273                         }
274                 }
275         }
276         return 0;
277 }
278
279 static int
280 oc_create_allowed(
281     ObjectClass         *soc,
282     char                **attrs,
283         int                     *op,
284     const char          **err )
285 {
286         char            **attrs1;
287         AttributeType   *sat;
288
289         if ( attrs ) {
290                 attrs1 = attrs;
291                 while ( *attrs1 ) {
292                         sat = at_find(*attrs1);
293                         if ( !sat ) {
294                                 *err = *attrs1;
295                                 return SLAP_SCHERR_ATTR_NOT_FOUND;
296                         }
297
298                         if( is_at_operational( sat )) (*op)++;
299
300                         if ( at_find_in_list(sat, soc->soc_required) < 0 &&
301                              at_find_in_list(sat, soc->soc_allowed) < 0 ) {
302                                 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
303                                         *err = *attrs1;
304                                         return SLAP_SCHERR_OUTOFMEM;
305                                 }
306                         }
307                         attrs1++;
308                 }
309         }
310         return 0;
311 }
312
313 static int
314 oc_add_sups(
315         ObjectClass             *soc,
316         char                    **sups,
317         int                     *op,
318         const char              **err )
319 {
320         int             code;
321         ObjectClass     *soc1;
322         int             nsups;
323         char    **sups1;
324         int             add_sups = 0;
325
326         if ( sups ) {
327                 if ( !soc->soc_sups ) {
328                         /* We are at the first recursive level */
329                         add_sups = 1;
330                         nsups = 1;
331                         sups1 = sups;
332                         while ( *sups1 ) {
333                                 nsups++;
334                                 sups1++;
335                         }
336                         soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
337                                           sizeof(ObjectClass *));
338                 }
339
340                 nsups = 0;
341                 sups1 = sups;
342                 while ( *sups1 ) {
343                         soc1 = oc_find(*sups1);
344                         if ( !soc1 ) {
345                                 *err = *sups1;
346                                 return SLAP_SCHERR_CLASS_NOT_FOUND;
347                         }
348
349                         /* check object class usage
350                          * abstract classes can only sup abstract classes 
351                          * structural classes can not sup auxiliary classes
352                          * auxiliary classes can not sup structural classes
353                          */
354                         if( soc->soc_kind != soc1->soc_kind
355                                 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
356                         {
357                                 *err = *sups1;
358                                 return SLAP_SCHERR_CLASS_BAD_SUP;
359                         }
360
361                         if( soc1->soc_obsolete && !soc->soc_obsolete ) {
362                                 *err = *sups1;
363                                 return SLAP_SCHERR_CLASS_BAD_SUP;
364                         }
365
366                         if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
367
368                         if ( add_sups ) {
369                                 soc->soc_sups[nsups] = soc1;
370                         }
371
372                         code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
373                         if ( code ) return code;
374
375                         code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
376                         if ( code ) return code;
377
378                         code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
379                         if ( code ) return code;
380
381                         nsups++;
382                         sups1++;
383                 }
384         }
385
386         return 0;
387 }
388
389 void
390 oc_destroy( void )
391 {
392         ObjectClass *o;
393
394         avl_free(oc_index, ldap_memfree);
395         while( !LDAP_STAILQ_EMPTY(&oc_list) ) {
396                 o = LDAP_STAILQ_FIRST(&oc_list);
397                 LDAP_STAILQ_REMOVE_HEAD(&oc_list, soc_next);
398
399                 if (o->soc_sups) ldap_memfree(o->soc_sups);
400                 if (o->soc_required) ldap_memfree(o->soc_required);
401                 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
402                 if (o->soc_oidmacro) ldap_memfree(o->soc_oidmacro);
403                 ldap_objectclass_free((LDAPObjectClass *)o);
404         }
405         
406         while( !LDAP_STAILQ_EMPTY(&oc_undef_list) ) {
407                 o = LDAP_STAILQ_FIRST(&oc_undef_list);
408                 LDAP_STAILQ_REMOVE_HEAD(&oc_undef_list, soc_next);
409
410                 ch_free( (ObjectClass *)o );
411         }
412 }
413
414 /*
415  * check whether the two ObjectClasses actually __are__ identical,
416  * or rather inconsistent
417  */
418 static int
419 oc_check_dup(
420         ObjectClass     *soc,
421         ObjectClass     *new_soc )
422 {
423         if ( new_soc->soc_oid != NULL ) {
424                 if ( soc->soc_oid == NULL ) {
425                         return SLAP_SCHERR_CLASS_INCONSISTENT;
426                 }
427
428                 if ( strcmp( soc->soc_oid, new_soc->soc_oid ) != 0 ) {
429                         return SLAP_SCHERR_CLASS_INCONSISTENT;
430                 }
431
432         } else {
433                 if ( soc->soc_oid != NULL ) {
434                         return SLAP_SCHERR_CLASS_INCONSISTENT;
435                 }
436         }
437
438         if ( new_soc->soc_names ) {
439                 int     i;
440
441                 if ( soc->soc_names == NULL ) {
442                         return SLAP_SCHERR_CLASS_INCONSISTENT;
443                 }
444
445                 for ( i = 0; new_soc->soc_names[ i ]; i++ ) {
446                         if ( soc->soc_names[ i ] == NULL ) {
447                                 return SLAP_SCHERR_CLASS_INCONSISTENT;
448                         }
449                         
450                         if ( strcasecmp( soc->soc_names[ i ],
451                                         new_soc->soc_names[ i ] ) != 0 )
452                         {
453                                 return SLAP_SCHERR_CLASS_INCONSISTENT;
454                         }
455                 }
456         } else {
457                 if ( soc->soc_names != NULL ) {
458                         return SLAP_SCHERR_CLASS_INCONSISTENT;
459                 }
460         }
461
462         return SLAP_SCHERR_CLASS_DUP;
463 }
464
465 static int
466 oc_insert(
467     ObjectClass         *soc,
468     const char          **err )
469 {
470         struct oindexrec        *oir;
471         char                    **names;
472
473         if ( soc->soc_oid ) {
474                 oir = (struct oindexrec *)
475                         ch_calloc( 1, sizeof(struct oindexrec) );
476                 oir->oir_name.bv_val = soc->soc_oid;
477                 oir->oir_name.bv_len = strlen( soc->soc_oid );
478                 oir->oir_oc = soc;
479
480                 assert( oir->oir_name.bv_val != NULL );
481                 assert( oir->oir_oc != NULL );
482
483                 if ( avl_insert( &oc_index, (caddr_t) oir,
484                         oc_index_cmp, avl_dup_error ) )
485                 {
486                         ObjectClass     *old_soc;
487                         int             rc;
488
489                         *err = soc->soc_oid;
490
491                         old_soc = oc_bvfind( &oir->oir_name );
492                         assert( old_soc != NULL );
493                         rc = oc_check_dup( old_soc, soc );
494
495                         ldap_memfree( oir );
496                         return rc;
497                 }
498
499                 /* FIX: temporal consistency check */
500                 assert( oc_bvfind( &oir->oir_name ) != NULL );
501         }
502
503         if ( (names = soc->soc_names) ) {
504                 while ( *names ) {
505                         oir = (struct oindexrec *)
506                                 ch_calloc( 1, sizeof(struct oindexrec) );
507                         oir->oir_name.bv_val = *names;
508                         oir->oir_name.bv_len = strlen( *names );
509                         oir->oir_oc = soc;
510
511                         assert( oir->oir_name.bv_val != NULL );
512                         assert( oir->oir_oc != NULL );
513
514                         if ( avl_insert( &oc_index, (caddr_t) oir,
515                                 oc_index_cmp, avl_dup_error ) )
516                         {
517                                 ObjectClass     *old_soc;
518                                 int             rc;
519
520                                 *err = *names;
521
522                                 old_soc = oc_bvfind( &oir->oir_name );
523                                 assert( old_soc != NULL );
524                                 rc = oc_check_dup( old_soc, soc );
525
526                                 ldap_memfree( oir );
527                                 return rc;
528                         }
529
530                         /* FIX: temporal consistency check */
531                         assert( oc_bvfind(&oir->oir_name) != NULL );
532
533                         names++;
534                 }
535         }
536         LDAP_STAILQ_INSERT_TAIL( &oc_list, soc, soc_next );
537
538         return 0;
539 }
540
541 int
542 oc_add(
543     LDAPObjectClass     *oc,
544         int user,
545         ObjectClass             **rsoc,
546     const char          **err )
547 {
548         ObjectClass     *soc;
549         int             code;
550         int             op = 0;
551         char    *oidm = NULL;
552
553         if ( oc->oc_names != NULL ) {
554                 int i;
555
556                 for( i=0; oc->oc_names[i]; i++ ) {
557                         if( !slap_valid_descr( oc->oc_names[i] ) ) {
558                                 return SLAP_SCHERR_BAD_DESCR;
559                         }
560                 }
561         }
562
563         if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
564                 /* Expand OID macros */
565                 char *oid = oidm_find( oc->oc_oid );
566                 if ( !oid ) {
567                         *err = oc->oc_oid;
568                         return SLAP_SCHERR_OIDM;
569                 }
570                 if ( oid != oc->oc_oid ) {
571                         oidm = oc->oc_oid;
572                         oc->oc_oid = oid;
573                 }
574         }
575
576         soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
577         AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
578
579         soc->soc_oidmacro = oidm;
580         if( oc->oc_names != NULL ) {
581                 soc->soc_cname.bv_val = soc->soc_names[0];
582         } else {
583                 soc->soc_cname.bv_val = soc->soc_oid;
584         }
585         soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
586
587         if( soc->soc_sup_oids == NULL &&
588                 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
589         {
590                 /* structural object classes implicitly inherit from 'top' */
591                 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
592                 code = oc_add_sups( soc, top_oids, &op, err );
593         } else {
594                 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
595         }
596
597         if ( code != 0 ) {
598                 goto done;
599         }
600
601         if ( user && op ) {
602                 code = SLAP_SCHERR_CLASS_BAD_SUP;
603                 goto done;
604         }
605
606         code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
607         if ( code != 0 ) {
608                 goto done;
609         }
610
611         code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
612         if ( code != 0 ) {
613                 goto done;
614         }
615
616         if ( user && op ) {
617                 code = SLAP_SCHERR_CLASS_BAD_USAGE;
618                 goto done;
619         }
620
621         if ( !user ) {
622                 soc->soc_flags |= SLAP_OC_HARDCODE;
623         }
624
625         code = oc_insert(soc,err);
626 done:;
627         if ( code != 0 ) {
628                 if ( soc->soc_sups ) {
629                         ch_free( soc->soc_sups );
630                 }
631
632                 if ( soc->soc_required ) {
633                         ch_free( soc->soc_required );
634                 }
635
636                 if ( soc->soc_allowed ) {
637                         ch_free( soc->soc_allowed );
638                 }
639
640                 ch_free( soc );
641
642         } else if ( rsoc ) {
643                 *rsoc = soc;
644         }
645         return code;
646 }
647
648 void
649 oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
650 {
651         ObjectClass *oc;
652         int i, num;
653         struct berval bv, *bva = NULL, idx;
654         char ibuf[32];
655
656         if ( !start )
657                 start = LDAP_STAILQ_FIRST( &oc_list );
658
659         /* count the result size */
660         i = 0;
661         for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
662                 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) continue;
663                 i++;
664                 if ( oc == end ) break;
665         }
666         if (!i) return;
667
668         num = i;
669         bva = ch_malloc( (num+1) * sizeof(struct berval) );
670         BER_BVZERO( bva );
671         idx.bv_val = ibuf;
672         if ( sys ) {
673                 idx.bv_len = 0;
674                 ibuf[0] = '\0';
675         }
676         i = 0;
677         for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
678                 LDAPObjectClass loc, *locp;
679                 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) continue;
680                 if ( oc->soc_oidmacro ) {
681                         loc = oc->soc_oclass;
682                         loc.oc_oid = oc->soc_oidmacro;
683                         locp = &loc;
684                 } else {
685                         locp = &oc->soc_oclass;
686                 }
687                 if ( ldap_objectclass2bv( locp, &bv ) == NULL ) {
688                         ber_bvarray_free( bva );
689                 }
690                 if ( !sys ) {
691                         idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
692                 }
693                 bva[i].bv_len = idx.bv_len + bv.bv_len;
694                 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
695                 strcpy( bva[i].bv_val, ibuf );
696                 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
697                 i++;
698                 bva[i].bv_val = NULL;
699                 ldap_memfree( bv.bv_val );
700                 if ( oc == end ) break;
701         }
702         *res = bva;
703 }
704
705 int
706 oc_schema_info( Entry *e )
707 {
708         AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
709         ObjectClass     *oc;
710         struct berval   val;
711         struct berval   nval;
712
713         LDAP_STAILQ_FOREACH( oc, &oc_list, soc_next ) {
714                 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
715
716                 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
717                         return -1;
718                 }
719
720                 nval = oc->soc_cname;
721
722 #if 0
723                 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
724                (long) val.bv_len, val.bv_val, nval.bv_val );
725 #endif
726
727                 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
728                         return -1;
729                 }
730                 ldap_memfree( val.bv_val );
731         }
732         return 0;
733 }