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;
29 LDAP_LOG ( OPERATION, ARGS,
30 "is_object_subclass(%s,%s) %d\n",
31 sup->soc_oid, sub->soc_oid, sup == sub );
33 Debug( LDAP_DEBUG_TRACE, "is_object_subclass(%s,%s) %d\n",
34 sup->soc_oid, sub->soc_oid, sup == sub );
42 if( sub->soc_sups == NULL ) {
46 for( i=0; sub->soc_sups[i] != NULL; i++ ) {
47 if( is_object_subclass( sup, sub->soc_sups[i] ) ) {
55 int is_entry_objectclass(
62 AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
63 assert(!( e == NULL || oc == NULL ));
65 if( e == NULL || oc == NULL ) {
69 if( set_flags && ( e->e_ocflags & SLAP_OC__END )) {
70 return (e->e_ocflags & oc->soc_flags) ? 1 : 0;
74 * find objectClass attribute
76 attr = attr_find(e->e_attrs, objectClass);
79 /* no objectClass attribute */
81 LDAP_LOG( OPERATION, ERR,
82 "is_entry_objectclass: dn(%s), oid (%s), no objectClass "
83 "attribute.\n", e->e_dn == NULL ? "" : e->e_dn,
84 oc->soc_oclass.oc_oid, 0 );
86 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
87 "no objectClass attribute\n",
88 e->e_dn == NULL ? "" : e->e_dn,
89 oc->soc_oclass.oc_oid, 0 );
95 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
96 ObjectClass *objectClass = oc_bvfind( bv );
98 if ( objectClass == oc && !set_flags ) {
102 if ( objectClass != NULL ) {
103 e->e_ocflags |= objectClass->soc_flags;
106 e->e_ocflags |= SLAP_OC__END; /* We've finished this */
108 return (e->e_ocflags & oc->soc_flags);
113 struct berval oir_name;
117 static Avlnode *oc_index = NULL;
118 static ObjectClass *oc_list = NULL;
122 struct oindexrec *oir1,
123 struct oindexrec *oir2 )
125 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
128 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
134 struct oindexrec *oir )
136 int i = name->bv_len - oir->oir_name.bv_len;
139 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
143 oc_find( const char *ocname )
147 bv.bv_val = (char *)ocname;
148 bv.bv_len = strlen( ocname );
150 return( oc_bvfind( &bv ) );
154 oc_bvfind( struct berval *ocname )
156 struct oindexrec *oir;
158 oir = (struct oindexrec *) avl_find( oc_index, ocname,
159 (AVL_CMP) oc_index_name_cmp );
162 return( oir->oir_oc );
177 AttributeType **satp;
183 sat = at_find(*attrs1);
186 return SLAP_SCHERR_ATTR_NOT_FOUND;
189 if( is_at_operational( sat )) (*op)++;
191 if ( at_find_in_list(sat, soc->soc_required) < 0) {
192 if ( at_append_to_list(sat, &soc->soc_required) ) {
194 return SLAP_SCHERR_OUTOFMEM;
199 /* Now delete duplicates from the allowed list */
200 for ( satp = soc->soc_required; *satp; satp++ ) {
201 i = at_find_in_list(*satp,soc->soc_allowed);
203 at_delete_from_list(i, &soc->soc_allowed);
223 sat = at_find(*attrs1);
226 return SLAP_SCHERR_ATTR_NOT_FOUND;
229 if( is_at_operational( sat )) (*op)++;
231 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
232 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
233 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
235 return SLAP_SCHERR_OUTOFMEM;
258 if ( !soc->soc_sups ) {
259 /* We are at the first recursive level */
267 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
268 sizeof(ObjectClass *));
274 soc1 = oc_find(*sups1);
277 return SLAP_SCHERR_CLASS_NOT_FOUND;
280 /* check object class usage
281 * abstract classes can only sup abstract classes
282 * structural classes can not sup auxiliary classes
283 * auxiliary classes can not sup structural classes
285 if( soc->soc_kind != soc1->soc_kind
286 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
289 return SLAP_SCHERR_CLASS_BAD_SUP;
292 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
294 return SLAP_SCHERR_CLASS_BAD_SUP;
297 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
300 soc->soc_sups[nsups] = soc1;
303 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
304 if ( code ) return code;
306 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
307 if ( code ) return code;
309 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
310 if ( code ) return code;
325 avl_free(oc_index, ldap_memfree);
326 for (o=oc_list; o; o=n)
329 if (o->soc_sups) ldap_memfree(o->soc_sups);
330 if (o->soc_required) ldap_memfree(o->soc_required);
331 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
332 ldap_objectclass_free((LDAPObjectClass *)o);
343 struct oindexrec *oir;
347 while ( *ocp != NULL ) {
348 ocp = &(*ocp)->soc_next;
352 if ( soc->soc_oid ) {
353 oir = (struct oindexrec *)
354 ch_calloc( 1, sizeof(struct oindexrec) );
355 oir->oir_name.bv_val = soc->soc_oid;
356 oir->oir_name.bv_len = strlen( soc->soc_oid );
359 assert( oir->oir_name.bv_val );
360 assert( oir->oir_oc );
362 if ( avl_insert( &oc_index, (caddr_t) oir,
363 (AVL_CMP) oc_index_cmp,
364 (AVL_DUP) avl_dup_error ) )
368 return SLAP_SCHERR_CLASS_DUP;
371 /* FIX: temporal consistency check */
372 assert( oc_bvfind(&oir->oir_name) != NULL );
375 if ( (names = soc->soc_names) ) {
377 oir = (struct oindexrec *)
378 ch_calloc( 1, sizeof(struct oindexrec) );
379 oir->oir_name.bv_val = *names;
380 oir->oir_name.bv_len = strlen( *names );
383 assert( oir->oir_name.bv_val );
384 assert( oir->oir_oc );
386 if ( avl_insert( &oc_index, (caddr_t) oir,
387 (AVL_CMP) oc_index_cmp,
388 (AVL_DUP) avl_dup_error ) )
392 return SLAP_SCHERR_CLASS_DUP;
395 /* FIX: temporal consistency check */
396 assert( oc_bvfind(&oir->oir_name) != NULL );
416 if ( oc->oc_names != NULL ) {
419 for( i=0; oc->oc_names[i]; i++ ) {
420 if( !slap_valid_descr( oc->oc_names[i] ) ) {
421 return SLAP_SCHERR_BAD_DESCR;
426 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
427 /* Expand OID macros */
428 char *oid = oidm_find( oc->oc_oid );
431 return SLAP_SCHERR_OIDM;
433 if ( oid != oc->oc_oid ) {
434 ldap_memfree( oc->oc_oid );
439 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
440 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
442 if( oc->oc_names != NULL ) {
443 soc->soc_cname.bv_val = soc->soc_names[0];
445 soc->soc_cname.bv_val = soc->soc_oid;
447 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
449 if( soc->soc_sup_oids == NULL &&
450 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
452 /* structural object classes implicitly inherit from 'top' */
453 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
454 code = oc_add_sups( soc, top_oids, &op, err );
456 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
459 if ( code != 0 ) return code;
461 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
462 if ( code != 0 ) return code;
464 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
465 if ( code != 0 ) return code;
467 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
469 code = oc_insert(soc,err);
474 oc_schema_info( Entry *e )
476 struct berval vals[2];
479 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
481 vals[1].bv_val = NULL;
483 for ( oc = oc_list; oc; oc = oc->soc_next ) {
484 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
486 if ( ldap_objectclass2bv( &oc->soc_oclass, vals ) == NULL ) {
491 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
492 (long) vals[0].bv_len, vals[0].bv_val, 0 );
494 if( attr_merge( e, ad_objectClasses, vals ) )
496 ldap_memfree( vals[0].bv_val );