1 /* oc.c - object class routines */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/string.h>
14 #include <ac/socket.h>
19 int is_object_subclass(
25 if( sub == NULL || sup == NULL ) return 0;
28 Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
29 sub->soc_oid, sup->soc_oid, sup == sub );
36 if( sup->soc_sups == NULL ) {
40 for( i=0; sup->soc_sups[i] != NULL; i++ ) {
41 if( is_object_subclass( sub, sup->soc_sups[i] ) ) {
49 int is_entry_objectclass(
56 AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
57 assert(!( e == NULL || oc == NULL ));
59 if( e == NULL || oc == NULL ) {
63 if( set_flags && ( e->e_ocflags & SLAP_OC__END )) {
64 return (e->e_ocflags & oc->soc_flags);
68 * find objectClass attribute
70 attr = attr_find(e->e_attrs, objectClass);
73 /* no objectClass attribute */
75 LDAP_LOG(( "operation", LDAP_LEVEL_ERR, "is_entry_objectclass: "
76 "dn(%s), oid (%s), no objectClass attribute.\n",
77 e->e_dn == NULL ? "" : e->e_dn,
78 oc->soc_oclass.oc_oid ));
80 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
81 "no objectClass attribute\n",
82 e->e_dn == NULL ? "" : e->e_dn,
83 oc->soc_oclass.oc_oid, 0 );
89 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
90 ObjectClass *objectClass = oc_bvfind( bv );
92 if ( objectClass == oc && !set_flags ) {
96 if ( objectClass != NULL ) {
97 e->e_ocflags |= objectClass->soc_flags;
100 e->e_ocflags |= SLAP_OC__END; /* We've finished this */
102 return (e->e_ocflags & oc->soc_flags);
107 struct berval oir_name;
111 static Avlnode *oc_index = NULL;
112 static ObjectClass *oc_list = NULL;
116 struct oindexrec *oir1,
117 struct oindexrec *oir2 )
119 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
122 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
128 struct oindexrec *oir )
130 int i = name->bv_len - oir->oir_name.bv_len;
133 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
137 oc_find( const char *ocname )
141 bv.bv_val = (char *)ocname;
142 bv.bv_len = strlen( ocname );
144 return( oc_bvfind( &bv ) );
148 oc_bvfind( struct berval *ocname )
150 struct oindexrec *oir;
152 oir = (struct oindexrec *) avl_find( oc_index, ocname,
153 (AVL_CMP) oc_index_name_cmp );
156 return( oir->oir_oc );
170 AttributeType **satp;
176 sat = at_find(*attrs1);
179 return SLAP_SCHERR_ATTR_NOT_FOUND;
181 if ( at_find_in_list(sat, soc->soc_required) < 0) {
182 if ( at_append_to_list(sat, &soc->soc_required) ) {
184 return SLAP_SCHERR_OUTOFMEM;
189 /* Now delete duplicates from the allowed list */
190 for ( satp = soc->soc_required; *satp; satp++ ) {
191 i = at_find_in_list(*satp,soc->soc_allowed);
193 at_delete_from_list(i, &soc->soc_allowed);
212 sat = at_find(*attrs1);
215 return SLAP_SCHERR_ATTR_NOT_FOUND;
217 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
218 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
219 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
221 return SLAP_SCHERR_OUTOFMEM;
243 if ( !soc->soc_sups ) {
244 /* We are at the first recursive level */
252 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
253 sizeof(ObjectClass *));
259 soc1 = oc_find(*sups1);
262 return SLAP_SCHERR_CLASS_NOT_FOUND;
265 /* check object class usage
266 * abstract classes can only sup abstract classes
267 * structural classes can not sup auxiliary classes
268 * auxiliary classes can not sup structural classes
270 if( soc->soc_kind != soc1->soc_kind
271 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
274 return SLAP_SCHERR_CLASS_BAD_USAGE;
278 soc->soc_sups[nsups] = soc1;
280 code = oc_add_sups( soc, soc1->soc_sup_oids, err );
281 if ( code ) return code;
283 code = oc_create_required( soc, soc1->soc_at_oids_must, err );
284 if ( code ) return code;
286 code = oc_create_allowed( soc, soc1->soc_at_oids_may, err );
287 if ( code ) return code;
302 avl_free(oc_index, ldap_memfree);
303 for (o=oc_list; o; o=n)
306 if (o->soc_sups) ldap_memfree(o->soc_sups);
307 if (o->soc_required) ldap_memfree(o->soc_required);
308 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
309 ldap_objectclass_free((LDAPObjectClass *)o);
320 struct oindexrec *oir;
324 while ( *ocp != NULL ) {
325 ocp = &(*ocp)->soc_next;
329 if ( soc->soc_oid ) {
330 oir = (struct oindexrec *)
331 ch_calloc( 1, sizeof(struct oindexrec) );
332 oir->oir_name.bv_val = soc->soc_oid;
333 oir->oir_name.bv_len = strlen( soc->soc_oid );
336 assert( oir->oir_name.bv_val );
337 assert( oir->oir_oc );
339 if ( avl_insert( &oc_index, (caddr_t) oir,
340 (AVL_CMP) oc_index_cmp,
341 (AVL_DUP) avl_dup_error ) )
345 return SLAP_SCHERR_DUP_CLASS;
348 /* FIX: temporal consistency check */
349 assert( oc_bvfind(&oir->oir_name) != NULL );
352 if ( (names = soc->soc_names) ) {
354 oir = (struct oindexrec *)
355 ch_calloc( 1, sizeof(struct oindexrec) );
356 oir->oir_name.bv_val = *names;
357 oir->oir_name.bv_len = strlen( *names );
360 assert( oir->oir_name.bv_val );
361 assert( oir->oir_oc );
363 if ( avl_insert( &oc_index, (caddr_t) oir,
364 (AVL_CMP) oc_index_cmp,
365 (AVL_DUP) avl_dup_error ) )
369 return SLAP_SCHERR_DUP_CLASS;
372 /* FIX: temporal consistency check */
373 assert( oc_bvfind(&oir->oir_name) != NULL );
391 if ( oc->oc_names != NULL ) {
394 for( i=0; oc->oc_names[i]; i++ ) {
395 if( !slap_valid_descr( oc->oc_names[i] ) ) {
396 return SLAP_SCHERR_BAD_DESCR;
401 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
402 /* Expand OID macros */
403 char *oid = oidm_find( oc->oc_oid );
406 return SLAP_SCHERR_OIDM;
408 if ( oid != oc->oc_oid ) {
409 ldap_memfree( oc->oc_oid );
414 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
415 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
417 if( soc->soc_sup_oids == NULL &&
418 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
420 /* structural object classes implicitly inherit from 'top' */
421 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
422 code = oc_add_sups( soc, top_oids, err );
424 code = oc_add_sups( soc, soc->soc_sup_oids, err );
427 if ( code != 0 ) return code;
429 code = oc_create_required( soc, soc->soc_at_oids_must, err );
430 if ( code != 0 ) return code;
432 code = oc_create_allowed( soc, soc->soc_at_oids_may, err );
433 if ( code != 0 ) return code;
435 code = oc_insert(soc,err);
442 oc_print( ObjectClass *oc )
447 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
448 if ( oc->soc_required != NULL ) {
450 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
452 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
455 if ( oc->soc_allowed != NULL ) {
457 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
459 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
467 #if defined( SLAPD_SCHEMA_DN )
470 oc_schema_info( Entry *e )
472 struct berval vals[2];
475 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
477 vals[1].bv_val = NULL;
479 for ( oc = oc_list; oc; oc = oc->soc_next ) {
480 if ( ldap_objectclass2bv( &oc->soc_oclass, vals ) == NULL ) {
484 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
485 (long) vals[0].bv_len, vals[0].bv_val, 0 );
487 attr_merge( e, ad_objectClasses, vals );
488 ldap_memfree( vals[0].bv_val );