1 /* oc.c - object class routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 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;
406 struct oindexrec tmpoir, *oir;
408 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
410 oir = (struct oindexrec *)avl_delete( &oc_index,
411 (caddr_t)&tmpoir, oc_index_cmp );
412 assert( oir != NULL );
418 /* Mark the ObjectClass as deleted, remove from list, and remove all its
419 * names from the AVL tree. Leave the OID in the tree.
422 oc_delete( ObjectClass *oc )
424 oc->soc_flags |= SLAP_OC_DELETED;
426 LDAP_STAILQ_REMOVE(&oc_list, oc, ObjectClass, soc_next);
428 oc_delete_names( oc );
432 oc_clean( ObjectClass *o )
435 ldap_memfree(o->soc_sups);
438 if (o->soc_required) {
439 ldap_memfree(o->soc_required);
440 o->soc_required = NULL;
442 if (o->soc_allowed) {
443 ldap_memfree(o->soc_allowed);
444 o->soc_allowed = NULL;
446 if (o->soc_oidmacro) {
447 ldap_memfree(o->soc_oidmacro);
448 o->soc_oidmacro = NULL;
453 oc_destroy_one( void *v )
455 struct oindexrec *oir = v;
456 ObjectClass *o = oir->oir_oc;
459 ldap_objectclass_free((LDAPObjectClass *)o);
468 while( !LDAP_STAILQ_EMPTY(&oc_list) ) {
469 o = LDAP_STAILQ_FIRST(&oc_list);
470 LDAP_STAILQ_REMOVE_HEAD(&oc_list, soc_next);
472 oc_delete_names( o );
475 avl_free( oc_index, oc_destroy_one );
477 while( !LDAP_STAILQ_EMPTY(&oc_undef_list) ) {
478 o = LDAP_STAILQ_FIRST(&oc_undef_list);
479 LDAP_STAILQ_REMOVE_HEAD(&oc_undef_list, soc_next);
481 ch_free( (ObjectClass *)o );
486 oc_start( ObjectClass **oc )
488 assert( oc != NULL );
490 *oc = LDAP_STAILQ_FIRST(&oc_list);
492 return (*oc != NULL);
496 oc_next( ObjectClass **oc )
498 assert( oc != NULL );
500 #if 0 /* pedantic check: breaks when deleting an oc, don't use it. */
502 ObjectClass *tmp = NULL;
504 LDAP_STAILQ_FOREACH(tmp,&oc_list,soc_next) {
510 assert( tmp != NULL );
518 *oc = LDAP_STAILQ_NEXT(*oc,soc_next);
520 return (*oc != NULL);
524 * check whether the two ObjectClasses actually __are__ identical,
525 * or rather inconsistent
530 ObjectClass *new_soc )
532 if ( new_soc->soc_oid != NULL ) {
533 if ( soc->soc_oid == NULL ) {
534 return SLAP_SCHERR_CLASS_INCONSISTENT;
537 if ( strcmp( soc->soc_oid, new_soc->soc_oid ) != 0 ) {
538 return SLAP_SCHERR_CLASS_INCONSISTENT;
542 if ( soc->soc_oid != NULL ) {
543 return SLAP_SCHERR_CLASS_INCONSISTENT;
547 if ( new_soc->soc_names ) {
550 if ( soc->soc_names == NULL ) {
551 return SLAP_SCHERR_CLASS_INCONSISTENT;
554 for ( i = 0; new_soc->soc_names[ i ]; i++ ) {
555 if ( soc->soc_names[ i ] == NULL ) {
556 return SLAP_SCHERR_CLASS_INCONSISTENT;
559 if ( strcasecmp( soc->soc_names[ i ],
560 new_soc->soc_names[ i ] ) != 0 )
562 return SLAP_SCHERR_CLASS_INCONSISTENT;
566 if ( soc->soc_names != NULL ) {
567 return SLAP_SCHERR_CLASS_INCONSISTENT;
571 return SLAP_SCHERR_CLASS_DUP;
574 static struct oindexrec *oir_old;
577 oc_dup_error( void *left, void *right )
589 struct oindexrec *oir;
591 ObjectClass *soc = *roc;
593 if ( soc->soc_oid ) {
594 oir = (struct oindexrec *)
595 ch_calloc( 1, sizeof(struct oindexrec) );
596 ber_str2bv( soc->soc_oid, 0, 0, &oir->oir_name );
600 if ( avl_insert( &oc_index, (caddr_t) oir,
601 oc_index_cmp, oc_dup_error ) )
603 ObjectClass *old_soc;
608 assert( oir_old != NULL );
609 old_soc = oir_old->oir_oc;
611 /* replacing a deleted definition? */
612 if ( old_soc->soc_flags & SLAP_OC_DELETED ) {
615 /* Keep old oid, free new oid;
616 * Keep new everything else, free old
620 old_soc->soc_oid = tmp.soc_oid;
621 tmp.soc_oid = soc->soc_oid;
625 oc_destroy_one( oir );
631 rc = oc_check_dup( old_soc, soc );
638 /* FIX: temporal consistency check */
639 assert( oc_bvfind( &oir->oir_name ) != NULL );
642 assert( soc != NULL );
644 if ( (names = soc->soc_names) ) {
646 oir = (struct oindexrec *)
647 ch_calloc( 1, sizeof(struct oindexrec) );
648 oir->oir_name.bv_val = *names;
649 oir->oir_name.bv_len = strlen( *names );
652 if ( avl_insert( &oc_index, (caddr_t) oir,
653 oc_index_cmp, avl_dup_error ) )
655 ObjectClass *old_soc;
660 old_soc = oc_bvfind( &oir->oir_name );
661 assert( old_soc != NULL );
662 rc = oc_check_dup( old_soc, soc );
666 while ( names > soc->soc_names ) {
667 struct oindexrec tmpoir;
670 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
672 oir = (struct oindexrec *)avl_delete( &oc_index,
673 (caddr_t)&tmpoir, oc_index_cmp );
674 assert( oir != NULL );
678 if ( soc->soc_oid ) {
679 struct oindexrec tmpoir;
681 ber_str2bv( soc->soc_oid, 0, 0, &tmpoir.oir_name );
683 oir = (struct oindexrec *)avl_delete( &oc_index,
684 (caddr_t)&tmpoir, oc_index_cmp );
685 assert( oir != NULL );
692 /* FIX: temporal consistency check */
693 assert( oc_bvfind(&oir->oir_name) != NULL );
698 if ( soc->soc_flags & SLAP_OC_HARDCODE ) {
703 LDAP_STAILQ_INSERT_AFTER( &oc_list, prev, soc, soc_next );
705 LDAP_STAILQ_INSERT_TAIL( &oc_list, soc, soc_next );
724 if ( oc->oc_names != NULL ) {
727 for( i=0; oc->oc_names[i]; i++ ) {
728 if( !slap_valid_descr( oc->oc_names[i] ) ) {
729 return SLAP_SCHERR_BAD_DESCR;
734 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
735 /* Expand OID macros */
736 char *oid = oidm_find( oc->oc_oid );
739 return SLAP_SCHERR_OIDM;
741 if ( oid != oc->oc_oid ) {
747 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
748 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
750 soc->soc_oidmacro = oidm;
751 if( oc->oc_names != NULL ) {
752 soc->soc_cname.bv_val = soc->soc_names[0];
754 soc->soc_cname.bv_val = soc->soc_oid;
756 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
758 if( soc->soc_sup_oids == NULL &&
759 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
761 /* structural object classes implicitly inherit from 'top' */
762 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
763 code = oc_add_sups( soc, top_oids, &op, err );
765 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
773 code = SLAP_SCHERR_CLASS_BAD_SUP;
777 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
782 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
788 code = SLAP_SCHERR_CLASS_BAD_USAGE;
793 soc->soc_flags |= SLAP_OC_HARDCODE;
796 code = oc_insert(&soc,prev,err);
799 if ( soc->soc_sups ) {
800 ch_free( soc->soc_sups );
803 if ( soc->soc_required ) {
804 ch_free( soc->soc_required );
807 if ( soc->soc_allowed ) {
808 ch_free( soc->soc_allowed );
811 if ( soc->soc_oidmacro ) {
812 ch_free( soc->soc_oidmacro );
824 oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
828 struct berval bv, *bva = NULL, idx;
832 start = LDAP_STAILQ_FIRST( &oc_list );
834 /* count the result size */
836 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
837 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) break;
839 if ( oc == end ) break;
844 bva = ch_malloc( (num+1) * sizeof(struct berval) );
852 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
853 LDAPObjectClass loc, *locp;
854 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) break;
855 if ( oc->soc_oidmacro ) {
856 loc = oc->soc_oclass;
857 loc.oc_oid = oc->soc_oidmacro;
860 locp = &oc->soc_oclass;
862 if ( ldap_objectclass2bv( locp, &bv ) == NULL ) {
863 ber_bvarray_free( bva );
866 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
868 bva[i].bv_len = idx.bv_len + bv.bv_len;
869 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
870 strcpy( bva[i].bv_val, ibuf );
871 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
873 bva[i].bv_val = NULL;
874 ldap_memfree( bv.bv_val );
875 if ( oc == end ) break;
881 oc_schema_info( Entry *e )
883 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
888 LDAP_STAILQ_FOREACH( oc, &oc_list, soc_next ) {
889 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
891 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
895 nval = oc->soc_cname;
898 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
899 (long) val.bv_len, val.bv_val, nval.bv_val );
902 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
905 ldap_memfree( val.bv_val );
911 register_oc( const char *def, ObjectClass **soc, int dupok )
917 oc = ldap_str2objectclass( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
919 Debug( LDAP_DEBUG_ANY,
920 "register_oc: objectclass \"%s\": %s, %s\n",
921 def, ldap_scherr2str(code), err );
924 code = oc_add(oc,0,NULL,NULL,&err);
925 if ( code && ( code != SLAP_SCHERR_CLASS_DUP || !dupok )) {
926 Debug( LDAP_DEBUG_ANY,
927 "register_oc: objectclass \"%s\": %s, %s\n",
928 def, scherr2str(code), err );
929 ldap_objectclass_free(oc);
933 *soc = oc_find(oc->oc_names[0]);
935 ldap_objectclass_free(oc);