1 /* oc.c - object class routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2010 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 );
95 /* mark flags as set */
96 e->e_ocflags |= SLAP_OC__END;
101 for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
102 ObjectClass *objectClass = oc_bvfind( bv );
104 if ( objectClass == NULL ) {
105 /* FIXME: is this acceptable? */
109 if ( !( flags & SLAP_OCF_SET_FLAGS ) ) {
110 if ( objectClass == oc ) {
114 if ( ( flags & SLAP_OCF_CHECK_SUP )
115 && is_object_subclass( oc, objectClass ) )
121 e->e_ocflags |= objectClass->soc_flags;
124 /* mark flags as set */
125 e->e_ocflags |= SLAP_OC__END;
127 return ( e->e_ocflags & oc->soc_flags & SLAP_OC__MASK ) != 0;
132 struct berval oir_name;
136 static Avlnode *oc_index = NULL;
137 static Avlnode *oc_cache = NULL;
138 static LDAP_STAILQ_HEAD(OCList, ObjectClass) oc_list
139 = LDAP_STAILQ_HEAD_INITIALIZER(oc_list);
141 ObjectClass *oc_sys_tail;
148 const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
149 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
151 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
159 const struct berval *name = v_name;
160 const struct oindexrec *oir = v_oir;
161 int i = name->bv_len - oir->oir_name.bv_len;
163 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
167 oc_find( const char *ocname )
171 bv.bv_val = (char *)ocname;
172 bv.bv_len = strlen( ocname );
174 return( oc_bvfind( &bv ) );
178 oc_bvfind( struct berval *ocname )
180 struct oindexrec *oir;
183 oir = avl_find( oc_cache, ocname, oc_index_name_cmp );
184 if ( oir ) return oir->oir_oc;
186 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
190 avl_insert( &oc_cache, (caddr_t) oir,
191 oc_index_cmp, avl_dup_error );
193 return( oir->oir_oc );
199 static LDAP_STAILQ_HEAD(OCUList, ObjectClass) oc_undef_list
200 = LDAP_STAILQ_HEAD_INITIALIZER(oc_undef_list);
203 oc_bvfind_undef( struct berval *ocname )
205 ObjectClass *oc = oc_bvfind( ocname );
211 LDAP_STAILQ_FOREACH( oc, &oc_undef_list, soc_next ) {
212 int d = oc->soc_cname.bv_len - ocname->bv_len;
218 if ( strcasecmp( oc->soc_cname.bv_val, ocname->bv_val ) == 0 ) {
227 oc = ch_malloc( sizeof( ObjectClass ) + ocname->bv_len + 1 );
228 memset( oc, 0, sizeof( ObjectClass ) );
230 oc->soc_cname.bv_len = ocname->bv_len;
231 oc->soc_cname.bv_val = (char *)&oc[ 1 ];
232 AC_MEMCPY( oc->soc_cname.bv_val, ocname->bv_val, ocname->bv_len );
233 oc->soc_cname.bv_val[ oc->soc_cname.bv_len ] = '\0';
235 /* canonical to upper case */
236 ldap_pvt_str2upper( oc->soc_cname.bv_val );
238 LDAP_STAILQ_NEXT( oc, soc_next ) = NULL;
239 ldap_pvt_thread_mutex_lock( &oc_undef_mutex );
240 LDAP_STAILQ_INSERT_HEAD( &oc_undef_list, oc, soc_next );
241 ldap_pvt_thread_mutex_unlock( &oc_undef_mutex );
255 AttributeType **satp;
261 sat = at_find(*attrs1);
264 return SLAP_SCHERR_ATTR_NOT_FOUND;
267 if( is_at_operational( sat )) (*op)++;
269 if ( at_find_in_list(sat, soc->soc_required) < 0) {
270 if ( at_append_to_list(sat, &soc->soc_required) ) {
272 return SLAP_SCHERR_OUTOFMEM;
277 /* Now delete duplicates from the allowed list */
278 for ( satp = soc->soc_required; *satp; satp++ ) {
279 i = at_find_in_list(*satp, soc->soc_allowed);
281 at_delete_from_list(i, &soc->soc_allowed);
301 sat = at_find(*attrs1);
304 return SLAP_SCHERR_ATTR_NOT_FOUND;
307 if( is_at_operational( sat )) (*op)++;
309 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
310 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
311 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
313 return SLAP_SCHERR_OUTOFMEM;
336 if ( !soc->soc_sups ) {
337 /* We are at the first recursive level */
345 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
346 sizeof(ObjectClass *));
352 soc1 = oc_find(*sups1);
355 return SLAP_SCHERR_CLASS_NOT_FOUND;
358 /* check object class usage
359 * abstract classes can only sup abstract classes
360 * structural classes can not sup auxiliary classes
361 * auxiliary classes can not sup structural classes
363 if( soc->soc_kind != soc1->soc_kind
364 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
367 return SLAP_SCHERR_CLASS_BAD_SUP;
370 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
372 return SLAP_SCHERR_CLASS_BAD_SUP;
375 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
378 soc->soc_sups[nsups] = soc1;
381 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
382 if ( code ) return code;
384 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
385 if ( code ) return code;
387 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
388 if ( code ) return code;
399 oc_delete_names( ObjectClass *oc )
401 char **names = oc->soc_names;
404 struct oindexrec tmpoir, *oir;
406 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
408 oir = (struct oindexrec *)avl_delete( &oc_index,
409 (caddr_t)&tmpoir, oc_index_cmp );
410 assert( oir != NULL );
416 /* Mark the ObjectClass as deleted, remove from list, and remove all its
417 * names from the AVL tree. Leave the OID in the tree.
420 oc_delete( ObjectClass *oc )
422 oc->soc_flags |= SLAP_OC_DELETED;
424 LDAP_STAILQ_REMOVE(&oc_list, oc, ObjectClass, soc_next);
426 oc_delete_names( oc );
430 oc_clean( ObjectClass *o )
433 ldap_memfree(o->soc_sups);
436 if (o->soc_required) {
437 ldap_memfree(o->soc_required);
438 o->soc_required = NULL;
440 if (o->soc_allowed) {
441 ldap_memfree(o->soc_allowed);
442 o->soc_allowed = NULL;
444 if (o->soc_oidmacro) {
445 ldap_memfree(o->soc_oidmacro);
446 o->soc_oidmacro = NULL;
451 oc_destroy_one( void *v )
453 struct oindexrec *oir = v;
454 ObjectClass *o = oir->oir_oc;
457 ldap_objectclass_free((LDAPObjectClass *)o);
466 while( !LDAP_STAILQ_EMPTY(&oc_list) ) {
467 o = LDAP_STAILQ_FIRST(&oc_list);
468 LDAP_STAILQ_REMOVE_HEAD(&oc_list, soc_next);
470 oc_delete_names( o );
473 avl_free( oc_index, oc_destroy_one );
475 while( !LDAP_STAILQ_EMPTY(&oc_undef_list) ) {
476 o = LDAP_STAILQ_FIRST(&oc_undef_list);
477 LDAP_STAILQ_REMOVE_HEAD(&oc_undef_list, soc_next);
479 ch_free( (ObjectClass *)o );
484 oc_start( ObjectClass **oc )
486 assert( oc != NULL );
488 *oc = LDAP_STAILQ_FIRST(&oc_list);
490 return (*oc != NULL);
494 oc_next( ObjectClass **oc )
496 assert( oc != NULL );
498 #if 0 /* pedantic check: breaks when deleting an oc, don't use it. */
500 ObjectClass *tmp = NULL;
502 LDAP_STAILQ_FOREACH(tmp,&oc_list,soc_next) {
508 assert( tmp != NULL );
516 *oc = LDAP_STAILQ_NEXT(*oc,soc_next);
518 return (*oc != NULL);
522 * check whether the two ObjectClasses actually __are__ identical,
523 * or rather inconsistent
528 ObjectClass *new_soc )
530 if ( new_soc->soc_oid != NULL ) {
531 if ( soc->soc_oid == NULL ) {
532 return SLAP_SCHERR_CLASS_INCONSISTENT;
535 if ( strcmp( soc->soc_oid, new_soc->soc_oid ) != 0 ) {
536 return SLAP_SCHERR_CLASS_INCONSISTENT;
540 if ( soc->soc_oid != NULL ) {
541 return SLAP_SCHERR_CLASS_INCONSISTENT;
545 if ( new_soc->soc_names ) {
548 if ( soc->soc_names == NULL ) {
549 return SLAP_SCHERR_CLASS_INCONSISTENT;
552 for ( i = 0; new_soc->soc_names[ i ]; i++ ) {
553 if ( soc->soc_names[ i ] == NULL ) {
554 return SLAP_SCHERR_CLASS_INCONSISTENT;
557 if ( strcasecmp( soc->soc_names[ i ],
558 new_soc->soc_names[ i ] ) != 0 )
560 return SLAP_SCHERR_CLASS_INCONSISTENT;
564 if ( soc->soc_names != NULL ) {
565 return SLAP_SCHERR_CLASS_INCONSISTENT;
569 return SLAP_SCHERR_CLASS_DUP;
572 static struct oindexrec *oir_old;
575 oc_dup_error( void *left, void *right )
587 struct oindexrec *oir;
589 ObjectClass *soc = *roc;
591 if ( soc->soc_oid ) {
592 oir = (struct oindexrec *)
593 ch_calloc( 1, sizeof(struct oindexrec) );
594 ber_str2bv( soc->soc_oid, 0, 0, &oir->oir_name );
598 if ( avl_insert( &oc_index, (caddr_t) oir,
599 oc_index_cmp, oc_dup_error ) )
601 ObjectClass *old_soc;
606 assert( oir_old != NULL );
607 old_soc = oir_old->oir_oc;
609 /* replacing a deleted definition? */
610 if ( old_soc->soc_flags & SLAP_OC_DELETED ) {
613 /* Keep old oid, free new oid;
614 * Keep new everything else, free old
618 old_soc->soc_oid = tmp.soc_oid;
619 tmp.soc_oid = soc->soc_oid;
623 oc_destroy_one( oir );
629 rc = oc_check_dup( old_soc, soc );
636 /* FIX: temporal consistency check */
637 assert( oc_bvfind( &oir->oir_name ) != NULL );
640 if ( (names = soc->soc_names) ) {
642 oir = (struct oindexrec *)
643 ch_calloc( 1, sizeof(struct oindexrec) );
644 oir->oir_name.bv_val = *names;
645 oir->oir_name.bv_len = strlen( *names );
648 assert( oir->oir_name.bv_val != NULL );
649 assert( oir->oir_oc != NULL );
651 if ( avl_insert( &oc_index, (caddr_t) oir,
652 oc_index_cmp, avl_dup_error ) )
654 ObjectClass *old_soc;
659 old_soc = oc_bvfind( &oir->oir_name );
660 assert( old_soc != NULL );
661 rc = oc_check_dup( old_soc, soc );
665 while ( names > soc->soc_names ) {
666 struct oindexrec tmpoir;
669 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
671 oir = (struct oindexrec *)avl_delete( &oc_index,
672 (caddr_t)&tmpoir, oc_index_cmp );
673 assert( oir != NULL );
677 if ( soc->soc_oid ) {
678 struct oindexrec tmpoir;
680 ber_str2bv( soc->soc_oid, 0, 0, &tmpoir.oir_name );
682 oir = (struct oindexrec *)avl_delete( &oc_index,
683 (caddr_t)&tmpoir, oc_index_cmp );
684 assert( oir != NULL );
691 /* FIX: temporal consistency check */
692 assert( oc_bvfind(&oir->oir_name) != NULL );
697 if ( soc->soc_flags & SLAP_OC_HARDCODE ) {
702 LDAP_STAILQ_INSERT_AFTER( &oc_list, prev, soc, soc_next );
704 LDAP_STAILQ_INSERT_TAIL( &oc_list, soc, soc_next );
723 if ( oc->oc_names != NULL ) {
726 for( i=0; oc->oc_names[i]; i++ ) {
727 if( !slap_valid_descr( oc->oc_names[i] ) ) {
728 return SLAP_SCHERR_BAD_DESCR;
733 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
734 /* Expand OID macros */
735 char *oid = oidm_find( oc->oc_oid );
738 return SLAP_SCHERR_OIDM;
740 if ( oid != oc->oc_oid ) {
746 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
747 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
749 soc->soc_oidmacro = oidm;
750 if( oc->oc_names != NULL ) {
751 soc->soc_cname.bv_val = soc->soc_names[0];
753 soc->soc_cname.bv_val = soc->soc_oid;
755 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
757 if( soc->soc_sup_oids == NULL &&
758 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
760 /* structural object classes implicitly inherit from 'top' */
761 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
762 code = oc_add_sups( soc, top_oids, &op, err );
764 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
772 code = SLAP_SCHERR_CLASS_BAD_SUP;
776 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
781 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
787 code = SLAP_SCHERR_CLASS_BAD_USAGE;
792 soc->soc_flags |= SLAP_OC_HARDCODE;
795 code = oc_insert(&soc,prev,err);
798 if ( soc->soc_sups ) {
799 ch_free( soc->soc_sups );
802 if ( soc->soc_required ) {
803 ch_free( soc->soc_required );
806 if ( soc->soc_allowed ) {
807 ch_free( soc->soc_allowed );
810 if ( soc->soc_oidmacro ) {
811 ch_free( soc->soc_oidmacro );
823 oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
827 struct berval bv, *bva = NULL, idx;
831 start = LDAP_STAILQ_FIRST( &oc_list );
833 /* count the result size */
835 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
836 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) break;
838 if ( oc == end ) break;
843 bva = ch_malloc( (num+1) * sizeof(struct berval) );
851 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
852 LDAPObjectClass loc, *locp;
853 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) break;
854 if ( oc->soc_oidmacro ) {
855 loc = oc->soc_oclass;
856 loc.oc_oid = oc->soc_oidmacro;
859 locp = &oc->soc_oclass;
861 if ( ldap_objectclass2bv( locp, &bv ) == NULL ) {
862 ber_bvarray_free( bva );
865 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
867 bva[i].bv_len = idx.bv_len + bv.bv_len;
868 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
869 strcpy( bva[i].bv_val, ibuf );
870 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
872 bva[i].bv_val = NULL;
873 ldap_memfree( bv.bv_val );
874 if ( oc == end ) break;
880 oc_schema_info( Entry *e )
882 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
887 LDAP_STAILQ_FOREACH( oc, &oc_list, soc_next ) {
888 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
890 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
894 nval = oc->soc_cname;
897 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
898 (long) val.bv_len, val.bv_val, nval.bv_val );
901 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
904 ldap_memfree( val.bv_val );
910 register_oc( const char *def, ObjectClass **soc, int dupok )
916 oc = ldap_str2objectclass( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
918 Debug( LDAP_DEBUG_ANY,
919 "register_oc: objectclass \"%s\": %s, %s\n",
920 def, ldap_scherr2str(code), err );
923 code = oc_add(oc,0,NULL,NULL,&err);
924 if ( code && ( code != SLAP_SCHERR_CLASS_DUP || !dupok )) {
925 Debug( LDAP_DEBUG_ANY,
926 "register_oc: objectclass \"%s\": %s, %s\n",
927 def, scherr2str(code), err );
928 ldap_objectclass_free(oc);
932 *soc = oc_find(oc->oc_names[0]);
934 ldap_objectclass_free(oc);