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, "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( 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 )
125 assert( oir->oir_name );
126 assert( oir->oir_oc );
128 rc = strncasecmp( name->bv_val, oir->oir_name, name->bv_len );
130 return oir->oir_name[name->bv_len] ? -1 : 0;
136 struct oindexrec *oir )
138 assert( oir->oir_name );
139 assert( oir->oir_oc );
141 return (strcasecmp( name, oir->oir_name ));
145 oc_find( const char *ocname )
147 struct oindexrec *oir;
149 oir = (struct oindexrec *) avl_find( oc_index, ocname,
150 (AVL_CMP) oc_index_name_cmp );
153 assert( oir->oir_name );
154 assert( oir->oir_oc );
156 return( oir->oir_oc );
163 oc_bvfind( struct berval *ocname )
165 struct oindexrec *oir;
167 oir = (struct oindexrec *) avl_find( oc_index, ocname,
168 (AVL_CMP) oc_index_bvname_cmp );
171 assert( oir->oir_name );
172 assert( oir->oir_oc );
174 return( oir->oir_oc );
188 AttributeType **satp;
194 sat = at_find(*attrs1);
197 return SLAP_SCHERR_ATTR_NOT_FOUND;
199 if ( at_find_in_list(sat, soc->soc_required) < 0) {
200 if ( at_append_to_list(sat, &soc->soc_required) ) {
202 return SLAP_SCHERR_OUTOFMEM;
207 /* Now delete duplicates from the allowed list */
208 for ( satp = soc->soc_required; *satp; satp++ ) {
209 i = at_find_in_list(*satp,soc->soc_allowed);
211 at_delete_from_list(i, &soc->soc_allowed);
230 sat = at_find(*attrs1);
233 return SLAP_SCHERR_ATTR_NOT_FOUND;
235 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
236 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
237 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
239 return SLAP_SCHERR_OUTOFMEM;
261 if ( !soc->soc_sups ) {
262 /* We are at the first recursive level */
270 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
271 sizeof(ObjectClass *));
277 soc1 = oc_find(*sups1);
280 return SLAP_SCHERR_CLASS_NOT_FOUND;
283 /* check object class usage
284 * abstract classes can only sup abstract classes
285 * structural classes can not sup auxiliary classes
286 * auxiliary classes can not sup structural classes
288 if( soc->soc_kind != soc1->soc_kind
289 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
292 return SLAP_SCHERR_CLASS_BAD_USAGE;
296 soc->soc_sups[nsups] = soc1;
298 code = oc_add_sups( soc, soc1->soc_sup_oids, err );
299 if ( code ) return code;
301 code = oc_create_required( soc, soc1->soc_at_oids_must, err );
302 if ( code ) return code;
304 code = oc_create_allowed( soc, soc1->soc_at_oids_may, err );
305 if ( code ) return code;
320 avl_free(oc_index, ldap_memfree);
321 for (o=oc_list; o; o=n)
324 ldap_memfree(o->soc_sups);
325 ldap_memfree(o->soc_required);
326 ldap_memfree(o->soc_allowed);
327 ldap_objectclass_free((LDAPObjectClass *)o);
338 struct oindexrec *oir;
342 while ( *ocp != NULL ) {
343 ocp = &(*ocp)->soc_next;
347 if ( soc->soc_oid ) {
348 oir = (struct oindexrec *)
349 ch_calloc( 1, sizeof(struct oindexrec) );
350 oir->oir_name = soc->soc_oid;
353 assert( oir->oir_name );
354 assert( oir->oir_oc );
356 if ( avl_insert( &oc_index, (caddr_t) oir,
357 (AVL_CMP) oc_index_cmp,
358 (AVL_DUP) avl_dup_error ) )
362 return SLAP_SCHERR_DUP_CLASS;
365 /* FIX: temporal consistency check */
366 assert( oc_find(oir->oir_name) != NULL );
369 if ( (names = soc->soc_names) ) {
371 oir = (struct oindexrec *)
372 ch_calloc( 1, sizeof(struct oindexrec) );
373 oir->oir_name = *names;
376 assert( oir->oir_name );
377 assert( oir->oir_oc );
379 if ( avl_insert( &oc_index, (caddr_t) oir,
380 (AVL_CMP) oc_index_cmp,
381 (AVL_DUP) avl_dup_error ) )
385 return SLAP_SCHERR_DUP_CLASS;
388 /* FIX: temporal consistency check */
389 assert( oc_find(oir->oir_name) != NULL );
407 if ( oc->oc_names != NULL ) {
410 for( i=0; oc->oc_names[i]; i++ ) {
411 if( !slap_valid_descr( oc->oc_names[i] ) ) {
412 return SLAP_SCHERR_BAD_DESCR;
417 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
418 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
420 if( soc->soc_sup_oids == NULL &&
421 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
423 /* structural object classes implicitly inherit from 'top' */
424 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
425 code = oc_add_sups( soc, top_oids, err );
427 code = oc_add_sups( soc, soc->soc_sup_oids, err );
430 if ( code != 0 ) return code;
432 code = oc_create_required( soc, soc->soc_at_oids_must, err );
433 if ( code != 0 ) return code;
435 code = oc_create_allowed( soc, soc->soc_at_oids_may, err );
436 if ( code != 0 ) return code;
438 code = oc_insert(soc,err);
445 oc_print( ObjectClass *oc )
450 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
451 if ( oc->soc_required != NULL ) {
453 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
455 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
458 if ( oc->soc_allowed != NULL ) {
460 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
462 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
470 #if defined( SLAPD_SCHEMA_DN )
473 oc_schema_info( Entry *e )
476 struct berval *vals[2];
479 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
484 for ( oc = oc_list; oc; oc = oc->soc_next ) {
485 val.bv_val = ldap_objectclass2str( &oc->soc_oclass );
486 if ( val.bv_val == NULL ) {
489 val.bv_len = strlen( val.bv_val );
491 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
492 (long) val.bv_len, val.bv_val, 0 );
494 attr_merge( e, ad_objectClasses, vals );
495 ldap_memfree( val.bv_val );