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 sup->soc_oid, sub->soc_oid, sup == sub );
36 if( sub->soc_sups == NULL ) {
40 for( i=0; sub->soc_sups[i] != NULL; i++ ) {
41 if( is_object_subclass( sup, sub->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) ? 1 : 0;
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 );
171 AttributeType **satp;
177 sat = at_find(*attrs1);
180 return SLAP_SCHERR_ATTR_NOT_FOUND;
183 if( is_at_operational( sat )) (*op)++;
185 if ( at_find_in_list(sat, soc->soc_required) < 0) {
186 if ( at_append_to_list(sat, &soc->soc_required) ) {
188 return SLAP_SCHERR_OUTOFMEM;
193 /* Now delete duplicates from the allowed list */
194 for ( satp = soc->soc_required; *satp; satp++ ) {
195 i = at_find_in_list(*satp,soc->soc_allowed);
197 at_delete_from_list(i, &soc->soc_allowed);
217 sat = at_find(*attrs1);
220 return SLAP_SCHERR_ATTR_NOT_FOUND;
223 if( is_at_operational( sat )) (*op)++;
225 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
226 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
227 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
229 return SLAP_SCHERR_OUTOFMEM;
252 if ( !soc->soc_sups ) {
253 /* We are at the first recursive level */
261 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
262 sizeof(ObjectClass *));
268 soc1 = oc_find(*sups1);
271 return SLAP_SCHERR_CLASS_NOT_FOUND;
274 /* check object class usage
275 * abstract classes can only sup abstract classes
276 * structural classes can not sup auxiliary classes
277 * auxiliary classes can not sup structural classes
279 if( soc->soc_kind != soc1->soc_kind
280 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
283 return SLAP_SCHERR_CLASS_BAD_USAGE;
286 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
289 soc->soc_sups[nsups] = soc1;
292 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
293 if ( code ) return code;
295 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
296 if ( code ) return code;
298 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
299 if ( code ) return code;
314 avl_free(oc_index, ldap_memfree);
315 for (o=oc_list; o; o=n)
318 if (o->soc_sups) ldap_memfree(o->soc_sups);
319 if (o->soc_required) ldap_memfree(o->soc_required);
320 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
321 ldap_objectclass_free((LDAPObjectClass *)o);
332 struct oindexrec *oir;
336 while ( *ocp != NULL ) {
337 ocp = &(*ocp)->soc_next;
341 if ( soc->soc_oid ) {
342 oir = (struct oindexrec *)
343 ch_calloc( 1, sizeof(struct oindexrec) );
344 oir->oir_name.bv_val = soc->soc_oid;
345 oir->oir_name.bv_len = strlen( soc->soc_oid );
348 assert( oir->oir_name.bv_val );
349 assert( oir->oir_oc );
351 if ( avl_insert( &oc_index, (caddr_t) oir,
352 (AVL_CMP) oc_index_cmp,
353 (AVL_DUP) avl_dup_error ) )
357 return SLAP_SCHERR_CLASS_DUP;
360 /* FIX: temporal consistency check */
361 assert( oc_bvfind(&oir->oir_name) != NULL );
364 if ( (names = soc->soc_names) ) {
366 oir = (struct oindexrec *)
367 ch_calloc( 1, sizeof(struct oindexrec) );
368 oir->oir_name.bv_val = *names;
369 oir->oir_name.bv_len = strlen( *names );
372 assert( oir->oir_name.bv_val );
373 assert( oir->oir_oc );
375 if ( avl_insert( &oc_index, (caddr_t) oir,
376 (AVL_CMP) oc_index_cmp,
377 (AVL_DUP) avl_dup_error ) )
381 return SLAP_SCHERR_CLASS_DUP;
384 /* FIX: temporal consistency check */
385 assert( oc_bvfind(&oir->oir_name) != NULL );
405 if ( oc->oc_names != NULL ) {
408 for( i=0; oc->oc_names[i]; i++ ) {
409 if( !slap_valid_descr( oc->oc_names[i] ) ) {
410 return SLAP_SCHERR_BAD_DESCR;
415 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
416 /* Expand OID macros */
417 char *oid = oidm_find( oc->oc_oid );
420 return SLAP_SCHERR_OIDM;
422 if ( oid != oc->oc_oid ) {
423 ldap_memfree( oc->oc_oid );
428 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
429 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
431 if( soc->soc_sup_oids == NULL &&
432 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
434 /* structural object classes implicitly inherit from 'top' */
435 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
436 code = oc_add_sups( soc, top_oids, &op, err );
438 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
441 if ( code != 0 ) return code;
443 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
444 if ( code != 0 ) return code;
446 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
447 if ( code != 0 ) return code;
449 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
451 code = oc_insert(soc,err);
458 oc_print( ObjectClass *oc )
463 printf( "objectclass %s\n", ldap_objectclass2name( &oc->soc_oclass ) );
464 if ( oc->soc_required != NULL ) {
466 for ( i = 0; oc->soc_required[i] != NULL; i++, mid = "," )
468 ldap_attributetype2name( &oc->soc_required[i]->sat_atype ) );
471 if ( oc->soc_allowed != NULL ) {
473 for ( i = 0; oc->soc_allowed[i] != NULL; i++, mid = "," )
475 ldap_attributetype2name( &oc->soc_allowed[i]->sat_atype ) );
483 #if defined( SLAPD_SCHEMA_DN )
486 oc_schema_info( Entry *e )
488 struct berval vals[2];
491 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
493 vals[1].bv_val = NULL;
495 for ( oc = oc_list; oc; oc = oc->soc_next ) {
496 if ( ldap_objectclass2bv( &oc->soc_oclass, vals ) == NULL ) {
500 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
503 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
504 (long) vals[0].bv_len, vals[0].bv_val, 0 );
506 attr_merge( e, ad_objectClasses, vals );
507 ldap_memfree( vals[0].bv_val );