1 /* oc.c - object class routines */
4 * Copyright 1998-2000 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,
71 "is_entry_objectclass: dn(%s), oid (%s), no objectlcass attribute.\n",
72 e->e_dn == NULL ? "" : e->e_dn, oc->soc_oclass.oc_oid ));
74 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
75 "no objectClass attribute\n",
76 e->e_dn == NULL ? "" : e->e_dn,
77 oc->soc_oclass.oc_oid, 0 );
84 for( i=0; attr->a_vals[i]; i++ ) {
85 ObjectClass *objectClass = oc_find( attr->a_vals[i]->bv_val );
87 if( objectClass == oc ) {
102 static Avlnode *oc_index = NULL;
103 static ObjectClass *oc_list = NULL;
107 struct oindexrec *oir1,
108 struct oindexrec *oir2 )
110 assert( oir1->oir_name );
111 assert( oir1->oir_oc );
112 assert( oir2->oir_name );
113 assert( oir2->oir_oc );
115 return (strcasecmp( oir1->oir_name, oir2->oir_name ));
121 struct oindexrec *oir )
123 assert( oir->oir_name );
124 assert( oir->oir_oc );
126 return (strcasecmp( name, oir->oir_name ));
130 oc_find( const char *ocname )
132 struct oindexrec *oir;
134 oir = (struct oindexrec *) avl_find( oc_index, ocname,
135 (AVL_CMP) oc_index_name_cmp );
138 assert( oir->oir_name );
139 assert( oir->oir_oc );
141 return( oir->oir_oc );
155 AttributeType **satp;
161 sat = at_find(*attrs1);
164 return SLAP_SCHERR_ATTR_NOT_FOUND;
166 if ( at_find_in_list(sat, soc->soc_required) < 0) {
167 if ( at_append_to_list(sat, &soc->soc_required) ) {
169 return SLAP_SCHERR_OUTOFMEM;
174 /* Now delete duplicates from the allowed list */
175 for ( satp = soc->soc_required; *satp; satp++ ) {
176 i = at_find_in_list(*satp,soc->soc_allowed);
178 at_delete_from_list(i, &soc->soc_allowed);
197 sat = at_find(*attrs1);
200 return SLAP_SCHERR_ATTR_NOT_FOUND;
202 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
203 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
204 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
206 return SLAP_SCHERR_OUTOFMEM;
228 if ( !soc->soc_sups ) {
229 /* We are at the first recursive level */
237 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
238 sizeof(ObjectClass *));
244 soc1 = oc_find(*sups1);
247 return SLAP_SCHERR_CLASS_NOT_FOUND;
250 /* check object class usage
251 * abstract classes can only sup abstract classes
252 * structural classes can not sup auxiliary classes
253 * auxiliary classes can not sup structural classes
255 if( soc->soc_kind != soc1->soc_kind
256 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
259 return SLAP_SCHERR_CLASS_BAD_USAGE;
263 soc->soc_sups[nsups] = soc1;
265 code = oc_add_sups( soc, soc1->soc_sup_oids, err );
266 if ( code ) return code;
268 code = oc_create_required( soc, soc1->soc_at_oids_must, err );
269 if ( code ) return code;
271 code = oc_create_allowed( soc, soc1->soc_at_oids_may, err );
272 if ( code ) return code;
289 struct oindexrec *oir;
293 while ( *ocp != NULL ) {
294 ocp = &(*ocp)->soc_next;
298 if ( soc->soc_oid ) {
299 oir = (struct oindexrec *)
300 ch_calloc( 1, sizeof(struct oindexrec) );
301 oir->oir_name = soc->soc_oid;
304 assert( oir->oir_name );
305 assert( oir->oir_oc );
307 if ( avl_insert( &oc_index, (caddr_t) oir,
308 (AVL_CMP) oc_index_cmp,
309 (AVL_DUP) avl_dup_error ) )
312 ldap_memfree(oir->oir_name);
314 return SLAP_SCHERR_DUP_CLASS;
317 /* FIX: temporal consistency check */
318 assert( oc_find(oir->oir_name) != NULL );
321 if ( (names = soc->soc_names) ) {
323 oir = (struct oindexrec *)
324 ch_calloc( 1, sizeof(struct oindexrec) );
325 oir->oir_name = ch_strdup(*names);
328 assert( oir->oir_name );
329 assert( oir->oir_oc );
331 if ( avl_insert( &oc_index, (caddr_t) oir,
332 (AVL_CMP) oc_index_cmp,
333 (AVL_DUP) avl_dup_error ) )
336 ldap_memfree(oir->oir_name);
338 return SLAP_SCHERR_DUP_CLASS;
341 /* FIX: temporal consistency check */
342 assert( oc_find(oir->oir_name) != NULL );
360 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
361 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
363 if( soc->soc_sup_oids == NULL &&
364 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
366 /* structural object classes implicitly inherit from 'top' */
367 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
368 code = oc_add_sups( soc, top_oids, err );
370 code = oc_add_sups( soc, soc->soc_sup_oids, err );
373 if ( code != 0 ) return code;
375 code = oc_create_required( soc, soc->soc_at_oids_must, err );
376 if ( code != 0 ) return code;
378 code = oc_create_allowed( soc, soc->soc_at_oids_may, err );
379 if ( code != 0 ) return code;
381 code = oc_insert(soc,err);
388 oc_print( ObjectClass *oc )
393 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
394 if ( oc->soc_required != NULL ) {
396 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
398 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
401 if ( oc->soc_allowed != NULL ) {
403 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
405 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
413 #if defined( SLAPD_SCHEMA_DN )
416 oc_schema_info( Entry *e )
419 struct berval *vals[2];
422 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
427 for ( oc = oc_list; oc; oc = oc->soc_next ) {
428 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
429 if ( val.bv_val == NULL ) {
432 val.bv_len = strlen( val.bv_val );
434 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
435 (long) val.bv_len, val.bv_val, 0 );
437 attr_merge( e, ad_objectClasses, vals );
438 ldap_memfree( val.bv_val );