1 /* oc.c - object class routines */
4 * Copyright 1998-2003 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(
61 * set_flags should only be true if oc is one of operational
62 * object classes which we support objectClass flags for
63 * (e.g., referral, alias, ...). See <slap.h>.
68 AttributeDescription *objectClass = slap_schema.si_ad_objectClass;
70 assert(!( e == NULL || oc == NULL ));
72 if( e == NULL || oc == NULL ) {
76 if( set_flags && ( e->e_ocflags & SLAP_OC__END )) {
77 /* flags are set, use them */
78 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
82 * find objectClass attribute
84 attr = attr_find(e->e_attrs, objectClass);
86 /* no objectClass attribute */
88 LDAP_LOG( OPERATION, ERR,
89 "is_entry_objectclass: dn(%s), oid (%s), no objectClass "
90 "attribute.\n", e->e_dn == NULL ? "" : e->e_dn,
91 oc->soc_oclass.oc_oid, 0 );
93 Debug( LDAP_DEBUG_ANY, "is_entry_objectclass(\"%s\", \"%s\") "
94 "no objectClass attribute\n",
95 e->e_dn == NULL ? "" : e->e_dn,
96 oc->soc_oclass.oc_oid, 0 );
102 for( bv=attr->a_vals; bv->bv_val; bv++ ) {
103 ObjectClass *objectClass = oc_bvfind( bv );
105 if ( !set_flags && objectClass == oc ) {
109 if ( objectClass != NULL ) {
110 e->e_ocflags |= objectClass->soc_flags;
114 /* mark flags as set */
115 e->e_ocflags |= SLAP_OC__END;
117 return (e->e_ocflags & oc->soc_flags & SLAP_OC__MASK) != 0;
122 struct berval oir_name;
126 static Avlnode *oc_index = NULL;
127 static LDAP_SLIST_HEAD(OCList, slap_object_class) oc_list
128 = LDAP_SLIST_HEAD_INITIALIZER(&oc_list);
135 const struct oindexrec *oir1 = v_oir1, *oir2 = v_oir2;
136 int i = oir1->oir_name.bv_len - oir2->oir_name.bv_len;
139 return strcasecmp( oir1->oir_name.bv_val, oir2->oir_name.bv_val );
147 const struct berval *name = v_name;
148 const struct oindexrec *oir = v_oir;
149 int i = name->bv_len - oir->oir_name.bv_len;
152 return strncasecmp( name->bv_val, oir->oir_name.bv_val, name->bv_len );
156 oc_find( const char *ocname )
160 bv.bv_val = (char *)ocname;
161 bv.bv_len = strlen( ocname );
163 return( oc_bvfind( &bv ) );
167 oc_bvfind( struct berval *ocname )
169 struct oindexrec *oir;
171 oir = avl_find( oc_index, ocname, oc_index_name_cmp );
174 return( oir->oir_oc );
189 AttributeType **satp;
195 sat = at_find(*attrs1);
198 return SLAP_SCHERR_ATTR_NOT_FOUND;
201 if( is_at_operational( sat )) (*op)++;
203 if ( at_find_in_list(sat, soc->soc_required) < 0) {
204 if ( at_append_to_list(sat, &soc->soc_required) ) {
206 return SLAP_SCHERR_OUTOFMEM;
211 /* Now delete duplicates from the allowed list */
212 for ( satp = soc->soc_required; *satp; satp++ ) {
213 i = at_find_in_list(*satp,soc->soc_allowed);
215 at_delete_from_list(i, &soc->soc_allowed);
235 sat = at_find(*attrs1);
238 return SLAP_SCHERR_ATTR_NOT_FOUND;
241 if( is_at_operational( sat )) (*op)++;
243 if ( at_find_in_list(sat, soc->soc_required) < 0 &&
244 at_find_in_list(sat, soc->soc_allowed) < 0 ) {
245 if ( at_append_to_list(sat, &soc->soc_allowed) ) {
247 return SLAP_SCHERR_OUTOFMEM;
270 if ( !soc->soc_sups ) {
271 /* We are at the first recursive level */
279 soc->soc_sups = (ObjectClass **)ch_calloc(nsups,
280 sizeof(ObjectClass *));
286 soc1 = oc_find(*sups1);
289 return SLAP_SCHERR_CLASS_NOT_FOUND;
292 /* check object class usage
293 * abstract classes can only sup abstract classes
294 * structural classes can not sup auxiliary classes
295 * auxiliary classes can not sup structural classes
297 if( soc->soc_kind != soc1->soc_kind
298 && soc1->soc_kind != LDAP_SCHEMA_ABSTRACT )
301 return SLAP_SCHERR_CLASS_BAD_SUP;
304 if( soc1->soc_obsolete && !soc->soc_obsolete ) {
306 return SLAP_SCHERR_CLASS_BAD_SUP;
309 if( soc->soc_flags & SLAP_OC_OPERATIONAL ) (*op)++;
312 soc->soc_sups[nsups] = soc1;
315 code = oc_add_sups( soc, soc1->soc_sup_oids, op, err );
316 if ( code ) return code;
318 code = oc_create_required( soc, soc1->soc_at_oids_must, op, err );
319 if ( code ) return code;
321 code = oc_create_allowed( soc, soc1->soc_at_oids_may, op, err );
322 if ( code ) return code;
337 avl_free(oc_index, ldap_memfree);
338 while( !LDAP_SLIST_EMPTY(&oc_list) ) {
339 o = LDAP_SLIST_FIRST(&oc_list);
340 LDAP_SLIST_REMOVE_HEAD(&oc_list, soc_next);
342 if (o->soc_sups) ldap_memfree(o->soc_sups);
343 if (o->soc_required) ldap_memfree(o->soc_required);
344 if (o->soc_allowed) ldap_memfree(o->soc_allowed);
345 ldap_objectclass_free((LDAPObjectClass *)o);
355 struct oindexrec *oir;
358 LDAP_SLIST_NEXT( soc, soc_next ) = NULL;
359 LDAP_SLIST_INSERT_HEAD( &oc_list, soc, soc_next );
361 if ( soc->soc_oid ) {
362 oir = (struct oindexrec *)
363 ch_calloc( 1, sizeof(struct oindexrec) );
364 oir->oir_name.bv_val = soc->soc_oid;
365 oir->oir_name.bv_len = strlen( soc->soc_oid );
368 assert( oir->oir_name.bv_val );
369 assert( oir->oir_oc );
371 if ( avl_insert( &oc_index, (caddr_t) oir,
372 oc_index_cmp, avl_dup_error ) )
376 return SLAP_SCHERR_CLASS_DUP;
379 /* FIX: temporal consistency check */
380 assert( oc_bvfind(&oir->oir_name) != NULL );
383 if ( (names = soc->soc_names) ) {
385 oir = (struct oindexrec *)
386 ch_calloc( 1, sizeof(struct oindexrec) );
387 oir->oir_name.bv_val = *names;
388 oir->oir_name.bv_len = strlen( *names );
391 assert( oir->oir_name.bv_val );
392 assert( oir->oir_oc );
394 if ( avl_insert( &oc_index, (caddr_t) oir,
395 oc_index_cmp, avl_dup_error ) )
399 return SLAP_SCHERR_CLASS_DUP;
402 /* FIX: temporal consistency check */
403 assert( oc_bvfind(&oir->oir_name) != NULL );
423 if ( oc->oc_names != NULL ) {
426 for( i=0; oc->oc_names[i]; i++ ) {
427 if( !slap_valid_descr( oc->oc_names[i] ) ) {
428 return SLAP_SCHERR_BAD_DESCR;
433 if ( !OID_LEADCHAR( oc->oc_oid[0] )) {
434 /* Expand OID macros */
435 char *oid = oidm_find( oc->oc_oid );
438 return SLAP_SCHERR_OIDM;
440 if ( oid != oc->oc_oid ) {
441 ldap_memfree( oc->oc_oid );
446 soc = (ObjectClass *) ch_calloc( 1, sizeof(ObjectClass) );
447 AC_MEMCPY( &soc->soc_oclass, oc, sizeof(LDAPObjectClass) );
449 if( oc->oc_names != NULL ) {
450 soc->soc_cname.bv_val = soc->soc_names[0];
452 soc->soc_cname.bv_val = soc->soc_oid;
454 soc->soc_cname.bv_len = strlen( soc->soc_cname.bv_val );
456 if( soc->soc_sup_oids == NULL &&
457 soc->soc_kind == LDAP_SCHEMA_STRUCTURAL )
459 /* structural object classes implicitly inherit from 'top' */
460 static char *top_oids[] = { SLAPD_TOP_OID, NULL };
461 code = oc_add_sups( soc, top_oids, &op, err );
463 code = oc_add_sups( soc, soc->soc_sup_oids, &op, err );
466 if ( code != 0 ) return code;
468 code = oc_create_required( soc, soc->soc_at_oids_must, &op, err );
469 if ( code != 0 ) return code;
471 code = oc_create_allowed( soc, soc->soc_at_oids_may, &op, err );
472 if ( code != 0 ) return code;
474 if( user && op ) return SLAP_SCHERR_CLASS_BAD_SUP;
476 code = oc_insert(soc,err);
481 oc_schema_info( Entry *e )
483 AttributeDescription *ad_objectClasses = slap_schema.si_ad_objectClasses;
490 LDAP_SLIST_FOREACH( oc, &oc_list, soc_next ) {
491 if( oc->soc_flags & SLAP_OC_HIDE ) continue;
493 if ( ldap_objectclass2bv( &oc->soc_oclass, &val ) == NULL ) {
498 Debug( LDAP_DEBUG_TRACE, "Merging oc [%ld] %s\n",
499 (long) val.bv_len, val.bv_val, 0 );
502 nval.bv_val = oc->soc_oid;
503 nval.bv_len = strlen(oc->soc_oid);
505 if( attr_merge_one( e, ad_objectClasses, &val, &nval ) )
507 if( attr_merge_one( e, ad_objectClasses, &val ) )
512 ldap_memfree( val.bv_val );