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);
143 const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
144 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
146 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
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;
158 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
162 oc_find( const char *ocname )
166 bv.bv_val = (char *)ocname;
167 bv.bv_len = strlen( ocname );
169 return( oc_bvfind( &bv ) );
173 oc_bvfind( struct berval *ocname )
175 struct oindexrec *oir;
178 oir = avl_find( oc_cache, ocname, oc_index_name_cmp );
179 if ( oir ) return oir->oir_oc;
181 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
185 avl_insert( &oc_cache, (caddr_t) oir,
186 oc_index_cmp, avl_dup_error );
188 return( oir->oir_oc );
194 static LDAP_STAILQ_HEAD(OCUList, slap_object_class) oc_undef_list
195 = LDAP_STAILQ_HEAD_INITIALIZER(oc_undef_list);
198 oc_bvfind_undef( struct berval *ocname )
200 ObjectClass *oc = oc_bvfind( ocname );
206 LDAP_STAILQ_FOREACH( oc, &oc_undef_list, soc_next ) {
207 int d = oc->soc_cname.bv_len - ocname->bv_len;
213 if ( strcasecmp( oc->soc_cname.bv_val, ocname->bv_val ) == 0 ) {
222 oc = ch_malloc( sizeof( ObjectClass ) + ocname->bv_len + 1 );
223 memset( oc, 0, sizeof( ObjectClass ) );
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 );
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 );
246 AttributeType **satp;
252 sat = at_find(*attrs1);
255 return SLAP_SCHERR_ATTR_NOT_FOUND;
258 if( is_at_operational( sat )) (*op)++;
260 if ( at_find_in_list(sat, soc->soc_required) < 0) {
261 if ( at_append_to_list(sat, &soc->soc_required) ) {
263 return SLAP_SCHERR_OUTOFMEM;
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);
272 at_delete_from_list(i, &soc->soc_allowed);
292 sat = at_find(*attrs1);
295 return SLAP_SCHERR_ATTR_NOT_FOUND;
298 if( is_at_operational( sat )) (*op)++;
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) ) {
304 return SLAP_SCHERR_OUTOFMEM;
327 if ( !soc->soc_sups ) {
328 /* We are at the first recursive level */
336 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
337 sizeof(ObjectClass *));
343 soc1 = oc_find(*sups1);
346 return SLAP_SCHERR_CLASS_NOT_FOUND;
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
354 if( soc->soc_kind != soc1->soc_kind
355 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
358 return SLAP_SCHERR_CLASS_BAD_SUP;
361 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
363 return SLAP_SCHERR_CLASS_BAD_SUP;
366 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
369 soc->soc_sups[nsups] = soc1;
372 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
373 if ( code ) return code;
375 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
376 if ( code ) return code;
378 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
379 if ( code ) return code;
390 oc_delete_names( ObjectClass *oc )
392 char **names = oc->soc_names;
395 struct oindexrec tmpoir, *oir;
397 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
399 oir = (struct oindexrec *)avl_delete( &oc_index,
400 (caddr_t)&tmpoir, oc_index_cmp );
401 assert( oir != NULL );
407 /* Mark the ObjectClass as deleted, remove from list, and remove all its
408 * names from the AVL tree. Leave the OID in the tree.
411 oc_delete( ObjectClass *oc )
413 oc->soc_flags |= SLAP_OC_DELETED;
415 LDAP_STAILQ_REMOVE(&oc_list,oc,slap_object_class,soc_next);
417 oc_delete_names( oc );
421 oc_clean( ObjectClass *o )
423 if (o->soc_sups) ldap_memfree(o->soc_sups);
424 if (o->soc_required) ldap_memfree(o->soc_required);
425 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
426 if (o->soc_oidmacro) ldap_memfree(o->soc_oidmacro);
430 oc_destroy_one( void *v )
432 struct oindexrec *oir = v;
433 ObjectClass *o = oir->oir_oc;
436 ldap_objectclass_free((LDAPObjectClass *)o);
445 while( !LDAP_STAILQ_EMPTY(&oc_list) ) {
446 o = LDAP_STAILQ_FIRST(&oc_list);
447 LDAP_STAILQ_REMOVE_HEAD(&oc_list, soc_next);
449 oc_delete_names( o );
452 avl_free( oc_index, oc_destroy_one );
454 while( !LDAP_STAILQ_EMPTY(&oc_undef_list) ) {
455 o = LDAP_STAILQ_FIRST(&oc_undef_list);
456 LDAP_STAILQ_REMOVE_HEAD(&oc_undef_list, soc_next);
458 ch_free( (ObjectClass *)o );
463 oc_start( ObjectClass **oc )
465 assert( oc != NULL );
467 *oc = LDAP_STAILQ_FIRST(&oc_list);
469 return (*oc != NULL);
473 oc_next( ObjectClass **oc )
475 assert( oc != NULL );
477 #if 1 /* pedantic check */
479 ObjectClass *tmp = NULL;
481 LDAP_STAILQ_FOREACH(tmp,&oc_list,soc_next) {
487 assert( tmp != NULL );
491 *oc = LDAP_STAILQ_NEXT(*oc,soc_next);
493 return (*oc != NULL);
497 * check whether the two ObjectClasses actually __are__ identical,
498 * or rather inconsistent
503 ObjectClass *new_soc )
505 if ( new_soc->soc_oid != NULL ) {
506 if ( soc->soc_oid == NULL ) {
507 return SLAP_SCHERR_CLASS_INCONSISTENT;
510 if ( strcmp( soc->soc_oid, new_soc->soc_oid ) != 0 ) {
511 return SLAP_SCHERR_CLASS_INCONSISTENT;
515 if ( soc->soc_oid != NULL ) {
516 return SLAP_SCHERR_CLASS_INCONSISTENT;
520 if ( new_soc->soc_names ) {
523 if ( soc->soc_names == NULL ) {
524 return SLAP_SCHERR_CLASS_INCONSISTENT;
527 for ( i = 0; new_soc->soc_names[ i ]; i++ ) {
528 if ( soc->soc_names[ i ] == NULL ) {
529 return SLAP_SCHERR_CLASS_INCONSISTENT;
532 if ( strcasecmp( soc->soc_names[ i ],
533 new_soc->soc_names[ i ] ) != 0 )
535 return SLAP_SCHERR_CLASS_INCONSISTENT;
539 if ( soc->soc_names != NULL ) {
540 return SLAP_SCHERR_CLASS_INCONSISTENT;
544 return SLAP_SCHERR_CLASS_DUP;
547 static struct oindexrec *oir_old;
550 oc_dup_error( void *left, void *right )
562 struct oindexrec *oir;
564 ObjectClass *soc = *roc;
566 if ( soc->soc_oid ) {
567 oir = (struct oindexrec *)
568 ch_calloc( 1, sizeof(struct oindexrec) );
569 ber_str2bv( soc->soc_oid, 0, 0, &oir->oir_name );
573 if ( avl_insert( &oc_index, (caddr_t) oir,
574 oc_index_cmp, oc_dup_error ) )
576 ObjectClass *old_soc;
581 assert( oir_old != NULL );
582 old_soc = oir_old->oir_oc;
584 /* replacing a deleted definition? */
585 if ( old_soc->soc_flags & SLAP_OC_DELETED ) {
588 /* Keep old oid, free new oid;
589 * Keep new everything else, free old
593 old_soc->soc_oid = tmp.soc_oid;
594 tmp.soc_oid = soc->soc_oid;
598 oc_destroy_one( oir );
604 rc = oc_check_dup( old_soc, soc );
611 /* FIX: temporal consistency check */
612 assert( oc_bvfind( &oir->oir_name ) != NULL );
615 if ( (names = soc->soc_names) ) {
617 oir = (struct oindexrec *)
618 ch_calloc( 1, sizeof(struct oindexrec) );
619 oir->oir_name.bv_val = *names;
620 oir->oir_name.bv_len = strlen( *names );
623 assert( oir->oir_name.bv_val != NULL );
624 assert( oir->oir_oc != NULL );
626 if ( avl_insert( &oc_index, (caddr_t) oir,
627 oc_index_cmp, avl_dup_error ) )
629 ObjectClass *old_soc;
634 old_soc = oc_bvfind( &oir->oir_name );
635 assert( old_soc != NULL );
636 rc = oc_check_dup( old_soc, soc );
640 while ( names > soc->soc_names ) {
641 struct oindexrec tmpoir;
644 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
646 oir = (struct oindexrec *)avl_delete( &oc_index,
647 (caddr_t)&tmpoir, oc_index_cmp );
648 assert( oir != NULL );
652 if ( soc->soc_oid ) {
653 struct oindexrec tmpoir;
655 ber_str2bv( soc->soc_oid, 0, 0, &tmpoir.oir_name );
657 oir = (struct oindexrec *)avl_delete( &oc_index,
658 (caddr_t)&tmpoir, oc_index_cmp );
659 assert( oir != NULL );
666 /* FIX: temporal consistency check */
667 assert( oc_bvfind(&oir->oir_name) != NULL );
672 LDAP_STAILQ_INSERT_TAIL( &oc_list, soc, soc_next );
690 if ( oc->oc_names != NULL ) {
693 for( i=0; oc->oc_names[i]; i++ ) {
694 if( !slap_valid_descr( oc->oc_names[i] ) ) {
695 return SLAP_SCHERR_BAD_DESCR;
700 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
701 /* Expand OID macros */
702 char *oid = oidm_find( oc->oc_oid );
705 return SLAP_SCHERR_OIDM;
707 if ( oid != oc->oc_oid ) {
713 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
714 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
716 soc->soc_oidmacro = oidm;
717 if( oc->oc_names != NULL ) {
718 soc->soc_cname.bv_val = soc->soc_names[0];
720 soc->soc_cname.bv_val = soc->soc_oid;
722 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
724 if( soc->soc_sup_oids == NULL &&
725 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
727 /* structural object classes implicitly inherit from 'top' */
728 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
729 code = oc_add_sups( soc, top_oids, &op, err );
731 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
739 code = SLAP_SCHERR_CLASS_BAD_SUP;
743 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
748 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
754 code = SLAP_SCHERR_CLASS_BAD_USAGE;
759 soc->soc_flags |= SLAP_OC_HARDCODE;
762 code = oc_insert(&soc,prev,err);
765 if ( soc->soc_sups ) {
766 ch_free( soc->soc_sups );
769 if ( soc->soc_required ) {
770 ch_free( soc->soc_required );
773 if ( soc->soc_allowed ) {
774 ch_free( soc->soc_allowed );
786 oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
790 struct berval bv, *bva = NULL, idx;
794 start = LDAP_STAILQ_FIRST( &oc_list );
796 /* count the result size */
798 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
799 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) continue;
801 if ( oc == end ) break;
806 bva = ch_malloc( (num+1) * sizeof(struct berval) );
814 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
815 LDAPObjectClass loc, *locp;
816 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) continue;
817 if ( oc->soc_oidmacro ) {
818 loc = oc->soc_oclass;
819 loc.oc_oid = oc->soc_oidmacro;
822 locp = &oc->soc_oclass;
824 if ( ldap_objectclass2bv( locp, &bv ) == NULL ) {
825 ber_bvarray_free( bva );
828 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
830 bva[i].bv_len = idx.bv_len + bv.bv_len;
831 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
832 strcpy( bva[i].bv_val, ibuf );
833 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
835 bva[i].bv_val = NULL;
836 ldap_memfree( bv.bv_val );
837 if ( oc == end ) break;
843 oc_schema_info( Entry *e )
845 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
850 LDAP_STAILQ_FOREACH( oc, &oc_list, soc_next ) {
851 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
853 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
857 nval = oc->soc_cname;
860 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
861 (long) val.bv_len, val.bv_val, nval.bv_val );
864 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
867 ldap_memfree( val.bv_val );
873 register_oc( char *def, ObjectClass **soc, int dupok )
879 oc = ldap_str2objectclass( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
881 Debug( LDAP_DEBUG_ANY,
882 "register_oc: objectclass \"%s\": %s, %s\n",
883 def, ldap_scherr2str(code), err );
886 code = oc_add(oc,0,NULL,NULL,&err);
887 if ( code && ( code != SLAP_SCHERR_CLASS_DUP || !dupok )) {
888 Debug( LDAP_DEBUG_ANY,
889 "register_oc: objectclass \"%s\": %s, %s\n",
890 def, scherr2str(code), err );
891 ldap_objectclass_free(oc);
895 *soc = oc_find(oc->oc_names[0]);
897 ldap_objectclass_free(oc);