1 /* oc.c - object class routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
27 int is_object_subclass(
33 if( sub == NULL || sup == NULL ) return 0;
36 Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
37 sup->soc_oid, sub->soc_oid, sup == sub );
44 if( sub->soc_sups == NULL ) {
48 for( i=0; sub->soc_sups[i] != NULL; i++ ) {
49 if( is_object_subclass( sup, sub->soc_sups[i] ) ) {
57 int is_entry_objectclass(
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>.
71 assert( !( e == NULL || oc == NULL ) );
72 assert( ( flags & SLAP_OCF_MASK ) != SLAP_OCF_MASK );
74 if( e == NULL || oc == NULL ) {
78 if( flags == SLAP_OCF_SET_FLAGS && ( e->e_ocflags & SLAP_OC__END ) )
80 /* flags are set, use them */
81 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
85 * find objectClass attribute
87 attr = attr_find( e->e_attrs, slap_schema.si_ad_objectClass );
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 );
98 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
99 ObjectClass *objectClass = oc_bvfind( bv );
101 if ( objectClass == NULL ) {
102 /* FIXME: is this acceptable? */
106 if ( !( flags & SLAP_OCF_SET_FLAGS ) ) {
107 if ( objectClass == oc ) {
111 if ( ( flags & SLAP_OCF_CHECK_SUP )
112 && is_object_subclass( oc, objectClass ) )
118 e->e_ocflags |= objectClass->soc_flags;
121 /* mark flags as set */
122 e->e_ocflags |= SLAP_OC__END;
124 return ( e->e_ocflags & oc->soc_flags & SLAP_OC__MASK ) != 0;
129 struct berval oir_name;
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 static ObjectClass *oc_sys_tail;
144 const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
145 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
147 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
155 const struct berval *name = v_name;
156 const struct oindexrec *oir = v_oir;
157 int i = name->bv_len - oir->oir_name.bv_len;
159 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
163 oc_find( const char *ocname )
167 bv.bv_val = (char *)ocname;
168 bv.bv_len = strlen( ocname );
170 return( oc_bvfind( &bv ) );
174 oc_bvfind( struct berval *ocname )
176 struct oindexrec *oir;
179 oir = avl_find( oc_cache, ocname, oc_index_name_cmp );
180 if ( oir ) return oir->oir_oc;
182 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
186 avl_insert( &oc_cache, (caddr_t) oir,
187 oc_index_cmp, avl_dup_error );
189 return( oir->oir_oc );
195 static LDAP_STAILQ_HEAD(OCUList, slap_object_class) oc_undef_list
196 = LDAP_STAILQ_HEAD_INITIALIZER(oc_undef_list);
199 oc_bvfind_undef( struct berval *ocname )
201 ObjectClass *oc = oc_bvfind( ocname );
207 LDAP_STAILQ_FOREACH( oc, &oc_undef_list, soc_next ) {
208 int d = oc->soc_cname.bv_len - ocname->bv_len;
214 if ( strcasecmp( oc->soc_cname.bv_val, ocname->bv_val ) == 0 ) {
223 oc = ch_malloc( sizeof( ObjectClass ) + ocname->bv_len + 1 );
224 memset( oc, 0, sizeof( ObjectClass ) );
226 oc->soc_cname.bv_len = ocname->bv_len;
227 oc->soc_cname.bv_val = (char *)&oc[ 1 ];
228 AC_MEMCPY( oc->soc_cname.bv_val, ocname->bv_val, ocname->bv_len );
230 LDAP_STAILQ_NEXT( oc, soc_next ) = NULL;
231 ldap_pvt_thread_mutex_lock( &oc_undef_mutex );
232 LDAP_STAILQ_INSERT_HEAD( &oc_undef_list, oc, soc_next );
233 ldap_pvt_thread_mutex_unlock( &oc_undef_mutex );
247 AttributeType **satp;
253 sat = at_find(*attrs1);
256 return SLAP_SCHERR_ATTR_NOT_FOUND;
259 if( is_at_operational( sat )) (*op)++;
261 if ( at_find_in_list(sat, soc->soc_required) < 0) {
262 if ( at_append_to_list(sat, &soc->soc_required) ) {
264 return SLAP_SCHERR_OUTOFMEM;
269 /* Now delete duplicates from the allowed list */
270 for ( satp = soc->soc_required; *satp; satp++ ) {
271 i = at_find_in_list(*satp, soc->soc_allowed);
273 at_delete_from_list(i, &soc->soc_allowed);
293 sat = at_find(*attrs1);
296 return SLAP_SCHERR_ATTR_NOT_FOUND;
299 if( is_at_operational( sat )) (*op)++;
301 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
302 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
303 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
305 return SLAP_SCHERR_OUTOFMEM;
328 if ( !soc->soc_sups ) {
329 /* We are at the first recursive level */
337 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
338 sizeof(ObjectClass *));
344 soc1 = oc_find(*sups1);
347 return SLAP_SCHERR_CLASS_NOT_FOUND;
350 /* check object class usage
351 * abstract classes can only sup abstract classes
352 * structural classes can not sup auxiliary classes
353 * auxiliary classes can not sup structural classes
355 if( soc->soc_kind != soc1->soc_kind
356 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
359 return SLAP_SCHERR_CLASS_BAD_SUP;
362 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
364 return SLAP_SCHERR_CLASS_BAD_SUP;
367 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
370 soc->soc_sups[nsups] = soc1;
373 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
374 if ( code ) return code;
376 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
377 if ( code ) return code;
379 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
380 if ( code ) return code;
391 oc_delete_names( ObjectClass *oc )
393 char **names = oc->soc_names;
396 struct oindexrec tmpoir, *oir;
398 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
400 oir = (struct oindexrec *)avl_delete( &oc_index,
401 (caddr_t)&tmpoir, oc_index_cmp );
402 assert( oir != NULL );
408 /* Mark the ObjectClass as deleted, remove from list, and remove all its
409 * names from the AVL tree. Leave the OID in the tree.
412 oc_delete( ObjectClass *oc )
414 oc->soc_flags |= SLAP_OC_DELETED;
416 LDAP_STAILQ_REMOVE(&oc_list,oc,slap_object_class,soc_next);
418 oc_delete_names( oc );
422 oc_clean( ObjectClass *o )
424 if (o->soc_sups) ldap_memfree(o->soc_sups);
425 if (o->soc_required) ldap_memfree(o->soc_required);
426 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
427 if (o->soc_oidmacro) ldap_memfree(o->soc_oidmacro);
431 oc_destroy_one( void *v )
433 struct oindexrec *oir = v;
434 ObjectClass *o = oir->oir_oc;
437 ldap_objectclass_free((LDAPObjectClass *)o);
446 while( !LDAP_STAILQ_EMPTY(&oc_list) ) {
447 o = LDAP_STAILQ_FIRST(&oc_list);
448 LDAP_STAILQ_REMOVE_HEAD(&oc_list, soc_next);
450 oc_delete_names( o );
453 avl_free( oc_index, oc_destroy_one );
455 while( !LDAP_STAILQ_EMPTY(&oc_undef_list) ) {
456 o = LDAP_STAILQ_FIRST(&oc_undef_list);
457 LDAP_STAILQ_REMOVE_HEAD(&oc_undef_list, soc_next);
459 ch_free( (ObjectClass *)o );
464 oc_start( ObjectClass **oc )
466 assert( oc != NULL );
468 *oc = LDAP_STAILQ_FIRST(&oc_list);
470 return (*oc != NULL);
474 oc_next( ObjectClass **oc )
476 assert( oc != NULL );
478 #if 1 /* pedantic check */
480 ObjectClass *tmp = NULL;
482 LDAP_STAILQ_FOREACH(tmp,&oc_list,soc_next) {
488 assert( tmp != NULL );
492 *oc = LDAP_STAILQ_NEXT(*oc,soc_next);
494 return (*oc != NULL);
498 * check whether the two ObjectClasses actually __are__ identical,
499 * or rather inconsistent
504 ObjectClass *new_soc )
506 if ( new_soc->soc_oid != NULL ) {
507 if ( soc->soc_oid == NULL ) {
508 return SLAP_SCHERR_CLASS_INCONSISTENT;
511 if ( strcmp( soc->soc_oid, new_soc->soc_oid ) != 0 ) {
512 return SLAP_SCHERR_CLASS_INCONSISTENT;
516 if ( soc->soc_oid != NULL ) {
517 return SLAP_SCHERR_CLASS_INCONSISTENT;
521 if ( new_soc->soc_names ) {
524 if ( soc->soc_names == NULL ) {
525 return SLAP_SCHERR_CLASS_INCONSISTENT;
528 for ( i = 0; new_soc->soc_names[ i ]; i++ ) {
529 if ( soc->soc_names[ i ] == NULL ) {
530 return SLAP_SCHERR_CLASS_INCONSISTENT;
533 if ( strcasecmp( soc->soc_names[ i ],
534 new_soc->soc_names[ i ] ) != 0 )
536 return SLAP_SCHERR_CLASS_INCONSISTENT;
540 if ( soc->soc_names != NULL ) {
541 return SLAP_SCHERR_CLASS_INCONSISTENT;
545 return SLAP_SCHERR_CLASS_DUP;
548 static struct oindexrec *oir_old;
551 oc_dup_error( void *left, void *right )
563 struct oindexrec *oir;
565 ObjectClass *soc = *roc;
567 if ( soc->soc_oid ) {
568 oir = (struct oindexrec *)
569 ch_calloc( 1, sizeof(struct oindexrec) );
570 ber_str2bv( soc->soc_oid, 0, 0, &oir->oir_name );
574 if ( avl_insert( &oc_index, (caddr_t) oir,
575 oc_index_cmp, oc_dup_error ) )
577 ObjectClass *old_soc;
582 assert( oir_old != NULL );
583 old_soc = oir_old->oir_oc;
585 /* replacing a deleted definition? */
586 if ( old_soc->soc_flags & SLAP_OC_DELETED ) {
589 /* Keep old oid, free new oid;
590 * Keep new everything else, free old
594 old_soc->soc_oid = tmp.soc_oid;
595 tmp.soc_oid = soc->soc_oid;
599 oc_destroy_one( oir );
605 rc = oc_check_dup( old_soc, soc );
612 /* FIX: temporal consistency check */
613 assert( oc_bvfind( &oir->oir_name ) != NULL );
616 if ( (names = soc->soc_names) ) {
618 oir = (struct oindexrec *)
619 ch_calloc( 1, sizeof(struct oindexrec) );
620 oir->oir_name.bv_val = *names;
621 oir->oir_name.bv_len = strlen( *names );
624 assert( oir->oir_name.bv_val != NULL );
625 assert( oir->oir_oc != NULL );
627 if ( avl_insert( &oc_index, (caddr_t) oir,
628 oc_index_cmp, avl_dup_error ) )
630 ObjectClass *old_soc;
635 old_soc = oc_bvfind( &oir->oir_name );
636 assert( old_soc != NULL );
637 rc = oc_check_dup( old_soc, soc );
641 while ( names > soc->soc_names ) {
642 struct oindexrec tmpoir;
645 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
647 oir = (struct oindexrec *)avl_delete( &oc_index,
648 (caddr_t)&tmpoir, oc_index_cmp );
649 assert( oir != NULL );
653 if ( soc->soc_oid ) {
654 struct oindexrec tmpoir;
656 ber_str2bv( soc->soc_oid, 0, 0, &tmpoir.oir_name );
658 oir = (struct oindexrec *)avl_delete( &oc_index,
659 (caddr_t)&tmpoir, oc_index_cmp );
660 assert( oir != NULL );
667 /* FIX: temporal consistency check */
668 assert( oc_bvfind(&oir->oir_name) != NULL );
673 if ( soc->soc_flags & SLAP_OC_HARDCODE ) {
678 LDAP_STAILQ_INSERT_AFTER( &oc_list, prev, soc, soc_next );
680 LDAP_STAILQ_INSERT_TAIL( &oc_list, soc, soc_next );
699 if ( oc->oc_names != NULL ) {
702 for( i=0; oc->oc_names[i]; i++ ) {
703 if( !slap_valid_descr( oc->oc_names[i] ) ) {
704 return SLAP_SCHERR_BAD_DESCR;
709 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
710 /* Expand OID macros */
711 char *oid = oidm_find( oc->oc_oid );
714 return SLAP_SCHERR_OIDM;
716 if ( oid != oc->oc_oid ) {
722 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
723 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
725 soc->soc_oidmacro = oidm;
726 if( oc->oc_names != NULL ) {
727 soc->soc_cname.bv_val = soc->soc_names[0];
729 soc->soc_cname.bv_val = soc->soc_oid;
731 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
733 if( soc->soc_sup_oids == NULL &&
734 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
736 /* structural object classes implicitly inherit from 'top' */
737 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
738 code = oc_add_sups( soc, top_oids, &op, err );
740 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
748 code = SLAP_SCHERR_CLASS_BAD_SUP;
752 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
757 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
763 code = SLAP_SCHERR_CLASS_BAD_USAGE;
768 soc->soc_flags |= SLAP_OC_HARDCODE;
771 code = oc_insert(&soc,prev,err);
774 if ( soc->soc_sups ) {
775 ch_free( soc->soc_sups );
778 if ( soc->soc_required ) {
779 ch_free( soc->soc_required );
782 if ( soc->soc_allowed ) {
783 ch_free( soc->soc_allowed );
795 oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
799 struct berval bv, *bva = NULL, idx;
803 start = LDAP_STAILQ_FIRST( &oc_list );
805 /* count the result size */
807 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
808 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) continue;
810 if ( oc == end ) break;
815 bva = ch_malloc( (num+1) * sizeof(struct berval) );
823 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
824 LDAPObjectClass loc, *locp;
825 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) continue;
826 if ( oc->soc_oidmacro ) {
827 loc = oc->soc_oclass;
828 loc.oc_oid = oc->soc_oidmacro;
831 locp = &oc->soc_oclass;
833 if ( ldap_objectclass2bv( locp, &bv ) == NULL ) {
834 ber_bvarray_free( bva );
837 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
839 bva[i].bv_len = idx.bv_len + bv.bv_len;
840 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
841 strcpy( bva[i].bv_val, ibuf );
842 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
844 bva[i].bv_val = NULL;
845 ldap_memfree( bv.bv_val );
846 if ( oc == end ) break;
852 oc_schema_info( Entry *e )
854 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
859 LDAP_STAILQ_FOREACH( oc, &oc_list, soc_next ) {
860 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
862 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
866 nval = oc->soc_cname;
869 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
870 (long) val.bv_len, val.bv_val, nval.bv_val );
873 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
876 ldap_memfree( val.bv_val );
882 register_oc( char *def, ObjectClass **soc, int dupok )
888 oc = ldap_str2objectclass( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
890 Debug( LDAP_DEBUG_ANY,
891 "register_oc: objectclass \"%s\": %s, %s\n",
892 def, ldap_scherr2str(code), err );
895 code = oc_add(oc,0,NULL,NULL,&err);
896 if ( code && ( code != SLAP_SCHERR_CLASS_DUP || !dupok )) {
897 Debug( LDAP_DEBUG_ANY,
898 "register_oc: objectclass \"%s\": %s, %s\n",
899 def, scherr2str(code), err );
900 ldap_objectclass_free(oc);
904 *soc = oc_find(oc->oc_names[0]);
906 ldap_objectclass_free(oc);