1 /* oc.c - object class routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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);
138 ObjectClass *oc_sys_tail;
145 const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
146 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
148 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
156 const struct berval *name = v_name;
157 const struct oindexrec *oir = v_oir;
158 int i = name->bv_len - oir->oir_name.bv_len;
160 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
164 oc_find( const char *ocname )
168 bv.bv_val = (char *)ocname;
169 bv.bv_len = strlen( ocname );
171 return( oc_bvfind( &bv ) );
175 oc_bvfind( struct berval *ocname )
177 struct oindexrec *oir;
180 oir = avl_find( oc_cache, ocname, oc_index_name_cmp );
181 if ( oir ) return oir->oir_oc;
183 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
187 avl_insert( &oc_cache, (caddr_t) oir,
188 oc_index_cmp, avl_dup_error );
190 return( oir->oir_oc );
196 static LDAP_STAILQ_HEAD(OCUList, slap_object_class) oc_undef_list
197 = LDAP_STAILQ_HEAD_INITIALIZER(oc_undef_list);
200 oc_bvfind_undef( struct berval *ocname )
202 ObjectClass *oc = oc_bvfind( ocname );
208 LDAP_STAILQ_FOREACH( oc, &oc_undef_list, soc_next ) {
209 int d = oc->soc_cname.bv_len - ocname->bv_len;
215 if ( strcasecmp( oc->soc_cname.bv_val, ocname->bv_val ) == 0 ) {
224 oc = ch_malloc( sizeof( ObjectClass ) + ocname->bv_len + 1 );
225 memset( oc, 0, sizeof( ObjectClass ) );
227 oc->soc_cname.bv_len = ocname->bv_len;
228 oc->soc_cname.bv_val = (char *)&oc[ 1 ];
229 AC_MEMCPY( oc->soc_cname.bv_val, ocname->bv_val, ocname->bv_len );
231 LDAP_STAILQ_NEXT( oc, soc_next ) = NULL;
232 ldap_pvt_thread_mutex_lock( &oc_undef_mutex );
233 LDAP_STAILQ_INSERT_HEAD( &oc_undef_list, oc, soc_next );
234 ldap_pvt_thread_mutex_unlock( &oc_undef_mutex );
248 AttributeType **satp;
254 sat = at_find(*attrs1);
257 return SLAP_SCHERR_ATTR_NOT_FOUND;
260 if( is_at_operational( sat )) (*op)++;
262 if ( at_find_in_list(sat, soc->soc_required) < 0) {
263 if ( at_append_to_list(sat, &soc->soc_required) ) {
265 return SLAP_SCHERR_OUTOFMEM;
270 /* Now delete duplicates from the allowed list */
271 for ( satp = soc->soc_required; *satp; satp++ ) {
272 i = at_find_in_list(*satp, soc->soc_allowed);
274 at_delete_from_list(i, &soc->soc_allowed);
294 sat = at_find(*attrs1);
297 return SLAP_SCHERR_ATTR_NOT_FOUND;
300 if( is_at_operational( sat )) (*op)++;
302 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
303 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
304 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
306 return SLAP_SCHERR_OUTOFMEM;
329 if ( !soc->soc_sups ) {
330 /* We are at the first recursive level */
338 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
339 sizeof(ObjectClass *));
345 soc1 = oc_find(*sups1);
348 return SLAP_SCHERR_CLASS_NOT_FOUND;
351 /* check object class usage
352 * abstract classes can only sup abstract classes
353 * structural classes can not sup auxiliary classes
354 * auxiliary classes can not sup structural classes
356 if( soc->soc_kind != soc1->soc_kind
357 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
360 return SLAP_SCHERR_CLASS_BAD_SUP;
363 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
365 return SLAP_SCHERR_CLASS_BAD_SUP;
368 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
371 soc->soc_sups[nsups] = soc1;
374 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
375 if ( code ) return code;
377 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
378 if ( code ) return code;
380 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
381 if ( code ) return code;
392 oc_delete_names( ObjectClass *oc )
394 char **names = oc->soc_names;
397 struct oindexrec tmpoir, *oir;
399 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
401 oir = (struct oindexrec *)avl_delete( &oc_index,
402 (caddr_t)&tmpoir, oc_index_cmp );
403 assert( oir != NULL );
409 /* Mark the ObjectClass as deleted, remove from list, and remove all its
410 * names from the AVL tree. Leave the OID in the tree.
413 oc_delete( ObjectClass *oc )
415 oc->soc_flags |= SLAP_OC_DELETED;
417 LDAP_STAILQ_REMOVE(&oc_list,oc,slap_object_class,soc_next);
419 oc_delete_names( oc );
423 oc_clean( ObjectClass *o )
425 if (o->soc_sups) ldap_memfree(o->soc_sups);
426 if (o->soc_required) ldap_memfree(o->soc_required);
427 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
428 if (o->soc_oidmacro) ldap_memfree(o->soc_oidmacro);
432 oc_destroy_one( void *v )
434 struct oindexrec *oir = v;
435 ObjectClass *o = oir->oir_oc;
438 ldap_objectclass_free((LDAPObjectClass *)o);
447 while( !LDAP_STAILQ_EMPTY(&oc_list) ) {
448 o = LDAP_STAILQ_FIRST(&oc_list);
449 LDAP_STAILQ_REMOVE_HEAD(&oc_list, soc_next);
451 oc_delete_names( o );
454 avl_free( oc_index, oc_destroy_one );
456 while( !LDAP_STAILQ_EMPTY(&oc_undef_list) ) {
457 o = LDAP_STAILQ_FIRST(&oc_undef_list);
458 LDAP_STAILQ_REMOVE_HEAD(&oc_undef_list, soc_next);
460 ch_free( (ObjectClass *)o );
465 oc_start( ObjectClass **oc )
467 assert( oc != NULL );
469 *oc = LDAP_STAILQ_FIRST(&oc_list);
471 return (*oc != NULL);
475 oc_next( ObjectClass **oc )
477 assert( oc != NULL );
479 #if 1 /* pedantic check */
481 ObjectClass *tmp = NULL;
483 LDAP_STAILQ_FOREACH(tmp,&oc_list,soc_next) {
489 assert( tmp != NULL );
493 *oc = LDAP_STAILQ_NEXT(*oc,soc_next);
495 return (*oc != NULL);
499 * check whether the two ObjectClasses actually __are__ identical,
500 * or rather inconsistent
505 ObjectClass *new_soc )
507 if ( new_soc->soc_oid != NULL ) {
508 if ( soc->soc_oid == NULL ) {
509 return SLAP_SCHERR_CLASS_INCONSISTENT;
512 if ( strcmp( soc->soc_oid, new_soc->soc_oid ) != 0 ) {
513 return SLAP_SCHERR_CLASS_INCONSISTENT;
517 if ( soc->soc_oid != NULL ) {
518 return SLAP_SCHERR_CLASS_INCONSISTENT;
522 if ( new_soc->soc_names ) {
525 if ( soc->soc_names == NULL ) {
526 return SLAP_SCHERR_CLASS_INCONSISTENT;
529 for ( i = 0; new_soc->soc_names[ i ]; i++ ) {
530 if ( soc->soc_names[ i ] == NULL ) {
531 return SLAP_SCHERR_CLASS_INCONSISTENT;
534 if ( strcasecmp( soc->soc_names[ i ],
535 new_soc->soc_names[ i ] ) != 0 )
537 return SLAP_SCHERR_CLASS_INCONSISTENT;
541 if ( soc->soc_names != NULL ) {
542 return SLAP_SCHERR_CLASS_INCONSISTENT;
546 return SLAP_SCHERR_CLASS_DUP;
549 static struct oindexrec *oir_old;
552 oc_dup_error( void *left, void *right )
564 struct oindexrec *oir;
566 ObjectClass *soc = *roc;
568 if ( soc->soc_oid ) {
569 oir = (struct oindexrec *)
570 ch_calloc( 1, sizeof(struct oindexrec) );
571 ber_str2bv( soc->soc_oid, 0, 0, &oir->oir_name );
575 if ( avl_insert( &oc_index, (caddr_t) oir,
576 oc_index_cmp, oc_dup_error ) )
578 ObjectClass *old_soc;
583 assert( oir_old != NULL );
584 old_soc = oir_old->oir_oc;
586 /* replacing a deleted definition? */
587 if ( old_soc->soc_flags & SLAP_OC_DELETED ) {
590 /* Keep old oid, free new oid;
591 * Keep new everything else, free old
595 old_soc->soc_oid = tmp.soc_oid;
596 tmp.soc_oid = soc->soc_oid;
600 oc_destroy_one( oir );
606 rc = oc_check_dup( old_soc, soc );
613 /* FIX: temporal consistency check */
614 assert( oc_bvfind( &oir->oir_name ) != NULL );
617 if ( (names = soc->soc_names) ) {
619 oir = (struct oindexrec *)
620 ch_calloc( 1, sizeof(struct oindexrec) );
621 oir->oir_name.bv_val = *names;
622 oir->oir_name.bv_len = strlen( *names );
625 assert( oir->oir_name.bv_val != NULL );
626 assert( oir->oir_oc != NULL );
628 if ( avl_insert( &oc_index, (caddr_t) oir,
629 oc_index_cmp, avl_dup_error ) )
631 ObjectClass *old_soc;
636 old_soc = oc_bvfind( &oir->oir_name );
637 assert( old_soc != NULL );
638 rc = oc_check_dup( old_soc, soc );
642 while ( names > soc->soc_names ) {
643 struct oindexrec tmpoir;
646 ber_str2bv( *names, 0, 0, &tmpoir.oir_name );
648 oir = (struct oindexrec *)avl_delete( &oc_index,
649 (caddr_t)&tmpoir, oc_index_cmp );
650 assert( oir != NULL );
654 if ( soc->soc_oid ) {
655 struct oindexrec tmpoir;
657 ber_str2bv( soc->soc_oid, 0, 0, &tmpoir.oir_name );
659 oir = (struct oindexrec *)avl_delete( &oc_index,
660 (caddr_t)&tmpoir, oc_index_cmp );
661 assert( oir != NULL );
668 /* FIX: temporal consistency check */
669 assert( oc_bvfind(&oir->oir_name) != NULL );
674 if ( soc->soc_flags & SLAP_OC_HARDCODE ) {
679 LDAP_STAILQ_INSERT_AFTER( &oc_list, prev, soc, soc_next );
681 LDAP_STAILQ_INSERT_TAIL( &oc_list, soc, soc_next );
700 if ( oc->oc_names != NULL ) {
703 for( i=0; oc->oc_names[i]; i++ ) {
704 if( !slap_valid_descr( oc->oc_names[i] ) ) {
705 return SLAP_SCHERR_BAD_DESCR;
710 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
711 /* Expand OID macros */
712 char *oid = oidm_find( oc->oc_oid );
715 return SLAP_SCHERR_OIDM;
717 if ( oid != oc->oc_oid ) {
723 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
724 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
726 soc->soc_oidmacro = oidm;
727 if( oc->oc_names != NULL ) {
728 soc->soc_cname.bv_val = soc->soc_names[0];
730 soc->soc_cname.bv_val = soc->soc_oid;
732 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
734 if( soc->soc_sup_oids == NULL &&
735 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
737 /* structural object classes implicitly inherit from 'top' */
738 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
739 code = oc_add_sups( soc, top_oids, &op, err );
741 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
749 code = SLAP_SCHERR_CLASS_BAD_SUP;
753 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
758 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
764 code = SLAP_SCHERR_CLASS_BAD_USAGE;
769 soc->soc_flags |= SLAP_OC_HARDCODE;
772 code = oc_insert(&soc,prev,err);
775 if ( soc->soc_sups ) {
776 ch_free( soc->soc_sups );
779 if ( soc->soc_required ) {
780 ch_free( soc->soc_required );
783 if ( soc->soc_allowed ) {
784 ch_free( soc->soc_allowed );
796 oc_unparse( BerVarray *res, ObjectClass *start, ObjectClass *end, int sys )
800 struct berval bv, *bva = NULL, idx;
804 start = LDAP_STAILQ_FIRST( &oc_list );
806 /* count the result size */
808 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
809 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) break;
811 if ( oc == end ) break;
816 bva = ch_malloc( (num+1) * sizeof(struct berval) );
824 for ( oc=start; oc; oc=LDAP_STAILQ_NEXT(oc, soc_next)) {
825 LDAPObjectClass loc, *locp;
826 if ( sys && !(oc->soc_flags & SLAP_OC_HARDCODE)) break;
827 if ( oc->soc_oidmacro ) {
828 loc = oc->soc_oclass;
829 loc.oc_oid = oc->soc_oidmacro;
832 locp = &oc->soc_oclass;
834 if ( ldap_objectclass2bv( locp, &bv ) == NULL ) {
835 ber_bvarray_free( bva );
838 idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
840 bva[i].bv_len = idx.bv_len + bv.bv_len;
841 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
842 strcpy( bva[i].bv_val, ibuf );
843 strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
845 bva[i].bv_val = NULL;
846 ldap_memfree( bv.bv_val );
847 if ( oc == end ) break;
853 oc_schema_info( Entry *e )
855 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
860 LDAP_STAILQ_FOREACH( oc, &oc_list, soc_next ) {
861 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
863 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
867 nval = oc->soc_cname;
870 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s (%s)\n",
871 (long) val.bv_len, val.bv_val, nval.bv_val );
874 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) ) {
877 ldap_memfree( val.bv_val );
883 register_oc( char *def, ObjectClass **soc, int dupok )
889 oc = ldap_str2objectclass( def, &code, &err, LDAP_SCHEMA_ALLOW_ALL );
891 Debug( LDAP_DEBUG_ANY,
892 "register_oc: objectclass \"%s\": %s, %s\n",
893 def, ldap_scherr2str(code), err );
896 code = oc_add(oc,0,NULL,NULL,&err);
897 if ( code && ( code != SLAP_SCHERR_CLASS_DUP || !dupok )) {
898 Debug( LDAP_DEBUG_ANY,
899 "register_oc: objectclass \"%s\": %s, %s\n",
900 def, scherr2str(code), err );
901 ldap_objectclass_free(oc);
905 *soc = oc_find(oc->oc_names[0]);
907 ldap_objectclass_free(oc);