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(
55 AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
56 assert(!( e == NULL || oc == NULL ));
58 if( e == NULL || oc == NULL ) {
63 * find objectClass attribute
65 attr = attr_find(e->e_attrs, objectClass);
68 /* no objectClass attribute */
70 LDAP_LOG(( "operation", LDAP_LEVEL_ERR, "is_entry_objectclass: "
71 "dn(%s), oid (%s), no objectClass attribute.\n",
72 e->e_dn == NULL ? "" : e->e_dn,
73 oc->soc_oclass.oc_oid ));
75 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
76 "no objectClass attribute\n",
77 e->e_dn == NULL ? "" : e->e_dn,
78 oc->soc_oclass.oc_oid, 0 );
84 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
85 ObjectClass *objectClass = oc_bvfind( bv );
87 if( objectClass == oc ) {
98 struct berval oir_name;
102 static Avlnode *oc_index = NULL;
103 static ObjectClass *oc_list = NULL;
107 struct oindexrec *oir1,
108 struct oindexrec *oir2 )
110 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
113 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
119 struct oindexrec *oir )
121 int i = name->bv_len - oir->oir_name.bv_len;
124 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
128 oc_find( const char *ocname )
132 bv.bv_val = (char *)ocname;
133 bv.bv_len = strlen( ocname );
135 return( oc_bvfind( &bv ) );
139 oc_bvfind( struct berval *ocname )
141 struct oindexrec *oir;
143 oir = (struct oindexrec *) avl_find( oc_index, ocname,
144 (AVL_CMP) oc_index_name_cmp );
147 return( oir->oir_oc );
161 AttributeType **satp;
167 sat = at_find(*attrs1);
170 return SLAP_SCHERR_ATTR_NOT_FOUND;
172 if ( at_find_in_list(sat, soc->soc_required) < 0) {
173 if ( at_append_to_list(sat, &soc->soc_required) ) {
175 return SLAP_SCHERR_OUTOFMEM;
180 /* Now delete duplicates from the allowed list */
181 for ( satp = soc->soc_required; *satp; satp++ ) {
182 i = at_find_in_list(*satp,soc->soc_allowed);
184 at_delete_from_list(i, &soc->soc_allowed);
203 sat = at_find(*attrs1);
206 return SLAP_SCHERR_ATTR_NOT_FOUND;
208 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
209 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
210 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
212 return SLAP_SCHERR_OUTOFMEM;
234 if ( !soc->soc_sups ) {
235 /* We are at the first recursive level */
243 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
244 sizeof(ObjectClass *));
250 soc1 = oc_find(*sups1);
253 return SLAP_SCHERR_CLASS_NOT_FOUND;
256 /* check object class usage
257 * abstract classes can only sup abstract classes
258 * structural classes can not sup auxiliary classes
259 * auxiliary classes can not sup structural classes
261 if( soc->soc_kind != soc1->soc_kind
262 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
265 return SLAP_SCHERR_CLASS_BAD_USAGE;
269 soc->soc_sups[nsups] = soc1;
271 code = oc_add_sups( soc, soc1->soc_sup_oids, err );
272 if ( code ) return code;
274 code = oc_create_required( soc, soc1->soc_at_oids_must, err );
275 if ( code ) return code;
277 code = oc_create_allowed( soc, soc1->soc_at_oids_may, err );
278 if ( code ) return code;
293 avl_free(oc_index, ldap_memfree);
294 for (o=oc_list; o; o=n)
297 if (o->soc_sups) ldap_memfree(o->soc_sups);
298 if (o->soc_required) ldap_memfree(o->soc_required);
299 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
300 ldap_objectclass_free((LDAPObjectClass *)o);
311 struct oindexrec *oir;
315 while ( *ocp != NULL ) {
316 ocp = &(*ocp)->soc_next;
320 if ( soc->soc_oid ) {
321 oir = (struct oindexrec *)
322 ch_calloc( 1, sizeof(struct oindexrec) );
323 oir->oir_name.bv_val = soc->soc_oid;
324 oir->oir_name.bv_len = strlen( soc->soc_oid );
327 assert( oir->oir_name.bv_val );
328 assert( oir->oir_oc );
330 if ( avl_insert( &oc_index, (caddr_t) oir,
331 (AVL_CMP) oc_index_cmp,
332 (AVL_DUP) avl_dup_error ) )
336 return SLAP_SCHERR_DUP_CLASS;
339 /* FIX: temporal consistency check */
340 assert( oc_bvfind(&oir->oir_name) != NULL );
343 if ( (names = soc->soc_names) ) {
345 oir = (struct oindexrec *)
346 ch_calloc( 1, sizeof(struct oindexrec) );
347 oir->oir_name.bv_val = *names;
348 oir->oir_name.bv_len = strlen( *names );
351 assert( oir->oir_name.bv_val );
352 assert( oir->oir_oc );
354 if ( avl_insert( &oc_index, (caddr_t) oir,
355 (AVL_CMP) oc_index_cmp,
356 (AVL_DUP) avl_dup_error ) )
360 return SLAP_SCHERR_DUP_CLASS;
363 /* FIX: temporal consistency check */
364 assert( oc_bvfind(&oir->oir_name) != NULL );
382 if ( oc->oc_names != NULL ) {
385 for( i=0; oc->oc_names[i]; i++ ) {
386 if( !slap_valid_descr( oc->oc_names[i] ) ) {
387 return SLAP_SCHERR_BAD_DESCR;
392 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
393 /* Expand OID macros */
394 char *oid = oidm_find( oc->oc_oid );
397 return SLAP_SCHERR_OIDM;
399 if ( oid != oc->oc_oid ) {
400 ldap_memfree( oc->oc_oid );
405 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
406 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
408 if( soc->soc_sup_oids == NULL &&
409 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
411 /* structural object classes implicitly inherit from 'top' */
412 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
413 code = oc_add_sups( soc, top_oids, err );
415 code = oc_add_sups( soc, soc->soc_sup_oids, err );
418 if ( code != 0 ) return code;
420 code = oc_create_required( soc, soc->soc_at_oids_must, err );
421 if ( code != 0 ) return code;
423 code = oc_create_allowed( soc, soc->soc_at_oids_may, err );
424 if ( code != 0 ) return code;
426 code = oc_insert(soc,err);
433 oc_print( ObjectClass *oc )
438 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
439 if ( oc->soc_required != NULL ) {
441 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
443 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
446 if ( oc->soc_allowed != NULL ) {
448 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
450 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
458 #if defined( SLAPD_SCHEMA_DN )
461 oc_schema_info( Entry *e )
463 struct berval vals[2];
466 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
468 vals[1].bv_val = NULL;
470 for ( oc = oc_list; oc; oc = oc->soc_next ) {
471 if ( ldap_objectclass2bv( &oc->soc_oclass, vals ) == NULL ) {
475 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
476 (long) vals[0].bv_len, vals[0].bv_val, 0 );
478 attr_merge( e, ad_objectClasses, vals );
479 ldap_memfree( vals[0].bv_val );