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 e->e_ocflags |= objectClass->soc_flags;
98 e->e_ocflags |= SLAP_OC__END; /* We've finished this */
100 return (e->e_ocflags & oc->soc_flags);
105 struct berval oir_name;
109 static Avlnode *oc_index = NULL;
110 static ObjectClass *oc_list = NULL;
114 struct oindexrec *oir1,
115 struct oindexrec *oir2 )
117 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
120 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
126 struct oindexrec *oir )
128 int i = name->bv_len - oir->oir_name.bv_len;
131 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
135 oc_find( const char *ocname )
139 bv.bv_val = (char *)ocname;
140 bv.bv_len = strlen( ocname );
142 return( oc_bvfind( &bv ) );
146 oc_bvfind( struct berval *ocname )
148 struct oindexrec *oir;
150 oir = (struct oindexrec *) avl_find( oc_index, ocname,
151 (AVL_CMP) oc_index_name_cmp );
154 return( oir->oir_oc );
168 AttributeType **satp;
174 sat = at_find(*attrs1);
177 return SLAP_SCHERR_ATTR_NOT_FOUND;
179 if ( at_find_in_list(sat, soc->soc_required) < 0) {
180 if ( at_append_to_list(sat, &soc->soc_required) ) {
182 return SLAP_SCHERR_OUTOFMEM;
187 /* Now delete duplicates from the allowed list */
188 for ( satp = soc->soc_required; *satp; satp++ ) {
189 i = at_find_in_list(*satp,soc->soc_allowed);
191 at_delete_from_list(i, &soc->soc_allowed);
210 sat = at_find(*attrs1);
213 return SLAP_SCHERR_ATTR_NOT_FOUND;
215 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
216 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
217 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
219 return SLAP_SCHERR_OUTOFMEM;
241 if ( !soc->soc_sups ) {
242 /* We are at the first recursive level */
250 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
251 sizeof(ObjectClass *));
257 soc1 = oc_find(*sups1);
260 return SLAP_SCHERR_CLASS_NOT_FOUND;
263 /* check object class usage
264 * abstract classes can only sup abstract classes
265 * structural classes can not sup auxiliary classes
266 * auxiliary classes can not sup structural classes
268 if( soc->soc_kind != soc1->soc_kind
269 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
272 return SLAP_SCHERR_CLASS_BAD_USAGE;
276 soc->soc_sups[nsups] = soc1;
278 code = oc_add_sups( soc, soc1->soc_sup_oids, err );
279 if ( code ) return code;
281 code = oc_create_required( soc, soc1->soc_at_oids_must, err );
282 if ( code ) return code;
284 code = oc_create_allowed( soc, soc1->soc_at_oids_may, err );
285 if ( code ) return code;
300 avl_free(oc_index, ldap_memfree);
301 for (o=oc_list; o; o=n)
304 if (o->soc_sups) ldap_memfree(o->soc_sups);
305 if (o->soc_required) ldap_memfree(o->soc_required);
306 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
307 ldap_objectclass_free((LDAPObjectClass *)o);
318 struct oindexrec *oir;
322 while ( *ocp != NULL ) {
323 ocp = &(*ocp)->soc_next;
327 if ( soc->soc_oid ) {
328 oir = (struct oindexrec *)
329 ch_calloc( 1, sizeof(struct oindexrec) );
330 oir->oir_name.bv_val = soc->soc_oid;
331 oir->oir_name.bv_len = strlen( soc->soc_oid );
334 assert( oir->oir_name.bv_val );
335 assert( oir->oir_oc );
337 if ( avl_insert( &oc_index, (caddr_t) oir,
338 (AVL_CMP) oc_index_cmp,
339 (AVL_DUP) avl_dup_error ) )
343 return SLAP_SCHERR_DUP_CLASS;
346 /* FIX: temporal consistency check */
347 assert( oc_bvfind(&oir->oir_name) != NULL );
350 if ( (names = soc->soc_names) ) {
352 oir = (struct oindexrec *)
353 ch_calloc( 1, sizeof(struct oindexrec) );
354 oir->oir_name.bv_val = *names;
355 oir->oir_name.bv_len = strlen( *names );
358 assert( oir->oir_name.bv_val );
359 assert( oir->oir_oc );
361 if ( avl_insert( &oc_index, (caddr_t) oir,
362 (AVL_CMP) oc_index_cmp,
363 (AVL_DUP) avl_dup_error ) )
367 return SLAP_SCHERR_DUP_CLASS;
370 /* FIX: temporal consistency check */
371 assert( oc_bvfind(&oir->oir_name) != NULL );
389 if ( oc->oc_names != NULL ) {
392 for( i=0; oc->oc_names[i]; i++ ) {
393 if( !slap_valid_descr( oc->oc_names[i] ) ) {
394 return SLAP_SCHERR_BAD_DESCR;
399 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
400 /* Expand OID macros */
401 char *oid = oidm_find( oc->oc_oid );
404 return SLAP_SCHERR_OIDM;
406 if ( oid != oc->oc_oid ) {
407 ldap_memfree( oc->oc_oid );
412 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
413 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
415 if( soc->soc_sup_oids == NULL &&
416 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
418 /* structural object classes implicitly inherit from 'top' */
419 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
420 code = oc_add_sups( soc, top_oids, err );
422 code = oc_add_sups( soc, soc->soc_sup_oids, err );
425 if ( code != 0 ) return code;
427 code = oc_create_required( soc, soc->soc_at_oids_must, err );
428 if ( code != 0 ) return code;
430 code = oc_create_allowed( soc, soc->soc_at_oids_may, err );
431 if ( code != 0 ) return code;
433 code = oc_insert(soc,err);
440 oc_print( ObjectClass *oc )
445 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
446 if ( oc->soc_required != NULL ) {
448 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
450 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
453 if ( oc->soc_allowed != NULL ) {
455 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
457 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
465 #if defined( SLAPD_SCHEMA_DN )
468 oc_schema_info( Entry *e )
470 struct berval vals[2];
473 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
475 vals[1].bv_val = NULL;
477 for ( oc = oc_list; oc; oc = oc->soc_next ) {
478 if ( ldap_objectclass2bv( &oc->soc_oclass, vals ) == NULL ) {
482 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
483 (long) vals[0].bv_len, vals[0].bv_val, 0 );
485 attr_merge( e, ad_objectClasses, vals );
486 ldap_memfree( vals[0].bv_val );